# Conflicts: # .agents/notes/implemented/architecture/2026-06-21-bounded-llm-request-recovery.md # docs/architecture.i18n.yaml # docs/config-catalog.md # docs/cordis-catalog/events.md # docs/cordis-catalog/services.md # docs/core-data-structures/llm-streaming.i18n.yaml # docs/core-data-structures/llm-streaming.md # docs/core-data-structures/llm-streaming.zh.md # docs/event-producer-consumer.md # docs/module-graph.md # examples/headless-agent/tests/headless.snapshot.ts # packages/compact/compact-basic/tests/compact-loop-repro.spec.ts # packages/cordis/tool-cordis/src/api-catalog.ts # packages/core/agent-loop/README.md # packages/core/agent-loop/src/loop.ts # packages/examples/agent-spine-demo/README.md # packages/llm/README.md # packages/llm/llm-deepseek/src/adapter.ts # packages/llm/llm-pi-ai/src/adapter.ts # packages/llm/llm-retry/README.md # packages/llm/llm/README.md # packages/llm/llm/src/index.ts # packages/llm/llm/tests/service.spec.ts # packages/support/llm-replay/src/index.ts # packages/support/llm-replay/tests/llm-replay.spec.ts
7.7 KiB
@deepseek-ai/dsh-llm-replay
English | 中文
A replay LLM plugin for keyless snapshot tests. It yields model streams reconstructed from a recorded session JSONL fixture, so a test can boot the real agent against a fixed model transcript with no API key. With providers configured it registers a replay-only adapter whose catalog is available to scenarios that exercise model discovery; without providers it installs the catch-all llm/stream waterfall used by tests that do not need discovery.
Its consumers are the ACP, headless stream-json, and TUI snapshot suites plus the web browser e2e lane. Loader-driven suites mount this plugin in place of a real LLM adapter; the web lane installs it directly to retain the teardown consumption handle. Keeping derivation and replay here places that logic under the per-file 100% coverage gate on packages/*/src.
How the fixture works
The fixture IS the persisted session log (<scenario>/session.jsonl). Its assistant/chunk events carry every StreamChunk, so grouping them by (turn, step) reconstructs each stream() call's chunk sequence (one model call per loop step). Recording is therefore "run the real agent once and harvest the .jsonl", done by the snapshot harness — this plugin does not record. A fixture may carry its request/header content tokenized to {{system}}/{{tools}} (the harness pins that content in one scenario and scrubs the rest); replay is indifferent — derivation reads only assistant/chunk events and the line-0 session header.
Two failure modes are not reconstructable from assistant/chunk alone — a pure throw before any chunk (e.g. an HTTP 401, where the log holds only a turn/end {error} and no chunks) and a cancel/hang (timing, not chunk content). A scenario that needs those supplies an optional sidecar (<scenario>/replay.override.json) that either replaces the derived script (a bare ReplayEntry[]) or augments it ({ patches: [{ at, entry }] }: keep every JSONL-derived call and swap the named 0-based call indexes; at equal to the derived length appends the retry attempt after an injected transient throw). Patch indexes must be unique. The override document, each patch and entry, and every chunk discriminant are validated when the file loads. A hang entry may name readyFile; replay writes that empty marker after its prefix chunks reach the loop and before it waits for cancellation, so an external driver can cancel deterministically without observing a presentation update.
Nested agents: per-session keying
A scenario where a parent agent delegates to in-process subagents records more than one log: the parent (session.jsonl) plus one per child (session.1.jsonl, …). Each agent runs as its own Session on the same context, so replay must serve each one its own script.
Replay keys every call by its calling session id (GenerateOptions.sessionId, stamped by the agent loop). Live session ids are freshly random each run and never equal the recorded ones, so a live session binds to a recorded script by first-call order: scripts are ordered by header createdAt (parent first — it streams before it can delegate), and the first live session to make any call claims the first script, the next new session the next, and so on. Each session then advances its own cursor. A call with no sessionId is one anonymous session bound to the primary script, so single-session scenarios behave exactly as before. More distinct live sessions than recorded scripts fails loud.
Config
| Key | Type | Default | Notes |
|---|---|---|---|
file |
string | $DSH_SNAPSHOT_FILE |
Path to the primary (parent) session.jsonl fixture. Required (config or env). |
overrideFile |
string | $DSH_SNAPSHOT_OVERRIDE |
Optional ReplayOverrideDoc sidecar for the primary session: a bare ReplayEntry[] replaces its derived script, while { patches } augments it by call index. |
childFiles |
string[] | $DSH_SNAPSHOT_CHILD_FILES (path-delimited) |
Recorded subagent child-session logs for a nested scenario; empty for a single-session scenario. |
providers |
ReplayProviderConfig[] |
— | Optional replay-only provider and model catalog. Each provider may set retryPolicy, and each model may publish contextWindow; configured routes dispatch through the replay adapter and never perform provider I/O. |
paceMs |
number | — (burst) | Optional per-chunk delay in ms so downstream transports (e.g. the web SSE mux observed by a real browser) see genuinely incremental delivery. A realism knob only — tests must not depend on it for correctness. Non-negative integer; abort during a pace wait cancels the stream promptly. |
- id: llm-replay
name: '@deepseek-ai/dsh-llm-replay'
config:
providers:
- id: deepseek
name: DeepSeek
retryPolicy:
mode: normal
backoff:
initialDelayMs: 1
maxDelayMs: 1
jitterRatio: 0
models:
- id: deepseek-v4-flash
contextWindow: 128000
- id: deepseek-v4-pro
# file/overrideFile/childFiles default to $DSH_SNAPSHOT_FILE /
# $DSH_SNAPSHOT_OVERRIDE / $DSH_SNAPSHOT_CHILD_FILES, set by the snapshot
# harness per scenario.
Exports
installLlmReplay(ctx, config)— install the configured replay adapter or catch-allllm/streamlistener; returns aReplayHandle(dispose()for HMR safety plusassertConsumed(), the teardown check that every recorded script bound to a live session and every bound cursor drained — turning a scenario that silently drove fewer model calls than recorded into a crisp diagnostic). Use this in tests to drive replay without the Loader or env vars.loadSessionScripts(config)— resolve the orderedSessionScript[](primary + children) for a scenario, ready to bind to live sessions in first-call order.loadReplayScript(config)— resolve theReplayEntry[]for the primary session only (validated sidecar replacement/patches if present, else derived from the JSONL; fail-loud if the fixture is missing).deriveReplayScript(events)/parseSessionLog(text)/parseSessionHeader(text)— the pure helpers that turn a recorded session log into a script and read its headerid/createdAt. A derived group must end in afinishchunk; a group without one is the fingerprint of a thrownstream()and must instead be expressed via an override sidecar.- Types
ReplayEntry/ReplayOverrideDoc/ReplayOverridePatch/SessionScript/ReplayConfig/ReplayProviderConfig/ReplayModelConfig/ReplayHandle/Config.
Plugin export shape
Named name / inject / Config / apply, with no default export: the cordis Loader's unwrapExports does exports.default ?? exports, so a stray default would collapse the module to the bare function and drop the inject namespace (see docs/postmortem/0001).
Model Experience
None, as this keyless test adapter sends no request to a provider model; it only replays recorded assistant chunks into the test loop.
KV Cache effect
None; this package neither assembles nor sends a provider request.
Known Limitations and Deferred Work
- First-call-order script binding assumes sequential delegation — a cut that runs sibling subagents concurrently (or a compaction summarize call landing mid-run) would bind live sessions to recorded scripts non-deterministically; a stronger keying is deferred until such a scenario exists (
XXX(concurrent-subagents)). - Only chunk-producing calls are derivable — a pure pre-chunk throw or a cancel/hang scenario needs the
replay.override.jsonsidecar. Replacement and patch forms affect only the primary session; child scripts still derive from their logs.