`pre-read` hook blocks re-reads even after the file was modified during the session
Bug description
The pre-read hook warns against (and flags in stats as) "repeated reads" when a file has already been read in the current session — but it never checks whether the file was modified since the last read. This means:
| Scenario | What happens | What should happen |
|---|---|---|
Claude reads foo.ts → Claude writes to foo.ts → Claude reads foo.ts again |
❌ Warned as repeated read | Re-read is legitimate — file content changed |
Claude reads foo.ts → user edits foo.ts externally → Claude reads foo.ts again |
❌ Warned as repeated read | Re-read is legitimate — file content changed |
Claude reads foo.ts → nothing changed → Claude reads foo.ts again |
✅ Warned (correct) | Correct |
This inflates the repeated_reads_blocked and estimated_savings_vs_bare_cli statistics, since some of those "blocked" re-reads were actually necessary.
Root cause
In src/hooks/pre-read.ts, the session state (_session.json) records only:
session.files_read[normalizedFile] = {
count: 1,
tokens: 0,
first_read: new Date().toISOString(),
};
It never records the file's mtime at the time of read, and the re-read check never compares against current mtime. The write hooks (pre-write.ts / post-write.ts) also never clear the files_read entry for a file that was written.
Suggested fix
Track read_mtime in the session entry and compare against current mtime before warning:
// In pre-read.ts, when recording first read:
import { statSync } from "node:fs";
session.files_read[normalizedFile] = {
count: 1,
tokens: 0,
first_read: new Date().toISOString(),
read_mtime: statSync(normalizedFile).mtimeMs, // ← ADD
};
// When checking for repeated reads, compare mtime:
if (session.files_read[normalizedFile]) {
const prev = session.files_read[normalizedFile];
const currentMtime = statSync(normalizedFile).mtimeMs;
if (currentMtime <= prev.read_mtime) {
// File has NOT changed — warn safely
process.stderr.write(`⚡ ... already read this session (~${prev.tokens} tokens). Consider using your existing knowledge.\n`);
session.repeated_reads_warned++;
} else {
// File WAS modified (by Claude or user) — re-read is legitimate, reset entry
delete session.files_read[normalizedFile];
// Fall through and re-register below
}
}
Alternative / complementary approach: have post-write.ts clear the files_read entry for any file that was written (when the path is recoverable from the write hook input).
Environment
- OpenWolf version: v1.0.4
- Node.js 20+
Confirming this on v1.0.4 — and flagging a second, related facet of the same nudge that survives even after the mtime fix proposed here.
Corroboration (staleness half)
Verified in dist/hooks/pre-read.js: the repeated-read branch fires purely on session.files_read[normalizedFile] presence (pre-read.js:38-40), with no mtime/size comparison, and post-write.js only ever touches files_written / edit_counts — it never clears files_read for an edited path. So a file modified mid-session still triggers "was already read this session … Consider using your existing knowledge of this file." on the next read. Your root cause and both suggested fixes (record read_mtime; and/or clear the entry in the write hook) match what I see in the code.
Additive facet: misplaced responsibility — the hook pre-empts a decision only the model can make
The deeper issue is a separation of responsibilities. The message "Consider using your existing knowledge of this file" makes the gist-vs-exact call on the model's behalf, defaulting to memory. But that call depends on the intent of the read, which only the model knows at the moment it issues the Read:
- Gist ("what does this module roughly do?") → a prior read may genuinely suffice.
- Exact (building an
Edit/str_replaceold_stringanchor, quoting verbatim lines, citing line numbers) → memory is unsafe; LLM recall of exact bytes is lossy.
A clean split:
- Hook's job = report facts it can actually know: already-read this session, ~N tokens, and (with the mtime fix above) whether it changed since.
- Model's job = classify its own need for this read (gist vs exact) and act on it.
The current wording collapses the two: the hook prescribes the answer (memory) instead of prompting the model to apply the intent it already has. So the nudge should surface the facts and hand the gist-vs-exact decision back to the model, rather than defaulting it to recall. Re-reading a few KB is far cheaper than the failure the memory-default invites.
Real-session failure it caused: prompted by this exact nudge, an agent built an Edit old_string from memory instead of re-reading. The anchor didn't exist verbatim on disk, the edit no-op'd, and the agent briefly misdiagnosed it as tool/output corruption before re-reading and finding its own recollection was wrong. The nudge actively rewarded the failure path.
(To be clear and avoid alarm: pre-read.js writes only to stderr and is purely advisory — it cannot alter the bytes Read returns. This is about the guidance, not content integrity.)
As a nice existence proof of the better design: Claude Code's own harness emits a comparable repeated-read notice, but it's staleness-aware and memory-neutral — "Wasted call — file unchanged since your last Read. Refer to that earlier tool_result instead." It points the model back to the prior tool_result (ground truth still in context), never to its memory, and only fires when the file is unchanged. That's exactly the split proposed above.
Suggested wording (complements the mtime fix here)
State the facts, then hand the decision back to the model instead of defaulting to memory:
⚡ OpenWolf:
<file>already read this session (~N tokens), unchanged since. You know why you're reading it: if you need only the gist, your prior read may suffice; if you need exact text — an Edit/str_replace anchor, verbatim quotes, or line numbers — re-read it. Don't reconstruct exact content from memory.
Optionally, skip the nudge entirely when the Read uses offset/limit (the agent is likely after a different region, so "already read" doesn't apply cleanly).
Even better: make it opt-out (it isn't, as shipped)
Beyond fixing the wording, this nudge is a tradeoff some users won't want — e.g. correctness-critical work where re-reading is cheap insurance — so it'd be ideal to let them turn it off. I checked how it behaves as shipped (v1.0.4), and there's currently no supported way to disable it short of removing the PreToolUse → pre-read entry from Claude Code's settings.json:
- the hook scripts don't read
.wolf/config.jsonat all, so there's no per-hook or verbosity key to set; openwolf.enabledships in the config template and is documented, but no hook reads it — so even the documented global switch doesn't silence hook output;- the
openwolfCLI has noconfig/disable/quietsubcommand.
The elegant part: one small piece of plumbing — having the hooks load .wolf/config.json — unlocks three things at once: (1) makes openwolf.enabled actually effective, (2) enables a granular per-nudge toggle, and (3) addresses the same class of over-eager-nudge complaint in #28. Something like, defaulting to today's behavior:
// .wolf/config.json
"hooks": { "pre_read": { "repeated_read_warning": true } } // default true → token savings stay on out of the box
Default-on preserves the out-of-the-box token savings; users who'd rather not take the tradeoff can opt out without touching Claude Code's settings.
Happy to fold all of this — the mtime fix from this issue, the wording change, and the config-gate wiring — into a single PR if that's welcome.
Hi maintainers! I'd love to work on this issue.
My proposed approach:
- Add a
read_mtimefield to thefiles_readsession entry inpre-read.tsby callingstatSync(normalizedFile).mtimeMsat first-read time. - On subsequent reads, compare
statSync(normalizedFile).mtimeMsagainst the storedread_mtime— if the file has changed, clear the entry and let the read proceed silently (no false warning). - As the complementary fix, in
post-write.tssection 3 (session tracker), deletesession.files_read[normalizedFile]for the written file — so writes immediately invalidate the stale read record without waiting for the nextstatSynccomparison.
Estimated scope: 2 files (pre-read.ts, post-write.ts), purely additive changes, no schema changes, no new dependencies.
Happy to adjust based on your feedback before I open a PR.