|
| 1 | +# How It Works |
| 2 | + |
| 3 | +This document explains the internal workings of `ai-blame`. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +```mermaid |
| 8 | +flowchart LR |
| 9 | + A[Claude Code Traces] --> B[Extractor] |
| 10 | + B --> C[Edit Records] |
| 11 | + C --> D[Filters] |
| 12 | + D --> E[File Histories] |
| 13 | + E --> F[Updater] |
| 14 | + F --> G[Modified Files] |
| 15 | +``` |
| 16 | + |
| 17 | +## Step 1: Locate Trace Files |
| 18 | + |
| 19 | +Claude Code stores execution traces in: |
| 20 | + |
| 21 | +``` |
| 22 | +~/.claude/projects/<encoded-cwd>/ |
| 23 | +``` |
| 24 | + |
| 25 | +Where `<encoded-cwd>` is the project path with `/` replaced by `-`. |
| 26 | + |
| 27 | +**Example:** |
| 28 | + |
| 29 | +| Project Path | Trace Directory | |
| 30 | +|--------------|-----------------| |
| 31 | +| `/Users/alice/myproject` | `~/.claude/projects/-Users-alice-myproject/` | |
| 32 | +| `/home/bob/work/repo` | `~/.claude/projects/-home-bob-work-repo/` | |
| 33 | + |
| 34 | +Each session generates a JSONL file (e.g., `a1b2c3d4-5678-90ab-cdef.jsonl`). |
| 35 | + |
| 36 | +## Step 2: Parse Trace Files |
| 37 | + |
| 38 | +Each trace file contains a sequence of JSON records representing the conversation and tool usage: |
| 39 | + |
| 40 | +```json |
| 41 | +{"type": "user", "uuid": "msg-1", "message": {...}} |
| 42 | +{"type": "assistant", "uuid": "msg-2", "message": {...}, "model": "claude-opus-4-5"} |
| 43 | +{"type": "user", "uuid": "msg-3", "toolUseResult": {...}} |
| 44 | +``` |
| 45 | + |
| 46 | +The extractor looks for **successful Edit/Write operations** by finding `toolUseResult` entries with: |
| 47 | + |
| 48 | +- A `filePath` field |
| 49 | +- Either `structuredPatch` (for edits) or `type: create` (for new files) |
| 50 | +- No error indicators |
| 51 | + |
| 52 | +```python |
| 53 | +def is_successful_edit(record: dict) -> bool: |
| 54 | + if record.get("type") != "user": |
| 55 | + return False |
| 56 | + |
| 57 | + tool_result = record.get("toolUseResult") |
| 58 | + if not tool_result or not isinstance(tool_result, dict): |
| 59 | + return False |
| 60 | + |
| 61 | + if tool_result.get("is_error") or tool_result.get("error"): |
| 62 | + return False |
| 63 | + |
| 64 | + file_path = tool_result.get("filePath", "") |
| 65 | + if not file_path: |
| 66 | + return False |
| 67 | + |
| 68 | + has_patch = "structuredPatch" in tool_result |
| 69 | + is_create = tool_result.get("type") == "create" |
| 70 | + |
| 71 | + return has_patch or is_create |
| 72 | +``` |
| 73 | + |
| 74 | +## Step 3: Extract Metadata |
| 75 | + |
| 76 | +For each successful edit, we extract: |
| 77 | + |
| 78 | +| Field | Source | |
| 79 | +|-------|--------| |
| 80 | +| `file_path` | `toolUseResult.filePath` | |
| 81 | +| `timestamp` | Record's `timestamp` field | |
| 82 | +| `model` | Parent message's `message.model` | |
| 83 | +| `session_id` | Record's `sessionId` | |
| 84 | +| `is_create` | `toolUseResult.type == "create"` | |
| 85 | +| `change_size` | Calculated from content/patch | |
| 86 | +| `agent_version` | Record's `version` field | |
| 87 | + |
| 88 | +The **model** is found by looking up the parent message (the assistant message that invoked the tool). |
| 89 | + |
| 90 | +## Step 4: Apply Filters |
| 91 | + |
| 92 | +Filters reduce the volume of edit records: |
| 93 | + |
| 94 | +### File Pattern Filter |
| 95 | + |
| 96 | +Keeps only files matching a substring: |
| 97 | + |
| 98 | +```python |
| 99 | +if file_pattern and file_pattern not in file_path: |
| 100 | + continue # Skip this record |
| 101 | +``` |
| 102 | + |
| 103 | +### Time Range Filters |
| 104 | + |
| 105 | +```python |
| 106 | +if config.since and edit.timestamp < config.since: |
| 107 | + continue |
| 108 | +if config.until and edit.timestamp > config.until: |
| 109 | + continue |
| 110 | +``` |
| 111 | + |
| 112 | +### Size Filter |
| 113 | + |
| 114 | +Skip small edits (likely typo fixes): |
| 115 | + |
| 116 | +```python |
| 117 | +if config.min_change_size > 0: |
| 118 | + edits = [e for e in edits if e.change_size >= config.min_change_size] |
| 119 | +``` |
| 120 | + |
| 121 | +### Initial and Recent Only |
| 122 | + |
| 123 | +Keep only the first and last edit per file: |
| 124 | + |
| 125 | +```python |
| 126 | +if config.initial_and_recent_only and len(edits) > 2: |
| 127 | + edits = [edits[0], edits[-1]] |
| 128 | +``` |
| 129 | + |
| 130 | +## Step 5: Convert to File Histories |
| 131 | + |
| 132 | +Edit records are grouped by file and converted to `FileHistory` objects: |
| 133 | + |
| 134 | +```python |
| 135 | +for abs_path, edits in edits_by_file.items(): |
| 136 | + rel_path = normalize_path(abs_path, repo_root) |
| 137 | + |
| 138 | + events = [] |
| 139 | + for i, edit in enumerate(edits): |
| 140 | + action = CurationAction.CREATED if (i == 0 and edit.is_create) else CurationAction.EDITED |
| 141 | + |
| 142 | + events.append(CurationEvent( |
| 143 | + timestamp=edit.timestamp, |
| 144 | + model=edit.model, |
| 145 | + action=action, |
| 146 | + agent_tool=edit.agent_tool, |
| 147 | + agent_version=edit.agent_version, |
| 148 | + )) |
| 149 | + |
| 150 | + histories[rel_path] = FileHistory(file_path=rel_path, events=events) |
| 151 | +``` |
| 152 | + |
| 153 | +## Step 6: Load Output Configuration |
| 154 | + |
| 155 | +The configuration determines how each file type is handled: |
| 156 | + |
| 157 | +```python |
| 158 | +# Auto-find .ai-blame.yaml |
| 159 | +config_path = find_config() |
| 160 | +if config_path: |
| 161 | + output_config = load_config(config_path) |
| 162 | +else: |
| 163 | + output_config = get_default_config() |
| 164 | +``` |
| 165 | + |
| 166 | +Rules are matched using glob patterns: |
| 167 | + |
| 168 | +```python |
| 169 | +def get_rule_for_file(self, path: str) -> FileRule | None: |
| 170 | + filename = Path(path).name |
| 171 | + |
| 172 | + for rule in self.rules: |
| 173 | + if "/" in rule.pattern or "**" in rule.pattern: |
| 174 | + if fnmatch(path, rule.pattern): |
| 175 | + return rule |
| 176 | + else: |
| 177 | + if fnmatch(filename, rule.pattern): |
| 178 | + return rule |
| 179 | + |
| 180 | + return self.defaults |
| 181 | +``` |
| 182 | + |
| 183 | +## Step 7: Apply Changes |
| 184 | + |
| 185 | +Based on the output policy: |
| 186 | + |
| 187 | +### Append Policy |
| 188 | + |
| 189 | +For YAML files, append a `edit_history` section: |
| 190 | + |
| 191 | +```python |
| 192 | +curation_yaml = generate_curation_yaml(history) |
| 193 | +new_content = content + "\n" + curation_yaml |
| 194 | +file_path.write_text(new_content) |
| 195 | +``` |
| 196 | + |
| 197 | +If `edit_history` already exists, it's replaced. |
| 198 | + |
| 199 | +### Sidecar Policy |
| 200 | + |
| 201 | +Write to a companion file: |
| 202 | + |
| 203 | +```python |
| 204 | +sidecar_path = resolve_sidecar_path(file_path, pattern) |
| 205 | +# e.g., main.py → main.history.yaml |
| 206 | + |
| 207 | +sidecar_data = { |
| 208 | + "source_file": file_path.name, |
| 209 | + "edit_history": events, |
| 210 | +} |
| 211 | +sidecar_path.write_text(yaml.dump(sidecar_data)) |
| 212 | +``` |
| 213 | + |
| 214 | +Existing sidecar files are merged (deduplicated by timestamp). |
| 215 | + |
| 216 | +### Comment Policy |
| 217 | + |
| 218 | +Embed as a comment block: |
| 219 | + |
| 220 | +```python |
| 221 | +if syntax == CommentSyntax.HASH: |
| 222 | + marker_start = "# --- edit_history ---" |
| 223 | + marker_end = "# --- end edit_history ---" |
| 224 | + commented = "\n".join(f"# {line}" for line in history_yaml.split("\n")) |
| 225 | + |
| 226 | +new_content = content + "\n" + comment_block |
| 227 | +``` |
| 228 | + |
| 229 | +## Data Flow Summary |
| 230 | + |
| 231 | +```mermaid |
| 232 | +flowchart TB |
| 233 | + subgraph Input |
| 234 | + T[Trace Files<br>~/.claude/projects/...] |
| 235 | + C[Config File<br>.ai-blame.yaml] |
| 236 | + end |
| 237 | +
|
| 238 | + subgraph Processing |
| 239 | + E[Extractor<br>parse_trace_file] |
| 240 | + F[Filter<br>apply_filters] |
| 241 | + H[Converter<br>convert_to_file_histories] |
| 242 | + end |
| 243 | +
|
| 244 | + subgraph Output |
| 245 | + A[Append<br>YAML/JSON files] |
| 246 | + S[Sidecar<br>*.history.yaml] |
| 247 | + M[Comment<br>Code files] |
| 248 | + end |
| 249 | +
|
| 250 | + T --> E |
| 251 | + E --> F |
| 252 | + F --> H |
| 253 | + C --> R[Rule Matcher] |
| 254 | + H --> R |
| 255 | + R -->|append| A |
| 256 | + R -->|sidecar| S |
| 257 | + R -->|comment| M |
| 258 | +``` |
0 commit comments