diff --git a/.agents/skills/dsh-find-simplifications/SKILL.md b/.agents/skills/dsh-find-simplifications/SKILL.md index 8fad167a60..59dde3df0d 100644 --- a/.agents/skills/dsh-find-simplifications/SKILL.md +++ b/.agents/skills/dsh-find-simplifications/SKILL.md @@ -41,6 +41,14 @@ Use parallel subagents when the user asks for breadth or many candidates. Give e If subagents are unavailable, simulate the same breadth yourself. Do not let the first good candidate stop the survey. +Start with the largest production-code deltas. A broad simplification audit that stops after obvious unused symbols can miss the files where duplicated lifecycle or defensive machinery carries most of the cost. + +## Audit Trust And Lifecycle Boundaries + +Classify every defensive copy, freeze, validator, and callback capture by the boundary it crosses. Same-process typed service/plugin calls ordinarily borrow readonly values; parser/config, queue, model/tool JSON, durable/file, worker, process, and wire boundaries own or validate data. Tests built around hostile getters, fake typed objects, callback replacement, or mutation after a same-process handoff are evidence of a potentially speculative contract, not automatic justification for keeping it. + +For complex asynchronous code, draw the ownership graph and map each sentinel, readiness promise, cancellation path, disposer, and state flag to a distinct owner or transition. When several mechanisms mirror the same liveness or settlement fact, propose one transaction or lifecycle controller instead. Preserve separate machinery where it protects a real boundary: synchronous publication and rollback, callback containment, first-terminal-outcome arbitration, worker/process ownership, or dispose-to-quiescence. + ## Prove Or Reject Each Candidate For every symbol or behavior, classify consumers before writing: @@ -60,7 +68,7 @@ Reject or downgrade a candidate when: ## Write The RFC -Create one file per durable proposal under `docs/rfc/proposed/yyyy-mm-dd-topic.md` and add it to the Proposed table in `docs/rfc/README.md`. Keep prose paragraphs on one physical line and use relative Markdown links. +Create one file per durable proposal under `docs/rfc///yyyy-mm-dd-topic.md`, following the lifecycle/classification contract in `docs/rfc/README.md`. Regenerate `docs/rfc/INDEX.md`; never add a manual RFC table to the README. Keep prose paragraphs on one physical line and use relative Markdown links. Prefer this shape, adjusting when the idea needs it: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index acb74c0c26..4840b88192 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -82,6 +82,20 @@ jobs: - name: Install (immutable) run: pnpm install --frozen-lockfile + # The snapshot lane REPLAYS the sandbox example's recorded scenarios, + # re-executing their bash calls under a real runner. ubuntu-latest has + # no bubblewrap preinstalled and no built Landlock launcher, so without + # this the confined executions fail closed (SANDBOX_UNAVAILABLE). Same + # install as sandbox.yml's bwrap leg (incl. the Ubuntu 24.04 AppArmor + # userns knob). + - name: Install bubblewrap (unrestrict userns) + if: matrix.lane == 'snapshot' + run: | + sudo apt-get update -q + sudo apt-get install -yq bubblewrap + sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0 \ + || echo "apparmor userns knob absent — the functional probe decides" + - uses: actions/cache@v4 if: matrix.lane == 'lint' with: diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index c0371947fd..e06fb025bd 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -82,6 +82,20 @@ jobs: - name: Install (immutable) run: pnpm install --frozen-lockfile + # The with-key escalation e2e (examples/sandbox-acp-agent/tests/ + # escalation.e2e.ts) self-skips without a usable runner — without this + # step it would never actually execute anywhere (CI had no bwrap, dev + # macs run Seatbelt instead), which is exactly how a broken harness + # composition once survived unseen. Same recipe as ci.yml's bwrap + # steps; the userns knob is best-effort (absent on pre-24.04 kernels, + # the probe decides). + - name: Install bubblewrap (unrestrict userns) + run: | + sudo apt-get update -q + sudo apt-get install -yq bubblewrap + sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0 \ + || echo "apparmor userns knob absent — the functional probe decides" + # Guard against a false green: the e2e suites self-skip when the key is # absent, so a missing/misconfigured secret would otherwise pass as # "all skipped". This job only runs on trusted events (the `if:` above diff --git a/.github/workflows/sandbox.yml b/.github/workflows/sandbox.yml new file mode 100644 index 0000000000..561d74b587 --- /dev/null +++ b/.github/workflows/sandbox.yml @@ -0,0 +1,124 @@ +# Sandbox CI: the keyless real-kernel confinement proofs. A separate workflow +# from ci.yml because the axis is different — these jobs fan out over +# OS×runner (kernel capabilities), not node versions. The Landlock launcher +# arrives from the registry with `pnpm install` (the npm package family +# `node-addon-landlock-run`, built and released from its own repository), so +# these legs exercise the true consumer path — nothing is compiled here. +name: Sandbox + +on: + push: + branches: [main, master] + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + # Keyless real-kernel sandbox proofs (sandbox RFC § Testing): each ladder + # rung is only provable on a host where it enforces, so this job fans out + # an OS×runner matrix — bwrap and Landlock on Linux (separate legs: the + # Landlock files force the bwrap rung off, so each leg proves exactly one + # rung; Landlock twice, once per architecture, each confining through the + # registry-installed launcher), Seatbelt on macOS (sandbox-exec ships with + # the OS). One node + # version only: kernel confinement does not vary by node, and ci.yml's + # node matrix already covers the node axis. + # + # The e2e files self-skip where their runner is absent, so a leg that lost + # its runner (no bwrap, kernel without Landlock, macOS without + # sandbox-exec) would otherwise pass as a false green — the same trap + # e2e.yml's key preflight guards against. Each leg therefore asserts BOTH + # its platform files actually ran: `Test Files 2 passed (2)`, no skips. + sandbox-e2e: + strategy: + fail-fast: false + matrix: + include: + - os: ubuntu-latest + runner: bwrap + - os: ubuntu-24.04 + runner: landlock + - os: ubuntu-24.04-arm + runner: landlock + - os: macos-latest + runner: seatbelt + name: sandbox e2e (${{ matrix.runner }}, ${{ matrix.os }}) + runs-on: ${{ matrix.os }} + timeout-minutes: 20 + steps: + - uses: actions/checkout@v6 + + - uses: actions/setup-node@v6 + with: + node-version: 24 + + - name: Enable corepack (pnpm) + run: corepack enable + + - name: Install (immutable) + run: pnpm install --frozen-lockfile + + # The bwrap rung needs bubblewrap on PATH and unprivileged user + # namespaces. Ubuntu 24.04 gates the latter behind an AppArmor knob; + # lift it best-effort — on images where the knob is absent the + # functional probe (and the run-guard below) is the arbiter anyway. + - name: Install bubblewrap (unrestrict userns) + if: matrix.runner == 'bwrap' + run: | + sudo apt-get update -q + sudo apt-get install -yq bubblewrap + sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0 \ + || echo "apparmor userns knob absent — the functional probe decides" + + # The unit suite runs on ubuntu in `checks`; this is the one darwin leg + # in the workflow, so run it here too — the platform-dependent unit + # expectations (Seatbelt path canonicalization: /tmp IS /private/tmp) + # take their darwin branch only on this runner. + - name: Unit tests (darwin parity) + if: matrix.runner == 'seatbelt' + run: pnpm run test + + - name: Sandbox e2e (real kernel confinement, world-verified) + # NO_COLOR: vitest force-enables ANSI color under GITHUB_ACTIONS even + # without a TTY, which would thread escape codes through the summary + # line the run-guard greps. + env: + NO_COLOR: 1 + run: | + set -u +e -o pipefail + out=$(pnpm exec vitest run --config vitest.e2e.config.ts \ + packages/sandbox/sandbox-local/tests/${{ matrix.runner }}.e2e.ts \ + packages/bash/bash-sandbox/tests/${{ matrix.runner }}.e2e.ts 2>&1); status=$? + echo "$out" + [ "$status" -eq 0 ] + # Both platform files must have RUN — a self-skip (runner missing on + # the very platform that exists to prove it) is a failure, not a pass. + echo "$out" | grep -qE 'Test Files[[:space:]]+2 passed \(2\)' + + # Publish-path rehearsal, Landlock legs only (the pack gates need built + # lib/). The e2e packs the workspace closure, installs the tarballs + # into a throwaway consumer — npm pulling `node-addon-landlock-run` + # and its platform package from the registry, the true consumer path — + # and confines through the INSTALLED launcher, asserting it executable + # apart (a mode-stripped binary must not masquerade as a non-enforcing + # kernel). Same no-silent-skip guard as above. + - name: Build packages (lib/ for the pack rehearsal) + if: matrix.runner == 'landlock' + run: pnpm run build + + - name: Packed-distribution e2e (pack → install → confine) + if: matrix.runner == 'landlock' + env: + NO_COLOR: 1 + run: | + set -u +e -o pipefail + out=$(pnpm exec vitest run --config vitest.e2e.config.ts \ + packages/sandbox/sandbox-local/tests/packed-install.e2e.ts 2>&1); status=$? + echo "$out" + [ "$status" -eq 0 ] + echo "$out" | grep -qE 'Test Files[[:space:]]+1 passed \(1\)' diff --git a/AGENTS.md b/AGENTS.md index 20abf56c87..843e09e8b2 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -15,6 +15,7 @@ packages/ Harness packages at packages///, all named @deepseek-ai llm/ LLM seam + the DeepSeek adapters (hand-rolled + pi-ai design twin) bash/ bash executor seam + local impl + model-facing bash tools fs/ filesystem seam + local impl + policy gate + read/write/edit tools + skill/ skill provider registry + local impl + catalog/loader tool web/ web seam + search/fetch providers + model-facing web tools compact/ compaction seam + basic backend subagent/ subagent seam + spawn/fork/ACP backends + delegation tool @@ -24,7 +25,7 @@ packages/ Harness packages at packages///, all named @deepseek-ai cordis/ self-referential toolset: the agent inspects/mounts plugins in its own runtime hooks/ Claude Code / Codex hook bridges + shared wire-protocol library session-persistence/ persistence seam + JSONL/SQLite backends - ui/ ACP bridge, app-boot glue, stdio/ACP app bins, user-interaction seam, ask-user tool + ui/ ACP bridge, app-boot glue, stdio/ACP app bins, user-approval and user-interaction seams, ask-user tool support/ dev/test infrastructure packages util/ zero-dependency utilities examples/ Runnable demos: thin cordis.yml leaves over the app packages (see examples/AGENTS.md) @@ -71,7 +72,7 @@ pnpm run hygiene out=$(printf 'echo ci smoke\n' | pnpm run demo:echo 2>&1) printf '%s\n' "$out" | grep -q '\[tool call\] echo({"text":"ci smoke"})' printf '%s\n' "$out" | grep -q '\[tool result\] ECHO: CI SMOKE' -ls .sessions/_no-cwd/main-session-*.jsonl >/dev/null +test -n "$(find .sessions -path '.sessions/cwd-*/main-session-*.jsonl' -type f -print -quit)" rm -rf .sessions pnpm exec vitest run --config vitest.e2e.config.ts packages/ui/stdio-agent/tests/built-bin.e2e.ts packages/ui/acp-agent/tests/built-bin.e2e.ts packages/workflow/workflow-workerthread/tests/built-worker.e2e.ts packages/code-runtime/code-runtime-worker/tests/built-lib.e2e.ts ``` diff --git a/docs/agent-lifecycle.md b/docs/agent-lifecycle.md index d6d9ab1ba7..fa2c4bd0fb 100644 --- a/docs/agent-lifecycle.md +++ b/docs/agent-lifecycle.md @@ -39,6 +39,7 @@ sequenceDiagram Tools-->>Session: tool-owned events when applicable Driver->>Session: tool/result and step/end Driver->>Hooks: agent/turn-continuation waterfall + Driver->>Hooks: agent/turn-stop serial terminal checkpoint Driver->>Session: turn/end Driver->>Persistence: session/flush parallel checkpoint Driver-->>SDK: agent/status idle diff --git a/docs/architecture.md b/docs/architecture.md index 01ae6ef7cb..a1f0cae9a8 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1,19 +1,18 @@ # DeepSeek Harness Architecture -The project is an SDK for building agent harnesses. The idea is to have **everything as a plugin**. For example, the agent loop is just one plugin shipped by default. +The **DeepSeek Harness SDK** builds agent harnesses on Cordis. The principle is simple: **everything is a plugin**. The shipped loop is one plugin, not a privileged kernel. ## Overview -The project is based on [Cordis](cordis-primer.md). +A harness is one [Cordis](cordis-primer.md) context. Packages contribute service keys, typed events, and disposable registrations: services expose stable calls (`ctx.llm`, `ctx.tools`, `ctx.sessions`), events provide interception and notifications (`agent/request`, `tools/pre-execute`, `session/event`), and registrations install prompt sections, tools, providers, adapters, or listeners. -A running harness is one Cordis context. Packages contribute service keys, typed events, and disposable registrations to that context. Services provides stable call signatures (`ctx.llm`, `ctx.tools`, `ctx.sessions`); events are interception and notification points (`agent/request`, `tools/pre-execute`, `session/event`); registrations install prompt sections, tool schemas, providers, adapters, and listeners. - -Composition is preferred over inheritance. `packages/core/` is a repository grouping for the default agent flow; capability around it are equally first-class plugins from a Cordis perspective. +`packages/core/` groups the default agent flow; surrounding capabilities are equally first-class Cordis plugins. ### Default Services | ctx key | Package | Role | |---|---|---| +| — | [`dsh-scope`](../packages/core/scope/README.md) | scoped-context registration primitive (library) | | `ctx.sessions` | `dsh-session` | in-memory event-sourced sessions | | `ctx.systemPrompt` | `dsh-system-prompt` | ordered prompt sections, tool schemas, and prompt variables | | `ctx.tools` | `dsh-tools` | tool registry and [execution pipeline](tool-execution-pipeline.md) | @@ -26,8 +25,10 @@ Composition is preferred over inheritance. `packages/core/` is a repository grou |---|---|---| | `ctx.llm` | [`llm/`](../packages/llm/README.md) | adapter registry and streaming model calls | | `ctx.bash` | [`bash/`](../packages/bash/README.md) | foreground/background command execution | +| `ctx.sandbox` | [`sandbox/`](../packages/sandbox/README.md) | same-world process confinement (argv wrapping, per-call policy) | | `ctx.codeRuntime` | [`code-runtime/`](../packages/code-runtime/README.md) | model-written program execution | | `ctx.fs` | [`fs/`](../packages/fs/README.md) | filesystem provider primitives and policy events | +| `ctx.skills` | [`skill/`](../packages/skill/README.md) | skill provider registry and progressive disclosure | | `ctx.web` | [`web/`](../packages/web/README.md) | search/fetch provider registries | | `ctx.compact` | [`compact/`](../packages/compact/README.md) | session-log compaction | | `ctx.subagents` | [`subagent/`](../packages/subagent/README.md) | named delegation providers | @@ -36,12 +37,10 @@ Composition is preferred over inheritance. `packages/core/` is a repository grou ## Event -Events are the harness extension API used by Service. The generated [events catalog](cordis-catalog/events.md) is the exhaustive reference. The [producer/consumer map](event-producer-consumer.md) shows which packages emit or listen to each event. +Events form the service extension API; see the exhaustive [events catalog](cordis-catalog/events.md) and [producer/consumer map](event-producer-consumer.md). ### Event Domains -Pick the event domain for new behavior: - - **Session events** are durable, replayable facts. Turn and step boundaries, user input, assistant output, tool calls, tool results, steering, compaction records, and tool-owned durable facts append to the session log and flow through `session/event`. - **Agent events** carry the live `Agent` handle for status, diagnostics, prompt admission, call-config shaping, result validation, and continuation policy. - **Capability events** belong to the seam that owns the action. `tools/*`, `llm/*`, `system-prompt/*`, `fs/*`, and `subagent/*` let policy and adapters attach without importing the loop. @@ -52,14 +51,16 @@ Waterfall events behave like around-middleware: a listener delegates by calling ## Default Loop Lifecycle -The shipped loop drains queued work, assembles a request, streams a model answer, executes tools, decides whether to continue, and checkpoints durable state. The important part is where it pauses: each pause is a documented service call or event that another plugin can use. +The shipped loop drains work, assembles requests, streams model answers, executes tools, applies continuation policy, and checkpoints state. Every pause is a service call or event available to plugins. A **session** is one agent's append-only event log. A **turn** drains one queued batch and runs until the model stops asking for tools and no plugin requests continuation. A **step** is one model request plus the tool executions caused by that response. In the flow below ([sequence companion](agent-lifecycle.md)), quoted names are durable session events and event names are extension points. ### Turn Flow ```text -create agent -> emit agent/session-start(source) +prepare private session + agent.ctx -> await unpublished setup + -> enter session + agent -> session/created -> agent/created + -> enable driving -> agent/session-start(source) -> start driver forever: wait for queued messages emit agent/status(running) @@ -81,19 +82,20 @@ forever: 'assistant/message' each tool call: 'tool/call' - tools/pre-execute -> tools/execute -> tools/post-execute + tools/pre-execute -> monotonic guards -> tools/execute -> tools/post-execute -> tools/result 'tool/result' append post-tool context and steering 'step/end' agent/turn-continuation + agent/turn-stop (terminal policy) stop unless tools or continuation policy ask for another step 'turn/end' checkpoint persistence and notify idle/running status ``` -Prompt assembly is single-path: `renderPrompt(assemble({ agent }))` IS the system prompt sent to the model. Plugins contribute ordered sections (static or computed from the per-call `AssembleContext`), tool schemas, and named variables interpolated as `{{name}}` at render — strictly, so an unknown or valueless reference fails the turn instead of shipping a hole. `dsh-system-prompt` owns the openers — the static `harness:identity` section (order −100) and the deployment's persona (order 0, its `persona` config, shared context-wide) — while the shipped loop registers the `model`/`cwd` variables; prompt-fact ownership is pinned by the [prompt-variables RFC](rfc/implemented/architecture/2026-07-05-prompt-variables-and-tool-guidance-ownership.md). +Prompt assembly is single-path: the loop sends `renderPrompt(await assemble(assembleContextFor(agent)))`; the helper couples the explicit agent and scope. Plugins contribute ordered sections, tool schemas, and named variables interpolated as `{{name}}` at render — strictly, so an unknown or valueless reference fails the turn instead of shipping a hole. `dsh-system-prompt` owns the openers — the static `harness:identity` section (order −100) and the deployment's global default persona (order 0, shadowable by a same-named agent-scoped section) — while the loop registers the `model`/`cwd` variables; prompt-fact ownership is pinned by the [prompt-variables RFC](rfc/implemented/architecture/2026-07-05-prompt-variables-and-tool-guidance-ownership.md). -Post-tool context lands after all tool results so tool-call/result adjacency stays stable. Steering drains between steps; leftover steering after a turn is re-queued as ordinary input. +Post-tool context lands after all tool results so tool-call/result adjacency stays stable. Steering drains between steps; ordinary leftover steering after a turn is re-queued as input. A terminal `agent/turn-stop` is the explicit exception: it runs after ordinary continuation and steering folding, then remains authoritative through turn close and flush so steering from those later listeners is discarded rather than becoming another step or turn; ordinary queued prompts are preserved. ### Failure Boundaries @@ -103,7 +105,11 @@ Every session event is turn-enclosed. Reloading a crashed session preserves the ### Agent Handles -`ctx.agents` owns live agents and returns an `AgentHandle { agent, dispose() }`. `Agent` is the API other plugins drive: `send()` queues work, `steer()` injects mid-turn content, `inject()` appends context and opens a one-shot injection turn when idle, `cancel()` is the public stop primitive, and `whenIdle()` observes quiescence. Lifecycle owners tear down with `await dispose()`. +`ctx.agents` owns live agents and returns an `AgentHandle { agent, dispose() }`. `Agent` is the API other plugins drive: `send()` queues work, `steer()` injects mid-turn content, `inject()` appends context and opens a one-shot injection turn when idle, `cancel()` is the public stop primitive, and `whenIdle()` observes quiescence. The caller fiber and concrete factory provider structurally co-own programmatic lifecycles; a consumer handle is the only non-structural teardown capability, and every owner reaches the same awaited disposer. + +### Agent Scope + +Every live agent owns `agent.ctx` ([`dsh-scope`](../packages/core/scope/README.md), keyed by the agent). Its registrations are visible only to that agent, shadow same-named globals, and unwind with it. Its listeners hear only that agent's dispatches; an opaque carrier routes while the real subject stays explicit. `CreateAgentOptions.setup(agentCtx)` composes this world before publication and does not drive. Dev invariants and `verify-scoped-dispatch` keep carrier/subject identity aligned with event declarations. Rationale: [agent-scope RFC](rfc/implemented/architecture/2026-07-08-agent-scope-contexts.md); subagent `persona`, `toolFilter`, and `maxDepth` are the separate [composition-controls feature](rfc/implemented/feature/2026-07-12-subagent-persona-tool-filter-and-depth.md). ## State @@ -125,13 +131,13 @@ Streaming is a raw chunk protocol (`block-start` through `finish`) with `BlockAs ### Capability Pattern -A swappable capability usually splits into **interface / implementation / consumer**: the interface owns the `ctx` key and event names; an implementation registers a backend; a consumer exposes model-facing behavior through `ctx.tools` or prompt assembly. The bash trio is the reference shape, and the [capability graph](capability-seams.md) shows the current package families. +A swappable capability usually splits into **interface / implementation / consumer**: the interface owns its `ctx` key and events, an implementation registers a backend, and a consumer exposes model behavior through tools or prompts. Bash is the reference; the [capability graph](capability-seams.md) shows every family. -Some cases bend the template deliberately. LLM keeps interface and consumer event names together because adapters are the implementations. Filesystem adds policy checks around provider primitives. Web is one service with search and fetch provider registries, so provider swaps do not rename model tools. Subagents use a named provider registry because multiple delegation backends can coexist; `spawn` starts fresh, `fork` seeds from the parent's completed-turn prefix, and ACP can drive an out-of-process child ([subagent.md](core-data-structures/subagent.md)). +Some seams bend the template deliberately. LLM keeps interface and consumer vocabulary together because adapters are the implementations. Filesystem adds policy gates around provider primitives. Web is one service with search and fetch provider registries, so provider swaps do not rename model tools. Skills and subagents use named provider registries; local skills scan project/user roots, and other providers can add embedded or remote catalogs without registry/tool changes. Subagents spawn fresh, fork from the parent's completed-turn prefix, or use ACP children ([subagent.md](core-data-structures/subagent.md)). ### Bundles And Apps -`dsh-agent-core` is the default bundle: one plugin loading the agent loop ([README](../packages/core/agent-core/README.md)). App packages compose it with a front end and own the entrypoint `bin`: `dsh-stdio-agent` for the terminal REPL, and `dsh-acp-agent` for ACP over JSON-RPC stdio with no stdout logger ([ui/](../packages/ui/README.md)). A deployment is a thin `cordis.yml` leaf: swappable backends, one app entry, and optional product tools ([examples/](../examples/AGENTS.md), [runnable wirings](cookbook/extension-cookbook.md#runnable-wirings), [graph atlas](graph-atlas.md)). +`dsh-agent-core` is the default composition bundle: one plugin loading the shared spine ([README](../packages/core/agent-core/README.md)). App packages compose it with a front door and boot `bin`: `dsh-stdio-agent` for terminal REPL, and `dsh-acp-agent` for ACP over JSON-RPC stdio with no stdout logger ([ui/](../packages/ui/README.md)). A deployment is a thin `cordis.yml` leaf: swappable backends, one app entry, and optional product tools ([examples/](../examples/AGENTS.md), [runnable wirings](cookbook/extension-cookbook.md#runnable-wirings), [graph atlas](graph-atlas.md)). ### Where New Behavior Goes @@ -143,17 +149,20 @@ New behavior should attach to a documented extension point; changing the shipped | Add a model-facing capability | register a tool on `ctx.tools`; schemas flow into prompt assembly | | Add command execution | implement and register a `ctx.bash` backend | | Add filesystem access or policy | implement a `ctx.fs` provider or listen on `fs/*` policy events | -| Intercept prompts, requests, tool use, or continuation | listen on the relevant `agent/*` or `tools/*` waterfall | +| Confine spawned processes | a `ctx.sandbox` backend; consumers wrap their argv before spawning | +| Intercept prompts, requests, tool use, or continuation | listen on the relevant `agent/*` or `tools/*` waterfall; use serial `agent/turn-stop` for a monotonic terminal stop | | Add a session-stable request prefix outside history | compose it on `agent/session-prefix`, once per loop instance; logged on the request header | | Add UI or editor integration | drive `ctx.agents` and render from `session/event` | | Add durable session state | add a `SessionEventMap` member and render/replay from the log | | Fork a live session | use `ctx.sessions.fork(source, boundary?, childSessionId?)` | +| Scope a tool, prompt section, or listener to ONE agent | register it through that agent's `agent.ctx` (see Agent Scope) | The [extension cookbook](cookbook/extension-cookbook.md) carries plugin skeletons and the feature-to-seam map; step-by-step guides cover [packages](cookbook/adding-a-package.md), [tools](cookbook/adding-a-tool.md), [LLM adapters](cookbook/adding-an-llm-adapter.md), and [vendored packages](cookbook/adding-a-vendored-package.md). ## Quick Reference +- Domain terms in the [glossary](glossary.md) - Type definitions in [core-data-structures/](core-data-structures/core.md) - Exact event and service signatures in [events](cordis-catalog/events.md) - [services](cordis-catalog/services.md) catalogs - package contracts in the [package map](../packages/README.md) -- [RFCs](rfc/README.md) \ No newline at end of file +- [RFCs](rfc/README.md) diff --git a/docs/capability-seams.md b/docs/capability-seams.md index 1c24a2916e..a6c9c766aa 100644 --- a/docs/capability-seams.md +++ b/docs/capability-seams.md @@ -29,23 +29,33 @@ flowchart LR pkg_tools["tools"] pkg_tool_fs["tool-fs"] pkg_tool_web["tool-web"] - svc_tools["ctx.tools
Tool registry and execution waterfall"] + svc_tools["ctx.tools
Tool registry and guarded execution pipeline"] pkg_tool_ask_user["tool-ask-user"] pkg_tool_bash["tool-bash"] pkg_tool_cordis["tool-cordis"] + pkg_tool_skill["tool-skill"] pkg_tool_subagent["tool-subagent"] pkg_tool_todo["tool-todo"] pkg_user_interaction["user-interaction"] svc_userInteraction["ctx.userInteraction
Human question/answer seam"] pkg_stdio_agent["stdio-agent"] + pkg_skill["skill"] + svc_skills["ctx.skills
Skill provider registry"] + pkg_skill_local["skill-local"] svc_agents["ctx.agents
Agent registry"] svc_agentLoop["ctx.agentLoop
Concrete loop driver"] pkg_agent_core["agent-core"] pkg_bash["bash"] svc_bash["ctx.bash
Bash executor seam"] pkg_bash_local["bash-local"] + pkg_bash_sandbox["bash-sandbox"] pkg_hooks_claude["hooks-claude"] pkg_hooks_codex["hooks-codex"] + pkg_sandbox["sandbox"] + svc_sandbox["ctx.sandbox
Process-sandbox seam"] + pkg_sandbox_local["sandbox-local"] + pkg_approval["approval"] + svc_approval["ctx.approval
Approval seam"] pkg_code_runtime["code-runtime"] svc_codeRuntime["ctx.codeRuntime
Code-execution seam"] pkg_code_runtime_worker["code-runtime-worker"] @@ -71,11 +81,14 @@ flowchart LR svc_workflows["ctx.workflows
Workflow script engine"] pkg_workflow_workerthread["workflow-workerthread"] pkg_tool_workflow["tool-workflow"] + pkg_acp --> svc_approval pkg_acp --> svc_userInteraction pkg_agent --> svc_agents pkg_agent_loop --> svc_agentLoop + pkg_approval --> svc_approval pkg_bash --> svc_bash pkg_bash_local --> svc_bash + pkg_bash_sandbox --> svc_bash pkg_code_runtime --> svc_codeRuntime pkg_code_runtime_worker --> svc_codeRuntime pkg_compact --> svc_compact @@ -86,10 +99,14 @@ flowchart LR pkg_llm_deepseek --> svc_llm pkg_llm_pi_ai --> svc_llm pkg_llm_replay --> svc_llm + pkg_sandbox --> svc_sandbox + pkg_sandbox_local --> svc_sandbox pkg_session --> svc_sessions pkg_session_persistence --> svc_sessionPersistence pkg_session_persistence_jsonl --> svc_sessionPersistence pkg_session_persistence_sqlite --> svc_sessionPersistence + pkg_skill --> svc_skills + pkg_skill_local --> svc_skills pkg_stdio_agent --> svc_userInteraction pkg_subagent --> svc_subagents pkg_subagent_acp --> svc_subagents @@ -112,6 +129,8 @@ flowchart LR svc_agents --> pkg_invariants svc_agents --> pkg_stdio_agent svc_agents --> pkg_subagent_inprocess + svc_approval --> pkg_tool_bash + svc_approval --> pkg_tools svc_bash --> pkg_hooks_claude svc_bash --> pkg_hooks_codex svc_bash --> pkg_tool_bash @@ -120,6 +139,7 @@ flowchart LR svc_fs --> pkg_tool_fs svc_llm --> pkg_agent_loop svc_llm --> pkg_compact_basic + svc_sandbox --> pkg_bash_sandbox svc_sessionPersistence --> pkg_acp svc_sessionPersistence --> pkg_agent_loop svc_sessions --> pkg_agent @@ -127,6 +147,7 @@ flowchart LR svc_sessions --> pkg_invariants svc_sessions --> pkg_session_persistence svc_sessions --> pkg_subagent_inprocess + svc_skills --> pkg_tool_skill svc_subagents --> pkg_tool_subagent svc_systemPrompt --> pkg_agent_loop svc_systemPrompt --> pkg_tool_fs @@ -138,6 +159,7 @@ flowchart LR svc_tools --> pkg_tool_bash svc_tools --> pkg_tool_cordis svc_tools --> pkg_tool_fs + svc_tools --> pkg_tool_skill svc_tools --> pkg_tool_subagent svc_tools --> pkg_tool_todo svc_tools --> pkg_tool_web @@ -155,11 +177,14 @@ flowchart LR | `ctx.sessions` | `core` | [`session`](../packages/core/session) | - | [`agent-loop`](../packages/core/agent-loop), [`agent`](../packages/core/agent), [`session-persistence`](../packages/session-persistence/session-persistence), [`subagent-inprocess`](../packages/subagent/subagent-inprocess), [`invariants`](../packages/support/invariants) | - | Owns append-only Session instances and emits the durable session event feed. | | `ctx.sessionPersistence` | `seam` | [`session-persistence`](../packages/session-persistence/session-persistence) | [`session-persistence-jsonl`](../packages/session-persistence/session-persistence-jsonl), [`session-persistence-sqlite`](../packages/session-persistence/session-persistence-sqlite) | [`agent-loop`](../packages/core/agent-loop), [`acp`](../packages/ui/acp) | - | Backends persist the same SessionEvent vocabulary; apps choose a backend at composition time. | | `ctx.systemPrompt` | `core` | [`system-prompt`](../packages/core/system-prompt) | - | [`agent-loop`](../packages/core/agent-loop), [`tools`](../packages/core/tools), [`tool-fs`](../packages/fs/tool-fs), [`tool-web`](../packages/web/tool-web) | - | Collects prompt sections and model-facing tool schemas for each step. | -| `ctx.tools` | `core` | [`tools`](../packages/core/tools) | - | [`agent-loop`](../packages/core/agent-loop), [`tool-ask-user`](../packages/ui/tool-ask-user), [`tool-bash`](../packages/bash/tool-bash), [`tool-cordis`](../packages/cordis/tool-cordis), [`tool-fs`](../packages/fs/tool-fs), [`tool-subagent`](../packages/subagent/tool-subagent), [`tool-todo`](../packages/todo/tool-todo), [`tool-web`](../packages/web/tool-web), [`acp`](../packages/ui/acp) | - | Registers tool definitions, exposes schemas to the prompt, and routes calls through tools/pre-execute and tools/post-execute. | +| `ctx.tools` | `core` | [`tools`](../packages/core/tools) | - | [`agent-loop`](../packages/core/agent-loop), [`tool-ask-user`](../packages/ui/tool-ask-user), [`tool-bash`](../packages/bash/tool-bash), [`tool-cordis`](../packages/cordis/tool-cordis), [`tool-fs`](../packages/fs/tool-fs), [`tool-skill`](../packages/skill/tool-skill), [`tool-subagent`](../packages/subagent/tool-subagent), [`tool-todo`](../packages/todo/tool-todo), [`tool-web`](../packages/web/tool-web), [`acp`](../packages/ui/acp) | - | Registers capabilities, owns Code Mode transport, and routes calls through pre-policy, monotonic guards, around dispatch, post-policy, and final-result observation. | | `ctx.userInteraction` | `seam` | [`user-interaction`](../packages/ui/user-interaction) | [`stdio-agent`](../packages/ui/stdio-agent), [`acp`](../packages/ui/acp) | [`tool-ask-user`](../packages/ui/tool-ask-user), [`stdio-agent`](../packages/ui/stdio-agent), [`acp`](../packages/ui/acp) | - | UI front doors provide the active human-answer provider; tool-ask-user pauses a tool call on the provider-neutral ask() promise. | +| `ctx.skills` | `seam` | [`skill`](../packages/skill/skill) | [`skill-local`](../packages/skill/skill-local) | [`tool-skill`](../packages/skill/tool-skill) | - | Merges provider skill catalogs; tool-skill renders the session-prefix catalog and loads complete skill bodies. | | `ctx.agents` | `core` | [`agent`](../packages/core/agent) | - | [`agent-loop`](../packages/core/agent-loop), [`acp`](../packages/ui/acp), [`subagent-inprocess`](../packages/subagent/subagent-inprocess), [`stdio-agent`](../packages/ui/stdio-agent), [`invariants`](../packages/support/invariants) | - | Owns live Agent handles and the create/resume factory seam. | | `ctx.agentLoop` | `bundle` | [`agent-loop`](../packages/core/agent-loop) | - | [`agent-core`](../packages/core/agent-core) | - | The one concrete loop plugin; extension packages depend on dsh-agent events and services, not on this package. | -| `ctx.bash` | `seam` | [`bash`](../packages/bash/bash) | [`bash-local`](../packages/bash/bash-local) | [`tool-bash`](../packages/bash/tool-bash), [`hooks-claude`](../packages/hooks/hooks-claude), [`hooks-codex`](../packages/hooks/hooks-codex) | - | The model-facing bash tools and hook bridges consume this seam; sandboxed or remote executors can replace bash-local. | +| `ctx.bash` | `seam` | [`bash`](../packages/bash/bash) | [`bash-local`](../packages/bash/bash-local), [`bash-sandbox`](../packages/bash/bash-sandbox) | [`tool-bash`](../packages/bash/tool-bash), [`hooks-claude`](../packages/hooks/hooks-claude), [`hooks-codex`](../packages/hooks/hooks-codex) | - | The model-facing bash tools and hook bridges consume this seam; sandboxed or remote executors replace bash-local without touching them. | +| `ctx.sandbox` | `seam` | [`sandbox`](../packages/sandbox/sandbox) | [`sandbox-local`](../packages/sandbox/sandbox-local) | [`bash-sandbox`](../packages/bash/bash-sandbox) | - | Consumers hand over the exact argv they are about to spawn; same-world backends wrap it under a per-call policy and report enforcement. | +| `ctx.approval` | `seam` | `approval` | [`acp`](../packages/ui/acp) | [`tools`](../packages/core/tools), [`tool-bash`](../packages/bash/tool-bash) | - | One-shot permission decisions dispatched over the `approval/request` waterfall; answerers are listeners (the ACP bridge for its own agents), absence fails closed to `unavailable`. | | `ctx.codeRuntime` | `seam` | [`code-runtime`](../packages/code-runtime/code-runtime) | [`code-runtime-worker`](../packages/code-runtime/code-runtime-worker) | [`tools`](../packages/core/tools) | - | Runs one model-written program against host-provided async bindings; backends differ by substrate and language (the tool registry consumes it for Code Mode). | | `ctx.fs` | `seam` | [`fs`](../packages/fs/fs) | [`fs-local`](../packages/fs/fs-local) | [`tool-fs`](../packages/fs/tool-fs) | [`fs-policy`](../packages/fs/fs-policy) | tool-fs executes read/write/edit through ctx.fs; fs-policy contributes observed-state checks through the fs/* event gate. | | `ctx.compact` | `seam` | [`compact`](../packages/compact/compact) | [`compact-basic`](../packages/compact/compact-basic) | [`compact-basic`](../packages/compact/compact-basic) | - | The basic backend currently consumes the pre-step event directly; a model-facing compact tool remains deferred. | diff --git a/docs/config-catalog.md b/docs/config-catalog.md index 772924df9c..1e8bd9e508 100644 --- a/docs/config-catalog.md +++ b/docs/config-catalog.md @@ -31,7 +31,7 @@ export interface AcpConfig { Depends on: `Stream` (`@agentclientprotocol/sdk`) -Source: [`packages/ui/acp/src/index.ts:236`](../packages/ui/acp/src/index.ts) +Source: [`packages/ui/acp/src/index.ts:248`](../packages/ui/acp/src/index.ts) ## `@deepseek-ai/dsh-acp-agent` @@ -56,10 +56,12 @@ export interface Config { tools?: ToolsConfig /** Directory the JSONL session backend writes under. Defaults to `./.sessions`. */ persistenceRoot?: string + /** Skill registry, local-provider, and model-facing consumer config forwarded to agent-core. */ + skills?: agentCore.SkillConfig } ``` -Depends on: [`ToolsConfig`](#deepseek-aidsh-tools) +Depends on: [`agentCore`](../packages/core/agent-core/src/index.ts) · [`ToolsConfig`](#deepseek-aidsh-tools) Source: [`packages/ui/acp-agent/src/index.ts:52`](../packages/ui/acp-agent/src/index.ts) @@ -71,12 +73,12 @@ Source: [`packages/ui/acp-agent/src/index.ts:52`](../packages/ui/acp-agent/src/i * `agents` to the agent loop (an app that pre-creates no agents, like the ACP * bridge, simply omits it), `persona` and `toolOrder` to the system-prompt * plugin (the deployment's persona section and the explicit model-facing tool - * order), the `tools` object to the tool registry (its presentation `mode`). - * Every field is optional INPUT here because each owner's schema - * supplies the default (`[]` / `''` / absent — lexicographic / `native`); the - * schema is the INTERSECTION of the owners' own schemas (the registry's - * nested under its `tools` key), so validation and defaulting can never - * drift from them. + * order), the `tools` object to the tool registry (its presentation `mode`), + * and `skills` to the skill registry/local provider/tool consumer. Every field + * is optional INPUT here because each owner's schema supplies the default; + * the schema is the INTERSECTION of the owners' own schemas (with registry + * schemas nested under their bundle keys), so validation and defaulting can + * never drift from them. */ export interface Config { /** The agent-loop `agents` list (see dsh-agent-loop's `Config`). */ @@ -87,40 +89,39 @@ export interface Config { toolOrder?: SystemPromptConfig['toolOrder'] /** The tool registry's config — its presentation `mode` (see dsh-tools' `Config`). */ tools?: ToolsConfig + /** Skill registry, local provider, and model-facing consumer config. */ + skills?: SkillConfig +} + +/** Skill bundle config forwarded to the registry, local provider, and model-facing consumer. */ +export interface SkillConfig { + /** Registry-level discovery cache settings. */ + registry?: SkillRegistryConfig + /** Local filesystem skill provider settings. */ + local?: SkillLocal.Config + /** Model-facing skill catalog and tool settings. */ + tool?: toolSkill.Config } ``` -Depends on: [`AgentLoopConfig`](#deepseek-aidsh-agent-loop) · [`SystemPromptConfig`](#deepseek-aidsh-system-prompt) · [`ToolsConfig`](#deepseek-aidsh-tools) +Depends on: [`AgentLoopConfig`](#deepseek-aidsh-agent-loop) · [`SkillLocal`](../packages/skill/skill-local/src/index.ts) · [`SkillRegistryConfig`](#deepseek-aidsh-skill) · [`SystemPromptConfig`](#deepseek-aidsh-system-prompt) · [`ToolsConfig`](#deepseek-aidsh-tools) · [`toolSkill`](../packages/skill/tool-skill/src/index.ts) -Source: [`packages/core/agent-core/src/index.ts:71`](../packages/core/agent-core/src/index.ts) +Source: [`packages/core/agent-core/src/index.ts:87`](../packages/core/agent-core/src/index.ts) ## `@deepseek-ai/dsh-agent-loop` Requires: `agents` · `sessions` · `llm` · `tools` · `systemPrompt` ```ts config-catalog -/** - * Plugin config: the agents to create — or resume, via `resumeSessionId` — - * declaratively at startup, so a cordis.yml deployment needs no code. - */ +/** Plugin configuration for declarative startup agents. */ export interface Config { - /** Agents created from configuration at startup. */ + /** Agents created or resumed at plugin startup. */ agents: (AgentOptions & { - /** Agent id to register under; also seeds the fresh per-run session id (`${id}-session-`). */ + /** Registry identity for the live agent. */ id: AgentId - /** - * If set, the config agent RESUMES this persisted session id instead of - * starting a fresh `${id}-session-`. Sourced from an env var in - * cordis.yml (`resumeSessionId: !!js process.env.RESUME_SESSION_ID`), so a - * demo can continue a prior conversation without code changes. Requires a - * `dsh-session-persistence` backend; the resume is deferred until that - * service is available (via `ctx.inject`) and the loaded session's events - * seed the live session so history continues. - * - * The schema accepts a plain string at runtime (cordis.yml values are - * untyped); the brand is compile-time only — the config format is the - * boundary where an id enters, so the TYPE declares the brand here. - */ + /** Optional workspace for a fresh session. */ + cwd?: string + /** Persisted session to resume instead of creating a fresh session. */ resumeSessionId?: SessionId })[] } @@ -128,7 +129,7 @@ export interface Config { Depends on: [`AgentId`](../packages/core/agent/src/index.ts) · [`AgentOptions`](../packages/core/agent/src/index.ts) · [`SessionId`](../packages/core/session/src/index.ts) -Source: [`packages/core/agent-loop/src/index.ts:36`](../packages/core/agent-loop/src/index.ts) +Source: [`packages/core/agent-loop/src/index.ts:325`](../packages/core/agent-loop/src/index.ts) ## `@deepseek-ai/dsh-bash-local` @@ -150,6 +151,33 @@ export interface Config { Source: [`packages/bash/bash-local/src/index.ts:29`](../packages/bash/bash-local/src/index.ts) +## `@deepseek-ai/dsh-bash-sandbox` + +Requires: `sandbox` + +```ts config-catalog +/** + * Plugin config: the local executor's knobs plus the sandbox policy. All + * optional — `static Config` supplies the defaults (`mode: 'read-only'` is the + * fail-safe default; an example that wants a workspace-writable agent opts in + * explicitly). The runner choice is NOT configured here: which platform + * backend confines the command is the `ctx.sandbox` provider's config. + */ +export interface Config extends LocalConfig { + /** File-sandbox mode commands run under (default: `read-only`). */ + mode?: SandboxMode + /** + * Root directory `workspace-write` mode may write under (default: the + * executor's default working directory — `cwd`, else `process.cwd()`). + */ + workspaceRoot?: string +} +``` + +Depends on: [`LocalConfig`](#deepseek-aidsh-bash-local) · [`SandboxMode`](core-data-structures/sandbox.md) + +Source: [`packages/bash/bash-sandbox/src/index.ts:60`](../packages/bash/bash-sandbox/src/index.ts) + ## `@deepseek-ai/dsh-code-runtime-worker` ```ts config-catalog @@ -300,24 +328,6 @@ export interface Config { Source: [`packages/hooks/hooks-codex/src/index.ts:43`](../packages/hooks/hooks-codex/src/index.ts) -## `@deepseek-ai/dsh-invariants` - -Requires: `sessions` - -```ts config-catalog -/** Plugin config. */ -export interface Config { - /** - * Deep-freeze logged session-event data so mutating a logged event throws. - * Default true — this plugin only runs in dev/test, where freezing is the - * point. Set false to assert the event contract without freezing. - */ - freeze?: boolean -} -``` - -Source: [`packages/support/invariants/src/index.ts:45`](../packages/support/invariants/src/index.ts) - ## `@deepseek-ai/dsh-llm-deepseek` Requires: `llm` @@ -430,6 +440,53 @@ export interface Config { Source: [`packages/guard/repeat-tool-guard/src/index.ts:55`](../packages/guard/repeat-tool-guard/src/index.ts) +## `@deepseek-ai/dsh-sandbox-local` + +```ts config-catalog +/** Plugin config. All optional — `static Config` supplies the defaults. */ +export interface Config { + /** + * Override the sandbox runner argv (the bwrap-shaped profile arguments are + * appended). A NON-EMPTY argv is the operator's assertion that this runner + * exists and FULLY enforces the profile (confinement reports + * `enforcement: 'full'`, and — the runner's kernel mechanism being unknown + * — carries both Linux file-denial dialects as its denial signatures) — + * the runner chain and its probes are skipped, + * and a broken runner fails loudly at execution time. The operator also + * supplies {@link runnerFailureSignatures}, which distinguish the runner + * refusing its profile from the wrapped command failing normally. + * Absent (or empty — the schema normalizes an omitted array to `[]`): the + * built-in platform chains — Linux `bwrap` then the Landlock launcher + * (probed in that order), darwin `sandbox-exec` (the sole candidate, + * selected without a probe). Used for custom/alternative runners and + * for deterministic fake runners in keyless test tiers. + */ + runnerCommand?: string[] + /** + * Case-insensitive stderr substrings emitted when a configured + * {@link runnerCommand} refuses its profile before executing the wrapped + * command. Required and non-empty with `runnerCommand`; rejected without + * it. Missing/unexecutable runner errors are added automatically from + * `runnerCommand[0]`, while these signatures cover an executable runner's + * own failure dialect. + */ + runnerFailureSignatures?: string[] + /** + * Per-probe timeout in milliseconds for the chain's functional probes + * (default: 5000; must be a positive finite number — Node treats a 0 + * `spawnSync` timeout as UNBOUNDED, so 0 is rejected at construction). A + * probe that exceeds it reads as an unusable rung, so a + * host slow enough to trip the default — cold NFS mounts, heavily loaded + * CI — would otherwise be misclassified `SANDBOX_UNAVAILABLE` with no + * config escape. Bounds ONE probe, and the chain walk runs each at most once + * per provider lifetime. + */ + probeTimeoutMs?: number +} +``` + +Source: [`packages/sandbox/sandbox-local/src/index.ts:36`](../packages/sandbox/sandbox-local/src/index.ts) + ## `@deepseek-ai/dsh-session-persistence-jsonl` Requires: `sessions` @@ -483,6 +540,36 @@ export type JournalMode = 'wal' | 'delete' | 'truncate' | 'persist' Source: [`packages/session-persistence/session-persistence-sqlite/src/index.ts:50`](../packages/session-persistence/session-persistence-sqlite/src/index.ts) +## `@deepseek-ai/dsh-skill` + +```ts config-catalog +/** Skill registry configuration. */ +export interface Config { + /** Maximum number of completed cwd/provider catalogs kept in memory. */ + readonly collectCacheMaxEntries?: number +} +``` + +Source: [`packages/skill/skill/src/index.ts:113`](../packages/skill/skill/src/index.ts) + +## `@deepseek-ai/dsh-skill-local` + +Requires: `skills` + +```ts config-catalog +/** Local filesystem skill provider configuration. */ +export interface Config { + /** DeepSeek Harness config root. Defaults to `$DSH_HOME` or `~/.dsh`. */ + dshHome?: string + /** Shared agent config root. Defaults to `$DSH_AGENTS_HOME` or `~/.agents`. */ + agentsHome?: string + /** Additional skill roots scanned after project roots and before user roots. */ + customSkillDirs?: string[] +} +``` + +Source: [`packages/skill/skill-local/src/index.ts:39`](../packages/skill/skill-local/src/index.ts) + ## `@deepseek-ai/dsh-stdio-agent` ```ts config-catalog @@ -492,7 +579,9 @@ Source: [`packages/session-persistence/session-persistence-sqlite/src/index.ts:5 * {@link @deepseek-ai/dsh-agent-core}'s forwarded `agents` list); `persona` is * the deployment persona (forwarded to the system-prompt plugin); `toolOrder` * is the explicit model-facing tool order (forwarded to the system-prompt plugin); - * `persistenceRoot` is the JSONL backend's directory; `welcome` is the UI banner. + * fresh sessions use `process.cwd()` as their workspace cwd; resumed sessions + * keep their persisted cwd. `persistenceRoot` is the JSONL backend's directory; + * `welcome` is the UI banner. */ export interface Config { /** Model name for the `main` agent (must have a registered adapter). */ @@ -507,6 +596,8 @@ export interface Config { persistenceRoot?: string /** stdin-chat banner printed once on start. Defaults to `'ready.'`. */ welcome?: string + /** Skill registry, local-provider, and model-facing consumer config forwarded to agent-core. */ + skills?: agentCore.SkillConfig /** * If set, the `main` agent RESUMES this persisted session id instead of * starting fresh. Sourced from an env var in the leaf `cordis.yml` @@ -516,9 +607,9 @@ export interface Config { } ``` -Depends on: [`ToolsConfig`](#deepseek-aidsh-tools) +Depends on: [`agentCore`](../packages/core/agent-core/src/index.ts) · [`ToolsConfig`](#deepseek-aidsh-tools) -Source: [`packages/ui/stdio-agent/src/index.ts:63`](../packages/ui/stdio-agent/src/index.ts) +Source: [`packages/ui/stdio-agent/src/index.ts:65`](../packages/ui/stdio-agent/src/index.ts) ## `@deepseek-ai/dsh-subagent-acp` @@ -578,7 +669,7 @@ Source: [`packages/subagent/subagent-acp/src/index.ts:30`](../packages/subagent/ ## `@deepseek-ai/dsh-subagent-fork` -Requires: `subagents` · `agents` +Requires: `subagents` ```ts config-catalog /** Config: the registry name to register the provider under. */ @@ -606,9 +697,11 @@ export interface Config { /** Which start-time capabilities to advertise (default: all `true`). */ capabilities?: Partial /** - * The context contract to declare ({@link SubagentProvider.inheritsParentContext}); - * default `false` (spawn-like). Set `true` to exercise the fork-shaped tool - * wording in consumer tests. + * The conversation-history descriptor to declare + * ({@link SubagentProvider.inheritsParentContext}); default `false` (fresh + * conversation). Set `true` to exercise seeded/fork wording in consumer + * tests. This flag says nothing about tool, service, scope, or authority + * inheritance. */ inheritsParentContext?: boolean /** @@ -621,11 +714,11 @@ export interface Config { Depends on: [`SubagentCapabilities`](../packages/subagent/subagent/src/index.ts) · [`SubagentStopReason`](../packages/subagent/subagent/src/index.ts) -Source: [`packages/support/subagent-mock/src/index.ts:84`](../packages/support/subagent-mock/src/index.ts) +Source: [`packages/support/subagent-mock/src/index.ts:97`](../packages/support/subagent-mock/src/index.ts) ## `@deepseek-ai/dsh-subagent-spawn` -Requires: `subagents` · `agents` +Requires: `subagents` ```ts config-catalog /** Config: the registry name to register the provider under. */ @@ -635,7 +728,7 @@ export interface Config { } ``` -Source: [`packages/subagent/subagent-spawn/src/index.ts:36`](../packages/subagent/subagent-spawn/src/index.ts) +Source: [`packages/subagent/subagent-spawn/src/index.ts:35`](../packages/subagent/subagent-spawn/src/index.ts) ## `@deepseek-ai/dsh-system-prompt` @@ -646,7 +739,10 @@ export interface Config { * The deployment's persona — the ONE deployment-authored fragment of the * system prompt, rendered as the order-0 `deployment:persona` section * (after the harness identity, before all tool guidance). Every agent in - * the context shares it, subagents included. Template, not free-form text: + * the context shares it by default; a per-agent persona is a SCOPED section + * of the same name registered through that agent's `agent.ctx` (it shadows + * this one for that agent — the subagent seam's `persona` request field does + * exactly that). Template, not free-form text: * every complete `{{…}}` group is interpreted strictly against the * registered prompt variables (the shipped agent loop registers `{{model}}` * and `{{cwd}}`), and there is no escape syntax for literal `{{…}}` prose @@ -681,7 +777,7 @@ export interface Config { } ``` -Source: [`packages/core/system-prompt/src/index.ts:179`](../packages/core/system-prompt/src/index.ts) +Source: [`packages/core/system-prompt/src/index.ts:225`](../packages/core/system-prompt/src/index.ts) ## `@deepseek-ai/dsh-tool-cordis` @@ -721,6 +817,20 @@ export interface Config { Source: [`packages/fs/tool-fs/src/index.ts:48`](../packages/fs/tool-fs/src/index.ts) +## `@deepseek-ai/dsh-tool-skill` + +Requires: `tools` · `skills` + +```ts config-catalog +/** Model-facing skill catalog configuration. */ +export interface Config { + /** Maximum normalized description length rendered in the session catalog; minimum 3. */ + catalogDescriptionMaxLength?: number +} +``` + +Source: [`packages/skill/tool-skill/src/index.ts:19`](../packages/skill/tool-skill/src/index.ts) + ## `@deepseek-ai/dsh-tool-subagent` Requires: `tools` · `subagents` @@ -740,17 +850,47 @@ export interface Config { toolName?: string /** * Default per-child agent options (model) applied to every spawned child. - * Omitted fields fall back to the child loop's own defaults. There is no - * per-child persona: the deployment persona (the system-prompt plugin's - * `persona` config) is a context-wide section every agent shares. + * Omitted fields fall back to the child loop's own defaults. */ agentOptions?: AgentOptions + /** + * Per-child persona applied to every child this tool spawns: a scoped + * `deployment:persona` section shadowing the deployment's persona for the + * child alone. Requires the bound provider's `persona` capability + * (in-process backends support it; a request against one that doesn't is + * rejected at start). Omitted ⇒ the child renders the deployment persona. + */ + persona?: string + /** + * Tool scoping applied to every child this tool spawns (see + * `SubagentStartRequest.toolFilter`): the named global tools vanish from + * the child's prompt AND refuse to execute. Requires the provider's + * `toolFilter` capability. Unknown names fail the spawn loudly. Note the + * child otherwise sees every global tool — including this delegation tool + * itself; `deny`-listing it (or setting `maxDepth`) is how a deployment + * bounds recursion. + */ + toolFilter?: { + /** Global tool names the child keeps; everything else is removed. */ + allow?: string[] + /** Global tool names removed from the child. */ + deny?: string[] + } + /** + * Recursion cap applied to every child this tool spawns (see + * `SubagentStartRequest.maxDepth`): a spawn whose child would sit deeper + * than this in the delegation tree is rejected. Requires the provider's + * `depthLimit` capability. Must be a non-negative safe integer and is + * validated when the plugin loads. Omitted ⇒ unbounded (bound it in + * deployments that expose this tool to children). + */ + maxDepth?: number } ``` Depends on: [`AgentOptions`](../packages/core/agent/src/index.ts) -Source: [`packages/subagent/tool-subagent/src/index.ts:44`](../packages/subagent/tool-subagent/src/index.ts) +Source: [`packages/subagent/tool-subagent/src/index.ts:47`](../packages/subagent/tool-subagent/src/index.ts) ## `@deepseek-ai/dsh-tool-web` @@ -799,9 +939,9 @@ Requires: `systemPrompt` export interface Config { /** * The presentation mode. `'native'` (the default) contributes every - * registered tool as a wire function definition — byte-for-byte today's - * behavior. `'code'` contributes exactly ONE wire tool, `run_code`, plus - * the generated `tools:sdk` prompt section declaring every other tool as a + * visible end capability as a native wire function definition. Under + * `'code'` this registry contributes exactly ONE wire tool, + * `run_code`, plus the generated `tools:sdk` prompt section declaring every other tool as a * TypeScript API the program calls. `'both'` contributes every native * definition AND `run_code` + the SDK section. Non-native modes require a * loaded `ctx.codeRuntime` whose `language` is `'typescript'` — a missing @@ -818,7 +958,38 @@ export interface Config { export type ToolPresentationMode = 'native' | 'code' | 'both' ``` -Source: [`packages/core/tools/src/index.ts:319`](../packages/core/tools/src/index.ts) +Source: [`packages/core/tools/src/index.ts:401`](../packages/core/tools/src/index.ts) + +## `@deepseek-ai/dsh-user-approval` + +```ts config-catalog +/** Plugin config. All optional — `static Config` supplies the defaults. */ +export interface Config { + /** + * The deployment's default {@link ApprovalPolicy} for sessions without an + * `approval/policy` override — `'ask'` delegates to the composed answerers + * (fail-closed with none); `'never'` auto-rejects every ask without + * prompting (the deterministic CI/unattended stance). + */ + readonly policy?: ApprovalPolicy +} + +/** + * A session's approval policy — what happens to an {@link ApprovalService} + * ask BEFORE any interactive answerer sees it: + * + * - `'ask'` (the default) — delegate to the composed answerers; with none + * composed the chain falls through to the fail-closed `'unavailable'` + * (exactly today's behavior). + * - `'never'` — never prompt anyone: every ask resolves `'rejected'` + * deterministically. The strict headless stance (CI, unattended runs) and + * the only policy value stated in the system prompt — unlike `'ask'`, its + * outcome is knowable without asking, so stating it cannot overclaim. + */ +export type ApprovalPolicy = 'ask' | 'never' +``` + +Source: [`packages/ui/user-approval/src/index.ts:270`](../packages/ui/user-approval/src/index.ts) ## `@deepseek-ai/dsh-web` @@ -967,6 +1138,7 @@ These load from a `cordis.yml` entry with no `config:` block; they declare no co - `@deepseek-ai/dsh-agent` ([`packages/core/agent/src/index.ts`](../packages/core/agent/src/index.ts)) - `@deepseek-ai/dsh-fs-policy` ([`packages/fs/fs-policy/src/index.ts`](../packages/fs/fs-policy/src/index.ts)) +- `@deepseek-ai/dsh-invariants` — requires `sessions` ([`packages/support/invariants/src/index.ts`](../packages/support/invariants/src/index.ts)) - `@deepseek-ai/dsh-llm` ([`packages/llm/llm/src/index.ts`](../packages/llm/llm/src/index.ts)) - `@deepseek-ai/dsh-session` ([`packages/core/session/src/index.ts`](../packages/core/session/src/index.ts)) - `@deepseek-ai/dsh-subagent` ([`packages/subagent/subagent/src/index.ts`](../packages/subagent/subagent/src/index.ts)) @@ -984,6 +1156,7 @@ Abstract service classes — a deployment loads a concrete implementation packag - `@deepseek-ai/dsh-code-runtime` — abstract `CodeRuntime` ([`packages/code-runtime/code-runtime/src/index.ts`](../packages/code-runtime/code-runtime/src/index.ts)) - `@deepseek-ai/dsh-compact` — abstract `CompactService` ([`packages/compact/compact/src/index.ts`](../packages/compact/compact/src/index.ts)) - `@deepseek-ai/dsh-fs` — abstract `FileSystem` ([`packages/fs/fs/src/index.ts`](../packages/fs/fs/src/index.ts)) +- `@deepseek-ai/dsh-sandbox` — abstract `SandboxProvider` ([`packages/sandbox/sandbox/src/index.ts`](../packages/sandbox/sandbox/src/index.ts)) - `@deepseek-ai/dsh-session-persistence` — abstract `SessionPersistence` ([`packages/session-persistence/session-persistence/src/index.ts`](../packages/session-persistence/session-persistence/src/index.ts)) - `@deepseek-ai/dsh-workflow` — abstract `WorkflowService` ([`packages/workflow/workflow/src/index.ts`](../packages/workflow/workflow/src/index.ts)) @@ -995,6 +1168,7 @@ Imported as libraries by other packages; a `cordis.yml` cannot load them. - `@deepseek-ai/dsh-app-boot` ([`packages/ui/app-boot/src/index.ts`](../packages/ui/app-boot/src/index.ts)) - `@deepseek-ai/dsh-brand` ([`packages/util/brand/src/index.ts`](../packages/util/brand/src/index.ts)) - `@deepseek-ai/dsh-hook-protocol` ([`packages/hooks/hook-protocol/src/index.ts`](../packages/hooks/hook-protocol/src/index.ts)) +- `@deepseek-ai/dsh-scope` ([`packages/core/scope/src/index.ts`](../packages/core/scope/src/index.ts)) - `@deepseek-ai/dsh-subagent-inprocess` ([`packages/subagent/subagent-inprocess/src/index.ts`](../packages/subagent/subagent-inprocess/src/index.ts)) - `@deepseek-ai/dsh-subagent-subprocess` ([`packages/subagent/subagent-subprocess/src/index.ts`](../packages/subagent/subagent-subprocess/src/index.ts)) - `@deepseek-ai/dsh-timeout` ([`packages/util/timeout/src/index.ts`](../packages/util/timeout/src/index.ts)) diff --git a/docs/cookbook/adding-a-tool.md b/docs/cookbook/adding-a-tool.md index 50c7cbccf8..2912fde6e4 100644 --- a/docs/cookbook/adding-a-tool.md +++ b/docs/cookbook/adding-a-tool.md @@ -22,7 +22,7 @@ export function apply(ctx: Context) { }, async execute(args, exec) { // args is TYPED from the schema: { path: string; limit?: number } - // exec carries { callId, name, arguments, agent?, signal? } + // exec carries immutable identity + token; signal is the operational field return [{ type: 'text', text: await readFile(args.path, 'utf8') }] }, })) @@ -34,7 +34,9 @@ Registration is effect-based: disposing the plugin fiber unregisters the tool (w ## Rules of the execute() contract - **Args are validated for you.** `defineTool` validates the model-generated `arguments` against the `SchemaSpec` before `execute` runs (type, required keys, enum membership, nested objects/arrays — [runtime arg validation](../rfc/implemented/architecture/2026-06-11-runtime-arg-validation.md)), so inside `execute` the args already match `InferArgs`. You still hand-check value constraints the DSL can't express (non-empty strings, positive numbers, cross-field rules); throw a descriptive Error for those. Raw JSON-Schema tools registered directly (MCP) are NOT validated by the harness — they validate their own input. -- **Throwing means isError.** The registry catches anything `execute()` throws and returns `{isError: true}` to the model. Use that for infrastructure failures (bad input, spawn errors, aborts) — but REPORT domain failures in the result text instead (e.g. tool-bash returns `[exit code: 9]` with `isError: false`: the model decides what a failing command means). +- **Registration borrows your readonly definition.** A typed same-process contribution is not a serialization boundary; do not mutate its schema or replace callbacks after registration. `schemas()` materializes only the explicit model-facing projection. To hot-swap a tool, dispose its owning effect and register the replacement; mutable state inside the callback's closure remains ordinary plugin state. +- **Execution identity is protected.** The registry materializes `arguments` as detached lossless JSON in one recursive pass, freezes that value before policy starts, and assigns an opaque `exec.token`; `callId`, `name`, `arguments`, `agent`, `token`, and an optional enclosing-transport `parent` token stay immutable through dispatch. `parent` is identity-only and exposes no live outer execution. Treat `args` as readonly input. An around-dispatch wrapper may add, replace, or remove only `exec.signal` to impose cancellation or a deadline. +- **Throwing or returning non-JSON data means isError.** The registry catches anything `execute()` throws and materializes the complete post-policy result as lossless JSON before final observers run. A throw, malformed result, or non-JSON content/context/meta becomes `{isError: true}` so the live outcome cannot succeed and then fail at the durable log. Use errors for infrastructure failures (bad input, spawn errors, aborts), but report domain failures in the result text instead (for example, tool-bash returns `[exit code: 9]` with `isError: false` because the model decides what a failing command means). - **Honor `exec.signal`.** Cancel in-flight work when it fires. - **Attach durable card data with `meta` (optional).** `execute` may return `{ content, meta }` instead of a bare `ContentBlock[]` — `meta` is a JSON-serializable payload the core treats as opaque, persisted on the `tool/result` event and handed back to your `presentResult` (so a card that needs more than `args`, like `write`/`edit`'s applied-hunk diff, survives a session replay). Keep UI-only data here, never in the model-facing `content`. - **Use `exec.agent` for async notifications.** `agent.inject(content, {source: {kind: 'plugin', plugin: ''}})` appends durable context the NEXT model request sees — it is not a wake-up (an idle agent stays idle). Guard against disposed agents (try/catch). @@ -45,13 +47,13 @@ Follow tool-bash's background pattern: a `run_in_background` flag returns a task > TODO: each tool reimplements this background pattern by hand today. At some point we need a generic long-running-tool layer that handles task ids, incremental polling, kill, and completion notices uniformly. -## Permissions / sandboxing +## Execution policy and observation -Prefer not to build policy into the tool. The seam is the `tools/pre-execute` gate (deny/ask — see the permission-gate example in [extension-cookbook.md](./extension-cookbook.md)) and the `tools/post-execute` inspect/transform seam, or a sandboxing implementation behind the tool's executor seam. +Prefer not to build deployment policy into the tool. Use `tools/pre-execute` for extensible allow/deny/ask policy (the [permission-gate example](./extension-cookbook.md#a-hook-plugin-permission-gate)), `ctx.tools.guard()` for a final monotonic deny that later listeners cannot undo, `tools/execute` to wrap core dispatch with a deadline/retry/metrics scope, `tools/post-execute` to transform or attach model-facing context, and `tools/result` to observe the immutable normalized outcome without changing it. A sandboxing implementation can also sit behind the tool's executor capability seam; the exact contracts are in the [`dsh-tools` README](../../packages/core/tools/README.md#extension-points). ## Code Mode reaches your tool for free -Under the registry's non-native `mode` ([Code Mode](../../packages/core/tools/README.md)), a registered tool is ALSO callable from a `run_code` program as `await tools.(args)` — nothing to add. The generated SDK declares your parameters from the same JSON Schema `defineTool` emits (constructs outside that subset degrade to `unknown`), each program call re-enters `execute()` through both waterfalls, and a failed call rejects the program-side promise with your error text. Two consequences worth designing for: your `description` and parameter `description`s become JSDoc a model reads while WRITING CODE, and non-text result blocks reach programs as placeholders (text is the lingua franca of the bridge). +Under the registry's non-native `mode` ([Code Mode](../../packages/core/tools/README.md)), each visible registered capability is callable from a `run_code` program as `await tools.(args)` — nothing to add. The registry keeps `run_code` itself as reserved, unfilterable presentation infrastructure while restrictions still control which end capabilities appear in the scoped SDK and bindings. The generated SDK declares parameters from the same JSON Schema `defineTool` emits (constructs outside that subset degrade to `unknown`); each program call receives its own immutable execution whose `parent` is the enclosing `run_code` token, then re-enters the complete pre/guard/around/post/result pipeline. A failed call rejects the program-side promise with your error text. Design `description` and parameter `description`s as JSDoc a model reads while writing code, and remember that non-text result blocks reach programs as placeholders (text is the bridge's lingua franca). ## How your tool renders in an editor (ACP presentation) diff --git a/docs/cookbook/extension-cookbook.md b/docs/cookbook/extension-cookbook.md index de21ece7b1..e2677e463a 100644 --- a/docs/cookbook/extension-cookbook.md +++ b/docs/cookbook/extension-cookbook.md @@ -28,6 +28,8 @@ export function apply(ctx: Context) { } ``` +This waterfall is the reorderable policy layer. Use `ctx.tools.guard()` when an invariant needs a monotonic final denial, `tools/execute` when a plugin must wrap the actual dispatch lifetime (timeouts/retries/metrics; only `exec.signal` is replaceable), `tools/post-execute` for explicit result transformation, and `tools/result` for contained observation of the immutable final outcome. The [adding-a-tool guide](./adding-a-tool.md#execution-policy-and-observation) gives the selection rule. + ## A UI plugin A UI plugin renders from the `session/event` feed (the assistant token stream as `assistant/chunk`, plus turn/step boundaries and tool activity), and drives input back in via `agent.send()` / `agent.steer()`. @@ -54,9 +56,9 @@ export function apply(ctx: Context) { ## A client-driver plugin (external protocol bridge) -A *client driver* is a UI plugin whose "user" is another program speaking a wire protocol rather than a human at a terminal. It owns the process's stdio (so it must run with **no stdout logger** — every non-protocol byte corrupts the stream), creates/resumes agents on demand through the `dsh-agent` factory seam, translates harness events (`session/event`, `agent/*`) into outbound protocol messages, and translates inbound requests back into `agent.send()` / `agent.cancel()`. Two harness-specific contracts make it correct: resolve each request exactly once off a settle signal (settle from the durable `turn/end` session event — the boundary is a session event, not an `agent/*` mirror — with `agent/status` as the fallback if a peer listener starved yours), and tear each agent down through its `AgentHandle.dispose()` (which stops the loop, `await`s its exit, and unregisters), not just `cancel()` — disposal must *reach* quiescence, not merely request it. +A *client driver* is a UI plugin whose "user" is another program speaking a wire protocol rather than a human at a terminal. It owns the process's stdio (so it must run with **no stdout logger** — every non-protocol byte corrupts the stream), creates/resumes agents on demand through the `dsh-agent` factory seam, translates harness events (`session/event`, `agent/*`) into outbound protocol messages, and translates inbound requests back into `agent.send()` / `agent.cancel()`. Two harness-specific contracts make it correct: correlate and settle each request exactly once from the durable `turn/end` session event even if rendering fails, and tear each agent down through its `AgentHandle.dispose()` (which stops the loop, `await`s its exit, and unregisters), not just `cancel()` — disposal must *reach* quiescence, not merely request it. -`packages/ui/acp` is the worked example: it bridges the agent to the Agent Client Protocol (JSON-RPC over stdio) so Zed and other ACP editors can drive it. See its README for the full method surface and the deferred-permission-gate note. +`packages/ui/acp` is the worked example: it bridges the agent to the Agent Client Protocol (JSON-RPC over stdio) so Zed and other ACP editors can drive it. See its README for the full method surface and the permission-prompt answerer it registers on the approval seam. ```ts import type { Context } from 'cordis' @@ -87,21 +89,26 @@ Three complete examples load their plugin trees from `cordis.yml`: [`examples/ec Every product feature maps to a listener on a documented extension seam — the microkernel claim made checkable ([microkernel RFC](../rfc/implemented/architecture/2026-06-11-microkernel-event-taxonomy.md)). No row modifies the loop. +`system-prompt/assemble` is an expert cooperative whole-assembly transform: its returned assembly is authoritative, so listener authors own preserving active Code Mode and structured-output protocol contributions. Prefer `ctx.tools.restrict()` for tool filtering that must stay aligned across presentation, lookup, and execution. + | Product feature | Plugin mechanism | |---|---| | Hook system (user + project level) | listeners on `agent/session-start`, `agent/prompt-submit`, `agent/request`, `agent/step-result`, `tools/pre-execute`, `tools/post-execute`, `agent/turn-continuation` — each interception waterfall returns a typed Decision; the `dsh-hooks-claude` / `dsh-hooks-codex` bridges map hook config files onto these seams | | `/goal` | force-continue via `agent/turn-continuation` + `steer()` reminders | | `/loop` | on the `turn/end` session event, `send()` the next iteration; or force-continue | -| Dynamic workflow | orchestrator plugin on `turn/end` (or `step/end`) driving `send`/`steer` + subagents | +| Dynamic workflow | `ctx.workflows` + the worker-thread engine + the `workflow` tool; structured in-process children enforce output with scoped prompt/tool registrations, a monotonic tool guard, final `tools/result` commit (including enclosing `run_code`), and terminal `agent/turn-stop` | | Queued + steering messages | core `Agent.send()` / `Agent.steer()` | | Context compaction (auto + manual) | the `ctx.compact` seam + a backend (`dsh-compact-basic`) on the serial `agent/pre-step` seam; auto = token-pressure check before each step; a manual trigger invokes the same `ctx.compact` routine ([compaction RFC](../rfc/implemented/feature/2026-06-18-compaction-capability-seam.md) — the model-facing `/compact` consumer tool is deferred) | -| System prompt configurability | `ctx.systemPrompt.section()` with ordering | +| System prompt configurability | `ctx.systemPrompt.section()` with ordering and scope-local shadowing | | AGENTS.md (root) | a section provider reading the file | | AGENTS.md (subdir, on-touch) + file-change notices | `agent.inject()` from a watcher / tool-result listener | | Built-in tools | `ctx.tools.register()`; schemas flow into the assembly automatically — the `dsh-tool-*` families (bash, fs, web, subagent, todo) are the shipped examples | -| ToolSearch / progressive disclosure | filter tools at `system-prompt/assemble` (the assembly carries the schemas; the loop logs the result as the request header, so disclosure stays reconstructable) | -| Tool sandbox (landlock / sandbox-exec) | `tools/pre-execute` (deny), or a sandboxing `BashExecutor` on the `dsh-bash` seam | -| Permission system / AskUserQuestion | `tools/pre-execute` (deny/ask); register an ask tool | +| ToolSearch / progressive disclosure | replace a scoped `ctx.tools.restrict()` registration as the visible set changes; the registry keeps presentation, lookup, and execution aligned | +| Tool deadline / retry / metrics | wrap core dispatch with `tools/execute`; a wrapper may replace `exec.signal`, delegate, and inspect the normalized result in one lexical lifetime | +| Final tool-result metrics / audit / capture | observe immutable authoritative outcomes with `tools/result`; use `tools/post-execute` instead only when the plugin must transform the result or attach context | +| Monotonic terminal turn policy | return `{ action: 'stop' }` from serial `agent/turn-stop`, after continuation and steering have already been folded | +| Subprocess sandbox (landlock / sandbox-exec) | use a `ctx.sandbox` backend through `dsh-bash-sandbox`; use `tools/pre-execute` for capability-level denial | +| Permission system / AskUserQuestion | return `ask` from `tools/pre-execute` and answer through `ctx.approval`; register a separate model-facing ask tool for ordinary user questions | | Plan mode | `tools/pre-execute` (deny writes) + a mode prompt section via `ctx.systemPrompt.section()` or `agent.inject()` (model-visible ⟺ logged: `agent/request` shapes call config only) | | Sub-agent delegation | the `ctx.subagents` provider registry (`dsh-subagent-spawn`/`-fork`/`-acp`) + `dsh-tool-subagent` exposing one configured provider to the model | | MCP | one plugin per server: discover tools → `ctx.tools.register()` | diff --git a/docs/cordis-catalog/events.md b/docs/cordis-catalog/events.md index a9dfb7e05c..acbd39b8fa 100644 --- a/docs/cordis-catalog/events.md +++ b/docs/cordis-catalog/events.md @@ -15,89 +15,89 @@ Dispatch modes: **emit** (fire-and-forget), **waterfall** (each listener gets `n ### `agent/created` — emit -An agent was registered in the AgentRegistry and is ready to receive messages. +An agent's fully composed scoped world was published in the AgentRegistry. Its session is already live in the session store. Setup is composition-only by contract; the subsequent `agent/session-start` boundary is the first supported place to inject or queue startup work. A synchronous listener throw vetoes publication and rollback emits the matching disposal edges; returned-promise rejection is observed and logged but cannot retroactively veto this synchronous boundary. A synchronous listener that requests the advanced registry detach does not remove the entry immediately: removal and the paired `agent/disposed` edge wait until the creation dispatch unwinds, so no later creation listener observes a disposal that preceded its own creation callback. ```ts cordis-catalog -'agent/created'(agent: Agent): void +'agent/created'(this: Scoped, agent: Agent): void ``` Types: [Agent](../core-data-structures/core.md) -Source: [`packages/core/agent/src/types.ts:265`](../../packages/core/agent/src/types.ts) +Source: [`packages/core/agent/src/types.ts:316`](../../packages/core/agent/src/types.ts) ### `agent/disposed` — emit -An agent was disposed and removed from the registry; its fiber and any in-flight turn have been torn down. +An agent was removed from the registry. The concrete AgentLoop lifecycle emits this only after its driver and any in-flight turn reach quiescence; a custom agent registered through the public registry owns its own driver contract, which the registry cannot infer. Ordered teardown may still be detaching the session and unwinding scoped registrations when this runs. ```ts cordis-catalog -'agent/disposed'(agent: Agent): void +'agent/disposed'(this: Scoped, agent: Agent): void ``` Types: [Agent](../core-data-structures/core.md) -Source: [`packages/core/agent/src/types.ts:272`](../../packages/core/agent/src/types.ts) +Source: [`packages/core/agent/src/types.ts:331`](../../packages/core/agent/src/types.ts) ### `agent/error` — emit A step or turn errored. The loop reports a failure here (plus the logger) even when the error has no in-turn position for a session `error` event. ```ts cordis-catalog -'agent/error'(agent: Agent, turn: number, step: number, error: Error): void +'agent/error'(this: Scoped, agent: Agent, turn: number, step: number, error: Error): void ``` Types: [Agent](../core-data-structures/core.md) -Source: [`packages/core/agent/src/types.ts:476`](../../packages/core/agent/src/types.ts) +Source: [`packages/core/agent/src/types.ts:605`](../../packages/core/agent/src/types.ts) ### `agent/pre-step` — serial Awaited pre-step surface-mutation checkpoint, fired once per step AFTER `turn/start` (and after the prior step closed) but BEFORE this step's `step/start` — so anything a listener appends lands OUTSIDE the step, between `turn/start`/`step/end` and the upcoming `step/start`. `step` is the number of the step about to start. The loop awaits `ctx.serial('agent/pre-step', …)` after assembling the system prompt, then opens the step and derives the request history ONCE from whatever the surface now holds. This is where compaction belongs: it mutates the session surface in place (shadowing an older range with a summary node) with its log-only `compact/*` records cleanly outside any step, and the single subsequent derive reflects the mutation — so there is no double-derive and no listener can see (or be expected to act on) an assembled `messages` array that does not exist yet. -Serial (awaited in registration order), not a waterfall: a listener mutates the surface as a side effect; there is nothing to transform, but the loop must wait for the mutation to complete before opening the step and deriving. Cordis `serial` bails early if a listener returns a bail value; this event is typed and documented as `void`, so listeners must not return a semantic veto value. `fullSystemPrompt` is the assembled prompt a listener needs to measure pressure (the system prompt counts toward the budget), and `sessionPrefix` is the instance's composed agent/session-prefix product for the same reason — every request carries it in front of the derived history, and it is composed BEFORE this seam fires precisely so a pressure gate counts the prefix the request will actually send (never a stale logged one). `signal` cancels any in-flight work a listener starts (e.g. a summarization model call). +Serial (awaited in registration order), not a waterfall: a listener mutates the surface as a side effect; there is nothing to transform, but the loop must wait for the mutation to complete before opening the step and deriving. Cordis `serial` bails early if a listener returns a bail value; this event is typed and documented as `void`, so listeners must not return a semantic veto value. `fullSystemPrompt` is the assembled prompt a listener needs to measure pressure (the system prompt counts toward the budget), and `sessionPrefix` is the instance's composed agent/session-prefix product for the same reason — every request carries it in front of the derived history, and it is composed BEFORE this seam fires precisely so a pressure gate counts the prefix the request will actually send (never a stale logged one). `signal` cancels any in-flight work a listener starts (e.g. a summarization model call). Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered through `agent.ctx` fires only for that agent's dispatches; a listener on a plain plugin context fires for every agent. The dispatch `this` is the scope carrier (`Scoped`), built by the emitting side via `scopeTarget`/`agentEvents`. ```ts cordis-catalog -'agent/pre-step'(agent: Agent, turn: number, step: number, fullSystemPrompt: string, sessionPrefix: readonly Message[], signal: AbortSignal): Promise | void +'agent/pre-step'(this: Scoped, agent: Agent, turn: number, step: number, fullSystemPrompt: string, sessionPrefix: readonly Message[], signal: AbortSignal): Promise | void ``` Types: [Agent](../core-data-structures/core.md) · [Message](../core-data-structures/core.md) -Source: [`packages/core/agent/src/types.ts:357`](../../packages/core/agent/src/types.ts) +Source: [`packages/core/agent/src/types.ts:438`](../../packages/core/agent/src/types.ts) ### `agent/prompt-submit` — waterfall Waterfall: decide what happens to ONE drained queued message before it becomes a `user/message` — allow (optionally rewriting the prompt bytes or attaching `additionalContext`) or block it. Fires inside the already-open turn, per drained message. Maps onto Claude Code's `UserPromptSubmit` hook. Call `next()` to delegate to the default (allow unchanged), or return a PromptDecision without calling `next()` to short-circuit. ```ts cordis-catalog -'agent/prompt-submit'(agent: Agent, content: ContentBlock[], source: MessageSource, next: () => Promise): Promise +'agent/prompt-submit'(this: Scoped, agent: Agent, content: ContentBlock[], source: MessageSource, next: () => Promise): Promise ``` Types: [Agent](../core-data-structures/core.md) · [ContentBlock](../core-data-structures/core.md) · [MessageSource](../core-data-structures/core.md) -Source: [`packages/core/agent/src/types.ts:370`](../../packages/core/agent/src/types.ts) +Source: [`packages/core/agent/src/types.ts:456`](../../packages/core/agent/src/types.ts) ### `agent/queued` — emit -A message entered the agent's inbox (queued or steering). `source` is the resolved source (defaults applied), not the caller's raw options. +A message entered the agent's inbox (queued or steering). Content and the resolved source are the detached, deeply-frozen values retained by the inbox. `source` has defaults applied and is not the caller's raw options. ```ts cordis-catalog -'agent/queued'(agent: Agent, content: ContentBlock[], info: { source: MessageSource; steering: boolean }): void +'agent/queued'(this: Scoped, agent: Agent, content: ContentBlock[], info: { source: MessageSource; steering: boolean }): void ``` Types: [Agent](../core-data-structures/core.md) · [ContentBlock](../core-data-structures/core.md) · [MessageSource](../core-data-structures/core.md) -Source: [`packages/core/agent/src/types.ts:290`](../../packages/core/agent/src/types.ts) +Source: [`packages/core/agent/src/types.ts:360`](../../packages/core/agent/src/types.ts) ### `agent/request` — waterfall Waterfall: shape the step's call configuration — model switching, sampling overrides — by returning a replacement LlmCallConfig (the frozen seed is the config the loop would otherwise use). Config is ALL a listener shapes here: every request is a pure function of the session log (the reconstructability RFC), so model-visible content flows through the log channels — `inject()`, steering, prompt-submit `additionalContext`, prompt sections via `system-prompt/assemble`, or the header-logged session prefix via agent/session-prefix — never through request mutation, and the loop records whatever config the request actually uses as a `request/header*` event before dispatch. The step's messages are already snapshotted when this fires (the `step/start` boundary): an `inject()` from a listener here lands in the log but joins the NEXT request. For surface mutation that must precede the snapshot (compaction), use agent/pre-step. Call `next()` to delegate, or return an LlmCallConfig without it to short-circuit. ```ts cordis-catalog -'agent/request'(agent: Agent, turn: number, step: number, config: LlmCallConfig, next: () => Promise): Promise +'agent/request'(this: Scoped, agent: Agent, turn: number, step: number, config: LlmCallConfig, next: () => Promise): Promise ``` Types: [Agent](../core-data-structures/core.md) · [LlmCallConfig](../core-data-structures/core.md) -Source: [`packages/core/agent/src/types.ts:394`](../../packages/core/agent/src/types.ts) +Source: [`packages/core/agent/src/types.ts:485`](../../packages/core/agent/src/types.ts) ### `agent/session-prefix` — waterfall @@ -105,63 +105,89 @@ Waterfall: compose the SESSION PREFIX — request-only messages placed in front This is the home for session-stable openers the model must always see but that must NOT become durable history — a skills catalog, an AGENTS.md digest, a workspace baseline: `Session.deriveMessages()` never returns the prefix, and the header events are its only durable record, so the request stays reconstructable from the log. Content that CHANGES mid-session belongs in the append-only history channels instead — `agent.inject()`, a `tools/post-execute` decision's `additionalContext`, prompt-submit `additionalContext` — each a durable `context/message` paid once and prefix-cached thereafter. -The seed is a frozen empty list; a contributing listener returns a NEW array — never an in-place push. The canonical contribution is a PREPEND, `[mine, ...await next()]`: the waterfall unwinds innermost-first (the LAST-registered listener's `next()` resolves first), so prepending yields registration order on the wire, and every plugin using it composes deterministically. The append form `[...await next(), mine]` is legal but places a contribution AFTER every later-registered plugin's — reverse registration order when all contributors append. Call `next()` to delegate, or return a list without it to short-circuit. +The seed is a frozen empty list; a contributing listener returns a NEW array — never an in-place push. The canonical contribution is a PREPEND, `[mine, ...await next()]`: the waterfall unwinds innermost-first (the LAST-registered listener's `next()` resolves first), so prepending yields registration order on the wire, and every plugin using it composes deterministically. The append form `[...await next(), mine]` is legal but places a contribution AFTER every later-registered plugin's — reverse registration order when all contributors append. Call `next()` to delegate, or return a list without it to short-circuit. Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered through `agent.ctx` fires only for that agent's dispatches; a listener on a plain plugin context fires for every agent. The dispatch `this` is the scope carrier (`Scoped`), built by the emitting side via `scopeTarget`/`agentEvents`. ```ts cordis-catalog -'agent/session-prefix'(agent: Agent, prefix: Message[], signal: AbortSignal, next: () => Promise): Promise +'agent/session-prefix'(this: Scoped, agent: Agent, prefix: Message[], signal: AbortSignal, next: () => Promise): Promise ``` Types: [Agent](../core-data-structures/core.md) · [Message](../core-data-structures/core.md) -Source: [`packages/core/agent/src/types.ts:441`](../../packages/core/agent/src/types.ts) +Source: [`packages/core/agent/src/types.ts:537`](../../packages/core/agent/src/types.ts) ### `agent/session-start` — emit -The agent's session lifecycle began, fired once before its first turn. `source` says why (SessionStartSource: fresh startup, a resumed persisted session, …). A pure NOTIFICATION (emit, not waterfall): it carries no veto — a session-start listener that wants to seed context does so via `agent.inject()` (a `context/message` the first request sees), not by returning a decision. Cannot block the session from starting; that gap is deliberate (a bridge logs/injects, it does not gate startup). +The agent's session lifecycle began, fired once before its first turn. `source` says why (SessionStartSource: fresh startup, a resumed persisted session, …). A pure NOTIFICATION (emit, not waterfall): a listener cannot veto by returning a decision or throwing. A listener that wants to seed context does so via `agent.inject()` (a `context/message` the first request sees). A lifecycle owner can still dispose its structural ownership edge during this notification; publication rechecks liveness and then aborts before the driver starts. ```ts cordis-catalog -'agent/session-start'(agent: Agent, source: SessionStartSource): void +'agent/session-start'(this: Scoped, agent: Agent, source: SessionStartSource): void ``` -Types: [Agent](../core-data-structures/core.md) +Types: [Agent](../core-data-structures/core.md) · [SessionStartSource](../core-data-structures/core.md) -Source: [`packages/core/agent/src/types.ts:305`](../../packages/core/agent/src/types.ts) +Source: [`packages/core/agent/src/types.ts:381`](../../packages/core/agent/src/types.ts) ### `agent/status` — emit Agent status changed (`idle` ⇄ `running`, or → `disposed`). Drive lifecycle off this transition, never off a status you just requested — `send()` does not flip status to `running` before it returns. ```ts cordis-catalog -'agent/status'(agent: Agent, status: AgentStatus): void +'agent/status'(this: Scoped, agent: Agent, status: AgentStatus): void ``` Types: [Agent](../core-data-structures/core.md) -Source: [`packages/core/agent/src/types.ts:281`](../../packages/core/agent/src/types.ts) +Source: [`packages/core/agent/src/types.ts:345`](../../packages/core/agent/src/types.ts) ### `agent/step-result` — waterfall Waterfall: post-process the assembled assistant Message before tool dispatch (validation, content rewriting, …). ```ts cordis-catalog -'agent/step-result'(agent: Agent, turn: number, step: number, message: Message, next: () => Promise): Promise +'agent/step-result'(this: Scoped, agent: Agent, turn: number, step: number, message: Message, next: () => Promise): Promise ``` Types: [Agent](../core-data-structures/core.md) · [Message](../core-data-structures/core.md) -Source: [`packages/core/agent/src/types.ts:451`](../../packages/core/agent/src/types.ts) +Source: [`packages/core/agent/src/types.ts:552`](../../packages/core/agent/src/types.ts) ### `agent/turn-continuation` — waterfall Waterfall: override the turn-continuation decision via a typed ContinuationDecision. The loop's `defaultDecision` is `continue` when the step had tool calls or steering was injected, else `stop`. Listeners force-continue (`/goal`, `/loop` — optionally attaching a `reason` recorded as next-step steering) or force-stop (budget guards). Call `next()` to delegate to the default, or return a decision to override. ```ts cordis-catalog -'agent/turn-continuation'(agent: Agent, turn: number, defaultDecision: ContinuationDecision, next: () => Promise): Promise +'agent/turn-continuation'(this: Scoped, agent: Agent, turn: number, defaultDecision: ContinuationDecision, next: () => Promise): Promise ``` Types: [Agent](../core-data-structures/core.md) -Source: [`packages/core/agent/src/types.ts:464`](../../packages/core/agent/src/types.ts) +Source: [`packages/core/agent/src/types.ts:570`](../../packages/core/agent/src/types.ts) + +### `agent/turn-stop` — serial + +Serial terminal-stop checkpoint after the ordinary `agent/turn-continuation` waterfall, any `continue.reason`, and the pending-steering continuation override have been folded. A listener returns `{ action: 'stop' }` to make this turn terminal, or `undefined` to abstain. Terminal stop is monotonic: listener order and steering cannot resume the turn, and pending steering is discarded rather than becoming another step or turn. + +```ts cordis-catalog +'agent/turn-stop'(this: Scoped, agent: Agent, turn: number): ContinuationStop | undefined +``` + +Types: [Agent](../core-data-structures/core.md) + +Source: [`packages/core/agent/src/types.ts:588`](../../packages/core/agent/src/types.ts) + +## `approval/*` + +### `approval/request` — waterfall + +Waterfall asking the composed answerers to decide one approval request. Dispatched only from ApprovalService.request — callers go through the service (which owns cancellation and the audit events), never through `ctx.waterfall` directly. A listener that can answer for this request's agent returns an outcome WITHOUT calling `next()` (the decision slot is single-occupancy, first listener to answer wins); a listener that does not recognize the agent MUST call `next()` so another answerer — or the fail-closed default `'unavailable'` — gets the question. Throwing is contained by the service and yields `'unavailable'`. Scope-filtered dispatch (`@deepseek-ai/dsh-scope`) keys the carrier by `req.agent`: a listener registered through `agent.ctx` receives only that agent's questions, while a plain-context listener receives every agent's. `req` is a readonly same-process value borrowed from the caller. + +```ts cordis-catalog +'approval/request'(this: Scoped, req: ApprovalRequest, next: () => Promise): Promise +``` + +Types: [ApprovalOutcome](../core-data-structures/approval.md) · [ApprovalRequest](../core-data-structures/approval.md) + +Source: [`packages/ui/user-approval/src/index.ts:70`](../../packages/ui/user-approval/src/index.ts) ## `fs/*` @@ -219,77 +245,109 @@ Source: [`packages/llm/llm/src/index.ts:39`](../../packages/llm/llm/src/index.ts ### `session/created` — emit -A session was created in the store. +A session was created in the store. A synchronous listener throw vetoes publication and rollback emits the matching `session/disposed` edge; returned-promise rejection is observed and logged but cannot retroactively veto this synchronous boundary. A synchronous listener that requests the advanced detach does not remove the entry immediately: removal and the paired `session/disposed` edge wait until the creation dispatch unwinds. Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): the carrier is the session's owner scope, captured when the session was ENTERED (an agent's session is entered through `agent.ctx`, so its events dispatch in that agent's scope; a bare `sessions.create()` from a plain plugin dispatches subject-less). A listener registered through `agent.ctx` hears only that agent's sessions; a plain plugin listener hears every session. ```ts cordis-catalog -'session/created'(session: Session): void +'session/created'(this: Scoped, session: Session): void ``` -Source: [`packages/core/session/src/index.ts:39`](../../packages/core/session/src/index.ts) +Source: [`packages/core/session/src/index.ts:52`](../../packages/core/session/src/index.ts) + +### `session/disposed` — emit + +A previously announced session left the store. Emitted exactly once on normal detach or publication rollback, and never for a prepared/entered session whose `session/created` announcement did not begin. Listener failures (including returned-promise rejections) are logged and contained per listener so teardown always reaches quiescence. Scope-filtered dispatch uses the same owner carrier captured at entry; agent-scoped listeners hear only their own session's teardown. + +```ts cordis-catalog +'session/disposed'(this: Scoped, session: Session): void +``` + +Source: [`packages/core/session/src/index.ts:64`](../../packages/core/session/src/index.ts) ### `session/event` — emit -An event was appended to a session log (sync, fire-and-forget). This is the per-append feed a UI or invariant plugin tails. +An event was appended to a session log (sync, fire-and-forget). This is the per-append feed a UI or invariant plugin tails. The log push is the commit point; synchronous throws and returned-promise rejections from observers are logged and contained per listener, so they cannot make a committed append appear to fail or starve later listeners. The exact callback list and Cordis internal-dispatch checks resolve before the push; callbacks themselves run only after it. Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): the carrier is the session's owner scope, captured when the session was ENTERED (an agent's session is entered through `agent.ctx`, so its events dispatch in that agent's scope; a bare `sessions.create()` from a plain plugin dispatches subject-less). A listener registered through `agent.ctx` hears only that agent's sessions; a plain plugin listener hears every session. ```ts cordis-catalog -'session/event'(session: Session, event: SessionEvent): void +'session/event'(this: Scoped, session: Session, event: SessionEvent): void ``` Types: [SessionEvent](../core-data-structures/core.md) -Source: [`packages/core/session/src/index.ts:47`](../../packages/core/session/src/index.ts) +Source: [`packages/core/session/src/index.ts:83`](../../packages/core/session/src/index.ts) ### `session/flush` — parallel -Awaited durability checkpoint. The agent loop awaits `ctx.parallel('session/flush', session)` at every turn end; persistence plugins (JSONL, SQLite) drain their write-behind buffers here and on fiber dispose. Awaited (parallel), not a waterfall: every listener runs and the loop waits for all of them, but none can veto. +Awaited durability checkpoint. The agent loop awaits `ctx.sessions.flush(session)` at every turn end; persistence plugins (JSONL, SQLite) drain their write-behind buffers here and on fiber dispose. Awaited (parallel), not a waterfall: every listener runs and the caller waits for all of them, but none can veto. Dispatch it through SessionStore.flush — the store owns the carrier — never via a raw `ctx.parallel`. Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): the carrier is the session's owner scope, captured when the session was ENTERED (an agent's session is entered through `agent.ctx`, so its events dispatch in that agent's scope; a bare `sessions.create()` from a plain plugin dispatches subject-less). A listener registered through `agent.ctx` hears only that agent's sessions; a plain plugin listener hears every session. ```ts cordis-catalog -'session/flush'(session: Session): Promise | void +'session/flush'(this: Scoped, session: Session): Promise | void ``` -Source: [`packages/core/session/src/index.ts:57`](../../packages/core/session/src/index.ts) +Source: [`packages/core/session/src/index.ts:101`](../../packages/core/session/src/index.ts) + +## `skill/*` + +### `skill/provider-added` — emit + +A skill provider became resolvable in the `ctx.skills` registry. Consumers can observe this instead of depending on Cordis plugin load order, which is concurrent for sibling plugins. + +```ts cordis-catalog +'skill/provider-added'(provider: SkillProvider): void +``` + +Source: [`packages/skill/skill/src/index.ts:131`](../../packages/skill/skill/src/index.ts) + +### `skill/provider-removed` — emit + +A skill provider left the registry because its plugin fiber was disposed. + +```ts cordis-catalog +'skill/provider-removed'(name: string): void +``` + +Source: [`packages/skill/skill/src/index.ts:137`](../../packages/skill/skill/src/index.ts) ## `subagent/*` ### `subagent/end` — emit -A subagent run settled — emitted when SubagentRun.result resolves (any stop reason). Paired with Events['subagent/start']. +A ready child settled. Scope-filtered dispatch uses the same delegating parent carrier as `subagent/start`, so the lifecycle pair reaches the same scoped audience. ```ts cordis-catalog -'subagent/end'(info: SubagentRunEndInfo): void +'subagent/end'(this: Scoped, info: SubagentRunEndInfo): void ``` -Source: [`packages/subagent/subagent/src/index.ts:98`](../../packages/subagent/subagent/src/index.ts) +Source: [`packages/subagent/subagent/src/index.ts:90`](../../packages/subagent/subagent/src/index.ts) ### `subagent/provider-added` — emit -A provider became resolvable in the SubagentService registry. Consumers that derive state from a named provider (e.g. the model-facing tool wording in `dsh-tool-subagent`) react HERE instead of assuming load order — the cordis Loader starts sibling plugins concurrently, so "listed earlier in cordis.yml" does not mean "registered earlier". +A provider became resolvable in the registry. ```ts cordis-catalog 'subagent/provider-added'(provider: SubagentProvider): void ``` -Source: [`packages/subagent/subagent/src/index.ts:72`](../../packages/subagent/subagent/src/index.ts) +Source: [`packages/subagent/subagent/src/index.ts:66`](../../packages/subagent/subagent/src/index.ts) ### `subagent/provider-removed` — emit -A provider left the registry (its plugin's fiber was disposed — an unload or an HMR reload). Consumers holding provider-derived state drop it here; a reload re-fires `subagent/provider-added` with the fresh provider. Delivered with per-listener containment: a throwing subscriber is logged, never starves later subscribers, and never disrupts the provider's teardown. +A provider left the registry. Accepted runs remain holder-owned. ```ts cordis-catalog 'subagent/provider-removed'(name: string): void ``` -Source: [`packages/subagent/subagent/src/index.ts:83`](../../packages/subagent/subagent/src/index.ts) +Source: [`packages/subagent/subagent/src/index.ts:72`](../../packages/subagent/subagent/src/index.ts) ### `subagent/start` — emit -A subagent run started — emitted after the provider is resolved and its capabilities validated, as the child run begins. Paired with Events['subagent/end']. +A provider established a ready child. For in-process providers, `ctx.agents.get(info.id)` resolves during this notification. Scope-filtered dispatch keys the carrier by the delegating parent, so a parent-scoped listener observes only its own delegations. Paired with `subagent/end`. ```ts cordis-catalog -'subagent/start'(info: SubagentRunInfo): void +'subagent/start'(this: Scoped, info: SubagentRunInfo): void ``` -Source: [`packages/subagent/subagent/src/index.ts:91`](../../packages/subagent/subagent/src/index.ts) +Source: [`packages/subagent/subagent/src/index.ts:82`](../../packages/subagent/subagent/src/index.ts) ## `system-prompt/*` @@ -297,69 +355,85 @@ Source: [`packages/subagent/subagent/src/index.ts:91`](../../packages/subagent/s Waterfall around prompt assembly — mutate or extend the PromptAssembly (sections + tools + variables) before it is rendered. Bound to the SystemPrompt service; call `next()` to delegate. +Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): the carrier is keyed by `context.scope` — a listener registered through `agent.ctx` fires only for that agent's assemblies; a plain plugin listener fires for every assembly (scope-less ones included, dispatched subject-less). + +The returned assembly is authoritative. This is an expert composition seam: a listener that removes or replaces another plugin's protocol contribution owns preserving that protocol's invariants. + ```ts cordis-catalog -'system-prompt/assemble'(this: SystemPrompt, assembly: PromptAssembly, context: AssembleContext, next: () => Promise): Promise +'system-prompt/assemble'(this: Scoped, assembly: PromptAssembly, context: AssembleContext, next: () => Promise): Promise ``` -Source: [`packages/core/system-prompt/src/index.ts:38`](../../packages/core/system-prompt/src/index.ts) +Source: [`packages/core/system-prompt/src/index.ts:49`](../../packages/core/system-prompt/src/index.ts) ### `system-prompt/change` — emit -A section, tool provider, or variable provider was registered or unregistered (the assembly inputs changed). +A section, tool provider, or variable provider was registered or unregistered (the assembly inputs changed — possibly for one scope only). An UNFILTERED registry-subject notification, deliberately not scope-filtered dispatch: a global change concerns every agent's next assembly, so a scoped listener subscribing here sees every change, not just its own scope's. ```ts cordis-catalog 'system-prompt/change'(): void ``` -Source: [`packages/core/system-prompt/src/index.ts:44`](../../packages/core/system-prompt/src/index.ts) +Source: [`packages/core/system-prompt/src/index.ts:59`](../../packages/core/system-prompt/src/index.ts) ## `tools/*` ### `tools/change` — emit -A tool was registered or unregistered (the available tool set changed). +A tool was registered or unregistered, or a scoped restriction changed (the available tool set changed — possibly for one scope only). An UNFILTERED registry-subject notification, deliberately not scope-filtered dispatch: a global change concerns every agent's next assembly, so a scoped listener subscribing here sees every change, not just its own scope's. ```ts cordis-catalog 'tools/change'(): void ``` -Source: [`packages/core/tools/src/index.ts:132`](../../packages/core/tools/src/index.ts) +Source: [`packages/core/tools/src/index.ts:173`](../../packages/core/tools/src/index.ts) ### `tools/execute` — waterfall -Around-dispatch waterfall wrapping the registry's core tool dispatch, between the `tools/pre-execute` gate and the `tools/post-execute` seam. A listener receives `(exec, next)`: call `next()` to delegate to dispatch (returning its ToolExecutionResult, optionally wrapped), or return a replacement result without calling `next()` to short-circuit dispatch. The base `next()` IS the dispatch-with-normalization thunk — a thrown tool (or unknown tool) is already normalized to an `isError` result by the time a listener's `await next()` returns, so a wrapper never sees a raw throw from the tool body. This is the seam a timeout/retry/metrics plugin wraps: it can mutate `exec` (e.g. replace `exec.signal` with a per-call deadline) BEFORE `next()` and inspect the result AFTER. (Cordis `next()` ignores any passed arguments and re-invokes downstream with the shared payload, so a wrapper mutates `exec` in place rather than passing a new object to `next()`.) Multiple listeners compose by registration order — an outer one wraps the inner ones plus dispatch. +Around-dispatch waterfall wrapping the registry's core tool dispatch, between the `tools/pre-execute` gate and the `tools/post-execute` seam. A listener receives `(exec, next)`: call `next()` to delegate to dispatch (returning its ToolExecutionResult, optionally wrapped), or return a replacement result without calling `next()` to short-circuit dispatch. The base `next()` IS the dispatch-with-normalization thunk — a thrown tool (or unknown tool) is already normalized to an `isError` result by the time a listener's `await next()` returns, so a wrapper never sees a raw throw from the tool body. This is the seam a timeout/retry/metrics plugin wraps: it can set or replace the one mutable field, `exec.signal` (e.g. with a per-call deadline), BEFORE `next()`, restore/delete it afterward, and inspect the result AFTER. Call identity (`token`, `callId`, `name`, `arguments`, `agent`, and `parent`) is immutable throughout the pipeline so a wrapper cannot change which tool and scope the pipeline accepted. (Cordis `next()` ignores passed arguments and re-invokes downstream with the shared payload, so a wrapper changes `exec.signal` in place rather than passing a new object to `next()`.) Multiple listeners compose by registration order — an outer one wraps the inner ones plus dispatch. Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): the carrier is keyed by `exec.agent` — a listener registered through `agent.ctx` wraps only that agent's calls; a plain plugin listener wraps every call (including agent-less ones, which dispatch subject-less). ```ts cordis-catalog -'tools/execute'(this: ToolRegistry, exec: ToolExecution, next: () => Promise): Promise +'tools/execute'(this: Scoped, exec: ToolExecution, next: () => Promise): Promise ``` Types: [ToolExecution](../core-data-structures/tools.md) · [ToolExecutionResult](../core-data-structures/tools.md) -Source: [`packages/core/tools/src/index.ts:111`](../../packages/core/tools/src/index.ts) +Source: [`packages/core/tools/src/index.ts:128`](../../packages/core/tools/src/index.ts) ### `tools/post-execute` — waterfall -Waterfall AFTER a tool runs — where hook plugins inspect the result and accept it (optionally REPLACING the model-facing content, and/or attaching `additionalContext` for the next request) or block it with corrective `feedback` (Claude Code's `PostToolUse`). Listeners receive `(exec, result, next)`: call `next()` to delegate to the default (accept unchanged), or return a PostToolDecision to override. Core tool dispatch runs earlier as the base `next()` of the `tools/execute` waterfall, all inside `execute`'s outer try/catch (and the tool body keeps its own inner try/catch, so a thrown tool still reaches `post-execute` as an `isError` result). +Waterfall AFTER a tool runs — where hook plugins inspect the result and accept it (optionally REPLACING the model-facing content, and/or attaching `additionalContext` for the next request) or block it with corrective `feedback` (Claude Code's `PostToolUse`). Listeners receive `(exec, result, next)`: call `next()` to delegate to the default (accept unchanged), or return a PostToolDecision to override. Core tool dispatch runs earlier as the base `next()` of the `tools/execute` waterfall, all inside `execute`'s outer try/catch (and the tool body keeps its own inner try/catch, so a thrown tool still reaches `post-execute` as an `isError` result). Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): the carrier is keyed by `exec.agent` — a listener registered through `agent.ctx` fires only for that agent's calls; a plain plugin listener fires for every call (including agent-less ones, which dispatch subject-less). ```ts cordis-catalog -'tools/post-execute'(this: ToolRegistry, exec: ToolExecution, result: ToolExecutionResult, next: () => Promise): Promise +'tools/post-execute'(this: Scoped, exec: ToolExecution, result: Readonly, next: () => Promise): Promise ``` Types: [ToolExecution](../core-data-structures/tools.md) · [ToolExecutionResult](../core-data-structures/tools.md) -Source: [`packages/core/tools/src/index.ts:127`](../../packages/core/tools/src/index.ts) +Source: [`packages/core/tools/src/index.ts:148`](../../packages/core/tools/src/index.ts) ### `tools/pre-execute` — waterfall -Waterfall BEFORE a tool runs — the gate where sandbox, permission, and hook plugins allow or deny a call (Claude Code's `PreToolUse`). Listeners receive `(exec, next)`: call `next()` to delegate to the default (allow), or return a PreToolDecision without calling `next()` to short-circuit. A `deny` skips dispatch and yields an `isError` result; the tool body never runs. Input rewrite is deliberately NOT offered here (see PreToolDecision); `ask` degrades to deny until the permission system lands (`FIXME(permissions)`). +Waterfall BEFORE a tool runs — the gate where sandbox, permission, and hook plugins allow or deny a call (Claude Code's `PreToolUse`). Listeners receive `(exec, next)`: call `next()` to delegate to the default (allow), or return a PreToolDecision without calling `next()` to short-circuit. A `deny` skips dispatch and yields an `isError` result; the tool body never runs. Input rewrite is deliberately NOT offered here (see PreToolDecision); `ask` is serviced by the `ctx.approval` seam when one is mounted, and degrades to deny otherwise. Scope-filtered dispatch (`@deepseek-ai/dsh-scope`) keys the carrier by `exec.agent`: a listener registered through `agent.ctx` fires only for that agent's calls, while a plain plugin listener fires for every call (including agent-less ones, which dispatch subject-less). ```ts cordis-catalog -'tools/pre-execute'(this: ToolRegistry, exec: ToolExecution, next: () => Promise): Promise +'tools/pre-execute'(this: Scoped, exec: ToolExecution, next: () => Promise): Promise ``` Types: [ToolExecution](../core-data-structures/tools.md) -Source: [`packages/core/tools/src/index.ts:91`](../../packages/core/tools/src/index.ts) +Source: [`packages/core/tools/src/index.ts:101`](../../packages/core/tools/src/index.ts) + +### `tools/result` — emit + +Synchronous notification of the authoritative FINAL tool outcome, after the complete pre/execute/post pipeline, final lossless-JSON validation, and outer error normalization. Unlike the three waterfalls, this seam cannot transform the result: each listener receives the now-frozen execution object and a deep-frozen result snapshot; listener failures are contained and logged, and ToolRegistry.execute still returns the outcome. Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): keyed by `exec.agent`, using the same carrier as the pipeline. + +```ts cordis-catalog +'tools/result'(this: Scoped, exec: Readonly, result: Readonly): undefined +``` + +Types: [ToolExecution](../core-data-structures/tools.md) · [ToolExecutionResult](../core-data-structures/tools.md) + +Source: [`packages/core/tools/src/index.ts:163`](../../packages/core/tools/src/index.ts) ## `workflow/*` @@ -375,7 +449,7 @@ Source: [`packages/workflow/workflow/src/index.ts:96`](../../packages/workflow/w ### `workflow/agent-start` — emit -One `agent()` call started a child run. Paired with Events['workflow/agent-end'] by `agent.seq`. +One `agent()` call established a ready child run. Paired with Events['workflow/agent-end'] by `agent.seq`. A call that never receives a ready run from the provider emits neither event in this pair. ```ts cordis-catalog 'workflow/agent-start'(info: WorkflowRunInfo, agent: WorkflowAgentInfo): void @@ -401,7 +475,7 @@ The script emitted a narration line (a `log(message)` call). 'workflow/log'(info: WorkflowRunInfo, message: string): void ``` -Source: [`packages/workflow/workflow/src/index.ts:77`](../../packages/workflow/workflow/src/index.ts) +Source: [`packages/workflow/workflow/src/index.ts:75`](../../packages/workflow/workflow/src/index.ts) ### `workflow/phase` — emit @@ -411,7 +485,7 @@ The script entered a phase (a `phase(title)` call) — progress grouping for obs 'workflow/phase'(info: WorkflowRunInfo, title: string): void ``` -Source: [`packages/workflow/workflow/src/index.ts:70`](../../packages/workflow/workflow/src/index.ts) +Source: [`packages/workflow/workflow/src/index.ts:68`](../../packages/workflow/workflow/src/index.ts) ### `workflow/start` — emit @@ -421,7 +495,7 @@ A workflow run started — the script's meta block validated, the body about to 'workflow/start'(info: WorkflowRunInfo): void ``` -Source: [`packages/workflow/workflow/src/index.ts:62`](../../packages/workflow/workflow/src/index.ts) +Source: [`packages/workflow/workflow/src/index.ts:60`](../../packages/workflow/workflow/src/index.ts) ## Inherited events (cordis core + loader/hmr/timer) diff --git a/docs/cordis-catalog/services.md b/docs/cordis-catalog/services.md index 79902ba2fc..cf41cfdd5f 100644 --- a/docs/cordis-catalog/services.md +++ b/docs/cordis-catalog/services.md @@ -11,34 +11,48 @@ The **harness tier** below (the `@deepseek-ai/dsh-*` packages) is the vocabulary ## `ctx.agentLoop` — `AgentLoop` -The agent-loop plugin (`ctx.agentLoop`): creates ReactLoopAgents, runs their loops, and registers them in `ctx.agents`. Also implements the AgentFactory seam, so plugins create/resume agents through `ctx.agents` (the interface) without depending on this concrete package. - -The loop itself is deliberately thin — every behavior beyond "call the model, run the tools, repeat" belongs to plugins listening on the event taxonomy declared in @deepseek-ai/dsh-agent. +Concrete ReactLoopAgent factory and driver service. ```ts cordis-catalog -create(id: AgentId, options: AgentOptions = {}): ReactLoopAgent -createAgent(options: CreateAgentOptions): AgentHandle -async resume(options: ResumeAgentOptions): Promise +create(id: AgentId, options: AgentOptions = {}, meta: Pick = {}): ReactLoopAgent +async createAgent(ownerCtx: Context, options: CreateAgentOptions): Promise +async resume(ownerCtx: Context, options: ResumeAgentOptions): Promise ``` -Source: [`packages/core/agent-loop/src/index.ts:68`](../../packages/core/agent-loop/src/index.ts) +Source: [`packages/core/agent-loop/src/index.ts:338`](../../packages/core/agent-loop/src/index.ts) ## `ctx.agents` — `AgentRegistry` -Agent registry (`ctx.agents`): tracks live agents so UI, hook, and orchestrator plugins can find them without depending on the concrete loop package. Agent *creation* is provided by whichever plugin implements the AgentFactory (phase 1: `@deepseek-ai/dsh-agent-loop`), registered via setFactory. +Agent registry (`ctx.agents`): tracks live agents so UI, hook, and orchestrator plugins can find them without depending on the concrete loop package. Agent *creation* is provided by whichever plugin implements the AgentFactory (`@deepseek-ai/dsh-agent-loop`), registered via setFactory. ```ts cordis-catalog setFactory(factory: AgentFactory): () => void -create(options: CreateAgentOptions): AgentHandle +async create(options: CreateAgentOptions): Promise async resume(options: ResumeAgentOptions): Promise register(agent: Agent): () => void +enter(agent: Agent): () => void +announce(agent: Agent): void get(id: AgentId): Agent | undefined list(): Agent[] ``` Types: [Agent](../core-data-structures/core.md) -Source: [`packages/core/agent/src/index.ts:117`](../../packages/core/agent/src/index.ts) +Source: [`packages/core/agent/src/index.ts:203`](../../packages/core/agent/src/index.ts) + +## `ctx.approval` — `ApprovalService` + +The `ctx.approval` service: dispatches ApprovalRequests to the `approval/request` waterfall and audits every ask/outcome pair to the requesting agent's session log. Stateless between requests — grants are returned to the caller, never stored here. + +Owns the policy tier too (`effective = fold(the session's 'approval/policy' events) ?? config.policy`): `request()` resolves `'never'` to `'rejected'` before dispatching any interactive answerer, a per-agent prompt section states a `'never'` policy (and only that one in prose — an `'ask'` promise could overclaim an answerer that headless compositions do not have), and an `agent/pre-step` narrator injects at most one coalesced notice when a session's effective policy moved past what the model was last told. + +```ts cordis-catalog +async request(req: ApprovalRequest): Promise +``` + +Types: [ApprovalOutcome](../core-data-structures/approval.md) · [ApprovalRequest](../core-data-structures/approval.md) + +Source: [`packages/ui/user-approval/src/index.ts:294`](../../packages/ui/user-approval/src/index.ts) ## `ctx.bash` — `BashExecutor` (abstract seam) @@ -65,7 +79,7 @@ onTaskDone(listener: BashTaskListener): () => void Types: [BashExecRequest](../core-data-structures/bash.md) · [BashExecSpec](../core-data-structures/bash.md) · [BashRunResult](../core-data-structures/bash.md) · [BashTask](../core-data-structures/bash.md) · [BashTaskRead](../core-data-structures/bash.md) -Source: [`packages/bash/bash/src/index.ts:59`](../../packages/bash/bash/src/index.ts) +Source: [`packages/bash/bash/src/index.ts:62`](../../packages/bash/bash/src/index.ts) ## `ctx.codeRuntime` — `CodeRuntime` (abstract seam) @@ -147,6 +161,24 @@ Types: [GenerateOptions](../core-data-structures/core.md) · [StreamChunk](../co Source: [`packages/llm/llm/src/index.ts:88`](../../packages/llm/llm/src/index.ts) +## `ctx.sandbox` — `SandboxProvider` (abstract seam) + +Abstract process-sandbox service. Subclass, implement confine, and load the subclass as a plugin — it registers as `ctx.sandbox` (one implementation per context; loading a second throws, cordis' standard duplicate-service behavior). + +Semantics every implementation must honor: + +- confine either returns an argv whose runner ENFORCES the policy or fails closed — at `confine` time with SandboxUnavailableError (no backend for this host), or at EXECUTION time by the runner itself refusing to run the command (exiting without exec'ing it, identified by ConfinedArgv.runnerFailureSignatures). A silent unconfined passthrough is never a legal outcome on either path. +- Probing exists to ARBITRATE between multiple candidate backends and may be skipped when a platform has exactly one: the sole candidate is selected directly and the runner's exec-time fail-closed refusal carries the safety property. When probing does run, it is functional (actually enforcing a profile, not a version check), at most once per provider lifetime; `confine` itself spawns nothing beyond that one-time probing. +- The returned ConfinedArgv.enforcement states the backend's actual completeness for THIS host; `partial` is reported, never silently upgraded to `full`. + +```ts cordis-catalog +abstract confine(argv: readonly string[], policy: SandboxPolicy): ConfinedArgv +``` + +Types: [ConfinedArgv](../core-data-structures/sandbox.md) · [SandboxPolicy](../core-data-structures/sandbox.md) + +Source: [`packages/sandbox/sandbox/src/index.ts:180`](../../packages/sandbox/sandbox/src/index.ts) + ## `ctx.sessionPersistence` — `SessionPersistence` (abstract seam) Abstract durable session-persistence service. Subclass, implement the abstract methods, and load the subclass as a plugin — it registers as `ctx.sessionPersistence` (one implementation per context; loading a second throws, cordis' standard duplicate-service behavior). @@ -155,7 +187,7 @@ Contracts every implementation MUST honor (a DB backend asserts them inside a tr - **Append-only; a crashed turn is closed, not truncated.** Committed events — those at or below a flushed `turn/end` — are never rewritten. A crash can leave an unclosed final turn whose events are real (and possibly large); load preserves them and closes the orphaned turn with synthetic boundary events (see load). Only a never-fully-written torn tail fragment is discarded. - **Contiguous seq.** A persisted log is contiguous: `events[i].seq === i`. load rejects a parse error or a `seq` gap in the COMMITTED region (unloadable); append's first event `seq` MUST equal the backend's stored next-seq (after `load` has balanced any interrupted turn). -- **JSON-serializable data.** `SessionEventMap` is merge-extensible and `event.data` is typed only as `SessionEventMap[K]`, so append REJECTS non-JSON-serializable data with an error naming the offending event type. A backend snapshots (serializes/clones) each event when it buffers, since `session.events` hands out the live mutable object. +- **JSON-serializable events.** `SessionEventMap` is merge-extensible, so append materializes each complete batch through the shared lossless-JSON boundary before buffering it. The public `session.events` view is immutable, but persistence still snapshots direct/replay callers at this independent trust boundary. - **Durability.** append returns only once the batch is durable (the file backend fsyncs; a DB commits). create MAY defer the physical write until the first append (lazy materialization). ```ts cordis-catalog @@ -180,25 +212,39 @@ create(id?: SessionId, options?: CreateSessionOptions): Session prepare(id?: SessionId, options?: CreateSessionOptions): Session enter(session: Session): () => void announce(session: Session): void +async flush(session: Session): Promise get(id: SessionId): Session | undefined list(): Session[] fork(source: SessionForkSource, boundary?: number, childSessionId?: SessionId): Session ``` -Source: [`packages/core/session/src/index.ts:405`](../../packages/core/session/src/index.ts) +Source: [`packages/core/session/src/index.ts:590`](../../packages/core/session/src/index.ts) + +## `ctx.skills` — `SkillService` + +Registry of skill providers. It merges provider catalogs with stable first-wins duplicate handling, exposes sorted model-visible summaries, and loads full skill bodies on demand. + +```ts cordis-catalog +registerProvider(provider: SkillProvider): () => void +register(skill: SkillRegistration): () => void +async list(options: SkillLookupOptions = {}): Promise +async get(name: string, options: SkillLookupOptions = {}): Promise +``` + +Source: [`packages/skill/skill/src/index.ts:158`](../../packages/skill/skill/src/index.ts) ## `ctx.subagents` — `SubagentService` -The `subagents` service: a registry of named SubagentProviders and a capability-checked start surface. +Named provider registry and capability-checked start surface. ```ts cordis-catalog registerProvider(provider: SubagentProvider): () => void getProvider(name: string): SubagentProvider | undefined list(): string[] -start(name: string, request: SubagentStartRequest): SubagentRun +async start(name: string, request: SubagentStartRequest): Promise ``` -Source: [`packages/subagent/subagent/src/index.ts:144`](../../packages/subagent/subagent/src/index.ts) +Source: [`packages/subagent/subagent/src/index.ts:123`](../../packages/subagent/subagent/src/index.ts) ## `ctx.systemPrompt` — `SystemPrompt` @@ -206,27 +252,31 @@ Registry service (`ctx.systemPrompt`): plugins contribute ordered text sections, ```ts cordis-catalog section(section: PromptSection): () => void -tools(provider: () => ToolSchema[]): () => void +tools(provider: (context: AssembleContext) => ToolProviderResult): () => void variable(name: string, provider: (context: AssembleContext) => string | undefined): () => void async assemble(context: AssembleContext = {}): Promise ``` -Source: [`packages/core/system-prompt/src/index.ts:291`](../../packages/core/system-prompt/src/index.ts) +Source: [`packages/core/system-prompt/src/index.ts:340`](../../packages/core/system-prompt/src/index.ts) ## `ctx.tools` — `ToolRegistry` -Tool registry (`ctx.tools`): tool plugins register definitions; the agent loop executes calls through the `tools/pre-execute` → `tools/execute` → `tools/post-execute` pipeline. The registry contributes its schemas into the system-prompt assembly — WHICH schemas is governed by its `mode` config (see Config.mode); under a non-native mode it also registers the `run_code` tool and the `tools:sdk` prompt section itself. +Tool registry (`ctx.tools`): tool plugins register definitions; the agent loop executes calls through the `tools/pre-execute` → guards → `tools/execute` → `tools/post-execute` → `tools/result` pipeline. The registry contributes its schemas into the system-prompt assembly — WHICH schemas is governed by its `mode` config (see Config.mode); under a non-native mode it also owns the reserved `run_code` presentation transport and the `tools:sdk` prompt section. + +Two registration layers (`@deepseek-ai/dsh-scope`): a registration through a plain plugin context is GLOBAL (visible to every agent); one through a scoped context (`agent.ctx`) is filed in that scope's layer — visible to that agent alone, disposed with the scope, and SHADOWING a global tool of the same name for that agent (most-specific-wins; within one layer a duplicate name still throws). restrict masks the global layer per scope. One private visibility resolver feeds the registry's prompt contribution, get, and execute — and, under a non-native mode, the SDK section and `run_code`'s bindings — so those registry-owned presentation and dispatch paths agree. An expert `system-prompt/assemble` listener may deliberately replace the final wire composition and owns any resulting divergence. ```ts cordis-catalog register(definition: ToolDefinition): () => void -get(name: string): ToolDefinition | undefined -schemas(): ToolSchema[] -async execute(exec: ToolExecution): Promise +restrict(filter: ToolRestriction): () => void +guard(guard: ToolGuard): () => void +get(name: string, scope?: ScopeKey): ToolDefinition | undefined +schemas(scope?: ScopeKey): ToolSchema[] +async execute(exec: ToolExecutionInput): Promise ``` -Types: [ToolDefinition](../core-data-structures/tools.md) · [ToolExecution](../core-data-structures/tools.md) · [ToolExecutionResult](../core-data-structures/tools.md) +Types: [ToolDefinition](../core-data-structures/tools.md) · [ToolExecutionInput](../core-data-structures/tools.md) · [ToolExecutionResult](../core-data-structures/tools.md) -Source: [`packages/core/tools/src/index.ts:345`](../../packages/core/tools/src/index.ts) +Source: [`packages/core/tools/src/index.ts:493`](../../packages/core/tools/src/index.ts) ## `ctx.userInteraction` — `UserInteractionService` @@ -268,7 +318,7 @@ Abstract workflow execution service. Subclass, implement start, and load the sub Semantics every implementation must honor: - start throws synchronously for a request that cannot begin (an unparseable script, an invalid meta block). Once it returns a WorkflowRun, `result` NEVER rejects — every failure resolves with `stopReason: 'error'` (or `'cancelled'`) — and once the run is cancelled, `result` SETTLES within the implementation's bounded grace even if the script itself never settles (a consumer awaiting `result` must never be wedged past a cancellation). -- The `workflow/*` events fire through emitWorkflowEvent (data snapshots, per-listener containment); `workflow/end` fires exactly once per started run, after `result` is settled or as it settles. +- The `workflow/*` events fire through emitWorkflowEvent (borrowed immutable data, per-listener containment); `workflow/end` fires exactly once per started run, after `result` is settled or as it settles. - `dispose()` reaches quiescence within a bounded grace: it cancels, waits for the script to settle AND its started children to finish disposing, and abandons whatever is left rather than hanging its caller (the engine documents what abandonment leaves behind). - Runs are HOLDER-OWNED: the engine hands control (`cancel`/`dispose`) to the `start()` caller and does not track its live runs — disposing the engine's own fiber mid-run deliberately leaves those runs to their holders' teardown, so an engine reload cannot yank a run out from under the consumer awaiting it. @@ -276,7 +326,7 @@ Semantics every implementation must honor: abstract start(request: WorkflowStartRequest): WorkflowRun ``` -Source: [`packages/workflow/workflow/src/index.ts:210`](../../packages/workflow/workflow/src/index.ts) +Source: [`packages/workflow/workflow/src/index.ts:211`](../../packages/workflow/workflow/src/index.ts) ## Inherited `ctx` members (cordis core + loader/hmr/timer) diff --git a/docs/cordis-primer.md b/docs/cordis-primer.md index b59363eea7..5b14da901c 100644 --- a/docs/cordis-primer.md +++ b/docs/cordis-primer.md @@ -27,7 +27,7 @@ The mode is part of the event's public contract. New harness events document it `ctx.waterfall` is around-middleware. A listener receives `(...args, next)`. Call `next()` to delegate the possibly wrapped result to the next service; return without `next()` to short-circuit. Values propagate through `next()`'s return value. -Cooperative listeners usually mutate a shared request or decision object and then delegate. A listener can also choose to repalce the result entirely and downstream listeners will only see the result after replacement. Use `prepend: true` only when the listener must run before ordinary registrations. +Cooperative listeners usually mutate a shared request or decision object and then delegate. A listener can also choose to replace the result entirely and downstream listeners will only see the result after replacement. Use `prepend: true` only when the listener must run before ordinary registrations. For single-decision events, short-circuiting is the design. A policy listener can return without `next()` when it owns the decision, while a listener that only annotates or observes must delegate. diff --git a/docs/core-data-structures/approval.md b/docs/core-data-structures/approval.md new file mode 100644 index 0000000000..c5fa1fe13b --- /dev/null +++ b/docs/core-data-structures/approval.md @@ -0,0 +1,64 @@ +# User Approval + +The user-approval seam of [dsh-user-approval](../../packages/ui/user-approval) answers one question: may this specific action proceed? It owns the shared request/outcome vocabulary, the `ctx.approval` dispatch service, the `approval/request` answerer waterfall, the log-only audit pair, and the per-session `ask`/`never` policy. UI channels such as [dsh-acp](../../packages/ui/acp) provide answerers; callers such as [dsh-tools](../../packages/core/tools) and [dsh-tool-bash](../../packages/bash/tool-bash) consume the closed outcome and fail closed unless it is `allowed-once`. + +Source: [`packages/ui/user-approval/src/index.ts`](../../packages/ui/user-approval/src/index.ts) + +## Identity and outcome + +Every request receives a fresh `ApprovalRequestId`. The brand pairs the `approval/asked` and `approval/decided` audit events without making approval ids interchangeable with tool-call, session, or agent ids. + +```ts type-equiv +type ApprovalRequestId = Branded<'ApprovalRequestId'> +``` + +`ApprovalOutcome` is closed and fail-closed. `allowed-once` grants only the asked-about action; callers deny on `rejected`, `cancelled`, and `unavailable`. A missing, non-owning, throwing, or non-conforming answerer becomes `unavailable` rather than opening the gate. + +```ts type-equiv +type ApprovalOutcome = 'allowed-once' | 'rejected' | 'cancelled' | 'unavailable' +``` + +## Per-session policy + +`ApprovalPolicy` determines what happens before interactive answerers run. `ask` delegates to the composed answerer chain, whose no-answer default is `unavailable`; `never` deterministically returns `rejected` without dispatching any answerer. The effective value is the last `approval/policy` event in the session log, falling back to the service config. `setApprovalPolicy(session, policy)` is the single write path, so replay reconstructs the override. + +```ts type-equiv +type ApprovalPolicy = 'ask' | 'never' +``` + +The prompt section states the deterministic `never` behavior and records either policy with a source-owned marker. The pre-step narrator reads that marker from the logged request header after restart; it does not infer state from deployment persona prose. An idle ACP switch is held in the bridge until the next `turn/start`, because approval audit and policy events must remain turn-enclosed for durable replay. + +## Approval request + +`ApprovalRequest` identifies the agent and tool action closely enough to route and audit the question. It deliberately omits tool arguments: an answerer attaches the prompt to the already-streamed tool call through `callId` instead of rendering a second copy that could drift. + +```ts type-equiv +interface ApprovalRequest { + /** + * The agent on whose behalf the question is asked. Routes the question (a + * UI answerer only answers for agents it owns) and receives the audit + * events on its session log. + */ + readonly agent: Agent + /** The tool the question is about (presentation and audit). */ + readonly toolName: string + /** + * The exact tool call being decided, when the asker has one — lets a UI + * attach the prompt to the tool call it already streamed. + */ + readonly callId?: CallId + /** The asker's human-readable explanation of WHY it is asking. */ + readonly reason?: string + /** + * Aborting withdraws the question: the request settles `'cancelled'` + * immediately and a late answer from a still-pending answerer is discarded. + */ + readonly signal?: AbortSignal +} +``` + +## Dispatch and audit + +`ctx.approval.request(req)` requires the requesting session to be inside an open turn. It appends `approval/asked`, obtains one outcome, appends the matching `approval/decided`, and resolves with that outcome. The `never` policy is enforced inside the service before waterfall dispatch, so even an answerer registered later with `prepend` cannot bypass it. Answerers return an outcome when they own the request or call `next()` to delegate; the first answer occupies the single decision slot. + +The audit events are log-only and do not enter the model transcript. Model-visible behavior is the caller's derived tool result, while the request header records the prompt policy that the model actually saw. Service disposal removes its prompt section and pre-step narrator together; answerer listeners are independently effect-bound to their owning plugins. diff --git a/docs/core-data-structures/bash.md b/docs/core-data-structures/bash.md index 273ba5ebe8..51f7cb0696 100644 --- a/docs/core-data-structures/bash.md +++ b/docs/core-data-structures/bash.md @@ -44,6 +44,20 @@ interface BashExecRequest { * ownerless background start (a non-agent caller). */ owner?: OwnerToken | undefined + /** + * Explicit per-call sandbox-policy input, overriding the executor's + * configured default mode for THIS call. Never a silent default: a + * consumer sets it only from an explicit policy source — an + * `'allowed-once'` grant a human just issued through `ctx.approval` (the + * escalation flow in the sandbox RFC § Escalation, which outranks), or the + * session's standing override folded from its own `bash/sandbox-mode` + * events (the sandbox RFC § Per-session mode switching — the user's recorded per-session + * choice). A sandboxing executor confines THIS call under the given mode; + * a non-sandboxing executor carries the field and confines nothing (the + * tool layer stamps neither escalation nor overrides without a sandboxing + * executor — see {@link BashExecutor.sandboxMode}). + */ + sandboxMode?: SandboxMode | undefined } ``` @@ -79,6 +93,16 @@ interface BashExecSpec { * task. `start()` stores it; `run()` (foreground) ignores it. */ owner: OwnerToken | undefined + /** + * The sandbox mode this call executes under, REQUIRED-but-nullable for the + * same visibility reason as `owner`. A sandboxing executor's `resolve()` + * stamps the effective mode (the request's explicit override, else its + * configured default) so `run()`/`start()` read the spec, never the config; + * a non-sandboxing executor carries the request value through verbatim and + * ignores it (`undefined` under such an executor means what its README says: + * unconfined execution). + */ + sandboxMode: SandboxMode | undefined } ``` @@ -106,6 +130,12 @@ interface BashRunResult { timeoutMs: number stdout: CollectedOutput stderr: CollectedOutput + /** + * Sandbox facts, present iff a sandboxing executor ran the command — an + * unsandboxed executor (e.g. `dsh-bash-local`) never sets it. See + * {@link BashSandboxInfo} for the `denied` classification semantics. + */ + sandbox?: BashSandboxInfo } ``` @@ -122,9 +152,52 @@ interface CollectedOutput { } ``` +## File sandbox: `BashSandboxInfo` + +A sandbox-consuming executor (`dsh-bash-sandbox`) exposes its configured fallback through `BashExecutor.sandboxMode`. The tool layer folds each agent session's durable `bash/sandbox-mode` override, stamps the effective mode onto the request, states it in the per-agent prompt, and may replace it for one user-approved strictly wider call. The mode/enforcement vocabulary is owned and cataloged by the [`@deepseek-ai/dsh-sandbox` seam](sandbox.md), whose provider wraps the executor's argv; modes govern FILE effects only, not network or process visibility. + +A sandboxed run always reports the facts it executed under on `BashRunResult.sandbox`: `denied` is the executor's conservative classification of a failure as sandbox-caused (a failed exit whose stderr carries a filesystem-permission signature — never a clean exit or a signal kill), read from the collected stderr tail; `enforcement` reports how completely the selected backend governs the mode's file effects (`SandboxEnforcement = 'full' | 'partial'` — `partial` when an older Landlock ABI governs only a subset of the requested accesses; absent under `danger-full-access`, where nothing is confined); `runnerFailed` marks the opposite of a denial — the sandbox RUNNER itself failed and the command never ran (stamped only on settled background tasks; a foreground run surfaces the same condition as the thrown `SANDBOX_UNAVAILABLE` error): + +```ts type-equiv +interface BashSandboxInfo { + /** The mode the command actually ran under. */ + mode: SandboxMode + /** + * True when the executor classifies this run's failure as the sandbox + * denying a file operation. The classification is CONSERVATIVE (a failed + * exit whose stderr carries a filesystem-permission signature) and reads + * the COLLECTED stderr — the bounded in-memory tail per + * {@link CollectedOutput} semantics, so a signature that survives only in a + * spill file is missed toward `denied: false`. A plain command failure + * keeps `denied: false` even under a sandboxed mode. + */ + denied: boolean + /** + * How completely the runner enforced `mode`'s file effects — see + * {@link SandboxEnforcement}. Absent exactly when `mode` is + * `danger-full-access`: nothing is confined, so there is no enforcement to + * report. + */ + enforcement?: SandboxEnforcement + /** + * True when the executor classifies this failure as the SANDBOX RUNNER + * itself failing (missing binary, refused profile, fail-closed refusal + * before exec) — the command NEVER RAN; this is a sandbox failure, not a + * task failure, and it outranks `denied` (a runner's own error text can + * contain denial words). Only ever stamped on settled BACKGROUND tasks: a + * foreground run surfaces the same condition as the thrown + * `SANDBOX_UNAVAILABLE` error instead (the foreground path has an error + * channel; a settled task's facts are its only channel). + */ + runnerFailed?: boolean +} +``` + +One more piece completes the vocabulary: the `SANDBOX_UNAVAILABLE` error code (owned by the [sandbox seam](sandbox.md)) is what the `ctx.sandbox` provider throws — and the executor propagates — when a confined mode has no usable backend. A selected runner refusing its profile reaches the same fail-closed foreground error; a settled background task records `runnerFailed`. The model sees the current effective mode in the prompt, receives denial/runner facts in results, and can request a one-shot strictly wider retry through `sandbox_permissions` plus `justification`; `ctx.approval` must grant that exact call before anything executes. The complete policy and switching design is the [sandbox RFC](../rfc/implemented/feature/2026-07-06-sandbox.md). + ## Background tasks: `BashTask` -A long-running command started with `start()` is tracked as a `BashTask`. `BashTaskStatus` is `'running' | 'completed' | 'killed'`; `done` resolves when the underlying process closes and never rejects. +A long-running command started with `start()` is tracked as a `BashTask`. `BashTaskStatus` is `'running' | 'completed' | 'killed'`; `done` resolves when the underlying process closes and never rejects. A sandboxing executor stamps `sandbox` once the task settles — classification runs against the settled task's collected stderr — so the field is absent while running and under an unsandboxed executor. ```ts type-equiv interface BashTask { @@ -137,6 +210,16 @@ interface BashTask { signal: NodeJS.Signals | null /** Resolves when the underlying process closes (never rejects). */ readonly done: Promise + /** + * Sandbox facts for this task's execution, stamped by a sandboxing executor + * once the task settles and BEFORE completion listeners are notified — an + * `onTaskDone` consumer and a `done` awaiter both see it. Denial + * classification runs against the settled task's collected stderr, so the + * field cannot exist earlier: absent while the task is running and under an + * executor that does not sandbox. See {@link BashSandboxInfo} for the + * `denied` semantics. + */ + sandbox?: BashSandboxInfo } ``` diff --git a/docs/core-data-structures/core.md b/docs/core-data-structures/core.md index 9611adab6f..757c74f399 100644 --- a/docs/core-data-structures/core.md +++ b/docs/core-data-structures/core.md @@ -16,13 +16,18 @@ Everything else is documented on a **sub-page**, not here. The rule that draws t | Sub-page | Owns | |---|---| | [llm-streaming.md](llm-streaming.md) | the `StreamChunk` wire protocol + adapter contract, `BlockAssembler`, the `LlmAdapter` seam | +| [scope.md](scope.md) | scoped registration identity, dispatch carriers, and the owned `Scope` context | | [session.md](session.md) | the full `SessionEventMap` variant catalog, `TurnTrigger`/`TurnEndReason`, `deriveMessages()`, the turn-enclosure invariant | | [persistence.md](persistence.md) | the durability seam: `SessionPersistence`, JSONL + SQLite backends, `session/flush`, crash recovery, `SessionHeader` | -| [tools.md](tools.md) | `ToolDefinition` full fields, the schema DSL, `ToolExecution`/`ToolResult`, tool-presentation UI types, the `tools/pre-execute`/`tools/post-execute` pipeline | +| [system-prompt.md](system-prompt.md) | per-assembly context, tool-provider results, prompt sections, and cooperative assembly | +| [tools.md](tools.md) | `ToolDefinition` full fields, the schema DSL, `ToolExecution`/`ToolResult`, tool-presentation UI types, and the guarded execution pipeline | | [user-interaction.md](user-interaction.md) | the UI-backed human question/answer seam: `AskUserQuestionRequest`, answer/options vocabulary, provider API, error taxonomy | +| [approval.md](approval.md) | the one-shot user-approval seam: `ApprovalRequest`, `ApprovalOutcome`, per-session policy, audit and answerer contracts | | [bash.md](bash.md) | the bash executor seam: `BashExecRequest`/`Spec`, `BashRunResult`, background `BashTask`s | +| [sandbox.md](sandbox.md) | the process-confinement seam: file-effect modes, `SandboxPolicy`, `ConfinedArgv`, enforcement and fail-closed errors | | [code-runtime.md](code-runtime.md) | the code-execution seam: `CodeRunRequest`/`Result`, binding namespaces, captured logs, the `CodeRunFailure` taxonomy | | [filesystem.md](filesystem.md) | the filesystem seam: `FsTarget`, read/write/edit outcomes, observed-file state, `FsErrorCode` | +| [skills.md](skills.md) | the skill service: discovery priority, `SkillSummary`/`SkillDefinition`, session-prefix catalog, model-facing `skill` loading | | [compaction.md](compaction.md) | the compaction seam: the `compact/*` session events, `CompactionResult`, the `CompactService` interface | | [subagent.md](subagent.md) | the subagent seam: the named-provider registry, `SubagentStartRequest`/`Result`/`Run`, the start-time-vs-runtime capability split | | [web.md](web.md) | the web access seam: `WebSearchRequest`/`Result`, `WebFetchRequest`/`Result`, `WebFetchBody`, provider/capability status, `WebError` | @@ -196,7 +201,7 @@ The model-facing `ToolSchema` is the wire shape; the registered `ToolDefinition` ### The request envelope: `LlmCallConfig` and the logged header -Requests are built by the loop, not shaped per call: the non-history half of a request — the `EpochHeader`: this call configuration plus the rendered system prompt, the tool schemas in the assembly's canonical order (dsh-system-prompt's `toolOrder` config, lexicographic when unset), and the session prefix — is logged session state (`request/header` snapshot and delta events, [session.md](session.md#the-request-header-events-requestheader-and-requestheader-delta)), so every conversation request is a pure function of the session log ([reconstructability RFC](../rfc/implemented/architecture/2026-07-05-reconstructable-requests.md)). The `agent/request` waterfall receives a frozen `LlmCallConfig` seed and a listener returns a replacement to switch model or sampling; the `agent/session-prefix` waterfall — fired once per loop instance — composes the request-only messages fronting the derived history (recorded as the header's `messagePrefix`) — the loop logs whatever the request actually uses. Loop-built requests arrive at `llm/stream` deep-frozen; mutation throws. +Requests are built by the loop, not shaped per call: the non-history half of a request — the `EpochHeader`: this call configuration plus the rendered system prompt, the tool schemas in the authoritative returned assembly order (initially canonicalized by dsh-system-prompt's `toolOrder` config, or lexicographically when unset), and the session prefix — is logged session state (`request/header` snapshot and delta events, [session.md](session.md#the-request-header-events-requestheader-and-requestheader-delta)), so every conversation request is a pure function of the session log ([reconstructability RFC](../rfc/implemented/architecture/2026-07-05-reconstructable-requests.md)). The `agent/request` waterfall receives a frozen `LlmCallConfig` seed and a listener returns a replacement to switch model or sampling; the `agent/session-prefix` waterfall — fired once per loop instance — composes the request-only messages fronting the derived history (recorded as the header's `messagePrefix`) — the loop logs whatever the request actually uses. Loop-built requests arrive at `llm/stream` deep-frozen; mutation throws. On the wire, a loop-built request reads in this order: the `system` slot (the rendered prompt assembly) → `messagePrefix` (the frozen session prefix) → the derived history — the boundary snapshot, whose tail is the newest `user/message` on a turn's first step and the previous step's tool results on later steps. The prefix never enters the derived history; its durable record is the header events, and the dev invariant recomputes exactly this equation against every loop-built request. @@ -254,12 +259,29 @@ interface Agent { readonly session: Session readonly status: AgentStatus - /** Queue a user message. Starts a turn when idle; otherwise waits for the next turn. */ + /** + * The agent's scope context (`@deepseek-ai/dsh-scope`, key = this agent): + * registrations through it — tools, prompt sections/variables, listeners, + * restrictions — are visible to this agent only and unwind when it is + * disposed; `agent.ctx.on('agent/…')` listeners fire only for this agent. + */ + readonly ctx: Context + + /** + * Queue a user message. Starts a turn when idle; otherwise waits for the next + * turn. Content and the resolved source are accepted as one detached, + * deeply-frozen lossless-JSON record before notification or enqueue, so + * caller or `agent/queued` listener in-place mutation cannot change later + * log/model input. Throws synchronously when either value is not losslessly + * JSON-serializable; `agent/prompt-submit` may still return an explicit + * replacement. + */ send(content: ContentBlock[], options?: SendOptions): void /** * Steer a running turn: content is injected between steps of the current - * turn. When idle, behaves like {@link send}. + * turn. Uses the same owned-value and synchronous-validation boundary as + * {@link send}; when idle, behaves exactly like that method. */ steer(content: ContentBlock[], options?: SendOptions): void @@ -331,7 +353,7 @@ interface Agent { } ``` -`AgentStatus` is `'idle' | 'running' | 'disposed'`. `AgentId` is a branded string. `AgentOptions` (`model?`) is merge-extensible — plugins add creation options by declaration merging; the persona is NOT an agent option but the `dsh-system-prompt` plugin's `persona` config, shared context-wide. The `agent/*` event taxonomy (lifecycle emits incl. `agent/session-start`, the serial `agent/pre-step` surface-mutation seam, and the `agent/prompt-submit`/`agent/request`/`agent/session-prefix`/`agent/step-result`/`agent/turn-continuation` waterfalls) is in [architecture.md § Event taxonomy](../architecture.md#event-taxonomy); turn/step boundaries are durable `session/event` records, not `agent/*` emits. +`AgentStatus` is `'idle' | 'running' | 'disposed'`. `AgentId` is a branded string. `AgentOptions` (`model?`) is merge-extensible — plugins add creation options by declaration merging. Persona is not an agent option: the `dsh-system-prompt` config supplies the global default, and an agent-scoped `deployment:persona` section may shadow it. The `agent/*` event taxonomy (lifecycle emits incl. `agent/session-start`, serial `agent/pre-step`/`agent/turn-stop` checkpoints, and the `agent/prompt-submit`/`agent/request`/`agent/session-prefix`/`agent/step-result`/`agent/turn-continuation` waterfalls) is in [architecture.md § Event taxonomy](../architecture.md#event-taxonomy); turn/step boundaries are durable `session/event` records, not `agent/*` emits. ## Interception decisions @@ -362,6 +384,12 @@ type ContinuationDecision = | { action: 'continue'; reason?: HookContext } ``` +`agent/turn-stop` returns the stop-only `ContinuationStop` subset or `undefined`. The loop calls this serial checkpoint after folding the ordinary decision, its reason, and pending steering; a stop is terminal and discards pending steering. + +```ts type-equiv +type ContinuationStop = Extract +``` + `agent/session-start` carries a `SessionStartSource` (why the session lifecycle began; a bridge keys its SessionStart matcher on it): ```ts type-equiv diff --git a/docs/core-data-structures/persistence.md b/docs/core-data-structures/persistence.md index 4dbb8fb2af..7bf102924b 100644 --- a/docs/core-data-structures/persistence.md +++ b/docs/core-data-structures/persistence.md @@ -25,15 +25,15 @@ interface SessionHeader { * session is created. A persistence backend rejects any other version on load * (no migration — see the constant). */ - version: number + readonly version: number /** The session's id (mirrors the {@link Session}'s id). */ - id: SessionId + readonly id: SessionId /** Unix epoch milliseconds when the session was created. */ - createdAt: number + readonly createdAt: number /** Absolute working directory the session was created in (if any). */ - cwd?: string + readonly cwd?: string /** The session this one was forked from (seed lineage), if any. */ - parentSession?: SessionId + readonly parentSession?: SessionId /** * How many leading events were INHERITED via a seed rather than produced by * this session — the seed boundary. Set when a fork seeds a child with a @@ -43,7 +43,7 @@ interface SessionHeader { * harness can skip the inherited prefix when deriving the child's OWN script * (the seeded events are the parent's, not this child's model calls). */ - seedLength?: number + readonly seedLength?: number } ``` @@ -54,7 +54,7 @@ Creating a `Session` through the store takes a `seed` (replay/fork an existing e ```ts type-equiv interface CreateSessionOptions { /** Events to seed the new session with (replay/fork). */ - seed?: SessionEvent[] + readonly seed?: readonly SessionEvent[] /** * Creation metadata. The store fills in `version`/`id` and defaults * `createdAt` to now; the caller supplies the storage-level fields (validated @@ -67,7 +67,12 @@ interface CreateSessionOptions { * length, not the original boundary — the caller must pass the persisted * boundary back. A fresh fork passes its actual seeded-prefix length. */ - meta?: { cwd?: string; parentSession?: SessionId; createdAt?: number; seedLength?: number } + readonly meta?: { + readonly cwd?: string + readonly parentSession?: SessionId + readonly createdAt?: number + readonly seedLength?: number + } } ``` diff --git a/docs/core-data-structures/sandbox.md b/docs/core-data-structures/sandbox.md new file mode 100644 index 0000000000..6b7b212373 --- /dev/null +++ b/docs/core-data-structures/sandbox.md @@ -0,0 +1,82 @@ +# Process Sandbox + +The process-sandbox seam of [dsh-sandbox](../../packages/sandbox/sandbox) wraps a same-world subprocess argv in a file-effect policy without coupling consumers to a platform runner. [dsh-sandbox-local](../../packages/sandbox/sandbox-local) supplies the Linux bwrap/Landlock and macOS Seatbelt backends; [dsh-bash-sandbox](../../packages/bash/bash-sandbox) is the first consumer. Containers, microVMs, and remote execution are sibling implementations of whole capability seams, not providers of `ctx.sandbox`. + +Source: [`packages/sandbox/sandbox/src/index.ts`](../../packages/sandbox/sandbox/src/index.ts) + +## Modes and enforcement + +`SandboxMode` governs filesystem effects only. `read-only` denies writes except the required `/dev/null` sink; `workspace-write` permits writes under the workspace root and the backend's promised temp area; `danger-full-access` bypasses confinement. Network and process visibility are outside this vocabulary. + +```ts type-equiv +type SandboxMode = 'read-only' | 'workspace-write' | 'danger-full-access' +``` + +Only the first two modes can be sent to a provider. A `danger-full-access` consumer spawns its original argv and does not call `ctx.sandbox`. + +```ts type-equiv +type ConfinedSandboxMode = Exclude +``` + +Enforcement is a reported fact. `full` means the backend governs every file effect promised by the mode; `partial` means an active backend or older kernel ABI governs only a subset, so consumers that require the absolute promise must reject or surface that distinction. + +```ts type-equiv +type SandboxEnforcement = 'full' | 'partial' +``` + +## Per-call policy + +The policy is fully resolved and carried per call. This permits concurrent consumers and one-shot escalated retries to ask the same provider for different boundaries without mutating provider state. + +```ts type-equiv +interface SandboxPolicy { + /** The file-effect mode this execution runs under. */ + mode: ConfinedSandboxMode + /** Absolute root directory `workspace-write` may write under. */ + workspaceRoot: string +} +``` + +## Wrapped argv and classification dialects + +`ConfinedArgv` is what the consumer spawns. Besides the replacement argv, it carries the backend's enforcement fact and two orthogonal stderr dialects. `denialSignatures` identify the confined command being blocked while the sandbox works correctly. `runnerFailureSignatures` identify the sandbox runner refusing or failing before it executes the command; consumers check these first and surface a sandbox infrastructure failure, never an ordinary task failure. + +```ts type-equiv +interface ConfinedArgv { + /** The wrapped argv (runner, profile, separator, then the caller's argv). */ + argv: string[] + /** How completely the selected backend enforces the policy's file effects. */ + enforcement: SandboxEnforcement + /** + * The selected backend's denial DIALECT: the case-insensitive stderr + * substrings a file effect denied by THIS backend produces (EROFS text + * under bwrap's read-only binds, EACCES under Landlock, EPERM under + * Seatbelt). A consumer that infers denials from a failed run's stderr + * matches against exactly these rather than a cross-backend union — the + * union claims denials a given backend never produces. + */ + denialSignatures: readonly string[] + /** + * How the RUNNER ITSELF failing identifies itself: case-insensitive stderr + * substrings produced when the sandbox binary is missing, refuses its + * profile, or fails closed before exec'ing the command (`bwrap: `, + * `landlock-run: `, `sandbox-exec: ` — each covers both the runner's own + * error prefix and the shell's runner-not-found message). ORTHOGONAL to + * {@link denialSignatures}: a denial is the confined COMMAND being blocked + * (the sandbox working as designed); a runner failure means the command + * NEVER RAN and must surface as a sandbox failure, not a task failure — + * consumers check these signatures FIRST (a runner's own error text may + * contain denial words, e.g. an unopenable grant root reporting + * `Permission denied`). + */ + runnerFailureSignatures: readonly string[] +} +``` + +An operator-configured local runner must supply at least one `runnerFailureSignatures` entry for its own pre-exec refusal dialect; the provider adds outer-shell missing and unexecutable forms automatically. This makes an executable custom runner rejecting its profile distinguishable from the wrapped command exiting with the same status. + +## Provider and fail-closed errors + +`ctx.sandbox.confine(argv, policy)` returns a `ConfinedArgv` or throws `SandboxUnavailableError` with code `SANDBOX_UNAVAILABLE` when no usable backend exists. A selected runner can also fail closed at execution time, in which case its failure signature carries the same infrastructure meaning. Silent unconfined passthrough is never legal for a confined policy. + +Provider probing arbitrates between multiple candidates and is cached for the provider lifetime. A platform with one candidate may select it directly; execution-time refusal retains the safety property. The local provider reports bwrap and Seatbelt as full and preserves the Landlock launcher's full/partial kernel verdict. diff --git a/docs/core-data-structures/scope.md b/docs/core-data-structures/scope.md new file mode 100644 index 0000000000..66d5f40de9 --- /dev/null +++ b/docs/core-data-structures/scope.md @@ -0,0 +1,31 @@ +# Scoped Registration + +The [scope package](../../packages/core/scope) supplies the identity and carrier vocabulary that makes one registration context mean both per-agent visibility and shared lifetime ownership. It is a library primitive rather than a Cordis service; the [agent-scope runtime-design RFC](../rfc/implemented/architecture/2026-07-12-agent-scope-runtime-design.md#scope-routing-one-opaque-key-selects-one-layer) owns the implementation rationale, while the package [README](../../packages/core/scope/README.md) owns the callable API and filtering semantics. + +Source: [`packages/core/scope/src/index.ts`](../../packages/core/scope/src/index.ts). + +## Identity and dispatch carrier + +`ScopeKey` is an opaque object identity. The shipped loop uses the live `Agent` object as its own key, but the primitive never inspects the object. + +```ts type-equiv +type ScopeKey = object +``` + +`Scoped` is the compile-time brand on the opaque routing receiver returned by `scopeTarget(base, key)`. Scope-filtered event declarations require this carrier as their `this` type, while the real event subject remains an explicit argument. + +```ts type-equiv +type Scoped = object & { readonly [ScopedBrand]: T } +``` + +## Owned registration context + +`Scope` pairs the tagged registration context with two teardown surfaces. `rawDispose` preserves the exact Cordis disposer identity needed by an ordered composite effect; `dispose()` is the public shared quiescence boundary for direct and racing callers. + +```ts type-equiv +interface Scope { + ctx: Context + rawDispose: () => Promise | void + dispose(): Promise +} +``` diff --git a/docs/core-data-structures/skills.md b/docs/core-data-structures/skills.md new file mode 100644 index 0000000000..56ac91a6da --- /dev/null +++ b/docs/core-data-structures/skills.md @@ -0,0 +1,116 @@ +# Skills + +The [skill capability family](../../packages/skill) is split across three packages: the registry ([dsh-skill](../../packages/skill/skill), `ctx.skills`) merges provider catalogs; the local provider ([dsh-skill-local](../../packages/skill/skill-local)) scans project/custom/user directories; the consumer ([dsh-tool-skill](../../packages/skill/tool-skill)) owns the session-prefix catalog and model-facing `skill` tool. Skills are optional instructions, not session events, so their vocabulary lives here rather than in [core.md](core.md). + +Source: [`packages/skill/skill/src/index.ts`](../../packages/skill/skill/src/index.ts), [`packages/skill/skill-local/src/index.ts`](../../packages/skill/skill-local/src/index.ts), and [`packages/skill/tool-skill/src/index.ts`](../../packages/skill/tool-skill/src/index.ts). + +## Provider registry + +`ctx.skills` is a multi-provider registry. Providers can represent local directories, embedded plugin data, HTTP catalogs, or another source. Provider plugins register synchronously during `apply()`; remote initialization, authentication, and discovery are awaited by `list()`. Provider objects, lookup options, and candidates are readonly same-process contracts, so the registry borrows them instead of manufacturing defensive snapshots. The registry still validates semantic fields, resolves duplicate skill names first-wins by rank/provider order/local order, and sorts the final summaries by `name` for deterministic consumers. A provider `list()` rejection is logged and skipped without caching the degraded catalog; malformed candidates still fail fast because they violate the provider contract. + +```ts type-equiv +interface SkillProvider { + readonly name: string + readonly list: (options: SkillLookupOptions) => Promise + readonly get: (candidate: SkillCandidate, options: SkillLookupOptions) => Promise +} +``` + +## Local discovery priority + +The shipped local provider scans roots in rank order: + +| Rank | Source | Root | +|---|---|---| +| 100 | `project-dsh` | `/.dsh/skills` | +| 200 | `project-agents` | `/.agents/skills` | +| 300 | `custom` | `Config.customSkillDirs` | +| 400 | `user-dsh` | `/skills` | +| 500 | `user-agents` | `/skills` | + +The project root is the nearest ancestor containing `.git`; without one, the current cwd is used. When `ctx.fs` is available, the git-root walk probes `.git` through the filesystem service so remote or sandboxed workspaces do not fall back to the host filesystem boundary. The user DSH root skips its `.system` child. The local provider does not ship built-in system skills; deployments supply built-ins through another provider. + +## Skill identity + +Skill names are kebab-case (`^[a-z0-9]+(?:-[a-z0-9]+)*$`). The local provider accepts directory bundles (`/SKILL.md`) and flat Markdown files (`.md`). Nested recursive `**/SKILL.md` discovery is intentionally outside v1. + +```ts type-equiv +type SkillSource = 'project-dsh' | 'project-agents' | 'runtime' | 'user-dsh' | 'user-agents' | 'custom' | (string & {}) +``` + +## Summaries, candidates, and complete definitions + +`SkillSummary` is the registry's model-invocable summary shape. Consumers choose which fields to render; the session catalog uses only `name` and `description`, never the body or absolute file path. `disableModelInvocation` hides a skill from model listings while allowing trusted code to load it by name. + +```ts type-equiv +interface SkillSummary { + readonly name: string + readonly description: string + readonly whenToUse?: string + readonly disableModelInvocation?: boolean + readonly source: SkillSource + readonly provider: string + readonly resourceBase?: SkillResourceBase +} +``` + +`SkillCandidate` is the provider-to-registry shape. `locator` is opaque provider state; the registry only stores it and gives it back to the winning provider's `get()`. + +```ts type-equiv +interface SkillCandidate extends SkillSummary { + readonly rank: number + readonly locator: unknown + readonly path?: string + readonly metadata?: Readonly> +} +``` + +`SkillDefinition` is the complete parsed result returned by `ctx.skills.get()` and used by the `skill` tool. `resourceBase` tells the tool how to render relative-resource guidance for local, URL, or provider-managed skills. + +```ts type-equiv +type SkillResourceBase = + | { readonly kind: 'directory'; readonly path: string } + | { readonly kind: 'url'; readonly url: string } + | { readonly kind: 'opaque'; readonly description: string } +``` + +```ts type-equiv +interface SkillDefinition extends SkillSummary { + readonly content: string + readonly path?: string + readonly metadata?: Readonly> +} +``` + +Runtime skills use the same complete shape and participate in the same first-wins collection order. The returned disposer removes the contribution and invalidates discovery caches. + +```ts type-equiv +type SkillRegistration = Omit & { + readonly provider?: string +} +``` + +## Lookup and configuration + +Skill lookup is cwd-sensitive because providers may expose workspace-local skills, and its optional signal cancels provider work for the caller. Providers receive the same readonly options object used for cache identity and loading. Cancellation is checked before and after catalog selection, including cache hits, and races both discovery and full-definition loading. If no git root is found, the local provider treats the supplied cwd itself as the project root. + +```ts type-equiv +interface SkillLookupOptions { + readonly cwd?: string | undefined + readonly signal?: AbortSignal | undefined +} +``` + +The registry owns only its discovery-cache bound. The local provider owns filesystem roots (`dshHome`, `agentsHome`, and `customSkillDirs`). The consumer owns its catalog description bound. + +```ts type-equiv +interface Config { + readonly collectCacheMaxEntries?: number +} +``` + +## Session catalog and tool contract + +`dsh-tool-skill` contributes a user-role `` through `agent/session-prefix`. The catalog contains sorted skill `name` and normalized, XML-escaped `description` only; it omits bodies, paths, sources, providers, and routing hints. Prefix discovery forwards the caller's abort signal through `SkillLookupOptions`. `catalogDescriptionMaxLength` is the consumer config for the description bound, with default `500` and integer minimum `3`. Its request-only, header-logged lifecycle is defined by the [session-prefix RFC](../rfc/implemented/feature/2026-07-07-session-prefix.md). + +The model-facing `skill({ name })` tool validates the kebab-case name, loads the complete definition for the calling agent cwd, reports an unresolved skill as unknown or no longer available, rejects `disableModelInvocation` skills, and returns a tool result containing ``, ``, and ``. `resourceBase` resolves explicitly referenced scripts, references, and assets only as needed; the loaded result does not enumerate a skill directory. The tool result is the model-visible path for complete instructions. diff --git a/docs/core-data-structures/subagent.md b/docs/core-data-structures/subagent.md index f21ef15669..5454f326b3 100644 --- a/docs/core-data-structures/subagent.md +++ b/docs/core-data-structures/subagent.md @@ -12,37 +12,41 @@ A provider advertises its **start-time** features on a static descriptor the ser ```ts type-equiv interface SubagentCapabilities { - outputSchema: boolean - depthLimit: boolean - toolFilter: boolean + readonly outputSchema: boolean + readonly depthLimit: boolean + readonly toolFilter: boolean + readonly persona: boolean } ``` ## The start request -What a caller asks for when starting a subagent. The tool layer builds this from the model's `{ description, prompt }` plus its own config; the service validates the start-time capabilities against the named provider, then passes it to `provider.start`. `parent` is REQUIRED — in-process backends read `parent.session.header` for the working directory, the `parentSession` lineage, and the delegation depth. The three optional fields (`outputSchema`, `maxDepth`, `toolFilter`) each gate on the matching `SubagentCapabilities` flag. `outputSchema` is an object-rooted JSON Schema within the subset `assertSupportedOutputSchema` (dsh-tools) enforces — a schema outside it is rejected loud at start; the in-process backends realize it with a forced `structured_output` capture tool (see the [driver README](../../packages/subagent/subagent-inprocess/README.md)). +What a caller asks for when starting a subagent. The tool layer builds this from the model's `{ description, prompt }` plus its own config; the service validates the start-time capabilities against the named provider, then passes it to `provider.start`. `parent` is REQUIRED — in-process backends read `parent.session.header` for the working directory, the `parentSession` lineage, and the delegation depth. The four optional fields (`outputSchema`, `maxDepth`, `toolFilter`, `persona`) each gate on the matching `SubagentCapabilities` flag — in-process backends realize `toolFilter` as a scoped `tools.restrict()` and `persona` as a scoped shadowing `deployment:persona` section, both composed in the child's creation window. `outputSchema` is an object-rooted JSON Schema within the subset `assertSupportedOutputSchema` (dsh-tools) enforces — a schema outside it is rejected loud at start; the in-process backends realize it with a forced `structured_output` capture tool (see the [driver README](../../packages/subagent/subagent-inprocess/README.md)). ```ts type-equiv interface SubagentStartRequest { - prompt: ContentBlock[] - parent: Agent - signal?: AbortSignal - agentOptions?: AgentOptions - outputSchema?: StructuredOutputSchema - maxDepth?: number - toolFilter?: { allow?: string[]; deny?: string[] } + readonly prompt: ContentBlock[] + readonly parent: Agent + readonly signal: AbortSignal + readonly agentOptions?: AgentOptions + readonly outputSchema?: StructuredOutputSchema + readonly maxDepth?: number + readonly toolFilter?: ToolRestriction + readonly persona?: string } ``` +`signal` is the single cancellation channel before and after readiness. The [subagent composition-controls RFC](../rfc/implemented/feature/2026-07-12-subagent-persona-tool-filter-and-depth.md) owns the persona, live global-tool filter, absolute-depth, and visibility-not-authority rationale. + ## The terminal result: `SubagentResult` -The outcome of a run, resolved by `SubagentRun.result`. `structured` is present iff the request carried an `outputSchema` AND the provider honored it. A non-`completed` `stopReason` means `output` may be partial — the consumer maps it to an `isError` tool result rather than reporting partial output as success. +The outcome of a run, resolved by `SubagentRun.result`. `structured` is present only after a requested `outputSchema` was successfully satisfied; requesting a schema does not guarantee it, and a provider may return `stopReason: 'error'` when the child fails or finishes without a valid capture. A non-`completed` `stopReason` means `output` may be partial — the consumer maps it to an `isError` tool result rather than reporting partial output as success. ```ts type-equiv interface SubagentResult { - output: ContentBlock[] - structured?: unknown - stopReason: SubagentStopReason + readonly output: ContentBlock[] + readonly structured?: unknown + readonly stopReason: SubagentStopReason } ``` @@ -60,37 +64,36 @@ interface SubagentStopReasonMap { ## A live run: `SubagentRun` -The handle the consumer holds while a child executes. The consumer awaits `result`, may `cancel` mid-flight, and MUST `dispose` on every path to reach child quiescence (no leaked idle child / session). `result` does NOT reject on a child-level failure — a model/transport failure resolves with `stopReason: 'error'` — so the consumer maps a non-`completed` reason to an `isError` result; it rejects only on an infrastructure fault the seam cannot represent. `sendMessage` and `resume` are OPTIONAL: a provider that supports the runtime capability defines the method; one that doesn't omits it. +The handle the consumer holds after a provider has established a ready child. The consumer awaits `result` and MUST `dispose` on every path to cancel remaining work and reach child quiescence. `result` does NOT reject on a child-level failure — a model/transport failure resolves with `stopReason: 'error'` — so the consumer maps a non-`completed` reason to an `isError` result; it rejects only on an infrastructure fault the seam cannot represent. `sendMessage` and `resume` are OPTIONAL: a provider that supports the runtime capability defines the method; one that doesn't omits it. ```ts type-equiv interface SubagentRun { readonly id: AgentId readonly result: Promise - cancel(reason?: string): void dispose(): Promise sendMessage?(content: ContentBlock[]): void - resume?(content: ContentBlock[]): SubagentRun + resume?(content: ContentBlock[]): Promise } ``` ## The provider seam: `SubagentProvider` -One transport for running a child agent. Implementations register under a unique name via `SubagentService.registerProvider`; multiple coexist in one context. The service validates every requested start-time capability before calling `start`, so an implementation may assume e.g. `request.maxDepth` is honorable when present. `inheritsParentContext` is a DESCRIPTIVE fact beside the capabilities (nothing validates against it): whether a child sees the parent conversation (`fork`: true, `spawn`/`acp`: false) — the model-facing consumer derives truthful tool wording from it. +One transport for running a child agent. Implementations register under a unique name via `SubagentService.registerProvider`; multiple coexist in one context. The service validates every requested start-time capability before calling `start`, so an implementation may assume e.g. `request.maxDepth` is honorable when present. `inheritsParentContext` is a DESCRIPTIVE fact beside the capabilities (nothing validates against it): whether a child sees the parent conversation (`fork`: true, `spawn`/`acp`: false) — the model-facing consumer derives truthful tool wording from it. It describes conversation history only, not tool registrations, injected services, or authority inheritance. ```ts type-equiv interface SubagentProvider { readonly name: string readonly capabilities: SubagentCapabilities readonly inheritsParentContext: boolean - start(request: SubagentStartRequest): SubagentRun + start(request: SubagentStartRequest): Promise } ``` -The service (`ctx.subagents`) emits `subagent/start` when a run begins and `subagent/end` when it settles (see the [events catalog](../cordis-catalog/events.md)). `subagent/end` carries `lastAssistantMessage` (the child's final `output`) on the settle path, so an observer sees WHAT the subagent produced without holding the run (absent when the run rejected at the infrastructure level — no result was produced). These are **observe-only** events: both are plain `emit`s (the `subagent/end` fires from a detached `.then` after the result settles and awaits no listener), so a subscriber observes but cannot change the run. Both emits contain a thrown listener **per listener** (logged, never propagated): one bad subscriber can neither strand a live run, surface as an unhandled rejection on the detached settle hook, nor starve the listeners registered after it. +`SubagentProvider.start()` and `ctx.subagents.start()` are the publication boundary: their promises fulfill only with a ready run. The service attaches result observation, emits `subagent/start`, and returns the same holder-owned run; a rejected start has already cleaned provider-owned partial resources and emits neither lifecycle event. For an in-process provider, a start listener can resolve the live child with `ctx.agents.get(info.id)`; a remote provider need not publish into the local registry. `subagent/end` carries `lastAssistantMessage` (the child's final `output`) on the settle path and reports `error` on infrastructure rejection. Both lifecycle events are observe-only emits with per-listener exception containment. ## In-process backends: depth and seed -The two in-process backends ([dsh-subagent-spawn](../../packages/subagent/subagent-spawn) fresh, [dsh-subagent-fork](../../packages/subagent/subagent-fork) seeded) run the child as a child `Agent` on the same context via `ctx.agents.create`. Two pieces of vocabulary ride on the existing agent/session types rather than new core types: +The two in-process backends ([dsh-subagent-spawn](../../packages/subagent/subagent-spawn) fresh, [dsh-subagent-fork](../../packages/subagent/subagent-fork) seeded) run the child as an ordinary `Agent` in the same application. The provider creates it directly through `parent.ctx`, passes the required signal into the core creation transaction, and delegates quiescent disposal to the returned `AgentHandle`. Provider removal prevents new starts but does not revoke an accepted run. The child receives a flat new scope rather than inheriting the parent's registrations. Two pieces of vocabulary ride on the existing agent/session types rather than new core types: -- **Delegation depth** is a merge-extensible `AgentOptions.subagentDepth` field (`0` for a top-level agent, parent + 1 for a child). The seam owns it — the loop neither sets nor reads it — so a nested spawn reads its parent's depth from `parent.options.subagentDepth` and the `depthLimit` capability caps the tree by refusing a child whose depth would exceed `request.maxDepth`. +- **Delegation depth** is a merge-extensible `AgentOptions.subagentDepth` field (`0` for a top-level agent, parent + 1 for a child). Only `undefined` means top level; every stored present value must be a non-negative safe integer. The seam owns it — the loop neither sets nor reads it — so a nested spawn validates its parent's stored depth, rejects a derived child depth outside the safe-integer domain, and applies a defined absolute `request.maxDepth` cap to that child. - **Fork seeding** uses `CreateAgentOptions.seed` (a `SessionEvent[]` prefix threaded through `AgentLoop.createAgent` → `ctx.sessions.prepare({ seed })`, the same primitive `resume` uses). The fork backend passes a *balanced completed-turn prefix* of the parent's log — the parent's events up to and including its last `turn/end` — so the seed is contiguous-from-0 and the [invariants](../../packages/support/invariants) replay accepts it (the in-flight, unbalanced turn is excluded). diff --git a/docs/core-data-structures/system-prompt.md b/docs/core-data-structures/system-prompt.md new file mode 100644 index 0000000000..4b6f1e6625 --- /dev/null +++ b/docs/core-data-structures/system-prompt.md @@ -0,0 +1,38 @@ +# System Prompt Assembly + +The [system-prompt package](../../packages/core/system-prompt) owns the data exchanged between prompt contributors and one assembly call. The package [README](../../packages/core/system-prompt/README.md) documents registration, ordering, scoping, and rendering behavior; this page pins the literal cross-package shapes that plugins implement or pass. + +Source: [`packages/core/system-prompt/src/index.ts`](../../packages/core/system-prompt/src/index.ts). + +## Assembly context + +`AssembleContext` identifies the scope layer one assembly resolves. It is merge-extensible: `dsh-agent` adds the optional live `agent` field, and `assembleContextFor(agent)` sets that field and `scope` together. + +```ts type-equiv +interface AssembleContext { + scope?: ScopeKey +} +``` + +## Tool-provider result + +`ToolProviderResult.schemas` is the model-visible set for the current assembly. `knownNames` is the provider's pre-restriction name universe used to distinguish a configured-name typo from a known tool that is deliberately hidden in this scope. + +```ts type-equiv +interface ToolProviderResult { + readonly schemas: readonly ToolSchema[] + readonly knownNames?: readonly string[] +} +``` + +## Prompt sections + +`PromptSection` is a readonly same-process registration contract. Its text may be static or resolved from the current assembly context. + +```ts type-equiv +interface PromptSection { + readonly name: string + readonly order: number + readonly text: string | ((context: AssembleContext) => string) +} +``` diff --git a/docs/core-data-structures/tools.md b/docs/core-data-structures/tools.md index 96d3e79bdc..d7e0464f01 100644 --- a/docs/core-data-structures/tools.md +++ b/docs/core-data-structures/tools.md @@ -1,6 +1,6 @@ # Tools -The tool pipeline of [dsh-tools](../../packages/core/tools). [core.md](core.md) introduces `ToolDefinition` as the one pipeline-authoring type promoted to the spine and `ToolSchema` as the model-facing wire shape. This page owns the full `ToolDefinition`, the typed schema DSL that builds it, the waterfall execution shapes, and the UI-presentation vocabulary. +The tool pipeline of [dsh-tools](../../packages/core/tools). [core.md](core.md) introduces `ToolDefinition` as the one pipeline-authoring type promoted to the spine and `ToolSchema` as the model-facing wire shape. This page owns the full `ToolDefinition`, the typed schema DSL that builds it, the guarded execution shapes, and the UI-presentation vocabulary. Source: [`packages/core/tools/src/index.ts`](../../packages/core/tools/src/index.ts) · [`packages/core/tools/src/schema.ts`](../../packages/core/tools/src/schema.ts) · [`packages/core/tools/src/presentation.ts`](../../packages/core/tools/src/presentation.ts) @@ -81,22 +81,60 @@ type InferArgs = Simplify< `defineTool({ name, description, parameters, execute, … })` ties it together: `parameters` is a `SchemaSpec`, `execute(args, exec)` gets `args: InferArgs`, and the helper converts the spec to JSON Schema (`schemaSpecToJsonSchema`) for the wire and validates model-generated args (`validateArgs`) before the typed body runs. A mismatch throws `ToolArgsError` (`code: 'INVALID_ARGS'`), which the registry turns into an `isError` result so the model can self-correct. Why a custom DSL and not schemastery: tool parameters need JSON Schema (the LLM wire format), not validation/transformation — the lightweight DSL gives the best authoring DX with the smallest surface. -## Execution: the `tools/pre-execute` / `tools/post-execute` pipeline shapes +Registration is a trusted same-process contract. The registry borrows the typed definition as readonly input and validates only semantic requirements such as a positive finite `timeoutMs`; `schemas()` materializes the explicit model-facing projection at the model boundary so execution and presentation share one resolved definition without leaking callbacks onto the wire. -`ctx.tools.execute()` runs each call through a two-waterfall pipeline — `tools/pre-execute` (the allow/deny/ask gate) → core dispatch → `tools/post-execute` (inspect/replace the result, attach context) — the seams where sandbox, permission, hook, and plan-mode plugins gate or transform a call. The pending call is a `ToolExecution`; the outcome is a `ToolExecutionResult`. +## `ToolRestriction` — one scope's live global filter + +`ToolRestriction` applies only to the live deployment-global tool layer. The registry compiles readonly names into private sets, intersects multiple restrictions, then overlays scope-local tools. A deny-only filter admits later unlisted globals, while an allow-list excludes them. ```ts type-equiv -interface ToolExecution { - callId: CallId - name: string +interface ToolRestriction { + readonly allow?: readonly string[] + readonly deny?: readonly string[] +} +``` + +## Execution: extensible waterfalls plus monotonic policy + +`ctx.tools.execute()` accepts a caller-owned `ToolExecutionInput`, materializes its parsed JSON arguments once into a pipeline-owned `ToolExecution`, and runs that call through `tools/pre-execute` (the reorderable allow/deny/ask waterfall) → registered monotonic guards → `tools/execute` (around-dispatch wrappers) → `tools/post-execute` (inspect/replace the result) → `tools/result` (the immutable authoritative outcome). The outcome is a `ToolExecutionResult`. + +```ts type-equiv +type ToolExecutionToken = symbol & { readonly [toolExecutionTokenBrand]: true } +``` + +```ts type-equiv +interface ToolExecutionInput { + readonly callId: CallId + readonly name: string /** Parsed JSON arguments (unknown — tools validate their own input). */ - arguments: unknown + readonly arguments: unknown /** The agent on whose behalf the call runs (set by the agent loop). */ - agent?: Agent + readonly agent?: Agent + /** + * Opaque token of the enclosing transport execution, when one exists. Code + * Mode sets this on SDK sub-dispatches so commit-style observers can wait for + * the outer `run_code` outcome without receiving its live mutable execution. + */ + readonly parent?: ToolExecutionToken signal?: AbortSignal } ``` +```ts type-equiv +interface ToolExecution extends ToolExecutionInput { + /** Registry-assigned identity shared with nested calls only as their opaque `parent` token. */ + readonly token: ToolExecutionToken +} +``` + +`ToolExecutionToken` is a compile-time opaque fresh `Symbol` at runtime; identity comparison is its only operation. Before policy runs, `ctx.tools.execute()` materializes `arguments` as detached lossless JSON, assigns the token, and deep-freezes the accepted arguments. A non-JSON value is normalized to an error before policy. `token`, `callId`, `name`, `arguments`, `agent`, and the optional `parent` token are readonly throughout the waterfalls, while an around-dispatch wrapper may add, replace, or remove only optional `signal`. After the complete pipeline the registry freezes the execution and exposes its stable identity to `tools/result` observers. + +A `ToolGuard` is scope-aware final pre-dispatch policy. Its shape deliberately has no allow result: `undefined` preserves the waterfall decision, while a returned reason can only reduce permission, so a later listener cannot undo it. + +```ts type-equiv +type ToolGuard = (execution: Readonly) => string | undefined +``` + ```ts type-equiv interface ToolExecutionResult { callId: CallId @@ -129,7 +167,9 @@ interface ToolExecutionResult { } ``` -Each interception waterfall returns a typed **Decision** (the idiom shared with the `agent/*` seams). `tools/pre-execute` listeners receive `(exec, next)` and return a `PreToolDecision`; `tools/post-execute` listeners receive `(exec, result, next)` and return a `PostToolDecision`: +The registry materializes and freezes the final accepted result immediately before `tools/result`. Its content, structured error, additional context, and presentation metadata must round-trip losslessly through JSON; an invalid outcome becomes a JSON-safe `isError` result, so the observed live outcome is safe for the later durable `tool/result` append. + +Each interception waterfall returns a typed **Decision** (the idiom shared with the `agent/*` seams). `tools/pre-execute` listeners receive `(exec, next)` and return a `PreToolDecision`; `tools/execute` wrappers return a `ToolExecutionResult`; `tools/post-execute` listeners receive `(exec, result, next)` and return a `PostToolDecision`: ```ts type-equiv type PreToolDecision = @@ -144,7 +184,7 @@ type PostToolDecision = | { kind: 'block'; feedback: ContentBlock[]; additionalContext?: HookContext } ``` -Call `next()` to delegate to the default (allow / accept-unchanged), or return a decision to short-circuit. A `pre-execute` `deny` (or `ask`, which degrades to deny until the permission system lands) skips dispatch and yields an `isError` result; input rewrite is deliberately NOT offered on `PreToolDecision` (it would desync the pre-execution audit/history/UI from what ran — its own proposed RFC). A `post-execute` `accept` may replace the model-facing `content` (clean, because `tool/result` is logged after `execute()` returns); a `block` turns the call into an `isError` whose content is the corrective `feedback`. Core dispatch sits between the waterfalls as plain code; the tool body keeps its own try/catch so a thrown tool still reaches `post-execute` as an `isError`. An unregistered tool routes through the same catch as a tool-thrown error, so both failure classes get a structured `{ name, code }` (`ToolNotFoundError` → `UNKNOWN_TOOL`) — the loop records a failed tool call instead of failing the whole turn. +Call `next()` to delegate to the default (allow / dispatch / accept-unchanged), or return a decision/result to short-circuit. A `pre-execute` `deny` skips dispatch and yields an `isError` result. An `ask` resolves through the optional approval seam: only `allowed-once` proceeds, while every non-grant, missing channel/service, or agent-less request becomes a normalized denial. A registered `ToolGuard` then runs and can still impose a final denial. Input rewrite is deliberately NOT offered on `PreToolDecision` because it would desync the pre-execution audit/history/UI from what ran. A `post-execute` `accept` may replace the model-facing `content`; a `block` turns the call into an `isError` whose content is the corrective `feedback`. The synchronous `tools/result` notification then receives the frozen execution identity and a deep-frozen result snapshot after every wrapper, post decision, and outer error catch; observers cannot transform the outcome or race each other through payload mutation, and one observer failure neither changes the result nor starves peers. An unregistered tool routes through the same catch as a tool-thrown error, so both failure classes get a structured `{ name, code }` (`ToolNotFoundError` → `UNKNOWN_TOOL`) — the loop records a failed tool call instead of failing the whole turn. ## The structured-output schema subset diff --git a/docs/development.i18n.yaml b/docs/development.i18n.yaml index c1e557170d..e2dd772eb0 100644 --- a/docs/development.i18n.yaml +++ b/docs/development.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write -development.md: bd6f6b561480419abea7a42a44b4078e2c59b1cb -development.zh.md: 54bf19765d2b4dc419e6b71684dbcfcd28230541 +development.md: ea5f2e5d08acbaf1dfce4661530218dbf1a051b9 +development.zh.md: 50877796f2ff47ad46cc67b35f3cd7b5704c8315 diff --git a/docs/development.md b/docs/development.md index bd6f6b5614..ea5f2e5d08 100644 --- a/docs/development.md +++ b/docs/development.md @@ -67,7 +67,7 @@ These hooks do not exactly mirror CI. Notably, `pre-push` runs unit tests withou ## CI gates -The keyless GitHub workflow has eight jobs: five Node 24 lanes run static gates, lint, coverage, snapshot replay, and artifact gates separately, and three compatibility jobs run `pnpm run check:node-compat` on Node 22.19, 24, and 26. The lane schedulers fan out independent gates from `package.json`: constraints, typecheck, lint, coverage, snapshot replay, `doc-sync` members, module-graph freshness, `knip`, and the echo-agent smoke test. +The keyless GitHub workflow has eight jobs: five Node 24 lanes run static gates, lint, coverage, snapshot replay, and artifact gates separately, and three compatibility jobs run `pnpm run check:node-compat` on Node 22.19, 24, and 26. The compatibility command runs the TypeScript typecheck and a keyless workflow-workerthread source-launch smoke on every runtime, so the matrix proves that the source graph typechecks and that a real unbuilt Worker loader path executes; the other lane schedulers fan out independent gates from `package.json`: constraints, lint, coverage, snapshot replay, `doc-sync` members, module-graph freshness, `knip`, and the echo-agent smoke test. `pnpm run build` feeds the artifact lane, and `publint`, `verify-node-next-types`, and built-bin smoke tests wait for build output. The separate real-API workflow runs `pnpm run test:e2e` with a secret and `DSH_E2E_MAX_WORKERS=14`. diff --git a/docs/development.zh.md b/docs/development.zh.md index 54bf19765d..50877796f2 100644 --- a/docs/development.zh.md +++ b/docs/development.zh.md @@ -67,7 +67,7 @@ vendor manifest 守卫检查 `vendor/*/src` 下的改动是否连同对应的 `v ## CI 门禁 -keyless GitHub 工作流有八个 job:五个 Node 24 lane 分别运行 static gates、lint、coverage、snapshot replay 和 artifact gates,三个兼容性 job 在 Node 22.19、24 和 26 上运行 `pnpm run check:node-compat`。各 lane 调度器并发运行来自 `package.json` 的独立门禁:constraints、typecheck、lint、coverage、snapshot replay、`doc-sync` 成员、module graph 新鲜度、`knip` 和 echo-agent 冒烟测试。 +keyless GitHub 工作流有八个 job:五个 Node 24 lane 分别运行 static gates、lint、coverage、snapshot replay 和 artifact gates,三个兼容性 job 在 Node 22.19、24 和 26 上运行 `pnpm run check:node-compat`。兼容性命令会在每个运行时上运行 TypeScript 类型检查和 keyless 的 workflow-workerthread 源码启动冒烟测试,因此该矩阵既证明源码图能通过类型检查,也会实际执行一条未构建的 Worker loader 路径;其他 lane 调度器并发运行来自 `package.json` 的独立门禁:constraints、lint、coverage、snapshot replay、`doc-sync` 成员、module graph 新鲜度、`knip` 和 echo-agent 冒烟测试。 `pnpm run build` 供给 artifact lane,`publint`、`verify-node-next-types` 和 built-bin 冒烟测试等待 build 输出。单独的真实 API 工作流带密钥运行 `pnpm run test:e2e`,并设置 `DSH_E2E_MAX_WORKERS=14`。 diff --git a/docs/event-producer-consumer.md b/docs/event-producer-consumer.md index acbe700965..a8810a01fe 100644 --- a/docs/event-producer-consumer.md +++ b/docs/event-producer-consumer.md @@ -7,40 +7,52 @@ This matrix shows which packages dispatch each harness-owned event and which pac | Event | Mode | Declared in | Dispatchers | Listeners | | --- | --- | --- | --- | --- | -| `agent/created` | `emit` | [`packages/core/agent/src/types.ts:265`](../packages/core/agent/src/types.ts) | [`agent`](../packages/core/agent) (`emit`) | [`stdio-agent`](../packages/ui/stdio-agent) | -| `agent/disposed` | `emit` | [`packages/core/agent/src/types.ts:272`](../packages/core/agent/src/types.ts) | [`agent`](../packages/core/agent) (`emit`) | [`stdio-agent`](../packages/ui/stdio-agent) | -| `agent/error` | `emit` | [`packages/core/agent/src/types.ts:476`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | - | -| `agent/pre-step` | `serial` | [`packages/core/agent/src/types.ts:357`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`serial`) | [`compact-basic`](../packages/compact/compact-basic) | -| `agent/prompt-submit` | `waterfall` | [`packages/core/agent/src/types.ts:370`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`hooks-claude`](../packages/hooks/hooks-claude), [`hooks-codex`](../packages/hooks/hooks-codex), [`repeat-tool-guard`](../packages/guard/repeat-tool-guard) | -| `agent/queued` | `emit` | [`packages/core/agent/src/types.ts:290`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | - | -| `agent/request` | `waterfall` | [`packages/core/agent/src/types.ts:394`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | - | -| `agent/session-prefix` | `waterfall` | [`packages/core/agent/src/types.ts:441`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | - | -| `agent/session-start` | `emit` | [`packages/core/agent/src/types.ts:305`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`hooks-claude`](../packages/hooks/hooks-claude), [`hooks-codex`](../packages/hooks/hooks-codex) | -| `agent/status` | `emit` | [`packages/core/agent/src/types.ts:281`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`acp`](../packages/ui/acp), [`invariants`](../packages/support/invariants), [`repeat-tool-guard`](../packages/guard/repeat-tool-guard), [`stdio-agent`](../packages/ui/stdio-agent) | -| `agent/step-result` | `waterfall` | [`packages/core/agent/src/types.ts:451`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | - | -| `agent/turn-continuation` | `waterfall` | [`packages/core/agent/src/types.ts:464`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`hooks-claude`](../packages/hooks/hooks-claude), [`hooks-codex`](../packages/hooks/hooks-codex) | +| `agent/created` | `emit` | [`packages/core/agent/src/types.ts:316`](../packages/core/agent/src/types.ts) | [`agent`](../packages/core/agent) (`events.dispatch`) | [`stdio-agent`](../packages/ui/stdio-agent) | +| `agent/disposed` | `emit` | [`packages/core/agent/src/types.ts:331`](../packages/core/agent/src/types.ts) | [`agent`](../packages/core/agent) (`events.dispatch`) | [`stdio-agent`](../packages/ui/stdio-agent) | +| `agent/error` | `emit` | [`packages/core/agent/src/types.ts:605`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | - | +| `agent/pre-step` | `serial` | [`packages/core/agent/src/types.ts:438`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`serial`) | [`compact-basic`](../packages/compact/compact-basic), [`user-approval`](../packages/ui/user-approval) | +| `agent/prompt-submit` | `waterfall` | [`packages/core/agent/src/types.ts:456`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`acp`](../packages/ui/acp), [`hooks-claude`](../packages/hooks/hooks-claude), [`hooks-codex`](../packages/hooks/hooks-codex), [`repeat-tool-guard`](../packages/guard/repeat-tool-guard) | +| `agent/queued` | `emit` | [`packages/core/agent/src/types.ts:360`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | - | +| `agent/request` | `waterfall` | [`packages/core/agent/src/types.ts:485`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | - | +| `agent/session-prefix` | `waterfall` | [`packages/core/agent/src/types.ts:537`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`tool-skill`](../packages/skill/tool-skill) | +| `agent/session-start` | `emit` | [`packages/core/agent/src/types.ts:381`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`hooks-claude`](../packages/hooks/hooks-claude), [`hooks-codex`](../packages/hooks/hooks-codex), [`invariants`](../packages/support/invariants) | +| `agent/status` | `emit` | [`packages/core/agent/src/types.ts:345`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`invariants`](../packages/support/invariants), [`repeat-tool-guard`](../packages/guard/repeat-tool-guard), [`stdio-agent`](../packages/ui/stdio-agent) | +| `agent/step-result` | `waterfall` | [`packages/core/agent/src/types.ts:552`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | - | +| `agent/turn-continuation` | `waterfall` | [`packages/core/agent/src/types.ts:570`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`hooks-claude`](../packages/hooks/hooks-claude), [`hooks-codex`](../packages/hooks/hooks-codex) | +| `agent/turn-stop` | `serial` | [`packages/core/agent/src/types.ts:588`](../packages/core/agent/src/types.ts) | [`agent-loop`](../packages/core/agent-loop) (`serial`) | [`subagent-inprocess`](../packages/subagent/subagent-inprocess) | +| `approval/request` | `waterfall` | [`packages/ui/user-approval/src/index.ts:70`](../packages/ui/user-approval/src/index.ts) | [`user-approval`](../packages/ui/user-approval) (`waterfall`) | [`acp`](../packages/ui/acp) | | `fs/edit-intent` | `waterfall` | [`packages/fs/fs/src/index.ts:123`](../packages/fs/fs/src/index.ts) | [`tool-fs`](../packages/fs/tool-fs) (`waterfall`) | [`fs-policy`](../packages/fs/fs-policy) | | `fs/observed` | `emit` | [`packages/fs/fs/src/index.ts:138`](../packages/fs/fs/src/index.ts) | [`tool-fs`](../packages/fs/tool-fs) (`emit`) | [`fs-policy`](../packages/fs/fs-policy) | | `fs/write-intent` | `waterfall` | [`packages/fs/fs/src/index.ts:109`](../packages/fs/fs/src/index.ts) | [`tool-fs`](../packages/fs/tool-fs) (`waterfall`) | [`fs-policy`](../packages/fs/fs-policy) | | `llm/stream` | `waterfall` | [`packages/llm/llm/src/index.ts:39`](../packages/llm/llm/src/index.ts) | [`llm`](../packages/llm/llm) (`waterfall`) | [`invariants`](../packages/support/invariants), [`llm-replay`](../packages/support/llm-replay) | -| `session/created` | `emit` | [`packages/core/session/src/index.ts:39`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`emit`) | [`invariants`](../packages/support/invariants), [`session-persistence`](../packages/session-persistence/session-persistence) | -| `session/event` | `emit` | [`packages/core/session/src/index.ts:47`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`emit`) | [`acp`](../packages/ui/acp), [`invariants`](../packages/support/invariants), [`session-persistence`](../packages/session-persistence/session-persistence), [`stdio-agent`](../packages/ui/stdio-agent) | -| `session/flush` | `parallel` | [`packages/core/session/src/index.ts:57`](../packages/core/session/src/index.ts) | [`agent-loop`](../packages/core/agent-loop) (`parallel`) | [`session-persistence`](../packages/session-persistence/session-persistence) | -| `subagent/end` | `emit` | [`packages/subagent/subagent/src/index.ts:98`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`events.dispatch`) | [`hooks-claude`](../packages/hooks/hooks-claude) | -| `subagent/provider-added` | `emit` | [`packages/subagent/subagent/src/index.ts:72`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`emit`) | [`tool-subagent`](../packages/subagent/tool-subagent) | -| `subagent/provider-removed` | `emit` | [`packages/subagent/subagent/src/index.ts:83`](../packages/subagent/subagent/src/index.ts) | - | [`tool-subagent`](../packages/subagent/tool-subagent) | -| `subagent/start` | `emit` | [`packages/subagent/subagent/src/index.ts:91`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`events.dispatch`) | [`hooks-claude`](../packages/hooks/hooks-claude) | -| `system-prompt/assemble` | `waterfall` | [`packages/core/system-prompt/src/index.ts:38`](../packages/core/system-prompt/src/index.ts) | [`system-prompt`](../packages/core/system-prompt) (`waterfall`) | - | -| `system-prompt/change` | `emit` | [`packages/core/system-prompt/src/index.ts:44`](../packages/core/system-prompt/src/index.ts) | [`system-prompt`](../packages/core/system-prompt) (`emit`) | - | -| `tools/change` | `emit` | [`packages/core/tools/src/index.ts:132`](../packages/core/tools/src/index.ts) | [`tools`](../packages/core/tools) (`emit`) | - | -| `tools/execute` | `waterfall` | [`packages/core/tools/src/index.ts:111`](../packages/core/tools/src/index.ts) | [`tools`](../packages/core/tools) (`waterfall`) | [`timeout-policy`](../packages/timeout/timeout-policy) | -| `tools/post-execute` | `waterfall` | [`packages/core/tools/src/index.ts:127`](../packages/core/tools/src/index.ts) | [`tools`](../packages/core/tools) (`waterfall`) | [`hooks-claude`](../packages/hooks/hooks-claude), [`hooks-codex`](../packages/hooks/hooks-codex), [`repeat-tool-guard`](../packages/guard/repeat-tool-guard) | -| `tools/pre-execute` | `waterfall` | [`packages/core/tools/src/index.ts:91`](../packages/core/tools/src/index.ts) | [`tools`](../packages/core/tools) (`waterfall`) | [`hooks-claude`](../packages/hooks/hooks-claude), [`hooks-codex`](../packages/hooks/hooks-codex) | +| `session/created` | `emit` | [`packages/core/session/src/index.ts:52`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`invariants`](../packages/support/invariants), [`session-persistence`](../packages/session-persistence/session-persistence) | +| `session/disposed` | `emit` | [`packages/core/session/src/index.ts:64`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | - | +| `session/event` | `emit` | [`packages/core/session/src/index.ts:83`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`acp`](../packages/ui/acp), [`invariants`](../packages/support/invariants), [`session-persistence`](../packages/session-persistence/session-persistence), [`stdio-agent`](../packages/ui/stdio-agent) | +| `session/flush` | `parallel` | [`packages/core/session/src/index.ts:101`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`session-persistence`](../packages/session-persistence/session-persistence) | +| `skill/provider-added` | `emit` | [`packages/skill/skill/src/index.ts:131`](../packages/skill/skill/src/index.ts) | [`skill`](../packages/skill/skill) (`emit`) | - | +| `skill/provider-removed` | `emit` | [`packages/skill/skill/src/index.ts:137`](../packages/skill/skill/src/index.ts) | [`skill`](../packages/skill/skill) (`emit`) | - | +| `subagent/end` | `emit` | [`packages/subagent/subagent/src/index.ts:90`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`events.dispatch`) | [`hooks-claude`](../packages/hooks/hooks-claude) | +| `subagent/provider-added` | `emit` | [`packages/subagent/subagent/src/index.ts:66`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`emit`) | [`tool-subagent`](../packages/subagent/tool-subagent) | +| `subagent/provider-removed` | `emit` | [`packages/subagent/subagent/src/index.ts:72`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`events.dispatch`) | [`tool-subagent`](../packages/subagent/tool-subagent) | +| `subagent/start` | `emit` | [`packages/subagent/subagent/src/index.ts:82`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`events.dispatch`) | [`hooks-claude`](../packages/hooks/hooks-claude) | +| `system-prompt/assemble` | `waterfall` | [`packages/core/system-prompt/src/index.ts:49`](../packages/core/system-prompt/src/index.ts) | [`system-prompt`](../packages/core/system-prompt) (`waterfall`) | - | +| `system-prompt/change` | `emit` | [`packages/core/system-prompt/src/index.ts:59`](../packages/core/system-prompt/src/index.ts) | [`system-prompt`](../packages/core/system-prompt) (`emit`) | - | +| `tools/change` | `emit` | [`packages/core/tools/src/index.ts:173`](../packages/core/tools/src/index.ts) | [`tools`](../packages/core/tools) (`emit`) | - | +| `tools/execute` | `waterfall` | [`packages/core/tools/src/index.ts:128`](../packages/core/tools/src/index.ts) | [`tools`](../packages/core/tools) (`waterfall`) | [`timeout-policy`](../packages/timeout/timeout-policy) | +| `tools/post-execute` | `waterfall` | [`packages/core/tools/src/index.ts:148`](../packages/core/tools/src/index.ts) | [`tools`](../packages/core/tools) (`waterfall`) | [`hooks-claude`](../packages/hooks/hooks-claude), [`hooks-codex`](../packages/hooks/hooks-codex), [`repeat-tool-guard`](../packages/guard/repeat-tool-guard) | +| `tools/pre-execute` | `waterfall` | [`packages/core/tools/src/index.ts:101`](../packages/core/tools/src/index.ts) | [`tools`](../packages/core/tools) (`waterfall`) | [`hooks-claude`](../packages/hooks/hooks-claude), [`hooks-codex`](../packages/hooks/hooks-codex) | +| `tools/result` | `emit` | [`packages/core/tools/src/index.ts:163`](../packages/core/tools/src/index.ts) | [`tools`](../packages/core/tools) (`events.dispatch`) | [`subagent-inprocess`](../packages/subagent/subagent-inprocess) | | `workflow/agent-end` | `emit` | [`packages/workflow/workflow/src/index.ts:96`](../packages/workflow/workflow/src/index.ts) | [`workflow`](../packages/workflow/workflow) (`events.dispatch`) | - | | `workflow/agent-start` | `emit` | [`packages/workflow/workflow/src/index.ts:85`](../packages/workflow/workflow/src/index.ts) | [`workflow`](../packages/workflow/workflow) (`events.dispatch`) | - | | `workflow/end` | `emit` | [`packages/workflow/workflow/src/index.ts:106`](../packages/workflow/workflow/src/index.ts) | [`workflow`](../packages/workflow/workflow) (`events.dispatch`) | - | -| `workflow/log` | `emit` | [`packages/workflow/workflow/src/index.ts:77`](../packages/workflow/workflow/src/index.ts) | [`workflow`](../packages/workflow/workflow) (`events.dispatch`) | - | -| `workflow/phase` | `emit` | [`packages/workflow/workflow/src/index.ts:70`](../packages/workflow/workflow/src/index.ts) | [`workflow`](../packages/workflow/workflow) (`events.dispatch`) | - | -| `workflow/start` | `emit` | [`packages/workflow/workflow/src/index.ts:62`](../packages/workflow/workflow/src/index.ts) | [`workflow`](../packages/workflow/workflow) (`events.dispatch`) | - | +| `workflow/log` | `emit` | [`packages/workflow/workflow/src/index.ts:75`](../packages/workflow/workflow/src/index.ts) | [`workflow`](../packages/workflow/workflow) (`events.dispatch`) | - | +| `workflow/phase` | `emit` | [`packages/workflow/workflow/src/index.ts:68`](../packages/workflow/workflow/src/index.ts) | [`workflow`](../packages/workflow/workflow) (`events.dispatch`) | - | +| `workflow/start` | `emit` | [`packages/workflow/workflow/src/index.ts:60`](../packages/workflow/workflow/src/index.ts) | [`workflow`](../packages/workflow/workflow) (`events.dispatch`) | - | + +## Non-harness or undeclared event strings seen in package source + +| Event string | Dispatchers | Listeners | +| --- | --- | --- | +| `internal/dispatch` | - | [`invariants`](../packages/support/invariants) | Maintenance mode: hybrid generated: Cordis event declarations and most producer/listener edges are AST-scanned; dynamic dispatch sites are classified in `scripts/gen-doc-graphs.ts`. diff --git a/docs/glossary.md b/docs/glossary.md new file mode 100644 index 0000000000..81b9b4f84a --- /dev/null +++ b/docs/glossary.md @@ -0,0 +1,17 @@ +# Glossary + +Domain vocabulary for the DeepSeek Harness SDK uses one canonical term per concept. Terms link to their entries with standard Markdown anchors; implementation detail stays in package READMEs and RFCs. + +FIXME(glossary-completeness): Expand this glossary before the first release so it covers the SDK's other core and capability subsystems, not only agent scope. + +## agent-scope + +- **scope** — the unit of per-agent registration: a contribution (tool, prompt section, variable, restriction, listener) is either *global* (visible to every agent) or *scoped* (owned by exactly one [scope key](#scope-key)). Two levels, flat: scoped registrations do not inherit down to subagents; subtree behavior is expressed with [lineage](#lineage) data, never scope structure. +- **scope key** — the opaque identity a scope is keyed by, compared by object identity. The harness convention: a live agent is the key of its own scope. +- **agent context (`agent.ctx`)** — the agent's scoped context; registrations through it are scope-visible AND scope-lifetime (one fact drives both), and listeners on it participate in that agent's scope-filtered dispatches. Registry-subject events may remain deliberately unfiltered under their own event contracts. +- **scope carrier** — the `thisArg` a scope-filtered dispatch carries (built by `scopeTarget`); its filter admits untagged listeners plus the subject's own. A *subject-less* carrier (no key) admits untagged listeners only. +- **scoped dispatch** — the rule: an event about one agent's activity dispatches with that agent's carrier. Events about a registry itself (a tool was added) are *registry-subject* and stay unfiltered. +- **shadowing** — most-specific-wins name resolution: a scoped tool/section/variable replaces its same-named global twin for that scope alone. The per-agent persona and per-agent tool-variant mechanism. +- **restriction / scope-local registration** — a restriction (`tools.restrict`) filters the GLOBAL tool surface for one scope (compose by intersection); scope-local registrations are merged after that filter. A filtered-away global tool is absent from the prompt AND refuses execution, indistinguishably from a nonexistent one. +- **setup window** — the creation slot where a creator composes an agent's scoped world (`CreateAgentOptions.setup`): after the scope and agent object exist but before the agent or session is published, `agent/session-start` fires, or the first prompt is assembled. Setup registers; it never drives the agent. +- **lineage** — parent/child facts carried as data (`parentSession`, `subagentDepth`); never affects visibility. diff --git a/docs/module-graph.md b/docs/module-graph.md index 68c680c914..2acee6ffc5 100644 --- a/docs/module-graph.md +++ b/docs/module-graph.md @@ -20,6 +20,7 @@ flowchart TD pkg_agent["agent"] pkg_agent_core["agent-core"] pkg_agent_loop["agent-loop"] + pkg_scope["scope"] pkg_session["session"] pkg_system_prompt["system-prompt"] pkg_tools["tools"] @@ -27,6 +28,7 @@ flowchart TD subgraph group_bash["packages/bash"] pkg_bash["bash"] pkg_bash_local["bash-local"] + pkg_bash_sandbox["bash-sandbox"] pkg_tool_bash["tool-bash"] end subgraph group_fs["packages/fs"] @@ -35,6 +37,11 @@ flowchart TD pkg_fs_policy["fs-policy"] pkg_tool_fs["tool-fs"] end + subgraph group_skill["packages/skill"] + pkg_skill["skill"] + pkg_skill_local["skill-local"] + pkg_tool_skill["tool-skill"] + end subgraph group_compact["packages/compact"] pkg_compact["compact"] pkg_compact_basic["compact-basic"] @@ -87,6 +94,7 @@ flowchart TD pkg_app_boot["app-boot"] pkg_stdio_agent["stdio-agent"] pkg_tool_ask_user["tool-ask-user"] + pkg_user_approval["user-approval"] pkg_user_interaction["user-interaction"] end subgraph group_code_runtime["packages/code-runtime"] @@ -96,30 +104,40 @@ flowchart TD subgraph group_guard["packages/guard"] pkg_repeat_tool_guard["repeat-tool-guard"] end + subgraph group_sandbox["packages/sandbox"] + pkg_sandbox["sandbox"] + pkg_sandbox_local["sandbox-local"] + end subgraph group_workflow["packages/workflow"] pkg_tool_workflow["tool-workflow"] pkg_workflow["workflow"] pkg_workflow_workerthread["workflow-workerthread"] end pkg_llm --> pkg_brand - pkg_bash --> pkg_brand pkg_code_runtime_worker --> pkg_code_runtime pkg_llm_deepseek --> pkg_llm pkg_llm_pi_ai --> pkg_llm pkg_session --> pkg_brand pkg_session --> pkg_llm + pkg_session --> pkg_scope pkg_system_prompt --> pkg_llm - pkg_bash_local --> pkg_bash - pkg_bash_local --> pkg_timeout + pkg_system_prompt --> pkg_scope pkg_fs --> pkg_brand pkg_fs --> pkg_llm pkg_web --> pkg_llm + pkg_sandbox --> pkg_llm pkg_agent --> pkg_brand pkg_agent --> pkg_llm + pkg_agent --> pkg_scope pkg_agent --> pkg_session pkg_agent --> pkg_system_prompt + pkg_bash --> pkg_brand + pkg_bash --> pkg_sandbox + pkg_bash --> pkg_session pkg_fs_local --> pkg_fs pkg_fs_policy --> pkg_fs + pkg_skill_local --> pkg_fs + pkg_skill_local --> pkg_skill pkg_compact --> pkg_llm pkg_compact --> pkg_session pkg_web_fetch_local --> pkg_timeout @@ -127,34 +145,51 @@ flowchart TD pkg_web_search_deepseek --> pkg_web pkg_web_search_exa --> pkg_web pkg_web_search_perplexity --> pkg_web - pkg_hook_protocol --> pkg_bash - pkg_hook_protocol --> pkg_session pkg_session_persistence --> pkg_session pkg_llm_replay --> pkg_llm pkg_llm_replay --> pkg_session - pkg_tools --> pkg_agent - pkg_tools --> pkg_code_runtime - pkg_tools --> pkg_llm - pkg_tools --> pkg_session - pkg_tools --> pkg_system_prompt + pkg_sandbox_local --> pkg_llm + pkg_sandbox_local --> pkg_sandbox + pkg_bash_local --> pkg_bash + pkg_bash_local --> pkg_timeout pkg_compact_basic --> pkg_agent pkg_compact_basic --> pkg_compact pkg_compact_basic --> pkg_llm pkg_compact_basic --> pkg_session + pkg_hook_protocol --> pkg_bash + pkg_hook_protocol --> pkg_session pkg_session_persistence_jsonl --> pkg_session pkg_session_persistence_jsonl --> pkg_session_persistence pkg_session_persistence_sqlite --> pkg_session pkg_session_persistence_sqlite --> pkg_session_persistence pkg_invariants --> pkg_agent pkg_invariants --> pkg_llm + pkg_invariants --> pkg_scope pkg_invariants --> pkg_session + pkg_user_approval --> pkg_agent + pkg_user_approval --> pkg_brand + pkg_user_approval --> pkg_llm + pkg_user_approval --> pkg_scope + pkg_user_approval --> pkg_session + pkg_user_approval --> pkg_system_prompt pkg_user_interaction --> pkg_agent pkg_user_interaction --> pkg_llm pkg_workflow --> pkg_agent pkg_workflow --> pkg_brand pkg_workflow --> pkg_llm + pkg_tools --> pkg_agent + pkg_tools --> pkg_code_runtime + pkg_tools --> pkg_llm + pkg_tools --> pkg_scope + pkg_tools --> pkg_session + pkg_tools --> pkg_system_prompt + pkg_tools --> pkg_user_approval + pkg_bash_sandbox --> pkg_bash + pkg_bash_sandbox --> pkg_bash_local + pkg_bash_sandbox --> pkg_sandbox pkg_agent_loop --> pkg_agent pkg_agent_loop --> pkg_llm + pkg_agent_loop --> pkg_scope pkg_agent_loop --> pkg_session pkg_agent_loop --> pkg_session_persistence pkg_agent_loop --> pkg_system_prompt @@ -162,15 +197,22 @@ flowchart TD pkg_tool_bash --> pkg_agent pkg_tool_bash --> pkg_bash pkg_tool_bash --> pkg_llm + pkg_tool_bash --> pkg_sandbox pkg_tool_bash --> pkg_system_prompt pkg_tool_bash --> pkg_tools + pkg_tool_bash --> pkg_user_approval pkg_tool_fs --> pkg_fs pkg_tool_fs --> pkg_llm pkg_tool_fs --> pkg_session pkg_tool_fs --> pkg_system_prompt pkg_tool_fs --> pkg_tools + pkg_tool_skill --> pkg_agent + pkg_tool_skill --> pkg_llm + pkg_tool_skill --> pkg_skill + pkg_tool_skill --> pkg_tools pkg_subagent --> pkg_agent pkg_subagent --> pkg_llm + pkg_subagent --> pkg_scope pkg_subagent --> pkg_tools pkg_tool_web --> pkg_llm pkg_tool_web --> pkg_system_prompt @@ -182,6 +224,7 @@ flowchart TD pkg_tool_todo --> pkg_agent pkg_tool_todo --> pkg_session pkg_tool_todo --> pkg_tools + pkg_tool_cordis --> pkg_scope pkg_tool_cordis --> pkg_tools pkg_hooks_codex --> pkg_agent pkg_hooks_codex --> pkg_hook_protocol @@ -189,10 +232,13 @@ flowchart TD pkg_hooks_codex --> pkg_session pkg_hooks_codex --> pkg_tools pkg_acp --> pkg_agent + pkg_acp --> pkg_bash pkg_acp --> pkg_llm + pkg_acp --> pkg_sandbox pkg_acp --> pkg_session pkg_acp --> pkg_session_persistence pkg_acp --> pkg_tools + pkg_acp --> pkg_user_approval pkg_acp --> pkg_user_interaction pkg_tool_ask_user --> pkg_agent pkg_tool_ask_user --> pkg_tools @@ -209,8 +255,11 @@ flowchart TD pkg_agent_core --> pkg_invariants pkg_agent_core --> pkg_llm pkg_agent_core --> pkg_session + pkg_agent_core --> pkg_skill + pkg_agent_core --> pkg_skill_local pkg_agent_core --> pkg_system_prompt pkg_agent_core --> pkg_tool_bash + pkg_agent_core --> pkg_tool_skill pkg_agent_core --> pkg_tools pkg_subagent_acp --> pkg_agent pkg_subagent_acp --> pkg_llm @@ -238,6 +287,7 @@ flowchart TD pkg_workflow_workerthread --> pkg_agent pkg_workflow_workerthread --> pkg_brand pkg_workflow_workerthread --> pkg_llm + pkg_workflow_workerthread --> pkg_session pkg_workflow_workerthread --> pkg_subagent pkg_workflow_workerthread --> pkg_tools pkg_workflow_workerthread --> pkg_workflow @@ -268,58 +318,66 @@ flowchart TD | --- | --- | --- | | [`brand`](../packages/util/brand) | `util` | — | | [`timeout`](../packages/util/timeout) | `util` | — | +| [`scope`](../packages/core/scope) | `core` | — | +| [`skill`](../packages/skill/skill) | `skill` | — | | [`subagent-subprocess`](../packages/subagent/subagent-subprocess) | `subagent` | — | | [`acp-snapshot`](../packages/support/acp-snapshot) | `support` | — | | [`app-boot`](../packages/ui/app-boot) | `ui` | — | | [`code-runtime`](../packages/code-runtime/code-runtime) | `code-runtime` | — | | [`llm`](../packages/llm/llm) | `llm` | [`brand`](../packages/util/brand) | -| [`bash`](../packages/bash/bash) | `bash` | [`brand`](../packages/util/brand) | | [`code-runtime-worker`](../packages/code-runtime/code-runtime-worker) | `code-runtime` | [`code-runtime`](../packages/code-runtime/code-runtime) | | [`llm-deepseek`](../packages/llm/llm-deepseek) | `llm` | [`llm`](../packages/llm/llm) | | [`llm-pi-ai`](../packages/llm/llm-pi-ai) | `llm` | [`llm`](../packages/llm/llm) | -| [`session`](../packages/core/session) | `core` | [`brand`](../packages/util/brand), [`llm`](../packages/llm/llm) | -| [`system-prompt`](../packages/core/system-prompt) | `core` | [`llm`](../packages/llm/llm) | -| [`bash-local`](../packages/bash/bash-local) | `bash` | [`bash`](../packages/bash/bash), [`timeout`](../packages/util/timeout) | +| [`session`](../packages/core/session) | `core` | [`brand`](../packages/util/brand), [`llm`](../packages/llm/llm), [`scope`](../packages/core/scope) | +| [`system-prompt`](../packages/core/system-prompt) | `core` | [`llm`](../packages/llm/llm), [`scope`](../packages/core/scope) | | [`fs`](../packages/fs/fs) | `fs` | [`brand`](../packages/util/brand), [`llm`](../packages/llm/llm) | | [`web`](../packages/web/web) | `web` | [`llm`](../packages/llm/llm) | -| [`agent`](../packages/core/agent) | `core` | [`brand`](../packages/util/brand), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`system-prompt`](../packages/core/system-prompt) | +| [`sandbox`](../packages/sandbox/sandbox) | `sandbox` | [`llm`](../packages/llm/llm) | +| [`agent`](../packages/core/agent) | `core` | [`brand`](../packages/util/brand), [`llm`](../packages/llm/llm), [`scope`](../packages/core/scope), [`session`](../packages/core/session), [`system-prompt`](../packages/core/system-prompt) | +| [`bash`](../packages/bash/bash) | `bash` | [`brand`](../packages/util/brand), [`sandbox`](../packages/sandbox/sandbox), [`session`](../packages/core/session) | | [`fs-local`](../packages/fs/fs-local) | `fs` | [`fs`](../packages/fs/fs) | | [`fs-policy`](../packages/fs/fs-policy) | `fs` | [`fs`](../packages/fs/fs) | +| [`skill-local`](../packages/skill/skill-local) | `skill` | [`fs`](../packages/fs/fs), [`skill`](../packages/skill/skill) | | [`compact`](../packages/compact/compact) | `compact` | [`llm`](../packages/llm/llm), [`session`](../packages/core/session) | | [`web-fetch-local`](../packages/web/web-fetch-local) | `web` | [`timeout`](../packages/util/timeout), [`web`](../packages/web/web) | | [`web-search-deepseek`](../packages/web/web-search-deepseek) | `web` | [`web`](../packages/web/web) | | [`web-search-exa`](../packages/web/web-search-exa) | `web` | [`web`](../packages/web/web) | | [`web-search-perplexity`](../packages/web/web-search-perplexity) | `web` | [`web`](../packages/web/web) | -| [`hook-protocol`](../packages/hooks/hook-protocol) | `hooks` | [`bash`](../packages/bash/bash), [`session`](../packages/core/session) | | [`session-persistence`](../packages/session-persistence/session-persistence) | `session-persistence` | [`session`](../packages/core/session) | | [`llm-replay`](../packages/support/llm-replay) | `support` | [`llm`](../packages/llm/llm), [`session`](../packages/core/session) | -| [`tools`](../packages/core/tools) | `core` | [`agent`](../packages/core/agent), [`code-runtime`](../packages/code-runtime/code-runtime), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`system-prompt`](../packages/core/system-prompt) | +| [`sandbox-local`](../packages/sandbox/sandbox-local) | `sandbox` | [`llm`](../packages/llm/llm), [`sandbox`](../packages/sandbox/sandbox) | +| [`bash-local`](../packages/bash/bash-local) | `bash` | [`bash`](../packages/bash/bash), [`timeout`](../packages/util/timeout) | | [`compact-basic`](../packages/compact/compact-basic) | `compact` | [`agent`](../packages/core/agent), [`compact`](../packages/compact/compact), [`llm`](../packages/llm/llm), [`session`](../packages/core/session) | +| [`hook-protocol`](../packages/hooks/hook-protocol) | `hooks` | [`bash`](../packages/bash/bash), [`session`](../packages/core/session) | | [`session-persistence-jsonl`](../packages/session-persistence/session-persistence-jsonl) | `session-persistence` | [`session`](../packages/core/session), [`session-persistence`](../packages/session-persistence/session-persistence) | | [`session-persistence-sqlite`](../packages/session-persistence/session-persistence-sqlite) | `session-persistence` | [`session`](../packages/core/session), [`session-persistence`](../packages/session-persistence/session-persistence) | -| [`invariants`](../packages/support/invariants) | `support` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`session`](../packages/core/session) | +| [`invariants`](../packages/support/invariants) | `support` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`scope`](../packages/core/scope), [`session`](../packages/core/session) | +| [`user-approval`](../packages/ui/user-approval) | `ui` | [`agent`](../packages/core/agent), [`brand`](../packages/util/brand), [`llm`](../packages/llm/llm), [`scope`](../packages/core/scope), [`session`](../packages/core/session), [`system-prompt`](../packages/core/system-prompt) | | [`user-interaction`](../packages/ui/user-interaction) | `ui` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm) | | [`workflow`](../packages/workflow/workflow) | `workflow` | [`agent`](../packages/core/agent), [`brand`](../packages/util/brand), [`llm`](../packages/llm/llm) | -| [`agent-loop`](../packages/core/agent-loop) | `core` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`session-persistence`](../packages/session-persistence/session-persistence), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools) | -| [`tool-bash`](../packages/bash/tool-bash) | `bash` | [`agent`](../packages/core/agent), [`bash`](../packages/bash/bash), [`llm`](../packages/llm/llm), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools) | +| [`tools`](../packages/core/tools) | `core` | [`agent`](../packages/core/agent), [`code-runtime`](../packages/code-runtime/code-runtime), [`llm`](../packages/llm/llm), [`scope`](../packages/core/scope), [`session`](../packages/core/session), [`system-prompt`](../packages/core/system-prompt), [`user-approval`](../packages/ui/user-approval) | +| [`bash-sandbox`](../packages/bash/bash-sandbox) | `bash` | [`bash`](../packages/bash/bash), [`bash-local`](../packages/bash/bash-local), [`sandbox`](../packages/sandbox/sandbox) | +| [`agent-loop`](../packages/core/agent-loop) | `core` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`scope`](../packages/core/scope), [`session`](../packages/core/session), [`session-persistence`](../packages/session-persistence/session-persistence), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools) | +| [`tool-bash`](../packages/bash/tool-bash) | `bash` | [`agent`](../packages/core/agent), [`bash`](../packages/bash/bash), [`llm`](../packages/llm/llm), [`sandbox`](../packages/sandbox/sandbox), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools), [`user-approval`](../packages/ui/user-approval) | | [`tool-fs`](../packages/fs/tool-fs) | `fs` | [`fs`](../packages/fs/fs), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools) | -| [`subagent`](../packages/subagent/subagent) | `subagent` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`tools`](../packages/core/tools) | +| [`tool-skill`](../packages/skill/tool-skill) | `skill` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`skill`](../packages/skill/skill), [`tools`](../packages/core/tools) | +| [`subagent`](../packages/subagent/subagent) | `subagent` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`scope`](../packages/core/scope), [`tools`](../packages/core/tools) | | [`tool-web`](../packages/web/tool-web) | `web` | [`llm`](../packages/llm/llm), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools), [`web`](../packages/web/web) | | [`timeout-policy`](../packages/timeout/timeout-policy) | `timeout` | [`llm`](../packages/llm/llm), [`timeout`](../packages/util/timeout), [`tools`](../packages/core/tools) | | [`tool-todo`](../packages/todo/tool-todo) | `todo` | [`agent`](../packages/core/agent), [`session`](../packages/core/session), [`tools`](../packages/core/tools) | -| [`tool-cordis`](../packages/cordis/tool-cordis) | `cordis` | [`tools`](../packages/core/tools) | +| [`tool-cordis`](../packages/cordis/tool-cordis) | `cordis` | [`scope`](../packages/core/scope), [`tools`](../packages/core/tools) | | [`hooks-codex`](../packages/hooks/hooks-codex) | `hooks` | [`agent`](../packages/core/agent), [`hook-protocol`](../packages/hooks/hook-protocol), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`tools`](../packages/core/tools) | -| [`acp`](../packages/ui/acp) | `ui` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`session-persistence`](../packages/session-persistence/session-persistence), [`tools`](../packages/core/tools), [`user-interaction`](../packages/ui/user-interaction) | +| [`acp`](../packages/ui/acp) | `ui` | [`agent`](../packages/core/agent), [`bash`](../packages/bash/bash), [`llm`](../packages/llm/llm), [`sandbox`](../packages/sandbox/sandbox), [`session`](../packages/core/session), [`session-persistence`](../packages/session-persistence/session-persistence), [`tools`](../packages/core/tools), [`user-approval`](../packages/ui/user-approval), [`user-interaction`](../packages/ui/user-interaction) | | [`tool-ask-user`](../packages/ui/tool-ask-user) | `ui` | [`agent`](../packages/core/agent), [`tools`](../packages/core/tools), [`user-interaction`](../packages/ui/user-interaction) | | [`repeat-tool-guard`](../packages/guard/repeat-tool-guard) | `guard` | [`agent`](../packages/core/agent), [`tools`](../packages/core/tools) | | [`tool-workflow`](../packages/workflow/tool-workflow) | `workflow` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools), [`workflow`](../packages/workflow/workflow) | -| [`agent-core`](../packages/core/agent-core) | `core` | [`agent`](../packages/core/agent), [`agent-loop`](../packages/core/agent-loop), [`invariants`](../packages/support/invariants), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`system-prompt`](../packages/core/system-prompt), [`tool-bash`](../packages/bash/tool-bash), [`tools`](../packages/core/tools) | +| [`agent-core`](../packages/core/agent-core) | `core` | [`agent`](../packages/core/agent), [`agent-loop`](../packages/core/agent-loop), [`invariants`](../packages/support/invariants), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`skill`](../packages/skill/skill), [`skill-local`](../packages/skill/skill-local), [`system-prompt`](../packages/core/system-prompt), [`tool-bash`](../packages/bash/tool-bash), [`tool-skill`](../packages/skill/tool-skill), [`tools`](../packages/core/tools) | | [`subagent-acp`](../packages/subagent/subagent-acp) | `subagent` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`subagent`](../packages/subagent/subagent), [`subagent-subprocess`](../packages/subagent/subagent-subprocess) | | [`subagent-inprocess`](../packages/subagent/subagent-inprocess) | `subagent` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`subagent`](../packages/subagent/subagent), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools) | | [`tool-subagent`](../packages/subagent/tool-subagent) | `subagent` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`subagent`](../packages/subagent/subagent), [`tools`](../packages/core/tools) | | [`hooks-claude`](../packages/hooks/hooks-claude) | `hooks` | [`agent`](../packages/core/agent), [`hook-protocol`](../packages/hooks/hook-protocol), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`subagent`](../packages/subagent/subagent), [`tools`](../packages/core/tools) | | [`subagent-mock`](../packages/support/subagent-mock) | `support` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`subagent`](../packages/subagent/subagent) | -| [`workflow-workerthread`](../packages/workflow/workflow-workerthread) | `workflow` | [`agent`](../packages/core/agent), [`brand`](../packages/util/brand), [`llm`](../packages/llm/llm), [`subagent`](../packages/subagent/subagent), [`tools`](../packages/core/tools), [`workflow`](../packages/workflow/workflow) | +| [`workflow-workerthread`](../packages/workflow/workflow-workerthread) | `workflow` | [`agent`](../packages/core/agent), [`brand`](../packages/util/brand), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`subagent`](../packages/subagent/subagent), [`tools`](../packages/core/tools), [`workflow`](../packages/workflow/workflow) | | [`subagent-fork`](../packages/subagent/subagent-fork) | `subagent` | [`agent`](../packages/core/agent), [`session`](../packages/core/session), [`subagent`](../packages/subagent/subagent), [`subagent-inprocess`](../packages/subagent/subagent-inprocess) | | [`subagent-spawn`](../packages/subagent/subagent-spawn) | `subagent` | [`subagent`](../packages/subagent/subagent), [`subagent-inprocess`](../packages/subagent/subagent-inprocess) | | [`acp-agent`](../packages/ui/acp-agent) | `ui` | [`acp`](../packages/ui/acp), [`agent-core`](../packages/core/agent-core), [`app-boot`](../packages/ui/app-boot), [`session-persistence-jsonl`](../packages/session-persistence/session-persistence-jsonl), [`tools`](../packages/core/tools), [`user-interaction`](../packages/ui/user-interaction) | diff --git a/docs/persistence-catalog.md b/docs/persistence-catalog.md index 917386af9b..245d25fa5f 100644 --- a/docs/persistence-catalog.md +++ b/docs/persistence-catalog.md @@ -11,6 +11,40 @@ The on-disk envelope around every payload is `SessionEvent` — `type`, monotoni ## Events +### `approval/*` + +#### `approval/asked` — log-only + +An approval question was put to the answerer chain — log-only audit (like `hook/*`; NOT a surface event, carries no `surfaceOp`). `id` pairs it with the `approval/decided` that always follows; `toolName` is the tool the question is about, `callId` the exact tool call when the asker had one, `reason` the asker's human-readable explanation (e.g. a hook's permission-decision reason). + +```ts persistence-catalog +'approval/asked': { id: ApprovalRequestId; toolName: string; callId?: CallId; reason?: string } +``` + +Types: [CallId](core-data-structures/core.md) + +Source: [`packages/ui/user-approval/src/index.ts:84`](../packages/ui/user-approval/src/index.ts) + +#### `approval/decided` — log-only + +The outcome of a prior `approval/asked` (same `id`) — log-only audit. Exactly one per ask, appended when the outcome is known: a decision, a cancellation, or the fail-closed `'unavailable'`. + +```ts persistence-catalog +'approval/decided': { id: ApprovalRequestId; outcome: ApprovalOutcome } +``` + +Source: [`packages/ui/user-approval/src/index.ts:95`](../packages/ui/user-approval/src/index.ts) + +#### `approval/policy` — log-only + +The session's approval policy was switched — log-only, durable, replayable, never in the model transcript (the model learns the policy from the prompt section and the narrator's notices). The LAST such event is the session's override (effectiveApprovalPolicy); who asked for it is derivable from position (an event after the log's last `request/header*` was a runtime switch by the user). + +```ts persistence-catalog +'approval/policy': { policy: ApprovalPolicy } +``` + +Source: [`packages/ui/user-approval/src/index.ts:107`](../packages/ui/user-approval/src/index.ts) + ### `assistant/*` #### `assistant/chunk` — log-only @@ -23,7 +57,7 @@ Raw stream chunk — token-level replay fidelity. Types: [StreamChunk](core-data-structures/llm-streaming.md) -Source: [`packages/core/session/src/types.ts:313`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:322`](../packages/core/session/src/types.ts) #### `assistant/message` — surface @@ -35,7 +69,19 @@ Assembled assistant message for one step (derived history uses this). Carries th Types: [ContentBlock](core-data-structures/core.md) · [TokenUsage](core-data-structures/llm-streaming.md) -Source: [`packages/core/session/src/types.ts:320`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:329`](../packages/core/session/src/types.ts) + +### `bash/*` + +#### `bash/sandbox-mode` — log-only + +The session's sandbox mode was switched — log-only (like `approval/*`; NOT a surface event, carries no `surfaceOp`): durable and replayable, never in the model transcript. The LAST such event is the session's override (effectiveSandboxMode); who asked for it is derivable from position (an event after the log's last `request/header*` was a runtime switch by the user; see the tool layer's narrator). + +```ts persistence-catalog +'bash/sandbox-mode': { mode: SandboxMode } +``` + +Source: [`packages/bash/bash/src/session-mode.ts:31`](../packages/bash/bash/src/session-mode.ts) ### `compact/*` @@ -83,7 +129,7 @@ In-session context injection (file-change notices, subdir AGENTS.md, skill conte Types: [ContentBlock](core-data-structures/core.md) · [MessageSource](core-data-structures/core.md) -Source: [`packages/core/session/src/types.ts:311`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:320`](../packages/core/session/src/types.ts) ### `hook/*` @@ -119,7 +165,7 @@ A queued prompt an `agent/prompt-submit` listener VETOED — the durable record Types: [ContentBlock](core-data-structures/core.md) · [MessageSource](core-data-structures/core.md) -Source: [`packages/core/session/src/types.ts:305`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:314`](../packages/core/session/src/types.ts) ### `request/*` @@ -131,7 +177,7 @@ Full snapshot of the EpochHeader the NEXT request is built under, with the Reque 'request/header': { header: EpochHeader; reason: RequestHeaderReason } ``` -Source: [`packages/core/session/src/types.ts:365`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:374`](../packages/core/session/src/types.ts) #### `request/header-delta` — log-only @@ -141,7 +187,7 @@ Amendment to the folded EpochHeader: at least one of a SystemDelta, a ToolsDelta 'request/header-delta': { system?: SystemDelta; tools?: ToolsDelta; config?: LlmCallConfig; messagePrefix?: Message[] } ``` -Source: [`packages/core/session/src/types.ts:382`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:391`](../packages/core/session/src/types.ts) ### `steering/*` @@ -155,7 +201,7 @@ Steering content injected between steps of a running turn. Types: [ContentBlock](core-data-structures/core.md) · [MessageSource](core-data-structures/core.md) -Source: [`packages/core/session/src/types.ts:338`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:347`](../packages/core/session/src/types.ts) ### `step/*` @@ -167,7 +213,7 @@ Closes step `step` of turn `turn`. 'step/end': { turn: number; step: number } ``` -Source: [`packages/core/session/src/types.ts:292`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:301`](../packages/core/session/src/types.ts) #### `step/start` — log-only @@ -177,7 +223,7 @@ Opens step `step` of turn `turn` — one model call plus the tool executions it 'step/start': { turn: number; step: number } ``` -Source: [`packages/core/session/src/types.ts:290`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:299`](../packages/core/session/src/types.ts) ### `todo/*` @@ -193,7 +239,7 @@ NOT a SurfaceEventType: it produces no LLM message and never reaches `deriveMess Types: [TodoItem](core-data-structures/session.md) -Source: [`packages/core/session/src/types.ts:352`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:361`](../packages/core/session/src/types.ts) ### `tool/*` @@ -207,7 +253,7 @@ The model requested one tool invocation: `name` with the raw `arguments` JSON st Types: [CallId](core-data-structures/core.md) -Source: [`packages/core/session/src/types.ts:326`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:335`](../packages/core/session/src/types.ts) #### `tool/code-dispatch` — log-only @@ -219,7 +265,7 @@ One bridged sub-dispatch from a `run_code` program: the parent `run_code` call i Types: [CallId](core-data-structures/core.md) -Source: [`packages/core/tools/src/code-mode.ts:36`](../packages/core/tools/src/code-mode.ts) +Source: [`packages/core/tools/src/code-mode.ts:38`](../packages/core/tools/src/code-mode.ts) #### `tool/result` — surface @@ -231,7 +277,7 @@ A completed tool call's model-facing result, plus an optional tool-private `meta Types: [CallId](core-data-structures/core.md) · [ContentBlock](core-data-structures/core.md) -Source: [`packages/core/session/src/types.ts:336`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:345`](../packages/core/session/src/types.ts) ### `turn/*` @@ -245,7 +291,7 @@ Closes turn `turn` with the TurnEndReason that ended it. The loop fires the awai Types: [TurnEndReason](core-data-structures/session.md) -Source: [`packages/core/session/src/types.ts:288`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:297`](../packages/core/session/src/types.ts) #### `turn/start` — log-only @@ -257,7 +303,7 @@ Opens turn `turn`. `trigger` records what started it — a drained message batch Types: [TurnTrigger](core-data-structures/session.md) -Source: [`packages/core/session/src/types.ts:282`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:291`](../packages/core/session/src/types.ts) ### `user/*` @@ -271,4 +317,4 @@ A user-visible prompt (queued message drained at turn start). Types: [ContentBlock](core-data-structures/core.md) · [MessageSource](core-data-structures/core.md) -Source: [`packages/core/session/src/types.ts:294`](../packages/core/session/src/types.ts) +Source: [`packages/core/session/src/types.ts:303`](../packages/core/session/src/types.ts) diff --git a/docs/rfc/INDEX.md b/docs/rfc/INDEX.md index 85fff2419d..98a055d0bd 100644 --- a/docs/rfc/INDEX.md +++ b/docs/rfc/INDEX.md @@ -8,8 +8,6 @@ Generated by `pnpm run gen-rfc-index` from the RFC tree — never edit by hand; | Title | First proposed | |---|---| -| [Agent Client Protocol (ACP) support — drive the coding agent from external editors](proposed/feature/2026-06-14-acp-agent-client-protocol.md) | 2026-06-14 | -| [Multiplex concurrent ACP sessions over one connection](proposed/feature/2026-06-14-acp-multi-session.md) | 2026-06-14 | | [Pre-tool input rewrite — a consistent design](proposed/feature/2026-06-30-pre-tool-input-rewrite.md) | 2026-06-30 | | [Claude Code and Codex subagent backends (out-of-process delegation to external coding agents)](proposed/feature/2026-07-07-claude-code-and-codex-subagent-backends.md) | 2026-07-07 | | [Interactive side sessions and merge-back](proposed/feature/2026-07-08-interactive-side-sessions.md) | 2026-07-08 | @@ -50,6 +48,8 @@ Generated by `pnpm run gen-rfc-index` from the RFC tree — never edit by hand; | Title | First proposed | |---|---| +| [Agent Client Protocol (ACP) support — drive the coding agent from external editors](implemented/feature/2026-06-14-acp-agent-client-protocol.md) | 2026-06-14 | +| [Multiplex concurrent ACP sessions over one connection](implemented/feature/2026-06-14-acp-multi-session.md) | 2026-06-14 | | [Code Mode — the model writes TypeScript against the tool registry](implemented/feature/2026-06-15-code-mode.md) | 2026-06-15 | | [Filesystem tool schemas — model-facing read/write/edit shapes](implemented/feature/2026-06-17-filesystem-tool-schemas.md) | 2026-06-17 | | [Rich ACP bash rendering — the terminal card via the `_meta` convention](implemented/feature/2026-06-18-acp-terminal-and-tool-rendering.md) | 2026-06-18 | @@ -64,10 +64,14 @@ Generated by `pnpm run gen-rfc-index` from the RFC tree — never edit by hand; | [SessionStore fork API](implemented/feature/2026-06-30-session-store-fork-api.md) | 2026-06-30 | | [Subagent lifecycle enrichment — lastAssistantMessage (observe-only)](implemented/feature/2026-06-30-subagent-observe-enrich.md) | 2026-06-30 | | [Dynamic workflows — a script-driven multi-agent orchestration seam](implemented/feature/2026-07-05-dynamic-workflows.md) | 2026-07-05 | +| [Skill system — progressive disclosure instructions for agents](implemented/feature/2026-07-05-skill-system.md) | 2026-07-05 | +| [The approval seam — one-shot permission decisions over a waterfall of answerers](implemented/feature/2026-07-06-approval-seam.md) | 2026-07-06 | | [Explicit model-facing tool order](implemented/feature/2026-07-06-explicit-tool-order.md) | 2026-07-06 | +| [The subprocess sandbox — confinement seam, native runners, escalation, and per-session modes](implemented/feature/2026-07-06-sandbox.md) | 2026-07-06 | | [The session prefix — request-only messages in front of the derived history](implemented/feature/2026-07-07-session-prefix.md) | 2026-07-07 | | [Repeat-tool-call guard plugin](implemented/feature/2026-07-08-repeat-tool-guard.md) | 2026-07-08 | | [The self-referential cordis toolset](implemented/feature/2026-07-08-self-referential-cordis-toolset.md) | 2026-07-08 | +| [Configure subagent persona, tool visibility, and depth](implemented/feature/2026-07-12-subagent-persona-tool-filter-and-depth.md) | 2026-07-12 | ### Simplification @@ -99,7 +103,7 @@ Generated by `pnpm run gen-rfc-index` from the RFC tree — never edit by hand; |---|---| | [Provider-neutral content-block vocabulary owned by dsh-llm](implemented/architecture/2026-06-11-content-block-vocabulary.md) | 2026-06-11 | | [Custom typed tool-schema DSL instead of schemastery](implemented/architecture/2026-06-11-custom-schema-dsl.md) | 2026-06-11 | -| [Dev-mode invariants over compile-time deep-readonly](implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md) | 2026-06-11 | +| [Source-owned session immutability and dev-mode invariants](implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md) | 2026-06-11 | | [Event-sourced sessions with derived message history](implemented/architecture/2026-06-11-event-sourced-sessions.md) | 2026-06-11 | | [Microkernel — extension via Cordis event taxonomy, one concrete loop](implemented/architecture/2026-06-11-microkernel-event-taxonomy.md) | 2026-06-11 | | [Runtime arg validation at the model boundary](implemented/architecture/2026-06-11-runtime-arg-validation.md) | 2026-06-11 | @@ -130,6 +134,8 @@ Generated by `pnpm run gen-rfc-index` from the RFC tree — never edit by hand; | [Subagent provider-lifecycle events — `subagent/provider-added` / `subagent/provider-removed`](implemented/architecture/2026-07-05-subagent-provider-lifecycle-events.md) | 2026-07-05 | | [A shared timeout/deadline primitive, with hard-kill left to each capability](implemented/architecture/2026-07-06-timeout-deadline-library.md) | 2026-07-06 | | [Tool-call timeout policy as a plugin](implemented/architecture/2026-07-07-tool-call-timeout-policy.md) | 2026-07-07 | +| [The agent is a registration scope](implemented/architecture/2026-07-08-agent-scope-contexts.md) | 2026-07-08 | +| [Agent-scope runtime design and correctness](implemented/architecture/2026-07-12-agent-scope-runtime-design.md) | 2026-07-12 | ### Process diff --git a/docs/rfc/implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md b/docs/rfc/implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md index a3240153d0..aac3991f46 100644 --- a/docs/rfc/implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md +++ b/docs/rfc/implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md @@ -1,29 +1,58 @@ -# RFC: Dev-mode invariants over compile-time deep-readonly +# RFC: Source-owned session immutability and dev-mode invariants Status: implemented ## Problem -The session log is append-only by contract, but the types don't enforce it: `session.events` returns `readonly SessionEvent[]` whose *elements* are mutable, and `deriveMessages()` handed the logged `content` arrays/blocks out by reference. The loop then passes those derived messages into the `agent/request` waterfall and on to adapters, where mutating the request is sanctioned — so a request middleware could reach back and rewrite history, silently breaking replay equivalence and the derived-history guarantee. Separately, the event taxonomy (turn/step nesting, seq monotonicity, tool-call/result pairing, legal status transitions) was asserted only where individual tests happened to look. +The session log needs two different protections: immutable ownership of each stored fact, and checks for relationships among facts across time and service seams. Conflating them in an optional development plugin would leave production history vulnerable; trying to express both through TypeScript readonly types would not create a runtime boundary or describe relational rules. -Two ways to defend the log: make immutability part of the type (`DeepReadonly` on the way out), or catch corruption at runtime in dev. The runtime-validation proposal took the runtime route; [the deep-readonly proposal](../../rejected/architecture/2026-06-11-immutable-public-surfaces.md) took the type route. +The session log is the durable source of truth for replay, request reconstruction, persistence, and user-visible history. Code outside the session package must be able to inspect that history without retaining a reference that can rewrite it later, and inputs accepted from callers must not remain connected to caller-owned mutable objects. + +Immutability of individual values is only half of the contract. A log can contain perfectly immutable records whose sequence, turn/step nesting, tool-call pairing, scoped delivery, or reconstructed model request is wrong. Those rules relate multiple records or services and cannot be established by freezing one object. + +TypeScript readonly types are not a sufficient runtime boundary. They disappear when the program runs, a cast can bypass them, and a recursive `DeepReadonly` would spread through every log and message consumer even though some downstream request-processing APIs intentionally work with mutable values. ## Decision -Reject the pervasive `DeepReadonly` type flip. Instead: +Responsibility is split between an always-on storage boundary and optional development assertions. -1. **Always-on:** `deriveMessages()` deep-clones the content it emits (one `structuredClone` per derived message). In-flight mutation of a request can no longer reach the log — this is the real fix, and it costs nothing meaningful next to a model call. -2. **Dev-mode:** a new `dsh-invariants` plugin (pure listeners, off in production, on in tests and demos) asserts the event contract and `Object.freeze`s logged event data so any *other* code that mutates a logged event throws instead of corrupting silently. Seeded sessions are frozen and checked on `session/created` (the constructor copies the seed without emitting `session/event`). +### Session owns immutable history -The invariants encode the *real* contract, not an idealized one: a `tool/call` may have no `tool/result` (a thrown tool-execution pipeline step ends the turn), and both `idle→disposed` and `running→disposed` are legal. +`Session` accepts an event only after one recursive pass has materialized a lossless JSON snapshot. That pass rejects unsupported values and produces the exact detached record that enters the log, so validation and storage cannot observe different values from a stateful getter or retain caller-owned nested references. + +The accepted event and all of its descendants are deep-frozen before publication. `append()` returns that owned frozen event, `session/event` observers receive the same record, and `session.events` returns a frozen array snapshot. A previously returned array does not grow after a later append. Seed records pass through the same validation, snapshot, and freeze boundary before construction succeeds. + +This guarantee belongs in `Session`, not in an optional listener, because every composition relies on trustworthy history. A production deployment, a focused test, or a custom embedding receives the same storage semantics whether or not development support plugins are registered. + +### Derived requests remain detached + +`deriveMessages()` projects logged surface events into detached, deep-frozen `Message` objects and returns a fresh array snapshot. Request assembly can therefore combine derived history with other inputs without exposing a path back into the log. The cache reuses safe immutable projections rather than recloning the complete history for each model call. + +### The invariants plugin checks relationships + +`dsh-invariants` is a pure-listener development plugin. It does not freeze records and has no configuration; disposal removes only its assertions. It checks rules that require trace state or observation of another seam, including monotonic sequence numbers, turn and step nesting, tool-call/result pairing, legal agent-status transitions, subject-correct scoped dispatch, and equality between a loop-built request and the request reconstructed from its session-log prefix. + +When the plugin attaches to an existing or seeded session, it replays the immutable log to rebuild trace state. This makes hot reload safe in the middle of a turn without giving the plugin ownership of session storage. ## Alternatives considered -**The pervasive `DeepReadonly` type flip** ([the rejected proposal](../../rejected/architecture/2026-06-11-immutable-public-surfaces.md)) — compile-time only (a plugin casts straight through it), high type-noise across every log/message consumer and adapter, and it would force readonly types through code where mutation is the sanctioned API. The clone draws the mutable/immutable boundary exactly at "logged vs in-flight" without any of that noise. +### Pervasive deep-readonly types + +[The rejected immutable-public-surfaces proposal](../../rejected/architecture/2026-06-11-immutable-public-surfaces.md) would apply a recursive readonly type across public log and message surfaces. That provides editor feedback but not a runtime guarantee: TypeScript types are erased and plugin code can cast through them. It also pushes readonly types into consumers where mutation is intentional. Runtime ownership at the `Session` boundary protects every caller without that type propagation. + +### Development-only freezing + +Freezing history only when an invariants plugin is installed would make the core guarantee composition-dependent. Code could pass development tests and still corrupt history in production or in a focused composition that omits the plugin. Storage immutability is therefore always on, while the more expensive relational checks remain opt-in development support. + +### Clone only when deriving messages + +Detaching `deriveMessages()` would protect the most common request path but leave other readers of `session.events`, append return values, and session-event observers able to mutate durable history. The log must protect its own boundary; derived projections are an additional isolation boundary, not a substitute. ## Consequences -- History corruption is caught loudly in tests and demos, at zero production cost and zero type noise. The trade-off is that the guarantee is dynamic (a dev-mode tripwire) rather than static. -- The invariants plugin doubles as executable documentation of the event taxonomy — the assertions are the contract. -- `Session.events` keeps its `readonly SessionEvent[]` type; no consumer churn. -- This folds in [the deep-readonly proposal](../../rejected/architecture/2026-06-11-immutable-public-surfaces.md) — there is no separate deep-readonly record; this records the decision to *not* pursue that approach. `InvariantError` is a plain `Error` with a `code` for now; a later taxonomy change can promote it. +- Every accepted live or seeded session event is detached from caller-owned inputs and deeply immutable before any observer can receive it. +- `session.events` exposes stable immutable snapshots instead of the private growing array. +- Request-side mutation cannot reach stored history through derived messages. +- Development builds can enable relational assertions without changing storage behavior, and disposing or omitting the plugin does not weaken log immutability. +- `dsh-invariants` has no `Config` surface because it has no behavior to tune. +- The runtime boundary carries a recursive snapshot-and-freeze cost once per accepted event; later readers and cached projections reuse the owned immutable records. diff --git a/docs/rfc/implemented/architecture/2026-06-11-microkernel-event-taxonomy.md b/docs/rfc/implemented/architecture/2026-06-11-microkernel-event-taxonomy.md index 501f8c7331..8293924d37 100644 --- a/docs/rfc/implemented/architecture/2026-06-11-microkernel-event-taxonomy.md +++ b/docs/rfc/implemented/architecture/2026-06-11-microkernel-event-taxonomy.md @@ -10,9 +10,10 @@ The product principle is "everything is a plugin": hooks, /goal, /loop, dynamic Pure Cordis event taxonomy. The loop's extension seams are typed events with deliberate dispatch modes: -- **waterfall** (around-middleware) where plugins mutate or veto: `agent/prompt-submit`, `agent/request`, `agent/step-result`, `agent/turn-continuation`, `tools/pre-execute`, `tools/post-execute`, `llm/stream`, `system-prompt/assemble`. -- **emit** (sync fire-and-forget) for notifications: turn/step boundaries, stream chunks, lifecycle, errors. -- **parallel** (awaited) for the one durability checkpoint: `session/flush`. +- **waterfall** (around-middleware) where plugins transform, veto, or wrap: `agent/prompt-submit`, `agent/request`, `agent/step-result`, `agent/turn-continuation`, `tools/pre-execute`, `tools/execute`, `tools/post-execute`, `llm/stream`, `system-prompt/assemble`. +- **serial** (awaited in listener order; a bail value stops later listeners) for ordered checkpoints: every `agent/pre-step` listener runs when all abstain, while the first stop returned from `agent/turn-stop` makes the terminal decision final. +- **parallel** (awaited fan-out) where every listener must get an independent chance: the `session/flush` durability checkpoint. +- **emit** (synchronous fire-and-forget) for notifications: turn/step boundaries, stream chunks, lifecycle, errors, and the contained immutable `tools/result` observation. The event vocabulary lives in interface packages (dsh-agent declares the agent/* events); `@deepseek-ai/dsh-agent-loop` is the only concrete loop plugin and is itself swappable — nothing outside it may depend on it. diff --git a/docs/rfc/implemented/architecture/2026-06-14-session-persistence.md b/docs/rfc/implemented/architecture/2026-06-14-session-persistence.md index 135c1671f6..03d8a30b37 100644 --- a/docs/rfc/implemented/architecture/2026-06-14-session-persistence.md +++ b/docs/rfc/implemented/architecture/2026-06-14-session-persistence.md @@ -6,7 +6,7 @@ Status: implemented ## Problem -Sessions lived only in memory. The example `session-jsonl.ts` plugin (duplicated byte-for-byte in both examples) was write-only telemetry: it buffered `session/event` and appended JSON lines, with no read/replay path, no crash-safety (no fsync, no atomic write, a fire-and-forget dispose drain), no listing, and no format versioning. Nothing could rehydrate a past session from disk into a live agent, so durable resume ("continue yesterday's task"), durable forking, and the ACP `session/load` method ([ACP support](../../proposed/feature/2026-06-14-acp-agent-client-protocol.md)) were all impossible. +Sessions lived only in memory. The example `session-jsonl.ts` plugin (duplicated byte-for-byte in both examples) was write-only telemetry: it buffered `session/event` and appended JSON lines, with no read/replay path, no crash-safety (no fsync, no atomic write, a fire-and-forget dispose drain), no listing, and no format versioning. Nothing could rehydrate a past session from disk into a live agent, so durable resume ("continue yesterday's task"), durable forking, and the ACP `session/load` method ([ACP support](../../implemented/feature/2026-06-14-acp-agent-client-protocol.md)) were all impossible. The [event-sourced model](2026-06-11-event-sourced-sessions.md) makes the append-only log the single source of truth and derives LLM history from it. Persistence had to stay faithful to that: persist the existing `SessionEvent` directly, with no parallel "persisted message" type that the log is converted to and from. The backend also had to be swappable — a file store now, a database store later — behind one interface. @@ -23,7 +23,7 @@ Key choices recorded here because they are durable, contested, and surprising: - **Append-only; a crashed turn is closed, never truncated.** Committed events — those at or below a flushed `turn/end` — are never rewritten. The loop only flushes at `turn/end`, so a crash can leave a durable log whose final turn never closed: real, fully-written events sit after the last `turn/end`. **A single turn can be huge in a long-horizon task** (many steps, large tool output spanning a long autonomous run), so discarding the interrupted turn would silently destroy a large amount of real work — truncating a turn is wrong. Instead, on reload `load` PRESERVES those events and CLOSES the orphaned turn by durably appending the minimal synthetic boundary events: an error `tool/result` for every `tool-call` the crash left unanswered, then a `step/end` if a step was still open, then a `turn/end` carrying the merge-extensible `{ kind: 'interrupted' }` reason (a marker that records the turn was cut short by a crash, not completed by the model — no loop ever emits it). The synthetic tool results matter for resume correctness: the loop logs the `assistant/message` (carrying the `tool-call` blocks) BEFORE running the tools, so a crash mid-tool leaves calls without results; `deriveMessages()` would then replay a dangling assistant tool-call, which every provider rejects as an invalid transcript on the next request. Answering each orphaned call with an error result keeps the rehydrated history valid. `load` returns the balanced log, so a resumed session is immediately usable. The ONLY thing discarded is a never-fully-written **torn tail fragment** — a final record whose bytes (JSONL) or row were never completely flushed; that fragment is not a valid event and is dropped before the synthetic closers are written. A parse error or `seq` gap in the COMMITTED region (at or before the last real `turn/end`) is genuine corruption and makes the session unloadable. - **File backend canonical, DB backend a proven drop-in.** `SessionEvent` maps 1:1 onto a row `(session_id, seq, type, time, data)` — `append` is INSERT (in a transaction asserting the contiguous-seq contract), `load` is SELECT … ORDER BY seq. `dsh-session-persistence-sqlite` is exactly this: a `SessionPersistence` subclass with no interface change (opencode runs this exact shape on SQLite/WAL), and it passes the same `runPersistenceContract` suite as the JSONL backend — so the contract holds both backends to identical semantics (lazy materialization, interrupted-turn close on load, contiguous-seq), expressed once over file bytes and once over rows. - **Metadata is out-of-log.** Format version, cwd, and lineage are storage concerns, not replayable conversation state, so they live in a `SessionHeader` owned by `dsh-session` and attached to a `Session` via a new readonly `session.header` — never in `SessionEventMap`, never reaching `deriveMessages()`. The alternative (a merge-extensible `session/meta` event as log line 0) was rejected: an in-log event would ride along with a seeded/forked session for free, but metadata is not replayable state, so the explicit out-of-log header seam is the cleaner cost. (The header was originally split into an immutable `SessionHeader` plus a mutable `SessionSummary` whose union was `SessionMeta`; the mutable summary was later removed as dead state — see [Drop the mutable session summary](../simplification/2026-06-19-drop-mutable-session-summary.md).) -- **Resume is an async factory, not a change to synchronous create.** `ctx.agents.resume({ resumeSessionId })` awaits `ctx.sessionPersistence.load`, recreates the live session with the loaded events (so `lastTurnNumber`/`deriveMessages` continue), and starts a fresh agent on the resumed id (NOT `${agentId}-session`). The agent-loop does NOT hard-inject `sessionPersistence` (that would pend non-persistent demos forever); `resume` rejects with a clear error when it is absent. +- **`ctx.agents.create()` and `ctx.agents.resume()` are async factories; resume additionally crosses the persistence boundary.** `ctx.agents.resume({ resumeSessionId })` awaits `ctx.sessionPersistence.load`, recreates the live session with the loaded events (so `lastTurnNumber`/`deriveMessages` continue), and starts a fresh agent on the resumed id (NOT `${agentId}-session`). The agent-loop does NOT hard-inject `sessionPersistence` (that would pend non-persistent demos forever); `resume` rejects with a clear error when it is absent. ## Alternatives considered @@ -33,4 +33,4 @@ Format versioning: the header carries a `version`; `load` rejects any non-curren ## Consequences -Two new packages and the metadata seam in `dsh-session` (`session.header`, the `create(id?, options?)` signature). Bought: durable resume/fork, a read/replay path, crash tolerance, and the foundation the ACP `session/load` ([ACP support](../../proposed/feature/2026-06-14-acp-agent-client-protocol.md)) needs — all over the existing event-sourced log, with the backend swappable behind one interface. The reusable `runPersistenceContract` suite holds every backend to the same append-only / contiguous-seq / lazy-materialization / serializability semantics. This completes [event-sourced sessions](2026-06-11-event-sourced-sessions.md)'s deferred "real persistence backend" and resolves its `TODO(review)` on the event vocabulary: persisting the log freezes its shape, and the `assistant/chunk` fidelity question is answered above (persist verbatim). +Two new packages and the metadata seam in `dsh-session` (`session.header`, the `create(id?, options?)` signature). Bought: durable resume/fork, a read/replay path, crash tolerance, and the foundation the ACP `session/load` ([ACP support](../../implemented/feature/2026-06-14-acp-agent-client-protocol.md)) needs — all over the existing event-sourced log, with the backend swappable behind one interface. The reusable `runPersistenceContract` suite holds every backend to the same append-only / contiguous-seq / lazy-materialization / serializability semantics. This completes [event-sourced sessions](2026-06-11-event-sourced-sessions.md)'s deferred "real persistence backend" and resolves its `TODO(review)` on the event vocabulary: persisting the log freezes its shape, and the `assistant/chunk` fidelity question is answered above (persist verbatim). diff --git a/docs/rfc/implemented/architecture/2026-06-18-agent-lifecycle-and-ownership-seams.md b/docs/rfc/implemented/architecture/2026-06-18-agent-lifecycle-and-ownership-seams.md index e786adbfe8..d64329262e 100644 --- a/docs/rfc/implemented/architecture/2026-06-18-agent-lifecycle-and-ownership-seams.md +++ b/docs/rfc/implemented/architecture/2026-06-18-agent-lifecycle-and-ownership-seams.md @@ -16,9 +16,9 @@ A new `cancel()` verb on the `Agent` interface — the single public stop primit ### 2. `AgentHandle` async disposer -`ctx.agents.create`/`resume` (and the `AgentFactory` interface) return `AgentHandle = { agent: Agent; dispose(): Promise }`. The disposer is a **capability** — only the holder can tear down exactly this agent: stop its loop, `await` the loop's exit (true quiescence, not just the `disposed` status flip), unregister it, and remove its session from the store. `ctx.agents.get(id)` still returns a bare `Agent`. Config-created agents stay owned by the `AgentLoop` fiber (the handle is discarded). ACP holds each session's disposer in its `SessionRecord` and runs it on disconnect/teardown, so a bare client disconnect leaves no registered agent and no session-store entry — even when `session/load` races teardown (the just-resumed handle is disposed before the closed-guard throw). +`ctx.agents.create`/`resume` (and the `AgentFactory` interface) return `AgentHandle = { agent: Agent; dispose(): Promise }`. The disposer is a **consumer capability** — a registry observer holding only the bare `Agent` cannot tear it down. The caller fiber and registered factory provider are structural co-owners: caller unload enforces structured ownership, while provider unload must stop old instances whose scoped dependency surface resolves through that provider. All three paths reach the same memoized teardown: stop the loop, await its exit and idle flushes (true quiescence, not just the `disposed` status flip), detach the agent, detach its session, and unwind its scope. Each public ID becomes reusable when its exact registry entry detaches; there is no separate reservation-release phase. Config-created agents are already owned by the `AgentLoop` fiber (the handle is discarded). ACP holds each session's disposer in its `SessionRecord` and runs it on disconnect/teardown, so a bare client disconnect leaves no registered agent and no session-store entry — even when `session/load` races teardown (the just-resumed handle is disposed before the closed-guard throw). -**Teardown ORDER is load-bearing for durability**, and the implementation folds the session lifecycle into the agent's SINGLE composite cordis effect (`SessionStore.prepare`/`enter`/`announce`, replacing a sibling-effect split). A fiber unload disposes sibling effects concurrently (`Promise.all`), which would race the session's `onAppend` detach against the loop's closing `session/flush` and drop the closing `turn/end`; inside one effect the disposers run as an ordered LIFO chain (loop stopped + `await agent.done` BEFORE the session detaches), so the loop's final flush is captured on BOTH the handle's `dispose()` and a fiber unload. The register disposer's `agent/disposed` emit is contained (a throwing listener must not reject the chain and skip the later session detach). +**Teardown ORDER is load-bearing for durability**, and the implementation folds the session lifecycle into the agent's SINGLE composite cordis effect (`SessionStore.prepare`/`enter`/`announce`, replacing a sibling-effect split). A fiber unload disposes sibling effects concurrently (`Promise.all`), which would race removing the session store's append publication hooks against the loop's closing `session/flush` and drop the closing `turn/end`; inside one effect the disposers run as an ordered LIFO chain (loop stopped + `await agent.done` BEFORE the session detaches), so the loop's final flush is captured on BOTH the handle's `dispose()` and a fiber unload. The contained `agent/disposed` and `session/disposed` notifications cannot reject the chain or skip later teardown. ### 3. Bash owner token in the seam @@ -33,16 +33,14 @@ These invariants hold and are pinned by tests: - A `tool-bash` HMR reload does NOT make an existing background task readable or killable by a different session (ownership survives on the executor). - Existing non-ACP demos still work without managing handles explicitly; config-created agents remain owned by the `AgentLoop` plugin fiber. -## Seam precondition (recorded) +## Session owner tokens are unique among live agents -The bash owner-token comparison relies on `session.header.id` being unique among live agents. The agent registry does NOT enforce this — it rejects a duplicate *agentId*, not a duplicate session id, and `createAgent` accepts an arbitrary `sessionId`. This is NOT reachable via ACP (UUID sessionId, `agentId === sessionId`, duplicate-load rejected), so it is not a live product hole, but a programmatic caller that registers two agents with the same session id would break bash isolation and mis-route the completion notice. The access *policy* (token comparison) stays in `tool-bash` (the consumer); the bash seam stores only an opaque `owner` string and never interprets it — the correct interface/impl/consumer split. - -The planned resolution is to remove the precondition by construction — see [unify the agent id and the session id](../../proposed/simplification/2026-06-20-unify-agent-and-session-id.md): once an agent IS its session (one id), the registry's existing unique-`agentId` check is a unique-session-id guarantee and no two live agents can share a session token. +The bash owner-token comparison relies on `session.header.id` being unique among live agents. Concurrent same-ID operations may both prepare privately, but publication enters the session and agent in order; `SessionStore.enter()` rejects a duplicate live session id, and every losing transaction rolls its private state back. A programmatic caller therefore cannot publish two live agents with one session token. The access *policy* (token comparison) stays in `tool-bash` (the consumer); the bash seam stores only an opaque `owner` string and never interprets it — the correct interface/implementation/consumer split. ## Alternatives considered - **A public `BashTask.owner` field** instead of the `BashExecutor.ownerOf(id)` seam — rejected: one read path, no redundant API. -- **Sibling cordis effects for the agent's session lifecycle** — rejected: a fiber unload disposes sibling effects concurrently (`Promise.all`), racing the session's `onAppend` detach against the loop's closing `session/flush`; the single composite effect's ordered LIFO chain is what captures the closing `turn/end` on both disposal paths. +- **Sibling cordis effects for the agent's session lifecycle** — rejected: a fiber unload disposes sibling effects concurrently (`Promise.all`), racing removal of the store-owned append publication hooks against the loop's closing `session/flush`; the single composite effect's ordered LIFO chain is what captures the closing `turn/end` on both disposal paths. - **A separate step-only `abort()` beside `cancel()`** — shipped originally, then removed as unused; `cancel()` is the single public stop primitive ([the public-stop-surface RFC](../simplification/2026-06-20-public-agent-stop-surface.md)). ## Consequences diff --git a/docs/rfc/implemented/architecture/2026-06-30-event-domain-semantics.md b/docs/rfc/implemented/architecture/2026-06-30-event-domain-semantics.md index 9375a6e248..f0ab95ca80 100644 --- a/docs/rfc/implemented/architecture/2026-06-30-event-domain-semantics.md +++ b/docs/rfc/implemented/architecture/2026-06-30-event-domain-semantics.md @@ -28,9 +28,9 @@ This is the foundational change in a stack that adds a Hooks subsystem; it estab ## Consequences -- The loop no longer emits any boundary mirror; `closeStep` appends `step/end` only and `closeTurn` appends `turn/end` only. A throwing `step/end`/`turn/end` session-event listener is the surviving boundary-listener failure path (contained inside `closeStep`/`closeTurn` — `Session.append` pushes the event before notifying listeners, so the boundary is durable and the turn closes balanced regardless). +- The loop no longer emits any boundary mirror; `closeStep` appends `step/end` only and `closeTurn` appends `turn/end` only. `Session.append` owns post-commit observer containment, so a throwing boundary observer cannot change the turn outcome or starve later consumers; an acceptance or internal validation failure still escapes before the boundary enters the log. - Tests that observed boundaries via the removed emits now observe the durable `turn/start`/`turn/end`/`step/start`/`step/end` session events — the behavior they pin (boundary ordering, step counting) is unchanged; only the feed they read moved to the canonical one. The tests that exercised a *throwing turn-boundary emit listener* were deleted, because that code path no longer exists (there is no emit to throw from). Per [AGENTS.md "tests document behavior, not golden truth"](../../../../AGENTS.md), the behavior and its test moved (or died) together. -- The loop marks the step open (`stepOpen = true`) BEFORE appending `step/start`, because `Session.append` pushes the event to the log before notifying `session/event` listeners (validation throws happen earlier, before the push — see [the session append contract](../../../core-data-structures/session.md)). So a throwing `step/start` session-event listener runs with the step already open and the event already in the log: the loop's outer catch then calls `closeStep()`, which appends the balancing `step/end`, and the turn closes balanced with an error (`turn/start → step/start → step/end → turn/end` — verified by the invariants oracle in the regression test). Closing the open step is owed precisely because the marker is set first. +- The loop marks the step open (`stepOpen = true`) only after `append('step/start')` returns. Internal dispatch validation runs before the log push and may reject without opening a step; post-commit `session/event` observer failures are contained inside `Session.append`. The marker therefore represents exactly the committed boundary that owes a later `step/end`. - The full realization of this is [the simplification RFC "Stop mirroring durable boundaries as agent events"](../simplification/2026-06-20-remove-agent-boundary-mirror-events.md): all four boundary mirrors are removed and every consumer reads boundaries off `session/event`. `agent/steering` (not a boundary mirror) stayed outside that RFC's scope and was removed by its own follow-up, [Remove the `agent/steering` mirror emit](../simplification/2026-07-04-remove-agent-steering-mirror.md) — it mirrored the durable `steering/message`. - The cordis events catalog (`docs/cordis-catalog/events.md`) is regenerated to drop the mirror events. diff --git a/docs/rfc/implemented/architecture/2026-07-05-prompt-variables-and-tool-guidance-ownership.md b/docs/rfc/implemented/architecture/2026-07-05-prompt-variables-and-tool-guidance-ownership.md index 24a525307e..0961830c1a 100644 --- a/docs/rfc/implemented/architecture/2026-07-05-prompt-variables-and-tool-guidance-ownership.md +++ b/docs/rfc/implemented/architecture/2026-07-05-prompt-variables-and-tool-guidance-ownership.md @@ -16,11 +16,11 @@ The assembled system prompt had four defects, all of one family: facts the harne ## Decision -**One principle: every fact in the prompt has exactly one owner.** The model name and workspace are config/session facts → the harness exposes them as variables and the persona references them. Per-tool semantics and when-to-use → the tool's `description`. Cross-call habits a description cannot carry → the tool package's prompt section. Identity and behavior → the deployment's persona, and nothing else. +**One principle: every fact in the prompt has exactly one owner.** The model name and workspace are config/session facts → the harness exposes them as variables and the persona references them. Per-tool semantics and when-to-use → the tool's `description`. Cross-call habits a description cannot carry → the tool package's prompt section. Harness provenance → the static `harness:identity` section. Deployment role and behavior → the deployment's persona. ### Assemble context -`SystemPrompt.assemble(context)` takes an `AssembleContext` — declared EMPTY and merge-extensible in `dsh-system-prompt` (the package stays agnostic of who assembles); `dsh-agent` declaration-merges `agent?: Agent` onto it (a new type-level edge `agent → system-prompt`, no cycle — `tools` already depends on both). The loop passes `{ agent }` each step; section text providers become `string | ((context) => string)` (zero-arg providers stay valid), and the `system-prompt/assemble` waterfall gains the context parameter so a listener can filter or extend per agent. +`SystemPrompt.assemble(context)` takes a merge-extensible `AssembleContext`. `dsh-system-prompt` declares the optional `scope` selector used for scoped routing, while `dsh-agent` declaration-merges the optional typed `agent` field onto it (a type-level edge `agent → system-prompt`, with no runtime dependency cycle). The loop calls `assembleContextFor(agent)` each step so both fields identify the same agent; section text providers may read that context, and the `system-prompt/assemble` waterfall receives it so a listener can filter or extend per agent. ### Prompt variables @@ -30,15 +30,15 @@ Plugins contribute named values via `ctx.systemPrompt.variable(name, provider)`; ### Persona as the order-0 section -`dsh-system-prompt` itself registers the two harness-owned sections (they must survive a swapped loop plugin, so they do NOT live on `dsh-agent-loop`): the static `harness:identity` at order `-100` — every prompt opens by stating the agent is powered by the DeepSeek Harness SDK — and `deployment:persona` at order 0, whose text is the plugin's own `persona` config. The persona is per-DEPLOYMENT, not per-agent: every agent in the context (subagents included) renders the same one, `AgentOptions.systemPrompt` is deleted along with the per-agent forwarding plumbing (the app configs' `systemPrompt` keys become a `persona` key routed to this plugin through `dsh-agent-core`), and the ACP bridge and `dsh-tool-subagent` stop carrying persona configuration entirely. The loop's special-case join is deleted: `fullSystemPrompt ≡ renderPrompt(assembly)`, one ordered pipeline for everything the model sees, and `agent/pre-step` (compaction's token-pressure input) measures exactly the real prompt. Order bands are now convention: harness identity `-100`, persona `0`, tool guidance `100–199`; other negative orders also render before the persona. +`dsh-system-prompt` itself registers the two harness-owned sections (they must survive a swapped loop plugin, so they do NOT live on `dsh-agent-loop`): the static `harness:identity` at order `-100` — every prompt opens by stating the agent is powered by the DeepSeek Harness SDK — and the global default `deployment:persona` at order 0, whose text is the plugin's own `persona` config. `AgentOptions.systemPrompt` and the loop's special-case join are gone: `fullSystemPrompt ≡ renderPrompt(assembly)`, one ordered pipeline for everything the model sees, and `agent/pre-step` (compaction's token-pressure input) measures exactly the real prompt. An agent-scoped section with the same `deployment:persona` name shadows the default for that agent; programmatic setup may register one directly, and the subagent persona feature installs one before publishing an in-process child when the selected provider supports it. Order bands are convention: harness identity `-100`, persona `0`, tool guidance `100–199`; other negative orders also render before the persona. ### Tool guidance ownership Per-tool semantics and when-to-use live in tool DESCRIPTIONS, which already ship in every request — the YAML prose was ~fully redundant with them. Sections carry only the cross-call habits a single call's description cannot: `dsh-tool-bash` contributes `tool:bash` (order 105) — check the `[exit code: N]` marker on every result; `dsh-tool-fs`'s read section gains the "not shell commands like cat" contrast. `todo_write` and the subagent tools need NO section — their descriptions already carry the whole contract. The leaf personas shrink to identity + behavior (verify your work; keep answers brief), and the welcome banner stops enumerating tools. -### The subagent context contract +### The subagent conversation-history descriptor -`SubagentProvider` gains `readonly inheritsParentContext: boolean` — a DESCRIPTIVE fact beside `capabilities`, not in it (capabilities are start-time validation; nothing validates against this flag). Spawn and ACP declare `false`, fork declares `true`. `dsh-tool-subagent` derives both the tool description and the `prompt` parameter description from the flag (`providerWording`): the fork instance now tells the model the child inherits the conversation's completed turns (not the in-flight turn) and that its prompt should state only what is new. Deriving the description from a provider that arrives on its own fiber is what forced the provider-lifecycle events and the tool's reactive registration — that mechanism, its Loader-concurrency rationale, and its rejected alternatives are recorded in [the provider-lifecycle-events RFC](2026-07-05-subagent-provider-lifecycle-events.md). +`SubagentProvider` gains `readonly inheritsParentContext: boolean` — a DESCRIPTIVE conversation-history fact beside `capabilities`, not in it (capabilities are start-time validation; nothing validates against this flag). Spawn and ACP declare `false`, fork declares `true`. The name refers only to conversation seeding, not Cordis scope, services, tools, or authority. `dsh-tool-subagent` derives both the tool description and the `prompt` parameter description from the flag (`providerWording`): the fork instance now tells the model the child is seeded with the conversation's completed turns (not the in-flight turn) and that its prompt should state only what is new. Deriving the description from a provider that arrives on its own fiber is what forced the provider-lifecycle events and the tool's reactive registration — that mechanism, its Loader-concurrency rationale, and its rejected alternatives are recorded in [the provider-lifecycle-events RFC](2026-07-05-subagent-provider-lifecycle-events.md). ## Alternatives considered @@ -56,7 +56,7 @@ Per-tool semantics and when-to-use live in tool DESCRIPTIONS, which already ship ## Shipped invariants -- `renderPrompt(assemble({ agent }))` for the coding-agent example renders the persona FIRST (with the agent's model name interpolated), then the fs/bash/web guidance sections; the loop has no other prompt-composition path. +- `renderPrompt(await assemble(assembleContextFor(agent)))` for the coding-agent example renders the harness identity, then the persona (with the agent's model name interpolated), then the fs/bash/web guidance sections; the loop has no other prompt-composition path. - The `subagent_fork` schema description says the child inherits the conversation; the `subagent` one says it does not. The tool follows its provider: absent before the backend activates, present after, gone when the backend unloads, re-worded from the fresh provider on reload. - Unknown/valueless/malformed/unbalanced `{{…}}` references throw with the section name in the message; duplicate section, variable, and tool-name registrations all throw. - Snapshot goldens are prompt-independent by construction: llm-replay keys replay on (turn, step) chunk streams and never re-verifies the outgoing request. diff --git a/docs/rfc/implemented/architecture/2026-07-05-reconstructable-requests.md b/docs/rfc/implemented/architecture/2026-07-05-reconstructable-requests.md index 73967c14b6..0b82e52581 100644 --- a/docs/rfc/implemented/architecture/2026-07-05-reconstructable-requests.md +++ b/docs/rfc/implemented/architecture/2026-07-05-reconstructable-requests.md @@ -24,7 +24,7 @@ Prefix-cache stability is corollary #1, not the headline: an append-only log pro **The loop, transmission-stateless.** Per step: render assembly (every step — value comparison needs no change-signal discipline, and a section that varies per step surfaces as a *logged* header event per step instead of a silent bust) → on the instance's FIRST step only, the `agent/session-prefix` waterfall — request-ONLY messages fronting the entire derived history (a frozen empty seed, contributions returned as an extension of `next()`; the home for session-stable openers that must NOT become history — a skills catalog, an AGENTS.md digest), deep-frozen and cached on the instance so reuse is structural and the prefix cannot drift mid-session — → `agent/pre-step`, carrying the composed prefix (compaction's surface mutations land before derivation, and its pressure gate counts the prefix this instance will actually send — never a previous instance's logged one, which could under-gate a resumed/forked instance whose contributor grew) → **messages snapshot, then `step/start` appended as the next operation in the same synchronous frame** → seed the call config (first request of the instance: from `AgentOptions`, so explicit options always beat the logged baseline — fork model-overrides and resume reconfiguration stay correct; afterwards: from the folded header) → the `agent/request` waterfall, re-typed `(agent, turn, step, config: LlmCallConfig, next) → LlmCallConfig` — a frozen seed and a returned replacement are ALL a listener shapes; durable content flows through the log channels (`inject()`, steering, prompt-submit `additionalContext`, sections via `system-prompt/assemble`) — → the header event the request owes the log, carrying the prefix as `messagePrefix` (no session event carries it, so the header is its only durable record; resume = a new instance = a recompose, anchored by its `'resume'` snapshot) → build `GenerateOptions` from `messagePrefix + snapshot` + header, deep-freeze (`deepFreeze` exempts the `AbortSignal`, the one live control channel — freezing one breaks `AbortController.abort()`), dispatch. The loop's per-instance bookkeeping is one boolean plus the cached prefix: whether this instance has logged its anchoring snapshot, and what it composed. -**The reconstruction boundary is `step/start`, unconditionally.** A step's messages are the derivation over `events[0..stepStartSeq)`. Because the snapshot precedes the `step/start` append in the same synchronous frame, nothing can enter this request past the boundary: an `agent.inject()` from an `agent/request` listener (or any concurrent task, or a `session/event` listener firing on `step/start` itself) lands in the log after the boundary and joins the NEXT request. For waterfall-window appends this matches the prior loop (it also derived before its waterfall); for a synchronous `step/start` listener it is a deliberate change — such a listener could previously reach the current request — and `agent/pre-step` is the sanctioned seam for content that must affect the CURRENT request. A step's header for reconstruction is the fold after its own `request/header*` event (which sits between its `step/start` and first response event) or the fold carried forward. +**The reconstruction boundary is `step/start`, unconditionally.** A step's messages are the derivation over `events[0..stepStartSeq)`. Because the snapshot precedes the `step/start` append in the same synchronous frame, an `agent.inject()` from an `agent/request` listener or any concurrent task lands after the boundary and joins the NEXT request. `session/event` is observe-only during publication: a reentrant append is rejected until the current callback list drains, preventing nested event delivery from overtaking the event being observed. `agent/pre-step` is the sanctioned seam for content that must affect the CURRENT request. A step's header for reconstruction is the fold after its own `request/header*` event (which sits between its `step/start` and first response event) or the fold carried forward. **Enforcement.** Dev-mode ([dsh-invariants](../../../../packages/support/invariants/src/index.ts)), on `llm/stream`: a frozen request with a live `sessionId` — the loop-built marker; hand-built one-shots are unfrozen and skipped — must carry messages deep-equal to the folded header's `messagePrefix` followed by the boundary derivation — the derivation rebuilt through a FRESH `Session` over `events[0..stepStartSeq)` so the live cache cannot vouch for itself — and header fields equal to `foldRequestHeader` over the log. There is no divergence allowance and nothing to allow: no seam can put unlogged content into a request — the `agent/session-prefix` seam's product enters only because the header event records it first. `prepend: true` only defends against the replay adapter's short-circuit (an append-registered listener); two prepended listeners have no defined mutual order in cordis, so correctness rests on the seq-bounded fold, never on listener timing. Measurement stays lean: the with-key e2e ([request-cache.e2e.ts](../../../../packages/core/agent-loop/tests/request-cache.e2e.ts)) proves `usage.cacheReadTokens > 0` on every request after the first against the live API, and per-step usage in the log is the production observable — a header event or compaction shows up as a cache-read collapse on the next step. diff --git a/docs/rfc/implemented/architecture/2026-07-08-agent-scope-contexts.md b/docs/rfc/implemented/architecture/2026-07-08-agent-scope-contexts.md new file mode 100644 index 0000000000..dcff4d3220 --- /dev/null +++ b/docs/rfc/implemented/architecture/2026-07-08-agent-scope-contexts.md @@ -0,0 +1,170 @@ +# RFC: The agent is a registration scope + +Status: implemented + +## Problem + +One application needs to share infrastructure across many agents while letting each agent have its own tools, prompt contributions, policies, and listeners. Shared adapters, persistence, and user interfaces belong to the deployment; a persona, tool variant, or listener often belongs to one agent. + +A separate service graph per agent duplicates shared infrastructure. One global registration graph has the opposite failure: an agent-specific contribution can leak into unrelated agents. Contributors need one ordinary registration mechanism that determines both who can see a contribution and when it is cleaned up. + +The mechanism also needs a publication boundary. An agent must not become visible before its local world is complete, and teardown must retain that world until final work has stopped. + +## Decision + +Every live agent owns one flat registration layer exposed as `agent.ctx`. Code registers through the context that owns a contribution; scope-aware services combine deployment-global registrations with exactly one matching agent layer; operations choose that layer from their real agent; and the layer exists for the agent's complete published lifetime. + +Cordis is the plugin framework underneath the SDK. A Cordis **context** is the object plugins use to access services and register effects whose cleanup follows that context. The [Cordis primer](../../../cordis-primer.md) explains the framework in more detail. + +For most contributors, the complete contract is four rules: + +| Question | Rule | +|---|---| +| Where do I register behavior for one agent? | Call the ordinary registration API through `agent.ctx` | +| What does an operation for an agent see? | Deployment globals plus that agent's layer, using the owning service's merge rules | +| Which scoped listeners run? | Unscoped listeners plus listeners registered for the operation's agent | +| How long does the layer exist? | Setup completes before publication; disposal keeps it until work reaches quiescence | + +The scope is flat. Resolution never walks parent or sibling scopes, and lifetime ownership does not imply registration inheritance. + +```mermaid +flowchart LR + plain["Plain plugin context
cleanup follows the plugin"] -->|"registers into"| globalLayer["Deployment-global layer"] + agentAContext["agentA.ctx
cleanup follows Agent A"] -->|"registers into"| agentALayer["Agent A layer"] + agentBContext["agentB.ctx
cleanup follows Agent B"] -->|"registers into"| agentBLayer["Agent B layer"] + + operationA["Operation for Agent A"] -->|"selects"| agentAView["Agent A view
globals plus A local"] + globalLayer --> agentAView + agentALayer --> agentAView + operationB["Operation for Agent B"] -->|"selects"| agentBView["Agent B view
globals plus B local"] + globalLayer --> agentBView + agentBLayer --> agentBView +``` + +The missing cross-edges are the isolation rule: Agent A's local registrations do not enter Agent B's view, and a parent's registrations do not enter a child merely because the parent owns the child's lifetime. + +The companion [runtime-design RFC](2026-07-12-agent-scope-runtime-design.md) explains the implementation and correctness reasoning. The [subagent composition-controls RFC](../feature/2026-07-12-subagent-persona-tool-filter-and-depth.md) owns the separate `persona`, `toolFilter`, and `maxDepth` feature. + +### Registration origin chooses visibility and cleanup + +A registration made through a plain plugin context is deployment-global and is disposed with that plugin. The same method called through `agent.ctx` contributes to one agent and is disposed with that agent's scope. + +| Registration origin | Default visibility | Disposed with | +|---|---|---| +| Plain plugin context | Every eligible agent view | Registering plugin | +| `agent.ctx` | Exactly that agent's view | Agent scope | + +Tools, prompt sections and variables, tool restrictions, guards, and scoped event listeners adopt this contract. Named local values ordinarily shadow a same-named global value for that agent; each owning service documents exceptions and merge behavior. + +The ordinary contributor pattern is to register the complete local world during agent setup: + +```js +const handle = await ctx.agents.create({ + agentId: AgentId('reviewer'), + sessionId: SessionId('reviewer-session'), + agentOptions: { model: 'model-name' }, + setup(agentCtx) { + agentCtx.systemPrompt.section({ + name: 'deployment:persona', + order: 0, + text: 'Review code, but do not modify files.', + }) + agentCtx.tools.register({ + name: 'review_summary', + description: 'Return the review summary.', + parameters: { type: 'object', properties: {} }, + async execute() { + return [{ type: 'text', text: 'review complete' }] + }, + }) + }, +}) + +ctx.tools.get('review_summary') // undefined: not global +ctx.tools.get('review_summary', handle.agent) // the reviewer-local tool + +await handle.dispose() +ctx.tools.get('review_summary', handle.agent) // undefined: scope is gone +``` + +Setup receives a full trusted Cordis context so it can compose ordinary plugins and services. Its contract is composition-only: driving or publishing the in-flight agent through casts or internal registry calls is unsupported. + +### The operation chooses the view + +Registration origin and operation subject are separate facts. Calling a service through `agent.ctx` selects where a new registration belongs; it does not bind later reads to that agent. + +Tool lookup and execution receive the agent they act for. Prompt assembly receives an assembly context for the agent whose request is being built. Event dispatch receives its domain subject. This keeps shared service instances reusable across agents while making each operation's view explicit. + +Only services that adopt the scope contract resolve an agent layer. `agent.ctx` does not automatically change arbitrary Cordis service calls. + +### Scoped events keep routing separate from event data + +An event about Agent A normally reaches unscoped listeners and A-scoped listeners, not B-scoped listeners. An event without an agent subject reaches only unscoped listeners. + +At the Cordis level, `Scoped` is an opaque routing receiver. It carries the filter used to choose listeners but is not the domain object. Event signatures therefore keep the real `Agent`, tool execution, approval request, or other subject as an explicit argument that listeners can inspect. + +A listener registered with `{ global: true }` deliberately bypasses contextual audience filtering while its cleanup still follows the registering context. Registry-membership notifications remain unfiltered because they describe shared registry state rather than one agent's operation. The generated [event catalog](../../../cordis-catalog/events.md) is the exhaustive event reference. + +### Creation publishes last and disposal revokes last + +`ctx.agents.create()` and `resume()` build an unpublished session, scope, agent, and driver. They await `setup`, admit the final session and agent entries, announce them in order, start the loop, and only then return a handle. + +An optional creation signal cancels work only while create or resume is pending. After the promise resolves, the returned `AgentHandle` owns explicit disposal. + +If loading, setup, admission, or publication fails, the private transaction rolls back everything it prepared. Concurrent operations using the same caller-supplied live ID may both reach setup, but final registry entry admits only one; every loser rejects and cleans its private resources. Sequential reuse after awaited disposal remains valid. + +`AgentHandle.dispose()` reverses the boundary. It deactivates creation or driving, waits for synchronous publication to unwind, stops and drains the driver and final session flushes, detaches the agent and session, and finally disposes the scope. Repeated or racing disposal requests join one completion promise. + +The calling Cordis context and the concrete AgentLoop factory are structural co-owners. Unloading either disposes the transaction or live agent. + +```mermaid +flowchart TB + request["Create or resume"] --> privateWorld["Build private session, scope, agent, and driver"] + privateWorld --> setup["Await composition through agent.ctx"] + setup --> admission["Admit final session and agent entries"] + admission --> publish["Announce lifecycle and start the driver"] + publish --> live["Return AgentHandle"] + + privateWorld -->|"failure, cancellation, or owner loss"| rollback["Rollback private work"] + setup -->|"failure, cancellation, or owner loss"| rollback + admission -->|"duplicate or owner loss"| rollback + publish -->|"listener failure or owner loss"| rollback + live -->|"handle or owner disposal"| quiesce["Stop and drain work"] + rollback --> quiesce + quiesce --> detach["Detach agent, then session"] + detach --> revoke["Dispose the agent scope"] +``` + +## Security and authority are non-goals + +Agent scopes compose trusted same-process registrations. They do not sandbox plugins, define a parent-to-child authority lattice, freeze grants at creation, or guarantee that a child can do no more than its parent. + +A parent may own a child whose visible tools are wider than its own because lifetime ownership does not donate or cap registrations. A plugin holding a Cordis context also runs in the same process and can call available services directly. + +Deployments that need non-escalation require a separate authority representation, propagation rule, and execution check. Parent-subset grants, creation-time authorization snapshots, explicit future-grant APIs, and generic capability/output/termination tags are outside this decision. + +## Alternatives considered + +The rejected designs either separate visibility from cleanup, cover only one registration family, duplicate shared infrastructure, or conflate lifetime ownership with inheritance. + +### Pass an agent option to every registration + +An API such as `tools.register(definition, { agent })` repeats scope plumbing in every registry and permits visibility ownership to drift from cleanup ownership. Registering through `agent.ctx` makes both facts follow one Cordis effect owner. + +### Filter events while keeping registries global + +Listener filtering prevents the wrong hook from running but does not scope tool schemas, executable lookup, prompt sections, variables, or other registered data. Agent-local composition would still require temporary global mutation. + +### Create one service graph per agent + +The required view is shared deployment services plus one local registration layer. Per-agent graphs duplicate adapters and complicate shared persistence, provider registries, and application boot. + +### Inherit parent registration scopes + +Parentage describes lifetime and conversation lineage, not a universal merge policy. Hierarchical lookup makes unrelated services inherit accidentally and cannot define security without a separate authority model. + +## Consequences + +Contributors use one familiar pattern: register shared behavior through a plugin context, register local behavior through `agent.ctx`, select the real agent on operations, and dispose the returned handle. Setup is atomic from an observer's perspective, and teardown preserves local behavior until work stops. + +The cost is explicit subject selection, asynchronous programmatic creation, and service-specific scope adoption. Flat registration scope is intentionally not authority, and subagent composition controls remain a separate feature rather than hidden scope semantics. diff --git a/docs/rfc/implemented/architecture/2026-07-12-agent-scope-runtime-design.md b/docs/rfc/implemented/architecture/2026-07-12-agent-scope-runtime-design.md new file mode 100644 index 0000000000..063cc4738c --- /dev/null +++ b/docs/rfc/implemented/architecture/2026-07-12-agent-scope-runtime-design.md @@ -0,0 +1,390 @@ +# RFC: Agent-scope runtime design and correctness + +Status: implemented + +## Problem + +The [agent-scope contract](2026-07-08-agent-scope-contexts.md) is simple for contributors: register through `agent.ctx`, resolve one global-plus-agent view, publish only after setup, and retain the scope until work stops. The runtime must preserve that contract across a cooperative plugin framework, asynchronous creation, reentrant listeners, durable session commits, and worker or process failure. + +The main design risk is adding a second mechanism for every race. Separate reservations, readiness sentinels, cancellation relays, snapshot layers, and protection registries can mirror the same fact until no reader can tell which one is authoritative. That machinery also encourages the runtime to treat trusted typed calls as hostile serialization boundaries. + +The implementation needs enough state to preserve real ownership and settlement boundaries, but no more. A correctness reviewer must be able to follow one fact from acceptance through publication and teardown without reconciling parallel representations. + +## Decision + +The runtime uses one mechanism per independent fact. Scope routing has an opaque carrier; each live registry object has one entry record; each create or resume operation has one transaction; typed same-process calls borrow readonly values; real data boundaries materialize once; the cooperative prompt-assembly result is authoritative; and worker/process code retains separate terminal and quiescence state only where different owners can genuinely race. + +The design can be skimmed as seven choices: + +| Problem | Authoritative mechanism | +|---|---| +| Select global plus one agent's registrations | Opaque scope key and routing carrier | +| Own one live agent or session | One registry entry captured by its disposer | +| Coordinate create/resume | One `AgentCreationTransaction` | +| Protect durable, queued, model, or wire data | Materialize once at that boundary | +| Pass typed values inside one process | Readonly borrowed contract | +| Compose the model-visible prompt and tool surface | One shared tool view plus the authoritative assembly-waterfall result | +| Coordinate subagent, worker, and process shutdown | One cancellation signal plus the independent terminal/quiescence facts of that boundary | + +The rest of this RFC expands those choices in dependency order. It first explains the Cordis mechanics, then scope routing, creation and session commit, tools and prompts, subagents and workflows, and finally the checks that make the reasoning executable. + +The [July 8 RFC](2026-07-08-agent-scope-contexts.md) remains the contributor contract. The separate [subagent composition-controls RFC](../feature/2026-07-12-subagent-persona-tool-filter-and-depth.md) owns `persona`, `toolFilter`, and `maxDepth`; this document discusses only how their setup fits the lifecycle. + +## Cordis model: context, fiber, effect, receiver, and waterfall + +Five Cordis ideas are required to understand the implementation. A context selects services and registration ownership; a fiber is one live plugin or child lifecycle; an effect attaches cleanup to a fiber; an event receiver selects listeners; and a waterfall lets listeners transform or veto an operation in sequence. + +### A context is an ownership path through one service graph + +All agents share one Cordis service graph. A derived context does not clone `ToolRegistry`, `SystemPrompt`, persistence, or model adapters; it changes how registrations made through that context are tagged and which effects own their cleanup. + +`agent.ctx` is such a derived context. Service calls still reach the shared instances, while a registration can inspect its calling context and store a contribution under the nearest scope key. Ordinary plugin contexts carry no scope key and therefore register globally. + +### Fibers and effects make cleanup structural + +A Cordis fiber is the live instance created when a plugin or child context is activated. Its state records whether that lifecycle is active, unloading, failed, or disposed. `ctx.effect()` and `ctx.on()` return disposers and also attach those disposers to the registering fiber, so unloading a plugin or agent scope removes everything registered through that context without a separate inventory. + +The vendored Cordis fiber implementation establishes ownership before arbitrary setup or `internal/plugin` observers run. A reentrant unload can see the child fiber or effect that has started, reject effects added after unload begins, and join cleanup already started through a public single-shot disposer. Teardown observers are contained individually so one callback cannot prevent structural cleanup. + +These are framework lifecycle guarantees rather than agent-specific policy. Agent creation depends on them because setup can activate arbitrary plugins and synchronously reenter owner disposal. + +### Receivers route listeners; waterfalls compose decisions + +Cordis filters listeners using the dispatch receiver (`this`), while harness listeners need an explicit agent, execution, request, or other subject. `Scoped` marks the receiver expected by a scoped event declaration, but the runtime carrier deliberately exposes no subject API. + +Product helpers therefore construct the carrier and pass the domain subject separately. This prevents listener routing from becoming an alternate object model and keeps event signatures understandable without knowledge of carrier internals. + +A Cordis waterfall is middleware-style dispatch. Each listener receives `next()`: calling it delegates to the remaining listeners and base operation, while returning without it vetoes or replaces the downstream result. Waterfalls power prompt assembly and tool policy; ordinary emit events notify synchronously, and parallel events await all listeners without a veto result. + +## Scope routing: one opaque key selects one layer + +The scope package implements the smallest object needed for Cordis routing. Its carrier holds only a composed service filter and scope predicate, while the package records the opaque key privately and exposes the scope fiber's quiescent disposer separately. + +### Scope identity uses object identity + +A `ScopeKey` is an opaque object compared by identity. The harness uses the live `Agent` as its own key, but the primitive is domain-neutral and supports other scoped owners. + +`createScope(parent, key)` returns a scope whose `ctx` shares the parent's services and whose effects are tagged with that key. `scopeOf(ctx)` reads the nearest registration key. `scopeTarget(base, key)` creates the event receiver whose filter preserves the base receiver's Cordis service filter, then admits unscoped listeners and listeners with that exact key. + +The receiver is a small carrier rather than a transparent proxy for the domain object. Code that needs the agent receives the explicit event argument; code that needs registration ownership receives `agent.ctx`. + +### Registry reads overlay one exact map + +Scope-aware registries store global contributions separately from identity-keyed local contributions. A read resolves the global layer and at most one local layer; it never traverses parentage. + +Each service retains its domain rule. Named prompt values and tools use local shadowing, tool restrictions filter globals before local tools are added, and events select listener audiences rather than registered data. Scope supplies identity and ownership, not a universal merge algorithm. + +### Fused dispatch helpers prevent subject drift + +`agentEvents(context, agent)` constructs the agent's carrier and injects the same agent as the event subject. Session, tool, approval, prompt, and subagent services likewise derive routing from the object they already own instead of accepting an unrelated key. + +The type marker rejects ordinary bare-receiver mistakes, and development invariants cover direct JavaScript or casted dispatch. The subject remains explicit because routing correctness and useful event data are different concerns. + +## Agent creation: one transaction owns the complete operation + +Create and resume are one asynchronous lifecycle with several phases, not several lifecycles. `AgentCreationTransaction` owns caller and factory liveness, optional cancellation, private resources, publication, rollback, and the memoized teardown observed by every owner. + +### Registry entries are the only live identity records + +AgentRegistry and SessionStore each keep one entry per live object. The entry holds the stable ID, object, scoped carrier, and the small amount of publication or append state that belongs to that object. + +A detach closure captures its exact entry. It deletes only when the map still points to that entry, so an old disposer cannot delete a later object that reuses the same ID. No registry rereads a mutable caller object to decide identity. + +There is no reservation API. Caller-supplied IDs are admitted at final entry. Concurrent same-ID operations may both complete private setup; exactly one final `enter()` succeeds, and every loser rolls its private resources back. Sequential reuse is valid after the earlier disposer reaches quiescence. + +### The transaction owns preparation before awaiting it + +The transaction is installed under both the calling Cordis context and the concrete AgentLoop factory before persistence load or setup can suspend. It also observes an optional create/resume signal until the public operation settles. + +Create prepares a new Session. Resume loads and validates the persisted Session before preparing the same live session identity. Both paths then build the scope, agent, and driver and invoke the same setup/publication algorithm. + +The factory stores concrete trace targets but invokes them through a caller-bound Cordis trace. This preserves dependency origin and caller ownership without stacking trace proxies. + +### Setup is trusted composition inside a private world + +Setup receives the full child context and may await plugin activation. It can register tools, prompt sections, restrictions, listeners, and other effects, but the public contract does not support driving or publishing the in-flight agent through casts or internal registry calls. + +The transaction races asynchronous load and setup against deactivation rather than waiting forever for a promise owned by external code. If cancellation or owner unload wins, public creation rejects after transaction-owned cleanup even when the external promise never settles. + +### Publication has one ordered commit path + +Publication admits and announces resources in the order required by observers: + +1. Enter the session. +2. Enter the agent. +3. Announce `session/created`. +4. Announce `agent/created`. +5. Enable public driving. +6. Emit `agent/session-start`. +7. Start the driver. + +The agent never drives before both registries and creation notifications agree. A synchronous listener may veto or dispose an owner; the transaction records publication in progress and waits for that callback stack to unwind before teardown continues. Every creation announcement that begins has a matching disposal announcement during rollback. + +The sequence diagram isolates the non-obvious race: a synchronous creation listener can request disposal while the publication call stack still owns both registry entries. Teardown must deactivate immediately but wait for that stack to unwind before stopping and detaching anything. + +```mermaid +sequenceDiagram + participant Tx as AgentCreationTransaction + participant Registries + participant Listener as Synchronous listener + participant Driver + + Tx->>Tx: mark publication in progress + Tx->>Registries: announce agent/created + Registries->>Listener: invoke inside the same call stack + Listener->>Tx: dispose reentrantly + Tx->>Tx: deactivate, teardown waits for publication + Tx-->>Listener: disposal request accepted + Listener-->>Registries: return + Registries-->>Tx: announcement unwound + Tx->>Tx: resolve publication settlement + Tx->>Driver: stop and drain + Tx->>Registries: detach agent, then session + Tx->>Tx: dispose scope and resolve teardown +``` + +### Teardown preserves work before revoking registrations + +Every teardown request joins one memoized path. The order is: + +1. Deactivate creation or driving and let synchronous publication finish. +2. Stop and drain the driver, including idle injection flushes. +3. Detach the agent. +4. Detach the session. +5. Dispose the agent scope. +6. Retire transaction ownership tracking. + +This order lets final agent and session events use the matching scoped listeners and keeps persistence observers attached through the final flush. Scope disposal comes last because registration revocation is the externally visible lifetime boundary. + +## Session append: materialize, validate, commit, notify + +Session events cross a durable boundary, so append owns their data. The rest of the algorithm uses one attached entry and one commit point. + +### Durable data is materialized once + +Session headers, seeds, and appended events are lossless JSON data. The Session constructor or append path materializes and validates them before storage and exposes frozen snapshots, so later caller mutation cannot change persistence, replay, or model reconstruction. + +This is a real ownership boundary: the values leave the caller, may be persisted, and must reconstruct the same request later. It is intentionally stricter than a typed same-process callback or registry definition. + +### Pre-commit listeners can veto; post-commit observers cannot + +Append follows one sequence: + +1. Materialize the durable event and surface intent. +2. Claim the SessionEntry and reject reentrant append on that entry. +3. Resolve scoped callbacks and run internal invariant validation. +4. Push exactly once; this is the commit point. +5. Notify each observer independently, containing synchronous and asynchronous failures. +6. Release append state and honor a detach requested during publication. + +No observer error makes a committed event look uncommitted, and one bad listener cannot starve later listeners. Session invariants stage their transition before commit and apply it only when the same event reaches the contained post-commit observer. + +`flush()` starts every persistence listener and awaits every result before reporting failure. This deliberate all-settled behavior prevents a synchronous failure from starving another backend or final flush. + +## Trust boundaries: copy only when ownership actually changes + +The runtime distinguishes typed in-process contracts from serialization and durability boundaries. This is the main simplification rule for values and callbacks. + +| Boundary | Ownership rule | +|---|---| +| Typed service/plugin call in the same process | Borrow readonly values and callbacks | +| Parsed plugin configuration or external file | Validate semantic and structural input | +| Queued inbox message | Materialize before asynchronous consumption | +| Model/tool JSON input or output | Materialize at the model/tool boundary | +| Durable session or persistence data | Materialize and validate before commit | +| Worker, process, or wire message | Serialize, validate, and own the decoded value | + +Tests that fabricate hostile getters, replace typed callbacks after handoff, or cast fake service objects do not define a production contract by themselves. The runtime keeps checks where data crosses a parser, queue, model, durable, file, worker, process, or wire boundary and relies on readonly types plus plugin discipline inside the trusted process. + +Callback containment is separate from data ownership. Listeners are arbitrary extension code and can throw even when their arguments are trusted; publication and post-commit paths still contain failures according to their event contract. + +## Tools and prompts: one view, authoritative assembly, committed outcomes + +Tool presentation and execution share one private resolver. Prompt assembly remains trusted cooperative composition: registries supply the ordered input, and the assembly waterfall's returned value is exactly what the loop logs and sends. Execution uses separate one-way boundaries only where policy or outcome settlement must be monotonic. + +### One resolver defines the tool view + +The private resolver applies the current presentation mode, live global restrictions, exact local overlay, and local shadowing. Schemas, lookup, execution, Code Mode SDK generation, and restriction validation all use that resolver or its pre-restriction global-name view. + +The [subagent composition-controls RFC](../feature/2026-07-12-subagent-persona-tool-filter-and-depth.md#tool-filtering-is-one-live-global-view-rule) owns the user-visible allow/deny semantics. The implementation requirement is agreement: a filtered-away global cannot remain executable through a different lookup path, and a locally shadowed definition is the same definition presented and executed. + +`ToolRestriction` accepts readonly allow/deny names and compiles them into internal sets. Multiple restrictions intersect. Public `visible()` and `knownNames()` methods are unnecessary because only the registry needs the intermediate views. + +### Tool execution owns identity and boundary materialization + +The registry assigns every execution a fresh branded `Symbol` token. Nested Code Mode calls carry the outer token as `parent`, so structured output can correlate an inner capture with its enclosing `run_code` result by identity. + +A fresh registry-assigned Symbol provides collision-free execution identity without a WeakSet membership registry. Callers cannot supply the execution's own token through `ToolExecutionInput`; they only receive the pipeline-owned `ToolExecution` after the registry creates it. This is a trusted typed contract, not a runtime defense against arbitrary casts or JavaScript callers. + +Arguments are materialized once where model/tool JSON enters the pipeline. Pre-, around-, and post-execute listeners operate on the typed execution and decisions. Call ID correlation, approval, monotonic guards, and Code Mode nesting remain explicit relational checks. + +After the last post-execute listener, the registry materializes and freezes the accepted final result once. Every synchronous `tools/result` observer receives that exact committed object, and observer failures are contained individually. An outer pipeline failure is normalized into a committed error result, so observers can discard staged work against the same authoritative boundary. + +### The assembly waterfall owns the final model-visible composition + +SystemPrompt first resolves the global-plus-agent sections, variables, and tool providers into a deterministic registry contribution. The scope-filtered `system-prompt/assemble` waterfall may then reorder, replace, add, or remove any section, variable, or schema. Its returned assembly is authoritative; there is no later restoration pass and no finality metadata on ordinary prompt sections, tool definitions, or provider results. + +This is a trusted same-process extension seam, not an authority boundary. A listener that changes Code Mode's `run_code` schema or `tools:sdk` instructions, or a structured child's capture schema or instruction, owns preserving a coherent protocol in the assembly it returns. ToolRegistry still reserves `run_code` against ordinary tool registration and restriction because those are registry invariants, but assembly middleware remains free to transform the final model-visible surface. + +Scope solves the real isolation problem directly. Structured-output contributions register in the child's exact scope, while Code Mode derives its transport and SDK from the same resolved tool view. A second named-protection system would need another ownership and collision rule across arbitrary schema providers—including providers that intentionally contribute duplicate names—without creating a new trust boundary. + +### Structured output commits only authoritative outcomes + +Structured output combines child-scoped composition with a two-phase execution commit. The child registers its `structured_output` tool and instruction before publication; a trusted assembly listener may transform those ordinary contributions and is responsible for preserving the protocol if the child is expected to complete. The tool body validates a candidate and stages it by the current `ToolExecution`, but successful capture is decided only by immutable `tools/result` observations. + +For a native call, the observer deletes the stage and commits its value only when that exact execution's final result succeeds. A post-execute block or outer pipeline failure therefore cannot leave a captured value behind. + +For a Code Mode SDK call, the inner successful result records `{ parentToken, value }` rather than committing. The observer waits for the `run_code` execution whose token matches `parentToken` and commits only if that outer final result also succeeds. Program failure, runtime abort, or outer post-policy denial discards the pending value. + +Once a value is pending or committed, a scoped monotonic guard denies later tool calls. After commit, the ordinary serial `agent/turn-stop` listener returns a stop decision after continuation and steering have already folded. A schema-validation failure remains an ordinary `INVALID_ARGS` tool error and leaves the child able to retry within the same turn. + +Pure Code Mode's registry contribution omits `structured_output` from native wire schemas and exposes it through the generated SDK. The assembly waterfall may deliberately change that presentation; execution still validates against the child-scoped definition, and the listener owns the consistency of any alternate model-visible route it creates. + +### Three execution boundaries are deliberately one-way + +Prompt assembly is intentionally cooperative, but three execution facts need one-way settlement after their extensible stages: + +| Boundary | Final power | Why ordinary listener order is insufficient | +|---|---|---| +| Tool pre-policy | Deny monotonically | A later listener must not re-allow an already denied call | +| Tool result | Observe the immutable committed outcome | Structured output must commit only the result that actually escaped the pipeline | +| Turn continuation | Stop after ordinary continuation folding | A committed terminal output must end the turn | + +`ToolGuard` is the monotonic policy registry. Committed tool observation is the contained `tools/result` point described above. Terminal structured output listens on the ordinary serial `agent/turn-stop` fold after normal continuation and steering decisions; no public `strictSerial()` dispatcher is needed for the typed listener contract. + +### Skill and approval services trust typed callers + +Skill registry definitions and approval policies are readonly same-process contracts. Their services do not clone callback objects or defend against post-handoff callback replacement. + +Skill still validates external skill files and parsed provider output, routes catalogs through the calling agent's tool view, and disposes registrations exactly. Approval still resolves policy, observes cancellation, routes `approval/request` by `request.agent`, records the durable audit pair, and contains answerer and post-commit observer failures. + +## Subagents: readiness is the start promise + +Subagent startup has one ownership transfer. The provider owns partial resources until its start promise fulfills with a ready published run; the caller owns the returned run and must dispose it. + +### The service contract has one cancellation channel + +`SubagentProvider.start()` and `SubagentService.start()` return `Promise`. The promise fulfills only after the backend has established the child it promises, so callers and `subagent/start` observers never need a second `run.started` readiness promise. + +`SubagentStartRequest.signal` is required. Aborting it requests cancellation during startup and after readiness. `SubagentRun.dispose()` also requests cancellation and awaits quiescence. There is no separate public `run.cancel()` channel. + +Optional `sendMessage()` supports a live backend that can accept steering. Optional `resume()` returns `Promise` because the resumed child has the same asynchronous readiness boundary. + +The service validates provider capabilities and request semantics before calling the provider. A provider rejection cleans any partial resources before the rejection escapes and emits no `subagent/start`/`subagent/end` pair. After fulfillment, the service attaches result observation, emits scoped start, and returns the run. Provider removal prevents later starts but does not revoke a run already accepted by the provider. + +### In-process providers reuse the core transaction + +Spawn and fork share one in-process driver. It creates the child through `parent.ctx`, passes the required signal into the core creation transaction, and installs persona, tool restriction, and structured-output contributions during unpublished setup. + +The provider awaits creation and returns only the published run. At the handoff, core creation detaches its creation-only abort listener; the provider immediately rechecks the signal before installing the live-run listener, so an abort in that narrow interval disposes the new handle instead of escaping cancellation. Parent teardown follows the child because the operation belongs to `parent.ctx`; provider unload blocks new starts but does not become a second revocation owner for accepted runs. The run disposer cancels the child and awaits the AgentHandle's ordered teardown. + +Spawn uses an empty session seed. Fork uses a validated completed-turn prefix. Conversation seeding changes history only and does not import scope, tools, services, or authority. + +### ACP providers own the process until readiness or cleanup + +An ACP provider crosses a real process and wire boundary, so it retains validation, environment scrubbing, message serialization, abort/process races, and kill-to-exit quiescence. + +Start resolves only after `initialize` and `newSession` succeed. Abort, spawn failure, RPC failure, or invalid startup response reaps the process before rejection. After readiness, result maps the ACP prompt outcome and streamed output; dispose requests cancellation, closes the connection, and awaits process exit through one memoized path. + +## Workflows and ACP UI: retain only independent async facts + +Worker and editor bridges need more state than same-process registries because messages, process death, and rendering can settle independently. Their state is organized around those real facts rather than duplicate cancellation protocols. + +### Workflow children are pending starts or published records + +The workflow host keeps pending provider-start promises and published child records. A child moves from pending to published only when async `SubagentService.start()` fulfills; rejected starts clean their partial provider work and produce no child lifecycle pair. + +One host-owned AbortController supplies the required signal to pending and live children. Closing workflow admission aborts that signal, so there is no duplicate `ChildCancel` worker RPC or explicit host-side `run.cancel()` fanout. Quiescence waits for both pending starts and published child disposal. + +The worker boundary still serializes requests and outcomes. The host retains first-terminal-outcome arbitration, exact child accounting, worker-death handling, grace termination, late/duplicate message rejection, and bounded cleanup because result receipt, worker exit, and child quiescence are genuinely independent facts. + +### Terminal result and physical cleanup remain separate + +The workflow result records the first accepted terminal outcome according to the public precedence rules. Cleanup can continue after that result is chosen: live children still need disposal, a worker still needs termination, and a slow external backend may outlive the configured grace bound. + +Public disposal claims its memoized promise before invoking callbacks. Worker death closes admission before processing any queued late child request, synthesizes missing lifecycle ends, and starts child/process cleanup without rewriting an outcome already claimed. + +### ACP prompt settlement does not depend on rendering success + +The ACP UI correlates a prompt with its observed turn directly. It does not scan from a `logWatermark` or use session status as a second reconciliation oracle. + +Prompt handling settles correlation in a `finally` around transcript rendering. A rendering failure can fail presentation, but it cannot skip prompt settlement or leave the session permanently in flight. Concurrent loads of the same persisted caller-supplied session ID remain excluded because that is a real persistence identity race, not a UUID collision concern. + +## Correctness enforcement + +The design is enforced at types, runtime escape points, generated contracts, and behavioral tests. No one layer is asked to prove what it cannot observe. + +### Types make the ordinary path hard to misuse + +Readonly contracts describe borrowed same-process values. `Scoped` marks event receivers, `agentEvents()` fuses carrier and subject, tool inputs omit registry-owned tokens, and subagent async return types expose readiness directly. + +TypeScript cannot govern JavaScript casts, direct Cordis dispatch, process messages, or durable files, so runtime enforcement remains at those escape points. + +### Runtime invariants cover cross-service facts + +The invariants plugin verifies that every declared scoped event uses a marked carrier and that event families exposing a subject use the matching key. Session trace validation stages before append commit and advances after the same event commits. + +The plugin does not police trusted setup by scanning registries or reject prompt assembly objects fabricated through casts. Those checks would turn composition contracts into speculative runtime machinery without protecting a real external boundary. + +### Generated artifacts keep public contracts aligned + +The event catalog, service catalog, producer/consumer matrix, configuration catalog, module graph, tool catalog, and type-equivalence blocks are generated or freshness-gated from source. `verify-scoped-dispatch` keeps the declared scoped-event set aligned with runtime invariant coverage. + +Behavioral tests pin scoped routing and disposal, final-entry collision cleanup, publication rollback, ordered quiescence, durable pre/post-commit behavior, live tool filtering across presentation and execution, cooperative prompt assembly, structured-output commit in native and Code Mode, async subagent startup and signal cancellation, worker terminal arbitration, ACP settlement, and process teardown. + +## Alternatives considered + +The [July 8 RFC](2026-07-08-agent-scope-contexts.md#alternatives-considered) owns alternatives to the public flat-scope contract. The alternatives here concern implementation shape. + +### Use a transparent proxy as the scope carrier + +A proxy that impersonates the subject must preserve property, callable, constructable, private-field, descriptor, and proxy-invariant behavior that listener routing never needs. A small opaque carrier keeps the filter and key while the explicit event argument carries the subject. + +### Reserve agent and session IDs before setup + +Reservations prevent duplicate private setup work but require cross-service capabilities, release ordering, abandoned-reservation cleanup, and prepared-object binding. IDs are caller-supplied and concurrent reuse is caller error; final entry can choose the winner while the losing transaction rolls back cleanly. + +### Snapshot every typed same-process argument + +Universal copying defends against stateful getters and callers that violate readonly contracts, but it adds allocation, duplicated validators, and paths that can forget to copy. Materialization belongs at parser, queue, model, durable, worker, process, and wire boundaries where ownership actually changes. + +### Give readiness, cancellation, and disposal separate controllers + +Parallel sentinels can all mirror whether one operation is live. One transaction or start promise owns the operation; separate promises remain only where publication unwind, external work, terminal result, and physical quiescence can settle independently. + +### Keep synchronous subagent start plus `run.started` + +This splits provider acceptance from readiness and forces every consumer to register a partial run, attach result observation, await readiness, and clean up readiness failure. An async start promise makes provider-to-caller ownership transfer the readiness boundary itself. + +### Restore selected prompt or tool contributions after assembly + +A post-waterfall restoration pass would create a second composition rule after the documented cooperative seam. Correctly assigning canonical presence or absence would also require provider ownership and collision rules for arbitrary tool-schema providers, whose ordinary output may contain duplicate names. Scoped registration already supplies the required per-agent isolation, and trusted assembly listeners own the protocol consistency of what they return, so named restoration adds machinery without establishing an independent boundary. + +### Remove worker/process lifecycle guards with same-process hardening + +Worker messages, process death, and durable input do cross ownership and serialization boundaries. First-outcome arbitration, validation, environment scrubbing, and quiescent process cleanup remain necessary even though hostile same-process callback machinery does not. + +## Consequences + +The implementation is smaller and its proof follows the same shape as its ownership graph. One key selects a layer, one entry owns a live registry object, one transaction owns creation, one resolver owns a tool view, and one async promise transfers subagent ownership. + +### What the design guarantees + +- A scoped contribution is visible only in its exact agent view and is disposed with that scope. +- Create and resume expose no partially configured handle; final-entry losers and publication failures clean every prepared resource. +- Disposal retains scoped listeners and persistence through driver drain and final session work, then revokes the scope. +- Durable, queued, model, worker, process, and wire values are owned at their real boundary; typed same-process values follow readonly contracts. +- ToolRegistry's presentation, lookup, and execution resolve the same live view before expert assembly transforms, and committed results have one immutable observation point. +- Registry contributions are deterministic inputs, while the trusted assembly waterfall owns the final model-visible composition. +- Subagent start returns only a ready run, required signals cancel pending or live work, and disposal reaches the backend's quiescence contract. +- Worker/process result precedence and cleanup remain correct under death, late messages, and bounded teardown. + +### Costs and limits + +Scope-aware services still maintain global and identity-keyed maps, and operations must carry their real agent explicitly. Async create/resume and subagent start require callers to await ownership transfer and dispose returned handles. + +A trusted `system-prompt/assemble` listener can remove or replace Code Mode and structured-output protocol pieces. This is deliberate: the listener owns final composition and must preserve any protocol the deployment expects to remain usable. + +The design trusts typed plugins in the same process. It does not defend against arbitrary casts, stateful getters, mutation that violates readonly contracts, or a plugin deliberately using ambient service access outside the supported composition API. + +The [security and authority non-goal](2026-07-08-agent-scope-contexts.md#security-and-authority-are-non-goals) remains fundamental. These mechanisms prove registration composition, publication, and lifetime ownership; they do not prove confinement or parent-to-child non-escalation. diff --git a/docs/rfc/implemented/feature/2026-06-14-acp-agent-client-protocol.md b/docs/rfc/implemented/feature/2026-06-14-acp-agent-client-protocol.md new file mode 100644 index 0000000000..293b1bbde6 --- /dev/null +++ b/docs/rfc/implemented/feature/2026-06-14-acp-agent-client-protocol.md @@ -0,0 +1,57 @@ +# RFC: Agent Client Protocol (ACP) support — drive the coding agent from external editors + +Status: implemented + +## Problem + +The harness originally exposed agents only through a readline loop. That surface could carry text, but it gave an editor no structured way to create or resume sessions, correlate prompt completion, stream reasoning and tool activity, render tool-specific UI, ask for permission, or cancel one conversation without disturbing another. ACP defines those interactions as JSON-RPC over stdio, and Zed is the target client used to make concrete compatibility decisions. + +The bridge must preserve the harness's existing ownership boundaries. It cannot depend on the concrete agent loop, bypass the tool registry, execute shell commands in the editor, or invent a second source of session truth. stdout is also the protocol transport, so any accidental log output corrupts the connection. + +## Decision + +`@deepseek-ai/dsh-acp` is a UI/client-driver plugin under `packages/ui/acp`. It uses `@agentclientprotocol/sdk`'s `AgentSideConnection` over stdin/stdout and programs only interface services: the agent create/resume factory, session persistence, tool registry, user interaction, and optional approval/bash capabilities. It does not change the agent loop and is not a capability-seam implementation. + +The bridge implements the following stable session path: + +- `initialize` negotiates the protocol version, advertises text plus `resource_link` prompts, and advertises `loadSession`. +- `session/new` validates an absolute `cwd`, stores it in `SessionHeader`, creates an agent through `ctx.agents`, and returns any composition-backed config options. +- `session/load` validates the requested cwd against persisted metadata before constructing an agent, reserves the id across the asynchronous resume, replays user/assistant/tool events as ACP updates, and reports the resumed config-option fold. +- `session/prompt` accepts text and resource links, rejects unsupported or empty content, allows one in-flight prompt per session, and settles against that prompt's owning `turn/end`. An error turn rejects the RPC; other closed turn reasons map through a total ACP stop-reason codec. +- `session/cancel` calls the queue-aware agent cancel path and settles only the addressed session's prompt. + +Tool-call presentation remains tool-owned. A tool's `presentCall` and `presentResult` return the `generic`, `terminal`, or `diff` render-intent variants; the bridge switches on that union and maps it to ACP. Presenter-less tools receive a generic fallback. Bash terminal cards use Zed's capability-gated `_meta.terminal_info`, `_meta.terminal_output`, and `_meta.terminal_exit` convention; the harness still executes the command through `ctx.bash`, preserving sandbox, environment scrub, ownership, and cwd. Clients without that extension receive ordinary text content. Filesystem tools provide diff cards and file locations without hard-coded tool-name branches in the bridge. + +Permission handling is an answerer on the [user-approval seam](2026-07-06-approval-seam.md), not an ask-every-tool policy in ACP. An `approval/request` for a bridge-owned agent with a call id becomes `session/request_permission` on that agent's editor session, with one-shot allow/reject choices. Foreign or call-less requests delegate; a missing or failed answerer remains fail-closed. The plugin that asks—such as a pre-execute policy or bash escalation—owns the decision to ask. + +The bridge advertises ACP config options instead of session modes. `sandbox-mode` exists only when the mounted bash executor reports sandbox capability, and `approval-policy` exists only when `ctx.approval` is composed. Each option is an independent select whose current value is the session event fold over the composition default. `session/set_config_option` validates against the owning domain vocabulary and writes through `setSandboxMode` or `setApprovalPolicy`. An open-turn switch appends immediately; an idle switch is overlaid in the response and anchored at the next turn start. Until that anchor it is memory-only and a crash reverts to the durable fold. ACP session modes are deliberately not modeled because one mode list cannot represent these orthogonal knobs and config options are the forward protocol surface. Runtime model selection remains outside this decision; `AcpConfig.model` is connection-wide. + +The bridge also provides the ACP-backed `UserInteractionProvider`: `ask_user_question` requests become form elicitations on the owning session. Select, multi-select, option descriptions, and custom-answer override semantics are preserved. + +Lifecycle ownership is explicit. The bridge holds an `AgentHandle` per live session. Disconnect and Cordis disposal cancel pending prompts, dispose every handle in parallel, await loop quiescence and persistence flush, and then remove the records. Stream notification failures are contained so a vanished client cannot corrupt an agent turn. The ACP app composition loads no stdout logger; a test guards stdout as framed JSON-RPC only. + +The precise supported and deferred protocol rows live in [`packages/ui/acp/acp-feature-support.md`](../../../../packages/ui/acp/acp-feature-support.md); the package README is the operational contract. + +## Alternatives considered + +**A prepended `tools/execute` listener that asks on every ACP-owned call** — rejected. It would hard-code permission policy into the UI bridge, ask even when no policy requires it, and could not serve approval requests that arise after execution begins. The shared user-approval seam keeps mechanism, asking policy, and UI answerer separate. + +**Inject the concrete `agentLoop`** — rejected. Agent creation, resume, idle observation, and disposal are interface-level ownership operations on `dsh-agent`; a UI plugin does not need a dependency-rule exception. + +**Execute bash through ACP `terminal/*`** — rejected. That would move execution outside the harness and bypass its sandbox, credential scrub, task ownership, cwd resolution, and session log. Terminal metadata is presentation only. + +**Represent sandbox and approval as ACP session modes** — rejected. They are independent composable settings, while a single current mode is mutually exclusive. ACP config options represent both without a cross-product and match the protocol's forward direction. + +**Hijack stdout defensively** — rejected. Process-wide monkey-patching is outside Cordis effect ownership and races the protocol transport. The app composition owns stdout purity. + +## Consequences + +Editors can create, load, prompt, cancel, render, ask, and reconfigure multiple harness sessions over one ACP connection without a loop-specific dependency. The session event log remains the durable source for replay, prompt settlement, cwd, and per-session configuration. Tool presentation and human-answer channels remain extensible plugin contracts instead of ACP-specific behavior. + +The bridge deliberately does not implement session list/delete/resume/close capabilities, MCP passthrough, additional directories, image/audio/embedded-resource prompts, runtime model selection, plans, slash commands, usage updates, editor filesystem delegation, or the ACP terminal execution sub-protocol. The feature checklist records these as unsupported rather than silently accepting them. + +An idle config selection is truthful in the live response but not durable until the next turn anchors it. Crashing before that boundary loses the pending selection; this is the cost of keeping session events turn-enclosed and replay-safe. + +## Verification + +The ACP suites cover the in-memory protocol codec, create/load replay, exact prompt settlement, cancellation races, unsupported content, tool presentation, terminal capability fallback, permission outcome mapping, config-option validation and persistence, multi-session isolation, disconnect/disposal quiescence, and HMR cleanup. Snapshot and built-bin tests exercise the app composition, while the real-API e2e self-skips without a key. diff --git a/docs/rfc/implemented/feature/2026-06-14-acp-multi-session.md b/docs/rfc/implemented/feature/2026-06-14-acp-multi-session.md new file mode 100644 index 0000000000..77fa2b4669 --- /dev/null +++ b/docs/rfc/implemented/feature/2026-06-14-acp-multi-session.md @@ -0,0 +1,37 @@ +# RFC: Multiplex concurrent ACP sessions over one connection + +Status: implemented + +## Problem + +An ACP editor can keep several conversations alive over one agent subprocess. A single-active-session bridge would force extra processes and would not match Zed's client model, which tracks multiple session ids and concurrent loads. Multiplexing introduces isolation risks: events, prompt completion, cancellation, permission prompts, config selections, and predictable background-task ids must never cross session boundaries. + +## Decision + +The ACP bridge stores live sessions in `Map` and keeps a `WeakMap` reverse index for agent-scoped callbacks. A record owns its agent handle, in-flight prompt, live tool-call presentation state, pending idle config switches, session cwd, and client capability snapshot. A separate loading-id set reserves each id before asynchronous resume so two pipelined loads cannot construct duplicate agents; distinct ids may load concurrently. + +Every `session/event` and `agent/status` callback resolves the owning record before sending or settling anything. Each session permits one in-flight prompt independently. The prompt records a log watermark, captures its own `turn/start`, and settles only on the matching `turn/end`; a late end from a cancelled prior turn cannot resolve a newer prompt. `session/cancel` addresses one record and calls only that agent's queue-aware cancel path. + +Permission ownership uses the same reverse index. The ACP `approval/request` answerer prompts only the editor session that owns the requesting agent and delegates foreign requests. User-interaction elicitations likewise route by agent ownership. Per-session sandbox and approval config values fold only that session's events, with pending idle switches stored on that record until the next turn anchors them. + +Background bash tasks carry an opaque owner token equal to the owning session id. `bash_output` and `bash_kill` compare the caller's token with the executor's task ownership before reading or killing; a predictable task id alone grants no access. Ownership is stored with the executor task, so a tool plugin reload does not erase it. + +Connection teardown clears the live map, settles each pending prompt as cancelled, and disposes all `AgentHandle`s in parallel. Each handle stops and awaits its loop, flushes the session while attached, unregisters the agent, and removes the session. Teardown is memoized and shared by client disconnect and plugin disposal. + +## Alternatives considered + +**One live session per connection** — rejected. It adds process overhead and contradicts the target client's multi-session shape without removing multiplexing needs from the editor. + +**A per-session `ctx.extend()`** — rejected. A child context does not by itself create a child plugin fiber, so listeners would still belong to the bridge fiber. The implemented bridge instead uses global listeners with explicit O(1) demultiplexing and per-session owned records; agent lifecycle is owned by `AgentHandle`. + +**Agent object identity as bash-task ownership** — rejected. A resumed or replaced agent object may legitimately represent the same durable session. The opaque session token is the cross-boundary identity that should survive plugin reloads. + +## Consequences + +N sessions can stream, prompt, request permission, switch config, and run background tasks concurrently without interleaving or cross-settling. A cancel or dispose in one session does not affect its neighbors. The bridge pays for explicit maps and isolation tests, but it does not add one listener set per session and therefore avoids listener fan-out during long-lived connections. + +The bridge still exposes no protocol method to close one live session independently. Today records leave together on connection teardown; session close/resume lifecycle capabilities remain deferred in the ACP feature checklist. + +## Verification + +The multi-session suite drives concurrent sessions through interleaved updates, independent in-flight prompts, targeted cancellation, same-id and distinct-id load races, permission routing, config isolation, and teardown. Tool-bash tests prove one session cannot read or kill another session's background task. diff --git a/docs/rfc/implemented/feature/2026-06-15-code-mode.md b/docs/rfc/implemented/feature/2026-06-15-code-mode.md index 4cc373c65c..60acfa7c88 100644 --- a/docs/rfc/implemented/feature/2026-06-15-code-mode.md +++ b/docs/rfc/implemented/feature/2026-06-15-code-mode.md @@ -4,47 +4,49 @@ Status: implemented ## Problem -Today the agent loop advertises every registered tool to the model as a native JSON-schema function definition. `ToolRegistry` contributes its schemas to the system-prompt assembly, the assembly's `tools` land on the wire (and in the logged request header), the model invokes one `tool-call` block per step, and the loop dispatches each call through `ctx.tools.execute()` **sequentially** (parallel tool execution is an explicit open TODO in `dsh-tools` and [docs/architecture.md](../../../architecture.md)), with **every** intermediate `tool-result` re-entering the model's context on the next request. +In the registry's native presentation, the agent loop advertises every visible capability as a JSON-schema function definition. `ToolRegistry` contributes its schemas to the system-prompt assembly, the assembly's `tools` land on the wire (and in the logged request header), the model invokes one `tool-call` block per step, and the loop dispatches each call through `ctx.tools.execute()` **sequentially** (parallel tool execution is an explicit open TODO in `dsh-tools` and [docs/architecture.md](../../../architecture.md)), with **every** intermediate `tool-result` re-entering the model's context on the next request. For multi-step tool work this is token-heavy and serial. The model cannot compose tools — loop over a result set, branch on an intermediate value, fan out, post-process — without a full model round-trip per call, and each round-trip drags the entire intermediate result back into context whether the model needs it or not. Cloudflare's [Code Mode](https://blog.cloudflare.com/code-mode/) proposes an alternative grounded in a simple observation: LLMs are better at writing code than at emitting tool calls, because they have seen millions of lines of real code and comparatively few contrived tool-calling traces. Instead of one tool call per step, the model writes a TypeScript program against a generated API over the tools, the program executes in a sandboxed runtime, and the model curates what comes back — only what it prints or returns — instead of every intermediate result. -An earlier draft of this RFC designed Code Mode as an add-on consumer plugin with zero core changes, deferring the execution substrate to a follow-up. Both constraints are dropped here, deliberately. First, the harness is pre-release and optimizes for the correct foundation over blast radius: tool presentation is the registry's own concern, and bolting a second presentation onto it from outside means transforming the registry's contribution after the fact — a waterfall listener whose correctness depends on listener ordering, which fights the [reconstructable-requests](../../implemented/architecture/2026-07-05-reconstructable-requests.md) design instead of riding it (that refactor removed request mutation from `agent/request`, the seam the old draft relied on). Second, the substrate question is answerable now: a Node `worker_threads` runtime gives real containment — separate isolate, empty environment, heap caps, and a `terminate()` that reliably stops a hot synchronous loop — where the old draft's `node:vm` stub had none of those, and it fits the harness's existing trust model (§Trust posture) without a hardening follow-up. +Tool presentation belongs to the registry that owns tool visibility: implementing a second presentation as an after-the-fact waterfall transform would make correctness depend on listener order and fight [reconstructable requests](../../implemented/architecture/2026-07-05-reconstructable-requests.md). The execution substrate is also part of the foundation rather than a placeholder: Node `worker_threads` provides a separate isolate, an empty environment, heap caps, and termination of a hot synchronous loop, while fitting the harness's existing trust model (§Trust posture). ## Decision Three decisions, each elaborated in its own section below: -1. **Code Mode is a first-class presentation mode of `ToolRegistry`** (`dsh-tools`), selected by a validated `mode` config: `'native'` (today's behavior, the default), `'code'` (the wire carries exactly one tool, `run_code`, plus a generated SDK `.d.ts` in the system prompt), or `'both'` (native schemas *and* `run_code` + SDK). The registry's existing tool-schema provider contributes whatever the mode dictates, so the wire tool list is shaped at its source — no interception, no listener-ordering caveats — and the logged request header records it for free. -2. **Code execution is a capability seam** — a new group `packages/code-runtime/` with the interface package `@deepseek-ai/dsh-code-runtime` owning `ctx.codeRuntime` ([capability seams](../../implemented/architecture/2026-06-13-capability-seams.md); consumer = `dsh-tools`, with core-consumes-a-seam precedent in `agent-loop` → `dsh-llm`). The runtime knows nothing about tools: it is handed a program and named async bindings, runs the program, and reports `{ value, logs, error? }`. Language and substrate are backend properties, so a future Python or container backend is a new implementation package, not a redesign. +1. **Code Mode is a first-class presentation mode of `ToolRegistry`** (`dsh-tools`), selected by a validated `mode` config: `'native'` (the default, contributing the visible capability schemas), `'code'` (the registry contributes only its reserved `run_code` transport plus a generated SDK `.d.ts` in the system prompt), or `'both'` (native schemas and the transport + SDK). The registry shapes its canonical contribution at the source; the cooperative prompt-assembly result remains authoritative, and the logged request header records exactly that returned presentation. +2. **Code execution is a capability seam** — `packages/code-runtime/` contains the interface package `@deepseek-ai/dsh-code-runtime`, which owns `ctx.codeRuntime` ([capability seams](../../implemented/architecture/2026-06-13-capability-seams.md); consumer = `dsh-tools`, with core-consumes-a-seam precedent in `agent-loop` → `dsh-llm`). The runtime knows nothing about tools: it is handed a program and named async bindings, runs the program, and reports `{ value, logs, error? }`. Language and substrate are backend properties, so a future Python or container backend is another implementation package, not a redesign. 3. **The shipped implementation is `@deepseek-ai/dsh-code-runtime-worker`**: one fresh Node worker thread per run, executing the model's TypeScript after type-strip, with bindings bridged over the message port, an empty environment, configurable heap/output/time caps, and hard termination. Its trust posture is bash-equivalent by design — no unsafe-acknowledgement flags — because the harness already ships `dsh-bash-local`, which executes arbitrary model-written shell commands with strictly *more* ambient authority. ### The registry owns the mode `ToolRegistry` gains a schemastery-validated config (`static Config`), its first: `mode: 'native' | 'code' | 'both'`, default `'native'`. A deployment flips it from `cordis.yml` (`tools: { mode: code }`) — no code edit, per the no-hardcoded-tunables convention. -**Wire tool list = the registry's contribution.** The registry already feeds the assembly through `ctx.systemPrompt.tools(() => this.schemas())`; the provider becomes mode-aware: `'native'` contributes all schemas (unchanged), `'code'` contributes only `run_code`'s schema, `'both'` contributes all schemas plus `run_code`. Because [`PromptAssembly.tools` is the single source the loop's request header snapshots](../../../../packages/core/system-prompt/src/index.ts), the collapse is automatically logged and reconstructable — model-visible ⟺ logged holds with zero new mechanism. Scope of the guarantee, stated honestly: the mode governs the **registry's** contribution, and the registry is the only shipped schema source — but `systemPrompt.tools()` is a public multi-provider API and the `system-prompt/assemble` waterfall may transform the assembly, so a deployment that wires a second direct provider (or a mutating listener) owns what it adds, exactly as in native mode. Those are deliberate acts; what the design eliminates is the *accidental* leak the old draft worried about — a listener-ordering race around an after-the-fact collapse — and the shipped-configuration invariant (`'code'` ⇒ assembled tools exactly `[run_code]`) is pinned by tests and, like every request, by the logged header. +**Wire tool list = the registry's contribution before cooperative assembly.** The registry feeds assembly through a mode-aware provider: `'native'` contributes every capability visible to that assembly scope, `'code'` contributes only `run_code`, and `'both'` contributes both. Because [`PromptAssembly.tools` is the single source the loop's request header snapshots](../../../../packages/core/system-prompt/src/index.ts), the final presentation is logged and reconstructable. The reserved transport is not a capability: it lives outside global/scoped registration and restriction layers, cannot be registered or shadowed there, and cannot be named by `ctx.tools.restrict()`. The mode governs this provider's input to assembly; other direct `systemPrompt.tools()` providers own their schemas, and the trusted assembly waterfall owns the returned wire list. -**Interaction with `toolOrder`, stated up front:** a configured `systemPrompt.toolOrder` naming native tools rejects every assembly under `mode: 'code'` (those names are no longer contributed), by the existing fail-loud rule for unlisted names. This is correct behavior, not a bug: a deployment switching modes updates its order config or drops it. +**Interaction with `toolOrder`, stated up front:** a configured `systemPrompt.toolOrder` naming native capabilities rejects every assembly under `mode: 'code'`, because those names are outside that mode's wire-validation universe. This is correct behavior, not a bug: a deployment using Code Mode updates its order config or drops it. -**The SDK prompt section.** Under `'code'` and `'both'` the registry registers one lazy prompt section (`tools:sdk`, in the 100–199 tool-guidance order band) whose thunk regenerates, at each assembly, a TypeScript declaration of every registered tool except `run_code` itself, plus fixed usage instructions. The thunk reads the live store and emits tools in lexicographic name order, so its output is deterministic and stable across steps — an unchanged tool set produces byte-identical text (prefix-cache-friendly; a mid-session registration surfaces as one logged header delta, exactly like a native-mode tool change). +**The SDK prompt section.** Under `'code'` and `'both'` the registry registers one lazy prompt section (`tools:sdk`, in the 100–199 tool-guidance order band) whose thunk regenerates, for each assembly scope, a TypeScript declaration of every visible end-capability tool plus fixed usage instructions. It uses the same visibility resolver as lookup and execution, so scoped grants and shadows appear while restricted globals disappear; the reserved `run_code` transport itself is excluded. The thunk emits tools in lexicographic name order, so an unchanged visible set produces byte-identical text. + +**Assembly ownership.** `run_code` and `tools:sdk` enter the trusted `system-prompt/assemble` waterfall as normal assembly inputs. A scoped `tools:sdk` section may shadow the global default before dispatch, and a listener may remove or replace either contribution. The waterfall's returned assembly is final, so whoever changes these inputs owns preserving a viable Code Mode protocol when the deployment expects Code Mode to remain usable; no restoration pass overrides deliberate composition. **Codegen.** A pure `jsonSchemaToTs(schema)` module inside `dsh-tools` (sibling of `json-schema.ts` — `schemas()` and the SDK are two projections of the same store) maps the JSON-Schema subset the `defineTool` DSL emits (object/string/number/boolean/array, `properties`, `required`, string `enum` → literal union, nested objects, array `items`, `description` → JSDoc) to a TS type literal. It is **total**: any construct outside that subset (`$ref`, `oneOf`/`anyOf`, `integer`, future MCP shapes, …) degrades to `unknown` without throwing. Because `ToolSchema.name` is an arbitrary string, the SDK is declared as one object constant — `declare const tools: { "some-mcp-tool"(args: …): Promise; bash(args: …): Promise; … }` — quoted keys make every name reachable with no sanitization or alias-collision logic. Typing is advisory (the runtime executes type-stripped JS); the instructions say so. ### The run_code tool and the dispatch bridge -Under `'code'` and `'both'` the registry registers `run_code` in itself as an ordinary tool — one required parameter `{ code: string }` — so the unchanged loop dispatches it through the normal pipeline and `tools/pre-execute` / `tools/post-execute` gate it like any other call (a permission plugin can inspect the program text before it runs). Its `execute(args, exec)`: +Under `'code'` and `'both'` the registry owns `run_code` as a reserved presentation transport with one required parameter, `{ code: string }`. It is represented by a normal `ToolDefinition` for dispatch but stays outside the filterable capability layers, so restrictions cannot accidentally remove Code Mode's only entry point. Calls traverse the complete tool pipeline — `tools/pre-execute` → monotonic guards → `tools/execute` around dispatch → `tools/post-execute` → immutable `tools/result` notification — exactly like native calls; a permission plugin can inspect the program text before it runs, and final-result observers see the normalized outer outcome. Its `execute(args, exec)`: -1. **Builds the bindings**: the bridge owns a **run-scoped `AbortController`** whose signal follows `exec.signal` (an outer cancel propagates in) and which the bridge itself fires the moment the run settles for any reason — completion, program exception, `computeMs`/`maxWallMs` expiry, worker exit. For every registered tool except `run_code`, the binding is an async function that (a) checks the run signal before and after (throwing stops the program — necessary because `ctx.tools.execute()` converts errors to `isError` data), (b) **JSON-normalizes the argument** — a `JSON.parse(JSON.stringify(args))` round-trip, rejecting that one call with a descriptive `Error` when the value does not survive (`BigInt`, circular structures) — because the seam's structured-clone boundary is wider than JSON while the session log accepts only JSON: normalizing BEFORE dispatch makes the dispatched form and the logged form the same JSON value by construction, so an executed sub-call can never fail at logging time, (c) awaits its turn on the **per-run serialization queue** (below), (d) calls `this.execute({ callId, name, arguments, agent: exec.agent, signal: runSignal })` with a deterministic sub-id `` CallId(`${exec.callId}:code:${n}`) `` — the run signal, not the bare outer one, so a budget expiry aborts an in-flight sub-tool (`bash-local` kills on its spec signal) instead of orphaning it, (e) appends a `tool/code-dispatch` session event, and (f) maps the result: success → the text-block contents joined as a `string` (non-text blocks become placeholders, an MVP limitation), `isError` → **the binding rejects** with an `Error` carrying the result text. Rejection is the deliberate model-facing contract — real code signals failure by throwing, `try/catch` and `Promise.all` short-circuiting behave as every model has seen them behave — where the old draft's `{ output, isError }` envelope made error handling a bespoke convention. -2. **Runs the program**: `ctx.codeRuntime.run({ program: args.code, bindings: [{ global: 'tools', functions }], signal: exec.signal })`. -3. **Surfaces the outcome — after reaching quiescence.** When `ctx.codeRuntime.run()` resolves, the bridge fires the run-scoped abort (cancelling any in-flight sub-dispatch and abandoning queued-unstarted ones), then **awaits the dispatch queue's drain before returning**, per the dispose-to-quiescence rule in [defensive patterns](../../../defensive-patterns.md): an aborted in-flight sub-call still settles and logs its `isError` `tool/code-dispatch` event *inside* the open turn, and nothing can append after `run_code` returns. A successful run then returns one text block — the captured console/stdout output followed by the rendered return value (if any) — plus a `meta` payload (capped logs, dispatch count) for presentation. A run with `result.error` throws a `CodeRunFailedError extends HarnessError` (`code: 'CODE_RUN_FAILED'`, message = the error kind and text plus captured logs so the model can self-correct); the registry's existing catch turns it into a structured `isError` result. +1. **Builds the bindings**: the bridge owns a **run-scoped `AbortController`** whose signal follows `exec.signal` (an outer cancel propagates in) and which the bridge itself fires the moment the run settles for any reason — completion, program exception, `computeMs`/`maxWallMs` expiry, worker exit. For every visible capability tool, the binding is an async function that (a) checks the run signal before and after, (b) **JSON-normalizes the argument** — a `JSON.parse(JSON.stringify(args))` round-trip, rejecting that one call with a descriptive `Error` when the value does not survive (`BigInt`, circular structures) — because the seam's structured-clone boundary is wider than JSON while the session log accepts only JSON, (c) awaits its turn on the **per-run serialization queue** (below), (d) calls `this.execute({ callId, name, arguments, agent: exec.agent, parent: exec.token, signal: runSignal })` with a deterministic sub-id `` CallId(`${exec.callId}:code:${n}`) ``, (e) appends a `tool/code-dispatch` session event, and (f) maps the result: success → the text-block contents joined as a `string` (non-text blocks become placeholders), `isError` → **the binding rejects** with an `Error` carrying the result text. The child's readonly `parent` is only the outer execution's frozen, property-free token, so commit-style observers can correlate outcomes without receiving a mutation path into the live `run_code` wrapper. Every sub-call still traverses the full pipeline under its own immutable identity and registry-assigned token. The run signal, rather than the bare outer one, lets budget expiry abort an in-flight sub-tool instead of orphaning it. Rejection gives programs ordinary `try/catch` and `Promise.all` failure semantics rather than a bespoke result envelope. +2. **Runs the program**: `ctx.codeRuntime.run({ program: args.code, bindings: [{ global: 'tools', functions }], signal: runController.signal })`. The runtime receives the run-scoped signal, not only the caller's outer signal, so any way the outer run settles also aborts work inside the runtime. +3. **Surfaces the outcome — after reaching quiescence.** When `ctx.codeRuntime.run()` settles, whether by fulfillment or rejection, the bridge fires the run-scoped abort (cancelling any in-flight sub-dispatch and abandoning queued-unstarted ones), then **awaits the dispatch queue's drain before returning or propagating**, per the dispose-to-quiescence rule in [defensive patterns](../../../defensive-patterns.md): an aborted in-flight sub-call still settles and logs its `isError` `tool/code-dispatch` event *inside* the open turn, and nothing can append after `run_code` settles. A successful result then returns one text block — the captured console/stdout output followed by the rendered return value (if any) — plus a `meta` payload (capped logs, dispatch count) for presentation. A fulfilled run with `result.error` throws a `CodeRunFailedError extends HarnessError` (`code: 'CODE_RUN_FAILED'`, message = the error kind and text plus captured logs so the model can self-correct); a backend rejection propagates through the same registry error boundary. Both become structured `isError` tool results. **Sub-call `additionalContext` is suppressed, deliberately.** A `tools/post-execute` hook may attach `additionalContext` to a call; for loop-dispatched calls the loop buffers those and appends each as a `context/message` only after the step's `tool/result`s, preserving call/result adjacency. A sub-dispatch result's `additionalContext` has no such safe outlet from inside a running `run_code`: injecting immediately would land a `context/message` between the parent's `tool/call` and its `tool/result` (breaking the adjacency the buffering exists to protect), and `PostToolDecision.additionalContext` is singular where a program may produce many. The MVP therefore drops sub-call `additionalContext`, pinned by a test and stated in the hooks bridge's docs; the follow-up (a plural context channel or loop-level sub-dispatch buffering) is deferred until a real hook needs it through Code Mode. -**Concurrency: serialized, enforced by the binding.** The bindings are async, so a model writing `Promise.all([tools.a(…), tools.b(…)])` starts both immediately — concurrent dispatch would be the *default*, while the tool contract still carries no concurrency-safety metadata (the open parallel-execution TODO). Each `run_code` invocation therefore owns a dispatch queue and every binding call chains onto it, so even `Promise.all` executes the underlying `ctx.tools.execute()` calls one at a time in submission order; when the run settles, queued-but-unstarted dispatches are abandoned. Lifting this per-tool once tools can declare themselves concurrency-safe is deferred work, same as before. +**Concurrency: serialized, enforced by the binding.** The bindings are async, so a model writing `Promise.all([tools.a(…), tools.b(…)])` starts both immediately — concurrent dispatch would be the default, while the tool contract carries no concurrency-safety metadata (the open parallel-execution TODO). Each `run_code` invocation therefore owns a dispatch queue and every binding call chains onto it, so even `Promise.all` executes the underlying `ctx.tools.execute()` calls one at a time in submission order; when the run settles, queued-but-unstarted dispatches are abandoned. Lifting this per tool remains tied to tools declaring themselves concurrency-safe. -**Presentation.** `run_code`'s render intent is decided here per the [render-intent RFC](../../implemented/architecture/2026-07-02-tool-render-intent-union.md): `presentCall` → a `generic` card, `kind: 'execute'`, title `Run code`, `rawInput` = the program text; `presentResult` → a `generic` card whose content is the captured output (from `meta`). Not a `terminal` card: that card's semantics are "a shell command in a working directory", which a program is not. +**Presentation.** `run_code`'s render intent is decided here per the [render-intent RFC](../../implemented/architecture/2026-07-02-tool-render-intent-union.md): `presentCall` → a `generic` card, `kind: 'execute'`, title = the program text, `rawInput` = the same program text; `presentResult` → a `generic` card whose content is the captured output (from `meta`). The program is the title because ACP execute cards reliably render that field while some clients omit body and raw-input content. This is not a `terminal` card: that card's semantics are "a shell command in a working directory", which a program is not. ### Observability: `tool/code-dispatch` @@ -56,12 +58,12 @@ Each sub-dispatch appends one session event, declared by `dsh-tools` via `Sessio - `CodeRunRequest = { program: string; bindings: CodeBindingNamespace[]; signal?: AbortSignal }` - `CodeBindingNamespace = { global: string; functions: Record Promise> }` — the runtime exposes each namespace as a global object of async functions inside the program; binding arguments and resolutions must be structured-cloneable (a runtime may cross a serialization boundary; ours does). -- `CodeRunResult = { value?: unknown; logs: CodeLogEntry[]; error?: CodeRunFailure }` — an error is a field on a resolved result, never a rejection of `run()`. +- `CodeRunResult = { value?: unknown; logs: CodeLogEntry[]; error?: CodeRunFailure }` — program execution outcomes, including exception, timeout, abort, and worker exit, resolve as the `error` field. `run()` may reject only for caller/seam misuse (for example a duplicate binding namespace); consumers still contain a non-conforming backend rejection at their own error boundary. - `CodeLogEntry = { source: 'console' | 'stdout' | 'stderr'; level?: 'log' | 'info' | 'warn' | 'error' | 'debug'; text: string }` - `CodeRunFailure = { kind: 'exception' | 'timeout' | 'abort' | 'worker-exit'; message: string }` — orthogonal outcomes reported independently per [defensive patterns](../../../defensive-patterns.md); a timed-out run is not an exception, an abort is not a timeout. - Two readonly backend descriptors, informational not gating: `language` (what the program must be written in — `'typescript'` for the shipped backend; a Python backend would say so, and pair with its own SDK generator on the presentation side) and `isolation` (`'worker-thread'` for the shipped backend; `'process'`, `'container'`, … for future ones). `dsh-tools` requires `language === 'typescript'` in the MVP — its codegen emits TS — and fails the assembly loudly otherwise, the same misconfiguration idiom as `toolOrder` violations (as when `mode` is non-native with no `ctx.codeRuntime` loaded at all). -Per explicit-over-implicit at seams, the request spells out everything the runtime acts on; defaulting (timeouts, caps) is the implementation's validated config, never a hidden `??` inside `run()`. Consumption uses the loop's established optional-backend idiom: cordis has no optional injection — every `inject` entry gates activation — so a static `inject` on the registry would hold `ctx.tools` (and every tool plugin behind it) hostage to a code runtime existing even under `mode: 'native'`; instead the registry reads `ctx.get('codeRuntime')` at use time, exactly as `agent-loop` consumes `sessionPersistence`, with absence failing loud in the provider thunk as above. The seam split is justified by real planned divergence on both axes — substrate (worker now; container/microVM later) and language (the Python/AssemblyScript direction sketched in the earlier draft survives as future work) — not by speculation: `dsh-tools` consumes the interface today and tests against a trivial in-repo fake, exactly the interface/implementation/consumer shape of the bash template. +Per explicit-over-implicit at seams, the request spells out everything the runtime acts on; defaulting (timeouts, caps) is the implementation's validated config, never a hidden `??` inside `run()`. Consumption uses the loop's optional-backend idiom: Cordis has no optional injection — every `inject` entry gates activation — so a static `inject` on the registry would hold `ctx.tools` (and every tool plugin behind it) hostage to a code runtime existing even under `mode: 'native'`; instead the registry reads `ctx.get('codeRuntime')` at use time, exactly as `agent-loop` consumes `sessionPersistence`, with absence failing loud in the provider thunk. The seam has concrete divergence on both axes: the worker-thread substrate can be replaced by a container or microVM implementation, and the TypeScript language contract can be paired with a language-specific SDK and runtime. `dsh-tools` consumes only the interface and tests against a trivial in-repo fake, exactly the interface/implementation/consumer shape of the bash template. ### The worker-thread runtime @@ -76,7 +78,7 @@ Per explicit-over-implicit at seams, the request spells out everything the runti ### Trust posture -The worker runtime is **containment, not a security boundary**, and the RFC says so without ceremony. Model code in the worker can reach Node globals — `fetch`, `process` (with an empty env), dynamic `import()` of built-ins — so a deliberately adversarial program has ambient authority comparable to what the harness's own `bash` tool already grants every model turn: `dsh-bash-local` runs arbitrary model-written commands with the host filesystem, network, and a scrubbed-but-populated environment. One asymmetry runs the other way and is stated plainly: `worker.terminate()` ends the thread, not OS processes a program may have spawned via `node:child_process` — weaker than `bash-local`'s process-group kill for direct children (equivalent for double-forked daemons, which survive both); the wall-clock ceiling bounds the worker itself, and orphan cleanup is the same deployment-level concern it already is for bash. Code Mode is gated where bash is gated — `tools/pre-execute`, where permission/sandbox plugins veto or approve the program before it runs — and adds containment bash does not have: empty env, heap caps, hard termination of the program itself, a separate isolate. The earlier draft's two-flag unsafe ceremony (`{ unsafe: true }` constructor + `allowUnsafeRuntime`) existed for a `node:vm` stub with *no* containment and is dropped with it; demanding scarier flags for the better-contained executor than for bash would be posture theater. A deployment that needs a hard boundary (untrusted multi-tenant input) needs it for bash too; that is a future `isolation: 'container'` backend, and the `isolation` descriptor exists so such a deployment can tell backends apart. +The worker runtime is **containment, not a security boundary**. Model code in the worker can reach Node globals — `fetch`, `process` (with an empty env), dynamic `import()` of built-ins — so a deliberately adversarial program has ambient authority comparable to what the harness's own `bash` tool grants every model turn: `dsh-bash-local` runs arbitrary model-written commands with the host filesystem, network, and a scrubbed-but-populated environment. One asymmetry runs the other way: `worker.terminate()` ends the thread, not OS processes a program may have spawned via `node:child_process` — weaker than `bash-local`'s process-group kill for direct children (equivalent for double-forked daemons, which survive both); the wall-clock ceiling bounds the worker itself, and orphan cleanup is the same deployment-level concern it is for bash. Code Mode is gated where bash is gated — `tools/pre-execute`, where permission/sandbox plugins veto or approve the program — and adds containment bash does not have: empty env, heap caps, hard termination of the program itself, and a separate isolate. A `node:vm` executor with no containment would need explicit unsafe acknowledgement; imposing that ceremony on the better-contained worker while bash needs none would be posture theater. A deployment that needs a hard boundary (untrusted multi-tenant input) needs it for bash too; that is a future `isolation: 'container'` backend, and the `isolation` descriptor lets deployments distinguish backends. ### What the model sees @@ -84,37 +86,37 @@ The `tools:sdk` section carries the `.d.ts` plus fixed instructions: the program ## Consequences -The design shipped as four stacked changes — this RFC, the `dsh-code-runtime` interface package, the `dsh-code-runtime-worker` backend, and the `dsh-tools` integration — each gates-green with docs in the same change; review fixes landed on the change that introduced them and merged down. +The design consists of the `dsh-code-runtime` interface package, the `dsh-code-runtime-worker` backend, and the `dsh-tools` presentation and dispatch integration. -What exists now: +Shipped surface: - **The seam**: `packages/code-runtime/` — `@deepseek-ai/dsh-code-runtime` (abstract `CodeRuntime`, the vocabulary above, `ctx.codeRuntime`) and `@deepseek-ai/dsh-code-runtime-worker` (the worker-thread backend, every cap a validated config field). Rows in the service map, capability-seams graph, config catalog, and cordis catalog. -- **The registry surface**: `ToolRegistry`'s first config (`mode`), the mode-aware wire contribution, the `tools:sdk` section, `jsonSchemaToTs`/`renderToolsSdk` (exported), `run_code` + the dispatch bridge + `CodeRunFailedError`, and the `tool/code-dispatch` log event (declaration-merged into `SessionEventMap`, regenerated into the persistence catalog; `run_code` in the tool catalog). -- **The composed surface**: the `tools` config forwards through `agent-core` and both app packages (`stdio-agent`, `acp-agent`); `demo:code-mode` boots each UI example's `code-mode.cordis.yml` overlay (the worker runtime + `mode: 'code'` over the base tree); the adding-a-tool cookbook states that a registered tool is reachable from programs for free, and the tool-pipeline doc shows sub-dispatches re-entering both waterfalls. -- **Interactions inherited by deployments**: a `toolOrder` naming native tools rejects every assembly under `'code'` (update or drop the order config when switching modes); sub-call `additionalContext` is dropped by the bridge (a plural context channel is deferred until a real hook needs it through Code Mode); sub-dispatch stays serialized until tools can declare concurrency safety — the same metadata the native parallel-dispatch TODO waits on. +- **The registry surface**: `ToolRegistry`'s `mode` config, mode-aware wire contribution, lazy `tools:sdk` section and reserved `run_code` transport, `jsonSchemaToTs`/`renderToolsSdk` (exported), the dispatch bridge and `CodeRunFailedError`, and the `tool/code-dispatch` log event (declaration-merged into `SessionEventMap`, regenerated into the persistence catalog; `run_code` in the tool catalog). +- **The composed surface**: the `tools` config forwards through `agent-core` and both app packages (`stdio-agent`, `acp-agent`); `demo:code-mode` boots each UI example's `code-mode.cordis.yml` overlay (the worker runtime + `mode: 'code'` over the base tree); every program sub-dispatch resolves the same scoped capability view and re-enters the complete tool pipeline with an immutable link to its enclosing transport execution. +- **Interactions inherited by deployments**: a `toolOrder` naming native tools rejects every assembly under `'code'` (update or drop the order config when switching modes); restrictions can hide end capabilities but cannot remove the registry-owned presentation transport, while assembly listeners may rewrite the final model-visible surface and own its protocol integrity; sub-call `additionalContext` is dropped by the bridge (a plural context channel is deferred until a real hook needs it through Code Mode); sub-dispatch stays serialized until tools can declare concurrency safety — the same metadata the native parallel-dispatch TODO waits on. ## Testing What the suites pin, per tier: - **Unit — worker runtime** (real workers, no mocks): output/value capture and log-source attribution; error kinds (exception incl. non-erasable syntax, abort, worker-exit under OOM); the two budgets from both sides (a hot loop behind an un-awaited pending dispatch dies at `computeMs` busy time; a program idling on a slow binding outlives `computeMs` and dies only at `maxWallMs`); binding-bridge hostility (junk/forged port traffic incl. non-object messages and forged `log`/`done` cap bypass attempts, unknown names, duplicate ids, post-settlement replies, `__proto__`/`constructor`/`toString` binding names); structured-clone fallback and cap truncation; `env` emptiness verified from inside a program; dispose-awaits-exit. A real-load-path e2e runs the BUILT package under plain `node` so the worker entry resolves both unbuilt (tsx) and built — the published-artifact guard from [docs/testing.md](../../../testing.md). -- **Unit — registry integration**: the codegen table (DSL subset, quoted names, `unknown` degradation, byte-identical determinism); provider contribution per mode (`'native'` unchanged, `'code'` exactly `[run_code]`, `'both'` all + `run_code`); `toolOrder × mode` rejection; missing-runtime / wrong-language loud failures; serialization non-overlap (a probe tool records enter/exit under `Promise.all`); abort aborting the in-flight sub-dispatch and abandoning queued ones; binding rejection on `isError` and on JSON-unrepresentable arguments; `CodeRunFailedError` → structured `isError` carrying kind + logs; `tool/code-dispatch` payloads (JSON-normalized arguments identical to what dispatched); `deriveMessages()` ignoring the event; sub-call `additionalContext` suppression; HMR safety (disposing the registry removes the tool and the section). +- **Unit — registry integration**: the codegen table (DSL subset, quoted names, `unknown` degradation, byte-identical determinism); provider contribution per mode (`'native'` capabilities, `'code'` exactly `[run_code]`, `'both'` capabilities + `run_code`); reserved-name, restriction, scoped shadowing, authoritative assembly transformation, and `toolOrder × mode` invariants; missing-runtime / wrong-language loud failures; full-pipeline and opaque parent-token behavior for sub-dispatches; serialization non-overlap (a probe tool records enter/exit under `Promise.all`); abort aborting the in-flight sub-dispatch and abandoning queued ones; binding rejection on `isError` and on JSON-unrepresentable arguments; `CodeRunFailedError` → structured `isError` carrying kind + logs; `tool/code-dispatch` payloads (JSON-normalized arguments identical to what dispatched); `deriveMessages()` ignoring the event; sub-call `additionalContext` suppression; HMR safety. - **e2e (with-key, self-skips)**: a real model under `mode: 'code'` composes two bash calls in one program (`examples/coding-agent/tests/code-mode.e2e.ts`) — every logged `request/header` carries exactly `[run_code]`, the dispatch events land under the parent call, the file the program wrote exists, and the final answer is the curated output. - **Snapshot (keyless replay)**: goldens for a `run_code` turn under `'code'` and `'both'` (`code-mode-turn`, `both-mode-turn`), each its own header-pinning class — the SDK section text, the collapsed header tool list, the dispatch events, and the result card are committed and replayed. ## Alternatives considered -**An add-on consumer plugin, zero core changes (the previous draft of this RFC).** Rejected on both halves. The wire-collapse half aged out from under it: it targeted the `agent/request` waterfall, which [reconstructable requests](../../implemented/architecture/2026-07-05-reconstructable-requests.md) has since re-typed to call-config-only, and the surviving alternative — transforming the assembly a waterfall listener receives — is strictly worse than contributing the right list in the first place (transformation must undo `toolOrder` canonicalization it cannot see the config for, and its correctness depends on where it sits in a listener chain). The deeper reason is ownership: which tools the model is offered, in which representation, is the registry's single concern — `schemas()` for function calling and the SDK for Code Mode are two projections of one store, and splitting the second projection into a satellite package would preserve a boundary the domain does not have. +**An add-on consumer plugin with zero core changes.** Rejected because `agent/request` is call-config-only under [reconstructable requests](../../implemented/architecture/2026-07-05-reconstructable-requests.md), while transforming an assembled tool list would have to undo `toolOrder` canonicalization without owning its config and would depend on listener order. Which tools the model is offered, and in which representation, is the registry's single concern: native schemas and the SDK are two projections of one visible store. -**`node:vm` as the reference runtime, hardening deferred (also the previous draft).** Rejected: `node:vm` is not isolation (prototype-chain escapes reach the host realm), cannot interrupt a hot loop, and forced the draft into a two-flag unsafe ceremony plus a mandatory follow-up RFC. The worker thread delivers the missing properties now — separate isolate, empty env, `resourceLimits`, reliable `terminate()` (all verified by probe before this revision) — at bash-equivalent trust, so the reference implementation and the production one are the same package and the ceremony dissolves. +**`node:vm` as the reference runtime, with hardening deferred.** Rejected: `node:vm` is not isolation (prototype-chain escapes reach the host realm) and cannot interrupt a hot loop. A worker thread provides a separate isolate, empty environment, `resourceLimits`, and reliable `terminate()` at bash-equivalent trust, so the reference and production implementation are one package without an unsafe-acknowledgement ceremony. -**Result elision / summarization over native tool-calling.** Addresses only the context-bloat half of the problem: trimming old `tool-result`s (now cheap to add as a logged surface replace, per the reconstructable-requests consequences) still pays one model round-trip per call and cannot express loops, branches, or joins. Complementary, not competing; it can layer under Code Mode for residual native calls. +**Result elision / summarization over native tool-calling.** Addresses only the context-bloat half of the problem: trimming old `tool-result`s is cheap to add as a logged surface replacement under reconstructable requests, but still pays one model round-trip per call and cannot express loops, branches, or joins. Complementary, not competing; it can layer under Code Mode for residual native calls. **Parallel native dispatch in the loop.** The other answer to round-trip cost; still valid future work (the open TODO), still blocked on concurrency-safety metadata, and still no composition — it parallelizes calls the model already decided on in one step. Code Mode's serialized-queue decision keeps the two compatible: when the metadata lands, both native parallel dispatch and per-tool binding parallelism unlock together. **Always-exclusive (Cloudflare-faithful, no mode).** Rejected for this SDK's primary consumer: a coding agent's bread-and-butter single calls (`bash`, `read`, `edit`) are already ideal as native calls, and forcing every edit through a program taxes the common case. The mode config keeps the faithful form (`'code'`) one line away without imposing it. -**Per-tool visibility tiers (this tool native, that tool code-only).** Deferred again, knowingly: it needs per-tool metadata and a presentation split that `'native' | 'code' | 'both'` does not, and every learning it depends on (how models actually split usage under `'both'`) arrives only after this ships. +**Per-tool visibility tiers (this tool native, that tool code-only).** Deferred: it needs per-tool metadata and a presentation split that `'native' | 'code' | 'both'` does not, and its design depends on evidence about how models split usage under `'both'`. **Sanitized identifier aliases in the SDK** (`my-tool` → `my_tool`, Cloudflare's approach). Rejected: quoted keys on a `declare const` make every name reachable with zero alias-collision logic; models handle `tools["my-tool"](…)` fine. diff --git a/docs/rfc/implemented/feature/2026-06-18-acp-terminal-and-tool-rendering.md b/docs/rfc/implemented/feature/2026-06-18-acp-terminal-and-tool-rendering.md index bca351e906..5eb38f1f99 100644 --- a/docs/rfc/implemented/feature/2026-06-18-acp-terminal-and-tool-rendering.md +++ b/docs/rfc/implemented/feature/2026-06-18-acp-terminal-and-tool-rendering.md @@ -4,7 +4,7 @@ Status: implemented ## Problem -The ACP bridge lets each tool own its call rendering via `presentCall`/`presentResult` (see [tool-call UI presentation](../../proposed/feature/2026-06-14-acp-agent-client-protocol.md) and `packages/core/tools`). For `bash` we surface the exact command as the `tool_call` title, the model's `description` as a content text block, `kind: 'execute'`, and the completed output wrapped in a fenced ` ```console ` text block. +The ACP bridge lets each tool own its call rendering via `presentCall`/`presentResult` (see [tool-call UI presentation](../../implemented/feature/2026-06-14-acp-agent-client-protocol.md) and `packages/core/tools`). For `bash` we surface the exact command as the `tool_call` title, the model's `description` as a content text block, `kind: 'execute'`, and the completed output wrapped in a fenced ` ```console ` text block. That is a correct, capability-free baseline, but not how the reference editors render a *terminal* tool at its best. An editor like Zed has a dedicated terminal tool-call card — a header showing the working directory, the command as the label, the command output rendered as a terminal, and an exit-status pill — but it only builds that card when the `tool_call` carries terminal metadata (below). With a plain text block the output appears as static markdown and there is no cwd header. (Zed also HIDES `rawInput` for `kind: 'execute'`, which is why the command IS the title — both reference adapters do the same. The human-readable description rides as a separate content block above the card; note this is a DELIBERATE divergence — claude-agent-acp DROPS the description in terminal mode and renders only the card — we keep the summary visible alongside.) diff --git a/docs/rfc/implemented/feature/2026-06-21-subagent-capability-seam.md b/docs/rfc/implemented/feature/2026-06-21-subagent-capability-seam.md index 28daff14ff..652eac5522 100644 --- a/docs/rfc/implemented/feature/2026-06-21-subagent-capability-seam.md +++ b/docs/rfc/implemented/feature/2026-06-21-subagent-capability-seam.md @@ -35,18 +35,18 @@ A new package group `packages/subagent/`: | `@deepseek-ai/dsh-subagent-mock` | support: a scripted provider for testing the seam through the real load path | | `@deepseek-ai/dsh-tool-subagent` | consumer: the model-facing `subagent` tool over `ctx.subagents` | -### The primitive: `start → SubagentRun` +### The primitive: async `start → SubagentRun` -A provider exposes `start(request) → SubagentRun`. The run carries a `result` promise (the terminal `SubagentResult`), `cancel()`, and `dispose()`. The transport-neutral verb is **`start`**; "spawn" is reserved for the in-process `dsh-subagent-spawn` backend's identity, not the service verb. The service's `start(name, request)` resolves the named provider, validates capabilities, delegates, and emits `subagent/start` / `subagent/end` around the run. +A provider exposes `start(request) → Promise`. Promise fulfillment is the publication/readiness and provider-to-caller ownership boundary: for an in-process backend the child is already published in `ctx.agents`, and for ACP the remote session already exists. `SubagentStartRequest.signal` is the single cancellation channel before and after readiness; `SubagentRun` carries the terminal `result` and a `dispose()` method that cancels remaining work and awaits quiescence. The transport-neutral verb is **`start`**; "spawn" is reserved for the in-process `dsh-subagent-spawn` backend's identity, not the service verb. A rejected start cleans provider-owned partial resources and emits neither subagent lifecycle event. ### Two kinds of optional capability, discovered two ways -- **Start-time features** (`outputSchema`, `depthLimit`, `toolFilter`) ride on a static `provider.capabilities` descriptor. The service checks every requested one BEFORE delegating and **rejects loud** (`SubagentError('UNSUPPORTED_CAPABILITY')`) if the provider lacks it — never accepted-then-ignored. They must be checked before a run exists, which is why they cannot be runtime methods. +- **Start-time features** (`outputSchema`, `depthLimit`, `toolFilter`, `persona`) ride on a static `provider.capabilities` descriptor. The service checks every requested one BEFORE delegating and **rejects loud** (`SubagentError('UNSUPPORTED_CAPABILITY')`) if the provider lacks it — never accepted-then-ignored. They must be checked before a run exists, which is why they cannot be runtime methods. - **Runtime features** (steering via `sendMessage`, follow-up via `resume`) are **optional methods** on `SubagentRun`. The method's presence IS the capability, and TypeScript narrowing is the discovery mechanism: a consumer cannot call an absent method without narrowing first, so there is no silent-degradation path and no separate flags object to keep in sync. ### Fork vs. fresh are separate backends, not a flag -Rather than a `context: 'fresh' | 'fork'` request field, the distinction is the provider's identity: `dsh-subagent-spawn` (fresh, isolated, own system prompt) and `dsh-subagent-fork` (seeded from the parent's log) are two registered providers. You pick behavior by picking a provider — consistent with the registry being the selection mechanism. The fork backend seeds only a **balanced, completed-turn prefix** of the parent log: at tool-execute time the parent's turn is open (it holds the `assistant/message` and the dangling spawn `tool/call` with no `tool/result`), and seeding that raw prefix would give the child an unbalanced turn the [invariants](../../../../packages/support/invariants/src/index.ts) freeze-check rejects. +Rather than a `context: 'fresh' | 'fork'` request field, the distinction is the provider's identity: `dsh-subagent-spawn` (fresh, isolated, own system prompt) and `dsh-subagent-fork` (seeded from the parent's log) are two registered providers. You pick behavior by picking a provider — consistent with the registry being the selection mechanism. The fork backend seeds only a **balanced, completed-turn prefix** of the parent log: at tool-execute time the parent's turn is open (it holds the `assistant/message` and the dangling spawn `tool/call` with no `tool/result`), and seeding that raw prefix would give the child an unbalanced turn that the [invariants](../../../../packages/support/invariants/src/index.ts) trace replay rejects. ### Child isolation and the parent log @@ -54,7 +54,7 @@ Each subagent runs in its **own `Session`** (own id, `parentSession` lineage), p ### Synchronous collect (first cut) -The `dsh-tool-subagent` consumer awaits `run.result` and returns the child's final output as the tool result, blocking the parent's turn until the child finishes. It does so inside a `try/finally` that always `dispose()`s the run (no leaked idle child/session on any path), bridges `exec.signal` to `run.cancel()`, and maps a non-`completed` stop reason to an `isError` result rather than returning partial output as success. Steering (`sendMessage`) is part of the contract but **intentionally unused** this cut. +The `dsh-tool-subagent` consumer passes its execution signal into the start request, awaits the ready run's `result`, and returns the child's final output as the tool result, blocking the parent's turn until the child finishes. A `try/finally` always `dispose()`s the run, so no success, failure, or cancellation path leaks an idle child/session. A non-`completed` stop reason maps to an `isError` result rather than returning partial output as success. Steering (`sendMessage`) is part of the contract but intentionally unused in this consumer. ### Provider selection is config, not model-facing @@ -66,7 +66,7 @@ The seam is tested through the real cordis Loader / export path, not a hand-buil ## Consequences -- **Recursion.** Without a guard, an in-process child inherits the spawn tool and can spawn unboundedly. Depth-limit is an optional capability (the in-process backends enforce it; ACP advertises it off and rejects a `maxDepth` request); tool-filtering is likewise optional. Tool-filtering, when implemented, needs a `tools/pre-execute` deny in the child context — schema filtering alone is insufficient because a model can hallucinate a denied tool name. +- **Recursion.** Without a bound, an in-process child can see the delegation tool and recurse. The in-process backends implement the optional absolute depth limit and scoped live-global `toolFilter`; ACP advertises both capabilities off and rejects such a request. The [subagent composition-controls RFC](2026-07-12-subagent-persona-tool-filter-and-depth.md) owns their exact semantics and security limits. - **Blocking the parent turn.** Synchronous collect holds the parent's `runStep` open for the child's full duration. This is acceptable for the first cut; **background / poll / spill semantics are deferred to a future redesign that unifies long-running-tool handling across subagents AND bash** (a sub-agent and a long `bash` background task pose the same "the model started something slow, how does it collect later" problem, and should share one mechanism rather than each inventing its own). - **Live progress.** This cut surfaces only lifecycle + final result; a per-chunk child→parent update stream is deferred with the background redesign. - **ACP client surface.** Proxying `fs`/`terminal` from the ACP child back to the parent (a shared-workspace mode) is future work; the first cut advertises neither, so the child self-serves in its own process. diff --git a/docs/rfc/implemented/feature/2026-06-22-acp-subagent-backend.md b/docs/rfc/implemented/feature/2026-06-22-acp-subagent-backend.md index ec608177b4..518f9dedef 100644 --- a/docs/rfc/implemented/feature/2026-06-22-acp-subagent-backend.md +++ b/docs/rfc/implemented/feature/2026-06-22-acp-subagent-backend.md @@ -34,7 +34,7 @@ The child is a separate process, so it inherits an environment. Credential-shape Designed at every tier the backend touches, per the root AGENTS.md rule that a new capability shape names its coverage at every tier at plan time: -- **Keyless unit/integration** (`subagent-acp.spec.ts`): spawns a scripted mock ACP server subprocess (`tests/mock-acp-server.ts`) and drives it through the real backend over real ACP stdio. Covers: the prompt round-trip + output accumulation; every StopReason mapping; cancellation via `run.cancel()` and via the request signal; the already-aborted-before-start case; the cancel-races-ahead-of-newSession case; a torn-pipe-after-cancel (child crashes on cancel) settling `aborted`; permission auto-answer under both policies (including the allow-policy-no-allow-option fallback); a non-message update consumed but not accumulated; a nonexistent-command spawn failure settling `error`; HMR provider cleanup; and the namespace export shape. 100% per-file coverage. +- **Keyless unit/integration** (`subagent-acp.spec.ts`): spawns a scripted mock ACP server subprocess (`tests/mock-acp-server.ts`) and drives it through the real backend over real ACP stdio. Coverage includes the prompt round-trip and output accumulation; every StopReason mapping; cancellation through the required request signal and through disposal; already-aborted and cancel-races-ahead-of-newSession starts; a torn pipe after cancellation settling `aborted`; permission auto-answer under both policies; non-message updates; nonexistent-command startup failure with process reaping; provider HMR; and the namespace export shape. - **With-key e2e** (`subagent-acp.e2e.ts`): the harness drives ITSELF — the backend spawns the real `acp-agent` example process and a real model in that child answers a prompt (PONG) and does real file work (writes `proof.txt`, verified on disk). Self-skips without `DEEPSEEK_API_KEY`. This is the "talk to our own process" smoke and the out-of-process analogue of the in-process spawn e2e. - **Snapshot**: deferred as `TODO(acp-subagent-replay)`. An ACP child is a distinct replay shape — each child is its own PROCESS with its own single-agent replay (booted under `DSH_SNAPSHOT=replay` with its own sessions-root + fixture), unlike the in-process per-session keying that [the per-session replay RFC](../testing/2026-06-22-subagent-snapshot-replay.md) added. The keyless mock-server tests give deterministic coverage of the backend in the meantime; the snapshot follow-up would record the parent driving a real-but-replayed ACP child. diff --git a/docs/rfc/implemented/feature/2026-06-30-hook-bridges.md b/docs/rfc/implemented/feature/2026-06-30-hook-bridges.md index 630d4dd3ed..4c9b89b8d0 100644 --- a/docs/rfc/implemented/feature/2026-06-30-hook-bridges.md +++ b/docs/rfc/implemented/feature/2026-06-30-hook-bridges.md @@ -26,9 +26,11 @@ Each bridge maps the neutral `MergedHookOutcome` from the shared lib onto the se | `tools/pre-execute` | `deny`→`deny`; `ask`→`ask` | `block`→`deny` (no allow/ask) | | `tools/post-execute` | `deny`→`block`+feedback; context-only→delegate+fold | same | | `agent/turn-continuation` | blocking Stop → `continue` (reason = next-step steering) | same | -| `subagent/start` (emit) | additionalContext → inject into the live child | — (not a Codex event) | +| `subagent/start` (emit) | additionalContext → inject into a live in-process child; a remote child has no local injection target | — (not a Codex event) | | `subagent/end` (emit) | observe-only | — | +The CC bridge's `ask` result is a real permission path, not a terminal bridge decision: `dsh-tools` resolves it through the optional [approval seam](2026-07-06-approval-seam.md). A composed ACP answerer prompts the owning editor session and `allowed-once` proceeds; without an ApprovalService or answerer, the call fails closed to `deny`. + ### Context source is always the plugin (the mislabel guard) `agent.inject()` defaults a missing `MessageSource` to `{ kind: 'user' }` — which would record plugin-injected context as if the user had typed it. So every bridge `inject()` and every `HookContext` passes an explicit `{ kind: 'plugin', plugin: 'hooks-claude' | 'hooks-codex' }` source. A test asserts the resulting `context/message.source` is the plugin, never `user`. @@ -52,11 +54,10 @@ Two different cwds, kept distinct on purpose. The hooks **themselves** run in th ## Deferred (faithful-but-degraded) - **Tool-input rewrite.** A CC/Codex `updatedInput` is logged + warned, not honored — input rewrite is a deferred consistency-design problem ([the pre-tool-input-rewrite RFC](../../proposed/feature/2026-06-30-pre-tool-input-rewrite.md)), because the pre-execution args are read by `tool/call` audit + `assistant/message` history + ACP/tool-bash presentation, so an honest rewrite is a design unit, not a field. -- **Stop loop-guard** (`TODO(stop-loop-guard)`). CC/Codex break an infinite force-continue with `stop_hook_active` (true once a Stop hook fired this run) plus a max-consecutive cap; both are deferred. Today `stop_hook_active` is always `false`, so a Stop hook that unconditionally blocks would force-continue every step — a hook author must self-limit until the guard lands. -- **Permission `ask`** degrades to `deny` at the `tools/pre-execute` seam (`FIXME(permissions)` in the interception-seams RFC) — there is no interactive permission prompt yet. +- **Stop loop-guard** (`TODO(stop-loop-guard)`). CC/Codex break an infinite force-continue with `stop_hook_active` (true once a Stop hook fired this run) plus a max-consecutive cap; both are deferred. `stop_hook_active` is always `false`, so a Stop hook that unconditionally blocks would force-continue every step — a hook author must self-limit until the guard lands. - **Hook `continue:false` (hard halt).** A hook can ask to halt the whole run (CC/Codex `continue:false`); the shared merge folds it into `MergedHookOutcome.stop`/`stopReason`, but no bridge acts on it (`TODO(hook-continue-false)`) — the interception seams have no "hard-halt the agent" primitive yet (a Decision blocks/steers a single point, not the run). Deferred with the loop-guard work; the halt request is recorded in the `hook/result` log, and the hook keeps its per-point effect (decision/context) meanwhile. - **Config discovery.** The path is explicit in `cordis.yml` and process-level (see above); the full multi-layer CC/Codex precedence walk, per-session project-local discovery, and the trust/hash model are not reimplemented (`TODO(per-session-hook-config)`). -- **Session-start / subagent-start context is best-effort, not gated (`TODO(session-start-gating)`).** `agent/session-start` is a synchronous emit and the bridge runs its hook on a detached `.then`, so the injected `additionalContext` is not guaranteed to land before the first turn reaches the model — a slow hook can miss the first request (the context then arrives as a later injection). `subagent/start` is sharper: an in-process provider may have already queued the child's prompt before the listener runs, and a short-lived child can finish before the detached inject fires. Making startup context a gated/awaited primitive is a loop-level change deferred to the interception seams; today the contract is "injected as soon as the hook resolves", not "before the first request". The bridge tests do NOT wait on the injection where they assert the guaranteed-timing behavior, so they document the real (best-effort) timing rather than masking it. +- **Session-start / subagent-start context is best-effort, not gated (`TODO(session-start-gating)`).** `agent/session-start` is a synchronous emit and the bridge runs its hook on a detached `.then`, so the injected `additionalContext` is not guaranteed to land before the first turn reaches the model — a slow hook can miss the first request (the context then arrives as a later injection). `subagent/start` is emitted only after child publication, so the bridge can capture the live in-process child synchronously, but the result driver may queue the prompt as that same readiness boundary resolves and a short-lived child can finish before the detached hook injects. Making startup context a gated/awaited primitive is a loop-level change deferred to the interception seams; the contract is "injected as soon as the hook resolves", not "before the first request". The bridge tests do NOT wait on the injection where they assert the guaranteed-timing behavior, so they document the real (best-effort) timing rather than masking it. ## Alternatives considered diff --git a/docs/rfc/implemented/feature/2026-06-30-interception-seams.md b/docs/rfc/implemented/feature/2026-06-30-interception-seams.md index 7795b044f6..cb653c2519 100644 --- a/docs/rfc/implemented/feature/2026-06-30-interception-seams.md +++ b/docs/rfc/implemented/feature/2026-06-30-interception-seams.md @@ -6,21 +6,31 @@ Status: implemented The harness needs a hooks subsystem: users extend or gate the agent at lifecycle points the way Claude Code (CC) and Codex do. The key reframe driving this design is that **"native hooks" are not a package** — a native hook is just an ordinary Cordis plugin subscribing to the canonical lifecycle events. So the real product is a *powerful, well-typed canonical event surface*; the CC/Codex bridges (the `dsh-hooks-claude` / `dsh-hooks-codex` packages) are merely translators that map an external shell-hook protocol onto that same surface. Anything a bridge can do, a plain plugin can do directly — more powerfully (no serialization boundary, full `ctx`, typed returns). -Before this change the interception surface was incomplete and inconsistent for that goal: there was no per-prompt seam (CC's `UserPromptSubmit`), no session-start signal (CC's `SessionStart`), the single `tools/execute` waterfall conflated the pre-gate and post-inspect phases (CC splits `PreToolUse`/`PostToolUse`), and `agent/turn-continuation` returned a bare `boolean` with no room for a force-continue *reason*. The [event-domain-semantics RFC](../architecture/2026-06-30-event-domain-semantics.md) pinned down the three-domain rule and the typed-Decision idiom as the interception convention; this RFC builds the actual seams on top of it. +The surface needs distinct contracts for per-prompt policy (CC's `UserPromptSubmit`), session-start observation (CC's `SessionStart`), pre-tool policy, around-dispatch control, post-tool transformation, final-result observation, and continuation with a model-facing reason. Conflating those phases gives plugins mutation channels they do not need and makes finality depend on listener ordering. The [event-domain-semantics RFC](../architecture/2026-06-30-event-domain-semantics.md) supplies the three-domain rule and the typed-Decision idiom; this RFC applies them to the lifecycle seams. ## Decision -Add/​reshape the interception seams so every one returns a small, seam-specific **typed Decision union**, and the set covers the hook points in scope (`session-start`, `prompt-submit`, `pre-tool`, `post-tool`, `stop`-via-continuation). +The canonical surface separates transformable policy, around-dispatch control, and observe-only notification. Policy waterfalls return small seam-specific **typed Decision unions**; wrappers return normalized results; notifications receive immutable snapshots and cannot affect the outcome. The set covers the hook points in scope (`session-start`, `prompt-submit`, `pre-tool`, `post-tool`, `stop`-via-continuation) while leaving non-hook execution policy independently composable. -**New `agent/*` events** (`dsh-agent`): +**Agent events** (`dsh-agent`): - `agent/session-start(agent, source)` — emit, once before turn 1, carrying a `SessionStartSource` (`startup` for a fresh/forked create, `resume` for a reloaded persisted session; `clear`/`compact` reserved). A pure notification — it CANNOT block startup (a deliberate gap: a bridge logs/injects, it does not gate startup). A listener seeds context via `agent.inject()`. - `agent/prompt-submit(agent, content, source, next) → PromptDecision` — waterfall, fired per drained queued message inside the open turn, before the `user/message` append. `allow` (optionally rewriting the prompt `content` or attaching `additionalContext`) or `block` (dropping the prompt; the loop appends a durable `prompt/blocked` in its place — see the dispatch note below). -**Reshaped** `agent/turn-continuation` from `(…, defaultDecision: boolean) → boolean` to `(…, defaultDecision: ContinuationDecision) → ContinuationDecision`. A `{action:'continue', reason?}` may carry model-facing context recorded as next-step steering in the same turn — the typed twin of the existing `/goal` step-end-steer pattern. +**`agent/turn-continuation`** receives and returns a `ContinuationDecision`. A `{action:'continue', reason?}` may carry model-facing context recorded as next-step steering in the same turn — the typed twin of the `/goal` step-end-steer pattern. -**Split** the single `tools/execute` waterfall into `tools/pre-execute` (→ `PreToolDecision` allow/deny/ask gate) and `tools/post-execute` (→ `PostToolDecision` accept/block, optionally replacing content or attaching `additionalContext`). Core dispatch sits between them as plain code inside `ToolRegistry.execute`'s outer try/catch, and the tool body keeps its own inner try/catch so a thrown tool still becomes an `isError` result that `post-execute` listeners can inspect. +### The tool pipeline gives each phase one kind of authority -**New `TurnEndReason` variant** `rejected` (`dsh-session`): a turn whose entire prompt batch was blocked by `prompt-submit`. +Every call follows one ordered pipeline: `tools/pre-execute` → monotonic guards → `tools/execute` → core dispatch → `tools/post-execute` → `tools/result`. The registry reads each caller-owned input field once, materializes `arguments` as detached lossless JSON in one recursive pass, and snapshots `ToolExecutionInput` into a pipeline execution with its own opaque token. Identity fields and deeply frozen arguments are immutable for the whole pipeline, and a nested call's `parent` contains only the enclosing execution's token rather than its live object. Optional `signal` is the only operational field an around-dispatch wrapper may add, replace, or remove, and the complete object freezes before final observers run. This identity contract prevents a policy listener from silently changing what the log, UI, and tool body believe ran. + +- **`tools/pre-execute`** is the extensible waterfall gate. Its `PreToolDecision` allows, denies, or asks. Deny skips `tools/execute` and core dispatch. Ask resolves through the optional approval seam: only `allowed-once` continues through guards and dispatch; rejection, cancellation, an unavailable channel, a missing approval service, or an agent-less call becomes a normalized denial. Every outcome still reaches post-policy and final observers. +- **`ctx.tools.guard()`** installs synchronous scope-aware policy after the whole pre-execute waterfall. A guard may deny or abstain, never force-allow, so listener ordering cannot resurrect an operation that a final invariant forbids. +- **`tools/execute`** is the around-dispatch waterfall for timeout, retry, and metrics plugins. A wrapper delegates to core dispatch with `next()`, may add, replace, or remove only `exec.signal` before doing so, and receives the already-normalized result of a thrown or unknown tool; returning its own valid result short-circuits dispatch. +- **`tools/post-execute`** is the inspect/transform waterfall. Its `PostToolDecision` accepts, blocks with feedback, optionally replaces content, or attaches `additionalContext`; in-place mutation of the result is not a transform channel, because the registry rebuilds the outcome from a protected snapshot plus the returned decision. +- **`tools/result`** is the synchronous contained notification after every transform, lossless-JSON materialization, and the outer error boundary. It receives the same frozen execution identity and an immutable snapshot of the authoritative result; observer failures are contained per listener and cannot change or reject `ToolRegistry.execute()`'s returned outcome. + +Core dispatch and the tool body sit inside normalization boundaries, so tool, listener, malformed-result, non-JSON result, and identity-shape failures resolve as JSON-safe `isError` results rather than escaping the turn. A post-execute listener can therefore inspect a thrown tool, and a final observer sees exactly what the caller receives and the session log can persist. + +**`TurnEndReason.rejected`** (`dsh-session`): a turn whose entire prompt batch was blocked by `prompt-submit`. ### Three load-bearing loop decisions @@ -30,13 +40,13 @@ Add/​reshape the interception seams so every one returns a small, seam-specifi 3. **A forced `continue` `reason` is enqueued through the steering channel**, so the next step's top-of-loop drain records it as steering for the continued turn — next-*step* steering within the SAME turn, not a next-*turn* prompt (matching the existing `hasSteering` force-continue override). -### Pre-tool INPUT rewrite is DEFERRED (the over-reach signal) +### Pre-tool input rewrite is a separate consistency decision -`PreToolDecision` is allow/deny/ask only — **no `arguments` rewrite**. Output replacement (`PostToolDecision.accept.content`) is safe because `tool/result` is logged AFTER execution (one source of truth). Input rewrite is NOT safe today: `assistant/message` (the model-history source) and `tool/call` (the audit record) are both logged BEFORE execution, and live consumers READ `tool/call.arguments` for presentation (the ACP bridge remembers them for `presentResult`; `dsh-tool-bash` derives the title/cwd/terminal-vs-background from them). A rewrite that changed only execution would make the UI show one command while another RAN. Designing that consistently (rewriting the audit + history + presentation as one unit) is a real consistency-design problem CC itself warns is racy — so it gets its own [proposed RFC](../../proposed/feature/2026-06-30-pre-tool-input-rewrite.md), and `TODO(pre-tool-input-rewrite)` anchors it at the loop's pre-execute call site. This does not regress any production consumer (no production `tools/execute` listener mutated `exec.arguments`). The low-level capability to mutate `exec` in a `pre-execute` listener still exists (unadvertised — a test shim uses it to thread a generated id), but it is not a first-class advertised contract. +`PreToolDecision` is allow/deny/ask only — **no `arguments` rewrite**. Output replacement is safe because `tool/result` is logged after execution from the final result. Input rewrite is different: `assistant/message` (model history) and `tool/call` (the audit record) are logged before `ToolRegistry.execute()`, while ACP and tool presentation read those arguments. The registry therefore seals the materialized arguments before `tools/pre-execute`; no listener or test shim can mutate them in place. An honest rewrite must update history, audit, presentation, and execution as one unit before that identity is created, which belongs to the separate [pre-tool input-rewrite proposal](../../proposed/feature/2026-06-30-pre-tool-input-rewrite.md) and its loop-side `TODO(pre-tool-input-rewrite)`. -### What this PR does NOT do +### Boundaries -It does **not** declare `hook/*` SessionEvents (the durable hook-invocation log) — those belong to the `dsh-hook-protocol` library, because a native plugin can already use the typed Decisions without a durable hook log. A worked native-plugin example/test in this PR (`packages/core/agent-loop/tests/interception.spec.ts`) proves all the seams compose end-to-end through the REAL loop with NO `hook/*` involved — the concrete proof that "native hooks are just a plugin". Compaction (`PreCompact`/`PostCompact`), the Notification hook, Codex `PermissionRequest`, the permission/`ask` system, and the Stop loop-guard remain deferred (`FIXME(permissions)` marks the `ask`→deny degrade). +The seam package does **not** declare `hook/*` session events (the durable hook-invocation log); those belong to `dsh-hook-protocol`, because a native plugin uses typed decisions without an external hook log. The native-plugin integration test (`packages/core/agent-loop/tests/interception.spec.ts`) composes the seams through the real loop with no `hook/*` protocol. Compaction (`PreCompact`/`PostCompact`), Notification, and Codex `PermissionRequest` remain outside this decision. The [approval seam](2026-07-06-approval-seam.md) resolves `ask` decisions through `ctx.approval`, while terminal monotonic stopping is owned separately by `agent/turn-stop`. ## Alternatives considered @@ -45,4 +55,4 @@ It does **not** declare `hook/*` SessionEvents (the durable hook-invocation log) ## Consequences -The canonical interception surface is now complete and uniformly typed: a native plugin returns typed decisions directly, and a CC/Codex bridge maps its protocol fields onto the same unions. The loop gained four firing points (session-start emit, prompt-submit waterfall, the post-tool context buffer, the continuation reshape) and the `dsh-tools` registry runs a two-waterfall pipeline; both are documented in [architecture.md](../../../architecture.md) and the package READMEs, and the decision types in [core-data-structures](../../../core-data-structures/core.md#interception-decisions) + [tools.md](../../../core-data-structures/tools.md). All existing `tools/execute` and `turn-continuation` listeners (tests, docs) migrated to the new seams. The ACP bridge maps the new `rejected` reason to `cancelled` (its codec). A pure internal change with no editor-visible transcript shift for the existing scenarios — the new behavior only fires when a hook is registered — so the snapshot goldens are unchanged; a hook-driven snapshot scenario lands with the `dsh-hooks-claude` bridge, which is what makes a hook observable end-to-end through ACP. +The canonical interception surface is uniformly typed without giving every extension the same power: hooks return decisions, execution wrappers wrap, terminal guards only deny, and final observers only observe. The loop owns session-start, prompt-submit, post-tool context buffering, and continuation; `dsh-tools` owns identity sealing and the five-phase execution pipeline. Their contracts are documented in [architecture.md](../../../architecture.md), package READMEs, [core interception decisions](../../../core-data-structures/core.md#interception-decisions), and [tool structures](../../../core-data-structures/tools.md). The ACP bridge maps `rejected` turns to its `cancelled` codec value, while hook-driven snapshots verify the observable bridge behavior end to end. diff --git a/docs/rfc/implemented/feature/2026-06-30-subagent-observe-enrich.md b/docs/rfc/implemented/feature/2026-06-30-subagent-observe-enrich.md index f3b1c9bfb6..aa853edd24 100644 --- a/docs/rfc/implemented/feature/2026-06-30-subagent-observe-enrich.md +++ b/docs/rfc/implemented/feature/2026-06-30-subagent-observe-enrich.md @@ -6,13 +6,13 @@ Status: implemented The hooks subsystem ([interception seams RFC](2026-06-30-interception-seams.md)) lets a plugin observe and gate the agent at lifecycle points. Claude Code and Codex both expose **SubagentStart / SubagentStop** hooks, and CC's carry the subagent's final message. The harness already emits `subagent/start` and `subagent/end` lifecycle events ([the subagent capability-seam](2026-06-21-subagent-capability-seam.md)), but their payloads were minimal (`provider`, `id`, and on end `stopReason`) — not enough for a hooks bridge to report WHAT a subagent produced without separately reaching for the live run. -This RFC enriches the end payload. It is deliberately **observe-only**: no control-flow change, no waterfall, no `start()` restructure. A run-affecting subagent-stop decision (continuation, injection that changes the run) is a separate, larger redesign and stays out of scope. +This RFC enriches the end payload. It is deliberately **observe-only**: no control-flow change and no waterfall. A run-affecting subagent-stop decision (continuation, injection that changes the run) is a separate, larger redesign and stays out of scope. ## Decision -**Add `lastAssistantMessage` — the child's final output — to `SubagentRunEndInfo`.** On the settle path it is a DEEP CLONE of `SubagentResult.output` (so an observer sees WHAT the subagent produced without holding the run). On the REJECT path (an infrastructure fault where no `SubagentResult` was produced — the seam only knows `stopReason: 'error'`) it is absent. The clone is load-bearing for observe-only: the `subagent/end` emit fires from a detached `.then` registered *before* `start()` returns, i.e. before the caller's own `await run.result` continuation — handing listeners the same array reference would let a mutating listener corrupt the caller's `SubagentResult.output`. `structuredClone` makes the event a read-only view (a regression test mutates the event's array and asserts the caller's result is untouched); a clone failure is contained (logged, the event still fires without `lastAssistantMessage`) rather than becoming an unhandled rejection on the detached `.then`. +**Add `lastAssistantMessage` — the child's final output — to `SubagentRunEndInfo`.** On the settle path it is the readonly typed `SubagentResult.output`, so an observer sees what the child produced without holding the run. On an infrastructure rejection where no `SubagentResult` exists, it is absent and the event reports `stopReason: 'error'`. Providers and listeners are trusted same-process collaborators and honor the borrowed immutable payload contract. -Both events stay plain **`emit`s**. `subagent/end` fires from a detached `.then` on `run.result` and awaits no listener, so it is genuinely observe-only by construction — a `subagent/start` listener can still reach the live child via `ctx.agents.get(info.id)` and `inject()` into it; a `subagent/end` listener can only observe (the run has settled). Per-listener containment (already in place) keeps one bad subscriber from stranding a live run or surfacing as an unhandled rejection on the detached settle hook. +Both events stay plain **`emit`s**. Async `SubagentService.start()` attaches result observation to the ready provider run, emits `subagent/start`, and then returns the run; an in-process listener can therefore reach the published child via `ctx.agents.get(info.id)`, while a remote provider need not have a local registry entry. A rejected provider start emits neither event. The callbacks remain observe-only and per-listener containment keeps one bad subscriber from stranding a live run or starving later listeners. ## Alternatives considered diff --git a/docs/rfc/implemented/feature/2026-07-05-dynamic-workflows.md b/docs/rfc/implemented/feature/2026-07-05-dynamic-workflows.md index 2f711651f6..9fef336d9c 100644 --- a/docs/rfc/implemented/feature/2026-07-05-dynamic-workflows.md +++ b/docs/rfc/implemented/feature/2026-07-05-dynamic-workflows.md @@ -24,7 +24,9 @@ One deliberate strictness DIVERGENCE from CC: hook misuse — unknown or deferre **Trust premise (governs every engine decision below)**: workflow scripts are MODEL-WRITTEN — the same trust level as the model's existing bash access — so the engine defends against BUGGY scripts, never hostile ones. In scope: `result` never rejects, no unhandled rejections from dropped hook promises, loud rejection of values JSON cannot carry, fatal-vs-null hook discipline, cancellation that always frees the caller. Out of scope, deliberately: adversarial values (throwing/spinning accessors, proxies with hostile traps, prototype forgery, `prepareStackTrace` hijack) AND Node-API escape from the script's context — the vm context shares object machinery with its surrounding realm, so a script can reach the `Function` constructor (`globalThis.constructor.constructor`) and from it `process` and every Node builtin; the absent globals are API surface, not containment, and a worker thread is NOT a security boundary (an escapee holds process-wide privileges — Node's permission model is per-process). Worker-side code MAY run script code while reading script values, and that is accepted: a synchronous spin costs the script its OWN thread (terminated at the post-cancel grace), never the host loop, so containing error VALUES would be cost without a threat model. Genuine sandboxing (isolated-vm, a separate process) remains an engine swap behind the seam, not incremental defenses here. -**Why node:worker_threads**: one run = one worker thread, no pooling — a run is heavyweight (many children), so thread spin-up (~tens of ms) is noise. The script runs in a vm context INSIDE the worker, keeping the script-visible surface exactly the hook contract above (a bare worker realm would leak `setTimeout`/`fetch`/`process` as accidental API), and every `agent()` bridges to `ctx.subagents` by message-port RPC — children are I/O-bound LLM loops and stay on the host loop; the thread isolates the SCRIPT, the only part that can spin. What the thread buys: `start()` never blocks the host (an in-process engine runs the initial synchronous slice inline and cannot kill a spin past the first await — it could only ABANDON such a script, leaving the spin on the host loop), the post-cancel grace ends in a REAL `worker.terminate()`, and the value boundary is serialization by construction. isolated-vm was rejected for actual sandboxing: maintenance mode, `--no-node-snapshot` on EVERY consumer process (including published bins) on Node ≥ 20, node-gyp source-build fallback. Key mechanics (details in the package README): meta shape-validation and a body pre-parse stay HOST-side (preserving the seam's synchronous throws), a ready→go handshake keeps a run cancelled before start from ever executing the body, `cancel()` drives both child-cancel channels host-side (the shared request signal AND each child's explicit `cancel()` — a wedged worker cannot relay its own cancel RPCs), a host-side child registry backs worker-death reaping and `dispose()` quiescence, the wire protocol is enum-keyed payload maps private to the package, and on a termination path `agentsStarted` degrades to the host-observed count. Coverage puts the worker-side session on an in-process `MessageChannel` (real-Worker code is invisible to main-process v8) and proves the built `lib/worker.js` — a second tsdown entry, sanctioned in the workspace-constraints gate by the `"./worker"` subpath export — under plain node in the built-bin smoke gate. +**Why node:worker_threads**: one run uses one unpooled worker because a workflow run is already heavyweight relative to thread startup. The script runs in a vm context inside the worker, keeping the script-visible surface to the hook contract instead of exposing a bare worker realm, while `agent()` bridges by message-port RPC to I/O-bound child loops on the host. This keeps `start()` from blocking the host on the script's synchronous slice, makes the post-cancel deadline end in a real `worker.terminate()`, and gives cross-thread values a serialization boundary by construction. isolated-vm was rejected for its maintenance state, required `--no-node-snapshot` consumer flag on Node ≥ 20, and node-gyp fallback. + +Host-side meta validation and body pre-parsing preserve the seam's synchronous errors, and private enum-keyed payload maps define the wire protocol. Pending async starts, published child records, one host cancellation signal, worker-death reaping, result precedence, and disposal quiescence preserve the subagent run contract across that wire; the [agent-scope runtime-design RFC](../architecture/2026-07-12-agent-scope-runtime-design.md#workflow-children-are-pending-starts-or-published-records) owns those race algorithms. Coverage uses an in-process `MessageChannel` for worker-side logic that main-process V8 coverage cannot see and separately proves the built `lib/worker.js`—a second tsdown entry sanctioned by the `"./worker"` subpath export—under plain Node in the built-bin smoke gate. **Meta as data, never evaluated**: the meta block reaches the seam as a plain JSON request field (the tool's schema-validated `meta` parameter) and the engine only shape-validates it, every violation named. This is a host-isolation invariant, not a convenience: evaluating a meta literal host-side — even one contractually "pure", in an empty timed vm context — hands script-controlled getters a host stack with no timeout the moment the result is READ, defeating the exact spin isolation the worker thread buys. @@ -36,7 +38,11 @@ A `workflow` tool mirroring `dsh-tool-subagent`'s synchronous shape: start, awai ### The foundation: structured output on the subagent seam -`agent({schema})` needs `SubagentStartRequest.outputSchema` to actually work; it was vocabulary without an implementation (`outputSchema: false` everywhere). Implemented in `dsh-subagent-inprocess` for both in-process backends: a globally registered `structured_output` capture tool whose per-child schema is enforced by a `prepend: true` `system-prompt/assemble` listener doing FINAL-ASSEMBLY enforcement (post-processing `await next()` — cooperative mutation would not survive a downstream listener returning a replacement assembly; the calling instruction rides as a trailing prompt section, since `AgentOptions` carries no per-agent prompt field, and the loop logs the result as the step's `request/header`, keeping the injection reconstructable), a `prepend: true` `agent/turn-continuation` veto after capture (no wasted extra model step) plus a `tools/pre-execute` deny for calls arriving after the capture (terminal within the step, not only at its end), and validation-retry in-turn via `ToolArgsError`. The schema is `structuredClone`d at `start()` (caller mutation cannot drift enforcement). Deliberately NO re-prompt: a child that finishes cleanly without calling the tool settles `error` to the parent. Lifetime is refcounted by backends (plugin lifetime) AND live runs (start → settle). The seam's `outputSchema` type became the raw JSON-Schema SUBSET (`StructuredOutputSchema` in dsh-tools: single-string `type`, `properties`/`required`/`additionalProperties`, `items`, scalar `enum`/`const`; anything unenforced is rejected loud) — the schema travels verbatim to the model as the forced tool's parameters, so the wire format, not the author DSL, is the right vocabulary. +`SubagentStartRequest.outputSchema` is implemented by `dsh-subagent-inprocess` for both in-process backends. Each structured child receives its own scoped capture tool, instruction, and enforcement registrations on `child.ctx`; concurrent children can use different schemas without sharing mutable policy, and disposing the child removes the entire attachment. + +An output schema makes a schema-valid committed capture mandatory for successful child completion. The scoped runtime presents the capture tool and instruction, commits only a successful final outcome—including the enclosing `run_code` outcome for an SDK call—denies later side effects after capture becomes pending, and stops the child without another model step after commit. A validation failure remains a retryable tool error; clean completion without a committed capture settles as an error. + +`StructuredOutputSchema` is the raw enforceable JSON-Schema subset in `dsh-tools` (single-string `type`, `properties`/`required`/`additionalProperties`, `items`, scalar `enum`/`const`), and unsupported keywords fail loudly because that wire data becomes the capture tool's parameters verbatim. The [agent-scope runtime-design RFC](../architecture/2026-07-12-agent-scope-runtime-design.md#structured-output-commits-only-authoritative-outcomes) owns the assembly, commit, guard, and terminal-stop correctness algorithms. ## Deferred (documented non-goals of this cut) @@ -51,11 +57,11 @@ A `workflow` tool mirroring `dsh-tool-subagent`'s synchronous shape: start, awai ## Alternatives considered -- **Hostile-value containment in the host** (trap-free proxy rejection, accessor-never-invoked descriptor walks, realm-side pre-rendering of thrown values, realm-built promises/arrays/error clones with structural fatal recognition): an earlier revision built all of it, and review showed the cost was real while the threat model was not — every one of those defenses guards against an author the premise already trusts. Removed in favor of the plain boundary above; the thread boundary makes such machinery redundant anyway (serialization by construction). -- **In-process `node:vm` execution** (the first cut of this RFC shipped it): mechanically simplest — no RPC, no thread — but `start()` blocks the caller for the script's initial synchronous slice, a synchronous spin past the first await cannot be killed in-process (the vm `timeout` covers only that first slice), and `dispose()` could only ABANDON an unsettling script, leaving the spin on the host loop. Superseded by the worker-thread engine, which keeps the same vm-context script surface while unblocking the host and making termination real. +- **Hostile-value containment in the host** (trap-free proxy rejection, accessor-never-invoked descriptor walks, realm-side pre-rendering of thrown values, realm-built promises/arrays/error clones with structural fatal recognition): rejected because every defense targets an author the trust premise accepts, while the thread's serialization boundary already makes cross-realm values total by construction. +- **In-process `node:vm` execution**: mechanically simplest — no RPC, no thread — but `start()` blocks the caller for the script's initial synchronous slice, a synchronous spin past the first await cannot be killed in-process (the vm `timeout` covers only that first slice), and `dispose()` could only abandon an unsettling script on the host loop. The worker-thread engine keeps the same vm-context script surface while unblocking the host and making termination real. - **Background execution as the default** (CC's shape): deferred; foreground-synchronous matches `dsh-tool-subagent`'s cut, and background semantics should be designed ONCE across bash/subagent/workflow rather than per-tool. - **Workflow-layer JSON parsing for `agent({schema})`**: duplicating a seam concern at one consumer while the seam's capability flag stayed dishonestly `false`. -- **Meta embedded in the script as `export const meta = {...}`** (CC's exact format; the first cut shipped it): keeps scripts self-contained and CC scripts drop-in, but obtaining meta means evaluating model-written text on the HOST — the shipped extractor ran the literal in an empty timed vm context, yet reading the RESULT still executed script-controlled getters on the host stack outside any timeout, re-opening the host-spin hole the worker thread exists to close. A JSON parameter deletes the scanner, the evaluation, and the hole outright; the cost is that a CC script's meta header must move into the parameter (the body stays drop-in). +- **Meta embedded in the script as `export const meta = {...}`** (CC's exact format): keeps scripts self-contained and CC scripts drop-in, but obtaining meta requires evaluating model-written text on the host. Even an empty timed vm context cannot bound script-controlled getters when the host reads the resulting object. A JSON parameter removes the scanner, evaluation, and host-spin hole; the cost is that a CC script's meta header must move into the parameter (the body stays drop-in). - **`SchemaSpec` as the outputSchema type**: the author-facing DSL cannot express what arrives as data and cannot be validated against without conversion loss. - **A schema-object library (zod, or the repo's schemastery) for the structured-output subset**: the schema is wire data — plain JSON that crosses the vm realm boundary in `agent({schema})` and lands verbatim in the forced tool's parameters — exactly where live schema objects cannot sit; consuming raw JSON Schema at runtime would need a third-party converter on top (zod core only emits JSON Schema, not the reverse), and it would put a second schema language beside schemastery's config role. - **ajv for value validation**: it validates FULL JSON Schema, so the subset gate — the module's actual point, since every accepted keyword must be one the harness enforces — would remain hand-written regardless; it compiles validators through `new Function`; and it would be dsh-tools' first runtime dependency, all to replace the ~70-line value walker while the path-qualified, every-violation error reporting stays custom either way. @@ -63,4 +69,4 @@ A `workflow` tool mirroring `dsh-tool-subagent`'s synchronous shape: start, awai ## Consequences -The harness gains CC-compatible script orchestration: fan-out plans live in a rerunnable artifact instead of the parent context, and the structured-output half of the subagent seam is now real (the vocabulary stopped lying about `outputSchema`). What it cost, all bounded by the trust premise: a worker thread per run (~tens-of-ms spin-up), every hook crossing a message port as RPC, and a termination-path `agentsStarted` that degrades to the host-observed count; in exchange `start()` never blocks the host, a post-cancel grace ends in a real `worker.terminate()`, and the value boundary is serialization by construction. A worker thread is still NOT a security boundary — scripts share the model's trust level, and actual sandboxing names its exit (the isolated-vm/separate-process engine swap behind the seam). The fatal-vs-null strictness divergence from CC means a CC-authored script that RELIES on option typos dissolving to `null` behaves differently here — judged worth it to keep the repo's no-accepted-then-ignored rule. Consumers must hold the run handle for control (`cancel`/`dispose`); observers get data snapshots only, so no listener can extend a run's lifetime or corrupt another's view. +The harness gains CC-compatible script orchestration: fan-out plans live in a rerunnable artifact instead of the parent context, and `outputSchema` yields an authoritative structured child result across native and Code Mode presentation. The cost, bounded by the trust premise, is a worker thread per run (~tens-of-ms spin-up), every hook crossing a message port as RPC, and a termination-path `agentsStarted` that degrades to the host-observed count; in exchange `start()` never blocks the host, a post-cancel grace ends in a real `worker.terminate()`, and the value boundary is serialization by construction. A worker thread is still not a security boundary — scripts share the model's trust level, and actual sandboxing requires an isolated-vm/separate-process engine behind the seam. The fatal-vs-null strictness divergence from CC means a CC-authored script that relies on option typos dissolving to `null` behaves differently, preserving the repo's no-accepted-then-ignored rule. Consumers must hold the run handle for control (`cancel`/`dispose`); observers get data snapshots only, so no listener can extend a run's lifetime or corrupt another's view. diff --git a/docs/rfc/implemented/feature/2026-07-05-skill-system.md b/docs/rfc/implemented/feature/2026-07-05-skill-system.md new file mode 100644 index 0000000000..b3007529cd --- /dev/null +++ b/docs/rfc/implemented/feature/2026-07-05-skill-system.md @@ -0,0 +1,53 @@ +# RFC: Skill system — progressive disclosure instructions for agents + +Status: implemented + +## Problem + +Agent products have converged on a skill pattern: keep the request prompt small by listing only available instruction bundles, then load the full body when the model decides a task matches. Codex, Claude Code, OpenCode, and Kimi Code differ in details, but all separate discovery metadata from complete instructions so a workspace can carry reusable behavior without paying the full prompt cost on every turn. + +DeepSeek Harness uses the same primitive so project-specific review, plugin-authoring, and tool-usage guidance lives next to the workspace or the user's agent configuration instead of being hard-coded into the loop. + +## Decision + +`@deepseek-ai/dsh-skill` is the pure provider registry (`ctx.skills`), `@deepseek-ai/dsh-skill-local` is the shipped local filesystem provider, and `@deepseek-ai/dsh-tool-skill` owns the session-prefix catalog and model-facing loader tool. `dsh-agent-core` loads the registry, local provider, and consumer by default so stdio and ACP apps get the same behavior while embedded or remote providers contribute skills without changing the registry or consumer. Its `skills` config forwards `registry`, `local`, and `tool` branches to those owners. + +Provider plugins register synchronously during `apply()`. Provider catalogs return ranked candidates from awaited `list()` calls, where remote providers perform initialization, authentication, and discovery while honoring the lookup abort signal. The registry validates each candidate, resolves same-name skills first-wins by rank, provider registration order, and provider-local order, then sorts summaries by skill name for deterministic consumers. It caches only completed catalog snapshots and retries when a provider/runtime revision changes during discovery, so an unload cannot freeze a stale, unresolvable skill into a session prefix. Runtime `ctx.skills.register(...)` remains a convenience for embedded in-process skills and uses project-over-user priority; `runtime` is reserved as the registry-owned provider name. + +The local provider scans cwd-sensitive project roots, custom roots, and user roots in first-wins rank order: project `.dsh`, project `.agents`, `customSkillDirs`, user `.dsh`, then user `.agents`. The user `.dsh/skills` scan skips `.system` so a system-owned directory is not treated as normal user content. DeepSeek Harness does not ship built-in system skills; embedded or remote providers supply additional skills when configured. + +Each skill is either `/SKILL.md` or `.md` with YAML frontmatter. `name` and `description` are required; `whenToUse`, `disableModelInvocation`, and `metadata` are optional. Names are kebab-case. YAML frontmatter is parsed with the `yaml` package instead of `js-yaml` or a hand-written parser: `yaml` is the already-declared modern parser for this package's limited frontmatter needs, and a narrow parser would either reject valid YAML users expect to work or grow into an unreviewed YAML subset. + +Local skill filesystem I/O goes through `ctx.fs` when a filesystem service is loaded: project-root lookup probes `.git` with `resolve` and `stat`, root discovery uses `listDir`, and skill reads use `readText`. The Node filesystem remains a fallback for minimal contexts that mount `dsh-skill-local` without the fs seam. Missing roots, unreadable or malformed skill files, and transient provider `list()` failures degrade to warn-and-skip so one bad source does not make every agent request fail; malformed candidates still fail fast because they are provider contract violations. + +`dsh-tool-skill` contributes one user-role `` catalog through [`agent/session-prefix`](2026-07-07-session-prefix.md). The catalog contains sorted skill name and description only; it excludes bodies, paths, sources, providers, and routing hints. Descriptions are whitespace-normalized, XML-escaped, and capped by `catalogDescriptionMaxLength`, whose default is `500` and minimum is `3`. The session-prefix seam freezes the request-only catalog per loop instance and records it in the request header, preserving reconstructability without adding it to durable history. Full skill bodies are never included in the catalog. + +The `skill({ name })` tool loads one full skill for the current agent cwd and returns a tool result containing ``, ``, and ``. `resourceBase` supplies a directory, URL, or opaque provider-managed base for explicitly referenced scripts, references, and assets; resources load only as needed, without directory enumeration. An unresolved name reports that the skill is unknown or no longer available; invalid names and skills marked `disableModelInvocation` retain distinct tool errors. The tool result is the model-visible disclosure path. + +The data structures and catalog/tool contract are documented in [skills.md](../../../core-data-structures/skills.md), with service signatures in the generated [services catalog](../../../cordis-catalog/services.md). + +## Alternatives considered + +**Inject full skill bodies into every system prompt.** Rejected because it destroys progressive disclosure and makes every request pay for instructions that may not apply. + +**Expose skills only as slash commands.** Rejected because model-initiated loading is the core capability; slash/ACP command advertisement does not change discovery. + +**Put local filesystem scanning directly inside `ctx.skills`.** Rejected because coding agents, web agents, and future plugin ecosystems need different skill sources. A provider registry mirrors the subagent seam: the registry owns conflict resolution and consumers, while implementations own loading. + +**Use a system-prompt section.** Rejected because the rendered system prompt is a single string, while the catalog is a user-role `` message with request-only lifecycle requirements. [`agent/session-prefix`](2026-07-07-session-prefix.md) is the selected mechanism: it places the catalog ahead of derived history and records the composed message in the request header. + +**Materialize built-in DSH authoring skills under `~/.dsh/skills/.system`.** Rejected because bundled skills do not write user home on startup, and embedded or remote providers supply configured skills. + +**Recursively discover nested `**/SKILL.md`.** Rejected. Flat files and one-level directory bundles cover the configured roots while keeping duplicate handling and catalog order easy to reason about. + +**Hand-parse frontmatter.** Rejected because the accepted schema includes an open `metadata` object. A narrow parser would either reject valid YAML users expect to work or grow into an unreviewed YAML subset. + +## Consequences + +The agent-core spine includes one session-prefix contributor, one local provider, and one model-facing tool. Skill discovery is cwd-sensitive, so callers that create agents with different session cwd values can observe different project skill overrides by design. + +The catalog is deterministic for a fixed root set and runtime registration revision, but disk changes are not watched; discovery is memoized until runtime registration invalidates the cache or the process restarts. + +## Deferred + +Forked skill contexts (`context: fork`), direct user/slash invocation (`user-invocable`), parameter declarations and hints (`arguments` and `argument-hint`), and per-skill tool constraints (`allowed-tools` and `disallowed-tools`) are outside the shipped contract. The registry, local provider, and model-facing tool do not parse, advertise, or enforce these fields. diff --git a/docs/rfc/implemented/feature/2026-07-06-approval-seam.md b/docs/rfc/implemented/feature/2026-07-06-approval-seam.md new file mode 100644 index 0000000000..5d82226a96 --- /dev/null +++ b/docs/rfc/implemented/feature/2026-07-06-approval-seam.md @@ -0,0 +1,141 @@ +# RFC: The approval seam — one-shot permission decisions over a waterfall of answerers + +Status: implemented + +## Problem + +Two callers need to put one question — "may this specific action proceed?" — to a human: `tools/pre-execute`'s `ask` decision (including the Claude-Code hook bridge's `permissionDecision: ask`) and the [sandbox RFC](2026-07-06-sandbox.md)'s post-denial one-shot escalation retry. A shared seam keeps them from inventing separate outcome vocabularies, UI routing, cancellation, and audit trails, while guaranteeing that a deployment with no UI can never grant an unanswerable request. + +The routing problem is ownership: an approval prompt must reach the editor session that owns the asking agent (the ACP bridge multiplexes N sessions over one connection), fail closed for agents nobody owns (in-process subagents, tests), and stay out of deployments that compose no UI (headless, CI). + +## Decision + +One package, `dsh-user-approval` (`packages/ui/user-approval`), owning the vocabulary and the `ctx.approval` service — the MECHANISM. The POLICY — who answers, and whether a session is asked at all — lives outside it: answerers are `approval/request` waterfall listeners registered by the plugins that own the channel (the ACP bridge; future terminal UIs; test scripts), and a per-session policy tier can decide before any human is involved. Consumers (`dsh-tools`' ask routing, the sandbox escalation gate) resolve a question to a closed outcome and derive their own tool results from it. Deliberately ONE package, not the capability-seam three (see Alternatives). + +### How a deployment uses it + +One `cordis.yml` entry mounts the seam. Not loading it is the fail-closed opt-out: consumers deny unanswerable requests with zero approval code registered. + +```yaml +- id: approval + name: '@deepseek-ai/dsh-user-approval' + # config: + # policy: never # deployment default for sessions without an override; 'ask' when omitted +``` + +The entry alone provides mechanism, not a channel: with no answerer composed, every ask resolves `unavailable` and the asking tool call denies — fail-closed needs no configuration. Composing the ACP app (`@deepseek-ai/dsh-acp-agent`, as in [the sandbox example](../../../../examples/sandbox-acp-agent/README.md)) completes the loop: its bridge registers an answerer that prompts the owning editor session via `session/request_permission`, so a hook's `ask` or an escalation request surfaces as a one-shot Allow/Reject prompt attached to the already-streamed tool call. `policy: never` is the unattended stance — every ask auto-rejects deterministically, stated in the system prompt, no human in the loop. `policy` is validated against the closed list at plugin load; anything else throws. + +What a composed deployment observes: `allowed-once` lets exactly that call proceed; rejection, dismissal, and channel absence deny with three distinct reasons the model can tell apart; every ask lands a durable `approval/asked`/`approval/decided` pair on the asking agent's session log; nothing about a grant persists past the call that asked. + +One ask under this composition, verbatim from the sandbox example's recorded `escalation-approved` scenario — the model requests a sandbox escalation, the gate asks, the bridge prompts the owning editor, the user clicks Allow once: + +``` +tool/call bash {"command": "printf 'escalated\n' > escalated.txt && cat escalated.txt", + "sandbox_permissions": "workspace-write", + "justification": "the user asked to write escalated.txt in the workspace"} +approval/asked {"toolName": "bash", "callId": "call_00_…", + "reason": "escalate sandbox to workspace-write: the user asked to write escalated.txt in the workspace"} + → session/request_permission {"toolCall": {"toolCallId": "call_00_…"}, + "options": [{"optionId": "allow-once", "name": "Allow once", "kind": "allow_once"}, + {"optionId": "reject-once", "name": "Reject", "kind": "reject_once"}]} + ← the user picks "Allow once" on the prompt the editor attaches to the streamed bash call +approval/decided {"outcome": "allowed-once"} +tool/result "escalated" — this one call ran under the wider mode; the grant died with it +``` + +The `escalation-rejected` twin ends in `{"outcome": "rejected"}` instead: nothing executes, and the model's result carries the asker's verbatim fail-closed text (`the user rejected escalating this command to "workspace-write"`). A hook's `permissionDecision: ask` rides the identical wire; only the asker and its deny texts differ (§ Ask routing in dsh-tools). Headless, the same request skips the prompt entirely and settles `unavailable`. + +### Design detail + +#### The seam: mechanism and policy split + +After request validation and a successful `approval/asked` append, the answerer phase always resolves to a closed `ApprovalOutcome` — `allowed-once` / `rejected` / `cancelled` / `unavailable`. `ApprovalRequest` is a readonly same-process contract, so the service borrows its routing identity and cancellation signal instead of copying the record or capturing a parallel callback bundle. It dispatches the `approval/request` waterfall, races the request signal (abort settles `cancelled`; a late answer is discarded, never double-audited), contains a throwing answerer as `unavailable`, normalizes a rogue non-vocabulary return to `unavailable`, and lands the log-only audit pair `approval/asked`/`approval/decided` (paired by the branded `ApprovalRequestId`) on the request agent's session log. Request acceptance and either pre-commit audit append may still reject; returning a decision that could not be logged would violate the pair. Session owns post-commit observer containment, so a callback failure cannot turn an authoritative audit append into a rejected request or suppress the matching event. Grants are one-shot by definition: `allowed-once` authorizes the single asked-about action, never a class of future ones, and the service stores nothing between requests. `request()` also throws before appending anything when the agent's session has no open turn — the audit pair must be turn-enclosed, the turn being the durable log's commit/replay boundary (a bare event between turns is dropped as crash tail on reload); every ask path runs mid-turn already, and idle asks are a deferred design. + +Answerers are the policy, and they are `approval/request` waterfall listeners. The waterfall buys exactly what the seam needs: with zero listeners the dispatch falls through to the caller-supplied default — `unavailable`, so fail-closed needs no configuration and no code in any deployment; a listener that recognizes the request's agent answers by returning an outcome without calling `next()` (the decision slot is single-occupancy, first answer wins — the same documented semantics as the `fs/write-intent` gate); a listener that does not recognize the agent MUST delegate via `next()` so another answerer or the default gets the question; and listeners dispose with their owning fiber, so an unloaded UI plugin degrades the next ask to `unavailable` instead of leaving a dangling channel. Registration order across sibling plugins is not load-order deterministic (the loader starts siblings concurrently), so a deployment composes ONE terminal answerer and reserves `prepend` listeners for decide-or-delegate gates. + +`ApprovalRequest` carries the asking `agent` (routes the question; receives the audit events), the `toolName`, the optional exact `callId`, the asker's human-readable `reason`, and the optional `signal`. The caller retains ownership and honors the readonly contract for the duration of `request()`. The vocabulary is deliberately self-contained — it names the tool-call by the `CallId` brand from `dsh-llm` and never imports `dsh-tools` — because `dsh-tools` depends on `dsh-user-approval` (the ask routing) and a `ToolCallView` import would close a package cycle. It deliberately does NOT carry tool arguments: a UI answerer attaches the prompt to the already-streamed tool call via `callId` instead of re-rendering the call. + +#### Ask routing in dsh-tools + +`ToolRegistry.execute()` resolves an `ask` decision through the seam before the shared deny path: `allowed-once` proceeds to guards and dispatch, and the three non-grants deny with distinct reasons — "the user rejected…", "…was cancelled", "…no approval channel is available" — so the model can tell a human "no" from an absent channel. The seam is consumed opportunistically (`ctx.get('approval')`, the `tool-bash`/`agent-loop` pattern), not statically injected: with no ApprovalService, or after one unmounts, the next ask fails closed without gating the registry's fiber. An agent-less execution also fails closed — without an agent there is no session to audit to and no UI to route to. + +#### The per-session policy tier + +The seam also owns the session-scoped approval policy — the approval knob of the two-knob per-session switching design ([the sandbox RFC](2026-07-06-sandbox.md) § Per-session modes is the pattern's home: one log-only event per knob, a pure fold, THE write path, ACP config-option advertisement, and turn-anchoring). `ApprovalPolicy` is `'ask' | 'never'`, and `effectiveApprovalPolicy(events) ?? Config.policy` (default `'ask'`) decides every request BEFORE any interactive answerer: the service resolves a `'never'` session to `'rejected'` INSIDE `request()`, before dispatching the waterfall at all — no listener registration, including a later `prepend`, can sit ahead of it — while `'ask'` dispatches unchanged and falls through to fail-closed `'unavailable'` when nobody answers. Visibility follows the switching design's two layers with one asymmetry: the prompt section states ONLY `'never'` (deterministic, availability-independent — "you will be prompted" would overclaim in a composition with no answerer, and absence under a logged header is exactly how the narrator reads `'ask'` back), the narrator injects at most one coalesced notice per switch, and the audit pair still lands on every ask, including the policy's auto-rejections. + +#### The ACP answerer + +The bridge registers the first real answerer: it resolves the owning session through its existing `WeakMap` reverse map, issues `session/request_permission` with the request's `callId` as the `toolCall` reference and the one-shot options `allow_once`/`reject_once`, and maps the response — selected `allow-once` → `allowed-once`, any other selection → `rejected` (an unknown optionId from a non-conforming client never grants), client `cancelled` → `cancelled`. A request for a foreign agent — or one without a `callId`, since the protocol prompt must attach to a tool call — delegates via `next()`. A rejected RPC (client gone mid-prompt) propagates to the service, which contains it as `unavailable`. Whether a call ASKS at all is policy — a hook or `tools/pre-execute` plugin returning `ask` — never the bridge's own judgment. + +The answerer routes through the bridge's reverse-map ownership seam described by [the ACP support RFC](../../implemented/feature/2026-06-14-acp-agent-client-protocol.md), implementing the per-session permission ownership required by [the multi-session RFC](../../implemented/feature/2026-06-14-acp-multi-session.md). + +#### Audit, and what the model sees + +`approval/asked` / `approval/decided` are log-only session events (the `hook/invoked`/`hook/result` precedent): durable, replayable, never in the model transcript. The model's entire view of an approval is the tool result the asker derives from the outcome — reconstructability holds because that result is an ordinary logged `tool/result`. One `decided` lands per `asked`, whatever the outcome, including an already-aborted signal (settled `cancelled` without dispatching), a contained answerer failure, or a session observer that throws after either event is already appended. + +#### Entities and dependencies + +One package, no cycles: `dsh-user-approval` peers on `cordis`, `dsh-session` (event-map merge + append), `dsh-agent` (the `Agent` type), `dsh-llm` (`CallId`, via `dsh-brand`). `dsh-tools` and `dsh-acp` each peer on it; the escalation phase's asker lives in `dsh-tool-bash` (see [the sandbox RFC](2026-07-06-sandbox.md) § Escalation), so the sandbox family keeps its ZERO-edge relation (the executor contributes the per-call override mechanism, and transport seams never ask humans questions). The seam is one package, not the capability-seam three: the service body (dispatch + audit) has no replaceable implementation — the replaceable part is the answerer listeners, and those live with their owners (the bridge; future terminal UIs; test scripts). `@cordisjs/plugin-capability` stays orthogonal (a static grant registry answers "is this already authorized", not "ask the user now"), and `subagent-acp`'s child-side `permission` auto-answer is untouched — routing a child's approvals to the parent session is deferred (§ Deferred). + +### Testing + +Unit tier: the service's outcome branches (fail-closed default, first-wins slot, delegation, containment, rogue-value normalization, abort-before and abort-during with late-answer discard, fresh ids, fiber-disposal degradation), scoped routing, post-append observer throws on both audit events, and the policy tier (both values × dispatch/decide, a `'never'` decision unbypassable even by an answerer prepended AFTER the service, audit pair intact) in `dsh-user-approval`; the ask routing matrix (grant dispatches; three non-grant reasons pinned verbatim; unmounted and agent-less degrades; the registry's own exhaustiveness backstop against a non-conforming stand-in) in `dsh-tools`; the answerer (wire shape of the prompt, outcome mapping, unknown-option conservatism, foreign-agent and call-less delegation) driven through a real bridge + scripted client in `dsh-acp`. + +Snapshot tier: the harness accepts scripted permission answers (`permissionAnswers` in a scenario's `input.json`, consumed FIFO; an unscripted prompt answers `cancelled`, fail closed). The seam's wire is recorded end to end in the sandbox example's suite: both escalation branches drive `session/request_permission` through this seam over scripted answers (grant and rejection), and the recorded `mode-switching` scenario pins the `'never'` prompt sentence and the policy-switch notice ([the sandbox RFC](2026-07-06-sandbox.md) § Testing). + +## Deferred + +- **`allow_always` grant storage** — honoring a persistent grant means designing storage, scope identity (call? path? prefix? session? time window?), and revocation; until designed, only the one-shot options are advertised ([the sandbox RFC](2026-07-06-sandbox.md) § Escalation records the open scope question). +- **A recorded hook-driven `ask` through a composed answerer** — the human-prompt wire is recorded through the sandbox example's escalation branches. The hook matrix's `hook-cc-pretool-ask` pins the no-ApprovalService fallback denial, while the hook-producer-plus-answerer composition remains on the unit tier. +- **Routing a child agent's approvals to the parent session** — `subagent-acp`'s child auto-answers its own `permission` requests; surfacing them to the parent's editor is its own design. + +## Alternatives considered + +- **A single registered provider instead of waterfall listeners** — rejected: a `registerProvider()` surface forces every composition question — allowlist pre-filters, external hook deciders, scripted test answers, a policy gate in front of a human — inside one provider implementation. The waterfall gets composition, fail-closed absence, and HMR disposal from machinery the runtime already has; the seam's JSDoc pins the single-decision-slot convention instead of inventing a provider registry. +- **An inline `tools/pre-execute` permission gate in the ACP bridge** — rejected: prompting for every bridge-owned call hardwires the asking POLICY into the UI plugin, cannot serve a second asker (sandbox escalation happens after execution starts, with no pre-execute moment), and leaves hook-produced `ask` decisions without a shared mechanism. +- **The generic user-interaction seam (`ctx.userInteraction`)** — rejected as the approval mechanism: the two share a skeleton (route by agent, block for a human, handle absence), but approval's contract is narrower in every dimension that matters: a closed outcome vocabulary instead of free text, a protocol-native prompt attached to a tool call instead of a generic form, mandatory fail-closed absence, and audit events. Approval therefore does not ride the shipped `packages/ui/user-interaction` / `ask_user_question` elicitation path — an elicitation form is not a permission prompt, and a free-text answer is not a closed outcome; sharing provider plumbing stays open if the two ever converge. +- **Static optional injection in `dsh-tools`** — rejected: the vendored cordis `Inject` type has no optional flag — the object form maps service names to intercept config, and a declared inject gates the fiber. `ctx.get('approval')` is the documented opportunistic-consumption pattern (the `tool-bash` owner-token lookup, the loop's persistence probe), reads presence per call, and degrades correctly across HMR without extra machinery. +- **The capability-seam three-package split** — rejected: interface/implementation/consumer fits a seam whose implementation is swappable (bash-local vs bash-sandbox). Here the service body is fixed mechanism and the variable part is listeners that live with their owners — splitting would manufacture an implementation package with nothing in it ("don't split preemptively"). +- **Offering `allow_always` now** — rejected: the protocol can express it, but honoring it means designing grant storage, scope identity, and revocation (§ Deferred). Advertising an option the harness cannot honor manufactures doomed grants. + +## Consequences + +The implemented contract is pinned by the suites in Testing: + +- With an ApprovalService and an answerer composed, a hook's `ask` reaches a human and `allowed-once` dispatches the tool; every other outcome denies with its distinct reason. +- A `'never'` session auto-rejects every ask without prompting anyone, states the policy in its prompt, and narrates switches (the shared switching mechanics are pinned in [the sandbox RFC](2026-07-06-sandbox.md)). +- Every unanswerable path fails closed to `unavailable`: no service, no listener, a foreign or agent-less request, a throwing answerer, a rogue return value, or a dead client connection. +- Every `request()` routes through its readonly agent identity and lands exactly one `approval/asked`/`approval/decided` pair on that agent's log, replayable and invisible to the model transcript; post-append observer failures cannot split the pair. +- Prompts route per-session through the bridge's ownership map; one session's prompt can never reach another session's editor. +- A deployment with no ApprovalService emits no approval prompt or approval audit events and denies every `ask` request. + +Costs and accepted limits: + +- **Two decide-eager answerers race for the slot.** Sibling-plugin listener order is not deterministic, so the seam cannot referee competing terminal answerers — mitigated by convention (one terminal answerer per deployment; `prepend` only for decide-or-delegate gates) rather than a priority mechanism the event bus does not have. +- **Production exercise rests on one composition.** `ask` has two producer families — the hook bridges through `tools/pre-execute`, and sandbox escalation through its own gate — with the wire recorded in the sandbox example's snapshot suite, so the seam's real-world coverage is that one composition until more deployments compose it. +- **Ownership keys on `Agent` object identity.** The answerer resolves sessions through the bridge's existing WeakMap; every current path hands the same object through the loop and the seams, but a future boundary that clones or proxies agents would make the bridge delegate and fail closed — safe, but silently UI-less — and would need session-id matching instead. + +## FAQ + +Behavioral and usage questions only — every "why not X?" design question lives in [Alternatives considered](#alternatives-considered), whose job is exactly that. + +- **What happens in a deployment with no answerer at all (headless, CI)?** Every ask falls through the empty waterfall to `unavailable` and the tool call denies with the "no approval channel is available" reason. Fail-closed is the zero-listener default, not a configuration. +- **Can a grant persist — "always allow this"?** No. `allowed-once` authorizes the single asked-about action and the service stores nothing between requests; `allow_always` is deliberately not advertised until grant storage is designed (§ Deferred). +- **What does the model see of an approval?** Only the tool result the asker derives from the outcome — the audit pair never enters the transcript. The three non-grant reasons are distinct, so the model can tell a human "no" from a dismissed prompt from a missing channel. +- **Who decides whether a call asks in the first place?** Policy producers: a hook returning `permissionDecision: ask`, any `tools/pre-execute` listener, or the sandbox escalation gate. The seam and the bridge only route and answer; neither injects its own judgment about what deserves a prompt. +- **What happens when the user dismisses the prompt, or the turn aborts mid-ask?** Dismissal maps to `cancelled` with its own deny text. An already-aborted signal settles `cancelled` without dispatching; an abort during the ask discards the late answer — one audit pair either way, never two. +- **What if the client answers with an option the harness never offered?** Any selection other than the offered `allow_once` maps to `rejected` — an unknown optionId from a non-conforming client can never grant. +- **How do subagents' approvals route?** An agent no answerer owns delegates through the whole waterfall and fails closed — in-process subagents are deliberately unanswerable. `subagent-acp`'s child-side auto-answer is separate; routing a child's asks to the parent's editor is deferred (§ Deferred). +- **What does `policy: 'never'` actually change at runtime?** The service resolves every ask for that session to `rejected` before dispatching any answerer (in-service, so no registration order can bypass it); the system prompt states the policy; switches are narrated at boundaries; the audit pair still lands for every auto-rejection. +- **What happens across a hot reload, or when the UI plugin unloads mid-session?** Answerers dispose with their owning fiber, so the next ask degrades to `unavailable` instead of hanging on a dead channel; remounting re-registers the answerer with no catch-up state. +- **Where does the user see what they are approving?** On the tool call itself: the prompt attaches to the already-streamed call via `callId` — arguments included — and adds the asker's human-readable `reason`; the request carries no argument copy of its own. + +## Prior art + +In-repo precedents this design copies or contrasts with: + +- The `fs/write-intent` gate (`packages/fs/fs/`) — the documented single-occupancy decision-slot waterfall semantics (first answer wins, delegate via `next()`) the answerer contract reuses. +- `hook/invoked`/`hook/result` — the log-only audit-pair precedent `approval/asked`/`approval/decided` follows; [the hook-bridges RFC](2026-06-30-hook-bridges.md) ships `permissionDecision: ask`, the first producer. +- [The interception-seams RFC](2026-06-30-interception-seams.md) — the `tools/pre-execute` `allow`/`deny`/`ask` vocabulary whose `ask` this seam services. +- [The ACP support RFC](../../implemented/feature/2026-06-14-acp-agent-client-protocol.md) — the `WeakMap` ownership seam the answerer routes through; [the multi-session RFC](../../implemented/feature/2026-06-14-acp-multi-session.md) — the per-session permission-ownership blocker this implements. +- The opportunistic `ctx.get()` consumption pattern (`tool-bash`'s owner-token lookup, the loop's persistence probe) — how `dsh-tools` consumes the seam without gating its fiber on it. diff --git a/docs/rfc/implemented/feature/2026-07-06-explicit-tool-order.md b/docs/rfc/implemented/feature/2026-07-06-explicit-tool-order.md index 3604e70972..39345c2b53 100644 --- a/docs/rfc/implemented/feature/2026-07-06-explicit-tool-order.md +++ b/docs/rfc/implemented/feature/2026-07-06-explicit-tool-order.md @@ -17,7 +17,7 @@ The system-prompt assembly owns the canonical model-facing tool order, exactly w - The list must contain the rest entry exactly once and no duplicate names. - When `toolOrder` is unset, the canonical order is plain lexicographic name order (code-unit comparison, locale-independent), so determinism requires no configuration. -The policy is applied where the list is born: `assemble()`, before the `system-prompt/assemble` waterfall. The assembly canonicalizes the tools it collects from providers the same way it sorts sections by their `order` field — on the initial assembly, killing the registration-order entropy at its source. Everything downstream inherits the order untouched: the waterfall, the loop's `EpochHeader`, the `request/header` event, the deep-frozen request, and the dev invariant's cross-check all see one deterministic list, with no new loop change. +The policy is applied where the list is born: `assemble()`, before the `system-prompt/assemble` waterfall. The assembly canonicalizes the tools it collects from providers the same way it sorts sections by their `order` field — on the initial assembly, killing the registration-order entropy at its source. The waterfall therefore starts from one deterministic list; when a listener leaves that order intact, the loop's `EpochHeader`, the `request/header` event, the deep-frozen request, and the dev invariant's cross-check inherit it with no new loop change. Scope is deliberately narrow: this fixes the REGISTRATION-ORDER race, not plugin behavior. A `system-prompt/assemble` listener may still add, remove, or rearrange tools — same as it may edit sections after their sort — and owns the determinism of what it emits; the waterfall contract already demands deterministic listeners (the reconstructability invariant would catch a listener that diverges between build and replay). @@ -36,8 +36,8 @@ Config plumbing follows the `persona` precedent, and `toolOrder` sits beside it: ## Consequences -- Every assembly — and therefore every `request/header` event and model request — has a deterministic tool order on every host; the CI-vs-local golden flip is structurally gone. The default order is lexicographic, no longer registration order. -- `PromptAssembly.tools` itself is canonical, so every assembly consumer (the loop, waterfall listeners, any future prompt inspector) sees the model-facing order; provider registration order is observable nowhere downstream of the registry. +- Every registry-built assembly starts with a deterministic tool order on every host; absent an expert listener that deliberately changes it, every `request/header` event and model request inherits that order. The CI-vs-local registration-order flip is structurally gone, and the default is lexicographic. +- The initial `PromptAssembly.tools` is canonical, so waterfall listeners start from the model-facing order; provider registration order is observable nowhere before that cooperative seam. - The snapshot suite's single pinned request-header fixture (`text-turn`) carries the new canonical tool order; every other ACP snapshot keeps the header bulk scrubbed as `{{system}}`/`{{tools}}`, per the pinned-header design. - A pure tool reordering between steps is representable only as a `request/header` `'fallback'` snapshot (the name-keyed `ToolsDelta` cannot express it); with a stable canonical order such reorders no longer occur in practice, so the fallback path stays a safety valve. - The `toolOrder` key rides the app → `agent-core` → `SystemPrompt` forwarding chain, so deployments set it next to `persona` in the app config; `dsh-llm` and the agent loop are untouched. diff --git a/docs/rfc/implemented/feature/2026-07-06-sandbox.md b/docs/rfc/implemented/feature/2026-07-06-sandbox.md new file mode 100644 index 0000000000..e3e740b31c --- /dev/null +++ b/docs/rfc/implemented/feature/2026-07-06-sandbox.md @@ -0,0 +1,225 @@ +# RFC: The subprocess sandbox — confinement seam, native runners, escalation, and per-session modes + +Status: implemented + +## Problem + +A coding agent needs this product path: bash subprocesses — and the hook commands that ride them — execute under a restricted file sandbox by default; if and only if the sandbox actually denies an operation, the model may request one user approval for that same operation and, once granted, retry it once with wider permissions. An every-tool boundary is deliberately NOT the claim: fs/web/todo execute in-process where an `execve` wrapper is meaningless (§ In-process tools), and the cross-family boundary is staged follow-up work (§ Deferred phases). Without a shared vocabulary, every tool reinvents approval fields, denial parsing, retry matching, and permission-state hints. + +The harness is an SDK, so confinement must be a capability developers COMPOSE: whether to sandbox, and which backend per platform, belongs in the leaf `cordis.yml` as a first-class entry — not inside one executor's private machinery. And the first-choice runner, `bwrap`, is unusable on exactly the hosts a sandbox matters most (minimal containers, disabled unprivileged userns, LSMs that deny `mount`), so a fallback runner has to ship with the SDK rather than be assumed on the host. + +Confinement alone leaves two gaps. A denial with no escalation path is terminal — the model can only give up, which pressure-cooks operators into configuring `workspace-write` or `danger-full-access` globally and defeats the sandbox. And the model-visible knobs (the sandbox mode, the approval policy) change over an agent's lifetime — an ACP user flips a per-session setting, an operator edits `cordis.yml` while the process is down — while the model must never act on a stale belief about them: what IS the state on every request, what changed while the agent lives, and what changed while nobody was watching all need answers. + +## Decision + +One seam, one per-platform chain of local backends, one consumer, and two levers on top: a per-call escalation path and per-session runtime modes. Everything below composes from the leaf `cordis.yml`; nothing touches `agent-loop`. The scope is deliberately bounded: the phases this RFC names but does not design — per-session workspace root, cross-family fs enforcement, the `subagent-acp` consumer, more environments, a Windows chain — are listed under § Deferred phases, each a follow-up design, not a config knob. + +### How a deployment uses it + +Three `cordis.yml` entries turn an unconfined coding agent into the sandboxed product path; [`examples/sandbox-acp-agent`](../../../../examples/sandbox-acp-agent/README.md) is this composition, live: + +```yaml +- id: sandbox + name: '@deepseek-ai/dsh-sandbox-local' # the per-platform runner provider (ctx.sandbox) +- id: bash + name: '@deepseek-ai/dsh-bash-sandbox' # the confined executor, replacing dsh-bash-local behind ctx.bash + config: + mode: read-only # the deployment default every session starts from + workspaceRoot: !!js process.cwd() # the boundary workspace-write may write under +- id: approval + name: '@deepseek-ai/dsh-user-approval' # the escalation gate's channel (the approval RFC) +``` + +The swap is invisible to every consumer of `ctx.bash`: the bash tools, hook commands, and background tasks run exactly as before, spawned through the wrapped argv the provider returns. Deleting the `sandbox` and `bash` entries and loading `@deepseek-ai/dsh-bash-local` instead is the opt-out — execution is unconfined again and the escalation fields vanish from the tool schema, because they are capability-gated on the mounted executor, not on configuration. Omitting only `approval` keeps confinement but fails every escalation closed with its own error text. + +Misconfiguration fails loud: `mode` outside the closed vocabulary is rejected at plugin load, and a host with no usable backend throws the structured `SANDBOX_UNAVAILABLE` — at `confine()` before the command ever spawns — rather than degrading to unconfined execution. `runnerCommand` on `dsh-sandbox-local` is the operator's explicit assertion of a bwrap-compatible runner (chain and probes skipped); it doubles as the deterministic fake-runner seam for keyless tests. + +What the model then experiences: denied file effects come back as result facts with a `[sandbox: file access denied under mode]` marker plus standing instructions not to retry around them; under a confining executor the schema offers `sandbox_permissions` + `justification` for the one-approval escalated retry (validated strictly wider than the session's effective mode at execution); the system prompt deliberately does NOT state the sandbox mode — the model learns the boundary from the marker (which names the mode) when it hits it, instead of preemptively refusing work a standing declaration discourages. What an ACP editor experiences: a `sandbox-mode` and an `approval-policy` config-option select per session (each advertised only when its knob is composable), switchable at runtime; a sandbox switch simply changes what subsequent commands may do, while an approval-policy switch to `'never'` is stated in the prompt and narrated. + +The product path, concretely (the escalation arc is verbatim from the recorded `escalation-approved` scenario; the denial leg is pinned on the real-kernel e2e tier): + +``` +tool/result … [sandbox: file access denied under read-only mode] ← the write RAN; the kernel refused it +tool/call bash {"command": "printf 'escalated\n' > escalated.txt && cat escalated.txt", + "sandbox_permissions": "workspace-write", + "justification": "the user asked to write escalated.txt in the workspace"} + → the editor is prompted on this very call (session/request_permission through the approval seam); Allow once +tool/result "escalated" — THIS call ran under workspace-write and its result facts say so; the session stays read-only +``` + +Reject instead and nothing executes: the result is the verbatim `the user rejected escalating this command to "workspace-write"`, and the teaching makes that final — no re-ask. + +### Design detail + +#### Grounding — verified against the code + +- Runtime OS subprocesses exist at exactly two sites: the `ctx.bash` seam's single spawn (`packages/bash/bash-local/src/run.ts`; hook commands flow through `ctx.bash`, so bash confinement covers them transitively) and `subagent-acp`'s child agents (`packages/subagent/subagent-acp/src/run.ts`) — the second consumer that makes a shared seam due rather than preemptive under the [capability seams RFC](../architecture/2026-06-13-capability-seams.md)'s "don't split preemptively" rule. +- Everything else executes inside the harness process (fs is in-process `node:fs`, web is in-process `fetch`, every `ToolDefinition.execute()` closes over `ctx`): an OS sandbox wraps `execve` and cannot wrap an in-process function call, so "sandbox any tool" is policy at each tool's seam, never a mechanical transport change. +- `tools/pre-execute` (`allow`/`deny`/`ask`) exists, with `ask` serviced by [the approval seam](2026-07-06-approval-seam.md); the fs intent gates are version guards with no mode input yet. +- `dsh-bash`'s request/spec split (`BashExecRequest` → `resolve()` → `BashExecSpec`) carries per-call fields the way escalation needs — `owner` is the template: request-optional, spec required-but-nullable, carried verbatim — and the result types already speak `SandboxMode`, so a per-call policy field adds no dependency edge. +- The pinned-header snapshot design means a schema/description change churns at most one pinning fixture per suite, and the escalation fields are advertised only under a sandboxing executor — so they live in exactly one pinned header, the sandbox example suite's `mode-switching` fixture. + +#### The seam: `ctx.sandbox` + +`dsh-sandbox` owns the vocabulary and the `SandboxProvider` contract: `confine(argv, policy)` returns the argv to spawn INSTEAD of the caller's own — wrapped so the process and everything it spawns run confined — plus the `enforcement` completeness the selected backend achieves, its denial dialect (`denialSignatures`, the stderr substrings that backend's kernel prints on a denied file effect), and its runner-failure dialect (`runnerFailureSignatures`, how the runner ITSELF failing — and therefore the command never running — identifies itself); with no usable backend it throws the fail-closed `SANDBOX_UNAVAILABLE` error, never a silent unconfined passthrough. The vocabulary: `SandboxMode` (`read-only` / `workspace-write` / `danger-full-access`, FILE effects only — network and process visibility are not claimed), `SandboxEnforcement` (`full` / `partial`), `SandboxPolicy` (mode + workspace root). + +Policy rides each CALL, not the provider: two consumers may confine under different policies at the same instant (bash under `read-only` while a confined child agent keeps its state directory writable), and an approved escalated retry is a new call with a wider policy — inexpressible under a config-fixed provider mode. + +The seam confines SAME-WORLD subprocesses only: a backend shares the host's filesystem and kernel. Containers, microVMs, and remote executors are NOT backends of this seam — they replace whole capability implementations (`ctx.bash`, `ctx.fs`) as environment-coherent groups, because an agent whose bash runs in a container while its fs tools write the host lives in two split worlds. + +Left open, for the phase that needs them: whether network restriction arrives as a separate `network_mode` or merges into `sandbox_mode` once a runner enforces both, and whether `SandboxPolicy` grows extra writable-root grants now (the launcher already speaks `--rw `) or only when escalation needs them. + +#### Local backends and the shipped launcher + +`dsh-sandbox-local` selects BY PLATFORM, once per lifetime, and caches the verdict: each platform names its runner chain, a chain of one is selected directly — probing arbitrates between candidates, and a sole candidate leaves nothing to arbitrate — and a chain of several is probed FUNCTIONALLY in preference order (build and enforce a real profile, never `--version` — a present-but-unusable `bwrap` must fail its probe). Linux: `bwrap` first (its mount profile is closest to the mode vocabulary: whole tree read-only, fresh `/dev`+`/proc`, `workspace-write` adds an ephemeral `/tmp` and rebinds the workspace root; deliberately no `--unshare-pid` and no network claim), else the npm-distributed `landlock-run` Landlock launcher. darwin: `sandbox-exec` speaking a Seatbelt (SBPL) profile — allow-default with `(deny file-write*)` plus write allow-lists, every granted root canonicalized because Seatbelt matches resolved paths (`/tmp` IS `/private/tmp`) — unprobed, the sole candidate. A platform with no chain fails closed at `confine()`; an unprobed runner that turns out unusable fails closed at EXECUTION instead — it refuses to run the command, and every wrap carries `runnerFailureSignatures` (the runner's own error prefix, which also matches the shell's runner-not-found message) so the consumer classifies that as a SANDBOX failure, never a task failure: on either path the command neither runs unconfined nor slips through as a plain failure. A non-empty `runnerCommand` config is the operator's assertion of a runner that fully enforces the bwrap-shaped profile — chain and probes skipped; it doubles as the deterministic fake-runner seam for keyless tests. It is not exempt from fail-closed execution: its wrap carries argv0-scoped outer-shell failure shapes (`exec: : not found`, `: No such file or directory`, `: Permission denied`) as its runner-failure dialect, so a missing or unexecutable configured runner classifies as a sandbox failure like every other rung — never as a failing command, and never as a denial. + +The launcher is a ~300-line C program (plain C11 over the raw Landlock UAPI — no libraries beyond a statically linked musl, so the audit surface is that one file plus the kernel's stable syscall contract): `--ro ` / `--rw ` grants, `--`, the wrapped argv; it installs the ruleset on itself and `exec`s (rulesets are inherited across `execve`, and it sets `no_new_privs` before restricting); `--probe` enforces a maximal ruleset in a short-lived child and exits 0 only when the kernel actually enforces; launcher failures exit 125 without exec'ing. + +The launcher lives in its own repository and reaches the harness as the npm package family [`node-addon-landlock-run`](https://www.npmjs.com/package/node-addon-landlock-run) (the per-platform-package pattern of `node-addon-require-builtin` and esbuild): an entry package — `dsh-sandbox-local`'s one runtime dependency — plus per-platform binary packages selected at install time by npm's `os`/`cpu` fields. The entry package owns the launcher's CLI contract end to end (`launcherPath()` resolution with a never-existing fallback, the functional `probe()`, `grantArgs()` flag spelling), versioned together with the binary so probe-report parsing can never drift against it; the harness keeps only the policy side, `landlockProfileArgs()` mapping the mode vocabulary to grants. Native-only per-architecture builds, pack gates (binary presence, executability, ELF architecture), and the byte-pinned publish rehearsal are that repository's release pipeline; this repo's Landlock CI legs install the published family from the registry — the true consumer path — and prove real-kernel confinement through it. + +FIXME: Revisit the separate-repository boundary and try to maintain the launcher source and its platform package family inside this monorepo, so the native release surface and harness contract evolve together. + +Profile parity is honest rather than identical: under Landlock, `read-only` grants `--ro /` plus `--rw /dev/null` (the node, not `/dev` — the host's `/dev/shm` is a persistent shared tmpfs), and `workspace-write` grants the HOST `/tmp` where bwrap's is ephemeral; under Seatbelt, `read-only` likewise grants only the `/dev/null` literal, and `workspace-write` grants the host `/tmp` plus the per-user darwin temp dir (`os.tmpdir()` — the platform's real temp area for mkstemp-family tools; omitting it would deny what the mode promises). Every wrap carries the rung's denial dialect (`denialSignatures`: EROFS text under bwrap, EACCES under Landlock, EPERM under Seatbelt) so consumers match the active backend rather than a cross-runner union. Enforcement is honest per ABI level: an older kernel enforces the subset its ABI governs (path truncate is ungoverned before ABI v3), the probe's report line distinguishes the cases, and every confined result carries the structured `enforcement: 'full' | 'partial'` fact — refusing partial enforcement would deny the fallback to precisely the older-kernel hosts that need it. The bwrap and Seatbelt profiles govern every promised file effect by construction, so their passing probes always report `full`. + +#### The bash consumer + +`dsh-bash-sandbox` extends `LocalBashExecutor` (spawn mechanics, process-group kills, spill files, background tasks, credential scrub inherited verbatim) and hands `ctx.sandbox` the exact `['bash', '-c', command]` argv it is about to spawn. A sandbox denial is a RESULT FACT, not an error: the command RAN and the kernel refused a file operation, so `result.sandbox.denied` is orthogonal to `exitCode`/`signal`. Classification is conservative text inference over the collected stderr tail against the WRAP's own dialect, so a backend is never credited with a denial text its kernel does not speak (bare EPERM under a Linux runner names non-file boundaries the mode vocabulary does not govern); the known residual false positive is non-sandbox text in the active dialect (an ssh auth failure under Landlock, a refused `kill` under Seatbelt), and a structured runner signal wins once one exists. A RUNNER failure is the opposite of a denial and outranks it in classification (a runner's error text can itself contain denial words): the wrap's `runnerFailureSignatures` matching a failed run means the sandbox broke and the command NEVER RAN — the foreground path re-throws it as the structured `SANDBOX_UNAVAILABLE` error (the late twin of the confine-time throw, carrying the runner's first stderr line), a settled background task stamps `sandbox.runnerFailed` and `bash_output` renders its own marker — so a broken sandbox can never read as a failing command. + +The model's view is result facts only: the static tool description explains the denial marker (`[sandbox: file access denied under mode]`), encourages attempting commands that may be denied, and forbids retrying around a denial; when the escalation fields are advertised, a denied result additionally carries the escalation hint itself, so the sanctioned same-turn retry is prompted at the decision point rather than depending on the model recalling the description (§ Escalation). No prompt section states the sandbox mode (§ Per-session modes). + +#### Escalation: one approved wider retry after a denial + +The seam level is mechanism only. `BashExecRequest` carries `sandboxMode?: SandboxMode`, an explicit per-call policy input; `BashExecSpec` carries it required-but-nullable (the `owner` pattern: a forgotten field is a visible `undefined`, and `resolve()` is the one explicit defaulting step); `BashExecutor` exposes the capability fact `get sandboxMode(): SandboxMode | undefined` — `undefined` in the base class, the configured mode in `SandboxBashExecutor` — so the tool layer can advertise only what the mounted executor honors: composition truth, not configuration. The seam honors ANY explicit mode, including a narrower one; the wider-only ladder is escalation policy and lives in the tool. A non-sandboxing executor (`dsh-bash-local`) carries the field verbatim and confines nothing — the field reaching it means the caller bypassed the tool's gate, and its honest behavior stays unconfined execution, not a guess at enforcement it does not have. + +`SandboxBashExecutor.resolve()` stamps the effective mode — escalation grant > session override > configured default — so `run()`/`start()` read the spec, never the config. The `danger-full-access` branch, the confine call, and the result facts all key off the spec's mode, and the per-task facts map carries each task's mode alongside its wrap facts (`notifyTaskDone()` stamps from the map entry): one escalated call — foreground or background — reports the mode it ACTUALLY ran under while every neighbor keeps its own. + +The tool gate advertises two extra parameters exactly when `ctx.bash.sandboxMode` reports a confining mode at registration: `sandbox_permissions`, an enum of the closed escalation-target vocabulary — `workspace-write`/`danger-full-access`, every mode a session could ever escalate TO — and `justification`, required together with it. The enum is deliberately NOT cut down to the modes wider than the executor's DEFAULT: schemas are registry-global while the effective mode is per-session and switchable, so a default-relative ladder strands a session overridden NARROWER than the default (with a `danger-full-access` default and a `read-only` override it would advertise nothing at all — confined, but with no lever). Strict widening is instead enforced at EXECUTION against the call's effective mode (session override ?? executor default): a request that is not strictly wider fails closed with its own text and prompts no one. An escalating call resolves approval BEFORE anything executes — no `ctx.approval` composed, or no agent on the execution, fails closed with its own text; otherwise `ctx.approval.request({ agent, toolName: 'bash', callId, reason, signal })` with the audit-self-contained reason `escalate sandbox to ${mode}: ${justification}`, while the UI attaches the prompt to the already-streamed call (the command is visible there; the approval RFC's no-arguments rule holds). The four outcomes map to distinct results: `allowed-once` stamps `sandboxMode` onto the bash request and proceeds; `rejected`, `cancelled`, and `unavailable` each produce their own error text, so the model can tell a human "no" from a dismissed prompt from a missing channel. The grant is consumed by the very call that asked; nothing is stored. + +The tool description teaches — and a denied result itself prompts — the SAME-TURN flow when the fields exist: on a denial a wider mode would cure, escalate immediately in that turn by retrying the exact command once with `sandbox_permissions` (the narrowest mode that suffices) + `justification`, without detouring through chat to ask first — the approval prompt raised by the retry IS how the user consents. Never speculatively: an escalation is grounded in a real denial — normally the one the command just hit, up front only when the session already denied the same access — and a prompt stating approvals are disabled turns the exception off entirely; a rejected escalation is final for that command. Denial-grounding is deliberately model discipline plus human judgment, not harness bookkeeping — the human sees the exact command and justification on the prompt (see Alternatives for why hard-matching is rejected). No new session events anywhere: the attempt is an ordinary `tool/call` whose logged arguments carry the two fields, the decision is the approval seam's `approval/asked`/`approval/decided` pair, the outcome is an ordinary `tool/result` whose sandbox facts name the mode it ran under. The asker lives in `dsh-tool-bash`, NOT the executor: a transport seam has no `agent`, no `callId`, and no business asking humans questions. + +Left open, recorded for the phase that picks them up: what a grant's scope identity is beyond the sandbox mode — the exact call, a path, a command prefix, the session, a time window — the question `allow_always` grant storage must answer before that option can be advertised; how cancellation behaves while an approval prompt is pending; and how escalation is defined for `run_in_background` denials that arrive via `bash_output`. + +#### Per-session modes: the session log as the store + +``` +effective(session) = findLast(the session's own knob events)?.value ?? the composition-config default +``` + +The default is composition config (`cordis.yml`) — operator-owned, process-wide. A runtime switch is a SESSION-SCOPED override recorded as one log-only event in that session's own log. Restart immunity (resuming a session replays its log, so overrides come back with zero catch-up machinery) and multi-session isolation (one editor tab's `workspace-write` cannot disturb another's `read-only`) both fall out by construction, and no external config store exists anywhere. + +**One event per knob, owned by its domain** — the merge-extensible `SessionEventMap` idiom every existing event family already follows (`approval/*` in `dsh-user-approval`, `hook/*` in the hooks packages): + +```ts +interface SessionEventMap { + 'bash/sandbox-mode': { mode: 'read-only' | 'workspace-write' | 'danger-full-access' } + 'approval/policy': { policy: 'ask' | 'never' } +} +``` + +Each owner exports the same three-piece kit: the event declaration, a pure fold (`effectiveSandboxMode(events)` / `effectiveApprovalPolicy(events)` — a `findLast`, typed to the domain's closed union), and THE write path (`setSandboxMode(session, mode)` / `setApprovalPolicy(session, policy)` — a switch IS its event; nothing mutates state out of band). No shared owner service, no generic facts map, no registry: a third knob copies the ~40-line pattern into its own package. Execution follows the fold on both sides — the bash tool's per-call stamp reads it as the middle rung of the § Escalation precedence chain, and the approval seam's `'never'` gate is [the approval RFC](2026-07-06-approval-seam.md)'s side of the same pattern. + +**Visibility is deliberately asymmetric between the knobs.** The SANDBOX mode is stated nowhere and its switches are not narrated: a standing "you are read-only" declaration teaches the model to refuse preemptively (observed live: sessions where the model would not even attempt a write it could have escalated), while the denial marker already names the mode the command ran under at exactly the moment the boundary matters — behavior, not belief, carries the state, and a switch simply changes what the next command does. The APPROVAL policy keeps both layers, because its failure mode is the opposite: an auto-rejected ask under `'never'` returns "the user rejected …" wording no behavior can disambiguate, so the prompt states `'never'` (and ONLY `'never'` — an `'ask'` promise is unknowable without asking, and absence under a logged header is how the narrator reads `'ask'` back), and an `agent/pre-step` narrator injects at most one coalesced notice per policy switch: idle flip-flops collapse to one notice at the next turn's first step, a net-zero round trip to none, and a mid-turn change is narrated no later than the next step. Its "last told" is in-memory with a log-derived fallback (the folded header's system text parsed against the closed candidate sentence; LAST occurrence wins, so a persona quoting it cannot shadow the real section), so restarts lose nothing; attribution is positional (a knob event after the log's last `request/header*` reads `changed by the user`, a drift with no such event reads `changed by the operator/config`). + +**The editor surface** is protocol-native [Session Config Options](https://agentclientprotocol.com/protocol/session-config-options) — the spec's replacement for session modes (slated for removal in ACP v2), already SDK-typed. The bridge advertises one independent `select` per composable knob — `sandbox-mode` (category `mode`) iff the mounted executor confines, `approval-policy` iff the approval seam is composed — with `currentValue` folded from each session's own log, in `session/new` and `session/load` responses. `session/set_config_option` validates against the same closed lists, routes to the domain setter, and returns the complete refreshed state (the spec contract). + +**Anchoring: turn-enclosure is the commit boundary.** The turn-enclosure contract makes a bare between-turns append invalid (the JSONL backend treats a post-`turn/end` tail as crash garbage; dev invariants throw). A switch while a turn is open appends immediately — openness read from the LOG (last boundary event is `turn/start`), not `agent.status`, which stays `running` between queued turns. An idle switch is held on the bridge's session record and anchored at the next turn's `agent/prompt-submit` — inside the turn, before anything in it assembles or executes, last write per knob, and OUTSIDE any `session/event` emit (appending from inside that feed reorders events for later-registered listeners — a bug the dev invariants caught live). Until anchored, the switch exists only in bridge memory: responses overlay it truthfully, and a crash before the next turn reverts it — `session/load` then reports the fold's truth, so the editor UI self-corrects rather than lies. + +#### In-process tools + +fs/web/todo execute in-process, so their sandbox semantics are policy at their seams: the fs intent gates deciding by the shared mode vocabulary (§ Deferred phases, cross-family) make `read-only` a real boundary instead of a bash-only approximation — until then the contract says so honestly. No generic per-tool sandbox runtime: a host-mediated tool leaves the process only by returning declarative effects the host validates, which is a rewrite, not a wrapper. + +FIXME: Revisit this tool-local boundary. The follow-up design needs to determine whether sandboxing becomes a global harness capability that applies uniformly to every tool, instead of expressing in-process enforcement independently at each tool seam. + +### Testing + +- Unit tier (no real runner anywhere): profile dialects, per-platform chain selection (sole candidate unprobed, no chain fails closed, multi-candidate probe order), verdict caching, the fail-closed end, probe-report parsing, and the launcher/`sandbox-exec` CLI contracts via fake runner scripts in `dsh-sandbox-local`; wrapping, policy hand-off, fact stamping, and runner-failure-outranks-denial classification (foreground throw, background `runnerFailed` fact) against a fake provider in `dsh-bash-sandbox`; the error's structured identity in `dsh-sandbox`. The escalation matrix spans the three bash packages: verbatim carry-through in `dsh-bash-local`, stamp/branch/per-task-facts in `dsh-bash-sandbox`, and the capability gate, `justification` pairing, fail-closed texts (pinned verbatim), and grant stamping in `dsh-tool-bash`. The switching surface pins the folds, the stamping precedence, the `'never'` gate, per-session section rendering, the full narrator matrix (cold start, coalescing, net-zero, resume drift with operator wording, positional attribution, persona-shadow hardening), and the bridge's advertisement gating, validation rejections, idle-vs-mid-turn anchoring (dev invariants mounted), and `session/load` reporting over a real two-process JSONL round trip. +- Keyless real-runner e2e, split along the seam and per rung: CI's `sandbox-e2e` matrix runs bwrap and Landlock on Linux (the Landlock leg once per architecture, each confining through the registry-installed launcher) and Seatbelt on macOS against real kernels, failing on a silent all-skip. World-proofs live in `dsh-sandbox-local` (denied writes absent on disk, workspace writes landing, temp-area grants pinned, kernel denial text matching the advertised dialect) and `dsh-bash-sandbox` (the through-`ctx.bash` consumer proofs, including denied-then-overridden-write-lands). This package's own publish path is rehearsed without publishing (`packed-install.e2e.ts`): `pnpm pack`, tarballs installed into a throwaway consumer with the launcher family resolving from the registry, plain `node` confining through the INSTALLED launcher — asserted executable apart, so a mode-stripped binary can never masquerade as a non-enforcing kernel. The switching surface has its own keyless e2e (`examples/sandbox-acp-agent`): the real `cordis.yml` tree advertises both options, honors switches end to end, and rejects out-of-vocabulary values. +- With-key e2e (`examples/sandbox-acp-agent/tests/escalation.e2e.ts`): real model + real runner + the REAL bridge answerer, world-verified — denied under `read-only`, escalates with justification, the scripted editor grants and the retried write lands on disk, while a rejected escalation leaves no write. Self-skips without `DEEPSEEK_API_KEY` or a usable runner (e2e.yml installs bubblewrap so it actually executes in CI). +- Snapshot tier (`examples/sandbox-acp-agent/tests/acp.snapshot.ts`): the keyless config-option wire; the recorded mode-switching arc as the suite's pinned header — necessarily, since mid-session switches emit the `request/header-delta`s the uniformity guard licenses only in the pin — committing both switches, the prompt-section delta and one "changed by the user" notice per knob, and a confined write landing under the switched mode; and both recorded escalation branches over scripted `permissionAnswers` (grant runs confined under `workspace-write`; rejection executes nothing and pins the fail-closed text). Replay re-executes every fixture's bash calls under the host's real runner (ci.yml's snapshot lane installs bubblewrap). Deliberately absent: a fixture carrying a real DENIAL — denial stderr is the backend's dialect and would pin a fixture to its recording platform; the escalation prompts assert the prior denial instead, and the denial→marker path stays on the tiers above. + +## Deferred phases + +Each phase gets its full design when picked up, validated against the code at that time, and lands with unit, real-API e2e, and snapshot coverage at the tiers it touches. + +- **Per-session workspace root** — the executor's write boundary stays config-fixed for its lifetime while each ACP session has its own cwd; a per-session root rides the same per-call policy carrier once designed. +- **Cross-family boundary** — the fs intent gates decide by the shared mode, making `read-only`/`workspace-write` real boundaries beyond bash. +- **Second consumer** — `subagent-acp` optionally confines child agents (per-call policy; unconfined default — a child agent must write its own persistence). +- **More environments** — an environment-coherent capability group example (e.g. bash+fs against one container). +- **Windows chain** — `PLATFORM_CHAINS.win32` is reserved and empty (fail-closed); filling it means a confinement runner from the AppContainer/restricted-token family, shipped from its own repository on the `node-addon-landlock-run` template, plus its profile dialect and denial/runner-failure signatures. + +## Alternatives considered + +- **Command-string heuristic preflight** — rejected: cannot understand expansion/subprocesses/symlinks; the strict attempt (run it, let the kernel decide) is the only trustworthy denial signal. +- **Functionally probe even a platform's sole backend** — rejected: probing arbitrates between candidates; with one there is nothing to decide, and probe cost taxes the first confined command of every session (prohibitive for heavy future backends). The runner's own exec-time fail-closed refusal plus `runnerFailureSignatures` classification carries the safety property instead. +- **Commit the built launcher binaries** — rejected: a binary in a diff is unreviewable and churns history; reviewed source + native CI builds + the launcher repo's byte-pinned publish rehearsal keep bytes out of every tree. +- **Compile the launcher on install** — rejected: pushes a C toolchain onto every consumer; a fallback that exists only where a compiler happens to be is not a fallback. +- **Cross-compile both architectures from one builder** — rejected: requires carrying a pinned cross toolchain (rustup targets, zig, or a container image) solely to rebuild two ~70 KB binaries; per-architecture native runners already exist and each builds its own platform package (the `node-addon-require-builtin` model, the launcher repo's own pipeline). +- **No fallback (bwrap or fail closed)** — rejected: concentrates failure on the hosts a sandbox matters most, degrading to `danger-full-access` by resignation. +- **Keep the mechanism inside `dsh-bash-sandbox`** — rejected: blocks the existing second consumer, makes future phases read mode out of a bash plugin's config, and cannot express escalation. +- **Config-fixed mode on the provider** — rejected: one mode per process; cannot serve concurrent consumers with different policies nor the one-shot widened retry. +- **One interface spanning containers/VMs too** — rejected: `confine(argv)` presupposes a shared filesystem; environment isolation is capability-sibling backends deployed as coherent groups. +- **Generic ToolRuntime wrapping any tool** — rejected: mechanically false for in-process tools (closures over `ctx`); the declarative-effects rewrite is unjustified for fs/web/todo. +- **Ask inside the executor (`dsh-bash-sandbox`)** — rejected: no `agent` to route through, no `callId` to attach the prompt to; adding them teaches a transport seam about sessions and UIs — the tool layer holds both and owns the model-facing vocabulary. +- **Auto-retry inside the same tool call** — rejected: a hidden re-entry the log cannot reconstruct: one `tool/call` would have produced two executions with different policies — the retry is a NEW logged call with its own arguments and result facts. +- **Advertise the escalation fields unconditionally** — rejected: under `dsh-bash-local` they are a dead lever — advertising an option the harness cannot honor manufactures doomed grants; capability-gating costs one registration-time read. +- **A default-relative escalation ladder (advertise only the modes wider than the executor's registration-time default)** — rejected: per-session overrides make the default the wrong baseline — a session switched narrower than the default loses exactly the lever it needs, and under a `danger-full-access` default the fields vanish entirely while a `read-only`-overridden session stays confined with no escalation path. The enum pins the closed target vocabulary; strict widening is a per-call execution check against the session's effective mode. +- **Per-session dynamic tool schemas** — rejected: schemas are registry-global by design (one assembly vocabulary, the pinned-header snapshot contract), and re-registering per session would buy only what the execution-time strict-wider check already guarantees, at the cost of a per-session schema surface and header churn on every switch. +- **Hard-match the retry to a prior denial** — rejected: command-string identity is fragile (quoting, `workdir`, env prefixes, a pipeline retried as its failing stage) — false-rejects honest retries or is trivially satisfied; the real boundary is the human seeing command + justification. Revisit only if `allow_always` grant storage ever needs machine-checkable scopes. +- **A generic `env/state` facts map with an owner service** — rejected: approval and sandbox compose independently, so neither's state may drag in a third package; single-key folds are one `findLast` each, dissolving the owner service; no invariant spans the knobs, so atomic multi-key patches bought nothing. +- **Narrate via `agent/user-message` + a bus event** — rejected: it presupposes a turn-entry seam that does not exist (the real seam is `agent/prompt-submit`), and pre-step's position serves both the coalesced turn-entry notice and the mid-turn immediacy bound with one listener. +- **A standing prompt statement of the sandbox mode (+ a switch narrator)** — shipped first, then removed on live evidence: with `Bash commands run under the "read-only" file sandbox.` in every request, the model refused to ATTEMPT denied-then-escalatable work (five of twelve turns in the first manual session ended with zero tool calls), turning the sandbox into a soft lockout. The denial marker names the mode at the moment it matters and the escalation fields carry the recovery; the approval knob keeps its statement because an auto-rejection is behaviorally indistinguishable from a human "no". +- **Track "last told" with its own bookkeeping events** — rejected: the `request/header*` fold already records the exact prompt the model saw; parsing the closed candidate sentences back replaces a second bookkeeping stream — events are needed only where they ARE the store. +- **ACP session modes instead of config options** — rejected: one mode list cannot carry two orthogonal knobs; config options are the spec's designed surface and modes are slated for removal in ACP v2. + +## Consequences + +What shipped pins — the tiers in Testing hold each: + +- A denied command retried with `sandbox_permissions` + `justification` prompts the user through the composed answerer chain; a grant runs THAT call under the wider mode (result facts say so) while every other call keeps its own effective mode; every non-grant outcome produces its distinct error text and executes nothing. +- The escalation fields exist exactly when the mounted executor confines; a request that is not strictly wider than the call's effective mode fails closed with its own text and prompts no one; a deployment with no ApprovalService fails escalating calls closed and leaves plain calls untouched. +- The system prompt never states the sandbox mode (an approval `'never'` policy is the one stated knob), and the whole exchange — headers, knob events, notices, approvals, results — reconstructs from the session log alone, with no event types beyond the two knob events. +- N idle-time flips produce at most one anchored event per knob (a net-zero sequence anchors none — a no-op push from a client echoing current selections records nothing); an approval-policy switch is narrated in at most one coalesced notice; a mid-turn sandbox switch is honored by the next call's stamp. +- A resumed session's overrides apply and are reported to the editor with no special-casing; a default changed while the process was down is narrated before the session's first new request, attributed to the operator. +- Two concurrent sessions never see each other's state, notices, or config options. +- `agent-loop` is untouched — everything rides `systemPrompt.section`, `SessionEventMap` merging, `agent.inject()`, `agent/pre-step`, `agent/prompt-submit`, and the ACP handler surface. + +Costs and accepted limits: + +- **The one-wrapper illusion is given up knowingly.** A `tools/pre-execute` wrapper plus prompt conventions does not solve sandbox approval — the correct design costs structured denials, native runner probes, per-call policy carriage, and consistent cross-family enforcement, and this design pays it. +- **`read-only` is not yet a cross-family boundary.** Until the fs intent gates decide by the shared mode, the claim holds for bash only; the contract says so honestly (§ In-process tools). +- **Windows has no backend.** Its chain slot is reserved empty — fail-closed, never a fallthrough; filling it is a deferred phase. +- **The Seatbelt rung leans on Apple's deprecated-but-shipped `sandbox-exec` CLI.** As darwin's sole candidate it is selected without probing, so a future removal surfaces at execution as the runner-failure classification — re-thrown `SANDBOX_UNAVAILABLE`, the command never runs; fail closed, never open. +- **Landlock confinement is only as complete as the running kernel's ABI.** Reported as `enforcement: 'partial'` rather than refused — the deliberate trade that keeps the fallback available on older-kernel hosts. +- **The launcher arrives as a registry dependency.** Trusted through its own repository's release pipeline (reviewed C source, native CI builders, byte-pinned publish rehearsal) plus this repo's version pin — the real-kernel e2e legs are what vouch for behavior through the installed bytes. +- **The model may over-ask.** Escalating without denial grounding, or picking `danger-full-access` where `workspace-write` suffices: the description steers and the enum forces the ladder, but the human prompt is the actual gate; the `approval/asked` reasons make over-asking auditable, and a `prepend` policy answerer can auto-reject patterns a deployment never wants. +- **The advertised target set is static while the effective mode is per-session** (schemas are registry-global) — a session already at the widest mode is still offered the fields. Harmless by construction: the strict-wider check at execution, not the enum, is the safety boundary — a non-widening request fails with its own text and never prompts anyone. +- **A granted escalation is not a working sandbox.** An unavailable backend still fails closed even for a granted escalation to a confining mode — at `confine()` when the platform has no chain or every probe fails, at execution when an unprobed sole runner refuses (classified as a sandbox failure, not a command failure) — while a granted `danger-full-access` run never touches the provider at all: there the grant, not the probe, is the authority. +- **An idle switch lives in bridge memory until the next turn anchors it.** A crash in that window reverts it (reported honestly on `session/load`), and a session that never runs another turn never persists it — accepted, with the loop-owned idle commit turn named as future work if durability becomes a requirement. +- **The approval narrator's restart baseline parses prompt prose.** The closed candidate sentence is owned by the writing module itself, so a wording change is a coordinated writer+parser edit in one file; a session whose headers predate the section silently adopts the current policy without a notice. +- **The approval section is still a dynamic prompt surface** (a `'never'` switch breaks provider prompt-prefix caching for that session). Accepted: policy switches are rare, and a model acting on a stale `'never'` is worse. The sandbox knob no longer touches the prompt at all. +- **The model may hold a stale belief about the sandbox mode** (nothing announces a switch). Accepted deliberately: the next attempt's marker or success corrects it, and the observed failure mode of announcing — preemptive refusal — is worse than one wasted retry. + +## FAQ + +Behavioral and usage questions only — every "why not X?" design question lives in [Alternatives considered](#alternatives-considered), whose job is exactly that. + +- **A command came back with `[sandbox: file access denied under read-only mode]` — did it fail?** It RAN, and the kernel refused a file effect: the denial is a result fact orthogonal to exit code. The teaching forbids retrying around it; the one sanctioned move is the same command retried once with an escalation request. +- **How is a BROKEN sandbox told apart from a failing command?** Runner failure outranks denial in classification: a failed run matching the wrap's `runnerFailureSignatures` means the command NEVER ran — foreground re-throws the structured `SANDBOX_UNAVAILABLE` with the runner's stderr line, a background task stamps `sandbox.runnerFailed` and renders its own marker. A broken sandbox can never read as a failing command, and the command never runs unconfined. +- **What happens on a platform with no backend — Windows today?** `confine()` throws the fail-closed `SANDBOX_UNAVAILABLE` and the command never spawns; `win32` is a reserved EMPTY chain, pinned by test to fail closed identically until a Windows runner fills it (§ Deferred phases). +- **`bwrap` is installed on my host but unusable (disabled unprivileged userns, an LSM denying `mount`) — what happens?** The chain probe is functional — it builds and enforces a real profile rather than checking `--version` — so a present-but-unusable `bwrap` fails its probe, selection falls to the registry-installed Landlock launcher, and the verdict is cached for the provider's lifetime. +- **Does the sandbox restrict network or process visibility?** No — `SandboxMode` claims FILE effects only; the bwrap profile deliberately does not unshare pid, and no backend claims network. Whether network restriction becomes its own knob is left open in § The seam. +- **Which tools actually run confined?** OS subprocesses through `ctx.bash` — the bash tools, and hook commands transitively. fs/web/todo execute in-process, where an `execve` wrapper is mechanically meaningless; their `read-only` semantics arrive with the cross-family deferred phase, and until then the contract says bash-only honestly. +- **Does a granted escalation persist, or cover background tasks?** Neither: the grant is consumed by the very call that asked (foreground or background), that one call reports the mode it actually ran under, and every neighbor keeps its own. How escalation should be DEFINED for a background denial that only surfaces later via `bash_output` is left open in § Escalation. +- **When does an editor's mode switch take effect?** Mid-turn: appended immediately, honored by the very next call's stamp. Idle: held on the bridge's session record, anchored at the next turn's `agent/prompt-submit`, with N flips coalescing to at most one event (none if net-zero); a crash before anchoring reverts it and `session/load` reports the truth. The model is not told — its next command simply behaves under the new mode. +- **What survives a restart — and what if the operator changed the config default while the process was down?** Overrides replay from the session log (`effective = fold ?? config`), so a resumed session keeps its modes with zero catch-up machinery; a default that drifted offline changes behavior the same way a switch does (the approval policy, being stated, is additionally narrated with operator/config attribution). +- **What does `enforcement: 'partial'` on a result mean?** The selected backend enforces the subset its kernel ABI governs — e.g. Landlock before ABI v3 does not govern path truncate — and says so structurally instead of refusing the host; the probe's report line distinguishes the cases. The bwrap and Seatbelt profiles govern every promised file effect by construction, so they always report `full`. + +## Prior art + +In-repo precedents this design copies or contrasts with: + +- [The capability-seams RFC](../architecture/2026-06-13-capability-seams.md) — the interface/implementation/consumer split and the "don't split preemptively" timing rule the second consumer satisfied. +- The `dsh-bash` request/spec split and its `owner` field ([the bash vocabulary catalog](../../../core-data-structures/bash.md)) — the per-call carrier template `sandboxMode` rides, and the explicit-`resolve()` defaulting convention. +- [The approval seam RFC](2026-07-06-approval-seam.md) — the channel escalation asks through; its answerer waterfall, audit pair, and one-package rationale are recorded there. +- [Event-sourced sessions](../architecture/2026-06-11-event-sourced-sessions.md) and [the turn-enclosure invariant](../architecture/2026-06-15-turn-enclosure-invariant.md) — the log-as-store foundation the per-session modes fold over, and the commit boundary the anchoring design obeys. +- [The interception-seams RFC](2026-06-30-interception-seams.md) — the `tools/pre-execute` vocabulary the escalation gate deliberately does not reuse (an escalating call has no pre-execute moment of its own). diff --git a/docs/rfc/implemented/feature/2026-07-12-subagent-persona-tool-filter-and-depth.md b/docs/rfc/implemented/feature/2026-07-12-subagent-persona-tool-filter-and-depth.md new file mode 100644 index 0000000000..5b6eeb4ccb --- /dev/null +++ b/docs/rfc/implemented/feature/2026-07-12-subagent-persona-tool-filter-and-depth.md @@ -0,0 +1,92 @@ +# RFC: Configure subagent persona, tool visibility, and depth + +Status: implemented + +## Problem + +A reusable subagent provider answers how to run a child, but different delegation tools need different child behavior. One deployment may want a reviewer persona, a research-only tool set, or a hard recursion bound without creating a new provider for every combination. + +These controls affect the child's first model request and therefore cannot be installed after the child is visible. They also need honest provider support: an ACP backend cannot silently accept an in-process-only tool filter, and a filter must not be described as a security boundary when every plugin runs in the same trusted process. + +## Decision + +Subagent starts have three independent composition controls: `persona`, `toolFilter`, and `maxDepth`. A provider advertises support for each control, the service rejects unsupported requests before starting a run, and an in-process provider installs the requested composition while the child is still unpublished. + +The controls answer different questions: + +| Control | Question | Result | +|---|---|---| +| `persona` | What role instructions replace the deployment persona for this child? | A child-local prompt section shadows `deployment:persona` | +| `toolFilter` | Which deployment-global tools enter this child's visible tool view? | A scoped restriction filters globals before child-local tools are added | +| `maxDepth` | How deep may this delegation tree grow? | A start whose child depth exceeds the absolute cap is rejected | + +`dsh-tool-subagent` exposes the controls as plugin configuration and copies them into each request it creates. Direct `SubagentService` callers may choose them per request. The provider capability descriptor remains the source of truth for whether a backend can honor each field. + +### Persona is a scoped shadow + +The persona control changes one child without changing deployment-wide prompt assembly. During unpublished setup, an in-process provider registers a child-scoped section named `deployment:persona`; ordinary most-specific-wins resolution replaces the global section only in that child's assemblies. + +The value has the same strict template semantics as the deployment persona. Omitting it inherits the deployment section through the global layer; an explicit empty string shadows the global persona with an empty section. Parent and sibling personas never enter the child's flat scope. + +This uses the normal system-prompt registration mechanism rather than a second persona channel. The first prompt therefore sees the same named contribution that later prompts and prompt-inspection tools see. + +### Tool filtering is one live global-view rule + +The tool filter controls visibility and executable lookup together. An in-process provider installs `ToolRegistry.restrict()` in the child's scope before publication, and the registry's single resolver applies the same result to prompt schemas, lookup, execution, and Code Mode SDK generation. + +Resolution follows these rules: + +1. Each restriction applies `allow` before `deny` to the live deployment-global tool registry. +2. Multiple restrictions intersect, so every installed restriction must admit a global tool. +3. Child-scoped tools are added after global filtering and may shadow an admitted global tool. +4. Reserved `run_code` presentation and other scope-local protocol contributions are outside the global filter. + +Configuration fails loudly when a filter supplies neither `allow` nor `deny`, or names something outside the current global restrictable set, including a scope-local-only or reserved name. `allow: []` is valid and deliberately hides every global tool. These checks catch misspellings and prevent configuration from appearing effective when it cannot affect the named entry. + +The global registry remains live. A deny-only filter admits a later global name unless it explicitly denies that name; an allow-list excludes a later global name unless it explicitly allows that name. Removing a global tool removes it from every resolved view. These semantics preserve hot registration while making the difference between allow and deny explicit. + +### Depth is an absolute tree cap + +The depth limit bounds recursive delegation independently of tool visibility. A top-level agent has depth zero; an in-process child has its parent's validated depth plus one. `maxDepth` is an absolute non-negative safe integer, and a start rejects before child ownership begins when the derived child depth is greater than the cap. + +Every public entry validates the domain rather than relying on one model-facing configuration path. Negative values, fractions, negative zero, non-finite values, unsafe integers, malformed stored parent depth, and derived overflow all reject. Omitting the cap leaves depth unbounded by this mechanism. + +A deployment can combine depth and filtering. For example, it may keep the delegation tool visible at depth one but set `maxDepth: 1`, or deny the delegation tool entirely in children. Neither choice changes the provider's conversation-history behavior. + +### Capability gating keeps providers honest + +Capabilities separate a requested feature from a provider implementation. `SubagentCapabilities` advertises `persona`, `toolFilter`, and `depthLimit`; `SubagentService.start()` checks every present request field against those flags before calling the provider. + +This lets spawn and fork providers share the in-process implementation while external providers advertise only what they can enforce. A request never degrades silently: selecting an unsupported control produces `UNSUPPORTED_CAPABILITY`, and no run or lifecycle event exists. + +### Unpublished setup makes the first request correct + +All child-local composition is complete before the child becomes observable. The in-process provider supplies one setup callback to agent creation; that callback installs persona, tool restriction, and structured-output contributions in the child's scope. Only after setup succeeds does creation publish the session and agent and allow the driver to start. + +A setup failure rolls back the private child. No observer can acquire a child whose first prompt used the deployment persona or unfiltered tool set and whose later prompts use the requested configuration. + +## Visibility is not authority + +These controls compose trusted same-process behavior; they do not authorize it. `toolFilter` changes the child view resolved by the tool registry, but it does not create a parent-to-child grant lattice, require a child to be a subset of its parent, sandbox plugins, or prevent code with another Cordis context from calling services directly. + +In particular, a child-local tool is added after the global filter and may be absent from the parent's view. A deny-only child also sees later global tools not named by the deny-list. Those are deliberate live-composition semantics, not non-escalation guarantees. + +A security design would need a separate authority representation, propagation rule, and execution-time enforcement point. Creation-time grant snapshots, parent-subset grants, explicit future-grant APIs, and generic capability/output/termination tags are outside this feature. + +## Alternatives considered + +**Create one provider per persona or tool set.** This multiplies providers that share the same transport and lifecycle implementation, makes dynamic deployment configuration awkward, and still needs a recursion mechanism. Providers remain about execution transport; requests carry per-child composition. + +**Copy the parent's complete tool view.** Registration scope is flat by design, and lifetime ownership does not imply visibility inheritance. Copying a resolved view would also freeze dynamic global registrations and conflate composition with authority without defining either contract fully. + +**Snapshot allowed global tools at child creation.** A frozen allow-set makes future registration uniformly unavailable, but it changes hot-registration semantics and starts an authorization design. The implemented filter stays a live registry predicate and documents allow-versus-deny behavior directly. + +**Hide only tool schemas.** Presentation-only filtering lets the model execute a tool that the prompt says does not exist through Code Mode or a forged call. One resolver governs both presentation and execution instead. + +**Use only tool filtering to stop recursion.** Removing the delegation tool is useful but provider-specific and does not protect direct service callers or alternate delegation tools. Absolute depth is an independent structural bound. + +## Consequences + +Contributors can configure child role, visible global tools, and recursion without defining new providers. Capability checks fail before ownership starts, unpublished setup makes the first request consistent, and one tool resolver prevents presentation/execution drift. + +The cost is that deployments must understand live allow/deny behavior and the distinction between visibility and authority. Provider authors must advertise each supported control accurately, and in-process providers must install every requested contribution before publication. The controls deliberately do not solve security confinement or parent-to-child non-escalation. diff --git a/docs/rfc/implemented/process/2026-07-06-node-engine-floor.md b/docs/rfc/implemented/process/2026-07-06-node-engine-floor.md index 72c9f09eda..51328a5a1a 100644 --- a/docs/rfc/implemented/process/2026-07-06-node-engine-floor.md +++ b/docs/rfc/implemented/process/2026-07-06-node-engine-floor.md @@ -8,7 +8,7 @@ The Node 22 branch of the root `engines.node` range is a contract for the instal ## Decision -Set `engines.node` to `^22.19.0 || >=24.0.0` and test the keyless CI compatibility matrix on `['22.19', 24, 26]`. The real-API e2e workflow stays on Node 24 because it exercises API integration rather than the runtime floor. +Set `engines.node` to `^22.19.0 || >=24.0.0` and test the keyless CI compatibility matrix on `['22.19', 24, 26]`. Every matrix leg runs the TypeScript typecheck plus a keyless source-mode worker smoke, so the floor is exercised through both a complete source typecheck and a real unbuilt runtime path. The real-API e2e workflow stays on Node 24 because it exercises API integration rather than the runtime floor. Two Node features gate the source runtime: @@ -22,7 +22,7 @@ Those source features clear on the 22.x line at **22.18**, but the installed Pi ## Consequences - The advertised LTS branch no longer undercuts the Pi adapter dependency floor. -- CI proves the Node 22 LTS floor directly with Node 22.19, keeps the Node 24 branch on `node: 24`, and keeps Node 26 for the next even line. +- CI proves the Node 22 LTS floor directly with Node 22.19, keeps the Node 24 branch on `node: 24`, and keeps Node 26 for the next even line; each leg typechecks the source graph and launches the unbuilt workflow worker for real. - The built-bin smoke needs no version-conditional flag: at 22.19 type-stripping is already the default, so the test stays the plain `node lib/bin.js` path it documents. - A future dependency or source API that raises the runtime floor must move `engines.node`, the compatibility matrix, and this RFC in the same change. diff --git a/docs/rfc/implemented/process/2026-07-06-parallel-github-ci-gates.md b/docs/rfc/implemented/process/2026-07-06-parallel-github-ci-gates.md index d63a8ad713..061d534b9b 100644 --- a/docs/rfc/implemented/process/2026-07-06-parallel-github-ci-gates.md +++ b/docs/rfc/implemented/process/2026-07-06-parallel-github-ci-gates.md @@ -10,9 +10,9 @@ The hard part is the artifact boundary. `publint`, `verify-node-next-types`, and ## Decision -[CI](../../../../.github/workflows/ci.yml) keeps the keyless workflow to a few broad jobs instead of one job per gate. The Node 24 matrix has five lanes: static gates (`pnpm run check:ci:static`), lint (`pnpm run check:ci:lint`), coverage (`pnpm run check:ci:coverage`), snapshot replay (`pnpm run check:ci:snapshot`), and artifact gates (`pnpm run check:ci:artifacts`). The Node 26 compatibility job installs once and runs `pnpm run check:node-compat`. +[CI](../../../../.github/workflows/ci.yml) keeps the keyless workflow to a few broad jobs instead of one job per gate. The Node 24 matrix has five lanes: static gates (`pnpm run check:ci:static`), lint (`pnpm run check:ci:lint`), coverage (`pnpm run check:ci:coverage`), snapshot replay (`pnpm run check:ci:snapshot`), and artifact gates (`pnpm run check:ci:artifacts`). The compatibility matrix has Node 22.19, 24, and 26 jobs; each installs once and runs `pnpm run check:node-compat`. -Each lane delegates to [scripts/run-gates.ts](../../../../scripts/run-gates.ts), an in-process scheduler with bounded concurrency (`DSH_GATE_CONCURRENCY`). The static lane fans out constraints, the echo-agent demo smoke, `doc-sync` leaf gates, module-graph freshness, and `knip`; the lint lane runs ESLint with its own Node heap cap and a content-strategy ESLint cache; the coverage lane runs Vitest coverage with bounded file workers (`DSH_COVERAGE_MAX_WORKERS`); the snapshot lane isolates replay; the artifact lane builds once and then fans out the artifact consumers; the Node 26 compatibility job owns the TypeScript typecheck. The scheduler buffers each gate's output and prints a named result block with duration, so independent failures stay attributable inside each broad job log. +Each lane delegates to [scripts/run-gates.ts](../../../../scripts/run-gates.ts), an in-process scheduler with bounded concurrency (`DSH_GATE_CONCURRENCY`). The static lane fans out constraints, the echo-agent demo smoke, `doc-sync` leaf gates, module-graph freshness, and `knip`; the lint lane runs ESLint with its own Node heap cap and a content-strategy ESLint cache; the coverage lane runs Vitest coverage with bounded file workers (`DSH_COVERAGE_MAX_WORKERS`); the snapshot lane isolates replay; the artifact lane builds once and then fans out the artifact consumers. Every compatibility job runs the TypeScript typecheck and a keyless workflow-workerthread source-launch smoke, which starts a real unbuilt worker and therefore catches Node-version-specific loader/runtime failures that typechecking cannot. The scheduler buffers each gate's output and prints a named result block with duration, so independent failures stay attributable inside each broad job log. Generated `.sessions/` logs and `.doc-typecheck-*` temp directories are ignored by lint. The aggregate local CI mode still runs demo smoke after lint, while the split GitHub static lane can run demo smoke directly because lint is isolated in its own lane. @@ -36,4 +36,4 @@ The broad-lane split repeats checkout, setup, and install more often than a sing The split introduces a maintenance obligation: when `package.json` adds or removes a gate that belongs in CI, [scripts/run-gates.ts](../../../../scripts/run-gates.ts) needs the matching leaf. That obligation is intentional because the runner is the parallel execution plan for the same gate vocabulary, not a separate quality policy. -The Node 26 signal is narrower than the primary Node 24 signal. It proves the source graph on the newer runtime without doubling documentation, coverage, publication, snapshot, and smoke checks whose failures are not expected to vary by Node minor version. +The compatibility signal is narrower than the primary Node 24 signal. It proves that the source graph typechecks and that the real unbuilt workflow-worker launch path executes on every advertised runtime line without doubling documentation, coverage, publication, snapshot replay, and unrelated smoke checks whose failures are not expected to vary by Node version. diff --git a/docs/rfc/implemented/simplification/2026-06-20-remove-agent-boundary-mirror-events.md b/docs/rfc/implemented/simplification/2026-06-20-remove-agent-boundary-mirror-events.md index e4b72ab5d1..dbef22f3a3 100644 --- a/docs/rfc/implemented/simplification/2026-06-20-remove-agent-boundary-mirror-events.md +++ b/docs/rfc/implemented/simplification/2026-06-20-remove-agent-boundary-mirror-events.md @@ -13,7 +13,7 @@ Status: implemented ## Problem -The loop records the canonical transcript in `SessionEvent` and also emitted a parallel set of live `agent/*` boundary mirror events: `agent/turn-start`, `agent/turn-end`, `agent/step-start`, and `agent/step-end`. The mirrors made consumers choose between two sources of truth for the SAME durable fact. ACP already chose the session log for the editor-facing transcript because a throwing peer listener can prevent later `agent/*` listeners from observing a boundary, while the session event was already appended. The stdio UI was the only production consumer that still rendered turn boundaries from the mirror events; it already rendered tool calls and results from `session/event`. +The loop records the canonical transcript in `SessionEvent` and also emitted a parallel set of live `agent/*` boundary mirror events: `agent/turn-start`, `agent/turn-end`, `agent/step-start`, and `agent/step-end`. The mirrors made consumers choose between two sources of truth for the SAME durable fact. ACP already chose the session log for the editor-facing transcript because it is the one durable, replayable record; consuming a live mirror would require reconciling its timing with the boundary already stored in that log. The stdio UI was the only production consumer that still rendered turn boundaries from the mirror events; it already rendered tool calls and results from `session/event`. This duplication is not free. Every lifecycle change had to update the session event, the mirror event, docs, invariants, tests, and snapshot expectations. The duplicate boundary events also made failure ordering subtle: a turn can be durably closed before a live `agent/turn-end` listener runs, so a post-boundary listener failure has no valid in-log position left and must be reported out of band. diff --git a/docs/rfc/implemented/testing/2026-06-19-acp-snapshot-tests.md b/docs/rfc/implemented/testing/2026-06-19-acp-snapshot-tests.md index ae5ea96f83..b424a253bd 100644 --- a/docs/rfc/implemented/testing/2026-06-19-acp-snapshot-tests.md +++ b/docs/rfc/implemented/testing/2026-06-19-acp-snapshot-tests.md @@ -51,7 +51,7 @@ The ACP server app loads `@deepseek-ai/dsh-llm-deepseek`, whose `apply` throws w A snapshot run asserts **two** normalized surfaces, because the harness's external surfaces are distinct: 1. The **stdout transcript** — the framed `session/update` JSON-RPC the editor sees. Catches regressions in the ACP bridge's event→update translation (`streamSessionEventUpdate`). Compared against a committed `stdout.golden.jsonl`. -2. The **re-persisted session JSONL** — the log the replay run itself persists, compared against the scenario's `session.jsonl`. Catches regressions in the loop, tool dispatch, and turn/step structure that never surface on stdout. There is no separate session golden: `session.jsonl` is BOTH the replay source (recorded scenarios) and the expected produced log. Both sides pass through `normalizeSessionLog` before comparing — the fixture is raw-harvested (its own real session id / cwd / timestamps) and the replay output has fresh ones, so each is scrubbed against ITS OWN volatile values (the fixture's read from its header line) and the comparison is on normalized form. Request-header CONTENT (the composed system prompt + tool schemas) is additionally scrubbed to `{{system}}`/`{{tools}}` tokens on both sides — in the stored fixtures too — for every scenario except the one that pins it ([pinned-header RFC](2026-07-06-pin-request-header-content-in-one-scenario.md)). For an authored override scenario the same `session.jsonl` holds the expected produced log; `replay.override.json` drives the model, and `llm-replay` ignores the fixture for model chunks when an override exists, so committing the expected log there does not affect replay. +2. The **re-persisted session JSONL** — the log the replay run itself persists, compared against the scenario's `session.jsonl`. Catches regressions in the loop, tool dispatch, and turn/step structure that never surface on stdout. There is no separate session golden: `session.jsonl` is both the replay source (recorded scenarios) and the expected produced log. Both sides pass through `normalizeSessionLog` before comparing — the fixture is raw-harvested (its own real session id / cwd / timestamps) and the replay output has fresh ones, so each is scrubbed against its own volatile values (the fixture's read from its header line) and the comparison is on normalized form. Every stored JSONL additionally scrubs composed prompt text to `{{system}}`; each header class's pinning scenario stores that prompt readably in `system-prompt.golden.md` and keeps the complete tool schemas in its JSONL, while other scenarios scrub schemas to `{{tools}}` ([pinned-header RFC](2026-07-06-pin-request-header-content-in-one-scenario.md)). For an authored override scenario the same `session.jsonl` holds the expected produced log; `replay.override.json` drives the model, and `llm-replay` ignores the fixture for model chunks when an override exists, so committing the expected log there does not affect replay. The two are genuinely additive: stdout is the bridge's *lossy projection* of the log (it drops `assistant/message.usage`, `step/*`, exact `seq`/`time`, and renders tool I/O differently), so a loop/tool/turn-structure regression can change the JSONL while leaving the stdout projection identical, and a bridge-translation regression can change stdout while the JSONL is untouched. Asserting the JSONL equality also echoes the proposed [universal replay fixture](../../proposed/testing/2026-06-11-deterministic-and-stress-testing.md) idea. diff --git a/docs/rfc/implemented/testing/2026-07-06-pin-request-header-content-in-one-scenario.md b/docs/rfc/implemented/testing/2026-07-06-pin-request-header-content-in-one-scenario.md index 862dfd42fa..00a60ad4bc 100644 --- a/docs/rfc/implemented/testing/2026-07-06-pin-request-header-content-in-one-scenario.md +++ b/docs/rfc/implemented/testing/2026-07-06-pin-request-header-content-in-one-scenario.md @@ -4,29 +4,30 @@ Status: implemented ## Problem -Every model-driving ACP snapshot fixture (`session.jsonl`) embedded the full composed system prompt and the complete tool-schema list in its `request/header` event — roughly 8 KB on one line, per fixture. That content is identical across the suite (byte-identical tool list everywhere, including subagent children; identical prompt modulo each recording's temp cwd), so any change touching a tool description or a system-prompt line had to update every fixture: re-record everything against the live API (churning model responses and stdout goldens along the way) or hand-edit ~35 giant header lines. Introducing the dynamic-workflows feature — one new tool plus one prompt paragraph — rewrote every snapshot fixture in the repo, burying the behavioral diff a reviewer should be reading. +An ACP snapshot suite needs to prove the exact composed system prompt and tool-schema list sent in each `request/header`, but duplicating that content inside every `session.jsonl` makes a prompt or schema edit rewrite dozens of giant one-line JSON records. Keeping one raw header avoids the duplication but still makes prompt review poor: prose is JSON-escaped onto one line and mixed with thousands of characters of tool schemas. ## Decision -Exactly one scenario — `text-turn`, flagged `pinsHeader` in the `acp.snapshot.ts` scenario table — commits and compares the full request-header content; the pin mechanics live in [`dsh-acp-snapshot`](../../../../packages/support/acp-snapshot/README.md), whose suite factory enforces one pin per consuming suite. Every other fixture stores and compares that content as stable tokens via the pure normalizer `scrubRequestHeaders` in that package's `normalize.ts`: a `request/header` event's `header.system` becomes `"{{system}}"` and `header.tools` becomes `"{{tools}}"`; a `request/header-delta` keeps its structural facts — the system delta's `keepStart`/`keepEnd` line positions with one `{{system}}` token per inserted line, the tools delta's added/removed/changed tool names — and tokenizes only the bulk (prompt text, schema bodies), so two different deltas still compare different. The scrub is composed in front of `normalizeSessionLog` on BOTH sides of a non-pinning scenario's log compare and applied to the harvested logs record mode writes, so a re-record cannot smuggle the content back. Absent fields stay absent — WHETHER a header carried a prompt or tools is behavior and stays visible — and `config`/`reason` stay verbatim: a model swap churns every fixture by design (it invalidates the recorded responses), while a prompt or schema edit churns none of them (replay derives model behavior exclusively from `assistant/chunk` events and never reads header content — see `dsh-llm-replay`). +Exactly one scenario per header-composition class is flagged `pinsHeader`. Its directory splits the pin by review format: `system-prompt.golden.md` contains the normalized composed prompt as ordinary Markdown, while `session.jsonl` keeps the full tool-schema list, config, and reason but stores `header.system` as `"{{system}}"`. Every other JSONL stores both the system prompt and tool list as `"{{system}}"` / `"{{tools}}"`. The pin mechanics live in [`dsh-acp-snapshot`](../../../../packages/support/acp-snapshot/README.md), whose suite factory enforces one pin per class. -A system-prompt or tool-schema change therefore lands as exactly one committed-fixture diff — the pinned `text-turn` header line — updated by hand or by re-recording that one scenario (`pnpm run test:snapshot:record` with `-t text-turn`). +The pure `scrubSystemPrompts` normalizer applies to every stored session fixture and tokenizes both an initial header's prompt and a header delta's inserted prompt lines. `scrubRequestHeaders` additionally tokenizes tool schemas and session-prefix content for non-pinning scenarios while retaining structural facts: system-delta positions and arity, added/removed/changed tool names, prefix message count, field presence, config, and reason. Record and refresh write-back apply the appropriate scrub before writing JSONL and regenerate the Markdown prompt from the normalized live header, so neither path can reintroduce prompt text into JSONL or leave the readable snapshot stale. -Guards make the split self-enforcing. On disk (fixture meta-tests): every non-pinning `session*.jsonl` must be a fixed point of `scrubRequestHeaders` (unscrubbed content crept in — apply the scrub), the pinning scenario's fixture must NOT be one (the pin lost its content), and exactly one scenario must pin. Live (every non-pinning scenario run): each `request/header` the run produces — parent, spawn child, fork child, initial or resume — must equal the pinned fixture's header after both sides normalize their own volatile values, and no `request/header-delta` may appear at all (a mid-run header change diverges from the pin by construction, and its content would be invisible under the scrub), so the single-pin premise is asserted rather than assumed. +Guards make the split self-enforcing. On disk, every `session*.jsonl` is a fixed point of `scrubSystemPrompts`, only non-pinning fixtures are fixed points of the full header scrub, `system-prompt.golden.md` exists exactly beside pinning fixtures, and each class has one pin. Live, every `request/header` produced by a parent, spawn child, fork child, initial request, or resume must match both halves of its class's pin after volatile-value normalization. A header without a string prompt or any `request/header-delta` fails loud because the two static pin artifacts cannot represent it. One pin covers the whole suite because every session — parent, spawn child, fork child — composes the identical tool list and the identical prompt modulo cwd, and the uniformity guard fails the suite the moment that stops holding. If header composition ever becomes session-dependent by design (a restricted subagent toolset, say), the divergent shape gets its own pinning scenario. ## Alternatives considered -- **Re-record or hand-edit every fixture per change** — the status quo; the churn this RFC removes. -- **Scrub at compare time only, keeping fixtures raw** — the compares go green without fixture edits, but every committed fixture then carries a permanently stale copy of the prompt and schemas: dead weight that misleads readers and still rewrites wholesale on the next re-record. Storing the tokens keeps the fixture honest about what it does and does not pin. +- **Re-record or hand-edit every fixture per change** — preserves exact headers but buries behavioral diffs under duplicated prompt and schema content. +- **Scrub at compare time only, keeping fixtures raw** — lets compares pass while committed fixtures retain stale duplicate content and rewrite wholesale on the next recording. Stored tokens state honestly what each JSONL does not pin. - **Scrub everywhere, pin nowhere** — loses the only end-to-end record of the composed header as actually sent (prompt assembly, registered-tool order, full schemas). The generated tool catalog documents each tool in isolation; only a real fixture pins the composed set. +- **Keep the one full pin entirely in JSONL** — removes suite-wide duplication but leaves system-prompt changes as an escaped one-line diff entangled with the tool list. Markdown gives prompt prose its natural review format without weakening the header assertion. - **Slim the session log itself (log a content digest, store the header elsewhere)** — violates the reconstructability contract: the product log must reproduce each request bit-for-bit ([reconstructable-requests RFC](../architecture/2026-07-05-reconstructable-requests.md)). Header bulk is a test-artifact concern, solved in test normalization; the live log is untouched. ## Verification -All 37 snapshot scenarios replay green with the scrubbed fixtures (the committed fixtures were rewritten once through `scrubRequestHeaders` itself; `text-turn` untouched). The fixed-point, pin-retains-content, exactly-one-pin, live header-uniformity, and no-unpinned-delta guards run inside the suite, and `scrubRequestHeaders` has unit coverage for both header event types, delta structure preservation (line positions, insert arity, tool names), absent-field preservation, config/reason retention, byte-for-byte pass-through of other lines, and idempotence. +The suite replays every scenario against the split pins. Unit coverage exercises both scrub levels, Markdown formatting, record/refresh regeneration, normalized prompt extraction, fixed-point enforcement, required-file symmetry, header uniformity, and delta rejection. ## Consequences -A tool-description or system-prompt change churns one committed fixture line instead of every fixture in the suite, so snapshot diffs read as behavior again, and ~270 KB of duplicated header bytes leave the repo. The cost: non-pinning fixtures no longer display header content, so reading one shows tokens where the prompt and schemas were — the pinned `text-turn` fixture is the place to look, and the live uniformity guard guarantees it speaks for every session in the suite. A header change surfaces as a suite-wide test failure whose fix is the one pinned line, rather than as ~35 fixture rewrites. +A system-prompt change produces a normal line-oriented Markdown diff in one file per affected composition class; a tool-description change produces one pinned JSONL line per class; ordinary behavioral fixtures remain untouched. Session fixtures display tokens for omitted content, and the live uniformity guard makes each split pin authoritative for every session in its class. The pinning scenario carries one extra generated artifact whose terminal newline is canonicalized for repository hygiene. diff --git a/docs/rfc/implemented/testing/2026-07-08-shared-acp-snapshot-package.md b/docs/rfc/implemented/testing/2026-07-08-shared-acp-snapshot-package.md index 910f179313..2f631a5d8a 100644 --- a/docs/rfc/implemented/testing/2026-07-08-shared-acp-snapshot-package.md +++ b/docs/rfc/implemented/testing/2026-07-08-shared-acp-snapshot-package.md @@ -16,7 +16,7 @@ The machinery lives in [`packages/support/acp-snapshot`](../../../../packages/su **`src/normalize.ts`** — the pure normalizers, hook-free by policy: when a future event carries a new volatile field (an approval duration, say), the shared normalizer learns it in the same change, keeping one home for what "normalized" means rather than per-suite scrub extensions. -**`src/suite.ts`** — the `Scenario` type and `defineAcpSnapshotSuite(options)`, registering the per-scenario compares, record-mode fixture write-back, the header pin with its live uniformity guard, and the fixture guard block (no orphan scenario dirs, required files present, exactly one pin, non-pinning fixtures are `scrubRequestHeaders` fixed points). The pinned-header contract ([pinned-header RFC](2026-07-06-pin-request-header-content-in-one-scenario.md)) is per-suite: each suite flags exactly one `pinsHeader` scenario (the factory throws on zero, a meta-test rejects more than one; WHICH scenario pins is the table's reviewable choice), and the uniformity guard compares only that suite's sessions. The pure helpers (`childFixturePaths`, `fixtureContext`, `normalizedHeaders`, `headerDeltaCount`) are exported for direct unit coverage. +**`src/suite.ts`** — the `Scenario` type and `defineAcpSnapshotSuite(options)`, registering the per-scenario compares, record/refresh fixture write-back, the header pin with its live uniformity guard, and the fixture guard block (no orphan scenario dirs, required files present, exactly one pin per class, every JSONL a `scrubSystemPrompts` fixed point, non-pinning fixtures also `scrubRequestHeaders` fixed points). The pinned-header contract ([pinned-header RFC](2026-07-06-pin-request-header-content-in-one-scenario.md)) is per-suite: each header class flags exactly one `pinsHeader` scenario, whose `system-prompt.golden.md` and JSONL tool list split the composed header into reviewable artifacts; the uniformity guard compares both against every live header in that class. The pure helpers (`childFixturePaths`, `fixtureContext`, `normalizedHeaders`, `normalizedSystemPrompts`, `formatSystemPromptSnapshot`, `headerDeltaCount`) are exported from the module for direct unit coverage. ## Alternatives considered diff --git a/docs/rfc/proposed/feature/2026-06-14-acp-agent-client-protocol.md b/docs/rfc/proposed/feature/2026-06-14-acp-agent-client-protocol.md deleted file mode 100644 index c321c56d53..0000000000 --- a/docs/rfc/proposed/feature/2026-06-14-acp-agent-client-protocol.md +++ /dev/null @@ -1,84 +0,0 @@ -# RFC: Agent Client Protocol (ACP) support — drive the coding agent from external editors - -Status: proposed - -> **Implementation status (MVP landed):** steps 1, 2, 3, 4, 6, 7, 8 are implemented in `packages/ui/acp` + `examples/acp-agent`. **Step 5 (the `session/request_permission` permission gate) is deferred** — the bridge ships a pass-through (tools run with the executor's full authority) marked `TODO(rfc010-permission-gate)`, and lays down only the `WeakMap` ownership seam the gate will build on. Status stays `proposed` until the gate lands. `session/cancel` is the queue-aware `agent.cancel()`: it aborts a running step, clears queued + steering work, and drops a turn that is about to start, so a queued-but-not-yet-started prompt never runs and a later prompt cannot be batched into the cancelled turn. **Per-session `cwd` is now honored** (lifting the original "launch the server in the workspace root" restriction — see § Deferred): `session/new` accepts any absolute `cwd`, and `session/load` requires the request `cwd` to match the persisted session `cwd` so the editor and bash executor agree on the workspace. - -## Problem - -The coding agent is reachable only through the readline `stdio-chat` plugin: it reads lines from stdin, calls `agent.send()`, and prints the assistant token stream (`session/event` `assistant/chunk`) to stdout. There is no structured protocol, so the agent cannot be embedded in an editor — no streaming render, no tool-call display, no permission UI, no resumable sessions. - -Editors are converging on the Agent Client Protocol (ACP), which Zed and others speak: JSON-RPC 2.0 over newline-delimited stdio, modeled on the Language Server Protocol. An editor boots the agent as a subprocess and exchanges `initialize` / `session/new` / `session/prompt`, rendering streamed `session/update` notifications and `session/request_permission` prompts. The goal is for the agent to be a drop-in ACP server — implement the protocol once and run in any ACP client, with no per-editor glue. - -This RFC has a hard prerequisite on [session persistence](../../implemented/architecture/2026-06-14-session-persistence.md): it assumes durable session persistence (the `SessionPersistence` service and the async `AgentLoop.resume` seam) is implemented, so resuming a session via `session/load` is in scope. None of those APIs exist yet — `AgentLoop` currently exposes only the synchronous `create` — so ACP must land after, or in the same change as, [session persistence](../../implemented/architecture/2026-06-14-session-persistence.md), and pins to its `resume(agentId, resumeSessionId)` contract. Session persistence persists every `SessionEvent` verbatim (including `assistant/chunk`), so a loaded session has the stream chunks needed to replay turns to the client. - -## Proposal - -A new plugin package `@deepseek-ai/dsh-acp` — a client-driver / UI plugin, the structured analogue of `stdio-chat`. It is NOT a change to the loop and NOT an [capability seams](../../implemented/architecture/2026-06-13-capability-seams.md) interface/implementation/consumer capability split; it consumes the existing `agent/*` event taxonomy and the `tools/pre-execute`/`tools/post-execute` waterfalls. - -It depends on the official `@agentclientprotocol/sdk` (the `AgentSideConnection` class) — Apache-2.0, actively versioned. The SDK declares a `zod` peer dependency and imports `zod/v4` at runtime, so `packages/ui/acp` must declare `zod` itself (per the workspace dependency constraints). This is the renamed successor to `@zed-industries/agent-client-protocol`, which is now deprecated on npm. - -The mapping between ACP and existing harness seams — each row names the seam and any required extension: - -| ACP (client ⇄ agent) | Harness seam | Notes | -|---|---|---| -| `initialize` | static handler | negotiate `protocolVersion` (echo the supported version, else error); advertise text-only `promptCapabilities` and `loadSession: true`; report agent name/version | -| `session/new {cwd, mcpServers, additionalDirectories}` → `{sessionId}` | the `dsh-agent` create factory (see Dependency note + Plan) | the seam must accept `{ sessionId, meta }` so the ACP-generated `sessionId` becomes the live/persisted session id and the validated `cwd` is attached as the `SessionHeader` (today `AgentLoop.create(id)` hardcodes `${id}-session` and takes no metadata); reject a 2nd session (single-session MVP, see [ACP multi-session](2026-06-14-acp-multi-session.md)); `cwd` validated (require absolute) — any absolute cwd is honored: it becomes the session's `SessionHeader.cwd` and the default bash workdir (per-session cwd, see § Deferred → RESOLVED), so the server need not launch in the workspace; non-empty `mcpServers` and `additionalDirectories` are rejected for the MVP because silently ignoring requested servers/roots would desync the client's tool and filesystem-scope UI | -| `session/load {sessionId, cwd, mcpServers, additionalDirectories}` | the `dsh-agent` resume factory ([session persistence](../../implemented/architecture/2026-06-14-session-persistence.md) + Dependency note) | load `{ meta, events }`, seed the session, re-derive history via `deriveMessages()`, replay prior turns to the client as `session/update` per the ACP load contract; `mcpServers` and `additionalDirectories` rejected as in `session/new` | -| `session/prompt {prompt}` | `agent.send()` (idle) | text blocks → `TextBlock`; reject image/audio per advertised capabilities; one in-flight prompt per session | -| resolve `session/prompt` → `{stopReason}` | the `turn/end` `session/event` (its `reason`) | map the harness kebab `TurnEndReason` to the ACP snake_case `StopReason` wire enum: `completed`→`end_turn`, `max-tokens`→`max_tokens`, `aborted`(cancel)→`cancelled`, plus `refusal`/`max_turn_requests` when applicable; honor the batch-into-one-turn and send-not-synchronously-running settle semantics | -| `session/update: agent_message_chunk` | `session/event` `assistant/chunk` `text-delta` only | do NOT also emit on `block-end(TextBlock)` — it carries the fully-assembled block and would duplicate the streamed text | -| `session/update: agent_thought_chunk` | `session/event` `assistant/chunk` `reasoning-delta` | | -| `session/update: tool_call` (pending→in_progress) | `session/event` `tool/call` | demux via a Session→sessionId map; `kind` inferred from the tool name | -| `session/update: tool_call_update` (completed/failed) | `session/event` `tool/result` | a throwing `tools/execute` yields NO `tool/result` → fail the pending tool UI from `agent/error`/turn-end | -| `session/request_permission {sessionId, toolCall, options}` | prepended `tools/execute` listener | no-op unless `exec.agent` is ACP-owned; await the outcome; `selected/allow_*` → `next()`; `reject_*`/`cancelled` → veto `ToolExecutionResult{isError}` | -| `session/cancel` (notification) | `agent.cancel(reason)` | the queue-aware cancel (abort running step, clear queued + steering, drop an about-to-start turn); settle the in-flight prompt as `cancelled`; resolve any pending permission as `cancelled` exactly once | - -The permission gate is the first real consumer of the `tools/execute` veto seam (the documented "single veto/sandbox/permission seam" plus the deferred "Permission system" TODO in [docs/architecture.md](../../../architecture.md)). It is a single global listener registered with `prepend: true` so it runs before any other tool wrapper. `ToolExecution.agent` is optional and the `Agent` interface carries no origin marker, so the bridge tracks ownership itself: it records each agent it creates in a `WeakMap` and the gate no-ops (calls `next()` immediately) for any `exec.agent` it does not own — non-ACP agents and the no-agent case pass straight through. For an owned agent it resolves the session, issues `session/request_permission`, and stores the pending resolver on that session's record so the outcome — or a `session/cancel`/connection-close — settles it exactly once. - -Lifecycle and disposal: the connection, listeners, and in-flight permission promises register via `ctx.effect`/`ctx.on`; teardown is async and must *reach* quiescence, not just request it — close the connection, settle/reject pending permissions, and dispose each owned agent through its `AgentHandle.dispose()` (which stops the loop, `await`s its exit, and unregisters). Owner teardown goes through that handle seam, not the loop's concrete `agent.done` (which exists only on `ReactLoopAgent`); a non-owner that merely wants to *observe* the current work settling without tearing the agent down awaits the interface-level `agent.whenIdle()`. Every listener contains its `send()` exceptions (log, never reject the turn) because stream chunks are emitted inside the model step, so a throwing listener would corrupt the turn. - -**Dependency note (architecture rule).** [docs/architecture.md](../../../architecture.md) states "plugins depend on interface packages, never on `dsh-agent-loop`." Creating and resuming agents is currently only on the concrete `AgentLoop` (`ctx.agentLoop`), so this RFC proposes adding an **abstract create/resume factory** to the `dsh-agent` interface (registry-level `create({ sessionId, meta })` / `resume(...)`), implemented by the loop, so `dsh-acp` injects only `agents` (the interface) and the dependency rule holds. The alternative — injecting the concrete `agentLoop` and recording a documented exception in the architecture doc — is explicitly the non-preferred fallback. - -## Plan - -1. Package scaffold `packages/ui/acp/` per [the cookbook](../../../cookbook/adding-a-package.md); add `@agentclientprotocol/sdk` and `zod`. Add the abstract create/resume factory to `dsh-agent` (the interface) so the bridge can `inject: ['agents', 'sessions', 'tools', 'sessionPersistence']` without depending on the concrete loop; `sessionPersistence` is required because `session/load` advertises `loadSession: true`. (Fallback only if the factory is judged not worth it: inject `agentLoop` directly and record the architecture-rule exception in `docs/architecture.md`.) -2. Connection plus `initialize`/`session/new`: wire `AgentSideConnection` to stdin/stdout; protocolVersion negotiation; the single-session guard; create the live session through the new `{ sessionId, meta }` factory seam (so the ACP `sessionId` and validated `cwd` become the session's id and header); the `sessionId↔agent` and `Session↔sessionId` maps. -3. Internal edit — turn-end reason fidelity (sanctioned: edit internals to fit ACP). Extend `TurnEndReasonMap` in the proper places: (a) declaration-merge a `max-tokens` variant in the owning package (`packages/core/session/src/types.ts`, alongside `completed|aborted|error|disposed`) — add `max-tokens` because `FinishReasonMap` produces it (DeepSeek maps `length` → `max-tokens`); do not add `refusal`, since no current adapter produces it (unknown DeepSeek finish reasons collapse to `error`), but leave a comment in `TurnEndReasonMap` noting `refusal` should be added when an adapter first emits it (`FinishReasonMap` is merge-extensible); (b) make `agent-loop`'s `loop.ts` populate the reason from the model `finish` chunk — `assembler.finish` lives inside `runStep`, so `runStep` must return it up to `runTurn`, and the rule is "the last step's finish reason wins, but any `max-tokens` in the turn surfaces as `max-tokens`"; (c) no consumer exhaustively switches over `TurnEndReason` today (the invariants plugin switches on `SessionEventType`, and `deriveMessages` ignores `turn/end`), so adding `max-tokens` is a non-breaking extension — but recheck before landing; (d) update [docs/architecture.md](../../../architecture.md) (the CI-verified loop-lifecycle/event-taxonomy doc) and the affected package READMEs/JSDoc (`dsh-session`, `dsh-agent`, `dsh-agent-loop`) per the repo doc-sync policy. This replaces a fragile "observe the finish chunk in the bridge" hack with a real, documented contract. -4. Prompt-turn streaming plus load: translate `session/event` (the `assistant/chunk` token stream plus boundaries and tool activity) into `session/update`; resolve `session/prompt` on settle, mapping the harness `TurnEndReason` to the ACP `StopReason` wire enum (`completed`→`end_turn`, `max-tokens`→`max_tokens`, `aborted`→`cancelled`) — a small total function with a test asserting the exact wire strings, since the SDK rejects an unknown `stopReason`. Concrete correlation, since the loop batches queued messages into one turn and `send()` does not synchronously flip to running: install the `session/event` listener before `send()`; capture the prompt's owning turn from its `turn/start` record, then resolve on that turn's `turn/end` (with `agent/status` idle/disposed as a fallback); reject an empty/whitespace prompt up front rather than calling `send()` (no turn would ever start, so the RPC would hang). Implement `session/load` on the session-persistence resume seam. -5. Permission gate: a single `tools/execute` listener registered with `prepend: true`, owning a `WeakMap` of bridge-created agents; no-op (`next()`) for unowned/no-agent calls; for owned calls → `session/request_permission` → allow (`next()`) / veto; settle the stored resolver exactly once on outcome, cancel, or connection close. -6. Example wiring (extract a shared base). `@cordisjs/plugin-include` is itself a plugin entry that resets `ctx.baseUrl` and loads a path, so a child `cordis.yml` can nest-include a shared base; the extraction is safe because every dependent plugin declares `inject` (loader groups initialize via `Promise.all`, so YAML order is NOT the dependency mechanism — never rely on it). Extract the provider/tool core (`llm, sessions, system-prompt, tools, agents, invariants, llm-deepseek, bash-local, tool-bash`) into `examples/base.yml`; have both `coding-agent` and a new `examples/acp-agent/` include it and add their own UI plugin plus logger. Keep `agent-loop` per-example (NOT in the base): `AgentLoop` creates its configured agents in its constructor, and the two examples disagree — `coding-agent` needs a pre-created `main` (its `stdio-chat` calls `ctx.agents.get('main')`), while `acp-agent` must pre-create none (ACP `session/new` creates agents). So `coding-agent` declares `agent-loop` with `agents: [{ id: main, … }]` and `acp-agent` with `agents: []`. `acp-agent` loads `dsh-session-persistence-jsonl` (from [session persistence](../../implemented/architecture/2026-06-14-session-persistence.md) — required for `session/load`), omits the stdout logger (see Risks), and adds `pnpm run demo:acp` plus the Zed `agent_servers` snippet. -7. Tests (the repo cares a lot here): a property-based test for the protocol shape (precedent: [property-based testing](../../implemented/testing/2026-06-11-property-based-testing.md)) — fuzz arbitrary harness event sequences and assert ACP-stream invariants (never a `tool_call_update` before its `tool_call`; exactly one `session/prompt` resolution per prompt; monotonic, well-formed ordering; `stopReason` in the legal set); codec unit tests over an in-memory `Duplex` pair (drive `AgentSideConnection` without a subprocess; assert exact frames for `initialize`, `session/new`, a full prompt turn); the mandatory HMR-safety test (dispose the fiber; assert the connection closed, all `ctx.on` listeners gone, any in-flight `request_permission` settled); failure-path tests (connection closes mid-stream; closes with a permission pending; a notification `send()` rejects but the turn survives; `finish{kind:'error'|'aborted'}`; a `tools/execute` throw with no `tool/result`; a second `session/new` rejected; a `session/prompt` while one is in flight; an empty prompt rejected without hanging; a `session/load` re-derives identical history and replays it); and an e2e (`*.e2e.ts`, self-skips without `DEEPSEEK_API_KEY`) that boots `examples/acp-agent`, connects a `ClientSideConnection`, sends a real prompt, owns and disposes the harness in `afterEach`, and verifies the world (files on disk), not the agent's self-report. -8. Docs: module/JSDoc plus a package README; extend [the extension cookbook](../../../cookbook/extension-cookbook.md) with the client-driver pattern. Flip Status to `implemented` on landing; record a decision in this RFC only if it proves durable, contested, and surprising (candidates: the `tools/execute` permission-ownership rule, the npm-dependency choice) — not auto-required. - -Deferred (each names its owning future work): - -- Multiplexing concurrent sessions → [ACP multi-session](2026-06-14-acp-multi-session.md). -- ~~`cwd` honoring.~~ **RESOLVED.** Originally there was no path from `session/new.cwd` to the bash workdir (`tool-bash` forwarded only an explicit `args.workdir`; `LocalBashExecutor.resolve` defaulted to its own config or `process.cwd()`), so the MVP validated `cwd` (require absolute) AND required the server to launch in the workspace root, erroring on a mismatch. This is now lifted: the validated `cwd` is stored as `SessionHeader.cwd`, and `dsh-tool-bash` defaults the bash workdir to the calling agent's `session.header.cwd` (an explicit model `workdir` still wins; a relative one resolves against it). Any absolute `cwd` is honored — the server need not launch in the workspace, and N sessions can each target a different directory. Widening scope beyond the single cwd (`additionalDirectories`) remains deferred. -- Client `terminal/*` proxying (a live editor terminal) and `fs/*` (editor-rendered diffs) — a future `BashExecutor` over the [capability seams](../../implemented/architecture/2026-06-13-capability-seams.md) bash seam, gated on `clientCapabilities.terminal`. -- Image/audio prompts (blocked on the DeepSeek adapter, which skips `image` blocks today), modes, auth, `available_commands`/slash-commands, `plan`, and `usage_update`. - -## Alternatives considered - -- **A process-wide stdout hijack inside `dsh-acp`** (defensively monkey-patching `console.log` / `process.stdout.write`) — rejected: it lives outside Cordis' effect-scoped, HMR-friendly plugin model, races the connection's own stdout handoff, and fights the logger. The stdout guarantee is config-only. -- **Injecting `agentLoop` directly instead of the abstract create/resume factory** — the recorded fallback, taken only if the factory seam is judged not worth it, with the architecture-rule exception recorded in `docs/architecture.md`. - -## Acceptance criteria - -- The `acp-agent` example speaks ACP over stdio end-to-end: `initialize`, `session/new` with a validated absolute `cwd` honored as the session workspace, streamed `session/update` frames per prompt turn, `session/load` re-deriving identical history, and `session/prompt` resolving with the correct wire `stopReason`. -- stdout carries only framed JSON-RPC (asserted by test); the permission gate settles every `session/request_permission` exactly once — on outcome, cancel, or connection close. -- The plan's test set runs green: the property-based protocol invariants, the codec unit tests over an in-memory duplex pair, the HMR-safety test, the failure-path matrix, and the self-skipping real-API e2e that verifies the world. - -## Risks - -stdout is the protocol — guaranteed by config, not by monkey-patching. The console logger writes through `console.log` to stdout, so any stdout UI/logger plugin corrupts JSON-RPC. The guarantee is config-only: the `acp-agent` example loads no stdout plugin (no console logger, no `stdio-chat`) and, if logging is wanted, uses a stderr exporter. A defensive process-wide `process.stdout.write`/`console.log` hijack inside `dsh-acp` is explicitly rejected — it lives outside Cordis' effect-scoped, HMR-friendly plugin model, races the connection's own stdout handoff, and fights the logger. A test asserts the example emits only framed JSON-RPC on stdout. - -New third-party runtime dependency plus protocol drift: `@agentclientprotocol/sdk` is young (0.25.x, recently renamed) and evolving. Pin the version and isolate churn to the one bridge package. This is not a vendoring-policy violation — [vendoring Cordis as source](../../implemented/process/2026-06-11-vendor-cordis-as-source.md) vendors the framework; genuine third-party deps already live on npm (`@earendil-works/pi-ai`). - -Turn-settle and prompt-correlation hazards: honor "queued messages batch into one turn" and "`send()` does not synchronously flip to running" (see `stdio-chat.ts` and the defensive-patterns section of [docs/architecture.md](../../../architecture.md)); gate resolution on an observed running→idle transition and handle the empty-prompt / no-work branch so an RPC can't hang. - -Permission-await and disposal hangs: a pending `request_permission` whose connection closes or whose turn aborts must settle exactly once; disposal must reach quiescence — tear each owned agent down through `AgentHandle.dispose()` (which stops the loop and awaits its exit), rather than orphaning awaits on a closed pipe. - -The 100% per-file coverage gate (repo policy) makes a branch-heavy protocol bridge real work. Accepted deliberately, surfaced so it isn't a surprise at PR time. - -ACP protocol-shape details (exact method names, `session/update` variants, permission option kinds, stop reasons) are taken from the ACP spec and the `@agentclientprotocol/sdk` types; they are not independently verifiable until the dependency is added, so the implementation pins the SDK version and conforms to its types rather than to this RFC's prose where they differ. diff --git a/docs/rfc/proposed/feature/2026-06-14-acp-multi-session.md b/docs/rfc/proposed/feature/2026-06-14-acp-multi-session.md deleted file mode 100644 index a65f6b67eb..0000000000 --- a/docs/rfc/proposed/feature/2026-06-14-acp-multi-session.md +++ /dev/null @@ -1,48 +0,0 @@ -# RFC: Multiplex concurrent ACP sessions over one connection - -Status: proposed - -> **Implementation status:** the multi-session bridge (steps 1, 3, 4) and the bash task-ownership isolation are implemented in `packages/ui/acp` + `packages/bash/tool-bash`. **Per-session *permission* ownership is deferred** — it depends on [the ACP support permission gate](2026-06-14-acp-agent-client-protocol.md) (`TODO(rfc010-permission-gate)`), which is itself deferred; the `agent→sessionId` reverse map the gate will route through is in place. Step 2's per-session disposer scope is now implemented (see [agent lifecycle & ownership seams](../../implemented/architecture/2026-06-18-agent-lifecycle-and-ownership-seams.md)): the factory returns a per-agent `AgentHandle` whose `dispose()` stops the loop, awaits quiescence, unregisters the agent, and removes its session, so a bare client disconnect leaves no registered agent or session-store entry. Status stays `proposed` until per-session permission ownership lands. - -> **Target-client note:** Zed is the current target ACP client, and its ACP client maintains a `HashMap` plus `pending_sessions` for concurrent `session/load` calls. The competing simplification to return to one live session per connection was rejected after checking that target-client shape; this RFC remains the path for finishing multiplexing and per-session permission ownership. See [the rejected simplification](../../rejected/simplification/2026-06-20-single-session-acp-bridge.md). - -## Problem - -[ACP support](2026-06-14-acp-agent-client-protocol.md) ships with a single active session per connection: a second `session/new` is rejected. Editors expect to run several conversations over one agent subprocess — a user opens multiple threads, or a client pre-warms sessions. The single-session guard is a deliberate MVP scope cut, not an architectural limit; this RFC lifts it. - -This paragraph is historical: the multi-session bridge has landed. The remaining proposed work is per-session permission ownership plus the lifecycle seams now tracked in [agent lifecycle and ownership seams](../../implemented/architecture/2026-06-18-agent-lifecycle-and-ownership-seams.md). - -## Proposal - -The harness core already supports many agents (`AgentRegistry.list()` and `AgentLoop.create` impose no count limit), so multiplexing is a bridge-layer change in `@deepseek-ai/dsh-acp`, not a loop or core change. - -- Lift the single-session guard in `session/new`; allow N live sessions, each mapped to its own `ReactLoopAgent`. -- The bridge's `sessionId→agent` and `Session→sessionId` maps (introduced single-entry by [the ACP support RFC](2026-06-14-acp-agent-client-protocol.md)) become true multi-entry, plus a third `agent→sessionId` reverse map: the `tools/execute` permission gate receives only `exec.agent` (no sessionId), so it needs an O(1) reverse lookup to find the owning session. Every `agent/*` event and every `session/event` is demuxed strictly by id, so two sessions streaming at once never interleave their `session/update` notifications. -- Per-session prompt queues: [the ACP support RFC](2026-06-14-acp-agent-client-protocol.md)'s single-entry in-flight-prompt state becomes multi-entry — one in-flight prompt *per session*, tracked per `sessionId`. -- Per-session cancel routing: `session/cancel` cancels only its own session's agent (via the queue-aware `agent.cancel()`) and settles only that session's in-flight prompt. The cancel is scoped to that one agent — a per-agent `AbortController` for the running step plus the agent's own queued/steering FIFOs — so it never touches another session's stream or pending prompt. -- Per-session permission ownership: a `session/request_permission` and its outcome are bound to the originating session via the reverse map, so a permission prompt or a cancel in one session can never resolve another session's pending permission. - -## Plan - -1. Generalize the two id maps to multi-entry and add the `agent→sessionId` reverse map; add a per-session record holding the agent, the in-flight-prompt state, the pending-permission registry, and the session's disposer scope (see step 2). -2. Give each session a real per-session disposer scope, NOT `ctx.extend()` — in Cordis `ctx.extend()` only creates a child context/prototype, but `ctx.on()` registered on it is still owned by the current plugin fiber, so disposing it would not remove that session's listeners. Use a genuine child fiber (load a per-session sub-plugin, e.g. `ctx.plugin(...)` returning a fork, or collect each session's `ctx.on` disposers in its session record and call them on teardown). Demux every `agent/*` and `session/event` by id into the right session record. Note the single global `tools/execute` listener stays on the bridge root (it must see all agents) and routes via the reverse map. -3. Lift the `session/new` guard; keep `session/load` ([from ACP support](2026-06-14-acp-agent-client-protocol.md)) working per session. -4. Tests for cross-session isolation: two sessions streaming and permission-prompting concurrently never interleave; a cancel/abort in one session leaves the other's stream and pending permission untouched; per-session in-flight-prompt enforcement holds independently; disposing one session leaves the others running. - -## Alternatives considered - -**A per-session `ctx.extend()` scope** — rejected: in Cordis, `ctx.extend()` only creates a child context/prototype, and `ctx.on()` registered on it is still owned by the current plugin fiber, so disposing it would not remove that session's listeners. A genuine child fiber (or a per-session collection of disposers) is required. - -## Acceptance criteria - -- N concurrent sessions stream and permission-prompt without interleaving their `session/update` notifications; a cancel in one session leaves every other session's stream, queued prompts, and pending permissions untouched. -- Disposing one session removes exactly its own listeners; connection teardown reaches quiescence across all sessions. -- One session's agent cannot read or kill another session's background bash task. - -## Risks - -Listener fan-out cost: each session adds listeners; ensure disposal of one session removes exactly its own and the connection teardown ([from ACP support](2026-06-14-acp-agent-client-protocol.md)) still reaches quiescence across all sessions. - -The subtle correctness trap is cross-session leakage — a cancel or abort on one session settling another session's pending permission. The per-session permission ownership rule (routed via the `agent→sessionId` reverse map) and its isolation test are the guard. - -Shared background-task state: the bash executor's task ids are global and predictable (`bash-1`, `bash-2`, …), and `bash_output`/`bash_kill` look up by id without checking the caller. Under one session this is benign; under N sessions one session's agent could read or kill another's background task. This is a pre-existing `tool-bash` gap that multi-session turns into a real isolation hole — fixing it (validate the caller against the task owner) belongs with this RFC or a companion `tool-bash` change. diff --git a/docs/rfc/proposed/feature/2026-06-30-pre-tool-input-rewrite.md b/docs/rfc/proposed/feature/2026-06-30-pre-tool-input-rewrite.md index b3e6ab3cd7..9657512082 100644 --- a/docs/rfc/proposed/feature/2026-06-30-pre-tool-input-rewrite.md +++ b/docs/rfc/proposed/feature/2026-06-30-pre-tool-input-rewrite.md @@ -4,7 +4,7 @@ Status: proposed ## Problem -The [interception-seams RFC](../../implemented/feature/2026-06-30-interception-seams.md) added `tools/pre-execute` returning a `PreToolDecision` (allow/deny/ask) — but deliberately NOT input rewrite (a hook changing a tool call's `arguments` before it runs). Claude Code's `PreToolUse` hook offers an `updatedInput`, so a faithful CC bridge wants the same. This RFC designs that, separately, because doing it consistently is a real problem — not a field to bolt onto the allow decision. +The [interception-seams RFC](../../implemented/feature/2026-06-30-interception-seams.md) defines `tools/pre-execute` as an allow/deny/ask gate over an execution whose identity is already protected and whose arguments are deeply frozen. Claude Code's `PreToolUse` hook also offers `updatedInput`, so a faithful bridge needs an explicit rewrite mechanism. A rewrite cannot be a mutation escape hatch on the existing execution object: it must keep the durable history, audit record, presentation, and executed value consistent. ## The problem: three readers of pre-execution arguments @@ -14,37 +14,38 @@ In the loop, a tool call's arguments are committed to the log and read by live c 2. **`tool/call`** is the durable AUDIT record, appended before `ctx.tools.execute()`. 3. **Live presentation reads `tool/call.arguments`**: the ACP bridge remembers them and passes them to `presentResult`; `dsh-tool-bash` derives the card title, the rawInput, the cwd, and the terminal-vs-background treatment from them. -So an "input rewrite" that changes ONLY what executes would make the UI show one command while another RAN, and render result state against the wrong arguments — a real inconsistency, not a documentable gap. (The existing low-level capability to mutate `exec.arguments` in a listener has exactly this latent inconsistency; it is unadvertised precisely because of this — yet not unused: a tool-bash integration test rewrites a scripted call's arguments through it (`packages/bash/tool-bash/tests/integration.spec.ts`), so this design must either sanction that path with the consistency unit below or seal it — `readonly` arguments at the seam, with the test shim moved onto a behavior-level helper.) +An execution-only rewrite would make the UI show one command while another ran and render the result against the wrong arguments. The registry prevents that failure mode today: it structured-clones and deep-freezes `arguments`, makes the execution identity properties non-writable, and exposes no test shim or listener path that can replace them. The rewrite design must preserve that protected-identity boundary rather than weaken it. ## Proposal -A sketch, to validate against the code when built. Treat input rewrite as a consistency unit: when a `pre-execute` hook supplies `updatedInput`, the rewrite must be reflected in ALL three readers, atomically, before execution: +A rewrite is a pre-identity consistency transaction. When a hook supplies `updatedInput`, the effective value must be chosen before the registry constructs its immutable `ToolExecution`, and it must be reflected in all three readers atomically: - The `tool/call` audit event records the REWRITTEN arguments (with the original retained in a sidecar field for the audit trail — a hook changed the call, and both the original and the effective arguments are facts worth keeping). - The `assistant/message` in derived history must agree with what executed — options to evaluate: rewrite the assistant message's tool-call block in place (changes what the model "sees it said"), or record a separate correction the next request carries. The CC model is that the model sees the rewrite took effect. - Presentation (`presentCall`/`presentResult`) reads the rewritten arguments, so the UI shows what actually ran. -The shape would extend `PreToolDecision` with an allow-variant `arguments` (or a dedicated `{kind:'rewrite', arguments}`), and the loop would thread the rewrite through the three readers above rather than only into `ctx.tools.execute()`. +Extending `PreToolDecision` at its current firing point is insufficient: both durable records already exist by then, and the execution identity is protected. The implementation must either move the relevant decision before the log commit or add a dedicated earlier rewrite decision over the pending model call. After the loop commits the effective arguments to history and audit, it constructs the ordinary immutable execution and runs the existing allow/deny/ask and tool pipeline unchanged. ## Alternatives considered -### Why not now +### Why not mutate the execution object? -The interception-seams RFC notes input rewrite "fought the code across two review rounds" — the signal AGENTS.md names for an over-reaching change. Shipping allow/deny/ask first keeps the seam honest (no advertised contract that silently desyncs the UI), and a CC/Codex bridge that receives an `updatedInput` logs it and surfaces a faithful-but-degraded warning (like `ask`→deny) until this lands. This RFC is the home for the consistent design; `TODO(pre-tool-input-rewrite)` in the loop's pre-execute call site anchors it. +Allowing a pre-execute listener to assign `exec.arguments` would provide only an execution rewrite, leaving model history, audit, and presentation unchanged. Keeping the identity protected makes such partial behavior unrepresentable. Until the consistency transaction exists, a CC/Codex bridge logs and warns about `updatedInput` rather than claiming it was honored; `TODO(pre-tool-input-rewrite)` at the loop dispatch site anchors the missing earlier phase. ## Acceptance criteria -- A `pre-execute` rewrite is reflected in all three readers atomically before execution: the `tool/call` audit records the rewritten arguments (the original retained in a sidecar field), derived history agrees with what executed, and presentation renders the rewritten arguments. -- The unadvertised `exec.arguments` mutation path is either sanctioned by this consistency unit or sealed (`readonly` arguments at the seam, the test shim moved onto a behavior-level helper). +- A requested rewrite is resolved before `ToolExecution` identity is created and reflected in all three readers atomically: the `tool/call` audit records the rewritten arguments (the original retained in a sidecar field), derived history agrees with what executed, and presentation renders the rewritten arguments. +- The effective `ToolExecution.arguments` remains deeply frozen and non-writable throughout pre-policy, guards, dispatch, post-policy, and final observation; no mutation shim is introduced. - The CC/Codex bridges honor `updatedInput` instead of logging the faithful-but-degraded warning. ## Risks - Rewriting the `assistant/message` tool-call block changes what the model "sees it said"; whether any provider rejects that on replay is the open question that must be settled empirically before the decision shape freezes. -- Until this lands, the unadvertised mutation path keeps its latent UI-desync inconsistency. +- An earlier rewrite phase changes the ordering relationship among `assistant/message`, `tool/call`, hook audit events, and execution; the design must pin that ordering without weakening turn enclosure or call/result adjacency. ## Open questions - Does rewriting the `assistant/message` tool-call block corrupt any provider's expectation on replay, or is a separate correction safer? - Should the original arguments be preserved on the `tool/call` event (audit) and, if so, under what field? +- Does the rewrite decision move before the log commit or become a dedicated earlier seam, and how do existing pre-tool allow/deny hooks avoid running twice? - How does this interact with a future permission `ask` flow (a user approving a rewritten call)? diff --git a/docs/rfc/proposed/simplification/2026-07-04-prune-dead-core-spine-surface.md b/docs/rfc/proposed/simplification/2026-07-04-prune-dead-core-spine-surface.md index a6f932b1ed..c6bfbe967c 100644 --- a/docs/rfc/proposed/simplification/2026-07-04-prune-dead-core-spine-surface.md +++ b/docs/rfc/proposed/simplification/2026-07-04-prune-dead-core-spine-surface.md @@ -8,13 +8,13 @@ Three pieces of public spine surface share one defect class: their only possible 1. **`SurfaceManager.invalidate()`** (`packages/core/session/src/surface.ts`). Its documented trigger — "the log has been replaced wholesale (e.g. after Session seed)" — is structurally unreachable: seeding happens inside the `Session` constructor, `_surface` is created lazily on first access, and the log reference is never reassigned afterward, so no constructed `SurfaceManager` ever observes a wholesale replacement. Sole caller: its own unit test. A rollback primitive protecting a scenario the implementation cannot produce. 2. **The `runLoop`, `Inbox`, and `InboxMessage` exports** (`packages/core/agent-loop/src/index.ts`). `runLoop` has no importer outside the package — the only callers are the package's own internals (the agent constructs its loop with it), so the public re-export has zero consumers; `Inbox`/`InboxMessage` likewise reach outside code only through the package's own inbox spec (switchable to the source module). The exports contradict the package's own docs — the inbox module doc says the public surface is `Agent.send()`/`Agent.steer()` — and the [architecture dependency rule](../../../architecture.md): nothing programs against `dsh-agent-loop`; a replacement loop is a different bundle built on `dsh-agent`, not a consumer of this package's internals. `ReactLoopAgent` stays exported (cross-package tests construct it by package name). -3. **`ToolExecutionResult.callId`** (`packages/core/tools/src/index.ts`; the *input* `ToolExecution.callId` stays). Zero readers — and no listener can even construct a result: `tools/pre-execute`/`tools/post-execute` listeners return Decisions, the registry builds every result itself and always sets `callId` to the input `exec.callId`, and the post-execute dispatch snapshots the outcome before the waterfall precisely so a listener mutating the shared result reference cannot corrupt the id. The loop independently ignores `result.callId` in favor of its own `call.id`, and two regression tests exist solely to prove the field cannot matter (the loop's ignores-result-callId test and the registry's mutation guard). A field that is by construction a copy of its input, defended by snapshot machinery, and pinned by tests proving it is ignored is pure liability surface; the ACP bridge correlates via the session event's `data.callId`, never via the execution result. +3. **`ToolExecutionResult.callId`** (`packages/core/tools/src/index.ts`; the input `ToolExecution.callId` stays). Zero consumers read it. A `tools/execute` wrapper may construct or replace a result, but the registry rejects any `callId` that differs from the immutable execution identity and rebuilds later outcomes from protected snapshots; `tools/post-execute` receives that same execution beside the result, and the observe-only `tools/result` notification receives both as immutable values. The loop independently correlates with its model call's `call.id`, while ACP correlates through the session event's `data.callId`. The result field is therefore a compulsory copy of information already present at every extension point, plus validation and regression tests whose only job is to prove the copy cannot disagree. ## Proposal -Delete the method and its test; delete the three export lines and their `packages/core/agent-loop/README.md` rows, pointing the inbox spec at the source module; drop the result field from the type, the registry's construction sites (the deny result, the dispatch result, `toolErrorResult`, and the post-execute snapshot's `callId` leg), the loop's ignore-comment, the proves-ignored regression test, and the mutation guard's `callId` assertions — the hazard they all pin disappears with the field, while the result's `additionalContext` ferry (a consumed post-execute channel) stays untouched. Update the `ToolExecutionResult` paste in [tools.md](../../../core-data-structures/tools.md) (and its `scripts/type-equiv.manifest.json` row) and the result-shape row in `packages/core/tools/README.md`; for the `invalidate()` removal, amend the [session-surface RFC](../../implemented/architecture/2026-06-18-session-surface.md)'s full-rebuild-after-wholesale-replacement sentence per [implemented/AGENTS.md](../../implemented/AGENTS.md). +Delete the method and its test; delete the three export lines and their `packages/core/agent-loop/README.md` rows, pointing the inbox spec at the source module; drop the result field from the type, the registry's construction sites (deny, dispatch, `toolErrorResult`, post-execute snapshots), its around-wrapper mismatch validation, the loop's ignore-comment, and the tests that prove the duplicate id cannot matter. The result's consumed `additionalContext` ferry and the execution object's authoritative `callId` stay untouched. Update the `ToolExecutionResult` paste in [tools.md](../../../core-data-structures/tools.md) (and its `scripts/type-equiv.manifest.json` row) and the result-shape row in `packages/core/tools/README.md`; for the `invalidate()` removal, amend the [session-surface RFC](../../implemented/architecture/2026-06-18-session-surface.md)'s full-rebuild-after-wholesale-replacement sentence per [implemented/AGENTS.md](../../implemented/AGENTS.md). -Sequencing: the in-flight surface-cache work (tool-pairing balance caching) neither uses nor touches `invalidate`, so that removal lands after or alongside it mechanically. The execute pipeline is `tools/pre-execute` → dispatch → `tools/post-execute`, and post-execute listeners receive the execution object alongside the result — nothing needs the result's own id. +Sequencing: the surface-cache work (tool-pairing balance caching) neither uses nor touches `invalidate`, so that removal can land after or alongside it mechanically. The full execution pipeline carries the immutable execution object through pre-policy, guards, around-dispatch wrappers, post-policy, and final result observation; nothing needs the result to repeat its id. ## Alternatives considered @@ -25,7 +25,7 @@ A future consumer that swaps a session's log in place would want a reset primiti ## Acceptance criteria - `invalidate()` and the result `callId` appear only in this RFC; `runLoop`/`Inbox`/`InboxMessage` remain package-internal only — no re-export from the package index and no outside-package importer; the agent-loop README lists only the consumed public surface; the inbox spec imports the source module. -- The pre-/post-execute pipeline contract tests pass with the shrunk result type; the mutation-guard and proves-ignored tests shed their `callId` legs with the hazard they pin. +- The complete tool-pipeline contract tests pass with the shrunk result type; the around-wrapper mismatch test, mutation-guard id assertions, and proves-ignored loop test disappear with the duplicate field. ## Risks diff --git a/docs/rfc/rejected/architecture/2026-06-11-immutable-public-surfaces.md b/docs/rfc/rejected/architecture/2026-06-11-immutable-public-surfaces.md index 6c404a05a9..3eedd92a2d 100644 --- a/docs/rfc/rejected/architecture/2026-06-11-immutable-public-surfaces.md +++ b/docs/rfc/rejected/architecture/2026-06-11-immutable-public-surfaces.md @@ -1,25 +1,24 @@ # RFC: Deep-readonly public surfaces -Status: rejected — the pervasive `DeepReadonly` type flip was rejected in favor of an always-on `deriveMessages` clone plus dev-mode `Object.freeze` + invariants. The immutability *goal* shipped via that alternative; see [dev-mode invariants](../../implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md). +Status: rejected — the pervasive `DeepReadonly` type flip is replaced by source-owned runtime immutability in `Session` plus relational development assertions. See [source-owned session immutability and dev-mode invariants](../../implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md). ## Problem -The session log is append-only by contract, but `session.events` returns `readonly SessionEvent[]` whose *elements* are mutable: a plugin can reach in and rewrite history (`events[0].data.content.push(...)`), silently breaking replay equivalence and the derived-history guarantee. The same applies to derived messages and prompt assemblies passed through waterfalls — mutation is sometimes the intended idiom (waterfall middleware mutates the request) and sometimes corruption (mutating a *logged* event), and the types don't distinguish. +The rejected proposal targeted an ownership hole that a `readonly SessionEvent[]` type alone cannot close: its elements remain mutable at runtime, so a cast or plain JavaScript can rewrite nested history. The implemented design closes that hole in `Session` by materializing and deep-freezing every accepted event and returning frozen array snapshots. In-flight prompt waterfalls remain intentionally transformable, so immutability is an ownership boundary rather than a blanket type rule. ## Proposal -> **Implemented differently — see the Status line and [dev-mode invariants](../../implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md).** The `DeepReadonly` design below was rejected as written (compile-only, high type-noise, castable). What shipped: an always-on deep clone in `deriveMessages` (closing the request/adapter aliasing path) plus a dev-mode `Object.freeze` + invariants plugin. The proposal text is kept for the record. +> **Implemented differently — see the Status line and [source-owned session immutability and dev-mode invariants](../../implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md).** The `DeepReadonly` design below is rejected as written: it is compile-only, noisy across consumers, and castable. `Session` instead snapshots and deep-freezes accepted events and public log snapshots in every composition; `deriveMessages()` returns detached frozen projections; the development plugin checks cross-record and cross-seam relationships. Make immutability part of the type where mutation is corruption: - `SessionEvent` data becomes `DeepReadonly` on the way OUT of a session (`events`, `session/event` listeners); `append()` keeps taking plain mutable input. A `DeepReadonly` utility type lands in dsh-llm next to the brand/never helpers. - `deriveMessages()` returns deep-readonly messages; the loop clones before handing a mutable request to the `agent/request` waterfall (mutation there is sanctioned — the clone makes the boundary explicit and cheap, once per step). - `PromptAssembly` stays mutable through its waterfall (sanctioned) but the registry's internal section list is cloned per assembly (already true). -- Optionally, dev-mode `Object.freeze` of event data behind [the dev-mode invariants](../../implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md) flag, so sanctioned-mutation violations throw in tests rather than corrupting silently. ## Plan -Introduce `DeepReadonly`, flip the session read paths, fix resulting compile errors in consumers (expected: a handful in tests), add the freeze-in-dev option alongside [the dev-mode invariants](../../implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md) plugin. +Introduce `DeepReadonly`, flip the session read paths, and fix the resulting compile errors in consumers. ## Risks diff --git a/docs/rfc/rejected/simplification/2026-06-20-single-session-acp-bridge.md b/docs/rfc/rejected/simplification/2026-06-20-single-session-acp-bridge.md index e68cd24346..8f1d4e4531 100644 --- a/docs/rfc/rejected/simplification/2026-06-20-single-session-acp-bridge.md +++ b/docs/rfc/rejected/simplification/2026-06-20-single-session-acp-bridge.md @@ -4,7 +4,7 @@ Status: rejected — Zed is the current target ACP client and its ACP implementa ## Problem -The ACP bridge now supports multiple live sessions on one JSON-RPC connection. That capability brings multi-entry session maps, reverse session/agent lookups, per-session prompt state, loading ids, demux for every event, cross-session teardown, and isolation concerns for future permission prompts and background tasks. The older [multi-session ACP proposal](../../proposed/feature/2026-06-14-acp-multi-session.md) still tracks the unfinished permission-ownership piece; this RFC is the competing simplification path. +The ACP bridge now supports multiple live sessions on one JSON-RPC connection. That capability brings multi-entry session maps, reverse session/agent lookups, per-session prompt state, loading ids, demux for every event, cross-session teardown, and isolation concerns for future permission prompts and background tasks. The older [multi-session ACP proposal](../../implemented/feature/2026-06-14-acp-multi-session.md) still tracks the unfinished permission-ownership piece; this RFC is the competing simplification path. The product target has proven it needs concurrent editor conversations over one harness process: Zed's ACP connection owns multiple sessions and load states. The snapshot replay tier still avoids concurrent model streams because its replay entries are positional; that is a test-fixture limitation, not a reason to remove bridge multiplexing. @@ -20,7 +20,7 @@ Remove the multi-session maps and demux where a single `SessionRecord | undefine - `session/new` and `session/load` reject while that record exists. - Event handlers no longer demux across a `Map`. - Multi-session tests are removed or moved under the proposal that continues to defend multiplexing. -- The existing [multi-session ACP proposal](../../proposed/feature/2026-06-14-acp-multi-session.md) is updated to link this RFC and remains the live direction. +- The existing [multi-session ACP proposal](../../implemented/feature/2026-06-14-acp-multi-session.md) is updated to link this RFC and remains the live direction. ## What we give up diff --git a/docs/testing.md b/docs/testing.md index a4a77c9d0c..62989a1096 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -1,17 +1,17 @@ # Testing policy -How this repo tests, tier by tier, and the rules that keep a green suite meaning something. Commands live in the root [AGENTS.md](../AGENTS.md) § Commands; the RFCs linked per tier carry the design rationale. +How this repo tests, tier by tier, and the rules that keep a green suite meaningful. Commands live in root [AGENTS.md](../AGENTS.md); linked RFCs carry the rationale. ## Tiers -- **Unit** (`pnpm run test`): vitest over `packages|examples/*/tests/**/*.spec.ts`, colocated with what they test. Every registry gets an HMR-safety test (dispose the contributing fiber, assert cleanup). Excessive tests are welcome — err toward covering edge cases, error paths, event ordering, and concurrency races; review findings get regression tests (see `packages/core/agent-loop/tests/review-fixes.spec.ts`). +- **Unit** (`pnpm run test`): vitest over `packages|examples/*/tests/**/*.spec.ts`, colocated with what they test. Every registry gets an HMR-safety test (dispose the contributing fiber, assert cleanup). Prefer edge cases, error paths, event ordering, and concurrency races; review findings get regression tests (see `packages/core/agent-loop/tests/review-fixes.spec.ts`). - **Coverage gate** (`pnpm run test:coverage`): the gating run, per-file 100% on `packages/*/*/src`. An uncovered line is often dead code the gate is correctly flagging for deletion, not a missing test to bolt on. Line coverage is necessary, never sufficient — it proves lines ran, not that the feature works as shipped. - **Real-API e2e** (`pnpm run test:e2e`): with-key tests against live provider APIs — the DeepSeek model plus provider-specific smokes that gate on their own keys (`EXA_API_KEY`, `PERPLEXITY_API_KEY`, …); each suite self-skips without its key so keyless CI stays green ([real-API e2e RFC](rfc/implemented/testing/2026-06-19-real-api-e2e-ci.md)). -- **Snapshot** (`pnpm run test:snapshot`): boots the real example subprocess, replays a recorded session keyless, diffs normalized stdout + the re-persisted log against committed goldens ([snapshot RFC](rfc/implemented/testing/2026-06-19-acp-snapshot-tests.md)). Re-record with `pnpm run test:snapshot:record`; reviewing the golden diff is part of the review. System-prompt/tool-schema content is pinned by ONE scenario (`text-turn`) and tokenized in every other fixture, so a prompt or schema edit churns one committed line ([pinned-header RFC](rfc/implemented/testing/2026-07-06-pin-request-header-content-in-one-scenario.md)). +- **Snapshot** (`pnpm run test:snapshot`): boots the real example subprocess, replays a recorded session keyless, diffs normalized stdout + the re-persisted log against committed goldens ([snapshot RFC](rfc/implemented/testing/2026-06-19-acp-snapshot-tests.md)). Use `pnpm run test:snapshot:record` when the model transcript should change; use `pnpm run test:snapshot:refresh` when the committed transcript is still the right mock LLM input and replay goldens need keyless rewrite. Review the golden diff. System-prompt/tool-schema content is pinned by ONE scenario (`text-turn`) and tokenized in every other fixture, so a prompt or schema edit churns one committed line ([pinned-header RFC](rfc/implemented/testing/2026-07-06-pin-request-header-content-in-one-scenario.md)). ## The with-key policy: inference is cheap here -We are DeepSeek — do not ration real-API tests. A no-key test proves the plumbing; only a with-key run proves the agent works against a real model. Write many: real prompts that write files, multi-turn conversations, tool use, cancellation mid-stream. Cheapest and highest-value are **smoke tests** that boot the real example, send one real prompt, and check the world — they catch the "green unit tests, broken product" class that mocks structurally cannot ([postmortem 0001](postmortem/0001-acp-default-export-drops-inject.md)). The self-skip exists only so secretless CI and keyless contributors aren't blocked; it is not a cost signal. Every example ships a keyless smoke and — unless keyless-by-nature — a with-key smoke ([examples/AGENTS.md](../examples/AGENTS.md)). +We are DeepSeek — do not ration real-API tests. A no-key test proves plumbing; only a with-key run proves the agent works against a real model. Write many: file-writing prompts, multi-turn conversations, tool use, cancellation mid-stream. Highest-value are **smoke tests** that boot the real example, send one real prompt, and check the world — they catch the "green unit tests, broken product" class that mocks structurally cannot ([postmortem 0001](postmortem/0001-acp-default-export-drops-inject.md)). The self-skip exists only so secretless CI and keyless contributors aren't blocked; it is not a cost signal. Every example ships a keyless smoke and — unless keyless-by-nature — a with-key smoke ([examples/AGENTS.md](../examples/AGENTS.md)). ## Prefer the real implementation over a mock diff --git a/docs/tool-catalog.md b/docs/tool-catalog.md index 6d97017f1d..9ea71d3005 100644 --- a/docs/tool-catalog.md +++ b/docs/tool-catalog.md @@ -16,10 +16,11 @@ This table connects model-visible tool names to the plugin package and service s | Tool package | Model-visible names | Requires | Writes / affects | Shipped aliases | Deployment note | | --- | --- | --- | --- | --- | --- | | `@deepseek-ai/dsh-tool-ask-user` | `ask_user_question` | `ctx.tools`, `ctx.userInteraction` | `tool/call`, `tool/result after a UI/provider answers the question` | - | ask_user_question pauses the tool call until the active UI provider returns a human answer. | -| `@deepseek-ai/dsh-tools` | `run_code` | `ctx.tools`, `ctx.codeRuntime (execution time)`, `ctx.systemPrompt` | `tool/call`, `one tool/code-dispatch per bridged sub-call`, `tool/result` | - | Registered by the tool registry itself under `mode: code` / `mode: both` (see the Code Mode RFC). Under `code` it is the ONLY wire tool; the other registered tools are declared to the model as a generated TypeScript SDK prompt section instead, and a program calls them through port-bridged bindings that dispatch through the ordinary tools/pre-execute → tools/post-execute pipeline, one at a time. | +| `@deepseek-ai/dsh-tools` | `run_code` | `ctx.tools`, `ctx.codeRuntime (execution time)`, `ctx.systemPrompt` | `tool/call`, `one tool/code-dispatch per bridged sub-call`, `tool/result` | - | Owned by the tool registry as a reserved transport outside filterable capability layers under `mode: code` / `mode: both` (see the Code Mode RFC). Under `code` it is the registry's only wire contribution; the other visible capabilities are declared in a generated TypeScript SDK section, and a program calls them through serialized bindings that re-enter the complete guarded tool pipeline and link each nested execution to this outer result. | | `@deepseek-ai/dsh-tool-bash` | `bash`, `bash_kill`, `bash_output` | `ctx.tools`, `ctx.bash` | `tool/call`, `tool/result`, `context/message via agent.inject() for background completion notices` | - | The bash/bash_output/bash_kill tools are model-facing consumers of the bash executor seam. | | `@deepseek-ai/dsh-tool-cordis` | `cordis_inspect`, `cordis_mount`, `cordis_unmount` | `ctx.tools` | `tool/call`, `tool/result`, `live plugin-tree mutations (mount/unmount)` | - | Ships in examples/cordis-agent only (a deliberate opt-in — mounted code gets the real ctx, see docs/rfc/implemented/feature/2026-07-08-self-referential-cordis-toolset.md). Plugins the model mounts may register ADDITIONAL model-visible tools at runtime; the request-header ToolsDelta logs those tool-set changes. | | `@deepseek-ai/dsh-tool-fs` | `edit`, `read`, `write` | `ctx.tools`, `ctx.fs`, `ctx.systemPrompt` | `tool/call`, `fs/write-intent or fs/edit-intent for mutations`, `fs/observed after successful file operations`, `tool/result` | - | The read-before-write/edit policy is added by `@deepseek-ai/dsh-fs-policy` (an `fs/*` event-gate plugin, no schema change); a deployment that loads these tools is expected to also load it. The tool schemas above are identical with or without the policy plugin. | +| `@deepseek-ai/dsh-tool-skill` | `skill` | `ctx.tools`, `ctx.skills` | `tool/call`, `tool/result` | - | - | | `@deepseek-ai/dsh-tool-subagent` | `subagent` | `ctx.tools`, `ctx.subagents` | `tool/call`, `tool/result`, `child session events through the chosen provider` | `subagent`, `subagent_fork` | The registered tool name is the load-time `toolName` config (default `subagent`); the schema above is that default. The shipped example agents load this package once per subagent backend, so the model additionally sees `subagent_fork` (bound to the fork backend) with an identical schema — see `examples/coding-agent/cordis.yml` and `examples/acp-agent/cordis.yml`. | | `@deepseek-ai/dsh-tool-todo` | `todo_write` | `ctx.tools`, `owning Agent session` | `tool/call`, `todo/write`, `tool/result` | - | todo_write is session-owned state; UIs render the latest todo/write event as a checklist or ACP plan. | | `@deepseek-ai/dsh-tool-workflow` | `workflow` | `ctx.tools`, `ctx.workflows`, `ctx.systemPrompt`, `a calling Agent (exec.agent parents the script children)` | `tool/call`, `tool/result` | - | - | @@ -118,13 +119,13 @@ Execute a TypeScript program against the available tools. Write the BODY of an a Source: [`packages/core/tools/src/code-mode.ts`](../packages/core/tools/src/code-mode.ts) -Registered by the tool registry itself under `mode: code` / `mode: both` (see the Code Mode RFC). Under `code` it is the ONLY wire tool; the other registered tools are declared to the model as a generated TypeScript SDK prompt section instead, and a program calls them through port-bridged bindings that dispatch through the ordinary tools/pre-execute → tools/post-execute pipeline, one at a time. +Owned by the tool registry as a reserved transport outside filterable capability layers under `mode: code` / `mode: both` (see the Code Mode RFC). Under `code` it is the registry's only wire contribution; the other visible capabilities are declared in a generated TypeScript SDK section, and a program calls them through serialized bindings that re-enter the complete guarded tool pipeline and link each nested execution to this outer result. ## `@deepseek-ai/dsh-tool-bash` ### `bash` -Execute a bash command (`bash -c`) and return its stdout/stderr. Each call runs in a fresh shell: no state (cwd, variables, functions) persists between calls — pass `workdir` instead of using `cd`. Non-zero exits are reported as `[exit code: N]`. Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. Set `run_in_background: true` for long-running commands: the call returns a task id immediately; poll it with `bash_output` and stop it with `bash_kill`. +Execute a bash command (`bash -c`) and return its stdout/stderr. Each call runs in a fresh shell: no state (cwd, variables, functions) persists between calls — pass `workdir` instead of using `cd`. Non-zero exits are reported as `[exit code: N]`. Commands may run under a file sandbox; a blocked file operation is reported as `[sandbox: file access denied under mode]` — a policy denial, not a bug in the command; do not retry another way (a background task reports the same marker via bash_output once it has finished). Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. Set `run_in_background: true` for long-running commands: the call returns a task id immediately; poll it with `bash_output` and stop it with `bash_kill`. ```json { @@ -370,6 +371,29 @@ Source: [`packages/fs/tool-fs/src/index.ts`](../packages/fs/tool-fs/src/index.ts The read-before-write/edit policy is added by `@deepseek-ai/dsh-fs-policy` (an `fs/*` event-gate plugin, no schema change); a deployment that loads these tools is expected to also load it. The tool schemas above are identical with or without the policy plugin. +## `@deepseek-ai/dsh-tool-skill` + +### `skill` + +Load the full instructions for an available skill. Call this with the exact skill name from the session skill catalog before acting on a task that names or clearly matches that skill. + +```json +{ + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "The exact skill name from the available skills list." + } + }, + "required": [ + "name" + ] +} +``` + +Source: [`packages/skill/tool-skill/src/index.ts`](../packages/skill/tool-skill/src/index.ts) + ## `@deepseek-ai/dsh-tool-subagent` ### `subagent` diff --git a/docs/tool-execution-pipeline.md b/docs/tool-execution-pipeline.md index 09a863badc..6f5c8890bf 100644 --- a/docs/tool-execution-pipeline.md +++ b/docs/tool-execution-pipeline.md @@ -3,7 +3,7 @@ # Tool Execution Pipeline -This graph shows where policy, hooks, sandboxing, filesystem guards, result rewriting, and UI rendering fit without changing the loop. The key extension points are the `tools/pre-execute`, `tools/execute`, and `tools/post-execute` waterfalls. +This graph shows where policy, hooks, sandboxing, filesystem guards, result rewriting, final-outcome observation, and UI rendering fit without changing the loop. The transformable extension points are the `tools/pre-execute`, `tools/execute`, and `tools/post-execute` waterfalls; monotonic guards and `tools/result` are the owner-enforced boundaries around them. ```mermaid flowchart TD @@ -11,32 +11,43 @@ flowchart TD toolCall["Session event: tool/call
logged before execution"] presentCall["UI pending card
presentCall(args)"] pre["tools/pre-execute waterfall
hooks, permission, sandbox"] - denied["deny or ask
tool body skipped"] + guards["Registered monotonic guards
deny or abstain; identity protected"] + denied["denied or approval refused
tool body skipped"] + approval["ctx.approval one-shot prompt
absent or unanswerable: deny"] around["tools/execute waterfall
timeout, retry, metrics (around dispatch)"] toolBody["Registered tool execute() body"] fsGate["fs/write-intent or fs/edit-intent
tool-fs mutations only"] owned["Tool-owned session events
todo/write, fs/observed, hook/invoked, hook/result, tool/code-dispatch"] post["tools/post-execute waterfall
accept, block, replace, add context"] + final["tools/result synchronous notification
frozen authoritative outcome"] context["Buffered additionalContext
context/message after all tool results"] toolResult["Session event: tool/result
single model-facing outcome"] + allResults["All calls in the step settled
and tool/result events recorded"] presentResult["UI completed card
presentResult(args, result)"] model --> toolCall toolCall --> presentCall toolCall --> pre - pre -->|allow| around + pre -->|allow| guards + guards -->|allow| around + guards -->|deny| denied around --> toolBody - pre -->|deny or ask| denied + pre -->|deny| denied + pre -->|ask| approval + approval -->|allowed-once| guards + approval -->|rejected, cancelled, unavailable| denied denied --> post toolBody --> fsGate fsGate --> toolBody toolBody --> owned toolBody --> around around --> post - post --> context - post --> toolResult + post --> final + final --> toolResult toolResult --> presentResult + toolResult --> allResults + allResults --> context ``` -Filesystem read-before-edit checks live below `tool-fs` on the `fs/*` event gate; hook bridges and future permission prompts live on the generic pre/post tool waterfalls; and around-dispatch concerns like the tool-call timeout policy (`@deepseek-ai/dsh-timeout-policy`) wrap core dispatch on `tools/execute`. That split lets the same hooks observe bash, fs, web, todo, and subagent calls without coupling those tools to one policy service. Code Mode rides the same pipeline twice over: `run_code` is itself a registered tool body, and each tool call its program makes re-enters `ctx.tools.execute()` through BOTH waterfalls — serialized one at a time, logged as a `tool/code-dispatch` session event, with a deny surfacing to the program as a binding rejection (a sub-call's `additionalContext` is deliberately dropped — no safe outlet mid-run preserves call/result adjacency). +Filesystem read-before-edit checks live below `tool-fs` on the `fs/*` event gate; hook bridges and approval-triggering permission policy enter through the generic pre/post tool waterfalls, while `ctx.approval` resolves an `ask` before the monotonic guards; owner policy that must not be reordered uses registered guards; and around-dispatch concerns like the tool-call timeout policy (`@deepseek-ai/dsh-timeout-policy`) wrap core dispatch on `tools/execute`. The synchronous `tools/result` notification observes the immutable final outcome after every transform, lossless-JSON validation, and outer error normalization. That split lets the same hooks observe bash, fs, web, todo, skill, and subagent calls without coupling those tools to one policy service. Code Mode rides the whole pipeline twice over: `run_code` is the reserved registry-owned transport whose body enters the pipeline, and each tool call its program makes re-enters `ctx.tools.execute()` — serialized one at a time, carrying the outer execution's opaque token for correlation, and logged as a `tool/code-dispatch` session event, with a deny surfacing to the program as a binding rejection (a sub-call's `additionalContext` is deliberately dropped — no safe outlet mid-run preserves call/result adjacency). Maintenance mode: curated Mermaid flow; exact tool schemas and event signatures live in generated catalogs. diff --git a/eslint.config.mjs b/eslint.config.mjs index cbb696bccb..c62d3e9739 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -22,6 +22,7 @@ export default tseslint.config( '**/lib/**', '**/node_modules/**', '**/.sessions/**', + '.claude/**', // harness-local state (worktrees, skills) — other checkouts, not this one's sources '**/.doc-typecheck-*/**', 'vendor/**', // vendored source keeps upstream style and idioms '**/*.js', @@ -38,7 +39,14 @@ export default tseslint.config( ], languageOptions: { parserOptions: { - project: ['./packages/*/*/tsconfig.json', './tsconfig.json'], + // One shared tsserver-style project service instead of 60+ standalone + // per-package programs: the old `project` glob built every package's + // full dependency closure (sibling sources via the dev `paths` map + + // the vendored Cordis stack) as its own program and kept them all + // resident — ~5 GB peak, an OOM past node's default heap. The service + // resolves each file to its nearest owning tsconfig and shares the + // graph. + projectService: true, tsconfigRootDir: import.meta.dirname, }, }, @@ -89,7 +97,9 @@ export default tseslint.config( ], languageOptions: { parserOptions: { - project: ['./tsconfig.json'], + // Same shared project service as the src block: test files resolve + // through the root tsconfig (its include covers every tests/ tree). + projectService: true, tsconfigRootDir: import.meta.dirname, }, }, diff --git a/examples/AGENTS.md b/examples/AGENTS.md index 3d2a700ca0..d54104a189 100644 --- a/examples/AGENTS.md +++ b/examples/AGENTS.md @@ -2,7 +2,7 @@ Runnable demos showing how the harness is wired. **Examples are NOT workspaces** — each `examples/*/package.json` is a private, dependency-free stub, never built. They are booted as unbuilt `tsx` subprocesses via the cordis Loader reading a `cordis.yml`; the `@deepseek-ai/dsh-*` plugin names in those YAML files resolve through the root `tsconfig.json` `paths` map, not through `node_modules`. -Because examples are not under the `packages/*/src` coverage gate, an example that grows real, reusable *logic* should extract it into a `packages/` package (where it gets the per-file 100% gate and a README). Keep only example-specific glue here: the `cordis.yml` wiring, demo-only mocks/teaching artifacts, and the e2e/snapshot scenarios. There is no `start.ts` — the boot glue (Loader tail, `.env` load, snapshot-mode selection, stdin-dispose lifecycle) lives in each app package's `bin` (`@deepseek-ai/dsh-stdio-agent`, `@deepseek-ai/dsh-acp-agent`), which the `demo:*` scripts invoke against the leaf `cordis.yml`. +Because examples are not under the `packages/*/src` coverage gate, an example that grows real, reusable *logic* should extract it into a `packages/` package (where it gets the per-file 100% gate and a README). Keep only example-specific glue here: the `cordis.yml` wiring, demo-only mocks/teaching artifacts, and the e2e/snapshot scenarios. There is no `start.ts` — the boot glue lives in each app package's `bin` (`@deepseek-ai/dsh-stdio-agent`, `@deepseek-ai/dsh-acp-agent`), which the `demo:*` scripts invoke against the leaf `cordis.yml`. ## Every example ships e2e smokes (keyless + with-key) @@ -13,7 +13,7 @@ Each example must have **both** kinds of end-to-end smoke, because they catch di **Exception — keyless-by-nature examples.** An example whose model is itself a mock/deterministic stand-in (no real provider) has no meaningful with-key smoke; the keyless smoke is the complete requirement. State the exception inline in the test. -A keyless smoke that spawns the example from a temp cwd must set `TSX_TSCONFIG_PATH` to the repo-root tsconfig — the unbuilt `paths` map is found by searching UP from cwd, so a temp cwd outside the repo would otherwise fall back to stale built `lib/`. Pass `--expose-internals` when the example's `cordis.yml` loads the HMR plugin (mirror the `demo:*` script). +A keyless smoke that spawns the example from a temp cwd must set `TSX_TSCONFIG_PATH` to the repo-root tsconfig (the unbuilt `paths` map is found by searching UP from cwd), and pass `--expose-internals` when the `cordis.yml` loads the HMR plugin (mirror the `demo:*` script). ## Current state @@ -22,6 +22,7 @@ A keyless smoke that spawns the example from a temp cwd must set `TSX_TSCONFIG_P | `echo-agent` | `tests/echo.e2e.ts` — boots the real `cordis.yml`, drives the echo tool round-trip and the direct canned reply | **N/A — keyless by nature** (the `mock-echo` model has no real provider) | | `coding-agent` | `tests/keyless-smoke.e2e.ts` — boots the full real tree (dummy key, no prompt → no model call), asserts banner + clean exit; `tests/code-mode-keyless-smoke.e2e.ts` — the same boot guard for the Code Mode overlay | `tests/{full-loop,coding-task,resume,compaction,todo-write}.e2e.ts` — real model + real bash + real todo_write, world-verified; `tests/code-mode.e2e.ts` — a real model composes two bash calls in one `run_code` program; collapsed header, dispatch events, written file all verified | | `cordis-agent` | `tests/keyless-smoke.e2e.ts` — boots the real tree incl. `@deepseek-ai/dsh-tool-cordis` by package name; the tool logic is unit-tested in `packages/cordis/tool-cordis` | `tests/cordis-tools.e2e.ts` — real model mounts a listener (tagged line fires), builds+calls its own tool, composes two mounts via provide/inject | +| `sandbox-acp-agent` | `escalation.e2e.ts` — boots the real tree (sandbox + approval + bridge) keyless: initialize + `session/new` | same file — denied → escalates → a scripted client grants (the write must land) or rejects (it must not); skips without key/runner | | `acp-agent` | `pnpm run test:snapshot` — boots the real ACP subprocess and replays a recorded session keyless (incl. the hook matrix: a scenario per hook point × outcome for BOTH the Claude and Codex bridges — block, deny, ask, context-fold, force-continue); `tests/acp.e2e.ts` also asserts stdout purity without a key | `tests/acp.e2e.ts` — real ACP prompt, verifies a file the agent wrote; `tests/hooks.e2e.ts` — a real `PreToolUse` hook blocks bash, verifies the file is NOT written | See [the root AGENTS.md](../AGENTS.md) for repo-wide conventions and [docs/architecture.md](../docs/architecture.md) for the design. diff --git a/examples/README.md b/examples/README.md index e8a41a366b..7f711cb297 100644 --- a/examples/README.md +++ b/examples/README.md @@ -19,7 +19,7 @@ A REPL agent demo: DeepSeek V4 + the `read`/`write`/`edit` filesystem tools + th Run with: `pnpm run demo:repl` (needs `DEEPSEEK_API_KEY` in the environment or a gitignored repo-root `.env`). See [coding-agent/README.md](coding-agent/README.md) for details. -Its `code-mode.cordis.yml` overlay flips the same tree to **Code Mode**: the worker-thread code runtime is loaded and the tool registry runs `mode: code`, so the model gets exactly one wire tool — `run_code` — plus a generated TypeScript SDK section, and composes the other tools by writing a program whose output it curates. Run with: `pnpm run demo:code-mode` (the REPL is the default UI; `acp` as the argument serves the acp-agent example's same-shaped overlay instead) — see the [Code Mode section](coding-agent/README.md#code-mode) for what to try. +Its `code-mode.cordis.yml` overlay flips the same tree to **Code Mode**: the worker-thread code runtime is loaded and the tool registry runs `mode: code`, so its registry contribution is the reserved `run_code` transport plus a generated TypeScript SDK section, and the model composes the other tools by writing a program whose output it curates. Run with: `pnpm run demo:code-mode` (the REPL is the default UI; `acp` as the argument serves the acp-agent example's same-shaped overlay instead) — see the [Code Mode section](coding-agent/README.md#code-mode) for what to try. ## cordis-agent @@ -32,3 +32,9 @@ Run with: `pnpm run demo:cordis` (needs `DEEPSEEK_API_KEY`). See [cordis-agent/R An agent demo exposed as an **Agent Client Protocol (ACP)** server over JSON-RPC stdio, via the [`@deepseek-ai/dsh-acp-agent`](../packages/ui/acp-agent) app — drive it from Zed or any other ACP client. Also the home of the keyless snapshot tests. Run with: `pnpm run demo:acp` (needs `DEEPSEEK_API_KEY`); `pnpm run demo:code-mode acp` boots the same server in Code Mode via the `code-mode.cordis.yml` overlay. See [acp-agent/README.md](acp-agent/README.md) for the Zed setup and the snapshot-test design. + +## sandbox-acp-agent + +The coding agent with its bash executor swapped for the sandbox stack ([`@deepseek-ai/dsh-sandbox-local`](../packages/sandbox/sandbox-local) + [`@deepseek-ai/dsh-bash-sandbox`](../packages/bash/bash-sandbox) — the one-entry executor swap the `ctx.bash` capability seam exists for), served over ACP with [`@deepseek-ai/dsh-user-approval`](../packages/ui/user-approval) mounted — the first composition where the approval loop is LIVE: a sandbox denial escalated by the model becomes a `session/request_permission` prompt in the editor, and "Allow once" runs exactly that command under the wider mode. + +Run with: `pnpm run demo:sandbox-acp` (needs `DEEPSEEK_API_KEY`; bwrap, a Landlock-enforcing kernel, or macOS for confined runs). See [sandbox-acp-agent/README.md](sandbox-acp-agent/README.md). diff --git a/examples/acp-agent/README.md b/examples/acp-agent/README.md index 5b3c936651..e3d8542efd 100644 --- a/examples/acp-agent/README.md +++ b/examples/acp-agent/README.md @@ -33,8 +33,8 @@ The editor sets each session's `cwd` to the project it opens; both the agent's b ## Snapshot tests (record-once / replay-deterministic) -This example is the home of the harness's **snapshot tests** — they boot this server as a real subprocess, drive it with a deterministic input script, and diff its normalized output against committed golden files. The model is made deterministic by `@deepseek-ai/dsh-llm-replay`, a function/namespace plugin that installs an `llm/stream` waterfall listener and short-circuits it, serving model streams reconstructed from a recorded **session JSONL** fixture (`/session.jsonl`) — so replay needs no API key. The fixture IS the persisted session log: its `assistant/chunk` events carry every `StreamChunk`, so grouping them by `(turn, step)` reconstructs each `stream()` call (one model call per loop step). Recording is therefore "run the real agent once and harvest the `.jsonl`". The two failure modes not expressible as logged chunks — a pure throw before any chunk, and cancel/hang — use an optional `/replay.override.json` sidecar (a `ReplayEntry[]` that replaces the derived script). A scenario that needs the agent to operate on existing files ships an optional `/workspace/` directory — the harness copies its contents into the temp cwd before the run (see `workspace-edit`). See [the ACP snapshot tests RFC](../../docs/rfc/implemented/testing/2026-06-19-acp-snapshot-tests.md) for the full design. +This example is the home of the harness's **snapshot tests** — they boot this server as a real subprocess, drive it with a deterministic input script, and diff its normalized output against committed golden files. The model is made deterministic by `@deepseek-ai/dsh-llm-replay`, a function/namespace plugin that installs an `llm/stream` waterfall listener and short-circuits it, serving model streams reconstructed from a recorded **session JSONL** fixture (`/session.jsonl`) — so replay needs no API key. The fixture IS the persisted session log: its `assistant/chunk` events carry every `StreamChunk`, so grouping them by `(turn, step)` reconstructs each `stream()` call (one model call per loop step). Recording is therefore "run the real agent once and harvest the `.jsonl`"; use `pnpm run test:snapshot:record` when the model transcript itself should change, and `pnpm run test:snapshot:refresh` when the committed model transcript is still the right mock input and only the current replay output/goldens need to be rewritten. The two failure modes not expressible as logged chunks — a pure throw before any chunk, and cancel/hang — use an optional `/replay.override.json` sidecar (a `ReplayEntry[]` that replaces the derived script). A scenario that needs the agent to operate on existing files ships an optional `/workspace/` directory — the harness copies its contents into the temp cwd before the run (see `workspace-edit`). See [the ACP snapshot tests RFC](../../docs/rfc/implemented/testing/2026-06-19-acp-snapshot-tests.md) for the full design. ## MVP limitations -The bridge supports N concurrent sessions per connection, each in its own workspace `cwd` (RFC 011). Remaining limits: prompts support ACP's baseline `text` and `resource_link` blocks only, `additionalDirectories` and `mcpServers` are rejected, and the tool-permission gate is deferred (`TODO(rfc010-permission-gate)` — tools run with the executor's full authority). See `packages/ui/acp/README.md` for the full contract. +The bridge supports N concurrent sessions per connection, each in its own workspace `cwd` (RFC 011). Remaining limits: prompts support ACP's baseline `text` and `resource_link` blocks only, and `additionalDirectories` and `mcpServers` are rejected. Permission prompts (`session/request_permission`) are wired through the approval seam, but this example composes no ask-producing policy, so tools run with the executor's full authority. See `packages/ui/acp/README.md` for the full contract. diff --git a/examples/acp-agent/both-mode.cordis.snapshot.yml b/examples/acp-agent/both-mode.cordis.snapshot.yml index 67044b8066..8ee54b3078 100644 --- a/examples/acp-agent/both-mode.cordis.snapshot.yml +++ b/examples/acp-agent/both-mode.cordis.snapshot.yml @@ -20,11 +20,9 @@ tools: mode: both persona: | - You are a coding assistant powered by the {{model}} model. Your working - directory is {{cwd}}. + You are a coding assistant powered by the {{model}} model. Your working directory is {{cwd}}. - Verify your work by running the code or tests. Keep answers brief and - factual. + Verify your work by running the code or tests. Keep answers brief and factual. - insert: - id: code-runtime name: '@deepseek-ai/dsh-code-runtime-worker' diff --git a/examples/acp-agent/both-mode.cordis.yml b/examples/acp-agent/both-mode.cordis.yml index b449a568ec..3dff66d60a 100644 --- a/examples/acp-agent/both-mode.cordis.yml +++ b/examples/acp-agent/both-mode.cordis.yml @@ -19,11 +19,9 @@ tools: mode: both persona: | - You are a coding assistant powered by the {{model}} model. Your working - directory is {{cwd}}. + You are a coding assistant powered by the {{model}} model. Your working directory is {{cwd}}. - Verify your work by running the code or tests. Keep answers brief and - factual. + Verify your work by running the code or tests. Keep answers brief and factual. - insert: - id: code-runtime name: '@deepseek-ai/dsh-code-runtime-worker' diff --git a/examples/acp-agent/code-mode.cordis.snapshot.yml b/examples/acp-agent/code-mode.cordis.snapshot.yml index bcaa225eba..d525afc5d6 100644 --- a/examples/acp-agent/code-mode.cordis.snapshot.yml +++ b/examples/acp-agent/code-mode.cordis.snapshot.yml @@ -20,11 +20,9 @@ tools: mode: code persona: | - You are a coding assistant powered by the {{model}} model. Your working - directory is {{cwd}}. + You are a coding assistant powered by the {{model}} model. Your working directory is {{cwd}}. - Verify your work by running the code or tests. Keep answers brief and - factual. + Verify your work by running the code or tests. Keep answers brief and factual. - insert: - id: code-runtime name: '@deepseek-ai/dsh-code-runtime-worker' diff --git a/examples/acp-agent/code-mode.cordis.yml b/examples/acp-agent/code-mode.cordis.yml index d46e490494..323c35b5b4 100644 --- a/examples/acp-agent/code-mode.cordis.yml +++ b/examples/acp-agent/code-mode.cordis.yml @@ -20,11 +20,9 @@ tools: mode: code persona: | - You are a coding assistant powered by the {{model}} model. Your working - directory is {{cwd}}. + You are a coding assistant powered by the {{model}} model. Your working directory is {{cwd}}. - Verify your work by running the code or tests. Keep answers brief and - factual. + Verify your work by running the code or tests. Keep answers brief and factual. - insert: - id: code-runtime name: '@deepseek-ai/dsh-code-runtime-worker' diff --git a/examples/acp-agent/cordis.yml b/examples/acp-agent/cordis.yml index 0ff736dd59..dc03ea6b03 100644 --- a/examples/acp-agent/cordis.yml +++ b/examples/acp-agent/cordis.yml @@ -44,11 +44,9 @@ # loop resolves per session (every ACP session carries the client's cwd, # so the persona can state the workspace). persona: | - You are a coding assistant powered by the {{model}} model. Your working - directory is {{cwd}}. + You are a coding assistant powered by the {{model}} model. Your working directory is {{cwd}}. - Verify your work by running the code or tests. Keep answers brief and - factual. + Verify your work by running the code or tests. Keep answers brief and factual. # The subagent seam + both in-process backends + two model-facing tools, as leaf # entries after the app (which provides ctx.agents/ctx.tools). spawn (a fresh diff --git a/examples/acp-agent/tests/acp.e2e.ts b/examples/acp-agent/tests/acp.e2e.ts index 714791fa3e..8ff012c2c3 100644 --- a/examples/acp-agent/tests/acp.e2e.ts +++ b/examples/acp-agent/tests/acp.e2e.ts @@ -63,7 +63,16 @@ function spawnAcpAgent(cwd: string, env: NodeJS.ProcessEnv = process.env): Spawn const child = spawn( process.execPath, ['--import', tsxLoader, binScript, configPath], - { cwd, env: { ...env, TSX_TSCONFIG_PATH: repoTsconfig }, stdio: ['pipe', 'pipe', 'pipe'] }, + { + cwd, + env: { + ...env, + TSX_TSCONFIG_PATH: repoTsconfig, + DSH_HOME: join(cwd, '.dsh'), + DSH_AGENTS_HOME: join(cwd, '.agents'), + }, + stdio: ['pipe', 'pipe', 'pipe'], + }, ) const stderr: string[] = [] child.stderr.setEncoding('utf8') @@ -80,8 +89,9 @@ function spawnAcpAgent(cwd: string, env: NodeJS.ProcessEnv = process.env): Spawn return Promise.resolve() }, requestPermission(_params: RequestPermissionRequest): Promise { - // Permission gate is deferred (TODO(rfc010-permission-gate)); the bridge - // never requests permission yet, so just allow if it ever does. + // This example composes no ask-producing policy (no hooks), so the + // bridge never prompts here; answer cancelled (fail closed) if it ever + // does — an unexpected prompt must not grant anything. return Promise.resolve({ outcome: { outcome: 'cancelled' } }) }, }) @@ -150,7 +160,13 @@ describe('acp-agent over real stdio (no key required)', () => { // which this purity test never triggers). So this runs WITHOUT real creds. const child = spawn(process.execPath, ['--import', tsxLoader, binScript, configPath], { cwd: workdir, - env: { ...process.env, DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY ?? 'sk-dummy-for-boot', TSX_TSCONFIG_PATH: repoTsconfig }, + env: { + ...process.env, + DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY ?? 'sk-dummy-for-boot', + TSX_TSCONFIG_PATH: repoTsconfig, + DSH_HOME: join(workdir, '.dsh'), + DSH_AGENTS_HOME: join(workdir, '.agents'), + }, stdio: ['pipe', 'pipe', 'pipe'], }) const out: string[] = [] diff --git a/examples/acp-agent/tests/acp.snapshot.ts b/examples/acp-agent/tests/acp.snapshot.ts index d45ddf8591..eaa4e0381a 100644 --- a/examples/acp-agent/tests/acp.snapshot.ts +++ b/examples/acp-agent/tests/acp.snapshot.ts @@ -1,15 +1,16 @@ import { fileURLToPath } from 'node:url' import { dirname, join } from 'node:path' -import { defineAcpSnapshotSuite, type Scenario } from '@deepseek-ai/dsh-acp-snapshot' +import { defineAcpSnapshotSuite, type Scenario, type SnapshotSuiteOptions } from '@deepseek-ai/dsh-acp-snapshot' /** * The acp-agent example's snapshot suite: the scenario table for * `dsh-acp-snapshot`'s suite factory, which owns every compare/guard mechanic - * (golden + re-persisted-log diffs, record write-back, the pinned-header + * (golden + re-persisted-log diffs, record/refresh write-back, the pinned-header * uniformity guard, the fixture guards). Fixtures live under `snapshots//`; - * `pnpm run test:snapshot:record` re-records the `recorded` scenarios against - * the real API. See the package README (packages/support/acp-snapshot) and the - * snapshot RFC, docs/rfc/implemented/testing/2026-06-19-acp-snapshot-tests.md. + * `pnpm run test:snapshot:record` re-records model transcripts against the real + * API; `pnpm run test:snapshot:refresh` rewrites current replay goldens keyless. + * See the package README (packages/support/acp-snapshot) and the snapshot RFC, + * docs/rfc/implemented/testing/2026-06-19-acp-snapshot-tests.md. */ // The dsh-acp-agent bin (the demo:acp entry), this example's cordis.yml, and @@ -26,16 +27,31 @@ const AGENT = { const CODE_MODE_CONFIG = fileURLToPath(new URL('../code-mode.cordis.yml', import.meta.url)) const BOTH_MODE_CONFIG = fileURLToPath(new URL('../both-mode.cordis.yml', import.meta.url)) +function snapshotModeFromEnv(value: string | undefined): SnapshotSuiteOptions['mode'] { + switch (value) { + case undefined: + case '': + case 'replay': + return 'replay' + case 'record': + return 'record' + case 'refresh': + return 'refresh' + default: + throw new Error(`unknown DSH_SNAPSHOT mode: ${value}`) + } +} + const SCENARIOS: Scenario[] = [ { name: 'handshake', hasModelTurn: false, recorded: false }, { name: 'reject-extra-dirs', hasModelTurn: false, recorded: false }, - // text-turn is the pinned-header scenario: the minimal single text turn, - // whose fixture is the ONE place the full system prompt + tool schemas are - // committed and compared verbatim. + // text-turn is the pinned-header scenario: the minimal single text turn. + // Its system-prompt.golden.md and JSONL tool list pin the composed header. { name: 'text-turn', hasModelTurn: true, recorded: true, pinsHeader: true }, { name: 'tool-call-turn', hasModelTurn: true, recorded: true }, { name: 'fs-terminal-card', hasModelTurn: true, recorded: true }, { name: 'todo-plan', hasModelTurn: true, recorded: true }, + { name: 'skill-load', hasModelTurn: true, recorded: false, pinsHeader: true, headerClass: 'skill' }, { name: 'workspace-edit', hasModelTurn: true, recorded: true }, { name: 'fs-read', hasModelTurn: true, recorded: true }, { name: 'fs-write', hasModelTurn: true, recorded: true }, @@ -111,5 +127,5 @@ defineAcpSnapshotSuite({ agent: AGENT, snapshotsDir: join(dirname(fileURLToPath(import.meta.url)), 'snapshots'), scenarios: SCENARIOS, - mode: process.env.DSH_SNAPSHOT === 'record' ? 'record' : 'replay', + mode: snapshotModeFromEnv(process.env.DSH_SNAPSHOT), }) diff --git a/examples/acp-agent/tests/snapshots/both-mode-turn/session.jsonl b/examples/acp-agent/tests/snapshots/both-mode-turn/session.jsonl index df7187bbbb..b49097188e 100644 --- a/examples/acp-agent/tests/snapshots/both-mode-turn/session.jsonl +++ b/examples/acp-agent/tests/snapshots/both-mode-turn/session.jsonl @@ -2,7 +2,7 @@ {"type":"turn/start","seq":0,"time":1783611774323,"data":{"turn":1,"trigger":{"kind":"message","source":{"kind":"user"}}}} {"type":"user/message","seq":1,"time":1783611774323,"data":{"content":[{"type":"text","text":"Call the run_code tool (NOT the native bash tool directly) with a program that runs exactly `echo BOTH_OK` via tools.bash and returns its output. Then reply with that output only and stop."}],"source":{"kind":"user"}},"surfaceOp":"append"} {"type":"step/start","seq":2,"time":1783611774324,"data":{"turn":1,"step":1}} -{"type":"request/header","seq":3,"time":1783611774325,"data":{"header":{"config":{"model":"deepseek-v4-flash"},"system":"You are an AI agent powered by the DeepSeek Harness SDK.\n\nYou are a coding assistant powered by the deepseek-v4-flash model. Your working\ndirectory is /var/folders/2c/psb0_fmx7hbgz558xjt_f0l00000gn/T/acp-snap-cwd-52lrTl.\n\nVerify your work by running the code or tests. Keep answers brief and\nfactual.\n\n\nUse the read tool — not shell commands like cat — to inspect text files. Results include line numbers. Use offset and limit to continue reading large files.\n\nUse the write tool to create files or completely replace file contents. Existing files are overwritten, so read an existing file first (the default fs-policy requires it) and prefer edit for targeted changes.\n\nUse the edit tool for targeted changes to existing UTF-8 text files. It replaces literal old_string with new_string; by default old_string must appear exactly once. If old_string appears multiple times, provide a more specific old_string or set replace_all to true. Read the file first (the default fs-policy requires it), unless you just created or edited it in this session.\n\nCheck the [exit code: N] marker on every bash result; investigate failures before moving on.\n\nUse the workflow tool ONLY when the user explicitly asks for a workflow or for large multi-agent orchestration: you write a JavaScript script (the tool description documents the exact format) that fans work out across many subagents with phases and structured results. For one or two delegations, prefer plain subagent calls.\n\n## Writing code for run_code\n\nPass `run_code` the body of an async TypeScript function (erasable syntax only — no `enum` or namespaces; type annotations are advisory, the code runs type-stripped). Inside the program:\n\n- Call tools as `await tools.name(args)` — quoted access for exotic names: `tools[\"my-tool\"](args)`. Every call resolves to the tool's text output as a string. Tool arguments must be JSON-serializable.\n- A FAILED tool call rejects with an `Error` carrying the tool's error text — `try/catch` it to handle and continue.\n- Calls execute sequentially, even under `Promise.all`.\n- Emit results with `return` and/or `console.log(...)`. ONLY what you print or return comes back to you — intermediate tool results never enter the conversation, so extract just what you need.\n\nThe available tools:\n\n```ts\ndeclare const tools: {\n /** Execute a bash command (`bash -c`) and return its stdout/stderr. Each call runs in a fresh shell: no state (cwd, variables, functions) persists between calls — pass `workdir` instead of using `cd`. Non-zero exits are reported as `[exit code: N]`. Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. Set `run_in_background: true` for long-running commands: the call returns a task id immediately; poll it with `bash_output` and stop it with `bash_kill`. */\n bash(args: {\n /** The bash command to execute. */\n command: string;\n /** Clear, concise description of what this command does in active voice, 5-10 words (shown in the UI). Examples: \"ls\" → \"List files in current directory\"; \"git status\" → \"Show working tree status\"; \"npm install\" → \"Install package dependencies\". */\n description: string;\n /** Timeout in milliseconds. The executor applies its configured default and cap, and kills the command on expiry. */\n timeoutMs?: number;\n /** Working directory for this command. Defaults to the session workspace; a relative path is resolved against it. */\n workdir?: string;\n /** Run in the background and return a task id immediately. No timeout applies. */\n run_in_background?: boolean;\n }): Promise;\n /** Ask the executor to kill a running background bash task by task id. */\n bash_kill(args: {\n /** Task id returned by the bash tool. */\n task_id: string;\n }): Promise;\n /** Read new output from a background bash task started with `bash` + `run_in_background`. Returns only output produced since the previous bash_output call, plus the task status. Tasks keep running while you do other work; poll again later for more output. */\n bash_output(args: {\n /** Task id returned by the bash tool. */\n task_id: string;\n }): Promise;\n /** Edit an existing UTF-8 text file by replacing literal text. */\n edit(args: {\n /** Path to edit, resolved by the filesystem backend. */\n file_path: string;\n /** Literal text to replace. Must match exactly. */\n old_string: string;\n /** Literal replacement text. Use an empty string to delete the match. */\n new_string: string;\n /** Replace all matches. Defaults to false; when false, old_string must appear exactly once. */\n replace_all?: boolean;\n }): Promise;\n /** Read a UTF-8 text file and return line-numbered content. */\n read(args: {\n /** Path to read, resolved by the filesystem backend. */\n file_path: string;\n /** 1-based first line to return. Defaults to 1. */\n offset?: number;\n /** Maximum number of lines to return. Defaults to 2000. */\n limit?: number;\n }): Promise;\n /** Delegate a self-contained task to a subagent (a separate agent that works in its own context) and return its final result. Use this to offload focused, independent work — research, a scoped implementation, an analysis — so it does not consume this conversation's context. The subagent runs to completion and you receive only its final answer, not its intermediate steps. Give it a complete, standalone prompt: it does not see this conversation. */\n subagent(args: {\n /** A short (3-5 word) description of the delegated task, for display. */\n description: string;\n /** The complete, self-contained task for the subagent. It does not share this conversation's context, so include everything it needs. */\n prompt: string;\n }): Promise;\n /** Delegate a task to a subagent that INHERITS this conversation: a child agent seeded with all completed turns so far (it does not see the current in-flight turn), returning only its final result. Use this when the subtask builds on this conversation's context — a follow-up analysis, a review, a continuation — without consuming this conversation's context for the work itself. You receive only its final answer, not its intermediate steps. */\n subagent_fork(args: {\n /** A short (3-5 word) description of the delegated task, for display. */\n description: string;\n /** The task for the subagent. It already sees this conversation's completed turns, so build on them freely and state only what is new. */\n prompt: string;\n }): Promise;\n /** Record and update a structured task list for the current work. Send the ENTIRE list every call — it REPLACES the previous list (there are no partial updates, no per-item edits). Use it to plan multi-step work and show progress: add one todo per concrete step before you start. Keep AT MOST ONE todo `in_progress` at a time; while work remains, exactly one active task should be `in_progress`. Mark a todo `completed` the moment it is done (do not batch completions), and allow no `in_progress` item only once all work is complete. Skip the list for trivial single-step tasks. Statuses: `pending` (not started), `in_progress` (being worked on now), `completed` (finished). */\n todo_write(args: {\n /** The COMPLETE task list, replacing any previous list. */\n todos: ({\n /** What the task is — a short imperative line. */\n content: string;\n /** pending (not started) | in_progress (now) | completed (done). */\n status: \"pending\" | \"in_progress\" | \"completed\";\n })[];\n }): Promise;\n /** Run a JavaScript workflow script that orchestrates subagents at scale. Use this for work that fans out across many independent pieces — an audit over many files, a migration, multi-angle research, adversarial verification of findings — where you write the orchestration as a script instead of delegating turn by turn. The workflow's identity rides the `meta` parameter as JSON: required `name` (short kebab-case) and `description` strings, optional `whenToUse` string and `phases` array (`{title, detail?, model?}`). The `script` parameter is the plain JavaScript body ONLY (NOT TypeScript, and NO `export const meta` statement — meta is a parameter, not code), running with top-level await; end with `return ` — the value must be JSON-serializable and is this tool's result. Script-body hooks: - `agent(prompt, opts?): Promise` — run one subagent to completion. Without `opts.schema` it resolves to the child's final text; with `opts.schema` (an object-rooted JSON Schema using ONLY type/properties/required/additionalProperties/items/enum/const — no oneOf/pattern/format/numeric bounds) it resolves to the validated object. Resolves `null` when the child fails (filter with `.filter(Boolean)`). Other opts: `label` (display), `phase` (progress group), `model` (override). Anything else (`effort`/`isolation`/`agentType`) is rejected loudly. - `pipeline(items, ...stages): Promise` — run each item through the stages independently with NO barrier between stages (prefer this for multi-stage work). Each stage receives `(prev, item, index)`. An ordinary stage throw drops that ITEM to `null` and skips its remaining stages. - `parallel(thunks): Promise` — run zero-argument functions concurrently and await ALL of them (a barrier; use only when a stage genuinely needs every prior result together). A throwing thunk resolves to `null`. - `phase(title)` — start a progress phase; `log(message)` — narrate progress; `args` — the tool call's `args` input, verbatim. Misused hooks (bad arguments, unknown options, unsupported schemas, tripped caps) throw errors that ALWAYS kill the script — they never dissolve into a per-item `null`. Constraints: concurrency and total-agent caps apply; no filesystem, network, timers, or Node.js APIs are provided — the agents do the work, the script only coordinates them. The run executes in the foreground: this call returns when the whole script finishes. */\n workflow(args: {\n /** The plain-JS workflow script body (top-level await allowed; NO `export const meta` statement; end with `return `). */\n script: string;\n /** The workflow identity block (plain JSON — never code). */\n meta: {\n /** Short kebab-case workflow name. */\n name: string;\n /** One-line description of what the workflow does. */\n description: string;\n /** Optional guidance on when this workflow applies. */\n whenToUse?: string;\n /** Optional phase declarations matched by phase() calls. */\n phases?: {\n /** The phase title phase() calls match by exact string. */\n title: string;\n /** Optional one-line description of the phase. */\n detail?: string;\n /** Optional model override this phase is expected to use. */\n model?: string;\n }[];\n };\n /** Optional JSON input exposed to the script as the `args` global (wrap a bare list as a field, e.g. {\"files\": [...]}). */\n args?: Record;\n }): Promise;\n /** Create or fully replace a UTF-8 text file. */\n write(args: {\n /** Path to write, resolved by the filesystem backend. */\n file_path: string;\n /** Full UTF-8 text content to write. */\n content: string;\n }): Promise;\n}\n```","tools":[{"name":"bash","description":"Execute a bash command (`bash -c`) and return its stdout/stderr. Each call runs in a fresh shell: no state (cwd, variables, functions) persists between calls — pass `workdir` instead of using `cd`. Non-zero exits are reported as `[exit code: N]`. Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. Set `run_in_background: true` for long-running commands: the call returns a task id immediately; poll it with `bash_output` and stop it with `bash_kill`.","parameters":{"type":"object","properties":{"command":{"type":"string","description":"The bash command to execute."},"description":{"type":"string","description":"Clear, concise description of what this command does in active voice, 5-10 words (shown in the UI). Examples: \"ls\" → \"List files in current directory\"; \"git status\" → \"Show working tree status\"; \"npm install\" → \"Install package dependencies\"."},"timeoutMs":{"type":"number","description":"Timeout in milliseconds. The executor applies its configured default and cap, and kills the command on expiry."},"workdir":{"type":"string","description":"Working directory for this command. Defaults to the session workspace; a relative path is resolved against it."},"run_in_background":{"type":"boolean","description":"Run in the background and return a task id immediately. No timeout applies."}},"required":["command","description"]}},{"name":"bash_kill","description":"Ask the executor to kill a running background bash task by task id.","parameters":{"type":"object","properties":{"task_id":{"type":"string","description":"Task id returned by the bash tool."}},"required":["task_id"]}},{"name":"bash_output","description":"Read new output from a background bash task started with `bash` + `run_in_background`. Returns only output produced since the previous bash_output call, plus the task status. Tasks keep running while you do other work; poll again later for more output.","parameters":{"type":"object","properties":{"task_id":{"type":"string","description":"Task id returned by the bash tool."}},"required":["task_id"]}},{"name":"edit","description":"Edit an existing UTF-8 text file by replacing literal text.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to edit, resolved by the filesystem backend."},"old_string":{"type":"string","description":"Literal text to replace. Must match exactly."},"new_string":{"type":"string","description":"Literal replacement text. Use an empty string to delete the match."},"replace_all":{"type":"boolean","description":"Replace all matches. Defaults to false; when false, old_string must appear exactly once."}},"required":["file_path","old_string","new_string"]}},{"name":"read","description":"Read a UTF-8 text file and return line-numbered content.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to read, resolved by the filesystem backend."},"offset":{"type":"number","description":"1-based first line to return. Defaults to 1."},"limit":{"type":"number","description":"Maximum number of lines to return. Defaults to 2000."}},"required":["file_path"]}},{"name":"run_code","description":"Execute a TypeScript program against the available tools. Write the BODY of an async function (erasable syntax only; top-level `await` and `return` work) and call tools as `await tools.name(args)` per the declarations in the system prompt. Only what you print or return comes back — curate it.","parameters":{"type":"object","properties":{"code":{"type":"string","description":"The program: the body of an async TypeScript function."}},"required":["code"]}},{"name":"subagent","description":"Delegate a self-contained task to a subagent (a separate agent that works in its own context) and return its final result. Use this to offload focused, independent work — research, a scoped implementation, an analysis — so it does not consume this conversation's context. The subagent runs to completion and you receive only its final answer, not its intermediate steps. Give it a complete, standalone prompt: it does not see this conversation.","parameters":{"type":"object","properties":{"description":{"type":"string","description":"A short (3-5 word) description of the delegated task, for display."},"prompt":{"type":"string","description":"The complete, self-contained task for the subagent. It does not share this conversation's context, so include everything it needs."}},"required":["description","prompt"]}},{"name":"subagent_fork","description":"Delegate a task to a subagent that INHERITS this conversation: a child agent seeded with all completed turns so far (it does not see the current in-flight turn), returning only its final result. Use this when the subtask builds on this conversation's context — a follow-up analysis, a review, a continuation — without consuming this conversation's context for the work itself. You receive only its final answer, not its intermediate steps.","parameters":{"type":"object","properties":{"description":{"type":"string","description":"A short (3-5 word) description of the delegated task, for display."},"prompt":{"type":"string","description":"The task for the subagent. It already sees this conversation's completed turns, so build on them freely and state only what is new."}},"required":["description","prompt"]}},{"name":"todo_write","description":"Record and update a structured task list for the current work. Send the ENTIRE list every call — it REPLACES the previous list (there are no partial updates, no per-item edits). Use it to plan multi-step work and show progress: add one todo per concrete step before you start. Keep AT MOST ONE todo `in_progress` at a time; while work remains, exactly one active task should be `in_progress`. Mark a todo `completed` the moment it is done (do not batch completions), and allow no `in_progress` item only once all work is complete. Skip the list for trivial single-step tasks. Statuses: `pending` (not started), `in_progress` (being worked on now), `completed` (finished).","parameters":{"type":"object","properties":{"todos":{"type":"array","description":"The COMPLETE task list, replacing any previous list.","items":{"type":"object","properties":{"content":{"type":"string","description":"What the task is — a short imperative line."},"status":{"type":"string","description":"pending (not started) | in_progress (now) | completed (done).","enum":["pending","in_progress","completed"]}},"required":["content","status"]}}},"required":["todos"]}},{"name":"workflow","description":"Run a JavaScript workflow script that orchestrates subagents at scale. Use this for work that fans out across many independent pieces — an audit over many files, a migration, multi-angle research, adversarial verification of findings — where you write the orchestration as a script instead of delegating turn by turn.\n\nThe workflow's identity rides the `meta` parameter as JSON: required `name` (short kebab-case) and `description` strings, optional `whenToUse` string and `phases` array (`{title, detail?, model?}`). The `script` parameter is the plain JavaScript body ONLY (NOT TypeScript, and NO `export const meta` statement — meta is a parameter, not code), running with top-level await; end with `return ` — the value must be JSON-serializable and is this tool's result.\n\nScript-body hooks:\n- `agent(prompt, opts?): Promise` — run one subagent to completion. Without `opts.schema` it resolves to the child's final text; with `opts.schema` (an object-rooted JSON Schema using ONLY type/properties/required/additionalProperties/items/enum/const — no oneOf/pattern/format/numeric bounds) it resolves to the validated object. Resolves `null` when the child fails (filter with `.filter(Boolean)`). Other opts: `label` (display), `phase` (progress group), `model` (override). Anything else (`effort`/`isolation`/`agentType`) is rejected loudly.\n- `pipeline(items, ...stages): Promise` — run each item through the stages independently with NO barrier between stages (prefer this for multi-stage work). Each stage receives `(prev, item, index)`. An ordinary stage throw drops that ITEM to `null` and skips its remaining stages.\n- `parallel(thunks): Promise` — run zero-argument functions concurrently and await ALL of them (a barrier; use only when a stage genuinely needs every prior result together). A throwing thunk resolves to `null`.\n- `phase(title)` — start a progress phase; `log(message)` — narrate progress; `args` — the tool call's `args` input, verbatim.\n\nMisused hooks (bad arguments, unknown options, unsupported schemas, tripped caps) throw errors that ALWAYS kill the script — they never dissolve into a per-item `null`.\n\nConstraints: concurrency and total-agent caps apply; no filesystem, network, timers, or Node.js APIs are provided — the agents do the work, the script only coordinates them. The run executes in the foreground: this call returns when the whole script finishes.","parameters":{"type":"object","properties":{"script":{"type":"string","description":"The plain-JS workflow script body (top-level await allowed; NO `export const meta` statement; end with `return `)."},"meta":{"type":"object","description":"The workflow identity block (plain JSON — never code).","properties":{"name":{"type":"string","description":"Short kebab-case workflow name."},"description":{"type":"string","description":"One-line description of what the workflow does."},"whenToUse":{"type":"string","description":"Optional guidance on when this workflow applies."},"phases":{"type":"array","description":"Optional phase declarations matched by phase() calls.","items":{"type":"object","properties":{"title":{"type":"string","description":"The phase title phase() calls match by exact string."},"detail":{"type":"string","description":"Optional one-line description of the phase."},"model":{"type":"string","description":"Optional model override this phase is expected to use."}},"required":["title"]}}},"required":["name","description"]},"args":{"type":"object","description":"Optional JSON input exposed to the script as the `args` global (wrap a bare list as a field, e.g. {\"files\": [...]})."}},"required":["script","meta"]}},{"name":"write","description":"Create or fully replace a UTF-8 text file.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to write, resolved by the filesystem backend."},"content":{"type":"string","description":"Full UTF-8 text content to write."}},"required":["file_path","content"]}}]},"reason":"initial"}} +{"type":"request/header","seq":3,"time":1783611774325,"data":{"header":{"config":{"model":"deepseek-v4-flash"},"system":"{{system}}","tools":[{"name":"bash","description":"Execute a bash command (`bash -c`) and return its stdout/stderr. Each call runs in a fresh shell: no state (cwd, variables, functions) persists between calls — pass `workdir` instead of using `cd`. Non-zero exits are reported as `[exit code: N]`. Commands may run under a file sandbox; a blocked file operation is reported as `[sandbox: file access denied under mode]` — a policy denial, not a bug in the command; do not retry another way (a background task reports the same marker via bash_output once it has finished). Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. Set `run_in_background: true` for long-running commands: the call returns a task id immediately; poll it with `bash_output` and stop it with `bash_kill`.","parameters":{"type":"object","properties":{"command":{"type":"string","description":"The bash command to execute."},"description":{"type":"string","description":"Clear, concise description of what this command does in active voice, 5-10 words (shown in the UI). Examples: \"ls\" → \"List files in current directory\"; \"git status\" → \"Show working tree status\"; \"npm install\" → \"Install package dependencies\"."},"timeoutMs":{"type":"number","description":"Timeout in milliseconds. The executor applies its configured default and cap, and kills the command on expiry."},"workdir":{"type":"string","description":"Working directory for this command. Defaults to the session workspace; a relative path is resolved against it."},"run_in_background":{"type":"boolean","description":"Run in the background and return a task id immediately. No timeout applies."}},"required":["command","description"]}},{"name":"bash_kill","description":"Ask the executor to kill a running background bash task by task id.","parameters":{"type":"object","properties":{"task_id":{"type":"string","description":"Task id returned by the bash tool."}},"required":["task_id"]}},{"name":"bash_output","description":"Read new output from a background bash task started with `bash` + `run_in_background`. Returns only output produced since the previous bash_output call, plus the task status. Tasks keep running while you do other work; poll again later for more output.","parameters":{"type":"object","properties":{"task_id":{"type":"string","description":"Task id returned by the bash tool."}},"required":["task_id"]}},{"name":"edit","description":"Edit an existing UTF-8 text file by replacing literal text.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to edit, resolved by the filesystem backend."},"old_string":{"type":"string","description":"Literal text to replace. Must match exactly."},"new_string":{"type":"string","description":"Literal replacement text. Use an empty string to delete the match."},"replace_all":{"type":"boolean","description":"Replace all matches. Defaults to false; when false, old_string must appear exactly once."}},"required":["file_path","old_string","new_string"]}},{"name":"read","description":"Read a UTF-8 text file and return line-numbered content.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to read, resolved by the filesystem backend."},"offset":{"type":"number","description":"1-based first line to return. Defaults to 1."},"limit":{"type":"number","description":"Maximum number of lines to return. Defaults to 2000."}},"required":["file_path"]}},{"name":"run_code","description":"Execute a TypeScript program against the available tools. Write the BODY of an async function (erasable syntax only; top-level `await` and `return` work) and call tools as `await tools.name(args)` per the declarations in the system prompt. Only what you print or return comes back — curate it.","parameters":{"type":"object","properties":{"code":{"type":"string","description":"The program: the body of an async TypeScript function."}},"required":["code"]}},{"name":"skill","description":"Load the full instructions for an available skill. Call this with the exact skill name from the session skill catalog before acting on a task that names or clearly matches that skill.","parameters":{"type":"object","properties":{"name":{"type":"string","description":"The exact skill name from the available skills list."}},"required":["name"]}},{"name":"subagent","description":"Delegate a self-contained task to a subagent (a separate agent that works in its own context) and return its final result. Use this to offload focused, independent work — research, a scoped implementation, an analysis — so it does not consume this conversation's context. The subagent runs to completion and you receive only its final answer, not its intermediate steps. Give it a complete, standalone prompt: it does not see this conversation.","parameters":{"type":"object","properties":{"description":{"type":"string","description":"A short (3-5 word) description of the delegated task, for display."},"prompt":{"type":"string","description":"The complete, self-contained task for the subagent. It does not share this conversation's context, so include everything it needs."}},"required":["description","prompt"]}},{"name":"subagent_fork","description":"Delegate a task to a subagent that INHERITS this conversation: a child agent seeded with all completed turns so far (it does not see the current in-flight turn), returning only its final result. Use this when the subtask builds on this conversation's context — a follow-up analysis, a review, a continuation — without consuming this conversation's context for the work itself. You receive only its final answer, not its intermediate steps.","parameters":{"type":"object","properties":{"description":{"type":"string","description":"A short (3-5 word) description of the delegated task, for display."},"prompt":{"type":"string","description":"The task for the subagent. It already sees this conversation's completed turns, so build on them freely and state only what is new."}},"required":["description","prompt"]}},{"name":"todo_write","description":"Record and update a structured task list for the current work. Send the ENTIRE list every call — it REPLACES the previous list (there are no partial updates, no per-item edits). Use it to plan multi-step work and show progress: add one todo per concrete step before you start. Keep AT MOST ONE todo `in_progress` at a time; while work remains, exactly one active task should be `in_progress`. Mark a todo `completed` the moment it is done (do not batch completions), and allow no `in_progress` item only once all work is complete. Skip the list for trivial single-step tasks. Statuses: `pending` (not started), `in_progress` (being worked on now), `completed` (finished).","parameters":{"type":"object","properties":{"todos":{"type":"array","description":"The COMPLETE task list, replacing any previous list.","items":{"type":"object","properties":{"content":{"type":"string","description":"What the task is — a short imperative line."},"status":{"type":"string","description":"pending (not started) | in_progress (now) | completed (done).","enum":["pending","in_progress","completed"]}},"required":["content","status"]}}},"required":["todos"]}},{"name":"workflow","description":"Run a JavaScript workflow script that orchestrates subagents at scale. Use this for work that fans out across many independent pieces — an audit over many files, a migration, multi-angle research, adversarial verification of findings — where you write the orchestration as a script instead of delegating turn by turn.\n\nThe workflow's identity rides the `meta` parameter as JSON: required `name` (short kebab-case) and `description` strings, optional `whenToUse` string and `phases` array (`{title, detail?, model?}`). The `script` parameter is the plain JavaScript body ONLY (NOT TypeScript, and NO `export const meta` statement — meta is a parameter, not code), running with top-level await; end with `return ` — the value must be JSON-serializable and is this tool's result.\n\nScript-body hooks:\n- `agent(prompt, opts?): Promise` — run one subagent to completion. Without `opts.schema` it resolves to the child's final text; with `opts.schema` (an object-rooted JSON Schema using ONLY type/properties/required/additionalProperties/items/enum/const — no oneOf/pattern/format/numeric bounds) it resolves to the validated object. Resolves `null` when the child fails (filter with `.filter(Boolean)`). Other opts: `label` (display), `phase` (progress group), `model` (override). Anything else (`effort`/`isolation`/`agentType`) is rejected loudly.\n- `pipeline(items, ...stages): Promise` — run each item through the stages independently with NO barrier between stages (prefer this for multi-stage work). Each stage receives `(prev, item, index)`. An ordinary stage throw drops that ITEM to `null` and skips its remaining stages.\n- `parallel(thunks): Promise` — run zero-argument functions concurrently and await ALL of them (a barrier; use only when a stage genuinely needs every prior result together). A throwing thunk resolves to `null`.\n- `phase(title)` — start a progress phase; `log(message)` — narrate progress; `args` — the tool call's `args` input, verbatim.\n\nMisused hooks (bad arguments, unknown options, unsupported schemas, tripped caps) throw errors that ALWAYS kill the script — they never dissolve into a per-item `null`.\n\nConstraints: concurrency and total-agent caps apply; no filesystem, network, timers, or Node.js APIs are provided — the agents do the work, the script only coordinates them. The run executes in the foreground: this call returns when the whole script finishes.","parameters":{"type":"object","properties":{"script":{"type":"string","description":"The plain-JS workflow script body (top-level await allowed; NO `export const meta` statement; end with `return `)."},"meta":{"type":"object","description":"The workflow identity block (plain JSON — never code).","properties":{"name":{"type":"string","description":"Short kebab-case workflow name."},"description":{"type":"string","description":"One-line description of what the workflow does."},"whenToUse":{"type":"string","description":"Optional guidance on when this workflow applies."},"phases":{"type":"array","description":"Optional phase declarations matched by phase() calls.","items":{"type":"object","properties":{"title":{"type":"string","description":"The phase title phase() calls match by exact string."},"detail":{"type":"string","description":"Optional one-line description of the phase."},"model":{"type":"string","description":"Optional model override this phase is expected to use."}},"required":["title"]}}},"required":["name","description"]},"args":{"type":"object","description":"Optional JSON input exposed to the script as the `args` global (wrap a bare list as a field, e.g. {\"files\": [...]})."}},"required":["script","meta"]}},{"name":"write","description":"Create or fully replace a UTF-8 text file.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to write, resolved by the filesystem backend."},"content":{"type":"string","description":"Full UTF-8 text content to write."}},"required":["file_path","content"]}}]},"reason":"initial"}} {"type":"assistant/chunk","seq":4,"time":1783611774792,"data":{"turn":1,"step":1,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}} {"type":"assistant/chunk","seq":5,"time":1783611774792,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"The"}}} {"type":"assistant/chunk","seq":6,"time":1783611774879,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" user"}}} diff --git a/examples/acp-agent/tests/snapshots/both-mode-turn/system-prompt.golden.md b/examples/acp-agent/tests/snapshots/both-mode-turn/system-prompt.golden.md new file mode 100644 index 0000000000..5dd8547aa8 --- /dev/null +++ b/examples/acp-agent/tests/snapshots/both-mode-turn/system-prompt.golden.md @@ -0,0 +1,136 @@ +You are an AI agent powered by the DeepSeek Harness SDK. + +You are a coding assistant powered by the deepseek-v4-flash model. Your working directory is {{cwd}}. + +Verify your work by running the code or tests. Keep answers brief and factual. + + +Use the read tool — not shell commands like cat — to inspect text files. Results include line numbers. Use offset and limit to continue reading large files. + +Use the write tool to create files or completely replace file contents. Existing files are overwritten, so read an existing file first (the default fs-policy requires it) and prefer edit for targeted changes. + +Use the edit tool for targeted changes to existing UTF-8 text files. It replaces literal old_string with new_string; by default old_string must appear exactly once. If old_string appears multiple times, provide a more specific old_string or set replace_all to true. Read the file first (the default fs-policy requires it), unless you just created or edited it in this session. + +Check the [exit code: N] marker on every bash result; investigate failures before moving on. + +Use the workflow tool ONLY when the user explicitly asks for a workflow or for large multi-agent orchestration: you write a JavaScript script (the tool description documents the exact format) that fans work out across many subagents with phases and structured results. For one or two delegations, prefer plain subagent calls. + +## Writing code for run_code + +Pass `run_code` the body of an async TypeScript function (erasable syntax only — no `enum` or namespaces; type annotations are advisory, the code runs type-stripped). Inside the program: + +- Call tools as `await tools.name(args)` — quoted access for exotic names: `tools["my-tool"](args)`. Every call resolves to the tool's text output as a string. Tool arguments must be JSON-serializable. +- A FAILED tool call rejects with an `Error` carrying the tool's error text — `try/catch` it to handle and continue. +- Calls execute sequentially, even under `Promise.all`. +- Emit results with `return` and/or `console.log(...)`. ONLY what you print or return comes back to you — intermediate tool results never enter the conversation, so extract just what you need. + +The available tools: + +```ts +declare const tools: { + /** Execute a bash command (`bash -c`) and return its stdout/stderr. Each call runs in a fresh shell: no state (cwd, variables, functions) persists between calls — pass `workdir` instead of using `cd`. Non-zero exits are reported as `[exit code: N]`. Commands may run under a file sandbox; a blocked file operation is reported as `[sandbox: file access denied under mode]` — a policy denial, not a bug in the command; do not retry another way (a background task reports the same marker via bash_output once it has finished). Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. Set `run_in_background: true` for long-running commands: the call returns a task id immediately; poll it with `bash_output` and stop it with `bash_kill`. */ + bash(args: { + /** The bash command to execute. */ + command: string; + /** Clear, concise description of what this command does in active voice, 5-10 words (shown in the UI). Examples: "ls" → "List files in current directory"; "git status" → "Show working tree status"; "npm install" → "Install package dependencies". */ + description: string; + /** Timeout in milliseconds. The executor applies its configured default and cap, and kills the command on expiry. */ + timeoutMs?: number; + /** Working directory for this command. Defaults to the session workspace; a relative path is resolved against it. */ + workdir?: string; + /** Run in the background and return a task id immediately. No timeout applies. */ + run_in_background?: boolean; + }): Promise; + /** Ask the executor to kill a running background bash task by task id. */ + bash_kill(args: { + /** Task id returned by the bash tool. */ + task_id: string; + }): Promise; + /** Read new output from a background bash task started with `bash` + `run_in_background`. Returns only output produced since the previous bash_output call, plus the task status. Tasks keep running while you do other work; poll again later for more output. */ + bash_output(args: { + /** Task id returned by the bash tool. */ + task_id: string; + }): Promise; + /** Edit an existing UTF-8 text file by replacing literal text. */ + edit(args: { + /** Path to edit, resolved by the filesystem backend. */ + file_path: string; + /** Literal text to replace. Must match exactly. */ + old_string: string; + /** Literal replacement text. Use an empty string to delete the match. */ + new_string: string; + /** Replace all matches. Defaults to false; when false, old_string must appear exactly once. */ + replace_all?: boolean; + }): Promise; + /** Read a UTF-8 text file and return line-numbered content. */ + read(args: { + /** Path to read, resolved by the filesystem backend. */ + file_path: string; + /** 1-based first line to return. Defaults to 1. */ + offset?: number; + /** Maximum number of lines to return. Defaults to 2000. */ + limit?: number; + }): Promise; + /** Load the full instructions for an available skill. Call this with the exact skill name from the session skill catalog before acting on a task that names or clearly matches that skill. */ + skill(args: { + /** The exact skill name from the available skills list. */ + name: string; + }): Promise; + /** Delegate a self-contained task to a subagent (a separate agent that works in its own context) and return its final result. Use this to offload focused, independent work — research, a scoped implementation, an analysis — so it does not consume this conversation's context. The subagent runs to completion and you receive only its final answer, not its intermediate steps. Give it a complete, standalone prompt: it does not see this conversation. */ + subagent(args: { + /** A short (3-5 word) description of the delegated task, for display. */ + description: string; + /** The complete, self-contained task for the subagent. It does not share this conversation's context, so include everything it needs. */ + prompt: string; + }): Promise; + /** Delegate a task to a subagent that INHERITS this conversation: a child agent seeded with all completed turns so far (it does not see the current in-flight turn), returning only its final result. Use this when the subtask builds on this conversation's context — a follow-up analysis, a review, a continuation — without consuming this conversation's context for the work itself. You receive only its final answer, not its intermediate steps. */ + subagent_fork(args: { + /** A short (3-5 word) description of the delegated task, for display. */ + description: string; + /** The task for the subagent. It already sees this conversation's completed turns, so build on them freely and state only what is new. */ + prompt: string; + }): Promise; + /** Record and update a structured task list for the current work. Send the ENTIRE list every call — it REPLACES the previous list (there are no partial updates, no per-item edits). Use it to plan multi-step work and show progress: add one todo per concrete step before you start. Keep AT MOST ONE todo `in_progress` at a time; while work remains, exactly one active task should be `in_progress`. Mark a todo `completed` the moment it is done (do not batch completions), and allow no `in_progress` item only once all work is complete. Skip the list for trivial single-step tasks. Statuses: `pending` (not started), `in_progress` (being worked on now), `completed` (finished). */ + todo_write(args: { + /** The COMPLETE task list, replacing any previous list. */ + todos: ({ + /** What the task is — a short imperative line. */ + content: string; + /** pending (not started) | in_progress (now) | completed (done). */ + status: "pending" | "in_progress" | "completed"; + })[]; + }): Promise; + /** Run a JavaScript workflow script that orchestrates subagents at scale. Use this for work that fans out across many independent pieces — an audit over many files, a migration, multi-angle research, adversarial verification of findings — where you write the orchestration as a script instead of delegating turn by turn. The workflow's identity rides the `meta` parameter as JSON: required `name` (short kebab-case) and `description` strings, optional `whenToUse` string and `phases` array (`{title, detail?, model?}`). The `script` parameter is the plain JavaScript body ONLY (NOT TypeScript, and NO `export const meta` statement — meta is a parameter, not code), running with top-level await; end with `return ` — the value must be JSON-serializable and is this tool's result. Script-body hooks: - `agent(prompt, opts?): Promise` — run one subagent to completion. Without `opts.schema` it resolves to the child's final text; with `opts.schema` (an object-rooted JSON Schema using ONLY type/properties/required/additionalProperties/items/enum/const — no oneOf/pattern/format/numeric bounds) it resolves to the validated object. Resolves `null` when the child fails (filter with `.filter(Boolean)`). Other opts: `label` (display), `phase` (progress group), `model` (override). Anything else (`effort`/`isolation`/`agentType`) is rejected loudly. - `pipeline(items, ...stages): Promise` — run each item through the stages independently with NO barrier between stages (prefer this for multi-stage work). Each stage receives `(prev, item, index)`. An ordinary stage throw drops that ITEM to `null` and skips its remaining stages. - `parallel(thunks): Promise` — run zero-argument functions concurrently and await ALL of them (a barrier; use only when a stage genuinely needs every prior result together). A throwing thunk resolves to `null`. - `phase(title)` — start a progress phase; `log(message)` — narrate progress; `args` — the tool call's `args` input, verbatim. Misused hooks (bad arguments, unknown options, unsupported schemas, tripped caps) throw errors that ALWAYS kill the script — they never dissolve into a per-item `null`. Constraints: concurrency and total-agent caps apply; no filesystem, network, timers, or Node.js APIs are provided — the agents do the work, the script only coordinates them. The run executes in the foreground: this call returns when the whole script finishes. */ + workflow(args: { + /** The plain-JS workflow script body (top-level await allowed; NO `export const meta` statement; end with `return `). */ + script: string; + /** The workflow identity block (plain JSON — never code). */ + meta: { + /** Short kebab-case workflow name. */ + name: string; + /** One-line description of what the workflow does. */ + description: string; + /** Optional guidance on when this workflow applies. */ + whenToUse?: string; + /** Optional phase declarations matched by phase() calls. */ + phases?: { + /** The phase title phase() calls match by exact string. */ + title: string; + /** Optional one-line description of the phase. */ + detail?: string; + /** Optional model override this phase is expected to use. */ + model?: string; + }[]; + }; + /** Optional JSON input exposed to the script as the `args` global (wrap a bare list as a field, e.g. {"files": [...]}). */ + args?: Record; + }): Promise; + /** Create or fully replace a UTF-8 text file. */ + write(args: { + /** Path to write, resolved by the filesystem backend. */ + file_path: string; + /** Full UTF-8 text content to write. */ + content: string; + }): Promise; +} +``` diff --git a/examples/acp-agent/tests/snapshots/code-mode-turn/session.jsonl b/examples/acp-agent/tests/snapshots/code-mode-turn/session.jsonl index 72533bcdeb..9367e2deb0 100644 --- a/examples/acp-agent/tests/snapshots/code-mode-turn/session.jsonl +++ b/examples/acp-agent/tests/snapshots/code-mode-turn/session.jsonl @@ -2,7 +2,7 @@ {"type":"turn/start","seq":0,"time":1783611771394,"data":{"turn":1,"trigger":{"kind":"message","source":{"kind":"user"}}}} {"type":"user/message","seq":1,"time":1783611771394,"data":{"content":[{"type":"text","text":"Using ONE run_code program: call the bash tool twice — exactly `echo CODE_ONE` then exactly `echo CODE_TWO` — and return the two outputs joined with a plus sign. Then reply with that joined string only and stop."}],"source":{"kind":"user"}},"surfaceOp":"append"} {"type":"step/start","seq":2,"time":1783611771396,"data":{"turn":1,"step":1}} -{"type":"request/header","seq":3,"time":1783611771396,"data":{"header":{"config":{"model":"deepseek-v4-flash"},"system":"You are an AI agent powered by the DeepSeek Harness SDK.\n\nYou are a coding assistant powered by the deepseek-v4-flash model. Your working\ndirectory is /var/folders/2c/psb0_fmx7hbgz558xjt_f0l00000gn/T/acp-snap-cwd-BteTVR.\n\nVerify your work by running the code or tests. Keep answers brief and\nfactual.\n\n\nUse the read tool — not shell commands like cat — to inspect text files. Results include line numbers. Use offset and limit to continue reading large files.\n\nUse the write tool to create files or completely replace file contents. Existing files are overwritten, so read an existing file first (the default fs-policy requires it) and prefer edit for targeted changes.\n\nUse the edit tool for targeted changes to existing UTF-8 text files. It replaces literal old_string with new_string; by default old_string must appear exactly once. If old_string appears multiple times, provide a more specific old_string or set replace_all to true. Read the file first (the default fs-policy requires it), unless you just created or edited it in this session.\n\nCheck the [exit code: N] marker on every bash result; investigate failures before moving on.\n\nUse the workflow tool ONLY when the user explicitly asks for a workflow or for large multi-agent orchestration: you write a JavaScript script (the tool description documents the exact format) that fans work out across many subagents with phases and structured results. For one or two delegations, prefer plain subagent calls.\n\n## Writing code for run_code\n\nPass `run_code` the body of an async TypeScript function (erasable syntax only — no `enum` or namespaces; type annotations are advisory, the code runs type-stripped). Inside the program:\n\n- Call tools as `await tools.name(args)` — quoted access for exotic names: `tools[\"my-tool\"](args)`. Every call resolves to the tool's text output as a string. Tool arguments must be JSON-serializable.\n- A FAILED tool call rejects with an `Error` carrying the tool's error text — `try/catch` it to handle and continue.\n- Calls execute sequentially, even under `Promise.all`.\n- Emit results with `return` and/or `console.log(...)`. ONLY what you print or return comes back to you — intermediate tool results never enter the conversation, so extract just what you need.\n\nThe available tools:\n\n```ts\ndeclare const tools: {\n /** Execute a bash command (`bash -c`) and return its stdout/stderr. Each call runs in a fresh shell: no state (cwd, variables, functions) persists between calls — pass `workdir` instead of using `cd`. Non-zero exits are reported as `[exit code: N]`. Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. Set `run_in_background: true` for long-running commands: the call returns a task id immediately; poll it with `bash_output` and stop it with `bash_kill`. */\n bash(args: {\n /** The bash command to execute. */\n command: string;\n /** Clear, concise description of what this command does in active voice, 5-10 words (shown in the UI). Examples: \"ls\" → \"List files in current directory\"; \"git status\" → \"Show working tree status\"; \"npm install\" → \"Install package dependencies\". */\n description: string;\n /** Timeout in milliseconds. The executor applies its configured default and cap, and kills the command on expiry. */\n timeoutMs?: number;\n /** Working directory for this command. Defaults to the session workspace; a relative path is resolved against it. */\n workdir?: string;\n /** Run in the background and return a task id immediately. No timeout applies. */\n run_in_background?: boolean;\n }): Promise;\n /** Ask the executor to kill a running background bash task by task id. */\n bash_kill(args: {\n /** Task id returned by the bash tool. */\n task_id: string;\n }): Promise;\n /** Read new output from a background bash task started with `bash` + `run_in_background`. Returns only output produced since the previous bash_output call, plus the task status. Tasks keep running while you do other work; poll again later for more output. */\n bash_output(args: {\n /** Task id returned by the bash tool. */\n task_id: string;\n }): Promise;\n /** Edit an existing UTF-8 text file by replacing literal text. */\n edit(args: {\n /** Path to edit, resolved by the filesystem backend. */\n file_path: string;\n /** Literal text to replace. Must match exactly. */\n old_string: string;\n /** Literal replacement text. Use an empty string to delete the match. */\n new_string: string;\n /** Replace all matches. Defaults to false; when false, old_string must appear exactly once. */\n replace_all?: boolean;\n }): Promise;\n /** Read a UTF-8 text file and return line-numbered content. */\n read(args: {\n /** Path to read, resolved by the filesystem backend. */\n file_path: string;\n /** 1-based first line to return. Defaults to 1. */\n offset?: number;\n /** Maximum number of lines to return. Defaults to 2000. */\n limit?: number;\n }): Promise;\n /** Delegate a self-contained task to a subagent (a separate agent that works in its own context) and return its final result. Use this to offload focused, independent work — research, a scoped implementation, an analysis — so it does not consume this conversation's context. The subagent runs to completion and you receive only its final answer, not its intermediate steps. Give it a complete, standalone prompt: it does not see this conversation. */\n subagent(args: {\n /** A short (3-5 word) description of the delegated task, for display. */\n description: string;\n /** The complete, self-contained task for the subagent. It does not share this conversation's context, so include everything it needs. */\n prompt: string;\n }): Promise;\n /** Delegate a task to a subagent that INHERITS this conversation: a child agent seeded with all completed turns so far (it does not see the current in-flight turn), returning only its final result. Use this when the subtask builds on this conversation's context — a follow-up analysis, a review, a continuation — without consuming this conversation's context for the work itself. You receive only its final answer, not its intermediate steps. */\n subagent_fork(args: {\n /** A short (3-5 word) description of the delegated task, for display. */\n description: string;\n /** The task for the subagent. It already sees this conversation's completed turns, so build on them freely and state only what is new. */\n prompt: string;\n }): Promise;\n /** Record and update a structured task list for the current work. Send the ENTIRE list every call — it REPLACES the previous list (there are no partial updates, no per-item edits). Use it to plan multi-step work and show progress: add one todo per concrete step before you start. Keep AT MOST ONE todo `in_progress` at a time; while work remains, exactly one active task should be `in_progress`. Mark a todo `completed` the moment it is done (do not batch completions), and allow no `in_progress` item only once all work is complete. Skip the list for trivial single-step tasks. Statuses: `pending` (not started), `in_progress` (being worked on now), `completed` (finished). */\n todo_write(args: {\n /** The COMPLETE task list, replacing any previous list. */\n todos: ({\n /** What the task is — a short imperative line. */\n content: string;\n /** pending (not started) | in_progress (now) | completed (done). */\n status: \"pending\" | \"in_progress\" | \"completed\";\n })[];\n }): Promise;\n /** Run a JavaScript workflow script that orchestrates subagents at scale. Use this for work that fans out across many independent pieces — an audit over many files, a migration, multi-angle research, adversarial verification of findings — where you write the orchestration as a script instead of delegating turn by turn. The workflow's identity rides the `meta` parameter as JSON: required `name` (short kebab-case) and `description` strings, optional `whenToUse` string and `phases` array (`{title, detail?, model?}`). The `script` parameter is the plain JavaScript body ONLY (NOT TypeScript, and NO `export const meta` statement — meta is a parameter, not code), running with top-level await; end with `return ` — the value must be JSON-serializable and is this tool's result. Script-body hooks: - `agent(prompt, opts?): Promise` — run one subagent to completion. Without `opts.schema` it resolves to the child's final text; with `opts.schema` (an object-rooted JSON Schema using ONLY type/properties/required/additionalProperties/items/enum/const — no oneOf/pattern/format/numeric bounds) it resolves to the validated object. Resolves `null` when the child fails (filter with `.filter(Boolean)`). Other opts: `label` (display), `phase` (progress group), `model` (override). Anything else (`effort`/`isolation`/`agentType`) is rejected loudly. - `pipeline(items, ...stages): Promise` — run each item through the stages independently with NO barrier between stages (prefer this for multi-stage work). Each stage receives `(prev, item, index)`. An ordinary stage throw drops that ITEM to `null` and skips its remaining stages. - `parallel(thunks): Promise` — run zero-argument functions concurrently and await ALL of them (a barrier; use only when a stage genuinely needs every prior result together). A throwing thunk resolves to `null`. - `phase(title)` — start a progress phase; `log(message)` — narrate progress; `args` — the tool call's `args` input, verbatim. Misused hooks (bad arguments, unknown options, unsupported schemas, tripped caps) throw errors that ALWAYS kill the script — they never dissolve into a per-item `null`. Constraints: concurrency and total-agent caps apply; no filesystem, network, timers, or Node.js APIs are provided — the agents do the work, the script only coordinates them. The run executes in the foreground: this call returns when the whole script finishes. */\n workflow(args: {\n /** The plain-JS workflow script body (top-level await allowed; NO `export const meta` statement; end with `return `). */\n script: string;\n /** The workflow identity block (plain JSON — never code). */\n meta: {\n /** Short kebab-case workflow name. */\n name: string;\n /** One-line description of what the workflow does. */\n description: string;\n /** Optional guidance on when this workflow applies. */\n whenToUse?: string;\n /** Optional phase declarations matched by phase() calls. */\n phases?: {\n /** The phase title phase() calls match by exact string. */\n title: string;\n /** Optional one-line description of the phase. */\n detail?: string;\n /** Optional model override this phase is expected to use. */\n model?: string;\n }[];\n };\n /** Optional JSON input exposed to the script as the `args` global (wrap a bare list as a field, e.g. {\"files\": [...]}). */\n args?: Record;\n }): Promise;\n /** Create or fully replace a UTF-8 text file. */\n write(args: {\n /** Path to write, resolved by the filesystem backend. */\n file_path: string;\n /** Full UTF-8 text content to write. */\n content: string;\n }): Promise;\n}\n```","tools":[{"name":"run_code","description":"Execute a TypeScript program against the available tools. Write the BODY of an async function (erasable syntax only; top-level `await` and `return` work) and call tools as `await tools.name(args)` per the declarations in the system prompt. Only what you print or return comes back — curate it.","parameters":{"type":"object","properties":{"code":{"type":"string","description":"The program: the body of an async TypeScript function."}},"required":["code"]}}]},"reason":"initial"}} +{"type":"request/header","seq":3,"time":1783611771396,"data":{"header":{"config":{"model":"deepseek-v4-flash"},"system":"{{system}}","tools":[{"name":"run_code","description":"Execute a TypeScript program against the available tools. Write the BODY of an async function (erasable syntax only; top-level `await` and `return` work) and call tools as `await tools.name(args)` per the declarations in the system prompt. Only what you print or return comes back — curate it.","parameters":{"type":"object","properties":{"code":{"type":"string","description":"The program: the body of an async TypeScript function."}},"required":["code"]}}]},"reason":"initial"}} {"type":"assistant/chunk","seq":4,"time":1783611771869,"data":{"turn":1,"step":1,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}} {"type":"assistant/chunk","seq":5,"time":1783611771869,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"The"}}} {"type":"assistant/chunk","seq":6,"time":1783611771978,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" user"}}} diff --git a/examples/acp-agent/tests/snapshots/code-mode-turn/system-prompt.golden.md b/examples/acp-agent/tests/snapshots/code-mode-turn/system-prompt.golden.md new file mode 100644 index 0000000000..5dd8547aa8 --- /dev/null +++ b/examples/acp-agent/tests/snapshots/code-mode-turn/system-prompt.golden.md @@ -0,0 +1,136 @@ +You are an AI agent powered by the DeepSeek Harness SDK. + +You are a coding assistant powered by the deepseek-v4-flash model. Your working directory is {{cwd}}. + +Verify your work by running the code or tests. Keep answers brief and factual. + + +Use the read tool — not shell commands like cat — to inspect text files. Results include line numbers. Use offset and limit to continue reading large files. + +Use the write tool to create files or completely replace file contents. Existing files are overwritten, so read an existing file first (the default fs-policy requires it) and prefer edit for targeted changes. + +Use the edit tool for targeted changes to existing UTF-8 text files. It replaces literal old_string with new_string; by default old_string must appear exactly once. If old_string appears multiple times, provide a more specific old_string or set replace_all to true. Read the file first (the default fs-policy requires it), unless you just created or edited it in this session. + +Check the [exit code: N] marker on every bash result; investigate failures before moving on. + +Use the workflow tool ONLY when the user explicitly asks for a workflow or for large multi-agent orchestration: you write a JavaScript script (the tool description documents the exact format) that fans work out across many subagents with phases and structured results. For one or two delegations, prefer plain subagent calls. + +## Writing code for run_code + +Pass `run_code` the body of an async TypeScript function (erasable syntax only — no `enum` or namespaces; type annotations are advisory, the code runs type-stripped). Inside the program: + +- Call tools as `await tools.name(args)` — quoted access for exotic names: `tools["my-tool"](args)`. Every call resolves to the tool's text output as a string. Tool arguments must be JSON-serializable. +- A FAILED tool call rejects with an `Error` carrying the tool's error text — `try/catch` it to handle and continue. +- Calls execute sequentially, even under `Promise.all`. +- Emit results with `return` and/or `console.log(...)`. ONLY what you print or return comes back to you — intermediate tool results never enter the conversation, so extract just what you need. + +The available tools: + +```ts +declare const tools: { + /** Execute a bash command (`bash -c`) and return its stdout/stderr. Each call runs in a fresh shell: no state (cwd, variables, functions) persists between calls — pass `workdir` instead of using `cd`. Non-zero exits are reported as `[exit code: N]`. Commands may run under a file sandbox; a blocked file operation is reported as `[sandbox: file access denied under mode]` — a policy denial, not a bug in the command; do not retry another way (a background task reports the same marker via bash_output once it has finished). Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. Set `run_in_background: true` for long-running commands: the call returns a task id immediately; poll it with `bash_output` and stop it with `bash_kill`. */ + bash(args: { + /** The bash command to execute. */ + command: string; + /** Clear, concise description of what this command does in active voice, 5-10 words (shown in the UI). Examples: "ls" → "List files in current directory"; "git status" → "Show working tree status"; "npm install" → "Install package dependencies". */ + description: string; + /** Timeout in milliseconds. The executor applies its configured default and cap, and kills the command on expiry. */ + timeoutMs?: number; + /** Working directory for this command. Defaults to the session workspace; a relative path is resolved against it. */ + workdir?: string; + /** Run in the background and return a task id immediately. No timeout applies. */ + run_in_background?: boolean; + }): Promise; + /** Ask the executor to kill a running background bash task by task id. */ + bash_kill(args: { + /** Task id returned by the bash tool. */ + task_id: string; + }): Promise; + /** Read new output from a background bash task started with `bash` + `run_in_background`. Returns only output produced since the previous bash_output call, plus the task status. Tasks keep running while you do other work; poll again later for more output. */ + bash_output(args: { + /** Task id returned by the bash tool. */ + task_id: string; + }): Promise; + /** Edit an existing UTF-8 text file by replacing literal text. */ + edit(args: { + /** Path to edit, resolved by the filesystem backend. */ + file_path: string; + /** Literal text to replace. Must match exactly. */ + old_string: string; + /** Literal replacement text. Use an empty string to delete the match. */ + new_string: string; + /** Replace all matches. Defaults to false; when false, old_string must appear exactly once. */ + replace_all?: boolean; + }): Promise; + /** Read a UTF-8 text file and return line-numbered content. */ + read(args: { + /** Path to read, resolved by the filesystem backend. */ + file_path: string; + /** 1-based first line to return. Defaults to 1. */ + offset?: number; + /** Maximum number of lines to return. Defaults to 2000. */ + limit?: number; + }): Promise; + /** Load the full instructions for an available skill. Call this with the exact skill name from the session skill catalog before acting on a task that names or clearly matches that skill. */ + skill(args: { + /** The exact skill name from the available skills list. */ + name: string; + }): Promise; + /** Delegate a self-contained task to a subagent (a separate agent that works in its own context) and return its final result. Use this to offload focused, independent work — research, a scoped implementation, an analysis — so it does not consume this conversation's context. The subagent runs to completion and you receive only its final answer, not its intermediate steps. Give it a complete, standalone prompt: it does not see this conversation. */ + subagent(args: { + /** A short (3-5 word) description of the delegated task, for display. */ + description: string; + /** The complete, self-contained task for the subagent. It does not share this conversation's context, so include everything it needs. */ + prompt: string; + }): Promise; + /** Delegate a task to a subagent that INHERITS this conversation: a child agent seeded with all completed turns so far (it does not see the current in-flight turn), returning only its final result. Use this when the subtask builds on this conversation's context — a follow-up analysis, a review, a continuation — without consuming this conversation's context for the work itself. You receive only its final answer, not its intermediate steps. */ + subagent_fork(args: { + /** A short (3-5 word) description of the delegated task, for display. */ + description: string; + /** The task for the subagent. It already sees this conversation's completed turns, so build on them freely and state only what is new. */ + prompt: string; + }): Promise; + /** Record and update a structured task list for the current work. Send the ENTIRE list every call — it REPLACES the previous list (there are no partial updates, no per-item edits). Use it to plan multi-step work and show progress: add one todo per concrete step before you start. Keep AT MOST ONE todo `in_progress` at a time; while work remains, exactly one active task should be `in_progress`. Mark a todo `completed` the moment it is done (do not batch completions), and allow no `in_progress` item only once all work is complete. Skip the list for trivial single-step tasks. Statuses: `pending` (not started), `in_progress` (being worked on now), `completed` (finished). */ + todo_write(args: { + /** The COMPLETE task list, replacing any previous list. */ + todos: ({ + /** What the task is — a short imperative line. */ + content: string; + /** pending (not started) | in_progress (now) | completed (done). */ + status: "pending" | "in_progress" | "completed"; + })[]; + }): Promise; + /** Run a JavaScript workflow script that orchestrates subagents at scale. Use this for work that fans out across many independent pieces — an audit over many files, a migration, multi-angle research, adversarial verification of findings — where you write the orchestration as a script instead of delegating turn by turn. The workflow's identity rides the `meta` parameter as JSON: required `name` (short kebab-case) and `description` strings, optional `whenToUse` string and `phases` array (`{title, detail?, model?}`). The `script` parameter is the plain JavaScript body ONLY (NOT TypeScript, and NO `export const meta` statement — meta is a parameter, not code), running with top-level await; end with `return ` — the value must be JSON-serializable and is this tool's result. Script-body hooks: - `agent(prompt, opts?): Promise` — run one subagent to completion. Without `opts.schema` it resolves to the child's final text; with `opts.schema` (an object-rooted JSON Schema using ONLY type/properties/required/additionalProperties/items/enum/const — no oneOf/pattern/format/numeric bounds) it resolves to the validated object. Resolves `null` when the child fails (filter with `.filter(Boolean)`). Other opts: `label` (display), `phase` (progress group), `model` (override). Anything else (`effort`/`isolation`/`agentType`) is rejected loudly. - `pipeline(items, ...stages): Promise` — run each item through the stages independently with NO barrier between stages (prefer this for multi-stage work). Each stage receives `(prev, item, index)`. An ordinary stage throw drops that ITEM to `null` and skips its remaining stages. - `parallel(thunks): Promise` — run zero-argument functions concurrently and await ALL of them (a barrier; use only when a stage genuinely needs every prior result together). A throwing thunk resolves to `null`. - `phase(title)` — start a progress phase; `log(message)` — narrate progress; `args` — the tool call's `args` input, verbatim. Misused hooks (bad arguments, unknown options, unsupported schemas, tripped caps) throw errors that ALWAYS kill the script — they never dissolve into a per-item `null`. Constraints: concurrency and total-agent caps apply; no filesystem, network, timers, or Node.js APIs are provided — the agents do the work, the script only coordinates them. The run executes in the foreground: this call returns when the whole script finishes. */ + workflow(args: { + /** The plain-JS workflow script body (top-level await allowed; NO `export const meta` statement; end with `return `). */ + script: string; + /** The workflow identity block (plain JSON — never code). */ + meta: { + /** Short kebab-case workflow name. */ + name: string; + /** One-line description of what the workflow does. */ + description: string; + /** Optional guidance on when this workflow applies. */ + whenToUse?: string; + /** Optional phase declarations matched by phase() calls. */ + phases?: { + /** The phase title phase() calls match by exact string. */ + title: string; + /** Optional one-line description of the phase. */ + detail?: string; + /** Optional model override this phase is expected to use. */ + model?: string; + }[]; + }; + /** Optional JSON input exposed to the script as the `args` global (wrap a bare list as a field, e.g. {"files": [...]}). */ + args?: Record; + }): Promise; + /** Create or fully replace a UTF-8 text file. */ + write(args: { + /** Path to write, resolved by the filesystem backend. */ + file_path: string; + /** Full UTF-8 text content to write. */ + content: string; + }): Promise; +} +``` diff --git a/examples/acp-agent/tests/snapshots/skill-load/input.json b/examples/acp-agent/tests/snapshots/skill-load/input.json new file mode 100644 index 0000000000..a5ee78bff6 --- /dev/null +++ b/examples/acp-agent/tests/snapshots/skill-load/input.json @@ -0,0 +1,7 @@ +{ + "steps": [ + { "op": "initialize" }, + { "op": "newSession" }, + { "op": "prompt", "text": "Load the snapshot-skill skill with the skill tool, then reply DONE." } + ] +} diff --git a/examples/acp-agent/tests/snapshots/skill-load/session.jsonl b/examples/acp-agent/tests/snapshots/skill-load/session.jsonl new file mode 100644 index 0000000000..645671709e --- /dev/null +++ b/examples/acp-agent/tests/snapshots/skill-load/session.jsonl @@ -0,0 +1,29 @@ +{"type":"session","version":0,"id":"9eb4181f-2d05-49d3-98fc-3711fe2f5664","createdAt":1783654655599,"cwd":"/var/folders/_g/59jgff8x2gqd39f5vy1wnbfc0000gn/T/acp-snap-cwd-DhYwNW"} +{"type":"turn/start","seq":0,"time":1783654655602,"data":{"turn":1,"trigger":{"kind":"message","source":{"kind":"user"}}}} +{"type":"user/message","seq":1,"time":1783654655603,"data":{"content":[{"type":"text","text":"Load the snapshot-skill skill with the skill tool, then reply DONE."}],"source":{"kind":"user"}},"surfaceOp":"append"} +{"type":"step/start","seq":2,"time":1783654655608,"data":{"turn":1,"step":1}} +{"type":"request/header","seq":3,"time":1783654655608,"data":{"header":{"config":{"model":"deepseek-v4-flash"},"system":"{{system}}","tools":[{"name":"bash","description":"Execute a bash command (`bash -c`) and return its stdout/stderr. Each call runs in a fresh shell: no state (cwd, variables, functions) persists between calls — pass `workdir` instead of using `cd`. Non-zero exits are reported as `[exit code: N]`. Commands may run under a file sandbox; a blocked file operation is reported as `[sandbox: file access denied under mode]` — a policy denial, not a bug in the command; do not retry another way (a background task reports the same marker via bash_output once it has finished). Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. Set `run_in_background: true` for long-running commands: the call returns a task id immediately; poll it with `bash_output` and stop it with `bash_kill`.","parameters":{"type":"object","properties":{"command":{"type":"string","description":"The bash command to execute."},"description":{"type":"string","description":"Clear, concise description of what this command does in active voice, 5-10 words (shown in the UI). Examples: \"ls\" → \"List files in current directory\"; \"git status\" → \"Show working tree status\"; \"npm install\" → \"Install package dependencies\"."},"timeoutMs":{"type":"number","description":"Timeout in milliseconds. The executor applies its configured default and cap, and kills the command on expiry."},"workdir":{"type":"string","description":"Working directory for this command. Defaults to the session workspace; a relative path is resolved against it."},"run_in_background":{"type":"boolean","description":"Run in the background and return a task id immediately. No timeout applies."}},"required":["command","description"]}},{"name":"bash_kill","description":"Ask the executor to kill a running background bash task by task id.","parameters":{"type":"object","properties":{"task_id":{"type":"string","description":"Task id returned by the bash tool."}},"required":["task_id"]}},{"name":"bash_output","description":"Read new output from a background bash task started with `bash` + `run_in_background`. Returns only output produced since the previous bash_output call, plus the task status. Tasks keep running while you do other work; poll again later for more output.","parameters":{"type":"object","properties":{"task_id":{"type":"string","description":"Task id returned by the bash tool."}},"required":["task_id"]}},{"name":"edit","description":"Edit an existing UTF-8 text file by replacing literal text.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to edit, resolved by the filesystem backend."},"old_string":{"type":"string","description":"Literal text to replace. Must match exactly."},"new_string":{"type":"string","description":"Literal replacement text. Use an empty string to delete the match."},"replace_all":{"type":"boolean","description":"Replace all matches. Defaults to false; when false, old_string must appear exactly once."}},"required":["file_path","old_string","new_string"]}},{"name":"read","description":"Read a UTF-8 text file and return line-numbered content.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to read, resolved by the filesystem backend."},"offset":{"type":"number","description":"1-based first line to return. Defaults to 1."},"limit":{"type":"number","description":"Maximum number of lines to return. Defaults to 2000."}},"required":["file_path"]}},{"name":"skill","description":"Load the full instructions for an available skill. Call this with the exact skill name from the session skill catalog before acting on a task that names or clearly matches that skill.","parameters":{"type":"object","properties":{"name":{"type":"string","description":"The exact skill name from the available skills list."}},"required":["name"]}},{"name":"subagent","description":"Delegate a self-contained task to a subagent (a separate agent that works in its own context) and return its final result. Use this to offload focused, independent work — research, a scoped implementation, an analysis — so it does not consume this conversation's context. The subagent runs to completion and you receive only its final answer, not its intermediate steps. Give it a complete, standalone prompt: it does not see this conversation.","parameters":{"type":"object","properties":{"description":{"type":"string","description":"A short (3-5 word) description of the delegated task, for display."},"prompt":{"type":"string","description":"The complete, self-contained task for the subagent. It does not share this conversation's context, so include everything it needs."}},"required":["description","prompt"]}},{"name":"subagent_fork","description":"Delegate a task to a subagent that INHERITS this conversation: a child agent seeded with all completed turns so far (it does not see the current in-flight turn), returning only its final result. Use this when the subtask builds on this conversation's context — a follow-up analysis, a review, a continuation — without consuming this conversation's context for the work itself. You receive only its final answer, not its intermediate steps.","parameters":{"type":"object","properties":{"description":{"type":"string","description":"A short (3-5 word) description of the delegated task, for display."},"prompt":{"type":"string","description":"The task for the subagent. It already sees this conversation's completed turns, so build on them freely and state only what is new."}},"required":["description","prompt"]}},{"name":"todo_write","description":"Record and update a structured task list for the current work. Send the ENTIRE list every call — it REPLACES the previous list (there are no partial updates, no per-item edits). Use it to plan multi-step work and show progress: add one todo per concrete step before you start. Keep AT MOST ONE todo `in_progress` at a time; while work remains, exactly one active task should be `in_progress`. Mark a todo `completed` the moment it is done (do not batch completions), and allow no `in_progress` item only once all work is complete. Skip the list for trivial single-step tasks. Statuses: `pending` (not started), `in_progress` (being worked on now), `completed` (finished).","parameters":{"type":"object","properties":{"todos":{"type":"array","description":"The COMPLETE task list, replacing any previous list.","items":{"type":"object","properties":{"content":{"type":"string","description":"What the task is — a short imperative line."},"status":{"type":"string","description":"pending (not started) | in_progress (now) | completed (done).","enum":["pending","in_progress","completed"]}},"required":["content","status"]}}},"required":["todos"]}},{"name":"workflow","description":"Run a JavaScript workflow script that orchestrates subagents at scale. Use this for work that fans out across many independent pieces — an audit over many files, a migration, multi-angle research, adversarial verification of findings — where you write the orchestration as a script instead of delegating turn by turn.\n\nThe workflow's identity rides the `meta` parameter as JSON: required `name` (short kebab-case) and `description` strings, optional `whenToUse` string and `phases` array (`{title, detail?, model?}`). The `script` parameter is the plain JavaScript body ONLY (NOT TypeScript, and NO `export const meta` statement — meta is a parameter, not code), running with top-level await; end with `return ` — the value must be JSON-serializable and is this tool's result.\n\nScript-body hooks:\n- `agent(prompt, opts?): Promise` — run one subagent to completion. Without `opts.schema` it resolves to the child's final text; with `opts.schema` (an object-rooted JSON Schema using ONLY type/properties/required/additionalProperties/items/enum/const — no oneOf/pattern/format/numeric bounds) it resolves to the validated object. Resolves `null` when the child fails (filter with `.filter(Boolean)`). Other opts: `label` (display), `phase` (progress group), `model` (override). Anything else (`effort`/`isolation`/`agentType`) is rejected loudly.\n- `pipeline(items, ...stages): Promise` — run each item through the stages independently with NO barrier between stages (prefer this for multi-stage work). Each stage receives `(prev, item, index)`. An ordinary stage throw drops that ITEM to `null` and skips its remaining stages.\n- `parallel(thunks): Promise` — run zero-argument functions concurrently and await ALL of them (a barrier; use only when a stage genuinely needs every prior result together). A throwing thunk resolves to `null`.\n- `phase(title)` — start a progress phase; `log(message)` — narrate progress; `args` — the tool call's `args` input, verbatim.\n\nMisused hooks (bad arguments, unknown options, unsupported schemas, tripped caps) throw errors that ALWAYS kill the script — they never dissolve into a per-item `null`.\n\nConstraints: concurrency and total-agent caps apply; no filesystem, network, timers, or Node.js APIs are provided — the agents do the work, the script only coordinates them. The run executes in the foreground: this call returns when the whole script finishes.","parameters":{"type":"object","properties":{"script":{"type":"string","description":"The plain-JS workflow script body (top-level await allowed; NO `export const meta` statement; end with `return `)."},"meta":{"type":"object","description":"The workflow identity block (plain JSON — never code).","properties":{"name":{"type":"string","description":"Short kebab-case workflow name."},"description":{"type":"string","description":"One-line description of what the workflow does."},"whenToUse":{"type":"string","description":"Optional guidance on when this workflow applies."},"phases":{"type":"array","description":"Optional phase declarations matched by phase() calls.","items":{"type":"object","properties":{"title":{"type":"string","description":"The phase title phase() calls match by exact string."},"detail":{"type":"string","description":"Optional one-line description of the phase."},"model":{"type":"string","description":"Optional model override this phase is expected to use."}},"required":["title"]}}},"required":["name","description"]},"args":{"type":"object","description":"Optional JSON input exposed to the script as the `args` global (wrap a bare list as a field, e.g. {\"files\": [...]})."}},"required":["script","meta"]}},{"name":"write","description":"Create or fully replace a UTF-8 text file.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to write, resolved by the filesystem backend."},"content":{"type":"string","description":"Full UTF-8 text content to write."}},"required":["file_path","content"]}}],"messagePrefix":[{"role":"user","content":[{"type":"text","text":"\nA skill is a reusable set of task-specific instructions. The following skills are available in this session:\n\n\n- `snapshot-skill`: Exercise project skill discovery and loading in snapshot tests.\n\n\nIf the user names a skill, or the task clearly matches a skill's description, call the `skill` tool with the exact skill name before taking task actions. Load all applicable skills, then follow their full instructions. This catalog contains summaries only; do not infer or follow a skill's instructions until it has been loaded.\n"}]}]},"reason":"initial"}} +{"type":"assistant/chunk","seq":4,"time":1783654655609,"data":{"turn":1,"step":1,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}} +{"type":"assistant/chunk","seq":5,"time":1783654655609,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"Load the requested skill."}}} +{"type":"assistant/chunk","seq":6,"time":1783654655609,"data":{"turn":1,"step":1,"chunk":{"type":"block-start","index":1,"blockType":"tool-call"}}} +{"type":"assistant/chunk","seq":7,"time":1783654655609,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_skill_load","name":"skill","argumentsDelta":"{\"name\":\"snapshot-skill\"}"}}} +{"type":"assistant/chunk","seq":8,"time":1783654655609,"data":{"turn":1,"step":1,"chunk":{"type":"block-end","index":0,"block":{"type":"reasoning","text":"Load the requested skill."}}}} +{"type":"assistant/chunk","seq":9,"time":1783654655609,"data":{"turn":1,"step":1,"chunk":{"type":"block-end","index":1,"block":{"type":"tool-call","id":"call_skill_load","name":"skill","arguments":"{\"name\":\"snapshot-skill\"}"}}}} +{"type":"assistant/chunk","seq":10,"time":1783654655609,"data":{"turn":1,"step":1,"chunk":{"type":"usage","usage":{"inputTokens":100,"outputTokens":20,"cacheReadTokens":0,"reasoningTokens":5}}}} +{"type":"assistant/chunk","seq":11,"time":1783654655609,"data":{"turn":1,"step":1,"chunk":{"type":"finish","reason":{"kind":"tool-calls"}}}} +{"type":"assistant/message","seq":12,"time":1783654655609,"data":{"turn":1,"step":1,"content":[{"type":"reasoning","text":"Load the requested skill."},{"type":"tool-call","id":"call_skill_load","name":"skill","arguments":"{\"name\":\"snapshot-skill\"}"}],"usage":{"inputTokens":100,"outputTokens":20,"cacheReadTokens":0,"reasoningTokens":5}},"sourceEventSeqs":[4,5,6,7,8,9,10,11],"surfaceOp":"append"} +{"type":"tool/call","seq":13,"time":1783654655609,"data":{"turn":1,"step":1,"callId":"call_skill_load","name":"skill","arguments":"{\"name\":\"snapshot-skill\"}"}} +{"type":"tool/result","seq":14,"time":1783654655610,"data":{"turn":1,"step":1,"callId":"call_skill_load","content":[{"type":"text","text":"\n\nBase directory for this skill: /var/folders/_g/59jgff8x2gqd39f5vy1wnbfc0000gn/T/acp-snap-cwd-DhYwNW/.dsh/skills/snapshot-skill\nResolve relative paths mentioned by this skill against the base directory before using them. Load referenced resources only as needed.\n\n\n\nFollow these snapshot-only instructions.\nResolve referenced resources relative to this skill directory.\n\n"}],"isError":false},"sourceEventSeqs":[13],"surfaceOp":"append"} +{"type":"step/end","seq":15,"time":1783654655610,"data":{"turn":1,"step":1}} +{"type":"step/start","seq":16,"time":1783654655610,"data":{"turn":1,"step":2}} +{"type":"assistant/chunk","seq":17,"time":1783654655611,"data":{"turn":1,"step":2,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}} +{"type":"assistant/chunk","seq":18,"time":1783654655611,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"The skill is loaded."}}} +{"type":"assistant/chunk","seq":19,"time":1783654655611,"data":{"turn":1,"step":2,"chunk":{"type":"block-start","index":1,"blockType":"text"}}} +{"type":"assistant/chunk","seq":20,"time":1783654655611,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":"DONE"}}} +{"type":"assistant/chunk","seq":21,"time":1783654655611,"data":{"turn":1,"step":2,"chunk":{"type":"block-end","index":0,"block":{"type":"reasoning","text":"The skill is loaded."}}}} +{"type":"assistant/chunk","seq":22,"time":1783654655611,"data":{"turn":1,"step":2,"chunk":{"type":"block-end","index":1,"block":{"type":"text","text":"DONE"}}}} +{"type":"assistant/chunk","seq":23,"time":1783654655611,"data":{"turn":1,"step":2,"chunk":{"type":"usage","usage":{"inputTokens":180,"outputTokens":10,"cacheReadTokens":0,"reasoningTokens":4}}}} +{"type":"assistant/chunk","seq":24,"time":1783654655611,"data":{"turn":1,"step":2,"chunk":{"type":"finish","reason":{"kind":"stop"}}}} +{"type":"assistant/message","seq":25,"time":1783654655611,"data":{"turn":1,"step":2,"content":[{"type":"reasoning","text":"The skill is loaded."},{"type":"text","text":"DONE"}],"usage":{"inputTokens":180,"outputTokens":10,"cacheReadTokens":0,"reasoningTokens":4}},"sourceEventSeqs":[17,18,19,20,21,22,23,24],"surfaceOp":"append"} +{"type":"step/end","seq":26,"time":1783654655611,"data":{"turn":1,"step":2}} +{"type":"turn/end","seq":27,"time":1783654655611,"data":{"turn":1,"reason":{"kind":"completed"}}} diff --git a/examples/acp-agent/tests/snapshots/skill-load/stdout.golden.jsonl b/examples/acp-agent/tests/snapshots/skill-load/stdout.golden.jsonl new file mode 100644 index 0000000000..00e707088e --- /dev/null +++ b/examples/acp-agent/tests/snapshots/skill-load/stdout.golden.jsonl @@ -0,0 +1,8 @@ +{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":1,"agentInfo":{"name":"deepseek-harness-acp","version":"0.0.1"},"agentCapabilities":{"loadSession":true,"promptCapabilities":{"image":false,"audio":false,"embeddedContext":false}},"authMethods":[]}} +{"jsonrpc":"2.0","id":2,"result":{"sessionId":"{{sessionId}}"}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"Load the requested skill."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"tool_call","toolCallId":"call_skill_load","title":"Load skill snapshot-skill","kind":"read","status":"in_progress","rawInput":"snapshot-skill"}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"tool_call_update","toolCallId":"call_skill_load","status":"completed","content":[{"type":"content","content":{"type":"text","text":"\n\nBase directory for this skill: {{cwd}}/.dsh/skills/snapshot-skill\nResolve relative paths mentioned by this skill against the base directory before using them. Load referenced resources only as needed.\n\n\n\nFollow these snapshot-only instructions.\nResolve referenced resources relative to this skill directory.\n\n"}}]}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"The skill is loaded."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"DONE"}}}} +{"jsonrpc":"2.0","id":3,"result":{"stopReason":"end_turn"}} diff --git a/examples/acp-agent/tests/snapshots/skill-load/system-prompt.golden.md b/examples/acp-agent/tests/snapshots/skill-load/system-prompt.golden.md new file mode 100644 index 0000000000..18f0cbcd07 --- /dev/null +++ b/examples/acp-agent/tests/snapshots/skill-load/system-prompt.golden.md @@ -0,0 +1,16 @@ +You are an AI agent powered by the DeepSeek Harness SDK. + +You are a coding assistant powered by the deepseek-v4-flash model. Your working directory is {{cwd}}. + +Verify your work by running the code or tests. Keep answers brief and factual. + + +Use the read tool — not shell commands like cat — to inspect text files. Results include line numbers. Use offset and limit to continue reading large files. + +Use the write tool to create files or completely replace file contents. Existing files are overwritten, so read an existing file first (the default fs-policy requires it) and prefer edit for targeted changes. + +Use the edit tool for targeted changes to existing UTF-8 text files. It replaces literal old_string with new_string; by default old_string must appear exactly once. If old_string appears multiple times, provide a more specific old_string or set replace_all to true. Read the file first (the default fs-policy requires it), unless you just created or edited it in this session. + +Check the [exit code: N] marker on every bash result; investigate failures before moving on. + +Use the workflow tool ONLY when the user explicitly asks for a workflow or for large multi-agent orchestration: you write a JavaScript script (the tool description documents the exact format) that fans work out across many subagents with phases and structured results. For one or two delegations, prefer plain subagent calls. diff --git a/examples/acp-agent/tests/snapshots/skill-load/workspace/.dsh/skills/snapshot-skill/SKILL.md b/examples/acp-agent/tests/snapshots/skill-load/workspace/.dsh/skills/snapshot-skill/SKILL.md new file mode 100644 index 0000000000..b0816d6bee --- /dev/null +++ b/examples/acp-agent/tests/snapshots/skill-load/workspace/.dsh/skills/snapshot-skill/SKILL.md @@ -0,0 +1,7 @@ +--- +name: snapshot-skill +description: Exercise project skill discovery and loading in snapshot tests. +--- + +Follow these snapshot-only instructions. +Resolve referenced resources relative to this skill directory. diff --git a/examples/acp-agent/tests/snapshots/text-turn/session.jsonl b/examples/acp-agent/tests/snapshots/text-turn/session.jsonl index 8475c97896..324ef8d4eb 100644 --- a/examples/acp-agent/tests/snapshots/text-turn/session.jsonl +++ b/examples/acp-agent/tests/snapshots/text-turn/session.jsonl @@ -2,7 +2,7 @@ {"type":"turn/start","seq":0,"time":1783600629541,"data":{"turn":1,"trigger":{"kind":"message","source":{"kind":"user"}}}} {"type":"user/message","seq":1,"time":1783600629541,"data":{"content":[{"type":"text","text":"Reply with exactly the word: PONG. Do not use any tools."}],"source":{"kind":"user"}},"surfaceOp":"append"} {"type":"step/start","seq":2,"time":1783600629542,"data":{"turn":1,"step":1}} -{"type":"request/header","seq":3,"time":1783600629542,"data":{"header":{"config":{"model":"deepseek-v4-flash"},"system":"You are an AI agent powered by the DeepSeek Harness SDK.\n\nYou are a coding assistant powered by the deepseek-v4-flash model. Your working\ndirectory is /var/folders/bn/vj1dvck95yd5jh3x4wskflxm0000gn/T/acp-snap-cwd-ka5r8w.\n\nVerify your work by running the code or tests. Keep answers brief and\nfactual.\n\n\nUse the read tool — not shell commands like cat — to inspect text files. Results include line numbers. Use offset and limit to continue reading large files.\n\nUse the write tool to create files or completely replace file contents. Existing files are overwritten, so read an existing file first (the default fs-policy requires it) and prefer edit for targeted changes.\n\nUse the edit tool for targeted changes to existing UTF-8 text files. It replaces literal old_string with new_string; by default old_string must appear exactly once. If old_string appears multiple times, provide a more specific old_string or set replace_all to true. Read the file first (the default fs-policy requires it), unless you just created or edited it in this session.\n\nCheck the [exit code: N] marker on every bash result; investigate failures before moving on.\n\nUse the workflow tool ONLY when the user explicitly asks for a workflow or for large multi-agent orchestration: you write a JavaScript script (the tool description documents the exact format) that fans work out across many subagents with phases and structured results. For one or two delegations, prefer plain subagent calls.","tools":[{"name":"bash","description":"Execute a bash command (`bash -c`) and return its stdout/stderr. Each call runs in a fresh shell: no state (cwd, variables, functions) persists between calls — pass `workdir` instead of using `cd`. Non-zero exits are reported as `[exit code: N]`. Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. Set `run_in_background: true` for long-running commands: the call returns a task id immediately; poll it with `bash_output` and stop it with `bash_kill`.","parameters":{"type":"object","properties":{"command":{"type":"string","description":"The bash command to execute."},"description":{"type":"string","description":"Clear, concise description of what this command does in active voice, 5-10 words (shown in the UI). Examples: \"ls\" → \"List files in current directory\"; \"git status\" → \"Show working tree status\"; \"npm install\" → \"Install package dependencies\"."},"timeoutMs":{"type":"number","description":"Timeout in milliseconds. The executor applies its configured default and cap, and kills the command on expiry."},"workdir":{"type":"string","description":"Working directory for this command. Defaults to the session workspace; a relative path is resolved against it."},"run_in_background":{"type":"boolean","description":"Run in the background and return a task id immediately. No timeout applies."}},"required":["command","description"]}},{"name":"bash_kill","description":"Ask the executor to kill a running background bash task by task id.","parameters":{"type":"object","properties":{"task_id":{"type":"string","description":"Task id returned by the bash tool."}},"required":["task_id"]}},{"name":"bash_output","description":"Read new output from a background bash task started with `bash` + `run_in_background`. Returns only output produced since the previous bash_output call, plus the task status. Tasks keep running while you do other work; poll again later for more output.","parameters":{"type":"object","properties":{"task_id":{"type":"string","description":"Task id returned by the bash tool."}},"required":["task_id"]}},{"name":"edit","description":"Edit an existing UTF-8 text file by replacing literal text.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to edit, resolved by the filesystem backend."},"old_string":{"type":"string","description":"Literal text to replace. Must match exactly."},"new_string":{"type":"string","description":"Literal replacement text. Use an empty string to delete the match."},"replace_all":{"type":"boolean","description":"Replace all matches. Defaults to false; when false, old_string must appear exactly once."}},"required":["file_path","old_string","new_string"]}},{"name":"read","description":"Read a UTF-8 text file and return line-numbered content.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to read, resolved by the filesystem backend."},"offset":{"type":"number","description":"1-based first line to return. Defaults to 1."},"limit":{"type":"number","description":"Maximum number of lines to return. Defaults to 2000."}},"required":["file_path"]}},{"name":"subagent","description":"Delegate a self-contained task to a subagent (a separate agent that works in its own context) and return its final result. Use this to offload focused, independent work — research, a scoped implementation, an analysis — so it does not consume this conversation's context. The subagent runs to completion and you receive only its final answer, not its intermediate steps. Give it a complete, standalone prompt: it does not see this conversation.","parameters":{"type":"object","properties":{"description":{"type":"string","description":"A short (3-5 word) description of the delegated task, for display."},"prompt":{"type":"string","description":"The complete, self-contained task for the subagent. It does not share this conversation's context, so include everything it needs."}},"required":["description","prompt"]}},{"name":"subagent_fork","description":"Delegate a task to a subagent that INHERITS this conversation: a child agent seeded with all completed turns so far (it does not see the current in-flight turn), returning only its final result. Use this when the subtask builds on this conversation's context — a follow-up analysis, a review, a continuation — without consuming this conversation's context for the work itself. You receive only its final answer, not its intermediate steps.","parameters":{"type":"object","properties":{"description":{"type":"string","description":"A short (3-5 word) description of the delegated task, for display."},"prompt":{"type":"string","description":"The task for the subagent. It already sees this conversation's completed turns, so build on them freely and state only what is new."}},"required":["description","prompt"]}},{"name":"todo_write","description":"Record and update a structured task list for the current work. Send the ENTIRE list every call — it REPLACES the previous list (there are no partial updates, no per-item edits). Use it to plan multi-step work and show progress: add one todo per concrete step before you start. Keep AT MOST ONE todo `in_progress` at a time; while work remains, exactly one active task should be `in_progress`. Mark a todo `completed` the moment it is done (do not batch completions), and allow no `in_progress` item only once all work is complete. Skip the list for trivial single-step tasks. Statuses: `pending` (not started), `in_progress` (being worked on now), `completed` (finished).","parameters":{"type":"object","properties":{"todos":{"type":"array","description":"The COMPLETE task list, replacing any previous list.","items":{"type":"object","properties":{"content":{"type":"string","description":"What the task is — a short imperative line."},"status":{"type":"string","description":"pending (not started) | in_progress (now) | completed (done).","enum":["pending","in_progress","completed"]}},"required":["content","status"]}}},"required":["todos"]}},{"name":"workflow","description":"Run a JavaScript workflow script that orchestrates subagents at scale. Use this for work that fans out across many independent pieces — an audit over many files, a migration, multi-angle research, adversarial verification of findings — where you write the orchestration as a script instead of delegating turn by turn.\n\nThe workflow's identity rides the `meta` parameter as JSON: required `name` (short kebab-case) and `description` strings, optional `whenToUse` string and `phases` array (`{title, detail?, model?}`). The `script` parameter is the plain JavaScript body ONLY (NOT TypeScript, and NO `export const meta` statement — meta is a parameter, not code), running with top-level await; end with `return ` — the value must be JSON-serializable and is this tool's result.\n\nScript-body hooks:\n- `agent(prompt, opts?): Promise` — run one subagent to completion. Without `opts.schema` it resolves to the child's final text; with `opts.schema` (an object-rooted JSON Schema using ONLY type/properties/required/additionalProperties/items/enum/const — no oneOf/pattern/format/numeric bounds) it resolves to the validated object. Resolves `null` when the child fails (filter with `.filter(Boolean)`). Other opts: `label` (display), `phase` (progress group), `model` (override). Anything else (`effort`/`isolation`/`agentType`) is rejected loudly.\n- `pipeline(items, ...stages): Promise` — run each item through the stages independently with NO barrier between stages (prefer this for multi-stage work). Each stage receives `(prev, item, index)`. An ordinary stage throw drops that ITEM to `null` and skips its remaining stages.\n- `parallel(thunks): Promise` — run zero-argument functions concurrently and await ALL of them (a barrier; use only when a stage genuinely needs every prior result together). A throwing thunk resolves to `null`.\n- `phase(title)` — start a progress phase; `log(message)` — narrate progress; `args` — the tool call's `args` input, verbatim.\n\nMisused hooks (bad arguments, unknown options, unsupported schemas, tripped caps) throw errors that ALWAYS kill the script — they never dissolve into a per-item `null`.\n\nConstraints: concurrency and total-agent caps apply; no filesystem, network, timers, or Node.js APIs are provided — the agents do the work, the script only coordinates them. The run executes in the foreground: this call returns when the whole script finishes.","parameters":{"type":"object","properties":{"script":{"type":"string","description":"The plain-JS workflow script body (top-level await allowed; NO `export const meta` statement; end with `return `)."},"meta":{"type":"object","description":"The workflow identity block (plain JSON — never code).","properties":{"name":{"type":"string","description":"Short kebab-case workflow name."},"description":{"type":"string","description":"One-line description of what the workflow does."},"whenToUse":{"type":"string","description":"Optional guidance on when this workflow applies."},"phases":{"type":"array","description":"Optional phase declarations matched by phase() calls.","items":{"type":"object","properties":{"title":{"type":"string","description":"The phase title phase() calls match by exact string."},"detail":{"type":"string","description":"Optional one-line description of the phase."},"model":{"type":"string","description":"Optional model override this phase is expected to use."}},"required":["title"]}}},"required":["name","description"]},"args":{"type":"object","description":"Optional JSON input exposed to the script as the `args` global (wrap a bare list as a field, e.g. {\"files\": [...]})."}},"required":["script","meta"]}},{"name":"write","description":"Create or fully replace a UTF-8 text file.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to write, resolved by the filesystem backend."},"content":{"type":"string","description":"Full UTF-8 text content to write."}},"required":["file_path","content"]}}]},"reason":"initial"}} +{"type":"request/header","seq":3,"time":1783600629542,"data":{"header":{"config":{"model":"deepseek-v4-flash"},"system":"{{system}}","tools":[{"name":"bash","description":"Execute a bash command (`bash -c`) and return its stdout/stderr. Each call runs in a fresh shell: no state (cwd, variables, functions) persists between calls — pass `workdir` instead of using `cd`. Non-zero exits are reported as `[exit code: N]`. Commands may run under a file sandbox; a blocked file operation is reported as `[sandbox: file access denied under mode]` — a policy denial, not a bug in the command; do not retry another way (a background task reports the same marker via bash_output once it has finished). Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. Set `run_in_background: true` for long-running commands: the call returns a task id immediately; poll it with `bash_output` and stop it with `bash_kill`.","parameters":{"type":"object","properties":{"command":{"type":"string","description":"The bash command to execute."},"description":{"type":"string","description":"Clear, concise description of what this command does in active voice, 5-10 words (shown in the UI). Examples: \"ls\" → \"List files in current directory\"; \"git status\" → \"Show working tree status\"; \"npm install\" → \"Install package dependencies\"."},"timeoutMs":{"type":"number","description":"Timeout in milliseconds. The executor applies its configured default and cap, and kills the command on expiry."},"workdir":{"type":"string","description":"Working directory for this command. Defaults to the session workspace; a relative path is resolved against it."},"run_in_background":{"type":"boolean","description":"Run in the background and return a task id immediately. No timeout applies."}},"required":["command","description"]}},{"name":"bash_kill","description":"Ask the executor to kill a running background bash task by task id.","parameters":{"type":"object","properties":{"task_id":{"type":"string","description":"Task id returned by the bash tool."}},"required":["task_id"]}},{"name":"bash_output","description":"Read new output from a background bash task started with `bash` + `run_in_background`. Returns only output produced since the previous bash_output call, plus the task status. Tasks keep running while you do other work; poll again later for more output.","parameters":{"type":"object","properties":{"task_id":{"type":"string","description":"Task id returned by the bash tool."}},"required":["task_id"]}},{"name":"edit","description":"Edit an existing UTF-8 text file by replacing literal text.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to edit, resolved by the filesystem backend."},"old_string":{"type":"string","description":"Literal text to replace. Must match exactly."},"new_string":{"type":"string","description":"Literal replacement text. Use an empty string to delete the match."},"replace_all":{"type":"boolean","description":"Replace all matches. Defaults to false; when false, old_string must appear exactly once."}},"required":["file_path","old_string","new_string"]}},{"name":"read","description":"Read a UTF-8 text file and return line-numbered content.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to read, resolved by the filesystem backend."},"offset":{"type":"number","description":"1-based first line to return. Defaults to 1."},"limit":{"type":"number","description":"Maximum number of lines to return. Defaults to 2000."}},"required":["file_path"]}},{"name":"skill","description":"Load the full instructions for an available skill. Call this with the exact skill name from the session skill catalog before acting on a task that names or clearly matches that skill.","parameters":{"type":"object","properties":{"name":{"type":"string","description":"The exact skill name from the available skills list."}},"required":["name"]}},{"name":"subagent","description":"Delegate a self-contained task to a subagent (a separate agent that works in its own context) and return its final result. Use this to offload focused, independent work — research, a scoped implementation, an analysis — so it does not consume this conversation's context. The subagent runs to completion and you receive only its final answer, not its intermediate steps. Give it a complete, standalone prompt: it does not see this conversation.","parameters":{"type":"object","properties":{"description":{"type":"string","description":"A short (3-5 word) description of the delegated task, for display."},"prompt":{"type":"string","description":"The complete, self-contained task for the subagent. It does not share this conversation's context, so include everything it needs."}},"required":["description","prompt"]}},{"name":"subagent_fork","description":"Delegate a task to a subagent that INHERITS this conversation: a child agent seeded with all completed turns so far (it does not see the current in-flight turn), returning only its final result. Use this when the subtask builds on this conversation's context — a follow-up analysis, a review, a continuation — without consuming this conversation's context for the work itself. You receive only its final answer, not its intermediate steps.","parameters":{"type":"object","properties":{"description":{"type":"string","description":"A short (3-5 word) description of the delegated task, for display."},"prompt":{"type":"string","description":"The task for the subagent. It already sees this conversation's completed turns, so build on them freely and state only what is new."}},"required":["description","prompt"]}},{"name":"todo_write","description":"Record and update a structured task list for the current work. Send the ENTIRE list every call — it REPLACES the previous list (there are no partial updates, no per-item edits). Use it to plan multi-step work and show progress: add one todo per concrete step before you start. Keep AT MOST ONE todo `in_progress` at a time; while work remains, exactly one active task should be `in_progress`. Mark a todo `completed` the moment it is done (do not batch completions), and allow no `in_progress` item only once all work is complete. Skip the list for trivial single-step tasks. Statuses: `pending` (not started), `in_progress` (being worked on now), `completed` (finished).","parameters":{"type":"object","properties":{"todos":{"type":"array","description":"The COMPLETE task list, replacing any previous list.","items":{"type":"object","properties":{"content":{"type":"string","description":"What the task is — a short imperative line."},"status":{"type":"string","description":"pending (not started) | in_progress (now) | completed (done).","enum":["pending","in_progress","completed"]}},"required":["content","status"]}}},"required":["todos"]}},{"name":"workflow","description":"Run a JavaScript workflow script that orchestrates subagents at scale. Use this for work that fans out across many independent pieces — an audit over many files, a migration, multi-angle research, adversarial verification of findings — where you write the orchestration as a script instead of delegating turn by turn.\n\nThe workflow's identity rides the `meta` parameter as JSON: required `name` (short kebab-case) and `description` strings, optional `whenToUse` string and `phases` array (`{title, detail?, model?}`). The `script` parameter is the plain JavaScript body ONLY (NOT TypeScript, and NO `export const meta` statement — meta is a parameter, not code), running with top-level await; end with `return ` — the value must be JSON-serializable and is this tool's result.\n\nScript-body hooks:\n- `agent(prompt, opts?): Promise` — run one subagent to completion. Without `opts.schema` it resolves to the child's final text; with `opts.schema` (an object-rooted JSON Schema using ONLY type/properties/required/additionalProperties/items/enum/const — no oneOf/pattern/format/numeric bounds) it resolves to the validated object. Resolves `null` when the child fails (filter with `.filter(Boolean)`). Other opts: `label` (display), `phase` (progress group), `model` (override). Anything else (`effort`/`isolation`/`agentType`) is rejected loudly.\n- `pipeline(items, ...stages): Promise` — run each item through the stages independently with NO barrier between stages (prefer this for multi-stage work). Each stage receives `(prev, item, index)`. An ordinary stage throw drops that ITEM to `null` and skips its remaining stages.\n- `parallel(thunks): Promise` — run zero-argument functions concurrently and await ALL of them (a barrier; use only when a stage genuinely needs every prior result together). A throwing thunk resolves to `null`.\n- `phase(title)` — start a progress phase; `log(message)` — narrate progress; `args` — the tool call's `args` input, verbatim.\n\nMisused hooks (bad arguments, unknown options, unsupported schemas, tripped caps) throw errors that ALWAYS kill the script — they never dissolve into a per-item `null`.\n\nConstraints: concurrency and total-agent caps apply; no filesystem, network, timers, or Node.js APIs are provided — the agents do the work, the script only coordinates them. The run executes in the foreground: this call returns when the whole script finishes.","parameters":{"type":"object","properties":{"script":{"type":"string","description":"The plain-JS workflow script body (top-level await allowed; NO `export const meta` statement; end with `return `)."},"meta":{"type":"object","description":"The workflow identity block (plain JSON — never code).","properties":{"name":{"type":"string","description":"Short kebab-case workflow name."},"description":{"type":"string","description":"One-line description of what the workflow does."},"whenToUse":{"type":"string","description":"Optional guidance on when this workflow applies."},"phases":{"type":"array","description":"Optional phase declarations matched by phase() calls.","items":{"type":"object","properties":{"title":{"type":"string","description":"The phase title phase() calls match by exact string."},"detail":{"type":"string","description":"Optional one-line description of the phase."},"model":{"type":"string","description":"Optional model override this phase is expected to use."}},"required":["title"]}}},"required":["name","description"]},"args":{"type":"object","description":"Optional JSON input exposed to the script as the `args` global (wrap a bare list as a field, e.g. {\"files\": [...]})."}},"required":["script","meta"]}},{"name":"write","description":"Create or fully replace a UTF-8 text file.","parameters":{"type":"object","properties":{"file_path":{"type":"string","description":"Path to write, resolved by the filesystem backend."},"content":{"type":"string","description":"Full UTF-8 text content to write."}},"required":["file_path","content"]}}]},"reason":"initial"}} {"type":"assistant/chunk","seq":4,"time":1783600630819,"data":{"turn":1,"step":1,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}} {"type":"assistant/chunk","seq":5,"time":1783600630820,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"The"}}} {"type":"assistant/chunk","seq":6,"time":1783600630822,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" user"}}} diff --git a/examples/acp-agent/tests/snapshots/text-turn/system-prompt.golden.md b/examples/acp-agent/tests/snapshots/text-turn/system-prompt.golden.md new file mode 100644 index 0000000000..18f0cbcd07 --- /dev/null +++ b/examples/acp-agent/tests/snapshots/text-turn/system-prompt.golden.md @@ -0,0 +1,16 @@ +You are an AI agent powered by the DeepSeek Harness SDK. + +You are a coding assistant powered by the deepseek-v4-flash model. Your working directory is {{cwd}}. + +Verify your work by running the code or tests. Keep answers brief and factual. + + +Use the read tool — not shell commands like cat — to inspect text files. Results include line numbers. Use offset and limit to continue reading large files. + +Use the write tool to create files or completely replace file contents. Existing files are overwritten, so read an existing file first (the default fs-policy requires it) and prefer edit for targeted changes. + +Use the edit tool for targeted changes to existing UTF-8 text files. It replaces literal old_string with new_string; by default old_string must appear exactly once. If old_string appears multiple times, provide a more specific old_string or set replace_all to true. Read the file first (the default fs-policy requires it), unless you just created or edited it in this session. + +Check the [exit code: N] marker on every bash result; investigate failures before moving on. + +Use the workflow tool ONLY when the user explicitly asks for a workflow or for large multi-agent orchestration: you write a JavaScript script (the tool description documents the exact format) that fans work out across many subagents with phases and structured results. For one or two delegations, prefer plain subagent calls. diff --git a/examples/coding-agent/README.md b/examples/coding-agent/README.md index 7731aa8f09..5a607d3c7b 100644 --- a/examples/coding-agent/README.md +++ b/examples/coding-agent/README.md @@ -33,7 +33,7 @@ The id is wired through `cordis.yml` (`resumeSessionId: !!js process.env.RESUME_ ## Code Mode -[`code-mode.cordis.yml`](code-mode.cordis.yml) is this same tree flipped to [Code Mode](../../docs/rfc/implemented/feature/2026-06-15-code-mode.md): an include overlay over `./cordis.yml` whose two patches insert the worker-thread code runtime (`@deepseek-ai/dsh-code-runtime-worker`, registering `ctx.codeRuntime`) and set `tools: { mode: code }` on the app. The model is then offered exactly ONE wire tool — `run_code` — plus a generated TypeScript SDK section declaring every other registered tool; it composes them by writing a program, each program tool call bridges back through the ordinary `tools/pre-execute`/`post-execute` pipeline one at a time and is logged as a `tool/code-dispatch` session event, and ONLY what the program prints or returns re-enters its context. (Flip the mode to `both` to offer native calls AND `run_code` side by side.) +[`code-mode.cordis.yml`](code-mode.cordis.yml) is this same tree flipped to [Code Mode](../../docs/rfc/implemented/feature/2026-06-15-code-mode.md): an include overlay over `./cordis.yml` whose two patches insert the worker-thread code runtime (`@deepseek-ai/dsh-code-runtime-worker`, registering `ctx.codeRuntime`) and set `tools: { mode: code }` on the app. The registry contributes exactly one reserved wire transport — `run_code` — plus a generated TypeScript SDK section declaring the visible end-capability tools. The model composes those capabilities by writing a program; each program call carries an immutable link to its enclosing transport, bridges back through pre-policy, monotonic guards, around dispatch, post-policy, and final-result observation one at a time, and is logged as a `tool/code-dispatch` session event. Only what the program prints or returns re-enters model context. (Flip the mode to `both` to offer native calls and `run_code` side by side.) ```sh pnpm run demo:code-mode # this overlay under the REPL (default UI) diff --git a/examples/coding-agent/tests/code-mode-keyless-smoke.e2e.ts b/examples/coding-agent/tests/code-mode-keyless-smoke.e2e.ts index 7894e9ff9f..718aa96721 100644 --- a/examples/coding-agent/tests/code-mode-keyless-smoke.e2e.ts +++ b/examples/coding-agent/tests/code-mode-keyless-smoke.e2e.ts @@ -26,6 +26,14 @@ const tsxLoader = fileURLToPath(import.meta.resolve('tsx')) // `paths` map; tsx searches UP from cwd, and we spawn from a temp dir outside // the repo, so point it at the repo tsconfig. const repoTsconfig = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url)) +// The real-API workflow runs up to 14 e2e files at once. Cold tsx/Loader +// startup can therefore outlive a tight smoke-test deadline before the child +// emits any output; 30s still detects a wedged process without confusing +// bounded CI contention with a lifecycle failure. +const PROCESS_TIMEOUT_MS = 30_000 +// Leave enough room for the process-owned timeout to report captured output +// before Vitest aborts the test itself. +const TEST_TIMEOUT_MS = PROCESS_TIMEOUT_MS + 15_000 let child: ChildProcessWithoutNullStreams | undefined let workdir: string | undefined @@ -67,8 +75,8 @@ async function bootAndEof(): Promise<{ stdout: string; code: number }> { const timer = setTimeout(() => { proc.kill('SIGKILL') - reject(new Error(`code-mode overlay did not exit within 10s. stdout:\n${stdout}\nstderr:\n${stderr}`)) - }, 10_000) + reject(new Error(`code-mode overlay did not exit within ${PROCESS_TIMEOUT_MS / 1_000}s. stdout:\n${stdout}\nstderr:\n${stderr}`)) + }, PROCESS_TIMEOUT_MS) proc.on('exit', (code) => { clearTimeout(timer) @@ -87,5 +95,5 @@ describe('code-mode overlay keyless smoke (real code-mode.cordis.yml via the Loa const { stdout, code } = await bootAndEof() expect(code).toBe(0) expect(stdout).toContain('code-mode agent ready.') - }, 15_000) + }, TEST_TIMEOUT_MS) }) diff --git a/examples/coding-agent/tests/keyless-smoke.e2e.ts b/examples/coding-agent/tests/keyless-smoke.e2e.ts index dba8b140c7..c6b9f930a8 100644 --- a/examples/coding-agent/tests/keyless-smoke.e2e.ts +++ b/examples/coding-agent/tests/keyless-smoke.e2e.ts @@ -35,6 +35,14 @@ const tsxLoader = fileURLToPath(import.meta.resolve('tsx')) // `paths` map; tsx searches UP from cwd, and we spawn from a temp dir outside // the repo, so point it at the repo tsconfig (root is four levels up). const repoTsconfig = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url)) +// The real-API workflow runs up to 14 e2e files at once. Cold tsx/Loader +// startup can therefore outlive a tight smoke-test deadline before the child +// emits any output; 30s still detects a wedged process without confusing +// bounded CI contention with a lifecycle failure. +const PROCESS_TIMEOUT_MS = 30_000 +// Leave enough room for the process-owned timeout to report captured output +// before Vitest aborts the test itself. +const TEST_TIMEOUT_MS = PROCESS_TIMEOUT_MS + 15_000 let child: ChildProcessWithoutNullStreams | undefined let workdir: string | undefined @@ -62,6 +70,8 @@ async function bootAndEof(): Promise<{ stdout: string; code: number }> { // A dummy key so llm-deepseek's apply() (key-PRESENT check only) boots. // No prompt is sent, so the adapter never streams — no network call. DEEPSEEK_API_KEY: 'keyless-smoke-no-call', + DSH_HOME: join(cwd, '.dsh'), + DSH_AGENTS_HOME: join(cwd, '.agents'), }, stdio: ['pipe', 'pipe', 'pipe'], }, @@ -76,8 +86,8 @@ async function bootAndEof(): Promise<{ stdout: string; code: number }> { const timer = setTimeout(() => { proc.kill('SIGKILL') - reject(new Error(`coding-agent did not exit within 10s. stdout:\n${stdout}\nstderr:\n${stderr}`)) - }, 10_000) + reject(new Error(`coding-agent did not exit within ${PROCESS_TIMEOUT_MS / 1_000}s. stdout:\n${stdout}\nstderr:\n${stderr}`)) + }, PROCESS_TIMEOUT_MS) proc.on('exit', (code) => { clearTimeout(timer) @@ -96,5 +106,5 @@ describe('coding-agent keyless smoke (real cordis.yml via the Loader)', () => { const { stdout, code } = await bootAndEof() expect(code).toBe(0) expect(stdout).toContain('agent REPL ready.') - }, 15_000) + }, TEST_TIMEOUT_MS) }) diff --git a/examples/coding-agent/tests/resume.e2e.ts b/examples/coding-agent/tests/resume.e2e.ts index fc216a9848..70382b4beb 100644 --- a/examples/coding-agent/tests/resume.e2e.ts +++ b/examples/coding-agent/tests/resume.e2e.ts @@ -39,11 +39,11 @@ describe.skipIf(!process.env.DEEPSEEK_API_KEY)('resume: continue a persisted ses // dispose the whole context (simulating process exit) so only the JSONL // log on disk survives. ctx = await codingHarness(process.cwd(), { persona: SYSTEM_PROMPT, persistenceRoot: root }) - const first = ctx.agents.create({ + const first = (await ctx.agents.create({ agentId: AgentId('resume-1'), sessionId: SESSION_ID, agentOptions: { model: 'deepseek-v4-flash' }, - }).agent as ReactLoopAgent + })).agent as ReactLoopAgent first.send([{ type: 'text', text: `Remember this code for later: ${SECRET}. Just acknowledge it.` }]) await waitForIdle(ctx, first) await ctx.fiber.dispose() diff --git a/examples/cordis-agent/tests/keyless-smoke.e2e.ts b/examples/cordis-agent/tests/keyless-smoke.e2e.ts index b37ea8d83e..d09cdf4728 100644 --- a/examples/cordis-agent/tests/keyless-smoke.e2e.ts +++ b/examples/cordis-agent/tests/keyless-smoke.e2e.ts @@ -29,6 +29,14 @@ const tsxLoader = fileURLToPath(import.meta.resolve('tsx')) // `paths` map; tsx searches UP from cwd, and we spawn from a temp dir outside // the repo, so point it at the repo tsconfig (root is three levels up). const repoTsconfig = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url)) +// The real-API workflow runs up to 14 e2e files at once. Cold tsx/Loader +// startup can therefore outlive a tight smoke-test deadline before the child +// emits any output; 30s still detects a wedged process without confusing +// bounded CI contention with a lifecycle failure. +const PROCESS_TIMEOUT_MS = 30_000 +// Leave enough room for the process-owned timeout to report captured output +// before Vitest aborts the test itself. +const TEST_TIMEOUT_MS = PROCESS_TIMEOUT_MS + 15_000 let child: ChildProcessWithoutNullStreams | undefined let workdir: string | undefined @@ -70,8 +78,8 @@ async function bootAndEof(): Promise<{ stdout: string; code: number }> { const timer = setTimeout(() => { proc.kill('SIGKILL') - reject(new Error(`cordis-agent did not exit within 10s. stdout:\n${stdout}\nstderr:\n${stderr}`)) - }, 10_000) + reject(new Error(`cordis-agent did not exit within ${PROCESS_TIMEOUT_MS / 1_000}s. stdout:\n${stdout}\nstderr:\n${stderr}`)) + }, PROCESS_TIMEOUT_MS) proc.on('exit', (code) => { clearTimeout(timer) @@ -90,5 +98,5 @@ describe('cordis-agent keyless smoke (real cordis.yml via the Loader)', () => { const { stdout, code } = await bootAndEof() expect(code).toBe(0) expect(stdout).toContain('cordis-agent ready.') - }, 15_000) + }, TEST_TIMEOUT_MS) }) diff --git a/examples/echo-agent/README.md b/examples/echo-agent/README.md index de42234ef1..dad0ce348a 100644 --- a/examples/echo-agent/README.md +++ b/examples/echo-agent/README.md @@ -31,4 +31,4 @@ node --expose-internals --import tsx packages/ui/stdio-agent/src/bin.ts examples Type a message and press Enter. "echo " triggers a tool call round-trip (the mock model requests the `echo` tool, which echoes the text uppercased, and the next model step acknowledges it). -The session is persisted under `.sessions/` relative to the directory you launch the demo from. `pnpm run demo:echo` runs from the repo root, so the logs land in `/.sessions/` (a session with no cwd goes in the `_no-cwd/` bucket, one `.jsonl` log per session). Clean up with: `rm -rf .sessions` +The session is persisted under `.sessions/` relative to the directory you launch the demo from. `pnpm run demo:echo` runs from the repo root, so the logs land in `/.sessions/cwd-/` (one `.jsonl` log per session). Clean up with: `rm -rf .sessions` diff --git a/examples/echo-agent/tests/echo.e2e.ts b/examples/echo-agent/tests/echo.e2e.ts index 01bb34bff0..5db02fb6be 100644 --- a/examples/echo-agent/tests/echo.e2e.ts +++ b/examples/echo-agent/tests/echo.e2e.ts @@ -37,6 +37,14 @@ const tsxLoader = fileURLToPath(import.meta.resolve('tsx')) // a temp cwd OUTSIDE the repo, so point tsx at the repo tsconfig explicitly // (repo root is four levels up from examples/echo-agent/tests). const repoTsconfig = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url)) +// The real-API workflow runs up to 14 e2e files at once. Cold tsx/Loader +// startup can therefore outlive a tight smoke-test deadline before the child +// emits any output; 30s still detects a wedged process without confusing +// bounded CI contention with a lifecycle failure. +const PROCESS_TIMEOUT_MS = 30_000 +// Leave enough room for the process-owned timeout to report captured output +// before Vitest aborts the test itself. +const TEST_TIMEOUT_MS = PROCESS_TIMEOUT_MS + 15_000 let child: ChildProcessWithoutNullStreams | undefined let workdir: string | undefined @@ -51,7 +59,7 @@ afterEach(async () => { /** * Boot echo-agent, write `lines` to its stdin, close stdin, and resolve with * the full stdout once the process exits (the stdio UI exits on EOF after the - * agent settles). Rejects on a non-zero exit or a 10s timeout. + * agent settles). Rejects on a non-zero exit or the process deadline. */ async function runEcho(lines: string[]): Promise<{ stdout: string; code: number }> { workdir = await mkdtemp(join(tmpdir(), 'echo-smoke-')) @@ -63,7 +71,16 @@ async function runEcho(lines: string[]): Promise<{ stdout: string; code: number // requires it (mirrors the `demo:echo` script). The whole point is to boot // the example EXACTLY as it really runs, through the bin + Loader. ['--expose-internals', '--import', tsxLoader, binScript, configPath], - { cwd, env: { ...process.env, TSX_TSCONFIG_PATH: repoTsconfig }, stdio: ['pipe', 'pipe', 'pipe'] }, + { + cwd, + env: { + ...process.env, + TSX_TSCONFIG_PATH: repoTsconfig, + DSH_HOME: join(cwd, '.dsh'), + DSH_AGENTS_HOME: join(cwd, '.agents'), + }, + stdio: ['pipe', 'pipe', 'pipe'], + }, ) child = proc let stdout = '' @@ -75,8 +92,8 @@ async function runEcho(lines: string[]): Promise<{ stdout: string; code: number const timer = setTimeout(() => { proc.kill('SIGKILL') - reject(new Error(`echo-agent did not exit within 10s. stdout:\n${stdout}\nstderr:\n${stderr}`)) - }, 10_000) + reject(new Error(`echo-agent did not exit within ${PROCESS_TIMEOUT_MS / 1_000}s. stdout:\n${stdout}\nstderr:\n${stderr}`)) + }, PROCESS_TIMEOUT_MS) proc.on('exit', (code) => { clearTimeout(timer) @@ -96,19 +113,19 @@ describe('echo-agent keyless smoke (real cordis.yml via the Loader)', () => { const { stdout, code } = await runEcho([]) expect(code).toBe(0) expect(stdout).toContain('echo-agent ready.') - }, 15_000) + }, TEST_TIMEOUT_MS) it('runs the echo tool round-trip for an "echo …" line', async () => { const { stdout } = await runEcho(['echo hello world']) // mock-llm.ts emits a tool-call for the echo tool; echo-tool.ts uppercases. expect(stdout).toContain('[tool call] echo') expect(stdout).toContain('[tool result] ECHO: HELLO WORLD') - }, 15_000) + }, TEST_TIMEOUT_MS) it('streams a direct canned reply for a non-echo line', async () => { const { stdout } = await runEcho(['just chatting']) // The direct-response branch of mock-llm.ts quotes the input back. expect(stdout).toContain('just chatting') expect(stdout).not.toContain('[tool call]') - }, 15_000) + }, TEST_TIMEOUT_MS) }) diff --git a/examples/sandbox-acp-agent/README.md b/examples/sandbox-acp-agent/README.md new file mode 100644 index 0000000000..bada288bde --- /dev/null +++ b/examples/sandbox-acp-agent/README.md @@ -0,0 +1,16 @@ +# sandbox-acp-agent + +The coding agent with its bash executor swapped for the sandbox stack ([`@deepseek-ai/dsh-sandbox-local`](../../packages/sandbox/sandbox-local/) + [`@deepseek-ai/dsh-bash-sandbox`](../../packages/bash/bash-sandbox/) — the one-entry executor swap the `ctx.bash` capability seam exists for), served over the **Agent Client Protocol**, plus [`@deepseek-ai/dsh-user-approval`](../../packages/ui/user-approval/) — which makes this the first composition where the approval loop is LIVE end to end: bash runs under `read-only`, a denial comes back as the structured marker, the model retries once with `sandbox_permissions` + `justification`, the ACP bridge's answerer turns that ask into a `session/request_permission` prompt in your editor, and "Allow once" runs exactly that command under the wider mode ([sandbox RFC § Escalation](../../docs/rfc/implemented/feature/2026-07-06-sandbox.md)). + +```sh +pnpm run demo:sandbox-acp # needs DEEPSEEK_API_KEY; drive it from Zed or any ACP client +``` + +Zed setup is the same as [acp-agent](../acp-agent/README.md) with this example's command; only the leaf `cordis.yml` differs (the sandbox stack + the approval entry in place of the local bash executor and the extra tool stacks). + +- **Every approval is one-shot** (`Allow once` / `Reject` — no `allow_always`: the harness has no grant storage yet), and a dismissed prompt or a rejected ask fails closed with its own error text; so does every ask when no editor is attached to answer. +- **Two session config options are live** ([sandbox RFC § Per-session mode switching](../../docs/rfc/implemented/feature/2026-07-06-sandbox.md)): a capable client shows `Sandbox` (`read-only`/`workspace-write`/`danger-full-access`) and `Approvals` (`ask`/`never`) selectors per session — a switch is one log-only event on that session's log and execution follows it; the sandbox mode is deliberately NOT stated in the prompt or narrated (the model learns the boundary from the denial marker — behavior, not belief), while an approval switch to `never` is stated and narrated; a resumed session reports its overrides back on `session/load`. +- **The write boundary is config-fixed**: an escalated `workspace-write` run may write under the launch directory (`workspaceRoot: process.cwd()`) plus the platform temp area — a per-session root is config-phase future work in the [sandbox RFC](../../docs/rfc/implemented/feature/2026-07-06-sandbox.md). +- **No usable runner fails closed per command** (structured `SANDBOX_UNAVAILABLE`), and the filesystem tools stay unloaded for the same reason as `sandbox-agent`: they would bypass the bash sandbox. + +Tests: `tests/escalation.e2e.ts` — keyless, it boots the real `cordis.yml` through the Loader as an ACP subprocess, proves the whole tree (sandbox executor + approval service + bridge) initializes and opens a session, and drives the config options end to end (both advertised with composition currents, switches honored and echoed as complete state, out-of-vocabulary values rejected); with a key and a usable runner, a scripted ACP client plays the human — the real model gets denied, escalates, the client answers `allow-once`, and the retried write must land on disk. `tests/acp.snapshot.ts` (the [shared snapshot kit](../../packages/support/acp-snapshot/) over this composition's `cordis.snapshot.yml` replay overlay) pins four scenarios as committed wire bytes: the keyless config-option exchange, the recorded `mode-switching` arc (the suite's pinned header — both switches, their prompt-section deltas, one "changed by the user" notice per knob, and a confined write landing under the switched mode), and both recorded escalation branches (`session/request_permission` answered allow-once / reject-once). Replay re-executes every recorded bash call under the host's real runner — Seatbelt works out of the box on macOS; on Linux install bubblewrap (or build the Landlock launcher) first, exactly what ci.yml's snapshot lane does. No fixture carries a real denial: denial stderr is backend dialect and would pin a fixture to its recording platform (the rationale comment atop the suite file). diff --git a/examples/sandbox-acp-agent/cordis.snapshot.yml b/examples/sandbox-acp-agent/cordis.snapshot.yml new file mode 100644 index 0000000000..b5f8ac73ed --- /dev/null +++ b/examples/sandbox-acp-agent/cordis.snapshot.yml @@ -0,0 +1,30 @@ +# Snapshot-test REPLAY overlay for the sandboxed composition: the SAME app +# tree as cordis.yml, derived from it by an include — the one difference is +# the model backend. A keyless replay run cannot boot the real adapter +# (llm-deepseek's apply() throws without DEEPSEEK_API_KEY), so the include +# patches the live tree at load time: the llm-deepseek entry is disabled by +# id, and the llm-replay entry (which serves a recorded session JSONL — no +# API key, no network) is inserted. Every other entry — the sandbox provider, +# the confined bash executor, the approval seam, the app — IS the live tree. +# The sandbox provider probes for a platform runner per EXECUTION, not at +# boot, so a protocol-only scenario (session config options) replays on hosts +# with no runner at all. +# +# The dsh-acp-agent bin selects this file for DSH_SNAPSHOT=replay (the +# sibling-swap of whatever config path it was handed). The replay fixture +# path comes from $DSH_SNAPSHOT_FILE, set by the snapshot harness. stdout +# stays reserved for the ACP JSON-RPC protocol. +- id: base + name: '@cordisjs/plugin-include' + config: + path: ./cordis.yml + patches: + # The name is an assertion, not an override: the include skips the patch + # (warning) when the id points at a different plugin, so this can never + # disable the wrong entry. + - id: llm-deepseek + name: '@deepseek-ai/dsh-llm-deepseek' + disabled: true + - insert: + - id: llm-replay + name: '@deepseek-ai/dsh-llm-replay' diff --git a/examples/sandbox-acp-agent/cordis.yml b/examples/sandbox-acp-agent/cordis.yml new file mode 100644 index 0000000000..d02253342e --- /dev/null +++ b/examples/sandbox-acp-agent/cordis.yml @@ -0,0 +1,60 @@ +# The sandbox-acp-agent plugin tree: the sandboxed coding agent served over the +# Agent Client Protocol, with the approval seam composed — the first LIVE +# approval composition. A sandbox denial escalated by the model +# (sandbox_permissions + justification) reaches the EDITOR as a +# session/request_permission prompt through the ACP bridge's answerer, and an +# "Allow once" runs exactly that command under the wider mode. +# +# CRITICAL: this tree loads NO stdout logger and NO hmr — stdout is reserved +# for the ACP JSON-RPC protocol (a property of @deepseek-ai/dsh-acp-agent, +# same as examples/acp-agent). +# +# Requires DEEPSEEK_API_KEY (and optionally DEEPSEEK_BASE_URL) — the +# dsh-acp-agent bin loads the gitignored repo-root .env first (on STDERR only). + +# The DeepSeek adapter. +- id: llm-deepseek + name: '@deepseek-ai/dsh-llm-deepseek' + config: + apiKey: !!js process.env.DEEPSEEK_API_KEY + baseURL: !!js process.env.DEEPSEEK_BASE_URL + models: + - deepseek-v4-flash + +# The sandbox stack: the platform-runner provider (bwrap → per-platform +# Landlock launcher → Seatbelt, functionally probed), then the confined bash executor. +# read-only is the fail-safe default; the write boundary for an escalated +# workspace-write run is workspaceRoot + the platform's temp area. NOTE: the +# workspace root is CONFIG-FIXED for the executor's lifetime (the launch dir +# here), while each ACP session has its own cwd — a per-session root is config- +# phase future work in the sandbox RFC. +- id: sandbox + name: '@deepseek-ai/dsh-sandbox-local' +- id: bash + name: '@deepseek-ai/dsh-bash-sandbox' + config: + mode: read-only + workspaceRoot: !!js process.cwd() + +# The approval seam (ctx.approval — mechanism only, no config): with it +# mounted, the bash tool's escalation gate has a channel, and the ACP bridge +# inside dsh-acp-agent answers for the sessions it owns by prompting the +# editor. Without an editor attached nothing can answer, and every ask fails +# closed. +- id: approval + name: '@deepseek-ai/dsh-user-approval' + +# The ACP server app: the agent-core spine + JSONL persistence + the ACP +# bridge (whose approval answerer completes the loop). +- id: acp-agent + name: '@deepseek-ai/dsh-acp-agent' + config: + model: deepseek-v4-flash + # Persistence root: $DSH_SNAPSHOT_SESSIONS_ROOT when the snapshot harness + # sets it (so a record run's logs land where the harness harvests them), + # else the local ./.sessions default. + persistenceRoot: !!js process.env.DSH_SNAPSHOT_SESSIONS_ROOT ?? './.sessions' + persona: | + You are a coding assistant powered by the {{model}} model. Your working directory is {{cwd}}. Your bash tool runs under a file sandbox — a `[sandbox: file access denied …]` result is policy, not a command bug. + + Verify your work by running the code or tests. Keep answers brief and factual. diff --git a/examples/sandbox-acp-agent/package.json b/examples/sandbox-acp-agent/package.json new file mode 100644 index 0000000000..5a4c9a989e --- /dev/null +++ b/examples/sandbox-acp-agent/package.json @@ -0,0 +1,7 @@ +{ + "name": "sandbox-acp-agent-example", + "description": "Runnable demo: the sandboxed coding agent as an ACP server, with sandbox-escalation approval prompts answered by the editor", + "private": true, + "version": "0.0.1", + "type": "module" +} diff --git a/examples/sandbox-acp-agent/tests/acp.snapshot.ts b/examples/sandbox-acp-agent/tests/acp.snapshot.ts new file mode 100644 index 0000000000..418b8068d3 --- /dev/null +++ b/examples/sandbox-acp-agent/tests/acp.snapshot.ts @@ -0,0 +1,82 @@ +import { dirname, join } from 'node:path' +import { fileURLToPath } from 'node:url' +import { defineAcpSnapshotSuite, type Scenario, type SnapshotSuiteOptions } from '@deepseek-ai/dsh-acp-snapshot' + +/** + * Snapshot suite for the SANDBOXED composition (`../cordis.yml`, swapped to + * the sibling `cordis.snapshot.yml` replay overlay by the bin under + * `DSH_SNAPSHOT=replay`). Replay swaps only the MODEL for the recorded + * transcript — every bash call re-executes for real under the host's actual + * runner (Seatbelt on macOS, bwrap on Linux CI: ci.yml's snapshot lane + * installs bubblewrap for exactly this), so the recorded scenarios double as + * cross-backend confinement regression: an allowed command a runner change + * starts denying fails replay outright. Their commands are limited to + * `cat`/`printf` shapes whose bytes are identical across those backends and + * across GNU/BSD userlands. + * + * Deliberately ABSENT: a scenario whose transcript carries a real sandbox + * DENIAL. The harness-authored `[sandbox: file access denied …]` marker is + * byte-stable, but the denied command's own stderr is the backend's dialect + * (bwrap EROFS "Read-only file system", Landlock EACCES "Permission + * denied", Seatbelt EPERM "Operation not permitted", GNU vs BSD phrasing on + * top), and stderr reaches both compared surfaces — such a fixture replays + * only on the platform that recorded it. The denial→marker path stays on + * dsh-tool-bash's unit tests and the real-kernel sandbox e2e legs + * (.github/workflows/sandbox.yml); the escalation scenarios below sidestep + * it by having the USER assert the prior denial, so the recorded model + * escalates without a platform-variant denial in the log. + */ +const SCENARIOS: Scenario[] = [ + // Protocol-only (keyless, authored): the session config-option surface + // this composition adds — both advertised selects on session/new, the + // complete refreshed state every session/set_config_option answers with, + // and both rejection shapes — as committed wire bytes. No bash runs, so + // this one still replays on runner-less hosts. + { name: 'config-options', hasModelTurn: false, recorded: false }, + // The runtime mode-switching arc, and NECESSARILY the pinned-header + // scenario: an approval-policy switch rewrites its prompt section, and the + // resulting request/header-delta is legal only in the pinning scenario + // (the factory's uniformity guard). The pin commits this composition's + // full header — persona, tool schemas WITH the escalation fields — plus + // the approval delta and its "changed by the user" notice verbatim. The + // SANDBOX switch is deliberately silent (no section, no notice — the + // sandbox RFC's visibility asymmetry): the recorded arc proves it by + // BEHAVIOR, a confined write landing under the switched mode with no + // header change. + { name: 'mode-switching', hasModelTurn: true, recorded: true, pinsHeader: true, expectedHeaderDeltas: 1 }, + // The approval wire end-to-end, under the DEFAULT read-only/ask (a switch + // would emit a header-delta the uniformity guard forbids here): the + // escalating bash call streams, session/request_permission attaches to it + // (allow-once / reject-once), and the scripted answer drives each branch — + // an approved run executes CONFINED under the granted workspace-write; a + // rejected one executes nothing and fails with the deterministic + // rejection text. + { name: 'escalation-approved', hasModelTurn: true, recorded: true }, + { name: 'escalation-rejected', hasModelTurn: true, recorded: true }, +] + +function snapshotModeFromEnv(value: string | undefined): SnapshotSuiteOptions['mode'] { + switch (value) { + case undefined: + case '': + case 'replay': + return 'replay' + case 'record': + return 'record' + case 'refresh': + return 'refresh' + default: + throw new Error(`unknown DSH_SNAPSHOT mode: ${value}`) + } +} + +defineAcpSnapshotSuite({ + agent: { + binScript: fileURLToPath(new URL('../../../packages/ui/acp-agent/src/bin.ts', import.meta.url)), + configPath: fileURLToPath(new URL('../cordis.yml', import.meta.url)), + tsconfigPath: fileURLToPath(new URL('../../../tsconfig.json', import.meta.url)), + }, + snapshotsDir: join(dirname(fileURLToPath(import.meta.url)), 'snapshots'), + scenarios: SCENARIOS, + mode: snapshotModeFromEnv(process.env.DSH_SNAPSHOT), +}) diff --git a/examples/sandbox-acp-agent/tests/escalation.e2e.ts b/examples/sandbox-acp-agent/tests/escalation.e2e.ts new file mode 100644 index 0000000000..93915717a1 --- /dev/null +++ b/examples/sandbox-acp-agent/tests/escalation.e2e.ts @@ -0,0 +1,213 @@ +import { spawn, spawnSync, type ChildProcessWithoutNullStreams } from 'node:child_process' +import { Readable, Writable } from 'node:stream' +import { mkdtemp, readFile, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { fileURLToPath } from 'node:url' +import { afterEach, describe, expect, it } from 'vitest' +import { + ClientSideConnection, + ndJsonStream, + PROTOCOL_VERSION, + type Agent as AcpAgent, + type Client, + type RequestPermissionRequest, + type RequestPermissionResponse, + type SessionNotification, +} from '@agentclientprotocol/sdk' + +/** + * examples/sandbox-acp-agent end to end. + * + * Keyless smoke: boot the REAL `cordis.yml` through the `dsh-acp-agent` bin as + * an ACP subprocess and drive initialize + session/new — the real-Loader-path + * guard (postmortem 0001) for THIS tree's export shapes, which now include the + * sandbox executor AND the approval service. No prompt is sent, so neither the + * model nor a sandbox runner is ever exercised. + * + * With-key escalation flow (self-skips without DEEPSEEK_API_KEY or a usable + * platform runner): a scripted ACP client plays the human. The real model is + * denied under `read-only`, escalates with `sandbox_permissions` + + * `justification`, the bridge prompts THIS client over + * `session/request_permission`, the client answers `allow-once`, and the + * retried write must land ON DISK (world-verified). The session cwd is a temp + * dir under the platform temp area, which `workspace-write` grants — so either + * escalation target the model picks can land the write. + */ + +const binScript = fileURLToPath(new URL('../../../packages/ui/acp-agent/src/bin.ts', import.meta.url)) +const configPath = fileURLToPath(new URL('../cordis.yml', import.meta.url)) +const tsxLoader = fileURLToPath(import.meta.resolve('tsx')) +// The subprocess runs from a temp cwd OUTSIDE the repo; point tsx at the repo +// tsconfig so the unbuilt `paths` map resolves (see examples/AGENTS.md). +const repoTsconfig = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url)) + +// A usable confining runner, probed the same way the executor suites do: +// bwrap on Linux, Seatbelt's sandbox-exec on macOS. Without one the strict +// attempt would fail closed (SANDBOX_UNAVAILABLE) instead of producing the +// denial this flow starts from. +const hasBwrap = spawnSync('bwrap', ['--ro-bind', '/', '/', '--dev', '/dev', '--proc', '/proc', '--die-with-parent', '--', 'true'], { + timeout: 5_000, + stdio: 'ignore', +}).status === 0 +const hasSeatbelt = process.platform === 'darwin' && spawnSync('sandbox-exec', ['-p', '(version 1)(allow default)', 'true'], { + timeout: 5_000, + stdio: 'ignore', +}).status === 0 +const hasRunner = hasBwrap || hasSeatbelt + +interface Spawned { + child: ChildProcessWithoutNullStreams + client: ClientSideConnection + updates: SessionNotification['update'][] + permissionRequests: RequestPermissionRequest[] + stderr: string[] +} + +/** Boot the example as an ACP subprocess; the scripted client answers every permission prompt with `answer`. */ +function spawnSandboxAcpAgent(cwd: string, answer: 'allow-once' | 'reject-once'): Spawned { + const child = spawn( + process.execPath, + ['--import', tsxLoader, binScript, configPath], + { + cwd, + // A dummy key lets the deepseek adapter boot keyless (presence-checked at + // apply, used only on a real model call); the with-key tests carry the + // real key, so the fallback is inert there. + env: { ...process.env, DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY ?? 'sk-dummy-for-boot', TSX_TSCONFIG_PATH: repoTsconfig }, + stdio: ['pipe', 'pipe', 'pipe'], + }, + ) + const stderr: string[] = [] + child.stderr.setEncoding('utf8') + child.stderr.on('data', (chunk: string) => stderr.push(chunk)) + + const updates: SessionNotification['update'][] = [] + const permissionRequests: RequestPermissionRequest[] = [] + const stream = ndJsonStream( + Writable.toWeb(child.stdin) as WritableStream, + Readable.toWeb(child.stdout) as ReadableStream, + ) + const makeClient = (_agent: AcpAgent): Client => ({ + sessionUpdate(params: SessionNotification): Promise { + updates.push(params.update) + return Promise.resolve() + }, + requestPermission(params: RequestPermissionRequest): Promise { + permissionRequests.push(params) + const option = params.options.find(o => o.optionId === answer) + // The scripted human: pick the requested option when the prompt offers + // it; an unexpected prompt shape cancels (fail closed, never grants). + if (option === undefined) return Promise.resolve({ outcome: { outcome: 'cancelled' } }) + return Promise.resolve({ outcome: { outcome: 'selected', optionId: option.optionId } }) + }, + }) + const client = new ClientSideConnection(makeClient, stream) + return { child, client, updates, permissionRequests, stderr } +} + +let spawned: Spawned | undefined +let workdir: string | undefined + +afterEach(async () => { + if (spawned !== undefined && spawned.child.exitCode === null) spawned.child.kill('SIGKILL') + spawned = undefined + if (workdir !== undefined) await rm(workdir, { recursive: true, force: true }) + workdir = undefined +}) + +describe('sandbox-acp-agent keyless smoke (real cordis.yml via the Loader)', () => { + it('boots the tree — sandbox executor + approval service + bridge — and opens a session', async () => { + workdir = await mkdtemp(join(tmpdir(), 'sandbox-acp-smoke-')) + spawned = spawnSandboxAcpAgent(workdir, 'reject-once') + const { client } = spawned + // A dummy key boots the adapter; no prompt is ever sent, so no model call + // and no sandbox runner probe happen. This drives the fiber tree the same + // way an editor would, which is what catches a broken export/inject shape. + const init = await client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} }) + expect(init.protocolVersion).toBe(PROTOCOL_VERSION) + const { sessionId } = await client.newSession({ cwd: workdir, mcpServers: [] }) + expect(sessionId.length).toBeGreaterThan(0) + }, 30_000) + + it('advertises both session config options and honors a switch end to end (no key, no model)', async () => { + workdir = await mkdtemp(join(tmpdir(), 'sandbox-acp-config-')) + spawned = spawnSandboxAcpAgent(workdir, 'reject-once') + const { client } = spawned + await client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} }) + // This tree composes bash-sandbox (mode: read-only) + approval → both + // knobs advertise, currents from composition config. + const created = await client.newSession({ cwd: workdir, mcpServers: [] }) + const advertised = created.configOptions ?? [] + expect(advertised.map(option => [option.id, 'currentValue' in option ? option.currentValue : undefined])) + .toEqual([['sandbox-mode', 'read-only'], ['approval-policy', 'ask']]) + // A switch responds with the COMPLETE refreshed state (the spec contract), + // and the new currents survive in the response of a second switch. + const afterSandbox = await client.setSessionConfigOption({ + sessionId: created.sessionId, configId: 'sandbox-mode', value: 'workspace-write', + }) + const afterApproval = await client.setSessionConfigOption({ + sessionId: created.sessionId, configId: 'approval-policy', value: 'never', + }) + const currents = (afterApproval.configOptions ?? []).map(option => + [option.id, 'currentValue' in option ? option.currentValue : undefined]) + expect(afterSandbox.configOptions?.find(option => option.id === 'sandbox-mode')) + .toMatchObject({ currentValue: 'workspace-write' }) + expect(currents).toEqual([['sandbox-mode', 'workspace-write'], ['approval-policy', 'never']]) + // An out-of-vocabulary value is a protocol error, never a silent default. + await expect(client.setSessionConfigOption({ + sessionId: created.sessionId, configId: 'sandbox-mode', value: 'yolo', + })).rejects.toThrow(/unknown sandbox-mode value/) + }, 30_000) +}) + +describe.skipIf(!process.env.DEEPSEEK_API_KEY || !hasRunner)('sandbox-acp-agent e2e: the live approval loop', () => { + it('denial → model escalation → editor prompt → allow-once → the retried write lands on disk', async () => { + workdir = await mkdtemp(join(tmpdir(), 'sandbox-acp-e2e-')) + spawned = spawnSandboxAcpAgent(workdir, 'allow-once') + const { client, permissionRequests } = spawned + + await client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} }) + const { sessionId } = await client.newSession({ cwd: workdir, mcpServers: [] }) + const res = await client.prompt({ + sessionId, + prompt: [{ type: 'text', text: `Use the bash tool to create the file ${workdir}/escalated.txt containing exactly "ACP_ESCALATION_OK". ` + + 'If the sandbox denies it, retry once with sandbox_permissions and a one-sentence justification, then stop.' }], + }) + expect(['end_turn', 'max_tokens']).toContain(res.stopReason) + + // The WORLD: the approved escalated retry landed the write read-only denied. + const proof = await readFile(join(workdir, 'escalated.txt'), 'utf8') + expect(proof).toContain('ACP_ESCALATION_OK') + + // The CHANNEL: the grant came through a real session/request_permission + // prompt attached to the escalating tool call, offering exactly the + // one-shot options. + expect(permissionRequests.length).toBeGreaterThan(0) + const prompt = permissionRequests[0] + if (prompt === undefined) throw new Error('expected a permission request') + expect(prompt.sessionId).toBe(sessionId) + expect(typeof prompt.toolCall.toolCallId).toBe('string') + expect(prompt.options.map(o => o.optionId).sort()).toEqual(['allow-once', 'reject-once']) + }, 240_000) + + it('a rejected escalation stays denied: no write lands, the turn still ends', async () => { + workdir = await mkdtemp(join(tmpdir(), 'sandbox-acp-e2e-')) + spawned = spawnSandboxAcpAgent(workdir, 'reject-once') + const { client, permissionRequests } = spawned + + await client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} }) + const { sessionId } = await client.newSession({ cwd: workdir, mcpServers: [] }) + const res = await client.prompt({ + sessionId, + prompt: [{ type: 'text', text: `Use the bash tool to create the file ${workdir}/refused.txt containing "NO". ` + + 'If the sandbox denies it, retry once with sandbox_permissions and a one-sentence justification. If that is rejected, stop and say so.' }], + }) + expect(['end_turn', 'max_tokens']).toContain(res.stopReason) + + // The WORLD: rejected means the file never appeared. + await expect(readFile(join(workdir, 'refused.txt'), 'utf8')).rejects.toThrow() + // And the rejection really flowed through a prompt (not a missing channel). + expect(permissionRequests.length).toBeGreaterThan(0) + }, 240_000) +}) diff --git a/examples/sandbox-acp-agent/tests/snapshots/config-options/input.json b/examples/sandbox-acp-agent/tests/snapshots/config-options/input.json new file mode 100644 index 0000000000..043019b659 --- /dev/null +++ b/examples/sandbox-acp-agent/tests/snapshots/config-options/input.json @@ -0,0 +1,10 @@ +{ + "steps": [ + { "op": "initialize" }, + { "op": "newSession" }, + { "op": "setConfigOption", "configId": "sandbox-mode", "value": "workspace-write" }, + { "op": "setConfigOption", "configId": "approval-policy", "value": "never" }, + { "op": "setConfigOptionExpectError", "configId": "sandbox-mode", "value": "yolo" }, + { "op": "setConfigOptionExpectError", "configId": "reasoning-effort", "value": "max" } + ] +} diff --git a/examples/sandbox-acp-agent/tests/snapshots/config-options/session.jsonl b/examples/sandbox-acp-agent/tests/snapshots/config-options/session.jsonl new file mode 100644 index 0000000000..a6f73319bc --- /dev/null +++ b/examples/sandbox-acp-agent/tests/snapshots/config-options/session.jsonl @@ -0,0 +1 @@ +{"type":"session","version":0,"id":"00000000-0000-0000-0000-000000000000","createdAt":0} diff --git a/examples/sandbox-acp-agent/tests/snapshots/config-options/stdout.golden.jsonl b/examples/sandbox-acp-agent/tests/snapshots/config-options/stdout.golden.jsonl new file mode 100644 index 0000000000..3fab1e81fa --- /dev/null +++ b/examples/sandbox-acp-agent/tests/snapshots/config-options/stdout.golden.jsonl @@ -0,0 +1,6 @@ +{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":1,"agentInfo":{"name":"deepseek-harness-acp","version":"0.0.1"},"agentCapabilities":{"loadSession":true,"promptCapabilities":{"image":false,"audio":false,"embeddedContext":false}},"authMethods":[]}} +{"jsonrpc":"2.0","id":2,"result":{"sessionId":"{{sessionId}}","configOptions":[{"id":"sandbox-mode","name":"Sandbox","description":"The file sandbox mode bash commands in this session run under.","category":"mode","type":"select","currentValue":"read-only","options":[{"value":"read-only","name":"read-only"},{"value":"workspace-write","name":"workspace-write"},{"value":"danger-full-access","name":"danger-full-access"}]},{"id":"approval-policy","name":"Approvals","description":"ask: permission prompts reach you; never: they are rejected automatically.","type":"select","currentValue":"ask","options":[{"value":"ask","name":"ask"},{"value":"never","name":"never"}]}]}} +{"jsonrpc":"2.0","id":3,"result":{"configOptions":[{"id":"sandbox-mode","name":"Sandbox","description":"The file sandbox mode bash commands in this session run under.","category":"mode","type":"select","currentValue":"workspace-write","options":[{"value":"read-only","name":"read-only"},{"value":"workspace-write","name":"workspace-write"},{"value":"danger-full-access","name":"danger-full-access"}]},{"id":"approval-policy","name":"Approvals","description":"ask: permission prompts reach you; never: they are rejected automatically.","type":"select","currentValue":"ask","options":[{"value":"ask","name":"ask"},{"value":"never","name":"never"}]}]}} +{"jsonrpc":"2.0","id":4,"result":{"configOptions":[{"id":"sandbox-mode","name":"Sandbox","description":"The file sandbox mode bash commands in this session run under.","category":"mode","type":"select","currentValue":"workspace-write","options":[{"value":"read-only","name":"read-only"},{"value":"workspace-write","name":"workspace-write"},{"value":"danger-full-access","name":"danger-full-access"}]},{"id":"approval-policy","name":"Approvals","description":"ask: permission prompts reach you; never: they are rejected automatically.","type":"select","currentValue":"never","options":[{"value":"ask","name":"ask"},{"value":"never","name":"never"}]}]}} +{"jsonrpc":"2.0","id":5,"error":{"code":-32602,"message":"Invalid params: unknown sandbox-mode value \"yolo\""}} +{"jsonrpc":"2.0","id":6,"error":{"code":-32602,"message":"Invalid params: unknown config option \"reasoning-effort\""}} diff --git a/examples/sandbox-acp-agent/tests/snapshots/escalation-approved/input.json b/examples/sandbox-acp-agent/tests/snapshots/escalation-approved/input.json new file mode 100644 index 0000000000..4cfa610f55 --- /dev/null +++ b/examples/sandbox-acp-agent/tests/snapshots/escalation-approved/input.json @@ -0,0 +1,10 @@ +{ + "steps": [ + { "op": "initialize" }, + { "op": "newSession" }, + { "op": "prompt", "text": "The sandbox already denied writing escalated.txt in this workspace earlier. Retry it now exactly once: one single bash call with the command printf 'escalated\\n' > escalated.txt && cat escalated.txt, with sandbox_permissions set to workspace-write and the justification 'the user asked to write escalated.txt in the workspace'. Do not run it without sandbox_permissions first. I will approve the permission prompt. After the result, reply with the single word DONE and stop." } + ], + "permissionAnswers": [ + { "kind": "allow_once" } + ] +} diff --git a/examples/sandbox-acp-agent/tests/snapshots/escalation-approved/session.jsonl b/examples/sandbox-acp-agent/tests/snapshots/escalation-approved/session.jsonl new file mode 100644 index 0000000000..2ac9d27044 --- /dev/null +++ b/examples/sandbox-acp-agent/tests/snapshots/escalation-approved/session.jsonl @@ -0,0 +1,149 @@ +{"type":"session","version":0,"id":"8f399407-f505-45e5-9058-0994ff7b0865","createdAt":1783486769425,"cwd":"/var/folders/2g/b32ct0qn1d728l_v6tdkjytr0000gn/T/acp-snap-cwd-xKPr5d"} +{"type":"turn/start","seq":0,"time":1783486769426,"data":{"turn":1,"trigger":{"kind":"message","source":{"kind":"user"}}}} +{"type":"user/message","seq":1,"time":1783486769426,"data":{"content":[{"type":"text","text":"The sandbox already denied writing escalated.txt in this workspace earlier. Retry it now exactly once: one single bash call with the command printf 'escalated\\n' > escalated.txt && cat escalated.txt, with sandbox_permissions set to workspace-write and the justification 'the user asked to write escalated.txt in the workspace'. Do not run it without sandbox_permissions first. I will approve the permission prompt. After the result, reply with the single word DONE and stop."}],"source":{"kind":"user"}},"surfaceOp":"append"} +{"type":"step/start","seq":2,"time":1783486769427,"data":{"turn":1,"step":1}} +{"type":"request/header","seq":3,"time":1783486769427,"data":{"header":{"config":{"model":"deepseek-v4-flash"},"system":"{{system}}","tools":"{{tools}}"},"reason":"initial"}} +{"type":"assistant/chunk","seq":4,"time":1783486770051,"data":{"turn":1,"step":1,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}} +{"type":"assistant/chunk","seq":5,"time":1783486770052,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"The"}}} +{"type":"assistant/chunk","seq":6,"time":1783486770179,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" user"}}} +{"type":"assistant/chunk","seq":7,"time":1783486770212,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" wants"}}} +{"type":"assistant/chunk","seq":8,"time":1783486770213,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" me"}}} +{"type":"assistant/chunk","seq":9,"time":1783486770214,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" to"}}} +{"type":"assistant/chunk","seq":10,"time":1783486770214,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" ret"}}} +{"type":"assistant/chunk","seq":11,"time":1783486770215,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"ry"}}} +{"type":"assistant/chunk","seq":12,"time":1783486770215,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" the"}}} +{"type":"assistant/chunk","seq":13,"time":1783486770242,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" command"}}} +{"type":"assistant/chunk","seq":14,"time":1783486770243,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" with"}}} +{"type":"assistant/chunk","seq":15,"time":1783486770277,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" sand"}}} +{"type":"assistant/chunk","seq":16,"time":1783486770278,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"box"}}} +{"type":"assistant/chunk","seq":17,"time":1783486770278,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"_per"}}} +{"type":"assistant/chunk","seq":18,"time":1783486770306,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"missions"}}} +{"type":"assistant/chunk","seq":19,"time":1783486770307,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" set"}}} +{"type":"assistant/chunk","seq":20,"time":1783486770335,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" to"}}} +{"type":"assistant/chunk","seq":21,"time":1783486770337,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" workspace"}}} +{"type":"assistant/chunk","seq":22,"time":1783486770338,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"-w"}}} +{"type":"assistant/chunk","seq":23,"time":1783486770338,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"rite"}}} +{"type":"assistant/chunk","seq":24,"time":1783486770338,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":25,"time":1783486770368,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" They"}}} +{"type":"assistant/chunk","seq":26,"time":1783486770369,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" explicitly"}}} +{"type":"assistant/chunk","seq":27,"time":1783486770430,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" said"}}} +{"type":"assistant/chunk","seq":28,"time":1783486770431,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" they"}}} +{"type":"assistant/chunk","seq":29,"time":1783486770431,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" will"}}} +{"type":"assistant/chunk","seq":30,"time":1783486770458,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" approve"}}} +{"type":"assistant/chunk","seq":31,"time":1783486770459,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" the"}}} +{"type":"assistant/chunk","seq":32,"time":1783486770459,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" permission"}}} +{"type":"assistant/chunk","seq":33,"time":1783486770474,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" prompt"}}} +{"type":"assistant/chunk","seq":34,"time":1783486770474,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":35,"time":1783486770475,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" Let"}}} +{"type":"assistant/chunk","seq":36,"time":1783486770475,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" me"}}} +{"type":"assistant/chunk","seq":37,"time":1783486770475,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" proceed"}}} +{"type":"assistant/chunk","seq":38,"time":1783486770518,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":39,"time":1783486770606,"data":{"turn":1,"step":1,"chunk":{"type":"block-start","index":1,"blockType":"tool-call"}}} +{"type":"assistant/chunk","seq":40,"time":1783486770606,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":""}}} +{"type":"assistant/chunk","seq":41,"time":1783486770606,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"{"}}} +{"type":"assistant/chunk","seq":42,"time":1783486770606,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":43,"time":1783486770634,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"command"}}} +{"type":"assistant/chunk","seq":44,"time":1783486770634,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":45,"time":1783486770634,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":": "}}} +{"type":"assistant/chunk","seq":46,"time":1783486770634,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":47,"time":1783486770682,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"printf"}}} +{"type":"assistant/chunk","seq":48,"time":1783486770683,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" '"}}} +{"type":"assistant/chunk","seq":49,"time":1783486770683,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"es"}}} +{"type":"assistant/chunk","seq":50,"time":1783486770683,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"cal"}}} +{"type":"assistant/chunk","seq":51,"time":1783486770694,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"ated"}}} +{"type":"assistant/chunk","seq":52,"time":1783486770694,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\\\\n"}}} +{"type":"assistant/chunk","seq":53,"time":1783486770694,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"'"}}} +{"type":"assistant/chunk","seq":54,"time":1783486770695,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" >"}}} +{"type":"assistant/chunk","seq":55,"time":1783486770697,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" escalated"}}} +{"type":"assistant/chunk","seq":56,"time":1783486770697,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":".txt"}}} +{"type":"assistant/chunk","seq":57,"time":1783486770740,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" &&"}}} +{"type":"assistant/chunk","seq":58,"time":1783486770740,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" cat"}}} +{"type":"assistant/chunk","seq":59,"time":1783486770740,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" escalated"}}} +{"type":"assistant/chunk","seq":60,"time":1783486770740,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":".txt"}}} +{"type":"assistant/chunk","seq":61,"time":1783486770740,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":62,"time":1783486770802,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":", "}}} +{"type":"assistant/chunk","seq":63,"time":1783486770802,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":64,"time":1783486770802,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"description"}}} +{"type":"assistant/chunk","seq":65,"time":1783486770802,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":66,"time":1783486770803,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":": "}}} +{"type":"assistant/chunk","seq":67,"time":1783486770820,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":68,"time":1783486770821,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"Write"}}} +{"type":"assistant/chunk","seq":69,"time":1783486770821,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" escalated"}}} +{"type":"assistant/chunk","seq":70,"time":1783486770909,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":".txt"}}} +{"type":"assistant/chunk","seq":71,"time":1783486770910,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" and"}}} +{"type":"assistant/chunk","seq":72,"time":1783486770910,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" cat"}}} +{"type":"assistant/chunk","seq":73,"time":1783486770910,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" its"}}} +{"type":"assistant/chunk","seq":74,"time":1783486770924,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" content"}}} +{"type":"assistant/chunk","seq":75,"time":1783486770925,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":76,"time":1783486770953,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":", "}}} +{"type":"assistant/chunk","seq":77,"time":1783486770953,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":78,"time":1783486770953,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"sand"}}} +{"type":"assistant/chunk","seq":79,"time":1783486770953,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"box"}}} +{"type":"assistant/chunk","seq":80,"time":1783486770981,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"_per"}}} +{"type":"assistant/chunk","seq":81,"time":1783486770983,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"missions"}}} +{"type":"assistant/chunk","seq":82,"time":1783486770983,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":83,"time":1783486770983,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":": "}}} +{"type":"assistant/chunk","seq":84,"time":1783486771018,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":85,"time":1783486771018,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"works"}}} +{"type":"assistant/chunk","seq":86,"time":1783486771018,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"pace"}}} +{"type":"assistant/chunk","seq":87,"time":1783486771019,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"-w"}}} +{"type":"assistant/chunk","seq":88,"time":1783486771019,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"rite"}}} +{"type":"assistant/chunk","seq":89,"time":1783486771050,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":90,"time":1783486771083,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":", "}}} +{"type":"assistant/chunk","seq":91,"time":1783486771083,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":92,"time":1783486771083,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"just"}}} +{"type":"assistant/chunk","seq":93,"time":1783486771083,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"ification"}}} +{"type":"assistant/chunk","seq":94,"time":1783486771083,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":95,"time":1783486771084,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":": "}}} +{"type":"assistant/chunk","seq":96,"time":1783486771113,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":97,"time":1783486771114,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"the"}}} +{"type":"assistant/chunk","seq":98,"time":1783486771114,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" user"}}} +{"type":"assistant/chunk","seq":99,"time":1783486771114,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" asked"}}} +{"type":"assistant/chunk","seq":100,"time":1783486771142,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" to"}}} +{"type":"assistant/chunk","seq":101,"time":1783486771142,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" write"}}} +{"type":"assistant/chunk","seq":102,"time":1783486771142,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" escalated"}}} +{"type":"assistant/chunk","seq":103,"time":1783486771142,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":".txt"}}} +{"type":"assistant/chunk","seq":104,"time":1783486771142,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" in"}}} +{"type":"assistant/chunk","seq":105,"time":1783486771216,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" the"}}} +{"type":"assistant/chunk","seq":106,"time":1783486771217,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":" workspace"}}} +{"type":"assistant/chunk","seq":107,"time":1783486771217,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":108,"time":1783486771219,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","argumentsDelta":"}"}}} +{"type":"assistant/chunk","seq":109,"time":1783486771231,"data":{"turn":1,"step":1,"chunk":{"type":"block-end","index":0,"block":{"type":"reasoning","text":"The user wants me to retry the command with sandbox_permissions set to workspace-write. They explicitly said they will approve the permission prompt. Let me proceed."}}}} +{"type":"assistant/chunk","seq":110,"time":1783486771232,"data":{"turn":1,"step":1,"chunk":{"type":"block-end","index":1,"block":{"type":"tool-call","id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","arguments":"{\"command\": \"printf 'escalated\\\\n' > escalated.txt && cat escalated.txt\", \"description\": \"Write escalated.txt and cat its content\", \"sandbox_permissions\": \"workspace-write\", \"justification\": \"the user asked to write escalated.txt in the workspace\"}"}}}} +{"type":"assistant/chunk","seq":111,"time":1783486771232,"data":{"turn":1,"step":1,"chunk":{"type":"usage","usage":{"inputTokens":1255,"outputTokens":160,"cacheReadTokens":0,"reasoningTokens":34}}}} +{"type":"assistant/chunk","seq":112,"time":1783486771232,"data":{"turn":1,"step":1,"chunk":{"type":"finish","reason":{"kind":"tool-calls"}}}} +{"type":"assistant/message","seq":113,"time":1783486771236,"data":{"turn":1,"step":1,"content":[{"type":"reasoning","text":"The user wants me to retry the command with sandbox_permissions set to workspace-write. They explicitly said they will approve the permission prompt. Let me proceed."},{"type":"tool-call","id":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","arguments":"{\"command\": \"printf 'escalated\\\\n' > escalated.txt && cat escalated.txt\", \"description\": \"Write escalated.txt and cat its content\", \"sandbox_permissions\": \"workspace-write\", \"justification\": \"the user asked to write escalated.txt in the workspace\"}"}],"usage":{"inputTokens":1255,"outputTokens":160,"cacheReadTokens":0,"reasoningTokens":34}},"sourceEventSeqs":[4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112],"surfaceOp":"append"} +{"type":"tool/call","seq":114,"time":1783486771236,"data":{"turn":1,"step":1,"callId":"call_00_ZSEIrZNdgQhL2QJgVHww8689","name":"bash","arguments":"{\"command\": \"printf 'escalated\\\\n' > escalated.txt && cat escalated.txt\", \"description\": \"Write escalated.txt and cat its content\", \"sandbox_permissions\": \"workspace-write\", \"justification\": \"the user asked to write escalated.txt in the workspace\"}"}} +{"type":"approval/asked","seq":115,"time":1783486771238,"data":{"id":"3ec45405-5add-4929-a755-e8c077ec7a7e","toolName":"bash","callId":"call_00_ZSEIrZNdgQhL2QJgVHww8689","reason":"escalate sandbox to workspace-write: the user asked to write escalated.txt in the workspace"}} +{"type":"approval/decided","seq":116,"time":1783486771243,"data":{"id":"3ec45405-5add-4929-a755-e8c077ec7a7e","outcome":"allowed-once"}} +{"type":"tool/result","seq":117,"time":1783486771442,"data":{"turn":1,"step":1,"callId":"call_00_ZSEIrZNdgQhL2QJgVHww8689","content":[{"type":"text","text":"escalated\n"}],"isError":false},"sourceEventSeqs":[114],"surfaceOp":"append"} +{"type":"step/end","seq":118,"time":1783486771443,"data":{"turn":1,"step":1}} +{"type":"step/start","seq":119,"time":1783486771443,"data":{"turn":1,"step":2}} +{"type":"assistant/chunk","seq":120,"time":1783486771965,"data":{"turn":1,"step":2,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}} +{"type":"assistant/chunk","seq":121,"time":1783486771965,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"The"}}} +{"type":"assistant/chunk","seq":122,"time":1783486772052,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" command"}}} +{"type":"assistant/chunk","seq":123,"time":1783486772113,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" succeeded"}}} +{"type":"assistant/chunk","seq":124,"time":1783486772113,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":125,"time":1783486772113,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" The"}}} +{"type":"assistant/chunk","seq":126,"time":1783486772114,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" user"}}} +{"type":"assistant/chunk","seq":127,"time":1783486772115,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" asked"}}} +{"type":"assistant/chunk","seq":128,"time":1783486772115,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" me"}}} +{"type":"assistant/chunk","seq":129,"time":1783486772141,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" to"}}} +{"type":"assistant/chunk","seq":130,"time":1783486772142,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" reply"}}} +{"type":"assistant/chunk","seq":131,"time":1783486772142,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" with"}}} +{"type":"assistant/chunk","seq":132,"time":1783486772185,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" the"}}} +{"type":"assistant/chunk","seq":133,"time":1783486772186,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" single"}}} +{"type":"assistant/chunk","seq":134,"time":1783486772186,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" word"}}} +{"type":"assistant/chunk","seq":135,"time":1783486772186,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" D"}}} +{"type":"assistant/chunk","seq":136,"time":1783486772204,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"ONE"}}} +{"type":"assistant/chunk","seq":137,"time":1783486772205,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":138,"time":1783486772205,"data":{"turn":1,"step":2,"chunk":{"type":"block-start","index":1,"blockType":"text"}}} +{"type":"assistant/chunk","seq":139,"time":1783486772205,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":"D"}}} +{"type":"assistant/chunk","seq":140,"time":1783486772205,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":"ONE"}}} +{"type":"assistant/chunk","seq":141,"time":1783486772228,"data":{"turn":1,"step":2,"chunk":{"type":"block-end","index":0,"block":{"type":"reasoning","text":"The command succeeded. The user asked me to reply with the single word DONE."}}}} +{"type":"assistant/chunk","seq":142,"time":1783486772229,"data":{"turn":1,"step":2,"chunk":{"type":"block-end","index":1,"block":{"type":"text","text":"DONE"}}}} +{"type":"assistant/chunk","seq":143,"time":1783486772229,"data":{"turn":1,"step":2,"chunk":{"type":"usage","usage":{"inputTokens":23,"outputTokens":20,"cacheReadTokens":1408,"reasoningTokens":17}}}} +{"type":"assistant/chunk","seq":144,"time":1783486772229,"data":{"turn":1,"step":2,"chunk":{"type":"finish","reason":{"kind":"stop"}}}} +{"type":"assistant/message","seq":145,"time":1783486772230,"data":{"turn":1,"step":2,"content":[{"type":"reasoning","text":"The command succeeded. The user asked me to reply with the single word DONE."},{"type":"text","text":"DONE"}],"usage":{"inputTokens":23,"outputTokens":20,"cacheReadTokens":1408,"reasoningTokens":17}},"sourceEventSeqs":[120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144],"surfaceOp":"append"} +{"type":"step/end","seq":146,"time":1783486772230,"data":{"turn":1,"step":2}} +{"type":"turn/end","seq":147,"time":1783486772230,"data":{"turn":1,"reason":{"kind":"completed"}}} diff --git a/examples/sandbox-acp-agent/tests/snapshots/escalation-approved/stdout.golden.jsonl b/examples/sandbox-acp-agent/tests/snapshots/escalation-approved/stdout.golden.jsonl new file mode 100644 index 0000000000..88fe79c0ce --- /dev/null +++ b/examples/sandbox-acp-agent/tests/snapshots/escalation-approved/stdout.golden.jsonl @@ -0,0 +1,59 @@ +{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":1,"agentInfo":{"name":"deepseek-harness-acp","version":"0.0.1"},"agentCapabilities":{"loadSession":true,"promptCapabilities":{"image":false,"audio":false,"embeddedContext":false}},"authMethods":[]}} +{"jsonrpc":"2.0","id":2,"result":{"sessionId":"{{sessionId}}","configOptions":[{"id":"sandbox-mode","name":"Sandbox","description":"The file sandbox mode bash commands in this session run under.","category":"mode","type":"select","currentValue":"read-only","options":[{"value":"read-only","name":"read-only"},{"value":"workspace-write","name":"workspace-write"},{"value":"danger-full-access","name":"danger-full-access"}]},{"id":"approval-policy","name":"Approvals","description":"ask: permission prompts reach you; never: they are rejected automatically.","type":"select","currentValue":"ask","options":[{"value":"ask","name":"ask"},{"value":"never","name":"never"}]}]}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"The"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" user"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" wants"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" me"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" to"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" ret"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"ry"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" the"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" command"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" with"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" sand"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"box"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"_per"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"missions"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" set"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" to"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" workspace"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"-w"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"rite"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" They"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" explicitly"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" said"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" they"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" will"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" approve"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" the"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" permission"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" prompt"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" Let"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" me"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" proceed"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"tool_call","toolCallId":"call_00_ZSEIrZNdgQhL2QJgVHww8689","title":"printf 'escalated\\n' > escalated.txt && cat escalated.txt","kind":"execute","status":"in_progress","rawInput":"printf 'escalated\\n' > escalated.txt && cat escalated.txt","content":[{"type":"content","content":{"type":"text","text":"Write escalated.txt and cat its content"}}]}}} +{"jsonrpc":"2.0","id":1,"method":"session/request_permission","params":{"sessionId":"{{sessionId}}","toolCall":{"toolCallId":"call_00_ZSEIrZNdgQhL2QJgVHww8689"},"options":[{"optionId":"allow-once","name":"Allow once","kind":"allow_once"},{"optionId":"reject-once","name":"Reject","kind":"reject_once"}]}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"tool_call_update","toolCallId":"call_00_ZSEIrZNdgQhL2QJgVHww8689","status":"completed","content":[{"type":"content","content":{"type":"text","text":"```console\nescalated\n```"}}]}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"The"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" command"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" succeeded"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" The"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" user"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" asked"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" me"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" to"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" reply"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" with"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" the"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" single"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" word"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" D"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"ONE"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"D"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"ONE"}}}} +{"jsonrpc":"2.0","id":3,"result":{"stopReason":"end_turn"}} diff --git a/examples/sandbox-acp-agent/tests/snapshots/escalation-rejected/input.json b/examples/sandbox-acp-agent/tests/snapshots/escalation-rejected/input.json new file mode 100644 index 0000000000..d645402478 --- /dev/null +++ b/examples/sandbox-acp-agent/tests/snapshots/escalation-rejected/input.json @@ -0,0 +1,10 @@ +{ + "steps": [ + { "op": "initialize" }, + { "op": "newSession" }, + { "op": "prompt", "text": "The sandbox already denied writing escalated.txt in this workspace earlier. Retry it now exactly once: one single bash call with the command printf 'escalated\\n' > escalated.txt && cat escalated.txt, with sandbox_permissions set to workspace-write and the justification 'the user asked to write escalated.txt in the workspace'. Do not run it without sandbox_permissions first. I will reject the permission prompt; after the rejection, do not retry and do not work around it — explain in one short sentence that the escalation was rejected, then stop." } + ], + "permissionAnswers": [ + { "kind": "reject_once" } + ] +} diff --git a/examples/sandbox-acp-agent/tests/snapshots/escalation-rejected/session.jsonl b/examples/sandbox-acp-agent/tests/snapshots/escalation-rejected/session.jsonl new file mode 100644 index 0000000000..ee2586bc34 --- /dev/null +++ b/examples/sandbox-acp-agent/tests/snapshots/escalation-rejected/session.jsonl @@ -0,0 +1,202 @@ +{"type":"session","version":0,"id":"46ee90bb-006b-477c-9c56-04dd4923aeb6","createdAt":1783486772550,"cwd":"/var/folders/2g/b32ct0qn1d728l_v6tdkjytr0000gn/T/acp-snap-cwd-OivXMN"} +{"type":"turn/start","seq":0,"time":1783486772551,"data":{"turn":1,"trigger":{"kind":"message","source":{"kind":"user"}}}} +{"type":"user/message","seq":1,"time":1783486772551,"data":{"content":[{"type":"text","text":"The sandbox already denied writing escalated.txt in this workspace earlier. Retry it now exactly once: one single bash call with the command printf 'escalated\\n' > escalated.txt && cat escalated.txt, with sandbox_permissions set to workspace-write and the justification 'the user asked to write escalated.txt in the workspace'. Do not run it without sandbox_permissions first. I will reject the permission prompt; after the rejection, do not retry and do not work around it — explain in one short sentence that the escalation was rejected, then stop."}],"source":{"kind":"user"}},"surfaceOp":"append"} +{"type":"step/start","seq":2,"time":1783486772552,"data":{"turn":1,"step":1}} +{"type":"request/header","seq":3,"time":1783486772552,"data":{"header":{"config":{"model":"deepseek-v4-flash"},"system":"{{system}}","tools":"{{tools}}"},"reason":"initial"}} +{"type":"assistant/chunk","seq":4,"time":1783486773136,"data":{"turn":1,"step":1,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}} +{"type":"assistant/chunk","seq":5,"time":1783486773136,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"The"}}} +{"type":"assistant/chunk","seq":6,"time":1783486773275,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" user"}}} +{"type":"assistant/chunk","seq":7,"time":1783486773326,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" wants"}}} +{"type":"assistant/chunk","seq":8,"time":1783486773328,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" me"}}} +{"type":"assistant/chunk","seq":9,"time":1783486773328,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" to"}}} +{"type":"assistant/chunk","seq":10,"time":1783486773328,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" ret"}}} +{"type":"assistant/chunk","seq":11,"time":1783486773347,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"ry"}}} +{"type":"assistant/chunk","seq":12,"time":1783486773348,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" the"}}} +{"type":"assistant/chunk","seq":13,"time":1783486773349,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" exact"}}} +{"type":"assistant/chunk","seq":14,"time":1783486773381,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" command"}}} +{"type":"assistant/chunk","seq":15,"time":1783486773382,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" `"}}} +{"type":"assistant/chunk","seq":16,"time":1783486773412,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"printf"}}} +{"type":"assistant/chunk","seq":17,"time":1783486773413,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" '"}}} +{"type":"assistant/chunk","seq":18,"time":1783486773413,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"es"}}} +{"type":"assistant/chunk","seq":19,"time":1783486773413,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"cal"}}} +{"type":"assistant/chunk","seq":20,"time":1783486773414,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"ated"}}} +{"type":"assistant/chunk","seq":21,"time":1783486773414,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"\\n"}}} +{"type":"assistant/chunk","seq":22,"time":1783486773445,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"'"}}} +{"type":"assistant/chunk","seq":23,"time":1783486773446,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" >"}}} +{"type":"assistant/chunk","seq":24,"time":1783486773446,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" escalated"}}} +{"type":"assistant/chunk","seq":25,"time":1783486773446,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":".txt"}}} +{"type":"assistant/chunk","seq":26,"time":1783486773446,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" &&"}}} +{"type":"assistant/chunk","seq":27,"time":1783486773447,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" cat"}}} +{"type":"assistant/chunk","seq":28,"time":1783486773473,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" escalated"}}} +{"type":"assistant/chunk","seq":29,"time":1783486773473,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":".txt"}}} +{"type":"assistant/chunk","seq":30,"time":1783486773473,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"`"}}} +{"type":"assistant/chunk","seq":31,"time":1783486773473,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" with"}}} +{"type":"assistant/chunk","seq":32,"time":1783486773474,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" `"}}} +{"type":"assistant/chunk","seq":33,"time":1783486773500,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"sand"}}} +{"type":"assistant/chunk","seq":34,"time":1783486773501,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"box"}}} +{"type":"assistant/chunk","seq":35,"time":1783486773501,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"_per"}}} +{"type":"assistant/chunk","seq":36,"time":1783486773501,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"missions"}}} +{"type":"assistant/chunk","seq":37,"time":1783486773501,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"`"}}} +{"type":"assistant/chunk","seq":38,"time":1783486773542,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" set"}}} +{"type":"assistant/chunk","seq":39,"time":1783486773543,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" to"}}} +{"type":"assistant/chunk","seq":40,"time":1783486773543,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" `"}}} +{"type":"assistant/chunk","seq":41,"time":1783486773543,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"works"}}} +{"type":"assistant/chunk","seq":42,"time":1783486773543,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"pace"}}} +{"type":"assistant/chunk","seq":43,"time":1783486773543,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"-w"}}} +{"type":"assistant/chunk","seq":44,"time":1783486773568,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"rite"}}} +{"type":"assistant/chunk","seq":45,"time":1783486773569,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"`"}}} +{"type":"assistant/chunk","seq":46,"time":1783486773569,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" and"}}} +{"type":"assistant/chunk","seq":47,"time":1783486773569,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" the"}}} +{"type":"assistant/chunk","seq":48,"time":1783486773597,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" justification"}}} +{"type":"assistant/chunk","seq":49,"time":1783486773632,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" they"}}} +{"type":"assistant/chunk","seq":50,"time":1783486773667,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" specified"}}} +{"type":"assistant/chunk","seq":51,"time":1783486773668,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":52,"time":1783486773669,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" They"}}} +{"type":"assistant/chunk","seq":53,"time":1783486773685,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" explicitly"}}} +{"type":"assistant/chunk","seq":54,"time":1783486773716,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" say"}}} +{"type":"assistant/chunk","seq":55,"time":1783486773731,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" they"}}} +{"type":"assistant/chunk","seq":56,"time":1783486773732,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" will"}}} +{"type":"assistant/chunk","seq":57,"time":1783486773732,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" reject"}}} +{"type":"assistant/chunk","seq":58,"time":1783486773746,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" the"}}} +{"type":"assistant/chunk","seq":59,"time":1783486773746,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" permission"}}} +{"type":"assistant/chunk","seq":60,"time":1783486773747,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" prompt"}}} +{"type":"assistant/chunk","seq":61,"time":1783486773773,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":","}}} +{"type":"assistant/chunk","seq":62,"time":1783486773773,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" so"}}} +{"type":"assistant/chunk","seq":63,"time":1783486773774,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" after"}}} +{"type":"assistant/chunk","seq":64,"time":1783486773774,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" rejection"}}} +{"type":"assistant/chunk","seq":65,"time":1783486773806,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" I"}}} +{"type":"assistant/chunk","seq":66,"time":1783486773807,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" should"}}} +{"type":"assistant/chunk","seq":67,"time":1783486773833,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" explain"}}} +{"type":"assistant/chunk","seq":68,"time":1783486773834,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" in"}}} +{"type":"assistant/chunk","seq":69,"time":1783486773861,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" one"}}} +{"type":"assistant/chunk","seq":70,"time":1783486773862,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" short"}}} +{"type":"assistant/chunk","seq":71,"time":1783486773862,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" sentence"}}} +{"type":"assistant/chunk","seq":72,"time":1783486773862,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" and"}}} +{"type":"assistant/chunk","seq":73,"time":1783486773862,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" stop"}}} +{"type":"assistant/chunk","seq":74,"time":1783486773900,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":75,"time":1783486773978,"data":{"turn":1,"step":1,"chunk":{"type":"block-start","index":1,"blockType":"tool-call"}}} +{"type":"assistant/chunk","seq":76,"time":1783486773979,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":""}}} +{"type":"assistant/chunk","seq":77,"time":1783486774009,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"{"}}} +{"type":"assistant/chunk","seq":78,"time":1783486774011,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":79,"time":1783486774012,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"command"}}} +{"type":"assistant/chunk","seq":80,"time":1783486774012,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":81,"time":1783486774012,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":": "}}} +{"type":"assistant/chunk","seq":82,"time":1783486774049,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":83,"time":1783486774050,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"printf"}}} +{"type":"assistant/chunk","seq":84,"time":1783486774051,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" '"}}} +{"type":"assistant/chunk","seq":85,"time":1783486774051,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"es"}}} +{"type":"assistant/chunk","seq":86,"time":1783486774068,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"cal"}}} +{"type":"assistant/chunk","seq":87,"time":1783486774068,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"ated"}}} +{"type":"assistant/chunk","seq":88,"time":1783486774069,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\\\\n"}}} +{"type":"assistant/chunk","seq":89,"time":1783486774069,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"'"}}} +{"type":"assistant/chunk","seq":90,"time":1783486774069,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" >"}}} +{"type":"assistant/chunk","seq":91,"time":1783486774069,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" escalated"}}} +{"type":"assistant/chunk","seq":92,"time":1783486774107,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":".txt"}}} +{"type":"assistant/chunk","seq":93,"time":1783486774108,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" &&"}}} +{"type":"assistant/chunk","seq":94,"time":1783486774108,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" cat"}}} +{"type":"assistant/chunk","seq":95,"time":1783486774108,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" escalated"}}} +{"type":"assistant/chunk","seq":96,"time":1783486774108,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":".txt"}}} +{"type":"assistant/chunk","seq":97,"time":1783486774138,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":98,"time":1783486774162,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":", "}}} +{"type":"assistant/chunk","seq":99,"time":1783486774162,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":100,"time":1783486774162,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"description"}}} +{"type":"assistant/chunk","seq":101,"time":1783486774162,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":102,"time":1783486774163,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":": "}}} +{"type":"assistant/chunk","seq":103,"time":1783486774194,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":104,"time":1783486774194,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"Write"}}} +{"type":"assistant/chunk","seq":105,"time":1783486774194,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" escalated"}}} +{"type":"assistant/chunk","seq":106,"time":1783486774194,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":".txt"}}} +{"type":"assistant/chunk","seq":107,"time":1783486774194,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" with"}}} +{"type":"assistant/chunk","seq":108,"time":1783486774221,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" workspace"}}} +{"type":"assistant/chunk","seq":109,"time":1783486774221,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"-w"}}} +{"type":"assistant/chunk","seq":110,"time":1783486774283,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"rite"}}} +{"type":"assistant/chunk","seq":111,"time":1783486774283,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" permission"}}} +{"type":"assistant/chunk","seq":112,"time":1783486774283,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":113,"time":1783486774283,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":", "}}} +{"type":"assistant/chunk","seq":114,"time":1783486774283,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":115,"time":1783486774283,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"sand"}}} +{"type":"assistant/chunk","seq":116,"time":1783486774303,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"box"}}} +{"type":"assistant/chunk","seq":117,"time":1783486774303,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"_per"}}} +{"type":"assistant/chunk","seq":118,"time":1783486774303,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"missions"}}} +{"type":"assistant/chunk","seq":119,"time":1783486774304,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":120,"time":1783486774304,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":": "}}} +{"type":"assistant/chunk","seq":121,"time":1783486774339,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":122,"time":1783486774339,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"works"}}} +{"type":"assistant/chunk","seq":123,"time":1783486774339,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"pace"}}} +{"type":"assistant/chunk","seq":124,"time":1783486774339,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"-w"}}} +{"type":"assistant/chunk","seq":125,"time":1783486774339,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"rite"}}} +{"type":"assistant/chunk","seq":126,"time":1783486774368,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":127,"time":1783486774392,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":", "}}} +{"type":"assistant/chunk","seq":128,"time":1783486774395,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":129,"time":1783486774395,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"just"}}} +{"type":"assistant/chunk","seq":130,"time":1783486774395,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"ification"}}} +{"type":"assistant/chunk","seq":131,"time":1783486774395,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":132,"time":1783486774440,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":": "}}} +{"type":"assistant/chunk","seq":133,"time":1783486774441,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":134,"time":1783486774441,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"the"}}} +{"type":"assistant/chunk","seq":135,"time":1783486774441,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" user"}}} +{"type":"assistant/chunk","seq":136,"time":1783486774469,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" asked"}}} +{"type":"assistant/chunk","seq":137,"time":1783486774469,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" to"}}} +{"type":"assistant/chunk","seq":138,"time":1783486774469,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" write"}}} +{"type":"assistant/chunk","seq":139,"time":1783486774469,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" escalated"}}} +{"type":"assistant/chunk","seq":140,"time":1783486774469,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":".txt"}}} +{"type":"assistant/chunk","seq":141,"time":1783486774470,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" in"}}} +{"type":"assistant/chunk","seq":142,"time":1783486774480,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" the"}}} +{"type":"assistant/chunk","seq":143,"time":1783486774480,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":" workspace"}}} +{"type":"assistant/chunk","seq":144,"time":1783486774481,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":145,"time":1783486774510,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","argumentsDelta":"}"}}} +{"type":"assistant/chunk","seq":146,"time":1783486774568,"data":{"turn":1,"step":1,"chunk":{"type":"block-end","index":0,"block":{"type":"reasoning","text":"The user wants me to retry the exact command `printf 'escalated\\n' > escalated.txt && cat escalated.txt` with `sandbox_permissions` set to `workspace-write` and the justification they specified. They explicitly say they will reject the permission prompt, so after rejection I should explain in one short sentence and stop."}}}} +{"type":"assistant/chunk","seq":147,"time":1783486774569,"data":{"turn":1,"step":1,"chunk":{"type":"block-end","index":1,"block":{"type":"tool-call","id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","arguments":"{\"command\": \"printf 'escalated\\\\n' > escalated.txt && cat escalated.txt\", \"description\": \"Write escalated.txt with workspace-write permission\", \"sandbox_permissions\": \"workspace-write\", \"justification\": \"the user asked to write escalated.txt in the workspace\"}"}}}} +{"type":"assistant/chunk","seq":148,"time":1783486774569,"data":{"turn":1,"step":1,"chunk":{"type":"usage","usage":{"inputTokens":1269,"outputTokens":197,"cacheReadTokens":0,"reasoningTokens":70}}}} +{"type":"assistant/chunk","seq":149,"time":1783486774569,"data":{"turn":1,"step":1,"chunk":{"type":"finish","reason":{"kind":"tool-calls"}}}} +{"type":"assistant/message","seq":150,"time":1783486774572,"data":{"turn":1,"step":1,"content":[{"type":"reasoning","text":"The user wants me to retry the exact command `printf 'escalated\\n' > escalated.txt && cat escalated.txt` with `sandbox_permissions` set to `workspace-write` and the justification they specified. They explicitly say they will reject the permission prompt, so after rejection I should explain in one short sentence and stop."},{"type":"tool-call","id":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","arguments":"{\"command\": \"printf 'escalated\\\\n' > escalated.txt && cat escalated.txt\", \"description\": \"Write escalated.txt with workspace-write permission\", \"sandbox_permissions\": \"workspace-write\", \"justification\": \"the user asked to write escalated.txt in the workspace\"}"}],"usage":{"inputTokens":1269,"outputTokens":197,"cacheReadTokens":0,"reasoningTokens":70}},"sourceEventSeqs":[4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149],"surfaceOp":"append"} +{"type":"tool/call","seq":151,"time":1783486774572,"data":{"turn":1,"step":1,"callId":"call_00_ODln9LCQtuvTw4FDZEfe3479","name":"bash","arguments":"{\"command\": \"printf 'escalated\\\\n' > escalated.txt && cat escalated.txt\", \"description\": \"Write escalated.txt with workspace-write permission\", \"sandbox_permissions\": \"workspace-write\", \"justification\": \"the user asked to write escalated.txt in the workspace\"}"}} +{"type":"approval/asked","seq":152,"time":1783486774574,"data":{"id":"ed977255-38a3-4c1d-9f4e-0e258ef86e94","toolName":"bash","callId":"call_00_ODln9LCQtuvTw4FDZEfe3479","reason":"escalate sandbox to workspace-write: the user asked to write escalated.txt in the workspace"}} +{"type":"approval/decided","seq":153,"time":1783486774578,"data":{"id":"ed977255-38a3-4c1d-9f4e-0e258ef86e94","outcome":"rejected"}} +{"type":"tool/result","seq":154,"time":1783486774579,"data":{"turn":1,"step":1,"callId":"call_00_ODln9LCQtuvTw4FDZEfe3479","content":[{"type":"text","text":"Error: the user rejected escalating this command to \"workspace-write\""}],"isError":true},"sourceEventSeqs":[151],"surfaceOp":"append"} +{"type":"step/end","seq":155,"time":1783486774579,"data":{"turn":1,"step":1}} +{"type":"step/start","seq":156,"time":1783486774580,"data":{"turn":1,"step":2}} +{"type":"assistant/chunk","seq":157,"time":1783486775719,"data":{"turn":1,"step":2,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}} +{"type":"assistant/chunk","seq":158,"time":1783486775719,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"The"}}} +{"type":"assistant/chunk","seq":159,"time":1783486775820,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" user"}}} +{"type":"assistant/chunk","seq":160,"time":1783486775856,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" rejected"}}} +{"type":"assistant/chunk","seq":161,"time":1783486775857,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" the"}}} +{"type":"assistant/chunk","seq":162,"time":1783486775857,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" escalation"}}} +{"type":"assistant/chunk","seq":163,"time":1783486775857,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":164,"time":1783486775879,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" I"}}} +{"type":"assistant/chunk","seq":165,"time":1783486775879,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" need"}}} +{"type":"assistant/chunk","seq":166,"time":1783486775921,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" to"}}} +{"type":"assistant/chunk","seq":167,"time":1783486775922,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" explain"}}} +{"type":"assistant/chunk","seq":168,"time":1783486775922,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" in"}}} +{"type":"assistant/chunk","seq":169,"time":1783486775934,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" one"}}} +{"type":"assistant/chunk","seq":170,"time":1783486775934,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" short"}}} +{"type":"assistant/chunk","seq":171,"time":1783486775935,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" sentence"}}} +{"type":"assistant/chunk","seq":172,"time":1783486775935,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" and"}}} +{"type":"assistant/chunk","seq":173,"time":1783486775935,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" stop"}}} +{"type":"assistant/chunk","seq":174,"time":1783486775935,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":175,"time":1783486775977,"data":{"turn":1,"step":2,"chunk":{"type":"block-start","index":1,"blockType":"text"}}} +{"type":"assistant/chunk","seq":176,"time":1783486775978,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":"The"}}} +{"type":"assistant/chunk","seq":177,"time":1783486775978,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" escalation"}}} +{"type":"assistant/chunk","seq":178,"time":1783486775978,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" request"}}} +{"type":"assistant/chunk","seq":179,"time":1783486776011,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" to"}}} +{"type":"assistant/chunk","seq":180,"time":1783486776026,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" write"}}} +{"type":"assistant/chunk","seq":181,"time":1783486776027,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" to"}}} +{"type":"assistant/chunk","seq":182,"time":1783486776027,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" the"}}} +{"type":"assistant/chunk","seq":183,"time":1783486776057,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" workspace"}}} +{"type":"assistant/chunk","seq":184,"time":1783486776057,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" was"}}} +{"type":"assistant/chunk","seq":185,"time":1783486776057,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" rejected"}}} +{"type":"assistant/chunk","seq":186,"time":1783486776058,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":","}}} +{"type":"assistant/chunk","seq":187,"time":1783486776128,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" so"}}} +{"type":"assistant/chunk","seq":188,"time":1783486776128,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" the"}}} +{"type":"assistant/chunk","seq":189,"time":1783486776129,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" command"}}} +{"type":"assistant/chunk","seq":190,"time":1783486776129,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" was"}}} +{"type":"assistant/chunk","seq":191,"time":1783486776145,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" not"}}} +{"type":"assistant/chunk","seq":192,"time":1783486776146,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":" executed"}}} +{"type":"assistant/chunk","seq":193,"time":1783486776184,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":"."}}} +{"type":"assistant/chunk","seq":194,"time":1783486776185,"data":{"turn":1,"step":2,"chunk":{"type":"block-end","index":0,"block":{"type":"reasoning","text":"The user rejected the escalation. I need to explain in one short sentence and stop."}}}} +{"type":"assistant/chunk","seq":195,"time":1783486776185,"data":{"turn":1,"step":2,"chunk":{"type":"block-end","index":1,"block":{"type":"text","text":"The escalation request to write to the workspace was rejected, so the command was not executed."}}}} +{"type":"assistant/chunk","seq":196,"time":1783486776185,"data":{"turn":1,"step":2,"chunk":{"type":"usage","usage":{"inputTokens":341,"outputTokens":36,"cacheReadTokens":1152,"reasoningTokens":17}}}} +{"type":"assistant/chunk","seq":197,"time":1783486776185,"data":{"turn":1,"step":2,"chunk":{"type":"finish","reason":{"kind":"stop"}}}} +{"type":"assistant/message","seq":198,"time":1783486776186,"data":{"turn":1,"step":2,"content":[{"type":"reasoning","text":"The user rejected the escalation. I need to explain in one short sentence and stop."},{"type":"text","text":"The escalation request to write to the workspace was rejected, so the command was not executed."}],"usage":{"inputTokens":341,"outputTokens":36,"cacheReadTokens":1152,"reasoningTokens":17}},"sourceEventSeqs":[157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197],"surfaceOp":"append"} +{"type":"step/end","seq":199,"time":1783486776186,"data":{"turn":1,"step":2}} +{"type":"turn/end","seq":200,"time":1783486776186,"data":{"turn":1,"reason":{"kind":"completed"}}} diff --git a/examples/sandbox-acp-agent/tests/snapshots/escalation-rejected/stdout.golden.jsonl b/examples/sandbox-acp-agent/tests/snapshots/escalation-rejected/stdout.golden.jsonl new file mode 100644 index 0000000000..f8b292e4c3 --- /dev/null +++ b/examples/sandbox-acp-agent/tests/snapshots/escalation-rejected/stdout.golden.jsonl @@ -0,0 +1,111 @@ +{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":1,"agentInfo":{"name":"deepseek-harness-acp","version":"0.0.1"},"agentCapabilities":{"loadSession":true,"promptCapabilities":{"image":false,"audio":false,"embeddedContext":false}},"authMethods":[]}} +{"jsonrpc":"2.0","id":2,"result":{"sessionId":"{{sessionId}}","configOptions":[{"id":"sandbox-mode","name":"Sandbox","description":"The file sandbox mode bash commands in this session run under.","category":"mode","type":"select","currentValue":"read-only","options":[{"value":"read-only","name":"read-only"},{"value":"workspace-write","name":"workspace-write"},{"value":"danger-full-access","name":"danger-full-access"}]},{"id":"approval-policy","name":"Approvals","description":"ask: permission prompts reach you; never: they are rejected automatically.","type":"select","currentValue":"ask","options":[{"value":"ask","name":"ask"},{"value":"never","name":"never"}]}]}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"The"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" user"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" wants"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" me"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" to"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" ret"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"ry"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" the"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" exact"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" command"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" `"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"printf"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" '"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"es"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"cal"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"ated"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"\\n"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"'"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" >"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" escalated"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":".txt"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" &&"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" cat"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" escalated"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":".txt"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"`"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" with"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" `"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"sand"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"box"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"_per"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"missions"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"`"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" set"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" to"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" `"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"works"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"pace"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"-w"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"rite"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"`"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" and"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" the"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" justification"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" they"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" specified"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" They"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" explicitly"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" say"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" they"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" will"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" reject"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" the"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" permission"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" prompt"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":","}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" so"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" after"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" rejection"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" I"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" should"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" explain"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" in"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" one"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" short"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" sentence"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" and"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" stop"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"tool_call","toolCallId":"call_00_ODln9LCQtuvTw4FDZEfe3479","title":"printf 'escalated\\n' > escalated.txt && cat escalated.txt","kind":"execute","status":"in_progress","rawInput":"printf 'escalated\\n' > escalated.txt && cat escalated.txt","content":[{"type":"content","content":{"type":"text","text":"Write escalated.txt with workspace-write permission"}}]}}} +{"jsonrpc":"2.0","id":1,"method":"session/request_permission","params":{"sessionId":"{{sessionId}}","toolCall":{"toolCallId":"call_00_ODln9LCQtuvTw4FDZEfe3479"},"options":[{"optionId":"allow-once","name":"Allow once","kind":"allow_once"},{"optionId":"reject-once","name":"Reject","kind":"reject_once"}]}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"tool_call_update","toolCallId":"call_00_ODln9LCQtuvTw4FDZEfe3479","status":"failed","content":[{"type":"content","content":{"type":"text","text":"```console\nError: the user rejected escalating this command to \"workspace-write\"\n```"}}]}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"The"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" user"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" rejected"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" the"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" escalation"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" I"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" need"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" to"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" explain"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" in"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" one"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" short"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" sentence"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" and"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" stop"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"The"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" escalation"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" request"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" to"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" write"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" to"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" the"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" workspace"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" was"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" rejected"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":","}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" so"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" the"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" command"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" was"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" not"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" executed"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","id":3,"result":{"stopReason":"end_turn"}} diff --git a/examples/sandbox-acp-agent/tests/snapshots/mode-switching/input.json b/examples/sandbox-acp-agent/tests/snapshots/mode-switching/input.json new file mode 100644 index 0000000000..e11cec253e --- /dev/null +++ b/examples/sandbox-acp-agent/tests/snapshots/mode-switching/input.json @@ -0,0 +1,11 @@ +{ + "steps": [ + { "op": "initialize" }, + { "op": "newSession" }, + { "op": "prompt", "text": "Use the bash tool to run exactly: cat notes.txt. Then reply with the single word DONE and stop." }, + { "op": "setConfigOption", "configId": "sandbox-mode", "value": "workspace-write" }, + { "op": "prompt", "text": "Use the bash tool to run exactly this one command in a single call: printf 'switched\\n' > out.txt && cat out.txt. Then reply with the single word DONE and stop." }, + { "op": "setConfigOption", "configId": "approval-policy", "value": "never" }, + { "op": "prompt", "text": "Without using any tools, state your current approval policy in one short sentence and stop." } + ] +} diff --git a/examples/sandbox-acp-agent/tests/snapshots/mode-switching/session.jsonl b/examples/sandbox-acp-agent/tests/snapshots/mode-switching/session.jsonl new file mode 100644 index 0000000000..ef287390aa --- /dev/null +++ b/examples/sandbox-acp-agent/tests/snapshots/mode-switching/session.jsonl @@ -0,0 +1,245 @@ +{"type":"session","version":0,"id":"eee406bd-e76b-4d87-8fb7-0ffa2dc94fe6","createdAt":1783613224994,"cwd":"/var/folders/2g/b32ct0qn1d728l_v6tdkjytr0000gn/T/acp-snap-cwd-mNdf7I"} +{"type":"turn/start","seq":0,"time":1783613224997,"data":{"turn":1,"trigger":{"kind":"message","source":{"kind":"user"}}}} +{"type":"user/message","seq":1,"time":1783613224997,"data":{"content":[{"type":"text","text":"Use the bash tool to run exactly: cat notes.txt. Then reply with the single word DONE and stop."}],"source":{"kind":"user"}},"surfaceOp":"append"} +{"type":"step/start","seq":2,"time":1783613224997,"data":{"turn":1,"step":1}} +{"type":"request/header","seq":3,"time":1783613224997,"data":{"header":{"config":{"model":"deepseek-v4-flash"},"system":"{{system}}","tools":[{"name":"bash","description":"Execute a bash command (`bash -c`) and return its stdout/stderr. Each call runs in a fresh shell: no state (cwd, variables, functions) persists between calls — pass `workdir` instead of using `cd`. Non-zero exits are reported as `[exit code: N]`. Commands may run under a file sandbox; a blocked file operation is reported as `[sandbox: file access denied under mode]` — a policy denial, not a bug in the command; do not retry another way (a background task reports the same marker via bash_output once it has finished). Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. Set `run_in_background: true` for long-running commands: the call returns a task id immediately; poll it with `bash_output` and stop it with `bash_kill`. Attempting a command the sandbox may deny is safe and expected: run it and read the marker rather than assuming the denial. When a command IS denied and a wider mode would let it succeed, escalate immediately in the SAME turn — the ONE sanctioned exception to a denial: retry the exact same command once with `sandbox_permissions` (the narrowest wider mode that suffices) plus a one-sentence `justification`. Do not detour through chat to ask permission first — the approval prompt raised by that retry IS how the user consents. If the session states approval prompts are disabled, there is no exception: a denial is final — do not set `sandbox_permissions`. Never escalate speculatively: ground the request in a real denial — normally the one THIS command just hit; escalating up front is fine only when this session already denied the same access. A rejected escalation is final for THAT command — stop and explain, never work around it — but it does not forbid attempting or escalating other commands later.","parameters":{"type":"object","properties":{"command":{"type":"string","description":"The bash command to execute."},"description":{"type":"string","description":"Clear, concise description of what this command does in active voice, 5-10 words (shown in the UI). Examples: \"ls\" → \"List files in current directory\"; \"git status\" → \"Show working tree status\"; \"npm install\" → \"Install package dependencies\"."},"timeoutMs":{"type":"number","description":"Timeout in milliseconds. The executor applies its configured default and cap, and kills the command on expiry."},"workdir":{"type":"string","description":"Working directory for this command. Defaults to the session workspace; a relative path is resolved against it."},"run_in_background":{"type":"boolean","description":"Run in the background and return a task id immediately. No timeout applies."},"sandbox_permissions":{"type":"string","description":"The wider sandbox mode this command needs. Only valid as a one-shot retry of a command the sandbox just denied; requires justification and user approval.","enum":["workspace-write","danger-full-access"]},"justification":{"type":"string","description":"Required with sandbox_permissions: one sentence for the user explaining why this exact command needs the wider access."}},"required":["command","description"]}},{"name":"bash_kill","description":"Ask the executor to kill a running background bash task by task id.","parameters":{"type":"object","properties":{"task_id":{"type":"string","description":"Task id returned by the bash tool."}},"required":["task_id"]}},{"name":"bash_output","description":"Read new output from a background bash task started with `bash` + `run_in_background`. Returns only output produced since the previous bash_output call, plus the task status. Tasks keep running while you do other work; poll again later for more output.","parameters":{"type":"object","properties":{"task_id":{"type":"string","description":"Task id returned by the bash tool."}},"required":["task_id"]}},{"name":"skill","description":"Load the full instructions for an available skill. Call this with the exact skill name from the session skill catalog before acting on a task that names or clearly matches that skill.","parameters":{"type":"object","properties":{"name":{"type":"string","description":"The exact skill name from the available skills list."}},"required":["name"]}}]},"reason":"initial"}} +{"type":"assistant/chunk","seq":4,"time":1783613225437,"data":{"turn":1,"step":1,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}} +{"type":"assistant/chunk","seq":5,"time":1783613225438,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"The"}}} +{"type":"assistant/chunk","seq":6,"time":1783613225658,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" user"}}} +{"type":"assistant/chunk","seq":7,"time":1783613225687,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" wants"}}} +{"type":"assistant/chunk","seq":8,"time":1783613225687,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" me"}}} +{"type":"assistant/chunk","seq":9,"time":1783613225687,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" to"}}} +{"type":"assistant/chunk","seq":10,"time":1783613225687,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" run"}}} +{"type":"assistant/chunk","seq":11,"time":1783613225688,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" `"}}} +{"type":"assistant/chunk","seq":12,"time":1783613225717,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"cat"}}} +{"type":"assistant/chunk","seq":13,"time":1783613225718,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" notes"}}} +{"type":"assistant/chunk","seq":14,"time":1783613225718,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":".txt"}}} +{"type":"assistant/chunk","seq":15,"time":1783613225718,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"`"}}} +{"type":"assistant/chunk","seq":16,"time":1783613225718,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" using"}}} +{"type":"assistant/chunk","seq":17,"time":1783613225746,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" the"}}} +{"type":"assistant/chunk","seq":18,"time":1783613225747,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" bash"}}} +{"type":"assistant/chunk","seq":19,"time":1783613225747,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" tool"}}} +{"type":"assistant/chunk","seq":20,"time":1783613225747,"data":{"turn":1,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":21,"time":1783613225832,"data":{"turn":1,"step":1,"chunk":{"type":"block-start","index":1,"blockType":"tool-call"}}} +{"type":"assistant/chunk","seq":22,"time":1783613225832,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":""}}} +{"type":"assistant/chunk","seq":23,"time":1783613225859,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":"{"}}} +{"type":"assistant/chunk","seq":24,"time":1783613225860,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":25,"time":1783613225860,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":"command"}}} +{"type":"assistant/chunk","seq":26,"time":1783613225860,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":27,"time":1783613225860,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":": "}}} +{"type":"assistant/chunk","seq":28,"time":1783613225888,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":29,"time":1783613225888,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":"cat"}}} +{"type":"assistant/chunk","seq":30,"time":1783613225889,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":" notes"}}} +{"type":"assistant/chunk","seq":31,"time":1783613225889,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":".txt"}}} +{"type":"assistant/chunk","seq":32,"time":1783613225889,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":33,"time":1783613225945,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":", "}}} +{"type":"assistant/chunk","seq":34,"time":1783613225945,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":35,"time":1783613225945,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":"description"}}} +{"type":"assistant/chunk","seq":36,"time":1783613225945,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":37,"time":1783613225946,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":": "}}} +{"type":"assistant/chunk","seq":38,"time":1783613225979,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":39,"time":1783613225979,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":"Show"}}} +{"type":"assistant/chunk","seq":40,"time":1783613225979,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":" contents"}}} +{"type":"assistant/chunk","seq":41,"time":1783613225979,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":" of"}}} +{"type":"assistant/chunk","seq":42,"time":1783613225980,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":" notes"}}} +{"type":"assistant/chunk","seq":43,"time":1783613226003,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":".txt"}}} +{"type":"assistant/chunk","seq":44,"time":1783613226004,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":45,"time":1783613226031,"data":{"turn":1,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","argumentsDelta":"}"}}} +{"type":"assistant/chunk","seq":46,"time":1783613226061,"data":{"turn":1,"step":1,"chunk":{"type":"block-end","index":0,"block":{"type":"reasoning","text":"The user wants me to run `cat notes.txt` using the bash tool."}}}} +{"type":"assistant/chunk","seq":47,"time":1783613226061,"data":{"turn":1,"step":1,"chunk":{"type":"block-end","index":1,"block":{"type":"tool-call","id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","arguments":"{\"command\": \"cat notes.txt\", \"description\": \"Show contents of notes.txt\"}"}}}} +{"type":"assistant/chunk","seq":48,"time":1783613226061,"data":{"turn":1,"step":1,"chunk":{"type":"usage","usage":{"inputTokens":1202,"outputTokens":81,"cacheReadTokens":0,"reasoningTokens":16}}}} +{"type":"assistant/chunk","seq":49,"time":1783613226061,"data":{"turn":1,"step":1,"chunk":{"type":"finish","reason":{"kind":"tool-calls"}}}} +{"type":"assistant/message","seq":50,"time":1783613226064,"data":{"turn":1,"step":1,"content":[{"type":"reasoning","text":"The user wants me to run `cat notes.txt` using the bash tool."},{"type":"tool-call","id":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","arguments":"{\"command\": \"cat notes.txt\", \"description\": \"Show contents of notes.txt\"}"}],"usage":{"inputTokens":1202,"outputTokens":81,"cacheReadTokens":0,"reasoningTokens":16}},"sourceEventSeqs":[4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49],"surfaceOp":"append"} +{"type":"tool/call","seq":51,"time":1783613226064,"data":{"turn":1,"step":1,"callId":"call_00_I9sT0gKlfz9YP4dlf2pA3899","name":"bash","arguments":"{\"command\": \"cat notes.txt\", \"description\": \"Show contents of notes.txt\"}"}} +{"type":"tool/result","seq":52,"time":1783613226148,"data":{"turn":1,"step":1,"callId":"call_00_I9sT0gKlfz9YP4dlf2pA3899","content":[{"type":"text","text":"hello from the sandboxed workspace\n"}],"isError":false},"sourceEventSeqs":[51],"surfaceOp":"append"} +{"type":"step/end","seq":53,"time":1783613226148,"data":{"turn":1,"step":1}} +{"type":"step/start","seq":54,"time":1783613226148,"data":{"turn":1,"step":2}} +{"type":"assistant/chunk","seq":55,"time":1783613226790,"data":{"turn":1,"step":2,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}} +{"type":"assistant/chunk","seq":56,"time":1783613226791,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"The"}}} +{"type":"assistant/chunk","seq":57,"time":1783613226970,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" command"}}} +{"type":"assistant/chunk","seq":58,"time":1783613227003,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" ran"}}} +{"type":"assistant/chunk","seq":59,"time":1783613227025,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" successfully"}}} +{"type":"assistant/chunk","seq":60,"time":1783613227025,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":61,"time":1783613227025,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" The"}}} +{"type":"assistant/chunk","seq":62,"time":1783613227025,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" user"}}} +{"type":"assistant/chunk","seq":63,"time":1783613227052,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" asked"}}} +{"type":"assistant/chunk","seq":64,"time":1783613227053,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" me"}}} +{"type":"assistant/chunk","seq":65,"time":1783613227053,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" to"}}} +{"type":"assistant/chunk","seq":66,"time":1783613227053,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" reply"}}} +{"type":"assistant/chunk","seq":67,"time":1783613227053,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" with"}}} +{"type":"assistant/chunk","seq":68,"time":1783613227053,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" the"}}} +{"type":"assistant/chunk","seq":69,"time":1783613227082,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" single"}}} +{"type":"assistant/chunk","seq":70,"time":1783613227082,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" word"}}} +{"type":"assistant/chunk","seq":71,"time":1783613227083,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" D"}}} +{"type":"assistant/chunk","seq":72,"time":1783613227110,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"ONE"}}} +{"type":"assistant/chunk","seq":73,"time":1783613227110,"data":{"turn":1,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":74,"time":1783613227140,"data":{"turn":1,"step":2,"chunk":{"type":"block-start","index":1,"blockType":"text"}}} +{"type":"assistant/chunk","seq":75,"time":1783613227140,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":"D"}}} +{"type":"assistant/chunk","seq":76,"time":1783613227141,"data":{"turn":1,"step":2,"chunk":{"type":"text-delta","index":1,"text":"ONE"}}} +{"type":"assistant/chunk","seq":77,"time":1783613227141,"data":{"turn":1,"step":2,"chunk":{"type":"block-end","index":0,"block":{"type":"reasoning","text":"The command ran successfully. The user asked me to reply with the single word DONE."}}}} +{"type":"assistant/chunk","seq":78,"time":1783613227141,"data":{"turn":1,"step":2,"chunk":{"type":"block-end","index":1,"block":{"type":"text","text":"DONE"}}}} +{"type":"assistant/chunk","seq":79,"time":1783613227141,"data":{"turn":1,"step":2,"chunk":{"type":"usage","usage":{"inputTokens":23,"outputTokens":21,"cacheReadTokens":1280,"reasoningTokens":18}}}} +{"type":"assistant/chunk","seq":80,"time":1783613227141,"data":{"turn":1,"step":2,"chunk":{"type":"finish","reason":{"kind":"stop"}}}} +{"type":"assistant/message","seq":81,"time":1783613227142,"data":{"turn":1,"step":2,"content":[{"type":"reasoning","text":"The command ran successfully. The user asked me to reply with the single word DONE."},{"type":"text","text":"DONE"}],"usage":{"inputTokens":23,"outputTokens":21,"cacheReadTokens":1280,"reasoningTokens":18}},"sourceEventSeqs":[55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80],"surfaceOp":"append"} +{"type":"step/end","seq":82,"time":1783613227142,"data":{"turn":1,"step":2}} +{"type":"turn/end","seq":83,"time":1783613227142,"data":{"turn":1,"reason":{"kind":"completed"}}} +{"type":"turn/start","seq":84,"time":1783613227169,"data":{"turn":2,"trigger":{"kind":"message","source":{"kind":"user"}}}} +{"type":"bash/sandbox-mode","seq":85,"time":1783613227169,"data":{"mode":"workspace-write"}} +{"type":"user/message","seq":86,"time":1783613227169,"data":{"content":[{"type":"text","text":"Use the bash tool to run exactly this one command in a single call: printf 'switched\\n' > out.txt && cat out.txt. Then reply with the single word DONE and stop."}],"source":{"kind":"user"}},"surfaceOp":"append"} +{"type":"step/start","seq":87,"time":1783613227169,"data":{"turn":2,"step":1}} +{"type":"assistant/chunk","seq":88,"time":1783613227737,"data":{"turn":2,"step":1,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}} +{"type":"assistant/chunk","seq":89,"time":1783613227738,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"The"}}} +{"type":"assistant/chunk","seq":90,"time":1783613227839,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" user"}}} +{"type":"assistant/chunk","seq":91,"time":1783613227858,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" wants"}}} +{"type":"assistant/chunk","seq":92,"time":1783613227859,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" me"}}} +{"type":"assistant/chunk","seq":93,"time":1783613227859,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" to"}}} +{"type":"assistant/chunk","seq":94,"time":1783613227859,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" run"}}} +{"type":"assistant/chunk","seq":95,"time":1783613227859,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" a"}}} +{"type":"assistant/chunk","seq":96,"time":1783613227859,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" specific"}}} +{"type":"assistant/chunk","seq":97,"time":1783613227888,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" command"}}} +{"type":"assistant/chunk","seq":98,"time":1783613227888,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":99,"time":1783613227888,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" Let"}}} +{"type":"assistant/chunk","seq":100,"time":1783613227916,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" me"}}} +{"type":"assistant/chunk","seq":101,"time":1783613227916,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" execute"}}} +{"type":"assistant/chunk","seq":102,"time":1783613227917,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" it"}}} +{"type":"assistant/chunk","seq":103,"time":1783613227917,"data":{"turn":2,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":104,"time":1783613228006,"data":{"turn":2,"step":1,"chunk":{"type":"block-start","index":1,"blockType":"tool-call"}}} +{"type":"assistant/chunk","seq":105,"time":1783613228006,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":""}}} +{"type":"assistant/chunk","seq":106,"time":1783613228034,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"{"}}} +{"type":"assistant/chunk","seq":107,"time":1783613228035,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":108,"time":1783613228035,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"description"}}} +{"type":"assistant/chunk","seq":109,"time":1783613228035,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":110,"time":1783613228035,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":": "}}} +{"type":"assistant/chunk","seq":111,"time":1783613228063,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":112,"time":1783613228063,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"Write"}}} +{"type":"assistant/chunk","seq":113,"time":1783613228063,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":" and"}}} +{"type":"assistant/chunk","seq":114,"time":1783613228100,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":" read"}}} +{"type":"assistant/chunk","seq":115,"time":1783613228100,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":" out"}}} +{"type":"assistant/chunk","seq":116,"time":1783613228100,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":".txt"}}} +{"type":"assistant/chunk","seq":117,"time":1783613228121,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":118,"time":1783613228150,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":", "}}} +{"type":"assistant/chunk","seq":119,"time":1783613228150,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":120,"time":1783613228151,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"command"}}} +{"type":"assistant/chunk","seq":121,"time":1783613228151,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":122,"time":1783613228151,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":": "}}} +{"type":"assistant/chunk","seq":123,"time":1783613228179,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":124,"time":1783613228179,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"printf"}}} +{"type":"assistant/chunk","seq":125,"time":1783613228179,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":" '"}}} +{"type":"assistant/chunk","seq":126,"time":1783613228179,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"sw"}}} +{"type":"assistant/chunk","seq":127,"time":1783613228209,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"itched"}}} +{"type":"assistant/chunk","seq":128,"time":1783613228209,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"\\\\n"}}} +{"type":"assistant/chunk","seq":129,"time":1783613228209,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"'"}}} +{"type":"assistant/chunk","seq":130,"time":1783613228209,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":" >"}}} +{"type":"assistant/chunk","seq":131,"time":1783613228209,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":" out"}}} +{"type":"assistant/chunk","seq":132,"time":1783613228209,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":".txt"}}} +{"type":"assistant/chunk","seq":133,"time":1783613228237,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":" &&"}}} +{"type":"assistant/chunk","seq":134,"time":1783613228237,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":" cat"}}} +{"type":"assistant/chunk","seq":135,"time":1783613228237,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":" out"}}} +{"type":"assistant/chunk","seq":136,"time":1783613228237,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":".txt"}}} +{"type":"assistant/chunk","seq":137,"time":1783613228237,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"\""}}} +{"type":"assistant/chunk","seq":138,"time":1783613228265,"data":{"turn":2,"step":1,"chunk":{"type":"tool-call-delta","index":1,"id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","argumentsDelta":"}"}}} +{"type":"assistant/chunk","seq":139,"time":1783613228325,"data":{"turn":2,"step":1,"chunk":{"type":"block-end","index":0,"block":{"type":"reasoning","text":"The user wants me to run a specific command. Let me execute it."}}}} +{"type":"assistant/chunk","seq":140,"time":1783613228325,"data":{"turn":2,"step":1,"chunk":{"type":"block-end","index":1,"block":{"type":"tool-call","id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","arguments":"{\"description\": \"Write and read out.txt\", \"command\": \"printf 'switched\\\\n' > out.txt && cat out.txt\"}"}}}} +{"type":"assistant/chunk","seq":141,"time":1783613228325,"data":{"turn":2,"step":1,"chunk":{"type":"usage","usage":{"inputTokens":70,"outputTokens":90,"cacheReadTokens":1280,"reasoningTokens":15}}}} +{"type":"assistant/chunk","seq":142,"time":1783613228325,"data":{"turn":2,"step":1,"chunk":{"type":"finish","reason":{"kind":"tool-calls"}}}} +{"type":"assistant/message","seq":143,"time":1783613228325,"data":{"turn":2,"step":1,"content":[{"type":"reasoning","text":"The user wants me to run a specific command. Let me execute it."},{"type":"tool-call","id":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","arguments":"{\"description\": \"Write and read out.txt\", \"command\": \"printf 'switched\\\\n' > out.txt && cat out.txt\"}"}],"usage":{"inputTokens":70,"outputTokens":90,"cacheReadTokens":1280,"reasoningTokens":15}},"sourceEventSeqs":[88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142],"surfaceOp":"append"} +{"type":"tool/call","seq":144,"time":1783613228325,"data":{"turn":2,"step":1,"callId":"call_00_htXcbtvwTFK0NqfclLWL7784","name":"bash","arguments":"{\"description\": \"Write and read out.txt\", \"command\": \"printf 'switched\\\\n' > out.txt && cat out.txt\"}"}} +{"type":"tool/result","seq":145,"time":1783613228412,"data":{"turn":2,"step":1,"callId":"call_00_htXcbtvwTFK0NqfclLWL7784","content":[{"type":"text","text":"switched\n"}],"isError":false},"sourceEventSeqs":[144],"surfaceOp":"append"} +{"type":"step/end","seq":146,"time":1783613228412,"data":{"turn":2,"step":1}} +{"type":"step/start","seq":147,"time":1783613228412,"data":{"turn":2,"step":2}} +{"type":"assistant/chunk","seq":148,"time":1783613228863,"data":{"turn":2,"step":2,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}} +{"type":"assistant/chunk","seq":149,"time":1783613228864,"data":{"turn":2,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"Command"}}} +{"type":"assistant/chunk","seq":150,"time":1783613228966,"data":{"turn":2,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" executed"}}} +{"type":"assistant/chunk","seq":151,"time":1783613228994,"data":{"turn":2,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" successfully"}}} +{"type":"assistant/chunk","seq":152,"time":1783613228994,"data":{"turn":2,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":153,"time":1783613228994,"data":{"turn":2,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" The"}}} +{"type":"assistant/chunk","seq":154,"time":1783613228995,"data":{"turn":2,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" output"}}} +{"type":"assistant/chunk","seq":155,"time":1783613228995,"data":{"turn":2,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" is"}}} +{"type":"assistant/chunk","seq":156,"time":1783613229020,"data":{"turn":2,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":" \""}}} +{"type":"assistant/chunk","seq":157,"time":1783613229020,"data":{"turn":2,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"sw"}}} +{"type":"assistant/chunk","seq":158,"time":1783613229020,"data":{"turn":2,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"itched"}}} +{"type":"assistant/chunk","seq":159,"time":1783613229020,"data":{"turn":2,"step":2,"chunk":{"type":"reasoning-delta","index":0,"text":"\"."}}} +{"type":"assistant/chunk","seq":160,"time":1783613229020,"data":{"turn":2,"step":2,"chunk":{"type":"block-start","index":1,"blockType":"text"}}} +{"type":"assistant/chunk","seq":161,"time":1783613229020,"data":{"turn":2,"step":2,"chunk":{"type":"text-delta","index":1,"text":"D"}}} +{"type":"assistant/chunk","seq":162,"time":1783613229048,"data":{"turn":2,"step":2,"chunk":{"type":"text-delta","index":1,"text":"ONE"}}} +{"type":"assistant/chunk","seq":163,"time":1783613229048,"data":{"turn":2,"step":2,"chunk":{"type":"block-end","index":0,"block":{"type":"reasoning","text":"Command executed successfully. The output is \"switched\"."}}}} +{"type":"assistant/chunk","seq":164,"time":1783613229048,"data":{"turn":2,"step":2,"chunk":{"type":"block-end","index":1,"block":{"type":"text","text":"DONE"}}}} +{"type":"assistant/chunk","seq":165,"time":1783613229048,"data":{"turn":2,"step":2,"chunk":{"type":"usage","usage":{"inputTokens":47,"outputTokens":14,"cacheReadTokens":1408,"reasoningTokens":11}}}} +{"type":"assistant/chunk","seq":166,"time":1783613229048,"data":{"turn":2,"step":2,"chunk":{"type":"finish","reason":{"kind":"stop"}}}} +{"type":"assistant/message","seq":167,"time":1783613229049,"data":{"turn":2,"step":2,"content":[{"type":"reasoning","text":"Command executed successfully. The output is \"switched\"."},{"type":"text","text":"DONE"}],"usage":{"inputTokens":47,"outputTokens":14,"cacheReadTokens":1408,"reasoningTokens":11}},"sourceEventSeqs":[148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166],"surfaceOp":"append"} +{"type":"step/end","seq":168,"time":1783613229049,"data":{"turn":2,"step":2}} +{"type":"turn/end","seq":169,"time":1783613229049,"data":{"turn":2,"reason":{"kind":"completed"}}} +{"type":"turn/start","seq":170,"time":1783613229056,"data":{"turn":3,"trigger":{"kind":"message","source":{"kind":"user"}}}} +{"type":"approval/policy","seq":171,"time":1783613229056,"data":{"policy":"never"}} +{"type":"user/message","seq":172,"time":1783613229056,"data":{"content":[{"type":"text","text":"Without using any tools, state your current approval policy in one short sentence and stop."}],"source":{"kind":"user"}},"surfaceOp":"append"} +{"type":"context/message","seq":173,"time":1783613229057,"data":{"content":[{"type":"text","text":"The approval policy changed from \"ask\" to \"never\" (changed by the user)."}],"source":{"kind":"plugin","plugin":"user-approval"}},"surfaceOp":"append"} +{"type":"step/start","seq":174,"time":1783613229057,"data":{"turn":3,"step":1}} +{"type":"request/header-delta","seq":175,"time":1783613229057,"data":{"system":{"keepStart":9,"keepEnd":0,"insert":["{{system}}","{{system}}"]}}} +{"type":"assistant/chunk","seq":176,"time":1783613230100,"data":{"turn":3,"step":1,"chunk":{"type":"block-start","index":0,"blockType":"reasoning"}}} +{"type":"assistant/chunk","seq":177,"time":1783613230100,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"The"}}} +{"type":"assistant/chunk","seq":178,"time":1783613230192,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" user"}}} +{"type":"assistant/chunk","seq":179,"time":1783613230221,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" wants"}}} +{"type":"assistant/chunk","seq":180,"time":1783613230249,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" me"}}} +{"type":"assistant/chunk","seq":181,"time":1783613230250,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" to"}}} +{"type":"assistant/chunk","seq":182,"time":1783613230250,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" state"}}} +{"type":"assistant/chunk","seq":183,"time":1783613230250,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" my"}}} +{"type":"assistant/chunk","seq":184,"time":1783613230250,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" current"}}} +{"type":"assistant/chunk","seq":185,"time":1783613230278,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" approval"}}} +{"type":"assistant/chunk","seq":186,"time":1783613230279,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" policy"}}} +{"type":"assistant/chunk","seq":187,"time":1783613230279,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" without"}}} +{"type":"assistant/chunk","seq":188,"time":1783613230279,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" using"}}} +{"type":"assistant/chunk","seq":189,"time":1783613230306,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" any"}}} +{"type":"assistant/chunk","seq":190,"time":1783613230307,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" tools"}}} +{"type":"assistant/chunk","seq":191,"time":1783613230307,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":192,"time":1783613230307,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" From"}}} +{"type":"assistant/chunk","seq":193,"time":1783613230335,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" the"}}} +{"type":"assistant/chunk","seq":194,"time":1783613230335,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" context"}}} +{"type":"assistant/chunk","seq":195,"time":1783613230335,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":","}}} +{"type":"assistant/chunk","seq":196,"time":1783613230335,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" the"}}} +{"type":"assistant/chunk","seq":197,"time":1783613230363,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" approval"}}} +{"type":"assistant/chunk","seq":198,"time":1783613230363,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" policy"}}} +{"type":"assistant/chunk","seq":199,"time":1783613230364,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" is"}}} +{"type":"assistant/chunk","seq":200,"time":1783613230392,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" \""}}} +{"type":"assistant/chunk","seq":201,"time":1783613230393,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"never"}}} +{"type":"assistant/chunk","seq":202,"time":1783613230393,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"\""}}} +{"type":"assistant/chunk","seq":203,"time":1783613230393,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" —"}}} +{"type":"assistant/chunk","seq":204,"time":1783613230393,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" meaning"}}} +{"type":"assistant/chunk","seq":205,"time":1783613230423,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" actions"}}} +{"type":"assistant/chunk","seq":206,"time":1783613230450,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" that"}}} +{"type":"assistant/chunk","seq":207,"time":1783613230451,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" require"}}} +{"type":"assistant/chunk","seq":208,"time":1783613230451,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" approval"}}} +{"type":"assistant/chunk","seq":209,"time":1783613230451,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" are"}}} +{"type":"assistant/chunk","seq":210,"time":1783613230451,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" rejected"}}} +{"type":"assistant/chunk","seq":211,"time":1783613230478,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" automatically"}}} +{"type":"assistant/chunk","seq":212,"time":1783613230479,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":","}}} +{"type":"assistant/chunk","seq":213,"time":1783613230479,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" and"}}} +{"type":"assistant/chunk","seq":214,"time":1783613230507,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" I"}}} +{"type":"assistant/chunk","seq":215,"time":1783613230507,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" should"}}} +{"type":"assistant/chunk","seq":216,"time":1783613230507,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" not"}}} +{"type":"assistant/chunk","seq":217,"time":1783613230508,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" request"}}} +{"type":"assistant/chunk","seq":218,"time":1783613230508,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" sand"}}} +{"type":"assistant/chunk","seq":219,"time":1783613230536,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"box"}}} +{"type":"assistant/chunk","seq":220,"time":1783613230536,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":" escalation"}}} +{"type":"assistant/chunk","seq":221,"time":1783613230536,"data":{"turn":3,"step":1,"chunk":{"type":"reasoning-delta","index":0,"text":"."}}} +{"type":"assistant/chunk","seq":222,"time":1783613230537,"data":{"turn":3,"step":1,"chunk":{"type":"block-start","index":1,"blockType":"text"}}} +{"type":"assistant/chunk","seq":223,"time":1783613230537,"data":{"turn":3,"step":1,"chunk":{"type":"text-delta","index":1,"text":"Appro"}}} +{"type":"assistant/chunk","seq":224,"time":1783613230564,"data":{"turn":3,"step":1,"chunk":{"type":"text-delta","index":1,"text":"vals"}}} +{"type":"assistant/chunk","seq":225,"time":1783613230564,"data":{"turn":3,"step":1,"chunk":{"type":"text-delta","index":1,"text":" are"}}} +{"type":"assistant/chunk","seq":226,"time":1783613230564,"data":{"turn":3,"step":1,"chunk":{"type":"text-delta","index":1,"text":" never"}}} +{"type":"assistant/chunk","seq":227,"time":1783613230565,"data":{"turn":3,"step":1,"chunk":{"type":"text-delta","index":1,"text":" asked"}}} +{"type":"assistant/chunk","seq":228,"time":1783613230605,"data":{"turn":3,"step":1,"chunk":{"type":"text-delta","index":1,"text":";"}}} +{"type":"assistant/chunk","seq":229,"time":1783613230606,"data":{"turn":3,"step":1,"chunk":{"type":"text-delta","index":1,"text":" any"}}} +{"type":"assistant/chunk","seq":230,"time":1783613230625,"data":{"turn":3,"step":1,"chunk":{"type":"text-delta","index":1,"text":" action"}}} +{"type":"assistant/chunk","seq":231,"time":1783613230626,"data":{"turn":3,"step":1,"chunk":{"type":"text-delta","index":1,"text":" requiring"}}} +{"type":"assistant/chunk","seq":232,"time":1783613230626,"data":{"turn":3,"step":1,"chunk":{"type":"text-delta","index":1,"text":" approval"}}} +{"type":"assistant/chunk","seq":233,"time":1783613230653,"data":{"turn":3,"step":1,"chunk":{"type":"text-delta","index":1,"text":" is"}}} +{"type":"assistant/chunk","seq":234,"time":1783613230654,"data":{"turn":3,"step":1,"chunk":{"type":"text-delta","index":1,"text":" automatically"}}} +{"type":"assistant/chunk","seq":235,"time":1783613230654,"data":{"turn":3,"step":1,"chunk":{"type":"text-delta","index":1,"text":" rejected"}}} +{"type":"assistant/chunk","seq":236,"time":1783613230654,"data":{"turn":3,"step":1,"chunk":{"type":"text-delta","index":1,"text":"."}}} +{"type":"assistant/chunk","seq":237,"time":1783613230689,"data":{"turn":3,"step":1,"chunk":{"type":"block-end","index":0,"block":{"type":"reasoning","text":"The user wants me to state my current approval policy without using any tools. From the context, the approval policy is \"never\" — meaning actions that require approval are rejected automatically, and I should not request sandbox escalation."}}}} +{"type":"assistant/chunk","seq":238,"time":1783613230689,"data":{"turn":3,"step":1,"chunk":{"type":"block-end","index":1,"block":{"type":"text","text":"Approvals are never asked; any action requiring approval is automatically rejected."}}}} +{"type":"assistant/chunk","seq":239,"time":1783613230689,"data":{"turn":3,"step":1,"chunk":{"type":"usage","usage":{"inputTokens":1530,"outputTokens":60,"cacheReadTokens":0,"reasoningTokens":45}}}} +{"type":"assistant/chunk","seq":240,"time":1783613230689,"data":{"turn":3,"step":1,"chunk":{"type":"finish","reason":{"kind":"stop"}}}} +{"type":"assistant/message","seq":241,"time":1783613230690,"data":{"turn":3,"step":1,"content":[{"type":"reasoning","text":"The user wants me to state my current approval policy without using any tools. From the context, the approval policy is \"never\" — meaning actions that require approval are rejected automatically, and I should not request sandbox escalation."},{"type":"text","text":"Approvals are never asked; any action requiring approval is automatically rejected."}],"usage":{"inputTokens":1530,"outputTokens":60,"cacheReadTokens":0,"reasoningTokens":45}},"sourceEventSeqs":[176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240],"surfaceOp":"append"} +{"type":"step/end","seq":242,"time":1783613230690,"data":{"turn":3,"step":1}} +{"type":"turn/end","seq":243,"time":1783613230690,"data":{"turn":3,"reason":{"kind":"completed"}}} diff --git a/examples/sandbox-acp-agent/tests/snapshots/mode-switching/stdout.golden.jsonl b/examples/sandbox-acp-agent/tests/snapshots/mode-switching/stdout.golden.jsonl new file mode 100644 index 0000000000..e6b16698af --- /dev/null +++ b/examples/sandbox-acp-agent/tests/snapshots/mode-switching/stdout.golden.jsonl @@ -0,0 +1,134 @@ +{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":1,"agentInfo":{"name":"deepseek-harness-acp","version":"0.0.1"},"agentCapabilities":{"loadSession":true,"promptCapabilities":{"image":false,"audio":false,"embeddedContext":false}},"authMethods":[]}} +{"jsonrpc":"2.0","id":2,"result":{"sessionId":"{{sessionId}}","configOptions":[{"id":"sandbox-mode","name":"Sandbox","description":"The file sandbox mode bash commands in this session run under.","category":"mode","type":"select","currentValue":"read-only","options":[{"value":"read-only","name":"read-only"},{"value":"workspace-write","name":"workspace-write"},{"value":"danger-full-access","name":"danger-full-access"}]},{"id":"approval-policy","name":"Approvals","description":"ask: permission prompts reach you; never: they are rejected automatically.","type":"select","currentValue":"ask","options":[{"value":"ask","name":"ask"},{"value":"never","name":"never"}]}]}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"The"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" user"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" wants"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" me"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" to"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" run"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" `"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"cat"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" notes"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":".txt"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"`"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" using"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" the"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" bash"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" tool"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"tool_call","toolCallId":"call_00_I9sT0gKlfz9YP4dlf2pA3899","title":"cat notes.txt","kind":"execute","status":"in_progress","rawInput":"cat notes.txt","content":[{"type":"content","content":{"type":"text","text":"Show contents of notes.txt"}}]}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"tool_call_update","toolCallId":"call_00_I9sT0gKlfz9YP4dlf2pA3899","status":"completed","content":[{"type":"content","content":{"type":"text","text":"```console\nhello from the sandboxed workspace\n```"}}]}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"The"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" command"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" ran"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" successfully"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" The"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" user"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" asked"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" me"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" to"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" reply"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" with"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" the"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" single"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" word"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" D"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"ONE"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"D"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"ONE"}}}} +{"jsonrpc":"2.0","id":3,"result":{"stopReason":"end_turn"}} +{"jsonrpc":"2.0","id":4,"result":{"configOptions":[{"id":"sandbox-mode","name":"Sandbox","description":"The file sandbox mode bash commands in this session run under.","category":"mode","type":"select","currentValue":"workspace-write","options":[{"value":"read-only","name":"read-only"},{"value":"workspace-write","name":"workspace-write"},{"value":"danger-full-access","name":"danger-full-access"}]},{"id":"approval-policy","name":"Approvals","description":"ask: permission prompts reach you; never: they are rejected automatically.","type":"select","currentValue":"ask","options":[{"value":"ask","name":"ask"},{"value":"never","name":"never"}]}]}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"The"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" user"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" wants"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" me"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" to"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" run"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" a"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" specific"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" command"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" Let"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" me"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" execute"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" it"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"tool_call","toolCallId":"call_00_htXcbtvwTFK0NqfclLWL7784","title":"printf 'switched\\n' > out.txt && cat out.txt","kind":"execute","status":"in_progress","rawInput":"printf 'switched\\n' > out.txt && cat out.txt","content":[{"type":"content","content":{"type":"text","text":"Write and read out.txt"}}]}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"tool_call_update","toolCallId":"call_00_htXcbtvwTFK0NqfclLWL7784","status":"completed","content":[{"type":"content","content":{"type":"text","text":"```console\nswitched\n```"}}]}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"Command"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" executed"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" successfully"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" The"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" output"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" is"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" \""}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"sw"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"itched"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"\"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"D"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"ONE"}}}} +{"jsonrpc":"2.0","id":5,"result":{"stopReason":"end_turn"}} +{"jsonrpc":"2.0","id":6,"result":{"configOptions":[{"id":"sandbox-mode","name":"Sandbox","description":"The file sandbox mode bash commands in this session run under.","category":"mode","type":"select","currentValue":"workspace-write","options":[{"value":"read-only","name":"read-only"},{"value":"workspace-write","name":"workspace-write"},{"value":"danger-full-access","name":"danger-full-access"}]},{"id":"approval-policy","name":"Approvals","description":"ask: permission prompts reach you; never: they are rejected automatically.","type":"select","currentValue":"never","options":[{"value":"ask","name":"ask"},{"value":"never","name":"never"}]}]}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"The"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" user"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" wants"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" me"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" to"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" state"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" my"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" current"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" approval"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" policy"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" without"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" using"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" any"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" tools"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" From"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" the"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" context"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":","}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" the"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" approval"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" policy"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" is"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" \""}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"never"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"\""}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" —"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" meaning"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" actions"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" that"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" require"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" approval"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" are"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" rejected"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" automatically"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":","}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" and"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" I"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" should"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" not"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" request"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" sand"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"box"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" escalation"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"Appro"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"vals"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" are"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" never"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" asked"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":";"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" any"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" action"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" requiring"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" approval"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" is"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" automatically"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" rejected"}}}} +{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"{{sessionId}}","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"."}}}} +{"jsonrpc":"2.0","id":7,"result":{"stopReason":"end_turn"}} diff --git a/examples/sandbox-acp-agent/tests/snapshots/mode-switching/system-prompt.golden.md b/examples/sandbox-acp-agent/tests/snapshots/mode-switching/system-prompt.golden.md new file mode 100644 index 0000000000..2da5f39805 --- /dev/null +++ b/examples/sandbox-acp-agent/tests/snapshots/mode-switching/system-prompt.golden.md @@ -0,0 +1,15 @@ +You are an AI agent powered by the DeepSeek Harness SDK. + +You are a coding assistant powered by the deepseek-v4-flash model. Your working directory is {{cwd}}. Your bash tool runs under a file sandbox — a `[sandbox: file access denied …]` result is policy, not a command bug. + +Verify your work by running the code or tests. Keep answers brief and factual. + + +Check the [exit code: N] marker on every bash result; investigate failures before moving on. + + + + + +Approval prompts are disabled in this session: actions that require approval are rejected automatically — do not request sandbox escalation (do not set `sandbox_permissions`). + diff --git a/examples/sandbox-acp-agent/tests/snapshots/mode-switching/workspace/notes.txt b/examples/sandbox-acp-agent/tests/snapshots/mode-switching/workspace/notes.txt new file mode 100644 index 0000000000..a6eda7f939 --- /dev/null +++ b/examples/sandbox-acp-agent/tests/snapshots/mode-switching/workspace/notes.txt @@ -0,0 +1 @@ +hello from the sandboxed workspace diff --git a/knip.json b/knip.json index 1c5b0f6b6f..cf43b90e34 100644 --- a/knip.json +++ b/knip.json @@ -1,6 +1,7 @@ { "$schema": "https://unpkg.com/knip@5/schema.json", "exclude": ["duplicates"], + "ignoreBinaries": ["bwrap", "sandbox-exec"], "ignoreWorkspaces": ["vendor/*"], "workspaces": { ".": { @@ -18,6 +19,14 @@ "entry": ["tests/**/*.spec.ts"], "project": ["src/**/*.ts", "tests/**/*.ts"] }, + "packages/bash/bash-sandbox": { + "entry": ["tests/**/*.spec.ts", "tests/**/*.e2e.ts"], + "project": ["src/**/*.ts", "tests/**/*.ts"] + }, + "packages/sandbox/sandbox-local": { + "entry": ["tests/**/*.spec.ts", "tests/**/*.e2e.ts"], + "project": ["src/**/*.ts", "tests/**/*.ts"] + }, "packages/util/brand": { "project": ["src/**/*.ts"], "ignoreDependencies": ["cordis"] diff --git a/package.json b/package.json index c4cc307893..ed0b603feb 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "test:e2e": "vitest run --config vitest.e2e.config.ts", "test:snapshot": "vitest run --config vitest.snapshot.config.ts", "test:snapshot:record": "DSH_SNAPSHOT=record vitest run --config vitest.snapshot.config.ts --update", + "test:snapshot:refresh": "DSH_SNAPSHOT=refresh vitest run --config vitest.snapshot.config.ts", "check:ci": "tsx scripts/run-gates.ts ci-primary", "check:ci:static": "tsx scripts/run-gates.ts ci-static", "check:ci:lint": "tsx scripts/run-gates.ts ci-lint", @@ -59,15 +60,17 @@ "gen-persistence-catalog": "tsx scripts/gen-persistence-catalog.ts", "verify-persistence-catalog": "tsx scripts/gen-persistence-catalog.ts --check", "gen-module-graph": "tsx scripts/gen-module-graph.ts", + "verify-scoped-dispatch": "tsx scripts/verify-scoped-dispatch.ts", "verify-module-graph": "tsx scripts/gen-module-graph.ts --check", "constraints": "tsx scripts/check-workspace-constraints.ts", - "doc-sync": "pnpm run doc-typecheck && pnpm run verify-cordis-catalog && pnpm run verify-cordis-api && pnpm run verify-export-jsdoc && pnpm run verify-tool-catalog && pnpm run verify-config-catalog && pnpm run verify-persistence-catalog && pnpm run verify-doc-graphs && pnpm run verify-md-wrap && pnpm run verify-md-links && pnpm run verify-doc-refs && pnpm run verify-package-paths && pnpm run verify-mermaid && pnpm run verify-rfc-classification && pnpm run verify-rfc-format && pnpm run verify-type-equiv && pnpm run verify-translation-pairing && pnpm run verify-doc-budgets", + "doc-sync": "pnpm run doc-typecheck && pnpm run verify-cordis-catalog && pnpm run verify-cordis-api && pnpm run verify-export-jsdoc && pnpm run verify-tool-catalog && pnpm run verify-config-catalog && pnpm run verify-persistence-catalog && pnpm run verify-doc-graphs && pnpm run verify-scoped-dispatch && pnpm run verify-md-wrap && pnpm run verify-md-links && pnpm run verify-doc-refs && pnpm run verify-package-paths && pnpm run verify-mermaid && pnpm run verify-rfc-classification && pnpm run verify-rfc-format && pnpm run verify-type-equiv && pnpm run verify-translation-pairing && pnpm run verify-doc-budgets", "hygiene": "pnpm run knip && pnpm run publint && pnpm run constraints && pnpm run verify-node-next-types", "demo:echo": "node --expose-internals --import tsx packages/ui/stdio-agent/src/bin.ts examples/echo-agent/cordis.yml", "demo:repl": "node --expose-internals --import tsx packages/ui/stdio-agent/src/bin.ts examples/coding-agent/cordis.yml", "demo:code-mode": "node scripts/demo-code-mode.mjs", "demo:cordis": "node --expose-internals --import tsx packages/ui/stdio-agent/src/bin.ts examples/cordis-agent/cordis.yml", "demo:acp": "node --import tsx packages/ui/acp-agent/src/bin.ts examples/acp-agent/cordis.yml", + "demo:sandbox-acp": "node --import tsx packages/ui/acp-agent/src/bin.ts examples/sandbox-acp-agent/cordis.yml", "postinstall": "node scripts/install-lefthook.mjs" }, "devDependencies": { diff --git a/packages/AGENTS.md b/packages/AGENTS.md index ac26b926f7..4b7bed4d1c 100644 --- a/packages/AGENTS.md +++ b/packages/AGENTS.md @@ -5,6 +5,8 @@ This directory contains all `@deepseek-ai/dsh-*` harness packages. Repo-wide con - **Plugin export shape — namespace OR default, never both.** A *service* package exports the service class as `export default` (the Loader instantiates it). A *function/namespace* plugin exports `name` / `inject` / `Config` / `apply` as separate named exports and **must NOT add `export default`** — the cordis Loader's `unwrapExports` does `exports.default ?? exports`, so a stray default export collapses the module to the bare `apply` function and silently discards the `inject`/`name`/`Config` namespace, leaving the plugin with no injected services (it then throws `cannot get property … without inject` at load). See [docs/postmortem/0001](../docs/postmortem/0001-acp-default-export-drops-inject.md). - **Read an optional (non-injected) service via `ctx.get(name)`, not `ctx.`.** For a service a plugin reads opportunistically but deliberately leaves out of `static inject` (e.g. `AgentLoop` reading `sessionPersistence`), the `ctx.` property proxy resolves by an ancestor-only fiber walk that throws when the call arrives through a foreign traceable shadow (the service lives on a sibling fiber). `ctx.get(name)` is the topology-independent global-store lookup, strict by default (an inactive/absent backend reads as `undefined` — prefer it over the `ctx.get(name, false)` overload, which also skips the active-state check). Services that ARE in `static inject` resolve fine via `ctx.`. See [docs/postmortem/0001](../docs/postmortem/0001-acp-default-export-drops-inject.md). - **A plugin shipped via `cordis.yml` needs at least one test through the REAL Loader/export path** — hand-built `ctx.plugin({...})` mounts bypass `unwrapExports` and cannot catch a broken export shape. Full testing policy (tiers, with-key generosity, real-entry-path guards): [docs/testing.md](../docs/testing.md). +- **Typed same-process service and plugin calls are contracts, not serialization boundaries.** Prefer readonly borrowed values; materialize or defensively validate only at parser/config, queued, model/tool JSON, durable/file, worker, process, or wire boundaries. +- **Represent one asynchronous operation with one lifecycle controller or transaction.** Separate readiness, cancellation, disposal, reservation, or sentinel state requires an independent owner or settlement boundary; otherwise fold it while preserving rollback, callback containment, and quiescence. Naming notes: diff --git a/packages/README.md b/packages/README.md index a656812253..35c4652a1f 100644 --- a/packages/README.md +++ b/packages/README.md @@ -1,10 +1,10 @@ # Packages -Harness packages, all under the `@deepseek-ai/dsh-*` scope. Each package is a Cordis plugin (microkernel-style): it exports either a default `Service` subclass or a functional plugin, declares its ctx key/events through declaration merging, and exposes extension points through `ctx.effect()`, `ctx.on()`, and `ctx.waterfall()`. Authoring conventions: [AGENTS.md](AGENTS.md) (subtree) and the root [AGENTS.md](../AGENTS.md) § Conventions. +Harness packages use the `@deepseek-ai/dsh-*` scope. Each is a Cordis plugin: a default `Service` subclass or functional plugin declaring ctx keys/events through declaration merging and contributing through `ctx.effect()`, `ctx.on()`, or `ctx.waterfall()`. Authoring conventions: [AGENTS.md](AGENTS.md) and root [AGENTS.md](../AGENTS.md) § Conventions. ## Hierarchy -Packages are grouped by modular role at `packages///`. The group directory is a pure container (no `package.json` of its own); the package name stays `@deepseek-ai/dsh-` regardless of group. **Each group README is the canonical per-package map** — package roles, ctx keys, and the product-vs-support split live there, next to the code. +Packages are grouped by modular role at `packages///`. The group directory is a pure container (no `package.json`); the package name stays `@deepseek-ai/dsh-` regardless of group. **Each group README is the canonical per-package map** — package roles, ctx keys, and the product-vs-support split live there, next to the code. | Group | Role | Release expectation | |---|---|---| @@ -12,7 +12,9 @@ Packages are grouped by modular role at `packages///`. The group dir | [`llm/`](llm/README.md) | LLM capability family: the abstract service + provider adapters | Product — stable surface | | [`bash/`](bash/README.md) | Bash capability family: the executor seam, a local impl, and the model-facing tool | Product — stable surface | | [`code-runtime/`](code-runtime/README.md) | Code-execution capability family: the abstract runtime seam for model-written programs + a worker-thread backend | Product — stable surface | +| [`sandbox/`](sandbox/README.md) | Process-confinement seam; bwrap/Landlock/Seatbelt backends | Product — stable surface | | [`fs/`](fs/README.md) | Filesystem capability family: the abstract seam, a local impl, and the model-facing file tools | Product — stable surface | +| [`skill/`](skill/README.md) | Skill capability family: the provider registry, local provider, and model-facing catalog/loader | Product — stable surface | | [`compact/`](compact/README.md) | Compaction capability family: the abstract seam + a basic backend (tool deferred) | Product — stable surface | | [`subagent/`](subagent/README.md) | Subagent capability family: the provider-registry seam and the model-facing delegation tool | Product — stable surface | | [`workflow/`](workflow/README.md) | Workflow capability family: the script-engine seam, the worker-thread engine, and the model-facing `workflow` tool | Product — stable surface | @@ -23,7 +25,7 @@ Packages are grouped by modular role at `packages///`. The group dir | [`cordis/`](cordis/README.md) | Self-referential runtime toolset: inspect the live runtime's plugins and services, mount/unmount model-written plugins ([design](../docs/rfc/implemented/feature/2026-07-08-self-referential-cordis-toolset.md)) | Product — stable surface | | [`hooks/`](hooks/README.md) | Hook bridges + the shared Claude Code / Codex wire-protocol library | Product — stable surface | | [`session-persistence/`](session-persistence/README.md) | Persistence capability family: the seam + JSONL/SQLite backends | Product — stable surface | -| [`ui/`](ui/README.md) | Editor/client integration surfaces: ACP bridge, app packages, user-interaction seam, ask-user tool | Product — stable surface | +| [`ui/`](ui/README.md) | Editor/client integration surfaces: ACP bridge, app packages, user-approval and user-interaction seams, ask-user tool | Product — stable surface | | [`support/`](support/README.md) | Dev/test/example infrastructure (invariants, replay adapter, subagent mock) | Support — lower compatibility expectations | | [`util/`](util/README.md) | Low-level zero-dependency utilities shared across groups (the `Branded` primitive) | Support — small, stable, harness-dep-free | diff --git a/packages/bash/README.md b/packages/bash/README.md index 9a9dba88d5..4072998f3e 100644 --- a/packages/bash/README.md +++ b/packages/bash/README.md @@ -1,11 +1,12 @@ # bash/ — bash capability family -The canonical three-package capability seam (see [capability seams](../../docs/rfc/implemented/architecture/2026-06-13-capability-seams.md)): an abstract executor interface, a concrete local implementation, and the model-facing tool that consumes it. All **product** packages. +The canonical three-package capability seam (see [capability seams](../../docs/rfc/implemented/architecture/2026-06-13-capability-seams.md)): an abstract executor interface, concrete implementations, and the model-facing tool that consumes it. All **product** packages. | Package | Role | ctx key | |---|---|---| -| `bash/` | Abstract bash executor seam (interface + vocabulary) | `ctx.bash` | +| `bash/` | Abstract bash executor seam (interface + vocabulary; sandbox result facts carry the [`sandbox/`](../sandbox/README.md) seam's mode/enforcement vocabulary) | `ctx.bash` | | `bash-local/` | Local-subprocess `BashExecutor` implementation | (registers `ctx.bash`) | +| `bash-sandbox/` | Sandbox-consuming `BashExecutor` (wraps every command argv via `ctx.sandbox`, stamps denial/enforcement facts; extends `bash-local`'s mechanics) | (registers `ctx.bash`) | | `tool-bash/` | Model-facing `bash`/`bash_output`/`bash_kill` tool schemas | (registers on `ctx.tools`) | -The interface lives at `bash/bash/`. A sandboxed executor would replace `bash-local` without touching the interface or the tool — the split is what makes that possible. +The interface lives at `bash/bash/`. `bash-sandbox` replacing `bash-local` without touching the interface or the tool is the split doing exactly what it exists for — a leaf `cordis.yml` picks one executor entry, plus a `ctx.sandbox` provider entry for the confined one (see [examples/sandbox-acp-agent](../../examples/sandbox-acp-agent/)). diff --git a/packages/bash/bash-local/README.md b/packages/bash/bash-local/README.md index dec29ce93b..1ac8a7d06b 100644 --- a/packages/bash/bash-local/README.md +++ b/packages/bash/bash-local/README.md @@ -27,4 +27,4 @@ Design surveyed against the bash tools of Claude Code, OpenCode, Codex, and pi; ## Sandboxing -`TODO(permissions/sandbox)`: execution policy does NOT belong in this package. Use the `tools/pre-execute` deny/ask gate or implement a sandboxing `BashExecutor` — see docs/architecture.md § Extending The Harness. Reference points: Claude Code wraps commands in sandbox-exec/bubblewrap; Codex applies seatbelt/landlock plus an execpolicy prefix-rule engine. +Execution policy does NOT belong in this package: this executor always runs commands unconfined. Confinement is [`dsh-bash-sandbox`](../bash-sandbox/README.md), which extends this executor verbatim and confines commands under the `ctx.sandbox` seam's bwrap/Landlock/Seatbelt backends ([sandbox RFC](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md)); per-call allow/deny/ask policy belongs on the `tools/pre-execute` gate. diff --git a/packages/bash/bash-local/src/index.ts b/packages/bash/bash-local/src/index.ts index 3e09d7e35b..6903c06e32 100644 --- a/packages/bash/bash-local/src/index.ts +++ b/packages/bash/bash-local/src/index.ts @@ -133,6 +133,10 @@ export class LocalBashExecutor extends BashExecutor { // Carry the owner through verbatim (required-but-nullable on the spec): // the executor never interprets it — the consumer's access policy does. owner: request.owner, + // Carry a sandbox-mode override through verbatim: this executor never + // confines, so the field is inert here (the seam contract) — a + // sandboxing subclass overrides resolve() to stamp its default instead. + sandboxMode: request.sandboxMode, } } @@ -212,6 +216,18 @@ export class LocalBashExecutor extends BashExecutor { return this.tasks.get(id) } + /** + * Full collected stderr of a tracked task from stream start (bounded by the + * in-memory cap; bytes only in the spill file are not re-read). A protected + * seam for subclasses that classify a settled task's outcome — reading here + * does NOT advance the consumer's {@link readOutput} cursor. An unknown id + * (a task already dropped by disposal) reads as empty. + */ + protected collectedStderr(id: BashTaskId): string { + const task = this.tasks.get(id) + return task === undefined ? '' : task.running.stderr.readFrom(0).text + } + ownerOf(id: BashTaskId): OwnerToken | undefined { // Unknown id and known-but-ownerless both read as undefined — the consumer // treats undefined as "open" and a truly unknown id fails at readOutput/kill. diff --git a/packages/bash/bash-sandbox/README.md b/packages/bash/bash-sandbox/README.md new file mode 100644 index 0000000000..2a5ae63b8e --- /dev/null +++ b/packages/bash/bash-sandbox/README.md @@ -0,0 +1,33 @@ +# @deepseek-ai/dsh-bash-sandbox + +Sandbox-consuming implementation of the [`@deepseek-ai/dsh-bash`](../bash/) executor seam. Load it **instead of** `@deepseek-ai/dsh-bash-local`, together with a [`ctx.sandbox`](../../sandbox/sandbox/) provider (e.g. [`@deepseek-ai/dsh-sandbox-local`](../../sandbox/sandbox-local/)) — the model-facing tool layer (`dsh-tool-bash`) is untouched; that swap is exactly what the seams exist for. + +Every command is confined by handing the provider the exact `['bash', '-c', command]` argv this executor is about to spawn and spawning the returned (wrapped) argv instead. WHICH platform runner confines it — and whether one is usable at all (fail closed with a structured `SANDBOX_UNAVAILABLE` error, never a silent unconfined run) — is the provider's concern; this package owns the bash side only. + +| Mode | File effects | +|---|---| +| `read-only` (default) | No writes anywhere (of `/dev`, only the `/dev/null` node is writable, so `>/dev/null` keeps working) | +| `workspace-write` | Writes only under `workspaceRoot` + `/tmp` (ephemeral under bwrap, the host `/tmp` under Landlock, `/private/tmp` plus the per-user temp dir under Seatbelt) | +| `danger-full-access` | No confinement; the provider is never consulted. Execution is `dsh-bash-local`'s verbatim — foreground results still carry `sandbox: { mode, denied: false }` (no `enforcement`: nothing was confined), background tasks carry no sandbox facts | + +Semantics: + +- **Denials are result facts.** A failed run whose stderr carries the selected backend's own denial dialect — the signatures the provider stamps on every wrap (EROFS text under bwrap, EACCES under Landlock, EPERM under Seatbelt) — is reported as `BashRunResult.sandbox.denied: true` (conservative classification, read from the collected stderr tail); every CONFINED run also carries the mode it executed under (`result.sandbox.mode`) and the provider's enforcement completeness (`result.sandbox.enforcement`: `full`, or `partial` on an older Landlock ABI). +- **Runner failures are sandbox failures, never task failures.** A failed run matching the wrap's `runnerFailureSignatures` (the runner's own error prefix — also what the shell prints for a missing runner) means the sandbox itself broke and the command NEVER RAN; the check outranks denial classification because a runner's error text can contain denial words. The foreground path re-throws it as the structured fail-closed `SANDBOX_UNAVAILABLE` error, with the runner's first stderr line as the cause; a settled background task stamps `task.sandbox.runnerFailed` instead (no error channel remains after settle), which `bash_output` renders as its own marker. +- **Config-time default, per-call policy.** The DEFAULT mode is fixed by this entry's config for the executor's lifetime; `resolve()` stamps it onto every spec, and an explicit request-level `sandboxMode` override — set by the tool layer only for a call whose wider mode a human granted through `ctx.approval` ([the sandbox RFC § Escalation](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md)) — makes THAT call run, classify, and report under its own mode while every neighbor keeps the default (background facts are stamped per task at settle). The capability fact `ctx.bash.sandboxMode` reports the configured default so the tool layer advertises escalation only when this executor is mounted. The model learns of the sandbox only through result facts — the static bash tool description explains the denial marker; there is no current-mode statement in the system prompt. +- **File effects only.** Network and process visibility are deliberately not restricted — the mode vocabulary does not pretend to cover what the backend does not enforce. +- Process mechanics (spawn, process-group kills, output collection/spill, background tasks, credential scrub) are inherited verbatim from [`dsh-bash-local`](../bash-local/); the runner ladder, probes, and the per-platform Landlock launcher packages live with [`dsh-sandbox-local`](../../sandbox/sandbox-local/). + +Deny-only at the seam: a denial is a reported fact, and this executor never negotiates permissions itself — the approval question lives in the tool layer (`dsh-tool-bash`), which drives the override this package honors. + +```yaml +- id: sandbox + name: '@deepseek-ai/dsh-sandbox-local' +- id: bash + name: '@deepseek-ai/dsh-bash-sandbox' + config: + mode: read-only + workspaceRoot: !!js process.cwd() +``` + +The keyless consumer-integration proofs are `tests/bwrap.e2e.ts`, `tests/landlock.e2e.ts`, and `tests/seatbelt.e2e.ts` (the real provider + real runner driven through `ctx.bash`, world-verified, each self-skipping where its runner is absent); see [`examples/sandbox-acp-agent`](../../../examples/sandbox-acp-agent/) for the runnable demo. diff --git a/packages/bash/bash-sandbox/package.json b/packages/bash/bash-sandbox/package.json new file mode 100644 index 0000000000..0077511946 --- /dev/null +++ b/packages/bash/bash-sandbox/package.json @@ -0,0 +1,41 @@ +{ + "name": "@deepseek-ai/dsh-bash-sandbox", + "description": "Sandbox-consuming implementation of the DeepSeek Harness bash executor seam (confines every command via ctx.sandbox, reports denial/enforcement result facts)", + "version": "0.0.1", + "private": true, + "type": "module", + "main": "lib/index.js", + "types": "lib/types/index.d.ts", + "exports": { + ".": { + "types": "./lib/types/index.d.ts", + "default": "./lib/index.js" + }, + "./src/*": "./src/*", + "./package.json": "./package.json" + }, + "files": [ + "lib/index.js", + "lib/types/**/*.d.ts", + "lib/types/**/*.d.ts.map", + "src" + ], + "license": "BSD-3-Clause", + "peerDependencies": { + "@deepseek-ai/dsh-bash": "^0.0.1", + "@deepseek-ai/dsh-bash-local": "^0.0.1", + "@deepseek-ai/dsh-sandbox": "^0.0.1", + "cordis": "^4.0.0-rc.6" + }, + "dependencies": { + "schemastery": "^3.18.0" + }, + "devDependencies": { + "@deepseek-ai/dsh-bash": "workspace:^", + "@deepseek-ai/dsh-bash-local": "workspace:^", + "@deepseek-ai/dsh-sandbox": "workspace:^", + "@deepseek-ai/dsh-sandbox-local": "workspace:^", + "node-addon-landlock-run": "0.0.0-test.0", + "cordis": "^4.0.0-rc.6" + } +} diff --git a/packages/bash/bash-sandbox/src/index.ts b/packages/bash/bash-sandbox/src/index.ts new file mode 100644 index 0000000000..090d06b2fe --- /dev/null +++ b/packages/bash/bash-sandbox/src/index.ts @@ -0,0 +1,302 @@ +/** + * `SandboxBashExecutor`: the sandbox-consuming implementation of the + * `@deepseek-ai/dsh-bash` executor seam. Every spawned command is wrapped by + * the `ctx.sandbox` provider (`@deepseek-ai/dsh-sandbox`) according to the + * configured {@link SandboxMode}: the executor hands the provider the exact + * `['bash', '-c', command]` argv it is about to spawn and spawns the wrapped + * argv instead. WHICH platform runner confines it — and whether one is + * usable at all (the provider fails CLOSED with a structured + * `SANDBOX_UNAVAILABLE` error rather than passing the argv through) — is the + * provider's concern (`@deepseek-ai/dsh-sandbox-local` first). + * + * Extends `LocalBashExecutor` so all process mechanics — spawn, process-group + * kills, timeout escalation, output collection and spill files, background + * tasks, the credential scrub — are the local implementation's, verbatim. + * This package adds only the seam consumption and the result facts, which is + * exactly the split the capability seam was designed for (a sandboxing + * executor replaces `dsh-bash-local` without touching `dsh-tool-bash`, and + * swapping the confinement backend never touches this package). + * + * A failed run whose stderr carries the selected backend's own denial + * dialect (the signatures the provider stamps on every wrap) is classified + * as a sandbox denial on `BashRunResult.sandbox`, and every confined result + * also carries how completely the selected runner enforces the mode + * (`sandbox.enforcement`, from the provider's wrap). A failure carrying the + * backend's RUNNER-FAILURE signature instead means the sandbox itself broke + * and the command never ran: the foreground path re-throws it as the + * structured fail-closed `SANDBOX_UNAVAILABLE` error (late twin of the + * provider's confine-time throw), a settled background task stamps + * `sandbox.runnerFailed` — either way a broken sandbox can never read as a + * failing command, and the command never slips through unconfined. + * + * Deny-only at the seam, escalation at the tool: a denial is a reported FACT + * here, and the one-shot user-approved escalated retry of a denied action + * (docs/rfc/implemented/feature/2026-07-06-sandbox.md) is driven by + * `dsh-tool-bash` through `ctx.approval` — this executor's contribution is the + * per-call `sandboxMode` override it honors in {@link resolve}: an escalated + * call runs (and classifies, and reports) under ITS granted mode while every + * neighboring call keeps its session's standing mode (or the configured + * default when that session has no override). + * + * @module @deepseek-ai/dsh-bash-sandbox + */ + +import { resolve } from 'node:path' +import { Context } from 'cordis' +import z from 'schemastery' +import type { BashExecRequest, BashExecSpec, BashRunResult, BashTask, BashTaskId } from '@deepseek-ai/dsh-bash' +import { SandboxUnavailableError } from '@deepseek-ai/dsh-sandbox' +import type { ConfinedSandboxMode, SandboxEnforcement, SandboxMode } from '@deepseek-ai/dsh-sandbox' +import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local' +import type { Config as LocalConfig } from '@deepseek-ai/dsh-bash-local' + +/** + * Plugin config: the local executor's knobs plus the sandbox policy. All + * optional — `static Config` supplies the defaults (`mode: 'read-only'` is the + * fail-safe default; an example that wants a workspace-writable agent opts in + * explicitly). The runner choice is NOT configured here: which platform + * backend confines the command is the `ctx.sandbox` provider's config. + */ +export interface Config extends LocalConfig { + /** File-sandbox mode commands run under (default: `read-only`). */ + mode?: SandboxMode + /** + * Root directory `workspace-write` mode may write under (default: the + * executor's default working directory — `cwd`, else `process.cwd()`). + */ + workspaceRoot?: string +} + +/** + * Quote one string as a single-quoted POSIX shell word (embedded single + * quotes become `'\''`), so a wrapped argv element survives the outer + * `bash -c` re-parse byte-for-byte. + * @param text - the raw argv element to quote. + * @returns the single-quoted shell word. + */ +export function shellQuote(text: string): string { + return `'${text.replaceAll("'", String.raw`'\''`)}'` +} + +/** + * Conservative sandbox-denial classifier: a run counts as denied only when it + * FAILED (nonzero exit — a signal kill is not a denial) and its stderr + * carries one of the SELECTED BACKEND's own denial signatures — the dialect + * the provider stamps on every wrap (`ConfinedArgv.denialSignatures`: + * `Read-only file system` under bwrap's EROFS mounts, `Permission denied` + * under Landlock's EACCES, `Operation not permitted` under Seatbelt's + * EPERM). Matching the backend's dialect rather than a cross-backend union + * keeps the classifier from claiming denials the active backend never + * produces (bare EPERM text under a Linux runner names non-file boundaries — + * mount, kill, ptrace — that fail the same way unsandboxed). Text inference + * is the fallback signal until a runner provides a structured one (which + * wins once it exists); it errs toward NOT claiming a denial, and its known + * residual imprecision is non-sandbox text in the active dialect (an ssh + * auth failure reads as a denial under Landlock, a refused `kill` under + * Seatbelt). + * @param result - the settled foreground run to classify. + * @param signatures - the active wrap's denial dialect, case-insensitive + * stderr substrings. + * @returns whether the run's failure reads as a sandbox denial. + */ +export function classifyDenial(result: BashRunResult, signatures: readonly string[]): boolean { + return matchesSignature(result.exitCode, result.stderr.text, signatures) +} + +/** + * Runner-failure classifier: a failed run whose stderr carries the SELECTED + * BACKEND's own runner-failure signature (`ConfinedArgv. + * runnerFailureSignatures`: the runner's error prefix, which also matches + * the shell's runner-not-found message) means the SANDBOX itself failed and + * the command never ran. Checked BEFORE {@link classifyDenial} — a runner's + * error text can contain denial words (an unopenable grant root reports + * `Permission denied`) — and surfaced as the fail-closed + * `SANDBOX_UNAVAILABLE` error on the foreground path, `sandbox.runnerFailed` + * on a settled background task. Same conservative-text-inference stance and + * residual imprecision as the denial classifier (a failing task that itself + * prints the runner's prefix reads as a runner failure). + * @param result - the settled foreground run to classify. + * @param signatures - the active wrap's runner-failure signatures, + * case-insensitive stderr substrings. + * @returns whether the run's failure reads as the runner itself failing. + */ +export function classifyRunnerFailure(result: BashRunResult, signatures: readonly string[]): boolean { + return matchesSignature(result.exitCode, result.stderr.text, signatures) +} + +/** + * The classifier core shared by foreground results and settled background + * tasks: failed AND signature present. Lowercases BOTH sides — the seam + * declares its signatures case-insensitive, and producers compose them from + * runtime data of any case (an `argv0` path, `No such file or directory`). + */ +function matchesSignature(exitCode: number | null, stderr: string, signatures: readonly string[]): boolean { + if (exitCode === null || exitCode === 0) return false + const lowered = stderr.toLowerCase() + return signatures.some(signature => lowered.includes(signature.toLowerCase())) +} + +/** + * Sandbox-consuming bash executor. Registers as `ctx.bash` (loading it + * INSTEAD OF `dsh-bash-local`, together with a `ctx.sandbox` provider, is + * the whole swap — the tool layer is untouched). Its configured mode is the + * fallback exposed by {@link sandboxMode}; `dsh-tool-bash` folds a session's + * durable `bash/sandbox-mode` override and stamps the effective mode onto each + * request, while an approved escalation may stamp a strictly wider mode for + * one call. The tool's per-agent prompt section states that same effective + * mode, and each run's `result.sandbox` reports what actually executed plus + * enforcement completeness. + */ +export class SandboxBashExecutor extends LocalBashExecutor { + static inject = ['sandbox'] + + // The sandbox-specific fields intersect the local executor's Config as an + // inline schema call: the config catalog walks `static Config` statically. + static override Config: z = z.intersect([ + LocalBashExecutor.Config, + z.object({ + mode: z.union(['read-only', 'workspace-write', 'danger-full-access'] as const).default('read-only'), + workspaceRoot: z.string(), + }), + ]) + + private readonly mode: SandboxMode + private readonly workspaceRoot: string + /** + * Per-task facts, keyed by task id from `start()` until the settle stamp + * consumes them: the mode the task runs under (per-call — an escalated task + * differs from its neighbors) plus its wrap facts. The seam returns facts + * PER WRAP — a provider may legally vary enforcement or dialect between + * calls — so overlapping background tasks must each classify against their + * OWN wrap; a single latest-wrap field would let a later `start()` clobber + * an earlier task's facts before it settles. A `danger-full-access` task + * has NO entry (nothing confined it), which is what the settle stamp keys + * off. + */ + private readonly taskFacts = new Map() + + constructor(ctx: Context, config: Config) { + super(ctx, config) + // schemastery (static Config) already filled the defaulted fields — the + // cast records that runtime fact (mirrors LocalBashExecutor's config + // cast). `workspaceRoot` and `cwd` have NO schema default, so their + // fallback chain is real branching. + this.mode = config.mode as SandboxMode + this.workspaceRoot = resolve(config.workspaceRoot ?? config.cwd ?? process.cwd()) + } + + /** The configured default mode — the capability fact the tool layer reads. */ + override get sandboxMode(): SandboxMode { + return this.mode + } + + /** + * Stamp the effective mode onto the spec — the request's explicit override + * (an approved escalation), else this executor's configured default — so + * defaulting stays an explicit resolve step and `run()`/`start()` read the + * spec, never the config. + */ + override resolve(request: BashExecRequest): BashExecSpec { + return { ...super.resolve(request), sandboxMode: request.sandboxMode ?? this.mode } + } + + override async run(spec: BashExecSpec): Promise { + // resolve() always stamps the mode; the cast records that invariant + // (mirrors the constructor's config casts). + const mode = spec.sandboxMode as SandboxMode + if (mode === 'danger-full-access') { + const result = await super.run(spec) + return { ...result, sandbox: { mode, denied: false } } + } + const confined = this.confine(spec.command, mode) + const result = await super.run({ ...spec, command: confined.command }) + // Runner failure outranks denial: the sandbox itself failed and the + // command NEVER RAN — surface the same structured fail-closed error a + // confine-time discovery throws (late detection, same outcome), with + // the runner's own first stderr line as the cause. Returning it as a + // task result would let a broken sandbox read as a failing command. + if (classifyRunnerFailure(result, confined.runnerFailureSignatures)) { + throw new SandboxUnavailableError(mode, result.stderr.text.trim().split('\n')[0]) + } + return { ...result, sandbox: { mode, denied: classifyDenial(result, confined.denialSignatures), enforcement: confined.enforcement } } + } + + override start(spec: BashExecSpec): BashTask { + // Same stamped-by-resolve invariant as run(). + const mode = spec.sandboxMode as SandboxMode + if (mode === 'danger-full-access') return super.start(spec) + // Sandbox facts are stamped at settle time by {@link notifyTaskDone} + // (denial classification runs against the settled task's collected + // stderr). The map entry lands synchronously after spawn, strictly + // before the earliest possible settle (a process exit reaches us no + // sooner than the next tick). + const confined = this.confine(spec.command, mode) + const task = super.start({ ...spec, command: confined.command }) + const { enforcement, denialSignatures, runnerFailureSignatures } = confined + this.taskFacts.set(task.id, { mode, enforcement, denialSignatures, runnerFailureSignatures }) + return task + } + + /** + * Stamp the sandbox facts BEFORE completion listeners run: the base + * executor notifies from inside the task's settle path, so overriding the + * notification point is what makes `task.sandbox` visible to `onTaskDone` + * consumers and `done` awaiters alike. Each task classifies against the + * facts of ITS OWN wrap and reports ITS OWN mode (consumed from the + * per-task map here — settle is the entry's end of life): with per-call + * escalation, tasks under different modes settle side by side, so keying + * anything off the configured default would misreport them. A + * `danger-full-access` task has no map entry and carries no facts (nothing + * confined it); a signal-killed task (null exit code) is never a denial, + * mirroring the foreground classifier. + */ + protected override notifyTaskDone(task: BashTask): void { + const facts = this.taskFacts.get(task.id) + if (facts !== undefined) { + this.taskFacts.delete(task.id) + const stderr = this.collectedStderr(task.id) + // Runner failure outranks denial (the command never ran; the runner's + // own error text can contain denial words). A settled task has no + // error channel left, so the fact IS the surface here — the foreground + // path throws instead. + const runnerFailed = matchesSignature(task.exitCode, stderr, facts.runnerFailureSignatures) + task.sandbox = { + mode: facts.mode, + denied: !runnerFailed && matchesSignature(task.exitCode, stderr, facts.denialSignatures), + enforcement: facts.enforcement, + ...(runnerFailed ? { runnerFailed } : {}), + } + } + super.notifyTaskDone(task) + } + + /** + * Wrap one shell command via the `ctx.sandbox` provider: hand over the + * exact `['bash', '-c', command]` argv this executor would spawn, get back + * the confined argv, and re-assemble it into the `exec …` command string + * the inherited spawn path runs (the outer `bash -c` that `runBash` spawns + * `exec`s into the runner, so no extra shell lingers). Provider errors + * (fail-closed `SANDBOX_UNAVAILABLE`) propagate to the caller unchanged. + */ + private confine(command: string, mode: ConfinedSandboxMode): { + command: string + enforcement: SandboxEnforcement + denialSignatures: readonly string[] + runnerFailureSignatures: readonly string[] + } { + const confined = this.ctx.sandbox.confine(['bash', '-c', command], { mode, workspaceRoot: this.workspaceRoot }) + return { + command: `exec ${confined.argv.map(shellQuote).join(' ')}`, + enforcement: confined.enforcement, + denialSignatures: confined.denialSignatures, + runnerFailureSignatures: confined.runnerFailureSignatures, + } + } +} + +export default SandboxBashExecutor diff --git a/packages/bash/bash-sandbox/tests/bwrap.e2e.ts b/packages/bash/bash-sandbox/tests/bwrap.e2e.ts new file mode 100644 index 0000000000..6a748bc389 --- /dev/null +++ b/packages/bash/bash-sandbox/tests/bwrap.e2e.ts @@ -0,0 +1,101 @@ +import { spawnSync } from 'node:child_process' +import { existsSync, readFileSync } from 'node:fs' +import { mkdtemp, rm } from 'node:fs/promises' +import { homedir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { Context } from 'cordis' +import { bwrapProfileArgs, LocalSandboxProvider } from '@deepseek-ai/dsh-sandbox-local' +import { SandboxBashExecutor } from '@deepseek-ai/dsh-bash-sandbox' + +/** + * KEYLESS consumer-integration proof under bwrap: the REAL + * `LocalSandboxProvider` (nothing forced — bwrap is the ladder's first rung, + * so a passing probe selects it) underneath the REAL `SandboxBashExecutor`, + * driven through the executor's public run/start paths. Verifies the WORLD + * (files exist or don't) plus the stamped result facts — in particular that + * bwrap's EROFS denial text classifies as `denied: true` through the + * wrap-carried dialect; the backend-only confinement proofs live with + * `@deepseek-ai/dsh-sandbox-local`. + * + * Self-skips wherever the functional probe fails — no `bwrap` on PATH, or a + * host that denies unprivileged user namespaces. + * + * HOME-based dirs on purpose: bwrap's `/tmp` is an ephemeral mount, so only + * paths outside it prove the workspace-root boundary. + */ + +const probe = spawnSync('bwrap', [...bwrapProfileArgs({ mode: 'read-only', workspaceRoot: '/' }), '--', 'true'], { timeout: 5_000, stdio: 'ignore' }) +const bwrapUsable = probe.status === 0 + +let ctx: Context | undefined +const tempDirs: string[] = [] + +afterEach(async () => { + await ctx?.fiber.dispose() + ctx = undefined + await Promise.all(tempDirs.splice(0).map(dir => rm(dir, { recursive: true, force: true }))) +}) + +async function tempDir(base: string): Promise { + const dir = await mkdtemp(join(base, 'dsh-bwrap-e2e-')) + tempDirs.push(dir) + return dir +} + +async function sandboxedBash(workspace: string, mode: 'read-only' | 'workspace-write'): Promise { + ctx = new Context() + await ctx.plugin(LocalSandboxProvider, {}) + await ctx.plugin(SandboxBashExecutor, { mode, cwd: workspace, workspaceRoot: workspace, timeoutMs: 30_000 }) + return ctx.bash as SandboxBashExecutor +} + +describe.skipIf(!bwrapUsable)('bash-sandbox: real bwrap confinement through ctx.bash', () => { + it('read-only denies a write — the file must NOT exist, and EROFS text classifies as a denial', async () => { + const workdir = await tempDir(homedir()) + const bash = await sandboxedBash(workdir, 'read-only') + const result = await bash.run(bash.resolve({ command: `echo hi > ${workdir}/denied.txt` })) + expect(result.exitCode).not.toBe(0) + expect(result.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' }) + expect(existsSync(join(workdir, 'denied.txt'))).toBe(false) + }) + + it('workspace-write lands a write inside the workspace root and still denies one beside it', async () => { + const workdir = await tempDir(homedir()) + const outside = await tempDir(homedir()) + const bash = await sandboxedBash(workdir, 'workspace-write') + + const inside = await bash.run(bash.resolve({ command: `printf bwrap-ok > ${workdir}/allowed.txt` })) + expect(inside.exitCode).toBe(0) + expect(inside.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: 'full' }) + expect(readFileSync(join(workdir, 'allowed.txt'), 'utf8')).toBe('bwrap-ok') + + const denied = await bash.run(bash.resolve({ command: `echo hi > ${outside}/denied.txt` })) + expect(denied.exitCode).not.toBe(0) + expect(denied.sandbox).toEqual({ mode: 'workspace-write', denied: true, enforcement: 'full' }) + expect(existsSync(join(outside, 'denied.txt'))).toBe(false) + }) + + it('classifies a background denial once the task settles', async () => { + const workdir = await tempDir(homedir()) + const bash = await sandboxedBash(workdir, 'read-only') + const task = bash.start(bash.resolve({ command: `echo hi > ${workdir}/bg-denied.txt` })) + await task.done + expect(task.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' }) + expect(existsSync(join(workdir, 'bg-denied.txt'))).toBe(false) + }) + + it('an approved escalated retry — the spec-level workspace-write override — lands the exact write read-only denied', async () => { + const workdir = await tempDir(homedir()) + const bash = await sandboxedBash(workdir, 'read-only') + const command = `printf escalated > ${workdir}/escalated.txt` + const strict = await bash.run(bash.resolve({ command })) + expect(strict.exitCode).not.toBe(0) + expect(strict.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' }) + expect(existsSync(join(workdir, 'escalated.txt'))).toBe(false) + const retried = await bash.run(bash.resolve({ command, sandboxMode: 'workspace-write' })) + expect(retried.exitCode).toBe(0) + expect(retried.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: 'full' }) + expect(readFileSync(join(workdir, 'escalated.txt'), 'utf8')).toBe('escalated') + }) +}) diff --git a/packages/bash/bash-sandbox/tests/landlock.e2e.ts b/packages/bash/bash-sandbox/tests/landlock.e2e.ts new file mode 100644 index 0000000000..8263dad6fe --- /dev/null +++ b/packages/bash/bash-sandbox/tests/landlock.e2e.ts @@ -0,0 +1,100 @@ +import { spawnSync } from 'node:child_process' +import { existsSync, readFileSync } from 'node:fs' +import { mkdtemp, rm } from 'node:fs/promises' +import { homedir, tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { Context } from 'cordis' +import { launcherPath } from 'node-addon-landlock-run' +import { LocalSandboxProvider } from '@deepseek-ai/dsh-sandbox-local' +import { SandboxBashExecutor } from '@deepseek-ai/dsh-bash-sandbox' + +/** + * KEYLESS consumer-integration proof: the REAL `LocalSandboxProvider` (bwrap + * rung forced off, so the npm-distributed `landlock-run` confines) underneath the + * REAL `SandboxBashExecutor`, driven through the executor's public run/start + * paths. Verifies the WORLD (files exist or don't) plus the stamped result + * facts; the backend-only confinement proofs live with + * `@deepseek-ai/dsh-sandbox-local`. + * + * Self-skips when the running kernel does not enforce Landlock; the + * launcher binary itself arrives with `pnpm install` (`node-addon-landlock-run`). + */ + +const probe = spawnSync(launcherPath(), ['--probe'], { timeout: 5_000, encoding: 'utf8' }) +const landlockUsable = probe.status === 0 +/** The kernel's enforcement level from the probe report — stamped facts below must match it. */ +const enforcement = /partially enforced/.test(probe.stdout ?? '') ? 'partial' : 'full' + +let ctx: Context | undefined +const tempDirs: string[] = [] + +afterEach(async () => { + await ctx?.fiber.dispose() + ctx = undefined + await Promise.all(tempDirs.splice(0).map(dir => rm(dir, { recursive: true, force: true }))) +}) + +async function tempDir(base: string): Promise { + const dir = await mkdtemp(join(base, 'dsh-landlock-e2e-')) + tempDirs.push(dir) + return dir +} + +async function sandboxedBash(workspace: string, mode: 'read-only' | 'workspace-write'): Promise { + ctx = new Context() + await ctx.plugin(LocalSandboxProvider, {}) + ;(ctx.sandbox as LocalSandboxProvider).internals = { probeBwrap: () => false } + await ctx.plugin(SandboxBashExecutor, { mode, cwd: workspace, workspaceRoot: workspace, timeoutMs: 30_000 }) + return ctx.bash as SandboxBashExecutor +} + +describe.skipIf(!landlockUsable)('bash-sandbox: real Landlock confinement through ctx.bash', () => { + it('read-only denies a write — the file must NOT exist, the result carries denial + enforcement facts', async () => { + const workdir = await tempDir(tmpdir()) + const bash = await sandboxedBash(workdir, 'read-only') + const result = await bash.run(bash.resolve({ command: `echo hi > ${workdir}/denied.txt` })) + expect(result.exitCode).not.toBe(0) + expect(result.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement }) + expect(existsSync(join(workdir, 'denied.txt'))).toBe(false) + }) + + it('workspace-write lands a write inside the workspace root and still denies one beside it', async () => { + const workdir = await tempDir(homedir()) + const outside = await tempDir(homedir()) + const bash = await sandboxedBash(workdir, 'workspace-write') + + const inside = await bash.run(bash.resolve({ command: `printf landlock-ok > ${workdir}/allowed.txt` })) + expect(inside.exitCode).toBe(0) + expect(inside.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement }) + expect(readFileSync(join(workdir, 'allowed.txt'), 'utf8')).toBe('landlock-ok') + + const denied = await bash.run(bash.resolve({ command: `echo hi > ${outside}/denied.txt` })) + expect(denied.exitCode).not.toBe(0) + expect(denied.sandbox).toEqual({ mode: 'workspace-write', denied: true, enforcement }) + expect(existsSync(join(outside, 'denied.txt'))).toBe(false) + }) + + it('classifies a background denial once the task settles', async () => { + const workdir = await tempDir(homedir()) + const bash = await sandboxedBash(workdir, 'read-only') + const task = bash.start(bash.resolve({ command: `echo hi > ${workdir}/bg-denied.txt` })) + await task.done + expect(task.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement }) + expect(existsSync(join(workdir, 'bg-denied.txt'))).toBe(false) + }) + + it('an approved escalated retry — the spec-level workspace-write override — lands the exact write read-only denied', async () => { + const workdir = await tempDir(homedir()) + const bash = await sandboxedBash(workdir, 'read-only') + const command = `printf escalated > ${workdir}/escalated.txt` + const strict = await bash.run(bash.resolve({ command })) + expect(strict.exitCode).not.toBe(0) + expect(strict.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: enforcement }) + expect(existsSync(join(workdir, 'escalated.txt'))).toBe(false) + const retried = await bash.run(bash.resolve({ command, sandboxMode: 'workspace-write' })) + expect(retried.exitCode).toBe(0) + expect(retried.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: enforcement }) + expect(readFileSync(join(workdir, 'escalated.txt'), 'utf8')).toBe('escalated') + }) +}) diff --git a/packages/bash/bash-sandbox/tests/sandbox.spec.ts b/packages/bash/bash-sandbox/tests/sandbox.spec.ts new file mode 100644 index 0000000000..4f92ba38f0 --- /dev/null +++ b/packages/bash/bash-sandbox/tests/sandbox.spec.ts @@ -0,0 +1,327 @@ +/** + * SandboxBashExecutor tests: the CONSUMER side of the sandbox seam. A fake + * `ctx.sandbox` provider (injected as a real cordis service) makes wrapping, + * policy hand-off, fail-closed propagation, classification, and fact + * stamping all deterministic without any real runner; the real-provider + * integration proof lives in `tests/landlock.e2e.ts`. Denials are produced + * with plain unix permissions (a 0555 directory), which exercises the same + * stderr signature the classifier keys on. + */ + +import { chmodSync, mkdirSync, mkdtempSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join, resolve } from 'node:path' +import { describe, expect, it, vi } from 'vitest' +import { Context } from 'cordis' +import type { BashRunResult, CollectedOutput } from '@deepseek-ai/dsh-bash' +import { SANDBOX_UNAVAILABLE, SandboxProvider, SandboxUnavailableError } from '@deepseek-ai/dsh-sandbox' +import type { ConfinedArgv, SandboxPolicy } from '@deepseek-ai/dsh-sandbox' +import { classifyDenial, classifyRunnerFailure, SandboxBashExecutor, shellQuote } from '@deepseek-ai/dsh-bash-sandbox' +import type { Config } from '@deepseek-ai/dsh-bash-sandbox' + +const spillDir = mkdtempSync(join(tmpdir(), 'dsh-bash-sandbox-spec-')) + +/** One recorded provider call: the argv handed over and the policy it rode with. */ +interface ConfineCall { + argv: string[] + policy: SandboxPolicy +} + +/** The Linux file-denial dialects the fake wraps carry — matches the unix-permission denials the tests below produce. */ +const UNIX_SIGNATURES = ['read-only file system', 'permission denied'] as const + +/** The runner-failure prefix the fake wraps carry (a fake-runner: error line marks the sandbox itself failing). */ +const RUNNER_FAILURE = ['fake-runner: '] as const + +/** A passthrough wrap: the caller's argv unchanged, asserted full — commands run unconfined, deterministically. */ +const passthrough = (argv: readonly string[]): ConfinedArgv => + ({ argv: [...argv], enforcement: 'full', denialSignatures: UNIX_SIGNATURES, runnerFailureSignatures: RUNNER_FAILURE }) + +/** + * Boot a context with a recording fake `ctx.sandbox` (behavior injectable + * per test) and the executor under test on top of it. + */ +async function setup(config: Config = {}, behavior: (argv: readonly string[], policy: SandboxPolicy) => ConfinedArgv = passthrough) { + const calls: ConfineCall[] = [] + class FakeSandboxProvider extends SandboxProvider { + confine(argv: readonly string[], policy: SandboxPolicy): ConfinedArgv { + calls.push({ argv: [...argv], policy }) + return behavior(argv, policy) + } + } + const ctx = new Context() + await ctx.plugin(FakeSandboxProvider) + await ctx.plugin(SandboxBashExecutor, { graceMs: 200, ...config }) + const bash = ctx.bash as SandboxBashExecutor + bash.internals = { spillDir } + return { ctx, bash, calls } +} + +function output(text: string): CollectedOutput { + return { text, truncated: false } +} + +function runResult(exitCode: number | null, stderr: string): BashRunResult { + return { exitCode, signal: null, timedOut: false, aborted: false, timeoutMs: 1000, stdout: output(''), stderr: output(stderr) } +} + +describe('the provider hand-off', () => { + it('hands the provider the exact bash argv and the per-call policy, and runs the returned argv', async () => { + const { bash, calls } = await setup() + const result = await bash.run(bash.resolve({ command: 'echo \'a b\' "c\'d"' })) + expect(result.stdout.text).toBe('a b c\'d\n') + expect(result.sandbox).toEqual({ mode: 'read-only', denied: false, enforcement: 'full' }) + expect(calls).toEqual([{ + argv: ['bash', '-c', 'echo \'a b\' "c\'d"'], + policy: { mode: 'read-only', workspaceRoot: resolve(process.cwd()) }, + }]) + }) + + it('a wrapped argv from the provider is what actually spawns (prefix survives, quoting round-trips)', async () => { + // The fake wraps with `env MARKER=...` — a real (if tiny) runner prefix: + // the sentinel only prints if the executor spawned the WRAPPED argv. + const { bash } = await setup({}, argv => ({ argv: ['env', 'DSH_WRAP=1', ...argv], enforcement: 'full', denialSignatures: UNIX_SIGNATURES, runnerFailureSignatures: RUNNER_FAILURE })) + const result = await bash.run(bash.resolve({ command: 'printf "%s" "$DSH_WRAP"' })) + expect(result.stdout.text).toBe('1') + expect(result.sandbox).toEqual({ mode: 'read-only', denied: false, enforcement: 'full' }) + }) + + it('workspace-write rides the policy, workspaceRoot falling back to cwd when not configured', async () => { + const { bash, calls } = await setup({ mode: 'workspace-write', cwd: tmpdir() }) + const result = await bash.run(bash.resolve({ command: 'true' })) + expect(result.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: 'full' }) + expect(calls[0]?.policy).toEqual({ mode: 'workspace-write', workspaceRoot: resolve(tmpdir()) }) + }) + + it('an explicit workspaceRoot wins over cwd', async () => { + const { calls, bash } = await setup({ mode: 'workspace-write', workspaceRoot: '/ws', cwd: tmpdir() }) + await bash.run(bash.resolve({ command: 'true' })) + expect(calls[0]?.policy.workspaceRoot).toBe(resolve('/ws')) + }) + + it('the provider is consulted per wrap (no caching in the consumer): run and start each hand off', async () => { + const { bash, calls } = await setup() + await bash.run(bash.resolve({ command: 'true' })) + const task = bash.start(bash.resolve({ command: 'true' })) + await task.done + expect(calls).toHaveLength(2) + }) + + it('shellQuote survives embedded single quotes (the argv re-assembly primitive)', () => { + expect(shellQuote('a\'b')).toBe(String.raw`'a'\''b'`) + }) +}) + +describe('fail closed', () => { + it('propagates the provider\'s structured SANDBOX_UNAVAILABLE on run() and start()', async () => { + const { bash } = await setup({}, () => { throw new SandboxUnavailableError('read-only') }) + const spec = bash.resolve({ command: 'echo hi' }) + await expect(bash.run(spec)).rejects.toMatchObject({ name: 'SandboxUnavailableError', code: SANDBOX_UNAVAILABLE }) + expect(() => bash.start(spec)).toThrow(SandboxUnavailableError) + }) +}) + +describe('danger-full-access', () => { + it('runs unwrapped: the provider is never consulted, facts carry no enforcement', async () => { + const { bash, calls } = await setup({ mode: 'danger-full-access' }) + const result = await bash.run(bash.resolve({ command: 'echo free' })) + expect(result.stdout.text).toBe('free\n') + expect(result.sandbox).toEqual({ mode: 'danger-full-access', denied: false }) + expect(calls).toHaveLength(0) + }) + + it('start() passes through unwrapped and stamps nothing at settle', async () => { + const { bash, calls } = await setup({ mode: 'danger-full-access' }) + const task = bash.start(bash.resolve({ command: 'echo free-bg' })) + await task.done + expect(task.sandbox).toBeUndefined() + expect(bash.readOutput(task.id).delta).toContain('free-bg') + expect(calls).toHaveLength(0) + }) +}) + +describe('per-call sandboxMode override (the escalation mechanism)', () => { + it('exposes the configured default as the capability fact, and resolve() stamps it', async () => { + const { bash } = await setup() + expect(bash.sandboxMode).toBe('read-only') + expect(bash.resolve({ command: 'true' }).sandboxMode).toBe('read-only') + }) + + it('an explicit override outranks the default at resolve(), and the wrap policy follows it', async () => { + const { bash, calls } = await setup() + expect(bash.resolve({ command: 'true', sandboxMode: 'workspace-write' }).sandboxMode).toBe('workspace-write') + await bash.run(bash.resolve({ command: 'true', sandboxMode: 'workspace-write' })) + await bash.run(bash.resolve({ command: 'true' })) + expect(calls.map(call => call.policy.mode)).toEqual(['workspace-write', 'read-only']) + }) + + it('an escalated run reports the mode it ACTUALLY ran under', async () => { + const { bash } = await setup() + const result = await bash.run(bash.resolve({ command: 'true', sandboxMode: 'workspace-write' })) + expect(result.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: 'full' }) + }) + + it('escalating to danger-full-access bypasses the provider entirely — the grant, not a probe, is the authority there', async () => { + const { bash, calls } = await setup() + const result = await bash.run(bash.resolve({ command: 'echo free', sandboxMode: 'danger-full-access' })) + expect(result.stdout.text).toBe('free\n') + expect(result.sandbox).toEqual({ mode: 'danger-full-access', denied: false }) + expect(calls).toHaveLength(0) + }) + + it('overlapping background tasks settle with their OWN modes (an escalated task next to a default one)', async () => { + // With per-call policy, tasks under different modes are in flight at + // once — anything keyed off the configured default would misreport the + // escalated one at its settle stamp. + const { bash } = await setup() + const escalated = bash.start(bash.resolve({ command: 'sleep 0.3; echo "x: Permission denied" >&2; exit 1', sandboxMode: 'workspace-write' })) + const plain = bash.start(bash.resolve({ command: 'true' })) + await plain.done + await escalated.done + expect(escalated.sandbox).toEqual({ mode: 'workspace-write', denied: true, enforcement: 'full' }) + expect(plain.sandbox).toEqual({ mode: 'read-only', denied: false, enforcement: 'full' }) + }) + + it('an escalated danger-full-access background task carries no facts (nothing confined it)', async () => { + const { bash, calls } = await setup() + const task = bash.start(bash.resolve({ command: 'echo bg-free', sandboxMode: 'danger-full-access' })) + await task.done + expect(task.sandbox).toBeUndefined() + expect(bash.readOutput(task.id).delta).toContain('bg-free') + expect(calls).toHaveLength(0) + }) +}) + +describe('classifyDenial', () => { + it('never classifies a clean exit or a signal kill as a denial', () => { + expect(classifyDenial(runResult(0, 'Permission denied'), UNIX_SIGNATURES)).toBe(false) + expect(classifyDenial(runResult(null, 'Permission denied'), UNIX_SIGNATURES)).toBe(false) + }) + + it('classifies failed runs by the wrap\'s own dialect, conservatively', () => { + expect(classifyDenial(runResult(1, 'touch: cannot touch /x: Read-only file system'), UNIX_SIGNATURES)).toBe(true) + expect(classifyDenial(runResult(1, 'sh: /x: Permission denied'), UNIX_SIGNATURES)).toBe(true) + // Bare EPERM is not a Linux runner's dialect: mount/kill/ptrace fail with + // it unsandboxed too, and the mode vocabulary governs file effects only — + // claiming a file denial here would tell the model the sandbox blocked + // something it never governed. + expect(classifyDenial(runResult(1, 'mount: Operation not permitted'), UNIX_SIGNATURES)).toBe(false) + expect(classifyDenial(runResult(1, 'No such file or directory'), UNIX_SIGNATURES)).toBe(false) + }) + + it('matches exactly the active backend\'s dialect: EPERM classifies under Seatbelt, EACCES does not under bwrap', () => { + // The same stderr flips meaning with the backend: under Seatbelt, EPERM + // text IS how the kernel refuses a governed file write; under bwrap's + // EROFS-only dialect, `Permission denied` is ordinary DAC, not the + // sandbox — per-wrap signatures are what keep both classifications honest. + expect(classifyDenial(runResult(1, 'bash: /etc/x: Operation not permitted'), ['operation not permitted'])).toBe(true) + expect(classifyDenial(runResult(1, 'sh: /x: Permission denied'), ['read-only file system'])).toBe(false) + }) +}) + +describe('classifyRunnerFailure', () => { + it('matches the dialect case-insensitively on BOTH sides — the seam declares it so, and producers compose signatures from runtime data (an argv0 path, the shell\'s `No such file or directory`)', () => { + const signatures = ['exec: /Opt/Runners/bwrap: not found', '/Opt/Runners/bwrap: No such file or directory'] + expect(classifyRunnerFailure(runResult(127, 'bash: /Opt/Runners/bwrap: No such file or directory'), signatures)).toBe(true) + expect(classifyRunnerFailure(runResult(127, 'BASH: LINE 1: EXEC: /OPT/RUNNERS/BWRAP: NOT FOUND'), signatures)).toBe(true) + }) +}) + +describe('result facts', () => { + it('reports a real permission failure as a sandbox denial with the mode it ran under', async () => { + const { bash } = await setup() + const lockedDir = join(mkdtempSync(join(tmpdir(), 'dsh-sandbox-denied-')), 'locked') + mkdirSync(lockedDir) + chmodSync(lockedDir, 0o555) + const result = await bash.run(bash.resolve({ command: `echo x > ${lockedDir}/f` })) + expect(result.exitCode).not.toBe(0) + expect(result.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' }) + }) + + it('carries the provider\'s partial-enforcement fact through unchanged', async () => { + const { bash } = await setup({}, argv => ({ argv: [...argv], enforcement: 'partial', denialSignatures: UNIX_SIGNATURES, runnerFailureSignatures: RUNNER_FAILURE })) + const result = await bash.run(bash.resolve({ command: 'true' })) + expect(result.sandbox).toEqual({ mode: 'read-only', denied: false, enforcement: 'partial' }) + }) +}) + +describe('background sandbox facts', () => { + it('stamps a settled denial: nonzero exit + permission stderr under a confined mode', async () => { + const { bash } = await setup() + const task = bash.start(bash.resolve({ command: 'echo "x: Permission denied" >&2; exit 1' })) + await task.done + expect(task.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' }) + }) + + it('a foreground runner failure throws the fail-closed error, never a task result', async () => { + // The wrap's runner prefix on a failed run means the SANDBOX broke and + // the command never ran — the late twin of the confine-time throw, with + // the runner's own first stderr line carried as the cause. + const { bash } = await setup() + const run = bash.run(bash.resolve({ command: 'echo "fake-runner: ruleset rejected" >&2; exit 125' })) + await expect(run).rejects.toThrow(expect.objectContaining({ code: SANDBOX_UNAVAILABLE })) + await expect(run).rejects.toThrow('fake-runner: ruleset rejected') + }) + + it('a foreground runner failure outranks denial: runner error text may contain denial words', async () => { + const { bash } = await setup() + await expect(bash.run(bash.resolve({ command: 'echo "fake-runner: cannot open rule path: /x: Permission denied" >&2; exit 125' }))) + .rejects.toThrow(expect.objectContaining({ code: SANDBOX_UNAVAILABLE })) + }) + + it('a settled background runner failure stamps runnerFailed (no error channel remains), not denied', async () => { + const { bash } = await setup() + const task = bash.start(bash.resolve({ command: 'echo "fake-runner: cannot open rule path: /x: Permission denied" >&2; exit 125' })) + await task.done + expect(task.sandbox).toEqual({ mode: 'read-only', denied: false, enforcement: 'full', runnerFailed: true }) + }) + + it('completion listeners already see the stamped facts (stamp precedes notify)', async () => { + const { ctx, bash } = await setup({}, argv => ({ argv: [...argv], enforcement: 'partial', denialSignatures: UNIX_SIGNATURES, runnerFailureSignatures: RUNNER_FAILURE })) + const seen: unknown[] = [] + ctx.bash.onTaskDone((task) => { seen.push(task.sandbox) }) + const task = bash.start(bash.resolve({ command: 'echo "x: Permission denied" >&2; exit 1' })) + await task.done + expect(seen).toEqual([{ mode: 'read-only', denied: true, enforcement: 'partial' }]) + }) + + it('overlapping background tasks keep their OWN wrap facts (per-task, not latest-wrap)', async () => { + // The seam returns facts PER WRAP — a legal provider may vary them + // between calls. The slow task settles AFTER the quick one started, so a + // latest-wrap field would classify its denial against the quick task's + // dialect (missing it) and stamp the wrong enforcement. + const wraps: Array> = [ + { enforcement: 'partial', denialSignatures: ['permission denied'] }, + { enforcement: 'full', denialSignatures: ['read-only file system'] }, + ] + let call = 0 + const { bash } = await setup({}, (argv) => { + const wrap = wraps[Math.min(call++, wraps.length - 1)] as Pick + return { argv: [...argv], ...wrap, runnerFailureSignatures: RUNNER_FAILURE } + }) + const slow = bash.start(bash.resolve({ command: 'sleep 0.4; echo "x: Permission denied" >&2; exit 1' })) + const quick = bash.start(bash.resolve({ command: 'true' })) + await quick.done + await slow.done + expect(slow.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'partial' }) + expect(quick.sandbox).toEqual({ mode: 'read-only', denied: false, enforcement: 'full' }) + }) + + it('a signal-killed task is never a denial (null exit code)', async () => { + const { bash } = await setup() + const task = bash.start(bash.resolve({ command: 'echo "Permission denied" >&2; sleep 30' })) + // Let the stderr land before the kill so the classifier sees the + // signature and must still refuse it on the null exit code alone. + await vi.waitFor(() => { expect(bash.readOutput(task.id).delta).toContain('Permission denied') }) + bash.kill(task.id) + await task.done + expect(task.sandbox).toEqual({ mode: 'read-only', denied: false, enforcement: 'full' }) + }) + + it('disposal kills wrapped background tasks (inherited HMR safety)', async () => { + const { ctx, bash } = await setup() + const task = bash.start(bash.resolve({ command: 'sleep 30' })) + await ctx.fiber.dispose() + expect(task.status).toBe('killed') + }) +}) diff --git a/packages/bash/bash-sandbox/tests/seatbelt.e2e.ts b/packages/bash/bash-sandbox/tests/seatbelt.e2e.ts new file mode 100644 index 0000000000..8ae25a8d39 --- /dev/null +++ b/packages/bash/bash-sandbox/tests/seatbelt.e2e.ts @@ -0,0 +1,101 @@ +import { spawnSync } from 'node:child_process' +import { existsSync, readFileSync } from 'node:fs' +import { mkdtemp, rm } from 'node:fs/promises' +import { homedir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { Context } from 'cordis' +import { LocalSandboxProvider, seatbeltProfileArgs } from '@deepseek-ai/dsh-sandbox-local' +import { SandboxBashExecutor } from '@deepseek-ai/dsh-bash-sandbox' + +/** + * KEYLESS consumer-integration proof on macOS: the REAL `LocalSandboxProvider` + * (Linux rungs forced off, so `sandbox-exec`/Seatbelt confines) underneath + * the REAL `SandboxBashExecutor`, driven through the executor's public + * run/start paths. Verifies the WORLD (files exist or don't) plus the + * stamped result facts — in particular that Seatbelt's EPERM denial text + * classifies as `denied: true` through the wrap-carried dialect; the + * backend-only confinement proofs live with `@deepseek-ai/dsh-sandbox-local`. + * + * Self-skips wherever the functional probe fails — every non-macOS host, or + * a macOS whose `sandbox-exec` refuses the profile. + */ + +const probe = spawnSync('sandbox-exec', [...seatbeltProfileArgs({ mode: 'read-only', workspaceRoot: '/' }), '--', 'true'], { timeout: 5_000, stdio: 'ignore' }) +const seatbeltUsable = probe.status === 0 + +let ctx: Context | undefined +const tempDirs: string[] = [] + +afterEach(async () => { + await ctx?.fiber.dispose() + ctx = undefined + await Promise.all(tempDirs.splice(0).map(dir => rm(dir, { recursive: true, force: true }))) +}) + +async function tempDir(base: string): Promise { + const dir = await mkdtemp(join(base, 'dsh-seatbelt-e2e-')) + tempDirs.push(dir) + return dir +} + +async function sandboxedBash(workspace: string, mode: 'read-only' | 'workspace-write'): Promise { + ctx = new Context() + await ctx.plugin(LocalSandboxProvider, {}) + ;(ctx.sandbox as LocalSandboxProvider).internals = { probeBwrap: () => false, probeLandlock: () => 'unusable' } + await ctx.plugin(SandboxBashExecutor, { mode, cwd: workspace, workspaceRoot: workspace, timeoutMs: 30_000 }) + return ctx.bash as SandboxBashExecutor +} + +describe.skipIf(!seatbeltUsable)('bash-sandbox: real Seatbelt confinement through ctx.bash', () => { + it('read-only denies a write — the file must NOT exist, and EPERM text classifies as a denial', async () => { + const workdir = await tempDir(homedir()) + const bash = await sandboxedBash(workdir, 'read-only') + const result = await bash.run(bash.resolve({ command: `echo hi > ${workdir}/denied.txt` })) + expect(result.exitCode).not.toBe(0) + expect(result.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' }) + expect(existsSync(join(workdir, 'denied.txt'))).toBe(false) + }) + + it('workspace-write lands a write inside the workspace root and still denies one beside it', async () => { + // HOME-based dirs on purpose: workspace-write grants /tmp and the + // per-user temp dir wholesale, so only paths outside both prove the + // workspace-root boundary. + const workdir = await tempDir(homedir()) + const outside = await tempDir(homedir()) + const bash = await sandboxedBash(workdir, 'workspace-write') + + const inside = await bash.run(bash.resolve({ command: `printf seatbelt-ok > ${workdir}/allowed.txt` })) + expect(inside.exitCode).toBe(0) + expect(inside.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: 'full' }) + expect(readFileSync(join(workdir, 'allowed.txt'), 'utf8')).toBe('seatbelt-ok') + + const denied = await bash.run(bash.resolve({ command: `echo hi > ${outside}/denied.txt` })) + expect(denied.exitCode).not.toBe(0) + expect(denied.sandbox).toEqual({ mode: 'workspace-write', denied: true, enforcement: 'full' }) + expect(existsSync(join(outside, 'denied.txt'))).toBe(false) + }) + + it('classifies a background denial once the task settles', async () => { + const workdir = await tempDir(homedir()) + const bash = await sandboxedBash(workdir, 'read-only') + const task = bash.start(bash.resolve({ command: `echo hi > ${workdir}/bg-denied.txt` })) + await task.done + expect(task.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' }) + expect(existsSync(join(workdir, 'bg-denied.txt'))).toBe(false) + }) + + it('an approved escalated retry — the spec-level workspace-write override — lands the exact write read-only denied', async () => { + const workdir = await tempDir(homedir()) + const bash = await sandboxedBash(workdir, 'read-only') + const command = `printf escalated > ${workdir}/escalated.txt` + const strict = await bash.run(bash.resolve({ command })) + expect(strict.exitCode).not.toBe(0) + expect(strict.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' }) + expect(existsSync(join(workdir, 'escalated.txt'))).toBe(false) + const retried = await bash.run(bash.resolve({ command, sandboxMode: 'workspace-write' })) + expect(retried.exitCode).toBe(0) + expect(retried.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: 'full' }) + expect(readFileSync(join(workdir, 'escalated.txt'), 'utf8')).toBe('escalated') + }) +}) diff --git a/packages/bash/bash-sandbox/tsconfig.json b/packages/bash/bash-sandbox/tsconfig.json new file mode 100644 index 0000000000..6dad98d54f --- /dev/null +++ b/packages/bash/bash-sandbox/tsconfig.json @@ -0,0 +1,36 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "lib/types" + }, + "include": [ + "src" + ], + "references": [ + { + "path": "../../../vendor/cosmokit" + }, + { + "path": "../../../vendor/cordis" + }, + { + "path": "../../../vendor/schemastery" + }, + { + "path": "../../util/brand" + }, + { + "path": "../../llm/llm" + }, + { + "path": "../../sandbox/sandbox" + }, + { + "path": "../../bash/bash" + }, + { + "path": "../../bash/bash-local" + } + ] +} diff --git a/packages/bash/bash/README.md b/packages/bash/bash/README.md index 39318ae371..ac7e00c3a3 100644 --- a/packages/bash/bash/README.md +++ b/packages/bash/bash/README.md @@ -2,15 +2,16 @@ The **bash executor seam**: an abstract `BashExecutor` service (`ctx.bash`) defining WHAT a bash backend does — run commands, manage background tasks — without saying HOW. -This package is one third of the bash capability, split so each concern can evolve (and be swapped) independently: +This package is the interface quarter of the bash capability, split so each concern can evolve (and be swapped) independently: | Package | Role | |---|---| | `@deepseek-ai/dsh-bash` (this) | the interface: abstract service + vocabulary types | | `@deepseek-ai/dsh-bash-local` | an implementation: local subprocesses | +| `@deepseek-ai/dsh-bash-sandbox` | an implementation: `dsh-bash-local`'s mechanics with every spawn confined via [`ctx.sandbox`](../../sandbox/sandbox/), denials reported as result facts | | `@deepseek-ai/dsh-tool-bash` | the model-facing tool schemas over `ctx.bash` | -The split mirrors the LLM seam (`LlmService`/`LlmAdapter`) and the agent-tool survey: pi hides execution behind a `BashOperations` interface (local shell / SSH / VM backends), Codex behind an exec-server protocol. A future sandboxed, containerized, or remote executor implements this interface and the tool schemas don't change. +The split mirrors the LLM seam (`LlmService`/`LlmAdapter`) and the agent-tool survey: pi hides execution behind a `BashOperations` interface (local shell / SSH / VM backends), Codex behind an exec-server protocol. `dsh-bash-sandbox` is exactly that swap in action — a sandboxing executor behind the same interface, tool schemas untouched; a containerized or remote executor slots in the same way. ## Service API (`ctx.bash`) @@ -19,6 +20,7 @@ The split mirrors the LLM seam (`LlmService`/`LlmAdapter`) and the agent-tool su | `run(spec)` | Foreground execution. Resolves when the command finishes. **Rejects only for infrastructure failures** (unusable workdir, missing shell, pre-aborted signal); nonzero exits, timeout kills, and abort kills resolve with a descriptive `BashRunResult`. | | `start(spec)` | Background execution. Returns a `BashTask` handle immediately; **no timeout applies** (stop tasks via `kill`). | | `get(id)` / `list()` | Task lookup. | +| `sandboxMode` | The capability fact for the tool layer: the default mode a SANDBOXING executor confines under (`undefined` in the base class — "this executor does not sandbox"). `dsh-tool-bash` reads it at registration to advertise the escalation fields only when the composition honors them. | | `ownerOf(id)` | The opaque OWNER token recorded for a background task at `start` (from the spec's `owner`), or `undefined` for an unknown id OR a known-but-ownerless task. The executor stores/returns it verbatim and NEVER interprets it — the access POLICY lives in the consumer (`dsh-tool-bash`), which compares `ownerOf(id)` to the caller's token. Storing ownership here (disposed with the executor's fiber) is what makes it survive a consumer HMR reload. | | `readOutput(id)` | **Incremental** output read — consecutive reads never re-deliver. Reads that lost data to buffer bounds flag `lossy` and point at full-stream spill files. Throws for unknown ids. | | `kill(id)` | Kill a running task. Returns `false` when it already finished; throws for unknown ids. | @@ -28,6 +30,8 @@ Implementations subclass `BashExecutor`, implement the abstract methods, and cal ## Vocabulary -`BashExecRequest` (command, workdir?, timeoutMs?, signal?, stdin?, env?, owner?) resolves to `BashExecSpec` (command, workdir, timeoutMs, signal?, stdin?, env?, owner) before execution; `owner` is optional on the request and **required-but-nullable** (`OwnerToken | undefined`) on the resolved spec, so a forgotten owner is a visible `undefined` rather than a silently-absent property. The task id (`BashTaskId`) and the `owner` token (`OwnerToken`) are [branded](../../util/brand) — `OwnerToken` is a DISTINCT brand from `SessionId` (the seam never imports `dsh-session`; the `dsh-tool-bash` consumer is the single boundary that casts its `SessionId` into one). `run()` returns `BashRunResult` (exitCode, signal, timedOut, aborted, timeoutMs, stdout/stderr as `CollectedOutput`) and `start()`/`readOutput()` use `BashTask`/`BashTaskRead` for the background side. See `src/types.ts` for the full contracts. +`BashExecRequest` (command, workdir?, timeoutMs?, signal?, stdin?, env?, owner?, sandboxMode?) resolves to `BashExecSpec` (command, workdir, timeoutMs, signal?, stdin?, env?, owner, sandboxMode) before execution; `owner` and `sandboxMode` are optional on the request and **required-but-nullable** on the resolved spec, so a forgotten one is a visible `undefined` rather than a silently-absent property. `sandboxMode` is the explicit per-call sandbox-policy input: an escalation grant a human just issued ([the sandbox RFC § Escalation](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md), which outranks) or the session's standing override ([the sandbox RFC § Per-session mode switching](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md)); a sandboxing executor's `resolve()` stamps its configured default when the request carries none, and a non-sandboxing executor carries the field verbatim and confines nothing. + +The seam also owns the per-session mode override vocabulary (the sandbox RFC § Per-session mode switching): the log-only `'bash/sandbox-mode'` session event, the pure fold `effectiveSandboxMode(events)` (last event wins; `undefined` means "apply the executor default"), and THE write path `setSandboxMode(session, mode)` — the session log is the store, so an override survives restart by replay and two sessions can never see each other's mode. Writers must respect turn-enclosure: the ACP bridge anchors an idle switch at the next turn rather than appending between turns. The task id (`BashTaskId`) and the `owner` token (`OwnerToken`) are [branded](../../util/brand) — `OwnerToken` is a DISTINCT brand from `SessionId` (the seam never imports `dsh-session`; the `dsh-tool-bash` consumer is the single boundary that casts its `SessionId` into one). `run()` returns `BashRunResult` (exitCode, signal, timedOut, aborted, timeoutMs, stdout/stderr as `CollectedOutput`) and `start()`/`readOutput()` use `BashTask`/`BashTaskRead` for the background side. A sandboxing executor additionally stamps `sandbox` result facts on results and settled tasks (`BashSandboxInfo`: the mode it executed under, the conservative `denied` classification, and — for confined modes — the backend's `enforcement` completeness); the mode/enforcement vocabulary is owned by the [`dsh-sandbox`](../../sandbox/sandbox/) seam, and the facts are documented in [core-data-structures/bash.md](../../../docs/core-data-structures/bash.md). See `src/types.ts` for the full contracts. `stdin` and `env` are set by in-process plugins (the hooks bridges, native plugins) to feed a hook command its JSON payload on stdin and its `CLAUDE_PROJECT_DIR`/`CLAUDE_PLUGIN_ROOT` env. The model-facing `dsh-tool-bash` tool does not expose them as parameters — a model already has equivalent power through shell syntax (`FOO=bar cmd`, a heredoc), so they would be redundant tool params. This is not a security boundary: the implementation's credential scrub (not these fields) is what keeps the harness's ambient secrets out of a spawned command. They are plain optionals on the resolved spec (unlike `owner`'s required-but-nullable): a missing one means "none", the safe default. See [the bash-stdin-env RFC](../../../docs/rfc/implemented/architecture/2026-06-30-bash-stdin-env-trusted-plugin-surface.md). diff --git a/packages/bash/bash/package.json b/packages/bash/bash/package.json index f1bc43c2b4..bfa71d73e3 100644 --- a/packages/bash/bash/package.json +++ b/packages/bash/bash/package.json @@ -23,10 +23,14 @@ "license": "BSD-3-Clause", "peerDependencies": { "@deepseek-ai/dsh-brand": "^0.0.1", + "@deepseek-ai/dsh-sandbox": "^0.0.1", + "@deepseek-ai/dsh-session": "^0.0.1", "cordis": "^4.0.0-rc.6" }, "devDependencies": { "@deepseek-ai/dsh-brand": "workspace:^", + "@deepseek-ai/dsh-sandbox": "workspace:^", + "@deepseek-ai/dsh-session": "workspace:^", "cordis": "^4.0.0-rc.6" } } diff --git a/packages/bash/bash/src/index.ts b/packages/bash/bash/src/index.ts index b629f10e4d..63c5757175 100644 --- a/packages/bash/bash/src/index.ts +++ b/packages/bash/bash/src/index.ts @@ -15,13 +15,16 @@ */ import { Context, Service } from 'cordis' +import type { SandboxMode } from '@deepseek-ai/dsh-sandbox' import type { BashExecRequest, BashExecSpec, BashRunResult, BashTask, BashTaskId, BashTaskListener, BashTaskRead, OwnerToken } from './types.ts' export { BashTaskId, OwnerToken } from './types.ts' +export { SANDBOX_MODES, effectiveSandboxMode, setSandboxMode } from './session-mode.ts' export type { BashExecRequest, BashExecSpec, BashRunResult, + BashSandboxInfo, BashTask, BashTaskListener, BashTaskRead, @@ -70,6 +73,22 @@ export abstract class BashExecutor extends Service { }, 'bash listener teardown') } + /** + * The sandbox mode this executor confines commands under BY DEFAULT, or + * `undefined` when it does not sandbox at all — the capability fact the + * tool and ACP layers read to advertise sandbox controls honestly. The + * getter proves a sandboxing executor is mounted and supplies its fallback + * mode; a session override may make the effective mode narrower or wider, + * so strict escalation widening is checked per call rather than encoded in + * this default-relative capability fact. The base class reports + * `undefined`; a sandboxing implementation overrides the getter. + * @returns the configured default mode of a sandboxing executor; + * `undefined` for an executor that never confines. + */ + get sandboxMode(): SandboxMode | undefined { + return undefined + } + /** * Resolve a caller's {@link BashExecRequest} into a fully-specified * {@link BashExecSpec}, applying this implementation's config defaults and diff --git a/packages/bash/bash/src/session-mode.ts b/packages/bash/bash/src/session-mode.ts new file mode 100644 index 0000000000..03ad6e3d7c --- /dev/null +++ b/packages/bash/bash/src/session-mode.ts @@ -0,0 +1,65 @@ +/** + * Per-session sandbox-mode override: the session log as the store. A runtime + * switch (an ACP `session/set_config_option`, a test scenario) is recorded as + * one `bash/sandbox-mode` event on the session it applies to; + * `effective = fold(events) ?? the executor's configured default`, so an + * override survives restart by replay, two sessions can never see each + * other's state, and there is no external config store. The event is + * log-only (the `approval/*` precedent): the model learns the mode from the + * prompt section and the boundary notices in `@deepseek-ai/dsh-tool-bash`, + * never from the event itself. EXECUTION honors the fold in the tool layer — + * it stamps the effective mode onto each call's `BashExecRequest.sandboxMode` + * (weakest-precedence: an escalation grant for the call outranks it) — the + * executor itself stays a config-fixed default plus per-call overrides. + * + * @module dsh-bash/session-mode + */ + +import type { Session, SessionEvent } from '@deepseek-ai/dsh-session' +import type { SandboxMode } from '@deepseek-ai/dsh-sandbox' + +declare module '@deepseek-ai/dsh-session' { + interface SessionEventMap { + /** + * The session's sandbox mode was switched — log-only (like `approval/*`; + * NOT a surface event, carries no `surfaceOp`): durable and replayable, + * never in the model transcript. The LAST such event is the session's + * override ({@link effectiveSandboxMode}); who asked for it is derivable + * from position (an event after the log's last `request/header*` was a + * runtime switch by the user; see the tool layer's narrator). + */ + 'bash/sandbox-mode': { mode: SandboxMode } + } +} + +/** Every {@link SandboxMode}, for option advertisement and runtime validation of untrusted mode strings. */ +export const SANDBOX_MODES: readonly SandboxMode[] = ['read-only', 'workspace-write', 'danger-full-access'] + +/** + * The session's sandbox-mode override: the last `bash/sandbox-mode` event in + * the log, or undefined when the session never switched (callers apply the + * executor's configured default). The pure fold — resume needs no catch-up + * machinery because replaying the log IS the state. + * @param events - session events in log order (other event types are skipped). + * @returns the mode of the last switch event, or undefined without one. + */ +export function effectiveSandboxMode(events: readonly SessionEvent[]): SandboxMode | undefined { + for (let index = events.length - 1; index >= 0; index -= 1) { + const event = events[index] as SessionEvent + if (event.type === 'bash/sandbox-mode') return event.data.mode + } + return undefined +} + +/** + * THE write path for a session's sandbox-mode override: appends exactly one + * `bash/sandbox-mode` event — the switch IS its event; nothing mutates mode + * state out of band. Takes effect on the session's next bash call and next + * prompt assembly (the consumers fold on every read). + * @param session - the session the override belongs to. + * @param mode - the mode every subsequent bash call in this session runs + * under (until the next switch). + */ +export function setSandboxMode(session: Session, mode: SandboxMode): void { + session.append('bash/sandbox-mode', { mode }) +} diff --git a/packages/bash/bash/src/types.ts b/packages/bash/bash/src/types.ts index 4715ace318..39dbc162c6 100644 --- a/packages/bash/bash/src/types.ts +++ b/packages/bash/bash/src/types.ts @@ -7,6 +7,7 @@ */ import type { Branded } from '@deepseek-ai/dsh-brand' +import type { SandboxEnforcement, SandboxMode } from '@deepseek-ai/dsh-sandbox' /** Identifies one background task within an executor (generated `bash-N`). */ export type BashTaskId = Branded<'BashTaskId'> @@ -40,6 +41,48 @@ export function OwnerToken(id: string): OwnerToken { return id as OwnerToken } +/** + * Sandbox facts for one foreground run — present on {@link BashRunResult} iff + * a sandboxing executor ran the command (an unsandboxed executor reports no + * `sandbox` field at all). Reported independently of `exitCode`/`signal` + * (orthogonal outcomes), so a caller can tell "the command failed on its own" + * from "the sandbox blocked a file operation". The mode/enforcement + * vocabulary lives on the `@deepseek-ai/dsh-sandbox` seam; this shape is the + * bash seam's result-fact carrier for it. + */ +export interface BashSandboxInfo { + /** The mode the command actually ran under. */ + mode: SandboxMode + /** + * True when the executor classifies this run's failure as the sandbox + * denying a file operation. The classification is CONSERVATIVE (a failed + * exit whose stderr carries a filesystem-permission signature) and reads + * the COLLECTED stderr — the bounded in-memory tail per + * {@link CollectedOutput} semantics, so a signature that survives only in a + * spill file is missed toward `denied: false`. A plain command failure + * keeps `denied: false` even under a sandboxed mode. + */ + denied: boolean + /** + * How completely the runner enforced `mode`'s file effects — see + * {@link SandboxEnforcement}. Absent exactly when `mode` is + * `danger-full-access`: nothing is confined, so there is no enforcement to + * report. + */ + enforcement?: SandboxEnforcement + /** + * True when the executor classifies this failure as the SANDBOX RUNNER + * itself failing (missing binary, refused profile, fail-closed refusal + * before exec) — the command NEVER RAN; this is a sandbox failure, not a + * task failure, and it outranks `denied` (a runner's own error text can + * contain denial words). Only ever stamped on settled BACKGROUND tasks: a + * foreground run surfaces the same condition as the thrown + * `SANDBOX_UNAVAILABLE` error instead (the foreground path has an error + * channel; a settled task's facts are its only channel). + */ + runnerFailed?: boolean +} + /** * A caller's execution REQUEST: `workdir` and `timeoutMs` are optional and * filled by {@link BashExecutor.resolve} from the implementation's config. @@ -81,6 +124,20 @@ export interface BashExecRequest { * ownerless background start (a non-agent caller). */ owner?: OwnerToken | undefined + /** + * Explicit per-call sandbox-policy input, overriding the executor's + * configured default mode for THIS call. Never a silent default: a + * consumer sets it only from an explicit policy source — an + * `'allowed-once'` grant a human just issued through `ctx.approval` (the + * escalation flow in the sandbox RFC § Escalation, which outranks), or the + * session's standing override folded from its own `bash/sandbox-mode` + * events (the sandbox RFC § Per-session mode switching — the user's recorded per-session + * choice). A sandboxing executor confines THIS call under the given mode; + * a non-sandboxing executor carries the field and confines nothing (the + * tool layer stamps neither escalation nor overrides without a sandboxing + * executor — see {@link BashExecutor.sandboxMode}). + */ + sandboxMode?: SandboxMode | undefined } /** @@ -122,6 +179,16 @@ export interface BashExecSpec { * task. `start()` stores it; `run()` (foreground) ignores it. */ owner: OwnerToken | undefined + /** + * The sandbox mode this call executes under, REQUIRED-but-nullable for the + * same visibility reason as `owner`. A sandboxing executor's `resolve()` + * stamps the effective mode (the request's explicit override, else its + * configured default) so `run()`/`start()` read the spec, never the config; + * a non-sandboxing executor carries the request value through verbatim and + * ignores it (`undefined` under such an executor means what its README says: + * unconfined execution). + */ + sandboxMode: SandboxMode | undefined } /** One captured stream: the (possibly truncated) text plus recovery info. */ @@ -148,6 +215,12 @@ export interface BashRunResult { timeoutMs: number stdout: CollectedOutput stderr: CollectedOutput + /** + * Sandbox facts, present iff a sandboxing executor ran the command — an + * unsandboxed executor (e.g. `dsh-bash-local`) never sets it. See + * {@link BashSandboxInfo} for the `denied` classification semantics. + */ + sandbox?: BashSandboxInfo } /** Lifecycle of a background task. */ @@ -164,6 +237,16 @@ export interface BashTask { signal: NodeJS.Signals | null /** Resolves when the underlying process closes (never rejects). */ readonly done: Promise + /** + * Sandbox facts for this task's execution, stamped by a sandboxing executor + * once the task settles and BEFORE completion listeners are notified — an + * `onTaskDone` consumer and a `done` awaiter both see it. Denial + * classification runs against the settled task's collected stderr, so the + * field cannot exist earlier: absent while the task is running and under an + * executor that does not sandbox. See {@link BashSandboxInfo} for the + * `denied` semantics. + */ + sandbox?: BashSandboxInfo } /** One incremental {@link BashExecutor.readOutput} read. */ diff --git a/packages/bash/bash/tests/service.spec.ts b/packages/bash/bash/tests/service.spec.ts index 81530843ed..94d299f175 100644 --- a/packages/bash/bash/tests/service.spec.ts +++ b/packages/bash/bash/tests/service.spec.ts @@ -15,6 +15,7 @@ class StubExecutor extends BashExecutor { timeoutMs: request.timeoutMs ?? 1000, ...request.signal ? { signal: request.signal } : {}, owner: request.owner, + sandboxMode: request.sandboxMode, } } @@ -96,6 +97,11 @@ describe('BashExecutor service seam', () => { expect(result.exitCode).toBe(0) }) + it('reports no default sandbox mode (composition truth: the base never confines)', async () => { + const { bash } = await setup() + expect(bash.sandboxMode).toBeUndefined() + }) + it('onTaskDone delivers completions to registered listeners', async () => { const { bash } = await setup() const seen: string[] = [] diff --git a/packages/bash/bash/tsconfig.json b/packages/bash/bash/tsconfig.json index 342f636170..13d297a292 100644 --- a/packages/bash/bash/tsconfig.json +++ b/packages/bash/bash/tsconfig.json @@ -16,6 +16,12 @@ }, { "path": "../../util/brand" + }, + { + "path": "../../sandbox/sandbox" + }, + { + "path": "../../core/session" } ] } diff --git a/packages/bash/tool-bash/README.md b/packages/bash/tool-bash/README.md index eabb9298aa..800de0132c 100644 --- a/packages/bash/tool-bash/README.md +++ b/packages/bash/tool-bash/README.md @@ -4,7 +4,7 @@ The model-facing bash tools — `bash`, `bash_output`, `bash_kill` — registere Requires a loaded executor implementation (e.g. `@deepseek-ai/dsh-bash-local`); the plugin stays pending until `ctx.bash` exists (`inject: ['tools', 'bash', 'systemPrompt']`). -The plugin also contributes the `tool:bash` prompt section (order 105) — the cross-call habit the per-tool descriptions cannot carry: check the `[exit code: N]` marker on every result and investigate failures before moving on. +The plugin also contributes the `tool:bash` prompt section (order 105) — the cross-call habit the per-tool descriptions cannot carry: check the `[exit code: N]` marker on every result and investigate failures before moving on. Under a sandboxing executor it additionally contributes the per-agent `env:bash-sandbox` section (order 110) stating each session's EFFECTIVE mode, and the pre-step narrator — see [Per-session mode](#per-session-mode-switching-and-visibility). ## Tools @@ -17,14 +17,16 @@ The plugin also contributes the `tool:bash` prompt section (order 105) — the c | `timeoutMs` | number | Timeout override in milliseconds. The executor applies its configured default and cap. | | `workdir` | string | Working directory for this call. Defaults to the calling agent's session cwd (`session.header.cwd`) so each session runs in its own workspace; a relative `workdir` is resolved against that session cwd. | | `run_in_background` | boolean | Return a task id immediately; no timeout applies. | +| `sandbox_permissions` | string enum | ADVERTISED ONLY when the mounted executor sandboxes (`ctx.bash.sandboxMode` reports a confining default): the wider mode a denied command needs, from the closed target vocabulary `workspace-write`/`danger-full-access` (never cut down to the executor's default — the effective mode is per-session; strict widening is checked at execution against it, and a non-widening request fails without prompting anyone). | +| `justification` | string | Required together with `sandbox_permissions` (each without the other is a validation error): one sentence for the user explaining why this exact command needs the wider access. | `command`, `workdir`, and `timeoutMs` are resolved against the executor's config defaults via `ctx.bash.resolve()` before execution, so the executor seam (`BashExecSpec`) receives explicit `workdir`/`timeoutMs` values. The workdir default is applied in the tool layer (from the calling agent's `session.header.cwd`) BEFORE `resolve()` — the per-session cwd must come from `exec.agent`, since N sessions share one executor; only when no session cwd is available does the executor fall back to its own config / `process.cwd()`. -Result text: stdout, then a `[stderr]` section, then status markers — `[timed out after Nms]` whenever the executor's timer fired (reported independently of how the process ended, so a command that traps SIGTERM and exits 0 still shows it), `[killed by signal: …]` for a signal death, `[exit code: N]` for a non-zero exit (reported, **not** `isError`: the model decides how to react), and `[output truncated; full output: ]` when the tail was kept and a safe spill file is available. If the executor knows output was dropped but cannot safely advertise a complete spill file, the path is reported as `(unavailable)`. Only infrastructure failures (spawn errors, aborts) surface as `isError` results. +Result text: stdout, then a `[stderr]` section, then status markers — `[sandbox: file access denied under mode]` when a sandboxing executor classified the failure as a policy denial (reported first so `[exit code: N]` stays the last line; the static description tells the model a denial is policy, not a command bug, and forbids retrying around it), `[timed out after Nms]` whenever the executor's timer fired (reported independently of how the process ended, so a command that traps SIGTERM and exits 0 still shows it), `[killed by signal: …]` for a signal death, `[exit code: N]` for a non-zero exit (reported, **not** `isError`: the model decides how to react), and `[output truncated; full output: ]` when the tail was kept and a safe spill file is available. If the executor knows output was dropped but cannot safely advertise a complete spill file, the path is reported as `(unavailable)`. Only infrastructure failures (spawn errors, aborts) surface as `isError` results. ### `bash_output` -`task_id` → output produced **since the previous `bash_output` call** plus a status line (`running` / `completed, exit code: N` / `killed`). Reads that lost data to buffer bounds say so and point at the full-output spill file when one is safely available, otherwise `(unavailable)`. +`task_id` → output produced **since the previous `bash_output` call** plus a status line (`running` / `completed, exit code: N` / `killed`). A settled task classified as a sandbox denial carries the same `[sandbox: file access denied under mode]` marker on every read that sees it (denials are only classifiable once the whole stderr has been collected). Reads that lost data to buffer bounds say so and point at the full-output spill file when one is safely available, otherwise `(unavailable)`. ### `bash_kill` @@ -46,6 +48,12 @@ When a background task finishes, a short notice is injected into the owning agen The `BashExecRequest` seam carries optional `stdin` and `env`, used by the hooks bridges to feed a hook command its JSON payload and `CLAUDE_*` env. This tool does **not** expose them as parameters: its request is built from `command`/`workdir`/`timeoutMs`/`signal`/`owner` only, so a model that includes `env` or `stdin` keys in its tool arguments has them ignored. This is not a trust boundary — a model already has equivalent power through shell syntax (`FOO=bar cmd`, a heredoc), and the real defense against leaking the harness's ambient secrets is `dsh-bash-local`'s credential scrub, which works regardless. A regression guard drives the real tool with those extra args and asserts the resulting request carries neither field — its job is to catch a future refactor that blindly spreads `...args` into the request (which would silently forward model input into the post-scrub `env` merge), not to defend a wall. See [the bash-stdin-env RFC](../../../docs/rfc/implemented/architecture/2026-06-30-bash-stdin-env-trusted-plugin-surface.md). -## Permissions +## Permissions and escalation -`TODO(permissions)`: commands run with the executor's full authority. The permission/sandbox seam is the `tools/pre-execute` waterfall (deny or ask) plus sandboxing `BashExecutor` implementations — see docs/architecture.md. `@cordisjs/plugin-capability` (a named-permission service with a session `test()`) is a candidate building block for that work. +Commands run with the executor's full authority unless a sandboxing executor ([`dsh-bash-sandbox`](../bash-sandbox/)) confines them — the deny-only sandbox reports denials as result facts, rendered here as the denial marker; per-call allow/deny/ask policy is the `tools/pre-execute` waterfall (see docs/architecture.md). + +On top of a denial sits the escalation gate ([the sandbox RFC § Escalation](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md)): an escalating call (`sandbox_permissions` + `justification`) resolves [`ctx.approval`](../../ui/user-approval/README.md) BEFORE anything executes — `allowed-once` stamps the granted mode onto the bash request as the seam-level `sandboxMode` override (that one call runs, classifies, and reports under the wider mode; its neighbors keep the session's effective mode), while `rejected`/`cancelled`/`unavailable` and the no-service / no-agent paths each fail closed with their own error text and execute nothing. The seam is consumed opportunistically (`ctx.get('approval')`, the dsh-tools ask-routing pattern); the grant is consumed by the very call that asked, and nothing is stored. The static description teaches — and a denied result itself prompts, via the escalation-available marker appended exactly when the fields are advertised — the SAME-TURN flow: on a denial a wider mode would cure, retry the exact command once with `sandbox_permissions` (the narrowest mode that suffices) + `justification` immediately, without detouring through chat (the approval prompt IS the user's consent); never speculatively — an escalation is grounded in a real denial (up-front only when the session already denied the same access), a prompt-stated approvals-disabled policy turns the exception off entirely, and a rejected escalation is final for that command. + +## Per-session mode switching + +Under a sandboxing executor this plugin makes the session's standing mode override ([the sandbox RFC § Per-session mode switching](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md); the `bash/sandbox-mode` fold owned by [`dsh-bash`](../bash/README.md)) real at EXECUTION: every call is stamped `escalation grant > session override > undefined` onto `BashExecRequest.sandboxMode`; without either, the executor's `resolve()` applies its configured default. Nothing is stamped under a non-sandboxing executor (nothing would honor it) or for an agent-less caller (no session to fold). The prompt deliberately does NOT state the mode and a switch is not narrated: a standing declaration teaches the model to refuse preemptively, while the denial marker already names the mode the command ran under exactly when the boundary is hit — behavior, not belief, carries the state. diff --git a/packages/bash/tool-bash/package.json b/packages/bash/tool-bash/package.json index d8836d6a21..eec5d79ccb 100644 --- a/packages/bash/tool-bash/package.json +++ b/packages/bash/tool-bash/package.json @@ -23,8 +23,10 @@ "license": "BSD-3-Clause", "peerDependencies": { "@deepseek-ai/dsh-agent": "^0.0.1", + "@deepseek-ai/dsh-user-approval": "^0.0.1", "@deepseek-ai/dsh-bash": "^0.0.1", "@deepseek-ai/dsh-llm": "^0.0.1", + "@deepseek-ai/dsh-sandbox": "^0.0.1", "@deepseek-ai/dsh-system-prompt": "^0.0.1", "@deepseek-ai/dsh-tools": "^0.0.1", "cordis": "^4.0.0-rc.6" @@ -32,9 +34,13 @@ "devDependencies": { "@deepseek-ai/dsh-agent": "workspace:^", "@deepseek-ai/dsh-agent-loop": "workspace:^", + "@deepseek-ai/dsh-user-approval": "workspace:^", "@deepseek-ai/dsh-bash": "workspace:^", "@deepseek-ai/dsh-bash-local": "workspace:^", + "@deepseek-ai/dsh-bash-sandbox": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", + "@deepseek-ai/dsh-sandbox": "workspace:^", + "@deepseek-ai/dsh-sandbox-local": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", "@deepseek-ai/dsh-system-prompt": "workspace:^", "@deepseek-ai/dsh-tools": "workspace:^", diff --git a/packages/bash/tool-bash/src/index.ts b/packages/bash/tool-bash/src/index.ts index d4a3105165..31e6512ae0 100644 --- a/packages/bash/tool-bash/src/index.ts +++ b/packages/bash/tool-bash/src/index.ts @@ -30,10 +30,27 @@ * completion landing during the reload gap still drops its one notice — the * pre-existing reload-gap drop — but the ownership fence itself is HMR-proof.) * - * TODO(permissions): commands run with the executor's full authority. The - * permission/sandbox seam is the `tools/pre-execute` waterfall (deny/ask) plus - * sandboxing `BashExecutor` implementations — see docs/architecture.md - * § Extending The Harness. + * Commands run with the executor's full authority unless a sandboxing + * executor (`@deepseek-ai/dsh-bash-sandbox`) confines them; per-call + * allow/deny/ask policy is the `tools/pre-execute` waterfall — see + * docs/architecture.md § Extension And Composition. Under a sandboxing + * executor this plugin also advertises the ESCALATION surface + * (`sandbox_permissions`/`justification` — the sandbox RFC § Escalation, + * docs/rfc/implemented/feature/2026-07-06-sandbox.md): a command the + * sandbox denied may be retried once under a strictly wider mode, resolved + * through `ctx.approval` BEFORE anything executes and failing closed on every + * unanswerable path. The fields exist only when the mounted executor reports + * a confining default (`ctx.bash.sandboxMode`) — a lever is never advertised + * that the composition cannot honor. + * + * Per-session mode switching (the sandbox RFC § Per-session mode switching): a session may carry a + * standing sandbox-mode override — the `bash/sandbox-mode` event fold from + * `@deepseek-ai/dsh-bash` — which this plugin makes real at EXECUTION: each + * call is stamped `escalation grant > session override > executor default`. + * The prompt deliberately does NOT state the mode and no switch is narrated: + * the model learns the boundary from the denial marker (which names the mode + * it ran under) exactly when it matters, instead of preemptively refusing + * work a standing declaration would discourage. * * @module @deepseek-ai/dsh-tool-bash */ @@ -41,10 +58,16 @@ import type { Context } from 'cordis' import { isAbsolute, resolve as resolvePath } from 'node:path' import { defineTool } from '@deepseek-ai/dsh-tools' -import type { GenericCallView, TerminalCallView, ToolResult, ToolResultView } from '@deepseek-ai/dsh-tools' +import type { GenericCallView, TerminalCallView, ToolExecution, ToolResult, ToolResultView } from '@deepseek-ai/dsh-tools' import type { Agent } from '@deepseek-ai/dsh-agent' +import { assertNever } from '@deepseek-ai/dsh-llm' import type {} from '@deepseek-ai/dsh-system-prompt' -import { BashTaskId, OwnerToken } from '@deepseek-ai/dsh-bash' +// Side-effect type import: declaration-merges `ctx.approval`, consumed +// opportunistically by the escalation gate (`ctx.get('approval')` — the seam +// stays optional at runtime, same pattern as dsh-tools' ask routing). +import type {} from '@deepseek-ai/dsh-user-approval' +import type { SandboxMode } from '@deepseek-ai/dsh-sandbox' +import { BashTaskId, OwnerToken, effectiveSandboxMode } from '@deepseek-ai/dsh-bash' import type { BashRunResult, BashTask, CollectedOutput } from '@deepseek-ai/dsh-bash' export const name = 'tool-bash' @@ -55,16 +78,12 @@ export const inject = ['tools', 'bash', 'systemPrompt'] * validates parsed args against the SchemaSpec before `execute` runs (the * arg-validation RFC), so type/required/enum checks are already done and `args` * is the validated `InferArgs` shape here. What remains are value constraints - * the DSL has no vocabulary for: non-empty strings and a positive, finite - * timeout. + * the DSL has no vocabulary for: non-empty strings, a positive finite timeout, + * and the escalation pairing (`sandbox_permissions` and `justification` travel + * together — an approval prompt without a reason, or a reason driving nothing, + * is a malformed ask). */ -function validateBashArgs(args: { - command: string - description: string - timeoutMs?: number - workdir?: string - run_in_background?: boolean -}): void { +function validateBashArgs(args: BashToolArgs): void { if (args.command.trim().length === 0) { throw new Error('invalid command: expected a non-empty string') } @@ -74,6 +93,15 @@ function validateBashArgs(args: { if (args.timeoutMs !== undefined && (!Number.isFinite(args.timeoutMs) || args.timeoutMs <= 0)) { throw new Error(`invalid timeoutMs: expected a positive number, got ${JSON.stringify(args.timeoutMs)}`) } + if (args.sandbox_permissions !== undefined && args.justification === undefined) { + throw new Error('invalid escalation: sandbox_permissions requires a justification') + } + if (args.justification !== undefined && args.sandbox_permissions === undefined) { + throw new Error('invalid escalation: justification is only valid together with sandbox_permissions') + } + if (args.justification !== undefined && args.justification.trim().length === 0) { + throw new Error('invalid justification: expected a non-empty sentence') + } } /** @@ -88,6 +116,75 @@ function validateTaskId(value: string): BashTaskId { return BashTaskId(value) } +/** + * The bash tool's validated argument shape — the base parameters plus the two + * escalation fields, which are ADVERTISED only when the mounted executor + * reports a confining default mode (absent from the schema otherwise, so the + * SchemaSpec validator rejects them before `execute` ever sees one). + */ +interface BashToolArgs { + command: string + description: string + timeoutMs?: number + workdir?: string + run_in_background?: boolean + sandbox_permissions?: string + justification?: string +} + +/** + * The strictly-wider table: what a call whose effective mode is the key may + * escalate TO. Checked at EXECUTION, never baked into the schema — the + * schema's enum is {@link ESCALATION_TARGETS}, because schemas are + * registry-global while the effective mode is per-call truth. + */ +const WIDER_MODES: Record = { + 'read-only': ['workspace-write', 'danger-full-access'], + 'workspace-write': ['danger-full-access'], +} + +/** + * The closed escalation-target vocabulary — every mode a call could ever + * escalate TO (`read-only` is the floor; nothing escalates to it). Advertised + * whenever the mounted executor confines: cutting the enum down to the modes + * wider than the executor's DEFAULT would strand a session whose effective + * mode sits below it (a `danger-full-access` default would advertise nothing + * while a narrower-switched session stays confined with no lever). + */ +const ESCALATION_TARGETS: readonly SandboxMode[] = ['workspace-write', 'danger-full-access'] + +/** + * The bash tool's static description. The base text is byte-stable regardless + * of composition (it is part of the pinned snapshot header); the escalation + * teaching rides only when the mounted executor actually honors the fields — + * it names the ONE sanctioned exception to the base text's "do not retry + * another way" rule. Its deference clause ("If the session states approval + * prompts are disabled…") points at the approval plugin's never-policy prompt + * sentence by meaning, not by parsed wording — a rendezvous kept working by + * that sentence continuing to open with the approvals-disabled claim. + */ +function bashDescription(escalationModes: readonly SandboxMode[]): string { + const base = 'Execute a bash command (`bash -c`) and return its stdout/stderr. ' + + 'Each call runs in a fresh shell: no state (cwd, variables, functions) persists between calls — ' + + 'pass `workdir` instead of using `cd`. Non-zero exits are reported as `[exit code: N]`. ' + + 'Commands may run under a file sandbox; a blocked file operation is reported as `[sandbox: file access denied under mode]` — a policy denial, not a bug in the command; do not retry another way (a background task reports the same marker via bash_output once it has finished). ' + + 'Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. ' + + 'Set `run_in_background: true` for long-running commands: the call returns a task id immediately; ' + + 'poll it with `bash_output` and stop it with `bash_kill`.' + if (escalationModes.length === 0) return base + return base + ' Attempting a command the sandbox may deny is safe and expected: run it and read the ' + + 'marker rather than assuming the denial. When a command IS denied and a wider mode would let it ' + + 'succeed, escalate immediately in the SAME turn — the ONE sanctioned exception to a denial: retry ' + + 'the exact same command once with `sandbox_permissions` (the narrowest wider mode that suffices) ' + + 'plus a one-sentence `justification`. Do not detour through chat to ask permission first — the ' + + 'approval prompt raised by that retry IS how the user consents. If the session states approval ' + + 'prompts are disabled, there is no exception: a denial is final — do not set `sandbox_permissions`. ' + + 'Never escalate speculatively: ground the request in a real denial — normally the one THIS command ' + + 'just hit; escalating up front is fine only when this session already denied the same access. ' + + 'A rejected escalation is final for THAT command — stop and explain, never work around ' + + 'it — but it does not forbid attempting or escalating other commands later.' +} + /** Append the truncation notice (with the full-output spill path) to a stream's text. */ function streamText(output: CollectedOutput): string { if (!output.truncated) return output.text @@ -100,9 +197,15 @@ function streamText(output: CollectedOutput): string { * errored — the model decides how to react; only infrastructure failures * (spawn errors, aborts) surface as isError results. * @param result - the completed foreground run from the executor. + * @param escalationModes - the escalation targets this composition advertises; + * non-empty adds the same-turn escalation hint after a denial marker + * (default `[]`: no hint). * @returns the model-facing text: output body (or `(no output)`), then any timeout/signal/exit markers, each on its own line. */ -export function renderResult(result: BashRunResult): string { +export function renderResult( + result: BashRunResult, + escalationModes: readonly SandboxMode[] = [], +): string { const out = streamText(result.stdout) const err = streamText(result.stderr) @@ -115,6 +218,19 @@ export function renderResult(result: BashRunResult): string { if (body.length === 0) body = '(no output)' const markers: string[] = [] + // The sandbox marker precedes the exit-status markers so `[exit code: N]` + // stays the LAST line (exitStatus() anchors its parse there). Denial is a + // reported fact like timeout: the model decides how to react. + if (result.sandbox?.denied) { + markers.push(`[sandbox: file access denied under ${result.sandbox.mode} mode]`) + // The same-turn nudge lives at the decision point: only when this + // composition advertises the fields (a lever is never hinted that the + // schema does not offer), and inside the sandbox marker family so the + // exit-code marker stays the last line. + if (escalationModes.length > 0) { + markers.push('[sandbox: escalation available — retry this exact command once with sandbox_permissions (the narrowest wider mode that suffices) + justification; the approval prompt asks the user]') + } + } // Timeout is reported independently of how the process actually ended: a // command can trap SIGTERM and exit 0 after our timer fired (e.g. // `trap "exit 0" TERM; sleep 60`), giving timedOut:true / exitCode:0 / @@ -355,14 +471,87 @@ export function apply(ctx: Context): void { } }) + // The escalation surface exists whenever the mounted executor confines. + // Its enum is the closed target vocabulary, deliberately NOT cut down by + // the configured default: a session may switch to a narrower effective mode + // while sharing this globally registered schema. Strict widening therefore + // belongs to the per-call check below. An executor swap restarts this fiber + // (static inject) and re-registers the schema. + const defaultMode = ctx.bash.sandboxMode + const escalationModes: readonly SandboxMode[] = defaultMode === undefined ? [] : ESCALATION_TARGETS + + /** + * The session's standing mode override for an ordinary (non-escalating) + * call: the `bash/sandbox-mode` fold of the calling agent's log, stamped + * onto the request so EXECUTION follows the same effective mode the prompt + * section states. Weakest precedence — an escalation grant (freshly + * approved for exactly this call) outranks it, and without either the + * executor's `resolve()` applies its configured default. Undefined for a + * non-sandboxing executor (nothing honors it) and for agent-less callers + * (no session to fold). + */ + const sessionOverride = (exec: ToolExecution): SandboxMode | undefined => + defaultMode === undefined || exec.agent === undefined ? undefined : effectiveSandboxMode(exec.agent.session.events) + + /** + * Resolve a sandbox-escalation request through `ctx.approval` BEFORE + * anything executes. Returns the granted mode to stamp onto the bash + * request; throws the distinct fail-closed text for every other path (no + * service composed, an agent-less execution, a rejection, a cancellation, + * an unanswerable ask) — the registry turns the throw into this call's + * isError result, and nothing has run. The seam is consumed + * opportunistically (`ctx.get`, the dsh-tools ask-routing pattern), so a + * deployment without it degrades per call, never at registration. + */ + const approveEscalation = async (mode: string, justification: string, exec: ToolExecution): Promise => { + // Schema validation only checks ADVERTISED keys, so an unadvertised + // `sandbox_permissions` (no sandboxing executor) still reaches execute — reject it here so a + // human is never prompted to "escalate" a sandbox that is not there. When + // the fields ARE advertised, the registry's SchemaSpec enum has already + // pinned `mode` to this ladder for every caller. + if (escalationModes.length === 0) { + throw new Error('sandbox_permissions is not available in this composition (no sandboxing executor to escalate)') + } + // Strict widening is an EXECUTION check against the call's effective + // mode — session override ?? executor default, the same fold ordinary + // calls are stamped with — deliberately not a schema constraint (the + // enum is the closed target vocabulary; the effective mode is per-call + // truth). A non-widening request fails closed here and never prompts a + // human. + const effectiveMode = (sessionOverride(exec) ?? defaultMode) as SandboxMode + if (!(WIDER_MODES[effectiveMode] ?? []).includes(mode as SandboxMode)) { + throw new Error(`sandbox escalation to "${mode}" is not strictly wider than this call's current "${effectiveMode}" mode`) + } + const approval = ctx.get('approval') + if (approval === undefined) { + throw new Error(`sandbox escalation to "${mode}" requires approval, but no approval service is composed`) + } + if (exec.agent === undefined) { + throw new Error(`sandbox escalation to "${mode}" requires approval, but the call has no agent to route it through`) + } + const outcome = await approval.request({ + agent: exec.agent, + toolName: 'bash', + callId: exec.callId, + // Self-contained for the audit trail: approval/asked stores this + // reason, and the target mode is part of the grant's identity. + reason: `escalate sandbox to ${mode}: ${justification}`, + ...exec.signal ? { signal: exec.signal } : {}, + }) + switch (outcome) { + // The SchemaSpec enum already pinned `mode` to the closed target + // vocabulary; the per-call check above proved it is strictly wider. + case 'allowed-once': return mode as SandboxMode + case 'rejected': throw new Error(`the user rejected escalating this command to "${mode}"`) + case 'cancelled': throw new Error(`approval for escalating to "${mode}" was cancelled`) + case 'unavailable': throw new Error(`sandbox escalation to "${mode}" requires approval, but no approval channel is available`) + default: return assertNever(outcome, 'ApprovalOutcome') + } + } + ctx.tools.register(defineTool({ name: 'bash', - description: 'Execute a bash command (`bash -c`) and return its stdout/stderr. ' - + 'Each call runs in a fresh shell: no state (cwd, variables, functions) persists between calls — ' - + 'pass `workdir` instead of using `cd`. Non-zero exits are reported as `[exit code: N]`. ' - + 'Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. ' - + 'Set `run_in_background: true` for long-running commands: the call returns a task id immediately; ' - + 'poll it with `bash_output` and stop it with `bash_kill`.', + description: bashDescription(escalationModes), parameters: { command: { type: 'string', required: true, description: 'The bash command to execute.' }, description: { @@ -375,12 +564,33 @@ export function apply(ctx: Context): void { timeoutMs: { type: 'number', description: 'Timeout in milliseconds. The executor applies its configured default and cap, and kills the command on expiry.' }, workdir: { type: 'string', description: 'Working directory for this command. Defaults to the session workspace; a relative path is resolved against it.' }, run_in_background: { type: 'boolean', description: 'Run in the background and return a task id immediately. No timeout applies.' }, + ...escalationModes.length > 0 ? { + sandbox_permissions: { + type: 'string' as const, + enum: [...escalationModes], + description: 'The wider sandbox mode this command needs. Only valid as a one-shot retry ' + + 'of a command the sandbox just denied; requires justification and user approval.', + }, + justification: { + type: 'string' as const, + description: 'Required with sandbox_permissions: one sentence for the user explaining ' + + 'why this exact command needs the wider access.', + }, + } : {}, }, - async execute(args, exec) { + async execute(args: BashToolArgs, exec) { validateBashArgs(args) // `description` is display/logging metadata only (surfaced to UIs via // the tool/call session event); it is intentionally NOT forwarded to // ctx.bash and has no effect on execution. + // An escalating call resolves approval BEFORE anything executes; every + // non-grant outcome throws its distinct error text and runs nothing. + // (validateBashArgs pinned the pairing, so the double narrow is exact.) + // An ordinary call carries the session's standing override instead — + // grant > session override > executor default (see sessionOverride). + const sandboxMode = args.sandbox_permissions !== undefined && args.justification !== undefined + ? await approveEscalation(args.sandbox_permissions, args.justification, exec) + : sessionOverride(exec) // Default the workdir to the calling agent's session cwd so each ACP // session runs in its own workspace (see resolveWorkdir); an explicit // model workdir still wins. @@ -390,6 +600,7 @@ export function apply(ctx: Context): void { ...workdir !== undefined ? { workdir } : {}, ...args.timeoutMs !== undefined ? { timeoutMs: args.timeoutMs } : {}, ...exec.signal ? { signal: exec.signal } : {}, + ...sandboxMode !== undefined ? { sandboxMode } : {}, } if (args.run_in_background === true) { // Stamp the owner token (the agent's session id) onto the spec so the @@ -401,7 +612,7 @@ export function apply(ctx: Context): void { } const result = await ctx.bash.run(ctx.bash.resolve(request)) if (result.aborted) throw new Error('command aborted') - return [{ type: 'text', text: renderResult(result) }] + return [{ type: 'text', text: renderResult(result, escalationModes) }] }, presentCall: presentBashCall, presentResult: presentBashResult, @@ -428,6 +639,21 @@ export function apply(ctx: Context): void { text += `\n[some output was dropped from memory; full output: ${fullOutput}]` } text += `\n${statusLine(read.task)}` + if (read.task.sandbox?.runnerFailed) { + // The sandbox RUNNER itself failed — the command never ran. The + // foreground path surfaces this as the structured SANDBOX_UNAVAILABLE + // error; a settled task's read carries the marker instead. + text += `\n[sandbox: the sandbox runner itself failed under ${read.task.sandbox.mode} mode — the command did not run; this is a sandbox problem, not a command failure]` + } else if (read.task.sandbox?.denied) { + // Mirrors the foreground result marker (and its same-turn escalation + // hint). Background denials are only classifiable once the task + // settles (the classifier needs the whole stderr), so the marker + // rides every read that sees the settled task. + text += `\n[sandbox: file access denied under ${read.task.sandbox.mode} mode]` + if (escalationModes.length > 0) { + text += '\n[sandbox: escalation available — retry this exact command once with sandbox_permissions (the narrowest wider mode that suffices) + justification; the approval prompt asks the user]' + } + } return Promise.resolve([{ type: 'text', text }]) }, presentCall: args => presentTaskCall('Read output from', args), diff --git a/packages/bash/tool-bash/tests/integration.spec.ts b/packages/bash/tool-bash/tests/integration.spec.ts index b3d6bb3f77..a22e86bd99 100644 --- a/packages/bash/tool-bash/tests/integration.spec.ts +++ b/packages/bash/tool-bash/tests/integration.spec.ts @@ -119,37 +119,30 @@ describe('bash tool through the agent loop', () => { it('background: start → poll → completion notice lands as context/message', async () => { const adapter = new MockAdapter([ toolCallResponse('call-1', 'bash', { command: 'echo bg-ok', description: 'test command', run_in_background: true }), - toolCallResponse('call-2', 'bash_output', {}, undefined), + // Each harness owns a fresh BashLocal service, whose first task id is + // deterministically bash-1. Keep the scripted call faithful to what the + // model sent; tool arguments are immutable once execution policy begins. + toolCallResponse('call-2', 'bash_output', { task_id: 'bash-1' }, undefined), textResponse('Background task finished.'), ]) - // The second tool call needs the REAL task id from the first result; - // a tools/pre-execute listener rewrites the scripted arguments. (This uses - // the low-level capability to mutate `exec` before dispatch — the - // unadvertised mechanism behind a future first-class input-rewrite decision; - // here it is a test shim to thread the generated id, not a product feature.) let taskId = '' const ctx = await harness(adapter) const agent = ctx.agentLoop.create(AgentId('it-bg'), { model: 'mock' }) - // Intercept the first tool result to capture the generated task id, then - // rewrite the second scripted call's arguments to use it. + // Capture the generated id so the deterministic fixture is checked against + // the real executor instead of silently assuming it. ctx.on('session/event', (_session, event) => { if (event.type === 'tool/result' && taskId === '') { const match = /task (bash-\d+)/.exec(resultText(event)) if (match) taskId = match[1]! } }) - ctx.on('tools/pre-execute', async (exec, next) => { - if (exec.name === 'bash_output') { - exec.arguments = { task_id: taskId } - } - return next() - }) - agent.send([{ type: 'text', text: 'run echo bg-ok in the background' }]) await waitForIdle(ctx, agent) + expect(taskId).toBe('bash-1') + // Wait for the background task itself (completion may race turn end). const task = ctx.bash.get(BashTaskId(taskId)) if (!task) throw new Error(`task ${taskId} not registered`) diff --git a/packages/bash/tool-bash/tests/tools.spec.ts b/packages/bash/tool-bash/tests/tools.spec.ts index 7d4b34f74f..38576b6f28 100644 --- a/packages/bash/tool-bash/tests/tools.spec.ts +++ b/packages/bash/tool-bash/tests/tools.spec.ts @@ -1,21 +1,40 @@ -import { mkdtempSync } from 'node:fs' +import { chmodSync, mkdirSync, mkdtempSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { describe, expect, it, vi } from 'vitest' import { Context } from 'cordis' import { CallId } from '@deepseek-ai/dsh-llm' -import { BashExecutor, BashTaskId } from '@deepseek-ai/dsh-bash' +import { BashExecutor, BashTaskId, setSandboxMode } from '@deepseek-ai/dsh-bash' import type { BashExecRequest, BashExecSpec, BashRunResult, BashTask, BashTaskRead, OwnerToken } from '@deepseek-ai/dsh-bash' +import { Session, SessionId } from '@deepseek-ai/dsh-session' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRegistry from '@deepseek-ai/dsh-tools' import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local' +import { SandboxBashExecutor } from '@deepseek-ai/dsh-bash-sandbox' +import { SandboxProvider } from '@deepseek-ai/dsh-sandbox' +import type { ConfinedArgv } from '@deepseek-ai/dsh-sandbox' +import { LocalSandboxProvider } from '@deepseek-ai/dsh-sandbox-local' +import ApprovalService from '@deepseek-ai/dsh-user-approval' +import type { ApprovalOutcome } from '@deepseek-ai/dsh-user-approval' import * as ToolBash from '@deepseek-ai/dsh-tool-bash' import { renderResult } from '@deepseek-ai/dsh-tool-bash' const spillDir = mkdtempSync(join(tmpdir(), 'dsh-tool-bash-spec-')) +// Pure-config passthrough runner (same knob the snapshot tier uses): skips the +// profile args up to `--` and execs the command unconfined — deterministic +// without a host bwrap. +const PASSTHROUGH_RUNNER = ['bash', '-c', 'while [ "$1" != "--" ]; do shift; done; shift; exec "$@"', 'passthrough-runner'] +const PASSTHROUGH_RUNNER_CONFIG = { + runnerCommand: PASSTHROUGH_RUNNER, + // The script has no pre-exec failure path; the provider still requires an + // explicit dialect so a future script change cannot silently turn runner + // failure into an ordinary command result. + runnerFailureSignatures: ['passthrough-runner: profile rejected'], +} + async function setup() { const ctx = new Context() await ctx.plugin(SystemPrompt) @@ -35,7 +54,7 @@ async function setup() { * The registration disposer is tracked so {@link unregisterFakeAgents} can drop * it (simulating the owning session disconnecting before a task completes). */ -const fakeAgentDisposers = new Map void)[]>() +const fakeAgentDisposers = new Map Promise | void)[]>() function registerFakeAgent(ctx: Context, sessionId: string, inject: (...args: unknown[]) => void): Agent { // The registry KEY (agent.id) is deliberately DIFFERENT from the session // token (session.header.id) — a config agent has `agentId !== sessionId`. The @@ -53,7 +72,7 @@ function registerFakeAgent(ctx: Context, sessionId: string, inject: (...args: un /** Unregister every fake agent in this ctx (simulate the owning session disconnecting). */ function unregisterFakeAgents(ctx: Context): void { - for (const dispose of fakeAgentDisposers.get(ctx) ?? []) dispose() + for (const dispose of fakeAgentDisposers.get(ctx) ?? []) void dispose() fakeAgentDisposers.delete(ctx) } @@ -100,6 +119,7 @@ class LossyReadBashExecutor extends BashExecutor { timeoutMs: request.timeoutMs ?? 0, ...request.signal ? { signal: request.signal } : {}, owner: request.owner, + sandboxMode: request.sandboxMode, } } @@ -242,7 +262,6 @@ describe('bash tool', () => { [{ command: ' ', description: 'd' }, /invalid command/], [{ command: 'x', description: ' ' }, /invalid description/], [{ command: 'x', description: 'd', timeoutMs: -1 }, /invalid timeoutMs/], - [{ command: 'x', description: 'd', timeoutMs: Number.NaN }, /invalid timeoutMs/], ])('rejects value-invalid args %j', async (args, pattern) => { const ctx = await setup() const result = await call(ctx, 'bash', args) @@ -250,6 +269,15 @@ describe('bash tool', () => { expect(text(result)).toMatch(pattern) }) + it('rejects a non-JSON numeric argument before tool-specific validation', async () => { + const ctx = await setup() + const result = await call(ctx, 'bash', { + command: 'x', description: 'd', timeoutMs: Number.NaN, + }) + expect(result.isError).toBe(true) + expect(text(result)).toContain('tool execution arguments must be losslessly JSON-serializable') + }) + it('registers all three schemas in the system prompt assembly', async () => { const ctx = await setup() const names = ctx.tools.schemas().map(schema => schema.name) @@ -894,6 +922,7 @@ describe('the model-facing bash tool builds its request from named args only (no ...request.stdin !== undefined ? { stdin: request.stdin } : {}, ...request.env !== undefined ? { env: request.env } : {}, owner: request.owner, + sandboxMode: request.sandboxMode, } } run(): Promise { @@ -970,3 +999,512 @@ describe('the model-facing bash tool builds its request from named args only (no expect('owner' in request).toBe(true) }) }) + +describe('sandbox rendering', () => { + const sandboxResult = (denied: boolean, exitCode: number): BashRunResult => ({ + exitCode, + signal: null, + timedOut: false, + aborted: false, + timeoutMs: 1000, + stdout: { text: '', truncated: false }, + stderr: { text: denied ? 'bash: /x: Read-only file system' : 'boom', truncated: false }, + sandbox: { mode: 'read-only', denied }, + }) + + it('renders a denial marker BEFORE the exit-code marker (the $-anchored parse survives)', () => { + const text = renderResult(sandboxResult(true, 1)) + expect(text).toMatch(/\[sandbox: file access denied under read-only mode\]\n\[exit code: 1\]$/) + }) + + it('appends the same-turn escalation hint to a denial exactly when the fields are advertised', () => { + const hinted = renderResult(sandboxResult(true, 1), ['workspace-write', 'danger-full-access']) + expect(hinted).toMatch( + /denied under read-only mode\]\n\[sandbox: escalation available — retry this exact command once with sandbox_permissions [^\n]+\]\n\[exit code: 1\]$/, // eslint-disable-line @stylistic/max-len -- the hint sentence is pinned verbatim + ) + // Default (no advertisement): no hint — a lever the schema does not offer is never suggested. + expect(renderResult(sandboxResult(true, 1))).not.toContain('escalation available') + // A non-denied result never hints, advertised or not. + expect(renderResult(sandboxResult(false, 2), ['danger-full-access'])).not.toContain('escalation available') + }) + + it('renders no sandbox marker for a plain failure under a sandboxed mode', () => { + expect(renderResult(sandboxResult(false, 2))).not.toContain('[sandbox:') + }) + + it('bash_output reports a settled background denial with the same marker', async () => { + const ctx = new Context() + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(AgentRegistry) + await ctx.plugin(LocalSandboxProvider, PASSTHROUGH_RUNNER_CONFIG) + await ctx.plugin(SandboxBashExecutor, { graceMs: 200 }) + const bash = ctx.bash as SandboxBashExecutor + bash.internals = { spillDir } + await ctx.plugin(ToolBash) + const started = await call(ctx, 'bash', { command: 'echo "x: Permission denied" >&2; exit 1', description: 'test command', run_in_background: true }) + const id = text(started).match(/started background task (bash-\d+)/)![1] + await bash.list().find(task => task.id === id)!.done + const read = await call(ctx, 'bash_output', { task_id: id }) + expect(text(read)).toMatch( + /\[status: completed, exit code: 1\]\n\[sandbox: file access denied under read-only mode\]\n\[sandbox: escalation available[^\n]+\]$/, + ) + }) + + it('a settled background denial renders no escalation hint without a confining executor (defensive arm)', async () => { + // Structurally near-unreachable through the real stack — every confining + // default advertises the static target set — but the read path guards + // it anyway: an executor that reports no sandboxMode (fields never + // advertised) whose task nonetheless carries denial facts must render + // the marker without suggesting a lever the schema does not offer. + class FactsOnlyExecutor extends BashExecutor { + private readonly task: BashTask = { + id: BashTaskId('bash-facts'), + command: 'fake', + status: 'completed', + exitCode: 1, + signal: null, + done: Promise.resolve(), + sandbox: { mode: 'read-only', denied: true }, + } + + resolve(request: BashExecRequest): BashExecSpec { + return { + command: request.command, + workdir: request.workdir ?? process.cwd(), + timeoutMs: request.timeoutMs ?? 0, + ...request.signal ? { signal: request.signal } : {}, + owner: request.owner, + sandboxMode: request.sandboxMode, + } + } + + run(): Promise { return Promise.reject(new Error('not used')) } + start(): BashTask { return this.task } + get(id: string): BashTask | undefined { return id === this.task.id ? this.task : undefined } + list(): BashTask[] { return [this.task] } + kill(): boolean { return false } + ownerOf(): OwnerToken | undefined { return undefined } + readOutput(): BashTaskRead { + return { task: this.task, delta: '', lossy: false } + } + } + const ctx = new Context() + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(AgentRegistry) + await ctx.plugin(FactsOnlyExecutor) + await ctx.plugin(ToolBash) + const read = await call(ctx, 'bash_output', { task_id: 'bash-facts' }) + expect(text(read)).toMatch(/\[sandbox: file access denied under read-only mode\]$/) + expect(text(read)).not.toContain('escalation available') + }) + + it('bash_output reports a settled background RUNNER failure as a sandbox problem, outranking the denial marker', async () => { + // A provider whose wrap carries a runner-failure signature: the settled + // task's stderr matching it means the sandbox itself broke and the + // command never ran — even though the same stderr also carries denial + // words (a runner's error text may contain them). + class FakeProvider extends SandboxProvider { + confine(argv: readonly string[]): ConfinedArgv { + return { argv: [...argv], enforcement: 'full', denialSignatures: ['permission denied'], runnerFailureSignatures: ['fake-runner: '] } + } + } + const ctx = new Context() + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(AgentRegistry) + await ctx.plugin(FakeProvider) + await ctx.plugin(SandboxBashExecutor, { graceMs: 200 }) + const bash = ctx.bash as SandboxBashExecutor + bash.internals = { spillDir } + await ctx.plugin(ToolBash) + const started = await call(ctx, 'bash', { command: 'echo "fake-runner: cannot open rule path: /x: Permission denied" >&2; exit 125', description: 'test command', run_in_background: true }) + const id = text(started).match(/started background task (bash-\d+)/)![1] + await bash.list().find(task => task.id === id)!.done + const read = await call(ctx, 'bash_output', { task_id: id }) + expect(text(read)).toMatch(/\[sandbox: the sandbox runner itself failed under read-only mode — the command did not run; /) + expect(text(read)).toMatch(/this is a sandbox problem, not a command failure\]$/) + expect(text(read)).not.toContain('file access denied') + }) + + it('classifies an executable configured runner that refuses its profile before the command runs', async () => { + const signature = 'custom-runner-rejected' + const ctx = new Context() + await ctx.plugin(LocalSandboxProvider, { + runnerCommand: ['bash', '-c', `printf '${signature}\\n' >&2; exit 125`, 'custom-runner'], + runnerFailureSignatures: [signature], + }) + await ctx.plugin(SandboxBashExecutor, { graceMs: 200 }) + const bash = ctx.bash as SandboxBashExecutor + bash.internals = { spillDir } + + await expect(bash.run(bash.resolve({ command: 'echo command-must-not-run' }))) + .rejects.toMatchObject({ code: 'SANDBOX_UNAVAILABLE' }) + + const task = bash.start(bash.resolve({ command: 'echo command-must-not-run' })) + await task.done + expect(task.sandbox).toEqual({ mode: 'read-only', denied: false, enforcement: 'full', runnerFailed: true }) + }) + + it('reports a real denial end-to-end through the shipping sandbox executor', async () => { + const ctx = new Context() + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(AgentRegistry) + await ctx.plugin(LocalSandboxProvider, PASSTHROUGH_RUNNER_CONFIG) + await ctx.plugin(SandboxBashExecutor, { graceMs: 200 }) + const bash = ctx.bash as SandboxBashExecutor + bash.internals = { spillDir } + await ctx.plugin(ToolBash) + const lockedDir = join(mkdtempSync(join(tmpdir(), 'dsh-tool-bash-denied-')), 'locked') + mkdirSync(lockedDir) + chmodSync(lockedDir, 0o555) + const result = await call(ctx, 'bash', { command: `echo x > ${lockedDir}/f`, description: 'Write into a locked directory' }) + expect(result.isError).toBe(false) + expect(text(result)).toMatch( + /denied under read-only mode\]\n\[sandbox: escalation available[^\n]+\]\n\[exit code: \d+\]$/, + ) + }) +}) + +describe('sandbox escalation (sandbox_permissions / justification)', () => { + /** Compose the real sandbox stack (passthrough runner) at a given default mode. */ + async function setupSandboxed(mode?: 'read-only' | 'workspace-write' | 'danger-full-access', opts: { approval?: boolean; policy?: 'ask' | 'never' } = {}) { + const ctx = new Context() + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(AgentRegistry) + await ctx.plugin(LocalSandboxProvider, PASSTHROUGH_RUNNER_CONFIG) + await ctx.plugin(SandboxBashExecutor, { graceMs: 200, ...mode !== undefined ? { mode } : {} }) + const bash = ctx.bash as SandboxBashExecutor + bash.internals = { spillDir } + if (opts.approval === true) await ctx.plugin(ApprovalService, opts.policy !== undefined ? { policy: opts.policy } : {}) + await ctx.plugin(ToolBash) + return { ctx, bash } + } + + /** The registered bash tool's wire schema (what the model actually sees). */ + function bashSchema(ctx: Context) { + const schema = ctx.tools.schemas().find(s => s.name === 'bash') + if (!schema) throw new Error('bash tool not registered') + return schema as unknown as { description: string; parameters: { properties: Record } } + } + + /** + * A fake agent whose session records appends — the approval audit surface. + * Seeded mid-turn: an escalating call always runs inside one, and request() + * enforces the enclosure. + */ + function escalationAgent(events: Array<{ type: string; data: Record }>): Agent { + return { + id: 'agent-esc', + session: { + header: { version: 0, id: 'sess-esc', createdAt: 0 }, + events: [{ type: 'turn/start' }], + append: (type: string, data: Record) => { events.push({ type, data }) }, + }, + } as unknown as Agent + } + + let escCall = 0 + function callAs(ctx: Context, agent: Agent | undefined, args: unknown) { + return ctx.tools.execute({ callId: CallId(`call-esc-${++escCall}`), name: 'bash', arguments: args, ...agent ? { agent } : {} }) + } + + const ESCALATE = { command: 'true', description: 'test escalation', sandbox_permissions: 'workspace-write', justification: 'the test needs it' } + + it('advertises no escalation surface under a non-sandboxing executor', async () => { + const ctx = await setup() + expect(ctx.bash.sandboxMode).toBeUndefined() + const schema = bashSchema(ctx) + expect(schema.parameters.properties['sandbox_permissions']).toBeUndefined() + expect(schema.parameters.properties['justification']).toBeUndefined() + expect(schema.description).not.toContain('sanctioned exception') + }) + + it('advertises the full closed target vocabulary under any confining default', async () => { + // The enum is deliberately NOT default-relative: a session's effective + // mode is per-session and switchable, so every confining composition + // advertises every possible target — strict widening is checked at + // execution against the call's effective mode instead. + for (const mode of [undefined, 'workspace-write', 'danger-full-access'] as const) { + const { ctx } = await setupSandboxed(mode) + const schema = bashSchema(ctx) + expect(schema.parameters.properties['sandbox_permissions']?.enum).toEqual(['workspace-write', 'danger-full-access']) + expect(schema.parameters.properties['justification']).toBeDefined() + expect(schema.description).toContain('sanctioned exception') + } + }) + + it('a non-widening request fails at execution with its own text and prompts no one', async () => { + const { ctx } = await setupSandboxed('danger-full-access', { approval: true }) + const consulted = vi.fn() + ctx.on('approval/request', (_req, next) => { consulted(); return next() }) + const result = await callAs(ctx, escalationAgent([]), { command: 'true', description: 'd', sandbox_permissions: 'workspace-write', justification: 'already wider' }) + expect(result.isError).toBe(true) + expect(text(result)).toContain('not strictly wider than this call\'s current "danger-full-access" mode') + expect(consulted).not.toHaveBeenCalled() + }) + + it('rejects sandbox_permissions without a justification, and vice versa, and a blank justification', async () => { + const { ctx } = await setupSandboxed() + const missing = await callAs(ctx, undefined, { command: 'true', description: 'd', sandbox_permissions: 'workspace-write' }) + expect(missing.isError).toBe(true) + expect(text(missing)).toContain('sandbox_permissions requires a justification') + const orphan = await callAs(ctx, undefined, { command: 'true', description: 'd', justification: 'why not' }) + expect(orphan.isError).toBe(true) + expect(text(orphan)).toContain('only valid together with sandbox_permissions') + const blank = await callAs(ctx, undefined, { command: 'true', description: 'd', sandbox_permissions: 'workspace-write', justification: ' ' }) + expect(blank.isError).toBe(true) + expect(text(blank)).toContain('expected a non-empty sentence') + }) + + it('the schema enum rejects a mode outside the target vocabulary before execute (registry-level, any caller)', async () => { + const { ctx } = await setupSandboxed() + const result = await callAs(ctx, undefined, { command: 'true', description: 'd', sandbox_permissions: 'read-only', justification: 'narrow' }) + expect(result.isError).toBe(true) + expect(text(result)).toContain('must be one of') + }) + + it('rejects an unadvertised sandbox_permissions injection under a non-sandboxing executor', async () => { + const ctx = await setup() + const result = await callAs(ctx, undefined, { command: 'true', description: 'd', sandbox_permissions: 'workspace-write', justification: 'sneaky' }) + expect(result.isError).toBe(true) + expect(text(result)).toContain('not available in this composition') + }) + + it('fails closed with its own text when no approval service is composed', async () => { + const { ctx } = await setupSandboxed() + const result = await callAs(ctx, escalationAgent([]), ESCALATE) + expect(result.isError).toBe(true) + expect(text(result)).toContain('no approval service is composed') + }) + + it('fails closed with its own text for an agent-less escalating call', async () => { + const { ctx } = await setupSandboxed('read-only', { approval: true }) + const result = await callAs(ctx, undefined, ESCALATE) + expect(result.isError).toBe(true) + expect(text(result)).toContain('no agent to route it through') + }) + + it('fails closed with its own text when the service has no answerer', async () => { + const { ctx } = await setupSandboxed('read-only', { approval: true }) + const result = await callAs(ctx, escalationAgent([]), ESCALATE) + expect(result.isError).toBe(true) + expect(text(result)).toContain('no approval channel is available') + }) + + it('a grant runs THAT call under the wider mode — the denial marker names it — and lands the audit pair', async () => { + const { ctx } = await setupSandboxed('read-only', { approval: true }) + ctx.on('approval/request', () => Promise.resolve('allowed-once')) + const events: Array<{ type: string; data: Record }> = [] + // A real unix denial under the passthrough runner: the marker's mode can + // only say workspace-write if the override actually rode the spec. + const lockedDir = join(mkdtempSync(join(tmpdir(), 'dsh-esc-denied-')), 'locked') + mkdirSync(lockedDir) + chmodSync(lockedDir, 0o555) + const result = await callAs(ctx, escalationAgent(events), { + command: `echo x > ${lockedDir}/f`, + description: 'write into a locked directory', + sandbox_permissions: 'workspace-write', + justification: 'must write outside the workspace', + }) + expect(result.isError).toBe(false) + expect(text(result)).toMatch(/\[sandbox: file access denied under workspace-write mode\]/) + expect(events.map(e => e.type)).toEqual(['approval/asked', 'approval/decided']) + expect(events[0]?.data['toolName']).toBe('bash') + expect(events[0]?.data['reason']).toBe('escalate sandbox to workspace-write: must write outside the workspace') + expect(events[1]?.data['outcome']).toBe('allowed-once') + }) + + it('a granted background start settles with the wider mode\'s facts', async () => { + const { ctx, bash } = await setupSandboxed('read-only', { approval: true }) + ctx.on('approval/request', () => Promise.resolve('allowed-once')) + const started = await callAs(ctx, escalationAgent([]), { ...ESCALATE, run_in_background: true }) + expect(started.isError).toBe(false) + const id = text(started).match(/started background task (bash-\d+)/)?.[1] + const task = bash.list().find(t => t.id === id) + if (!task) throw new Error('escalated task not tracked') + await task.done + expect(task.sandbox).toMatchObject({ mode: 'workspace-write', denied: false }) + }) + + it('a rejection denies with the user-said-no text and runs nothing', async () => { + const { ctx } = await setupSandboxed('read-only', { approval: true }) + ctx.on('approval/request', () => Promise.resolve('rejected')) + // A live (non-aborted) signal rides the execution: the gate threads it + // into the approval request so a turn cancellation can withdraw the ask. + const result = await ctx.tools.execute({ + callId: CallId(`call-esc-${++escCall}`), + name: 'bash', + arguments: ESCALATE, + agent: escalationAgent([]), + signal: new AbortController().signal, + }) + expect(result.isError).toBe(true) + expect(text(result)).toContain('the user rejected escalating this command to "workspace-write"') + }) + + it('a cancellation denies with the cancelled text', async () => { + const { ctx } = await setupSandboxed('read-only', { approval: true }) + ctx.on('approval/request', () => Promise.resolve('cancelled')) + const result = await callAs(ctx, escalationAgent([]), ESCALATE) + expect(result.isError).toBe(true) + expect(text(result)).toContain('approval for escalating to "workspace-write" was cancelled') + }) + + it('a rogue approval stand-in returning a non-vocabulary outcome hits the exhaustiveness backstop', async () => { + const { ctx } = await setupSandboxed() + ctx.provide('approval', { request: () => Promise.resolve('yolo') } as unknown as InstanceType) + const result = await callAs(ctx, escalationAgent([]), ESCALATE) + expect(result.isError).toBe(true) + expect(text(result)).toContain('unreachable') + }) + + it('a never policy rejects an escalation deterministically without consulting any answerer', async () => { + // The live-session e.md case: the model requests escalation against a + // 'never' session — the prepend gate answers rejected before any + // interactive answerer, the fail-closed text is the ordinary rejection + // wording, and the audit pair still lands. + const { ctx } = await setupSandboxed('read-only', { approval: true, policy: 'never' }) + const consulted = vi.fn() + ctx.on('approval/request', (_req, next) => { consulted(); return next() }) + const events: Array<{ type: string; data: Record }> = [] + const result = await callAs(ctx, escalationAgent(events), ESCALATE) + expect(result.isError).toBe(true) + expect(text(result)).toContain('the user rejected escalating this command to "workspace-write"') + expect(consulted).not.toHaveBeenCalled() + expect(events.map(e => e.type)).toEqual(['approval/asked', 'approval/decided']) + expect(events[1]?.data).toMatchObject({ outcome: 'rejected' }) + }) + + it('a plain call under a sandboxing executor never consults approval', async () => { + const { ctx } = await setupSandboxed('read-only', { approval: true }) + const asked = vi.fn() + ctx.on('approval/request', (_req, next) => { asked(); return next() }) + const result = await callAs(ctx, escalationAgent([]), { command: 'echo plain', description: 'plain run' }) + expect(result.isError).toBe(false) + expect(text(result)).toContain('plain') + expect(asked).not.toHaveBeenCalled() + }) +}) + +describe('per-session sandbox mode (the bash/sandbox-mode fold)', () => { + /** Compose the real sandbox stack (passthrough runner) at a given default mode. */ + async function setupModal(mode: 'read-only' | 'workspace-write' | 'danger-full-access' = 'read-only', opts: { approval?: boolean } = {}) { + const ctx = new Context() + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(AgentRegistry) + await ctx.plugin(LocalSandboxProvider, PASSTHROUGH_RUNNER_CONFIG) + await ctx.plugin(SandboxBashExecutor, { graceMs: 200, mode }) + ;(ctx.bash as SandboxBashExecutor).internals = { spillDir } + if (opts.approval === true) await ctx.plugin(ApprovalService) + await ctx.plugin(ToolBash) + return ctx + } + + /** + * An agent stand-in over a REAL Session — the stamping folds real events; + * the opened turn satisfies approval's enclosure precondition on escalating + * calls. + */ + function sessionAgent(id: string): { agent: Agent; session: Session; injected: string[] } { + const session = new Session(SessionId(id)) + session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) + const injected: string[] = [] + const agent = { + id, + session, + inject: (content: { type: string; text: string }[]) => { injected.push(content[0]?.text ?? '') }, + } as unknown as Agent + return { agent, session, injected } + } + + let modeCall = 0 + const callAs = (ctx: Context, agent: Agent | undefined, args: unknown) => + ctx.tools.execute({ callId: CallId(`call-mode-${++modeCall}`), name: 'bash', arguments: args, ...agent ? { agent } : {} }) + + + it('stamps calls with grant > session override > nothing (executor default)', async () => { + const ctx = await setupModal('read-only', { approval: true }) + ctx.on('approval/request', () => Promise.resolve('allowed-once')) + const seen: (string | undefined)[] = [] + const original = ctx.bash.resolve.bind(ctx.bash) + vi.spyOn(ctx.bash, 'resolve').mockImplementation((req) => { + seen.push(req.sandboxMode) + return original(req) + }) + const { agent, session } = sessionAgent('sess-stamp-1') + const run = { command: 'true', description: 'stamp probe' } + await callAs(ctx, agent, run) // no override yet + setSandboxMode(session, 'workspace-write') + await callAs(ctx, agent, run) // standing override + await callAs(ctx, undefined, run) // agent-less caller: no session to fold + await callAs(ctx, agent, { ...run, sandbox_permissions: 'danger-full-access', justification: 'grant outranks override' }) + expect(seen).toEqual([undefined, 'workspace-write', undefined, 'danger-full-access']) + }) + + it('escalates relative to the session effective mode, not the executor default (narrower override)', async () => { + // The blocker scenario: a workspace-write default with a read-only + // override — the sensible escalation is workspace-write, which a + // default-relative ladder could not even express. The static target + // vocabulary advertises it and the execution check accepts it as + // strictly wider than the CALL's effective (overridden) mode. + const ctx = await setupModal('workspace-write', { approval: true }) + ctx.on('approval/request', () => Promise.resolve('allowed-once')) + const seen: (string | undefined)[] = [] + const original = ctx.bash.resolve.bind(ctx.bash) + vi.spyOn(ctx.bash, 'resolve').mockImplementation((req) => { + seen.push(req.sandboxMode) + return original(req) + }) + const { agent, session } = sessionAgent('sess-esc-narrow') + setSandboxMode(session, 'read-only') + const result = await callAs(ctx, agent, { command: 'true', description: 'd', sandbox_permissions: 'workspace-write', justification: 'the override is narrower than the default' }) + expect(result.isError).toBe(false) + expect(seen).toEqual(['workspace-write']) + }) + + it('a danger-full-access default still offers the lever to a narrower-switched session', async () => { + // Under the default-relative ladder these fields VANISHED (nothing is + // wider than the default), stranding a read-only-overridden session + // with no escalation path at all. + const ctx = await setupModal('danger-full-access', { approval: true }) + ctx.on('approval/request', () => Promise.resolve('allowed-once')) + const schema = ctx.tools.schemas().find(t => t.name === 'bash') as unknown as { parameters: { properties: Record } } + expect(schema.parameters.properties['sandbox_permissions']?.enum).toEqual(['workspace-write', 'danger-full-access']) + const { agent, session } = sessionAgent('sess-esc-dfa') + setSandboxMode(session, 'read-only') + const result = await callAs(ctx, agent, { command: 'true', description: 'd', sandbox_permissions: 'workspace-write', justification: 'confined by override under a wide default' }) + expect(result.isError).toBe(false) + }) + + it('rejects a non-widening request against the OVERRIDDEN effective mode without prompting', async () => { + const ctx = await setupModal('read-only', { approval: true }) + const consulted = vi.fn() + ctx.on('approval/request', (_req, next) => { consulted(); return next() }) + const { agent, session } = sessionAgent('sess-esc-nonwide') + setSandboxMode(session, 'danger-full-access') + const result = await callAs(ctx, agent, { command: 'true', description: 'd', sandbox_permissions: 'workspace-write', justification: 'already wider via override' }) + expect(result.isError).toBe(true) + expect(text(result)).toContain('not strictly wider than this call\'s current "danger-full-access" mode') + expect(consulted).not.toHaveBeenCalled() + }) + + it('never stamps an override under a non-sandboxing executor (nothing honors it)', async () => { + const ctx = await setup() + const seen: (string | undefined)[] = [] + const original = ctx.bash.resolve.bind(ctx.bash) + vi.spyOn(ctx.bash, 'resolve').mockImplementation((req) => { + seen.push(req.sandboxMode) + return original(req) + }) + const { agent, session } = sessionAgent('sess-stamp-2') + setSandboxMode(session, 'danger-full-access') + await callAs(ctx, agent, { command: 'true', description: 'plain probe' }) + expect(seen).toEqual([undefined]) + }) + +}) diff --git a/packages/bash/tool-bash/tsconfig.json b/packages/bash/tool-bash/tsconfig.json index 89b10bfea8..c4d738c7dd 100644 --- a/packages/bash/tool-bash/tsconfig.json +++ b/packages/bash/tool-bash/tsconfig.json @@ -25,6 +25,15 @@ }, { "path": "../../bash/bash" + }, + { + "path": "../../core/system-prompt" + }, + { + "path": "../../ui/user-approval" + }, + { + "path": "../../sandbox/sandbox" } ] } diff --git a/packages/compact/compact-basic/tests/compact-basic.spec.ts b/packages/compact/compact-basic/tests/compact-basic.spec.ts index e3d80cd567..e47ca434e3 100644 --- a/packages/compact/compact-basic/tests/compact-basic.spec.ts +++ b/packages/compact/compact-basic/tests/compact-basic.spec.ts @@ -1700,7 +1700,7 @@ describe('BasicCompactService under the real invariants plugin', () => { async function setup(): Promise<{ ctx: Context; session: Session; svc: BasicCompactService }> { const ctx = new Context() await ctx.plugin(SessionStore) - await ctx.plugin(Invariants, {}) + await ctx.plugin(Invariants) await ctx.plugin(LlmService) ctx.llm.registerAdapter(['test-model'], new ScriptedAdapter('CONDENSED')) await ctx.plugin(BasicCompactService, cfg({ auto: false })) diff --git a/packages/compact/compact-basic/tests/compact-loop-repro.spec.ts b/packages/compact/compact-basic/tests/compact-loop-repro.spec.ts index f2ca3ccdd0..1efb417b48 100644 --- a/packages/compact/compact-basic/tests/compact-loop-repro.spec.ts +++ b/packages/compact/compact-basic/tests/compact-loop-repro.spec.ts @@ -76,7 +76,7 @@ async function harness(toolSteps: number): Promise<{ ctx: Context; compact: Repr const ctx = new Context() await ctx.plugin(LlmService) await ctx.plugin(SessionStore) - await ctx.plugin(Invariants, {}) + await ctx.plugin(Invariants) await ctx.plugin(SystemPrompt) await ctx.plugin(ToolRegistry) await ctx.plugin(AgentRegistry) diff --git a/packages/cordis/tool-cordis/package.json b/packages/cordis/tool-cordis/package.json index 657013f1c4..a7d99eeea3 100644 --- a/packages/cordis/tool-cordis/package.json +++ b/packages/cordis/tool-cordis/package.json @@ -22,6 +22,7 @@ ], "license": "BSD-3-Clause", "peerDependencies": { + "@deepseek-ai/dsh-scope": "^0.0.1", "@deepseek-ai/dsh-tools": "^0.0.1", "cordis": "^4.0.0-rc.6" }, @@ -30,6 +31,7 @@ }, "devDependencies": { "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-scope": "workspace:^", "@deepseek-ai/dsh-agent-loop": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", diff --git a/packages/cordis/tool-cordis/src/api-catalog.ts b/packages/cordis/tool-cordis/src/api-catalog.ts index 68fd63ae25..b4420e80c7 100644 --- a/packages/cordis/tool-cordis/src/api-catalog.ts +++ b/packages/cordis/tool-cordis/src/api-catalog.ts @@ -54,11 +54,11 @@ export interface TypeApiEntry { export const SERVICE_API: readonly ServiceApiEntry[] = [ { key: 'agentLoop', - summary: 'The agent-loop plugin (`ctx.agentLoop`): creates ReactLoopAgents, runs their loops, and registers them in `ctx.agents`.', + summary: 'Concrete ReactLoopAgent factory and driver service.', methods: [ - 'create(id: AgentId, options: AgentOptions = {}): ReactLoopAgent', - 'createAgent(options: CreateAgentOptions): AgentHandle', - 'async resume(options: ResumeAgentOptions): Promise', + 'create(id: AgentId, options: AgentOptions = {}, meta: Pick = {}): ReactLoopAgent', + 'async createAgent(ownerCtx: Context, options: CreateAgentOptions): Promise', + 'async resume(ownerCtx: Context, options: ResumeAgentOptions): Promise', ], }, { @@ -66,13 +66,22 @@ export const SERVICE_API: readonly ServiceApiEntry[] = [ summary: 'Agent registry (`ctx.agents`): tracks live agents so UI, hook, and orchestrator plugins can find them without depending on the concrete loop package.', methods: [ 'setFactory(factory: AgentFactory): () => void', - 'create(options: CreateAgentOptions): AgentHandle', + 'async create(options: CreateAgentOptions): Promise', 'async resume(options: ResumeAgentOptions): Promise', 'register(agent: Agent): () => void', + 'enter(agent: Agent): () => void', + 'announce(agent: Agent): void', 'get(id: AgentId): Agent | undefined', 'list(): Agent[]', ], }, + { + key: 'approval', + summary: 'The `ctx.approval` service: dispatches ApprovalRequests to the `approval/request` waterfall and audits every ask/outcome pair to the requesting agent\'s session log.', + methods: [ + 'async request(req: ApprovalRequest): Promise', + ], + }, { key: 'bash', summary: 'Abstract bash execution service.', @@ -125,6 +134,13 @@ export const SERVICE_API: readonly ServiceApiEntry[] = [ 'stream(options: GenerateOptions): AsyncIterable', ], }, + { + key: 'sandbox', + summary: 'Abstract process-sandbox service.', + methods: [ + 'abstract confine(argv: readonly string[], policy: SandboxPolicy): ConfinedArgv', + ], + }, { key: 'sessionPersistence', summary: 'Abstract durable session-persistence service.', @@ -143,19 +159,30 @@ export const SERVICE_API: readonly ServiceApiEntry[] = [ 'prepare(id?: SessionId, options?: CreateSessionOptions): Session', 'enter(session: Session): () => void', 'announce(session: Session): void', + 'async flush(session: Session): Promise', 'get(id: SessionId): Session | undefined', 'list(): Session[]', 'fork(source: SessionForkSource, boundary?: number, childSessionId?: SessionId): Session', ], }, + { + key: 'skills', + summary: 'Registry of skill providers.', + methods: [ + 'registerProvider(provider: SkillProvider): () => void', + 'register(skill: SkillRegistration): () => void', + 'async list(options: SkillLookupOptions = {}): Promise', + 'async get(name: string, options: SkillLookupOptions = {}): Promise', + ], + }, { key: 'subagents', - summary: 'The `subagents` service: a registry of named SubagentProviders and a capability-checked start surface.', + summary: 'Named provider registry and capability-checked start surface.', methods: [ 'registerProvider(provider: SubagentProvider): () => void', 'getProvider(name: string): SubagentProvider | undefined', 'list(): string[]', - 'start(name: string, request: SubagentStartRequest): SubagentRun', + 'async start(name: string, request: SubagentStartRequest): Promise', ], }, { @@ -163,19 +190,21 @@ export const SERVICE_API: readonly ServiceApiEntry[] = [ summary: 'Registry service (`ctx.systemPrompt`): plugins contribute ordered text sections, tool-schema providers, and named prompt variables; the agent loop calls `assemble(context)` once per step.', methods: [ 'section(section: PromptSection): () => void', - 'tools(provider: () => ToolSchema[]): () => void', + 'tools(provider: (context: AssembleContext) => ToolProviderResult): () => void', 'variable(name: string, provider: (context: AssembleContext) => string | undefined): () => void', 'async assemble(context: AssembleContext = {}): Promise', ], }, { key: 'tools', - summary: 'Tool registry (`ctx.tools`): tool plugins register definitions; the agent loop executes calls through the `tools/pre-execute` → `tools/execute` → `tools/post-execute` pipeline.', + summary: 'Tool registry (`ctx.tools`): tool plugins register definitions; the agent loop executes calls through the `tools/pre-execute` → guards → `tools/execute` → `tools/post-execute` → `tools/result` pipeline.', methods: [ 'register(definition: ToolDefinition): () => void', - 'get(name: string): ToolDefinition | undefined', - 'schemas(): ToolSchema[]', - 'async execute(exec: ToolExecution): Promise', + 'restrict(filter: ToolRestriction): () => void', + 'guard(guard: ToolGuard): () => void', + 'get(name: string, scope?: ScopeKey): ToolDefinition | undefined', + 'schemas(scope?: ScopeKey): ToolSchema[]', + 'async execute(exec: ToolExecutionInput): Promise', ], }, { @@ -210,75 +239,87 @@ export const EVENT_API: readonly EventApiEntry[] = [ { name: 'agent/created', mode: 'emit', - signature: '\'agent/created\'(agent: Agent): void', - summary: 'An agent was registered in the AgentRegistry and is ready to receive messages.', + signature: '\'agent/created\'(this: Scoped, agent: Agent): void', + summary: 'An agent\'s fully composed scoped world was published in the AgentRegistry.', }, { name: 'agent/disposed', mode: 'emit', - signature: '\'agent/disposed\'(agent: Agent): void', - summary: 'An agent was disposed and removed from the registry; its fiber and any in-flight turn have been torn down.', + signature: '\'agent/disposed\'(this: Scoped, agent: Agent): void', + summary: 'An agent was removed from the registry.', }, { name: 'agent/error', mode: 'emit', - signature: '\'agent/error\'(agent: Agent, turn: number, step: number, error: Error): void', + signature: '\'agent/error\'(this: Scoped, agent: Agent, turn: number, step: number, error: Error): void', summary: 'A step or turn errored.', }, { name: 'agent/pre-step', mode: 'serial', - signature: '\'agent/pre-step\'(agent: Agent, turn: number, step: number, fullSystemPrompt: string, sessionPrefix: readonly Message[], signal: AbortSignal): Promise | void', + signature: '\'agent/pre-step\'(this: Scoped, agent: Agent, turn: number, step: number, fullSystemPrompt: string, sessionPrefix: readonly Message[], signal: AbortSignal): Promise | void', summary: 'Awaited pre-step surface-mutation checkpoint, fired once per step AFTER `turn/start` (and after the prior step closed) but BEFORE this step\'s `step/start` — so anything a listener appends lands OUTSIDE the step, between `turn/start`/`step/end` and the upcoming `step/start`.', }, { name: 'agent/prompt-submit', mode: 'waterfall', - signature: '\'agent/prompt-submit\'(agent: Agent, content: ContentBlock[], source: MessageSource, next: () => Promise): Promise', + signature: '\'agent/prompt-submit\'(this: Scoped, agent: Agent, content: ContentBlock[], source: MessageSource, next: () => Promise): Promise', summary: 'Waterfall: decide what happens to ONE drained queued message before it becomes a `user/message` — allow (optionally rewriting the prompt bytes or attaching `additionalContext`) or block it.', }, { name: 'agent/queued', mode: 'emit', - signature: '\'agent/queued\'(agent: Agent, content: ContentBlock[], info: { source: MessageSource; steering: boolean }): void', + signature: '\'agent/queued\'(this: Scoped, agent: Agent, content: ContentBlock[], info: { source: MessageSource; steering: boolean }): void', summary: 'A message entered the agent\'s inbox (queued or steering).', }, { name: 'agent/request', mode: 'waterfall', - signature: '\'agent/request\'(agent: Agent, turn: number, step: number, config: LlmCallConfig, next: () => Promise): Promise', + signature: '\'agent/request\'(this: Scoped, agent: Agent, turn: number, step: number, config: LlmCallConfig, next: () => Promise): Promise', summary: 'Waterfall: shape the step\'s call configuration — model switching, sampling overrides — by returning a replacement LlmCallConfig (the frozen seed is the config the loop would otherwise use).', }, { name: 'agent/session-prefix', mode: 'waterfall', - signature: '\'agent/session-prefix\'(agent: Agent, prefix: Message[], signal: AbortSignal, next: () => Promise): Promise', + signature: '\'agent/session-prefix\'(this: Scoped, agent: Agent, prefix: Message[], signal: AbortSignal, next: () => Promise): Promise', summary: 'Waterfall: compose the SESSION PREFIX — request-only messages placed in front of the ENTIRE derived history (directly after the provider\'s system slot) on every request this loop instance sends.', }, { name: 'agent/session-start', mode: 'emit', - signature: '\'agent/session-start\'(agent: Agent, source: SessionStartSource): void', + signature: '\'agent/session-start\'(this: Scoped, agent: Agent, source: SessionStartSource): void', summary: 'The agent\'s session lifecycle began, fired once before its first turn.', }, { name: 'agent/status', mode: 'emit', - signature: '\'agent/status\'(agent: Agent, status: AgentStatus): void', + signature: '\'agent/status\'(this: Scoped, agent: Agent, status: AgentStatus): void', summary: 'Agent status changed (`idle` ⇄ `running`, or → `disposed`).', }, { name: 'agent/step-result', mode: 'waterfall', - signature: '\'agent/step-result\'(agent: Agent, turn: number, step: number, message: Message, next: () => Promise): Promise', + signature: '\'agent/step-result\'(this: Scoped, agent: Agent, turn: number, step: number, message: Message, next: () => Promise): Promise', summary: 'Waterfall: post-process the assembled assistant Message before tool dispatch (validation, content rewriting, …).', }, { name: 'agent/turn-continuation', mode: 'waterfall', - signature: '\'agent/turn-continuation\'(agent: Agent, turn: number, defaultDecision: ContinuationDecision, next: () => Promise): Promise', + signature: '\'agent/turn-continuation\'(this: Scoped, agent: Agent, turn: number, defaultDecision: ContinuationDecision, next: () => Promise): Promise', summary: 'Waterfall: override the turn-continuation decision via a typed ContinuationDecision.', }, + { + name: 'agent/turn-stop', + mode: 'serial', + signature: '\'agent/turn-stop\'(this: Scoped, agent: Agent, turn: number): ContinuationStop | undefined', + summary: 'Serial terminal-stop checkpoint after the ordinary `agent/turn-continuation` waterfall, any `continue.reason`, and the pending-steering continuation override have been folded.', + }, + { + name: 'approval/request', + mode: 'waterfall', + signature: '\'approval/request\'(this: Scoped, req: ApprovalRequest, next: () => Promise): Promise', + summary: 'Waterfall asking the composed answerers to decide one approval request.', + }, { name: 'fs/edit-intent', mode: 'waterfall', @@ -306,81 +347,105 @@ export const EVENT_API: readonly EventApiEntry[] = [ { name: 'session/created', mode: 'emit', - signature: '\'session/created\'(session: Session): void', + signature: '\'session/created\'(this: Scoped, session: Session): void', summary: 'A session was created in the store.', }, + { + name: 'session/disposed', + mode: 'emit', + signature: '\'session/disposed\'(this: Scoped, session: Session): void', + summary: 'A previously announced session left the store.', + }, { name: 'session/event', mode: 'emit', - signature: '\'session/event\'(session: Session, event: SessionEvent): void', + signature: '\'session/event\'(this: Scoped, session: Session, event: SessionEvent): void', summary: 'An event was appended to a session log (sync, fire-and-forget).', }, { name: 'session/flush', mode: 'parallel', - signature: '\'session/flush\'(session: Session): Promise | void', + signature: '\'session/flush\'(this: Scoped, session: Session): Promise | void', summary: 'Awaited durability checkpoint.', }, + { + name: 'skill/provider-added', + mode: 'emit', + signature: '\'skill/provider-added\'(provider: SkillProvider): void', + summary: 'A skill provider became resolvable in the `ctx.skills` registry.', + }, + { + name: 'skill/provider-removed', + mode: 'emit', + signature: '\'skill/provider-removed\'(name: string): void', + summary: 'A skill provider left the registry because its plugin fiber was disposed.', + }, { name: 'subagent/end', mode: 'emit', - signature: '\'subagent/end\'(info: SubagentRunEndInfo): void', - summary: 'A subagent run settled — emitted when SubagentRun.result resolves (any stop reason).', + signature: '\'subagent/end\'(this: Scoped, info: SubagentRunEndInfo): void', + summary: 'A ready child settled.', }, { name: 'subagent/provider-added', mode: 'emit', signature: '\'subagent/provider-added\'(provider: SubagentProvider): void', - summary: 'A provider became resolvable in the SubagentService registry.', + summary: 'A provider became resolvable in the registry.', }, { name: 'subagent/provider-removed', mode: 'emit', signature: '\'subagent/provider-removed\'(name: string): void', - summary: 'A provider left the registry (its plugin\'s fiber was disposed — an unload or an HMR reload).', + summary: 'A provider left the registry.', }, { name: 'subagent/start', mode: 'emit', - signature: '\'subagent/start\'(info: SubagentRunInfo): void', - summary: 'A subagent run started — emitted after the provider is resolved and its capabilities validated, as the child run begins.', + signature: '\'subagent/start\'(this: Scoped, info: SubagentRunInfo): void', + summary: 'A provider established a ready child.', }, { name: 'system-prompt/assemble', mode: 'waterfall', - signature: '\'system-prompt/assemble\'(this: SystemPrompt, assembly: PromptAssembly, context: AssembleContext, next: () => Promise): Promise', + signature: '\'system-prompt/assemble\'(this: Scoped, assembly: PromptAssembly, context: AssembleContext, next: () => Promise): Promise', summary: 'Waterfall around prompt assembly — mutate or extend the PromptAssembly (sections + tools + variables) before it is rendered.', }, { name: 'system-prompt/change', mode: 'emit', signature: '\'system-prompt/change\'(): void', - summary: 'A section, tool provider, or variable provider was registered or unregistered (the assembly inputs changed).', + summary: 'A section, tool provider, or variable provider was registered or unregistered (the assembly inputs changed — possibly for one scope only).', }, { name: 'tools/change', mode: 'emit', signature: '\'tools/change\'(): void', - summary: 'A tool was registered or unregistered (the available tool set changed).', + summary: 'A tool was registered or unregistered, or a scoped restriction changed (the available tool set changed — possibly for one scope only).', }, { name: 'tools/execute', mode: 'waterfall', - signature: '\'tools/execute\'(this: ToolRegistry, exec: ToolExecution, next: () => Promise): Promise', + signature: '\'tools/execute\'(this: Scoped, exec: ToolExecution, next: () => Promise): Promise', summary: 'Around-dispatch waterfall wrapping the registry\'s core tool dispatch, between the `tools/pre-execute` gate and the `tools/post-execute` seam.', }, { name: 'tools/post-execute', mode: 'waterfall', - signature: '\'tools/post-execute\'(this: ToolRegistry, exec: ToolExecution, result: ToolExecutionResult, next: () => Promise): Promise', + signature: '\'tools/post-execute\'(this: Scoped, exec: ToolExecution, result: Readonly, next: () => Promise): Promise', summary: 'Waterfall AFTER a tool runs — where hook plugins inspect the result and accept it (optionally REPLACING the model-facing content, and/or attaching `additionalContext` for the next request) or block it with corrective `feedback` (Claude Code\'s `PostToolUse`).', }, { name: 'tools/pre-execute', mode: 'waterfall', - signature: '\'tools/pre-execute\'(this: ToolRegistry, exec: ToolExecution, next: () => Promise): Promise', + signature: '\'tools/pre-execute\'(this: Scoped, exec: ToolExecution, next: () => Promise): Promise', summary: 'Waterfall BEFORE a tool runs — the gate where sandbox, permission, and hook plugins allow or deny a call (Claude Code\'s `PreToolUse`).', }, + { + name: 'tools/result', + mode: 'emit', + signature: '\'tools/result\'(this: Scoped, exec: Readonly, result: Readonly): undefined', + summary: 'Synchronous notification of the authoritative FINAL tool outcome, after the complete pre/execute/post pipeline, final lossless-JSON validation, and outer error normalization.', + }, { name: 'workflow/agent-end', mode: 'emit', @@ -391,7 +456,7 @@ export const EVENT_API: readonly EventApiEntry[] = [ name: 'workflow/agent-start', mode: 'emit', signature: '\'workflow/agent-start\'(info: WorkflowRunInfo, agent: WorkflowAgentInfo): void', - summary: 'One `agent()` call started a child run.', + summary: 'One `agent()` call established a ready child run.', }, { name: 'workflow/end', @@ -423,11 +488,11 @@ export const EVENT_API: readonly EventApiEntry[] = [ export const TYPE_API: readonly TypeApiEntry[] = [ { name: 'Agent', - declaration: 'export interface Agent {\n readonly id: AgentId;\n readonly options: AgentOptions;\n readonly session: Session;\n readonly status: AgentStatus;\n send(content: ContentBlock[], options?: SendOptions): void;\n steer(content: ContentBlock[], options?: SendOptions): void;\n inject(content: ContentBlock[], options?: SendOptions): void;\n cancel(reason?: string): void;\n whenIdle(): Promise;\n}', + declaration: 'export interface Agent {\n readonly id: AgentId;\n readonly options: AgentOptions;\n readonly session: Session;\n readonly status: AgentStatus;\n readonly ctx: Context;\n send(content: ContentBlock[], options?: SendOptions): void;\n steer(content: ContentBlock[], options?: SendOptions): void;\n inject(content: ContentBlock[], options?: SendOptions): void;\n cancel(reason?: string): void;\n whenIdle(): Promise;\n}', }, { name: 'AgentFactory', - declaration: 'export interface AgentFactory {\n createAgent(options: CreateAgentOptions): AgentHandle;\n resume(options: ResumeAgentOptions): Promise;\n}', + declaration: 'export interface AgentFactory {\n createAgent(ownerCtx: Context, options: CreateAgentOptions): Promise;\n resume(ownerCtx: Context, options: ResumeAgentOptions): Promise;\n}', }, { name: 'AgentHandle', @@ -445,6 +510,14 @@ export const TYPE_API: readonly TypeApiEntry[] = [ name: 'AgentStatus', declaration: 'export type AgentStatus = \'idle\' | \'running\' | \'disposed\';', }, + { + name: 'ApprovalOutcome', + declaration: 'export type ApprovalOutcome = \'allowed-once\' | \'rejected\' | \'cancelled\' | \'unavailable\';', + }, + { + name: 'ApprovalRequest', + declaration: 'export interface ApprovalRequest {\n readonly agent: Agent;\n readonly toolName: string;\n readonly callId?: CallId;\n readonly reason?: string;\n readonly signal?: AbortSignal;\n}', + }, { name: 'AskUserQuestionAnswer', declaration: 'export interface AskUserQuestionAnswer {\n answers: AskUserQuestionAnswerItem[];\n}', @@ -467,7 +540,7 @@ export const TYPE_API: readonly TypeApiEntry[] = [ }, { name: 'AssembleContext', - declaration: 'export interface AssembleContext {\n}', + declaration: 'export interface AssembleContext {\n scope?: ScopeKey;\n}', }, { name: 'AssembledSection', @@ -475,19 +548,23 @@ export const TYPE_API: readonly TypeApiEntry[] = [ }, { name: 'BashExecRequest', - declaration: 'export interface BashExecRequest {\n command: string;\n workdir?: string | undefined;\n timeoutMs?: number | undefined;\n signal?: AbortSignal | undefined;\n stdin?: string | undefined;\n env?: Record | undefined;\n owner?: OwnerToken | undefined;\n}', + declaration: 'export interface BashExecRequest {\n command: string;\n workdir?: string | undefined;\n timeoutMs?: number | undefined;\n signal?: AbortSignal | undefined;\n stdin?: string | undefined;\n env?: Record | undefined;\n owner?: OwnerToken | undefined;\n sandboxMode?: SandboxMode | undefined;\n}', }, { name: 'BashExecSpec', - declaration: 'export interface BashExecSpec {\n command: string;\n workdir: string;\n timeoutMs: number;\n signal?: AbortSignal | undefined;\n stdin?: string | undefined;\n env?: Record | undefined;\n owner: OwnerToken | undefined;\n}', + declaration: 'export interface BashExecSpec {\n command: string;\n workdir: string;\n timeoutMs: number;\n signal?: AbortSignal | undefined;\n stdin?: string | undefined;\n env?: Record | undefined;\n owner: OwnerToken | undefined;\n sandboxMode: SandboxMode | undefined;\n}', }, { name: 'BashRunResult', - declaration: 'export interface BashRunResult {\n exitCode: number | null;\n signal: NodeJS.Signals | null;\n timedOut: boolean;\n aborted: boolean;\n timeoutMs: number;\n stdout: CollectedOutput;\n stderr: CollectedOutput;\n}', + declaration: 'export interface BashRunResult {\n exitCode: number | null;\n signal: NodeJS.Signals | null;\n timedOut: boolean;\n aborted: boolean;\n timeoutMs: number;\n stdout: CollectedOutput;\n stderr: CollectedOutput;\n sandbox?: BashSandboxInfo;\n}', + }, + { + name: 'BashSandboxInfo', + declaration: 'export interface BashSandboxInfo {\n mode: SandboxMode;\n denied: boolean;\n enforcement?: SandboxEnforcement;\n runnerFailed?: boolean;\n}', }, { name: 'BashTask', - declaration: 'export interface BashTask {\n readonly id: BashTaskId;\n readonly command: string;\n status: BashTaskStatus;\n exitCode: number | null;\n signal: NodeJS.Signals | null;\n readonly done: Promise;\n}', + declaration: 'export interface BashTask {\n readonly id: BashTaskId;\n readonly command: string;\n status: BashTaskStatus;\n exitCode: number | null;\n signal: NodeJS.Signals | null;\n readonly done: Promise;\n sandbox?: BashSandboxInfo;\n}', }, { name: 'BashTaskId', @@ -549,6 +626,14 @@ export const TYPE_API: readonly TypeApiEntry[] = [ name: 'CompactionResult', declaration: 'export interface CompactionResult {\n startSeq: number;\n summarySeq: number;\n endSeq: number;\n summary: ContentBlock[];\n shadowedRange: {\n start: number;\n end: number;\n };\n shadowedSeqs: number[];\n shadowedTokenCount: number;\n}', }, + { + name: 'ConfinedArgv', + declaration: 'export interface ConfinedArgv {\n argv: string[];\n enforcement: SandboxEnforcement;\n denialSignatures: readonly string[];\n runnerFailureSignatures: readonly string[];\n}', + }, + { + name: 'ConfinedSandboxMode', + declaration: 'export type ConfinedSandboxMode = Exclude;', + }, { name: 'ContentBlockMap', declaration: 'export interface ContentBlockMap {\n \'text\': TextBlock;\n \'reasoning\': ReasoningBlock;\n \'tool-call\': ToolCallBlock;\n \'tool-result\': ToolResultBlock;\n}', @@ -559,11 +644,11 @@ export const TYPE_API: readonly TypeApiEntry[] = [ }, { name: 'CreateAgentOptions', - declaration: 'export interface CreateAgentOptions {\n agentId: AgentId;\n sessionId: SessionId;\n meta?: {\n cwd?: string;\n parentSession?: SessionId;\n seedLength?: number;\n };\n seed?: SessionEvent[];\n agentOptions?: AgentOptions;\n}', + declaration: 'export interface CreateAgentOptions {\n readonly agentId: AgentId;\n readonly sessionId: SessionId;\n readonly meta?: {\n readonly cwd?: string;\n readonly parentSession?: SessionId;\n readonly seedLength?: number;\n };\n readonly seed?: readonly SessionEvent[];\n readonly agentOptions?: AgentOptions;\n readonly signal?: AbortSignal;\n readonly setup?: (agentCtx: Context) => Promise | void;\n}', }, { name: 'CreateSessionOptions', - declaration: 'export interface CreateSessionOptions {\n seed?: SessionEvent[];\n meta?: {\n cwd?: string;\n parentSession?: SessionId;\n createdAt?: number;\n seedLength?: number;\n };\n}', + declaration: 'export interface CreateSessionOptions {\n readonly seed?: readonly SessionEvent[];\n readonly meta?: {\n readonly cwd?: string;\n readonly parentSession?: SessionId;\n readonly createdAt?: number;\n readonly seedLength?: number;\n };\n}', }, { name: 'DiffCallView', @@ -663,7 +748,7 @@ export const TYPE_API: readonly TypeApiEntry[] = [ }, { name: 'PromptSection', - declaration: 'export interface PromptSection {\n name: string;\n order: number;\n text: string | ((context: AssembleContext) => string);\n}', + declaration: 'export interface PromptSection {\n readonly name: string;\n readonly order: number;\n readonly text: string | ((context: AssembleContext) => string);\n}', }, { name: 'ReasoningBlock', @@ -671,7 +756,23 @@ export const TYPE_API: readonly TypeApiEntry[] = [ }, { name: 'ResumeAgentOptions', - declaration: 'export interface ResumeAgentOptions {\n agentId: AgentId;\n resumeSessionId: SessionId;\n agentOptions?: AgentOptions;\n}', + declaration: 'export interface ResumeAgentOptions {\n readonly agentId: AgentId;\n readonly resumeSessionId: SessionId;\n readonly agentOptions?: AgentOptions;\n readonly signal?: AbortSignal;\n readonly setup?: (agentCtx: Context) => Promise | void;\n}', + }, + { + name: 'SandboxEnforcement', + declaration: 'export type SandboxEnforcement = \'full\' | \'partial\';', + }, + { + name: 'SandboxMode', + declaration: 'export type SandboxMode = \'read-only\' | \'workspace-write\' | \'danger-full-access\';', + }, + { + name: 'SandboxPolicy', + declaration: 'export interface SandboxPolicy {\n mode: ConfinedSandboxMode;\n workspaceRoot: string;\n}', + }, + { + name: 'ScopeKey', + declaration: 'export type ScopeKey = object;', }, { name: 'SendOptions', @@ -695,12 +796,44 @@ export const TYPE_API: readonly TypeApiEntry[] = [ }, { name: 'SessionHeader', - declaration: 'export interface SessionHeader {\n version: number;\n id: SessionId;\n createdAt: number;\n cwd?: string;\n parentSession?: SessionId;\n seedLength?: number;\n}', + declaration: 'export interface SessionHeader {\n readonly version: number;\n readonly id: SessionId;\n readonly createdAt: number;\n readonly cwd?: string;\n readonly parentSession?: SessionId;\n readonly seedLength?: number;\n}', }, { name: 'SessionId', declaration: 'export type SessionId = Branded<\'SessionId\'>;', }, + { + name: 'SkillCandidate', + declaration: 'export interface SkillCandidate extends SkillSummary {\n readonly rank: number;\n readonly locator: unknown;\n readonly path?: string;\n readonly metadata?: Readonly>;\n}', + }, + { + name: 'SkillDefinition', + declaration: 'export interface SkillDefinition extends SkillSummary {\n readonly content: string;\n readonly path?: string;\n readonly metadata?: Readonly>;\n}', + }, + { + name: 'SkillLookupOptions', + declaration: 'export interface SkillLookupOptions {\n readonly cwd?: string | undefined;\n readonly signal?: AbortSignal | undefined;\n}', + }, + { + name: 'SkillProvider', + declaration: 'export interface SkillProvider {\n readonly name: string;\n readonly list: (options: SkillLookupOptions) => Promise;\n readonly get: (candidate: SkillCandidate, options: SkillLookupOptions) => Promise;\n}', + }, + { + name: 'SkillRegistration', + declaration: 'export type SkillRegistration = Omit & {\n readonly provider?: string;\n};', + }, + { + name: 'SkillResourceBase', + declaration: 'export type SkillResourceBase = {\n readonly kind: \'directory\';\n readonly path: string;\n} | {\n readonly kind: \'url\';\n readonly url: string;\n} | {\n readonly kind: \'opaque\';\n readonly description: string;\n};', + }, + { + name: 'SkillSource', + declaration: 'export type SkillSource = \'project-dsh\' | \'project-agents\' | \'runtime\' | \'user-dsh\' | \'user-agents\' | \'custom\' | (string & {});', + }, + { + name: 'SkillSummary', + declaration: 'export interface SkillSummary {\n readonly name: string;\n readonly description: string;\n readonly whenToUse?: string;\n readonly disableModelInvocation?: boolean;\n readonly source: SkillSource;\n readonly provider: string;\n readonly resourceBase?: SkillResourceBase;\n}', + }, { name: 'StreamChunk', declaration: 'export type StreamChunk = {\n type: \'block-start\';\n index: number;\n blockType: ContentBlockType;\n} | {\n type: \'text-delta\';\n index: number;\n text: string;\n} | {\n type: \'reasoning-delta\';\n index: number;\n text: string;\n} | {\n type: \'tool-call-delta\';\n index: number;\n id: CallId;\n name?: string;\n argumentsDelta: string;\n} | {\n type: \'block-end\';\n index: number;\n block: ContentBlock;\n} | {\n type: \'usage\';\n usage: TokenUsage;\n} | {\n type: \'finish\';\n reason: FinishReason;\n};', @@ -723,23 +856,23 @@ export const TYPE_API: readonly TypeApiEntry[] = [ }, { name: 'SubagentCapabilities', - declaration: 'export interface SubagentCapabilities {\n outputSchema: boolean;\n depthLimit: boolean;\n toolFilter: boolean;\n}', + declaration: 'export interface SubagentCapabilities {\n readonly outputSchema: boolean;\n readonly depthLimit: boolean;\n readonly toolFilter: boolean;\n readonly persona: boolean;\n}', }, { name: 'SubagentProvider', - declaration: 'export interface SubagentProvider {\n readonly name: string;\n readonly capabilities: SubagentCapabilities;\n readonly inheritsParentContext: boolean;\n start(request: SubagentStartRequest): SubagentRun;\n}', + declaration: 'export interface SubagentProvider {\n readonly name: string;\n readonly capabilities: SubagentCapabilities;\n readonly inheritsParentContext: boolean;\n start(request: SubagentStartRequest): Promise;\n}', }, { name: 'SubagentResult', - declaration: 'export interface SubagentResult {\n output: ContentBlock[];\n structured?: unknown;\n stopReason: SubagentStopReason;\n}', + declaration: 'export interface SubagentResult {\n readonly output: ContentBlock[];\n readonly structured?: unknown;\n readonly stopReason: SubagentStopReason;\n}', }, { name: 'SubagentRun', - declaration: 'export interface SubagentRun {\n readonly id: AgentId;\n readonly result: Promise;\n cancel(reason?: string): void;\n dispose(): Promise;\n sendMessage?(content: ContentBlock[]): void;\n resume?(content: ContentBlock[]): SubagentRun;\n}', + declaration: 'export interface SubagentRun {\n readonly id: AgentId;\n readonly result: Promise;\n dispose(): Promise;\n sendMessage?(content: ContentBlock[]): void;\n resume?(content: ContentBlock[]): Promise;\n}', }, { name: 'SubagentStartRequest', - declaration: 'export interface SubagentStartRequest {\n prompt: ContentBlock[];\n parent: Agent;\n signal?: AbortSignal;\n agentOptions?: AgentOptions;\n outputSchema?: StructuredOutputSchema;\n maxDepth?: number;\n toolFilter?: {\n allow?: string[];\n deny?: string[];\n };\n}', + declaration: 'export interface SubagentStartRequest {\n readonly prompt: ContentBlock[];\n readonly parent: Agent;\n readonly signal: AbortSignal;\n readonly agentOptions?: AgentOptions;\n readonly outputSchema?: StructuredOutputSchema;\n readonly maxDepth?: number;\n readonly toolFilter?: ToolRestriction;\n readonly persona?: string;\n}', }, { name: 'SubagentStopReason', @@ -799,12 +932,32 @@ export const TYPE_API: readonly TypeApiEntry[] = [ }, { name: 'ToolExecution', - declaration: 'export interface ToolExecution {\n callId: CallId;\n name: string;\n arguments: unknown;\n agent?: Agent;\n signal?: AbortSignal;\n}', + declaration: 'export interface ToolExecution extends ToolExecutionInput {\n readonly token: ToolExecutionToken;\n}', + }, + { + name: 'ToolExecutionInput', + declaration: 'export interface ToolExecutionInput {\n readonly callId: CallId;\n readonly name: string;\n readonly arguments: unknown;\n readonly agent?: Agent;\n readonly parent?: ToolExecutionToken;\n signal?: AbortSignal;\n}', }, { name: 'ToolExecutionResult', declaration: 'export interface ToolExecutionResult {\n callId: CallId;\n content: ContentBlock[];\n isError: boolean;\n error?: ToolErrorInfo;\n additionalContext?: HookContext;\n meta?: unknown;\n}', }, + { + name: 'ToolExecutionToken', + declaration: 'export type ToolExecutionToken = symbol & {\n readonly [toolExecutionTokenBrand]: true;\n};', + }, + { + name: 'ToolGuard', + declaration: 'export type ToolGuard = (execution: Readonly) => string | undefined;', + }, + { + name: 'ToolProviderResult', + declaration: 'export interface ToolProviderResult {\n readonly schemas: readonly ToolSchema[];\n readonly knownNames?: readonly string[];\n}', + }, + { + name: 'ToolRestriction', + declaration: 'export interface ToolRestriction {\n readonly allow?: readonly string[];\n readonly deny?: readonly string[];\n}', + }, { name: 'ToolResult', declaration: 'export interface ToolResult {\n content: ContentBlock[];\n isError: boolean;\n meta?: unknown;\n}', diff --git a/packages/cordis/tool-cordis/src/guard.ts b/packages/cordis/tool-cordis/src/guard.ts index f51faeb42e..3e1c70e461 100644 --- a/packages/cordis/tool-cordis/src/guard.ts +++ b/packages/cordis/tool-cordis/src/guard.ts @@ -50,6 +50,7 @@ import { Context } from 'cordis' import type { Plugin } from 'cordis' +import { scopeOf } from '@deepseek-ai/dsh-scope' import { defineTool } from '@deepseek-ai/dsh-tools' import type { ToolDefinition, ToolExecuteReturn } from '@deepseek-ai/dsh-tools' @@ -252,15 +253,20 @@ const CTX_VERBS = new Set(['on', 'once', 'provide', 'timeout', 'interval', 'setT * metadata (`schemas`, and `get` returning a schema view, never the live * `ToolDefinition`). Exposing the raw definition would hand mount code the * tool's `execute` function, letting it call another tool directly and bypass - * `ToolRegistry.execute` — the pre/post-execute waterfall (permission gates, - * accounting) and result normalization. So `get` returns the same + * `ToolRegistry.execute` — identity protection, pre-policy, monotonic guards, + * around dispatch, post-policy, final observation, and result normalization. So `get` returns the same * name/description/parameters view as `schemas()`, and nothing invocable. */ function sandboxTools(ctx: Context): Record { + // Reads resolve through the MOUNT's own scope (`scopeOf(ctx)`), mirroring + // where the façade's `register` lands its writes (the calling context's + // layer): mount code always sees the tools its own world sees — the global + // view for today's global mounts, its agent's view if a mount ever runs + // under an agent scope. return { register: (tool: unknown): (() => void) => sandboxRegisterTool(ctx, tool), - schemas: () => ctx.tools.schemas(), - get: (name: string) => ctx.tools.schemas().find(schema => schema.name === name), + schemas: () => ctx.tools.schemas(scopeOf(ctx)), + get: (name: string) => ctx.tools.schemas(scopeOf(ctx)).find(schema => schema.name === name), } } diff --git a/packages/cordis/tool-cordis/src/index.ts b/packages/cordis/tool-cordis/src/index.ts index 836d516120..409123f7c6 100644 --- a/packages/cordis/tool-cordis/src/index.ts +++ b/packages/cordis/tool-cordis/src/index.ts @@ -101,11 +101,13 @@ export function apply(ctx: Context, config: Config): void { description: 'Limit the report to one section. Omit for all sections.', }, }, - execute(args): Promise<{ type: 'text'; text: string }[]> { + execute(args, exec): Promise<{ type: 'text'; text: string }[]> { const sections: [heading: string, body: () => string[]][] = [ ['services', () => describeServices(ctx)], ['plugins', () => describePlugins(ctx)], - ['tools', () => describeTools(ctx)], + // The calling agent's view: scoped/shadowed tools included, restricted + // globals absent — "what you can call", not the global registry. + ['tools', () => describeTools(ctx, exec.agent)], ['dynamic', () => describeDynamic(ctx, mounts)], ['api', () => describeApi(ctx)], ['events', () => describeEvents()], diff --git a/packages/cordis/tool-cordis/src/inspect.ts b/packages/cordis/tool-cordis/src/inspect.ts index 5b44ed7ca5..260fa4dd14 100644 --- a/packages/cordis/tool-cordis/src/inspect.ts +++ b/packages/cordis/tool-cordis/src/inspect.ts @@ -10,6 +10,7 @@ */ import type { Context, Fiber } from 'cordis' +import type { ScopeKey } from '@deepseek-ai/dsh-scope' import { EVENT_API, INHERITED_CTX_API, SERVICE_API, TYPE_API } from './api-catalog.ts' import type { EventApiEntry, InheritedApiEntry, ServiceApiEntry, TypeApiEntry } from './api-catalog.ts' import { FiberState, STATE_LABELS } from './fiber-state.ts' @@ -76,12 +77,15 @@ export function describePlugins(ctx: Context): string[] { } /** - * The `tools` section: the model-facing tool names currently registered. + * The `tools` section: the model-facing tool names the CALLING agent can see + * (its scoped layer shadowing/joining the restricted global surface) — the + * honest answer to the tool description's "what you can call". * @param ctx - the runtime whose tool registry is read. - * @returns one line per registered tool. + * @param scope - the calling agent (the viewing scope); omitted = global view. + * @returns one line per visible tool. */ -export function describeTools(ctx: Context): string[] { - return ctx.tools.schemas().map(schema => `- ${schema.name}`) +export function describeTools(ctx: Context, scope?: ScopeKey): string[] { + return ctx.tools.schemas(scope).map(schema => `- ${schema.name}`) } /** diff --git a/packages/cordis/tool-cordis/tests/cordis-lifecycle.spec.ts b/packages/cordis/tool-cordis/tests/cordis-lifecycle.spec.ts new file mode 100644 index 0000000000..b290ae998e --- /dev/null +++ b/packages/cordis/tool-cordis/tests/cordis-lifecycle.spec.ts @@ -0,0 +1,287 @@ +import { Context, CordisError, FiberState, type Fiber } from 'cordis' +import { describe, expect, it } from 'vitest' + +/** + * Direct regressions for the vendored Cordis ownership substrate used by + * tool-cordis's dynamic plugin tree and every other harness plugin. + */ + +describe('Cordis effect ownership', () => { + it('makes an effect visible to a reentrant owner restart and awaits setup plus cleanup', async () => { + const ctx = new Context() + const setupGate = Promise.withResolvers() + const cleanupGate = Promise.withResolvers() + const cleanupStarted = Promise.withResolvers() + let restarted!: Promise + let setupFinished = false + let cleanupFinished = false + + ctx.effect(async () => { + restarted = ctx.fiber.restart() + await setupGate.promise + setupFinished = true + return async () => { + cleanupStarted.resolve(undefined) + await cleanupGate.promise + cleanupFinished = true + } + }, 'reentrant-restart') + + let settled = false + void restarted.then(() => { settled = true }) + await Promise.resolve() + expect(settled).toBe(false) + + setupGate.resolve(undefined) + await cleanupStarted.promise + expect(setupFinished).toBe(true) + await Promise.resolve() + expect(settled).toBe(false) + + cleanupGate.resolve(undefined) + await restarted + expect(cleanupFinished).toBe(true) + expect(ctx.fiber.getEffects()).toEqual([]) + }) + + it('rolls back collected cleanup and its owner-list entry when setup throws synchronously', () => { + const ctx = new Context() + let cleanups = 0 + + expect(() => ctx.effect(function* () { + yield () => { cleanups += 1 } + throw new Error('setup failed') + }, 'throwing-setup')).toThrow('setup failed') + + expect(cleanups).toBe(1) + expect(ctx.fiber.getEffects()).toEqual([]) + }) + + it('makes a reentrant owner restart await asynchronous rollback after synchronous setup failure', async () => { + const ctx = new Context() + const cleanupGate = Promise.withResolvers() + const cleanupStarted = Promise.withResolvers() + let restarted!: Promise + + expect(() => ctx.effect(function* () { + yield async () => { + cleanupStarted.resolve(undefined) + await cleanupGate.promise + } + restarted = ctx.fiber.restart() + throw new Error('setup failed after restart') + }, 'reentrant-throw')).toThrow('setup failed after restart') + + await cleanupStarted.promise + let settled = false + void restarted.then(() => { settled = true }) + await Promise.resolve() + expect(settled).toBe(false) + + cleanupGate.resolve(undefined) + await restarted + expect(ctx.fiber.getEffects()).toEqual([]) + }) + + it('keeps ordinary teardown synchronous and the public disposer single-shot', () => { + const ctx = new Context() + let cleanups = 0 + const dispose = ctx.effect(() => () => { cleanups += 1 }, 'sync-effect') + + expect(dispose()).toBeUndefined() + expect(cleanups).toBe(1) + expect(dispose()).toBeUndefined() + expect(cleanups).toBe(1) + expect(ctx.fiber.getEffects()).toEqual([]) + }) + + it('rejects cleanup-time registration while a restart is unloading', async () => { + const ctx = new Context() + let registrationError: unknown + + ctx.effect(() => () => { + try { + ctx.effect(() => () => {}, 'too-late') + } catch (error) { + registrationError = error + } + }, 'restart-cleanup') + + await ctx.fiber.restart() + expect(registrationError).toBeInstanceOf(CordisError) + expect((registrationError as CordisError).code).toBe('INACTIVE_EFFECT') + expect(ctx.fiber.state).toBe(FiberState.ACTIVE) + expect(ctx.fiber.getEffects()).toEqual([]) + }) + + it('keeps effect registration legal while child fibers are PENDING and LOADING', async () => { + const ctx = new Context() + let pendingCleanup = false + let loadingCleanup = false + + ctx.on('internal/plugin', (fiber) => { + if (fiber.name !== 'state-probe' || fiber.uid === null) return + expect(fiber.state).toBe(FiberState.PENDING) + fiber.ctx.effect(() => () => { pendingCleanup = true }, 'pending-effect') + }) + + const fiber = await ctx.plugin({ + name: 'state-probe', + apply(inner) { + expect(inner.fiber.state).toBe(FiberState.LOADING) + inner.effect(() => () => { loadingCleanup = true }, 'loading-effect') + }, + }) + await fiber.dispose() + + expect(pendingCleanup).toBe(true) + expect(loadingCleanup).toBe(true) + }) + + it('resolves dependencies that internal/plugin adds before child activation', async () => { + const ctx = new Context() + ctx.provide('late-inject', {}) + let applyCalls = 0 + + ctx.on('internal/plugin', (fiber) => { + if (fiber.name !== 'loader-shaped' || fiber.uid === null) return + fiber.inject['late-inject'] = {} + }) + + const fiber = await ctx.plugin({ + name: 'loader-shaped', + apply() { + applyCalls += 1 + }, + }) + + expect(applyCalls).toBe(1) + expect(fiber.state).toBe(FiberState.ACTIVE) + }) +}) + +describe('Cordis child publication ownership', () => { + it('rolls back parent and runtime ownership when internal/plugin publication throws', () => { + const ctx = new Context() + const plugin = { name: 'publication-failure', apply() {} } + ctx.on('internal/plugin', (fiber) => { + if (fiber.name === plugin.name) throw new Error('publication failed') + }) + + expect(() => ctx.plugin(plugin)).toThrow('publication failed') + expect(ctx.registry.has(plugin)).toBe(false) + }) + + it('contains teardown notification failures so ownership cleanup and peers complete', async () => { + const ctx = new Context() + const errors: unknown[] = [] + ctx.logger.error = ((error: unknown) => { errors.push(error) }) as typeof ctx.logger.error + const observed: string[] = [] + ctx.on('internal/plugin', (fiber) => { + if (fiber.name === 'contained-teardown' && fiber.uid === null) { + throw new Error('broken teardown observer') + } + }) + ctx.on('internal/plugin', (fiber) => { + if (fiber.name === 'contained-teardown' && fiber.uid === null) observed.push('disposed') + }) + const child = await ctx.plugin({ name: 'contained-teardown', apply() {} }) + + await expect(child.dispose()).resolves.toBeUndefined() + expect(observed).toEqual(['disposed']) + expect(errors).toHaveLength(1) + expect(errors[0]).toEqual(expect.objectContaining({ message: 'broken teardown observer' })) + expect(child.uid).toBeNull() + }) + + it('makes a LOADING parent join child cleanup started before its unload snapshot', async () => { + const ctx = new Context() + const cleanupGate = Promise.withResolvers() + const cleanupStarted = Promise.withResolvers() + let ownerFiber!: Fiber + let ownerDisposal!: Promise + let childDisposal!: Promise + let childFiber!: Fiber + + ctx.on('internal/plugin', (fiber) => { + if (fiber.name !== 'loading-child' || fiber.uid === null) return + childFiber = fiber + fiber.ctx.effect(() => async () => { + cleanupStarted.resolve(undefined) + await cleanupGate.promise + }, 'loading-child-cleanup') + ownerDisposal = ownerFiber.dispose() + childDisposal = Promise.resolve(fiber.dispose()) + }) + + const ownerMount = ctx.plugin({ + name: 'loading-owner', + apply(inner) { + ownerFiber = inner.fiber + inner.plugin({ name: 'loading-child', apply() {} }) + }, + }) + + await cleanupStarted.promise + let ownerSettled = false + void ownerDisposal.then(() => { ownerSettled = true }) + await Promise.resolve() + expect(ownerSettled).toBe(false) + + cleanupGate.resolve(undefined) + await Promise.all([ownerDisposal, childDisposal, ownerMount]) + expect(childFiber.uid).toBeNull() + expect(ownerFiber.uid).toBeNull() + }) + + it('lets parent disposal during internal/plugin await the unpublished child to quiescence', async () => { + const ctx = new Context() + let ownerCtx!: Context + const owner = await ctx.plugin({ + name: 'owner', + apply(inner) { + ownerCtx = inner + }, + }) + + const cleanupGate = Promise.withResolvers() + const cleanupStarted = Promise.withResolvers() + let cleanupFinished = false + let childApplyCalls = 0 + let parentDisposal!: Promise + + ctx.on('internal/plugin', (fiber) => { + if (fiber.name !== 'child' || fiber.uid === null) return + expect(fiber.state).toBe(FiberState.PENDING) + fiber.ctx.effect(() => async () => { + cleanupStarted.resolve(undefined) + await cleanupGate.promise + cleanupFinished = true + }, 'pending-child-cleanup') + }) + ctx.on('internal/plugin', (fiber) => { + if (fiber.name !== 'child' || fiber.uid === null) return + parentDisposal = owner.dispose() + }) + + const child = ownerCtx.plugin({ + name: 'child', + apply() { + childApplyCalls += 1 + }, + }) + + await cleanupStarted.promise + let settled = false + void parentDisposal.then(() => { settled = true }) + await Promise.resolve() + expect(settled).toBe(false) + + cleanupGate.resolve(undefined) + await parentDisposal + expect(cleanupFinished).toBe(true) + expect(childApplyCalls).toBe(0) + expect(child.uid).toBeNull() + expect(child.state).toBe(FiberState.DISPOSED) + }) +}) diff --git a/packages/cordis/tool-cordis/tsconfig.json b/packages/cordis/tool-cordis/tsconfig.json index c4d4b6f656..cc9928d81f 100644 --- a/packages/cordis/tool-cordis/tsconfig.json +++ b/packages/cordis/tool-cordis/tsconfig.json @@ -20,6 +20,9 @@ { "path": "../../../vendor/schemastery" }, + { + "path": "../../core/scope" + }, { "path": "../../core/tools" } diff --git a/packages/core/README.md b/packages/core/README.md index eee8e3eed0..b132b04d49 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -1,16 +1,19 @@ # core/ — product API spine -The packages every harness build is assembled from: the session log, the system-prompt assembly, the tool registry, the agent vocabulary, and the one concrete loop that drives them. These are **product** packages — the stable surface plugins and consumers build against. +The session log, system-prompt assembly, tool registry, agent vocabulary, and concrete loop that form the harness's default control spine. These are **product** packages — the stable surface plugins and consumers build against. | Package | Role | ctx key | |---|---|---| +| `scope/` | Scoped-context registration primitive (scope tags, scope-filtered dispatch) | (library — no ctx key) | | `session/` | Event-sourced session log + in-memory store | `ctx.sessions` | | `system-prompt/` | Prompt-section + tool-schema assembly registry | `ctx.systemPrompt` | -| `tools/` | Tool registry + `tools/pre-execute`/`tools/post-execute` pipeline | `ctx.tools` | +| `tools/` | Scoped tool registry + pre-policy, guards, around-dispatch, post-policy, and final-result observation | `ctx.tools` | | `agent/` | Agent interface, registry, `agent/*` event vocabulary | `ctx.agents` | | `agent-loop/` | The concrete loop plugin: `ReactLoopAgent` + the loop driver | `ctx.agentLoop` | -| `agent-core/` | Bundle plugin: the providerless/executor-less/UI-less spine as code | (loads the spine) | +| `agent-core/` | Bundle plugin: the default executor-less/UI-less spine as code | (loads the spine) | + +`scope/` is the one non-service package here: a dependency-free library (`createScope`/`scopeOf`/`scopeTarget`) the registries and the loop build per-agent scoping on — it sits below `session/` and `system-prompt/` in the module graph precisely so they can consume it without a cycle. `agent-loop` is the one concrete implementation of the `agent` seam and lives here because it is the harness's default product loop; everything else in `core/` is interface/vocabulary. Plugins depend on the `agent` vocabulary, never on `agent-loop` directly, so the loop stays swappable. -`agent-core` is the composition counterpart: one bundle plugin that loads the whole providerless spine (`timer` + `llm` + sessions + system-prompt + tools + agents + invariants + `tool-bash` + `agent-loop`) and forwards `agent-loop`'s `agents` list as its own config. App packages (`ui/stdio-agent`, `ui/acp-agent`) consume it and add only a front door; a leaf adds the swappable backends plus any optional product tools it wants to expose. It lives in `core/` because it composes exclusively `core/` + interface packages and ships no provider, executor, or UI of its own. +`agent-core` is the composition counterpart: one bundle plugin that loads the control spine plus selected default capabilities (`timer` + `llm` + sessions + system-prompt + tools + agents + invariants + the local [skill family](../skill/README.md) + `tool-bash` + `agent-loop`) and forwards `agent-loop`'s `agents` list as its own config. App packages (`ui/stdio-agent`, `ui/acp-agent`) consume it and add only a front door; a leaf adds the swappable backends plus any optional product tools it wants to expose. It lives in `core/` because it composes the shared control spine while leaving executors, LLM adapters, alternate skill providers, and UI front doors outside the bundle. diff --git a/packages/core/agent-core/README.md b/packages/core/agent-core/README.md index 3f2ff08c0e..33cd0faddd 100644 --- a/packages/core/agent-core/README.md +++ b/packages/core/agent-core/README.md @@ -1,6 +1,6 @@ # @deepseek-ai/dsh-agent-core -The **providerless, executor-less, UI-less agent spine** as ONE Cordis bundle plugin. It loads the fixed set of services every harness agent needs and forwards the loop's `agents` list as its own config — so an app package composes a working agent by adding only a front door and the swappable backends. +The **default executor-less, UI-less agent spine** as ONE Cordis bundle plugin. It loads the fixed set of services every harness agent needs, including the local skill provider, and forwards the loop's `agents` list as its own config — so an app package composes a working agent by adding only a front door and the swappable backends. This is the package to read to see **the whole plugin tree at once** — the teaching role the inlined `echo-agent` `cordis.yml` used to play before the spine moved behind this bundle. @@ -13,10 +13,13 @@ This is the package to read to see **the whole plugin tree at once** — the tea @deepseek-ai/dsh-llm abstract LLM service + content-block vocabulary @deepseek-ai/dsh-session event-sourced session log + store @deepseek-ai/dsh-system-prompt prompt-section + tool-schema assembly -@deepseek-ai/dsh-tools tool registry + tools/pre-execute/post-execute +@deepseek-ai/dsh-tools registry + guarded pre/around/post/final-result pipeline +@deepseek-ai/dsh-skill skill provider registry +@deepseek-ai/dsh-skill-local local filesystem skill provider @deepseek-ai/dsh-agent agent registry + agent/* event vocabulary @deepseek-ai/dsh-invariants dev-mode event-contract assertions @deepseek-ai/dsh-tool-bash the model-facing bash/bash_output/bash_kill schemas +@deepseek-ai/dsh-tool-skill session-prefix skill catalog + model-facing loader schema @deepseek-ai/dsh-agent-loop THE concrete loop (gets the forwarded `agents`) (dsh-system-prompt gets the forwarded `persona`) ``` @@ -27,6 +30,7 @@ The spine is everything COMMON to every front door. The swappable and front-door - **the LLM adapter** — the bundle ships the abstract `llm` service; the leaf registers a concrete adapter on `ctx.llm` (`llm-deepseek`, `llm-pi-ai`, `llm-replay`). - **the bash executor** — the bundle ships `tool-bash` (the consumer schema); the leaf provides `ctx.bash` (`bash-local` or a sandboxed impl). +- **non-local skill providers** — the bundle ships the skill registry, the local filesystem provider, and the `skill` tool; deployments can add other providers such as embedded or remote catalogs as siblings. - **presentation + per-app infra** — the stdio UI / ACP bridge, a console logger, `hmr`. These form the coupled "front-door cluster" that the app packages ([`dsh-stdio-agent`](../../ui/stdio-agent/README.md), [`dsh-acp-agent`](../../ui/acp-agent/README.md)) bake in. `timer` is in the spine (common to both, stdout-silent); a console logger is NOT (it writes to stdout, which the ACP bridge reserves for JSON-RPC). This is the [interface/implementation/consumer seam](../../../docs/rfc/implemented/architecture/2026-06-13-capability-seams.md) raised to the composition level: the bundle owns the shared spine, the leaf owns the backends, the app package owns the front door. @@ -35,11 +39,11 @@ This is the [interface/implementation/consumer seam](../../../docs/rfc/implement ```ts import type { Config } from '@deepseek-ai/dsh-agent-core' -// { agents?, persona?, toolOrder? } — the schema is z.intersect([AgentLoop.Config, SystemPrompt.Config]), -// so validation and defaulting can never drift from the owners'. +// { agents?, persona?, toolOrder?, tools?, skills? } — the schema intersects the owner schemas, +// so validation and defaulting can never drift from the owners. ``` -The bundle FORWARDS each field to the child that owns it: `agents` to `agent-loop` (default `[]`), so each app supplies its own pre-created agents — a stdio app pre-creates a `main`; the ACP app pre-creates none (it creates agents on demand at `session/new`) — `persona` to `dsh-system-prompt` (default `''`), the deployment's persona section — and `toolOrder` to `dsh-system-prompt` (absent — lexicographic), the explicit model-facing tool order. Forwarding is exactly why the owners can live in the shared spine even though the apps disagree on what to configure. +The bundle FORWARDS each field to the child that owns it: `agents` to `agent-loop` (default `[]`), so each app supplies its own pre-created agents — a stdio app pre-creates a `main`; the ACP app pre-creates none (it creates agents on demand at `session/new`) — `persona` and `toolOrder` to `dsh-system-prompt`; `tools` to the tool registry for its presentation mode; and `skills.registry`, `skills.local`, and `skills.tool` to the skill registry, local provider, and model-facing consumer. Forwarding is exactly why the owners can live in the shared spine even though the apps disagree on what to configure. ## Why a code bundle, not a shared YAML include diff --git a/packages/core/agent-core/package.json b/packages/core/agent-core/package.json index 5b1eed413a..039a6ac505 100644 --- a/packages/core/agent-core/package.json +++ b/packages/core/agent-core/package.json @@ -1,6 +1,6 @@ { "name": "@deepseek-ai/dsh-agent-core", - "description": "The providerless/executor-less/UI-less agent spine as one Cordis bundle plugin (timer + llm + sessions + system-prompt + tools + agents + invariants + tool-bash + agent-loop)", + "description": "The default executor-less/UI-less agent spine as one Cordis bundle plugin (timer + llm + sessions + system-prompt + tools + skills + agents + invariants + tool-bash + tool-skill + agent-loop)", "version": "0.0.1", "private": true, "type": "module", @@ -28,8 +28,11 @@ "@deepseek-ai/dsh-invariants": "^0.0.1", "@deepseek-ai/dsh-llm": "^0.0.1", "@deepseek-ai/dsh-session": "^0.0.1", + "@deepseek-ai/dsh-skill": "^0.0.1", + "@deepseek-ai/dsh-skill-local": "^0.0.1", "@deepseek-ai/dsh-system-prompt": "^0.0.1", "@deepseek-ai/dsh-tool-bash": "^0.0.1", + "@deepseek-ai/dsh-tool-skill": "^0.0.1", "@deepseek-ai/dsh-tools": "^0.0.1", "cordis": "^4.0.0-rc.6" }, @@ -40,8 +43,11 @@ "@deepseek-ai/dsh-invariants": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", + "@deepseek-ai/dsh-skill": "workspace:^", + "@deepseek-ai/dsh-skill-local": "workspace:^", "@deepseek-ai/dsh-system-prompt": "workspace:^", "@deepseek-ai/dsh-tool-bash": "workspace:^", + "@deepseek-ai/dsh-tool-skill": "workspace:^", "@deepseek-ai/dsh-tools": "workspace:^", "cordis": "^4.0.0-rc.6" }, diff --git a/packages/core/agent-core/src/index.ts b/packages/core/agent-core/src/index.ts index a3221bdf8f..d12ef3bad5 100644 --- a/packages/core/agent-core/src/index.ts +++ b/packages/core/agent-core/src/index.ts @@ -1,10 +1,10 @@ /** - * The providerless, executor-less, UI-less agent spine as ONE bundle plugin. + * The default executor-less, UI-less agent spine as ONE bundle plugin. * * Loads the fixed set of services every harness agent needs — `timer`, the LLM * service, the session store, system-prompt assembly, the tool registry, the - * agent registry, the dev-mode invariants, the model-facing `bash` tool - * schemas, and the concrete `agent-loop` — and forwards the loop's `agents` + * skill registry plus local skill provider, the agent registry, the dev-mode + * invariants, the model-facing `bash` and `skill` tool schemas, and the concrete `agent-loop` — and forwards the loop's `agents` * list as its OWN config (default `[]`), so each app supplies its own * pre-created agents. * @@ -19,6 +19,9 @@ * (a console logger, `hmr`) — these are the coupled "front-door cluster" the * app packages ({@link @deepseek-ai/dsh-stdio-agent}, * {@link @deepseek-ai/dsh-acp-agent}) bake in, NOT the shared spine. + * - additional SKILL PROVIDERS; the bundle ships the local filesystem provider + * because local skills are default agent behavior, while embedded or remote + * providers remain deployment choices. * * This is the interface/implementation/consumer seam at the composition level: * the bundle owns the shared spine, the leaf owns the backends, the app package @@ -49,24 +52,37 @@ import LlmService from '@deepseek-ai/dsh-llm' import SessionStore from '@deepseek-ai/dsh-session' import SystemPrompt, { type Config as SystemPromptConfig } from '@deepseek-ai/dsh-system-prompt' import ToolRegistry, { type Config as ToolsConfig } from '@deepseek-ai/dsh-tools' +import SkillService, { type Config as SkillRegistryConfig } from '@deepseek-ai/dsh-skill' +import * as SkillLocal from '@deepseek-ai/dsh-skill-local' import AgentRegistry from '@deepseek-ai/dsh-agent' import * as invariants from '@deepseek-ai/dsh-invariants' import * as toolBash from '@deepseek-ai/dsh-tool-bash' +import * as toolSkill from '@deepseek-ai/dsh-tool-skill' import AgentLoop, { type Config as AgentLoopConfig } from '@deepseek-ai/dsh-agent-loop' export const name = 'agent-core' +/** Skill bundle config forwarded to the registry, local provider, and model-facing consumer. */ +export interface SkillConfig { + /** Registry-level discovery cache settings. */ + registry?: SkillRegistryConfig + /** Local filesystem skill provider settings. */ + local?: SkillLocal.Config + /** Model-facing skill catalog and tool settings. */ + tool?: toolSkill.Config +} + /** * Bundle config: each field forwarded verbatim to the child that owns it — * `agents` to the agent loop (an app that pre-creates no agents, like the ACP * bridge, simply omits it), `persona` and `toolOrder` to the system-prompt * plugin (the deployment's persona section and the explicit model-facing tool - * order), the `tools` object to the tool registry (its presentation `mode`). - * Every field is optional INPUT here because each owner's schema - * supplies the default (`[]` / `''` / absent — lexicographic / `native`); the - * schema is the INTERSECTION of the owners' own schemas (the registry's - * nested under its `tools` key), so validation and defaulting can never - * drift from them. + * order), the `tools` object to the tool registry (its presentation `mode`), + * and `skills` to the skill registry/local provider/tool consumer. Every field + * is optional INPUT here because each owner's schema supplies the default; + * the schema is the INTERSECTION of the owners' own schemas (with registry + * schemas nested under their bundle keys), so validation and defaulting can + * never drift from them. */ export interface Config { /** The agent-loop `agents` list (see dsh-agent-loop's `Config`). */ @@ -77,10 +93,23 @@ export interface Config { toolOrder?: SystemPromptConfig['toolOrder'] /** The tool registry's config — its presentation `mode` (see dsh-tools' `Config`). */ tools?: ToolsConfig + /** Skill registry, local provider, and model-facing consumer config. */ + skills?: SkillConfig } -/** Intersect the owners' schemas so validation + defaulting stay identical (the registry's nested under `tools`). */ -export const Config = z.intersect([AgentLoop.Config, SystemPrompt.Config, z.object({ tools: ToolRegistry.Config })]) as unknown as z +/** The skill config schema exported for app packages that forward `skills`. */ +export const SkillConfigSchema: z = z.object({ + registry: SkillService.Config, + local: SkillLocal.Config, + tool: toolSkill.Config, +}) + +/** Intersect the owners' schemas so validation + defaulting stay identical. */ +export const Config = z.intersect([ + AgentLoop.Config, + SystemPrompt.Config, + z.object({ tools: ToolRegistry.Config, skills: SkillConfigSchema }), +]) as unknown as z /** * Load the spine. Each `ctx.plugin(...)` mounts one child of the bundle fiber; @@ -106,8 +135,11 @@ export function apply(ctx: Context, config: Config): void { ...config.toolOrder !== undefined ? { toolOrder: config.toolOrder } : {}, }) ctx.plugin(ToolRegistry, config.tools ?? {}) + ctx.plugin(SkillService, config.skills?.registry ?? {}) + ctx.plugin(SkillLocal, config.skills?.local ?? {}) ctx.plugin(AgentRegistry) ctx.plugin(invariants) ctx.plugin(toolBash) + ctx.plugin(toolSkill, config.skills?.tool ?? {}) ctx.plugin(AgentLoop, { agents: config.agents ?? [] }) } diff --git a/packages/core/agent-core/tests/agent-core.spec.ts b/packages/core/agent-core/tests/agent-core.spec.ts index 818da77311..855bb28d49 100644 --- a/packages/core/agent-core/tests/agent-core.spec.ts +++ b/packages/core/agent-core/tests/agent-core.spec.ts @@ -1,13 +1,26 @@ import { describe, expect, it } from 'vitest' +import { mkdir, mkdtemp, writeFile } from 'node:fs/promises' +import { join } from 'node:path' +import { tmpdir } from 'node:os' import { Context } from 'cordis' import Loader from '@cordisjs/plugin-loader' import { TOOL_ORDER_REST } from '@deepseek-ai/dsh-system-prompt' import * as agentCore from '../src/index.ts' -import { AgentId } from '@deepseek-ai/dsh-agent' +import { AgentId, agentEvents, type Agent } from '@deepseek-ai/dsh-agent' +import type { Message } from '@deepseek-ai/dsh-llm' + +async function composePrefix(ctx: Context, cwd: string): Promise { + const agent = { session: { header: { cwd } } } as unknown as Agent + const empty: Message[] = [] + return await agentEvents(ctx, agent).waterfall( + 'agent/session-prefix', empty, new AbortController().signal, + () => Promise.resolve(empty), + ) +} /** * Unit coverage for the @deepseek-ai/dsh-agent-core bundle: mounting it brings - * up the whole providerless spine in one `ctx.plugin`, and the forwarded + * up the whole default spine in one `ctx.plugin`, and the forwarded * `agents` config reaches the loop (default `[]`, or a pre-created agent). * * The bundle is exercised through `ctx.plugin(agentCore, …)` — the NAMESPACE @@ -16,16 +29,54 @@ import { AgentId } from '@deepseek-ai/dsh-agent' * bin smokes; here we assert the composition + config forwarding. */ async function mount(config?: agentCore.Config): Promise { + const oldDshHome = process.env.DSH_HOME + const oldAgentsHome = process.env.DSH_AGENTS_HOME + process.env.DSH_HOME = await mkdtemp(join(tmpdir(), 'dsh-agent-core-home-')) + process.env.DSH_AGENTS_HOME = await mkdtemp(join(tmpdir(), 'dsh-agent-core-agents-')) const ctx = new Context() - await ctx.plugin(agentCore, config) - // The bundle mounts its children inside apply() (not awaited there); let their - // fibers settle so the spine services and any pre-created agent are ready. - await new Promise(resolve => setTimeout(resolve, 50)) - return ctx + try { + await ctx.plugin(agentCore, config) + // The bundle mounts its children inside apply() (not awaited there); let their + // fibers settle so the spine services and any pre-created agent are ready. + await new Promise(resolve => setTimeout(resolve, 50)) + return ctx + } finally { + if (oldDshHome === undefined) { + delete process.env.DSH_HOME + } else { + process.env.DSH_HOME = oldDshHome + } + if (oldAgentsHome === undefined) { + delete process.env.DSH_AGENTS_HOME + } else { + process.env.DSH_AGENTS_HOME = oldAgentsHome + } + } +} + +async function withIsolatedSkillHomes(run: () => Promise): Promise { + const oldDshHome = process.env.DSH_HOME + const oldAgentsHome = process.env.DSH_AGENTS_HOME + process.env.DSH_HOME = await mkdtemp(join(tmpdir(), 'dsh-agent-core-home-')) + process.env.DSH_AGENTS_HOME = await mkdtemp(join(tmpdir(), 'dsh-agent-core-agents-')) + try { + return await run() + } finally { + if (oldDshHome === undefined) { + delete process.env.DSH_HOME + } else { + process.env.DSH_HOME = oldDshHome + } + if (oldAgentsHome === undefined) { + delete process.env.DSH_AGENTS_HOME + } else { + process.env.DSH_AGENTS_HOME = oldAgentsHome + } + } } describe('dsh-agent-core bundle', () => { - it('brings up the full providerless spine', async () => { + it('brings up the full default spine', async () => { const ctx = await mount() // One service from each layer of the spine proves the children loaded. expect(ctx.get('timer')).toBeDefined() @@ -33,11 +84,22 @@ describe('dsh-agent-core bundle', () => { expect(ctx.get('sessions')).toBeDefined() expect(ctx.get('systemPrompt')).toBeDefined() expect(ctx.get('tools')).toBeDefined() + expect(ctx.get('skills')).toBeDefined() expect(ctx.get('agents')).toBeDefined() expect(ctx.get('agentLoop')).toBeDefined() await ctx.fiber.dispose() }) + it('includes the skill registry, local provider, and skill tool without builtin skills', async () => { + const ctx = await mount() + + expect(ctx.skills).toBeDefined() + expect(ctx.tools.schemas().map(tool => tool.name)).toContain('skill') + expect(await ctx.skills.list()).toEqual([]) + + await ctx.fiber.dispose() + }) + it('defaults the agents list to empty (no pre-created agents)', async () => { const ctx = await mount() expect(ctx.get('agents')?.get(AgentId('main'))).toBeUndefined() @@ -68,6 +130,40 @@ describe('dsh-agent-core bundle', () => { await ctx.fiber.dispose() }) + it('forwards skill config to the registry, local provider, and model-facing consumer', async () => { + const home = await mkdtemp(join(tmpdir(), 'dsh-agent-core-skill-home-')) + const agentsHome = await mkdtemp(join(tmpdir(), 'dsh-agent-core-skill-agents-')) + const custom = await mkdtemp(join(tmpdir(), 'dsh-agent-core-skill-custom-')) + await mkdir(custom, { recursive: true }) + await writeFile(join(custom, 'custom-skill.md'), '---\nname: custom-skill\ndescription: Custom skill\n---\n\nCustom body.\n') + const ctx = await mount({ + agents: [], + skills: { + registry: { collectCacheMaxEntries: 4 }, + local: { + dshHome: join(home, '.dsh'), + agentsHome: join(agentsHome, '.agents'), + customSkillDirs: [custom], + }, + tool: { catalogDescriptionMaxLength: 6 }, + }, + }) + expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['custom-skill']) + expect(JSON.stringify(await composePrefix(ctx, '/tmp'))).toContain('- `custom-skill`: Cus...') + await ctx.fiber.dispose() + }) + + it('uses the default skill config when apply is called directly without skills', async () => { + await withIsolatedSkillHomes(async () => { + const ctx = new Context() + agentCore.apply(ctx, { agents: [] }) + await new Promise(resolve => setTimeout(resolve, 50)) + expect(ctx.skills).toBeDefined() + expect(await ctx.skills.list()).toEqual([]) + await ctx.fiber.dispose() + }) + }) + it('forwards toolOrder to the system-prompt assembly', async () => { const ctx = await mount({ toolOrder: ['zulu', TOOL_ORDER_REST] }) // The bundle's own bash tools pend on the absent `ctx.bash` executor in @@ -81,7 +177,7 @@ describe('dsh-agent-core bundle', () => { }) } const assembly = await ctx.get('systemPrompt')!.assemble() - expect(assembly.tools.map(tool => tool.name)).toEqual(['zulu', 'alpha']) + expect(assembly.tools.map(tool => tool.name)).toEqual(['zulu', 'alpha', 'skill']) await ctx.fiber.dispose() }) diff --git a/packages/core/agent-core/tsconfig.json b/packages/core/agent-core/tsconfig.json index 91e5ec894e..4fd0a81e97 100644 --- a/packages/core/agent-core/tsconfig.json +++ b/packages/core/agent-core/tsconfig.json @@ -17,6 +17,9 @@ { "path": "../../../vendor/timer" }, + { + "path": "../../../vendor/schemastery" + }, { "path": "../../llm/llm" }, @@ -29,6 +32,15 @@ { "path": "../../core/tools" }, + { + "path": "../../skill/skill" + }, + { + "path": "../../skill/skill-local" + }, + { + "path": "../../skill/tool-skill" + }, { "path": "../../core/agent" }, diff --git a/packages/core/agent-loop/README.md b/packages/core/agent-loop/README.md index 2af8ad29f6..6832800d2e 100644 --- a/packages/core/agent-loop/README.md +++ b/packages/core/agent-loop/README.md @@ -8,14 +8,20 @@ This is the only package in the harness that contains concrete loop logic. Every ### Public API -- `ctx.agentLoop.create(id: string, options?: AgentOptions): ReactLoopAgent` — config-driven create: an agent on a fresh per-run session id `${id}-session-` (no cwd). Used for `cordis.yml`-configured agents. The per-run uuid avoids colliding with the on-disk log a prior run materialized once a durable persistence backend is loaded; each run is a new session (a deliberate demo simplification — a real resume-or-create policy is a TODO). Disposed with the calling fiber. +Creation and resume are one rollback-covered transaction: construct a private session, concrete agent, and scoped context; await optional setup; enter both registries; announce `session/created` then `agent/created`; emit `agent/session-start`; and only then start the driver. Setup receives the full scoped `Context` as trusted same-process composition code and must not drive the unpublished agent. Ordinary typed identity and option inputs are borrowed under their readonly contract, while seed events and session metadata are validated and snapshotted because they cross the durable session boundary. An optional `AbortSignal` cancels only load/setup/publication and is detached before the returned handle becomes visible. + +The caller fiber and the AgentLoop provider are co-owners. `AgentFactory.createAgent(ownerCtx, options)` and `resume(ownerCtx, options)` receive caller ownership explicitly, while the factory keeps its own dependency context for `sessions`/`llm`/`tools`/`systemPrompt`; this lets a caller inject only `agents` without shrinking the new agent's service surface. Caller unload, handle disposal, or provider unload converge on one memoized quiescence boundary. Provider shutdown waits both resource teardown and the public create/resume wrapper that observed deactivation, so no continuation can publish after dependencies disappear. + +IDs are caller-chosen and assumed globally unique; accidental UUID collisions are outside the supported model. Two concurrent operations with the same agent or session id may both prepare, but the final `enter()` calls arbitrate publication and every loser rolls its private resources back. Each detach is bound to the exact entered object, so a stale disposer cannot remove a later same-id replacement. A detach requested during a synchronous creation notification waits for that dispatch to unwind, preserving created/disposed pairing. Teardown runs stop and drain (including outstanding idle-injection flushes) → detach agent → detach session → unwind scope; IDs become reusable at detach even if private scope cleanup is still finishing. Ordinary non-vetoing `agent/*` notifications go through `agentEvents(ctx, agent)`, per-step assembly goes through `assembleContextFor(agent)`, and turn-end durability checkpoints go through `ctx.sessions.flush(session)`. + +- `ctx.agentLoop.create(id: string, options?: AgentOptions, meta?: { cwd?: string }): ReactLoopAgent` — synchronous no-setup create, used directly by programs and by `cordis.yml`-configured agents. It creates a fresh per-run session id `${id}-session-` with optional metadata; the uuid avoids colliding with a prior durable log. Each call is a new session (a deliberate demo simplification — a real resume-or-create policy is a TODO). Disposed with the calling fiber. `AgentLoop` also implements the `AgentFactory` seam and registers itself via `ctx.agents.setFactory(this)`, so plugins create/resume agents through `ctx.agents` (the interface): -- `ctx.agents.create({ agentId, sessionId, meta?, seed?, agentOptions? }): AgentHandle` — programmatic create on a caller-supplied `sessionId` (e.g. an ACP-generated id), NOT `${id}-session`; `meta` carries cwd/lineage/seed-boundary metadata and `seed` reconstructs a forked child prefix. Returns an [`AgentHandle`](../agent/README.md) — the owner disposes it to tear down exactly this agent (stop loop + await quiescence + unregister + remove session). -- `ctx.agents.resume({ agentId, resumeSessionId, agentOptions? }): Promise` — load a persisted session via `ctx.sessionPersistence` ([session persistence](../../../docs/rfc/implemented/architecture/2026-06-14-session-persistence.md)) and resume an agent on it. The live session id is the resumed id; turn numbering and derived history continue from the loaded log. Requires a session-persistence backend (NOT hard-injected — non-persistent demos still work; `resume` rejects with a clear error when persistence is absent). Returns an `AgentHandle`. +- `ctx.agents.create({ agentId, sessionId, meta?, seed?, agentOptions?, setup?, signal? }): Promise` — programmatic create on a caller-supplied `sessionId`, NOT `${id}-session`. It awaits the unpublished setup transaction before returning; `meta` carries cwd/lineage/seed-boundary metadata and `seed` reconstructs a forked child prefix after the session boundary validates and snapshots the durable values. `signal` applies only until this promise settles. The resolved [`AgentHandle`](../agent/README.md) owns exact teardown. +- `ctx.agents.resume({ agentId, resumeSessionId, agentOptions?, setup?, signal? }): Promise` — load a persisted session via `ctx.sessionPersistence` ([session persistence](../../../docs/rfc/implemented/architecture/2026-06-14-session-persistence.md)), reconstruct its history, then await setup against a fresh unpublished agent scope before rollback-covered publication. The live session id is the resumed id; turn numbering and derived history continue from the loaded log. Requires a session-persistence backend (NOT hard-injected — non-persistent demos still work; `resume` rejects with a clear error when persistence is absent). `signal` is creation-only. Returns an `AgentHandle`. -The config-driven `ctx.agentLoop.create()` path keeps its agent owned by the loop fiber (it discards the handle) — only the programmatic factory callers (the ACP bridge and in-process subagent backends) hold a handle and own per-agent teardown. +The config-driven `ctx.agentLoop.create()` path keeps its agent owned by the loop fiber (it discards the handle). For a programmatic agent, the handle holder is the only consumer-facing teardown capability; AgentLoop provider unload is the independent structural teardown edge, not another handle exposed to application code. ### Injected services @@ -28,20 +34,23 @@ interface Config { agents: Array<{ id: string // required model?: string + resumeSessionId?: string // load this persisted session instead of creating one + cwd?: string // optional workspace cwd for the fresh session }> } ``` -Agents listed in config are auto-created at startup. (There is no per-agent persona: the deployment persona is `dsh-system-prompt`'s own `persona` config, shared by every agent in the context.) The plugin registers the built-in `model`/`cwd` prompt variables on `ctx.systemPrompt`, resolved per step from the `assemble({ agent })` context — runtime facts of the agents THIS loop drives, unlike the `harness:identity`/`deployment:persona` sections, which live on `dsh-system-prompt` so they survive a swapped loop plugin. +Agents listed in config are auto-created at startup. `cwd` applies only to fresh config-created sessions; `resumeSessionId` keeps the persisted session header. Config agents have no per-agent persona field: they use `dsh-system-prompt`'s deployment default, while programmatic factory callers can register an agent-scoped `deployment:persona` shadow in `setup`. The plugin registers the built-in `model`/`cwd` prompt variables on `ctx.systemPrompt`, resolved per step from `assembleContextFor(agent)` — the helper couples the typed agent with its matching scope selector. These are runtime facts of the agents THIS loop drives, unlike the `harness:identity` and default `deployment:persona` sections, which live on `dsh-system-prompt` so they survive a swapped loop plugin. -### Classes +### Exported concrete class -- `ReactLoopAgent` — the concrete `Agent` implementation. Owns the inbox (`Inbox`), the per-step `AbortController`, and the loop driver. Everything observable happens through session events and the `agent/*` event taxonomy. -- `Inbox` — per-agent queued + steering FIFOs (`enqueue`, `steer`, `drainQueued`, `drainSteering`, `waitForQueued`). +- `ReactLoopAgent` — the concrete `Agent` implementation. Its inbox is a JavaScript native-private field, and one prepared session can be claimed by only one concrete driver. Everything observable happens through session events and the `agent/*` event taxonomy. + +`Inbox`, `runLoop`, and the instance-bound publication/start controls are package-internal. The package root does not export them, and the package exports map exposes no `./src/*` escape hatch; lifecycle owners create agents through `ctx.agents` rather than constructing or starting the driver internals. `ReactLoopAgent.send()` and running `steer()` materialize content plus resolved source once as detached, deeply frozen lossless JSON, then share that accepted record between `agent/queued` and the inbox; malformed data throws before either boundary. ### Loop lifecycle (`loop.ts`) -One invocation of `runLoop()` drives one agent for its whole lifetime: +The internal loop driver runs one agent for its whole lifetime: ``` create agent → emit agent/session-start(source) ⟵ once, before turn 1 @@ -54,7 +63,8 @@ forever: if every prompt blocked: 'turn/end'(rejected), no step ⟵ zero-step turn STEP loop: drain steering - assembly = systemPrompt.assemble({agent}) ⟵ renderPrompt(assembly) IS the full prompt + assembly = await systemPrompt.assemble(assembleContextFor(agent)) + ⟵ renderPrompt(assembly) IS the full prompt prefix ??= waterfall agent/session-prefix ⟵ once per instance (first step): frozen session prefix; on the header, never history await serial agent/pre-step(…, prefix) ⟵ surface mutation (compaction) outside the step; @@ -67,29 +77,33 @@ forever: message = waterfall agent/step-result session('assistant/message') each tool-call: session('tool/call') - → tools.execute() [waterfall tools/pre-execute → dispatch → tools/post-execute] + → tools.execute() [pre waterfall → monotonic guards → around dispatch → post waterfall → final notification] → session('tool/result') append buffered post-execute additionalContext as session('context/message')(s) drain steering → session('steering/message') cont = waterfall agent/turn-continuation → ContinuationDecision ({action:'continue', reason?} records reason as next-step steering) - if action==stop (and no pending steering): break + pending steering can override an ordinary stop + terminal = serial agent/turn-stop → ContinuationStop | undefined + (after ordinary decision/reason/steering folding) + if terminal stop, or ordinary action==stop with no pending steering: break session('turn/end') await session/flush - re-enqueue leftover steering as queued + terminal turn: discard steering added before/during close and flush; keep ordinary queued sends + ordinary turn: re-enqueue leftover steering as queued idle unless more queued ``` -Error containment: a throwing plugin ends the **turn**, never the loop. Dispose mid-turn emits `agent/status('disposed')` and ends with reason `disposed`. A step that hits the model's output-token ceiling makes the turn end `max-tokens` (the rule: any `max-tokens` step in the turn surfaces as `max-tokens`; `disposed`/`aborted`/`error` still take precedence) — distinct from a clean `completed` stop. +Error containment: a throwing plugin ends the **turn**, never the loop. A throwing `agent/turn-stop` policy likewise fails the turn closed. A successful terminal stop stays authoritative through `turn/end` and `session/flush`, preventing their listeners from resurrecting steering through the late fallback. Dispose mid-turn emits `agent/status('disposed')` and ends with reason `disposed`. A step that hits the model's output-token ceiling makes the turn end `max-tokens` (the rule: any `max-tokens` step in the turn surfaces as `max-tokens`; `disposed`/`aborted`/`error` still take precedence) — distinct from a clean `completed` stop. Cancellation: `agent.cancel()` is the single public stop primitive — it clears the queued + steering FIFOs, aborts the in-flight step, and drives a turn-scoped marker the driver checks at every point a turn could start or continue (right after the idle wait, after the `running` flip, before each step, and at the continuation gate) so a turn about to start is dropped. A cancelled turn ends `aborted`; a queued-but-not-started prompt never runs and cannot be batched into the cancelled turn. The marker is reset once per loop iteration, so a cancel governs exactly one turn and never leaks onto a later prompt. (The loop still aborts its own per-step `AbortController` directly on disposal and from `cancel()`; that controller is loop-internal, not a public verb.) ### What is NOT here Everything that goes beyond "call the model, run the tools, repeat" belongs to plugins listening on the event taxonomy: -- Hooks: `agent/session-start`, `agent/prompt-submit`, `agent/pre-step`, `agent/request`, `agent/session-prefix`, `agent/step-result`, `tools/pre-execute`, `tools/post-execute`, `agent/turn-continuation` +- Hooks and policy: the relevant `agent/*` checkpoints plus the guarded `tools/pre-execute` → `tools/execute` → `tools/post-execute` → `tools/result` pipeline; exact signatures and modes live in the [generated event catalog](../../../docs/cordis-catalog/events.md) - Compaction: `agent/pre-step` -- Sandbox, permission, plan mode: `tools/pre-execute` (deny/ask gate), `tools/post-execute` +- Sandbox, permission, plan mode: `tools/pre-execute` for extensible deny/ask, `tools.guard()` for monotonic owner policy, `tools/post-execute` for result decisions, and `tools/result` for final observation - Sub-agents: implemented outside the loop as `ctx.subagents` providers; in-process providers use `ctx.agents.create()` and owned `AgentHandle` teardown, while child streaming/progress and background/poll collection remain deferred. - Persistence: `session/event` + `session/flush` - UI: `session/event` (assistant token stream, boundaries, tool activity) + `agent/*` control events (`agent/status`, `agent/created`/`agent/disposed`) diff --git a/packages/core/agent-loop/package.json b/packages/core/agent-loop/package.json index 6e92adb6ab..5d180bd08c 100644 --- a/packages/core/agent-loop/package.json +++ b/packages/core/agent-loop/package.json @@ -11,7 +11,6 @@ "types": "./lib/types/index.d.ts", "default": "./lib/index.js" }, - "./src/*": "./src/*", "./package.json": "./package.json" }, "files": [ @@ -24,6 +23,7 @@ "peerDependencies": { "@deepseek-ai/dsh-agent": "^0.0.1", "@deepseek-ai/dsh-llm": "^0.0.1", + "@deepseek-ai/dsh-scope": "^0.0.1", "@deepseek-ai/dsh-session": "^0.0.1", "@deepseek-ai/dsh-session-persistence": "^0.0.1", "@deepseek-ai/dsh-system-prompt": "^0.0.1", @@ -37,6 +37,7 @@ "@deepseek-ai/dsh-agent": "workspace:^", "@deepseek-ai/dsh-invariants": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", + "@deepseek-ai/dsh-scope": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", "@deepseek-ai/dsh-session-persistence": "workspace:^", "@deepseek-ai/dsh-session-persistence-jsonl": "workspace:^", diff --git a/packages/core/agent-loop/src/agent.ts b/packages/core/agent-loop/src/agent.ts index 49de0f77c4..ff66f942be 100644 --- a/packages/core/agent-loop/src/agent.ts +++ b/packages/core/agent-loop/src/agent.ts @@ -7,13 +7,89 @@ */ import type { Context } from 'cordis' +import { agentEvents } from '@deepseek-ai/dsh-agent' import type { AgentId, AgentOptions, AgentStatus, SendOptions } from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' +import { deepFreeze } from '@deepseek-ai/dsh-llm' import type { ContentBlock, MessageSource } from '@deepseek-ai/dsh-llm' -import type { Session } from '@deepseek-ai/dsh-session' -import { Inbox } from './inbox.ts' +import { snapshotJsonValue, type Session } from '@deepseek-ai/dsh-session' +import { Inbox, type InboxMessage } from './inbox.ts' import { isTurnOpen, lastTurnNumber, runLoop } from './loop.ts' +/** Sessions already claimed by a concrete driver construction. */ +const claimedDriverSessions = new WeakSet() + +/** Module-private driver entry: its symbol is absent from the package surface. */ +const startDriver = Symbol('dsh.agent-loop.start-driver') + +/** Module-private quiescent stop, valid both before and after driver start. */ +const stopDriver = Symbol('dsh.agent-loop.stop-driver') + +/** Module-private context binding for the mutually referential agent scope. */ +const bindContext = Symbol('dsh.agent-loop.bind-context') + +/** Module-private publication marker. */ +const publishAgent = Symbol('dsh.agent-loop.publish-agent') + +/** Factory-owned controls that can operate only on the agent created with them. */ +export interface PreparedReactLoopAgent { + /** The unpublished concrete agent. */ + agent: ReactLoopAgent + /** Mark the agent public so teardown emits its status lifecycle. */ + markPublished(): void + /** Stop the prepared instance even when publication has not started its loop. */ + dispose(): Promise | void + /** + * Start its driver after publication and session-start notification. + * The returned disposer reaches quiescence for both the loop and every + * fire-and-forget idle-injection flush the agent started. + */ + startDriver(): () => Promise | void +} + +/** + * Construct one concrete agent together with unforgeable, instance-bound + * lifecycle controls. The package surface deliberately exposes neither source + * subpaths nor this helper: setup code may identify the concrete class, but it + * cannot publish or start the factory's unpublished instance. + * @param ctx - the agent-loop service context used for driving and events. + * @param id - the concrete agent identity. + * @param options - loop options for the agent. + * @param session - the prepared session the agent will own. + * @returns the agent and closures bound only to that exact instance. + */ +export function prepareReactLoopAgent( + ctx: Context, id: AgentId, options: AgentOptions, session: Session, +): PreparedReactLoopAgent { + if (claimedDriverSessions.has(session)) { + throw new Error(`session "${session.id}" already has a concrete agent driver`) + } + const agent = new ReactLoopAgent(ctx, id, options, session) + claimedDriverSessions.add(session) + const dispose = () => agent[stopDriver]() + return { + agent, + markPublished: () => { agent[publishAgent]() }, + dispose, + startDriver: () => { + agent[startDriver]() + return dispose + }, + } +} + +/** + * Install the concrete agent's scope context exactly once. Construction and + * scope minting are mutually referential (the scope key is the agent), so the + * factory performs this one post-construction binding before setup receives + * the unpublished agent. The module-private binding rejects a second bind. + * @param agent - the unpublished concrete agent to bind. + * @param ctx - its fully extended agent scope context. + */ +export function bindReactLoopAgentContext(agent: ReactLoopAgent, ctx: Context): void { + agent[bindContext](ctx) +} + /** * The concrete {@link Agent} implementation owned by the agent-loop plugin. * @@ -22,14 +98,31 @@ import { isTurnOpen, lastTurnNumber, runLoop } from './loop.ts' * the agent/* event taxonomy — plugins never need this class. */ export class ReactLoopAgent implements Agent { + /** Queued + steering FIFOs; native-private so callers cannot bypass the public driving verbs. */ + readonly #inbox = new Inbox() + /** - * The queued + steering FIFOs behind {@link send}/{@link steer}. Public so - * the driver loop can drain it; {@link cancel} clears it wholesale. + * The agent's scope context ({@link Agent.ctx}), wired by the factory right + * after the scope is minted — before the agent is registered, announced, or + * driven, so no consumer can observe it unset. Definite-assignment (`!`) + * expresses that two-phase construction: the agent object and its scope + * context are mutually referential (the scope is keyed BY this agent), so + * neither can exist strictly before the other. */ - readonly inbox = new Inbox() + private boundContext: Context | undefined + + /** The agent's scoped composition context, bound once by its factory. */ + get ctx(): Context { + if (this.boundContext === undefined) throw new Error(`agent "${this.id}" context is not bound`) + return this.boundContext + } private _status: AgentStatus = 'idle' private currentAbort: AbortController | undefined + /** Whether runLoop has been installed into {@link done}. */ + private driverStarted = false + /** Whether registry publication began and status disposal is externally visible. */ + private published = false /** * Turn-scoped cancel marker, set by {@link cancel} and read/cleared by the * driver loop (via the LoopHandle) at every point a turn could start or @@ -61,9 +154,15 @@ export class ReactLoopAgent implements Agent { * the `disposed` transition fires and leave the promise hanging. */ private idleWaiters: (() => void)[] = [] + /** + * Durability checkpoints started by idle {@link inject} calls. `inject()` is + * synchronous, so it cannot await them itself; the driver disposer drains + * this set before the lifecycle unregisters the agent or detaches its session. + */ + private pendingIdleFlushes = new Set>() constructor( - private ctx: Context, + private loopCtx: Context, public readonly id: AgentId, public readonly options: AgentOptions, public readonly session: Session, @@ -86,17 +185,13 @@ export class ReactLoopAgent implements Agent { // waiter (docs/defensive-patterns.md "contain callback exceptions" — a lifecycle await must // not hang on one bad listener). if (status !== 'running') this.settleIdleWaiters() - try { - this.ctx.emit('agent/status', this, status) - } catch (error: unknown) { - this.ctx.logger.warn(`agent "${this.id}": agent/status listener threw on ${status}: ${String(error)}`) - } + agentEvents(this.loopCtx, this).emit('agent/status', status) } /** * Resolve and clear all pending {@link whenIdle} waiters. Called on a * running→idle transition (from {@link setStatus}) and on disposal (from the - * {@link start} disposer, which chains `done` for true loop-exit quiescence). + * internal driver disposer, which chains `done` for true loop-exit quiescence). */ private settleIdleWaiters(): void { const waiters = this.idleWaiters @@ -108,23 +203,45 @@ export class ReactLoopAgent implements Agent { return options?.source ?? { kind: 'user' } } - send(content: ContentBlock[], options?: SendOptions): void { - if (this._status === 'disposed') throw new Error(`agent "${this.id}" is disposed`) + /** + * Accept one public send/steer payload as the exact detached record shared by + * the live notification and inbox. Lossless-JSON materialization reads every + * nested field once; deep freeze prevents an observer from rewriting queued + * work before the loop drains it. + */ + private acceptInboxMessage(content: ContentBlock[], options?: SendOptions): InboxMessage { const source = this.resolveSource(options) - this.inbox.enqueue({ content, source }) - this.ctx.emit('agent/queued', this, content, { source, steering: false }) + const accepted = snapshotJsonValue({ content, source }) + if (accepted === undefined) { + throw new TypeError('agent message content and source must be losslessly JSON-serializable') + } + return deepFreeze(accepted) + } + + /** Reject a driving operation once teardown has synchronously closed the agent. */ + private assertNotDisposed(): void { + if (this._status === 'disposed') throw new Error(`agent "${this.id}" is disposed`) + } + + send(content: ContentBlock[], options?: SendOptions): void { + this.assertNotDisposed() + const accepted = this.acceptInboxMessage(content, options) + this.#inbox.enqueue(accepted) + const info = { source: accepted.source, steering: false } as const + agentEvents(this.loopCtx, this).emit('agent/queued', accepted.content, info) } steer(content: ContentBlock[], options?: SendOptions): void { - if (this._status === 'disposed') throw new Error(`agent "${this.id}" is disposed`) + this.assertNotDisposed() if (this._status !== 'running') { this.send(content, options); return } - const source = this.resolveSource(options) - this.inbox.steer({ content, source }) - this.ctx.emit('agent/queued', this, content, { source, steering: true }) + const accepted = this.acceptInboxMessage(content, options) + this.#inbox.steer(accepted) + const info = { source: accepted.source, steering: true } as const + agentEvents(this.loopCtx, this).emit('agent/queued', accepted.content, info) } inject(content: ContentBlock[], options?: SendOptions): void { - if (this._status === 'disposed') throw new Error(`agent "${this.id}" is disposed`) + this.assertNotDisposed() const source = this.resolveSource(options) if (isTurnOpen(this.session)) { // A turn is open in the LOG (decided from the log, not agent status — @@ -136,61 +253,49 @@ export class ReactLoopAgent implements Agent { // No turn open: wrap the injection in a one-shot turn so every event stays // turn-enclosed (the durability/replay boundary is the turn). const turn = lastTurnNumber(this.session) + 1 - // Once turn/start enters the log, a turn/end is OWED no matter what — even - // if a throwing `session/event` listener escapes from the turn/start append - // (Session.append pushes the event BEFORE notifying listeners) or the - // context/message append throws (non-serializable content, throwing - // listener). The finally re-checks the log via isTurnOpen() and closes the - // turn if one was actually opened, so the log never carries a permanently - // open injection turn that would corrupt later turns/replay. (If the - // turn/start append throws BEFORE pushing — non-serializable trigger, which - // can't happen for our fixed trigger — no turn was opened and none is owed.) + // Once turn/start enters the log, a turn/end is owed even if the message + // append fails acceptance or pre-commit validation. The finally re-checks + // the log and closes only a turn that actually opened; post-commit observers + // are contained by Session and cannot create a false append failure. try { this.session.append('turn/start', { turn, trigger: { kind: 'injection', source } }) this.session.append('context/message', { content, source }, { surfaceOp: 'append' }) } finally { - // Close the turn if turn/start made it into the log. Contain a throwing - // turn/end listener: Session.append pushes before notifying, so a throw - // here still leaves turn/end in the log (the turn is balanced) — swallow - // it so it neither replaces the original exception nor skips the flush - // decision below. (It surfaces through the flush path is not needed; the - // turn-balance contract is what matters and it holds.) + // Close the turn if turn/start made it into the log. A pre-commit veto + // must escape rather than being mistaken for a committed turn/end. if (isTurnOpen(this.session)) { - try { - this.session.append('turn/end', { turn, reason: { kind: 'completed' } }) - } catch { - // turn/end is already in the log (pushed before the listener threw), - // so the turn is balanced; the throw is the listener's bug. - } + this.session.append('turn/end', { turn, reason: { kind: 'completed' } }) } - // Decide the durability checkpoint from the LOG, not a flag: a turn was - // recorded iff this turn's turn/start is logged (it may have been closed - // by a throwing-listener turn/end above, which still counts). A - // `turnRecorded` boolean set after append('turn/end') would be skipped by - // a throwing turn/end listener, losing the flush for a balanced in-memory - // turn (crash before the next turn/dispose would drop the idle injection). + // Decide the durability checkpoint from the log: an accepted one-shot + // turn must be flushed even when its message append was the failing step. const turnRecorded = this.session.events.some(e => e.type === 'turn/start' && e.data.turn === turn) // Checkpoint the one-shot turn for durability, exactly as the loop does at // every turn/end. The loop is NOT running (we are idle), so nothing else // will flush this turn. Fire-and-forget with error containment: inject() // is synchronous, and a persistence backend failing must not throw into // the caller (e.g. a tool-bash task-done callback). Disposal still drains - // independently, so a slow flush is safe. A flush failure is reported via - // agent/error (step 0 — the idle-injection convention, there is no real - // step) AND the logger, mirroring the loop's post-turn/end flush path so - // plugins monitoring agent/error see idle-injection persistence failures - // too. A throwing agent/error listener is contained. + // independently, so a slow flush is safe. The task is tracked until it + // settles: driver disposal awaits every pending idle-injection checkpoint + // before unregistering the agent or detaching the session. A flush failure + // is reported via agent/error (step 0 — the idle-injection convention, + // there is no real step) AND the logger, mirroring the loop's post-turn/end + // flush path so plugins monitoring agent/error see idle-injection + // persistence failures too. A throwing agent/error listener is contained. if (turnRecorded) { - void Promise.resolve(this.ctx.parallel('session/flush', this.session)).catch((error: unknown) => { - const err = error instanceof Error ? error : new Error(String(error)) - this.ctx.logger.warn(`agent "${this.id}": flush after idle injection failed: ${err.message}`) - try { - this.ctx.emit('agent/error', this, turn, 0, err) - } catch { - // contained: the failure is already logged; a throwing agent/error - // listener must not escape this fire-and-forget catch. - } + // Through the store's flush (the carrier owner), never a raw parallel. + const flush = this.loopCtx.sessions.flush(this.session).catch((error: unknown) => { + const rendered = renderThrown(error) + const err = error instanceof Error ? error : new Error(rendered) + this.loopCtx.logger.warn(`agent "${this.id}": flush after idle injection failed: ${rendered}`) + agentEvents(this.loopCtx, this).emit('agent/error', turn, 0, err) }) + this.pendingIdleFlushes.add(flush) + // Attach the same retirement callback to both settlement arms so even a + // logger failure in the catch above cannot become an unhandled rejection. + // Teardown uses allSettled for the same reason: a reporting failure must + // not strand ownership. + const retire = (): void => { this.pendingIdleFlushes.delete(flush) } + void flush.then(retire, retire) } } } @@ -205,7 +310,7 @@ export class ReactLoopAgent implements Agent { // the pre-step window (a send() queued but the loop not yet flipped to // running) has status `idle` with `hasQueued` true, and the marker exists // precisely to cover it. - if (this._status === 'running' || this.currentAbort !== undefined || this.inbox.hasQueued || this.inbox.hasSteering) { + if (this._status === 'running' || this.currentAbort !== undefined || this.#inbox.hasQueued || this.#inbox.hasSteering) { this.cancelRequested = true // Capture the resolved reason for the marker-only windows (pre-step / // continuation). The mid-step path reads it from abort.signal.reason @@ -216,7 +321,7 @@ export class ReactLoopAgent implements Agent { // cancelled turn's steering is not re-enqueued). Cleared directly even when // the loop is parked in waitForQueued — there is no turn to stop and nothing // left for the parked loop to run, so no wake is needed. - this.inbox.clear() + this.#inbox.clear() // Interrupt an in-flight step immediately (the running turn observes the // abort and ends `aborted`). The marker covers the windows where no step is // running (pre-step, continuation). @@ -234,12 +339,12 @@ export class ReactLoopAgent implements Agent { * fully ended) or chaining {@link done} on `disposed` (wait for the loop to * actually exit). Implements the {@link Agent.whenIdle} contract: a non-owner * quiescence-observation hook, distinct from teardown (a lifecycle owner stops - * and unregisters via `AgentHandle.dispose()`, which awaits {@link done} - * directly, not through this). + * and unregisters via `AgentHandle.dispose()`, whose driver boundary awaits + * both {@link done} and outstanding idle-injection flushes, not through this). */ whenIdle(): Promise { if (this._status === 'disposed') return this.done - if (this._status !== 'running' && !this.inbox.hasQueued) return Promise.resolve() + if (this._status !== 'running' && !this.#inbox.hasQueued) return Promise.resolve() // Register an internal waiter (resolved by settleIdleWaiters on the next // running→idle/disposed transition), NOT an effect-scoped `ctx.on` listener: // a concurrent fiber disposal runs this agent's listener disposers, which @@ -254,17 +359,27 @@ export class ReactLoopAgent implements Agent { }) } + /** Bind the mutually referential scope context once. */ + private [bindContext](ctx: Context): void { + if (this.boundContext !== undefined) throw new Error(`agent "${this.id}" context is already bound`) + this.boundContext = ctx + } + + /** Mark that public lifecycle publication began. */ + private [publishAgent](): void { + this.published = true + } + /** - * Start the driver loop. Returns a disposer: calling it sets status to - * `disposed`, emits `agent/status('disposed')`, resolves the disposed - * promise (unblocking the idle wait), releases any `whenIdle` waiters, and - * aborts the current request if any. The returned `agent.done` promise - * resolves once the loop exits. - * @returns the disposer — idempotent and infallible (it runs inside the - * fiber's LIFO disposal chain, where a throw would skip later disposers). + * Start the driver loop. The prepared controller already owns its stable + * disposer, so teardown can mark the agent disposed even in the narrow + * publication window before this method runs. */ - start(): () => void { - this.done = runLoop(this.ctx, this, { + [startDriver](): void { + if (this._status === 'disposed') return + this.driverStarted = true + this.done = runLoop(this.loopCtx, this, { + inbox: this.#inbox, setStatus: (status) => { this.setStatus(status) }, setAbort: controller => void (this.currentAbort = controller), disposed: this.disposed, @@ -280,11 +395,15 @@ export class ReactLoopAgent implements Agent { // that would resolve a freshly-queued prompt as cancelled. settleIdle: () => { this.settleIdleWaiters() }, }) - // The disposer must be infallible: it runs inside the fiber's LIFO - // disposal chain, where a throw would skip later disposers (e.g. the - // registry unregistration) and leave `done` pending forever. - return () => { - if (this._status === 'disposed') return + } + + /** + * Quiescent stop shared by pre-start rollback and live teardown. It marks the + * agent disposed synchronously, contains an unexpected loop rejection, and + * drains every idle-injection flush before resolving. + */ + private [stopDriver](): Promise | void { + if (this._status !== 'disposed') { this._status = 'disposed' this.resolveDisposed() // Release whenIdle waiters BEFORE the (guarded) event emit — they are @@ -292,14 +411,39 @@ export class ReactLoopAgent implements Agent { // waiter chains `done`, so it resolves only once the loop actually exits. this.settleIdleWaiters() this.currentAbort?.abort('disposed') - // setStatus refuses transitions out of 'disposed', so emit directly — - // 'disposed' is part of the agent/status contract. Guarded: a throwing - // listener must not break the disposal chain. - try { - this.ctx.emit('agent/status', this, 'disposed') - } catch { - // listener error during disposal — nothing safe left to do with it + // An unpublished rollback has no public status lifecycle to announce. + // Once publication begins, disposed is part of the agent/status contract. + if (this.published) { + agentEvents(this.loopCtx, this).emit('agent/status', 'disposed') } } + // Before runLoop starts there is normally nothing asynchronous to drain; + // keep publication rollback synchronous so create() cannot throw while its + // session/agent entries are still briefly live. A session-start listener + // may have called inject(), however, so preserve + // its durability checkpoint as a real quiescence boundary. + if (!this.driverStarted && this.pendingIdleFlushes.size === 0) return + return this.drainDriver() + } + + /** Await the loop (when started) and every outstanding idle flush. */ + private async drainDriver(): Promise { + // An unexpected driver rejection must not skip registry/session/scope + // cleanup. The normal loop contains turn failures itself; allSettled is the + // final lifecycle backstop for anything outside those boundaries. + await Promise.allSettled([this.done]) + // No new inject() can start after the synchronous disposed transition. + // Loop because settled tasks retire themselves in promise reactions that + // may run beside this continuation; either the set is empty or this waits + // the exact remaining quiescence boundary. allSettled keeps a failure in + // error reporting from skipping registry/session/scope disposers. + while (this.pendingIdleFlushes.size > 0) { + await Promise.allSettled([...this.pendingIdleFlushes]) + } } } + +/** Render an ordinary thrown value for the error event and log. */ +function renderThrown(value: unknown): string { + return value instanceof Error ? value.message : String(value) +} diff --git a/packages/core/agent-loop/src/index.ts b/packages/core/agent-loop/src/index.ts index e7e0e562d9..4ad4779176 100644 --- a/packages/core/agent-loop/src/index.ts +++ b/packages/core/agent-loop/src/index.ts @@ -1,27 +1,319 @@ /** - * THE concrete agent plugin: creates ReactLoopAgents, runs their loops, and - * registers them in ctx.agents. Deliberately thin — every behavior beyond - * "call the model, run the tools, repeat" belongs to plugins on the event - * taxonomy. + * Concrete agent-loop plugin: creates scoped ReactLoopAgents, publishes them + * through the agent/session registries, and owns their ordered teardown. * * @module @deepseek-ai/dsh-agent-loop */ -import { Context, Service } from 'cordis' +import { Context, FiberState, Service } from 'cordis' import { randomUUID } from 'node:crypto' import z from 'schemastery' -import type { AgentFactory, AgentHandle, AgentId, AgentOptions, CreateAgentOptions, ResumeAgentOptions, SessionStartSource } from '@deepseek-ai/dsh-agent' +import { createScope } from '@deepseek-ai/dsh-scope' +import type { Scope } from '@deepseek-ai/dsh-scope' +import { agentEvents } from '@deepseek-ai/dsh-agent' +import type { + AgentFactory, + AgentHandle, + AgentId, + AgentOptions, + CreateAgentOptions, + ResumeAgentOptions, + SessionStartSource, +} from '@deepseek-ai/dsh-agent' import type {} from '@deepseek-ai/dsh-llm' import { SessionId } from '@deepseek-ai/dsh-session' -import type { Session } from '@deepseek-ai/dsh-session' +import type { Session, SessionHeader } from '@deepseek-ai/dsh-session' import type {} from '@deepseek-ai/dsh-system-prompt' import type {} from '@deepseek-ai/dsh-tools' import type { SessionPersistence } from '@deepseek-ai/dsh-session-persistence' -import { ReactLoopAgent } from './agent.ts' +import { + bindReactLoopAgentContext, + prepareReactLoopAgent, + ReactLoopAgent, +} from './agent.ts' +import type { PreparedReactLoopAgent } from './agent.ts' export { ReactLoopAgent } from './agent.ts' -export { Inbox, type InboxMessage } from './inbox.ts' -export { runLoop } from './loop.ts' + +/** Fiber states that cannot own or serve a new lifecycle. */ +const INACTIVE_STATES: ReadonlySet = new Set([ + FiberState.UNLOADING, + FiberState.DISPOSED, + FiberState.FAILED, +]) + +/** Factory-level ownership of every preparing or live transaction. */ +class FactoryOwnership { + private accepting = true + private transactions = new Set() + + constructor(private readonly fiber: Context['fiber']) {} + + isActive(): boolean { + return this.accepting && !INACTIVE_STATES.has(this.fiber.state) + } + + track(transaction: AgentCreationTransaction): () => void { + this.transactions.add(transaction) + return () => { this.transactions.delete(transaction) } + } + + async dispose(): Promise { + this.accepting = false + const reason = new Error('agent loop is not active') + await Promise.all( + [...this.transactions].map(transaction => transaction.disposeForFactory(reason)), + ) + } +} + +/** Build the public cancellation error while preserving a caller-supplied cause. */ +function signalAbortError(id: AgentId, signal: AbortSignal): Error { + if (signal.reason instanceof Error) return signal.reason + return new Error(`agent "${id}" creation aborted`, { cause: signal.reason }) +} + +/** + * One create/resume transaction from caller ownership through unpublished + * setup, rollback-covered publication, and final quiescent teardown. + * + * The class deliberately owns the state machine in one place. Registries only + * arbitrate identity at their final `enter()` calls; before that point every + * resource is private to this transaction. + */ +class AgentCreationTransaction { + private active = true + private failure: Error | undefined + private readonly deactivation = Promise.withResolvers() + private readonly publication = Promise.withResolvers() + private readonly torndown = Promise.withResolvers() + private readonly wrapperCompletion = Promise.withResolvers() + private preparing: Promise | undefined + private driver: PreparedReactLoopAgent | undefined + private scope: Scope | undefined + private session: Session | undefined + private lifecycleDispose: (() => Promise | void) | undefined + private detachSession: (() => void) | undefined + private detachAgent: (() => void) | undefined + private publishing = false + private cleanupTask: Promise | undefined + private ownerFollowing = true + private readonly ownerDispose: () => Promise | void + private readonly untrackFactory: () => void + private readonly abortListener: (() => void) | undefined + readonly ownerAgent: Context['agent'] + readonly ownerFiber: Context['fiber'] + + constructor( + private readonly loopCtx: Context, + private readonly ownerCtx: Context, + private readonly ownership: FactoryOwnership, + readonly id: AgentId, + signal?: AbortSignal, + ) { + ownerCtx.fiber.assertActive() + this.ownerAgent = ownerCtx.agent + this.ownerFiber = ownerCtx.fiber + if (!ownership.isActive()) throw new Error('agent loop is not active') + this.ownerDispose = ownerCtx.effect(() => () => { + if (!this.ownerFollowing) return + return this.dispose(new Error(`agent "${id}" setup aborted: owner disposed during setup`)) + }, `agentLoop.owner(${id})`) + this.untrackFactory = ownership.track(this) + if (signal === undefined) { + this.abortListener = undefined + } else { + this.abortListener = () => { + /* v8 ignore next 3 -- transaction teardown contains callback/driver failures; rejection is a future-drift backstop. */ + void this.dispose(signalAbortError(id, signal)).catch((error: unknown) => { + this.loopCtx.logger.error(error) + }) + } + signal.addEventListener('abort', this.abortListener, { once: true }) + if (signal.aborted) this.deactivate(signalAbortError(id, signal)) + } + this.signal = signal + } + + private readonly signal: AbortSignal | undefined + + /** Whether caller, provider, and optional parent-agent ownership remain live. */ + isActive(): boolean { + return this.active + && this.ownership.isActive() + && this.ownerFiber.uid !== null + && !INACTIVE_STATES.has(this.ownerFiber.state) + && this.ownerAgent?.status !== 'disposed' + } + + /** Fail synchronously at every real lifecycle boundary after deactivation. */ + assertActive(): void { + if (this.isActive()) return + if (!this.ownership.isActive()) throw new Error('agent loop is not active') + throw this.failure ?? new Error(`agent "${this.id}" setup aborted: owner disposed during setup`) + } + + /** Race an external async operation against structural/signal deactivation. */ + async waitFor(operation: PromiseLike | T): Promise { + this.assertActive() + return await Promise.race([ + Promise.resolve(operation), + this.deactivation.promise.then(() => { + /* v8 ignore next -- deactivate() assigns failure before resolving deactivation. */ + throw this.failure ?? new Error(`agent "${this.id}" creation deactivated`) + }), + ]) + } + + /** Construct the driver and scope, then install their complete ordered lifecycle. */ + prepare(options: AgentOptions, session: Session): ReactLoopAgent { + this.assertActive() + const gate = Promise.withResolvers() + this.preparing = gate.promise + try { + this.session = session + const driver = prepareReactLoopAgent(this.loopCtx, this.id, options, session) + this.driver = driver + const agent = driver.agent + const scope = createScope(this.loopCtx, agent) + this.scope = scope + bindReactLoopAgentContext(agent, scope.ctx.extend({ agent })) + this.installLifecycle(scope, driver) + this.assertActive() + return agent + } catch (error: unknown) { + if (!this.isActive() && error instanceof Error && /inactive context/.test(error.message)) { + throw this.failure ?? this.disposalReason() + } + throw error + } finally { + gate.resolve() + this.preparing = undefined + } + } + + /** Register the exact scope disposer inside the ordered transaction effect. */ + private installLifecycle(scope: Scope, driver: PreparedReactLoopAgent): void { + this.lifecycleDispose = this.ownerCtx.effect(function* (this: AgentCreationTransaction) { + // First yielded, disposed last. + yield () => { this.finish() } + yield scope.rawDispose + yield () => { + this.detachSession?.() + this.detachSession = undefined + } + yield () => { + this.detachAgent?.() + this.detachAgent = undefined + } + // Last yielded, disposed first. + yield () => { + this.deactivate(this.disposalReason()) + if (this.publishing) { + return this.publication.promise.then(() => driver.dispose()) + } + return driver.dispose() + } + }.bind(this), `agentLoop.lifecycle(${this.id})`) + } + + /** Publish the exact prepared objects and start the driver. */ + publish(source: SessionStartSource): AgentHandle { + this.assertActive() + const driver = this.driver + /* v8 ignore next -- publish() is private and every caller invokes prepare() first. */ + if (driver === undefined) throw new Error(`agent "${this.id}" is not prepared`) + const agent = driver.agent + const session = this.session + /* v8 ignore next -- prepare() assigns the session before it can produce the driver above. */ + if (session === undefined) throw new Error(`agent "${this.id}" has no prepared session`) + this.publishing = true + try { + this.detachSession = agent.ctx.sessions.enter(session) + this.detachAgent = this.loopCtx.agents.enter(agent) + + agent.ctx.sessions.announce(session) + this.assertActive() + this.loopCtx.agents.announce(agent) + this.assertActive() + + driver.markPublished() + agentEvents(this.loopCtx, agent).emit('agent/session-start', source) + this.assertActive() + driver.startDriver() + return { agent, dispose: () => this.dispose() } + } finally { + this.publishing = false + this.publication.resolve() + } + } + + /** Mark the transaction inactive exactly once and wake load/setup races. */ + private deactivate(reason: Error): void { + if (!this.active) return + this.active = false + this.failure = reason + this.deactivation.resolve() + } + + /** Choose the structural cause when an owner/factory effect starts teardown first. */ + private disposalReason(): Error { + if (this.failure !== undefined) return this.failure + if (!this.ownership.isActive()) return new Error('agent loop is not active') + if (this.ownerFiber.uid === null || INACTIVE_STATES.has(this.ownerFiber.state) || this.ownerAgent?.status === 'disposed') { + return new Error(`agent "${this.id}" setup aborted: owner disposed during setup`) + } + return new Error(`agent "${this.id}" lifecycle disposed`) + } + + /** Complete ownership bookkeeping after every resource reached quiescence. */ + private finish(): void { + this.untrackFactory() + this.ownerFollowing = false + void this.ownerDispose() + this.torndown.resolve() + } + + /** + * Deactivate and quiesce this transaction. The promise is memoized because + * Cordis effect disposers are single-shot while handles promise shared + * quiescence to every racing owner. + */ + dispose(reason = new Error(`agent "${this.id}" lifecycle disposed`)): Promise { + this.deactivate(reason) + return (this.cleanupTask ??= (async () => { + if (this.preparing !== undefined) await this.preparing + if (this.lifecycleDispose !== undefined) { + await this.lifecycleDispose() + await this.torndown.promise + return + } + try { + await this.driver?.dispose() + } finally { + try { + await this.scope?.dispose() + } finally { + this.finish() + } + } + })()) + } + + /** Mark the public create/resume continuation settled and detach its creation-only signal. */ + finishWrapper(): void { + if (this.signal !== undefined && this.abortListener !== undefined) { + this.signal.removeEventListener('abort', this.abortListener) + } + this.wrapperCompletion.resolve() + } + + /** Factory shutdown joins both resource teardown and the public wrapper's deactivation continuation. */ + async disposeForFactory(reason: Error): Promise { + await this.dispose(reason) + await this.wrapperCompletion.promise + } +} declare module 'cordis' { interface Context { @@ -29,320 +321,172 @@ declare module 'cordis' { } } -/** - * Plugin config: the agents to create — or resume, via `resumeSessionId` — - * declaratively at startup, so a cordis.yml deployment needs no code. - */ +/** Plugin configuration for declarative startup agents. */ export interface Config { - /** Agents created from configuration at startup. */ + /** Agents created or resumed at plugin startup. */ agents: (AgentOptions & { - /** Agent id to register under; also seeds the fresh per-run session id (`${id}-session-`). */ + /** Registry identity for the live agent. */ id: AgentId - /** - * If set, the config agent RESUMES this persisted session id instead of - * starting a fresh `${id}-session-`. Sourced from an env var in - * cordis.yml (`resumeSessionId: !!js process.env.RESUME_SESSION_ID`), so a - * demo can continue a prior conversation without code changes. Requires a - * `dsh-session-persistence` backend; the resume is deferred until that - * service is available (via `ctx.inject`) and the loaded session's events - * seed the live session so history continues. - * - * The schema accepts a plain string at runtime (cordis.yml values are - * untyped); the brand is compile-time only — the config format is the - * boundary where an id enters, so the TYPE declares the brand here. - */ + /** Optional workspace for a fresh session. */ + cwd?: string + /** Persisted session to resume instead of creating a fresh session. */ resumeSessionId?: SessionId })[] } -/** - * The agent-loop plugin (`ctx.agentLoop`): creates {@link ReactLoopAgent}s, runs - * their loops, and registers them in `ctx.agents`. Also implements the - * {@link AgentFactory} seam, so plugins create/resume agents through - * `ctx.agents` (the interface) without depending on this concrete package. - * - * The loop itself is deliberately thin — every behavior beyond "call the - * model, run the tools, repeat" belongs to plugins listening on the event - * taxonomy declared in @deepseek-ai/dsh-agent. - */ +/** Concrete ReactLoopAgent factory and driver service. */ export class AgentLoop extends Service implements AgentFactory { static inject = ['agents', 'sessions', 'llm', 'tools', 'systemPrompt'] - // The schema validates plain strings (cordis.yml config values are untyped at - // runtime); the {@link Config} TYPE declares the branded `id`/`resumeSessionId` - // because the config format is the boundary where an id enters. The brand is a - // zero-cost compile-time cast, so the runtime schema stays string-based and we - // assert the branded view once here — the single schema boundary. + /** Runtime schema for declarative agents. */ static Config = z.object({ agents: z.array(z.object({ id: z.string().required(), model: z.string(), + cwd: z.string(), resumeSessionId: z.string(), })).default([]), }) as unknown as z + private readonly ownership: FactoryOwnership + /** Plain holder prevents Cordis from re-tracing the factory's dependency context through a caller shadow. */ + private readonly runtime: { ctx: Context } + constructor(ctx: Context, public config: Config) { super(ctx, 'agentLoop') - // Provide the agent-creation factory to the registry (effect-scoped: the - // slot is cleared on dispose). - ctx.effect(() => this.ctx.agents.setFactory(this), 'agentLoop.setFactory()') - // The prompt variables the shipped loop provides, registered once. The - // sections themselves (`harness:identity`, `deployment:persona`) belong to - // dsh-system-prompt — they must survive a swapped loop plugin — but - // `{{model}}`/`{{cwd}}` are runtime facts of the agents THIS loop drives: - // it assembles with `{ agent }` each step (loop.ts), and the variables - // project the agent's configured model and its session workspace from that - // context. A provider returns undefined when the fact is absent - // (renderPrompt then rejects a persona that claims it — fail loud). + this.ownership = new FactoryOwnership(ctx.fiber) + this.runtime = { ctx } + ctx.effect(() => () => this.ownership.dispose(), 'agentLoop.transactions()') + ctx.effect(() => ctx.agents.setFactory(this), 'agentLoop.setFactory()') ctx.systemPrompt.variable('model', context => context.agent?.options.model) ctx.systemPrompt.variable('cwd', context => context.agent?.session.header.cwd) - for (const { id, resumeSessionId, ...options } of config.agents) { - if (resumeSessionId !== undefined && resumeSessionId !== '') { - // Resume a prior session instead of starting fresh. resume() needs - // `ctx.sessionPersistence`, which may load AFTER this plugin (cordis.yml - // lists the backend later). `ctx.inject(['sessionPersistence'], cb)` - // runs `cb` with a child ctx once the service exists; the child reads - // the persistence and hands it to resumeWith (which uses this.ctx — the - // parent — for sessions/registry, all in AgentLoop's static inject). A - // failed resume is contained + logged: startup must not crash. - ctx.effect(() => { - const fiber = this.ctx.inject(['sessionPersistence'], (childCtx: Context) => { - void this.resumeWith(childCtx.sessionPersistence, { agentId: id, resumeSessionId, agentOptions: options }) - .catch((error: unknown) => { - this.ctx.logger.warn(`agent "${id}": config-driven resume of "${resumeSessionId}" failed: ${String(error)}`) - }) - }) - return () => void fiber.dispose() - }, `agentLoop.resume(${id})`) - } else { - this.create(id, options) + + for (const { id, cwd, resumeSessionId, ...options } of config.agents) { + if (resumeSessionId === undefined || resumeSessionId === '') { + this.create(id, options, cwd === undefined ? {} : { cwd }) + continue } + ctx.effect(() => { + const fiber = ctx.inject(['sessionPersistence'], (childCtx: Context) => { + void this.resumeWith(ctx, childCtx.sessionPersistence, { + agentId: id, + resumeSessionId, + agentOptions: options, + }).catch((error: unknown) => { + ctx.logger.warn(`agent "${id}": config-driven resume of "${resumeSessionId}" failed: ${String(error)}`) + }) + }) + return fiber.dispose + }, `agentLoop.resume(${id})`) } } /** - * Config-driven create: an agent on a FRESH, non-colliding session id per run - * (`${id}-session-`, no cwd). Used for `cordis.yml`-configured agents - * and as the shared core for the programmatic factory {@link createAgent}. - * - * Why a per-run id, not a fixed `${id}-session`: once a durable persistence - * backend is loaded, a fixed id collides on the second run — the backend - * refuses to re-create an id whose log already exists on disk (the SessionId - * is the identity). A fresh id means each run is a new session. - * - * TODO(demo): each run starting a brand-new session is fine for demos but is - * NOT real conversation continuity. A production config-driven agent needs a - * deliberate resume-or-create policy (resume the prior session if one exists, - * else start fresh) or an explicit caller-chosen session id — revisit when the - * UI/ACP path owns session selection. - * @param id - the agent id; also seeds the generated session id. - * @param options - loop options (model, limits, …); defaults applied per option. - * @returns the running agent, owned by the calling fiber (no handle). + * Create an agent on a fresh per-run session, owned by the accessing fiber. + * Constructor-driven config calls use the loop fiber itself. + * @param id - agent registry id. + * @param options - concrete loop options. + * @param meta - optional fresh-session workspace metadata. + * @returns the published running agent. */ - create(id: AgentId, options: AgentOptions = {}): ReactLoopAgent { - this.assertAgentIdFree(id) - // Config/programmatic path: prepare the session and let start() fold its - // lifecycle into the agent's composite effect (so a fiber unload tears the - // session + agent down as one ordered chain, capturing the loop's closing - // flush). The whole effect is owned by THIS fiber; no AgentHandle is needed. - const session = this.ctx.sessions.prepare(SessionId(`${id}-session-${randomUUID()}`), { meta: {} }) - const { agent } = this.start(id, options, session, 'startup') - return agent + create(id: AgentId, options: AgentOptions = {}, meta: Pick = {}): ReactLoopAgent { + const loopCtx = this.runtime.ctx + const transaction = new AgentCreationTransaction(loopCtx, this.ctx, this.ownership, id) + try { + const sessionId = SessionId(`${id}-session-${randomUUID()}`) + const session = loopCtx.sessions.prepare(sessionId, { meta }) + const agent = transaction.prepare(options, session) + transaction.publish('startup') + return agent + } catch (error: unknown) { + void transaction.dispose(error instanceof Error ? error : new Error(String(error))) + throw error + } finally { + transaction.finishWrapper() + } } /** - * Programmatic factory create ({@link AgentFactory}): an agent on a - * caller-supplied `sessionId` (NOT `${id}-session`), with optional session - * metadata (validated `cwd`, lineage) and an optional `seed` event prefix. The - * ACP bridge uses this so the client-generated session id becomes the - * live/persisted session id; the in-process FORK subagent backend passes a - * `seed` (a balanced completed-turn prefix of the parent's log) so the child - * starts with the parent's context. Returns an {@link AgentHandle} the owner - * disposes to tear down exactly this agent. - * @param options - agent id, caller-supplied session id, optional seed/meta, - * and agent options. - * @returns the handle whose dispose tears down exactly this agent. + * Create an owned agent on a caller-supplied session id. + * @param ownerCtx - caller context that structurally owns the transaction. + * @param options - identities, session seed/metadata, loop options, setup, and cancellation. + * @returns the published handle. */ - createAgent(options: CreateAgentOptions): AgentHandle { - // Check the agent id BEFORE preparing the session: register() would reject a - // duplicate id only AFTER the session enters the store, leaving an orphaned - // live session (and lazy persistence state) that blocks reuse of that id. - this.assertAgentIdFree(options.agentId) - const session = this.ctx.sessions.prepare(options.sessionId, { - ...options.seed !== undefined ? { seed: options.seed } : {}, - meta: options.meta ?? {}, - }) - // A seeded (forked) create is still a fresh start, NOT a resume — `resume` - // is reserved for reloading a PERSISTED session via resume()/resumeWith(). - return this.startOwned(options.agentId, options.agentOptions ?? {}, session, 'startup') + async createAgent(ownerCtx: Context, options: CreateAgentOptions): Promise { + const transaction = new AgentCreationTransaction( + this.runtime.ctx, + ownerCtx, + this.ownership, + options.agentId, + options.signal, + ) + try { + const session = this.runtime.ctx.sessions.prepare(options.sessionId, { + ...options.seed === undefined ? {} : { seed: options.seed }, + ...options.meta === undefined ? {} : { meta: options.meta }, + }) + const agent = transaction.prepare(options.agentOptions ?? {}, session) + await transaction.waitFor(options.setup?.(agent.ctx)) + transaction.assertActive() + return transaction.publish('startup') + } catch (error: unknown) { + await transaction.dispose(error instanceof Error ? error : new Error(String(error))) + throw error + } finally { + transaction.finishWrapper() + } } /** - * Resume an agent on a persisted session ({@link AgentFactory}). Loads the - * session log + metadata via `ctx.sessionPersistence`, reconstructs the live - * session with the loaded events (so `lastTurnNumber`/`deriveMessages` - * continue), and starts a fresh agent on it. The live session id is the - * resumed id, NOT `${agentId}-session`. - * - * Requires `ctx.sessionPersistence`; rejects with a clear error if it is not - * configured. NOT hard-injected (that would make non-persistent demos pend - * forever) — callers that need resume (ACP) inject `sessionPersistence`, so - * by the time this runs the service exists. - * @param options - the persisted session id to reload, plus agent id/options. - * @returns the handle for the agent resumed on the reconstructed session. + * Resume an owned agent from the configured persistence service. + * @param ownerCtx - caller context that owns load, setup, and the live lifecycle. + * @param options - persisted identity, loop options, setup, and cancellation. + * @returns the published handle. */ - async resume(options: ResumeAgentOptions): Promise { - // Read the service through `ctx.get('sessionPersistence')` — a direct - // global-store lookup keyed by the isolate symbol — NOT - // `this.ctx.sessionPersistence`. AgentLoop deliberately does NOT inject - // `sessionPersistence` (injecting it would pend non-persistent demos - // forever). The `ctx.` property proxy resolves a service by an - // ancestor-only walk of the current fiber's parent chain; from AgentLoop's - // own fiber (which lacks the inject) that walk never reaches the sibling - // backend fiber and throws "cannot get property … without inject". Worse, - // when the call arrives via a traceable shadow (e.g. the ACP bridge child - // fiber → `ctx.agents.resume()` → `this.factory.resume()`), the walk starts - // at the shadow's origin fiber and fails the same way. `ctx.get(name)` - // sidesteps the fiber walk entirely (a store lookup by the global isolate - // key), so resume works from any caller fiber. It is strict by default: a - // backend that is not ACTIVE (absent, or mid-teardown) reads as undefined - // and we reject below, rather than handing back an unusable handle. - const persistence = this.ctx.get('sessionPersistence') + async resume(ownerCtx: Context, options: ResumeAgentOptions): Promise { + const persistence = this.runtime.ctx.get('sessionPersistence') if (persistence === undefined) { throw new Error('cannot resume: session persistence is not configured (load a dsh-session-persistence backend)') } - return this.resumeWith(persistence, options) + return this.resumeWith(ownerCtx, persistence, options) } - /** - * Resume against an EXPLICIT persistence handle. Factored out of {@link resume} - * so the config-driven path can pass the handle it obtained from a - * `ctx.inject(['sessionPersistence'], …)` child context: `this.ctx` (the - * service's own fiber) did not inject `sessionPersistence`, so reading it - * there from inside the inject child trips the cordis inject guard. The - * sessions store + registry are still read through `this.ctx` (both are in - * AgentLoop's static inject, so they resolve fine). - */ - private async resumeWith(persistence: SessionPersistence, options: ResumeAgentOptions): Promise { - this.assertAgentIdFree(options.agentId) - const { meta, events } = await persistence.load(options.resumeSessionId) - // Re-check the agent id AFTER the await: the pre-load check above can go - // stale while load() is pending (a concurrent resume/create may register the - // same id). Re-checking immediately before prepare()/start keeps the - // "no orphaned session on a duplicate id" guarantee under concurrency. - this.assertAgentIdFree(options.agentId) - // Reconstruct the live session with the FULL persisted header (createdAt, - // cwd, lineage) so resume preserves identity, not just the cwd. The seed - // events make lastTurnNumber/deriveMessages continue; the backend already - // has state (cursor) from the load above, so onCreated is a no-op and the - // seed is not re-persisted. prepare() (not create()) so the session - // lifecycle folds into the agent's composite effect (ordered teardown). - const session = this.ctx.sessions.prepare(options.resumeSessionId, { - seed: events, - meta: { - createdAt: meta.createdAt, - ...meta.cwd !== undefined ? { cwd: meta.cwd } : {}, - ...meta.parentSession !== undefined ? { parentSession: meta.parentSession } : {}, - // Reconstruct the seed boundary from the persisted header, NOT from - // `events.length` (the resume seeds the WHOLE stored log). - ...meta.seedLength !== undefined ? { seedLength: meta.seedLength } : {}, - }, - }) - return this.startOwned(options.agentId, options.agentOptions ?? {}, session, 'resume') - } - - /** - * Reject a duplicate agent id BEFORE the session is entered into the store, so - * a failed factory call never leaves an orphaned live session (and lazy - * persistence state) behind. `register()` enforces the same uniqueness, but - * only after the session has already entered the store. - */ - private assertAgentIdFree(id: AgentId): void { - if (this.ctx.agents.get(id) !== undefined) { - throw new Error(`agent "${id}" is already registered`) + /** Resume through an explicit persistence handle used by the deferred config path. */ + private async resumeWith( + ownerCtx: Context, + persistence: SessionPersistence, + options: ResumeAgentOptions, + ): Promise { + const transaction = new AgentCreationTransaction( + this.runtime.ctx, + ownerCtx, + this.ownership, + options.agentId, + options.signal, + ) + try { + const loaded = await transaction.waitFor(persistence.load(options.resumeSessionId)) + transaction.assertActive() + const session = this.runtime.ctx.sessions.prepare(options.resumeSessionId, { + seed: loaded.events, + meta: { + createdAt: loaded.meta.createdAt, + ...loaded.meta.cwd === undefined ? {} : { cwd: loaded.meta.cwd }, + ...loaded.meta.parentSession === undefined ? {} : { parentSession: loaded.meta.parentSession }, + ...loaded.meta.seedLength === undefined ? {} : { seedLength: loaded.meta.seedLength }, + }, + }) + const agent = transaction.prepare(options.agentOptions ?? {}, session) + await transaction.waitFor(options.setup?.(agent.ctx)) + transaction.assertActive() + return transaction.publish('resume') + } catch (error: unknown) { + await transaction.dispose(error instanceof Error ? error : new Error(String(error))) + throw error + } finally { + transaction.finishWrapper() } } - - /** - * Shared: construct a ReactLoopAgent over a PREPARED (not-yet-entered) - * session, then build the ONE composite effect that owns the whole agent - * lifecycle — session entry, registry registration, and the loop. Keeping all - * three in a SINGLE effect (not sibling effects) is load-bearing: a fiber - * unload disposes sibling effects CONCURRENTLY (`Promise.all`), which would - * race the session detach against the loop's closing flush and drop the - * closing `turn/end`. Inside one effect the disposers run as an ORDERED LIFO - * chain — the runtime awaits each disposer's returned promise before the next: - * - * yield session-detach (disposed LAST — detach onAppend + remove entry) - * yield register (disposed 2nd — unregister) - * yield stop-and-drain (disposed FIRST — request loop stop, await agent.done) - * - * So on teardown: the loop is stopped and AWAITED to exit (its final - * `session/flush` + `turn/end` fire through the still-attached `onAppend`), - * THEN the agent is unregistered, THEN the session is detached — capturing the - * closing events before detach, whether the trigger is the handle's `dispose()` - * OR a fiber unload. Rollback safety: each yield runs before the next mutation, - * so a throwing `session/created`/`agent/created` listener unwinds the - * already-yielded disposers instead of leaking. - * - * `source` says why the session began ({@link SessionStartSource}); it is - * emitted as `agent/session-start` once, AFTER the agent is registered (so a - * listener can resolve the agent via `ctx.agents.get(id)` and `inject()` into - * it) and BEFORE the loop starts its first turn. The emit is contained: a - * throwing session-start listener must not abort agent construction — it is - * logged, and the agent still starts. (Unlike a turn-boundary throw, there is - * no open turn here to balance; the durable evidence of a session-start hook - * is whatever it `inject()`ed.) - * - * Returns the agent plus the composite effect's disposer (`disposeAgent`). - */ - private start( - id: AgentId, options: AgentOptions, session: Session, source: SessionStartSource, - ): { agent: ReactLoopAgent; disposeAgent: () => Promise } { - const agent = new ReactLoopAgent(this.ctx, id, options, session) - const dispose = this.ctx.effect(function* (this: AgentLoop) { - yield this.ctx.sessions.enter(session) - this.ctx.sessions.announce(session) - yield this.ctx.agents.register(agent) - // Fire AFTER register (a listener can ctx.agents.get(id) + inject()) and - // BEFORE the loop's first turn. Contained: a throwing listener is logged, - // never aborts construction (no open turn to balance here). - try { - this.ctx.emit('agent/session-start', agent, source) - } catch (error: unknown) { - this.ctx.logger.warn(`agent "${id}": agent/session-start listener threw: ${String(error)}`) - } - const stop = agent.start() - // Disposed FIRST (LIFO): request loop stop (sync), then AWAIT the loop's - // actual exit so its closing flush lands while onAppend (yielded above, - // disposed later) is still attached. - yield async () => { stop(); await agent.done } - }.bind(this), 'agentLoop.start()') - return { agent, disposeAgent: async () => { await dispose() } } - } - - /** - * Build an {@link AgentHandle} for a PREPARED session + a fresh agent. The - * handle's `dispose()` runs the composite effect's disposer (see - * {@link start}) — which stops the loop, awaits its exit (final flush - * captured), unregisters the agent, and detaches the session, in that order. - * The same composite effect is what a fiber unload disposes, so both teardown - * triggers honor the ordering identically. - * - * `dispose()` is MEMOIZED: the underlying cordis effect disposer is - * single-shot (a second call returns immediately because the effect's epoch is - * already cleared, NOT awaiting the in-flight teardown), so concurrent/repeated - * `dispose()` calls would otherwise resolve before the first call's - * `await agent.done` + final flush completed. Memoizing the promise makes every - * caller observe the SAME quiescence boundary, honoring the - * `AgentHandle.dispose(): Promise` contract (mirrors the ACP `quiesce()` - * helper). - */ - private startOwned(id: AgentId, options: AgentOptions, session: Session, source: SessionStartSource): AgentHandle { - const { agent, disposeAgent } = this.start(id, options, session, source) - let disposing: Promise | undefined - return { agent, dispose: () => (disposing ??= disposeAgent()) } - } } export default AgentLoop diff --git a/packages/core/agent-loop/src/loop.ts b/packages/core/agent-loop/src/loop.ts index 33ddee8f64..7a7ca2e28c 100644 --- a/packages/core/agent-loop/src/loop.ts +++ b/packages/core/agent-loop/src/loop.ts @@ -10,7 +10,8 @@ import type { Context } from 'cordis' import type { FinishReason, GenerateOptions, LlmCallConfig, Message } from '@deepseek-ai/dsh-llm' import { BlockAssembler, HarnessError, deepFreeze } from '@deepseek-ai/dsh-llm' -import type { ContinuationDecision, HookContext, PromptDecision } from '@deepseek-ai/dsh-agent' +import { agentEvents, assembleContextFor } from '@deepseek-ai/dsh-agent' +import type { AgentEventDispatch, ContinuationDecision, HookContext, PromptDecision } from '@deepseek-ai/dsh-agent' import { canonicalHeader } from '@deepseek-ai/dsh-session' import type { Session, TurnEndReason, TurnTrigger } from '@deepseek-ai/dsh-session' import { createTransmissionLog, recordRequestHeader } from './request-log.ts' @@ -19,6 +20,7 @@ import { renderPrompt } from '@deepseek-ai/dsh-system-prompt' import type { PromptAssembly } from '@deepseek-ai/dsh-system-prompt' import type {} from '@deepseek-ai/dsh-tools' import type { ReactLoopAgent } from './agent.ts' +import type { Inbox } from './inbox.ts' /** An Error with an optional machine-readable code (e.g., from LlmError or a throwing plugin). */ type CodedError = Error & { code?: string } @@ -107,6 +109,8 @@ function stepFinishReason(finish: FinishReason): TurnEndReason | undefined { * loop testable without a real agent. */ export interface LoopHandle { + /** Native-private agent inbox handed to the driver only at internal startup. */ + readonly inbox: Inbox setStatus(status: 'idle' | 'running'): void setAbort(controller: AbortController | undefined): void /** Resolves when the agent is disposed — unblocks the idle wait. */ @@ -155,12 +159,13 @@ export interface LoopHandle { * every prompt blocked → 'turn/end'(rejected), 0 steps * STEP loop: * drain steering → session('steering/message') ⟵ catches late steering - * assembly = ctx.systemPrompt.assemble({agent}) ⟵ waterfall system-prompt/assemble; renderPrompt + * assembly = ctx.systemPrompt.assemble(assembleContextFor(agent)) ⟵ waterfall system-prompt/assemble + * (scope-filtered; scoped sections/tools join); renderPrompt * (persona section + {{variables}}) IS the full prompt - * prefix ??= waterfall agent/session-prefix ⟵ once per loop instance (first step): frozen + * prefix ??= waterfall agent/session-prefix ⟵ once per loop instance (first step): frozen * session prefix; logged on the header, never - * session history - * await ctx.serial('agent/pre-step', …, prefix) ⟵ surface mutation (compaction) OUTSIDE the step; + * session history (scope-filtered, fused dispatch) + * await events.serial('agent/pre-step', …, prefix) ⟵ surface mutation (compaction) OUTSIDE the step; * pressure gates see the prefix the request carries * boundary = session.deriveMessages() ⟵ the reconstruction boundary: snapshot in the * session('step/start') same sync frame, strictly before step/start @@ -183,9 +188,12 @@ export interface LoopHandle { * {action: hadToolCalls||steered ? 'continue':'stop'}; a continue.reason is * recorded as next-step steering * if action==stop && steering arrived (step/end/continuation listeners): continue anyway + * terminal = serial agent/turn-stop ⟵ stop or abstain; after all ordinary + * continuation and steering folding + * if terminal: discard pending steering and break * if action==stop: break * session('turn/end') ⟵ durable turn boundary (no agent/* mirror) - * await ctx.parallel('session/flush', session) ⟵ durability checkpoint + * await ctx.sessions.flush(session) ⟵ durability checkpoint (store-owned carrier) * re-enqueue leftover steering as queued ⟵ steering is never stranded * idle (emit agent/status) unless more queued * ``` @@ -202,9 +210,13 @@ export async function runLoop(ctx: Context, agent: ReactLoopAgent, handle: LoopH const transmission = createTransmissionLog() const { session } = agent + // The fused agent-subject dispatcher: every agent/* dispatch below carries + // the agent's scope (an `agent.ctx` listener hears only this agent) with + // the subject injected — one spelling, checked by the dev invariants. + const events = agentEvents(ctx, agent) while (!handle.isDisposed()) { - await agent.inbox.waitForQueued(handle.disposed) + await handle.inbox.waitForQueued(handle.disposed) if (handle.isDisposed()) break // Pre-step cancel (window 1): a `cancel()` landed after a `send()` woke the @@ -222,7 +234,7 @@ export async function runLoop(ctx: Context, agent: ReactLoopAgent, handle: LoopH // resolve before it runs (the quiescence contract). if (handle.isCancelled()) { handle.clearCancel() - if (!agent.inbox.hasQueued) { + if (!handle.inbox.hasQueued) { handle.settleIdle() continue } @@ -244,7 +256,7 @@ export async function runLoop(ctx: Context, agent: ReactLoopAgent, handle: LoopH // is still queued and unrun (the same early-resolve race window 1 fixes). if (handle.isCancelled()) { handle.clearCancel() - if (!agent.inbox.hasQueued) { + if (!handle.inbox.hasQueued) { handle.setStatus('idle') continue } @@ -255,8 +267,9 @@ export async function runLoop(ctx: Context, agent: ReactLoopAgent, handle: LoopH // the loop waits above, so the next real turn must continue from whatever // turn number is actually last in the log — a stale counter would collide. const turn = lastTurnNumber(session) + 1 + let terminalStopped = false try { - await runTurn(ctx, agent, handle, turn, transmission) + terminalStopped = await runTurn(ctx, events, agent, handle, turn, transmission) } catch (error: unknown) { // Backstop: runTurn rethrows only a PRE-turn throw (the invariant guard // before turn/start) — no turn/start was appended, so no turn is open and @@ -264,10 +277,13 @@ export async function runLoop(ctx: Context, agent: ReactLoopAgent, handle: LoopH // the previous turn/end), where the persistence backend drops it as a // crash tail (the turn-enclosure RFC). Report via agent/error + the logger only; the // driver survives and moves on. + // Acceptance and internal dispatch validation can reject before + // turn/start commits. Report that supported pre-turn failure without + // inventing a turn/end for a turn that never opened. const err = toError(error) ctx.logger.warn(`agent "${agent.id}": turn ${turn} failed before it started: ${err.message}`) try { - ctx.emit('agent/error', agent, turn, 0, err) + events.emit('agent/error', turn, 0, err) } catch { /* contained: a throwing agent/error listener must not kill the driver */ } } @@ -280,27 +296,30 @@ export async function runLoop(ctx: Context, agent: ReactLoopAgent, handle: LoopH // cancelled. handle.clearCancel() - // Steering that arrived too late to join this turn (turn-end listeners, - // flush) becomes a queued message — it must never be stranded. (A cancelled - // turn already cleared its steering, so there is nothing to re-enqueue.) - for (const message of agent.inbox.drainSteering()) { - agent.inbox.enqueue(message) + // Steering that arrived too late to join an ordinary turn (turn-end + // listeners, flush) becomes queued input so it is never stranded. A + // terminal-stop owner is the deliberate exception: discard the steering + // again after the close + flush window so terminal policy cannot be undone + // after its in-turn drain. Ordinary queued sends live in a separate FIFO and + // remain untouched. + for (const message of handle.inbox.drainSteering()) { + if (!terminalStopped) handle.inbox.enqueue(message) } - if (!agent.inbox.hasQueued) handle.setStatus('idle') + if (!handle.inbox.hasQueued) handle.setStatus('idle') } } async function runTurn( - ctx: Context, agent: ReactLoopAgent, handle: LoopHandle, turn: number, transmission: TransmissionLog, -): Promise { + ctx: Context, events: AgentEventDispatch, agent: ReactLoopAgent, handle: LoopHandle, turn: number, transmission: TransmissionLog, +): Promise { const { session } = agent // --- Pre-turn. A throw here (the invariant guard) is owed NO turn/end — // turn/start has not been appended — so it propagates to runLoop's backstop // untouched. The queued messages are drained here but appended AFTER // turn/start (below), so every event in the log lives inside a turn. - const queued = agent.inbox.drainQueued() + const queued = handle.inbox.drainQueued() const first = queued[0] /* v8 ignore next 3 -- invariant guard: runLoop only calls runTurn when hasQueued */ if (!first) throw new Error('runTurn invariant violated: no queued message at turn start') @@ -310,35 +329,16 @@ async function runTurn( let step = 0 let stepOpen = false let errorReported = false + let terminalStopped = false - // Close the open step exactly once (idempotent via stepOpen). Step boundaries - // are durable session events only — there is no agent/* step emit to mirror - // them (see the agent event-domain rule). A throwing step/end session-event - // listener must not abort finalization and strand the turn open (turn/end - // balance > notifying one bad listener); it is contained and surfaced as a - // turn error below. - const closeStep = (): boolean => { - if (!stepOpen) return false + // Close the open step exactly once (idempotent via stepOpen). Post-commit + // session/event observers are contained by Session; a pre-commit validator + // failure still escapes so the outer recovery path may retry the boundary or + // fail loudly without pretending an uncommitted step/end exists. + const closeStep = (): void => { + if (!stepOpen) return + session.append('step/end', { turn, step }) stepOpen = false - // Session.append pushes step/end BEFORE notifying session/event listeners, - // so a throwing listener leaves step/end in the log (balance holds) but - // would otherwise abort finalization. Contain it and surface it as a turn - // error below. - let failure: unknown - try { - session.append('step/end', { turn, step }) - } catch (error: unknown) { - failure = error - } - // A throwing step/end session-event listener surfaces as a turn error via - // failTurn (idempotent). This prevents a throwing listener from producing a - // silent "completed" turn when the step itself succeeded, AND keeps - // finalization going when closeStep runs from the outer catch. - if (failure !== undefined) { - failTurn(toError(failure)) - return true - } - return false } // Record a step/turn failure exactly once: set the error reason (carrying the @@ -350,45 +350,29 @@ async function runTurn( const failTurn = (err: CodedError): void => { if (errorReported) return errorReported = true - // The turn is always still open here: the only failure that can reach - // failTurn once turn/end is appended would be a throwing turn-boundary - // listener, and turn boundaries are durable session events with no agent/* - // mirror to throw. A throwing `turn/end` session-event listener is already - // contained inside closeTurn (append pushes before notifying, so the - // boundary is durable). So set the error reason for closeTurn to append. + // The turn is still open here. Post-commit observers cannot escape append, + // and a pre-commit turn/end veto leaves no closing boundary to overwrite. + // Set the reason that the next successful closeTurn will append. reason = { kind: 'error', step, ...errorData(err) } try { - ctx.emit('agent/error', agent, turn, step, err) + events.emit('agent/error', turn, step, err) } catch { // contained: the error is already captured on `reason`; a throwing // agent/error listener must not prevent the turn from closing. } } - // Close the turn. Called exactly once per turn — the normal loop exit and the - // outer catch are mutually exclusive paths, and this never throws (the append - // is contained below), so there is no re-entry to guard against (unlike - // closeStep, which the cancel branches and the outer catch can both reach). - // Turn boundaries are durable session events only — there is no agent/* turn - // emit to mirror them (see the agent event-domain rule). + // Close the turn. Post-commit observer failures are contained by Session; + // pre-commit validation failures escape to recovery instead of being mistaken + // for a committed boundary. Turn boundaries are durable session events only. const closeTurn = (): void => { - // Session.append pushes turn/end BEFORE notifying session/event listeners, - // so a throwing listener leaves turn/end in the log (the turn is balanced) - // but would otherwise escape — from the outer catch it would propagate to - // the runLoop backstop. Contain it: the boundary is durable either way, and - // finalization must not abort on a bad listener. - try { - session.append('turn/end', { turn, reason }) - } catch (error: unknown) { - ctx.logger.warn(`agent "${agent.id}": session/event listener threw on turn/end at turn ${turn}: ${toError(error).message}`) - } + session.append('turn/end', { turn, reason }) } try { // --- Turn boundary. Once turn/start is appended, a turn/end is owed no - // matter what throws below; the catch + closeTurn guarantee it (the catch - // decides "owed" from the log via isTurnOpen, so even a throwing turn/start - // listener — append pushes before notifying — still gets its turn/end). + // matter what throws below; the catch + closeTurn guarantee it. A pre-commit + // veto leaves no turn/start in the log and therefore owes no turn/end. session.append('turn/start', { turn, trigger }) // Each drained queued message runs the `agent/prompt-submit` waterfall before // it becomes a `user/message` — a hook can rewrite the prompt or block it. @@ -402,8 +386,8 @@ async function runTurn( // batch always reports the last vetoing reason. let lastBlockReason = 'prompt blocked by hook' for (const message of queued) { - const decision = await ctx.waterfall( - 'agent/prompt-submit', agent, message.content, message.source, + const decision = await events.waterfall( + 'agent/prompt-submit', message.content, message.source, () => Promise.resolve({ kind: 'allow' }), ) if (decision.kind === 'block') { @@ -443,7 +427,7 @@ async function runTurn( // Steering from the previous round's continuation listeners joins before // the request. - drainSteering(agent, turn) + drainSteering(agent, handle.inbox, turn) // The step's AbortController exists BEFORE any async pre-step work so a // dispose() or cancel() — in a synchronous turn-start listener or an @@ -458,9 +442,9 @@ async function runTurn( // against the system prompt (it counts toward the budget). runStep reuses // this same assembly for the request, so the prompt is assembled once per // step. renderPrompt IS the full prompt — the persona is the order-0 - // section (registered by the AgentLoop plugin) and `{{variable}}` + // section (owned by dsh-system-prompt) and `{{variable}}` // interpolation happens in the render, so there is no separate join. - const assembly = await ctx.systemPrompt.assemble({ agent }) + const assembly = await ctx.systemPrompt.assemble(assembleContextFor(agent)) const fullSystemPrompt = renderPrompt(assembly) // Interruption landing after assembly: dispose() or cancel() in a @@ -497,8 +481,8 @@ async function runTurn( // CURRENT request. if (transmission.sessionPrefix === undefined) { const emptyPrefix: Message[] = deepFreeze([]) - const composed = await ctx.waterfall( - 'agent/session-prefix', agent, emptyPrefix, abort.signal, + const composed = await events.waterfall( + 'agent/session-prefix', emptyPrefix, abort.signal, () => Promise.resolve(emptyPrefix), ) @@ -533,7 +517,7 @@ async function runTurn( // pre-step plugin ends the turn, not the loop. The composed session // prefix rides along so token-pressure listeners count everything the // request will actually carry. - await ctx.serial('agent/pre-step', agent, turn, step, fullSystemPrompt, transmission.sessionPrefix, abort.signal) + await events.serial('agent/pre-step', turn, step, fullSystemPrompt, transmission.sessionPrefix, abort.signal) // Interruption landing during the pre-step seam: do not open an empty step. if (handle.isCancelled() || handle.isDisposed()) { @@ -546,20 +530,19 @@ async function runTurn( // messages are snapshotted HERE, in the same synchronous frame as the // step/start append directly below — so the snapshot is exactly the // derivation over the log prefix strictly before step/start's seq. - // Anything appended later — by a step/start session/event listener, an - // agent/request-window inject(), any concurrent task — lands after the - // boundary and joins the NEXT request. An external reconstructor + // Anything appended later by the request-window inject seam or a + // concurrent task lands after the boundary and joins the NEXT request. + // session/event itself is observe-only: append reentrancy is rejected + // until the current callback list drains. An external reconstructor // recovers these exact messages by folding the surface over // events[0..stepStartSeq). const boundaryMessages = session.deriveMessages() - // Mark the step open BEFORE the append: Session.append pushes the event - // to the log before notifying session/event listeners, so a THROWING - // step/start listener leaves step/start in the log. Setting stepOpen first - // means the outer catch's closeStep() then appends the balancing step/end - // (turn stays enclosed) instead of stranding an open step under turn/end. - stepOpen = true session.append('step/start', { turn, step }) + // Only a committed step/start creates a balancing obligation. A + // pre-commit veto throws before this assignment; post-commit observers + // are contained inside Session.append(). + stepOpen = true // Cancel landing in the step-start window: a synchronous `session/event` // step/start listener can cancel after the step is already open. Check @@ -574,7 +557,8 @@ async function runTurn( let stepOutcome: { hadToolCalls: boolean; finish: FinishReason } | { error: Error } try { - stepOutcome = await runStep(ctx, agent, turn, step, assembly, fullSystemPrompt, boundaryMessages, transmission, abort.signal) + stepOutcome = await runStep( + ctx, events, agent, turn, step, assembly, fullSystemPrompt, boundaryMessages, transmission, abort.signal) } catch (error: unknown) { stepOutcome = { error: toError(error) } } finally { @@ -609,15 +593,15 @@ async function runTurn( if (stepReason) reason = stepReason // Steering that arrived during streaming/tool execution. - const steered = drainSteering(agent, turn) + const steered = drainSteering(agent, handle.inbox, turn) - if (closeStep()) break + closeStep() const defaultDecision: ContinuationDecision = { action: stepOutcome.hadToolCalls || steered ? 'continue' : 'stop' } let decision: ContinuationDecision try { - decision = await ctx.waterfall( - 'agent/turn-continuation', agent, turn, defaultDecision, + decision = await events.waterfall( + 'agent/turn-continuation', turn, defaultDecision, () => Promise.resolve(defaultDecision), ) } catch (error: unknown) { @@ -631,14 +615,38 @@ async function runTurn( // iteration drains it before its request — the typed twin of the /goal // step/end-steer pattern. if (decision.action === 'continue' && decision.reason) { - agent.inbox.steer({ content: decision.reason.content, source: decision.reason.source }) + handle.inbox.steer({ content: decision.reason.content, source: decision.reason.source }) } let shouldContinue = decision.action === 'continue' // Steering from step/end session-event or continuation listeners (the // /goal pattern) demands the model see it — it overrides a stop decision; // the next iteration's drain records it. - if (!shouldContinue && agent.inbox.hasSteering) shouldContinue = true + if (!shouldContinue && handle.inbox.hasSteering) shouldContinue = true + + // Terminal policy runs only AFTER the extensible continuation waterfall, + // its optional reason, and late steering have all been folded. Unlike the + // waterfall, this serial seam is monotonic: the first stop bail wins, and + // no later listener or steering override can resurrect the turn. + let terminalStop = false + try { + const stop = await events.serial('agent/turn-stop', turn) + terminalStop = stop !== undefined + } catch (error: unknown) { + // A broken terminal policy is an ordinary continuation failure: fail + // this turn closed while leaving the driver alive for later turns. + failTurn(toError(error)) + break + } + if (terminalStop) { + terminalStopped = true + // A continuation reason or listener may have queued steering before the + // terminal checkpoint. Discard only steering (never ordinary queued + // prompts) so it cannot become a next step or be re-enqueued as a fresh + // turn by runLoop's late-steering fallback. + handle.inbox.drainSteering() + shouldContinue = false + } // A cancel that landed during the continuation window — after the step's // AbortController was cleared (setAbort(undefined)) but before the next @@ -660,21 +668,10 @@ async function runTurn( // Normal / inline-error loop exit: close the turn. closeTurn() } catch (error: unknown) { - // Decide whether this turn was ever opened from the LOG, not a flag. - // Session.append pushes the event BEFORE notifying session/event listeners, - // so a throwing listener on the `turn/start` append leaves turn/start in the - // log even though execution never reached the lines after that append. - // Gating on a "turn started" boolean would skip turn/end and leave a - // permanently OPEN turn that poisons the next turn/replay (the turn-enclosure RFC). We - // check the log for THIS turn's turn/start: present means a turn/end is owed - // and the normal-exit `closeTurn()` did NOT run (we are here because a throw - // preceded it — the two `closeTurn()` sites are on mutually exclusive paths), - // so this catch appends turn/end with the disposed/error reason chosen below. - // `closeStep()` IS idempotent (guarded by `stepOpen`) — it may have run - // already in a step branch, so running it again is a safe no-op. Absent - // turn/start means the append threw BEFORE its push (a non-serializable - // trigger — impossible for our fixed trigger); nothing was opened, so rethrow - // to the runLoop backstop. + // Decide whether this turn opened from the LOG, not a speculative flag. A + // pre-commit validator or acceptance failure leaves no turn/start and owes + // no turn/end, so it propagates to runLoop's backstop. Once turn/start is + // present, this path balances any committed step and records the failure. const turnStartLogged = session.events.some(e => e.type === 'turn/start' && e.data.turn === turn) if (!turnStartLogged) throw error closeStep() @@ -694,8 +691,9 @@ async function runTurn( // Durability checkpoint: persistence plugins drain write-behind buffers. // A failing persistence plugin is reported but doesn't kill the agent. + // Through the store's flush (the carrier owner), never a raw parallel. try { - await ctx.parallel('session/flush', session) + await ctx.sessions.flush(session) } catch (error: unknown) { // The turn is already closed (turn/end appended above) and flush must run // AFTER turn/end to be a checkpoint — so there is no in-turn position left @@ -707,16 +705,17 @@ async function runTurn( const err = toError(error) ctx.logger.warn(`agent "${agent.id}": session/flush failed at turn ${turn}: ${err.message}`) try { - ctx.emit('agent/error', agent, turn, step, err) + events.emit('agent/error', turn, step, err) } catch { // contained: a throwing agent/error listener must not escape the loop. } } + return terminalStopped } /** Drain the steering queue into the session. Returns whether any arrived. */ -function drainSteering(agent: ReactLoopAgent, turn: number): boolean { - const messages = agent.inbox.drainSteering() +function drainSteering(agent: ReactLoopAgent, inbox: Inbox, turn: number): boolean { + const messages = inbox.drainSteering() for (const message of messages) { agent.session.append('steering/message', { turn, content: message.content, source: message.source }, { surfaceOp: 'append' }) } @@ -732,6 +731,7 @@ function drainSteering(agent: ReactLoopAgent, turn: number): boolean { * the surface prefix at step/start and already reflects any compaction. */ async function runStep( ctx: Context, + events: AgentEventDispatch, agent: ReactLoopAgent, turn: number, step: number, @@ -764,7 +764,7 @@ async function runStep( // model-visible content flows through the log channels). The header event // below records whatever the request ACTUALLY uses, so a listener's switch // is a logged, reconstructable fact, never silent drift. - const config = await ctx.waterfall('agent/request', agent, turn, step, seedConfig, () => Promise.resolve(seedConfig)) + const config = await events.waterfall('agent/request', turn, step, seedConfig, () => Promise.resolve(seedConfig)) if (!config.model) { throw new Error(`agent "${agent.id}" has no model: set AgentOptions.model or supply one via the agent/request waterfall`) } @@ -823,7 +823,7 @@ async function runStep( if (assembler.finish.kind === 'max-tokens') { let message: Message = withoutToolCalls(assembler.message()) - message = withoutToolCalls(await ctx.waterfall('agent/step-result', agent, turn, step, message, () => Promise.resolve(message))) + message = withoutToolCalls(await events.waterfall('agent/step-result', turn, step, message, () => Promise.resolve(message))) // Fire the assistant/message when there is content OR usage: a max-tokens // step can be cut off with empty content but still carry token accounting, // and assistant/message is the only host for usage (there is no standalone @@ -846,7 +846,7 @@ async function runStep( // source of truth for derived history and replay) records the message that // tool dispatch actually uses. let message: Message = assembler.message() - message = await ctx.waterfall('agent/step-result', agent, turn, step, message, () => Promise.resolve(message)) + message = await events.waterfall('agent/step-result', turn, step, message, () => Promise.resolve(message)) // Same content-or-usage guard as the max-tokens branch: a step that finishes // with neither assembled content nor usage (e.g. a bare `stop` finish that diff --git a/packages/core/agent-loop/tests/agent.spec.ts b/packages/core/agent-loop/tests/agent.spec.ts index ed163bf4c2..7f65bbc5f3 100644 --- a/packages/core/agent-loop/tests/agent.spec.ts +++ b/packages/core/agent-loop/tests/agent.spec.ts @@ -7,6 +7,7 @@ import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRegistry from '@deepseek-ai/dsh-tools' import AgentRegistry from '@deepseek-ai/dsh-agent' import AgentLoop, { ReactLoopAgent } from '@deepseek-ai/dsh-agent-loop' +import { bindReactLoopAgentContext, prepareReactLoopAgent } from '../src/agent.ts' import { MockAdapter, textResponse } from './mock-adapter.ts' async function harness(adapter: MockAdapter) { @@ -48,6 +49,33 @@ function send(agent: ReactLoopAgent, text: string) { } describe('ReactLoopAgent', () => { + it('rejects access before context binding and a second driver for one session', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + const session = ctx.sessions.create(SessionId('exclusive-driver')) + const prepared = prepareReactLoopAgent(ctx, AgentId('first-driver'), { model: 'mock' }, session) + + expect(() => prepared.agent.ctx).toThrow('context is not bound') + expect(() => prepareReactLoopAgent(ctx, AgentId('second-driver'), { model: 'mock' }, session)) + .toThrow('already has a concrete agent driver') + + await prepared.dispose() + await ctx.fiber.dispose() + }) + + it('borrows caller options and binds its scoped context exactly once', async () => { + const ctx = await harness(new MockAdapter([textResponse('unused')])) + const options = { model: 'mock' } + const agent = ctx.agentLoop.create(AgentId('owned-bindings'), options) + + expect(agent.options).toBe(options) + expect(agent.id).toBe('owned-bindings') + expect(agent.session.id).toMatch(/^owned-bindings-session-/) + expect(() => { bindReactLoopAgentContext(agent, new Context()) }).toThrow(/context is already bound/) + + await ctx.fiber.dispose() + }) + it('send() throws after disposal', async () => { const adapter = new MockAdapter(['hang']) const ctx = await harness(adapter) @@ -158,10 +186,8 @@ describe('ReactLoopAgent', () => { const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' }) let flushes = 0 ctx.on('session/flush', () => { flushes += 1 }) - // A session/event listener that throws on the synthetic turn/end. Append - // pushes before notifying, so turn/end is in the log (turn balanced) but the - // throw must NOT skip the durability checkpoint — the flush decision is made - // from the log, not a flag set after the (throwing) append. + // Session contains a throwing post-commit turn/end observer. The accepted + // boundary still triggers the idle injection's durability checkpoint. let threw = false ctx.on('session/event', (_s, event) => { if (!threw && event.type === 'turn/end') { threw = true; throw new Error('boom turn/end') } @@ -226,26 +252,45 @@ describe('ReactLoopAgent', () => { }) it('disposer is idempotent (double-stop)', async () => { - // Create a bare ReactLoopAgent and call start() directly to get the disposer. - // Then call it twice — the second call hits the early-return branch. + // Create a bare ReactLoopAgent and start it through the package-internal + // test seam. Then call its disposer twice — the second call hits the + // early-return branch. const ctx = new Context() await ctx.plugin(SessionStore) const session = ctx.sessions.create(SessionId('test')) - const agent = new ReactLoopAgent(ctx, AgentId('bare'), { model: 'mock' }, session) + const prepared = prepareReactLoopAgent(ctx, AgentId('bare'), { model: 'mock' }, session) + const { agent } = prepared // Start the loop to get the disposer; the agent waits for messages // (idle, never-resolving cancel), so it will stay idle. - const dispose = agent.start() + prepared.markPublished() + const dispose = prepared.startDriver() // First dispose - dispose() + const firstDisposal = dispose() expect(agent.status).toBe('disposed') + await firstDisposal // Second dispose — idempotent, no throw - expect(() => { dispose() }).not.toThrow() + await expect(dispose()).resolves.toBeUndefined() expect(agent.status).toBe('disposed') }) + it('a pre-start disposal makes a later driver-start attempt inert', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + const session = ctx.sessions.create(SessionId('pre-start-dispose')) + const prepared = prepareReactLoopAgent(ctx, AgentId('pre-start-dispose'), { model: 'mock' }, session) + + await prepared.dispose() + expect(prepared.agent.status).toBe('disposed') + const dispose = prepared.startDriver() + await dispose() + await expect(prepared.agent.done).resolves.toBeUndefined() + expect(prepared.agent.session.events).toEqual([]) + await ctx.fiber.dispose() + }) + it('setting the same status does not emit agent/status again', async () => { const adapter = new MockAdapter([textResponse('ok')]) const ctx = await harness(adapter) @@ -324,7 +369,7 @@ describe('ReactLoopAgent', () => { // Covers the waiter's disposed arm: whenIdle() queues an internal waiter // while running (not the fast path), then the disposer settles it and chains // `done` (loop exit), not an eager resolve. A bare ReactLoopAgent + direct - // start() disposer keeps the emit synchronous. + // internal driver disposer keeps the emit synchronous. const ctx = new Context() await ctx.plugin(LlmService) await ctx.plugin(SessionStore) @@ -334,17 +379,19 @@ describe('ReactLoopAgent', () => { const adapter = new MockAdapter(['hang']) ctx.llm.registerAdapter(['mock'], adapter) const session = ctx.sessions.create(SessionId('bare')) - const agent = new ReactLoopAgent(ctx, AgentId('bare'), { model: 'mock' }, session) - const dispose = agent.start() + const prepared = prepareReactLoopAgent(ctx, AgentId('bare'), { model: 'mock' }, session) + const { agent } = prepared + prepared.markPublished() + const dispose = prepared.startDriver() agent.send([{ type: 'text', text: 'go' }]) await new Promise(r => setTimeout(r, 30)) expect(agent.status).toBe('running') const idle = agent.whenIdle() // queues an internal waiter (running) - dispose() // settles the waiter synchronously; whenIdle chains done + const disposal = dispose() // settles the waiter synchronously; whenIdle chains done await idle expect(agent.status).toBe('disposed') - await agent.done + await disposal }) it('whenIdle() subscribed while running survives a FIBER dispose (no hung promise)', async () => { @@ -409,7 +456,7 @@ describe('ReactLoopAgent', () => { expect(adapter.requests).toHaveLength(1) expect(agent.status).toBe('idle') - expect(warn).toHaveBeenCalledWith(expect.stringContaining('agent/status listener threw on running')) + expect(warn).toHaveBeenCalledWith(expect.stringContaining('agent event "agent/status" listener threw')) warn.mockRestore() }) @@ -427,7 +474,7 @@ describe('ReactLoopAgent', () => { expect(adapter.requests).toHaveLength(1) expect(agent.status).toBe('idle') - expect(warn).toHaveBeenCalledWith(expect.stringContaining('agent/status listener threw on idle')) + expect(warn).toHaveBeenCalledWith(expect.stringContaining('agent event "agent/status" listener threw')) warn.mockRestore() }) }) diff --git a/packages/core/agent-loop/tests/cancel.spec.ts b/packages/core/agent-loop/tests/cancel.spec.ts index 63a75447ca..56376b69a7 100644 --- a/packages/core/agent-loop/tests/cancel.spec.ts +++ b/packages/core/agent-loop/tests/cancel.spec.ts @@ -202,7 +202,7 @@ describe('Agent.cancel()', () => { await ctx.plugin(AgentLoop, { agents: [] }) ctx.llm.registerAdapter(['mock'], adapter) - const handle = ctx.agents.create({ + const handle = await ctx.agents.create({ agentId: AgentId('a-dispose-prefix'), sessionId: SessionId('dispose-prefix-session'), agentOptions: { model: 'mock' }, @@ -333,7 +333,7 @@ describe('Agent.cancel()', () => { await ctx.plugin(AgentLoop, { agents: [] }) ctx.llm.registerAdapter(['mock'], adapter) - const handle = ctx.agents.create({ + const handle = await ctx.agents.create({ agentId: AgentId('a-dispose-step-start'), sessionId: SessionId('dispose-step-start-session'), agentOptions: { model: 'mock' }, diff --git a/packages/core/agent-loop/tests/config-session-id.spec.ts b/packages/core/agent-loop/tests/config-session-id.spec.ts index a645fb3553..07d6cd9e9b 100644 --- a/packages/core/agent-loop/tests/config-session-id.spec.ts +++ b/packages/core/agent-loop/tests/config-session-id.spec.ts @@ -24,6 +24,24 @@ function waitForIdle(ctx: Context, agent: ReactLoopAgent): Promise { } describe('config-driven session id', () => { + it('identity-nests the deferred resume fiber under its labeled owner effect', async () => { + const ctx = new Context() + await ctx.plugin(LlmService) + await ctx.plugin(SessionStore) + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(AgentRegistry) + const loopFiber = await ctx.plugin(AgentLoop, { + agents: [{ id: AgentId('main'), model: 'mock', resumeSessionId: SessionId('deferred') }], + }) + + const resumeEffect = loopFiber.getEffects().find(effect => effect.label === 'agentLoop.resume(main)') + expect(resumeEffect?.children.map(child => child.label)).toEqual(['ctx.plugin()']) + expect(loopFiber.getEffects().filter(effect => effect.label === 'ctx.plugin()')).toEqual([]) + + await loopFiber.dispose() + }) + it('config-driven create uses a fresh ${id}-session- per run (restart-safe)', async () => { const root = await mkdtemp(join(tmpdir(), 'dsh-cfg-session-')) dirs.push(root) @@ -78,7 +96,7 @@ describe('config-driven session id', () => { await ctx1.plugin(AgentLoop, { agents: [] }) await ctx1.plugin(SessionPersistenceJsonl, { root }) ctx1.llm.registerAdapter(['mock'], new MockAdapter([textResponse('first')])) - const a1 = ctx1.agents.create({ agentId: AgentId('main'), sessionId: SessionId('sticky-1') }).agent as ReactLoopAgent + const a1 = (await ctx1.agents.create({ agentId: AgentId('main'), sessionId: SessionId('sticky-1') })).agent as ReactLoopAgent a1.send([{ type: 'text', text: 'remember me' }], { source: { kind: 'user' } }) await waitForIdle(ctx1, a1) await ctx1.fiber.dispose() diff --git a/packages/core/agent-loop/tests/coverage-edges.spec.ts b/packages/core/agent-loop/tests/coverage-edges.spec.ts index 7a044bfb57..23e5670f85 100644 --- a/packages/core/agent-loop/tests/coverage-edges.spec.ts +++ b/packages/core/agent-loop/tests/coverage-edges.spec.ts @@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest' import { Context } from 'cordis' import LlmService, { CallId, LlmError, StreamChunk } from '@deepseek-ai/dsh-llm' import SessionStore, { TurnEndReason } from '@deepseek-ai/dsh-session' +import type { SessionEvent } from '@deepseek-ai/dsh-session' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRegistry, { defineTool } from '@deepseek-ai/dsh-tools' import AgentRegistry, { AgentId } from '@deepseek-ai/dsh-agent' @@ -35,31 +36,24 @@ function send(agent: ReactLoopAgent, text: string) { agent.send([{ type: 'text', text }]) } -describe('turn boundary listener throws (handled in-turn, loop survives)', () => { - it('a pre-push turn/start failure (non-serializable source) is rethrown to the runLoop backstop', async () => { - // A non-serializable message source makes the turn/start append throw BEFORE - // the event is pushed (Session.append validates before push), so turn/start - // never enters the log. runTurn sees no logged turn/start and rethrows; the - // runLoop backstop reports via agent/error (step 0) + the logger and the - // driver survives. This is the ONLY path that reaches the backstop. - const adapter = new MockAdapter([textResponse('turn 2')]) +describe('inbox acceptance', () => { + it('rejects non-serializable content or source synchronously before notification or enqueue', async () => { + const adapter = new MockAdapter([textResponse('turn 1')]) const ctx = await harness(adapter) const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' }) + let queued = 0 + ctx.on('agent/queued', () => { queued += 1 }) - const errors: { turn: number; step: number; message: string }[] = [] - ctx.on('agent/error', (_a, turn, step, error) => void errors.push({ turn, step, message: error.message })) + expect(() => { + agent.send([{ type: 'text', text: 'first', bad: 1n } as never]) + }).toThrow(/losslessly JSON-serializable/) + expect(() => { + agent.send([{ type: 'text', text: 'first' }], { source: { kind: 'plugin', plugin: 'p', bad: 1n } as never }) + }).toThrow(/losslessly JSON-serializable/) + expect(queued).toBe(0) + expect(agent.session.events).toHaveLength(0) - // A non-serializable source (BigInt) on the queued message. - agent.send([{ type: 'text', text: 'first' }], { source: { kind: 'plugin', plugin: 'p', bad: 1n } as never }) - await waitForIdle(ctx, agent) - - expect(errors).toHaveLength(1) - expect(errors[0]!.step).toBe(0) - expect(errors[0]!.message).toMatch(/non-JSON-serializable/) - // No turn boundary was written (the turn/start append threw before push). - expect(agent.session.events.some(e => e.type === 'turn/start')).toBe(false) - - // loop survives: a well-formed second turn runs normally. + // The rejected value never woke or poisoned the loop; a valid message runs. send(agent, 'second') await waitForIdle(ctx, agent) expect(adapter.requests).toHaveLength(1) @@ -129,13 +123,15 @@ describe('tool JSON parse', () => { }) describe('toError normalization', () => { - it('normalizes non-Error throws from a turn/start session-event listener via toError', async () => { + it('normalizes non-Error throws from pre-commit dispatch validation via the runLoop backstop', async () => { const adapter = new MockAdapter([textResponse('ok')]) const ctx = await harness(adapter) const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' }) let threwOnce = false - ctx.on('session/event', (_session, event) => { + ctx.on('internal/dispatch', (_mode, name, args) => { + if (name !== 'session/event') return + const event = args[1] as SessionEvent if (event.type === 'turn/start' && !threwOnce) { threwOnce = true throw 'naked string error' // non-Error throw, normalized via toError @@ -148,11 +144,9 @@ describe('toError normalization', () => { send(agent, 'go') await waitForIdle(ctx, agent) expect(errors).toHaveLength(1) - expect(errors[0]!.message).toBe('naked string error') - // A non-Error throw is wrapped in a HarnessError with code UNKNOWN, so the - // turn-end error reason carries a routable code instead of degrading. - const turnEnd = agent.session.events.find(e => e.type === 'turn/end') - expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason.kind === 'error' && turnEnd.data.reason.code).toBe('UNKNOWN') + expect(errors[0]).toMatchObject({ message: 'naked string error', code: 'UNKNOWN' }) + expect(adapter.requests).toEqual([]) + expect(agent.session.events.some(event => event.type === 'turn/start' || event.type === 'turn/end')).toBe(false) }) it('normalizes non-Error throws from agent/request waterfall via inline toError in runStep catch', async () => { diff --git a/packages/core/agent-loop/tests/inbox.spec.ts b/packages/core/agent-loop/tests/inbox.spec.ts index 5406197b75..79a518ba2b 100644 --- a/packages/core/agent-loop/tests/inbox.spec.ts +++ b/packages/core/agent-loop/tests/inbox.spec.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { Inbox } from '@deepseek-ai/dsh-agent-loop' +import { Inbox } from '../src/inbox.ts' function resolverPair() { let r!: () => void diff --git a/packages/core/agent-loop/tests/interception.spec.ts b/packages/core/agent-loop/tests/interception.spec.ts index bab2ae6ea1..d4ec6312ba 100644 --- a/packages/core/agent-loop/tests/interception.spec.ts +++ b/packages/core/agent-loop/tests/interception.spec.ts @@ -311,6 +311,36 @@ describe('agent/session-start', () => { }) describe('agent/session-prefix', () => { + it('dispatches to global and matching agent-scope listeners only', async () => { + const adapter = new MockAdapter([textResponse('a done'), textResponse('b done')]) + const ctx = await harness(adapter) + const agentA = ctx.agentLoop.create(AgentId('prefix-a'), { model: 'mock' }) + const agentB = ctx.agentLoop.create(AgentId('prefix-b'), { model: 'mock' }) + const seen: string[] = [] + ctx.on('agent/session-prefix', async (agent, _prefix, _signal, next) => { + seen.push(`global:${agent.id}`) + return next() + }) + agentA.ctx.on('agent/session-prefix', async (agent, _prefix, _signal, next) => { + seen.push(`a:${agent.id}`) + return next() + }) + agentB.ctx.on('agent/session-prefix', async (agent, _prefix, _signal, next) => { + seen.push(`b:${agent.id}`) + return next() + }) + + send(agentA, 'run a') + await waitForIdle(ctx, agentA) + send(agentB, 'run b') + await waitForIdle(ctx, agentB) + + expect(seen).toEqual([ + 'global:prefix-a', 'a:prefix-a', + 'global:prefix-b', 'b:prefix-b', + ]) + }) + it('composes once per loop instance and fronts every request; the header records it; history stays untouched', async () => { const adapter = new MockAdapter([ toolCallResponse('c1', 'echo', { text: 'ping' }), diff --git a/packages/core/agent-loop/tests/loop.spec.ts b/packages/core/agent-loop/tests/loop.spec.ts index eebf56ee82..71be5b339e 100644 --- a/packages/core/agent-loop/tests/loop.spec.ts +++ b/packages/core/agent-loop/tests/loop.spec.ts @@ -168,7 +168,7 @@ describe('agent loop', () => { it('resolves {{cwd}} from the agent session workspace (factory create with meta.cwd)', async () => { const adapter = new MockAdapter([textResponse('ok')]) const ctx = await harness(adapter, 'Working in {{cwd}}.') - const handle = ctx.agents.create({ + const handle = await ctx.agents.create({ agentId: AgentId('a-cwd'), sessionId: SessionId('s-cwd'), meta: { cwd: '/work/space' }, @@ -243,6 +243,44 @@ describe('agent loop', () => { expect(adapter.requests[0]!.system).toBe('You are an AI agent powered by the DeepSeek Harness SDK.\n\nYou run on mock.') }) + it.each([ + ['BigInt', { n: 1n }], + ['Map', new Map([['key', 'value']])], + ['class instance', new (class ResultMeta { x = 1 })()], + ])('normalizes non-JSON tool meta (%s) before the durable result commit', async (_kind, meta) => { + const adapter = new MockAdapter([ + toolCallResponse('bad-meta-call', 'bad-meta', {}, 'calling'), + textResponse('recovered'), + ]) + const ctx = await harness(adapter) + ctx.tools.register(defineTool({ + name: 'bad-meta', + description: 'returns invalid durable metadata', + parameters: {}, + execute: () => Promise.resolve({ content: [{ type: 'text' as const, text: 'apparent success' }], meta }), + })) + const agent = ctx.agentLoop.create(AgentId('bad-meta-agent'), { model: 'mock' }) + + send(agent, 'use the tool') + await waitForIdle(ctx, agent) + + const result = agent.session.events.find(event => event.type === 'tool/result') + expect(result?.type).toBe('tool/result') + if (result?.type === 'tool/result') { + expect(result.data.callId).toBe('bad-meta-call') + expect(result.data.isError).toBe(true) + expect(result.data.meta).toBeUndefined() + expect(result.data.content).toEqual([{ + type: 'text', + text: 'Error: tool result must be losslessly JSON-serializable', + }]) + } + // The normalized failure was durably logged and fed back to the model; the + // turn continued normally instead of failing after an apparent success. + expect(adapter.requests).toHaveLength(2) + expect(JSON.stringify(adapter.requests[1]!.messages)).toContain('losslessly JSON-serializable') + }) + it('omits the system field when a system-prompt/assemble veto empties the assembly', async () => { // The documented escape valve: a deployment that must drop the harness // openers short-circuits the assemble waterfall; the request then carries @@ -772,10 +810,10 @@ describe('agent loop', () => { ]) }) - it('stops the turn when a step/end session-event listener failure has recorded an error', async () => { + it('contains a step/end observer failure without changing continuation', async () => { const adapter = new MockAdapter([ toolCallResponse('c1', 'echo', { text: 'x' }), - textResponse('should not run'), + textResponse('continued after tool call'), ]) const ctx = await harness(adapter) ctx.tools.register(defineTool({ @@ -788,9 +826,8 @@ describe('agent loop', () => { })) const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' }) let threw = false - // A throwing step/end session-event listener is the surviving boundary-listener - // failure path (step boundaries have no agent/* mirror): closeStep contains it - // and surfaces it as a turn error rather than stranding the turn open. + // Post-commit session observers cannot control the loop. The tool call still + // drives the second model request, and the turn completes normally. ctx.on('session/event', (_session, event) => { if (event.type === 'step/end' && !threw) { threw = true; throw new Error('bad step/end listener') } }) @@ -798,9 +835,9 @@ describe('agent loop', () => { send(agent, 'go') await waitForIdle(ctx, agent) - expect(adapter.requests).toHaveLength(1) + expect(adapter.requests).toHaveLength(2) const turnEnd = agent.session.events.findLast(e => e.type === 'turn/end') - expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason.kind).toBe('error') + expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason.kind).toBe('completed') }) it('chains queued messages into consecutive turns', async () => { @@ -916,6 +953,21 @@ describe('agent loop', () => { expect(adapter.requests).toHaveLength(1) }) + it('attaches config agent cwd to the fresh session header', async () => { + const ctx = new Context() + await ctx.plugin(LlmService) + await ctx.plugin(SessionStore) + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(AgentRegistry) + await ctx.plugin(AgentLoop, { + agents: [{ id: AgentId('config-agent'), model: 'mock', cwd: '/work/project' }], + }) + + const agent = ctx.agents.get(AgentId('config-agent'))! as ReactLoopAgent + expect(agent.session.header.cwd).toBe('/work/project') + }) + it('replays a session log into an identical derived history', async () => { const adapter = new MockAdapter([ toolCallResponse('c1', 'echo', { text: 'x' }), diff --git a/packages/core/agent-loop/tests/request-reconstruction.spec.ts b/packages/core/agent-loop/tests/request-reconstruction.spec.ts index f1d6b02ac4..bb6d613954 100644 --- a/packages/core/agent-loop/tests/request-reconstruction.spec.ts +++ b/packages/core/agent-loop/tests/request-reconstruction.spec.ts @@ -222,7 +222,7 @@ describe('request stability across the loop', () => { // one's full log (the resume/fork path). const adapter2 = new MockAdapter([textResponse('two')]) const ctx2 = await harness(adapter2) - const handle = ctx2.agents.create({ + const handle = await ctx2.agents.create({ agentId: AgentId('gen2'), sessionId: SessionId('gen2-session'), seed: [...agent.session.events], diff --git a/packages/core/agent-loop/tests/resume.spec.ts b/packages/core/agent-loop/tests/resume.spec.ts index 805f61d392..93605008ec 100644 --- a/packages/core/agent-loop/tests/resume.spec.ts +++ b/packages/core/agent-loop/tests/resume.spec.ts @@ -19,6 +19,10 @@ afterEach(async () => { for (const d of dirs.splice(0)) await rm(d, { recursive: async function persistentHarness(adapter: MockAdapter): Promise<{ ctx: Context; root: string }> { const root = await mkdtemp(join(tmpdir(), 'dsh-resume-')) dirs.push(root) + return { ctx: await mountPersistentHarness(root, adapter), root } +} + +async function mountPersistentHarness(root: string, adapter: MockAdapter): Promise { const ctx = new Context() await ctx.plugin(LlmService) await ctx.plugin(SessionStore) @@ -28,7 +32,22 @@ async function persistentHarness(adapter: MockAdapter): Promise<{ ctx: Context; await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(SessionPersistenceJsonl, { root }) ctx.llm.registerAdapter(['mock'], adapter) - return { ctx, root } + return ctx +} + +async function persistSession(sessionId: SessionId): Promise { + const { ctx, root } = await persistentHarness(new MockAdapter([textResponse('seed')])) + // Persistence deliberately has no artifact for a truly empty session. A + // balanced completed turn is the smallest resumable log and avoids running + // the model merely to construct this lifecycle fixture. + const seed: SessionEvent[] = [ + { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } }, + { type: 'turn/end', seq: 1, time: 2, data: { turn: 1, reason: { kind: 'completed' } } }, + ] + const session = ctx.sessions.create(sessionId, { seed }) + await ctx.sessions.flush(session) + await ctx.fiber.dispose() + return root } function waitForIdle(ctx: Context, agent: ReactLoopAgent): Promise { @@ -39,11 +58,44 @@ function waitForIdle(ctx: Context, agent: ReactLoopAgent): Promise { }) } +/** Fail a lifecycle regression promptly instead of waiting for Vitest's suite timeout. */ +async function promptly(task: Promise): Promise { + const timeout = Promise.withResolvers() + const timer = setTimeout(() => { timeout.reject(new Error('lifecycle task did not settle promptly')) }, 1000) + try { + return await Promise.race([task, timeout.promise]) + } finally { + clearTimeout(timer) + } +} + +/** Throw an arbitrary callback value to exercise the public unknown-error boundary. */ +function throwUnknown(value: unknown): never { + throw value +} + describe('the session-persistence RFC: AgentLoop factory create/resume', () => { + it('normalizes a non-Error resume publication failure for rollback and rethrows it', async () => { + const sessionId = SessionId('unknown-resume-failure-s') + const root = await persistSession(sessionId) + const ctx = await mountPersistentHarness(root, new MockAdapter([textResponse('next')])) + const failure = { source: 'resume' } + ctx.on('session/created', () => throwUnknown(failure)) + + await expect(ctx.agents.resume({ + agentId: AgentId('unknown-resume-failure'), + resumeSessionId: sessionId, + })).rejects.toBe(failure) + + expect(ctx.agents.get(AgentId('unknown-resume-failure'))).toBeUndefined() + expect(ctx.sessions.get(sessionId)).toBeUndefined() + await ctx.fiber.dispose() + }) + it('createAgent uses the caller-supplied sessionId (not ${id}-session)', async () => { const adapter = new MockAdapter([textResponse('hi')]) const { ctx } = await persistentHarness(adapter) - const { agent } = ctx.agents.create({ agentId: AgentId('a1'), sessionId: SessionId('custom-session'), meta: { cwd: '/w' } }) + const { agent } = await ctx.agents.create({ agentId: AgentId('a1'), sessionId: SessionId('custom-session'), meta: { cwd: '/w' } }) expect(agent.session.id).toBe('custom-session') expect(agent.session.header.cwd).toBe('/w') await ctx.fiber.dispose() @@ -52,10 +104,10 @@ describe('the session-persistence RFC: AgentLoop factory create/resume', () => { it('createAgent rejects a duplicate agent id BEFORE creating the session (no orphan)', async () => { const adapter = new MockAdapter([textResponse('hi')]) const { ctx } = await persistentHarness(adapter) - ctx.agents.create({ agentId: AgentId('dup'), sessionId: SessionId('sess-a') }) + await ctx.agents.create({ agentId: AgentId('dup'), sessionId: SessionId('sess-a') }) // A second create with the SAME agent id but a fresh session id must reject // up front — and must NOT leave an orphaned 'sess-b' session behind. - expect(() => ctx.agents.create({ agentId: AgentId('dup'), sessionId: SessionId('sess-b') })).toThrow(/already registered/) + await expect(ctx.agents.create({ agentId: AgentId('dup'), sessionId: SessionId('sess-b') })).rejects.toThrow(/already registered/) expect(ctx.sessions.get(SessionId('sess-b'))).toBeUndefined() await ctx.fiber.dispose() }) @@ -63,7 +115,7 @@ describe('the session-persistence RFC: AgentLoop factory create/resume', () => { it('createAgent works without meta (no cwd)', async () => { const adapter = new MockAdapter([textResponse('hi')]) const { ctx } = await persistentHarness(adapter) - const { agent } = ctx.agents.create({ agentId: AgentId('a-nometa'), sessionId: SessionId('nometa-session') }) + const { agent } = await ctx.agents.create({ agentId: AgentId('a-nometa'), sessionId: SessionId('nometa-session') }) expect(agent.session.id).toBe('nometa-session') expect(agent.session.header.cwd).toBeUndefined() await ctx.fiber.dispose() @@ -73,7 +125,7 @@ describe('the session-persistence RFC: AgentLoop factory create/resume', () => { // Lifecycle 1: create a no-cwd session and run a turn. const adapter1 = new MockAdapter([textResponse('a')]) const { ctx: ctx1, root } = await persistentHarness(adapter1) - const a1 = ctx1.agents.create({ agentId: AgentId('m'), sessionId: SessionId('nocwd-sess') }).agent as ReactLoopAgent + const a1 = (await ctx1.agents.create({ agentId: AgentId('m'), sessionId: SessionId('nocwd-sess') })).agent as ReactLoopAgent a1.send([{ type: 'text', text: 'q' }], { source: { kind: 'user' } }) await waitForIdle(ctx1, a1) await ctx1.fiber.dispose() @@ -100,7 +152,7 @@ describe('the session-persistence RFC: AgentLoop factory create/resume', () => { const { ctx: ctx1, root } = await persistentHarness(adapter1) const sources1: string[] = [] ctx1.on('agent/session-start', (_agent, source) => void sources1.push(source)) - const a1 = ctx1.agents.create({ agentId: AgentId('s'), sessionId: SessionId('start-sess') }).agent as ReactLoopAgent + const a1 = (await ctx1.agents.create({ agentId: AgentId('s'), sessionId: SessionId('start-sess') })).agent as ReactLoopAgent expect(sources1).toEqual(['startup']) a1.send([{ type: 'text', text: 'q' }], { source: { kind: 'user' } }) await waitForIdle(ctx1, a1) @@ -124,6 +176,250 @@ describe('the session-persistence RFC: AgentLoop factory create/resume', () => { await ctx2.fiber.dispose() }) + it('resume awaits setup while unpublished, then publishes a fully composed world in order', async () => { + const sessionId = SessionId('resume-setup-success') + const root = await persistSession(sessionId) + const ctx = await mountPersistentHarness(root, new MockAdapter([textResponse('next')])) + const gate = Promise.withResolvers() + const setupStarted = Promise.withResolvers() + const order: string[] = [] + + ctx.on('session/created', (session) => { + expect(ctx.sessions.get(session.id)).toBe(session) + expect(ctx.agents.get(AgentId('resumed-atomic'))?.session).toBe(session) + order.push('session/created') + }) + ctx.on('agent/created', (agent) => { + expect(agent.status).toBe('idle') + order.push('agent/created') + }) + ctx.on('agent/session-start', (agent) => { + expect(() => { agent.cancel('now live') }).not.toThrow() + order.push('agent/session-start') + }) + + const resuming = ctx.agents.resume({ + agentId: AgentId('resumed-atomic'), + resumeSessionId: sessionId, + agentOptions: { model: 'mock' }, + setup: async (agentCtx) => { + expect(agentCtx.agent?.id).toBe(AgentId('resumed-atomic')) + expect(agentCtx.agent?.session.events).toHaveLength(2) + agentCtx.on('session/created', () => void order.push('setup-listener:session/created')) + agentCtx.on('agent/created', () => void order.push('setup-listener:agent/created')) + order.push('setup:start') + setupStarted.resolve(undefined) + await gate.promise + order.push('setup:end') + }, + }) + + await setupStarted.promise + expect(ctx.agents.get(AgentId('resumed-atomic'))).toBeUndefined() + expect(ctx.sessions.get(sessionId)).toBeUndefined() + expect(order).toEqual(['setup:start']) + + gate.resolve(undefined) + const handle = await resuming + expect(order).toEqual([ + 'setup:start', + 'setup:end', + 'session/created', + 'setup-listener:session/created', + 'agent/created', + 'setup-listener:agent/created', + 'agent/session-start', + ]) + await handle.dispose() + await ctx.fiber.dispose() + }) + + it('successful resume disposal retires its caller-owned transaction effects', async () => { + const sessionId = SessionId('resume-retired-effects-s') + const agentId = AgentId('resume-retired-effects') + const root = await persistSession(sessionId) + const ctx = await mountPersistentHarness(root, new MockAdapter([textResponse('next')])) + const handle = await ctx.agents.resume({ + agentId, + resumeSessionId: sessionId, + agentOptions: { model: 'mock' }, + }) + const transactionLabels = [ + `agentLoop.owner(${agentId})`, + `agentLoop.lifecycle(${agentId})`, + ] + + expect(ctx.fiber.getEffects().map(effect => effect.label)).toEqual(expect.arrayContaining(transactionLabels)) + await handle.dispose() + expect(ctx.fiber.getEffects().filter(effect => transactionLabels.includes(effect.label))).toEqual([]) + await ctx.fiber.dispose() + }) + + it('resume setup rejection publishes nothing, unwinds, and releases both identities', async () => { + const sessionId = SessionId('resume-setup-reject') + const root = await persistSession(sessionId) + const ctx = await mountPersistentHarness(root, new MockAdapter([textResponse('next')])) + const published: string[] = [] + ctx.on('session/created', () => void published.push('session/created')) + ctx.on('agent/created', () => void published.push('agent/created')) + ctx.on('agent/session-start', () => void published.push('agent/session-start')) + + await expect(ctx.agents.resume({ + agentId: AgentId('resume-reject'), + resumeSessionId: sessionId, + agentOptions: { model: 'mock' }, + setup: async () => { + await Promise.resolve() + throw new Error('resume setup failed') + }, + })).rejects.toThrow('resume setup failed') + + expect(published).toEqual([]) + expect(ctx.agents.get(AgentId('resume-reject'))).toBeUndefined() + expect(ctx.sessions.get(sessionId)).toBeUndefined() + const retry = await ctx.agents.resume({ + agentId: AgentId('resume-reject'), + resumeSessionId: sessionId, + agentOptions: { model: 'mock' }, + }) + await retry.dispose() + await ctx.fiber.dispose() + }) + + it('owner unload aborts resume setup and cannot publish after the callback settles', async () => { + const sessionId = SessionId('resume-setup-owner-unload') + const root = await persistSession(sessionId) + const ctx = await mountPersistentHarness(root, new MockAdapter([textResponse('next')])) + const gate = Promise.withResolvers() + const setupStarted = Promise.withResolvers() + const published: string[] = [] + ctx.on('session/created', () => void published.push('session/created')) + ctx.on('agent/created', () => void published.push('agent/created')) + + let resuming!: ReturnType + const owner = await ctx.plugin(Object.assign((inner: Context) => { + resuming = inner.agents.resume({ + agentId: AgentId('resume-owner-race'), + resumeSessionId: sessionId, + agentOptions: { model: 'mock' }, + setup: async () => { + setupStarted.resolve(undefined) + await gate.promise + }, + }) + }, { inject: ['agents'] })) + await setupStarted.promise + + await owner.dispose() + await expect(resuming).rejects.toThrow(/owner disposed during setup/) + expect(published).toEqual([]) + expect(ctx.agents.get(AgentId('resume-owner-race'))).toBeUndefined() + expect(ctx.sessions.get(sessionId)).toBeUndefined() + + gate.resolve(undefined) + await Promise.resolve() + expect(published).toEqual([]) + await ctx.fiber.dispose() + }) + + it('owner unload aborts a never-settling persistence load, releases identities, and blocks late publication', async () => { + const sessionId = SessionId('resume-load-owner-unload') + const agentId = AgentId('resume-load-race') + const root = await persistSession(sessionId) + const ctx = await mountPersistentHarness(root, new MockAdapter([textResponse('next')])) + const snapshot = await ctx.sessionPersistence.load(sessionId) + const lateLoad = Promise.withResolvers() + const loadStarted = Promise.withResolvers() + let loads = 0 + ctx.sessionPersistence.load = (id) => { + expect(id).toBe(sessionId) + loads += 1 + if (loads === 1) { + loadStarted.resolve(undefined) + return lateLoad.promise + } + return Promise.resolve(structuredClone(snapshot)) + } + + const published: string[] = [] + ctx.on('session/created', () => void published.push('session/created')) + ctx.on('agent/created', () => void published.push('agent/created')) + ctx.on('agent/session-start', () => void published.push('agent/session-start')) + + let resuming!: ReturnType + const owner = await ctx.plugin(Object.assign((inner: Context) => { + resuming = inner.agents.resume({ agentId, resumeSessionId: sessionId, agentOptions: { model: 'mock' } }) + }, { inject: ['agents'] })) + await loadStarted.promise + + const rejection = expect(promptly(resuming)).rejects.toThrow(/owner disposed during setup/) + await promptly(owner.dispose()) + expect(published).toEqual([]) + expect(ctx.agents.get(agentId)).toBeUndefined() + expect(ctx.sessions.get(sessionId)).toBeUndefined() + + // owner.dispose() awaited transaction settlement, so the same identities + // can be reused before awaiting the public rejection. + const retry = await promptly(ctx.agents.resume({ agentId, resumeSessionId: sessionId, agentOptions: { model: 'mock' } })) + await rejection + expect(loads).toBe(2) + expect(published).toEqual(['session/created', 'agent/created', 'agent/session-start']) + + // Settlement of the abandoned backend promise cannot resume the old + // transaction or emit a second publication after the retry owns the ids. + lateLoad.resolve(structuredClone(snapshot)) + await Promise.resolve() + await Promise.resolve() + expect(ctx.agents.get(agentId)).toBe(retry.agent) + expect(ctx.sessions.get(sessionId)).toBe(retry.agent.session) + expect(published).toEqual(['session/created', 'agent/created', 'agent/session-start']) + + await retry.dispose() + await ctx.fiber.dispose() + }) + + it('AgentLoop unload aborts persistence load and awaits wrapper settlement', async () => { + const sessionId = SessionId('resume-load-factory-unload') + const agentId = AgentId('resume-load-factory-race') + const root = await persistSession(sessionId) + const ctx = new Context() + await ctx.plugin(LlmService) + await ctx.plugin(SessionStore) + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(AgentRegistry) + const loopFiber = await ctx.plugin(AgentLoop, { agents: [] }) + await ctx.plugin(SessionPersistenceJsonl, { root }) + ctx.llm.registerAdapter(['mock'], new MockAdapter([textResponse('next')])) + + const snapshot = await ctx.sessionPersistence.load(sessionId) + const lateLoad = Promise.withResolvers() + const loadStarted = Promise.withResolvers() + ctx.sessionPersistence.load = (id) => { + expect(id).toBe(sessionId) + loadStarted.resolve(undefined) + return lateLoad.promise + } + const published: string[] = [] + ctx.on('session/created', () => void published.push('session/created')) + ctx.on('agent/created', () => void published.push('agent/created')) + + const resuming = ctx.agents.resume({ agentId, resumeSessionId: sessionId, agentOptions: { model: 'mock' } }) + await loadStarted.promise + const rejection = expect(promptly(resuming)).rejects.toThrow(/agent loop is not active/) + await promptly(loopFiber.dispose()) + await rejection + + expect(published).toEqual([]) + expect(ctx.agents.get(agentId)).toBeUndefined() + expect(ctx.sessions.get(sessionId)).toBeUndefined() + lateLoad.resolve(structuredClone(snapshot)) + await Promise.resolve() + await Promise.resolve() + expect(published).toEqual([]) + await ctx.fiber.dispose() + }) + it('resume of a forked session preserves the parentSession lineage and seed boundary in the header', async () => { // Lifecycle 1: persist a FORKED session (carries parentSession + seedLength // in its header) by creating it with a complete-turn seed — the write path @@ -170,7 +466,7 @@ describe('the session-persistence RFC: AgentLoop factory create/resume', () => { // disk, since a crash before the next turn would otherwise lose it. const adapter1 = new MockAdapter([textResponse('answer')]) const { ctx: ctx1, root } = await persistentHarness(adapter1) - const a1 = ctx1.agents.create({ agentId: AgentId('m'), sessionId: SessionId('inject-sess'), meta: { cwd: '/w' } }).agent as ReactLoopAgent + const a1 = (await ctx1.agents.create({ agentId: AgentId('m'), sessionId: SessionId('inject-sess'), meta: { cwd: '/w' } })).agent as ReactLoopAgent a1.send([{ type: 'text', text: 'q' }], { source: { kind: 'user' } }) await waitForIdle(ctx1, a1) a1.inject([{ type: 'text', text: 'background task 42 finished' }], { source: { kind: 'plugin', plugin: 'tool-bash' } }) @@ -195,7 +491,7 @@ describe('the session-persistence RFC: AgentLoop factory create/resume', () => { // drop it on reload (the bug this guards). const adapter1 = new MockAdapter([textResponse('answer')]) const { ctx: ctx1, root } = await persistentHarness(adapter1) - const a1 = ctx1.agents.create({ agentId: AgentId('m'), sessionId: SessionId('inject-sess'), meta: { cwd: '/w' } }).agent as ReactLoopAgent + const a1 = (await ctx1.agents.create({ agentId: AgentId('m'), sessionId: SessionId('inject-sess'), meta: { cwd: '/w' } })).agent as ReactLoopAgent a1.send([{ type: 'text', text: 'q' }], { source: { kind: 'user' } }) await waitForIdle(ctx1, a1) a1.inject([{ type: 'text', text: 'background task 42 finished' }], { source: { kind: 'plugin', plugin: 'tool-bash' } }) @@ -223,7 +519,7 @@ describe('the session-persistence RFC: AgentLoop factory create/resume', () => { // Lifecycle 1: run one full turn, persisting it. const adapter1 = new MockAdapter([textResponse('first answer')]) const { ctx: ctx1, root } = await persistentHarness(adapter1) - const a1 = ctx1.agents.create({ agentId: AgentId('main'), sessionId: SessionId('sess-resume'), meta: { cwd: '/w' } }).agent as ReactLoopAgent + const a1 = (await ctx1.agents.create({ agentId: AgentId('main'), sessionId: SessionId('sess-resume'), meta: { cwd: '/w' } })).agent as ReactLoopAgent a1.send([{ type: 'text', text: 'first question' }], { source: { kind: 'user' } }) await waitForIdle(ctx1, a1) const events1 = [...a1.session.events] diff --git a/packages/core/agent-loop/tests/review-fixes.spec.ts b/packages/core/agent-loop/tests/review-fixes.spec.ts index e487a2c428..5898a82ee2 100644 --- a/packages/core/agent-loop/tests/review-fixes.spec.ts +++ b/packages/core/agent-loop/tests/review-fixes.spec.ts @@ -1,18 +1,16 @@ import { describe, expect, it } from 'vitest' import { Context } from 'cordis' -import LlmService, { CallId, MessageSource, StreamChunk } from '@deepseek-ai/dsh-llm' +import LlmService, { CallId, ContentBlock, MessageSource, StreamChunk } from '@deepseek-ai/dsh-llm' import SessionStore, { Session, SessionEvent, SessionId, TurnEndReason } from '@deepseek-ai/dsh-session' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRegistry, { defineTool } from '@deepseek-ai/dsh-tools' import AgentRegistry, { AgentId, type ContinuationDecision } from '@deepseek-ai/dsh-agent' import AgentLoop, { ReactLoopAgent } from '@deepseek-ai/dsh-agent-loop' +import { prepareReactLoopAgent } from '../src/agent.ts' import * as Invariants from '@deepseek-ai/dsh-invariants' import { MockAdapter, textResponse, toolCallResponse } from './mock-adapter.ts' -/** - * Regression tests for the findings of the first architecture review - * (Codex + sub-agent, post phase-1). Each describe block names the finding. - */ +/** Regression tests for agent-loop boundary, identity, and lifecycle contracts. */ async function harness(adapter: MockAdapter) { const ctx = new Context() @@ -436,6 +434,94 @@ describe('MEDIUM: misc registry and config fixes', () => { const steeringSources = agent.session.events.flatMap(e => e.type === 'steering/message' ? [e.data.source] : []) expect(steeringSources).toEqual([{ kind: 'plugin', plugin: 'goal' }]) }) + + it('send() owns content and source before notification and delivery', async () => { + const adapter = new MockAdapter([textResponse('done')]) + const ctx = await harness(adapter) + const agent = ctx.agentLoop.create(AgentId('owned-send'), { model: 'mock' }) + const content = [{ type: 'text' as const, text: 'accepted-send' }] + const source = { kind: 'plugin' as const, plugin: 'accepted-source' } + let notifiedContent: ContentBlock[] | undefined + let notifiedSource: MessageSource | undefined + ctx.on('agent/queued', (subject, acceptedContent, info) => { + if (subject !== agent || info.steering) return + // Retain the exact notification references: cloning here would test the + // listener's copy rather than the event/inbox ownership boundary. + notifiedContent = acceptedContent + notifiedSource = info.source + }) + + agent.send(content, { source }) + content[0]!.text = 'caller-mutated-send' + source.plugin = 'caller-mutated-source' + await waitForIdle(ctx, agent) + + expect(notifiedContent).toEqual([{ type: 'text', text: 'accepted-send' }]) + expect(notifiedSource).toEqual({ kind: 'plugin', plugin: 'accepted-source' }) + expect(Object.isFrozen(notifiedContent)).toBe(true) + expect(Object.isFrozen(notifiedContent?.[0])).toBe(true) + expect(Object.isFrozen(notifiedSource)).toBe(true) + const recorded = agent.session.events.flatMap(event => event.type === 'user/message' ? [event.data] : []) + expect(recorded).toContainEqual({ + content: [{ type: 'text', text: 'accepted-send' }], + source: { kind: 'plugin', plugin: 'accepted-source' }, + }) + const request = JSON.stringify(adapter.requests[0]!.messages) + expect(request).toContain('accepted-send') + expect(request).not.toContain('caller-mutated-send') + }) + + it('running steer() owns content and source before notification and delivery', async () => { + const adapter = new MockAdapter([toolCallResponse('c1', 'gate', {}), textResponse('done')]) + const ctx = await harness(adapter) + const agent = ctx.agentLoop.create(AgentId('owned-steer'), { model: 'mock' }) + const entered = Promise.withResolvers() + const release = Promise.withResolvers() + ctx.tools.register(defineTool({ + name: 'gate', + description: '', + parameters: {}, + async execute() { + entered.resolve(undefined) + await release.promise + return [{ type: 'text', text: 'tool done' }] + }, + })) + let notifiedContent: ContentBlock[] | undefined + let notifiedSource: MessageSource | undefined + ctx.on('agent/queued', (subject, acceptedContent, info) => { + if (subject !== agent || !info.steering) return + notifiedContent = acceptedContent + notifiedSource = info.source + }) + + agent.send([{ type: 'text', text: 'start' }]) + await entered.promise + expect(agent.status).toBe('running') + const content = [{ type: 'text' as const, text: 'accepted-steer' }] + const source = { kind: 'plugin' as const, plugin: 'accepted-source' } + agent.steer(content, { source }) + content[0]!.text = 'caller-mutated-steer' + source.plugin = 'caller-mutated-source' + const idle = waitForIdle(ctx, agent) + release.resolve(undefined) + await idle + + expect(notifiedContent).toEqual([{ type: 'text', text: 'accepted-steer' }]) + expect(notifiedSource).toEqual({ kind: 'plugin', plugin: 'accepted-source' }) + expect(Object.isFrozen(notifiedContent)).toBe(true) + expect(Object.isFrozen(notifiedContent?.[0])).toBe(true) + expect(Object.isFrozen(notifiedSource)).toBe(true) + const recorded = agent.session.events.flatMap(event => event.type === 'steering/message' ? [event.data] : []) + expect(recorded).toContainEqual({ + turn: 1, + content: [{ type: 'text', text: 'accepted-steer' }], + source: { kind: 'plugin', plugin: 'accepted-source' }, + }) + const request = JSON.stringify(adapter.requests[1]!.messages) + expect(request).toContain('accepted-steer') + expect(request).not.toContain('caller-mutated-steer') + }) }) describe('MEDIUM: turn numbering continues across seeded (forked) sessions', () => { @@ -458,8 +544,10 @@ describe('MEDIUM: turn numbering continues across seeded (forked) sessions', () ctx2.llm.registerAdapter(['mock'], second) const seeded = ctx2.sessions.create(SessionId('forked'), { seed: [...agent.session.events] }) - const forked = new ReactLoopAgent(ctx2, AgentId('forked-agent'), { model: 'mock' }, seeded) - ctx2.effect(() => forked.start()) + const prepared = prepareReactLoopAgent(ctx2, AgentId('forked-agent'), { model: 'mock' }, seeded) + const forked = prepared.agent + prepared.markPublished() + ctx2.effect(() => prepared.startDriver()) const turns: number[] = [] ctx2.on('session/event', (_s, event) => { if (event.type === 'turn/start') turns.push(event.data.turn) }) @@ -558,7 +646,7 @@ describe('HIGH: a finish-error stream chunk ends the turn as error, not complete }) }) -describe('P1-6: a step/start session-event listener sees the event already in the log', () => { +describe('step boundary publication order', () => { it('the step/start event is in session.events when its session/event listener fires', async () => { const adapter = new MockAdapter([textResponse('done')]) const ctx = await harness(adapter) @@ -589,7 +677,7 @@ describe('P1-6: a step/start session-event listener sees the event already in th }) }) -describe('P1-5: a started turn (and any open step) is always closed on a boundary throw', () => { +describe('turn and step boundary recovery', () => { // Harness with the invariants plugin loaded as an oracle: it throws on // append if the log goes unbalanced (turn/end while a step is open, // turn/start while a turn is open, etc.), so a regression surfaces as an @@ -602,7 +690,7 @@ describe('P1-5: a started turn (and any open step) is always closed on a boundar await ctx.plugin(ToolRegistry) await ctx.plugin(AgentRegistry) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(Invariants, { freeze: false }) + await ctx.plugin(Invariants) ctx.llm.registerAdapter(['mock'], adapter) return ctx } @@ -620,19 +708,13 @@ describe('P1-5: a started turn (and any open step) is always closed on a boundar } } - it('a throwing step/start session-event listener closes the open step then the turn (step/end before turn/end)', async () => { - const adapter = new MockAdapter([textResponse('never reached')]) + it('a throwing step/start observer cannot change a successful turn', async () => { + const adapter = new MockAdapter([textResponse('request completed')]) const ctx = await balancedHarness(adapter) const agent = ctx.agentLoop.create(AgentId('a-stepstart'), { model: 'mock' }) - // Step boundaries have no agent/* mirror; a throwing step/start session-event - // listener is the surviving step-boundary-listener failure. The loop marks - // the step open BEFORE appending step/start (Session.append pushes before - // notifying, so a post-push listener throw still leaves stepOpen=true), so - // the outer catch's closeStep() appends the balancing step/end — the turn - // stays enclosed. The invariants oracle (balancedHarness) rejects any - // imbalance, so a green run proves turn/start → step/start → step/end → - // turn/end nesting holds. + // Session owns post-commit containment. The loop sees a successful append, + // runs the request, and balances the ordinary step and turn boundaries. let threw = false ctx.on('session/event', (_s, event) => { if (event.type === 'step/start' && !threw) { threw = true; throw new Error('boom step-start') } @@ -645,8 +727,8 @@ describe('P1-5: a started turn (and any open step) is always closed on a boundar const e = [...agent.session.events] const c = boundaryCounts(agent) - expect(c).toMatchObject({ turnStart: 1, turnEnd: 1, stepStart: 1, stepEnd: 1, errors: 1 }) - expect(errors.map(x => x.message)).toEqual(['boom step-start']) + expect(c).toMatchObject({ turnStart: 1, turnEnd: 1, stepStart: 1, stepEnd: 1, errors: 0 }) + expect(errors).toEqual([]) // step/end precedes turn/end (the invariants oracle would reject // turn/end-while-step-open, but assert the order explicitly too). const stepEndIdx = e.findIndex(x => x.type === 'step/end') @@ -655,6 +737,101 @@ describe('P1-5: a started turn (and any open step) is always closed on a boundar expect(stepEndIdx).toBeLessThan(turnEndIdx) }) + it('a pre-commit step/start validation failure does not invent a step boundary', async () => { + const adapter = new MockAdapter([textResponse('never reached')]) + const ctx = await balancedHarness(adapter) + const agent = ctx.agentLoop.create(AgentId('a-stepstart-veto'), { model: 'mock' }) + let rejected = false + ctx.on('internal/dispatch', (_mode, name, args) => { + if (name !== 'session/event') return + const event = args[1] as SessionEvent + if (event.type === 'step/start' && !rejected) { + rejected = true + throw new Error('reject step-start before commit') + } + }) + const errors: Error[] = [] + ctx.on('agent/error', (_agent, _turn, _step, error) => { errors.push(error) }) + + send(agent, 'go') + await waitForIdle(ctx, agent) + + expect(adapter.requests).toEqual([]) + expect(boundaryCounts(agent)).toMatchObject({ + turnStart: 1, + turnEnd: 1, + stepStart: 0, + stepEnd: 0, + errors: 1, + }) + expect(errors.map(error => error.message)).toEqual(['reject step-start before commit']) + }) + + it('a one-shot turn/end validation failure preserves the earlier turn error on retry', async () => { + const errorStream: StreamChunk[] = [{ type: 'finish', reason: { kind: 'error', message: 'provider failed' } }] + const adapter = new MockAdapter([errorStream]) + const ctx = await balancedHarness(adapter) + const agent = ctx.agentLoop.create(AgentId('a-turnend-veto'), { model: 'mock' }) + let rejected = false + ctx.on('internal/dispatch', (_mode, name, args) => { + if (name !== 'session/event') return + const event = args[1] as SessionEvent + if (event.type === 'turn/end' && !rejected) { + rejected = true + throw new Error('reject first turn-end') + } + }) + const errors: Error[] = [] + ctx.on('agent/error', (_agent, _turn, _step, error) => { errors.push(error) }) + + send(agent, 'go') + await waitForIdle(ctx, agent) + + expect(errors.map(error => error.message)).toEqual(['provider failed']) + expect(boundaryCounts(agent)).toMatchObject({ + turnStart: 1, + turnEnd: 1, + stepStart: 1, + stepEnd: 1, + errors: 1, + }) + const turnEnd = agent.session.events.findLast(event => event.type === 'turn/end') + expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason).toMatchObject({ + kind: 'error', + message: 'provider failed', + }) + }) + + it('a one-shot step/end validation failure keeps the step open until retry succeeds', async () => { + const adapter = new MockAdapter([textResponse('completed before close validation')]) + const ctx = await balancedHarness(adapter) + const agent = ctx.agentLoop.create(AgentId('a-stepend-veto'), { model: 'mock' }) + let rejected = false + ctx.on('internal/dispatch', (_mode, name, args) => { + if (name !== 'session/event') return + const event = args[1] as SessionEvent + if (event.type === 'step/end' && !rejected) { + rejected = true + throw new Error('reject first step-end') + } + }) + const errors: Error[] = [] + ctx.on('agent/error', (_agent, _turn, _step, error) => { errors.push(error) }) + + send(agent, 'go') + await waitForIdle(ctx, agent) + + expect(adapter.requests).toHaveLength(1) + expect(errors.map(error => error.message)).toEqual(['reject first step-end']) + expect(boundaryCounts(agent)).toMatchObject({ + turnStart: 1, + turnEnd: 1, + stepStart: 1, + stepEnd: 1, + errors: 1, + }) + }) + it('a throwing agent/error listener during a step-error path still balances the turn, loop survives', async () => { // First turn: model stream ends with a finish-error → step error path → // failTurn emits agent/error, whose listener throws. The turn must still @@ -717,13 +894,9 @@ describe('P1-5: a started turn (and any open step) is always closed on a boundar }) it('preserves reason disposed when a pre-step listener disposes then throws (outer-catch disposed branch)', async () => { - // Reach the OUTER catch while disposed: an `agent/pre-step` listener requests - // disposal AND throws. The throw escapes the pre-step `await` (line ~419) to - // the loop's outer catch — BEFORE the post-pre-step disposal check at ~422 - // gets to run — so the catch sees `isDisposed() && !errorReported` and must - // PRESERVE reason=disposed rather than overwrite it with the listener's throw - // (disposal is not a failure). This is the surviving path to that sub-branch - // now that there is no turn-boundary emit to throw from. + // A pre-step listener requests disposal and then throws before the ordinary + // post-listener disposal check. The outer catch sees disposal already won + // and must preserve reason=disposed rather than rewrite it as a plugin error. const adapter = new MockAdapter([textResponse('never reached')]) const ctx = await balancedHarness(adapter) let agent!: ReactLoopAgent @@ -759,16 +932,8 @@ describe('P1-5: a started turn (and any open step) is always closed on a boundar expect(errorEmits).toHaveLength(0) }) - it('a throwing session/event listener on the turn/start append still balances the turn', async () => { - // Session.append pushes the event BEFORE notifying session/event listeners, - // so a listener throwing on turn/start leaves turn/start IN THE LOG. The - // loop must therefore still owe (and append) a turn/end — deciding "owed" - // from the log via isTurnOpen, not a "turn started" flag that the throw - // skipped. Otherwise the turn stays permanently open and poisons the next - // turn/replay (the turn-enclosure RFC). (Uses the plain harness — NOT the invariants - // oracle — because the throwing listener is itself a session/event - // subscriber.) - const adapter = new MockAdapter([textResponse('turn 2')]) + it('a throwing turn/start observer cannot starve the loop or later turns', async () => { + const adapter = new MockAdapter([textResponse('turn 1'), textResponse('turn 2')]) const ctx = await harness(adapter) const agent = ctx.agentLoop.create(AgentId('a-preturn'), { model: 'mock' }) @@ -782,12 +947,9 @@ describe('P1-5: a started turn (and any open step) is always closed on a boundar send(agent, 'go') await waitForIdle(ctx, agent) - // The error was surfaced exactly once via agent/error. - expect(errors.map(e => e.message)).toEqual(['boom turn/start append']) - // The turn is BALANCED: turn/start is in the log (it was pushed before the - // listener threw), so a turn/end was owed and appended — no open turn. The - // last turn-boundary event being turn/end is exactly the loop's isTurnOpen - // check (no open turn remains). + expect(errors).toEqual([]) + // Session contains the observer failure per listener, so the committed turn + // remains visible to later observers and executes normally. const types = [...agent.session.events].map(e => e.type) expect(types.filter(t => t === 'turn/start')).toHaveLength(1) expect(types.filter(t => t === 'turn/end')).toHaveLength(1) @@ -798,15 +960,10 @@ describe('P1-5: a started turn (and any open step) is always closed on a boundar // loop survives: a second turn runs normally. send(agent, 'second') await waitForIdle(ctx, agent) - expect(adapter.requests).toHaveLength(1) + expect(adapter.requests).toHaveLength(2) }) - it('a throwing step/end session-event listener during a successful step ends the turn as error, not completed', async () => { - // closeStep() must surface a throwing step/end listener via failTurn so the - // turn ends with reason error, not a silent "completed" with the throw - // swallowed. Regression test for the closeStep() catch that previously - // swallowed the throw in the normal (no-tool, no-steering) path. (Step - // boundaries have no agent/* mirror; the session-event listener is the path.) + it('a throwing step/end observer cannot rewrite the turn outcome', async () => { const adapter = new MockAdapter([textResponse('all good'), textResponse('turn 2 ok')]) const ctx = await balancedHarness(adapter) const agent = ctx.agentLoop.create(AgentId('a-stepend-throw'), { model: 'mock' }) @@ -822,11 +979,10 @@ describe('P1-5: a started turn (and any open step) is always closed on a boundar await waitForIdle(ctx, agent) const c = boundaryCounts(agent) - // step opened and closed; exactly one error turn-end; turn balanced. - expect(c).toMatchObject({ turnStart: 1, turnEnd: 1, stepStart: 1, stepEnd: 1, errors: 1 }) - expect(errors.map(e => e.message)).toEqual(['boom step-end']) + expect(c).toMatchObject({ turnStart: 1, turnEnd: 1, stepStart: 1, stepEnd: 1, errors: 0 }) + expect(errors).toEqual([]) expect(c.lastTurnEnd?.type === 'turn/end' && c.lastTurnEnd.data.reason) - .toEqual({ kind: 'error', step: 1, message: 'boom step-end' }) + .toEqual({ kind: 'completed' }) // step/end precedes turn/end (ordering contract) const e = [...agent.session.events] @@ -844,14 +1000,11 @@ describe('P1-5: a started turn (and any open step) is always closed on a boundar expect(c2.stepStart).toBe(c2.stepEnd) }) - it('a throwing session/event listener on step/end during finalization still appends turn/end', async () => { + it('a throwing step/end observer cannot interrupt error finalization', async () => { // A finish-error stream opens a step then fails it, driving finalization - // through closeStep() with the step open. closeStep appends step/end; a - // session/event listener throwing on THAT must not abort the catch before - // closeTurn — step/end is already logged (balance holds) and the throw is - // contained + surfaced via failTurn, so turn/end is still appended. (The - // failed step itself also routes through failTurn; the step/end-listener - // throw is the second, contained, failure.) + // through closeStep() with the step open. Session contains the observer + // failure after committing step/end, so closeTurn still records the model + // failure and balances the turn. const errorStream: StreamChunk[] = [{ type: 'finish', reason: { kind: 'error', message: 'provider 500' } }] const adapter = new MockAdapter([errorStream, textResponse('turn 2 ok')]) const ctx = await harness(adapter) @@ -872,7 +1025,7 @@ describe('P1-5: a started turn (and any open step) is always closed on a boundar expect(e.some(x => x.type === 'step/end')).toBe(true) expect(e.some(x => x.type === 'turn/end')).toBe(true) expect(e.at(-1)?.type).toBe('turn/end') - expect(errors.length).toBeGreaterThanOrEqual(1) // surfaced via agent/error + expect(errors.map(error => error.message)).toEqual(['provider 500']) // loop survives. send(agent, 'again') @@ -881,12 +1034,8 @@ describe('P1-5: a started turn (and any open step) is always closed on a boundar }) it('a throwing session/event listener on turn/end is contained (turn still balanced, loop survives)', async () => { - // closeTurn appends turn/end; Session.append pushes it BEFORE notifying - // session/event listeners, so a throwing listener leaves turn/end in the log - // (the turn is balanced) but must not escape — from the normal-path closeTurn - // it would otherwise propagate; the append is contained so the loop continues. - // Turn boundaries are durable session events only (no agent/* mirror), so this - // session/event append-notify throw is the sole turn-end-listener failure path. + // Session contains the observer failure after committing turn/end, so the + // boundary stays authoritative and the loop continues normally. const adapter = new MockAdapter([textResponse('turn 1'), textResponse('turn 2')]) const ctx = await harness(adapter) const agent = ctx.agentLoop.create(AgentId('a-turnendappend'), { model: 'mock' }) @@ -912,7 +1061,7 @@ describe('P1-5: a started turn (and any open step) is always closed on a boundar }) }) -describe('P1-7: tool/result is logged under the originating call.id, not result.callId', () => { +describe('tool result call identity', () => { it('the loop records tool/result under the model call.id even when a post-execute listener replaces content', async () => { // Model emits a tool-call with id "c1", then a final text turn. const adapter = new MockAdapter([ @@ -992,7 +1141,7 @@ describe('surface: assistant/message omits sourceEventSeqs when no chunks stream -describe('disposal/cancel honored during pre-step assembly (P1-1)', () => { +describe('disposal and cancellation during pre-step assembly', () => { it('disposal during system-prompt assembly drops the about-to-start step as disposed', { timeout: 30000 }, async () => { // Block `system-prompt/assemble` on a promise. Start disposal (which // calls stop() synchronously, setting status=disposed), then release the @@ -1011,7 +1160,7 @@ describe('disposal/cancel honored during pre-step assembly (P1-1)', () => { await ctx.plugin(ToolRegistry) await ctx.plugin(AgentRegistry) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(Invariants, { freeze: false }) + await ctx.plugin(Invariants) ctx.llm.registerAdapter(['mock'], adapter) // Blocking listener on the parent context (survives fiber disposal). @@ -1068,7 +1217,7 @@ describe('disposal/cancel honored during pre-step assembly (P1-1)', () => { await ctx.plugin(ToolRegistry) await ctx.plugin(AgentRegistry) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(Invariants, { freeze: false }) + await ctx.plugin(Invariants) ctx.llm.registerAdapter(['mock'], adapter) const unlisten = ctx.on('system-prompt/assemble', async function (_assembly, _context, next) { @@ -1124,7 +1273,7 @@ describe('disposal/cancel honored during pre-step assembly (P1-1)', () => { await ctx.plugin(ToolRegistry) await ctx.plugin(AgentRegistry) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(Invariants, { freeze: false }) + await ctx.plugin(Invariants) ctx.llm.registerAdapter(['mock'], adapter) ctx.on('agent/pre-step', async () => { @@ -1176,7 +1325,7 @@ describe('disposal/cancel honored during pre-step assembly (P1-1)', () => { await ctx.plugin(ToolRegistry) await ctx.plugin(AgentRegistry) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(Invariants, { freeze: false }) + await ctx.plugin(Invariants) ctx.llm.registerAdapter(['mock'], adapter) ctx.on('agent/pre-step', async () => { @@ -1225,7 +1374,7 @@ describe('disposal/cancel honored during pre-step assembly (P1-1)', () => { await ctx.plugin(ToolRegistry) await ctx.plugin(AgentRegistry) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(Invariants, { freeze: false }) + await ctx.plugin(Invariants) ctx.llm.registerAdapter(['mock'], adapter) ctx.on('system-prompt/assemble', async function (_assembly, _context, next) { diff --git a/packages/core/agent-loop/tests/scope-lifecycle.spec.ts b/packages/core/agent-loop/tests/scope-lifecycle.spec.ts new file mode 100644 index 0000000000..2c478ad1f0 --- /dev/null +++ b/packages/core/agent-loop/tests/scope-lifecycle.spec.ts @@ -0,0 +1,1088 @@ +import { describe, expect, it } from 'vitest' +import { Context, symbols, type EffectMeta, type Fiber } from 'cordis' +import LlmService from '@deepseek-ai/dsh-llm' +import SessionStore, { SessionId, type SessionEvent } from '@deepseek-ai/dsh-session' +import SystemPrompt from '@deepseek-ai/dsh-system-prompt' +import ToolRegistry from '@deepseek-ai/dsh-tools' +import AgentRegistry, { AgentId, agentEvents, assembleContextFor } from '@deepseek-ai/dsh-agent' +import type { Agent } from '@deepseek-ai/dsh-agent' +import { scopeOf } from '@deepseek-ai/dsh-scope' +import AgentLoop, { ReactLoopAgent } from '@deepseek-ai/dsh-agent-loop' +import type { ContentBlock } from '@deepseek-ai/dsh-llm' +import { MockAdapter, textResponse } from './mock-adapter.ts' + +async function harnessWithLoop(adapter: MockAdapter = new MockAdapter([textResponse('ok')])): Promise<{ ctx: Context; loopFiber: Fiber }> { + const ctx = new Context() + await ctx.plugin(LlmService) + await ctx.plugin(SessionStore) + await ctx.plugin(SystemPrompt, { persona: 'You are the deployment.' }) + await ctx.plugin(ToolRegistry) + await ctx.plugin(AgentRegistry) + const loopFiber = await ctx.plugin(AgentLoop, { agents: [] }) + ctx.llm.registerAdapter(['mock'], adapter) + return { ctx, loopFiber } +} + +async function harness(adapter: MockAdapter = new MockAdapter([textResponse('ok')])): Promise { + return (await harnessWithLoop(adapter)).ctx +} + +function waitForIdle(ctx: Context, agent: ReactLoopAgent): Promise { + return new Promise((resolve) => { + const dispose = ctx.on('agent/status', (subject, status) => { + if (subject === agent && status === 'idle') { + dispose() + resolve() + } + }) + }) +} + +const text = (t: string): ContentBlock[] => [{ type: 'text', text: t }] + +/** Throw an arbitrary callback value to exercise the public unknown-error boundary. */ +function throwUnknown(value: unknown): never { + throw value +} + +/** Invoke the exact lifecycle effect to exercise same-stack reentrant teardown. */ +function disposeCurrentLifecycle(ownerCtx: Context): void { + const lifecycle = [...ownerCtx.fiber._disposables] + .find((dispose) => { + const effect = (dispose as typeof dispose & { [symbols.effect]?: EffectMeta })[symbols.effect] + return effect?.label.startsWith('agentLoop.lifecycle(') === true + }) + if (lifecycle === undefined) throw new Error('agent lifecycle effect not found') + void lifecycle() +} + +describe('agent scope lifecycle', () => { + it('rejects an already-aborted creation signal before publishing either identity', async () => { + const ctx = await harness() + const reason = new Error('cancelled before creation') + const controller = new AbortController() + controller.abort(reason) + + await expect(ctx.agents.create({ + agentId: AgentId('pre-aborted'), + sessionId: SessionId('pre-aborted-s'), + signal: controller.signal, + })).rejects.toBe(reason) + + expect(ctx.agents.get(AgentId('pre-aborted'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('pre-aborted-s'))).toBeUndefined() + + const valueController = new AbortController() + valueController.abort('plain cancellation reason') + await expect(ctx.agents.create({ + agentId: AgentId('pre-aborted-value'), + sessionId: SessionId('pre-aborted-value-s'), + signal: valueController.signal, + })).rejects.toMatchObject({ + message: 'agent "pre-aborted-value" creation aborted', + cause: 'plain cancellation reason', + }) + + expect(ctx.agents.get(AgentId('pre-aborted-value'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('pre-aborted-value-s'))).toBeUndefined() + await ctx.fiber.dispose() + }) + + it('joins cleanup when an abort lands reentrantly during scope preparation', async () => { + const ctx = await harness() + const reason = new Error('cancelled while preparing') + const controller = new AbortController() + let aborted = false + ctx.on('internal/plugin', (fiber) => { + if (aborted || fiber.name !== 'scope') return + aborted = true + controller.abort(reason) + }) + + await expect(ctx.agents.create({ + agentId: AgentId('prepare-abort'), + sessionId: SessionId('prepare-abort-s'), + signal: controller.signal, + })).rejects.toBe(reason) + + expect(ctx.agents.get(AgentId('prepare-abort'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('prepare-abort-s'))).toBeUndefined() + await ctx.fiber.dispose() + }) + + it('normalizes non-Error create failures for rollback while rethrowing the original value', async () => { + const ctx = await harness() + let thrown: unknown + ctx.on('session/created', () => { + if (thrown === undefined) return + const value = thrown + thrown = undefined + throwUnknown(value) + }) + + const createFailure = { source: 'create' } + thrown = createFailure + let createCaught: unknown + try { + ctx.agentLoop.create(AgentId('unknown-create')) + } catch (error: unknown) { + createCaught = error + } + expect(createCaught).toBe(createFailure) + + const ownedFailure = { source: 'createAgent' } + thrown = ownedFailure + await expect(ctx.agents.create({ + agentId: AgentId('unknown-owned-create'), + sessionId: SessionId('unknown-owned-create-s'), + })).rejects.toBe(ownedFailure) + + expect(ctx.agents.get(AgentId('unknown-create'))).toBeUndefined() + expect(ctx.agents.get(AgentId('unknown-owned-create'))).toBeUndefined() + await ctx.fiber.dispose() + }) + + it('wires agent.ctx: tagged with the agent, DX field set, ctx.agent safe elsewhere', async () => { + const ctx = await harness() + const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' }) + expect(scopeOf(agent.ctx)).toBe(agent) + expect(agent.ctx.agent).toBe(agent) + // The root accessor default: a plain context answers undefined, not a throw. + expect(ctx.agent).toBeUndefined() + await ctx.agents.get(AgentId('a1'))?.whenIdle() + }) + + it('scoped registrations live in the agent world and die with the agent', async () => { + const ctx = await harness() + const handle = await ctx.agents.create({ agentId: AgentId('a1'), sessionId: SessionId('s1'), agentOptions: { model: 'mock' } }) + const { agent } = handle + agent.ctx.systemPrompt.section({ name: 'deployment:persona', order: 0, text: 'You run tests.' }) + agent.ctx.tools.register({ + name: 'mine', description: 'scoped', parameters: {}, + execute: () => Promise.resolve(text('ran')), + }) + + const scopedAssembly = await ctx.systemPrompt.assemble(assembleContextFor(agent)) + expect(scopedAssembly.sections.find(s => s.name === 'deployment:persona')?.text).toBe('You run tests.') + expect(scopedAssembly.tools.map(t => t.name)).toContain('mine') + // Other assemblies are untouched. + const globalAssembly = await ctx.systemPrompt.assemble() + expect(globalAssembly.sections.find(s => s.name === 'deployment:persona')?.text).toBe('You are the deployment.') + expect(globalAssembly.tools.map(t => t.name)).not.toContain('mine') + + await handle.dispose() + // The scoped world unwound with the agent: nothing leaked into the registries. + expect(ctx.tools.get('mine', agent)).toBeUndefined() + const after = await ctx.systemPrompt.assemble(assembleContextFor(agent)) + expect(after.sections.find(s => s.name === 'deployment:persona')?.text).toBe('You are the deployment.') + }) + + it('agent.ctx listeners hear only their own agent (scoped dispatch end to end)', async () => { + const ctx = await harness(new MockAdapter([textResponse('one'), textResponse('two')])) + const a = ctx.agentLoop.create(AgentId('a'), { model: 'mock' }) + const b = ctx.agentLoop.create(AgentId('b'), { model: 'mock' }) + + const heard: string[] = [] + a.ctx.on('agent/status', (subject, status) => void heard.push(`a-sees:${subject.id}:${status}`)) + a.ctx.on('session/event', (_s, event) => { + if (event.type === 'user/message') heard.push('a-sees:user-message') + }) + + b.send(text('for b')) + await waitForIdle(ctx, b) + expect(heard).toEqual([]) // nothing of b's leaked into a's scope + + a.send(text('for a')) + await waitForIdle(ctx, a) + expect(heard).toContain('a-sees:a:running') + expect(heard).toContain('a-sees:user-message') + }) + + it('runs setup in the guaranteed slot: scoped world complete before session-start and the first assembly', async () => { + const ctx = await harness() + const order: string[] = [] + ctx.on('agent/session-start', (agent) => { + order.push('session-start') + // The scoped section is already registered by the time session-start fires. + void ctx.systemPrompt.assemble(assembleContextFor(agent)).then((assembly) => { + order.push(`persona:${assembly.sections.find(s => s.name === 'deployment:persona')?.text}`) + }) + }) + + const handle = await ctx.agents.create({ + agentId: AgentId('child'), + sessionId: SessionId('child-s'), + agentOptions: { model: 'mock' }, + setup: async (agentCtx) => { + order.push('setup') + await Promise.resolve() + agentCtx.systemPrompt.section({ name: 'deployment:persona', order: 0, text: 'You are the child.' }) + }, + }) + await new Promise(resolve => setTimeout(resolve, 0)) + expect(order).toEqual(['setup', 'session-start', 'persona:You are the child.']) + await handle.dispose() + }) + + it('keeps both identities unpublished until async setup completes, then announces in order', async () => { + const ctx = await harness() + const gate = Promise.withResolvers() + const setupStarted = Promise.withResolvers() + const order: string[] = [] + ctx.on('session/created', (session) => { + expect(ctx.sessions.get(session.id)).toBe(session) + expect(ctx.agents.get(AgentId('atomic'))?.session).toBe(session) + order.push('session/created') + }) + ctx.on('agent/created', () => void order.push('agent/created')) + ctx.on('agent/session-start', () => void order.push('agent/session-start')) + const acceptedOptions = { model: 'mock' } + + const creating = ctx.agents.create({ + agentId: AgentId('atomic'), + sessionId: SessionId('atomic-s'), + agentOptions: acceptedOptions, + setup: async (agentCtx) => { + expect(agentCtx.agent?.id).toBe(AgentId('atomic')) + agentCtx.on('session/created', () => void order.push('setup-listener:session/created')) + agentCtx.on('agent/created', () => void order.push('setup-listener:agent/created')) + order.push('setup:start') + setupStarted.resolve(undefined) + await gate.promise + order.push('setup:end') + }, + }) + await setupStarted.promise + expect(ctx.agents.get(AgentId('atomic'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('atomic-s'))).toBeUndefined() + expect(order).toEqual(['setup:start']) + gate.resolve(undefined) + const handle = await creating + expect(handle.agent.options).toBe(acceptedOptions) + expect(order).toEqual([ + 'setup:start', + 'setup:end', + 'session/created', + 'setup-listener:session/created', + 'agent/created', + 'setup-listener:agent/created', + 'agent/session-start', + ]) + await handle.dispose() + }) + + it('lets the final enter arbitrate unsupported concurrent same-id creation and rolls the loser back', async () => { + const ctx = await harness() + const gate = Promise.withResolvers() + const bothStarted = Promise.withResolvers() + let started = 0 + const setup = async (): Promise => { + started += 1 + if (started === 2) bothStarted.resolve(undefined) + await gate.promise + } + const agentId = AgentId('concurrent-final-enter') + const first = ctx.agents.create({ + agentId, + sessionId: SessionId('concurrent-final-enter-a'), + agentOptions: { model: 'mock' }, + setup, + }) + const second = ctx.agents.create({ + agentId, + sessionId: SessionId('concurrent-final-enter-b'), + agentOptions: { model: 'mock' }, + setup, + }) + await bothStarted.promise + expect(ctx.agents.list()).toEqual([]) + expect(ctx.sessions.list()).toEqual([]) + + gate.resolve(undefined) + const outcomes = await Promise.allSettled([first, second]) + const fulfilled = outcomes.filter((outcome): outcome is PromiseFulfilledResult> => outcome.status === 'fulfilled') + const rejected = outcomes.filter((outcome): outcome is PromiseRejectedResult => outcome.status === 'rejected') + expect(fulfilled).toHaveLength(1) + expect(rejected).toHaveLength(1) + expect(String(rejected[0]!.reason)).toMatch(/already registered/) + expect(ctx.agents.list()).toEqual([fulfilled[0]!.value.agent]) + expect(ctx.sessions.list()).toEqual([fulfilled[0]!.value.agent.session]) + + await fulfilled[0]!.value.dispose() + expect(ctx.agents.list()).toEqual([]) + expect(ctx.sessions.list()).toEqual([]) + }) + + it('uses signal only for creation: aborts pending setup but not a returned live handle', async () => { + const ctx = await harness() + const pendingController = new AbortController() + const setupStarted = Promise.withResolvers() + const pending = ctx.agents.create({ + agentId: AgentId('signal-pending'), + sessionId: SessionId('signal-pending-s'), + agentOptions: { model: 'mock' }, + signal: pendingController.signal, + setup: async () => { + setupStarted.resolve(undefined) + await new Promise(() => {}) + }, + }) + await setupStarted.promise + pendingController.abort(new Error('cancel pending creation')) + await expect(pending).rejects.toThrow('cancel pending creation') + expect(ctx.agents.get(AgentId('signal-pending'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('signal-pending-s'))).toBeUndefined() + + const liveController = new AbortController() + const live = await ctx.agents.create({ + agentId: AgentId('signal-live'), + sessionId: SessionId('signal-live-s'), + agentOptions: { model: 'mock' }, + signal: liveController.signal, + }) + liveController.abort(new Error('too late')) + await Promise.resolve() + expect(ctx.agents.get(live.agent.id)).toBe(live.agent) + expect(live.agent.status).toBe('idle') + await live.dispose() + }) + + it('owner unload aborts a pending setup and publishes nothing', async () => { + const ctx = await harness() + const gate = Promise.withResolvers() + const setupStarted = Promise.withResolvers() + const published: string[] = [] + ctx.on('session/created', () => void published.push('session/created')) + ctx.on('agent/created', () => void published.push('agent/created')) + + let creating!: ReturnType + const owner = await ctx.plugin(Object.assign((inner: Context) => { + creating = inner.agents.create({ + agentId: AgentId('owner-race'), + sessionId: SessionId('owner-race-s'), + agentOptions: { model: 'mock' }, + setup: async () => { + setupStarted.resolve(undefined) + await gate.promise + }, + }) + }, { inject: ['agents'] })) + await setupStarted.promise + + await owner.dispose() + await expect(creating).rejects.toThrow(/owner disposed during setup/) + expect(published).toEqual([]) + expect(ctx.agents.get(AgentId('owner-race'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('owner-race-s'))).toBeUndefined() + // Let the losing callback settle; Promise.race already observes it. + gate.resolve(undefined) + await Promise.resolve() + + // The other ordering in the same race: setup resolves first (its reaction + // is queued), then owner disposal flips active before that continuation can + // publish. The post-race active check must still reject. + const gate2 = Promise.withResolvers() + const setupStarted2 = Promise.withResolvers() + let creating2!: ReturnType + const owner2 = await ctx.plugin(Object.assign((inner: Context) => { + creating2 = inner.agents.create({ + agentId: AgentId('owner-race-2'), + sessionId: SessionId('owner-race-s-2'), + agentOptions: { model: 'mock' }, + setup: async () => { + setupStarted2.resolve(undefined) + await gate2.promise + }, + }) + }, { inject: ['agents'] })) + await setupStarted2.promise + gate2.resolve(undefined) + const unload2 = owner2.dispose() + await expect(creating2).rejects.toThrow(/owner disposed during setup/) + await unload2 + expect(ctx.agents.get(AgentId('owner-race-2'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('owner-race-s-2'))).toBeUndefined() + }) + + it('an AgentLoop unload aborts pending setup, awaits cleanup, and releases both ids', async () => { + const { ctx, loopFiber } = await harnessWithLoop() + const gate = Promise.withResolvers() + const setupStarted = Promise.withResolvers() + const published: string[] = [] + ctx.on('session/created', () => void published.push('session/created')) + ctx.on('agent/created', () => void published.push('agent/created')) + + const creating = ctx.agents.create({ + agentId: AgentId('factory-setup-race'), + sessionId: SessionId('factory-setup-race-s'), + agentOptions: { model: 'mock' }, + setup: async () => { + setupStarted.resolve(undefined) + await gate.promise + }, + }) + await setupStarted.promise + + await loopFiber.dispose() + await expect(creating).rejects.toThrow(/agent loop is not active/) + expect(published).toEqual([]) + expect(ctx.agents.get(AgentId('factory-setup-race'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('factory-setup-race-s'))).toBeUndefined() + + gate.resolve(undefined) + await ctx.fiber.dispose() + }) + + it('factory unload during scope minting skips setup and awaits provisional cleanup', async () => { + const { ctx, loopFiber } = await harnessWithLoop() + let unloaded = false + let setupCalls = 0 + ctx.on('internal/plugin', (fiber) => { + if (unloaded || fiber.name !== 'scope') return + unloaded = true + void loopFiber.dispose() + }) + + const creating = ctx.agents.create({ + agentId: AgentId('factory-scope-race'), + sessionId: SessionId('factory-scope-race-s'), + agentOptions: { model: 'mock' }, + setup: () => { setupCalls += 1 }, + }) + await expect(creating).rejects.toThrow(/agent loop is not active/) + await loopFiber.dispose() + expect(setupCalls).toBe(0) + expect(ctx.agents.get(AgentId('factory-scope-race'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('factory-scope-race-s'))).toBeUndefined() + + await ctx.fiber.dispose() + }) + + it('caller unload during scope minting owns and drains the half-built child', async () => { + const ctx = await harness() + const gate = Promise.withResolvers() + const cleanupStarted = Promise.withResolvers() + let ownerFiber!: Fiber + let ownerDisposal!: Promise + let scopeFiber: Fiber | undefined + let creating!: ReturnType + ctx.on('internal/plugin', (fiber) => { + if (fiber.name !== 'scope' || scopeFiber !== undefined) return + scopeFiber = fiber + fiber.ctx.effect(() => async () => { + cleanupStarted.resolve(undefined) + await gate.promise + }) + ownerDisposal = ownerFiber.dispose() + }) + + const owner = ctx.plugin(Object.assign((inner: Context) => { + ownerFiber = inner.fiber + creating = inner.agents.create({ + agentId: AgentId('caller-scope-race'), + sessionId: SessionId('caller-scope-race-s'), + agentOptions: { model: 'mock' }, + }) + }, { inject: ['agents'] })) + + await cleanupStarted.promise + let ownerSettled = false + void ownerDisposal.then(() => { ownerSettled = true }) + await Promise.resolve() + expect(ownerSettled).toBe(false) + gate.resolve(undefined) + await expect(creating).rejects.toThrow(/owner disposed during setup/) + await ownerDisposal + await owner + expect(scopeFiber?.uid).toBeNull() + expect(ctx.agents.get(AgentId('caller-scope-race'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('caller-scope-race-s'))).toBeUndefined() + await owner.dispose() + await ctx.fiber.dispose() + }) + + it('synchronous create rechecks provider liveness before its first publication edge', async () => { + const { ctx, loopFiber } = await harnessWithLoop() + const sessionsBefore = ctx.sessions.list().length + let unloaded = false + ctx.on('internal/plugin', (fiber) => { + if (unloaded || fiber.name !== 'scope') return + unloaded = true + void loopFiber.dispose() + }) + + expect(() => ctx.agentLoop.create(AgentId('config-scope-race'), { model: 'mock' })) + .toThrow(/agent loop is not active/) + await loopFiber.dispose() + expect(ctx.agents.get(AgentId('config-scope-race'))).toBeUndefined() + expect(ctx.sessions.list()).toHaveLength(sessionsBefore) + await ctx.fiber.dispose() + }) + + it('synchronous create leaves no lifecycle state when session preparation fails', async () => { + const ctx = await harness() + const id = AgentId('config-prepare-failure') + + expect(() => ctx.agentLoop.create(id, { model: 'mock' }, { cwd: 'relative' })) + .toThrow(/absolute path/) + const replacement = ctx.agentLoop.create(id, { model: 'mock' }, { cwd: '/recovered' }) + expect(ctx.agents.get(id)).toBe(replacement) + await replacement.whenIdle() + await ctx.fiber.dispose() + }) + + it('factory unload awaits provisional cleanup when scope preparation throws', async () => { + const { ctx, loopFiber } = await harnessWithLoop() + let triggered = false + ctx.on('internal/plugin', (fiber) => { + if (triggered || fiber.name !== 'scope') return + triggered = true + void loopFiber.dispose() + throw new Error('scope preparation failed') + }) + + await expect(ctx.agents.create({ + agentId: AgentId('factory-scope-throw'), + sessionId: SessionId('factory-scope-throw-s'), + agentOptions: { model: 'mock' }, + })).rejects.toThrow('scope preparation failed') + await loopFiber.dispose() + expect(ctx.agents.get(AgentId('factory-scope-throw'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('factory-scope-throw-s'))).toBeUndefined() + + await ctx.fiber.dispose() + }) + + it('AgentLoop unload is a structural co-owner of every live programmatic agent', async () => { + const { ctx, loopFiber } = await harnessWithLoop() + const loop = ctx.agentLoop + const agentId = AgentId('factory-live') + const handle = await ctx.agents.create({ + agentId, + sessionId: SessionId('factory-live-s'), + agentOptions: { model: 'mock' }, + }) + + await loopFiber.dispose() + expect(handle.agent.status).toBe('disposed') + expect(ctx.agents.get(agentId)).toBeUndefined() + expect(ctx.sessions.get(SessionId('factory-live-s'))).toBeUndefined() + expect(ctx.fiber.getEffects().filter(effect => effect.label === `agentLoop.owner(${agentId})`)).toEqual([]) + // The consumer handle shares the provider's completed quiescence boundary. + await handle.dispose() + + await expect(loop.createAgent(ctx, { + agentId: AgentId('factory-inactive'), + sessionId: SessionId('factory-inactive-s'), + })).rejects.toThrow('agent loop is not active') + await ctx.fiber.dispose() + }) + + it('keeps AgentLoop dependencies available when the caller injects only agents', async () => { + const ctx = await harness() + let creating!: ReturnType + const owner = await ctx.plugin(Object.assign((inner: Context) => { + creating = inner.agents.create({ + agentId: AgentId('dependency-origin'), + sessionId: SessionId('dependency-origin-s'), + agentOptions: { model: 'mock' }, + setup: (agentCtx) => { + agentCtx.tools.register({ + name: 'dependency-origin-tool', + description: 'proves AgentLoop dependency origin', + parameters: {}, + execute: () => Promise.resolve(text('ok')), + }) + agentCtx.systemPrompt.section({ + name: 'dependency-origin-section', + order: 1, + text: 'factory dependency surface', + }) + }, + }) + }, { inject: ['agents'] })) + + const handle = await creating + const assembly = await ctx.systemPrompt.assemble(assembleContextFor(handle.agent)) + expect(assembly.tools.map(tool => tool.name)).toContain('dependency-origin-tool') + expect(assembly.sections.map(section => section.name)).toContain('dependency-origin-section') + await handle.dispose() + await owner.dispose() + await ctx.fiber.dispose() + }) + + it('keeps both entries and the scope live through a reentrant session/created teardown', async () => { + const ctx = await harness() + let ownerCtx!: Context + let creating!: ReturnType + const lifecycle: string[] = [] + ctx.on('session/created', (session) => { + if (session.id !== SessionId('session-created-barrier-s')) return + lifecycle.push('session-created:dispose') + disposeCurrentLifecycle(ownerCtx) + }) + ctx.on('session/created', (session) => { + if (session.id !== SessionId('session-created-barrier-s')) return + const agent = ctx.agents.get(AgentId('session-created-barrier'))! + expect(ctx.sessions.get(session.id)).toBe(session) + expect(agent.session).toBe(session) + agent.ctx.effect(() => () => { lifecycle.push('scope-disposed') }) + lifecycle.push('session-created:observer') + }) + ctx.on('agent/created', () => void lifecycle.push('agent-created')) + ctx.on('agent/disposed', () => void lifecycle.push('agent-disposed')) + ctx.on('session/disposed', (session) => { + if (session.id === SessionId('session-created-barrier-s')) lifecycle.push('session-disposed') + }) + + const owner = await ctx.plugin(Object.assign((inner: Context) => { + ownerCtx = inner + creating = inner.agents.create({ + agentId: AgentId('session-created-barrier'), + sessionId: SessionId('session-created-barrier-s'), + agentOptions: { model: 'mock' }, + }) + }, { inject: ['agents'] })) + + await expect(creating).rejects.toThrow(/lifecycle disposed/) + await owner.dispose() + expect(lifecycle).toEqual([ + 'session-created:dispose', + 'session-created:observer', + 'session-disposed', + 'scope-disposed', + ]) + expect(ctx.agents.get(AgentId('session-created-barrier'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('session-created-barrier-s'))).toBeUndefined() + await ctx.fiber.dispose() + }) + + it('keeps both entries and the scope live through a reentrant agent/created teardown', async () => { + const ctx = await harness() + let ownerCtx!: Context + let creating!: ReturnType + const lifecycle: string[] = [] + ctx.on('session/created', (session) => { + if (session.id === SessionId('agent-created-barrier-s')) lifecycle.push('session-created') + }) + ctx.on('agent/created', (agent) => { + if (agent.id !== AgentId('agent-created-barrier')) return + lifecycle.push('agent-created:dispose') + disposeCurrentLifecycle(ownerCtx) + }) + ctx.on('agent/created', (agent) => { + if (agent.id !== AgentId('agent-created-barrier')) return + expect(ctx.agents.get(agent.id)).toBe(agent) + expect(ctx.sessions.get(agent.session.id)).toBe(agent.session) + agent.ctx.effect(() => () => { lifecycle.push('scope-disposed') }) + lifecycle.push('agent-created:observer') + }) + ctx.on('agent/disposed', (agent) => { + if (agent.id === AgentId('agent-created-barrier')) lifecycle.push('agent-disposed') + }) + ctx.on('session/disposed', (session) => { + if (session.id === SessionId('agent-created-barrier-s')) lifecycle.push('session-disposed') + }) + + const owner = await ctx.plugin(Object.assign((inner: Context) => { + ownerCtx = inner + creating = inner.agents.create({ + agentId: AgentId('agent-created-barrier'), + sessionId: SessionId('agent-created-barrier-s'), + agentOptions: { model: 'mock' }, + }) + }, { inject: ['agents'] })) + + await expect(creating).rejects.toThrow(/lifecycle disposed/) + await owner.dispose() + expect(lifecycle).toEqual([ + 'session-created', + 'agent-created:dispose', + 'agent-created:observer', + 'agent-disposed', + 'session-disposed', + 'scope-disposed', + ]) + expect(ctx.agents.get(AgentId('agent-created-barrier'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('agent-created-barrier-s'))).toBeUndefined() + await ctx.fiber.dispose() + }) + + it('rechecks caller liveness after creation listeners before unlocking the driver', async () => { + const ctx = await harness() + const starts: string[] = [] + let ownerCtx!: Context + let creating!: ReturnType + ctx.on('agent/session-start', agent => void starts.push(agent.id)) + ctx.on('agent/created', (agent) => { + if (agent.id === AgentId('listener-dispose')) void ownerCtx.fiber.dispose() + }) + + const owner = await ctx.plugin(Object.assign((inner: Context) => { + ownerCtx = inner + creating = inner.agents.create({ + agentId: AgentId('listener-dispose'), + sessionId: SessionId('listener-dispose-s'), + agentOptions: { model: 'mock' }, + }) + }, { inject: ['agents'] })) + + await expect(creating).rejects.toThrow(/owner disposed during setup/) + await owner.dispose() + expect(starts).toEqual([]) + expect(ctx.agents.get(AgentId('listener-dispose'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('listener-dispose-s'))).toBeUndefined() + await ctx.fiber.dispose() + }) + + it('rechecks caller liveness after session-start before starting the driver', async () => { + const ctx = await harness() + let ownerCtx!: Context + let creating!: ReturnType + let announced!: ReactLoopAgent + const statuses: string[] = [] + let scopeDisposed = false + let observerSawLive = false + ctx.on('agent/status', (agent, status) => { + if (agent.id === AgentId('session-start-dispose')) statuses.push(status) + }) + ctx.on('agent/session-start', (agent) => { + if (agent.id !== AgentId('session-start-dispose')) return + announced = agent as ReactLoopAgent + disposeCurrentLifecycle(ownerCtx) + }) + ctx.on('agent/session-start', (agent) => { + if (agent.id !== AgentId('session-start-dispose')) return + expect(ctx.agents.get(agent.id)).toBe(agent) + expect(ctx.sessions.get(agent.session.id)).toBe(agent.session) + agent.ctx.effect(() => () => { scopeDisposed = true }) + observerSawLive = true + }) + + const owner = await ctx.plugin(Object.assign((inner: Context) => { + ownerCtx = inner + creating = inner.agents.create({ + agentId: AgentId('session-start-dispose'), + sessionId: SessionId('session-start-dispose-s'), + agentOptions: { model: 'mock' }, + }) + }, { inject: ['agents'] })) + + await expect(creating).rejects.toThrow(/lifecycle disposed/) + await owner.dispose() + expect(announced.status).toBe('disposed') + expect(statuses).toEqual(['disposed']) + expect(observerSawLive).toBe(true) + expect(scopeDisposed).toBe(true) + expect(announced.session.events).toEqual([]) + expect(ctx.agents.get(AgentId('session-start-dispose'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('session-start-dispose-s'))).toBeUndefined() + await ctx.fiber.dispose() + }) + + it('a rejecting setup publishes nothing and unwinds the unpublished scope', async () => { + const ctx = await harness() + const published: string[] = [] + ctx.on('session/created', () => void published.push('session/created')) + ctx.on('agent/created', () => void published.push('agent/created')) + ctx.on('agent/session-start', () => void published.push('agent/session-start')) + await expect(ctx.agents.create({ + agentId: AgentId('bad'), + sessionId: SessionId('bad-s'), + agentOptions: { model: 'mock' }, + setup: async () => { + await Promise.resolve() + throw new Error('boom setup') + }, + })).rejects.toThrow('boom setup') + + // Nothing leaked: no agent, no session, and the ids are reusable. + expect(published).toEqual([]) + expect(ctx.agents.get(AgentId('bad'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('bad-s'))).toBeUndefined() + const retry = await ctx.agents.create({ agentId: AgentId('bad'), sessionId: SessionId('bad-s'), agentOptions: { model: 'mock' } }) + await retry.dispose() + }) + + it('rejects an exotic durable seed before publishing either identity', async () => { + const ctx = await harness() + const published: string[] = [] + ctx.on('session/created', () => { published.push('session') }) + ctx.on('agent/created', () => { published.push('agent') }) + class ExoticData { readonly value = 'not durable JSON' } + const seed = [{ + seq: 0, + type: 'test/exotic-seed', + data: new ExoticData(), + }] as unknown as SessionEvent[] + + await expect(ctx.agents.create({ + agentId: AgentId('exotic-seed'), + sessionId: SessionId('exotic-seed-session'), + agentOptions: { model: 'mock' }, + seed, + })).rejects.toThrow(/seed event at index 0 is not losslessly JSON-serializable/) + + expect(published).toEqual([]) + expect(ctx.agents.get(AgentId('exotic-seed'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('exotic-seed-session'))).toBeUndefined() + const retry = await ctx.agents.create({ + agentId: AgentId('exotic-seed'), + sessionId: SessionId('exotic-seed-session'), + agentOptions: { model: 'mock' }, + }) + await retry.dispose() + }) + + it('a throwing session/created listener disposes the scope (pre-nesting rollback window)', async () => { + const ctx = await harness() + let boom = true + const disposed: string[] = [] + ctx.on('agent/disposed', agent => void disposed.push(agent.id)) + ctx.on('session/created', () => { + if (boom) { boom = false; throw new Error('boom created') } + }) + await expect(ctx.agents.create({ + agentId: AgentId('bad'), sessionId: SessionId('bad-s'), agentOptions: { model: 'mock' }, + })).rejects.toThrow('boom created') + expect(ctx.agents.get(AgentId('bad'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('bad-s'))).toBeUndefined() + expect(disposed).toEqual([]) // inserted but never announced: no impossible disposed edge + // The rollback also disposed the scope fiber: re-creating works cleanly. + const retry = await ctx.agents.create({ agentId: AgentId('bad'), sessionId: SessionId('bad-s'), agentOptions: { model: 'mock' } }) + expect(scopeOf(retry.agent.ctx)).toBe(retry.agent) + await retry.dispose() + }) + + it('pairs session and agent announcements when agent creation aborts publication', async () => { + const ctx = await harness() + const lifecycle: string[] = [] + ctx.on('session/created', (session) => { lifecycle.push(`session-created:${session.id}`) }) + ctx.on('session/disposed', (session) => { lifecycle.push(`session-disposed:${session.id}`) }) + ctx.on('agent/created', (agent) => { + lifecycle.push(`agent-created:${agent.id}`) + throw new Error('agent observer failed') + }) + ctx.on('agent/disposed', (agent) => { lifecycle.push(`agent-disposed:${agent.id}`) }) + + await expect(ctx.agents.create({ + agentId: AgentId('partial-agent'), + sessionId: SessionId('partial-session'), + agentOptions: { model: 'mock' }, + })).rejects.toThrow('agent observer failed') + + expect(lifecycle).toEqual([ + 'session-created:partial-session', + 'agent-created:partial-agent', + 'agent-disposed:partial-agent', + 'session-disposed:partial-session', + ]) + expect(ctx.agents.get(AgentId('partial-agent'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('partial-session'))).toBeUndefined() + }) + + it('the synchronous config helper rolls back when publication throws', async () => { + const ctx = await harness() + const sessionsBefore = ctx.sessions.list().length + let boom = true + ctx.on('session/created', () => { + if (boom) { + boom = false + throw new Error('config publish failed') + } + }) + + expect(() => ctx.agentLoop.create(AgentId('config-bad'), { model: 'mock' })) + .toThrow('config publish failed') + expect(ctx.agents.get(AgentId('config-bad'))).toBeUndefined() + expect(ctx.sessions.list()).toHaveLength(sessionsBefore) + }) + + it('registrations through a disposed agent ctx throw INACTIVE_EFFECT', async () => { + const ctx = await harness() + const handle = await ctx.agents.create({ agentId: AgentId('a1'), sessionId: SessionId('s1'), agentOptions: { model: 'mock' } }) + await handle.dispose() + expect(() => handle.agent.ctx.on('agent/status', () => {})).toThrow(/inactive context/) + }) + + it('agentEvents fuses carrier and subject for custom drivers', async () => { + const ctx = await harness() + const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' }) + const other = ctx.agentLoop.create(AgentId('a2'), { model: 'mock' }) + const heard: string[] = [] + agent.ctx.on('agent/error', (subject: Agent, turn: number) => void heard.push(`${subject.id}:${turn}`)) + + agentEvents(ctx, other).emit('agent/error', 1, 0, new Error('not for a1')) + agentEvents(ctx, agent).emit('agent/error', 2, 0, new Error('for a1')) + expect(heard).toEqual(['a1:2']) + }) + + it('owner unload honors the documented teardown order: unregistration AFTER the drain, before detach', async () => { + const ctx = await harness() + let handle!: Awaited> + const owner = await ctx.plugin(Object.assign(async (inner: Context) => { + handle = await inner.agents.create({ agentId: AgentId('o1'), sessionId: SessionId('o1-s'), agentOptions: { model: 'mock' } }) + }, { inject: ['agents'] })) + const { agent } = handle + + const order: string[] = [] + ctx.on('session/event', (_s, event) => { + if (event.type === 'turn/end') order.push('turn-end') + }) + ctx.on('agent/disposed', () => { + order.push(`disposed(listed=${ctx.agents.get(AgentId('o1')) !== undefined})`) + order.push(`session-still-stored=${ctx.sessions.get(SessionId('o1-s')) !== undefined}`) + }) + + // Open a turn so the drain has real work: the loop must finish it BEFORE + // the registry entry goes away (the agent/disposed contract: "its fiber + // and any in-flight turn have been torn down"). Wait for the turn to be + // OPEN in the log — a dispose landing in the pre-step window would drop + // the queued prompt without ever opening a turn. + const turnOpen = new Promise((resolve) => { + const off = ctx.on('session/event', (_s, event) => { + if (event.type === 'turn/start') { off(); resolve() } + }) + }) + agent.send(text('work')) + await turnOpen + await owner.dispose() + expect(order).toEqual(['turn-end', 'disposed(listed=false)', 'session-still-stored=true']) + expect(ctx.sessions.get(SessionId('o1-s'))).toBeUndefined() + }) + + it('handle.dispose() during owner unload still awaits true quiescence (shared boundary)', async () => { + const ctx = await harness() + let handle!: Awaited> + const owner = await ctx.plugin(Object.assign(async (inner: Context) => { + handle = await inner.agents.create({ agentId: AgentId('h1'), sessionId: SessionId('h1-s'), agentOptions: { model: 'mock' } }) + }, { inject: ['agents'] })) + + const teardownDone: string[] = [] + ctx.on('agent/disposed', () => void teardownDone.push('unregistered')) + + // Owner unload begins FIRST (invokes the raw cordis wrapper)… + const unload = owner.dispose() + // …and a concurrent handle.dispose() must not resolve before the chain + // actually finished (the raw wrapper returns undefined on a repeat call). + await handle.dispose() + expect(teardownDone).toContain('unregistered') + expect(ctx.agents.get(AgentId('h1'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('h1-s'))).toBeUndefined() + await unload + }) + + it('successful handle disposal retires its caller ownership effect', async () => { + const ctx = await harness() + const agentId = AgentId('retired-owner-effect') + const handle = await ctx.agents.create({ + agentId, + sessionId: SessionId('retired-owner-effect-s'), + agentOptions: { model: 'mock' }, + }) + + expect(ctx.fiber.getEffects().map(effect => effect.label)).toContain(`agentLoop.owner(${agentId})`) + await handle.dispose() + expect(ctx.fiber.getEffects().filter(effect => effect.label === `agentLoop.owner(${agentId})`)).toEqual([]) + await ctx.fiber.dispose() + }) + + it('owner unload after handle-first teardown follows the same in-flight boundary', async () => { + const ctx = await harness() + const gate = Promise.withResolvers() + const cleanupStarted = Promise.withResolvers() + let handle!: Awaited> + const owner = await ctx.plugin(Object.assign(async (inner: Context) => { + handle = await inner.agents.create({ + agentId: AgentId('manual-first'), + sessionId: SessionId('manual-first-s'), + agentOptions: { model: 'mock' }, + setup(agentCtx) { + agentCtx.effect(() => async () => { + cleanupStarted.resolve(undefined) + await gate.promise + }) + }, + }) + }, { inject: ['agents'] })) + + const disposing = handle.dispose() + await cleanupStarted.promise + let ownerSettled = false + const unloading = owner.dispose().then(() => { ownerSettled = true }) + await Promise.resolve() + expect(ownerSettled).toBe(false) + gate.resolve(undefined) + await Promise.all([disposing, unloading]) + expect(ctx.agents.get(AgentId('manual-first'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('manual-first-s'))).toBeUndefined() + await ctx.fiber.dispose() + }) + + it('reopens ids after detach while the prior private scope finishes quiescing', async () => { + const ctx = await harness() + const gate = Promise.withResolvers() + const cleanupStarted = Promise.withResolvers() + const sessionDisposed = Promise.withResolvers() + const agentId = AgentId('quiescent-reuse') + const sessionId = SessionId('quiescent-reuse-s') + ctx.on('session/disposed', (session) => { + if (session.id === sessionId) sessionDisposed.resolve(undefined) + }) + const first = await ctx.agents.create({ + agentId, + sessionId, + agentOptions: { model: 'mock' }, + setup(agentCtx) { + agentCtx.effect(() => async () => { + cleanupStarted.resolve(undefined) + await gate.promise + }) + }, + }) + + const disposing = first.dispose() + await Promise.all([sessionDisposed.promise, cleanupStarted.promise]) + expect(ctx.agents.get(agentId)).toBeUndefined() + expect(ctx.sessions.get(sessionId)).toBeUndefined() + const replacement = await ctx.agents.create({ agentId, sessionId, agentOptions: { model: 'mock' } }) + expect(ctx.agents.get(agentId)).toBe(replacement.agent) + expect(ctx.sessions.get(sessionId)).toBe(replacement.agent.session) + + gate.resolve(undefined) + await disposing + await replacement.dispose() + await ctx.fiber.dispose() + }) + + it('handle.dispose() awaits an idle-injection flush before unregistering or detaching', async () => { + const ctx = await harness() + const handle = await ctx.agents.create({ + agentId: AgentId('idle-flush'), + sessionId: SessionId('idle-flush-s'), + agentOptions: { model: 'mock' }, + }) + const gate = Promise.withResolvers() + let flushStarted = false + ctx.on('session/flush', (session) => { + if (session !== handle.agent.session) return + flushStarted = true + return gate.promise + }) + + handle.agent.inject(text('durable idle context'), { source: { kind: 'plugin', plugin: 'test' } }) + expect(flushStarted).toBe(true) + + let disposed = false + const disposal = handle.dispose().then(() => { disposed = true }) + await new Promise(resolve => setTimeout(resolve, 0)) + expect(disposed).toBe(false) + expect(ctx.agents.get(AgentId('idle-flush'))).toBe(handle.agent) + expect(ctx.sessions.get(SessionId('idle-flush-s'))).toBe(handle.agent.session) + + gate.resolve(undefined) + await disposal + expect(ctx.agents.get(AgentId('idle-flush'))).toBeUndefined() + expect(ctx.sessions.get(SessionId('idle-flush-s'))).toBeUndefined() + }) +}) diff --git a/packages/core/agent-loop/tests/tool-order.spec.ts b/packages/core/agent-loop/tests/tool-order.spec.ts index 35329ce679..d9b0a87e1d 100644 --- a/packages/core/agent-loop/tests/tool-order.spec.ts +++ b/packages/core/agent-loop/tests/tool-order.spec.ts @@ -107,7 +107,7 @@ describe('loop-level canonical tool order', () => { agent.send([{ type: 'text', text: 'go' }]) await waitForIdle(ctx, agent) expect(adapter.requests).toHaveLength(0) - expect(errors.map(e => e.message)).toEqual(['toolOrder lists unregistered tool "ghost"; registered tools: alpha']) + expect(errors.map(e => e.message)).toEqual(['toolOrder lists unregistered tool "ghost"; known tools: alpha']) expect(foldRequestHeader(agent.session.events)).toBeUndefined() const end = agent.session.events.find(e => e.type === 'turn/end') expect(end?.type === 'turn/end' && end.data.reason).toMatchObject({ kind: 'error', step: 1 }) diff --git a/packages/core/agent-loop/tests/turn-stop.spec.ts b/packages/core/agent-loop/tests/turn-stop.spec.ts new file mode 100644 index 0000000000..c275fee88c --- /dev/null +++ b/packages/core/agent-loop/tests/turn-stop.spec.ts @@ -0,0 +1,185 @@ +import { describe, expect, it } from 'vitest' +import { Context } from 'cordis' +import LlmService from '@deepseek-ai/dsh-llm' +import SessionStore, { type TurnEndReason } from '@deepseek-ai/dsh-session' +import SystemPrompt from '@deepseek-ai/dsh-system-prompt' +import ToolRegistry, { defineTool } from '@deepseek-ai/dsh-tools' +import AgentRegistry, { AgentId, type ContinuationStop } from '@deepseek-ai/dsh-agent' +import AgentLoop, { type ReactLoopAgent } from '@deepseek-ai/dsh-agent-loop' +import * as Invariants from '@deepseek-ai/dsh-invariants' +import { MockAdapter, textResponse, toolCallResponse } from './mock-adapter.ts' + +async function harness(adapter: MockAdapter): Promise { + const ctx = new Context() + await ctx.plugin(LlmService) + await ctx.plugin(SessionStore) + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(AgentRegistry) + await ctx.plugin(Invariants) + await ctx.plugin(AgentLoop, { agents: [] }) + ctx.llm.registerAdapter(['mock'], adapter) + return ctx +} + +function send(agent: ReactLoopAgent, text = 'go'): Promise { + agent.send([{ type: 'text', text }]) + return agent.whenIdle() +} + +function registerEcho(ctx: Context): void { + ctx.tools.register(defineTool({ + name: 'echo', + description: 'echo', + parameters: { text: { type: 'string' } }, + async execute(args) { + return [{ type: 'text', text: String(args.text) }] + }, + })) +} + +describe('agent/turn-stop', () => { + it('runs after steering folding and discards terminal steering instead of creating another step or turn', async () => { + const adapter = new MockAdapter([ + textResponse('the ordinary decision is stop'), + textResponse('must not be requested'), + ]) + const ctx = await harness(adapter) + const agent = ctx.agentLoop.create(AgentId('terminal-steering'), { model: 'mock' }) + agent.ctx.on('agent/turn-stop', (): ContinuationStop => ({ action: 'stop' })) + + let steered = false + ctx.on('agent/turn-continuation', async (subject, _turn, _default, next) => { + const downstream = await next() + if (subject === agent && !steered) { + steered = true + subject.steer([{ type: 'text', text: 'late continuation steering' }]) + } + return downstream + }, { prepend: true }) + + await send(agent) + + expect(adapter.requests).toHaveLength(1) + expect(agent.session.events.filter(event => event.type === 'turn/start')).toHaveLength(1) + expect(agent.session.events.filter(event => event.type === 'step/start')).toHaveLength(1) + expect(agent.session.events.filter(event => event.type === 'steering/message')).toHaveLength(0) + }) + + it('discards steering that arrives from session/flush after the terminal checkpoint', async () => { + const adapter = new MockAdapter([ + textResponse('terminal answer'), + textResponse('must not become a late-steering turn'), + ]) + const ctx = await harness(adapter) + const agent = ctx.agentLoop.create(AgentId('terminal-flush-steering'), { model: 'mock' }) + agent.ctx.on('agent/turn-stop', (): ContinuationStop => ({ action: 'stop' })) + + let injected = false + ctx.on('session/flush', (session) => { + if (session !== agent.session || injected) return + injected = true + agent.steer([{ type: 'text', text: 'steering from flush' }]) + }) + + await send(agent) + + expect(injected).toBe(true) + expect(agent.status).toBe('idle') + expect(adapter.requests).toHaveLength(1) + expect(agent.session.events.filter(event => event.type === 'turn/start')).toHaveLength(1) + expect(agent.session.events.filter(event => event.type === 'step/start')).toHaveLength(1) + expect(agent.session.events.filter(event => event.type === 'steering/message')).toHaveLength(0) + }) + + it('preserves an ordinary queued send that arrives during terminal flush', async () => { + const adapter = new MockAdapter([ + textResponse('first terminal answer'), + textResponse('queued follow-up answer'), + ]) + const ctx = await harness(adapter) + const agent = ctx.agentLoop.create(AgentId('terminal-flush-send'), { model: 'mock' }) + agent.ctx.on('agent/turn-stop', (): ContinuationStop => ({ action: 'stop' })) + + let queued = false + ctx.on('session/flush', (session) => { + if (session !== agent.session || queued) return + queued = true + agent.send([{ type: 'text', text: 'ordinary queued follow-up' }]) + }) + + await send(agent) + + expect(agent.status).toBe('idle') + expect(adapter.requests).toHaveLength(2) + expect(agent.session.events.filter(event => event.type === 'turn/start')).toHaveLength(2) + expect(agent.session.events.filter(event => event.type === 'step/start')).toHaveLength(2) + }) + + it('filters a scoped terminal listener to its own agent', async () => { + const adapter = new MockAdapter([ + toolCallResponse('a1', 'echo', { text: 'a' }), + toolCallResponse('b1', 'echo', { text: 'b' }), + textResponse('b continues normally'), + ]) + const ctx = await harness(adapter) + registerEcho(ctx) + const stopped = ctx.agentLoop.create(AgentId('stopped'), { model: 'mock' }) + const ordinary = ctx.agentLoop.create(AgentId('ordinary'), { model: 'mock' }) + stopped.ctx.on('agent/turn-stop', (): ContinuationStop => ({ action: 'stop' })) + + await send(stopped) + expect(adapter.requests).toHaveLength(1) + await send(ordinary) + + expect(adapter.requests).toHaveLength(3) + expect(stopped.session.events.filter(event => event.type === 'step/start')).toHaveLength(1) + expect(ordinary.session.events.filter(event => event.type === 'step/start')).toHaveLength(2) + }) + + it('unregisters with its scoped owner disposer', async () => { + const adapter = new MockAdapter([ + toolCallResponse('first', 'echo', { text: 'first' }), + toolCallResponse('second', 'echo', { text: 'second' }), + textResponse('continued after listener disposal'), + ]) + const ctx = await harness(adapter) + registerEcho(ctx) + const agent = ctx.agentLoop.create(AgentId('owned-listener'), { model: 'mock' }) + const disposeStop = agent.ctx.on('agent/turn-stop', (): ContinuationStop => ({ action: 'stop' })) + + await send(agent, 'first turn') + expect(adapter.requests).toHaveLength(1) + + disposeStop() + await send(agent, 'second turn') + expect(adapter.requests).toHaveLength(3) + }) + + it('fails a throwing terminal policy closed while the driver survives', async () => { + const adapter = new MockAdapter([ + textResponse('throwing policy'), + textResponse('healthy later turn'), + ]) + const ctx = await harness(adapter) + const agent = ctx.agentLoop.create(AgentId('bad-policy'), { model: 'mock' }) + const reasons: TurnEndReason[] = [] + const errors: string[] = [] + ctx.on('session/event', (session, event) => { + if (session === agent.session && event.type === 'turn/end') reasons.push(event.data.reason) + }) + agent.ctx.on('agent/error', (_subject, _turn, _step, error) => { errors.push(error.message) }) + + const disposeThrowing = agent.ctx.on('agent/turn-stop', () => { + throw new Error('terminal policy exploded') + }) + await send(agent, 'first') + disposeThrowing() + + await send(agent, 'healthy') + + expect(reasons.map(reason => reason.kind)).toEqual(['error', 'completed']) + expect(errors).toContain('terminal policy exploded') + expect(adapter.requests).toHaveLength(2) + }) +}) diff --git a/packages/core/agent-loop/tsconfig.json b/packages/core/agent-loop/tsconfig.json index 03df67cfc4..5d7cf98bb7 100644 --- a/packages/core/agent-loop/tsconfig.json +++ b/packages/core/agent-loop/tsconfig.json @@ -34,6 +34,9 @@ }, { "path": "../../core/agent" + }, + { + "path": "../../core/scope" } ] } diff --git a/packages/core/agent/README.md b/packages/core/agent/README.md index 8ad204c579..9ee324b0c3 100644 --- a/packages/core/agent/README.md +++ b/packages/core/agent/README.md @@ -8,61 +8,39 @@ Tracks live agents so UI, hook, and orchestrator plugins can find them without i ### Public API +The scoped-registration surface: `Agent.ctx` is the agent's scope context (`dsh-scope`, key = the agent) — register tools/sections/variables/listeners through it for that agent alone, all unwound on disposal. `agentEvents(ctx, agent)` is the fused dispatcher for ordinary agent-subject operations (carrier + injected subject in one move); its notification mode invokes every listener and contains both synchronous throws and returned-promise rejections. The registry lifecycle pair reuses one stable routing carrier. `assembleContextFor(agent)` builds the per-agent assembly context (`agent` + `scope` together). `CreateAgentOptions.setup(agentCtx)` and `ResumeAgentOptions.setup(agentCtx)` compose a fresh or resumed agent's scoped world while both objects remain unpublished. Setup is trusted, composition-only same-process code: drive the agent only after creation resolves. + - `ctx.agents.register(agent: Agent): () => void` — record an **already-constructed** agent. Disposed with the calling fiber. +- Advanced ordered lifecycle: `enter(agent): () => void` performs the authoritative ID collision check and inserts without announcing; `announce(agent)` emits `agent/created` exactly once. A detach requested synchronously by a creation listener is deferred until that dispatch unwinds, and every detach checks the captured entry object, so a stale capability cannot delete a later same-ID replacement. The async factory uses this split; ordinary plugins use `register()`. - `ctx.agents.get(id: AgentId): Agent | undefined` - `ctx.agents.list(): Agent[]` #### Factory seam (creation) -Agent *creation* is provided by whichever plugin implements `AgentFactory` (phase 1: `dsh-agent-loop`), registered via `setFactory`. This keeps creation on the `dsh-agent` interface so consumers (UI, the ACP bridge) program against `ctx.agents` without depending on the concrete loop package. +Agent *creation* is provided by the plugin implementing `AgentFactory` (`dsh-agent-loop`), registered via `setFactory`. This keeps creation on the `dsh-agent` interface so consumers (UI, the ACP bridge) program against `ctx.agents` without depending on the concrete loop package. The registry canonicalizes an already traced Service to its concrete target and re-traces each call through the caller's context; this avoids nested Cordis shadows while passing an explicit caller-bound `ownerCtx` to plain factories. - `ctx.agents.setFactory(factory: AgentFactory): () => void` — register the creation factory (the loop calls this on construction). Throws on a second factory; the slot clears on dispose. -- `ctx.agents.create(options: CreateAgentOptions): AgentHandle` — construct, start, AND register a new agent on a caller-supplied `sessionId` (with optional `meta.cwd`/`meta.parentSession`/`meta.seedLength` and optional `seed` events for forked children). Distinct from `register` (which only records). Throws if no factory is registered. -- `ctx.agents.resume(options: ResumeAgentOptions): Promise` — load a persisted session ([session persistence](../../../docs/rfc/implemented/architecture/2026-06-14-session-persistence.md)) and resume an agent on it. Async; rejects if no factory is registered, or if the factory finds session persistence unconfigured. +- `ctx.agents.create(options: CreateAgentOptions): Promise` — create a session and agent, await optional setup while unpublished, then publish through final `SessionStore.enter()` and `AgentRegistry.enter()` checks. Concurrent same-ID creation is unsupported: more than one operation may prepare, but only one can enter; every loser rolls its private scope/session/driver back. An optional creation-only `signal` cancels unpublished setup and is detached before the handle is returned; later cancellation uses `handle.dispose()` or `agent.cancel()`. Publication is rollback-covered and every delivered creation edge is paired during rollback. Rejects if no factory is registered. +- `ctx.agents.resume(options: ResumeAgentOptions): Promise` — load a persisted session ([session persistence](../../../docs/rfc/implemented/architecture/2026-06-14-session-persistence.md)), mint a fresh unpublished agent scope, await optional setup, and use the same final-entry publication sequence. Its optional `signal` is likewise creation-only. Rejects if no factory is registered or session persistence is unconfigured. -`AgentHandle = { agent: Agent; dispose(): Promise }`. The disposer is a **capability** — only the holder can tear this agent down. `dispose()` stops the loop, `await`s its exit (quiescence — NOT just the `disposed` status flip), unregisters the agent, and removes its session from the store, in an order that captures the loop's final `session/flush` before the session is detached. `ctx.agents.get(id)` still returns a bare `Agent` — the handle is only for the OWNER that created it. The ACP bridge and in-process subagent backends are production consumers; config-created agents are owned by the loop fiber and never need a handle. +`AgentHandle = { agent: Agent; dispose(): Promise }`. The disposer is a **consumer capability** — no observer holding the bare registry entry can tear the agent down. The caller fiber and the registered factory provider are structural co-owners: caller unload enforces structured ownership, while factory unload must stop old instances because their scoped dependency surface belongs to that provider. `dispose()` from any owner reaches one memoized quiescence boundary: it stops the loop, `await`s its exit plus every outstanding idle-injection flush (not just the `disposed` status flip), unregisters the agent, removes its session from the store, and finally unwinds its scoped world. This order captures every agent-started `session/flush` before the session is detached and keeps scoped listeners alive through those checkpoints. `ctx.agents.get(id)` still returns a bare `Agent`; the ACP bridge and in-process subagent backends hold consumer handles, while config-created agents are already owned by the loop fiber. -### Events +### Live events -The full `agent/*` event taxonomy is declared via declaration merging in `dsh-agent` (not `dsh-agent-loop`), so plugins depend only on this package. +`dsh-agent` declares the live `agent/*` coordination vocabulary so plugins do not depend on the concrete loop. Exact signatures, dispatch modes, scope-filtering rules, and payload contracts live in the generated [Cordis event catalog](../../../docs/cordis-catalog/events.md); the [architecture turn flow](../../../docs/architecture.md#turn-flow) shows their order relative to durable session events. -#### Lifecycle (emit) +The lifecycle edges have two important local caveats. `agent/created` runs after scoped setup and after both session and agent registry entries exist. Setup is trusted composition-only code; the immediately following non-vetoing `agent/session-start` notification is the first supported startup injection point. `agent/disposed` always means the exact agent has left the registry. AgentLoop emits it after its driver is quiescent, while ordered teardown may still be detaching the session and unwinding the scope; custom agents registered directly own any stronger driver-ordering contract themselves. -- `agent/created`, `agent/disposed` — registration/deregistration -- `agent/status` — idle / running / disposed transition -- `agent/queued` — message entered inbox (source-resolved, steering flag) -- `agent/session-start` — the session lifecycle began (once, before turn 1), carrying a `SessionStartSource` (`startup` for a fresh or forked create, `resume` for a reloaded persisted session; `clear`/`compact` reserved). A pure notification — it cannot block startup; a listener seeds context via `agent.inject()` (a `context/message` the first request sees). +Most interception points are cooperative waterfalls returning seam-specific decisions. `agent/pre-step` is a serial surface-mutation checkpoint, while `agent/turn-stop` is the terminal serial fold: it runs after ordinary continuation and steering folding, and a returned stop remains in force through turn close and flush so later steering cannot create an extra step or turn. Ordinary queued prompts remain intact. The full rationale is in the [agent-scope runtime-design RFC](../../../docs/rfc/implemented/architecture/2026-07-12-agent-scope-runtime-design.md#three-execution-boundaries-are-deliberately-one-way). -#### Boundaries are durable session events, not `agent/*` emits - -Turn and step boundaries are NOT mirrored as `agent/*` emits: a consumer that needs them reads the durable `turn/start`/`turn/end`/`step/start`/`step/end` events off the `session/event` feed (the session log is the live boundary feed, carrying the `Session` — the turn/step numbers and reasons ride on the event data). See [the event-domain-semantics RFC](../../../docs/rfc/implemented/architecture/2026-06-30-event-domain-semantics.md) and [the remove-boundary-mirror-events RFC](../../../docs/rfc/implemented/simplification/2026-06-20-remove-agent-boundary-mirror-events.md). - -#### Interception seams - -`agent/pre-step` is a **serial** surface-mutation checkpoint; the rest are **waterfalls** that return a small, seam-specific typed **Decision** union (the unified idiom across the taxonomy — a CC/Codex bridge maps its `permissionDecision`/`decision`/`continue` fields onto these, a native plugin returns them directly): - -- `agent/session-start` (emit) — fired once before the first turn; a listener seeds context via `agent.inject()` (it cannot veto startup). -- `agent/prompt-submit` — decide what happens to one drained queued message before it becomes a `user/message`: `PromptDecision` = `allow` (optionally rewriting the prompt `content` or attaching `additionalContext`) or `block` (drop it; a batch whose every prompt is blocked opens a zero-step turn that ends `rejected`). Maps onto Claude Code's `UserPromptSubmit`. -- `agent/pre-step` (serial) — mutate the session surface before the step opens and history is derived (compaction). Fires after `turn/start` and before `step/start`, so a listener's appended events land outside the step; carries the assembled system prompt and the instance's composed session prefix so a token-pressure gate counts everything the request will carry. -- `agent/request` — shape the call config before the model call: a frozen `LlmCallConfig` seed in, a replacement out (model switching, sampling overrides). Content is not shapeable here — every request is a pure function of the session log ([reconstructability RFC](../../../docs/rfc/implemented/architecture/2026-07-05-reconstructable-requests.md)); the loop logs whatever config the request actually uses as a `request/header*` event -- `agent/session-prefix` — compose the session prefix: request-only messages placed in front of the ENTIRE derived history on every request. Fired ONCE per loop instance, lazily before its first pre-step (so pressure gates see this instance's real prefix, never a previous instance's logged one); the composed result is deep-frozen, recorded as `EpochHeader.messagePrefix` on the anchoring `request/header` snapshot, and reused verbatim afterwards — the prefix cannot change mid-session, so the provider prefix cache holds by construction (resume = a new instance = a recompose, attributably anchored by its `'resume'` snapshot). The home for session-stable openers that must not become durable history (a skills catalog, an AGENTS.md digest); `deriveMessages()` never returns it. Content that CHANGES mid-session belongs in the append-only history channels instead — `agent.inject()`, `tools/post-execute` `additionalContext`, prompt-submit `additionalContext` — each a durable `context/message` paid once and prefix-cached thereafter -- `agent/step-result` — post-process the assembled assistant message before tool dispatch (validates what the log records) -- `agent/turn-continuation` — override the continue/stop decision via `ContinuationDecision` = `{action:'stop'}` or `{action:'continue', reason?}` (a `continue` `reason` is recorded as next-step steering in the same turn — the typed `/goal` pattern). Force-continue `/loop`, force-stop budget guard. - -Tool interception is the `tools/pre-execute` / `tools/post-execute` pair in [`dsh-tools`](../tools/README.md) (`PreToolDecision` allow/deny/ask, `PostToolDecision` accept/block) — same typed-Decision idiom, owned there because it is the tool registry's seam. - -#### Error notifications (emit) - -- `agent/error` — step/turn error - -The model's token stream is NOT an `agent/*` event: read it off the durable `session/event` feed as `assistant/chunk` (the same feed persistence and the ACP bridge use). +Turn and step boundaries and the model token stream are durable `session/event` facts rather than mirrored `agent/*` notifications. Consumers read `turn/*`, `step/*`, and `assistant/chunk` from the session feed; tool policy and outcome observation belong to the complete pipeline documented by [`dsh-tools`](../tools/README.md). ### Agent interface (`types.ts`) The handle every plugin programs against: -- `agent.send(content, options?)` — queue a message; starts a turn when idle -- `agent.steer(content, options?)` — steer a running turn (inject between steps); behaves like `send` when idle +- `agent.send(content, options?)` — queue a message; starts a turn when idle. Content and resolved source become one detached, deeply frozen lossless-JSON record before `agent/queued` and enqueue; invalid data throws synchronously, and caller or notification-listener in-place mutation cannot change the log or model input (`agent/prompt-submit` still rewrites by returning replacement content). +- `agent.steer(content, options?)` — steer a running turn (inject between steps); uses the same owned acceptance boundary and behaves like `send` when idle - `agent.inject(content, options?)` — inject in-session context (context/message event); the next request sees it. Does not run the model. While a turn is open it joins that turn; while idle it is wrapped in a one-shot `injection` turn so every event stays turn-enclosed ([the turn-enclosure invariant](../../../docs/rfc/implemented/architecture/2026-06-15-turn-enclosure-invariant.md)) - `agent.cancel(reason?)` — cancel ALL pending work: clears the queued + steering FIFOs, aborts the in-flight step, and drops a turn about to start (the pre-step window) so a queued-but-not-started prompt never runs. A UI/ACP `session/cancel` maps to this. The single public stop primitive. Idle with nothing pending → a safe no-op. - `agent.whenIdle()` — resolve once the agent reaches quiescence after settling out of `running` (idle → immediately; disposed → awaits the loop exit). A non-owner's quiescence-observation hook: it observes the work settling WITHOUT tearing the agent down. Teardown is separate — a lifecycle owner stops and unregisters via `AgentHandle.dispose()`, which awaits the loop exit directly. diff --git a/packages/core/agent/package.json b/packages/core/agent/package.json index e38a6c8d61..b8e6108904 100644 --- a/packages/core/agent/package.json +++ b/packages/core/agent/package.json @@ -24,6 +24,7 @@ "peerDependencies": { "@deepseek-ai/dsh-brand": "^0.0.1", "@deepseek-ai/dsh-llm": "^0.0.1", + "@deepseek-ai/dsh-scope": "^0.0.1", "@deepseek-ai/dsh-session": "^0.0.1", "@deepseek-ai/dsh-system-prompt": "^0.0.1", "cordis": "^4.0.0-rc.6" @@ -31,6 +32,7 @@ "devDependencies": { "@deepseek-ai/dsh-brand": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", + "@deepseek-ai/dsh-scope": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", "@deepseek-ai/dsh-system-prompt": "workspace:^", "cordis": "^4.0.0-rc.6" diff --git a/packages/core/agent/src/dispatch.ts b/packages/core/agent/src/dispatch.ts new file mode 100644 index 0000000000..cbb641d271 --- /dev/null +++ b/packages/core/agent/src/dispatch.ts @@ -0,0 +1,134 @@ +/** + * Fused scope-carrier dispatch for agent-subject operations, plus the assembly + * context builder. The sanctioned ordinary spelling is + * `agentEvents(ctx, agent).waterfall('agent/request', …)`: it builds the scope + * carrier ({@link scopeTarget} keyed by the agent) AND injects the subject as + * the first argument in one move, so a site cannot name a different subject. + * The registry lifecycle pair is the deliberate exception: `enter()` captures + * one stable carrier before commit and `announce()`/detach dispatch through it + * directly, so both lifecycle edges use the same routing identity. The dev + * scoped-dispatch invariant checks both shapes. + * + * @module @deepseek-ai/dsh-agent/dispatch + */ + +import type { Context, Events } from 'cordis' +import { scopeTarget } from '@deepseek-ai/dsh-scope' +import type { Scoped } from '@deepseek-ai/dsh-scope' +import type { AssembleContext } from '@deepseek-ai/dsh-system-prompt' +import type { Agent } from './types.ts' + +/** Extract the parameter tuple from an event handler type (its `this` is not part of the tuple). */ +type Params = F extends (...args: infer P) => unknown ? P : never +/** Extract the return type from an event handler type. */ +type Return = F extends (...args: never[]) => infer R ? R : never + +/** + * The event names whose subject is an agent: handler parameters start with an + * `Agent` AND the handler declares a `Scoped` `this` (the scope-carrier + * contract). The `this` check keeps accidental first-parameter-happens-to-be- + * an-Agent events (or zero-arg events, whose parameter tuple would satisfy a + * bare rest-tuple check via callability) out of the fused-dispatch surface. + */ +export type AgentSubjectEvent = { + [K in keyof Events]: Events[K] extends (this: Scoped, ...args: infer P) => unknown + ? P extends [Agent, ...unknown[]] ? K : never + : never +}[keyof Events] + +/** The event arguments AFTER the injected agent subject. */ +type Tail = Params extends [Agent, ...infer R] ? R : never + +/** + * The fused dispatcher {@link agentEvents} returns: each method dispatches the + * named agent-subject event with the agent's scope carrier as `thisArg` and + * the agent itself injected as the first event argument. + */ +export interface AgentEventDispatch { + /** + * Fire-and-forget notification in the agent's scope. Every listener is + * invoked; synchronous throws and returned-promise rejections are logged and + * contained per listener, so a notification cannot veto lifecycle progress + * or starve a later observer. + * @param name - the agent-subject event to emit. + * @param rest - the event's arguments after the injected agent. + */ + emit(name: K, ...rest: Tail): void + /** + * Awaited in-order dispatch (Cordis `serial`) in the agent's scope. + * @param name - the agent-subject event to dispatch. + * @param rest - the event's arguments after the injected agent. + * @returns the serial chain's result (the first bail value, if any). + */ + serial(name: K, ...rest: Tail): Promise>> + /** + * Around-middleware dispatch (Cordis `waterfall`) in the agent's scope. The + * declared event parameters already end with the `next` callback, so `rest` + * is exactly the event's arguments after the injected agent — the final + * element being the innermost `next` (the default the listener chain wraps). + * @param name - the agent-subject event to dispatch. + * @param rest - the event's arguments after the injected agent. + * @returns the waterfall's composed result. + */ + waterfall(name: K, ...rest: Tail): Return +} + +/** + * Build the fused dispatcher for `agent`'s events (see the module doc). Cheap + * (one carrier + one small object) — dispatch sites create it per run/turn + * rather than caching it on the agent. + * @param ctx - the context to dispatch through (any context of the app). + * @param agent - the subject agent; also the scope-carrier key. + * @returns the fused dispatcher. + */ +export function agentEvents(ctx: Context, agent: Agent): AgentEventDispatch { + const carrier: Scoped = scopeTarget(agent, agent) + // The ordinary dispatch methods forward through Cordis' variadic mixins. The + // fused (carrier, name, agent, ...rest) tuple is provably a valid argument + // list for the matching thisArg overload, but TypeScript cannot relate the + // generic Tail spread back to that overload's conditional parameter + // tuple — hence one contained, shape-preserving cast per method. + return { + emit(name, ...rest) { + // Cordis emit invokes callbacks through Array.map: one synchronous throw + // starves later listeners, and returned promises are discarded. Agent + // notifications are non-vetoing, so resolve the same filtered callback + // set ourselves and contain both failure modes independently. + const args: unknown[] = [carrier, name, agent, ...rest] + const callbacks = ctx.events.dispatch('emit', args) + for (const callback of callbacks) { + try { + const returned: unknown = callback(...args) + void Promise.resolve(returned).catch((error: unknown) => { + ctx.logger.warn(`agent event "${name}" listener rejected: ${String(error)}`) + }) + } catch (error: unknown) { + ctx.logger.warn(`agent event "${name}" listener threw: ${String(error)}`) + } + } + }, + async serial(name, ...rest) { + // eslint-disable-next-line @typescript-eslint/unbound-method -- the events mixin accessor returns a pre-bound function + const serial = ctx.serial as (thisArg: Scoped, name: string, ...args: unknown[]) => Promise + return await serial(carrier, name, agent, ...rest) + }, + waterfall(name, ...rest) { + // eslint-disable-next-line @typescript-eslint/unbound-method -- the events mixin accessor returns a pre-bound function + const waterfall = ctx.waterfall as (thisArg: Scoped, name: string, ...args: unknown[]) => never + return waterfall(carrier, name, agent, ...rest) + }, + } +} + +/** + * The assembly context for one agent's prompt: the typed `agent` DX field and + * the `scope` layer selector, set together (setting `agent` without `scope` + * silently drops the agent's scoped sections/tools from the assembly — the + * dev invariants flag it). THE way the loop (and any custom driver) builds + * its per-step `ctx.systemPrompt.assemble(…)` input. + * @param agent - the agent the assembly is for. + * @returns the context to pass to `assemble()`. + */ +export function assembleContextFor(agent: Agent): AssembleContext { + return { agent, scope: agent } +} diff --git a/packages/core/agent/src/index.ts b/packages/core/agent/src/index.ts index 096555f925..b77e3e8a60 100644 --- a/packages/core/agent/src/index.ts +++ b/packages/core/agent/src/index.ts @@ -5,15 +5,29 @@ * @module @deepseek-ai/dsh-agent */ -import { Context, Service } from 'cordis' +import { Context, getTraceable, Service, symbols } from 'cordis' +import { scopeTarget } from '@deepseek-ai/dsh-scope' +import type { Scoped } from '@deepseek-ai/dsh-scope' import type { SessionEvent, SessionId } from '@deepseek-ai/dsh-session' import type { Agent, AgentId, AgentOptions } from './types.ts' export * from './types.ts' +export { agentEvents, assembleContextFor } from './dispatch.ts' +export type { AgentEventDispatch, AgentSubjectEvent } from './dispatch.ts' declare module 'cordis' { interface Context { agents: AgentRegistry + /** + * The agent association installed as an own property on `Agent.ctx`, or + * `undefined` on a plain context. Contexts derived from `Agent.ctx` inherit + * the association; a deliberately nested scope may carry a nearer + * `dsh-scope` tag while retaining it, so this field is DX context rather + * than the scope resolver. {@link AgentRegistry} registers a root accessor + * defaulting to `undefined`, and core packages below the agent layer use + * `scopeOf()` for layer selection instead of reading this field. + */ + agent?: Agent } } @@ -26,31 +40,52 @@ declare module 'cordis' { */ export interface CreateAgentOptions { /** The agent's id (the registry handle). */ - agentId: AgentId + readonly agentId: AgentId /** The live session's id (NOT derived from agentId). */ - sessionId: SessionId + readonly sessionId: SessionId /** * Session creation metadata: validated absolute `cwd`, `parentSession` * fork lineage, and the `seedLength` seed boundary. Mirrors the * `cwd`/`parentSession`/`seedLength` fields of * {@link CreateSessionOptions.meta} in dsh-session (the internal-only * `createdAt`, used when reconstructing a persisted session, is deliberately - * excluded — a factory caller never sets it). + * excluded — a factory caller never sets it). This is durable session data, + * so the session boundary validates and snapshots it before asynchronous + * setup begins. */ - meta?: { cwd?: string; parentSession?: SessionId; seedLength?: number } + readonly meta?: { readonly cwd?: string; readonly parentSession?: SessionId; readonly seedLength?: number } /** * Seed events to reconstruct the child session's log from (the fork lineage * primitive). When present, the factory creates the session with this event * prefix so `deriveMessages()`/`lastTurnNumber` continue from it — used by the * in-process FORK subagent backend to seed a child with a balanced * completed-turn prefix of the parent's log. The prefix MUST be contiguous - * from seq 0 and balanced (no open turn/step, no dangling tool-call), or the - * session constructor (and the dev-mode invariants replay) reject it. Absent - * for a fresh (spawn) child. + * from seq 0, carry only lossless-JSON data, and be balanced (no open + * turn/step, no dangling tool-call), or the session constructor (and the + * dev-mode invariants replay) reject it. The factory passes the raw seed to + * the session's durable validator/snapshot boundary. Absent for a fresh + * (spawn) child. */ - seed?: SessionEvent[] + readonly seed?: readonly SessionEvent[] /** Per-agent options (model, …). */ - agentOptions?: AgentOptions + readonly agentOptions?: AgentOptions + /** Optional creation-only cancellation signal; detached before the returned handle becomes visible. */ + readonly signal?: AbortSignal + /** + * Creation-time composition of the agent's scoped world. The factory awaits + * setup after minting `agentCtx` but BEFORE inserting or announcing either + * the session or agent, so observers can never see a partially configured + * world. Everything registered through `agentCtx` (scoped tools, prompt + * sections/variables, `restrict()`, listeners, awaited child plugins) exists + * before `session/created`, `agent/created`, `agent/session-start`, and the + * first prompt assembly. A throw/rejection or owner disposal rolls the scope + * back without publishing either id. + * + * **Setup composes, it never drives**: the callback is trusted same-process + * code and receives the full scoped context, so this is a contract rather + * than a runtime restriction. Drive the agent only after creation resolves. + */ + readonly setup?: (agentCtx: Context) => Promise | void } /** @@ -59,24 +94,42 @@ export interface CreateAgentOptions { */ export interface ResumeAgentOptions { /** The agent's id (the registry handle). */ - agentId: AgentId + readonly agentId: AgentId /** The persisted session id to load and resume on. */ - resumeSessionId: SessionId + readonly resumeSessionId: SessionId /** Per-agent options (model, …). */ - agentOptions?: AgentOptions + readonly agentOptions?: AgentOptions + /** Optional creation-only cancellation signal for persistence load/setup; detached before return. */ + readonly signal?: AbortSignal + /** + * Resume-time composition of the agent's fresh scoped world. Persistence is + * loaded first; the factory then mints `agentCtx` and awaits setup while the + * reconstructed session and agent remain unpublished. The callback has the + * same trusted composition-only contract as + * {@link CreateAgentOptions.setup}: all registrations exist before either + * creation announcement, and rejection or owner disposal rolls the + * transaction back without publishing either id. + */ + readonly setup?: (agentCtx: Context) => Promise | void } /** * An owned agent plus its disposer, returned by {@link AgentRegistry.create} / - * {@link AgentRegistry.resume}. The disposer is a CAPABILITY: only the holder - * can tear this agent down. `dispose()` unregisters the agent, stops its loop, - * awaits the loop's exit (quiescence — NOT just the `disposed` status flip), and - * removes the agent's session from the store, in an order that captures the - * loop's final `session/flush` before the session is detached. + * {@link AgentRegistry.resume}. The disposer is a CAPABILITY: among consumers, + * only the holder can tear this agent down. The registered factory provider is + * also a structural owner because the scoped agent depends on that provider's + * service surface; provider unload stops and drains every live handle it made. + * `dispose()` stops the loop, awaits its exit and every outstanding + * idle-injection flush (quiescence — NOT just the `disposed` + * status flip), unregisters the agent, removes its session from the store, and + * finally unwinds its scoped world. This order captures every agent-started + * `session/flush` before the session is detached and keeps scoped listeners + * alive through those checkpoints. * - * `ctx.agents.get(id)` still returns a bare {@link Agent} — the handle is only - * for the OWNER that created it. Config-created agents (the loop's own startup) - * are owned by the loop fiber and never need a handle. + * `ctx.agents.get(id)` still returns a bare {@link Agent} — the handle is + * exposed only to the consumer owner that created it; the structural provider + * reaches the same teardown internally. Config-created agents (the loop's own + * startup) are owned by the loop fiber and never need a handle. */ export interface AgentHandle { agent: Agent @@ -91,121 +144,289 @@ export interface AgentHandle { */ export interface AgentFactory { /** - * Create, start, and register a new agent on a caller-supplied session id. - * Returns an {@link AgentHandle} — the owner disposes it to tear down exactly - * this agent (unregister + stop loop + await quiescence + remove session). + * Create a new agent on a caller-supplied session id. Async because creation + * awaits unpublished setup, inserts both session and agent, emits their + * creation notifications in order, emits `agent/session-start`, and only + * then starts the loop. The sequence is + * rollback-covered, but notifications delivered before a later listener + * failure remain observable; every agent or session creation announcement + * that began is paired by `agent/disposed` or `session/disposed` during + * rollback. The owner disposes the resolved handle to stop/drain, + * unregister, remove the session, and unwind the scope. + * The registry passes a context carrying the `create()` caller's fiber and + * scope as `ownerCtx`. The implementation attaches the unpublished + * transaction and resulting lifecycle to that owner; it must not infer + * ownership from the factory object's registration context. + * @param ownerCtx - caller-bound context that owns the transaction and live handle. + * @param options - agent/session identity, configuration, and optional setup. + * @returns the owned handle after setup, both announcements, and loop start complete. */ - createAgent(options: CreateAgentOptions): AgentHandle + createAgent(ownerCtx: Context, options: CreateAgentOptions): Promise /** * Load a persisted session and resume an agent on it. Async because it awaits - * `ctx.sessionPersistence.load`; must be called after that service exists - * (consumers inject `sessionPersistence`). Returns an {@link AgentHandle}. + * both `ctx.sessionPersistence.load` and the optional unpublished setup + * transaction; must be called after that service exists (consumers inject + * `sessionPersistence`). Publication follows the same ordered boundary as + * {@link createAgent}. + * @param ownerCtx - caller-bound context that owns load, setup, and the live handle. + * @param options - persisted identity, configuration, and optional setup. + * @returns the owned handle after setup, both announcements, and loop start complete. */ - resume(options: ResumeAgentOptions): Promise + resume(ownerCtx: Context, options: ResumeAgentOptions): Promise } /** Thrown when create/resume is called before an agent factory is registered. */ const NO_FACTORY_MESSAGE = 'no agent factory registered (load an agent-loop plugin)' +/** All mutable lifecycle state for one exact registry entry. */ +interface AgentEntry { + readonly id: AgentId + readonly agent: Agent + readonly carrier: Scoped + announced: boolean + announcing: boolean + detachRequested: boolean +} + +/** Plain holder prevents Cordis from tracing the factory field before the caller context is known. */ +interface FactorySlot { + readonly target: AgentFactory +} + /** * Agent registry (`ctx.agents`): tracks live agents so UI, hook, and * orchestrator plugins can find them without depending on the concrete loop * package. Agent *creation* is provided by whichever plugin implements the - * {@link AgentFactory} (phase 1: `@deepseek-ai/dsh-agent-loop`), registered via + * {@link AgentFactory} (`@deepseek-ai/dsh-agent-loop`), registered via * {@link setFactory}. */ export class AgentRegistry extends Service { - private store = new Map() - private factory: AgentFactory | undefined + private store = new Map() + private entries = new WeakMap() + private factory: FactorySlot | undefined constructor(ctx: Context) { super(ctx, 'agents') + // The `ctx.agent` DX accessor: default `undefined` on every context, so a + // plain plugin context reads cleanly instead of hitting the Cordis + // unknown-property throw. Each Agent.ctx shadows it with an own property + // (own properties resolve before the context proxy is consulted), so the + // accessor body never needs to resolve a scope itself. Effect-scoped: + // unwinds with this service's fiber. + ctx.accessor('agent', { get: () => undefined }) } /** * Register the agent-creation factory (the loop calls this on construction, - * effect-scoped). Throws if a factory is already registered. Returns the - * disposer; on dispose the factory slot is cleared. + * effect-scoped). A traced Cordis service is canonicalized to its concrete + * target; each create/resume call is then traced through that caller's + * context so ownership follows the caller without stacking proxy layers. + * Throws if a factory is already registered. Returns the disposer; on + * dispose the factory slot is cleared. * @param factory - the loop-owned factory {@link create}/{@link resume} delegate to. - * @returns the disposer that clears the factory slot. + * @returns the disposer that clears the factory slot. The exact + * Cordis effect disposer (single-shot): composite (generator) effects may + * yield it directly — exact identity nests the teardown in order. */ setFactory(factory: AgentFactory): () => void { const dispose = this.ctx.effect(() => { if (this.factory !== undefined) throw new Error('an agent factory is already registered') - this.factory = factory + // Avoid stacking two Cordis shadow layers when a caller passes a Service + // already read through a context. Calls are re-traced through their + // actual owner context below. + const target = (factory as AgentFactory & { [symbols.original]?: AgentFactory })[symbols.original] ?? factory + this.factory = { target } return () => { this.factory = undefined } }, 'agents.setFactory()') - return () => void dispose() + // The exact cordis effect disposer (the agents.register() convention): a + // caller's composite effect can yield it for in-order teardown; the + // loop's constructor effect returns it directly, identity-nesting the + // registration under that effect. + // eslint-disable-next-line @typescript-eslint/no-misused-promises -- synchronous cleanup; direct return preserves disposer identity + return dispose + } + + /** Return the active creation factory. */ + private requireFactory(): FactorySlot { + if (this.factory === undefined) throw new Error(NO_FACTORY_MESSAGE) + return this.factory } /** - * Create, start, and register a new agent through the registered factory. + * Create and publish a new agent through the registered factory. * Distinct from {@link register} (which records an already-constructed - * agent): this constructs the agent and its session. Throws if no factory is - * registered. Returns an {@link AgentHandle} — the owner disposes it to tear - * down exactly this agent. + * agent): this constructs the agent and its session. Rejects if no factory is + * registered or creation/setup fails. The resolved {@link AgentHandle} lets + * the owner tear down exactly this agent. * @param options - agent id, session id/seed/metadata, and agent options. - * @returns the handle whose dispose tears down exactly this agent. + * @returns the handle after setup, rollback-covered publication, and loop start complete. */ - create(options: CreateAgentOptions): AgentHandle { - if (this.factory === undefined) throw new Error(NO_FACTORY_MESSAGE) - return this.factory.createAgent(options) + async create(options: CreateAgentOptions): Promise { + const ownerCtx = this.ctx + // Re-trace a Service-backed factory through the accessing context + // explicitly. This preserves AgentLoop's dependency origin while binding + // its effects to ownerCtx; plain factories receive ownerCtx as an explicit + // capability and need no Cordis tracker magic. + const { target } = this.requireFactory() + const receiver = getTraceable(ownerCtx, target) + // eslint-disable-next-line @typescript-eslint/unbound-method -- Reflect.apply intentionally supplies the caller-traced receiver + return Reflect.apply(target.createAgent, receiver, [ownerCtx, options]) } /** * Load a persisted session and resume an agent on it through the registered * factory. Rejects if no factory is registered; the factory rejects if - * session persistence is not configured. Returns an {@link AgentHandle}. - * @param options - the persisted session id plus agent id and options. - * @returns the handle for the resumed agent. + * session persistence is not configured or persistence/setup fails. + * @param options - persisted identity, configuration, and optional setup. + * @returns the handle after setup, rollback-covered publication, and loop start complete. */ async resume(options: ResumeAgentOptions): Promise { - if (this.factory === undefined) throw new Error(NO_FACTORY_MESSAGE) - return this.factory.resume(options) + const ownerCtx = this.ctx + const { target } = this.requireFactory() + const receiver = getTraceable(ownerCtx, target) + // eslint-disable-next-line @typescript-eslint/unbound-method -- Reflect.apply intentionally supplies the caller-traced receiver + return Reflect.apply(target.resume, receiver, [ownerCtx, options]) } /** * Register a live agent. Throws if an agent with the same id is already * registered. Emits `agent/created` on registration and `agent/disposed` - * when the calling fiber is disposed. Returns the disposer. + * when the calling fiber is disposed — both with the agent's scope carrier + * (`scopeTarget(agent, agent)`): the subject is the agent in hand, so the + * emits are scope-filtered regardless of which context invoked `register` + * (calling through `agent.ctx` scopes EFFECTS; dispatch scoping always + * requires passing the carrier). Returns the disposer. * @param agent - the already-constructed agent to record in the store. - * @returns the disposer that removes the agent and emits `agent/disposed`. + * @returns the EXACT Cordis effect disposer (single-shot; a repeat call + * returns undefined without awaiting an in-flight teardown). Exact + * identity is load-bearing: a composite (generator) effect that owns a + * teardown ORDER — the agent factory's lifecycle chain — must yield THIS + * function so Cordis nests the unregistration at that yield position; + * yielding a wrapper would leave it disposing as a concurrent sibling on + * owner unload, unregistering the agent (and emitting `agent/disposed`) + * while its final turn is still draining. */ register(agent: Agent): () => void { const dispose = this.ctx.effect(function* (this: AgentRegistry) { - if (this.store.has(agent.id)) { - throw new Error(`agent "${agent.id}" is already registered`) - } - this.store.set(agent.id, agent) - // Yield the rollback BEFORE emitting `agent/created`: a generator effect - // collects each yielded disposer before the next step runs, so a - // throwing `agent/created` listener rolls the entry back instead of - // leaking it (a leak would wedge the duplicate-id check until restart). - // The duplicate throw above fires before any mutation — it leaks nothing. - yield () => { - this.store.delete(agent.id) - // CONTAIN a throwing `agent/disposed` listener: this disposer runs as - // one link in the owning fiber/effect's disposal chain, and Cordis - // chains later disposers with `task.then(next)` — so an UNCAUGHT throw - // here rejects the chain and SKIPS every later disposer. When this - // registration shares a composite effect with a session (the agent - // factory's `AgentLoop.start`, where the session-detach disposer runs - // AFTER this one), a swallowed-less throw would strand the session in - // the store with `onAppend` attached — a leak AND a durability hole. - // The store entry is already removed above (the useful state), so - // logging the listener bug and continuing is correct (mirrors the - // guarded `agent/status` emit in dsh-agent-loop's ReactLoopAgent). - try { - this.ctx.emit('agent/disposed', agent) - } catch (error: unknown) { - this.ctx.logger.warn(`agent "${agent.id}": agent/disposed listener threw: ${String(error)}`) - } - } - this.ctx.emit('agent/created', agent) + yield this.enter(agent) + this.announce(agent) }.bind(this), 'agents.register()') - // ctx.effect's disposer returns Promise; our disposer API is - // synchronous fire-and-forget — discard the (always-resolved) promise. - return () => void dispose() + // eslint-disable-next-line @typescript-eslint/no-misused-promises -- synchronous cleanup; direct return preserves disposer identity + return dispose + } + + /** + * Insert an already-constructed agent without announcing it. This is the + * advanced ordered-lifecycle primitive used by the async agent factory: it + * first completes setup while the agent is unpublished, then assigns the + * returned detach closure into its pre-installed composite teardown before + * calling {@link announce}. Ordinary callers use {@link register}. + * @param agent - the prepared, unpublished agent. + * @returns an idempotent closure that removes this exact entry and emits + * `agent/disposed` with listener failures contained. When called from a + * synchronous `agent/created` listener, removal and disposal wait until + * that creation dispatch unwinds. + */ + enter(agent: Agent): () => void { + const id = agent.id + const carrier = scopeTarget(agent, agent) + // This is the authoritative collision boundary. Concurrent create/resume + // operations may both prepare, but only one exact entry can publish. + if (this.entries.has(agent) || this.store.has(id)) throw new Error(`agent "${id}" is already registered`) + const entry: AgentEntry = { + id, + agent, + carrier, + announced: false, + announcing: false, + detachRequested: false, + } + this.store.set(id, entry) + this.entries.set(agent, entry) + let entered = true + const detach = (): void => { + if (!entered) return + entered = false + // Every callback reached by this creation dispatch must observe the same + // live entry, and disposal must follow creation. A listener may own + // the advanced detach capability, so make that ordering structural: + // visibility and the paired disposal are deferred until announce()'s + // synchronous dispatch has unwound. + if (entry.announcing) { + entry.detachRequested = true + return + } + this.detachEntered(entry) + } + return detach + } + + /** Remove one exact entered agent and emit its paired disposal when announced. */ + private detachEntered(entry: AgentEntry): void { + entry.detachRequested = false + // A stale capability can never delete a later same-id lifecycle. The + // captured entry identity is the final boundary. + /* v8 ignore next -- enter() rejects replacement while this single-shot detach capability is live. */ + if (this.store.get(entry.id) !== entry) return + this.store.delete(entry.id) + this.entries.delete(entry.agent) + // An insertion rolled back before announce was never externally created, + // so emitting disposed would invent an impossible lifecycle edge. Marking + // happens before the created emit: if a later created listener throws, + // earlier listeners may already have observed it and must see disposal. + if (!entry.announced) return + this.emitDisposed(entry) + } + + /** Emit the paired disposal edge through the entry's stable carrier. */ + private emitDisposed(entry: AgentEntry): void { + const args: unknown[] = [entry.carrier, 'agent/disposed', entry.agent] + for (const callback of this.ctx.events.dispatch('emit', args)) { + try { + const returned: unknown = callback(...args) + void Promise.resolve(returned).catch((error: unknown) => { + this.ctx.logger.warn(`agent "${entry.id}": agent/disposed listener rejected: ${String(error)}`) + }) + } catch (error: unknown) { + this.ctx.logger.warn(`agent "${entry.id}": agent/disposed listener threw: ${String(error)}`) + } + } + } + + /** + * Announce an agent previously inserted with {@link enter}. + * @param agent - the live inserted agent to announce. + * @throws if `agent` is not the exact live registry entry for its id, or its + * creation announcement already began (including a reentrant call from a + * creation listener). + */ + announce(agent: Agent): void { + const entry = this.entries.get(agent) + if (entry === undefined || this.store.get(entry.id) !== entry) { + throw new Error(`agent "${agent.id}" is not live in this registry`) + } + if (entry.announced || entry.announcing) { + throw new Error(`agent "${entry.id}" was already announced`) + } + // Mark before dispatch so a listener cannot recursively create a second + // lifecycle edge; detach still pairs a partially delivered first edge. + entry.announcing = true + entry.announced = true + const args: unknown[] = [entry.carrier, 'agent/created', entry.agent] + try { + for (const callback of this.ctx.events.dispatch('emit', args)) { + // A synchronous creation failure vetoes publication and rolls back. + // Returned-promise rejection happens after this synchronous boundary, so + // observe and report it instead of leaking an unhandled rejection. + const returned: unknown = callback(...args) + void Promise.resolve(returned).catch((error: unknown) => { + this.ctx.logger.warn(`agent "${entry.id}": agent/created listener rejected: ${String(error)}`) + }) + } + } finally { + entry.announcing = false + if (entry.detachRequested) this.detachEntered(entry) + } } /** @@ -214,7 +435,7 @@ export class AgentRegistry extends Service { * @returns the agent, or undefined when no live agent has that id. */ get(id: AgentId): Agent | undefined { - return this.store.get(id) + return this.store.get(id)?.agent } /** @@ -222,7 +443,7 @@ export class AgentRegistry extends Service { * @returns a fresh array; mutating it does not affect the registry. */ list(): Agent[] { - return [...this.store.values()] + return [...this.store.values()].map(entry => entry.agent) } } diff --git a/packages/core/agent/src/types.ts b/packages/core/agent/src/types.ts index 2bde463be5..7a046dcc82 100644 --- a/packages/core/agent/src/types.ts +++ b/packages/core/agent/src/types.ts @@ -18,8 +18,8 @@ * - **`agent/*`** (this module) — the LIVE runtime surface. Always carries the * live `Agent`. Two shapes: INTERCEPTION seams (the `agent/prompt-submit`/ * `agent/request`/`agent/session-prefix`/`agent/step-result`/ - * `agent/turn-continuation` waterfalls and - * the serial `agent/pre-step`) that mutate/veto, and TRANSIENT emits + * `agent/turn-continuation` waterfalls and the serial `agent/pre-step` / + * `agent/turn-stop` checkpoints) that mutate/veto, and TRANSIENT emits * (`agent/status`, `agent/error`, `agent/created`/ * `agent/disposed`, `agent/queued`, `agent/session-start`) * that notify with the `Agent` in hand. Turn/step boundaries are NOT here — @@ -37,14 +37,17 @@ * and `docs/rfc/implemented/simplification/2026-06-20-remove-agent-boundary-mirror-events.md`. * * The interception waterfalls here (`agent/prompt-submit`, `agent/request`, - * `agent/step-result`, `agent/turn-continuation`) each return a typed Decision — - * the convention pinned by + * `agent/step-result`, `agent/turn-continuation`) each return a typed Decision; + * the terminal serial `agent/turn-stop` returns the stop-only subset. The + * convention is pinned by * `docs/rfc/implemented/feature/2026-06-30-interception-seams.md`. * * @module @deepseek-ai/dsh-agent/types */ import type { Branded } from '@deepseek-ai/dsh-brand' +import type { Context } from 'cordis' +import type { Scoped } from '@deepseek-ai/dsh-scope' import type { ContentBlock, LlmCallConfig, Message, MessageSource } from '@deepseek-ai/dsh-llm' import type {} from '@deepseek-ai/dsh-system-prompt' @@ -65,19 +68,23 @@ declare module '@deepseek-ai/dsh-system-prompt' { interface AssembleContext { /** * The agent this assembly is for. The agent loop passes it on every - * per-step `assemble({ agent })`; variable providers project per-agent - * facts from it (`options.model` → `{{model}}`, `session.header.cwd` → + * per-step assembly (via its `assembleContextFor(agent)` helper, which + * also sets the `scope` field to the same agent — the layer selector + * `dsh-system-prompt` reads); variable providers project per-agent facts + * from it (`options.model` → `{{model}}`, `session.header.cwd` → * `{{cwd}}`). Optional because a bare `assemble()` (tests, diagnostics) - * has no agent — providers must tolerate its absence. + * has no agent — providers must tolerate its absence. Never set `agent` + * without `scope`: the assembly would silently miss the agent's scoped + * sections/tools (the dev invariants flag it). */ agent?: Agent } } /** - * Options an agent is created with. The persona is NOT here — it is the - * deployment's `persona` config on the dsh-system-prompt plugin, shared by - * every agent in the context. + * Options an agent is created with. The persona is NOT here: the + * dsh-system-prompt config supplies the global default, and a scoped + * `deployment:persona` section may override it for one agent. * Merge-extensible: plugins declare extra fields via declaration merging. */ export interface AgentOptions { @@ -155,6 +162,13 @@ export type ContinuationDecision = | { action: 'stop' } | { action: 'continue'; reason?: HookContext } +/** + * The terminal subset of {@link ContinuationDecision}. A listener on + * `agent/turn-stop` returns this to make the already-composed continuation + * outcome terminal; `undefined` abstains. + */ +export type ContinuationStop = Extract + /** * Why an agent's session lifecycle began, carried by `agent/session-start`. A * bridge keys its SessionStart hook's matcher on this (Claude Code's @@ -176,13 +190,33 @@ export interface Agent { readonly options: AgentOptions readonly session: Session readonly status: AgentStatus + /** + * The agent's scope context (`@deepseek-ai/dsh-scope`, key = this agent). + * Registrations through it — tools, prompt sections/variables, event + * listeners, restrictions — are visible to THIS agent only and unwind when + * the agent is disposed; `agent.ctx.on('agent/…')` listeners fire only for + * this agent's dispatches (zero self-filtering). Service resolution through + * it flows through the loop plugin's dependency surface — handing out + * `agent.ctx` hands out that capability. Live for exactly the agent's + * lifetime: registrations after disposal throw Cordis's INACTIVE_EFFECT. + */ + readonly ctx: Context - /** Queue a user message. Starts a turn when idle; otherwise waits for the next turn. */ + /** + * Queue a user message. Starts a turn when idle; otherwise waits for the next + * turn. Content and the resolved source are accepted as one detached, + * deeply-frozen lossless-JSON record before notification or enqueue, so + * caller or `agent/queued` listener in-place mutation cannot change later + * log/model input. Throws synchronously when either value is not losslessly + * JSON-serializable; `agent/prompt-submit` may still return an explicit + * replacement. + */ send(content: ContentBlock[], options?: SendOptions): void /** * Steer a running turn: content is injected between steps of the current - * turn. When idle, behaves like {@link send}. + * turn. Uses the same owned-value and synchronous-validation boundary as + * {@link send}; when idle, behaves exactly like that method. */ steer(content: ContentBlock[], options?: SendOptions): void @@ -197,8 +231,10 @@ export interface Agent { * turn (`turn/start` → `context/message` → `turn/end`) and checkpoints it for * durability, so every event stays inside a turn and a persistence backend * never loses a between-turn notice. The idle checkpoint is fire-and-forget - * (inject is synchronous): a failing flush is reported via `agent/error` - * (step `0`) and the logger, never thrown into the caller. + * from this synchronous method, but lifecycle disposal awaits it before + * unregistering the agent or detaching its session. A failing flush is + * reported via `agent/error` (step `0`) and the logger, never thrown into the + * caller. * * Live-adapter review has validated the tagged-envelope rendering against * current DeepSeek behavior; provider-specific mismatches belong in that @@ -257,52 +293,92 @@ declare module 'cordis' { interface Events { // ---- lifecycle (emit) ---- /** - * An agent was registered in the {@link AgentRegistry} and is ready to - * receive messages. - * @param agent - the newly registered agent, already resolvable in the registry. + * An agent's fully composed scoped world was published in the + * {@link AgentRegistry}. Its session is already live in the session store. + * Setup is composition-only by contract; the subsequent + * `agent/session-start` boundary is the first supported place to inject or + * queue startup work. A synchronous listener throw + * vetoes publication and rollback emits the matching disposal edges; + * returned-promise rejection is observed and logged but cannot + * retroactively veto this synchronous boundary. A synchronous listener + * that requests the advanced registry detach does not remove the entry + * immediately: removal and the paired `agent/disposed` edge wait until the + * creation dispatch unwinds, so no later creation listener observes a + * disposal that preceded its own creation callback. + * @param agent - the newly registered agent with its live session and completed setup. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered + * through `agent.ctx` fires only for that agent's dispatches; a listener on a + * plain plugin context fires for every agent. The dispatch `this` is the + * scope carrier (`Scoped`), built by the emitting side via + * `scopeTarget`/`agentEvents`. * @mode emit */ - 'agent/created'(agent: Agent): void + 'agent/created'(this: Scoped, agent: Agent): void /** - * An agent was disposed and removed from the registry; its fiber and any - * in-flight turn have been torn down. - * @param agent - the agent that was torn down; its handle is now inert. + * An agent was removed from the registry. The concrete AgentLoop lifecycle + * emits this only after its driver and any in-flight turn reach quiescence; + * a custom agent registered through the public registry owns its own driver + * contract, which the registry cannot infer. Ordered teardown may still be + * detaching the session and unwinding scoped registrations when this runs. + * @param agent - the exact agent removed from the registry. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered + * through `agent.ctx` fires only for that agent's dispatches; a listener on a + * plain plugin context fires for every agent. The dispatch `this` is the + * scope carrier (`Scoped`), built by the emitting side via + * `scopeTarget`/`agentEvents`. * @mode emit */ - 'agent/disposed'(agent: Agent): void + 'agent/disposed'(this: Scoped, agent: Agent): void /** * Agent status changed (`idle` ⇄ `running`, or → `disposed`). Drive * lifecycle off this transition, never off a status you just requested — * `send()` does not flip status to `running` before it returns. * @param agent - the agent whose status flipped. * @param status - the status just entered (the transition's destination). + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered + * through `agent.ctx` fires only for that agent's dispatches; a listener on a + * plain plugin context fires for every agent. The dispatch `this` is the + * scope carrier (`Scoped`), built by the emitting side via + * `scopeTarget`/`agentEvents`. * @mode emit */ - 'agent/status'(agent: Agent, status: AgentStatus): void + 'agent/status'(this: Scoped, agent: Agent, status: AgentStatus): void /** - * A message entered the agent's inbox (queued or steering). `source` is - * the resolved source (defaults applied), not the caller's raw options. + * A message entered the agent's inbox (queued or steering). Content and the + * resolved source are the detached, deeply-frozen values retained by the + * inbox. `source` has defaults applied and is not the caller's raw options. * @param agent - the agent whose inbox received the message. - * @param content - the enqueued content blocks, verbatim. - * @param info - the resolved source plus whether it entered as steering. + * @param content - the accepted content blocks retained by the inbox. + * @param info - the accepted source plus whether it entered as steering. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered + * through `agent.ctx` fires only for that agent's dispatches; a listener on a + * plain plugin context fires for every agent. The dispatch `this` is the + * scope carrier (`Scoped`), built by the emitting side via + * `scopeTarget`/`agentEvents`. * @mode emit */ - 'agent/queued'(agent: Agent, content: ContentBlock[], info: { source: MessageSource; steering: boolean }): void + 'agent/queued'(this: Scoped, agent: Agent, content: ContentBlock[], info: { source: MessageSource; steering: boolean }): void // ---- session lifecycle (emit) ---- /** * The agent's session lifecycle began, fired once before its first turn. * `source` says why ({@link SessionStartSource}: fresh startup, a resumed - * persisted session, …). A pure NOTIFICATION (emit, not waterfall): it - * carries no veto — a session-start listener that wants to seed context does - * so via `agent.inject()` (a `context/message` the first request sees), not - * by returning a decision. Cannot block the session from starting; that gap - * is deliberate (a bridge logs/injects, it does not gate startup). + * persisted session, …). A pure NOTIFICATION (emit, not waterfall): a + * listener cannot veto by returning a decision or throwing. A listener that + * wants to seed context does so via `agent.inject()` (a `context/message` the + * first request sees). A lifecycle owner can still dispose its structural + * ownership edge during this notification; publication rechecks liveness and + * then aborts before the driver starts. * @param agent - the agent whose session lifecycle began. * @param source - why the session started (fresh startup, resume, …). + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered + * through `agent.ctx` fires only for that agent's dispatches; a listener on a + * plain plugin context fires for every agent. The dispatch `this` is the + * scope carrier (`Scoped`), built by the emitting side via + * `scopeTarget`/`agentEvents`. * @mode emit */ - 'agent/session-start'(agent: Agent, source: SessionStartSource): void + 'agent/session-start'(this: Scoped, agent: Agent, source: SessionStartSource): void // Turn and step boundaries are NOT mirrored as agent/* emits: a consumer // that needs them reads the durable `turn/start`/`turn/end`/`step/start`/ @@ -340,6 +416,11 @@ declare module 'cordis' { * request will actually send (never a stale logged one). `signal` cancels * any in-flight work a listener starts (e.g. a * summarization model call). + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered + * through `agent.ctx` fires only for that agent's dispatches; a listener on a + * plain plugin context fires for every agent. The dispatch `this` is the + * scope carrier (`Scoped`), built by the emitting side via + * `scopeTarget`/`agentEvents`. * @param agent - the agent about to open the step. * @param turn - the already-open turn this step belongs to. * @param step - the number of the step about to start. @@ -354,7 +435,7 @@ declare module 'cordis' { // reads. Revisit if no second consumer appears: e.g. hand listeners a lazy // prompt provider, or move token-pressure measurement behind a // compaction-specific seam instead of the shared pre-step checkpoint. - 'agent/pre-step'(agent: Agent, turn: number, step: number, fullSystemPrompt: string, sessionPrefix: readonly Message[], signal: AbortSignal): Promise | void + 'agent/pre-step'(this: Scoped, agent: Agent, turn: number, step: number, fullSystemPrompt: string, sessionPrefix: readonly Message[], signal: AbortSignal): Promise | void /** * Waterfall: decide what happens to ONE drained queued message before it * becomes a `user/message` — allow (optionally rewriting the prompt bytes or @@ -365,9 +446,14 @@ declare module 'cordis' { * @param agent - the agent draining its inbox. * @param content - the drained message's blocks, as queued. * @param source - the message's resolved source. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered + * through `agent.ctx` fires only for that agent's dispatches; a listener on a + * plain plugin context fires for every agent. The dispatch `this` is the + * scope carrier (`Scoped`), built by the emitting side via + * `scopeTarget`/`agentEvents`. * @mode waterfall */ - 'agent/prompt-submit'(agent: Agent, content: ContentBlock[], source: MessageSource, next: () => Promise): Promise + 'agent/prompt-submit'(this: Scoped, agent: Agent, content: ContentBlock[], source: MessageSource, next: () => Promise): Promise /** * Waterfall: shape the step's call configuration — model switching, * sampling overrides — by returning a replacement {@link LlmCallConfig} @@ -389,9 +475,14 @@ declare module 'cordis' { * @param turn - the open turn number. * @param step - the step whose request this is. * @param config - the config the loop would use (frozen); return a replacement to switch. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered + * through `agent.ctx` fires only for that agent's dispatches; a listener on a + * plain plugin context fires for every agent. The dispatch `this` is the + * scope carrier (`Scoped`), built by the emitting side via + * `scopeTarget`/`agentEvents`. * @mode waterfall */ - 'agent/request'(agent: Agent, turn: number, step: number, config: LlmCallConfig, next: () => Promise): Promise + 'agent/request'(this: Scoped, agent: Agent, turn: number, step: number, config: LlmCallConfig, next: () => Promise): Promise /** * Waterfall: compose the SESSION PREFIX — request-only messages placed in * front of the ENTIRE derived history (directly after the provider's @@ -433,12 +524,17 @@ declare module 'cordis' { * every later-registered plugin's — reverse registration order when all * contributors append. Call `next()` to * delegate, or return a list without it to short-circuit. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered + * through `agent.ctx` fires only for that agent's dispatches; a listener on a + * plain plugin context fires for every agent. The dispatch `this` is the + * scope carrier (`Scoped`), built by the emitting side via + * `scopeTarget`/`agentEvents`. * @param agent - the agent whose session prefix is being composed. * @param prefix - the frozen empty seed; return an extended replacement to contribute. * @param signal - aborts in-flight listener work (e.g. a discovery scan) when the step is torn down. * @mode waterfall */ - 'agent/session-prefix'(agent: Agent, prefix: Message[], signal: AbortSignal, next: () => Promise): Promise + 'agent/session-prefix'(this: Scoped, agent: Agent, prefix: Message[], signal: AbortSignal, next: () => Promise): Promise /** * Waterfall: post-process the assembled assistant {@link Message} before * tool dispatch (validation, content rewriting, …). @@ -446,9 +542,14 @@ declare module 'cordis' { * @param turn - the open turn number. * @param step - the step that produced the message. * @param message - the assistant message as assembled from the stream. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered + * through `agent.ctx` fires only for that agent's dispatches; a listener on a + * plain plugin context fires for every agent. The dispatch `this` is the + * scope carrier (`Scoped`), built by the emitting side via + * `scopeTarget`/`agentEvents`. * @mode waterfall */ - 'agent/step-result'(agent: Agent, turn: number, step: number, message: Message, next: () => Promise): Promise + 'agent/step-result'(this: Scoped, agent: Agent, turn: number, step: number, message: Message, next: () => Promise): Promise /** * Waterfall: override the turn-continuation decision via a typed * {@link ContinuationDecision}. The loop's `defaultDecision` is `continue` @@ -459,9 +560,32 @@ declare module 'cordis' { * @param agent - the agent deciding whether to run another step. * @param turn - the turn being continued or stopped. * @param defaultDecision - what the loop would do absent an override. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered + * through `agent.ctx` fires only for that agent's dispatches; a listener on a + * plain plugin context fires for every agent. The dispatch `this` is the + * scope carrier (`Scoped`), built by the emitting side via + * `scopeTarget`/`agentEvents`. * @mode waterfall */ - 'agent/turn-continuation'(agent: Agent, turn: number, defaultDecision: ContinuationDecision, next: () => Promise): Promise + 'agent/turn-continuation'(this: Scoped, agent: Agent, turn: number, defaultDecision: ContinuationDecision, next: () => Promise): Promise + /** + * Serial terminal-stop checkpoint after the ordinary + * `agent/turn-continuation` waterfall, any `continue.reason`, and the + * pending-steering continuation override have been folded. A listener + * returns `{ action: 'stop' }` to make this turn terminal, or `undefined` + * to abstain. Terminal stop is monotonic: listener order and steering + * cannot resume the turn, and pending steering is discarded rather than + * becoming another step or turn. + * @param agent - the agent whose composed continuation outcome may be stopped. + * @param turn - the turn at its terminal-stop checkpoint. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered + * through `agent.ctx` fires only for that agent's dispatches; a listener on a + * plain plugin context fires for every agent. The dispatch `this` is the + * scope carrier (`Scoped`), built by the emitting side via + * `scopeTarget`/`agentEvents`. + * @mode serial + */ + 'agent/turn-stop'(this: Scoped, agent: Agent, turn: number): ContinuationStop | undefined // ---- error notifications (emit) ---- /** @@ -471,8 +595,13 @@ declare module 'cordis' { * @param turn - the turn in which the failure surfaced. * @param step - the step at which the failure surfaced. * @param error - the failure, verbatim. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): a listener registered + * through `agent.ctx` fires only for that agent's dispatches; a listener on a + * plain plugin context fires for every agent. The dispatch `this` is the + * scope carrier (`Scoped`), built by the emitting side via + * `scopeTarget`/`agentEvents`. * @mode emit */ - 'agent/error'(agent: Agent, turn: number, step: number, error: Error): void + 'agent/error'(this: Scoped, agent: Agent, turn: number, step: number, error: Error): void } } diff --git a/packages/core/agent/tests/agent.spec.ts b/packages/core/agent/tests/agent.spec.ts index c344cd2a6f..5d541d56a8 100644 --- a/packages/core/agent/tests/agent.spec.ts +++ b/packages/core/agent/tests/agent.spec.ts @@ -1,7 +1,9 @@ -import { describe, expect, it } from 'vitest' -import { Context } from 'cordis' +import { describe, expect, expectTypeOf, it } from 'vitest' +import { Context, Service, symbols } from 'cordis' +import type { Events } from 'cordis' import { Session, SessionId } from '@deepseek-ai/dsh-session' -import AgentRegistry, { Agent, AgentId } from '@deepseek-ai/dsh-agent' +import AgentRegistry, { AgentId, agentEvents } from '@deepseek-ai/dsh-agent' +import type { Agent, AgentFactory, ContinuationStop, CreateAgentOptions, ResumeAgentOptions } from '@deepseek-ai/dsh-agent' function stubAgent(rawId: string): Agent { const id = AgentId(rawId) @@ -10,6 +12,7 @@ function stubAgent(rawId: string): Agent { options: {}, session: new Session(SessionId(`${id}-session`)), status: 'idle', + ctx: new Context(), send() {}, steer() {}, inject() {}, @@ -19,120 +22,211 @@ function stubAgent(rawId: string): Agent { } describe('AgentRegistry', () => { - it('registers agents and emits created/disposed events', async () => { + it('keeps terminal stop decisions synchronous', () => { + type TurnStopListener = Events['agent/turn-stop'] + type AsyncTurnStopListener = () => Promise + + expectTypeOf().not.toExtend() + expectTypeOf>().toEqualTypeOf() + }) + + it('registers exact entries, emits lifecycle events, and unregisters on owner disposal', async () => { const ctx = new Context() await ctx.plugin(AgentRegistry) - - const created: string[] = [] - const disposed: string[] = [] - ctx.on('agent/created', agent => void created.push(agent.id)) - ctx.on('agent/disposed', agent => void disposed.push(agent.id)) + const lifecycle: string[] = [] + ctx.on('agent/created', agent => void lifecycle.push(`created:${agent.id}`)) + ctx.on('agent/disposed', agent => void lifecycle.push(`disposed:${agent.id}`)) const agent = stubAgent('a1') const dispose = ctx.agents.register(agent) - expect(created).toEqual(['a1']) - expect(ctx.agents.get(AgentId('a1'))).toBe(agent) + expect(ctx.agents.get(agent.id)).toBe(agent) expect(ctx.agents.list()).toEqual([agent]) + expect(() => ctx.agents.register(stubAgent('a1'))).toThrow(/already registered/) dispose() - expect(disposed).toEqual(['a1']) - expect(ctx.agents.get(AgentId('a1'))).toBeUndefined() + expect(ctx.agents.get(agent.id)).toBeUndefined() + expect(lifecycle).toEqual(['created:a1', 'disposed:a1']) }) - it('rejects duplicate ids and unregisters on fiber dispose (HMR safety)', async () => { + it('rolls an entry back and pairs a partially delivered creation when a listener throws', async () => { const ctx = new Context() await ctx.plugin(AgentRegistry) - ctx.agents.register(stubAgent('main')) - expect(() => ctx.agents.register(stubAgent('main'))).toThrow('already registered') + const lifecycle: string[] = [] + ctx.on('agent/created', agent => void lifecycle.push(`created:${agent.id}`)) + ctx.on('agent/created', () => { throw new Error('creation veto') }) + ctx.on('agent/disposed', agent => void lifecycle.push(`disposed:${agent.id}`)) - const fiber = await ctx.plugin(Object.assign((inner: Context) => { - inner.agents.register(stubAgent('scoped')) - }, { inject: ['agents'] })) - expect(ctx.agents.list().map(a => a.id)).toEqual(['main', 'scoped']) - - await fiber.dispose() - expect(ctx.agents.list().map(a => a.id)).toEqual(['main']) + expect(() => ctx.agents.register(stubAgent('vetoed'))).toThrow('creation veto') + expect(ctx.agents.get(AgentId('vetoed'))).toBeUndefined() + expect(lifecycle).toEqual(['created:vetoed', 'disposed:vetoed']) }) - it('rolls back the agent entry when an agent/created listener throws (P1-1)', async () => { + it('contains asynchronous creation rejection and every disposal-listener failure', async () => { const ctx = new Context() await ctx.plugin(AgentRegistry) + const warnings: string[] = [] + const heard: string[] = [] + ctx.logger.warn = ((message: unknown) => { warnings.push(String(message)) }) as typeof ctx.logger.warn + ctx.on('agent/created', () => Promise.reject(new Error('created async')) as never) + ctx.on('agent/disposed', () => { throw new Error('disposed sync') }) + ctx.on('agent/disposed', () => Promise.reject(new Error('disposed async')) as never) + ctx.on('agent/disposed', agent => void heard.push(agent.id)) - let threw = false + const dispose = ctx.agents.register(stubAgent('contained')) + await Promise.resolve() + dispose() + await Promise.resolve() + + expect(heard).toEqual(['contained']) + expect(warnings).toEqual([ + 'agent "contained": agent/created listener rejected: Error: created async', + 'agent "contained": agent/disposed listener threw: Error: disposed sync', + 'agent "contained": agent/disposed listener rejected: Error: disposed async', + ]) + }) + + it('separates entry from announcement and stale/idempotent detach cannot remove a replacement', async () => { + const ctx = new Context() + await ctx.plugin(AgentRegistry) + const lifecycle: string[] = [] + ctx.on('agent/created', agent => void lifecycle.push(`created:${agent.id}`)) + ctx.on('agent/disposed', agent => void lifecycle.push(`disposed:${agent.id}`)) + + const first = stubAgent('split') + const detachFirst = ctx.agents.enter(first) + expect(lifecycle).toEqual([]) + ctx.agents.announce(first) + expect(() => { ctx.agents.announce(first) }).toThrow(/already announced/) + detachFirst() + detachFirst() + + const replacement = stubAgent('split') + const detachReplacement = ctx.agents.enter(replacement) + detachFirst() + expect(ctx.agents.get(replacement.id)).toBe(replacement) + expect(() => { ctx.agents.announce(first) }).toThrow(/not live/) + detachReplacement() + expect(lifecycle).toEqual(['created:split', 'disposed:split']) + }) + + it('defers detach requested by a creation listener until that dispatch unwinds', async () => { + const ctx = new Context() + await ctx.plugin(AgentRegistry) + const order: string[] = [] + const agent = stubAgent('reentrant') ctx.on('agent/created', () => { - if (!threw) { threw = true; throw new Error('boom created listener') } + order.push(`first:${ctx.agents.get(agent.id) === agent}`) + detach() + order.push(`after-detach:${ctx.agents.get(agent.id) === agent}`) }) + ctx.on('agent/created', () => void order.push(`second:${ctx.agents.get(agent.id) === agent}`)) + ctx.on('agent/disposed', () => void order.push('disposed')) + const detach = ctx.agents.enter(agent) + ctx.agents.announce(agent) + expect(order).toEqual(['first:true', 'after-detach:true', 'second:true', 'disposed']) + expect(ctx.agents.get(agent.id)).toBeUndefined() + }) +}) - // The throwing emit must roll the entry back, not leak it. - expect(() => ctx.agents.register(stubAgent('main'))).toThrow('boom created listener') - expect(ctx.agents.get(AgentId('main'))).toBeUndefined() // rolled back, not leaked +describe('agentEvents()', () => { + it('contains each synchronous throw and returned-promise rejection', async () => { + const ctx = new Context() + const warnings: string[] = [] + const heard: string[] = [] + ctx.logger.warn = ((message: unknown) => { warnings.push(String(message)) }) as typeof ctx.logger.warn + const agent = stubAgent('event') + ctx.on('agent/status', () => { throw new Error('sync listener') }) + ctx.on('agent/status', () => Promise.reject(new Error('async listener')) as never) + ctx.on('agent/status', (_agent, status) => void heard.push(status)) - // A subsequent listener-free register of the SAME id succeeds and is - // tracked exactly once (the duplicate-id check is not wedged). - const dispose = ctx.agents.register(stubAgent('main')) - expect(ctx.agents.list().map(a => a.id)).toEqual(['main']) - dispose() - expect(ctx.agents.get(AgentId('main'))).toBeUndefined() + agentEvents(ctx, agent).emit('agent/status', 'running') + await Promise.resolve() + expect(heard).toEqual(['running']) + expect(warnings).toEqual([ + 'agent event "agent/status" listener threw: Error: sync listener', + 'agent event "agent/status" listener rejected: Error: async listener', + ]) }) }) describe('AgentRegistry factory seam', () => { - /** A stub AgentFactory that records calls and returns a stub agent. */ function stubFactory() { - const calls: { create: unknown[]; resume: unknown[] } = { create: [], resume: [] } - const factory: import('@deepseek-ai/dsh-agent').AgentFactory = { - createAgent(options) { - calls.create.push(options) + const calls: { + create: Array<{ ownerCtx: Context; options: CreateAgentOptions }> + resume: Array<{ ownerCtx: Context; options: ResumeAgentOptions }> + } = { create: [], resume: [] } + const factory: AgentFactory = { + async createAgent(ownerCtx, options) { + calls.create.push({ ownerCtx, options }) return { agent: stubAgent(options.agentId), dispose: () => Promise.resolve() } }, - resume(options) { - calls.resume.push(options) - return Promise.resolve({ agent: stubAgent(options.agentId), dispose: () => Promise.resolve() }) + async resume(ownerCtx, options) { + calls.resume.push({ ownerCtx, options }) + return { agent: stubAgent(options.agentId), dispose: () => Promise.resolve() } }, } return { factory, calls } } - it('create()/resume() throw when no factory is registered', async () => { - const ctx = new Context() - await ctx.plugin(AgentRegistry) - expect(() => ctx.agents.create({ agentId: AgentId('a'), sessionId: SessionId('s') })).toThrow(/no agent factory/) - await expect(ctx.agents.resume({ agentId: AgentId('a'), resumeSessionId: SessionId('s') })).rejects.toThrow(/no agent factory/) - }) - - it('setFactory registers a factory; create/resume delegate to it', async () => { + it('requires a factory and delegates through the calling context', async () => { const ctx = new Context() await ctx.plugin(AgentRegistry) + await expect(ctx.agents.create({ agentId: AgentId('a'), sessionId: SessionId('s') })).rejects.toThrow(/no agent factory/) const { factory, calls } = stubFactory() ctx.agents.setFactory(factory) - const created = ctx.agents.create({ agentId: AgentId('c1'), sessionId: SessionId('sess-1'), meta: { cwd: '/w' } }) - expect(created.agent.id).toBe('c1') - expect(calls.create).toEqual([{ agentId: AgentId('c1'), sessionId: SessionId('sess-1'), meta: { cwd: '/w' } }]) - - const resumed = await ctx.agents.resume({ agentId: AgentId('r1'), resumeSessionId: SessionId('old-sess') }) - expect(resumed.agent.id).toBe('r1') - expect(calls.resume).toEqual([{ agentId: AgentId('r1'), resumeSessionId: SessionId('old-sess') }]) - }) - - it('setFactory rejects a second factory', async () => { - const ctx = new Context() - await ctx.plugin(AgentRegistry) - ctx.agents.setFactory(stubFactory().factory) - expect(() => ctx.agents.setFactory(stubFactory().factory)).toThrow(/already registered/) - }) - - it('disposing the setFactory fiber clears the factory (HMR safety)', async () => { - const ctx = new Context() - await ctx.plugin(AgentRegistry) - let dispose!: () => void - const fiber = await ctx.plugin(Object.assign((inner: Context) => { - dispose = inner.agents.setFactory(stubFactory().factory) + let callerFiber: Context['fiber'] | undefined + await ctx.plugin(Object.assign(async (inner: Context) => { + callerFiber = inner.fiber + await inner.agents.create({ agentId: AgentId('create'), sessionId: SessionId('create-s') }) + await inner.agents.resume({ agentId: AgentId('resume'), resumeSessionId: SessionId('resume-s') }) }, { inject: ['agents'] })) - expect(() => ctx.agents.create({ agentId: AgentId('a'), sessionId: SessionId('s') })).not.toThrow() - void dispose - await fiber.dispose() - // factory slot cleared → create throws again - expect(() => ctx.agents.create({ agentId: AgentId('a2'), sessionId: SessionId('s2') })).toThrow(/no agent factory/) + expect(calls.create[0]?.ownerCtx.fiber).toBe(callerFiber) + expect(calls.resume[0]?.ownerCtx.fiber).toBe(callerFiber) + }) + + it('rejects a second factory and clears the slot with its owner (HMR)', async () => { + const ctx = new Context() + await ctx.plugin(AgentRegistry) + const owner = await ctx.plugin(Object.assign((inner: Context) => { + inner.agents.setFactory(stubFactory().factory) + expect(() => inner.agents.setFactory(stubFactory().factory)).toThrow(/already registered/) + }, { inject: ['agents'] })) + await expect(ctx.agents.create({ agentId: AgentId('before'), sessionId: SessionId('before-s') })).resolves.toBeDefined() + await owner.dispose() + await expect(ctx.agents.create({ agentId: AgentId('after'), sessionId: SessionId('after-s') })).rejects.toThrow(/no agent factory/) + }) + + it('canonicalizes an already traced Service before tracing it for the caller', async () => { + const ctx = new Context() + await ctx.plugin(AgentRegistry) + const states = new WeakMap() + class TracedFactory extends Service implements AgentFactory { + constructor(inner: Context) { + super(inner, 'tracedFactory') + states.set(this, []) + } + private calls(): string[] { + const original = (this as unknown as { [symbols.original]?: TracedFactory })[symbols.original] ?? this + const calls = states.get(original) + if (calls === undefined) throw new Error('factory receiver was not canonicalized') + return calls + } + async createAgent(_ownerCtx: Context, options: CreateAgentOptions) { + this.calls().push('create') + return { agent: stubAgent(options.agentId), dispose: () => Promise.resolve() } + } + async resume(_ownerCtx: Context, options: ResumeAgentOptions) { + this.calls().push('resume') + return { agent: stubAgent(options.agentId), dispose: () => Promise.resolve() } + } + } + await ctx.plugin(TracedFactory) + const traced = (ctx as Context & { tracedFactory: TracedFactory }).tracedFactory + ctx.agents.setFactory(traced) + await ctx.agents.create({ agentId: AgentId('create'), sessionId: SessionId('create-s') }) + await ctx.agents.resume({ agentId: AgentId('resume'), resumeSessionId: SessionId('resume-s') }) + const raw = (traced as unknown as { [symbols.original]?: TracedFactory })[symbols.original] + expect(states.get(raw!)).toEqual(['create', 'resume']) }) }) diff --git a/packages/core/agent/tsconfig.json b/packages/core/agent/tsconfig.json index 7f4f457598..2692e1b7f7 100644 --- a/packages/core/agent/tsconfig.json +++ b/packages/core/agent/tsconfig.json @@ -17,6 +17,9 @@ { "path": "../../util/brand" }, + { + "path": "../../core/scope" + }, { "path": "../../llm/llm" }, diff --git a/packages/core/scope/README.md b/packages/core/scope/README.md new file mode 100644 index 0000000000..55a0591d0e --- /dev/null +++ b/packages/core/scope/README.md @@ -0,0 +1,20 @@ +# dsh-scope + +Scoped-context registration primitive. `createScope(ctx, key)` mints a Cordis context that TAGS everything registered through it with an opaque `ScopeKey` and OWNS those registrations' lifetime (one backing fiber drives both facts); `scopeOf(ctx)` reads the tag; `scopeTarget(base, key)` builds the dispatch carrier that makes an event scope-filtered — listeners registered through a scoped context fire only for their key's subject, while plain plugin listeners keep firing for every subject. The agent loop is the one scope minter today (one scope per live agent, key = the `Agent` object — the `Agent.ctx` contract in `dsh-agent`), but the mechanism is key-agnostic so packages below the agent layer (`dsh-session`, `dsh-system-prompt`) depend on it without a dependency cycle. + +## Public API + +- `createScope(ctx: Context, key: ScopeKey): Scope` Mint a scope under `ctx`'s fiber. Usable synchronously (effect collection is uid-gated; service resolution falls through to the minting plugin's dependency surface). The typed, same-process key is trusted; an inactive minting context still fails through Cordis (`INACTIVE_EFFECT`). +- `Scope.ctx` The tagged context: registrations through it are scope-visible AND scope-lifetime. Derived contexts (an `extend`, a fiber mounted under it) inherit the tag; nested scopes shadow (nearest tag wins). +- `Scope.rawDispose` The EXACT Cordis disposer for the backing fiber — a composite (generator) effect yields THIS function to nest the scope's teardown at that yield position (Cordis dedupes nested effects by function identity; yielding a wrapper leaves the scope disposing as a concurrent sibling). +- `Scope.dispose(): Promise` Idempotent, shared quiescence boundary for every registration made through the scope. Racing/repeat calls await the same teardown, including when `rawDispose` invoked the underlying single-shot Cordis disposer first. +- `scopeOf(ctx: Context): ScopeKey | undefined` The tag a context (or any context derived from it) carries; `undefined` = context-global. +- `scopeTarget(base: T, key: ScopeKey | undefined): Scoped` Build the opaque dispatch `thisArg` for a scope-filtered event. It composes `base`'s existing `Context.filter` with the scope predicate (untagged listener ⇒ admitted; tagged ⇒ admitted iff tag === key; `key === undefined` ⇒ untagged only). The carrier contains routing state only; the real subject is carried by the event arguments. `{ global: true }` listeners bypass filtering (Cordis semantics). +- `Scoped` The compile-time opaque carrier brand: scope-filtered events demand it as their `this` type, so dispatching with a bare subject is a compile error. The type parameter records the subject type but does not expose its properties. +- `isScopeCarrier(value)` / `carrierKeyOf(value)` Runtime carrier marks, used by the dev invariants to assert every scope-filtered dispatch carries a carrier keyed to the subject its arguments name. + +## Design contract + +Ownership and visibility derive from ONE fact — which context a registration went through. An explicit `{ scope }` registration parameter could express "visible to X, disposed with Y", which is almost always a bug; the scoped context makes it unrepresentable. This is trusted registration and listener routing, not sandboxing or an authority hierarchy: a same-process plugin is not confined, and a child scope need not be a subset of its parent's view. Rationale, alternatives, and the security non-goal: [the agent-scope RFC](../../../docs/rfc/implemented/architecture/2026-07-08-agent-scope-contexts.md#security-and-authority-are-explicit-non-goals). + +Handing out a scoped context hands out the minting plugin's service-resolution surface (resolution walks the minting fiber's dependency chain, not the holder's) — mint it from the plugin whose dependencies the scoped registrations need to resolve. diff --git a/packages/core/scope/package.json b/packages/core/scope/package.json new file mode 100644 index 0000000000..89c2b4428b --- /dev/null +++ b/packages/core/scope/package.json @@ -0,0 +1,30 @@ +{ + "name": "@deepseek-ai/dsh-scope", + "description": "Scoped-context registration primitive (scope tags, scope-filtered event dispatch) for the DeepSeek Harness", + "version": "0.0.1", + "private": true, + "type": "module", + "main": "lib/index.js", + "types": "lib/types/index.d.ts", + "exports": { + ".": { + "types": "./lib/types/index.d.ts", + "default": "./lib/index.js" + }, + "./src/*": "./src/*", + "./package.json": "./package.json" + }, + "files": [ + "lib/index.js", + "lib/types/**/*.d.ts", + "lib/types/**/*.d.ts.map", + "src" + ], + "license": "BSD-3-Clause", + "peerDependencies": { + "cordis": "^4.0.0-rc.6" + }, + "devDependencies": { + "cordis": "^4.0.0-rc.6" + } +} diff --git a/packages/core/scope/src/index.ts b/packages/core/scope/src/index.ts new file mode 100644 index 0000000000..e229243504 --- /dev/null +++ b/packages/core/scope/src/index.ts @@ -0,0 +1,116 @@ +/** + * Scoped-context primitive: mint a Cordis context that tags registrations with + * an opaque identity and build routing-only event carriers for that identity. + * + * @module @deepseek-ai/dsh-scope + */ + +import type { Context, Fiber } from 'cordis' +import { Context as CordisContext } from 'cordis' + +/** An opaque, identity-compared scope key. */ +export type ScopeKey = object + +/** Context tag written by {@link createScope}. */ +const kScope = Symbol('dsh.scope') + +declare const ScopedBrand: unique symbol + +/** + * A routing-only event receiver built by {@link scopeTarget}. The type + * parameter records the subject type for dispatch checking; the carrier does + * not expose the subject's properties. Event payloads carry the real subject. + */ +export type Scoped = object & { readonly [ScopedBrand]: T } + +/** The key associated with each carrier. Presence distinguishes an unkeyed carrier from a non-carrier. */ +const carrierKeys = new WeakMap() + +/** A minted registration scope and its quiescent disposal boundaries. */ +export interface Scope { + /** Context through which scope-owned registrations are made. */ + ctx: Context + /** Exact Cordis disposer, used when nesting this scope in an ordered composite effect. */ + rawDispose: () => Promise | void + /** Dispose every scope-owned registration; racing calls await the same completion. */ + dispose(): Promise +} + +/** Follow a Cordis fiber through asynchronous teardown even if its raw disposer was already claimed. */ +async function quiesceFiber(fiber: Fiber): Promise { + await Promise.resolve(fiber.dispose()) + while (fiber.inertia !== undefined) await fiber.inertia +} + +/** Shared no-op plugin used as the backing scope fiber. */ +function scope(): void {} + +/** + * Mint a scope under `ctx`. The scoped context inherits the minting plugin's + * dependency surface and owns every registration made through it. + * @param ctx - active context whose dependency surface the scope inherits. + * @param key - opaque identity used for listener routing. + * @returns the scoped context and exact/shared disposal boundaries. + */ +export function createScope(ctx: Context, key: ScopeKey): Scope { + const fiber = ctx.plugin(scope) + const scoped: Context = fiber.ctx.extend({ [kScope]: key }) + let disposing: Promise | undefined + return { + ctx: scoped, + rawDispose: fiber.dispose, + dispose: () => (disposing ??= quiesceFiber(fiber)), + } +} + +/** + * Read the nearest scope tag inherited by a context. + * @param ctx - context to inspect. + * @returns its scope key, or `undefined` for an unscoped context. + */ +export function scopeOf(ctx: Context): ScopeKey | undefined { + return (ctx as Context & { [kScope]?: ScopeKey })[kScope] +} + +/** + * Build the routing receiver for a scope-filtered event. Untagged listeners + * remain global; tagged listeners run only when their key matches. A base + * Cordis filter is composed before the scope predicate. + * + * The receiver is deliberately opaque: listener code obtains the real subject + * from event arguments, never from `this`. + * @param base - subject or service whose existing Cordis filter is preserved. + * @param key - routed scope identity, or `undefined` for an unscoped subject. + * @returns an opaque dispatch carrier. + */ +export function scopeTarget(base: T, key: ScopeKey | undefined): Scoped { + const baseFilter = (base as { [CordisContext.filter]?: (ctx: Context) => boolean })[CordisContext.filter] + const carrier = { + [CordisContext.filter](ctx: Context): boolean { + if (baseFilter !== undefined && !baseFilter.call(base, ctx)) return false + const tag = scopeOf(ctx) + return tag === undefined || tag === key + }, + } + carrierKeys.set(carrier, key) + return carrier as unknown as Scoped +} + +/** + * Test whether a value is a scope carrier. + * @param value - dispatch receiver to inspect. + * @returns whether {@link scopeTarget} created it. + */ +export function isScopeCarrier(value: unknown): value is Scoped { + return typeof value === 'object' && value !== null && carrierKeys.has(value) +} + +/** + * Read a carrier's routing key. + * @param value - dispatch receiver to inspect. + * @returns the carrier key, or `undefined` for an unkeyed/non-carrier value. + */ +export function carrierKeyOf(value: unknown): ScopeKey | undefined { + if (!isScopeCarrier(value)) return undefined + return carrierKeys.get(value) +} diff --git a/packages/core/scope/tests/scope.spec.ts b/packages/core/scope/tests/scope.spec.ts new file mode 100644 index 0000000000..0b7bbef348 --- /dev/null +++ b/packages/core/scope/tests/scope.spec.ts @@ -0,0 +1,155 @@ +import { describe, expect, expectTypeOf, it } from 'vitest' +import { Context } from 'cordis' +import { carrierKeyOf, createScope, isScopeCarrier, scopeOf, scopeTarget } from '@deepseek-ai/dsh-scope' +import type { Scope, Scoped } from '@deepseek-ai/dsh-scope' + +declare module 'cordis' { + interface Events { + /** + * Test-only event for scope-filtered dispatch. + * @param value - opaque payload recorded by listeners. + * @mode emit + */ + 'scope-test/ping'(value: string): void + } +} + +/** Mount a host plugin and mint a scope inside it. */ +async function mintScope(ctx: Context, key: object): Promise { + let scope!: Scope + await ctx.plugin((inner: Context) => { scope = createScope(inner, key) }) + return scope +} + +describe('createScope', () => { + it('tags contexts and derived contexts, with the nearest tag winning', async () => { + const ctx = new Context() + const outerKey = { name: 'outer' } + const innerKey = { name: 'inner' } + const outer = await mintScope(ctx, outerKey) + const inner = createScope(outer.ctx, innerKey) + + expect(scopeOf(ctx)).toBeUndefined() + expect(scopeOf(outer.ctx)).toBe(outerKey) + expect(scopeOf(outer.ctx.extend({}))).toBe(outerKey) + expect(scopeOf(inner.ctx)).toBe(innerKey) + + await inner.dispose() + await outer.dispose() + }) + + it('is usable synchronously before the backing fiber activates', async () => { + const ctx = new Context() + const events: string[] = [] + let scope!: Scope + await ctx.plugin((inner: Context) => { + scope = createScope(inner, { name: 'sync' }) + scope.ctx.effect(() => () => void events.push('disposed')) + events.push('registered') + }) + expect(events).toEqual(['registered']) + await scope.dispose() + expect(events).toEqual(['registered', 'disposed']) + }) + + it('shares quiescence across repeat and raw-disposer-first calls', async () => { + const ctx = new Context() + const scope = await mintScope(ctx, { name: 'quiescence' }) + const gate = Promise.withResolvers() + let finished = false + scope.ctx.effect(() => async () => { + await gate.promise + finished = true + }) + + const raw = Promise.resolve(scope.rawDispose()) + const publicDispose = scope.dispose() + await Promise.resolve() + expect(finished).toBe(false) + gate.resolve(undefined) + await Promise.all([raw, publicDispose, scope.dispose()]) + expect(finished).toBe(true) + }) + + it('exposes the exact raw disposer for ordered composite teardown', async () => { + const ctx = new Context() + const order: string[] = [] + let dispose!: () => Promise | void + await ctx.plugin((inner: Context) => { + dispose = inner.effect(function* () { + yield () => void order.push('outer') + const scope = createScope(inner, { name: 'nested' }) + scope.ctx.effect(() => () => void order.push('scope')) + yield scope.rawDispose + yield () => void order.push('inner') + }) + }) + await dispose() + expect(order).toEqual(['inner', 'scope', 'outer']) + }) +}) + +describe('scopeTarget', () => { + it('routes scoped listeners by key while untagged listeners remain global', async () => { + const ctx = new Context() + const keyA = { name: 'A' } + const keyB = { name: 'B' } + const scopeA = await mintScope(ctx, keyA) + const scopeB = await mintScope(ctx, keyB) + const heard: string[] = [] + ctx.on('scope-test/ping', value => void heard.push(`global:${value}`)) + scopeA.ctx.on('scope-test/ping', value => void heard.push(`A:${value}`)) + scopeB.ctx.on('scope-test/ping', value => void heard.push(`B:${value}`)) + + ctx.emit(scopeTarget(ctx, keyA), 'scope-test/ping', 'a') + ctx.emit(scopeTarget(ctx, keyB), 'scope-test/ping', 'b') + ctx.emit(scopeTarget(ctx, undefined), 'scope-test/ping', 'none') + + expect(heard).toEqual(['global:a', 'A:a', 'global:b', 'B:b', 'global:none']) + await Promise.all([scopeA.dispose(), scopeB.dispose()]) + }) + + it('preserves a base Cordis filter and its receiver', async () => { + const ctx = new Context() + const key = { name: 'A' } + const scope = await mintScope(ctx, key) + const heard: string[] = [] + ctx.on('scope-test/ping', value => void heard.push(`global:${value}`)) + scope.ctx.on('scope-test/ping', value => void heard.push(`A:${value}`)) + let receiverMatches = false + const base = { + [Context.filter](this: object): boolean { + receiverMatches = this === base + return false + }, + } + + ctx.emit(scopeTarget(base, key), 'scope-test/ping', 'vetoed') + expect(heard).toEqual([]) + expect(receiverMatches).toBe(true) + await scope.dispose() + }) + + it('{ global: true } listeners retain Cordis global-listener semantics', async () => { + const ctx = new Context() + const scope = await mintScope(ctx, { name: 'A' }) + const heard: string[] = [] + scope.ctx.on('scope-test/ping', value => void heard.push(value), { global: true }) + ctx.emit(scopeTarget(ctx, { name: 'other' }), 'scope-test/ping', 'foreign') + ctx.emit(scopeTarget(ctx, undefined), 'scope-test/ping', 'none') + expect(heard).toEqual(['foreign', 'none']) + await scope.dispose() + }) + + it('uses an opaque branded carrier with a separately tracked key', () => { + const key = { name: 'key' } + const subject = { value: 1 } + const carrier = scopeTarget(subject, key) + expect(isScopeCarrier(carrier)).toBe(true) + expect(carrierKeyOf(carrier)).toBe(key) + expect(isScopeCarrier(subject)).toBe(false) + expect(carrierKeyOf(subject)).toBeUndefined() + expect('value' in carrier).toBe(false) + expectTypeOf(carrier).toEqualTypeOf>() + }) +}) diff --git a/packages/core/scope/tsconfig.json b/packages/core/scope/tsconfig.json new file mode 100644 index 0000000000..754725418e --- /dev/null +++ b/packages/core/scope/tsconfig.json @@ -0,0 +1,18 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "lib/types" + }, + "include": [ + "src" + ], + "references": [ + { + "path": "../../../vendor/cosmokit" + }, + { + "path": "../../../vendor/cordis" + } + ] +} diff --git a/packages/core/session/README.md b/packages/core/session/README.md index 1cc393e172..97b18b0504 100644 --- a/packages/core/session/README.md +++ b/packages/core/session/README.md @@ -4,43 +4,45 @@ Event-sourced session log and in-memory store. A `Session` is the append-only so ## Service: `SessionStore` (ctx key: `sessions`) -Creates and holds event-sourced `Session` instances. Persistence is intentionally not implemented here — plugins subscribe to `session/event` and flush on `session/flush`. +Creates and holds event-sourced `Session` instances. Persistence is intentionally not implemented here — plugins subscribe to `session/event`, flush on `session/flush`, and may mirror the paired `session/created`/`session/disposed` lifecycle. ### Public API -- `ctx.sessions.create(id?: SessionId, options?: { seed?: SessionEvent[]; meta?: { cwd?: string; parentSession?: SessionId; createdAt?: number; seedLength?: number } }): Session` — Create a session. `options.seed` replays/forks an existing event log; `options.meta` attaches creation metadata (validated absolute `cwd`, `parentSession` lineage, seed boundary) as the immutable `SessionHeader`. The store fills `version`/`id` and defaults `createdAt` to now; a caller reconstructing a persisted session passes the original `createdAt` and persisted `seedLength` to preserve them. Disposed with the calling fiber. +- `ctx.sessions.create(id?: SessionId, options?: { seed?: readonly SessionEvent[]; meta?: { cwd?: string; parentSession?: SessionId; createdAt?: number; seedLength?: number } }): Session` — Create a session. The persistence/replay seed and resulting header are validated, detached, and deep-frozen at this durable boundary. The store fills `version`/`id` and defaults `createdAt` to now; a persisted reconstruction supplies the original `createdAt` and `seedLength`. Disposed with the calling fiber. +- `ctx.sessions.flush(session: Session): Promise` Dispatch the awaited `session/flush` durability checkpoint with the carrier captured at enter — THE flush entry point (the loop's turn-end checkpoint and idle injection call it; never dispatch a raw `ctx.parallel`). Every captured listener starts, the call waits for all of them to settle, and a failure rejects only after the other listeners finish. Rejects a prepared, detached, or stale same-id object instead of inventing a subject-less carrier. - `ctx.sessions.fork(source, boundary?, childSessionId?): Session` — Resolve a live session object or id, select a seed through the inclusive `boundary` event seq (default: current last event), require that boundary to be `turn/end`, and create a live child session with lineage metadata. - `ctx.sessions.get(id: SessionId): Session | undefined` - `ctx.sessions.list(): Session[]` #### Advanced: ordered-teardown lifecycle primitives -`create()` covers the common case (the session is owned by the calling fiber). When a session must be torn down **in order with another resource** — so a final flush is captured before `onAppend` detaches — `create()`'s self-contained effect is wrong, because a fiber unload disposes sibling effects *concurrently*. For that, split the lifecycle and fold it into the owner's single effect: +`create()` covers the common case (the session is owned by the calling fiber). When a session must be torn down **in order with another resource** — so a final flush is captured before the store attachment and publication hooks are removed — `create()`'s self-contained effect is wrong, because a fiber unload disposes sibling effects *concurrently*. For that, split the lifecycle and fold it into the owner's single effect: -- `ctx.sessions.prepare(id?, options?): Session` — validate the id/cwd and construct the `Session`, WITHOUT entering it into the store. Same options as `create`. -- `ctx.sessions.enter(session): () => void` — wire `onAppend` → `session/event` and add the session to the store; returns the DETACH disposer. Does NOT emit `session/created` (the caller yields the disposer first, then calls `announce`, so a throwing listener rolls the attach back). The id was already validated by `prepare`, which runs in the same synchronous sequence, so `enter` does not re-check. -- `ctx.sessions.announce(session): void` — emit `session/created` for an entered session. +- `ctx.sessions.prepare(id?, options?): Session` — validate durable seed/header data and construct the `Session` WITHOUT entering it into the store. Same options as `create`. +- `ctx.sessions.enter(session): () => void` — perform the authoritative ID collision check, install append publication state, and insert the exact session without announcing it. Returns an idempotent detach bound to the captured entry object, so a stale disposer cannot remove a later same-ID replacement. Concurrent same-ID preparation is allowed; only one final entry succeeds. +- `ctx.sessions.announce(session): void` — begin the one allowed `session/created` announcement for an entered session; repeat and reentrant calls reject before dispatch. A detach requested synchronously by a creation listener is deferred until that dispatch unwinds, so another creation listener cannot observe `session/disposed` before its own `session/created` callback. Detach emits `session/disposed` exactly once, including rollback after a partially delivered creation notification; a never-announced entry emits neither edge. -`dsh-agent-loop`'s `AgentLoop.start` is the canonical consumer: it yields `enter`'s detach disposer, the registry unregister, and the loop-stop disposer into ONE composite effect, so teardown stops + awaits the loop (final flush captured) BEFORE detaching the session — whether the trigger is the `AgentHandle`'s `dispose()` or a fiber unload. +`dsh-agent-loop` is the canonical consumer: after unpublished agent setup it enters both session and agent before announcing either, then nests loop stop, agent removal, session detach, and scope unwind in one ordered lifecycle. The final flush therefore settles before this package detaches the session, whether teardown starts from an `AgentHandle` or owner-fiber unload. -### Events +### Live service events -| Event | Mode | Purpose | -|---|---|---| -| `session/created` | emit | A session was created | -| `session/event` | emit | An event was appended (sync, fire-and-forget) | -| `session/flush` | parallel | Awaited durability checkpoint (persistence plugins drain buffers here) | +The store pairs announced creation with disposal, publishes each append, and provides an awaited durability checkpoint. Before the log push it resolves the scoped `session/event` callback list. The push is the commit point; callback throws or returned-promise rejections are logged and contained per observer. A committed append therefore returns normally, later observers still run, and detach waits until publication unwinds. Exact `session/*` signatures, modes, and scope-carrier behavior live in the generated [Cordis event catalog](../../../docs/cordis-catalog/events.md); the append-only payload vocabulary is separately generated into the [persistence catalog](../../../docs/persistence-catalog.md). ### Class: `Session` Plain class (not a Cordis Service). Create via `ctx.sessions.create()`. -- `session.append(type, data, opts?): SessionEvent` — synchronous, never blocks on I/O. **Throws** if `data` is not losslessly JSON-serializable (BigInt, function, symbol, undefined, non-finite number, circular ref, or an exotic object like Map/Set/Date) — the event log is the durable source of truth, so this invariant is enforced at the source (exported as `isJsonValue` for backends to reuse on their replay/fork entry points). A third parameter `opts: SurfaceIntent` carries surface metadata: `surfaceOp` controls how the event enters the surface linked list, and `sourceEventSeqs` records provenance (the seq numbers of events this one derives from). It is **required** for the five `SurfaceEventType` events (every message-producing event must declare how it joins the surface) and rejected by the compiler for non-surface types. The marker requirement is enforced two ways: the typed overload makes `opts` mandatory when `type` is a specific `SurfaceEventType` literal, AND `append` **throws** at runtime if a surface-eligible event arrives with no `surfaceOp` — covering the case where `type` widens to the `SessionEventType` union (a caller iterating raw events, where the conditional overload collapses to optional) so a marker-less message event can never silently land in the log and vanish from `deriveMessages()`. -- `session.deriveMessages(): Message[]` — the LLM message history, CACHED: each surface node is projected exactly once, when first seen (O(new nodes) per call; a surface rewrite rebuilds via `surface.replaceGeneration`). Returns a fresh array snapshot per call over SHARED, deep-frozen `Message` objects — cloned once off the log at projection time, so a consumer can never mutate logged data (mutation throws). The surface is the single source of derived history — there is no raw-log fallback. -- `session.deriveEventMessage(event): Message | null` — the per-event projection `deriveMessages()` folds: one event's derived message (an unfrozen clone), or `null` when it produces none (a non-surface event, or an empty-content `assistant/message` hosting only usage). External reconstructors and the dev invariant fold the same function over a log prefix's surface, so no two paths can disagree about what a request's messages were (the reconstructability RFC). +- `session.append(type, data, opts?): SessionEvent` — synchronous, never blocks on I/O. At this durable boundary, data and surface metadata are lossless-JSON snapshotted and deep-frozen. For an attached session, a reentrant append during dispatch/observer publication rejects, and detach waits for that publication to unwind. Callbacks resolve before the log push; the push is the commit point, after which each observer failure is contained independently. Runtime surface validation covers widened unions and raw seed/load logs. +- `session.deriveMessages(): Message[]` — the LLM message history, CACHED: each surface node is projected exactly once, when first seen (O(new nodes) per call; a surface rewrite rebuilds via `surface.replaceGeneration`). Returns a fresh array per call over shared, deep-frozen `Message` objects. Each projection reuses the already deep-frozen content in its durable log event, so no second deep clone is needed and a consumer still cannot mutate logged data. The surface is the single source of derived history — there is no raw-log fallback. +- `session.deriveEventMessage(event): Message | null` — the per-event projection `deriveMessages()` folds: a fresh message wrapper that reuses the event's already frozen content, or `null` when the event produces none (a non-surface event, or an empty-content `assistant/message` hosting only usage). External reconstructors and the dev invariant fold the same function over a log prefix's surface, so no two paths can disagree about what a request's messages were (the reconstructability RFC). - `session.surface: SurfaceManager` — the derived surface, lazily rebuilt from `surfaceOp` markers in the log. Processes only new events (delta) on each access — the log is append-only, so prior events never change. `surface.replaceGeneration` is the rewrite signal: bumped by every folded `replace` and by `invalidate()`, never reset, so an incremental consumer comparing generations cannot be fooled. -- `session.events`, `session.seq`, `session.id` -- `session.header: SessionHeader` — immutable creation metadata (`version`, `id`, `createdAt`, optional `cwd`/`parentSession`/`seedLength`). Kept out of the event log (a storage concern, not replayable state); a minimal header (stamped with the current `SESSION_FORMAT_VERSION`) is synthesized for bare `Session` construction. +- `session.events` — a cached, frozen array snapshot over deep-frozen events. Repeated reads without an append return the same array; an append invalidates the cache and the next read returns a new snapshot, while earlier snapshots stay unchanged. Neither a cast nor a retained reference can push into the live log or rewrite an accepted event. +- `session.seq`, `session.id` — current sequence and readonly typed identity. +- `session.header: SessionHeader` — detached, deep-frozen creation metadata (`version`, `id`, `createdAt`, optional `cwd`/`parentSession`/`seedLength`). Construction validates the durable record and requires its id to match `session.id`. + +### Lossless JSON utilities + +Durable values need one accepted representation, not a check followed by a second read. `isJsonValue(value)` is the boolean predicate; `snapshotJsonValue(value)` recursively validates and copies a plain value in one pass, returning `undefined` for invalid input and propagating a throwing getter. The snapshot helper accepts finite JSON numbers except `-0` (JSON rewrites it to `0`), dense ordinary arrays, and plain or null-prototype objects; it rejects cycles, unsupported scalars, and exotic prototypes before normalization. ### Surface types @@ -68,12 +70,12 @@ Every `SessionEvent` carries two optional top-level fields (structural metadata) ### Metadata types (`types.ts`) -- `SessionHeader` — immutable session metadata, written once: `{ version, id, createdAt, cwd?, parentSession?, seedLength? }`. Owned here (beside `SessionId`) because `Session.header` is typed by it; persistence backends re-export it rather than own it (which would force a package cycle). +- `SessionHeader` — session metadata written once when published as `Session.header`, where detachment and deep-freezing enforce immutability at runtime: `{ version, id, createdAt, cwd?, parentSession?, seedLength? }`. Persistence loaders may return mutable detached copies of the same data type. Owned here (beside `SessionId`) because `Session.header` is typed by it; persistence backends re-export it rather than own it (which would force a package cycle). ### Extension points - Persistence plugins: subscribe to `session/event` (write-behind) and drain on `session/flush` (awaited) and fiber dispose. A durable backend reads the log and reloads it into a live session; the metadata seam (`SessionHeader`, `session.header`) is what such a backend stores beside the log. -- Replay/fork: `ctx.sessions.create(id, { seed })` seeds a new session with an existing event log. The surface rebuilds deterministically from `surfaceOp` markers in the seeded events. The seed is validated to the SAME always-on invariants `append` enforces — contiguous seqs, JSON-serializable data, and required `surfaceOp` markers on surface-eligible events — so marker-less message events are rejected at construction rather than silently vanishing from `deriveMessages()`. Broader turn-enclosure checks stay in `dsh-invariants` and persistence repair. Ordinary live-session forks use `ctx.sessions.fork(source, boundary?, childSessionId?)`, where `boundary` is the inclusive source event seq to fork through. +- Replay/fork: `ctx.sessions.create(id, { seed })` seeds a new session with an existing event log. The surface rebuilds deterministically from `surfaceOp` markers in the seeded events. The constructor reads each seed entry once and uses the same one-pass lossless-JSON snapshot and exact surface-metadata shape checks as `append`, then enforces contiguous seqs and deep-freezes every accepted record; a stateful caller, exotic nested value, marker-less or malformed surface event, metadata on a non-surface event, or retained seed reference therefore cannot silently change the reconstructed history. Broader turn-enclosure checks stay in `dsh-invariants` and persistence repair. Ordinary live-session forks use `ctx.sessions.fork(source, boundary?, childSessionId?)`, where `boundary` is the inclusive source event seq to fork through. - Compaction: the `dsh-compact-basic` plugin appends a `user/message` with `surfaceOp: { op: 'replace', start, end }` to shadow old surface nodes behind a summary checkpoint. ### What is NOT here (TODO) diff --git a/packages/core/session/package.json b/packages/core/session/package.json index 8c6645e37e..454a0d7cc3 100644 --- a/packages/core/session/package.json +++ b/packages/core/session/package.json @@ -24,11 +24,13 @@ "peerDependencies": { "@deepseek-ai/dsh-brand": "^0.0.1", "@deepseek-ai/dsh-llm": "^0.0.1", + "@deepseek-ai/dsh-scope": "^0.0.1", "cordis": "^4.0.0-rc.6" }, "devDependencies": { "@deepseek-ai/dsh-brand": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", + "@deepseek-ai/dsh-scope": "workspace:^", "cordis": "^4.0.0-rc.6" } } diff --git a/packages/core/session/src/index.ts b/packages/core/session/src/index.ts index cd9e1828b2..a6dff5cd27 100644 --- a/packages/core/session/src/index.ts +++ b/packages/core/session/src/index.ts @@ -9,15 +9,17 @@ import { Context, Service } from 'cordis' import { isAbsolute } from 'node:path' import { deepFreeze } from '@deepseek-ai/dsh-llm' +import { scopeOf, scopeTarget } from '@deepseek-ai/dsh-scope' +import type { Scoped } from '@deepseek-ai/dsh-scope' import type { ContentBlock, Message, MessageSource } from '@deepseek-ai/dsh-llm' import { SESSION_FORMAT_VERSION, SessionId } from './types.ts' import type { CreateSessionOptions, EpochHeader, SessionEvent, SessionEventMap, SessionEventType, SessionHeader, SurfaceIntent, SurfaceEventType } from './types.ts' -import { isJsonValue } from './json.ts' +import { snapshotJsonValue } from './json.ts' import { SurfaceManager, isSurfaceEligibleType } from './surface.ts' import { foldRequestHeader } from './request-header.ts' export * from './types.ts' -export { isJsonValue } from './json.ts' +export { isJsonValue, snapshotJsonValue } from './json.ts' export type { JsonValue } from './json.ts' export { interruptedTurnClosers } from './repair.ts' export type { SurfaceNode } from './surface.ts' @@ -32,29 +34,71 @@ declare module 'cordis' { interface Events { /** - * A session was created in the store. + * A session was created in the store. A synchronous listener throw vetoes + * publication and rollback emits the matching `session/disposed` edge; + * returned-promise rejection is observed and logged but cannot retroactively + * veto this synchronous boundary. A synchronous listener that requests the + * advanced detach does not remove the entry immediately: removal and the + * paired `session/disposed` edge wait until the creation dispatch unwinds. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): the carrier is the + * session's owner scope, captured when the session was ENTERED (an agent's + * session is entered through `agent.ctx`, so its events dispatch in that + * agent's scope; a bare `sessions.create()` from a plain plugin dispatches + * subject-less). A listener registered through `agent.ctx` hears only that + * agent's sessions; a plain plugin listener hears every session. * @param session - the session just entered and announced. * @mode emit */ - 'session/created'(session: Session): void + 'session/created'(this: Scoped, session: Session): void + /** + * A previously announced session left the store. Emitted exactly once on + * normal detach or publication rollback, and never for a prepared/entered + * session whose `session/created` announcement did not begin. Listener + * failures (including returned-promise rejections) are logged and contained + * per listener so teardown always reaches quiescence. + * Scope-filtered dispatch uses the same owner carrier captured at entry; + * agent-scoped listeners hear only their own session's teardown. + * @param session - the session that is no longer live in the store. + * @mode emit + */ + 'session/disposed'(this: Scoped, session: Session): void /** * An event was appended to a session log (sync, fire-and-forget). This is - * the per-append feed a UI or invariant plugin tails. + * the per-append feed a UI or invariant plugin tails. The log push is the + * commit point; synchronous throws and returned-promise rejections from + * observers are logged and contained per listener, so they cannot make a + * committed append appear to fail or starve later listeners. The exact + * callback list and Cordis internal-dispatch checks resolve before the push; + * callbacks themselves run only after it. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): the carrier is the + * session's owner scope, captured when the session was ENTERED (an agent's + * session is entered through `agent.ctx`, so its events dispatch in that + * agent's scope; a bare `sessions.create()` from a plain plugin dispatches + * subject-less). A listener registered through `agent.ctx` hears only that + * agent's sessions; a plain plugin listener hears every session. * @param session - the session whose log grew. * @param event - the appended event, exactly as recorded. * @mode emit */ - 'session/event'(session: Session, event: SessionEvent): void + 'session/event'(this: Scoped, session: Session, event: SessionEvent): void /** * Awaited durability checkpoint. The agent loop awaits - * `ctx.parallel('session/flush', session)` at every turn end; persistence + * `ctx.sessions.flush(session)` at every turn end; persistence * plugins (JSONL, SQLite) drain their write-behind buffers here and on * fiber dispose. Awaited (parallel), not a waterfall: every listener runs - * and the loop waits for all of them, but none can veto. + * and the caller waits for all of them, but none can veto. Dispatch it + * through {@link SessionStore.flush} — the store owns the carrier — never + * via a raw `ctx.parallel`. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): the carrier is the + * session's owner scope, captured when the session was ENTERED (an agent's + * session is entered through `agent.ctx`, so its events dispatch in that + * agent's scope; a bare `sessions.create()` from a plain plugin dispatches + * subject-less). A listener registered through `agent.ctx` hears only that + * agent's sessions; a plain plugin listener hears every session. * @param session - the session whose buffered events must reach durable storage. * @mode parallel */ - 'session/flush'(session: Session): Promise | void + 'session/flush'(this: Scoped, session: Session): Promise | void } } @@ -77,6 +121,137 @@ function renderTagged(tag: string, content: ContentBlock[], source: MessageSourc ] } +/** Detach, validate, and freeze the creation metadata published by a session. */ +function snapshotSessionHeader(id: SessionId, source?: SessionHeader): SessionHeader { + const input: unknown = source === undefined + ? { version: SESSION_FORMAT_VERSION, id, createdAt: Date.now() } + : source + const snapshot = snapshotJsonValue(input) + if (snapshot === undefined) throw new Error('session header is not losslessly JSON-serializable') + if (snapshot === null || typeof snapshot !== 'object' || Array.isArray(snapshot)) { + throw new Error('session header is not a plain JSON record') + } + const record = snapshot as Record + if (record.version !== SESSION_FORMAT_VERSION) { + throw new Error(`session header version must be ${SESSION_FORMAT_VERSION}, got ${String(record.version)}`) + } + if (record.id !== id) { + throw new Error(`session header id "${String(record.id)}" does not match session id "${id}"`) + } + if (typeof record.createdAt !== 'number' || !Number.isFinite(record.createdAt)) { + throw new Error('session header createdAt must be a finite number') + } + if (record.cwd !== undefined) { + if (typeof record.cwd !== 'string') throw new Error('session header cwd must be a string') + if (!isAbsolute(record.cwd)) { + throw new Error(`session header cwd must be an absolute path, got "${record.cwd}"`) + } + } + if (record.parentSession !== undefined && typeof record.parentSession !== 'string') { + throw new Error('session header parentSession must be a string') + } + if (record.seedLength !== undefined + && (typeof record.seedLength !== 'number' || !Number.isSafeInteger(record.seedLength) || record.seedLength < 0)) { + throw new Error('session header seedLength must be a non-negative safe integer') + } + return deepFreeze(record as unknown as SessionHeader) +} + +/** Validate the runtime shape of surface metadata after its JSON snapshot. */ +function assertSurfaceMetadataShape( + type: string, + surfaceOp: unknown, + sourceEventSeqs: unknown, +): void { + const eligible = isSurfaceEligibleType(type) + if (!eligible) { + if (surfaceOp !== undefined || sourceEventSeqs !== undefined) { + throw new Error(`session event "${type}" is not surface-eligible and cannot carry surface metadata`) + } + return + } + if (surfaceOp === undefined) { + throw new Error(`session event "${type}" is surface-eligible and requires a surfaceOp marker`) + } + if (surfaceOp !== 'append') { + if (surfaceOp === null || typeof surfaceOp !== 'object' || Array.isArray(surfaceOp)) { + throw new Error(`session event "${type}" carries an invalid surfaceOp`) + } + const op = surfaceOp as Record + const keys = Object.keys(op) + if (keys.length !== 3 || !Object.hasOwn(op, 'op') || !Object.hasOwn(op, 'start') || !Object.hasOwn(op, 'end') + || op['op'] !== 'replace' + || typeof op['start'] !== 'number' || !Number.isSafeInteger(op['start']) || op['start'] < 0 + || typeof op['end'] !== 'number' || !Number.isSafeInteger(op['end']) || op['end'] < 0) { + throw new Error(`session event "${type}" carries an invalid replace surfaceOp`) + } + } + if (sourceEventSeqs !== undefined) { + if (!Array.isArray(sourceEventSeqs) + || sourceEventSeqs.some(seq => typeof seq !== 'number' || !Number.isSafeInteger(seq) || seq < 0)) { + throw new Error(`session event "${type}" sourceEventSeqs must contain non-negative safe integers`) + } + } +} + +/** Validate the fixed event envelope after one-pass JSON materialization. */ +function assertSessionEventEnvelope(value: Record, index: number): asserts value is SessionEvent { + const event = value + const allowed = new Set(['type', 'seq', 'time', 'data', 'surfaceOp', 'sourceEventSeqs']) + if (Object.keys(event).some(key => !allowed.has(key)) + || !Object.hasOwn(event, 'type') || typeof event['type'] !== 'string' + || !Object.hasOwn(event, 'seq') || typeof event['seq'] !== 'number' + || !Number.isSafeInteger(event['seq']) || event['seq'] < 0 + || !Object.hasOwn(event, 'time') || typeof event['time'] !== 'number' + || !Number.isSafeInteger(event['time']) || event['time'] < 0 + || !Object.hasOwn(event, 'data')) { + throw new Error(`seed event at index ${index} has an invalid event envelope`) + } +} + +type SessionCallback = (...args: unknown[]) => unknown + +/** Resolve one listener snapshot, including Cordis's internal dispatch checks. */ +function collectSessionCallbacks(ctx: Context, args: unknown[]): SessionCallback[] { + return [...ctx.events.dispatch('emit', args)] as SessionCallback[] +} + +/** Invoke one resolved observe-only listener snapshot with per-listener containment. */ +function invokeContainedSessionObservers( + ctx: Context, + name: 'session/event' | 'session/disposed', + id: SessionId, + args: unknown[], + callbacks: SessionCallback[], +): void { + for (const callback of callbacks) { + try { + const returned: unknown = callback(...args) + void Promise.resolve(returned).catch((error: unknown) => { + ctx.logger.warn(`session "${id}": ${name} listener rejected: ${String(error)}`) + }) + } catch (error: unknown) { + ctx.logger.warn(`session "${id}": ${name} listener threw: ${String(error)}`) + } + } +} + +/** All mutable lifecycle state for one exact store entry. */ +interface SessionEntry { + readonly id: SessionId + readonly session: Session + readonly carrier: Scoped + readonly emitCtx: Context + announced: boolean + announcing: boolean + appending: boolean + detachRequested: boolean + detach(): void +} + +/** Store attachment for the append path; module-private to keep Session store-agnostic publicly. */ +const attachments = new WeakMap() + /** * An event-sourced session: an append-only log of {@link SessionEvent}s. * @@ -85,8 +260,6 @@ function renderTagged(tag: string, content: ContentBlock[], source: MessageSourc */ export class Session { private log: SessionEvent[] = [] - /** Set by the store so appends are observable; undefined when detached. */ - onAppend: ((event: SessionEvent) => void) | undefined /** * Derived surface — a cached linked list of message-producing events. @@ -104,16 +277,16 @@ export class Session { } /** - * Immutable creation metadata (format version, cwd, lineage, seed boundary). - * Supplied by the store via `ctx.sessions.create()`. When a `Session` is - * constructed bare (tests, ad-hoc replay), a minimal header is synthesized - * (stamped with the current {@link SESSION_FORMAT_VERSION}) so + * Detached, deep-frozen creation metadata (format version, cwd, lineage, + * seed boundary). Supplied by the store via `ctx.sessions.create()`. When a + * `Session` is constructed bare (tests, ad-hoc replay), a minimal header is + * synthesized (stamped with the current {@link SESSION_FORMAT_VERSION}) so * `session.header` is always present. Kept out of the event log — it is a * storage concern, not replayable conversation state. */ readonly header: SessionHeader - constructor(public readonly id: SessionId, seed?: SessionEvent[], header?: SessionHeader) { + constructor(public readonly id: SessionId, seed?: readonly SessionEvent[], header?: SessionHeader) { if (seed) { // Validate the seed to the SAME invariants `append` enforces, so a // replay/fork (`ctx.sessions.create(id, { seed })`) cannot construct a @@ -122,12 +295,16 @@ export class Session { // `seq = log.length` contract the whole system relies on). Without this, // a bad seed would surface only later as a backend rejection or a silent // divergence between the live log and disk. - seed.forEach((event, index) => { - if (event.seq !== index) { - throw new Error(`seed event at index ${index} has seq ${event.seq} (expected ${index}); seed must be contiguous from 0`) + this.log = Array.from(seed, (source, index) => { + // The seed is a persistence/replay boundary: validate and detach the + // complete event in one lossless-JSON pass. + const snapshot = snapshotJsonValue(source) + if (snapshot === undefined) { + throw new Error(`seed event at index ${index} is not losslessly JSON-serializable`) } - if (!isJsonValue(event.data)) { - throw new Error(`seed event "${event.type}" (seq ${event.seq}) carries non-JSON-serializable data`) + assertSessionEventEnvelope(snapshot, index) + if (snapshot.seq !== index) { + throw new Error(`seed event at index ${index} has seq ${snapshot.seq} (expected ${index}); seed must be contiguous from 0`) } // Surface-eligible events MUST carry a surfaceOp marker — the surface is // the sole source of derived history, so a marker-less message event @@ -135,30 +312,30 @@ export class Session { // this at compile time via its typed overload; a seed arrives as raw // SessionEvent[] (replay/fork/load), bypassing that, so re-check at // runtime here rather than silently resuming with empty history. - if (isSurfaceEligibleType(event.type) - && (event as SessionEvent).surfaceOp === undefined) { - throw new Error(`seed event "${event.type}" (seq ${event.seq}) is surface-eligible but carries no surfaceOp marker`) + const structural = snapshot as SessionEvent & { surfaceOp?: unknown; sourceEventSeqs?: unknown } + try { + assertSurfaceMetadataShape(snapshot.type, structural.surfaceOp, structural.sourceEventSeqs) + } catch (error: unknown) { + throw new Error(`invalid seed event at index ${index}: ${error instanceof Error ? error.message : 'invalid surface metadata'}`) } + return deepFreeze(snapshot) }) - // Deep-clone each seed event, NOT just the array: the seed events and - // their `data` are still owned by the caller (or the source session of a - // fork), so keeping the references would let a post-create mutation of the - // original rewrite this session's durable log — or reintroduce a - // non-JSON-serializable value AFTER the validation above. Snapshotting at - // the boundary makes `session.events` independent and keeps it equal to - // what was validated. Serializability is guaranteed by the check above, so - // structuredClone can never hit a non-cloneable value here. - this.log = seed.map(event => structuredClone(event)) } - this.header = header ?? { version: SESSION_FORMAT_VERSION, id, createdAt: Date.now() } + this.header = snapshotSessionHeader(id, header) } + /** Cached immutable public snapshot of the private append-only log. */ + private eventsSnapshot: readonly SessionEvent[] | undefined + /** - * The append-only event log, exposed live by reference (readonly-typed, not - * a snapshot): later appends are visible through the same array. + * An immutable snapshot of the append-only event log. The snapshot is reused + * until the next append; a previously returned array does not grow later. + * Events and their nested data are deep-frozen at acceptance, so neither a + * cast nor ordinary JavaScript can rewrite durable history. */ get events(): readonly SessionEvent[] { - return this.log + this.eventsSnapshot ??= Object.freeze([...this.log]) + return this.eventsSnapshot } /** The next event's sequence number — always the log length (the `seq = log.length` contiguity contract). */ @@ -168,8 +345,11 @@ export class Session { /** * Append one typed event to the log and synchronously notify observers via - * `onAppend`. The hot path never blocks on I/O — persistence plugins buffer - * asynchronously. + * the store-owned, module-private publication hooks. The hot path never blocks + * on I/O — persistence plugins buffer asynchronously. Once the event enters + * the log, the append is committed: observer failures are logged and + * contained per listener, so they do not change the return value or prevent + * later listeners from observing the same accepted event. * * @param type - The event type (key of {@link SessionEventMap}). * @param data - The event payload; must be JSON-serializable. @@ -183,66 +363,71 @@ export class Session { * @returns the logged event — its assigned `seq`/`time` plus the SNAPSHOT of * `data` that entered the log, so reading `event.data` back sees the logged * value, never the caller's still-mutable input. - * @throws if `data` is not losslessly JSON-serializable (BigInt, function, - * symbol, undefined, non-finite number, circular ref, or an exotic object - * like Map/Set/Date). The event log is the durable source of truth, so this - * invariant is enforced at the source — a bad event never enters the log, - * keeping `session.events` always equal to what a backend can persist. The - * throw surfaces at the buggy caller's append site, not asynchronously in a - * backend flush. + * @throws if `data` or surface metadata is not losslessly JSON-serializable + * (BigInt, function, symbol, undefined, negative zero, non-finite number, + * circular reference, sparse array, or an exotic object such as + * Map/Set/Date/class instance). One recursive pass reads, validates, and + * copies each nested value once, so a stateful getter cannot supply one value + * to validation and another to storage. The event log is the durable source + * of truth, so a bad event fails at the append site rather than later during + * a backend flush. A synchronous internal dispatch validation failure or an + * append reentered while this acceptance/publication boundary is open also + * rejects before the log changes. */ append( type: T, data: SessionEventMap[T], ...opts: T extends SurfaceEventType ? [opts: SurfaceIntent] : [] ): SessionEvent { - if (!isJsonValue(data)) { + const surfaceOpts: SurfaceIntent | undefined = opts[0] + const surfaceMetadata = { + ...surfaceOpts?.sourceEventSeqs === undefined ? {} : { sourceEventSeqs: surfaceOpts.sourceEventSeqs }, + ...surfaceOpts?.surfaceOp === undefined ? {} : { surfaceOp: surfaceOpts.surfaceOp }, + } + const dataSnapshot = snapshotJsonValue(data) + if (dataSnapshot === undefined) { throw new Error(`session event "${type}" carries non-JSON-serializable data`) } - const surfaceOpts: SurfaceIntent | undefined = opts[0] - // Surface-eligible events MUST carry a surfaceOp marker — the surface is the - // sole source of derived history, so a marker-less message event would be - // logged yet vanish from deriveMessages(). The typed `opts` overload makes - // the marker mandatory only when `T` is a SPECIFIC SurfaceEventType literal; - // when `T` widens to the SessionEventType union (a caller iterating raw - // events: `for (const e of log) append(e.type, e.data)`), the conditional - // rest collapses to optional and the compiler stops enforcing it. Re-check - // at runtime so that loophole can't silently drop history. - if (isSurfaceEligibleType(type) && surfaceOpts?.surfaceOp === undefined) { - throw new Error(`session event "${type}" is surface-eligible and requires a surfaceOp marker`) + const surfaceMetadataSnapshot = snapshotJsonValue(surfaceMetadata) + if (surfaceMetadataSnapshot === undefined) { + throw new Error(`session event "${type}" carries non-JSON-serializable surface metadata`) } - // Snapshot `data` into the log, NOT the caller's reference: the validation - // above proves it is JSON-serializable AT THIS MOMENT, but the caller still - // owns the object and could mutate it afterwards (before a persistence - // flush, or permanently in the in-memory history) — making `session.events` - // diverge from the value that passed validation, or reintroducing a - // non-serializable value. Cloning here keeps the log equal to what was - // validated. structuredClone is safe because serializability was just - // checked. The returned event carries the SAME snapshot, so a caller reading - // back `event.data` sees the logged value, not its own mutable input. - // - // Surface metadata is snapshot separately: sourceEventSeqs (number[] — - // primitives, so array spread is a complete copy) and surfaceOp (a string - // primitive, or cloned if it's a replace object). - // Build the event shape with conditional surface fields via spreading. - // The result is cast through `unknown` because the conditional spreads - // produce an intersection type that the assignability checker can't - // narrow to a specific discriminated-union member when T is generic. - // This is a safe internal boundary: data was validated above, and - // surface metadata was snapshot from primitive/clone-safe values. - const event = { + assertSurfaceMetadataShape( type, - seq: this.log.length, - time: Date.now(), - data: structuredClone(data), - ...surfaceOpts?.sourceEventSeqs !== undefined ? { sourceEventSeqs: [...surfaceOpts.sourceEventSeqs] } : {}, - ...surfaceOpts?.surfaceOp !== undefined ? { - surfaceOp: typeof surfaceOpts.surfaceOp === 'string' ? surfaceOpts.surfaceOp : structuredClone(surfaceOpts.surfaceOp), - } : {}, - } as unknown as SessionEvent - this.log.push(event as unknown as SessionEvent) - this.onAppend?.(event as unknown as SessionEvent) - return event + (surfaceMetadataSnapshot as { surfaceOp?: unknown }).surfaceOp, + (surfaceMetadataSnapshot as { sourceEventSeqs?: unknown }).sourceEventSeqs, + ) + + const entry = attachments.get(this) + if (entry?.appending) { + throw new Error('session append cannot reenter while another append is being published') + } + if (entry !== undefined) entry.appending = true + try { + const event = deepFreeze({ + type, + seq: this.log.length, + time: Date.now(), + data: dataSnapshot, + ...surfaceMetadataSnapshot, + } as unknown as SessionEvent) + let callbacks: SessionCallback[] | undefined + const callbackArgs: unknown[] = [this, event] + if (entry !== undefined) { + callbacks = collectSessionCallbacks(entry.emitCtx, [entry.carrier, 'session/event', ...callbackArgs]) + } + this.log.push(event as SessionEvent) + this.eventsSnapshot = undefined + if (callbacks !== undefined && entry !== undefined) { + invokeContainedSessionObservers(entry.emitCtx, 'session/event', entry.id, callbackArgs, callbacks) + } + return event + } finally { + if (entry !== undefined) { + entry.appending = false + if (entry.detachRequested && !entry.announcing) entry.detach() + } + } } /** Cached fold of the request-header events — see {@link requestHeader}. */ @@ -290,10 +475,9 @@ export class Session { * call costs O(new nodes), and a surface rewrite (a `replace`; * {@link SurfaceManager.replaceGeneration}) rebuilds. The returned array is * a fresh snapshot per call (later appends never grow an array a caller - * already holds); the `Message` objects in it are SHARED and **deep-frozen** - * — cloned once off the log at projection time, so consumers can never - * mutate logged data, and mutation attempts throw instead of silently - * diverging replay from history. + * already holds); the `Message` objects in it are SHARED and **deep-frozen**. + * Their content reuses the already frozen durable event data, so the cache + * needs no second deep clone and consumers still cannot mutate the log. * @returns a fresh array of the shared, frozen derived history. */ deriveMessages(): Message[] { @@ -325,9 +509,10 @@ export class Session { * The per-node pure function {@link deriveMessages} folds over the surface; * an external reconstructor (or the dev invariant) folds the same function * over a log prefix's surface to rebuild the exact messages any request was - * built from (the reconstructability RFC). The returned `content` is - * deep-cloned off the logged event: the log is append-only by contract, so - * no live reference to logged data leaves this boundary. + * built from (the reconstructability RFC). The returned message wrapper is + * fresh; its content reuses the logged event's already deep-frozen durable + * data, so changing the wrapper cannot rewrite the log and changing content + * throws. * @param event - the event to project. * @returns the derived message, or null when the event produces none. */ @@ -338,29 +523,29 @@ export class Session { switch (event.type) { case 'user/message': { - return { role: 'user', content: structuredClone(event.data.content) } + return { role: 'user', content: event.data.content } } case 'assistant/message': { // Skip an empty-content assistant/message: it exists only to host a // max-tokens step's usage and must not inject a content-less assistant // turn into the provider transcript. if (event.data.content.length === 0) return null - return { role: 'assistant', content: structuredClone(event.data.content) } + return { role: 'assistant', content: event.data.content } } case 'tool/result': { const { callId, content, isError } = event.data return { role: 'user', - content: [{ type: 'tool-result', toolCallId: callId, content: structuredClone(content), isError }], + content: [{ type: 'tool-result', toolCallId: callId, content, isError }], } } case 'context/message': { const { content, source } = event.data - return { role: 'user', content: renderTagged('context', structuredClone(content), source) } + return { role: 'user', content: renderTagged('context', content, source) } } case 'steering/message': { const { content, source } = event.data - return { role: 'user', content: renderTagged('steering', structuredClone(content), source) } + return { role: 'user', content: renderTagged('steering', content, source) } } default: // A non-surface event (boundary, chunk, log-only record) projects to @@ -403,7 +588,7 @@ export class SessionForkError extends Error { * subscribe to `session/event` and flush on `session/flush` / dispose. */ export class SessionStore extends Service { - private store = new Map() + private store = new Map() private counter = 0 constructor(ctx: Context) { @@ -419,15 +604,16 @@ export class SessionStore extends Service { * fills `version`/`id`/`createdAt`). * * For an agent whose session must be torn down IN ORDER with its loop (so the - * loop's final flush is captured before `onAppend` detaches), do NOT use this + * loop's final flush is captured before the store attachment ends), do NOT use this * — fold the session lifecycle into the agent's own effect via - * {@link prepare} + {@link enter} + {@link announce} (see `dsh-agent-loop`'s - * `startOwned`). + * {@link prepare} + {@link enter} + {@link announce} (see + * `dsh-agent-loop`'s creation transaction). * * @param id - the session id; omitted, the store mints `session-`. * @param options - seed events and/or creation metadata for the header. * @returns the live session, already entered and announced. - * @throws if a session with `id` already exists, or if `meta.cwd` is a + * @throws if a session with `id` already exists, metadata is not a plain + * lossless-JSON record with valid scalar fields, or `meta.cwd` is a * non-absolute path (storage backends key directories off it). */ create(id?: SessionId, options?: CreateSessionOptions): Session { @@ -435,7 +621,7 @@ export class SessionStore extends Service { // Single effect owned by the calling fiber. Yield the detach BEFORE // announcing so a throwing `session/created` listener rolls the attach back // (the generator effect disposes already-yielded disposers on a throw) - // instead of leaking the store entry + onAppend. + // instead of leaking the store entry and its publication hooks. this.ctx.effect(function* (this: SessionStore) { yield this.enter(session) this.announce(session) @@ -449,37 +635,42 @@ export class SessionStore extends Service { * Pairs with {@link enter} + {@link announce}: a caller that owns a composite * `ctx.effect` (the agent factory) folds the session lifecycle into that ONE * effect so a fiber unload tears the session + agent down as a single ORDERED - * chain rather than as racing sibling effects — which would detach `onAppend` + * chain rather than as racing sibling effects — which would remove the publication hooks * before the loop's closing `session/flush`, dropping the closing events. * * @param id - the session id; omitted, the store mints `session-`. * @param options - seed events and/or creation metadata for the header. * @returns the constructed session, NOT yet in the store. - * @throws if a session with `id` already exists, or if `meta.cwd` is a + * @throws if a session with `id` already exists, metadata is not a plain + * lossless-JSON record with valid scalar fields, or `meta.cwd` is a * non-absolute path. */ prepare(id?: SessionId, options?: CreateSessionOptions): Session { - const sessionId = SessionId(id ?? `session-${++this.counter}`) - if (this.store.has(sessionId)) throw new Error(`session "${sessionId}" already exists`) - const cwd = options?.meta?.cwd - if (cwd !== undefined && !isAbsolute(cwd)) { - throw new Error(`session cwd must be an absolute path, got "${cwd}"`) + let sessionId: SessionId + if (id === undefined) { + do sessionId = SessionId(`session-${++this.counter}`) + while (this.store.has(sessionId)) + } else { + sessionId = SessionId(id) } + if (this.store.has(sessionId)) throw new Error(`session "${sessionId}" already exists`) + const seed = options?.seed + const meta = options?.meta const header: SessionHeader = { version: SESSION_FORMAT_VERSION, id: sessionId, - createdAt: options?.meta?.createdAt ?? Date.now(), - ...cwd !== undefined ? { cwd } : {}, - ...options?.meta?.parentSession !== undefined ? { parentSession: options.meta.parentSession } : {}, - ...options?.meta?.seedLength !== undefined ? { seedLength: options.meta.seedLength } : {}, + createdAt: meta?.createdAt ?? Date.now(), + ...meta?.cwd === undefined ? {} : { cwd: meta.cwd }, + ...meta?.parentSession === undefined ? {} : { parentSession: meta.parentSession }, + ...meta?.seedLength === undefined ? {} : { seedLength: meta.seedLength }, } - return new Session(sessionId, options?.seed, header) + return new Session(sessionId, seed, header) } /** - * Enter a {@link prepare}d session into the store: wire `onAppend` → - * `session/event` and add it to the store. Returns the DETACH disposer - * (`onAppend = undefined` + store removal). Does NOT emit `session/created` — + * Enter a {@link prepare}d session into the store: install the module-private + * append publication hooks and add it to the store. Returns the DETACH + * disposer (hooks + store removal). Does NOT emit `session/created` — * the caller yields this disposer inside its effect and THEN calls * {@link announce}, so a throwing `session/created` listener rolls the attach * back instead of leaking it. @@ -493,25 +684,143 @@ export class SessionStore extends Service { * assume that. * * @param session - a {@link prepare}d session not yet in the store. - * @returns the detach disposer (`onAppend = undefined` + store removal). + * @returns the detach disposer (publication hooks + store removal). When called from + * a synchronous `session/created` listener, removal and disposal wait until + * that creation dispatch unwinds. * @throws if a session with this id is already in the store. */ enter(session: Session): () => void { - if (this.store.has(session.id)) throw new Error(`session "${session.id}" already exists`) - session.onAppend = (event) => { this.ctx.emit('session/event', session, event) } - this.store.set(session.id, session) - return () => { - session.onAppend = undefined - this.store.delete(session.id) + const id = session.id + const carrier = scopeTarget(session, scopeOf(this.ctx)) + // This is the authoritative collision boundary after arbitrary unpublished + // preparation. Only one exact same-id transaction can publish. + if (this.store.has(id)) throw new Error(`session "${id}" already exists`) + if (attachments.has(session)) throw new Error(`session "${id}" is already attached to a store`) + const entry: SessionEntry = { + id, + session, + carrier, + emitCtx: this.ctx, + announced: false, + announcing: false, + appending: false, + detachRequested: false, + detach: () => { this.detachEntered(entry) }, + } + this.store.set(id, entry) + attachments.set(session, entry) + let entered = true + const detach = (): void => { + if (!entered) return + entered = false + // A lifecycle listener may own the advanced detach capability. Keep the + // entry and its publication hooks live until synchronous creation or append + // publication unwinds, then publish the paired disposal edge. + if (entry.announcing || entry.appending) { + entry.detachRequested = true + return + } + entry.detach() + } + return detach + } + + /** Remove one exact entered session and emit its paired disposal when announced. */ + private detachEntered(entry: SessionEntry): void { + entry.detachRequested = false + // A stale capability cannot remove observers or storage belonging to a + // later same-id lifecycle. + /* v8 ignore next -- enter() rejects replacement while this single-shot detach capability is live. */ + if (this.store.get(entry.id) !== entry) return + this.store.delete(entry.id) + attachments.delete(entry.session) + if (entry.announced) this.emitDisposed(entry) + } + + /** Emit `session/created` exactly once for an {@link enter}ed session (with + * the carrier {@link enter} captured). Separate from {@link enter} so the + * caller can yield the detach disposer first (rollback safety — see + * {@link enter}). + * @param session - the entered session to announce to listeners. + * @throws if the session is not live or its announcement already began, + * including a reentrant call from a creation listener. */ + announce(session: Session): void { + const entry = this.liveEntryFor(session) + if (entry.announced || entry.announcing) { + throw new Error(`session "${entry.id}" was already announced`) + } + // Mark before emit: Cordis emit may deliver to earlier listeners and then + // throw. Rollback must still pair that partial creation with disposal, and + // a listener cannot recursively create a second lifecycle edge. + entry.announced = true + const callbackArgs: unknown[] = [session] + entry.announcing = true + try { + const callbacks = collectSessionCallbacks(this.ctx, [entry.carrier, 'session/created', session]) + for (const callback of callbacks) { + // Synchronous throws intentionally propagate and veto publication; the + // yielded detach then emits the paired disposal edge. An async function + // is nevertheless assignable to a void listener, so observe its returned + // promise: rejection is too late to roll back and must be logged instead + // of becoming unhandled. + const returned: unknown = callback(...callbackArgs) + void Promise.resolve(returned).catch((error: unknown) => { + this.ctx.logger.warn(`session "${entry.id}": session/created listener rejected: ${String(error)}`) + }) + } + } finally { + entry.announcing = false + if (entry.detachRequested && !entry.appending) entry.detach() } } - /** Emit `session/created` for an {@link enter}ed session. Separate from - * {@link enter} so the caller can yield the detach disposer first (rollback - * safety — see {@link enter}). - * @param session - the entered session to announce to listeners. */ - announce(session: Session): void { - this.ctx.emit('session/created', session) + /** Emit the paired teardown notification with per-listener containment. */ + private emitDisposed(entry: SessionEntry): void { + const callbackArgs: unknown[] = [entry.session] + try { + const callbacks = collectSessionCallbacks(this.ctx, [entry.carrier, 'session/disposed', entry.session]) + invokeContainedSessionObservers(this.ctx, 'session/disposed', entry.id, callbackArgs, callbacks) + } catch (error: unknown) { + this.ctx.logger.warn(`session "${entry.id}": session/disposed dispatch threw: ${String(error)}`) + } + } + + /** + * Dispatch the awaited `session/flush` durability checkpoint for `session`, + * with the carrier captured at {@link enter}. THE flush entry point: the + * store owns the carrier, so callers (the loop's turn-end checkpoint, idle + * injection, teardown drains) must come through here rather than dispatch a + * raw `ctx.parallel('session/flush', …)` — one owner, one spelling, and the + * scoped-dispatch invariant can pin it. + * @param session - the session whose buffered events must reach durable storage. + * @returns resolves when every flush listener has settled; after all settle, + * rejects with the first registered listener failure if any listener failed. + */ + async flush(session: Session): Promise { + const { carrier } = this.liveEntryFor(session) + const callbackArgs: unknown[] = [session] + const callbacks = collectSessionCallbacks(this.ctx, [carrier, 'session/flush', session]) + const results = await Promise.allSettled(callbacks.map((callback) => { + try { + return callback(...callbackArgs) + } catch (error: unknown) { + // Preserve the listener's exact rejection value; flush is a caller-owned + // failure boundary, and Cordis listeners may throw arbitrary values. + // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors + return Promise.reject(error) + } + })) + const failure = results.find((result): result is PromiseRejectedResult => result.status === 'rejected') + if (failure !== undefined) throw failure.reason + } + + /** Return the exact live entry; detached/prepared objects reject. */ + private liveEntryFor(session: Session): SessionEntry { + const entry = attachments.get(session) + if (entry === undefined || this.store.get(entry.id) !== entry) { + throw new Error(`session "${session.id}" is not live in this store`) + } + return entry } /** @@ -520,7 +829,7 @@ export class SessionStore extends Service { * @returns the session, or undefined when no live session has that id. */ get(id: SessionId): Session | undefined { - return this.store.get(id) + return this.store.get(id)?.session } /** @@ -528,7 +837,7 @@ export class SessionStore extends Service { * @returns a fresh array; mutating it does not affect the store. */ list(): Session[] { - return [...this.store.values()] + return [...this.store.values()].map(entry => entry.session) } /** @@ -598,7 +907,7 @@ export class SessionStore extends Service { ) } - return events.slice(0, boundary + 1).map(event => structuredClone(event)) + return events.slice(0, boundary + 1) } private _resolveForkSource(source: SessionForkSource): Session { diff --git a/packages/core/session/src/json.ts b/packages/core/session/src/json.ts index 22303c6c61..2ec36087dd 100644 --- a/packages/core/session/src/json.ts +++ b/packages/core/session/src/json.ts @@ -1,45 +1,127 @@ /** - * JSON-serializability validation for session event data. + * Lossless-JSON validation and snapshot materialization for session data. * * The session event log is the durable source of truth (the event-sourcing / session-persistence RFCs): every * `event.data` must round-trip losslessly through JSON so any persistence * backend can store and reload it byte-identically. This invariant belongs to * the log itself — `Session.append` enforces it at the source, so a * non-serializable event never enters `session.events` and the live log can - * never diverge from what a backend can persist. Backends re-use the same - * predicate to validate their own `append(events)` entry point (replay/fork - * paths that do not go through a live `Session`). + * never diverge from what a backend can persist. Other public boundaries use + * {@link snapshotJsonValue} when they must validate and detach in one pass; + * {@link isJsonValue} remains the non-copying structural predicate. * * @module @deepseek-ai/dsh-session/json */ /** * A value that round-trips losslessly through JSON: `null`, a boolean, a finite - * number, a string, an array of such values, or a plain object whose values are - * such values. The static type companion to {@link isJsonValue} (which validates - * the same shape at runtime). Use it to type a payload that must survive - * session-log persistence and replay byte-identically — e.g. a tool's private - * presentation `meta`. + * number other than negative zero, a string, an array of such values, or a + * plain object whose values are such values. TypeScript cannot distinguish + * `-0` from `number`, so {@link isJsonValue} and {@link snapshotJsonValue} + * enforce that last numeric detail at runtime. Use this type for a payload that + * must survive session-log persistence and replay byte-identically — e.g. a + * tool's private presentation `meta`. */ export type JsonValue = null | boolean | number | string | JsonValue[] | { [key: string]: JsonValue } /** - * Whether `value` is losslessly JSON-serializable: only `null`, finite numbers, - * booleans, strings, plain arrays, and plain objects of such values. Rejects - * `BigInt`, function, symbol, `undefined`, non-finite numbers (`NaN`/`Infinity`, - * which `JSON.stringify` turns into `null`), and exotic objects (`Map`/`Set`/ - * `Date`/class instances) — anything `JSON.stringify` would drop, throw on, or - * convert lossily. Sparse arrays are rejected too: a hole serializes to `null`, - * so `[1, , 3]` would not round-trip. Detects circular references (which would - * throw) and reports them as non-serializable rather than propagating the throw. + * Materialize one detached lossless-JSON snapshot in a SINGLE recursive pass. + * Each array slot or own enumerable string-keyed object value is read exactly + * once, validated, and copied immediately. This is intentionally not + * `isJsonValue(value)` followed by `structuredClone(value)`: a stateful getter + * could return plain JSON to the check and an exotic class instance to the + * clone, whose prototype `structuredClone` would erase before a later check. * - * Scope — matches `JSON.stringify` exactly: only an object's OWN ENUMERABLE - * STRING-keyed properties are inspected (`Object.values`). Symbol-keyed and - * non-enumerable properties are NOT examined, because `JSON.stringify` likewise - * drops them — they never reach the durable form, so a non-serializable value - * hiding under a symbol/non-enumerable key cannot make the round-trip lossy. - * Getters are invoked during the check (again as `JSON.stringify` would), so the - * contract is for plain data records, not objects with side-effecting accessors. + * Accepts the same scalar/object vocabulary as {@link isJsonValue}: arrays use + * the ordinary `Array.prototype` (subclass instances are not plain JSON + * containers), while null-prototype objects are accepted and normalized to + * ordinary plain objects. Sparse arrays, cycles, negative zero, non-finite + * numbers, unsupported scalar types, and exotic object or array shells return + * `undefined`. A throwing getter is a caller failure and propagates unchanged. + * + * @param value - the candidate value to validate and detach. + * @returns the detached snapshot, or `undefined` when the value is not + * losslessly JSON-serializable. + */ +export function snapshotJsonValue(value: T): T | undefined { + const ancestors = new Set() + + const visit = (current: unknown): JsonValue | undefined => { + if (current === null) return null + switch (typeof current) { + case 'boolean': + case 'string': + return current + case 'number': + return Number.isFinite(current) && !Object.is(current, -0) ? current : undefined + case 'bigint': + case 'function': + case 'symbol': + case 'undefined': + return undefined + case 'object': + break + } + + if (ancestors.has(current)) return undefined + ancestors.add(current) + try { + if (Array.isArray(current)) { + if (Object.getPrototypeOf(current) !== Array.prototype) return undefined + const length = current.length + const snapshot: JsonValue[] = [] + for (let index = 0; index < length; index++) { + if (!Object.prototype.hasOwnProperty.call(current, index)) return undefined + const item = visit(current[index]) + if (item === undefined) return undefined + snapshot.push(item) + } + return snapshot + } + + const prototype = Object.getPrototypeOf(current) as unknown + if (prototype !== Object.prototype && prototype !== null) return undefined + const snapshot: { [key: string]: JsonValue } = {} + for (const key of Object.keys(current)) { + const item = visit((current as Record)[key]) + if (item === undefined) return undefined + // Define the key as data so a JSON field literally named "__proto__" + // cannot mutate the snapshot's prototype through ordinary assignment. + Object.defineProperty(snapshot, key, { + value: item, + enumerable: true, + configurable: true, + writable: true, + }) + } + return snapshot + } finally { + ancestors.delete(current) + } + } + + return visit(value) as T | undefined +} + +/** + * Whether `value` is losslessly JSON-serializable: only `null`, finite numbers + * other than negative zero, booleans, strings, plain arrays, and plain objects + * of such values. Rejects `BigInt`, function, symbol, `undefined`, `-0` (which + * JSON rewrites to `0`), non-finite numbers (`NaN`/`Infinity`, which JSON turns + * into `null`), and exotic objects (`Map`/`Set`/`Date`/class instances) — + * anything `JSON.stringify` would drop, throw on, or convert lossily. Sparse + * arrays are rejected too: a hole serializes to `null`, so `[1, , 3]` would not + * round-trip. Detects circular references (which would throw) and reports them + * as non-serializable rather than propagating the throw. + * + * Scope — this is a structural plain-data predicate, not an invocation of + * `JSON.stringify`: only an object's OWN ENUMERABLE STRING-keyed properties are + * inspected (`Object.values`). Symbol-keyed and non-enumerable properties are + * omitted from the durable data surface. Custom `toJSON` behavior is not + * executed; boundaries that persist a value first materialize a new plain-data + * record with {@link snapshotJsonValue}. Getters are invoked during this check, + * so callers that need a stable detached value use that one-pass materializer + * instead of checking and then rereading a side-effecting record. * @param value - the candidate event data to test. * @param seen - objects on the current descent path, for circular-reference * detection; the recursion threads it — callers omit it. @@ -52,7 +134,7 @@ export function isJsonValue(value: unknown, seen: Set = new Set()): bool case 'string': return true case 'number': - return Number.isFinite(value) + return Number.isFinite(value) && !Object.is(value, -0) case 'bigint': case 'function': case 'symbol': @@ -66,6 +148,7 @@ export function isJsonValue(value: unknown, seen: Set = new Set()): bool seen.add(value) try { if (Array.isArray(value)) { + if (Object.getPrototypeOf(value) !== Array.prototype) return false // Reject sparse arrays: a hole is skipped by `every`/`forEach` but // JSON.stringify writes it as `null`, so `[1, , 3]` would round-trip // lossily. Require every index 0..length-1 to be an OWN property. diff --git a/packages/core/session/src/types.ts b/packages/core/session/src/types.ts index ca6779e1dc..c4838808c1 100644 --- a/packages/core/session/src/types.ts +++ b/packages/core/session/src/types.ts @@ -32,6 +32,9 @@ export const SESSION_FORMAT_VERSION = 0 /** * Immutable session metadata — written once at creation and never rewritten. + * {@link Session} enforces that contract at runtime: it validates and detaches + * the accepted scalar fields, requires this header's id to match the session + * id, and deep-freezes the published record. * * Kept SEPARATE from the event log deliberately: format-version, cwd, and * lineage are storage concerns, not conversation events, so they stay out of @@ -45,15 +48,15 @@ export interface SessionHeader { * session is created. A persistence backend rejects any other version on load * (no migration — see the constant). */ - version: number + readonly version: number /** The session's id (mirrors the {@link Session}'s id). */ - id: SessionId + readonly id: SessionId /** Unix epoch milliseconds when the session was created. */ - createdAt: number + readonly createdAt: number /** Absolute working directory the session was created in (if any). */ - cwd?: string + readonly cwd?: string /** The session this one was forked from (seed lineage), if any. */ - parentSession?: SessionId + readonly parentSession?: SessionId /** * How many leading events were INHERITED via a seed rather than produced by * this session — the seed boundary. Set when a fork seeds a child with a @@ -63,7 +66,7 @@ export interface SessionHeader { * harness can skip the inherited prefix when deriving the child's OWN script * (the seeded events are the parent's, not this child's model calls). */ - seedLength?: number + readonly seedLength?: number } /** @@ -73,9 +76,10 @@ export interface SessionHeader { */ export interface CreateSessionOptions { /** Events to seed the new session with (replay/fork). */ - seed?: SessionEvent[] + readonly seed?: readonly SessionEvent[] /** - * Creation metadata. The store fills in `version`/`id` and defaults + * Creation metadata. The store reads this plain record and each accepted + * field once, then fills in `version`/`id` and defaults * `createdAt` to now; the caller supplies the storage-level fields (validated * absolute `cwd`, `parentSession` lineage, the seed boundary `seedLength`, and * — when reconstructing a persisted session — the original `createdAt` to @@ -86,7 +90,12 @@ export interface CreateSessionOptions { * length, not the original boundary — the caller must pass the persisted * boundary back. A fresh fork passes its actual seeded-prefix length. */ - meta?: { cwd?: string; parentSession?: SessionId; createdAt?: number; seedLength?: number } + readonly meta?: { + readonly cwd?: string + readonly parentSession?: SessionId + readonly createdAt?: number + readonly seedLength?: number + } } /** diff --git a/packages/core/session/tests/derived-cache.spec.ts b/packages/core/session/tests/derived-cache.spec.ts index b416d15639..46015106c8 100644 --- a/packages/core/session/tests/derived-cache.spec.ts +++ b/packages/core/session/tests/derived-cache.spec.ts @@ -89,15 +89,15 @@ describe('Session.deriveEventMessage — the per-event projection', () => { expect(session.deriveEventMessage(event)).toEqual(session.deriveMessages().at(-1)) }) - it('clones content off the log: the projection never aliases the logged event', () => { + it('reuses the logged event\'s already frozen content', () => { const session = new Session(SessionId('per-event-clone')) session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) const event = session.append('user/message', { content: [{ type: 'text', text: 'orig' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) const message = session.deriveEventMessage(event)! - expect(message.content).not.toBe(event.data.content) - // deriveEventMessage returns an unfrozen clone (the cache freezes ITS - // copies); mutating it must not reach the log. - ;(message.content[0] as { text: string }).text = 'mutated' + expect(message.content).toBe(event.data.content) + expect(Object.isFrozen(message.content)).toBe(true) + expect(Object.isFrozen(message.content[0])).toBe(true) + expect(() => { (message.content[0] as { text: string }).text = 'mutated' }).toThrow() expect(session.deriveMessages().at(-1)!.content).toEqual([{ type: 'text', text: 'orig' }]) }) diff --git a/packages/core/session/tests/fork.spec.ts b/packages/core/session/tests/fork.spec.ts index 25328294cf..af143ea5ee 100644 --- a/packages/core/session/tests/fork.spec.ts +++ b/packages/core/session/tests/fork.spec.ts @@ -60,7 +60,7 @@ describe('SessionStore.fork', () => { }) }) - it('forks the latest completed boundary by default and deep-clones seed events', async () => { + it('forks the latest completed boundary by default into detached frozen seed events', async () => { const { ctx, sessions } = await setup() const source = ctx.sessions.create(SessionId('parent'), { meta: { cwd: '/workspace' } }) appendClosedTurn(source, 1, 'hello') @@ -70,8 +70,11 @@ describe('SessionStore.fork', () => { expect(child.events).toEqual(source.events) expect(child.events).not.toBe(source.events) expect(child.events[1]).not.toBe(source.events[1]) - firstUserMessage(child.events).data.content[0] = { type: 'text', text: 'child mutation' } + expect(() => { + firstUserMessage(child.events).data.content[0] = { type: 'text', text: 'child mutation' } + }).toThrow(TypeError) expect(firstUserMessage(source.events).data.content).toEqual([{ type: 'text', text: 'hello' }]) + expect(firstUserMessage(child.events).data.content).toEqual([{ type: 'text', text: 'hello' }]) expect(child.header).toMatchObject({ id: SessionId('child'), cwd: '/workspace', diff --git a/packages/core/session/tests/json.spec.ts b/packages/core/session/tests/json.spec.ts new file mode 100644 index 0000000000..4fb06fd744 --- /dev/null +++ b/packages/core/session/tests/json.spec.ts @@ -0,0 +1,152 @@ +import { describe, expect, it } from 'vitest' +import { isJsonValue, snapshotJsonValue } from '@deepseek-ai/dsh-session' + +describe('snapshotJsonValue', () => { + it('copies the complete JSON scalar vocabulary and rejects unsupported scalars', () => { + const unsupportedFunction = (): void => {} + + expect(snapshotJsonValue(null)).toBeNull() + expect(snapshotJsonValue(true)).toBe(true) + expect(snapshotJsonValue('text')).toBe('text') + expect(snapshotJsonValue(1.25)).toBe(1.25) + expect(snapshotJsonValue(-0)).toBeUndefined() + expect(isJsonValue(-0)).toBe(false) + expect(snapshotJsonValue(Number.NaN)).toBeUndefined() + expect(snapshotJsonValue(Number.POSITIVE_INFINITY)).toBeUndefined() + expect(snapshotJsonValue(1n)).toBeUndefined() + expect(snapshotJsonValue(unsupportedFunction)).toBeUndefined() + expect(snapshotJsonValue(Symbol('value'))).toBeUndefined() + const unsupportedUndefined: unknown = undefined + expect(snapshotJsonValue(unsupportedUndefined)).toBeUndefined() + }) + + it('recursively detaches dense arrays and plain or null-prototype objects', () => { + const shared = { value: 1 } + const nullPrototype = Object.assign(Object.create(null) as Record, { shared }) + const source = { list: [nullPrototype, shared], alias: shared } + + const snapshot = snapshotJsonValue(source)! + shared.value = 2 + + expect(snapshot).toEqual({ list: [{ shared: { value: 1 } }, { value: 1 }], alias: { value: 1 } }) + expect(snapshot).not.toBe(source) + expect(snapshot.list).not.toBe(source.list) + expect(snapshot.alias).not.toBe(shared) + expect(snapshot.list[0]).not.toBe(nullPrototype) + expect(Object.getPrototypeOf(snapshot.list[0])).toBe(Object.prototype) + }) + + it('reads each object value and array slot once while materializing', () => { + class Exotic { + readonly accepted = false + } + let objectReads = 0 + let arrayReads = 0 + const nested = Object.defineProperty({}, 'value', { + enumerable: true, + get: () => { + objectReads += 1 + return objectReads === 1 ? { accepted: true } : new Exotic() + }, + }) + const array = new Array(1) + Object.defineProperty(array, 0, { + enumerable: true, + get: () => { + arrayReads += 1 + return arrayReads === 1 ? nested : new Exotic() + }, + }) + + expect(snapshotJsonValue(array)).toEqual([{ value: { accepted: true } }]) + expect(objectReads).toBe(1) + expect(arrayReads).toBe(1) + }) + + it('rejects exotic containers, sparse arrays, cycles, and invalid children', () => { + class ExoticObject { + readonly value = 1 + } + class ExoticArray extends Array {} + const sparse = new Array(1) + const cyclic: Record = {} + cyclic.self = cyclic + + expect(snapshotJsonValue(new ExoticObject())).toBeUndefined() + expect(snapshotJsonValue(new Map([['value', 1]]))).toBeUndefined() + expect(snapshotJsonValue(new ExoticArray(1))).toBeUndefined() + expect(snapshotJsonValue(sparse)).toBeUndefined() + expect(snapshotJsonValue(cyclic)).toBeUndefined() + expect(snapshotJsonValue([undefined])).toBeUndefined() + expect(snapshotJsonValue({ value: undefined })).toBeUndefined() + }) + + it('preserves a literal __proto__ JSON key without changing the snapshot prototype', () => { + const source = Object.create(null) as Record + source.__proto__ = { safe: true } + + const snapshot = snapshotJsonValue(source)! + + expect(Object.getPrototypeOf(snapshot)).toBe(Object.prototype) + expect(Object.prototype.hasOwnProperty.call(snapshot, '__proto__')).toBe(true) + expect(snapshot.__proto__).toEqual({ safe: true }) + }) + + it('propagates a throwing getter after reading it once', () => { + const failure = new Error('getter failed') + let reads = 0 + const source = Object.defineProperty({}, 'value', { + enumerable: true, + get: () => { + reads += 1 + throw failure + }, + }) + + expect(() => snapshotJsonValue(source)).toThrow(failure) + expect(reads).toBe(1) + }) +}) + +describe('isJsonValue', () => { + it('recognizes supported scalars and rejects every lossy scalar case', () => { + const unsupportedFunction = (): void => {} + const unsupportedUndefined: unknown = undefined + + expect(isJsonValue(null)).toBe(true) + expect(isJsonValue(false)).toBe(true) + expect(isJsonValue('text')).toBe(true) + expect(isJsonValue(1.25)).toBe(true) + expect(isJsonValue(-0)).toBe(false) + expect(isJsonValue(Number.NaN)).toBe(false) + expect(isJsonValue(1n)).toBe(false) + expect(isJsonValue(unsupportedFunction)).toBe(false) + expect(isJsonValue(Symbol('value'))).toBe(false) + expect(isJsonValue(unsupportedUndefined)).toBe(false) + }) + + it('accepts dense arrays and plain objects, including null-prototype records', () => { + const nullPrototype = Object.assign(Object.create(null) as Record, { value: true }) + + expect(isJsonValue([1, { nested: null }, nullPrototype])).toBe(true) + expect(isJsonValue({ value: [1, 2] })).toBe(true) + expect(isJsonValue(nullPrototype)).toBe(true) + }) + + it('rejects sparse arrays, invalid children, exotic objects, and cycles', () => { + class Exotic { + readonly value = 1 + } + class ExoticArray extends Array {} + const sparse = new Array(1) + const cyclic: Record = {} + cyclic.self = cyclic + + expect(isJsonValue(sparse)).toBe(false) + expect(isJsonValue(new ExoticArray(1))).toBe(false) + expect(isJsonValue([undefined])).toBe(false) + expect(isJsonValue({ value: undefined })).toBe(false) + expect(isJsonValue(new Exotic())).toBe(false) + expect(isJsonValue(cyclic)).toBe(false) + }) +}) diff --git a/packages/core/session/tests/scoped.spec.ts b/packages/core/session/tests/scoped.spec.ts new file mode 100644 index 0000000000..7a5e617254 --- /dev/null +++ b/packages/core/session/tests/scoped.spec.ts @@ -0,0 +1,180 @@ +import { describe, expect, it } from 'vitest' +import { Context } from 'cordis' +import { createScope, scopeOf } from '@deepseek-ai/dsh-scope' +import type { Scope, ScopeKey } from '@deepseek-ai/dsh-scope' +import SessionStore from '@deepseek-ai/dsh-session' +import type { Session } from '@deepseek-ai/dsh-session' + +async function mount(): Promise { + const ctx = new Context() + await ctx.plugin(SessionStore) + return ctx +} + +async function mintScope(ctx: Context, name: string): Promise { + let scope!: Scope + // The scoped context resolves services through the MINTING plugin's + // dependency chain — the minter must inject what scope holders will reach. + await ctx.plugin(Object.assign((inner: Context) => { scope = createScope(inner, { name }) }, + { inject: ['sessions'] })) + return scope +} + +/** The key a test scope was minted with. */ +function keyOf(scope: Scope): ScopeKey { + + return scopeOf(scope.ctx)! +} + +describe('session dispatch carriers', () => { + it('a session entered through a scoped context dispatches its events in that scope', async () => { + const ctx = await mount() + const scope = await mintScope(ctx, 'owner') + const otherScope = await mintScope(ctx, 'other') + + const heard: string[] = [] + ctx.on('session/event', (_session, event) => void heard.push(`global:${event.type}`)) + scope.ctx.on('session/event', (_session, event) => void heard.push(`owner:${event.type}`)) + otherScope.ctx.on('session/event', (_session, event) => void heard.push(`other:${event.type}`)) + scope.ctx.on('session/created', session => void heard.push(`owner-created:${session.id}`)) + otherScope.ctx.on('session/created', session => void heard.push(`other-created:${session.id}`)) + + const session = scope.ctx.sessions.create() + session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) + + expect(heard).toEqual([ + `owner-created:${session.id}`, + 'global:turn/start', + 'owner:turn/start', + ]) + }) + + it('a bare session dispatches subject-less: scoped listeners never hear it', async () => { + const ctx = await mount() + const scope = await mintScope(ctx, 'owner') + const heard: string[] = [] + ctx.on('session/event', (_s, event) => void heard.push(`global:${event.type}`)) + scope.ctx.on('session/event', (_s, event) => void heard.push(`owner:${event.type}`)) + + const bare = ctx.sessions.create() + bare.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) + expect(heard).toEqual(['global:turn/start']) + }) + + it('reuses the captured owner carrier for the paired disposal notification', async () => { + const ctx = await mount() + const owner = await mintScope(ctx, 'owner') + const other = await mintScope(ctx, 'other') + const heard: string[] = [] + ctx.on('session/disposed', (session) => { heard.push(`global:${session.id}`) }) + owner.ctx.on('session/disposed', (session) => { heard.push(`owner:${session.id}`) }) + other.ctx.on('session/disposed', (session) => { heard.push(`other:${session.id}`) }) + + const session = owner.ctx.sessions.prepare() + const detach = owner.ctx.sessions.enter(session) + owner.ctx.sessions.announce(session) + detach() + + expect(heard).toEqual([`global:${session.id}`, `owner:${session.id}`]) + }) +}) + +describe('sessions.flush()', () => { + it('dispatches session/flush with the owning carrier and awaits all listeners', async () => { + const ctx = await mount() + const scope = await mintScope(ctx, 'owner') + const flushed: string[] = [] + ctx.on('session/flush', async (session: Session) => { + await Promise.resolve() + flushed.push(`global:${session.id}`) + }) + scope.ctx.on('session/flush', (session: Session) => void flushed.push(`owner:${session.id}`)) + + const owned = scope.ctx.sessions.create() + const bare = ctx.sessions.create() + await ctx.sessions.flush(owned) + await ctx.sessions.flush(bare) + + // Parallel dispatch: listener completion order is unspecified (the global + // listener awaits a microtask) — assert set membership per flush instead. + expect(flushed.slice(0, 2).sort()).toEqual([`global:${owned.id}`, `owner:${owned.id}`]) + expect(flushed.slice(2)).toEqual([`global:${bare.id}`]) + }) + + it('propagates a rejecting flush listener (the caller owns the failure policy)', async () => { + const ctx = await mount() + ctx.on('session/flush', () => Promise.reject(new Error('disk full'))) + const session = ctx.sessions.create() + await expect(ctx.sessions.flush(session)).rejects.toThrow('disk full') + }) + + it('does not let a synchronous flush failure starve later listeners', async () => { + const ctx = await mount() + const flushed: Session[] = [] + ctx.on('session/flush', () => { throw new Error('disk full') }) + ctx.on('session/flush', (session) => { flushed.push(session) }) + const session = ctx.sessions.create() + + await expect(ctx.sessions.flush(session)).rejects.toThrow('disk full') + expect(flushed).toEqual([session]) + }) + + it('waits for slower flush listeners before reporting another listener failure', async () => { + const ctx = await mount() + const gate = Promise.withResolvers() + let slowStarted = false + let settled = false + ctx.on('session/flush', () => Promise.reject(new Error('disk full'))) + ctx.on('session/flush', () => { + slowStarted = true + return gate.promise + }) + const session = ctx.sessions.create() + + const flushing = ctx.sessions.flush(session) + void flushing.finally(() => { settled = true }).catch(() => undefined) + await Promise.resolve() + expect(slowStarted).toBe(true) + expect(settled).toBe(false) + + gate.resolve(undefined) + await expect(flushing).rejects.toThrow('disk full') + expect(settled).toBe(true) + }) + + it('rejects a never-entered session instead of inventing a carrier', async () => { + const ctx = await mount() + const scope = await mintScope(ctx, 'owner') + const flushed: string[] = [] + ctx.on('session/flush', (session: Session) => void flushed.push(`global:${session.id}`)) + scope.ctx.on('session/flush', (session: Session) => void flushed.push(`owner:${session.id}`)) + + const prepared = ctx.sessions.prepare() + await expect(ctx.sessions.flush(prepared)).rejects.toThrow(/not live/) + expect(flushed).toEqual([]) + }) + + it('clears a detached carrier and rejects stale flushes', async () => { + const ctx = await mount() + const scope = await mintScope(ctx, 'owner') + const flushed: string[] = [] + ctx.on('session/flush', (session: Session) => void flushed.push(`global:${session.id}`)) + scope.ctx.on('session/flush', (session: Session) => void flushed.push(`owner:${session.id}`)) + + const session = scope.ctx.sessions.prepare() + const detach = scope.ctx.sessions.enter(session) + await ctx.sessions.flush(session) + expect(flushed.sort()).toEqual([`global:${session.id}`, `owner:${session.id}`]) + + detach() + await expect(ctx.sessions.flush(session)).rejects.toThrow(/not live/) + expect(flushed).toHaveLength(2) + }) + + it('keyOf sanity: distinct scopes carry distinct keys', async () => { + const ctx = await mount() + const a = await mintScope(ctx, 'a') + const b = await mintScope(ctx, 'b') + expect(keyOf(a)).not.toBe(keyOf(b)) + }) +}) diff --git a/packages/core/session/tests/session.spec.ts b/packages/core/session/tests/session.spec.ts index f338b635f3..2204fc9027 100644 --- a/packages/core/session/tests/session.spec.ts +++ b/packages/core/session/tests/session.spec.ts @@ -1,8 +1,8 @@ -import { describe, expect, it } from 'vitest' +import { describe, expect, it, vi } from 'vitest' import { Context } from 'cordis' import { CallId } from '@deepseek-ai/dsh-llm' import SessionStore, { SESSION_FORMAT_VERSION, Session, SessionEvent, SessionId } from '@deepseek-ai/dsh-session' -import type { SessionEventType, TodoItem } from '@deepseek-ai/dsh-session' +import type { CreateSessionOptions, SessionEventType, SessionHeader, TodoItem } from '@deepseek-ai/dsh-session' describe('Session', () => { it('derives message history from the event log', () => { @@ -156,7 +156,7 @@ describe('Session', () => { const badSeed = [ { type: 'user/message' as const, seq: 0, time: 1, data: { content: [{ type: 'text' as const, text: 'x' }], source: { kind: 'user' as const }, bad: 1n } }, ] as unknown as SessionEvent[] - expect(() => new Session(SessionId('seed-bad'), badSeed)).toThrow(/non-JSON-serializable/) + expect(() => new Session(SessionId('seed-bad'), badSeed)).toThrow(/losslessly JSON-serializable/) }) it('validates seed events: rejects a non-contiguous seq', () => { @@ -177,7 +177,7 @@ describe('Session', () => { { type: 'user/message' as const, seq: 1, time: 2, data: { content: [{ type: 'text' as const, text: 'hi' }], source: { kind: 'user' as const } } }, { type: 'turn/end' as const, seq: 2, time: 3, data: { turn: 1, reason: { kind: 'completed' as const } } }, ] as SessionEvent[] - expect(() => new Session(SessionId('seed-no-marker'), markerlessSeed)).toThrow(/surface-eligible but carries no surfaceOp/) + expect(() => new Session(SessionId('seed-no-marker'), markerlessSeed)).toThrow(/requires a surfaceOp marker/) }) it('accepts a well-formed contiguous serializable seed', () => { @@ -190,6 +190,151 @@ describe('Session', () => { expect(session.events).toHaveLength(3) }) + it('reads each seed array entry once so validation and storage use the same event', () => { + const accepted = { + type: 'turn/start' as const, + seq: 0, + time: 1, + data: { turn: 1, trigger: { kind: 'message' as const, source: { kind: 'user' as const } } }, + } + const drifted = { ...accepted, seq: 99, data: { invalid: 1n } } + let reads = 0 + const seed = new Array(1) + Object.defineProperty(seed, 0, { + enumerable: true, + get: () => { + reads += 1 + return reads === 1 ? accepted : drifted + }, + }) + + const session = new Session(SessionId('seed-entry-snapshot'), seed) + + expect(reads).toBe(1) + expect(session.events).toEqual([accepted]) + }) + + it('reads a nested seed-data getter once and stores its first JSON value', () => { + let reads = 0 + const data = Object.defineProperty({}, 'value', { + enumerable: true, + get: () => { + reads += 1 + return reads === 1 ? 'accepted' : 1n + }, + }) + const seed = [{ type: 'test/unstable', seq: 0, time: 1, data }] as unknown as SessionEvent[] + + const session = new Session(SessionId('seed-nested-drift'), seed) + + expect(reads).toBe(1) + expect(session.events[0]!.data).toEqual({ value: 'accepted' }) + }) + + it('rejects non-JSON surface metadata in a seed event', () => { + const seed = [{ + type: 'user/message', + seq: 0, + time: 1, + data: { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } }, + surfaceOp: { op: 'replace', start: 1n, end: 2 }, + }] as unknown as SessionEvent[] + + expect(() => new Session(SessionId('seed-bad-metadata'), seed)) + .toThrow(/losslessly JSON-serializable/) + }) + + it('rejects exotic seed metadata before cloning can erase its prototype', () => { + class ReplaceOp { + readonly op = 'replace' as const + readonly start = 0 + readonly end = 0 + } + const seed = [{ + type: 'user/message', + seq: 0, + time: 1, + data: { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } }, + surfaceOp: new ReplaceOp(), + }] as unknown as SessionEvent[] + + expect(() => new Session(SessionId('seed-exotic-metadata'), seed)) + .toThrow(/losslessly JSON-serializable/) + }) + + it('rejects an exotic seed event shell before spreading erases its prototype', () => { + class SeedEvent { + readonly type = 'turn/start' as const + readonly seq = 0 + readonly time = 1 + readonly data = { turn: 1, trigger: { kind: 'message' as const, source: { kind: 'user' as const } } } + } + const seed: SessionEvent[] = [new SeedEvent()] + + expect(() => new Session(SessionId('seed-exotic-shell'), seed)) + .toThrow(/not losslessly JSON-serializable/) + }) + + it('accepts a null-prototype seed event shell as a plain JSON record', () => { + const event = Object.assign(Object.create(null) as Record, { + type: 'turn/start' as const, + seq: 0, + time: 1, + data: { turn: 1, trigger: { kind: 'message' as const, source: { kind: 'user' as const } } }, + }) as unknown as SessionEvent + + const session = new Session(SessionId('seed-null-prototype'), [event]) + + expect(session.events).toEqual([{ ...event }]) + }) + + it('reads a nested seed-metadata getter once and stores its first JSON value', () => { + let reads = 0 + const surfaceOp = Object.defineProperty({ op: 'replace', end: 0 }, 'start', { + enumerable: true, + get: () => { + reads += 1 + return reads === 1 ? 0 : 1n + }, + }) + const seed = [{ + type: 'user/message', + seq: 0, + time: 1, + data: { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } }, + surfaceOp, + }] as unknown as SessionEvent[] + + const session = new Session(SessionId('seed-unstable-metadata'), seed) + const event = session.events[0]! + if (event.type !== 'user/message') throw new Error('test fixture must remain a user/message') + + expect(reads).toBe(1) + expect(event.surfaceOp).toEqual({ op: 'replace', start: 0, end: 0 }) + }) + + it('adds seed context when surface validation throws a non-Error value', () => { + const originalHasOwn = Object.hasOwn + const hasOwn = vi.spyOn(Object, 'hasOwn').mockImplementation((object: object, property: PropertyKey): boolean => { + if ((object as Record)['op'] === 'replace') throw 'validator failed' + return originalHasOwn(object, property) + }) + const seed = [{ + type: 'user/message', + seq: 0, + time: 1, + data: { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } }, + surfaceOp: { op: 'replace', start: 0, end: 0 }, + }] as unknown as SessionEvent[] + + try { + expect(() => new Session(SessionId('seed-non-error-metadata-failure'), seed)) + .toThrow('invalid seed event at index 0: invalid surface metadata') + } finally { + hasOwn.mockRestore() + } + }) + it('snapshots the seed: mutating the original after construction does not affect session.events', () => { const seed = [ { type: 'turn/start' as const, seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message' as const, source: { kind: 'user' as const } } } }, @@ -222,6 +367,261 @@ describe('Session', () => { // The returned event carries the same snapshot, not the caller's input. expect((event.data.content[0] as { text: string }).text).toBe('original') }) + + it('reads a nested append-data getter once and stores its first JSON value', () => { + const session = new Session(SessionId('append-nested-drift')) + let reads = 0 + const data = Object.defineProperty({}, 'value', { + enumerable: true, + get: () => { + reads += 1 + return reads === 1 ? 'accepted' : 1n + }, + }) + + const event = session.append('todo/write', data as never) + + expect(reads).toBe(1) + expect(event.data).toEqual({ value: 'accepted' }) + expect(session.events).toEqual([event]) + }) + + it('rejects non-JSON surface metadata before appending the event', () => { + const session = new Session(SessionId('append-bad-metadata')) + + expect(() => session.append( + 'user/message', + { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } }, + { surfaceOp: { op: 'replace', start: 1n, end: 2 } } as never, + )).toThrow(/non-JSON-serializable surface metadata/) + expect(session.events).toEqual([]) + }) + + it('rejects exotic surface metadata before cloning can erase its prototype', () => { + class ReplaceOp { + readonly op = 'replace' as const + readonly start = 0 + readonly end = 0 + } + const session = new Session(SessionId('append-exotic-metadata')) + + expect(() => session.append( + 'user/message', + { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } }, + { surfaceOp: new ReplaceOp() }, + )).toThrow(/non-JSON-serializable surface metadata/) + expect(session.events).toEqual([]) + }) + + it('reads a nested append-metadata getter once and stores its first JSON value', () => { + const session = new Session(SessionId('append-unstable-metadata')) + let reads = 0 + const surfaceOp = Object.defineProperty({ op: 'replace', end: 0 }, 'start', { + enumerable: true, + get: () => { + reads += 1 + return reads === 1 ? 0 : 1n + }, + }) + + const event = session.append( + 'user/message', + { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } }, + { surfaceOp } as never, + ) + + expect(reads).toBe(1) + expect(event.surfaceOp).toEqual({ op: 'replace', start: 0, end: 0 }) + expect(session.events).toEqual([event]) + }) + + it('rejects invalid plain surface metadata shapes at append', () => { + const session = new Session(SessionId('append-invalid-surface-shape')) + const appendRaw = session.append.bind(session) as unknown as ( + type: SessionEventType, + data: unknown, + opts?: unknown, + ) => SessionEvent + const data = { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } } + + expect(() => appendRaw('user/message', data, { surfaceOp: 'invalid' })) + .toThrow(/invalid surfaceOp/) + expect(() => appendRaw('user/message', data, { + surfaceOp: { op: 'replace', start: -1, end: 0 }, + })).toThrow(/invalid replace surfaceOp/) + expect(() => appendRaw('user/message', data, { + surfaceOp: 'append', + sourceEventSeqs: [0, -1], + })).toThrow(/non-negative safe integers/) + expect(session.events).toEqual([]) + }) + + it('rejects surface metadata on non-surface append and seed events', () => { + const session = new Session(SessionId('non-surface-metadata')) + const appendRaw = session.append.bind(session) as unknown as ( + type: SessionEventType, + data: unknown, + opts?: unknown, + ) => SessionEvent + + expect(() => appendRaw( + 'turn/start', + { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }, + { surfaceOp: 'append' }, + )).toThrow(/not surface-eligible and cannot carry surface metadata/) + expect(() => new Session(SessionId('non-surface-metadata-seed'), [{ + type: 'turn/start', + seq: 0, + time: 1, + data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }, + surfaceOp: 'append', + } as unknown as SessionEvent])).toThrow(/invalid seed event.*not surface-eligible/) + expect(session.events).toEqual([]) + }) + + it('deep-freezes seeded and appended event snapshots', () => { + const seeded = new Session(SessionId('seed-frozen'), [{ + type: 'turn/start', + seq: 0, + time: 1, + data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }, + }]) + const seededEvent = seeded.events[0]! + if (seededEvent.type !== 'turn/start') throw new Error('test fixture must remain a turn/start') + expect(Object.isFrozen(seededEvent)).toBe(true) + expect(Object.isFrozen(seededEvent.data)).toBe(true) + expect(Object.isFrozen(seededEvent.data.trigger)).toBe(true) + expect(() => { seededEvent.data.turn = 99 }).toThrow(TypeError) + + const appended = new Session(SessionId('append-frozen')) + const appendedEvent = appended.append('todo/write', { + todos: [{ content: 'first', status: 'pending' }], + }) + expect(Object.isFrozen(appendedEvent)).toBe(true) + expect(Object.isFrozen(appendedEvent.data)).toBe(true) + expect(Object.isFrozen(appendedEvent.data.todos)).toBe(true) + expect(Object.isFrozen(appendedEvent.data.todos[0])).toBe(true) + expect(() => { appendedEvent.data.todos[0]!.content = 'mutated' }).toThrow(TypeError) + }) + + it('returns cached frozen event-array snapshots that do not grow after append', () => { + const session = new Session(SessionId('events-snapshot')) + session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) + const before = session.events + const beforeEvent = before[0]! + if (beforeEvent.type !== 'turn/start') throw new Error('test fixture must remain a turn/start') + + expect(session.events).toBe(before) + expect(Object.isFrozen(before)).toBe(true) + expect(() => { (before as SessionEvent[]).push(beforeEvent) }).toThrow(TypeError) + expect(() => { beforeEvent.data.turn = 99 }).toThrow(TypeError) + + session.append('turn/end', { turn: 1, reason: { kind: 'completed' } }) + const after = session.events + expect(before).toHaveLength(1) + expect(after).toHaveLength(2) + expect(after).not.toBe(before) + expect(session.events).toBe(after) + }) + + it('detaches and freezes an explicitly supplied session header', () => { + const input = { + version: SESSION_FORMAT_VERSION, + id: SessionId('header-owned'), + createdAt: 123, + cwd: '/accepted', + parentSession: SessionId('parent'), + seedLength: 2, + } + + const session = new Session(SessionId('header-owned'), undefined, input) + input.cwd = '/caller-mutated' + + expect(session.header).toEqual({ + version: SESSION_FORMAT_VERSION, + id: 'header-owned', + createdAt: 123, + cwd: '/accepted', + parentSession: 'parent', + seedLength: 2, + }) + expect(session.header).not.toBe(input) + expect(Object.isFrozen(session.header)).toBe(true) + expect(Reflect.set(session.header, 'cwd', '/published-mutated')).toBe(false) + expect(session.id).toBe('header-owned') + expect(session.header.cwd).toBe('/accepted') + }) + + it('rejects an exotic, non-JSON, or mismatched supplied header', () => { + class ExoticHeader implements SessionHeader { + readonly version = SESSION_FORMAT_VERSION + readonly id = SessionId('header-invalid') + readonly createdAt = 123 + } + + expect(() => new Session(SessionId('header-invalid'), undefined, new ExoticHeader())) + .toThrow(/not losslessly JSON-serializable/) + expect(() => new Session(SessionId('header-invalid'), undefined, { + version: SESSION_FORMAT_VERSION, + id: SessionId('header-invalid'), + createdAt: 123, + parentSession: 1n, + } as unknown as SessionHeader)).toThrow(/not losslessly JSON-serializable/) + expect(() => new Session(SessionId('header-invalid'), undefined, { + version: SESSION_FORMAT_VERSION, + id: SessionId('other'), + createdAt: 123, + })).toThrow(/does not match session id/) + }) + + it('rejects invalid scalar fields in an explicitly supplied header', () => { + const base = { + version: SESSION_FORMAT_VERSION, + id: SessionId('header-shape'), + createdAt: 123, + } + const cases: Array<{ header: unknown; error: RegExp }> = [ + { header: 1, error: /not a plain JSON record/ }, + { header: null, error: /not a plain JSON record/ }, + { header: { ...base, version: 1 }, error: /header version/ }, + { header: { ...base, createdAt: '123' }, error: /createdAt must be a finite number/ }, + { header: { ...base, cwd: 1 }, error: /header cwd must be a string/ }, + { header: { ...base, cwd: 'relative' }, error: /header cwd must be an absolute path/ }, + { header: { ...base, parentSession: 1 }, error: /header parentSession must be a string/ }, + { header: { ...base, seedLength: '1' }, error: /seedLength must be a non-negative safe integer/ }, + { header: { ...base, seedLength: 0.5 }, error: /seedLength must be a non-negative safe integer/ }, + { header: { ...base, seedLength: -1 }, error: /seedLength must be a non-negative safe integer/ }, + ] + + for (const { header, error } of cases) { + expect(() => new Session(SessionId('header-shape'), undefined, header as SessionHeader)).toThrow(error) + } + }) + + it('rejects seed records with invalid fixed-envelope fields', () => { + const base = { + type: 'turn/start', + seq: 0, + time: 1, + data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }, + } + const cases: unknown[] = [ + { ...base, extra: true }, + { ...base, type: 1 }, + { ...base, seq: '0' }, + { ...base, seq: 0.5 }, + { ...base, seq: -1 }, + { ...base, time: '1' }, + { ...base, time: 0.5 }, + { ...base, time: -1 }, + { type: base.type, seq: base.seq, time: base.time }, + ] + + for (const [index, event] of cases.entries()) { + expect(() => new Session(SessionId(`bad-envelope-${index}`), [event as SessionEvent])) + .toThrow(/invalid event envelope/) + } + }) }) @@ -238,6 +638,10 @@ describe('SessionStore', () => { const session = ctx.sessions.create() expect(created).toEqual([session]) + // The store-owned append publication hooks are module-private. A JavaScript caller + // may create an unrelated property with the old implementation's name, + // but cannot suppress the durable event feed. + expect(Reflect.set(session, 'onAppend', undefined)).toBe(true) session.append('user/message', { content: [{ type: 'text', text: 'x' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) expect(events).toHaveLength(1) expect(events[0]![0]).toBe(session) @@ -289,9 +693,97 @@ describe('SessionStore', () => { expect(created).toEqual([session]) // The detach disposer removes the entry + stops notification. detach() + detach() // idempotent: cannot disturb a later same-id lifecycle expect(ctx.sessions.get(SessionId('lifecycle'))).toBeUndefined() }) + it('prevents simultaneous attachment of one session object to two stores', async () => { + const firstCtx = new Context() + const secondCtx = new Context() + await firstCtx.plugin(SessionStore) + await secondCtx.plugin(SessionStore) + const session = new Session(SessionId('owned-key')) + const detachFirst = firstCtx.sessions.enter(session) + + expect(() => secondCtx.sessions.enter(session)).toThrow(/already attached to a store/) + expect(firstCtx.sessions.get(SessionId('owned-key'))).toBe(session) + + detachFirst() + expect(firstCtx.sessions.get(SessionId('owned-key'))).toBeUndefined() + const detachSecond = secondCtx.sessions.enter(session) + expect(secondCtx.sessions.get(SessionId('owned-key'))).toBe(session) + detachSecond() + + }) + + it('rejects direct and reentrant repeat announcements to preserve one lifecycle pair', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + let created = 0 + let disposed = 0 + let reentrantError = '' + ctx.on('session/created', (session) => { + created += 1 + try { + ctx.sessions.announce(session) + } catch (error: unknown) { + reentrantError = String(error) + } + }) + ctx.on('session/disposed', () => { disposed += 1 }) + + const session = ctx.sessions.prepare(SessionId('once')) + const detach = ctx.sessions.enter(session) + ctx.sessions.announce(session) + expect(reentrantError).toMatch(/already announced/) + expect(() => { ctx.sessions.announce(session) }).toThrow(/already announced/) + detach() + expect({ created, disposed }).toEqual({ created: 1, disposed: 1 }) + }) + + it('defers a reentrant detach until the creation dispatch unwinds', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + const order: string[] = [] + const session = ctx.sessions.prepare(SessionId('reentrant-detach')) + const detach = ctx.sessions.enter(session) + + ctx.on('session/created', (created) => { + order.push('created:first') + detach() + expect(ctx.sessions.get(created.id)).toBe(created) + }) + ctx.on('session/created', (created) => { + order.push('created:second') + expect(ctx.sessions.get(created.id)).toBe(created) + }) + ctx.on('session/disposed', (disposed) => { + order.push('disposed') + expect(ctx.sessions.get(disposed.id)).toBeUndefined() + }) + + ctx.sessions.announce(session) + + expect(order).toEqual(['created:first', 'created:second', 'disposed']) + expect(ctx.sessions.get(session.id)).toBeUndefined() + detach() + }) + + it('rolls back create when its owner unloads from session/created', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + let ownerCtx!: Context + const owner = await ctx.plugin(Object.assign((inner: Context) => { ownerCtx = inner }, { inject: ['sessions'] })) + const id = SessionId('create-unload-race') + ctx.on('session/created', (session) => { + if (session.id === id) void owner.dispose() + }) + + ownerCtx.sessions.create(id) + await owner.dispose() + expect(ctx.sessions.get(id)).toBeUndefined() + }) + it('synthesizes a minimal current-version header for a bare-created session', async () => { const ctx = new Context() await ctx.plugin(SessionStore) @@ -316,6 +808,26 @@ describe('SessionStore', () => { }) }) + it('rejects non-JSON and invalid scalar session metadata', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + const cases: Array<{ meta: unknown; error: RegExp }> = [ + { meta: { parentSession: 1n }, error: /header is not losslessly JSON-serializable/ }, + { meta: { cwd: 1 }, error: /header cwd must be a string/ }, + { meta: { parentSession: 1 }, error: /header parentSession must be a string/ }, + { meta: { createdAt: '123' }, error: /header createdAt must be a finite number/ }, + { meta: { seedLength: '1' }, error: /seedLength must be a non-negative safe integer/ }, + { meta: { seedLength: 0.5 }, error: /seedLength must be a non-negative safe integer/ }, + { meta: { seedLength: -1 }, error: /seedLength must be a non-negative safe integer/ }, + ] + + for (const [index, { meta, error }] of cases.entries()) { + expect(() => ctx.sessions.prepare(SessionId(`bad-meta-${index}`), { + meta: meta as NonNullable, + })).toThrow(error) + } + }) + it('rejects a non-absolute meta.cwd', async () => { const ctx = new Context() await ctx.plugin(SessionStore) @@ -350,11 +862,13 @@ describe('SessionStore', () => { expect(observed).toBe(0) }) - it('rolls back the session (and onAppend) when a session/created listener throws (P1-1)', async () => { + it('pairs a partial session/created announcement with disposal during rollback', async () => { const ctx = new Context() await ctx.plugin(SessionStore) let threw = false + const disposed: Session[] = [] + ctx.on('session/disposed', (session) => { disposed.push(session) }) ctx.on('session/created', () => { if (!threw) { threw = true; throw new Error('boom created listener') } }) @@ -362,9 +876,10 @@ describe('SessionStore', () => { // The throwing emit must roll the store entry back, not leak it. expect(() => ctx.sessions.create(SessionId('fixed'))).toThrow('boom created listener') expect(ctx.sessions.get(SessionId('fixed'))).toBeUndefined() // rolled back, not leaked + expect(disposed.map(session => session.id)).toEqual(['fixed']) // A subsequent create of the SAME id succeeds (the already-exists check is - // not wedged) and its onAppend is correctly wired (events observable). + // not wedged) and its store-owned publication hooks are correctly wired. const events: SessionEvent[] = [] ctx.on('session/event', (_session, event) => void events.push(event)) const session = ctx.sessions.create(SessionId('fixed')) @@ -372,6 +887,243 @@ describe('SessionStore', () => { session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) expect(events).toHaveLength(1) }) + + it('contains session/event observer failures after the append commit point', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + const warnings: string[] = [] + ctx.logger.warn = ((message: unknown) => { warnings.push(String(message)) }) as typeof ctx.logger.warn + const session = ctx.sessions.create(SessionId('contained-event')) + const heard: SessionEvent[] = [] + let committedBeforeNotify = false + ctx.on('session/event', (observedSession, event) => { + committedBeforeNotify = observedSession.events.at(-1) === event + throw new Error('sync event observer') + }) + ctx.on('session/event', () => Promise.reject(new Error('async event observer')) as never) + ctx.on('session/event', (_observedSession, event) => { heard.push(event) }) + + let appended!: SessionEvent + expect(() => { + appended = session.append('turn/start', { + turn: 1, + trigger: { kind: 'message', source: { kind: 'user' } }, + }) + }).not.toThrow() + expect(committedBeforeNotify).toBe(true) + expect(session.events).toEqual([appended]) + expect(heard).toEqual([appended]) + await Promise.resolve() + await Promise.resolve() + + expect(warnings).toEqual([ + 'session "contained-event": session/event listener threw: Error: sync event observer', + 'session "contained-event": session/event listener rejected: Error: async event observer', + ]) + }) + + it('runs internal dispatch validation on one frozen candidate before commit and resets after a veto', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + const session = ctx.sessions.create(SessionId('dispatch-veto')) + const validations: Array<{ event: SessionEvent; logLength: number; frozen: boolean }> = [] + const observed: SessionEvent[] = [] + let reject = true + ctx.on('internal/dispatch', (_mode, name, args) => { + if (name !== 'session/event') return + const [observedSession, event] = args as [Session, SessionEvent] + validations.push({ + event, + logLength: observedSession.events.length, + frozen: Object.isFrozen(event) && Object.isFrozen(event.data), + }) + if (reject) { + reject = false + throw new Error('reject first candidate') + } + }) + ctx.on('session/event', (_observedSession, event) => { observed.push(event) }) + + expect(() => session.append('turn/start', { + turn: 1, + trigger: { kind: 'message', source: { kind: 'user' } }, + })).toThrow('reject first candidate') + expect(session.events).toEqual([]) + expect(observed).toEqual([]) + + const appended = session.append('turn/start', { + turn: 1, + trigger: { kind: 'message', source: { kind: 'user' } }, + }) + expect(validations.map(({ logLength, frozen }) => ({ logLength, frozen }))).toEqual([ + { logLength: 0, frozen: true }, + { logLength: 0, frozen: true }, + ]) + expect(validations.map(({ event }) => event.seq)).toEqual([0, 0]) + expect(validations[1]!.event).toBe(appended) + expect(session.events).toEqual([appended]) + expect(observed).toEqual([appended]) + }) + + it('resolves session/event dispatch before commit so instrumentation failure cannot hide a logged event', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + const session = ctx.sessions.create(SessionId('dispatch-check')) + const observed: SessionEvent[] = [] + ctx.on('internal/dispatch', (_mode, name) => { + if (name === 'session/event') throw new Error('dispatch instrumentation rejected the carrier') + }) + ctx.on('session/event', (_observedSession, event) => { observed.push(event) }) + + expect(() => session.append('turn/start', { + turn: 1, + trigger: { kind: 'message', source: { kind: 'user' } }, + })).toThrow('dispatch instrumentation rejected the carrier') + expect(session.events).toEqual([]) + expect(observed).toEqual([]) + }) + + it('contains a reentrant observer append without reordering later observers', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + const warnings: string[] = [] + ctx.logger.warn = ((message: unknown) => { warnings.push(String(message)) }) as typeof ctx.logger.warn + const session = ctx.sessions.create(SessionId('reentrant-observer')) + const heard: SessionEvent[] = [] + ctx.on('session/event', (observedSession) => { + observedSession.append('todo/write', { todos: [] }) + }) + ctx.on('session/event', (_observedSession, event) => { heard.push(event) }) + + const appended = session.append('turn/start', { + turn: 1, + trigger: { kind: 'message', source: { kind: 'user' } }, + }) + expect(session.events).toEqual([appended]) + expect(heard).toEqual([appended]) + expect(warnings).toEqual([ + 'session "reentrant-observer": session/event listener threw: Error: session append cannot reenter while another append is being published', + ]) + }) + + it('defers detach through dispatch resolution, commit, and observer publication', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + const order: string[] = [] + const session = ctx.sessions.prepare(SessionId('detach-during-append')) + const detach = ctx.sessions.enter(session) + ctx.on('internal/dispatch', (_mode, name, args) => { + if (name !== 'session/event') return + const session = args[0] as Session + order.push(`resolve:${ctx.sessions.get(session.id) === session ? 'live' : 'detached'}`) + detach() + }) + ctx.on('session/event', (session) => { + order.push(`observe:${ctx.sessions.get(session.id) === session ? 'live' : 'detached'}`) + }) + ctx.on('session/disposed', (session) => { + order.push(`dispose:${ctx.sessions.get(session.id) === session ? 'live' : 'detached'}`) + }) + ctx.sessions.announce(session) + + const appended = session.append('turn/start', { + turn: 1, + trigger: { kind: 'message', source: { kind: 'user' } }, + }) + + expect(session.events).toEqual([appended]) + expect(order).toEqual(['resolve:live', 'observe:live', 'dispose:detached']) + expect(ctx.sessions.get(session.id)).toBeUndefined() + }) + + it('observes async session/created rejection without rolling back or starving peers', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + const warnings: string[] = [] + ctx.logger.warn = ((message: unknown) => { warnings.push(String(message)) }) as typeof ctx.logger.warn + const heard: string[] = [] + ctx.on('session/created', () => Promise.reject(new Error('late creation failure')) as never) + ctx.on('session/created', (session) => { heard.push(session.id) }) + + const session = ctx.sessions.create(SessionId('async-created')) + await Promise.resolve() + await Promise.resolve() + + expect(ctx.sessions.get(session.id)).toBe(session) + expect(heard).toEqual(['async-created']) + expect(warnings).toEqual([ + 'session "async-created": session/created listener rejected: Error: late creation failure', + ]) + }) + + it('contains synchronous and async session/disposed listener failures per observer', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + const warnings: string[] = [] + ctx.logger.warn = ((message: unknown) => { warnings.push(String(message)) }) as typeof ctx.logger.warn + const heard: string[] = [] + ctx.on('session/disposed', () => { throw new Error('sync disposed') }) + ctx.on('session/disposed', () => Promise.reject(new Error('async disposed')) as never) + ctx.on('session/disposed', (session) => { heard.push(session.id) }) + + const unannounced = ctx.sessions.prepare(SessionId('never-announced')) + const detachUnannounced = ctx.sessions.enter(unannounced) + detachUnannounced() + expect(heard).toEqual([]) + + const announced = ctx.sessions.prepare(SessionId('contained-disposal')) + const detach = ctx.sessions.enter(announced) + ctx.sessions.announce(announced) + expect(() => { detach() }).not.toThrow() + await Promise.resolve() + await Promise.resolve() + + expect(heard).toEqual(['contained-disposal']) + expect(warnings).toEqual([ + 'session "contained-disposal": session/disposed listener threw: Error: sync disposed', + 'session "contained-disposal": session/disposed listener rejected: Error: async disposed', + ]) + }) + + it('contains internal dispatch failure after session detachment', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + const warnings: string[] = [] + ctx.logger.warn = ((message: unknown) => { warnings.push(String(message)) }) as typeof ctx.logger.warn + const heard: Session[] = [] + ctx.on('internal/dispatch', (_mode, name) => { + if (name === 'session/disposed') throw new Error('disposed dispatch instrumentation') + }) + ctx.on('session/disposed', (session) => { heard.push(session) }) + const session = ctx.sessions.prepare(SessionId('disposed-dispatch')) + const detach = ctx.sessions.enter(session) + ctx.sessions.announce(session) + + expect(() => { detach() }).not.toThrow() + expect(ctx.sessions.get(session.id)).toBeUndefined() + expect(heard).toEqual([]) + expect(warnings).toEqual([ + 'session "disposed-dispatch": session/disposed dispatch threw: Error: disposed dispatch instrumentation', + ]) + }) + + it('does not let internal dispatch replace the disposed callback tuple', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + const replacement = new Session(SessionId('replacement-disposed')) + const heard: Session[] = [] + ctx.on('internal/dispatch', (_mode, name, args) => { + if (name === 'session/disposed') args[0] = replacement + }) + ctx.on('session/disposed', (session) => { heard.push(session) }) + const session = ctx.sessions.prepare(SessionId('fixed-disposed-tuple')) + const detach = ctx.sessions.enter(session) + ctx.sessions.announce(session) + + detach() + + expect(heard).toEqual([session]) + }) }) describe('todo/write event', () => { diff --git a/packages/core/session/tsconfig.json b/packages/core/session/tsconfig.json index 7ca1556695..b19b98c5ad 100644 --- a/packages/core/session/tsconfig.json +++ b/packages/core/session/tsconfig.json @@ -19,6 +19,9 @@ }, { "path": "../../llm/llm" + }, + { + "path": "../../core/scope" } ] } diff --git a/packages/core/system-prompt/README.md b/packages/core/system-prompt/README.md index 704cd8e80f..dc1101faaf 100644 --- a/packages/core/system-prompt/README.md +++ b/packages/core/system-prompt/README.md @@ -1,34 +1,31 @@ # dsh-system-prompt -System prompt assembly registry. Plugins contribute ordered text sections, tool-schema providers, and named prompt variables; the agent loop calls `assemble(context)` once per step, and `renderPrompt(assembly)` is the full system prompt the model sees. The plugin registers the harness-owned openers itself — the static `harness:identity` section and the deployment's `deployment:persona` section — so they exist for every agent regardless of which loop plugin drives it. +System prompt assembly registry. Plugins contribute ordered text sections, tool-schema providers, and named prompt variables. The agent loop calls `assemble(context)` once per step, and `renderPrompt(assembly)` is the full system prompt the model sees. The plugin registers the harness-owned openers itself — the static `harness:identity` section and the global default `deployment:persona` section — so they remain available regardless of which loop plugin drives an agent. An agent-scoped contribution with the same persona name shadows that default for its agent. ## Config | Key | Default | Meaning | |---|---|---| -| `persona` | `''` | The deployment persona: the ONE deployment-authored prompt fragment, rendered as the order-0 `deployment:persona` section and shared by every agent in the context (subagents included). A template — complete `{{…}}` groups are interpreted strictly against the registered variables (the shipped loop registers `{{model}}`/`{{cwd}}`), with no escape syntax for literal braces yet. Empty ⇒ the section is dropped at render. | +| `persona` | `''` | The global deployment-persona default: the ONE config-authored prompt fragment, rendered as the order-0 `deployment:persona` section unless an agent-scoped contribution shadows it. A template — complete `{{…}}` groups are interpreted strictly against the registered variables (the shipped loop registers `{{model}}`/`{{cwd}}`), with no escape syntax for literal braces yet. Empty ⇒ the section is dropped at render. | | `toolOrder` | — | Explicit model-facing tool order, as a list of `ToolSchema.name`s with one `''` rest entry (`TOOL_ORDER_REST`): listed tools take their listed position, unlisted tools land at the rest entry in lexicographic name order. Absent ⇒ plain lexicographic name order. Applied to the collected tools BEFORE the `system-prompt/assemble` waterfall — like the sections' `order` sort, it canonicalizes what the registry contributed (registration order is a plugin-load artifact), and a waterfall listener that mutates the list owns the determinism of what it emits. Misconfiguration fails loud: a list without exactly one rest entry, or with duplicates, throws at load; a listed name with no registered tool rejects every `assemble()`; a tool provider returning the reserved rest-entry name also rejects. Under the shipped loop the turn fails before any model request. Why a central list and not per-plugin weights: [Explicit model-facing tool order](../../../docs/rfc/implemented/feature/2026-07-06-explicit-tool-order.md). | ## Service: `SystemPrompt` (ctx key: `systemPrompt`) ### Public API -- `ctx.systemPrompt.section(section: PromptSection): () => void` Contribute a section. Duplicate names throw. Disposed with the calling fiber. -- `ctx.systemPrompt.tools(provider: () => ToolSchema[]): () => void` Contribute tool schemas (evaluated at each assembly). A provider must not return a schema named `TOOL_ORDER_REST`; that name is reserved for `toolOrder`'s rest entry. Disposed with the calling fiber. -- `ctx.systemPrompt.variable(name: string, provider: (context) => string | undefined): () => void` Contribute a prompt variable, referenced from section text as `{{name}}`. Duplicate or unreferenceable names throw; `undefined` means "no value for this assembly". Disposed with the calling fiber. -- `ctx.systemPrompt.assemble(context?: AssembleContext): Promise` Assemble the prompt for one caller. Runs through the `system-prompt/assemble` waterfall. Rejects when a configured `toolOrder` names a tool no provider contributed, or when a provider returns the reserved rest-entry name. +- `ctx.systemPrompt.section(section: PromptSection): () => void` Contribute a section. The layer is the calling context's scope: `agent.ctx` contributes to that agent alone, shadowing a same-named global section there. Duplicate names within one layer and non-finite orders throw. Disposed with the calling fiber. +- `ctx.systemPrompt.tools(provider: (context: AssembleContext) => ToolProviderResult): () => void` Contribute tool schemas, evaluated at each assembly with that assembly's context. `ToolProviderResult` = `{ schemas, knownNames? }`: `schemas` is the post-restriction visible set; `knownNames` is the pre-restriction universe used by `toolOrder`. A provider must not return a schema named `TOOL_ORDER_REST`. Scoped providers are consulted only for their scope's assemblies. Disposed with the calling fiber. +- `ctx.systemPrompt.variable(name: string, provider: (context) => string | undefined): () => void` Contribute a prompt variable, referenced from section text as `{{name}}`. Scoped variables shadow a same-named global for that agent. Duplicate-in-layer or unreferenceable names throw; `undefined` means "no value for this assembly". Disposed with the calling fiber. +- `ctx.systemPrompt.assemble(context?: AssembleContext): Promise` Assemble the prompt for one caller: the global layer merged with `context.scope`'s layer, with tool schemas detached before the transform seam. Runs through the scope-filtered `system-prompt/assemble` waterfall and returns its authoritative result. Rejects when a configured `toolOrder` names a tool outside the providers' `knownNames` universe, or when a provider returns the reserved rest-entry name. -### Events +### Live events -| Event | Mode | Purpose | -|---|---|---| -| `system-prompt/assemble` | waterfall | Mutate/extend the assembly (with the caller's context) before it reaches the model | -| `system-prompt/change` | emit | A section, tool provider, or variable was registered or unregistered | +`system-prompt/assemble` is an expert cooperative seam: its returned assembly is authoritative, and a listener that replaces or removes entries owns preserving any active Code Mode or structured-output protocol. Prefer [`ToolRegistry.restrict()`](../tools/README.md) when tool filtering must stay aligned across model presentation, lookup, and execution. Registry change is the deliberately unfiltered notification that an assembly input changed, possibly for one scope; exact signatures, dispatch modes, and filtering contracts live in the generated [Cordis event catalog](../../../docs/cordis-catalog/events.md). ### Key types -- `AssembleContext` — what one `assemble()` call is FOR. Declared empty here and merge-extensible; `dsh-agent` declares `agent?: Agent`, so providers project per-agent facts. Providers must tolerate absent fields (a bare `assemble()` carries an empty context). -- `PromptSection` — `{ name, order, text: string | ((context) => string) }`. Sections are concatenated in ascending `order`. Order bands: `-100` is the harness identity, `0` the deployment persona (both registered by this plugin), tool guidance uses `100–199`; other negative orders also render before the persona. +- `AssembleContext` — what one `assemble()` call is FOR. Merge-extensible; declares `scope?: ScopeKey` (the layer selector) here, and `dsh-agent` declares `agent?: Agent` (the typed DX field — never set without `scope`; use `assembleContextFor(agent)`). Providers must tolerate absent fields (a bare `assemble()` carries an empty, scope-less context). +- `PromptSection` — `{ name, order, text }`. Sections are concatenated in ascending `order`. Order bands: `-100` is the harness identity, `0` the deployment persona, tool guidance uses `100–199`. - `PromptAssembly` — `{ sections: AssembledSection[], tools: ToolSchema[], variables: Record }`. Section texts arrive resolved but not yet interpolated; `variables` holds every registered variable resolved against the context. Tool schemas are part of the assembly by design: "what the model is told it can do" is one coherent thing, even though adapters transmit schemas as a separate wire field. - `renderPrompt(assembly)` — interpolates `{{variable}}` references in each section, drops empty sections, joins with blank lines. STRICT: an unknown reference (`Object.hasOwn` lookup — prototype names like `{{constructor}}` are unknown), a registered-but-valueless reference, a malformed complete `{{…}}` group, or a `{{` that opens no complete group while a `}}` still follows (`{{{model}}}`) throws — fail loud beats shipping a malformed prompt. A lone `{{` with no `}}` anywhere after it passes through verbatim; substituted values are never re-scanned. @@ -39,11 +36,11 @@ Merge-extensible: plugins can declare extra fields on `PromptAssembly` and `Asse - Section providers: tool packages own their cross-call guidance (`tool:bash`, `tool:read`, …); this plugin owns `harness:identity` and `deployment:persona`. - Variable providers: the agent loop registers `model` and `cwd`; any plugin can register the facts it owns (a future `date`, git state, …). - Tool schema providers: `ToolRegistry` registers itself as a tool provider automatically. -- The `system-prompt/assemble` waterfall: mutate or replace the assembly per caller (dynamic tool filtering, extra variables). +- The [`system-prompt/assemble` waterfall](#live-events): cooperatively mutate or replace the assembly per caller. ### What is NOT here -- Any deployment-authored prompt text outside config — the persona is this plugin's `persona` config, and every other section comes from the plugin that owns the fact. (The `harness:identity` line is deliberately a code literal: a harness fact, not a deployment choice; the `system-prompt/assemble` waterfall is the escape valve for a deployment that must drop it.) +- Any end-user prompt-editing API — this plugin owns the config-authored global persona default, creator plugins may register agent-scoped shadows during setup, and every other section comes from the plugin that owns the fact. (The `harness:identity` line is deliberately a code literal: a harness fact, not a deployment choice; the `system-prompt/assemble` waterfall is the escape valve for a deployment that must drop it.) - Prompt compaction (belongs on the `agent/pre-step` seam in `dsh-agent`). Design rationale: [the prompt-variables RFC](../../../docs/rfc/implemented/architecture/2026-07-05-prompt-variables-and-tool-guidance-ownership.md). diff --git a/packages/core/system-prompt/package.json b/packages/core/system-prompt/package.json index d97a7b8538..120e10ef11 100644 --- a/packages/core/system-prompt/package.json +++ b/packages/core/system-prompt/package.json @@ -23,6 +23,7 @@ "license": "BSD-3-Clause", "peerDependencies": { "@deepseek-ai/dsh-llm": "^0.0.1", + "@deepseek-ai/dsh-scope": "^0.0.1", "cordis": "^4.0.0-rc.6" }, "dependencies": { @@ -30,6 +31,7 @@ }, "devDependencies": { "@deepseek-ai/dsh-llm": "workspace:^", + "@deepseek-ai/dsh-scope": "workspace:^", "cordis": "^4.0.0-rc.6" } } diff --git a/packages/core/system-prompt/src/index.ts b/packages/core/system-prompt/src/index.ts index 81ca8087bb..63ba6b350d 100644 --- a/packages/core/system-prompt/src/index.ts +++ b/packages/core/system-prompt/src/index.ts @@ -1,8 +1,8 @@ /** * System prompt assembly registry. Plugins contribute ordered text sections, * tool schema providers, and named prompt variables; `assemble(context)` - * collates them through a waterfall that runs once per step, and - * `renderPrompt` interpolates `{{variable}}` references into the final text. + * collates them through a waterfall that runs once per step, and `renderPrompt` + * interpolates `{{variable}}` references into the final text. * * The harness-owned prompt openers live here too: this plugin registers the * static `harness:identity` section (order −100) and the deployment's @@ -14,6 +14,8 @@ import { Context, Service } from 'cordis' import z from 'schemastery' +import { scopeOf, scopeTarget } from '@deepseek-ai/dsh-scope' +import type { ScopeKey, Scoped } from '@deepseek-ai/dsh-scope' import type { ToolSchema } from '@deepseek-ai/dsh-llm' declare module 'cordis' { @@ -27,6 +29,15 @@ declare module 'cordis' { * {@link PromptAssembly} (sections + tools + variables) before it is * rendered. Bound to the {@link SystemPrompt} service; call `next()` to * delegate. + * + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): the carrier is keyed + * by `context.scope` — a listener registered through `agent.ctx` fires only + * for that agent's assemblies; a plain plugin listener fires for every + * assembly (scope-less ones included, dispatched subject-less). + * + * The returned assembly is authoritative. This is an expert composition + * seam: a listener that removes or replaces another plugin's protocol + * contribution owns preserving that protocol's invariants. * @param assembly - the assembly built from the registered sections, tool * providers, and variable providers; listeners may mutate it or return a * replacement. @@ -35,10 +46,14 @@ declare module 'cordis' { * is for), so a listener can filter or extend per agent. * @mode waterfall */ - 'system-prompt/assemble'(this: SystemPrompt, assembly: PromptAssembly, context: AssembleContext, next: () => Promise): Promise + 'system-prompt/assemble'(this: Scoped, assembly: PromptAssembly, context: AssembleContext, next: () => Promise): Promise /** - * A section, tool provider, or variable provider was registered or - * unregistered (the assembly inputs changed). + * A section, tool provider, or variable provider was registered + * or unregistered (the assembly inputs changed — possibly for one scope + * only). An UNFILTERED registry-subject notification, deliberately not + * scope-filtered dispatch: a global change concerns every agent's next + * assembly, so a scoped listener subscribing here sees every change, not + * just its own scope's. * @mode emit */ 'system-prompt/change'(): void @@ -47,30 +62,41 @@ declare module 'cordis' { /** * Per-assembly input: what one {@link SystemPrompt.assemble} call is FOR. - * Declared empty here so this package stays agnostic of who assembles; - * merge-extensible — `@deepseek-ai/dsh-agent` declares the `agent` field, so - * section text and variable providers can be functions of the calling agent. - * Every field is optional by nature: a bare `assemble()` (tests, diagnostics) - * carries an empty context, and providers must tolerate absent fields. + * Merge-extensible and agnostic of who assembles — `@deepseek-ai/dsh-agent` + * declares the `agent` field, so section text and variable providers can be + * functions of the calling agent. Every field is optional by nature: a bare + * `assemble()` (tests, diagnostics) carries an empty, scope-less context, and + * providers must tolerate absent fields. */ -export interface AssembleContext {} +export interface AssembleContext { + /** + * The scope layer this assembly resolves (`@deepseek-ai/dsh-scope`): scoped + * sections/variables/tool-providers registered through this key's context + * join the assembly (shadowing same-named global contributions), and the + * `system-prompt/assemble` waterfall dispatches in this scope. The agent + * loop sets it to the agent (alongside the `agent` DX field — never set + * `agent` without `scope`; the dev invariants flag the mismatch). Absent = + * a scope-less assembly: global layer only, subject-less dispatch. + */ + scope?: ScopeKey +} /** One contributed section of the system prompt (registry input). */ export interface PromptSection { /** Unique name — a duplicate registration throws (see {@link SystemPrompt.section}). */ - name: string + readonly name: string /** * Sections are concatenated in ascending order. Convention: `-100` is the * harness identity, `0` the deployment persona, tool guidance uses 100–199; * other negative orders also render before the persona. */ - order: number + readonly order: number /** * Static text or a provider evaluated at each assembly with that assembly's * {@link AssembleContext}. The text may reference `{{variable}}`s — they are * interpolated later, by {@link renderPrompt}. */ - text: string | ((context: AssembleContext) => string) + readonly text: string | ((context: AssembleContext) => string) } /** One section of an assembly: {@link PromptSection} with its text resolved. */ @@ -83,6 +109,23 @@ export interface AssembledSection { text: string } +/** + * What one tool-schema provider contributes to an assembly + * ({@link SystemPrompt.tools}). `schemas` is the provider's POST-restriction + * visible set for the assembly's scope — exactly what the model may be shown. + * `knownNames` is its PRE-restriction name universe: the set configured names + * (`toolOrder`) are validated against, so a config typo fails loud while a + * restricted-away tool stays a normal, non-erroneous absence. Omitted, + * `knownNames` defaults to the names of `schemas` (right for providers with no + * restriction concept). + */ +export interface ToolProviderResult { + /** The schemas this provider contributes to THIS assembly. */ + readonly schemas: readonly ToolSchema[] + /** The pre-restriction name universe for config validation (defaults to `schemas`' names). */ + readonly knownNames?: readonly string[] +} + /** * The assembled prompt. * @@ -146,23 +189,26 @@ function validateToolOrder(toolOrder: string[] | undefined): string[] | undefine * list, plain lexicographic name order; with one, listed names take their * listed position and every unlisted tool lands at the * {@link TOOL_ORDER_REST} rest entry in lexicographic name order. A listed - * name with no collected tool throws — misconfiguration fails loud, and this - * is the earliest moment the registered tool set exists to check against - * (tool plugins register after the service constructs, so load time is too - * early): the assembly rejects, failing the caller's turn before any model - * request. Never drops a tool, and both sorts are stable, so tools sharing a - * name keep their collection order. + * name outside `knownNames` — the providers' PRE-restriction name universe — + * throws: misconfiguration fails loud, and each assembly is the earliest + * moment the registered tool set exists to check against (tool plugins + * register after the service constructs, so load time is too early); the + * assembly rejects, failing the caller's turn before any model request. A + * listed name that is KNOWN but not collected (a tool restricted away for + * this assembly's scope) is a normal absence: its position simply + * contributes nothing — `toolOrder` stays compatible with per-agent + * `restrict()` masks. Never drops a collected tool, and both sorts are + * stable, so tools sharing a name keep their collection order. */ -function orderTools(tools: ToolSchema[], toolOrder: string[] | undefined): ToolSchema[] { +function orderTools(tools: ToolSchema[], toolOrder: string[] | undefined, knownNames: ReadonlySet): ToolSchema[] { const reserved = tools.find(tool => tool.name === TOOL_ORDER_REST) if (reserved !== undefined) { throw new Error(`tool provider returned reserved tool name "${TOOL_ORDER_REST}" (reserved for toolOrder's rest entry)`) } if (toolOrder === undefined) return tools.sort(compareToolNames) - const registered = new Set(tools.map(tool => tool.name)) - const unknown = toolOrder.filter(name => name !== TOOL_ORDER_REST && !registered.has(name)) + const unknown = toolOrder.filter(name => name !== TOOL_ORDER_REST && !knownNames.has(name)) if (unknown.length > 0) { - throw new Error(`toolOrder lists unregistered tool${unknown.length > 1 ? 's' : ''} ${unknown.map(name => `"${name}"`).join(', ')}; registered tools: ${[...registered].sort().join(', ') || '(none)'}`) + throw new Error(`toolOrder lists unregistered tool${unknown.length > 1 ? 's' : ''} ${unknown.map(name => `"${name}"`).join(', ')}; known tools: ${[...knownNames].sort().join(', ') || '(none)'}`) } const listed = new Set(toolOrder) const rest = tools.filter(tool => !listed.has(tool.name)).sort(compareToolNames) @@ -181,7 +227,10 @@ export interface Config { * The deployment's persona — the ONE deployment-authored fragment of the * system prompt, rendered as the order-0 `deployment:persona` section * (after the harness identity, before all tool guidance). Every agent in - * the context shares it, subagents included. Template, not free-form text: + * the context shares it by default; a per-agent persona is a SCOPED section + * of the same name registered through that agent's `agent.ctx` (it shadows + * this one for that agent — the subagent seam's `persona` request field does + * exactly that). Template, not free-form text: * every complete `{{…}}` group is interpreted strictly against the * registered prompt variables (the shipped agent loop registers `{{model}}` * and `{{cwd}}`), and there is no escape syntax for literal `{{…}}` prose @@ -301,8 +350,12 @@ export class SystemPrompt extends Service { }) private sections: PromptSection[] = [] - private toolProviders: (() => ToolSchema[])[] = [] + private toolProviders: ((context: AssembleContext) => ToolProviderResult)[] = [] private variableProviders = new Map string | undefined>() + /** Per-scope layers (`@deepseek-ai/dsh-scope`); entries drop when a layer empties, so a disposed scope leaves no residue. */ + private scopedSections = new Map() + private scopedToolProviders = new Map ToolProviderResult)[]>() + private scopedVariableProviders = new Map string | undefined>>() private readonly toolOrder: string[] | undefined constructor(ctx: Context, public config: Config) { @@ -330,61 +383,110 @@ export class SystemPrompt extends Service { /** * Contribute a text section to the system prompt. Order is determined by - * `section.order` (ascending). Throws if a section with the same name is - * already registered (a duplicate would silently double prompt text — e.g. - * a double-loaded tool plugin). The section is removed when the calling - * fiber is disposed. Emits `system-prompt/change` on register/unregister. + * `section.order` (ascending). The layer is decided by the CALLING context + * (`@deepseek-ai/dsh-scope`): a plain plugin context contributes globally; a + * scoped context (`agent.ctx`) contributes to that scope alone — and a + * scoped section SHADOWS a same-named global section for that scope's + * assemblies (most-specific-wins; this is how a per-agent persona overrides + * `deployment:persona`). The readonly typed contribution is borrowed until + * disposal; only the semantic + * finite-order rule is checked at runtime. Throws if the SAME layer already has the name (a + * duplicate would silently double prompt text — e.g. a double-loaded tool + * plugin; the global-duplicate message names `agent.ctx` as the per-agent + * alternative). Removed when the calling fiber is disposed. Emits + * `system-prompt/change` on register/unregister. * @param section - the section to contribute (name, order, text or provider). - * @returns the disposer that removes the section. + * @returns the disposer that removes the section. The exact + * Cordis effect disposer (single-shot): composite (generator) effects may + * yield it directly — exact identity nests the teardown in order. */ section(section: PromptSection): () => void { + if (!Number.isFinite(section.order)) { + throw new TypeError(`prompt section "${section.name}" order must be a finite number`) + } + const scope = scopeOf(this.ctx) const dispose = this.ctx.effect(function* (this: SystemPrompt) { - if (this.sections.some(existing => existing.name === section.name)) { - throw new Error(`prompt section "${section.name}" is already registered`) + const layer = scope === undefined + ? this.sections + : this.scopedSections.get(scope) ?? (() => { + const created: PromptSection[] = [] + this.scopedSections.set(scope, created) + return created + })() + if (layer.some(existing => existing.name === section.name)) { + throw new Error(scope === undefined + ? `prompt section "${section.name}" is already registered (for a per-agent override, register through that agent's \`agent.ctx\` instead)` + : `prompt section "${section.name}" is already registered in this scope`) } - this.sections.push(section) + layer.push(section) // Yield the rollback BEFORE emitting `system-prompt/change`: a generator // effect collects each yielded disposer before the next step runs, so a // throwing change listener removes the section instead of leaking it into // every future assembly. yield () => { - const index = this.sections.indexOf(section) + const index = layer.indexOf(section) /* v8 ignore next 3 -- defensive: section was registered, so indexOf is guaranteed >= 0 */ - if (index >= 0) this.sections.splice(index, 1) + if (index >= 0) layer.splice(index, 1) + if (scope !== undefined && layer.length === 0) this.scopedSections.delete(scope) this.ctx.emit('system-prompt/change') } this.ctx.emit('system-prompt/change') }.bind(this), 'systemPrompt.section()') - // ctx.effect's disposer returns Promise; our disposer API is - // synchronous fire-and-forget — discard the (always-resolved) promise. - return () => void dispose() + // The EXACT cordis effect disposer, not a wrapper: a composite (generator) + // effect that owns a teardown ORDER must be able to yield THIS function — + // cordis nests a disposer out of the fiber's concurrent sibling list by + // exact function identity, so a wrapper would silently break the nesting + // (the agents.register() lesson). Cleanup is synchronous because this + // registration installs only synchronous state and notifications. + // eslint-disable-next-line @typescript-eslint/no-misused-promises -- synchronous cleanup; direct return preserves disposer identity + return dispose } /** - * Contribute a tool-schema provider that is evaluated at each assembly - * call (so it can reflect the live registry state). The provider is - * removed when the calling fiber is disposed. A provider must not return a - * schema named {@link TOOL_ORDER_REST}; that name is reserved for + * Contribute a tool-schema provider, evaluated at each assembly call with + * that assembly's {@link AssembleContext} (so it reflects the live registry + * state AND the assembly's scope — see {@link ToolProviderResult} for the + * `schemas`/`knownNames` split). The layer is decided by the calling + * context: a scoped provider (registered through `agent.ctx`) is consulted + * only for that scope's assemblies. Removed when the calling fiber is + * disposed. A provider must not return a schema named + * {@link TOOL_ORDER_REST}; that name is reserved for * {@link Config.toolOrder}'s rest entry and rejects the assembly. Emits * `system-prompt/change`. * @param provider - evaluated at every {@link assemble} for fresh schemas. - * @returns the disposer that removes the provider. + * @returns the disposer that removes the provider. The exact + * Cordis effect disposer (single-shot): composite (generator) effects may + * yield it directly — exact identity nests the teardown in order. */ - tools(provider: () => ToolSchema[]): () => void { + tools(provider: (context: AssembleContext) => ToolProviderResult): () => void { + const scope = scopeOf(this.ctx) const dispose = this.ctx.effect(function* (this: SystemPrompt) { - this.toolProviders.push(provider) + const layer = scope === undefined + ? this.toolProviders + : this.scopedToolProviders.get(scope) ?? (() => { + const created: ((context: AssembleContext) => ToolProviderResult)[] = [] + this.scopedToolProviders.set(scope, created) + return created + })() + layer.push(provider) // Yield the rollback BEFORE emitting `system-prompt/change` (see section()). yield () => { - const index = this.toolProviders.indexOf(provider) + const index = layer.indexOf(provider) /* v8 ignore next 3 -- defensive: provider was registered, so indexOf is guaranteed >= 0 */ - if (index >= 0) this.toolProviders.splice(index, 1) + if (index >= 0) layer.splice(index, 1) + if (scope !== undefined && layer.length === 0) this.scopedToolProviders.delete(scope) this.ctx.emit('system-prompt/change') } this.ctx.emit('system-prompt/change') }.bind(this), 'systemPrompt.tools()') - // ctx.effect's disposer returns Promise; our disposer API is - // synchronous fire-and-forget — discard the (always-resolved) promise. - return () => void dispose() + // The EXACT cordis effect disposer, not a wrapper: a composite (generator) + // effect that owns a teardown ORDER must be able to yield THIS function — + // cordis nests a disposer out of the fiber's concurrent sibling list by + // exact function identity, so a wrapper would silently break the nesting + // (the agents.register() lesson). Cleanup is synchronous because this + // registration installs only synchronous state and notifications. + // eslint-disable-next-line @typescript-eslint/no-misused-promises -- synchronous cleanup; direct return preserves disposer identity + return dispose } /** @@ -392,50 +494,75 @@ export class SystemPrompt extends Service { * `{{name}}`. The provider is evaluated at each assembly with that * assembly's {@link AssembleContext}; returning `undefined` means "no value * for this assembly" (a section referencing it then fails to render — a - * deployment must not claim facts it does not have). Throws on a name that - * does not match `[a-z][a-z0-9_]*` (it could never be referenced) or is - * already registered. Removed when the calling fiber is disposed; emits + * deployment must not claim facts it does not have). The layer is decided + * by the calling context: a scoped variable (registered through + * `agent.ctx`) resolves only for that scope's assemblies and SHADOWS a + * same-named global variable there. Throws on a name that does not match + * `[a-z][a-z0-9_]*` (it could never be referenced) or one already registered + * in the SAME layer. Removed when the calling fiber is disposed; emits * `system-prompt/change` on register/unregister. * @param name - the reference name (matches `[a-z][a-z0-9_]*`). * @param provider - evaluated at every {@link assemble} for the value. - * @returns the disposer that removes the variable. + * @returns the disposer that removes the variable. The exact + * Cordis effect disposer (single-shot): composite (generator) effects may + * yield it directly — exact identity nests the teardown in order. */ variable(name: string, provider: (context: AssembleContext) => string | undefined): () => void { + if (!VARIABLE_NAME.test(name)) { + throw new Error(`invalid prompt variable name "${name}" (must match ${String(VARIABLE_NAME)})`) + } + const scope = scopeOf(this.ctx) const dispose = this.ctx.effect(function* (this: SystemPrompt) { - if (!VARIABLE_NAME.test(name)) { - throw new Error(`invalid prompt variable name "${name}" (must match ${String(VARIABLE_NAME)})`) + const layer = scope === undefined + ? this.variableProviders + : this.scopedVariableProviders.get(scope) ?? (() => { + const created = new Map string | undefined>() + this.scopedVariableProviders.set(scope, created) + return created + })() + if (layer.has(name)) { + throw new Error(scope === undefined + ? `prompt variable "${name}" is already registered (for a per-agent value, register through that agent's \`agent.ctx\` instead)` + : `prompt variable "${name}" is already registered in this scope`) } - if (this.variableProviders.has(name)) { - throw new Error(`prompt variable "${name}" is already registered`) - } - this.variableProviders.set(name, provider) + layer.set(name, provider) // Yield the rollback BEFORE emitting `system-prompt/change` (see section()). yield () => { - this.variableProviders.delete(name) + layer.delete(name) + if (scope !== undefined && layer.size === 0) this.scopedVariableProviders.delete(scope) this.ctx.emit('system-prompt/change') } this.ctx.emit('system-prompt/change') }.bind(this), 'systemPrompt.variable()') - // ctx.effect's disposer returns Promise; our disposer API is - // synchronous fire-and-forget — discard the (always-resolved) promise. - return () => void dispose() + // The EXACT cordis effect disposer, not a wrapper: a composite (generator) + // effect that owns a teardown ORDER must be able to yield THIS function — + // cordis nests a disposer out of the fiber's concurrent sibling list by + // exact function identity, so a wrapper would silently break the nesting + // (the agents.register() lesson). Cleanup is synchronous because this + // registration installs only synchronous state and notifications. + // eslint-disable-next-line @typescript-eslint/no-misused-promises -- synchronous cleanup; direct return preserves disposer identity + return dispose } /** - * Assemble the current prompt for one caller: section texts are resolved - * against `context` and sorted by order, tools collected from all providers - * and put in the canonical model-facing order ({@link Config.toolOrder}, or - * lexicographic name order when unconfigured — provider registration order - * is a plugin-load artifact and never reaches the assembly; a configured - * order naming a tool no provider contributed rejects the assembly), and every - * registered variable resolved against `context` into `assembly.variables`. - * Tool schemas are deep-cloned because adapters and request waterfalls may - * mutate schema objects. Runs through the `system-prompt/assemble` - * waterfall, giving listeners the opportunity to mutate or replace the - * assembly before it reaches the model — like the sections' `order` sort, - * tool canonicalization happens on the initial assembly, and a listener - * owns the determinism of whatever it emits. Await the result before - * reading the assembly values — waterfall listeners may be async. + * Assemble the current prompt for one caller: the global layer merged with + * {@link AssembleContext.scope}'s layer (scoped sections/variables SHADOW + * same-named global ones — most-specific-wins) — section texts resolved + * against `context` and sorted by order across the union, tools collected + * from the global providers plus the scope's and put in the canonical + * model-facing order ({@link Config.toolOrder}, or lexicographic name order + * when unconfigured — provider registration order is a plugin-load artifact + * and never reaches the assembly; a configured order naming a tool outside + * the providers' `knownNames` universe rejects the assembly, while a known + * name restricted away for this scope is a normal absence), and every + * visible variable resolved against `context` into `assembly.variables`. + * Tool schemas are detached because assembly waterfalls may mutate them. + * Runs through the `system-prompt/assemble` waterfall, giving listeners the + * opportunity to mutate or replace the assembly; the returned value is the + * authoritative model-visible composition. Like the sections' `order` + * sort, tool canonicalization happens on the initial assembly; listener + * output owns its own determinism. Await the result before reading the + * assembly values — waterfall listeners may be async. * Interpolation happens later, in {@link renderPrompt}. * @param context - what this assembly is for (defaults to an empty context; * see {@link AssembleContext}). @@ -445,25 +572,64 @@ export class SystemPrompt extends Service { // rejection: a Promise-returning method must not throw synchronously // (`assemble().catch(...)` would miss it). async assemble(context: AssembleContext = {}): Promise { + const scope = context.scope + // Variables: global layer first, then the scope's layer OVERWRITES + // same-named entries (shadowing — a per-agent value wins for that agent). const variables: Record = {} for (const [name, provider] of this.variableProviders) { variables[name] = provider(context) } + const scopedVariables = scope === undefined ? undefined : this.scopedVariableProviders.get(scope) + for (const [name, provider] of scopedVariables ?? []) { + variables[name] = provider(context) + } + // Sections: merge by name, scoped REPLACING same-named global entries + // (most-specific-wins — the per-agent persona mechanism), then sort by + // order across the union. Registration order within a layer is preserved + // for equal orders (stable sort). + const sectionByName = new Map() + for (const section of this.sections) sectionByName.set(section.name, section) + for (const section of (scope === undefined ? [] : this.scopedSections.get(scope)) ?? []) { + sectionByName.set(section.name, section) + } + // Tools: consult the global providers plus the scope's, each with this + // assembly's context. `schemas` are what the model may see (already + // post-restriction, per provider); `knownNames` (defaulting to the + // schemas' names) form the pre-restriction universe `toolOrder` is + // validated against, so a restricted-away tool is a normal absence while + // a config typo still fails every assembly loudly. + const providers = [ + ...this.toolProviders, + ...(scope === undefined ? [] : this.scopedToolProviders.get(scope)) ?? [], + ] + const collected: ToolSchema[] = [] + const knownNames = new Set() + for (const provider of providers) { + const result = provider(context) + const schemas = result.schemas.map(({ name, description, parameters }): ToolSchema => ({ + name, + description, + parameters: structuredClone(parameters), + })) + const acceptedKnownNames = result.knownNames ?? schemas.map(tool => tool.name) + collected.push(...schemas) + for (const name of acceptedKnownNames) knownNames.add(name) + } const assembly: PromptAssembly = { - sections: this.sections + sections: [...sectionByName.values()] .map(section => ({ name: section.name, order: section.order, text: typeof section.text === 'function' ? section.text(context) : section.text, })) .sort((a, b) => a.order - b.order), - tools: orderTools( - this.toolProviders.flatMap(provider => - provider().map(tool => ({ ...tool, parameters: structuredClone(tool.parameters) }))), - this.toolOrder), + tools: orderTools(collected, this.toolOrder, knownNames), variables, } - return this.ctx.waterfall(this, 'system-prompt/assemble', assembly, context, () => Promise.resolve(assembly)) + return this.ctx.waterfall( + scopeTarget(this, scope), 'system-prompt/assemble', assembly, context, + () => Promise.resolve(assembly), + ) } } diff --git a/packages/core/system-prompt/tests/scoped.spec.ts b/packages/core/system-prompt/tests/scoped.spec.ts new file mode 100644 index 0000000000..a452bb6797 --- /dev/null +++ b/packages/core/system-prompt/tests/scoped.spec.ts @@ -0,0 +1,153 @@ +import { describe, expect, it } from 'vitest' +import { Context } from 'cordis' +import { createScope, scopeOf } from '@deepseek-ai/dsh-scope' +import type { Scope, ScopeKey } from '@deepseek-ai/dsh-scope' +import SystemPrompt, { TOOL_ORDER_REST, renderPrompt } from '@deepseek-ai/dsh-system-prompt' +import type { Config, PromptAssembly } from '@deepseek-ai/dsh-system-prompt' + +async function mount(config: Config = {}): Promise { + const ctx = new Context() + await ctx.plugin(SystemPrompt, config) + return ctx +} + +async function mintScope(ctx: Context, name: string): Promise { + let scope!: Scope + // The scoped context resolves services through the MINTING plugin's + // dependency chain — the minter must inject what scope holders will reach. + await ctx.plugin(Object.assign((inner: Context) => { scope = createScope(inner, { name }) }, + { inject: ['systemPrompt'] })) + return scope +} + +const schema = (name: string) => ({ name, description: `tool ${name}`, parameters: {} }) + +/** The key a test scope was minted with (scopeOf over the scope's own ctx). */ +function scopeKeyOf(scope: Scope): ScopeKey { + // scopeOf never answers undefined for a context the scope itself minted. + + return scopeOf(scope.ctx)! +} + +describe('scoped sections', () => { + it('a scoped persona shadows deployment:persona for that scope only (either order)', async () => { + const ctx = await mount({ persona: 'You are the deployment.' }) + const scope = await mintScope(ctx, 'child') + scope.ctx.systemPrompt.section({ name: 'deployment:persona', order: 0, text: 'You run tests.' }) + + const scoped = renderPrompt(await ctx.systemPrompt.assemble({ scope: scopeKeyOf(scope) })) + const global = renderPrompt(await ctx.systemPrompt.assemble()) + expect(scoped).toContain('You run tests.') + expect(scoped).not.toContain('You are the deployment.') + expect(global).toContain('You are the deployment.') + expect(global).not.toContain('You run tests.') + }) + + it('scoped-only sections join that scope alone; disposal removes them', async () => { + const ctx = await mount() + const scope = await mintScope(ctx, 'child') + scope.ctx.systemPrompt.section({ name: 'child:extra', order: 50, text: 'Extra guidance.' }) + + expect(renderPrompt(await ctx.systemPrompt.assemble({ scope: scopeKeyOf(scope) }))).toContain('Extra guidance.') + expect(renderPrompt(await ctx.systemPrompt.assemble())).not.toContain('Extra guidance.') + await scope.dispose() + expect(renderPrompt(await ctx.systemPrompt.assemble({ scope: scopeKeyOf(scope) }))).not.toContain('Extra guidance.') + }) + + it('duplicate names throw per layer, naming agent.ctx for the global case', async () => { + const ctx = await mount() + const scope = await mintScope(ctx, 'child') + ctx.systemPrompt.section({ name: 'x', order: 1, text: 'a' }) + expect(() => ctx.systemPrompt.section({ name: 'x', order: 1, text: 'b' })).toThrow(/agent\.ctx/) + scope.ctx.systemPrompt.section({ name: 'y', order: 1, text: 'a' }) + expect(() => scope.ctx.systemPrompt.section({ name: 'y', order: 1, text: 'b' })).toThrow(/already registered in this scope/) + }) + +}) + +describe('scoped variables', () => { + it('a scoped variable shadows its global name-twin for that scope', async () => { + const ctx = await mount({ persona: 'Mode: {{mode}}.' }) + const scope = await mintScope(ctx, 'child') + ctx.systemPrompt.variable('mode', () => 'normal') + scope.ctx.systemPrompt.variable('mode', () => 'strict') + + expect(renderPrompt(await ctx.systemPrompt.assemble({ scope: scopeKeyOf(scope) }))).toContain('Mode: strict.') + expect(renderPrompt(await ctx.systemPrompt.assemble())).toContain('Mode: normal.') + }) + + it('same-layer duplicates throw; scoped layer cleans up on dispose', async () => { + const ctx = await mount() + const scope = await mintScope(ctx, 'child') + scope.ctx.systemPrompt.variable('v', () => '1') + expect(() => scope.ctx.systemPrompt.variable('v', () => '2')).toThrow(/already registered in this scope/) + await scope.dispose() + // Re-minting a scope with the SAME key starts clean. + const again = await mintScope(ctx, 'child2') + again.ctx.systemPrompt.variable('v', () => '3') + }) +}) + +describe('scoped tool providers and toolOrder × restriction', () => { + it('scoped providers are consulted only for their scope', async () => { + const ctx = await mount() + const scope = await mintScope(ctx, 'child') + ctx.systemPrompt.tools(() => ({ schemas: [schema('global_tool')] })) + scope.ctx.systemPrompt.tools(() => ({ schemas: [schema('scoped_tool')] })) + + const scoped = await ctx.systemPrompt.assemble({ scope: scopeKeyOf(scope) }) + const global = await ctx.systemPrompt.assemble() + expect(scoped.tools.map(t => t.name)).toEqual(['global_tool', 'scoped_tool']) + expect(global.tools.map(t => t.name)).toEqual(['global_tool']) + }) + + it('disposing a scoped tool provider empties its layer without residue', async () => { + const ctx = await mount() + const scope = await mintScope(ctx, 'child') + const dispose = scope.ctx.systemPrompt.tools(() => ({ schemas: [schema('scoped_tool')] })) + dispose() + const after = await ctx.systemPrompt.assemble({ scope: scopeKeyOf(scope) }) + expect(after.tools.map(t => t.name)).toEqual([]) + // Re-registering through the same scope starts a fresh layer. + scope.ctx.systemPrompt.tools(() => ({ schemas: [schema('again')] })) + const again = await ctx.systemPrompt.assemble({ scope: scopeKeyOf(scope) }) + expect(again.tools.map(t => t.name)).toEqual(['again']) + }) + + it('a toolOrder entry restricted away for a scope is a normal absence, while a typo still throws', async () => { + const ctx = await mount({ toolOrder: ['bash', TOOL_ORDER_REST] }) + // A provider mimicking the registry's restriction split: bash exists + // (knownNames) but is masked for this assembly (schemas). + ctx.systemPrompt.tools(() => ({ + schemas: [schema('read')], + knownNames: ['read', 'bash'], + })) + const assembly = await ctx.systemPrompt.assemble() + expect(assembly.tools.map(t => t.name)).toEqual(['read']) + + const bad = await mount({ toolOrder: ['basj', TOOL_ORDER_REST] }) + bad.systemPrompt.tools(() => ({ schemas: [schema('read')], knownNames: ['read', 'bash'] })) + await expect(bad.systemPrompt.assemble()).rejects.toThrow('toolOrder lists unregistered tool "basj"; known tools: bash, read') + }) +}) + +describe('scoped assemble dispatch', () => { + it('an agent.ctx assemble listener shapes only its own scope\'s assemblies', async () => { + const ctx = await mount() + const scope = await mintScope(ctx, 'child') + const shaped: (ScopeKey | undefined)[] = [] + scope.ctx.on('system-prompt/assemble', async (_assembly: PromptAssembly, context, next: () => Promise) => { + shaped.push(context.scope) + const result = await next() + result.sections.push({ name: 'listener:extra', order: 999, text: 'listener text' }) + return result + }) + + const scoped = await ctx.systemPrompt.assemble({ scope: scopeKeyOf(scope) }) + const global = await ctx.systemPrompt.assemble() + expect(scoped.sections.some(s => s.name === 'listener:extra')).toBe(true) + expect(global.sections.some(s => s.name === 'listener:extra')).toBe(false) + expect(shaped).toHaveLength(1) + }) + +}) diff --git a/packages/core/system-prompt/tests/system-prompt.spec.ts b/packages/core/system-prompt/tests/system-prompt.spec.ts index c0bd8be6d2..c7436711fa 100644 --- a/packages/core/system-prompt/tests/system-prompt.spec.ts +++ b/packages/core/system-prompt/tests/system-prompt.spec.ts @@ -52,7 +52,7 @@ describe('SystemPrompt', () => { ctx.systemPrompt.section({ name: 'cwd', order: 20, text: () => 'cwd: /tmp' }) ctx.systemPrompt.section({ name: 'rules', order: 10, text: 'Be precise.' }) - ctx.systemPrompt.tools(() => [{ name: 'echo', description: 'echo back', parameters: {} }]) + ctx.systemPrompt.tools(() => ({ schemas: [{ name: 'echo', description: 'echo back', parameters: {} }] })) const assembly = await ctx.systemPrompt.assemble() expect(assembly.sections.map(s => s.name)).toEqual(['harness:identity', 'deployment:persona', 'rules', 'cwd']) @@ -84,7 +84,7 @@ describe('SystemPrompt', () => { const fiber = await ctx.plugin(Object.assign((inner: Context) => { inner.systemPrompt.section({ name: 'scoped', order: 0, text: 'scoped section' }) - inner.systemPrompt.tools(() => [{ name: 'scoped-tool', description: '', parameters: {} }]) + inner.systemPrompt.tools(() => ({ schemas: [{ name: 'scoped-tool', description: '', parameters: {} }] })) inner.systemPrompt.variable('scoped_var', () => 'v') }, { inject: ['systemPrompt'] })) @@ -111,6 +111,14 @@ describe('SystemPrompt', () => { expect(contributed(assembly).map(s => s.text)).toEqual(['first']) }) + it('rejects a non-finite section order', async () => { + const ctx = new Context() + await ctx.plugin(SystemPrompt) + expect(() => ctx.systemPrompt.section({ name: 'bad-order', order: Number.NaN, text: 'x' })) + .toThrow('order must be a finite number') + expect(contributed(await ctx.systemPrompt.assemble())).toEqual([]) + }) + it('rolls back a section when a system-prompt/change listener throws (P1-1)', async () => { const ctx = new Context() await ctx.plugin(SystemPrompt) @@ -141,11 +149,11 @@ describe('SystemPrompt', () => { if (!threw) { threw = true; throw new Error('boom change listener') } }) - expect(() => ctx.systemPrompt.tools(() => [{ name: 't', description: '', parameters: {} }])).toThrow('boom change listener') + expect(() => ctx.systemPrompt.tools(() => ({ schemas: [{ name: 't', description: '', parameters: {} }] }))).toThrow('boom change listener') expect((await ctx.systemPrompt.assemble()).tools).toHaveLength(0) // nothing leaked off() - ctx.systemPrompt.tools(() => [{ name: 't', description: '', parameters: {} }]) + ctx.systemPrompt.tools(() => ({ schemas: [{ name: 't', description: '', parameters: {} }] })) expect((await ctx.systemPrompt.assemble()).tools.map(t => t.name)).toEqual(['t']) }) @@ -209,7 +217,7 @@ describe('SystemPrompt', () => { const ctx = new Context() await ctx.plugin(SystemPrompt) ctx.systemPrompt.section({ name: 'base', order: 0, text: 'base' }) - ctx.systemPrompt.tools(() => [{ name: 't', description: 'tool', parameters: { type: 'object', properties: {} } }]) + ctx.systemPrompt.tools(() => ({ schemas: [{ name: 't', description: 'tool', parameters: { type: 'object', properties: {} } }] })) const first = await ctx.systemPrompt.assemble() first.sections[0]!.name = 'mutated' @@ -243,7 +251,7 @@ describe('SystemPrompt', () => { let changeCount = 0 ctx.on('system-prompt/change', () => void changeCount++) - const dispose = ctx.systemPrompt.tools(() => []) + const dispose = ctx.systemPrompt.tools(() => ({ schemas: [] })) // registration emits change expect(changeCount).toBe(1) @@ -257,7 +265,7 @@ describe('SystemPrompt', () => { await ctx.plugin(SystemPrompt) const fiber = await ctx.plugin(Object.assign((inner: Context) => { - inner.systemPrompt.tools(() => [{ name: 'fiber-tool', description: '', parameters: {} }]) + inner.systemPrompt.tools(() => ({ schemas: [{ name: 'fiber-tool', description: '', parameters: {} }] })) }, { inject: ['systemPrompt'] })) expect((await ctx.systemPrompt.assemble()).tools).toHaveLength(1) @@ -280,7 +288,7 @@ describe('SystemPrompt', () => { const ctx = new Context() await ctx.plugin(SystemPrompt) - const dispose = ctx.systemPrompt.tools(() => [{ name: 'direct-tool', description: '', parameters: {} }]) + const dispose = ctx.systemPrompt.tools(() => ({ schemas: [{ name: 'direct-tool', description: '', parameters: {} }] })) expect((await ctx.systemPrompt.assemble()).tools).toHaveLength(1) dispose() diff --git a/packages/core/system-prompt/tests/tool-order.spec.ts b/packages/core/system-prompt/tests/tool-order.spec.ts index 02cc99b2d7..16eff6e354 100644 --- a/packages/core/system-prompt/tests/tool-order.spec.ts +++ b/packages/core/system-prompt/tests/tool-order.spec.ts @@ -26,39 +26,39 @@ describe('SystemPrompt tool order', () => { it('assembles tools in lexicographic name order when no toolOrder is configured', async () => { const ctx = await mount() - ctx.systemPrompt.tools(() => [tool('charlie'), tool('alpha')]) - ctx.systemPrompt.tools(() => [tool('bravo')]) + ctx.systemPrompt.tools(() => ({ schemas: [tool('charlie'), tool('alpha')] })) + ctx.systemPrompt.tools(() => ({ schemas: [tool('bravo')] })) expect(names(await ctx.systemPrompt.assemble())).toEqual(['alpha', 'bravo', 'charlie']) }) it('assembles the same order regardless of provider registration order', async () => { const forward = await mount() - forward.systemPrompt.tools(() => [tool('alpha')]) - forward.systemPrompt.tools(() => [tool('zulu')]) + forward.systemPrompt.tools(() => ({ schemas: [tool('alpha')] })) + forward.systemPrompt.tools(() => ({ schemas: [tool('zulu')] })) const backward = await mount() - backward.systemPrompt.tools(() => [tool('zulu')]) - backward.systemPrompt.tools(() => [tool('alpha')]) + backward.systemPrompt.tools(() => ({ schemas: [tool('zulu')] })) + backward.systemPrompt.tools(() => ({ schemas: [tool('alpha')] })) expect(names(await forward.systemPrompt.assemble())).toEqual(['alpha', 'zulu']) expect(names(await backward.systemPrompt.assemble())).toEqual(['alpha', 'zulu']) }) it('applies a configured toolOrder: listed positions, rest at the rest entry lexicographically', async () => { const ctx = await mount({ toolOrder: ['todo_write', TOOL_ORDER_REST, 'bash'] }) - ctx.systemPrompt.tools(() => [tool('bash'), tool('echo_b'), tool('todo_write'), tool('echo_a')]) + ctx.systemPrompt.tools(() => ({ schemas: [tool('bash'), tool('echo_b'), tool('todo_write'), tool('echo_a')] })) expect(names(await ctx.systemPrompt.assemble())).toEqual(['todo_write', 'echo_a', 'echo_b', 'bash']) }) it('rejects the assembly when toolOrder names a tool that is not registered (misconfiguration blocks work)', async () => { const ctx = await mount({ toolOrder: ['todo_write', 'ghost', TOOL_ORDER_REST, 'wraith'] }) - ctx.systemPrompt.tools(() => [tool('bash'), tool('todo_write')]) + ctx.systemPrompt.tools(() => ({ schemas: [tool('bash'), tool('todo_write')] })) await expect(ctx.systemPrompt.assemble()).rejects.toThrow( - 'toolOrder lists unregistered tools "ghost", "wraith"; registered tools: bash, todo_write') + 'toolOrder lists unregistered tools "ghost", "wraith"; known tools: bash, todo_write') }) it('names the single unregistered tool when no tools are registered at all', async () => { const ctx = await mount({ toolOrder: ['ghost', TOOL_ORDER_REST] }) await expect(ctx.systemPrompt.assemble()).rejects.toThrow( - 'toolOrder lists unregistered tool "ghost"; registered tools: (none)') + 'toolOrder lists unregistered tool "ghost"; known tools: (none)') }) it.each([ @@ -66,21 +66,21 @@ describe('SystemPrompt tool order', () => { ['with only the rest entry configured', [TOOL_ORDER_REST]], ])('rejects a provider tool named like the reserved rest entry %s', async (_case, toolOrder) => { const ctx = await mount(toolOrder === undefined ? {} : { toolOrder }) - ctx.systemPrompt.tools(() => [tool(TOOL_ORDER_REST)]) + ctx.systemPrompt.tools(() => ({ schemas: [tool(TOOL_ORDER_REST)] })) await expect(ctx.systemPrompt.assemble()).rejects.toThrow( `tool provider returned reserved tool name "${TOOL_ORDER_REST}"`) }) it('keeps collection order between tools that share a name (stable sort)', async () => { const ctx = await mount() - ctx.systemPrompt.tools(() => [tool('dup', 'first'), tool('anchor'), tool('dup', 'second')]) + ctx.systemPrompt.tools(() => ({ schemas: [tool('dup', 'first'), tool('anchor'), tool('dup', 'second')] })) const assembly = await ctx.systemPrompt.assemble() expect(assembly.tools.map(t => t.description)).toEqual(['anchor', 'first', 'second']) }) it('canonicalizes BEFORE the assemble waterfall: listeners see the ordered list and own their own edits', async () => { const ctx = await mount() - ctx.systemPrompt.tools(() => [tool('zulu'), tool('alpha')]) + ctx.systemPrompt.tools(() => ({ schemas: [tool('zulu'), tool('alpha')] })) let seen: string[] | undefined ctx.on('system-prompt/assemble', function (assembly, _context, next) { seen = assembly.tools.map(t => t.name) diff --git a/packages/core/system-prompt/tsconfig.json b/packages/core/system-prompt/tsconfig.json index e9de391ba1..91e7bf1ba4 100644 --- a/packages/core/system-prompt/tsconfig.json +++ b/packages/core/system-prompt/tsconfig.json @@ -19,6 +19,9 @@ }, { "path": "../../llm/llm" + }, + { + "path": "../../core/scope" } ] } diff --git a/packages/core/tools/README.md b/packages/core/tools/README.md index 7a383c5064..a9411f6cba 100644 --- a/packages/core/tools/README.md +++ b/packages/core/tools/README.md @@ -1,6 +1,6 @@ # dsh-tools -Tool registry and execution pipeline. Tool plugins register their schemas and executors; the agent loop executes each call through `tools/pre-execute` (the allow/deny gate) → `tools/execute` (an around-dispatch wrapper for timeout/retry/metrics plugins) → `tools/post-execute` (inspect/replace the result, attach context). The registry also owns HOW its tools are presented to the model — its `mode` config selects native function calling, [Code Mode](#code-mode), or both. +Tool registry and execution pipeline. Tool plugins register their schemas and executors; the agent loop executes each call through `tools/pre-execute` (the extensible allow/deny gate) → monotonic registered guards → `tools/execute` (an around-dispatch wrapper for timeout/retry/metrics plugins) → `tools/post-execute` (inspect/replace the result, attach context) → the observe-only `tools/result` notification. The registry also owns HOW its tools are presented to the model — its `mode` config selects native function calling, [Code Mode](#code-mode), or both. ## Service: `ToolRegistry` (ctx key: `tools`) @@ -11,41 +11,41 @@ tools: mode: native # native (default) | code | both ``` -`native` contributes every registered tool as a wire function definition — the default, byte-for-byte the pre-config behavior. `code` contributes exactly ONE wire tool, `run_code`, plus the generated `tools:sdk` prompt section (see [Code Mode](#code-mode)). `both` contributes every native definition AND `run_code` + the SDK section. Non-native modes require a loaded `ctx.codeRuntime` with `language: 'typescript'`; a missing or mismatched runtime rejects every prompt assembly with an actionable error, and a `systemPrompt.toolOrder` naming tools the mode no longer contributes rejects the assembly the same way. +`native` contributes the calling agent's visible end capabilities as wire function definitions. Under `code`, this registry contributes the reserved `run_code` transport plus the generated `tools:sdk` prompt section (see [Code Mode](#code-mode)); `both` contributes the visible native definitions and both infrastructure pieces. Restrictions cannot remove `run_code`, and registering, shadowing, or explicitly filtering that reserved name fails loudly. An expert `system-prompt/assemble` listener may replace any prompt or schema contribution; its returned assembly is authoritative, so the listener owns preserving Code Mode when the protocol should remain active. Non-native modes require a loaded `ctx.codeRuntime` with `language: 'typescript'`; a missing or mismatched runtime rejects every prompt assembly with an actionable error, and a `systemPrompt.toolOrder` naming tools the mode no longer contributes rejects the assembly the same way. ### Public API -- `ctx.tools.register(definition: ToolDefinition): () => void` Register a tool. Disposed with the calling fiber. -- `ctx.tools.get(name: string): ToolDefinition | undefined` -- `ctx.tools.schemas(): ToolSchema[]` Schemas of all registered tools (without the `execute` functions). The shipped tools' schemas are catalogued in [docs/tool-catalog.md](../../../docs/tool-catalog.md), generated by booting each tool plugin and harvesting this method (see [the tool-schema-catalog RFC](../../../docs/rfc/implemented/process/2026-07-02-tool-schema-catalog.md)). -- `ctx.tools.execute(exec: ToolExecution): Promise` Execute one tool call through the `tools/pre-execute` → `tools/execute` → `tools/post-execute` pipeline. +- `ctx.tools.register(definition: ToolDefinition): () => void` Register a trusted typed same-process definition. The layer is the calling context's scope: a plain plugin context registers globally; an agent's `agent.ctx` registers for that agent alone, shadowing a same-named global tool there. Duplicate names within one layer throw; non-native modes also reject the reserved `run_code` transport name. `timeoutMs`, when present, must be positive and finite. Disposed with the calling fiber. +- `ctx.tools.restrict(filter: ToolRestriction): () => void` Scoped-only (throws on a plain context): mask the global end-capability surface for the calling agent — `allow` keeps only the listed global tools, `deny` removes them; multiple restrictions intersect; scope-local registrations are merged afterward. The readonly arrays compile once into private sets. Every listed name must exist in the current pre-restriction global registry; scope-local, unknown, and reserved `run_code` names fail loudly. A deny-list admits a later global tool unless it names that tool; an allow-list excludes later names; neither filters a later scope-local registration. `restrict({})` rejects. This is live registration composition, not a parent-derived authority ceiling; see the [agent-scope security non-goal](../../../docs/rfc/implemented/architecture/2026-07-08-agent-scope-contexts.md#security-and-authority-are-explicit-non-goals). +- `ctx.tools.get(name: string, scope?: ScopeKey): ToolDefinition | undefined` Resolution as one scope sees it (shadowing applied; a restricted-away global reads as absent) — presenters pass the calling agent so the card matches what executed. +- `ctx.tools.schemas(scope?: ScopeKey): ToolSchema[]` Schemas of everything the scope can see (without the `execute` functions). The shipped tools' schemas are catalogued in [docs/tool-catalog.md](../../../docs/tool-catalog.md), generated by booting each tool plugin and harvesting this method (see [the tool-schema-catalog RFC](../../../docs/rfc/implemented/process/2026-07-02-tool-schema-catalog.md)). +- `ctx.tools.guard(guard: ToolGuard): () => void` Register a monotonic synchronous execution guard after `tools/pre-execute`: returning a reason denies the call, while `undefined` leaves it unchanged. A plain-context guard applies globally; an `agent.ctx` guard applies only to that agent. Later waterfall listeners cannot turn a guard denial back into permission. Disposed with the calling fiber. +- `ctx.tools.execute(exec: ToolExecutionInput): Promise` Assign a fresh opaque correlation token, losslessly materialize and deep-freeze arguments once at the model/tool boundary, then run the call through `tools/pre-execute` → guards → `tools/execute` → `tools/post-execute`. Invalid arguments normalize through the same authoritative result path without reaching policy or the body. The final outcome is independently materialized and deep-frozen once before `tools/result`. Optional `signal` remains the operational field an around-dispatch wrapper may replace. ### Injected services -`SystemPrompt` — the registry automatically feeds its tool schemas into the system-prompt assembly via `ctx.systemPrompt.tools()`. +`SystemPrompt` — the registry automatically feeds its tool schemas into the system-prompt assembly via `ctx.systemPrompt.tools()`. The approval seam is consumed opportunistically instead (`ctx.get('approval')`, no static inject): a deployment without it keeps the ask→deny degrade, and the registry stays active either way. -### Events +### Live events -| Event | Mode | Purpose | -|---|---|---| -| `tools/pre-execute` | waterfall | Allow/deny gate BEFORE a tool runs (sandbox, permission, hooks); returns `PreToolDecision` | -| `tools/execute` | waterfall | Around-dispatch wrapper (timeout, retry, metrics): `(exec, next)` → the dispatched `ToolExecutionResult`; `next()` is dispatch-with-normalization | -| `tools/post-execute` | waterfall | Inspect/replace the result AFTER a tool runs, attach context; returns `PostToolDecision` | -| `tools/change` | emit | A tool was registered or unregistered | +The live registry pipeline has three transformable waterfalls followed by the observe-only `tools/result` boundary; registry changes are deliberately unfiltered shared-state notifications. Exact signatures, dispatch modes, scope filtering, and failure-containment contracts live in the generated [Cordis event catalog](../../../docs/cordis-catalog/events.md), while the complete ordering is visualized in the generated [tool execution pipeline](../../../docs/tool-execution-pipeline.md). `tools/result` is live; the similarly named `tool/result` is the durable session event the agent loop appends afterwards. ### Key types -- `ToolDefinition` — `ToolSchema` + `execute(args, exec): Promise` (the bare array is the model-facing content; the object form additionally attaches an opaque, JSON-serializable `meta` presentation payload persisted on the `tool/result` event and handed back to `presentResult`), plus optional `presentCall(args)` / `presentResult(args, result)` for tool-owned UI presentation (see below). It also carries an optional cooperative timeout budget `timeoutMs?: number` (ms) enforced by `@deepseek-ai/dsh-timeout-policy`, never sent to the model. -- `ToolExecution` — one pending tool call: `{ callId, name, arguments, agent?, signal? }`. -- `ToolExecutionResult` — outcome: `{ callId, content, isError, error?, additionalContext?, meta? }`. On failure with a `HarnessError`, `error: { name, code }` carries the structured failure class alongside the model-facing text (the loop forwards it onto the `tool/result` session event for retry/sandbox plugins and replay). `additionalContext` (a `HookContext`) ferries any `tools/post-execute` context up to the loop, which buffers it and appends it as a `context/message` after all `tool/result`s in the step. `meta` is the tool's opaque presentation payload from a successful `execute` (the object return form); the loop forwards it onto the `tool/result` session event for result-card rendering. -- `PreToolDecision` — `{kind:'allow'}` | `{kind:'deny', reason}` | `{kind:'ask', reason?}`. Input rewrite (changing `arguments`) is deliberately NOT offered (it would desync the pre-execution audit/history/UI from what ran — its own proposed RFC); `ask` degrades to `deny` until the permission system lands. +- `ToolDefinition` — `ToolSchema` + `execute(args, exec)`, optional presentation callbacks, and cooperative `timeoutMs`. +- `ToolExecutionInput` — the caller-supplied call description: `{ callId, name, arguments, agent?, parent?, signal? }`; callers may pass an enclosing execution's opaque token as `parent` but never choose the new execution's own token. +- `ToolExecutionToken` — a fresh branded `Symbol` assigned by the registry. It supports equality correlation only and never crosses a model, log, or worker boundary. +- `ToolExecution` — the pipeline-owned call: immutable `{ token, callId, name, arguments, agent?, parent? }` identity plus optional operational `signal`, which an around wrapper may add, replace, remove, and restore. A nested call's `parent` is a `ToolExecutionToken`, not an execution object. +- `ToolExecutionResult` — losslessly JSON-serializable outcome: `{ callId, content, isError, error?, additionalContext?, meta? }`. The registry materializes and freezes the complete post-policy value before final observation. On failure with a `HarnessError`, `error: { name, code }` carries the structured failure class alongside the model-facing text. +- `PreToolDecision` — `{kind:'allow'}` | `{kind:'deny', reason}` | `{kind:'ask', reason?}`. Input rewrite is deliberately not offered; `ask` is serviced by [`ctx.approval`](../../ui/user-approval/README.md) when mounted and otherwise degrades to deny. - `PostToolDecision` — `{kind:'accept', content?, additionalContext?}` (keep the call successful, optionally replacing the model-facing content) | `{kind:'block', feedback, additionalContext?}` (turn it into an `isError` whose content is the corrective feedback). Output replacement is clean because `tool/result` is logged AFTER `execute()` returns. +- `ToolGuard` — `(execution) => string | undefined`; the returned string is a final monotonic denial reason evaluated after the reorderable pre-execute waterfall and before dispatch. - `ToolCallView` / `ToolResultView` — provider-neutral `card`-tagged render intents a tool returns from `presentCall` / `presentResult` to own how a UI renders ITS calls (see "Tool-owned UI presentation"). ### Extension points - Tool plugins call `ctx.tools.register()` — schemas flow into the assembly automatically. -- `tools/pre-execute` is the allow/deny gate (sandbox, permission, hooks): listeners receive `(exec, next)` and call `next()` to delegate to the default (allow) or return a `PreToolDecision` to short-circuit; a `deny`/`ask` skips dispatch and yields an `isError` result. `tools/execute` is the around-dispatch seam (timeout, retry, metrics): listeners receive `(exec, next)` and call `next()` to delegate to core dispatch (returning its `ToolExecutionResult`, optionally wrapped), or return a replacement result to short-circuit dispatch; the base `next()` IS dispatch-with-normalization, so `await next()` already yields an `isError` result for a thrown/unknown tool (never a raw throw). A wrapper mutates `exec` in place before `next()` — e.g. replacing `exec.signal` with a per-call deadline — because cordis `next()` ignores passed arguments. `tools/post-execute` is the inspect/transform seam: `(exec, result, next)` → a `PostToolDecision` that can replace content, block with feedback, or attach `additionalContext`. Core dispatch is the base of the `tools/execute` waterfall; the tool body keeps its own try/catch so a thrown tool still reaches `post-execute` as an `isError`. All follow the typed-Decision idiom shared with the `agent/*` interception seams (see [`dsh-agent`](../agent/README.md)); `@deepseek-ai/dsh-timeout-policy` is the reference `tools/execute` wrapper. +- `tools/pre-execute` is the reorderable allow/deny/ask gate (sandbox, permission, hooks): listeners receive `(exec, next)` and call `next()` to delegate to the default (allow) or return a `PreToolDecision` to short-circuit; a `deny` skips dispatch, while an `ask` resolves through the approval seam and dispatches only after a grant. Either non-grant path yields an `isError` result. `ctx.tools.guard()` installs scope-aware monotonic policy after that waterfall when a denial must not be overridable by listener ordering. `tools/execute` is the around-dispatch seam (timeout, retry, metrics): listeners receive `(exec, next)` and call `next()` to delegate to core dispatch (returning its `ToolExecutionResult`, optionally wrapped), or return a replacement result to short-circuit dispatch; the base `next()` is dispatch-with-normalization, so `await next()` already yields an `isError` result for a thrown or unknown tool. A wrapper may change only `exec.signal` before `next()`—adding a per-call deadline, replacing a caller signal, or restoring absence afterwards—because call identity is protected before policy begins. `tools/post-execute` is the inspect/transform seam: `(exec, result, next)` → a `PostToolDecision` that can replace content, block with feedback, or attach `additionalContext`. Core dispatch is the base of the `tools/execute` waterfall; the tool body keeps its own error boundary so a thrown tool still reaches `post-execute` as an `isError`. Finally, `tools/result` observes the immutable authoritative result after every transform and error boundary. All follow the typed-decision idiom shared with the `agent/*` seams (see [`dsh-agent`](../agent/README.md)); `@deepseek-ai/dsh-timeout-policy` is the reference `tools/execute` wrapper. - MCP servers: one plugin per server, discover tools, call `ctx.tools.register()` with the server's schemas. ### Typed tool parameter schemas @@ -75,7 +75,7 @@ ctx.tools.register(defineTool({ })) ``` -The helper converts the author-facing `SchemaSpec` (with `required: true` as a per-property boolean) to standard JSON Schema for the wire format. Raw JSON-Schema tool definitions (from MCP servers) are still accepted by the registry directly. +The helper converts the author-facing `SchemaSpec` (with `required: true` as a per-property boolean) to standard JSON Schema for the wire format and uses the same typed spec for execute/presentation validation. Raw JSON-Schema tool definitions (from MCP servers) are still accepted by the registry directly. A `defineTool` tool also **validates the model-generated arguments against its `SchemaSpec` before `execute` runs** (`validateArgs`). The model's JSON is untrusted — `InferArgs` is a compile-time claim, not a runtime guarantee — so on a mismatch (missing required key, wrong primitive, bad enum member, nested violation) the tool throws a `ToolArgsError` (`code: 'INVALID_ARGS'`); the registry turns it into an `isError` result whose text lists the violations, which the model sees and self-corrects from. Validation mirrors the JSON Schema conversion exactly: extra keys are allowed, `default` is not applied, and an `object`/`array` prop without `properties`/`items` only type-checks. Raw-registered tools (MCP) are **not** validated by the harness — they validate their own input. @@ -130,15 +130,15 @@ const bash = defineTool({ ### Code Mode -Under `mode: code` (or `both`) the registry turns the tool surface into a programming API, per the [Code Mode RFC](../../../docs/rfc/implemented/feature/2026-06-15-code-mode.md): the model writes a TypeScript program (the body of an async function) and passes it to the ONE wire tool `run_code`; the program runs in `ctx.codeRuntime` (the [code-execution seam](../../code-runtime/README.md) — the shipped backend is a worker thread) with one async binding per registered tool (`await tools.bash({...})`), and ONLY what it prints or returns re-enters the model's context. +Under `mode: code` (or `both`) the registry turns the tool surface into a programming API, per the [Code Mode RFC](../../../docs/rfc/implemented/feature/2026-06-15-code-mode.md): the model writes a TypeScript program (the body of an async function) and passes it to the reserved wire transport `run_code`; the program runs in `ctx.codeRuntime` (the [code-execution seam](../../code-runtime/README.md) — the shipped backend is a worker thread) with one async binding per visible end-capability tool (`await tools.bash({...})`), and ONLY what it prints or returns re-enters the model's context. Scope restrictions change those SDK bindings but cannot remove or replace the transport itself. -- **The SDK section** (`tools:sdk`, order 150): a lazy prompt section regenerating, at each assembly, a `declare const tools: {...}` TypeScript declaration of every registered tool except `run_code` (exotic names via quoted keys), plus fixed usage instructions. Deterministic — lexicographic tool order, byte-identical text for an unchanged tool set (prefix-cache-friendly). The codegen (`jsonSchemaToTs`, exported) is TOTAL: constructs outside the `defineTool` subset degrade to `unknown`, never throw. -- **The dispatch bridge** (`run_code`'s execute): every binding call is JSON-normalized BEFORE dispatch (a value that does not survive — `BigInt`, circulars — rejects that one call, so the dispatched form and the logged form are the same JSON value by construction), serialized through a per-run queue (even `Promise.all` executes the underlying `ctx.tools.execute()` calls one at a time in submission order — the tool contract carries no concurrency-safety metadata yet), gated by `tools/pre-execute`/`tools/post-execute` like any native call (a deny reaches the program as a binding rejection), and logged as one `tool/code-dispatch` session event (log-only: `deriveMessages()` never surfaces it) with the deterministic sub-id `:code:`. A failed sub-call REJECTS the program-side promise with the tool's error text — real code error handling, no bespoke envelope. A sub-call's `additionalContext` is deliberately DROPPED (no safe outlet mid-run without breaking tool-call/result adjacency; deferred until a real hook needs it through Code Mode). +- **The SDK section** (`tools:sdk`, order 150): a lazy prompt section regenerating, at each assembly, a `declare const tools: {...}` TypeScript declaration of the calling scope's visible end capabilities (exotic names via quoted keys), plus fixed usage instructions. Deterministic — lexicographic tool order, byte-identical text for an unchanged tool set (prefix-cache-friendly). The codegen (`jsonSchemaToTs`, exported) is total: constructs outside the `defineTool` subset degrade to `unknown`, never throw. +- **The dispatch bridge** (`run_code`'s execute): every binding call is JSON-normalized before dispatch (a value that does not survive — `BigInt`, circulars — rejects that one call, so the dispatched form and logged form are the same JSON value by construction), serialized through a per-run queue (even `Promise.all` executes underlying calls one at a time in submission order), given the outer execution's opaque token as `parent`, and run through the complete pre-execute → guards → execute → post-execute → result pipeline. A denial reaches the program as a binding rejection, and each sub-call is logged as a `tool/code-dispatch` session event with deterministic id `:code:`; `deriveMessages()` does not surface that event. Token correlation lets commit-style observers defer an inner success until the final `run_code` result without exposing the live outer execution; ordinary tool side effects are not rolled back. A sub-call's `additionalContext` is deliberately dropped because inserting it inside a running parent call would break tool-call/result adjacency. - **Settlement discipline**: the bridge owns a run-scoped abort that follows the outer signal in and fires when the run settles for any reason, so a budget expiry aborts an in-flight sub-tool instead of orphaning it; the bridge then drains its queue BEFORE returning, so every `tool/code-dispatch` lands inside the open turn. A failed run throws `CodeRunFailedError` (`code: 'CODE_RUN_FAILED'`, message = the failure kind + captured logs), which the pipeline converts to a structured `isError` the model self-corrects from. -The wire collapse is the registry's own contribution (`systemPrompt.tools()` is mode-aware), so the logged `request/header` records it for free — under `code`, the assembled tool list is exactly `[run_code]`, pinned by tests and the snapshot goldens. Try it: `pnpm run demo:code-mode` ([the coding-agent example's Code Mode overlay](../../../examples/coding-agent/README.md#code-mode)); `pnpm run demo:code-mode acp` serves the same mode over ACP instead of the REPL. +The wire collapse is the registry's own contribution (`systemPrompt.tools()` is mode-aware), so the logged `request/header` records it for free. When no assembly listener changes the registry's prompt or schema contributions, `code` assembles exactly `[run_code]`, pinned by tests and the snapshot goldens. Try it: `pnpm run demo:code-mode` ([the coding-agent example's Code Mode overlay](../../../examples/coding-agent/README.md#code-mode)); `pnpm run demo:code-mode acp` serves the same mode over ACP instead of the REPL. ### What is NOT here (TODO) -- **Tool shapes review** — when real tools land (e.g. a concurrency-safety hint for parallel execution); phase 1 executes tool calls sequentially. -- **Parallel execution** — the loop currently iterates tool calls sequentially. +- **Concurrency metadata** — tool definitions do not declare whether executions are safe to overlap. +- **Parallel execution** — the loop and Code Mode bridge execute tool calls sequentially until that metadata exists. diff --git a/packages/core/tools/package.json b/packages/core/tools/package.json index f6425538b1..9b4b80d67c 100644 --- a/packages/core/tools/package.json +++ b/packages/core/tools/package.json @@ -23,8 +23,10 @@ "license": "BSD-3-Clause", "peerDependencies": { "@deepseek-ai/dsh-agent": "^0.0.1", + "@deepseek-ai/dsh-user-approval": "^0.0.1", "@deepseek-ai/dsh-code-runtime": "^0.0.1", "@deepseek-ai/dsh-llm": "^0.0.1", + "@deepseek-ai/dsh-scope": "^0.0.1", "@deepseek-ai/dsh-session": "^0.0.1", "@deepseek-ai/dsh-system-prompt": "^0.0.1", "cordis": "^4.0.0-rc.6" @@ -34,8 +36,10 @@ }, "devDependencies": { "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-user-approval": "workspace:^", "@deepseek-ai/dsh-code-runtime": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", + "@deepseek-ai/dsh-scope": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", "@deepseek-ai/dsh-system-prompt": "workspace:^", "cordis": "^4.0.0-rc.6" diff --git a/packages/core/tools/src/code-mode.ts b/packages/core/tools/src/code-mode.ts index a6ef7a271a..323fbeed2b 100644 --- a/packages/core/tools/src/code-mode.ts +++ b/packages/core/tools/src/code-mode.ts @@ -1,12 +1,14 @@ /** * Code Mode: the `run_code` tool and its dispatch bridge. The model writes a - * TypeScript program; the bridge hands it to `ctx.codeRuntime` with one - * async binding per registered tool, serializes every binding call through a - * per-run queue onto `ToolRegistry.execute()` (so `tools/pre-execute` / - * `tools/post-execute` gate sub-calls exactly like native ones), logs each - * sub-dispatch as a `tool/code-dispatch` session event, and returns only the - * program's curated output. The registry itself decides WHEN this tool - * exists (its `mode` config); this module owns only the tool and the bridge. + * TypeScript program; the bridge hands it to `ctx.codeRuntime` with one async + * binding per end capability visible to the calling agent, then serializes + * every binding call through a per-run queue onto `ToolRegistry.execute()`. + * Sub-calls therefore traverse the complete pre/guard/around/post/final-result + * pipeline exactly like native calls and carry the outer execution's opaque + * token for correlation. The bridge logs each sub-dispatch as a + * `tool/code-dispatch` session event and returns only the program's curated + * output. The registry itself decides WHEN this tool exists (its `mode` + * config); this module owns only the tool and the bridge. * * @module @deepseek-ai/dsh-tools/src/code-mode */ @@ -138,7 +140,8 @@ function asRunCodeMeta(meta: unknown): RunCodeMeta | undefined { /** * Build the `run_code` {@link ToolDefinition}: one required `code` parameter, * executed through the dispatch bridge described in the module doc. The - * registry registers it under non-native modes. + * registry reserves it as presentation infrastructure under non-native modes, + * outside the filterable global/scoped capability layers. * @param registry - the owning registry (sub-calls go through its `execute`, * bindings cover its registered tools). * @param requireRuntime - resolves `ctx.codeRuntime` or throws the loud @@ -204,6 +207,7 @@ export function createRunCodeTool(registry: ToolRegistry, requireRuntime: () => name, arguments: normalized.dispatched, ...exec.agent ? { agent: exec.agent } : {}, + parent: exec.token, signal: runController.signal, }) const text = textOf(result.content) @@ -244,7 +248,11 @@ export function createRunCodeTool(registry: ToolRegistry, requireRuntime: () => // silently dropping the binding), and the runtime host resolves // binding names as own properties only. const functions: Record = Object.create(null) as Record - for (const schema of registry.schemas()) { + // Enumerate the CALLING AGENT's visible set (scoped tools join, + // restricted globals vanish) — the same view the SDK section declared, + // so a program can bind exactly what its prompt promised; sub-dispatch + // re-resolves per call through the same view (exec.agent threads down). + for (const schema of registry.schemas(exec.agent)) { if (schema.name === RUN_CODE_NAME) continue Object.defineProperty(functions, schema.name, { enumerable: true, value: binding(schema.name) }) } diff --git a/packages/core/tools/src/index.ts b/packages/core/tools/src/index.ts index e113a0c77c..08edb8d8ae 100644 --- a/packages/core/tools/src/index.ts +++ b/packages/core/tools/src/index.ts @@ -1,15 +1,15 @@ /** * Tool registry and execution pipeline. Plugins register tools; the registry * feeds schemas into the system prompt, and `execute()` dispatches each call - * through `tools/pre-execute` (the allow/deny gate) → `tools/execute` (an - * around-dispatch wrapper for timeout/retry/metrics plugins) → `tools/post-execute` - * (inspect/replace the result, attach context) for sandbox, permission, and hook - * plugins to gate or transform a call. + * through `tools/pre-execute` (the extensible allow/deny gate) → monotonic + * registered guards → `tools/execute` (an around-dispatch wrapper for + * timeout/retry/metrics plugins) → `tools/post-execute` (inspect/replace the + * result, attach context) → the observe-only `tools/result` notification. * * The registry also owns HOW its tools are presented to the model — its * `mode` config: `'native'` (every tool as a wire function definition, - * today's behavior and the default), `'code'` (the wire carries exactly one - * tool, `run_code`, plus a generated TypeScript SDK prompt section), or + * today's behavior and the default), `'code'` (the registry's canonical wire + * contribution is one tool, `run_code`, plus a generated TypeScript SDK prompt section), or * `'both'`. See `code-mode.ts` (the tool + dispatch bridge) and * `ts-types.ts` (the SDK codegen); design in the Code Mode RFC. * @@ -18,11 +18,17 @@ import { Context, Service } from 'cordis' import z from 'schemastery' +import { scopeOf, scopeTarget } from '@deepseek-ai/dsh-scope' +import type { ScopeKey, Scoped } from '@deepseek-ai/dsh-scope' import type { CallId, ContentBlock, ToolSchema } from '@deepseek-ai/dsh-llm' -import { HarnessError } from '@deepseek-ai/dsh-llm' +import { assertNever, deepFreeze, HarnessError } from '@deepseek-ai/dsh-llm' import type { Agent, HookContext } from '@deepseek-ai/dsh-agent' -import type {} from '@deepseek-ai/dsh-system-prompt' +import { snapshotJsonValue } from '@deepseek-ai/dsh-session' +import type { ToolProviderResult } from '@deepseek-ai/dsh-system-prompt' import type { CodeRuntime } from '@deepseek-ai/dsh-code-runtime' +// Type-only: makes `ctx.get('approval')` resolve to the ApprovalService +// augmentation. The seam stays optional at runtime — see `serviceAsk`. +import type {} from '@deepseek-ai/dsh-user-approval' import type { ToolCallView, ToolResultView } from './presentation.ts' import { createRunCodeTool, RUN_CODE_NAME, SDK_SECTION_ORDER } from './code-mode.ts' import { renderToolsSdk } from './ts-types.ts' @@ -83,12 +89,16 @@ declare module 'cordis' { * or return a {@link PreToolDecision} without calling `next()` to * short-circuit. A `deny` skips dispatch and yields an `isError` result; the * tool body never runs. Input rewrite is deliberately NOT offered here (see - * {@link PreToolDecision}); `ask` degrades to deny until the permission - * system lands (`FIXME(permissions)`). + * {@link PreToolDecision}); `ask` is serviced by the `ctx.approval` seam + * when one is mounted, and degrades to deny otherwise. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`) keys the carrier by `exec.agent`: a + * listener registered through `agent.ctx` fires only for that agent's + * calls, while a plain plugin listener fires for every call (including + * agent-less ones, which dispatch subject-less). * @param exec - the pending call (name, parsed arguments, caller agent). * @mode waterfall */ - 'tools/pre-execute'(this: ToolRegistry, exec: ToolExecution, next: () => Promise): Promise + 'tools/pre-execute'(this: Scoped, exec: ToolExecution, next: () => Promise): Promise /** * Around-dispatch waterfall wrapping the registry's core tool dispatch, * between the `tools/pre-execute` gate and the `tools/post-execute` seam. A @@ -99,16 +109,23 @@ declare module 'cordis' { * unknown tool) is already normalized to an `isError` result by the time a * listener's `await next()` returns, so a wrapper never sees a raw throw from * the tool body. This is the seam a timeout/retry/metrics plugin wraps: it can - * mutate `exec` (e.g. replace `exec.signal` with a per-call deadline) BEFORE - * `next()` and inspect the result AFTER. (Cordis `next()` ignores any passed - * arguments and re-invokes downstream with the shared payload, so a wrapper - * mutates `exec` in place rather than passing a new object to `next()`.) + * set or replace the one mutable field, `exec.signal` (e.g. with a per-call + * deadline), BEFORE `next()`, restore/delete it afterward, and inspect the result AFTER. Call identity + * (`token`, `callId`, `name`, `arguments`, `agent`, and `parent`) is immutable throughout the + * pipeline so a wrapper cannot change which tool and scope the pipeline + * accepted. (Cordis `next()` ignores passed arguments and re-invokes + * downstream with the shared payload, so a wrapper changes `exec.signal` in + * place rather than passing a new object to `next()`.) * Multiple listeners compose by registration order — an outer one wraps the * inner ones plus dispatch. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): the carrier is keyed by + * `exec.agent` — a listener registered through `agent.ctx` wraps only that + * agent's calls; a plain plugin listener wraps every call (including + * agent-less ones, which dispatch subject-less). * @param exec - the allowed call about to dispatch (name, parsed arguments, caller agent, signal). * @mode waterfall */ - 'tools/execute'(this: ToolRegistry, exec: ToolExecution, next: () => Promise): Promise + 'tools/execute'(this: Scoped, exec: ToolExecution, next: () => Promise): Promise /** * Waterfall AFTER a tool runs — where hook plugins inspect the result and * accept it (optionally REPLACING the model-facing content, and/or attaching @@ -120,23 +137,45 @@ declare module 'cordis' { * waterfall, all inside `execute`'s outer try/catch (and the tool body keeps * its own inner try/catch, so a thrown tool still reaches `post-execute` as an * `isError` result). + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): the carrier is keyed by + * `exec.agent` — a listener registered through `agent.ctx` fires only for + * that agent's calls; a plain plugin listener fires for every call + * (including agent-less ones, which dispatch subject-less). * @param exec - the call that just ran (name, parsed arguments, caller agent). * @param result - the dispatch outcome a listener may accept, replace, or block. * @mode waterfall */ - 'tools/post-execute'(this: ToolRegistry, exec: ToolExecution, result: ToolExecutionResult, next: () => Promise): Promise + 'tools/post-execute'(this: Scoped, exec: ToolExecution, result: Readonly, next: () => Promise): Promise /** - * A tool was registered or unregistered (the available tool set changed). + * Synchronous notification of the authoritative FINAL tool outcome, after the + * complete pre/execute/post pipeline, final lossless-JSON validation, and + * outer error normalization. + * Unlike the three waterfalls, this seam cannot transform the result: each + * listener receives the now-frozen execution object and a deep-frozen result + * snapshot; listener failures are contained and logged, and + * {@link ToolRegistry.execute} still returns the outcome. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`): keyed by + * `exec.agent`, using the same carrier as the pipeline. + * @param exec - the execution object that traversed the pipeline. + * @param result - a deep-frozen snapshot of the final returned result. + * @mode emit + */ + 'tools/result'(this: Scoped, exec: Readonly, result: Readonly): undefined + /** + * A tool was registered or unregistered, or a scoped restriction changed + * (the available tool set changed — possibly for one scope only). An + * UNFILTERED registry-subject notification, deliberately not scope-filtered + * dispatch: a global change concerns every agent's next assembly, so a + * scoped listener subscribing here sees every change, not just its own + * scope's. * @mode emit */ 'tools/change'(): void } } -// TODO(review): revisit these shapes when the first real tools and -// sandbox/permission plugins land (e.g. a concurrency-safety hint for -// parallel execution — Claude Code partitions read-only tools; phase 1 -// executes sequentially). +// TODO(review): revisit these shapes when concurrency metadata becomes useful +// (for example, a read-only hint that would permit safe parallel execution). /** * What a tool's `execute` returns. The bare {@link ContentBlock}`[]` form is the @@ -195,17 +234,49 @@ export interface ToolResult { meta?: unknown } -/** One pending tool call, as it flows through the execution pipeline (`tools/pre-execute` → dispatch → `tools/post-execute`). */ -export interface ToolExecution { - callId: CallId - name: string - /** Parsed JSON arguments (unknown — tools validate their own input). */ - arguments: unknown +declare const toolExecutionTokenBrand: unique symbol + +/** + * Opaque identity for one trip through the tool pipeline. Nested + * transports carry the enclosing execution's token instead of its live object, + * so observe-only result listeners can correlate calls without gaining a + * mutation path into an outer around-dispatch wrapper. + */ +export type ToolExecutionToken = symbol & { readonly [toolExecutionTokenBrand]: true } + +/** + * Caller-supplied description of one tool call. {@link ToolRegistry.execute} + * adds the registry-owned token to form a pipeline {@link ToolExecution}; + * callers do not choose that token. + */ +export interface ToolExecutionInput { + readonly callId: CallId + readonly name: string + /** Losslessly JSON-serializable parsed arguments (tools validate their own schema). */ + readonly arguments: unknown /** The agent on whose behalf the call runs (set by the agent loop). */ - agent?: Agent + readonly agent?: Agent + /** + * Opaque token of the enclosing transport execution, when one exists. Code + * Mode sets this on SDK sub-dispatches so commit-style observers can wait for + * the outer `run_code` outcome without receiving its live mutable execution. + */ + readonly parent?: ToolExecutionToken signal?: AbortSignal } +/** + * One pending tool call inside the registry pipeline. Parsed arguments cross + * one lossless-JSON materialization boundary before policy and are deep-frozen; + * call identity and the registry-assigned {@link token} are readonly. An + * around-dispatch wrapper may set, replace, or remove `signal`. The registry + * freezes the complete object before `tools/result` observers run. + */ +export interface ToolExecution extends ToolExecutionInput { + /** Registry-assigned identity shared with nested calls only as their opaque `parent` token. */ + readonly token: ToolExecutionToken +} + /** Structured error metadata for a failed tool call (alongside the model-facing text). */ export interface ToolErrorInfo { name: string @@ -236,7 +307,6 @@ export interface ToolExecutionResult { * text in `content` is always present; this is extra structure for code. */ error?: ToolErrorInfo - /** /** * Extra model-facing context a `tools/post-execute` listener attached for the * NEXT request (Claude Code's PostToolUse `additionalContext`). It is NOT part @@ -268,8 +338,9 @@ export interface ToolExecutionResult { * would desync the UI from what RAN. That consistency redesign is its own * `proposed` RFC; `TODO(pre-tool-input-rewrite)` anchors it at the call site.) * - `deny` skips dispatch; the loop records an `isError` result carrying `reason`. - * - `ask` is the permission-prompt intent; until the permission system exists it - * degrades to `deny` (`FIXME(permissions)`). + * - `ask` is the permission-prompt intent: serviced as a one-shot decision by + * the `ctx.approval` seam when one is mounted (`allowed-once` proceeds to + * dispatch; every other outcome denies), degrading to `deny` when none is. */ export type PreToolDecision = | { kind: 'allow' } @@ -299,17 +370,28 @@ export type PostToolDecision = * is stringified. */ function errorMessage(error: unknown): string { - if (error instanceof Error) return error.message - if (typeof error === 'object' && error !== null - && 'message' in error && typeof error.message === 'string') { - return error.message + try { + if (error instanceof Error) return error.message + if (typeof error === 'object' && error !== null + && 'message' in error && typeof error.message === 'string') { + return error.message + } + return String(error) + } catch { + // A hostile thrown value can trap `instanceof`, property access, or string + // coercion. Error normalization is the outermost safety boundary, so its + // fallback must itself be total. + return '' } - return String(error) } /** Structured `{ name, code }` for a thrown HarnessError, else undefined. */ function errorInfo(error: unknown): ToolErrorInfo | undefined { - return error instanceof HarnessError ? { name: error.name, code: error.code } : undefined + try { + return error instanceof HarnessError ? { name: error.name, code: error.code } : undefined + } catch { + return undefined + } } /** How the registry presents its tools to the model (see {@link Config.mode}). */ @@ -319,9 +401,9 @@ export type ToolPresentationMode = 'native' | 'code' | 'both' export interface Config { /** * The presentation mode. `'native'` (the default) contributes every - * registered tool as a wire function definition — byte-for-byte today's - * behavior. `'code'` contributes exactly ONE wire tool, `run_code`, plus - * the generated `tools:sdk` prompt section declaring every other tool as a + * visible end capability as a native wire function definition. Under + * `'code'` this registry contributes exactly ONE wire tool, + * `run_code`, plus the generated `tools:sdk` prompt section declaring every other tool as a * TypeScript API the program calls. `'both'` contributes every native * definition AND `run_code` + the SDK section. Non-native modes require a * loaded `ctx.codeRuntime` whose `language` is `'typescript'` — a missing @@ -334,13 +416,79 @@ export interface Config { mode?: ToolPresentationMode } +/** + * A per-scope restriction over the GLOBAL tool surface, registered via + * {@link ToolRegistry.restrict}. `allow` keeps only the listed global tools; + * `deny` removes the listed ones; both present = allow first, then deny. + * Restrictions never touch scoped registrations — a tool registered through + * the same scope is merged after the global filter (which is what keeps e.g. a + * structured-output capture tool alive under an allow-list). The readonly + * filter values compile to private sets at registration, but resolution uses the live global registry: + * a later global name passes a deny-only filter unless explicitly denied and + * fails an allow-list unless explicitly allowed. The + * reserved `run_code` presentation transport is likewise outside capability + * filtering, and naming it explicitly is rejected. Multiple restrictions on + * one scope compose by intersection: every one must admit. + */ +export interface ToolRestriction { + /** Global tool names that stay visible; everything else is removed. */ + readonly allow?: readonly string[] + /** Global tool names removed from visibility. */ + readonly deny?: readonly string[] +} + +/** One restriction compiled at registration for repeated live-global lookup. */ +interface CompiledToolRestriction { + readonly allow?: ReadonlySet + readonly deny?: ReadonlySet +} + +/** One scope's complete registry view, derived in a single layer traversal. */ +interface ToolView { + /** Visible definitions after restrictions, scoped shadowing, and transport insertion. */ + readonly visible: ReadonlyMap + /** Pre-restriction capability names used by prompt-order validation. */ + readonly knownNames: ReadonlySet + /** Current global names that a scoped restriction may name. */ + readonly restrictableNames: ReadonlySet +} + +/** + * A monotonic execution guard evaluated after every `tools/pre-execute` + * listener and before the tool body. Returning a reason denies the call; + * returning `undefined` leaves it unchanged. Because guards have no allow + * result, listener ordering cannot turn a denial back into permission. + * @param execution - the identity-protected call after extensible pre-execute policy completed. + * @returns a final denial reason, or `undefined` to leave the call allowed. + */ +export type ToolGuard = (execution: Readonly) => string | undefined + +/** One guard registration; the wrapper preserves independent duplicate registrations. */ +interface ToolGuardRegistration { + guard: ToolGuard +} + /** * Tool registry (`ctx.tools`): tool plugins register definitions; the agent - * loop executes calls through the `tools/pre-execute` → `tools/execute` → - * `tools/post-execute` pipeline. The registry contributes its schemas into the - * system-prompt assembly — WHICH schemas is governed by its `mode` config - * (see {@link Config.mode}); under a non-native mode it also registers the - * `run_code` tool and the `tools:sdk` prompt section itself. + * loop executes calls through the `tools/pre-execute` → guards → + * `tools/execute` → `tools/post-execute` → `tools/result` pipeline. The + * registry contributes its schemas into the system-prompt assembly — WHICH + * schemas is governed by its `mode` config + * (see {@link Config.mode}); under a non-native mode it also owns the reserved + * `run_code` presentation transport and the `tools:sdk` prompt section. + * + * Two registration layers (`@deepseek-ai/dsh-scope`): a registration through a + * plain plugin context is GLOBAL (visible to every agent); one through a + * scoped context (`agent.ctx`) is filed in that scope's layer — visible to + * that agent alone, disposed with the scope, and SHADOWING a global tool of + * the same name for that agent (most-specific-wins; within one layer a + * duplicate name still throws). {@link restrict} masks the global layer per + * scope. One private visibility resolver feeds the registry's prompt + * contribution, {@link get}, and {@link execute} — and, under a non-native + * mode, the SDK section and `run_code`'s bindings — so those registry-owned + * presentation and dispatch paths agree. An expert `system-prompt/assemble` + * listener may deliberately replace the final wire composition and owns any + * resulting divergence. */ export class ToolRegistry extends Service { static inject = ['systemPrompt'] @@ -349,44 +497,82 @@ export class ToolRegistry extends Service { mode: z.union(['native', 'code', 'both'] as const).default('native'), }) - private store = new Map() + private global = new Map() + private scoped = new Map>() + /** Compiled restriction filters, per scope (see {@link restrict}). */ + private restrictions = new Map() + /** Monotonic post-policy guards, split into global and per-agent layers. */ + private globalGuards = new Set() + private scopedGuards = new Map>() private readonly mode: ToolPresentationMode + /** Reserved presentation transport, kept outside the filterable registration layers. */ + private readonly codeTransport: ToolDefinition | undefined constructor(ctx: Context, config: Config = {}) { super(ctx, 'tools') // The schema already defaulted an omitted mode; the ?? narrows the // optional-input type for direct (non-Loader) construction in tests. this.mode = config.mode ?? 'native' - ctx.systemPrompt.tools(() => this.wireSchemas()) + // `run_code` is presentation infrastructure, not an end capability. It + // therefore does not enter the global layer: per-agent restrictions must + // not remove it, and a scoped registration must not shadow it. The + // visibility resolver appends this reserved definition after resolving + // the filterable global/scoped capability layers. + this.codeTransport = this.mode === 'native' + ? undefined + : createRunCodeTool(this, () => this.requireCodeRuntime()) + ctx.systemPrompt.tools(context => this.wireSchemas(context.scope)) if (this.mode !== 'native') { - this.register(createRunCodeTool(this, () => this.requireCodeRuntime())) ctx.systemPrompt.section({ name: 'tools:sdk', order: SDK_SECTION_ORDER, - // A lazy thunk over the live store: regenerated at each assembly, in - // lexicographic tool order, so an unchanged tool set renders - // byte-identical text (prefix-cache-friendly) and a mid-session - // registration surfaces exactly like a native-mode tool change. - text: () => { + // A lazy thunk over the live registry, per assembly CONTEXT: + // regenerated at each assembly over the CALLING SCOPE's visible set + // (scoped tools join, restricted globals vanish — the SDK declares + // exactly what that agent's programs can call), in lexicographic + // tool order, so an unchanged tool set renders byte-identical text + // (prefix-cache-friendly) and a mid-session registration surfaces + // exactly like a native-mode tool change. + text: (context) => { this.requireCodeRuntime() - return renderToolsSdk(this.schemas().filter(schema => schema.name !== RUN_CODE_NAME)) + return renderToolsSdk(this.schemas(context.scope).filter(schema => schema.name !== RUN_CODE_NAME)) }, }) } } /** - * The registry's contribution to the wire tool list, per {@link Config.mode}. - * Because `PromptAssembly.tools` is what the loop's request header - * snapshots, the mode's collapse is logged and reconstructable for free. - * Under a non-native mode this is also the loud misconfiguration gate: no - * usable code runtime → every assembly rejects before any model request. + * The registry's contribution to the wire tool list, per {@link Config.mode}, + * as ONE SCOPE sees it (scoped layer joins, shadowing and restrictions + * applied — {@link schemas}). Because `PromptAssembly.tools` is what the + * loop's request header snapshots, the mode's collapse is logged and + * reconstructable for free. Under a non-native mode this is also the loud + * misconfiguration gate: no usable code runtime → every assembly rejects + * before any model request. + * + * The `knownNames` universe distinguishes the two ways a tool can be off + * the wire: a per-scope RESTRICTION is runtime state, so `knownNames` stays + * pre-restriction and a restricted-away tool in `toolOrder` is a normal + * absence — while the MODE collapse is deployment config, so under + * `mode: 'code'` the universe is `[run_code]` and a `toolOrder` naming a + * native tool is dead configuration that fails every assembly loud. Under + * `mode: 'both'`, the provider adds the reserved transport to the + * capability-only known-name universe for `toolOrder` validation. */ - private wireSchemas(): ToolSchema[] { - if (this.mode === 'native') return this.schemas() + private wireSchemas(scope?: ScopeKey): ToolProviderResult { + const view = this.view(scope) + const schemas = [...view.visible.values()].map(definition => this.schemaOf(definition, false)) + if (this.mode === 'native') { + return { schemas, knownNames: [...view.knownNames] } + } this.requireCodeRuntime() - const all = this.schemas() - return this.mode === 'code' ? all.filter(schema => schema.name === RUN_CODE_NAME) : all + if (this.mode === 'code') { + return { + schemas: schemas.filter(schema => schema.name === RUN_CODE_NAME), + knownNames: [RUN_CODE_NAME], + } + } + return { schemas, knownNames: [...view.knownNames, RUN_CODE_NAME] } } /** @@ -409,134 +595,460 @@ export class ToolRegistry extends Service { } /** - * Register a tool. Throws if a tool with the same name is already - * registered. The tool's schema (minus the `execute` function) is - * automatically contributed to the system-prompt assembly. Disposed - * with the calling fiber. Emits `tools/change` on register/unregister. + * Register a tool. The layer is decided by the CALLING context: a plain + * plugin context registers globally; a scoped context (`agent.ctx`) + * registers into that scope's layer — visible to that agent alone, disposed + * with the scope, and shadowing a same-named global tool for that agent. + * Throws if the SAME layer already has the name (cross-layer name twins are + * the shadowing feature, not an error; the global-duplicate message names + * `agent.ctx` as the per-agent alternative), or if a non-native mode reserves + * the `run_code` name for its presentation transport. The visible schema set + * flows into prompt assembly automatically. Definitions are trusted typed + * same-process contributions; JSON materialization happens when the schema or + * result reaches its model/log boundary. Emits `tools/change` on + * register/unregister. * @param definition - the tool's schema plus its execute (and optional * presentation) functions. - * @returns the disposer that unregisters the tool. + * @returns the disposer that unregisters the tool. The exact + * Cordis effect disposer (single-shot): composite (generator) effects may + * yield it directly — exact identity nests the teardown in order. */ register(definition: ToolDefinition): () => void { + const scope = scopeOf(this.ctx) + const name = definition.name + const timeoutMs = definition.timeoutMs + if (timeoutMs !== undefined + && (!Number.isFinite(timeoutMs) || timeoutMs <= 0)) { + throw new TypeError(`tool "${name}" timeoutMs must be a positive finite number`) + } + if (this.codeTransport !== undefined && name === RUN_CODE_NAME) { + throw new Error(`tool name "${RUN_CODE_NAME}" is reserved for the Code Mode presentation transport and cannot be registered or shadowed`) + } const dispose = this.ctx.effect(function* (this: ToolRegistry) { - if (this.store.has(definition.name)) { - throw new Error(`tool "${definition.name}" is already registered`) + const layer = scope === undefined ? this.global : this.layerFor(scope) + if (layer.has(name)) { + throw new Error(scope === undefined + ? `tool "${name}" is already registered (for a per-agent variant, register through that agent's \`agent.ctx\` instead)` + : `tool "${name}" is already registered in this scope`) } - this.store.set(definition.name, definition) + layer.set(name, definition) // Yield the rollback BEFORE emitting `tools/change`: a generator effect // collects each yielded disposer before the next step runs, so a throwing // `tools/change` listener removes the tool instead of leaking it (a leak // would wedge the duplicate-name check until restart). The duplicate // throw above fires before any mutation — it leaks nothing. yield () => { - this.store.delete(definition.name) + layer.delete(name) + // An emptied scope layer is dropped so a disposed scope leaves no + // residue keyed by its (dead) key. + if (scope !== undefined && layer.size === 0) this.scoped.delete(scope) this.ctx.emit('tools/change') } this.ctx.emit('tools/change') }.bind(this), 'tools.register()') - // ctx.effect's disposer returns Promise; our disposer API is - // synchronous fire-and-forget — discard the (always-resolved) promise. - return () => void dispose() + // The EXACT cordis effect disposer, not a wrapper: a composite (generator) + // effect that owns a teardown ORDER must be able to yield THIS function — + // cordis nests a disposer out of the fiber's concurrent sibling list by + // exact function identity, so a wrapper would silently break the nesting + // (the agents.register() lesson). Cleanup is synchronous because this + // registration installs only synchronous state and notifications. + // eslint-disable-next-line @typescript-eslint/no-misused-promises -- synchronous cleanup; direct return preserves disposer identity + return dispose } /** - * Look up a registered tool. - * @param name - the tool name as registered. - * @returns the definition, or undefined when no tool has that name. + * Restrict the GLOBAL tool surface for the calling scope. Must be called + * through a scoped context (`agent.ctx`) — restricting "everyone" is not a + * thing (throw), and an empty filter (neither `allow` nor `deny`) is a no-op + * that can only be a bug (throw — the materialized-empty-config trap). + * Validates every listed name against the CURRENT global end-capability + * universe and throws on an unknown or scope-local name (fail loud + * beats a typo silently filtering nothing) — register restrictions after the + * global tools they mask exist (the agent-creation `setup` window satisfies + * this). A non-native mode's reserved `run_code` presentation transport is + * not a filterable capability; naming it explicitly throws, while omitting + * it from an allow-list cannot remove it. The readonly arrays are compiled to + * private sets at registration. Resolution still uses the live global registry, so a later + * global name passes a deny-only filter unless named and fails an allow-list + * unless named. Multiple restrictions compose by intersection. Scoped + * registrations are merged after restrictions and therefore remain visible. + * Disposed with the calling fiber (revocable independently); emits + * `tools/change`. + * @param filter - global-surface mask: `allow` (keep only) and/or `deny` (remove). + * @returns the disposer that lifts this restriction. The exact + * Cordis effect disposer (single-shot): composite (generator) effects may + * yield it directly — exact identity nests the teardown in order. */ - get(name: string): ToolDefinition | undefined { - return this.store.get(name) + restrict(filter: ToolRestriction): () => void { + const scope = scopeOf(this.ctx) + if (scope === undefined) { + throw new Error('tools.restrict() requires a scoped context (agent.ctx): a context-global restriction would mask every agent — deny the tool for the intended agent instead') + } + const allow = filter.allow + const deny = filter.deny + if (allow === undefined && deny === undefined) { + throw new Error('tools.restrict({}) is a no-op: pass `allow` and/or `deny` (an empty filter is almost always a materialized-empty-config bug)') + } + const compiled: CompiledToolRestriction = { + ...allow !== undefined ? { allow: new Set(allow) } : {}, + ...deny !== undefined ? { deny: new Set(deny) } : {}, + } + if (this.codeTransport !== undefined + && [...allow ?? [], ...deny ?? []].includes(RUN_CODE_NAME)) { + throw new Error(`tools.restrict() cannot name reserved Code Mode presentation transport "${RUN_CODE_NAME}"; restrict end-capability tools instead`) + } + const known = this.view(scope).restrictableNames + const unknown = [...allow ?? [], ...deny ?? []].filter(name => !known.has(name)) + if (unknown.length > 0) { + throw new Error(`tools.restrict() names unknown global tool${unknown.length > 1 ? 's' : ''} ${unknown.map(n => `"${n}"`).join(', ')}; known global tools: ${[...known].sort().join(', ') || '(none)'}`) + } + const dispose = this.ctx.effect(function* (this: ToolRegistry) { + const list = this.restrictions.get(scope) ?? [] + this.restrictions.set(scope, list) + list.push(compiled) + yield () => { + const index = list.indexOf(compiled) + /* v8 ignore next 3 -- defensive: the compiled restriction was pushed, so indexOf is guaranteed >= 0 */ + if (index >= 0) list.splice(index, 1) + if (list.length === 0) this.restrictions.delete(scope) + this.ctx.emit('tools/change') + } + this.ctx.emit('tools/change') + }.bind(this), 'tools.restrict()') + // The EXACT cordis effect disposer, not a wrapper: a composite (generator) + // effect that owns a teardown ORDER must be able to yield THIS function — + // cordis nests a disposer out of the fiber's concurrent sibling list by + // exact function identity, so a wrapper would silently break the nesting + // (the agents.register() lesson). Cleanup is synchronous because this + // registration installs only synchronous state and notifications. + // eslint-disable-next-line @typescript-eslint/no-misused-promises -- synchronous cleanup; direct return preserves disposer identity + return dispose } /** - * Return all registered tool schemas — exactly the model-facing fields - * (`name`, `description`, `parameters`), as sent to the model via the - * system-prompt assembly. Constructed EXPLICITLY rather than by stripping + * Register a monotonic guard after the extensible `tools/pre-execute` + * waterfall. A plain-context guard applies globally; one registered through + * `agent.ctx` applies only to that agent. Any matching guard may deny by + * returning a reason, while no guard can force-allow a call another guard + * denied. The exact effect disposer is returned for ordered ownership and + * HMR cleanup. + * @param guard - synchronous check; a returned string denies the execution. + * @returns the exact disposer that unregisters the guard. + */ + guard(guard: ToolGuard): () => void { + const scope = scopeOf(this.ctx) + const registration = { guard } + const dispose = this.ctx.effect(function* (this: ToolRegistry) { + const layer = scope === undefined ? this.globalGuards : this.guardLayerFor(scope) + layer.add(registration) + yield () => { + layer.delete(registration) + if (scope !== undefined && layer.size === 0) this.scopedGuards.delete(scope) + } + }.bind(this), 'tools.guard()') + // eslint-disable-next-line @typescript-eslint/no-misused-promises -- synchronous cleanup; direct return preserves disposer identity + return dispose + } + + /** The (created-on-demand) scoped layer for `scope`. */ + private layerFor(scope: ScopeKey): Map { + let layer = this.scoped.get(scope) + if (!layer) { + layer = new Map() + this.scoped.set(scope, layer) + } + return layer + } + + /** Get or create the guard layer for one agent scope. */ + private guardLayerFor(scope: ScopeKey): Set { + let layer = this.scopedGuards.get(scope) + if (layer === undefined) { + layer = new Set() + this.scopedGuards.set(scope, layer) + } + return layer + } + + /** First monotonic denial from the global then matching scoped guard layers. */ + private guardReason(exec: ToolExecution): string | undefined { + for (const { guard } of this.globalGuards) { + const reason = guard(exec) + if (reason !== undefined) return reason + } + if (exec.agent !== undefined) { + for (const { guard } of this.scopedGuards.get(exec.agent) ?? []) { + const reason = guard(exec) + if (reason !== undefined) return reason + } + } + return undefined + } + + /** Whether every restriction registered for `scope` admits the global tool `name` (intersection semantics). */ + private admits(scope: ScopeKey | undefined, name: string): boolean { + if (scope === undefined) return true + const filters = this.restrictions.get(scope) + if (!filters) return true + return filters.every(filter => + (filter.allow === undefined || filter.allow.has(name)) + && (filter.deny === undefined || !filter.deny.has(name))) + } + + /** + * Resolve every registry fact one scope needs in one layer traversal. The + * visible map applies global restrictions, scoped shadowing, and the reserved + * presentation transport; the other sets retain the pre-restriction facts + * needed by restriction and prompt-order validation. + * @param scope - the viewing scope (the agent), or undefined for the global view. + * @returns the complete derived view for that scope. + */ + private view(scope?: ScopeKey): ToolView { + const layer = scope === undefined ? undefined : this.scoped.get(scope) + const visible = new Map() + const knownNames = new Set() + const restrictableNames = new Set() + for (const [name, definition] of this.global) { + knownNames.add(name) + restrictableNames.add(name) + if (this.admits(scope, name)) visible.set(name, definition) + } + // Scoped layer second: same-name entries REPLACE (shadow) the global ones, + // and scope-local registrations are never part of the global filter above. + for (const [name, definition] of layer ?? []) { + knownNames.add(name) + visible.set(name, definition) + } + // Presentation infrastructure is resolved last and outside capability + // filtering. Registration rejects this reserved name, so the insertion is + // an invariant assertion as well as protection against future layer changes. + if (this.codeTransport !== undefined) { + visible.set(RUN_CODE_NAME, this.codeTransport) + } + return { visible, knownNames, restrictableNames } + } + + /** + * Look up a tool as one scope sees it (scoped + * shadows global; a restricted-away global reads as absent). Presenters pass + * the calling agent so the rendered card matches the definition that + * actually executed. + * @param name - the tool name as registered. + * @param scope - the viewing scope (the agent); omitted = the global view. + * @returns the definition the scope resolves, or undefined when none is visible. + */ + get(name: string, scope?: ScopeKey): ToolDefinition | undefined { + return this.view(scope).visible.get(name) + } + + /** + * The model-facing schemas of everything `scope` can see — exactly the + * fields (`name`, `description`, `parameters`) this registry contributes to + * system-prompt assembly before its expert transformation waterfall. + * Constructed EXPLICITLY rather than by stripping * known non-schema members: a `ToolDefinition` also carries `execute` and the * optional `presentCall`/`presentResult` UI callbacks, and those (especially * the functions) must never leak into a model request. An allowlist can't * drift when a new non-schema member is added to the definition; a denylist * (rest-destructure) would silently leak it. - * @returns one deep-cloned schema per registered tool, in registration order. + * @param scope - the viewing scope (the agent); omitted = the global view. + * @returns one deep-cloned schema per visible tool. */ - schemas(): ToolSchema[] { - return [...this.store.values()].map(({ name, description, parameters }): ToolSchema => ({ + schemas(scope?: ScopeKey): ToolSchema[] { + return [...this.view(scope).visible.values()].map(definition => this.schemaOf(definition, true)) + } + + /** Project one definition onto the model-facing schema fields. */ + private schemaOf(definition: ToolDefinition, detachParameters: boolean): ToolSchema { + const { name, description, parameters } = definition + return { name, description, - parameters: structuredClone(parameters), - })) + parameters: detachParameters ? structuredClone(parameters) : parameters, + } } /** - * Execute one tool call through the `tools/pre-execute` → `tools/execute` - * (around dispatch) → `tools/post-execute` pipeline. `pre-execute` is the gate - * (allow/deny), `tools/execute` wraps core dispatch (a timeout/retry/metrics + * Execute one tool call through the `tools/pre-execute` → guards → + * `tools/execute` (around dispatch) → `tools/post-execute` → `tools/result` + * pipeline. `pre-execute` is the extensible gate + * (allow/deny/ask), `tools/execute` wraps core dispatch (a timeout/retry/metrics * seam), and `post-execute` is the inspect/transform seam; core dispatch sits * as the base `next()` of the `tools/execute` waterfall. The whole thing is * wrapped in one outer try/catch so a throwing listener (in any waterfall) * becomes an `isError` result instead of failing the turn; the tool body ALSO * keeps its own inner try/catch, so a thrown tool becomes an `isError` result * that `tools/execute` and `post-execute` listeners can still inspect. If the - * tool is not registered, the result is an `isError` carrying a `UNKNOWN_TOOL` - * structured error. A thrown {@link HarnessError} surfaces its `{ name, code }` - * on the result. - * @param exec - the call to run (name, parsed arguments, caller agent, signal). - * @returns the final result after every waterfall; failures resolve as - * `isError` results, never rejections. + * tool is not registered (or not visible to the calling agent — a + * restricted-away global is exactly as absent as a nonexistent one), the + * result is an `isError` carrying a `UNKNOWN_TOOL` structured error. A thrown + * {@link HarnessError} surfaces its `{ name, code }` on the result. Before + * the final observe-only notification, the authoritative outcome is + * materialized as a detached lossless-JSON snapshot; an invalid outcome is + * normalized to an error. + * @param exec - the typed same-process call input. The registry assigns its + * correlation token before policy begins. + * @returns the materialized final result after every waterfall; listener and + * tool failures resolve as `isError` results rather than rejections. */ - async execute(exec: ToolExecution): Promise { + async execute(exec: ToolExecutionInput): Promise { + const token = createExecutionToken() + const callId = exec.callId + const name = exec.name + const agent = exec.agent + const parent = exec.parent + const signal = exec.signal + const base = { + token, + callId, + name, + ...agent !== undefined ? { agent } : {}, + ...parent !== undefined ? { parent } : {}, + ...signal !== undefined ? { signal } : {}, + } + let execution: ToolExecution try { - // --- Gate: tools/pre-execute. A deny (or an ask, which degrades to deny - // until the permission system lands) skips dispatch entirely. --- - const decision = await this.ctx.waterfall( - this, 'tools/pre-execute', exec, - () => Promise.resolve({ kind: 'allow' }), - ) - if (decision.kind !== 'allow') { - // deny → isError. ask has no permission UI yet, so degrade to deny - // (FIXME(permissions)): a forthcoming permission system turns `ask` into - // a real prompt; today it is the conservative "not allowed". - const reason = decision.kind === 'deny' - ? decision.reason - : decision.reason ?? `tool "${exec.name}" requires approval (not yet supported)` - const denied: ToolExecutionResult = { - callId: exec.callId, - content: [{ type: 'text', text: `Error: ${reason}` }], - isError: true, - } - return await this.postExecute(exec, denied) + const detached = snapshotJsonValue(exec.arguments) + if (detached === undefined) { + throw new TypeError('tool execution arguments must be losslessly JSON-serializable') + } + execution = { + ...base, + arguments: deepFreeze(detached), } - - // --- Around-dispatch: tools/execute. The base `next` is the dispatch- - // with-normalization thunk — the tool body's own try/catch turns a throw - // into an isError result so a wrapper (and post-execute) can inspect it; - // an unknown tool routes through the same catch. A `tools/execute` listener - // (e.g. a timeout plugin) wraps this thunk: it may mutate `exec` before - // delegating and inspect the normalized result after. --- - const result = await this.ctx.waterfall( - this, 'tools/execute', exec, - async (): Promise => { - try { - const tool = this.store.get(exec.name) - if (!tool) throw new ToolNotFoundError(exec.name) - // Normalize the two `execute` return shapes: a bare ContentBlock[] (no - // meta) or a { content, meta } object (a tool attaching a private - // presentation payload). An array IS the content; the object carries it. - const returned = await tool.execute(exec.arguments, exec) - const content = Array.isArray(returned) ? returned : returned.content - const meta = Array.isArray(returned) ? undefined : returned.meta - return { callId: exec.callId, content, isError: false, ...meta !== undefined ? { meta } : {} } - } catch (error: unknown) { - return toolErrorResult(exec.callId, error) - } - }, - ) - - return await this.postExecute(exec, result) } catch (error: unknown) { - // Outer backstop: a throwing pre/post-execute listener (or the waterfall - // machinery) becomes an isError result, never a turn failure. - return toolErrorResult(exec.callId, error) + execution = { ...base, arguments: undefined } + const result = this.materializeFinalResult(toolErrorResult(callId, error)) + this.notifyResult(execution, result) + return result + } + let result: ToolExecutionResult + try { + result = this.materializeFinalResult(await this.executePipeline(execution)) + } catch (error: unknown) { + // Outer backstop: a throwing pre/post-execute listener, guard, or the + // waterfall machinery becomes an isError result, never a turn failure. + result = this.materializeFinalResult(toolErrorResult(execution.callId, error)) + } + this.notifyResult(execution, result) + return result + } + + /** Run the transformable pipeline; {@link execute} owns final normalization and notification. */ + private async executePipeline(exec: ToolExecution): Promise { + // --- Gate: tools/pre-execute. An `ask` resolves through the optional + // approval seam (or degrades to deny) before the monotonic guards run. The + // carrier keys dispatch by exec.agent, so an `agent.ctx` listener gates only + // its own agent's calls (agent-less calls are subject-less). + const carrier = scopeTarget(this, exec.agent) + const gate = await this.ctx.waterfall( + carrier, 'tools/pre-execute', exec, + () => Promise.resolve({ kind: 'allow' }), + ) + const decision = gate.kind === 'ask' ? await this.serviceAsk(exec, gate) : gate + const denialReason = decision.kind === 'allow' + ? this.guardReason(exec) + : decision.reason + if (denialReason !== undefined) { + // Every non-grant, including a failed/unavailable approval request, takes + // the same deny path and still reaches post-policy plus result observers. + const denied: ToolExecutionResult = { + callId: exec.callId, + content: [{ type: 'text', text: `Error: ${denialReason}` }], + isError: true, + } + return await this.postExecute(exec, denied) + } + + // --- Around-dispatch: tools/execute. The base `next` is the dispatch- + // with-normalization thunk — the tool body's own try/catch turns a throw + // into an isError result so a wrapper (and post-execute) can inspect it; + // an unknown tool routes through the same catch. A `tools/execute` listener + // (e.g. a timeout plugin) wraps this thunk: it may replace `exec.signal` + // before delegating and inspect the normalized result after. Dispatched with the + // same carrier as the gate, so an `agent.ctx` wrapper wraps only its own + // agent's calls. --- + const result = await this.ctx.waterfall( + carrier, 'tools/execute', exec, + async (): Promise => { + try { + // Resolve through the CALLER's visible view ({@link get}): a scoped + // tool shadows its global name-twin for that agent, and a + // restricted-away global tool is exactly as absent as a nonexistent + // one — same UNKNOWN_TOOL result, no capability leak in the error. + const tool = this.get(exec.name, exec.agent) + if (!tool) throw new ToolNotFoundError(exec.name) + // Normalize the two `execute` return shapes: a bare ContentBlock[] (no + // meta) or a { content, meta } object (a tool attaching a private + // presentation payload). An array IS the content; the object carries it. + const returned = await tool.execute(exec.arguments, exec) + const content = Array.isArray(returned) ? returned : returned.content + const meta = Array.isArray(returned) ? undefined : returned.meta + return { callId: exec.callId, content, isError: false, ...meta !== undefined ? { meta } : {} } + } catch (error: unknown) { + return toolErrorResult(exec.callId, error) + } + }, + ) + if (result.callId !== exec.callId) { + throw new TypeError(`tools/execute returned callId "${String(result.callId)}" for authoritative call "${exec.callId}"`) + } + + return await this.postExecute(exec, result) + } + + /** Notify final-result observers without giving them a mutation/error channel into the outcome. */ + private notifyResult(exec: ToolExecution, result: ToolExecutionResult): void { + // The pipeline is over: freeze the remaining mutable signal slot so every + // observer sees the SAME WeakMap-keyable execution without a mutation race. + Object.freeze(exec) + const callbacks = this.ctx.events.dispatch('emit', [ + scopeTarget(this, exec.agent), 'tools/result', exec, result, + ]) + for (const callback of callbacks) { + try { + callback(exec, result) + } catch (error: unknown) { + this.ctx.logger.warn(`tool "${exec.name}" (${exec.callId}): tools/result observer failed: ${errorMessage(error)}`) + } + } + } + + /** + * Resolve an `ask` decision to allow/deny through the approval seam. The + * seam is consumed opportunistically with `ctx.get('approval')` — a + * deployment that composes no ApprovalService keeps the historical degrade + * to deny, and an unmount mid-session degrades the same way on the next ask. + * An agent-less execution also degrades: without an agent there is no + * session to audit to and no UI to route to. Otherwise the outcome maps + * one-to-one — `allowed-once` proceeds; the three non-grants deny with + * distinct reasons so the model can tell a human "no" from an absent + * approval channel. + */ + private async serviceAsk( + exec: ToolExecution, + ask: Extract, + ): Promise> { + const approval = this.ctx.get('approval') + if (approval === undefined) { + return { kind: 'deny', reason: ask.reason ?? `tool "${exec.name}" requires approval (not yet supported)` } + } + if (exec.agent === undefined) { + return { kind: 'deny', reason: `tool "${exec.name}" requires approval, but the call has no agent to route it through` } + } + const outcome = await approval.request({ + agent: exec.agent, + toolName: exec.name, + callId: exec.callId, + ...ask.reason !== undefined ? { reason: ask.reason } : {}, + ...exec.signal !== undefined ? { signal: exec.signal } : {}, + }) + switch (outcome) { + case 'allowed-once': return { kind: 'allow' } + case 'rejected': return { kind: 'deny', reason: `the user rejected tool "${exec.name}"` } + case 'cancelled': return { kind: 'deny', reason: `approval for tool "${exec.name}" was cancelled` } + case 'unavailable': return { kind: 'deny', reason: `tool "${exec.name}" requires approval, but no approval channel is available` } + default: return assertNever(outcome, 'ApprovalOutcome') } } @@ -549,43 +1061,40 @@ export class ToolRegistry extends Service { * Runs inside `execute`'s outer try/catch (a throwing listener → isError). */ private async postExecute(exec: ToolExecution, result: ToolExecutionResult): Promise { - // Snapshot the protected outcome BEFORE the waterfall. A listener receives - // the same `result` reference, so a post-waterfall read of `result.callId`/ - // `.isError`/`.error` could carry a listener's mutation — violating the - // authoritative-call-id requirement and the "preserve the dispatched - // isError/error" contract. The decision is the ONLY sanctioned channel for a - // listener to change the outcome (block, or accept-with-replacement); the - // call id is always the authoritative `exec.callId`. `content` is copied into - // a fresh array so a listener's in-place `push`/`splice` on `result.content` - // cannot leak into the returned content either (the elements are the same - // references — the snapshot guards the array structure, not deep immutability). - const dispatched = { - callId: exec.callId, - content: [...result.content], - isError: result.isError, - ...result.error ? { error: result.error } : {}, - ...result.meta !== undefined ? { meta: result.meta } : {}, - } const decision = await this.ctx.waterfall( - this, 'tools/post-execute', exec, result, + scopeTarget(this, exec.agent), 'tools/post-execute', exec, result, () => Promise.resolve({ kind: 'accept' }), ) const additionalContext = decision.additionalContext if (decision.kind === 'block') { return { - callId: dispatched.callId, + callId: result.callId, content: decision.feedback, isError: true, ...additionalContext ? { additionalContext } : {}, } } - // accept: replace content if supplied, preserve the dispatched isError/error. + // Accept: replace content if supplied and preserve the dispatched outcome. return { - ...dispatched, + ...result, ...decision.content ? { content: decision.content } : {}, ...additionalContext ? { additionalContext } : {}, } } + + /** Materialize the authoritative commit outcome once, immediately before `tools/result`. */ + private materializeFinalResult(result: ToolExecutionResult): ToolExecutionResult { + const detached = snapshotJsonValue(result) + if (detached === undefined) { + throw new TypeError('tool result must be losslessly JSON-serializable') + } + return deepFreeze(detached) + } +} + +/** Mint a same-process correlation token whose identity is its value. */ +function createExecutionToken(): ToolExecutionToken { + return Symbol('dsh.tool.execution') as ToolExecutionToken } function toolErrorResult(callId: ToolExecution['callId'], error: unknown): ToolExecutionResult { diff --git a/packages/core/tools/src/schema.ts b/packages/core/tools/src/schema.ts index 1a428ffd40..d3a2d32e18 100644 --- a/packages/core/tools/src/schema.ts +++ b/packages/core/tools/src/schema.ts @@ -287,21 +287,21 @@ export function validateArgs(spec: SchemaSpec, args: unknown): string[] { /** Options for {@link defineTool}. */ export interface DefineToolOptions { /** Tool name (must be unique). */ - name: string + readonly name: string /** Human-readable description sent to the model. */ - description: string + readonly description: string /** * Parameter schema using the per-property-required DSL. Converted to * standard JSON Schema at runtime. */ - parameters: S + readonly parameters: S /** * Optional cooperative tool-call timeout budget in milliseconds. When given it * must be a positive finite number; it is attached to the produced * {@link ToolDefinition} for `@deepseek-ai/dsh-timeout-policy` to enforce and * is never sent to the model. */ - timeoutMs?: number + readonly timeoutMs?: number /** * Tool execution function. `args` is typed as {@link InferArgs} — zero * casts needed. Returns either a bare {@link ContentBlock}`[]` (model-facing @@ -353,6 +353,7 @@ export interface DefineToolOptions { * Raw JSON-Schema tool definitions (from MCP servers) are still accepted * by `ToolRegistry.register()` directly — `defineTool` is sugar for * first-party plugin authors. + * * @param options - the tool's name, description, typed parameter schema, * execute body, and optional presenters. * @returns a registry-ready {@link ToolDefinition}: its `execute` validates the diff --git a/packages/core/tools/tests/code-mode.spec.ts b/packages/core/tools/tests/code-mode.spec.ts index f7f4b058d8..f9d84aadbf 100644 --- a/packages/core/tools/tests/code-mode.spec.ts +++ b/packages/core/tools/tests/code-mode.spec.ts @@ -1,11 +1,14 @@ import { describe, expect, it } from 'vitest' import { Context } from 'cordis' import { CallId } from '@deepseek-ai/dsh-llm' +import { createScope } from '@deepseek-ai/dsh-scope' +import type { Scope } from '@deepseek-ai/dsh-scope' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import { CodeRuntime } from '@deepseek-ai/dsh-code-runtime' import type { CodeRunRequest, CodeRunResult } from '@deepseek-ai/dsh-code-runtime' import ToolRegistry, { CodeRunFailedError, RUN_CODE_NAME, defineTool } from '@deepseek-ai/dsh-tools' import type { Config, PostToolDecision, ToolExecutionResult } from '@deepseek-ai/dsh-tools' +import { AgentId } from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import { Session, SessionId } from '@deepseek-ai/dsh-session' import type { SessionEventMap } from '@deepseek-ai/dsh-session' @@ -54,6 +57,15 @@ async function setup(options: SetupOptions = {}) { return { ctx, tools: ctx.tools, systemPrompt: ctx.systemPrompt, runtime: runtime! } } +/** Mint one production-shaped agent scope that can register scoped tool policy. */ +async function mintAgentScope(ctx: Context, name = 'scoped'): Promise<{ scope: Scope; agent: Agent }> { + const agent = { id: AgentId(name) } as Agent + let scope!: Scope + await ctx.plugin(Object.assign((inner: Context) => { scope = createScope(inner, agent) }, + { inject: ['tools', 'systemPrompt'] })) + return { scope, agent } +} + /** Register a trivial echo tool; returns the calls it received. */ function registerEcho(ctx: Context, name = 'echo'): unknown[] { const calls: unknown[] = [] @@ -111,6 +123,35 @@ describe('mode-aware wire contribution', () => { expect(sdk?.text).not.toContain('run_code(args:') }) + it.each(['code', 'both'] as const)('treats expert assembly output as authoritative in mode %s', async (mode) => { + const { ctx, systemPrompt } = await setup({ mode }) + registerEcho(ctx) + ctx.on('system-prompt/assemble', async (_assembly, _context, next) => { + const assembly = await next() + return { + ...assembly, + sections: assembly.sections.filter(section => section.name !== 'tools:sdk'), + tools: assembly.tools.filter(tool => tool.name !== RUN_CODE_NAME), + } + }, { prepend: true }) + + const assembly = await systemPrompt.assemble() + expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(false) + expect(assembly.tools.some(tool => tool.name === RUN_CODE_NAME)).toBe(false) + }) + + it.each(['code', 'both'] as const)('lets one scope shadow the default SDK section in mode %s', async (mode) => { + const { ctx, systemPrompt } = await setup({ mode }) + registerEcho(ctx) + const { scope, agent } = await mintAgentScope(ctx) + scope.ctx.systemPrompt.section({ name: 'tools:sdk', order: 150, text: 'SCOPED SDK' }) + + const scoped = await systemPrompt.assemble({ scope: agent }) + const global = await systemPrompt.assemble() + expect(scoped.sections.find(section => section.name === 'tools:sdk')?.text).toBe('SCOPED SDK') + expect(global.sections.find(section => section.name === 'tools:sdk')?.text).toContain('declare const tools:') + }) + it("mode 'both' contributes every native schema plus run_code, and the SDK section", async () => { const { ctx, systemPrompt } = await setup({ mode: 'both' }) registerEcho(ctx) @@ -119,6 +160,107 @@ describe('mode-aware wire contribution', () => { expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(true) }) + it.each(['code', 'both'] as const)('keeps the run_code transport outside scoped allow-list filtering in mode %s', async (mode) => { + const { ctx, systemPrompt, runtime } = await setup({ mode }) + registerEcho(ctx, 'echo') + registerEcho(ctx, 'hidden') + const { scope, agent } = await mintAgentScope(ctx) + const lift = scope.ctx.tools.restrict({ allow: ['echo'] }) + + const assembly = await systemPrompt.assemble({ scope: agent }) + expect(assembly.tools.map(tool => tool.name)).toEqual(mode === 'code' + ? [RUN_CODE_NAME] + : ['echo', RUN_CODE_NAME]) + const sdk = assembly.sections.find(section => section.name === 'tools:sdk')?.text + expect(sdk).toContain('echo(args:') + expect(sdk).not.toContain('hidden(args:') + + runtime.behavior = request => Promise.resolve({ + logs: [], + value: Object.keys(request.bindings[0]!.functions).sort().join(','), + }) + const result = await runCode(ctx, 'return Object.keys(tools)', { agent }) + expect(result.isError).toBe(false) + expect(result.content).toEqual([{ type: 'text', text: 'echo' }]) + + lift() + const unrestricted = await systemPrompt.assemble({ scope: agent }) + expect(unrestricted.tools.map(tool => tool.name)).toEqual(mode === 'code' + ? [RUN_CODE_NAME] + : ['echo', 'hidden', RUN_CODE_NAME]) + }) + + it.each(['code', 'both'] as const)('keeps the run_code transport outside scoped deny-list filtering in mode %s', async (mode) => { + const { ctx, systemPrompt, runtime } = await setup({ mode }) + registerEcho(ctx, 'denied') + registerEcho(ctx, 'kept') + const { scope, agent } = await mintAgentScope(ctx) + scope.ctx.tools.restrict({ deny: ['denied'] }) + + const assembly = await systemPrompt.assemble({ scope: agent }) + expect(assembly.tools.map(tool => tool.name)).toEqual(mode === 'code' + ? [RUN_CODE_NAME] + : ['kept', RUN_CODE_NAME]) + const sdk = assembly.sections.find(section => section.name === 'tools:sdk')?.text + expect(sdk).not.toContain('denied(args:') + expect(sdk).toContain('kept(args:') + + runtime.behavior = request => Promise.resolve({ + logs: [], + value: Object.keys(request.bindings[0]!.functions).sort().join(','), + }) + const result = await runCode(ctx, 'return Object.keys(tools)', { agent }) + expect(result.isError).toBe(false) + expect(result.content).toEqual([{ type: 'text', text: 'kept' }]) + }) + + it.each(['code', 'both'] as const)('reserves run_code against scoped shadows and explicit restrictions in mode %s', async (mode) => { + const { ctx, systemPrompt } = await setup({ mode }) + const { scope, agent } = await mintAgentScope(ctx) + const impostor = defineTool({ + name: RUN_CODE_NAME, + description: 'Scoped impostor.', + parameters: {}, + execute: () => Promise.resolve([{ type: 'text' as const, text: 'impostor' }]), + }) + + expect(() => scope.ctx.tools.register(impostor)).toThrow(/reserved for the Code Mode presentation transport/) + expect(() => ctx.tools.register(impostor)).toThrow(/reserved for the Code Mode presentation transport/) + expect(() => scope.ctx.tools.restrict({ allow: [RUN_CODE_NAME] })).toThrow(/cannot name reserved Code Mode presentation transport/) + expect(() => scope.ctx.tools.restrict({ deny: [RUN_CODE_NAME] })).toThrow(/cannot name reserved Code Mode presentation transport/) + scope.ctx.systemPrompt.section({ name: 'scoped-note', order: 149, text: 'safe note' }) + scope.ctx.tools.register(defineTool({ + name: 'scoped_safe', + description: 'Safe scoped tool.', + parameters: {}, + execute: () => Promise.resolve([{ type: 'text' as const, text: 'safe' }]), + })) + + const assembly = await systemPrompt.assemble({ scope: agent }) + const transports = assembly.tools.filter(tool => tool.name === RUN_CODE_NAME) + expect(transports).toHaveLength(1) + expect(transports[0]?.description).toContain('Execute a TypeScript program') + expect(assembly.sections.find(section => section.name === 'scoped-note')?.text).toBe('safe note') + expect(assembly.sections.find(section => section.name === 'tools:sdk')?.text).toContain('scoped_safe(args:') + expect(ctx.tools.get(RUN_CODE_NAME, agent)).toBe(ctx.tools.get(RUN_CODE_NAME)) + const result = await runCode(ctx, 'return 1', { agent }) + expect(result.content).toEqual([{ type: 'text', text: '(run_code completed with no output)' }]) + }) + + it.each(['code', 'both'] as const)('keeps run_code in the toolOrder universe without exposing it as a restriction target in mode %s', async (mode) => { + const { ctx, systemPrompt } = await setup({ + mode, + toolOrder: [RUN_CODE_NAME, ''], + }) + registerEcho(ctx) + const { agent } = await mintAgentScope(ctx) + + const assembly = await systemPrompt.assemble({ scope: agent }) + expect(assembly.tools.map(tool => tool.name)).toEqual(mode === 'code' + ? [RUN_CODE_NAME] + : [RUN_CODE_NAME, 'echo']) + }) + it("never exposes run_code to programs, even under mode 'both' (no recursive dispatch path)", async () => { const { ctx, runtime } = await setup({ mode: 'both' }) registerEcho(ctx) @@ -200,6 +342,36 @@ describe('the run_code dispatch bridge', () => { expect(result.meta).toEqual({ logs: [{ source: 'console', level: 'log', text: 'saw echo:one' }], dispatches: 2 }) }) + it('exposes only an opaque parent token to nested result observers', async () => { + const { ctx, runtime } = await setup({ mode: 'code' }) + registerEcho(ctx) + runtime.behavior = async (request) => { + await request.bindings[0]!.functions.echo!({ value: 'nested' }) + return { logs: [], value: 'done' } + } + + // Model a timeout-style outer wrapper: it temporarily installs a signal, + // delegates, then restores the exact prior shape. A nested result observer + // is observe-only and must not receive the live outer execution object; + // freezing the correlation value it sees therefore cannot break restore. + ctx.on('tools/execute', async (exec, next) => { + if (exec.name !== RUN_CODE_NAME) return next() + const previous = exec.signal + exec.signal = new AbortController().signal + const result = await next() + if (previous === undefined) delete exec.signal + else exec.signal = previous + return result + }) + ctx.on('tools/result', (exec) => { + if (exec.parent !== undefined) Object.freeze(exec.parent) + }) + + const result = await runCode(ctx, 'await tools.echo({ value: "nested" })') + expect(result.isError).toBe(false) + expect(result.content).toEqual([{ type: 'text', text: 'done' }]) + }) + it('serializes Promise.all dispatches: tool executions never overlap, in submission order', async () => { const { ctx, runtime } = await setup({ mode: 'code' }) const intervals: [string, string][] = [] @@ -533,16 +705,17 @@ describe('the run_code dispatch bridge', () => { expect(events.filter(event => event.type === 'tool/code-dispatch')).toEqual([]) }) - it('logs the value the tool RECEIVED even when the tool mutates its arguments', async () => { + it('gives the tool and durable log the same immutable argument value', async () => { const { ctx, runtime } = await setup({ mode: 'code' }) const { agent, events } = fakeAgent() + let mutationSucceeded: boolean | undefined ctx.tools.register(defineTool({ name: 'mutator', - description: 'Mutates its own args object.', + description: 'Attempts to mutate its args object.', parameters: { list: { type: 'array', required: true } }, execute(args) { - args.list.push('injected-by-tool') - return Promise.resolve([{ type: 'text' as const, text: 'mutated' }]) + mutationSucceeded = Reflect.set(args.list, 1, 'injected-by-tool') + return Promise.resolve([{ type: 'text' as const, text: 'protected' }]) }, })) runtime.behavior = async (request) => { @@ -551,6 +724,7 @@ describe('the run_code dispatch bridge', () => { } const result = await runCode(ctx, 'program', { agent }) expect(result.isError).toBe(false) + expect(mutationSucceeded).toBe(false) const dispatch = events.find(event => event.type === 'tool/code-dispatch')?.data as SessionEventMap['tool/code-dispatch'] expect(dispatch.arguments).toEqual({ list: ['original'] }) }) diff --git a/packages/core/tools/tests/gen-tool-catalog.spec.ts b/packages/core/tools/tests/gen-tool-catalog.spec.ts index 5c03a69fc4..8bab060a5d 100644 --- a/packages/core/tools/tests/gen-tool-catalog.spec.ts +++ b/packages/core/tools/tests/gen-tool-catalog.spec.ts @@ -35,7 +35,7 @@ describe('gen-tool-catalog collectToolCatalog', () => { it('boots every shipped tool package and harvests its model-facing schemas', async () => { const catalog = await collectToolCatalog() const names = catalog.flatMap(entry => entry.schemas.map(s => s.name)).sort() - expect(names).toEqual(['ask_user_question', 'bash', 'bash_kill', 'bash_output', 'cordis_inspect', 'cordis_mount', 'cordis_unmount', 'edit', 'read', 'run_code', 'subagent', 'todo_write', 'web_fetch', 'web_search', 'workflow', 'write']) + expect(names).toEqual(['ask_user_question', 'bash', 'bash_kill', 'bash_output', 'cordis_inspect', 'cordis_mount', 'cordis_unmount', 'edit', 'read', 'run_code', 'skill', 'subagent', 'todo_write', 'web_fetch', 'web_search', 'workflow', 'write']) // Every tool carries a JSON-Schema `parameters` object (what the model sees). for (const entry of catalog) { for (const schema of entry.schemas) { diff --git a/packages/core/tools/tests/scoped.spec.ts b/packages/core/tools/tests/scoped.spec.ts new file mode 100644 index 0000000000..6599112c2a --- /dev/null +++ b/packages/core/tools/tests/scoped.spec.ts @@ -0,0 +1,594 @@ +import { describe, expect, expectTypeOf, it, vi } from 'vitest' +import { Context } from 'cordis' +import type { Events } from 'cordis' +import { createScope } from '@deepseek-ai/dsh-scope' +import type { Scope } from '@deepseek-ai/dsh-scope' +import SystemPrompt from '@deepseek-ai/dsh-system-prompt' +import ToolRegistry from '@deepseek-ai/dsh-tools' +import type { PreToolDecision, ToolDefinition, ToolExecution, ToolExecutionInput, ToolExecutionToken } from '@deepseek-ai/dsh-tools' +import type { Agent, AgentId } from '@deepseek-ai/dsh-agent' +import { CallId } from '@deepseek-ai/dsh-llm' +import type { ContentBlock } from '@deepseek-ai/dsh-llm' + +/** Mount the registry (with its systemPrompt dependency) on a fresh context. */ +async function mount(): Promise { + const ctx = new Context() + await ctx.plugin(SystemPrompt, {}) + await ctx.plugin(ToolRegistry) + return ctx +} + +/** Mint a scope whose key doubles as a minimal Agent-like object. */ +async function mintAgentScope(ctx: Context, name: string): Promise<{ scope: Scope; key: Agent }> { + const key = { id: name as AgentId } as Agent + let scope!: Scope + // The scoped context resolves services through the MINTING plugin's + // dependency chain — the minter must inject what scope holders will reach + // (in production the agent loop's inject list plays this role). + await ctx.plugin(Object.assign((inner: Context) => { scope = createScope(inner, key) }, + { inject: ['tools', 'systemPrompt'] })) + return { scope, key } +} + +function tool(name: string, reply = `ran:${name}`): ToolDefinition { + return { + name, + description: `tool ${name}`, + parameters: { type: 'object', properties: {} }, + execute: (): Promise => Promise.resolve([{ type: 'text', text: reply }]), + } +} + +async function run(ctx: Context, name: string, agent?: Agent): Promise { + const result = await ctx.tools.execute({ + callId: CallId('c1'), + name, + arguments: {}, + ...agent ? { agent } : {}, + }) + const first = result.content[0] + return first?.type === 'text' ? first.text : JSON.stringify(result.content) +} + +describe('scoped tool registration', () => { + it('keeps final-result observers synchronous', () => { + type ToolResultListener = Events['tools/result'] + type AsyncToolResultListener = () => Promise + + expectTypeOf().not.toExtend() + expectTypeOf>().toEqualTypeOf() + }) + + it('files a scoped tool in its layer: visible/executable for that scope only', async () => { + const ctx = await mount() + const { scope, key } = await mintAgentScope(ctx, 'a') + const other = { id: 'other' as AgentId } as Agent + ctx.tools.register(tool('shared')) + scope.ctx.tools.register(tool('mine')) + + expect(ctx.tools.schemas(key).map(t => t.name).sort()).toEqual(['mine', 'shared']) + expect(ctx.tools.schemas().map(t => t.name)).toEqual(['shared']) + expect(ctx.tools.schemas(other).map(t => t.name)).toEqual(['shared']) + + expect(await run(ctx, 'mine', key)).toBe('ran:mine') + // Out-of-view execution is indistinguishable from a nonexistent tool. + expect(await run(ctx, 'mine', other)).toBe('Error: unknown tool "mine"') + expect(await run(ctx, 'mine')).toBe('Error: unknown tool "mine"') + }) + + it('scoped shadows global on a name conflict, in either registration order', async () => { + const ctx = await mount() + const { scope, key } = await mintAgentScope(ctx, 'a') + // scoped-then-global + scope.ctx.tools.register(tool('bash', 'restricted-bash')) + ctx.tools.register(tool('bash', 'global-bash')) + expect(await run(ctx, 'bash', key)).toBe('restricted-bash') + expect(await run(ctx, 'bash')).toBe('global-bash') + expect(ctx.tools.get('bash', key)?.description).toBe(ctx.tools.get('bash', key)?.description) + // Exactly one 'bash' in the scope's schema view (the shadow, not a double). + expect(ctx.tools.schemas(key).filter(t => t.name === 'bash')).toHaveLength(1) + }) + + it('rejects a duplicate name within one layer, naming agent.ctx for the global case', async () => { + const ctx = await mount() + const { scope } = await mintAgentScope(ctx, 'a') + ctx.tools.register(tool('x')) + expect(() => ctx.tools.register(tool('x'))).toThrow(/agent\.ctx/) + scope.ctx.tools.register(tool('y')) + expect(() => scope.ctx.tools.register(tool('y'))).toThrow(/already registered in this scope/) + }) + + it('disposing the scope unwinds its registrations and leaves no residue', async () => { + const ctx = await mount() + const { scope, key } = await mintAgentScope(ctx, 'a') + scope.ctx.tools.register(tool('mine')) + expect(ctx.tools.get('mine', key)).toBeDefined() + await scope.dispose() + expect(ctx.tools.get('mine', key)).toBeUndefined() + expect(ctx.tools.schemas(key)).toEqual([]) + }) +}) + +describe('restrict()', () => { + it('masks global tools, merges scope-local tools afterward, and keeps assembly with execution', async () => { + const ctx = await mount() + const { scope, key } = await mintAgentScope(ctx, 'a') + ctx.tools.register(tool('read')) + ctx.tools.register(tool('bash')) + scope.ctx.tools.register(tool('capture')) + scope.ctx.tools.restrict({ allow: ['read'] }) + + // The scope-local registration survives the allow-list; the unlisted global is gone. + expect(ctx.tools.schemas(key).map(t => t.name).sort()).toEqual(['capture', 'read']) + expect(await run(ctx, 'bash', key)).toBe('Error: unknown tool "bash"') + expect(await run(ctx, 'read', key)).toBe('ran:read') + expect(await run(ctx, 'capture', key)).toBe('ran:capture') + // Other scopes and the global view are untouched. + expect(ctx.tools.schemas().map(t => t.name).sort()).toEqual(['bash', 'read']) + }) + + it('applies snapshotted filters to the live global registry before merging later scope-local tools', async () => { + const ctx = await mount() + const denied = await mintAgentScope(ctx, 'denied') + const allowed = await mintAgentScope(ctx, 'allowed') + ctx.tools.register(tool('read')) + ctx.tools.register(tool('bash')) + denied.scope.ctx.tools.restrict({ deny: ['bash'] }) + allowed.scope.ctx.tools.restrict({ allow: ['read'] }) + + ctx.tools.register(tool('web')) + denied.scope.ctx.tools.register(tool('denied-local')) + allowed.scope.ctx.tools.register(tool('allowed-local')) + + expect(ctx.tools.schemas(denied.key).map(t => t.name).sort()) + .toEqual(['denied-local', 'read', 'web']) + expect(ctx.tools.schemas(allowed.key).map(t => t.name).sort()) + .toEqual(['allowed-local', 'read']) + expect(await run(ctx, 'web', denied.key)).toBe('ran:web') + expect(await run(ctx, 'web', allowed.key)).toBe('Error: unknown tool "web"') + expect(await run(ctx, 'denied-local', denied.key)).toBe('ran:denied-local') + expect(await run(ctx, 'allowed-local', allowed.key)).toBe('ran:allowed-local') + }) + + it('composes multiple restrictions by intersection and lifts each independently', async () => { + const ctx = await mount() + const { scope, key } = await mintAgentScope(ctx, 'a') + for (const name of ['a', 'b', 'c']) ctx.tools.register(tool(name)) + const liftAllow = scope.ctx.tools.restrict({ allow: ['a', 'b'] }) + scope.ctx.tools.restrict({ deny: ['b'] }) + expect(ctx.tools.schemas(key).map(t => t.name)).toEqual(['a']) + liftAllow() + // The deny remains after the allow-list is lifted. + expect(ctx.tools.schemas(key).map(t => t.name).sort()).toEqual(['a', 'c']) + }) + + it('compiles the readonly filter values at registration', async () => { + const ctx = await mount() + const { scope, key } = await mintAgentScope(ctx, 'a') + ctx.tools.register(tool('a')) + ctx.tools.register(tool('b')) + const filter = { deny: ['a'] } + scope.ctx.tools.restrict(filter) + filter.deny.push('b') + expect(ctx.tools.schemas(key).map(t => t.name)).toEqual(['b']) + }) + + it('fails loud on an unscoped call, an empty filter, and non-global names', async () => { + const ctx = await mount() + const { scope } = await mintAgentScope(ctx, 'a') + ctx.tools.register(tool('real')) + scope.ctx.tools.register(tool('local')) + expect(() => ctx.tools.restrict({ deny: ['real'] })).toThrow(/requires a scoped context/) + expect(() => scope.ctx.tools.restrict({})).toThrow(/no-op/) + expect(() => scope.ctx.tools.restrict({ allow: ['local'] })).toThrow(/unknown global tool "local"/) + expect(() => scope.ctx.tools.restrict({ allow: ['reall'] })).toThrow(/unknown global tool "reall"; known global tools: real/) + expect(() => scope.ctx.tools.restrict({ deny: ['ghost', 'wraith'] })).toThrow(/unknown global tools "ghost", "wraith"/) + + const emptyCtx = await mount() + const { scope: emptyScope } = await mintAgentScope(emptyCtx, 'empty') + expect(() => emptyScope.ctx.tools.restrict({ deny: ['ghost'] })) + .toThrow(/known global tools: \(none\)/) + }) +}) + +describe('scoped execution dispatch', () => { + it('an agent.ctx pre-execute listener gates only its own agent (and never subject-less calls)', async () => { + const ctx = await mount() + const { scope, key } = await mintAgentScope(ctx, 'a') + const other = { id: 'other' as AgentId } as Agent + ctx.tools.register(tool('t')) + + const seen: (string | undefined)[] = [] + scope.ctx.on('tools/pre-execute', (exec: ToolExecution, _next: () => Promise) => { + seen.push(exec.agent?.id) + return Promise.resolve({ kind: 'deny', reason: 'scoped veto' }) + }) + + expect(await run(ctx, 't', key)).toBe('Error: scoped veto') + expect(await run(ctx, 't', other)).toBe('ran:t') + expect(await run(ctx, 't')).toBe('ran:t') + expect(seen).toEqual(['a']) + }) + + it('applies scoped guards after pre-execute and unwinds duplicate registrations independently', async () => { + const ctx = await mount() + const { scope, key } = await mintAgentScope(ctx, 'a') + const other = { id: 'other' as AgentId } as Agent + let bodyCalls = 0 + ctx.tools.register({ + ...tool('t'), + execute: () => { + bodyCalls += 1 + return Promise.resolve([{ type: 'text', text: 'ran:t' }]) + }, + }) + const guard = (execution: Readonly): string => { + expect(Object.isFrozen(execution.arguments)).toBe(true) + return 'terminal policy' + } + const liftFirst = scope.ctx.tools.guard(guard) + scope.ctx.tools.guard(guard) + // Registered later and prepended outside every existing waterfall listener: + // it can force the extensible pre decision to allow, but cannot bypass the + // owner-level monotonic guard that runs after the waterfall. + scope.ctx.on('tools/pre-execute', () => Promise.resolve({ kind: 'allow' }), { prepend: true }) + + expect(await run(ctx, 't', key)).toBe('Error: terminal policy') + expect(await run(ctx, 't', other)).toBe('ran:t') + expect(bodyCalls).toBe(1) + + liftFirst() + expect(await run(ctx, 't', key)).toBe('Error: terminal policy') + await scope.dispose() + expect(await run(ctx, 't', key)).toBe('ran:t') + expect(bodyCalls).toBe(2) + }) + + it('composes global guards monotonically when one abstains and a later one denies', async () => { + const ctx = await mount() + let bodyCalls = 0 + ctx.tools.register({ + ...tool('t'), + execute: () => { + bodyCalls += 1 + return Promise.resolve([]) + }, + }) + ctx.tools.guard(() => undefined) + ctx.tools.guard(() => 'global denial') + + expect(await run(ctx, 't')).toBe('Error: global denial') + expect(bodyCalls).toBe(0) + }) + + it('shares one token and materialized argument value across the pipeline', async () => { + const ctx = await mount() + const { scope, key } = await mintAgentScope(ctx, 'a') + let safeCalls = 0 + let dangerCalls = 0 + let scopedResults = 0 + let safeArguments: unknown + const tokens = new Set() + ctx.tools.register({ + ...tool('safe'), + execute: (args) => { + safeCalls += 1 + safeArguments = args + return Promise.resolve([{ type: 'text', text: 'safe' }]) + }, + }) + ctx.tools.register({ + ...tool('danger'), + execute: () => { + dangerCalls += 1 + return Promise.resolve([{ type: 'text', text: 'danger' }]) + }, + }) + scope.ctx.tools.guard(exec => exec.name === 'danger' ? 'danger denied' : undefined) + ctx.on('tools/pre-execute', (exec, next) => { + tokens.add(exec.token) + expect(Object.isFrozen(exec.arguments)).toBe(true) + return next() + }) + ctx.on('tools/execute', (exec, next) => { + tokens.add(exec.token) + return next() + }) + ctx.on('tools/post-execute', (exec, _result, next) => { + tokens.add(exec.token) + return next() + }) + scope.ctx.on('tools/result', () => { scopedResults += 1 }) + + expect(await run(ctx, 'danger', key)).toBe('Error: danger denied') + const callerArguments = { source: true } + const safeResult = await ctx.tools.execute({ + callId: CallId('safe-call'), + name: 'safe', + arguments: callerArguments, + agent: key, + }) + expect(safeResult.content[0]).toMatchObject({ text: 'safe' }) + expect(Object.isFrozen(callerArguments)).toBe(false) + expect(safeArguments).not.toBe(callerArguments) + expect(Object.isFrozen(safeArguments)).toBe(true) + expect(callerArguments).toEqual({ source: true }) + // One token for danger and one shared by every phase of safe. + expect(tokens.size).toBe(2) + expect({ safeCalls, dangerCalls, scopedResults }).toEqual({ + safeCalls: 1, + dangerCalls: 0, + scopedResults: 2, + }) + }) + + it('normalizes non-cloneable arguments and still publishes one scoped final outcome', async () => { + const ctx = await mount() + const { scope, key } = await mintAgentScope(ctx, 'a') + let policyCalls = 0 + let bodyCalls = 0 + let scopedObserved = 0 + let globalObserved = 0 + ctx.tools.register({ + ...tool('t'), + execute: () => { + bodyCalls += 1 + return Promise.resolve([]) + }, + }) + ctx.on('tools/pre-execute', (_exec, next) => { + policyCalls += 1 + return next() + }) + let parent!: ToolExecutionToken + ctx.tools.register(tool('parent')) + const stopCapture = ctx.on('tools/pre-execute', (exec, next) => { + if (exec.name === 'parent') parent = exec.token + return next() + }) + await ctx.tools.execute({ callId: CallId('parent'), name: 'parent', arguments: {} }) + stopCapture() + policyCalls = 0 + const signal = new AbortController().signal + scope.ctx.on('tools/result', (exec, result) => { + scopedObserved += 1 + expect(exec.arguments).toBeUndefined() + expect(exec.parent).toBe(parent) + expect(exec.signal).toBe(signal) + expect(Object.isFrozen(exec)).toBe(true) + expect(result.isError).toBe(true) + }) + ctx.on('tools/result', () => { globalObserved += 1 }) + const callerArguments = { invalid: () => undefined } + + const scopedResult = await ctx.tools.execute({ + callId: CallId('non-cloneable'), + name: 't', + arguments: callerArguments, + agent: key, + parent, + signal, + }) + const subjectlessResult = await ctx.tools.execute({ + callId: CallId('non-cloneable-subjectless'), + name: 't', + arguments: { invalid: () => undefined }, + }) + expect(scopedResult.isError).toBe(true) + expect(scopedResult.content[0]?.type === 'text' && scopedResult.content[0].text).toContain('losslessly JSON-serializable') + expect(subjectlessResult.isError).toBe(true) + expect({ policyCalls, bodyCalls, scopedObserved, globalObserved }).toEqual({ + policyCalls: 0, + bodyCalls: 0, + scopedObserved: 1, + globalObserved: 2, + }) + expect(Object.isFrozen(callerArguments)).toBe(false) + expect(callerArguments.invalid).toBeTypeOf('function') + }) + + it('reads a stateful parent accessor once before policy, dispatch, and result observation', async () => { + const ctx = await mount() + const observed: (ToolExecutionToken | undefined)[] = [] + ctx.tools.register({ + ...tool('t'), + execute: (_args, exec) => { + observed.push(exec.parent) + return Promise.resolve([{ type: 'text', text: 'ran:t' }]) + }, + }) + ctx.on('tools/pre-execute', (exec, next) => { + observed.push(exec.parent) + return next() + }) + ctx.on('tools/execute', (exec, next) => { + observed.push(exec.parent) + return next() + }) + ctx.on('tools/result', (exec) => { observed.push(exec.parent) }) + const forged = { fake: true } as unknown as ToolExecutionToken + let parentReads = 0 + const input = { + callId: CallId('stateful-parent'), + name: 't', + arguments: {}, + get parent(): ToolExecutionToken | undefined { + parentReads += 1 + return parentReads === 1 ? undefined : forged + }, + } as ToolExecutionInput + + const result = await ctx.tools.execute(input) + + expect(result.isError).toBe(false) + expect(parentReads).toBe(1) + expect(observed).toEqual([undefined, undefined, undefined, undefined]) + }) + + it('uses one input snapshot for the normalized error shell', async () => { + const ctx = await mount() + const { scope, key } = await mintAgentScope(ctx, 'accepted') + const driftAgent = { id: 'drift' as AgentId } as Agent + ctx.tools.register(tool('parent')) + ctx.tools.register(tool('t')) + let parent!: ToolExecutionToken + const stopCapture = ctx.on('tools/pre-execute', (exec, next) => { + if (exec.name === 'parent') parent = exec.token + return next() + }) + await ctx.tools.execute({ callId: CallId('parent'), name: 'parent', arguments: {} }) + stopCapture() + const acceptedSignal = new AbortController().signal + const driftSignal = new AbortController().signal + const forged = { fake: true } as unknown as ToolExecutionToken + const reads = { callId: 0, name: 0, arguments: 0, agent: 0, parent: 0, signal: 0 } + const input = { + get callId() { reads.callId += 1; return CallId('unstable-error') }, + get name() { reads.name += 1; return 't' }, + get arguments(): unknown { reads.arguments += 1; return { invalid: () => undefined } }, + get agent() { reads.agent += 1; return reads.agent === 1 ? key : driftAgent }, + get parent() { reads.parent += 1; return reads.parent <= 2 ? parent : forged }, + get signal() { reads.signal += 1; return reads.signal === 1 ? acceptedSignal : driftSignal }, + } as ToolExecutionInput + let observed: Readonly | undefined + let scopedObserved = 0 + ctx.on('tools/result', (exec) => { observed = exec }) + scope.ctx.on('tools/result', () => { scopedObserved += 1 }) + + const result = await ctx.tools.execute(input) + + expect(result.isError).toBe(true) + expect(reads).toEqual({ callId: 1, name: 1, arguments: 1, agent: 1, parent: 1, signal: 1 }) + expect(scopedObserved).toBe(1) + expect(observed).toMatchObject({ + callId: CallId('unstable-error'), + name: 't', + agent: key, + parent, + signal: acceptedSignal, + }) + expect(Object.isFrozen(observed)).toBe(true) + }) + + it('normalizes a throwing arguments accessor without rereading it or losing the final notification', async () => { + const ctx = await mount() + ctx.tools.register(tool('t')) + let argumentReads = 0 + let observed = 0 + ctx.on('tools/result', (exec, result) => { + observed += 1 + expect(exec.arguments).toBeUndefined() + expect(result.isError).toBe(true) + }) + const input = { + callId: CallId('throwing-arguments'), + name: 't', + get arguments(): unknown { + argumentReads += 1 + throw new Error('getter exploded') + }, + } as ToolExecutionInput + + const result = await ctx.tools.execute(input) + + expect(result.isError).toBe(true) + expect(result.content).toEqual([{ type: 'text', text: 'Error: getter exploded' }]) + expect(argumentReads).toBe(1) + expect(observed).toBe(1) + }) + + it.each([ + ['Map', new Map([['mutable', true]])], + ['class instance', new (class Arguments { value = 1 })()], + ])('rejects cloneable non-JSON arguments (%s) before policy or dispatch', async (_kind, argumentsValue) => { + const ctx = await mount() + let policyCalls = 0 + let bodyCalls = 0 + let observed = 0 + ctx.tools.register({ + ...tool('t'), + execute: () => { + bodyCalls += 1 + return Promise.resolve([]) + }, + }) + ctx.on('tools/pre-execute', (_exec, next) => { + policyCalls += 1 + return next() + }) + ctx.on('tools/result', (exec, result) => { + observed += 1 + expect(exec.arguments).toBeUndefined() + expect(result.isError).toBe(true) + }) + + const result = await ctx.tools.execute({ + callId: CallId('bad-arguments'), name: 't', arguments: argumentsValue, + }) + + expect(result.isError).toBe(true) + expect(result.content).toEqual([{ + type: 'text', text: 'Error: tool execution arguments must be losslessly JSON-serializable', + }]) + expect({ policyCalls, bodyCalls, observed }).toEqual({ policyCalls: 0, bodyCalls: 0, observed: 1 }) + }) + + it('reads nested arguments once into the executed snapshot', async () => { + const ctx = await mount() + ctx.tools.register(tool('t')) + let reads = 0 + const argumentsValue = Object.defineProperty({}, 'value', { + enumerable: true, + get: () => ++reads === 1 ? 'safe' : new Map([['mutable', true]]), + }) + + const result = await ctx.tools.execute({ + callId: CallId('unstable-arguments'), name: 't', arguments: argumentsValue, + }) + + expect(reads).toBe(1) + expect(result).toEqual({ + callId: CallId('unstable-arguments'), + content: [{ type: 'text', text: 'ran:t' }], + isError: false, + }) + }) + + it('notifies every tools/result observer with the frozen final outcome and contains failures', async () => { + const ctx = await mount() + const { scope, key } = await mintAgentScope(ctx, 'a') + ctx.tools.register(tool('t')) + const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => ctx.logger) + const seen: boolean[] = [] + const dispatchModes: string[] = [] + ctx.on('internal/dispatch', (mode, name) => { + if (name === 'tools/result') dispatchModes.push(mode) + }) + ctx.on('tools/execute', async (exec, next) => { + await next() + return { + callId: exec.callId, + content: [{ type: 'text', text: 'outer failure' }], + isError: true, + } + }, { prepend: true }) + scope.ctx.on('tools/result', (_exec, result) => { + expect(Object.isFrozen(_exec)).toBe(true) + expect(Object.isFrozen(_exec.arguments)).toBe(true) + expect(Object.isFrozen(result)).toBe(true) + expect(Object.isFrozen(result.content)).toBe(true) + seen.push(result.isError) + }) + ctx.on('tools/result', () => { + throw { toString: () => { throw new Error('coercion trap') } } + }) + ctx.on('tools/result', (_exec, result) => { seen.push(result.isError) }) + + const result = await ctx.tools.execute({ callId: CallId('final'), name: 't', arguments: {}, agent: key }) + expect(result).toMatchObject({ isError: true, content: [{ type: 'text', text: 'outer failure' }] }) + expect(seen).toEqual([true, true]) + expect(dispatchModes).toEqual(['emit']) + expect(warn).toHaveBeenCalledOnce() + expect(String(warn.mock.calls[0]?.[0])).toContain('') + }) +}) diff --git a/packages/core/tools/tests/tools.spec.ts b/packages/core/tools/tests/tools.spec.ts index cad484640b..e74766c699 100644 --- a/packages/core/tools/tests/tools.spec.ts +++ b/packages/core/tools/tests/tools.spec.ts @@ -2,6 +2,8 @@ import { describe, expect, expectTypeOf, it } from 'vitest' import { Context } from 'cordis' import { CallId, HarnessError } from '@deepseek-ai/dsh-llm' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' +import type { Agent } from '@deepseek-ai/dsh-agent' +import ApprovalService, { type ApprovalOutcome, type ApprovalRequest } from '@deepseek-ai/dsh-user-approval' import ToolRegistry, { defineTool, schemaSpecToJsonSchema, validateArgs, ToolArgsError, ToolNotFoundError, type InferArgs, type SchemaSpec, type PreToolDecision, type PostToolDecision, @@ -113,6 +115,26 @@ describe('ToolRegistry', () => { expect('meta' in result).toBe(false) }) + it('normalizes a contract-violating non-cloneable result before final notification', async () => { + const ctx = await setup() + let observedError: boolean | undefined + ctx.on('tools/result', (_exec, result) => { observedError = result.isError }) + ctx.tools.register({ + ...echoTool, + name: 'bad-meta', + async execute() { + return { content: [], meta: () => undefined } + }, + }) + + const result = await ctx.tools.execute({ + callId: CallId('bad-meta'), name: 'bad-meta', arguments: {}, + }) + expect(result.isError).toBe(true) + expect(result.content[0]?.type === 'text' && result.content[0].text).toContain('Error:') + expect(observedError).toBe(true) + }) + it('returns isError results for unknown tools and throwing tools', async () => { const ctx = await setup() ctx.tools.register({ @@ -134,6 +156,28 @@ describe('ToolRegistry', () => { expect(thrown.content[0]).toMatchObject({ text: 'Error: exploded' }) }) + it('normalizes a hostile thrown value whose inspection and coercion both throw', async () => { + const ctx = await setup() + ctx.tools.register({ + ...echoTool, + name: 'hostile-throw', + async execute() { + throw new Proxy({}, { + getPrototypeOf: () => { throw new Error('prototype trap') }, + has: () => { throw new Error('has trap') }, + get: () => { throw new Error('get trap') }, + }) + }, + }) + + await expect(ctx.tools.execute({ + callId: CallId('hostile'), name: 'hostile-throw', arguments: {}, + })).resolves.toMatchObject({ + isError: true, + content: [{ type: 'text', text: 'Error: ' }], + }) + }) + it('ToolNotFoundError carries the tool name and a stable code', async () => { const { HarnessError } = await import('@deepseek-ai/dsh-llm') const err = new ToolNotFoundError('ghost') @@ -158,7 +202,7 @@ describe('ToolRegistry', () => { expect(result.content[0]).toMatchObject({ text: 'Error: denied by policy' }) }) - it('an ask decision degrades to deny until the permission system lands', async () => { + it('an ask decision degrades to deny when no approval seam is mounted', async () => { const ctx = await setup() ctx.tools.register(echoTool) @@ -181,6 +225,107 @@ describe('ToolRegistry', () => { expect(result.content[0]).toMatchObject({ text: 'Error: tool "echo" requires approval (not yet supported)' }) }) + describe('ask routing through ctx.approval', () => { + /** + * A minimal Agent stand-in — the approval seam reaches + * `agent.session.append` and folds `.events`; the seeded open turn + * satisfies request()'s enclosure precondition. + */ + function fakeAgent(): Agent { + return { + session: { events: [{ type: 'turn/start' }], append: () => ({}) }, + } as unknown as Agent + } + + async function approvalSetup() { + const ctx = await setup() + await ctx.plugin(ApprovalService) + ctx.tools.register(echoTool) + return ctx + } + + it('dispatches the tool when the answerer grants allowed-once, forwarding the ask fields', async () => { + const ctx = await approvalSetup() + const agent = fakeAgent() + const controller = new AbortController() + const seen: ApprovalRequest[] = [] + ctx.on('approval/request', (req) => { + seen.push(req) + return Promise.resolve('allowed-once') + }) + ctx.on('tools/pre-execute', async (_exec, _next): Promise => + ({ kind: 'ask', reason: 'hook wants a human' })) + + const result = await ctx.tools.execute({ + callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' }, agent, signal: controller.signal, + }) + + expect(result).toMatchObject({ isError: false, content: [{ type: 'text', text: 'hi' }] }) + expect(seen).toHaveLength(1) + expect(seen[0]).toMatchObject({ agent, toolName: 'echo', callId: 'c1', reason: 'hook wants a human' }) + expect(seen[0]?.signal).toBe(controller.signal) + }) + + it('denies with the user-rejection reason on rejected', async () => { + const ctx = await approvalSetup() + ctx.on('approval/request', () => Promise.resolve('rejected')) + ctx.on('tools/pre-execute', async (_exec, _next): Promise => ({ kind: 'ask' })) + + const result = await ctx.tools.execute({ callId: CallId('c1'), name: 'echo', arguments: {}, agent: fakeAgent() }) + expect(result.isError).toBe(true) + expect(result.content[0]).toMatchObject({ text: 'Error: the user rejected tool "echo"' }) + }) + + it('denies with the cancellation reason on cancelled', async () => { + const ctx = await approvalSetup() + ctx.on('approval/request', () => Promise.resolve('cancelled')) + ctx.on('tools/pre-execute', async (_exec, _next): Promise => ({ kind: 'ask' })) + + const result = await ctx.tools.execute({ callId: CallId('c1'), name: 'echo', arguments: {}, agent: fakeAgent() }) + expect(result.isError).toBe(true) + expect(result.content[0]).toMatchObject({ text: 'Error: approval for tool "echo" was cancelled' }) + }) + + it('denies with the no-channel reason when the seam is mounted but nobody answers', async () => { + const ctx = await approvalSetup() + ctx.on('tools/pre-execute', async (_exec, _next): Promise => ({ kind: 'ask' })) + + const result = await ctx.tools.execute({ callId: CallId('c1'), name: 'echo', arguments: {}, agent: fakeAgent() }) + expect(result.isError).toBe(true) + expect(result.content[0]).toMatchObject({ text: 'Error: tool "echo" requires approval, but no approval channel is available' }) + }) + + it('denies an agent-less execution without asking — nothing to route or audit through', async () => { + const ctx = await approvalSetup() + let asked = false + ctx.on('approval/request', () => { + asked = true + return Promise.resolve('allowed-once') + }) + ctx.on('tools/pre-execute', async (_exec, _next): Promise => ({ kind: 'ask' })) + + const result = await ctx.tools.execute({ callId: CallId('c1'), name: 'echo', arguments: {} }) + expect(asked).toBe(false) + expect(result.isError).toBe(true) + expect(result.content[0]).toMatchObject({ text: 'Error: tool "echo" requires approval, but the call has no agent to route it through' }) + }) + + it('turns a rogue outcome from a NON-conforming approval stand-in into an isError result', async () => { + // ApprovalService normalizes rogue answers itself; this pins the + // registry's own exhaustiveness backstop by shadowing the service with a + // stand-in that violates the outcome contract. + const ctx = await setup() + ctx.tools.register(echoTool) + ctx.provide('approval', { request: () => Promise.resolve('yolo') } as unknown as ApprovalService) + ctx.on('tools/pre-execute', async (_exec, _next): Promise => ({ kind: 'ask' })) + + const result = await ctx.tools.execute({ callId: CallId('c1'), name: 'echo', arguments: {}, agent: fakeAgent() }) + expect(result.isError).toBe(true) + const text = result.content[0]?.type === 'text' ? result.content[0].text : '' + expect(text).toContain('unreachable') + }) + }) + it('a tools/post-execute listener can replace the result content (accept) ', async () => { const ctx = await setup() ctx.tools.register(echoTool) @@ -233,33 +378,6 @@ describe('ToolRegistry', () => { expect(result.additionalContext).toMatchObject({ content: [{ text: 'fyi' }], source: { kind: 'plugin', plugin: 'test' } }) }) - it('a post-execute listener mutating the result object cannot corrupt callId/isError/error', async () => { - // The decision is the ONLY sanctioned channel to change the outcome. A - // listener that reaches in and mutates the passed result reference (flipping - // isError, rewriting callId, attaching a bogus error) must NOT affect what - // execute() returns — the registry snapshots the authoritative fields before - // the waterfall and rebuilds from the snapshot + decision. - const ctx = await setup() - ctx.tools.register(echoTool) - - ctx.on('tools/post-execute', async (_exec, result, next) => { - const mutable = result as { callId: string; isError: boolean; error?: unknown; content: unknown[] } - mutable.callId = 'hijacked' - mutable.isError = true - mutable.error = { name: 'Evil', code: 'EVIL' } - mutable.content.push({ type: 'text', text: 'INJECTED' }) // in-place array mutation - return next() // delegate to the default accept — no decision-level override - }) - - const result = await ctx.tools.execute({ callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } }) - expect(result.callId).toBe(CallId('c1')) // authoritative exec.callId, not 'hijacked' - expect(result.isError).toBe(false) // the real (successful) dispatch outcome - expect(result.error).toBeUndefined() // no listener-injected error - expect(result.content).toHaveLength(1) // the in-place push did not leak in - expect(result.content[0]).toMatchObject({ text: 'hi' }) - expect(result.content.some(b => (b as { text?: string }).text === 'INJECTED')).toBe(false) - }) - it('composes pre + post waterfalls around dispatch (sandbox-wrap pattern)', async () => { const ctx = await setup() ctx.tools.register(echoTool) @@ -416,6 +534,42 @@ describe('ToolRegistry', () => { expect(result.content[0]).toMatchObject({ text: 'short-circuited' }) }) + it('preserves additionalContext supplied by an around-dispatch result', async () => { + const ctx = await setup() + ctx.tools.register(echoTool) + ctx.on('tools/execute', async exec => ({ + callId: exec.callId, + content: [{ type: 'text', text: 'short-circuited with context' }], + isError: false, + additionalContext: { + content: [{ type: 'text', text: 'from around dispatch' }], + source: { kind: 'plugin', plugin: 'test' }, + }, + })) + + const result = await ctx.tools.execute({ + callId: CallId('around-context'), name: 'echo', arguments: {}, + }) + expect(result.additionalContext).toEqual({ + content: [{ type: 'text', text: 'from around dispatch' }], + source: { kind: 'plugin', plugin: 'test' }, + }) + }) + + it('normalizes a tools/execute result with the wrong call id', async () => { + const ctx = await setup() + ctx.tools.register(echoTool) + ctx.on('tools/execute', async () => ({ callId: CallId('other'), content: [], isError: false })) + + const result = await ctx.tools.execute({ + callId: CallId('malformed-shape'), name: 'echo', arguments: {}, + }) + expect(result.isError).toBe(true) + expect(result.content[0]).toMatchObject({ + text: 'Error: tools/execute returned callId "other" for authoritative call "malformed-shape"', + }) + }) + it('returns an isError result when a tools/execute listener throws', async () => { const ctx = await setup() ctx.tools.register(echoTool) @@ -493,6 +647,14 @@ describe('ToolRegistry', () => { }]) }) + it('rejects a non-positive or non-finite registration timeout', async () => { + const ctx = await setup() + expect(() => ctx.tools.register({ ...echoTool, name: 'zero-timeout', timeoutMs: 0 })) + .toThrow('timeoutMs must be a positive finite number') + expect(() => ctx.tools.register({ ...echoTool, name: 'infinite-timeout', timeoutMs: Number.POSITIVE_INFINITY })) + .toThrow('timeoutMs must be a positive finite number') + }) + it('rejects duplicate names and unregisters on fiber dispose (HMR safety)', async () => { const ctx = await setup() ctx.tools.register(echoTool) @@ -539,6 +701,35 @@ describe('ToolRegistry', () => { dispose() expect(ctx.tools.get('echo')).toBeUndefined() }) + + it('register() returns the EXACT effect disposer: a composite yield nests the teardown in order', async () => { + // The registry-disposer convention (set by agents.register): the returned + // function IS the cordis effect disposer, so a composite (generator) + // effect that yields it has the unregistration run at that yield's LIFO + // position on owner unload. A wrapper would leave the inner effect + // disposing as a CONCURRENT SIBLING of the composite; the async probe + // below (disposed first, LIFO) yields the event loop exactly like the + // agent factory's stop-and-drain link, and a sibling unregistration fires + // in that window — the probe would observe the tool already gone. Pins + // the convention for the whole register-method family (system-prompt + // registrars, registerProvider, setFactory share the same return). + const ctx = await setup() + const order: string[] = [] + const fiber = await ctx.plugin(Object.assign((inner: Context) => { + inner.effect(function* () { + yield () => { order.push('disposed-last') } + yield inner.tools.register({ ...echoTool, name: 'nested' }) + order.push('registered') + yield async () => { + await new Promise(resolve => setTimeout(resolve, 0)) + order.push(inner.tools.get('nested') ? 'first: still registered' : 'first: already gone') + } + }) + }, { inject: ['tools'] })) + await fiber.dispose() + expect(order).toEqual(['registered', 'first: still registered', 'disposed-last']) + expect(ctx.tools.get('nested')).toBeUndefined() + }) }) describe('defineTool / schema DSL', () => { diff --git a/packages/core/tools/tsconfig.json b/packages/core/tools/tsconfig.json index 68edd3b003..c94b270c8c 100644 --- a/packages/core/tools/tsconfig.json +++ b/packages/core/tools/tsconfig.json @@ -28,6 +28,12 @@ }, { "path": "../../core/agent" + }, + { + "path": "../../core/scope" + }, + { + "path": "../../ui/user-approval" } ] } diff --git a/packages/fs/tool-fs/tests/fs-tools.e2e.ts b/packages/fs/tool-fs/tests/fs-tools.e2e.ts index da5412530d..a270e7c6d8 100644 --- a/packages/fs/tool-fs/tests/fs-tools.e2e.ts +++ b/packages/fs/tool-fs/tests/fs-tools.e2e.ts @@ -64,7 +64,7 @@ describe.skipIf(!process.env.DEEPSEEK_API_KEY)('fs tools with-key smoke', () => const sessionDir = await mkdtemp(join(tmpdir(), 'dsh-fs-e2e-session-')) try { ctx = await fsHarness(configDir, SYSTEM) - const handle = ctx.agents.create({ + const handle = await ctx.agents.create({ agentId: AgentId('fs-e2e-cwd'), sessionId: SessionId(`fs-e2e-cwd-${Date.now()}`), meta: { cwd: sessionDir }, diff --git a/packages/fs/tool-fs/tests/tools.spec.ts b/packages/fs/tool-fs/tests/tools.spec.ts index 53f912cce2..c17bd875ba 100644 --- a/packages/fs/tool-fs/tests/tools.spec.ts +++ b/packages/fs/tool-fs/tests/tools.spec.ts @@ -164,11 +164,10 @@ describe('read tool', () => { expect(text(result)).toContain('offset must be a positive integer') }) - it('rejects a fractional or NaN offset, and a zero/negative limit', async () => { + it('rejects a fractional offset and a zero/negative limit', async () => { const { ctx } = await setup() for (const args of [ { file_path: 'a.txt', offset: 1.5 }, - { file_path: 'a.txt', offset: Number.NaN }, { file_path: 'a.txt', limit: 0 }, { file_path: 'a.txt', limit: -3 }, ]) { @@ -178,6 +177,13 @@ describe('read tool', () => { } }) + it('rejects a non-JSON numeric offset before tool-specific validation', async () => { + const { ctx } = await setup() + const result = await call(ctx, 'read', { file_path: 'a.txt', offset: Number.NaN }) + expect(result.isError).toBe(true) + expect(text(result)).toContain('tool execution arguments must be losslessly JSON-serializable') + }) + it('rejects a limit above the cap', async () => { const { ctx } = await setup() const result = await call(ctx, 'read', { file_path: 'a.txt', limit: 99999 }) diff --git a/packages/hooks/hook-protocol/tests/runner.spec.ts b/packages/hooks/hook-protocol/tests/runner.spec.ts index 1972a39c99..6f52198a93 100644 --- a/packages/hooks/hook-protocol/tests/runner.spec.ts +++ b/packages/hooks/hook-protocol/tests/runner.spec.ts @@ -26,6 +26,7 @@ function recordingBash(run: (spec: BashExecSpec) => Promise): { ...request.stdin !== undefined ? { stdin: request.stdin } : {}, ...request.env !== undefined ? { env: request.env } : {}, owner: request.owner, + sandboxMode: request.sandboxMode, } }, async run(spec: BashExecSpec): Promise { diff --git a/packages/hooks/hooks-claude/README.md b/packages/hooks/hooks-claude/README.md index 306bfdbfeb..f97cdb5cfc 100644 --- a/packages/hooks/hooks-claude/README.md +++ b/packages/hooks/hooks-claude/README.md @@ -39,7 +39,7 @@ The hooks **themselves** run in the agent's session workspace: for the agent-sco | `PreToolUse` | `tools/pre-execute` (waterfall) | `deny` → `PreToolDecision.deny`; `ask` → `PreToolDecision.ask` | | `PostToolUse` | `tools/post-execute` (waterfall) | `deny` → `block` with feedback; additionalContext-only → delegate via `next()` then fold context onto the downstream decision (a Code Mode sub-call’s context is dropped by the run_code bridge — see [the pipeline doc](../../../docs/tool-execution-pipeline.md)) | | `Stop` | `agent/turn-continuation` (waterfall) | a blocking Stop hook forces `continue`, feeding its reason as next-step steering | -| `SubagentStart` | `subagent/start` (emit) | additionalContext → `agent.inject()` into the live child | +| `SubagentStart` | `subagent/start` (emit) | additionalContext → `agent.inject()` into a live in-process child; a remote child has no local injection target | | `SubagentStop` | `subagent/end` (emit) | observe-only | The three emit points run detached — no seam awaits a `SessionStart`/`SubagentStart`/`SubagentStop` hook. Each run chain is tracked, and disposing the bridge aborts still-running hook processes, then drains the continuations before the dispose resolves (`createDetachedRuns` in `dsh-hook-protocol`). diff --git a/packages/hooks/hooks-claude/tests/coverage.spec.ts b/packages/hooks/hooks-claude/tests/coverage.spec.ts index f06cdcf6b4..7feb46df05 100644 --- a/packages/hooks/hooks-claude/tests/coverage.spec.ts +++ b/packages/hooks/hooks-claude/tests/coverage.spec.ts @@ -448,7 +448,7 @@ describe('hooks-claude coverage — continue:false, context arm, no-cwd', () => const ctx = await harness(path, adapter) // NB: no projectDir // The factory create() path honors meta.cwd (the plain agentLoop.create() does not). const { SessionId } = await import('@deepseek-ai/dsh-session') - const handle = ctx.agents.create({ agentId: AgentId('a1'), sessionId: SessionId('s1'), meta: { cwd: workspace }, agentOptions: { model: 'mock' } }) + const handle = await ctx.agents.create({ agentId: AgentId('a1'), sessionId: SessionId('s1'), meta: { cwd: workspace }, agentOptions: { model: 'mock' } }) handle.agent.send([{ type: 'text', text: 'go' }]) await waitForIdle(ctx, handle.agent as ReactLoopAgent) expect(events(handle.agent as ReactLoopAgent).some(e => e.type === 'context/message' @@ -613,7 +613,7 @@ describe('hooks-claude coverage — hook runs in the session cwd, not the server ctx.tools.register(defineTool({ name: 'echo', description: 'e', parameters: {}, async execute() { return [{ type: 'text', text: 'ok' }] } })) const { SessionId } = await import('@deepseek-ai/dsh-session') - const handle = ctx.agents.create({ agentId: AgentId('a1'), sessionId: SessionId('s1'), meta: { cwd: sessionDir }, agentOptions: { model: 'mock' } }) + const handle = await ctx.agents.create({ agentId: AgentId('a1'), sessionId: SessionId('s1'), meta: { cwd: sessionDir }, agentOptions: { model: 'mock' } }) handle.agent.send([{ type: 'text', text: 'go' }]) await waitForIdle(ctx, handle.agent as ReactLoopAgent) @@ -649,7 +649,7 @@ describe('hooks-claude coverage — hook runs in the session cwd, not the server // Register a live child on its own session cwd; emit subagent/end with its id. const { SessionId } = await import('@deepseek-ai/dsh-session') - const childHandle = ctx.agents.create({ agentId: AgentId('child-stop'), sessionId: SessionId('child-stop-session'), meta: { cwd: childDir }, agentOptions: { model: 'mock' } }) + const childHandle = await ctx.agents.create({ agentId: AgentId('child-stop'), sessionId: SessionId('child-stop-session'), meta: { cwd: childDir }, agentOptions: { model: 'mock' } }) ctx.emit('subagent/end', { provider: 'inproc', id: childHandle.agent.id, stopReason: 'completed' }) await waitFor(() => existsSync(marker)) diff --git a/packages/hooks/hooks-codex/tests/coverage.spec.ts b/packages/hooks/hooks-codex/tests/coverage.spec.ts index c83137df53..774b308fa1 100644 --- a/packages/hooks/hooks-codex/tests/coverage.spec.ts +++ b/packages/hooks/hooks-codex/tests/coverage.spec.ts @@ -553,7 +553,7 @@ describe('hooks-codex coverage — decision mapping paths', () => { ctx.llm.registerAdapter(['mock'], adapter) ctx.tools.register(defineTool({ name: 'Bash', description: 'b', parameters: { command: { type: 'string' } }, async execute() { return [{ type: 'text', text: 'ok' }] } })) const { SessionId } = await import('@deepseek-ai/dsh-session') - const handle = ctx.agents.create({ agentId: AgentId('a1'), sessionId: SessionId('s1'), meta: { cwd: sessionDir }, agentOptions: { model: 'mock' } }) + const handle = await ctx.agents.create({ agentId: AgentId('a1'), sessionId: SessionId('s1'), meta: { cwd: sessionDir }, agentOptions: { model: 'mock' } }) handle.agent.send([{ type: 'text', text: 'go' }]) await waitForIdle(ctx, handle.agent as ReactLoopAgent) expect(existsSync(marker)).toBe(true) diff --git a/packages/sandbox/README.md b/packages/sandbox/README.md new file mode 100644 index 0000000000..9e104192d1 --- /dev/null +++ b/packages/sandbox/README.md @@ -0,0 +1,12 @@ +# sandbox/ — process-sandbox capability family + +The confinement half of the [capability-seam split](../../docs/rfc/implemented/architecture/2026-06-13-capability-seams.md): an abstract provider interface and platform backends. Consumers hand `ctx.sandbox` the exact argv they are about to spawn and spawn the returned (wrapped) argv instead; policy (`SandboxPolicy`: mode + workspace root) rides each call, so different consumers confine under different policies at the same instant. All **product** packages. + +| Package | Role | ctx key | +|---|---|---| +| `sandbox/` | Abstract process-sandbox seam (the `SandboxProvider` contract + the mode/enforcement/policy vocabulary) | `ctx.sandbox` | +| `sandbox-local/` | Local backends by platform chain: Linux `bwrap` else the `landlock-run` launcher (the npm-distributed [`node-addon-landlock-run`](https://www.npmjs.com/package/node-addon-landlock-run) family, built and released from its own repository), darwin `sandbox-exec`/Seatbelt — multi-candidate chains functionally probed, sole candidates selected directly, verdict cached, fail-closed | (registers `ctx.sandbox`) | + +The seam confines SAME-WORLD subprocesses only (shared filesystem and kernel). Containers, microVMs, and remote executors are NOT backends here — they replace whole capability implementations (`ctx.bash`, `ctx.fs`) as environment-coherent groups; the boundary is recorded in [the sandbox RFC](../../docs/rfc/implemented/feature/2026-07-06-sandbox.md). + +Consumers today: [`bash/bash-sandbox`](../bash/bash-sandbox/) (wraps `['bash', '-c', command]`; see [examples/sandbox-acp-agent](../../examples/sandbox-acp-agent/) for the composed leaf). In-process tools (fs/web) cannot be confined by an OS wrapper — their sandbox semantics are policy at their own seams (the sandbox RFC's cross-family phase). diff --git a/packages/sandbox/sandbox-local/README.md b/packages/sandbox/sandbox-local/README.md new file mode 100644 index 0000000000..abf7511438 --- /dev/null +++ b/packages/sandbox/sandbox-local/README.md @@ -0,0 +1,18 @@ +# @deepseek-ai/dsh-sandbox-local + +Local implementation of the [`@deepseek-ai/dsh-sandbox`](../sandbox/) seam: wraps a caller's argv in a platform confinement runner. Selection is BY PLATFORM, resolved once and cached: each platform names its runner chain, a chain of one is selected directly (probing arbitrates between candidates — a sole candidate leaves nothing to arbitrate), and a chain of several is probed functionally in preference order. Linux: [`bwrap`](https://github.com/containers/bubblewrap) when its probe passes, else the [`landlock-run`](https://www.npmjs.com/package/node-addon-landlock-run) Landlock launcher (kernel confinement that needs no userns/mount privileges — see the [sandbox RFC](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md) for the prebuilt-binary decision and profile-parity notes); darwin: `sandbox-exec` speaking a Seatbelt (SBPL) profile, unprobed. A platform with no chain means `confine()` FAILS CLOSED with the seam's structured `SANDBOX_UNAVAILABLE` error (win32 today: a reserved, deliberately empty chain awaiting an AppContainer-family runner); an unprobed runner that turns out unusable fails closed at EXECUTION instead — it refuses to run the command, and every wrap's `runnerFailureSignatures` let the consumer classify that as a sandbox failure rather than a task failure. Never a silent unconfined passthrough on any path. + +Policy is per call (`SandboxPolicy`: mode + workspace root); the provider holds only the mechanism and the cached ladder verdict. Every wrap reports the selected runner's `enforcement` (`full`, or `partial` on an older Landlock ABI that governs only a subset of accesses — read from the launcher's `--probe` report line) and its `denialSignatures` — the stderr dialect that rung's kernel speaks on a denied file effect (EROFS text under bwrap, EACCES under Landlock, EPERM under Seatbelt), which stderr-inferring consumers match instead of a cross-runner union. A non-empty `runnerCommand` config is the operator's assertion of a runner that fully enforces the bwrap-shaped profile: the ladder and probes are skipped (the wrap carries both Linux denial dialects, the mechanism being unknown) — also the deterministic fake-runner seam for keyless test tiers. Its runner-failure dialect is the OUTER shell's argv0-scoped failure shapes (`exec: : not found`, `: No such file or directory`, `: Permission denied`) — the consumer re-joins the wrap through `bash -c 'exec …'`, so a missing or unexecutable configured runner classifies as a sandbox failure (fail closed at execution), never as a failing command or a denial. `probeTimeoutMs` (default 5000) bounds each functional probe, the escape hatch for hosts slow enough that a timed-out probe would otherwise misread as `SANDBOX_UNAVAILABLE`. + +The Seatbelt profile is allow-default with `(deny file-write*)` plus write allow-lists, so exactly the mode's promised file effects are governed: `read-only` grants the `/dev/null` literal alone; `workspace-write` adds the workspace root, `/tmp`, and the per-user darwin temp dir (`os.tmpdir()` — the platform's real temp area for mkstemp-family tools), every root canonicalized because Seatbelt matches resolved paths (`/tmp` IS `/private/tmp`). Apple marks the `sandbox-exec` CLI deprecated but ships it on every macOS; the functional probe is what fails closed if that ever changes. + +The Landlock launcher comes from the npm package family [`node-addon-landlock-run`](https://www.npmjs.com/package/node-addon-landlock-run) — an entry package (this package's one runtime dependency) plus per-platform binary packages selected by npm's `os`/`cpu` fields, built and released from [its own repository](https://github.com/deepseek-harness/node-addon-landlock-run). The entry package owns the launcher's CLI contract: `launcherPath()` resolution (a host with no platform package yields a never-existing path whose probe fails exactly like an unenforcing kernel), the functional `probe()`, and `grantArgs()` flag spelling — versioned together with the binary, so probe-report parsing can never drift against it. This provider keeps only the policy side: the mode → grants mapping (`landlockProfileArgs`) and the ladder. The consumer path is rehearsed by `tests/packed-install.e2e.ts`: pack THIS package's closure, install into a throwaway consumer with the launcher family coming from the registry, assert the installed binary executable (a stripped mode bit must not masquerade as a non-enforcing kernel), and confine through it under plain `node`. + +Every rung has its keyless world-proof (`tests/bwrap.e2e.ts`, `tests/landlock.e2e.ts`, `tests/seatbelt.e2e.ts`), each self-skipping where its runner is absent; CI's `sandbox-e2e` matrix runs all of them against real kernels (bwrap plus one Landlock leg per architecture on Linux, Seatbelt on macOS) and fails on a silent all-skip. + +```yaml +- id: sandbox + name: '@deepseek-ai/dsh-sandbox-local' +``` + +Consumers: [`@deepseek-ai/dsh-bash-sandbox`](../../bash/bash-sandbox/); see [`examples/sandbox-acp-agent`](../../../examples/sandbox-acp-agent/) for the runnable composition. diff --git a/packages/sandbox/sandbox-local/package.json b/packages/sandbox/sandbox-local/package.json new file mode 100644 index 0000000000..d2f8b34161 --- /dev/null +++ b/packages/sandbox/sandbox-local/package.json @@ -0,0 +1,38 @@ +{ + "name": "@deepseek-ai/dsh-sandbox-local", + "description": "Local process-sandbox backends for the DeepSeek Harness sandbox seam: bwrap, the npm-distributed landlock-run launcher, or macOS Seatbelt — functionally probed, fail-closed", + "version": "0.0.1", + "private": true, + "type": "module", + "main": "lib/index.js", + "types": "lib/types/index.d.ts", + "exports": { + ".": { + "types": "./lib/types/index.d.ts", + "default": "./lib/index.js" + }, + "./src/*": "./src/*", + "./package.json": "./package.json" + }, + "files": [ + "lib/index.js", + "lib/types/**/*.d.ts", + "lib/types/**/*.d.ts.map", + "src" + ], + "license": "BSD-3-Clause", + "peerDependencies": { + "@deepseek-ai/dsh-llm": "^0.0.1", + "@deepseek-ai/dsh-sandbox": "^0.0.1", + "cordis": "^4.0.0-rc.6" + }, + "dependencies": { + "node-addon-landlock-run": "0.0.0-test.0", + "schemastery": "^3.18.0" + }, + "devDependencies": { + "@deepseek-ai/dsh-llm": "workspace:^", + "@deepseek-ai/dsh-sandbox": "workspace:^", + "cordis": "^4.0.0-rc.6" + } +} diff --git a/packages/sandbox/sandbox-local/src/index.ts b/packages/sandbox/sandbox-local/src/index.ts new file mode 100644 index 0000000000..66f3077319 --- /dev/null +++ b/packages/sandbox/sandbox-local/src/index.ts @@ -0,0 +1,478 @@ +/** + * `LocalSandboxProvider`: the local implementation of the + * `@deepseek-ai/dsh-sandbox` seam. Wraps a caller's argv in a platform + * confinement runner selected BY PLATFORM: each platform names its runner + * chain ({@link PLATFORM_CHAINS}), a chain of one is selected directly (no + * probe — there is nothing to arbitrate), and a chain of several is probed + * FUNCTIONALLY in preference order (build and enforce a real profile once, + * not `--version`), the verdict cached for the provider's lifetime. Linux: + * `bwrap`, else the `landlock-run` Landlock launcher (kernel confinement + * that needs no userns/mount privileges; distributed as the npm package + * family `node-addon-landlock-run` — the decision recorded in + * docs/rfc/implemented/feature/2026-07-06-sandbox.md); darwin: macOS + * `sandbox-exec` speaking a Seatbelt (SBPL) profile, unprobed. + * When the platform has no chain or no candidate passes, + * {@link LocalSandboxProvider.confine} FAILS CLOSED with the seam's + * structured `SANDBOX_UNAVAILABLE` error instead of passing the argv + * through unconfined; an unusable runner selected WITHOUT a probe fails + * closed at execution time instead (it refuses to run the command), which + * the wrap's `runnerFailureSignatures` let consumers classify as a sandbox + * failure rather than a task failure. + * + * @module @deepseek-ai/dsh-sandbox-local + */ + +import { spawnSync } from 'node:child_process' +import { realpathSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { grantArgs as landlockGrantArgs, LAUNCHER_BIN, launcherPath as landlockLauncherPath, probe as defaultProbeLandlock } from 'node-addon-landlock-run' +import { Context } from 'cordis' +import z from 'schemastery' +import { assertNever } from '@deepseek-ai/dsh-llm' +import { SandboxProvider, SandboxUnavailableError } from '@deepseek-ai/dsh-sandbox' +import type { ConfinedArgv, ConfinedSandboxMode, SandboxEnforcement, SandboxPolicy } from '@deepseek-ai/dsh-sandbox' + +/** Plugin config. All optional — `static Config` supplies the defaults. */ +export interface Config { + /** + * Override the sandbox runner argv (the bwrap-shaped profile arguments are + * appended). A NON-EMPTY argv is the operator's assertion that this runner + * exists and FULLY enforces the profile (confinement reports + * `enforcement: 'full'`, and — the runner's kernel mechanism being unknown + * — carries both Linux file-denial dialects as its denial signatures) — + * the runner chain and its probes are skipped, + * and a broken runner fails loudly at execution time. The operator also + * supplies {@link runnerFailureSignatures}, which distinguish the runner + * refusing its profile from the wrapped command failing normally. + * Absent (or empty — the schema normalizes an omitted array to `[]`): the + * built-in platform chains — Linux `bwrap` then the Landlock launcher + * (probed in that order), darwin `sandbox-exec` (the sole candidate, + * selected without a probe). Used for custom/alternative runners and + * for deterministic fake runners in keyless test tiers. + */ + runnerCommand?: string[] + /** + * Case-insensitive stderr substrings emitted when a configured + * {@link runnerCommand} refuses its profile before executing the wrapped + * command. Required and non-empty with `runnerCommand`; rejected without + * it. Missing/unexecutable runner errors are added automatically from + * `runnerCommand[0]`, while these signatures cover an executable runner's + * own failure dialect. + */ + runnerFailureSignatures?: string[] + /** + * Per-probe timeout in milliseconds for the chain's functional probes + * (default: 5000; must be a positive finite number — Node treats a 0 + * `spawnSync` timeout as UNBOUNDED, so 0 is rejected at construction). A + * probe that exceeds it reads as an unusable rung, so a + * host slow enough to trip the default — cold NFS mounts, heavily loaded + * CI — would otherwise be misclassified `SANDBOX_UNAVAILABLE` with no + * config escape. Bounds ONE probe, and the chain walk runs each at most once + * per provider lifetime. + */ + probeTimeoutMs?: number +} + +/** + * The `bwrap` profile arguments for one policy. The whole host tree is bound + * read-only; a fresh `/dev` keeps `>/dev/null` redirects working and a fresh + * `/proc` keeps process-inspecting tools working. `workspace-write` + * additionally mounts an ephemeral writable `/tmp` and rebinds the workspace + * root read-write (bind order matters: later binds overlay earlier ones). + * Deliberately NO `--unshare-pid` (it would break the process-group kill + * semantics shell consumers rely on) and NO network unsharing (the seam's + * mode vocabulary promises file effects only). + * @param policy - the file-effect policy to express as bwrap arguments. + * @returns the bwrap profile arguments (before the trailing `--` + argv). + */ +export function bwrapProfileArgs(policy: SandboxPolicy): string[] { + const args = ['--ro-bind', '/', '/', '--dev', '/dev', '--proc', '/proc', '--die-with-parent'] + if (policy.mode === 'workspace-write') { + args.push('--tmpfs', '/tmp') + args.push('--bind', policy.workspaceRoot, policy.workspaceRoot) + } + return args +} + +/** + * The `landlock-run` grant arguments for one policy — the bwrap + * profile's file-effect semantics expressed as a Landlock allow-list + * (Landlock cannot mount, so there are no fresh/ephemeral filesystems). The + * whole tree is readable and executable; of `/dev`, ONLY `/dev/null` is + * writable — a whole-`/dev` grant would expose real host paths beneath it + * (`/dev/shm`, a shared tmpfs) to persistent writes, which `read-only` + * promises never happen. bwrap can hand out a fresh ephemeral `/dev`; on the + * host's own `/dev` the write grant must be node-by-node, and `>/dev/null` + * is the one redirects need. `workspace-write` adds the HOST `/tmp` (shared + * and persistent, where bwrap's is ephemeral — the honest difference, + * recorded in the sandbox RFC's runner notes) plus the workspace + * root read-write. The flag spelling belongs to `node-addon-landlock-run`'s + * `grantArgs`; this function owns only the policy → grants mapping. + * @param policy - the file-effect policy to express as launcher grants. + * @returns the launcher grant arguments (before `--` + argv). + */ +export function landlockProfileArgs(policy: SandboxPolicy): string[] { + const readWrite = ['/dev/null'] + if (policy.mode === 'workspace-write') { + readWrite.push('/tmp', policy.workspaceRoot) + } + return landlockGrantArgs({ readOnly: ['/'], readWrite }) +} + +/** + * Resolve a granted root to the path the kernel actually sees. Seatbelt path + * filters match the CANONICAL path (symlinks resolved), and the roots this + * profile grants are symlinked on every macOS: `/tmp` is `/private/tmp` and + * the user temp dir lives under `/var` → `/private/var` — an as-spelled + * grant would match nothing. + */ +function canonicalPath(path: string): string { + try { + return realpathSync(path) + } catch { + // realpathSync failed: the path (or a prefix) is missing or unreadable. + // Grant the spelling as-is — an unresolvable root matches nothing until + // it exists, which is the conservative outcome, and inventing a fallback + // resolution here would grant a path the caller never named. + return path + } +} + +/** Quote one path as an SBPL string literal (backslashes and double quotes escaped). */ +function sbplString(path: string): string { + return `"${path.replaceAll('\\', String.raw`\\`).replaceAll('"', String.raw`\"`)}"` +} + +/** + * The `sandbox-exec` arguments for one policy: `-p` plus a Seatbelt (SBPL) + * profile with the same file-effect semantics as the other dialects, built + * as allow-default → `(deny file-write*)` → write allow-list (later rules + * win), so exactly the mode's promised file effects are governed — network + * and process visibility stay unrestricted, which is all the seam's mode + * vocabulary claims. Of `/dev`, ONLY the `/dev/null` literal is writable + * (the same node-not-directory reasoning as the Landlock grant). + * `workspace-write` adds the workspace root, the host `/tmp`, and the + * per-user darwin temp dir (`os.tmpdir()`, launchd's `TMPDIR`, inherited by + * the confined child) — on darwin that directory IS the platform's `/tmp` + * for every mkstemp-family tool, so omitting it would deny the mode's + * promised temp area. All granted roots are canonicalized because Seatbelt + * matches resolved paths ({@link canonicalPath}); duplicates after + * resolution collapse. + * @param policy - the file-effect policy to express as an SBPL profile. + * @returns the `sandbox-exec` arguments (`-p` + profile, before `--` + argv). + */ +export function seatbeltProfileArgs(policy: SandboxPolicy): string[] { + const forms = ['(version 1)', '(allow default)', '(deny file-write*)', `(allow file-write* (literal ${sbplString('/dev/null')}))`] + if (policy.mode === 'workspace-write') { + const roots = [...new Set([policy.workspaceRoot, '/tmp', tmpdir()].map(canonicalPath))] + forms.push(`(allow file-write* ${roots.map(root => `(subpath ${sbplString(root)})`).join(' ')})`) + } + return ['-p', forms.join(' ')] +} + +/** + * Functional `bwrap` probe: can it actually build the read-only profile on + * this host? (`--version` alone would miss a disabled unprivileged user + * namespace.) Synchronous by design — it runs once, lazily, before the first + * confined wrap, and the chain's verdict is cached for the provider's + * lifetime. `timeoutMs` bounds the probe (the `probeTimeoutMs` config). + * The Landlock rung needs no such helper: resolution (`launcherPath`) and + * the functional probe (`probe`) come from `node-addon-landlock-run`, the + * package family that ships the launcher binary itself, so the probe-report + * parsing can never drift against the binary. + */ +function defaultProbeBwrap(timeoutMs: number): boolean { + const probe = spawnSync('bwrap', ['--ro-bind', '/', '/', '--dev', '/dev', '--proc', '/proc', '--die-with-parent', '--', 'true'], { + timeout: timeoutMs, + stdio: 'ignore', + }) + return probe.status === 0 +} + +/** + * Functional Seatbelt probe: apply the real `read-only` profile through + * `sandbox-exec -p` and run `true` under it — exit 0 means the kernel + * accepted and enforced the profile (`sandbox-exec` exits non-zero when + * `sandbox_init` refuses it). A missing `sandbox-exec` (every non-macOS + * host) fails the spawn and probes `unusable`, exactly like the other + * rungs' absent binaries. Apple marks the CLI deprecated but ships it on + * every macOS; if it ever disappears, this probe is what fails closed. + */ +function defaultProbeSeatbelt(seatbeltExec: string, timeoutMs: number): boolean { + const probe = spawnSync(seatbeltExec, [...seatbeltProfileArgs({ mode: 'read-only', workspaceRoot: '/' }), '--', 'true'], { + timeout: timeoutMs, + stdio: 'ignore', + }) + return probe.status === 0 +} + +/** Test seam: inject probe verdicts / a fake launcher / a platform without real runners. */ +export interface SandboxInternals { + /** Replaces `process.platform` for chain selection (exercise any platform's chain from any host). */ + platform?: string + /** Replaces the platform's chain wholesale (walk mechanics — e.g. probing a rung the product chains only reach unprobed). */ + chain?: readonly SelectedRunner['runner'][] + /** Replaces the functional `bwrap` probe (the Linux chain's first rung). */ + probeBwrap?: () => boolean + /** Replaces the functional Landlock launcher probe (the Linux chain's second rung). */ + probeLandlock?: (launcher: string) => SandboxEnforcement | 'unusable' + /** Replaces the functional Seatbelt probe (the darwin chain's sole rung — only consulted if that chain ever grows). */ + probeSeatbelt?: (seatbeltExec: string) => boolean + /** Replaces the resolved `landlock-run` launcher path (a fake launcher script). */ + landlockLauncher?: string + /** Replaces the `sandbox-exec` executable the probe and wraps invoke (a fake script). */ + seatbeltExec?: string +} + +/** The chain's verdict: which runner confines, and how completely it enforces. */ +type SelectedRunner = { runner: 'bwrap' | 'landlock' | 'seatbelt'; enforcement: SandboxEnforcement } + +/** + * The runner chain per platform — selection is BY PLATFORM first, probes + * second: a platform's chain is probed in preference order only when it has + * MORE than one candidate (probing arbitrates; it does not re-validate a + * choice that has no alternative). A platform with no chain fails closed at + * `confine()`. Linux prefers `bwrap` (its mount profile is closest to the + * mode vocabulary) over the Landlock launcher; darwin has exactly one + * candidate, selected without any probe. + */ +const PLATFORM_CHAINS: Record = { + linux: ['bwrap', 'landlock'], + darwin: ['seatbelt'], + // Reserved slot, deliberately empty: Windows support fills it with a + // confinement runner (AppContainer / restricted-token family, shipped from + // its own repository on the landlock-run template) plus a + // SelectedRunner['runner'] union member — the switches' assertNever guards + // then walk the implementer to every site. An empty chain fails closed at + // confine(), identical to an unlisted platform: reserving the slot never + // weakens the fail-closed end. + win32: [], +} + +/** + * Enforcement completeness a rung claims when selected WITHOUT a probe (a + * chain of one). `bwrap` and Seatbelt govern every promised file effect by + * construction, so the claim is a profile fact; `landlock` is listed for the + * table's totality but is unreachable unprobed today (the Linux chain has + * two rungs, so it is only ever selected through its probe, whose report is + * what distinguishes full from per-ABI-partial — and the launcher additionally + * self-reports partial enforcement on stderr at every confined run). + */ +const STATIC_ENFORCEMENT: Record = { + bwrap: 'full', + landlock: 'full', + seatbelt: 'full', +} + +/** + * A probe bound must be a positive finite number: Node treats + * `spawnSync({ timeout: 0 })` as NO timeout, so an unvalidated 0 would + * silently mean "unbounded" — the opposite of what the field promises. + */ +function assertPositiveFinite(name: string, value: number): void { + if (!Number.isFinite(value) || value <= 0) { + throw new Error(`sandbox-local: ${name} must be a positive finite number`) + } +} + +/** + * The denial dialect each runner's kernel speaks — the case-insensitive + * stderr substrings a denied file effect produces under it, carried on every + * wrap (the seam's `ConfinedArgv.denialSignatures`). Kernel facts, not + * tunables: bwrap denies through its read-only bind mounts (EROFS), Landlock + * refuses with EACCES, Seatbelt with EPERM — whose text is also what + * non-file EPERM boundaries print, the residual imprecision the consumer's + * conservative classifier documents. An operator-configured `runnerCommand` + * has an unknown kernel mechanism, so its wraps carry both Linux file-denial + * dialects; bare EPERM stays excluded there (it names non-file boundaries + * the mode vocabulary does not govern). + */ +const DENIAL_SIGNATURES = { + bwrap: ['read-only file system'], + landlock: ['permission denied'], + seatbelt: ['operation not permitted'], + runnerCommand: ['read-only file system', 'permission denied'], +} as const satisfies Record + +/** + * How each runner's OWN failure identifies itself on stderr (the seam's + * `ConfinedArgv.runnerFailureSignatures`): every runner prefixes its error + * lines with its program name, and the shell's runner-not-found message + * carries the same `name: ` shape (`bash: bwrap: command not found`, + * `bash: …/bin/landlock-run: No such file or directory`) — so one substring + * per runner covers both "runner broke" and "runner missing". Consumers + * match these BEFORE the denial dialect: a runner's error text can contain + * denial words (an unopenable grant root reports `Permission denied`), and + * a runner failure means the command never ran at all. + */ +const RUNNER_FAILURE_SIGNATURES = { + bwrap: ['bwrap: '], + landlock: [`${LAUNCHER_BIN}: `], + seatbelt: ['sandbox-exec: '], +} as const satisfies Record + +/** + * Local process-sandbox provider. Registers as `ctx.sandbox`. Stateless + * apart from the cached chain verdict — it spawns nothing but the one-time + * probes, so there is no disposal work beyond cordis' own. + */ +export class LocalSandboxProvider extends SandboxProvider { + // Inline schema call: the config catalog walks `static Config` statically. + static Config: z = z.object({ + runnerCommand: z.array(z.string()).default([]), + runnerFailureSignatures: z.array(z.string()).default([]), + probeTimeoutMs: z.natural().default(5_000), + }) + + /** Test seam (mirrors the bash executors' `internals`). */ + internals: SandboxInternals = {} + + private readonly runnerCommand: string[] | undefined + private readonly configuredRunnerFailureSignatures: string[] + private readonly probeTimeoutMs: number + /** Cached chain verdict; undefined until the first confined wrap needs it. */ + private selectedRunner: SelectedRunner | 'unavailable' | undefined + + constructor(ctx: Context, config: Config) { + super(ctx) + // The schema (static Config) defaults every field — the casts record + // those runtime facts. An empty runnerCommand means "not configured": + // use the platform chain. + const runner = config.runnerCommand as string[] + const runnerFailureSignatures = config.runnerFailureSignatures as string[] + if (runner.length === 0 && runnerFailureSignatures.length > 0) { + throw new Error('sandbox-local: runnerFailureSignatures requires runnerCommand') + } + if (runner.length > 0 && runnerFailureSignatures.length === 0) { + throw new Error('sandbox-local: runnerCommand requires at least one runnerFailureSignatures entry') + } + if (runnerFailureSignatures.some(signature => signature.trim().length === 0)) { + throw new Error('sandbox-local: runnerFailureSignatures entries must be non-empty') + } + this.runnerCommand = runner.length > 0 ? runner : undefined + this.configuredRunnerFailureSignatures = runnerFailureSignatures + this.probeTimeoutMs = config.probeTimeoutMs as number + assertPositiveFinite('probeTimeoutMs', this.probeTimeoutMs) + } + + /** + * Wrap `argv` in the selected runner's invocation for `policy` — the + * configured `runnerCommand` when present (the operator's assertion, no + * probe), else the platform chain's runner speaking its own profile + * dialect. Every wrap carries the runner's enforcement completeness, its + * denial dialect, and its runner-failure signatures. + * @param argv - the exact argv the caller is about to spawn. + * @param policy - the file-effect policy this execution runs under. + * @returns the wrapped argv plus the selected backend's enforcement + * completeness, denial signatures, and runner-failure signatures; + * throws the fail-closed `SANDBOX_UNAVAILABLE` error when the platform + * has no usable runner. + */ + confine(argv: readonly string[], policy: SandboxPolicy): ConfinedArgv { + if (this.runnerCommand !== undefined) { + const argv0 = this.runnerCommand[0] as string + return { + argv: [...this.runnerCommand, ...bwrapProfileArgs(policy), '--', ...argv], + enforcement: 'full', + denialSignatures: DENIAL_SIGNATURES.runnerCommand, + // The operator names the configured runner's OWN pre-exec refusal + // dialect; the consumer additionally re-joins the wrap through an + // outer `bash -c 'exec …'`, so we can add the missing/unexecutable + // outer-shell shapes ourselves. Scoping every automatic shape to + // argv0 keeps in-command errors out (a bare `exec:`/`Permission + // denied` prefix would claim tool output; `exec: : not found` + // cannot). The residual text-collision trade is documented by the + // seam's conservative classifier contract. + runnerFailureSignatures: [ + ...this.configuredRunnerFailureSignatures, + `exec: ${argv0}: not found`, + `${argv0}: No such file or directory`, + `${argv0}: Permission denied`, + ], + } + } + const selected = this.selectRunner(policy.mode) + return { + argv: [...this.runnerArgv(selected.runner, policy), '--', ...argv], + enforcement: selected.enforcement, + denialSignatures: DENIAL_SIGNATURES[selected.runner], + runnerFailureSignatures: RUNNER_FAILURE_SIGNATURES[selected.runner], + } + } + + /** The selected rung's runner invocation (program + profile arguments) for one policy. */ + private runnerArgv(runner: SelectedRunner['runner'], policy: SandboxPolicy): string[] { + switch (runner) { + case 'bwrap': return ['bwrap', ...bwrapProfileArgs(policy)] + case 'landlock': return [this.landlockLauncher(), ...landlockProfileArgs(policy)] + case 'seatbelt': return [this.seatbeltExec(), ...seatbeltProfileArgs(policy)] + default: return assertNever(runner) + } + } + + /** + * Resolve which runner confines commands, once, for the provider's + * lifetime: this platform's chain ({@link PLATFORM_CHAINS}), its sole + * candidate selected directly, multiple candidates arbitrated by + * functional probes in chain order. Fail closed when the platform has no + * chain or no candidate passes — the command never runs. + */ + private selectRunner(mode: ConfinedSandboxMode): SelectedRunner { + this.selectedRunner ??= this.chainVerdict() + if (this.selectedRunner === 'unavailable') throw new SandboxUnavailableError(mode) + return this.selectedRunner + } + + /** Walk this platform's chain: sole candidate unprobed, several probed in order, none usable → unavailable. */ + private chainVerdict(): SelectedRunner | 'unavailable' { + const chain = this.internals.chain ?? PLATFORM_CHAINS[this.internals.platform ?? process.platform] ?? [] + const [first, ...rest] = chain + if (first === undefined) return 'unavailable' + // One candidate = nothing to arbitrate: select it without probing. Its + // runner fails closed at EXECUTION time if unusable (refuses to run the + // command), and the wrap's runnerFailureSignatures let the consumer + // classify that as a sandbox failure — never a silent unconfined run, + // never a plain task failure. + if (rest.length === 0) return { runner: first, enforcement: STATIC_ENFORCEMENT[first] } + for (const runner of chain) { + const enforcement = this.probeRunner(runner) + if (enforcement !== 'unusable') return { runner, enforcement } + } + return 'unavailable' + } + + /** One rung's functional probe (each at most once, via the chain walk). */ + private probeRunner(runner: SelectedRunner['runner']): SandboxEnforcement | 'unusable' { + // bwrap's mount profile and Seatbelt's deny-file-write* profile govern + // every promised file effect by construction, so their passing probes + // are always full enforcement; only the Landlock launcher's probe report + // distinguishes full from per-ABI-partial. + switch (runner) { + case 'bwrap': { + const probe = this.internals.probeBwrap ?? (() => defaultProbeBwrap(this.probeTimeoutMs)) + return probe() ? 'full' : 'unusable' + } + case 'landlock': { + const probe = this.internals.probeLandlock ?? (launcher => defaultProbeLandlock(launcher, { timeoutMs: this.probeTimeoutMs })) + return probe(this.landlockLauncher()) + } + case 'seatbelt': { + const probe = this.internals.probeSeatbelt ?? (exec => defaultProbeSeatbelt(exec, this.probeTimeoutMs)) + return probe(this.seatbeltExec()) ? 'full' : 'unusable' + } + default: return assertNever(runner) + } + } + + /** The Landlock launcher to probe and exec (test seam over the resolved one). */ + private landlockLauncher(): string { + return this.internals.landlockLauncher ?? landlockLauncherPath() + } + + /** The `sandbox-exec` executable to probe and exec (test seam over the system one). */ + private seatbeltExec(): string { + return this.internals.seatbeltExec ?? 'sandbox-exec' + } +} + +export default LocalSandboxProvider diff --git a/packages/sandbox/sandbox-local/tests/bwrap.e2e.ts b/packages/sandbox/sandbox-local/tests/bwrap.e2e.ts new file mode 100644 index 0000000000..da6e683da1 --- /dev/null +++ b/packages/sandbox/sandbox-local/tests/bwrap.e2e.ts @@ -0,0 +1,118 @@ +import { spawnSync } from 'node:child_process' +import { existsSync, readFileSync, rmSync } from 'node:fs' +import { mkdtemp, rm } from 'node:fs/promises' +import { homedir, tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { Context } from 'cordis' +import type { SandboxPolicy } from '@deepseek-ai/dsh-sandbox' +import { bwrapProfileArgs, LocalSandboxProvider } from '@deepseek-ai/dsh-sandbox-local' + +/** + * KEYLESS bwrap integration proof for the BACKEND: the REAL `bwrap` confining + * REAL processes through `confine()` + a direct spawn of the returned argv. + * Nothing is forced off: bwrap is the ladder's FIRST rung, so a passing probe + * selects it naturally — the wrap shape assertion pins that. Verifies the + * WORLD (files exist or don't) and that the kernel's denial text matches the + * dialect the wrap advertises; the through-`ctx.bash` consumer proof lives + * with `@deepseek-ai/dsh-bash-sandbox`. + * + * Self-skips wherever the functional probe fails — no `bwrap` on PATH, or a + * host that denies unprivileged user namespaces (the probe is the same + * profile the provider enforces, so skip conditions match runtime exactly). + * + * Workspaces for the workspace-write tests live under the HOME directory on + * purpose: bwrap's `/tmp` is an EPHEMERAL mount (the documented + * bwrap-profile difference — pinned by its own test below), so only a + * workspace OUTSIDE `/tmp` proves the workspace-root rebind itself. + */ + +const probe = spawnSync('bwrap', [...bwrapProfileArgs({ mode: 'read-only', workspaceRoot: '/' }), '--', 'true'], { timeout: 5_000, stdio: 'ignore' }) +const bwrapUsable = probe.status === 0 + +let ctx: Context | undefined +const tempDirs: string[] = [] +const tempFiles: string[] = [] + +afterEach(async () => { + await ctx?.fiber.dispose() + ctx = undefined + await Promise.all(tempDirs.splice(0).map(dir => rm(dir, { recursive: true, force: true }))) + for (const file of tempFiles.splice(0)) rmSync(file, { force: true }) +}) + +async function tempDir(base: string): Promise { + const dir = await mkdtemp(join(base, 'dsh-bwrap-e2e-')) + tempDirs.push(dir) + return dir +} + +async function provider(): Promise { + ctx = new Context() + await ctx.plugin(LocalSandboxProvider, {}) + return ctx.sandbox as LocalSandboxProvider +} + +/** Confine a shell command under `policy` and run it for real; returns the spawn result and the wrap's facts. */ +function runConfined(sandbox: LocalSandboxProvider, command: string, policy: SandboxPolicy) { + const confined = sandbox.confine(['bash', '-c', command], policy) + const result = spawnSync(confined.argv[0] as string, confined.argv.slice(1), { timeout: 30_000, encoding: 'utf8' }) + return { result, confined } +} + +describe.skipIf(!bwrapUsable)('sandbox-local: real bwrap confinement', () => { + it('the passing probe selects the bwrap rung naturally — first in the ladder, full enforcement, EROFS dialect', async () => { + const workdir = await tempDir(tmpdir()) + const sandbox = await provider() + const confined = sandbox.confine(['true'], { mode: 'read-only', workspaceRoot: workdir }) + expect(confined.argv[0]).toBe('bwrap') + expect(confined.enforcement).toBe('full') + expect(confined.denialSignatures).toEqual(['read-only file system']) + }) + + it('read-only denies a write — the file must NOT exist, and the kernel speaks the advertised dialect', async () => { + const workdir = await tempDir(tmpdir()) + const sandbox = await provider() + const { result } = runConfined(sandbox, `echo hi > ${workdir}/denied.txt`, { mode: 'read-only', workspaceRoot: workdir }) + expect(result.status).not.toBe(0) + // The wrap's denialSignatures must be what the kernel actually prints. + expect(result.stderr.toLowerCase()).toContain('read-only file system') + expect(existsSync(join(workdir, 'denied.txt'))).toBe(false) + }) + + it('read-only keeps the tree readable/executable and the fresh /dev/null writable', async () => { + const workdir = await tempDir(tmpdir()) + const sandbox = await provider() + const { result } = runConfined(sandbox, 'ls / > /dev/null && echo dev-ok', { mode: 'read-only', workspaceRoot: workdir }) + expect(result.status).toBe(0) + expect(result.stdout).toBe('dev-ok\n') + }) + + it('workspace-write lands a write inside the workspace root and still denies one beside it', async () => { + const workdir = await tempDir(homedir()) + const outside = await tempDir(homedir()) + const sandbox = await provider() + + const inside = runConfined(sandbox, `printf bwrap-ok > ${workdir}/allowed.txt`, { mode: 'workspace-write', workspaceRoot: workdir }) + expect(inside.result.status).toBe(0) + expect(readFileSync(join(workdir, 'allowed.txt'), 'utf8')).toBe('bwrap-ok') + + const denied = runConfined(sandbox, `echo hi > ${outside}/denied.txt`, { mode: 'workspace-write', workspaceRoot: workdir }) + expect(denied.result.status).not.toBe(0) + expect(existsSync(join(outside, 'denied.txt'))).toBe(false) + }) + + it('workspace-write mounts an EPHEMERAL /tmp: the write succeeds inside, the host /tmp stays untouched', async () => { + // The documented bwrap-profile difference: Landlock and Seatbelt grant + // the HOST temp areas, bwrap swaps in a fresh tmpfs that dies with the + // process — the strongest of the three temp semantics. + const workdir = await tempDir(homedir()) + const target = `/tmp/dsh-bwrap-e2e-ephemeral-${process.pid}.txt` + tempFiles.push(target) + const sandbox = await provider() + const { result } = runConfined(sandbox, `printf tmp-ok > ${target} && cat ${target}`, { mode: 'workspace-write', workspaceRoot: workdir }) + expect(result.status).toBe(0) + expect(result.stdout).toBe('tmp-ok') + expect(existsSync(target)).toBe(false) + }) +}) diff --git a/packages/sandbox/sandbox-local/tests/landlock.e2e.ts b/packages/sandbox/sandbox-local/tests/landlock.e2e.ts new file mode 100644 index 0000000000..104d3b318a --- /dev/null +++ b/packages/sandbox/sandbox-local/tests/landlock.e2e.ts @@ -0,0 +1,118 @@ +import { spawnSync } from 'node:child_process' +import { existsSync, readFileSync } from 'node:fs' +import { mkdtemp, rm } from 'node:fs/promises' +import { homedir, tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { Context } from 'cordis' +import type { SandboxPolicy } from '@deepseek-ai/dsh-sandbox' +import { launcherPath } from 'node-addon-landlock-run' +import { LocalSandboxProvider } from '@deepseek-ai/dsh-sandbox-local' + +/** + * KEYLESS Landlock integration proof for the BACKEND: the REAL npm-distributed + * `landlock-run` launcher (`node-addon-landlock-run`) confining REAL processes through `confine()` + a direct + * spawn of the returned argv, with the bwrap rung forced off so the ladder + * lands on the launcher. Verifies the WORLD (files exist or don't), not the + * wrapper argv alone; the through-`ctx.bash` consumer proof lives with + * `@deepseek-ai/dsh-bash-sandbox`. + * + * Self-skips when the running kernel does not enforce Landlock (or this + * platform has no launcher package — the probe cannot pass then). The + * binary itself arrives with `pnpm install`, so absence is not a checkout + * state. + * + * Workspaces live under the HOME directory on purpose: `workspace-write` + * grants the host `/tmp` wholesale (the documented Landlock-profile + * difference), so only a workspace OUTSIDE `/tmp` proves the workspace-root + * grant itself. + */ + +const probe = spawnSync(launcherPath(), ['--probe'], { timeout: 5_000, encoding: 'utf8' }) +const landlockUsable = probe.status === 0 +/** The running kernel's enforcement level, from the launcher's probe report — every wrap below must carry exactly this. */ +const enforcement = /partially enforced/.test(probe.stdout ?? '') ? 'partial' : 'full' + +let ctx: Context | undefined +const tempDirs: string[] = [] + +afterEach(async () => { + await ctx?.fiber.dispose() + ctx = undefined + await Promise.all(tempDirs.splice(0).map(dir => rm(dir, { recursive: true, force: true }))) +}) + +async function tempDir(base: string): Promise { + const dir = await mkdtemp(join(base, 'dsh-landlock-e2e-')) + tempDirs.push(dir) + return dir +} + +async function provider(): Promise { + ctx = new Context() + await ctx.plugin(LocalSandboxProvider, {}) + const sandbox = ctx.sandbox as LocalSandboxProvider + sandbox.internals = { probeBwrap: () => false } + return sandbox +} + +/** Confine a shell command under `policy` and run it for real; returns the spawn result and the wrap's enforcement. */ +function runConfined(sandbox: LocalSandboxProvider, command: string, policy: SandboxPolicy) { + const confined = sandbox.confine(['bash', '-c', command], policy) + const result = spawnSync(confined.argv[0] as string, confined.argv.slice(1), { timeout: 30_000, encoding: 'utf8' }) + return { result, enforcement: confined.enforcement } +} + +describe.skipIf(!landlockUsable)('sandbox-local: real Landlock confinement through the bundled launcher', () => { + it('read-only denies a write — the file must NOT exist, the wrap reports the probed enforcement', async () => { + const workdir = await tempDir(tmpdir()) + const sandbox = await provider() + const { result, enforcement: wrapped } = runConfined(sandbox, `echo hi > ${workdir}/denied.txt`, { mode: 'read-only', workspaceRoot: workdir }) + expect(result.status).not.toBe(0) + expect(wrapped).toBe(enforcement) + expect(existsSync(join(workdir, 'denied.txt'))).toBe(false) + }) + + it('read-only keeps the tree readable/executable and /dev/null writable', async () => { + const workdir = await tempDir(tmpdir()) + const sandbox = await provider() + const { result } = runConfined(sandbox, 'ls / > /dev/null && echo dev-ok', { mode: 'read-only', workspaceRoot: workdir }) + expect(result.status).toBe(0) + expect(result.stdout).toBe('dev-ok\n') + }) + + it('read-only denies a write beneath the host /dev (the /dev/shm tmpfs must stay untouched)', async () => { + // The grant is /dev/null the FILE, not /dev the directory: /dev/shm is a + // world-writable host tmpfs, and a write landing there would be exactly + // the persistent host effect read-only promises never happen. + const workdir = await tempDir(tmpdir()) + const sandbox = await provider() + const target = `/dev/shm/dsh-landlock-e2e-${process.pid}` + const { result } = runConfined(sandbox, `echo hi > ${target}`, { mode: 'read-only', workspaceRoot: workdir }) + expect(result.status).not.toBe(0) + expect(existsSync(target)).toBe(false) + }) + + it('workspace-write lands a write inside the workspace root and still denies one beside it', async () => { + const workdir = await tempDir(homedir()) + const outside = await tempDir(homedir()) + const sandbox = await provider() + + const inside = runConfined(sandbox, `printf landlock-ok > ${workdir}/allowed.txt`, { mode: 'workspace-write', workspaceRoot: workdir }) + expect(inside.result.status).toBe(0) + expect(readFileSync(join(workdir, 'allowed.txt'), 'utf8')).toBe('landlock-ok') + + const denied = runConfined(sandbox, `echo hi > ${outside}/denied.txt`, { mode: 'workspace-write', workspaceRoot: workdir }) + expect(denied.result.status).not.toBe(0) + expect(existsSync(join(outside, 'denied.txt'))).toBe(false) + }) + + it('workspace-write grants the host /tmp (the documented Landlock-profile difference)', async () => { + const workdir = await tempDir(homedir()) + const scratch = await tempDir(tmpdir()) + const sandbox = await provider() + const { result } = runConfined(sandbox, `printf tmp-ok > ${scratch}/scratch.txt`, { mode: 'workspace-write', workspaceRoot: workdir }) + expect(result.status).toBe(0) + expect(readFileSync(join(scratch, 'scratch.txt'), 'utf8')).toBe('tmp-ok') + }) +}) diff --git a/packages/sandbox/sandbox-local/tests/local.spec.ts b/packages/sandbox/sandbox-local/tests/local.spec.ts new file mode 100644 index 0000000000..852a50e1e9 --- /dev/null +++ b/packages/sandbox/sandbox-local/tests/local.spec.ts @@ -0,0 +1,371 @@ +/** + * LocalSandboxProvider tests. No real runner is assumed to exist on the test + * host: `runnerCommand` injects deterministic runner argvs, and `internals` + * injects probe verdicts plus fake Landlock launcher / `sandbox-exec` + * scripts, so profile dialects, ladder selection, verdict caching, + * probe-report parsing, per-rung denial signatures, and fail-closed behavior + * are all exercised through the real `confine()` path. + */ + +import { mkdtempSync, realpathSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { describe, expect, it, vi } from 'vitest' +import { Context } from 'cordis' +import { SANDBOX_UNAVAILABLE, SandboxUnavailableError } from '@deepseek-ai/dsh-sandbox' +import type { SandboxPolicy } from '@deepseek-ai/dsh-sandbox' +import { + bwrapProfileArgs, + landlockProfileArgs, + LocalSandboxProvider, + seatbeltProfileArgs, +} from '@deepseek-ai/dsh-sandbox-local' +import type { Config } from '@deepseek-ai/dsh-sandbox-local' + +const RO: SandboxPolicy = { mode: 'read-only', workspaceRoot: '/ws' } +const WW: SandboxPolicy = { mode: 'workspace-write', workspaceRoot: '/ws' } + +async function setup(config: Config = {}, internals: LocalSandboxProvider['internals'] = {}) { + const ctx = new Context() + await ctx.plugin(LocalSandboxProvider, config) + const sandbox = ctx.sandbox as LocalSandboxProvider + sandbox.internals = internals + return { ctx, sandbox } +} + +/** Write an executable fake `landlock-run` that answers `--probe` with `report`. */ +function fakeLauncher(report = 'landlock: fully enforced'): string { + const dir = mkdtempSync(join(tmpdir(), 'dsh-fake-landlock-')) + const launcher = join(dir, 'landlock-run') + writeFileSync(launcher, `#!/bin/sh\nif [ "$1" = "--probe" ]; then echo "${report}"; exit 0; fi\nexit 125\n`, { mode: 0o755 }) + return launcher +} + +/** Write an executable fake `sandbox-exec` that exits `status` for any invocation. */ +function fakeSeatbeltExec(status: number): string { + const dir = mkdtempSync(join(tmpdir(), 'dsh-fake-seatbelt-')) + const exec = join(dir, 'sandbox-exec') + writeFileSync(exec, `#!/bin/sh\nexit ${status}\n`, { mode: 0o755 }) + return exec +} + +/** The seatbelt read-only profile — every seatbelt profile starts with these forms. */ +const SEATBELT_RO_PROFILE = '(version 1) (allow default) (deny file-write*) (allow file-write* (literal "/dev/null"))' + +describe('profile dialects', () => { + it('bwrap read-only: whole tree read-only with fresh /dev and /proc, no writable mounts', () => { + expect(bwrapProfileArgs(RO)).toEqual(['--ro-bind', '/', '/', '--dev', '/dev', '--proc', '/proc', '--die-with-parent']) + }) + + it('bwrap workspace-write: adds an ephemeral /tmp and rebinds the workspace root', () => { + expect(bwrapProfileArgs(WW)).toEqual([ + '--ro-bind', '/', '/', '--dev', '/dev', '--proc', '/proc', '--die-with-parent', + '--tmpfs', '/tmp', '--bind', '/ws', '/ws', + ]) + }) + + it('landlock read-only: readable tree plus a writable /dev/null, nothing else', () => { + // /dev/null specifically, NOT /dev: a whole-/dev grant would let confined + // commands write real host paths beneath it (/dev/shm) under read-only. + expect(landlockProfileArgs(RO)).toEqual(['--ro', '/', '--rw', '/dev/null']) + }) + + it('landlock workspace-write: adds the host /tmp and the workspace root', () => { + expect(landlockProfileArgs(WW)).toEqual(['--ro', '/', '--rw', '/dev/null', '--rw', '/tmp', '--rw', '/ws']) + }) + + it('seatbelt read-only: allow-default with every file write denied except the /dev/null literal', () => { + expect(seatbeltProfileArgs(RO)).toEqual(['-p', SEATBELT_RO_PROFILE]) + }) + + it('seatbelt workspace-write: one more allow for the canonicalized workspace root, /tmp, and the user temp dir', () => { + // `/ws` does not exist, so it is granted as spelled (the canonicalization + // fallback); `/tmp` and `os.tmpdir()` exist everywhere and are granted + // CANONICALIZED — Seatbelt matches resolved paths (`/tmp` IS + // `/private/tmp` on macOS), and both collapse to one grant on hosts + // where they resolve to the same directory. + const roots = [...new Set(['/ws', realpathSync('/tmp'), realpathSync(tmpdir())])] + const allow = `(allow file-write* ${roots.map(root => `(subpath "${root}")`).join(' ')})` + expect(seatbeltProfileArgs(WW)).toEqual(['-p', `${SEATBELT_RO_PROFILE} ${allow}`]) + }) + + it('seatbelt workspace-write dedups a workspace root that already IS the temp dir', () => { + const profile = seatbeltProfileArgs({ mode: 'workspace-write', workspaceRoot: tmpdir() })[1] as string + const grant = `(subpath "${realpathSync(tmpdir())}")` + expect(profile).toContain(grant) + expect(profile.split(grant)).toHaveLength(2) + }) +}) + +describe('runnerCommand config', () => { + it('a non-empty runnerCommand skips the chain: runner argv + bwrap-shaped profile + -- + caller argv, asserted full', async () => { + const probeBwrap = vi.fn(() => false) + const probeLandlock = vi.fn(() => 'unusable' as const) + const probeSeatbelt = vi.fn(() => false) + const { sandbox } = await setup({ + runnerCommand: ['fake-runner', '--flag'], + runnerFailureSignatures: ['fake-runner: profile rejected'], + }, { probeBwrap, probeLandlock, probeSeatbelt }) + const confined = sandbox.confine(['bash', '-c', 'echo hi'], WW) + expect(confined).toEqual({ + argv: ['fake-runner', '--flag', ...bwrapProfileArgs(WW), '--', 'bash', '-c', 'echo hi'], + enforcement: 'full', + // An operator runner's kernel mechanism is unknown: both Linux + // file-denial dialects, never bare EPERM. + denialSignatures: ['read-only file system', 'permission denied'], + // The runner's own dialect is unknown, but the consumer re-joins the + // wrap through an outer `bash -c 'exec …'` — a missing or + // unexecutable runner fails with the OUTER shell's argv0-scoped + // shapes, and those classify as sandbox failures like any rung. + runnerFailureSignatures: [ + 'fake-runner: profile rejected', + 'exec: fake-runner: not found', + 'fake-runner: No such file or directory', + 'fake-runner: Permission denied', + ], + }) + expect(probeBwrap).not.toHaveBeenCalled() + expect(probeLandlock).not.toHaveBeenCalled() + expect(probeSeatbelt).not.toHaveBeenCalled() + }) + + it('an EMPTY runnerCommand means unconfigured: the platform chain still gates the wrap', async () => { + const probeBwrap = vi.fn(() => false) + const { sandbox } = await setup({ runnerCommand: [] }, { platform: 'linux', probeBwrap, probeLandlock: () => 'unusable' }) + expect(() => sandbox.confine(['true'], RO)).toThrow(SandboxUnavailableError) + expect(probeBwrap).toHaveBeenCalledTimes(1) + }) + + it('requires an operator-owned failure dialect for every configured runner', async () => { + await expect(setup({ runnerCommand: ['fake-runner'] })).rejects.toThrow( + 'runnerCommand requires at least one runnerFailureSignatures entry', + ) + }) + + it('rejects runner failure signatures when no custom runner consumes them', async () => { + await expect(setup({ runnerFailureSignatures: ['profile rejected'] })).rejects.toThrow( + 'runnerFailureSignatures requires runnerCommand', + ) + }) + + it('rejects blank configured-runner failure signatures', async () => { + await expect(setup({ runnerCommand: ['fake-runner'], runnerFailureSignatures: [' '] })).rejects.toThrow( + 'runnerFailureSignatures entries must be non-empty', + ) + }) +}) + +describe('the platform chains', () => { + it('linux probes bwrap first: a passing probe wraps with the bwrap dialect at full enforcement', async () => { + const probeBwrap = vi.fn(() => true) + const probeLandlock = vi.fn(() => 'full' as const) + const { sandbox } = await setup({}, { platform: 'linux', probeBwrap, probeLandlock }) + const confined = sandbox.confine(['true'], RO) + expect(confined).toEqual({ + argv: ['bwrap', ...bwrapProfileArgs(RO), '--', 'true'], + enforcement: 'full', + denialSignatures: ['read-only file system'], + runnerFailureSignatures: ['bwrap: '], + }) + expect(probeLandlock).not.toHaveBeenCalled() + }) + + it('linux falls back to the launcher when the bwrap probe fails, speaking the landlock dialect', async () => { + const probeBwrap = vi.fn(() => false) + const probeLandlock = vi.fn(() => 'full' as const) + const launcher = fakeLauncher() + const { sandbox } = await setup({}, { platform: 'linux', probeBwrap, probeLandlock, landlockLauncher: launcher }) + const confined = sandbox.confine(['bash', '-c', 'echo hi'], WW) + expect(confined).toEqual({ + argv: [launcher, ...landlockProfileArgs(WW), '--', 'bash', '-c', 'echo hi'], + enforcement: 'full', + denialSignatures: ['permission denied'], + runnerFailureSignatures: ['landlock-run: '], + }) + expect(probeLandlock).toHaveBeenCalledWith(launcher) + }) + + it('darwin selects its sole candidate WITHOUT probing: nothing to arbitrate', async () => { + // The safety property moves to execution time: an unusable sandbox-exec + // refuses to run the command, and the wrap's runnerFailureSignatures let + // the consumer classify that as a sandbox failure, not a task failure. + const probeSeatbelt = vi.fn(() => true) + const { sandbox } = await setup({}, { platform: 'darwin', probeSeatbelt }) + const confined = sandbox.confine(['bash', '-c', 'echo hi'], RO) + expect(confined).toEqual({ + argv: ['sandbox-exec', ...seatbeltProfileArgs(RO), '--', 'bash', '-c', 'echo hi'], + enforcement: 'full', + denialSignatures: ['operation not permitted'], + runnerFailureSignatures: ['sandbox-exec: '], + }) + expect(probeSeatbelt).not.toHaveBeenCalled() + }) + + it('a platform with no chain fails closed without a single probe: the command never runs', async () => { + const probeBwrap = vi.fn(() => true) + const probeLandlock = vi.fn(() => 'full' as const) + const probeSeatbelt = vi.fn(() => true) + const { sandbox } = await setup({}, { platform: 'freebsd', probeBwrap, probeLandlock, probeSeatbelt }) + expect(() => sandbox.confine(['true'], RO)).toThrow(expect.objectContaining({ name: 'SandboxUnavailableError', code: SANDBOX_UNAVAILABLE })) + expect(probeBwrap).not.toHaveBeenCalled() + expect(probeLandlock).not.toHaveBeenCalled() + expect(probeSeatbelt).not.toHaveBeenCalled() + }) + + it('win32 is a reserved EMPTY chain: fails closed identically until a Windows runner fills it', async () => { + // The slot exists so Windows support is an additive fill-in (chain entry + // + runner union member), never a redesign — and reserving it must not + // weaken the fail-closed end in the meantime. + const { sandbox } = await setup({}, { platform: 'win32' }) + expect(() => sandbox.confine(['true'], RO)).toThrow(expect.objectContaining({ code: SANDBOX_UNAVAILABLE })) + }) + + it('caches the verdict for the provider lifetime: one chain walk across wraps', async () => { + const probeBwrap = vi.fn(() => true) + const { sandbox } = await setup({}, { platform: 'linux', probeBwrap }) + sandbox.confine(['true'], RO) + sandbox.confine(['true'], WW) + expect(probeBwrap).toHaveBeenCalledTimes(1) + }) + + it('the unavailable verdict is cached too, and the error is structured', async () => { + const probeBwrap = vi.fn(() => false) + const probeLandlock = vi.fn(() => 'unusable' as const) + const { sandbox } = await setup({}, { platform: 'linux', probeBwrap, probeLandlock }) + expect(() => sandbox.confine(['true'], RO)).toThrow(expect.objectContaining({ name: 'SandboxUnavailableError', code: SANDBOX_UNAVAILABLE })) + expect(() => sandbox.confine(['true'], RO)).toThrow(SandboxUnavailableError) + expect(probeBwrap).toHaveBeenCalledTimes(1) + expect(probeLandlock).toHaveBeenCalledTimes(1) + }) + + it('a multi-rung chain probes a seatbelt rung like any other (the walk, not the platform table, decides)', async () => { + // The product chains reach seatbelt only as darwin's sole (unprobed) + // candidate; the chain seam exercises the probing path it would take in + // a grown chain, keeping the default seatbelt probe honest. + const exec = fakeSeatbeltExec(0) + const probeBwrap = vi.fn(() => false) + const { sandbox } = await setup({}, { chain: ['bwrap', 'seatbelt'], probeBwrap, seatbeltExec: exec }) + const confined = sandbox.confine(['true'], RO) + expect(confined.argv[0]).toBe(exec) + expect(confined.enforcement).toBe('full') + expect(probeBwrap).toHaveBeenCalledTimes(1) + }) + + it('a rogue chain entry throws via the probe walk\'s exhaustiveness guard (closed union)', async () => { + // Same convention as the wrap switch below: the union is closed, so a + // runner added later fails to compile at the probe switch instead of + // silently selecting without a probe. Only a cast can reach the guard. + const { sandbox } = await setup({}, { chain: ['chroot', 'bwrap'] as unknown as readonly ['bwrap'] }) + expect(() => sandbox.confine(['true'], RO)).toThrow('unreachable variant') + }) + + it('a rogue cached runner tag throws via the exhaustiveness guard (closed union)', async () => { + // The wrap switches on the chain verdict's runner tag and ends with + // assertNever: a rogue tag (only reachable by a cast — the union is + // closed and chainVerdict writes only its own literals) must throw, so a + // runner added later fails to compile at the switch instead of silently + // wrapping with another runner's dialect. + const { sandbox } = await setup() + ;(sandbox as unknown as { selectedRunner: unknown }).selectedRunner = { runner: 'chroot', enforcement: 'full' } + expect(() => sandbox.confine(['true'], RO)).toThrow('unreachable variant') + }) + + it('runs the real default probes on the linux chain when none are injected (usable here or fail closed there)', async () => { + // Pinning the platform (not the probes) makes the REAL defaultProbeBwrap + // spawn run on every host: bwrap answers on a Linux box, ENOENT reads as + // an unusable rung anywhere else — either way the walk is genuine. + const { sandbox } = await setup({}, { platform: 'linux' }) + const verdict = (() => { + try { + sandbox.confine(['true'], RO) + return 'usable' + } catch (error: unknown) { + if (error instanceof SandboxUnavailableError) return 'unavailable' + throw error + } + })() + expect(['usable', 'unavailable']).toContain(verdict) + }) + + it('walks the real platform chain when nothing is injected (usable here or fail closed there)', async () => { + const { sandbox } = await setup({}, {}) + const verdict = (() => { + try { + sandbox.confine(['true'], RO) + return 'usable' + } catch (error: unknown) { + if (error instanceof SandboxUnavailableError) return 'unavailable' + throw error + } + })() + expect(['usable', 'unavailable']).toContain(verdict) + }) +}) + +describe('the default landlock probe (launcher CLI contract)', () => { + it('parses a fully-enforced probe report as full enforcement', async () => { + const { sandbox } = await setup({}, { platform: 'linux', probeBwrap: () => false, landlockLauncher: fakeLauncher() }) + expect(sandbox.confine(['true'], RO).enforcement).toBe('full') + }) + + it('parses a partially-enforced (older-ABI) probe report as partial enforcement', async () => { + const launcher = fakeLauncher('landlock: partially enforced (older ABI)') + const { sandbox } = await setup({}, { platform: 'linux', probeBwrap: () => false, landlockLauncher: launcher }) + expect(sandbox.confine(['true'], RO).enforcement).toBe('partial') + }) + + it('reads a failing launcher as unusable: the chain ends and fails closed', async () => { + const dir = mkdtempSync(join(tmpdir(), 'dsh-fake-landlock-')) + const launcher = join(dir, 'landlock-run') + writeFileSync(launcher, '#!/bin/sh\nexit 125\n', { mode: 0o755 }) + const { sandbox } = await setup({}, { platform: 'linux', probeBwrap: () => false, landlockLauncher: launcher }) + expect(() => sandbox.confine(['true'], RO)).toThrow(expect.objectContaining({ code: SANDBOX_UNAVAILABLE })) + }) +}) + +describe('probeTimeoutMs config', () => { + it('rejects 0 at construction: Node treats a 0 spawnSync timeout as UNBOUNDED, the opposite of the field', async () => { + const ctx = new Context() + await expect(ctx.plugin(LocalSandboxProvider, { probeTimeoutMs: 0 })) + .rejects.toThrow(/probeTimeoutMs must be a positive finite number/) + }) + + it('bounds the default probes: a launcher slower than the configured timeout reads as unusable', async () => { + // The same sleeping launcher passes under the default 5000ms budget and + // fails under a 250ms one — the config demonstrably reaches spawnSync. + const dir = mkdtempSync(join(tmpdir(), 'dsh-slow-landlock-')) + const launcher = join(dir, 'landlock-run') + writeFileSync(launcher, '#!/bin/sh\nsleep 1\necho "landlock: fully enforced"\nexit 0\n', { mode: 0o755 }) + + const patient = await setup({}, { platform: 'linux', probeBwrap: () => false, landlockLauncher: launcher }) + expect(patient.sandbox.confine(['true'], RO).enforcement).toBe('full') + + const impatient = await setup( + { probeTimeoutMs: 250 }, + { platform: 'linux', probeBwrap: () => false, landlockLauncher: launcher }, + ) + expect(() => impatient.sandbox.confine(['true'], RO)).toThrow(expect.objectContaining({ code: SANDBOX_UNAVAILABLE })) + }) +}) + +describe('the default seatbelt probe (sandbox-exec contract)', () => { + // The product chains reach seatbelt only unprobed (darwin's sole + // candidate), so the default probe's contract is pinned through the chain + // seam: a grown chain must probe it like any other rung. + it('selects the rung when the executable applies the read-only profile and exits 0', async () => { + const exec = fakeSeatbeltExec(0) + const { sandbox } = await setup({}, { chain: ['bwrap', 'seatbelt'], probeBwrap: () => false, seatbeltExec: exec }) + const confined = sandbox.confine(['true'], RO) + expect(confined).toEqual({ + argv: [exec, ...seatbeltProfileArgs(RO), '--', 'true'], + enforcement: 'full', + denialSignatures: ['operation not permitted'], + runnerFailureSignatures: ['sandbox-exec: '], + }) + }) + + it('reads a failing executable as unusable: the chain ends and fails closed', async () => { + const { sandbox } = await setup({}, { chain: ['bwrap', 'seatbelt'], probeBwrap: () => false, seatbeltExec: fakeSeatbeltExec(1) }) + expect(() => sandbox.confine(['true'], RO)).toThrow(expect.objectContaining({ code: SANDBOX_UNAVAILABLE })) + }) +}) diff --git a/packages/sandbox/sandbox-local/tests/packed-install.e2e.ts b/packages/sandbox/sandbox-local/tests/packed-install.e2e.ts new file mode 100644 index 0000000000..9d0e8ade4f --- /dev/null +++ b/packages/sandbox/sandbox-local/tests/packed-install.e2e.ts @@ -0,0 +1,173 @@ +import { spawnSync } from 'node:child_process' +import { accessSync, constants, existsSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs' +import { rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { fileURLToPath } from 'node:url' +import { afterAll, beforeAll, describe, expect, it } from 'vitest' + +/** + * KEYLESS publish-path rehearsal for this package's own distribution: the + * provider must work from its PACKED tarball plus its REGISTRY launcher + * dependency, not the git checkout. `pnpm pack` produces the EXACT bytes + * `pnpm publish` would upload; this suite packs the workspace closure + * (`dsh-sandbox-local` + its `@deepseek-ai` peers), installs the tarballs + * into a throwaway consumer OUTSIDE the repo — npm resolving the + * `node-addon-landlock-run` dependency (and its os/cpu-selected platform + * package) from the public registry, the real consumer path — and drives + * the INSTALLED packages under plain `node`: no tsx, no tsconfig paths, no + * workspace resolution, so a `files`-list omission, a broken launcher + * dependency, or a mode-stripped binary fails here instead of at the first + * real install. + * + * World-proofs: the registry-installed launcher carries this host's ELF + * architecture and IS executable (a tarball that loses the mode bit would + * otherwise masquerade as a non-enforcing kernel — the fail-closed branch + * below must never absorb that), and the installed provider confines a real + * process THROUGH it (bwrap forced off) — or fails closed when the running + * kernel does not enforce Landlock, which is itself the installed + * fail-closed contract. Byte provenance of the launcher is the + * `node-addon-landlock-run` repository's own release-pipeline concern. + * + * Self-skips off Linux or when the built `lib/` is absent (run + * `pnpm run build` first — CI's landlock legs do). + */ + +const packageDir = fileURLToPath(new URL('..', import.meta.url)) +const repoRoot = fileURLToPath(new URL('../../../..', import.meta.url)) + +/** The closure the consumer needs: the package and its transitive `@deepseek-ai` peers; the launcher family arrives from the registry. */ +const WORKSPACE_CLOSURE = [ + 'packages/sandbox/sandbox-local', + 'packages/sandbox/sandbox', + 'packages/llm/llm', + 'packages/util/brand', +] + +/** ELF `e_machine` (offset 18, LE) for this host: x86-64 = 62, AArch64 = 183. */ +const E_MACHINE = { x64: 62, arm64: 183 }[process.arch as 'x64' | 'arm64'] + +const packable = process.platform === 'linux' + && E_MACHINE !== undefined + && existsSync(join(packageDir, 'lib', 'index.js')) + +let consumerDir = '' +let workDir = '' +/** The consumer script's JSON verdict (see its source below). */ +let verdict: { + launcher: string + launcherExists: boolean + enforcing: boolean + wrapArgv0?: string + enforcement?: string + exitCode?: number | null + stderrHasDialect?: boolean + confineOutcome?: string +} = { launcher: '', launcherExists: false, enforcing: false } + +describe.skipIf(!packable)('sandbox-local: packed-tarball distribution (publish-path rehearsal)', () => { + beforeAll(async () => { + const packDest = mkdtempSync(join(tmpdir(), 'dsh-pack-')) + consumerDir = mkdtempSync(join(tmpdir(), 'dsh-packed-consumer-')) + workDir = mkdtempSync(join(tmpdir(), 'dsh-packed-work-')) + + // Pack each closure member with the exact bytes publish would upload. + const tarballs: string[] = [] + for (const pkg of WORKSPACE_CLOSURE) { + const pack = spawnSync('pnpm', ['pack', '--pack-destination', packDest], { + cwd: join(repoRoot, pkg), + encoding: 'utf8', + timeout: 120_000, + }) + expect(pack.status, `pnpm pack failed for ${pkg}:\n${pack.stdout}\n${pack.stderr}`).toBe(0) + const lines = pack.stdout.trim().split('\n') + tarballs.push(lines[lines.length - 1] as string) + } + + // A real consumer: plain ESM project, tarballs installed by npm — the + // peer ranges (^0.0.1) resolve to the tarball versions, cordis pins to + // the peer range's rc, and `node-addon-landlock-run` (with its + // os/cpu-selected platform package, an OPTIONAL dependency of the entry + // — so no `--omit=optional` here) comes from the public registry. + writeFileSync(join(consumerDir, 'package.json'), JSON.stringify({ name: 'dsh-packed-consumer', private: true, type: 'module' })) + const install = spawnSync('npm', ['install', '--no-audit', '--no-fund', ...tarballs, 'cordis@4.0.0-rc.6'], { + cwd: consumerDir, + encoding: 'utf8', + timeout: 300_000, + }) + expect(install.status, `npm install failed:\n${install.stdout}\n${install.stderr}`).toBe(0) + + // The consumer script runs under PLAIN node against the installed + // packages and reports a JSON verdict; every assertion happens back in + // the test. bwrap is forced off so the wrap must select the INSTALLED + // launcher; a non-enforcing kernel must surface the fail-closed error. + writeFileSync(join(consumerDir, 'consumer.mjs'), ` + import { spawnSync } from 'node:child_process' + import { existsSync } from 'node:fs' + import { Context } from 'cordis' + import { launcherPath } from 'node-addon-landlock-run' + import { LocalSandboxProvider } from '@deepseek-ai/dsh-sandbox-local' + const ctx = new Context() + await ctx.plugin(LocalSandboxProvider, {}) + const sandbox = ctx.sandbox + sandbox.internals = { probeBwrap: () => false } + const launcher = launcherPath() + const probe = spawnSync(launcher, ['--probe'], { encoding: 'utf8', timeout: 5000 }) + const out = { launcher, launcherExists: existsSync(launcher), enforcing: probe.status === 0 } + const workdir = process.argv[2] + if (out.enforcing) { + const confined = sandbox.confine(['bash', '-c', \`echo hi > \${workdir}/denied.txt\`], { mode: 'read-only', workspaceRoot: workdir }) + out.wrapArgv0 = confined.argv[0] + out.enforcement = confined.enforcement + const run = spawnSync(confined.argv[0], confined.argv.slice(1), { encoding: 'utf8', timeout: 30000 }) + out.exitCode = run.status + out.stderrHasDialect = /permission denied/i.test(run.stderr) + } else { + try { + sandbox.confine(['true'], { mode: 'read-only', workspaceRoot: workdir }) + out.confineOutcome = 'wrapped' + } catch (error) { + out.confineOutcome = error?.code === 'SANDBOX_UNAVAILABLE' ? 'fail-closed' : String(error) + } + } + console.log(JSON.stringify(out)) + `) + const consumer = spawnSync('node', ['consumer.mjs', workDir], { cwd: consumerDir, encoding: 'utf8', timeout: 60_000 }) + expect(consumer.status, `consumer script failed:\n${consumer.stdout}\n${consumer.stderr}`).toBe(0) + verdict = JSON.parse(consumer.stdout.trim().split('\n').pop() as string) as typeof verdict + }, 480_000) + + afterAll(async () => { + await Promise.all([consumerDir, workDir].filter(Boolean).map(dir => rm(dir, { recursive: true, force: true }))) + }) + + it('installs the registry launcher for this host: present, EXECUTABLE, right ELF arch', () => { + const installed = join(consumerDir, 'node_modules', `node-addon-landlock-run-linux-${process.arch}`, 'bin', 'landlock-run') + expect(existsSync(installed), 'platform package missing from the installed tree').toBe(true) + // A tarball or extraction step that strips the mode bit would leave the + // probe failing exactly like a non-enforcing kernel — assert it apart. + expect(() => { accessSync(installed, constants.X_OK) }, 'installed launcher is not executable').not.toThrow() + expect(readFileSync(installed).readUInt16LE(18), 'ELF e_machine').toBe(E_MACHINE) + }) + + it('the installed provider resolves the launcher INSIDE the consumer node_modules platform package', () => { + expect(verdict.launcher) + .toBe(join(consumerDir, 'node_modules', `node-addon-landlock-run-linux-${process.arch}`, 'bin', 'landlock-run')) + }) + + it('confines through the installed launcher (enforcing kernel) or fails closed (non-enforcing) — never unconfined', async () => { + // Fail-closed is only the acceptable outcome when the installed binary + // IS present and executable and the kernel merely does not enforce — + // the first test pins that apart, so nothing hides behind this branch. + expect(verdict.launcherExists, 'installed launcher missing').toBe(true) + if (verdict.enforcing) { + expect(verdict.wrapArgv0).toBe(verdict.launcher) + expect(['full', 'partial']).toContain(verdict.enforcement) + expect(verdict.exitCode).not.toBe(0) + expect(verdict.stderrHasDialect, 'kernel denial text must match the advertised dialect').toBe(true) + expect(existsSync(join(workDir, 'denied.txt'))).toBe(false) + } else { + expect(verdict.confineOutcome).toBe('fail-closed') + } + }) +}) diff --git a/packages/sandbox/sandbox-local/tests/seatbelt.e2e.ts b/packages/sandbox/sandbox-local/tests/seatbelt.e2e.ts new file mode 100644 index 0000000000..7136e4e524 --- /dev/null +++ b/packages/sandbox/sandbox-local/tests/seatbelt.e2e.ts @@ -0,0 +1,120 @@ +import { spawnSync } from 'node:child_process' +import { existsSync, readFileSync } from 'node:fs' +import { mkdtemp, rm } from 'node:fs/promises' +import { homedir, tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { Context } from 'cordis' +import type { SandboxPolicy } from '@deepseek-ai/dsh-sandbox' +import { LocalSandboxProvider, seatbeltProfileArgs } from '@deepseek-ai/dsh-sandbox-local' + +/** + * KEYLESS Seatbelt integration proof for the BACKEND: the REAL macOS + * `sandbox-exec` confining REAL processes through `confine()` + a direct + * spawn of the returned argv, with the Linux rungs forced off so the ladder + * lands on Seatbelt. Verifies the WORLD (files exist or don't) and that the + * kernel's denial text matches the dialect the wrap advertises; the + * through-`ctx.bash` consumer proof lives with `@deepseek-ai/dsh-bash-sandbox`. + * + * Self-skips wherever the functional probe fails — every non-macOS host, or + * a macOS whose `sandbox-exec` refuses the profile. + * + * Workspaces for the workspace-write tests live under the HOME directory on + * purpose: `workspace-write` grants `/tmp` and the per-user temp dir + * wholesale (the documented Seatbelt-profile temp areas), so only a + * workspace OUTSIDE both proves the workspace-root grant itself. + */ + +const probe = spawnSync('sandbox-exec', [...seatbeltProfileArgs({ mode: 'read-only', workspaceRoot: '/' }), '--', 'true'], { timeout: 5_000, stdio: 'ignore' }) +const seatbeltUsable = probe.status === 0 + +let ctx: Context | undefined +const tempDirs: string[] = [] + +afterEach(async () => { + await ctx?.fiber.dispose() + ctx = undefined + await Promise.all(tempDirs.splice(0).map(dir => rm(dir, { recursive: true, force: true }))) +}) + +async function tempDir(base: string): Promise { + const dir = await mkdtemp(join(base, 'dsh-seatbelt-e2e-')) + tempDirs.push(dir) + return dir +} + +async function provider(): Promise { + ctx = new Context() + await ctx.plugin(LocalSandboxProvider, {}) + const sandbox = ctx.sandbox as LocalSandboxProvider + sandbox.internals = { probeBwrap: () => false, probeLandlock: () => 'unusable' } + return sandbox +} + +/** Confine a shell command under `policy` and run it for real; returns the spawn result and the wrap's facts. */ +function runConfined(sandbox: LocalSandboxProvider, command: string, policy: SandboxPolicy) { + const confined = sandbox.confine(['bash', '-c', command], policy) + const result = spawnSync(confined.argv[0] as string, confined.argv.slice(1), { timeout: 30_000, encoding: 'utf8' }) + return { result, confined } +} + +describe.skipIf(!seatbeltUsable)('sandbox-local: real Seatbelt confinement through sandbox-exec', () => { + it('read-only denies a write — the file must NOT exist, and the kernel speaks the advertised dialect', async () => { + const workdir = await tempDir(tmpdir()) + const sandbox = await provider() + const { result, confined } = runConfined(sandbox, `echo hi > ${workdir}/denied.txt`, { mode: 'read-only', workspaceRoot: workdir }) + expect(result.status).not.toBe(0) + expect(confined.enforcement).toBe('full') + // The wrap's denialSignatures must be what the kernel actually prints. + expect(result.stderr.toLowerCase()).toContain('operation not permitted') + expect(existsSync(join(workdir, 'denied.txt'))).toBe(false) + }) + + it('read-only keeps the tree readable/executable and /dev/null writable', async () => { + const workdir = await tempDir(tmpdir()) + const sandbox = await provider() + const { result } = runConfined(sandbox, 'ls / > /dev/null && echo dev-ok', { mode: 'read-only', workspaceRoot: workdir }) + expect(result.status).toBe(0) + expect(result.stdout).toBe('dev-ok\n') + }) + + it('read-only grants no temp area: a write under the user temp dir is denied too', async () => { + // The per-user darwin temp dir is a workspace-write grant, not a + // read-only one — under read-only the only write-shaped path is /dev/null. + const workdir = await tempDir(tmpdir()) + const sandbox = await provider() + const target = join(workdir, 'tmp-denied.txt') + const { result } = runConfined(sandbox, `echo hi > ${target}`, { mode: 'read-only', workspaceRoot: await tempDir(homedir()) }) + expect(result.status).not.toBe(0) + expect(existsSync(target)).toBe(false) + }) + + it('workspace-write lands a write inside the workspace root and still denies one beside it', async () => { + const workdir = await tempDir(homedir()) + const outside = await tempDir(homedir()) + const sandbox = await provider() + + const inside = runConfined(sandbox, `printf seatbelt-ok > ${workdir}/allowed.txt`, { mode: 'workspace-write', workspaceRoot: workdir }) + expect(inside.result.status).toBe(0) + expect(readFileSync(join(workdir, 'allowed.txt'), 'utf8')).toBe('seatbelt-ok') + + const denied = runConfined(sandbox, `echo hi > ${outside}/denied.txt`, { mode: 'workspace-write', workspaceRoot: workdir }) + expect(denied.result.status).not.toBe(0) + expect(existsSync(join(outside, 'denied.txt'))).toBe(false) + }) + + it('workspace-write grants /tmp and the user temp dir (the documented Seatbelt-profile temp areas)', async () => { + const workdir = await tempDir(homedir()) + const hostTmp = await tempDir('/tmp') + const userTmp = await tempDir(tmpdir()) + const sandbox = await provider() + const { result } = runConfined( + sandbox, + `printf tmp-ok > ${hostTmp}/scratch.txt && printf user-tmp-ok > ${userTmp}/scratch.txt`, + { mode: 'workspace-write', workspaceRoot: workdir }, + ) + expect(result.status).toBe(0) + expect(readFileSync(join(hostTmp, 'scratch.txt'), 'utf8')).toBe('tmp-ok') + expect(readFileSync(join(userTmp, 'scratch.txt'), 'utf8')).toBe('user-tmp-ok') + }) +}) diff --git a/packages/sandbox/sandbox-local/tsconfig.json b/packages/sandbox/sandbox-local/tsconfig.json new file mode 100644 index 0000000000..c756b6af69 --- /dev/null +++ b/packages/sandbox/sandbox-local/tsconfig.json @@ -0,0 +1,27 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "lib/types" + }, + "include": [ + "src" + ], + "references": [ + { + "path": "../../../vendor/cosmokit" + }, + { + "path": "../../../vendor/cordis" + }, + { + "path": "../../../vendor/schemastery" + }, + { + "path": "../../llm/llm" + }, + { + "path": "../sandbox" + } + ] +} diff --git a/packages/sandbox/sandbox/README.md b/packages/sandbox/sandbox/README.md new file mode 100644 index 0000000000..ef381625b2 --- /dev/null +++ b/packages/sandbox/sandbox/README.md @@ -0,0 +1,11 @@ +# @deepseek-ai/dsh-sandbox + +Abstract process-sandbox seam. Owns the `ctx.sandbox` service contract ([`SandboxProvider`](src/index.ts)) and the confinement vocabulary the harness shares: `SandboxMode` (`read-only` / `workspace-write` / `danger-full-access`, file effects only), `SandboxEnforcement` (`full` / `partial`, per kernel ABI), `SandboxPolicy` (per-CALL policy — mode + workspace root), and the fail-closed `SANDBOX_UNAVAILABLE` error. Interface package of the [capability-seam split](../../../docs/rfc/implemented/architecture/2026-06-13-capability-seams.md): depends only on cordis (+ the harness error base), never on a backend. + +The contract in one line: `ctx.sandbox.confine(argv, policy)` returns the argv to spawn INSTEAD of your own — wrapped so the process (and everything it spawns) runs confined — plus two facts about the selected backend: the enforcement completeness it achieves and its denial dialect (`denialSignatures`, the stderr substrings its kernel prints on a denied file effect — what stderr-inferring consumers match instead of a cross-backend union); when no backend is usable it throws rather than passing the argv through unconfined. + +Policy rides the call, not the provider: two consumers may confine under different policies at the same instant (bash under `read-only` while a confined child agent keeps its state directory writable), and an approved escalated retry is just a new call with a wider policy. + +**Same-world confinement only.** A backend shares the host's filesystem and kernel (`bwrap`, Landlock, Seatbelt); `workspaceRoot` names a real host path. Containers, microVMs, and remote executors are NOT backends of this seam — they replace whole capability implementations (`ctx.bash`, `ctx.fs`) as environment-coherent groups. The boundary and its rationale: [the sandbox RFC](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md). + +Implementations: [`@deepseek-ai/dsh-sandbox-local`](../sandbox-local/) (Linux: `bwrap`, else the per-platform Landlock launcher; macOS: `sandbox-exec`/Seatbelt). Consumers: [`@deepseek-ai/dsh-bash-sandbox`](../../bash/bash-sandbox/) (wraps `['bash', '-c', command]`). diff --git a/packages/sandbox/sandbox/package.json b/packages/sandbox/sandbox/package.json new file mode 100644 index 0000000000..b37ef714ea --- /dev/null +++ b/packages/sandbox/sandbox/package.json @@ -0,0 +1,32 @@ +{ + "name": "@deepseek-ai/dsh-sandbox", + "description": "Abstract process-sandbox seam (ctx.sandbox) for the DeepSeek Harness: same-world confinement vocabulary and the SandboxProvider contract", + "version": "0.0.1", + "private": true, + "type": "module", + "main": "lib/index.js", + "types": "lib/types/index.d.ts", + "exports": { + ".": { + "types": "./lib/types/index.d.ts", + "default": "./lib/index.js" + }, + "./src/*": "./src/*", + "./package.json": "./package.json" + }, + "files": [ + "lib/index.js", + "lib/types/**/*.d.ts", + "lib/types/**/*.d.ts.map", + "src" + ], + "license": "BSD-3-Clause", + "peerDependencies": { + "@deepseek-ai/dsh-llm": "^0.0.1", + "cordis": "^4.0.0-rc.6" + }, + "devDependencies": { + "@deepseek-ai/dsh-llm": "workspace:^", + "cordis": "^4.0.0-rc.6" + } +} diff --git a/packages/sandbox/sandbox/src/index.ts b/packages/sandbox/sandbox/src/index.ts new file mode 100644 index 0000000000..55b9da4540 --- /dev/null +++ b/packages/sandbox/sandbox/src/index.ts @@ -0,0 +1,199 @@ +/** + * The process-sandbox seam (`ctx.sandbox`): an abstract service defining WHAT + * platform confinement does — wrap a subprocess argv so it executes under a + * file-effect policy — without saying HOW. Implementations subclass + * {@link SandboxProvider} and register as the `sandbox` service; + * `@deepseek-ai/dsh-sandbox-local` (per-platform chains: Linux `bwrap` then the + * npm-distributed `landlock-run` launcher, macOS `sandbox-exec`/Seatbelt) is + * the first. + * Consumers hand over the exact argv they are about to spawn + * (`@deepseek-ai/dsh-bash-sandbox` wraps `['bash', '-c', command]`; a + * subagent backend wraps its child-agent argv) and spawn the returned argv + * instead. + * + * The seam confines SAME-WORLD subprocesses only: a backend shares the + * host's filesystem and kernel, and the policy's `workspaceRoot` names a + * real host path. Containers, microVMs, and remote executors are NOT + * backends of this seam — they are sibling implementations of whole + * capability seams (`ctx.bash`, `ctx.fs`), deployed as environment-coherent + * groups; the boundary is recorded in + * docs/rfc/implemented/feature/2026-07-06-sandbox.md. + * + * @module @deepseek-ai/dsh-sandbox + */ + +import { Context, Service } from 'cordis' +import { HarnessError } from '@deepseek-ai/dsh-llm' + +/** + * File-effect policy a sandbox backend enforces on confined processes. + * + * - `read-only` — the process cannot write the filesystem anywhere; a + * write-shaped `/dev/null` sink stays available so `>/dev/null` redirects + * keep working (HOW is the backend's choice: bwrap mounts a fresh `/dev`, + * the Landlock launcher and Seatbelt grant the single `/dev/null` node). + * - `workspace-write` — writes are allowed only under the policy's + * workspace root and `/tmp`; everything else stays read-only. Which `/tmp` + * is backend-specific — an ephemeral mount under bwrap, the HOST `/tmp` + * under the Landlock launcher, the host `/private/tmp` plus the per-user + * darwin temp dir under Seatbelt: the seam promises the write boundary, + * not the mount's nature. + * - `danger-full-access` — no confinement; a consumer configured with it + * spawns its argv unwrapped and never calls the provider. + * + * The mode governs FILE effects only: network and process visibility are not + * restricted (a backend that cannot honestly enforce them must not pretend + * to). How completely the file effects themselves are enforced is likewise a + * reported fact, not an assumption — see {@link SandboxEnforcement}. + */ +export type SandboxMode = 'read-only' | 'workspace-write' | 'danger-full-access' + +/** A confining (non-`danger-full-access`) mode — the modes a {@link SandboxPolicy} can carry. */ +export type ConfinedSandboxMode = Exclude + +/** + * How completely the selected backend enforces a confined mode's file + * effects. + * + * - `full` — every file effect the mode promises to block is governed: the + * `bwrap` mount profile, a Landlock kernel enforcing the launcher's whole + * ruleset, or an operator-configured runner (configuring one asserts full + * enforcement along with existence). + * - `partial` — the backend is active but the kernel governs only the subset + * of accesses its ABI knows (an older Landlock ABI: path-based truncate is + * ungoverned before ABI v3), so a file effect the mode promises to block + * may still land. A caller that needs the mode's promise to be absolute + * must treat `partial` as outside that promise. + */ +export type SandboxEnforcement = 'full' | 'partial' + +/** + * What one confined execution is allowed to touch — carried PER CALL, not + * fixed on the provider: two consumers may confine under different policies + * at the same instant (bash under `read-only` while a confined child agent + * needs its state directory writable), and an approved escalated retry is a + * new call with a wider policy. Defaulting/resolution is the consumer's + * explicit step (its config owns the fallback chain); the provider treats + * the policy as fully specified. + */ +export interface SandboxPolicy { + /** The file-effect mode this execution runs under. */ + mode: ConfinedSandboxMode + /** Absolute root directory `workspace-write` may write under. */ + workspaceRoot: string +} + +/** + * A {@link SandboxProvider.confine} result: the argv to spawn in place of + * the caller's own, plus the enforcement completeness the selected backend + * achieves for it. + */ +export interface ConfinedArgv { + /** The wrapped argv (runner, profile, separator, then the caller's argv). */ + argv: string[] + /** How completely the selected backend enforces the policy's file effects. */ + enforcement: SandboxEnforcement + /** + * The selected backend's denial DIALECT: the case-insensitive stderr + * substrings a file effect denied by THIS backend produces (EROFS text + * under bwrap's read-only binds, EACCES under Landlock, EPERM under + * Seatbelt). A consumer that infers denials from a failed run's stderr + * matches against exactly these rather than a cross-backend union — the + * union claims denials a given backend never produces. + */ + denialSignatures: readonly string[] + /** + * How the RUNNER ITSELF failing identifies itself: case-insensitive stderr + * substrings produced when the sandbox binary is missing, refuses its + * profile, or fails closed before exec'ing the command (`bwrap: `, + * `landlock-run: `, `sandbox-exec: ` — each covers both the runner's own + * error prefix and the shell's runner-not-found message). ORTHOGONAL to + * {@link denialSignatures}: a denial is the confined COMMAND being blocked + * (the sandbox working as designed); a runner failure means the command + * NEVER RAN and must surface as a sandbox failure, not a task failure — + * consumers check these signatures FIRST (a runner's own error text may + * contain denial words, e.g. an unopenable grant root reporting + * `Permission denied`). + */ + runnerFailureSignatures: readonly string[] +} + +/** + * Error `code` carried by the infrastructure error a provider throws when a + * confined policy is requested but no backend is available or usable on this + * host: confinement FAILS CLOSED (refuses to run) rather than silently + * executing unconfined. Thrown as a `HarnessError`, it reaches the model + * through the structured `{ name, code }` error channel on `tool/result`, so + * callers can distinguish "the sandbox is missing" from a failing command. + */ +export const SANDBOX_UNAVAILABLE = 'SANDBOX_UNAVAILABLE' + +/** + * Thrown by {@link SandboxProvider.confine} when a confined policy is + * requested but no backend is usable on this host: confinement fails closed. + * Carries the {@link SANDBOX_UNAVAILABLE} code through the structured + * `{ name, code }` error channel. + */ +export class SandboxUnavailableError extends HarnessError { + constructor(mode: ConfinedSandboxMode, detail?: string) { + super( + `sandbox mode "${mode}" is requested but no sandbox backend is usable on this host; ` + + 'refusing to run the command unconfined. Install bubblewrap or run a Landlock-enforcing ' + + 'kernel (Linux), ensure sandbox-exec is usable (macOS) — Windows has no confinement ' + + 'backend yet — or switch the consumer to danger-full-access.' + + (detail === undefined ? '' : ` Runner failure: ${detail}`), + SANDBOX_UNAVAILABLE, + ) + this.name = 'SandboxUnavailableError' + } +} + +declare module 'cordis' { + interface Context { + sandbox: SandboxProvider + } +} + +/** + * Abstract process-sandbox service. Subclass, implement {@link confine}, and + * load the subclass as a plugin — it registers as `ctx.sandbox` (one + * implementation per context; loading a second throws, cordis' standard + * duplicate-service behavior). + * + * Semantics every implementation must honor: + * - {@link confine} either returns an argv whose runner ENFORCES the policy + * or fails closed — at `confine` time with {@link SandboxUnavailableError} + * (no backend for this host), or at EXECUTION time by the runner itself + * refusing to run the command (exiting without exec'ing it, identified by + * {@link ConfinedArgv.runnerFailureSignatures}). A silent unconfined + * passthrough is never a legal outcome on either path. + * - Probing exists to ARBITRATE between multiple candidate backends and may + * be skipped when a platform has exactly one: the sole candidate is + * selected directly and the runner's exec-time fail-closed refusal carries + * the safety property. When probing does run, it is functional (actually + * enforcing a profile, not a version check), at most once per provider + * lifetime; `confine` itself spawns nothing beyond that one-time probing. + * - The returned {@link ConfinedArgv.enforcement} states the backend's + * actual completeness for THIS host; `partial` is reported, never silently + * upgraded to `full`. + */ +export abstract class SandboxProvider extends Service { + constructor(ctx: Context) { + super(ctx, 'sandbox') + } + + /** + * Wrap `argv` so it executes confined under `policy` on this host; the + * caller spawns the returned argv in place of its own. + * @param argv - the exact argv the caller is about to spawn (program plus + * arguments), NOT a shell string — a shell-shaped consumer passes + * `['bash', '-c', command]`. + * @param policy - the file-effect policy this execution runs under, + * carried per call (see {@link SandboxPolicy}). + * @returns the argv to spawn instead, plus the enforcement completeness + * the selected backend achieves for it. + */ + abstract confine(argv: readonly string[], policy: SandboxPolicy): ConfinedArgv +} + +export default SandboxProvider diff --git a/packages/sandbox/sandbox/tests/vocabulary.spec.ts b/packages/sandbox/sandbox/tests/vocabulary.spec.ts new file mode 100644 index 0000000000..caba42b2a7 --- /dev/null +++ b/packages/sandbox/sandbox/tests/vocabulary.spec.ts @@ -0,0 +1,35 @@ +/** + * Vocabulary-contract tests for the sandbox seam: the fail-closed error's + * structured identity is what tool results and consumers key on, so its + * shape is pinned here, next to the vocabulary that owns it. Provider + * behavior is each implementation's suite (`dsh-sandbox-local`); consumer + * behavior is each consumer's (`dsh-bash-sandbox`). + */ + +import { describe, expect, it } from 'vitest' +import { SANDBOX_UNAVAILABLE, SandboxUnavailableError } from '@deepseek-ai/dsh-sandbox' + +describe('SandboxUnavailableError', () => { + it('carries the structured { name, code } identity consumers key on', () => { + const error = new SandboxUnavailableError('read-only') + expect(error.name).toBe('SandboxUnavailableError') + expect(error.code).toBe(SANDBOX_UNAVAILABLE) + expect(error).toBeInstanceOf(Error) + }) + + it('names the refused mode and the operator escape hatches in its message', () => { + const error = new SandboxUnavailableError('workspace-write') + expect(error.message).toContain('"workspace-write"') + expect(error.message).toContain('danger-full-access') + expect(error.message).not.toContain('Runner failure') + }) + + it('carries the runner detail when the failure is discovered at execution time', () => { + // The late twin of the confine-time throw: an unprobed sole candidate + // that fails closed at exec surfaces the SAME error, with the runner's + // own first stderr line as the cause. + const error = new SandboxUnavailableError('read-only', 'landlock-run: landlock is not enforced by this kernel') + expect(error.code).toBe(SANDBOX_UNAVAILABLE) + expect(error.message).toContain('Runner failure: landlock-run: landlock is not enforced by this kernel') + }) +}) diff --git a/packages/sandbox/sandbox/tsconfig.json b/packages/sandbox/sandbox/tsconfig.json new file mode 100644 index 0000000000..9f687793d7 --- /dev/null +++ b/packages/sandbox/sandbox/tsconfig.json @@ -0,0 +1,21 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "lib/types" + }, + "include": [ + "src" + ], + "references": [ + { + "path": "../../../vendor/cosmokit" + }, + { + "path": "../../../vendor/cordis" + }, + { + "path": "../../llm/llm" + } + ] +} diff --git a/packages/session-persistence/session-persistence-jsonl/README.md b/packages/session-persistence/session-persistence-jsonl/README.md index 9a76381614..990ad2fcc4 100644 --- a/packages/session-persistence/session-persistence-jsonl/README.md +++ b/packages/session-persistence/session-persistence-jsonl/README.md @@ -29,4 +29,4 @@ The JSONL durable session-persistence backend — a concrete `SessionPersistence ## Write path -The plugin generalizes the example `session-jsonl.ts`: it subscribes to `session/created` (capture the header; persist a fork's seed once), `session/event` (snapshot each event when buffering — the live `session.events` object is mutable), and `session/flush`/dispose (drain the write-behind buffer through `append`). A per-session write cursor means a resumed session never re-appends already-stored events. Existing live sessions are seeded on plugin apply (HMR does not replay `session/created`). All backend operations for one session are serialized, and disposal awaits quiescence (every init + final drain) before returning, so no write lands after teardown. +The plugin generalizes the example `session-jsonl.ts`: it subscribes to `session/created` (capture the header; persist a fork's seed once), `session/event` (copy each already-frozen event into the persistence-owned write-behind buffer), and `session/flush`/dispose (drain that buffer through `append`). A per-session write cursor means a resumed session never re-appends already-stored events. Existing live sessions are seeded on plugin apply (HMR does not replay `session/created`). All backend operations for one session are serialized, and disposal awaits quiescence (every init + final drain) before returning, so no write lands after teardown. diff --git a/packages/session-persistence/session-persistence-jsonl/tests/jsonl.spec.ts b/packages/session-persistence/session-persistence-jsonl/tests/jsonl.spec.ts index 6eed63a2f4..757ba03aac 100644 --- a/packages/session-persistence/session-persistence-jsonl/tests/jsonl.spec.ts +++ b/packages/session-persistence/session-persistence-jsonl/tests/jsonl.spec.ts @@ -4,7 +4,7 @@ import { appendFile, mkdtemp, mkdir, rm, readFile, writeFile, readdir, stat } fr import { tmpdir } from 'node:os' import { join } from 'node:path' import SessionStore, { SessionId } from '@deepseek-ai/dsh-session' -import type { Session, SessionEvent } from '@deepseek-ai/dsh-session' +import type { Session, SessionEvent, SessionHeader } from '@deepseek-ai/dsh-session' import SessionPersistenceJsonl from '@deepseek-ai/dsh-session-persistence-jsonl' import { encodeSegment, logPath, scanLog, sessionDir } from '../src/format.ts' import { runPersistenceContract, meta, oneTurnLog, appendLog } from '../../session-persistence/tests/contract.ts' @@ -13,6 +13,13 @@ import { runCoordinatorContract, type CoordinatorFixture } from '../../session-p let root: string const dirs: string[] = [] +type MutableSessionHeader = { -readonly [K in keyof SessionHeader]: SessionHeader[K] } + +/** Test-only mutable view used to verify that backends detach returned/caller metadata. */ +function mutableHeader(header: SessionHeader): MutableSessionHeader { + return header +} + async function freshRoot(): Promise { const dir = await mkdtemp(join(tmpdir(), 'dsh-jsonl-')) dirs.push(dir) @@ -255,7 +262,7 @@ describe('SessionPersistenceJsonl: durability and crash semantics', () => { const loaded = await ctx.sessionPersistence.load(m.id) // A consumer mutates the returned meta's cwd. The backend's stored pathing // metadata must be unaffected, so a later append still finds the right log. - loaded.meta.cwd = '/evil' + mutableHeader(loaded.meta).cwd = '/evil' await ctx.sessionPersistence.append(m.id, [ { type: 'turn/start', seq: 6, time: 9, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } }, { type: 'turn/end', seq: 7, time: 10, data: { turn: 2, reason: { kind: 'completed' } } }, @@ -421,7 +428,7 @@ describe('SessionPersistenceJsonl: edge cases', () => { const m = meta('create-snap', '/orig') const p = ctx.sessionPersistence.create(m) // Mutate the caller's meta object immediately after calling create. - m.cwd = '/mutated' + mutableHeader(m).cwd = '/mutated' await p await ctx.sessionPersistence.append(SessionId('create-snap'), oneTurnLog()) // The log materialized under the ORIGINAL cwd, not the mutated one. diff --git a/packages/session-persistence/session-persistence-sqlite/README.md b/packages/session-persistence/session-persistence-sqlite/README.md index c14d3a70ea..082b60af88 100644 --- a/packages/session-persistence/session-persistence-sqlite/README.md +++ b/packages/session-persistence/session-persistence-sqlite/README.md @@ -27,4 +27,4 @@ interface Config { ## Write path -Like the JSONL backend, the plugin also installs the `session/event` → buffer → `session/flush` drain: it snapshots each event when buffered (the live `session.events` object is mutable), persists a fork's seed once on `session/created`, keeps a per-session write cursor so a resumed session never re-appends stored events, and seeds existing live sessions on apply (HMR does not replay `session/created`). Dispose awaits every in-flight init + final drain and then closes the database, so no write lands after teardown. +Like the JSONL backend, the plugin also installs the `session/event` → buffer → `session/flush` drain: it copies each already-frozen event into a persistence-owned buffer, persists a fork's seed once on `session/created`, keeps a per-session write cursor so a resumed session never re-appends stored events, and seeds existing live sessions on apply (HMR does not replay `session/created`). Dispose awaits every in-flight init + final drain and then closes the database, so no write lands after teardown. diff --git a/packages/session-persistence/session-persistence/README.md b/packages/session-persistence/session-persistence/README.md index 8bd3fed568..bf7da03757 100644 --- a/packages/session-persistence/session-persistence/README.md +++ b/packages/session-persistence/session-persistence/README.md @@ -17,7 +17,7 @@ The persisted unit IS the existing `SessionEvent` (event-sourced model — the l - **Append-only; a crashed turn is closed, not truncated.** Committed events (at or below a flushed `turn/end`) are never rewritten. A crash can leave an unclosed final turn whose events are real and possibly large; `load` preserves them and durably appends synthetic closers (an error `tool/result` per unanswered `tool-call`, then `step/end?`+`turn/end {interrupted}`) to balance the log and keep the rehydrated history a valid provider transcript. Only a never-fully-written torn tail fragment is discarded. - **Contiguous seq.** `load` rejects a `seq` gap/parse error in the MIDDLE of the log; `append`'s first `seq` must equal the stored next-seq. -- **JSON-serializable data.** `append` rejects non-serializable `event.data`; backends snapshot each event when buffering (the live `session.events` object is mutable). +- **JSON-serializable data.** `append` materializes each direct/replay batch through the shared one-pass lossless-JSON boundary. Live `Session` events are already deep-frozen, but the write coordinator still copies each event into a persistence-owned buffer. - **Durability.** `append` returns only once the batch is durable. ## The write coordinator diff --git a/packages/session-persistence/session-persistence/src/coordinator.ts b/packages/session-persistence/session-persistence/src/coordinator.ts index 0180999842..b1fc118a21 100644 --- a/packages/session-persistence/session-persistence/src/coordinator.ts +++ b/packages/session-persistence/session-persistence/src/coordinator.ts @@ -25,9 +25,9 @@ */ import { Context } from 'cordis' -import { interruptedTurnClosers, SESSION_FORMAT_VERSION } from '@deepseek-ai/dsh-session' +import { interruptedTurnClosers, SESSION_FORMAT_VERSION, snapshotJsonValue } from '@deepseek-ai/dsh-session' import type { Session, SessionEvent, SessionId, SessionHeader } from '@deepseek-ai/dsh-session' -import { assertSerializable, seedCoversPrefix } from './index.ts' +import { seedCoversPrefix } from './index.ts' /** * A stored session's durable prefix as read back from a backend: its @@ -186,14 +186,18 @@ export class PersistenceCoordinator { /** * Register a new session's metadata (lazy: no physical write until the first * {@link append}). Rejects if the id is already tracked or already persisted. - * @param meta - the immutable header (id, version, cwd, lineage) to record; snapshotted at call time. + * @param meta - the header (id, version, cwd, lineage) to record; materialized + * as a detached lossless-JSON snapshot at call time. */ create(meta: SessionHeader): Promise { // Snapshot the metadata at call time: the op runs later (behind the // per-session chain) and the snapshot is stored as the lazy state, so keeping // the caller's object by reference would let a later mutation of `id`/`cwd` // register under one key but materialize under a different path/header. - const snapshot: SessionHeader = { ...meta } + const snapshot = snapshotJsonValue(meta) + if (snapshot === undefined) { + return Promise.reject(new TypeError('session metadata must be losslessly JSON-serializable')) + } return this.serialize(snapshot.id, () => this.createCore(snapshot)) } @@ -212,23 +216,25 @@ export class PersistenceCoordinator { this.states.set(meta.id, { meta, cursor: 0, materialized: false }) } - // `async` so the synchronous validate/clone below reject (not throw) per the - // Promise contract — callers use `await expect(...).rejects`. + // `async` so synchronous materialization failures below reject (not throw) per + // the Promise contract — callers use `await expect(...).rejects`. /** * Durably persist a batch of events. Honors the append-only and contiguous-seq * contracts; rejects non-JSON-serializable `event.data`. * @param id - the session the batch belongs to. - * @param events - the contiguous batch to persist, in seq order; deep-cloned at call time. + * @param events - the contiguous batch to persist, in seq order; materialized + * as a detached lossless-JSON snapshot at call time. */ async append(id: SessionId, events: readonly SessionEvent[]): Promise { - // Validate serializability BEFORE cloning so a bad event surfaces the typed - // error rather than an opaque DataCloneError from structuredClone. - assertSerializable(events) - // Deep-snapshot the batch HERE, before the op waits behind the per-session - // chain: a caller that mutates a live array (e.g. session.events) — or an - // event inside it — before the op runs would otherwise have those changes - // persisted. The clone is taken synchronously (at call time). - const batch = events.map(e => structuredClone(e)) + // Validate and deep-snapshot the complete batch HERE, in one traversal, + // before the op waits behind the per-session chain. A check followed by + // structuredClone would reread accessors and could sanitize an exotic value + // into an apparently valid record; the single-pass materializer makes the + // checked value exactly the value persisted. + const batch = snapshotJsonValue(events) + if (batch === undefined) { + throw new TypeError('session event batch is not losslessly JSON-serializable because it contains non-JSON-serializable data') + } return this.serialize(id, () => this.appendCore(id, batch)) } @@ -338,9 +344,10 @@ export class PersistenceCoordinator { // promise so flush/dispose can await it (onCreated is async). ctx.on('session/created', (session) => { void this.initFor(session) }) - // Snapshot + buffer every event (the live object is mutable; clone so a later - // in-place mutation cannot rewrite a buffered event). Serializability is - // guaranteed at the source (Session.append), so structuredClone is safe. + // Session emits an owned frozen event. Keep a persistence-owned copy anyway + // so the write-behind queue owns exactly the record it will flush rather than + // retaining a product-layer record by identity. Serializability is guaranteed + // at the source, so structuredClone is safe. ctx.on('session/event', (session, event) => { let buffer = this.buffers.get(session) if (!buffer) this.buffers.set(session, buffer = []) @@ -391,8 +398,8 @@ export class PersistenceCoordinator { const existing = this.inits.get(session) if (existing) return existing // Snapshot the seed SYNCHRONOUSLY — initFor runs inside the `session/created` - // emit, before any later `append` adds non-seed events. A clone freezes it - // against later mutation of the live event objects. + // emit, before any later append invalidates the public array snapshot. Events + // are already frozen; cloning gives persistence independent ownership. const seed = session.events.map(e => structuredClone(e)) const p = this.onCreated(session, seed) // Attach a no-op rejection handler so a failing init does not surface as an diff --git a/packages/session-persistence/session-persistence/src/index.ts b/packages/session-persistence/session-persistence/src/index.ts index 1588ed2526..b03691d17b 100644 --- a/packages/session-persistence/session-persistence/src/index.ts +++ b/packages/session-persistence/session-persistence/src/index.ts @@ -22,7 +22,7 @@ */ import { Context, Service } from 'cordis' -import { isJsonValue } from '@deepseek-ai/dsh-session' +import { snapshotJsonValue } from '@deepseek-ai/dsh-session' import type { SessionEvent, SessionId, SessionHeader } from '@deepseek-ai/dsh-session' // Re-export the metadata vocabulary so consumers import it from the seam. @@ -58,16 +58,16 @@ export function seedCoversPrefix(seed: readonly SessionEvent[], prefix: readonly } /** - * Reject non-JSON-serializable event data before a backend serializes a batch. - * Live session appends already enforce this; persistence append paths also - * accept replay/fork batches that may bypass a live session instance. - * @param events - the batch to validate; throws naming the offending event's type and seq. + * Reject a batch that is not wholly losslessly JSON-serializable. Live session + * appends already enforce this; persistence append paths also accept replay or + * direct batches that may bypass a live session instance. Validation uses the + * same one-pass materializer as the coordinator, so getters are read once. + * @param events - the complete event batch to validate. */ export function assertSerializable(events: readonly SessionEvent[]): void { - for (const event of events) { - if (!isJsonValue(event.data)) { - throw new Error(`event "${event.type}" carries non-JSON-serializable data (seq ${event.seq})`) - } + const snapshot = snapshotJsonValue(events) + if (snapshot === undefined) { + throw new Error('session event batch is not losslessly JSON-serializable because it contains non-JSON-serializable data') } } @@ -90,11 +90,11 @@ export function assertSerializable(events: readonly SessionEvent[]): void { * {@link load} rejects a parse error or a `seq` gap in the COMMITTED region * (unloadable); {@link append}'s first event `seq` MUST equal the backend's * stored next-seq (after `load` has balanced any interrupted turn). - * - **JSON-serializable data.** `SessionEventMap` is merge-extensible and - * `event.data` is typed only as `SessionEventMap[K]`, so {@link append} - * REJECTS non-JSON-serializable data with an error naming the offending - * event type. A backend snapshots (serializes/clones) each event when it - * buffers, since `session.events` hands out the live mutable object. + * - **JSON-serializable events.** `SessionEventMap` is merge-extensible, so + * {@link append} materializes each complete batch through the shared + * lossless-JSON boundary before buffering it. The public `session.events` + * view is immutable, but persistence still snapshots direct/replay callers at + * this independent trust boundary. * - **Durability.** {@link append} returns only once the batch is durable * (the file backend fsyncs; a DB commits). {@link create} MAY defer the * physical write until the first {@link append} (lazy materialization). diff --git a/packages/session-persistence/session-persistence/tests/contract.ts b/packages/session-persistence/session-persistence/tests/contract.ts index 9f2facb827..789aa72c91 100644 --- a/packages/session-persistence/session-persistence/tests/contract.ts +++ b/packages/session-persistence/session-persistence/tests/contract.ts @@ -245,7 +245,7 @@ export function runPersistenceContract(name: string, make: () => Promise Promise< } }) - it('snapshot-on-buffer: mutating an event after session/event does not corrupt the persisted copy', async () => { + it('source-frozen events cannot be mutated after buffering and persist unchanged', async () => { const fix = await makeFixture() const { ctx, fiber } = await freshCtx(fix) try { const session = ctx.sessions.create(SessionId('mutate'), { meta: { cwd: WORK } }) const ev = session.append('user/message', { content: [{ type: 'text', text: 'original' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) - // Mutate the live event object AFTER it was buffered by session/event. - ;(ev.data as { content: { type: 'text'; text: string }[] }).content[0]!.text = 'HACKED' + expect(() => { + ;(ev.data as { content: { type: 'text'; text: string }[] }).content[0]!.text = 'HACKED' + }).toThrow(TypeError) session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) session.append('turn/end', { turn: 1, reason: { kind: 'completed' } }) await ctx.parallel('session/flush', session) diff --git a/packages/session-persistence/session-persistence/tests/persistence.spec.ts b/packages/session-persistence/session-persistence/tests/persistence.spec.ts index 4e4cf67822..9c766d4b66 100644 --- a/packages/session-persistence/session-persistence/tests/persistence.spec.ts +++ b/packages/session-persistence/session-persistence/tests/persistence.spec.ts @@ -158,6 +158,17 @@ describe('SessionPersistence service registration', () => { expect(loaded.events).toHaveLength(6) await fiber.dispose() }) + + it('rejects non-JSON session metadata before registering lazy state', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + const fiber = await ctx.plugin(MemoryPersistence) + const invalid = { ...meta('invalid-meta'), createdAt: 1n as unknown as number } + + await expect(ctx.sessionPersistence.create(invalid)) + .rejects.toThrow('session metadata must be losslessly JSON-serializable') + await fiber.dispose() + }) }) describe('shared persistence helpers', () => { @@ -187,10 +198,10 @@ describe('shared persistence helpers', () => { expect(() => { assertSerializable(oneTurnLog()) }).not.toThrow() }) - it('rejects non-JSON-serializable event data with type and seq context', () => { + it('rejects a batch containing non-JSON-serializable event data', () => { const bad = [ { type: 'user/message', seq: 0, time: 1, data: { content: 1n } }, ] as unknown as SessionEvent[] - expect(() => { assertSerializable(bad) }).toThrow(/"user\/message".*seq 0/) + expect(() => { assertSerializable(bad) }).toThrow(/batch is not losslessly JSON-serializable/) }) }) diff --git a/packages/skill/README.md b/packages/skill/README.md new file mode 100644 index 0000000000..447b82fc47 --- /dev/null +++ b/packages/skill/README.md @@ -0,0 +1,11 @@ +# skill/ - skill capability family + +The canonical three-package capability seam for reusable agent instructions: a provider registry, a local implementation, and the model-facing catalog/loader consumer. All are **product** packages. + +| Package | Role | ctx key | +|---|---|---| +| `skill/` | Provider registry, precedence resolution, stable catalog snapshots, and full-definition lookup | `ctx.skills` | +| `skill-local/` | Project/custom/user filesystem provider | (registers on `ctx.skills`) | +| `tool-skill/` | Session-prefix catalog and model-facing `skill` loader | (registers on `ctx.tools`) | + +The interface lives at `skill/skill/`. Providers register synchronously and perform asynchronous discovery through `ctx.skills`; `tool-skill` consumes only that interface, so an embedded or remote provider can replace or complement `skill-local` without changing the model-facing contract. `agent-core` loads this family by default, but it remains a capability outside the core control spine, parallel to [`bash/`](../bash/README.md), [`fs/`](../fs/README.md), [`web/`](../web/README.md), and [`subagent/`](../subagent/README.md). diff --git a/packages/skill/skill-local/README.md b/packages/skill/skill-local/README.md new file mode 100644 index 0000000000..c885416ff5 --- /dev/null +++ b/packages/skill/skill-local/README.md @@ -0,0 +1,37 @@ +# @deepseek-ai/dsh-skill-local + +Local filesystem provider for the `ctx.skills` registry. + +This package implements one skill source. It scans local project, custom, and user skill roots, parses `SKILL.md` or flat Markdown skill files, and registers the provider on `ctx.skills`. The registry remains in `@deepseek-ai/dsh-skill`; the session-prefix catalog and model-facing loader tool remain in `@deepseek-ai/dsh-tool-skill`. + +## Plugin + +Requires `ctx.skills` (`inject: ['skills']`). + +### Config + +| Field | Default | Meaning | +|---|---|---| +| `dshHome` | `$DSH_HOME` or `~/.dsh` | DeepSeek Harness config root; scans `skills` under this directory. | +| `agentsHome` | `$DSH_AGENTS_HOME` or `~/.agents` | Shared agent config root scanned for compatible skills. | +| `customSkillDirs` | `[]` | Additional local skill roots scanned after project roots and before user roots. | + +## Discovery + +Default roots are resolved in this provider's rank order: + +| Rank | Source | Path | +|---|---|---| +| 100 | `project-dsh` | `/.dsh/skills` | +| 200 | `project-agents` | `/.agents/skills` | +| 300 | `custom` | `Config.customSkillDirs` | +| 400 | `user-dsh` | `/skills` | +| 500 | `user-agents` | `/skills` | + +The project root is the nearest ancestor containing `.git`; without one, the current cwd is used. The user DSH root skips its `.system` child so system-owned directories are not accidentally treated as normal user skills. DeepSeek Harness no longer ships built-in system skills from this provider; additional built-ins can be supplied later by another provider. + +When `ctx.fs` is available, discovery lists roots through `ctx.fs.listDir`, reads skill files through `ctx.fs.readText`, and probes `.git` through the filesystem service. Full skill loads forward the lookup abort signal to filesystem metadata and content reads. Without a filesystem service, the provider falls back to abortable Node filesystem I/O so minimal local contexts can still load skills. Missing, unreadable, or malformed skill files warn and skip instead of failing the whole request. + +## Skill Format + +Skills can be single-level directory bundles (`/SKILL.md`) or flat Markdown files (`.md`). Nested `**/SKILL.md` discovery is intentionally not part of v1. Frontmatter is parsed as YAML with the `yaml` package; it requires `name` and `description`, while `whenToUse`, `disableModelInvocation`, and `metadata` are optional. Names must be kebab-case. diff --git a/packages/skill/skill-local/package.json b/packages/skill/skill-local/package.json new file mode 100644 index 0000000000..dcacc5960a --- /dev/null +++ b/packages/skill/skill-local/package.json @@ -0,0 +1,38 @@ +{ + "name": "@deepseek-ai/dsh-skill-local", + "description": "Local filesystem skill provider for the DeepSeek Harness", + "version": "0.0.1", + "private": true, + "type": "module", + "main": "lib/index.js", + "types": "lib/types/index.d.ts", + "exports": { + ".": { + "types": "./lib/types/index.d.ts", + "default": "./lib/index.js" + }, + "./src/*": "./src/*", + "./package.json": "./package.json" + }, + "files": [ + "lib/index.js", + "lib/types/**/*.d.ts", + "lib/types/**/*.d.ts.map", + "src" + ], + "license": "BSD-3-Clause", + "peerDependencies": { + "@deepseek-ai/dsh-fs": "^0.0.1", + "@deepseek-ai/dsh-skill": "^0.0.1", + "cordis": "^4.0.0-rc.6" + }, + "dependencies": { + "schemastery": "^3.18.0", + "yaml": "^2.4.2" + }, + "devDependencies": { + "@deepseek-ai/dsh-fs": "workspace:^", + "@deepseek-ai/dsh-skill": "workspace:^", + "cordis": "^4.0.0-rc.6" + } +} diff --git a/packages/skill/skill-local/src/index.ts b/packages/skill/skill-local/src/index.ts new file mode 100644 index 0000000000..19a15f1de8 --- /dev/null +++ b/packages/skill/skill-local/src/index.ts @@ -0,0 +1,424 @@ +/** + * Local filesystem skill provider. + * + * This package is one implementation of the `ctx.skills` provider registry. It + * discovers directory-bundle and flat Markdown skills from project, custom, and + * user roots, parses YAML frontmatter, and loads bodies through `ctx.fs` when a + * filesystem service is present. + * + * @module @deepseek-ai/dsh-skill-local + */ + +import { access, readdir, readFile, stat } from 'node:fs/promises' +import { dirname, join, resolve } from 'node:path' +import { homedir } from 'node:os' +import type { Context } from 'cordis' +import z from 'schemastery' +import type Schema from 'schemastery' +import { parse as parseYaml } from 'yaml' +import type { FileSystem, FsDirEntry, FsTarget } from '@deepseek-ai/dsh-fs' +import { + isSkillName, + type SkillCandidate, + type SkillDefinition, + type SkillLookupOptions, + type SkillProvider, + type SkillSource, +} from '@deepseek-ai/dsh-skill' + +const PROJECT_DSH_RANK = 100 +const PROJECT_AGENTS_RANK = 200 +const CUSTOM_RANK = 300 +const USER_DSH_RANK = 400 +const USER_AGENTS_RANK = 500 + +export const name = 'skill-local' +export const inject = ['skills'] + +/** Local filesystem skill provider configuration. */ +export interface Config { + /** DeepSeek Harness config root. Defaults to `$DSH_HOME` or `~/.dsh`. */ + dshHome?: string + /** Shared agent config root. Defaults to `$DSH_AGENTS_HOME` or `~/.agents`. */ + agentsHome?: string + /** Additional skill roots scanned after project roots and before user roots. */ + customSkillDirs?: string[] +} + +export const Config: Schema = z.object({ + dshHome: z.string(), + agentsHome: z.string(), + customSkillDirs: z.array(z.string()).default([]), +}) + +interface SkillRoot { + path: string + source: SkillSource + rank: number + skipSystem?: boolean +} + +interface SkillRootEntry { + name: string + type: 'directory' | 'file' | 'other' + path: string +} + +interface ParsedSkill { + name: string + description: string + whenToUse?: string + disableModelInvocation?: boolean + metadata?: Record + content: string +} + +interface LocalLocator { + path: string + directory: string +} + +/** Register the local filesystem skill provider on `ctx.skills`. */ +export function apply(ctx: Context, config: Config = {}): void { + const provider = new LocalSkillProvider(ctx, config) + ctx.skills.registerProvider(provider) +} + +/** Provider that maps local project/user skill roots into `ctx.skills`. */ +export class LocalSkillProvider implements SkillProvider { + readonly name = 'local' + private readonly dshHome: string + private readonly agentsHome: string + private readonly customSkillDirs: string[] + + constructor(private readonly ctx: Context, config: Config = {}) { + this.dshHome = resolve(config.dshHome ?? process.env.DSH_HOME ?? join(homedir(), '.dsh')) + this.agentsHome = resolve(config.agentsHome ?? process.env.DSH_AGENTS_HOME ?? join(homedir(), '.agents')) + this.customSkillDirs = (config.customSkillDirs ?? []).map(root => resolve(root)) + } + + /** + * Discover local skill summaries for a cwd-sensitive workspace. + * @param options - lookup options; `cwd` selects the project roots to scan. + * @returns local provider candidates with stable root ranks. + */ + async list(options: SkillLookupOptions): Promise { + const roots = await this.roots(options.cwd) + const candidates: SkillCandidate[] = [] + for (const root of roots) { + for (const skill of await discoverRoot(root, this.ctx)) { + candidates.push(skill) + } + } + return candidates + } + + /** + * Load a complete local skill body from the candidate's file locator. + * @param candidate - the winning candidate returned by this provider. + * @param options - lookup options whose signal cancels filesystem reads. + * @returns the full local skill, or `undefined` if the file disappeared. + */ + async get(candidate: SkillCandidate, options: SkillLookupOptions): Promise { + const locator = candidate.locator as LocalLocator + const parsed = await parseSkillFile(locator.path, this.ctx, options.signal) + if (parsed === undefined) return undefined + return { + name: parsed.name, + description: parsed.description, + ...parsed.whenToUse !== undefined ? { whenToUse: parsed.whenToUse } : {}, + ...parsed.disableModelInvocation !== undefined ? { disableModelInvocation: parsed.disableModelInvocation } : {}, + source: candidate.source, + provider: this.name, + resourceBase: { kind: 'directory', path: locator.directory }, + path: locator.path, + ...parsed.metadata !== undefined ? { metadata: parsed.metadata } : {}, + content: parsed.content, + } + } + + private async roots(cwd: string | undefined): Promise { + const roots: SkillRoot[] = [] + if (cwd !== undefined) { + const projectRoot = await findProjectRoot(resolve(cwd), optionalFileSystem(this.ctx)) + roots.push( + { path: join(projectRoot, '.dsh/skills'), source: 'project-dsh', rank: PROJECT_DSH_RANK }, + { path: join(projectRoot, '.agents/skills'), source: 'project-agents', rank: PROJECT_AGENTS_RANK }, + ) + } + roots.push( + ...this.customSkillDirs.map(path => ({ path, source: 'custom' as const, rank: CUSTOM_RANK })), + { path: join(this.dshHome, 'skills'), source: 'user-dsh', rank: USER_DSH_RANK, skipSystem: true }, + { path: join(this.agentsHome, 'skills'), source: 'user-agents', rank: USER_AGENTS_RANK }, + ) + return roots + } +} + +async function discoverRoot(root: SkillRoot, ctx: Context): Promise { + const skills: SkillCandidate[] = [] + const entries = await listSkillRootEntries(root, ctx) + for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) { + if (root.skipSystem && entry.name === '.system') continue + const locator = entry.type === 'directory' + ? { path: join(entry.path, 'SKILL.md'), directory: entry.path } + : entry.type === 'file' && entry.name.endsWith('.md') + ? { path: entry.path, directory: root.path } + : undefined + if (locator === undefined) continue + const parsed = await parseSkillFile(locator.path, ctx) + if (parsed === undefined) continue + skills.push({ + name: parsed.name, + description: parsed.description, + ...parsed.whenToUse !== undefined ? { whenToUse: parsed.whenToUse } : {}, + ...parsed.disableModelInvocation !== undefined ? { disableModelInvocation: parsed.disableModelInvocation } : {}, + provider: 'local', + source: root.source, + rank: root.rank, + locator, + resourceBase: { kind: 'directory', path: locator.directory }, + path: locator.path, + ...parsed.metadata !== undefined ? { metadata: parsed.metadata } : {}, + }) + } + return skills +} + +async function listSkillRootEntries(root: SkillRoot, ctx: Context): Promise { + const fs = optionalFileSystem(ctx) + if (fs !== undefined) return await listSkillRootEntriesFromFileSystem(root, fs) + return await listSkillRootEntriesFromNode(root, ctx) +} + +async function listSkillRootEntriesFromFileSystem(root: SkillRoot, fs: FileSystem): Promise { + // Skill roots are optional; an absent or unlistable root contributes no skills. + const entries = await fsListDir(fs, root.path).catch(() => undefined) + return entries === undefined ? [] : entries.map(entryFromFs) +} + +async function fsListDir(fs: FileSystem, path: string): Promise { + const target = await fs.resolve(path) + return await fs.listDir(target) +} + +function entryFromFs(entry: FsDirEntry): SkillRootEntry { + return { name: entry.name, type: entry.type, path: entry.target.displayPath } +} + +async function listSkillRootEntriesFromNode(root: SkillRoot, ctx: Context): Promise { + let entries + try { + entries = await readdir(root.path, { withFileTypes: true, encoding: 'utf8' }) + } catch { + // Missing or unreadable local skill roots are expected in most deployments. + return [] + } + + const result: SkillRootEntry[] = [] + for (const entry of entries) { + const path = join(root.path, entry.name) + const type = await nodeEntryKind(path, entry, ctx) + result.push({ name: entry.name, type: type ?? 'other', path }) + } + return result +} + +async function parseSkillFile(path: string, ctx: Context, signal?: AbortSignal): Promise { + const raw = await readSkillText(ctx, path, signal) + signal?.throwIfAborted() + if (raw === undefined) { + return undefined + } + let parsed + try { + parsed = parseFrontmatter(raw) + } catch (error) { + ctx.logger.warn(`skill file ${path} ignored: invalid YAML frontmatter: ${errorMessage(error)}`) + return undefined + } + if (!parsed) { + ctx.logger.warn(`skill file ${path} ignored: missing YAML frontmatter`) + return undefined + } + const name = stringField(parsed.data, 'name') + const description = stringField(parsed.data, 'description') + if (name === undefined || description === undefined) { + ctx.logger.warn(`skill file ${path} ignored: frontmatter requires name and description`) + return undefined + } + if (!isSkillName(name)) { + ctx.logger.warn(`skill file ${path} ignored: invalid skill name "${name}"`) + return undefined + } + return { + name, + description, + ...optionalString(parsed.data, 'whenToUse'), + ...optionalBoolean(parsed.data, 'disableModelInvocation'), + ...optionalMetadata(parsed.data), + content: parsed.body.trim(), + } +} + +function optionalFileSystem(ctx: Context): FileSystem | undefined { + return ctx.get('fs') +} + +async function readSkillText(ctx: Context, path: string, signal?: AbortSignal): Promise { + signal?.throwIfAborted() + const fs = optionalFileSystem(ctx) + if (fs !== undefined) { + return await readSkillTextFromFileSystem(ctx, fs, path, signal) + } + try { + return await readFile(path, { encoding: 'utf8', signal }) + } catch { + signal?.throwIfAborted() + return undefined + } +} + +async function readSkillTextFromFileSystem(ctx: Context, fs: FileSystem, path: string, signal?: AbortSignal): Promise { + // A missing or temporarily inaccessible skill file is not fatal to discovery. + signal?.throwIfAborted() + const target = await fs.resolve(path).catch(() => undefined) + signal?.throwIfAborted() + if (target === undefined) return undefined + let info + try { + info = await fs.stat(target, signal) + } catch (error) { + signal?.throwIfAborted() + ctx.logger.warn(`skill file ${path} ignored: failed to stat through filesystem service: ${errorMessage(error)}`) + return undefined + } + if (info === undefined || info.type !== 'file') return undefined + try { + return await fs.readText(target, signal) + } catch (error) { + signal?.throwIfAborted() + ctx.logger.warn(`skill file ${path} ignored: ${fsReadErrorMessage(target, error)}`) + return undefined + } +} + +function fsReadErrorMessage(target: FsTarget, error: unknown): string { + return `failed to read text file at ${target.displayPath}: ${errorMessage(error)}` +} + +async function nodeEntryKind(fullPath: string, entry: { isDirectory(): boolean; isFile(): boolean; isSymbolicLink(): boolean }, ctx: Context): Promise<'directory' | 'file' | undefined> { + if (entry.isDirectory()) return 'directory' + if (entry.isFile()) return 'file' + /* v8 ignore next -- Non-file directory entries such as FIFOs are platform-specific and intentionally skipped. */ + if (!entry.isSymbolicLink()) return undefined + try { + const info = await stat(fullPath) + if (info.isDirectory()) return 'directory' + if (info.isFile()) return 'file' + return undefined + } catch (error) { + ctx.logger.warn(`skill entry ${fullPath} ignored: failed to follow symbolic link: ${errorMessage(error)}`) + return undefined + } +} + +function parseFrontmatter(raw: string): { data: Record; body: string } | undefined { + const firstLineEnd = raw.indexOf('\n') + if (firstLineEnd < 0) return undefined + const firstLine = raw.slice(0, firstLineEnd).replace(/\r$/, '') + if (firstLine !== '---') return undefined + const start = firstLineEnd + 1 + const closing = findClosingFrontmatter(raw, start) + if (closing === undefined) return undefined + const yaml = raw.slice(start, closing.start) + const parsed = parseYaml(yaml) as unknown + if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) return undefined + return { data: parsed as Record, body: raw.slice(closing.bodyStart) } +} + +function findClosingFrontmatter(raw: string, start: number): { start: number; bodyStart: number } | undefined { + let lineStart = start + while (lineStart <= raw.length) { + const nextNewline = raw.indexOf('\n', lineStart) + const lineEnd = nextNewline < 0 ? raw.length : nextNewline + const line = raw.slice(lineStart, lineEnd).replace(/\r$/, '') + if (line === '---') { + return { start: lineStart, bodyStart: nextNewline < 0 ? raw.length : nextNewline + 1 } + } + if (nextNewline < 0) return undefined + lineStart = nextNewline + 1 + } +} + +async function findProjectRoot(cwd: string, fs: FileSystem | undefined): Promise { + let current = cwd + while (true) { + if (await pathExists(join(current, '.git'), fs)) { + return current + } + const parent = dirname(current) + if (parent === current) return cwd + current = parent + } +} + +async function pathExists(path: string, fs: FileSystem | undefined): Promise { + if (fs !== undefined) { + return await pathExistsInFileSystem(path, fs) + } + return await pathExistsInNode(path) +} + +async function pathExistsInFileSystem(path: string, fs: FileSystem): Promise { + let target + try { + target = await fs.resolve(path) + } catch { + // A backend may reject or hide this candidate; continue walking upward. + return false + } + try { + return await fs.stat(target) !== undefined + } catch { + // Transient stat failures make only this git-root candidate unusable. + return false + } +} + +async function pathExistsInNode(path: string): Promise { + try { + await access(path) + return true + } catch { + // Missing host paths are expected while walking toward the filesystem root. + return false + } +} + +function stringField(data: Record, key: string): string | undefined { + const value = data[key] + return typeof value === 'string' && value.length > 0 ? value : undefined +} + +function optionalString(data: Record, key: string): { [K in typeof key]?: string } { + const value = data[key] + return typeof value === 'string' && value.length > 0 ? { [key]: value } : {} +} + +function optionalBoolean(data: Record, key: string): { [K in typeof key]?: boolean } { + const value = data[key] + return typeof value === 'boolean' ? { [key]: value } : {} +} + +function optionalMetadata(data: Record): { metadata?: Record } { + const value = data.metadata + if (typeof value === 'object' && value !== null && !Array.isArray(value)) { + return { metadata: value as Record } + } + return {} +} + +function errorMessage(error: unknown): string { + return String(error) +} diff --git a/packages/skill/skill-local/tests/skill-local.spec.ts b/packages/skill/skill-local/tests/skill-local.spec.ts new file mode 100644 index 0000000000..94a48ce53f --- /dev/null +++ b/packages/skill/skill-local/tests/skill-local.spec.ts @@ -0,0 +1,393 @@ +import { describe, expect, it } from 'vitest' +import { mkdir, readdir, readFile, stat, symlink, writeFile } from 'node:fs/promises' +import { dirname, join } from 'node:path' +import { tmpdir } from 'node:os' +import { Context } from 'cordis' +import SkillService from '@deepseek-ai/dsh-skill' +import { FileSystem, FsVersion, type FsDirEntry, type FsEditOutcome, type FsEditRequest, type FsInfo, type FsTarget, type FsWriteOutcome } from '@deepseek-ai/dsh-fs' +import * as SkillLocal from '../src/index.ts' + +async function tempDir(name: string): Promise { + return await import('node:fs/promises').then(fs => fs.mkdtemp(join(tmpdir(), `dsh-${name}-`))) +} + +async function writeSkill(root: string, name: string, description: string, body = 'Use the skill.'): Promise { + const dir = join(root, name) + await mkdir(dir, { recursive: true }) + await writeFile(join(dir, 'SKILL.md'), `---\nname: ${name}\ndescription: ${description}\n---\n\n${body}\n`) +} + +async function writeFlatSkill(root: string, name: string, description: string, body = 'Flat body.'): Promise { + await mkdir(root, { recursive: true }) + await writeFile(join(root, `${name}.md`), `---\nname: ${name}\ndescription: ${description}\n---\n\n${body}\n`) +} + +class TestFileSystem extends FileSystem { + listDirCalls = 0 + failResolvePaths = new Set() + failStatPaths = new Set() + statOverrides = new Map() + statSignals: Array = [] + readTextSignals: Array = [] + readTextOverride?: (target: FsTarget, signal?: AbortSignal) => Promise + + override async resolve(path: string): Promise { + if (this.failResolvePaths.has(path)) throw new Error('resolve failed') + return { targetKey: path as never, displayPath: path } + } + + override async stat(target: FsTarget, signal?: AbortSignal): Promise { + this.statSignals.push(signal) + if (this.failStatPaths.has(target.displayPath)) throw new Error('stat failed') + if (this.statOverrides.has(target.displayPath)) return this.statOverrides.get(target.displayPath) + try { + const fs = await import('node:fs/promises') + const info = await fs.stat(target.displayPath) + return { + version: FsVersion(String(info.mtimeMs)), + type: info.isFile() ? 'file' : info.isDirectory() ? 'directory' : 'other', + size: info.size, + } + } catch { + return undefined + } + } + + override async readText(target: FsTarget, signal?: AbortSignal): Promise { + this.readTextSignals.push(signal) + if (this.readTextOverride !== undefined) return await this.readTextOverride(target, signal) + const text = await readFile(target.displayPath, 'utf8') + if (text.includes('\uFFFD')) throw new Error('not text') + return text + } + + override async streamText(_target: FsTarget): Promise> { + throw new Error('not needed in skill tests') + } + + override async listDir(target: FsTarget): Promise { + this.listDirCalls += 1 + const entries = await readdir(target.displayPath, { withFileTypes: true, encoding: 'utf8' }) + const result: FsDirEntry[] = [] + for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name))) { + const childPath = join(target.displayPath, entry.name) + let type: FsInfo['type'] = 'other' + let size: number | undefined + try { + const info = await stat(childPath) + type = info.isFile() ? 'file' : info.isDirectory() ? 'directory' : 'other' + size = info.isFile() ? info.size : undefined + } catch { + type = 'other' + } + result.push({ + name: entry.name, + type, + target: { targetKey: childPath as never, displayPath: childPath }, + version: FsVersion('test'), + ...(size !== undefined ? { size } : {}), + }) + } + return result + } + + override async writeText(target: FsTarget, content: string): Promise { + await mkdir(dirname(target.displayPath), { recursive: true }) + await writeFile(target.displayPath, content) + return { operation: 'create', version: FsVersion('test'), before: null, after: content } + } + + override async editText(_target: FsTarget, _request: FsEditRequest): Promise { + throw new Error('not needed in skill tests') + } +} + +async function setupLocal(home: string, config: Partial = {}): Promise { + const ctx = new Context() + await ctx.plugin(SkillService) + await ctx.plugin(SkillLocal, { + dshHome: join(home, '.dsh'), + agentsHome: join(home, '.agents'), + ...config, + }) + return ctx +} + +describe('dsh-skill-local plugin exports', () => { + it('declares stable plugin metadata', () => { + expect(SkillLocal.name).toBe('skill-local') + expect(SkillLocal.inject).toEqual(['skills']) + }) +}) + +describe('LocalSkillProvider', () => { + it('discovers project, custom, user, and agents skill roots in priority order', async () => { + const home = await tempDir('skill-home') + const project = await tempDir('skill-project') + const custom = await tempDir('skill-custom') + await mkdir(join(project, '.git'), { recursive: true }) + + await writeSkill(join(home, '.agents/skills'), 'same', 'user agents skill') + await writeSkill(join(home, '.dsh/skills'), 'same', 'user dsh skill') + await writeSkill(custom, 'same', 'custom skill') + await writeSkill(join(project, '.agents/skills'), 'same', 'project agents skill') + await writeSkill(join(project, '.dsh/skills'), 'same', 'project dsh skill') + await writeSkill(custom, 'custom-only', 'custom only') + await writeSkill(join(home, '.dsh/skills/.system'), 'hidden-system', 'hidden system') + + const ctx = await setupLocal(home, { customSkillDirs: [custom] }) + + const skills = await ctx.skills.list({ cwd: join(project, 'src') }) + expect(skills.map(skill => [skill.name, skill.description])).toEqual([ + ['custom-only', 'custom only'], + ['same', 'project dsh skill'], + ]) + expect(skills.find(skill => skill.name === 'same')?.source).toBe('project-dsh') + expect(skills.find(skill => skill.name === 'hidden-system')).toBeUndefined() + + const noGit = await tempDir('skill-no-git') + await writeSkill(join(noGit, '.dsh/skills'), 'fallback-root', 'Fallback root') + expect((await ctx.skills.list({ cwd: noGit })).map(skill => skill.name)).toContain('fallback-root') + }) + + it('lets project skills override runtime while runtime overrides custom and user skills', async () => { + const home = await tempDir('skill-runtime-priority') + const project = await tempDir('skill-runtime-project') + const custom = await tempDir('skill-runtime-custom') + await mkdir(join(project, '.git'), { recursive: true }) + + await writeSkill(join(project, '.dsh/skills'), 'project-name', 'Project wins') + await writeSkill(custom, 'runtime-name', 'Custom loses') + await writeSkill(join(home, '.dsh/skills'), 'runtime-name', 'User loses') + + const ctx = await setupLocal(home, { customSkillDirs: [custom] }) + ctx.skills.register({ + name: 'project-name', + description: 'Runtime loses to project', + content: 'Runtime body.', + source: 'runtime', + }) + ctx.skills.register({ + name: 'runtime-name', + description: 'Runtime wins', + content: 'Runtime body.', + source: 'runtime', + }) + + expect((await ctx.skills.get('project-name', { cwd: project }))?.description).toBe('Project wins') + expect((await ctx.skills.get('runtime-name', { cwd: project }))?.description).toBe('Runtime wins') + }) + + it('parses flat skills and filters invalid or model-disabled skills from listing', async () => { + const home = await tempDir('skill-flat') + const root = join(home, '.dsh/skills') + await writeFlatSkill(root, 'flat-skill', 'flat description', 'Flat instructions.') + await writeFile(join(root, 'rich-skill.md'), [ + '---', + 'name: rich-skill', + 'description: rich description', + 'whenToUse: For richer local parsing', + 'disableModelInvocation: false', + 'metadata:', + ' owner: tests', + '---', + '', + 'Rich body.', + ].join('\n')) + await writeFile(join(root, 'bad.md'), '---\nname: Bad_Name\ndescription: bad\n---\n\nbad') + await writeFile(join(root, 'missing-description.md'), '---\nname: missing-description\n---\n\nbad') + await writeFile(join(root, 'no-frontmatter.md'), 'No frontmatter.') + await writeFile(join(root, 'plain-markdown.md'), '# Notes\nNot a skill.') + await writeFile(join(root, 'open-frontmatter.md'), '---\nname: open-frontmatter') + await writeFile(join(root, 'non-object.md'), '---\n[]\n---\n\nbad') + await writeFile(join(root, 'no-trailing-body.md'), '---\nname: no-trailing-body\ndescription: No trailing body\n---') + await writeFile(join(root, 'notes.txt'), 'ignored') + await mkdir(join(root, 'not-a-skill'), { recursive: true }) + await writeSkill(root, 'hidden-skill', 'hidden description', 'Hidden.') + await writeFile(join(root, 'hidden-skill/SKILL.md'), '---\nname: hidden-skill\ndescription: hidden description\ndisableModelInvocation: true\n---\n\nHidden.\n') + + const ctx = await setupLocal(home) + const listedBeforeDelete = await ctx.skills.list() + const flatSummary = listedBeforeDelete.find(skill => skill.name === 'flat-skill') + if (flatSummary === undefined) throw new Error('expected flat-skill') + await writeFile(join(root, 'flat-skill.md'), '') + + expect(listedBeforeDelete.map(skill => skill.name)).toEqual(['flat-skill', 'no-trailing-body', 'rich-skill']) + expect(await ctx.skills.get('flat-skill')).toBeUndefined() + expect((await ctx.skills.get('hidden-skill'))?.content).toContain('Hidden.') + expect(await ctx.skills.get('rich-skill')).toMatchObject({ + whenToUse: 'For richer local parsing', + disableModelInvocation: false, + metadata: { owner: 'tests' }, + }) + expect(await ctx.skills.get('Bad_Name')).toBeUndefined() + }) + + it('supports CRLF frontmatter and ignores delimiter-looking text inside YAML values', async () => { + const home = await tempDir('skill-frontmatter-crlf') + const root = join(home, '.dsh/skills') + await mkdir(root, { recursive: true }) + await writeFile(join(root, 'crlf-skill.md'), [ + '---', + 'name: crlf-skill', + 'description: CRLF skill', + 'metadata:', + ' marker: "----"', + '---', + '', + 'CRLF body.', + ].join('\r\n')) + await writeFile(join(root, 'block-skill.md'), [ + '---', + 'name: block-skill', + 'description: |', + ' Includes a ---- marker that is not a delimiter.', + '---', + '', + 'Block body.', + ].join('\n')) + + const ctx = await setupLocal(home) + + expect((await ctx.skills.get('crlf-skill'))?.content).toBe('CRLF body.') + expect((await ctx.skills.get('crlf-skill'))?.metadata).toEqual({ marker: '----' }) + expect((await ctx.skills.get('block-skill'))?.description).toBe('Includes a ---- marker that is not a delimiter.\n') + expect((await ctx.skills.get('block-skill'))?.content).toBe('Block body.') + }) + + it('skips invalid YAML skill files without hiding valid siblings', async () => { + const home = await tempDir('skill-invalid-yaml') + const root = join(home, '.dsh/skills') + await writeSkill(root, 'good-skill', 'Good skill') + await writeFile(join(root, 'bad-yaml.md'), '---\nname: bad-yaml\ndescription: [unclosed\n---\n\nBad body.\n') + + const ctx = await setupLocal(home) + + expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['good-skill']) + }) + + it('discovers symlinked skill directories and flat files', async () => { + const home = await tempDir('skill-symlink-home') + const external = await tempDir('skill-symlink-external') + await writeSkill(external, 'linked-dir', 'Linked directory') + await writeFlatSkill(external, 'linked-flat', 'Linked flat') + await mkdir(join(home, '.dsh/skills'), { recursive: true }) + await symlink(join(external, 'linked-dir'), join(home, '.dsh/skills/linked-dir')) + await symlink(join(external, 'linked-flat.md'), join(home, '.dsh/skills/linked-flat.md')) + await symlink(join(external, 'missing'), join(home, '.dsh/skills/broken-link')) + await symlink('/dev/null', join(home, '.dsh/skills/device-link')) + + const ctx = await setupLocal(home) + + expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['linked-dir', 'linked-flat']) + }) + + it('uses the filesystem service for discovery, reads, and project-root lookup', async () => { + const home = await tempDir('skill-read-fs') + const project = await tempDir('skill-project-root-backend') + const nestedCwd = join(project, 'packages/app') + const root = join(home, '.dsh/skills') + await mkdir(nestedCwd, { recursive: true }) + await writeFlatSkill(root, 'text-skill', 'Text skill', 'Text body.') + await writeFlatSkill(root, 'resolve-fail', 'Resolve fail', 'Resolve body.') + await writeFlatSkill(root, 'stat-fail', 'Stat fail', 'Stat body.') + await mkdir(join(root, 'empty-dir'), { recursive: true }) + await mkdir(join(root, 'directory-skill/SKILL.md'), { recursive: true }) + await writeFile(join(root, 'binary-skill.md'), Buffer.concat([ + Buffer.from('---\nname: binary-skill\ndescription: Binary skill\n---\n\n'), + Buffer.from([0xff]), + Buffer.from('\n'), + ])) + await writeSkill(join(project, '.agents/skills'), 'backend-root', 'Backend root skill') + + const ctx = new Context() + await ctx.plugin(TestFileSystem) + const fs = ctx.fs as TestFileSystem + fs.failResolvePaths.add(join(root, 'resolve-fail.md')) + fs.failStatPaths.add(join(root, 'stat-fail.md')) + fs.failResolvePaths.add(join(nestedCwd, '.git')) + fs.failStatPaths.add(join(project, 'packages/.git')) + fs.statOverrides.set(join(project, '.git'), { + version: FsVersion('virtual-git'), + type: 'directory', + size: 0, + }) + await ctx.plugin(SkillService) + await ctx.plugin(SkillLocal, { dshHome: join(home, '.dsh'), agentsHome: join(home, '.agents') }) + + expect((await ctx.skills.list({ cwd: nestedCwd })).map(skill => [skill.name, skill.source])).toEqual([ + ['backend-root', 'project-agents'], + ['text-skill', 'user-dsh'], + ]) + expect(fs.listDirCalls).toBeGreaterThan(0) + expect(await ctx.skills.get('binary-skill')).toBeUndefined() + }) + + it('forwards cancellation to filesystem reads while loading a skill', async () => { + const home = await tempDir('skill-read-abort') + await writeSkill(join(home, '.dsh/skills'), 'abortable-skill', 'Abortable skill') + + const ctx = new Context() + await ctx.plugin(TestFileSystem) + const fs = ctx.fs as TestFileSystem + await ctx.plugin(SkillService) + await ctx.plugin(SkillLocal, { dshHome: join(home, '.dsh'), agentsHome: join(home, '.agents') }) + expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['abortable-skill']) + + fs.statSignals = [] + fs.readTextSignals = [] + const started = Promise.withResolvers() + fs.readTextOverride = async (_target, signal) => { + if (signal === undefined) throw new Error('expected the skill lookup signal') + started.resolve(undefined) + return await new Promise((_resolve, reject) => { + signal.addEventListener('abort', () => { + const abortReason = signal.reason as unknown + reject(abortReason instanceof Error ? abortReason : new Error(String(abortReason))) + }, { once: true }) + }) + } + const controller = new AbortController() + const reason = new Error('turn cancelled') + const loading = ctx.skills.get('abortable-skill', { signal: controller.signal }) + await started.promise + controller.abort(reason) + + await expect(loading).rejects.toBe(reason) + expect(fs.statSignals).toEqual([controller.signal]) + expect(fs.readTextSignals).toEqual([controller.signal]) + }) + + it('uses default home root resolution without exposing builtin skills', async () => { + const previousDshHome = process.env.DSH_HOME + const previousAgentsHome = process.env.DSH_AGENTS_HOME + const envHome = await tempDir('skill-env-home') + try { + process.env.DSH_HOME = join(envHome, '.dsh') + process.env.DSH_AGENTS_HOME = join(envHome, '.agents') + await writeSkill(join(envHome, '.dsh/skills'), 'env-skill', 'Env skill') + const ctx = new Context() + await ctx.plugin(SkillService) + await ctx.plugin(SkillLocal) + expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['env-skill']) + + process.env.DSH_HOME = join(envHome, 'empty-dsh') + process.env.DSH_AGENTS_HOME = join(envHome, 'empty-agents') + const empty = new Context() + await empty.plugin(SkillService) + SkillLocal.apply(empty, {}) + expect(await empty.skills.list()).toEqual([]) + } finally { + if (previousDshHome === undefined) { + delete process.env.DSH_HOME + } else { + process.env.DSH_HOME = previousDshHome + } + if (previousAgentsHome === undefined) { + delete process.env.DSH_AGENTS_HOME + } else { + process.env.DSH_AGENTS_HOME = previousAgentsHome + } + } + }) +}) diff --git a/packages/skill/skill-local/tsconfig.json b/packages/skill/skill-local/tsconfig.json new file mode 100644 index 0000000000..018f0a4a50 --- /dev/null +++ b/packages/skill/skill-local/tsconfig.json @@ -0,0 +1,15 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "lib/types" + }, + "include": ["src"], + "references": [ + { "path": "../../../vendor/cosmokit" }, + { "path": "../../../vendor/cordis" }, + { "path": "../../../vendor/schemastery" }, + { "path": "../../fs/fs" }, + { "path": "../skill" } + ] +} diff --git a/packages/skill/skill/README.md b/packages/skill/skill/README.md new file mode 100644 index 0000000000..1f0c8d552b --- /dev/null +++ b/packages/skill/skill/README.md @@ -0,0 +1,36 @@ +# @deepseek-ai/dsh-skill + +Pure agent skill provider registry. + +This package owns the `ctx.skills` interface. It does not know whether skills come from local files, embedded plugin data, HTTP, or another backend; providers register those sources with `ctx.skills.registerProvider(...)`. The shipped local implementation is [`@deepseek-ai/dsh-skill-local`](../skill-local). + +## Service: `SkillService` (ctx key: `skills`) + +### Public API + +- `ctx.skills.registerProvider(provider): () => void` Registers a readonly provider by unique `provider.name`. Duplicate provider names throw, and `runtime` is reserved for `ctx.skills.register(...)`. The registry borrows the provider object and invokes its methods directly. The registration is effect-scoped and HMR-safe, and the exact Cordis disposer supports ordered composite teardown. +- `ctx.skills.list({ cwd?, signal? })` Borrows the readonly lookup options, then returns model-invocable summaries for the current workspace, merged across providers and sorted by name. +- `ctx.skills.get(name, { cwd?, signal? })` Uses the same readonly options and winning candidate for discovery and loading, rechecks cancellation after discovery or a cache hit, races provider loading against the signal, validates the loaded definition, then returns it, including disabled-for-model skills. +- `ctx.skills.register(skill): () => void` Registers a readonly runtime embedded skill, adding `provider: "runtime"` when omitted. Same-name runtime registrations are first-wins: a duplicate logs a warning and gets a no-op disposer. Successful registrations return the exact Cordis disposer for ordered composite teardown. + +### Config + +| Field | Default | Meaning | +|---|---|---| +| `collectCacheMaxEntries` | `128` | Maximum completed cwd/provider catalogs kept in memory. | + +## Provider Contract + +A provider registers synchronously from its `apply()` and returns `readonly SkillCandidate[]` from `list(options)` when discovery is requested. The provider, lookup options, candidates, and loaded definitions are readonly same-process contracts: the registry borrows them rather than cloning, freezing, or rebinding callbacks. Remote setup, authentication, and discovery belong in the awaited `list()` call rather than plugin registration. Providers should stop promptly when `options.signal` aborts; the registry also stops awaiting uncooperative discovery and loading work so agent cancellation cannot hang prefix composition or skill loading. + +The registry validates parsed provider candidates before caching them and validates loaded definitions before returning them. The winning provider receives the exact candidate and opaque `locator` identity it returned from `list()`; a local provider can therefore use a file-path handle while a remote provider can use a URL, id, or version token. Callers and providers must honor the readonly contract after handing values to the registry. + +Parsed candidate and loaded-definition fields are validated at the provider boundary: names/descriptions/content use their declared string types, ranks are finite numbers, and `disableModelInvocation` is boolean when present. Candidate contract violations fail fast because the provider or its parser is malformed; a provider `list()` rejection is treated as a transient source failure, logged, skipped for that request, and not cached. Only completed catalogs are cached, and a provider/runtime revision change during discovery discards the stale result and retries. Duplicate skill names are resolved first-wins by `rank`, provider registration order, then the provider's own local order. The final summary list is sorted by skill `name` for deterministic consumers. + +## Runtime Skills + +`ctx.skills.register(...)` is a convenience for embedded runtime skills. Runtime skills use rank `250`: project providers can override them, while they override the shipped local provider's custom and user roots. Runtime definitions and nested resource metadata are borrowed readonly; the service only materializes the top-level definition needed to supply the default `provider`. Registration is first-wins within runtime contributions, so a duplicate contribution cannot remove the active one through its disposer. + +## Consumer boundary + +The registry does not render model guidance or register model-facing tools. [`@deepseek-ai/dsh-tool-skill`](../tool-skill) consumes `ctx.skills` to provide the session-prefix catalog and `skill` tool, so providers remain independent of the model surface. diff --git a/packages/skill/skill/package.json b/packages/skill/skill/package.json new file mode 100644 index 0000000000..b303e16bed --- /dev/null +++ b/packages/skill/skill/package.json @@ -0,0 +1,33 @@ +{ + "name": "@deepseek-ai/dsh-skill", + "description": "Agent skill provider registry for the DeepSeek Harness", + "version": "0.0.1", + "private": true, + "type": "module", + "main": "lib/index.js", + "types": "lib/types/index.d.ts", + "exports": { + ".": { + "types": "./lib/types/index.d.ts", + "default": "./lib/index.js" + }, + "./src/*": "./src/*", + "./package.json": "./package.json" + }, + "files": [ + "lib/index.js", + "lib/types/**/*.d.ts", + "lib/types/**/*.d.ts.map", + "src" + ], + "license": "BSD-3-Clause", + "peerDependencies": { + "cordis": "^4.0.0-rc.6" + }, + "dependencies": { + "schemastery": "^3.18.0" + }, + "devDependencies": { + "cordis": "^4.0.0-rc.6" + } +} diff --git a/packages/skill/skill/src/index.ts b/packages/skill/skill/src/index.ts new file mode 100644 index 0000000000..5ef3f78465 --- /dev/null +++ b/packages/skill/skill/src/index.ts @@ -0,0 +1,559 @@ +/** + * Agent skill provider registry. + * + * This package is the interface third of the skill capability seam. Concrete + * providers such as `@deepseek-ai/dsh-skill-local` decide where skills come + * from; this service only merges provider catalogs, resolves the winning skill + * for a name, and exposes the winning summaries and definitions to consumers. + * + * @module @deepseek-ai/dsh-skill + */ + +import { Context, Service } from 'cordis' +import z from 'schemastery' +import type Schema from 'schemastery' + +const SKILL_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/ +const DEFAULT_COLLECT_CACHE_ENTRIES = 128 +const RUNTIME_PROVIDER = 'runtime' +const RUNTIME_RANK = 250 + +/** + * Return whether a string is a valid kebab-case skill name. + * @param name - candidate skill name to validate. + * @returns whether the name matches the public skill-name grammar. + */ +export function isSkillName(name: string): boolean { + return SKILL_NAME.test(name) +} + +/** Origin bucket for a skill contribution. The value is prompt-visible metadata, not precedence by itself. */ +export type SkillSource = 'project-dsh' | 'project-agents' | 'runtime' | 'user-dsh' | 'user-agents' | 'custom' | (string & {}) + +/** Optional provider-specific base used by loaded skill bodies to resolve relative resources. */ +export type SkillResourceBase = + | { readonly kind: 'directory'; readonly path: string } + | { readonly kind: 'url'; readonly url: string } + | { readonly kind: 'opaque'; readonly description: string } + +/** Model-visible skill metadata returned by `ctx.skills.list()` and rendered into request guidance. */ +export interface SkillSummary { + /** Kebab-case identifier used with the `skill` tool. */ + readonly name: string + /** Short routing description shown to the model. */ + readonly description: string + /** Optional extra routing guidance shown to the model. */ + readonly whenToUse?: string + /** Whether the skill is hidden from model listings while remaining loadable by trusted callers. */ + readonly disableModelInvocation?: boolean + /** Discovery source that produced this winning skill. */ + readonly source: SkillSource + /** Provider that owns this skill body. */ + readonly provider: string + /** Provider-specific base for relative resources. */ + readonly resourceBase?: SkillResourceBase +} + +/** Provider catalog entry used by the registry to merge and later load skills. */ +export interface SkillCandidate extends SkillSummary { + /** Lower ranks win duplicate skill names before provider registration order is considered. */ + readonly rank: number + /** Opaque provider-owned handle passed back to `provider.get()`. */ + readonly locator: unknown + /** Absolute file path when the provider has one. */ + readonly path?: string + /** Parsed optional metadata object from provider-specific skill frontmatter. */ + readonly metadata?: Readonly> +} + +/** Complete parsed skill definition, including the body loaded by `ctx.skills.get()`. */ +export interface SkillDefinition extends SkillSummary { + /** Markdown instruction body after any provider-specific metadata removal. */ + readonly content: string + /** Absolute file path when the skill came from disk. */ + readonly path?: string + /** Parsed optional metadata object from frontmatter. */ + readonly metadata?: Readonly> +} + +/** Runtime skill contribution accepted by `ctx.skills.register()`. */ +export type SkillRegistration = Omit & { readonly provider?: string } + +/** Caller context used for cwd-sensitive and abortable provider work. */ +export interface SkillLookupOptions { + /** Workspace selector for the current lookup. */ + readonly cwd?: string | undefined + /** Abort discovery or loading work for the current caller. */ + readonly signal?: AbortSignal | undefined +} + +/** Provider interface for one source of skills, such as local directories or a remote registry. */ +export interface SkillProvider { + /** Unique provider name in the `ctx.skills` registry. */ + readonly name: string + /** + * List available skill candidates for the current lookup context. Provider + * plugins register synchronously during `apply()`; remote initialization, + * authentication, and discovery are awaited inside this method. Implementations + * should settle promptly when `options.signal` aborts. + * @param options - lookup options; `cwd` selects workspace-sensitive skills and `signal` cancels work. + * @returns provider candidates with precedence ranks and opaque locators. + */ + readonly list: (options: SkillLookupOptions) => Promise + /** + * Load a complete skill body for a previously listed candidate. + * @param candidate - the winning candidate originally returned by this provider. + * @param options - lookup options; `cwd` selects workspace-sensitive skills and `signal` cancels work. + * @returns the full skill body, or `undefined` if it is no longer loadable. + */ + readonly get: (candidate: SkillCandidate, options: SkillLookupOptions) => Promise +} + +/** Skill registry configuration. */ +export interface Config { + /** Maximum number of completed cwd/provider catalogs kept in memory. */ + readonly collectCacheMaxEntries?: number +} + +declare module 'cordis' { + interface Context { + skills: SkillService + } + + interface Events { + /** + * A skill provider became resolvable in the `ctx.skills` registry. + * Consumers can observe this instead of depending on Cordis plugin load + * order, which is concurrent for sibling plugins. + * @param provider - the provider that just registered. + * @mode emit + */ + 'skill/provider-added'(provider: SkillProvider): void + /** + * A skill provider left the registry because its plugin fiber was disposed. + * @param name - the registry name that no longer resolves. + * @mode emit + */ + 'skill/provider-removed'(name: string): void + } +} + +interface IndexedCandidate { + candidate: SkillCandidate + provider: SkillProvider + providerOrder: number + localOrder: number +} + +interface CollectResult { + entries: IndexedCandidate[] + cacheable: boolean +} + +/** + * Registry of skill providers. It merges provider catalogs with stable + * first-wins duplicate handling, exposes sorted model-visible summaries, and + * loads full skill bodies on demand. + */ +export class SkillService extends Service { + static Config: Schema = z.object({ + collectCacheMaxEntries: z.number().default(DEFAULT_COLLECT_CACHE_ENTRIES), + }) + + private readonly collectCacheMaxEntries: number + private readonly providers = new Map() + private readonly runtime = new Map() + private readonly collectCache = new Map() + private providerRevision = 0 + private nextProviderOrder = 0 + private runtimeRevision = 0 + + constructor(ctx: Context, config: Config = {}) { + super(ctx, 'skills') + this.collectCacheMaxEntries = config.collectCacheMaxEntries ?? DEFAULT_COLLECT_CACHE_ENTRIES + assertPositiveInteger('collectCacheMaxEntries', this.collectCacheMaxEntries) + } + + /** + * Register a skill provider synchronously during the provider plugin's + * `apply()`. Throws if another provider already owns the same provider name, + * including the reserved runtime provider name. Providers that need remote + * initialization do that work inside `list()` after registration. Providers + * are readonly same-process registrations: the registry borrows the provider + * object and invokes its methods directly. Effect-scoped and HMR-safe: + * disposing the caller's fiber unregisters the provider and invalidates + * cached catalogs. + * @param provider - the provider to register by `provider.name`. + * @returns the exact Cordis effect disposer that unregisters this provider; + * composite effects may yield it directly to preserve teardown ordering. + */ + registerProvider(provider: SkillProvider): () => void { + const name = provider.name + if (name === RUNTIME_PROVIDER) { + throw new Error(`"${RUNTIME_PROVIDER}" is reserved for runtime skill registrations`) + } + if (this.providers.has(name)) { + throw new Error(`a skill provider named "${name}" is already registered`) + } + const providers = this.providers + const ctx = this.ctx + const order = this.nextProviderOrder + const invalidateCache = (): void => { this.invalidateCache() } + this.nextProviderOrder += 1 + const dispose = ctx.effect(function* () { + providers.set(name, { provider, order }) + invalidateCache() + yield () => { + providers.delete(name) + invalidateCache() + ctx.emit('skill/provider-removed', name) + } + ctx.emit('skill/provider-added', provider) + }, 'skills.registerProvider()') + // eslint-disable-next-line @typescript-eslint/no-misused-promises -- synchronous cleanup; direct return preserves disposer identity + return dispose + } + + /** + * Register a runtime skill contribution. Runtime registrations are treated as + * embedded provider entries with project-over-user priority. Same-name runtime + * registrations are first-wins: a duplicate logs a warning and gets a no-op + * disposer so it cannot remove the active contribution. Runtime definitions + * are readonly same-process registrations; the registry borrows their nested + * resource metadata. + * @param skill - the complete skill definition to expose for discovery. + * @returns the exact Cordis effect disposer that removes this runtime + * contribution and invalidates caches; composite effects may yield it + * directly to preserve teardown ordering. + */ + register(skill: SkillRegistration): () => void { + validateRuntimeSkill(skill) + const existing = this.runtime.get(skill.name) + if (existing !== undefined) { + this.ctx.logger.warn(`runtime skill "${skill.name}" ignored because it is already registered`) + return () => {} + } + const runtime = this.runtime + const updateRevision = (): void => { this.runtimeRevision += 1 } + const invalidateCache = (): void => { this.invalidateCache() } + const dispose = this.ctx.effect(function* () { + runtime.set(skill.name, skill) + updateRevision() + invalidateCache() + yield () => { + runtime.delete(skill.name) + updateRevision() + invalidateCache() + } + }, 'skills.register()') + // eslint-disable-next-line @typescript-eslint/no-misused-promises -- synchronous cleanup; direct return preserves disposer identity + return dispose + } + + /** + * List model-invocable skill summaries for a workspace. Lookup options and + * provider candidates are readonly same-process values borrowed throughout + * discovery. + * @param options - lookup options; `cwd` selects project roots and `signal` cancels discovery. + * @returns sorted summaries, excluding skills disabled for model invocation. + */ + async list(options: SkillLookupOptions = {}): Promise { + return (await this.collect(options)) + .map(entry => entry.candidate) + .filter(skill => skill.disableModelInvocation !== true) + .map(toSummary) + .sort(compareSkillSummary) + } + + /** + * Load one full skill definition by name. The provider receives the winning + * candidate it returned during discovery, including its opaque locator, and + * the registry returns the provider's definition after validating it. + * Cancellation is rechecked after catalog + * selection (including a cache hit), and provider loading is raced against the + * same signal so an uncooperative provider cannot hang the caller. + * @param name - kebab-case skill name. + * @param options - lookup options; `cwd` selects workspace-sensitive skills and `signal` cancels work. + * @returns the full skill, including body content, or `undefined`. + */ + async get(name: string, options: SkillLookupOptions = {}): Promise { + if (!isSkillName(name)) return undefined + const collected = await this.collect(options) + throwIfAborted(options.signal) + const match = collected.find(entry => entry.candidate.name === name) + if (match === undefined) return undefined + const definition = await waitWithAbort( + match.provider.get(match.candidate, options), + options.signal, + ) + if (definition === undefined) return undefined + validateDefinition(definition) + return definition + } + + private async collect(options: SkillLookupOptions): Promise { + throwIfAborted(options.signal) + while (true) { + const providerRevision = this.providerRevision + const runtimeRevision = this.runtimeRevision + const key = collectCacheKey(options, providerRevision, runtimeRevision) + const cached = this.collectCache.get(key) + if (cached !== undefined) return cached + + const result = await this.collectFresh(options) + throwIfAborted(options.signal) + if (providerRevision !== this.providerRevision || runtimeRevision !== this.runtimeRevision) continue + if (result.cacheable) { + this.collectCache.set(key, result.entries) + if (this.collectCache.size > this.collectCacheMaxEntries) { + const oldest = this.collectCache.keys().next() as IteratorYieldResult + this.collectCache.delete(oldest.value) + } + } + return result.entries + } + } + + private async collectFresh(options: SkillLookupOptions): Promise { + const collected = await this.listAllCandidates(options) + collected.entries.sort(compareIndexedCandidates) + const seen = new Set() + const result: IndexedCandidate[] = [] + for (const entry of collected.entries) { + const skill = entry.candidate + if (seen.has(skill.name)) { + this.ctx.logger.warn(`skill "${skill.name}" from ${skill.source} ignored because a higher-priority skill already exists`) + continue + } + seen.add(skill.name) + result.push(entry) + } + return { entries: result, cacheable: collected.cacheable } + } + + private async listAllCandidates(options: SkillLookupOptions): Promise { + throwIfAborted(options.signal) + const candidates: IndexedCandidate[] = [] + let cacheable = true + let runtimeOrder = 0 + for (const skill of [...this.runtime.values()].sort((a, b) => compareCodePoints(a.name, b.name))) { + candidates.push({ + candidate: runtimeCandidate(skill), + provider: RUNTIME_SKILL_PROVIDER, + providerOrder: -1, + localOrder: runtimeOrder, + }) + runtimeOrder += 1 + } + for (const { provider, order } of [...this.providers.values()]) { + let localOrder = 0 + let output: unknown + try { + output = await waitWithAbort(provider.list(options), options.signal) + } catch (error) { + if (options.signal?.aborted === true) throw toError(options.signal.reason) + cacheable = false + this.ctx.logger.warn(`skill provider "${provider.name}" skipped: ${errorMessage(error)}`) + } + if (output === undefined) continue + if (!Array.isArray(output)) { + throw new TypeError(`skill provider "${provider.name}" list() must return an array`) + } + const listed = output as readonly SkillCandidate[] + for (const candidate of listed) { + validateCandidate(candidate, provider.name) + candidates.push({ candidate, provider, providerOrder: order, localOrder }) + localOrder += 1 + } + } + return { entries: candidates, cacheable } + } + + private invalidateCache(): void { + this.providerRevision += 1 + this.collectCache.clear() + } +} + +const RUNTIME_SKILL_PROVIDER: SkillProvider = { + name: RUNTIME_PROVIDER, + /* v8 ignore next -- Runtime skills are injected directly by the registry; this provider only owns `get()`. */ + list() { + return Promise.resolve([]) + }, + get(candidate) { + const skill = candidate.locator as SkillRegistration + return Promise.resolve({ ...skill, provider: skill.provider ?? RUNTIME_PROVIDER }) + }, +} + +function runtimeCandidate(skill: SkillRegistration): SkillCandidate { + return { + name: skill.name, + description: skill.description, + ...skill.whenToUse !== undefined ? { whenToUse: skill.whenToUse } : {}, + ...skill.disableModelInvocation !== undefined ? { disableModelInvocation: skill.disableModelInvocation } : {}, + source: skill.source, + provider: skill.provider ?? RUNTIME_PROVIDER, + ...skill.resourceBase !== undefined ? { resourceBase: skill.resourceBase } : {}, + rank: RUNTIME_RANK, + locator: skill, + ...skill.path !== undefined ? { path: skill.path } : {}, + ...skill.metadata !== undefined ? { metadata: skill.metadata } : {}, + } +} + +function validateCandidate(candidate: SkillCandidate, providerName: string): void { + if (typeof candidate.name !== 'string') { + throw new TypeError(`skill provider "${providerName}" returned a non-string skill name`) + } + if (!SKILL_NAME.test(candidate.name)) { + throw new Error(`skill provider "${providerName}" returned invalid skill name "${candidate.name}"`) + } + if (typeof candidate.description !== 'string') { + throw new TypeError(`skill provider "${providerName}" returned skill "${candidate.name}" with a non-string description`) + } + if (candidate.description.length === 0) { + throw new Error(`skill provider "${providerName}" returned skill "${candidate.name}" without a description`) + } + if (candidate.disableModelInvocation !== undefined && typeof candidate.disableModelInvocation !== 'boolean') { + throw new TypeError(`skill provider "${providerName}" returned skill "${candidate.name}" with a non-boolean disableModelInvocation`) + } + if (candidate.whenToUse !== undefined && typeof candidate.whenToUse !== 'string') { + throw new TypeError(`skill provider "${providerName}" returned skill "${candidate.name}" with a non-string whenToUse`) + } + if (typeof candidate.source !== 'string') { + throw new TypeError(`skill provider "${providerName}" returned skill "${candidate.name}" with a non-string source`) + } + if (typeof candidate.rank !== 'number' || !Number.isFinite(candidate.rank)) { + throw new Error(`skill provider "${providerName}" returned skill "${candidate.name}" with an invalid rank`) + } + if (typeof candidate.provider !== 'string') { + throw new TypeError(`skill provider "${providerName}" returned skill "${candidate.name}" with a non-string provider`) + } + if (candidate.provider !== providerName) { + throw new Error(`skill provider "${providerName}" returned skill "${candidate.name}" for provider "${candidate.provider}"`) + } + if (candidate.path !== undefined && typeof candidate.path !== 'string') { + throw new TypeError(`skill provider "${providerName}" returned skill "${candidate.name}" with a non-string path`) + } +} + +function validateRuntimeSkill(skill: SkillRegistration): void { + if (!SKILL_NAME.test(skill.name)) throw new Error(`invalid skill name "${skill.name}"`) + if (skill.description.length === 0) throw new Error(`skill "${skill.name}" requires a description`) +} + +/** Validate a definition loaded from a provider-controlled parser or remote source. */ +function validateDefinition(skill: SkillDefinition): void { + const name = skill.name + const description = skill.description + const whenToUse = skill.whenToUse + const disableModelInvocation = skill.disableModelInvocation + const source = skill.source + const provider = skill.provider + const content = skill.content + const path = skill.path + if (typeof name !== 'string') throw new TypeError('loaded skill name must be a string') + if (!SKILL_NAME.test(name)) throw new Error(`loaded skill has invalid name "${name}"`) + if (typeof description !== 'string') throw new TypeError(`loaded skill "${name}" description must be a string`) + if (description.length === 0) throw new Error(`loaded skill "${name}" requires a description`) + if (disableModelInvocation !== undefined && typeof disableModelInvocation !== 'boolean') { + throw new TypeError(`loaded skill "${name}" disableModelInvocation must be a boolean`) + } + if (whenToUse !== undefined && typeof whenToUse !== 'string') throw new TypeError(`loaded skill "${name}" whenToUse must be a string`) + if (typeof source !== 'string') throw new TypeError(`loaded skill "${name}" source must be a string`) + if (typeof provider !== 'string') throw new TypeError(`loaded skill "${name}" provider must be a string`) + if (typeof content !== 'string') throw new TypeError(`loaded skill "${name}" content must be a string`) + if (path !== undefined && typeof path !== 'string') throw new TypeError(`loaded skill "${name}" path must be a string`) +} + +function toSummary(skill: SkillDefinition | SkillCandidate): SkillSummary { + const { name, description, whenToUse, disableModelInvocation, source, provider, resourceBase } = skill + return { + name, + description, + ...whenToUse !== undefined ? { whenToUse } : {}, + ...disableModelInvocation !== undefined ? { disableModelInvocation } : {}, + source, + provider, + ...resourceBase !== undefined ? { resourceBase } : {}, + } +} + +function compareSkillSummary(left: SkillSummary, right: SkillSummary): number { + return compareCodePoints(left.name, right.name) +} + +function compareCodePoints(left: string, right: string): number { + if (left < right) return -1 + if (left > right) return 1 + return 0 +} + +function compareIndexedCandidates(left: IndexedCandidate, right: IndexedCandidate): number { + return left.candidate.rank - right.candidate.rank + || left.providerOrder - right.providerOrder + || left.localOrder - right.localOrder +} + +function assertPositiveInteger(name: string, value: number, minimum = 1): void { + if (!Number.isInteger(value) || value < minimum) { + throw new Error(`skill: ${name} must be an integer greater than or equal to ${minimum}`) + } +} + +function collectCacheKey(options: SkillLookupOptions, providerRevision: number, runtimeRevision: number): string { + return JSON.stringify({ cwd: options.cwd, providerRevision, runtimeRevision }) +} + +function waitWithAbort(promise: Promise, signal: AbortSignal | undefined): Promise { + if (signal === undefined) return promise + throwIfAborted(signal) + return new Promise((resolve, reject) => { + const cleanup = (): void => { + signal.removeEventListener('abort', onAbort) + } + const onAbort = (): void => { + cleanup() + reject(toError(signal.reason)) + } + signal.addEventListener('abort', onAbort, { once: true }) + void promise.then( + (value) => { + cleanup() + resolve(value) + }, + (error: unknown) => { + cleanup() + reject(toError(error)) + }, + ) + }) +} + +/** Throw a total Error for an already-aborted lookup. */ +function throwIfAborted(signal: AbortSignal | undefined): void { + if (signal?.aborted === true) throw toError(signal.reason) +} + +/** Normalize an arbitrary abort or provider failure without trusting coercion. */ +function toError(error: unknown): Error { + try { + if (error instanceof Error) return error + } catch { + // A hostile proxy may throw during instanceof; fall through to the total renderer. + } + return new Error(errorMessage(error)) +} + +/** Render an arbitrary provider failure without letting coercion escape containment. */ +function errorMessage(error: unknown): string { + try { + return String(error) + } catch { + return '[unrenderable thrown value]' + } +} + +export default SkillService diff --git a/packages/skill/skill/tests/skill.spec.ts b/packages/skill/skill/tests/skill.spec.ts new file mode 100644 index 0000000000..8195d418cb --- /dev/null +++ b/packages/skill/skill/tests/skill.spec.ts @@ -0,0 +1,681 @@ +import { describe, expect, it, vi } from 'vitest' +import { Context } from 'cordis' +import SkillService, { type SkillCandidate, type SkillDefinition, type SkillLookupOptions, type SkillProvider } from '@deepseek-ai/dsh-skill' + +function memorySkill(name: string, description: string, rank: number, body = `${name} body.`): SkillCandidate { + return { + name, + description, + provider: 'memory', + source: 'memory', + rank, + locator: { content: body }, + } +} + +class MemoryProvider implements SkillProvider { + readonly name = 'memory' + listCalls = 0 + + constructor(private candidates: SkillCandidate[]) {} + + async list(_options: SkillLookupOptions): Promise { + this.listCalls += 1 + return this.candidates + } + + async get(candidate: SkillCandidate): Promise { + const locator = candidate.locator as { content: string } + return { ...candidate, content: locator.content } + } + + replace(candidates: SkillCandidate[]): void { + this.candidates = candidates + } +} + +describe('SkillService registry', () => { + it('registers providers, resolves duplicates first-wins, and disposes providers', async () => { + const ctx = new Context() + await ctx.plugin(SkillService) + const provider = new MemoryProvider([ + memorySkill('z-skill', 'Z skill', 20), + memorySkill('a-skill', 'A skill', 10), + memorySkill('shadowed', 'Lower priority', 20), + ]) + const overrideProvider: SkillProvider = { + name: 'override', + async list() { + return [{ + name: 'shadowed', + description: 'Higher priority', + provider: 'override', + source: 'override', + rank: 5, + locator: { content: 'Override body.' }, + }] + }, + async get(candidate) { + return { ...candidate, content: (candidate.locator as { content: string }).content } + }, + } + const disposeMemory = ctx.skills.registerProvider(provider) + ctx.skills.registerProvider(overrideProvider) + + expect((await ctx.skills.list()).map(skill => [skill.name, skill.description, skill.provider])).toEqual([ + ['a-skill', 'A skill', 'memory'], + ['shadowed', 'Higher priority', 'override'], + ['z-skill', 'Z skill', 'memory'], + ]) + expect((await ctx.skills.get('shadowed'))?.content).toBe('Override body.') + const sameRankProvider: SkillProvider = { + name: 'same-rank', + async list() { + return [{ + name: 'same-rank-skill', + description: 'Same rank', + provider: 'same-rank', + source: 'same-rank', + rank: 10, + locator: { content: 'Same rank body.' }, + }] + }, + async get(candidate) { + return { ...candidate, content: (candidate.locator as { content: string }).content } + }, + } + ctx.skills.registerProvider(sameRankProvider) + expect((await ctx.skills.list()).find(skill => skill.name === 'same-rank-skill')?.provider).toBe('same-rank') + await expect(ctx.plugin({ + name: 'duplicate-memory', + inject: ['skills'], + apply(pluginCtx: Context) { + pluginCtx.skills.registerProvider(new MemoryProvider([])) + }, + })).rejects.toThrow('already registered') + expect(() => ctx.skills.registerProvider({ + name: 'runtime', + async list() { + return [] + }, + async get() { + return undefined + }, + })).toThrow('reserved') + + disposeMemory() + expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['same-rank-skill', 'shadowed']) + }) + + it('validates parsed candidate fields', async () => { + const ctx = new Context() + await ctx.plugin(SkillService) + const badDescription = { value: 'object-description' } + ctx.skills.registerProvider({ + name: 'bad-candidate', + list: () => Promise.resolve([{ + ...memorySkill('bad-candidate', 'placeholder', 1), + provider: 'bad-candidate', + description: badDescription as unknown as string, + disableModelInvocation: 'false' as unknown as boolean, + }]), + get: () => Promise.resolve(undefined), + }) + await expect(ctx.skills.list()).rejects.toThrow('non-string description') + + const badBoolean = new Context() + await badBoolean.plugin(SkillService) + badBoolean.skills.registerProvider({ + name: 'bad-boolean', + list: () => Promise.resolve([{ + ...memorySkill('bad-boolean', 'Bad boolean', 1), + provider: 'bad-boolean', + disableModelInvocation: 'false' as unknown as boolean, + }]), + get: () => Promise.resolve(undefined), + }) + await expect(badBoolean.skills.list()).rejects.toThrow('non-boolean disableModelInvocation') + }) + + it('rejects non-array provider results and every malformed candidate scalar', async () => { + const badList = new Context() + await badList.plugin(SkillService) + badList.skills.registerProvider({ + name: 'non-array-list', + list: () => Promise.resolve({} as unknown as SkillCandidate[]), + get: () => Promise.resolve(undefined), + }) + await expect(badList.skills.list()).rejects.toThrow('list() must return an array') + + const cases: { patch: Partial; expected: string }[] = [ + { patch: { name: { value: 'candidate' } as unknown as string }, expected: 'non-string skill name' }, + { patch: { whenToUse: 1 as unknown as string }, expected: 'non-string whenToUse' }, + { patch: { source: { value: 'source' } as unknown as string }, expected: 'non-string source' }, + { patch: { rank: '1' as unknown as number }, expected: 'invalid rank' }, + { patch: { provider: { value: 'provider' } as unknown as string }, expected: 'non-string provider' }, + { patch: { path: 1 as unknown as string }, expected: 'non-string path' }, + ] + for (const [index, { patch, expected }] of cases.entries()) { + const ctx = new Context() + await ctx.plugin(SkillService) + const providerName = `candidate-provider-${index}` + const candidate = { + name: `candidate-${index}`, + description: 'Candidate', + whenToUse: 'Use this candidate.', + disableModelInvocation: false, + provider: providerName, + source: 'test', + rank: 1, + locator: 'candidate', + path: '/skills/candidate/SKILL.md', + ...patch, + } as SkillCandidate + ctx.skills.registerProvider({ + name: providerName, + list: () => Promise.resolve([candidate]), + get: () => Promise.resolve(undefined), + }) + + await expect(ctx.skills.list()).rejects.toThrow(expected) + } + }) + + it('borrows the exact lookup options through discovery and loading', async () => { + const ctx = new Context() + await ctx.plugin(SkillService) + const options: SkillLookupOptions = { cwd: '/workspace/a' } + let listedWith: SkillLookupOptions | undefined + let loadedWith: SkillLookupOptions | undefined + const candidate: SkillCandidate = { + name: 'skill-a', + description: 'Skill A', + provider: 'contextual', + source: 'test', + rank: 1, + locator: 'skill-a', + } + ctx.skills.registerProvider({ + name: 'contextual', + async list(received) { + listedWith = received + return [candidate] + }, + async get(received, lookup) { + expect(received).toBe(candidate) + loadedWith = lookup + return { ...received, content: 'Skill A body.' } + }, + }) + + expect((await ctx.skills.list(options)).map(skill => skill.name)).toEqual(['skill-a']) + expect(await ctx.skills.get('skill-a', options)).toMatchObject({ content: 'Skill A body.' }) + expect(listedWith).toBe(options) + expect(loadedWith).toBe(options) + }) + + it('rechecks cancellation after cached discovery before provider loading', async () => { + const ctx = new Context() + await ctx.plugin(SkillService) + let getCalls = 0 + ctx.skills.registerProvider({ + name: 'cached', + async list() { + return [{ + name: 'cached-skill', + description: 'Cached skill', + provider: 'cached', + source: 'test', + rank: 1, + locator: 'cached', + }] + }, + async get(candidate) { + getCalls += 1 + return { ...candidate, content: 'Cached body.' } + }, + }) + await ctx.skills.list({ cwd: '/workspace/cache' }) + const controller = new AbortController() + const reason = new Error('cancelled after cached discovery') + + const pending = ctx.skills.get('cached-skill', { + cwd: '/workspace/cache', + signal: controller.signal, + }) + controller.abort(reason) + + await expect(pending).rejects.toBe(reason) + expect(getCalls).toBe(0) + }) + + it('stops waiting for cached provider loading when a hostile abort reason fires', async () => { + const ctx = new Context() + await ctx.plugin(SkillService) + let markStarted: (() => void) | undefined + let release: (() => void) | undefined + let seenSignal: AbortSignal | undefined + const started = new Promise((resolve) => { markStarted = resolve }) + const held = new Promise((resolve) => { + release = () => { + resolve({ + name: 'held-skill', + description: 'Held skill', + provider: 'held', + source: 'test', + content: 'Held body.', + }) + } + }) + ctx.skills.registerProvider({ + name: 'held', + async list() { + return [{ + name: 'held-skill', + description: 'Held skill', + provider: 'held', + source: 'test', + rank: 1, + locator: 'held', + }] + }, + get(_candidate, options) { + seenSignal = options.signal + markStarted?.() + return held + }, + }) + await ctx.skills.list({ cwd: '/workspace/cache' }) + const controller = new AbortController() + const hostileReason = { + [Symbol.toPrimitive]() { + throw new Error('abort reason coercion failed') + }, + } + const pending = ctx.skills.get('held-skill', { + cwd: '/workspace/cache', + signal: controller.signal, + }) + const outcome = pending.then( + () => 'resolved', + (error: unknown) => error instanceof Error && error.message === '[unrenderable thrown value]' + ? 'aborted' + : 'other-error', + ) + await started + controller.abort(hostileReason) + + const settled = await Promise.race([ + outcome, + new Promise<'timeout'>(resolve => setTimeout(() => { resolve('timeout') }, 25)), + ]) + release?.() + await pending.catch(() => undefined) + + expect(seenSignal).toBe(controller.signal) + expect(settled).toBe('aborted') + }) + + it('borrows cached candidates and loaded definitions from the provider', async () => { + const ctx = new Context() + await ctx.plugin(SkillService) + const locator = { id: 'provider-owned' } + const candidate: SkillCandidate = { + name: 'stable-skill', + description: 'Stable description', + whenToUse: 'When stability matters.', + disableModelInvocation: false, + provider: 'detached', + source: 'test', + resourceBase: { kind: 'opaque', description: 'candidate resources' }, + rank: 1, + locator, + path: '/skills/stable/SKILL.md', + metadata: { owner: 'candidate' }, + } + const definition: SkillDefinition = { + name: 'stable-skill', + description: 'Stable description', + whenToUse: 'When stability matters.', + disableModelInvocation: false, + provider: 'detached', + source: 'test', + resourceBase: { kind: 'opaque', description: 'definition resources' }, + path: '/skills/stable/SKILL.md', + metadata: { owner: 'definition' }, + content: 'Stable body.', + } + let listCalls = 0 + let received: SkillCandidate | undefined + ctx.skills.registerProvider({ + name: 'detached', + async list() { + listCalls += 1 + return [candidate] + }, + async get(loaded) { + received = loaded + return definition + }, + }) + + const listed = await ctx.skills.list() + expect(listed).toEqual([expect.objectContaining({ + name: 'stable-skill', + description: 'Stable description', + resourceBase: { kind: 'opaque', description: 'candidate resources' }, + })]) + expect(listed[0]?.resourceBase).toBe(candidate.resourceBase) + expect(listCalls).toBe(1) + + const loaded = await ctx.skills.get('stable-skill') + expect(received).toBe(candidate) + expect(received?.locator).toBe(locator) + expect(loaded).toBe(definition) + }) + + it('preserves readonly runtime resource identities while adding the default provider', async () => { + const ctx = new Context() + await ctx.plugin(SkillService) + const resourceBase = { kind: 'opaque' as const, description: 'runtime resources' } + const metadata = { owner: 'runtime' } + const registration = { + name: 'runtime-skill', + description: 'Runtime', + whenToUse: 'When runtime data is needed.', + disableModelInvocation: false, + source: 'runtime', + resourceBase, + metadata, + content: 'Runtime body.', + } + ctx.skills.register(registration) + ctx.skills.register({ + name: 'z-runtime', + description: 'Second runtime skill', + source: 'runtime', + content: 'Second runtime body.', + }) + const listed = await ctx.skills.list() + const loaded = await ctx.skills.get('runtime-skill') + expect(listed[0]?.resourceBase).toBe(resourceBase) + expect(loaded?.resourceBase).toBe(resourceBase) + expect(loaded?.metadata).toBe(metadata) + expect(loaded?.provider).toBe('runtime') + }) + + it('rejects every malformed scalar in provider-loaded definitions', async () => { + const cases: { patch: Partial; expected: string }[] = [ + { patch: { name: { value: 'loaded' } as unknown as string }, expected: 'loaded skill name must be a string' }, + { patch: { name: 'Bad_Name' }, expected: 'loaded skill has invalid name' }, + { patch: { description: { value: 'description' } as unknown as string }, expected: 'description must be a string' }, + { patch: { description: '' }, expected: 'requires a description' }, + { patch: { disableModelInvocation: 'false' as unknown as boolean }, expected: 'disableModelInvocation must be a boolean' }, + { patch: { whenToUse: 1 as unknown as string }, expected: 'whenToUse must be a string' }, + { patch: { source: { value: 'source' } as unknown as string }, expected: 'source must be a string' }, + { patch: { provider: { value: 'provider' } as unknown as string }, expected: 'provider must be a string' }, + { patch: { content: { value: 'content' } as unknown as string }, expected: 'content must be a string' }, + { patch: { path: 1 as unknown as string }, expected: 'path must be a string' }, + ] + for (const [index, { patch, expected }] of cases.entries()) { + const ctx = new Context() + await ctx.plugin(SkillService) + const providerName = `definition-provider-${index}` + const skillName = `definition-${index}` + ctx.skills.registerProvider({ + name: providerName, + list: () => Promise.resolve([{ + name: skillName, + description: 'Candidate', + provider: providerName, + source: 'test', + rank: 1, + locator: 'definition', + }]), + get: () => Promise.resolve({ + name: skillName, + description: 'Definition', + whenToUse: 'Use this definition.', + disableModelInvocation: false, + provider: providerName, + source: 'test', + content: 'Definition body.', + path: '/skills/definition/SKILL.md', + ...patch, + } as SkillDefinition), + }) + + await expect(ctx.skills.get(skillName)).rejects.toThrow(expected) + } + }) + + it('validates provider candidates and invalid registry caps', async () => { + const defaultedService = new SkillService(new Context()) + expect(await defaultedService.list()).toEqual([]) + + const ctx = new Context() + await ctx.plugin(SkillService) + ctx.skills.registerProvider({ + name: 'bad', + async list() { + return [memorySkill('Bad_Name', 'bad', 1)] + }, + async get() { + return undefined + }, + }) + await expect(ctx.skills.list()).rejects.toThrow('invalid skill name') + + const invalidCandidates = [ + { ...memorySkill('empty-description', '', 1), provider: 'empty-description' }, + { ...memorySkill('bad-rank', 'Bad rank', Number.NaN), provider: 'bad-rank' }, + { ...memorySkill('wrong-provider', 'Wrong provider', 1), provider: 'different' }, + ] + for (const candidate of invalidCandidates) { + const invalid = new Context() + await invalid.plugin(SkillService) + invalid.skills.registerProvider({ + name: candidate.name, + async list() { + return [candidate] + }, + async get() { + return undefined + }, + }) + await expect(invalid.skills.list()).rejects.toThrow('skill provider') + } + + await expect(new Context().plugin(SkillService, { collectCacheMaxEntries: 1.5 })).rejects.toThrow('collectCacheMaxEntries') + }) + + it('sorts model-visible summaries without locale-sensitive collation', async () => { + const ctx = new Context() + await ctx.plugin(SkillService) + ctx.skills.registerProvider(new MemoryProvider([ + memorySkill('z-skill', 'Z skill', 10), + memorySkill('a-skill', 'A skill', 10), + ])) + const localeCompare = vi.spyOn(String.prototype, 'localeCompare') + const sort = vi.spyOn(Array.prototype, 'sort') + + try { + const skills = await ctx.skills.list() + expect(skills.map(skill => skill.name)).toEqual(['a-skill', 'z-skill']) + expect(localeCompare).not.toHaveBeenCalled() + + const summaryComparator = sort.mock.calls.at(-1)?.[0] + expect(summaryComparator).toBeTypeOf('function') + expect(summaryComparator?.(skills[0], skills[0])).toBe(0) + } finally { + sort.mockRestore() + localeCompare.mockRestore() + } + }) + + it('caches provider discovery, skips failing providers, and invalidates on runtime skills', async () => { + const ctx = new Context() + await ctx.plugin(SkillService, { collectCacheMaxEntries: 1 }) + const provider = new MemoryProvider([memorySkill('first-skill', 'First', 10)]) + ctx.skills.registerProvider(provider) + + expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['first-skill']) + provider.replace([memorySkill('second-skill', 'Second', 10)]) + expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['first-skill']) + + const disposeRuntime = ctx.skills.register({ + name: 'runtime-skill', + description: 'Runtime', + source: 'runtime', + resourceBase: { kind: 'opaque', description: 'runtime memory' }, + path: 'memory://runtime-skill', + metadata: { owner: 'tests' }, + content: 'Runtime body.', + }) + expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['runtime-skill', 'second-skill']) + expect(await ctx.skills.get('runtime-skill')).toMatchObject({ + content: 'Runtime body.', + path: 'memory://runtime-skill', + metadata: { owner: 'tests' }, + }) + disposeRuntime() + await ctx.skills.list({ cwd: '/tmp/first-cache-key' }) + await ctx.skills.list({ cwd: '/tmp/second-cache-key' }) + + let fail = true + let flakyCalls = 0 + ctx.skills.registerProvider({ + name: 'flaky', + async list() { + flakyCalls += 1 + if (fail) throw new Error('transient discovery failure') + return [{ ...memorySkill('flaky-skill', 'Flaky', 10), provider: 'flaky' }] + }, + async get() { + return undefined + }, + }) + expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['second-skill']) + expect(flakyCalls).toBe(1) + expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['second-skill']) + expect(flakyCalls).toBe(2) + fail = false + expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['flaky-skill', 'second-skill']) + expect(flakyCalls).toBe(3) + expect((await ctx.skills.list()).map(skill => skill.name)).toEqual(['flaky-skill', 'second-skill']) + expect(flakyCalls).toBe(3) + }) + + it('contains a provider rejection whose string coercion throws', async () => { + const ctx = new Context() + await ctx.plugin(SkillService) + const warnings: string[] = [] + ctx.logger.warn = ((message: unknown) => { warnings.push(String(message)) }) as typeof ctx.logger.warn + const hostileFailure = { + toString() { + throw new Error('provider failure coercion failed') + }, + } + ctx.skills.registerProvider({ + name: 'hostile-failure', + list() { + // Deliberately violate the provider contract to prove containment is total. + // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors + return Promise.reject(hostileFailure) + }, + async get() { + return undefined + }, + }) + + await expect(ctx.skills.list()).resolves.toEqual([]) + expect(warnings).toEqual([ + 'skill provider "hostile-failure" skipped: [unrenderable thrown value]', + ]) + }) + + it('abandons an in-flight catalog when provider registrations change', async () => { + const ctx = new Context() + await ctx.plugin(SkillService) + let markStarted: (() => void) | undefined + let release: (() => void) | undefined + const started = new Promise((resolve) => { markStarted = resolve }) + const gate = new Promise((resolve) => { release = resolve }) + const dispose = ctx.skills.registerProvider({ + name: 'delayed', + async list() { + markStarted?.() + await gate + return [{ ...memorySkill('stale-skill', 'Stale', 10), provider: 'delayed' }] + }, + async get(candidate) { + return { ...candidate, content: 'Stale body.' } + }, + }) + + const pending = ctx.skills.list() + await started + dispose() + release?.() + + expect(await pending).toEqual([]) + }) + + it('stops waiting for discovery when its lookup signal aborts', async () => { + const ctx = new Context() + await ctx.plugin(SkillService) + let markStarted: (() => void) | undefined + let release: (() => void) | undefined + let seenSignal: AbortSignal | undefined + const started = new Promise((resolve) => { markStarted = resolve }) + const held = new Promise((resolve) => { + release = () => { resolve([]) } + }) + ctx.skills.registerProvider({ + name: 'uncooperative', + list(options) { + seenSignal = options.signal + markStarted?.() + return held + }, + async get() { + return undefined + }, + }) + const controller = new AbortController() + const reason = 'discovery cancelled' + const pending = ctx.skills.list({ signal: controller.signal }) + const outcome = pending.then( + () => 'resolved', + (error: unknown) => error instanceof Error && error.message === reason ? 'aborted' : 'other-error', + ) + await started + controller.abort(reason) + + const settled = await Promise.race([ + outcome, + new Promise<'timeout'>(resolve => setTimeout(() => { resolve('timeout') }, 25)), + ]) + release?.() + await pending.catch(() => undefined) + + expect(seenSignal).toBe(controller.signal) + expect(settled).toBe('aborted') + }) + + it('rejects invalid runtime skill registrations and ignores duplicates', async () => { + const ctx = new Context() + await ctx.plugin(SkillService) + expect(() => ctx.skills.register({ name: 'Bad_Name', description: 'Bad', source: 'runtime', content: 'bad' })).toThrow('invalid skill name') + expect(() => ctx.skills.register({ name: 'no-description', description: '', source: 'runtime', content: 'bad' })).toThrow('requires a description') + expect(await ctx.skills.get('missing-skill')).toBeUndefined() + expect(await ctx.skills.get('Bad_Name')).toBeUndefined() + + const disposeFirst = ctx.skills.register({ name: 'same-skill', description: 'First', source: 'runtime', content: 'first' }) + const disposeSecond = ctx.skills.register({ name: 'same-skill', description: 'Second', source: 'runtime', content: 'second' }) + disposeSecond() + expect((await ctx.skills.get('same-skill'))?.description).toBe('First') + disposeFirst() + expect(await ctx.skills.get('same-skill')).toBeUndefined() + }) +}) diff --git a/packages/skill/skill/tsconfig.json b/packages/skill/skill/tsconfig.json new file mode 100644 index 0000000000..1b1855dcc4 --- /dev/null +++ b/packages/skill/skill/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "lib/types" + }, + "include": ["src"], + "references": [ + { "path": "../../../vendor/cosmokit" }, + { "path": "../../../vendor/cordis" }, + { "path": "../../../vendor/schemastery" } + ] +} diff --git a/packages/skill/tool-skill/README.md b/packages/skill/tool-skill/README.md new file mode 100644 index 0000000000..7e72f89000 --- /dev/null +++ b/packages/skill/tool-skill/README.md @@ -0,0 +1,21 @@ +# @deepseek-ai/dsh-tool-skill + +The model-facing skill catalog and `skill` tool. + +Requires `ctx.tools` and `ctx.skills` (`inject: ['tools', 'skills']`). + +## Session-prefix catalog + +The plugin contributes one user-role `` catalog through `agent/session-prefix`. It resolves skills for the calling session's cwd, forwards the prefix abort signal to discovery, and lists only sorted `name` and `description` entries; skill bodies, paths, sources, providers, and `whenToUse` hints remain outside the catalog. The catalog is omitted when no model-invocable skills are available, and also when that agent's tool view restricts away the shipped `skill` tool or resolves a same-name scoped shadow instead. This exact-definition check keeps prompt guidance, the model-visible schema, and executable dispatch aligned. + +`catalogDescriptionMaxLength` controls normalized, XML-escaped catalog descriptions. Its default is `500` and values must be integers of at least `3`, which reserves room for a truncation ellipsis. The [session-prefix RFC](../../../docs/rfc/implemented/feature/2026-07-07-session-prefix.md) defines the request-only, header-logged lifecycle of this message. + +## Tool: `skill` + +| Arg | Type | Notes | +|---|---|---| +| `name` | string (required) | Exact kebab-case skill name from the available skills listing. | + +Execution uses the calling agent's `session.header.cwd` so workspace-sensitive providers can resolve the right winning skill. A successful call returns one text tool result with ``, containing `` followed by ``. Resource guidance resolves paths or URLs explicitly referenced by the loaded instructions against `resourceBase`; referenced scripts, references, and assets load only when needed, and the tool does not enumerate a skill directory. Local filesystem skills provide a base directory, while remote or embedded providers can provide a URL or opaque provider-managed guidance. A name that cannot be resolved reports that the skill is unknown or no longer available; invalid names and skills marked `disableModelInvocation: true` retain distinct `isError` results. + +The tool does not call `agent.inject()` in v1. Its result is already recorded as the tool result and becomes available to the next model step without duplicating the content as synthetic context. diff --git a/packages/skill/tool-skill/package.json b/packages/skill/tool-skill/package.json new file mode 100644 index 0000000000..bf7a77a49e --- /dev/null +++ b/packages/skill/tool-skill/package.json @@ -0,0 +1,43 @@ +{ + "name": "@deepseek-ai/dsh-tool-skill", + "description": "Model-facing skill loading tool for the DeepSeek Harness", + "version": "0.0.1", + "private": true, + "type": "module", + "main": "lib/index.js", + "types": "lib/types/index.d.ts", + "exports": { + ".": { + "types": "./lib/types/index.d.ts", + "default": "./lib/index.js" + }, + "./src/*": "./src/*", + "./package.json": "./package.json" + }, + "files": [ + "lib/index.js", + "lib/types/**/*.d.ts", + "lib/types/**/*.d.ts.map", + "src" + ], + "license": "BSD-3-Clause", + "peerDependencies": { + "@deepseek-ai/dsh-agent": "^0.0.1", + "@deepseek-ai/dsh-llm": "^0.0.1", + "@deepseek-ai/dsh-skill": "^0.0.1", + "@deepseek-ai/dsh-tools": "^0.0.1", + "cordis": "^4.0.0-rc.6" + }, + "dependencies": { + "schemastery": "^3.18.0" + }, + "devDependencies": { + "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-llm": "workspace:^", + "@deepseek-ai/dsh-scope": "workspace:^", + "@deepseek-ai/dsh-skill": "workspace:^", + "@deepseek-ai/dsh-skill-local": "workspace:^", + "@deepseek-ai/dsh-tools": "workspace:^", + "cordis": "^4.0.0-rc.6" + } +} diff --git a/packages/skill/tool-skill/src/index.ts b/packages/skill/tool-skill/src/index.ts new file mode 100644 index 0000000000..39fa0253b4 --- /dev/null +++ b/packages/skill/tool-skill/src/index.ts @@ -0,0 +1,167 @@ +/** + * Session-prefix skill catalog and model-facing `skill` loader tool. + * + * @module @deepseek-ai/dsh-tool-skill + */ + +import type { Context } from 'cordis' +import z from 'schemastery' +import { defineTool } from '@deepseek-ai/dsh-tools' +import { assertNever, type Message } from '@deepseek-ai/dsh-llm' +import { isSkillName, type SkillDefinition, type SkillSummary } from '@deepseek-ai/dsh-skill' + +export const name = 'tool-skill' +export const inject = ['tools', 'skills'] + +const DEFAULT_CATALOG_DESCRIPTION_MAX_LENGTH = 500 + +/** Model-facing skill catalog configuration. */ +export interface Config { + /** Maximum normalized description length rendered in the session catalog; minimum 3. */ + catalogDescriptionMaxLength?: number +} + +/** Validate and default the model-facing skill catalog configuration. */ +export const Config: z = z.object({ + catalogDescriptionMaxLength: z.number().default(DEFAULT_CATALOG_DESCRIPTION_MAX_LENGTH), +}) + +/** + * Register the model-facing skill loader and its visibility-matched + * session-prefix catalog. The catalog is emitted only when the calling agent + * resolves this plugin's exact tool registration; a restriction or scoped + * same-name shadow therefore removes both the schema and its call guidance. + */ +export function apply(ctx: Context, config: Config = {}): void { + const catalogDescriptionMaxLength = config.catalogDescriptionMaxLength ?? DEFAULT_CATALOG_DESCRIPTION_MAX_LENGTH + assertPositiveInteger('catalogDescriptionMaxLength', catalogDescriptionMaxLength, 3) + + const skillTool = defineTool({ + name: 'skill', + description: 'Load the full instructions for an available skill. Call this with the exact skill name from the session skill catalog before acting on a task that names or clearly matches that skill.', + parameters: { + name: { type: 'string', required: true, description: 'The exact skill name from the available skills list.' }, + }, + async execute(args, exec) { + if (!isSkillName(args.name)) { + throw new Error(`invalid skill name "${args.name}"`) + } + const skill = await ctx.skills.get(args.name, { cwd: exec.agent?.session.header.cwd, signal: exec.signal }) + if (!skill) { + throw new Error(`skill "${args.name}" is unknown or no longer available`) + } + if (skill.disableModelInvocation === true) { + throw new Error(`skill "${args.name}" is not available for model invocation`) + } + return [{ type: 'text', text: renderSkillContent(skill) }] + }, + presentCall(args) { + return { card: 'generic', title: `Load skill ${args.name}`, kind: 'read', rawInput: args.name } + }, + }) + ctx.tools.register(skillTool) + const registeredSkillTool = ctx.tools.get(skillTool.name) + /* v8 ignore next 3 -- register() publishes synchronously or throws; this guards future registry drift. */ + if (registeredSkillTool === undefined) { + throw new Error('dsh-tool-skill: registered skill tool is not visible in the global registry') + } + + // Register after the tool so reverse-order fiber teardown removes this + // guidance listener before its referenced tool. Exact definition identity is + // the shared truth for restrictions and scoped shadows: another tool merely + // named `skill` must not inherit this plugin's catalog or instructions. + ctx.on('agent/session-prefix', async (agent, _prefix, signal, next): Promise => { + if (ctx.tools.get(skillTool.name, agent) !== registeredSkillTool) return await next() + const skills = await ctx.skills.list({ cwd: agent.session.header.cwd, signal }) + const rest = await next() + if (skills.length === 0) return rest + return [renderCatalogMessage(skills, catalogDescriptionMaxLength), ...rest] + }) +} + +function renderSkillContent(skill: SkillDefinition): string { + const resourceHint = renderResourceHint(skill) + return [ + ``, + '', + ...resourceHint, + '', + '', + '', + skill.content, + '', + '', + ].join('\n') +} + +function renderResourceHint(skill: SkillDefinition): string[] { + const base = skill.resourceBase + if (base === undefined) { + return [ + `Resources for this skill are managed by provider "${escapeText(skill.provider)}".`, + 'Load referenced resources only as needed.', + ] + } + switch (base.kind) { + case 'directory': + return [ + `Base directory for this skill: ${escapeText(base.path)}`, + 'Resolve relative paths mentioned by this skill against the base directory before using them. Load referenced resources only as needed.', + ] + case 'url': + return [ + `Base URL for this skill: ${escapeText(base.url)}`, + 'Resolve relative URLs mentioned by this skill against the base URL before using them. Load referenced resources only as needed.', + ] + case 'opaque': + return [ + `Resources for this skill: ${escapeText(base.description)}`, + 'Load referenced resources only as needed.', + ] + default: + return assertNever(base, 'SkillResourceBase.kind') + } +} + +function renderCatalogMessage(skills: SkillSummary[], descriptionMaxLength: number): Message { + const entries = skills.map(skill => `- \`${skill.name}\`: ${catalogDescription(skill.description, descriptionMaxLength)}`) + return { + role: 'user', + content: [{ + type: 'text', + text: [ + '', + 'A skill is a reusable set of task-specific instructions. The following skills are available in this session:', + '', + '', + ...entries, + '', + '', + "If the user names a skill, or the task clearly matches a skill's description, call the `skill` tool with the exact skill name before taking task actions. Load all applicable skills, then follow their full instructions. This catalog contains summaries only; do not infer or follow a skill's instructions until it has been loaded.", + '', + ].join('\n'), + }], + } +} + +function catalogDescription(value: string, maxLength: number): string { + const normalized = value.replaceAll(/\s+/g, ' ').trim() + const truncated = normalized.length <= maxLength + ? normalized + : `${normalized.slice(0, maxLength - 3)}...` + return escapeText(truncated) +} + +function assertPositiveInteger(name: string, value: number, minimum = 1): void { + if (!Number.isInteger(value) || value < minimum) { + throw new Error(`tool-skill: ${name} must be an integer greater than or equal to ${minimum}`) + } +} + +function escapeAttr(value: string): string { + return value.replaceAll('&', '&').replaceAll('"', '"').replaceAll('<', '<') +} + +function escapeText(value: string): string { + return value.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>') +} diff --git a/packages/skill/tool-skill/tests/tool-skill.spec.ts b/packages/skill/tool-skill/tests/tool-skill.spec.ts new file mode 100644 index 0000000000..cab7f4aa02 --- /dev/null +++ b/packages/skill/tool-skill/tests/tool-skill.spec.ts @@ -0,0 +1,323 @@ +import { describe, expect, it } from 'vitest' +import { mkdir, writeFile } from 'node:fs/promises' +import { join } from 'node:path' +import { tmpdir } from 'node:os' +import { Context } from 'cordis' +import { CallId, type Message } from '@deepseek-ai/dsh-llm' +import { createScope, type Scope } from '@deepseek-ai/dsh-scope' +import SystemPrompt, { renderPrompt } from '@deepseek-ai/dsh-system-prompt' +import ToolRegistry, { defineTool } from '@deepseek-ai/dsh-tools' +import { agentEvents, type Agent } from '@deepseek-ai/dsh-agent' +import SkillService from '@deepseek-ai/dsh-skill' +import * as SkillLocal from '@deepseek-ai/dsh-skill-local' +import * as toolSkill from '@deepseek-ai/dsh-tool-skill' + +async function tempDir(name: string): Promise { + return await import('node:fs/promises').then(fs => fs.mkdtemp(join(tmpdir(), `dsh-${name}-`))) +} + +async function writeSkill(root: string, name: string, description: string, body: string): Promise { + const dir = join(root, name) + await mkdir(dir, { recursive: true }) + await writeFile(join(dir, 'SKILL.md'), `---\nname: ${name}\ndescription: ${description}\n---\n\n${body}\n`) +} + +async function setup(home: string, config: toolSkill.Config = {}): Promise { + const ctx = new Context() + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(SkillService) + await ctx.plugin(SkillLocal, { dshHome: join(home, '.dsh'), agentsHome: join(home, '.agents') }) + await ctx.plugin(toolSkill, config) + return ctx +} + +function agentForCwd(cwd: string): Agent { + return { session: { header: { cwd } } } as unknown as Agent +} + +async function composePrefix(ctx: Context, cwd: string, signal = new AbortController().signal): Promise { + return await composePrefixForAgent(ctx, agentForCwd(cwd), signal) +} + +async function composePrefixForAgent(ctx: Context, agent: Agent, signal = new AbortController().signal): Promise { + const empty: Message[] = [] + return await agentEvents(ctx, agent).waterfall( + 'agent/session-prefix', empty, signal, + () => Promise.resolve(empty), + ) +} + +async function mintAgentScope(ctx: Context, cwd: string): Promise<{ agent: Agent; scope: Scope }> { + const agent = agentForCwd(cwd) + let scope!: Scope + await ctx.plugin(Object.assign((inner: Context) => { scope = createScope(inner, agent) }, { + inject: ['tools'], + })) + return { agent, scope } +} + +describe('dsh-tool-skill', () => { + it('registers the skill tool schema and removes it on dispose', async () => { + const ctx = new Context() + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + const home = await tempDir('tool-schema') + await ctx.plugin(SkillService) + await ctx.plugin(SkillLocal, { dshHome: join(home, '.dsh'), agentsHome: join(home, '.agents') }) + ctx.skills.register({ name: 'lifecycle-skill', description: 'Lifecycle', source: 'runtime', content: 'body' }) + + const fiber = await ctx.plugin(toolSkill) + expect(ctx.tools.schemas().map(tool => tool.name)).toEqual(['skill']) + expect(await composePrefix(ctx, '/workspace')).toHaveLength(1) + expect(ctx.tools.get('skill')?.presentCall?.({ name: 'project-skill' })).toEqual({ + card: 'generic', + title: 'Load skill project-skill', + kind: 'read', + rawInput: 'project-skill', + }) + await fiber.dispose() + expect(ctx.tools.schemas()).toEqual([]) + expect(await composePrefix(ctx, '/workspace')).toEqual([]) + + toolSkill.apply(ctx) + expect(ctx.tools.schemas().map(tool => tool.name)).toEqual(['skill']) + }) + + it('forwards the session-prefix abort signal to skill discovery', async () => { + const home = await tempDir('tool-prefix-signal') + const ctx = await setup(home) + let seenSignal: AbortSignal | undefined + ctx.skills.registerProvider({ + name: 'signal-probe', + async list(options) { + seenSignal = options.signal + return [] + }, + async get() { + return undefined + }, + }) + const controller = new AbortController() + + await composePrefix(ctx, '/workspace', controller.signal) + + expect(seenSignal).toBe(controller.signal) + }) + + it('contributes a stable name-and-description catalog through the session prefix', async () => { + const home = await tempDir('tool-catalog') + const ctx = await setup(home, { catalogDescriptionMaxLength: 50 }) + ctx.skills.register({ + name: 'z-skill', + description: 'Long description '.repeat(5), + whenToUse: 'Never render this routing hint.', + source: 'secret-source', + provider: 'runtime', + resourceBase: { kind: 'directory', path: '/secret/path' }, + content: 'Secret body.', + }) + ctx.skills.register({ + name: 'a-skill', + description: 'Use {{placeholder}} & carefully.', + source: 'runtime', + provider: 'runtime', + content: 'A body.', + }) + ctx.on('agent/session-prefix', async (_agent, _prefix, _signal, next): Promise => [ + { role: 'user', content: [{ type: 'text', text: 'later contribution' }] }, + ...await next(), + ]) + + const prefix = await composePrefix(ctx, '/workspace') + + expect(prefix).toEqual([ + { + role: 'user', + content: [{ + type: 'text', + text: [ + '', + 'A skill is a reusable set of task-specific instructions. The following skills are available in this session:', + '', + '', + '- `a-skill`: Use {{placeholder}} <safely> & carefully.', + '- `z-skill`: Long description Long description Long descript...', + '', + '', + "If the user names a skill, or the task clearly matches a skill's description, call the `skill` tool with the exact skill name before taking task actions. Load all applicable skills, then follow their full instructions. This catalog contains summaries only; do not infer or follow a skill's instructions until it has been loaded.", + '', + ].join('\n'), + }], + }, + { role: 'user', content: [{ type: 'text', text: 'later contribution' }] }, + ]) + const rendered = JSON.stringify(prefix[0]) + expect(rendered).not.toContain('whenToUse') + expect(rendered).not.toContain('secret-source') + expect(rendered).not.toContain('/secret/path') + expect(rendered).not.toContain('Secret body') + expect(renderPrompt(await ctx.systemPrompt.assemble({ agent: agentForCwd('/workspace') }))).not.toContain('') + }) + + it('does not contribute a session-prefix message when no skills are available', async () => { + const home = await tempDir('tool-empty-catalog') + const ctx = await setup(home) + + expect(await composePrefix(ctx, '/workspace')).toEqual([]) + }) + + it('omits catalog guidance when the calling agent restricts away the shipped skill tool', async () => { + const home = await tempDir('tool-restricted-catalog') + const ctx = await setup(home) + ctx.skills.register({ name: 'listed-skill', description: 'Listed', source: 'runtime', content: 'body' }) + const { agent, scope } = await mintAgentScope(ctx, '/workspace') + scope.ctx.tools.restrict({ deny: ['skill'] }) + + expect(ctx.tools.get('skill', agent)).toBeUndefined() + expect(await composePrefixForAgent(ctx, agent)).toEqual([]) + expect(await composePrefix(ctx, '/workspace')).toHaveLength(1) + await scope.dispose() + }) + + it('does not attach shipped catalog guidance to a scoped same-name tool shadow', async () => { + const home = await tempDir('tool-shadowed-catalog') + const ctx = await setup(home) + ctx.skills.register({ name: 'listed-skill', description: 'Listed', source: 'runtime', content: 'body' }) + const { agent, scope } = await mintAgentScope(ctx, '/workspace') + scope.ctx.tools.register(defineTool({ + name: 'skill', + description: 'A scoped tool with unrelated semantics.', + parameters: {}, + execute() { + return Promise.resolve([{ type: 'text', text: 'shadow' }]) + }, + })) + + expect(ctx.tools.get('skill', agent)).not.toBe(ctx.tools.get('skill')) + expect(await composePrefixForAgent(ctx, agent)).toEqual([]) + expect(await composePrefix(ctx, '/workspace')).toHaveLength(1) + await scope.dispose() + }) + + it('validates the catalog description cap', async () => { + const home = await tempDir('tool-invalid-catalog-cap') + const ctx = new Context() + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(SkillService) + await ctx.plugin(SkillLocal, { dshHome: join(home, '.dsh'), agentsHome: join(home, '.agents') }) + + await expect(ctx.plugin(toolSkill, { catalogDescriptionMaxLength: 2 })).rejects.toThrow('greater than or equal to 3') + }) + + it('loads a skill for the calling agent cwd', async () => { + const home = await tempDir('tool-load') + const project = await tempDir('tool-project') + await mkdir(join(project, '.git'), { recursive: true }) + await writeSkill(join(project, '.dsh/skills'), 'project-skill', 'Project skill', 'Project instructions.') + const ctx = await setup(home) + + const result = await ctx.tools.execute({ + callId: CallId('c1'), + name: 'skill', + arguments: { name: 'project-skill' }, + agent: { session: { header: { cwd: project } } } as never, + }) + + expect(result.isError).toBe(false) + const block = result.content[0] + expect(block?.type).toBe('text') + if (block?.type !== 'text') throw new Error('expected text skill result') + expect(block.text).toBe([ + '', + '', + `Base directory for this skill: ${join(project, '.dsh/skills/project-skill')}`, + 'Resolve relative paths mentioned by this skill against the base directory before using them. Load referenced resources only as needed.', + '', + '', + '', + 'Project instructions.', + '', + '', + ].join('\n')) + expect(block.text).not.toContain('# Skill:') + }) + + it('renders provider-managed resource hints for non-local skills', async () => { + const home = await tempDir('tool-resource-hints') + const ctx = await setup(home) + ctx.skills.register({ + name: 'opaque-skill', + description: 'Opaque skill', + source: 'runtime', + provider: 'runtime', + resourceBase: { kind: 'opaque', description: 'runtime memory' }, + content: 'Opaque instructions.', + }) + ctx.skills.register({ + name: 'url-skill', + description: 'URL skill', + source: 'runtime', + provider: 'runtime', + resourceBase: { kind: 'url', url: 'https://skills.example.test/url-skill' }, + content: 'URL instructions.', + }) + ctx.skills.register({ + name: 'provider-skill', + description: 'Provider skill', + source: 'runtime', + provider: 'runtime', + content: 'Provider instructions.', + }) + + const opaque = await ctx.tools.execute({ callId: CallId('c2'), name: 'skill', arguments: { name: 'opaque-skill' } }) + const url = await ctx.tools.execute({ callId: CallId('c3'), name: 'skill', arguments: { name: 'url-skill' } }) + const provider = await ctx.tools.execute({ callId: CallId('c4'), name: 'skill', arguments: { name: 'provider-skill' } }) + + if (opaque.content[0]?.type !== 'text' || url.content[0]?.type !== 'text' || provider.content[0]?.type !== 'text') { + throw new Error('expected text tool results') + } + expect(opaque.content[0].text).toContain('\nResources for this skill: runtime memory\nLoad referenced resources only as needed.\n') + expect(url.content[0].text).toContain('\nBase URL for this skill: https://skills.example.test/url-skill\nResolve relative URLs mentioned by this skill against the base URL before using them. Load referenced resources only as needed.\n') + expect(provider.content[0].text).toContain('\nResources for this skill are managed by provider "runtime".\nLoad referenced resources only as needed.\n') + }) + + it('fails loud on an unknown resource base kind', async () => { + const home = await tempDir('tool-resource-assert-never') + const ctx = await setup(home) + ctx.skills.register({ + name: 'rogue-resource-skill', + description: 'Rogue resource skill', + source: 'runtime', + provider: 'runtime', + resourceBase: { kind: 'future' } as never, + content: 'Rogue instructions.', + }) + + const result = await ctx.tools.execute({ callId: CallId('c5'), name: 'skill', arguments: { name: 'rogue-resource-skill' } }) + + expect(result.isError).toBe(true) + const block = result.content[0] + if (block?.type !== 'text') throw new Error('expected text tool result') + expect(block.text).toContain('unreachable variant') + }) + + it('returns isError for unknown, invalid, and model-disabled skills', async () => { + const home = await tempDir('tool-errors') + await writeSkill(join(home, '.dsh/skills'), 'hidden-skill', 'Hidden skill', 'Hidden instructions.') + await writeFile(join(home, '.dsh/skills/hidden-skill/SKILL.md'), '---\nname: hidden-skill\ndescription: Hidden skill\ndisableModelInvocation: true\n---\n\nHidden instructions.\n') + const ctx = await setup(home) + + const unknown = await ctx.tools.execute({ callId: CallId('c1'), name: 'skill', arguments: { name: 'missing' } }) + const invalid = await ctx.tools.execute({ callId: CallId('c2'), name: 'skill', arguments: { name: 'Bad_Name' } }) + const disabled = await ctx.tools.execute({ callId: CallId('c3'), name: 'skill', arguments: { name: 'hidden-skill' } }) + + expect(unknown.isError).toBe(true) + expect(invalid.isError).toBe(true) + expect(disabled.isError).toBe(true) + const unknownBlock = unknown.content[0] + if (unknownBlock?.type !== 'text') throw new Error('expected text tool result') + expect(unknownBlock.text).toContain('skill "missing" is unknown or no longer available') + }) +}) diff --git a/packages/skill/tool-skill/tsconfig.json b/packages/skill/tool-skill/tsconfig.json new file mode 100644 index 0000000000..52ebb8bf9d --- /dev/null +++ b/packages/skill/tool-skill/tsconfig.json @@ -0,0 +1,18 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "lib/types" + }, + "include": ["src"], + "references": [ + { "path": "../../../vendor/cosmokit" }, + { "path": "../../../vendor/cordis" }, + { "path": "../../../vendor/schemastery" }, + { "path": "../../core/scope" }, + { "path": "../../llm/llm" }, + { "path": "../../core/agent" }, + { "path": "../skill" }, + { "path": "../../core/tools" } + ] +} diff --git a/packages/subagent/README.md b/packages/subagent/README.md index 87930167e9..62de4ffb08 100644 --- a/packages/subagent/README.md +++ b/packages/subagent/README.md @@ -5,13 +5,13 @@ The subagent seam: an agent delegating work to a child agent. Like the [bash](.. | Package | Role | ctx key | |---|---|---| | `subagent/` | Abstract subagent seam: named-provider registry + vocabulary | `ctx.subagents` | -| `subagent-inprocess/` | Shared in-process run driver (pure lib; registers nothing) | — | +| `subagent-inprocess/` | Shared in-process run driver (no provider; one cleanup effect per run) | — | | `subagent-spawn/` | In-process backend: a fresh child agent | (registers on `ctx.subagents`) | | `subagent-fork/` | In-process backend: a child seeded with the parent's completed-turn prefix | (registers on `ctx.subagents`) | | `subagent-subprocess/` | Shared out-of-process machinery: env scrub, dispose ladder, isolated config dirs (pure lib; registers nothing) | — | | `subagent-acp/` | Out-of-process backend: a child agent in a spawned subprocess, driven over ACP | (registers on `ctx.subagents`) | | `tool-subagent/` | Model-facing `subagent` delegation tool over `ctx.subagents` | (registers on `ctx.tools`) | -The interface lives at `subagent/subagent/`. The in-process `subagent-spawn` / `subagent-fork` backends share the `subagent-inprocess` driver (a pure library — both depend on it, neither on the other), the out-of-process `subagent-acp` backend builds on the `subagent-subprocess` library (the credential env scrub, the dispose ladder, isolated config dirs) and ships alongside them here; the test-only `dsh-subagent-mock` (in [support](../support/README.md)) is separate. All **product** packages except the mock. +The interface lives at `subagent/subagent/`. The in-process `subagent-spawn` / `subagent-fork` backends share the `subagent-inprocess` driver (a library with no provider of its own — both depend on it, neither on the other), the out-of-process `subagent-acp` backend builds on the `subagent-subprocess` library (the credential env scrub, the dispose ladder, isolated config dirs) and ships alongside them here; the test-only `dsh-subagent-mock` (in [support](../support/README.md)) is separate. All **product** packages except the mock. The proposal and design rationale: [docs/rfc/implemented/feature/2026-06-21-subagent-capability-seam.md](../../docs/rfc/implemented/feature/2026-06-21-subagent-capability-seam.md). diff --git a/packages/subagent/subagent-acp/README.md b/packages/subagent/subagent-acp/README.md index 7fb087dcdc..c949685fcc 100644 --- a/packages/subagent/subagent-acp/README.md +++ b/packages/subagent/subagent-acp/README.md @@ -1,32 +1,31 @@ # @deepseek-ai/dsh-subagent-acp -The out-of-process **ACP subagent backend**: runs each child agent in a spawned subprocess, driven over the [Agent Client Protocol](https://github.com/zed-industries/agent-client-protocol) (ACP) as the *client*. Registers a `SubagentProvider` on `ctx.subagents` (the [subagent seam](../subagent)), alongside the in-process [`-spawn`](../subagent-spawn)/[`-fork`](../subagent-fork) backends — multiple backends coexist by name. +The ACP provider runs each subagent in a fresh subprocess and drives it as an Agent Client Protocol client. It is the out-of-process alternative to spawn and fork: the child has its own runtime, session, model configuration, and tools. -It is the direction-inverted twin of the server-side bridge in [`@deepseek-ai/dsh-acp`](../../ui/acp): that package is the ACP *agent* (it answers `initialize`/`newSession`/`prompt`); this one is the ACP *client* (it *calls* them and implements the `sessionUpdate`/`requestPermission` callbacks). Point the configured command at the `acp-agent` example to "talk to our own process". +## Start and ownership -## What it does +`start(request)` performs `spawn` → ACP `initialize` → `newSession` before it fulfills. Fulfillment therefore means a remote session is ready and ownership has transferred to the caller. A spawn, initialization, new-session, or pre-publication cancellation failure rejects only after the subprocess has been reaped. -`start(request)` spawns the configured command, wraps its stdio in an ACP `ClientSideConnection`, and drives one session: `initialize` → `newSession` → `prompt`. The child's streamed `agent_message_chunk` text becomes the `SubagentResult.output`; the prompt's terminal `StopReason` maps to the stop reason. `dispose()` kills the subprocess and awaits its exit. +After publication, the provider sends the prompt and collects streamed `agent_message_chunk` text into `SubagentResult.output`. A prompt/transport failure resolves with `stopReason: 'error'`, or `aborted` when the required request signal or disposal requested cancellation. -**Fresh process per run.** Each `start` spawns a new child, runs exactly one ACP session, and disposes it. Persistent-process pooling is a future optimization (see the RFC). +`dispose()` is idempotent. It removes the signal listener, requests ACP cancellation when possible, closes stdin, waits `disposeEofGraceMs`, escalates to SIGTERM, waits `disposeGraceMs`, and finally uses SIGKILL if necessary. Every run uses a fresh process; process pooling is not implemented. -Unlike the in-process backends, the child does NOT share this cordis context — it is a separate process with its own session, model client, and tools. So this backend: -- injects only `subagents` (no `ctx.agents`); -- advertises NO start-time capabilities (an out-of-process child can't enforce the parent's depth/tool-filter); -- ignores `request.parent`. +## Capabilities and context -## Config +ACP advertises no start-time capabilities because this process cannot enforce the remote child's depth, tool filter, persona, or structured-output runtime. It also reports `inheritsParentContext: false`: the remote session starts fresh and ignores `request.parent` beyond the seam's required attribution field. -| Key | Type | Default | Notes | -|---|---|---|---| -| `providerName` | string | `acp` | Registry name on `ctx.subagents`. | -| `command` | string | — (required) | The executable to spawn for each run (the child ACP agent). | -| `args` | string[] | `[]` | Arguments passed to `command`. | -| `cwd` | string | parent cwd | Working directory for the child process and its ACP session. | -| `permission` | `'allow' \| 'reject'` | `reject` | How to auto-answer the child's `session/request_permission` prompts. `reject` declines every prompt (answer `cancelled`); `allow` approves via the first allow-shaped option. The first cut surfaces no prompt to a human. | -| `env` | Record | `{}` | Extra env vars for the child (e.g. its own `DEEPSEEK_API_KEY`). Forwarded on top of a credential-scrubbed copy of the parent env, so an explicit key reaches the child while ambient secrets do not leak implicitly. | -| `disposeEofGraceMs` | number | `6000` | Dispose ladder tier 1: how long the child gets to quiesce on its own after stdin EOF (flush persistence, tear down its nested subprocesses) before SIGTERM. | -| `disposeGraceMs` | number | `3000` | Dispose ladder tier 2: grace between SIGTERM and the SIGKILL escalation. | +## Configuration + +| Key | Default | Meaning | +|---|---|---| +| `providerName` | `acp` | Registry name on `ctx.subagents`. | +| `command` | required | Executable spawned for each run. | +| `args` | `[]` | Command arguments. | +| `cwd` | process cwd | Child process and ACP session working directory. | +| `permission` | `reject` | Auto-answer permission requests by rejecting or choosing the first allow-shaped option. | +| `env` | `{}` | Explicit child environment layered over a credential-scrubbed parent environment. | +| `disposeEofGraceMs` | `6000` | Grace after stdin EOF before SIGTERM. | +| `disposeGraceMs` | `3000` | Grace after SIGTERM before SIGKILL. | ```yaml - id: subagent-acp @@ -40,32 +39,20 @@ Unlike the in-process backends, the child does NOT share this cordis context — DEEPSEEK_API_KEY: !!js process.env.DEEPSEEK_API_KEY ``` -## StopReason mapping +## Stop-reason mapping -ACP `StopReason` → harness `SubagentStopReason`: - -| ACP | harness | +| ACP | Harness | |---|---| | `end_turn` | `completed` | | `max_tokens` | `max-tokens` | | `refusal` | `refusal` | | `cancelled` | `aborted` | -| `max_turn_requests` | `error` (no clean equivalent; the task did not finish) | -| _(unknown)_ | `error` | +| `max_turn_requests` or unknown | `error` | -A spawn/transport/RPC failure resolves `error` (or `aborted` if a cancel was requested) — `result` never rejects on a child-level failure, per the seam contract. +## Process boundary -## Environment scrub +The child environment is built by [`buildChildEnv`](../subagent-subprocess/README.md): credential-shaped ambient variables are removed, then explicit `config.env` values are applied. The ACP wire is the real serialization boundary; same-process subagent values are not defensively cloned. -The child env is built by [`buildChildEnv` from `@deepseek-ai/dsh-subagent-subprocess`](../subagent-subprocess/README.md) — the ambient env minus credential-shaped vars, with `config.env` layered on top after the scrub; the pattern and full semantics live there. For this backend that means the parent harness's own secrets never leak into the spawned agent implicitly, while the child's OWN `DEEPSEEK_API_KEY` is supplied deliberately via `config.env` and survives. +The package has no default export. Cordis loader unwrapping would otherwise hide the named `inject` metadata; see [postmortem 0001](../../../docs/postmortem/0001-acp-default-export-drops-inject.md). -## Testing - -- **Keyless** (`subagent-acp.spec.ts`): spawns a scripted mock ACP server subprocess (`tests/mock-acp-server.ts`) and drives it through the real backend over real ACP stdio — connection setup, client callbacks, the prompt round-trip, stop-reason mapping, cancellation (including the early-cancel race and a torn-pipe-after-cancel), permission auto-answer, and quiescent disposal. No model, no key. -- **With-key e2e** (`subagent-acp.e2e.ts`): the harness drives ITSELF — the backend spawns the real `acp-agent` example process and a real model in that child answers a prompt and does real file work (verified on disk). Self-skips without `DEEPSEEK_API_KEY`. - -`TODO(acp-subagent-replay)`: snapshot-tier coverage of an ACP child is a separate replay shape (each child is its own PROCESS with its own single-agent replay, distinct from the in-process per-session keying), deferred — see the RFC. - -## 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](../../../docs/postmortem/0001-acp-default-export-drops-inject.md)). +Keyless tests drive a scripted ACP subprocess over real stdio. The with-key e2e drives the repository's real ACP agent and self-skips without `DEEPSEEK_API_KEY`. diff --git a/packages/subagent/subagent-acp/src/index.ts b/packages/subagent/subagent-acp/src/index.ts index b2ddd9ab2d..dd0aeb5aea 100644 --- a/packages/subagent/subagent-acp/src/index.ts +++ b/packages/subagent/subagent-acp/src/index.ts @@ -89,7 +89,7 @@ type ResolvedConfig = Required> & Pick * a request needing any of them before `start` runs). */ class AcpProvider implements SubagentProvider { - readonly capabilities: SubagentCapabilities = { outputSchema: false, depthLimit: false, toolFilter: false } + readonly capabilities: SubagentCapabilities = { outputSchema: false, depthLimit: false, toolFilter: false, persona: false } // Context contract: an out-of-process ACP child starts fresh — no parent conversation crosses the process boundary. readonly inheritsParentContext = false diff --git a/packages/subagent/subagent-acp/src/run.ts b/packages/subagent/subagent-acp/src/run.ts index a9fefba27c..5222906a2d 100644 --- a/packages/subagent/subagent-acp/src/run.ts +++ b/packages/subagent/subagent-acp/src/run.ts @@ -89,6 +89,7 @@ export interface AcpRunSpec { * (the seam contract forbids `result` rejecting). The driver calls this with * the original error and the chosen stop reason so the fault is preserved * rather than silently lost; the provider wires it to `ctx.logger.warn`. + * A throw from the sink itself is contained — it cannot reject `result`. * Optional — omitted in a unit test that asserts the stop reason directly. */ onError?: (error: Error, stopReason: SubagentStopReason) => void @@ -179,29 +180,19 @@ function toError(value: unknown): Error { * and drives one session: `initialize` → `newSession` → `prompt`. The accumulated * `agent_message_chunk` text is the result output; the prompt's terminal * `StopReason` maps to the stop reason. `result` never REJECTS on a child-level - * failure (a spawn/transport/RPC error resolves with `stopReason: 'error'`), per - * the seam contract. `cancel()` sends `session/cancel`; `dispose()` kills the - * subprocess and awaits its exit (quiescent teardown). - * @param request - the start request; the driver consumes `prompt` and `signal` - * (an already-aborted signal yields an inert `aborted` run with no spawn). + * failure after publication resolves with `stopReason: 'error'`. A spawn, + * initialize, new-session, or pre-publication cancellation failure instead + * rejects only after the process has been reaped. `dispose()` requests ACP + * cancellation, then kills and reaps the subprocess. + * @param request - the start request; its signal is the cancellation channel. * @param spec - the resolved spawn spec: command/args/cwd, env, permission * policy, dispose graces, and the optional error sink. - * @returns the live run handle for the child subprocess. + * @returns the ready run handle for the child subprocess. */ -export function startAcpRun(request: SubagentStartRequest, spec: AcpRunSpec): SubagentRun { +export async function startAcpRun(request: SubagentStartRequest, spec: AcpRunSpec): Promise { const id = AgentId(randomUUID()) - // A request already aborted before it starts never spawns the child at all — - // return an inert run that settled `aborted`, rather than launching the - // configured binary just to tear it down. `dispose`/`cancel` are no-ops. - if (request.signal?.aborted) { - return { - id, - result: Promise.resolve({ output: [], stopReason: 'aborted' }), - cancel(_reason?: string): void { /* nothing was started */ }, - dispose(): Promise { return Promise.resolve() }, - } - } + if (request.signal.aborted) throw new Error('subagent request was aborted before the ACP child started') // Spawn the child ACP agent. stdin = ACP request channel, stdout = ACP // response channel, stderr = INHERIT so the child's diagnostics surface on the @@ -218,9 +209,18 @@ export function startAcpRun(request: SubagentStartRequest, spec: AcpRunSpec): Su // `error` like any child failure. const spawnFailed = spawnFailure(child) + // One memoized quiescence transaction is shared by startup rollback and the + // published run's disposer. Once start fulfills, only the holder can invoke + // it; before fulfillment the provider invokes it on every failure path. + let processDisposal: Promise | undefined + const disposeProcess = (): Promise => (processDisposal ??= disposeChildProcess(child, { + disposeEofGraceMs: spec.disposeEofGraceMs, + disposeGraceMs: spec.disposeGraceMs, + })) + // Accumulate the child's streamed assistant text — the SubagentResult output. const output: string[] = [] - // `cancelled` records that a cancel was requested (signal or cancel()), so a + // `cancelled` records that the required signal or disposal requested cancel, so a // run torn down before the prompt resolves settles `aborted` rather than the // generic error mapping. Held on a mutable object so the async closures that // set it (the abort listener) and the IIFE that reads it don't fight TS's @@ -264,7 +264,7 @@ export function startAcpRun(request: SubagentStartRequest, spec: AcpRunSpec): Su // Resolves when a cancel is requested, so `result` can settle `aborted` even // if the child never cooperates with `session/cancel` (it ignores the notify, // or the prompt wedges). The result path races this against the ACP drive: the - // FIRST to settle wins, so `cancel()` always honors the contract (`result` + // FIRST to settle wins, so signal/dispose cancellation always honors the contract (`result` // settles `aborted`) without waiting on a non-cooperative child. `dispose` // still kills the process and reaps it; this only unblocks `result`. The // executor runs synchronously, so `signalCancelSettled` is assigned before the @@ -272,6 +272,7 @@ export function startAcpRun(request: SubagentStartRequest, spec: AcpRunSpec): Su let signalCancelSettled!: () => void const cancelSettled = new Promise((resolve) => { signalCancelSettled = resolve }) const requestCancel = (): void => { + if (flags.cancelled) return flags.cancelled = true signalCancelSettled() // Best-effort: tell the child to cancel the in-flight turn. Swallows a @@ -286,26 +287,21 @@ export function startAcpRun(request: SubagentStartRequest, spec: AcpRunSpec): Su if (sessionId !== undefined) void conn.cancel({ sessionId }).catch(() => { /* child gone / no session */ }) } const onAbort = (): void => { requestCancel() } - request.signal?.addEventListener('abort', onAbort, { once: true }) + request.signal.addEventListener('abort', onAbort, { once: true }) - const result: Promise = (async (): Promise => { - // The accumulated child text as harness ContentBlocks (empty array when the - // child streamed nothing). Read at every return so a partial answer survives - // a later cancel/error. - const collectOutput = (): ContentBlock[] => { - const text = output.join('') - return text.length > 0 ? [{ type: 'text', text }] : [] - } - try { - // Race three outcomes, first to settle wins: - // - driveAcp: the normal initialize → newSession → prompt path; - // - spawnFailed: a bad command never speaks ACP, so `initialize` would - // hang forever — the spawn `error` event is the only signal, and a - // rejected race settles the run `error` via the catch; - // - cancelSettled: a cancel was requested — settle `aborted` immediately - // rather than waiting on a child that may ignore `session/cancel` or - // wedge the prompt (the `cancel()` contract: `result` settles `aborted`). - const driveAcp = async (): Promise => { + // The accumulated child text as harness ContentBlocks (empty array when the + // child streamed nothing). Read at every return so a partial answer survives + // a later cancel/error. + const collectOutput = (): ContentBlock[] => { + const text = output.join('') + return text.length > 0 ? [{ type: 'text', text }] : [] + } + + // Establish the remote session before publishing a handle. Any failure owns + // the still-private process and therefore reaps it before rejecting. + try { + await Promise.race([ + (async (): Promise => { await conn.initialize({ protocolVersion: PROTOCOL_VERSION, // Advertise NO optional client capabilities (no fs, no terminal): the @@ -314,41 +310,68 @@ export function startAcpRun(request: SubagentStartRequest, spec: AcpRunSpec): Su }) const session = await conn.newSession({ cwd: spec.cwd, mcpServers: [] }) sessionId = session.sessionId - // A cancel that raced ahead of `newSession` set `cancelled` but could not - // send `session/cancel` (no session id yet). Honor it here: settle - // `aborted` without ever issuing the prompt, rather than running the child - // to completion and ignoring the cancel. - if (flags.cancelled) return { output: collectOutput(), stopReason: 'aborted' } - const promptResult = await conn.prompt({ sessionId, prompt: toAcpPrompt(request.prompt) }) + if (flags.cancelled) throw new Error('subagent cancelled before the ACP session started') + })(), + spawnFailed.then((err): never => { throw err }), + cancelSettled.then((): never => { throw new Error('subagent cancelled before the ACP session started') }), + ]) + } catch (error: unknown) { + request.signal.removeEventListener('abort', onAbort) + await disposeProcess() + if (flags.cancelled) throw new Error('subagent request was aborted before the ACP child started') + throw toError(error) + } + + const result: Promise = (async (): Promise => { + try { + // Race two post-publication outcomes, first to settle wins: + // - prompt: the normal remote turn; + // - cancelSettled: a cancel was requested — settle `aborted` immediately + // rather than waiting on a child that may ignore `session/cancel` or + // wedge the prompt (`result` settles `aborted`). After `newSession` + // succeeds, transport/process failure rejects the in-flight prompt RPC. + const prompt = async (): Promise => { + // The startup phase cannot fulfill without assigning the session id. + const promptResult = await conn.prompt({ sessionId: sessionId as string, prompt: toAcpPrompt(request.prompt) }) return { output: collectOutput(), stopReason: acpStopReason(promptResult.stopReason) } } return await Promise.race([ - driveAcp(), - spawnFailed.then((err): SubagentResult => { throw err }), + prompt(), cancelSettled.then((): SubagentResult => ({ output: collectOutput(), stopReason: 'aborted' })), ]) } catch (error: unknown) { + // A deterministic cancellation resolves `cancelSettled` before its + // best-effort ACP cancel can reject the prompt. This fallback is only for + // a process/pipe rejection already queued when the abort event fires; its + // first-outcome ordering cannot be forced without a timing-dependent test. + /* v8 ignore next */ + if (flags.cancelled) return { output: collectOutput(), stopReason: 'aborted' } // The seam contract: result resolves (never rejects) on a child-level - // failure. Cancellation is handled by the `cancelSettled` race arm above - // (it settles `aborted` the instant cancel is requested, beating any - // rejection), so a rejection that reaches HERE is always a genuine - // child-level error — the awaited ACP RPCs or the spawn-failure race - // (initialize/newSession/prompt transport/RPC errors, or ENOENT), not a - // local bug. Flatten to `error` and surface the original via onError so a - // real fault is preserved rather than silently lost. - spec.onError?.(toError(error), 'error') + // failure. Startup failures were already rejected before publication; + // every rejection here is a prompt transport/RPC failure. + // Flatten to `error` and surface the original via onError so a real fault + // is preserved rather than silently lost. + try { + spec.onError?.(toError(error), 'error') + } catch { + // Swallows only the caller-supplied sink's OWN throw: an unguarded + // sink exception would reject `result` and break the contract above. + // The child-level failure being reported still settles as `error`. + } return { output: collectOutput(), stopReason: 'error' } + } finally { + request.signal.removeEventListener('abort', onAbort) } })() + let disposal: Promise | undefined return { id, result, - cancel(_reason?: string): void { + dispose(): Promise { + if (disposal !== undefined) return disposal + request.signal.removeEventListener('abort', onAbort) requestCancel() - }, - async dispose(): Promise { - request.signal?.removeEventListener('abort', onAbort) // Quiescent teardown via the shared ladder (stdin EOF → SIGTERM → // SIGKILL, awaiting the actual exit). For THIS child the EOF tier is the // one that matters: our acp-agent has NO SIGTERM handler in a normal @@ -358,10 +381,8 @@ export function startAcpRun(request: SubagentStartRequest, spec: AcpRunSpec): Su // turn/end BEFORE that post-turn flush lands, so the child still has // durable work owed when dispose runs (hence the wide EOF grace; see // DEFAULT_DISPOSE_EOF_GRACE_MS). - await disposeChildProcess(child, { - disposeEofGraceMs: spec.disposeEofGraceMs, - disposeGraceMs: spec.disposeGraceMs, - }) + disposal = disposeProcess() + return disposal }, } } diff --git a/packages/subagent/subagent-acp/tests/mock-acp-server.ts b/packages/subagent/subagent-acp/tests/mock-acp-server.ts index 9cfeac1f44..fb200f3505 100644 --- a/packages/subagent/subagent-acp/tests/mock-acp-server.ts +++ b/packages/subagent/subagent-acp/tests/mock-acp-server.ts @@ -68,6 +68,7 @@ const WANT_PERMISSION = process.env.MOCK_PERMISSION === '1' const NO_ALLOW = process.env.MOCK_NO_ALLOW === '1' const THOUGHT = process.env.MOCK_THOUGHT === '1' const CRASH_ON_CANCEL = process.env.MOCK_CRASH_ON_CANCEL === '1' +const CRASH_ON_PROMPT = process.env.MOCK_CRASH_ON_PROMPT === '1' const IGNORE_CANCEL = process.env.MOCK_IGNORE_CANCEL === '1' const READY_FILE = process.env.MOCK_READY_FILE const FLUSH_ON_EOF = process.env.MOCK_FLUSH_ON_EOF @@ -105,6 +106,7 @@ function makeAgent(conn: AgentSideConnection): Agent { return Promise.resolve() }, async prompt(params: PromptRequest): Promise { + if (CRASH_ON_PROMPT) process.exit(1) if (WANT_PERMISSION) { // Ask the client to approve before answering; honor its decision. Under // MOCK_NO_ALLOW the only options are reject-shaped, so an `allow`-policy @@ -225,4 +227,3 @@ if (process.env.MOCK_IGNORE_EOF === '1') { setInterval(() => { /* stay alive past EOF until SIGTERM */ }, 1000) if (READY_FILE !== undefined) writeFileSync(READY_FILE, 'ignore-eof-armed') } - diff --git a/packages/subagent/subagent-acp/tests/subagent-acp.e2e.ts b/packages/subagent/subagent-acp/tests/subagent-acp.e2e.ts index 826ef198dd..7814dfaca3 100644 --- a/packages/subagent/subagent-acp/tests/subagent-acp.e2e.ts +++ b/packages/subagent/subagent-acp/tests/subagent-acp.e2e.ts @@ -60,9 +60,10 @@ describe.skipIf(!process.env.DEEPSEEK_API_KEY)('ACP backend with-key e2e (drive }, }) - const run = ctx.subagents.start('acp', { + const run = await ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'Reply with exactly the word PONG and nothing else. Do not use any tools.' }], parent: fakeParent, + signal: new AbortController().signal, }) const result = await run.result await run.dispose() @@ -93,11 +94,12 @@ describe.skipIf(!process.env.DEEPSEEK_API_KEY)('ACP backend with-key e2e (drive }, }) - const run = ctx.subagents.start('acp', { + const run = await ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'Use the bash tool to write the text ACP_CHILD_WAS_HERE into a file named proof.txt ' + 'in the current directory. Then reply DONE.' }], parent: fakeParent, + signal: new AbortController().signal, }) const result = await run.result await run.dispose() diff --git a/packages/subagent/subagent-acp/tests/subagent-acp.spec.ts b/packages/subagent/subagent-acp/tests/subagent-acp.spec.ts index 92c025077a..eed3cfe1ed 100644 --- a/packages/subagent/subagent-acp/tests/subagent-acp.spec.ts +++ b/packages/subagent/subagent-acp/tests/subagent-acp.spec.ts @@ -27,6 +27,10 @@ const repoTsconfig = fileURLToPath(new URL('../../../../tsconfig.json', import.m /** A throwaway parent Agent — the ACP backend ignores it, but the seam requires one. */ const fakeParent = { id: 'parent', session: { header: {} } } as unknown as Agent +function request(text = 'p', signal = new AbortController().signal) { + return { prompt: [{ type: 'text' as const, text }], parent: fakeParent, signal } +} + interface SetupEnv { /** Mock-server scripting env: MOCK_TEXT / MOCK_STOP / MOCK_HANG / MOCK_PERMISSION. */ [key: string]: string @@ -119,16 +123,18 @@ describe('buildChildEnv', () => { describe('dsh-subagent-acp', () => { it('drives a child process to completion and returns its streamed output', async () => { const ctx = await setup({ MOCK_TEXT: 'hello from acp child', MOCK_STOP: 'end_turn' }) - const run = ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'do X' }], parent: fakeParent }) + const run = await ctx.subagents.start('acp', request('do X')) const result = await run.result expect(result.stopReason).toBe('completed') expect(text(result.output)).toBe('hello from acp child') - await run.dispose() + const disposal = run.dispose() + expect(run.dispose()).toBe(disposal) + await disposal }) it('maps a max_tokens stop reason', async () => { const ctx = await setup({ MOCK_TEXT: 'cut off', MOCK_STOP: 'max_tokens' }) - const run = ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }) + const run = await ctx.subagents.start('acp', request()) const result = await run.result expect(result.stopReason).toBe('max-tokens') await run.dispose() @@ -136,22 +142,23 @@ describe('dsh-subagent-acp', () => { it('maps a refusal stop reason', async () => { const ctx = await setup({ MOCK_TEXT: '', MOCK_STOP: 'refusal' }) - const run = ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }) + const run = await ctx.subagents.start('acp', request()) const result = await run.result expect(result.stopReason).toBe('refusal') await run.dispose() }) - it('cancelling a running child settles aborted (session/cancel via run.cancel)', async () => { + it('aborting the required signal cancels a running child', async () => { const tmp = mkdtempSync(join(tmpdir(), 'acp-cancel-')) const readyFile = join(tmp, 'ready') try { const ctx = await setup({ MOCK_TEXT: 'partial', MOCK_HANG: '1', MOCK_READY_FILE: readyFile }) - const run = ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }) + const controller = new AbortController() + const run = await ctx.subagents.start('acp', request('p', controller.signal)) // Wait until the child's prompt is in flight (condition, not a sleep), // then cancel — so we exercise the mid-run session/cancel path. await waitForFile(readyFile) - run.cancel('test') + controller.abort('test') const result = await run.result expect(result.stopReason).toBe('aborted') await run.dispose() @@ -160,7 +167,7 @@ describe('dsh-subagent-acp', () => { } }) - it('settles aborted WITHOUT spawning the child when the signal is already aborted', async () => { + it('rejects WITHOUT spawning the child when the signal is already aborted', async () => { // A pre-aborted request must not even launch the configured binary. Point // the command at one that would create a sentinel file if it ever ran, and // assert the sentinel never appears. @@ -169,17 +176,11 @@ describe('dsh-subagent-acp', () => { try { const controller = new AbortController() controller.abort() - const run = startAcpRun( - { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent, signal: controller.signal }, + await expect(startAcpRun( + request('p', controller.signal), // `touch ` — runs only if the process is actually spawned. { command: 'touch', args: [sentinel], cwd: tmp, permission: 'reject', env: {}, disposeEofGraceMs: DEFAULT_DISPOSE_EOF_GRACE_MS, disposeGraceMs: DEFAULT_DISPOSE_GRACE_MS }, - ) - const result = await run.result - expect(result.stopReason).toBe('aborted') - expect(result.output).toEqual([]) - // cancel/dispose on the inert run are safe no-ops. - run.cancel('noop') - await run.dispose() + )).rejects.toThrow('aborted before the ACP child started') // The binary was never launched — no sentinel. expect(existsSync(sentinel)).toBe(false) } finally { @@ -206,7 +207,7 @@ describe('dsh-subagent-acp', () => { disposeEofGraceMs: 150, disposeGraceMs: 150, } - const run = startAcpRun({ prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }, spec) + const run = await startAcpRun(request(), spec) // Wait until the child has BOOTED AND ARMED THE TRAP (a condition, not a // sleep) — otherwise SIGTERM races the trap install and the default handler // terminates the child, never exercising the escalation. @@ -253,7 +254,7 @@ describe('dsh-subagent-acp', () => { disposeEofGraceMs: 2000, disposeGraceMs: 50, } - const run = startAcpRun({ prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }, spec) + const run = await startAcpRun(request(), spec) // Wait until the child is fully booted with its prompt in flight (its ACP // stdin reader is attached), so dispose's stdin EOF reaches a live child. await waitForFile(ready) @@ -290,7 +291,7 @@ describe('dsh-subagent-acp', () => { disposeEofGraceMs: 150, disposeGraceMs: 2000, } - const run = startAcpRun({ prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }, spec) + const run = await startAcpRun(request(), spec) await waitForFile(ready) // Bound it so a hang fails loud rather than stalling the suite. await expect(Promise.race([ @@ -305,7 +306,7 @@ describe('dsh-subagent-acp', () => { } }) - it('honors a cancel that races AHEAD of newSession (no session id yet) without running the prompt', async () => { + it('rejects after cleanup when the signal aborts during newSession', async () => { // Gate the child at newSession: it signals `ready` and blocks until `go`. // We cancel WHILE newSession is pending (sessionId still undefined, so the // backend cannot send session/cancel) — the `cancelled` flag alone must @@ -315,14 +316,12 @@ describe('dsh-subagent-acp', () => { const go = join(tmp, 'go') try { const ctx = await setup({ MOCK_NEWSESSION_READY: ready, MOCK_NEWSESSION_GO: go, MOCK_TEXT: 'should not run' }) - const run = ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }) + const controller = new AbortController() + const starting = ctx.subagents.start('acp', request('p', controller.signal)) await waitForFile(ready) // newSession is now in flight, sessionId undefined - run.cancel('early') // sets cancelled; cannot send session/cancel yet + controller.abort('early') writeFileSync(go, 'go') // let newSession resolve - const result = await run.result - expect(result.stopReason).toBe('aborted') - expect(result.output).toEqual([]) - await run.dispose() + await expect(starting).rejects.toThrow('aborted before the ACP child started') } finally { rmSync(tmp, { recursive: true, force: true }) } @@ -334,7 +333,7 @@ describe('dsh-subagent-acp', () => { try { const controller = new AbortController() const ctx = await setup({ MOCK_TEXT: 'partial', MOCK_HANG: '1', MOCK_READY_FILE: readyFile }) - const run = ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent, signal: controller.signal }) + const run = await ctx.subagents.start('acp', request('p', controller.signal)) await waitForFile(readyFile) controller.abort() const result = await run.result @@ -347,7 +346,7 @@ describe('dsh-subagent-acp', () => { it('auto-rejects a permission prompt by default (child settles cancelled→aborted)', async () => { const ctx = await setup({ MOCK_TEXT: 'x', MOCK_PERMISSION: '1' }, 'reject') - const run = ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }) + const run = await ctx.subagents.start('acp', request()) const result = await run.result // The child asked permission, the backend rejected, the child returned cancelled. expect(result.stopReason).toBe('aborted') @@ -356,7 +355,7 @@ describe('dsh-subagent-acp', () => { it('auto-approves a permission prompt under the allow policy', async () => { const ctx = await setup({ MOCK_TEXT: 'approved answer', MOCK_PERMISSION: '1', MOCK_STOP: 'end_turn' }, 'allow') - const run = ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }) + const run = await ctx.subagents.start('acp', request()) const result = await run.result expect(result.stopReason).toBe('completed') expect(text(result.output)).toBe('approved answer') @@ -367,7 +366,7 @@ describe('dsh-subagent-acp', () => { // The child asks permission but offers ONLY reject-shaped options, so an // allow-policy client finds nothing to select and must answer cancelled. const ctx = await setup({ MOCK_PERMISSION: '1', MOCK_NO_ALLOW: '1' }, 'allow') - const run = ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }) + const run = await ctx.subagents.start('acp', request()) const result = await run.result expect(result.stopReason).toBe('aborted') await run.dispose() @@ -377,7 +376,7 @@ describe('dsh-subagent-acp', () => { // The child streams an agent_thought_chunk before its answer; the backend // must consume it but NOT include it in the result output. const ctx = await setup({ MOCK_THOUGHT: '1', MOCK_TEXT: 'final answer', MOCK_STOP: 'end_turn' }) - const run = ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }) + const run = await ctx.subagents.start('acp', request()) const result = await run.result expect(result.stopReason).toBe('completed') // Only the message text, NOT the thought. @@ -385,18 +384,11 @@ describe('dsh-subagent-acp', () => { await run.dispose() }) - it('resolves error (not reject) when the spawn command does not exist', async () => { - // Direct startAcpRun with NO onError sink — the catch must still flatten the - // spawn failure to `error` (the onError call is optional, covering the - // absent-sink branch). - const run = startAcpRun( - { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }, + it('rejects a spawn failure after provider-owned cleanup', async () => { + await expect(startAcpRun( + request(), { command: '/nonexistent/acp-agent-binary', args: [], cwd: process.cwd(), permission: 'reject', env: {}, disposeEofGraceMs: DEFAULT_DISPOSE_EOF_GRACE_MS, disposeGraceMs: DEFAULT_DISPOSE_GRACE_MS }, - ) - const result = await run.result - // The seam contract: a child-level failure resolves error, never rejects. - expect(result.stopReason).toBe('error') - await run.dispose() + )).rejects.toThrow() }) it('plugin-config dispose graces reach the run (SIGKILL escalation through the provider)', async () => { @@ -418,7 +410,7 @@ describe('dsh-subagent-acp', () => { disposeEofGraceMs: 150, disposeGraceMs: 150, }) - const run = ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }) + const run = await ctx.subagents.start('acp', request()) await waitForFile(ready) await expect(Promise.race([ run.dispose(), @@ -440,7 +432,7 @@ describe('dsh-subagent-acp', () => { } }) - it('resolves error via the provider (real load path) when the command does not exist', async () => { + it('rejects a startup failure via the provider load path', async () => { const ctx = new Context() await ctx.plugin(SubagentService) await ctx.plugin(acp, { @@ -450,26 +442,23 @@ describe('dsh-subagent-acp', () => { permission: 'reject', env: {}, }) - const run = ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }) - const result = await run.result - expect(result.stopReason).toBe('error') - await run.dispose() + await expect(ctx.subagents.start('acp', request())).rejects.toThrow() }) it('reports a flattened child failure through onError (preserved, not silently lost)', async () => { // The seam forbids `result` rejecting, so a child-level failure is flattened // to a stop reason — onError must still surface the original error so a real - // fault is logged, not swallowed. A nonexistent command triggers the spawn - // failure path; the spy records the error + the chosen stop reason. + // fault is logged, not swallowed. The child exits after its session is + // published but while prompt is in flight. const errors: { message: string; stopReason: string }[] = [] - const run = startAcpRun( - { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }, + const run = await startAcpRun( + request(), { - command: '/nonexistent/acp-agent-binary', - args: [], + command: process.execPath, + args: ['--import', tsxLoader, mockServer], cwd: process.cwd(), permission: 'reject', - env: {}, + env: { MOCK_CRASH_ON_PROMPT: '1', TSX_TSCONFIG_PATH: repoTsconfig }, disposeEofGraceMs: DEFAULT_DISPOSE_EOF_GRACE_MS, disposeGraceMs: DEFAULT_DISPOSE_GRACE_MS, onError: (error, stopReason) => { errors.push({ message: error.message, stopReason }) }, @@ -483,6 +472,41 @@ describe('dsh-subagent-acp', () => { await run.dispose() }) + it('logs a flattened child failure through the registered provider', async () => { + const ctx = await setup({ MOCK_CRASH_ON_PROMPT: '1' }) + const warnings: string[] = [] + ctx.logger.warn = ((message: unknown) => { warnings.push(String(message)) }) as typeof ctx.logger.warn + const run = await ctx.subagents.start('acp', request()) + const result = await run.result + expect(result.stopReason).toBe('error') + expect(warnings).toEqual([ + expect.stringContaining('subagent-acp "acp": child run failed (error):'), + ]) + await run.dispose() + }) + + it('resolves error (never rejects) even when the onError sink itself throws', async () => { + // onError is a caller-supplied callback boundary: its own exception must be + // contained, or it would reject `result` and break the seam's "result never + // rejects" contract that the flattening above exists to uphold. + const run = await startAcpRun( + request(), + { + command: process.execPath, + args: ['--import', tsxLoader, mockServer], + cwd: process.cwd(), + permission: 'reject', + env: { MOCK_CRASH_ON_PROMPT: '1', TSX_TSCONFIG_PATH: repoTsconfig }, + disposeEofGraceMs: DEFAULT_DISPOSE_EOF_GRACE_MS, + disposeGraceMs: DEFAULT_DISPOSE_GRACE_MS, + onError: () => { throw new Error('sink boom') }, + }, + ) + const result = await run.result + expect(result.stopReason).toBe('error') + await run.dispose() + }) + it('settles aborted when the child crashes (tears the pipe) AFTER a cancel', async () => { // The child hangs, we cancel, and instead of answering the child exits hard // — the pending prompt RPC rejects. With a cancel already requested, the @@ -492,9 +516,10 @@ describe('dsh-subagent-acp', () => { const ready = join(tmp, 'ready') try { const ctx = await setup({ MOCK_TEXT: 'partial', MOCK_HANG: '1', MOCK_CRASH_ON_CANCEL: '1', MOCK_READY_FILE: ready }) - const run = ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }) + const controller = new AbortController() + const run = await ctx.subagents.start('acp', request('p', controller.signal)) await waitForFile(ready) - run.cancel('crash it') + controller.abort('crash it') const result = await run.result expect(result.stopReason).toBe('aborted') await run.dispose() @@ -503,8 +528,8 @@ describe('dsh-subagent-acp', () => { } }) - it('settles aborted on cancel even when the child IGNORES session/cancel (non-cooperative)', async () => { - // The contract: run.cancel() → result settles `aborted`. A child that hangs + it('settles aborted on signal even when the child IGNORES session/cancel', async () => { + // The signal contract requires `result` to settle `aborted`. A child that hangs // its prompt AND ignores session/cancel must not wedge the parent — the // backend's own cancel-settle path resolves `aborted` without the child's // cooperation, and dispose() still reaps the process. @@ -512,9 +537,10 @@ describe('dsh-subagent-acp', () => { const ready = join(tmp, 'ready') try { const ctx = await setup({ MOCK_TEXT: 'partial', MOCK_HANG: '1', MOCK_IGNORE_CANCEL: '1', MOCK_READY_FILE: ready }) - const run = ctx.subagents.start('acp', { prompt: [{ type: 'text', text: 'p' }], parent: fakeParent }) + const controller = new AbortController() + const run = await ctx.subagents.start('acp', request('p', controller.signal)) await waitForFile(ready) - run.cancel('test') + controller.abort('test') // Bound it: a regression (cancel only notifies the child, which ignores it) // would hang result forever — fail loud instead of stalling the suite. const result = await Promise.race([ @@ -531,7 +557,7 @@ describe('dsh-subagent-acp', () => { it('advertises no start-time capabilities (out-of-process child)', async () => { const ctx = await setup() const provider = ctx.subagents.getProvider('acp')! - expect(provider.capabilities).toEqual({ outputSchema: false, depthLimit: false, toolFilter: false }) + expect(provider.capabilities).toEqual({ outputSchema: false, depthLimit: false, toolFilter: false, persona: false }) }) it('unregisters the provider when its fiber is disposed (HMR safety)', async () => { diff --git a/packages/subagent/subagent-fork/README.md b/packages/subagent/subagent-fork/README.md index 1abd7951fd..bf90ecdf52 100644 --- a/packages/subagent/subagent-fork/README.md +++ b/packages/subagent/subagent-fork/README.md @@ -1,23 +1,23 @@ # @deepseek-ai/dsh-subagent-fork -The in-process **fork** subagent backend: a [`SubagentProvider`](../subagent/README.md) that runs each child as a child [`Agent`](../../core/agent) **seeded with a prefix of the parent's session log** — so the child inherits the parent's conversation context instead of starting fresh. Shares the run driver (`startInProcessRun`) with [`dsh-subagent-spawn`](../subagent-spawn/README.md); the only difference is the seed. +The fork provider creates an in-process child seeded with the parent's completed conversation turns. It shares all run mechanics with spawn; the session seed is the only behavioral difference. -## The seed boundary (the crux) +## Seed boundary -At the moment a subagent tool's `execute` runs, the parent's CURRENT turn is open and unbalanced: the log holds the `assistant/message` carrying this spawn's tool-call and the dangling `tool/call` with no `tool/result` yet. Seeding that raw prefix would give the child an open turn that the session constructor and the dev-mode [invariants](../../support/invariants) replay **reject**. +The parent's current tool-calling turn is still open when a subagent starts: its log contains the assistant tool call but not the matching tool result or `turn/end`. Copying that raw log would give the child an invalid, unbalanced session. -So the fork seeds only the **balanced completed-turn prefix** — the parent's log up to and including its last `turn/end`, excluding the in-flight turn entirely (`completedTurnPrefix`). Because the live log keeps `seq === index`, the slice is contiguous-from-0 and a valid seed. A parent on its very first (not-yet-complete) turn forks an *empty* seed — i.e. effectively a fresh child. +Fork therefore uses `completedTurnPrefix(parent.session.events)`: the contiguous prefix ending at the last `turn/end`. The child sees all completed parent turns and none of the in-flight turn. If the parent has not completed a turn yet, the seed is empty and the child behaves like a fresh spawn. -The seam this rides on: `CreateAgentOptions.seed` (added on `dsh-agent`, threaded through `AgentLoop.createAgent` → `ctx.sessions.prepare({ seed })`), the same primitive `resume` uses. +The seed transfers conversation history only. The child still receives a fresh flat registration scope; it does not inherit the parent's tool restrictions or authority. -## Capabilities +## Start and capabilities -`{ outputSchema: true, depthLimit: true, toolFilter: false }` — identical to spawn (the depth/model/structured-output behavior is the shared driver's). +`start(request)` passes the completed-turn seed to [`startInProcessRun`](../subagent-inprocess/README.md) and awaits child publication. The shared driver owns cancellation, depth, customization, result reading, and disposal. + +Fork advertises `{ outputSchema: true, depthLimit: true, toolFilter: true, persona: true }`, identical to spawn. ## Config | Key | Meaning | |---|---| | `providerName` | Registry name on `ctx.subagents` (default `fork`). | - -See [`dsh-subagent-spawn`](../subagent-spawn/README.md) for the run lifecycle, model inheritance, and depth tracking — all shared. diff --git a/packages/subagent/subagent-fork/src/index.ts b/packages/subagent/subagent-fork/src/index.ts index d8c77a03ac..ebf64e7b70 100644 --- a/packages/subagent/subagent-fork/src/index.ts +++ b/packages/subagent/subagent-fork/src/index.ts @@ -32,7 +32,7 @@ export const name = 'subagent-fork' // per-run structured runtime gates its capture-tool registration on `tools` // itself, so this backend's apply timing (and the delegation tool's position // in the model-visible tool list) is unchanged by structured output. -export const inject = ['subagents', 'agents'] +export const inject = ['subagents'] /** Config: the registry name to register the provider under. */ export interface Config { @@ -64,20 +64,19 @@ export function completedTurnPrefix(parent: Agent): SessionEvent[] { /** * The fork provider. Supports `depthLimit` and `outputSchema` (via the shared - * in-process structured runtime); NOT `toolFilter` this cut (the service - * rejects a request needing it before `start` runs). + * in-process structured runtime), plus `toolFilter`/`persona` (scoped + * restrict() and a scoped shadowing persona section). */ class ForkProvider implements SubagentProvider { - readonly capabilities: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: false } + readonly capabilities: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: true, persona: true } // Context contract: a forked child IS seeded with the parent's completed-turn prefix. readonly inheritsParentContext = true - constructor(readonly name: string, private readonly ctx: Context) {} + constructor(readonly name: string) {} start(request: SubagentStartRequest) { const seed = completedTurnPrefix(request.parent) - return startInProcessRun(this.ctx, request, { - providerName: this.name, + return startInProcessRun(request, { // Only pass a seed when there's a completed turn to inherit; an empty seed // is equivalent to a fresh child, so omit it to keep the session unseeded. ...seed.length > 0 ? { seed } : {}, @@ -86,5 +85,5 @@ class ForkProvider implements SubagentProvider { } export function apply(ctx: Context, config: Config): void { - ctx.subagents.registerProvider(new ForkProvider(config.providerName, ctx)) + ctx.subagents.registerProvider(new ForkProvider(config.providerName)) } diff --git a/packages/subagent/subagent-fork/tests/multi-subagent.spec.ts b/packages/subagent/subagent-fork/tests/multi-subagent.spec.ts index 1f932fbaf9..8060a77fd4 100644 --- a/packages/subagent/subagent-fork/tests/multi-subagent.spec.ts +++ b/packages/subagent/subagent-fork/tests/multi-subagent.spec.ts @@ -7,13 +7,17 @@ import ToolRegistry from '@deepseek-ai/dsh-tools' import AgentRegistry, { AgentId } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import * as Invariants from '@deepseek-ai/dsh-invariants' -import SubagentService from '@deepseek-ai/dsh-subagent' +import SubagentService, { type SubagentStartRequest } from '@deepseek-ai/dsh-subagent' import * as Spawn from '@deepseek-ai/dsh-subagent-spawn' import { MockAdapter, textResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' import * as fork from '../src/index.ts' type Script = ConstructorParameters[0] +function start(ctx: Context, provider: string, request: Omit & { signal?: AbortSignal }) { + return ctx.subagents.start(provider, { signal: request.signal ?? new AbortController().signal, ...request }) +} + /** * The two in-process backends coexist on one context: the SAME parent agent * delegates to a `spawn` child (fresh) and a `fork` child (seeded with its log), @@ -62,13 +66,13 @@ describe('multi-subagent coexistence (spawn + fork on one context)', () => { const parentPrefixLen = parent.session.events.length // Delegate to a fresh spawn child. - const spawnRun = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'spawn task' }], parent }) + const spawnRun = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'spawn task' }], parent }) const spawnResult = await spawnRun.result expect(spawnResult.stopReason).toBe('completed') expect(text(spawnResult.output)).toBe('spawn child reply') // Delegate to a fork child (seeded with the parent's turn-1 prefix). - const forkRun = ctx.subagents.start('fork', { prompt: [{ type: 'text', text: 'fork task' }], parent }) + const forkRun = await start(ctx, 'fork', { prompt: [{ type: 'text', text: 'fork task' }], parent }) const forkResult = await forkRun.result expect(forkResult.stopReason).toBe('completed') expect(text(forkResult.output)).toBe('fork child reply') diff --git a/packages/subagent/subagent-fork/tests/subagent-fork.spec.ts b/packages/subagent/subagent-fork/tests/subagent-fork.spec.ts index 90e2d583d8..e089cebe5f 100644 --- a/packages/subagent/subagent-fork/tests/subagent-fork.spec.ts +++ b/packages/subagent/subagent-fork/tests/subagent-fork.spec.ts @@ -8,7 +8,7 @@ import ToolRegistry from '@deepseek-ai/dsh-tools' import AgentRegistry, { AgentId } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import * as Invariants from '@deepseek-ai/dsh-invariants' -import SubagentService from '@deepseek-ai/dsh-subagent' +import SubagentService, { type SubagentStartRequest } from '@deepseek-ai/dsh-subagent' import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' import type { StreamChunk } from '@deepseek-ai/dsh-llm' import * as fork from '../src/index.ts' @@ -17,16 +17,19 @@ import { completedTurnPrefix } from '../src/index.ts' type Script = ConstructorParameters[0] +function start(ctx: Context, provider: string, request: Omit & { signal?: AbortSignal }) { + return ctx.subagents.start(provider, { signal: request.signal ?? new AbortController().signal, ...request }) +} + /** A bare `stop` finish that streams no content → the turn ends `completed` * with NO `assistant/message` of its own. */ const emptyStop: StreamChunk[] = [{ type: 'finish', reason: { kind: 'stop' } }] /** * Drives the REAL fork backend with a real loop + scripted mock MODEL + the - * real dsh-invariants plugin. The invariants plugin re-replays a seeded child - * log on `session/created` (its freeze-check), so a malformed (unbalanced) fork - * seed makes these tests THROW — that is the regression guard for the - * completed-turn-prefix boundary. + * real dsh-invariants plugin. The plugin replays a seeded child log on + * `session/created`, so a malformed (unbalanced) fork seed makes these tests + * THROW — that is the regression guard for the completed-turn-prefix boundary. */ async function setup(script: Script) { const ctx = new Context() @@ -71,12 +74,29 @@ describe('completedTurnPrefix', () => { }) describe('dsh-subagent-fork', () => { + it('emits subagent/start only after the seeded child is published', async () => { + const { ctx, parent } = await setup([textResponse('child answer')]) + let childAtStart: ReturnType + ctx.on('subagent/start', (info) => { + if (info.provider === 'fork') childAtStart = ctx.agents.get(info.id) + }) + + const starting = start(ctx, 'fork', { prompt: [{ type: 'text', text: 'child q' }], parent }) + expect(childAtStart).toBeUndefined() + const run = await starting + expect(childAtStart).toBe(ctx.agents.get(run.id)) + expect(childAtStart?.id).toBe(run.id) + + await run.result + await run.dispose() + }) + it('forks an UNSEEDED (fresh) child when the parent has no completed turn', async () => { // The parent has never completed a turn → empty prefix → the provider omits // the seed → the child runs fresh. Exercises the `seed.length > 0` false arm. const { ctx, parent } = await setup([textResponse('fresh child')]) expect(completedTurnPrefix(parent)).toEqual([]) - const run = ctx.subagents.start('fork', { prompt: [{ type: 'text', text: 'child q' }], parent }) + const run = await start(ctx, 'fork', { prompt: [{ type: 'text', text: 'child q' }], parent }) const result = await run.result expect(result.stopReason).toBe('completed') expect(text(result.output)).toBe('fresh child') @@ -94,7 +114,7 @@ describe('dsh-subagent-fork', () => { await parent.whenIdle() const parentPrefixLen = parent.session.events.length - const run = ctx.subagents.start('fork', { prompt: [{ type: 'text', text: 'child question' }], parent }) + const run = await start(ctx, 'fork', { prompt: [{ type: 'text', text: 'child question' }], parent }) const result = await run.result expect(result.stopReason).toBe('completed') expect(text(result.output)).toBe('child answer') @@ -127,7 +147,7 @@ describe('dsh-subagent-fork', () => { await new Promise(r => setTimeout(r, 20)) // let the hanging turn open // Forking now must NOT throw (the open second turn is excluded from the seed). - const run = ctx.subagents.start('fork', { prompt: [{ type: 'text', text: 'child q' }], parent }) + const run = await start(ctx, 'fork', { prompt: [{ type: 'text', text: 'child q' }], parent }) const result = await run.result expect(result.stopReason).toBe('completed') expect(text(result.output)).toBe('child') @@ -149,7 +169,7 @@ describe('dsh-subagent-fork', () => { ]) parent.send([{ type: 'text', text: 'warm up' }]) await parent.whenIdle() - const run = ctx.subagents.start('fork', { + const run = await start(ctx, 'fork', { prompt: [{ type: 'text', text: 'report structured' }], parent, outputSchema: { type: 'object', properties: { answer: { type: 'number' } }, required: ['answer'] }, @@ -173,7 +193,7 @@ describe('dsh-subagent-fork', () => { parent.send([{ type: 'text', text: 'parent question' }]) await parent.whenIdle() - const run = ctx.subagents.start('fork', { prompt: [{ type: 'text', text: 'child question' }], parent }) + const run = await start(ctx, 'fork', { prompt: [{ type: 'text', text: 'child question' }], parent }) const result = await run.result // The child completed its own (empty) turn — completed, but with NO output // borrowed from the seeded parent prefix. @@ -182,9 +202,9 @@ describe('dsh-subagent-fork', () => { await run.dispose() }) - it('advertises depthLimit and outputSchema but not toolFilter', async () => { + it('advertises every start-time capability (depthLimit, outputSchema, toolFilter, persona)', async () => { const { ctx } = await setup([]) - expect(ctx.subagents.getProvider('fork')!.capabilities).toEqual({ outputSchema: true, depthLimit: true, toolFilter: false }) + expect(ctx.subagents.getProvider('fork')!.capabilities).toEqual({ outputSchema: true, depthLimit: true, toolFilter: true, persona: true }) }) it('unregisters the provider when its fiber is disposed (HMR safety)', async () => { @@ -200,12 +220,12 @@ describe('dsh-subagent-fork', () => { it('has the namespace-plugin export shape (no stray default)', () => { expect('default' in fork).toBe(false) expect(fork.name).toBe('subagent-fork') - expect(fork.inject).toEqual(['subagents', 'agents']) + expect(fork.inject).toEqual(['subagents']) const loader = Object.create(Loader.prototype) as Loader const unwrapped = loader.unwrapExports(fork) as Record expect(unwrapped).toBe(fork) expect(unwrapped.name).toBe('subagent-fork') - expect(unwrapped.inject).toEqual(['subagents', 'agents']) + expect(unwrapped.inject).toEqual(['subagents']) expect(typeof unwrapped.apply).toBe('function') }) }) diff --git a/packages/subagent/subagent-inprocess/README.md b/packages/subagent/subagent-inprocess/README.md index f6870929a8..48fd0a201d 100644 --- a/packages/subagent/subagent-inprocess/README.md +++ b/packages/subagent/subagent-inprocess/README.md @@ -1,41 +1,41 @@ # @deepseek-ai/dsh-subagent-inprocess -The shared **in-process subagent run driver**. A pure library (no provider, no registration) that the in-process backends — [spawn](../subagent-spawn/README.md) (a fresh child) and [fork](../subagent-fork/README.md) (a child seeded with a prefix of the parent's log) — both build on. The backends are thin shells that differ ONLY in the session seed they pass; everything downstream lives here, so neither backend depends on the other. +This package is the shared run driver for the two in-process providers. Spawn passes no session seed; fork passes the parent's completed-turn prefix. Everything else—depth, child creation, optional child customization, result reading, cancellation, and disposal—has one implementation here. -## What it exports +## Start contract -### `startInProcessRun(ctx, request, options): SubagentRun` +`startInProcessRun(request, options): Promise` fulfills only after the child is published in `ctx.agents`. A rejected start has already quiesced the agent factory's unpublished creation transaction, so the caller never receives a half-created handle. -Runs a child as a child [`Agent`](../../core/agent) on the same cordis context (`ctx.agents`): +The driver follows this sequence: -1. computes child depth = `depthOf(parent) + 1`; if `request.maxDepth` is set and exceeded, throws `SubagentDepthError` (the `depthLimit` capability); a `request.outputSchema` is asserted against the supported subset (`assertSupportedOutputSchema` from [dsh-tools](../../core/tools/README.md)) and then snapshotted with `structuredClone` before any child exists — assertion first so a hostile value fails as `OutputSchemaError` (never a raw clone error), the snapshot so a post-`start()` caller mutation cannot drift the enforced schema; -2. creates a child via `ctx.agents.create` with a fresh `AgentId`/`SessionId`, the parent's `cwd` + `parentSession` lineage, the optional `options.seed` (fork's completed-turn prefix; omitted for a fresh child), and `agentOptions` (the child inherits the **parent's model** by default — a child with no model can't run — overridable via `request.agentOptions.model`; the deployment persona needs no inheritance — it is a context-wide prompt section); -3. drives the one-shot: `child.send(prompt)` then `await child.whenIdle()` (ordering matters — `send` enqueues synchronously, so `whenIdle` observes the queued work and resolves on the child's `running → idle` transition, never before the turn starts); there is deliberately NO re-prompt for a structured child that finished cleanly without calling `structured_output` — the shortfall maps to an `error` result for the parent; -4. reads the result, scoped to the child's OWN events (everything at or after `seedLength`, so a seeded child that produced no message of its own never returns the seeded parent's last message): the last `assistant/message` content (deep-cloned — the log is frozen) and the last `turn/end.reason` mapped to a `SubagentStopReason`. A structured run surfaces the captured value as `result.structured`; a structured child that finished cleanly WITHOUT ever capturing settles `error` (a clean finish without the demanded result is a failure, not a success with a missing field). +1. Validate the parent depth and optional absolute `maxDepth`, then derive child depth as parent depth plus one. +2. Call `parent.ctx.agents.create` directly, passing the required request signal into the factory's creation transaction. +3. During that transaction's unpublished setup window, install the requested persona, tool restriction, and structured-output runtime. +4. Publish the child, retain the returned `AgentHandle`, and drive one task with `child.send(prompt)` followed by `child.whenIdle()`. +5. Read the child's own last assistant message and terminal turn reason, excluding any fork seed. -`dispose()` delegates to `AgentHandle.dispose()` (stop loop → await quiescence → remove session); `cancel()` cancels the child's in-flight turn. A cancel landing before any `turn/end` (the pre-turn window) still settles `aborted`, honoring the cancel contract rather than the generic no-turn `error`. +The child gets the parent's working-directory/session lineage and inherits the parent model unless `request.agentOptions` overrides it. It gets a fresh flat registration scope: parent ownership does not import parent tool restrictions or establish an authority subset. -### `InProcessRunOptions` +## Cancellation and ownership -`{ providerName: string; seed?: SessionEvent[] }` — the per-backend inputs: the provider name (for error context) and the optional child-session seed. +The required request signal covers both startup and the live run. Before publication, `AgentCreationTransaction` observes it, rolls back, and rejects. The factory detaches that creation-only listener before returning; the driver immediately checks the signal once more before installing a minimal live-run listener, closing the handoff race. After publication, abort cancels the child. -### Structured output (package-internal runtime) +After fulfillment, the caller owns the run. Provider-plugin unload does not revoke it. `dispose()` removes the live abort listener, records cancellation, and delegates to the returned `AgentHandle.dispose()`, whose memoized quiescence transaction stops the loop, removes the agent and session, and unwinds scoped registrations. Cancellation owns every non-completed in-flight outcome and reports `aborted`; an already-completed turn remains completed. -The mechanism behind `outputSchema` for in-process children — acquired per structured RUN inside `startInProcessRun` (nothing is registered on a context that never runs a structured child; only the model-facing constants `STRUCTURED_OUTPUT_TOOL`/`STRUCTURED_OUTPUT_INSTRUCTION` are exported). One globally registered `structured_output` capture tool (its registered parameters are a placeholder) plus four listeners: +## Spawn and fork inputs -- a `system-prompt/assemble` waterfall listener registered `prepend: true` that post-processes `await next()` — **final-assembly enforcement**: the assembly the loop renders never carries `structured_output` for an agent without a structured run, and for one that has it always carries the run's OWN schema (as the tool's `parameters`) plus the calling instruction as a trailing prompt section (the demand travels with the tool — `AgentOptions` has no per-agent prompt field to carry it). The loop logs the rendered assembly as the step's `request/header`, so the injection is reconstructable log state, never a wire-only mutation. Per-agent shaping lives here because the tool registry and prompt assembly are context-global while schemas differ per concurrent child (FIXME in the module doc: per-agent/per-session scoping would dissolve this); cooperative mutate-then-`next()` would not survive a downstream listener returning a replacement assembly. -- a `tools/post-execute` listener (`prepend: true` = outermost, so `await next()` yields the composed final decision) that COMMITS the capture: the tool body only stages the validated value, and it becomes the run's result only when the final decision accepts the call — a downstream block (a PostToolUse hook) turns the logged result into `isError`, and the run must not report `structured` success for a call the model and session log saw fail. -- a `tools/pre-execute` deny for any call arriving after the agent's capture — terminal means terminal WITHIN the step: a response listing `structured_output` before further tool calls cannot run side effects after the final answer was accepted. -- an `agent/turn-continuation` listener (also `prepend: true` — an earlier-registered force-continue listener returning without `next()` must not decide the turn before the veto runs) that stops a child's turn once its output is captured, so a successful capture doesn't buy a wasted extra model step. +`InProcessRunOptions` is `{ seed?: SessionEvent[] }`. Spawn omits it. Fork supplies a balanced completed-turn prefix and records its length so the result reader never mistakes a seeded parent message for child output. -The capture tool validates each call against the run's schema (`validateStructuredValue`) — violations become an `INVALID_ARGS` isError result the model retries in-turn; a valid call stages the value for the post-execute commit. +`depthOf(agent)` reads `AgentOptions.subagentDepth`, treating absence as top-level depth zero and rejecting malformed stored values. `SubagentDepthError` reports an attempted child depth above `maxDepth`; an unrepresentable depth above the safe-integer domain is a `RangeError`. -Lifetime is refcounted by live structured runs: each acquires at start and releases at settle, so the registrations exist exactly while at least one structured child is live, a backend hot-reload mid-run cannot unregister the capture tool under a live child, and the last settle disposes everything. `release()` is idempotent per acquisition. +## Structured output -### `depthOf(agent): number` +`attachStructuredRuntime(childCtx, schema)` installs the whole contract in the child's scope: -Delegation depth rides on a merge-extensible `AgentOptions.subagentDepth` field (0 for a top-level agent, parent + 1 for a child), so a nested spawn reads its parent's depth from `parent.options.subagentDepth`. `depthOf` reads it (absent ⇒ 0). +- A `structured_output` tool registered with the requested schema validates and stages the model's value. +- An order-190 system-prompt section tells the child that the tool call is the terminal answer. +- Both contributions are ordinary child-scoped registrations. An expert `system-prompt/assemble` listener may replace them and therefore owns preserving the structured-output protocol for that child. +- A `tools/result` observer commits a staged value only after that execution's authoritative final tool result succeeds, including the enclosing `run_code` result for Code Mode sub-dispatch. +- A monotonic tool guard blocks later calls after capture, and `agent/turn-stop` ends the turn after the structured result commits. -### `SubagentDepthError` - -Thrown by `startInProcessRun` when a spawn would exceed the request's `maxDepth` cap; carries `attemptedDepth` and `maxDepth`. +A clean turn that never commits the required structured value reports `error`; the driver does not re-prompt. All registrations ride the child fiber and disappear with it. diff --git a/packages/subagent/subagent-inprocess/src/index.ts b/packages/subagent/subagent-inprocess/src/index.ts index 9026954b97..f6de7200cf 100644 --- a/packages/subagent/subagent-inprocess/src/index.ts +++ b/packages/subagent/subagent-inprocess/src/index.ts @@ -1,33 +1,24 @@ /** - * The shared in-process subagent run driver: run a child as a child - * {@link Agent} on the SAME cordis context (`ctx.agents`) — the cheapest - * transport, reusing the agent factory's quiescent {@link AgentHandle} - * teardown. The concrete in-process backends are thin shells over this driver, - * differing ONLY in the `seed` they pass (a fresh child vs. a child seeded with - * a prefix of the parent's log); everything downstream — drive the child, read - * its final output, map the stop reason, dispose — is identical and lives here. - * - * This package owns no provider and registers nothing; it is a pure library the - * backend packages depend on, so neither backend needs to know about the other. + * Shared driver for in-process subagent providers. The agent factory's + * creation transaction owns unpublished setup and rollback; after publication + * the returned AgentHandle is the one quiescent lifecycle owner held by the + * provider's caller. * * @module @deepseek-ai/dsh-subagent-inprocess */ import { randomUUID } from 'node:crypto' import type { Context } from 'cordis' -import { AgentId, type Agent, type AgentHandle, type AgentOptions } from '@deepseek-ai/dsh-agent' +import { AgentId, type Agent, type AgentOptions } from '@deepseek-ai/dsh-agent' import { SessionId, type SessionEvent, type TurnEndReason } from '@deepseek-ai/dsh-session' import type { ContentBlock } from '@deepseek-ai/dsh-llm' -import { assertSupportedOutputSchema } from '@deepseek-ai/dsh-tools' +import { assertSubagentMaxDepth } from '@deepseek-ai/dsh-subagent' import type { SubagentResult, SubagentRun, SubagentStartRequest, SubagentStopReason } from '@deepseek-ai/dsh-subagent' import { - acquireStructuredRuntime, - type StructuredAcquisition, + attachStructuredRuntime, + type StructuredAttachment, } from './structured.ts' -// The runtime itself (acquire/attach/release) is package-internal: runs -// acquire it inside startInProcessRun, and no other package drives it. Only -// the model-facing vocabulary is public. export { STRUCTURED_OUTPUT_TOOL, STRUCTURED_OUTPUT_INSTRUCTION, @@ -35,28 +26,26 @@ export { declare module '@deepseek-ai/dsh-agent' { interface AgentOptions { - /** - * The agent's delegation depth in the subagent tree — 0 for a top-level - * (config/ACP-created) agent, parent depth + 1 for a subagent. Set by the - * in-process backends on every child they create so a nested spawn reads its - * parent's depth from `parent.options.subagentDepth` and the `depthLimit` - * capability can cap the tree. Merge-extensible field (the seam owns it; the - * loop neither sets nor reads it). - */ + /** Delegation depth: zero for a top-level agent and parent depth + 1 for a child. */ subagentDepth?: number } } /** - * Read an agent's delegation depth (absent ⇒ a top-level agent, depth 0). - * @param agent - the agent whose options may carry `subagentDepth`. - * @returns 0 for a top-level agent, its parent's depth + 1 for a subagent. + * Read an agent's delegation depth, treating absence as top-level depth zero. + * @param agent - the agent whose options carry the depth. + * @returns its non-negative safe-integer depth. */ export function depthOf(agent: Agent): number { - return agent.options.subagentDepth ?? 0 + const depth = agent.options.subagentDepth + if (depth === undefined) return 0 + if (!Number.isSafeInteger(depth) || depth < 0 || Object.is(depth, -0)) { + throw new TypeError('agent subagentDepth must be a non-negative safe integer') + } + return depth } -/** Thrown when a spawn would exceed the request's `maxDepth` cap. */ +/** Thrown when starting a child would exceed the requested depth cap. */ export class SubagentDepthError extends Error { constructor(public readonly attemptedDepth: number, public readonly maxDepth: number) { super(`subagent depth ${attemptedDepth} exceeds maxDepth ${maxDepth}`) @@ -64,7 +53,7 @@ export class SubagentDepthError extends Error { } } -/** Map a session `turn/end` reason to a {@link SubagentStopReason}. */ +/** Map a session turn outcome to the subagent seam's terminal vocabulary. */ function toStopReason(reason: TurnEndReason | undefined): SubagentStopReason { switch (reason?.kind) { case 'completed': @@ -73,9 +62,6 @@ function toStopReason(reason: TurnEndReason | undefined): SubagentStopReason { return 'max-tokens' case 'aborted': return 'aborted' - // `disposed` (torn down mid-turn) and `interrupted` (crash-closed) both mean - // the turn did not finish cleanly; surface them as a generic failure rather - // than a clean completion. A missing reason (no turn ran) is also an error. case 'error': case 'disposed': case 'interrupted': @@ -84,168 +70,120 @@ function toStopReason(reason: TurnEndReason | undefined): SubagentStopReason { } } -/** Extra inputs the spawn/fork backends supply to {@link startInProcessRun}. */ +/** Extra inputs the spawn and fork providers supply to the shared driver. */ export interface InProcessRunOptions { - /** The provider name (`spawn`/`fork`), for error context only. */ - readonly providerName: string - /** - * The child session's seed: a balanced, contiguous-from-0 prefix of the - * parent's log (FORK), or `undefined` for a fresh child (SPAWN). - */ + /** Completed-turn seed for fork, or undefined for a fresh spawn. */ readonly seed?: SessionEvent[] } +/** Error used when cancellation wins before the child publication boundary. */ +function prePublicationAbort(): Error { + return new Error('subagent request was aborted before child publication') +} + /** - * Start an in-process child agent for `request` and return a {@link SubagentRun}. - * - * Drives the child as a one-shot: `send(prompt)` then `whenIdle()` (the ordering - * matters — `send` enqueues synchronously, so `whenIdle` observes the queued - * work and resolves only on the child's `running → idle` transition, never - * before the turn starts). The final `assistant/message` is the result output, - * the matching `turn/end.reason` the stop reason. `dispose()` delegates to the - * factory's {@link AgentHandle.dispose} (stop loop → await quiescence → remove - * session); `cancel()` cancels the child's in-flight turn. - * - * Throws {@link SubagentDepthError} before creating anything when the child's - * depth (parent depth + 1) would exceed `request.maxDepth`. - * @param ctx - the context whose `agents` factory creates and owns the child. - * @param request - the start request (prompt, parent, signal, per-child options). - * @param options - the backend's inputs: provider name plus the optional seed. - * @returns the live run handle for the child agent. + * Establish and drive one in-process child. Fulfillment means the agent is + * already published in the registry; rejection means the agent factory's + * creation transaction and any partially-created child have reached quiescence. + * @param request - the trusted typed start request, including its required signal. + * @param options - the optional fork seed. + * @returns a ready holder-owned run. */ -export function startInProcessRun( - ctx: Context, +export async function startInProcessRun( request: SubagentStartRequest, options: InProcessRunOptions, -): SubagentRun { - const childDepth = depthOf(request.parent) + 1 +): Promise { + assertSubagentMaxDepth(request.maxDepth) + if (request.signal.aborted) throw prePublicationAbort() + const parent = request.parent + const childDepth = depthOf(parent) + 1 + if (!Number.isSafeInteger(childDepth)) { + throw new RangeError('subagent child depth exceeds the safe-integer range') + } if (request.maxDepth !== undefined && childDepth > request.maxDepth) { throw new SubagentDepthError(childDepth, request.maxDepth) } - // Assert, then snapshot, the schema subset BEFORE any child exists (the - // service has already capability-gated; this rejects a schema outside the - // enforced subset loud). Assertion comes FIRST so a hostile value fails as - // OutputSchemaError, never as structuredClone's raw DataCloneError — the - // asserted subset is plain JSON data, which always clones. The snapshot is - // load-bearing: the caller keeps its reference, so attaching the ORIGINAL - // would let a post-start() mutation drift the enforced schema away from the - // asserted one — the clone (taken synchronously with the assertion, no - // interleaving possible) pins assertion, the model-visible parameters, and - // validateStructuredValue to one isolation-immutable value. - if (request.outputSchema !== undefined) assertSupportedOutputSchema(request.outputSchema) - const schema = request.outputSchema === undefined ? undefined : structuredClone(request.outputSchema) const childId = AgentId(randomUUID()) - // The child's OWN events begin after the seed (fork seeds the parent's - // completed-turn prefix; spawn seeds nothing). `readResult` scopes to this - // boundary so a child that produces no message of its own never returns the - // SEEDED parent's last assistant message as its result. const seedLength = options.seed?.length ?? 0 - const parentHeader = request.parent.session.header - // Inherit the parent's model by default (a child with no model cannot run); - // an explicit `request.agentOptions.model` overrides it. The persona needs - // no inheritance: the deployment persona is a context-wide prompt section, - // so parent and child render the same one. A structured run's - // structured_output instruction is NOT prompt state either — the structured - // runtime's final-request listener appends it per request (see structured.ts). + const parentHeader = parent.session.header + const parentModel = parent.options.model const agentOptions: AgentOptions = { - ...request.parent.options.model !== undefined ? { model: request.parent.options.model } : {}, + ...parentModel !== undefined ? { model: parentModel } : {}, ...request.agentOptions, subagentDepth: childDepth, } - // The structured runtime is held for the WHOLE run (acquired before the child - // exists, released when the result settles), so a backend hot-reload mid-run - // cannot unregister the capture tool out from under this live child. - const structured: StructuredAcquisition | undefined = schema !== undefined ? acquireStructuredRuntime(ctx) : undefined + let structured: StructuredAttachment | undefined + const setup = (childCtx: Context): void => { + if (request.persona !== undefined) { + childCtx.systemPrompt.section({ name: 'deployment:persona', order: 0, text: request.persona }) + } + if (request.toolFilter !== undefined) childCtx.tools.restrict(request.toolFilter) + if (request.outputSchema !== undefined) { + structured = attachStructuredRuntime(childCtx, request.outputSchema) + } + } - const handle: AgentHandle = ctx.agents.create({ + const flags = { cancelled: false } + const handle = await parent.ctx.agents.create({ agentId: childId, sessionId: SessionId(randomUUID()), meta: { ...parentHeader.cwd !== undefined ? { cwd: parentHeader.cwd } : {}, parentSession: parentHeader.id, - // Record the seed boundary so a reload (and a replay harness) can tell the - // inherited prefix from the child's OWN events. 0 for a fresh spawn. ...seedLength > 0 ? { seedLength } : {}, }, ...options.seed !== undefined ? { seed: options.seed } : {}, agentOptions, + signal: request.signal, + setup, }) const child = handle.agent - if (structured && schema !== undefined) structured.attach(child, schema) - - // Bridge the request's abort signal to the child (the consumer also bridges - // its own exec.signal, but a backend-level bridge keeps the contract local). - // `cancelled` records that a cancel was requested at all, so the pre-turn - // cancel window — where the child clears the queued prompt before any - // `turn/end` is logged — settles as `aborted` (honoring the cancel contract) - // rather than falling through to the no-turn `error` mapping. - let cancelled = false - // An accessor, not an inline read: `cancelled` mutates from closures (the - // abort listener, run.cancel), which control-flow narrowing cannot see — an - // inline read at the result mapping would narrow to the initializer. - const isCancelled = (): boolean => cancelled - const requestCancel = (reason: string): void => { - cancelled = true - child.cancel(reason) + // Agent creation detaches its creation-only abort listener before returning. + // Close the narrow handoff race before installing the live-run listener. + // Static analysis does not model the abort that may land between the + // factory's listener detachment and this continuation. + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (request.signal.aborted) { + flags.cancelled = true + await handle.dispose() + throw prePublicationAbort() } - const onAbort = (): void => { requestCancel('subagent cancelled') } - request.signal?.addEventListener('abort', onAbort, { once: true }) + + const onAbort = (): void => { + flags.cancelled = true + child.cancel('subagent request aborted') + } + request.signal.addEventListener('abort', onAbort, { once: true }) const result: Promise = (async () => { try { - // A signal already aborted BEFORE the run starts never fires an `abort` - // event (`addEventListener` only fires on the transition), so the listener - // above won't catch it — settle `aborted` without running the child rather - // than completing an already-cancelled request. - if (request.signal?.aborted) return { output: [], stopReason: 'aborted' } child.send(request.prompt) await child.whenIdle() - // Deliberately NO re-prompt when a structured child finishes cleanly - // without calling structured_output: readResult maps that to `error` — - // the shortfall goes to the parent instead of buying extra model turns. - return readResult(child, seedLength, isCancelled(), structured ? { captured: structured.captured(child) } : undefined) + return readResult( + child, + seedLength, + flags.cancelled, + structured ? { captured: structured.captured() } : undefined, + ) } finally { - request.signal?.removeEventListener('abort', onAbort) - if (structured) { - structured.detach(child) - structured.release() - } + request.signal.removeEventListener('abort', onAbort) } })() return { id: childId, result, - cancel(reason?: string): void { - requestCancel(reason ?? 'subagent cancelled') - }, - async dispose(): Promise { - request.signal?.removeEventListener('abort', onAbort) - await handle.dispose() + dispose(): Promise { + request.signal.removeEventListener('abort', onAbort) + flags.cancelled = true + return handle.dispose() }, } } -/** - * Read a settled child's terminal result from its session log, scoped to the - * child's OWN events (everything at or after `seedLength` — fork seeds the - * parent's completed-turn prefix, so a child that produced no message of its - * own must NOT return the seeded parent's last assistant message). The output - * is the child's last `assistant/message` content (deep-cloned — the log is - * frozen); the stop reason is the child's last `turn/end` reason mapped to a - * {@link SubagentStopReason}. When `cancelled` is set but no `turn/end` was - * logged (a cancel landed in the pre-turn window, before any turn ran), the - * run settles `aborted` per the {@link SubagentRun.cancel} contract rather than - * the generic no-turn `error`. - * - * A structured run (`structured` present) additionally reports the captured - * value on {@link SubagentResult.structured}. A structured child that finished - * CLEANLY without ever capturing (the nudges ran out) settles `error` — a clean - * finish without the demanded structured result is a failure, not a success - * with a missing field; a non-`completed` reason keeps its own honest mapping. - */ +/** Read one settled child's result from events after its optional fork seed. */ function readResult( child: Agent, seedLength: number, @@ -253,17 +191,20 @@ function readResult( structured?: { captured?: { value: unknown } | undefined }, ): SubagentResult { const own = child.session.events.slice(seedLength) - const lastMessage = own.findLast((e): e is SessionEvent<'assistant/message'> => e.type === 'assistant/message') - const lastEnd = own.findLast((e): e is SessionEvent<'turn/end'> => e.type === 'turn/end') - const output: ContentBlock[] = lastMessage ? structuredClone(lastMessage.data.content) : [] - const stopReason: SubagentStopReason = lastEnd === undefined && cancelled + const lastMessage = own.findLast((event): event is SessionEvent<'assistant/message'> => event.type === 'assistant/message') + const lastEnd = own.findLast((event): event is SessionEvent<'turn/end'> => event.type === 'turn/end') + const output: ContentBlock[] = lastMessage?.data.content ?? [] + const recorded = toStopReason(lastEnd?.data.reason) + // Disposal can tear the owner down before the loop records its ordinary + // `aborted` end, yielding `disposed` instead. A requested cancellation owns + // every non-completed in-flight outcome; a turn already completed stays so. + const stopReason: SubagentStopReason = cancelled && recorded !== 'completed' ? 'aborted' - : toStopReason(lastEnd?.data.reason) - if (structured) { - if (structured.captured) return { output, structured: structured.captured.value, stopReason } - // No capture on a cleanly-completed turn: an ERROR when the run was left - // to finish (the nudges ran out), but ABORTED when a cancel is why the - // nudging stopped — the cancel contract outranks the schema shortfall. + : recorded + if (structured !== undefined) { + if (structured.captured !== undefined) { + return { output, structured: structured.captured.value, stopReason } + } if (stopReason === 'completed') return { output, stopReason: cancelled ? 'aborted' : 'error' } } return { output, stopReason } diff --git a/packages/subagent/subagent-inprocess/src/structured.ts b/packages/subagent/subagent-inprocess/src/structured.ts index a557e44785..16ecad7bd4 100644 --- a/packages/subagent/subagent-inprocess/src/structured.ts +++ b/packages/subagent/subagent-inprocess/src/structured.ts @@ -1,312 +1,173 @@ /** - * Structured-output support for the in-process subagent backends: the mechanism - * behind `SubagentStartRequest.outputSchema` for children that run as agents on - * the same context. + * Structured-output support for the in-process subagent backends: the + * mechanism behind `SubagentStartRequest.outputSchema` for children that run + * as agents on the same context. * - * The model-facing surface is one globally registered `structured_output` tool - * whose REGISTERED parameters are a placeholder — the real schema is per run. - * Because the tool registry and prompt assembly are context-global while - * schemas differ per child (two concurrent structured runs may carry different - * schemas), per-agent shaping happens on the `system-prompt/assemble` - * waterfall with a `prepend: true` listener that post-processes `await next()` - * — FINAL-ASSEMBLY enforcement: whatever downstream listeners mutated or - * replaced, the assembly the loop renders never carries `structured_output` - * for an agent without a structured run, and for one that has it always - * carries the run's OWN schema plus a trailing - * {@link STRUCTURED_OUTPUT_INSTRUCTION} section (the demand travels with the - * tool). The loop logs what the assembly produced as the request header, so - * the injection is a reconstructable fact of the session log, never a - * wire-only mutation (the reconstructability RFC). - * (Cooperative mutate-then-`next()` would not survive a downstream listener - * returning a replacement assembly — see the waterfall composition caveat in - * docs/architecture.md.) + * Everything is a SCOPED registration on the child agent's context + * (`child.ctx`, the dsh-scope seam): the `structured_output` capture tool + * carries the run's REAL schema as its registered parameters (each child sees + * exactly its own schema — two concurrent structured runs never interact), the + * demand instruction is an ordinary order-190 scoped section, and the + * enforcement listeners fire only for this child (scope-filtered dispatch). + * Registration lifetime rides the child's fiber, so a backend hot-reload + * mid-run cannot unregister the capture tool out from under a live child, and + * a disposed child leaves no residue — no placeholder schema, + * strip-for-everyone-else pass, or refcounted global runtime. * - * FIXME: the whole enforcement dance above exists because the tool registry - * and prompt assembly are context-global. If they become per-agent or - * per-session scoped, a structured run just registers its own schema'd tool on - * the child's scope and this module reduces to the capture tool plus the - * turn-stop — no placeholder, no final-assembly swap, no strip-for-everyone- - * else, no global-registration lifetime dance. + * The child scope's registrations enforce the contract: * - * A companion `agent/turn-continuation` listener stops a child's turn once its - * output is captured — without it, the loop's default "had tool calls ⇒ - * continue" buys a wasted extra model step per structured child. It is also - * `prepend: true`: the veto must run before any earlier-registered listener - * that could short-circuit the chain into a forced continue. A third listener - * closes the within-step window the continuation veto cannot: a - * `tools/pre-execute` deny for any call arriving after the agent's capture, so - * a response that lists `structured_output` before further tool calls cannot - * run side effects after the final answer was accepted. A fourth, - * `tools/post-execute`, is the capture COMMIT: the tool body only stages the - * validated value, and it becomes the run's captured result only when the - * final post-execute decision accepts the call — a blocking hook downstream - * yields `isError` in the log, and the run must not report success for it. - * - * Lifetime is refcounted by structured RUNS: each acquires from start to - * settle, so the registrations exist exactly while at least one structured - * child is live — a plain deployment that never passes `outputSchema` carries - * no always-on global state, and a backend hot-reload mid-run cannot - * unregister the capture tool out from under a live child (the run holds its - * own acquisition). Registrations land on the ROOT context and the refcount - * disposes them when the last run settles; the next structured run - * re-registers them. + * - The scoped capture tool and instruction are ordinary assembly inputs. The + * loop logs the assembled request header, so the demand is reconstructable + * log state rather than a wire-only mutation. As with every other assembly + * contribution, an expert `system-prompt/assemble` listener that deliberately + * removes or replaces either input owns the resulting composition. + * - `agent/turn-stop` (serial, scoped): stop the child's turn once its output + * is captured. This terminal checkpoint runs after the ordinary continuation + * waterfall and steering folding, so listener order cannot resurrect a + * completed structured run or carry terminal steering into another turn. + * - `tools.guard()` is the monotonic terminal gate after the extensible + * pre-execute waterfall: once capture commits, no later listener can turn + * the denial back into a dispatched side effect. + * - `tools/result` is the capture COMMIT point. The tool body only STAGES the + * validated value in a WeakMap keyed by the execution object; the awaited, + * non-transforming notification promotes it only when the authoritative + * result after the whole pre/execute/post pipeline succeeds. For a Code Mode + * sub-dispatch, promotion waits again for the enclosing `run_code` result, so + * a runtime failure or outer post-policy block cannot report structured + * success. Execution identity makes call-id reuse and orphaned stages + * irrelevant. * * @module @deepseek-ai/dsh-subagent-inprocess/structured */ import type { Context } from 'cordis' -import type { Agent } from '@deepseek-ai/dsh-agent' +import type { ContinuationStop } from '@deepseek-ai/dsh-agent' import type { ContentBlock, ToolSchema } from '@deepseek-ai/dsh-llm' -import type { ContinuationDecision } from '@deepseek-ai/dsh-agent' -import type { AssembleContext, PromptAssembly } from '@deepseek-ai/dsh-system-prompt' -import type { PostToolDecision, PreToolDecision, ToolExecution, ToolExecutionResult } from '@deepseek-ai/dsh-tools' +import type { ToolExecution } from '@deepseek-ai/dsh-tools' import { ToolArgsError, validateStructuredValue, type StructuredOutputSchema } from '@deepseek-ai/dsh-tools' /** The model-facing tool name a structured child must call to finish. */ export const STRUCTURED_OUTPUT_TOOL = 'structured_output' /** - * The instruction the assembly listener appends to a structured child's - * system prompt as a trailing section on every assembly. Per-assembly state, - * NOT agent prompt state: `AgentOptions` has no prompt field (the persona is - * deployment config on the system-prompt plugin), so the same final-assembly - * enforcement that injects the schema'd tool carries the instruction that - * demands calling it. + * The instruction registered as the child's trailing (order-190, the end of + * the tool-guidance band) scoped prompt section: the demand travels with the + * tool, as ordinary prompt state of exactly one agent. */ export const STRUCTURED_OUTPUT_INSTRUCTION = 'When you have your final answer, you MUST report it by calling the ' + `\`${STRUCTURED_OUTPUT_TOOL}\` tool with arguments matching its parameter schema exactly. ` + 'Do not finish with a plain text answer: only the tool call counts as your result.' -/** One structured run's state: the schema to enforce and the captured value, once recorded. */ -interface RunState { - readonly schema: StructuredOutputSchema +/** One structured run's live handle: read the captured value once the child settles. */ +export interface StructuredAttachment { /** - * A validated value awaiting the post-execute verdict on ITS OWN call. Set - * by the capture tool's body, promoted to {@link RunState.captured} only - * when the final `tools/post-execute` decision accepts the call — a - * downstream block turns the logged result into `isError`, and a value - * committed at body time would let the run report success for a call the - * model saw fail. + * The captured value, once the child called the tool with valid arguments + * and the authoritative final tool result accepted that call. + * @returns the committed value, or undefined while none was accepted. */ - pending?: { value: unknown } - captured?: { value: unknown } -} - -/** The per-root-context runtime: run states plus the shared registrations. */ -interface StructuredRuntime { - refs: number - readonly states: WeakMap - readonly disposers: (() => void)[] -} - -/** One root context ⇒ one runtime (multi-app test isolation). */ -const runtimes = new WeakMap() - -/** - * One holder's handle on the shared structured runtime. `release()` is - * idempotent per acquisition; the runtime's registrations are disposed when the - * LAST holder (backend plugin or live run) releases. - */ -export interface StructuredAcquisition { - /** Enforce `schema` on `agent`'s requests and start capturing its `structured_output` call. */ - attach(agent: Agent, schema: StructuredOutputSchema): void - /** The captured value, once the child called the tool with valid arguments. */ - captured(agent: Agent): { value: unknown } | undefined - /** Stop enforcing/capturing for `agent` (WeakMap-backed; safe to call twice). */ - detach(agent: Agent): void - /** Drop this holder's reference (idempotent); the last release unregisters everything. */ - release(): void + captured(): { value: unknown } | undefined } /** - * Acquire the per-root-context structured runtime, registering the capture tool - * and the runtime's listeners on the FIRST acquisition. See the module doc - * for the enforcement and lifetime design. - * @param ctx - any context of the app; the runtime keys off `ctx.root`. - * @returns this holder's handle (attach/captured/detach + idempotent release). + * Attach the structured-output runtime to a child for `schema`: register the + * scoped capture tool (real schema), the scoped instruction section, and the + * scoped enforcement registrations (see the module doc). Call from the + * agent-creation `setup` window with the child's scope context — every + * registration rides the child's fiber and unwinds with the child. + * @param childCtx - the child agent's scope context (`setup`'s argument). + * @param schema - the trusted, already-asserted schema subset to enforce (see + * `assertSupportedOutputSchema` in dsh-tools). + * @returns the attachment handle (read `captured()` after the child settles). */ -export function acquireStructuredRuntime(ctx: Context): StructuredAcquisition { - const root: Context = ctx.root - let runtime = runtimes.get(root) - if (!runtime) { - runtime = { refs: 0, states: new WeakMap(), disposers: [] } - runtimes.set(root, runtime) - registerRuntime(root, runtime) - } - runtime.refs += 1 +export function attachStructuredRuntime(childCtx: Context, schema: StructuredOutputSchema): StructuredAttachment { + /** + * Validated values staged by the capture tool body, awaiting THEIR OWN + * authoritative `tools/result` notification. The execution object's identity + * uniquely identifies a trip through the pipeline: adapter call ids may + * repeat across steps, but another execution can never reach this WeakMap + * entry. This is distinct from the opaque `ToolExecutionToken` used to + * correlate nested transports. The final notification always deletes its own + * stage, whether the result succeeded or failed. + */ + const staged = new WeakMap() + /** Successful nested capture waiting for its enclosing transport to commit. */ + let pending: { parent: ToolExecution['token']; value: unknown } | undefined + let captured: { value: unknown } | undefined - let released = false - return { - attach(agent: Agent, schema: StructuredOutputSchema): void { - runtime.states.set(agent, { schema }) - }, - captured(agent: Agent): { value: unknown } | undefined { - return runtime.states.get(agent)?.captured - }, - detach(agent: Agent): void { - runtime.states.delete(agent) - }, - release(): void { - if (released) return - released = true - runtime.refs -= 1 - if (runtime.refs > 0) return - runtimes.delete(root) - for (const dispose of runtime.disposers.splice(0)) dispose() - }, + const schemaEntry: ToolSchema = { + name: STRUCTURED_OUTPUT_TOOL, + description: + 'Report your final structured result. Call this exactly once, when your answer is complete; ' + + 'the arguments must match this tool\'s parameter schema exactly.', + // ToolSchema.parameters is the wire-level JSON Schema object; the + // asserted subset type is structurally exactly that. + parameters: schema as unknown as Record, } -} -/** Register the capture tool + the two listeners on the root context (first acquire). */ -function registerRuntime(root: Context, runtime: StructuredRuntime): void { - // The registered parameters are a PLACEHOLDER: the request listener below - // swaps in the run's real schema per child, and strips the tool entirely for - // every agent without a structured run — so this shape is never model-visible. - // - // Registration does NOT ride on the acquiring backend's plugin-level - // `inject`: a backend that waited on `tools` would apply later than it did - // before this module existed, shifting when its PROVIDER registers — and the - // delegation tool mirrors provider lifecycle, so that shift would reorder - // the model-visible tool list of every existing prompt. Instead the capture - // tool registers synchronously when `tools` is already live (the common - // case), and through a scoped inject fiber when the Loader happens to start - // the backend first. Either way the registration lands on root and is - // disposed by the runtime's refcount; disposing the fiber also covers the - // never-activated case. - let disposeTool: (() => void) | undefined - const registerCapture = (tools: Context['tools']): void => { - disposeTool = tools.register({ - name: STRUCTURED_OUTPUT_TOOL, - description: - 'Report your final structured result. Call this exactly once, when your answer is complete; ' - + 'the arguments must match this tool\'s parameter schema exactly.', - parameters: { type: 'object', properties: {} }, - execute(args: unknown, exec: ToolExecution): Promise { - const state = exec.agent ? runtime.states.get(exec.agent) : undefined - if (!state) { - // Reachable only if a non-structured agent somehow calls the tool (the - // request listener strips it, so the model never sees it) — fail loud - // rather than capture into nowhere. - throw new Error(`${STRUCTURED_OUTPUT_TOOL} is only available to subagents started with an output schema`) + childCtx.tools.register({ + ...schemaEntry, + execute(args: unknown, exec: ToolExecution): Promise { + const violations = validateStructuredValue(schema, args) + // ToolArgsError → isError result with INVALID_ARGS: the model retries + // within the same turn, exactly like a schema-validated defineTool call. + if (violations.length > 0) throw new ToolArgsError(violations) + // Two-phase commit, keyed by THIS execution: later transformable + // waterfalls may still turn the success into an error. ToolRegistry has + // already frozen model-bound arguments at the actual input boundary. + staged.set(exec, { value: args }) + return Promise.resolve([{ type: 'text', text: 'Structured output recorded.' }]) + }, + }) + + childCtx.systemPrompt.section({ + name: `tool:${STRUCTURED_OUTPUT_TOOL}`, + order: 190, + text: STRUCTURED_OUTPUT_INSTRUCTION, + }) + + // Stop the child's turn once its output is captured. This monotonic serial + // checkpoint runs after the ordinary continuation waterfall, its reason, + // and late-steering folding, so no ordering trick can resume a finished run. + childCtx.on('agent/turn-stop', function (this: unknown): ContinuationStop | undefined { + return captured === undefined ? undefined : { action: 'stop' } + }) + + // Terminal WITHIN the step. Guards run after the whole pre-execute + // waterfall and compose monotonically (deny or abstain, never allow), so a + // later prepended listener cannot resurrect dispatch. Calls that precede + // capture in the same response remain untouched. + childCtx.tools.guard(exec => captured === undefined && pending === undefined + ? undefined + : `structured output already recorded: the run is complete, so \`${exec.name}\` is not executed`) + + // The capture COMMIT observes the immutable, authoritative result after the + // complete pipeline and outer error normalization. This notification cannot + // transform the outcome, so there is no wrapper outside the commit verdict. + childCtx.on('tools/result', function (this: unknown, exec, result) { + if (exec.name === STRUCTURED_OUTPUT_TOOL) { + const entry = staged.get(exec) + if (entry === undefined) return + staged.delete(exec) + if (result.isError) return + if (exec.parent === undefined) { + /* v8 ignore else -- sequential agent-loop dispatch lets the guard block every later supported call */ + if (captured === undefined) captured = { value: entry.value } + } else { + /* v8 ignore else -- Code Mode serializes sub-dispatches, so the guard blocks every later supported call */ + if (captured === undefined && pending === undefined) { + pending = { parent: exec.parent, value: entry.value } } - const violations = validateStructuredValue(state.schema, args) - // ToolArgsError → isError result with INVALID_ARGS: the model retries - // within the same turn, exactly like a schema-validated defineTool call. - if (violations.length > 0) throw new ToolArgsError(violations) - // Two-phase commit: the body only STAGES the value; the post-execute - // listener below promotes it once the final decision accepts the call. - state.pending = { value: args } - return Promise.resolve([{ type: 'text', text: 'Structured output recorded.' }]) - }, - }) - } - const liveTools = root.get('tools') - const toolsFiber = liveTools ? undefined : root.inject(['tools'], (childCtx: Context) => { - registerCapture(childCtx.root.tools) - }) - if (liveTools) registerCapture(liveTools) - runtime.disposers.push(() => { - disposeTool?.() - void toolsFiber?.dispose() - }) - - // FINAL-ASSEMBLY enforcement (prepend: true = first registered = OUTERMOST - // wrapper): post-process whatever the downstream listeners and the registry - // produced, so a downstream listener returning a replacement assembly cannot - // leak the tool to other agents or erase the child's schema. The loop logs - // the rendered assembly as the step's request header, so the swap is - // reconstructable log state, never a wire-only mutation. - runtime.disposers.push(root.on('system-prompt/assemble', async function ( - this: unknown, _assembly: PromptAssembly, context: AssembleContext, next: () => Promise, - ): Promise { - const final = await next() - const state = context.agent ? runtime.states.get(context.agent) : undefined - if (state) { - const schemaEntry: ToolSchema = { - name: STRUCTURED_OUTPUT_TOOL, - description: - 'Report your final structured result. Call this exactly once, when your answer is complete; ' - + 'the arguments must match this tool\'s parameter schema exactly.', - // ToolSchema.parameters is the wire-level JSON Schema object; the - // asserted subset type is structurally exactly that. - parameters: state.schema as unknown as Record, } - final.tools = [...final.tools.filter(tool => tool.name !== STRUCTURED_OUTPUT_TOOL), schemaEntry] - // The demand travels WITH the tool: a trailing section in the - // tool-guidance order band, appended after next() so it renders last - // (renderPrompt joins in array order). - final.sections = [...final.sections, { name: `tool:${STRUCTURED_OUTPUT_TOOL}`, order: 190, text: STRUCTURED_OUTPUT_INSTRUCTION }] - return final + return } - // No structured run: strip the placeholder so it is never model-visible. - // An empty tools array canonicalizes to an absent header/wire field - // (canonicalHeader pins empty ≡ absent), so no re-shaping is needed here. - final.tools = final.tools.filter(tool => tool.name !== STRUCTURED_OUTPUT_TOOL) - return final - }, { prepend: true })) + if (pending?.parent !== exec.token) return + const entry = pending + pending = undefined + if (result.isError) return + /* v8 ignore else -- Code Mode serializes outer executions, so the guard blocks every later supported call */ + if (captured === undefined) captured = { value: entry.value } + }) - // Stop a structured child's turn once its output is captured: the default - // "had tool calls ⇒ continue" would otherwise buy a wasted extra model step - // after every successful capture. `prepend: true` puts the veto OUTERMOST — - // an earlier-registered listener that short-circuits the chain (a goal-style - // force-continue returning without `next()`) would otherwise decide the turn - // before this listener ever ran, and no downstream decision may resurrect a - // structured turn that is already finished. - runtime.disposers.push(root.on('agent/turn-continuation', function ( - this: unknown, agent: Agent, _turn: number, _decision: ContinuationDecision, next: () => Promise, - ): Promise { - if (runtime.states.get(agent)?.captured) return Promise.resolve({ action: 'stop' }) - return next() - }, { prepend: true })) - - // The capture COMMIT: promote the staged value only when the final - // post-execute decision accepts the call. The capture tool's body cannot - // decide — `tools/post-execute` runs after it, and a blocking listener (a - // PostToolUse hook) turns the logged result into `isError` feedback; a value - // committed at body time would make readResult report `structured` success - // for a call whose result the model and session log saw fail. `prepend: - // true` = outermost at registration time, so `await next()` returns the - // COMPOSED downstream decision — the same final verdict the registry maps - // onto the result. (A later-registered outer listener that blocks without - // delegating skips this commit entirely: the staged value is dropped and the - // run errors — failure-safe in the same direction.) The staging slot clears - // on every path, including a rejecting downstream listener. - runtime.disposers.push(root.on('tools/post-execute', async function ( - this: unknown, exec: ToolExecution, _result: ToolExecutionResult, next: () => Promise, - ): Promise { - const state = exec.agent ? runtime.states.get(exec.agent) : undefined - if (!state || exec.name !== STRUCTURED_OUTPUT_TOOL || state.pending === undefined) return next() - const pending = state.pending - try { - const decision = await next() - if (decision.kind === 'accept') state.captured = pending - return decision - } finally { - delete state.pending - } - }, { prepend: true })) - - // Terminal means terminal WITHIN the step, not only at its end: the - // turn-continuation veto above runs after every call in the current model - // response has executed, so a response that puts `structured_output` before - // further tool calls would still perform those side effects after the final - // answer was accepted. Deny every later call for a captured agent at the - // allow/deny gate — dispatch is skipped and the model sees an `isError` - // result naming the contract. Calls that PRECEDE the capture in the same - // response ran before `captured` was set and are untouched; a second - // `structured_output` is denied like any other call. `prepend: true` for the - // same reason as the continuation veto: no earlier-registered allow may - // short-circuit past the terminal contract. - runtime.disposers.push(root.on('tools/pre-execute', function ( - this: unknown, exec: ToolExecution, next: () => Promise, - ): Promise { - if (exec.agent && runtime.states.get(exec.agent)?.captured) { - return Promise.resolve({ - kind: 'deny', - reason: `structured output already recorded: the run is complete, so \`${exec.name}\` is not executed`, - }) - } - return next() - }, { prepend: true })) + return { captured: () => captured } } diff --git a/packages/subagent/subagent-inprocess/tests/structured.spec.ts b/packages/subagent/subagent-inprocess/tests/structured.spec.ts index 0de036a0a0..3e23123622 100644 --- a/packages/subagent/subagent-inprocess/tests/structured.spec.ts +++ b/packages/subagent/subagent-inprocess/tests/structured.spec.ts @@ -5,21 +5,30 @@ import SessionStore from '@deepseek-ai/dsh-session' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRegistry from '@deepseek-ai/dsh-tools' import AgentRegistry, { AgentId } from '@deepseek-ai/dsh-agent' -import type { Agent, ContinuationDecision } from '@deepseek-ai/dsh-agent' +import type { ContinuationDecision } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import * as Invariants from '@deepseek-ai/dsh-invariants' import SubagentService, { type SubagentStartRequest } from '@deepseek-ai/dsh-subagent' -import type { StructuredOutputSchema } from '@deepseek-ai/dsh-tools' +import type { Config as ToolConfig, StructuredOutputSchema } from '@deepseek-ai/dsh-tools' +import { RUN_CODE_NAME } from '@deepseek-ai/dsh-tools' import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' import { startInProcessRun } from '../src/index.ts' import { - acquireStructuredRuntime, STRUCTURED_OUTPUT_INSTRUCTION, STRUCTURED_OUTPUT_TOOL, } from '../src/structured.ts' type Script = ConstructorParameters[0] +interface CodeRunRequestLike { + bindings: { global: string; functions: Record Promise> }[] +} + +interface SetupOptions { + toolMode?: ToolConfig['mode'] + codeRun?: (request: CodeRunRequestLike) => Promise<{ logs: never[]; value?: unknown }> +} + const SCHEMA: StructuredOutputSchema = { type: 'object', properties: { answer: { type: 'number' }, note: { type: 'string' } }, @@ -27,29 +36,36 @@ const SCHEMA: StructuredOutputSchema = { } /** - * Real loop + scripted mock model + an INLINE spawn-shaped provider over the + * Real loop + scripted mock model + an INLINE fresh-conversation provider over the * shared driver. The concrete backend plugins are deliberately NOT loaded — * they would devDep-cycle this package (spawn/fork already depend on the * driver), and the runtime under test is the driver's; plugin-level structured * coverage lives in the spawn/fork specs. The mock model script drives the * child's structured_output calls. */ -async function setup(script: Script) { +async function setup(script: Script, options: SetupOptions = {}) { const ctx = new Context() const adapter = new MockAdapter(script) await ctx.plugin(LlmService) await ctx.plugin(SessionStore) await ctx.plugin(SystemPrompt) - await ctx.plugin(ToolRegistry) + await ctx.plugin(ToolRegistry, { mode: options.toolMode ?? 'native' }) + if (options.toolMode === 'code' || options.toolMode === 'both') { + ctx.provide('codeRuntime', { + language: 'typescript', + isolation: 'test', + run: options.codeRun ?? (() => Promise.resolve({ logs: [] })), + } as never) + } await ctx.plugin(AgentRegistry) await ctx.plugin(Invariants) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(SubagentService) const disposeProvider = ctx.subagents.registerProvider({ name: 'spawn', - capabilities: { outputSchema: true, depthLimit: true, toolFilter: false }, + capabilities: { outputSchema: true, depthLimit: true, toolFilter: false, persona: false }, inheritsParentContext: false, - start: (request: SubagentStartRequest) => startInProcessRun(ctx, request, { providerName: 'spawn' }), + start: (request: SubagentStartRequest) => startInProcessRun(request, {}), }) ctx.llm.registerAdapter(['mock'], adapter) const parent = ctx.agentLoop.create(AgentId('parent'), { model: 'mock' }) @@ -57,7 +73,13 @@ async function setup(script: Script) { } function structuredRequest(parent: SubagentStartRequest['parent'], extra?: Partial): SubagentStartRequest { - return { prompt: [{ type: 'text', text: 'produce the answer' }], parent, outputSchema: SCHEMA, ...extra } + return { + prompt: [{ type: 'text', text: 'produce the answer' }], + parent, + signal: new AbortController().signal, + outputSchema: SCHEMA, + ...extra, + } } /** The tool names of one recorded model request. */ @@ -70,7 +92,7 @@ describe('in-process structured output', () => { const { ctx, parent } = await setup([ toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 42, note: 'done' }), ]) - const run = ctx.subagents.start('spawn', structuredRequest(parent)) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) const result = await run.result expect(result.stopReason).toBe('completed') expect(result.structured).toEqual({ answer: 42, note: 'done' }) @@ -82,7 +104,7 @@ describe('in-process structured output', () => { toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 1 }), textResponse('MUST NOT BE CONSUMED'), ]) - const run = ctx.subagents.start('spawn', structuredRequest(parent)) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) await run.result // Default continuation would run a second step after the tool call; the // structured runtime's turn-continuation veto stops the turn instead. @@ -113,7 +135,7 @@ describe('in-process structured output', () => { return Promise.resolve([{ type: 'text', text: 'ran' }]) }, }) - const run = ctx.subagents.start('spawn', structuredRequest(parent)) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) const result = await run.result expect(result.stopReason).toBe('completed') expect(result.structured).toEqual({ answer: 5 }) @@ -122,6 +144,44 @@ describe('in-process structured output', () => { await run.dispose() }) + it('a later prepended pre-execute listener cannot resurrect dispatch after capture', async () => { + const response = [ + ...toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 5 }).slice(0, -2), + { type: 'block-start', index: 1, blockType: 'tool-call' }, + { type: 'block-end', index: 1, block: { type: 'tool-call', id: CallId('c2'), name: 'side_effect', arguments: '{}' } }, + { type: 'usage', usage: { inputTokens: 10, outputTokens: 5 } }, + { type: 'finish', reason: { kind: 'tool-calls' } }, + ] as Script[number] + const { ctx, parent } = await setup([response]) + let sideEffectRan = false + ctx.tools.register({ + name: 'side_effect', + description: 'probe', + parameters: { type: 'object', properties: {} }, + execute(): Promise { + sideEffectRan = true + return Promise.resolve([{ type: 'text', text: 'ran' }]) + }, + }) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) + // Registered after the child and prepended: this listener returns allow + // after every downstream pre-execute decision. The service-owned guard + // runs after the waterfall and can only deny, so the body still cannot run. + ctx.on('tools/pre-execute', async (_exec, next) => { + await next() + return { kind: 'allow' as const } + }, { prepend: true }) + + const result = await run.result + expect(result.structured).toEqual({ answer: 5 }) + expect(sideEffectRan).toBe(false) + const child = ctx.agents.get(run.id) + const sideEffectResult = child?.session.events.find(event => + event.type === 'tool/result' && event.data.callId === 'c2') + expect(sideEffectResult?.type === 'tool/result' && sideEffectResult.data.isError).toBe(true) + await run.dispose() + }) + it('leaves tool calls that PRECEDE the capture in the same response untouched', async () => { const response = [ { type: 'block-start', index: 0, blockType: 'tool-call' }, @@ -140,7 +200,7 @@ describe('in-process structured output', () => { return Promise.resolve([{ type: 'text', text: 'ran' }]) }, }) - const run = ctx.subagents.start('spawn', structuredRequest(parent)) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) const result = await run.result // The call ran BEFORE captured was set: the deny gate only guards the // window after the terminal answer landed. @@ -149,57 +209,65 @@ describe('in-process structured output', () => { await run.dispose() }) - it('snapshots the schema at start(): caller mutation after start cannot drift enforcement', async () => { - const mutable: StructuredOutputSchema = { - type: 'object', - properties: { answer: { type: 'number' } }, - required: ['answer'], - additionalProperties: false, - } - const pristine = structuredClone(mutable) + it('a later-prepended continuation wrapper cannot resurrect a captured turn', async () => { const { ctx, parent, adapter } = await setup([ - toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 3 }), + toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 7 }), + textResponse('MUST NOT BE CONSUMED'), ]) - const run = ctx.subagents.start('spawn', structuredRequest(parent, { outputSchema: mutable })) - // Mutate the caller's object AFTER start() returned but before the child's - // first request assembles: with a live reference this would reach both the - // model-visible parameters and validateStructuredValue. - ;(mutable.properties as Record).answer = { type: 'string' } + ctx.on('agent/turn-continuation', () => Promise.resolve({ action: 'stop' })) + let wrapperInstalled = false + // Register before the ready-only start. The child session-start boundary is + // after unpublished setup attached structured output but before the loop + // can run. The wrapper awaits the + // explicit downstream stop above, then overwrites that result with continue. + // The later terminal checkpoint still wins. + ctx.on('agent/session-start', (child) => { + if (child === parent) return + wrapperInstalled = true + child.ctx.on('agent/turn-continuation', async (_subject, _turn, _decision, next): Promise => { + const downstream = await next() + expect(downstream).toEqual({ action: 'stop' }) + return { action: 'continue' } + }, { prepend: true }) + }) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) const result = await run.result - expect(result.structured).toEqual({ answer: 3 }) - // The child's request carried the PRISTINE schema, not the mutated one. - const childRequest = adapter.requests.at(-1) - const captureTool = (childRequest?.tools ?? []).find(tool => tool.name === STRUCTURED_OUTPUT_TOOL) - expect(captureTool?.parameters).toEqual(pristine) + expect(wrapperInstalled).toBe(true) + expect(result.structured).toEqual({ answer: 7 }) + expect(result.stopReason).toBe('completed') + expect(adapter.requests).toHaveLength(1) await run.dispose() }) - it('the captured-turn veto is prepend: an EARLIER force-continue listener cannot short-circuit it', async () => { - const ctx = new Context() - await ctx.plugin(SystemPrompt) - await ctx.plugin(ToolRegistry) - // Registered BEFORE the structured runtime exists — without prepend, this - // goal-style listener would decide the turn first (returning WITHOUT - // calling next()) and the veto would never run. - ctx.on('agent/turn-continuation', () => Promise.resolve({ action: 'continue' })) - const acquisition = acquireStructuredRuntime(ctx) - const agent = { id: AgentId('structured-child') } as unknown as Agent - acquisition.attach(agent, SCHEMA) - const captured = await ctx.tools.execute({ - callId: 'call-1' as never, - name: STRUCTURED_OUTPUT_TOOL, - arguments: { answer: 1 }, - agent, + it('a continuation wrapper cannot carry steering past a captured terminal stop', async () => { + const { ctx, parent, adapter } = await setup([ + toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 9 }), + textResponse('MUST NOT BE CONSUMED'), + ]) + // The downstream ordinary policy says stop. A wrapper registered after + // start() delegates to that stop, then queues steering; ordinary folding + // would turn the stop back into continue. The terminal checkpoint runs + // afterwards and discards that steering. + ctx.on('agent/turn-continuation', () => Promise.resolve({ action: 'stop' })) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) + ctx.on('agent/session-start', (child) => { + if (child.id !== run.id) return + child.ctx.on('agent/turn-continuation', async (subject, _turn, _decision, next): Promise => { + const downstream = await next() + expect(downstream).toEqual({ action: 'stop' }) + subject.steer([{ type: 'text', text: 'late steering after downstream stop' }]) + return downstream + }, { prepend: true }) }) - expect(captured.isError).toBeFalsy() - const decision = await ctx.waterfall( - 'agent/turn-continuation', agent, 1, - { action: 'continue' }, - () => Promise.resolve({ action: 'continue' }), - ) - expect(decision).toEqual({ action: 'stop' }) - acquisition.detach(agent) - acquisition.release() + + const result = await run.result + const child = ctx.agents.get(run.id) + + expect(result.structured).toEqual({ answer: 9 }) + expect(adapter.requests).toHaveLength(1) + expect(child?.session.events.filter(event => event.type === 'turn/start')).toHaveLength(1) + expect(child?.session.events.filter(event => event.type === 'steering/message')).toHaveLength(0) + await run.dispose() }) it('an invalid call gets an INVALID_ARGS isError result and the model retries in-turn', async () => { @@ -207,7 +275,7 @@ describe('in-process structured output', () => { toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 'not-a-number' }), toolCallResponse('c2', STRUCTURED_OUTPUT_TOOL, { answer: 7 }), ]) - const run = ctx.subagents.start('spawn', structuredRequest(parent)) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) const result = await run.result expect(result.structured).toEqual({ answer: 7 }) expect(result.stopReason).toBe('completed') @@ -224,7 +292,7 @@ describe('in-process structured output', () => { textResponse('here is my answer in prose'), textResponse('MUST NOT BE CONSUMED'), ]) - const run = ctx.subagents.start('spawn', structuredRequest(parent)) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) const result = await run.result expect(result.stopReason).toBe('error') expect(result.structured).toBeUndefined() @@ -238,7 +306,7 @@ describe('in-process structured output', () => { it('an errored child keeps its honest error result (no capture expected)', async () => { // Script exhaustion on the first call → the child turn errors. const { ctx, parent, adapter } = await setup([]) - const run = ctx.subagents.start('spawn', structuredRequest(parent)) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) const result = await run.result expect(result.stopReason).toBe('error') expect(adapter.requests.length).toBe(1) @@ -247,12 +315,13 @@ describe('in-process structured output', () => { it('a cancel landing after a clean capture-less turn settles aborted, not error', async () => { const { ctx, parent } = await setup([textResponse('prose, no capture')]) - const run = ctx.subagents.start('spawn', structuredRequest(parent)) - const child = ctx.agents.get(run.id)! + const controller = new AbortController() + const run = await ctx.subagents.start('spawn', structuredRequest(parent, { signal: controller.signal })) // Cancel synchronously inside the turn's end recording: the cancel // contract outranks the schema shortfall, so the result maps to aborted. ctx.on('session/event', (session, event) => { - if (session === child.session && event.type === 'turn/end') run.cancel('cancelled at turn end') + const child = ctx.agents.get(run.id) + if (session === child?.session && event.type === 'turn/end') controller.abort('cancelled at turn end') }) const result = await run.result expect(result.stopReason).toBe('aborted') @@ -261,20 +330,18 @@ describe('in-process structured output', () => { it('rejects a schema outside the subset loud, before any child exists', async () => { const { ctx, parent } = await setup([]) - expect(() => ctx.subagents.start('spawn', structuredRequest(parent, { + await expect(ctx.subagents.start('spawn', structuredRequest(parent, { outputSchema: { type: 'object', oneOf: [] } as unknown as StructuredOutputSchema, - }))).toThrow(/unsupported output schema/) + }))).rejects.toThrow(/unsupported output schema/) expect(ctx.agents.get(AgentId('parent'))).toBeDefined() }) - it('a schema carrying non-JSON values fails as OutputSchemaError, never as a raw clone error', async () => { + it('a schema carrying non-JSON values fails as OutputSchemaError at the validation boundary', async () => { const { ctx, parent } = await setup([]) - // Assertion runs BEFORE the defensive structuredClone: a function-valued - // annotation must surface as the subset violation it is, not escape as - // structuredClone's DataCloneError. - expect(() => ctx.subagents.start('spawn', structuredRequest(parent, { + // Semantic assertion runs before provider startup. + await expect(ctx.subagents.start('spawn', structuredRequest(parent, { outputSchema: { type: 'object', default: () => {} } as unknown as StructuredOutputSchema, - }))).toThrow(/unsupported output schema.*annotation must be JSON data/) + }))).rejects.toThrow(/unsupported output schema.*annotation must be JSON data/) }) it('a post-execute BLOCK on the capture call denies the capture: log and result agree on failure', async () => { @@ -282,15 +349,15 @@ describe('in-process structured output', () => { toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 7 }), textResponse('continues after the blocked capture'), ]) - // A PostToolUse-style hook, registered AFTER the runtime (so the runtime's - // prepend commit listener stays outermost and composes this verdict). + // A PostToolUse-style hook turns the tool body's provisional success into + // the authoritative final error observed by the commit notification. ctx.on('tools/post-execute', (exec, _result, next) => { if (exec.name === STRUCTURED_OUTPUT_TOOL) { return Promise.resolve({ kind: 'block' as const, feedback: [{ type: 'text' as const, text: 'capture rejected by hook' }] }) } return next() }) - const run = ctx.subagents.start('spawn', structuredRequest(parent)) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) const result = await run.result // No capture was committed: the run reports the schema shortfall... expect(result.structured).toBeUndefined() @@ -316,21 +383,46 @@ describe('in-process structured output', () => { } return next() }) - const run = ctx.subagents.start('spawn', structuredRequest(parent)) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) const result = await run.result expect(result.stopReason).toBe('completed') expect(result.structured).toEqual({ answer: 8 }) await run.dispose() }) + it('commits only after a later prepended post-execute wrapper returns the authoritative result', async () => { + const { ctx, parent } = await setup([ + toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 8 }), + textResponse('capture was rejected'), + ]) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) + // Registered after attachment and prepended, so it wraps every listener + // the child installed. It delegates first, then converts the apparent + // capture success into the pipeline's authoritative failure. + ctx.on('tools/post-execute', async (exec, _result, next) => { + const downstream = await next() + if (exec.name !== STRUCTURED_OUTPUT_TOOL) return downstream + return { kind: 'block' as const, feedback: [{ type: 'text' as const, text: 'rejected after downstream' }] } + }, { prepend: true }) + + const result = await run.result + expect(result.structured).toBeUndefined() + expect(result.stopReason).toBe('error') + const child = ctx.agents.get(run.id) + const captureResult = child?.session.events.find(event => + event.type === 'tool/result' && event.data.callId === 'c1') + expect(captureResult?.type === 'tool/result' && captureResult.data.isError).toBe(true) + await run.dispose() + }) + it('appends the structured instruction to the child REQUEST\'s system text (base prompt preserved)', async () => { const { ctx, parent, adapter } = await setup([toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 1 })]) // A context-wide section stands in for the deployment persona: the - // instruction must APPEND to whatever the prompt pipeline assembled, not - // replace it (AgentOptions has no prompt field — the instruction is - // per-request wire state added by the final-request listener). + // instruction must APPEND to the other scoped and global sections, not + // replace them (AgentOptions has no prompt field — the instruction is an + // ordinary child-scoped prompt registration). ctx.systemPrompt.section({ name: 'test:persona', order: 10, text: 'You are a counter.' }) - const run = ctx.subagents.start('spawn', structuredRequest(parent)) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) await run.result const childRequest = adapter.requests.at(-1)! expect(childRequest.system).toContain('You are a counter.') @@ -339,6 +431,84 @@ describe('in-process structured output', () => { await run.dispose() }) + it('keeps pure Code Mode at one wire tool and exposes structured capture through the SDK only', async () => { + const { ctx, parent, adapter } = await setup([ + toolCallResponse('c1', RUN_CODE_NAME, { code: 'return await tools.structured_output({ answer: 12 })' }), + ], { + toolMode: 'code', + codeRun: async (request) => { + const capture = request.bindings.at(0)?.functions[STRUCTURED_OUTPUT_TOOL] + if (!capture) throw new Error('structured_output binding missing') + await capture({ answer: 12 }) + return { logs: [], value: 'captured' } + }, + }) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) + + const result = await run.result + expect(result.structured).toEqual({ answer: 12 }) + const request = adapter.requests[0]! + expect(toolNames(request)).toEqual([RUN_CODE_NAME]) + expect(request.system).toContain('declare const tools:') + expect(request.system).toContain('structured_output(args:') + expect(request.system).toContain(STRUCTURED_OUTPUT_INSTRUCTION) + await run.dispose() + }) + + it('discards a nested capture when the enclosing run_code execution fails', async () => { + const { ctx, parent, adapter } = await setup([ + toolCallResponse('c1', RUN_CODE_NAME, { code: 'await tools.structured_output({ answer: 12 }); throw new Error("boom")' }), + textResponse('outer code failed'), + ], { + toolMode: 'code', + codeRun: async (request) => { + const capture = request.bindings.at(0)?.functions[STRUCTURED_OUTPUT_TOOL] + if (!capture) throw new Error('structured_output binding missing') + await capture({ answer: 12 }) + return { + logs: [], + error: { kind: 'runtime', message: 'boom after capture' }, + } as never + }, + }) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) + + const result = await run.result + expect(result.structured).toBeUndefined() + expect(result.stopReason).toBe('error') + expect(adapter.requests).toHaveLength(2) + const child = ctx.agents.get(run.id)! + const outer = child.session.events.find(event => + event.type === 'tool/result' && event.data.callId === CallId('c1')) + expect(outer?.type === 'tool/result' && outer.data.isError).toBe(true) + await run.dispose() + }) + + it('discards a nested capture when post-policy blocks the enclosing run_code result', async () => { + const { ctx, parent, adapter } = await setup([ + toolCallResponse('c1', RUN_CODE_NAME, { code: 'return await tools.structured_output({ answer: 12 })' }), + textResponse('outer code was blocked'), + ], { + toolMode: 'code', + codeRun: async (request) => { + const capture = request.bindings.at(0)?.functions[STRUCTURED_OUTPUT_TOOL] + if (!capture) throw new Error('structured_output binding missing') + await capture({ answer: 12 }) + return { logs: [], value: 'captured' } + }, + }) + ctx.on('tools/post-execute', (exec, _result, next) => exec.name === RUN_CODE_NAME + ? Promise.resolve({ kind: 'block' as const, feedback: [{ type: 'text' as const, text: 'outer blocked' }] }) + : next()) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) + + const result = await run.result + expect(result.structured).toBeUndefined() + expect(result.stopReason).toBe('error') + expect(adapter.requests).toHaveLength(2) + await run.dispose() + }) + it('the instruction rides ONLY structured requests: appended for the child, absent for a plain agent', async () => { const { ctx, parent, adapter } = await setup([ textResponse('parent answer'), @@ -347,7 +517,7 @@ describe('in-process structured output', () => { parent.send([{ type: 'text', text: 'hello' }]) await parent.whenIdle() expect(adapter.requests[0]!.system ?? '').not.toContain(STRUCTURED_OUTPUT_INSTRUCTION) - const run = ctx.subagents.start('spawn', structuredRequest(parent)) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) await run.result // The loop always assembles a base prompt (the harness identity section), // so the instruction APPENDS — never replaces. @@ -357,20 +527,14 @@ describe('in-process structured output', () => { await run.dispose() }) - describe('final-request enforcement (the prepend agent/request listener)', () => { - it('a plain agent assembling while the runtime is LIVE gets the placeholder stripped', async () => { - // Run-scoped acquisition means a plain deployment never registers the - // tool at all; the strip branch exists for the CONCURRENT case — a plain - // agent taking a turn while some structured child holds the runtime open. + describe('scoped registration (each child owns its capture tool)', () => { + it('a plain agent never sees the tool: nothing is registered globally at all', async () => { const { ctx, parent, adapter } = await setup([textResponse('parent answer')]) - const hold = acquireStructuredRuntime(ctx) parent.send([{ type: 'text', text: 'hello' }]) await parent.whenIdle() - // The placeholder IS in the registry during this turn; the assembly the - // loop rendered must not carry it for an agent without a structured run. - expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL)).toBeDefined() + // Scoped registration: the global view has no capture tool, ever. + expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL)).toBeUndefined() expect(toolNames(adapter.requests[0]!)).not.toContain(STRUCTURED_OUTPUT_TOOL) - hold.release() }) it('a structured child sees structured_output with ITS schema; a plain agent never sees the tool', async () => { @@ -384,7 +548,7 @@ describe('in-process structured output', () => { await parent.whenIdle() expect(toolNames(adapter.requests[0]!)).not.toContain(STRUCTURED_OUTPUT_TOOL) - const run = ctx.subagents.start('spawn', structuredRequest(parent)) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) await run.result const childRequest = adapter.requests[1]! expect(toolNames(childRequest)).toContain(STRUCTURED_OUTPUT_TOOL) @@ -417,8 +581,8 @@ describe('in-process structured output', () => { return toolCallResponse('c2', STRUCTURED_OUTPUT_TOOL, args) }, ]) - const runA = ctx.subagents.start('spawn', structuredRequest(parent)) - const runB = ctx.subagents.start('spawn', structuredRequest(parent, { outputSchema: otherSchema })) + const runA = await ctx.subagents.start('spawn', structuredRequest(parent)) + const runB = await ctx.subagents.start('spawn', structuredRequest(parent, { outputSchema: otherSchema })) const [a, b] = await Promise.all([runA.result, runB.result]) expect(a.structured).toEqual({ answer: 1 }) expect(b.structured).toEqual({ verdict: 'real' }) @@ -430,157 +594,62 @@ describe('in-process structured output', () => { await runB.dispose() }) - it('wins against a downstream listener that REPLACES the assembly object', async () => { + it('places the capture tool and instruction in their canonical orders', async () => { const { ctx, parent, adapter } = await setup([ - toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 5 }), + toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 7 }), ]) - // A downstream (non-prepend) listener that returns a brand-new assembly — - // the composition caveat that erases cooperative mutations. Registered - // AFTER the runtime's prepend listener, so it runs INSIDE it. - ctx.on('system-prompt/assemble', async (_assembly, _context, next) => { - const replaced = await next() - return { sections: [...replaced.sections], tools: [...replaced.tools], variables: { ...replaced.variables } } + // A global tool sorts lexicographically after structured_output, while a + // global section above the 190 band follows the capture instruction. + ctx.tools.register({ + name: 'zz_probe', + description: 'probe', + parameters: { type: 'object', properties: {} }, + execute: () => Promise.resolve([{ type: 'text', text: 'x' }]), }) - const run = ctx.subagents.start('spawn', structuredRequest(parent)) - const result = await run.result - expect(result.structured).toEqual({ answer: 5 }) - const entry = adapter.requests[0]!.tools!.find(tool => tool.name === STRUCTURED_OUTPUT_TOOL) - expect(entry).toBeDefined() - expect(entry!.parameters).toEqual(SCHEMA) + ctx.systemPrompt.section({ name: 'after-band', order: 200, text: 'AFTER-BAND' }) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) + await run.result + const request = adapter.requests[0]! + const names = toolNames(request) + expect(names.indexOf(STRUCTURED_OUTPUT_TOOL)).toBeGreaterThanOrEqual(0) + expect(names.indexOf(STRUCTURED_OUTPUT_TOOL)).toBeLessThan(names.indexOf('zz_probe')) + const system = request.system ?? '' + const instructionAt = system.indexOf(STRUCTURED_OUTPUT_INSTRUCTION) + expect(instructionAt).toBeGreaterThanOrEqual(0) + expect(system.indexOf('AFTER-BAND')).toBeGreaterThan(instructionAt) await run.dispose() }) it('a non-structured agent request keeps tools ABSENT when it had none (no tools: [] materialized)', async () => { - const { parent, adapter } = await setup([ - // The registry contributes the placeholder via prompt assembly, so - // tools is an array in the raw request — but after stripping the - // placeholder (its ONLY entry), the field must not be re-added as a - // different shape. - textResponse('plain'), - ]) + const { parent, adapter } = await setup([textResponse('plain')]) parent.send([{ type: 'text', text: 'q' }]) await parent.whenIdle() const request = adapter.requests[0]! - expect(toolNames(request)).not.toContain(STRUCTURED_OUTPUT_TOOL) + expect(request.tools).toBeUndefined() await new Promise(resolve => setTimeout(resolve, 0)) }) - it('shapes a bare assembly on the waterfall: no-agent context strips the placeholder; a structured agent gains schema + trailing instruction section', async () => { - // Drive ctx.systemPrompt.assemble directly — the enforcement listener - // must tolerate a context with NO agent (a bare diagnostic assemble) - // and shape a structured agent's assembly on the same path the loop - // renders and logs as the request header. - const { ctx, parent } = await setup([]) - const acquisition = acquireStructuredRuntime(ctx) - // Bare assemble WHILE the runtime is live: the no-agent branch must - // strip the registered placeholder (before the acquisition there is - // nothing to strip — run-scoped registration). - const bare = await ctx.systemPrompt.assemble({}) - expect(bare.tools.map(tool => tool.name)).not.toContain(STRUCTURED_OUTPUT_TOOL) - - acquisition.attach(parent, SCHEMA) - const shaped = await ctx.systemPrompt.assemble({ agent: parent }) - expect(shaped.tools.map(tool => tool.name)).toContain(STRUCTURED_OUTPUT_TOOL) - expect(shaped.tools.find(tool => tool.name === STRUCTURED_OUTPUT_TOOL)!.parameters).toEqual(SCHEMA) - // The demand travels with the tool: the instruction renders LAST - // (appended post-next(); renderPrompt joins in array order). - expect(shaped.sections.at(-1)).toMatchObject({ name: `tool:${STRUCTURED_OUTPUT_TOOL}`, text: STRUCTURED_OUTPUT_INSTRUCTION }) - acquisition.detach(parent) - acquisition.release() - }) - }) - - describe('runtime lifetime (refcount: live structured runs)', () => { - it('the runtime exists exactly while structured runs are live: nothing before, nothing after', async () => { - const { ctx, parent } = await setup([ + it('registrations ride the child fiber: disposing the run removes them; a provider reload mid-run cannot', async () => { + const { ctx, parent, disposeProvider } = await setup([ toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 4 }), ]) - // No always-on global state: a context that has run no structured child - // carries no capture tool. expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL)).toBeUndefined() - const run = ctx.subagents.start('spawn', structuredRequest(parent)) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) + // A backend hot-reload mid-run must not unregister the capture tool out + // from under the live child: the registration rides the CHILD's fiber. + disposeProvider() const result = await run.result - // The capture succeeded — the registrations existed while the run lived. expect(result.structured).toEqual({ answer: 4 }) - // The run's settle released the last acquisition. - expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL)).toBeUndefined() + const child = ctx.agents.get(run.id)! + expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL, child)).toBeDefined() await run.dispose() - }) - - it('concurrent structured runs share one runtime; the last settle disposes it', async () => { - const { ctx, parent } = await setup([ - toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 1 }), - toolCallResponse('c2', STRUCTURED_OUTPUT_TOOL, { answer: 2 }), - ]) - const first = ctx.subagents.start('spawn', structuredRequest(parent)) - const second = ctx.subagents.start('spawn', structuredRequest(parent)) - const [a, b] = await Promise.all([first.result, second.result]) - expect([a.structured, b.structured].sort()).toEqual([{ answer: 1 }, { answer: 2 }].sort()) - expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL)).toBeUndefined() - await first.dispose() - await second.dispose() - }) - - it('acquisition release is idempotent (double release cannot underflow the refcount)', async () => { - const ctx = new Context() - await ctx.plugin(SystemPrompt) - await ctx.plugin(ToolRegistry) - const first = acquireStructuredRuntime(ctx) - const second = acquireStructuredRuntime(ctx) - first.release() - first.release() - // The second holder still keeps the tool registered. - expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL)).toBeDefined() - second.release() - expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL)).toBeUndefined() - }) - - it('registers the capture tool through the scoped fiber when tools loads after the acquisition', async () => { - // The Loader starts sibling plugins concurrently, so a backend can - // acquire the runtime before dsh-tools has applied. The capture tool - // must then register as soon as `tools` exists — via the inject fiber, - // not by deferring the backend (which would reorder the prompt's tools). - const ctx = new Context() - const acquisition = acquireStructuredRuntime(ctx) - await ctx.plugin(SystemPrompt) - await ctx.plugin(ToolRegistry) - // Fiber activation completes asynchronously after the service appears. - await new Promise(resolve => setImmediate(resolve)) - expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL)).toBeDefined() - acquisition.release() - expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL)).toBeUndefined() - }) - - it('releasing before tools ever loads disposes the pending fiber without registering', async () => { - const ctx = new Context() - const acquisition = acquireStructuredRuntime(ctx) - acquisition.release() - await ctx.plugin(SystemPrompt) - await ctx.plugin(ToolRegistry) - await new Promise(resolve => setImmediate(resolve)) - // The disposed fiber never fires: nothing registers after the fact. - expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL)).toBeUndefined() - }) - - it('attach/captured/detach manage per-agent state through the acquisition surface', async () => { - const { ctx, parent } = await setup([]) - const acquisition = acquireStructuredRuntime(ctx) - expect(acquisition.captured(parent)).toBeUndefined() - acquisition.attach(parent, SCHEMA) - expect(acquisition.captured(parent)).toBeUndefined() - acquisition.detach(parent) - acquisition.detach(parent) - acquisition.release() - // That manual acquisition was the ONLY holder - release disposes. - expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL)).toBeUndefined() + // Child disposed ⇒ its scoped registrations are gone. + expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL, child)).toBeUndefined() }) }) - it('a direct structured_output call from an agent WITHOUT a structured run is an isError', async () => { + it('a structured_output call from an agent WITHOUT a structured run is UNKNOWN_TOOL (the tool does not exist for it)', async () => { const { ctx, parent } = await setup([]) - // Hold the runtime open (run-scoped: nothing is registered otherwise) so - // the call reaches the capture tool's own fail-loud guard, not UNKNOWN_TOOL. - const hold = acquireStructuredRuntime(ctx) const result = await ctx.tools.execute({ callId: 'x' as never, name: STRUCTURED_OUTPUT_TOOL, @@ -588,19 +657,140 @@ describe('in-process structured output', () => { agent: parent, }) expect(result.isError).toBe(true) - expect(JSON.stringify(result.content)).toContain('only available to subagents') - hold.release() + expect(result.error?.code).toBe('UNKNOWN_TOOL') }) - it('a structured_output call with NO calling agent at all is an isError', async () => { + it('a structured_output call with NO calling agent at all is UNKNOWN_TOOL', async () => { const { ctx } = await setup([]) - const hold = acquireStructuredRuntime(ctx) const result = await ctx.tools.execute({ callId: 'x' as never, name: STRUCTURED_OUTPUT_TOOL, arguments: { answer: 1 }, }) expect(result.isError).toBe(true) - hold.release() + expect(result.error?.code).toBe('UNKNOWN_TOOL') + }) + + it('a failed execution stage is discarded and never promoted by a later call', async () => { + const { ctx, parent } = await setup([ + toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 1 }), + ]) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) + // A prepended post-execute listener blocks the first capture without + // delegating. The final-result notification discards that execution's + // stage when it observes the error. + let blocks = 1 + ctx.on('tools/post-execute', (exec, _result, next) => { + if (exec.name === STRUCTURED_OUTPUT_TOOL && blocks > 0) { + blocks -= 1 + return Promise.resolve({ kind: 'block' as const, feedback: [{ type: 'text' as const, text: 'rejected' }] }) + } + return next() + }, { prepend: true }) + const result = await run.result + const child = ctx.agents.get(run.id)! + // The blocked capture must NOT surface as structured success… + expect(result.stopReason).toBe('error') + expect(result.structured).toBeUndefined() + // …and a LATER invalid call (its own body staged nothing) must not + // resurrect c1's discarded value: drive the pipeline directly. + const invalid = await ctx.tools.execute({ + callId: 'c2' as never, + name: STRUCTURED_OUTPUT_TOOL, + arguments: { answer: 'not-a-number' }, + agent: child, + }) + expect(invalid.isError).toBe(true) + // A fresh valid call still captures ITS OWN value. + const valid = await ctx.tools.execute({ + callId: 'c3' as never, + name: STRUCTURED_OUTPUT_TOOL, + arguments: { answer: 9 }, + agent: child, + }) + expect(valid.isError).toBeFalsy() + await run.dispose() + }) + + it('reusing a failed execution\'s call id never promotes its discarded stage', async () => { + const { ctx, parent } = await setup([ + toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 1 }), + ]) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) + // Block the first capture after its body stages a value. Its final error + // discards that execution's stage. + let blocks = 1 + ctx.on('tools/post-execute', (exec, _result, next) => { + if (exec.name === STRUCTURED_OUTPUT_TOOL && blocks > 0) { + blocks -= 1 + return Promise.resolve({ kind: 'block' as const, feedback: [{ type: 'text' as const, text: 'rejected' }] }) + } + return next() + }, { prepend: true }) + await run.result + const child = ctx.agents.get(run.id)! + // A SECOND capture call with the SAME call id whose body never stages + // (invalid args throw before the stage): the discarded value must not ride + // its acceptance. + const reused = await ctx.tools.execute({ + callId: 'c1' as never, + name: STRUCTURED_OUTPUT_TOOL, + arguments: { answer: 'not-a-number' }, + agent: child, + }) + expect(reused.isError).toBe(true) + // Nothing was ever committed: a fresh valid call is still required. + const valid = await ctx.tools.execute({ + callId: 'c1' as never, + name: STRUCTURED_OUTPUT_TOOL, + arguments: { answer: 5 }, + agent: child, + }) + expect(valid.isError).toBeFalsy() + await run.dispose() + }) + + it('a pre-execute deny with call-id reuse cannot promote another execution\'s stage', async () => { + const { ctx, parent } = await setup([ + toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 1 }), + ]) + const run = await ctx.subagents.start('spawn', structuredRequest(parent)) + // Discard the first capture's stage via a final post-execute block. + let blocks = 1 + ctx.on('tools/post-execute', (exec, _result, next) => { + if (exec.name === STRUCTURED_OUTPUT_TOOL && blocks > 0) { + blocks -= 1 + return Promise.resolve({ kind: 'block' as const, feedback: [{ type: 'text' as const, text: 'rejected' }] }) + } + return next() + }, { prepend: true }) + await run.result + const child = ctx.agents.get(run.id)! + // A prepended pre-execute deny skips the body, while the denied call still + // reaches the final notification with the same adapter-minted call id. + const offDeny = ctx.on('tools/pre-execute', (exec) => { + if (exec.name === STRUCTURED_OUTPUT_TOOL) { + return Promise.resolve({ kind: 'deny' as const, reason: 'outer veto' }) + } + return undefined as never + }, { prepend: true }) + const denied = await ctx.tools.execute({ + callId: 'c1' as never, + name: STRUCTURED_OUTPUT_TOOL, + arguments: { answer: 2 }, + agent: child, + }) + expect(denied.isError).toBe(true) + offDeny() + // The discarded value was never promoted: a fresh valid call is required + // (and succeeds, proving the runtime is not wedged). + const valid = await ctx.tools.execute({ + callId: 'c1' as never, + name: STRUCTURED_OUTPUT_TOOL, + arguments: { answer: 5 }, + agent: child, + }) + expect(valid.isError).toBeFalsy() + await run.dispose() }) }) diff --git a/packages/subagent/subagent-inprocess/tests/subagent-inprocess.spec.ts b/packages/subagent/subagent-inprocess/tests/subagent-inprocess.spec.ts index 7219e03988..0005246523 100644 --- a/packages/subagent/subagent-inprocess/tests/subagent-inprocess.spec.ts +++ b/packages/subagent/subagent-inprocess/tests/subagent-inprocess.spec.ts @@ -13,13 +13,6 @@ import { depthOf, SubagentDepthError, startInProcessRun } from '../src/index.ts' type Script = ConstructorParameters[0] -/** - * Drives the shared in-process run driver DIRECTLY (no provider package), so the - * driver's own contract — depth read/cap, the one-shot drive, the result read — - * is covered independently of which backend (spawn/fork) calls it. The only - * mocked boundary is the model; the real agent loop, SubagentService, and - * dsh-invariants are mounted, so a malformed child session log fails the test. - */ async function setup(script: Script) { const ctx = new Context() await ctx.plugin(LlmService) @@ -35,51 +28,127 @@ async function setup(script: Script) { return { ctx, parent } } -function text(blocks: { type: string; text?: string }[]): string { - return blocks.filter(b => b.type === 'text').map(b => b.text).join('') +function request(parent: Agent, signal = new AbortController().signal) { + return { prompt: [{ type: 'text' as const, text: 'child task' }], parent, signal } +} + +function text(blocks: readonly { type: string; text?: string }[]): string { + return blocks.filter(block => block.type === 'text').map(block => block.text).join('') } describe('depthOf', () => { - it('reads 0 for an agent with no subagentDepth, the set value otherwise', async () => { + it('reads zero for a top-level agent and an explicit child depth', async () => { const { parent } = await setup([]) expect(depthOf(parent)).toBe(0) - const withDepth = { options: { subagentDepth: 3 } } as unknown as Agent - expect(depthOf(withDepth)).toBe(3) + expect(depthOf({ options: { subagentDepth: 3 } } as unknown as Agent)).toBe(3) + }) + + it.each([Number.NaN, 1.5, -1, -0, Number.MAX_SAFE_INTEGER + 1])('rejects malformed depth %s', (value) => { + expect(() => depthOf({ options: { subagentDepth: value } } as unknown as Agent)) + .toThrow('non-negative safe integer') }) }) describe('startInProcessRun', () => { - it('drives a fresh child (no seed) to completion and returns its output', async () => { - const { ctx, parent } = await setup([textResponse('driver child answer')]) - const run = startInProcessRun(ctx, { prompt: [{ type: 'text', text: 'do X' }], parent }, { providerName: 'spawn' }) + it('returns only after publication, drives a fresh child, and disposes it', async () => { + const { ctx, parent } = await setup([textResponse('driver answer')]) + const run = await startInProcessRun(request(parent), {}) + expect(ctx.agents.get(run.id)).toBeDefined() const result = await run.result expect(result.stopReason).toBe('completed') - expect(text(result.output)).toBe('driver child answer') + expect(text(result.output)).toBe('driver answer') expect(depthOf(ctx.agents.get(run.id)!)).toBe(1) await run.dispose() + await run.dispose() + expect(ctx.agents.get(run.id)).toBeUndefined() }) - it('throws SubagentDepthError when the child would exceed maxDepth', async () => { - const { ctx, parent } = await setup([]) - expect(() => startInProcessRun(ctx, { prompt: [{ type: 'text', text: 'p' }], parent, maxDepth: 0 }, { providerName: 'spawn' })) - .toThrow(SubagentDepthError) - }) - - it('seeds the child session when a seed is supplied', async () => { - // Drive the parent through one real turn, then seed the child with that - // completed-turn prefix — the child must SEE the parent's history but its - // result is scoped to its OWN events (not the seeded parent message). - const { ctx, parent } = await setup([textResponse('parent turn'), textResponse('seeded child reply')]) - parent.send([{ type: 'text', text: 'parent q' }]) + it('seeds a forked child but reads only the child-owned output', async () => { + const { ctx, parent } = await setup([textResponse('parent answer'), textResponse('child answer')]) + parent.send([{ type: 'text', text: 'parent question' }]) await parent.whenIdle() const seed = parent.session.events.slice() - const run = startInProcessRun(ctx, { prompt: [{ type: 'text', text: 'child q' }], parent }, { providerName: 'fork', seed }) + const run = await startInProcessRun(request(parent), { seed }) const result = await run.result - expect(result.stopReason).toBe('completed') - expect(text(result.output)).toBe('seeded child reply') + expect(text(result.output)).toBe('child answer') const child = ctx.agents.get(run.id)! - // The child inherited the parent's prefix. - expect(child.session.events.slice(0, seed.length).some(e => e.type === 'user/message')).toBe(true) + expect(child.session.header.seedLength).toBe(seed.length) + expect(child.session.events.slice(0, seed.length)).toEqual(seed) await run.dispose() }) + + it('rejects invalid and exceeded depth before publication', async () => { + const { parent } = await setup([]) + await expect(startInProcessRun({ ...request(parent), maxDepth: -1 }, {})) + .rejects.toThrow('non-negative safe integer') + await expect(startInProcessRun({ ...request(parent), maxDepth: 0 }, {})) + .rejects.toBeInstanceOf(SubagentDepthError) + const maxParent = { options: { subagentDepth: Number.MAX_SAFE_INTEGER } } as unknown as Agent + await expect(startInProcessRun(request(maxParent), {})).rejects.toBeInstanceOf(RangeError) + }) + + it('rejects an already-aborted request without publishing a child', async () => { + const { ctx, parent } = await setup([]) + const beforeAgents = ctx.agents.list().length + const beforeSessions = ctx.sessions.list().length + const controller = new AbortController() + controller.abort('too late') + await expect(startInProcessRun(request(parent, controller.signal), {})) + .rejects.toThrow('aborted before child publication') + expect(ctx.agents.list()).toHaveLength(beforeAgents) + expect(ctx.sessions.list()).toHaveLength(beforeSessions) + }) + + it('uses the request signal after publication and dispose as cancellation paths', async () => { + const { parent } = await setup(['hang', 'hang']) + const controller = new AbortController() + const signalled = await startInProcessRun(request(parent, controller.signal), {}) + await new Promise(resolve => setTimeout(resolve, 30)) + controller.abort('stop child') + await expect(signalled.result).resolves.toMatchObject({ stopReason: 'aborted' }) + await signalled.dispose() + + const disposed = await startInProcessRun(request(parent), {}) + await new Promise(resolve => setTimeout(resolve, 30)) + await disposed.dispose() + await expect(disposed.result).resolves.toMatchObject({ stopReason: 'aborted' }) + }) + + it('cleans a failed unpublished setup before rejecting', async () => { + const { ctx, parent } = await setup([]) + const beforeAgents = ctx.agents.list().length + const beforeSessions = ctx.sessions.list().length + await expect(startInProcessRun({ + ...request(parent), + toolFilter: { deny: ['unknown-tool'] }, + }, {})).rejects.toThrow('unknown global tool') + expect(ctx.agents.list()).toHaveLength(beforeAgents) + expect(ctx.sessions.list()).toHaveLength(beforeSessions) + }) + + it('closes the abort handoff after the factory detaches its creation listener', async () => { + const { ctx, parent } = await setup([]) + const controller = new AbortController() + const beforeAgents = ctx.agents.list().length + const beforeSessions = ctx.sessions.list().length + const parentWithAbortAtHandoff = { + options: parent.options, + session: parent.session, + ctx: { + agents: { + create: async (options: Parameters[0]) => { + const handle = await ctx.agents.create(options) + // `create()` has detached its creation-only listener, but the + // provider continuation has not installed its live-run listener. + controller.abort('handoff race') + return handle + }, + }, + }, + } as unknown as Agent + await expect(startInProcessRun(request(parentWithAbortAtHandoff, controller.signal), {})) + .rejects.toThrow('aborted before child publication') + expect(ctx.agents.list()).toHaveLength(beforeAgents) + expect(ctx.sessions.list()).toHaveLength(beforeSessions) + }) }) diff --git a/packages/subagent/subagent-spawn/README.md b/packages/subagent/subagent-spawn/README.md index b976ef5a63..3c0f739ecf 100644 --- a/packages/subagent/subagent-spawn/README.md +++ b/packages/subagent/subagent-spawn/README.md @@ -1,16 +1,16 @@ # @deepseek-ai/dsh-subagent-spawn -The in-process **spawn** subagent backend: a [`SubagentProvider`](../subagent/README.md) that runs each child as a **fresh** child [`Agent`](../../core/agent) on the same cordis context (`ctx.agents`) — its own session, its own (or the parent's) model, zero inherited conversation. The cheapest transport, reusing the agent factory's quiescent [`AgentHandle`](../../core/agent) teardown. +The spawn provider creates a fresh child `Agent` in the current process. The child has its own session, sees no parent conversation history, and reuses the host's agent factory and LLM/tool services. -The run mechanics live in the shared [`@deepseek-ai/dsh-subagent-inprocess`](../subagent-inprocess/README.md) driver (`startInProcessRun`); this backend just passes **no seed** (a fresh child). The [fork](../subagent-fork/README.md) backend is an independent peer over the same driver — neither knows about the other. +## Behavior -## What it does +`start(request)` delegates to [`startInProcessRun`](../subagent-inprocess/README.md) with no seed and awaits publication before returning. The child receives parent working-directory/session lineage and inherits the parent model unless overridden, but starts with an empty conversation. -`start(request)` delegates to `startInProcessRun(ctx, request, { providerName })` with no seed: a fresh child agent with the parent's `cwd`/`parentSession` lineage and (by default) the parent's model. See the [driver README](../subagent-inprocess/README.md) for the full lifecycle (depth check, one-shot drive, result read, dispose). +The shared driver owns depth checking, persona and tool-filter setup, structured output, required-signal cancellation, one-shot execution, result reading, and quiescent disposal. A startup rejection leaves no published child; provider unload after fulfillment does not revoke the holder-owned run. ## Capabilities -`{ outputSchema: true, depthLimit: true, toolFilter: false }`. It constructs the child, so it enforces a recursion cap, and it supports structured output via the driver's [structured runtime](../subagent-inprocess/README.md) (acquired per structured run inside the driver — this backend registers nothing at apply). Tool-scoping is deferred (the service rejects a request needing it before `start` runs). +Spawn advertises `{ outputSchema: true, depthLimit: true, toolFilter: true, persona: true }` because it controls the child's creation window and can enforce all four features. ## Config diff --git a/packages/subagent/subagent-spawn/src/index.ts b/packages/subagent/subagent-spawn/src/index.ts index 2d8f118b4e..6d2d8f3a11 100644 --- a/packages/subagent/subagent-spawn/src/index.ts +++ b/packages/subagent/subagent-spawn/src/index.ts @@ -9,10 +9,10 @@ * ({@link startInProcessRun}); this backend just passes NO seed (a fresh * child). The fork backend is an independent peer over the same driver. * - * Structured output (`outputSchema`) is supported via the driver's shared - * structured runtime: the backend acquires it for its plugin lifetime (so the - * capture tool and request-shaping listeners exist before any run), and each - * structured run holds its own acquisition until it settles. + * Structured output (`outputSchema`) is supported through the driver's + * per-child scoped runtime: the child registers its real-schema capture tool, + * prompt instruction, and enforcement listeners inside the creation setup + * window, and its scope owns their lifetime. * * Plugin export shape: named `name`/`inject`/`Config`/`apply`, NO default. * @@ -25,12 +25,11 @@ import type { SubagentCapabilities, SubagentProvider, SubagentStartRequest } fro import { startInProcessRun } from '@deepseek-ai/dsh-subagent-inprocess' export const name = 'subagent-spawn' -// `tools` is deliberately NOT injected: the shared driver's structured runtime -// (acquired per structured RUN, not at apply) gates its own capture-tool -// registration on `tools` availability, so this backend's apply timing — and -// with it the provider-mirroring delegation tool's position in the -// model-visible tool list — stays what it was before structured output existed. -export const inject = ['subagents', 'agents'] +// `tools` is deliberately NOT injected: the shared driver registers structured +// output through the child's creation context, whose factory already requires +// the tool service. Keeping it out of this backend's inject list preserves the +// provider's independent apply timing. +export const inject = ['subagents'] /** Config: the registry name to register the provider under. */ export interface Config { @@ -43,26 +42,27 @@ export const Config: z = z.object({ }) /** - * The spawn provider. Supports `depthLimit` (it constructs the child, so it can - * enforce a recursion cap) and `outputSchema` (via the shared in-process - * structured runtime); NOT `toolFilter` in this cut — a request that needs it - * is rejected by the service before `start` runs. + * The spawn provider. Supports every start-time capability: `depthLimit` (it + * constructs the child, so it can enforce a recursion cap), `outputSchema` + * (the scoped structured runtime), and `toolFilter`/`persona` (scoped + * `restrict()` and a scoped shadowing persona section, applied in the child's + * creation window). */ class SpawnProvider implements SubagentProvider { - readonly capabilities: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: false } + readonly capabilities: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: true, persona: true } // Context contract: a spawned child starts fresh — it never sees the parent conversation. readonly inheritsParentContext = false - constructor(readonly name: string, private readonly ctx: Context) {} + constructor(readonly name: string) {} start(request: SubagentStartRequest) { // Fresh child: no seed. The shared driver mints ids, stamps cwd/lineage/ // depth, drives the one-shot (including the structured capture when the // request carries an outputSchema), and maps the result. - return startInProcessRun(this.ctx, request, { providerName: this.name }) + return startInProcessRun(request, {}) } } export function apply(ctx: Context, config: Config): void { - ctx.subagents.registerProvider(new SpawnProvider(config.providerName, ctx)) + ctx.subagents.registerProvider(new SpawnProvider(config.providerName)) } diff --git a/packages/subagent/subagent-spawn/tests/harness.ts b/packages/subagent/subagent-spawn/tests/harness.ts index b3e9d4ec24..e4ee2e6ab7 100644 --- a/packages/subagent/subagent-spawn/tests/harness.ts +++ b/packages/subagent/subagent-spawn/tests/harness.ts @@ -23,9 +23,10 @@ export async function spawnHarness(workdir: string): Promise { const ctx = new Context() await ctx.plugin(LlmService) await ctx.plugin(SessionStore) - // The deployment persona is context-wide (parent AND spawned children - // render it), so it stays neutral for both roles; the delegation nudge - // lives in the e2e's user prompt and the subagent tool's own description. + // This harness installs only the global default persona, so both parent and + // spawned children render it. It stays neutral for both roles; the + // delegation nudge lives in the e2e's user prompt and the subagent tool's + // own description. await ctx.plugin(SystemPrompt, { persona: 'You are a coding agent. Report only when the requested work is done.' }) await ctx.plugin(ToolRegistry) await ctx.plugin(AgentRegistry) diff --git a/packages/subagent/subagent-spawn/tests/subagent-spawn.spec.ts b/packages/subagent/subagent-spawn/tests/subagent-spawn.spec.ts index 1dad9748e9..9e4e489882 100644 --- a/packages/subagent/subagent-spawn/tests/subagent-spawn.spec.ts +++ b/packages/subagent/subagent-spawn/tests/subagent-spawn.spec.ts @@ -9,7 +9,7 @@ import AgentRegistry, { AgentId } from '@deepseek-ai/dsh-agent' import { SessionId } from '@deepseek-ai/dsh-session' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import * as Invariants from '@deepseek-ai/dsh-invariants' -import SubagentService from '@deepseek-ai/dsh-subagent' +import SubagentService, { type SubagentStartRequest } from '@deepseek-ai/dsh-subagent' import { MockAdapter, maxTokensResponse, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' import * as spawn from '../src/index.ts' import { depthOf, STRUCTURED_OUTPUT_TOOL, SubagentDepthError } from '@deepseek-ai/dsh-subagent-inprocess' @@ -44,20 +44,43 @@ function text(blocks: { type: string; text?: string }[]): string { return blocks.filter(b => b.type === 'text').map(b => b.text).join('') } +function start(ctx: Context, provider: string, request: Omit & { signal?: AbortSignal }) { + return ctx.subagents.start(provider, { signal: request.signal ?? new AbortController().signal, ...request }) +} + describe('dsh-subagent-spawn', () => { it('runs a fresh child to completion and returns its final assistant output', async () => { // One model call for the child: a plain text answer. const { ctx, parent } = await setup([textResponse('child answer')]) - const run = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'do X' }], parent }) + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'do X' }], parent }) const result = await run.result expect(result.stopReason).toBe('completed') expect(text(result.output)).toBe('child answer') await run.dispose() }) + it('emits subagent/start only after the fresh child is published', async () => { + const { ctx, parent } = await setup([textResponse('child answer')]) + let childAtStart: ReturnType + ctx.on('subagent/start', (info) => { + if (info.provider === 'spawn') childAtStart = ctx.agents.get(info.id) + }) + + const starting = start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'do X' }], parent }) + // Creation is asynchronous; no lifecycle claim is made while the child is + // still inside its unpublished setup transaction. + expect(childAtStart).toBeUndefined() + const run = await starting + expect(childAtStart).toBe(ctx.agents.get(run.id)) + expect(childAtStart?.id).toBe(run.id) + + await run.result + await run.dispose() + }) + it('gives the child its OWN session (not the parent\'s), with parentSession lineage', async () => { const { ctx, parent } = await setup([textResponse('hi')]) - const run = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) await run.result const child = ctx.agents.get(run.id)! expect(child.session.header.id).not.toBe(parent.session.header.id) @@ -73,7 +96,7 @@ describe('dsh-subagent-spawn', () => { const parentEventCount = parent.session.events.length expect(parentEventCount).toBeGreaterThan(0) - const run = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'child prompt' }], parent }) + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'child prompt' }], parent }) await run.result const child = ctx.agents.get(run.id)! // The child's first user/message is its OWN prompt, not the parent's history. @@ -84,7 +107,7 @@ describe('dsh-subagent-spawn', () => { it('disposes the child to quiescence (agent removed from the registry)', async () => { const { ctx, parent } = await setup([textResponse('x')]) - const run = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) await run.result expect(ctx.agents.get(run.id)).toBeDefined() await run.dispose() @@ -95,7 +118,7 @@ describe('dsh-subagent-spawn', () => { it('stamps child depth = parent depth + 1 (via the merged AgentOptions field)', async () => { const { ctx, parent } = await setup([textResponse('x')]) expect(depthOf(parent)).toBe(0) - const run = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) await run.result const child = ctx.agents.get(run.id)! expect(depthOf(child)).toBe(1) @@ -105,13 +128,13 @@ describe('dsh-subagent-spawn', () => { it('refuses to spawn past maxDepth (depthLimit capability)', async () => { const { ctx, parent } = await setup([]) // parent is depth 0, child would be depth 1 — cap at 0 forbids any child. - expect(() => ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'p' }], parent, maxDepth: 0 })) - .toThrow(SubagentDepthError) + await expect(start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent, maxDepth: 0 })) + .rejects.toThrow(SubagentDepthError) }) it('maps a child that hit its token ceiling to stopReason "max-tokens"', async () => { const { ctx, parent } = await setup([maxTokensResponse('cut off')]) - const run = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) const result = await run.result expect(result.stopReason).toBe('max-tokens') await run.dispose() @@ -121,14 +144,14 @@ describe('dsh-subagent-spawn', () => { // Empty script: the child's first model call throws "script exhausted", the // turn ends `error`, and there is no assistant/message → empty output. const { ctx, parent } = await setup([]) - const run = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) const result = await run.result expect(result.stopReason).toBe('error') expect(result.output).toEqual([]) await run.dispose() }) - it('settles aborted (without running the child) when the request signal is ALREADY aborted', async () => { + it('rejects without publishing when the request signal is already aborted', async () => { // Regression: a signal aborted BEFORE the run starts never fires an `abort` // event, so the listener can't catch it. The driver must check the // already-aborted case up front and settle `aborted` without running the @@ -137,26 +160,44 @@ describe('dsh-subagent-spawn', () => { const controller = new AbortController() controller.abort() const { ctx, parent } = await setup([]) - const run = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'p' }], parent, signal: controller.signal }) - const result = await run.result - expect(result.stopReason).toBe('aborted') - expect(result.output).toEqual([]) - await run.dispose() + await expect(start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent, signal: controller.signal })) + .rejects.toThrow('aborted before child publication') }) - it('cancelling BEFORE the child turn starts settles aborted, not error', async () => { - // Regression: a cancel landing in the pre-turn window clears the queued - // prompt before any `turn/end` is logged. Deriving the stop reason from - // `turn/end` alone then mis-maps the no-turn case to `error`; the run must - // honor the cancel contract and settle `aborted`. The cancel is synchronous - // (same tick as start, before the loop's queued-wait continuation runs), so - // the turn is dropped and the empty script is never consumed. + it('same-tick cancellation rejects start and prevents child publication', async () => { + // Regression: cancellation before publication used to set a flag but let the + // async factory publish a child anyway, so `started` fulfilled and lifecycle + // observers saw an agent for an attempt the caller had already cancelled. + // The empty script also proves no model turn can run. const { ctx, parent } = await setup([]) - const run = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) - run.cancel('early') + const beforeAgents = ctx.agents.list().length + const beforeSessions = ctx.sessions.list().length + const published: string[] = [] + ctx.on('session/created', () => void published.push('session/created')) + ctx.on('agent/created', () => void published.push('agent/created')) + ctx.on('agent/session-start', () => void published.push('agent/session-start')) + ctx.on('subagent/start', () => void published.push('subagent/start')) + ctx.on('subagent/end', () => void published.push('subagent/end')) + const controller = new AbortController() + const starting = start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent, signal: controller.signal }) + controller.abort('early') + + await expect(starting).rejects.toThrow() + await Promise.resolve() + expect(ctx.agents.list()).toHaveLength(beforeAgents) + expect(ctx.sessions.list()).toHaveLength(beforeSessions) + expect(published).toEqual([]) + }) + + it('a cancel from agent/queued maps a no-turn child log to aborted', async () => { + const { ctx, parent } = await setup([]) + const controller = new AbortController() + ctx.on('agent/queued', () => { controller.abort('queued-window') }) + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent, signal: controller.signal }) const result = await run.result - expect(result.stopReason).toBe('aborted') - expect(result.output).toEqual([]) + expect(result).toMatchObject({ stopReason: 'aborted', output: [] }) + const child = ctx.agents.get(run.id)! + expect(child.session.events.some(event => event.type === 'turn/end')).toBe(false) await run.dispose() }) @@ -164,7 +205,7 @@ describe('dsh-subagent-spawn', () => { // 'hang' makes the child's model stream one chunk then wait until aborted. const controller = new AbortController() const { ctx, parent } = await setup(['hang']) - const run = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'p' }], parent, signal: controller.signal }) + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent, signal: controller.signal }) // Let the child's turn start, then abort via the request signal (the // backend bridges it to child.cancel()). await new Promise(r => setTimeout(r, 30)) @@ -174,29 +215,18 @@ describe('dsh-subagent-spawn', () => { await run.dispose() }) - it('run.cancel() also cancels the child directly', async () => { + it('dispose cancels the child and reaches quiescence', async () => { const { ctx, parent } = await setup(['hang']) - const run = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) await new Promise(r => setTimeout(r, 30)) - run.cancel('test cancel') + await run.dispose() const result = await run.result expect(result.stopReason).toBe('aborted') - await run.dispose() - }) - - it('run.cancel() with no reason uses the default cancel reason', async () => { - const { ctx, parent } = await setup(['hang']) - const run = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) - await new Promise(r => setTimeout(r, 30)) - run.cancel() - const result = await run.result - expect(result.stopReason).toBe('aborted') - await run.dispose() }) it('does not expose the optional runtime methods (sendMessage/resume) in this cut', async () => { const { ctx, parent } = await setup([textResponse('x')]) - const run = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent }) expect('sendMessage' in run).toBe(false) expect('resume' in run).toBe(false) await run.result @@ -206,13 +236,13 @@ describe('dsh-subagent-spawn', () => { it('inherits the parent cwd into the child session', async () => { const { ctx } = await setup([textResponse('x')]) // A parent WITH a cwd (config agents have none, so create one explicitly). - const parentHandle = ctx.agents.create({ + const parentHandle = await ctx.agents.create({ agentId: AgentId('cwd-parent'), sessionId: SessionId('cwd-parent-session'), meta: { cwd: '/tmp/parent-workspace' }, agentOptions: { model: 'mock' }, }) - const run = ctx.subagents.start('spawn', { prompt: [{ type: 'text', text: 'p' }], parent: parentHandle.agent }) + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent: parentHandle.agent }) await run.result const child = ctx.agents.get(run.id)! expect(child.session.header.cwd).toBe('/tmp/parent-workspace') @@ -223,13 +253,13 @@ describe('dsh-subagent-spawn', () => { it('uses request.agentOptions.model when the parent has no model of its own', async () => { const { ctx } = await setup([textResponse('explicit model child')]) // A parent with NO model (its own turns would need one supplied per-request). - const parentHandle = ctx.agents.create({ + const parentHandle = await ctx.agents.create({ agentId: AgentId('modelless-parent'), sessionId: SessionId('modelless-parent-session'), agentOptions: {}, }) // The request supplies the child's model explicitly. - const run = ctx.subagents.start('spawn', { + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent: parentHandle.agent, agentOptions: { model: 'mock' }, @@ -241,10 +271,10 @@ describe('dsh-subagent-spawn', () => { await parentHandle.dispose() }) - it('advertises depthLimit and outputSchema but not toolFilter', async () => { + it('advertises every start-time capability (depthLimit, outputSchema, toolFilter, persona)', async () => { const { ctx } = await setup([]) const provider = ctx.subagents.getProvider('spawn')! - expect(provider.capabilities).toEqual({ outputSchema: true, depthLimit: true, toolFilter: false }) + expect(provider.capabilities).toEqual({ outputSchema: true, depthLimit: true, toolFilter: true, persona: true }) }) it('unregisters the provider when its fiber is disposed (HMR safety)', async () => { @@ -261,7 +291,7 @@ describe('dsh-subagent-spawn', () => { const { ctx, parent } = await setup([ toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 42 }), ]) - const run = ctx.subagents.start('spawn', { + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'produce the answer' }], parent, outputSchema: { type: 'object', properties: { answer: { type: 'number' } }, required: ['answer'] }, @@ -274,7 +304,7 @@ describe('dsh-subagent-spawn', () => { await run.dispose() }) - it('a backend unload mid-structured-run settles the run and releases the runtime', async () => { + it('a backend unload does not revoke an accepted holder-owned run', async () => { // Rebuild the stack by hand so we hold the backend's fiber. const ctx = new Context() const adapter = new MockAdapter(['hang']) @@ -289,31 +319,171 @@ describe('dsh-subagent-spawn', () => { const fiber = await ctx.plugin(spawn, { providerName: 'spawn' }) ctx.llm.registerAdapter(['mock'], adapter) const parent = ctx.agentLoop.create(AgentId('parent'), { model: 'mock' }) - const run = ctx.subagents.start('spawn', { + const controller = new AbortController() + const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'q' }], parent, + signal: controller.signal, outputSchema: { type: 'object', properties: { a: { type: 'number' } } }, }) - // Let the child's step start streaming, then unload the backend. The - // backend owns the child agent, so the unload tears the child down and - // the run settles — releasing its own runtime acquisition on the way out. + // Provider removal prevents new starts but the returned run belongs to its + // holder and remains live. await new Promise(resolve => setTimeout(resolve, 30)) await fiber.dispose() + expect(ctx.subagents.getProvider('spawn')).toBeUndefined() + expect(ctx.agents.get(run.id)).toBeDefined() + controller.abort('test complete') const result = await run.result - expect(result.stopReason).toBe('error') + expect(result.stopReason).toBe('aborted') expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL)).toBeUndefined() await run.dispose() }) + it('a start racing an already-unloading backend cannot begin child creation', async () => { + const ctx = new Context() + await ctx.plugin(LlmService) + await ctx.plugin(SessionStore) + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(AgentRegistry) + await ctx.plugin(AgentLoop, { agents: [] }) + await ctx.plugin(SubagentService) + const fiber = await ctx.plugin(spawn, { providerName: 'spawn' }) + const parent = ctx.agentLoop.create(AgentId('parent'), { model: 'mock' }) + const parentEffects = parent.ctx.fiber.getEffects().length + const published: string[] = [] + ctx.on('session/created', () => void published.push('session/created')) + ctx.on('agent/created', () => void published.push('agent/created')) + + const unloading = fiber.dispose() + await unloading + await expect(start(ctx, 'spawn', { + prompt: [{ type: 'text', text: 'must never start' }], parent, + })).rejects.toThrow(/no subagent provider/) + + expect(parent.ctx.fiber.getEffects()).toHaveLength(parentEffects) + expect(published).toEqual([]) + }) + it('has the namespace-plugin export shape (no stray default)', () => { expect('default' in spawn).toBe(false) expect(spawn.name).toBe('subagent-spawn') - expect(spawn.inject).toEqual(['subagents', 'agents']) + expect(spawn.inject).toEqual(['subagents']) const loader = Object.create(Loader.prototype) as Loader const unwrapped = loader.unwrapExports(spawn) as Record expect(unwrapped).toBe(spawn) expect(unwrapped.name).toBe('subagent-spawn') - expect(unwrapped.inject).toEqual(['subagents', 'agents']) + expect(unwrapped.inject).toEqual(['subagents']) expect(typeof unwrapped.apply).toBe('function') }) + + describe('persona and toolFilter (the scoped child world)', () => { + it('a per-child persona shadows the deployment persona in the child request only', async () => { + const { ctx, parent, adapter } = await setup([ + textResponse('parent answer'), + textResponse('child answer'), + ]) + parent.send([{ type: 'text', text: 'hi' }]) + await parent.whenIdle() + + const run = await start(ctx, 'spawn', { + prompt: [{ type: 'text', text: 'do X' }], + parent, + persona: 'You are the tersest test runner.', + }) + await run.result + const childRequest = adapter.requests.at(-1)! + expect(childRequest.system).toContain('You are the tersest test runner.') + // The parent's earlier request carried no such persona. + expect(adapter.requests[0]!.system ?? '').not.toContain('tersest test runner') + await run.dispose() + }) + + it('toolFilter hides denied tools from the child prompt AND refuses their execution', async () => { + const { ctx, parent, adapter } = await setup([ + // The child tries the denied tool anyway, then answers. + toolCallResponse('c1', 'forbidden_tool', {}), + textResponse('done'), + ]) + ctx.tools.register({ + name: 'forbidden_tool', description: 'global', parameters: {}, + execute: () => Promise.resolve([{ type: 'text', text: 'ran' }]), + }) + const run = await start(ctx, 'spawn', { + prompt: [{ type: 'text', text: 'do X' }], + parent, + toolFilter: { deny: ['forbidden_tool'] }, + }) + const result = await run.result + expect(result.stopReason).toBe('completed') + // Not advertised… + const childRequest = adapter.requests[0]! + expect((childRequest.tools ?? []).map(t => t.name)).not.toContain('forbidden_tool') + // …and the attempted call executed as UNKNOWN_TOOL (visible in the log). + const child = ctx.agents.get(run.id)! + const toolResult = child.session.events.find(e => e.type === 'tool/result')! + expect(JSON.stringify(toolResult.data)).toContain('unknown tool') + await run.dispose() + }) + + it('an unknown toolFilter name fails the spawn loudly with no orphaned child', async () => { + const { ctx, parent } = await setup([]) + const before = ctx.agents.list().length + await expect(start(ctx, 'spawn', { + prompt: [{ type: 'text', text: 'do X' }], + parent, + toolFilter: { deny: ['no_such_tool'] }, + })).rejects.toThrow(/unknown global tool "no_such_tool"/) + expect(ctx.agents.list().length).toBe(before) + }) + }) + + it('spawning from a DISPOSING parent fails loud with no orphaned child (INACTIVE_EFFECT teaching error)', async () => { + const { ctx } = await setup([]) + // A handle-owned parent we can dispose (config agents dispose with the loop fiber). + const parentHandle = await ctx.agents.create({ + agentId: AgentId('doomed-parent'), + sessionId: SessionId('doomed-s'), + agentOptions: { model: 'mock' }, + }) + await parentHandle.dispose() + const before = ctx.agents.list().length + const sessionsBefore = ctx.sessions.list().length + const published: string[] = [] + ctx.on('session/created', () => void published.push('session/created')) + ctx.on('agent/created', () => void published.push('agent/created')) + ctx.on('agent/session-start', () => void published.push('agent/session-start')) + await expect(start(ctx, 'spawn', { + prompt: [{ type: 'text', text: 'do X' }], + parent: parentHandle.agent, + })).rejects.toThrow(/inactive context/) + expect(ctx.agents.list().length).toBe(before) + expect(ctx.sessions.list()).toHaveLength(sessionsBefore) + expect(published).toEqual([]) + }) + + it('parent disposal during the child setup transaction prevents every publication notification', async () => { + const { ctx } = await setup([]) + const parentHandle = await ctx.agents.create({ + agentId: AgentId('setup-race-parent'), + sessionId: SessionId('setup-race-parent-session'), + agentOptions: { model: 'mock' }, + }) + const published: string[] = [] + ctx.on('session/created', () => void published.push('session/created')) + ctx.on('agent/created', () => void published.push('agent/created')) + ctx.on('agent/session-start', () => void published.push('agent/session-start')) + + const starting = start(ctx, 'spawn', { + prompt: [{ type: 'text', text: 'must never run' }], + parent: parentHandle.agent, + }) + // The factory has entered its awaited unpublished setup transaction. The + // parent context owns that transaction, so disposal wins without an + // observer ever seeing the child. + await parentHandle.dispose() + await expect(starting).rejects.toThrow(/owner disposed during setup|inactive context/) + + expect(published).toEqual([]) + }) }) diff --git a/packages/subagent/subagent/README.md b/packages/subagent/subagent/README.md index 8bab4c61c9..413bbbdafc 100644 --- a/packages/subagent/subagent/README.md +++ b/packages/subagent/subagent/README.md @@ -1,43 +1,61 @@ # @deepseek-ai/dsh-subagent -The **subagent seam**: an abstract `SubagentService` (`ctx.subagents`) for an agent delegating work to another agent. A *subagent* is a child agent; a `SubagentProvider` is one transport for running it. +The subagent seam lets one agent delegate work to a child through a named provider. Callers use one service API (`ctx.subagents`); providers decide whether the child runs in this process, in another process, or through a future transport. -This package is the interface third of the capability seam, split so each concern evolves (and swaps) independently: +## Package roles + +The family separates the stable interface from implementations and model-facing tools: | Package | Role | |---|---| -| `@deepseek-ai/dsh-subagent` (this) | the interface: registry service + vocabulary types | -| `@deepseek-ai/dsh-subagent-spawn` | an implementation: fresh in-process child | -| `@deepseek-ai/dsh-subagent-fork` | an implementation: in-process child seeded from the parent's log | -| `@deepseek-ai/dsh-subagent-acp` | an implementation: ACP client driving another process | -| `@deepseek-ai/dsh-tool-subagent` | the model-facing tool over `ctx.subagents` | +| `@deepseek-ai/dsh-subagent` | Provider registry, request/result types, and lifecycle events. | +| `@deepseek-ai/dsh-subagent-spawn` | Fresh in-process child. | +| `@deepseek-ai/dsh-subagent-fork` | In-process child seeded with completed parent turns. | +| `@deepseek-ai/dsh-subagent-acp` | Fresh out-of-process ACP child. | +| `@deepseek-ai/dsh-tool-subagent` | Model-facing tool over one configured provider. | -Unlike the bash seam (one executor per context, second load throws), **multiple providers coexist** here. Each registers under a unique name and a caller picks one by name — the shape mirrors the LLM adapter registry (`LlmService.registerAdapter`), not the single-service bash executor. This is the requirement that rules out the bash shape: an agent may want an in-process child for a cheap subtask and an out-of-process ACP child for an isolated one, in the same runtime. +Multiple providers may coexist under different names. This lets a deployment expose, for example, a cheap in-process child and an isolated ACP child without changing the service contract. -## Service API (`ctx.subagents`) +## Service API -| Member | Semantics | +`SubagentService` has four main operations: + +| Member | Meaning | |---|---| -| `registerProvider(provider)` | Register under `provider.name`. Throws `SubagentError('DUPLICATE_PROVIDER')` on a name clash. Effect-scoped (HMR-safe); returns the disposer. | -| `getProvider(name)` | Look up a provider (`undefined` if absent). | -| `list()` | Registered provider names (insertion order). | -| `start(name, request)` | Resolve the provider (`NO_PROVIDER` if absent), validate every requested START-TIME capability (`UNSUPPORTED_CAPABILITY` for the first unmet one — before any child is created), then delegate to `provider.start` and emit `subagent/start` / `subagent/end` around the run. | +| `registerProvider(provider)` | Register one trusted same-process implementation by name. Registration is effect-scoped; removing it prevents new starts but does not revoke runs already returned to callers. Duplicate names fail loud. | +| `getProvider(name)` | Return the provider, or `undefined` when absent. | +| `list()` | Return provider names in insertion order. | +| `start(name, request)` | Validate requested capabilities and semantic values, then await the provider until a real child is ready. Fulfillment returns a holder-owned `SubagentRun`; rejection means the provider has already cleaned every partial startup resource. | -## Capabilities: two kinds, discovered two ways +`SubagentStartRequest.signal` is required and is the canonical cancellation channel. An abort before publication makes `start()` reject after rollback; an abort after publication cancels the live child. The request may also select a model, require structured output, cap delegation depth, restrict child tools, or set a child persona. -- **Start-time features** (`outputSchema`, `depthLimit`, `toolFilter`) are a static `provider.capabilities` descriptor, checked by the service BEFORE a run exists. A request that needs one the provider lacks is **rejected loud** (`UNSUPPORTED_CAPABILITY`), never accepted-then-ignored. -- **Runtime features** (steering, resume) are **optional methods** on `SubagentRun` (`sendMessage?`, `resume?`). The method's presence IS the capability; TS narrowing is the discovery mechanism — a consumer cannot call an absent method without narrowing first, so there is no silent degradation path. +Same-process requests, descriptors, results, and event payloads are trusted typed values borrowed as immutable. The service does not clone or freeze them; serialization and hostile-input validation belong at actual process, worker, persistence, and model boundaries. -Beside `capabilities` sits one DESCRIPTIVE fact, not validated by the service: `provider.inheritsParentContext` — whether a child sees the parent conversation (`fork`: true — seeded with the completed-turn prefix; `spawn`/`acp`: false). The model-facing consumer (`dsh-tool-subagent`) derives truthful tool wording from it. +## Capabilities -## Run lifecycle +Start-time features are advertised in `provider.capabilities` because the service must reject an unsupported request before child creation: -`provider.start(request)` returns a `SubagentRun`: a handle with a `result` promise, `cancel()`, `dispose()`, and the optional runtime methods. `result` resolves with a `SubagentResult` (`output`, optional `structured`, `stopReason`) — it does **not** reject on a child-level failure (a model/transport failure resolves with `stopReason: 'error'`), so the consumer maps a non-`completed` reason to an `isError` tool result. The consumer MUST `dispose()` on every path (success, error, abort) to reach child quiescence and avoid leaking an idle child / session. +- `outputSchema` — enforce a structured final result. +- `depthLimit` — enforce `maxDepth`. +- `toolFilter` — apply the requested child tool restriction. +- `persona` — apply a per-child persona. -The service also announces provider lifecycle: `subagent/provider-added` (the live provider) fires after a registration and `subagent/provider-removed` (the name) after an unregistration, so a consumer deriving state from a named provider (the model-facing tool wording) mirrors registry membership instead of assuming load order — the cordis Loader starts sibling plugins concurrently, so "listed earlier" does not mean "registered earlier". The service emits `subagent/start` (payload `SubagentRunInfo`) and `subagent/end` (payload `SubagentRunEndInfo`) around the run — both **observe-only** (plain `emit`s; `subagent/end` fires from a detached `.then` and awaits no listener). `subagent/end` carries `lastAssistantMessage` (a deep clone of the child's final `output`) on the settle path, absent when the run rejected at the infrastructure level. The clone keeps the surface observe-only: the end emit fires from a detached `.then` before the caller's `await run.result` resumes, so a shared reference would let a mutating listener corrupt the caller's result. A `subagent/start` listener can still reach the live child via `ctx.agents.get(info.id)`; a `subagent/end` listener can only observe (the run has settled). Any run-affecting decision (continuation, injection that changes the run) is out of scope for this observe-only surface. +Runtime features are optional methods on `SubagentRun`: `sendMessage?` steers a live child, while `resume?` asynchronously creates a continuation run. Method presence is the capability check. -## Scope (first cut) +`inheritsParentContext` is descriptive rather than enforceable. It says only whether the child sees completed parent conversation history (`fork` does; `spawn` and ACP do not), not whether it inherits tools, services, or authority. -The consumer collects **synchronously**: it starts a run and awaits `result`. Steering (`sendMessage`) is part of the contract but intentionally unused. Background / poll / spill semantics are deferred to a future redesign unifying long-running-tool handling across subagents and bash. See the RFC: [docs/rfc/implemented/feature/2026-06-21-subagent-capability-seam.md](../../../docs/rfc/implemented/feature/2026-06-21-subagent-capability-seam.md). +## Ownership and lifecycle -See `src/types.ts` for the full contracts. +`provider.start(request): Promise` is the ownership-transfer boundary. Before fulfillment, the provider owns setup and must cancel, roll back, and quiesce partial resources on every failure. After fulfillment, the caller owns the run and must call `dispose()` on every path. + +`SubagentRun.result` resolves to `{ output, structured?, stopReason }`. Child-level failures resolve with a non-`completed` reason; only an infrastructure fault that the seam cannot represent may reject. `dispose()` is idempotent, cancels remaining work, and waits for the child resources to quiesce. + +The service emits `subagent/start` only after `start()` has fulfilled. It attaches the result observer before that synchronous notification, so even an already-settled child still produces `subagent/start` before `subagent/end`. In-process start observers can resolve the published child through `ctx.agents.get(info.id)`; remote providers need not publish a local agent. + +Run events are scoped to the delegating parent. Every listener is independently contained: a synchronous throw or rejected returned promise is logged without starving peer listeners or changing the run. + +Provider additions and removals also emit `subagent/provider-added` and `subagent/provider-removed`. Consumers such as the model-facing tool use those events because Cordis may load sibling plugins concurrently; configuration order does not prove registration order. + +## Collection model + +The current model-facing tool collects synchronously: it awaits the child result and disposes the run before returning. Background collection and polling remain outside this seam. See the [capability-seam RFC](../../../docs/rfc/implemented/feature/2026-06-21-subagent-capability-seam.md) and `src/types.ts` for the complete contracts. diff --git a/packages/subagent/subagent/package.json b/packages/subagent/subagent/package.json index be5baeb0e1..eb0dbf8da0 100644 --- a/packages/subagent/subagent/package.json +++ b/packages/subagent/subagent/package.json @@ -24,12 +24,14 @@ "peerDependencies": { "@deepseek-ai/dsh-agent": "^0.0.1", "@deepseek-ai/dsh-llm": "^0.0.1", + "@deepseek-ai/dsh-scope": "^0.0.1", "@deepseek-ai/dsh-tools": "^0.0.1", "cordis": "^4.0.0-rc.6" }, "devDependencies": { "@deepseek-ai/dsh-agent": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", + "@deepseek-ai/dsh-scope": "workspace:^", "@deepseek-ai/dsh-tools": "workspace:^", "cordis": "^4.0.0-rc.6" } diff --git a/packages/subagent/subagent/src/index.ts b/packages/subagent/subagent/src/index.ts index 91230b7ff4..4635de9697 100644 --- a/packages/subagent/subagent/src/index.ts +++ b/packages/subagent/subagent/src/index.ts @@ -1,41 +1,24 @@ /** * The subagent seam (`ctx.subagents`): a named-provider registry plus a - * capability-validating `start` surface. A subagent is an agent delegating - * work to another agent; a {@link SubagentProvider} is one transport for - * running that child (in-process spawn/fork, ACP to another process, and — - * later — A2A, the Codex app-server, the Claude Code Agent SDK). + * capability-validating asynchronous start surface. Providers establish a + * child before returning its run, so fulfillment is the single publication and + * ownership-transfer boundary. * - * Unlike the bash seam (one executor per context, second load throws), MULTIPLE - * providers coexist here: each registers under a unique name and a caller picks - * one by name. The shape mirrors the LLM adapter registry - * (`LlmService.registerAdapter`), not the single-service bash executor. - * - * This package is the INTERFACE third of the capability seam. Implementations - * (`@deepseek-ai/dsh-subagent-spawn`, `-fork`, `-acp`) and the model-facing - * consumer (`@deepseek-ai/dsh-tool-subagent`) are separate packages. - * - * Scope (first cut): the consumer collects synchronously — it starts a run and - * awaits {@link SubagentRun.result}. Steering ({@link SubagentRun.sendMessage}) - * is part of the contract but intentionally unused; background / poll / spill - * semantics are deferred to a future redesign that unifies long-running-tool - * handling across subagents and bash. - * - * The `subagent/start` / `subagent/end` lifecycle events carry an OBSERVE-ONLY - * payload; `subagent/end` additionally carries the child's `lastAssistantMessage` - * — see `docs/rfc/implemented/feature/2026-06-30-subagent-observe-enrich.md`. - * FIXME(subagent-continuation): a control-flow `subagent/end` (an awaited - * waterfall returning a stop/continue decision, like the other interception - * seams) would require reshaping this emit into a waterfall, awaiting listeners - * before settling, and a `resume` capability on the in-process provider — part - * of the deferred background/steering redesign, NOT this observe-only cut. + * Same-process providers are trusted typed collaborators. Requests, provider + * descriptors, results, and lifecycle payloads are borrowed immutable values; + * serialization and hostile-input validation belong at real process, worker, + * persistence, and model boundaries. * * @module @deepseek-ai/dsh-subagent */ import { Context, Service } from 'cordis' +import { scopeTarget } from '@deepseek-ai/dsh-scope' +import type { Scoped } from '@deepseek-ai/dsh-scope' +import { assertSupportedOutputSchema } from '@deepseek-ai/dsh-tools' import { HarnessError } from '@deepseek-ai/dsh-llm' import type { ContentBlock } from '@deepseek-ai/dsh-llm' -import type { AgentId } from '@deepseek-ai/dsh-agent' +import type { Agent, AgentId } from '@deepseek-ai/dsh-agent' import type { SubagentCapabilities, SubagentProvider, @@ -54,6 +37,21 @@ export type { SubagentStopReasonMap, } from './types.ts' +/** + * Reject a recursion cap that cannot represent an exact delegation depth. + * @param maxDepth - the optional runtime value to validate. + */ +export function assertSubagentMaxDepth(maxDepth: unknown): void { + if (maxDepth !== undefined && ( + typeof maxDepth !== 'number' + || !Number.isSafeInteger(maxDepth) + || maxDepth < 0 + || Object.is(maxDepth, -0) + )) { + throw new TypeError('subagent maxDepth must be a non-negative safe integer') + } +} + declare module 'cordis' { interface Context { subagents: SubagentService @@ -61,75 +59,59 @@ declare module 'cordis' { interface Events { /** - * A provider became resolvable in the {@link SubagentService} registry. - * Consumers that derive state from a named provider (e.g. the model-facing - * tool wording in `dsh-tool-subagent`) react HERE instead of assuming load - * order — the cordis Loader starts sibling plugins concurrently, so - * "listed earlier in cordis.yml" does not mean "registered earlier". - * @param provider - the provider that just registered, live in the registry. + * A provider became resolvable in the registry. + * @param provider - the registered provider. * @mode emit */ 'subagent/provider-added'(provider: SubagentProvider): void /** - * A provider left the registry (its plugin's fiber was disposed — an - * unload or an HMR reload). Consumers holding provider-derived state drop - * it here; a reload re-fires `subagent/provider-added` with the fresh - * provider. Delivered with per-listener containment: a throwing - * subscriber is logged, never starves later subscribers, and never - * disrupts the provider's teardown. - * @param name - the registry name that no longer resolves. + * A provider left the registry. Accepted runs remain holder-owned. + * @param name - the provider name that no longer resolves. * @mode emit */ 'subagent/provider-removed'(name: string): void /** - * A subagent run started — emitted after the provider is resolved and its - * capabilities validated, as the child run begins. Paired with - * {@link Events['subagent/end']}. - * @param info - which provider started which child agent. + * A provider established a ready child. For in-process providers, + * `ctx.agents.get(info.id)` resolves during this notification. + * Scope-filtered dispatch keys the carrier by the delegating parent, so a + * parent-scoped listener observes only its own delegations. Paired with + * `subagent/end`. + * @param info - the provider and ready child identity. * @mode emit */ - 'subagent/start'(info: SubagentRunInfo): void + 'subagent/start'(this: Scoped, info: SubagentRunInfo): void /** - * A subagent run settled — emitted when {@link SubagentRun.result} - * resolves (any stop reason). Paired with {@link Events['subagent/start']}. - * @param info - the run identity plus stop reason and final output. + * A ready child settled. Scope-filtered dispatch uses the same delegating + * parent carrier as `subagent/start`, so the lifecycle pair reaches the + * same scoped audience. + * @param info - the run identity and terminal outcome. * @mode emit */ - 'subagent/end'(info: SubagentRunEndInfo): void + 'subagent/end'(this: Scoped, info: SubagentRunEndInfo): void } } -/** Identifying detail for a started subagent run (the `subagent/start` payload). */ +/** Observe-only identifying detail for a ready subagent run. */ export interface SubagentRunInfo { - /** The provider that started the run. */ - provider: string + /** The provider that established the run. */ + readonly provider: string /** The child agent's id. */ - id: AgentId + readonly id: AgentId } -/** Outcome detail for a settled subagent run (the `subagent/end` payload). */ +/** Observe-only outcome detail for a settled subagent run. */ export interface SubagentRunEndInfo { /** The provider that ran it. */ - provider: string + readonly provider: string /** The child agent's id. */ - id: AgentId + readonly id: AgentId /** The terminal stop reason. */ - stopReason: SubagentResult['stopReason'] - /** - * The child's final assistant output ({@link SubagentResult.output}), carried - * onto the end event so an observer sees WHAT the subagent produced without - * holding the run. Absent when the run rejected at the infrastructure level - * (no {@link SubagentResult} was produced — the seam only knows `stopReason: - * 'error'`). - */ - lastAssistantMessage?: ContentBlock[] + readonly stopReason: SubagentResult['stopReason'] + /** The child's final assistant output, absent on infrastructure rejection. */ + readonly lastAssistantMessage?: ContentBlock[] } -/** - * Typed error for subagent-seam failures. Extends {@link HarnessError}, so the - * `code` string (`DUPLICATE_PROVIDER`, `NO_PROVIDER`, `UNSUPPORTED_CAPABILITY`) - * is shared, machine-routable taxonomy. - */ +/** Typed error for provider lookup, registration, and capability failures. */ export class SubagentError extends HarnessError { constructor(message: string, code: string, options?: ErrorOptions) { super(message, code, options) @@ -137,10 +119,7 @@ export class SubagentError extends HarnessError { } } -/** - * The `subagents` service: a registry of named {@link SubagentProvider}s and a - * capability-checked {@link start} surface. - */ +/** Named provider registry and capability-checked start surface. */ export class SubagentService extends Service { private providers = new Map() @@ -149,163 +128,120 @@ export class SubagentService extends Service { } /** - * Register a provider under its `provider.name`. Throws {@link SubagentError} - * (`DUPLICATE_PROVIDER`) if the name is already taken. Effect-scoped: disposed - * with the calling fiber (HMR-safe). Emits `subagent/provider-added` after - * the registration and `subagent/provider-removed` on unregistration, so - * consumers can mirror provider lifecycle instead of assuming load order. - * @param provider - the provider; its `name` is the registry key. - * @returns the disposer that unregisters the provider. + * Register a provider under its name. Registration is effect-scoped and HMR + * safe; removing a provider blocks new starts but does not revoke runs that + * were already returned to their holders. + * @param provider - the trusted provider implementation. + * @returns the exact Cordis effect disposer. */ registerProvider(provider: SubagentProvider): () => void { - const dispose = this.ctx.effect(function* (this: SubagentService) { - if (this.providers.has(provider.name)) { - throw new SubagentError(`a subagent provider named "${provider.name}" is already registered`, 'DUPLICATE_PROVIDER') + const name = provider.name + // eslint-disable-next-line @typescript-eslint/no-misused-promises -- synchronous cleanup; direct return preserves disposer identity + return this.ctx.effect(function* (this: SubagentService) { + if (this.providers.has(name)) { + throw new SubagentError(`a subagent provider named "${name}" is already registered`, 'DUPLICATE_PROVIDER') } - this.providers.set(provider.name, provider) - // Yield the rollback BEFORE emitting `subagent/provider-added`: a - // throwing added-listener then unregisters the provider (and announces - // the removal) instead of leaking it into the registry. The removal - // announcement itself is contained PER LISTENER ({@link emitLifecycle}): - // it runs inside this disposer, where a propagating subscriber would - // disrupt the backend fiber's teardown and starve later mirrors. + this.providers.set(name, provider) yield () => { - this.providers.delete(provider.name) - this.emitLifecycle('subagent/provider-removed', provider.name) + this.providers.delete(name) + this.emitLifecycle('subagent/provider-removed', name) } + // A throwing added-listener unwinds the yielded rollback, matching the + // repository's fail-loud registration semantics. this.ctx.emit('subagent/provider-added', provider) }.bind(this), 'subagents.registerProvider()') - // ctx.effect's disposer returns Promise; our disposer API is - // synchronous fire-and-forget — discard the (always-resolved) promise. - return () => void dispose() } /** - * Look up a registered provider by name (`undefined` if absent). - * @param name - the provider name as registered. - * @returns the provider, or undefined when the name is unknown. + * Look up a provider by name. + * @param name - the provider name. + * @returns the provider, or undefined when absent. */ getProvider(name: string): SubagentProvider | undefined { return this.providers.get(name) } /** - * The names of all registered providers (insertion order). - * @returns the registered provider names. + * List registered provider names in insertion order. + * @returns the registered names. */ list(): string[] { return [...this.providers.keys()] } /** - * Start a subagent run on the named provider. Resolves the provider (throws - * `NO_PROVIDER` if absent), validates every requested START-TIME capability - * against {@link SubagentProvider.capabilities} (throws `UNSUPPORTED_CAPABILITY` - * for the first unmet one — fail loud, before any child is created), then - * delegates to {@link SubagentProvider.start} and emits `subagent/start` / - * `subagent/end` around the run. - * @param name - the provider to run on. - * @param request - the child's prompt, capabilities, and options. - * @returns the live run (its `result` resolves when the child settles). + * Establish a ready child on the named provider. Capability and semantic + * checks run before delegation. Provider ownership lasts until its promise + * fulfills; a rejection therefore has no run for the caller to dispose and + * emits no run lifecycle events. + * @param name - the provider to use. + * @param request - child prompt, parent, signal, and optional capabilities. + * @returns the ready holder-owned run. */ - start(name: string, request: SubagentStartRequest): SubagentRun { + async start(name: string, request: SubagentStartRequest): Promise { const provider = this.providers.get(name) - if (!provider) { + if (provider === undefined) { throw new SubagentError(`no subagent provider registered for "${name}"`, 'NO_PROVIDER') } this.assertCapabilities(provider, request) + assertSubagentMaxDepth(request.maxDepth) + if (request.outputSchema !== undefined) assertSupportedOutputSchema(request.outputSchema) - const run = provider.start(request) - // Emit `subagent/start` with PER-LISTENER containment (see {@link emitLifecycle}): - // the run is already live, so neither a throwing subscriber escaping - // `start()` (the caller would never receive the run to dispose it — a leaked - // child) NOR one bad subscriber starving the listeners after it is - // acceptable. `ctx.emit` halts the dispatch on the first throw, so a single - // surrounding try/catch is not enough — each listener is invoked and - // contained individually. - this.emitLifecycle('subagent/start', { provider: name, id: run.id }) - // Emit `subagent/end` when the run settles. The result promise does not - // reject on a child-level failure (it resolves with stopReason 'error'), - // so a rejection here is an infrastructure fault — surface its stop reason - // as 'error' for the telemetry event without swallowing the rejection - // (the consumer still observes it via `run.result`). On the resolve path the - // child's final output rides on the event (lastAssistantMessage); on the - // reject path there is no SubagentResult, so only the stop reason is known. - // Per-listener containment also keeps a thrown `subagent/end` listener from - // becoming an unhandled rejection on this detached `.then`. + const parent = request.parent + const run = await provider.start(request) + // Attach the terminal observer before dispatching start. Promise reactions + // still run after this synchronous start emission, preserving start → end. void run.result.then( (result) => { - // Deep-clone the output onto the event: this detached `.then` runs BEFORE - // the caller's own `await run.result` continuation, so handing listeners - // the SAME array reference the caller consumes would let a mutating - // `subagent/end` listener corrupt the caller's SubagentResult.output — - // breaking the observe-only contract. A snapshot makes the event a - // read-only view, not a shared handle. The clone is wrapped: it runs - // inside `onFulfilled`, OUTSIDE emitLifecycle's per-listener containment, - // so an uncloneable value (a future non-serializable content-block type, - // or a contract-violating result with no `output`) would otherwise become - // an unhandled rejection on this detached `.then`. On clone failure, log - // and emit the event WITHOUT lastAssistantMessage rather than dropping the - // whole `subagent/end`. - let lastAssistantMessage: SubagentResult['output'] | undefined - try { - lastAssistantMessage = structuredClone(result.output) - } catch (error: unknown) { - this.ctx.logger.warn(`subagent: could not clone ${name} output for subagent/end: ${String(error)}`) - } - this.emitLifecycle('subagent/end', { provider: name, id: run.id, stopReason: result.stopReason, ...lastAssistantMessage !== undefined ? { lastAssistantMessage } : {} }) + this.emitLifecycle('subagent/end', { + provider: name, + id: run.id, + stopReason: result.stopReason, + lastAssistantMessage: result.output, + }, parent) + }, + () => { + this.emitLifecycle('subagent/end', { provider: name, id: run.id, stopReason: 'error' }, parent) }, - () => { this.emitLifecycle('subagent/end', { provider: name, id: run.id, stopReason: 'error' }) }, ) + this.emitLifecycle('subagent/start', { provider: name, id: run.id }, parent) return run } /** - * Emit a `subagent/*` lifecycle event with PER-LISTENER containment: dispatch - * each subscriber individually and log (never propagate) a thrown one, so one - * bad subscriber can neither strand the already-live run, surface as an - * unhandled rejection on the detached settle hook, NOR starve the listeners - * registered after it. A single try/catch around `ctx.emit` would not do the - * last part — cordis `emit` runs listeners in a `.map(cb => cb())` that halts - * on the first throw — so this resolves the listener callbacks via - * `ctx.events.dispatch` and contains each call, the same guarantee - * `BashExecutor.notifyTaskDone` gives its own listener set. - * - * `subagent/provider-removed` routes through here too: it fires inside the - * provider registration's DISPOSER, where a propagating listener would - * disrupt the backend fiber's teardown (dispose must reach quiescence) and a - * starved later listener would leave a mirror consumer (`dsh-tool-subagent`) - * holding a tool for a provider that no longer exists. `subagent/provider-added` - * deliberately does NOT: it fires at registration time, where a throwing - * listener unwinds the yielded rollback — the same fail-loud register-time - * semantics as the system-prompt registries. + * Emit lifecycle events with per-listener synchronous and asynchronous + * exception containment. Payloads are borrowed immutable values. */ - private emitLifecycle(name: 'subagent/start', info: SubagentRunInfo): void - private emitLifecycle(name: 'subagent/end', info: SubagentRunEndInfo): void + private emitLifecycle(name: 'subagent/start', info: SubagentRunInfo, parent: Agent): void + private emitLifecycle(name: 'subagent/end', info: SubagentRunEndInfo, parent: Agent): void private emitLifecycle(name: 'subagent/provider-removed', info: string): void private emitLifecycle( name: 'subagent/start' | 'subagent/end' | 'subagent/provider-removed', info: SubagentRunInfo | SubagentRunEndInfo | string, + parent?: Agent, ): void { - for (const callback of this.ctx.events.dispatch('emit', [name, info])) { + const dispatchArgs: unknown[] = parent === undefined + ? [name, info] + : [scopeTarget(this, parent), name, info] + for (const callback of this.ctx.events.dispatch('emit', dispatchArgs)) { try { - callback(info) + const returned: unknown = callback(info) + void Promise.resolve(returned).catch((error: unknown) => { + this.ctx.logger.warn(`subagent: ${name} listener rejected: ${renderThrown(error)}`) + }) } catch (error: unknown) { - this.ctx.logger.warn(`subagent: ${name} listener threw: ${String(error)}`) + this.ctx.logger.warn(`subagent: ${name} listener threw: ${renderThrown(error)}`) } } } - /** - * Reject a request that needs a start-time capability the provider lacks. - * Each optional request field maps to one {@link SubagentCapabilities} flag; - * the first unmet one throws `UNSUPPORTED_CAPABILITY`. - */ + /** Reject the first requested capability that the provider lacks. */ private assertCapabilities(provider: SubagentProvider, request: SubagentStartRequest): void { const needs: { when: boolean; cap: keyof SubagentCapabilities }[] = [ { when: request.outputSchema !== undefined, cap: 'outputSchema' }, { when: request.maxDepth !== undefined, cap: 'depthLimit' }, { when: request.toolFilter !== undefined, cap: 'toolFilter' }, + { when: request.persona !== undefined, cap: 'persona' }, ] for (const { when, cap } of needs) { if (when && !provider.capabilities[cap]) { @@ -318,4 +254,13 @@ export class SubagentService extends Service { } } +/** Render any listener-thrown value without letting coercion escape containment. */ +function renderThrown(value: unknown): string { + try { + return value instanceof Error ? `${value.name}: ${value.message}` : String(value) + } catch { + return '' + } +} + export default SubagentService diff --git a/packages/subagent/subagent/src/types.ts b/packages/subagent/subagent/src/types.ts index fb512c0bdb..3bdc78569a 100644 --- a/packages/subagent/subagent/src/types.ts +++ b/packages/subagent/subagent/src/types.ts @@ -8,7 +8,7 @@ import type { Agent, AgentId, AgentOptions } from '@deepseek-ai/dsh-agent' import type { ContentBlock } from '@deepseek-ai/dsh-llm' -import type { StructuredOutputSchema } from '@deepseek-ai/dsh-tools' +import type { StructuredOutputSchema, ToolRestriction } from '@deepseek-ai/dsh-tools' /** * Which START-TIME features a provider supports. Checked by the service @@ -24,11 +24,13 @@ import type { StructuredOutputSchema } from '@deepseek-ai/dsh-tools' */ export interface SubagentCapabilities { /** Honor {@link SubagentStartRequest.outputSchema} (structured final output). */ - outputSchema: boolean + readonly outputSchema: boolean /** Enforce {@link SubagentStartRequest.maxDepth} (recursion cap). */ - depthLimit: boolean + readonly depthLimit: boolean /** Enforce {@link SubagentStartRequest.toolFilter} (child tool scoping). */ - toolFilter: boolean + readonly toolFilter: boolean + /** Honor {@link SubagentStartRequest.persona} (a per-child persona). */ + readonly persona: boolean } /** @@ -39,22 +41,24 @@ export interface SubagentCapabilities { */ export interface SubagentStartRequest { /** The task/prompt for the child agent (a user message in the child session). */ - prompt: ContentBlock[] + readonly prompt: ContentBlock[] /** * The spawning ("parent") agent — the one whose tool call started this * subagent. REQUIRED: in-process backends read `parent.session.header` for * the working directory, the `parentSession` lineage to stamp on the child, * and the parent's delegation depth. Out-of-process backends (ACP) ignore it. */ - parent: Agent + readonly parent: Agent /** * Cancellation signal from the spawning context (the tool's `exec.signal`). - * A provider that honors it aborts the child when the signal fires; the - * consumer also bridges it to {@link SubagentRun.cancel} explicitly. + * This is the canonical cancellation channel both before and after startup: + * a provider rejects `start()` after cleaning partial resources when it + * fires before publication, and cancels a published child when it fires + * afterward. */ - signal?: AbortSignal - /** Per-child agent options (model, system prompt). */ - agentOptions?: AgentOptions + readonly signal: AbortSignal + /** Per-child agent options (model and plugin-defined extension fields). */ + readonly agentOptions?: AgentOptions /** * Optional structured-output schema — an object-rooted JSON Schema within the * enforced subset (see `assertSupportedOutputSchema` in dsh-tools; a schema @@ -65,17 +69,30 @@ export interface SubagentStartRequest { * data — a caller holding foreign-realm data materializes it first. * Requesting it against a provider that lacks the capability is rejected at start. */ - outputSchema?: StructuredOutputSchema + readonly outputSchema?: StructuredOutputSchema /** - * Optional recursion cap (max delegation depth below this child). Requires - * {@link SubagentCapabilities.depthLimit}; rejected at start otherwise. + * Optional absolute delegation-depth cap for the child being started: its + * computed depth must be less than or equal to this non-negative safe + * integer. Requires {@link SubagentCapabilities.depthLimit}; rejected at + * start otherwise. */ - maxDepth?: number + readonly maxDepth?: number /** * Optional child tool scoping. Requires {@link SubagentCapabilities.toolFilter}; - * rejected at start otherwise. + * rejected at start otherwise. In-process backends apply it as a scoped + * `tools.restrict()` in the child's creation window: the named tools vanish + * from the child's prompt AND refuse to execute (one visibility), with loud + * unknown-name validation. */ - toolFilter?: { allow?: string[]; deny?: string[] } + readonly toolFilter?: ToolRestriction + /** + * Optional per-child persona. Requires {@link SubagentCapabilities.persona}; + * rejected at start otherwise. In-process backends register it as a scoped + * `deployment:persona` section on the child, SHADOWING the deployment's + * persona for this child alone — same template semantics as the deployment + * persona (strict `{{…}}` interpolation against the registered variables). + */ + readonly persona?: string } /** @@ -87,7 +104,7 @@ export interface SubagentStartRequest { export interface SubagentStopReasonMap { /** The child finished its turn normally. */ completed: 'completed' - /** The run was cancelled (parent signal, explicit `cancel()`, or peer cancel). */ + /** The run was cancelled by its request signal or by disposal. */ aborted: 'aborted' /** The child failed (model error, transport error). */ error: 'error' @@ -105,29 +122,31 @@ export type SubagentStopReason = SubagentStopReasonMap[keyof SubagentStopReasonM */ export interface SubagentResult { /** The child's final assistant output (the last assistant message's content). */ - output: ContentBlock[] + readonly output: ContentBlock[] /** - * The structured result, present IFF the request carried an `outputSchema` - * AND the provider honored it. Shape is validated against the request schema - * by the provider; `unknown` here because the seam is schema-agnostic. + * The structured result after a requested `outputSchema` was successfully + * satisfied. Requesting a schema does not guarantee presence: a provider can + * end with `stopReason: 'error'` when the child fails or finishes without a + * valid capture. Shape is validated against the request schema by the + * provider; `unknown` here because the seam is schema-agnostic. */ - structured?: unknown + readonly structured?: unknown /** Why the run ended. A non-`completed` reason means `output` may be partial. */ - stopReason: SubagentStopReason + readonly stopReason: SubagentStopReason } /** * A live subagent run: a handle the consumer holds while a child executes. - * Returned by {@link SubagentProvider.start} (via the service). The consumer - * awaits {@link result}, may {@link cancel} mid-flight, and MUST {@link dispose} - * on every path to reach child quiescence (no leaked idle child / session). + * Returned by {@link SubagentProvider.start} (via the service) only after the + * child is ready. The consumer awaits {@link result} and MUST {@link dispose} + * on every path to cancel any remaining work and reach child quiescence. * * {@link sendMessage} and {@link resume} are OPTIONAL: a provider that supports * the runtime capability defines the method; one that doesn't omits it. The * presence of the method IS the capability — narrow before calling. */ export interface SubagentRun { - /** The child agent's id (use `ctx.agents.get(id)` to reach the live child). */ + /** The child agent's id (local in-process runs are already published in `ctx.agents`; remote transports need not publish locally). */ readonly id: AgentId /** * Resolves with the child's terminal {@link SubagentResult} when the run @@ -137,12 +156,10 @@ export interface SubagentRun { * cannot represent as a stop reason. */ readonly result: Promise - /** Request cancellation of the in-flight run; {@link result} settles `aborted`. */ - cancel(reason?: string): void /** - * Reach child quiescence and release the run's resources (in-process: dispose - * the owned agent handle and remove its session; ACP: kill the subprocess). - * Idempotent; awaits the child actually stopping, not merely requesting it. + * Cancel remaining work, reach child quiescence, and release the run's + * resources (in-process: dispose the owned agent and remove its session; + * ACP: kill and reap the subprocess). Idempotent. */ dispose(): Promise /** @@ -154,14 +171,16 @@ export interface SubagentRun { * OPTIONAL (resume capability): send a follow-up task to a settled child, * continuing its session, and return a fresh run for the continuation. */ - resume?(content: ContentBlock[]): SubagentRun + resume?(content: ContentBlock[]): Promise } /** * A subagent backend: one transport for running a child agent (in-process * spawn/fork, ACP to another process, …). Implementations register under a * unique name via {@link SubagentService.registerProvider}; multiple providers - * coexist in one context (unlike the single-implementation bash seam). + * coexist in one context (unlike the single-implementation bash seam). The + * Providers are trusted same-process implementations; callers treat their + * descriptors and returned values as borrowed immutable data. */ export interface SubagentProvider { /** Unique registry name (e.g. `spawn`, `fork`, `acp`). */ @@ -169,19 +188,24 @@ export interface SubagentProvider { /** The start-time features this provider supports (see {@link SubagentCapabilities}). */ readonly capabilities: SubagentCapabilities /** - * The provider's context contract: `true` when a child SEES the parent + * The provider's conversation-history descriptor: `true` when a child SEES the parent * conversation (fork — the child is seeded with the parent's completed-turn * prefix), `false` when it starts fresh (spawn, ACP). A DESCRIPTIVE fact, * not a start-time capability: the service validates nothing against it — * the model-facing consumer (`dsh-tool-subagent`) derives truthful tool * wording from it, so a tool bound to a fork provider stops telling the - * model the child "does not see this conversation". + * model the child "does not see this conversation". This descriptor concerns + * conversation history only; it says nothing about tool registrations, + * injected services, or authority inheritance. */ readonly inheritsParentContext: boolean /** - * Start a child run. The service has already validated that every requested - * start-time capability is supported, so an implementation may assume e.g. - * `request.maxDepth` is honorable when present. + * Establish a child and return its handle only after publication. The + * service has already validated that every requested start-time capability + * is supported, so an implementation may assume e.g. `request.maxDepth` is + * honorable when present. If setup fails or `request.signal` aborts before + * fulfillment, the provider owns and cleans all partial resources before this + * promise rejects. Ownership transfers to the caller only on fulfillment. */ - start(request: SubagentStartRequest): SubagentRun + start(request: SubagentStartRequest): Promise } diff --git a/packages/subagent/subagent/tests/service.spec.ts b/packages/subagent/subagent/tests/service.spec.ts index f70abf7e72..3d10b425ed 100644 --- a/packages/subagent/subagent/tests/service.spec.ts +++ b/packages/subagent/subagent/tests/service.spec.ts @@ -2,8 +2,10 @@ import { describe, expect, it, vi } from 'vitest' import { Context } from 'cordis' import { AgentId, type Agent } from '@deepseek-ai/dsh-agent' import { HarnessError } from '@deepseek-ai/dsh-llm' +import { carrierKeyOf } from '@deepseek-ai/dsh-scope' import SubagentService, { SubagentError, + assertSubagentMaxDepth, type SubagentCapabilities, type SubagentProvider, type SubagentResult, @@ -11,403 +13,218 @@ import SubagentService, { type SubagentStartRequest, } from '@deepseek-ai/dsh-subagent' -/** A minimal parent Agent stand-in — the service only reads `parent.id`. */ function fakeParent(id = 'parent-1'): Agent { return { id: AgentId(id) } as unknown as Agent } -const ALL_CAPS: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: true } -const NO_CAPS: SubagentCapabilities = { outputSchema: false, depthLimit: false, toolFilter: false } +const ALL_CAPS: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: true, persona: true } +const NO_CAPS: SubagentCapabilities = { outputSchema: false, depthLimit: false, toolFilter: false, persona: false } + +function baseRequest(overrides: Partial = {}): SubagentStartRequest { + return { + prompt: [{ type: 'text', text: 'do a thing' }], + parent: fakeParent(), + signal: new AbortController().signal, + ...overrides, + } +} -/** A scripted provider whose run settles immediately with a fixed result. */ class StubProvider implements SubagentProvider { - startCount = 0 readonly inheritsParentContext = false + startCount = 0 + constructor( readonly name: string, readonly capabilities: SubagentCapabilities = ALL_CAPS, - private readonly result: SubagentResult = { output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' }, + private readonly outcome: SubagentResult = { + output: [{ type: 'text', text: 'ok' }], + stopReason: 'completed', + }, ) {} - start(request: SubagentStartRequest): SubagentRun { - this.startCount++ + async start(request: SubagentStartRequest): Promise { + this.startCount += 1 return { id: AgentId(`child:${this.name}:${request.parent.id}`), - result: Promise.resolve(this.result), - cancel() {}, + result: Promise.resolve(this.outcome), async dispose() {}, } } } -function baseRequest(overrides: Partial = {}): SubagentStartRequest { - return { prompt: [{ type: 'text', text: 'do a thing' }], parent: fakeParent(), ...overrides } +async function service(): Promise<{ ctx: Context; subagents: SubagentService }> { + const ctx = new Context() + await ctx.plugin(SubagentService) + return { ctx, subagents: ctx.subagents } } describe('SubagentService', () => { - it('announces provider lifecycle: added on register, removed on dispose', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) + it('registers, lists, looks up, starts, and removes providers', async () => { + const { ctx, subagents } = await service() const added: string[] = [] const removed: string[] = [] ctx.on('subagent/provider-added', provider => void added.push(provider.name)) ctx.on('subagent/provider-removed', name => void removed.push(name)) - - const dispose = ctx.subagents.registerProvider(new StubProvider('alpha')) - expect(added).toEqual(['alpha']) - expect(removed).toEqual([]) - - dispose() - expect(removed).toEqual(['alpha']) - }) - - it('rolls back the registration when a provider-added listener throws', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) - let threw = false - const off = ctx.on('subagent/provider-added', () => { - if (!threw) { threw = true; throw new Error('boom added listener') } - }) - - expect(() => ctx.subagents.registerProvider(new StubProvider('alpha'))).toThrow('boom added listener') - expect(ctx.subagents.getProvider('alpha')).toBeUndefined() // nothing leaked - - off() - ctx.subagents.registerProvider(new StubProvider('alpha')) - expect(ctx.subagents.getProvider('alpha')).toBeDefined() - }) - - it('contains a throwing provider-removed listener: later mirrors still hear it, teardown completes', async () => { - // provider-removed fires inside the registration's DISPOSER, so a - // propagating listener would disrupt the backend's teardown; and cordis - // emit halts on the first throw, so an uncontained one would starve every - // mirror registered after it (a stale model-facing tool). Both are - // prevented by per-listener containment. - const ctx = new Context() - await ctx.plugin(SubagentService) - const warnings: string[] = [] - ctx.logger.warn = ((message: unknown) => void warnings.push(String(message))) as typeof ctx.logger.warn - ctx.on('subagent/provider-removed', () => { throw new Error('boom removed listener') }) - const heard: string[] = [] - ctx.on('subagent/provider-removed', name => void heard.push(name)) - - const dispose = ctx.subagents.registerProvider(new StubProvider('alpha')) - expect(() => { dispose() }).not.toThrow() - expect(heard).toEqual(['alpha']) // the listener AFTER the thrower still ran - expect(ctx.subagents.getProvider('alpha')).toBeUndefined() // teardown reached quiescence - expect(warnings.some(w => w.includes('boom removed listener'))).toBe(true) - }) - - it('registers a provider and starts a run on it by name', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) const provider = new StubProvider('alpha') - ctx.subagents.registerProvider(provider) - expect(ctx.subagents.list()).toEqual(['alpha']) - expect(ctx.subagents.getProvider('alpha')).toBe(provider) - - const run = ctx.subagents.start('alpha', baseRequest()) - expect(provider.startCount).toBe(1) + const dispose = subagents.registerProvider(provider) + expect(subagents.list()).toEqual(['alpha']) + expect(subagents.getProvider('alpha')).toBe(provider) + const run = await subagents.start('alpha', baseRequest()) await expect(run.result).resolves.toMatchObject({ stopReason: 'completed' }) - }) + expect(provider.startCount).toBe(1) - it('lets multiple providers coexist (the defining requirement)', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) - ctx.subagents.registerProvider(new StubProvider('spawn')) - ctx.subagents.registerProvider(new StubProvider('acp')) - - expect(ctx.subagents.list()).toEqual(['spawn', 'acp']) - expect(ctx.subagents.getProvider('spawn')).toBeDefined() - expect(ctx.subagents.getProvider('acp')).toBeDefined() - }) - - it('throws NO_PROVIDER when starting on an unregistered name', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) - try { - ctx.subagents.start('missing', baseRequest()) - expect.fail('expected NO_PROVIDER') - } catch (error: unknown) { - expect(error).toBeInstanceOf(SubagentError) - expect((error as SubagentError).code).toBe('NO_PROVIDER') - } - }) - - it('rejects duplicate provider names with DUPLICATE_PROVIDER', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) - ctx.subagents.registerProvider(new StubProvider('dup')) - try { - ctx.subagents.registerProvider(new StubProvider('dup')) - expect.fail('expected DUPLICATE_PROVIDER') - } catch (error: unknown) { - expect(error).toBeInstanceOf(SubagentError) - expect((error as SubagentError).code).toBe('DUPLICATE_PROVIDER') - } - }) - - it('unregisters a provider when its owning fiber is disposed (HMR safety)', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) - - const fiber = await ctx.plugin(Object.assign((inner: Context) => { - inner.subagents.registerProvider(new StubProvider('scoped')) - }, { inject: ['subagents'] })) - expect(ctx.subagents.list()).toEqual(['scoped']) - - await fiber.dispose() - expect(ctx.subagents.list()).toEqual([]) - }) - - it('re-registers a name after its prior registration is disposed (not wedged)', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) - - const dispose = ctx.subagents.registerProvider(new StubProvider('reuse')) - expect(ctx.subagents.list()).toEqual(['reuse']) dispose() - expect(ctx.subagents.list()).toEqual([]) - - const disposeAgain = ctx.subagents.registerProvider(new StubProvider('reuse')) - expect(ctx.subagents.list()).toEqual(['reuse']) - disposeAgain() - expect(ctx.subagents.list()).toEqual([]) + expect(added).toEqual(['alpha']) + expect(removed).toEqual(['alpha']) + expect(subagents.getProvider('alpha')).toBeUndefined() }) - describe('start-time capability validation (fail loud, before any child)', () => { - it.each([ - { field: 'outputSchema', request: baseRequest({ outputSchema: { type: 'object', properties: { x: { type: 'string' } } } }) }, - { field: 'maxDepth', request: baseRequest({ maxDepth: 2 }) }, - { field: 'toolFilter', request: baseRequest({ toolFilter: { deny: ['bash'] } }) }, - ])('rejects $field against a provider that lacks the capability — before start() runs', ({ request }) => { - const ctx = new Context() - return ctx.plugin(SubagentService).then(() => { - const provider = new StubProvider('weak', NO_CAPS) - ctx.subagents.registerProvider(provider) - try { - ctx.subagents.start('weak', request) - expect.fail('expected UNSUPPORTED_CAPABILITY') - } catch (error: unknown) { - expect(error).toBeInstanceOf(SubagentError) - expect((error as SubagentError).code).toBe('UNSUPPORTED_CAPABILITY') - } - // The child was never started — the check is pre-spawn. - expect(provider.startCount).toBe(0) - }) - }) - - it('allows a capability request when the provider supports it', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) - const provider = new StubProvider('strong', ALL_CAPS) - ctx.subagents.registerProvider(provider) - ctx.subagents.start('strong', baseRequest({ outputSchema: { type: 'object', properties: { x: { type: 'string' } } }, maxDepth: 1 })) - expect(provider.startCount).toBe(1) - }) + it('rolls registration back when provider-added throws', async () => { + const { ctx, subagents } = await service() + ctx.on('subagent/provider-added', () => { throw new Error('added boom') }) + expect(() => { subagents.registerProvider(new StubProvider('alpha')) }).toThrow('added boom') + expect(subagents.getProvider('alpha')).toBeUndefined() }) - it('emits subagent/start then subagent/end around a run', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) - ctx.subagents.registerProvider(new StubProvider('events')) + it('rejects duplicate and absent provider names with typed errors', async () => { + const { subagents } = await service() + subagents.registerProvider(new StubProvider('dup')) + expect(() => { subagents.registerProvider(new StubProvider('dup')) }) + .toThrow(expect.objectContaining({ code: 'DUPLICATE_PROVIDER' })) + await expect(subagents.start('missing', baseRequest())) + .rejects.toMatchObject({ code: 'NO_PROVIDER' }) + }) - const started = vi.fn() - const ended = vi.fn() - ctx.on('subagent/start', started) - ctx.on('subagent/end', ended) + it.each([ + ['outputSchema', { outputSchema: { type: 'object', properties: {} } }], + ['depthLimit', { maxDepth: 1 }], + ['toolFilter', { toolFilter: { deny: ['bash'] } }], + ['persona', { persona: 'reviewer' }], + ] as const)('rejects unsupported %s before provider startup', async (_capability, override) => { + const { subagents } = await service() + const provider = new StubProvider('weak', NO_CAPS) + subagents.registerProvider(provider) + await expect(subagents.start('weak', baseRequest(override))) + .rejects.toMatchObject({ code: 'UNSUPPORTED_CAPABILITY' }) + expect(provider.startCount).toBe(0) + }) - const run = ctx.subagents.start('events', baseRequest()) - expect(started).toHaveBeenCalledWith(expect.objectContaining({ provider: 'events', id: run.id })) + it('validates depth and schema semantics before provider startup', async () => { + const { subagents } = await service() + const provider = new StubProvider('strong') + subagents.registerProvider(provider) + await expect(subagents.start('strong', baseRequest({ maxDepth: -1 }))) + .rejects.toThrow('non-negative safe integer') + await expect(subagents.start('strong', baseRequest({ outputSchema: { type: 'string' } as never }))) + .rejects.toThrow() + expect(provider.startCount).toBe(0) + expect(() => { assertSubagentMaxDepth(undefined) }).not.toThrow() + }) - await run.result - // `subagent/end` fires from a `.then` on the result — let the microtask run. + it('publishes lifecycle only after async provider start and keeps parent scope', async () => { + const { ctx, subagents } = await service() + const ready = Promise.withResolvers() + const result = Promise.withResolvers() + subagents.registerProvider({ + name: 'deferred', + capabilities: NO_CAPS, + inheritsParentContext: false, + start: () => ready.promise, + }) + const parent = fakeParent('delegator') + const events: string[] = [] + const keys: unknown[] = [] + ctx.on('subagent/start', function () { events.push('start'); keys.push(carrierKeyOf(this)) }) + ctx.on('subagent/end', function () { events.push('end'); keys.push(carrierKeyOf(this)) }) + + const starting = subagents.start('deferred', baseRequest({ parent })) await Promise.resolve() - expect(ended).toHaveBeenCalledWith(expect.objectContaining({ provider: 'events', id: run.id, stopReason: 'completed' })) + expect(events).toEqual([]) + ready.resolve({ id: AgentId('child'), result: result.promise, async dispose() {} }) + const run = await starting + expect(events).toEqual(['start']) + result.resolve({ output: [{ type: 'text', text: 'answer' }], stopReason: 'completed' }) + await run.result + await Promise.resolve() + expect(events).toEqual(['start', 'end']) + expect(keys).toEqual([parent, parent]) }) - it('carries lastAssistantMessage (the child output) onto the end event', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) - ctx.subagents.registerProvider(new StubProvider( - 'enriched', - ALL_CAPS, - { output: [{ type: 'text', text: 'the child answer' }], stopReason: 'completed' }, - )) + it('emits no run lifecycle when provider startup rejects', async () => { + const { ctx, subagents } = await service() + subagents.registerProvider({ + name: 'failed', + capabilities: NO_CAPS, + inheritsParentContext: false, + start: async () => { throw new Error('setup rolled back') }, + }) + const lifecycle = vi.fn() + ctx.on('subagent/start', lifecycle) + ctx.on('subagent/end', lifecycle) + await expect(subagents.start('failed', baseRequest())).rejects.toThrow('setup rolled back') + expect(lifecycle).not.toHaveBeenCalled() + }) - const started = vi.fn() + it('emits an enriched end event and maps result rejection to error telemetry', async () => { + const { ctx, subagents } = await service() + const completed = new StubProvider('completed', NO_CAPS, { + output: [{ type: 'text', text: 'answer' }], + stopReason: 'completed', + }) + subagents.registerProvider(completed) const ended = vi.fn() - ctx.on('subagent/start', started) ctx.on('subagent/end', ended) - - const run = ctx.subagents.start('enriched', baseRequest()) - expect(started).toHaveBeenCalledWith(expect.objectContaining({ provider: 'enriched', id: run.id })) - + const run = await subagents.start('completed', baseRequest()) await run.result await Promise.resolve() expect(ended).toHaveBeenCalledWith(expect.objectContaining({ - provider: 'enriched', - id: run.id, + provider: 'completed', + lastAssistantMessage: [{ type: 'text', text: 'answer' }], stopReason: 'completed', - lastAssistantMessage: [{ type: 'text', text: 'the child answer' }], })) - }) - it('observe-only: a subagent/end listener mutating lastAssistantMessage cannot corrupt the caller\'s result', async () => { - // The subagent/end emit fires from a detached `.then` registered before - // start() returns — i.e. BEFORE the caller's own `await run.result` - // continuation. If the event shared the result.output reference, a mutating - // listener would change the SubagentResult the caller consumes. The service - // deep-clones output onto the event, so the listener mutates only its copy. - const ctx = new Context() - await ctx.plugin(SubagentService) - ctx.subagents.registerProvider(new StubProvider( - 'clone', - ALL_CAPS, - { output: [{ type: 'text', text: 'original' }], stopReason: 'completed' }, - )) - - ctx.on('subagent/end', (info) => { - // A hostile/buggy listener reaches in and mutates the event's array. - const blocks = info.lastAssistantMessage - if (blocks?.[0]?.type === 'text') blocks[0].text = 'HIJACKED' - blocks?.push({ type: 'text', text: 'injected' }) - }) - - const run = ctx.subagents.start('clone', baseRequest()) - const result = await run.result - await Promise.resolve() // let the detached settle hook (and its listener) run - // The caller's result.output is untouched by the listener's mutation. - expect(result.output).toEqual([{ type: 'text', text: 'original' }]) - }) - - it('omits lastAssistantMessage on the reject path (no SubagentResult was produced)', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) - ctx.subagents.registerProvider({ - name: 'rej', + const failure = Promise.withResolvers() + subagents.registerProvider({ + name: 'infra', capabilities: NO_CAPS, inheritsParentContext: false, - start: () => ({ - id: AgentId('rej-child'), - result: Promise.reject(new Error('infra fault')), - cancel() {}, - dispose: async () => {}, - }), + async start() { + return { id: AgentId('infra-child'), result: failure.promise, async dispose() {} } + }, }) - - const ended = vi.fn() - ctx.on('subagent/end', ended) - const run = ctx.subagents.start('rej', baseRequest()) - await run.result.catch(() => {}) + const failedRun = await subagents.start('infra', baseRequest()) + failure.reject(new Error('transport')) + await expect(failedRun.result).rejects.toThrow('transport') await Promise.resolve() - - const endInfo = ended.mock.calls[0]![0] as Record - expect(endInfo.stopReason).toBe('error') - expect('lastAssistantMessage' in endInfo).toBe(false) // no output exists on reject + expect(ended).toHaveBeenCalledWith(expect.objectContaining({ provider: 'infra', stopReason: 'error' })) }) - it('contains a structuredClone failure: emits subagent/end without lastAssistantMessage (no unhandled rejection)', async () => { - // The clone runs inside onFulfilled, OUTSIDE emitLifecycle's per-listener - // containment. An uncloneable output (here a content block carrying a - // function) would otherwise throw and become an unhandled rejection on the - // detached `.then`. The handler must instead log and emit the event WITHOUT - // lastAssistantMessage, still carrying the real stopReason. - const ctx = new Context() - await ctx.plugin(SubagentService) - const warn = vi.fn(); ctx.logger.warn = warn as never - // An output value structuredClone cannot handle (a function is uncloneable). - const uncloneable = [{ type: 'text', text: 'x', evil: () => 0 }] as unknown as SubagentResult['output'] - ctx.subagents.registerProvider({ - name: 'unclone', - capabilities: NO_CAPS, - inheritsParentContext: false, - start: () => ({ - id: AgentId('unclone-child'), - result: Promise.resolve({ output: uncloneable, stopReason: 'completed' } as SubagentResult), - cancel() {}, - dispose: async () => {}, - }), - }) + it('contains synchronous and asynchronous lifecycle observer failures', async () => { + const { ctx, subagents } = await service() + const warnings: string[] = [] + ctx.logger.warn = ((message: unknown) => void warnings.push(String(message))) as typeof ctx.logger.warn + const heard: string[] = [] + ctx.on('subagent/provider-removed', () => { throw new Error('sync boom') }) + // Runtime listeners may return thenables even though the declaration's observable result is void. + // eslint-disable-next-line @typescript-eslint/no-misused-promises -- exercises rejected-listener containment + ctx.on('subagent/provider-removed', async () => { throw new Error('async boom') }) + ctx.on('subagent/provider-removed', () => { throw { toString: () => { throw new Error('coercion') } } }) + ctx.on('subagent/provider-removed', name => void heard.push(name)) + const dispose = subagents.registerProvider(new StubProvider('contained')) - const ended = vi.fn() - ctx.on('subagent/end', ended) - const run = ctx.subagents.start('unclone', baseRequest()) - await run.result + dispose() await Promise.resolve() - - const endInfo = ended.mock.calls[0]![0] as Record - expect(endInfo.stopReason).toBe('completed') // the real outcome is preserved - expect('lastAssistantMessage' in endInfo).toBe(false) // clone failed → omitted, not crashed - expect(warn).toHaveBeenCalledWith(expect.stringContaining('could not clone')) + expect(heard).toEqual(['contained']) + expect(warnings.some(message => message.includes('sync boom'))).toBe(true) + expect(warnings.some(message => message.includes('async boom'))).toBe(true) + expect(warnings.some(message => message.includes(''))).toBe(true) }) - it('emits subagent/end with stopReason "error" when the run result promise rejects', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) - // A provider whose run.result REJECTS (an infrastructure fault — the seam - // contract says child-level failures resolve with stopReason 'error', but a - // rejection is still surfaced as an 'error' telemetry event). - ctx.subagents.registerProvider({ - name: 'rejecter', - capabilities: NO_CAPS, - inheritsParentContext: false, - start: () => ({ - id: AgentId('rej-child'), - result: Promise.reject(new Error('infra fault')), - cancel() {}, - dispose: async () => {}, - }), - }) - - const ended = vi.fn() - ctx.on('subagent/end', ended) - const run = ctx.subagents.start('rejecter', baseRequest()) - // Observe (and swallow) the rejection the consumer would see, then let the - // detached `.then` settle the telemetry emit. - await run.result.catch(() => {}) - await Promise.resolve() - expect(ended).toHaveBeenCalledWith(expect.objectContaining({ provider: 'rejecter', id: run.id, stopReason: 'error' })) - }) - - it('contains a throwing subagent/start listener per-listener: a later listener still observes the event and start() returns the run', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) - ctx.subagents.registerProvider(new StubProvider('contain')) - // Two listeners; the FIRST throws. Per-listener containment means the second - // must STILL run (a single try/catch around ctx.emit would let the first - // throw halt the dispatch and starve the second — the round-2 regression). - const second = vi.fn() - ctx.on('subagent/start', () => { throw new Error('bad start listener') }) - ctx.on('subagent/start', second) - - const run = ctx.subagents.start('contain', baseRequest()) - expect(run.id).toBeDefined() - expect(second).toHaveBeenCalledWith(expect.objectContaining({ provider: 'contain', id: run.id })) - await expect(run.result).resolves.toMatchObject({ stopReason: 'completed' }) - }) - - it('contains a throwing subagent/end listener per-listener: a later listener still observes the settle, no unhandled rejection', async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) - ctx.subagents.registerProvider(new StubProvider('contain-end')) - const second = vi.fn() - ctx.on('subagent/end', () => { throw new Error('bad end listener') }) - ctx.on('subagent/end', second) - - const run = ctx.subagents.start('contain-end', baseRequest()) - await run.result - // Let the detached `.then` + the contained emit run. - await Promise.resolve() - await Promise.resolve() - expect(second).toHaveBeenCalledWith(expect.objectContaining({ provider: 'contain-end', id: run.id, stopReason: 'completed' })) - }) - - it('SubagentError extends the shared HarnessError base', () => { - const err = new SubagentError('boom', 'NO_PROVIDER') - expect(err).toBeInstanceOf(HarnessError) - expect(err.name).toBe('SubagentError') - expect(err.code).toBe('NO_PROVIDER') + it('SubagentError participates in the harness error taxonomy', () => { + const error = new SubagentError('boom', 'NO_PROVIDER') + expect(error).toBeInstanceOf(HarnessError) + expect(error.name).toBe('SubagentError') + expect(error.code).toBe('NO_PROVIDER') }) }) diff --git a/packages/subagent/subagent/tsconfig.json b/packages/subagent/subagent/tsconfig.json index 0781a1129c..f93f929241 100644 --- a/packages/subagent/subagent/tsconfig.json +++ b/packages/subagent/subagent/tsconfig.json @@ -22,6 +22,9 @@ }, { "path": "../../core/tools" + }, + { + "path": "../../core/scope" } ] } diff --git a/packages/subagent/tool-subagent/README.md b/packages/subagent/tool-subagent/README.md index 6fe26d3083..c234d5832b 100644 --- a/packages/subagent/tool-subagent/README.md +++ b/packages/subagent/tool-subagent/README.md @@ -1,23 +1,28 @@ # @deepseek-ai/dsh-tool-subagent -The model-facing `subagent` tool: delegate a self-contained task to a child agent and return its final output. Pure schema + lifecycle shaping over the [`ctx.subagents`](../subagent/README.md) provider registry — an in-process, ACP, or future A2A backend swaps in without changing what the model sees. +The `subagent` tool lets the model delegate one self-contained task and collect the child's final output. It is a thin consumer of `ctx.subagents`; changing the configured provider changes the transport without changing the model-facing execution contract. -## Provider selection is config, not model-facing +## Provider selection -This plugin binds to **exactly one** provider (`Config.provider`). The model sees only `{ description, prompt }` — there is no provider/type parameter in the schema. To expose more than one transport, load the plugin more than once, each bound to a different provider **and a distinct `toolName`** (the tool registry rejects a duplicate name, so a second load that kept the default `subagent` name would throw). Keeping selection in config (not the schema) is the deliberate split: the *service* holds a multi-provider registry; the *tool* picks one. +Each plugin instance binds to exactly one provider. The model sees `{ description, prompt }`, not a provider selector. To expose multiple transports, load the plugin multiple times with distinct `toolName` values. -## The description states the provider's context contract +The description is derived from `provider.inheritsParentContext`: spawn and ACP tell the model to provide a standalone prompt, while fork says the child already sees completed conversation turns. The plugin follows `subagent/provider-added` and `subagent/provider-removed`, so concurrent Cordis plugin loading does not create a registration-order dependency. -The tool description and the `prompt` parameter description are DERIVED from the bound provider's `inheritsParentContext` (`providerWording`): a fresh-context provider (spawn, ACP) gets the standalone-prompt wording ("it does not see this conversation"), an inheriting provider (fork) tells the model the child already sees the conversation's completed turns and its prompt should state only what is new. Because the description is fixed at tool registration, the tool **mirrors the provider's lifecycle** (`subagent/provider-added`/`-removed`): it registers when the bound provider is (or becomes) available and unregisters when the provider goes away — no load-order requirement (the cordis Loader starts sibling entries concurrently, so "listed first" never guaranteed "registered first"), and an HMR reload of the backend re-derives the wording from the fresh provider. While the provider is absent the tool simply does not exist (a `ctx.logger` note records the wait; a typo'd provider name shows up as a tool that never materializes). +## Lifecycle -| Config key | Meaning | +`execute` passes the tool execution's abort signal when present, otherwise supplies an inert signal to satisfy the required `SubagentStartRequest.signal`. It awaits `ctx.subagents.start(...)`, then awaits `run.result` inside a `try/finally` that always calls `run.dispose()`. The selected signal therefore covers startup and live execution, while disposal guarantees quiescence on success, failure, and abort. + +A non-`completed` stop reason becomes an `isError` tool result; partial child output is never reported as success. The current tool blocks the parent turn until collection finishes; background and polling modes are deferred. + +## Config + +| Key | Meaning | |---|---| -| `provider` (required) | The `ctx.subagents` provider name to start runs on (`spawn`, `fork`, `acp`, …). | -| `toolName` | The model-facing tool name to register (default `subagent`). Set a distinct value per load when exposing multiple providers, e.g. `subagent` + `subagent_acp`. | -| `agentOptions` | Default per-child `{ model? }` applied to every spawned child. (No per-child persona: the deployment persona is a context-wide section every agent shares.) | +| `provider` | Required `ctx.subagents` provider name. | +| `toolName` | Model-facing tool name (default `subagent`). Must be unique per plugin instance. | +| `agentOptions` | Default child agent options, currently including `model`. | +| `persona` | Per-child persona; requires provider `persona` capability. | +| `toolFilter` | Per-child global-tool restriction; requires provider `toolFilter` capability. | +| `maxDepth` | Absolute delegation-depth cap; requires provider `depthLimit` capability. | -## Lifecycle (synchronous collect) - -`execute` starts a run on the configured provider and **awaits `run.result` inside a `try/finally` that always `dispose()`s the run** — the owned child agent/session is torn down on every path (success, error, abort), never leaked. The tool's abort signal (`exec.signal`) is bridged to `run.cancel()`. A non-`completed` stop reason (aborted/error/max-tokens/refusal) maps to an `isError` tool result rather than returning partial output as success. - -Background / poll collection is deferred (see the [RFC](../../../docs/rfc/implemented/feature/2026-06-21-subagent-capability-seam.md)); this cut blocks the parent turn until the child finishes. +`toolFilter` changes the child's visible global tool layer; it is not a parent-derived authority ceiling. See the [agent-scope security non-goal](../../../docs/rfc/implemented/architecture/2026-07-08-agent-scope-contexts.md#security-and-authority-are-explicit-non-goals). diff --git a/packages/subagent/tool-subagent/src/index.ts b/packages/subagent/tool-subagent/src/index.ts index f48ef4345e..d07e6726dd 100644 --- a/packages/subagent/tool-subagent/src/index.ts +++ b/packages/subagent/tool-subagent/src/index.ts @@ -11,10 +11,12 @@ * — there is no provider/type parameter in the model-facing schema. The model * sees only `{ description, prompt }`. * - * The tool DESCRIPTION is derived from the bound provider's context contract - * ({@link providerWording}): a fresh-context provider (spawn, ACP) gets the - * standalone-prompt wording, an inheriting provider (fork) tells the model the - * child already sees the conversation's completed turns. The tool MIRRORS the + * The tool DESCRIPTION is derived from the bound provider's conversation-history + * descriptor ({@link providerWording}): a fresh-conversation provider (spawn, + * ACP) gets the standalone-prompt wording, while a seeded-conversation provider + * (fork) tells the model the child already sees the conversation's completed + * turns. This descriptor says nothing about Cordis scope, services, tools, or + * authority. The tool MIRRORS the * provider's lifecycle via `subagent/provider-added`/`-removed` — it registers * when the provider is (or becomes) available and unregisters when the * provider goes away — so no load-order requirement exists and an HMR reload @@ -35,6 +37,7 @@ import z from 'schemastery' import { defineTool } from '@deepseek-ai/dsh-tools' import type { AgentOptions } from '@deepseek-ai/dsh-agent' import type { ContentBlock } from '@deepseek-ai/dsh-llm' +import { assertSubagentMaxDepth } from '@deepseek-ai/dsh-subagent' import type { SubagentProvider, SubagentResult, SubagentRun, SubagentStartRequest } from '@deepseek-ai/dsh-subagent' export const name = 'tool-subagent' @@ -54,19 +57,69 @@ export interface Config { toolName?: string /** * Default per-child agent options (model) applied to every spawned child. - * Omitted fields fall back to the child loop's own defaults. There is no - * per-child persona: the deployment persona (the system-prompt plugin's - * `persona` config) is a context-wide section every agent shares. + * Omitted fields fall back to the child loop's own defaults. */ agentOptions?: AgentOptions + /** + * Per-child persona applied to every child this tool spawns: a scoped + * `deployment:persona` section shadowing the deployment's persona for the + * child alone. Requires the bound provider's `persona` capability + * (in-process backends support it; a request against one that doesn't is + * rejected at start). Omitted ⇒ the child renders the deployment persona. + */ + persona?: string + /** + * Tool scoping applied to every child this tool spawns (see + * `SubagentStartRequest.toolFilter`): the named global tools vanish from + * the child's prompt AND refuse to execute. Requires the provider's + * `toolFilter` capability. Unknown names fail the spawn loudly. Note the + * child otherwise sees every global tool — including this delegation tool + * itself; `deny`-listing it (or setting `maxDepth`) is how a deployment + * bounds recursion. + */ + toolFilter?: { + /** Global tool names the child keeps; everything else is removed. */ + allow?: string[] + /** Global tool names removed from the child. */ + deny?: string[] + } + /** + * Recursion cap applied to every child this tool spawns (see + * `SubagentStartRequest.maxDepth`): a spawn whose child would sit deeper + * than this in the delegation tree is rejected. Requires the provider's + * `depthLimit` capability. Must be a non-negative safe integer and is + * validated when the plugin loads. Omitted ⇒ unbounded (bound it in + * deployments that expose this tool to children). + */ + maxDepth?: number } export const Config: z = z.object({ provider: z.string().required(), toolName: z.string().default('subagent'), + // Omitted-object discipline (see the toolFilter note below): without the + // forced default an omitted `agentOptions` materializes `{}`, which reads as + // present — the request would carry `agentOptions: {}` and the presence + // check in execute() could never be false through config. agentOptions: z.object({ model: z.string(), - }), + }).default(undefined as unknown as { model: string }), + persona: z.string(), + // A schemastery object materializes {} (with [] for nested arrays) when the + // key is omitted — for toolFilter that would mean an EMPTY ALLOW-LIST, i.e. + // deny-everything, silently. Force the omitted key to stay absent (the same + // shape discipline as SystemPrompt's toolOrder); the cast is needed because + // .default() expects the object type. + // The NESTED arrays get the same treatment as the object itself: a partial + // filter ({deny: […]}) must not materialize allow: [] beside it — an empty + // allow-list means deny-EVERYTHING, so the materialized default would turn + // a deny-one config into deny-all. An EXPLICIT allow: [] (grant-only + // children) survives, since only the omitted key defaults to undefined. + toolFilter: z.object({ + allow: z.array(z.string()).default(undefined as unknown as string[]), + deny: z.array(z.string()).default(undefined as unknown as string[]), + }).default(undefined as unknown as { allow: string[]; deny: string[] }), + maxDepth: z.natural().max(Number.MAX_SAFE_INTEGER), }) /** @@ -103,16 +156,19 @@ function stopReasonError(result: SubagentResult): string | undefined { } /** - * Model-facing wording per context contract ({@link SubagentProvider.inheritsParentContext}). + * Model-facing wording from the provider's conversation-history descriptor + * ({@link SubagentProvider.inheritsParentContext}). * A fresh child needs a standalone prompt; a forked child already sees the * conversation's completed turns — telling the model to restate everything * (or, worse, that the child "does not see this conversation") would be false * for a fork. Exported for tests. - * @param inherits - the bound provider's context contract. + * @param inheritsConversation - whether the child's conversation is seeded + * with the parent's completed turns; this says nothing about tool, service, + * scope, or authority inheritance. * @returns the tool `description` and the `prompt` parameter description. */ -export function providerWording(inherits: boolean): { description: string; promptDescription: string } { - if (inherits) { +export function providerWording(inheritsConversation: boolean): { description: string; promptDescription: string } { + if (inheritsConversation) { return { description: 'Delegate a task to a subagent that INHERITS this conversation: a child agent seeded with all ' @@ -139,6 +195,15 @@ export function providerWording(inherits: boolean): { description: string; promp } export function apply(ctx: Context, config: Config): void { + // Keep misconfiguration at plugin load even when a caller invokes apply() + // directly and bypasses Schemastery's natural/max metadata. + assertSubagentMaxDepth(config.maxDepth) + // Misconfiguration fails loud AT LOAD (the check is self-contained): an + // explicit `toolFilter: {}` would otherwise pass the capability gate and + // kill every delegation later, in the child-setup `restrict({})` throw. + if (config.toolFilter !== undefined && config.toolFilter.allow === undefined && config.toolFilter.deny === undefined) { + throw new Error('tool-subagent: `toolFilter` is configured but names neither `allow` nor `deny` — remove the key or fill the filter') + } // The tool MIRRORS its provider's lifecycle instead of assuming load order: // the cordis Loader starts sibling entries concurrently, so "backend listed // first in cordis.yml" does not guarantee "provider registered first", and @@ -177,21 +242,14 @@ export function apply(ctx: Context, config: Config): void { const request: SubagentStartRequest = { prompt: [{ type: 'text', text: args.prompt }], parent, - ...exec.signal ? { signal: exec.signal } : {}, - ...config.agentOptions ? { agentOptions: config.agentOptions } : {}, + signal: exec.signal ?? new AbortController().signal, + ...config.agentOptions !== undefined ? { agentOptions: config.agentOptions } : {}, + ...config.persona !== undefined ? { persona: config.persona } : {}, + ...config.toolFilter !== undefined ? { toolFilter: config.toolFilter } : {}, + ...config.maxDepth !== undefined ? { maxDepth: config.maxDepth } : {}, } - const run: SubagentRun = ctx.subagents.start(config.provider, request) - - // Bridge the tool's abort signal to the run: if the parent step is - // aborted while the child is in flight, cancel the child too. - const onAbort = (): void => { run.cancel('parent step aborted') } - exec.signal?.addEventListener('abort', onAbort, { once: true }) - // `addEventListener` does NOT fire for a signal already aborted before this - // line, so a step cancelled before the tool ran would never reach the - // child. Cancel explicitly in that case — the bridge must honor an - // already-aborted signal, not lean on each provider re-checking it. - if (exec.signal?.aborted) run.cancel('parent step aborted') + const run: SubagentRun = await ctx.subagents.start(config.provider, request) try { const result = await run.result @@ -203,7 +261,6 @@ export function apply(ctx: Context, config: Config): void { } return [{ type: 'text', text: outputText(result.output) }] } finally { - exec.signal?.removeEventListener('abort', onAbort) // Always reach child quiescence — never leak a live idle child/session. await run.dispose() } diff --git a/packages/subagent/tool-subagent/tests/tool-subagent.spec.ts b/packages/subagent/tool-subagent/tests/tool-subagent.spec.ts index 611069ef76..5830513cf4 100644 --- a/packages/subagent/tool-subagent/tests/tool-subagent.spec.ts +++ b/packages/subagent/tool-subagent/tests/tool-subagent.spec.ts @@ -111,12 +111,11 @@ describe('dsh-tool-subagent', () => { await ctx.plugin(SubagentService) ctx.subagents.registerProvider({ name: 'weird', - capabilities: { outputSchema: false, depthLimit: false, toolFilter: false }, + capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false }, inheritsParentContext: false, - start: () => ({ + start: async () => ({ id: AgentId('weird-child'), result: Promise.resolve({ output: [{ type: 'text', text: 'partial' }], stopReason: 'frobnicated' as never }), - cancel() {}, dispose: async () => {}, }), }) @@ -137,14 +136,13 @@ describe('dsh-tool-subagent', () => { await ctx.plugin(SubagentService) ctx.subagents.registerProvider({ name: 'capture', - capabilities: { outputSchema: false, depthLimit: false, toolFilter: false }, + capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false }, inheritsParentContext: false, - start: (request) => { + start: async (request) => { seen = request return { id: AgentId('capture-child'), result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }), - cancel() {}, dispose: async () => {}, } }, @@ -167,14 +165,13 @@ describe('dsh-tool-subagent', () => { await ctx.plugin(SubagentService) ctx.subagents.registerProvider({ name: 'bare', - capabilities: { outputSchema: false, depthLimit: false, toolFilter: false }, + capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false }, inheritsParentContext: false, - start: (request) => { + start: async (request) => { seen = request return { id: AgentId('bare-child'), result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }), - cancel() {}, dispose: async () => {}, } }, @@ -217,7 +214,7 @@ describe('dsh-tool-subagent', () => { await ctx.plugin(SystemPrompt) await ctx.plugin(ToolRegistry) await ctx.plugin(SubagentService) - const backend = await ctx.plugin(mock, { name: 'mock' }) // spawn-shaped (inherits: false) + const backend = await ctx.plugin(mock, { name: 'mock' }) // fresh conversation (descriptor: false) await ctx.plugin(tool, { provider: 'mock' }) expect(ctx.tools.schemas().find(s => s.name === 'subagent')!.description).toContain('does not see this conversation') @@ -225,7 +222,7 @@ describe('dsh-tool-subagent', () => { await backend.dispose() expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(false) - // Backend reloads with a DIFFERENT contract: the wording is re-derived + // Backend reloads with a DIFFERENT conversation-history descriptor: the wording is re-derived // from the fresh provider, not served stale from the first mount. await ctx.plugin(mock, { name: 'mock', inheritsParentContext: true }) expect(ctx.tools.schemas().find(s => s.name === 'subagent')!.description).toContain('INHERITS this conversation') @@ -270,7 +267,7 @@ describe('dsh-tool-subagent', () => { expect(ctx.tools.schemas().some(s => s.name === 'subagent')).toBe(true) }) - it('derives spawn-shaped wording from a fresh-context provider (default mock)', async () => { + it('derives spawn-shaped wording from a fresh-conversation provider (default mock)', async () => { const ctx = await setup({ provider: 'mock' }) const schema = ctx.tools.schemas().find(s => s.name === 'subagent')! expect(schema.description).toContain('does not see this conversation') @@ -278,7 +275,7 @@ describe('dsh-tool-subagent', () => { expect(props['prompt']!.description).toContain('include everything it needs') }) - it('derives fork-shaped wording from an inheriting provider (the description stops lying)', async () => { + it('derives fork-shaped wording from a seeded-conversation provider (the description stops lying)', async () => { const ctx = await setup({ provider: 'mock', toolName: 'subagent' }, { inheritsParentContext: true }) const schema = ctx.tools.schemas().find(s => s.name === 'subagent')! expect(schema.description).toContain('INHERITS this conversation') @@ -297,12 +294,11 @@ describe('dsh-tool-subagent', () => { await ctx.plugin(SubagentService) ctx.subagents.registerProvider({ name: 'spy', - capabilities: { outputSchema: false, depthLimit: false, toolFilter: false }, + capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false }, inheritsParentContext: false, - start: () => ({ + start: async () => ({ id: AgentId('spy-child'), result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }), - cancel() {}, dispose: async () => void disposed(), }), }) @@ -320,12 +316,11 @@ describe('dsh-tool-subagent', () => { await ctx.plugin(SubagentService) ctx.subagents.registerProvider({ name: 'spy', - capabilities: { outputSchema: false, depthLimit: false, toolFilter: false }, + capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false }, inheritsParentContext: false, - start: () => ({ + start: async () => ({ id: AgentId('spy-child'), result: Promise.resolve({ output: [], stopReason: 'error' as const }), - cancel() {}, dispose: async () => void disposed(), }), }) @@ -336,7 +331,7 @@ describe('dsh-tool-subagent', () => { expect(disposed).toHaveBeenCalledTimes(1) }) - it('bridges the tool abort signal to run.cancel()', async () => { + it('passes the tool abort signal as the provider cancellation channel', async () => { const cancelled = vi.fn() const ctx = new Context() await ctx.plugin(SystemPrompt) @@ -344,18 +339,19 @@ describe('dsh-tool-subagent', () => { await ctx.plugin(SubagentService) ctx.subagents.registerProvider({ name: 'spy', - capabilities: { outputSchema: false, depthLimit: false, toolFilter: false }, + capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false }, inheritsParentContext: false, - start: () => { + start: async (request) => { + if (request.signal.aborted) throw new Error('start aborted') let resolveResult: (r: { output: never[]; stopReason: 'aborted' }) => void const result = new Promise<{ output: never[]; stopReason: 'aborted' }>((res) => { resolveResult = res }) + request.signal.addEventListener('abort', () => { + cancelled() + resolveResult({ output: [], stopReason: 'aborted' }) + }, { once: true }) return { id: AgentId('spy-child'), result, - cancel: () => { - cancelled() - resolveResult({ output: [], stopReason: 'aborted' }) - }, dispose: async () => {}, } }, @@ -364,12 +360,7 @@ describe('dsh-tool-subagent', () => { const controller = new AbortController() const pending = callSubagent(ctx, { description: 'd', prompt: 'p' }, { signal: controller.signal }) - // Abort AFTER the tool body has had a chance to register its abort listener - // (ctx.tools.execute now awaits the tools/pre-execute waterfall before the - // body runs, so the listener is not registered synchronously). A few - // microtask turns let execute() reach `addEventListener('abort')`, so this - // exercises the LIVE onAbort bridge — distinct from the already-aborted - // sync path the next test covers. + // Let provider.start install its listener before aborting. await Promise.resolve() await Promise.resolve() controller.abort() @@ -378,33 +369,19 @@ describe('dsh-tool-subagent', () => { expect(result.isError).toBe(true) }) - it('cancels the run when the tool signal is ALREADY aborted before execute (no missed abort)', async () => { - // `addEventListener('abort')` does not fire for a signal already aborted - // before the listener is added, so a step cancelled before the tool ran - // would never reach the child unless the bridge re-checks `signal.aborted`. - // A provider that leans only on the abort EVENT (this spy never inspects - // request.signal) proves the bridge itself must cancel. - const cancelled = vi.fn() + it('passes an already-aborted signal so provider startup rejects', async () => { + const sawAborted = vi.fn() const ctx = new Context() await ctx.plugin(SystemPrompt) await ctx.plugin(ToolRegistry) await ctx.plugin(SubagentService) ctx.subagents.registerProvider({ name: 'spy', - capabilities: { outputSchema: false, depthLimit: false, toolFilter: false }, + capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false }, inheritsParentContext: false, - start: () => { - let resolveResult: (r: { output: never[]; stopReason: 'aborted' }) => void - const result = new Promise<{ output: never[]; stopReason: 'aborted' }>((res) => { resolveResult = res }) - return { - id: AgentId('spy-child'), - result, - cancel: () => { - cancelled() - resolveResult({ output: [], stopReason: 'aborted' }) - }, - dispose: async () => {}, - } + start: async (request) => { + if (request.signal.aborted) sawAborted() + throw new Error('start aborted') }, }) await ctx.plugin(tool, { provider: 'spy' }) @@ -412,7 +389,7 @@ describe('dsh-tool-subagent', () => { const controller = new AbortController() controller.abort() // already aborted BEFORE the tool runs const result = await callSubagent(ctx, { description: 'd', prompt: 'p' }, { signal: controller.signal }) - expect(cancelled).toHaveBeenCalledTimes(1) + expect(sawAborted).toHaveBeenCalledTimes(1) expect(result.isError).toBe(true) }) @@ -451,4 +428,130 @@ describe('dsh-tool-subagent', () => { expect(typeof unwrapped.apply).toBe('function') expect(unwrapped.Config).toBeDefined() }) + + it('passes persona/toolFilter/maxDepth config through to the start request', async () => { + let seen: { persona?: string; toolFilter?: unknown; maxDepth?: number } | undefined + const ctx = new Context() + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(SubagentService) + ctx.subagents.registerProvider({ + name: 'capture2', + capabilities: { outputSchema: false, depthLimit: true, toolFilter: true, persona: true }, + inheritsParentContext: false, + start: async (request) => { + seen = request + return { + id: AgentId('capture2-child'), + result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }), + dispose: async () => {}, + } + }, + }) + await ctx.plugin(tool, { + provider: 'capture2', + persona: 'You are the child.', + toolFilter: { deny: ['subagent'] }, + maxDepth: 2, + }) + + await callSubagent(ctx, { description: 'd', prompt: 'p' }) + expect(seen?.persona).toBe('You are the child.') + expect(seen?.toolFilter).toMatchObject({ deny: ['subagent'] }) + expect(seen?.maxDepth).toBe(2) + }) + + it.each([ + { label: 'null', value: null as unknown as number }, + { label: 'a string', value: '1' as unknown as number }, + { label: 'NaN', value: Number.NaN }, + { label: 'positive infinity', value: Number.POSITIVE_INFINITY }, + { label: 'negative infinity', value: Number.NEGATIVE_INFINITY }, + { label: 'a negative integer', value: -1 }, + { label: 'a fractional number', value: 1.5 }, + { label: 'negative zero', value: -0 }, + { label: 'an unsafe integer', value: Number.MAX_SAFE_INTEGER + 1 }, + ])('rejects maxDepth=$label when the plugin loads', async ({ value }) => { + await expect(setup({ provider: 'mock', maxDepth: value })) + .rejects.toThrow() + }) + + it('validates maxDepth when apply() is invoked directly without Schemastery', () => { + const ctx = new Context() + expect(() => { + tool.apply(ctx, { + provider: 'unused', + maxDepth: Number.NaN, + }) + }).toThrow('subagent maxDepth must be a non-negative safe integer') + }) + + it('a partial toolFilter (deny only) does not materialize an empty allow-list (deny-all trap)', async () => { + let seen: { toolFilter?: { readonly allow?: readonly string[]; readonly deny?: readonly string[] } } | undefined + const ctx = new Context() + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(SubagentService) + ctx.subagents.registerProvider({ + name: 'capture3', + capabilities: { outputSchema: false, depthLimit: false, toolFilter: true, persona: false }, + inheritsParentContext: false, + start: async (request) => { + seen = request + return { + id: AgentId('capture3-child'), + result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }), + dispose: async () => {}, + } + }, + }) + await ctx.plugin(tool, { provider: 'capture3', toolFilter: { deny: ['subagent'] } }) + await callSubagent(ctx, { description: 'd', prompt: 'p' }) + expect(seen?.toolFilter).toEqual({ deny: ['subagent'] }) + expect(seen?.toolFilter).not.toHaveProperty('allow') + }) + + it('an omitted agentOptions does not materialize an empty object onto the request', async () => { + // Same schemastery trap as toolFilter, adjacent field: an omitted + // `agentOptions` config key materializes `{}` without the forced default, + // which reads as present and puts a dishonest `agentOptions: {}` on every + // start request. + let seen: { agentOptions?: unknown } | undefined + const ctx = new Context() + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(SubagentService) + ctx.subagents.registerProvider({ + name: 'capture4', + capabilities: { outputSchema: false, depthLimit: false, toolFilter: false, persona: false }, + inheritsParentContext: false, + start: async (request) => { + seen = request + return { + id: AgentId('capture4-child'), + result: Promise.resolve({ output: [{ type: 'text', text: 'ok' }], stopReason: 'completed' as const }), + dispose: async () => {}, + } + }, + }) + await ctx.plugin(tool, { provider: 'capture4' }) + await callSubagent(ctx, { description: 'd', prompt: 'p' }) + expect(seen).toBeDefined() + expect(seen).not.toHaveProperty('agentOptions') + }) + + it('an explicit empty toolFilter fails at plugin load, not at first delegation', async () => { + const ctx = new Context() + await ctx.plugin(SystemPrompt) + await ctx.plugin(ToolRegistry) + await ctx.plugin(SubagentService) + ctx.subagents.registerProvider({ + name: 'p', + capabilities: { outputSchema: false, depthLimit: false, toolFilter: true, persona: false }, + inheritsParentContext: false, + start: () => { throw new Error('unreachable') }, + }) + const fiber = ctx.plugin(tool, { provider: 'p', toolFilter: {} }) + await expect(fiber).rejects.toThrow(/names neither `allow` nor `deny`/) + }) }) diff --git a/packages/support/README.md b/packages/support/README.md index 2a08063bad..c8883a89ff 100644 --- a/packages/support/README.md +++ b/packages/support/README.md @@ -5,7 +5,7 @@ Packages that exist to serve development, testing, and the examples rather than | Package | Role | ctx key | |---|---|---| | `acp-snapshot/` | ACP snapshot suite kit: subprocess scenario harness + golden normalizers + the `defineAcpSnapshotSuite` factory | (library — imported by example `*.snapshot.ts` suites) | -| `invariants/` | Dev-mode event-contract invariants + session-log freeze | (listens on `session/*`, `agent/*`) | +| `invariants/` | Dev-mode event-contract assertions | (listens on `session/*`, `agent/*`) | | `llm-replay/` | Record/replay adapter: short-circuits `llm/stream` from a recorded session JSONL (keyless snapshot tests) | (listens on `llm/stream`) | | `subagent-mock/` | Scripted `SubagentProvider` for deterministic seam/tool tests | (registers on `ctx.subagents`) | diff --git a/packages/support/acp-snapshot/README.md b/packages/support/acp-snapshot/README.md index 209b1a81cb..501b98b793 100644 --- a/packages/support/acp-snapshot/README.md +++ b/packages/support/acp-snapshot/README.md @@ -5,8 +5,8 @@ The ACP snapshot suite kit: the shared machinery behind the keyless snapshot tie Three layers, importable separately: - **`runScenario` (harness)** — boots the real agent bin as a subprocess via tsx (unbuilt, Loader path), drives it over ACP JSON-RPC stdio from a deterministic `input.json` script, tees raw stdout for the golden + purity check, and harvests every persisted session JSONL (parent + subagent children, primary-first) after a graceful stdin-EOF shutdown. Parameterized by `AgentUnderTest` (`binScript`, `configPath`, `tsconfigPath` — absolute paths; the subprocess cwd is a temp dir outside the repo). -- **Normalizers** — pure functions turning the two captured surfaces into stable text: `normalizeStdout` (JSON-RPC ids → first-seen sequence; UUIDs/cwd → tokens; doubles as the stdout-purity check), `normalizeSessionLog` (times zeroed, `seq` kept), and the composable `scrubRequestHeaders` (header bulk → `{{system}}`/`{{tools}}`, structure kept — [pinned-header RFC](../../../docs/rfc/implemented/testing/2026-07-06-pin-request-header-content-in-one-scenario.md)). -- **`defineAcpSnapshotSuite` (factory)** — registers the whole describe/it tree for a scenario table: per-scenario golden + re-persisted-log compares, record-mode fixture write-back, the per-header-class pin with its live uniformity guard, and the fixture guard block (no orphan scenario dirs, required files present, exactly one pin per class, pinning fixtures well-formed, non-pinning fixtures header-scrubbed). Must be called at vitest collection time. +- **Normalizers** — pure functions turning the two captured surfaces into stable text: `normalizeStdout` (JSON-RPC ids → first-seen sequence; UUIDs/cwd → tokens; doubles as the stdout-purity check), `normalizeSessionLog` (times zeroed, `seq` kept), `scrubSystemPrompts` (prompt text → `{{system}}` in every JSONL), and `scrubRequestHeaders` (the remaining header bulk → `{{tools}}`/`{{messagePrefix}}` outside each pin, structure kept — [pinned-header RFC](../../../docs/rfc/implemented/testing/2026-07-06-pin-request-header-content-in-one-scenario.md)). +- **`defineAcpSnapshotSuite` (factory)** — registers the whole describe/it tree for a scenario table: per-scenario golden + re-persisted-log compares, record/refresh fixture write-back, the per-header-class pin (`system-prompt.golden.md` plus the JSONL's full tool schemas) with its live uniformity guard, and the fixture guard block (no orphan scenario dirs, required files present, exactly one pin per class, every JSONL prompt-scrubbed, non-pinning fixtures fully header-scrubbed). Must be called at vitest collection time. A consuming `*.snapshot.ts` is the scenario table plus one factory call: @@ -27,12 +27,16 @@ defineAcpSnapshotSuite({ }, snapshotsDir: join(dirname(fileURLToPath(import.meta.url)), 'snapshots'), scenarios: SCENARIOS, // exactly one entry per header class sets pinsHeader - mode: process.env.DSH_SNAPSHOT === 'record' ? 'record' : 'replay', + mode: process.env.DSH_SNAPSHOT === 'record' + ? 'record' + : process.env.DSH_SNAPSHOT === 'refresh' + ? 'refresh' + : 'replay', }) ``` -A scenario booting a differently-composed tree sets its own `configPath` (an overlay whose basename still ends in `cordis.yml`, so the bin's replay swap finds the sibling `*cordis.snapshot.yml`) and, when that composition changes the request header, its own `headerClass` with its own pinning scenario — the acp-agent example's Code Mode scenarios are the template. +A scenario booting a differently-composed tree sets its own `configPath` (an overlay whose basename still ends in `cordis.yml`, so the bin's replay swap finds the sibling `*cordis.snapshot.yml`) and, when that composition changes the request header, its own `headerClass` with its own pinning scenario — the acp-agent example's Code Mode scenarios are the template. Each pinning directory's generated `system-prompt.golden.md` is the reviewable snapshot of the normalized composed prompt; `session.jsonl` stores `"system":"{{system}}"` while retaining the complete tool list. -The example also ships a `cordis.snapshot.yml` replay overlay next to its `cordis.yml` (the bin swaps them under `DSH_SNAPSHOT=replay` — [single-source replay config RFC](../../../docs/rfc/implemented/testing/2026-07-04-single-source-acp-replay-config.md)); replay fixtures are served by [`dsh-llm-replay`](../llm-replay/README.md), which this package points at via the `DSH_SNAPSHOT_*` env vars it sets on the child. Fixture roles, record/replay semantics, and scenario-table fields are documented on `Scenario` and in the [snapshot RFC](../../../docs/rfc/implemented/testing/2026-06-19-acp-snapshot-tests.md). +The example also ships a `cordis.snapshot.yml` replay overlay next to its `cordis.yml` (the bin swaps them under `DSH_SNAPSHOT=replay` — [single-source replay config RFC](../../../docs/rfc/implemented/testing/2026-07-04-single-source-acp-replay-config.md)); replay fixtures are served by [`dsh-llm-replay`](../llm-replay/README.md), which this package points at via the `DSH_SNAPSHOT_*` env vars it sets on the child. `pnpm run test:snapshot:record` calls the live LLM and rewrites the recorded scenarios' model fixtures; `pnpm run test:snapshot:refresh` stays keyless, runs the replay overlay, and rewrites stdout, comparable session-log goldens, and each pin's Markdown prompt snapshot from the committed model scripts. Fixture roles, record/replay/refresh semantics, and scenario-table fields are documented on `Scenario` and in the [snapshot RFC](../../../docs/rfc/implemented/testing/2026-06-19-acp-snapshot-tests.md). -Constraints: `suite.ts` imports vitest, so the package is importable only inside a vitest run (the harness and normalizers have no such dependency but ship from the same entry). ACP-specific by design — the harness speaks the SDK's `ClientSideConnection`. Permission round-trips are scriptable: `InputScript.permissionAnswers` is a FIFO queue of option-kind selections (`allow_once`, `reject_once`, …) the client maps to the agent-issued `optionId` at answer time; an absent or exhausted queue answers `cancelled`, and a kind the request never offered rejects the run (the agent is answered `cancelled`, so a tolerant agent cannot absorb the scenario bug). +Constraints: `suite.ts` imports vitest, so the package is importable only inside a vitest run (the harness and normalizers have no such dependency but ship from the same entry). ACP-specific by design — the harness speaks the SDK's `ClientSideConnection`. Permission round-trips are scriptable: `InputScript.permissionAnswers` is a FIFO queue of option-kind selections (`allow_once`, `reject_once`, …) the client maps to the agent-issued `optionId` at answer time; an absent or exhausted queue answers `cancelled`, and a kind the request never offered rejects the run (the agent is answered `cancelled`, so a tolerant agent cannot absorb the scenario bug). Session config options are scriptable too: the `setConfigOption` step switches a knob over `session/set_config_option`, and `setConfigOptionExpectError` asserts the bridge rejects an unknown id or out-of-vocabulary value (the error frame stays in the transcript). diff --git a/packages/support/acp-snapshot/src/harness.ts b/packages/support/acp-snapshot/src/harness.ts index c0f5788fca..491d3ea884 100644 --- a/packages/support/acp-snapshot/src/harness.ts +++ b/packages/support/acp-snapshot/src/harness.ts @@ -85,6 +85,8 @@ export type InputStep = | { op: 'promptExpectError'; text: string } | { op: 'promptAndCancel'; text: string } | { op: 'cancel' } + | { op: 'setConfigOption'; configId: string; value: string } + | { op: 'setConfigOptionExpectError'; configId: string; value: string } /** A scenario's `input.json`: an ordered list of input steps. */ export interface InputScript { @@ -210,6 +212,8 @@ export async function runScenario(input: InputScript, opts: RunOptions): Promise DSH_SNAPSHOT: opts.mode, DSH_SNAPSHOT_FILE: opts.fixtureFile, DSH_SNAPSHOT_SESSIONS_ROOT: sessionsRoot, + DSH_HOME: join(cwd, '.dsh'), + DSH_AGENTS_HOME: join(cwd, '.agents'), ...opts.overrideFile !== undefined ? { DSH_SNAPSHOT_OVERRIDE: opts.overrideFile } : {}, ...opts.childFiles !== undefined && opts.childFiles.length > 0 ? { DSH_SNAPSHOT_CHILD_FILES: opts.childFiles.join(delimiter) } @@ -406,6 +410,24 @@ async function runStep( await client.cancel({ sessionId }) return } + case 'setConfigOption': { + const sessionId = getSessionId() + if (sessionId === undefined) throw new Error('snapshot-harness: setConfigOption before newSession') + await client.setSessionConfigOption({ sessionId, configId: step.configId, value: step.value }) + return + } + case 'setConfigOptionExpectError': { + const sessionId = getSessionId() + if (sessionId === undefined) throw new Error('snapshot-harness: setConfigOptionExpectError before newSession') + // The bridge rejects an unknown id / out-of-vocabulary value; the SDK + // surfaces that as a rejected RPC — swallow it so the run completes and + // the error frame is captured in the transcript. + await client.setSessionConfigOption({ sessionId, configId: step.configId, value: step.value }).then( + () => { throw new Error('snapshot-harness: expected set_config_option to be rejected but it succeeded') }, + () => { /* expected: the bridge rejected the id or value */ }, + ) + return + } default: throw new Error(`snapshot-harness: unknown input op ${JSON.stringify(step)}`) } diff --git a/packages/support/acp-snapshot/src/index.ts b/packages/support/acp-snapshot/src/index.ts index bbe74030f2..74bee95385 100644 --- a/packages/support/acp-snapshot/src/index.ts +++ b/packages/support/acp-snapshot/src/index.ts @@ -3,7 +3,7 @@ * tier (`pnpm run test:snapshot`). Three layers, composable per example: * the subprocess scenario harness ({@link runScenario}), the pure golden * normalizers ({@link normalizeStdout} / {@link normalizeSessionLog} / - * {@link scrubRequestHeaders}), and the suite factory + * {@link scrubRequestHeaders} / {@link scrubSystemPrompts}), and the suite factory * ({@link defineAcpSnapshotSuite}) that registers a scenario table as a full * describe/it tree. An example's `*.snapshot.ts` supplies only its * {@link AgentUnderTest} paths, its snapshots directory, and its @@ -29,6 +29,7 @@ export { normalizeSessionLog, normalizeStdout, scrubRequestHeaders, + scrubSystemPrompts, type NormalizeContext, } from './normalize.ts' export { diff --git a/packages/support/acp-snapshot/src/normalize.ts b/packages/support/acp-snapshot/src/normalize.ts index 017dd504b3..a68fcf83d3 100644 --- a/packages/support/acp-snapshot/src/normalize.ts +++ b/packages/support/acp-snapshot/src/normalize.ts @@ -12,14 +12,14 @@ * `durationMs` (wall-clock hook runtime) → 0. NOT scrubbed: the log's `seq` * (deterministic — `seq = log.length`, part of the event-log contract). * - * A separate, composable normalizer — {@link scrubRequestHeaders} — replaces - * the bulky request-header CONTENT (the composed system prompt, the tool - * schema list, and the session prefix) with - * `{{system}}`/`{{tools}}`/`{{messagePrefix}}` tokens. It is deliberately NOT - * folded into {@link normalizeSessionLog}: each suite's one header-pinning - * scenario compares that content verbatim, every other scenario composes the - * scrub in (the `pinsHeader` flag on the scenario table, consumed by the suite - * factory in ./suite.ts; see the pinned-header RFC, + * Separate, composable normalizers keep bulky request-header content out of + * session fixtures. {@link scrubSystemPrompts} replaces the composed system + * prompt in EVERY fixture; {@link scrubRequestHeaders} additionally replaces + * tool schemas and the session prefix outside each suite's header-pinning + * scenario. They are deliberately NOT folded into + * {@link normalizeSessionLog}: the suite factory composes the right scrub for + * each scenario and snapshots the pin's actual prompt as Markdown (see the + * pinned-header RFC, * docs/rfc/implemented/testing/2026-07-06-pin-request-header-content-in-one-scenario.md). * * See docs/rfc/implemented/testing/2026-06-19-acp-snapshot-tests.md. @@ -135,37 +135,37 @@ export function normalizeSessionLog(rawLog: string, ctx: NormalizeContext): stri } /** - * Replace request-header CONTENT in a session JSONL with stable tokens, - * keeping its structure: a `request/header` event's `data.header.system` → - * `{{system}}`, `data.header.tools` → `{{tools}}`, and - * `data.header.messagePrefix` → one `{{messagePrefix}}` token per message - * (the session prefix is model-visible bulk — an AGENTS digest, a skills - * catalog — so its COUNT stays a structural fact while its text never lands - * in a fixture); a - * `request/header-delta` event keeps every structural fact — the system - * delta's `keepStart`/`keepEnd` line positions and inserted-line COUNT (one - * `{{system}}` token per inserted line), the tools delta's - * added/removed/changed tool NAMES, the prefix replacement's message COUNT — - * and tokenizes only the bulk (prompt - * text; each added/changed schema's fields other than `name` → `{{tools}}`; - * each replacement prefix message → `{{messagePrefix}}`), - * so two different deltas still compare different. - * Absent fields stay absent — WHETHER a header carried a system prompt, - * tools, or a prefix is behavior and stays visible; `config` and `reason` - * are small and - * stable, so they stay verbatim (a model swap churns every fixture by design - * — it invalidates the recorded responses; a prompt/schema edit churns none — - * replay never reads this content, see dsh-llm-replay). - * - * Only lines with something to scrub are re-serialized; every other line - * passes through byte-for-byte, so the transform is idempotent and applying - * it to an already-scrubbed fixture is a no-op — the on-disk-fixtures guard - * in ./suite.ts relies on exactly that. + * Replace system-prompt content in request headers and header deltas with + * `{{system}}` tokens while retaining field presence and delta structure. + * Other header content stays verbatim, so a header-pinning fixture can keep + * its complete tool schemas while every JSONL fixture omits the prompt text. + * Lines without a system payload pass through byte-for-byte; the transform is + * idempotent. * * @param rawLog The raw session `.jsonl` content. - * @returns The JSONL with header content tokenized, other lines byte-identical. + * @returns The JSONL with system-prompt content tokenized. + */ +export function scrubSystemPrompts(rawLog: string): string { + return scrubHeaderContent(rawLog, false) +} + +/** + * Replace all bulky request-header content in a session JSONL with stable + * tokens. This includes the system-prompt fields handled by + * {@link scrubSystemPrompts}, tool schemas, and session-prefix messages. It + * keeps system-delta line positions and arity, tool-delta names, prefix + * message counts, field presence, config, and reason. Lines without content + * to scrub pass through byte-for-byte, and the transform is idempotent. + * + * @param rawLog The raw session `.jsonl` content. + * @returns The JSONL with all header bulk tokenized, other lines byte-identical. */ export function scrubRequestHeaders(rawLog: string): string { + return scrubHeaderContent(rawLog, true) +} + +/** Transform header content, optionally including tool schemas and the session prefix. */ +function scrubHeaderContent(rawLog: string, scrubToolsAndPrefix: boolean): string { const lines = rawLog.split('\n') const out = lines.map((line) => { if (line.trim().length === 0) return line @@ -175,11 +175,14 @@ export function scrubRequestHeaders(rawLog: string): string { if (record.type === 'request/header') { const header = data.header as Record | null | undefined if (header === null || typeof header !== 'object') return line - if (!('system' in header) && !('tools' in header) && !('messagePrefix' in header)) return line - if ('system' in header) header.system = SYSTEM - if ('tools' in header) header.tools = TOOLS - if (Array.isArray(header.messagePrefix)) header.messagePrefix = header.messagePrefix.map(() => MESSAGE_PREFIX) - return JSON.stringify(record) + let touched = false + if ('system' in header) { header.system = SYSTEM; touched = true } + if (scrubToolsAndPrefix && 'tools' in header) { header.tools = TOOLS; touched = true } + if (scrubToolsAndPrefix && Array.isArray(header.messagePrefix)) { + header.messagePrefix = header.messagePrefix.map(() => MESSAGE_PREFIX) + touched = true + } + return touched ? JSON.stringify(record) : line } if (record.type === 'request/header-delta') { let touched = false @@ -189,11 +192,11 @@ export function scrubRequestHeaders(rawLog: string): string { touched = true } const tools = data.tools as Record | null | undefined - if (tools !== null && typeof tools === 'object') { + if (scrubToolsAndPrefix && tools !== null && typeof tools === 'object') { if (Array.isArray(tools.added)) { tools.added = tools.added.map(scrubToolSchema); touched = true } if (Array.isArray(tools.changed)) { tools.changed = tools.changed.map(scrubToolSchema); touched = true } } - if (Array.isArray(data.messagePrefix)) { + if (scrubToolsAndPrefix && Array.isArray(data.messagePrefix)) { data.messagePrefix = data.messagePrefix.map(() => MESSAGE_PREFIX) touched = true } diff --git a/packages/support/acp-snapshot/src/suite.ts b/packages/support/acp-snapshot/src/suite.ts index 53e05d7876..418470f2be 100644 --- a/packages/support/acp-snapshot/src/suite.ts +++ b/packages/support/acp-snapshot/src/suite.ts @@ -10,21 +10,23 @@ * (recorded scenarios) and the expected produced log (both sides normalized * before comparing). * - * Request-header content (the composed system prompt + tool schemas riding on - * `request/header` events) is pinned by exactly ONE scenario per HEADER CLASS - * — scenarios that boot the same config compose the same header; each class's - * `pinsHeader` scenario commits it verbatim — and scrubbed to - * `{{system}}`/`{{tools}}` tokens in every other fixture and compare, so a - * prompt or tool-schema edit churns one committed line per class instead of - * every fixture. A per-run uniformity guard keeps each pin sound: every live - * header must equal its class's pinned one, and no header-delta may appear - * outside a pinning scenario (see the pinned-header RFC, + * Request-header content is pinned by exactly ONE scenario per HEADER CLASS — + * scenarios that boot the same config compose the same header. Every JSONL + * fixture scrubs the system prompt to `{{system}}`; each class's pinning + * scenario stores the readable prompt in `system-prompt.golden.md` and keeps its full + * tool schemas in `session.jsonl`, while every other fixture also scrubs tools + * to `{{tools}}`. A per-run uniformity guard compares both artifacts against + * every live header and forbids unrepresented header deltas (see the + * pinned-header RFC, * docs/rfc/implemented/testing/2026-07-06-pin-request-header-content-in-one-scenario.md). * * `pnpm run test:snapshot:record` (DSH_SNAPSHOT=record + -u) re-records the * `session.jsonl` fixtures against the real API and refreshes the stdout golden - * in one pass; the caller resolves that env into {@link SnapshotSuiteOptions} - * (env reading stays at the suite edge, not in this library). + * in one pass. `pnpm run test:snapshot:refresh` (DSH_SNAPSHOT=refresh) instead + * replays the committed model scripts keylessly and writes the current stdout + * + persisted-log goldens back without calling a live LLM. The caller resolves + * that env into {@link SnapshotSuiteOptions} (env reading stays at the suite + * edge, not in this library). * * @module @deepseek-ai/dsh-acp-snapshot/suite */ @@ -34,7 +36,16 @@ import { existsSync } from 'node:fs' import { join } from 'node:path' import { describe, expect, it } from 'vitest' import { type AgentUnderTest, type HarvestedLog, type InputScript, runScenario } from './harness.ts' -import { type NormalizeContext, normalizeSessionLog, normalizeStdout, scrubRequestHeaders } from './normalize.ts' +import { + type NormalizeContext, + normalizeSessionLog, + normalizeStdout, + scrubRequestHeaders, + scrubSystemPrompts, +} from './normalize.ts' + +/** The readable system-prompt snapshot beside each header-pinning fixture. */ +const SYSTEM_PROMPT_SNAPSHOT = 'system-prompt.golden.md' /** A snapshot scenario and how its fixtures are produced. */ export interface Scenario { @@ -78,14 +89,13 @@ export interface Scenario { */ childSessions?: number /** - * Whether THIS scenario's fixtures keep the full request-header content (the - * composed system prompt and tool schema list on `request/header` / - * `request/header-delta` events) and compare it verbatim. Exactly one - * scenario per HEADER CLASS ({@link headerClass}) pins it; every other - * scenario of that class stores and compares that content as - * `{{system}}`/`{{tools}}` tokens ({@link scrubRequestHeaders}), - * so a system prompt or tool-schema change shows up as ONE committed-fixture - * diff per class, not one per scenario. One pin per class suffices because + * Whether THIS scenario pins its header class's model-facing request-header + * content. Its actual composed prompt is maintained as a readable + * `system-prompt.golden.md`; its JSONL keeps full tool schemas but stores the prompt + * as `{{system}}`. Every other scenario of the class stores tools as + * `{{tools}}` too ({@link scrubRequestHeaders}). A prompt or tool-schema + * change therefore shows up in one focused artifact per class, not every + * session fixture. One pin per class suffices because * header composition is class-uniform (parent, spawn child, and fork child * all compose the same prompt-modulo-cwd and the same tools) — and that * premise is ASSERTED, not assumed: every non-pinning run's live headers @@ -95,6 +105,15 @@ export interface Scenario { * Defaults to false. */ pinsHeader?: boolean + /** + * How many `request/header-delta` events this PINNING scenario's fixture + * legitimately carries (default 0). A recorded mid-run header change — a + * config-option switch rewriting a prompt section — is part of the pinned + * surface, with readable prompt text in Markdown; any OTHER count + * still fails, so fixture rot stays caught. Meaningless off the pin (the + * live uniformity guard keeps non-pinning scenarios delta-free). + */ + expectedHeaderDeltas?: number /** * Which header-composition class this scenario belongs to. Scenarios that * boot the same config compose the same header; each class has exactly one @@ -121,15 +140,16 @@ export interface SnapshotSuiteOptions { agent: AgentUnderTest /** Absolute path of the suite's `snapshots/` directory (one subdir per scenario). */ snapshotsDir: string - /** The scenario table; exactly one entry must set `pinsHeader`. */ + /** The scenario table; exactly one entry per header class must set `pinsHeader`. */ scenarios: Scenario[] /** - * `replay` (keyless, the default tier) or `record` (live API; re-records the - * `recorded` scenarios' fixtures and refreshes the vitest goldens under - * `--update`). The caller derives this from `$DSH_SNAPSHOT` — env reading - * stays outside this library. + * `replay` (keyless, the default tier), `record` (live API; re-records the + * `recorded` scenarios' fixtures and refreshes the Vitest goldens under + * `--update`), or `refresh` (keyless replay that rewrites stdout goldens and + * comparable session fixtures from the replay run). The caller derives this + * from `$DSH_SNAPSHOT` — env reading stays outside this library. */ - mode: 'replay' | 'record' + mode: 'replay' | 'record' | 'refresh' } /** @@ -188,6 +208,86 @@ export function normalizedHeaders(rawLog: string, ctx: NormalizeContext): unknow .map(record => record.data?.header) } +/** + * The normalized string-valued system prompts carried by request headers in a + * session JSONL, in log order. Headers without a string prompt are omitted so + * callers can assert one prompt per header explicitly. + * + * @param rawLog The session `.jsonl` content to inspect. + * @param ctx The volatile values of the run that produced it. + * @returns The normalized system prompts, in header order. + */ +export function normalizedSystemPrompts(rawLog: string, ctx: NormalizeContext): string[] { + return normalizedHeaders(rawLog, ctx).flatMap((header) => { + if (header === null || typeof header !== 'object') return [] + const system = (header as { system?: unknown }).system + return typeof system === 'string' ? [system] : [] + }) +} + +/** One normalized system-prompt edit carried by a `request/header-delta`. */ +export interface SystemPromptDeltaSnapshot { + /** How many leading lines remain from the prior prompt. */ + keepStart: number + /** How many trailing lines remain from the prior prompt. */ + keepEnd: number + /** The normalized replacement lines inserted between the retained ranges. */ + insert: string[] +} + +/** + * Extract normalized system-prompt edits from request-header deltas in log + * order. Deltas without a well-formed system edit are omitted; their non-prompt + * structure remains pinned in JSONL. + * + * @param rawLog The session `.jsonl` content to inspect. + * @param ctx The volatile values of the run that produced it. + * @returns The normalized system-prompt edits, in event order. + */ +export function normalizedSystemPromptDeltas(rawLog: string, ctx: NormalizeContext): SystemPromptDeltaSnapshot[] { + return normalizeSessionLog(rawLog, ctx) + .split('\n') + .filter(line => line.trim().length > 0) + .map(line => JSON.parse(line) as { type?: unknown; data?: { system?: unknown } }) + .filter(record => record.type === 'request/header-delta') + .flatMap((record) => { + const system = record.data?.system + if (system === null || typeof system !== 'object') return [] + const { keepStart, keepEnd, insert } = system as { keepStart?: unknown; keepEnd?: unknown; insert?: unknown } + if (typeof keepStart !== 'number' || typeof keepEnd !== 'number' || !Array.isArray(insert)) return [] + if (!insert.every(line => typeof line === 'string')) return [] + return [{ keepStart, keepEnd, insert: insert }] + }) +} + +/** + * Render a normalized prompt as a repository-friendly Markdown snapshot. + * Prompt text is unchanged except that a missing terminal newline is added so + * the committed file follows the repository newline contract. + * + * @param prompt The normalized system prompt. + * @param deltas Normalized prompt edits to append as readable sections. + * @returns Markdown snapshot text ending in a newline. + */ +export function formatSystemPromptSnapshot( + prompt: string, + deltas: readonly SystemPromptDeltaSnapshot[] = [], +): string { + let snapshot = prompt.endsWith('\n') ? prompt : `${prompt}\n` + for (const [index, delta] of deltas.entries()) { + snapshot += `\n\n\n` + const insert = delta.insert.join('\n') + snapshot += insert.endsWith('\n') ? insert : `${insert}\n` + } + return snapshot +} + +/** Return the initial-prompt portion of a possibly delta-bearing snapshot. */ +function initialSystemPromptSnapshot(snapshot: string): string { + const marker = snapshot.indexOf('\n + +NEW PROMPT LINE diff --git a/packages/support/acp-snapshot/tests/harness.spec.ts b/packages/support/acp-snapshot/tests/harness.spec.ts index 683d2aaf80..b0817a8d04 100644 --- a/packages/support/acp-snapshot/tests/harness.spec.ts +++ b/packages/support/acp-snapshot/tests/harness.spec.ts @@ -168,6 +168,8 @@ describe('runScenario', () => { [{ op: 'promptExpectError', text: 'x' }, /promptExpectError before newSession/], [{ op: 'promptAndCancel', text: 'x' }, /promptAndCancel before newSession/], [{ op: 'cancel' }, /cancel before newSession/], + [{ op: 'setConfigOption', configId: 'sandbox-mode', value: 'read-only' }, /setConfigOption before newSession/], + [{ op: 'setConfigOptionExpectError', configId: 'sandbox-mode', value: 'yolo' }, /setConfigOptionExpectError before newSession/], ] as [InputStep, RegExp][])('rejects %j before newSession', { timeout: 20_000 }, async (step, message) => { const { fixtureFile } = await scenario({}) await expect(runScenario( @@ -176,6 +178,53 @@ describe('runScenario', () => { )).rejects.toThrow(message) }) + it('setConfigOption switches a value and receives the complete refreshed option state', { timeout: 20_000 }, async () => { + const { fixtureFile } = await scenario({ + configOptions: { 'sandbox-mode': ['read-only', 'workspace-write'], 'approval-policy': ['ask', 'never'] }, + }) + const result = await runScenario( + { + steps: [...boot, + { op: 'setConfigOption', configId: 'sandbox-mode', value: 'workspace-write' }, + { op: 'setConfigOption', configId: 'approval-policy', value: 'never' }], + }, + { agent: AGENT, mode: 'replay', fixtureFile }, + ) + // Every set answers with the FULL state: the second response carries the + // first switch's value too — the complete-refreshed-state contract. + const frames = result.rawStdout.trim().split('\n').map(line => JSON.parse(line) as { result?: { configOptions?: { id: string; currentValue: string }[] } }) + const states = frames + .map(f => f.result?.configOptions) + .filter(options => options !== undefined) + .map(options => Object.fromEntries((options as { id: string; currentValue: string }[]).map(o => [o.id, o.currentValue]))) + expect(states).toEqual([ + { 'sandbox-mode': 'workspace-write', 'approval-policy': 'ask' }, + { 'sandbox-mode': 'workspace-write', 'approval-policy': 'never' }, + ]) + }) + + it('setConfigOptionExpectError swallows the rejection for unknown ids and out-of-vocabulary values', { timeout: 20_000 }, async () => { + const { fixtureFile } = await scenario({ configOptions: { 'sandbox-mode': ['read-only'] } }) + const result = await runScenario( + { + steps: [...boot, + { op: 'setConfigOptionExpectError', configId: 'sandbox-mode', value: 'yolo' }, + { op: 'setConfigOptionExpectError', configId: 'reasoning-effort', value: 'max' }], + }, + { agent: AGENT, mode: 'replay', fixtureFile }, + ) + expect(result.rawStdout).toContain('unknown sandbox-mode value yolo') + expect(result.rawStdout).toContain('unknown config option reasoning-effort') + }) + + it('setConfigOptionExpectError throws when the set unexpectedly succeeds', { timeout: 20_000 }, async () => { + const { fixtureFile } = await scenario({ configOptions: { 'sandbox-mode': ['read-only'] } }) + await expect(runScenario( + { steps: [...boot, { op: 'setConfigOptionExpectError', configId: 'sandbox-mode', value: 'read-only' }] }, + { agent: AGENT, mode: 'replay', fixtureFile }, + )).rejects.toThrow(/expected set_config_option to be rejected/) + }) + it('rejects an unknown input op', { timeout: 20_000 }, async () => { const { fixtureFile } = await scenario({}) const bogus = { op: 'reticulate' } as unknown as InputStep diff --git a/packages/support/acp-snapshot/tests/normalize.spec.ts b/packages/support/acp-snapshot/tests/normalize.spec.ts index daa9f8342d..bdde120a76 100644 --- a/packages/support/acp-snapshot/tests/normalize.spec.ts +++ b/packages/support/acp-snapshot/tests/normalize.spec.ts @@ -1,5 +1,11 @@ import { describe, expect, it } from 'vitest' -import { type NormalizeContext, normalizeSessionLog, normalizeStdout, scrubRequestHeaders } from '../src/normalize.ts' +import { + type NormalizeContext, + normalizeSessionLog, + normalizeStdout, + scrubRequestHeaders, + scrubSystemPrompts, +} from '../src/normalize.ts' /** * Unit tests for the pure snapshot normalizers. Live as a *.spec.ts (runs in @@ -260,3 +266,43 @@ describe('scrubRequestHeaders', () => { expect(scrubRequestHeaders(once)).toBe(once) }) }) + +describe('scrubSystemPrompts', () => { + it('scrubs only system prompt payloads while keeping tools and prefixes verbatim', () => { + const header = JSON.stringify({ + type: 'request/header', seq: 1, time: 2, + data: { + header: { + system: 'full prompt', + tools: [{ name: 'read', description: 'full schema' }], + messagePrefix: [{ role: 'user', content: [{ type: 'text', text: 'full prefix' }] }], + }, + reason: 'initial', + }, + }) + const delta = JSON.stringify({ + type: 'request/header-delta', seq: 2, time: 3, + data: { + system: { keepStart: 1, keepEnd: 2, insert: ['new prompt line'] }, + tools: { changed: [{ name: 'read', description: 'changed schema' }] }, + messagePrefix: [{ role: 'user', content: [{ type: 'text', text: 'changed prefix' }] }], + }, + }) + const toolsOnly = JSON.stringify({ + type: 'request/header', seq: 3, time: 4, + data: { header: { tools: [{ name: 'read', description: 'schema only' }] }, reason: 'resume' }, + }) + + const out = scrubSystemPrompts(`${header}\n${delta}\n${toolsOnly}\n`) + expect(out).toContain('"system":"{{system}}"') + expect(out).toContain('"insert":["{{system}}"]') + expect(out).not.toContain('full prompt') + expect(out).not.toContain('new prompt line') + expect(out).toContain('full schema') + expect(out).toContain('full prefix') + expect(out).toContain('changed schema') + expect(out).toContain('changed prefix') + expect(out.split('\n')[2]).toBe(toolsOnly) + expect(scrubSystemPrompts(out)).toBe(out) + }) +}) diff --git a/packages/support/acp-snapshot/tests/suite.spec.ts b/packages/support/acp-snapshot/tests/suite.spec.ts index 0f47010a19..d6e3e2b912 100644 --- a/packages/support/acp-snapshot/tests/suite.spec.ts +++ b/packages/support/acp-snapshot/tests/suite.spec.ts @@ -1,11 +1,21 @@ -import { cpSync, mkdtempSync } from 'node:fs' +import { cpSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs' import { rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { fileURLToPath } from 'node:url' import { afterAll, describe, expect, it } from 'vitest' -import { defineAcpSnapshotSuite, type Scenario } from '../src/index.ts' -import { childFixturePaths, fixtureContext, headerDeltaCount, normalizedHeaders } from '../src/suite.ts' +import { defineAcpSnapshotSuite, type HarvestedLog, type Scenario } from '../src/index.ts' +import { + childFixturePaths, + fixtureContext, + formatSystemPromptSnapshot, + headerDeltaCount, + normalizedHeaders, + normalizedSystemPromptDeltas, + normalizedSystemPrompts, + refreshFixtureReplacements, + stabilizeRefreshLog, +} from '../src/suite.ts' /** * Unit tests for the suite factory, by running it: two synthetic suites over @@ -41,7 +51,7 @@ const RECORD_SRC = fileURLToPath(new URL('./fixtures/record-suite', import.meta. // is what this suite can exercise; the real overlay boot is the acp-agent // example's code-mode scenarios). const REPLAY_SCENARIOS: Scenario[] = [ - { name: 'pin-turn', hasModelTurn: true, recorded: true, pinsHeader: true, headerClass: 'main' }, + { name: 'pin-turn', hasModelTurn: true, recorded: true, pinsHeader: true, expectedHeaderDeltas: 1, headerClass: 'main' }, { name: 'plain-turn', hasModelTurn: true, recorded: true, childSessions: 1, headerClass: 'main', configPath: AGENT.configPath }, { name: 'no-model', hasModelTurn: false, recorded: false, headerClass: 'main' }, { name: 'blocked-log', hasModelTurn: false, comparesLog: true, recorded: false, headerClass: 'main' }, @@ -55,16 +65,41 @@ const RECORD_SCENARIOS: Scenario[] = [ { name: 'rec-skip', hasModelTurn: true, recorded: false, overridden: true }, ] -// Record mode mutates its snapshots dir, so run it on a throwaway copy — -// except under the documented bootstrap knob, which regenerates the committed -// fixtures/goldens in place. +// Record/refresh modes mutate their snapshots dir, so run them on throwaway +// copies — except record's documented bootstrap knob, which regenerates the +// committed record fixtures/goldens in place. const BOOTSTRAP = process.env.ACP_SNAPSHOT_SPEC_BOOTSTRAP === '1' const recordDir = BOOTSTRAP ? RECORD_SRC : mkdtempSync(join(tmpdir(), 'acp-snap-record-suite-')) if (!BOOTSTRAP) cpSync(RECORD_SRC, recordDir, { recursive: true }) +const refreshDir = mkdtempSync(join(tmpdir(), 'acp-snap-refresh-suite-')) +cpSync(REPLAY_DIR, refreshDir, { recursive: true }) +staleRefreshFixtures(refreshDir) afterAll(async () => { if (!BOOTSTRAP) await rm(recordDir, { recursive: true, force: true }) + await rm(refreshDir, { recursive: true, force: true }) }) +function staleRefreshFixtures(dir: string): void { + writeFileSync(join(dir, 'plain-turn', 'stdout.golden.jsonl'), 'stale stdout\n') + writeFileSync(join(dir, 'pin-turn', 'system-prompt.golden.md'), 'STALE PROMPT\n') + + const plainBehaviorFile = join(dir, 'plain-turn', 'behavior.json') + const plainBehavior = JSON.parse(readFileSync(plainBehaviorFile, 'utf8')) as Record + plainBehavior.echoEnv = true + writeFileSync(plainBehaviorFile, `${JSON.stringify(plainBehavior, null, 2)}\n`) + + writeFileSync(join(dir, 'blocked-log', 'session.jsonl'), [ + '{"type":"session","id":"99999999-8888-4777-8666-555555555555","createdAt":13,"cwd":"/rec/blocked-cwd"}', + '{"type":"hook/result","seq":1,"time":13,"data":{"decision":"stale","durationMs":99}}', + '', + ].join('\n')) + writeFileSync(join(dir, 'authored-error', 'session.jsonl'), [ + '{"type":"session","id":"77777777-8888-4777-8666-555555555555","createdAt":13,"cwd":"/rec/error-cwd"}', + '{"type":"turn/end","seq":1,"time":9,"data":{"error":"stale"}}', + '', + ].join('\n')) +} + describe('defineAcpSnapshotSuite: replay mode', () => { defineAcpSnapshotSuite({ agent: AGENT, snapshotsDir: REPLAY_DIR, scenarios: REPLAY_SCENARIOS, mode: 'replay' }) }) @@ -75,6 +110,36 @@ describe('defineAcpSnapshotSuite: record mode', () => { defineAcpSnapshotSuite({ agent: AGENT, snapshotsDir: recordDir, scenarios: RECORD_SCENARIOS, mode: 'record' }) }) +describe('defineAcpSnapshotSuite: refresh mode', () => { + defineAcpSnapshotSuite({ agent: AGENT, snapshotsDir: refreshDir, scenarios: REPLAY_SCENARIOS, mode: 'refresh' }) +}) + +describe('defineAcpSnapshotSuite: refresh write-back', () => { + it('rewrites stdout and comparable logs from a replay-mode child run', () => { + const stdout = readFileSync(join(refreshDir, 'plain-turn', 'stdout.golden.jsonl'), 'utf8') + expect(stdout).not.toContain('stale stdout') + expect(stdout).toContain('env:{\\"mode\\":\\"replay\\"') + expect(stdout).not.toContain('\\"mode\\":\\"refresh\\"') + + const blocked = readFileSync(join(refreshDir, 'blocked-log', 'session.jsonl'), 'utf8') + expect(blocked).toContain('"decision":"block"') + expect(blocked).not.toContain('"decision":"stale"') + + const authored = readFileSync(join(refreshDir, 'authored-error', 'session.jsonl'), 'utf8') + expect(authored).toContain('"error":"model exploded"') + expect(authored).not.toContain('"error":"stale"') + + expect(readFileSync(join(refreshDir, 'pin-turn', 'system-prompt.golden.md'), 'utf8')).toBe([ + 'SYS PROMPT', + '', + '', + '', + 'NEW PROMPT LINE', + '', + ].join('\n')) + }) +}) + describe('defineAcpSnapshotSuite: registration contract', () => { it("throws when a scenario's header class has no pinning scenario", () => { expect(() => { @@ -167,6 +232,55 @@ describe('normalizedHeaders', () => { }) }) +describe('normalizedSystemPrompts', () => { + it('extracts normalized string prompts and omits absent or non-string fields', () => { + const log = [ + '{"type":"session","id":"a","createdAt":5,"cwd":"/w"}', + '{"type":"request/header","seq":0,"time":9,"data":{"header":{"system":"work in /w"}}}', + '{"type":"request/header","seq":1,"time":9,"data":{"header":{}}}', + '{"type":"request/header","seq":2,"time":9,"data":{"header":{"system":null}}}', + '{"type":"request/header","seq":3,"time":9,"data":{"header":null}}', + '{"type":"request/header","seq":4,"time":9,"data":{"header":"invalid"}}', + '', + ].join('\n') + expect(normalizedSystemPrompts(log, { sessionIds: [], cwd: '/w' })).toEqual(['work in {{cwd}}']) + }) +}) + +describe('normalizedSystemPromptDeltas', () => { + it('extracts and normalizes well-formed system edits', () => { + const log = [ + '{"type":"request/header-delta","data":{"system":{"keepStart":1,"keepEnd":0,"insert":["work in /w"]}}}', + '{"type":"request/header-delta","data":{"tools":{"replace":[]}}}', + '{"type":"request/header-delta","data":{"system":{"keepStart":"1","keepEnd":0,"insert":[]}}}', + '{"type":"request/header-delta","data":{"system":{"keepStart":1,"keepEnd":0,"insert":[null]}}}', + '', + ].join('\n') + expect(normalizedSystemPromptDeltas(log, { sessionIds: [], cwd: '/w' })).toEqual([ + { keepStart: 1, keepEnd: 0, insert: ['work in {{cwd}}'] }, + ]) + }) +}) + +describe('formatSystemPromptSnapshot', () => { + it('adds a missing terminal newline without changing an existing one', () => { + expect(formatSystemPromptSnapshot('prompt')).toBe('prompt\n') + expect(formatSystemPromptSnapshot('prompt\n')).toBe('prompt\n') + }) + + it('renders readable system-prompt delta sections', () => { + expect(formatSystemPromptSnapshot('prompt', [ + { keepStart: 1, keepEnd: 0, insert: ['new', 'lines'] }, + ])).toBe('prompt\n\n\n\nnew\nlines\n') + }) + + it('does not double the newline of a delta insert with a trailing blank line', () => { + expect(formatSystemPromptSnapshot('prompt\n', [ + { keepStart: 2, keepEnd: 1, insert: ['tail', ''] }, + ])).toBe('prompt\n\n\n\ntail\n') + }) +}) + describe('headerDeltaCount', () => { it('counts request/header-delta events, ignoring blanks and other lines', () => { const delta = JSON.stringify({ type: 'request/header-delta', seq: 2, time: 9, data: {} }) @@ -175,3 +289,56 @@ describe('headerDeltaCount', () => { expect(headerDeltaCount(`${other}\n`)).toBe(0) }) }) + +describe('refreshFixtureReplacements', () => { + it('maps fresh ids and cwd values to the existing fixture values, skipping non-replacements', () => { + const log = (content: string): HarvestedLog => ({ id: 'diagnostic', createdAt: 1, content }) + const logs = [ + log('{"type":"session","id":"","cwd":"/same"}\n'), + log('{"type":"session","id":"new-parent","cwd":"/new"}\n'), + log('{"type":"session","id":"new-child","cwd":"/new"}\n'), + ] + const fixtures = [ + '{"type":"session","id":"","cwd":"/same"}\n', + '{"type":"session","id":"old-parent","cwd":"/old"}\n', + ] + expect(refreshFixtureReplacements(logs, fixtures)).toEqual([ + { from: 'new-parent', to: 'old-parent' }, + { from: '/new', to: '/old' }, + ]) + }) +}) + +describe('stabilizeRefreshLog', () => { + it('keeps volatile fixture fields while preserving fresh meaningful payloads', () => { + const fresh = [ + '{"type":"session","id":"new-child","createdAt":200,"cwd":"/new","parentSession":"new-parent","seedLength":1}', + '{"type":"hook/result","seq":1,"time":22,"data":{"decision":"block","durationMs":37}}', + '{"type":"turn/end","seq":2,"time":33,"data":{"error":"fresh error"}}', + '{"type":"tool/result","seq":3,"time":44,"data":{"text":"new-parent in /new"}}', + '{"type":"hook/result","seq":4,"time":55,"data":{"decision":"allow","durationMs":5}}', + '', + ].join('\n') + const existing = [ + '{"type":"session","id":"old-child","createdAt":100,"cwd":"/old","parentSession":"old-parent","seedLength":5}', + '{"type":"hook/result","seq":1,"time":11,"data":{"decision":"stale","durationMs":99}}', + '{"type":"turn/end","seq":2,"data":{"error":"stale"}}', + '{"type":"assistant/message","seq":3,"time":12,"data":{"text":"different type"}}', + '{"type":"hook/result","seq":4,"time":13,"data":{"decision":"stale"}}', + '', + ].join('\n') + + expect(stabilizeRefreshLog(fresh, existing, [ + { from: 'new-parent', to: 'old-parent' }, + { from: 'new-child', to: 'old-child' }, + { from: '/new', to: '/old' }, + ])).toBe([ + '{"type":"session","id":"old-child","createdAt":100,"cwd":"/old","parentSession":"old-parent","seedLength":1}', + '{"type":"hook/result","seq":1,"time":11,"data":{"decision":"block","durationMs":99}}', + '{"type":"turn/end","seq":2,"time":33,"data":{"error":"fresh error"}}', + '{"type":"tool/result","seq":3,"time":44,"data":{"text":"old-parent in /old"}}', + '{"type":"hook/result","seq":4,"time":13,"data":{"decision":"allow","durationMs":5}}', + '', + ].join('\n')) + }) +}) diff --git a/packages/support/invariants/README.md b/packages/support/invariants/README.md index 94c2682f9d..c4b909773d 100644 --- a/packages/support/invariants/README.md +++ b/packages/support/invariants/README.md @@ -1,9 +1,13 @@ # dsh-invariants -Dev-mode event-contract invariants and session-log freeze. A pure-listener plugin (everything is a plugin) that asserts the harness event contract at runtime and, optionally, freezes logged session-event data so any code that mutates history throws instead of corrupting silently. +Dev-mode event-contract assertions. This pure-listener plugin checks relationships among session events, agent states, scoped dispatches, and model requests at runtime; it does not own or change product behavior. **Off in production.** Enable it in tests and the demos, where a contract violation should fail loudly. It costs nothing when not registered, and doubles as executable documentation of the event taxonomy — the assertions *are* the contract. +Session itself owns immutable log storage in every composition: it takes one lossless JSON snapshot of each accepted event, deep-freezes that record, and exposes the log through immutable array snapshots. The invariants plugin checks the cross-record and cross-seam rules that storage immutability cannot express. + +Session-log assertions run during Cordis `internal/dispatch`, while `Session.append()` is resolving the `session/event` callback snapshot but before it pushes the candidate into the log. A valid transition is staged by exact event identity and applied to the live trace only when that same committed event reaches the plugin's contained post-commit listener. A later internal dispatch check can therefore veto without advancing either the log or the invariant trace, while ordinary `session/event` observer failures remain observe-only. + ## Plugin A functional plugin — register the module namespace (this is what loading by name in `cordis.yml` does): @@ -14,17 +18,10 @@ import * as Invariants from '@deepseek-ai/dsh-invariants' declare const ctx: Context -await ctx.plugin(Invariants) // freeze on (default) -await ctx.plugin(Invariants, { freeze: false }) // assert contract, don't freeze +await ctx.plugin(Invariants) ``` -`inject`: `['sessions']` — it reads `ctx.sessions.list()` at apply time to rebuild trace state for sessions that already exist (so a hot reload mid-turn doesn't falsely reject the next event). It listens on `session/created`, `session/event`, and `agent/status`. - -### Config - -| Key | Default | Meaning | -|---|---|---| -| `freeze` | `true` | Deep-freeze each logged event's data so mutating a logged event throws. Set `false` to assert the contract without freezing. | +`inject`: `['sessions']` — it reads `ctx.sessions.list()` at apply time to rebuild trace state for sessions that already exist, so a hot reload mid-turn does not falsely reject the next event. The oracle listeners are explicitly global so pre-commit staging and post-commit application keep the same audience even if the plugin is mounted under a scoped context; their cleanup still belongs to that mounting fiber. The plugin has no configuration. ## Invariants asserted @@ -46,10 +43,10 @@ Model requests (on `llm/stream`): On any violation it throws `InvariantError` (`code: 'INVARIANT'`). -## Why runtime, not deep-readonly types +## Why runtime assertions remain useful -A `DeepReadonly` is high type-noise across every log consumer, and a plugin can cast straight through it. A dev-mode freeze plus these assertions catch real corruption at zero production cost and zero type noise. The always-on half of that defense — cloning derived messages so request/adapter mutation can't reach back into the log — lives in `dsh-session`'s `deriveMessages`. This package is the dev-mode tripwire. See [dev-mode invariants](../../../docs/rfc/implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md). +Session enforces the per-record storage boundary at runtime, where a cast cannot bypass it. Pervasive `DeepReadonly` types would add noise across consumers without expressing relationships such as turn/step nesting, subject-correct scoped dispatch, or equality between a request and its log reconstruction. This plugin checks those relationships in development while `dsh-session` keeps history immutable in every composition. See [source-owned session immutability and dev-mode invariants](../../../docs/rfc/implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md). ## Seeded sessions -A seeded/forked session arrives with events already in its log (the `Session` constructor copies the seed without emitting `session/event`). On `session/created` the plugin replays the existing log through the checker and freezes those entries, so seeded history is held to the same contract. +A seeded or forked session arrives with events already in its log because construction does not emit `session/event` for each seed record. `Session` validates, snapshots, and freezes every seed record before accepting it; on `session/created`, this plugin replays the accepted log only to rebuild and check its relational trace state. diff --git a/packages/support/invariants/package.json b/packages/support/invariants/package.json index 97d6160b03..588c658857 100644 --- a/packages/support/invariants/package.json +++ b/packages/support/invariants/package.json @@ -1,6 +1,6 @@ { "name": "@deepseek-ai/dsh-invariants", - "description": "Dev-mode event-contract invariants + session-log freeze for the DeepSeek Harness", + "description": "Dev-mode event-contract assertions for the DeepSeek Harness", "version": "0.0.1", "private": true, "type": "module", @@ -24,12 +24,14 @@ "peerDependencies": { "@deepseek-ai/dsh-agent": "^0.0.1", "@deepseek-ai/dsh-llm": "^0.0.1", + "@deepseek-ai/dsh-scope": "^0.0.1", "@deepseek-ai/dsh-session": "^0.0.1", "cordis": "^4.0.0-rc.6" }, "devDependencies": { "@deepseek-ai/dsh-agent": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", + "@deepseek-ai/dsh-scope": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", "cordis": "^4.0.0-rc.6" } diff --git a/packages/support/invariants/src/index.ts b/packages/support/invariants/src/index.ts index 8147a6deb6..f5ef6d2b4a 100644 --- a/packages/support/invariants/src/index.ts +++ b/packages/support/invariants/src/index.ts @@ -1,26 +1,25 @@ /** - * Dev-mode invariants: a pure-listener plugin that asserts the harness event - * contract at runtime, and (optionally) freezes logged session-event data so - * any code that mutates history throws instead of corrupting silently. + * Dev-mode invariants: a pure-listener plugin that asserts relationships in + * the harness event contract at runtime. * * Everything is a plugin — this is just listeners on `session/created`, - * `session/event`, and `agent/status`. It is **off in production**: enable it - * in tests and the demos, where a contract violation should be a loud failure, - * not a subtle one. It doubles as executable documentation of the event - * taxonomy: the assertions below ARE the contract. + * `session/event`, `agent/status`, and the scoped dispatch and request seams. + * It is **off in production**: enable it in tests and demos, where a contract + * violation should be a loud failure rather than a subtle one. It doubles as + * executable documentation of the event taxonomy: the assertions below are + * the contract. * - * Why runtime assertions instead of compile-time deep-readonly types? See - * the dev-invariants RFC. Briefly: a `DeepReadonly` is high type-noise across - * every log consumer and a plugin casts straight through it; a dev-mode freeze - * + assertions catch real corruption at zero production cost and zero type - * noise. The always-on half of that defense (cloning derived messages) lives - * in dsh-session; this package is the dev-mode tripwire. + * Session owns immutable log storage: it snapshots and deep-freezes every + * accepted event at the source. This plugin checks relationships that one + * event's types and immutability cannot express, including turn/step nesting, + * scoped dispatch, status transitions, and request reconstructability. * * @module @deepseek-ai/dsh-invariants */ import type { Context } from 'cordis' -import { HarnessError } from '@deepseek-ai/dsh-llm' +import { carrierKeyOf, isScopeCarrier } from '@deepseek-ai/dsh-scope' +import { assertNever, HarnessError } from '@deepseek-ai/dsh-llm' import type { CallId, GenerateOptions } from '@deepseek-ai/dsh-llm' import type { Agent, AgentStatus } from '@deepseek-ai/dsh-agent' import { Session, SessionId, foldRequestHeader } from '@deepseek-ai/dsh-session' @@ -41,16 +40,6 @@ export class InvariantError extends HarnessError { } } -/** Plugin config. */ -export interface Config { - /** - * Deep-freeze logged session-event data so mutating a logged event throws. - * Default true — this plugin only runs in dev/test, where freezing is the - * point. Set false to assert the event contract without freezing. - */ - freeze?: boolean -} - /** Per-session bookkeeping for the session-log invariants. */ interface SessionTrace { /** Highest `seq` seen so far (must strictly increase). */ @@ -79,27 +68,32 @@ interface SessionTrace { surface: number[] } -/** - * Deep-freeze a value and everything reachable from it. - * - * Walks every object's own properties even when the object itself is already - * frozen: `Session.append()` accepts event data from arbitrary plugins/tools, - * so a caller can hand us a SHALLOW-frozen object whose descendants are still - * mutable. Skipping an already-frozen node (the obvious idempotence shortcut) - * would leave exactly the kind of mutable history the dev-invariants RFC means to catch. A - * `WeakSet` of visited objects keeps it terminating on cycles and avoids - * re-walking shared subtrees / already-processed seed events. - */ -function deepFreeze(value: unknown, seen: WeakSet = new WeakSet()): void { - if (value === null || typeof value !== 'object') return - if (seen.has(value)) return - seen.add(value) - // Freeze the node (no-op if a caller pre-froze it), then ALWAYS descend — - // a frozen container can still hold mutable children. - Object.freeze(value) - for (const key of Object.keys(value)) { - deepFreeze((value as Record)[key], seen) - } +/** One accepted event's deferred mutation of a live session trace. */ +interface SessionTraceTransition { + /** Scalar state after the event commits. */ + scalars: Pick + /** The event's mutation of the open step's pending call set. */ + pendingCalls: + | { kind: 'none' } + | { kind: 'add' | 'delete'; callId: CallId } + | { kind: 'clear' } + /** The event's mutation of the derived surface order. */ + surface: + | { kind: 'none' | 'append' } + | { kind: 'replace'; start: number; count: number } + /** The committed event sequence to add to the known-sequence set. */ + seq: number +} + +/** Event payload prefix for scoped seams whose first argument names its agent. */ +interface AgentSubject { + agent: Agent +} + +/** Structural subject fields used without coupling this dev plugin to owning services. */ +interface ScopedSubjectFields { + agent?: Agent + scope?: object } /** Assert that a step-scoped event names the currently open turn and step. */ @@ -111,14 +105,19 @@ function requireOpenStep(trace: SessionTrace, kind: string, turn: number, step: } } -/** Assert one appended event against the per-session invariants. */ -function checkEvent(trace: SessionTrace, event: SessionEvent): void { +/** Validate one candidate event without mutating the committed session trace. */ +function validateEvent(trace: SessionTrace, event: SessionEvent): SessionTraceTransition { // seq is strictly monotonic — the spine of replay equivalence. lastSeq // starts at -1, so the first event (seq 0) passes. if (event.seq <= trace.lastSeq) { throw new InvariantError(`seq must strictly increase: saw ${event.seq} after ${trace.lastSeq}`) } - trace.lastSeq = event.seq + let openTurn = trace.openTurn + let openStep = trace.openStep + let nextTurn = trace.nextTurn + let nextStep = trace.nextStep + let pendingCalls: SessionTraceTransition['pendingCalls'] = { kind: 'none' } + let surface: SessionTraceTransition['surface'] = { kind: 'none' } // --- Surface invariants --- // Surface metadata (sourceEventSeqs, surfaceOp) is only valid on @@ -160,7 +159,7 @@ function checkEvent(trace: SessionTrace, event: SessionEvent): void { // positional range — every shadowed node must appear in sourceEventSeqs. if (se.surfaceOp !== undefined) { if (se.surfaceOp === 'append') { - trace.surface.push(event.seq) + surface = { kind: 'append' } } else { const { start, end } = se.surfaceOp const startIdx = trace.surface.indexOf(start) @@ -182,9 +181,7 @@ function checkEvent(trace: SessionTrace, event: SessionEvent): void { if (missing.length > 0) { throw new InvariantError(`surface replace: sourceEventSeqs must include every shadowed surface node; missing ${missing.join(', ')}`) } - // Apply the replace to the tracked surface: the new node takes the - // range's position so order stays in sync for later replaces. - trace.surface.splice(startIdx, shadowed.length, event.seq) + surface = { kind: 'replace', start: startIdx, count: shadowed.length } } } @@ -203,8 +200,8 @@ function checkEvent(trace: SessionTrace, event: SessionEvent): void { if (event.data.turn !== trace.nextTurn) { throw new InvariantError(`turn/start expected turn ${trace.nextTurn}, got ${event.data.turn}`) } - trace.openTurn = event.data.turn - trace.nextStep = 1 + openTurn = event.data.turn + nextStep = 1 break } case 'turn/end': { @@ -214,8 +211,8 @@ function checkEvent(trace: SessionTrace, event: SessionEvent): void { if (trace.openStep !== null) { throw new InvariantError(`turn/end ${event.data.turn} while step ${trace.openStep} is still open`) } - trace.openTurn = null - trace.nextTurn += 1 + openTurn = null + nextTurn += 1 break } case 'step/start': { @@ -229,16 +226,16 @@ function checkEvent(trace: SessionTrace, event: SessionEvent): void { if (event.data.step !== trace.nextStep) { throw new InvariantError(`step/start expected step ${trace.nextStep} in turn ${event.data.turn}, got ${event.data.step}`) } - trace.openStep = event.data.step + openStep = event.data.step break } case 'step/end': { requireOpenStep(trace, 'step/end', event.data.turn, event.data.step) // A result must arrive in the step that issued the call; orphan calls // (a step that errored before its result) do not carry to the next step. - trace.pendingCalls.clear() - trace.openStep = null - trace.nextStep += 1 + pendingCalls = { kind: 'clear' } + openStep = null + nextStep += 1 break } case 'assistant/chunk': { @@ -251,7 +248,7 @@ function checkEvent(trace: SessionTrace, event: SessionEvent): void { } case 'tool/call': { requireOpenStep(trace, 'tool/call', event.data.turn, event.data.step) - trace.pendingCalls.add(event.data.callId) + pendingCalls = { kind: 'add', callId: event.data.callId } break } case 'tool/result': { @@ -260,9 +257,10 @@ function checkEvent(trace: SessionTrace, event: SessionEvent): void { // does NOT hold: a call may have no result — a throwing tool-execution // pipeline step ends the turn with no tool/result, which is legal.) const syntheticInterrupted = event.data.isError && event.data.error?.code === 'interrupted' - if (!trace.pendingCalls.delete(event.data.callId) && !syntheticInterrupted) { + if (!trace.pendingCalls.has(event.data.callId) && !syntheticInterrupted) { throw new InvariantError(`tool/result for ${event.data.callId} with no prior tool/call in this step`) } + pendingCalls = { kind: 'delete', callId: event.data.callId } break } // Turn-enclosure (the turn-enclosure RFC): EVERY session event not handled by a boundary @@ -282,8 +280,52 @@ function checkEvent(trace: SessionTrace, event: SessionEvent): void { break } } - // Track every seq seen — used above to validate sourceEventSeqs references. - trace.knownSeqs.add(event.seq) + return { + scalars: { lastSeq: event.seq, openTurn, openStep, nextTurn, nextStep }, + pendingCalls, + surface, + seq: event.seq, + } +} + +/** Apply one already-validated transition after its event commits. */ +function applyTransition(trace: SessionTrace, transition: SessionTraceTransition): void { + Object.assign(trace, transition.scalars) + switch (transition.pendingCalls.kind) { + case 'none': + break + case 'add': + trace.pendingCalls.add(transition.pendingCalls.callId) + break + case 'delete': + trace.pendingCalls.delete(transition.pendingCalls.callId) + break + case 'clear': + trace.pendingCalls.clear() + break + /* v8 ignore next -- validateEvent produces this closed transition union */ + default: + assertNever(transition.pendingCalls, 'session trace pending-call transition') + } + switch (transition.surface.kind) { + case 'none': + break + case 'append': + trace.surface.push(transition.seq) + break + case 'replace': + trace.surface.splice(transition.surface.start, transition.surface.count, transition.seq) + break + /* v8 ignore next -- validateEvent produces this closed transition union */ + default: + assertNever(transition.surface, 'session trace surface transition') + } + trace.knownSeqs.add(transition.seq) +} + +/** Validate and apply one event while rebuilding an already-committed log. */ +function replayEvent(trace: SessionTrace, event: SessionEvent): void { + applyTransition(trace, validateEvent(trace, event)) } /** Legal agent status transitions (the only state machine the loop guarantees). */ @@ -303,14 +345,19 @@ function checkTransition(from: AgentStatus | undefined, to: AgentStatus): void { /** * Register the dev-mode invariants. Contributions are effect-scoped, so - * disposing the plugin fiber removes all listeners and stops freezing - * (HMR-safe). On (re-)apply the trace state is rebuilt by replaying each - * existing session's log, so a hot reload mid-turn does not falsely reject the - * next event. + * disposing the plugin fiber removes all listeners (HMR-safe). On (re-)apply + * the trace state is rebuilt by replaying each existing session's log, so a + * hot reload mid-turn does not falsely reject the next event. + * + * @param ctx - Cordis context that receives the invariant listeners. */ -export function apply(ctx: Context, config: Config = {}): void { - const freeze = config.freeze ?? true +export function apply(ctx: Context): void { const traces = new WeakMap() + const stagedTransitions = new WeakMap() // Agent status has no stored history to replay; the first observation after // (re-)apply seeds the baseline, so a reload never produces a false positive. const lastStatus = new WeakMap() @@ -326,20 +373,19 @@ export function apply(ctx: Context, config: Config = {}): void { surface: [], }) - /** Build (or rebuild) a session's trace by replaying its whole log; freeze it. */ + /** Build (or rebuild) a session's trace by replaying its whole log. */ const seedSession = (session: Session): SessionTrace => { const trace = freshTrace() traces.set(session, trace) for (const event of session.events) { - checkEvent(trace, event) - if (freeze) deepFreeze(event) + replayEvent(trace, event) } return trace } // Every store-created session (the only kind that emits session/event) is - // seeded first — via ctx.sessions.list() at apply or session/created — so - // the fallback is a defensive guard, never hit in practice. + // seeded first — via ctx.sessions.list() at apply or session/created — so the + // fallback is a defensive guard, never hit in practice. /* v8 ignore next -- traceFor's fallback: session/event always follows a seed */ const traceFor = (session: Session): SessionTrace => traces.get(session) ?? seedSession(session) @@ -350,17 +396,90 @@ export function apply(ctx: Context, config: Config = {}): void { // A newly created session may arrive seeded/forked (the constructor copies // the seed WITHOUT emitting session/event), so replay its log here too. - ctx.on('session/created', (session) => { seedSession(session) }) + ctx.on('session/created', (session) => { seedSession(session) }, { global: true }) ctx.on('session/event', (session, event) => { - checkEvent(traceFor(session), event) - if (freeze) deepFreeze(event) - }) + // Session resolves dispatch before committing, so internal/dispatch has + // already staged this exact event. A later dispatch veto skips every + // session/event callback and therefore leaves the live trace unchanged. + const staged = stagedTransitions.get(event) + /* v8 ignore next 2 -- internal/dispatch stages the exact callback arguments */ + if (staged === undefined || staged.session !== session) { + throw new InvariantError('session/event reached publication without matching pre-commit validation') + } + stagedTransitions.delete(event) + applyTransition(staged.trace, staged.transition) + }, { global: true }) ctx.on('agent/status', (agent, status) => { checkTransition(lastStatus.get(agent), status) lastStatus.set(agent, status) - }) + }, { global: true }) + + // --- Scoped-dispatch invariants (the agent-scoping seam) --------------- + // + // Every scope-filtered event family must dispatch with a scope carrier + // (scopeTarget) whose key IS the subject the event's arguments name — + // a dispatch without one silently reverts that event to global delivery + // (agent-scoped listeners over-hear foreign agents), and a mis-keyed one + // delivers to the wrong agent's listeners. `internal/dispatch` fires + // synchronously before listener delivery, so a violation throws at the + // dispatching call site. The table maps each family to how its subject is + // read from the event arguments; `null` = the subject is not recoverable + // from the arguments (session events key by the OWNING agent; subagent + // lifecycle events key by the delegating parent), so only carrier + // PRESENCE is asserted there. + const scopedSubject: Record unknown) | null> = { + 'agent/created': args => args[0], + 'agent/disposed': args => args[0], + 'agent/status': args => args[0], + 'agent/queued': args => args[0], + 'agent/session-start': args => args[0], + 'agent/pre-step': args => args[0], + 'agent/prompt-submit': args => args[0], + 'agent/request': args => args[0], + 'agent/session-prefix': args => args[0], + 'agent/step-result': args => args[0], + 'agent/turn-continuation': args => args[0], + 'agent/turn-stop': args => args[0], + 'agent/error': args => args[0], + 'approval/request': args => (args[0] as AgentSubject).agent, + 'tools/pre-execute': args => (args[0] as ScopedSubjectFields).agent, + 'tools/execute': args => (args[0] as ScopedSubjectFields).agent, + 'tools/post-execute': args => (args[0] as ScopedSubjectFields).agent, + 'tools/result': args => (args[0] as ScopedSubjectFields).agent, + 'system-prompt/assemble': args => (args[1] as ScopedSubjectFields).scope, + 'session/created': null, + 'session/disposed': null, + 'session/event': null, + 'session/flush': null, + 'subagent/start': null, + 'subagent/end': null, + } + ctx.on('internal/dispatch', (_mode, name, args, thisArg) => { + const subjectOf = scopedSubject[name] + if (subjectOf === undefined) return + if (!isScopeCarrier(thisArg)) { + throw new InvariantError( + `"${name}" is a scope-filtered event but was dispatched without a scope carrier — ` + + 'pass scopeTarget(base, subject) as the dispatch thisArg (agent events: use agentEvents(ctx, agent))') + } + if (subjectOf !== null && carrierKeyOf(thisArg) !== subjectOf(args)) { + throw new InvariantError( + `"${name}" was dispatched with a scope carrier keyed to a DIFFERENT subject than its arguments name — ` + + 'the carrier key and the event\'s subject must be the same object (use agentEvents(ctx, agent))') + } + if (name === 'session/event') { + const [session, event] = args as [Session, SessionEvent] + const trace = traceFor(session) + const transition = validateEvent(trace, event) + // The exact event identity reaches the contained post-commit listener. + // A later internal/dispatch listener may still veto; because validation + // is pure, abandoning this weakly keyed transition does not advance the + // committed trace or retain the session. + stagedTransitions.set(event, { session, trace, transition }) + } + }, { global: true }) // Request-reconstruction cross-check (the reconstructability RFC): a // loop-built request — frozen envelope + live sessionId is the marker; a @@ -437,5 +556,5 @@ export function apply(ctx: Context, config: Config = {}): void { throw new InvariantError(`llm request for session "${String(session.id)}" diverges from the folded request header`) } return next() - }, { prepend: true }) + }, { global: true, prepend: true }) } diff --git a/packages/support/invariants/tests/invariants.spec.ts b/packages/support/invariants/tests/invariants.spec.ts index af31f01911..5c26b1f4d7 100644 --- a/packages/support/invariants/tests/invariants.spec.ts +++ b/packages/support/invariants/tests/invariants.spec.ts @@ -1,16 +1,17 @@ import { describe, expect, it, vi } from 'vitest' import { Context } from 'cordis' +import { createScope, scopeTarget } from '@deepseek-ai/dsh-scope' import { CallId } from '@deepseek-ai/dsh-llm' import type { Agent } from '@deepseek-ai/dsh-agent' -import SessionStore, { SessionId } from '@deepseek-ai/dsh-session' +import SessionStore, { Session, SessionId } from '@deepseek-ai/dsh-session' import * as Invariants from '@deepseek-ai/dsh-invariants' import { InvariantError } from '@deepseek-ai/dsh-invariants' /** A Context with the session store and the invariants plugin registered. */ -async function setup(config?: { freeze?: boolean }) { +async function setup() { const ctx = new Context() await ctx.plugin(SessionStore) - const fiber = await ctx.plugin(Invariants, config ?? {}) + const fiber = await ctx.plugin(Invariants) return { ctx, fiber } } @@ -20,8 +21,27 @@ function mockAgent(id: string): Agent { } describe('session-log invariants', () => { + it('keeps pre-commit staging and post-commit application global when mounted under a scope', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + let scopedCtx!: Context + await ctx.plugin(Object.assign((inner: Context) => { + scopedCtx = createScope(inner, {}).ctx + }, { inject: ['sessions'] })) + await scopedCtx.plugin(Invariants) + const globalSession = ctx.sessions.create(SessionId('global-under-scoped-invariants')) + + expect(() => { + globalSession.append('turn/start', { + turn: 1, + trigger: { kind: 'message', source: { kind: 'user' } }, + }) + globalSession.append('turn/end', { turn: 1, reason: { kind: 'completed' } }) + }).not.toThrow() + }) + it('accepts a well-formed turn/step/tool sequence', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() expect(() => { session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) @@ -36,18 +56,59 @@ describe('session-log invariants', () => { }).not.toThrow() }) + it('does not advance the trace when a later internal-dispatch listener vetoes', async () => { + const { ctx } = await setup() + const session = ctx.sessions.create(SessionId('dispatch-veto-rollback')) + let veto = true + ctx.on('internal/dispatch', (_mode, name) => { + if (name !== 'session/event' || !veto) return + veto = false + throw new Error('later dispatch veto') + }) + + expect(() => session.append('turn/start', { + turn: 1, + trigger: { kind: 'message', source: { kind: 'user' } }, + })).toThrow('later dispatch veto') + expect(session.events).toEqual([]) + + expect(() => { + session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) + session.append('turn/end', { turn: 1, reason: { kind: 'completed' } }) + }).not.toThrow() + expect(session.events.map(event => event.type)).toEqual(['turn/start', 'turn/end']) + }) + + it('applies the committed transition after a prepended observer throws', async () => { + const { ctx } = await setup() + const warnings: string[] = [] + ctx.logger.warn = ((message: unknown) => { warnings.push(String(message)) }) as typeof ctx.logger.warn + const session = ctx.sessions.create(SessionId('postcommit-peer')) + ctx.on('session/event', () => { throw new Error('hostile observer') }, { prepend: true }) + + expect(() => { + session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) + session.append('turn/end', { turn: 1, reason: { kind: 'completed' } }) + }).not.toThrow() + expect(session.events.map(event => event.type)).toEqual(['turn/start', 'turn/end']) + expect(warnings).toEqual([ + 'session "postcommit-peer": session/event listener threw: Error: hostile observer', + 'session "postcommit-peer": session/event listener threw: Error: hostile observer', + ]) + }) + it('rejects a non-monotonic seq (replay spine)', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() // Session.append enforces seq-contiguity at the source, so drive the // invariants seq check directly via session/event with a regressing seq. - ctx.emit('session/event', session, { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } } as never) - expect(() => { ctx.emit('session/event', session, { type: 'turn/end', seq: 0, time: 2, data: { turn: 1, reason: { kind: 'completed' } } } as never) }) + ctx.emit(scopeTarget(session, undefined), 'session/event', session, { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } } as never) + expect(() => { ctx.emit(scopeTarget(session, undefined), 'session/event', session, { type: 'turn/end', seq: 0, time: 2, data: { turn: 1, reason: { kind: 'completed' } } } as never) }) .toThrow(/seq must strictly increase/) }) it('rejects a turn/start while another turn is open', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) expect(() => session.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })) @@ -55,7 +116,7 @@ describe('session-log invariants', () => { }) it('rejects a turn/end that does not match the open turn', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) expect(() => session.append('turn/end', { turn: 2, reason: { kind: 'completed' } })) @@ -63,14 +124,14 @@ describe('session-log invariants', () => { }) it('rejects a step/start outside its declared turn', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) expect(() => session.append('step/start', { turn: 2, step: 1 })).toThrow(/open turn is 1/) }) it('rejects a step/end that does not match the open step', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) session.append('step/start', { turn: 1, step: 1 }) @@ -78,7 +139,7 @@ describe('session-log invariants', () => { }) it('rejects an assistant/chunk outside an open step', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) expect(() => session.append('assistant/chunk', { turn: 1, step: 1, chunk: { type: 'text-delta', index: 0, text: 'x' } })) @@ -86,7 +147,7 @@ describe('session-log invariants', () => { }) it('rejects a message event appended outside any open turn (turn-enclosure)', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() // No turn open: every message-bearing event must be turn-enclosed (the turn-enclosure RFC). expect(() => session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' })) @@ -96,7 +157,7 @@ describe('session-log invariants', () => { }) it('rejects steering and plugin-added events appended outside any open turn', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() // steering/message is turn-scoped: outside a turn it would land past the // commit boundary and be dropped on resume (the turn-enclosure RFC). @@ -112,7 +173,7 @@ describe('session-log invariants', () => { }) it('accepts message events once a turn is open', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) expect(() => session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' })) @@ -120,7 +181,7 @@ describe('session-log invariants', () => { }) it('rejects a tool/result with no prior tool/call', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) session.append('step/start', { turn: 1, step: 1 }) @@ -129,7 +190,7 @@ describe('session-log invariants', () => { }) it('allows a synthetic interrupted tool/result from crash repair without a prior tool/call event', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() expect(() => { session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) @@ -151,7 +212,7 @@ describe('session-log invariants', () => { }) it('allows a tool/call with no matching tool/result (thrown waterfall ends the step)', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() expect(() => { session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) @@ -163,7 +224,7 @@ describe('session-log invariants', () => { }) it('holds seeded sessions to the contract on session/created', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() // A seq-contiguous, serializable seed (so it passes Session's constructor // validation) that nonetheless violates turn nesting — a second turn/start // while the first turn is still open — must be rejected by the invariants @@ -176,7 +237,7 @@ describe('session-log invariants', () => { }) it('tracks turns per session independently', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const a = ctx.sessions.create(SessionId('a')) const b = ctx.sessions.create(SessionId('b')) a.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) @@ -185,7 +246,7 @@ describe('session-log invariants', () => { }) it('accepts multiple steps in a turn and consecutive turns', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() expect(() => { session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) @@ -202,7 +263,7 @@ describe('session-log invariants', () => { }) it('rejects a skipped turn number', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) session.append('turn/end', { turn: 1, reason: { kind: 'completed' } }) @@ -211,7 +272,7 @@ describe('session-log invariants', () => { }) it('rejects a skipped step number within a turn', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) session.append('step/start', { turn: 1, step: 1 }) @@ -221,7 +282,7 @@ describe('session-log invariants', () => { }) it('rejects a turn/end while a step is still open', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) session.append('step/start', { turn: 1, step: 1 }) @@ -230,7 +291,7 @@ describe('session-log invariants', () => { }) it('rejects a step/start while a step is still open', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) session.append('step/start', { turn: 1, step: 1 }) @@ -238,7 +299,7 @@ describe('session-log invariants', () => { }) it('rejects a tool/result satisfying a call from a previous step', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) session.append('step/start', { turn: 1, step: 1 }) @@ -251,7 +312,7 @@ describe('session-log invariants', () => { }) it('rejects an assistant/message naming the wrong step', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) session.append('step/start', { turn: 1, step: 1 }) @@ -265,7 +326,7 @@ describe('HMR state rebuild', () => { const ctx = new Context() await ctx.plugin(SessionStore) // First registration, mid-turn: a turn is open when the plugin reloads. - const first = await ctx.plugin(Invariants, { freeze: false }) + const first = await ctx.plugin(Invariants) const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) session.append('step/start', { turn: 1, step: 1 }) @@ -273,7 +334,7 @@ describe('HMR state rebuild', () => { // Re-apply (HMR): the fresh fiber must replay the existing log so the open // step is known — the next chunk must NOT be a false positive. - await ctx.plugin(Invariants, { freeze: false }) + await ctx.plugin(Invariants) expect(() => session.append('assistant/chunk', { turn: 1, step: 1, chunk: { type: 'text-delta', index: 0, text: 'h' } })) .not.toThrow() // And a genuine violation is still caught after the rebuild. @@ -282,113 +343,86 @@ describe('HMR state rebuild', () => { }) }) -describe('dev-freeze', () => { - it('freezes appended event data so mutating a logged event throws', async () => { - const { ctx } = await setup() // freeze defaults true - const session = ctx.sessions.create() - session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) +describe('session immutability', () => { + it('always freezes appended event data without the invariants plugin', () => { + const session = new Session(SessionId('appended')) const event = session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) expect(Object.isFrozen(event)).toBe(true) expect(Object.isFrozen(event.data)).toBe(true) expect(Object.isFrozen(event.data.content)).toBe(true) + expect(Object.isFrozen(event.data.content[0])).toBe(true) + expect(Object.isFrozen(session.events)).toBe(true) expect(() => { (event.data.content[0] as { text: string }).text = 'HACKED' }).toThrow() }) - it('does not freeze when freeze:false', async () => { - const { ctx } = await setup({ freeze: false }) - const session = ctx.sessions.create() - session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) - const event = session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) - expect(Object.isFrozen(event)).toBe(false) - }) - - it('freezes seeded events on session/created', async () => { - const { ctx } = await setup() + it('always freezes seeded events without the invariants plugin', () => { const seed = [ { type: 'turn/start' as const, seq: 0, time: 0, data: { turn: 1, trigger: { kind: 'message' as const, source: { kind: 'user' as const } } } }, { type: 'user/message' as const, seq: 1, time: 0, data: { content: [{ type: 'text' as const, text: 'seeded' }], source: { kind: 'user' as const } }, surfaceOp: 'append' as const }, ] - const session = ctx.sessions.create(undefined, { seed }) + const session = new Session(SessionId('seeded'), seed) + expect(Object.isFrozen(seed[0])).toBe(false) + expect(Object.isFrozen(session.events)).toBe(true) expect(Object.isFrozen(session.events[0])).toBe(true) + expect(Object.isFrozen(session.events[0]?.data)).toBe(true) + expect(Object.isFrozen(session.events[1]?.data)).toBe(true) }) - it('freezes mutable descendants of a shallow-frozen event datum', async () => { - const { ctx } = await setup() - const session = ctx.sessions.create() - session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) - // A caller hands in a SHALLOW-frozen block whose nested array is still - // mutable. deepFreeze must descend into the already-frozen object and - // freeze the descendant, not short-circuit on the frozen container — - // otherwise dev-mode misses exactly the history mutation the dev-invariants RFC catches. - // `append` snapshots `data`, so the freeze applies to the LOGGED clone, not - // the caller's input — read the event back and assert on its data. + it('snapshots and freezes descendants of a shallow-frozen caller value', () => { + const session = new Session(SessionId('shallow-frozen')) const innerContent: { type: 'text'; text: string }[] = [{ type: 'text', text: 'inner' }] const block = Object.freeze({ type: 'tool-result' as const, toolCallId: CallId('c1'), content: innerContent, isError: false }) const event = session.append('user/message', { content: [block], source: { kind: 'user' } }, { surfaceOp: 'append' }) const logged = event.data.content[0] as { content: { type: 'text'; text: string }[] } + expect(Object.isFrozen(innerContent)).toBe(false) expect(Object.isFrozen(logged.content)).toBe(true) expect(Object.isFrozen(logged.content[0])).toBe(true) + innerContent[0]!.text = 'caller mutation' + expect(logged.content[0]!.text).toBe('inner') expect(() => { logged.content.push({ type: 'text', text: 'mutation' }) }).toThrow() }) - - it('terminates on a cyclic event datum (WeakSet guard)', async () => { - const { ctx } = await setup() - const session = ctx.sessions.create() - // The deep-freeze WeakSet guard must terminate on a self-referential - // structure rather than recursing forever. Session.append now rejects - // non-serializable (incl. cyclic) data at the source, so drive the freeze - // handler directly via hand-built session/events — exactly the shape the - // invariants listener receives. Open a turn first (seq 0) so the cyclic - // user/message (seq 1) satisfies the turn-enclosure invariant. - ctx.emit('session/event', session, { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } } as never) - const cyclic: Record = { type: 'text', text: 'x' } - cyclic['self'] = cyclic - const event = { type: 'user/message', seq: 1, time: 1, data: { content: [cyclic], source: { kind: 'user' } } } - expect(() => { ctx.emit('session/event', session, event as never) }).not.toThrow() - expect(Object.isFrozen(cyclic)).toBe(true) - }) }) describe('agent status invariants', () => { it('accepts legal transitions: idle→running→idle and →disposed', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const agent = mockAgent('a1') expect(() => { - ctx.emit('agent/status', agent, 'idle') - ctx.emit('agent/status', agent, 'running') - ctx.emit('agent/status', agent, 'idle') - ctx.emit('agent/status', agent, 'disposed') + ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'idle') + ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'running') + ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'idle') + ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'disposed') }).not.toThrow() }) it('accepts running→disposed', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const agent = mockAgent('a2') - ctx.emit('agent/status', agent, 'running') - expect(() => { ctx.emit('agent/status', agent, 'disposed') }).not.toThrow() + ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'running') + expect(() => { ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'disposed') }).not.toThrow() }) it('rejects a no-op transition', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const agent = mockAgent('a3') - ctx.emit('agent/status', agent, 'running') - expect(() => { ctx.emit('agent/status', agent, 'running') }).toThrow(/no-op transition/) + ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'running') + expect(() => { ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'running') }).toThrow(/no-op transition/) }) it('rejects leaving the terminal disposed state', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const agent = mockAgent('a4') - ctx.emit('agent/status', agent, 'disposed') - expect(() => { ctx.emit('agent/status', agent, 'idle') }).toThrow(/left terminal state disposed/) + ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'disposed') + expect(() => { ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'idle') }).toThrow(/left terminal state disposed/) }) it('tracks status per agent independently', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const a = mockAgent('a5') const b = mockAgent('b5') - ctx.emit('agent/status', a, 'running') + ctx.emit(scopeTarget(a, a), 'agent/status', a, 'running') // b's first observation is independent of a. - expect(() => { ctx.emit('agent/status', b, 'running') }).not.toThrow() + expect(() => { ctx.emit(scopeTarget(b, b), 'agent/status', b, 'running') }).not.toThrow() }) }) @@ -400,14 +434,14 @@ describe('HMR safety', () => { await fiber.dispose() - // After disposal: no freezing, no assertions. An event that WOULD have - // violated the open-turn rule now passes silently, and is not frozen. + // After disposal the plugin's assertions are gone, so an event that would + // violate the open-turn rule passes. Session still owns immutability. const event = session.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } }) - expect(Object.isFrozen(event)).toBe(false) + expect(Object.isFrozen(event)).toBe(true) // A no-op status transition no longer throws either. const agent = mockAgent('hmr') - ctx.emit('agent/status', agent, 'idle') - expect(() => { ctx.emit('agent/status', agent, 'idle') }).not.toThrow() + ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'idle') + expect(() => { ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'idle') }).not.toThrow() }) it('InvariantError carries a stable code', () => { @@ -418,16 +452,17 @@ describe('HMR safety', () => { expect(err.message).toBe('invariant violated: seq must strictly increase') }) - it('does not leak listeners across dispose (no stale freezing)', async () => { + it('does not leak listeners across dispose', async () => { const { ctx, fiber } = await setup() await fiber.dispose() const spy = vi.fn() ctx.on('session/event', spy) const session = ctx.sessions.create() session.append('user/message', { content: [{ type: 'text', text: 'x' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) - // our own spy fires, proving events still flow — but the plugin's frozen. + // The spy proves events still flow after plugin disposal. Session, not the + // disposed listener, freezes the accepted record. expect(spy).toHaveBeenCalledOnce() - expect(Object.isFrozen(session.events[0])).toBe(false) + expect(Object.isFrozen(session.events[0])).toBe(true) }) }) @@ -639,7 +674,7 @@ describe('surface invariants', () => { }) it('catches an incomplete-provenance replace on the load/seed path', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const badSeed = [ { type: 'turn/start' as const, seq: 0, time: 0, data: { turn: 1, trigger: { kind: 'message' as const, source: { kind: 'user' as const } } } }, { type: 'step/start' as const, seq: 1, time: 0, data: { turn: 1, step: 1 } }, @@ -654,10 +689,10 @@ describe('surface invariants', () => { const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) - // Type system prevents surface metadata on non-surface events; this test - // exercises the runtime guard against casts or persisted-data bypass. - // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return - expect(() => (session.append as any)('turn/end', { turn: 1, reason: { kind: 'completed' } }, { sourceEventSeqs: [0] })) + // Session rejects this at its own acceptance boundary. Emit a hand-built + // record to cover the listener's defensive check for alternate producers. + const event = { type: 'turn/end', seq: 1, time: 1, data: { turn: 1, reason: { kind: 'completed' } }, sourceEventSeqs: [0] } + expect(() => { ctx.emit(scopeTarget(session, undefined), 'session/event', session, event as never) }) .toThrow(/cannot carry sourceEventSeqs/) }) @@ -665,8 +700,8 @@ describe('surface invariants', () => { const { ctx } = await setup() const session = ctx.sessions.create() session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) - // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return - expect(() => (session.append as any)('turn/end', { turn: 1, reason: { kind: 'completed' } }, { surfaceOp: 'append' })) + const event = { type: 'turn/end', seq: 1, time: 1, data: { turn: 1, reason: { kind: 'completed' } }, surfaceOp: 'append' } + expect(() => { ctx.emit(scopeTarget(session, undefined), 'session/event', session, event as never) }) .toThrow(/cannot carry surfaceOp/) }) }) @@ -674,7 +709,7 @@ describe('surface invariants', () => { describe('request-reconstruction cross-check (llm/stream)', () => { /** Session with a boundary: one derivable user message, an open step, and the header event the loop would have logged. */ async function requestSetup() { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create(SessionId('req-check')) session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) @@ -736,7 +771,7 @@ describe('request-reconstruction cross-check (llm/stream)', () => { }) it('rejects a loop-built request with no header event or no step/start in its log', async () => { - const { ctx } = await setup({ freeze: false }) + const { ctx } = await setup() const session = ctx.sessions.create(SessionId('req-bare')) session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) const bare = Object.freeze({ model: 'm', messages: Object.freeze([]), sessionId: session.id }) @@ -777,7 +812,7 @@ describe('request cross-check ordering (prepend)', () => { const ctx = new Context() await ctx.plugin(SessionStore) ctx.on('llm/stream', () => (async function* () {})() as never) // short-circuits, no next() - await ctx.plugin(Invariants, { freeze: false }) + await ctx.plugin(Invariants) const session = ctx.sessions.create(SessionId('prepend-check')) session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) @@ -795,3 +830,67 @@ describe('request cross-check ordering (prepend)', () => { }).toThrow(/diverges from the boundary derivation/) }) }) + +describe('scoped-dispatch invariants', () => { + async function scopedCtx() { + const ctx = new Context() + await ctx.plugin(SessionStore) + await ctx.plugin(Invariants) + return ctx + } + + it('rejects a scoped-family dispatch without a carrier (teaching error)', async () => { + const ctx = await scopedCtx() + const agent = { id: 'a1' } as unknown as Agent + expect(() => { ctx.emit('agent/error', agent, 1, 0, new Error('x')) }) + .toThrow(/dispatched without a scope carrier/) + }) + + it('accepts a matching carrier and rejects a mismatched one for EVERY agent-subject event', async () => { + const ctx = await scopedCtx() + // Real Session objects: the session-start tracker WeakSet-keys them. + const agent = { id: 'a1', session: new Session(SessionId('a1-s')) } as unknown as Agent + const other = { id: 'a2', session: new Session(SessionId('a2-s')) } as unknown as Agent + // One dispatch per table row keeps every subject extractor covered: the + // matching carrier passes, the foreign-keyed one throws. + const rows: [string, unknown[]][] = [ + ['agent/created', [agent]], + ['agent/disposed', [agent]], + ['agent/status', [agent, 'idle']], + ['agent/queued', [agent, [], { source: { kind: 'user' }, steering: false }]], + ['agent/session-start', [agent, 'startup']], + ['agent/pre-step', [agent, 1, 1, '', new AbortController().signal]], + ['agent/prompt-submit', [agent, [], { kind: 'user' }, () => Promise.resolve({ kind: 'allow' })]], + ['agent/request', [agent, 1, 1, { model: 'm' }, () => Promise.resolve({ model: 'm' })]], + ['agent/session-prefix', [agent, [], new AbortController().signal, () => Promise.resolve([])]], + ['agent/step-result', [agent, 1, 1, { role: 'assistant', content: [] }, () => Promise.resolve({ role: 'assistant', content: [] })]], + ['agent/turn-continuation', [agent, 1, { action: 'stop' }, () => Promise.resolve({ action: 'stop' })]], + ['agent/turn-stop', [agent, 1]], + ['agent/error', [agent, 1, 0, new Error('x')]], + ['approval/request', [{ agent, toolName: 'echo' }, () => Promise.resolve('unavailable')]], + ['tools/pre-execute', [{ callId: 'c', name: 't', arguments: {}, agent }, () => Promise.resolve({ kind: 'allow' })]], + ['tools/execute', [{ callId: 'c', name: 't', arguments: {}, agent }, () => Promise.resolve({ callId: 'c', content: [], isError: false })]], + ['tools/post-execute', [{ callId: 'c', name: 't', arguments: {}, agent }, { callId: 'c', content: [], isError: false }, () => Promise.resolve({ kind: 'accept' })]], + ['tools/result', [{ callId: 'c', name: 't', arguments: {}, agent }, { callId: 'c', content: [], isError: false }]], + ] + for (const [event, args] of rows) { + const subject = event.startsWith('tools/') ? agent : agent + expect(() => { (ctx.emit as (...a: unknown[]) => void)(scopeTarget(agent, subject), event, ...args) }, + `${event} with matching carrier`).not.toThrow() + expect(() => { (ctx.emit as (...a: unknown[]) => void)(scopeTarget(agent, other), event, ...args) }, + `${event} with foreign carrier`).toThrow(/DIFFERENT subject/) + } + }) + + it('rejects a carrier keyed to a different subject than the arguments name', async () => { + const ctx = await scopedCtx() + const agent = { id: 'a1' } as unknown as Agent + const other = { id: 'a2' } as unknown as Agent + expect(() => { ctx.emit(scopeTarget(agent, other), 'agent/error', agent, 1, 0, new Error('x')) }) + .toThrow(/keyed to a DIFFERENT subject/) + // The correct spelling passes. + expect(() => { ctx.emit(scopeTarget(agent, agent), 'agent/error', agent, 1, 0, new Error('x')) }) + .not.toThrow() + }) + +}) diff --git a/packages/support/invariants/tsconfig.json b/packages/support/invariants/tsconfig.json index 8dca14c786..cd17f67d7c 100644 --- a/packages/support/invariants/tsconfig.json +++ b/packages/support/invariants/tsconfig.json @@ -22,6 +22,9 @@ }, { "path": "../../core/agent" + }, + { + "path": "../../core/scope" } ] } diff --git a/packages/support/subagent-mock/README.md b/packages/support/subagent-mock/README.md index af169ed4c2..b8d9fbba44 100644 --- a/packages/support/subagent-mock/README.md +++ b/packages/support/subagent-mock/README.md @@ -2,7 +2,7 @@ A scripted `SubagentProvider` for testing the [subagent seam](../../subagent/subagent/README.md) without a model or a real child agent — the subagent analog of [`dsh-llm-replay`](../llm-replay/README.md). -It lets a test drive `ctx.subagents` and the model-facing `dsh-tool-subagent` through the **real cordis Loader / export path**, exercising provider registration, start-time capability validation, the run lifecycle (`result` / `cancel` / `dispose`), and the structured-output branch — all deterministically and keylessly. +It lets a test drive `ctx.subagents` and the model-facing `dsh-tool-subagent` through the real Cordis loader/export path, exercising provider registration, async start, start-time capability validation, required-signal cancellation, `result`, `dispose`, and structured output deterministically and keylessly. ## Usage @@ -13,8 +13,8 @@ Load it as a plugin (functional shape: `name`/`inject`/`Config`/`apply`, no defa | `name` | `mock` | Registry name to register the provider under. | | `reply` | `mock subagent reply` | The scripted child's final answer text. | | `stopReason` | `completed` | The stop reason `result` settles with. | -| `capabilities` | all `true` | Which start-time capabilities (`outputSchema`/`depthLimit`/`toolFilter`) the provider advertises. | -| `inheritsParentContext` | `false` | The context contract to declare; `true` exercises the fork-shaped tool wording in consumer tests. | +| `capabilities` | all `true` | Which start-time capabilities (`outputSchema`, `depthLimit`, `toolFilter`, and `persona`) the provider advertises. | +| `inheritsParentContext` | `false` | Conversation-history descriptor: `false` means fresh, while `true` exercises seeded/fork wording. It says nothing about tool, service, scope, or authority inheritance. | | `structured` | `{ reply }` | Structured value surfaced when a request carries an `outputSchema` and the capability is on. | -A `cancel()` issued before `result` settles flips the stop reason to `aborted`, so the cancellation path is observable. +Aborting the required request signal or disposing before `result` settles flips the stop reason to `aborted`, so both holder-facing cancellation paths are observable. diff --git a/packages/support/subagent-mock/src/index.ts b/packages/support/subagent-mock/src/index.ts index e2effe5b4b..abc1a6c5e9 100644 --- a/packages/support/subagent-mock/src/index.ts +++ b/packages/support/subagent-mock/src/index.ts @@ -26,14 +26,13 @@ import type { const STOP_REASONS = ['completed', 'aborted', 'error', 'max-tokens', 'refusal'] as const -const DEFAULT_CAPS: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: true } +const DEFAULT_CAPS: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: true, persona: true } /** - * A scripted provider: every {@link start} returns a run whose `result` - * resolves on a microtask with the configured reply (and a structured value - * when the request asked for one and the capability is on). `dispose` is a - * no-op; a `cancel()` before the result settles flips the stop reason to - * `aborted`, so the cancellation path is observable in a test. + * A scripted provider: every {@link start} returns a ready run whose `result` + * resolves on the next task with the configured reply (and a structured value + * when the request asked for one and the capability is on). The required + * signal and `dispose()` both flip an unsettled result to `aborted`. */ class MockSubagentProvider implements SubagentProvider { readonly capabilities: SubagentCapabilities @@ -47,12 +46,22 @@ class MockSubagentProvider implements SubagentProvider { this.inheritsParentContext = config.inheritsParentContext ?? false } - start(request: SubagentStartRequest): SubagentRun { + async start(request: SubagentStartRequest): Promise { + if (request.signal.aborted) throw new Error('mock subagent start aborted before publication') const reply = this.config.reply ?? 'mock subagent reply' const output: ContentBlock[] = [{ type: 'text', text: reply }] const wantsStructured = request.outputSchema !== undefined && this.capabilities.outputSchema const baseStop: SubagentStopReason = this.config.stopReason ?? 'completed' - let cancelled = false + const flags = { cancelled: false } + const onAbort = (): void => { flags.cancelled = true } + request.signal.addEventListener('abort', onAbort, { once: true }) + // Make publication genuinely asynchronous so a same-turn abort is still + // a provider-owned startup failure rather than a returned live run. + await Promise.resolve() + if (flags.cancelled) { + request.signal.removeEventListener('abort', onAbort) + throw new Error('mock subagent start aborted before publication') + } // A deterministic child id derived from the parent — no clock/random (both // banned in deterministic paths here, and unnecessary for a scripted run). @@ -60,18 +69,22 @@ class MockSubagentProvider implements SubagentProvider { const resultFor = (): SubagentResult => ({ output, - structured: wantsStructured ? (this.config.structured ?? { reply }) : undefined, - stopReason: cancelled ? 'aborted' : baseStop, + ...wantsStructured ? { structured: this.config.structured ?? { reply } } : {}, + stopReason: flags.cancelled ? 'aborted' : baseStop, }) + const result = new Promise((resolve) => { + setTimeout(() => { resolve(resultFor()) }, 0) + }).finally(() => { + request.signal.removeEventListener('abort', onAbort) + }) return { id, - result: Promise.resolve().then(resultFor), - cancel() { - cancelled = true - }, - async dispose() { - // Scripted run holds no resources — nothing to await. + result, + dispose(): Promise { + flags.cancelled = true + request.signal.removeEventListener('abort', onAbort) + return Promise.resolve() }, } } @@ -91,9 +104,11 @@ export interface Config { /** Which start-time capabilities to advertise (default: all `true`). */ capabilities?: Partial /** - * The context contract to declare ({@link SubagentProvider.inheritsParentContext}); - * default `false` (spawn-like). Set `true` to exercise the fork-shaped tool - * wording in consumer tests. + * The conversation-history descriptor to declare + * ({@link SubagentProvider.inheritsParentContext}); default `false` (fresh + * conversation). Set `true` to exercise seeded/fork wording in consumer + * tests. This flag says nothing about tool, service, scope, or authority + * inheritance. */ inheritsParentContext?: boolean /** @@ -111,6 +126,7 @@ export const Config: z = z.object({ outputSchema: z.boolean(), depthLimit: z.boolean(), toolFilter: z.boolean(), + persona: z.boolean(), }), inheritsParentContext: z.boolean(), structured: z.any(), diff --git a/packages/support/subagent-mock/tests/subagent-mock.spec.ts b/packages/support/subagent-mock/tests/subagent-mock.spec.ts index ddd725da4b..7fd93c62bc 100644 --- a/packages/support/subagent-mock/tests/subagent-mock.spec.ts +++ b/packages/support/subagent-mock/tests/subagent-mock.spec.ts @@ -11,7 +11,7 @@ function fakeParent(id = 'parent-1'): Agent { } function baseRequest(over: Partial = {}): SubagentStartRequest { - return { prompt: [{ type: 'text', text: 'task' }], parent: fakeParent(), ...over } + return { prompt: [{ type: 'text', text: 'task' }], parent: fakeParent(), signal: new AbortController().signal, ...over } } async function mount(config: Partial = {}): Promise { @@ -26,12 +26,13 @@ describe('dsh-subagent-mock', () => { const ctx = await mount({ reply: 'hello from mock' }) expect(ctx.subagents.list()).toEqual(['mock']) - const run = ctx.subagents.start('mock', baseRequest()) + const run = await ctx.subagents.start('mock', baseRequest()) await expect(run.result).resolves.toEqual({ output: [{ type: 'text', text: 'hello from mock' }], structured: undefined, stopReason: 'completed', }) + await run.dispose() }) it('registers under a configurable name', async () => { @@ -41,13 +42,13 @@ describe('dsh-subagent-mock', () => { it('surfaces a structured result when the request carries an outputSchema', async () => { const ctx = await mount({ reply: 'r', structured: { answer: 42 } }) - const run = ctx.subagents.start('mock', baseRequest({ outputSchema: { type: 'object', properties: { answer: { type: 'number' } } } })) + const run = await ctx.subagents.start('mock', baseRequest({ outputSchema: { type: 'object', properties: { answer: { type: 'number' } } } })) await expect(run.result).resolves.toMatchObject({ structured: { answer: 42 } }) }) it('defaults structured output to { reply } when outputSchema is requested but no structured value is configured', async () => { const ctx = await mount({ reply: 'fallback reply' }) - const run = ctx.subagents.start('mock', baseRequest({ outputSchema: { type: 'object', properties: { answer: { type: 'number' } } } })) + const run = await ctx.subagents.start('mock', baseRequest({ outputSchema: { type: 'object', properties: { answer: { type: 'number' } } } })) await expect(run.result).resolves.toMatchObject({ structured: { reply: 'fallback reply' } }) }) @@ -56,23 +57,44 @@ describe('dsh-subagent-mock', () => { // The service rejects an outputSchema request against a no-cap provider, so // the structured path is only reachable when the cap is on; with it off and // no schema requested, the result has no structured field. - const run = ctx.subagents.start('mock', baseRequest()) - await expect(run.result).resolves.toMatchObject({ structured: undefined }) + const run = await ctx.subagents.start('mock', baseRequest()) + const result = await run.result + expect(result).not.toHaveProperty('structured') }) it('honors a configured stop reason', async () => { const ctx = await mount({ stopReason: 'refusal' }) - const run = ctx.subagents.start('mock', baseRequest()) + const run = await ctx.subagents.start('mock', baseRequest()) await expect(run.result).resolves.toMatchObject({ stopReason: 'refusal' }) }) - it('flips the stop reason to aborted when cancelled before the result settles', async () => { + it('flips the stop reason to aborted when the signal fires before the result settles', async () => { const ctx = await mount() - const run = ctx.subagents.start('mock', baseRequest()) - run.cancel() + const controller = new AbortController() + const run = await ctx.subagents.start('mock', baseRequest({ signal: controller.signal })) + controller.abort() await expect(run.result).resolves.toMatchObject({ stopReason: 'aborted' }) }) + it('rejects an already-aborted request before starting publication', async () => { + const ctx = await mount() + const controller = new AbortController() + controller.abort() + + await expect(ctx.subagents.start('mock', baseRequest({ signal: controller.signal }))) + .rejects.toThrow('mock subagent start aborted before publication') + }) + + it('rejects when cancellation wins the asynchronous publication handoff', async () => { + const ctx = await mount() + const controller = new AbortController() + const pending = ctx.subagents.start('mock', baseRequest({ signal: controller.signal })) + + controller.abort() + + await expect(pending).rejects.toThrow('mock subagent start aborted before publication') + }) + it('unregisters the provider when the owning fiber is disposed (HMR safety)', async () => { const ctx = new Context() await ctx.plugin(SubagentService) diff --git a/packages/timeout/timeout-policy/src/index.ts b/packages/timeout/timeout-policy/src/index.ts index 319de676a9..2e9ed0d635 100644 --- a/packages/timeout/timeout-policy/src/index.ts +++ b/packages/timeout/timeout-policy/src/index.ts @@ -80,13 +80,17 @@ export function toolTimeoutResult(callId: CallId, timeoutMs: number): ToolExecut * when its own timer fired. A tool that declares no budget delegates untouched. * * The budget source is the tool's own declaration read from the registry - * (`ctx.tools.get(exec.name)?.timeoutMs`), NOT a plugin config map — `exec.name` - * is the tool being dispatched, so the lookup always resolves and there is no - * mistypable tool name and no unknown-name path to warn or throw about. + * (`ctx.tools.get(exec.name, exec.agent)?.timeoutMs`), NOT a plugin config map — + * `exec.name` is the tool being dispatched, so the lookup always resolves and + * there is no mistypable tool name and no unknown-name path to warn or throw + * about. Resolution goes through the CALLER's visible view (the `exec.agent` + * scope), exactly like dispatch itself: a scoped tool's own `timeoutMs` governs + * its calls, and a global name-twin's budget is never misapplied to a shadowing + * per-agent variant. */ export function apply(ctx: Context): void { ctx.on('tools/execute', async (exec, next): Promise => { - const timeoutMs = ctx.tools.get(exec.name)?.timeoutMs + const timeoutMs = ctx.tools.get(exec.name, exec.agent)?.timeoutMs // A tool that declares no budget: no deadline, delegate unchanged. if (timeoutMs === undefined) return next() diff --git a/packages/timeout/timeout-policy/tests/timeout-policy.spec.ts b/packages/timeout/timeout-policy/tests/timeout-policy.spec.ts index ef5c52030f..30a7307515 100644 --- a/packages/timeout/timeout-policy/tests/timeout-policy.spec.ts +++ b/packages/timeout/timeout-policy/tests/timeout-policy.spec.ts @@ -11,7 +11,7 @@ import { Context } from 'cordis' import Loader from '@cordisjs/plugin-loader' import { CallId, HarnessError } from '@deepseek-ai/dsh-llm' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' -import ToolRegistry, { defineTool, type ToolExecution, type ToolExecutionResult, type PostToolDecision } from '@deepseek-ai/dsh-tools' +import ToolRegistry, { defineTool, type ToolExecutionInput, type ToolExecutionResult, type PostToolDecision } from '@deepseek-ai/dsh-tools' import * as timeoutPolicy from '@deepseek-ai/dsh-timeout-policy' import { TOOL_TIMEOUT, toolTimeoutResult } from '@deepseek-ai/dsh-timeout-policy' @@ -193,7 +193,7 @@ describe('dsh-timeout-policy real-load-path guard', () => { const loader = Object.create(Loader.prototype) as Loader const unwrapped = loader.unwrapExports(timeoutPolicy) as Parameters[0] const fiber = await ctx.plugin(unwrapped) - const result = await ctx.tools.execute({ callId: CallId('c1'), name: 'fast', arguments: {} } satisfies ToolExecution) + const result = await ctx.tools.execute({ callId: CallId('c1'), name: 'fast', arguments: {} } satisfies ToolExecutionInput) expect(result.isError).toBe(false) await fiber.dispose() }) diff --git a/packages/ui/README.md b/packages/ui/README.md index e87a5e4a10..604eff6521 100644 --- a/packages/ui/README.md +++ b/packages/ui/README.md @@ -5,6 +5,7 @@ Integrations that expose the agent to an external editor or client. These are ** | Package | Role | ctx key | |---|---|---| | `acp/` | Agent Client Protocol bridge: serves the agent to an ACP editor (Zed) over JSON-RPC stdio | (drives `ctx.agents`/`ctx.sessions`) | +| `user-approval/` | One-shot user-approval mechanism, closed outcome vocabulary, audit events, and per-session approval policy | `ctx.approval` | | `user-interaction/` | Abstract human question/answer seam used by UI-backed confirmation tools | `ctx.userInteraction` | | `tool-ask-user/` | Model-facing `ask_user_question` tool over `ctx.userInteraction` | (registers on `ctx.tools`) | | `stdio-agent/` | Terminal stdio chat APP: the agent-core spine + console logger + readline UI + a pre-created `main` agent, with a `bin` | (composition + `bin`) | @@ -13,6 +14,6 @@ Integrations that expose the agent to an external editor or client. These are ** A UI integration is a client-driver plugin, not a loop change and not a capability seam: it consumes the existing `agent/*` event taxonomy and the `dsh-agent` factory. The readline UI is the unstructured analogue of the `acp` bridge and lives INSIDE the stdio app (the `stdio-chat` module of [`stdio-agent/`](stdio-agent/README.md)): it is scaffolding for that one front door, not an independently swappable integration, so it carries no package boundary of its own. -`user-interaction` and `tool-ask-user` live here because asking a human is a UI-backed product affordance, not part of the providerless core spine. The seam remains provider-neutral (`ctx.userInteraction`), while the tool is the model-facing consumer and the app/bridge packages provide concrete providers. +`user-approval`, `user-interaction`, and `tool-ask-user` live here because asking a human is a UI-backed product affordance, not part of the providerless core spine. `user-approval` owns the one-shot `ctx.approval` decision mechanism and its policy tier; answerers remain with their UI channel owners. `user-interaction` remains provider-neutral (`ctx.userInteraction`), while `tool-ask-user` is its model-facing consumer and the app/bridge packages provide concrete providers. `stdio-agent` and `acp-agent` are the two **app packages**: each composes the [`core/agent-core`](../core/agent-core/README.md) spine with its coupled front-door cluster (and owns the boot `bin`), so a leaf `cordis.yml` is the swappable backends plus one app entry plus any optional product tools. They live in `ui/` because each IS a user-facing front door; the stdout-purity coupling (logger vs. no logger) becomes a property of the artifact rather than a leaf convention. diff --git a/packages/ui/acp-agent/README.md b/packages/ui/acp-agent/README.md index ee3a995eaa..d81c9cc091 100644 --- a/packages/ui/acp-agent/README.md +++ b/packages/ui/acp-agent/README.md @@ -1,6 +1,6 @@ # @deepseek-ai/dsh-acp-agent -The **ACP server app**: a Cordis app plugin that composes the providerless agent spine ([`@deepseek-ai/dsh-agent-core`](../../core/agent-core/README.md)) with the front-door cluster an [Agent Client Protocol](../acp/README.md) server needs, and a `bin` that boots a leaf `cordis.yml` speaking ACP JSON-RPC on stdio. +The **ACP server app**: a Cordis app plugin that composes the default agent spine ([`@deepseek-ai/dsh-agent-core`](../../core/agent-core/README.md)) with the front-door cluster an [Agent Client Protocol](../acp/README.md) server needs, and a `bin` that boots a leaf `cordis.yml` speaking ACP JSON-RPC on stdio. It is the structured counterpart to [`@deepseek-ai/dsh-stdio-agent`](../stdio-agent/README.md): both consume the same spine, but this one bakes in the OPPOSITE front-door cluster. diff --git a/packages/ui/acp-agent/package.json b/packages/ui/acp-agent/package.json index b6d59c9467..987ddb6c13 100644 --- a/packages/ui/acp-agent/package.json +++ b/packages/ui/acp-agent/package.json @@ -46,6 +46,7 @@ "@cordisjs/plugin-loader": "workspace:^", "@deepseek-ai/dsh-app-boot": "workspace:^", "@deepseek-ai/dsh-acp": "workspace:^", + "@deepseek-ai/dsh-agent": "workspace:^", "@deepseek-ai/dsh-agent-core": "workspace:^", "@deepseek-ai/dsh-tools": "workspace:^", "@deepseek-ai/dsh-system-prompt": "workspace:^", diff --git a/packages/ui/acp-agent/src/index.ts b/packages/ui/acp-agent/src/index.ts index 4a0242e8c7..27b919f756 100644 --- a/packages/ui/acp-agent/src/index.ts +++ b/packages/ui/acp-agent/src/index.ts @@ -1,5 +1,5 @@ /** - * The ACP server app: the providerless agent spine ({@link + * The ACP server app: the default agent spine ({@link * @deepseek-ai/dsh-agent-core}) plus the coupled front-door cluster an ACP * server needs — JSONL session persistence and the {@link @deepseek-ai/dsh-acp} * bridge, and DELIBERATELY NOTHING that writes to stdout. @@ -60,6 +60,8 @@ export interface Config { tools?: ToolsConfig /** Directory the JSONL session backend writes under. Defaults to `./.sessions`. */ persistenceRoot?: string + /** Skill registry, local-provider, and model-facing consumer config forwarded to agent-core. */ + skills?: agentCore.SkillConfig } export const Config: z = z.object({ @@ -71,6 +73,7 @@ export const Config: z = z.object({ toolOrder: z.array(z.string()).default(undefined as unknown as string[]), tools: ToolRegistry.Config, persistenceRoot: z.string().default('./.sessions'), + skills: agentCore.SkillConfigSchema, }) /** @@ -85,6 +88,7 @@ export function apply(ctx: Context, config: Config): void { ...config.persona !== undefined ? { persona: config.persona } : {}, ...config.toolOrder !== undefined ? { toolOrder: config.toolOrder } : {}, ...config.tools !== undefined ? { tools: config.tools } : {}, + ...config.skills !== undefined ? { skills: config.skills } : {}, }) ctx.plugin(UserInteractionService) ctx.plugin(SessionPersistenceJsonl, { root: config.persistenceRoot ?? './.sessions' }) diff --git a/packages/ui/acp-agent/tests/acp-agent.spec.ts b/packages/ui/acp-agent/tests/acp-agent.spec.ts index 6e2f202859..537155429c 100644 --- a/packages/ui/acp-agent/tests/acp-agent.spec.ts +++ b/packages/ui/acp-agent/tests/acp-agent.spec.ts @@ -1,7 +1,12 @@ import { describe, expect, it } from 'vitest' +import { mkdtemp } from 'node:fs/promises' +import { join } from 'node:path' +import { tmpdir } from 'node:os' import { Context } from 'cordis' import Loader from '@cordisjs/plugin-loader' +import { agentEvents, type Agent } from '@deepseek-ai/dsh-agent' import { TOOL_ORDER_REST } from '@deepseek-ai/dsh-system-prompt' +import type { Message } from '@deepseek-ai/dsh-llm' import * as acpAgent from '../src/index.ts' /** @@ -23,9 +28,48 @@ async function mount(config: acpAgent.Config): Promise { return ctx } +async function isolatedSkillsConfig(catalogDescriptionMaxLength?: number): Promise> { + const home = await mkdtemp(join(tmpdir(), 'dsh-acp-agent-skills-')) + return { + local: { dshHome: join(home, '.dsh'), agentsHome: join(home, '.agents') }, + ...catalogDescriptionMaxLength !== undefined ? { tool: { catalogDescriptionMaxLength } } : {}, + } +} + +async function composePrefix(ctx: Context): Promise { + const agent = { session: { header: { cwd: '/tmp' } } } as unknown as Agent + const empty: Message[] = [] + return await agentEvents(ctx, agent).waterfall( + 'agent/session-prefix', empty, new AbortController().signal, + () => Promise.resolve(empty), + ) +} + +async function withIsolatedSkillHomes(run: () => Promise): Promise { + const oldDshHome = process.env.DSH_HOME + const oldAgentsHome = process.env.DSH_AGENTS_HOME + const home = await mkdtemp(join(tmpdir(), 'dsh-acp-agent-default-skills-')) + process.env.DSH_HOME = join(home, '.dsh') + process.env.DSH_AGENTS_HOME = join(home, '.agents') + try { + return await run() + } finally { + if (oldDshHome === undefined) { + delete process.env.DSH_HOME + } else { + process.env.DSH_HOME = oldDshHome + } + if (oldAgentsHome === undefined) { + delete process.env.DSH_AGENTS_HOME + } else { + process.env.DSH_AGENTS_HOME = oldAgentsHome + } + } +} + describe('dsh-acp-agent composition', () => { it('brings up the spine + persistence + the ACP bridge', async () => { - const ctx = await mount({ model: 'mock', persona: 'hi', persistenceRoot: '/tmp/dsh-acp-agent-test' }) + const ctx = await mount({ model: 'mock', persona: 'hi', persistenceRoot: '/tmp/dsh-acp-agent-test', skills: await isolatedSkillsConfig() }) expect(ctx.get('agents')).toBeDefined() expect(ctx.get('sessions')).toBeDefined() expect(ctx.get('sessionPersistence')).toBeDefined() @@ -44,12 +88,30 @@ describe('dsh-acp-agent composition', () => { // persistenceRoot, so the runtime fallback is the one that fires. const ctx = new Context() // No persona: covers the omitted-persona forwarding branch too. - acpAgent.apply(ctx, { model: 'mock' }) + acpAgent.apply(ctx, { model: 'mock', skills: await isolatedSkillsConfig() }) await new Promise(resolve => setTimeout(resolve, 50)) expect(ctx.get('sessionPersistence')).toBeDefined() await ctx.fiber.dispose() }) + it('uses default skill config when apply is called directly without skills', async () => { + await withIsolatedSkillHomes(async () => { + const ctx = new Context() + acpAgent.apply(ctx, { model: 'mock' }) + await new Promise(resolve => setTimeout(resolve, 50)) + expect(ctx.skills).toBeDefined() + expect(await ctx.skills.list()).toEqual([]) + await ctx.fiber.dispose() + }) + }) + + it('forwards skill config into agent-core', async () => { + const ctx = await mount({ model: 'mock', persona: 'hi', skills: await isolatedSkillsConfig(6) }) + ctx.skills.register({ name: 'acp-skill', description: 'ACP skill', source: 'runtime', content: 'body' }) + expect(JSON.stringify(await composePrefix(ctx))).toContain('- `acp-skill`: ACP...') + await ctx.fiber.dispose() + }) + it('exposes its plugin shape', () => { expect(acpAgent.name).toBe('acp-agent') expect(acpAgent.Config).toBeDefined() @@ -72,7 +134,7 @@ describe('dsh-acp-agent composition', () => { }) } const assembly = await ctx.get('systemPrompt')!.assemble() - expect(assembly.tools.map(tool => tool.name)).toEqual(['zulu', 'alpha']) + expect(assembly.tools.map(tool => tool.name)).toEqual(['zulu', 'alpha', 'skill']) await ctx.fiber.dispose() }) diff --git a/packages/ui/acp-agent/tests/built-bin.e2e.ts b/packages/ui/acp-agent/tests/built-bin.e2e.ts index d900c14152..de4612a40a 100644 --- a/packages/ui/acp-agent/tests/built-bin.e2e.ts +++ b/packages/ui/acp-agent/tests/built-bin.e2e.ts @@ -99,7 +99,7 @@ async function makeConsumer(): Promise { ' name: \'@deepseek-ai/dsh-acp-agent\'', ' config:', ' model: deepseek-v4-flash', - ' systemPrompt: \'test agent\'', + ' persona: \'test agent\'', '', ].join('\n')) return dir @@ -120,7 +120,12 @@ describe.skipIf(!existsSync(acpBin))('dsh-acp-agent BUILT bin (node lib/bin.js, child = spawn(process.execPath, ['--expose-internals', acpBin, './cordis.yml'], { cwd: consumer, // Dummy key: initialize never reaches the model, so it is never used. - env: { ...process.env, DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY ?? 'sk-dummy-for-boot' }, + env: { + ...process.env, + DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY ?? 'sk-dummy-for-boot', + DSH_HOME: join(consumer, '.dsh'), + DSH_AGENTS_HOME: join(consumer, '.agents'), + }, stdio: ['pipe', 'pipe', 'pipe'], }) const stderr: string[] = [] @@ -180,7 +185,12 @@ function runBinExpectingExit(configArg: string, cwd: string = tmpdir()): Promise return new Promise((resolve, reject) => { const proc = spawn(process.execPath, ['--expose-internals', acpBin, configArg], { cwd, - env: { ...process.env, DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY ?? 'sk-dummy-for-boot' }, + env: { + ...process.env, + DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY ?? 'sk-dummy-for-boot', + DSH_HOME: join(cwd, '.dsh'), + DSH_AGENTS_HOME: join(cwd, '.agents'), + }, stdio: ['pipe', 'pipe', 'pipe'], }) child = proc diff --git a/packages/ui/acp-agent/tests/load-path.e2e.ts b/packages/ui/acp-agent/tests/load-path.e2e.ts index fd559d99bf..ec251a6e6b 100644 --- a/packages/ui/acp-agent/tests/load-path.e2e.ts +++ b/packages/ui/acp-agent/tests/load-path.e2e.ts @@ -54,7 +54,7 @@ const CORDIS_YML = ` name: '@deepseek-ai/dsh-acp-agent' config: model: deepseek-v4-flash - systemPrompt: 'You are a test agent.' + persona: 'You are a test agent.' ` interface Spawned { @@ -90,6 +90,8 @@ async function boot(): Promise { TSX_TSCONFIG_PATH: repoTsconfig, // Key-present check only; no prompt is sent, so the model is never called. DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY ?? 'keyless-acp-agent-smoke', + DSH_HOME: join(cwd, '.dsh'), + DSH_AGENTS_HOME: join(cwd, '.agents'), }, stdio: ['pipe', 'pipe', 'pipe'], }, diff --git a/packages/ui/acp-agent/tsconfig.json b/packages/ui/acp-agent/tsconfig.json index 13009a2e5c..5eb1c62282 100644 --- a/packages/ui/acp-agent/tsconfig.json +++ b/packages/ui/acp-agent/tsconfig.json @@ -23,6 +23,9 @@ { "path": "../acp" }, + { + "path": "../../core/agent" + }, { "path": "../../core/agent-core" }, diff --git a/packages/ui/acp/README.md b/packages/ui/acp/README.md index 49abfbff6a..f4947e5d25 100644 --- a/packages/ui/acp/README.md +++ b/packages/ui/acp/README.md @@ -1,6 +1,6 @@ # @deepseek-ai/dsh-acp -The **Agent Client Protocol (ACP)** bridge: exposes DeepSeek Harness SDK agents as an ACP server over JSON-RPC stdio, so editors (Zed and other ACP clients) can drive them — streaming render, tool-call display, and resumable sessions. Zed is the current target client: baseline ACP behavior should remain reasonable for other clients, but bridge capabilities and compatibility decisions are evaluated against Zed first. **N concurrent sessions per connection** (see [ACP multi-session](../../../docs/rfc/proposed/feature/2026-06-14-acp-multi-session.md)): each maps to its own `ReactLoopAgent`, and every event is demuxed strictly by session id so two sessions streaming at once never interleave. +The **Agent Client Protocol (ACP)** bridge: exposes DeepSeek Harness SDK agents as an ACP server over JSON-RPC stdio, so editors (Zed and other ACP clients) can drive them — streaming render, tool-call display, and resumable sessions. Zed is the current target client: baseline ACP behavior should remain reasonable for other clients, but bridge capabilities and compatibility decisions are evaluated against Zed first. **N concurrent sessions per connection** (see [ACP multi-session](../../../docs/rfc/implemented/feature/2026-06-14-acp-multi-session.md)): each maps to its own `ReactLoopAgent`, and every event is demuxed strictly by session id so two sessions streaming at once never interleave. It is a **client-driver / UI plugin**, the structured analogue of the readline `stdio-chat` plugin — NOT a loop change and NOT a [capability seam](../../../docs/rfc/implemented/architecture/2026-06-13-capability-seams.md). It consumes the existing `agent/*` event taxonomy, the `dsh-agent` create/resume factory, and `dsh-session-persistence`. @@ -16,7 +16,7 @@ It is a **client-driver / UI plugin**, the structured analogue of the readline ` |---|---|---| | `model` | — | Model name for created agents (must have a registered adapter). | -(No persona key: the deployment persona is `dsh-system-prompt`'s own `persona` config — a context-wide section, so ACP-created agents render it without the bridge carrying prompt text.) +(No persona key: `dsh-system-prompt`'s own `persona` config supplies the global default section, so ACP-created agents render it without the bridge carrying prompt text. An agent-scoped same-name section may still shadow that default.) The `initialize` handshake reports a fixed server identity (`agentInfo: { name: 'deepseek-harness-acp', version: '0.0.1' }`) — branding is a literal at the `initialize` site, not config. @@ -31,10 +31,16 @@ The `initialize` handshake reports a fixed server identity (`agentInfo: { name: | `session/cancel` | `agent.cancel()` | the queue-aware cancel: aborts a running step, clears queued + steering work, and drops a turn about to start, then settles the prompt `cancelled` — for ONLY that session (a cancel never touches another session's stream or prompt) | | `session/update` | `session/event` | `agent_message_chunk` (text-delta), `agent_thought_chunk` (reasoning-delta), `user_message_chunk` (load replay), `tool_call`/`tool_call_update` (the render intent — a `card`-tagged `ToolCallView`/`ToolResultView` — owned by the TOOL via `presentCall`/`presentResult`, which the bridge switches on to build the wire shape — see Tool-call presentation) | | `elicitation/create` | `ctx.userInteraction.ask()` | maps `ask_user_question` questions to ACP form elicitations; option descriptions are shown in enum titles, `multi_select` uses ACP array enums, optionless requests use a required `custom` field, and a non-empty custom answer overrides any selected choice | +| `session/request_permission` | `approval/request` listener | the bridge is the [`ctx.approval`](../user-approval/README.md) answerer for the agents it owns: an `ask` (a hook or `tools/pre-execute` plugin) becomes an editor prompt attached to the streamed tool call, offering one-shot `allow_once`/`reject_once` options only; a foreign or call-less request delegates down the answerer chain (fail-closed `unavailable` default). See "Permission prompts" | +| `session/set_config_option` | `setSandboxMode` / `setApprovalPolicy` | per-session knob switching over [session config options](https://agentclientprotocol.com/protocol/session-config-options) — see "Session config options" | ## Multi-session -The bridge multiplexes N sessions over one connection. Live sessions are held in a `Map` (forward) with a `WeakMap` reverse map so `agent/*` events — which carry only the `Agent` — demux in O(1). Every `session/event` and `agent/status` is routed strictly to its owning record, so concurrent sessions never cross-settle or interleave their `session/update` notifications. State is per session: one in-flight prompt each, `session/cancel` aborts and settles only its own agent/prompt, and disposal drains every live session in parallel to quiescence. (Per-session *permission* ownership is reserved for the deferred permission gate — `TODO(rfc010-permission-gate)`.) +The bridge multiplexes N sessions over one connection. Live sessions are held in a `Map` (forward) with a `WeakMap` reverse map so agent-scoped approval events demultiplex in O(1). Every `session/event` is routed strictly to its owning record, so concurrent sessions never cross-settle or interleave their `session/update` notifications. State is per session: one in-flight prompt each, `session/cancel` aborts and settles only its own agent/prompt, and disposal drains every live session in parallel to quiescence. Permission prompts follow the same ownership: the `approval/request` answerer resolves the owning session through the reverse map and prompts only there. + +## Session config options + +The bridge advertises one independent `select` per composable knob in the `session/new`/`session/load` responses — `sandbox-mode` (`read-only`/`workspace-write`/`danger-full-access`, category `mode`) iff the mounted executor confines (`ctx.get('bash')?.sandboxMode` defined), `approval-policy` (`ask`/`never`) iff the approval seam is composed — with each session's `currentValue` folded from its OWN log (`effectiveSandboxMode`/`effectiveApprovalPolicy` ?? the composition default), so `session/load` reports a resumed session's overrides with no catch-up machinery. `session/set_config_option` validates the value against the same closed vocabulary, routes to the domain's write path (`setSandboxMode`/`setApprovalPolicy` — ONE log-only event on that session's log), and returns the complete refreshed state per the spec. Anchoring honors turn-enclosure: a switch while a turn is open appends immediately (openness read from the LOG — `agent.status` stays `running` between queued turns); an idle switch is held on the session record and anchored at the next turn's `agent/prompt-submit` (inside the turn, before anything assembles, last write per knob — an idle flip-flop anchors as one event), because appending from inside a `session/event` listener would reorder events for later-registered peers. Until anchored the switch lives in bridge memory only: responses overlay it truthfully, and a crash before the next turn reverts it — `session/load` then reports the fold's truth. Design: [the sandbox RFC § Per-session mode switching](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md); protocol matrix: [acp-feature-support.md](acp-feature-support.md) § 6. Background-task isolation rides on `dsh-tool-bash`: bash task ids are global and predictable, so each task carries an opaque owner token — the owning agent's `session.header.id` — stored on the task inside the executor (`dsh-bash`'s `ownerOf(id)` seam). `bash_output`/`bash_kill` reject a task whose token differs from the caller's session token, so one session's agent can't read or kill another's task. Ownership is by session TOKEN, not `Agent` object identity — a different `Agent` object on the same session may access the task — and because the token lives on the executor's task it survives a `tool-bash` HMR reload. @@ -65,7 +71,11 @@ When the client does NOT advertise the capability, none of the `_meta`/terminal ## Settle-exactly-once -A `session/prompt` resolves (or rejects) exactly once, keyed off the canonical session log (the `session/event` stream). One listener captures the prompt's owning turn from the log's `turn/start` and settles on the matching `turn/end` — the durable boundary event (`closeTurn` appends it unconditionally; there is no `agent/*` turn mirror). A prompt settles only on ITS OWN turn (`inflight.turn === turn/end.turn`), so a stale `turn/end` for a previously-cancelled turn whose end arrives late can never settle the wrong prompt. A turn that ends `error` REJECTS the RPC with an internal error carrying the failure message (ACP has no error stop reason); every other reason resolves via the codec. As a fallback, when the agent settles to `idle`/`disposed` with a prompt still pending — e.g. a peer `session/event` listener registered before the bridge threw and starved the bridge's listener — an `agent/status` handler reconciles the prompt from the log (the owning turn's `turn/end`, or `cancelled` if the turn was torn down without one). An empty/whitespace prompt is rejected up front — it would queue no work, so no turn would start and the RPC would hang. +A `session/prompt` resolves or rejects exactly once from the canonical `session/event` stream. The listener captures the prompt's owning turn from `turn/start` and settles in a `finally` block when the matching `turn/end` is appended, so a presentation/streaming failure cannot strand the RPC after the durable terminal event exists. Correlation by turn id prevents a late end from a cancelled prompt from settling its successor. A turn ending in `error` rejects the RPC with an internal error carrying the failure message because ACP has no error stop reason; every other reason resolves through the codec. An empty or whitespace-only prompt is rejected before enqueue because it would start no turn and otherwise leave the RPC pending. + +## Permission prompts + +The bridge registers an `approval/request` waterfall listener — the ACP answerer of the [user-approval seam](../user-approval/README.md). When `ctx.approval` routes an `ask` for an agent the bridge owns, the listener resolves the owning session through the reverse map and issues `session/request_permission` with the request's `callId` as the `toolCall` reference (the editor attaches the prompt to the already-streamed call) and the one-shot options `allow_once`/`reject_once` (`allow_always` is deferred to the approval RFC's grant-storage question). Outcomes map `allow-once → allowed-once`, any other selection → `rejected` (an unknown optionId from a non-conforming client never grants), client `cancelled → cancelled`. A request for an agent the bridge does NOT own — or one without a `callId` to attach to — delegates via `next()` so another answerer or the seam's fail-closed `unavailable` default takes it. A rejected `requestPermission` RPC (client gone mid-prompt) propagates to the ApprovalService, which contains it as `unavailable`. Whether a call asks at all is policy — a hook or `tools/pre-execute` plugin returning `ask` — never the bridge's own judgment; without such policy, tools keep the executor's full authority. ## Disposal & disconnect @@ -73,12 +83,11 @@ Teardown reaches quiescence: for EVERY live session settle any pending prompt as ## Known limitations (tracked TODOs) -- **`TODO(rfc010-permission-gate)`** — the `tools/pre-execute` permission gate (`session/request_permission`) is NOT implemented; tools run with the executor's full authority. The `agent→sessionId` reverse map is in place so the gate can route a permission request (which receives only `exec.agent`) back to its originating session. [ACP support](../../../docs/rfc/proposed/feature/2026-06-14-acp-agent-client-protocol.md) and [ACP multi-session](../../../docs/rfc/proposed/feature/2026-06-14-acp-multi-session.md) stay `proposed` until the gate (and per-session permission ownership) land. - **`additionalDirectories`** — rejected. A session operates in its single `cwd` (see Per-session cwd); widening the tool/filesystem scope to extra roots is a separate sandbox concern, not yet implemented. ## stdout is the protocol -The JSON-RPC frames go on stdout, so this plugin MUST run in an example that loads **no stdout logger** (the console logger writes to stdout and would corrupt the frames). The guarantee is config-only — see `examples/acp-agent` (no console logger) and [ACP support risks](../../../docs/rfc/proposed/feature/2026-06-14-acp-agent-client-protocol.md#risks). A stderr exporter is fine for logging. +The JSON-RPC frames go on stdout, so this plugin MUST run in an example that loads **no stdout logger** (the console logger writes to stdout and would corrupt the frames). The guarantee is config-only — see `examples/acp-agent` (no console logger) and [ACP support risks](../../../docs/rfc/implemented/feature/2026-06-14-acp-agent-client-protocol.md#risks). A stderr exporter is fine for logging. ## Running diff --git a/packages/ui/acp/acp-feature-support.md b/packages/ui/acp/acp-feature-support.md index 6162fae137..be14fe6237 100644 --- a/packages/ui/acp/acp-feature-support.md +++ b/packages/ui/acp/acp-feature-support.md @@ -10,7 +10,7 @@ Legend: ✅ supported · ⚠️ partial / fallback · ❌ not yet · — n/a. Th ## At a glance -The bridge implements the **core prompt-turn loop** for N concurrent sessions: initialize, session new/load, prompt, cancel, streamed assistant/thought chunks, tool-call rendering (including Zed terminal cards), and resumable session replay. The largest **unbuilt** areas are the **permission gate** (`session/request_permission`), **MCP passthrough**, **session modes / config options / model selection**, **slash commands**, and **agent plans** — all of which both reference adapters ship — plus the client **filesystem** and **terminal** method families (which the adapters mostly do NOT drive either — see rows 43-49). See [Gap summary](#gap-summary). +The bridge implements the **core prompt-turn loop** for N concurrent sessions: initialize, session new/load, prompt, cancel, streamed assistant/thought chunks, tool-call rendering (including Zed terminal cards), resumable session replay, one-shot permission prompts, and per-session sandbox/approval config options. The largest **unbuilt** areas are **MCP passthrough**, runtime model selection, **slash commands**, and **agent plans**, plus the client **filesystem** and **terminal** method families (which the adapters mostly do NOT drive either — see rows 43-49). See [Gap summary](#gap-summary). ## 1. Agent methods (client → agent) @@ -25,8 +25,8 @@ The bridge implements the **core prompt-turn loop** for N concurrent sessions: i | `session/close` | S | ❌ | ✅ | ✅ | No `session/close` handler — the SDK dispatch returns `method_not_found`. The bridge tears sessions down on client disconnect / Cordis disposal (cross-cutting, see [§8](#8-cross-cutting)), but that is not the on-demand per-session method. | | `session/prompt` | S | ✅ | ✅ | ✅ | Maps to `agent.send`; one in-flight prompt per session; settles on the owning turn's end. | | `session/cancel` | S | ✅ | ✅ | ✅ | Queue-aware `agent.cancel`; settles the in-flight prompt `cancelled`, scoped to the one session. | -| `session/set_mode` | S | ❌ | ✅ | ✅ | Session modes not modeled (see [§6 Modes](#6-session-modes--config-options--models)). | -| `session/set_config_option` | S | ❌ | ✅ | ✅ | Config options not modeled. | +| `session/set_mode` | S | ❌ | ✅ | ✅ | Session modes deliberately skipped: config options are the spec's replacement (modes are slated for removal in ACP v2), and one mode list cannot carry the two orthogonal knobs (see [§6](#6-session-modes--config-options--models)). | +| `session/set_config_option` | S | ✅ | ✅ | ✅ | Two capability-gated selects — `sandbox-mode` (confining executor mounted) and `approval-policy` (approval seam composed); values validated against the domain vocabularies, one log-only event per switch on the session's own log, complete refreshed state in the response ([sandbox RFC § Per-session mode switching](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md)). | | model selection | S | ❌ | ✅ | ✅ | No distinct stable `session/set_model` — model is the `model`-category `session/set_config_option`. The bridge fixes the model per-bridge via config; no runtime switch. Codex still uses the legacy `unstable_setSessionModel` ext method. | | `session/list` | S | ❌ | ✅ | ✅ | Gated by `sessionCapabilities.list`. The harness HAS `sessionPersistence.list()` (used internally for load-cwd validation) but does not expose it over ACP. | | `session/delete` | S | ❌ | ✅ | ✅ | Gated by `sessionCapabilities.delete`. | @@ -39,7 +39,7 @@ These are capabilities the bridge would *drive* on the editor. The harness runs | Method | Stable | Bridge | Claude | Codex | Notes | |---|---|---|---|---|---| | `session/update` | S | ✅ | ✅ | ✅ | The bridge's primary output channel (see [§4](#4-sessionupdate-variants)). | -| `session/request_permission` | S | ❌ | ✅ | ✅ | **The biggest gap.** Tools run with the executor's full authority; no user authorization round-trip. The `agent→sessionId` reverse map is already in place to route a future permission request. Tracked `TODO(rfc010-permission-gate)`. | +| `session/request_permission` | S | ✅ | ✅ | ✅ | The bridge answers the [`ctx.approval`](../user-approval/README.md) seam for the agents it owns: an `ask` from a hook/plugin becomes an editor prompt attached to the streamed tool call, one-shot `allow_once`/`reject_once` options only. Whether a call asks is policy (nothing asks by default); `allow_always` is deferred (grant storage). | | `fs/read_text_file` | S | ❌ | ✅ | ❌ | The harness reads files directly (it does not see the editor's unsaved buffer state). Claude delegates; Codex does not. | | `fs/write_text_file` | S | ❌ | ✅ | ❌ | Same — direct writes, no editor delegation. | | `terminal/create` | S | ❌ | ❌ | ❌ | Neither reference adapter drives the client terminal API either — both, like the bridge, render shell output as tool-call content + a `_meta` channel (see [§5 Terminal](#terminal-rendering)). | @@ -86,7 +86,7 @@ These are capabilities the bridge would *drive* on the editor. The harness runs | `plan` | S | ❌ | ✅ | ✅ | No agent plan emitted. Both adapters emit real plan entries (Codex's `CodexEventHandler.updatePlan` maps `turn/plan/updated` → `{ sessionUpdate: 'plan', entries }`). | | `available_commands_update` | S | ❌ | ✅ | ✅ | No slash commands advertised. | | `current_mode_update` | S | ❌ | ✅ | ✅ | No session modes. | -| `config_option_update` | S | ❌ | ✅ | ✅ | No config options. | +| `config_option_update` | S | ❌ | ✅ | ✅ | Config options exist (advertised in `session/new`/`session/load`, switched via `session/set_config_option`), but the bridge never pushes agent-initiated changes — an operator default drift is narrated to the MODEL, not echoed to the editor. Future work in the [sandbox RFC § Per-session mode switching](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md). | | `usage_update` | S | ❌ | ✅ | ✅ | Token/cost reporting not surfaced (the harness records token usage internally on `assistant/message`). | | `session_info_update` | S | ❌ | ⚠️ | ⚠️ | Session title/metadata not pushed. | @@ -111,7 +111,7 @@ Tool-call presentation is **owned by each tool** (`presentCall` / `presentResult ## 6. Session modes / config options / models -❌ None modeled. Both reference adapters ship modes (Claude: a "plan" auto-mode; Codex: read-only / agent / agent-full-access mapping to its approval+sandbox policy), the newer config-option surface, and runtime model selection. The harness fixes the model per-bridge via `AcpConfig.model`. These are coupled to the unbuilt **permission gate** (a mode often selects an approval policy), so they are natural follow-ups to it. +Config options ✅ (the [sandbox RFC § Per-session mode switching](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md)): the bridge advertises one independent `select` per composable knob — `sandbox-mode` iff the mounted executor confines, `approval-policy` iff the approval seam is composed — with per-session current values folded from each session's own log, and honors `session/set_config_option` end to end (idle switches anchor at the next turn under the turn-enclosure contract). Session MODES stay deliberately unmodeled: config options are the spec's replacement (modes are slated for removal in ACP v2), and one mode list cannot carry two orthogonal knobs. Runtime model selection is still not modeled — the harness fixes the model per-bridge via `AcpConfig.model` (both reference adapters ship a model selector). ## 7. Content blocks @@ -130,7 +130,7 @@ The bridge rejects unsupported prompt blocks rather than silently dropping them | Feature | Stable | Bridge | Notes | |---|---|---|---| | `StopReason` mapping | S | ✅ | `turnEndToStopReason` is total over harness turn-end reasons → `end_turn`/`max_tokens`/`cancelled`. | -| Multi-session (N per connection) | S | ✅ | Strict per-session demux; concurrent streams never interleave. See the [multi-session RFC](../../../docs/rfc/proposed/feature/2026-06-14-acp-multi-session.md). | +| Multi-session (N per connection) | S | ✅ | Strict per-session demux; concurrent streams never interleave. See the [multi-session RFC](../../../docs/rfc/implemented/feature/2026-06-14-acp-multi-session.md). | | Disconnect / disposal teardown | S | ✅ | Quiesces every live session on client disconnect or Cordis disposal. | | `_meta` extensibility | S | ⚠️ | Consumed (Zed terminal cap) and emitted (terminal `_meta`); no other custom extensions. | | Background-task ownership isolation | — | ✅ | `bash_output`/`bash_kill` reject another session's task via an opaque owner token. | @@ -140,15 +140,14 @@ The bridge rejects unsupported prompt blocks rather than silently dropping them Ranked by how commonly the reference adapters ship them and how much UX they unlock: -1. **Permission gate** — `session/request_permission` + permission options. Tracked `TODO(rfc010-permission-gate)`; the reverse map is already wired and shared with `ask_user_question` routing. Foundational, and a prerequisite for modes. -2. **Session lifecycle** — `session/list` + `session/delete` (the persistence layer already lists), then `session/resume` / `session/close`. -3. **Modes / config options / model selection** — coupled to the permission gate. -4. **Agent plan** (`sessionUpdate: 'plan'`) — surface the loop's plan as structured entries. -5. **Slash commands** (`available_commands_update`). -6. **MCP passthrough** (`mcpServers` on `session/new` + `mcpCapabilities`). -7. **Richer prompt content** — image / embedded `resource` blocks (needs a multimodal model path). -9. **Usage reporting** (`usage_update`) — the harness already records token usage internally (on `assistant/message`). -10. **Editor filesystem delegation** (`fs/read_text_file` / `fs/write_text_file`) — lets the agent see unsaved buffers; lower priority since the harness has direct disk access. +1. **Session lifecycle** — `session/list` + `session/delete` (the persistence layer already lists), then `session/resume` / `session/close`. +2. **Model selection** — sandbox and approval config options are implemented; selecting the bridge's model at runtime remains open. +3. **Agent plan** (`sessionUpdate: 'plan'`) — surface the loop's plan as structured entries. +4. **Slash commands** (`available_commands_update`). +5. **MCP passthrough** (`mcpServers` on `session/new` + `mcpCapabilities`). +6. **Richer prompt content** — image / embedded `resource` blocks (needs a multimodal model path). +7. **Usage reporting** (`usage_update`) — the harness already records token usage internally (on `assistant/message`). +8. **Editor filesystem delegation** (`fs/read_text_file` / `fs/write_text_file`) — lets the agent see unsaved buffers; lower priority since the harness has direct disk access. ## Out of scope diff --git a/packages/ui/acp/package.json b/packages/ui/acp/package.json index 4ebc8485ce..a916722c52 100644 --- a/packages/ui/acp/package.json +++ b/packages/ui/acp/package.json @@ -28,7 +28,10 @@ }, "peerDependencies": { "@deepseek-ai/dsh-agent": "^0.0.1", + "@deepseek-ai/dsh-user-approval": "^0.0.1", + "@deepseek-ai/dsh-bash": "^0.0.1", "@deepseek-ai/dsh-llm": "^0.0.1", + "@deepseek-ai/dsh-sandbox": "^0.0.1", "@deepseek-ai/dsh-session": "^0.0.1", "@deepseek-ai/dsh-session-persistence": "^0.0.1", "@deepseek-ai/dsh-tools": "^0.0.1", @@ -38,10 +41,14 @@ "devDependencies": { "@deepseek-ai/dsh-agent": "workspace:^", "@deepseek-ai/dsh-agent-loop": "workspace:^", + "@deepseek-ai/dsh-user-approval": "workspace:^", + "@deepseek-ai/dsh-bash": "workspace:^", "@deepseek-ai/dsh-bash-local": "workspace:^", "@deepseek-ai/dsh-fs-local": "workspace:^", "@deepseek-ai/dsh-fs-policy": "workspace:^", + "@deepseek-ai/dsh-invariants": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", + "@deepseek-ai/dsh-sandbox": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", "@deepseek-ai/dsh-session-persistence": "workspace:^", "@deepseek-ai/dsh-session-persistence-jsonl": "workspace:^", diff --git a/packages/ui/acp/src/index.ts b/packages/ui/acp/src/index.ts index 928ab7da77..5a5ce9e51f 100644 --- a/packages/ui/acp/src/index.ts +++ b/packages/ui/acp/src/index.ts @@ -22,8 +22,10 @@ * `agent→sessionId` reverse map for O(1) demux of `agent/*` events; every * `session/event` and `agent/*` event is routed strictly to its owning session * record, so two sessions streaming at once never interleave their - * `session/update` notifications. The `tools/pre-execute` permission gate is - * deferred — see the TODO(rfc010-permission-gate) note below. + * `session/update` notifications. Permission prompts ride the same ownership + * map: the bridge answers `approval/request` for its own agents over + * `session/request_permission` (see the approval answerer below) — whether a + * call ASKS is policy (a hook or plugin returning `ask`), not the bridge's. * * stdout is the protocol: this plugin must run in an example that loads NO * stdout logger (the console logger writes to stdout and would corrupt the @@ -60,20 +62,30 @@ import { type PlanEntry, type PromptRequest, type PromptResponse, + type SessionConfigOption, type SessionNotification, + type SetSessionConfigOptionRequest, + type SetSessionConfigOptionResponse, type Stream, type StopReason, } from '@agentclientprotocol/sdk' import type { ContentBlock } from '@deepseek-ai/dsh-llm' import { assertNever, CallId } from '@deepseek-ai/dsh-llm' -import type { Agent, AgentStatus } from '@deepseek-ai/dsh-agent' +import type { Agent } from '@deepseek-ai/dsh-agent' import { AgentId } from '@deepseek-ai/dsh-agent' import { SessionId } from '@deepseek-ai/dsh-session' +import { SANDBOX_MODES, effectiveSandboxMode, setSandboxMode } from '@deepseek-ai/dsh-bash' +import { APPROVAL_POLICIES, effectiveApprovalPolicy, setApprovalPolicy } from '@deepseek-ai/dsh-user-approval' +import type { SandboxMode } from '@deepseek-ai/dsh-sandbox' +import type { ApprovalPolicy } from '@deepseek-ai/dsh-user-approval' import type { SessionEvent, TodoItem, TurnEndReason } from '@deepseek-ai/dsh-session' import type { ToolCallView, ToolRegistry, ToolResultView, TerminalResultView } from '@deepseek-ai/dsh-tools' // Side-effect type import: declaration-merges `ctx.sessionPersistence` onto // Context (the bridge injects it and reads `list()` for load cwd validation). import type {} from '@deepseek-ai/dsh-session-persistence' +// Side-effect type import: declaration-merges the `approval/request` waterfall +// the bridge answers for its own agents (see the approval answerer below). +import type {} from '@deepseek-ai/dsh-user-approval' import { UserInteractionError, type AskUserQuestionAnswer, @@ -285,7 +297,8 @@ interface SessionRecord { /** * The in-flight `session/prompt`, or `undefined` when none is pending. A * prompt resolves with a {@link StopReason} or rejects with an Error (a - * turn that ended in failure). Settled exactly once via {@link settlePrompt}. + * turn that ended in failure). Settled exactly once by its matching + * `turn/end`, direct cancellation, or teardown. * * `turn` is the loop turn number this prompt owns, captured from the log's * `turn/start` after `send()`. Until then it is `undefined` (the turn has not @@ -295,30 +308,32 @@ interface SessionRecord { * wrong prompt. A direct cancel/dispose settle clears the whole in-flight slot, * so a later stale `turn/end` finds no pending prompt. * - * `logWatermark` is the session log length at the moment the prompt was - * installed (before `send()`). The settle-from-log fallback uses it to infer - * the owning `turn/start` from the canonical log even when the live - * `session/event` capture was starved (a peer listener that throws on - * `turn/start` — see `settleFromLog`): the prompt owns the FIRST `turn/start` - * appended at or after this watermark. */ inflight: { resolve: (reason: StopReason) => void reject: (error: Error) => void turn: number | undefined - logWatermark: number } | undefined + /** + * Config switches accepted while the session was IDLE, not yet anchored in + * its log. The turn-enclosure contract makes a bare between-turns append + * invalid (the JSONL backend treats a post-`turn/end` tail as crash + * garbage, and dev invariants throw), so an idle switch waits here and is + * anchored at the next turn's prompt-submit — before anything in that + * turn assembles a prompt or runs a call, and last write + * per knob wins (an idle flip-flop anchors as one event). Until anchored, + * the switch lives only in bridge memory: the set/new/load responses + * overlay it truthfully, and a restart before the next turn reverts it — + * which `session/load` then reports honestly from the log's fold. + */ + pendingSwitches: { sandboxMode?: SandboxMode; approvalPolicy?: ApprovalPolicy } } /** * Drive the in-flight prompt's settle from the harness event stream. The bridge - * settles off the durable log: the `turn/end` session event on the - * `session/event` feed for the prompt's own turn, with the agent - * erroring/settling to idle as a fallback (docs/defensive-patterns.md "honor - * cross-seam contracts on BOTH sides") for the case where a throwing peer `session/event` listener - * starved the bridge's listener before it saw the boundary. The first of these - * to fire settles the prompt; `settle` is then cleared so the others are no-ops - * (settle-exactly-once). + * settles off the durable `turn/end` event for the prompt's own turn. Session + * contains post-commit observers independently, and this listener performs + * correlation in a `finally` so presentation failure cannot starve settlement. */ export function apply(ctx: Context, config: AcpConfig): void { // Capture the injected services NOW, during apply(), while we are inside this @@ -335,7 +350,7 @@ export function apply(ctx: Context, config: AcpConfig): void { const userInteraction = ctx.userInteraction // A new ToolPresenter per session (and a throwaway per load replay), each given // this warn sink so a throwing tool presenter is logged, not propagated. - const makePresenter = (): ToolPresenter => new ToolPresenter(tools, (message) => { logger.warn(message) }) + const makePresenter = (agent?: Agent): ToolPresenter => new ToolPresenter(tools, (message) => { logger.warn(message) }, agent) // Live sessions keyed by id (RFC 011 multi-session), plus an agent→sessionId // reverse map so `agent/*` events (which carry only the Agent) demux in O(1). @@ -476,87 +491,156 @@ export function apply(ctx: Context, config: AcpConfig): void { ctx.on('session/event', (session, event: SessionEvent) => { const rec = sessions.get(session.header.id) if (rec === undefined) return - streamSessionEventUpdate(rec.sessionId, event, notify, rec.presenter, { - enabled: rec.terminalEnabled, - cwd: session.header.cwd, - }, { includeUserMessages: false }) - const inflight = rec.inflight - if (inflight === undefined) return - if (event.type === 'turn/start') { - // Tag the in-flight prompt with its owning turn — but ONLY a - // `message`-triggered turn (the kind a `send()` prompt produces). A turn - // a plugin opens between prompt-install and the prompt's own turn (an idle - // `agent.inject()` writes a one-shot `injection`-triggered turn) must NOT - // be mistaken for the prompt's turn, or its turn/end would settle the RPC - // early. The first message turn at/after install owns the prompt - // (`turn === undefined` guard); the loop batches queued messages into one - // turn, so there is exactly one. - if (inflight.turn === undefined && event.data.trigger.kind === 'message') { - inflight.turn = event.data.turn + try { + streamSessionEventUpdate(rec.sessionId, event, notify, rec.presenter, { + enabled: rec.terminalEnabled, + cwd: session.header.cwd, + }, { includeUserMessages: false }) + } finally { + const inflight = rec.inflight + if (inflight !== undefined && event.type === 'turn/start') { + // The first message-triggered turn after prompt installation owns the + // prompt; injection-triggered turns must not settle it early. + if (inflight.turn === undefined && event.data.trigger.kind === 'message') { + inflight.turn = event.data.turn + } + } else if (inflight !== undefined && event.type === 'turn/end' && inflight.turn === event.data.turn) { + rec.inflight = undefined + settleFromTurnEnd(inflight, event.data.reason) } - return } - // Settle only on the OWNING turn's end. - if (event.type !== 'turn/end' || inflight.turn !== event.data.turn) return - rec.inflight = undefined - settleFromTurnEnd(inflight, event.data.reason) }) - // Settle fallback: a `session/event` listener registered BEFORE ACP that - // throws (on `turn/start` OR `turn/end`) would, via cordis `emit`'s - // stop-on-throw, starve ACP's listener above — the prompt would hang or, if - // only the turn number was missed, settle as the wrong outcome. So when the - // agent settles to `idle` (or is disposed), reconcile against the canonical - // log: determine the prompt's owning turn (the captured `turn`, or — if the - // live capture was starved — the FIRST `turn/start` appended at/after the - // install-time `logWatermark`), then settle from that turn's `turn/end` - // (reject on error, resolve via codec), or `cancelled` if no owning turn ever - // started. Never double-settles — clears `inflight` first. - const settleFromLog = (rec: SessionRecord): void => { - const inflight = rec.inflight - if (inflight === undefined) return - const events = rec.agent.session.events - // The owning turn number: the captured one, or — if the live capture was - // starved — inferred from the log as the first MESSAGE-triggered turn opened - // at/after the watermark. The message-trigger filter matches the live - // capture: a one-shot `injection` turn a plugin may open between - // prompt-install and the prompt's turn is NOT the prompt's turn. Undefined - // only if no message turn ever started for this prompt. - const owningTurn = inflight.turn ?? events.slice(inflight.logWatermark).find( - (e): e is Extract => - e.type === 'turn/start' && e.data.trigger.kind === 'message', - )?.data.turn - // The owning turn's end in the log. If `owningTurn` is undefined (no turn - // ever started for this prompt — a torn-down-before-turn case that quiesce's - // direct settle normally pre-empts), no `turn/end` matches (turn numbers are - // >= 1) and `findLast` returns undefined, falling through to cancelled. - const end = events.findLast( - (e): e is Extract => - e.type === 'turn/end' && e.data.turn === owningTurn, - ) - rec.inflight = undefined - if (end === undefined) { - // No owning turn / no clean turn/end (torn down mid-turn) → cancelled. - inflight.resolve('cancelled') - return - } - settleFromTurnEnd(inflight, end.data.reason) - } - - // On a settle to idle/disposed, reconcile any still-pending prompt from the - // log (covers a starved `session/event` listener — see settleFromLog). A mid- - // step disposal that never appended a clean turn/end resolves `cancelled`. - // Demux via the agent→sessionId reverse map. - ctx.on('agent/status', (agent, status: AgentStatus) => { - const sessionId = bySession.get(agent) - if (sessionId === undefined) return - const rec = sessions.get(sessionId) - if (rec === undefined) return - if (status === 'idle' || status === 'disposed') settleFromLog(rec) + // --- Approval answerer ----------------------------------------------------- + // The bridge is the approval channel for the agents it owns: an `ask` routed + // through `ctx.approval` (dsh-tools asks and sandbox escalation) becomes + // an editor permission prompt attached to the already-streamed tool call. The + // listener occupies the single decision slot ONLY for its own agents — a + // foreign or call-less request delegates via next() so another answerer (or + // the fail-closed `unavailable` default) takes the question. A rejected + // `requestPermission` (client gone, bridge torn down) propagates and the + // ApprovalService contains it as `unavailable`. Options are one-shot only: + // allow_always is a grant-storage design the approval RFC defers, so the + // prompt never offers a durable grant the harness could not honor. + ctx.on('approval/request', (req, next) => { + const sessionId = bySession.get(req.agent) + // The protocol requires `toolCall` (the prompt renders attached to it), so + // a request without a callId has nothing to attach to — delegate. + if (sessionId === undefined || req.callId === undefined) return next() + return conn.requestPermission({ + sessionId, + toolCall: { toolCallId: req.callId }, + options: [ + { optionId: 'allow-once', name: 'Allow once', kind: 'allow_once' }, + { optionId: 'reject-once', name: 'Reject', kind: 'reject_once' }, + ], + }).then(({ outcome }) => { + if (outcome.outcome === 'cancelled') return 'cancelled' + // Only the two advertised options exist; an unknown optionId from a + // non-conforming client counts as a rejection, never a grant. + return outcome.optionId === 'allow-once' ? 'allowed-once' : 'rejected' + }) }) // --- The ACP Agent method surface ----------------------------------------- + /** + * The session config options this composition can honor, with current + * values folded from the AGENT'S OWN session log (`effectiveSandboxMode` / + * `effectiveApprovalPolicy` — the log is the per-session store, so a + * `session/load` reports a resumed session's overrides with no catch-up + * machinery), overlaid with the record's not-yet-anchored pending switches + * (see {@link SessionRecord.pendingSwitches}). Capability-gated like every + * advertised lever: the sandbox option exists only when the mounted + * executor confines (`ctx.get('bash')?.sandboxMode` defined), the approval + * option only when the approval seam is composed — both read + * opportunistically so this bridge keeps working in compositions without + * them. + */ + const configOptionsFor = (agent: Agent, pending: SessionRecord['pendingSwitches'] = {}): SessionConfigOption[] => { + const options: SessionConfigOption[] = [] + const defaultMode = ctx.get('bash')?.sandboxMode + if (defaultMode !== undefined) { + options.push({ + id: 'sandbox-mode', + name: 'Sandbox', + description: 'The file sandbox mode bash commands in this session run under.', + category: 'mode', + type: 'select', + currentValue: pending.sandboxMode ?? effectiveSandboxMode(agent.session.events) ?? defaultMode, + options: SANDBOX_MODES.map(mode => ({ value: mode, name: mode })), + }) + } + const approval = ctx.get('approval') + if (approval !== undefined) { + options.push({ + id: 'approval-policy', + name: 'Approvals', + description: 'ask: permission prompts reach you; never: they are rejected automatically.', + type: 'select', + // `?? 'ask'` also shields against a provided stand-in whose config + // never went through the plugin schema (tests do this). + currentValue: pending.approvalPolicy ?? effectiveApprovalPolicy(agent.session.events) ?? approval.config.policy ?? 'ask', + options: APPROVAL_POLICIES.map(policy => ({ value: policy, name: policy })), + }) + } + return options + } + + /** + * Whether the session's log currently has an open turn — the last boundary + * event is a `turn/start`. Decides whether a config switch may append NOW + * (enclosed) or must wait for the next turn (see + * {@link SessionRecord.pendingSwitches}). Read from the LOG, not + * `agent.status`: status stays `running` across the gap between two queued + * turns, where a bare append would still land outside any turn. + */ + const isTurnOpen = (agent: Agent): boolean => { + const events = agent.session.events + for (let index = events.length - 1; index >= 0; index -= 1) { + const type = (events[index] as SessionEvent).type + if (type === 'turn/start') return true + if (type === 'turn/end') return false + } + return false + } + + /** + * Anchor a record's pending switches into its (just-opened) turn, last + * write per knob — skipping a value the session already effectively has, + * so a net-zero idle flip-flop anchors NOTHING (the log records switches, + * not select clicks). + */ + const flushPendingSwitches = (rec: SessionRecord): void => { + const pending = rec.pendingSwitches + rec.pendingSwitches = {} + const events = rec.agent.session.events + if (pending.sandboxMode !== undefined + && pending.sandboxMode !== (effectiveSandboxMode(events) ?? ctx.get('bash')?.sandboxMode)) { + setSandboxMode(rec.agent.session, pending.sandboxMode) + } + if (pending.approvalPolicy !== undefined + && pending.approvalPolicy !== (effectiveApprovalPolicy(events) ?? ctx.get('approval')?.config.policy ?? 'ask')) { + setApprovalPolicy(rec.agent.session, pending.approvalPolicy) + } + } + + // Idle-accepted switches anchor at the next turn's prompt-submit: the turn + // is open (the seam fires inside it, per drained message — the first flush + // empties the slot, later ones no-op), the loop has not yet assembled + // anything for it, and — unlike appending from inside a `session/event` + // listener — this seam fires OUTSIDE any log emit, so peer listeners + // (the dev invariants, persistence) observe the anchored events in strict + // log order. A turn with no prompt (an idle inject's one-shot injection + // turn) leaves the switch pending — it runs no step, so nothing executes + // or assembles under a stale value. + ctx.on('agent/prompt-submit', (agent, _content, _source, next) => { + const sessionId = bySession.get(agent) + const rec = sessionId === undefined ? undefined : sessions.get(sessionId) + if (rec !== undefined) flushPendingSwitches(rec) + return next() + }) + const makeAgent = (connection: AgentSideConnection): AcpAgent => { conn = connection return { @@ -591,27 +675,39 @@ export function apply(ctx: Context, config: AcpConfig): void { return Promise.resolve() }, - newSession(params: NewSessionRequest): Promise { + async newSession(params: NewSessionRequest): Promise { assertOpen() validateWorkspaceParams(params) validateMcpServers(params) const sessionId = SessionId(randomUUID()) - const handle = agents.create({ + const handle = await agents.create({ agentId: AgentId(sessionId), sessionId, meta: { cwd: params.cwd }, agentOptions: agentOptions(config), }) + // Creation is now asynchronous because it awaits the unpublished setup + // transaction. A client disconnect can therefore close this bridge + // after the entry check but before the handle resolves; never install a + // post-close record that quiesce() could not have seen. + /* v8 ignore next 4 -- the in-memory transport rejects the in-flight RPC + immediately on close; real stdio may let the handler resume */ + if (closed) { + await handle.dispose() + throw internalError('connection closed during session/new') + } bySession.set(handle.agent, sessionId) sessions.set(sessionId, { sessionId, agent: handle.agent, dispose: () => handle.dispose(), - presenter: makePresenter(), + presenter: makePresenter(handle.agent), terminalEnabled: terminalOutputCap, inflight: undefined, + pendingSwitches: {}, }) - return Promise.resolve({ sessionId }) + const configOptions = configOptionsFor(handle.agent) + return { sessionId, ...configOptions.length > 0 ? { configOptions } : {} } }, async loadSession(params: LoadSessionRequest): Promise { @@ -684,9 +780,10 @@ export function apply(ctx: Context, config: AcpConfig): void { sessionId, agent, dispose: () => handle.dispose(), - presenter: makePresenter(), + presenter: makePresenter(agent), terminalEnabled, inflight: undefined, + pendingSwitches: {}, } sessions.set(sessionId, record) // Replay the persisted event log to the client as session/update. Use @@ -702,7 +799,7 @@ export function apply(ctx: Context, config: AcpConfig): void { // future live events for this session. The throwaway pairs call→result // as the log replays in order (same as live) and is discarded after, // so the record's presenter starts clean for the post-load live stream. - const replayPresenter = makePresenter() + const replayPresenter = makePresenter(agent) const replayTerminal: TerminalRendering = { enabled: terminalEnabled, cwd: agent.session.header.cwd, @@ -710,7 +807,8 @@ export function apply(ctx: Context, config: AcpConfig): void { for (const event of agent.session.events) { streamSessionEventUpdate(sessionId, event, notify, replayPresenter, replayTerminal) } - return {} + const configOptions = configOptionsFor(agent) + return configOptions.length > 0 ? { configOptions } : {} } finally { loadingIds.delete(sessionId) } @@ -735,12 +833,10 @@ export function apply(ctx: Context, config: AcpConfig): void { // Install the in-flight slot BEFORE send() (send does not synchronously // flip status to running; the session/event listener records the turn // number and settle/rejects it). Capture the log length now as the - // watermark: the settle-from-log fallback infers the owning turn/start - // as the first one appended at/after it, surviving a starved live - // capture. A turn that ends in error rejects this promise (the codec - // never produces an error stop reason). + // A turn that ends in error rejects this promise (the codec never + // produces an error stop reason). const stopReason = await new Promise((resolve, reject) => { - rec.inflight = { resolve, reject, turn: undefined, logWatermark: rec.agent.session.events.length } + rec.inflight = { resolve, reject, turn: undefined } rec.agent.send([{ type: 'text', text }]) }) return { stopReason } @@ -759,12 +855,67 @@ export function apply(ctx: Context, config: AcpConfig): void { // as cancelled directly here: do NOT rely on the resulting turn/end to // settle it, because cancel() may drop the turn before any turn/end is // emitted, and removing this direct settle would move the RPC's - // resolution onto the settleFromLog/agent-status path, changing its - // timing. + // resolution onto a later observer path, changing its timing. rec.agent.cancel('session/cancel') settlePrompt(rec, 'cancelled') return Promise.resolve() }, + + setSessionConfigOption(params: SetSessionConfigOptionRequest): Promise { + assertOpen() + const rec = requireSession(SessionId(params.sessionId)) + // Both advertised options are selects, so the boolean-shaped variant of + // the request is a protocol misuse regardless of configId. + if (typeof params.value !== 'string') { + throw invalidParams(`config option ${params.configId} is a select; boolean values are not accepted`) + } + // The setters append ONE log-only event on this session's own log — + // the log is the store (the sandbox RFC § Per-session mode switching): execution, the + // prompt section, and the narrator all fold it from there, and a + // resumed session reports the override back through + // configOptionsFor. A switch while a turn is OPEN anchors + // immediately (the next step sees it); an IDLE switch waits in + // pendingSwitches for the next `turn/start` (turn-enclosure: a bare + // between-turns append would be dropped as crash tail on reload). + // Values are validated against the same closed lists the options + // advertised; an id this composition never advertised (or an unknown + // one) rejects. + switch (params.configId) { + case 'sandbox-mode': { + const defaultMode = ctx.get('bash')?.sandboxMode + if (defaultMode === undefined || !SANDBOX_MODES.includes(params.value as SandboxMode)) { + throw invalidParams(`unknown sandbox-mode value ${JSON.stringify(params.value)}`) + } + const value = params.value as SandboxMode + // A no-op switch (the value the session already shows — pending, + // else fold, else default) is acknowledged without recording + // anything: clients that re-push current selections on session + // start must not mint override events out of thin air. + const current = rec.pendingSwitches.sandboxMode ?? effectiveSandboxMode(rec.agent.session.events) ?? defaultMode + if (value === current) break + if (isTurnOpen(rec.agent)) setSandboxMode(rec.agent.session, value) + else rec.pendingSwitches.sandboxMode = value + break + } + case 'approval-policy': { + const approval = ctx.get('approval') + if (approval === undefined || !APPROVAL_POLICIES.includes(params.value as ApprovalPolicy)) { + throw invalidParams(`unknown approval-policy value ${JSON.stringify(params.value)}`) + } + const value = params.value as ApprovalPolicy + const current = rec.pendingSwitches.approvalPolicy ?? effectiveApprovalPolicy(rec.agent.session.events) ?? approval.config.policy ?? 'ask' + if (value === current) break + if (isTurnOpen(rec.agent)) setApprovalPolicy(rec.agent.session, value) + else rec.pendingSwitches.approvalPolicy = value + break + } + default: + throw invalidParams(`unknown config option ${JSON.stringify(params.configId)}`) + } + // The spec requires the COMPLETE refreshed config state in the response + // (a change may cascade); ours are independent, but the contract holds. + return Promise.resolve({ configOptions: configOptionsFor(rec.agent, rec.pendingSwitches) }) + }, } } @@ -787,15 +938,15 @@ export function apply(ctx: Context, config: AcpConfig): void { * quiescence"): for each session settle any pending prompt `cancelled`, then * run that session's {@link AgentHandle} `dispose()` — which stops the loop * (sets `disposed`, aborts the in-flight step), AWAITS the loop's exit (the - * final `turn/end` + `session/flush` are captured while `onAppend` is still + * final `turn/end` + `session/flush` are captured while the store-owned publication hooks are still * attached), unregisters the agent, and removes its session from the store. * The per-session disposes run in parallel. Idempotent — clears the `sessions` * map first and memoizes, so a second call (close racing dispose) is a no-op. * Shared by Cordis disposal AND client disconnect (`conn.closed`). * - * Per-agent disposal closes the former pre-step best-effort window — but via - * the DISPOSED path, not `cancel()`: the start-disposer resolves `handle.disposed`, - * which wakes the parked loop, and `isDisposed()` breaks the loop before a + * Per-agent disposal closes the queued-before-run window through the DISPOSED + * path, not `cancel()`: the start-disposer resolves `handle.disposed`, which + * wakes the parked loop, and `isDisposed()` breaks the loop before a * queued-but-not-yet-running turn can start (a turn cut off mid-flight ends * with reason `disposed`, not `aborted`). A bare client disconnect (resolves * `conn.closed` WITHOUT disposing the fiber) thus leaves NO registered agent @@ -1055,6 +1206,13 @@ export class ToolPresenter { constructor( private readonly tools: Pick, private readonly onError: (message: string) => void = () => {}, + /** + * The agent whose view resolves tool presentations: a scoped/shadowed + * tool presents with ITS OWN presentCall/presentResult — the same + * definition that executed — not a same-named global's. Absent (a replay + * with no live agent) the global view presents. + */ + private readonly agent?: Agent, ) {} /** @@ -1071,7 +1229,7 @@ export class ToolPresenter { const args = parseToolArguments(argsJson) let present: ToolCallView | undefined try { - present = this.tools.get(name)?.presentCall?.(args) + present = this.tools.get(name, this.agent)?.presentCall?.(args) } catch (error: unknown) { // A throwing presentCall must not break streaming: log and fall back. this.onError(`acp: tool "${name}" presentCall threw, using generic presentation: ${String(error)}`) @@ -1105,7 +1263,8 @@ export class ToolPresenter { if (call === undefined) return { card: 'generic', content } let present: ToolResultView | undefined try { - present = this.tools.get(call.name)?.presentResult?.(call.args, { content, isError, ...meta !== undefined ? { meta } : {} }) + present = this.tools.get(call.name, this.agent) + ?.presentResult?.(call.args, { content, isError, ...meta !== undefined ? { meta } : {} }) } catch (error: unknown) { // A throwing presentResult must not break streaming/replay: log + fall back. this.onError(`acp: tool "${call.name}" presentResult threw, using raw result: ${String(error)}`) diff --git a/packages/ui/acp/tests/approval.spec.ts b/packages/ui/acp/tests/approval.spec.ts new file mode 100644 index 0000000000..ed035aaf80 --- /dev/null +++ b/packages/ui/acp/tests/approval.spec.ts @@ -0,0 +1,108 @@ +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { mkdtemp, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { PROTOCOL_VERSION } from '@agentclientprotocol/sdk' +import { CallId } from '@deepseek-ai/dsh-llm' +import { AgentId, type Agent } from '@deepseek-ai/dsh-agent' +import ApprovalService, { type ApprovalRequest } from '@deepseek-ai/dsh-user-approval' +import { makeBridgeHarness, type BridgeHarness } from './harness.ts' + +/** + * The bridge's `approval/request` answerer: an ask for an agent the bridge + * owns becomes a `session/request_permission` prompt attached to the tool + * call; foreign or call-less requests delegate down to the fail-closed + * default. Driven through `ctx.approval` — the same path dsh-tools' ask + * routing takes — against the harness's scriptable client. + */ +describe('acp bridge — approval answerer', () => { + let storageDir: string + let harness: BridgeHarness | undefined + + beforeEach(async () => { storageDir = await mkdtemp(join(tmpdir(), 'acp-approval-')) }) + afterEach(async () => { + await harness?.dispose() + harness = undefined + await rm(storageDir, { recursive: true, force: true }) + }) + + async function ownedAgentRequest( + h: BridgeHarness, overrides: Partial = {}, + ): Promise<{ agent: Agent; request: ApprovalRequest }> { + await h.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} }) + const { sessionId } = await h.client.newSession({ cwd: process.cwd(), mcpServers: [] }) + const agent = h.ctx.agents.get(AgentId(sessionId)) + if (agent === undefined) throw new Error('newSession created no agent') + // In production an ask always fires mid-turn (tool execution); open one so + // request()'s turn-enclosure precondition holds for the direct drive below. + agent.session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) + return { agent, request: { agent, toolName: 'echo', callId: CallId('call-9'), ...overrides } } + } + + it('prompts the editor for an owned agent and maps allow-once → allowed-once', async () => { + harness = await makeBridgeHarness({ storageDir }) + await harness.ctx.plugin(ApprovalService) + harness.onPermission = () => ({ outcome: { outcome: 'selected', optionId: 'allow-once' } }) + + const { request } = await ownedAgentRequest(harness) + await expect(harness.ctx.approval.request(request)).resolves.toBe('allowed-once') + + expect(harness.permissionRequests).toHaveLength(1) + const wire = harness.permissionRequests[0] + expect(wire?.toolCall).toEqual({ toolCallId: 'call-9' }) + expect(wire?.options.map(o => ({ optionId: o.optionId, kind: o.kind }))).toEqual([ + { optionId: 'allow-once', kind: 'allow_once' }, + { optionId: 'reject-once', kind: 'reject_once' }, + ]) + }) + + it('maps reject-once → rejected', async () => { + harness = await makeBridgeHarness({ storageDir }) + await harness.ctx.plugin(ApprovalService) + harness.onPermission = () => ({ outcome: { outcome: 'selected', optionId: 'reject-once' } }) + + const { request } = await ownedAgentRequest(harness) + await expect(harness.ctx.approval.request(request)).resolves.toBe('rejected') + }) + + it('maps a client cancellation → cancelled', async () => { + harness = await makeBridgeHarness({ storageDir }) + await harness.ctx.plugin(ApprovalService) + harness.onPermission = () => ({ outcome: { outcome: 'cancelled' } }) + + const { request } = await ownedAgentRequest(harness) + await expect(harness.ctx.approval.request(request)).resolves.toBe('cancelled') + }) + + it('treats an unknown optionId from a non-conforming client as a rejection, never a grant', async () => { + harness = await makeBridgeHarness({ storageDir }) + await harness.ctx.plugin(ApprovalService) + harness.onPermission = () => ({ outcome: { outcome: 'selected', optionId: 'allow-always-i-insist' } }) + + const { request } = await ownedAgentRequest(harness) + await expect(harness.ctx.approval.request(request)).resolves.toBe('rejected') + }) + + it('delegates a foreign agent down to the fail-closed default', async () => { + harness = await makeBridgeHarness({ storageDir }) + await harness.ctx.plugin(ApprovalService) + harness.onPermission = () => ({ outcome: { outcome: 'selected', optionId: 'allow-once' } }) + + // Not created through the bridge: no bySession entry, so the answerer must + // call next() — nobody else answers, so the seam fails closed. + const foreign = { session: { events: [{ type: 'turn/start' }], append: () => ({}) } } as unknown as Agent + await expect(harness.ctx.approval.request({ agent: foreign, toolName: 'echo', callId: CallId('c') })) + .resolves.toBe('unavailable') + expect(harness.permissionRequests).toHaveLength(0) + }) + + it('delegates a call-less request — the protocol prompt must attach to a tool call', async () => { + harness = await makeBridgeHarness({ storageDir }) + await harness.ctx.plugin(ApprovalService) + harness.onPermission = () => ({ outcome: { outcome: 'selected', optionId: 'allow-once' } }) + + const { agent } = await ownedAgentRequest(harness) + await expect(harness.ctx.approval.request({ agent, toolName: 'echo' })).resolves.toBe('unavailable') + expect(harness.permissionRequests).toHaveLength(0) + }) +}) diff --git a/packages/ui/acp/tests/config-options.spec.ts b/packages/ui/acp/tests/config-options.spec.ts new file mode 100644 index 0000000000..1202e44bd3 --- /dev/null +++ b/packages/ui/acp/tests/config-options.spec.ts @@ -0,0 +1,260 @@ +/** + * Session config options over the bridge: the two per-session knobs + * (`sandbox-mode`, `approval-policy`) advertised from composition capability, + * their current values folded from each session's own log, switching via + * `session/set_config_option` (one log-only event per switch — the log is the + * store), and a resumed session reporting its overrides back on + * `session/load` with no catch-up machinery. + */ + +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { mkdtemp, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { PROTOCOL_VERSION } from '@agentclientprotocol/sdk' +import * as Invariants from '@deepseek-ai/dsh-invariants' +import ApprovalService from '@deepseek-ai/dsh-user-approval' +import type { ApprovalPolicy } from '@deepseek-ai/dsh-user-approval' +import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local' +import type { SandboxMode } from '@deepseek-ai/dsh-sandbox' +import { makeBridgeHarness, textResponse, type BridgeHarness } from './harness.ts' + +/** + * The REAL local executor reporting a confining default — `sandboxMode` is + * the documented capability override point (`dsh-bash-sandbox` overrides it + * the same way), so the bridge sees exactly what a sandboxing composition + * advertises without this suite dragging in a kernel sandbox stack. + */ +class SandboxedLocalExecutor extends LocalBashExecutor { + override get sandboxMode(): SandboxMode { + return 'read-only' + } +} + +/** The exact option payloads the bridge advertises (pinned verbatim). */ +function sandboxOption(currentValue: SandboxMode): object { + return { + id: 'sandbox-mode', + name: 'Sandbox', + description: 'The file sandbox mode bash commands in this session run under.', + category: 'mode', + type: 'select', + currentValue, + options: [ + { value: 'read-only', name: 'read-only' }, + { value: 'workspace-write', name: 'workspace-write' }, + { value: 'danger-full-access', name: 'danger-full-access' }, + ], + } +} + +function approvalOption(currentValue: ApprovalPolicy): object { + return { + id: 'approval-policy', + name: 'Approvals', + description: 'ask: permission prompts reach you; never: they are rejected automatically.', + type: 'select', + currentValue, + options: [ + { value: 'ask', name: 'ask' }, + { value: 'never', name: 'never' }, + ], + } +} + +describe('acp bridge — session config options', () => { + let storageDir: string + let h: BridgeHarness | undefined + let loader: BridgeHarness | undefined + + beforeEach(async () => { storageDir = await mkdtemp(join(tmpdir(), 'acp-config-')) }) + afterEach(async () => { + if (h) await h.dispose() + if (loader) await loader.dispose() + h = loader = undefined + await rm(storageDir, { recursive: true, force: true }) + }) + + /** A harness whose composition can honor both knobs (sandboxed executor + approval seam). */ + async function bothKnobs(options: { policy?: ApprovalPolicy; script?: NonNullable[0]>['script'] } = {}): Promise { + const harness = await makeBridgeHarness({ storageDir, ...options.script !== undefined ? { script: options.script } : {} }) + // The dev invariants police turn-enclosure: an idle switch that appended + // outside a turn would throw right here in the suite, not in production. + await harness.ctx.plugin(Invariants) + await harness.ctx.plugin(SandboxedLocalExecutor, { timeoutMs: 10_000 }) + await harness.ctx.plugin(ApprovalService, options.policy !== undefined ? { policy: options.policy } : {}) + await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} }) + return harness + } + + it('advertises no configOptions in a composition with neither knob', async () => { + h = await makeBridgeHarness({ storageDir }) + await h.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} }) + const res = await h.client.newSession({ cwd: process.cwd(), mcpServers: [] }) + expect(res.configOptions).toBeUndefined() + }) + + it('a non-confining executor advertises no sandbox option (nothing would honor it)', async () => { + h = await makeBridgeHarness({ storageDir, withBash: true }) + await h.ctx.plugin(ApprovalService) + await h.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} }) + const res = await h.client.newSession({ cwd: process.cwd(), mcpServers: [] }) + expect(res.configOptions).toEqual([approvalOption('ask')]) + }) + + it('advertises both knobs with capability-derived currents (config default included)', async () => { + h = await bothKnobs({ policy: 'never' }) + const res = await h.client.newSession({ cwd: process.cwd(), mcpServers: [] }) + expect(res.configOptions).toEqual([sandboxOption('read-only'), approvalOption('never')]) + }) + + it('an idle switch is pending (overlaid, not yet logged), then anchors INSIDE the next turn', async () => { + h = await bothKnobs({ script: [textResponse('ok')] }) + const { sessionId } = await h.client.newSession({ cwd: process.cwd(), mcpServers: [] }) + + const afterSandbox = await h.client.setSessionConfigOption({ sessionId, configId: 'sandbox-mode', value: 'workspace-write' }) + expect(afterSandbox.configOptions).toEqual([sandboxOption('workspace-write'), approvalOption('ask')]) + const afterApproval = await h.client.setSessionConfigOption({ sessionId, configId: 'approval-policy', value: 'never' }) + expect(afterApproval.configOptions).toEqual([sandboxOption('workspace-write'), approvalOption('never')]) + + // Idle: nothing in the log yet — turn-enclosure forbids a bare append + // (the dev invariants in this suite would throw), so the switch lives on + // the record until a turn opens. + const session = h.ctx.agents.list()[0]?.session + expect(session?.events.some(e => e.type === 'bash/sandbox-mode' || e.type === 'approval/policy')).toBe(false) + + // The next turn anchors both switches inside itself, one event per knob. + await h.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'anchor' }] }) + const events = session?.events ?? [] + expect(events.filter(e => e.type === 'bash/sandbox-mode').map(e => e.data)).toEqual([{ mode: 'workspace-write' }]) + expect(events.filter(e => e.type === 'approval/policy').map(e => e.data)).toEqual([{ policy: 'never' }]) + const turnStart = events.findIndex(e => e.type === 'turn/start') + const anchored = events.findIndex(e => e.type === 'bash/sandbox-mode') + expect(turnStart).toBeGreaterThanOrEqual(0) + expect(anchored).toBeGreaterThan(turnStart) + }) + + it('an idle flip-flop anchors as ONE event (last write per knob wins)', async () => { + h = await bothKnobs({ script: [textResponse('ok')] }) + const { sessionId } = await h.client.newSession({ cwd: process.cwd(), mcpServers: [] }) + await h.client.setSessionConfigOption({ sessionId, configId: 'sandbox-mode', value: 'workspace-write' }) + await h.client.setSessionConfigOption({ sessionId, configId: 'sandbox-mode', value: 'danger-full-access' }) + await h.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'anchor' }] }) + const events = h.ctx.agents.list()[0]?.session.events ?? [] + expect(events.filter(e => e.type === 'bash/sandbox-mode').map(e => e.data)).toEqual([{ mode: 'danger-full-access' }]) + // Idle again AFTER a completed turn (the log now ends in turn/end): a new + // switch pends rather than appending outside the closed turn. + const again = await h.client.setSessionConfigOption({ sessionId, configId: 'sandbox-mode', value: 'read-only' }) + expect(again.configOptions?.find(option => option.id === 'sandbox-mode')).toMatchObject({ currentValue: 'read-only' }) + expect(events.filter(e => e.type === 'bash/sandbox-mode')).toHaveLength(1) + }) + + it('a no-op switch (the value already shown) records nothing and keeps a live pending', async () => { + h = await bothKnobs({ script: [textResponse('ok')] }) + const { sessionId } = await h.client.newSession({ cwd: process.cwd(), mcpServers: [] }) + // Re-pushing the composition default (what clients that echo current + // selections on session start do) must not mint an override event. + const echo = await h.client.setSessionConfigOption({ sessionId, configId: 'approval-policy', value: 'ask' }) + expect(echo.configOptions?.find(option => option.id === 'approval-policy')).toMatchObject({ currentValue: 'ask' }) + // Re-sending a PENDING value keeps the pending switch alive (it is what + // the session shows), rather than cancelling it. + await h.client.setSessionConfigOption({ sessionId, configId: 'sandbox-mode', value: 'workspace-write' }) + const repeat = await h.client.setSessionConfigOption({ sessionId, configId: 'sandbox-mode', value: 'workspace-write' }) + expect(repeat.configOptions?.find(option => option.id === 'sandbox-mode')).toMatchObject({ currentValue: 'workspace-write' }) + await h.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'anchor' }] }) + const events = h.ctx.agents.list()[0]?.session.events ?? [] + expect(events.filter(e => e.type === 'approval/policy')).toHaveLength(0) + expect(events.filter(e => e.type === 'bash/sandbox-mode').map(e => e.data)).toEqual([{ mode: 'workspace-write' }]) + }) + + it('a net-zero idle flip-flop anchors NOTHING (switches are recorded, select clicks are not)', async () => { + h = await bothKnobs({ script: [textResponse('ok')] }) + const { sessionId } = await h.client.newSession({ cwd: process.cwd(), mcpServers: [] }) + await h.client.setSessionConfigOption({ sessionId, configId: 'sandbox-mode', value: 'workspace-write' }) + const back = await h.client.setSessionConfigOption({ sessionId, configId: 'sandbox-mode', value: 'read-only' }) + expect(back.configOptions?.find(option => option.id === 'sandbox-mode')).toMatchObject({ currentValue: 'read-only' }) + await h.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'anchor' }] }) + const events = h.ctx.agents.list()[0]?.session.events ?? [] + expect(events.filter(e => e.type === 'bash/sandbox-mode')).toHaveLength(0) + }) + + it('a mid-turn switch anchors immediately (the open turn encloses it)', async () => { + h = await bothKnobs({ script: ['hang'] }) + const { sessionId } = await h.client.newSession({ cwd: process.cwd(), mcpServers: [] }) + const hung = h.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'go' }] }) + // Give the loop a tick to open the turn (the turns.spec hang idiom). + await new Promise(resolve => setTimeout(resolve, 30)) + await h.client.setSessionConfigOption({ sessionId, configId: 'sandbox-mode', value: 'workspace-write' }) + await h.client.setSessionConfigOption({ sessionId, configId: 'approval-policy', value: 'never' }) + const events = h.ctx.agents.list()[0]?.session.events ?? [] + const turnStart = events.findIndex(e => e.type === 'turn/start') + const anchored = events.findIndex(e => e.type === 'bash/sandbox-mode') + expect(turnStart).toBeGreaterThanOrEqual(0) + expect(anchored).toBeGreaterThan(turnStart) + expect(events.some(e => e.type === 'approval/policy')).toBe(true) + await h.client.cancel({ sessionId }) + await hung + }) + + it('tolerates a provided approval stand-in whose config skipped the plugin schema', async () => { + h = await makeBridgeHarness({ storageDir, script: [textResponse('ok')] }) + h.ctx.provide('approval', { config: {} } as unknown as InstanceType) + await h.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} }) + const res = await h.client.newSession({ cwd: process.cwd(), mcpServers: [] }) + expect(res.configOptions).toEqual([approvalOption('ask')]) + const sessionId = res.sessionId + // The schema-less config also shields the no-op guard ('ask' by the ?? fallback)… + const echo = await h.client.setSessionConfigOption({ sessionId, configId: 'approval-policy', value: 'ask' }) + expect(echo.configOptions).toEqual([approvalOption('ask')]) + // …and the anchor-time comparison: a real switch under the stand-in still anchors. + await h.client.setSessionConfigOption({ sessionId, configId: 'approval-policy', value: 'never' }) + await h.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'anchor' }] }) + const events = h.ctx.agents.list()[0]?.session.events ?? [] + expect(events.filter(e => e.type === 'approval/policy').map(e => e.data)).toEqual([{ policy: 'never' }]) + }) + + it('rejects unknown ids, unadvertised ids, boolean values, and out-of-vocabulary values', async () => { + h = await makeBridgeHarness({ storageDir }) + await h.ctx.plugin(ApprovalService) + await h.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} }) + const { sessionId } = await h.client.newSession({ cwd: process.cwd(), mcpServers: [] }) + + await expect(h.client.setSessionConfigOption({ sessionId, configId: 'reasoning-effort', value: 'max' })) + .rejects.toThrow(/unknown config option/) + // sandbox-mode exists as a concept but THIS composition never advertised it. + await expect(h.client.setSessionConfigOption({ sessionId, configId: 'sandbox-mode', value: 'workspace-write' })) + .rejects.toThrow(/unknown sandbox-mode value/) + await expect(h.client.setSessionConfigOption({ sessionId, configId: 'approval-policy', type: 'boolean', value: true })) + .rejects.toThrow(/select; boolean values are not accepted/) + await expect(h.client.setSessionConfigOption({ sessionId, configId: 'approval-policy', value: 'always' })) + .rejects.toThrow(/unknown approval-policy value/) + }) + + it('a switch in one session never leaks into a concurrent one (state and pending both per-session)', async () => { + h = await bothKnobs() + const a = await h.client.newSession({ cwd: process.cwd(), mcpServers: [] }) + const b = await h.client.newSession({ cwd: process.cwd(), mcpServers: [] }) + await h.client.setSessionConfigOption({ sessionId: a.sessionId, configId: 'sandbox-mode', value: 'danger-full-access' }) + // B sees its own composition defaults, not A's pending switch... + const bAfter = await h.client.setSessionConfigOption({ sessionId: b.sessionId, configId: 'approval-policy', value: 'never' }) + expect(bAfter.configOptions).toEqual([sandboxOption('read-only'), approvalOption('never')]) + // ...and A keeps its own state, untouched by B's. + const aAfter = await h.client.setSessionConfigOption({ sessionId: a.sessionId, configId: 'sandbox-mode', value: 'danger-full-access' }) + expect(aAfter.configOptions).toEqual([sandboxOption('danger-full-access'), approvalOption('ask')]) + }) + + it('session/load reports a resumed session\'s overrides from its own log', async () => { + h = await bothKnobs({ script: [textResponse('ok')] }) + const { sessionId } = await h.client.newSession({ cwd: process.cwd(), mcpServers: [] }) + await h.client.setSessionConfigOption({ sessionId, configId: 'sandbox-mode', value: 'danger-full-access' }) + await h.client.setSessionConfigOption({ sessionId, configId: 'approval-policy', value: 'never' }) + // One turn checkpoints the log (the switch events flush with it). + await h.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'persist me' }] }) + await h.dispose() + h = undefined + + loader = await bothKnobs() + const res = await loader.client.loadSession({ sessionId, cwd: process.cwd(), mcpServers: [] }) + expect(res.configOptions).toEqual([sandboxOption('danger-full-access'), approvalOption('never')]) + }) +}) diff --git a/packages/ui/acp/tests/dispose.spec.ts b/packages/ui/acp/tests/dispose.spec.ts index ac092d9d16..f02a7b0649 100644 --- a/packages/ui/acp/tests/dispose.spec.ts +++ b/packages/ui/acp/tests/dispose.spec.ts @@ -159,8 +159,8 @@ describe('acp bridge — disposal & HMR safety', () => { it('the final turn closing events are persisted across an AgentHandle dispose (durability)', async () => { // The teardown-ORDER guarantee: a per-agent dispose must stop the loop, // AWAIT its exit (so the loop's final `turn/end` + `session/flush` fire - // through the still-attached `session.onAppend` → `session/event`), and only - // THEN detach onAppend + remove the session. If the order were inverted + // through the still-attached store observer → `session/event`), and only + // THEN remove its publication hooks and session entry. If the order were inverted // (detach first), the closing events would never reach persistence. Drive a // CLEAN turn to completion, dispose JUST the bridge, then re-load the // persisted log from disk and assert the closing turn/end is on disk — the @@ -190,7 +190,7 @@ describe('acp bridge — disposal & HMR safety', () => { // produced BY the dispose itself. Here the model stream HANGS, so the turn is // still open when teardown runs: the composite agent effect stops the loop, // the loop unwinds and appends `turn/end {disposed}` + runs its final - // `session/flush` — all while `onAppend` is still attached (the session + // `session/flush` — all while the store-owned publication hooks are still attached (the session // detach is the LAST disposer in the same effect's LIFO chain) — and only // THEN is the session detached. If the order were inverted (or the session // were a racing SIBLING effect), the abort-produced `turn/end` would never @@ -229,10 +229,10 @@ describe('acp bridge — disposal & HMR safety', () => { // dispose one handle, and assert the other survives, registered and // queryable, with its session still in the store. const harness = await makeBridgeHarness({ storageDir, script: [] }) - const handleA = harness.ctx.agents.create({ + const handleA = await harness.ctx.agents.create({ agentId: AgentId('sib-a'), sessionId: SessionId('sib-a'), agentOptions: { model: 'mock' }, }) - const handleB = harness.ctx.agents.create({ + const handleB = await harness.ctx.agents.create({ agentId: AgentId('sib-b'), sessionId: SessionId('sib-b'), agentOptions: { model: 'mock' }, }) expect(harness.ctx.agents.get(AgentId('sib-a'))).toBe(handleA.agent) @@ -255,13 +255,13 @@ describe('acp bridge — disposal & HMR safety', () => { // into ONE composite effect whose disposers run as a `.then()` chain. The // register disposer emits `agent/disposed`; if a listener throws and the // emit is UNCONTAINED, the rejected chain skips the LATER session-detach - // disposer — stranding the session in the store with `onAppend` attached (a + // disposer — stranding the session in the store with its publication hooks attached (a // leak AND a durability hole, since the new design relies on detach // running). The emit must be contained. Register a throwing listener, drive // a clean turn, dispose, and assert the session was STILL removed. const harness = await makeBridgeHarness({ storageDir, script: [textResponse('ok')] }) harness.ctx.on('agent/disposed', () => { throw new Error('boom disposed listener') }) - const handle = harness.ctx.agents.create({ + const handle = await harness.ctx.agents.create({ agentId: AgentId('guard-a'), sessionId: SessionId('guard-a'), agentOptions: { model: 'mock' }, }) handle.agent.send([{ type: 'text', text: 'go' }]) @@ -282,7 +282,7 @@ describe('acp bridge — disposal & HMR safety', () => { // first call's await agent.done + final flush finished. Every caller must // observe the same quiescence boundary. const harness = await makeBridgeHarness({ storageDir, script: ['hang'] }) - const handle = harness.ctx.agents.create({ + const handle = await harness.ctx.agents.create({ agentId: AgentId('conc-a'), sessionId: SessionId('conc-a'), agentOptions: { model: 'mock' }, }) // Drive a turn that hangs in the model stream, so the loop is mid-turn when diff --git a/packages/ui/acp/tests/edges.spec.ts b/packages/ui/acp/tests/edges.spec.ts index 69c935139d..e86fb9fc94 100644 --- a/packages/ui/acp/tests/edges.spec.ts +++ b/packages/ui/acp/tests/edges.spec.ts @@ -20,14 +20,14 @@ describe('acp bridge — demux & config edges', () => { it('ignores events from an agent the bridge does not own (strict id demux)', async () => { // A second agent created directly on the registry (NOT via the bridge) runs - // a turn. Its session/event + agent/status must NOT produce ACP updates and + // a turn. Its session events must NOT produce ACP updates and // must not settle anything — the bridge demuxes strictly by its own id. harness = await makeBridgeHarness({ storageDir, script: [textResponse('foreign')] }) await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} }) await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] }) const before = harness.updates.length - const { agent: foreign } = harness.ctx.agents.create({ agentId: AgentId('foreign'), sessionId: SessionId('foreign-session'), agentOptions: { model: 'mock' } }) + const { agent: foreign } = await harness.ctx.agents.create({ agentId: AgentId('foreign'), sessionId: SessionId('foreign-session'), agentOptions: { model: 'mock' } }) foreign.send([{ type: 'text', text: 'hi' }]) await foreign.whenIdle() await new Promise(r => setTimeout(r, 10)) diff --git a/packages/ui/acp/tests/turns.spec.ts b/packages/ui/acp/tests/turns.spec.ts index e182d5f2bf..f559ca36d9 100644 --- a/packages/ui/acp/tests/turns.spec.ts +++ b/packages/ui/acp/tests/turns.spec.ts @@ -235,12 +235,9 @@ describe('acp bridge — turn outcomes', () => { expect(failed).toHaveLength(1) }) - it('settles via the log fallback when a prior session/event listener throws (starvation)', async () => { - // A peer session/event listener that runs BEFORE the bridge's listener - // throws on turn/end (prepend: true puts it first). cordis emit stops at the - // throw, so the bridge's session/event listener never sees turn/end and - // cannot settle there. The agent/status idle-fallback must reconcile the - // prompt from the log so the RPC settles instead of hanging. + it('settles successfully when an earlier turn/end observer throws', async () => { + // Session contains each post-commit observer failure, so a prepended peer + // cannot starve the bridge's live turn/end delivery. harness = await makeBridgeHarness({ storageDir, script: [textResponse('answer')] }) harness.ctx.on('session/event', (_s, event) => { if (event.type === 'turn/end') throw new Error('peer listener boom') @@ -250,9 +247,7 @@ describe('acp bridge — turn outcomes', () => { expect(res.stopReason).toBe('end_turn') }) - it('log fallback REJECTS when the starved turn ended in error', async () => { - // Same starvation as above, but the turn fails: the idle-fallback must - // reject the RPC from the logged turn/end{error}, not resolve. + it('still rejects a failed turn when an earlier turn/end observer throws', async () => { harness = await makeBridgeHarness({ storageDir, script: [errorResponse('starved boom')] }) harness.ctx.on('session/event', (_s, event) => { if (event.type === 'turn/end') throw new Error('peer listener boom') @@ -262,21 +257,18 @@ describe('acp bridge — turn outcomes', () => { .rejects.toThrow(/turn failed: starved boom/) }) - it('log fallback infers the owning turn when turn/START capture is starved', async () => { - // A peer listener throws on turn/START (not turn/end): the bridge never + it('captures and settles the owning turn when an earlier turn-start observer throws', async () => { + // Turn correlation still reaches the bridge after the throwing peer and // captures inflight.turn via the live stream. A throwing turn/start listener - // also FAILS the turn (the throw is recorded as the turn's error). Without - // the watermark inference the fallback would resolve `cancelled` (the bug); - // with it, it infers the owning turn from the log and REJECTS from that - // turn's error turn/end. (The model's own error is never reached — the turn - // failed at start — so the rejection carries the listener's failure.) - harness = await makeBridgeHarness({ storageDir, script: [textResponse('never runs')] }) + // Session contains post-commit callbacks independently. + // The model request and normal turn outcome therefore still occur. + harness = await makeBridgeHarness({ storageDir, script: [textResponse('answer')] }) harness.ctx.on('session/event', (_s, event) => { if (event.type === 'turn/start') throw new Error('peer listener boom on start') }, { prepend: true }) const sessionId = await newSession(harness) - await expect(harness.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'go' }] })) - .rejects.toThrow(/turn failed:/) + const result = await harness.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'go' }] }) + expect(result.stopReason).toBe('end_turn') }) it('a between-turn injection does not settle the prompt early (message-trigger correlation)', async () => { diff --git a/packages/ui/acp/tsconfig.json b/packages/ui/acp/tsconfig.json index 9c2358c455..0c82438277 100644 --- a/packages/ui/acp/tsconfig.json +++ b/packages/ui/acp/tsconfig.json @@ -34,6 +34,15 @@ }, { "path": "../../session-persistence/session-persistence" + }, + { + "path": "../user-approval" + }, + { + "path": "../../sandbox/sandbox" + }, + { + "path": "../../bash/bash" } ] } diff --git a/packages/ui/stdio-agent/README.md b/packages/ui/stdio-agent/README.md index a299e2a94a..27f0d20be7 100644 --- a/packages/ui/stdio-agent/README.md +++ b/packages/ui/stdio-agent/README.md @@ -1,6 +1,6 @@ # @deepseek-ai/dsh-stdio-agent -The **terminal stdio chat app**: a Cordis app plugin that composes the providerless agent spine ([`@deepseek-ai/dsh-agent-core`](../../core/agent-core/README.md)) with the front-door cluster a terminal chat needs, and a `bin` that boots a leaf `cordis.yml`. +The **terminal stdio chat app**: a Cordis app plugin that composes the default agent spine ([`@deepseek-ai/dsh-agent-core`](../../core/agent-core/README.md)) with the front-door cluster a terminal chat needs, and a `bin` that boots a leaf `cordis.yml`. It is the readline counterpart to [`@deepseek-ai/dsh-acp-agent`](../acp-agent/README.md): both consume the same spine, but each bakes in the OPPOSITE front-door cluster. @@ -11,7 +11,7 @@ A terminal chat always wants the same cluster, so the package owns it rather tha | Plugin | Why it is here | |---|---| | `@cordisjs/plugin-logger-console` | the console logger — stdout is just the terminal here, so logging to it is correct (the ACP app must NOT have this) | -| `@deepseek-ai/dsh-agent-core` | the spine, pre-creating a `main` agent from this app's `model` and carrying its `persona` | +| `@deepseek-ai/dsh-agent-core` | the spine, pre-creating a `main` agent from this app's `model` with `process.cwd()` as the fresh session cwd and carrying its `persona` | | `@deepseek-ai/dsh-session-persistence-jsonl` | durable JSONL session log under `persistenceRoot` | | `@deepseek-ai/dsh-user-interaction` | the human question/answer seam used by confirmation tools | | `@deepseek-ai/dsh-tool-ask-user` | the model-facing `ask_user_question` tool | @@ -32,6 +32,8 @@ The leaf `cordis.yml` supplies only the **swappable backends** — an LLM adapte | `welcome` | `ready.` | the stdin-chat banner | | `resumeSessionId` | — | resume a persisted session id instead of starting fresh (sourced from an env var in the leaf) | +Fresh stdio sessions use the process launch directory as `session.header.cwd`, so project-scoped features such as skill discovery and default bash workdir follow the directory where `dsh-stdio-agent` was started. Resumed sessions keep the cwd stored in the persisted session header. + ## The bin `dsh-stdio-agent [path-to-cordis.yml]` (default `./cordis.yml`) loads a gitignored `.env` from the cwd (`DEEPSEEK_API_KEY` / `DEEPSEEK_BASE_URL`), then drives the cordis Loader against the config and awaits the whole plugin tree before returning. Run it under `node --expose-internals`: the cordis Loader resolves the config's bare plugin specifiers (`@deepseek-ai/dsh-*`, npm packages) through its internal module loader, which is only active under that flag. The `demo:echo` / `demo:repl` scripts invoke it that way. diff --git a/packages/ui/stdio-agent/src/index.ts b/packages/ui/stdio-agent/src/index.ts index 2617cbc87b..1037c28112 100644 --- a/packages/ui/stdio-agent/src/index.ts +++ b/packages/ui/stdio-agent/src/index.ts @@ -1,5 +1,5 @@ /** - * The stdio chat app: the providerless agent spine ({@link + * The stdio chat app: the default agent spine ({@link * @deepseek-ai/dsh-agent-core}) plus the coupled front-door cluster a terminal * chat needs — a console logger, the readline UI (the in-package `stdio-chat` * module), JSONL session @@ -58,7 +58,9 @@ export const name = 'stdio-agent' * {@link @deepseek-ai/dsh-agent-core}'s forwarded `agents` list); `persona` is * the deployment persona (forwarded to the system-prompt plugin); `toolOrder` * is the explicit model-facing tool order (forwarded to the system-prompt plugin); - * `persistenceRoot` is the JSONL backend's directory; `welcome` is the UI banner. + * fresh sessions use `process.cwd()` as their workspace cwd; resumed sessions + * keep their persisted cwd. `persistenceRoot` is the JSONL backend's directory; + * `welcome` is the UI banner. */ export interface Config { /** Model name for the `main` agent (must have a registered adapter). */ @@ -73,6 +75,8 @@ export interface Config { persistenceRoot?: string /** stdin-chat banner printed once on start. Defaults to `'ready.'`. */ welcome?: string + /** Skill registry, local-provider, and model-facing consumer config forwarded to agent-core. */ + skills?: agentCore.SkillConfig /** * If set, the `main` agent RESUMES this persisted session id instead of * starting fresh. Sourced from an env var in the leaf `cordis.yml` @@ -91,6 +95,7 @@ export const Config: z = z.object({ tools: ToolRegistry.Config, persistenceRoot: z.string().default('./.sessions'), welcome: z.string().default('ready.'), + skills: agentCore.SkillConfigSchema, resumeSessionId: z.string(), }) @@ -110,8 +115,10 @@ export function apply(ctx: Context, config: Config): void { agents: [{ id: AgentId('main'), model: config.model, + cwd: process.cwd(), ...config.resumeSessionId !== undefined ? { resumeSessionId: SessionId(config.resumeSessionId) } : {}, }], + ...config.skills !== undefined ? { skills: config.skills } : {}, }) ctx.plugin(SessionPersistenceJsonl, { root: config.persistenceRoot ?? './.sessions' }) ctx.plugin(UserInteractionService) diff --git a/packages/ui/stdio-agent/tests/built-bin.e2e.ts b/packages/ui/stdio-agent/tests/built-bin.e2e.ts index 7b84fad65b..31cdf3eb08 100644 --- a/packages/ui/stdio-agent/tests/built-bin.e2e.ts +++ b/packages/ui/stdio-agent/tests/built-bin.e2e.ts @@ -92,7 +92,7 @@ async function makeConsumer(welcome: string, disabledBrokenEntry = false): Promi ' name: \'@deepseek-ai/dsh-stdio-agent\'', ' config:', ' model: mock-echo', - ' systemPrompt: \'demo\'', + ' persona: \'demo\'', ` welcome: '${welcome}'`, ...disabledBrokenEntry ? ['- id: off', ' name: \'./src/does-not-exist.ts\'', ' disabled: true'] @@ -111,7 +111,7 @@ function runBuiltBin(cwd: string, configArg: string, line: string): Promise<{ st const child = spawn(process.execPath, ['--expose-internals', stdioBin, configArg], { cwd, // Mock model: never calls the network, so no key needed. - env: { ...process.env }, + env: { ...process.env, DSH_HOME: join(cwd, '.dsh'), DSH_AGENTS_HOME: join(cwd, '.agents') }, stdio: ['pipe', 'pipe', 'pipe'], }) let stdout = '' diff --git a/packages/ui/stdio-agent/tests/stdio-agent.spec.ts b/packages/ui/stdio-agent/tests/stdio-agent.spec.ts index 1f5a01a5b6..0668d25fb0 100644 --- a/packages/ui/stdio-agent/tests/stdio-agent.spec.ts +++ b/packages/ui/stdio-agent/tests/stdio-agent.spec.ts @@ -1,7 +1,11 @@ import { describe, it, expect } from 'vitest' +import { mkdtemp } from 'node:fs/promises' +import { join } from 'node:path' +import { tmpdir } from 'node:os' import { Context } from 'cordis' import Loader from '@cordisjs/plugin-loader' -import { AgentId } from '@deepseek-ai/dsh-agent' +import { AgentId, agentEvents, type Agent } from '@deepseek-ai/dsh-agent' +import type { Message } from '@deepseek-ai/dsh-llm' import { TOOL_ORDER_REST } from '@deepseek-ai/dsh-system-prompt' import * as stdioAgent from '../src/index.ts' @@ -30,9 +34,48 @@ async function mount(config: stdioAgent.Config): Promise { return ctx } +async function isolatedSkillsConfig(catalogDescriptionMaxLength?: number): Promise> { + const home = await mkdtemp(join(tmpdir(), 'dsh-stdio-agent-skills-')) + return { + local: { dshHome: join(home, '.dsh'), agentsHome: join(home, '.agents') }, + ...catalogDescriptionMaxLength !== undefined ? { tool: { catalogDescriptionMaxLength } } : {}, + } +} + +async function composePrefix(ctx: Context): Promise { + const agent = { session: { header: { cwd: '/tmp' } } } as unknown as Agent + const empty: Message[] = [] + return await agentEvents(ctx, agent).waterfall( + 'agent/session-prefix', empty, new AbortController().signal, + () => Promise.resolve(empty), + ) +} + +async function withIsolatedSkillHomes(run: () => Promise): Promise { + const oldDshHome = process.env.DSH_HOME + const oldAgentsHome = process.env.DSH_AGENTS_HOME + const home = await mkdtemp(join(tmpdir(), 'dsh-stdio-agent-default-skills-')) + process.env.DSH_HOME = join(home, '.dsh') + process.env.DSH_AGENTS_HOME = join(home, '.agents') + try { + return await run() + } finally { + if (oldDshHome === undefined) { + delete process.env.DSH_HOME + } else { + process.env.DSH_HOME = oldDshHome + } + if (oldAgentsHome === undefined) { + delete process.env.DSH_AGENTS_HOME + } else { + process.env.DSH_AGENTS_HOME = oldAgentsHome + } + } +} + describe('dsh-stdio-agent app', () => { it('composes the spine + front-door cluster and pre-creates the main agent', async () => { - const ctx = await mount({ model: 'mock', persona: 'hi', persistenceRoot: '/tmp/dsh-stdio-agent-spec' }) + const ctx = await mount({ model: 'mock', persona: 'hi', persistenceRoot: '/tmp/dsh-stdio-agent-spec', skills: await isolatedSkillsConfig() }) // The spine services (brought up by the agent-core bundle) are all present. expect(ctx.get('agents')).toBeDefined() expect(ctx.get('agentLoop')).toBeDefined() @@ -40,7 +83,9 @@ describe('dsh-stdio-agent app', () => { expect(ctx.get('userInteraction')).toBeDefined() expect(ctx.get('tools')?.get('ask_user_question')).toBeDefined() // The pre-created `main` agent the UI drives. - expect(ctx.get('agents')?.get(AgentId('main'))).toBeDefined() + const agent = ctx.get('agents')?.get(AgentId('main')) + expect(agent).toBeDefined() + expect(agent?.session.header.cwd).toBe(process.cwd()) await ctx.fiber.dispose() }) @@ -51,13 +96,24 @@ describe('dsh-stdio-agent app', () => { // schema-bypassing direct-mount caller. const ctx = new Context() // No persona: covers the omitted-persona forwarding branch too. - stdioAgent.apply(ctx, { model: 'mock' }) + stdioAgent.apply(ctx, { model: 'mock', skills: await isolatedSkillsConfig() }) await new Promise(resolve => setTimeout(resolve, 80)) expect(ctx.get('sessionPersistence')).toBeDefined() expect(ctx.get('agents')?.get(AgentId('main'))).toBeDefined() await ctx.fiber.dispose() }) + it('uses default skill config when apply is called directly without skills', async () => { + await withIsolatedSkillHomes(async () => { + const ctx = new Context() + stdioAgent.apply(ctx, { model: 'mock' }) + await new Promise(resolve => setTimeout(resolve, 80)) + expect(ctx.skills).toBeDefined() + expect(await ctx.skills.list()).toEqual([]) + await ctx.fiber.dispose() + }) + }) + it('forwards resumeSessionId onto the pre-created agent when set', async () => { // A resume id defers agent creation until persistence loads; with no backing // session the resume is contained + logged, so no `main` agent registers — @@ -67,11 +123,19 @@ describe('dsh-stdio-agent app', () => { persona: 'hi', persistenceRoot: '/tmp/dsh-stdio-agent-spec-resume', resumeSessionId: 'no-such-session', + skills: await isolatedSkillsConfig(), }) expect(ctx.get('agents')?.get(AgentId('main'))).toBeUndefined() await ctx.fiber.dispose() }) + it('forwards skill config into agent-core', async () => { + const ctx = await mount({ model: 'mock', persona: 'hi', skills: await isolatedSkillsConfig(6) }) + ctx.skills.register({ name: 'stdio-skill', description: 'Stdio skill', source: 'runtime', content: 'body' }) + expect(JSON.stringify(await composePrefix(ctx))).toContain('- `stdio-skill`: Std...') + await ctx.fiber.dispose() + }) + it('exposes its name and Config schema', () => { expect(stdioAgent.name).toBe('stdio-agent') expect(stdioAgent.Config).toBeDefined() @@ -94,7 +158,7 @@ describe('dsh-stdio-agent app', () => { }) } const assembly = await ctx.get('systemPrompt')!.assemble() - expect(assembly.tools.map(tool => tool.name)).toEqual(['zulu', 'alpha', 'ask_user_question']) + expect(assembly.tools.map(tool => tool.name)).toEqual(['zulu', 'alpha', 'ask_user_question', 'skill']) await ctx.fiber.dispose() }) diff --git a/packages/ui/user-approval/README.md b/packages/ui/user-approval/README.md new file mode 100644 index 0000000000..a50bffad0b --- /dev/null +++ b/packages/ui/user-approval/README.md @@ -0,0 +1,13 @@ +# @deepseek-ai/dsh-user-approval + +User-approval seam. Owns the `ctx.approval` service ([`ApprovalService`](src/index.ts)) and the one-shot permission vocabulary the harness shares: `ApprovalRequest` (agent + tool identity + reason + abort signal), the closed `ApprovalOutcome` union (`allowed-once` / `rejected` / `cancelled` / `unavailable`), the `ApprovalRequestId` brand pairing the two log-only audit events (`approval/asked` / `approval/decided`), and the `approval/request` waterfall the answerers listen on. It lives in the UI group because its purpose is human permission, while remaining channel-neutral: it depends only on Cordis and core vocabulary packages, never on a concrete UI. + +The contract in one line: `ctx.approval.request(req)` puts exactly one question — "may this specific action proceed?" — to whatever answerers the deployment composed, and its answerer phase always produces an outcome: an aborted signal yields `cancelled`, a throwing or missing answerer yields `unavailable`, and `allowed-once` is a grant for the single asked-about action, never a class of future ones. `ApprovalRequest` is a readonly same-process contract: the service borrows the exact request, agent, session, and abort signal rather than cloning or freezing them. The request requires an open turn because the audit pair is turn-enclosed by contract (the turn is the durable log's commit/replay boundary; a bare event between turns is crash-tail garbage on reload), so an idle ask rejects before appending. Either audit append may reject before commit because returning an unlogged decision would violate the pair. Session contains post-commit observer failures, so an authoritative audit append cannot reject the request or suppress its matching event. + +The service is the mechanism, answerers are the policy. Answerers are `approval/request` waterfall listeners occupying a single decision slot: answer for an agent you own by returning an outcome without calling `next()`, or delegate an agent you don't recognize by calling `next()` — the chain's built-in default is `unavailable`, so a deployment with no answerer (headless, CI) fails closed with zero configuration. Dispatch is keyed by `req.agent`: a listener registered through `agent.ctx` receives only that agent's questions, while a plain-context listener receives every agent's. Registration order across sibling plugins is not load-order deterministic; compose one terminal answerer per deployment and use `prepend` listeners only for decide-or-delegate gates. + +The seam also owns the per-session POLICY tier ([the sandbox RFC § Per-session mode switching](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md)): `ApprovalPolicy` is `'ask'` (delegate to the answerers) or `'never'` (deterministically reject without prompting anyone; the strict CI/unattended stance), with `effective = fold(the session's 'approval/policy' events, last one wins) ?? Config.policy` — the session log is the store, written only through `setApprovalPolicy(session, policy)`, which rejects any value outside that closed vocabulary before appending. The service decides `'never'` inside `request()` itself, before dispatching the waterfall (`'never'` → `'rejected'` with the audit pair still landing; no listener registration, including a later `prepend`, can precede it), states `'never'` — and only `'never'` in prose — in a per-agent prompt section, records either value with a source-owned header marker, and narrates a policy switch to the model in at most one coalesced `agent/pre-step` notice. The restart fallback reads the marker rather than deployment-controlled persona prose; attribution is positional (an override event after the last `request/header*` reads `changed by the user`, otherwise `changed by the operator/config`). + +One seam serves both ask paths of [the sandbox RFC](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md): the `tools/pre-execute` `ask` decision (routed by [`@deepseek-ai/dsh-tools`](../../core/tools/) when this service is mounted; degrading to deny when it is not), and the sandbox post-denial escalated retry (the bash tool's `sandbox_permissions` gate in [`@deepseek-ai/dsh-tool-bash`](../../bash/tool-bash/) — [the sandbox RFC § Escalation](../../../docs/rfc/implemented/feature/2026-07-06-sandbox.md)). The full design: [the approval-seam RFC](../../../docs/rfc/implemented/feature/2026-07-06-approval-seam.md). + +Answerers today: the ACP bridge ([`@deepseek-ai/dsh-acp`](../../ui/acp/)) forwards to the editor's `session/request_permission` prompt for agents it owns. The audit events are log-only session records — the model only ever sees the tool result the asker derives from the outcome. diff --git a/packages/ui/user-approval/package.json b/packages/ui/user-approval/package.json new file mode 100644 index 0000000000..602fee53af --- /dev/null +++ b/packages/ui/user-approval/package.json @@ -0,0 +1,45 @@ +{ + "name": "@deepseek-ai/dsh-user-approval", + "description": "User-approval seam (ctx.approval) for the DeepSeek Harness: one-shot permission decisions dispatched to composed answerers over the approval/request waterfall, fail-closed by default", + "version": "0.0.1", + "private": true, + "type": "module", + "main": "lib/index.js", + "types": "lib/types/index.d.ts", + "exports": { + ".": { + "types": "./lib/types/index.d.ts", + "default": "./lib/index.js" + }, + "./src/*": "./src/*", + "./package.json": "./package.json" + }, + "files": [ + "lib/index.js", + "lib/types/**/*.d.ts", + "lib/types/**/*.d.ts.map", + "src" + ], + "license": "BSD-3-Clause", + "peerDependencies": { + "@deepseek-ai/dsh-agent": "^0.0.1", + "@deepseek-ai/dsh-brand": "^0.0.1", + "@deepseek-ai/dsh-llm": "^0.0.1", + "@deepseek-ai/dsh-scope": "^0.0.1", + "@deepseek-ai/dsh-session": "^0.0.1", + "@deepseek-ai/dsh-system-prompt": "^0.0.1", + "cordis": "^4.0.0-rc.6" + }, + "dependencies": { + "schemastery": "^3.18.0" + }, + "devDependencies": { + "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-brand": "workspace:^", + "@deepseek-ai/dsh-llm": "workspace:^", + "@deepseek-ai/dsh-scope": "workspace:^", + "@deepseek-ai/dsh-session": "workspace:^", + "@deepseek-ai/dsh-system-prompt": "workspace:^", + "cordis": "^4.0.0-rc.6" + } +} diff --git a/packages/ui/user-approval/src/index.ts b/packages/ui/user-approval/src/index.ts new file mode 100644 index 0000000000..6b3ef962bb --- /dev/null +++ b/packages/ui/user-approval/src/index.ts @@ -0,0 +1,464 @@ +/** + * Approval seam: `ctx.approval` answers exactly one question — "may this + * specific action proceed?" — by dispatching the `approval/request` waterfall + * to whatever answerers the deployment composed (an ACP editor prompt, an + * auto-decide policy, a scripted test listener) and returning a closed + * {@link ApprovalOutcome}. With no answerer the waterfall falls through to the + * built-in default `'unavailable'`: absence of a UI can never grant anything. + * + * The service is the MECHANISM (dispatch, cancellation, audit); answerers are + * the POLICY. It serves both ask paths the sandbox RFC names — the + * `tools/pre-execute` `ask` decision and the sandbox post-denial escalation — + * so every asker shares one outcome + * vocabulary and one audit trail. Grants are one-shot by design: an + * `'allowed-once'` outcome authorizes the single action it was asked about, + * never a class of future actions. + * + * Every request lands two log-only session events on the requesting agent's + * log (`approval/asked` / `approval/decided`, paired by + * {@link ApprovalRequestId}) — an audit trail, deliberately NOT part of the + * model-visible transcript: the model only ever sees the tool result the + * caller derives from the outcome. + * + * The seam also owns the per-session POLICY tier (the sandbox RFC § Per-session mode switching): + * `effective = fold(the session's 'approval/policy' events, last one wins) + * ?? config.policy` — the session log is the store, so an override survives + * restart by replay. The service resolves `'never'` sessions to + * `'rejected'` inside `request()` before dispatching any answerer (no + * registration order, including a later `prepend`, can precede it); a prompt section states `'never'` + * (and only `'never'` — an availability promise is unknowable without + * asking); an `agent/pre-step` narrator explains a switch to the model in at + * most one coalesced notice per step. + * + * @module @deepseek-ai/dsh-user-approval + */ + +import { randomUUID } from 'node:crypto' +import { Context, Service } from 'cordis' +import z from 'schemastery' +import type { Branded } from '@deepseek-ai/dsh-brand' +import type { Agent } from '@deepseek-ai/dsh-agent' +import type { CallId } from '@deepseek-ai/dsh-llm' +import { scopeTarget } from '@deepseek-ai/dsh-scope' +import type { Scoped } from '@deepseek-ai/dsh-scope' +import type { Session, SessionEvent } from '@deepseek-ai/dsh-session' +import type {} from '@deepseek-ai/dsh-system-prompt' + +declare module 'cordis' { + interface Context { + approval: ApprovalService + } + + interface Events { + /** + * Waterfall asking the composed answerers to decide one approval request. + * Dispatched only from {@link ApprovalService.request} — callers go through + * the service (which owns cancellation and the audit events), never through + * `ctx.waterfall` directly. A listener that can answer for this request's + * agent returns an outcome WITHOUT calling `next()` (the decision slot is + * single-occupancy, first listener to answer wins); a listener that does + * not recognize the agent MUST call `next()` so another answerer — or the + * fail-closed default `'unavailable'` — gets the question. Throwing is + * contained by the service and yields `'unavailable'`. + * Scope-filtered dispatch (`@deepseek-ai/dsh-scope`) keys the carrier by `req.agent`: a + * listener registered through `agent.ctx` receives only that agent's + * questions, while a plain-context listener receives every agent's. + * `req` is a readonly same-process value borrowed from the caller. + * @param req - the pending decision (agent, tool identity, reason, signal). + * @mode waterfall + */ + 'approval/request'(this: Scoped, req: ApprovalRequest, next: () => Promise): Promise + } +} + +declare module '@deepseek-ai/dsh-session' { + interface SessionEventMap { + /** + * An approval question was put to the answerer chain — log-only audit + * (like `hook/*`; NOT a surface event, carries no `surfaceOp`). `id` pairs + * it with the `approval/decided` that always follows; `toolName` is the + * tool the question is about, `callId` the exact tool call when the asker + * had one, `reason` the asker's human-readable explanation (e.g. a hook's + * permission-decision reason). + */ + 'approval/asked': { + id: ApprovalRequestId + toolName: string + callId?: CallId + reason?: string + } + /** + * The outcome of a prior `approval/asked` (same `id`) — log-only audit. + * Exactly one per ask, appended when the outcome is known: a decision, a + * cancellation, or the fail-closed `'unavailable'`. + */ + 'approval/decided': { + id: ApprovalRequestId + outcome: ApprovalOutcome + } + /** + * The session's approval policy was switched — log-only, durable, + * replayable, never in the model transcript (the model learns the policy + * from the prompt section and the narrator's notices). The LAST such + * event is the session's override ({@link effectiveApprovalPolicy}); + * who asked for it is derivable from position (an event after the log's + * last `request/header*` was a runtime switch by the user). + */ + 'approval/policy': { policy: ApprovalPolicy } + } +} + +/** + * Pairs one `approval/asked` audit event with its `approval/decided`. + * Service-issued (one fresh id per {@link ApprovalService.request} call). + */ +export type ApprovalRequestId = Branded<'ApprovalRequestId'> + +/** + * Brand a string as an {@link ApprovalRequestId}. + * @param id - the raw id string to brand. + * @returns the same string carrying the brand. + */ +export function ApprovalRequestId(id: string): ApprovalRequestId { + return id as ApprovalRequestId +} + +/** + * The closed outcome vocabulary of one approval request. + * + * - `'allowed-once'` — a one-shot grant for exactly the asked-about action; + * consumed by proceeding, never a durable authorization. + * - `'rejected'` — an answerer (human or policy) said no. + * - `'cancelled'` — the question was withdrawn: the prompt was dismissed, or + * the requesting execution aborted while the question was pending. + * - `'unavailable'` — nobody composed could answer (no listener, none that + * recognizes the agent, or an answerer failed). Callers MUST fail closed on + * it, exactly like `'rejected'` — the two differ only for audit and wording. + */ +export type ApprovalOutcome = 'allowed-once' | 'rejected' | 'cancelled' | 'unavailable' + +/** Every {@link ApprovalOutcome}, for runtime normalization of answerer returns. */ +const OUTCOMES: readonly ApprovalOutcome[] = ['allowed-once', 'rejected', 'cancelled', 'unavailable'] + +/** + * A session's approval policy — what happens to an {@link ApprovalService} + * ask BEFORE any interactive answerer sees it: + * + * - `'ask'` (the default) — delegate to the composed answerers; with none + * composed the chain falls through to the fail-closed `'unavailable'` + * (exactly today's behavior). + * - `'never'` — never prompt anyone: every ask resolves `'rejected'` + * deterministically. The strict headless stance (CI, unattended runs) and + * the only policy value stated in the system prompt — unlike `'ask'`, its + * outcome is knowable without asking, so stating it cannot overclaim. + */ +export type ApprovalPolicy = 'ask' | 'never' + +/** Every {@link ApprovalPolicy}, for option advertisement and runtime validation of untrusted policy strings. */ +export const APPROVAL_POLICIES: readonly ApprovalPolicy[] = ['ask', 'never'] + +/** + * The prompt sentence stating a `'never'` policy — visibility for the one + * deterministic policy (see {@link ApprovalPolicy}). Narrator persistence + * does NOT parse this prose: deployments can quote it in a persona or another + * section, so the section also emits a source-owned marker. + */ +const NEVER_SENTENCE = 'Approval prompts are disabled in this session: actions that require approval are rejected automatically — do not request sandbox escalation (do not set `sandbox_permissions`).' + +/** Source-owned prompt markers used to reconstruct the policy in a logged header. */ +const POLICY_MARKERS = { + ask: '', + never: '', +} as const satisfies Record + +/** + * Read the policy fact emitted by this service from a logged system prompt. + * The section is ordered after deployment persona text, and the last marker + * wins so a persona quoting an earlier marker cannot shadow the service's own + * contribution. Ordinary policy prose is deliberately ignored. + */ +function toldApprovalPolicy(system: string | undefined): ApprovalPolicy | undefined { + if (system === undefined) return undefined + const ask = system.lastIndexOf(POLICY_MARKERS.ask) + const never = system.lastIndexOf(POLICY_MARKERS.never) + if (ask < 0 && never < 0) return undefined + return never > ask ? 'never' : 'ask' +} + +/** + * The session's approval-policy override: the last `approval/policy` event in + * the log, or undefined when the session never switched (callers apply the + * plugin's configured default). The pure fold — resume needs no catch-up + * machinery because replaying the log IS the state. + * @param events - session events in log order (other event types are skipped). + * @returns the policy of the last switch event, or undefined without one. + */ +export function effectiveApprovalPolicy(events: readonly SessionEvent[]): ApprovalPolicy | undefined { + for (let index = events.length - 1; index >= 0; index -= 1) { + const event = events[index] as SessionEvent + if (event.type === 'approval/policy') return event.data.policy + } + return undefined +} + +/** + * Whether the log currently sits inside an open turn (a `turn/start` not yet + * closed by a `turn/end`) — the {@link ApprovalService.request} precondition. + * The audit pair must be turn-enclosed: the turn is the durable log's + * commit/replay boundary, so a bare event appended between turns is + * indistinguishable from a crash tail and silently dropped on reload. + */ +function hasOpenTurn(events: readonly SessionEvent[]): boolean { + for (let index = events.length - 1; index >= 0; index -= 1) { + const type = (events[index] as SessionEvent).type + if (type === 'turn/start') return true + if (type === 'turn/end') return false + } + return false +} + +/** + * THE write path for a session's approval-policy override: appends exactly + * one `approval/policy` event — the switch IS its event; nothing mutates + * policy state out of band. Takes effect on the session's next ask and next + * prompt assembly (the consumers fold on every read). Rejects a value outside + * {@link APPROVAL_POLICIES} before appending anything. + * @param session - the session the override belongs to. + * @param policy - the policy every subsequent ask for this session resolves + * under (until the next switch). + */ +export function setApprovalPolicy(session: Session, policy: ApprovalPolicy): void { + if (!APPROVAL_POLICIES.includes(policy)) { + throw new TypeError('approval policy must be one of "ask" or "never"') + } + session.append('approval/policy', { policy }) +} + +/** + * One concrete permission question. Identifies the action precisely enough + * for an answerer to present it and for the audit events to reconstruct what + * was asked — it deliberately does NOT carry tool arguments: a UI answerer + * attaches the prompt to the already-streamed tool call via `callId` instead + * of re-rendering the call. This is a readonly same-process contract: + * `request()` borrows the request and its `agent` and `signal` capabilities + * directly rather than treating them as serialized input. + */ +export interface ApprovalRequest { + /** + * The agent on whose behalf the question is asked. Routes the question (a + * UI answerer only answers for agents it owns) and receives the audit + * events on its session log. + */ + readonly agent: Agent + /** The tool the question is about (presentation and audit). */ + readonly toolName: string + /** + * The exact tool call being decided, when the asker has one — lets a UI + * attach the prompt to the tool call it already streamed. + */ + readonly callId?: CallId + /** The asker's human-readable explanation of WHY it is asking. */ + readonly reason?: string + /** + * Aborting withdraws the question: the request settles `'cancelled'` + * immediately and a late answer from a still-pending answerer is discarded. + */ + readonly signal?: AbortSignal +} + +/** Plugin config. All optional — `static Config` supplies the defaults. */ +export interface Config { + /** + * The deployment's default {@link ApprovalPolicy} for sessions without an + * `approval/policy` override — `'ask'` delegates to the composed answerers + * (fail-closed with none); `'never'` auto-rejects every ask without + * prompting (the deterministic CI/unattended stance). + */ + readonly policy?: ApprovalPolicy +} + +/** + * The `ctx.approval` service: dispatches {@link ApprovalRequest}s to the + * `approval/request` waterfall and audits every ask/outcome pair to the + * requesting agent's session log. Stateless between requests — grants are + * returned to the caller, never stored here. + * + * Owns the policy tier too (`effective = fold(the session's 'approval/policy' + * events) ?? config.policy`): `request()` resolves `'never'` to `'rejected'` + * before dispatching any interactive answerer, a per-agent prompt section + * states a `'never'` policy (and only that one in prose — an `'ask'` promise + * could overclaim an answerer that headless compositions do not have), and an + * `agent/pre-step` narrator injects at most one coalesced notice when a + * session's effective policy moved past what the model was last told. + */ +export class ApprovalService extends Service { + static Config: z = z.object({ + policy: z.union(['ask', 'never'] as const).default('ask'), + }) + + constructor(ctx: Context, public config: Config) { + super(ctx, 'approval') + + const effective = (agent: Agent): ApprovalPolicy => this.effectivePolicy(agent.session) + + // Visibility layer 1, scoped on the prompt registry so headless + // compositions mount the seam without it: state the one deterministic + // policy per session. 'ask' renders only a source-owned state marker — + // stating "you will be asked" would overclaim in a composition with no + // answerer. The marker, not deployment-controlled prose, is what the + // restart narrator reads back from the logged request header. + ctx.inject(['systemPrompt'], (scope: Context) => { + scope.systemPrompt.section({ + name: 'approval:policy', + order: 115, + text: (context) => { + const agent = context.agent + // A bare assemble() (tests, diagnostics) has no session to state. + if (agent === undefined) return '' + const policy = effective(agent) + return policy === 'never' ? `${NEVER_SENTENCE}\n${POLICY_MARKERS.never}` : POLICY_MARKERS.ask + }, + }) + }) + + // Visibility layer 2: the boundary narrator. pre-step runs after prompt + // assembly but before the request history is derived, so the notice is + // seen by THIS step's request: idle-time flip-flops coalesce at the + // turn's first step (net-zero → nothing), and a mid-turn switch is + // narrated no later than the next step. What each session was last told + // is in-memory with a log-derived fallback (the folded header's system + // text), so restarts lose nothing. Attribution is positional: an + // override event after the log's last `request/header*` was a runtime + // switch by the user; otherwise the configured default moved under the + // session (operator/config). + const narrated = new WeakMap() + ctx.on('agent/pre-step', (agent) => { + const session = agent.session + const events = session.events + let overrideIndex = -1 + let headerIndex = -1 + for (let index = events.length - 1; index >= 0 && (overrideIndex < 0 || headerIndex < 0); index -= 1) { + const event = events[index] as (typeof events)[number] + if (overrideIndex < 0 && event.type === 'approval/policy') { + overrideIndex = index + } else if (headerIndex < 0 && (event.type === 'request/header' || event.type === 'request/header-delta')) { + headerIndex = index + } + } + // Same fold effectivePolicy performs — override is scanned here anyway + // for POSITIONAL attribution; the default lives once, in the method. + const current = this.effectivePolicy(session) + const header = session.requestHeader() + const told = narrated.get(session) ?? toldApprovalPolicy(header?.system) + narrated.set(session, current) + // Cold start (nothing ever told) narrates nothing — the section about + // to go out states the truth, and there is no delta to explain. + if (told === undefined || told === current) return + const cause = overrideIndex > headerIndex ? 'changed by the user' : 'changed by the operator/config' + agent.inject( + [{ type: 'text', text: `The approval policy changed from "${told}" to "${current}" (${cause}).` }], + { source: { kind: 'plugin', plugin: 'user-approval' } }, + ) + }) + } + + /** + * Ask the composed answerers to decide one readonly same-process request. + * The service borrows the request, agent, session, and live signal directly. + * The request requires an open turn because the audit pair must be enclosed + * by the durable log's commit/replay boundary; an idle ask rejects before + * appending anything. The answerer phase always produces an outcome: an + * aborted signal yields `'cancelled'`, a missing or throwing answerer yields + * `'unavailable'` (fail closed), and a rogue non-vocabulary return value is + * normalized to `'unavailable'`. A failure that prevents either audit append + * from committing still rejects because returning an unlogged decision would + * violate the pair. Session contains post-commit observer failures, so an + * authoritative append cannot reject the request or suppress its matching + * audit event. + * @param req - the pending decision (agent, tool identity, reason, signal). + * @returns the closed outcome; `'allowed-once'` is the only grant. + * @throws when no turn is open or either audit event fails before the session + * append commit point. + */ + async request(req: ApprovalRequest): Promise { + const session = req.agent.session + if (!hasOpenTurn(session.events)) { + throw new Error( + 'approval.request() outside an open turn: the approval/asked + approval/decided audit pair ' + + 'must be turn-enclosed (a bare event between turns is crash-tail garbage on reload). ' + + 'Ask from inside the turn that needs the decision.', + ) + } + const id = ApprovalRequestId(randomUUID()) + session.append('approval/asked', { + id, + toolName: req.toolName, + ...req.callId !== undefined ? { callId: req.callId } : {}, + ...req.reason !== undefined ? { reason: req.reason } : {}, + }) + const outcome = await this.decide(req, session) + session.append('approval/decided', { id, outcome }) + return outcome + } + + /** + * The session's effective policy: its own `approval/policy` fold, else the + * configured default (the schema already defaulted an omitted policy to + * `'ask'`; the `??` only narrows the optional-input TYPE). + * @param session - the exact accepted session whose policy applies. + * @returns the policy every ask for this session resolves under right now. + */ + private effectivePolicy(session: Session): ApprovalPolicy { + return effectiveApprovalPolicy(session.events) ?? this.config.policy ?? 'ask' + } + + /** + * Dispatch the waterfall, contained and raced against the request signal. + * @param req - the borrowed public request. + * @param session - the request agent's session used for policy lookup. + * @returns the normalized closed outcome. + */ + private async decide(req: ApprovalRequest, session: Session): Promise { + const signal = req.signal + if (signal?.aborted) return 'cancelled' + // The 'never' policy is decided HERE, before any dispatch: a listener + // registered with `prepend: true` after this service mounts would sit + // ahead of any gate LISTENER, so a listener-shaped gate cannot keep the + // documented promise that 'never' rejects deterministically regardless + // of registration order — only the service's own request path can. + if (this.effectivePolicy(session) === 'never') return 'rejected' + // Enter the promise chain BEFORE dispatching: a listener that throws + // SYNCHRONOUSLY (before its first await) must land in the same rejection + // path as an async one — `Promise.resolve(call())` would let it escape + // the containment into the caller. + const answer: Promise = Promise.resolve().then( + () => this.ctx.waterfall( + scopeTarget(this, req.agent), 'approval/request', req, + () => Promise.resolve('unavailable'), + ), + ).then( + // Normalize a rogue (non-vocabulary) answerer return to the fail-closed + // outcome instead of leaking it into callers' closed-union switches. + outcome => OUTCOMES.includes(outcome) ? outcome : 'unavailable', + // A throwing answerer must fail the QUESTION closed, not the caller's + // tool call open — the seam contains its callbacks. + () => 'unavailable', + ) + if (signal === undefined) return answer + return await new Promise((resolve) => { + const onAbort = () => { + signal.removeEventListener('abort', onAbort) + resolve('cancelled') + } + signal.addEventListener('abort', onAbort, { once: true }) + void answer.then((outcome) => { + signal.removeEventListener('abort', onAbort) + // After an abort won the race this resolve is a settled-promise no-op: + // the late answer is discarded by construction. + resolve(outcome) + }) + }) + } +} + +export default ApprovalService diff --git a/packages/ui/user-approval/tests/approval.spec.ts b/packages/ui/user-approval/tests/approval.spec.ts new file mode 100644 index 0000000000..ff0cd5c7d8 --- /dev/null +++ b/packages/ui/user-approval/tests/approval.spec.ts @@ -0,0 +1,579 @@ +import { describe, expect, it, vi } from 'vitest' +import { Context } from 'cordis' +import type { Agent } from '@deepseek-ai/dsh-agent' +import { CallId } from '@deepseek-ai/dsh-llm' +import { carrierKeyOf, createScope } from '@deepseek-ai/dsh-scope' +import type { Scope } from '@deepseek-ai/dsh-scope' +import SessionStore, { Session, SessionId } from '@deepseek-ai/dsh-session' +import type { SessionEvent } from '@deepseek-ai/dsh-session' +import SystemPrompt from '@deepseek-ai/dsh-system-prompt' +import ApprovalService, { ApprovalOutcome, ApprovalRequest, effectiveApprovalPolicy, setApprovalPolicy } from '@deepseek-ai/dsh-user-approval' + +/** + * A minimal Agent stand-in — the service only reaches `agent.session.append` + * and folds `.events`. Seeded inside an open turn by default (request()'s + * turn-enclosure precondition); pass `seed` to stage idle/closed logs. + * Returns the recorded audit appends alongside the fake. + */ +function fakeAgent(seed: Array<{ type: string }> = [{ type: 'turn/start' }, { type: 'user/message' }]): { agent: Agent; appended: Array<{ type: string; data: Record }> } { + const appended: Array<{ type: string; data: Record }> = [] + const agent = { + session: { + events: seed, + append: (type: string, data: Record) => { + appended.push({ type, data }) + return { type, data } as unknown as SessionEvent + }, + }, + } as unknown as Agent + return { agent, appended } +} + +async function mounted(): Promise { + const ctx = new Context() + await ctx.plugin(ApprovalService) + return ctx +} + +function requestOf(agent: Agent, overrides: Partial = {}): ApprovalRequest { + return { agent, toolName: 'echo', ...overrides } +} + +describe('ApprovalService.request', () => { + it('throws before appending anything when no turn has ever opened (idle ask)', async () => { + const ctx = await mounted() + const { agent, appended } = fakeAgent([]) + + await expect(ctx.approval.request(requestOf(agent))).rejects.toThrow(/outside an open turn/) + expect(appended).toHaveLength(0) + }) + + it('throws between turns — a closed turn does not satisfy the enclosure precondition', async () => { + const ctx = await mounted() + const { agent, appended } = fakeAgent([{ type: 'turn/start' }, { type: 'turn/end' }]) + + await expect(ctx.approval.request(requestOf(agent))).rejects.toThrow(/outside an open turn/) + expect(appended).toHaveLength(0) + }) + + it('fails closed to unavailable when nobody listens, auditing the asked/decided pair', async () => { + const ctx = await mounted() + const { agent, appended } = fakeAgent() + + const outcome = await ctx.approval.request(requestOf(agent, { callId: CallId('call-1'), reason: 'hook says ask' })) + + expect(outcome).toBe('unavailable') + expect(appended.map(e => e.type)).toEqual(['approval/asked', 'approval/decided']) + const [asked, decided] = appended + expect(asked?.data).toMatchObject({ toolName: 'echo', callId: 'call-1', reason: 'hook says ask' }) + expect(decided?.data).toMatchObject({ outcome: 'unavailable' }) + expect(decided?.data['id']).toBe(asked?.data['id']) + }) + + it('omits absent optional fields from the asked audit event', async () => { + const ctx = await mounted() + const { agent, appended } = fakeAgent() + + await ctx.approval.request(requestOf(agent)) + + expect(Object.keys(appended[0]?.data ?? {}).sort()).toEqual(['id', 'toolName']) + }) + + it('borrows the exact readonly request for scoped dispatch and audit', async () => { + const ctx = await mounted() + const { agent, appended } = fakeAgent() + let scope!: Scope + const scopeFiber = await ctx.plugin(Object.assign((inner: Context) => { + scope = createScope(inner, agent) + }, { inject: ['approval'] })) + let received: ApprovalRequest | undefined + let carrier: unknown + scope.ctx.on('approval/request', function (req) { + received = req + carrier = carrierKeyOf(this) + return Promise.resolve('allowed-once') + }) + const request = requestOf(agent, { + toolName: 'scoped-tool', + callId: CallId('scoped-call'), + reason: 'scoped reason', + }) + + await expect(ctx.approval.request(request)).resolves.toBe('allowed-once') + expect(carrier).toBe(agent) + expect(received).toBe(request) + expect(appended).toHaveLength(2) + expect(appended[0]?.data).toMatchObject({ + toolName: 'scoped-tool', + callId: 'scoped-call', + reason: 'scoped reason', + }) + expect(appended[1]?.data).toMatchObject({ outcome: 'allowed-once' }) + expect(appended[1]?.data['id']).toBe(appended[0]?.data['id']) + await scopeFiber.dispose() + }) + + it('contains an approval/asked observer throw after append and still completes the pair', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + await ctx.plugin(ApprovalService) + const session = ctx.sessions.create(SessionId('asked-observer-throw')) + session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) + const agent = { session } as unknown as Agent + const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => {}) + ctx.on('session/event', (_session, event) => { + if (event.type === 'approval/asked') throw new Error('observer failed after asked append') + }) + ctx.on('approval/request', () => Promise.resolve('allowed-once')) + + await expect(ctx.approval.request(requestOf(agent))).resolves.toBe('allowed-once') + + const audit = session.events.filter(event => event.type.startsWith('approval/')) + const asked = session.events.find((event): event is SessionEvent<'approval/asked'> => event.type === 'approval/asked') + const decided = session.events.find((event): event is SessionEvent<'approval/decided'> => event.type === 'approval/decided') + expect(audit.map(event => event.type)).toEqual(['approval/asked', 'approval/decided']) + expect(decided?.data.id).toBe(asked?.data.id) + expect(warn).toHaveBeenCalledWith(expect.stringContaining('session/event listener threw: Error: observer failed after asked append')) + }) + + it('contains an approval/decided observer throw after append and still resolves', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + await ctx.plugin(ApprovalService) + const session = ctx.sessions.create(SessionId('decided-observer-throw')) + session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) + const agent = { session } as unknown as Agent + const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => {}) + ctx.on('session/event', (_session, event) => { + if (event.type === 'approval/decided') throw new Error('observer failed after decided append') + }) + ctx.on('approval/request', () => Promise.resolve('rejected')) + + await expect(ctx.approval.request(requestOf(agent))).resolves.toBe('rejected') + + const audit = session.events.filter(event => event.type.startsWith('approval/')) + const asked = session.events.find((event): event is SessionEvent<'approval/asked'> => event.type === 'approval/asked') + const decided = session.events.find((event): event is SessionEvent<'approval/decided'> => event.type === 'approval/decided') + expect(audit.map(event => event.type)).toEqual(['approval/asked', 'approval/decided']) + expect(decided?.data).toMatchObject({ id: asked?.data.id, outcome: 'rejected' }) + expect(warn).toHaveBeenCalledWith(expect.stringContaining('session/event listener threw: Error: observer failed after decided append')) + }) + + it('propagates an append failure that prevented audit log growth', async () => { + const ctx = await mounted() + const failure = new Error('append failed before log growth') + const agent = { + session: { + events: [{ type: 'turn/start' }], + append: () => { throw failure }, + }, + } as unknown as Agent + + await expect(ctx.approval.request(requestOf(agent))).rejects.toBe(failure) + }) + + it('returns the first answering listener outcome (single decision slot)', async () => { + const ctx = await mounted() + const { agent } = fakeAgent() + let secondRan = false + ctx.on('approval/request', () => Promise.resolve('allowed-once')) + ctx.on('approval/request', () => { + secondRan = true + return Promise.resolve('rejected') + }) + + await expect(ctx.approval.request(requestOf(agent))).resolves.toBe('allowed-once') + expect(secondRan).toBe(false) + }) + + it('lets a non-owning listener delegate via next() down to the fail-closed default', async () => { + const ctx = await mounted() + const { agent } = fakeAgent() + ctx.on('approval/request', (_req, next) => next()) + + await expect(ctx.approval.request(requestOf(agent))).resolves.toBe('unavailable') + }) + + it('dispatches to global and matching agent-scoped listeners, never a foreign scope', async () => { + const ctx = await mounted() + const { agent: agentA } = fakeAgent() + const { agent: agentB } = fakeAgent() + let scopeA!: Scope + let scopeB!: Scope + const scopesFiber = await ctx.plugin(Object.assign((inner: Context) => { + scopeA = createScope(inner, agentA) + scopeB = createScope(inner, agentB) + }, { inject: ['approval'] })) + const heard: string[] = [] + ctx.on('approval/request', (req, next) => { + heard.push(req.agent === agentA ? 'global:A' : 'global:B') + return next() + }) + scopeA.ctx.on('approval/request', (_req, next) => { + heard.push('scoped:A') + return next() + }) + scopeB.ctx.on('approval/request', (_req, next) => { + heard.push('scoped:B') + return next() + }) + + await expect(ctx.approval.request(requestOf(agentA))).resolves.toBe('unavailable') + await expect(ctx.approval.request(requestOf(agentB))).resolves.toBe('unavailable') + + expect(heard).toEqual(['global:A', 'scoped:A', 'global:B', 'scoped:B']) + await scopesFiber.dispose() + }) + + it('keys the scoped dispatch carrier to the exact request agent', async () => { + const ctx = await mounted() + const { agent } = fakeAgent() + let scope!: Scope + const scopeFiber = await ctx.plugin(Object.assign((inner: Context) => { + scope = createScope(inner, agent) + }, { inject: ['approval'] })) + let seenKey: object | undefined + scope.ctx.on('approval/request', function (req, next) { + seenKey = carrierKeyOf(this) + expect(req.agent).toBe(agent) + return next() + }) + + await expect(ctx.approval.request(requestOf(agent))).resolves.toBe('unavailable') + + expect(seenKey).toBe(agent) + await scopeFiber.dispose() + }) + + it('contains a throwing answerer as unavailable', async () => { + const ctx = await mounted() + const { agent, appended } = fakeAgent() + ctx.on('approval/request', () => Promise.reject(new Error('transport died'))) + + await expect(ctx.approval.request(requestOf(agent))).resolves.toBe('unavailable') + expect(appended[1]?.data).toMatchObject({ outcome: 'unavailable' }) + }) + + it('normalizes a rogue non-vocabulary answer to unavailable', async () => { + const ctx = await mounted() + const { agent } = fakeAgent() + // A JS answerer can return anything; the seam must not leak it into + // callers' closed-union switches. + ctx.on('approval/request', () => Promise.resolve('yolo' as ApprovalOutcome)) + + await expect(ctx.approval.request(requestOf(agent))).resolves.toBe('unavailable') + }) + + it('settles cancelled immediately on an already-aborted signal without asking anyone', async () => { + const ctx = await mounted() + const { agent, appended } = fakeAgent() + let asked = false + ctx.on('approval/request', () => { + asked = true + return Promise.resolve('allowed-once') + }) + + const outcome = await ctx.approval.request(requestOf(agent, { signal: AbortSignal.abort() })) + + expect(outcome).toBe('cancelled') + expect(asked).toBe(false) + expect(appended.map(e => e.type)).toEqual(['approval/asked', 'approval/decided']) + expect(appended[1]?.data).toMatchObject({ outcome: 'cancelled' }) + }) + + it('resolves cancelled when the signal aborts mid-question and discards the late answer', async () => { + const ctx = await mounted() + const { agent, appended } = fakeAgent() + let settleLate: ((outcome: ApprovalOutcome) => void) | undefined + ctx.on('approval/request', () => new Promise((resolve) => { settleLate = resolve })) + const controller = new AbortController() + + const pending = ctx.approval.request(requestOf(agent, { signal: controller.signal })) + controller.abort() + await expect(pending).resolves.toBe('cancelled') + + // The answerer settles after the fact: no second decided event appears. + settleLate?.('allowed-once') + await Promise.resolve() + expect(appended.filter(e => e.type === 'approval/decided')).toHaveLength(1) + expect(appended[1]?.data).toMatchObject({ outcome: 'cancelled' }) + }) + + it('discards a late REJECTION after abort without an unhandled rejection', async () => { + const ctx = await mounted() + const { agent } = fakeAgent() + let rejectLate: ((error: Error) => void) | undefined + ctx.on('approval/request', () => new Promise((_resolve, reject) => { rejectLate = reject })) + const controller = new AbortController() + + const pending = ctx.approval.request(requestOf(agent, { signal: controller.signal })) + controller.abort() + await expect(pending).resolves.toBe('cancelled') + + rejectLate?.(new Error('answered too late')) + // Drain microtasks: the contained rejection must not escape the seam. + await new Promise((resolve) => { setTimeout(resolve, 0) }) + }) + + it('resolves the answer when the signal never aborts', async () => { + const ctx = await mounted() + const { agent } = fakeAgent() + ctx.on('approval/request', () => Promise.resolve('rejected')) + const controller = new AbortController() + + await expect(ctx.approval.request(requestOf(agent, { signal: controller.signal }))).resolves.toBe('rejected') + }) + + it('issues a fresh id per request', async () => { + const ctx = await mounted() + const { agent, appended } = fakeAgent() + + await ctx.approval.request(requestOf(agent)) + await ctx.approval.request(requestOf(agent)) + + const ids = appended.filter(e => e.type === 'approval/asked').map(e => e.data['id']) + expect(ids).toHaveLength(2) + expect(ids[0]).not.toBe(ids[1]) + }) + + it('drops a disposed plugin listener from the chain (HMR safety)', async () => { + const ctx = await mounted() + const { agent } = fakeAgent() + const fiber = await ctx.plugin((inner: Context) => { + inner.on('approval/request', () => Promise.resolve('allowed-once')) + }) + await expect(ctx.approval.request(requestOf(agent))).resolves.toBe('allowed-once') + + await fiber.dispose() + await expect(ctx.approval.request(requestOf(agent))).resolves.toBe('unavailable') + }) +}) + +describe('approval policy (the approval/policy fold)', () => { + const NEVER_SENTENCE = 'Approval prompts are disabled in this session: actions that require approval are rejected automatically — do not request sandbox escalation (do not set `sandbox_permissions`).' + const ASK_MARKER = '' + const NEVER_MARKER = '' + + /** + * An agent stand-in over a REAL Session — gate, section, and narrator fold + * real events; the opened turn satisfies request()'s enclosure precondition. + */ + function sessionAgent(id: string): { agent: Agent; session: Session; injected: string[] } { + const session = new Session(SessionId(id)) + session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } }) + const injected: string[] = [] + const agent = { + id, + session, + inject: (content: { type: string; text: string }[]) => { injected.push(content[0]?.text ?? '') }, + } as unknown as Agent + return { agent, session, injected } + } + + const preStep = (ctx: Context, agent: Agent): Promise => + ctx.serial('agent/pre-step', agent, 1, 1, '', [], new AbortController().signal) + + /** Append a `request/header` snapshot whose system text is exactly `system`. */ + function appendHeader(session: Session, system: string): void { + session.append('request/header', { header: { config: { model: 'mock' }, system }, reason: 'initial' }) + } + + it('folds to the last event, or undefined without one', () => { + const { session } = sessionAgent('sess-fold') + expect(effectiveApprovalPolicy(session.events)).toBeUndefined() + setApprovalPolicy(session, 'never') + setApprovalPolicy(session, 'ask') + expect(effectiveApprovalPolicy(session.events)).toBe('ask') + expect(session.events.at(-1)).toMatchObject({ type: 'approval/policy', data: { policy: 'ask' } }) + }) + + it('rejects a policy outside the closed vocabulary before appending', () => { + const append = vi.fn() + const session = { append } as unknown as Session + + expect(() => { setApprovalPolicy(session, 'sometimes' as Parameters[1]) }) + .toThrow('approval policy must be one of "ask" or "never"') + expect(append).not.toHaveBeenCalled() + }) + + it('defaults a schema-less construction to ask (the ?? narrows the optional TYPE)', async () => { + // Direct construction bypasses the plugin schema (the SystemPrompt-test + // precedent for covering a defaulted Config field's type-narrowing ??). + const ctx = new Context() + const service = new ApprovalService(ctx, {}) + const { agent } = sessionAgent('sess-bare-config') + ctx.on('approval/request', () => Promise.resolve('allowed-once')) + await expect(service.request({ agent, toolName: 'echo' })).resolves.toBe('allowed-once') + }) + + it('contains an answerer that throws SYNCHRONOUSLY as unavailable', async () => { + const ctx = new Context() + await ctx.plugin(ApprovalService) + const { agent } = sessionAgent('sess-syncthrow') + ctx.on('approval/request', () => { throw new Error('sync bug') }) + await expect(ctx.approval.request({ agent, toolName: 'echo' })).resolves.toBe('unavailable') + }) + + it('a never config rejects deterministically without consulting any answerer', async () => { + const ctx = new Context() + await ctx.plugin(ApprovalService, { policy: 'never' }) + const consulted = vi.fn() + ctx.on('approval/request', (_req, next) => { consulted(); return next() }) + const { agent, session } = sessionAgent('sess-gate-1') + await expect(ctx.approval.request({ agent, toolName: 'bash' })).resolves.toBe('rejected') + expect(consulted).not.toHaveBeenCalled() + // The audit pair still lands on the session log. + expect(session.events.filter(e => e.type === 'approval/asked')).toHaveLength(1) + expect(session.events.filter(e => e.type === 'approval/decided')).toHaveLength(1) + }) + + it('the gate decides FIRST even against an answerer registered before the service (prepend)', async () => { + const ctx = new Context() + ctx.on('approval/request', () => Promise.resolve('allowed-once')) + await ctx.plugin(ApprovalService, { policy: 'never' }) + const { agent } = sessionAgent('sess-gate-2') + await expect(ctx.approval.request({ agent, toolName: 'bash' })).resolves.toBe('rejected') + }) + + it('never is unbypassable even by an answerer PREPENDED after the service mounts', async () => { + // Cordis prepend unshifts ahead of every existing listener, including + // any gate LISTENER the service could register — which is exactly why + // the 'never' decision lives inside request() instead. The eager grant + // below must never be consulted. + const ctx = new Context() + await ctx.plugin(ApprovalService, { policy: 'never' }) + const consulted = vi.fn() + ctx.on('approval/request', () => { consulted(); return Promise.resolve('allowed-once') }, { prepend: true }) + const { agent, appended } = fakeAgent() + await expect(ctx.approval.request(requestOf(agent))).resolves.toBe('rejected') + expect(consulted).not.toHaveBeenCalled() + expect(appended.map(e => e.type)).toEqual(['approval/asked', 'approval/decided']) + }) + + it('a session override outranks the configured default, in both directions', async () => { + const ctx = new Context() + await ctx.plugin(ApprovalService, { policy: 'never' }) + ctx.on('approval/request', () => Promise.resolve('allowed-once')) + const { agent, session } = sessionAgent('sess-gate-3') + setApprovalPolicy(session, 'ask') + await expect(ctx.approval.request({ agent, toolName: 'bash' })).resolves.toBe('allowed-once') + setApprovalPolicy(session, 'never') + await expect(ctx.approval.request({ agent, toolName: 'bash' })).resolves.toBe('rejected') + }) + + it('states never (and only never) in prose while recording either policy with a source-owned marker', async () => { + const ctx = new Context() + await ctx.plugin(SystemPrompt) + await ctx.plugin(ApprovalService) + const askAgent = sessionAgent('sess-sect-ask').agent + const { agent: neverAgent, session } = sessionAgent('sess-sect-never') + setApprovalPolicy(session, 'never') + const sectionFor = async (context: object) => + (await ctx.systemPrompt.assemble(context)).sections.find(s => s.name === 'approval:policy')?.text + expect(await sectionFor({ agent: askAgent })).toBe(ASK_MARKER) + expect(await sectionFor({ agent: neverAgent })).toBe(`${NEVER_SENTENCE}\n${NEVER_MARKER}`) + // A bare assemble (no agent) has no session to state. + expect(await sectionFor({})).toBe('') + }) + + it('narrates nothing cold, once per coalesced switch (user wording), and idempotently', async () => { + const ctx = new Context() + await ctx.plugin(ApprovalService) + const { agent, session, injected } = sessionAgent('sess-narr-1') + await preStep(ctx, agent) + expect(injected).toEqual([]) + setApprovalPolicy(session, 'never') + setApprovalPolicy(session, 'ask') + setApprovalPolicy(session, 'never') + await preStep(ctx, agent) + expect(injected).toEqual(['The approval policy changed from "ask" to "never" (changed by the user).']) + await preStep(ctx, agent) + expect(injected).toHaveLength(1) + setApprovalPolicy(session, 'ask') + setApprovalPolicy(session, 'never') + await preStep(ctx, agent) + expect(injected).toHaveLength(1) + }) + + it('reads what the model was told back from the folded header text after a restart', async () => { + // A session whose last request carried the never sentence resumes under + // an ask default: the narrator attributes the change to the operator. + const ctx = new Context() + await ctx.plugin(ApprovalService) + const { agent, session, injected } = sessionAgent('sess-narr-2') + appendHeader(session, `persona\n\n${NEVER_SENTENCE}\n${NEVER_MARKER}`) + await preStep(ctx, agent) + expect(injected).toEqual(['The approval policy changed from "never" to "ask" (changed by the operator/config).']) + }) + + it('narrates a config default drift from the logged ask marker', async () => { + const ctx = new Context() + await ctx.plugin(ApprovalService, { policy: 'never' }) + const { agent, session, injected } = sessionAgent('sess-narr-3') + appendHeader(session, `persona only\n${ASK_MARKER}`) + await preStep(ctx, agent) + expect(injected).toEqual(['The approval policy changed from "ask" to "never" (changed by the operator/config).']) + }) + + it('a pinned override survives a default change silently', async () => { + const ctx = new Context() + await ctx.plugin(ApprovalService, { policy: 'never' }) + const { agent, session, injected } = sessionAgent('sess-narr-4') + appendHeader(session, `persona only\n${ASK_MARKER}`) + setApprovalPolicy(session, 'ask') + appendHeader(session, `persona only\n${ASK_MARKER}`) + await preStep(ctx, agent) + expect(injected).toEqual([]) + }) + + it('does not infer never from deployment prose that quotes the never sentence', async () => { + const ctx = new Context() + await ctx.plugin(ApprovalService) + const { agent, session, injected } = sessionAgent('sess-narr-spoof-prose') + appendHeader(session, `persona quotes this warning: ${NEVER_SENTENCE}\n${ASK_MARKER}`) + await preStep(ctx, agent) + expect(injected).toEqual([]) + }) + + it('treats a legacy header with no source-owned marker as untold', async () => { + const ctx = new Context() + await ctx.plugin(ApprovalService, { policy: 'never' }) + const { agent, session, injected } = sessionAgent('sess-narr-unmarked-header') + appendHeader(session, 'legacy persona-only header') + await preStep(ctx, agent) + expect(injected).toEqual([]) + }) + + it('uses the service marker after an earlier persona marker', async () => { + const ctx = new Context() + await ctx.plugin(ApprovalService) + const { agent, session, injected } = sessionAgent('sess-narr-spoof-marker') + appendHeader(session, `persona quotes ${NEVER_MARKER}\n${ASK_MARKER}`) + await preStep(ctx, agent) + expect(injected).toEqual([]) + }) + + it('disposes the service prompt section and pre-step narrator together (HMR safety)', async () => { + const ctx = new Context() + await ctx.plugin(SystemPrompt) + const fiber = await ctx.plugin(ApprovalService) + const live = sessionAgent('sess-hmr-service-live') + const afterDispose = sessionAgent('sess-hmr-service-disposed') + const sectionFor = async () => + (await ctx.systemPrompt.assemble({ agent: live.agent })).sections.find(section => section.name === 'approval:policy') + expect(await sectionFor()).toBeDefined() + + appendHeader(live.session, `persona\n${ASK_MARKER}`) + setApprovalPolicy(live.session, 'never') + await preStep(ctx, live.agent) + expect(live.injected).toEqual(['The approval policy changed from "ask" to "never" (changed by the user).']) + + appendHeader(afterDispose.session, `persona\n${ASK_MARKER}`) + setApprovalPolicy(afterDispose.session, 'never') + await fiber.dispose() + + expect(await sectionFor()).toBeUndefined() + await preStep(ctx, afterDispose.agent) + expect(afterDispose.injected).toEqual([]) + }) +}) diff --git a/packages/ui/user-approval/tsconfig.json b/packages/ui/user-approval/tsconfig.json new file mode 100644 index 0000000000..fb9a9e6e2d --- /dev/null +++ b/packages/ui/user-approval/tsconfig.json @@ -0,0 +1,39 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "lib/types" + }, + "include": [ + "src" + ], + "references": [ + { + "path": "../../../vendor/cosmokit" + }, + { + "path": "../../../vendor/cordis" + }, + { + "path": "../../../vendor/schemastery" + }, + { + "path": "../../util/brand" + }, + { + "path": "../../llm/llm" + }, + { + "path": "../../core/scope" + }, + { + "path": "../../core/session" + }, + { + "path": "../../core/agent" + }, + { + "path": "../../core/system-prompt" + } + ] +} diff --git a/packages/workflow/workflow-workerthread/README.md b/packages/workflow/workflow-workerthread/README.md index 1ee959cc0c..b9786ae1e2 100644 --- a/packages/workflow/workflow-workerthread/README.md +++ b/packages/workflow/workflow-workerthread/README.md @@ -1,49 +1,80 @@ # @deepseek-ai/dsh-workflow-workerthread -The [`WorkflowService`](../workflow/README.md) implementation, on **`node:worker_threads`**: each run gets its OWN worker thread (one run = one worker, no pooling — a run is heavyweight, so the ~tens-of-ms thread spin-up is noise), the script executes in a vm context INSIDE that worker with the workflow hooks injected, and every `agent()` call bridges back over the message port to [`ctx.subagents`](../../subagent/README.md) on the host. Child agents are I/O-bound LLM loops and stay on the host event loop; the thread isolates the SCRIPT, the only part that can spin synchronously. +This package implements `WorkflowService` with one Node worker thread per run. The worker executes the orchestration script; child agents remain on the host and are reached through `ctx.subagents` over a typed host/worker protocol. -## Trust premise: what the thread buys (and what it does not) +The split has one primary purpose: a synchronous script loop cannot block the harness event loop, and a script that ignores cancellation can be terminated with its worker. It is not a security sandbox. -Workflow scripts are **model-written** — the same trust level as the model's existing bash access — so this engine defends against **buggy** scripts, never hostile ones. A worker thread is NOT a security boundary: the vm context inside it is escapable by construction (`node:vm` shares object machinery with its surrounding realm, so a script can reach the `Function` constructor via `globalThis.constructor.constructor` and from it `process` and every Node builtin), and an escapee holds the same process privileges as the host — Node's permission model is process-wide. The absent globals are API surface that keeps honest scripts portable, not walls. What the thread concretely buys: +## Trust and isolation boundary -- **The host never blocks**: `start()` returns without running any script code on the host; a synchronous spin anywhere in the script occupies the worker's loop, not the harness's. -- **Termination is real**: a script that outlives its post-cancel grace is `worker.terminate()`d — nothing of it survives `dispose()`, where an in-process engine could only abandon the spin on its own loop. -- **No ambient credentials**: the worker spawns with an EMPTY environment (`env: {}` plus hermetic `execArgv`, the same stance as `dsh-code-runtime-worker`; the unbuilt dev shape forwards exactly one loader variable, `TSX_TSCONFIG_PATH` — path plumbing, not a secret), so an escapee reading `process.env` finds no harness secrets — ambient-channel hardening only; the process-wide privileges above (fs and the rest) remain, so a genuine sandbox is still the engine swap. -- **Serialization by construction**: everything crossing the thread is structured-clone data, and plain JSON before that — the `materializeFromRealm` walk rejects loud what JSON cannot carry, which is also what makes every postMessage hop total. +Workflow scripts are model-written and have the same trust premise as the model's existing bash access. `node:vm` inside a worker is an API-shaping mechanism, not a security boundary: an escaped script can recover Node capabilities with the host process's privileges. -What the seam guarantees regardless, because benign scripts hit these constantly: `result` never rejects, a dropped hook promise never becomes an unhandled rejection, values JSON cannot carry are rejected **loud** instead of silently mangled, and hook misuse is fatal instead of dissolving into a per-item `null`. Genuine sandboxing (containing what an escaped script may touch) remains an isolated-vm/separate-process engine swap behind the seam, still deferred. +The worker still provides useful containment: -## The script contract it executes +- Script CPU work and synchronous spins stay off the host event loop. +- `worker.terminate()` gives disposal a real final stop. +- The worker starts with an empty environment, except unbuilt loader plumbing, so ambient credentials do not cross through `process.env`. +- Host/worker messages use structured-clone data, with plain-JSON validation at the script boundary. -- **Meta as DATA** (`validateMeta`, host-side): the workflow's identity arrives on the start request as plain JSON (the tool carries it as its schema-validated `meta` parameter — never as script text) and is shape-validated loud, every violation named (`name`/`description` required; unknown fields rejected). The engine deliberately evaluates NO script text to obtain meta: an evaluated meta literal could smuggle getters that run on the host outside any vm timeout — the exact spin the worker thread exists to isolate. A body that still opens with a Claude Code-style `export const meta` statement is rejected with a pointed `SCRIPT_PARSE` message. -- **Hooks**: `agent(prompt, {label, phase, schema, model})` (schema = the [structured-output subset](../../core/tools/README.md), forwarded as `outputSchema`; result = validated object, or final text without a schema; a failed child resolves `null`), `parallel(thunks)`, `pipeline(items, ...stages)` with NO cross-stage barrier and `(prev, item, index)` stage callbacks, `phase(title)`, `log(message)`, and the `args` global. Anything else — `effort`/`isolation`/`agentType`, unknown options, malformed arguments, schemas outside the subset — throws a FATAL `WorkflowError` that `parallel`/`pipeline` re-throw rather than nulling (see the seam README's failure discipline). -- **No ambient APIs**: no timers, filesystem, or Node APIs are injected into the context (absence is API surface, not containment — see the trust premise). +A genuinely untrusted-script sandbox would require a different engine behind the same workflow seam. -## How a run executes +## Script contract -`start()` shape-validates the meta DATA host-side and parse-checks the body with the identical wrapper the worker compiles (`new vm.Script`, discarded), preserving the seam's synchronous `META_INVALID`/`SCRIPT_PARSE` throws; one redundant parse per run is the deliberate price. It then spawns the worker (`src/worker.ts` unbuilt via an explicit tsx `execArgv`; the sibling `lib/worker.js` bundle when built) with the meta, body, `args`, and worker-side limits as `workerData`. +The workflow's `meta` is host-provided data, not evaluated script text. The engine validates its required `name` and `description`, rejects unknown fields, and parse-checks the body before returning a run. -Inside the worker, `runWorkerSession` builds the execution core (hooks, combinators, concurrency semaphore, caps, fatal-error discipline) over a **child port**: `agent()` sends `child-start` and the host starts the child on `ctx.subagents` (parent attribution, the shared per-run abort signal, `outputSchema`/`model` pass-through), replying with the child id, its settlement (a JSON projection; an infrastructure REJECTION crosses as `child-failed` and stays the fatal `AGENT_RESULT`), and dispose acks. Observer narration (`phase`/`log`/`agent-start`/`agent-end`) crosses as messages and re-emits as the seam's `workflow/*` events. A **ready→go handshake** gates the body: a cancellation racing worker boot arrives before `go`, so a run cancelled before start never executes the body at all. +Inside the worker, the script receives `args` and these hooks: -## The value boundary +- `agent(prompt, { label, phase, schema, model })` starts one host-side subagent. With a schema it returns the structured value; otherwise it returns final text. An ordinary failed child yields `null`. +- `parallel(thunks)` runs thunks under the configured concurrency limit. +- `pipeline(items, ...stages)` passes `(previous, item, index)` without a cross-stage barrier. +- `phase(title)` and `log(message)` emit observer narration. -Values LEAVING the script (hook options/schemas, the script's return) are materialized by `materializeFromRealm`: a plain recursive walk that rejects loud everything JSON cannot carry (exotic prototypes, functions, symbols, cycles, sparse arrays, non-finite numbers, nested `undefined`), copying into plain containers via `defineProperty` so a `"__proto__"` key becomes a data property, never a prototype mutation. Getters are read ordinarily — the RESULT is what crosses; a read that throws fails loud; both run in the WORKER, never on the host. Values ENTERING the realm (`args`, `agent()` results, hook promises and their failures, combinator arrays) are handed over directly as worker-realm values — the script is trusted, so outer prototypes are not a leak; `args` is cloned once at start so a script scribbling on it cannot mutate the caller's object. One script-visible consequence: an error thrown by a hook is built OUTSIDE the script's vm context, so `e instanceof Error` inside the script is `false` — branch on `e.name`/`e.code` instead (the combinators recognize fatality by `instanceof` against their own realm's class, which a script-built object can never pass, so fatal-vs-null cannot be forged or dissolved). +Unknown options, malformed arguments, unsupported schemas, tripped caps, provider-start failures, and infrastructure result failures are fatal workflow errors. No timers, filesystem API, or Node globals are intentionally injected, though the trust caveat above still applies. -## Cancellation, death, disposal +## Run sequence -Per-run limits: a concurrency semaphore (`maxConcurrentAgents`), a total-`agent()` cap (`maxTotalAgents`), and a per-call item cap (`maxItemsPerCall`), all config. `cancel()` posts the cancel to the worker (its hooks start throwing `CANCELLED`; the script dies at its next await) and cancels every host-side child NOW on **both seam channels** — the shared request signal aborts AND each registered child's explicit `cancel()` is called host-side, because the seam leaves a provider free to honor either channel and a worker wedged in a synchronous spin could not relay its own per-child cancel RPCs (those later land as idempotent no-ops). The grace then arms: a run still unsettled `disposeGraceMs` later force-settles `cancelled` and the worker is **terminated**. A cancellation that lands before the body runs (the ready→go handshake) reports `cancelled` without executing anything; a worker `result` racing an in-flight host cancellation reports `cancelled` too (first-wins settlement — the seam-visible result had not settled when cancellation was requested); post-cancel `phase`/`log` narration is suppressed host-side, while cancelled children still deliver their paired `agent-end`. +`start()` validates meta and parses the body, creates the worker, and returns a holder-owned `WorkflowRun`. A ready/go handshake prevents a start-signal cancellation racing worker boot from executing the script's initial synchronous slice. -A worker that dies unexpectedly (an OOM, a script reaching `process.exit` through the documented vm escape) settles the run `stopReason: 'error'` with the exit diagnostics — or `'cancelled'` when a cancel was in flight — and the host-side child registry is what winds every surviving child down. `dispose()` = cancel + immediate host-driven disposal of every registered child (a wedged worker can relay no dispose RPC, so child teardown overlaps the grace instead of starting after it; the worker's own dispose RPCs join the same per-child disposal) + bounded wait (result, then child-registry quiescence, capped by the grace) + unconditional `worker.terminate()`: the thread never outlives its run. Once a run settles, stray children a script fired without awaiting are cancelled too, and `dispose()` waits for their disposal (bounded by the grace) before returning. `agent-start`/`agent-end` pairing is host-guaranteed the same way: forwarded starts live in a ledger, worker-reported ends pair them on the graceful paths, and the termination paths (grace force-settle, worker death) synthesize the missing ends (outcome `cancelled`) before the run settles — a start still in flight across the force-settle can surface after `workflow/end`, immediately paired the same way. +For each `agent()` call: -**Engine-specific limitations**: worker startup is paid per run; on a termination path `agentsStarted` reports the HOST-observed count (accepted `child-start`s — calls still queued worker-side for a concurrency slot are unknowable then); and a returned promise or thenable resolves per JavaScript semantics BEFORE materialization — that is what makes an un-awaited `return agent('x')` work — with the value-boundary guard applying to the resolution. +1. The worker sends `child-start` with a plain-data prompt and options. +2. The host calls the configured provider through async `SubagentService.start`, passing the workflow's parent and one canonical per-run abort signal. +3. If start rejects, the host sends `child-start-error`; provider startup has already reached quiescence and no child lifecycle event is emitted. +4. If start fulfills while the workflow still admits work, the host records the run, observes `result`, then sends `child-started`. Even an already-settled result is forwarded afterward, preserving start-before-result order. +5. The worker emits paired `workflow/agent-start` and `workflow/agent-end` narration and requests child disposal after collection. + +Provider starts are tracked separately from published children. If cancellation, worker death, or normal workflow settlement closes admission while a start is pending, the shared signal aborts it. A provider that nevertheless fulfills after closure is disposed by the host and never announced to the worker. + +## Value boundary + +Values leaving the script pass through `materializeFromRealm`, which accepts plain, lossless JSON data and rejects exotic prototypes, functions, symbols, cycles, sparse arrays, non-finite numbers, and nested `undefined`. The walk runs in the worker, and defines object keys as data properties so `__proto__` cannot mutate a prototype. + +Child results are projected and snapshotted before crossing from the host to the worker. This is a real process-like serialization boundary; it is deliberately different from trusted same-process workflow and subagent event payloads, which are borrowed immutable values. + +## Cancellation and disposal + +`WorkflowRun.cancel()` records the first reason, tells the worker to cancel, aborts the one signal shared by every pending and published child, and arms the `disposeGraceMs` timer. Worker hooks then throw `CANCELLED` at their next await. If the run remains unsettled at the deadline, the host resolves it as cancelled, pairs stranded child lifecycle events, and terminates the worker. + +The subagent seam has one cancellation channel: the request signal. There is no separate child-cancel RPC. Published child teardown uses `run.dispose()`; pending provider starts remain provider-owned until their promise rejects or fulfills. + +Normal settlement also aborts pending starts and begins disposing any published fire-and-forget children before the result becomes externally settled. The host's quiescence condition includes both pending starts and published child disposals, so cleanup does not forget an async startup transaction. + +`dispose()` is idempotent. It cancels the run, starts host-driven disposal immediately, waits for result plus child quiescence up to the same grace, terminates the worker unconditionally, and performs a final survivor sweep. Per-child disposal is memoized so worker RPC, host cancellation, death cleanup, and public disposal all join one operation. + +## Outcome and event guarantees + +Terminal outcome is first-wins at host claim points. An accepted external cancellation overrides a later non-cancelled worker result; a result or worker death that claims first cannot be rewritten by reentrant cleanup callbacks. + +Worker error, message failure, or premature exit closes message admission before cleanup, then resolves `error` unless cancellation already owns the run. Late queued messages cannot create children or narrate after that logical boundary. + +The host keeps a ledger of forwarded child starts. A graceful worker supplies their ends; death or force termination synthesizes any missing end as cancelled. Every forwarded `workflow/agent-start` is therefore paired exactly once, although cleanup after an already-arrived workflow result may complete afterward. ## Config | Key | Default | Meaning | |---|---|---| -| `provider` | `spawn` | The `ctx.subagents` provider children run on (host-side). | -| `maxConcurrentAgents` | `0` (auto) | Concurrent `agent()` ceiling; `0` resolves to `min(16, max(1, cores - 2))`. | -| `maxTotalAgents` | `1000` | Total `agent()` calls one run may start (runaway-loop backstop). | -| `maxItemsPerCall` | `4096` | Items accepted by one `parallel()`/`pipeline()` call. | -| `syncTimeoutMs` | `5000` | vm timeout for the script's initial synchronous slice (in the worker). | -| `disposeGraceMs` | `5000` | How long a cancelled run may stay unsettled before force-settle + terminate; also bounds `dispose()`. | +| `provider` | `spawn` | Host-side subagent provider used by `agent()`. | +| `maxConcurrentAgents` | `0` | Concurrent `agent()` ceiling; `0` resolves from available CPU parallelism. | +| `maxTotalAgents` | `1000` | Total `agent()` calls in one run. | +| `maxItemsPerCall` | `4096` | Items accepted by one `parallel()` or `pipeline()` call. | +| `syncTimeoutMs` | `5000` | VM timeout for the script's initial synchronous slice. | +| `disposeGraceMs` | `5000` | Bound before force-settlement/termination and for public disposal. | diff --git a/packages/workflow/workflow-workerthread/package.json b/packages/workflow/workflow-workerthread/package.json index f86c74c2f7..ed934cd0cc 100644 --- a/packages/workflow/workflow-workerthread/package.json +++ b/packages/workflow/workflow-workerthread/package.json @@ -30,6 +30,7 @@ "@deepseek-ai/dsh-agent": "^0.0.1", "@deepseek-ai/dsh-brand": "^0.0.1", "@deepseek-ai/dsh-llm": "^0.0.1", + "@deepseek-ai/dsh-session": "^0.0.1", "@deepseek-ai/dsh-subagent": "^0.0.1", "@deepseek-ai/dsh-tools": "^0.0.1", "@deepseek-ai/dsh-workflow": "^0.0.1", diff --git a/packages/workflow/workflow-workerthread/src/host.ts b/packages/workflow/workflow-workerthread/src/host.ts index 6b6391a02e..95c07f0d2d 100644 --- a/packages/workflow/workflow-workerthread/src/host.ts +++ b/packages/workflow/workflow-workerthread/src/host.ts @@ -1,63 +1,80 @@ /** * The host half of one worker-engine run: spawn the Worker, bridge its child - * RPC onto `ctx.subagents`, fan its observer messages into the engine's - * events, and own cancellation, the settle-within-grace guarantee, and child - * cleanup. The worker's lifetime IS the run's lifetime: `dispose()` always - * ends with `worker.terminate()`, so no thread outlives its run. + * RPC onto the holder-bound subagent service, fan its observer messages into + * the engine's events, and own cancellation, the settle-within-grace + * guarantee, and child cleanup. The worker's lifetime IS the run's lifetime: + * `dispose()` always ends with `worker.terminate()`, so no thread outlives its + * run. * * The run's `result` promise settles exactly once, from whichever of these - * lands first: the worker's `result` message (a host-side cancellation in - * flight overrides a non-cancelled report — the seam-visible result had not - * settled when cancellation was requested), an unexpected worker death - * (`error`/`messageerror`/premature `exit` → `stopReason: 'error'`, or - * `'cancelled'` when a cancel was in flight), or the post-cancel grace timer - * (a script that never settles is force-settled `cancelled` and its worker - * terminated — the real kill an in-process engine could not perform). + * lands first: receipt of the worker's `result` message, an unexpected worker + * death (`error`/`messageerror`/premature `exit` → `stopReason: 'error'`, or + * `'cancelled'` when a cancel was in flight), or the post-cancel grace timer (a + * script that never settles is force-settled `cancelled` and its worker + * terminated — the real kill an in-process engine could not perform). At + * `result` receipt the host snapshots whether caller/signal/dispose + * cancellation is already in flight: an earlier cancellation overrides a + * non-cancelled report; otherwise the report wins before settlement-only child + * cleanup invokes arbitrary provider callbacks. Worker death uses the same + * boundary: it claims `error` (or a previously requested `cancelled`) before + * reaping children, so cleanup callbacks cannot rewrite the outcome. That + * first signal also closes inbound message admission: Node may emit `error`, + * then deliver queued messages, then emit `exit`, but those late messages may + * neither create work nor narrate after settlement. If Result or grace already + * owns the outcome, death preserves it while still cleaning resources; the + * eventual exit performs a final disposal-only sweep without repeating child + * cancellation. * - * Children live in a host-side registry (callId → run): the worker drives - * their disposal by RPC on the graceful path, `dispose()` host-drives every - * registered child's disposal immediately (a wedged worker can relay no - * dispose RPC, and child teardown must overlap the grace, not start after - * it), and the registry is what lets the host abort and dispose every - * survivor when the worker dies or is terminated mid-flight. The three - * paths share ONE disposal per child (memoized by callId; the seam's - * dispose() is idempotent anyway, the memo keeps the bookkeeping and the - * containment warn single). Lifecycle pairing is host-guaranteed the same - * way: every forwarded `agent-start` lives in a ledger, and a start the - * dead or terminated worker never paired is closed by a synthesized - * `agent-end` (outcome `'cancelled'`) before the run settles. On a - * termination path `agentsStarted` reports the - * HOST-observed count (accepted `child-start` messages) — `agent()` calls - * still queued worker-side for a concurrency slot are unknowable then; the - * worker's own count rides the result message on every graceful path. + * Provider starts and published children are tracked separately. Every start + * receives one shared per-run abort signal; the provider owns partial setup + * until its promise fulfills. If admission closes while a start is pending, + * the signal aborts it; a late fulfillment is disposed without publication to + * the worker. Ready runs enter a callId registry whose memoized disposal is + * shared by graceful worker RPC, public disposal, normal-settlement reap, and + * worker-death cleanup. Quiescence requires both pending starts and published + * children to drain. Lifecycle pairing is host-guaranteed independently: + * every forwarded `agent-start` enters a ledger, and a dead or terminated + * worker's missing `agent-end` is synthesized exactly once as cancelled. On a + * termination path `agentsStarted` reports the host-observed child-start count; + * calls still queued worker-side for a concurrency slot are unknowable. * * @module @deepseek-ai/dsh-workflow-workerthread/host */ -import { fileURLToPath } from 'node:url' import { Worker } from 'node:worker_threads' import type { WorkerOptions } from 'node:worker_threads' import type { Context } from 'cordis' import type { Agent } from '@deepseek-ai/dsh-agent' import { assertNever } from '@deepseek-ai/dsh-llm' +import { snapshotJsonValue } from '@deepseek-ai/dsh-session' +import type SubagentService from '@deepseek-ai/dsh-subagent' import type { SubagentRun } from '@deepseek-ai/dsh-subagent' import type { WorkflowAgentEndInfo, WorkflowAgentInfo, WorkflowMeta, WorkflowResult, WorkflowRun, WorkflowRunId } from '@deepseek-ai/dsh-workflow' import { renderThrown } from './realm.ts' import type { ExecutionObserver } from './runtime.ts' import { HostToWorkerType, WorkerToHostType } from './protocol.ts' import type { HostToWorkerPayloads, WorkerToHostMessage } from './protocol.ts' -import type { ChildStartRequest, WorkerInit } from './types.ts' +import type { ChildResult, ChildStartRequest, WorkerInit } from './types.ts' + +/** One published child and its shared quiescent-disposal transaction. */ +interface ChildRecord { + readonly run: SubagentRun + disposal?: Promise +} /** * Resolve the worker entry and spawn options for the current runtime shape. * Unbuilt (tsx demos, vitest — `import.meta.url` points into `src/`), the - * entry is the TypeScript sibling and the worker needs the tsx loader - * registered explicitly: a worker thread inherits no transform pipeline from - * vitest (vite transforms in-process, not via a node loader), and passing - * execArgv explicitly also shields the worker from any loader flags the - * parent was started with. Built (`lib/index.js`), the entry is the sibling - * bundle the package tsdown config emits and no loader is needed (execArgv - * pinned empty — hermetic, like the environment). + * entry is a JavaScript data-URL bootstrap. That bootstrap runs INSIDE the + * user worker, registers tsx's ESM AND CommonJS transforms there, and only + * then imports the TypeScript sibling. The whole mixed-module source graph + * therefore receives TypeScript transformation and the tsconfig paths map in + * the worker's own module-loader realm. A worker inherits no + * transform pipeline from vitest (vite transforms in-process), and a parent + * `--import tsx` registration is not a contract that user workers share on + * every supported Node line. Built (`lib/index.js`), the entry is the sibling + * bundle the package tsdown config emits and no loader is needed (`execArgv` + * pinned empty in both shapes — hermetic, like the environment). * * Both shapes spawn with an EMPTY environment (`env: {}`): the documented vm * escape reaches `process`, and the harness's ambient credentials @@ -77,19 +94,31 @@ function resolveWorkerSpawn(init: WorkerInit): { entry: URL; options: WorkerOpti if (!import.meta.url.endsWith('.ts')) { return { entry: new URL('./worker.js', import.meta.url), options: { workerData: init, env: {}, execArgv: [] } } } - // Lazy tsx resolution: only the unbuilt shape needs it, so the built - // bundle never requires tsx to be installed. TSX_TSCONFIG_PATH is the one - // variable forwarded through the scrub: tsx finds a tsconfig by searching - // UP from the worker's cwd, and a parent running with its cwd outside the - // repo (the ACP snapshot harness pins the tsconfig through this exact - // variable) would otherwise lose the dsh-* paths map and resolve workspace - // imports to unbuilt lib/ bundles. Loader plumbing, not a secret. + // Resolve tsx lazily: only the unbuilt shape executes this arm, so a built + // consumer never needs the dev-only loader installed. A JavaScript entry is + // essential — it can install tsx's ESM and CommonJS hooks from INSIDE the + // user worker before any TypeScript enters Node's native strip-only parser. + // Both hooks are load-bearing because the source graph crosses both module + // shapes on supported Node lines. TSX_TSCONFIG_PATH is + // the one variable forwarded through the scrub: a parent running outside + // the repo cwd (the ACP snapshot harness is the real case) pins the paths + // map through it. Loader plumbing, not a secret. + const workerEntry = new URL('./worker.ts', import.meta.url) + const tsxEsmApiEntry = import.meta.resolve('tsx/esm/api') + const tsxCjsApiEntry = import.meta.resolve('tsx/cjs/api') + const bootstrap = [ + `import { register as registerEsm } from ${JSON.stringify(tsxEsmApiEntry)}`, + `import { register as registerCjs } from ${JSON.stringify(tsxCjsApiEntry)}`, + 'registerCjs()', + 'registerEsm()', + `await import(${JSON.stringify(workerEntry.href)})`, + ].join('\n') return { - entry: new URL('./worker.ts', import.meta.url), + entry: new URL(`data:text/javascript,${encodeURIComponent(bootstrap)}`), options: { workerData: init, env: process.env.TSX_TSCONFIG_PATH === undefined ? {} : { TSX_TSCONFIG_PATH: process.env.TSX_TSCONFIG_PATH }, - execArgv: ['--import', fileURLToPath(import.meta.resolve('tsx'))], + execArgv: [], }, } } @@ -97,15 +126,21 @@ function resolveWorkerSpawn(init: WorkerInit): { entry: URL; options: WorkerOpti /** * One live worker-engine run — the seam's {@link WorkflowRun}, returned by * `start()` directly. Owns the Worker, the child registry, and the result - * settlement; `result` never rejects. `meta` is this handle's OWN clone - * (event payloads carry separate clones), so a consumer mutating it corrupts - * nothing. + * settlement; `result` never rejects. `meta` is trusted same-process data + * borrowed as immutable by the handle and lifecycle events. The holder-bound + * SubagentService handle is captured before the + * engine returns this run, so unloading the engine removes only the ability to + * start another workflow; this run can still start and clean up its children. */ export class WorkerRun implements WorkflowRun { /** Settles exactly once with the run's outcome; never rejects. */ readonly result: Promise private settleResolve!: (result: WorkflowResult) => void private settled = false + /** A Result/death/grace outcome atomically won before teardown callbacks. */ + private terminalClaimed = false + /** The first death signal closes worker-message admission and owns failure-time cleanup. */ + private workerDeathObserved = false private cancelReason: string | undefined private graceTimer: NodeJS.Timeout | undefined private readonly worker: Worker @@ -113,19 +148,23 @@ export class WorkerRun implements WorkflowRun { private workerGone = false /** Accepted `child-start` messages — the terminate-path `agentsStarted` (see module doc). */ private hostStarted = 0 - /** Live children by callId; an entry leaves ONLY after its dispose settles (quiescence = empty). */ - private readonly children = new Map() - /** In-flight child disposals by callId — the memo that gives every path (worker RPC, dispose(), reap) ONE shared disposal per child. */ - private readonly childDisposals = new Map>() + /** Published children by callId; an entry leaves only after disposal settles. */ + private readonly children = new Map() + /** Provider starts that have not yet fulfilled or rejected. */ + private readonly pendingStarts = new Set>() /** Started-but-not-ended agents by seq — the pairing ledger the HOST guarantees (see {@link endAgent}). */ private readonly liveAgents = new Map() private readonly quiescenceWaiters: (() => void)[] = [] /** The per-run abort fanout every child start request carries. */ private readonly controller = new AbortController() + /** External start signal and the exact callback installed on it, retained only until first settle/teardown. */ + private inputSignal: AbortSignal | undefined + private inputSignalAbort: (() => void) | undefined private disposed: Promise | undefined constructor( private readonly ctx: Context, + private readonly subagents: SubagentService, readonly id: WorkflowRunId, readonly meta: WorkflowMeta, private readonly parent: Agent, @@ -142,46 +181,50 @@ export class WorkerRun implements WorkflowRun { const { entry, options } = resolveWorkerSpawn(init) this.worker = new Worker(entry, options) this.worker.on('message', (message: WorkerToHostMessage) => { this.onMessage(message) }) - this.worker.on('error', (error) => { this.onWorkerDeath(`workflow worker failed: ${renderThrown(error)}`) }) + this.worker.on('error', (error) => { this.onWorkerDeath(`workflow worker failed: ${renderThrown(error)}`, false) }) /* v8 ignore next -- messageerror: not constructible from the engine's own protocol (every payload is JSON data) */ - this.worker.on('messageerror', (error) => { this.onWorkerDeath(`workflow worker message failed to deserialize: ${renderThrown(error)}`) }) + this.worker.on('messageerror', (error) => { this.onWorkerDeath(`workflow worker message failed to deserialize: ${renderThrown(error)}`, false) }) this.worker.on('exit', (code) => { this.workerGone = true - this.onWorkerDeath(`workflow worker exited before the run settled (exit code ${code})`) + this.onWorkerDeath(`workflow worker exited before the run settled (exit code ${code})`, true) }) if (signal?.aborted) { this.cancel('workflow start signal already aborted') - } else { - signal?.addEventListener('abort', () => { this.cancel('workflow signal aborted') }, { once: true }) + } else if (signal !== undefined) { + const onAbort = (): void => { + this.detachInputSignal() + this.cancel('workflow signal aborted') + } + this.inputSignal = signal + this.inputSignalAbort = onAbort + signal.addEventListener('abort', onAbort, { once: true }) } } /** * Cancel the run: the worker is told (its hooks start throwing and the - * script dies at its next await), every host-side child is cancelled NOW on - * BOTH seam channels — the shared request signal aborts and each registered - * child's explicit `cancel()` is called (the seam leaves a provider free to - * honor either, and a worker wedged in a synchronous spin could not relay - * its own per-child cancel RPCs until far too late) — and the grace timer + * script dies at its next await), the required signal shared by every child + * start is aborted, and the grace timer * arms: a run still unsettled `disposeGraceMs` later force-settles * `cancelled` and its worker is TERMINATED. Idempotent; the first reason * wins. * @param reason - human-readable cause (default `'workflow cancelled'`). */ cancel(reason?: string): void { - // A settled run has nothing left to cancel: without this guard the + // A settled run has nothing left to cancel, and a terminal source claimed + // before its cleanup callbacks must exclude cancellation reentered by one + // of those callbacks. Without the settled guard the // ordinary consumer path (await result, then dispose -> cancel) would arm // a grace timer nothing ever clears, pinning the run and its Worker // closure until the grace expires - a bounded leak per completed run. - if (this.settled || this.cancelReason !== undefined) return + if (this.settled || this.terminalClaimed || this.cancelReason !== undefined) return this.cancelReason = reason ?? 'workflow cancelled' this.post(HostToWorkerType.Cancel, { reason: this.cancelReason }) - this.controller.abort(this.cancelReason) - // The explicit channel is driven host-side, not left to the worker: a - // provider honoring only run.cancel() must not wait on a wedged worker's - // ChildCancel relay (those later RPCs land as idempotent no-ops). - for (const run of this.children.values()) run.cancel(this.cancelReason) + this.abortChildren(this.cancelReason) this.graceTimer = setTimeout(() => { + // Cancellation already owns the race through cancelReason; close the + // terminal boundary explicitly before observer teardown callbacks. + this.terminalClaimed = true // The worker may no longer speak (it is about to be terminated): pair // every stranded start before the run settles, so ends precede // workflow/end. @@ -209,9 +252,21 @@ export class WorkerRun implements WorkflowRun { * @returns resolves when the run's resources are released or abandoned. */ dispose(): Promise { - this.disposed ??= (async () => { + if (this.disposed !== undefined) return this.disposed + // Claim the public transaction BEFORE its body invokes child/provider + // disposal. A raw provider callback can reenter handle.dispose(); it must + // join this promise rather than start a second traversal. + const claimed = Promise.withResolvers() + this.disposed = claimed.promise + void (async () => { + this.detachInputSignal() this.cancel('workflow disposed') - for (const [callId, run] of [...this.children]) void this.disposeChild(callId, run) + // cancel() deliberately becomes a no-op after terminal settlement, but + // disposal still owns every registered child. Reap independently so an + // already-settled workflow cannot wait on child quiescence before it has + // started the surviving children's disposals. On an unsettled run this + // joins the cancel path through the per-call cancellation/disposal gates. + this.reapChildren('workflow disposed') await Promise.race([ (async () => { await this.result @@ -221,13 +276,17 @@ export class WorkerRun implements WorkflowRun { ]) await this.worker.terminate() this.reapChildren('workflow disposed') - })() + })().then( + () => { claimed.resolve(undefined) }, + /* v8 ignore next -- result/quiescence never reject and Worker.terminate is the only external promise */ + (error: unknown) => { claimed.reject(error) }, + ) return this.disposed } /** Post one message to the worker (payload looked up from the tag's map entry), tolerating a thread that is already gone. */ private post(type: T, payload: HostToWorkerPayloads[T]): void { - if (this.workerGone) return + if (this.workerGone || this.workerDeathObserved) return try { this.worker.postMessage({ type, ...payload }) } catch (error: unknown) { @@ -240,6 +299,11 @@ export class WorkerRun implements WorkflowRun { } private onMessage(message: WorkerToHostMessage): void { + // Node may emit `error`, then deliver an already-queued `message`, then + // emit `exit`. The first death signal is the host's logical delivery + // barrier: nothing arriving afterward may create a child, narrate after + // workflow/end, or compete with the chosen outcome. + if (this.workerDeathObserved) return switch (message.type) { case WorkerToHostType.Ready: this.post(HostToWorkerType.Go, {}) @@ -269,9 +333,6 @@ export class WorkerRun implements WorkflowRun { case WorkerToHostType.ChildStart: this.onChildStart(message.callId, message.request) break - case WorkerToHostType.ChildCancel: - this.children.get(message.callId)?.cancel(message.reason) - break case WorkerToHostType.ChildDispose: this.onChildDispose(message.callId) break @@ -284,18 +345,44 @@ export class WorkerRun implements WorkflowRun { } } - private onChildStart(callId: number, request: ChildStartRequest): void { + /** Why a ready provider result may no longer be admitted to the worker. */ + private childAdmissionFailure(): { reason: string; rendered: string } | undefined { if (this.cancelReason !== undefined) { - // The worker's start raced our cancel: refuse — a child must never - // start on an already-aborted signal (a provider subscribing only to - // future abort events would never observe it). - this.post(HostToWorkerType.ChildStartError, { callId, rendered: `workflow run cancelled: ${this.cancelReason}` }) + return { reason: this.cancelReason, rendered: `workflow run cancelled: ${this.cancelReason}` } + } + if (this.workerDeathObserved) { + return { reason: 'workflow worker gone', rendered: 'workflow worker is no longer available' } + } + if (this.terminalClaimed) { + return { reason: 'workflow settled', rendered: 'workflow run already settled' } + } + return undefined + } + + private onChildStart(callId: number, request: ChildStartRequest): void { + const initialFailure = this.childAdmissionFailure() + if (initialFailure !== undefined) { + // Refuse after a terminal boundary: a child must never start on an + // already-aborted signal (a provider subscribing only to future abort + // events would never observe it). + this.post(HostToWorkerType.ChildStartError, { callId, rendered: initialFailure.rendered }) return } this.hostStarted += 1 + const task = this.startChild(callId, request) + this.pendingStarts.add(task) + void task.then( + () => { this.finishPendingStart(task) }, + /* v8 ignore next -- startChild contains provider and cleanup failures */ + () => { this.finishPendingStart(task) }, + ) + } + + /** Await one provider-owned startup transaction and publish only while admitted. */ + private async startChild(callId: number, request: ChildStartRequest): Promise { let run: SubagentRun try { - run = this.ctx.subagents.start(this.provider, { + run = await this.subagents.start(this.provider, { prompt: [{ type: 'text', text: request.prompt }], parent: this.parent, signal: this.controller.signal, @@ -303,36 +390,63 @@ export class WorkerRun implements WorkflowRun { ...request.model !== undefined ? { agentOptions: { model: request.model } } : {}, }) } catch (error: unknown) { - this.post(HostToWorkerType.ChildStartError, { callId, rendered: renderThrown(error) }) + const failure = this.childAdmissionFailure() + this.post(HostToWorkerType.ChildStartError, { + callId, + rendered: failure?.rendered ?? renderThrown(error), + }) return } - this.children.set(callId, run) - this.post(HostToWorkerType.ChildStarted, { callId, childId: run.id }) - run.result.then( + const failure = this.childAdmissionFailure() + if (failure !== undefined) { + this.post(HostToWorkerType.ChildStartError, { callId, rendered: failure.rendered }) + try { + await run.dispose() + } catch (error: unknown) { + this.ctx.logger.warn(`workflow-workerthread: refused child dispose failed: ${renderThrown(error)}`) + } + return + } + + const record: ChildRecord = { run } + this.children.set(callId, record) + // Attach result forwarding before publishing the child handle. Because the + // callback itself runs in a later microtask, ChildStarted is still posted + // first even for an already-settled scripted provider. + const forwardResult = run.result.then<() => void, () => void>( (result) => { - this.post(HostToWorkerType.ChildSettled, { - callId, - result: { + try { + const snapshot = snapshotJsonValue({ output: result.output, ...result.structured !== undefined ? { structured: result.structured } : {}, stopReason: result.stopReason, - }, - }) + }) + if (snapshot === undefined) throw new TypeError('child result is not losslessly JSON-serializable') + return () => { this.post(HostToWorkerType.ChildSettled, { callId, result: snapshot }) } + } catch (error: unknown) { + const rendered = `workflow child result could not cross the worker boundary: ${renderThrown(error)}` + return () => { this.post(HostToWorkerType.ChildFailed, { callId, rendered }) } + } + }, + (error: unknown) => { + const rendered = renderThrown(error) + return () => { this.post(HostToWorkerType.ChildFailed, { callId, rendered }) } }, - (error: unknown) => { this.post(HostToWorkerType.ChildFailed, { callId, rendered: renderThrown(error) }) }, ) + this.post(HostToWorkerType.ChildStarted, { callId, childId: run.id }) + void forwardResult.then((forward) => { forward() }) } private onChildDispose(callId: number): void { - const run = this.children.get(callId) - if (run === undefined) { + const record = this.children.get(callId) + if (record === undefined) { // Already disposed host-side (a dispose() drive or a death reap beat // the RPC) — the ack is still owed (the worker-side wrapper awaits it). this.post(HostToWorkerType.ChildDisposed, { callId }) return } // disposeChild never rejects (containment is inside), so the ack always follows. - void this.disposeChild(callId, run).then(() => { this.post(HostToWorkerType.ChildDisposed, { callId }) }) + void this.disposeChild(callId, record).then(() => { this.post(HostToWorkerType.ChildDisposed, { callId }) }) } /** @@ -344,54 +458,79 @@ export class WorkerRun implements WorkflowRun { * not supposed to reject, but a backend that does anyway must not break * quiescence): logged, and the child still leaves the registry. * @param callId - the child's registry key. - * @param run - the registered child (the caller looked it up). + * @param record - the registered child (the caller looked it up). * @returns resolves when the disposal settled either way; never rejects. */ - private disposeChild(callId: number, run: SubagentRun): Promise { - let disposal = this.childDisposals.get(callId) - if (disposal === undefined) { - disposal = run.dispose().then( - () => { this.finishChild(callId) }, - (error: unknown) => { - this.ctx.logger.warn(`workflow-workerthread: child dispose failed: ${renderThrown(error)}`) - this.finishChild(callId) - }, - ) - this.childDisposals.set(callId, disposal) - } - return disposal + private disposeChild(callId: number, record: ChildRecord): Promise { + if (record.disposal !== undefined) return record.disposal + record.disposal = Promise.resolve() + .then(() => record.run.dispose()) + .catch((error: unknown) => { + this.ctx.logger.warn(`workflow-workerthread: child dispose failed: ${renderThrown(error)}`) + }) + .then(() => { this.finishChild(callId) }) + return record.disposal } - /** Drop a child from the registry (and its disposal memo), releasing quiescence waiters at zero. */ + /** Drop a child record and release quiescence waiters when all work ends. */ private finishChild(callId: number): void { this.children.delete(callId) - this.childDisposals.delete(callId) - if (this.children.size === 0) { - for (const waiter of this.quiescenceWaiters.splice(0)) waiter() - } + this.notifyChildQuiescence() } - /** Resolves once the child registry is empty (every disposal settled). */ + /** Retire one provider startup transaction. */ + private finishPendingStart(task: Promise): void { + this.pendingStarts.delete(task) + this.notifyChildQuiescence() + } + + /** Release waiters only after both pending starts and published children end. */ + private notifyChildQuiescence(): void { + if (this.children.size !== 0 || this.pendingStarts.size !== 0) return + for (const waiter of this.quiescenceWaiters.splice(0)) waiter() + } + + /** Resolves once every pending start and published child has reached quiescence. */ private childQuiescence(): Promise { - if (this.children.size === 0) return Promise.resolve() + if (this.children.size === 0 && this.pendingStarts.size === 0) return Promise.resolve() return new Promise((resolve) => { this.quiescenceWaiters.push(resolve) }) } /** Abort + dispose every registered child (worker death / final teardown); disposal is contained, not awaited. */ private reapChildren(reason: string): void { - this.controller.abort(this.cancelReason ?? reason) - for (const [callId, run] of [...this.children]) { - run.cancel(this.cancelReason ?? reason) - void this.disposeChild(callId, run) + this.abortChildren(this.cancelReason ?? reason) + for (const [callId, record] of [...this.children]) { + void this.disposeChild(callId, record) } } + /** Abort the one canonical signal shared by pending and published children. */ + private abortChildren(reason: string): void { + if (!this.controller.signal.aborted) this.controller.abort(reason) + } + private onResult(result: WorkflowResult): void { - // The worker's settle-reap already child-cancel()s every stray; this - // abort fires the seam signal too, for providers that only honor the - // request signal (both channels, on every path). - if (this.cancelReason === undefined) this.controller.abort('workflow settled') - if (this.cancelReason !== undefined && result.stopReason !== 'cancelled') { + // The owned worker session sends one Result. Keep a late duplicate or a + // Result queued behind another terminal source completely side-effect-free. + if (this.terminalClaimed) return + // First-wins is decided when the Result message reaches the host. If no + // external cancellation was already in flight, this result won. Reaping a + // stray child below may synchronously reenter cancel() through provider + // callbacks, but that internal post-result cleanup must not retroactively + // rewrite the worker result that arrived first. + const cancellationWasRequested = this.cancelReason !== undefined + // Claim before settlement cleanup invokes provider disposal. Once Result + // won, a later cancellation cannot rewrite it. + this.terminalClaimed = true + // Abort pending starts and begin disposing published children before the + // workflow becomes externally settled. Cleanup remains independently + // tracked by childQuiescence and the holder's dispose(). + this.reapChildren('workflow settled') + if (!cancellationWasRequested) { + this.settleResult(result) + return + } + if (result.stopReason !== 'cancelled') { // The script settled while our cancel was crossing the thread boundary // — the seam-visible result had NOT settled when cancellation was // requested, so report cancelled (the vm drive()'s post-settle check, @@ -402,21 +541,39 @@ export class WorkerRun implements WorkflowRun { this.settleResult(result) } - /** An unexpected worker death (or the expected exit after termination). */ - private onWorkerDeath(message: string): void { - // Whatever the worker left behind must not leak — abort + dispose it all. - if (this.children.size > 0) this.reapChildren('workflow worker gone') - // The thread is gone: no more worker-authored agent-ends can arrive — - // pair every stranded start (a start that crossed between the grace - // force-settle and this exit included) before the run settles. - this.endStrandedAgents() - // settleResult no-ops on an already-settled run (the expected exit after - // a dispose's terminate lands here too). - if (this.cancelReason !== undefined) { - this.settleResult(this.cancelledResult(this.hostStarted)) - return + /** Process an error/messageerror/exit signal; `exit` also performs the final disposal sweep. */ + private onWorkerDeath(message: string, isExit: boolean): void { + if (!this.workerDeathObserved) { + // Close message admission BEFORE cleanup callbacks: Node can deliver a + // message queued before the crash after its `error` event. Treating the + // first death signal as a logical barrier prevents that late message + // from creating work or narrating after workflow/end. + this.workerDeathObserved = true + const outcomeWasClaimed = this.terminalClaimed + const cancellationWasRequested = this.cancelReason !== undefined + // When death is itself the terminal source, claim BEFORE child reap or + // synthesized observer callbacks. Either can reenter cancel(); a death + // that arrived first remains an error, while a cancellation already + // accepted before death remains cancelled. If Result/grace already won, + // preserve it while still performing prompt failure-time cleanup. + if (!outcomeWasClaimed) this.terminalClaimed = true + if (this.children.size > 0 || this.pendingStarts.size > 0) this.reapChildren('workflow worker gone') + this.endStrandedAgents() + if (!outcomeWasClaimed) { + if (cancellationWasRequested) { + this.settleResult(this.cancelledResult(this.hostStarted)) + } else { + this.settleResult({ value: null, stopReason: 'error', error: message, agentsStarted: this.hostStarted }) + } + } } - this.settleResult({ value: null, stopReason: 'error', error: message, agentsStarted: this.hostStarted }) + if (!isExit) return + // `error` is not Node's physical delivery barrier: a queued message may + // precede `exit`. Admission is already closed, so this final sweep only + // joins/starts disposal for registry survivors; it deliberately does not + // repeat explicit provider cancellation. + for (const [callId, record] of [...this.children]) void this.disposeChild(callId, record) + this.endStrandedAgents() } /** @@ -435,11 +592,14 @@ export class WorkerRun implements WorkflowRun { /** * Synthesize the missing `agent-end` for every started-but-unpaired agent, * outcome `'cancelled'`: the reap cancels every child, and a real - * settlement racing the force-settle loses to the cancellation — the same - * first-wins override {@link onResult} applies to the run's own result. + * settlement racing the force-settle loses to that already-started external + * cancellation. The atomic terminal boundaries in {@link onResult} and + * {@link onWorkerDeath} deliberately exclude teardown callbacks as contenders. * Called where the worker can no longer speak (the grace force-settle, - * worker death), BEFORE settleResult, so the paired ends reach observers - * before `workflow/end`. + * worker death, physical exit). When grace/death is the terminal source it + * runs before settleResult, so already-known pairs precede `workflow/end`; + * after an earlier Result, exit cleanup may close a survivor afterward. + * The ledger preserves exactly-once pairing in both orders. */ private endStrandedAgents(): void { for (const info of [...this.liveAgents.values()]) { @@ -455,10 +615,25 @@ export class WorkerRun implements WorkflowRun { return { value: null, stopReason: 'cancelled', error: `workflow run cancelled: ${reason}`, agentsStarted } } - /** First settle wins; disarms the grace timer. */ + /** Remove the exact abort callback installed on the caller's start signal. */ + private detachInputSignal(): void { + const signal = this.inputSignal + const onAbort = this.inputSignalAbort + if (signal === undefined || onAbort === undefined) return + this.inputSignal = undefined + this.inputSignalAbort = undefined + signal.removeEventListener('abort', onAbort) + } + + /** First settle wins; disarms the grace timer and releases the caller signal. */ private settleResult(result: WorkflowResult): void { + // Every current terminal source claims ownership before calling here; keep + // the fallback local so a future caller cannot resolve twice. + /* v8 ignore next -- defensive fallback outside the claimed state machine */ if (this.settled) return + this.terminalClaimed = true this.settled = true + this.detachInputSignal() clearTimeout(this.graceTimer) this.settleResolve(result) } diff --git a/packages/workflow/workflow-workerthread/src/index.ts b/packages/workflow/workflow-workerthread/src/index.ts index e0b8caf3fb..a1f5b47ccc 100644 --- a/packages/workflow/workflow-workerthread/src/index.ts +++ b/packages/workflow/workflow-workerthread/src/index.ts @@ -151,9 +151,7 @@ export class WorkerWorkflowEngine extends WorkflowService { const meta = validateMeta(request.meta) assertBodyParses(request.script, meta.name) const id = WorkflowRunId(randomUUID()) - // The event payloads and the run handle get SEPARATE meta clones: a - // listener mutating its snapshot must not corrupt the holder's view. - const info: WorkflowRunInfo = { id, meta: structuredClone(meta) } + const info: WorkflowRunInfo = { id, meta } const limits: WorkerLimits = { maxConcurrentAgents: this.config.maxConcurrentAgents === 0 ? Math.min(16, Math.max(1, availableParallelism() - 2)) @@ -168,10 +166,19 @@ export class WorkerWorkflowEngine extends WorkflowService { ...request.args !== undefined ? { args: request.args } : {}, limits, } + // Capture the dependency while this service call is still traced through + // the start() holder. Cordis strips the engine-provider shadow when it + // returns the SubagentService handle, so an already-returned run can keep + // starting children after an engine HMR unload removes ctx.workflows. + // Re-resolving `this.ctx.subagents` later from WorkerRun would instead walk + // the now-inactive engine fiber and break the seam's holder-owned lifetime. + const runCtx = this.ctx + const subagents = runCtx.subagents const workerRun = new WorkerRun( - this.ctx, + runCtx, + subagents, id, - structuredClone(meta), + meta, request.parent, init, this.config.provider, diff --git a/packages/workflow/workflow-workerthread/src/protocol.ts b/packages/workflow/workflow-workerthread/src/protocol.ts index ed6b54950a..70edc9a713 100644 --- a/packages/workflow/workflow-workerthread/src/protocol.ts +++ b/packages/workflow/workflow-workerthread/src/protocol.ts @@ -33,8 +33,6 @@ export enum WorkerToHostType { AgentEnd = 'agent-end', /** Child RPC: start a child on the host (answered by ChildStarted or ChildStartError). */ ChildStart = 'child-start', - /** Child RPC: cancel a started child (fire-and-forget). */ - ChildCancel = 'child-cancel', /** Child RPC: dispose a started child (answered by ChildDisposed). */ ChildDispose = 'child-dispose', /** The run's single terminal result. */ @@ -55,8 +53,6 @@ export interface WorkerToHostPayloads { [WorkerToHostType.AgentEnd]: { info: WorkflowAgentEndInfo } /** The RPC correlation id and the prompt plus validated options. */ [WorkerToHostType.ChildStart]: { callId: number; request: ChildStartRequest } - /** The RPC correlation id and the cancel reason (undefined = unspecified). */ - [WorkerToHostType.ChildCancel]: { callId: number; reason: string | undefined } /** The RPC correlation id of the child to dispose. */ [WorkerToHostType.ChildDispose]: { callId: number } /** The run's terminal outcome. */ @@ -69,9 +65,9 @@ export enum HostToWorkerType { Go = 'go', /** Cancel the run: hooks start throwing and the script dies at its next await. */ Cancel = 'cancel', - /** Child RPC reply: the start succeeded (exactly one of ChildStarted/ChildStartError per ChildStart). */ + /** Child RPC reply: the provider fulfilled with a ready run (exactly one start reply per ChildStart). */ ChildStarted = 'child-started', - /** Child RPC reply: the start was refused or threw. */ + /** Child RPC reply: the provider's asynchronous start failed. */ ChildStartError = 'child-start-error', /** Child RPC: a started child's result RESOLVED (its JSON projection). */ ChildSettled = 'child-settled', @@ -112,4 +108,3 @@ export type WorkerToHostMessage = */ export type HostToWorkerMessage = { [K in T]: { type: K } & HostToWorkerPayloads[K] }[T] - diff --git a/packages/workflow/workflow-workerthread/src/runtime.ts b/packages/workflow/workflow-workerthread/src/runtime.ts index 645a61b6c4..94282cb173 100644 --- a/packages/workflow/workflow-workerthread/src/runtime.ts +++ b/packages/workflow/workflow-workerthread/src/runtime.ts @@ -19,8 +19,9 @@ * (a benign-bug guard; the postMessage clone already isolated the caller). * * Failure discipline: fatal {@link WorkflowError}s (bad hook arguments, - * unsupported options/schemas, tripped caps, host start refusals and child - * result rejections, cancellation) ALWAYS propagate through + * unsupported options/schemas, tripped caps, synchronous start refusal, + * provider-start failure, ready-child result rejection, and + * cancellation) ALWAYS propagate through * `parallel`/`pipeline` — recognized by `instanceof` against this realm's * class, which a script inside the vm context cannot forge — and the per-item * `null` is reserved for child-run failures and ordinary in-stage script @@ -82,7 +83,8 @@ function defaultLabel(prompt: string): string { /** * One live script execution inside the worker. Constructed per run by the * session; `drive()` is called exactly once and NEVER rejects — every failure - * becomes a {@link WorkflowResult} with a non-`completed` stop reason. + * becomes a {@link WorkflowResult} with a non-`completed` stop reason. The + * host owns cancellation and cleanup of any dropped child work. */ export class WorkflowExecution { /** 1-based count of `agent()` calls started (the `agentsStarted` result field). */ @@ -91,7 +93,6 @@ export class WorkflowExecution { private readonly slotWaiters: { resolve(): void; reject(error: unknown): void }[] = [] private cancelReason: string | undefined private cancelError: WorkflowError | undefined - private readonly controller = new AbortController() private currentPhase: string | undefined private readonly context: vm.Context private readonly compiled: vm.Script @@ -127,11 +128,8 @@ export class WorkflowExecution { pipeline: (items: unknown, ...stages: unknown[]) => this.contain(this.pipeline(items, stages)), phase: (title: unknown) => { this.phase(title) }, log: (message: unknown) => { this.log(message) }, - // Cloned once: a script scribbling on args must not mutate the - // session's init object (a benign-bug guard; args is plain JSON by the - // seam contract and already crossed one structured clone as workerData, - // so this clone is total). - args: args === undefined ? undefined : structuredClone(args), + // workerData already performed the real cross-thread structured clone. + args, } for (const [key, value] of Object.entries(globals)) { // Data properties on the contextified global; frozen shape not required — @@ -162,21 +160,18 @@ export class WorkflowExecution { } /** - * Cancel the run: in-flight children get a cancel RPC (the shared abort - * fanout), waiting `agent()` slots reject, and every future hook call + * Cancel the run: waiting `agent()` slots reject and every future hook call * throws `CANCELLED` — the script dies at its next await. A script that * never settles anyway (parked on a promise no hook owns) is the HOST's * problem: its grace timer force-settles the run and terminates the * worker. Idempotent; the first reason wins. - * @param reason - human-readable cause, carried on the CANCELLED error and - * into child cancel RPCs. Required: every caller (the session's cancel - * message, drive()'s settle-reap) has a concrete reason. + * @param reason - human-readable cause carried on the CANCELLED error. The + * host independently aborts the required signal shared by every child. */ cancel(reason: string): void { if (this.cancelReason !== undefined) return this.cancelReason = reason this.cancelError = new WorkflowError(`workflow run cancelled: ${this.cancelReason}`, 'CANCELLED') - this.controller.abort(this.cancelReason) for (const waiter of this.slotWaiters.splice(0)) waiter.reject(this.cancelledError()) } @@ -184,8 +179,8 @@ export class WorkflowExecution { * Run the script to settlement. Resolves — never rejects — with the run's * {@link WorkflowResult}: the materialized return value on `completed`, the * failure message on `error`, and `cancelled` when the script died of - * cancellation. After settlement, any stray children a script fired without - * awaiting are cancelled (their `agent()` wrappers dispose them via RPC). + * cancellation. This method only chooses the result; the session publishes + * it and the host owns terminal child cancellation. * @returns the settled outcome — this promise NEVER rejects (the seam's * `result`-never-rejects contract); every failure maps to a variant. */ @@ -213,12 +208,6 @@ export class WorkflowExecution { // cannot throw — drive() resolving is the `result` never-rejects seam // contract. return { value: null, stopReason: 'error', error: renderThrown(error), agentsStarted: this.started } - } finally { - // Reap strays: a script that fired agent() calls without awaiting them - // leaves live children behind after settlement — cancel them all. (The - // per-call wrappers dispose each child; the contain() consumer keeps - // their rejections from going unhandled.) - if (this.cancelReason === undefined) this.cancel('workflow settled') } } @@ -327,17 +316,11 @@ export class WorkflowExecution { // wind the fresh child down instead of leaving it live behind a dead // script. if (this.isCancelled()) { - run.cancel(this.cancelReason) await run.dispose() throw this.cancelledError() } const info: WorkflowAgentInfo = { seq, label, ...phase !== undefined ? { phase } : {}, childId: AgentId(run.id) } this.observer.agentStart(info) - // Cancellation reaches the child through an explicit cancel RPC per - // child (the host also aborts its own per-run signal, but the seam - // leaves a provider free to honor either channel, so both are driven). - const onAbort = (): void => { run.cancel(this.cancelReason) } - this.controller.signal.addEventListener('abort', onAbort, { once: true }) try { let result try { @@ -378,7 +361,6 @@ export class WorkflowExecution { this.observer.agentEnd({ ...info, outcome: 'failed' }) return null } finally { - this.controller.signal.removeEventListener('abort', onAbort) await run.dispose() } } finally { diff --git a/packages/workflow/workflow-workerthread/src/session.ts b/packages/workflow/workflow-workerthread/src/session.ts index 718b8ac54d..671a9429ae 100644 --- a/packages/workflow/workflow-workerthread/src/session.ts +++ b/packages/workflow/workflow-workerthread/src/session.ts @@ -59,10 +59,6 @@ class RpcChildHandle implements ChildHandle { this.result = entry.settled.promise } - cancel(reason?: string): void { - this.post(WorkerToHostType.ChildCancel, { callId: this.callId, reason }) - } - dispose(): Promise { this.post(WorkerToHostType.ChildDispose, { callId: this.callId }) return this.entry.disposed.promise @@ -71,7 +67,7 @@ class RpcChildHandle implements ChildHandle { /** * The worker-side child-RPC bridge ({@link ChildPort}): allocates callIds, - * posts the start/cancel/dispose RPCs, and owns the per-call pending + * posts the start/dispose RPCs, and owns the per-call pending * book-keeping the session's message handler settles via the `onChild*` * entry points. */ @@ -89,24 +85,26 @@ class ChildRpcBridge implements ChildPort { settled: Promise.withResolvers(), disposed: Promise.withResolvers(), } - // Containment: when the start is refused (or the run torn down) the - // settled promise may never gain a consumer — it must not surface as an - // unhandled rejection and kill the worker. - entry.settled.promise.catch(() => { /* consumed: unconsumed child settlement after a refused start */ }) + // Containment: when asynchronous provider start fails (or + // the run is torn down), the settled promise may never gain a consumer — + // it must not surface as an unhandled rejection and kill the worker. + entry.settled.promise.catch(() => { /* consumed: unconsumed child settlement after failed start */ }) this.pending.set(callId, entry) this.post(WorkerToHostType.ChildStart, { callId, request }) const childId = await entry.started.promise return new RpcChildHandle(this.post, callId, entry, childId) } - /** The host started the child; releases the `startAgent` await. */ + /** The host established a ready child; releases the `startAgent` await. */ onChildStarted(callId: number, childId: string): void { this.pending.get(callId)?.started.resolve(childId) } - /** The host refused the start; `startAgent` rejects with the rendered cause. */ + /** Asynchronous provider start failed; reject and retire the pending RPC. */ onChildStartError(callId: number, rendered: string): void { - this.pending.get(callId)?.started.reject(new Error(rendered)) + const entry = this.pending.get(callId) + this.pending.delete(callId) + entry?.started.reject(new Error(rendered)) } /** The child's terminal result arrived. */ diff --git a/packages/workflow/workflow-workerthread/src/types.ts b/packages/workflow/workflow-workerthread/src/types.ts index a80b126a2d..2f29dd8137 100644 --- a/packages/workflow/workflow-workerthread/src/types.ts +++ b/packages/workflow/workflow-workerthread/src/types.ts @@ -77,8 +77,6 @@ export interface ChildHandle { * failed for its own reasons resolves with a non-`completed` stop reason. */ readonly result: Promise - /** Ask the host to cancel the child (fire-and-forget). */ - cancel(reason?: string): void /** Ask the host to dispose the child; resolves on the host's ack. */ dispose(): Promise } @@ -91,7 +89,8 @@ export interface ChildPort { /** * Start one child agent on the host (the `agent()` hook's start half). * @param request - the prompt and validated options. - * @returns the child handle; rejects when the host refuses the start. + * @returns the ready child handle; rejects when synchronous start or the + * provider's asynchronous start fails. */ startAgent(request: ChildStartRequest): Promise } diff --git a/packages/workflow/workflow-workerthread/tests/integration.spec.ts b/packages/workflow/workflow-workerthread/tests/integration.spec.ts index d6e8237804..0e1727f877 100644 --- a/packages/workflow/workflow-workerthread/tests/integration.spec.ts +++ b/packages/workflow/workflow-workerthread/tests/integration.spec.ts @@ -48,7 +48,12 @@ describe('dsh-workflow-workerthread over the real in-process stack', () => { toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { verdict: 'real', confidence: 0.9 }), ]) const childIds: string[] = [] - ctx.on('workflow/agent-start', (_info, agent) => { childIds.push(agent.childId) }) + ctx.on('workflow/agent-start', (_info, agent) => { + // The workflow bridge must await asynchronous provider start: an observer + // sees the real spawn child already published, never a reserved id. + expect(ctx.agents.get(agent.childId)).toBeDefined() + childIds.push(agent.childId) + }) const run = ctx.workflows.start({ meta: { name: 'integration', description: 'plain + structured children' }, script: `phase('Read') diff --git a/packages/workflow/workflow-workerthread/tests/session.spec.ts b/packages/workflow/workflow-workerthread/tests/session.spec.ts index 00051ad56a..5fc85ce647 100644 --- a/packages/workflow/workflow-workerthread/tests/session.spec.ts +++ b/packages/workflow/workflow-workerthread/tests/session.spec.ts @@ -212,7 +212,7 @@ describe('runWorkerSession over an in-process MessageChannel', () => { host.close() }) - it('cancel mid-run: in-flight children get cancel RPCs, hooks throw at entry, the run reports cancelled', async () => { + it('cancel mid-run: hooks throw at entry and the run reports cancelled', async () => { const host = fakeHost() void runWorkerSession(host.port, init(` phase('before') @@ -232,7 +232,6 @@ describe('runWorkerSession over an in-process MessageChannel', () => { const result = await host.result() expect(result.stopReason).toBe('cancelled') expect(result.error).toContain('stop everything') - expect(host.ofType(WorkerToHostType.ChildCancel).map(m => m.callId)).toContain(callId) expect(host.ofType(WorkerToHostType.AgentEnd)[0]!.info.outcome).toBe('cancelled') // No post-cancel narration left the runtime (the hooks threw at entry). expect(host.ofType(WorkerToHostType.Phase).map(m => m.title)).toEqual(['before']) @@ -433,7 +432,7 @@ describe('runWorkerSession over an in-process MessageChannel', () => { host.close() }) - it('a cancel landing DURING the start round-trip winds the fresh child down (cancel + dispose) and dies cancelled', async () => { + it('a cancel landing DURING the start round-trip disposes the fresh child and dies cancelled', async () => { const host = fakeHost({ manual: true }) void runWorkerSession(host.port, init("return await agent('p')")) await vi.waitFor(() => { expect(host.ofType(WorkerToHostType.ChildStart).length).toBe(1) }) @@ -447,7 +446,6 @@ describe('runWorkerSession over an in-process MessageChannel', () => { const result = await host.result() expect(result.stopReason).toBe('cancelled') await vi.waitFor(() => { - expect(host.ofType(WorkerToHostType.ChildCancel).map(m => m.callId)).toContain(callId) expect(host.ofType(WorkerToHostType.ChildDispose).map(m => m.callId)).toContain(callId) }) // The child never became an agent-start: it was wound down pre-lifecycle. diff --git a/packages/workflow/workflow-workerthread/tests/source-worker.compat.spec.ts b/packages/workflow/workflow-workerthread/tests/source-worker.compat.spec.ts new file mode 100644 index 0000000000..7f34396d52 --- /dev/null +++ b/packages/workflow/workflow-workerthread/tests/source-worker.compat.spec.ts @@ -0,0 +1,38 @@ +/** + * Keyless runtime smoke for the source-mode workflow worker. The Node + * compatibility matrix runs this WHOLE file, so renaming or removing its test + * cannot turn the runtime proof into a successful zero-match filter. + */ + +import { expect, it, vi } from 'vitest' +import { Context } from 'cordis' +import { AgentId } from '@deepseek-ai/dsh-agent' +import type { Agent } from '@deepseek-ai/dsh-agent' +import SubagentService from '@deepseek-ai/dsh-subagent' +import WorkerWorkflowEngine from '../src/index.ts' + +// A fresh thread compiles the source runtime. Leave contention headroom on +// shared CI runners without weakening any engine-level timeout assertion. +vi.setConfig({ testTimeout: 30_000 }) + +it('runs the default config through the source worker', async () => { + const ctx = new Context() + const subagents = await ctx.plugin(SubagentService) + const engine = await ctx.plugin(WorkerWorkflowEngine, {}) + const parent = { id: AgentId('workflow-compat-parent'), options: {} } as unknown as Agent + try { + const run = ctx.workflows.start({ + script: 'return 6 * 7', + meta: { name: 'source-worker-compat', description: 'exercise the unbuilt worker entry' }, + parent, + }) + try { + await expect(run.result).resolves.toMatchObject({ value: 42, stopReason: 'completed', agentsStarted: 0 }) + } finally { + await run.dispose() + } + } finally { + await engine.dispose() + await subagents.dispose() + } +}) diff --git a/packages/workflow/workflow-workerthread/tests/workflow-workerthread.e2e.ts b/packages/workflow/workflow-workerthread/tests/workflow-workerthread.e2e.ts index 6353868a02..1d6f61432a 100644 --- a/packages/workflow/workflow-workerthread/tests/workflow-workerthread.e2e.ts +++ b/packages/workflow/workflow-workerthread/tests/workflow-workerthread.e2e.ts @@ -61,7 +61,7 @@ return { prose, containsFour: judged === null ? null : judged.containsFour }` describe.skipIf(!process.env.DEEPSEEK_API_KEY)('worker workflow engine with-key e2e', () => { it('runs a two-phase script in a worker thread over real children, one through the structured runtime', async () => { ctx = await harness() - const parentHandle = ctx.agents.create({ + const parentHandle = await ctx.agents.create({ agentId: AgentId('wf-worker-e2e-parent'), sessionId: 'wf-worker-e2e-session' as never, agentOptions: { model: 'deepseek-v4-flash' }, diff --git a/packages/workflow/workflow-workerthread/tests/workflow-workerthread.spec.ts b/packages/workflow/workflow-workerthread/tests/workflow-workerthread.spec.ts index ecaeed61f6..4ad00ee02f 100644 --- a/packages/workflow/workflow-workerthread/tests/workflow-workerthread.spec.ts +++ b/packages/workflow/workflow-workerthread/tests/workflow-workerthread.spec.ts @@ -1,5 +1,6 @@ import { describe, expect, it, vi } from 'vitest' import { fileURLToPath } from 'node:url' +import type { Worker } from 'node:worker_threads' import { Context } from 'cordis' import Loader from '@cordisjs/plugin-loader' import { AgentId } from '@deepseek-ai/dsh-agent' @@ -8,20 +9,51 @@ import SubagentService from '@deepseek-ai/dsh-subagent' import type { SubagentCapabilities, SubagentProvider, SubagentResult, SubagentRun, SubagentStartRequest } from '@deepseek-ai/dsh-subagent' import type { WorkflowMeta, WorkflowResult, WorkflowResultInfo, WorkflowRunInfo } from '@deepseek-ai/dsh-workflow' import * as workerEngineModule from '../src/index.ts' -import WorkerWorkflowEngine, { type Config } from '../src/index.ts' +import WorkerWorkflowEngine, { HostToWorkerType, WorkerToHostType, type Config } from '../src/index.ts' /** A minimal parent stand-in: the engine only threads it through to the provider. */ function fakeParent(): Agent { return { id: AgentId('workflow-parent'), options: {} } as unknown as Agent } +// Worker-thread startup is CPU-bound (a fresh thread compiles the runtime on +// every start): on a contended CI runner it regularly blows past vitest's 5s +// default test timeout, observed repeatedly on the coverage lane. +vi.setConfig({ testTimeout: 30_000 }) + +/** + * `vi.waitFor` with a contention-proof default timeout: the 1s default + * flaked repeatedly on the CI coverage lane, where worker-thread cold start + * (CPU-bound — a fresh thread compiles the runtime) competes with three + * sibling vitest workers for CPU. The 10s default is for exactly those + * races — waiting for a worker to start, run its first script line, or + * deliver an async child-registration message to the host. It is NOT for a + * wait that asserts the HOST reacted PROMPTLY to something that already + * happened (a settled result, an observed worker death): those keep an + * explicit tight override below, or the generous default would silently + * accept a multi-second regression in host-side reap latency as passing + * (proven by injecting a 6s delay into one such reap and watching the + * un-overridden version of this helper still pass in ~6s). + * @param assertion - retried until it stops throwing or the timeout elapses. + * @param timeout - override for a wait that must stay deliberately tight. + * @returns resolves when the assertion passes. + */ +function waitFor(assertion: () => void, timeout = 10_000): Promise { + return vi.waitFor(assertion, { timeout, interval: 50 }) +} + /** The vm-context escape hatch, spelled once: real Worker tests use it to make the WORKER misbehave. */ const ESCAPE = "globalThis.constructor.constructor('return process')()" /** One controllable child run: the test (or auto mode) settles it. */ interface ControlledRun { request: SubagentStartRequest + /** Fulfill the provider's async start with a ready child. */ + publish(): void + /** Reject the provider's async start before ownership transfer. */ + rejectStart(error: unknown): void settle(result: SubagentResult): void + rejectResult(error: unknown): void cancelled: string | undefined disposed: boolean disposeCalls: number @@ -34,7 +66,7 @@ interface ControlledRun { * the request signal fires, like the real in-process backends. */ class StubProvider implements SubagentProvider { - readonly capabilities: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: true } + readonly capabilities: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: true, persona: false } readonly inheritsParentContext = false readonly runs: ControlledRun[] = [] @@ -42,26 +74,51 @@ class StubProvider implements SubagentProvider { readonly name: string, private readonly reply?: (request: SubagentStartRequest, index: number) => SubagentResult, private readonly disposeDelayMs = 0, + private readonly deferStart = false, + private readonly onAbortString?: (reason: string | undefined, index: number) => void, + private readonly onSignalAbort?: (reason: unknown, index: number) => void, ) {} - start(request: SubagentStartRequest): SubagentRun { - let settle!: (result: SubagentResult) => void - const result = new Promise((resolve) => { settle = resolve }) - const controlled: ControlledRun = { request, settle, cancelled: undefined, disposed: false, disposeCalls: 0 } + async start(request: SubagentStartRequest): Promise { + const startGate = Promise.withResolvers() + const terminal = Promise.withResolvers() + terminal.promise.catch(() => { /* provider owns early settlement until publication */ }) + let published = false + const controlled: ControlledRun = { + request, + publish: () => { published = true; startGate.resolve(undefined) }, + rejectStart: (error) => { startGate.reject(error) }, + settle: (result) => { terminal.resolve(result) }, + rejectResult: (error) => { terminal.reject(error) }, + cancelled: undefined, + disposed: false, + disposeCalls: 0, + } this.runs.push(controlled) const index = this.runs.length - 1 - request.signal?.addEventListener('abort', () => { settle({ output: [], stopReason: 'aborted' }) }, { once: true }) + request.signal.addEventListener('abort', () => { + controlled.cancelled = String(request.signal.reason ?? 'cancelled') + this.onAbortString?.(String(request.signal.reason ?? 'cancelled'), index) + this.onSignalAbort?.(request.signal.reason, index) + if (published) terminal.resolve({ output: [], stopReason: 'aborted' }) + else startGate.reject(new Error('child start aborted before publication')) + }, { once: true }) + if (!this.deferStart) controlled.publish() if (this.reply) { const reply = this.reply - queueMicrotask(() => { settle(reply(request, index)) }) + queueMicrotask(() => { terminal.resolve(reply(request, index)) }) } + try { + await startGate.promise + } catch (error: unknown) { + controlled.disposeCalls += 1 + controlled.disposed = true + throw error + } + if (request.signal.aborted) throw new Error('child start aborted before publication') return { id: AgentId(`stub-child-${index}`), - result, - cancel: (reason?: string) => { - controlled.cancelled = reason ?? 'cancelled' - settle({ output: [], stopReason: 'aborted' }) - }, + result: terminal.promise, dispose: () => { controlled.disposeCalls += 1 if (this.disposeDelayMs === 0) { @@ -89,6 +146,9 @@ interface SetupOptions { reply?: (request: SubagentStartRequest, index: number) => SubagentResult manual?: boolean disposeDelayMs?: number + deferStart?: boolean + onChildAbortString?: (reason: string | undefined, index: number) => void + onChildSignalAbort?: (reason: unknown, index: number) => void } async function setup(options?: SetupOptions) { @@ -98,13 +158,16 @@ async function setup(options?: SetupOptions) { 'stub', options?.manual ? undefined : options?.reply ?? (() => text('stub reply')), options?.disposeDelayMs ?? 0, + options?.deferStart ?? false, + options?.onChildAbortString, + options?.onChildSignalAbort, ) ctx.subagents.registerProvider(provider) // A fixed concurrency ceiling: the auto-resolved default is machine-derived // (cores - 2, floored at 1), so tests that expect N children in flight // would wedge on small CI runners. - await ctx.plugin(WorkerWorkflowEngine, { provider: 'stub', maxConcurrentAgents: 8, ...options?.config }) - return { ctx, provider, parent: fakeParent() } + const engineFiber = await ctx.plugin(WorkerWorkflowEngine, { provider: 'stub', maxConcurrentAgents: 8, ...options?.config }) + return { ctx, provider, parent: fakeParent(), engineFiber } } /** The standard test meta plus a body, spread into a start request. */ @@ -187,17 +250,124 @@ describe('dsh-workflow-workerthread', () => { expect(result.error).toContain('agent() could not start a child') }) + it('waits for async provider start before announcing a result that settled early', async () => { + const { ctx, parent, provider } = await setup({ manual: true, deferStart: true }) + const order: string[] = [] + ctx.on('workflow/agent-start', (_info, agent) => { order.push(`start:${agent.seq}`) }) + ctx.on('workflow/agent-end', (_info, agent) => { order.push(`end:${agent.outcome}`) }) + ctx.on('workflow/end', () => { order.push('run-end') }) + + const handle = ctx.workflows.start({ ...scripted("return await agent('p')"), parent }) + await waitFor(() => { expect(provider.runs.length).toBe(1) }) + const early = text('accepted value') + provider.runs[0]!.settle(early) + // The provider still owns this early result while start is pending. + await new Promise(resolve => setTimeout(resolve, 0)) + expect(order).toEqual([]) + + provider.runs[0]!.publish() + const result = await handle.result + expect(result.value).toBe('accepted value') + expect(order).toEqual(['start:1', 'end:completed', 'run-end']) + await handle.dispose() + expect(provider.runs[0]!.disposeCalls).toBe(1) + }) + + it('observes an early result rejection but sends ChildStarted before ChildFailed after start fulfills', async () => { + const { ctx, parent, provider } = await setup({ manual: true, deferStart: true }) + const lifecycle: string[] = [] + ctx.on('workflow/agent-start', () => { lifecycle.push('start') }) + ctx.on('workflow/agent-end', (_info, agent) => { lifecycle.push(`end:${agent.outcome}`) }) + const handle = ctx.workflows.start({ + ...scripted("try { await agent('p'); return 'unreachable' } catch (e) { return { code: e.code, message: e.message } }"), + parent, + }) + const worker = (handle as unknown as { worker: { postMessage(message: unknown): void } }).worker + const post = vi.spyOn(worker, 'postMessage') + const childMessageTypes = (): HostToWorkerType[] => post.mock.calls + .map(([message]) => (message as { type: HostToWorkerType }).type) + .filter(type => type === HostToWorkerType.ChildStarted || type === HostToWorkerType.ChildFailed) + + await waitFor(() => { expect(provider.runs.length).toBe(1) }) + provider.runs[0]!.rejectResult(new Error('backend failed before publication')) + await new Promise(resolve => setTimeout(resolve, 0)) + expect(childMessageTypes()).toEqual([]) + expect(lifecycle).toEqual([]) + + provider.runs[0]!.publish() + const result = await handle.result + expect(result.value).toMatchObject({ code: 'AGENT_RESULT' }) + expect((result.value as { message: string }).message).toContain('backend failed before publication') + expect(childMessageTypes()).toEqual([HostToWorkerType.ChildStarted, HostToWorkerType.ChildFailed]) + expect(lifecycle).toEqual(['start', 'end:failed']) + post.mockRestore() + await handle.dispose() + }) + + it('classifies provider start rejection as AGENT_START, drops an early result, and emits no false lifecycle pair', async () => { + const { ctx, parent, provider } = await setup({ manual: true, deferStart: true }) + const lifecycle: string[] = [] + ctx.on('workflow/agent-start', () => { lifecycle.push('start') }) + ctx.on('workflow/agent-end', () => { lifecycle.push('end') }) + + const handle = ctx.workflows.start({ + ...scripted("try { await agent('p'); return 'unreachable' } catch (e) { return { code: e.code, message: e.message } }"), + parent, + }) + await waitFor(() => { expect(provider.runs.length).toBe(1) }) + // ACP-style failure can settle result(error) before its session/publication + // boundary rejects. Start rejection must dominate that buffered child outcome. + provider.runs[0]!.settle({ output: [], stopReason: 'error' }) + await new Promise(resolve => setTimeout(resolve, 0)) + provider.runs[0]!.rejectStart(new Error('publication rolled back')) + + const result = await handle.result + expect(result.value).toMatchObject({ code: 'AGENT_START' }) + expect((result.value as { message: string }).message).toContain('publication rolled back') + expect(lifecycle).toEqual([]) + await waitFor(() => { + expect(provider.runs[0]!.disposed).toBe(true) + expect(provider.runs[0]!.disposeCalls).toBe(1) + }) + await handle.dispose() + expect(provider.runs[0]!.disposeCalls).toBe(1) + }) + + it('aborts a pending provider start once without publishing workflow lifecycle', async () => { + const { ctx, parent, provider } = await setup({ manual: true, deferStart: true, config: { disposeGraceMs: 500 } }) + const lifecycle: string[] = [] + ctx.on('workflow/agent-start', () => { lifecycle.push('start') }) + ctx.on('workflow/agent-end', () => { lifecycle.push('end') }) + + const handle = ctx.workflows.start({ ...scripted("return await agent('pending')"), parent }) + await waitFor(() => { expect(provider.runs.length).toBe(1) }) + const disposal = handle.dispose() + await waitFor(() => { + expect(provider.runs[0]!.cancelled).toBe('workflow disposed') + expect(provider.runs[0]!.disposed).toBe(true) + }) + // Ensure the host-driven disposal removed the registry entry before the + // late start rejection; its callback must not invoke dispose again. + await new Promise(resolve => setTimeout(resolve, 0)) + provider.runs[0]!.rejectStart(new Error('cancelled before publication')) + + const result = await handle.result + await disposal + expect(result.stopReason).toBe('cancelled') + expect(lifecycle).toEqual([]) + expect(provider.runs[0]!.disposeCalls).toBe(1) + }) + it('a child result REJECTION crosses back as a fatal AGENT_RESULT error (a broken provider is not a failed child)', async () => { const ctx = new Context() await ctx.plugin(SubagentService) const provider: SubagentProvider = { name: 'rejecting', - capabilities: { outputSchema: true, depthLimit: true, toolFilter: true }, + capabilities: { outputSchema: true, depthLimit: true, toolFilter: true, persona: false }, inheritsParentContext: false, - start: () => ({ + start: async () => ({ id: AgentId('reject-child'), result: Promise.reject(new Error('backend exploded')), - cancel: () => { /* nothing in flight */ }, dispose: () => Promise.resolve(), }), } @@ -210,18 +380,53 @@ describe('dsh-workflow-workerthread', () => { expect((result.value as { message: string }).message).toContain('backend exploded') }) - it('a child whose dispose() rejects cannot wedge the script (the host acks anyway)', async () => { + it('maps a non-JSON ready-child result to fatal AGENT_RESULT instead of wedging the bridge', async () => { + const { ctx, parent } = await setup({ + reply: () => ({ output: [], structured: () => { /* deliberately outside lossless JSON */ }, stopReason: 'completed' }), + }) + const result = await run(ctx, parent, scripted(` + try { await agent('p'); return 'unreachable' } catch (e) { return { code: e.code, message: e.message } } + `)) + expect(result.value).toMatchObject({ code: 'AGENT_RESULT' }) + expect((result.value as { message: string }).message).toContain('workflow child result could not cross the worker boundary') + }) + + it('contains a non-JSON result even if the injected subagent service violates its normalization contract', async () => { + // The real worker boundary must reject a non-JSON same-process result. + const { ctx, parent } = await setup() + const invalid = { + output: [], + structured: () => { /* deliberately outside lossless JSON */ }, + stopReason: 'completed', + } as unknown as SubagentResult + const start = vi.spyOn(ctx.subagents, 'start').mockResolvedValue({ + id: AgentId('raw-invalid-child'), + result: Promise.resolve(invalid), + dispose: () => Promise.resolve(), + }) + + const result = await run(ctx, parent, scripted(` + try { await agent('p'); return 'unreachable' } catch (e) { return { code: e.code, message: e.message } } + `)) + + expect(start).toHaveBeenCalledOnce() + expect(result.value).toMatchObject({ code: 'AGENT_RESULT' }) + expect((result.value as { message: string }).message) + .toContain('workflow child result could not cross the worker boundary') + }) + + it('a child whose dispose() throws synchronously cannot wedge the script (the host acks anyway)', async () => { const ctx = new Context() await ctx.plugin(SubagentService) const provider: SubagentProvider = { name: 'bad-dispose', - capabilities: { outputSchema: true, depthLimit: true, toolFilter: true }, + capabilities: { outputSchema: true, depthLimit: true, toolFilter: true, persona: false }, inheritsParentContext: false, - start: () => ({ + start: async () => ({ id: AgentId('bad-dispose-child'), result: Promise.resolve({ output: [{ type: 'text', text: 'fine' }], stopReason: 'completed' }), cancel: () => { /* settled already */ }, - dispose: () => Promise.reject(new Error('dispose exploded')), + dispose: () => { throw new Error('dispose exploded') }, }), } ctx.subagents.registerProvider(provider) @@ -236,9 +441,9 @@ describe('dsh-workflow-workerthread', () => { await ctx.plugin(SubagentService) const provider: SubagentProvider = { name: 'coercion-trap-dispose', - capabilities: { outputSchema: true, depthLimit: true, toolFilter: true }, + capabilities: { outputSchema: true, depthLimit: true, toolFilter: true, persona: false }, inheritsParentContext: false, - start: () => ({ + start: async () => ({ id: AgentId('trap-child'), result: Promise.resolve({ output: [{ type: 'text', text: 'fine' }], stopReason: 'completed' }), cancel: () => { /* settled already */ }, @@ -316,7 +521,7 @@ describe('dsh-workflow-workerthread', () => { const runEnds: WorkflowResultInfo[] = [] ctx.on('workflow/end', (_info, result) => { runEnds.push(result) }) const handle = ctx.workflows.start({ ...scripted("return await agent('long job')"), parent }) - await vi.waitFor(() => { expect(provider.runs.length).toBe(1) }) + await waitFor(() => { expect(provider.runs.length).toBe(1) }) handle.cancel('user stopped it') const result = await handle.result expect(result.stopReason).toBe('cancelled') @@ -357,12 +562,47 @@ describe('dsh-workflow-workerthread', () => { const controller = new AbortController() const second = ctx.workflows.start({ ...scripted("return await agent('job')"), parent, signal: controller.signal }) - await vi.waitFor(() => { expect(provider.runs.length).toBe(1) }) + await waitFor(() => { expect(provider.runs.length).toBe(1) }) controller.abort() expect((await second.result).stopReason).toBe('cancelled') await second.dispose() }) + it('removes the exact external abort callback on first settlement or teardown', async () => { + const { ctx, parent } = await setup() + const settledController = new AbortController() + const settledAdd = vi.spyOn(settledController.signal, 'addEventListener') + const settledRemove = vi.spyOn(settledController.signal, 'removeEventListener') + const completed = ctx.workflows.start({ ...scripted('return 123'), parent, signal: settledController.signal }) + const settledAbort = settledAdd.mock.calls.find(([type]) => type === 'abort')?.[1] + expect(typeof settledAbort).toBe('function') + + await expect(completed.result).resolves.toMatchObject({ value: 123, stopReason: 'completed' }) + expect(settledRemove).toHaveBeenCalledWith('abort', settledAbort) + const cancelAfterSettle = vi.spyOn(completed, 'cancel') + settledController.abort() + expect(cancelAfterSettle).not.toHaveBeenCalled() + cancelAfterSettle.mockRestore() + await completed.dispose() + + const manual = await setup({ manual: true }) + const teardownController = new AbortController() + const teardownAdd = vi.spyOn(teardownController.signal, 'addEventListener') + const teardownRemove = vi.spyOn(teardownController.signal, 'removeEventListener') + const tornDown = manual.ctx.workflows.start({ + ...scripted("return await agent('job')"), + parent: manual.parent, + signal: teardownController.signal, + }) + await waitFor(() => { expect(manual.provider.runs).toHaveLength(1) }) + const teardownAbort = teardownAdd.mock.calls.find(([type]) => type === 'abort')?.[1] + expect(typeof teardownAbort).toBe('function') + + const disposing = tornDown.dispose() + expect(teardownRemove).toHaveBeenCalledWith('abort', teardownAbort) + await disposing + }) + it('a child-start racing the host cancel is refused: no child starts after cancellation', async () => { const { ctx, parent, provider } = await setup({ manual: true }) // Cancel from INSIDE the log listener: the worker has already posted @@ -400,7 +640,7 @@ describe('dsh-workflow-workerthread', () => { `), parent, }) - await vi.waitFor(() => { expect(narration).toContain('started') }) + await waitFor(() => { expect(narration).toContain('started') }) handle.cancel('raced the completion') const result = await handle.result expect(result.stopReason).toBe('cancelled') @@ -480,35 +720,59 @@ describe('dsh-workflow-workerthread', () => { }) const result = await handle.result expect(result.stopReason).toBe('completed') - await vi.waitFor(() => { expect(provider.runs.length).toBe(1) }) + await waitFor(() => { expect(provider.runs.length).toBe(1) }) await handle.dispose() // Not a waitFor: by the time dispose() returns, the slow child disposal // must already be complete (host-side registry quiescence). expect(provider.runs[0]!.disposed).toBe(true) }) + it('result settlement reaps a registered stray even when the worker cannot relay disposal', async () => { + const { ctx, parent, provider } = await setup({ + manual: true, + config: { provider: 'stub', disposeGraceMs: 30_000 }, + }) + const handle = ctx.workflows.start({ + ...scripted("agent('stray')\nawait new Promise(() => {})"), + parent, + }) + await waitFor(() => { expect(provider.runs).toHaveLength(1) }) + + // Claim the host result while the real worker remains wedged, so it can + // send neither ChildDispose nor an exit. This leaves the accepted child + // in the host registry when public disposal begins. + const worker = (handle as unknown as { worker: Worker }).worker + worker.emit('message', { + type: WorkerToHostType.Result, + result: { value: 'synthetic completion', stopReason: 'completed', agentsStarted: 1 }, + }) + await expect(handle.result).resolves.toMatchObject({ stopReason: 'completed' }) + await waitFor(() => { expect(provider.runs[0]!.disposed).toBe(true) }, 1000) + + const disposal = handle.dispose() + await disposal + expect(provider.runs[0]!.disposeCalls).toBe(1) + await ctx.fiber.dispose() + }) + it('the settle-reap fires the request signal too: a provider honoring ONLY the signal winds its stray down promptly', async () => { const ctx = new Context() await ctx.plugin(SubagentService) const aborted: string[] = [] const provider: SubagentProvider = { name: 'signal-only', - capabilities: { outputSchema: true, depthLimit: true, toolFilter: true }, + capabilities: { outputSchema: true, depthLimit: true, toolFilter: true, persona: false }, inheritsParentContext: false, - start: (request) => { + start: async (request) => { let settle!: (result: SubagentResult) => void const result = new Promise((resolve) => { settle = resolve }) - request.signal?.addEventListener('abort', () => { - aborted.push(String(request.signal?.reason)) + request.signal.addEventListener('abort', () => { + aborted.push(String(request.signal.reason)) settle({ output: [], stopReason: 'aborted' }) }, { once: true }) return { id: AgentId('signal-only-child'), result, - // The seam leaves a provider free to honor EITHER cancel channel; - // this one deliberately ignores run.cancel() — only the request - // signal can wind it down. - cancel: () => { /* signal-only by design */ }, dispose: () => Promise.resolve(), } }, @@ -525,60 +789,103 @@ describe('dsh-workflow-workerthread', () => { const result = await handle.result expect(result.stopReason).toBe('completed') // BEFORE dispose(): the settlement itself must have aborted the signal — - // without it this child would stay live until dispose's terminate. - await vi.waitFor(() => { expect(aborted).toEqual(['workflow settled']) }) + // without it this child would stay live until dispose's terminate. This + // is a HOST-PROMPTNESS claim, not a cold-start race — a tight explicit + // bound (unlike the file default) so a multi-second reap regression + // cannot pass by outlasting the wait. + await waitFor(() => { expect(aborted).toEqual(['workflow settled']) }, 1000) await handle.dispose() }) - it("cancel() drives each child's explicit cancel() host-side: a wedged worker cannot delay it", async () => { - const ctx = new Context() - await ctx.plugin(SubagentService) - let starts = 0 - const cancelled: string[] = [] - const provider: SubagentProvider = { - name: 'cancel-only', - capabilities: { outputSchema: true, depthLimit: true, toolFilter: true }, - inheritsParentContext: false, - start: () => { - starts += 1 - return { - id: AgentId('cancel-only-child'), - result: new Promise(() => { /* only cancel() ends this child */ }), - // Deliberately ignores the request signal — the seam leaves a - // provider free to honor ONLY the explicit cancel() channel. - cancel: (reason?: string) => { cancelled.push(reason ?? 'cancelled') }, - dispose: () => Promise.resolve(), - } - }, - } - ctx.subagents.registerProvider(provider) - // A deliberately huge grace: if only the grace/terminate reap could - // reach this child, the assertion below would time out first. - await ctx.plugin(WorkerWorkflowEngine, { provider: 'cancel-only', maxConcurrentAgents: 2, disposeGraceMs: 30_000 }) + it('the settle-reap aborts a pending provider start before workflow/end', async () => { + const { ctx, parent, provider } = await setup({ manual: true, deferStart: true }) + const childLifecycle: string[] = [] + let cancellationAtWorkflowEnd: string | undefined + ctx.on('workflow/agent-start', () => { childLifecycle.push('start') }) + ctx.on('workflow/agent-end', () => { childLifecycle.push('end') }) + ctx.on('workflow/end', () => { + cancellationAtWorkflowEnd = provider.runs[0]?.cancelled + }) const handle = ctx.workflows.start({ - // The stray child's start RPC reaches the host, then the script wedges - // its own worker in a synchronous spin: the worker cannot process the - // Cancel message, so it can relay NO ChildCancel RPC — only the host's - // own children loop can deliver the explicit cancel in time. The - // microtask yields let the agent() continuation POST its child-start - // before the spin seizes the worker's loop (the posted message needs - // no further worker-loop turns to reach the host). ...scripted(` - agent('wedged child') + agent('start-pending stray') + return 'done' + `), + parent, + }) + + const result = await handle.result + + expect(result.stopReason).toBe('completed') + expect(provider.runs).toHaveLength(1) + expect(provider.runs[0]!.request.signal?.aborted).toBe(true) + expect(provider.runs[0]!.request.signal?.reason).toBe('workflow settled') + expect(provider.runs[0]!.cancelled).toBe('workflow settled') + expect(cancellationAtWorkflowEnd).toBe('workflow settled') + expect(childLifecycle).toEqual([]) + await handle.dispose() + expect(provider.runs[0]!.disposeCalls).toBe(1) + }) + + it('a duplicate Result after the terminal claim cannot repeat cleanup or rewrite the outcome', async () => { + let signalAborts = 0 + const { ctx, parent, provider } = await setup({ + manual: true, + onChildAbortString: (_reason, index) => { if (index === 0) signalAborts += 1 }, + }) + const handle = ctx.workflows.start({ + ...scripted("agent('stray')\nawait new Promise(() => {})"), + parent, + }) + await waitFor(() => { expect(provider.runs).toHaveLength(1) }) + const worker = (handle as unknown as { worker: Worker }).worker + + worker.emit('message', { + type: WorkerToHostType.Result, + result: { value: 'first', stopReason: 'completed', agentsStarted: 1 }, + }) + worker.emit('message', { + type: WorkerToHostType.Result, + result: { value: 'late', stopReason: 'completed', agentsStarted: 1 }, + }) + + await expect(handle.result).resolves.toMatchObject({ value: 'first', stopReason: 'completed' }) + expect(signalAborts).toBe(1) + await handle.dispose() + expect(signalAborts).toBe(1) + await ctx.fiber.dispose() + }) + + it('a grace-terminated worker reaps its child on exit without waiting for consumer dispose()', async () => { + const { ctx, parent, provider } = await setup({ + manual: true, + config: { provider: 'stub', maxConcurrentAgents: 2, disposeGraceMs: 100 }, + }) + const handle = ctx.workflows.start({ + // Let child-start cross, then make the worker unable to process its + // Cancel message. Grace settles the result and terminates the thread; + // that exit must independently own the host registry's disposal pass. + ...scripted(` + agent('survives until exit reap') for (let i = 0; i < 20; i++) await null const end = Date.now() + 1500 while (Date.now() < end) {} - return 'raced' + return 'unreachable' `), - parent: fakeParent(), + parent, }) - await vi.waitFor(() => { expect(starts).toBe(1) }) - handle.cancel('stop now') - await vi.waitFor(() => { expect(cancelled).toEqual(['stop now']) }, { timeout: 800 }) - // The wedged worker's own completion loses to the in-flight cancel. + await waitFor(() => { expect(provider.runs).toHaveLength(1) }) + + handle.cancel('force termination') const result = await handle.result + expect(result.stopReason).toBe('cancelled') + // Deliberately assert before handle.dispose(): host-owned worker exit, + // not consumer courtesy, is responsible for this resource guarantee. + await waitFor(() => { expect(provider.runs[0]!.disposed).toBe(true) }, 1000) + expect(provider.runs[0]!.disposeCalls).toBe(1) await handle.dispose() + await ctx.fiber.dispose() }, 15_000) it('dispose() on a wedged worker host-drives child disposal inside the grace: it returns with the children DISPOSED, not with their teardown still in flight', async () => { @@ -602,7 +909,7 @@ describe('dsh-workflow-workerthread', () => { `), parent, }) - await vi.waitFor(() => { expect(provider.runs.length).toBe(1) }) + await waitFor(() => { expect(provider.runs.length).toBe(1) }) const before = Date.now() await handle.dispose() // Bounded by the grace (plus the terminate), never by the 1.5s spin. @@ -625,7 +932,7 @@ describe('dsh-workflow-workerthread', () => { `), parent, }) - await vi.waitFor(() => { expect(provider.runs.length).toBe(1) }) + await waitFor(() => { expect(provider.runs.length).toBe(1) }) const handleDispose = handle.dispose() const result = await handle.result // The script itself settled (the wrapper's own dispose RPC found the @@ -663,7 +970,7 @@ describe('dsh-workflow-workerthread', () => { `), parent, }) - await vi.waitFor(() => { expect(order.filter(entry => entry.startsWith('start:')).length).toBe(2) }) + await waitFor(() => { expect(order.filter(entry => entry.startsWith('start:')).length).toBe(2) }) const fast = provider.runs.find(run => (run.request.prompt[0] as { text?: string }).text === 'fast')! fast.settle(text('fast done')) handle.cancel('stop now') @@ -694,7 +1001,7 @@ describe('dsh-workflow-workerthread', () => { ...scripted("await parallel([() => agent('a'), () => agent('b')])\nreturn 'unreachable'"), parent, }) - await vi.waitFor(() => { expect(provider.runs.length).toBe(2) }) + await waitFor(() => { expect(provider.runs.length).toBe(2) }) handle.cancel('user stop') const result = await handle.result expect(result.stopReason).toBe('cancelled') @@ -708,22 +1015,124 @@ describe('dsh-workflow-workerthread', () => { }) describe('worker death', () => { + it('the first death signal closes admission to messages Node delivers before exit', async () => { + const { ctx, parent, provider } = await setup({ manual: true }) + const phases: string[] = [] + ctx.on('workflow/phase', (_info, title) => { phases.push(title) }) + const handle = ctx.workflows.start({ + ...scripted('await new Promise(() => {})'), + parent, + }) + const worker = (handle as unknown as { worker: Worker }).worker + + // Node may physically emit error -> queued message -> exit. Reproduce + // that ordering deterministically at the Worker event boundary: the + // late protocol data must not create work, narrate, or rewrite error. + worker.emit('error', new Error('synthetic error-before-message')) + worker.emit('message', { type: WorkerToHostType.Phase, title: 'late phase' }) + worker.emit('message', { + type: WorkerToHostType.ChildStart, + callId: 999, + request: { prompt: 'late child' }, + }) + worker.emit('message', { + type: WorkerToHostType.Result, + result: { value: 'late', stopReason: 'completed', agentsStarted: 1 }, + }) + + const result = await handle.result + expect(result.stopReason).toBe('error') + expect(result.error).toContain('synthetic error-before-message') + expect(provider.runs).toHaveLength(0) + expect(phases).toEqual([]) + await handle.dispose() + await ctx.fiber.dispose() + }) + + it('refuses and disposes a provider run that becomes ready after its real worker dies', async () => { + const ctx = new Context() + await ctx.plugin(SubagentService) + const requested = Promise.withResolvers() + const ready = Promise.withResolvers() + let disposeCalls = 0 + const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => ctx.logger) + const provider: SubagentProvider = { + name: 'late-ready', + capabilities: { outputSchema: true, depthLimit: true, toolFilter: true, persona: false }, + inheritsParentContext: false, + start: (request) => { + requested.resolve(request) + // Model a backend whose independent startup boundary cannot be + // interrupted promptly. The host must still reject ownership if the + // worker dies before this promise transfers the ready run. + return ready.promise + }, + } + ctx.subagents.registerProvider(provider) + await ctx.plugin(WorkerWorkflowEngine, { provider: 'late-ready', maxConcurrentAgents: 1 }) + const lifecycle: string[] = [] + ctx.on('workflow/agent-start', () => { lifecycle.push('start') }) + ctx.on('workflow/agent-end', () => { lifecycle.push('end') }) + + const handle = ctx.workflows.start({ + ...scripted("return await agent('pending startup')"), + parent: fakeParent(), + }) + const request = await requested.promise + const worker = (handle as unknown as { worker: Worker }).worker + + // Kill the actual Worker while provider startup is independently + // pending. Death closes admission and aborts the shared signal, but this + // deliberately uncooperative provider still fulfills afterward. + await worker.terminate() + const result = await handle.result + expect(result.stopReason).toBe('error') + expect(result.error).toContain('exit code') + expect(request.signal.aborted).toBe(true) + expect(request.signal.reason).toBe('workflow worker gone') + + ready.resolve({ + id: AgentId('late-ready-child'), + result: Promise.resolve({ output: [], stopReason: 'aborted' }), + dispose: () => { + disposeCalls += 1 + return Promise.reject(new Error('late ready dispose failed')) + }, + }) + await waitFor(() => { + expect(disposeCalls).toBe(1) + expect(warn).toHaveBeenCalledWith(expect.stringContaining('refused child dispose failed: Error: late ready dispose failed')) + }, 1000) + expect(lifecycle).toEqual([]) + + await handle.dispose() + expect(disposeCalls).toBe(1) + await ctx.fiber.dispose() + }) + it('a worker that exits before settling reports an error result and reaps its children', async () => { const ctx = new Context() await ctx.plugin(SubagentService) // The child's dispose() REJECTS on top of the worker death: the reap // must contain it (warn, not crash) while still emptying the registry. - const cancelled: string[] = [] + const signalAborts: unknown[] = [] const provider: SubagentProvider = { name: 'doomed', - capabilities: { outputSchema: true, depthLimit: true, toolFilter: true }, + capabilities: { outputSchema: true, depthLimit: true, toolFilter: true, persona: false }, inheritsParentContext: false, - start: () => ({ - id: AgentId('doomed-child'), - result: new Promise(() => { /* never settles; the reap is the teardown */ }), - cancel: (reason?: string) => { cancelled.push(reason ?? 'cancelled') }, - dispose: () => Promise.reject(new Error('dispose exploded during reap')), - }), + start: async (request) => { + request.signal.addEventListener('abort', () => { + signalAborts.push(request.signal.reason) + // The death claim precedes the shared-signal fanout. This + // synchronous callback cannot turn death into cancellation. + handle.cancel('reentered from worker-death signal cleanup') + }, { once: true }) + return { + id: AgentId('doomed-child'), + result: new Promise(() => { /* never settles; the reap is the teardown */ }), + dispose: () => Promise.reject(new Error('dispose exploded during reap')), + } + }, } ctx.subagents.registerProvider(provider) await ctx.plugin(WorkerWorkflowEngine, { provider: 'doomed', maxConcurrentAgents: 2 }) @@ -749,7 +1158,13 @@ describe('dsh-workflow-workerthread', () => { // A worker death is a stop reason like any other: workflow/end fires // with the error outcome — for a bus observer it is the only obituary. expect(runEnds).toEqual([{ stopReason: 'error', error: result.error, agentsStarted: 1 }]) - await vi.waitFor(() => { expect(cancelled.length).toBe(1) }) + // Result already settled — this is the reap's promptness, not a + // cold-start race; tight explicit bound (see the helper's doc comment). + await waitFor(() => { + expect(signalAborts).toEqual(['workflow worker gone']) + }, 1000) + await Promise.resolve() + expect(result.stopReason).toBe('error') await handle.dispose() }, 15_000) @@ -770,10 +1185,12 @@ describe('dsh-workflow-workerthread', () => { expect(result.stopReason).toBe('error') expect(result.error).toContain('worker blew up') // The reap wound the stray child down (cancel + a CLEAN dispose). - await vi.waitFor(() => { + // Result already settled — this is the reap's promptness, not a + // cold-start race; tight explicit bound (see the helper's doc comment). + await waitFor(() => { expect(provider.runs.length).toBe(1) expect(provider.runs[0]!.disposed).toBe(true) - }) + }, 1000) await handle.dispose() }, 15_000) @@ -802,7 +1219,7 @@ describe('dsh-workflow-workerthread', () => { `), parent, }) - await vi.waitFor(() => { expect(order.filter(entry => entry.startsWith('start:')).length).toBe(2) }) + await waitFor(() => { expect(order.filter(entry => entry.startsWith('start:')).length).toBe(2) }) const fast = provider.runs.find(run => (run.request.prompt[0] as { text?: string }).text === 'fast')! fast.settle(text('fast done')) const result = await handle.result @@ -837,7 +1254,10 @@ describe('dsh-workflow-workerthread', () => { const result = await handle.result expect(result.stopReason).toBe('error') expect(result.error).toContain('exit code 5') - await vi.waitFor(() => { expect(provider.runs[0]!.disposed).toBe(true) }) + // Result already settled — this is the reap's promptness (bounded + // above the mock's fixed 300ms dispose delay, not a cold-start race); + // tight explicit bound (see the helper's doc comment). + await waitFor(() => { expect(provider.runs[0]!.disposed).toBe(true) }, 1000) await handle.dispose() }, 15_000) @@ -855,7 +1275,7 @@ describe('dsh-workflow-workerthread', () => { }) const logs: string[] = [] ctx.on('workflow/log', (_info, message) => { logs.push(message) }) - await vi.waitFor(() => { expect(logs).toContain('armed') }) + await waitFor(() => { expect(logs).toContain('armed') }) handle.cancel('stop it') // The grace is deliberately huge: only the worker's own death (exit 3, // unreachable by the cancel — the script ignores hooks) settles this. @@ -867,33 +1287,57 @@ describe('dsh-workflow-workerthread', () => { }) describe('service surface', () => { - it('run ids are unique per start; the run handle and event payloads hold SEPARATE meta clones', async () => { + it('run ids are unique and lifecycle meta is the run\'s borrowed immutable value', async () => { const { ctx, parent } = await setup() let eventMeta: WorkflowRunInfo | undefined ctx.on('workflow/start', (info) => { eventMeta = info }) const first = ctx.workflows.start({ ...scripted('return 1'), parent }) const second = ctx.workflows.start({ ...scripted('return 2'), parent }) expect(first.id).not.toBe(second.id) - eventMeta!.meta.name = 'corrupted' + expect(eventMeta!.meta).toBe(second.meta) expect(second.meta.name).toBe('test-flow') await Promise.all([first.result, second.result]) await first.dispose() await second.dispose() }) - it('unregisters ctx.workflows when the engine fiber is disposed (HMR safety), and default config runs (auto concurrency)', async () => { + it('unregisters ctx.workflows when the engine fiber is disposed (HMR safety)', async () => { const ctx = new Context() await ctx.plugin(SubagentService) const fiber = await ctx.plugin(WorkerWorkflowEngine, {}) expect(ctx.get('workflows')).toBeDefined() - // A zero-agent run through the DEFAULT config exercises the auto - // concurrency resolution (cores - 2, capped) in start(). - const result = await run(ctx, fakeParent(), scripted('return 6 * 7')) - expect(result.value).toBe(42) await fiber.dispose() expect(ctx.get('workflows')).toBeUndefined() }) + it('keeps a holder-owned run usable when the engine unloads before its child starts', async () => { + const { ctx, parent, provider, engineFiber } = await setup({ reply: () => text('survived reload') }) + let handle!: ReturnType + const holder = await ctx.plugin(Object.assign((inner: Context) => { + handle = inner.workflows.start({ ...scripted("return await agent('after reload')"), parent }) + }, { inject: ['workflows'] })) + + try { + // A real worker cannot deliver child-start in the synchronous start() + // slice. Unload the provider before that message arrives: the returned + // run belongs to `holder`, not to the engine fiber being reloaded. + expect(provider.runs).toHaveLength(0) + await engineFiber.dispose() + expect(ctx.get('workflows')).toBeUndefined() + + await expect(handle.result).resolves.toEqual({ + value: 'survived reload', + stopReason: 'completed', + agentsStarted: 1, + }) + expect(provider.runs).toHaveLength(1) + } finally { + await handle.dispose() + await holder.dispose() + await ctx.fiber.dispose() + } + }) + it('has the class-plugin export shape (default = the engine service class)', () => { expect(workerEngineModule.default).toBe(WorkerWorkflowEngine) const loader = Object.create(Loader.prototype) as Loader diff --git a/packages/workflow/workflow-workerthread/tsconfig.json b/packages/workflow/workflow-workerthread/tsconfig.json index 385651c192..730a3e61d9 100644 --- a/packages/workflow/workflow-workerthread/tsconfig.json +++ b/packages/workflow/workflow-workerthread/tsconfig.json @@ -26,6 +26,9 @@ { "path": "../../llm/llm" }, + { + "path": "../../core/session" + }, { "path": "../../subagent/subagent" }, diff --git a/packages/workflow/workflow/README.md b/packages/workflow/workflow/README.md index 5ff8028b0d..7caf169260 100644 --- a/packages/workflow/workflow/README.md +++ b/packages/workflow/workflow/README.md @@ -1,29 +1,43 @@ # @deepseek-ai/dsh-workflow -The **workflow seam** (`ctx.workflows`): an abstract service defining WHAT a workflow engine does — execute a model-written orchestration script that fans out subagents — without saying HOW. The bash-shaped third of the [workflow family](../README.md): implementations subclass `WorkflowService` and register as the `workflows` service (one per context); [`dsh-workflow-workerthread`](../workflow-workerthread/README.md) (one worker thread per run) is the implementation, and [`dsh-tool-workflow`](../tool-workflow/README.md) is the model-facing consumer. +The workflow seam (`ctx.workflows`) executes a model-written orchestration script that can fan out subagents. The seam defines the script, run, result, error, and event contracts; an engine decides how to isolate and execute the script. -## Service: `WorkflowService` (abstract) +`@deepseek-ai/dsh-workflow-workerthread` is the current engine and `@deepseek-ai/dsh-tool-workflow` is the model-facing consumer. A future process or sandbox engine can replace the implementation without changing the tool. -`start(request: WorkflowStartRequest): WorkflowRun` — parse and execute a script. Throws synchronously (`SCRIPT_PARSE`/`META_INVALID`) for a script that cannot begin; once a run is returned, its `result` NEVER rejects — every failure resolves with `stopReason: 'error'` (or `'cancelled'`) — and once the run is cancelled, `result` settles within the implementation's bounded grace even if the script itself never settles (a consumer awaiting `result` must never be wedged past a cancellation). `dispose()` must reach quiescence within a bounded grace (cancel → wait for the script to settle and its children to finish disposing → abandon), never hanging its caller. Runs are HOLDER-owned: the engine does not track its live runs, so disposing the engine's fiber mid-run leaves each run to its holder's teardown. +## Service and run contract -The protected `emitWorkflowEvent` helper dispatches the `workflow/*` events with PER-LISTENER containment and PER-LISTENER payload snapshots (a throwing subscriber is logged, never propagated, and cannot starve later listeners; each subscriber gets its own clone of the payload, so mutating it corrupts neither the engine nor other listeners) — the same containment guarantee as the subagent seam's lifecycle emits. +`WorkflowService.start(request): WorkflowRun` validates enough synchronously to reject a malformed meta block or unparseable script before a run exists. Once returned, `WorkflowRun.result` never rejects: execution failures resolve with `stopReason: 'error'`, and cancellation resolves with `cancelled` within the engine's bounded grace. -## Vocabulary +A run is holder-owned. Engine-plugin unload prevents new starts but does not revoke accepted runs. The holder must call `dispose()` on every path; disposal cancels remaining work and reaches or abandons quiescence within the documented bound. -- `WorkflowStartRequest` — `{ script, args?, parent: Agent, signal? }`. `parent` is REQUIRED: every child the script spawns is attributed to it. `args` must be plain host-realm JSON data. -- `WorkflowMeta` / `WorkflowPhase` — the workflow's identity block, carried as plain JSON data on the start request (Claude Code meta vocabulary: required `name`/`description`, optional `whenToUse`/`phases`) and shape-validated by the engine. -- `WorkflowRun` — `{ id, meta, result, cancel(reason?), dispose() }`; the consumer awaits `result` and MUST `dispose` on every path. -- `WorkflowResult` — `{ value, stopReason: 'completed'|'cancelled'|'error', error?, agentsStarted }`; `value` is the script's materialized return (plain JSON data; `null` for no return). -- `WorkflowError` — `HarnessError` with a `WorkflowErrorCode` and a `fatal` flag driving the combinator discipline: a fatal error (bad hook arguments, unsupported options/schemas, tripped caps, seam start failures, cancellation) always propagates through `parallel()`/`pipeline()` instead of dissolving into a per-item `null`. `isFatalWorkflowError(error)` is the catch-site predicate. +`WorkflowStartRequest` contains `{ meta, script, args?, parent, signal? }`. `parent` attributes every child agent to the invoking agent. `meta` and `args` are plain data, not script fragments. + +`WorkflowRun` exposes `{ id, meta, result, cancel(reason?), dispose() }`. `WorkflowResult` contains `{ value, stopReason, error?, agentsStarted }`; `value` is plain JSON data or `null`. ## Events -All observe-only emits carrying DATA SNAPSHOTS (`WorkflowRunInfo` = id + meta) — never the live `WorkflowRun`, so a listener cannot gain `cancel`/`dispose`; control stays with the `start()` caller: +Workflow events are observe-only. They carry `WorkflowRunInfo` (`id` plus `meta`) rather than the live run, so listeners cannot acquire cancellation or disposal authority. -- `workflow/start`(info) / `workflow/end`(info, resultInfo) — run lifecycle; `resultInfo` deliberately omits the value. -- `workflow/phase`(info, title) / `workflow/log`(info, message) — script narration. -- `workflow/agent-start`(info, agent) / `workflow/agent-end`(info, agent + outcome) — one pair per `agent()` call that STARTED a child run (a call rejected at validation or caps, refused at start, or cancelled while queued for a slot emits no pair), correlated by `seq`. +- `workflow/start` / `workflow/end` pair the run. +- `workflow/phase` and `workflow/log` expose script narration. +- `workflow/agent-start` / `workflow/agent-end` pair each child call by `seq`; a child whose async provider start rejects emits neither. -## Non-goals (this cut) +Same-process event payloads are borrowed immutable values. Every listener is independently contained: a synchronous throw or rejected returned promise is logged without starving peers or changing execution. -Background collection, journaling/resume, saved workflows, nested `workflow()`, token budgets — see the [RFC's deferred section](../../../docs/rfc/implemented/feature/2026-07-05-dynamic-workflows.md). +## Failure discipline + +`WorkflowError` carries a code and a `fatal` flag. Fatal errors always escape `parallel()` and `pipeline()` instead of becoming an ordinary per-item `null`: + +- `SCRIPT_PARSE` / `META_INVALID` — the workflow cannot start. +- `INVALID_ARGUMENT` / `UNSUPPORTED_OPTION` / `UNSUPPORTED_SCHEMA` — a hook call violates the engine contract. +- `AGENT_CAP` / `ITEM_CAP` — configured safety limits were exceeded. +- `AGENT_START` — the provider's async start rejected. +- `AGENT_RESULT` — a ready child's result rejected with an infrastructure fault. +- `RESULT_UNSERIALIZABLE` — a script/worker value is not plain JSON data. +- `CANCELLED` — cancellation owns the run and pending/future hooks reject. + +A child that resolves normally with a non-completed stop reason is not an infrastructure exception: `agent()` returns `null`, allowing the script to handle an ordinary child failure. + +## Non-goals + +Background collection, journaling/resume, saved workflows, nested `workflow()`, and token budgets are deferred. See the [dynamic-workflows RFC](../../../docs/rfc/implemented/feature/2026-07-05-dynamic-workflows.md). diff --git a/packages/workflow/workflow/src/index.ts b/packages/workflow/workflow/src/index.ts index 91caa314dc..0fa0289b41 100644 --- a/packages/workflow/workflow/src/index.ts +++ b/packages/workflow/workflow/src/index.ts @@ -9,14 +9,12 @@ * separate-process sandbox) swap in without touching the model-facing tool * that consumes them (`@deepseek-ai/dsh-tool-workflow`). * - * The `workflow/*` lifecycle events are OBSERVE-ONLY data snapshots: they + * The `workflow/*` lifecycle events are OBSERVE-ONLY data: they * carry {@link WorkflowRunInfo} (id + meta), never the live {@link WorkflowRun} * — a listener must not gain `cancel`/`dispose`; control stays with the - * `start()` caller holding the run. Every emit is per-listener contained (a - * throwing subscriber is logged, never propagated) and every listener gets its - * own payload clone (mutating it corrupts nothing), so one bad observer can - * neither strand a live run, starve later listeners, nor poison another - * listener's view. + * `start()` caller holding the run. Same-process payloads are borrowed + * immutable values. Every listener is independently contained, so a throw or + * rejected promise can neither strand a run nor starve peers. * * @module @deepseek-ai/dsh-workflow */ @@ -76,8 +74,10 @@ declare module 'cordis' { */ 'workflow/log'(info: WorkflowRunInfo, message: string): void /** - * One `agent()` call started a child run. Paired with - * {@link Events['workflow/agent-end']} by `agent.seq`. + * One `agent()` call established a ready child run. Paired with + * {@link Events['workflow/agent-end']} by `agent.seq`. A call that never + * receives a ready run from the provider emits neither + * event in this pair. * @param info - the run's identity snapshot. * @param agent - the call's sequence number, label, phase, and child id. * @mode emit @@ -129,10 +129,11 @@ export type WorkflowEventName = * - `UNSUPPORTED_SCHEMA` — an `agent()` schema outside the structured-output * subset (see dsh-tools). * - `AGENT_CAP` / `ITEM_CAP` — the run/agent caps tripped. - * - `AGENT_START` — the subagent seam refused to start a child. - * - `AGENT_RESULT` — a child's `result` REJECTED: an infrastructure fault at - * the subagent seam, distinct from a child that failed and resolved (which - * is the per-item `null`, never an error). + * - `AGENT_START` — the provider's asynchronous start rejected before + * cancellation took precedence. + * - `AGENT_RESULT` — a ready run had its `result` REJECT: an infrastructure + * fault at the subagent seam. This is distinct from a child that failed and resolved + * (which is the per-item `null`, never an error). * - `RESULT_UNSERIALIZABLE` — a value crossing the script/host value boundary * is not plain JSON data. * - `CANCELLED` — the run was cancelled; pending and future hooks reject @@ -194,8 +195,8 @@ export function isFatalWorkflowError(error: unknown): boolean { * `result` SETTLES within the implementation's bounded grace even if the * script itself never settles (a consumer awaiting `result` must never be * wedged past a cancellation). - * - The `workflow/*` events fire through {@link emitWorkflowEvent} (data - * snapshots, per-listener containment); `workflow/end` fires exactly once + * - The `workflow/*` events fire through {@link emitWorkflowEvent} (borrowed + * immutable data, per-listener containment); `workflow/end` fires exactly once * per started run, after `result` is settled or as it settles. * - `dispose()` reaches quiescence within a bounded grace: it cancels, waits * for the script to settle AND its started children to finish disposing, @@ -221,12 +222,9 @@ export abstract class WorkflowService extends Service { abstract start(request: WorkflowStartRequest): WorkflowRun /** - * Emit one `workflow/*` lifecycle event with PER-LISTENER containment and - * PER-LISTENER payload snapshots: each subscriber is dispatched individually - * with its OWN structural clone of the payload (the payloads are plain JSON - * data by the seam contract), so a listener mutating what it received can - * corrupt neither the engine's live state nor any other listener's or later - * event's view; a thrown listener is logged (never propagated — the logging + * Emit one `workflow/*` lifecycle event with per-listener containment. Each + * subscriber receives the same borrowed immutable payload; a throw or + * asynchronously rejected listener is logged (never propagated — the logging * itself is total, even for a thrown value whose own string coercion * throws), so one bad subscriber can neither fail the engine mid-run, * surface as an unhandled rejection on a detached settle hook, nor starve @@ -238,9 +236,10 @@ export abstract class WorkflowService extends Service { protected emitWorkflowEvent(name: WorkflowEventName, ...args: unknown[]): void { for (const callback of this.ctx.events.dispatch('emit', [name, ...args])) { try { - // The declared workflow/* signatures are all void-returning emits; the - // dispatch callback applies the payload tuple. - ;(callback as (...payload: unknown[]) => void)(...structuredClone(args)) + const returned: unknown = (callback as (...payload: unknown[]) => unknown)(...args) + void Promise.resolve(returned).catch((error: unknown) => { + this.ctx.logger.warn(`workflow: ${name} listener rejected: ${renderListenerError(error)}`) + }) } catch (error: unknown) { this.ctx.logger.warn(`workflow: ${name} listener threw: ${renderListenerError(error)}`) } diff --git a/packages/workflow/workflow/src/types.ts b/packages/workflow/workflow/src/types.ts index 66da4f1268..981a2da172 100644 --- a/packages/workflow/workflow/src/types.ts +++ b/packages/workflow/workflow/src/types.ts @@ -128,7 +128,7 @@ export interface WorkflowRun { dispose(): Promise } -/** Identifying detail for a run, carried by every `workflow/*` event (a data snapshot, never the live run). */ +/** Identifying detail for a run, carried by every `workflow/*` event as borrowed immutable data, never the live run. */ export interface WorkflowRunInfo { /** The run's id. */ id: WorkflowRunId diff --git a/packages/workflow/workflow/tests/workflow.spec.ts b/packages/workflow/workflow/tests/workflow.spec.ts index a983e5a2b3..0df3824071 100644 --- a/packages/workflow/workflow/tests/workflow.spec.ts +++ b/packages/workflow/workflow/tests/workflow.spec.ts @@ -66,26 +66,21 @@ describe('dsh-workflow (interface)', () => { ]) }) - it('gives each listener its OWN payload snapshot: mutation corrupts neither peers nor the caller', async () => { + it('contains an asynchronously rejected listener without starving peers', async () => { const ctx = new Context() await ctx.plugin(StubEngine) + const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => ctx.logger) const seen: string[] = [] - ctx.on('workflow/agent-start', (info, agent) => { - agent.label = 'HACKED' - info.meta.name = 'HACKED' - seen.push('mutator') - }) - ctx.on('workflow/agent-start', (info, agent) => { - seen.push(`${info.meta.name}/${agent.label}`) - }) + // Runtime listeners may return thenables even though the declaration's observable result is void. + // eslint-disable-next-line @typescript-eslint/no-misused-promises -- exercises rejected-listener containment + ctx.on('workflow/agent-start', async () => { throw new Error('async observer failed') }) + ctx.on('workflow/agent-start', (_info, agent) => { seen.push(agent.label) }) const engine = ctx.workflows as StubEngine - const info: WorkflowRunInfo = { id: WorkflowRunId('run-2'), meta: { name: 'w', description: 'd' } } const payload = { seq: 1, label: 'original', childId: 'c' } - engine.emit('workflow/agent-start', info, payload) - expect(seen).toEqual(['mutator', 'w/original']) - // The caller's own objects are pristine too — no listener ever saw them. - expect(info.meta.name).toBe('w') - expect(payload.label).toBe('original') + engine.emit('workflow/agent-start', INFO, payload) + await Promise.resolve() + expect(seen).toEqual(['original']) + expect(String(warn.mock.calls[0]![0])).toContain('listener rejected') }) it('contains a throwing listener PER LISTENER: later listeners still run, nothing propagates', async () => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4425c6bd41..649a986385 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -80,6 +80,12 @@ importers: '@deepseek-ai/dsh-brand': specifier: workspace:^ version: link:../../util/brand + '@deepseek-ai/dsh-sandbox': + specifier: workspace:^ + version: link:../../sandbox/sandbox + '@deepseek-ai/dsh-session': + specifier: workspace:^ + version: link:../../core/session cordis: specifier: ^4.0.0-rc.6 version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) @@ -100,6 +106,31 @@ importers: specifier: ^4.0.0-rc.6 version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) + packages/bash/bash-sandbox: + dependencies: + schemastery: + specifier: ^3.18.0 + version: 3.18.0 + devDependencies: + '@deepseek-ai/dsh-bash': + specifier: workspace:^ + version: link:../bash + '@deepseek-ai/dsh-bash-local': + specifier: workspace:^ + version: link:../bash-local + '@deepseek-ai/dsh-sandbox': + specifier: workspace:^ + version: link:../../sandbox/sandbox + '@deepseek-ai/dsh-sandbox-local': + specifier: workspace:^ + version: link:../../sandbox/sandbox-local + cordis: + specifier: ^4.0.0-rc.6 + version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) + node-addon-landlock-run: + specifier: 0.0.0-test.0 + version: 0.0.0-test.0 + packages/bash/tool-bash: devDependencies: '@deepseek-ai/dsh-agent': @@ -114,9 +145,18 @@ importers: '@deepseek-ai/dsh-bash-local': specifier: workspace:^ version: link:../bash-local + '@deepseek-ai/dsh-bash-sandbox': + specifier: workspace:^ + version: link:../bash-sandbox '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm + '@deepseek-ai/dsh-sandbox': + specifier: workspace:^ + version: link:../../sandbox/sandbox + '@deepseek-ai/dsh-sandbox-local': + specifier: workspace:^ + version: link:../../sandbox/sandbox-local '@deepseek-ai/dsh-session': specifier: workspace:^ version: link:../../core/session @@ -126,6 +166,9 @@ importers: '@deepseek-ai/dsh-tools': specifier: workspace:^ version: link:../../core/tools + '@deepseek-ai/dsh-user-approval': + specifier: workspace:^ + version: link:../../ui/user-approval cordis: specifier: ^4.0.0-rc.6 version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) @@ -212,6 +255,9 @@ importers: '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm + '@deepseek-ai/dsh-scope': + specifier: workspace:^ + version: link:../../core/scope '@deepseek-ai/dsh-session': specifier: workspace:^ version: link:../../core/session @@ -233,6 +279,9 @@ importers: '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm + '@deepseek-ai/dsh-scope': + specifier: workspace:^ + version: link:../scope '@deepseek-ai/dsh-session': specifier: workspace:^ version: link:../session @@ -267,12 +316,21 @@ importers: '@deepseek-ai/dsh-session': specifier: workspace:^ version: link:../session + '@deepseek-ai/dsh-skill': + specifier: workspace:^ + version: link:../../skill/skill + '@deepseek-ai/dsh-skill-local': + specifier: workspace:^ + version: link:../../skill/skill-local '@deepseek-ai/dsh-system-prompt': specifier: workspace:^ version: link:../system-prompt '@deepseek-ai/dsh-tool-bash': specifier: workspace:^ version: link:../../bash/tool-bash + '@deepseek-ai/dsh-tool-skill': + specifier: workspace:^ + version: link:../../skill/tool-skill '@deepseek-ai/dsh-tools': specifier: workspace:^ version: link:../tools @@ -295,6 +353,9 @@ importers: '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm + '@deepseek-ai/dsh-scope': + specifier: workspace:^ + version: link:../scope '@deepseek-ai/dsh-session': specifier: workspace:^ version: link:../session @@ -314,6 +375,12 @@ importers: specifier: ^4.0.0-rc.6 version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) + packages/core/scope: + devDependencies: + cordis: + specifier: ^4.0.0-rc.6 + version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) + packages/core/session: devDependencies: '@deepseek-ai/dsh-brand': @@ -322,6 +389,9 @@ importers: '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm + '@deepseek-ai/dsh-scope': + specifier: workspace:^ + version: link:../scope cordis: specifier: ^4.0.0-rc.6 version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) @@ -335,6 +405,9 @@ importers: '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm + '@deepseek-ai/dsh-scope': + specifier: workspace:^ + version: link:../scope cordis: specifier: ^4.0.0-rc.6 version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) @@ -354,12 +427,18 @@ importers: '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm + '@deepseek-ai/dsh-scope': + specifier: workspace:^ + version: link:../scope '@deepseek-ai/dsh-session': specifier: workspace:^ version: link:../session '@deepseek-ai/dsh-system-prompt': specifier: workspace:^ version: link:../system-prompt + '@deepseek-ai/dsh-user-approval': + specifier: workspace:^ + version: link:../../ui/user-approval cordis: specifier: ^4.0.0-rc.6 version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) @@ -605,6 +684,34 @@ importers: specifier: ^4.0.0-rc.6 version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) + packages/sandbox/sandbox: + devDependencies: + '@deepseek-ai/dsh-llm': + specifier: workspace:^ + version: link:../../llm/llm + cordis: + specifier: ^4.0.0-rc.6 + version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) + + packages/sandbox/sandbox-local: + dependencies: + node-addon-landlock-run: + specifier: 0.0.0-test.0 + version: 0.0.0-test.0 + schemastery: + specifier: ^3.18.0 + version: 3.18.0 + devDependencies: + '@deepseek-ai/dsh-llm': + specifier: workspace:^ + version: link:../../llm/llm + '@deepseek-ai/dsh-sandbox': + specifier: workspace:^ + version: link:../sandbox + cordis: + specifier: ^4.0.0-rc.6 + version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) + packages/session-persistence/session-persistence: devDependencies: '@deepseek-ai/dsh-session': @@ -646,6 +753,63 @@ importers: specifier: ^4.0.0-rc.6 version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) + packages/skill/skill: + dependencies: + schemastery: + specifier: ^3.18.0 + version: 3.18.0 + devDependencies: + cordis: + specifier: ^4.0.0-rc.6 + version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) + + packages/skill/skill-local: + dependencies: + schemastery: + specifier: ^3.18.0 + version: 3.18.0 + yaml: + specifier: ^2.4.2 + version: 2.9.0 + devDependencies: + '@deepseek-ai/dsh-fs': + specifier: workspace:^ + version: link:../../fs/fs + '@deepseek-ai/dsh-skill': + specifier: workspace:^ + version: link:../skill + cordis: + specifier: ^4.0.0-rc.6 + version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) + + packages/skill/tool-skill: + dependencies: + schemastery: + specifier: ^3.18.0 + version: 3.18.0 + devDependencies: + '@deepseek-ai/dsh-agent': + specifier: workspace:^ + version: link:../../core/agent + '@deepseek-ai/dsh-llm': + specifier: workspace:^ + version: link:../../llm/llm + '@deepseek-ai/dsh-scope': + specifier: workspace:^ + version: link:../../core/scope + '@deepseek-ai/dsh-skill': + specifier: workspace:^ + version: link:../skill + '@deepseek-ai/dsh-skill-local': + specifier: workspace:^ + version: link:../skill-local + '@deepseek-ai/dsh-tools': + specifier: workspace:^ + version: link:../../core/tools + cordis: + specifier: ^4.0.0-rc.6 + version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) + packages/subagent/subagent: devDependencies: '@deepseek-ai/dsh-agent': @@ -654,6 +818,9 @@ importers: '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm + '@deepseek-ai/dsh-scope': + specifier: workspace:^ + version: link:../../core/scope '@deepseek-ai/dsh-tools': specifier: workspace:^ version: link:../../core/tools @@ -875,6 +1042,9 @@ importers: '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm + '@deepseek-ai/dsh-scope': + specifier: workspace:^ + version: link:../../core/scope '@deepseek-ai/dsh-session': specifier: workspace:^ version: link:../../core/session @@ -973,6 +1143,9 @@ importers: '@deepseek-ai/dsh-agent-loop': specifier: workspace:^ version: link:../../core/agent-loop + '@deepseek-ai/dsh-bash': + specifier: workspace:^ + version: link:../../bash/bash '@deepseek-ai/dsh-bash-local': specifier: workspace:^ version: link:../../bash/bash-local @@ -982,9 +1155,15 @@ importers: '@deepseek-ai/dsh-fs-policy': specifier: workspace:^ version: link:../../fs/fs-policy + '@deepseek-ai/dsh-invariants': + specifier: workspace:^ + version: link:../../support/invariants '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm + '@deepseek-ai/dsh-sandbox': + specifier: workspace:^ + version: link:../../sandbox/sandbox '@deepseek-ai/dsh-session': specifier: workspace:^ version: link:../../core/session @@ -1012,6 +1191,9 @@ importers: '@deepseek-ai/dsh-tools': specifier: workspace:^ version: link:../../core/tools + '@deepseek-ai/dsh-user-approval': + specifier: workspace:^ + version: link:../user-approval '@deepseek-ai/dsh-user-interaction': specifier: workspace:^ version: link:../user-interaction @@ -1030,6 +1212,9 @@ importers: '@deepseek-ai/dsh-acp': specifier: workspace:^ version: link:../acp + '@deepseek-ai/dsh-agent': + specifier: workspace:^ + version: link:../../core/agent '@deepseek-ai/dsh-agent-core': specifier: workspace:^ version: link:../../core/agent-core @@ -1136,6 +1321,34 @@ importers: specifier: ^4.0.0-rc.6 version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) + packages/ui/user-approval: + dependencies: + schemastery: + specifier: ^3.18.0 + version: 3.18.0 + devDependencies: + '@deepseek-ai/dsh-agent': + specifier: workspace:^ + version: link:../../core/agent + '@deepseek-ai/dsh-brand': + specifier: workspace:^ + version: link:../../util/brand + '@deepseek-ai/dsh-llm': + specifier: workspace:^ + version: link:../../llm/llm + '@deepseek-ai/dsh-scope': + specifier: workspace:^ + version: link:../../core/scope + '@deepseek-ai/dsh-session': + specifier: workspace:^ + version: link:../../core/session + '@deepseek-ai/dsh-system-prompt': + specifier: workspace:^ + version: link:../../core/system-prompt + cordis: + specifier: ^4.0.0-rc.6 + version: 4.0.0-rc.6(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.4) + packages/ui/user-interaction: devDependencies: '@deepseek-ai/dsh-agent': @@ -3759,6 +3972,22 @@ packages: natural-compare@1.4.0: resolution: {integrity: sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==} + node-addon-landlock-run-linux-arm64@0.0.0-test.0: + resolution: {integrity: sha512-oJsXcC33qKl9mWYx0n9YPJ2pUAoY39PoIX0Gx4lDrSCTEvENFrEaODAsQYNY+eEGpn9YMN7E+FOftvea3/1FqQ==} + engines: {node: '>=20'} + cpu: [arm64] + os: [linux] + + node-addon-landlock-run-linux-x64@0.0.0-test.0: + resolution: {integrity: sha512-eXvdfnH/UV55MTZzroKvM3CD68SP5OlCsuth908YOcJOnn0LPD5KJjmBz6ToDlBYjF52NNK62+g7TvmUWjbKWQ==} + engines: {node: '>=20'} + cpu: [x64] + os: [linux] + + node-addon-landlock-run@0.0.0-test.0: + resolution: {integrity: sha512-c5qopltRonjW6+VinXYMp4FVi9Sxf8eQEb/9G82EksQQ+JC4CDprv+ko5URWmtyZ3wD4GFdeRTyw1AG5yCwJhQ==} + engines: {node: '>=20'} + node-domexception@1.0.0: resolution: {integrity: sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==} engines: {node: '>=10.5.0'} @@ -6765,6 +6994,17 @@ snapshots: natural-compare@1.4.0: {} + node-addon-landlock-run-linux-arm64@0.0.0-test.0: + optional: true + + node-addon-landlock-run-linux-x64@0.0.0-test.0: + optional: true + + node-addon-landlock-run@0.0.0-test.0: + optionalDependencies: + node-addon-landlock-run-linux-arm64: 0.0.0-test.0 + node-addon-landlock-run-linux-x64: 0.0.0-test.0 + node-domexception@1.0.0: {} node-fetch@3.3.2: diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index b2b731fc58..6407a99f52 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -19,3 +19,12 @@ allowBuilds: # need, so we deny them — install still succeeds. '@google/genai': false protobufjs: false + +# The Landlock launcher family is our own sibling-repo release, consumed +# fresh (hours old at each coordinated bump) — the release-age quarantine +# would block every such bump, so the family is exempted BY NAME, not by +# pinned version. +minimumReleaseAgeExclude: + - node-addon-landlock-run + - node-addon-landlock-run-linux-arm64 + - node-addon-landlock-run-linux-x64 diff --git a/scripts/doc-budgets.manifest.json b/scripts/doc-budgets.manifest.json index a5261698a2..235b8f21fa 100644 --- a/scripts/doc-budgets.manifest.json +++ b/scripts/doc-budgets.manifest.json @@ -1,11 +1,11 @@ { "AGENTS.md": 1802, "docs/AGENTS.md": 1315, - "docs/architecture.md": 1642, + "docs/architecture.md": 1790, "docs/cordis-primer.md": 550, "docs/defensive-patterns.md": 550, "docs/testing.md": 800, - "examples/AGENTS.md": 653, + "examples/AGENTS.md": 705, "packages/AGENTS.md": 450, - "packages/README.md": 660 + "packages/README.md": 710 } diff --git a/scripts/gen-config-catalog.ts b/scripts/gen-config-catalog.ts index eca36286ae..1cbcc007bd 100644 --- a/scripts/gen-config-catalog.ts +++ b/scripts/gen-config-catalog.ts @@ -633,11 +633,17 @@ export function collectConfigCatalog(scanRoot: string = root): CatalogEntry[] { const manifests: { dir: string; pkg: string }[] = [] for (const manifestRel of globSync('packages/*/*/package.json', { cwd: scanRoot }).sort()) { const dir = manifestRel.slice(0, -'/package.json'.length) - const pkg = (JSON.parse(readFileSync(resolve(scanRoot, manifestRel), 'utf8')) as { name?: string }).name + const manifest = JSON.parse(readFileSync(resolve(scanRoot, manifestRel), 'utf8')) as { name?: string; os?: string[]; cpu?: string[] } + const pkg = manifest.name if (!pkg) { violations.push(`${manifestRel} has no "name".`) continue } + if (manifest.os !== undefined && manifest.cpu !== undefined) { + // A per-platform native-binary package (npm os/cpu selection) ships no + // JavaScript at all — nothing to classify, no Config to catalog. + continue + } pkgDirByName.set(pkg, dir) manifests.push({ dir, pkg }) } diff --git a/scripts/gen-cordis-catalog.ts b/scripts/gen-cordis-catalog.ts index 9b8141807e..68139e9ff8 100644 --- a/scripts/gen-cordis-catalog.ts +++ b/scripts/gen-cordis-catalog.ts @@ -87,16 +87,25 @@ export const LINK_MAP: Record = { GenerateOptions: 'core.md', LlmCallConfig: 'core.md', SessionEvent: 'core.md', + SessionStartSource: 'core.md', StreamChunk: 'llm-streaming.md', TurnEndReason: 'session.md', ToolDefinition: 'tools.md', ToolExecution: 'tools.md', + ToolExecutionInput: 'tools.md', ToolExecutionResult: 'tools.md', + ToolExecutionToken: 'tools.md', + ApprovalOutcome: 'approval.md', + ApprovalPolicy: 'approval.md', + ApprovalRequest: 'approval.md', BashExecRequest: 'bash.md', BashExecSpec: 'bash.md', BashRunResult: 'bash.md', BashTask: 'bash.md', BashTaskRead: 'bash.md', + ConfinedArgv: 'sandbox.md', + SandboxMode: 'sandbox.md', + SandboxPolicy: 'sandbox.md', CodeRunRequest: 'code-runtime.md', CodeRunResult: 'code-runtime.md', FsEditOutcome: 'filesystem.md', diff --git a/scripts/gen-doc-graphs.ts b/scripts/gen-doc-graphs.ts index 610ff3f1ea..0db3be2f6b 100644 --- a/scripts/gen-doc-graphs.ts +++ b/scripts/gen-doc-graphs.ts @@ -70,7 +70,9 @@ const GROUP_ORDER = [ 'llm', 'core', 'bash', + 'sandbox', 'fs', + 'skill', 'compact', 'subagent', 'web', @@ -120,10 +122,10 @@ const SERVICE_ROLES: ServiceRole[] = [ { key: 'tools', pkg: 'tools', - title: 'Tool registry and execution waterfall', + title: 'Tool registry and guarded execution pipeline', mode: 'core', - consumers: ['agent-loop', 'tool-ask-user', 'tool-bash', 'tool-cordis', 'tool-fs', 'tool-subagent', 'tool-todo', 'tool-web', 'acp'], - note: 'Registers tool definitions, exposes schemas to the prompt, and routes calls through tools/pre-execute and tools/post-execute.', + consumers: ['agent-loop', 'tool-ask-user', 'tool-bash', 'tool-cordis', 'tool-fs', 'tool-skill', 'tool-subagent', 'tool-todo', 'tool-web', 'acp'], + note: 'Registers capabilities, owns Code Mode transport, and routes calls through pre-policy, monotonic guards, around dispatch, post-policy, and final-result observation.', }, { key: 'userInteraction', @@ -134,6 +136,15 @@ const SERVICE_ROLES: ServiceRole[] = [ consumers: ['tool-ask-user', 'stdio-agent', 'acp'], note: 'UI front doors provide the active human-answer provider; tool-ask-user pauses a tool call on the provider-neutral ask() promise.', }, + { + key: 'skills', + pkg: 'skill', + title: 'Skill provider registry', + mode: 'seam', + implementations: ['skill-local'], + consumers: ['tool-skill'], + note: 'Merges provider skill catalogs; tool-skill renders the session-prefix catalog and loads complete skill bodies.', + }, { key: 'agents', pkg: 'agent', @@ -155,9 +166,27 @@ const SERVICE_ROLES: ServiceRole[] = [ pkg: 'bash', title: 'Bash executor seam', mode: 'seam', - implementations: ['bash-local'], + implementations: ['bash-local', 'bash-sandbox'], consumers: ['tool-bash', 'hooks-claude', 'hooks-codex'], - note: 'The model-facing bash tools and hook bridges consume this seam; sandboxed or remote executors can replace bash-local.', + note: 'The model-facing bash tools and hook bridges consume this seam; sandboxed or remote executors replace bash-local without touching them.', + }, + { + key: 'sandbox', + pkg: 'sandbox', + title: 'Process-sandbox seam', + mode: 'seam', + implementations: ['sandbox-local'], + consumers: ['bash-sandbox'], + note: 'Consumers hand over the exact argv they are about to spawn; same-world backends wrap it under a per-call policy and report enforcement.', + }, + { + key: 'approval', + pkg: 'approval', + title: 'Approval seam', + mode: 'seam', + implementations: ['acp'], + consumers: ['tools', 'tool-bash'], + note: 'One-shot permission decisions dispatched over the `approval/request` waterfall; answerers are listeners (the ACP bridge for its own agents), absence fails closed to `unavailable`.', }, { key: 'codeRuntime', @@ -217,11 +246,34 @@ const SERVICE_ROLES: ServiceRole[] = [ ] const DYNAMIC_EVENT_DISPATCHERS: Array<{ event: string; pkg: string; method: string }> = [ + // Creation notifications preserve synchronous veto/rollback but observe + // returned promises explicitly so async listener rejection is not unhandled. + { event: 'agent/created', pkg: 'agent', method: 'events.dispatch' }, + // Registry disposal reuses the stable carrier captured before entry commit + // and contains each listener directly rather than rebuilding via agentEvents. + { event: 'agent/disposed', pkg: 'agent', method: 'events.dispatch' }, + { event: 'session/created', pkg: 'session', method: 'events.dispatch' }, + // Session event callbacks are likewise resolved before the log push, then + // invoked individually after commit so observer failures are contained. + { event: 'session/event', pkg: 'session', method: 'events.dispatch' }, + // Flush resolves the scoped callback set directly so internal instrumentation + // cannot substitute the accepted session before parallel invocation. + { event: 'session/flush', pkg: 'session', method: 'events.dispatch' }, + // Session disposal uses direct callback resolution so teardown contains each + // synchronous throw and returned-promise rejection independently. + { event: 'session/disposed', pkg: 'session', method: 'events.dispatch' }, + // tools/result uses ctx.events.dispatch directly so the registry can invoke + // every synchronous observer while containing each callback independently. + { event: 'tools/result', pkg: 'tools', method: 'events.dispatch' }, // Subagent lifecycle events intentionally bypass ctx.emit and call // ctx.events.dispatch directly so one throwing listener cannot starve later // listeners or strand an already-started child run. { event: 'subagent/start', pkg: 'subagent', method: 'events.dispatch' }, { event: 'subagent/end', pkg: 'subagent', method: 'events.dispatch' }, + // provider-removed fires inside the provider registration's DISPOSER and + // routes through the same contained dispatch (see emitLifecycle in + // dsh-subagent), so the AST scan cannot attribute it either. + { event: 'subagent/provider-removed', pkg: 'subagent', method: 'events.dispatch' }, // The workflow/* lifecycle events dispatch the same way, for the same // per-listener-containment reason (WorkflowService.emitWorkflowEvent). { event: 'workflow/start', pkg: 'workflow', method: 'events.dispatch' }, @@ -232,6 +284,12 @@ const DYNAMIC_EVENT_DISPATCHERS: Array<{ event: string; pkg: string; method: str { event: 'workflow/end', pkg: 'workflow', method: 'events.dispatch' }, ] +const DYNAMIC_EVENT_LISTENERS: Array<{ event: string; pkg: string }> = [ + // The invariants oracle marks the session started from its global + // internal/dispatch listener before product session-start callbacks run. + { event: 'agent/session-start', pkg: 'invariants' }, +] + function generatedHeader(title: string): string[] { return [ '>Session: tool-owned events when applicable', ` Driver->>Session: ${mermaidCode('tool/result')} and ${mermaidCode('step/end')}`, ` Driver->>Hooks: ${mermaidCode('agent/turn-continuation')} waterfall`, + ` Driver->>Hooks: ${mermaidCode('agent/turn-stop')} serial terminal checkpoint`, ` Driver->>Session: ${mermaidCode('turn/end')}`, ` Driver->>Persistence: ${mermaidCode('session/flush')} parallel checkpoint`, ` Driver-->>SDK: ${mermaidCode('agent/status')} idle`, @@ -665,7 +763,7 @@ function renderToolPipeline(): string { const maintenance = 'curated Mermaid flow; exact tool schemas and event signatures live in generated catalogs' return [ ...generatedHeader('Tool Execution Pipeline'), - 'This graph shows where policy, hooks, sandboxing, filesystem guards, result rewriting, and UI rendering fit without changing the loop. The key extension points are the `tools/pre-execute`, `tools/execute`, and `tools/post-execute` waterfalls.', + 'This graph shows where policy, hooks, sandboxing, filesystem guards, result rewriting, final-outcome observation, and UI rendering fit without changing the loop. The transformable extension points are the `tools/pre-execute`, `tools/execute`, and `tools/post-execute` waterfalls; monotonic guards and `tools/result` are the owner-enforced boundaries around them.', '', '```mermaid', 'flowchart TD', @@ -673,33 +771,44 @@ function renderToolPipeline(): string { ` toolCall["Session event: ${mermaidCode('tool/call')}
logged before execution"]`, ' presentCall["UI pending card
presentCall(args)"]', ` pre["${mermaidCode('tools/pre-execute')} waterfall
hooks, permission, sandbox"]`, - ' denied["deny or ask
tool body skipped"]', + ' guards["Registered monotonic guards
deny or abstain; identity protected"]', + ' denied["denied or approval refused
tool body skipped"]', + ` approval["${mermaidCode('ctx.approval')} one-shot prompt
absent or unanswerable: deny"]`, ` around["${mermaidCode('tools/execute')} waterfall
timeout, retry, metrics (around dispatch)"]`, ' toolBody["Registered tool execute() body"]', ` fsGate["${mermaidCode('fs/write-intent')} or ${mermaidCode('fs/edit-intent')}
tool-fs mutations only"]`, ` owned["Tool-owned session events
${mermaidCode('todo/write')}, ${mermaidCode('fs/observed')}, ${mermaidCode('hook/invoked')}, ${mermaidCode('hook/result')}, ${mermaidCode('tool/code-dispatch')}"]`, ` post["${mermaidCode('tools/post-execute')} waterfall
accept, block, replace, add context"]`, + ` final["${mermaidCode('tools/result')} synchronous notification
frozen authoritative outcome"]`, ' context["Buffered additionalContext
context/message after all tool results"]', ` toolResult["Session event: ${mermaidCode('tool/result')}
single model-facing outcome"]`, + ' allResults["All calls in the step settled
and tool/result events recorded"]', ' presentResult["UI completed card
presentResult(args, result)"]', ' model --> toolCall', ' toolCall --> presentCall', ' toolCall --> pre', - ' pre -->|allow| around', + ' pre -->|allow| guards', + ' guards -->|allow| around', + ' guards -->|deny| denied', ' around --> toolBody', - ' pre -->|deny or ask| denied', + ' pre -->|deny| denied', + ' pre -->|ask| approval', + ' approval -->|allowed-once| guards', + ' approval -->|rejected, cancelled, unavailable| denied', ' denied --> post', ' toolBody --> fsGate', ' fsGate --> toolBody', ' toolBody --> owned', ' toolBody --> around', ' around --> post', - ' post --> context', - ' post --> toolResult', + ' post --> final', + ' final --> toolResult', ' toolResult --> presentResult', + ' toolResult --> allResults', + ' allResults --> context', '```', '', - 'Filesystem read-before-edit checks live below `tool-fs` on the `fs/*` event gate; hook bridges and future permission prompts live on the generic pre/post tool waterfalls; and around-dispatch concerns like the tool-call timeout policy (`@deepseek-ai/dsh-timeout-policy`) wrap core dispatch on `tools/execute`. That split lets the same hooks observe bash, fs, web, todo, and subagent calls without coupling those tools to one policy service. Code Mode rides the same pipeline twice over: `run_code` is itself a registered tool body, and each tool call its program makes re-enters `ctx.tools.execute()` through BOTH waterfalls — serialized one at a time, logged as a `tool/code-dispatch` session event, with a deny surfacing to the program as a binding rejection (a sub-call\'s `additionalContext` is deliberately dropped — no safe outlet mid-run preserves call/result adjacency).', + 'Filesystem read-before-edit checks live below `tool-fs` on the `fs/*` event gate; hook bridges and approval-triggering permission policy enter through the generic pre/post tool waterfalls, while `ctx.approval` resolves an `ask` before the monotonic guards; owner policy that must not be reordered uses registered guards; and around-dispatch concerns like the tool-call timeout policy (`@deepseek-ai/dsh-timeout-policy`) wrap core dispatch on `tools/execute`. The synchronous `tools/result` notification observes the immutable final outcome after every transform, lossless-JSON validation, and outer error normalization. That split lets the same hooks observe bash, fs, web, todo, skill, and subagent calls without coupling those tools to one policy service. Code Mode rides the whole pipeline twice over: `run_code` is the reserved registry-owned transport whose body enters the pipeline, and each tool call its program makes re-enters `ctx.tools.execute()` — serialized one at a time, carrying the outer execution\'s opaque token for correlation, and logged as a `tool/code-dispatch` session event, with a deny surfacing to the program as a binding rejection (a sub-call\'s `additionalContext` is deliberately dropped — no safe outlet mid-run preserves call/result adjacency).', '', ...maintenanceFooter(maintenance), ].join('\n') diff --git a/scripts/gen-module-graph.ts b/scripts/gen-module-graph.ts index dc66dc843e..54dbcf2773 100644 --- a/scripts/gen-module-graph.ts +++ b/scripts/gen-module-graph.ts @@ -42,6 +42,7 @@ const GROUP_ORDER = [ 'core', 'bash', 'fs', + 'skill', 'compact', 'subagent', 'web', diff --git a/scripts/gen-tool-catalog.ts b/scripts/gen-tool-catalog.ts index 62e40907b8..9e7dfe44bd 100644 --- a/scripts/gen-tool-catalog.ts +++ b/scripts/gen-tool-catalog.ts @@ -47,10 +47,13 @@ import * as WebSearchExa from '@deepseek-ai/dsh-web-search-exa' import * as WebFetchLocal from '@deepseek-ai/dsh-web-fetch-local' import SubagentService from '@deepseek-ai/dsh-subagent' import * as SubagentMock from '@deepseek-ai/dsh-subagent-mock' +import SkillService from '@deepseek-ai/dsh-skill' +import * as SkillLocal from '@deepseek-ai/dsh-skill-local' import * as ToolAskUser from '@deepseek-ai/dsh-tool-ask-user' import * as ToolBash from '@deepseek-ai/dsh-tool-bash' import * as ToolCordis from '@deepseek-ai/dsh-tool-cordis' import * as ToolFs from '@deepseek-ai/dsh-tool-fs' +import * as ToolSkill from '@deepseek-ai/dsh-tool-skill' import * as ToolTodo from '@deepseek-ai/dsh-tool-todo' import * as ToolSubagent from '@deepseek-ai/dsh-tool-subagent' import * as ToolWeb from '@deepseek-ai/dsh-tool-web' @@ -137,7 +140,7 @@ const TOOL_PACKAGES: ToolPackage[] = [ toolsConfig: { mode: 'code' }, async mount() {}, note: - 'Registered by the tool registry itself under `mode: code` / `mode: both` (see the Code Mode RFC). Under `code` it is the ONLY wire tool; the other registered tools are declared to the model as a generated TypeScript SDK prompt section instead, and a program calls them through port-bridged bindings that dispatch through the ordinary tools/pre-execute → tools/post-execute pipeline, one at a time.', + 'Owned by the tool registry as a reserved transport outside filterable capability layers under `mode: code` / `mode: both` (see the Code Mode RFC). Under `code` it is the registry\'s only wire contribution; the other visible capabilities are declared in a generated TypeScript SDK section, and a program calls them through serialized bindings that re-enter the complete guarded tool pipeline and link each nested execution to this outer result.', }, { pkg: '@deepseek-ai/dsh-tool-bash', @@ -180,6 +183,21 @@ const TOOL_PACKAGES: ToolPackage[] = [ note: 'The read-before-write/edit policy is added by `@deepseek-ai/dsh-fs-policy` (an `fs/*` event-gate plugin, no schema change); a deployment that loads these tools is expected to also load it. The tool schemas above are identical with or without the policy plugin.', }, + { + pkg: '@deepseek-ai/dsh-tool-skill', + dir: 'tool-skill', + source: 'packages/skill/tool-skill/src/index.ts', + requires: ['ctx.tools', 'ctx.skills'], + writes: ['tool/call', 'tool/result'], + async mount(ctx) { + await ctx.plugin(SkillService) + await ctx.plugin(SkillLocal, { + dshHome: resolve(root, '.tmp/tool-catalog/.dsh'), + agentsHome: resolve(root, '.tmp/tool-catalog/.agents'), + }) + await ctx.plugin(ToolSkill) + }, + }, { pkg: '@deepseek-ai/dsh-tool-subagent', dir: 'tool-subagent', diff --git a/scripts/run-gates.ts b/scripts/run-gates.ts index 9ee5a66ed6..b26e13a56c 100644 --- a/scripts/run-gates.ts +++ b/scripts/run-gates.ts @@ -143,6 +143,11 @@ function gatesForMode(selected: Mode): Gate[] { case 'node-compat': return [ pnpmScript('typecheck', 'typecheck'), + pnpmExec('source-worker-smoke', [ + 'vitest', + 'run', + 'packages/workflow/workflow-workerthread/tests/source-worker.compat.spec.ts', + ], { label: 'source worker smoke' }), ] case 'pre-push': return [ @@ -262,6 +267,7 @@ function docSyncLeafGates(): Gate[] { pnpmScript('config-catalog', 'verify-config-catalog', { label: 'config catalog' }), pnpmScript('persistence-catalog', 'verify-persistence-catalog', { label: 'persistence catalog' }), pnpmScript('doc-graphs', 'verify-doc-graphs', { label: 'doc graphs' }), + pnpmScript('scoped-dispatch', 'verify-scoped-dispatch', { label: 'scoped dispatch' }), pnpmScript('markdown-wrap', 'verify-md-wrap', { label: 'markdown wrap' }), pnpmScript('markdown-links', 'verify-md-links', { label: 'markdown links' }), pnpmScript('doc-refs', 'verify-doc-refs', { label: 'doc refs' }), @@ -286,18 +292,28 @@ function demoSmokeGate(options: { needs?: string[] } = {}): Gate { ...dependencyOptions, verify: async (result) => { const output = result.stdout + result.stderr - if (!output.includes('[tool call] echo({"text":"ci smoke"})')) { - throw new Error('demo smoke did not show the echo tool call.') + const sessionsRoot = join(root, '.sessions') + try { + if (!output.includes('[tool call] echo({"text":"ci smoke"})')) { + throw new Error('demo smoke did not show the echo tool call.') + } + if (!output.includes('[tool result] ECHO: CI SMOKE')) { + throw new Error('demo smoke did not show the echo tool result.') + } + const buckets = await readdir(sessionsRoot, { withFileTypes: true }) + let found = false + for (const bucket of buckets) { + if (!bucket.isDirectory() || !bucket.name.startsWith('cwd-')) continue + const entries = await readdir(join(sessionsRoot, bucket.name)) + if (entries.some(entry => /^main-session-.+\.jsonl$/.test(entry))) { + found = true + break + } + } + if (!found) throw new Error('demo smoke did not create a main-session JSONL log in a cwd bucket.') + } finally { + await rm(sessionsRoot, { recursive: true, force: true }) } - if (!output.includes('[tool result] ECHO: CI SMOKE')) { - throw new Error('demo smoke did not show the echo tool result.') - } - const sessionDir = join(root, '.sessions', '_no-cwd') - const entries = await readdir(sessionDir) - if (!entries.some(entry => /^main-session-.+\.jsonl$/.test(entry))) { - throw new Error('demo smoke did not create a main-session JSONL log.') - } - await rm(join(root, '.sessions'), { recursive: true, force: true }) }, } } diff --git a/scripts/type-equiv.manifest.json b/scripts/type-equiv.manifest.json index 881ce06578..a219ea1b1d 100644 --- a/scripts/type-equiv.manifest.json +++ b/scripts/type-equiv.manifest.json @@ -14,8 +14,17 @@ { "doc": "docs/core-data-structures/core.md", "symbol": "HookContext", "source": "packages/core/agent/src/types.ts" }, { "doc": "docs/core-data-structures/core.md", "symbol": "PromptDecision", "source": "packages/core/agent/src/types.ts" }, { "doc": "docs/core-data-structures/core.md", "symbol": "ContinuationDecision", "source": "packages/core/agent/src/types.ts" }, + { "doc": "docs/core-data-structures/core.md", "symbol": "ContinuationStop", "source": "packages/core/agent/src/types.ts" }, { "doc": "docs/core-data-structures/core.md", "symbol": "SessionStartSource", "source": "packages/core/agent/src/types.ts" }, + { "doc": "docs/core-data-structures/scope.md", "symbol": "ScopeKey", "source": "packages/core/scope/src/index.ts" }, + { "doc": "docs/core-data-structures/scope.md", "symbol": "Scoped", "source": "packages/core/scope/src/index.ts" }, + { "doc": "docs/core-data-structures/scope.md", "symbol": "Scope", "source": "packages/core/scope/src/index.ts" }, + + { "doc": "docs/core-data-structures/system-prompt.md", "symbol": "AssembleContext", "source": "packages/core/system-prompt/src/index.ts" }, + { "doc": "docs/core-data-structures/system-prompt.md", "symbol": "PromptSection", "source": "packages/core/system-prompt/src/index.ts" }, + { "doc": "docs/core-data-structures/system-prompt.md", "symbol": "ToolProviderResult", "source": "packages/core/system-prompt/src/index.ts" }, + { "doc": "docs/core-data-structures/llm-streaming.md", "symbol": "StreamChunk", "source": "packages/llm/llm/src/types.ts" }, { "doc": "docs/core-data-structures/llm-streaming.md", "symbol": "TokenUsage", "source": "packages/llm/llm/src/types.ts" }, { "doc": "docs/core-data-structures/llm-streaming.md", "symbol": "ContentBlockMap", "source": "packages/llm/llm/src/types.ts" }, @@ -39,7 +48,11 @@ { "doc": "docs/core-data-structures/tools.md", "symbol": "SchemaProp", "source": "packages/core/tools/src/schema.ts" }, { "doc": "docs/core-data-structures/tools.md", "symbol": "SchemaSpec", "source": "packages/core/tools/src/schema.ts" }, { "doc": "docs/core-data-structures/tools.md", "symbol": "InferArgs", "source": "packages/core/tools/src/schema.ts" }, + { "doc": "docs/core-data-structures/tools.md", "symbol": "ToolExecutionToken", "source": "packages/core/tools/src/index.ts" }, + { "doc": "docs/core-data-structures/tools.md", "symbol": "ToolExecutionInput", "source": "packages/core/tools/src/index.ts" }, { "doc": "docs/core-data-structures/tools.md", "symbol": "ToolExecution", "source": "packages/core/tools/src/index.ts" }, + { "doc": "docs/core-data-structures/tools.md", "symbol": "ToolGuard", "source": "packages/core/tools/src/index.ts" }, + { "doc": "docs/core-data-structures/tools.md", "symbol": "ToolRestriction", "source": "packages/core/tools/src/index.ts" }, { "doc": "docs/core-data-structures/tools.md", "symbol": "ToolExecutionResult", "source": "packages/core/tools/src/index.ts" }, { "doc": "docs/core-data-structures/tools.md", "symbol": "PreToolDecision", "source": "packages/core/tools/src/index.ts" }, { "doc": "docs/core-data-structures/tools.md", "symbol": "PostToolDecision", "source": "packages/core/tools/src/index.ts" }, @@ -56,13 +69,25 @@ { "doc": "docs/core-data-structures/user-interaction.md", "symbol": "UserInteractionProvider", "source": "packages/ui/user-interaction/src/index.ts" }, { "doc": "docs/core-data-structures/user-interaction.md", "symbol": "UserInteractionError", "source": "packages/ui/user-interaction/src/index.ts" }, + { "doc": "docs/core-data-structures/approval.md", "symbol": "ApprovalRequestId", "source": "packages/ui/user-approval/src/index.ts" }, + { "doc": "docs/core-data-structures/approval.md", "symbol": "ApprovalOutcome", "source": "packages/ui/user-approval/src/index.ts" }, + { "doc": "docs/core-data-structures/approval.md", "symbol": "ApprovalPolicy", "source": "packages/ui/user-approval/src/index.ts" }, + { "doc": "docs/core-data-structures/approval.md", "symbol": "ApprovalRequest", "source": "packages/ui/user-approval/src/index.ts" }, + { "doc": "docs/core-data-structures/bash.md", "symbol": "BashExecRequest", "source": "packages/bash/bash/src/types.ts" }, { "doc": "docs/core-data-structures/bash.md", "symbol": "BashExecSpec", "source": "packages/bash/bash/src/types.ts" }, { "doc": "docs/core-data-structures/bash.md", "symbol": "BashRunResult", "source": "packages/bash/bash/src/types.ts" }, + { "doc": "docs/core-data-structures/bash.md", "symbol": "BashSandboxInfo", "source": "packages/bash/bash/src/types.ts" }, { "doc": "docs/core-data-structures/bash.md", "symbol": "CollectedOutput", "source": "packages/bash/bash/src/types.ts" }, { "doc": "docs/core-data-structures/bash.md", "symbol": "BashTask", "source": "packages/bash/bash/src/types.ts" }, { "doc": "docs/core-data-structures/bash.md", "symbol": "BashTaskRead", "source": "packages/bash/bash/src/types.ts" }, + { "doc": "docs/core-data-structures/sandbox.md", "symbol": "SandboxMode", "source": "packages/sandbox/sandbox/src/index.ts" }, + { "doc": "docs/core-data-structures/sandbox.md", "symbol": "ConfinedSandboxMode", "source": "packages/sandbox/sandbox/src/index.ts" }, + { "doc": "docs/core-data-structures/sandbox.md", "symbol": "SandboxEnforcement", "source": "packages/sandbox/sandbox/src/index.ts" }, + { "doc": "docs/core-data-structures/sandbox.md", "symbol": "SandboxPolicy", "source": "packages/sandbox/sandbox/src/index.ts" }, + { "doc": "docs/core-data-structures/sandbox.md", "symbol": "ConfinedArgv", "source": "packages/sandbox/sandbox/src/index.ts" }, + { "doc": "docs/core-data-structures/code-runtime.md", "symbol": "CodeRunRequest", "source": "packages/code-runtime/code-runtime/src/types.ts" }, { "doc": "docs/core-data-structures/code-runtime.md", "symbol": "CodeRunResult", "source": "packages/code-runtime/code-runtime/src/types.ts" }, { "doc": "docs/core-data-structures/code-runtime.md", "symbol": "CodeBindingNamespace", "source": "packages/code-runtime/code-runtime/src/types.ts" }, @@ -83,6 +108,16 @@ { "doc": "docs/core-data-structures/filesystem.md", "symbol": "FsPolicyExec", "source": "packages/fs/fs-policy/src/types.ts" }, { "doc": "docs/core-data-structures/filesystem.md", "symbol": "FileReadOutcome", "source": "packages/fs/tool-fs/src/read-render.ts" }, + { "doc": "docs/core-data-structures/skills.md", "symbol": "SkillSource", "source": "packages/skill/skill/src/index.ts" }, + { "doc": "docs/core-data-structures/skills.md", "symbol": "SkillResourceBase", "source": "packages/skill/skill/src/index.ts" }, + { "doc": "docs/core-data-structures/skills.md", "symbol": "SkillSummary", "source": "packages/skill/skill/src/index.ts" }, + { "doc": "docs/core-data-structures/skills.md", "symbol": "SkillCandidate", "source": "packages/skill/skill/src/index.ts" }, + { "doc": "docs/core-data-structures/skills.md", "symbol": "SkillDefinition", "source": "packages/skill/skill/src/index.ts" }, + { "doc": "docs/core-data-structures/skills.md", "symbol": "SkillRegistration", "source": "packages/skill/skill/src/index.ts" }, + { "doc": "docs/core-data-structures/skills.md", "symbol": "SkillLookupOptions", "source": "packages/skill/skill/src/index.ts" }, + { "doc": "docs/core-data-structures/skills.md", "symbol": "SkillProvider", "source": "packages/skill/skill/src/index.ts" }, + { "doc": "docs/core-data-structures/skills.md", "symbol": "Config", "source": "packages/skill/skill/src/index.ts" }, + { "doc": "docs/core-data-structures/compaction.md", "symbol": "CompactionResult", "source": "packages/compact/compact/src/types.ts" }, { "doc": "docs/core-data-structures/subagent.md", "symbol": "SubagentCapabilities", "source": "packages/subagent/subagent/src/types.ts" }, diff --git a/scripts/verify-md-wrap.ts b/scripts/verify-md-wrap.ts index 2d8845e030..3143ee5617 100644 --- a/scripts/verify-md-wrap.ts +++ b/scripts/verify-md-wrap.ts @@ -18,9 +18,11 @@ * A wrapped paragraph inside a list item or blockquote is still a `paragraph` * node, so those are caught too. Scope mirrors doc-typecheck plus the two * AGENTS.md files that doc-sync does NOT otherwise cover (the convention itself - * lives there): README.md, docs/** /*.md, packages/* /*.md, AGENTS.md, - * packages/AGENTS.md. The root and packages/ CLAUDE.md are symlinks to the - * AGENTS.md files, so they are deduped by real path. + * lives there), plus generated system-prompt Markdown goldens: README.md, + * docs/** /*.md, packages/* /*.md, examples/** /system-prompt.golden.md, + * packages/** /system-prompt.golden.md, AGENTS.md, packages/AGENTS.md. The root + * and packages/ CLAUDE.md are symlinks to the AGENTS.md files, so they are + * deduped by real path. * * Run: `tsx scripts/verify-md-wrap.ts`. */ @@ -34,8 +36,18 @@ import type { Nodes } from 'mdast' const root = resolve(import.meta.dirname, '..') -/** Files to check: doc-typecheck's scope plus the AGENTS.md pair. */ -const PATTERNS = ['README.md', 'README.zh.md', 'docs/**/*.md', 'packages/*/*.md', 'packages/*/*/*.md', 'AGENTS.md', 'packages/AGENTS.md'] +/** Files to check: doc-typecheck's scope, prompt goldens, and the AGENTS.md pair. */ +const PATTERNS = [ + 'README.md', + 'README.zh.md', + 'docs/**/*.md', + 'packages/*/*.md', + 'packages/*/*/*.md', + 'examples/**/system-prompt.golden.md', + 'packages/**/system-prompt.golden.md', + 'AGENTS.md', + 'packages/AGENTS.md', +] /** A located hard-wrap: a prose paragraph spanning more than one source line. */ interface Violation { diff --git a/scripts/verify-scoped-dispatch.ts b/scripts/verify-scoped-dispatch.ts new file mode 100644 index 0000000000..214d55f7dc --- /dev/null +++ b/scripts/verify-scoped-dispatch.ts @@ -0,0 +1,78 @@ +/** + * Scoped-dispatch drift gate: the set of scope-filtered events is declared in + * TWO places that must never diverge — the dev-invariants runtime table (the + * `scopedSubject` map in `packages/support/invariants/src/index.ts`, which + * enforces carriers at dispatch time) and the event declarations' JSDoc (the + * "Scope-filtered dispatch" sentence rendered into the events catalog, which + * tells plugin authors what a scoped listener will and won't hear). An event + * added to one side without the other either silently escapes runtime + * enforcement or documents filtering that never happens; this gate fails the + * build instead. + * + * Sources of truth: the invariant table is parsed from the invariants source; + * the documented set is parsed from every `declare module 'cordis'` Events + * JSDoc in packages/*\/*\/src carrying the marker sentence. Registry-subject + * notifications (`tools/change`, `system-prompt/change`, `subagent/provider-*`) + * are deliberately unfiltered and must appear in NEITHER set. + */ + +import { globSync, readFileSync } from 'node:fs' +import { resolve } from 'node:path' + +const root = resolve(import.meta.dirname, '..') + +/** The marker sentence every scope-filtered event's JSDoc carries. */ +const MARKER = 'Scope-filtered dispatch' + +/** Events that are deliberately UNFILTERED registry-subject notifications. */ +const REGISTRY_SUBJECT = new Set(['tools/change', 'system-prompt/change', 'subagent/provider-added', 'subagent/provider-removed']) + +function invariantTable(): Set { + const source = readFileSync(resolve(root, 'packages/support/invariants/src/index.ts'), 'utf8') + const start = source.indexOf('const scopedSubject') + if (start < 0) throw new Error('verify-scoped-dispatch: cannot find the scopedSubject table in dsh-invariants') + const block = source.slice(start, source.indexOf('}', start)) + return new Set([...block.matchAll(/'([a-z-]+\/[a-z-]+)':/g)].flatMap(match => match[1] === undefined ? [] : [match[1]])) +} + +function documentedSet(): Set { + const documented = new Set() + for (const rel of globSync('packages/*/*/src/**/*.ts', { cwd: root })) { + const source = readFileSync(resolve(root, rel), 'utf8') + if (!source.includes(MARKER)) continue + // Each event declaration: a JSDoc block followed by the quoted event name. + // Tolerate `//` comment lines between the JSDoc and the declaration + // (e.g. an inline TODO under the doc block). + for (const match of source.matchAll(/\/\*\*([\s\S]*?)\*\/\s*\n(?:\s*\/\/[^\n]*\n)*\s*'([a-z-]+\/[a-z-]+)'\(/g)) { + const [, doc, event] = match + if (doc === undefined || event === undefined) continue + if (doc.includes(MARKER)) documented.add(event) + } + } + return documented +} + +const table = invariantTable() +const documented = documentedSet() + +const problems: string[] = [] +for (const event of table) { + if (!documented.has(event)) { + problems.push(`"${event}" is enforced by the dev-invariants carrier table but its declaration JSDoc carries no "${MARKER}" sentence — document the filtering plugin authors will observe.`) + } + if (REGISTRY_SUBJECT.has(event)) { + problems.push(`"${event}" is a registry-subject notification (deliberately unfiltered) but appears in the dev-invariants carrier table.`) + } +} +for (const event of documented) { + if (!table.has(event)) { + problems.push(`"${event}" documents scope-filtered dispatch but is missing from the dev-invariants carrier table (packages/support/invariants) — a bare dispatch of it would silently revert to global delivery.`) + } +} + +if (problems.length > 0) { + console.error(`verify-scoped-dispatch: ${problems.length} drift(s) between the invariant table and the documented scoped-event set:`) + for (const problem of problems) console.error(` - ${problem}`) + process.exit(1) +} +console.log(`verify-scoped-dispatch: ${table.size} scope-filtered event(s) consistent between the invariant table and the declaration docs.`) diff --git a/tsconfig.base.json b/tsconfig.base.json index ddba51c53f..36401b77f7 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -45,6 +45,7 @@ "./packages/bash/*/src", "./packages/code-runtime/*/src", "./packages/fs/*/src", + "./packages/skill/*/src", "./packages/compact/*/src", "./packages/guard/*/src", "./packages/subagent/*/src", @@ -53,6 +54,7 @@ "./packages/timeout/*/src", "./packages/todo/*/src", "./packages/cordis/*/src", + "./packages/sandbox/*/src", "./packages/hooks/*/src", "./packages/session-persistence/*/src", "./packages/ui/*/src", diff --git a/tsconfig.build.json b/tsconfig.build.json index edbd2d742b..9c236fe02a 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -14,13 +14,18 @@ { "path": "./packages/util/timeout" }, { "path": "./packages/llm/llm" }, { "path": "./packages/core/session" }, + { "path": "./packages/core/scope" }, { "path": "./packages/session-persistence/session-persistence" }, { "path": "./packages/session-persistence/session-persistence-jsonl" }, { "path": "./packages/session-persistence/session-persistence-sqlite" }, { "path": "./packages/core/system-prompt" }, { "path": "./packages/core/agent" }, { "path": "./packages/ui/user-interaction" }, + { "path": "./packages/ui/user-approval" }, { "path": "./packages/core/tools" }, + { "path": "./packages/skill/skill" }, + { "path": "./packages/skill/skill-local" }, + { "path": "./packages/skill/tool-skill" }, { "path": "./packages/ui/tool-ask-user" }, { "path": "./packages/core/agent-loop" }, { "path": "./packages/core/agent-core" }, @@ -32,6 +37,9 @@ { "path": "./packages/llm/llm-deepseek" }, { "path": "./packages/llm/llm-pi-ai" }, { "path": "./packages/bash/bash-local" }, + { "path": "./packages/sandbox/sandbox" }, + { "path": "./packages/sandbox/sandbox-local" }, + { "path": "./packages/bash/bash-sandbox" }, { "path": "./packages/bash/tool-bash" }, { "path": "./packages/fs/fs" }, { "path": "./packages/fs/fs-local" }, diff --git a/tsconfig.json b/tsconfig.json index 61d510437c..2ab1a5da30 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,13 +25,18 @@ { "path": "./packages/util/timeout" }, { "path": "./packages/llm/llm" }, { "path": "./packages/core/session" }, + { "path": "./packages/core/scope" }, { "path": "./packages/session-persistence/session-persistence" }, { "path": "./packages/session-persistence/session-persistence-jsonl" }, { "path": "./packages/session-persistence/session-persistence-sqlite" }, { "path": "./packages/core/system-prompt" }, { "path": "./packages/core/agent" }, { "path": "./packages/ui/user-interaction" }, + { "path": "./packages/ui/user-approval" }, { "path": "./packages/core/tools" }, + { "path": "./packages/skill/skill" }, + { "path": "./packages/skill/skill-local" }, + { "path": "./packages/skill/tool-skill" }, { "path": "./packages/ui/tool-ask-user" }, { "path": "./packages/core/agent-loop" }, { "path": "./packages/core/agent-core" }, @@ -41,6 +46,9 @@ { "path": "./packages/llm/llm-deepseek" }, { "path": "./packages/llm/llm-pi-ai" }, { "path": "./packages/bash/bash-local" }, + { "path": "./packages/sandbox/sandbox" }, + { "path": "./packages/sandbox/sandbox-local" }, + { "path": "./packages/bash/bash-sandbox" }, { "path": "./packages/bash/tool-bash" }, { "path": "./packages/fs/fs" }, { "path": "./packages/fs/fs-local" }, diff --git a/tsdown.config.ts b/tsdown.config.ts index 7b172180d1..9479c5c91d 100644 --- a/tsdown.config.ts +++ b/tsdown.config.ts @@ -12,7 +12,8 @@ import { defineConfig } from 'tsdown' export default defineConfig({ // Explicit globs: `workspace: true` would also discover examples (any // package.json), but only vendor and the packages hierarchy are pnpm - // workspaces. + // workspaces. The Landlock launcher platform packages ship a prebuilt + // native binary and no JavaScript — nothing to bundle. workspace: ['vendor/*', 'packages/*/*'], entry: ['lib/types/index.js'], outDir: 'lib', diff --git a/vendor/README.md b/vendor/README.md index bf0f0b5a8c..0a709495db 100644 --- a/vendor/README.md +++ b/vendor/README.md @@ -35,6 +35,7 @@ Keep this log exhaustive — every divergence from upstream must be listed. 3. **All `tsconfig.json` files**: regenerated to extend the repo-root `tsconfig.base.json`, emit TypeScript intermediates to `lib/types`, and declare project references. 4. **Vendored TypeScript source internal specifiers**: changed local relative imports/exports from upstream's specifier shape to explicit `.ts` specifiers so TypeScript rewrites emitted JS to `.js` while declarations keep explicit, NodeNext-safe `.ts` specifiers. This includes `loader/src/config/isolate.ts` using `declare module './entry.ts'`. 5. **`schemastery/tsdown.config.ts` and `logger-console/tsdown.config.ts`**: ours, not upstream files — per-package build-shape overrides (dual ESM+CJS output; separate node/browser entries) for the repo-root tsdown build. They read the JS emitted under `lib/types` and then write the publish runtime entries under `lib/`. Like the regenerated tsconfigs, they are not part of the upstream sync surface. +6. **`cordis/src/fiber.ts` lifecycle hardening**: locally closes three reentrant disposal gaps. An effect's owner-list wrapper is registered before its setup body runs, so an unload begun from inside setup awaits setup and every collected cleanup; synchronous setup failure removes the wrapper and rolls back collected cleanup. Async cleanup stays owner-visible until quiescence, and Cordis's internal effect composition joins an already-running cleanup while repeated public disposer calls retain their upstream single-shot result. Effect creation is rejected while the owner is `UNLOADING` (while `PENDING` and `LOADING` remain legal), preventing cleanup-time registrations from escaping the unload snapshot. Child fibers register and receive their parent-owned disposer before `internal/plugin` publication, resolve dependency declarations added by that notification before activation, drain effects attached while pending, skip plugin execution when reentrant disposal invalidates the load epoch before its first checkpoint, and contain teardown-notification failures per observer so one callback cannot starve peers or interrupt ownership cleanup. ## Sync procedure diff --git a/vendor/cordis/src/fiber.ts b/vendor/cordis/src/fiber.ts index fd472e7733..7e7766b48d 100644 --- a/vendor/cordis/src/fiber.ts +++ b/vendor/cordis/src/fiber.ts @@ -80,6 +80,35 @@ interface EffectRunner { getOuterStack: () => string[] } +// Public effect disposers remain single-shot, but structural owners and outer +// effects must still be able to join a cleanup that another caller started. +const effectInertia = new WeakMap void | Promise>() + +function runDisposable(dispose: Disposable) { + const result = dispose() + return effectInertia.get(dispose)?.() ?? result +} + +/** Notify plugin teardown without allowing one observer to break ownership cleanup. */ +function emitPluginDisposed(context: Context, fiber: Fiber) { + const args: any[] = ['internal/plugin', fiber] + let callbacks: Function[] + try { + callbacks = context.events.dispatch('emit', args) + } catch (error) { + context.logger.error(error) + return + } + for (const callback of callbacks) { + try { + const returned = callback(...args) + void Promise.resolve(returned).catch(error => context.logger.error(error)) + } catch (error) { + context.logger.error(error) + } + } +} + /** Lifecycle state for one plugin fiber. */ export const enum FiberState { PENDING, @@ -175,24 +204,19 @@ export class Fiber { collect, } - this.context.emit('internal/plugin', this) - - for (const name of Object.keys(this.inject)) { - this._checkImpl(name) - } - + let shouldRefresh = false this.dispose = parent.fiber.effect(() => { const remove = runtime.fibers.push(this) try { this.config = resolveConfig(runtime, config) - this._refresh() + shouldRefresh = true } catch (error) { this.ctx.logger.error(error) this._error = error } return async () => { this.uid = null - this.context.emit('internal/plugin', this) + emitPluginDisposed(this.context, this) if (this.ctx.registry.has(runtime.callback)) { remove() if (!runtime.fibers.length) { @@ -200,6 +224,16 @@ export class Fiber { } } this._setEpoch(INACTIVE) + // A PENDING fiber can already own effects registered by an + // internal/plugin observer. Its epoch is still INACTIVE, so + // _setEpoch() has no transition to drive; explicitly unload that + // pre-activation work before reporting disposal complete. + if (!this.inertia) { + this._updateState(() => { + this.inertia = this._unload() + return FiberState.UNLOADING + }) + } // `this.inertia` itself should never reject — both `_reload` and // `_unload` swallow their own work errors via `ctx.logger.error`. // If it *does* reject, the only remaining cause is the logger @@ -211,6 +245,28 @@ export class Fiber { } } }, 'ctx.plugin()') + + try { + // Publish only after the parent owns a fully assigned disposer. A + // synchronous observer may dispose either this fiber or its parent. + this.context.emit('internal/plugin', this) + } catch (error) { + // Publication failed synchronously. The disposer removes the child + // from both the parent and runtime before control escapes. + void Promise.resolve(this.dispose()).catch(reason => this.ctx.logger.error(reason)) + throw error + } + + // Keep the initial notification's historical PENDING view. The loader + // may also extend `inject` in that notification, so resolve dependencies + // only after publication. A reentrant parent unload makes the child + // disposer responsible for draining any PENDING effects instead. + if (this.uid !== null && parent.fiber.state !== FiberState.UNLOADING) { + for (const name of Object.keys(this.inject)) { + this._checkImpl(name) + } + if (shouldRefresh) this._refresh() + } } else { this.uid = 0 this.ctx = this.context = parent @@ -292,21 +348,28 @@ export class Fiber { effect(execute: () => Effect, label?: string): AsyncDisposable> effect(execute: () => Effect, label = 'anonymous'): any { this.assertActive() + if (this.state === FiberState.UNLOADING) { + throw new CordisError('INACTIVE_EFFECT') + } const disposables: Disposable[] = [] + let disposing = false + let disposalTask: void | Promise const dispose = () => { + if (disposing) return disposalTask + disposing = true let task!: void | Promise - for (const dispose of disposables.splice(0).reverse()) { + for (const disposable of disposables.splice(0).reverse()) { if (task) { - task = task.then(dispose) + task = task.then(() => runDisposable(disposable)) } else { - const result = dispose() + const result = runDisposable(disposable) if (isObject(result) && 'then' in result) { task = result as any } } } - return task + return disposalTask = task } const meta: EffectMeta = { label, children: [] } @@ -324,34 +387,107 @@ export class Fiber { } let task: void | Promise + let executing = true + let resolveSetup: (() => void) | undefined + let rejectSetup: ((reason: unknown) => void) | undefined + let setupBarrier: Promise | undefined + let setupFailed = false + let inFlight: void | Promise + let removeWrapper = () => false + + const waitForSetup = () => { + setupBarrier ??= new Promise((resolve, reject) => { + resolveSetup = resolve + rejectSetup = reject + }) + return setupBarrier + } + + const disposeAfter = (setup: PromiseLike) => { + return Promise.resolve(setup).then( + () => dispose(), + async (reason) => { + await dispose() + throw reason + }, + ) + } + + const finalizeDisposal = (callback: () => void | Promise) => { + let result: void | Promise + try { + result = callback() + } catch (error) { + removeWrapper() + throw error + } + if (isObject(result) && 'then' in result) { + const pending = Promise.resolve(result).finally(() => { + removeWrapper() + if (inFlight === pending) inFlight = undefined + }) + return inFlight = pending + } + removeWrapper() + return result + } + + const wrapper = defineProperty(() => { + // A synchronous setup failure can race an owner unload that already + // captured this wrapper but has not invoked it yet. The failed effect is + // never returned publicly, so let that internal caller await rollback. + if (!runner.epoch) return setupFailed ? inFlight : undefined + runner.epoch = false + return finalizeDisposal(() => { + if (executing) return disposeAfter(waitForSetup()) + return task ? disposeAfter(task) : dispose() + }) + }, symbols.effect, meta) as AsyncDisposable + effectInertia.set(wrapper, () => inFlight) + + // Make the effect visible to a reentrant owner unload before execute() + // runs any plugin code. Async teardown stays owner-visible until it + // settles, allowing an outer effect to join cleanup another caller began. + removeWrapper = this._disposables.push(wrapper) try { task = this._execute(runner) } catch (reason) { - dispose() + executing = false + setupFailed = true + runner.epoch = false + let cleanup: void | Promise + try { + cleanup = finalizeDisposal(dispose) + } finally { + rejectSetup?.(reason) + } + if (isObject(cleanup) && 'then' in cleanup) { + cleanup.catch(error => this.ctx.logger.error(error)) + } throw reason } + executing = false + if (setupBarrier) { + Promise.resolve(task).then(resolveSetup, rejectSetup) + } // prevent unhandled rejection — both from `task` itself and from the // disposer chain if it fails to settle cleanly. - task?.catch(dispose).catch((error) => this.ctx.logger.error(error)) - - const wrapper = defineProperty(() => { - if (!runner.epoch) return - runner.epoch = false - return task ? task.then(dispose) : dispose() - }, symbols.effect, meta) as AsyncDisposable + task?.catch(() => { + if (!runner.epoch) return dispose() + return finalizeDisposal(dispose) + }).catch((error) => this.ctx.logger.error(error)) const disposeAsync = () => { if (!runner.epoch) return runner.epoch = false - return dispose() + return finalizeDisposal(dispose) } wrapper.then = async (onFulfilled, onRejected) => { return Promise.resolve(task) .then(() => disposeAsync) .then(onFulfilled, onRejected) } - disposables.push(this._disposables.push(wrapper)) return wrapper } @@ -434,7 +570,12 @@ export class Fiber { const oldEpoch = this._runner.epoch try { await Promise.resolve() - await this._execute(this._runner) + // A disposer queued before this checkpoint may already have invalidated + // the load. Do not run plugin code for a stale epoch; the state update + // below will drain any effects collected while the fiber was PENDING. + if (this._runner.epoch === oldEpoch) { + await this._execute(this._runner) + } } catch (reason) { // impl guarantees that the error is non-null (?) this.ctx.logger.error(reason) @@ -457,7 +598,7 @@ export class Fiber { await composeError(async (info) => { await Promise.resolve() info.error = new Error() - await dispose() + await runDisposable(dispose) }, this._runner.getOuterStack) } catch (reason) { this.ctx.logger.error(reason) diff --git a/vitest.snapshot.config.ts b/vitest.snapshot.config.ts index ecc8d911aa..d0a8ce6a54 100644 --- a/vitest.snapshot.config.ts +++ b/vitest.snapshot.config.ts @@ -7,10 +7,14 @@ import { defineConfig } from 'vitest/config' // normalized stdout transcript + re-persisted log against committed goldens. // `pnpm run test:snapshot:record` (DSH_SNAPSHOT=record + -u) re-records the // fixtures against the real API and refreshes the goldens. +// `pnpm run test:snapshot:refresh` (DSH_SNAPSHOT=refresh) stays keyless: it +// replays the committed model scripts and writes the current stdout/log goldens +// without calling the live LLM. // // Replay loads no .env (it must never reach the network — a recorded fixture -// drives the model). Record reads DEEPSEEK_API_KEY from the env or a gitignored -// repo-root .env, so a contributor with a key only in .env can still record. +// drives the model), and refresh uses that same keyless replay path. Record +// reads DEEPSEEK_API_KEY from the env or a gitignored repo-root .env, so a +// contributor with a key only in .env can still record. if (process.env.DSH_SNAPSHOT === 'record') { try { process.loadEnvFile(new URL('.env', import.meta.url).pathname)