Documentation
¶
Overview ¶
Package session resolves the conversation key that state is scoped to.
Per the design (D4): each host supplies an explicit id when it has one (bifrost proxy: x-context-guru-session header, else the agent's own id off the request body — Anthropic metadata.user_id or Bob Shell's metadata.taskId; sidecar plugin: pctx.Session A2A id; eval-containers: gateway-stamped). When none is available we fall back to a deterministic content hash of the system prompt + first user message, which needs no host cooperation.
Index ¶
Constants ¶
const MaxExplicitLen = 128
MaxExplicitLen caps a client-supplied session id. The derived-hash path produces 16 characters, so this is generous by an order of magnitude.
Variables ¶
This section is empty.
Functions ¶
func ExplicitID ¶ added in v0.1.1
ExplicitID returns the first usable id from the candidates a host can offer, in the caller's precedence order — the proxy passes the x-context-guru-session header first, then Anthropic's `metadata.user_id` (Claude Code), then `metadata.taskId` (Bob Shell). It returns "" when none is usable, which makes Scoped fall back to the derived content hash — i.e. today's behaviour.
A host-supplied id matters because of COMPACTION. The derived hash covers the system prompt plus the first user message, and when Claude Code auto-compacts it replaces the transcript with a synthetic first user message ("This session is being continued from a previous conversation…"). The hash therefore flips mid-conversation, splitting one conversation into two sessions: the dashboard's before/after view breaks, the extract cache, the frozen offload set and the summarize checkpoint all reset, /expand 404s for pre-compaction ids, and — the expensive one — the cached-prefix boundary (modes.Tracker.Turn) resets to 0, so the offloaders are let loose over a prefix the provider has already cached and the next turn pays a full cache write. Bob Shell is worse: its `system` head changes at compaction too (it regenerates environment boilerplate including a recursive listing of the working directory), so its derived key flips for two independent reasons. Both agents' OWN session ids are stable across a compaction, so reading one off the request fixes all of that in one place.
Claude Code does not send a bare id. Real captured traffic carries a JSON object STRING:
"metadata":{"user_id":"{\"device_id\":\"<64 hex>\",\"account_uuid\":\"\",\"session_id\":\"<uuid>\"}"}
which safeExplicit rejects wholesale (braces, quotes, commas, ~170 bytes). So the object's session_id — the half that survives compaction — is pulled out. Anything that is not that object is passed through unchanged, for safeExplicit to judge, so a host sending a plain id (Bob's `metadata.taskId` is a bare randomUUID) works with no special case.
func Resolve ¶
Resolve returns the session key. If explicit is non-empty it wins; otherwise a stable hash of (system, firstUser) is used, so two turns of the same conversation land on the same key.
That property depends entirely on the CALLER passing conversation-HEAD strings. Pass the concatenation of every system-role message and it breaks: the Claude Agent SDK appends a new system-role message per turn, which made every request derive its own key. Callers get the head via schema.SessionHead.
func Scoped ¶ added in v0.1.1
Scoped is Resolve namespaced by tenant, for a hosted multi-tenant deployment. An empty tenant returns exactly what Resolve does, so single-tenant keys are unchanged.
The namespace is not decoration. Both halves of the key need it for a different reason:
- The content-hash fallback is identical for two tenants running the same agent against the same repository — same system prompt, same first message, same hash. Unscoped, they would silently share one session: one sticky offload set, one cached-prefix boundary, one another's state.
- The EXPLICIT id is client-supplied (the x-context-guru-session header), so unscoped it lets any caller name another tenant's session by simply sending its id. Prefixing means a tenant can only ever name keys inside its own namespace, whatever it puts in the header.
The explicit half is also validated here (see safeExplicit): this is the single point every caller routes through, so one guard here is worth a guard in the archive path plus one in the store plus one in every DELETE.
Types ¶
This section is empty.