# Session- and auth-bound task context cache

Key and lifecycle in-process task-context caches by session id and live auth generation so shared-process sessions and mid-session auth replace cannot leak or reuse stale context.

Exact reference: {"kind":"skill_version","skill_id":"skl_wCKU3ytdp9mfmqZocBWzrA","version_id":"skv_QNGAXwUePOeqDf6TwFN_dA"}

Applicability: []

# Session- and auth-bound task context cache

## When to use
Use when an agent plugin caches task context before a system-message transform, multiple root or child sessions share one process, and authorization may be revoked or replaced while that process and root session stay alive.

## Steps
1. Key every entry as the pair sessionId plus authGeneration. Carry only sessionId in request scope such as AsyncLocalStorage. Resolve authGeneration from the live authorization handle for that auth lineage at write, read, and inject time—never from a module-level current pointer and never from an ALS-cached generation alone.
2. Look up by the full composite key. Do not select by sessionId alone. Missing or generation mismatch injects empty or default. Never fall back to parent, sibling, last-writer, or a prior generation.
3. Default-isolate child sessions. If inheritance is required, copy-on-create a snapshot into the child key; never share a mutable object with the parent. Children must read the live parent auth handle for generation checks. Maintain a parent-to-child or auth-lineage index so revoke sweeps every tied key.
4. Sample generation once at the start of a write. Key and tag the entry only with that sampled generation. On commit, inside the same critical section used for revoke or an equivalent compare-and-set, require sampled equals live; abort on mismatch. Never re-resolve live into the composite key after a bump, or a late writer can store pre-bump payload under the new generation.
5. Serialize child snapshot creation and lineage-index registration with that same critical section, or re-validate live generation after insert and delete the child entry on mismatch, so children created during a bump cannot survive the sweep.
6. On revoke or replace, in one critical section: bump generation, delete or tombstone older lineage entries including indexed children, then publish the new credential only if live.authGeneration still equals the bumped value.
7. Treat injected context as prompt data only—never as a network credential. Every outbound network or tool call must present a capability bound to the current generation; mismatch fails closed. Cancel work that will take another hop; do not assume already-gated single-shot I/O is recalled. Re-check live generation before applying response side effects.
8. Bound memory: cap entry count, bytes per entry, and total bytes; prefer delete-on-bump and session dispose; use LRU plus TTL for orphans; store slim summaries. Do not recycle a sessionId while an orphaned composite entry for that id may remain, or else use a process-wide epoch or otherwise unique generation space so restarted counters cannot collide.

## Limits
- Does not scrub prompt text already injected into an in-flight model turn before a bump.
- Cannot recall bytes already on the wire; re-check live generation before applying response side effects.
- Completed single-shot I/O under an old generation is not undone.
- Support for this guidance is reasoned analysis and independent design review, not executed tests.

## Failure prevented
Prevents cross-session context injection, late-writer reinstall of a revoked generation, write-key poison onto a post-bump generation, child snapshot races across parent auth replace, and same-session auth-rotate paths where stale cache entries authorize network activity or reappear under a recycled session identity.

## Supporting basis and limitations

Update grounded in reasoned design analysis from the cited native conversation on this root: session-scoped isolation, composite sessionId plus authGeneration keys, bump-first invalidation, and network fail-closed checks, plus independent subagent review findings that write commit must share the revoke critical section or equivalent CAS, child create must be serialized with revoke, and writers must key only with the sampled generation never re-key onto post-bump live. No repository inspection and no executed tests were performed.

## Change and rationale

Close revoke-versus-write and child-create races by requiring sampled-generation CAS in the revoke critical section, and forbid re-keying late writes onto the post-bump live generation.

Session-only keys and global current-context slots leak under concurrency. Auth replace without sampled-generation compare-and-set inside the revoke critical section, child-create serialization, and forbidding re-key onto live after bump leaves stale or poisoned entries that can inject or authorize after revoke.
