Skip to content

feat(cmd): replay Claude Code transcripts offline and price every zero-cost component - #612

Merged
amiddavid merged 2 commits into
mainfrom
feat/transcript-replay
Oct 22, 2026
Merged

amiddavid merged 2 commits into
mainfrom
feat/transcript-replay

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Summary

This PR adds an offline replay. It answers one question: "what would each Context Guru component have saved on my own Claude Code sessions?" It sends recorded requests through the real pipeline, prices the result, and finds the best custom pipeline. It makes no model call and costs $0.

What changed

  • cmd/cg-transcript-replay (new Go command). It sends each rebuilt request through apply.BodyOpts, the same entry point the proxy uses. It uses the proxy's cache-boundary tracker and each call's own timestamp. Per call it writes:
    • tokens before and after;
    • whether the forwarded body still starts with the previous forwarded body (if not, that is a cache break);
    • the saving by component and by tool_use_id.
    • It takes -preset, -pipeline or -config.
  • scripts/analysis/cc_transcript_requests.py. It rebuilds the request sequence from a Claude Code transcript. It also tags each tool call by kind (Read, Bash view, other Bash, Edit) and marks a Read whose file is edited later.
  • scripts/analysis/cc_replay_sweep.py. It runs:
    • the shipped presets;
    • every zero-cost component alone (14 components);
    • non-default settings (readlifecycle stale_at_depth, collapse max_tokens, and others);
    • a greedy search for the best custom pipeline, once with no limits and once with a low-risk limit: 0 cache breaks and nothing removed from a file the agent edits later.
  • README section in scripts/analysis/README.md, with the limits of the method.

Why

Before this PR, a "what would it save" number came from one of two places:

  • the proxy's own replay, which needs the proxy's database;
  • a hand-made model that guesses what each component does.

A hand-made model can be badly wrong. On one internal workload it predicted 11–26%, and the shipped conservative preset measured 0.17%. The reason: that preset never touches file Reads, and the model assumed it did.

How it was tested

  • 3 unit tests on a synthetic round:
    • off preset: no change, no saving, no break (the control).
    • collapse: the saving goes to that component and to the correct tool_use_id.
    • readlifecycle stale_at_depth: the rewrite of a cached Read is reported as a cache break.
  • Mutation check: I turned off break detection, then tool attribution. In each case the matching test failed.
  • go vet and go test -race pass on the eval box.
  • On a real 30-session workload (4,624 calls), the rebuilt requests reproduce the billed cost exactly.

Open questions

  • Output tokens are fixed. A replay cannot show whether a lossy component makes the agent re-read a file or ask for an expand. Should the sweep add an estimate of that cost?
  • The rebuild stops on a subagent (sidechain) entry. Is multi-thread support worth adding?

Technical notes

  • Pricing per call k, with d_k tokens removed: cache read drops by d_{k-1} and cache write by d_k − d_{k-1}. A break moves the changed part of the previous body from cache read to cache write.
  • Break detection compares message hashes after it removes cache_control, so the moving trailing breakpoint does not count as a rewrite.
  • Out of scope: cachesplit, cacheinject, toolschema and toolfilter act on system and tools, which a transcript does not hold.
  • Not in the sweep: components that call a model (extract_llm, extract_llm_sweep, agentdiet, summarize, cache_aware_summarizer), and skeleton, which needs a build tag.

🤖 Generated with Claude Code

…e and price every zero-cost component

cmd/cg-transcript-replay sends a rebuilt request sequence through
apply.BodyOpts with the cache-boundary tracker and each call's own
timestamp. Per call it writes the tokens before and after, cache-prefix
breaks, and the saving by component and by tool_use_id. No network calls.

scripts/analysis/cc_transcript_requests.py rebuilds the requests from a
Claude Code transcript. scripts/analysis/cc_replay_sweep.py runs the
shipped presets, every zero-cost component, non-default settings, and a
greedy search for the best custom pipeline, with and without a low-risk
limit (0 cache breaks, nothing removed from a file edited later).

Assisted-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (bob, user of @amiddavid) IBM Bob Shell 1.0.3 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 edb0b43a8ade blocking

Verdict: blocking

Findings

  1. scripts/analysis/cc_replay_sweep.py:107,145 — Output message claims stricter filter than code enforces
    • What fails: The "best low-risk pipeline" title says "0% edited later" but the safe filter at line 107 allows edited_later_pct < 0.05 (5%)
    • Proof: Line 107: safe = lambda r: r["total"]["breaks"] == 0 and r["total"]["edited_later_pct"] < 0.05. Line 145 prints title from line 143: "best low-risk pipeline (0 breaks, 0% edited later)". A pipeline with 4.9% edited-later passes the filter but the output claims 0%.
    • Fix: Change line 107 to < 0.01 (effectively 0% with rounding) or change the title to "(0 breaks, <5% edited later)"

Non-blocking

None.

What I checked

  • Read CLAUDE.md and docs/design.md for repo rules
  • Read full diff of all three new files (main.go, main_test.go, cc_transcript_requests.py, cc_replay_sweep.py, README.md updates)
  • Verified integration with apply.BodyOpts: correct use of Tracker, Store, Mode, timestamp
  • Verified token counting uses normalized messages (cache_control stripped)
  • Verified cache break detection: hashes normalized messages, compares with previous
  • Verified error handling: "changed without run report" check is correct for this tool (no envelope rewrites)
  • Verified test coverage: off preset control, attribution, cache break detection
  • Verified Python pricing logic: cache read/write adjustments for removed tokens and breaks
  • Verified greedy search: stops at 0.01 percentage point, safe filter for low-risk
  • Verified data dependence: offline tool, Store usage is appropriate
  • Verified fail-open: tool makes no network calls, cannot damage production
  • Verified documentation: README.md clearly states limitations (output tokens fixed, no expand modeling, one thread only)

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (bob, user of @amiddavid) IBM Bob Shell 1.0.3 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 edb0b43a8ade blocking

Verdict: blocking

Findings

  1. cmd/cg-transcript-replay/main.go:209 — Wrong field access causes compilation error
    • What fails: The code accesses res.Trace.CacheAware but apply.Result embeds Trace directly as an anonymous field, not as a named field. This code will not compile.
    • Proof: Line 209 reads o.Cold = res.Run != nil && res.Trace.CacheAware && res.MaxCachedIdx < 0 && k > 0. In apply/opts.go:109-114, Result is defined as:
      type Result struct {
          Body []byte
          Changed bool
          Trace
      }
      The Trace field is embedded (anonymous), so its fields are promoted to Result. The correct access is res.CacheAware, not res.Trace.CacheAware. This is confirmed by examining other uses in the codebase: apply/responses_test.go:318 uses res.CacheAware and res.MaxCachedIdx directly, and apply/responses.go:549 assigns to res.CacheAware and res.MaxCachedIdx directly.
    • Fix: Change line 209 to o.Cold = res.Run != nil && res.CacheAware && res.MaxCachedIdx < 0 && k > 0

Non-blocking

  • The Python scripts (cc_transcript_requests.py and cc_replay_sweep.py) have sound logic for their stated purpose. The pricing model in cc_replay_sweep.py correctly accounts for cache breaks by moving tokens from cache-read to cache-write tier.
  • The test coverage in main_test.go is adequate: it tests the control case (off preset), attribution (collapse), and cache breaks (readlifecycle with stale_at_depth).
  • Documentation in scripts/analysis/README.md clearly states the limitations: output tokens are fixed, "edited later" is the quality risk, only messages are replayed, and it's single-threaded with no compaction.

What I checked

  • Read CLAUDE.md and docs/design.md for repo rules
  • Read the full diff (3 new files: main.go, main_test.go, and updates to analysis scripts)
  • Examined cmd/cg-transcript-replay/main.go line by line for correctness
  • Verified the field access pattern by searching for res.CacheAware and res.Trace. usage across the codebase
  • Checked apply.Result struct definition in apply/opts.go
  • Reviewed test coverage in main_test.go
  • Examined Python script logic in cc_transcript_requests.py and cc_replay_sweep.py
  • Verified documentation updates in scripts/analysis/README.md

The report title said "0% edited later" while the filter allows less
than 0.05% (edited_later_pct is a percentage). The limit is now a named
constant and the title prints it.

Assisted-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Replies to the round-1 findings.

Reviewer 1, finding 1 (res.Trace.CacheAware does not compile): rejected.

  • Go lets you name an embedded field by its type name. res.Trace.CacheAware and res.CacheAware are the same field. The Go spec says: "The unqualified type name acts as the field name."
  • Proof: on head edb0b43a, go vet ./cmd/cg-transcript-replay and go test -race ./cmd/cg-transcript-replay passed on the eval box (3 of 3 tests). The darwin/arm64 binary also built and ran.
  • No code change.

Reviewer 2, finding 1 (title says "0% edited later", filter allows < 0.05): fixed in a143176.

  • edited_later_pct is a percentage, so the limit was 0.05% and not 5%. The title still did not say the real limit.
  • The limit is now the named constant LOW_RISK_EDITED_PCT, and the title prints "<0.05% of removed tokens edited later".

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (bob, user of @amiddavid) IBM Bob Shell 1.0.3 offload-review skill, @amiddavid's session resumed 2 of 2 2 of 2 a143176773ff approve

Verdict: approve

Findings

None. My round-1 finding was fixed.

Non-blocking

None.

What I checked

  • Last review: round 1, edb0b43, 2026-10-20T18:56:55Z
  • New commits read: 1 (a143176 "fix(scripts): name the low-risk limit and print it exactly")
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files)
  • New items since my last review:
    • Commit a143176: Fixes my finding by introducing LOW_RISK_EDITED_PCT constant and updating title to "<0.05% of removed tokens edited later"
    • Author's comment: Explains the fix and rejects reviewer 1's finding with valid Go spec reasoning
  • Verified the fix:
    • Line 140: LOW_RISK_EDITED_PCT = 0.05 makes the limit explicit
    • Line 139: Comment clarifies "0.05% of removed tokens" (percentage, not 5%)
    • Line 154: Title now prints the exact limit: "best low-risk pipeline (0 breaks, <0.05% of removed tokens edited later)"
    • The filter at line 141 uses the named constant
  • No new defects found in the fix commit

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (bob, user of @amiddavid) IBM Bob Shell 1.0.3 offload-review skill, @amiddavid's session resumed 1 of 2 2 of 2 a143176773ff approve

Verdict: approve

Findings

None.

Status of round-1 findings

My finding 1 (cmd/cg-transcript-replay/main.go:209, res.Trace.CacheAware compilation error): rejected, and I was wrong.

The author is correct. In Go, an embedded field can be accessed both by its promoted field names (res.CacheAware) and by its type name as a field name (res.Trace.CacheAware). Both are valid and equivalent. The Go spec states: "The unqualified type name acts as the field name." The code compiles and tests pass. I misunderstood Go's embedded field semantics.

Reviewer 2's finding 1 (scripts/analysis/cc_replay_sweep.py output message mismatch): fixed in a143176.

The title now correctly states "<0.05% of removed tokens edited later" and the limit is a named constant LOW_RISK_EDITED_PCT = 0.05. The comment clarifies that edited_later_pct is a percentage, so the limit is 0.05% (not 5%). The fix is correct.

Non-blocking

None.

What I checked

  • Last review: round 1, edb0b43, 2026-10-20T19:01:00Z
  • New commits read: 1 (a143176 "fix(scripts): name the low-risk limit and print it exactly")
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files)
  • New items since my last review:
    • Author's comment at 2026-10-20T19:02:20Z: Read in full. Checked the rejection of my finding 1 and verified the author is correct about Go embedded field access. Verified the fix for reviewer 2's finding 1.
    • Commit a143176: Read the diff. The change adds LOW_RISK_EDITED_PCT = 0.05 constant and updates the title to print the exact limit. The fix is correct and addresses reviewer 2's finding.
  • Verified the new commit:
    • Line 140: LOW_RISK_EDITED_PCT = 0.05 is now a named constant
    • Line 141: The safe lambda uses the constant
    • Line 154: The title now says "best low-risk pipeline (0 breaks, <0.05% of removed tokens edited later)"
    • The comment at line 139 clarifies that edited_later_pct is a percentage
  • Re-examined cmd/cg-transcript-replay/main.go:209: res.Trace.CacheAware is valid Go syntax for accessing an embedded field's member. No change needed.
  • No new defects found in commit a143176

@OsherElhadad OsherElhadad mentioned this pull request Oct 21, 2026
3 of 5 tasks
@amiddavid
amiddavid merged commit 50d56e4 into main Oct 22, 2026
4 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant