fix(pre-read): skip repeated-read warning when file was modified since last read - #60
Open
AdarshJ173 wants to merge 1 commit into
Open
fix(pre-read): skip repeated-read warning when file was modified since last read#60AdarshJ173 wants to merge 1 commit into
AdarshJ173 wants to merge 1 commit into
Conversation
…e last read Closes cytostack#41 - Track `read_mtime` (statSync.mtimeMs) alongside `first_read` in the files_read session entry so we can detect file changes between reads - On re-read: compare current mtime against stored read_mtime; if file changed (by Claude or user), clear the stale entry and let the read proceed without a false repeated-read warning - In post-write.ts section 3: delete session.files_read[normalizedFile] so a write immediately invalidates the cached read record, without waiting for the next statSync comparison - Both statSync calls are wrapped in try/catch; on error the existing behaviour is preserved (warn or allow as before)
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
Fixes #41 —
pre-readhook blocks re-reads even after the file was modified during the session.The
pre-readhook currently warns on every re-read of a file seen this session, even when Claude just wrote to that file or the user edited it externally. This inflatesrepeated_reads_warnedandestimated_savings_vs_bare_clistats with false positives, and — more practically — suppresses the anatomy hint and re-read token estimate that Claude needs after a file changes.What changed
src/hooks/pre-read.tssession.files_read[normalizedFile]stored onlycount,tokens, andfirst_read— no mtime snapshot, so there was no way to detect a change between reads.read_mtime: fs.statSync(normalizedFile).mtimeMs(wrapped intry/catch; ifstatSyncfails,read_mtimeis leftundefinedand the old behaviour is preserved).prev.read_mtime !== undefined, compare currentstatSync(normalizedFile).mtimeMsagainst the stored value. IfcurrentMtime > prev.read_mtime, the file changed — delete the stale entry and fall through to the normal first-read path (anatomy hint, no warning). If mtime is unchanged, the existing repeated-read warning fires as before.src/hooks/post-write.tsfiles_readsession entry for that file still held the oldread_mtime. The nextpre-readof the same file would have to go through astatSynccomparison to detect the change, which is fine — but the write hook already knows the file changed.delete session.files_read[normalizedFile]immediately after updatingfiles_written. This eagerly invalidates the read cache for the written file, so the very next re-read is treated as fresh without needing to compare mtimes at all.Acceptance criteria
foo.ts→ Claude writes tofoo.ts→ Claude readsfoo.tsagain → no repeated-read warning, anatomy hint re-firesfoo.ts→ user editsfoo.tsexternally → Claude readsfoo.tsagain → no repeated-read warning (mtime changed)foo.ts→ nothing changes → Claude readsfoo.tsagain → repeated-read warning fires (correct behaviour preserved)repeated_reads_warnedcounter is not incremented for legitimate re-readsstatSyncerrors (deleted file, permission denied) do not crash the hook — they fall back to the original warn-on-reread behaviourHow to test
openwolf initin a test project, open Claude Code⚡ OpenWolf: ... was already readwarning on the second readtouch src/foo.tsbetween two reads — expected: second read is treated as freshNotes
read_mtimeis an optional field; sessions without it (e.g. old_session.jsonfiles) fall back to the original behaviour because theif (prev.read_mtime !== undefined)guard is skippedstatSyncis already used in this file for the symbol-hint freshness check, so the import is not newpost-write.tschange addsfiles_readto theSessionDatainterface there (was only inpre-read.ts's local interface) to make the delete type-safe