Documentation
¶
Overview ¶
Package askcache is offload_ask's in-process, content-addressed result cache: an identical repeat of a call returns the answer the seat already produced instead of spending the seat again.
What it actually buys, stated honestly ¶
The dominant cost of an ask is SEAT TIME — 46-75 s measured on the fleet — not the file read. The harness already reads and inlines the files itself (delegate.InlineContextPaths), and that read is microseconds; caching digests of it would save nothing worth the code. So this caches the finished RESULT, which is the only thing that removes seat time.
It pays on an EXACT repeat and on nothing else. A DIFFERENT question over the same files still pays full seat time, because the seat has to reason about the new question. The only mechanism that would fix that is keeping a model context resident between calls, which needs llama-swap slot pinning — trading a seat's availability for cache warmth. That was explicitly declined. Nothing here should be read as a general speedup.
Why serving a cached answer is safe ¶
The key is derived from the file BYTES, never from the path. A file edited between two otherwise-identical calls hashes differently, so it is a different key and the seat runs again. There is therefore no window in which a stale answer can be served for changed content — that single property is the whole safety argument, and TestKey...ContentChanges (unit) plus TestAskEditedFileMissesTheCache (wired) exist to keep it true.
Scope: the process IS the session ¶
The MCP server is spawned per client over stdio, so one client connection is one process is one cache — it is born with the connection and dies with it. That equivalence is why there is NO session_id argument: adding a required input to offload_ask would undercut the one-call friction removal the tool exists for, and it would be a second, weaker spelling of a boundary the process already draws exactly.
Index ¶
Constants ¶
const MaxEntries = 32
MaxEntries bounds one process's cached answers, oldest evicted first.
Thirty-two because a session touching more distinct file sets than that is not reusing a working set — it is sweeping a corpus, where a repeat is unlikely and the cache has stopped helping. So it stops growing rather than holding the whole session's reading. Insertion order, not recency: "oldest out" is the property that bounds the memory a long connection can pin, and an LRU would let one hot entry keep 31 cold ones resident.
Variables ¶
This section is empty.
Functions ¶
func Key ¶
func Key(question, readRoot string, docs []core.ContextDoc) string
Key derives the cache key for one ask from everything that could change its answer: the question, the resolved read_root, and the NAME AND CONTENT HASH of each resolved file, in the order the seat will see them.
docs must be the contract's own resolved context (post read_root confinement, post de-dupe, post name de-collision), not the caller's raw path strings — one file can be named two ways ("/abs/cfg.go" and "cfg.go") and both must key identically, because both hand the seat the identical bytes.
The CONTENT half is the load-bearing one: hashing the bytes rather than the path is what makes an edited file a different key, and therefore what makes serving a cached answer safe at all. Replace it with the path and a stale answer becomes servable.
Every component is length-prefixed before hashing so two fields cannot slide into one another and collide (question "ab" + root "c" must not equal question "a" + root "bc"). cache.Key length-prefixes each of its parts as well, so the fields would stay apart without field(); this prefix stays as a second, local layer so these keys do not rest on that alone.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a bounded, content-addressed map from an ask's inputs to its finished result. The zero value is not usable; call New. Every method is safe for concurrent use, and safe on a nil receiver (a nil cache is an always-miss, never a panic — the optimization is lost but no answer is ever wrong).
func (*Cache) Get ¶
Get returns a COPY of the stored result, and whether there was one.
A copy because the caller stamps cache_hit onto what it returns; handing back the stored map would let that stamp — or any later edit — leak into the cache and into every subsequent hit. Shallow is sufficient and deliberate: the stored values are the response's own scalars and slices, which nothing mutates in place. GUARD: this holds only as long as that stays true. Any new value ever added to the response map that is itself a map, or a slice that something later appends to in place, needs clone (below) deepened to cover it — a shallow copy would let that mutation alias back through the stored entry into every future hit.
func (*Cache) Len ¶
Len reports how many entries are held. Used by the bound test; the bound is the reason this cache cannot grow without limit on a long-lived connection.
func (*Cache) Put ¶
Put stores a copy of result under key, evicting oldest-first past MaxEntries.
Callers must only ever reach here with a SUCCESSFUL, non-deferred result. A defer, a refusal or a runner error is a statement about this minute, not about these files, and caching one would turn a transient seat failure into a lane that stays dead for the rest of the connection.