Skip to content

Fix stale cache reads on equal-size, same-mtime transcript rewrites - #572

Merged
anilmurty merged 6 commits into
Metabuilder-Labs:mainfrom
Vam-si-krish:fix/issue-560-contribpilot
Jul 30, 2026
Merged

anilmurty merged 6 commits into
Metabuilder-Labs:mainfrom
Vam-si-krish:fix/issue-560-contribpilot

Conversation

@Vam-si-krish

@Vam-si-krish Vam-si-krish commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

The transcript cache previously trusted (size, mtime), which can miss equal-size in-place rewrites. This adds a full-content fingerprint while keeping cache misses and concurrent rewrites safe.

Summary

  • Stream SHA-256 over the complete transcript so middle-only rewrites invalidate the cache.
  • Compute the fingerprint lazily only when a loaded entry already matches size and mtime.
  • Re-stat and re-fingerprint after parsing; skip the cache write when the source changed during the parse.
  • Preserve deliberate None fingerprints for unreadable-but-stattable files while invalidating both readable-to-unreadable and unreadable-to-readable transitions.
  • Document that cache hits still read the source bytes for hashing but avoid the more expensive JSONL parse.

Issue #560: equal-size transcript rewrites

The original metadata-only validity check could return stale parsed records when content changed without changing file size or modification time. A streaming whole-file digest closes that gap, including changes located away from the head and tail of large transcripts.

The revalidation step also prevents a digest observed before parsing from being stored beside records produced from different content. If the source moves while parsing, the fresh records are returned to the caller but no internally inconsistent cache entry is persisted.

Tests / Verification

  • .venv/bin/python -m pytest tests/unit/test_transcript_cache.py -q — 18 passed
  • .venv/bin/ruff check tokenjam/ — passed
  • .venv/bin/mypy tokenjam/ — 268 source files checked, no issues
  • Full non-e2e suite — 5,062 passed, 7 skipped
  • Six unrelated quickstart/summarize failures in the full run were reproduced identically on a clean origin/main worktree; none touch the transcript cache.

What's NOT in this PR

  • No changes to transcript parsing or the opt-in cache call sites.
  • No attempt to fix the six unrelated failures currently present on main.
  • No bounded head/tail sampling; every byte is intentionally hashed so middle-only rewrites are visible.

Closes #560

🤖 Generated with OpenAI Codex

Co-Authored-By: anshss [email protected]

@greptile-apps

greptile-apps Bot commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR strengthens transcript-cache validation and race handling.

  • Replaces edge sampling with a streaming SHA-256 fingerprint of the complete transcript.
  • Lazily fingerprints metadata-matching cache entries and preserves deliberate None fingerprints for unreadable files.
  • Revalidates metadata and, on warm-cache misses, content after parsing before storing records.
  • Adds regression tests for equal-metadata rewrites, middle-only changes, unreadable transitions, legacy entries, and warm-cache parse races.

Confidence Score: 3/5

The PR is not yet safe to merge because cold and metadata-mismatched reads can persist records from one transcript version under another version's fingerprint.

When no metadata-matching entry exists, the implementation parses before taking its only fingerprint and skips content comparison during revalidation; an equal-size, same-mtime rewrite between those reads can consequently create a cache entry that later returns stale or incomplete records.

Files Needing Attention: tokenjam/core/transcript_cache.py, tests/unit/test_transcript_cache.py

Important Files Changed

Filename Overview
tokenjam/core/transcript_cache.py Adds complete-content hashing and post-parse cache validation, but the previously reported cold/metadata-mismatch version-pairing race remains outstanding.
tests/unit/test_transcript_cache.py Adds broad regression coverage for fingerprint invalidation and warm-cache races, without covering the outstanding cold or metadata-mismatched race.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant Cache as cached_read_records
    participant Source as Transcript
    participant Store as Cache file
    Caller->>Cache: read(path)
    Cache->>Source: stat
    Cache->>Store: load entry
    alt metadata-matching entry
        Cache->>Source: fingerprint before parse
        alt fingerprint matches
            Cache-->>Caller: cached records
        else fingerprint differs
            Cache->>Source: parse records
            Cache->>Source: stat and fingerprint after parse
            Cache->>Store: store only if revalidation succeeds
        end
    else cold or metadata-mismatched entry
        Cache->>Source: parse records
        Cache->>Source: stat and fingerprint after parse
        Cache->>Store: store when metadata remains equal
    end
Loading

Reviews (5): Last reviewed commit: "Merge branch 'main' into fix/issue-560-c..." | Re-trigger Greptile

Comment thread tokenjam/core/transcript_cache.py Resolved
Comment thread tokenjam/core/transcript_cache.py Resolved
Comment thread tokenjam/core/transcript_cache.py Resolved
@anshss

anshss commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

@Vam-si-krish Thanks for this pr bro! A few things before merge:

  1. Make the fingerprint lazy. It's computed unconditionally before _load, so
    every cache miss hashes the whole file and then _parse_records immediately reads
    it again. Move it inside the hit check, after size and mtime already match.

  2. Close the store-time gap. The fingerprint is taken before _parse_records, so
    a rewrite landing between the two stores one version's fingerprint against another
    version's records. Re-stat and re-fingerprint after the parse, and skip the store if
    anything moved.

  3. Fix test_unreadable_file_still_reuses_its_cache_entry. It patches _fingerprint
    to None at warm time as well, so it never exercises the real
    warm-then-becomes-unreadable transition it's named for.

  4. Update the PR body. It still describes the 4096-byte head/tail design and its I/O
    bound, which the current code no longer does. Please also drop the line about the
    suite failing on the upstream base: it's green at 491d13f7 (3643 passed, 1 skipped).

  5. Note the tradeoff in the module docstring. Hashing every byte means a cache hit
    now skips the parse but no longer skips the read. Worth stating explicitly so the
    next reader doesn't re-litigate it.

anshss added a commit to anshss/tokenjam that referenced this pull request Jul 25, 2026
Agentic contribution tools (Cursor background agents and similar) poll
GitHub's search API on `is:open no:assignee label:"good first issue"` —
they don't read claim comments. When 15 issues got both labels in one
burst, two different tools opened PRs for the same issue within 50
minutes of each other because neither tool's feed reflected the other's
claim (public PRs Metabuilder-Labs#572, Metabuilder-Labs#573 against the same issue Metabuilder-Labs#560).

Removing the labels the instant a closing PR opens is the lever that
actually keeps an issue off those feeds; a comment changes nothing they
read. Labels are restored, and only the exact ones this workflow itself
removed, if that PR is later closed without merging.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@anilmurty

Copy link
Copy Markdown
Contributor

welcome and thanks for the contribution @Vam-si-krish !

@anilmurty

Copy link
Copy Markdown
Contributor

Adding to @anshss's review (his 5 points are the right gate — lazy fingerprint, the store-time divergence gap, the unreadable-file test, the PR body, and the docstring tradeoff all stand). Two things his list doesn't cover:

  1. CI hasn't actually run yet. The Python test matrix (test (3.10/3.11/3.12)) is still pending — this is an external-fork PR, so a maintainer needs to approve the workflow run first. Let's get it green before merging; the new fingerprint path and its regression tests need to be verified on CI, not just locally.

  2. Cover BOTH unreadable↔readable transitions, not just one. Ansh's point 3 is about the warm-then-becomes-unreadable test. The mirror case matters too: a file that was unreadable at warm time (stored fingerprint = None) and later becomes readable must invalidate (None vs a real hash), while a still-unreadable file must reuse (None vs None). Please make sure the equality logic handles both directions and add a test for the unreadable→readable transition — that's the one most likely to silently serve stale records.

Not merging yet — for the maintainers once CI is green and the above + Ansh's points are addressed.

@anilmurty anilmurty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

noted in the comments

anilmurty and others added 2 commits July 25, 2026 13:39
Delay hashing until metadata suggests a cache hit, then revalidate the source after parsing before persisting records. This prevents a digest from being paired with records parsed from different content while preserving readable and unreadable cache transitions.

ContribPilot: CP-0023
Issue: Metabuilder-Labs#560

Co-Authored-By: anshss <[email protected]>
Co-Authored-By: OpenAI GPT-5 <[email protected]>
@anilmurty

Copy link
Copy Markdown
Contributor

Thanks @Vam-si-krish. The whole-file SHA-256 fingerprint is the right direction and it does fix the middle-rewrite case. But the last round of feedback isn't addressed yet — the last code commit (afc95df) predates both reviews, and the only commit since is a bare main merge. Still open:

  1. Store-time divergence (Greptile P1): _fingerprint(path) and _parse_records(path) are two separate reads; a rewrite landing between them stores version-A's fingerprint with version-B's records, so a later lookup of version-A serves version-B's stale records. Re-stat + re-fingerprint after the parse, and skip the store if anything moved.
  2. Lazy the fingerprint: it currently hashes the whole file on every call before _load. Move it inside the hit check, after size+mtime already match — otherwise every cache miss hashes and then re-reads the same file.
  3. test_unreadable_file_still_reuses_its_cache_entry patches _fingerprint → None at warm time, so it never exercises the warm-then-becomes-unreadable transition it claims to cover.
  4. Add the unreadable → readable transition test (stored None fingerprint vs a real hash must invalidate) — that's the direction most likely to silently serve stale records.
  5. Refresh the PR body — it still describes the abandoned first/last-4096-byte design and claims the base suite was failing (it's green — 3643 passed).

Also the branch is behind main — gh pr update-branch (or rebase) when you push the fixes. Ping me when it's ready for another look.

@anilmurty

Copy link
Copy Markdown
Contributor

@Vam-si-krish - are you planning to work on this?

Bring the review fixes onto the latest upstream base before rerunning the full validation suite.

Co-Authored-By: OpenAI Codex (GPT-5) <[email protected]>
@Vam-si-krish

Copy link
Copy Markdown
Contributor Author

@anilmurty @anshss — yes, I’ve addressed the review gate and pushed the update.

  • fingerprinting is now lazy after the cached size/mtime match
  • the source is re-statted and re-fingerprinted after parsing; divergent parses are not stored
  • the readable → unreadable test now warms with a real fingerprint first
  • unreadable → readable invalidation is covered
  • the module docstring and PR body now describe the whole-file hash and read-vs-parse tradeoff
  • the branch is updated to current main

Local verification:

  • transcript-cache tests: 18 passed
  • Ruff: passed
  • mypy: 268 source files, no issues
  • full non-e2e suite: 5,062 passed, 7 skipped; the six unrelated failures reproduce identically on clean origin/main

The refreshed CI run is waiting for maintainer approval: https://github.com/Metabuilder-Labs/tokenjam/actions/runs/30514071537

Once that workflow is approved, this is ready for another review.

Comment thread tokenjam/core/transcript_cache.py Resolved

@anilmurty anilmurty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @Vam-si-krish — this is a clean, well-reasoned fix. Going full-file SHA-256 instead of the edge-sampling I'd suggested in the issue was the right call: edge-sampling would've missed a middle-only rewrite, and your tests cover exactly that alongside the equal-size/same-mtime case, unreadable files, and legacy cache entries without a fingerprint. The deferred-fingerprint-on-cold-miss tradeoff is sensible and documented, and re-fingerprinting after the parse to refuse a divergent entry is a nice touch.

Addresses my earlier review. Merging. Thanks to @anshss for the co-author work too.

@anilmurty
anilmurty merged commit 9f697a4 into Metabuilder-Labs:main Jul 30, 2026
6 checks passed
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.

transcript parse cache can serve stale records on an equal-size in-place rewrite

3 participants