fix(reference): Fix line attribution accuracy and add Claude Code model extraction#7
Open
muraalee wants to merge 1 commit intocursor:mainfrom
Open
fix(reference): Fix line attribution accuracy and add Claude Code model extraction#7muraalee wants to merge 1 commit intocursor:mainfrom
muraalee wants to merge 1 commit intocursor:mainfrom
Conversation
7962a28 to
f553217
Compare
… 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
f553217 to
f7eb887
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes two correctness issues in the reference implementation affecting Claude Code trace accuracy:
model_idProblem Details
Issue 1: Line Attribution
Claude Code's
Edittool includes context lines in bothold_stringandnew_string. The previous implementation attributed the entirenew_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
modelin hook payloads. The model must be extracted from the transcript JSONL file atentry.message.model.Solution
trace-store.ts
diffToFindChangedLines()- Compares old/new strings to identify only changed linesextractModelFromTranscript()- 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)PostToolUse,SessionStart,SessionEndhandlersTest Results
Tested with alternating Cursor and Claude Code edits in a sample repository:
All traces correctly captured:
Backward Compatibility
afterFileEditunchanged (uses explicit ranges from payload)old_stringandnew_stringare present