feat(bootstrap): cache session's bootstrap files so we don't invalidate prompt cache when modifying MEMORY and friends#22220
Conversation
Remove redundant mockLoad setup (beforeEach handles it) and reword the comment to make explicit that the third getOrLoadBootstrapFiles call is what proves sk2's cache survived clearing sk1. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Post-compaction context is intentionally fresh — it re-orients the agent after compaction, so reading the latest AGENTS.md from disk is the right behavior. Remove the cache lookup, the opts param, and the related tests. Also unexport resolveBootstrapCacheKey and drop getBootstrapFileContent which are no longer needed externally. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…ment" This reverts commit 2fca354.
- clear snapshot in ensureSessionRuntimeCleanup (fires on both reset and delete) - drop TTL sweep and sessionId fallback — sessionKey is always present at call sites - simplify getOrLoadBootstrapFiles to require sessionKey; bootstrap-files.ts falls back to direct disk read when absent Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
A session key is bound to a single workspace, so the cache entry never needs to track or validate workspaceDir. Remove the wrapper type, the comparison branch, and its test. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
A big YES for the idea, but I have one issue with the implementation: The way you're storing the cache---in memory--doesn't survive gateway restarts, but sessions do. It would be better to store the cache in the session, so it survives. There are still plenty of reasons to restart the gateway, and getting an update every second day is just one of them. |
I dunno. Copying the content of those files into every session json sounds like kind of a lot. All that just to protect against a single cache inval in the corner case where you have a long running session, wrote to your soul, and then restarted the gateway? |
|
It's more the memory than the soul I'm concerned with. And if it's about file size...I see that, and I have a solution: Put it into a second file and drop that once the session gets closed. |
Again sessions aren’t that long lived that it matters I don’t think. And you’d have to have a separate file for every session and then clean up those files on some regular basis because the gateway restart won’t ask you nicely. I again think that it’s rare enough to restart your gateway - even once a day is not very often - to make it worth introducing this complexity. |
|
@HenryLoenwind Thinking about it more, I can see an argument for putting it in the session files, but it’s less of a caching argument and more a consistency argument. It’d be nice to be able to reconstruct the original prompts that the session was run with. I don’t know if it’s worth blocking this PR over - I’d rather see this get merged than wait for a tweak that stores it in the file. But I might revisit this while waiting and change the behavior. |
|
@greptileai review |
|
Land note: this is now covered on Implemented via
Behavior/result: editing dynamic files like Changelog credit is included ("Thanks @anisoptera"). |
|
Closing as covered by 40db3fe on main. |
Summary
memory/YYYY-MM-DD.md. If these files are modified they will bust the cache.bootstrap-cache.tsmodule snapshots bootstrap files on first load per session key. All subsequent calls within the same session — including compaction'sresolveBootstrapContextForRun()and the memory flush sub-run — return the snapshot instead of re-reading disk.readPostCompactionContext) still reads AGENTS.md fresh from disk — intentional, it's re-orienting the agent after compaction, and we're already busting the cache pretty hard. No config changes, no new hooks, no changes to the hook system.This change was mostly planned by Opus and executed by Sonnet, and then the human went and told them to delete 2/3 of what they wrote, so, at this point, it's her problem, not theirs.
Change Type
Scope
Linked Issue/PR
User-visible / Behavior Changes
Memory written during a session (via memory flush or tool calls to
memory/*.md) will not appear in the system prompt until the next session. Neither will anything written to SOUL, AGENTS, etc.Additionally, models all seem to think this stuff doesn't update live. For example, just in this section alone I had to delete a line the model wrote about how it "was already true in most cases." It's uncanny.
Anyway, if you think about it, right now (before this change), when we ask a model to write something in one of these files, we're then killing that context and replacing it with one where it always thought that. It must be very weird to read a conversation about something you know the ending of already.
Security Impact
Repro + Verification
Locally tested before/after and validated that caching behavior is much nicer when working with memory documents.
Environment
Steps
Expected
Actual (before this PR)
Evidence
100% cache hit rate after mod of memory, well worth it especially on local.
Human Verification
Compatibility / Migration
Failure Recovery
bootstrap-files.tsto useloadWorkspaceBootstrapFiles()directly (one line)src/agents/bootstrap-files.tsRisks and Mitigations
Greptile Summary
Implements session-scoped caching for bootstrap files (
AGENTS.md,MEMORY.md,SOUL.md, etc.) to preserve prompt cache when these files are modified during a session.Key changes:
bootstrap-cache.tsmodule provides simple Map-based caching keyed bysessionKeyresolveBootstrapFilesForRuninbootstrap-files.tsuses cached files whensessionKeyis providedsessions.tsduring session reset/delete viaclearBootstrapSnapshotreadPostCompactionContext) continues to readAGENTS.mdfresh from disk, as mentioned in PR descriptionImpact:
Confidence Score: 4/5
sessionKeywithoutworkspaceDir, which relies on the assumption that a session key is always bound to a single workspace - this was an intentional simplification per commit de20b01. The PR explicitly documents the behavior change where memory modifications won't appear until next session. One minor concern is long-running processes with many sessions could accumulate cache entries, but the PR acknowledges this and notes that cache entries are cleared when sessions are deleted or reset.Last reviewed commit: aa82281
(4/5) You can add custom instructions or style guidelines for the agent here!