This workflow uses Claude Code Hooks to automatically capture the context of AI code edits, before-and-after snapshots, and diffs, then uses a lightweight model to generate concise design-intent summaries. Over time, these records become reusable material for building coding standards. The approach solves a common problem: AI can produce correct code, but not code that sounds like you wrote it. Keywords: Claude Code, Hook, coding standards accumulation.
This mechanism continuously feeds your personal coding habits back into AI
| Parameter | Description |
|---|---|
| Primary languages | Python, Markdown, JSON |
| Runtime environment | Claude Code Hook |
| Core protocols/interfaces | stdin JSON, filesystem snapshots, unified diff |
| GitHub stars | Not provided in the original article |
| Core dependencies | Python standard library difflib, Claude Haiku, ~/.claude/settings.json |
The most common pain point with AI-generated code is not that the model cannot write code. It is that the code does not look like your code. Naming, null handling, branch structure, and comment placement can all drift away from personal or team conventions, which forces manual correction every time.
The key value of this approach is that it automatically records the exact moments when a change was important enough to intervene. Compared with writing a full coding standard up front, this method maps better to real development work and is easier to accumulate over time.
The core idea turns every AI edit into a reviewable asset
The workflow has four steps: detect a code-editing conversation worth recording, capture a snapshot before the change, overwrite and save the snapshot after the change, and generate a diff and summary at the end of the session, then archive everything into a timestamped Markdown file.
# Minimal logic for tracking an intentional optimization session
KEYWORDS = ['优化', '重构', '改进']
# Initialize tracking only when a keyword matches to reduce noise
has_keyword = any(kw in prompt for kw in KEYWORDS)
if has_keyword:
create_tracking_round(session_id, prompt, workdir)
This code uses keywords to trigger tracking so the system does not record every conversation indiscriminately.
The Hook event chain automatically links pre-change and post-change states
The original implementation relies on four Claude Code Hook event types: UserPromptSubmit, PreToolUse, PostToolUse, and Stop. They correspond to intent detection, pre-edit capture, post-edit capture, and session finalization.
AI Visual Insight: This diagram shows a typical event-driven data flow: when the user submits a prompt, the system initializes tracking state; before a file edit, it creates the original snapshot; after the edit, it refreshes the final snapshot; and in the Stop event, it consolidates context, builds the diff, calls a model to generate a summary, and writes the record into the archive directory.
The main advantage of this design is low intrusion. Developers do not need to change their existing workflow. They only need to configure Hooks, and the system can keep accumulating evidence of personal coding style in the background.
{
"hooks": {
"UserPromptSubmit": [{
"matcher": "",
"hooks": [{"type": "command", "command": "python3 ~/.claude/hooks/opt_track.py"}]
}],
"PreToolUse": [{
"matcher": "Edit|Write|MultiEdit",
"hooks": [{"type": "command", "command": "python3 ~/.claude/hooks/opt_capture_before.py"}]
}],
"PostToolUse": [{
"matcher": "Edit|Write|MultiEdit",
"hooks": [{"type": "command", "command": "python3 ~/.claude/hooks/opt_capture_after.py"}]
}],
"Stop": [{
"hooks": [{"type": "command", "command": "python3 ~/.claude/hooks/opt_write_record.py"}]
}]
}
}
This configuration binds four scripts to different events and acts as the integration entry point for the entire workflow.
Four scripts handle tracking, snapshots, and summarization
opt_track.py creates a tracking round and writes the prompt, working directory, and session index into a temporary directory. If no keyword matches but there is already an active round, it appends the new prompt as an execution step so the system preserves context.
opt_capture_before.py is designed around one rule: capture each file only once per round. That way, even if the AI modifies the same file multiple times, the final diff still reflects the full change from the original state to the final state.
Post-edit snapshots and diff generation make records comparable
opt_capture_after.py overwrites the .after file after each write and keeps only the final result. Paired with .before, it creates a stable before/after set that allows the system to generate a unified diff directly when the session ends.
import difflib
# Generate a unified diff between before and after versions for summaries and review
diff = list(difflib.unified_diff(
before_lines,
after_lines,
fromfile='修改前',
tofile='修改后',
n=3 # Keep context lines for readability
))
This code generates a standard unified diff as the factual basis for describing what changed.
During the summary phase, the original article uses a lightweight model such as Claude Haiku only to compress the intent, execution process, and diff information from the current round. This keeps costs low while still supporting later human review.
summary_prompt = (
'The following content comes from a code optimization session. Please summarize it in 2 to 4 sentences:'
' what changed most significantly, and which code design principles or best practices it reflects.\n\n'
+ context + '\n\nOutput only the summary and nothing else.'
)
This code sends the intent, process, and diff to the model and produces a short summary suitable for later review.
Archived Markdown records become the real starting point for coding standards
The output is not meant to exist only as an archive. Its real purpose is periodic extraction. The author recommends reviewing the records once every two weeks, identifying high-frequency patterns under 代码规范积累/YYYY-MM/, and then rewriting them into a formal 代码风格.md file or a project-level CLAUDE.md.
That creates a closed loop: AI edits code, the system records it automatically, humans distill rules from the records, and those rules then constrain future AI output. The standards are not invented in the abstract. They are extracted from real behavior, which makes them more stable and more aligned with actual team practice.
This feedback loop is more sustainable than writing standards once
Compared with drafting an abstract style guide in advance, this method is better at capturing fine-grained preferences such as “handle nulls early instead of creating deep nesting,” “reduce if/else branching in the Service layer,” or “remove meaningless do prefixes.”
Its limitations are also clear. If you trigger tracking only with terms such as “optimization,” “refactor,” or “improve,” you will miss many valuable changes that use different wording. A more robust next step is to record every conversation that results in file modifications, then filter during the review stage.
Installation and runtime constraints must be understood clearly
Hook scripts receive JSON through stdin. Typical fields include session_id, tool_input, and prompt. In addition, if PreToolUse returns a non-zero status, it will directly block the tool invocation. That means snapshot scripts should exit silently when they cannot find tracking state instead of raising an error that interrupts execution.
In practice, script design should prioritize robustness: create missing directories automatically, skip safely when state is unavailable, escape and isolate file names, and strictly separate storage across sessions to avoid polluting historical records.
AI Visual Insight: This image is a QR-code-style promotional graphic for a mini app. Its technical relevance is limited to distribution and user entry points, and it does not directly participate in the Hook architecture, snapshot capture, or diff generation flow discussed in this article.
FAQ: The three questions developers care about most
1. Why not write all coding standards directly into CLAUDE.md once?
Because the details that determine whether code truly feels like your own often appear inside concrete edits, and those details are hard to enumerate completely in advance. Automatic recording captures these high-value, low-frequency, but critical preferences.
2. Why save both before and after states instead of only the final file?
Because only by preserving both states can you generate an explainable diff. The diff becomes the factual foundation for later summaries, reviews, and coding-standard extraction, which is far more valuable than saving the final result alone.
3. Is a lightweight model accurate enough for summarization?
For record triage, it usually is. The goal of summarization is not to replace human judgment, but to help you quickly decide whether a change is worth promoting into formal coding standards.
Core summary: This article reconstructs a practical Claude Code Hook workflow that automatically captures before-and-after snapshots of AI code edits, generates diffs and summaries, and stores them as reviewable Markdown records. Those records can then feed back into CLAUDE.md, helping AI stay closer to your personal coding style over time.