Skip to content

fix(reference): Fix line attribution accuracy and add Claude Code model extraction#7

Open
muraalee wants to merge 1 commit intocursor:mainfrom
muraalee:fix/claude-code-attribution
Open

fix(reference): Fix line attribution accuracy and add Claude Code model extraction#7
muraalee wants to merge 1 commit intocursor:mainfrom
muraalee:fix/claude-code-attribution

Conversation

@muraalee
Copy link

@muraalee muraalee commented Feb 3, 2026

Summary

This PR fixes two correctness issues in the reference implementation affecting Claude Code trace accuracy:

  1. Over-attribution of context lines - Edits were claiming unchanged surrounding lines
  2. Missing model identification - Claude Code traces had no model_id

Problem Details

Issue 1: Line Attribution

Claude Code's Edit tool includes context lines in both old_string and new_string. The previous implementation attributed the entire new_string, incorrectly claiming ownership of unchanged lines.

Before: Adding 1 line → ranges: [{start_line: 1, end_line: 3}]
After: Adding 1 line → ranges: [{start_line: 2, end_line: 2}]

Issue 2: Model Identification

Unlike Cursor, Claude Code does not include model in hook payloads. The model must be extracted from the transcript JSONL file at entry.message.model.

Solution

trace-store.ts

  • diffToFindChangedLines() - Compares old/new strings to identify only changed lines
  • extractModelFromTranscript() - Reads model from transcript tail (4KB initial read, doubles if needed)

trace-hook.ts

  • resolveModel() - Transparently resolves model from payload (Cursor) or transcript (Claude Code)
  • Updated PostToolUse, SessionStart, SessionEnd handlers

Test Results

Tested with alternating Cursor and Claude Code edits in a sample repository:

Tool Model Lines Verified
Cursor openai/gpt-5.2-codex 67-70
Claude Code anthropic/claude-opus-4-5-20251101 71-74
Cursor openai/gpt-5.2-codex 75-78
Claude Code anthropic/claude-opus-4-5-20251101 79-82

All traces correctly captured:

  • Accurate line ranges matching actual file content
  • Model identification present for both tools
  • Transcript URLs properly linked

Backward Compatibility

  • Cursor's afterFileEdit unchanged (uses explicit ranges from payload)
  • Diff logic only activates when both old_string and new_string are present
  • Model resolution falls back gracefully when transcript unavailable

@muraalee muraalee force-pushed the fix/claude-code-attribution branch 2 times, most recently from 7962a28 to f553217 Compare February 3, 2026 02:13
… model extraction

The reference implementation had two issues affecting trace accuracy:

1. Line Attribution: When processing edits with context lines (common in
   Claude Code's Edit tool), the entire new_string was attributed to AI,
   including unchanged surrounding lines. This produced inflated attribution
   ranges.

2. Model Identification: Claude Code does not include the model identifier
   in hook payloads. Traces were created with missing model_id, making it
   impossible to distinguish which model produced the code.

Changes:
- Add diffToFindChangedLines() to compute actual changed lines by comparing
  old_string and new_string, excluding context lines from attribution
- Add extractModelFromTranscript() to parse Claude Code's JSONL transcript
  files and extract the model identifier from message entries
- Add resolveModel() helper to transparently handle model resolution for
  both Cursor (direct payload) and Claude Code (transcript extraction)
- Update PostToolUse, SessionStart, and SessionEnd handlers to use the
  new model resolution logic
@muraalee muraalee force-pushed the fix/claude-code-attribution branch from f553217 to f7eb887 Compare February 3, 2026 02:14
@muraalee muraalee changed the title fix(reference): improve line attribution accuracy and add Claude Code fix(reference): Fix line attribution accuracy and add Claude Code model extraction Feb 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant