* feat(system-prompt): admit section updates incrementally * docs(system-prompt): accept retirement restart gap * test(system-prompt): cover retirement and reentry before admission * test(system-prompt): align rebase fixtures and persistence history * fix: align section prompt consumers and CI fixtures * test: pin credentials for section snapshot fixtures * test: align remaining snapshots with section admission * test: preserve section metadata in Web and PowerShell replays * refactor(system-prompt): retire sections through Cordis cleanup * chore: refresh section admission history after rebase * fix(system-prompt): advance admission head after compaction * fix(system-prompt): trim sections before admission hashing * docs: move system upgrade guides to current version * docs: add system message contribution cookbook * perf(system-prompt): avoid indexing history for admission * refactor(system-prompt): derive admission from logged sections * test(system-prompt): update lifecycle and compaction consumers * test(system-prompt): refresh CLI section event expectations * test(system-prompt): refresh question composer section metadata * feat(system-prompt): resolve same-name sections by scope * refactor(persona): use ordinary section registrations * refactor(system-prompt): clarify assembly and related control flow * test(system-prompt): reconcile rebased section snapshots * test(python): use current system text helper after rebase * test(system-prompt): refresh CLI expected section blocks * test(system-prompt): reconcile latest master integration * test(agent-loop): seed tools before deferred addition assertion * refactor(compaction): replace system heads when compacting prefixes * refactor(system-prompt): separate source text and notice attribution * fix(system-prompt): preserve legacy instructions in compaction snapshots * refactor(compaction): build system snapshots while reading the summary prefix * refactor(compaction): rebuild current system instructions * refactor(system-prompt): defer compaction rebuild to admission * refactor(system-prompt): remove redundant fixtures and setup * fix(plan-mode): announce committed user transitions * docs(system-prompt): reconcile section lifecycle and compaction design * refactor(system-prompt): consolidate projection version increments * fix(system-prompt): reconcile master integration and replay fixtures * docs(system-prompt): align upgrade guide with current release * refactor(system-prompt): make admission operations explicit * fix(system-prompt): reconcile master API and release changes * docs(system-prompt): regenerate catalogs after master refresh * refactor(llm-pi-ai): delegate text rendering to pi-ai * fix(llm-pi-ai): preserve blank lines between system text parts * refactor(system-prompt): remove redundant retirement metadata * refactor(system-prompt): own admission and use canonical surface targets * refactor(system-prompt): compare captured sections at commitment Assembly fixes section text and lifecycle; PromptAssembly.systemUpdates() compares them with the committed projection when the loop commits. This replaces SystemPrompt.admit(), needsRebuild() and isCurrent(), the SystemAdmission export, the loop's reassembly after compaction, and SessionSurface.systemHeadResetSeq. The projection records a pending rebuild as a boolean that an attributed reset clears. After a compaction inside a step, the rebuilt head uses the sections captured for that step; later source changes arrive as ordinary updates at the next step. * test: await the session read fixture and type a matcher for lint * test(tool-executor-ptc): describe same-name section override on executor replacement Duplicate section names no longer reject a scoped executor; the earlier registration is overridden and returns as an ordinary update when the executor leaves. * docs(upgrade-guide): move system-prompt integrations to v0.2.2-rc.3 The root version on master is 0.2.2-rc.3; verify-upgrade-guides rejects a guide added under an earlier version directory. * refactor(system-prompt): rebuild over system messages without sections A system message without source.sections now marks rebuilding pending, like a compaction summary. The next model attempt replaces the head with current sections and clears later system messages. This removes the separate legacy flag and its appended whole-prompt replacement and notice. Sessions written by earlier releases, migrated logs, and imported DeepSeek conversations take the same path, so the importer and its tests return to their master versions. * test(web): refresh the imported-conversation recording and drop a stray bytecode file The Web recording for a resumed DeepSeek import was recorded while the prompt was appended after the imported history; the head is replaced again, as on master. The .pyc under the macOS signing skill was staged by accident in an earlier merge. * refactor(session): scan system nodes on read surface.systemNodes is read only when a system rebuild is written. Scanning the current surface nodes on each read restores the surface fold's plan types and state to their master definitions, which no longer carry the system message. * refactor(session): rename SystemSurfaceNode.hasContent to isEmpty The flag reports that the logged content array has no blocks. isEmpty matches the documented "empty head" wording and covers a head that was written empty without ever being cleared. * refactor(llm): join system text through extractTextContent extractTextContent gains a trim option that trims each text block and omits blocks left empty. The DeepSeek serializer, the pi-ai converter, and the prompt inspector use it with a blank-line separator in place of three hand-written joins. The pi-ai converter now trims system and developer text like the other two, so whitespace-only blocks are omitted there as well. * test(snapshots): refresh the PowerShell and cordis-inspect recordings The two PowerShell scenarios still held the initial system message as one unattributed block; replay now writes one block per section with its attribution. The cordis-inspect-jsdoc recording embeds the API catalog, which names SystemSurfaceNode.isEmpty since the rename. * test(plan-mode,harness-dev-context): cover a rejected pre-step and a repeated legacy prompt A rejected pre-step must leave a queued plan-mode switch pending and unannounced until the next accepted step. A second unattributed system message with the same legacy paragraphs must keep the admitted paragraphs. * test(web): refresh Web recordings for section-attributed system messages Twenty-seven Web recordings still held the initial system message as one unattributed block. Replay now writes one text block per section with its attribution; twenty-six recordings differ only in that line. plan-review also records the plan-mode switch notice before the first prompt and appends a plan:status update on exit in place of a head replacement and a series header, so its expanded golden gains the notice row. The Creator persona test joins the system text blocks with blank lines, as the adapters do, before matching the persona line. * refactor(system-prompt): require the retry argument of systemUpdates The agent loop passes it on every attempt. A custom loop that omitted it on a retry received a full comparison in place of the pending rebuild, with no diagnostic; the type now makes each caller state which attempt it is. * docs(upgrade-guide): move the system prompt integrations guide to v0.2.2-rc.5 The root version is 0.2.2-rc.5 after the master merge, and verify-upgrade-guides rejects a guide added under an earlier version directory. * test(web): refresh the mention path presence recording for section-attributed system messages * test: align two specs that master left failing The DeepSeek settings spec waited for a saved notice carrying the provider label from before the rename to DeepSeek API. The Windows packaging workflow spec seeded one of the two helpers that the Stage workflow helpers step copies. * chore: remove a Python cache file committed by mistake
description, kind
| description | kind |
|---|---|
| The Agent handle, live registry, process-local initiator scope, and agent/* event vocabulary for plugins, UI, and orchestrators building or extending agents. | package-reference |
@deepseek-ai/dsh-agent
English | 中文
Summary
Use dsh-agent to create or resume live agents, send follow-up or steering input, inject model-facing context, cancel work, and wait for idle completion. Plugins, UI, hooks, and orchestrators can also observe or intercept agent activity and apply capabilities to one agent without affecting others. Choose it when code needs to control or extend live agents through the public Agent API. Pair it with an agent driver such as dsh-agent-loop; this package does not create model requests by itself. Initiator attribution is process-local and must be carried explicitly across workers, processes, durable queues, and restarts.
Table of Contents
- Use this package
- Understand the implementation
- Further Exploration
- Model Experience
- Known Limitations and Deferred Work
- Dev Note
Use this package
Mount dsh-agent with dsh-session-projection: it provides ctx.agents, the Agent handle, and turnBoundaryOf(session) for recorded turn and step positions. Creating or resuming an Agent also requires a driver to register a factory; the shipped driver is dsh-agent-loop. Session model selection is available on each handle as agent.model.
Create or resume an agent
ctx.agents.create() builds a fresh agent and session under one identity; ctx.agents.resume() loads a persisted session and rebuilds the agent on it. Both delegate to the registered factory and return an AgentHandle — the only object that can tear that agent down. See AgentFactory for publication and failure-cleanup timing. get(id) and list() find live agents. The subagent service owns delegated execution and runtime tree membership.
seed carries the complete constructor history. A fork additionally passes parentSource: { sessionId, endSeq }: the parent whose stored events the first endSeq seed events reproduce, as a live log or a persistence read returned them. The loop's creation lifecycle owns seed validation, storage, and cancellation.
const handle = await ctx.agents.create({
sessionId,
agentOptions: { provider: 'deepseek', model: 'deepseek-chat' },
})
// later:
await handle.dispose() // stops the loop, unregisters, removes the session, unwinds the scope
AgentOptions supplies the initial provider/model route, optional adapter-owned reasoningEffort, and optional positive maxTokens output cap. The loop validates exact-model reasoning support, resolves adapter defaults, records the effective values in the request header, and applies them to each conversation request. An optional setup(agentCtx, agent) callback composes the agent's scoped world before it is published: agentCtx owns registrations, while the explicit unpublished Agent provides its Session; the Context has no reverse Agent property. Scoped tools, prompt sections, and listeners exist before agent/created. Setup is composition-only: drive the agent only after creation resolves. The Session has entered its store and completed session/created before setup, so initialization events follow the live event feed.
Select a model
agent.model implements Model, alongside agent.inbox. current() returns a detached selection: the pending choice, then the latest request's choice, then live agent.options; it returns undefined when no source supplies both provider and model. Reading does not pin a choice or change a prepared step.
select({ provider, model, reasoningEffort? }) assigns the complete choice for the next step whose preparation has not begun. Omitted effort uses adapter defaults; selection neither inherits old effort nor queries defaults. An equal recorded choice is a no-op, but a choice equal only to initial options is recorded as model/selection. Callers validate availability and capabilities; append failures retain the Session's existing guarantees.
Ordinary resume keeps the recorded choice. An entry point explicitly overriding the model first builds a complete selection, retaining the previous explicit effort only when the configured route is unchanged and omits effort, then calls select(). Web and ACP user selections call select() directly, so omitted effort means Auto. Selecting a Session model does not change deployment defaults.
For scoped switch notices, call installModelSelection(agentCtx) from dsh-agent during Agent setup. The scope owns the listener and removes it on disposal.
Drive an agent's conversation
The handle's methods route identified user-role messages into the agent's inbox. followup() queues an ordinary next-turn prompt and wakes the driver; steer() submits next-step input and wakes it; inject() adds model-facing context without waking the driver, so it lands in the next admitted step. cancel(cause) aborts the active activity and, unless keepInbox is set, clears pending work; whenIdle() resolves after the whole agent reaches quiescence. A queue-write failure during cancellation still aborts active work and propagates to the caller.
handle.agent.followup({
content: [{ type: 'text', text: 'Summarize this workspace.' }],
source: { kind: 'user' },
})
handle.agent.steer({
content: [{ type: 'text', text: 'Focus on the tests.' }],
source: { kind: 'my-plugin' },
})
await handle.agent.whenIdle()
Scope registrations to one agent
Agent.ctx is the agent's scoped context: registrations made through it (tools, prompt sections, variables, event listeners, restrictions) apply to that agent alone and unwind on disposal. The same mechanism is what agent presets use to give one session a different capability set without affecting its neighbors.
Intercept or observe work in flight
agent/request is deprecated and retained for existing listeners. Do not add new listeners. Provider, model, and optional explicit reasoning effort are fixed at step preparation; listeners must preserve all three, including an absent effort. AgentOptions.maxTokens sets the output cap. Other per-request control overrides have no replacement API. See the migration guide for affected callers.
The agent/* events let plugins act on live work without depending on the loop package. agent/pre-step can reject a proposed step or replace the messages entering it; agent/request-error lets a listener retry a failed model request; agent/turn-stopping runs before an otherwise completed turn closes and can steer to keep it open. agent/assistant-stream carries one process-local Assistant attempt's ordered start, transient chunk, and end frames. Start names the attempt's turn and step, chunk indexes are dense from zero, and end.index is the next chunk position. The loop commits the complete compact stream as one assistant/message or assistant/attempt before a committed end frame, so the live event remains presentation data rather than the replay source. agent/status, agent/created, and agent/disposed drive UI and coordination state, and the per-message agent/inbox/* notifications keep inbox projections in sync. Exact signatures, dispatch modes, and payload contracts live in the generated region of the core subsystem page.
Understand the implementation
Implementation internals — click to expand
This section explains how the package realizes the behavior above; the observable contract is covered in Use this package.
Design concept
The package is built on one separation: the public Agent surface and registry live here, while construction and driving live in the loop package behind a registered factory. Consumers therefore depend on dsh-agent and never on dsh-agent-loop, keeping the driver swappable. The second idea is the initiator scope: an AsyncLocalStorage chain that carries the exact live Agent through the asynchronous driver work it starts, so helpers below a driver can attribute their work without forwarding the agent through every call.
Step admission
PreStepDecision is either { kind: 'reject' } or { kind: 'enter', messages, startsRequestSeries? }. The enter branch contains the complete identified, frozen message batch. Acceptance does not commit it: after assembly and step/start, the deprecated agent/request hook adjusts controls without changing the step's model selection, and prepareCall() binds the adapter before the loop commits the system prompt and user batch. Cancellation during either async phase commits neither. startsRequestSeries: true declares a distinct model-message series; a wrapping listener preserves that declaration and the batch unless it intentionally replaces either one. Claiming removes offered messages from the inbox, while messages inserted after the claim remain pending for a later boundary.
Durable inbox
Agent.inbox exposes only the structural Inbox interface and the projection vocabulary stays in this package. dsh-agent-loop owns the package-internal ReactLoopInbox and the standard inbox projection; the loop service acquires one registration for the durable agent/inbox/spliced fold and gives each inbox a state-reading callback. The registry remains the sole owner of the live { 'next-turn', 'next-step' } state. Reconstruction rejects unsafe or out-of-range splice coordinates and duplicate MessageId values across both pending lists and reports the offending event seq.
Inbox exposes pending nextTurn and nextStep messages and mutates them through append, prepend, replace, remove, clear, and splice. Ordinary removals and clear() are durable cancellations. At a step boundary, the loop's internal implementation claims pending input through pure deletion splices. Live notifications are deliberately per-message and minimal: agent/inbox/inserted { message }, agent/inbox/claimed { message, turn }, and agent/inbox/discarded { message }.
Durable model selection
Model and the modelSelection projection types live here. The default driver creates each AgentModel and owns projection registration alongside its inbox. Each instance binds the Session, live options, and a projection reader; it holds no second copy of the choice. The loop captures a detached selection before step preparation and retains it across retries.
Source map
| File | Role |
|---|---|
src/index.ts |
Plugin entry: AgentRegistry, factory slot, initiator scope, CreateAgentOptions/ResumeAgentOptions |
src/runtime-types.ts |
Agent, structural Inbox, AgentStatus, and the agent/* event declarations |
src/types.ts |
AgentOptions, cancellation causes, and inbox projection vocabulary |
src/dispatch.ts |
agentEvents fused dispatcher and assembleContextFor(agent) |
src/model-selection.ts |
AgentModel, its durable projection, and installModelSelection switch notices |
src/consumed-work.ts |
foldConsumedWork(events): what the log's consumed work became |
src/archive-admission.ts |
The turn family of the Workspace registry's archive admission: a running turn and its user-cause cancel |
Registry and lifecycle
AgentRegistry keeps one entry per live agent with its carrier and creator relation. Await register() to finish serial creation listeners with source startup before using an already-constructed agent; the async factory uses the split enter()/announce() pair so setup and initialization stay rollback-covered. A detach requested during creation waits for every awaited listener to settle, and each detach is bound to the exact entry, so a stale disposer cannot remove a later same-id replacement. Teardown stops and drains the loop, unwinds the scope, detaches the agent, then detaches the session; the id becomes reusable after private cleanup.
The registry also answers the Workspace registry's archive admission (seam) for every Agent it publishes: workspace/session-activity reports the turn family while the Session's Agent is running, a turn waiting for an approval or an answer included, and workspace/session-stop cancels that turn the way the user's own stop does — agent.cancel({ kind: 'user' }), but without the stop button's keepInbox, so queued input is discarded with a logged inbox splice instead of waking the archived Session later. Nothing is awaited to settlement; a Session without a live Agent has no turn that could run. The turn key is merged into SessionActivityKindMap from src/types.ts, so a Client that renders the families imports @deepseek-ai/dsh-agent/types for it.
Initiator scope
Each driver runs its complete lifetime inside ctx.agents.withInitiator(agent, ...), so inherited async chains observe that agent; withoutInitiator() hides it for unrelated process-local work such as shared timers. The boundary is process-local attribution only — ambient presence is neither liveness proof nor authorization, and explicit identity stays authoritative at worker, process, persistence, and wire boundaries. Teardown rejects new boundaries, lets returned-Promise boundaries drain, then disables the underlying storage. The initiator-scope decision owns the detailed contract.
Ownership invariants
The AgentHandle disposer is a capability: among consumers, only its holder can tear the agent down. The registered factory provider is a structural co-owner, because scoped agents depend on that provider's service API; provider unload stops and drains every live handle it made. ctx.agents.get(id) still returns a bare Agent — the handle is exposed only to the consumer that created it.
Further Exploration
The package-level contract is enough for most consumers; read these when you need the surrounding domain and the design rationale.
- Core subsystem — the loop map,
Agenthandle, interception decisions, and generated service API. - agent-loop package — the default driver that creates, drives, and disposes agents.
- Session subsystem — the durable log and derived history behind the handle.
- Initiator-scope Agent Note — the boundary and teardown contract.
- Core group map — how the core packages compose.
Model Experience
User, steering, and injected messages
What the model sees
followup, steer, and inject feed the owning session as identified user-role messages; accepted content becomes part of the derived history the model reads on later steps. agent/pre-step and the other declared events let plugins reject a proposed step or add durable request material. The optional scoped model-switch listener adds the notice described below.
Token effect
Accepted content becomes retained history or a repeated session prefix; blocked content contributes no request tokens. Size is caller- and plugin-dependent.
KV Cache effect
Accepted history and steering are append-only; a blocked submission sends no request. A session prefix remains stable within its loop instance, while a new or resumed instance may establish a different prefix.
Agent-scoped request composition
What the model sees
Registrations through agent.ctx can shadow prompt sections or tools and can install agent-only interceptors during unpublished setup, so one agent sees a different prompt and tool set than its neighbors. Model selection captures one provider/model/effort value before prompt assembly and applies it to the same step's request and retries; a later concurrent change waits for another step.
Token effect
Scoped contributions affect only that agent and disappear on disposal.
KV Cache effect
Setup or reload that changes prompt sections, tool definitions, or request listeners may invalidate reuse from the first affected request token.
Optional model-switch notice
What the model sees
The scoped listener appends [model changed: assistant turns above this point were generated by <previous>; the session continues with <next>] when an accepted step's fixed route differs from the last request. Labels include providers only for cross-provider switches. Initial requests, effort-only changes, rejected steps, and empty no-request decisions add nothing. Failure before header persistence causes the next request step to repeat the notice.
Token effect
Each admitted notice adds retained user-message text; selection events themselves add no prompt text.
KV Cache effect
Notices append without replacing earlier history. Switching models may prevent reuse of the previous model's cache; cache availability is provider-owned.
Known Limitations and Deferred Work
These limits define when this package needs special care. They are current package constraints, not a task backlog.
- Initiator scope is process-local — workers, child processes, HTTP, durable queues, and restarts must materialize any required identity explicitly.
- Ambient identity may outlive liveness — consumers still check
agent.status, cancellation, and the owning capability contract before lifecycle-sensitive work. - Creation listeners share the initialization lifetime. An
agent/createdlistener must not awaitagent.whenIdle()or its own owner's disposal: those operations wait for creation to finish. Return only after required asynchronous tool and prompt installation completes. cancel()clears the inbox by default — it aborts the in-flight turn plus queued and steering work;cancel(cause, { keepInbox: true })aborts only the turn and preserves pending items, and there is no step-only abort that keeps the turn running.- Each additional
UserMessagecarries exactly oneMessageSource— contributions from several plugins merged onto one message collapse under one source, so the message cannot name several producers.
Dev Note
Working context for maintainers — click to expand
This Dev Note is working context for maintainers; it is explicitly non-authoritative. Open, undecided directions: inter-agent channels beyond delegation — shared state, streaming child output, and background or poll semantics remain outside the current delegation seam; and the SessionStartSource values 'clear'/'compact' are reserved with no emitter yet, pending the driving subsystems.