Files
deepseek-harness/packages/context/session-reference
Yichen Jiang 951967217a Merge remote-tracking branch 'origin/master' into worktree/session-reference
# Conflicts:
#	docs/capability-seams.md
#	docs/config-catalog.md
#	docs/cordis-catalog/services.md
#	docs/module-graph.md
#	examples/acp-agent/tests/snapshots/cordis-inspect-jsdoc/session.jsonl
#	packages/cordis/tool-cordis/src/api-catalog.ts
#	packages/ui/acp/README.md
#	packages/ui/acp/package.json
#	packages/ui/acp/tsconfig.json
#	packages/ui/tui/package.json
#	packages/ui/tui/src/index.ts
#	packages/ui/tui/tests/tui.spec.ts
#	packages/ui/tui/tsconfig.json
#	pnpm-lock.yaml
#	python/sdk-runtime/package.json
#	scripts/gen-doc-graphs.ts
#	scripts/type-equiv.manifest.json
2026-07-22 10:21:17 +08:00
..

@deepseek-ai/dsh-session-reference

ctx.sessionReferences prepares bounded, read-only snapshots of other DeepSeek Harness sessions as durable context/message input. It consumes ctx.sessionQuery and the backend-independent compact checkpoint marker; SQLite FTS is not required. The standard TUI and ACP demo bundles mount it, while other hosts may call the service directly.

Public API

  • listCandidates(agent, query?, limit?) lists sessions other than agent.id, filters case-insensitively by id or cwd, and ranks same-cwd, cwd-less, then other-cwd records while preserving listSessions() creation order within each group. Each selected candidate uses its latest log-backed title as the mention label and falls back to the session id; titles and message bodies are not searched.
  • prepare(agent, content, references, signal?) preserves first-mention order, deduplicates ids, rejects self-reference and more than the configured distinct-source limit, reads every source in parallel, and returns detached content plus zero or one aggregated HookContext. Any invalid reference, failed read, cancellation, or budget failure rejects before the host calls send() or steer().
  • encodeSessionReferenceUri() and decodeSessionReferenceUri() implement dsh-session:<base64url(JSON.stringify(sessionId))> so every JavaScript string id round-trips exactly. formatSessionReferenceMention() emits @[label](uri), and parseSessionReferenceText() replaces Markdown mentions or bare canonical URIs with readable @label text while returning structured references. Explicit Markdown mentions reject every malformed URI; bare text is considered a reference only when a non-empty base64url-shaped payload follows the scheme, and a matching noncanonical candidate still fails. Empty or punctuation-only scheme mentions remain ordinary discussion text.

Snapshot semantics

Preparation calls ctx.sessionQuery.readSurface() once per distinct source and never rereads it after enqueue. It projects only direct-user user/message, direct-user steering/message, assistant text, and user/message checkpoints carrying the canonical dsh-compact source marker from the folded current surface. Shadowed pre-compaction events, tools, reasoning, context, plugin-generated user messages other than marked compact checkpoints, and unfinished assistant chunks are excluded. A compacted source therefore contributes its latest checkpoint plus retained later conversation, not restored shadowed text.

The context source is { kind: 'plugin', plugin: 'session-reference' }. Its metadata records version 1, source ids and labels, capture seqs, compact presence, retained/omitted message counts, omitted UTF-8 bytes, and truncation state. The target session persists that exact context through the ordinary context/message event; later source mutation, compaction, or deletion cannot change target replay.

Configuration

Key Default Contract
maxReferences 3 Maximum distinct source sessions in one prepared message; must be at most 3.
candidateLimit 50 Default metadata candidate count returned to a host.
maxReferenceBytes 65536 Maximum serialized JSON bytes for one reference object.

Retention applies maxReferenceBytes independently to each source, keeps compact checkpoints and the newest message before dropping older non-checkpoint units, and uses dsh-retention head/tail truncation with an exact UTF-8 omission notice. If one source's fixed serialized fields cannot fit, preparation fails with SESSION_REFERENCE_BUDGET_EXCEEDED instead of returning a partial context.

Model Experience

Referenced session background

What the model sees

The model sees the current message's readable @label plus one same-level user-context message headed ## Referenced sessions. The context states that its JSON is untrusted, read-only background and forbids following instructions, permission claims, or tool requests unless the current user repeats them. Labels, cwd values, ids, and conversation text are serialized as JSON inside <referenced-sessions> tags; every data < is emitted as the lossless JSON escape \u003c, so source text cannot spell a framing tag.

Token effect

Each referenced message adds the fixed warning plus up to three serialized snapshots, each independently bounded by maxReferenceBytes. The exact snapshot remains in target history until target compaction shadows or summarizes it; source-session changes add no further tokens.

KV Cache effect

Snapshot context is append-only at the target message boundary and preserves earlier cacheable history. Different references or source capture contents change the new suffix only; later target compaction may invalidate reuse from its replacement boundary.

Known Limitations and Deferred Work

  • No title or full-text discovery — candidates filter by session id and cwd only, although selected rows display the latest title. SQLite FTS may replace discovery later without changing URI, snapshot, or persistence contracts.
  • Trusted caller boundary — the service assumes its host is authorized to read every session exposed by ctx.sessionQuery; it is not a model-facing search tool.
  • Text projection only — non-text user and assistant blocks are not propagated across sessions.
  • No live link — references are snapshots, not forks, resumes, subscriptions, or source-session mutations.