diff --git a/.agents/notes/implemented/architecture/2026-07-31-claimed-pre-step-inbox-lifecycle.i18n.yaml b/.agents/notes/implemented/architecture/2026-07-31-claimed-pre-step-inbox-lifecycle.i18n.yaml index 9b41e2e0cf..1b529c379b 100644 --- a/.agents/notes/implemented/architecture/2026-07-31-claimed-pre-step-inbox-lifecycle.i18n.yaml +++ b/.agents/notes/implemented/architecture/2026-07-31-claimed-pre-step-inbox-lifecycle.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 .agents/notes/implemented/architecture/2026-07-31-claimed-pre-step-inbox-lifecycle.md -2026-07-31-claimed-pre-step-inbox-lifecycle.md: 73768e1eee8957f8976d40812b0a31a2961f0825 -2026-07-31-claimed-pre-step-inbox-lifecycle.zh.md: 343816abaf394b8f64924cf36b753c6b1b2e34ca +2026-07-31-claimed-pre-step-inbox-lifecycle.md: 737e3835263a3215a0fd2e52dad4ee05402bd888 +2026-07-31-claimed-pre-step-inbox-lifecycle.zh.md: ecb731df663e0d48b374a3118d7db7f6a34bfc18 diff --git a/.agents/notes/implemented/architecture/2026-07-31-claimed-pre-step-inbox-lifecycle.md b/.agents/notes/implemented/architecture/2026-07-31-claimed-pre-step-inbox-lifecycle.md index 73768e1eee..737e383526 100644 --- a/.agents/notes/implemented/architecture/2026-07-31-claimed-pre-step-inbox-lifecycle.md +++ b/.agents/notes/implemented/architecture/2026-07-31-claimed-pre-step-inbox-lifecycle.md @@ -12,17 +12,19 @@ Occurrence-local inbox wrappers also duplicated the identity already carried by ## Decision -Before every proposed step, `Inbox.claim(target)` atomically removes the complete batch: all `next-step` messages and, at a turn boundary, one `next-turn` message. At the initial boundary the loop first commits `turn/start`, so the claim and its single `agent/pre-step` decision have durable turn ownership. Claiming records normalized `agent/inbox/spliced` pure deletions with no outcome. The loop then emits `agent/inbox/claimed { message, turn }` once per claimed message and awaits the waterfall with that exclusive batch and `{ turn, step, signal }`. +Before every proposed step, the loop's package-internal `ReactLoopInbox` atomically claims the complete batch: all `next-step` messages and, at a turn boundary, one `next-turn` message. At the initial boundary the loop first commits `turn/start`, so the claim and its single `agent/pre-step` decision have durable turn ownership. Claiming records normalized `agent/inbox/spliced` pure deletions with no outcome, emits `agent/inbox/claimed { message, turn }` once per claimed message, and returns the exclusive batch for the loop's waterfall with `{ turn, step, signal }`. `PreStepDecision` is `{ kind: 'reject' } | { kind: 'enter'; messages: UserMessage[] }`. Reject opens no step, leaves the claimed batch removed, and closes the turn as blocked without any step events. Empty entry, cancellation, and failure before `step/start` likewise close a balanced no-step turn. Enter supplies the complete batch appended as `user/message` events after `step/start`. A listener wrapping `next()` preserves downstream changes unless it intentionally replaces them, so all message rewrites settle once in the final return value. There is no `agent/prompt-prepare`, `agent/prompt-submit`, or `agent/step` extension point. -The durable inbox remains two `UserMessage[]` lists addressed by `MessageId`. `append`, `prepend`, and `splice` take a target, while `replace(messageId, newMessage)` and `remove(messageId)` locate the pending message across both lists before committing a normalized splice. Replacement may change identity and emits the old message as discarded followed by the new message as inserted. Every insertion emits `agent/inbox/inserted { message }`; an ordinary removal records `outcome: 'canceled'` and emits `agent/inbox/discarded { message }`. Claiming is the loop's internal step-boundary operation on the inbox and records pure deletions without notifications or an outcome, so the loop can publish claimed events itself. These live events add no placement, outcome, or batch fields. +The durable inbox remains two `UserMessage[]` lists addressed by `MessageId`. `append`, `prepend`, and `splice` take a target, while `replace(messageId, newMessage)` and `remove(messageId)` locate the pending message across both lists before committing a normalized splice. Replacement may change identity and emits the old message as discarded followed by the new message as inserted. Every insertion emits `agent/inbox/inserted { message }`; an ordinary removal records `outcome: 'canceled'` and emits `agent/inbox/discarded { message }`. Claiming records pure deletions without an outcome and emits claimed events from `ReactLoopInbox`. These live events add no placement, outcome, or batch fields. -The two event surfaces have separate consumers. Observers following one message use `agent/inbox/inserted`, `claimed`, and `discarded`. Whole-queue consumers, including the Web queue projection and reconnect baseline, use the durable `agent/inbox/spliced` stream; UI edits and removals route through `Inbox.splice()` or another Inbox mutation method so the same projection records every change. +`Agent.inbox` exposes only the structural `Inbox` interface for reading and mutating pending work; loop-only `hasPending` and claim operations are absent from that public face. dsh-agent-loop constructs one `ReactLoopInbox` and uses it for both structural commands and driver operations. The concrete constructor receives `SessionProjectionRegistry` directly instead of the wider Cordis `Context` and registers the standard definition on the agent scope before its first read. `AgentLoop` requires the registry service at activation, and the registry reference-counts the definition across live agent scopes. + +The two event surfaces have separate consumers. Observers following one message use `agent/inbox/inserted`, `claimed`, and `discarded`. Each `ReactLoopInbox` contributes the standard `inbox` projection over the durable `agent/inbox/spliced` stream from its agent scope; UI edits and removals route through an Inbox mutation method so the same projection records every change. When that projection reconstructs durable history, it rejects unsafe or out-of-range coordinates and duplicate `MessageId` values across both lists, and reports the offending event seq. Whole-queue control consumers use the projection change feed: the Session controller publishes the projection frame, then derives the queue replacement from the same post-fold inbox value. Plugins that need current-step atomic rewriting return messages from `agent/pre-step`. Plugins that only need later context may mutate `agent.inbox` directly. Workspace context uses both paths: asynchronous filesystem projections stage one replaceable `next-step` item, while the next entering pre-step folds that item or a newly composed baseline into its final batch and removes the pending copy. Rejection keeps the item queued. -The archived [addressable queue occurrence decision](../../archived/feature/2026-07-29-addressable-queue-operations.md) describes the superseded occurrence-wrapper design. `MessageId` now owns addressability, while the retained Host queue mirror derives its snapshots from the durable splice projection. +The archived [addressable queue occurrence decision](../../archived/feature/2026-07-29-addressable-queue-operations.md) describes the superseded occurrence-wrapper design. `MessageId` owns addressability, while `ReactLoopInbox` contributes `inbox` as the standard session projection over durable splices. The generic projection carrier serves that fold for live updates, history-tail reconnect baselines, and cold process-restart recovery without a live Agent mirror. ## Alternatives considered @@ -34,7 +36,7 @@ The archived [addressable queue occurrence decision](../../archived/feature/2026 ## Verification -Agent-loop coverage pins turn-start-before-claim-before-pre-step ordering, exact live event payloads, balanced no-step rejection, final-batch rewriting, input inserted after a claim, listener failure, and cancellation. Inbox and consumer tests pin pure claim deletions, canceled ordinary removals, agent-instructions staging, replacement, and same-step entry, plan/goal/hook behavior, UI cleanup, compaction, checkpointing, and resumed durable projection. Generated event and type catalogs expose only the new waterfall and payloads. +Agent-loop coverage pins turn-start-before-claim-before-pre-step ordering, exact live event payloads, balanced no-step rejection, final-batch rewriting, input inserted after a claim, listener failure, cancellation, and agent-scope projection removal after the last owner unloads. Inbox and consumer tests pin pure claim deletions, canceled ordinary removals, agent-instructions staging, replacement, and same-step entry, plan/goal/hook behavior, UI cleanup, compaction, checkpointing, resumed durable projection, rejection of invalid persisted coordinates or cross-list identities, and post-fold queue replacement when the controller registers before the projection registry. Consumer-domain tests use a process-local Inbox stub only when durability is outside the test subject; claiming, durable projection, recovery, validation, and live-notification tests create Agents through the production AgentLoop test harness, so test support never reimplements the projection. Generated event and type catalogs expose only the new waterfall and payloads. ## Consequences diff --git a/.agents/notes/implemented/architecture/2026-07-31-claimed-pre-step-inbox-lifecycle.zh.md b/.agents/notes/implemented/architecture/2026-07-31-claimed-pre-step-inbox-lifecycle.zh.md index 343816abaf..ecb731df66 100644 --- a/.agents/notes/implemented/architecture/2026-07-31-claimed-pre-step-inbox-lifecycle.zh.md +++ b/.agents/notes/implemented/architecture/2026-07-31-claimed-pre-step-inbox-lifecycle.zh.md @@ -12,17 +12,19 @@ Status: implemented ## 决策 -每个拟议步骤之前,`Inbox.claim(target)` 会原子移除完整批次:全部 `next-step` 消息,以及轮次边界上的一条 `next-turn` 消息。在首次边界,循环会先提交 `turn/start`,使领取及其唯一一次 `agent/pre-step` 决策拥有持久轮次归属。领取会记录规范化、不带 outcome 的纯删除 `agent/inbox/spliced`。随后,循环针对每条已领取消息发出一次 `agent/inbox/claimed { message, turn }`,并用该独占批次与 `{ turn, step, signal }` 等待 waterfall(瀑布式事件)。 +每个拟议步骤之前,循环包内部的 `ReactLoopInbox` 会原子领取完整批次:全部 `next-step` 消息,以及轮次边界上的一条 `next-turn` 消息。在首次边界,循环会先提交 `turn/start`,使领取及其唯一一次 `agent/pre-step` 决策拥有持久轮次归属。领取会记录规范化、不带 outcome 的纯删除 `agent/inbox/spliced`,针对每条已领取消息发出一次 `agent/inbox/claimed { message, turn }`,并把独占批次返回给循环,由后者用 `{ turn, step, signal }` 等待 waterfall(瀑布式事件)。 `PreStepDecision` 为 `{ kind: 'reject' } | { kind: 'enter'; messages: UserMessage[] }`。reject 不会打开步骤,会让已领取批次保持已删除,并将轮次关闭为 blocked,且不产生任何步骤事件。空的 enter、取消以及 `step/start` 前的失败同样会关闭一个边界平衡的无步骤轮次。enter 提供在 `step/start` 后以 `user/message` 追加的完整批次。包装 `next()` 的监听器会保留下游变更,除非有意替换,因此全部消息改写只在最终返回值中一次性结算。系统不再存在 `agent/prompt-prepare`、`agent/prompt-submit` 或 `agent/step` 扩展点。 -持久 inbox 仍是两份通过 `MessageId` 寻址的 `UserMessage[]` 列表。`append`、`prepend` 与 `splice` 接受 target;`replace(messageId, newMessage)` 与 `remove(messageId)` 则在提交规范化 splice 前,通过 `MessageId` 跨两份列表定位待处理消息。替换可以改变标识,并先将旧消息作为 discarded 发布,再将新消息作为 inserted 发布。每次插入发出 `agent/inbox/inserted { message }`;普通删除记录 `outcome: 'canceled'` 并发出 `agent/inbox/discarded { message }`。领取是循环在 inbox 上的内部步骤边界操作,记录不带通知或 outcome 的纯删除,因此循环可以自行发布 claimed 事件。这些实时事件不增加 placement、outcome 或批次字段。 +持久 inbox 仍是两份通过 `MessageId` 寻址的 `UserMessage[]` 列表。`append`、`prepend` 与 `splice` 接受 target;`replace(messageId, newMessage)` 与 `remove(messageId)` 则在提交规范化 splice 前,通过 `MessageId` 跨两份列表定位待处理消息。替换可以改变标识,并先将旧消息作为 discarded 发布,再将新消息作为 inserted 发布。每次插入发出 `agent/inbox/inserted { message }`;普通删除记录 `outcome: 'canceled'` 并发出 `agent/inbox/discarded { message }`。领取记录不带 outcome 的纯删除,并由 `ReactLoopInbox` 发出 claimed 事件。这些实时事件不增加 placement、outcome 或批次字段。 -两类事件接口服务不同消费方。跟踪单条消息的观察方使用 `agent/inbox/inserted`、`claimed` 与 `discarded`。包括 Web 队列投影和重连基线在内的整体队列消费方使用持久 `agent/inbox/spliced` 流;UI 编辑与移除通过 `Inbox.splice()` 或其他 Inbox 变更方法处理,从而让同一投影记录所有变化。 +`Agent.inbox` 只暴露用于读取和变更待处理工作的结构化 `Inbox` 接口;仅供循环使用的 `hasPending` 与领取操作不在该公开接口上。dsh-agent-loop 只构造一个 `ReactLoopInbox`,同时用于结构化命令与驱动器操作。具体构造函数直接接收 `SessionProjectionRegistry`,而不是更宽泛的 Cordis `Context`,并在首次读取前从 agent 作用域注册标准定义。`AgentLoop` 激活时要求该注册表服务存在,注册表则对多个 live agent 作用域贡献的定义进行引用计数。 + +两类事件接口服务不同消费方。跟踪单条消息的观察方使用 `agent/inbox/inserted`、`claimed` 与 `discarded`。每个 `ReactLoopInbox` 都从其 agent 作用域在持久 `agent/inbox/spliced` 流上贡献标准 `inbox` 投影;UI 编辑与移除通过 Inbox 变更方法处理,从而让同一投影记录所有变化。该投影重建持久历史时,会拒绝不安全或越界的坐标,以及跨两份列表重复的 `MessageId`,并报告出错事件的 seq。整体队列的 control 消费方使用投影变更流:Session controller 先发布 projection frame,再从同一份折叠后的 inbox 值派生 queue replacement。 必须对当前步骤进行原子改写的插件从 `agent/pre-step` 返回消息。只需要稍后上下文的插件可以直接修改 `agent.inbox`。Workspace context 同时使用两条路径:异步文件系统投影会暂存一条可替换的 `next-step` 消息,而下一次进入步骤的 pre-step 会把该消息或新组合的基线折入最终批次,并移除仍待处理的副本。reject 会让该条目继续排队。 -已归档的[可寻址队列项决策](../../archived/feature/2026-07-29-addressable-queue-operations.md)描述了已被取代的单次出现包装层设计。现在由 `MessageId` 负责寻址,而保留的 Host 队列镜像根据持久 splice 投影派生快照。 +已归档的[可寻址队列项决策](../../archived/feature/2026-07-29-addressable-queue-operations.md)描述了已被取代的单次出现包装层设计。`MessageId` 负责寻址,而 `ReactLoopInbox` 把 `inbox` 作为持久 splice 上的标准会话投影贡献给投影注册表。通用投影传输层会将该折叠结果用于实时更新、历史尾页的重连基线和冷进程重启恢复,无需 live Agent 镜像。 ## 曾考虑的替代方案 @@ -34,7 +36,7 @@ Status: implemented ## 验证 -agent loop(智能体循环)覆盖固定先 `turn/start`、再领取、后 pre-step 的顺序、实时事件的确切载荷、边界平衡的无步骤 reject、最终批次改写、领取后插入的输入、监听器失败与取消。Inbox 和消费方测试固定纯领取删除、普通删除的 canceled 结果、agent-instructions 的暂存、替换与同一步骤进入、plan/goal/钩子行为、UI 清理、压缩(compaction)、检查点以及恢复后的持久投影。生成的事件与类型目录只公开新的 waterfall 与载荷。 +agent loop(智能体循环)覆盖固定先 `turn/start`、再领取、后 pre-step 的顺序、实时事件的确切载荷、边界平衡的无步骤 reject、最终批次改写、领取后插入的输入、监听器失败、取消,以及最后一个所有者卸载后移除 agent 作用域投影。Inbox 和消费方测试固定纯领取删除、普通删除的 canceled 结果、agent-instructions 的暂存、替换与同一步骤进入、plan/goal/钩子行为、UI 清理、压缩(compaction)、检查点、恢复后的持久投影、对非法持久坐标或跨列表重复标识的拒绝,以及 controller 早于投影注册表注册时仍使用折叠后队列值。只有当持久性不属于测试对象时,消费方领域测试才使用进程内 Inbox 桩;领取、持久投影、恢复、校验与实时通知测试通过生产 AgentLoop 测试 harness 创建 Agent,因此测试支持代码不会重新实现该投影。生成的事件与类型目录只公开新的 waterfall 与载荷。 ## 后果 diff --git a/apps/cli/package.json b/apps/cli/package.json index 54cc008bc7..c9807d7941 100644 --- a/apps/cli/package.json +++ b/apps/cli/package.json @@ -104,6 +104,8 @@ "@agentclientprotocol/sdk": "1.4.0", "@deepseek-ai/dsh-acp": "workspace:^", "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-attachment-local": "workspace:^", "@deepseek-ai/dsh-bash-local": "workspace:^", "@deepseek-ai/dsh-credentials-local": "workspace:^", diff --git a/apps/cli/tests/fixtures/dsh-badge/snapshot.ts b/apps/cli/tests/fixtures/dsh-badge/snapshot.ts index fc7aa1c8b4..4c2331ed0b 100644 --- a/apps/cli/tests/fixtures/dsh-badge/snapshot.ts +++ b/apps/cli/tests/fixtures/dsh-badge/snapshot.ts @@ -1,10 +1,11 @@ import { fileURLToPath } from 'node:url' -import { agentEvents, Inbox, type Agent } from '@deepseek-ai/dsh-agent' +import { agentEvents, type Agent } from '@deepseek-ai/dsh-agent' import { ToolCallId } from '@deepseek-ai/dsh-llm' import { boot, loadOverlayPatches } from '@deepseek-ai/dsh-app-boot' import { SessionId } from '@deepseek-ai/dsh-session' import type {} from '@deepseek-ai/dsh-skill' import type {} from '@deepseek-ai/dsh-tools' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' const overlayPath = process.argv[2] if (overlayPath === undefined) throw new Error('dsh-badge snapshot requires an overlay path') @@ -23,7 +24,7 @@ try { id: agentId, options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: unsupportedInbox(), status: 'idle', send: () => {}, followup: () => {}, diff --git a/apps/cli/tests/profiles/headless/tests/harness.ts b/apps/cli/tests/profiles/headless/tests/harness.ts index fd8fa81384..ee98abba6b 100644 --- a/apps/cli/tests/profiles/headless/tests/harness.ts +++ b/apps/cli/tests/profiles/headless/tests/harness.ts @@ -2,7 +2,6 @@ import { Context } from '@deepseek-ai/cordis' import type { SessionEvent } from '@deepseek-ai/dsh-session' import type { Agent } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local' import * as BashEnvPlugin from '@deepseek-ai/dsh-shell-env' @@ -56,7 +55,6 @@ export interface CodingHarnessOptions { export async function codingHarness(workdir: string, options: CodingHarnessOptions = {}): Promise { const ctx = new Context() - await ctx.plugin(SessionProjectionRegistry) await mountAgentLoopTestDependencies(ctx, { systemPrompt: { personaPrefix: options.personaPrefix ?? '' }, }) diff --git a/benchmarks/agent-continuation/agent-continuation.worker.ts b/benchmarks/agent-continuation/agent-continuation.worker.ts index 7e676e9971..91faa5f347 100644 --- a/benchmarks/agent-continuation/agent-continuation.worker.ts +++ b/benchmarks/agent-continuation/agent-continuation.worker.ts @@ -10,7 +10,6 @@ import { createUserMessage, LlmAdapter } from '@deepseek-ai/dsh-llm' import type { GenerateOptions, LlmResolvedModelInfo, StreamChunk } from '@deepseek-ai/dsh-llm' import { SESSION_FORMAT_VERSION } from '@deepseek-ai/dsh-session' import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { defineContentToolFixture } from '@deepseek-ai/dsh-tools' import { assertBuiltBenchmarkRuntime } from '../support/built-worker.ts' import { PARENT_ID, response, resultText, syntheticHistory, TIME_ZERO, WORKLOAD } from './workload.ts' @@ -82,7 +81,6 @@ async function measure(root: string, scenario: string): Promise { const ctx = new Context() - await ctx.plugin(SessionProjectionRegistry) const agentScenario = scenario === 'agent-resume' if (agentScenario) await mountAgentLoopTestDependencies(ctx) - else await ctx.plugin(SessionStore) + else { + await ctx.plugin(SessionProjectionRegistry) + await ctx.plugin(SessionStore) + } await installProjectionSet(ctx, agentScenario) await ctx.plugin(JsonlSessionPersistence, { root, compression: 'zstd' }) let history: SessionHistoryController | undefined diff --git a/docs/event-producer-consumer.i18n.yaml b/docs/event-producer-consumer.i18n.yaml index f0995f4194..df5d25aa81 100644 --- a/docs/event-producer-consumer.i18n.yaml +++ b/docs/event-producer-consumer.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 docs/event-producer-consumer.md -event-producer-consumer.md: c352fe57795ee3c0c8f8a4adef0b06ff109dec76 -event-producer-consumer.zh.md: 5a091d09ca9ba8ea9ee3274096b1cca181768cfe +event-producer-consumer.md: edf5ea33526afb46e941314852dea653105fb748 +event-producer-consumer.zh.md: f7d96a94eef611817e9a9c0c2dcc93d62ea3a178 diff --git a/docs/event-producer-consumer.md b/docs/event-producer-consumer.md index c352fe5779..edf5ea3352 100644 --- a/docs/event-producer-consumer.md +++ b/docs/event-producer-consumer.md @@ -9,19 +9,19 @@ This matrix shows which packages dispatch each harness-owned event and which pac | --- | --- | --- | --- | --- | | `agent-loop/config-start-failed` | `emit` | [`packages/core/agent-loop/src/index.ts:246`](../packages/core/agent-loop/src/index.ts) | [`agent-loop`](../packages/core/agent-loop) (`events.dispatch`) | - | | `agent-preset/selected` | `emit` | [`packages/preset/agent-presets/src/types.ts:80`](../packages/preset/agent-presets/src/types.ts) | [`agent-presets`](../packages/preset/agent-presets) (`emit`) | `remotes` | -| `agent/assistant-stream` | `emit` | [`packages/core/agent/src/runtime-types.ts:315`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`headless`](../packages/bundle/headless), `session-controller` | -| `agent/created` | `emit` | [`packages/core/agent/src/runtime-types.ts:204`](../packages/core/agent/src/runtime-types.ts) | [`agent`](../packages/core/agent) (`events.dispatch`) | [`agent-presets`](../packages/preset/agent-presets), [`file-reference-local`](../packages/context/file-reference-local), [`goal-round-driver`](../packages/goal/goal-round-driver), [`loader-smoke`](../packages/test-support/loader-smoke), [`schedule`](../packages/schedule/schedule), `tool-agent-team`, [`tool-subagent`](../packages/subagent/tool-subagent) | -| `agent/disposed` | `emit` | [`packages/core/agent/src/runtime-types.ts:213`](../packages/core/agent/src/runtime-types.ts) | [`agent`](../packages/core/agent) (`events.dispatch`) | [`agent-loop`](../packages/core/agent-loop), [`file-reference-local`](../packages/context/file-reference-local), [`goal-round-driver`](../packages/goal/goal-round-driver), `session-controller`, [`subagent`](../packages/subagent/subagent), `tool-agent-team`, [`tool-subagent`](../packages/subagent/tool-subagent) | -| `agent/error` | `emit` | [`packages/core/agent/src/runtime-types.ts:345`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`acp`](../packages/acp/acp), [`goal-round-driver`](../packages/goal/goal-round-driver), `session-controller`, [`session-telemetry`](../packages/session/session-telemetry) | -| `agent/inbox/claimed` | `emit` | [`packages/core/agent/src/runtime-types.ts:242`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`acp`](../packages/acp/acp), [`goal-round-driver`](../packages/goal/goal-round-driver), [`subagent`](../packages/subagent/subagent), [`tool-jobs`](../packages/jobs/tool-jobs) | -| `agent/inbox/discarded` | `emit` | [`packages/core/agent/src/runtime-types.ts:250`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`goal-round-driver`](../packages/goal/goal-round-driver), [`subagent`](../packages/subagent/subagent) | -| `agent/inbox/inserted` | `emit` | [`packages/core/agent/src/runtime-types.ts:231`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`goal-round-driver`](../packages/goal/goal-round-driver) | -| `agent/pre-step` | `waterfall` | [`packages/core/agent/src/runtime-types.ts:276`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`agent`](../packages/core/agent), [`agent-instructions`](../packages/context/agent-instructions), [`compaction-basic`](../packages/compaction/compaction-basic), [`goal-round-driver`](../packages/goal/goal-round-driver), [`hooks-claude-code`](../packages/hooks/hooks-claude-code), [`hooks-codex`](../packages/hooks/hooks-codex), [`plan-mode`](../packages/plan/plan-mode), [`repeat-tool-reminder`](../packages/guard/repeat-tool-reminder), [`session-checkpoint-policy`](../packages/session/session-checkpoint-policy), [`session-reference`](../packages/context/session-reference), [`subagent-in-process-driver`](../packages/subagent/subagent-in-process-driver), [`time-context`](../packages/context/time-context), [`tmux-context`](../packages/context/tmux-context), [`tool-cordis`](../packages/extensions/tool-cordis), [`tool-skill`](../packages/skill/tool-skill), [`tool-subagent`](../packages/subagent/tool-subagent) | -| `agent/request` | `waterfall` | [`packages/core/agent/src/runtime-types.ts:289`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`agent`](../packages/core/agent), [`webhook`](../packages/webhook/webhook) | -| `agent/request-error` | `waterfall` | [`packages/core/agent/src/runtime-types.ts:305`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`compaction-basic`](../packages/compaction/compaction-basic), [`llm-retry`](../packages/llm/llm-retry) | -| `agent/session-start` | `emit` | [`packages/core/agent/src/runtime-types.ts:262`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emitAgentEvent`) | `agent-team`, [`goal`](../packages/goal/goal), [`goal-round-driver`](../packages/goal/goal-round-driver), [`hooks-claude-code`](../packages/hooks/hooks-claude-code), [`hooks-codex`](../packages/hooks/hooks-codex) | -| `agent/status` | `emit` | [`packages/core/agent/src/runtime-types.ts:223`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`agent`](../packages/core/agent), `agent-team`, [`compaction-basic`](../packages/compaction/compaction-basic), [`goal-round-driver`](../packages/goal/goal-round-driver), [`schedule`](../packages/schedule/schedule), `server`, `session-controller` | -| `agent/turn-stopping` | `serial` | [`packages/core/agent/src/runtime-types.ts:333`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`serial`) | [`hooks-claude-code`](../packages/hooks/hooks-claude-code), [`hooks-codex`](../packages/hooks/hooks-codex) | +| `agent/assistant-stream` | `emit` | [`packages/core/agent/src/runtime-types.ts:369`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`headless`](../packages/bundle/headless), `session-controller` | +| `agent/created` | `emit` | [`packages/core/agent/src/runtime-types.ts:258`](../packages/core/agent/src/runtime-types.ts) | [`agent`](../packages/core/agent) (`events.dispatch`) | [`agent-presets`](../packages/preset/agent-presets), [`file-reference-local`](../packages/context/file-reference-local), [`goal-round-driver`](../packages/goal/goal-round-driver), [`loader-smoke`](../packages/test-support/loader-smoke), [`schedule`](../packages/schedule/schedule), `tool-agent-team`, [`tool-subagent`](../packages/subagent/tool-subagent) | +| `agent/disposed` | `emit` | [`packages/core/agent/src/runtime-types.ts:267`](../packages/core/agent/src/runtime-types.ts) | [`agent`](../packages/core/agent) (`events.dispatch`) | [`agent-loop`](../packages/core/agent-loop), [`file-reference-local`](../packages/context/file-reference-local), [`goal-round-driver`](../packages/goal/goal-round-driver), `session-controller`, [`subagent`](../packages/subagent/subagent), `tool-agent-team`, [`tool-subagent`](../packages/subagent/tool-subagent) | +| `agent/error` | `emit` | [`packages/core/agent/src/runtime-types.ts:399`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`acp`](../packages/acp/acp), [`goal-round-driver`](../packages/goal/goal-round-driver), `session-controller`, [`session-telemetry`](../packages/session/session-telemetry) | +| `agent/inbox/claimed` | `emit` | [`packages/core/agent/src/runtime-types.ts:296`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`acp`](../packages/acp/acp), [`goal-round-driver`](../packages/goal/goal-round-driver), [`subagent`](../packages/subagent/subagent), [`tool-jobs`](../packages/jobs/tool-jobs) | +| `agent/inbox/discarded` | `emit` | [`packages/core/agent/src/runtime-types.ts:304`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`goal-round-driver`](../packages/goal/goal-round-driver), [`subagent`](../packages/subagent/subagent) | +| `agent/inbox/inserted` | `emit` | [`packages/core/agent/src/runtime-types.ts:285`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`goal-round-driver`](../packages/goal/goal-round-driver) | +| `agent/pre-step` | `waterfall` | [`packages/core/agent/src/runtime-types.ts:330`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`agent`](../packages/core/agent), [`agent-instructions`](../packages/context/agent-instructions), [`compaction-basic`](../packages/compaction/compaction-basic), [`goal-round-driver`](../packages/goal/goal-round-driver), [`hooks-claude-code`](../packages/hooks/hooks-claude-code), [`hooks-codex`](../packages/hooks/hooks-codex), [`plan-mode`](../packages/plan/plan-mode), [`repeat-tool-reminder`](../packages/guard/repeat-tool-reminder), [`session-checkpoint-policy`](../packages/session/session-checkpoint-policy), [`session-reference`](../packages/context/session-reference), [`subagent-in-process-driver`](../packages/subagent/subagent-in-process-driver), [`time-context`](../packages/context/time-context), [`tmux-context`](../packages/context/tmux-context), [`tool-cordis`](../packages/extensions/tool-cordis), [`tool-skill`](../packages/skill/tool-skill), [`tool-subagent`](../packages/subagent/tool-subagent) | +| `agent/request` | `waterfall` | [`packages/core/agent/src/runtime-types.ts:343`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`agent`](../packages/core/agent), [`webhook`](../packages/webhook/webhook) | +| `agent/request-error` | `waterfall` | [`packages/core/agent/src/runtime-types.ts:359`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`compaction-basic`](../packages/compaction/compaction-basic), [`llm-retry`](../packages/llm/llm-retry) | +| `agent/session-start` | `emit` | [`packages/core/agent/src/runtime-types.ts:316`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emitAgentEvent`) | `agent-team`, [`goal`](../packages/goal/goal), [`goal-round-driver`](../packages/goal/goal-round-driver), [`hooks-claude-code`](../packages/hooks/hooks-claude-code), [`hooks-codex`](../packages/hooks/hooks-codex) | +| `agent/status` | `emit` | [`packages/core/agent/src/runtime-types.ts:277`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`agent`](../packages/core/agent), `agent-team`, [`compaction-basic`](../packages/compaction/compaction-basic), [`goal-round-driver`](../packages/goal/goal-round-driver), [`schedule`](../packages/schedule/schedule), `server`, `session-controller` | +| `agent/turn-stopping` | `serial` | [`packages/core/agent/src/runtime-types.ts:387`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`serial`) | [`hooks-claude-code`](../packages/hooks/hooks-claude-code), [`hooks-codex`](../packages/hooks/hooks-codex) | | `api-session/activity` | `emit` | [`packages/api/session-controller/src/types.ts:586`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | | `api-session/added` | `emit` | [`packages/api/session-controller/src/types.ts:566`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | | `api-session/error` | `emit` | [`packages/api/session-controller/src/types.ts:593`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | diff --git a/docs/event-producer-consumer.zh.md b/docs/event-producer-consumer.zh.md index 5a091d09ca..f7d96a94ee 100644 --- a/docs/event-producer-consumer.zh.md +++ b/docs/event-producer-consumer.zh.md @@ -11,19 +11,19 @@ | --- | --- | --- | --- | --- | | `agent-loop/config-start-failed` | `emit` | [`packages/core/agent-loop/src/index.ts:246`](../packages/core/agent-loop/src/index.ts) | [`agent-loop`](../packages/core/agent-loop) (`events.dispatch`) | - | | `agent-preset/selected` | `emit` | [`packages/preset/agent-presets/src/types.ts:80`](../packages/preset/agent-presets/src/types.ts) | [`agent-presets`](../packages/preset/agent-presets) (`emit`) | `remotes` | -| `agent/assistant-stream` | `emit` | [`packages/core/agent/src/runtime-types.ts:315`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`headless`](../packages/bundle/headless), `session-controller` | -| `agent/created` | `emit` | [`packages/core/agent/src/runtime-types.ts:204`](../packages/core/agent/src/runtime-types.ts) | [`agent`](../packages/core/agent) (`events.dispatch`) | [`agent-presets`](../packages/preset/agent-presets), [`file-reference-local`](../packages/context/file-reference-local), [`goal-round-driver`](../packages/goal/goal-round-driver), [`loader-smoke`](../packages/test-support/loader-smoke), [`schedule`](../packages/schedule/schedule), `tool-agent-team`, [`tool-subagent`](../packages/subagent/tool-subagent) | -| `agent/disposed` | `emit` | [`packages/core/agent/src/runtime-types.ts:213`](../packages/core/agent/src/runtime-types.ts) | [`agent`](../packages/core/agent) (`events.dispatch`) | [`agent-loop`](../packages/core/agent-loop), [`file-reference-local`](../packages/context/file-reference-local), [`goal-round-driver`](../packages/goal/goal-round-driver), `session-controller`, [`subagent`](../packages/subagent/subagent), `tool-agent-team`, [`tool-subagent`](../packages/subagent/tool-subagent) | -| `agent/error` | `emit` | [`packages/core/agent/src/runtime-types.ts:345`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`acp`](../packages/acp/acp), [`goal-round-driver`](../packages/goal/goal-round-driver), `session-controller`, [`session-telemetry`](../packages/session/session-telemetry) | -| `agent/inbox/claimed` | `emit` | [`packages/core/agent/src/runtime-types.ts:242`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`acp`](../packages/acp/acp), [`goal-round-driver`](../packages/goal/goal-round-driver), [`subagent`](../packages/subagent/subagent), [`tool-jobs`](../packages/jobs/tool-jobs) | -| `agent/inbox/discarded` | `emit` | [`packages/core/agent/src/runtime-types.ts:250`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`goal-round-driver`](../packages/goal/goal-round-driver), [`subagent`](../packages/subagent/subagent) | -| `agent/inbox/inserted` | `emit` | [`packages/core/agent/src/runtime-types.ts:231`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`goal-round-driver`](../packages/goal/goal-round-driver) | -| `agent/pre-step` | `waterfall` | [`packages/core/agent/src/runtime-types.ts:276`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`agent`](../packages/core/agent), [`agent-instructions`](../packages/context/agent-instructions), [`compaction-basic`](../packages/compaction/compaction-basic), [`goal-round-driver`](../packages/goal/goal-round-driver), [`hooks-claude-code`](../packages/hooks/hooks-claude-code), [`hooks-codex`](../packages/hooks/hooks-codex), [`plan-mode`](../packages/plan/plan-mode), [`repeat-tool-reminder`](../packages/guard/repeat-tool-reminder), [`session-checkpoint-policy`](../packages/session/session-checkpoint-policy), [`session-reference`](../packages/context/session-reference), [`subagent-in-process-driver`](../packages/subagent/subagent-in-process-driver), [`time-context`](../packages/context/time-context), [`tmux-context`](../packages/context/tmux-context), [`tool-cordis`](../packages/extensions/tool-cordis), [`tool-skill`](../packages/skill/tool-skill), [`tool-subagent`](../packages/subagent/tool-subagent) | -| `agent/request` | `waterfall` | [`packages/core/agent/src/runtime-types.ts:289`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`agent`](../packages/core/agent), [`webhook`](../packages/webhook/webhook) | -| `agent/request-error` | `waterfall` | [`packages/core/agent/src/runtime-types.ts:305`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`compaction-basic`](../packages/compaction/compaction-basic), [`llm-retry`](../packages/llm/llm-retry) | -| `agent/session-start` | `emit` | [`packages/core/agent/src/runtime-types.ts:262`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emitAgentEvent`) | `agent-team`, [`goal`](../packages/goal/goal), [`goal-round-driver`](../packages/goal/goal-round-driver), [`hooks-claude-code`](../packages/hooks/hooks-claude-code), [`hooks-codex`](../packages/hooks/hooks-codex) | -| `agent/status` | `emit` | [`packages/core/agent/src/runtime-types.ts:223`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`agent`](../packages/core/agent), `agent-team`, [`compaction-basic`](../packages/compaction/compaction-basic), [`goal-round-driver`](../packages/goal/goal-round-driver), [`schedule`](../packages/schedule/schedule), `server`, `session-controller` | -| `agent/turn-stopping` | `serial` | [`packages/core/agent/src/runtime-types.ts:333`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`serial`) | [`hooks-claude-code`](../packages/hooks/hooks-claude-code), [`hooks-codex`](../packages/hooks/hooks-codex) | +| `agent/assistant-stream` | `emit` | [`packages/core/agent/src/runtime-types.ts:369`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`headless`](../packages/bundle/headless), `session-controller` | +| `agent/created` | `emit` | [`packages/core/agent/src/runtime-types.ts:258`](../packages/core/agent/src/runtime-types.ts) | [`agent`](../packages/core/agent) (`events.dispatch`) | [`agent-presets`](../packages/preset/agent-presets), [`file-reference-local`](../packages/context/file-reference-local), [`goal-round-driver`](../packages/goal/goal-round-driver), [`loader-smoke`](../packages/test-support/loader-smoke), [`schedule`](../packages/schedule/schedule), `tool-agent-team`, [`tool-subagent`](../packages/subagent/tool-subagent) | +| `agent/disposed` | `emit` | [`packages/core/agent/src/runtime-types.ts:267`](../packages/core/agent/src/runtime-types.ts) | [`agent`](../packages/core/agent) (`events.dispatch`) | [`agent-loop`](../packages/core/agent-loop), [`file-reference-local`](../packages/context/file-reference-local), [`goal-round-driver`](../packages/goal/goal-round-driver), `session-controller`, [`subagent`](../packages/subagent/subagent), `tool-agent-team`, [`tool-subagent`](../packages/subagent/tool-subagent) | +| `agent/error` | `emit` | [`packages/core/agent/src/runtime-types.ts:399`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`acp`](../packages/acp/acp), [`goal-round-driver`](../packages/goal/goal-round-driver), `session-controller`, [`session-telemetry`](../packages/session/session-telemetry) | +| `agent/inbox/claimed` | `emit` | [`packages/core/agent/src/runtime-types.ts:296`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`acp`](../packages/acp/acp), [`goal-round-driver`](../packages/goal/goal-round-driver), [`subagent`](../packages/subagent/subagent), [`tool-jobs`](../packages/jobs/tool-jobs) | +| `agent/inbox/discarded` | `emit` | [`packages/core/agent/src/runtime-types.ts:304`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`goal-round-driver`](../packages/goal/goal-round-driver), [`subagent`](../packages/subagent/subagent) | +| `agent/inbox/inserted` | `emit` | [`packages/core/agent/src/runtime-types.ts:285`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`goal-round-driver`](../packages/goal/goal-round-driver) | +| `agent/pre-step` | `waterfall` | [`packages/core/agent/src/runtime-types.ts:330`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`agent`](../packages/core/agent), [`agent-instructions`](../packages/context/agent-instructions), [`compaction-basic`](../packages/compaction/compaction-basic), [`goal-round-driver`](../packages/goal/goal-round-driver), [`hooks-claude-code`](../packages/hooks/hooks-claude-code), [`hooks-codex`](../packages/hooks/hooks-codex), [`plan-mode`](../packages/plan/plan-mode), [`repeat-tool-reminder`](../packages/guard/repeat-tool-reminder), [`session-checkpoint-policy`](../packages/session/session-checkpoint-policy), [`session-reference`](../packages/context/session-reference), [`subagent-in-process-driver`](../packages/subagent/subagent-in-process-driver), [`time-context`](../packages/context/time-context), [`tmux-context`](../packages/context/tmux-context), [`tool-cordis`](../packages/extensions/tool-cordis), [`tool-skill`](../packages/skill/tool-skill), [`tool-subagent`](../packages/subagent/tool-subagent) | +| `agent/request` | `waterfall` | [`packages/core/agent/src/runtime-types.ts:343`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`agent`](../packages/core/agent), [`webhook`](../packages/webhook/webhook) | +| `agent/request-error` | `waterfall` | [`packages/core/agent/src/runtime-types.ts:359`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`waterfall`) | [`compaction-basic`](../packages/compaction/compaction-basic), [`llm-retry`](../packages/llm/llm-retry) | +| `agent/session-start` | `emit` | [`packages/core/agent/src/runtime-types.ts:316`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emitAgentEvent`) | `agent-team`, [`goal`](../packages/goal/goal), [`goal-round-driver`](../packages/goal/goal-round-driver), [`hooks-claude-code`](../packages/hooks/hooks-claude-code), [`hooks-codex`](../packages/hooks/hooks-codex) | +| `agent/status` | `emit` | [`packages/core/agent/src/runtime-types.ts:277`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`emit`) | [`agent`](../packages/core/agent), `agent-team`, [`compaction-basic`](../packages/compaction/compaction-basic), [`goal-round-driver`](../packages/goal/goal-round-driver), [`schedule`](../packages/schedule/schedule), `server`, `session-controller` | +| `agent/turn-stopping` | `serial` | [`packages/core/agent/src/runtime-types.ts:387`](../packages/core/agent/src/runtime-types.ts) | [`agent-loop`](../packages/core/agent-loop) (`serial`) | [`hooks-claude-code`](../packages/hooks/hooks-claude-code), [`hooks-codex`](../packages/hooks/hooks-codex) | | `api-session/activity` | `emit` | [`packages/api/session-controller/src/types.ts:586`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | | `api-session/added` | `emit` | [`packages/api/session-controller/src/types.ts:566`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | | `api-session/error` | `emit` | [`packages/api/session-controller/src/types.ts:593`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | @@ -56,10 +56,10 @@ | `settings/document-updated` | `emit` | [`packages/settings/settings/src/types.ts:105`](../packages/settings/settings/src/types.ts) | [`settings`](../packages/settings/settings) (`events.dispatch`) | `remotes` | | `settings/updated` | `emit` | [`packages/settings/settings/src/types.ts:92`](../packages/settings/settings/src/types.ts) | [`settings`](../packages/settings/settings) (`events.dispatch`) | [`settings`](../packages/settings/settings) | | `skills/change` | `emit` | [`packages/skill/skill/src/index.ts:298`](../packages/skill/skill/src/index.ts) | [`skill`](../packages/skill/skill) (`events.dispatch`) | - | -| `subagent/end` | `emit` | [`packages/subagent/subagent/src/index.ts:172`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`events.dispatch`) | [`hooks-claude-code`](../packages/hooks/hooks-claude-code), `server`, [`subagent`](../packages/subagent/subagent) | -| `subagent/provider-added` | `emit` | [`packages/subagent/subagent/src/index.ts:146`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`emit`) | [`subagent`](../packages/subagent/subagent), [`tool-subagent`](../packages/subagent/tool-subagent) | -| `subagent/provider-removed` | `emit` | [`packages/subagent/subagent/src/index.ts:152`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`events.dispatch`) | [`subagent`](../packages/subagent/subagent), [`tool-subagent`](../packages/subagent/tool-subagent) | -| `subagent/start` | `emit` | [`packages/subagent/subagent/src/index.ts:163`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`events.dispatch`) | [`hooks-claude-code`](../packages/hooks/hooks-claude-code), [`subagent`](../packages/subagent/subagent) | +| `subagent/end` | `emit` | [`packages/subagent/subagent/src/index.ts:168`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`events.dispatch`) | [`hooks-claude-code`](../packages/hooks/hooks-claude-code), `server`, [`subagent`](../packages/subagent/subagent) | +| `subagent/provider-added` | `emit` | [`packages/subagent/subagent/src/index.ts:142`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`emit`) | [`subagent`](../packages/subagent/subagent), [`tool-subagent`](../packages/subagent/tool-subagent) | +| `subagent/provider-removed` | `emit` | [`packages/subagent/subagent/src/index.ts:148`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`events.dispatch`) | [`subagent`](../packages/subagent/subagent), [`tool-subagent`](../packages/subagent/tool-subagent) | +| `subagent/start` | `emit` | [`packages/subagent/subagent/src/index.ts:159`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`events.dispatch`) | [`hooks-claude-code`](../packages/hooks/hooks-claude-code), [`subagent`](../packages/subagent/subagent) | | `system-prompt/assemble` | `waterfall` | [`packages/core/system-prompt/src/index.ts:31`](../packages/core/system-prompt/src/index.ts) | [`system-prompt`](../packages/core/system-prompt) (`waterfall`) | [`agent`](../packages/core/agent), [`agent-presets`](../packages/preset/agent-presets), [`session-reference`](../packages/context/session-reference), [`system-prompt`](../packages/core/system-prompt) | | `system-prompt/change` | `emit` | [`packages/core/system-prompt/src/index.ts:37`](../packages/core/system-prompt/src/index.ts) | [`system-prompt`](../packages/core/system-prompt) (`emit`) | - | | `tools/change` | `emit` | [`packages/core/tools/src/index.ts:199`](../packages/core/tools/src/index.ts) | [`agent-presets`](../packages/preset/agent-presets) (`emit`), [`tools`](../packages/core/tools) (`emit`) | [`tool-subagent`](../packages/subagent/tool-subagent) | diff --git a/docs/module-graph.i18n.yaml b/docs/module-graph.i18n.yaml index 79ef8b4b09..c59e8ddf45 100644 --- a/docs/module-graph.i18n.yaml +++ b/docs/module-graph.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 docs/module-graph.md -module-graph.md: 0951780b0bf53b35f75ef13c50a3875138d756cc -module-graph.zh.md: 2a073370bd4de913f3a1293fcbebb9b9e25954f4 +module-graph.md: 2c31c50117cc6caabccdcb8fdf6afc9a160fe4dd +module-graph.zh.md: d41df4241163ca9b17e47c62051a891e45e131e0 diff --git a/docs/module-graph.md b/docs/module-graph.md index 0951780b0b..2c31c50117 100644 --- a/docs/module-graph.md +++ b/docs/module-graph.md @@ -457,6 +457,7 @@ flowchart TD pkg_agent --> pkg_session_projection pkg_agent --> pkg_system_prompt pkg_agent --> pkg_typert_protocol + pkg_agent --> pkg_util_values pkg_fs --> pkg_brand pkg_fs --> pkg_invariants pkg_fs --> pkg_llm @@ -868,11 +869,6 @@ flowchart TD pkg_tool_terminal --> pkg_system_prompt pkg_tool_terminal --> pkg_terminal pkg_tool_terminal --> pkg_tools - pkg_agent_loop_testkit --> pkg_agent - pkg_agent_loop_testkit --> pkg_llm - pkg_agent_loop_testkit --> pkg_session - pkg_agent_loop_testkit --> pkg_system_prompt - pkg_agent_loop_testkit --> pkg_tools pkg_llm_replay --> pkg_compaction pkg_llm_replay --> pkg_deepseek_llm_api_extensions pkg_llm_replay --> pkg_llm @@ -946,6 +942,13 @@ flowchart TD pkg_tool_pwsh --> pkg_system_prompt pkg_tool_pwsh --> pkg_tools pkg_tool_pwsh --> pkg_user_approval + pkg_agent_loop_testkit --> pkg_agent + pkg_agent_loop_testkit --> pkg_agent_loop + pkg_agent_loop_testkit --> pkg_llm + pkg_agent_loop_testkit --> pkg_session + pkg_agent_loop_testkit --> pkg_session_projection + pkg_agent_loop_testkit --> pkg_system_prompt + pkg_agent_loop_testkit --> pkg_tools pkg_webhook --> pkg_agent pkg_webhook --> pkg_agent_default_model pkg_webhook --> pkg_agent_presets @@ -1290,7 +1293,7 @@ flowchart TD | [`session-projection`](../packages/session/session-projection) | `session` | [`session`](../packages/core/session) | | [`settings`](../packages/settings/settings) | `settings` | [`brand`](../packages/util/brand), [`invariants`](../packages/runtime-diagnostics/invariants), [`session`](../packages/core/session) | | [`session-snapshot`](../packages/test-support/session-snapshot) | `test-support` | [`http-proxy`](../packages/util/http-proxy), [`session`](../packages/core/session) | -| [`agent`](../packages/core/agent) | `core` | [`invariants`](../packages/runtime-diagnostics/invariants), [`llm`](../packages/llm/llm), [`scope`](../packages/core/scope), [`session`](../packages/core/session), [`session-projection`](../packages/session/session-projection), [`system-prompt`](../packages/core/system-prompt), [`typert-protocol`](../packages/typert/protocol) | +| [`agent`](../packages/core/agent) | `core` | [`invariants`](../packages/runtime-diagnostics/invariants), [`llm`](../packages/llm/llm), [`scope`](../packages/core/scope), [`session`](../packages/core/session), [`session-projection`](../packages/session/session-projection), [`system-prompt`](../packages/core/system-prompt), [`typert-protocol`](../packages/typert/protocol), [`util-values`](../packages/util/values) | | [`fs`](../packages/fs/fs) | `fs` | [`brand`](../packages/util/brand), [`invariants`](../packages/runtime-diagnostics/invariants), [`llm`](../packages/llm/llm), [`sandbox`](../packages/sandbox/sandbox) | | [`spill-local`](../packages/spill/spill-local) | `spill` | [`spill`](../packages/spill/spill) | | [`session-log-export`](../packages/session-query/session-log-export) | `session-query` | [`session`](../packages/core/session), [`session-persistence`](../packages/session/session-persistence) | @@ -1377,7 +1380,6 @@ flowchart TD | [`tool-bash-persistent`](../packages/shell/tool-bash-persistent) | `shell` | [`agent`](../packages/core/agent), [`terminal`](../packages/terminal/terminal), [`timeout`](../packages/util/timeout), [`tools`](../packages/core/tools) | | [`tool-pwsh-persistent`](../packages/shell/tool-pwsh-persistent) | `shell` | [`agent`](../packages/core/agent), [`terminal`](../packages/terminal/terminal), [`timeout`](../packages/util/timeout), [`tools`](../packages/core/tools) | | [`tool-terminal`](../packages/terminal/tool-terminal) | `terminal` | [`agent`](../packages/core/agent), [`jobs`](../packages/jobs/jobs), [`llm`](../packages/llm/llm), [`output-retention`](../packages/util/output-retention), [`system-prompt`](../packages/core/system-prompt), [`terminal`](../packages/terminal/terminal), [`tools`](../packages/core/tools) | -| [`agent-loop-testkit`](../packages/test-support/agent-loop-testkit) | `test-support` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools) | | [`llm-replay`](../packages/test-support/llm-replay) | `test-support` | [`compaction`](../packages/compaction/compaction), [`deepseek-llm-api-extensions`](../packages/llm/deepseek-llm-api-extensions), [`llm`](../packages/llm/llm), [`session`](../packages/core/session) | | [`tool-workflow`](../packages/workflow/tool-workflow) | `workflow` | [`agent`](../packages/core/agent), [`invariants`](../packages/runtime-diagnostics/invariants), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools), [`workflow`](../packages/workflow/workflow) | | [`plugin-package-inventory-deepseek`](../packages/llm/plugin-package-inventory-deepseek) | `llm` | [`agent`](../packages/core/agent), [`agent-presets`](../packages/preset/agent-presets), [`deepseek-llm-api-extensions`](../packages/llm/deepseek-llm-api-extensions), [`session`](../packages/core/session) | @@ -1390,6 +1392,7 @@ flowchart TD | [`host-plugin-inventory`](../packages/host/plugin-inventory) | `host` | [`agent-presets`](../packages/preset/agent-presets), [`brand`](../packages/util/brand), [`typert-protocol`](../packages/typert/protocol) | | [`tool-bash`](../packages/shell/tool-bash) | `shell` | [`agent`](../packages/core/agent), [`jobs`](../packages/jobs/jobs), [`llm`](../packages/llm/llm), [`sandbox`](../packages/sandbox/sandbox), [`sandbox-policy`](../packages/sandbox/sandbox-policy), [`shell`](../packages/shell/shell), [`shell-env`](../packages/shell/shell-env), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools), [`user-approval`](../packages/interaction/user-approval) | | [`tool-pwsh`](../packages/shell/tool-pwsh) | `shell` | [`agent`](../packages/core/agent), [`jobs`](../packages/jobs/jobs), [`llm`](../packages/llm/llm), [`sandbox`](../packages/sandbox/sandbox), [`sandbox-policy`](../packages/sandbox/sandbox-policy), [`shell`](../packages/shell/shell), [`shell-env`](../packages/shell/shell-env), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools), [`user-approval`](../packages/interaction/user-approval) | +| [`agent-loop-testkit`](../packages/test-support/agent-loop-testkit) | `test-support` | [`agent`](../packages/core/agent), [`agent-loop`](../packages/core/agent-loop), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`session-projection`](../packages/session/session-projection), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools) | | [`webhook`](../packages/webhook/webhook) | `webhook` | [`agent`](../packages/core/agent), [`agent-default-model`](../packages/core/agent-default-model), [`agent-presets`](../packages/preset/agent-presets), [`invariants`](../packages/runtime-diagnostics/invariants), [`llm`](../packages/llm/llm), [`permission-presets`](../packages/interaction/permission-presets), [`session`](../packages/core/session), [`session-title`](../packages/session/session-title), [`workspace`](../packages/workspace/workspace) | | [`subagent`](../packages/subagent/subagent) | `subagent` | [`agent`](../packages/core/agent), [`agent-presets`](../packages/preset/agent-presets), [`attachment`](../packages/attachment/attachment), [`invariants`](../packages/runtime-diagnostics/invariants), [`jobs`](../packages/jobs/jobs), [`llm`](../packages/llm/llm), [`sandbox`](../packages/sandbox/sandbox), [`sandbox-policy`](../packages/sandbox/sandbox-policy), [`scope`](../packages/core/scope), [`session`](../packages/core/session), [`session-persistence`](../packages/session/session-persistence), [`session-projection`](../packages/session/session-projection), [`session-projection-cache`](../packages/session/session-projection-cache), [`session-query`](../packages/session-query/session-query), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools), [`typert-protocol`](../packages/typert/protocol), [`user-approval`](../packages/interaction/user-approval), [`util-time`](../packages/util/time) | | [`session-query-sqlite`](../packages/session-query/session-query-sqlite) | `session-query` | [`session`](../packages/core/session), [`session-persistence`](../packages/session/session-persistence), [`session-query`](../packages/session-query/session-query) | diff --git a/docs/module-graph.zh.md b/docs/module-graph.zh.md index 2a073370bd..d41df42411 100644 --- a/docs/module-graph.zh.md +++ b/docs/module-graph.zh.md @@ -459,6 +459,7 @@ flowchart TD pkg_agent --> pkg_session_projection pkg_agent --> pkg_system_prompt pkg_agent --> pkg_typert_protocol + pkg_agent --> pkg_util_values pkg_fs --> pkg_brand pkg_fs --> pkg_invariants pkg_fs --> pkg_llm @@ -870,11 +871,6 @@ flowchart TD pkg_tool_terminal --> pkg_system_prompt pkg_tool_terminal --> pkg_terminal pkg_tool_terminal --> pkg_tools - pkg_agent_loop_testkit --> pkg_agent - pkg_agent_loop_testkit --> pkg_llm - pkg_agent_loop_testkit --> pkg_session - pkg_agent_loop_testkit --> pkg_system_prompt - pkg_agent_loop_testkit --> pkg_tools pkg_llm_replay --> pkg_compaction pkg_llm_replay --> pkg_deepseek_llm_api_extensions pkg_llm_replay --> pkg_llm @@ -948,6 +944,13 @@ flowchart TD pkg_tool_pwsh --> pkg_system_prompt pkg_tool_pwsh --> pkg_tools pkg_tool_pwsh --> pkg_user_approval + pkg_agent_loop_testkit --> pkg_agent + pkg_agent_loop_testkit --> pkg_agent_loop + pkg_agent_loop_testkit --> pkg_llm + pkg_agent_loop_testkit --> pkg_session + pkg_agent_loop_testkit --> pkg_session_projection + pkg_agent_loop_testkit --> pkg_system_prompt + pkg_agent_loop_testkit --> pkg_tools pkg_webhook --> pkg_agent pkg_webhook --> pkg_agent_default_model pkg_webhook --> pkg_agent_presets @@ -1292,7 +1295,7 @@ flowchart TD | [`session-projection`](../packages/session/session-projection) | `session` | [`session`](../packages/core/session) | | [`settings`](../packages/settings/settings) | `settings` | [`brand`](../packages/util/brand), [`invariants`](../packages/runtime-diagnostics/invariants), [`session`](../packages/core/session) | | [`session-snapshot`](../packages/test-support/session-snapshot) | `test-support` | [`http-proxy`](../packages/util/http-proxy), [`session`](../packages/core/session) | -| [`agent`](../packages/core/agent) | `core` | [`invariants`](../packages/runtime-diagnostics/invariants), [`llm`](../packages/llm/llm), [`scope`](../packages/core/scope), [`session`](../packages/core/session), [`session-projection`](../packages/session/session-projection), [`system-prompt`](../packages/core/system-prompt), [`typert-protocol`](../packages/typert/protocol) | +| [`agent`](../packages/core/agent) | `core` | [`invariants`](../packages/runtime-diagnostics/invariants), [`llm`](../packages/llm/llm), [`scope`](../packages/core/scope), [`session`](../packages/core/session), [`session-projection`](../packages/session/session-projection), [`system-prompt`](../packages/core/system-prompt), [`typert-protocol`](../packages/typert/protocol), [`util-values`](../packages/util/values) | | [`fs`](../packages/fs/fs) | `fs` | [`brand`](../packages/util/brand), [`invariants`](../packages/runtime-diagnostics/invariants), [`llm`](../packages/llm/llm), [`sandbox`](../packages/sandbox/sandbox) | | [`spill-local`](../packages/spill/spill-local) | `spill` | [`spill`](../packages/spill/spill) | | [`session-log-export`](../packages/session-query/session-log-export) | `session-query` | [`session`](../packages/core/session), [`session-persistence`](../packages/session/session-persistence) | @@ -1379,7 +1382,6 @@ flowchart TD | [`tool-bash-persistent`](../packages/shell/tool-bash-persistent) | `shell` | [`agent`](../packages/core/agent), [`terminal`](../packages/terminal/terminal), [`timeout`](../packages/util/timeout), [`tools`](../packages/core/tools) | | [`tool-pwsh-persistent`](../packages/shell/tool-pwsh-persistent) | `shell` | [`agent`](../packages/core/agent), [`terminal`](../packages/terminal/terminal), [`timeout`](../packages/util/timeout), [`tools`](../packages/core/tools) | | [`tool-terminal`](../packages/terminal/tool-terminal) | `terminal` | [`agent`](../packages/core/agent), [`jobs`](../packages/jobs/jobs), [`llm`](../packages/llm/llm), [`output-retention`](../packages/util/output-retention), [`system-prompt`](../packages/core/system-prompt), [`terminal`](../packages/terminal/terminal), [`tools`](../packages/core/tools) | -| [`agent-loop-testkit`](../packages/test-support/agent-loop-testkit) | `test-support` | [`agent`](../packages/core/agent), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools) | | [`llm-replay`](../packages/test-support/llm-replay) | `test-support` | [`compaction`](../packages/compaction/compaction), [`deepseek-llm-api-extensions`](../packages/llm/deepseek-llm-api-extensions), [`llm`](../packages/llm/llm), [`session`](../packages/core/session) | | [`tool-workflow`](../packages/workflow/tool-workflow) | `workflow` | [`agent`](../packages/core/agent), [`invariants`](../packages/runtime-diagnostics/invariants), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools), [`workflow`](../packages/workflow/workflow) | | [`plugin-package-inventory-deepseek`](../packages/llm/plugin-package-inventory-deepseek) | `llm` | [`agent`](../packages/core/agent), [`agent-presets`](../packages/preset/agent-presets), [`deepseek-llm-api-extensions`](../packages/llm/deepseek-llm-api-extensions), [`session`](../packages/core/session) | @@ -1392,6 +1394,7 @@ flowchart TD | [`host-plugin-inventory`](../packages/host/plugin-inventory) | `host` | [`agent-presets`](../packages/preset/agent-presets), [`brand`](../packages/util/brand), [`typert-protocol`](../packages/typert/protocol) | | [`tool-bash`](../packages/shell/tool-bash) | `shell` | [`agent`](../packages/core/agent), [`jobs`](../packages/jobs/jobs), [`llm`](../packages/llm/llm), [`sandbox`](../packages/sandbox/sandbox), [`sandbox-policy`](../packages/sandbox/sandbox-policy), [`shell`](../packages/shell/shell), [`shell-env`](../packages/shell/shell-env), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools), [`user-approval`](../packages/interaction/user-approval) | | [`tool-pwsh`](../packages/shell/tool-pwsh) | `shell` | [`agent`](../packages/core/agent), [`jobs`](../packages/jobs/jobs), [`llm`](../packages/llm/llm), [`sandbox`](../packages/sandbox/sandbox), [`sandbox-policy`](../packages/sandbox/sandbox-policy), [`shell`](../packages/shell/shell), [`shell-env`](../packages/shell/shell-env), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools), [`user-approval`](../packages/interaction/user-approval) | +| [`agent-loop-testkit`](../packages/test-support/agent-loop-testkit) | `test-support` | [`agent`](../packages/core/agent), [`agent-loop`](../packages/core/agent-loop), [`llm`](../packages/llm/llm), [`session`](../packages/core/session), [`session-projection`](../packages/session/session-projection), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools) | | [`webhook`](../packages/webhook/webhook) | `webhook` | [`agent`](../packages/core/agent), [`agent-default-model`](../packages/core/agent-default-model), [`agent-presets`](../packages/preset/agent-presets), [`invariants`](../packages/runtime-diagnostics/invariants), [`llm`](../packages/llm/llm), [`permission-presets`](../packages/interaction/permission-presets), [`session`](../packages/core/session), [`session-title`](../packages/session/session-title), [`workspace`](../packages/workspace/workspace) | | [`subagent`](../packages/subagent/subagent) | `subagent` | [`agent`](../packages/core/agent), [`agent-presets`](../packages/preset/agent-presets), [`attachment`](../packages/attachment/attachment), [`invariants`](../packages/runtime-diagnostics/invariants), [`jobs`](../packages/jobs/jobs), [`llm`](../packages/llm/llm), [`sandbox`](../packages/sandbox/sandbox), [`sandbox-policy`](../packages/sandbox/sandbox-policy), [`scope`](../packages/core/scope), [`session`](../packages/core/session), [`session-persistence`](../packages/session/session-persistence), [`session-projection`](../packages/session/session-projection), [`session-projection-cache`](../packages/session/session-projection-cache), [`session-query`](../packages/session-query/session-query), [`system-prompt`](../packages/core/system-prompt), [`tools`](../packages/core/tools), [`typert-protocol`](../packages/typert/protocol), [`user-approval`](../packages/interaction/user-approval), [`util-time`](../packages/util/time) | | [`session-query-sqlite`](../packages/session-query/session-query-sqlite) | `session-query` | [`session`](../packages/core/session), [`session-persistence`](../packages/session/session-persistence), [`session-query`](../packages/session-query/session-query) | diff --git a/docs/persistence-catalog.i18n.yaml b/docs/persistence-catalog.i18n.yaml index 2671f9fa0c..6775871bcd 100644 --- a/docs/persistence-catalog.i18n.yaml +++ b/docs/persistence-catalog.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 docs/persistence-catalog.md -persistence-catalog.md: c47cd8a09d4c7a5179bb36d9c62611564b95a5cc -persistence-catalog.zh.md: c67d315c91565cee5855ee5b0667672151b83323 +persistence-catalog.md: 8d6f66dc949b6d41530eb387534689dc66f1e99f +persistence-catalog.zh.md: 06d07ceab984ad20ee6637227d54fafbbb58bdd4 diff --git a/docs/persistence-catalog.md b/docs/persistence-catalog.md index c47cd8a09d..8d6f66dc94 100644 --- a/docs/persistence-catalog.md +++ b/docs/persistence-catalog.md @@ -101,8 +101,8 @@ Sources: [`packages/core/session/src/types.ts:385`](../packages/core/session/src ```ts persistence-catalog /** * One normalized mutation of an agent's durable pending-message lists. - * Live dispatch precedes projection mutation, so synchronous observers may - * read the pre-splice inbox to recover the removed messages. + * The session-projection registry applies the committed event before + * `Session.append()` returns; Inbox live notifications follow that commit. */ 'agent/inbox/spliced': { target: InboxTarget @@ -113,7 +113,7 @@ Sources: [`packages/core/session/src/types.ts:385`](../packages/core/session/src } ``` -Source: [`packages/core/agent/src/types.ts:58`](../packages/core/agent/src/types.ts) +Source: [`packages/core/agent/src/types.ts:87`](../packages/core/agent/src/types.ts) ### `agent-preset/*` diff --git a/docs/persistence-catalog.zh.md b/docs/persistence-catalog.zh.md index c67d315c91..06d07ceab9 100644 --- a/docs/persistence-catalog.zh.md +++ b/docs/persistence-catalog.zh.md @@ -103,8 +103,8 @@ export type SessionEvent = { ```ts persistence-catalog /** * One normalized mutation of an agent's durable pending-message lists. - * Live dispatch precedes projection mutation, so synchronous observers may - * read the pre-splice inbox to recover the removed messages. + * The session-projection registry applies the committed event before + * `Session.append()` returns; Inbox live notifications follow that commit. */ 'agent/inbox/spliced': { target: InboxTarget @@ -115,7 +115,7 @@ export type SessionEvent = { } ``` -来源:[`packages/core/agent/src/types.ts:58`](../packages/core/agent/src/types.ts) +来源:[`packages/core/agent/src/types.ts:87`](../packages/core/agent/src/types.ts) ### `agent-preset/*` diff --git a/docs/subsystems/core.i18n.yaml b/docs/subsystems/core.i18n.yaml index 811c2e5d21..25c3042d2f 100644 --- a/docs/subsystems/core.i18n.yaml +++ b/docs/subsystems/core.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 docs/subsystems/core.md -core.md: 29e332d068857254e3cd27892adda36a9229a0d7 -core.zh.md: c148b8f6a48d7f01b12f59b5fc14c655a1fb8daa +core.md: 4af3a22478f324655dea1b132f0c8a8528f4b883 +core.zh.md: 7359e5aaa75e9f750d44f1386acab388665e1e1e diff --git a/docs/subsystems/core.md b/docs/subsystems/core.md index 29e332d068..4af3a22478 100644 --- a/docs/subsystems/core.md +++ b/docs/subsystems/core.md @@ -65,7 +65,7 @@ interface Agent { readonly options: AgentOptions /** The live session this agent drives; its log is the durable source of truth. */ readonly session: Session - /** The agent-owned projection of durable pending work. */ + /** Agent-owned access to durable pending work. */ readonly inbox: Inbox /** The current lifecycle state, mirrored on every `agent/status` transition. */ readonly status: AgentStatus @@ -210,12 +210,69 @@ Dispatch requires `provider` and `model` after `agent/request`. An explicit `rea The inbox is the delivery vocabulary — two ordered pending-message lists the agent owns as a durable projection: +```ts type-equiv +/** Agent-owned access to pending work; concrete storage belongs to the driver. */ +interface Inbox { + /** Prompts awaiting individual turns. */ + readonly nextTurn: readonly UserMessage[] + /** Input awaiting the next step boundary. */ + readonly nextStep: readonly UserMessage[] + + /** Durably cancel all pending input, clearing next-step before next-turn. */ + clear(): void + + /** + * Append one message to a pending list. + * @param target - pending list to extend. + * @param message - message to append. + */ + append(target: InboxTarget, message: UserMessage): void + + /** + * Prepend one message to a pending list. + * @param target - pending list to extend. + * @param message - message to prepend. + */ + prepend(target: InboxTarget, message: UserMessage): void + + /** + * Replace one pending message in place. + * @param messageId - identity of the pending message to replace. + * @param newMessage - replacement message. + * @returns whether the message was still pending. + */ + replace(messageId: MessageId, newMessage: UserMessage): boolean + + /** + * Remove one pending message. + * @param messageId - identity of the pending message to remove. + * @returns whether the message was still pending. + */ + remove(messageId: MessageId): boolean + + /** + * Apply standard splice semantics and durably record the normalized result. + * @param target - pending list to mutate. + * @param start - splice position. + * @param deleteCount - maximum number of messages to remove. + * @param inserted - messages to insert at the resolved position. + * @returns messages removed by the splice. + */ + splice( + target: InboxTarget, + start: number, + deleteCount: number, + inserted: UserMessage[], + ): UserMessage[] +} +``` + ```ts type-equiv /** One of the two ordered pending-message lists owned by an agent. */ type InboxTarget = 'next-turn' | 'next-step' ``` -Every pending occurrence is its `UserMessage`; `MessageId` is the sole identity. `Inbox.append`, `prepend`, `replace`, `remove`, `clear`, `splice`, and `claim` record normalized durable `agent/inbox/spliced` mutations and reject duplicate pending ids. `replace(messageId, newMessage)` and `remove(messageId)` locate the pending message across both lists; replacement may change identity and emits the old message as discarded followed by the new message as inserted. Ordinary removals and `clear()` are cancellations. `claim(target)` removes the proposed step batch — all `next-step` input plus, at a turn boundary, one `next-turn` message — through pure deletion splices without emitting discarded notifications, and the loop separately emits per-message claimed notifications. Whole-queue consumers such as UI projections reconstruct `nextTurn` and `nextStep` from the durable splices, while consumers following one message use the exact `agent/inbox/inserted`, `claimed`, and `discarded` notifications. +Every pending occurrence is its `UserMessage`; `MessageId` is the sole identity. The structural `Inbox` methods record normalized durable `agent/inbox/spliced` mutations and reject duplicate pending ids. `replace(messageId, newMessage)` and `remove(messageId)` locate the pending message across both lists; replacement may change identity and emits the old message as discarded followed by the new message as inserted. Ordinary removals and `clear()` are cancellations. At a step boundary, dsh-agent-loop's package-internal `ReactLoopInbox` removes the proposed batch — all `next-step` input plus, at a turn boundary, one `next-turn` message — through pure deletion splices without discarded notifications, then emits per-message claimed notifications. Loop-only pending detection and claiming are not part of `Agent.inbox`. Each `ReactLoopInbox` constructor contributes the standard `inbox` projection from its agent scope; the registry shares that definition across agents by reference count, and its cell is the sole live state while the same fold serves cold consumers. The fold rejects unsafe or out-of-range splice coordinates and duplicate identities across both lists, identifying malformed durable history by event seq. Consumers following one message use the exact `agent/inbox/inserted`, `claimed`, and `discarded` notifications. Cancellation: diff --git a/docs/subsystems/core.zh.md b/docs/subsystems/core.zh.md index c148b8f6a4..7359e5aaa7 100644 --- a/docs/subsystems/core.zh.md +++ b/docs/subsystems/core.zh.md @@ -69,7 +69,7 @@ interface Agent { readonly options: AgentOptions /** The live session this agent drives; its log is the durable source of truth. */ readonly session: Session - /** The agent-owned projection of durable pending work. */ + /** Agent-owned access to durable pending work. */ readonly inbox: Inbox /** The current lifecycle state, mirrored on every `agent/status` transition. */ readonly status: AgentStatus @@ -214,12 +214,69 @@ interface AgentOptions { inbox 即投递词汇——agent 以持久投影形式拥有的两条有序待处理消息列表: +```ts type-equiv +/** Agent-owned access to pending work; concrete storage belongs to the driver. */ +interface Inbox { + /** Prompts awaiting individual turns. */ + readonly nextTurn: readonly UserMessage[] + /** Input awaiting the next step boundary. */ + readonly nextStep: readonly UserMessage[] + + /** Durably cancel all pending input, clearing next-step before next-turn. */ + clear(): void + + /** + * Append one message to a pending list. + * @param target - pending list to extend. + * @param message - message to append. + */ + append(target: InboxTarget, message: UserMessage): void + + /** + * Prepend one message to a pending list. + * @param target - pending list to extend. + * @param message - message to prepend. + */ + prepend(target: InboxTarget, message: UserMessage): void + + /** + * Replace one pending message in place. + * @param messageId - identity of the pending message to replace. + * @param newMessage - replacement message. + * @returns whether the message was still pending. + */ + replace(messageId: MessageId, newMessage: UserMessage): boolean + + /** + * Remove one pending message. + * @param messageId - identity of the pending message to remove. + * @returns whether the message was still pending. + */ + remove(messageId: MessageId): boolean + + /** + * Apply standard splice semantics and durably record the normalized result. + * @param target - pending list to mutate. + * @param start - splice position. + * @param deleteCount - maximum number of messages to remove. + * @param inserted - messages to insert at the resolved position. + * @returns messages removed by the splice. + */ + splice( + target: InboxTarget, + start: number, + deleteCount: number, + inserted: UserMessage[], + ): UserMessage[] +} +``` + ```ts type-equiv /** One of the two ordered pending-message lists owned by an agent. */ type InboxTarget = 'next-turn' | 'next-step' ``` -每个待处理入队项就是其 `UserMessage`;`MessageId` 是唯一标识。`Inbox.append`、`prepend`、`replace`、`remove`、`clear`、`splice` 与 `claim` 会记录规范化的持久 `agent/inbox/spliced` 变更,并拒绝重复的待处理 id。`replace(messageId, newMessage)` 与 `remove(messageId)` 通过 `MessageId` 跨两份列表定位待处理消息;替换可以改变标识,并先将旧消息作为 discarded 发布,再将新消息作为 inserted 发布。普通删除和 `clear()` 都表示取消。`claim(target)` 通过纯删除 splice 移除拟进入步骤的批次——全部 `next-step` 输入,外加轮次边界上的一条 `next-turn` 消息——且不发出 discarded 通知;循环另行逐条发出 claimed 通知。UI 投影等整体队列消费方通过持久 splice 重建 `nextTurn` 与 `nextStep`,而跟踪单条消息的消费方使用精确的 `agent/inbox/inserted`、`claimed` 与 `discarded` 通知。 +每个待处理入队项就是其 `UserMessage`;`MessageId` 是唯一标识。结构化 `Inbox` 方法会记录规范化的持久 `agent/inbox/spliced` 变更,并拒绝重复的待处理 id。`replace(messageId, newMessage)` 与 `remove(messageId)` 通过 `MessageId` 跨两份列表定位待处理消息;替换可以改变标识,并先将旧消息作为 discarded 发布,再将新消息作为 inserted 发布。普通删除和 `clear()` 都表示取消。在步骤边界,dsh-agent-loop 包内部的 `ReactLoopInbox` 会通过纯删除 splice 移除拟进入步骤的批次——全部 `next-step` 输入,外加轮次边界上的一条 `next-turn` 消息——且不发出 discarded 通知,随后逐条发出 claimed 通知。仅供循环使用的待处理检测与领取操作不属于 `Agent.inbox`。每个 `ReactLoopInbox` 构造函数都从其 agent 作用域贡献标准 `inbox` 投影;注册表通过引用计数在多个 agent 之间共享该定义,其 cell 是唯一 live 状态,同一份折叠也服务于冷消费方。该 fold 会拒绝不安全或越界的 splice 坐标,以及跨两份列表重复的标识,并通过事件 seq 指出格式错误的持久历史。跟踪单条消息的消费方使用精确的 `agent/inbox/inserted`、`claimed` 与 `discarded` 通知。 取消: diff --git a/packages/acp/acp/tests/harness.ts b/packages/acp/acp/tests/harness.ts index aa37bf1bc8..51d89dab25 100644 --- a/packages/acp/acp/tests/harness.ts +++ b/packages/acp/acp/tests/harness.ts @@ -23,7 +23,6 @@ import type { ImageAttachmentLimits, ImageAttachmentRef, SaveImageAttachment, St import { type GenerateOptions, LlmAdapter, ReasoningEffortId, type LlmResolvedModelInfo, type StreamChunk } from '@deepseek-ai/dsh-llm' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl' import TokenMeter from '@deepseek-ai/dsh-token-meter' import * as AcpPlugin from '../src/index.ts' @@ -232,10 +231,6 @@ export async function makeBridgeHarness(options: { const ownsPersistenceRoot = options.persistenceRoot === undefined const persistenceRoot = options.persistenceRoot ?? await mkdtemp(join(tmpdir(), 'dsh-acp-test-')) await mountAgentLoopTestDependencies(ctx, { systemPrompt: { personaPrefix: options.persona ?? '' } }) - // The agent loop and the composed approval/permission services declare - // sessionProjections a required injection: mount the registry (and with it - // the loop's turnBoundary unit) before the loop activates. - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(JsonlSessionPersistence, { root: persistenceRoot, compression: 'none' }) await ctx.plugin(TokenMeter) if (options.attachments !== false) await ctx.plugin(MemoryAttachmentStore) diff --git a/packages/api/session-controller/README.i18n.yaml b/packages/api/session-controller/README.i18n.yaml index 6ae203a68b..981e5f12ca 100644 --- a/packages/api/session-controller/README.i18n.yaml +++ b/packages/api/session-controller/README.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 packages/api/session-controller/README.md -README.md: a2639ebd5fb648ed193d7c3575a649891ebf76b9 -README.zh.md: 7bed20d3e47855b045bb838b354328f1769273e2 +README.md: 6b56c4976b5ca66fbdbdb274bdf5bf8f38d52b1d +README.zh.md: a9c53d37371e3280c5bc0ae21a93a511036a810d diff --git a/packages/api/session-controller/README.md b/packages/api/session-controller/README.md index a2639ebd5f..6b56c4976b 100644 --- a/packages/api/session-controller/README.md +++ b/packages/api/session-controller/README.md @@ -27,7 +27,7 @@ History pages and follow opening snapshots carry one `{ type: 'event', event: Se Each endpoint states its activation policy. List reads only stored headers and projection-cache rows: it never calls per-session stat or opens a cold Session body. A current-format cache identity may supply every list hint; a lifecycle-matching predecessor cache may supply only its version-compatible title as a stale display fact, never as an authoritative fold seed. Search, attachment, history pages, log following, skill discovery, and workspace-path opening can inspect persistence without activating an Agent; `canOpenWorkspacePath()` reports native-opening availability without addressing a Session. Queue mutation and cancellation require live state; model, rename, prompt, and file-reference operations may resolve or resume an ordinary Session. Prompt admission consumes opaque receipts from the injected [`fileUploads`](../../client/file-upload/README.md) Host service and resolves every same-Agent receipt before sending the complete ordered content list through `ctx.attachments`. Prompt retries whose `requestId` is already queued or logged return the original acceptance without inserting another message. Create and fork are the only operations that create a new Agent directly. The service applies one preset-aware resume policy and subagent ownership fence to its own methods and to the Typert Agent and Session lookups used by other Remote namespaces. Queue mutation has one narrow exception: a live child whose current projected identity is continuable and comes from its own non-seed suffix accepts the ordinary Edit, Remove, and QueueDock Steer actions across both inbox destinations. One-shot, missing, unknown, corrupt, seed-only, or cold children remain rejected without resume. The skill catalog uses a live Agent when present or the recorded preset's standing scope when cold, so listing never starts an Agent. -The Client adapter exposes `SessionEventStream`, a Gateway `RemoteJournalStream` bound to one ordinary or direct-subagent address. It opens follow before the initial page, publishes only contiguous `replace`, `prepend`, `append`, and `settle-assistant` changes, and repairs reconnect or sequence gaps through a tail page. Backwards paging has two verbs: `loadOlder()` pulls one 50-message page, and `loadThrough(seq)` — the turn-jump loader — loops 200-message pages until the window covers the target seq, lowering a shared target on repeated calls, stopping on a page that makes no progress, and reporting busy through the same `loadingOlder` snapshot bit. The Web adapter explicitly opts into cursorless Assistant frames: each opening carries the active attempt's `startedAfterSeq`, `nextIndex`, and compact stream, and every stream member becomes a Client-only `assistant/live-chunk` entry ordered between durable cursors. The Host captures a follower-local arrival ordinal with that baseline and suppresses buffered frames at or before the cut; a replacement Agent may restart frame revision at one. A durable `assistant/message` or `assistant/attempt` arriving after an active opening stays staged only when its seq follows `startedAfterSeq` and its Turn and Step match; the matching end type, seq, and index publishes one named settlement delta that retires the attempt's transient rows and adds the durable entry while earlier same-step retries remain visible. Revision, dense-index, or settlement gaps for a known attempt reopen follow, while a controller that missed the start ignores unknown-attempt frames and publishes their durable settlement normally. An abandoned end publishes a settlement delta without a durable entry so its transient rows retire immediately. A durable gap-repair page has no Assistant baseline, so its held notification reopens follow once for a paired page and baseline. Every history record covers exactly its event seq. A business, persistence, or unresolved continuity failure terminates the stream, while only physical carrier loss selects automatic resumption. `SessionControlStream` is a Gateway `RemoteSnapshotStream`; every generation opens with a complete process-local baseline, so reconnect replaces queue, jobs, and projection state instead of treating transient values as durable events. Client Agent contexts provide the identity used by the independent [`fileUpload`](../../client/file-upload/README.md) service; Session objects expose lifecycle, prompt, queue, and history operations rather than file transfer. +The Client adapter exposes `SessionEventStream`, a Gateway `RemoteJournalStream` bound to one ordinary or direct-subagent address. It opens follow before the initial page, publishes only contiguous `replace`, `prepend`, `append`, and `settle-assistant` changes, and repairs reconnect or sequence gaps through a tail page. Backwards paging has two verbs: `loadOlder()` pulls one 50-message page, and `loadThrough(seq)` — the turn-jump loader — loops 200-message pages until the window covers the target seq, lowering a shared target on repeated calls, stopping on a page that makes no progress, and reporting busy through the same `loadingOlder` snapshot bit. The Web adapter explicitly opts into cursorless Assistant frames: each opening carries the active attempt's `startedAfterSeq`, `nextIndex`, and compact stream, and every stream member becomes a Client-only `assistant/live-chunk` entry ordered between durable cursors. The Host captures a follower-local arrival ordinal with that baseline and suppresses buffered frames at or before the cut; a replacement Agent may restart frame revision at one. A durable `assistant/message` or `assistant/attempt` arriving after an active opening stays staged only when its seq follows `startedAfterSeq` and its Turn and Step match; the matching end type, seq, and index publishes one named settlement delta that retires the attempt's transient rows and adds the durable entry while earlier same-step retries remain visible. Revision, dense-index, or settlement gaps for a known attempt reopen follow, while a controller that missed the start ignores unknown-attempt frames and publishes their durable settlement normally. An abandoned end publishes a settlement delta without a durable entry so its transient rows retire immediately. A durable gap-repair page has no Assistant baseline, so its held notification reopens follow once for a paired page and baseline. Every history record covers exactly its event seq. A business, persistence, or unresolved continuity failure terminates the stream, while only physical carrier loss selects automatic resumption. `SessionControlStream` is a Gateway `RemoteSnapshotStream`; every generation opens with a complete process-local baseline, so reconnect replaces queue, jobs, and projection state instead of treating transient values as durable events. For each inbox change, the Host publishes the projection frame first and derives the queue replacement from that same validated post-fold value, so listener registration order cannot produce a stale queue frame.Client Agent contexts provide the identity used by the independent [`fileUpload`](../../client/file-upload/README.md) service; Session objects expose lifecycle, prompt, queue, and history operations rather than file transfer. The Session object also carries local submission echoes: `session.beginSubmission` inserts one into `SessionSnapshot.pendingSubmissions` synchronously, before the caller serializes and prompts, so a conversation UI can show the message on the submit click's own frame. The echo stores ordered image previews and durable file references. Session derives its `transcript`, `queued`, or `steering` placement from the current running state and requested delivery mode, then retains that placement while serialization is in flight. The prompt's `requestId` is the correlation identity: the Host echoes it as the durable user source's `rpcId`, and queue occurrences project it as `SessionQueuedItem.rpcId`. An echo retires one animation frame after its durable event or queue occurrence is observed, immediately when its identified prompt fails or is abandoned, and as failed on disposal. Each retirement fires `onRetire` exactly once; an observed retirement includes the ordered durable attachment references so the composer can release successful cards while preserving failed drafts. Echoes are Client memory only; reload and reconnect rebuild the conversation from durable events alone. diff --git a/packages/api/session-controller/README.zh.md b/packages/api/session-controller/README.zh.md index 7bed20d3e4..a9c53d3737 100644 --- a/packages/api/session-controller/README.zh.md +++ b/packages/api/session-controller/README.zh.md @@ -27,7 +27,7 @@ kind: "package-reference" 每个 endpoint 都声明自己的激活策略。列表只读取持久化 header 与 projection cache row,绝不调用逐 Session stat 或打开冷 Session body。当前格式 cache identity 可以提供全部列表 hint;生命周期匹配的 predecessor cache 只能提供版本兼容的 title,作为可能过时的展示事实,绝不能作为权威 fold seed。搜索、附件、历史页、日志跟随、skill 发现和工作区路径打开可以在不激活 Agent 的情况下检查 persistence;`canOpenWorkspacePath()` 无需指定 Session 即可报告原生打开能力。queue 变更与取消要求 live 状态;模型、重命名、prompt 和文件引用操作可以解析或恢复普通 Session。prompt 准入从注入的 [`fileUploads`](../../client/file-upload/README.zh.md) Host 服务取得不透明凭证,在把完整有序内容列表交给 `ctx.attachments` 前解析每个属于同一 Agent 的凭证。`requestId` 已进入 queue 或日志时,prompt 重试直接返回原来的接受结果,不会重复插入消息。只有 create 与 fork 会直接创建新 Agent。该服务把同一套感知 preset 的恢复策略和 subagent ownership fence 同时用于自身方法,以及其他 Remote namespace 使用的 Typert Agent 与 Session lookup。Queue 变更只有一个狭窄例外:当前 projection identity 为 continuable 且来自自身非 seed suffix 的在线 child,可以在两个 inbox 目标上使用普通 Edit、Remove 与 QueueDock Steer action。One-shot、缺失、未知、损坏、仅含 seed identity 或冷 child 继续被拒绝,且不会恢复。skill 目录优先使用已有 live Agent,否则使用所记录 preset 的常驻 scope,因此列表查询绝不会启动 Agent。 -Client adapter 提供 `SessionEventStream`,即绑定到一个普通 Session 或 direct subagent address 的 Gateway `RemoteJournalStream`。它在读取首个 page 前打开 follow,只发布连续的 `replace`、`prepend`、`append` 与 `settle-assistant` 变更,并通过 tail page 修复重连或 seq 缺口。向后分页有两个动词:`loadOlder()` 拉一页 50 条 message,而 `loadThrough(seq)`,即轮次跳转加载器,按 200 条 message 一页循环拉取直到窗口覆盖目标 seq,重复调用会下调共享目标,遇到无进展的页即停止,忙碌状态复用同一个 `loadingOlder` 快照位。Web adapter 显式选择接收无 cursor 的 Assistant frame:每个 opening 携带活跃 attempt 的 `startedAfterSeq`、`nextIndex` 与紧凑 stream,每个 stream member 都成为排在持久 cursor 之间的 Client-only `assistant/live-chunk` 条目。Host 会随该 baseline 捕获 follower 本地到达序号,并抑制该 cut 及之前的 buffered frame;replacement Agent 可以从 revision 一重新开始。活跃 opening 之后到达的持久 `assistant/message` 或 `assistant/attempt` 只有在其 seq 晚于 `startedAfterSeq` 且 Turn 与 Step 匹配时才会保持暂存;匹配的 end type、seq 与 index 会发布一个具名 settlement delta,删除该 attempt 的瞬态 row、加入持久条目,并保留同一步骤中更早的 retry。已知 attempt 的 revision、密集 index 或 settlement 缺口会重新打开 follow;若 controller 错过 start,则忽略 unknown-attempt frame,并正常发布其持久 settlement。Abandoned end 会发布不含持久条目的 settlement delta,使瞬态 row 立即退出。持久缺口修复 page 不携带 Assistant baseline,因此 held notification 会重新打开 follow 一次,以取得配对的 page 与 baseline。每条历史 record 只覆盖自身的 event seq。业务、persistence 或无法恢复的连续性错误会终止 stream,只有物理载体断开才触发自动恢复。`SessionControlStream` 是 Gateway `RemoteSnapshotStream`;每代都以完整的进程本地 baseline 开始,因此重连会替换 queue、jobs 和 projection 状态,而不会把瞬态值当作 durable event。Client Agent context 提供独立 [`fileUpload`](../../client/file-upload/README.zh.md) 服务使用的身份;Session 对象提供生命周期、prompt、queue 与历史操作,不提供文件传输。 +Client adapter 提供 `SessionEventStream`,即绑定到一个普通 Session 或 direct subagent address 的 Gateway `RemoteJournalStream`。它在读取首个 page 前打开 follow,只发布连续的 `replace`、`prepend`、`append` 与 `settle-assistant` 变更,并通过 tail page 修复重连或 seq 缺口。向后分页有两个动词:`loadOlder()` 拉一页 50 条 message,而 `loadThrough(seq)`——轮次跳转加载器——按 200 条 message 一页循环拉取直到窗口覆盖目标 seq,重复调用会下调共享目标,遇到无进展的页即停止,忙碌状态复用同一个 `loadingOlder` 快照位。Web adapter 显式选择接收无 cursor 的 Assistant frame:每个 opening 携带活跃 attempt 的 `startedAfterSeq`、`nextIndex` 与紧凑 stream,每个 stream member 都成为排在持久 cursor 之间的 Client-only `assistant/live-chunk` 条目。Host 会随该 baseline 捕获 follower 本地到达序号,并抑制该 cut 及之前的 buffered frame;replacement Agent 可以从 revision 一重新开始。活跃 opening 之后到达的持久 `assistant/message` 或 `assistant/attempt` 只有在其 seq 晚于 `startedAfterSeq` 且 Turn 与 Step 匹配时才会保持暂存;匹配的 end type、seq 与 index 会发布一个具名 settlement delta,删除该 attempt 的瞬态 row、加入持久条目,并保留同一步骤中更早的 retry。已知 attempt 的 revision、密集 index 或 settlement 缺口会重新打开 follow;若 controller 错过 start,则忽略 unknown-attempt frame,并正常发布其持久 settlement。Abandoned end 会发布不含持久条目的 settlement delta,使瞬态 row 立即退出。持久缺口修复 page 不携带 Assistant baseline,因此 held notification 会重新打开 follow 一次,以取得配对的 page 与 baseline。每条历史 record 只覆盖自身的 event seq。业务、persistence 或无法恢复的连续性错误会终止 stream,只有物理载体断开才触发自动恢复。`SessionControlStream` 是 Gateway `RemoteSnapshotStream`;每代都以完整的进程本地 baseline 开始,因此重连会替换 queue、jobs 和 projection 状态,而不会把瞬态值当作 durable event。每次 inbox 变更时,Host 会先发布 projection frame,再从同一份已校验的折叠后值派生 queue replacement,因此监听器注册顺序不会产生陈旧的 queue frame。Client Agent context 提供独立 [`fileUpload`](../../client/file-upload/README.zh.md) 服务使用的身份;Session 对象提供生命周期、prompt、queue 与历史操作,不提供文件传输。 Session 对象还承载本地提交回显:`session.beginSubmission` 在调用方序列化与 prompt 之前,同步把一条回显写入 `SessionSnapshot.pendingSubmissions`,会话 UI 因此能在点击提交的当帧显示消息。回显按顺序存放图片预览与持久文件引用。Session 根据当前运行状态与请求的投递模式推导其 `transcript`、`queued` 或 `steering` 位置,并在序列化期间保留该位置。prompt 的 `requestId` 是关联标识:Host 把它回显为 durable user source 的 `rpcId`,queue occurrence 也把它投影为 `SessionQueuedItem.rpcId`。回显在观察到其 durable event 或 queue occurrence 后延迟一个动画帧退休,带标识的 prompt 失败或被放弃时立即退休,销毁时按 failed 退休。每次退休恰好触发一次 `onRetire`;observed 退休还会携带有序的持久附件引用,让 composer 释放成功卡片并保留失败草稿。回显只存在于 Client 内存;刷新与重连只从 durable event 重建会话。 diff --git a/packages/api/session-controller/package.json b/packages/api/session-controller/package.json index ffc8f45118..65b757e0e1 100644 --- a/packages/api/session-controller/package.json +++ b/packages/api/session-controller/package.json @@ -118,6 +118,8 @@ "@deepseek-ai/cordis": "workspace:^", "@deepseek-ai/dsh-agent": "workspace:^", "@deepseek-ai/dsh-agent-default-model": "workspace:^", + "@deepseek-ai/dsh-agent-loop": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-agent-presets": "workspace:^", "@deepseek-ai/dsh-api-gateway": "workspace:^", "@deepseek-ai/dsh-attachment": "workspace:^", diff --git a/packages/api/session-controller/src/control.ts b/packages/api/session-controller/src/control.ts index 1a03011644..9bdd5a05bf 100644 --- a/packages/api/session-controller/src/control.ts +++ b/packages/api/session-controller/src/control.ts @@ -1,11 +1,11 @@ /** Live Session queue, jobs, and projection state with reconnect baselines. */ import type { Context } from '@deepseek-ai/cordis' -import type { Agent } from '@deepseek-ai/dsh-agent' +import type { Agent, InboxState } from '@deepseek-ai/dsh-agent' import { Deque } from '@deepseek-ai/dsh-deque' import type { JobSnapshot } from '@deepseek-ai/dsh-jobs' import type { - Session, SessionEvent, SessionEventMap, SessionId, UserMessage, + Session, SessionId, UserMessage, } from '@deepseek-ai/dsh-session' import type { JsonValue } from '@deepseek-ai/dsh-util-values' import type { @@ -23,7 +23,6 @@ export class SessionControlController { /** @param ctx - Host context carrying live Agent, projection, and jobs services. */ constructor(private readonly ctx: Context) { - ctx.on('session/event', (session, event) => { this.onSessionEvent(session, event) }) ctx.sessionProjections.onChanged((session, key, value, seq) => { this.broadcast({ type: 'projection', @@ -32,6 +31,14 @@ export class SessionControlController { value: value as JsonValue, seq, }) + if (key !== 'inbox') return + const agent = this.ctx.agents.get(session.id) + if (agent?.session !== session) return + this.broadcast({ + type: 'queue', + sessionId: session.id, + items: queueItemsFromInbox(value as InboxState), + }) }) ctx.inject(['jobs'], (jobsCtx) => { jobsCtx.jobs.onJobsChanged((owner) => { this.onJobsChanged(owner) }) @@ -95,17 +102,6 @@ export class SessionControlController { return blocks } - private onSessionEvent(session: Session, event: SessionEvent): void { - if (event.type !== 'agent/inbox/spliced') return - const agent = this.ctx.agents.get(session.id) - if (agent?.session !== session) return - this.broadcast({ - type: 'queue', - sessionId: session.id, - items: queueItems(agent, event.data), - }) - } - private onJobsChanged(owner: Agent | undefined): void { if (owner !== undefined) { this.broadcast({ type: 'jobs', sessionId: owner.id, jobs: this.jobsFor(owner) }) @@ -171,24 +167,22 @@ class ControlQueue { } } -function queueItems( - agent: Agent, - splice?: SessionEventMap['agent/inbox/spliced'], -): SessionQueuedItem[] { - const project = (target: 'next-turn' | 'next-step'): readonly UserMessage[] => { - const messages = target === 'next-turn' ? agent.inbox.nextTurn : agent.inbox.nextStep - return splice?.target === target - ? messages.toSpliced(splice.start, splice.removedCount ?? 0, ...splice.inserted) - : messages - } +function queueItems(agent: Agent): SessionQueuedItem[] { + return queueItemsFromInbox({ + 'next-turn': agent.inbox.nextTurn, + 'next-step': agent.inbox.nextStep, + }) +} + +function queueItemsFromInbox(inbox: InboxState): SessionQueuedItem[] { return [ - ...project('next-turn').map(message => ({ + ...inbox['next-turn'].map(message => ({ id: message.id, placement: 'queued' as const, ...promptRpcId(message), message: { id: message.id, content: message.content as unknown as JsonValue[] }, })), - ...project('next-step').map(message => ({ + ...inbox['next-step'].map(message => ({ id: message.id, placement: message.source.kind === 'user' ? 'steering' as const : 'context' as const, ...promptRpcId(message), diff --git a/packages/api/session-controller/tests/commands-queue-attachment.host.spec.ts b/packages/api/session-controller/tests/commands-queue-attachment.host.spec.ts index 30665e979c..e96a3b128b 100644 --- a/packages/api/session-controller/tests/commands-queue-attachment.host.spec.ts +++ b/packages/api/session-controller/tests/commands-queue-attachment.host.spec.ts @@ -1,6 +1,6 @@ import { Context } from '@deepseek-ai/cordis' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' -import type { Agent, ModelSelectionRef } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' +import type { Agent, Inbox, ModelSelectionRef } from '@deepseek-ai/dsh-agent' import { AttachmentError, AttachmentId } from '@deepseek-ai/dsh-attachment' import type { ImageAttachmentRef } from '@deepseek-ai/dsh-attachment' import { createAssistantMessage, createUserMessage, MessageId } from '@deepseek-ai/dsh-llm' @@ -13,6 +13,7 @@ import { subagentIdentityProjectionDefinition } from '@deepseek-ai/dsh-subagent/ import { describe, expect, it, vi } from 'vitest' import { ApiSessionAgentController } from '../src/agent.ts' import { SessionCommandController } from '../src/commands.ts' +import { createInboxStub } from '@deepseek-ai/dsh-agent-loop-testkit' import { installSessionReadTestServices, testSessionPersistence } from './test-remote.ts' async function commandHarness( @@ -68,7 +69,7 @@ async function commandHarness( provider: 1, } as never) } - const inbox = new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }) + const inbox = createInboxStub() const steer = vi.fn((message: UserMessage) => { inbox.append('next-step', message) }) const cancel = vi.fn() const agent = { diff --git a/packages/api/session-controller/tests/commands-upload-file.host.spec.ts b/packages/api/session-controller/tests/commands-upload-file.host.spec.ts index 4e7565c7ff..a6073c5f2a 100644 --- a/packages/api/session-controller/tests/commands-upload-file.host.spec.ts +++ b/packages/api/session-controller/tests/commands-upload-file.host.spec.ts @@ -1,5 +1,6 @@ import { Context } from '@deepseek-ai/cordis' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' +import { createInboxStub } from '@deepseek-ai/dsh-agent-loop-testkit' import type { Agent, ModelSelectionRef } from '@deepseek-ai/dsh-agent' import AttachmentStore, { AttachmentId } from '@deepseek-ai/dsh-attachment' import type { @@ -38,7 +39,7 @@ async function uploadHarness(origin?: 'subagent'): Promise<{ const session = ctx.sessions.create(SESSION, { meta: { cwd: '/workspace', ...(origin === undefined ? {} : { origin }) }, }) - const inbox = new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }) + const inbox = createInboxStub() const followup = vi.fn() const agent = { id: session.id, diff --git a/packages/api/session-controller/tests/control-jobs.host.spec.ts b/packages/api/session-controller/tests/control-jobs.host.spec.ts index cf6378f5e8..4c88ba21df 100644 --- a/packages/api/session-controller/tests/control-jobs.host.spec.ts +++ b/packages/api/session-controller/tests/control-jobs.host.spec.ts @@ -1,5 +1,5 @@ import { Context } from '@deepseek-ai/cordis' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import type { JobOutcome } from '@deepseek-ai/dsh-jobs' import LocalJobRegistry from '@deepseek-ai/dsh-jobs-local' @@ -9,6 +9,7 @@ import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { describe, expect, it } from 'vitest' import { SessionControlController } from '../src/control.ts' import type { SessionControlFrame } from '../src/types.ts' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' type BaselineFrame = Extract type JobFrame = Extract @@ -36,20 +37,28 @@ async function harness(withJobs: boolean): Promise<{ }> { const ctx = new Context() await ctx.plugin(SessionStore) - await ctx.plugin(AgentRegistry) await ctx.plugin(SessionProjectionRegistry) + await ctx.plugin(AgentRegistry) if (withJobs) { await ctx.plugin(LocalJobRegistry) ctx.jobs.attachController('session-controller-test') } const session = ctx.sessions.create() - const agent = { + const agent: Agent = { id: session.id, + options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: unsupportedInbox(), status: 'idle', ctx, - } as Agent + send: () => {}, + followup: () => {}, + steer: () => {}, + inject: () => {}, + cancel: () => {}, + runMaintenance: task => task(new AbortController().signal), + whenIdle: () => Promise.resolve(), + } ctx.agents.register(agent) const control = new SessionControlController(ctx) await new Promise(resolve => setTimeout(resolve, 0)) diff --git a/packages/api/session-controller/tests/control-queue.host.spec.ts b/packages/api/session-controller/tests/control-queue.host.spec.ts index 0a1e5420a7..b3f1d9b19d 100644 --- a/packages/api/session-controller/tests/control-queue.host.spec.ts +++ b/packages/api/session-controller/tests/control-queue.host.spec.ts @@ -1,11 +1,20 @@ import { Context } from '@deepseek-ai/cordis' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' -import type { Agent } from '@deepseek-ai/dsh-agent' +import type { Agent, Inbox } from '@deepseek-ai/dsh-agent' import { createUserMessage } from '@deepseek-ai/dsh-llm' -import SessionStore, { SessionId } from '@deepseek-ai/dsh-session' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' -import { describe, expect, it } from 'vitest' +import { SessionId } from '@deepseek-ai/dsh-session' +import { afterEach, describe, expect, it } from 'vitest' import { SessionControlController } from '../src/control.ts' +import type { SessionControlFrame } from '../src/types.ts' +import { + mountAgentLoopTestDependencies, + mountAgentLoopTestHarness, +} from '@deepseek-ai/dsh-agent-loop-testkit' + +const ownedContexts = new Set() +afterEach(async () => { + await Promise.all([...ownedContexts].map(ctx => ctx.fiber.dispose())) + ownedContexts.clear() +}) async function harness(): Promise<{ ctx: Context @@ -14,14 +23,11 @@ async function harness(): Promise<{ inbox: Inbox }> { const ctx = new Context() - await ctx.plugin(SessionStore) - await ctx.plugin(AgentRegistry) - await ctx.plugin(SessionProjectionRegistry) - const session = ctx.sessions.create(SessionId('queue-session')) - const inbox = new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }) - const agent = { id: session.id, session, inbox, status: 'running', ctx } as Agent - ctx.agents.register(agent) - return { ctx, control: new SessionControlController(ctx), agent, inbox } + ownedContexts.add(ctx) + await mountAgentLoopTestDependencies(ctx) + const loop = await mountAgentLoopTestHarness(ctx) + const agent = await loop.create(SessionId('queue-session')) + return { ctx, control: new SessionControlController(ctx), agent, inbox: agent.inbox } } function message(text: string, source: 'user' | 'plugin' = 'user') { @@ -32,6 +38,17 @@ function message(text: string, source: 'user' | 'plugin' = 'user') { } describe('Session control queue projection', () => { + /** Consume frames until the next queue replacement (inbox projection frames interleave). */ + async function nextQueueFrame( + iterator: AsyncIterator, + ): Promise> { + for (;;) { + const next = await iterator.next() + if (next.done) throw new Error('stream ended before a queue frame') + if (next.value.type === 'queue') return next.value + } + } + it('projects both pending lists in baselines and live replacement frames', async () => { const { control, inbox } = await harness() const queued = message('queued') @@ -59,13 +76,41 @@ describe('Session control queue projection', () => { const replacement = message('replacement') inbox.append('next-turn', replacement) - const replaced = await iterator.next() - if (replaced.done || replaced.value.type !== 'queue') throw new Error('missing queue replacement') - expect(replaced.value.items.map(item => item.id)).toContain(replacement.id) + const replaced = await nextQueueFrame(iterator) + expect(replaced.items.map(item => item.id)).toContain(replacement.id) inbox.remove(steering.id) - const removed = await iterator.next() - if (removed.done || removed.value.type !== 'queue') throw new Error('missing queue replacement') - expect(removed.value.items.map(item => item.id)).not.toContain(steering.id) + const removed = await nextQueueFrame(iterator) + expect(removed.items.map(item => item.id)).not.toContain(steering.id) + + abort.abort() + await iterator.next() + }) + + it('derives queue replacements from the completed projection regardless of registration order', async () => { + const ctx = new Context() + ownedContexts.add(ctx) + await mountAgentLoopTestDependencies(ctx) + const loop = await mountAgentLoopTestHarness(ctx) + const control = new SessionControlController(ctx) + const agent = await loop.create(SessionId('late-projection-queue')) + const { inbox } = agent + const abort = new AbortController() + const iterator = control.control(abort.signal)[Symbol.asyncIterator]() + await iterator.next() + const pending = message('late projection') + + inbox.append('next-turn', pending) + + await expect(iterator.next()).resolves.toMatchObject({ + value: { + type: 'projection', + key: 'inbox', + value: { 'next-turn': [{ id: pending.id }], 'next-step': [] }, + }, + }) + await expect(nextQueueFrame(iterator)).resolves.toMatchObject({ + items: [{ id: pending.id, placement: 'queued' }], + }) abort.abort() await iterator.next() @@ -133,14 +178,19 @@ describe('Session control queue projection', () => { const { ctx, control, inbox } = await harness() const iterator = control.control(new AbortController().signal)[Symbol.asyncIterator]() await iterator.next() - inbox.append('next-turn', message('first')) - inbox.append('next-turn', message('second')) + const first = message('first') + const second = message('second') + inbox.append('next-turn', first) + inbox.append('next-turn', second) - const first = await iterator.next() - expect(first).toMatchObject({ done: false, value: { type: 'queue' } }) + const queues: Extract[] = [] + ownedContexts.delete(ctx) await ctx.fiber.dispose() - const second = await iterator.next() - expect(second).toMatchObject({ done: false, value: { type: 'queue' } }) - await expect(iterator.next()).resolves.toMatchObject({ done: true }) + for (;;) { + const next = await iterator.next() + if (next.done) break + if (next.value.type === 'queue') queues.push(next.value) + } + expect(queues.map(queue => queue.items.map(item => item.id))).toEqual([[first.id], [first.id, second.id]]) }) }) diff --git a/packages/api/session-controller/tests/session-cold.host.spec.ts b/packages/api/session-controller/tests/session-cold.host.spec.ts index deb58794ca..088fefad1c 100644 --- a/packages/api/session-controller/tests/session-cold.host.spec.ts +++ b/packages/api/session-controller/tests/session-cold.host.spec.ts @@ -8,14 +8,15 @@ import { SESSION_FORMAT_VERSION, SessionLogOffset, SessionSeq } from '@deepseek- import { describe, expect, it, vi } from 'vitest' import { Context } from '@deepseek-ai/cordis' import SessionStore from '@deepseek-ai/dsh-session' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import { SessionHistoryController } from '@deepseek-ai/dsh-api-session-controller/src/history.ts' import { subagentIdentityProjectionDefinition } from '@deepseek-ai/dsh-subagent/src/projection.ts' import TypertRegistry from '@deepseek-ai/dsh-typert-registry' import { createUserMessage, MessageId } from '@deepseek-ai/dsh-llm' import { snapshotSubagentDescriptor } from '@deepseek-ai/dsh-subagent' -import type { Agent } from '@deepseek-ai/dsh-agent' -import type { Session, SessionEvent, SessionHeader, SessionId } from '@deepseek-ai/dsh-session' +import { createInboxStub } from '@deepseek-ai/dsh-agent-loop-testkit' +import type { Agent, Inbox } from '@deepseek-ai/dsh-agent' +import type { SessionEvent, SessionHeader, SessionId } from '@deepseek-ai/dsh-session' import type { SessionPromptRequest, SessionRequestId } from '../src/types.ts' import { SessionPersistenceRevision, @@ -42,8 +43,8 @@ function promptRequest( } } -function inboxFor(session: Session): Inbox { - return new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }) +function inboxFor(): Inbox { + return createInboxStub() } function header(id: string, createdAt: number, extra: Partial = {}): SessionHeader { @@ -545,7 +546,7 @@ describe('subagent ownership fence', () => { }) const followup = vi.fn() const agent = { - id: session.id, session, inbox: inboxFor(session), status: 'idle', ctx, followup, + id: session.id, session, inbox: inboxFor(), status: 'idle', ctx, followup, } as unknown as Agent ctx.agents.register(agent) const remote = createSessionTestRemote(ctx, { defaultModelSelection: () => ({ provider: 'p', model: 'm' }), cwd: '/tmp' }) @@ -566,7 +567,7 @@ describe('subagent ownership fence', () => { const session = ctx.sessions.create(sid('session-browser-zone'), { meta: { cwd: '/proj' } }) const followup = vi.fn() const agent = { - id: session.id, session, inbox: inboxFor(session), status: 'idle', ctx, followup, + id: session.id, session, inbox: inboxFor(), status: 'idle', ctx, followup, } as unknown as Agent ctx.agents.register(agent) const remote = createSessionTestRemote(ctx, { @@ -687,7 +688,7 @@ describe('sessions.prompt synchronous rejection', () => { ctx.agents.register({ id: session.id, session, - inbox: inboxFor(session), + inbox: inboxFor(), status: 'idle', ctx, followup: () => { throw new Error('agent "session-throwing" lifecycle disposed') }, diff --git a/packages/api/session-controller/tests/session-projections.host.spec.ts b/packages/api/session-controller/tests/session-projections.host.spec.ts index 1456e87abc..90ffb10456 100644 --- a/packages/api/session-controller/tests/session-projections.host.spec.ts +++ b/packages/api/session-controller/tests/session-projections.host.spec.ts @@ -7,19 +7,18 @@ * pushed through the control stream. */ -import { describe, expect, it, vi } from 'vitest' +import { afterEach, describe, expect, it, vi } from 'vitest' import { mkdtemp, readFile, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { Context } from '@deepseek-ai/cordis' import { z } from 'zod' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import { AttachmentStore } from '@deepseek-ai/dsh-attachment' import { agentPresetProjectionDefinition } from '@deepseek-ai/dsh-agent-presets' -import type { Agent } from '@deepseek-ai/dsh-agent' import { createUserMessage } from '@deepseek-ai/dsh-llm' -import SessionStore, { SESSION_FORMAT_VERSION, SessionId, SessionSeq } from '@deepseek-ai/dsh-session' -import type { Session } from '@deepseek-ai/dsh-session' +import SessionStore, { SESSION_FORMAT_VERSION, SessionId, SessionLogOffset, SessionSeq } from '@deepseek-ai/dsh-session' +import type { Session, SessionEvent, SessionHeader, UserMessage } from '@deepseek-ai/dsh-session' import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import type { ProjectionDefinition } from '@deepseek-ai/dsh-session-projection' import SessionProjectionCache, { projectionCacheDomainSpec } from '@deepseek-ai/dsh-session-projection-cache' @@ -27,8 +26,19 @@ import Storage from '@deepseek-ai/dsh-storage' import * as StorageDomain from '@deepseek-ai/dsh-storage-domain' import * as StorageJson from '@deepseek-ai/dsh-storage-json' import type { SessionControlFrame, SessionFollowFrame } from '@deepseek-ai/dsh-api-session-controller/types' +import { + mountAgentLoopTestDependencies, + mountAgentLoopTestHarness, +} from '@deepseek-ai/dsh-agent-loop-testkit' import { createSessionTestRemote, testSessionPersistence, type TestSessionRemote } from './test-remote.ts' +const ownedContexts = new Set() +afterEach(async () => { + await Promise.all([...ownedContexts].map(ctx => ctx.fiber.dispose())) + ownedContexts.clear() +}) +let nextHarnessSession = 1 + declare module '@deepseek-ai/dsh-session-projection/types' { interface SessionProjectionStateMap { 'test/last-user': LastUserState @@ -108,15 +118,35 @@ const privatePromptUnit = () => ({ stateVersion: 1, }) satisfies ProjectionDefinition<'test/private-prompt', string | null> -async function harness(withRegistry: boolean): Promise<{ ctx: Context; session: Session }> { +async function harness(withRegistry: boolean): Promise<{ + ctx: Context + session: Session + readonly claim: (target: 'next-turn' | 'next-step') => UserMessage[] +}> { const ctx = new Context() - await ctx.plugin(SessionStore) - await ctx.plugin(AgentRegistry) - if (withRegistry) await ctx.plugin(SessionProjectionRegistry) - const session = ctx.sessions.create(undefined, { meta: { cwd: '/workspace' } }) - // The gateway reads both the session and durable inbox baseline. - ctx.agents.register({ id: session.id, session, inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), status: 'idle', ctx } as Agent) - return { ctx, session } + ownedContexts.add(ctx) + if (!withRegistry) { + await ctx.plugin(SessionStore) + await ctx.plugin(AgentRegistry) + const session = ctx.sessions.create(undefined, { meta: { cwd: '/workspace' } }) + return { + ctx, + session, + claim: () => { throw new Error('inbox is unavailable without the projection registry') }, + } + } + await mountAgentLoopTestDependencies(ctx) + const loop = await mountAgentLoopTestHarness(ctx) + const agent = await loop.create( + SessionId(`session-projections-${String(nextHarnessSession++)}`), + {}, + { cwd: '/workspace' }, + ) + return { + ctx, + session: agent.session, + claim: target => loop.claim(agent, target, 1), + } } /** Append `count` user messages so the log has paginable message boundaries. */ @@ -205,6 +235,74 @@ describe('session.history projections block', () => { expect(last?.event.seq).toBe(projections.asOfSeq) }) + it('reconstructs a cold persisted queue without publishing or resuming an Agent', async () => { + const { ctx } = await harness(true) + const coldId = SessionId('cold-persisted-queue') + const meta: SessionHeader = { version: SESSION_FORMAT_VERSION, id: coldId, createdAt: 1, cwd: '/tmp', isSeeded: false } + const message = createUserMessage({ + content: [{ type: 'text', text: 'survive process restart' }], + source: { kind: 'user' }, + }) + const events: SessionEvent[] = [{ + type: 'agent/inbox/spliced', + seq: SessionSeq(0), + time: 2, + data: { target: 'next-turn', start: 0, inserted: [message] }, + }] + ctx.provide('sessionPersistence', testSessionPersistence(ctx, { + list: () => Promise.resolve([meta]), + inspect: () => Promise.resolve({ meta, events, inheritedEventCount: SessionLogOffset(0) }), + }) as never) + const snapshot = await opening(remote(ctx), coldId) + + expect(snapshot.projections.values.inbox).toEqual({ + 'next-turn': [message], + 'next-step': [], + }) + expect(ctx.agents.get(coldId)).toBeUndefined() + expect(ctx.sessions.get(coldId)).toBeUndefined() + }) + + it('removes claimed steering from the pending Inbox projection immediately', async () => { + const { ctx, session, claim } = await harness(true) + const proxy = remote(ctx) + const message = createUserMessage({ + content: [{ type: 'text', text: 'apply this now' }], + source: { kind: 'user' }, + }) + const agent = ctx.agents.get(session.id) + if (agent === undefined) throw new Error('missing Agent') + agent.inbox.append('next-step', message) + claim('next-step') + + const during = await opening(proxy, session.id) + expect(during.projections.values.inbox).toEqual({ + 'next-turn': [], + 'next-step': [], + }) + + session.append('user/message', message, { surfaceOp: 'append' }) + const settled = await opening(proxy, session.id) + expect(settled.projections.values.inbox).toEqual({ + 'next-turn': [], + 'next-step': [], + }) + + const rejected = createUserMessage({ + content: [{ type: 'text', text: 'reject this pre-step' }], + source: { kind: 'user' }, + }) + session.append('turn/start', { turn: 1 }) + agent.inbox.append('next-step', rejected) + claim('next-step') + session.append('turn/end', { turn: 1, reason: { kind: 'blocked' } }) + const closed = await opening(proxy, session.id) + expect(closed.projections.values.inbox).toEqual({ + 'next-turn': [], + 'next-step': [], + }) + }) + it('returns a complete current replacement cut on each follow generation', async () => { const { ctx, session } = await harness(true) ctx.sessionProjections.register(lastUserUnit()) diff --git a/packages/bundle/headless/package.json b/packages/bundle/headless/package.json index b6aa2d08f1..31a817873a 100644 --- a/packages/bundle/headless/package.json +++ b/packages/bundle/headless/package.json @@ -59,6 +59,8 @@ "@deepseek-ai/cordis-plugin-loader": "workspace:^", "@deepseek-ai/dsh-agent": "workspace:^", "@deepseek-ai/dsh-agent-default-model": "workspace:^", + "@deepseek-ai/dsh-agent-loop": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^" } diff --git a/packages/bundle/headless/tests/headless.spec.ts b/packages/bundle/headless/tests/headless.spec.ts index b380537720..e590b69b83 100644 --- a/packages/bundle/headless/tests/headless.spec.ts +++ b/packages/bundle/headless/tests/headless.spec.ts @@ -2,12 +2,14 @@ import { afterEach, describe, expect, it } from 'vitest' import { Context } from '@deepseek-ai/cordis' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent, AgentHandle, AssistantStreamFrame, CreateAgentOptions } from '@deepseek-ai/dsh-agent' import AgentDefaultModelConfig from '@deepseek-ai/dsh-agent-default-model' import { LlmAttemptId, createAssistantMessage, type StreamChunk } from '@deepseek-ai/dsh-llm' import SessionStore from '@deepseek-ai/dsh-session' +import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import type { Session, UserMessage } from '@deepseek-ai/dsh-session' +import { createInboxStub } from '@deepseek-ai/dsh-agent-loop-testkit' import { apply, Config, internals } from '../src/index.ts' const originalInternals = { ...internals } @@ -82,6 +84,7 @@ async function bench(script: Script): Promise<{ let err = '' const order: string[] = [] await ctx.plugin(SessionStore) + await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentRegistry) await ctx.plugin(AgentDefaultModelConfig, { provider: 'test-provider', model: 'test-model' }) ctx.agents.setFactory({ @@ -89,16 +92,15 @@ async function bench(script: Script): Promise<{ const session = ctx.sessions.create(options.sessionId, { ...options.meta === undefined ? {} : { meta: options.meta }, }) + const inbox = createInboxStub() let idle = Promise.resolve() - const agent = {} as Agent - const agentCtx = ownerCtx.extend({ agent }) - Object.assign(agent, { + const agent: Agent = { id: session.id, options: options.agentOptions ?? {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox, status: 'idle', - ctx: agentCtx, + ctx: ownerCtx, cancel: () => {}, runMaintenance: () => Promise.reject(new Error('not used')), send: () => {}, @@ -109,7 +111,11 @@ async function bench(script: Script): Promise<{ steer: () => {}, inject: () => {}, whenIdle: () => idle, - } satisfies Partial) + } + const agentCtx = ownerCtx.extend({ agent }) + Object.assign(agent, { + ctx: agentCtx, + }) await options.setup?.(agentCtx) script.before?.(session) ctx.agents.register(agent) @@ -158,6 +164,25 @@ describe('headless runner', () => { await test.ctx.fiber.dispose() }) + it('ignores durable inbox events before the first owned turn', async () => { + const test = await bench({ + afterPrompt(session, message) { + session.append('agent/inbox/spliced', { + target: 'next-turn', + start: 0, + inserted: [message], + }) + appendTurn(session, 1, message, 'answer after inbox activity', true) + }, + }) + expect(await test.run()).toMatchObject({ + code: 0, + out: 'answer after inbox activity\n', + err: '', + }) + await test.ctx.fiber.dispose() + }) + it('waits for asynchronously appended events instead of racing Agent idleness', async () => { const test = await bench({ afterPrompt: async (session, message) => { @@ -336,16 +361,20 @@ describe('headless runner', () => { }) it('fails when an event below the captured Session length cannot be read', async () => { + let capturedLength = 0 const test = await bench({ afterPrompt(session, message) { appendTurn(session, 1, message, 'unreachable', true) + capturedLength = session.seq Object.defineProperty(session, 'eventAt', { value: () => undefined }) }, }) - expect(await test.run()).toMatchObject({ + const result = await test.run() + expect(capturedLength).toBeGreaterThan(0) + expect(result).toMatchObject({ code: 1, out: '', - err: 'dsh: headless summary cannot read seq 0 below captured length 7\n', + err: `dsh: headless summary cannot read seq 0 below captured length ${String(capturedLength)}\n`, }) await test.ctx.fiber.dispose() }) diff --git a/packages/compaction/compaction-basic/tests/compaction-loop-repro.spec.ts b/packages/compaction/compaction-basic/tests/compaction-loop-repro.spec.ts index 3ab00ca5e7..b7b1bb883e 100644 --- a/packages/compaction/compaction-basic/tests/compaction-loop-repro.spec.ts +++ b/packages/compaction/compaction-basic/tests/compaction-loop-repro.spec.ts @@ -13,7 +13,6 @@ import * as SessionInvariant from '@deepseek-ai/dsh-session/invariant' import * as AgentInvariant from '@deepseek-ai/dsh-agent/invariant' import * as AgentLoopInvariant from '@deepseek-ai/dsh-agent-loop/invariant' import { BasicCompactionEngine } from '@deepseek-ai/dsh-compaction-basic' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import TokenMeter from '@deepseek-ai/dsh-token-meter' import * as LlmRetry from '@deepseek-ai/dsh-llm-retry' import { Session, SessionId, type SessionEvent, type SurfaceEvent } from '@deepseek-ai/dsh-session' @@ -151,9 +150,6 @@ async function harness(toolSteps: number): Promise<{ ctx: Context; compact: Repr const ctx = new Context() await mountAgentLoopTestDependencies(ctx) await mountInvariants(ctx) - // AgentLoop and TokenMeter both declare the registry as a required - // injection; mount it before either activates. - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(TokenMeter) ctx.llm.registerAdapter(['mock'], new StepwiseToolAdapter(toolSteps)) @@ -317,7 +313,6 @@ describe('context-overflow recovery across the real loop and compaction-basic', const adapter = new OverflowRecoveryAdapter(delivery) await mountAgentLoopTestDependencies(ctx) await mountInvariants(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(TokenMeter) ctx.llm.registerAdapter(['mock'], adapter) @@ -396,7 +391,6 @@ describe('context-overflow recovery across the real loop and compaction-basic', const adapter = new OverflowRecoveryAdapter('thrown', true) await mountAgentLoopTestDependencies(ctx) await mountInvariants(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(LlmRetry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(TokenMeter) diff --git a/packages/compaction/compaction-basic/tests/manual-compaction.spec.ts b/packages/compaction/compaction-basic/tests/manual-compaction.spec.ts index 5e295baf0a..e7425500f1 100644 --- a/packages/compaction/compaction-basic/tests/manual-compaction.spec.ts +++ b/packages/compaction/compaction-basic/tests/manual-compaction.spec.ts @@ -104,7 +104,6 @@ async function loopHarness(): Promise { await ctx.plugin(AgentInvariant) await ctx.plugin(AgentLoopInvariant) await ctx.plugin(CompactionInvariant) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(TokenMeter) const adapter = new TextAdapter() diff --git a/packages/context/agent-instructions/package.json b/packages/context/agent-instructions/package.json index 5a3eda934e..91e4df65bf 100644 --- a/packages/context/agent-instructions/package.json +++ b/packages/context/agent-instructions/package.json @@ -45,6 +45,7 @@ "@deepseek-ai/cordis-plugin-loader": "workspace:^", "@deepseek-ai/dsh-agent": "workspace:^", "@deepseek-ai/dsh-agent-loop": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-fs": "workspace:^", "@deepseek-ai/dsh-fs-local": "workspace:^", "@deepseek-ai/dsh-home-paths": "workspace:^", diff --git a/packages/context/agent-instructions/tests/agent-instructions.e2e.ts b/packages/context/agent-instructions/tests/agent-instructions.e2e.ts index 6e9d89c040..7e35bdee21 100644 --- a/packages/context/agent-instructions/tests/agent-instructions.e2e.ts +++ b/packages/context/agent-instructions/tests/agent-instructions.e2e.ts @@ -4,14 +4,10 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, describe, expect, it } from 'vitest' import { Context } from '@deepseek-ai/cordis' -import LlmRuntime from '@deepseek-ai/dsh-llm' -import SessionStore, { SessionId } from '@deepseek-ai/dsh-session' -import SystemPrompt from '@deepseek-ai/dsh-system-prompt' -import ToolRuntime from '@deepseek-ai/dsh-tools' -import AgentRegistry from '@deepseek-ai/dsh-agent' +import { SessionId } from '@deepseek-ai/dsh-session' import type { Agent } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' +import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek' import * as WorkspaceContext from '@deepseek-ai/dsh-agent-instructions' import { candidateScopeKey } from '../src/render.ts' @@ -38,12 +34,9 @@ async function harness(): Promise<{ ctx: Context; agent: Agent }> { await mkdir(join(workdir, '.git'), { recursive: true }) await writeFile(join(workdir, 'AGENTS.md'), `If the user asks for the workspace context handshake, reply with exactly this string and nothing else: ${PROBE}.\n`) ctx = new Context() - await ctx.plugin(LlmRuntime) - await ctx.plugin(SessionStore) - await ctx.plugin(SessionProjectionRegistry) - await ctx.plugin(SystemPrompt, { personaPrefix: 'Answer the user exactly and concisely.' }) - await ctx.plugin(ToolRuntime) - await ctx.plugin(AgentRegistry) + await mountAgentLoopTestDependencies(ctx, { + systemPrompt: { personaPrefix: 'Answer the user exactly and concisely.' }, + }) await ctx.plugin(LocalFileSystem, { cwd: '/' }) await ctx.plugin(ToolFs) await ctx.plugin(WorkspaceContext, { maxBytes: 65536 }) diff --git a/packages/context/agent-instructions/tests/agent-instructions.spec.ts b/packages/context/agent-instructions/tests/agent-instructions.spec.ts index 28669e88d0..7e3586faeb 100644 --- a/packages/context/agent-instructions/tests/agent-instructions.spec.ts +++ b/packages/context/agent-instructions/tests/agent-instructions.spec.ts @@ -1,15 +1,15 @@ import { chmod, mkdtemp, mkdir, rm, stat, symlink, utimes, writeFile } from 'node:fs/promises' import { dirname, isAbsolute, join, relative, resolve } from 'node:path' import { tmpdir } from 'node:os' -import { describe, expect, it, vi } from 'vitest' +import { afterAll, describe, expect, it, vi } from 'vitest' import { Context } from '@deepseek-ai/cordis' import Loader from '@deepseek-ai/cordis-plugin-loader' import * as workspaceContext from '@deepseek-ai/dsh-agent-instructions' import LlmRuntime, { createUserMessage, ToolCallId, type Message, type StreamChunk } from '@deepseek-ai/dsh-llm' -import SessionStore, { Session, SessionId, SessionSeq, SESSION_FORMAT_VERSION, type SessionEvent, type UserMessage } from '@deepseek-ai/dsh-session' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' -import AgentRegistry, { agentEvents, Inbox, type Agent } from '@deepseek-ai/dsh-agent' +import SessionStore, { SessionId, SessionSeq, type SessionEvent, type SurfaceIntent, type UserMessage } from '@deepseek-ai/dsh-session' +import AgentRegistry, { agentEvents, type Agent } from '@deepseek-ai/dsh-agent' import AgentLoop, { turnBoundaryProjectionDefinition } from '@deepseek-ai/dsh-agent-loop' +import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { FileSystem, FsTargetKey, FsVersion } from '@deepseek-ai/dsh-fs' import type { FsDirEntry, @@ -43,11 +43,27 @@ import { import { resolveConfig } from '../src/config.ts' import { candidateScopeKey, renderInstructionChanges, renderWorkspaceInstructionSet, USER_GLOBAL_DIRECTORY, USER_GLOBAL_FILE } from '../src/render.ts' import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' +import { + mountAgentLoopTestDependencies, + mountAgentLoopTestHarness, +} from '@deepseek-ai/dsh-agent-loop-testkit' /** Per-candidate reconciliation scope key: directory paired with the file name. */ const sk = (directory: string, candidateName: string): string => candidateScopeKey(directory, candidateName) const testToolSignal = new AbortController().signal +const isolatedInboxCtx = new Context() +await mountAgentLoopTestDependencies(isolatedInboxCtx) +const isolatedAgentLoop = await mountAgentLoopTestHarness(isolatedInboxCtx) +let nextStubSession = 1 +afterAll(() => isolatedInboxCtx.fiber.dispose()) + +type TestAgent = Agent + +/** Admit one test Agent's pending input through the production loop driver. */ +function claimInbox(agent: Agent, target: 'next-turn' | 'next-step'): UserMessage[] { + return isolatedAgentLoop.claim(agent, target, 1) +} const requestTimeoutMs = process.platform === 'win32' ? 5_000 : 1_000 async function tempRepo(): Promise { @@ -188,26 +204,30 @@ async function mountFileToolsAndWorkspaceContext(ctx: Context, config: workspace return mountWorkspaceContextPlugin(ctx, config) } -function stubAgent(cwd?: string, seed: readonly SessionEvent[] = []): Agent { - const id = SessionId('s1') - const session = Session.create(id, seed, cwd === undefined - ? undefined - : { version: SESSION_FORMAT_VERSION, id, createdAt: 0, cwd, isSeeded: false }) - return { - ctx: new Context(), - id: SessionId('a1'), - options: {}, - session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), - status: 'idle', - send: () => {}, - followup: () => {}, - steer: () => {}, - inject: () => { throw new Error('agent-instructions must append directly to the open step') }, - cancel() {}, - runMaintenance: task => task(new AbortController().signal), - whenIdle: () => Promise.resolve(), +async function stubAgent(cwd?: string, seed: readonly SessionEvent[] = []): Promise { + const id = SessionId(`agent-instructions-${String(nextStubSession++)}`) + const agent = await isolatedAgentLoop.create( + id, + {}, + cwd === undefined ? {} : { cwd }, + ) + const append = agent.session.append.bind(agent.session) as unknown as ( + type: SessionEvent['type'], + data: SessionEvent['data'], + opts?: Partial, + ) => SessionEvent + for (const event of seed) { + if ('surfaceOp' in event || 'sourceEventSeqs' in event) { + append(event.type, event.data, { + ...event.surfaceOp === undefined ? {} : { surfaceOp: event.surfaceOp }, + ...event.sourceEventSeqs === undefined ? {} : { sourceEventSeqs: event.sourceEventSeqs }, + }) + } else { + append(event.type, event.data) + } } + if (seed.at(-1)?.type !== 'session/end-seed') agent.session.append('session/end-seed', {}) + return agent } function stubToolExecution( @@ -255,10 +275,10 @@ function baselineEvents(agent: Agent): SessionEvent[] { && event.data.source.baseline === true) } -async function appendAdditionalContexts(ctx: Context, agent: Agent): Promise { +async function appendAdditionalContexts(ctx: Context, agent: TestAgent): Promise { await syncedWorkspaceContext(ctx, agent) let lastSeq: SessionSeq | undefined - for (const claimed of agent.inbox.claim('next-step', 1)) { + for (const claimed of claimInbox(agent, 'next-step')) { if (claimed.source.kind !== 'agent-instructions') continue const event = agent.session.append('user/message', claimed, { surfaceOp: 'append' }) ctx.emit('session/event', agent.session, event) @@ -269,14 +289,14 @@ async function appendAdditionalContexts(ctx: Context, agent: Agent): Promise() -async function composeBaselinePrefix(ctx: Context, agent: Agent): Promise { +async function composeBaselinePrefix(ctx: Context, agent: TestAgent): Promise { const signal = new AbortController().signal await agentEvents(ctx, agent).waterfall( 'agent/pre-step', { messages: [], turn: 1, step: 1, signal }, () => Promise.resolve({ kind: 'enter' as const, messages: [] }), ) - const claimed = agent.inbox.claim('next-step', 1) + const claimed = claimInbox(agent, 'next-step') const decision = await agentEvents(ctx, agent).waterfall( 'agent/pre-step', { messages: claimed, turn: 1, step: 2, signal }, @@ -492,7 +512,7 @@ describe('workspace context instruction discovery', () => { await symlink(join(outside, 'shared.md'), join(root, 'AGENTS.md')) const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -1019,7 +1039,7 @@ describe('workspace context request injection', () => { callId: ToolCallId('missing-turn-boundary'), name: 'read', arguments: { file_path: 'file.txt' }, - agent: stubAgent('/virtual/repo'), + agent: await stubAgent('/virtual/repo'), signal: testToolSignal, }) @@ -1036,7 +1056,7 @@ describe('workspace context request injection', () => { const ctx = new Context() try { await mountWorkspaceContextPlugin(ctx, { maxBytes: 65536 }) - const agent = stubAgent('/virtual/repo') + const agent = await stubAgent('/virtual/repo') await composeBaselinePrefix(ctx, agent) @@ -1054,7 +1074,7 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'repo rule') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -1093,7 +1113,7 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'repo rule') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const first = await composeBaselinePrefix(ctx, agent) const second = await composeBaselinePrefix(ctx, agent) @@ -1115,12 +1135,12 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'repo rule') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const original = stubAgent(root) + const original = await stubAgent(root) await composeBaselinePrefix(ctx, original) - const firstResume = stubAgent(root, original.session.snapshotEvents()) + const firstResume = await stubAgent(root, original.session.snapshotEvents()) await composeBaselinePrefix(ctx, firstResume) - const secondResume = stubAgent(root, firstResume.session.snapshotEvents()) + const secondResume = await stubAgent(root, firstResume.session.snapshotEvents()) await composeBaselinePrefix(ctx, secondResume) expect(baselineEvents(firstResume)).toHaveLength(1) @@ -1143,11 +1163,11 @@ describe('workspace context request injection', () => { fs.entries.set(join(root, '.git'), { type: 'directory' }) fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'repo rule' }) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const original = stubAgent(root) + const original = await stubAgent(root) await composeBaselinePrefix(ctx, original) fs.throwOnStat.add(join(root, 'AGENTS.md')) - const resumed = stubAgent(root, original.session.snapshotEvents()) + const resumed = await stubAgent(root, original.session.snapshotEvents()) await composeBaselinePrefix(ctx, resumed) expect(baselineEvents(resumed)).toHaveLength(1) @@ -1170,12 +1190,12 @@ describe('workspace context request injection', () => { await write(join(cwd, 'AGENTS.md'), 'package rule') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 700 }) - const original = stubAgent(cwd) + const original = await stubAgent(cwd) await composeBaselinePrefix(ctx, original) - const firstResume = stubAgent(cwd, original.session.snapshotEvents()) + const firstResume = await stubAgent(cwd, original.session.snapshotEvents()) await composeBaselinePrefix(ctx, firstResume) - const secondResume = stubAgent(cwd, firstResume.session.snapshotEvents()) + const secondResume = await stubAgent(cwd, firstResume.session.snapshotEvents()) await composeBaselinePrefix(ctx, secondResume) expect(baselineEvents(secondResume)).toHaveLength(1) @@ -1199,11 +1219,11 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'root '.repeat(200)) const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 700 }) - const original = stubAgent(cwd) + const original = await stubAgent(cwd) await composeBaselinePrefix(ctx, original) await write(join(cwd, 'AGENTS.md'), 'package rule') - const resumed = stubAgent(cwd, original.session.snapshotEvents()) + const resumed = await stubAgent(cwd, original.session.snapshotEvents()) await composeBaselinePrefix(ctx, resumed) expect(baselineEvents(resumed)).toHaveLength(1) @@ -1232,7 +1252,7 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'agents rule') await write(join(root, 'CLAUDE.md'), 'claude rule') await mountWorkspaceContext(originalCtx, { dshHome: home, maxBytes: 65536 }) - const original = stubAgent(root) + const original = await stubAgent(root) await composeBaselinePrefix(originalCtx, original) await mountWorkspaceContext(resumedCtx, { @@ -1240,7 +1260,7 @@ describe('workspace context request injection', () => { maxBytes: 65536, instructionFileCandidates: ['CLAUDE.md', 'AGENTS.md'], }) - const resumed = stubAgent(root, original.session.snapshotEvents()) + const resumed = await stubAgent(root, original.session.snapshotEvents()) await composeBaselinePrefix(resumedCtx, resumed) const baselines = baselineEvents(resumed) @@ -1259,7 +1279,7 @@ describe('workspace context request injection', () => { : []) expect(new Set(baselineIdentities).size).toBe(2) - const repeated = stubAgent(root, resumed.session.snapshotEvents()) + const repeated = await stubAgent(root, resumed.session.snapshotEvents()) await composeBaselinePrefix(resumedCtx, repeated) expect(baselineEvents(repeated)).toHaveLength(2) } finally { @@ -1285,7 +1305,7 @@ describe('workspace context request injection', () => { maxBytes: 65536, instructionFileCandidates: ['AGENTS.md'], }) - const original = stubAgent(root) + const original = await stubAgent(root) await composeBaselinePrefix(agentsCtx, original) await mountWorkspaceContext(claudeCtx, { @@ -1293,7 +1313,7 @@ describe('workspace context request injection', () => { maxBytes: 65536, instructionFileCandidates: ['CLAUDE.md'], }) - const claudeResume = stubAgent(root, original.session.snapshotEvents()) + const claudeResume = await stubAgent(root, original.session.snapshotEvents()) await composeBaselinePrefix(claudeCtx, claudeResume) const claudeBaseline = baselineEvents(claudeResume).at(-1) expect(claudeBaseline?.type === 'user/message' && claudeBaseline.data.source.kind === 'agent-instructions' @@ -1308,7 +1328,7 @@ describe('workspace context request injection', () => { maxBytes: 65536, instructionFileCandidates: ['AGENTS.md'], }) - const restored = stubAgent(root, claudeResume.session.snapshotEvents()) + const restored = await stubAgent(root, claudeResume.session.snapshotEvents()) await composeBaselinePrefix(restoredCtx, restored) const restoredBaseline = baselineEvents(restored).at(-1) expect(restoredBaseline?.type === 'user/message' && restoredBaseline.data.source.kind === 'agent-instructions' @@ -1335,7 +1355,7 @@ describe('workspace context request injection', () => { await mkdir(join(root, '.git'), { recursive: true }) await write(join(root, 'AGENTS.md'), 'agents rule') await mountWorkspaceContext(originalCtx, { dshHome: home, maxBytes: 65536 }) - const original = stubAgent(root) + const original = await stubAgent(root) await composeBaselinePrefix(originalCtx, original) await mountWorkspaceContext(resumedCtx, { @@ -1343,7 +1363,7 @@ describe('workspace context request injection', () => { maxBytes: 65536, instructionFileCandidates: ['POLICY.md'], }) - const resumed = stubAgent(root, original.session.snapshotEvents()) + const resumed = await stubAgent(root, original.session.snapshotEvents()) await composeBaselinePrefix(resumedCtx, resumed) const baselines = baselineEvents(resumed) @@ -1357,7 +1377,7 @@ describe('workspace context request injection', () => { { action: 'remove', scope: sk('.', 'AGENTS.md'), path: 'AGENTS.md' }, ]) - const repeated = stubAgent(root, resumed.session.snapshotEvents()) + const repeated = await stubAgent(root, resumed.session.snapshotEvents()) await composeBaselinePrefix(resumedCtx, repeated) expect(baselineEvents(repeated)).toHaveLength(2) } finally { @@ -1376,7 +1396,7 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'repo rule') const ctx = new Context() const fiber = await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const original = stubAgent(root) + const original = await stubAgent(root) await agentEvents(ctx, original).waterfall( 'agent/pre-step', { messages: [], turn: 1, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) }, @@ -1387,9 +1407,9 @@ describe('workspace context request injection', () => { await fiber.dispose() await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const resumed = stubAgent(root, original.session.snapshotEvents()) + const resumed = await stubAgent(root, original.session.snapshotEvents()) agentEvents(ctx, resumed).emit('agent/session-start', { source: 'resume' }) - const claimed = resumed.inbox.claim('next-step', 1) + const claimed = claimInbox(resumed, 'next-step') const decision = await agentEvents(ctx, resumed).waterfall( 'agent/pre-step', { messages: claimed, turn: 1, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) }, @@ -1421,7 +1441,7 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'old repo rule') const ctx = new Context() const fiber = await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const original = stubAgent(root) + const original = await stubAgent(root) await agentEvents(ctx, original).waterfall( 'agent/pre-step', { messages: [], turn: 1, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) }, @@ -1433,9 +1453,9 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'new repo rule') await fiber.dispose() await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const resumed = stubAgent(root, original.session.snapshotEvents()) + const resumed = await stubAgent(root, original.session.snapshotEvents()) agentEvents(ctx, resumed).emit('agent/session-start', { source: 'resume' }) - const staleClaim = resumed.inbox.claim('next-step', 1) + const staleClaim = claimInbox(resumed, 'next-step') const staleDecision = await agentEvents(ctx, resumed).waterfall( 'agent/pre-step', { messages: staleClaim, turn: 1, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) }, @@ -1474,7 +1494,7 @@ describe('workspace context request injection', () => { await mkdir(join(root, '.git'), { recursive: true }) await write(join(root, 'AGENTS.md'), 'repo rule') await mountWorkspaceContext(originalCtx, { dshHome: home, maxBytes: 65536 }) - const original = stubAgent(root) + const original = await stubAgent(root) await agentEvents(originalCtx, original).waterfall( 'agent/pre-step', { messages: [], turn: 1, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) }, @@ -1486,9 +1506,9 @@ describe('workspace context request injection', () => { await originalCtx.fiber.dispose() if (provideFs) await resumedCtx.plugin(LocalFileSystem, { cwd: '/' }) await mountWorkspaceContextPlugin(resumedCtx, { dshHome: home, maxBytes }) - const resumed = stubAgent(root, original.session.snapshotEvents()) + const resumed = await stubAgent(root, original.session.snapshotEvents()) agentEvents(resumedCtx, resumed).emit('agent/session-start', { source: 'resume' }) - const claimed = resumed.inbox.claim('next-step', 1) + const claimed = claimInbox(resumed, 'next-step') const decision = await agentEvents(resumedCtx, resumed).waterfall( 'agent/pre-step', { messages: claimed, turn: 1, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) }, @@ -1513,7 +1533,7 @@ describe('workspace context request injection', () => { await mkdir(join(root, '.git'), { recursive: true }) const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) agent.session.append('user/message', createUserMessage({ content: [{ type: 'text', text: 'stale nested instructions' }], source: { @@ -1547,7 +1567,7 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'repo rule') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) agent.session.append('user/message', createUserMessage({ content: [{ type: 'text', text: 'stale nested instructions' }], source: { @@ -1586,7 +1606,7 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'repo rule') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const prompt = createUserMessage({ content: [{ type: 'text', text: 'current prompt' }], source: { kind: 'user' }, @@ -1621,7 +1641,7 @@ describe('workspace context request injection', () => { await write(join(home, 'AGENTS.md'), 'global rule') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) await write(join(home, 'AGENTS.md'), 'updated global rule') @@ -1647,7 +1667,7 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'repo rule') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const downstream = { kind: 'reject' as const } const decision = await agentEvents(ctx, agent).waterfall( @@ -1674,7 +1694,7 @@ describe('workspace context request injection', () => { await write(join(root, 'file.txt'), 'hello') const ctx = new Context() const fiber = await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) // Hot remount over the live session: the durable baseline remains @@ -1711,7 +1731,7 @@ describe('workspace context request injection', () => { const ctx = new Context() await ctx.plugin(LocalFileSystem, { cwd: '/' }) const fiber = await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) const baseline = baselineEvents(agent)[0] expect(baseline).toBeDefined() @@ -1744,7 +1764,7 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'first post-compaction request rule') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) const baseline = baselineEvents(agent)[0] expect(baseline).toBeDefined() @@ -1787,13 +1807,13 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'old root rule') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const original = stubAgent(root) + const original = await stubAgent(root) await composeBaselinePrefix(ctx, original) // The first resumed pre-step retains the compatible visible baseline and // appends only the offline file transition needed to reach current state. await write(join(root, 'AGENTS.md'), 'new root rule after offline edit') - const resumed = stubAgent(root, original.session.snapshotEvents()) + const resumed = await stubAgent(root, original.session.snapshotEvents()) // Resume announces its lifecycle start before the first step. agentEvents(ctx, resumed).emit('agent/session-start', { source: 'resume' }) @@ -1827,7 +1847,7 @@ describe('workspace context request injection', () => { await write(join(cwd, 'AGENTS.md'), 'package rule') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 700 }) - const agent = stubAgent(cwd) + const agent = await stubAgent(cwd) await composeBaselinePrefix(ctx, agent) @@ -1859,7 +1879,7 @@ describe('workspace context request injection', () => { } }) - const prefix = await composeBaselinePrefix(ctx, stubAgent(root)) + const prefix = await composeBaselinePrefix(ctx, await stubAgent(root)) expect(prefix).toHaveLength(2) expect(blocksText(prefix[0]?.content)).toContain('Instructions from: AGENTS.md') @@ -1879,7 +1899,7 @@ describe('workspace context request injection', () => { await write(join(root, 'file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) await write(join(root, 'AGENTS.md'), 'new root rule with more detail') @@ -1908,7 +1928,7 @@ describe('workspace context request injection', () => { await write(join(root, 'file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) await rm(join(root, 'AGENTS.md')) @@ -1934,7 +1954,7 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'shared root and global rule') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: root, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -1954,7 +1974,7 @@ describe('workspace context request injection', () => { await write(join(root, 'file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -1978,7 +1998,7 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'x'.repeat(1000)) const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -2013,7 +2033,7 @@ describe('workspace context request injection', () => { fs.entries.set(join(root, '.git'), { type: 'directory' }) fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'ctx.fs rule' }) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -2036,7 +2056,7 @@ describe('workspace context request injection', () => { fs.entries.set(join(root, '.git'), { type: 'directory' }) fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'provider-only rule' }) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -2079,7 +2099,7 @@ describe('workspace context request injection', () => { fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'far too large' }) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536, maxSourceBytes: 4 }) - const prefix = await composeBaselinePrefix(ctx, stubAgent(root)) + const prefix = await composeBaselinePrefix(ctx, await stubAgent(root)) expect(prefix).toEqual([]) expect(fs.readTargets).toEqual([]) @@ -2104,7 +2124,7 @@ describe('workspace context request injection', () => { fs.omitSizes.add(instructionPath) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536, maxSourceBytes: 4 }) - const prefix = await composeBaselinePrefix(ctx, stubAgent(root)) + const prefix = await composeBaselinePrefix(ctx, await stubAgent(root)) expect(prefix).toEqual([]) expect(fs.readTargets).toEqual([instructionPath, instructionPath]) @@ -2128,7 +2148,7 @@ describe('workspace context request injection', () => { await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) const controller = new AbortController() const reason = new Error('cancel prefix') - const pending = agentEvents(ctx, stubAgent(root)).waterfall( + const pending = agentEvents(ctx, await stubAgent(root)).waterfall( 'agent/pre-step', { messages: [], turn: 1, step: 1, signal: controller.signal }, () => Promise.resolve({ kind: 'enter' as const, messages: [] }), @@ -2160,7 +2180,7 @@ describe('workspace context request injection', () => { fs.entries.set(join(home, 'AGENTS.md'), { type: 'file', content: 'ctx global rule' }) fs.entries.set(join(root, 'CLAUDE.md'), { type: 'file', content: 'ctx claude rule' }) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -2186,7 +2206,7 @@ describe('workspace context request injection', () => { fs.entries.set(join(root, '.git'), { type: 'directory' }) fs.entries.set(join(root, 'AGENTS.md'), { type: 'directory' }) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -2209,7 +2229,7 @@ describe('workspace context request injection', () => { fs.entries.set(join(root, '.git'), { type: 'directory' }) fs.entries.set(join(root, 'AGENTS.md'), { type: 'file' }) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -2232,7 +2252,7 @@ describe('workspace context request injection', () => { fs.entries.set(join(root, '.git'), { type: 'directory' }) fs.throwOnStat.add(join(root, 'AGENTS.md')) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -2254,7 +2274,7 @@ describe('workspace context request injection', () => { fs.throwOnStat.add(join(root, 'AGENTS.md')) fs.entries.set(join(root, 'CLAUDE.md'), { type: 'file', content: 'claude sibling rule' }) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -2279,7 +2299,7 @@ describe('workspace context request injection', () => { fs.throwOnStat.add(join(root, '.git')) fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'repo rule' }) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -2301,8 +2321,8 @@ describe('workspace context request injection', () => { await write(join(repoB, 'AGENTS.md'), 'repo B only') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agentA = stubAgent(repoA) - const agentB = stubAgent(repoB) + const agentA = await stubAgent(repoA) + const agentB = await stubAgent(repoB) await composeBaselinePrefix(ctx, agentA) await composeBaselinePrefix(ctx, agentB) @@ -2329,7 +2349,7 @@ describe('workspace context request injection', () => { const ctx = new Context() await ctx.plugin(LocalFileSystem, { cwd: '/' }) await mountWorkspaceContextPlugin(ctx, { maxBytes: 65536 }) - const agent = stubAgent(cwd) + const agent = await stubAgent(cwd) await composeBaselinePrefix(ctx, agent) @@ -2350,7 +2370,7 @@ describe('workspace context request injection', () => { const ctx = new Context() await ctx.plugin(LocalFileSystem, { cwd: '/' }) await mountWorkspaceContextPlugin(ctx, { maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -2371,7 +2391,7 @@ describe('workspace context request injection', () => { const ctx = new Context() const fiber = await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) await fiber.dispose() - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -2390,7 +2410,7 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'repo rule') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 0 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -2409,7 +2429,7 @@ describe('workspace context request injection', () => { await write(join(root, 'AGENTS.md'), 'repo rule') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: -1 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -2427,7 +2447,7 @@ describe('workspace context request injection', () => { await mkdir(join(root, '.git'), { recursive: true }) const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) @@ -2540,6 +2560,7 @@ describe('dynamic nested workspace context injection', () => { ]) await ctx.plugin(LlmRuntime) await ctx.plugin(SessionStore) + await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SystemPrompt) await ctx.plugin(ToolRuntime) await ctx.plugin(AgentRegistry) @@ -2596,8 +2617,8 @@ describe('dynamic nested workspace context injection', () => { expect(state.versions).toEqual(new Map()) }) - it('creates and releases version-cache state only for non-empty updates', () => { - const agent = stubAgent('/repo') + it('creates and releases version-cache state only for non-empty updates', async () => { + const agent = await stubAgent('/repo') const cache: InstructionVersionCache = new WeakMap() const change = { action: 'set' as const, scope: sk('.', 'AGENTS.md'), path: 'AGENTS.md', digest: 'digest' } applyInstructionVersionUpdates(agent.session, [], cache) @@ -2629,7 +2650,7 @@ describe('dynamic nested workspace context injection', () => { callId: ToolCallId('cancelled-dynamic-read'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, - agent: stubAgent(root), + agent: await stubAgent(root), signal: controller.signal, }) @@ -2659,7 +2680,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/deep/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const result = await ctx.tools.execute({ signal: testToolSignal, @@ -2704,7 +2725,7 @@ describe('dynamic nested workspace context injection', () => { await mkdir(join(root, '.git'), { recursive: true }) await write(join(root, 'pkg/AGENTS.md'), 'nested package rule') await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const controller = new AbortController() ctx.emit('tools/result', stubToolExecution({ @@ -2738,7 +2759,7 @@ describe('dynamic nested workspace context injection', () => { maxBytes: 65536, instructionFileCandidates: ['CLAUDE.local.md', 'AGENTS.md', 'CLAUDE.md'], }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, @@ -2771,7 +2792,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/deep/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, @@ -2814,7 +2835,7 @@ describe('dynamic nested workspace context injection', () => { maxBytes: 65536, localInstructionFileCandidates: [], }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, @@ -2842,7 +2863,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/deep/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const first = await ctx.tools.execute({ signal: testToolSignal, @@ -2885,7 +2906,7 @@ describe('dynamic nested workspace context injection', () => { fs.entries.set(join(root, 'pkg/file.txt'), { type: 'file', content: 'hello' }) await ctx.plugin(ToolFs) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const first = await ctx.tools.execute({ signal: testToolSignal, @@ -2922,7 +2943,7 @@ describe('dynamic nested workspace context injection', () => { fs.entries.set(join(root, 'pkg/file.txt'), { type: 'file', content: 'hello' }) await ctx.plugin(ToolFs) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, @@ -2968,8 +2989,8 @@ describe('dynamic nested workspace context injection', () => { await ctx.plugin(ToolFs) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const firstAgent = stubAgent(root) - const secondAgent = stubAgent(root) + const firstAgent = await stubAgent(root) + const secondAgent = await stubAgent(root) const first = await ctx.tools.execute({ signal: testToolSignal, callId: ToolCallId('read-from-first-session'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent: firstAgent, @@ -3000,7 +3021,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, @@ -3043,7 +3064,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, @@ -3081,7 +3102,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/deep/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, @@ -3113,7 +3134,7 @@ describe('dynamic nested workspace context injection', () => { fs.entries.set(join(root, '.git'), { type: 'directory' }) fs.entries.set(join(root, 'pkg/CLAUDE.md'), { type: 'file', content: 'nested rule' }) fs.throwOnStat.add(join(root, 'pkg/AGENTS.md')) - const agent = stubAgent(root) + const agent = await stubAgent(root) const agentsScope = sk('pkg', 'AGENTS.md') const loaded = baselineInstructionState([{ absolutePath: join(root, 'pkg/AGENTS.md'), @@ -3183,7 +3204,7 @@ describe('dynamic nested workspace context injection', () => { const fs = ctx.fs as RecordingFileSystem fs.entries.set(join(root, '.git'), { type: 'directory' }) fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'repo rule' }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const rootScope = sk('.', 'AGENTS.md') const loaded = baselineInstructionState([{ absolutePath: join(root, 'AGENTS.md'), @@ -3228,7 +3249,7 @@ describe('dynamic nested workspace context injection', () => { const fs = ctx.fs as RecordingFileSystem fs.entries.set(join(root, '.git'), { type: 'directory' }) fs.throwOnStat.add(join(root, 'pkg/AGENTS.md')) - const agent = stubAgent(root) + const agent = await stubAgent(root) agent.session.append('user/message', createUserMessage({ content: [{ type: 'text', text: 'removed nested instructions' }], source: { @@ -3268,7 +3289,7 @@ describe('dynamic nested workspace context injection', () => { const fs = ctx.fs as RecordingFileSystem fs.entries.set(join(root, '.git'), { type: 'directory' }) fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'shared rule' }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const resolved = resolveConfig({ dshHome: root, maxBytes: 65536, @@ -3303,7 +3324,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, @@ -3340,7 +3361,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, @@ -3378,7 +3399,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, @@ -3418,7 +3439,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, @@ -3458,7 +3479,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, @@ -3503,7 +3524,7 @@ describe('dynamic nested workspace context injection', () => { fs.entries.set(join(root, 'pkg/file.txt'), { type: 'file', content: 'hello' }) await ctx.plugin(ToolFs) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const first = await ctx.tools.execute({ signal: testToolSignal, @@ -3534,7 +3555,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/deep/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const first = await ctx.tools.execute({ signal: testToolSignal, callId: ToolCallId('read-before-resume'), @@ -3543,7 +3564,7 @@ describe('dynamic nested workspace context injection', () => { agent, }) await appendAdditionalContexts(ctx, agent) - const resumed = stubAgent(root, agent.session.snapshotEvents()) + const resumed = await stubAgent(root, agent.session.snapshotEvents()) const afterResume = await ctx.tools.execute({ signal: testToolSignal, @@ -3570,14 +3591,14 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const original = stubAgent(root) + const original = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, callId: ToolCallId('read-before-offline-change'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent: original, }) await appendAdditionalContexts(ctx, original) await write(join(root, 'pkg/AGENTS.md'), 'new nested rule after resume') - const resumed = stubAgent(root, original.session.snapshotEvents()) + const resumed = await stubAgent(root, original.session.snapshotEvents()) await composeBaselinePrefix(ctx, resumed) @@ -3601,7 +3622,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/deep/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const first = await ctx.tools.execute({ signal: testToolSignal, callId: ToolCallId('read-before-compact'), @@ -3653,7 +3674,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await composeBaselinePrefix(ctx, agent) const baseline = baselineEvents(agent)[0] expect(baseline).toBeDefined() @@ -3713,7 +3734,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/sub/file.txt'), 'subtree file') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, callId: ToolCallId('read-package'), @@ -3751,7 +3772,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/sub/file.txt'), 'subtree file') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 700 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, callId: ToolCallId('read-subtree-omitting-parent'), @@ -3788,7 +3809,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/deep/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) agent.session.append('user/message', createUserMessage({ content: [ { type: 'reasoning', text: 'Additional instructions from: pkg/AGENTS.md' }, @@ -3839,7 +3860,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/deep/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const rootResult = await ctx.tools.execute({ signal: testToolSignal, @@ -3882,7 +3903,7 @@ describe('dynamic nested workspace context injection', () => { fs.throwOnRead.add(nested) await ctx.plugin(ToolFs) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const result = await ctx.tools.execute({ signal: testToolSignal, @@ -3914,7 +3935,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/deep/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) ctx.on('tools/post-execute', async () => ({ kind: 'accept' as const, value: { @@ -3976,7 +3997,7 @@ describe('dynamic nested workspace context injection', () => { await write(join(root, 'pkg/deep/file.txt'), 'hello') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) ctx.on('tools/post-execute', async () => ({ kind: 'block' as const, feedback: [{ type: 'text' as const, text: 'blocked downstream' }], @@ -4023,7 +4044,7 @@ describe('dynamic nested workspace context injection', () => { : downstream }) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const blocked = await ctx.tools.execute({ signal: testToolSignal, @@ -4092,7 +4113,7 @@ describe('dynamic nested workspace context injection', () => { : downstream }) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const blocked = await ctx.tools.execute({ signal: testToolSignal, @@ -4119,7 +4140,7 @@ describe('dynamic nested workspace context injection', () => { const fs = ctx.fs as RecordingFileSystem fs.entries.set(join(root, '.git'), { type: 'directory' }) fs.entries.set(join(root, 'pkg/AGENTS.md'), { type: 'file', content: 'nested package rule' }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const turnStart = agent.session.append('turn/start', { turn: 1 }) ctx.emit('session/event', agent.session, turnStart) const stepStart = agent.session.append('step/start', { turn: 1, step: 1 }) @@ -4185,7 +4206,7 @@ describe('dynamic nested workspace context injection', () => { const fs = ctx.fs as RecordingFileSystem fs.entries.set(join(root, '.git'), { type: 'directory' }) fs.entries.set(join(root, 'pkg/AGENTS.md'), { type: 'file', content: 'nested package rule' }) - const agent = stubAgent(root) + const agent = await stubAgent(root) agent.session.append('turn/start', { turn: 1 }) agent.session.append('step/start', { turn: 1, step: 1 }) agent.session.append('step/end', { turn: 1, step: 1 }) @@ -4215,7 +4236,7 @@ describe('dynamic nested workspace context injection', () => { await ctx.plugin(RecordingFileSystem) await mountWorkspaceContextPlugin(ctx, { maxBytes: 65536 }) const fs = ctx.fs as RecordingFileSystem - const agent = stubAgent('/') + const agent = await stubAgent('/') const plainResult = { callId: ToolCallId('plain'), content: [], isError: false as const, value: null } const aborted = new AbortController() aborted.abort(new Error('cancelled')) @@ -4265,7 +4286,7 @@ describe('dynamic nested workspace context injection', () => { await mountWorkspaceContextPlugin(ctx, { maxBytes: 65536 }) const fs = ctx.fs as RecordingFileSystem const root = resolve('/') - const agent = stubAgent(root) + const agent = await stubAgent(root) const failure = new Error('projection failed') const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => undefined) fs.entries.set(join(root, '.git'), { type: 'directory' }) @@ -4303,7 +4324,7 @@ describe('dynamic nested workspace context injection', () => { callId: ToolCallId('read-with-disabled-budget'), name: 'read', arguments: { file_path: join('pkg', 'deep', 'file.txt') }, - agent: stubAgent(root), + agent: await stubAgent(root), }) expect(result.isError).toBe(false) @@ -4329,7 +4350,7 @@ describe('dynamic nested workspace context injection', () => { fs.entries.set(join(root, 'pkg/file.txt'), { type: 'file', content: 'hello' }) await ctx.plugin(ToolFs) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 20 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const first = await ctx.tools.execute({ signal: testToolSignal, @@ -4369,7 +4390,7 @@ describe('dynamic nested workspace context injection', () => { callId: ToolCallId('read-missing'), name: 'read', arguments: { file_path: join('pkg', 'missing.txt') }, - agent: stubAgent(root), + agent: await stubAgent(root), }) expect(result.isError).toBe(true) @@ -4390,7 +4411,7 @@ describe('dynamic nested workspace context injection', () => { const ctx = new Context() const fiber = await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) await fiber.dispose() - const agent = stubAgent(root) + const agent = await stubAgent(root) const result = await ctx.tools.execute({ signal: testToolSignal, @@ -4426,7 +4447,7 @@ describe('workspace context inbox synchronization', () => { await write(join(root, 'AGENTS.md'), 'duplicate baseline') const ctx = new Context() await mountWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await syncWorkspaceContext(ctx, agent) const desired = agent.inbox.nextStep[0]! agent.inbox.append('next-step', createUserMessage({ content: desired.content, source: desired.source })) @@ -4451,7 +4472,7 @@ describe('workspace context inbox synchronization', () => { fs.entries.set(join(root, '.git'), { type: 'directory' }) fs.entries.set(join(root, 'pkg/AGENTS.md'), { type: 'file', content: 'tiny-budget rule' }) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 1 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) ctx.emit('tools/result', stubToolExecution({ signal: testToolSignal, callId: ToolCallId('tiny-budget-touch'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent, @@ -4479,7 +4500,7 @@ describe('workspace context inbox synchronization', () => { await write(join(root, 'pkg/file.txt'), 'file') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, callId: ToolCallId('pending-v1'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent, @@ -4527,7 +4548,7 @@ describe('workspace context inbox synchronization', () => { fs.entries.set(join(root, 'a/AGENTS.md'), { type: 'file', content: 'restored A' }) fs.entries.set(join(root, 'b/AGENTS.md'), { type: 'file', content: 'restored B' }) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const first = stubToolExecution({ signal: testToolSignal, callId: ToolCallId('projected-before-abort'), name: 'read', arguments: { file_path: join('a', 'file.txt') }, agent, @@ -4567,7 +4588,7 @@ describe('workspace context inbox synchronization', () => { fs.entries.set(join(root, 'a/AGENTS.md'), { type: 'file', content: 'scope A' }) fs.entries.set(join(root, 'b/AGENTS.md'), { type: 'file', content: 'scope B' }) await mountWorkspaceContextPlugin(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(root) + const agent = await stubAgent(root) const first = stubToolExecution({ signal: testToolSignal, callId: ToolCallId('concurrent-a'), name: 'read', arguments: { file_path: join('a', 'file.txt') }, agent, @@ -4605,13 +4626,13 @@ describe('workspace context inbox synchronization', () => { await write(join(root, 'b/file.txt'), 'b') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const original = stubAgent(root) + const original = await stubAgent(root) await ctx.tools.execute({ signal: testToolSignal, callId: ToolCallId('recover-pending-a'), name: 'read', arguments: { file_path: join('a', 'file.txt') }, agent: original, }) await syncWorkspaceContext(ctx, original) - const resumed = stubAgent(root, original.session.snapshotEvents()) + const resumed = await stubAgent(root, original.session.snapshotEvents()) await ctx.tools.execute({ signal: testToolSignal, @@ -4640,9 +4661,9 @@ describe('workspace context inbox synchronization', () => { await write(join(root, 'pkg/file.txt'), 'file') const ctx = new Context() await mountFileToolsAndWorkspaceContext(ctx, { dshHome: home, maxBytes: 65536 }) - const agent = stubAgent(join(root, 'pkg')) + const agent = await stubAgent(join(root, 'pkg')) await syncedWorkspaceContext(ctx, agent) - const claimed = agent.inbox.claim('next-step', 1) + const claimed = claimInbox(agent, 'next-step') await write(join(root, 'pkg/AGENTS.md'), 'new claimed rule with more detail') const downstream = { kind: 'enter' as const, messages: claimed } diff --git a/packages/context/time-context/tests/time-context.spec.ts b/packages/context/time-context/tests/time-context.spec.ts index 63d1bcb2b0..5d47095e5b 100644 --- a/packages/context/time-context/tests/time-context.spec.ts +++ b/packages/context/time-context/tests/time-context.spec.ts @@ -4,11 +4,11 @@ import Loader from '@deepseek-ai/cordis-plugin-loader' import { createUserMessage, ToolCallId, LlmAdapter } from '@deepseek-ai/dsh-llm' import type { GenerateOptions, StreamChunk } from '@deepseek-ai/dsh-llm' import { Session, SessionId, type SessionEvent } from '@deepseek-ai/dsh-session' -import AgentRegistry, { agentEvents, Inbox, type Agent } from '@deepseek-ai/dsh-agent' +import AgentRegistry, { agentEvents, type Agent } from '@deepseek-ai/dsh-agent' import { defineContentToolFixture } from '@deepseek-ai/dsh-tools' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' -import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' +import { unsupportedInbox, mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import * as timeContext from '@deepseek-ai/dsh-time-context' import type { Config } from '@deepseek-ai/dsh-time-context' @@ -38,11 +38,11 @@ async function mount(config: Config = {}) { } function sessionAgent(session: Session, id = 'agent'): Agent { - return { + const agent: Agent = { id: SessionId(id), options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: unsupportedInbox(), status: 'running', ctx: new Context(), send: () => {}, @@ -53,6 +53,7 @@ function sessionAgent(session: Session, id = 'agent'): Agent { runMaintenance: task => task(new AbortController().signal), whenIdle: () => Promise.resolve(), } + return agent } function openMessageTurn(session: Session, turn: number, clientTimeZone?: string): void { @@ -139,7 +140,6 @@ class ScriptedAdapter extends LlmAdapter { async function loopHarness(adapter: ScriptedAdapter, config: Config = {}): Promise { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(timeContext, config) ctx.llm.registerAdapter(['mock'], adapter) diff --git a/packages/context/tmux-context/package.json b/packages/context/tmux-context/package.json index c8ba14da68..43d6c7e956 100644 --- a/packages/context/tmux-context/package.json +++ b/packages/context/tmux-context/package.json @@ -39,6 +39,7 @@ }, "devDependencies": { "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-shell": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", diff --git a/packages/context/tmux-context/tests/tmux-context.spec.ts b/packages/context/tmux-context/tests/tmux-context.spec.ts index cc1c4985d3..bc9f9f0ed5 100644 --- a/packages/context/tmux-context/tests/tmux-context.spec.ts +++ b/packages/context/tmux-context/tests/tmux-context.spec.ts @@ -1,13 +1,14 @@ import { afterEach, describe, expect, it, vi } from 'vitest' import { Context } from '@deepseek-ai/cordis' import { Session, SessionId } from '@deepseek-ai/dsh-session' -import AgentRegistry, { agentEvents, Inbox, type Agent } from '@deepseek-ai/dsh-agent' +import AgentRegistry, { agentEvents, type Agent } from '@deepseek-ai/dsh-agent' import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { createUserMessage } from '@deepseek-ai/dsh-llm' import { ShellExecutor } from '@deepseek-ai/dsh-shell' import type { ShellExecRequest, ShellExecSpec, ShellProcess, ShellRunResult } from '@deepseek-ai/dsh-shell' import * as tmuxContext from '@deepseek-ai/dsh-tmux-context' import type { Config } from '@deepseek-ai/dsh-tmux-context' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' const SIGNAL = new AbortController().signal @@ -94,11 +95,11 @@ async function mount( } function sessionAgent(session: Session, id = 'agent'): Agent { - return { + const agent: Agent = { id: SessionId(id), options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: unsupportedInbox(), status: 'running', ctx: new Context(), send: () => {}, @@ -109,6 +110,7 @@ function sessionAgent(session: Session, id = 'agent'): Agent { runMaintenance: task => task(new AbortController().signal), whenIdle: () => Promise.resolve(), } + return agent } function openMessageTurn(session: Session, turn: number): void { diff --git a/packages/core/agent-loop/README.i18n.yaml b/packages/core/agent-loop/README.i18n.yaml index b1cc6893b2..6cda32c516 100644 --- a/packages/core/agent-loop/README.i18n.yaml +++ b/packages/core/agent-loop/README.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 packages/core/agent-loop/README.md -README.md: 55966a4b0eed0c1cc3484314e809de79341072de -README.zh.md: 1d27bf3743f54d8fe66a58e75565fc85d94ae1ed +README.md: b24151c8fb512b144b15c017b662b0b2a57532fc +README.zh.md: d4c3b511426ce06c955d36d3f0f3852fe459ce0b diff --git a/packages/core/agent-loop/README.md b/packages/core/agent-loop/README.md index 55966a4b0e..b24151c8fb 100644 --- a/packages/core/agent-loop/README.md +++ b/packages/core/agent-loop/README.md @@ -68,6 +68,8 @@ const handle = await ctx.agents.create({ }) ``` +Every inbox mutation commits one normalized `agent/inbox/spliced` event. The projection registry folds that event synchronously, so the live projection reflects the splice when `Session.append()` returns. Insertions, edits, removals, claiming, and cancellation replay through the same standard splice coordinates. Ordinary removals carry `outcome: 'canceled'` and emit `agent/inbox/discarded { message }`; claiming uses pure deletions with no outcome and emits `agent/inbox/claimed`. Every insertion emits `agent/inbox/inserted { message }`. `MessageId` stays unique across both pending lists. Consumers that need a removed message use the claimed or discarded notification instead of depending on a pre-splice `session/event` view. + ### What a step does Each step sends the agent's rendered system prompt, its visible tool schemas, and the session's derived history; the model's tool calls run through the guarded tool pipeline and every accepted fact is appended to the session log before the next step derives from it. Parallel-safe calls may overlap up to `maxParallelToolCalls`; exclusive calls run alone as ordering barriers. Cancellation is cooperative: `agent.cancel()` aborts the current activity and, unless `keepInbox` is set, clears pending work; a cancelled stream finalizes the text already delivered to the user. @@ -98,6 +100,7 @@ The loop deep-freezes each derived message identity on its first request and reu |---|---| | [`src/index.ts`](src/index.ts) | Plugin entry: `AgentLoop` service, config schema, declarative agent startup, factory registration | | [`src/agent.ts`](src/agent.ts) | The concrete `ReactLoopAgent` driver: inbox, turn/step machine, cancellation | +| [`src/inbox.ts`](src/inbox.ts) | Package-internal `ReactLoopInbox`: durable projection, structural commands, and loop-only claim state | | [`src/tool-calls.ts`](src/tool-calls.ts) | Tool scheduling: exclusive barriers and the bounded parallel pool | | [`src/runtime-context.ts`](src/runtime-context.ts) | Per-step runtime-context snapshot handling | | [`src/constants.ts`](src/constants.ts) | `DEFAULT_MAX_PARALLEL_TOOL_CALLS` | @@ -113,7 +116,7 @@ The loop is the production acquisition point for session write handles. When `ct ### Turn and step flow -The driver owns one agent for its lifetime and runs inside `ctx.agents.withInitiator(agent, ...)`. At a turn boundary it opens the durable turn, then atomically claims pending next-step input plus one queued prompt; between steps it claims only next-step input. `agent/pre-step` decides what enters the step. An entered decision appends its complete `user/message` batch before the driver can claim again, while a rejected decision appends none. Each model attempt emits one process-local `start`, emits every `chunk` only after the matching durable `assistant/chunk`, and emits exactly one terminal `end`; final assembly or message-append failure settles it as `aborted`, while `committed` follows the durable `assistant/message`. Each successful model call appends one message anchor citing its chunk seqs, and a cancelled stream appends an `interrupted: true` anchor with the delivered prefix so the next request contains what the user saw. Within a step, exclusive calls form barriers and parallel-safe calls use the bounded rolling pool; policy, durable results, and result context remain model-ordered. +The driver owns one agent for its lifetime and runs inside `ctx.agents.withInitiator(agent, ...)`. Its package-internal `ReactLoopInbox` constructor registers the standard `inbox` projection on the agent scope, then uses that projection for structural commands and loop-only claims. Registry reference counting keeps the shared key active until the last agent scope unloads. At a turn boundary the driver opens the durable turn, then atomically claims pending next-step input plus one queued prompt; between steps it claims only next-step input. `agent/pre-step` decides what enters the step. An entered decision appends its complete `user/message` batch before the driver can claim again, while a rejected decision appends none. Each model attempt emits one process-local `start`, emits every `chunk` only after the matching durable `assistant/chunk`, and emits exactly one terminal `end`; final assembly or message-append failure settles it as `aborted`, while `committed` follows the durable `assistant/message`. Each successful model call appends one message anchor citing its chunk seqs, and a cancelled stream appends an `interrupted: true` anchor with the delivered prefix so the next request contains what the user saw. Within a step, exclusive calls form barriers and parallel-safe calls use the bounded rolling pool; policy, durable results, and result context remain model-ordered. ### Failure and cancellation diff --git a/packages/core/agent-loop/README.zh.md b/packages/core/agent-loop/README.zh.md index 1d27bf3743..d4c3b51142 100644 --- a/packages/core/agent-loop/README.zh.md +++ b/packages/core/agent-loop/README.zh.md @@ -68,6 +68,8 @@ const handle = await ctx.agents.create({ }) ``` +每次 inbox 变更都会提交一条规范化的 `agent/inbox/spliced` 事件。投影注册表会同步折叠该事件,因此 `Session.append()` 返回时,实时投影已经反映该 splice。插入、编辑、移除、领取与取消都通过同一组标准 splice 坐标回放。普通删除携带 `outcome: 'canceled'` 并发出 `agent/inbox/discarded { message }`;领取使用不带 outcome 的纯删除,并发出 `agent/inbox/claimed`。每次插入都会发出 `agent/inbox/inserted { message }`。`MessageId` 在两个待处理列表之间保持唯一。需要被移除消息的消费方应使用 claimed 或 discarded 通知,而不依赖 splice 前的 `session/event` 投影视图。 + ### 一个步骤做什么 每个步骤都会发送该 agent 渲染后的系统提示词、其可见工具 schema 与会话的派生历史;模型的工具调用经过受守卫的工具流水线,每个被接纳的事实都会在下一步据此派生之前追加到会话日志。并行安全调用最多可重叠 `maxParallelToolCalls` 个;独占调用单独运行并构成排序屏障。取消是协作式的:`agent.cancel()` 中止当前活动,并在未设置 `keepInbox` 时清除待处理工作;被取消的流会终结已送达用户的文本。 @@ -98,6 +100,7 @@ const handle = await ctx.agents.create({ |---|---| | [`src/index.ts`](src/index.ts) | 插件入口:`AgentLoop` 服务、配置 schema、声明式 agent 启动、工厂注册 | | [`src/agent.ts`](src/agent.ts) | 具体 `ReactLoopAgent` 驱动器:收件箱、轮次/步骤状态机、取消 | +| [`src/inbox.ts`](src/inbox.ts) | 包内部的 `ReactLoopInbox`:持久投影、结构化命令与仅供循环使用的领取状态 | | [`src/tool-calls.ts`](src/tool-calls.ts) | 工具调度:独占屏障与有界并行池 | | [`src/runtime-context.ts`](src/runtime-context.ts) | 每步骤 runtime-context 快照处理 | | [`src/constants.ts`](src/constants.ts) | `DEFAULT_MAX_PARALLEL_TOOL_CALLS` | @@ -113,7 +116,7 @@ const handle = await ctx.agents.create({ ### 轮次与步骤流程 -驱动器在其整个生命周期内拥有一个 agent,并在 `ctx.agents.withInitiator(agent, ...)` 内运行。在轮次边界,它先打开持久轮次,再原子领取待处理的 next-step 输入与一条排队提示词;在步骤之间则只领取 next-step 输入。`agent/pre-step` 决定什么进入该步骤。进入步骤的决定会在驱动器再次领取消息前追加完整的 `user/message` 批次,被拒绝的决定则不追加任何消息。每次模型尝试会发出一个进程本地 `start`,仅在匹配的持久 `assistant/chunk` 之后发出各个 `chunk`,并恰好发出一个终态 `end`;最终组装或消息追加失败时以 `aborted` 结算,`committed` 则出现在持久 `assistant/message` 之后。每次成功的模型调用都恰好追加一个引用其分片 seq 的 message 锚点,被取消的流则追加带 `interrupted: true` 的锚点并携带已交付前缀,使下一次请求包含用户看到的内容。在步骤内,独占调用形成屏障,并行安全调用使用有界滚动池;策略、持久结果与结果上下文保持模型顺序。 +驱动器在其整个生命周期内拥有一个 agent,并在 `ctx.agents.withInitiator(agent, ...)` 内运行。其包内部 `ReactLoopInbox` 构造函数在 agent 作用域上注册标准 `inbox` 投影,随后将该投影用于结构化命令与仅供 loop 使用的领取操作。注册表引用计数会使共享 key 持续有效,直至最后一个 agent 作用域卸载。在轮次边界,驱动器先打开持久轮次,再原子领取待处理的 next-step 输入与一条排队提示词;在步骤之间则只领取 next-step 输入。`agent/pre-step` 决定什么进入该步骤。进入步骤的决定会在驱动器再次领取消息前追加完整的 `user/message` 批次,被拒绝的决定则不追加任何消息。每次模型尝试会发出一个进程本地 `start`,仅在匹配的持久 `assistant/chunk` 之后发出各个 `chunk`,并恰好发出一个终态 `end`;最终组装或消息追加失败时以 `aborted` 结算,`committed` 则出现在持久 `assistant/message` 之后。每次成功的模型调用都恰好追加一个引用其分片 seq 的 message 锚点,被取消的流则追加带 `interrupted: true` 的锚点并携带已交付前缀,使下一次请求包含用户看到的内容。在步骤内,独占调用形成屏障,并行安全调用使用有界滚动池;策略、持久结果与结果上下文保持模型顺序。 ### 失败与取消 diff --git a/packages/core/agent-loop/src/agent.ts b/packages/core/agent-loop/src/agent.ts index 634a1a52de..0bb02706ed 100644 --- a/packages/core/agent-loop/src/agent.ts +++ b/packages/core/agent-loop/src/agent.ts @@ -15,7 +15,7 @@ import type { PreStepDecision, RequestErrorAction, } from '@deepseek-ai/dsh-agent' -import { Inbox, agentEvents, assembleContextFor } from '@deepseek-ai/dsh-agent' +import { agentEvents, assembleContextFor } from '@deepseek-ai/dsh-agent' import type { GenerateOptions, LlmCallConfig, Message, PreparedLlmCall } from '@deepseek-ai/dsh-llm' import { LlmError, @@ -32,6 +32,7 @@ import { joinContextSections, renderContextSections, renderPrompt } from '@deeps import type { PromptAssembly } from '@deepseek-ai/dsh-system-prompt' import type {} from '@deepseek-ai/dsh-session-projection' import type { Context } from '@deepseek-ai/cordis' +import { ReactLoopInbox } from './inbox.ts' import { RuntimeContextProjection } from './runtime-context.ts' import { AssistantStreamAttempt } from './assistant-stream.ts' import { executeToolCalls } from './tool-calls.ts' @@ -68,7 +69,7 @@ function requestProposal(header: EpochHeader): LlmCallConfig { /** Drives one session through turn and step boundaries. */ export class ReactLoopAgent implements Agent { - readonly inbox: Inbox + readonly inbox: ReactLoopInbox private phase: Phase private activityDone: Promise = Promise.resolve() @@ -97,16 +98,12 @@ export class ReactLoopAgent implements Agent { public readonly session: Session, ) { this.dispatch = agentEvents(loopCtx, this) - this.inbox = new Inbox(session, { - inserted: (message) => { this.dispatch.emit('agent/inbox/inserted', { message }) }, - discarded: (message) => { this.dispatch.emit('agent/inbox/discarded', { message }) }, - claimed: (message, turn) => { this.dispatch.emit('agent/inbox/claimed', { message, turn }) }, - }) + this.scope = createScope(loopCtx, this) + this.ctx = this.scope.ctx.extend({ agent: this }) + this.inbox = new ReactLoopInbox(this.ctx.sessionProjections, session, this.dispatch) /* v8 ignore next -- the loop registers its own turnBoundary unit, so the key is always present */ const lastTurn = this.loopCtx.sessionProjections.stateOf(session, 'turnBoundary')?.lastTurn ?? 0 this.phase = { kind: 'idle', lastTurn } - this.scope = createScope(loopCtx, this) - this.ctx = this.scope.ctx.extend({ agent: this }) this.runtimeContext = new RuntimeContextProjection(this.ctx, session) } diff --git a/packages/core/agent-loop/src/inbox.ts b/packages/core/agent-loop/src/inbox.ts new file mode 100644 index 0000000000..db89cd3072 --- /dev/null +++ b/packages/core/agent-loop/src/inbox.ts @@ -0,0 +1,247 @@ +/** + * Driver-owned durable agent inbox projection and command facade. + * + * @module @deepseek-ai/dsh-agent-loop/inbox + */ + +import type { MessageId } from '@deepseek-ai/dsh-llm' +import type { ProjectionDefinition } from '@deepseek-ai/dsh-session-projection' +import type SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' +import type { Session, SessionEventMap, UserMessage } from '@deepseek-ai/dsh-session' +import type { + AgentEventDispatch, + Inbox as InboxContract, + InboxState, + InboxTarget, + InboxWireState, +} from '@deepseek-ai/dsh-agent' +import { z } from 'zod' + +/** Wire validation for pending agent input reconstructed from durable inbox splices. */ +export const inboxProjectionSchema = z.object({ + 'next-turn': z.array(z.custom()).readonly(), + 'next-step': z.array(z.custom()).readonly(), +}).readonly() + +/** Standard fold that reconstructs pending input and rejects invalid durable splice history. */ +export const inboxProjectionDefinition = { + key: 'inbox', + stateSchema: inboxProjectionSchema, + init: (): InboxState => ({ 'next-turn': [], 'next-step': [] }), + apply(state: InboxState, event) { + if (event.type !== 'agent/inbox/spliced') return state + const splice = event.data + try { + const inbox = state[splice.target] + const removedCount = splice.removedCount ?? 0 + if (!Number.isSafeInteger(splice.start) || splice.start < 0 || splice.start > inbox.length + || !Number.isSafeInteger(removedCount) || removedCount < 0 + || splice.start + removedCount > inbox.length) { + throw new Error('invalid inbox splice') + } + const next = inbox.toSpliced(splice.start, removedCount, ...splice.inserted) + const ids = new Set() + for (const message of splice.target === 'next-turn' + ? [...next, ...state['next-step']] + : [...state['next-turn'], ...next]) { + if (ids.has(message.id)) throw new Error(`message "${message.id}" is already pending`) + ids.add(message.id) + } + return splice.target === 'next-turn' + ? { 'next-turn': next, 'next-step': state['next-step'] } + : { 'next-turn': state['next-turn'], 'next-step': next } + } catch (error: unknown) { + throw new Error(`invalid persisted inbox splice at session seq ${event.seq}`, { cause: error }) + } + }, + wire: { + // The wire value is the fold state itself: every pending message already + // round-trips the session log as lossless JSON. Only the static type + // narrows to the JSON-safe projection table entry. + viewSchema: inboxProjectionSchema as unknown as z.ZodType, + view: (state: InboxState) => state as unknown as InboxWireState, + }, + stateVersion: 1, +} satisfies ProjectionDefinition<'inbox', InboxState> + +/** + * Driver-owned durable Inbox implementation used by ReactLoopAgent and focused + * provider tests. + * @param projections - registry that owns the standard Inbox projection. + * @param session - session whose durable events store pending input. + * @param dispatch - agent-scoped notifications for Inbox lifecycle events. + */ +export class ReactLoopInbox implements InboxContract { + constructor( + private readonly projections: SessionProjectionRegistry, + private readonly session: Session, + private readonly dispatch: AgentEventDispatch, + ) { + this.projections.register(inboxProjectionDefinition) + } + + /** Prompts awaiting individual turns. */ + get nextTurn(): readonly UserMessage[] { + return this.current()['next-turn'] + } + + /** Input awaiting the next step boundary. */ + get nextStep(): readonly UserMessage[] { + return this.current()['next-step'] + } + + /** Whether either pending-message list contains work. */ + get hasPending(): boolean { + const state = this.current() + return state['next-turn'].length > 0 || state['next-step'].length > 0 + } + + /** Durably cancel all pending input, clearing next-step before next-turn. */ + clear(): void { + this.splice('next-step', 0, this.nextStep.length, []) + this.splice('next-turn', 0, this.nextTurn.length, []) + } + + /** + * Remove and return the complete batch proposed for one step. + * @param target - whether this boundary also consumes one queued turn. + * @param turn - turn that will own the claimed batch. + * @returns next-step input followed by the queued turn, when requested. + */ + claim(target: InboxTarget, turn: number): UserMessage[] { + const claimed = this.mutate('next-step', 0, this.nextStep.length, [], false) + if (target === 'next-turn') claimed.push(...this.mutate('next-turn', 0, 1, [], false)) + for (const message of claimed) this.dispatch.emit('agent/inbox/claimed', { message, turn }) + return claimed + } + + /** + * Append one message to a pending list. + * @param target - pending list to extend. + * @param message - message to append. + */ + append(target: InboxTarget, message: UserMessage): void { + this.splice(target, this.current()[target].length, 0, [message]) + } + + /** + * Prepend one message to a pending list. + * @param target - pending list to extend. + * @param message - message to prepend. + */ + prepend(target: InboxTarget, message: UserMessage): void { + this.splice(target, 0, 0, [message]) + } + + /** + * Replace one pending message in place. + * @param messageId - identity of the pending message to replace. + * @param newMessage - replacement message. + * @returns whether the message was still pending. + */ + replace(messageId: MessageId, newMessage: UserMessage): boolean { + const location = this.locate(messageId) + if (location === undefined) return false + this.splice(location.target, location.index, 1, [newMessage]) + return true + } + + /** + * Remove one pending message. + * @param messageId - identity of the pending message to remove. + * @returns whether the message was still pending. + */ + remove(messageId: MessageId): boolean { + const location = this.locate(messageId) + if (location === undefined) return false + this.splice(location.target, location.index, 1, []) + return true + } + + /** + * Apply standard splice semantics and durably record the normalized result. + * @param target - pending list to mutate. + * @param start - splice position. + * @param deleteCount - maximum number of messages to remove. + * @param inserted - messages to insert at the resolved position. + * @returns messages removed by the splice. + */ + splice( + target: InboxTarget, + start: number, + deleteCount: number, + inserted: UserMessage[], + ): UserMessage[] { + return this.mutate(target, start, deleteCount, inserted, true) + } + + /** Locate one pending identity across both owned lists. */ + private locate(messageId: MessageId): { target: InboxTarget; index: number } | undefined { + const state = this.current() + for (const target of ['next-turn', 'next-step'] as const) { + const index = state[target].findIndex(message => message.id === messageId) + if (index >= 0) return { target, index } + } + return undefined + } + + /** Read the current durable projection state. */ + private current(): InboxState { + const state = this.projections.stateOf(this.session, 'inbox') + /* v8 ignore next -- the constructor registers this key before any read */ + if (state === undefined) { + throw new Error( + `agent "${this.session.id}" cannot read inbox state: its projection registration is not active`, + ) + } + return state + } + + /** Commit one normalized mutation and publish its live events. */ + private mutate( + target: InboxTarget, + start: number, + deleteCount: number, + inserted: UserMessage[], + discardRemoved: boolean, + ): UserMessage[] { + const state = this.current() + const inbox = state[target] + const truncatedStart = Math.trunc(start) + const offset = Number.isNaN(truncatedStart) ? 0 : truncatedStart + const actualStart = offset < 0 + ? Math.max(inbox.length + offset, 0) + : Math.min(offset, inbox.length) + const truncatedDeleteCount = Math.trunc(deleteCount) + const actualDeleteCount = Math.min( + Math.max(Number.isNaN(truncatedDeleteCount) ? 0 : truncatedDeleteCount, 0), + inbox.length - actualStart, + ) + if (actualDeleteCount === 0 && inserted.length === 0) return [] + const candidate = inbox.toSpliced(actualStart, actualDeleteCount, ...inserted) + const ids = new Set() + for (const message of target === 'next-turn' + ? [...candidate, ...state['next-step']] + : [...state['next-turn'], ...candidate]) { + if (ids.has(message.id)) throw new Error(`message "${message.id}" is already pending`) + ids.add(message.id) + } + const outcome = discardRemoved && actualDeleteCount > 0 ? 'canceled' as const : undefined + const splice: SessionEventMap['agent/inbox/spliced'] = { + target, + start: actualStart, + ...(actualDeleteCount === 0 ? {} : { removedCount: actualDeleteCount }), + inserted, + ...(outcome === undefined ? {} : { outcome }), + } + const removed = inbox.slice(actualStart, actualStart + actualDeleteCount) + const event = this.session.append('agent/inbox/spliced', splice) + if (discardRemoved) { + for (const message of removed) this.dispatch.emit('agent/inbox/discarded', { message }) + } + for (const message of event.data.inserted) { + this.dispatch.emit('agent/inbox/inserted', { message }) + } + return removed + } +} diff --git a/packages/core/agent-loop/src/index.ts b/packages/core/agent-loop/src/index.ts index 073ecae7b7..db25ec4e64 100644 --- a/packages/core/agent-loop/src/index.ts +++ b/packages/core/agent-loop/src/index.ts @@ -584,7 +584,9 @@ export class AgentLoop extends Service implements AgentFactory { // Disposal IS a disposed-cause cancel followed by quiescence. New work // sent after this point is the sender's bug — the registries are about // to drop the agent, so nothing should still hold it. + /* v8 ignore next -- Cordis effect teardown waits for synchronous setup before observing the machine slot. */ if (machine === undefined) await machineReady.promise + /* v8 ignore next -- setup failure untracks this disposer before resolving without a machine. */ if (machine !== undefined) { machine.cancel({ kind: 'disposed' }) await machine.whenIdle() @@ -617,15 +619,21 @@ export class AgentLoop extends Service implements AgentFactory { const untrack = this.ownership.track(dispose) let unfollowOwner: () => Promise | void try { - unfollowOwner = ownerCtx.effect(() => () => { - // Owner disposal owns the same quiescence boundary. Its teardown skips - // unregistering this already-running owner effect from inside itself. - if (disposing !== undefined) return - abort.abort(new Error(`agent "${id}" setup aborted: owner disposed during setup`)) - return dispose(true) + unfollowOwner = ownerCtx.effect(function* () { + machine = new ReactLoopAgent(loopCtx, id, options, session) + machineReady.resolve() + yield machine.scope.rawDispose + yield () => { + // Owner disposal owns the same quiescence boundary. Its teardown skips + // unregistering this already-running owner effect from inside itself. + if (disposing !== undefined) return + abort.abort(new Error(`agent "${id}" setup aborted: owner disposed during setup`)) + return dispose(true) + } }, `agentLoop.lifecycle(${id})`) /* v8 ignore start -- ctx.effect throws only on an inactive fiber, which assertActive() above already rejected */ } catch (error: unknown) { + machineReady.resolve() untrack() callerSignal?.removeEventListener('abort', onCallerAbort) this.ownership.signal.removeEventListener('abort', onFactoryTeardown) @@ -642,8 +650,9 @@ export class AgentLoop extends Service implements AgentFactory { throw abort.signal.reason instanceof Error ? abort.signal.reason : new Error(String(abort.signal.reason)) } try { - const agent = machine = new ReactLoopAgent(loopCtx, id, options, session) - machineReady.resolve() + /* v8 ignore next -- a synchronous effect exhausts the generator before returning */ + if (machine === undefined) throw new Error(`agent "${id}" lifecycle did not construct its driver`) + const agent = machine assertLive() return { diff --git a/packages/core/agent-loop/tests/contract-regressions.spec.ts b/packages/core/agent-loop/tests/contract-regressions.spec.ts index cb0ee6a933..68cb93f129 100644 --- a/packages/core/agent-loop/tests/contract-regressions.spec.ts +++ b/packages/core/agent-loop/tests/contract-regressions.spec.ts @@ -7,7 +7,6 @@ import ToolRuntime, { defineContentToolFixture, type PostToolDecision } from '@d import AgentRegistry, { type Agent } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' -import { ReactLoopAgent } from '../src/agent.ts' import InvariantRegistry from '@deepseek-ai/dsh-invariants' import * as SessionInvariant from '@deepseek-ai/dsh-session/invariant' import * as AgentInvariant from '@deepseek-ai/dsh-agent/invariant' @@ -249,7 +248,11 @@ describe('abort during tool execution ends the turn', () => { ? [event.data.content] : [])) .toEqual([]) - expect(agent.inbox.nextStep.map(inboxText)) + expect(agent.session.snapshotEvents() + .flatMap(event => event.type === 'agent/inbox/spliced' && event.data.target === 'next-step' + ? [event.data.inserted.map(inboxText)] + : []) + .at(-1)) .toEqual(['accepted result context during disposal']) expect(agent.session.snapshotEvents().filter(event => event.type === 'turn/start')) .toHaveLength(1) @@ -583,10 +586,11 @@ describe('turn numbering continues across seeded sessions', () => { await ctx2.plugin(AgentLoop, { agents: [] }) ctx2.llm.registerAdapter(['mock'], second) - const seeded = ctx2.sessions.create(SessionId('forked'), { seed: agent.session.snapshotEvents() }) - const forked = new ReactLoopAgent( - ctx2, SessionId('forked-agent'), { provider: 'mock', model: 'mock' }, seeded, - ) + const { agent: forked } = await ctx2.agents.create({ + sessionId: SessionId('forked'), + seed: agent.session.snapshotEvents(), + agentOptions: { provider: 'mock', model: 'mock' }, + }) const turns: number[] = [] ctx2.on('session/event', (_s, event) => { if (event.type === 'turn/start') turns.push(event.data.turn) }) diff --git a/packages/core/agent-loop/tests/inbox.spec.ts b/packages/core/agent-loop/tests/inbox.spec.ts new file mode 100644 index 0000000000..028c42ca5c --- /dev/null +++ b/packages/core/agent-loop/tests/inbox.spec.ts @@ -0,0 +1,266 @@ +import { Context } from '@deepseek-ai/cordis' +import { agentEvents, type Agent } from '@deepseek-ai/dsh-agent' +import { createUserMessage, freezeMessage } from '@deepseek-ai/dsh-llm' +import SessionStore, { Session, SessionId } from '@deepseek-ai/dsh-session' +import type { UserMessage } from '@deepseek-ai/dsh-session' +import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' +import { describe, expect, it } from 'vitest' +import { ReactLoopInbox } from '../src/inbox.ts' + +function unsupportedInbox(): Agent['inbox'] { + const rejectMutation = (): never => { + throw new Error('this test Agent does not support Inbox mutations') + } + return { + nextTurn: [], nextStep: [], clear: rejectMutation, append: rejectMutation, + prepend: rejectMutation, replace: rejectMutation, remove: rejectMutation, splice: rejectMutation, + } +} + +function stubAgent(rawId: string, overrides: Partial = {}): Agent { + const id = SessionId(rawId) + const session = overrides.session ?? Session.create(id) + const ctx = overrides.ctx ?? new Context() + return { + id, + options: {}, + session, + inbox: unsupportedInbox(), + status: 'idle', + ctx, + send: () => {}, + followup: () => {}, + steer: () => {}, + inject: () => {}, + cancel() {}, + runMaintenance: task => task(new AbortController().signal), + whenIdle: () => Promise.resolve(), + ...overrides, + } +} + +async function inboxAgent(rawId: string): Promise<{ + ctx: Context + session: Session + agent: Agent + inbox: ReactLoopInbox +}> { + const ctx = new Context() + await ctx.plugin(SessionStore) + await ctx.plugin(SessionProjectionRegistry) + const session = ctx.sessions.create(SessionId(rawId)) + const agent = stubAgent(rawId, { ctx, session }) + const inbox = new ReactLoopInbox(ctx.sessionProjections, session, agentEvents(ctx, agent)) + Object.assign(agent, { inbox }) + return { ctx, session, agent, inbox } +} + +async function reconstructPersistedInbox( + rawId: string, + populate: (session: Session) => void, +): Promise { + const ctx = new Context() + await ctx.plugin(SessionStore) + const session = ctx.sessions.create(SessionId(rawId)) + populate(session) + await ctx.plugin(SessionProjectionRegistry) + const agent = stubAgent(rawId, { ctx, session }) + const inbox = new ReactLoopInbox(ctx.sessionProjections, session, agentEvents(ctx, agent)) + try { + void inbox.nextTurn + } catch (error: unknown) { + if (error instanceof Error) return error + throw error + } + throw new Error('persisted inbox reconstruction unexpectedly succeeded') +} + +describe('ReactLoopInbox', () => { + it('registers the durable projection in its constructor', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + await ctx.plugin(SessionProjectionRegistry) + const session = ctx.sessions.create(SessionId('inbox-projection')) + const pending = createUserMessage({ + content: [{ type: 'text', text: 'pending' }], + source: { kind: 'user' }, + }) + session.append('agent/inbox/spliced', { + target: 'next-turn', start: 0, inserted: [pending], + }) + const agent = stubAgent('inbox-projection', { ctx, session }) + const dispatch = agentEvents(ctx, agent) + const first = new ReactLoopInbox(ctx.sessionProjections, session, dispatch) + const second = new ReactLoopInbox(ctx.sessionProjections, session, dispatch) + + expect(first.nextTurn).toEqual([pending]) + expect(second.nextTurn).toEqual([pending]) + expect(ctx.sessionProjections.snapshot(session).values.inbox).toEqual({ + 'next-turn': [pending], + 'next-step': [], + }) + }) + + it('rejects invalid durable coordinates and duplicate identities during reconstruction', async () => { + const outOfRange = await reconstructPersistedInbox('invalid-inbox-range', (session) => { + session.append('agent/inbox/spliced', { + target: 'next-turn', start: 0, removedCount: 1, inserted: [], + }) + }) + expect(outOfRange.message).toBe('invalid persisted inbox splice at session seq 0') + expect((outOfRange.cause as Error).message).toBe('invalid inbox splice') + + const pending = createUserMessage({ + content: [{ type: 'text', text: 'duplicate' }], + source: { kind: 'user' }, + }) + const duplicate = await reconstructPersistedInbox('invalid-inbox-duplicate', (session) => { + session.append('agent/inbox/spliced', { + target: 'next-turn', start: 0, inserted: [pending], + }) + session.append('agent/inbox/spliced', { + target: 'next-step', start: 0, inserted: [pending], + }) + }) + expect(duplicate.message).toBe('invalid persisted inbox splice at session seq 1') + expect((duplicate.cause as Error).message).toBe(`message "${pending.id}" is already pending`) + }) + + it('projects inherited inbox events in a forked session', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + await ctx.plugin(SessionProjectionRegistry) + const parent = ctx.sessions.create(SessionId('inbox-fork-parent')) + const parentAgent = stubAgent('inbox-fork-parent', { ctx, session: parent }) + const parentInbox = new ReactLoopInbox(ctx.sessionProjections, parent, agentEvents(ctx, parentAgent)) + const inherited = createUserMessage({ + content: [{ type: 'text', text: 'parent pending' }], + source: { kind: 'user' }, + }) + parentInbox.append('next-turn', inherited) + const child = ctx.sessions.fork(parent, undefined, SessionId('inbox-fork-child')) + const childAgent = stubAgent('inbox-fork-child', { ctx, session: child }) + const childInbox = new ReactLoopInbox(ctx.sessionProjections, child, agentEvents(ctx, childAgent)) + + expect(child.inheritedEventCount).toBe(parent.snapshotEvents().length) + expect(childInbox.nextTurn).toEqual([inherited]) + + const own = createUserMessage({ + content: [{ type: 'text', text: 'child pending' }], + source: { kind: 'user' }, + }) + childInbox.append('next-turn', own) + expect(childInbox.nextTurn).toEqual([inherited, own]) + + }) + + it('updates the projection cell before session observers run', async () => { + const { ctx, session, inbox } = await inboxAgent('inbox-live-projection') + const pending = createUserMessage({ + content: [{ type: 'text', text: 'direct' }], + source: { kind: 'user' }, + }) + let observed: readonly UserMessage[] | undefined + ctx.on('session/event', (subject, event) => { + if (subject === session && event.type === 'agent/inbox/spliced') { + observed = ctx.sessionProjections.stateOf(session, 'inbox')?.['next-turn'] + } + }) + + inbox.append('next-turn', pending) + + expect(observed).toEqual([pending]) + expect(ctx.sessionProjections.snapshot(session).values.inbox).toEqual({ + 'next-turn': [pending], 'next-step': [], + }) + }) + + it('replaces a pending message by identity across both lists', async () => { + const { ctx, agent } = await inboxAgent('replace-inbox') + const inserted: UserMessage[] = [] + const discarded: UserMessage[] = [] + ctx.on('agent/inbox/inserted', ({ message }) => void inserted.push(message)) + ctx.on('agent/inbox/discarded', ({ message }) => void discarded.push(message)) + const original = createUserMessage({ + content: [{ type: 'text', text: 'original' }], + source: { kind: 'user' }, + }) + const nextStep = createUserMessage({ + content: [{ type: 'text', text: 'step' }], + source: { kind: 'user' }, + }) + const replacement = createUserMessage({ + content: [{ type: 'text', text: 'replacement' }], + source: { kind: 'user' }, + }) + const editedStep = freezeMessage({ + ...nextStep, + content: [{ type: 'text', text: 'edited step' }], + }) + agent.inbox.append('next-turn', original) + agent.inbox.append('next-step', nextStep) + + expect(agent.inbox.replace(createUserMessage({ + content: [{ type: 'text', text: 'missing' }], + source: { kind: 'user' }, + }).id, replacement)).toBe(false) + expect(agent.inbox.replace(original.id, replacement)).toBe(true) + expect(agent.inbox.replace(nextStep.id, editedStep)).toBe(true) + expect(agent.inbox.nextTurn).toEqual([replacement]) + expect(agent.inbox.nextStep).toEqual([editedStep]) + expect(discarded).toEqual([original, nextStep]) + expect(inserted).toEqual([original, nextStep, replacement, editedStep]) + expect(() => { agent.inbox.replace(editedStep.id, replacement) }) + .toThrow(`message "${replacement.id}" is already pending`) + }) + + it('normalizes splice coordinates, rejects duplicate identities, and reports missing removals', async () => { + const { agent } = await inboxAgent('splice-inbox') + const first = createUserMessage({ + content: [{ type: 'text', text: 'first' }], + source: { kind: 'user' }, + }) + const second = createUserMessage({ + content: [{ type: 'text', text: 'second' }], + source: { kind: 'user' }, + }) + const prefixed = createUserMessage({ + content: [{ type: 'text', text: 'prefixed' }], + source: { kind: 'user' }, + }) + + agent.inbox.splice('next-turn', Number.NaN, Number.NaN, [first, second]) + expect(agent.inbox.nextTurn).toEqual([first, second]) + expect(agent.inbox.splice('next-turn', -1, 1, [])).toEqual([second]) + agent.inbox.prepend('next-turn', prefixed) + expect(agent.inbox.nextTurn).toEqual([prefixed, first]) + expect(agent.inbox.remove(second.id)).toBe(false) + expect(() => { agent.inbox.append('next-step', first) }).toThrow(`message "${first.id}" is already pending`) + }) + + it('clears both pending lists as durable cancellations', async () => { + const { ctx, session, agent } = await inboxAgent('clear-inbox') + const discarded: UserMessage[] = [] + ctx.on('agent/inbox/discarded', ({ message }) => void discarded.push(message)) + const nextTurn = createUserMessage({ content: [{ type: 'text', text: 'turn' }], source: { kind: 'user' } }) + const nextStep = createUserMessage({ content: [{ type: 'text', text: 'step' }], source: { kind: 'user' } }) + agent.inbox.append('next-turn', nextTurn) + agent.inbox.append('next-step', nextStep) + const beforeClear = session.snapshotEvents().length + + agent.inbox.clear() + + expect(agent.inbox.nextTurn).toEqual([]) + expect(agent.inbox.nextStep).toEqual([]) + expect(discarded).toEqual([nextStep, nextTurn]) + expect(session.snapshotEvents().slice(beforeClear).map(event => event.type === 'agent/inbox/spliced' + ? event.data + : event.type)).toEqual([ + { target: 'next-step', start: 0, removedCount: 1, inserted: [], outcome: 'canceled' }, + { target: 'next-turn', start: 0, removedCount: 1, inserted: [], outcome: 'canceled' }, + ]) + + agent.inbox.clear() + expect(session.snapshotEvents()).toHaveLength(beforeClear + 2) + }) +}) diff --git a/packages/core/agent-loop/tests/interception.spec.ts b/packages/core/agent-loop/tests/interception.spec.ts index 8ef5929522..b50fb45942 100644 --- a/packages/core/agent-loop/tests/interception.spec.ts +++ b/packages/core/agent-loop/tests/interception.spec.ts @@ -292,7 +292,8 @@ describe('agent/pre-step', () => { decision.resolve({ kind: 'enter', messages: claimed }) await idle - expect(agent.inbox.hasPending).toBe(false) + expect(agent.inbox.nextTurn).toEqual([]) + expect(agent.inbox.nextStep).toEqual([]) const staged = events(agent).filter(event => event.type === 'turn/start' || event.type === 'user/message') @@ -472,7 +473,8 @@ describe('agent/pre-step', () => { send(agent, 'blocked prompt') }).toThrow('append unavailable') expect(events(agent)).toEqual([]) - expect(agent.inbox.hasPending).toBe(false) + expect(agent.inbox.nextTurn).toEqual([]) + expect(agent.inbox.nextStep).toEqual([]) expect(agent.status).toBe('idle') }) diff --git a/packages/core/agent-loop/tests/request-freeze.spec.ts b/packages/core/agent-loop/tests/request-freeze.spec.ts index cfb2c64da2..c86e88cb65 100644 --- a/packages/core/agent-loop/tests/request-freeze.spec.ts +++ b/packages/core/agent-loop/tests/request-freeze.spec.ts @@ -8,7 +8,6 @@ import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-test import { createAssistantMessage, createUserMessage, isAgentLoopRequest } from '@deepseek-ai/dsh-llm' import type { GenerateOptions, ToolSchema } from '@deepseek-ai/dsh-llm' import { Session, SessionId, SessionLogOffset, SESSION_FORMAT_VERSION } from '@deepseek-ai/dsh-session' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import * as values from '@deepseek-ai/dsh-util-values' import { ReactLoopAgent } from '../src/agent.ts' import { MockAdapter, textResponse } from './mock-adapter.ts' @@ -23,14 +22,13 @@ afterEach(async () => { } }) -async function harness(adapter?: MockAdapter): Promise { +async function harness(adapter?: MockAdapter): Promise<{ ctx: Context; loopCtx: Context }> { const ctx = new Context() cleanups.push(() => ctx.fiber.dispose()) await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) - await ctx.plugin(AgentLoop, { agents: [] }) + const loopFiber = await ctx.plugin(AgentLoop, { agents: [] }) if (adapter) ctx.effect(() => ctx.llm.registerAdapter(['mock'], adapter)) - return ctx + return { ctx, loopCtx: loopFiber.ctx } } async function send(agent: Agent, text: string): Promise { @@ -46,7 +44,7 @@ function expectFrozen(value: unknown): void { describe('loop-owned request freezing', () => { it('adopts restored identities, freezes nested messages at dispatch, and leaves event wrappers mutable', async () => { - const ctx = await harness(new MockAdapter([textResponse('one'), textResponse('two'), textResponse('three'), textResponse('four')])) + const { ctx, loopCtx } = await harness(new MockAdapter([textResponse('one'), textResponse('two'), textResponse('three'), textResponse('four')])) const id = SessionId('restored-freeze') const seed = Session.create(id) seed.append('user/message', createUserMessage({ @@ -75,7 +73,7 @@ describe('loop-owned request freezing', () => { expect(Object.isFrozen(userEvent.data.content)).toBe(false) expect(Object.isFrozen(assistantEvent.data.message)).toBe(false) ctx.effect(() => ctx.sessions.enter(session)) - const agent = new ReactLoopAgent(ctx, id, { provider: 'mock', model: 'mock' }, session) + const agent = new ReactLoopAgent(loopCtx, id, { provider: 'mock', model: 'mock' }, session) cleanups.push(async () => { agent.cancel({ kind: 'disposed' }) await agent.whenIdle() @@ -124,7 +122,7 @@ describe('loop-owned request freezing', () => { expect(Object.isFrozen(session.deriveMessages())).toBe(false) expect(freeze.mock.calls.filter(([value]) => value === userEvent.data)).toHaveLength(1) expect(freeze.mock.calls.filter(([value]) => value === replacement.data)).toHaveLength(1) - const resumed = new ReactLoopAgent(ctx, id, { provider: 'mock', model: 'mock' }, session) + const resumed = new ReactLoopAgent(loopCtx, id, { provider: 'mock', model: 'mock' }, session) cleanups.push(async () => { resumed.cancel({ kind: 'disposed' }) await resumed.whenIdle() @@ -136,7 +134,7 @@ describe('loop-owned request freezing', () => { }) it('retries freezing an identity whose previous traversal failed', async () => { - const ctx = await harness(new MockAdapter([textResponse('done')])) + const { ctx } = await harness(new MockAdapter([textResponse('done')])) const agent = await ctx.agentLoop.create(SessionId('freeze-failure'), { provider: 'mock', model: 'mock' }) const message = agent.session.append('user/message', createUserMessage({ content: [{ type: 'text', text: 'history' }], source: { kind: 'user' }, @@ -163,7 +161,7 @@ describe('loop-owned request freezing', () => { it.each([true, false])('freezes each local header with an adapter present: %s', async (registered) => { const adapter = registered ? new MockAdapter([textResponse('one'), textResponse('two')]) : undefined - const ctx = await harness(adapter) + const { ctx } = await harness(adapter) const schemas: ToolSchema[][] = [] const stops: string[][] = [] ctx.on('system-prompt/assemble', async (_assembly, _context, next) => { @@ -206,7 +204,7 @@ describe('loop-owned request freezing', () => { }) it('keeps the live request signal mutable and observes cancellation after dispatch', async () => { - const ctx = await harness(new MockAdapter(['hang'])) + const { ctx } = await harness(new MockAdapter(['hang'])) const agent = await ctx.agentLoop.create(SessionId('cancel-freeze'), { provider: 'mock', model: 'mock' }) const started = Promise.withResolvers() ctx.on('llm/stream', (request, next) => { started.resolve(request); return next() }) diff --git a/packages/core/agent-loop/tests/scope-lifecycle.spec.ts b/packages/core/agent-loop/tests/scope-lifecycle.spec.ts index c27d0d2534..5f19bbc422 100644 --- a/packages/core/agent-loop/tests/scope-lifecycle.spec.ts +++ b/packages/core/agent-loop/tests/scope-lifecycle.spec.ts @@ -195,6 +195,33 @@ describe('agent scope lifecycle', () => { expect(after.sections.find(s => s.name === 'deployment:persona-prefix')?.text).toBe('You are the deployment.') }) + it('keeps the inbox projection until the last owning agent fiber unloads', async () => { + const ctx = await harness() + let first!: Awaited> + let second!: Awaited> + const firstOwner = await ctx.plugin(Object.assign(async (inner: Context) => { + first = await inner.agents.create({ + sessionId: SessionId('projection-owner-first'), + agentOptions: { provider: 'mock', model: 'mock' }, + }) + }, { inject: ['agents'] })) + const secondOwner = await ctx.plugin(Object.assign(async (inner: Context) => { + second = await inner.agents.create({ + sessionId: SessionId('projection-owner-second'), + agentOptions: { provider: 'mock', model: 'mock' }, + }) + }, { inject: ['agents'] })) + + expect(ctx.sessionProjections.stateOf(first.agent.session, 'inbox')).toBeDefined() + await firstOwner.dispose() + expect(ctx.sessionProjections.stateOf(second.agent.session, 'inbox')).toBeDefined() + await secondOwner.dispose() + expect(ctx.sessionProjections.stateOf(second.agent.session, 'inbox')).toBeUndefined() + + await Promise.all([first.dispose(), second.dispose()]) + await ctx.fiber.dispose() + }) + 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 = await ctx.agentLoop.create(SessionId('a'), { provider: 'mock', model: 'mock' }) diff --git a/packages/core/agent/README.i18n.yaml b/packages/core/agent/README.i18n.yaml index 7c0fb5c510..5180dec6b1 100644 --- a/packages/core/agent/README.i18n.yaml +++ b/packages/core/agent/README.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 packages/core/agent/README.md -README.md: f413abd855be17fde2e38997df8269d1ca2670b5 -README.zh.md: 74797894177994cb3098aa67e969f45ced357634 +README.md: b1333e96c1eaf83c4a5598a23bdae068e227c25f +README.zh.md: d15ce22b8f845e268fb935382ff16c7fd20b9c67 diff --git a/packages/core/agent/README.md b/packages/core/agent/README.md index f413abd855..b1333e96c1 100644 --- a/packages/core/agent/README.md +++ b/packages/core/agent/README.md @@ -84,14 +84,19 @@ The package is built on one separation: the public `Agent` surface and registry `PreStepDecision` is either `{ kind: 'reject' }` or `{ kind: 'enter', messages, startsRequestSeries? }`. The enter branch contains the complete identified, frozen message batch. `startsRequestSeries: true` declares a distinct model-message series; a wrapping listener preserves that declaration and the batch unless it intentionally replaces either one. Claiming removes offered messages from the inbox, while messages inserted after the claim remain pending for a later boundary. +### Durable inbox + +`Agent.inbox` exposes only the structural `Inbox` interface and the projection vocabulary stays in this package. dsh-agent-loop owns the package-internal `ReactLoopInbox` and the standard `inbox` projection; constructing its concrete inbox ensures that the projection registry owns one registration for the durable `agent/inbox/spliced` fold. The registry remains the sole owner of the live `{ 'next-turn', 'next-step' }` state. Reconstruction rejects unsafe or out-of-range splice coordinates and duplicate `MessageId` values across both pending lists and reports the offending event seq. + +`Inbox` exposes pending `nextTurn` and `nextStep` messages and mutates them through `append`, `prepend`, `replace`, `remove`, `clear`, and `splice`. Ordinary removals and `clear()` are durable cancellations. At a step boundary, the loop's internal implementation claims pending input through pure deletion splices. Live notifications are deliberately per-message and minimal: `agent/inbox/inserted { message }`, `agent/inbox/claimed { message, turn }`, and `agent/inbox/discarded { message }`. + ### Source map | File | Role | |---|---| | [`src/index.ts`](src/index.ts) | Plugin entry: `AgentRegistry`, factory slot, initiator scope, `CreateAgentOptions`/`ResumeAgentOptions` | -| [`src/runtime-types.ts`](src/runtime-types.ts) | `Agent`, `AgentStatus`, and the `agent/*` event declarations | -| [`src/types.ts`](src/types.ts) | `AgentOptions`, cancellation causes, and inbox vocabulary | -| [`src/inbox.ts`](src/inbox.ts) | The `Inbox` projection over durable `agent/inbox/spliced` events | +| [`src/runtime-types.ts`](src/runtime-types.ts) | `Agent`, structural `Inbox`, `AgentStatus`, and the `agent/*` event declarations | +| [`src/types.ts`](src/types.ts) | `AgentOptions`, cancellation causes, and inbox projection vocabulary | | [`src/dispatch.ts`](src/dispatch.ts) | `agentEvents` fused dispatcher and `assembleContextFor(agent)` | | [`src/consumed-work.ts`](src/consumed-work.ts) | `foldConsumedWork(events)`: what the log's consumed work became | | [`src/model-selection.ts`](src/model-selection.ts) | `installModelSelection`: coupling one selection to assembly and routing | diff --git a/packages/core/agent/README.zh.md b/packages/core/agent/README.zh.md index 7479789417..d15ce22b8f 100644 --- a/packages/core/agent/README.zh.md +++ b/packages/core/agent/README.zh.md @@ -84,14 +84,19 @@ await handle.agent.whenIdle() `PreStepDecision` 要么是 `{ kind: 'reject' }`,要么是 `{ kind: 'enter', messages, startsRequestSeries? }`。enter 分支包含完整、带标识且冻结的消息批次。`startsRequestSeries: true` 声明一个独立的模型消息序列;包装下游 enter 的监听器会保留该声明与批次,除非有意替换其中一项。领取会从 inbox 移除候选消息,领取后插入的消息则等待后续边界。 +### 持久 inbox + +`Agent.inbox` 只暴露结构化 `Inbox` 接口,投影词汇仍位于本包。dsh-agent-loop 持有包内部的 `ReactLoopInbox` 与标准 `inbox` 投影;构造具体 inbox 时会确保投影注册表为持久 `agent/inbox/spliced` fold 持有一份注册。注册表继续作为实时 `{ 'next-turn', 'next-step' }` 状态的唯一所有者。重建过程会拒绝不安全或越界的 splice 坐标,以及跨两份待处理列表重复的 `MessageId`,并报告出错事件的 seq。 + +`Inbox` 暴露待处理的 `nextTurn` 与 `nextStep` 消息,并通过 `append`、`prepend`、`replace`、`remove`、`clear` 与 `splice` 变更它们。普通删除和 `clear()` 都是持久取消。在步骤边界,循环的内部实现会通过纯删除 splice 领取待处理输入。实时通知刻意采用逐消息的最小载荷:`agent/inbox/inserted { message }`、`agent/inbox/claimed { message, turn }` 与 `agent/inbox/discarded { message }`。 + ### 源码地图 | 文件 | 职责 | |---|---| | [`src/index.ts`](src/index.ts) | 插件入口:`AgentRegistry`、工厂槽位、发起方作用域、`CreateAgentOptions`/`ResumeAgentOptions` | -| [`src/runtime-types.ts`](src/runtime-types.ts) | `Agent`、`AgentStatus` 与 `agent/*` 事件声明 | -| [`src/types.ts`](src/types.ts) | `AgentOptions`、取消原因与收件箱词汇 | -| [`src/inbox.ts`](src/inbox.ts) | 持久 `agent/inbox/spliced` 事件之上的 `Inbox` 投影 | +| [`src/runtime-types.ts`](src/runtime-types.ts) | `Agent`、结构化 `Inbox`、`AgentStatus` 与 `agent/*` 事件声明 | +| [`src/types.ts`](src/types.ts) | `AgentOptions`、取消原因与收件箱投影词汇 | | [`src/dispatch.ts`](src/dispatch.ts) | `agentEvents` 融合分发器与 `assembleContextFor(agent)` | | [`src/consumed-work.ts`](src/consumed-work.ts) | `foldConsumedWork(events)`:日志消费掉的工作最终怎样了 | | [`src/model-selection.ts`](src/model-selection.ts) | `installModelSelection`:把一个选择耦合到组装与路由 | diff --git a/packages/core/agent/package.json b/packages/core/agent/package.json index 23d5d34088..11fa110fcf 100644 --- a/packages/core/agent/package.json +++ b/packages/core/agent/package.json @@ -41,20 +41,22 @@ "@deepseek-ai/dsh-llm": "workspace:^", "@deepseek-ai/dsh-scope": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", + "@deepseek-ai/dsh-session-projection": "workspace:^", "@deepseek-ai/dsh-system-prompt": "workspace:^", "@deepseek-ai/dsh-typert-protocol": "workspace:^", - "@deepseek-ai/cordis": "workspace:^", - "@deepseek-ai/dsh-session-projection": "workspace:^" + "@deepseek-ai/dsh-util-values": "workspace:^", + "@deepseek-ai/cordis": "workspace:^" }, "devDependencies": { "@deepseek-ai/dsh-invariants": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", "@deepseek-ai/dsh-scope": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", + "@deepseek-ai/dsh-session-projection": "workspace:^", "@deepseek-ai/dsh-system-prompt": "workspace:^", "@deepseek-ai/dsh-typert-protocol": "workspace:^", "@deepseek-ai/dsh-typert-registry": "workspace:^", - "@deepseek-ai/cordis": "workspace:^", - "@deepseek-ai/dsh-session-projection": "workspace:^" + "@deepseek-ai/dsh-util-values": "workspace:^", + "@deepseek-ai/cordis": "workspace:^" } } diff --git a/packages/core/agent/src/inbox.ts b/packages/core/agent/src/inbox.ts deleted file mode 100644 index c5d50b222a..0000000000 --- a/packages/core/agent/src/inbox.ts +++ /dev/null @@ -1,220 +0,0 @@ -/** - * Incremental projection of durable agent inbox events. - * - * @module @deepseek-ai/dsh-agent/inbox - */ - -import type { MessageId } from '@deepseek-ai/dsh-llm' -import type { Session, SessionEventMap, UserMessage } from '@deepseek-ai/dsh-session' -import type { InboxTarget } from './types.ts' - -/** Mutable state privately owned by an {@link Inbox}. */ -type InboxState = Record - -/** Live notifications committed by inbox mutations. */ -export interface InboxNotifications { - /** Publish one inserted message. */ - inserted(message: UserMessage): void - /** Publish one discarded message. */ - discarded(message: UserMessage): void - /** Publish one claimed message inside its owning turn. */ - claimed(message: UserMessage, turn: number): void -} - -/** A replay-once projection that incrementally consumes later inbox splices. */ -export class Inbox { - private readonly state: InboxState = { 'next-turn': [], 'next-step': [] } - - constructor( - private readonly session: Session, - private readonly notifications: InboxNotifications, - ) { - for (const event of session.ownEvents()) { - if (event.type !== 'agent/inbox/spliced') continue - try { - this.apply(event.data) - } catch (error: unknown) { - throw new Error(`invalid persisted inbox splice at session seq ${event.seq}`, { cause: error }) - } - } - } - - /** Prompts awaiting individual turns. */ - get nextTurn(): readonly UserMessage[] { - return this.state['next-turn'] - } - - /** Input awaiting the next step boundary. */ - get nextStep(): readonly UserMessage[] { - return this.state['next-step'] - } - - /** Whether either pending-message list contains work. */ - get hasPending(): boolean { - return this.nextTurn.length > 0 || this.nextStep.length > 0 - } - - /** Durably cancel all pending input, clearing next-step before next-turn. */ - clear(): void { - this.splice('next-step', 0, this.nextStep.length, []) - this.splice('next-turn', 0, this.nextTurn.length, []) - } - - /** - * Remove and return the complete batch proposed for one step, publishing - * each claimed message. The durable splices are pure deletions. - * @param target - whether this boundary also consumes one queued turn. - * @param turn - turn that will own the claimed batch. - * @returns next-step input followed by the queued turn, when requested. - * @internal - The agent loop's step-boundary operation, not a plugin extension point. - */ - claim(target: InboxTarget, turn: number): UserMessage[] { - const claimed = this.mutate('next-step', 0, this.nextStep.length, [], false) - if (target === 'next-turn') { - claimed.push(...this.mutate('next-turn', 0, 1, [], false)) - } - for (const message of claimed) this.notifications.claimed(message, turn) - return claimed - } - - /** - * Append one message to a pending list and durably record the insertion. - * @param target - pending list to extend. - * @param message - message to append. - * @throws if the message identity is already pending. - */ - append(target: InboxTarget, message: UserMessage): void { - this.splice(target, this.state[target].length, 0, [message]) - } - - /** - * Prepend one message to a pending list and durably record the insertion. - * @param target - pending list to extend. - * @param message - message to prepend. - * @throws if the message identity is already pending. - */ - prepend(target: InboxTarget, message: UserMessage): void { - this.splice(target, 0, 0, [message]) - } - - /** - * Replace one pending message in place, possibly changing its identity. A - * successful replacement publishes the old message as discarded and the new - * message as inserted. - * @param messageId - identity of the pending message to replace. - * @param newMessage - replacement message. - * @returns whether the message was still pending. - * @throws if the replacement duplicates another pending message identity. - */ - replace(messageId: MessageId, newMessage: UserMessage): boolean { - const location = this.locate(messageId) - if (location === undefined) return false - this.splice(location.target, location.index, 1, [newMessage]) - return true - } - - /** - * Remove one pending message and durably record its cancellation. - * @param messageId - identity of the pending message to remove. - * @returns whether the message was still pending. - */ - remove(messageId: MessageId): boolean { - const location = this.locate(messageId) - if (location === undefined) return false - this.splice(location.target, location.index, 1, []) - return true - } - - /** - * Apply standard splice semantics and durably record the normalized result. - * The durable event commits before the live projection mutates, so synchronous - * `session/event` observers see the pre-splice lists and can reconstruct the - * removed messages from the normalized coordinates. - * @param target - pending list to mutate. - * @param start - splice position. - * @param deleteCount - maximum number of messages to remove. - * @param inserted - messages to insert at the resolved position. - * @returns messages removed by the splice. - */ - splice( - target: InboxTarget, - start: number, - deleteCount: number, - inserted: UserMessage[], - ): UserMessage[] { - return this.mutate(target, start, deleteCount, inserted, true) - } - - /** Locate one pending identity across both owned lists. */ - private locate(messageId: MessageId): { target: InboxTarget; index: number } | undefined { - for (const target of ['next-turn', 'next-step'] as const) { - const index = this.state[target].findIndex(message => message.id === messageId) - if (index >= 0) return { target, index } - } - return undefined - } - - /** Commit one normalized mutation and publish its live notifications. */ - private mutate( - target: InboxTarget, - start: number, - deleteCount: number, - inserted: UserMessage[], - discardRemoved: boolean, - ): UserMessage[] { - const inbox = this.state[target] - const truncatedStart = Math.trunc(start) - const offset = Number.isNaN(truncatedStart) ? 0 : truncatedStart - const actualStart = offset < 0 - ? Math.max(inbox.length + offset, 0) - : Math.min(offset, inbox.length) - const truncatedDeleteCount = Math.trunc(deleteCount) - const actualDeleteCount = Math.min( - Math.max(Number.isNaN(truncatedDeleteCount) ? 0 : truncatedDeleteCount, 0), - inbox.length - actualStart, - ) - if (actualDeleteCount === 0 && inserted.length === 0) return [] - const outcome = discardRemoved && actualDeleteCount > 0 ? 'canceled' as const : undefined - const splice = { - target, - start: actualStart, - ...(actualDeleteCount === 0 ? {} : { removedCount: actualDeleteCount }), - inserted, - ...(outcome === undefined ? {} : { outcome }), - } - this.validate(splice) - const event = this.session.append('agent/inbox/spliced', splice) - const removed = inbox.splice(actualStart, actualDeleteCount, ...event.data.inserted) - if (discardRemoved) { - for (const message of removed) this.notifications.discarded(message) - } - for (const message of event.data.inserted) this.notifications.inserted(message) - return removed - } - - /** Apply one normalized durable splice to the projection. */ - private apply(splice: SessionEventMap['agent/inbox/spliced']): UserMessage[] { - this.validate(splice) - const inbox = this.state[splice.target] - return inbox.splice(splice.start, splice.removedCount ?? 0, ...splice.inserted) - } - - /** Validate one normalized splice against the current projection. */ - private validate(splice: SessionEventMap['agent/inbox/spliced']): void { - const inbox = this.state[splice.target] - const removedCount = splice.removedCount ?? 0 - if (!Number.isSafeInteger(splice.start) || splice.start < 0 || splice.start > inbox.length - || !Number.isSafeInteger(removedCount) || removedCount < 0 - || splice.start + removedCount > inbox.length) { - throw new Error('invalid inbox splice') - } - const candidate = inbox.toSpliced(splice.start, removedCount, ...splice.inserted) - const ids = new Set() - for (const message of splice.target === 'next-turn' - ? [...candidate, ...this.nextStep] - : [...this.nextTurn, ...candidate]) { - if (ids.has(message.id)) throw new Error(`message "${message.id}" is already pending`) - ids.add(message.id) - } - } -} diff --git a/packages/core/agent/src/index.ts b/packages/core/agent/src/index.ts index 7df60db27c..e9d41bc0dd 100644 --- a/packages/core/agent/src/index.ts +++ b/packages/core/agent/src/index.ts @@ -18,7 +18,6 @@ import type { AgentOptions } from './runtime-types.ts' export * from './runtime-types.ts' export * from './types.ts' export type * from './projection.ts' -export * from './inbox.ts' export * from './consumed-work.ts' export * from './model-selection.ts' export { agentCarrier, agentEvents, assembleContextFor, emitAgentEvent } from './dispatch.ts' diff --git a/packages/core/agent/src/runtime-types.ts b/packages/core/agent/src/runtime-types.ts index 2b6931b257..8f2f471f12 100644 --- a/packages/core/agent/src/runtime-types.ts +++ b/packages/core/agent/src/runtime-types.ts @@ -8,12 +8,11 @@ import type { Context } from '@deepseek-ai/cordis' import type { Scoped } from '@deepseek-ai/dsh-scope' import type { - LlmAttemptId, LlmCallConfig, LlmFailure, ReasoningEffortId, ResolvedRetryPolicy, StreamChunk, + LlmAttemptId, LlmCallConfig, LlmFailure, MessageId, ReasoningEffortId, ResolvedRetryPolicy, StreamChunk, } from '@deepseek-ai/dsh-llm' import type { AgentCancelCause, Session, SessionSeq, UserMessage } from '@deepseek-ai/dsh-session' export type { AgentCancelCause } from '@deepseek-ai/dsh-session' -import type { Inbox } from './inbox.ts' -import type { Agent } from './types.ts' +import type { Agent, InboxTarget } from './types.ts' export type { Agent } from './types.ts' import type {} from '@deepseek-ai/dsh-system-prompt' declare module '@deepseek-ai/dsh-system-prompt' { @@ -45,6 +44,61 @@ export interface CancelOptions { keepInbox?: boolean | undefined } +/** Agent-owned access to pending work; concrete storage belongs to the driver. */ +export interface Inbox { + /** Prompts awaiting individual turns. */ + readonly nextTurn: readonly UserMessage[] + /** Input awaiting the next step boundary. */ + readonly nextStep: readonly UserMessage[] + + /** Durably cancel all pending input, clearing next-step before next-turn. */ + clear(): void + + /** + * Append one message to a pending list. + * @param target - pending list to extend. + * @param message - message to append. + */ + append(target: InboxTarget, message: UserMessage): void + + /** + * Prepend one message to a pending list. + * @param target - pending list to extend. + * @param message - message to prepend. + */ + prepend(target: InboxTarget, message: UserMessage): void + + /** + * Replace one pending message in place. + * @param messageId - identity of the pending message to replace. + * @param newMessage - replacement message. + * @returns whether the message was still pending. + */ + replace(messageId: MessageId, newMessage: UserMessage): boolean + + /** + * Remove one pending message. + * @param messageId - identity of the pending message to remove. + * @returns whether the message was still pending. + */ + remove(messageId: MessageId): boolean + + /** + * Apply standard splice semantics and durably record the normalized result. + * @param target - pending list to mutate. + * @param start - splice position. + * @param deleteCount - maximum number of messages to remove. + * @param inserted - messages to insert at the resolved position. + * @returns messages removed by the splice. + */ + splice( + target: InboxTarget, + start: number, + deleteCount: number, + inserted: UserMessage[], + ): UserMessage[] +} + /** * An agent's lifecycle state, emitted on every transition as `agent/status`: * `idle` means no driver is active; `running` begins when waking input starts @@ -112,7 +166,7 @@ declare module './types.ts' { readonly options: AgentOptions /** The live session this agent drives; its log is the durable source of truth. */ readonly session: Session - /** The agent-owned projection of durable pending work. */ + /** Agent-owned access to durable pending work. */ readonly inbox: Inbox /** The current lifecycle state, mirrored on every `agent/status` transition. */ readonly status: AgentStatus diff --git a/packages/core/agent/src/types.ts b/packages/core/agent/src/types.ts index c7b82e243e..d0be69ac58 100644 --- a/packages/core/agent/src/types.ts +++ b/packages/core/agent/src/types.ts @@ -7,6 +7,7 @@ import type { UserMessage } from '@deepseek-ai/dsh-llm/types' import type { OptionalSessionSeq, SessionId, SessionSeq } from '@deepseek-ai/dsh-session/types' import type { TypertContext, TypertLookup } from '@deepseek-ai/dsh-typert-protocol' +import type { JsonValue } from '@deepseek-ai/dsh-util-values' /** Public live-agent handle; the runtime face augments its live capabilities. */ export interface Agent { @@ -28,6 +29,34 @@ declare module '@deepseek-ai/dsh-typert-protocol' { /** One of the two ordered pending-message lists owned by an agent. */ export type InboxTarget = 'next-turn' | 'next-step' +/** Complete pending Inbox value reconstructed from durable splices. */ +export interface InboxState { + readonly 'next-turn': readonly UserMessage[] + readonly 'next-step': readonly UserMessage[] +} + +/** + * Wire-JSON pending Inbox value. Each message round-trips the session log + * losslessly, but the fold state's full `UserMessage` type cannot cross a + * typert Remote boundary (its source union carries an `unknown` replay + * field), so the typed projection table keeps this JSON-safe form. + */ +export interface InboxWireState { + readonly 'next-turn': readonly JsonValue[] + readonly 'next-step': readonly JsonValue[] +} + +declare module '@deepseek-ai/dsh-session-projection/types' { + interface SessionProjectionStateMap { + /** Pending agent input reconstructed from durable inbox splices. */ + inbox: InboxState + } + interface SessionProjectionMap { + /** Pending agent input reconstructed from durable inbox splices. */ + inbox: InboxWireState + } +} + /** * Turn and step boundaries folded from one agent session log. * @@ -52,8 +81,8 @@ declare module '@deepseek-ai/dsh-session/types' { interface SessionEventMap { /** * One normalized mutation of an agent's durable pending-message lists. - * Live dispatch precedes projection mutation, so synchronous observers may - * read the pre-splice inbox to recover the removed messages. + * The session-projection registry applies the committed event before + * `Session.append()` returns; Inbox live notifications follow that commit. */ 'agent/inbox/spliced': { target: InboxTarget diff --git a/packages/core/agent/tests/agent.spec.ts b/packages/core/agent/tests/agent.spec.ts index e1da38b652..f0649dd5f5 100644 --- a/packages/core/agent/tests/agent.spec.ts +++ b/packages/core/agent/tests/agent.spec.ts @@ -1,11 +1,7 @@ import { describe, expect, expectTypeOf, it } from 'vitest' import { Context, Service, symbols } from '@deepseek-ai/cordis' -import { createUserMessage, freezeMessage } from '@deepseek-ai/dsh-llm' -import { Session, SessionId, type UserMessage } from '@deepseek-ai/dsh-session' -import AgentRegistry, { - agentEvents, - Inbox, -} from '@deepseek-ai/dsh-agent' +import { Session, SessionId } from '@deepseek-ai/dsh-session' +import AgentRegistry, { agentEvents } from '@deepseek-ai/dsh-agent' import TypertRegistry from '@deepseek-ai/dsh-typert-registry' import type { @@ -19,14 +15,17 @@ import type { function stubAgent(rawId: string, overrides: Partial = {}): Agent { const id = SessionId(rawId) - const session = Session.create(id) + const session = overrides.session ?? Session.create(id) + const ctx = overrides.ctx ?? new Context() const agent: Agent = { id, options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: { + nextTurn: [], nextStep: [], + } as never, status: 'idle', - ctx: new Context(), + ctx, send: () => {}, followup: () => {}, steer: () => ({ outcome: Promise.resolve({ status: 'rejected' as const }) }), @@ -34,114 +33,11 @@ function stubAgent(rawId: string, overrides: Partial = {}): Agent { cancel() {}, runMaintenance: task => task(new AbortController().signal), whenIdle: () => Promise.resolve(), + ...overrides, } - return Object.assign(agent, overrides) + return agent } -describe('Inbox', () => { - it('rejects an invalid durable splice during reconstruction', () => { - const session = Session.create(SessionId('invalid-inbox-replay')) - session.append('agent/inbox/spliced', { - target: 'next-turn', - start: 1, - inserted: [], - }) - - expect(() => new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} })) - .toThrow('invalid persisted inbox splice at session seq 0') - }) - - it('replaces a pending message by identity across both lists', () => { - const session = Session.create(SessionId('replace-inbox')) - const inserted: UserMessage[] = [] - const discarded: UserMessage[] = [] - const inbox = new Inbox(session, { - claimed: () => {}, - inserted: message => void inserted.push(message), - discarded: message => void discarded.push(message), - }) - const original = createUserMessage({ - content: [{ type: 'text', text: 'original' }], - source: { kind: 'user' }, - }) - const nextStep = createUserMessage({ - content: [{ type: 'text', text: 'step' }], - source: { kind: 'user' }, - }) - const replacement = createUserMessage({ - content: [{ type: 'text', text: 'replacement' }], - source: { kind: 'user' }, - }) - const editedStep = freezeMessage({ - ...nextStep, - content: [{ type: 'text', text: 'edited step' }], - }) - inbox.append('next-turn', original) - inbox.append('next-step', nextStep) - - expect(inbox.replace(createUserMessage({ - content: [{ type: 'text', text: 'missing' }], - source: { kind: 'user' }, - }).id, replacement)).toBe(false) - expect(inbox.replace(original.id, replacement)).toBe(true) - expect(inbox.replace(nextStep.id, editedStep)).toBe(true) - expect(inbox.nextTurn).toEqual([replacement]) - expect(inbox.nextStep).toEqual([editedStep]) - expect(discarded).toEqual([original, nextStep]) - expect(inserted).toEqual([original, nextStep, replacement, editedStep]) - expect(() => { inbox.replace(editedStep.id, replacement) }) - .toThrow(`message "${replacement.id}" is already pending`) - }) - - it('normalizes splice coordinates, rejects duplicate identities, and reports missing removals', () => { - const session = Session.create(SessionId('splice-inbox')) - const inbox = new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }) - const first = createUserMessage({ - content: [{ type: 'text', text: 'first' }], - source: { kind: 'user' }, - }) - const second = createUserMessage({ - content: [{ type: 'text', text: 'second' }], - source: { kind: 'user' }, - }) - - inbox.splice('next-turn', Number.NaN, Number.NaN, [first, second]) - expect(inbox.nextTurn).toEqual([first, second]) - expect(inbox.splice('next-turn', -1, 1, [])).toEqual([second]) - expect(inbox.remove(second.id)).toBe(false) - expect(() => { inbox.append('next-step', first) }).toThrow(`message "${first.id}" is already pending`) - }) - - it('clears both pending lists as durable cancellations', () => { - const session = Session.create(SessionId('clear-inbox')) - const discarded: UserMessage[] = [] - const inbox = new Inbox(session, { - claimed: () => {}, - inserted: () => {}, - discarded: message => void discarded.push(message), - }) - const nextTurn = createUserMessage({ content: [{ type: 'text', text: 'turn' }], source: { kind: 'user' } }) - const nextStep = createUserMessage({ content: [{ type: 'text', text: 'step' }], source: { kind: 'user' } }) - inbox.append('next-turn', nextTurn) - inbox.append('next-step', nextStep) - const beforeClear = session.snapshotEvents().length - - inbox.clear() - - expect(inbox.hasPending).toBe(false) - expect(discarded).toEqual([nextStep, nextTurn]) - expect(session.snapshotEvents().slice(beforeClear).map(event => event.type === 'agent/inbox/spliced' - ? event.data - : event.type)).toEqual([ - { target: 'next-step', start: 0, removedCount: 1, inserted: [], outcome: 'canceled' }, - { target: 'next-turn', start: 0, removedCount: 1, inserted: [], outcome: 'canceled' }, - ]) - - inbox.clear() - expect(session.snapshotEvents()).toHaveLength(beforeClear + 2) - }) -}) - describe('AgentRegistry', () => { it('contributes Agent lookup and scoped Context providers while Typert is live', async () => { const ctx = new Context() diff --git a/packages/core/agent/tsconfig.json b/packages/core/agent/tsconfig.json index f7aae0283e..068c1e6b8f 100644 --- a/packages/core/agent/tsconfig.json +++ b/packages/core/agent/tsconfig.json @@ -23,15 +23,15 @@ { "path": "../../core/session" }, + { + "path": "../../session/session-projection" + }, { "path": "../../core/system-prompt" }, { "path": "../../runtime-diagnostics/invariants" }, - { - "path": "../../session/session-projection" - }, { "path": "../../typert/protocol" } diff --git a/packages/e2b/e2b/package.json b/packages/e2b/e2b/package.json index 88fa83c233..8c0b15f0bf 100644 --- a/packages/e2b/e2b/package.json +++ b/packages/e2b/e2b/package.json @@ -37,6 +37,7 @@ "devDependencies": { "@deepseek-ai/cordis": "workspace:^", "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-app-boot": "workspace:^", "@deepseek-ai/dsh-bash-local": "workspace:^", "@deepseek-ai/dsh-fs-e2b": "workspace:^", diff --git a/packages/e2b/e2b/tests/composition.e2e.ts b/packages/e2b/e2b/tests/composition.e2e.ts index dbbdff065e..de6f192bf3 100644 --- a/packages/e2b/e2b/tests/composition.e2e.ts +++ b/packages/e2b/e2b/tests/composition.e2e.ts @@ -3,7 +3,6 @@ import { join, posix } from 'node:path' import { fileURLToPath } from 'node:url' import { Context } from '@deepseek-ai/cordis' import { describe, expect, it } from 'vitest' -import { Inbox } from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import { runLoaderSmoke } from '@deepseek-ai/dsh-loader-smoke' import { @@ -17,6 +16,7 @@ import SandboxPolicyService from '@deepseek-ai/dsh-sandbox-policy' import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { Session, SessionId } from '@deepseek-ai/dsh-session' import E2BSubprocessRuntime from '@deepseek-ai/dsh-subprocess-e2b' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' const fixtureRoot = fileURLToPath(new URL('./fixtures/composition/', import.meta.url)) const binScript = join(fixtureRoot, 'bin.ts') @@ -87,7 +87,7 @@ describe.skipIf(!process.env.E2B_API_KEY)('E2B live Loader composition', () => { id: ownerId, options: {}, session: ownerSession, - inbox: new Inbox(ownerSession, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: unsupportedInbox(), status: 'idle', ctx, send() {}, diff --git a/packages/e2b/e2b/tests/fixtures/composition/bin.ts b/packages/e2b/e2b/tests/fixtures/composition/bin.ts index 4e4ffc120e..d762a2dda7 100644 --- a/packages/e2b/e2b/tests/fixtures/composition/bin.ts +++ b/packages/e2b/e2b/tests/fixtures/composition/bin.ts @@ -1,8 +1,7 @@ import { readFile } from 'node:fs/promises' import { resolve } from 'node:path' import { boot } from '@deepseek-ai/dsh-app-boot' -import { Inbox } from '@deepseek-ai/dsh-agent' -import type { Agent } from '@deepseek-ai/dsh-agent' +import { type Agent } from '@deepseek-ai/dsh-agent' import { Session, SessionId } from '@deepseek-ai/dsh-session' import type {} from '@deepseek-ai/dsh-fs-e2b' import type {} from '@deepseek-ai/dsh-bash-local' @@ -16,11 +15,23 @@ const ctx = await boot('e2b-composition', resolve(configPath)) const ownerFiber = ctx.plugin(() => {}) const ownerId = SessionId('e2b-live-owner') const session = Session.create(ownerId) +const unsupportedInboxMutation = (): never => { + throw new Error('the E2B composition owner does not support Inbox mutations') +} const owner: Agent = { id: ownerId, options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: { + nextTurn: [], + nextStep: [], + clear: unsupportedInboxMutation, + append: unsupportedInboxMutation, + prepend: unsupportedInboxMutation, + replace: unsupportedInboxMutation, + remove: unsupportedInboxMutation, + splice: unsupportedInboxMutation, + }, status: 'idle', ctx: ownerFiber.ctx, send() {}, diff --git a/packages/experimental/agent-team/tests/persistence.spec.ts b/packages/experimental/agent-team/tests/persistence.spec.ts index 7fc870e5db..6ff838ddf0 100644 --- a/packages/experimental/agent-team/tests/persistence.spec.ts +++ b/packages/experimental/agent-team/tests/persistence.spec.ts @@ -8,9 +8,7 @@ import type { Agent } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import { createUserMessage } from '@deepseek-ai/dsh-llm' -import { SessionId } from '@deepseek-ai/dsh-session' -import type { SessionEvent } from '@deepseek-ai/dsh-session' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' +import { SessionId, type SessionEvent } from '@deepseek-ai/dsh-session' import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl' import SubagentService, { snapshotSubagentDescriptor } from '@deepseek-ai/dsh-subagent' import * as SubagentSpawn from '@deepseek-ai/dsh-subagent-spawn-in-process' @@ -102,7 +100,6 @@ async function stack( const ctx = new Context() contexts.add(ctx) await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await backend.mount(ctx, root) await ctx.plugin(TestSessionQuery) await ctx.plugin(AgentLoop, { agents: [] }) diff --git a/packages/experimental/agent-team/tests/team.spec.ts b/packages/experimental/agent-team/tests/team.spec.ts index 321192432f..c48190f421 100644 --- a/packages/experimental/agent-team/tests/team.spec.ts +++ b/packages/experimental/agent-team/tests/team.spec.ts @@ -8,7 +8,6 @@ import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import { createUserMessage } from '@deepseek-ai/dsh-llm' import { SessionLogOffset, SessionId, type Session, type SessionEvent } from '@deepseek-ai/dsh-session' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl' import SubagentService from '@deepseek-ai/dsh-subagent' import { deliverSubagentPrompt, type HostPromptDeliverer } from '@deepseek-ai/dsh-subagent/internal' @@ -62,7 +61,6 @@ async function setup( ) { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) const storageRoot = mkdtempSync(join(tmpdir(), 'dsh-team-')) roots.push(storageRoot) await ctx.plugin(JsonlSessionPersistence, { root: storageRoot }) @@ -170,7 +168,6 @@ describe('Team identity and provisioning', () => { it('supports direct-constructor defaults and recovers roots that already exist', async () => { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) const storageRoot = mkdtempSync(join(tmpdir(), 'dsh-team-direct-')) roots.push(storageRoot) await ctx.plugin(JsonlSessionPersistence, { root: storageRoot }) @@ -1411,7 +1408,6 @@ describe('Team mailbox and waiting', () => { it('waits for one change, supports cancellation, times out, and releases waiters on HMR disposal', async () => { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) const storageRoot = mkdtempSync(join(tmpdir(), 'dsh-team-wait-')) roots.push(storageRoot) await ctx.plugin(JsonlSessionPersistence, { root: storageRoot }) diff --git a/packages/experimental/tool-agent-team/tests/tool-team.spec.ts b/packages/experimental/tool-agent-team/tests/tool-team.spec.ts index dffad3b4d7..6ae2fddca3 100644 --- a/packages/experimental/tool-agent-team/tests/tool-team.spec.ts +++ b/packages/experimental/tool-agent-team/tests/tool-team.spec.ts @@ -9,7 +9,6 @@ import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-test import { ToolCallId } from '@deepseek-ai/dsh-llm' import { scopeOf } from '@deepseek-ai/dsh-scope' import { SessionId } from '@deepseek-ai/dsh-session' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl' import SessionQueryEngine from '@deepseek-ai/dsh-session-query' import SubagentService from '@deepseek-ai/dsh-subagent' @@ -56,7 +55,6 @@ afterEach(() => { async function setup(script: ConstructorParameters[0], legacyControl = false) { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) const storageRoot = mkdtempSync(join(tmpdir(), 'dsh-tool-team-')) roots.push(storageRoot) await ctx.plugin(JsonlSessionPersistence, { root: storageRoot }) diff --git a/packages/feedback/command-feedback/package.json b/packages/feedback/command-feedback/package.json index 7cbe0ee6cb..d93e2fdb5a 100644 --- a/packages/feedback/command-feedback/package.json +++ b/packages/feedback/command-feedback/package.json @@ -36,6 +36,7 @@ "@deepseek-ai/cordis-plugin-include": "workspace:^", "@deepseek-ai/cordis-plugin-loader": "workspace:^", "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-commands": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", diff --git a/packages/feedback/command-feedback/tests/command-feedback.spec.ts b/packages/feedback/command-feedback/tests/command-feedback.spec.ts index 71aa288cc5..e59020bfc6 100644 --- a/packages/feedback/command-feedback/tests/command-feedback.spec.ts +++ b/packages/feedback/command-feedback/tests/command-feedback.spec.ts @@ -1,11 +1,12 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import { Context } from '@deepseek-ai/cordis' import Loader from '@deepseek-ai/cordis-plugin-loader' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent, AgentStatus } from '@deepseek-ai/dsh-agent' import CommandRuntime from '@deepseek-ai/dsh-commands' import SessionStore, { foldSurface, Session, SessionId } from '@deepseek-ai/dsh-session' import * as commandFeedback from '@deepseek-ai/dsh-command-feedback' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' const { USER_ID, getOrCreateAnonymousUserId } = vi.hoisted(() => { const USER_ID = '01234567-89ab-4cde-8f01-23456789abcd' @@ -28,13 +29,12 @@ interface Harness { /** Build a live idle agent over a store-owned session, as an app's spine does. */ function stubAgent(ctx: Context, id: string): { agent: Agent; session: Session } { const session = ctx.sessions.create(SessionId(id)) - const inbox = new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }) let status: AgentStatus = 'idle' const agent: Agent = { id: session.id, options: {}, session, - inbox, + inbox: unsupportedInbox(), ctx: new Context(), get status() { return status }, send: () => {}, diff --git a/packages/feedback/command-feedback/tests/loader-composition.spec.ts b/packages/feedback/command-feedback/tests/loader-composition.spec.ts index 1ad8abf770..6ab826e516 100644 --- a/packages/feedback/command-feedback/tests/loader-composition.spec.ts +++ b/packages/feedback/command-feedback/tests/loader-composition.spec.ts @@ -6,12 +6,13 @@ import { afterEach, describe, expect, it, vi } from 'vitest' import { Context } from '@deepseek-ai/cordis' import Loader from '@deepseek-ai/cordis-plugin-loader' import Include from '@deepseek-ai/cordis-plugin-include' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent, AgentStatus } from '@deepseek-ai/dsh-agent' import CommandRuntime from '@deepseek-ai/dsh-commands' import SessionStore, { SessionId } from '@deepseek-ai/dsh-session' import * as CommandFeedback from '@deepseek-ai/dsh-command-feedback' import { getOrCreateAnonymousUserId } from '@deepseek-ai/dsh-anonymous-user-id' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' let root: string | undefined let context: Context | undefined @@ -29,13 +30,12 @@ function agent(ctx: Context): Agent { const scope = ctx.plugin(() => {}) const id = SessionId('feedback-loader-agent') const session = ctx.sessions.create(id) - const inbox = new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }) let status: AgentStatus = 'idle' const value: Agent = { id, options: {}, session, - inbox, + inbox: unsupportedInbox(), ctx: scope.ctx, get status() { return status }, send: () => {}, diff --git a/packages/fs/tool-fs/tests/harness.ts b/packages/fs/tool-fs/tests/harness.ts index 184e74f099..b556ec9270 100644 --- a/packages/fs/tool-fs/tests/harness.ts +++ b/packages/fs/tool-fs/tests/harness.ts @@ -1,7 +1,6 @@ import { Context } from '@deepseek-ai/cordis' import type { Agent } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import LocalFileSystem from '@deepseek-ai/dsh-fs-local' import * as FsPolicy from '@deepseek-ai/dsh-fs-observation-policy' @@ -15,7 +14,6 @@ import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek' */ export async function fsHarness(fsCwd: string, persona = ''): Promise { const ctx = new Context() - await ctx.plugin(SessionProjectionRegistry) await mountAgentLoopTestDependencies(ctx, { systemPrompt: { personaPrefix: persona } }) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(LlmDeepSeek) diff --git a/packages/fs/tool-str-replace-editor/package.json b/packages/fs/tool-str-replace-editor/package.json index 8cfa3da728..f2e438074b 100644 --- a/packages/fs/tool-str-replace-editor/package.json +++ b/packages/fs/tool-str-replace-editor/package.json @@ -37,6 +37,7 @@ }, "devDependencies": { "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-fs": "workspace:^", "@deepseek-ai/dsh-fs-local": "workspace:^", "@deepseek-ai/dsh-fs-observation-policy": "workspace:^", diff --git a/packages/fs/tool-str-replace-editor/tests/tools.spec.ts b/packages/fs/tool-str-replace-editor/tests/tools.spec.ts index 7d32b26d72..e50f87f3a3 100644 --- a/packages/fs/tool-str-replace-editor/tests/tools.spec.ts +++ b/packages/fs/tool-str-replace-editor/tests/tools.spec.ts @@ -6,7 +6,7 @@ import { Context } from '@deepseek-ai/cordis' import { FsVersion } from '@deepseek-ai/dsh-fs' import { ToolCallId } from '@deepseek-ai/dsh-llm' import { SESSION_FORMAT_VERSION, Session, SessionId } from '@deepseek-ai/dsh-session' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import LocalFileSystem from '@deepseek-ai/dsh-fs-local' import * as FsPolicy from '@deepseek-ai/dsh-fs-observation-policy' @@ -16,6 +16,7 @@ import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRuntime from '@deepseek-ai/dsh-tools' import * as ToolStrReplaceEditor from '@deepseek-ai/dsh-tool-str-replace-editor' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' const contexts: Context[] = [] const roots: string[] = [] @@ -36,7 +37,7 @@ function agent(ctx: Context, cwd: string): Agent { id, options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: unsupportedInbox(), status: 'idle', ctx: scope.ctx, send: () => {}, diff --git a/packages/goal/command-goal/package.json b/packages/goal/command-goal/package.json index 28b99bf4b2..8f4439dbb8 100644 --- a/packages/goal/command-goal/package.json +++ b/packages/goal/command-goal/package.json @@ -35,6 +35,8 @@ "devDependencies": { "@deepseek-ai/cordis-plugin-loader": "workspace:^", "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-commands": "workspace:^", "@deepseek-ai/dsh-goal": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", diff --git a/packages/goal/command-goal/tests/command-goal.spec.ts b/packages/goal/command-goal/tests/command-goal.spec.ts index ed53189f7d..1c1761cf74 100644 --- a/packages/goal/command-goal/tests/command-goal.spec.ts +++ b/packages/goal/command-goal/tests/command-goal.spec.ts @@ -1,7 +1,7 @@ import { describe, expect, it, vi } from 'vitest' import { Context } from '@deepseek-ai/cordis' import Loader from '@deepseek-ai/cordis-plugin-loader' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent, AgentStatus } from '@deepseek-ai/dsh-agent' import CommandRuntime from '@deepseek-ai/dsh-commands' import GoalService from '@deepseek-ai/dsh-goal' @@ -9,6 +9,7 @@ import type { GoalRef } from '@deepseek-ai/dsh-goal' import SessionStore, { Session, SessionId, type SessionEvent } from '@deepseek-ai/dsh-session' import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import * as commandGoal from '@deepseek-ai/dsh-command-goal' +import { createInboxStub } from '@deepseek-ai/dsh-agent-loop-testkit' interface Harness { readonly ctx: Context @@ -21,7 +22,7 @@ interface Harness { function stubAgent(ctx: Context, id: string): { agent: Agent; session: Session } { // Store-created: the command executor durably logs lifecycle events on it. const session = ctx.sessions.create(SessionId(id)) - const inbox = new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }) + const inbox = createInboxStub() let status: AgentStatus = 'idle' const agent: Agent = { id: session.id, @@ -33,7 +34,7 @@ function stubAgent(ctx: Context, id: string): { agent: Agent; session: Session } send: () => {}, followup: () => {}, steer: () => {}, - inject(input) { inbox.append('next-step', input) }, + inject(input) { this.inbox.append('next-step', input) }, cancel() { status = 'idle' }, runMaintenance: task => task(new AbortController().signal), whenIdle() { return Promise.resolve() }, @@ -45,9 +46,9 @@ function stubAgent(ctx: Context, id: string): { agent: Agent; session: Session } async function harness(): Promise { const ctx = new Context() await ctx.plugin(SessionStore) + await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(CommandRuntime) await ctx.plugin(AgentRegistry) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(GoalService) const plugin = await ctx.plugin(commandGoal) const { agent, session } = stubAgent(ctx, `command-goal-${Math.random()}`) diff --git a/packages/goal/goal-round-driver/tests/goal-round-driver.spec.ts b/packages/goal/goal-round-driver/tests/goal-round-driver.spec.ts index 5cef3e75de..2ec805d71b 100644 --- a/packages/goal/goal-round-driver/tests/goal-round-driver.spec.ts +++ b/packages/goal/goal-round-driver/tests/goal-round-driver.spec.ts @@ -10,7 +10,6 @@ import { createUserMessage, LlmAdapter, LlmError } from '@deepseek-ai/dsh-llm' import type { GenerateOptions, StreamChunk } from '@deepseek-ai/dsh-llm' import { SessionId } from '@deepseek-ai/dsh-session' import type { UserMessage } from '@deepseek-ai/dsh-session' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import * as goalSession from '../src/index.ts' type ScriptEntry = StreamChunk[] | Error | 'hang' | ((options: GenerateOptions) => StreamChunk[]) @@ -90,7 +89,6 @@ async function harness(script: ScriptEntry[]): Promise { const ctx = new Context() contexts.push(ctx) await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(GoalService) const driver = await ctx.plugin(goalSession) await ctx.plugin(AgentLoop, { agents: [] }) @@ -217,7 +215,6 @@ describe('same-session goal driving', () => { const ctx = new Context() contexts.push(ctx) await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(GoalService) await ctx.plugin(AgentLoop, { agents: [] }) const adapter = new ScriptedAdapter([textResponse('after resume')]) diff --git a/packages/goal/goal/package.json b/packages/goal/goal/package.json index 878fa2f6dc..6db9f2da2b 100644 --- a/packages/goal/goal/package.json +++ b/packages/goal/goal/package.json @@ -69,6 +69,8 @@ }, "devDependencies": { "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-bash-local": "workspace:^", "@deepseek-ai/dsh-session-projection": "workspace:^", "@deepseek-ai/dsh-brand": "workspace:^", diff --git a/packages/goal/goal/tests/goal.spec.ts b/packages/goal/goal/tests/goal.spec.ts index d72ac7c751..e3140e9231 100644 --- a/packages/goal/goal/tests/goal.spec.ts +++ b/packages/goal/goal/tests/goal.spec.ts @@ -1,6 +1,6 @@ import { describe, expect, it, vi } from 'vitest' import { Context } from '@deepseek-ai/cordis' -import AgentRegistry, { agentEvents, Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry, { agentEvents } from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import { createUserMessage, HarnessError } from '@deepseek-ai/dsh-llm' import SessionStore, { Session, SessionId, type UserMessage } from '@deepseek-ai/dsh-session' @@ -12,12 +12,19 @@ import GoalService, { foldGoal, } from '@deepseek-ai/dsh-goal' import type { GoalChangeMeta, GoalRef, GoalSnapshotChangeMeta } from '@deepseek-ai/dsh-goal' +import { createInboxStub } from '@deepseek-ai/dsh-agent-loop-testkit' interface StubAgent { agent: Agent session: Session } +const isolatedInboxCtx = new Context() +await isolatedInboxCtx.plugin(SessionStore) +await isolatedInboxCtx.plugin(SessionProjectionRegistry) +await isolatedInboxCtx.plugin(AgentRegistry) +const sessionStubs = new WeakMap() + /** Number the next balanced test-fixture turn. */ function nextTurn(session: Session): number { return session.snapshotEvents().reduce((max, event) => event.type === 'turn/start' ? Math.max(max, event.data.turn) : max, 0) + 1 @@ -25,46 +32,61 @@ function nextTurn(session: Session): number { /** Mirror the public Agent.inject contract for domain tests. */ function appendInjection(session: Session, input: UserMessage): void { - new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }).append('next-step', input) + stubAgentForSession(session).agent.inbox.append('next-step', input) } /** Build a registry-compatible agent around one concrete session. */ -function stubAgentForSession(session: Session): StubAgent { +function stubAgentForSession(session: Session, suppliedCtx?: Context): StubAgent { + const existing = sessionStubs.get(session) + if (existing !== undefined) return existing const id = session.id - const inbox = new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }) + const agentCtx = suppliedCtx ?? isolatedInboxCtx + if (suppliedCtx === undefined) { + agentCtx.sessions.enter(session) + } + const inbox = createInboxStub() const agent: Agent = { id, options: {}, session, inbox, - ctx: new Context(), + ctx: agentCtx, status: 'idle', send: () => {}, followup: () => {}, steer: () => {}, - inject(input) { inbox.append('next-step', input) }, + inject(input) { this.inbox.append('next-step', input) }, cancel() {}, runMaintenance: task => task(new AbortController().signal), whenIdle() { return Promise.resolve() }, } - return { + const stub = { agent, session, } + sessionStubs.set(session, stub) + return stub } /** Build a registry-compatible agent around a fresh session. */ -function stubAgent(rawId: string, seed?: readonly import('@deepseek-ai/dsh-session').SessionEvent[]): StubAgent { - return stubAgentForSession(Session.create(SessionId(rawId), seed)) +function stubAgent( + rawId: string, + seed?: readonly import('@deepseek-ai/dsh-session').SessionEvent[], + ctx?: Context, +): StubAgent { + const session = ctx === undefined + ? Session.create(SessionId(rawId), seed) + : ctx.sessions.create(SessionId(rawId), { ...(seed === undefined ? {} : { seed }) }) + return stubAgentForSession(session, ctx) } async function harness(config: { defaultMaxGoalRounds?: number } = {}) { const ctx = new Context() await ctx.plugin(SessionStore) - await ctx.plugin(AgentRegistry) await ctx.plugin(SessionProjectionRegistry) + await ctx.plugin(AgentRegistry) await ctx.plugin(GoalService, config) - const stub = stubAgentForSession(ctx.sessions.create(SessionId(`goal-test-${Math.random()}`))) + const stub = stubAgent(`goal-test-${Math.random()}`, undefined, ctx) ctx.agents.register(stub.agent) return { ctx, ...stub } } @@ -182,15 +204,15 @@ describe('GoalService creation and replay', () => { it('inherits the completed-turn goal prefix through SessionStore.fork with child activation disarmed', async () => { const ctx = new Context() await ctx.plugin(SessionStore) - await ctx.plugin(AgentRegistry) await ctx.plugin(SessionProjectionRegistry) + await ctx.plugin(AgentRegistry) await ctx.plugin(GoalService) - const parent = stubAgentForSession(ctx.sessions.create(SessionId('goal-fork-parent'))) + const parent = stubAgentForSession(ctx.sessions.create(SessionId('goal-fork-parent')), ctx) ctx.agents.register(parent.agent) const goal = ctx.goals.create(parent.agent, { objective: 'inherit through fork', maxGoalRounds: 5 }) appendRound(parent.session, goal, 1) - const child = stubAgentForSession(ctx.sessions.fork(parent.session)) + const child = stubAgentForSession(ctx.sessions.fork(parent.session), ctx) ctx.agents.register(child.agent) expect(ctx.goals.get(child.agent)).toMatchObject({ id: goal.id, @@ -252,7 +274,7 @@ describe('GoalService creation and replay', () => { const { ctx, agent } = await harness() // A same-id agent backed by a different session object — the live-instance // check must reject it even though the ids match. - const impostor = stubAgentForSession(Session.create(agent.id)).agent + const impostor = { ...agent, session: Session.create(agent.id) } as Agent expect(() => ctx.goals.get(impostor)).toThrow(expect.objectContaining({ code: 'GOAL_AGENT_NOT_LIVE' })) expect(() => ctx.goals.create(impostor, { objective: 'no' })).toThrow(expect.objectContaining({ code: 'GOAL_AGENT_NOT_LIVE', @@ -439,10 +461,10 @@ describe('GoalService mutations', () => { it('publishes a mutation consistently to a reentrant session observer', async () => { const ctx = new Context() await ctx.plugin(SessionStore) - await ctx.plugin(AgentRegistry) await ctx.plugin(SessionProjectionRegistry) + await ctx.plugin(AgentRegistry) await ctx.plugin(GoalService) - const stub = stubAgentForSession(ctx.sessions.create(SessionId('goal-reentrant-observer'))) + const stub = stubAgentForSession(ctx.sessions.create(SessionId('goal-reentrant-observer')), ctx) ctx.agents.register(stub.agent) let observed: ReturnType ctx.on('session/event', (session, event) => { @@ -501,6 +523,31 @@ describe('GoalService mutations', () => { }) }) + it('rejects a corrupt append while preserving the valid prefix', async () => { + const { ctx, agent, session } = await harness() + expect(ctx.goals.get(agent)).toBeUndefined() + const change: GoalSnapshotChangeMeta = { + kind: 'goal/change', + version: 1, + operation: 'create', + goal: { + id: GoalId('goal-valid-prefix'), + revision: 1, + objective: 'valid prefix', + phase: 'active', + maxGoalRounds: 4, + }, + roundsStarted: 0, + createdAt: 12, + updatedAt: 12, + } + session.append('goal/change', change) + expect(() => { + session.append('goal/change', { ...change, operation: 'edit', extra: true } as never) + }).toThrow('snapshot change must have exactly') + + expect(ctx.goals.get(agent)).toMatchObject({ id: change.goal.id, objective: 'valid prefix' }) + }) }) describe('goal replay validation', () => { @@ -566,7 +613,7 @@ describe('goal replay validation', () => { content: [{ type: 'text', text: 'unrelated pending context' }], source: { kind: 'plugin', plugin: 'test' }, }) - const inbox = new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }) + const inbox = stubAgentForSession(session).agent.inbox inbox.append('next-step', message) expect(inbox.remove(message.id)).toBe(true) expect(foldGoal(session.snapshotEvents())).toMatchObject({ goal: { id: change.goal.id, revision: 1 } }) diff --git a/packages/goal/goal/tests/projection.spec.ts b/packages/goal/goal/tests/projection.spec.ts index 4dd473db31..840141a1da 100644 --- a/packages/goal/goal/tests/projection.spec.ts +++ b/packages/goal/goal/tests/projection.spec.ts @@ -10,15 +10,15 @@ import { describe, expect, it, vi } from 'vitest' import { Context } from '@deepseek-ai/cordis' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent, AgentStatus } from '@deepseek-ai/dsh-agent' import { createUserMessage } from '@deepseek-ai/dsh-llm' -import type { UserMessage } from '@deepseek-ai/dsh-session' import SessionStore from '@deepseek-ai/dsh-session' import type { Session } from '@deepseek-ai/dsh-session' import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import GoalService, { GoalId, applyGoalProjection, foldGoal, goalProjectionDefinition } from '@deepseek-ai/dsh-goal' import type { GoalProjection, GoalProjectionState, GoalRef } from '@deepseek-ai/dsh-goal' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' interface Bench { ctx: Context @@ -31,20 +31,17 @@ interface Bench { /** Register a minimal registry-compatible live agent over a store session. */ function liveAgent(ctx: Context, session: Session): Agent { const status: AgentStatus = 'idle' - const inbox = new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }) const agent: Agent = { id: session.id, options: {}, session, - inbox, + inbox: unsupportedInbox(), ctx, get status() { return status }, send: () => {}, followup: () => {}, steer: () => ({ outcome: Promise.resolve({ status: 'rejected' as const }) }), - inject(input: UserMessage) { - inbox.append('next-step', input) - }, + inject: () => { throw new Error('goal projection tests do not inject model context') }, cancel() {}, runMaintenance: task => task(new AbortController().signal), whenIdle() { return Promise.resolve() }, @@ -82,7 +79,7 @@ describe('goal projection unit', () => { it('serves null before the first create', async () => { const bench = await harness(true) seedMessage(bench.session) - expect(bench.tailValues()).toEqual({ goal: null }) + expect(bench.tailValues().goal).toBeNull() expect(bench.tailAsOfSeq()).toBe(bench.session.seq - 1) }) @@ -130,10 +127,14 @@ describe('goal projection unit', () => { const created = bench.ctx.goals.create(bench.agent, { objective: 'stay cleared' }) bench.ctx.goals.clear(bench.agent, created) - bench.agent.inbox.prepend('next-step', createUserMessage({ - content: [{ type: 'text', text: 'unrelated pending context' }], - source: { kind: 'plugin', plugin: 'test' }, - })) + bench.session.append('agent/inbox/spliced', { + target: 'next-step', + start: 0, + inserted: [createUserMessage({ + content: [{ type: 'text', text: 'unrelated pending context' }], + source: { kind: 'plugin', plugin: 'test' }, + })], + }) expect(bench.tailValues().goal).toBeNull() expect(foldGoal(bench.session.snapshotEvents()).goal).toBeUndefined() @@ -240,7 +241,7 @@ describe('goal projection unit', () => { const bench = await harness(false) seedMessage(bench.session) const fiber = await bench.ctx.plugin(GoalService) - expect(bench.tailValues()).toEqual({ goal: null }) + expect(bench.tailValues().goal).toBeNull() await fiber.dispose() expect('goal' in (bench.tailValues() ?? {})).toBe(false) }) diff --git a/packages/goal/tool-goal/package.json b/packages/goal/tool-goal/package.json index ec03183b8f..5ba86bc1ac 100644 --- a/packages/goal/tool-goal/package.json +++ b/packages/goal/tool-goal/package.json @@ -42,6 +42,7 @@ "devDependencies": { "@deepseek-ai/cordis-plugin-loader": "workspace:^", "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-agent-loop": "workspace:^", "@deepseek-ai/dsh-goal": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", diff --git a/packages/goal/tool-goal/tests/tool-goal.spec.ts b/packages/goal/tool-goal/tests/tool-goal.spec.ts index 1839a9ecdf..533b6aa6c6 100644 --- a/packages/goal/tool-goal/tests/tool-goal.spec.ts +++ b/packages/goal/tool-goal/tests/tool-goal.spec.ts @@ -1,44 +1,58 @@ import { describe, expect, it } from 'vitest' import { Context } from '@deepseek-ai/cordis' import Loader from '@deepseek-ai/cordis-plugin-loader' -import AgentRegistry, { agentEvents, Inbox } from '@deepseek-ai/dsh-agent' -import type { Agent, AgentStatus } from '@deepseek-ai/dsh-agent' +import AgentRegistry, { agentEvents } from '@deepseek-ai/dsh-agent' +import type { Agent, AgentStatus, Inbox } from '@deepseek-ai/dsh-agent' import { turnBoundaryProjectionDefinition } from '@deepseek-ai/dsh-agent-loop' import GoalService, { GoalId } from '@deepseek-ai/dsh-goal' import type { GoalRef } from '@deepseek-ai/dsh-goal' import { createUserMessage, ToolCallId } from '@deepseek-ai/dsh-llm' import type { MessageSource } from '@deepseek-ai/dsh-llm' -import { +import SessionStore, { SESSION_FORMAT_VERSION, Session, SessionId, SessionLogOffset, } from '@deepseek-ai/dsh-session' +import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRuntime from '@deepseek-ai/dsh-tools' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import type { ToolExecutionResult } from '@deepseek-ai/dsh-tools' import * as toolGoal from '@deepseek-ai/dsh-tool-goal' +import { createInboxStub } from '@deepseek-ai/dsh-agent-loop-testkit' const testToolSignal = new AbortController().signal interface StubAgent { readonly agent: Agent readonly session: Session + readonly inbox: Inbox setStatus(status: AgentStatus): void } -/** Build one registry-compatible live agent whose injections enter the durable inbox. */ -function stubAgent(rawId: string, supplied?: Session): StubAgent { - const session = supplied ?? Session.create(SessionId(rawId)) +const isolatedInboxCtx = new Context() +await isolatedInboxCtx.plugin(SessionStore) +await isolatedInboxCtx.plugin(SessionProjectionRegistry) +await isolatedInboxCtx.plugin(AgentRegistry) + +/** Build one registry-compatible live agent whose injections enter its test Inbox. */ +function stubAgent(rawId: string, supplied?: Session, suppliedCtx?: Context): StubAgent { + const agentCtx = suppliedCtx ?? isolatedInboxCtx + const session = supplied ?? (suppliedCtx === undefined + ? agentCtx.sessions.create(SessionId(rawId)) + : suppliedCtx.sessions.create(SessionId(rawId))) + if (suppliedCtx === undefined) { + if (agentCtx.sessions.get(session.id) !== session) agentCtx.sessions.enter(session) + } + const inbox = createInboxStub() let status: AgentStatus = 'running' const agent: Agent = { id: session.id, options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox, get status() { return status }, - ctx: new Context(), + ctx: agentCtx, send: () => {}, followup: () => {}, steer: () => ({ outcome: Promise.resolve({ status: 'rejected' as const }) }), @@ -49,7 +63,7 @@ function stubAgent(rawId: string, supplied?: Session): StubAgent { runMaintenance: task => task(new AbortController().signal), whenIdle() { return Promise.resolve() }, } - return { agent, session, setStatus(value) { status = value } } + return { agent, session, inbox, setStatus(value) { status = value } } } /** Open one message-triggered turn with its accepted model-visible input. */ @@ -62,7 +76,7 @@ function openTurn(stub: StubAgent, source: MessageSource, text = 'prompt'): numb source, }) stub.agent.inbox.append('next-turn', message) - const claimed = stub.agent.inbox.claim('next-turn', turn) + const claimed = stub.inbox.splice('next-turn', 0, 1, []) if (claimed.length === 0) throw new Error('expected queued turn input') stub.session.append('turn/start', { turn }) for (const admitted of claimed) { @@ -78,14 +92,15 @@ function closeTurn(stub: StubAgent, turn: number): void { async function harness(config: toolGoal.Config = {}) { const ctx = new Context() + await ctx.plugin(SessionStore) + await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SystemPrompt) await ctx.plugin(AgentRegistry) await ctx.plugin(ToolRuntime) - await ctx.plugin(SessionProjectionRegistry) ctx.sessionProjections.register(turnBoundaryProjectionDefinition) await ctx.plugin(GoalService) const fiber = await ctx.plugin(toolGoal, config) - const root = stubAgent(`goal-tool-root-${Math.random()}`) + const root = stubAgent(`goal-tool-root-${Math.random()}`, undefined, ctx) ctx.agents.register(root.agent) return { ctx, fiber, root } } @@ -252,7 +267,7 @@ describe('goal tool execution authority', () => { openTurn(root, { kind: 'user' }) // A distinct agent object over root's exact session: same id, not the live // registered instance, so the executor must reject it. - const stale = stubAgent('goal-tool-stale', root.agent.session).agent + const stale = stubAgent('goal-tool-stale', root.agent.session, ctx).agent const staleResult = await execute(ctx, 'get_goal', {}, stale, stale) expect(staleResult.error?.info?.code).toBe('GOAL_TOOL_DRIVER_REQUIRED') diff --git a/packages/guard/repeat-tool-reminder/tests/repeat-tool-reminder.spec.ts b/packages/guard/repeat-tool-reminder/tests/repeat-tool-reminder.spec.ts index b8a32f2370..4c505d6cf0 100644 --- a/packages/guard/repeat-tool-reminder/tests/repeat-tool-reminder.spec.ts +++ b/packages/guard/repeat-tool-reminder/tests/repeat-tool-reminder.spec.ts @@ -6,7 +6,6 @@ import { defineContentToolFixture } from '@deepseek-ai/dsh-tools' import type { Agent } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import * as RepeatToolGuard from '@deepseek-ai/dsh-repeat-tool-reminder' import type { Config } from '@deepseek-ai/dsh-repeat-tool-reminder' import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' @@ -25,8 +24,6 @@ const testToolSignal = new AbortController().signal async function harness(config: Config = {}): Promise { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - // AgentLoop declares the registry as a required injection. - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(RepeatToolGuard, config) ctx.tools.register(defineContentToolFixture({ name: 'probe', description: 'p', parameters: {}, async execute() { return [{ type: 'text', text: 'ok' }] } })) @@ -373,7 +370,6 @@ describe('config validation fails loud', () => { async function spine(): Promise { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) return ctx } diff --git a/packages/hooks/hooks-claude-code/tests/bridge.spec.ts b/packages/hooks/hooks-claude-code/tests/bridge.spec.ts index 7f5facfa65..68f6c8319d 100644 --- a/packages/hooks/hooks-claude-code/tests/bridge.spec.ts +++ b/packages/hooks/hooks-claude-code/tests/bridge.spec.ts @@ -15,7 +15,6 @@ import LocalSubprocessRuntime from '@deepseek-ai/dsh-subprocess-local' import { scopeTarget } from '@deepseek-ai/dsh-scope' import SubagentRuntime, { SubagentRunId } from '@deepseek-ai/dsh-subagent' import * as HooksClaude from '@deepseek-ai/dsh-hooks-claude-code' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' /** @@ -58,7 +57,6 @@ async function harnessWithFiber( ): Promise<{ ctx: Context; hooks: Fiber }> { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(LocalSubprocessRuntime) await ctx.plugin(LocalBashExecutor, { timeoutMs: 10_000 }) @@ -356,7 +354,6 @@ describe('hooks-claude-code bridge — load resilience', () => { const adapter = new MockAdapter([textResponse('fine')]) const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(LocalSubprocessRuntime) await ctx.plugin(LocalBashExecutor, { timeoutMs: 10_000 }) @@ -417,7 +414,6 @@ describe('hooks-claude-code bridge — load resilience', () => { const adapter = new MockAdapter([textResponse('ok')]) const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(LocalSubprocessRuntime) await ctx.plugin(LocalBashExecutor, { timeoutMs: 10_000 }) diff --git a/packages/hooks/hooks-claude-code/tests/coverage-cases.ts b/packages/hooks/hooks-claude-code/tests/coverage-cases.ts index 0f90281364..8627a970cd 100644 --- a/packages/hooks/hooks-claude-code/tests/coverage-cases.ts +++ b/packages/hooks/hooks-claude-code/tests/coverage-cases.ts @@ -15,7 +15,6 @@ import LocalSubprocessRuntime from '@deepseek-ai/dsh-subprocess-local' import { scopeTarget } from '@deepseek-ai/dsh-scope' import SubagentRuntime, { SubagentRunId } from '@deepseek-ai/dsh-subagent' import * as HooksClaude from '@deepseek-ai/dsh-hooks-claude-code' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' const testToolSignal = new AbortController().signal @@ -42,7 +41,6 @@ type HarnessOpts = { pluginRoot?: string; projectDir?: string; stderrSummaryMaxC async function harness(configPath: string, adapter: MockAdapter, opts: HarnessOpts = {}): Promise { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) if (opts.sessionRoot !== undefined) await ctx.plugin(JsonlSessionPersistence, { root: opts.sessionRoot }) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(LocalSubprocessRuntime) @@ -363,7 +361,6 @@ export function defineCoverageCases(group: CoverageGroup): void { const adapter = new MockAdapter([textResponse('ok')]) const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(LocalSubprocessRuntime) await ctx.plugin(LocalBashExecutor, { timeoutMs: 10_000 }) @@ -663,7 +660,6 @@ export function defineCoverageCases(group: CoverageGroup): void { const adapter = new MockAdapter([toolCallResponse('c1', 'echo', {}), textResponse('done')]) const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) // Executor default cwd = serverDir (deliberately NOT the session cwd). await ctx.plugin(LocalSubprocessRuntime) @@ -693,7 +689,6 @@ export function defineCoverageCases(group: CoverageGroup): void { hooks(serverDir, { SubagentStop: [{ hooks: [{ type: 'command', command: 'cat > stoppayload.tmp; mv stoppayload.tmp stoppayload; pwd > stopwhere' }] }] }) const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) // Executor default cwd = serverDir (deliberately NOT the child session cwd). await ctx.plugin(LocalSubprocessRuntime) diff --git a/packages/hooks/hooks-codex/tests/bridge.spec.ts b/packages/hooks/hooks-codex/tests/bridge.spec.ts index 511ec5e12a..1ef87de7c1 100644 --- a/packages/hooks/hooks-codex/tests/bridge.spec.ts +++ b/packages/hooks/hooks-codex/tests/bridge.spec.ts @@ -13,7 +13,6 @@ import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-test import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local' import LocalSubprocessRuntime from '@deepseek-ai/dsh-subprocess-local' import * as HooksCodex from '@deepseek-ai/dsh-hooks-codex' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' /** @@ -43,7 +42,6 @@ function writeHooks(dir: string, hooks: unknown): void { async function harness(dir: string, adapter: MockAdapter, beforeHooks?: (ctx: Context) => void): Promise { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(LocalSubprocessRuntime) await ctx.plugin(LocalBashExecutor, { timeoutMs: 10_000 }) @@ -184,7 +182,6 @@ describe('hooks-codex bridge', () => { const adapter = new MockAdapter([textResponse('ok')]) const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(LocalSubprocessRuntime) await ctx.plugin(LocalBashExecutor, { timeoutMs: 10_000 }) @@ -208,7 +205,6 @@ describe('hooks-codex bridge', () => { writeHooks(dir, { SessionStart: [{ hooks: [{ type: 'command', command: slow }] }] }) const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(LocalSubprocessRuntime) await ctx.plugin(LocalBashExecutor, { timeoutMs: 10_000 }) diff --git a/packages/hooks/hooks-codex/tests/coverage-cases.ts b/packages/hooks/hooks-codex/tests/coverage-cases.ts index 85ab765f7e..12a3c370ab 100644 --- a/packages/hooks/hooks-codex/tests/coverage-cases.ts +++ b/packages/hooks/hooks-codex/tests/coverage-cases.ts @@ -13,7 +13,6 @@ import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-test import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local' import LocalSubprocessRuntime from '@deepseek-ai/dsh-subprocess-local' import * as HooksCodex from '@deepseek-ai/dsh-hooks-codex' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' const testToolSignal = new AbortController().signal @@ -32,7 +31,6 @@ type HarnessOpts = { stderrSummaryMaxChars?: number; sessionRoot?: string } async function harness(configPath: string, adapter: MockAdapter, opts: HarnessOpts = {}): Promise { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) if (opts.sessionRoot !== undefined) await ctx.plugin(JsonlSessionPersistence, { root: opts.sessionRoot }) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(LocalSubprocessRuntime) @@ -309,7 +307,6 @@ export function defineCoverageCases(groups: CoverageGroup | readonly CoverageGro const adapter = new MockAdapter([textResponse('ok')]) const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(LocalSubprocessRuntime) await ctx.plugin(LocalBashExecutor, { timeoutMs: 10_000 }) @@ -620,7 +617,6 @@ export function defineCoverageCases(groups: CoverageGroup | readonly CoverageGro const adapter = new MockAdapter([toolCallResponse('c1', 'Bash', { command: 'x' }), textResponse('done')]) const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(LocalSubprocessRuntime) await ctx.plugin(LocalBashExecutor, { timeoutMs: 10_000, cwd: serverDir }) diff --git a/packages/jobs/jobs-local/package.json b/packages/jobs/jobs-local/package.json index 7dee4cf159..4124f176fd 100644 --- a/packages/jobs/jobs-local/package.json +++ b/packages/jobs/jobs-local/package.json @@ -40,6 +40,7 @@ "@deepseek-ai/cordis-plugin-include": "workspace:^", "@deepseek-ai/cordis-plugin-loader": "workspace:^", "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-brand": "workspace:^", "@deepseek-ai/dsh-scope": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", diff --git a/packages/jobs/jobs-local/tests/jobs.spec.ts b/packages/jobs/jobs-local/tests/jobs.spec.ts index 33ea29ba68..14f4ecd847 100644 --- a/packages/jobs/jobs-local/tests/jobs.spec.ts +++ b/packages/jobs/jobs-local/tests/jobs.spec.ts @@ -1,13 +1,14 @@ import { describe, expect, expectTypeOf, it, vi } from 'vitest' import { Context } from '@deepseek-ai/cordis' import { Session, SessionId } from '@deepseek-ai/dsh-session' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import { bindScopeParent, createScope, scopeOf } from '@deepseek-ai/dsh-scope' import type { ScopeKey } from '@deepseek-ai/dsh-scope' import { JobId } from '@deepseek-ai/dsh-jobs' import type { JobHooks, JobKind, JobOutcome, JobSnapshot, JobStart } from '@deepseek-ai/dsh-jobs' import LocalJobRegistry, { type Config as JobsConfig } from '@deepseek-ai/dsh-jobs-local' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' declare module '@deepseek-ai/dsh-jobs' { interface JobKindMap { @@ -34,7 +35,7 @@ function stubAgent(ctx: Context, rawId: string, presetScope?: ScopeKey): Agent { id, options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: unsupportedInbox(), status: 'idle' as const, ctx: agentCtx, send: () => {}, @@ -44,7 +45,7 @@ function stubAgent(ctx: Context, rawId: string, presetScope?: ScopeKey): Agent { cancel() {}, runMaintenance: (job: (signal: AbortSignal) => Promise) => job(new AbortController().signal), whenIdle() { return Promise.resolve() }, - } + } satisfies Agent agentScopeDisposers.set(agent, async () => { await scopeFiber.dispose() }) return agent } diff --git a/packages/llm/llm-retry/package.json b/packages/llm/llm-retry/package.json index 6087c98f38..02ef57fda0 100644 --- a/packages/llm/llm-retry/package.json +++ b/packages/llm/llm-retry/package.json @@ -61,6 +61,7 @@ "@deepseek-ai/dsh-llm-deepseek": "workspace:^", "@deepseek-ai/dsh-llm-mock-server": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", + "@deepseek-ai/dsh-session-projection": "workspace:^", "@deepseek-ai/dsh-session-persistence-jsonl": "workspace:^", "@deepseek-ai/dsh-system-prompt": "workspace:^", "@deepseek-ai/dsh-timeout": "workspace:^", diff --git a/packages/llm/llm-retry/tests/transport-recovery.spec.ts b/packages/llm/llm-retry/tests/transport-recovery.spec.ts index fcf19574e3..ba218842de 100644 --- a/packages/llm/llm-retry/tests/transport-recovery.spec.ts +++ b/packages/llm/llm-retry/tests/transport-recovery.spec.ts @@ -11,7 +11,6 @@ import type { MockLlmBehavior, MockLlmServer } from '@deepseek-ai/dsh-llm-mock-s import { startMockLlmServer } from '@deepseek-ai/dsh-llm-mock-server' import { SessionId } from '@deepseek-ai/dsh-session' import type { SessionEvent } from '@deepseek-ai/dsh-session' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import * as Retry from '../src/index.ts' let context: Context | undefined @@ -39,7 +38,6 @@ async function harness( vi.stubEnv('DEEPSEEK_API_KEY', 'mock-key') const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(LlmDeepSeek, { baseURL, streamIdleTimeoutMs: options.streamIdleTimeoutMs ?? 1_000, diff --git a/packages/plan/plan-mode/tests/integration.spec.ts b/packages/plan/plan-mode/tests/integration.spec.ts index 5e4240e05b..a814b5d9af 100644 --- a/packages/plan/plan-mode/tests/integration.spec.ts +++ b/packages/plan/plan-mode/tests/integration.spec.ts @@ -2,11 +2,11 @@ import { describe, expect, it } from 'vitest' import { Context } from '@deepseek-ai/cordis' import LlmRuntime, { createUserMessage, type StreamChunk } from '@deepseek-ai/dsh-llm' import SessionStore, { SessionId, type SessionEvent } from '@deepseek-ai/dsh-session' +import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRuntime, { defineContentToolFixture } from '@deepseek-ai/dsh-tools' import AgentRegistry, { type Agent } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import PlanModeController from '@deepseek-ai/dsh-plan-mode' import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' diff --git a/packages/plan/plan-mode/tests/projection.spec.ts b/packages/plan/plan-mode/tests/projection.spec.ts index 24ba27c41d..f4d46d5b62 100644 --- a/packages/plan/plan-mode/tests/projection.spec.ts +++ b/packages/plan/plan-mode/tests/projection.spec.ts @@ -64,7 +64,7 @@ function commitPlanMode(session: Session, active: boolean, turn: number): void { describe('plan projection unit', () => { it('serves inactive/not-pending for the empty log', async () => { const bench = await harness(true) - expect(bench.values()).toEqual({ plan: { active: false, pending: false } }) + expect(bench.values().plan).toEqual({ active: false, pending: false }) }) it('a logged /plan selection reads pending until plan/mode records it', async () => { diff --git a/packages/preset/agent-presets/tests/invariant.spec.ts b/packages/preset/agent-presets/tests/invariant.spec.ts index aebccc9b7f..e1fb150b5c 100644 --- a/packages/preset/agent-presets/tests/invariant.spec.ts +++ b/packages/preset/agent-presets/tests/invariant.spec.ts @@ -5,9 +5,9 @@ import Loader from '@deepseek-ai/cordis-plugin-loader' import Include from '@deepseek-ai/cordis-plugin-include' import LlmRuntime from '@deepseek-ai/dsh-llm' import SessionStore, { SessionId } from '@deepseek-ai/dsh-session' +import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRuntime from '@deepseek-ai/dsh-tools' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import AgentRegistry, { assembleContextFor } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import InvariantRegistry from '@deepseek-ai/dsh-invariants' @@ -28,10 +28,10 @@ async function harness(roster: Partial = {}): Promise { ctx.loader.builtins.include = Include await ctx.plugin(LlmRuntime) await ctx.plugin(SessionStore) + await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SystemPrompt, { personaPrefix: '' }) await ctx.plugin(ToolRuntime) await ctx.plugin(AgentRegistry) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(AgentPresets, { default: 'standard', roots: ROOTS, includeShippedRoot: false, includeUserRoot: false, ...roster }) await ctx.plugin(InvariantRegistry) diff --git a/packages/preset/agent-presets/tests/mount.spec.ts b/packages/preset/agent-presets/tests/mount.spec.ts index 664e4c1c02..6c43c2192c 100644 --- a/packages/preset/agent-presets/tests/mount.spec.ts +++ b/packages/preset/agent-presets/tests/mount.spec.ts @@ -8,9 +8,9 @@ import Include from '@deepseek-ai/cordis-plugin-include' import Group from '@deepseek-ai/cordis-plugin-group' import LlmRuntime from '@deepseek-ai/dsh-llm' import SessionStore, { SessionId } from '@deepseek-ai/dsh-session' +import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRuntime from '@deepseek-ai/dsh-tools' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import AgentRegistry, { assembleContextFor, type Agent } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' @@ -51,10 +51,10 @@ async function harness(roster: Config = { default: 'standard', roots: ROOTS, inc ctx.loader.builtins.group = Group await ctx.plugin(LlmRuntime) await ctx.plugin(SessionStore) + await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SystemPrompt, { personaPrefix: '' }) await ctx.plugin(ToolRuntime) await ctx.plugin(AgentRegistry) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(AgentPresets, roster) return ctx @@ -459,10 +459,10 @@ describe('the preset file is an input, never a persistence target', () => { scoped.loader.builtins.group = Group await scoped.plugin(LlmRuntime) await scoped.plugin(SessionStore) + await scoped.plugin(SessionProjectionRegistry) await scoped.plugin(SystemPrompt, { personaPrefix: '' }) await scoped.plugin(ToolRuntime) await scoped.plugin(AgentRegistry) - await scoped.plugin(SessionProjectionRegistry) await scoped.plugin(AgentLoop, { agents: [] }) await scoped.plugin(AgentPresets, { default: 'self-disposing', roots: [{ path: root, trust: 'user' as const }], includeShippedRoot: false, includeUserRoot: false }) @@ -648,10 +648,10 @@ describe('replacing a composition', () => { scoped.loader.builtins.group = Group await scoped.plugin(LlmRuntime) await scoped.plugin(SessionStore) + await scoped.plugin(SessionProjectionRegistry) await scoped.plugin(SystemPrompt, { personaPrefix: '' }) await scoped.plugin(ToolRuntime) await scoped.plugin(AgentRegistry) - await scoped.plugin(SessionProjectionRegistry) await scoped.plugin(AgentLoop, { agents: [] }) await scoped.plugin(AgentPresets, { default: 'first', roots: [{ path: root, trust: 'user' as const }], includeShippedRoot: false, includeUserRoot: false }) const handle = await scoped.agents.create({ diff --git a/packages/preset/agent-presets/tests/remote.spec.ts b/packages/preset/agent-presets/tests/remote.spec.ts index b7511cdbab..b1a685d019 100644 --- a/packages/preset/agent-presets/tests/remote.spec.ts +++ b/packages/preset/agent-presets/tests/remote.spec.ts @@ -79,10 +79,10 @@ async function harness( ctx.loader.builtins.include = Include await ctx.plugin(LlmRuntime) await ctx.plugin(SessionStore) + await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SystemPrompt, { personaPrefix: '' }) await ctx.plugin(ToolRuntime) await ctx.plugin(AgentRegistry) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(AgentPresets, roster) return ctx diff --git a/packages/schedule/schedule/tests/jsonl-restart.spec.ts b/packages/schedule/schedule/tests/jsonl-restart.spec.ts index dccc95d137..baf02e4702 100644 --- a/packages/schedule/schedule/tests/jsonl-restart.spec.ts +++ b/packages/schedule/schedule/tests/jsonl-restart.spec.ts @@ -7,7 +7,6 @@ import { afterEach, describe, expect, it } from 'vitest' import { Context } from '@deepseek-ai/cordis' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { LlmAdapter, type GenerateOptions, type StreamChunk } from '@deepseek-ai/dsh-llm' import SessionStore, { SessionId } from '@deepseek-ai/dsh-session' import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl' @@ -52,7 +51,6 @@ async function mountRuntime(root: string, adapter: RecordingAdapter): Promise { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(PersistenceProbe) ctx.on('session/flush', () => {}) await ctx.plugin(AgentLoop, { agents: [] }) diff --git a/packages/schedule/schedule/tests/runtime.spec.ts b/packages/schedule/schedule/tests/runtime.spec.ts index 86b8246c32..f216babd7d 100644 --- a/packages/schedule/schedule/tests/runtime.spec.ts +++ b/packages/schedule/schedule/tests/runtime.spec.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { Context } from '@deepseek-ai/cordis' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent, AgentCancelCause, InboxTarget } from '@deepseek-ai/dsh-agent' import type { UserMessage } from '@deepseek-ai/dsh-llm' import SessionStore, { SessionId } from '@deepseek-ai/dsh-session' @@ -11,6 +11,7 @@ import { foldScheduleEvents, } from '../src/domain.ts' import { MAX_TIMER_DELAY_MS, ScheduleRuntime } from '../src/runtime.ts' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' const contexts: Context[] = [] const runtimes: ScheduleRuntime[] = [] @@ -57,12 +58,11 @@ async function harness(): Promise { onFollowup: undefined as (() => void) | undefined, idle: Promise.withResolvers(), } - const inbox = new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }) const agent: Agent = { id: session.id, options: {}, session, - inbox, + inbox: unsupportedInbox(), status: 'idle', ctx: new Context(), send(_message: UserMessage, _target: InboxTarget, _wakeup: boolean) {}, diff --git a/packages/schedule/schedule/tests/tools.spec.ts b/packages/schedule/schedule/tests/tools.spec.ts index 4ac5d91717..35e4dc1724 100644 --- a/packages/schedule/schedule/tests/tools.spec.ts +++ b/packages/schedule/schedule/tests/tools.spec.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { Context } from '@deepseek-ai/cordis' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent, AgentCancelCause, InboxTarget } from '@deepseek-ai/dsh-agent' import { ToolCallId } from '@deepseek-ai/dsh-llm' import type { UserMessage } from '@deepseek-ai/dsh-llm' @@ -10,6 +10,7 @@ import ToolRuntime from '@deepseek-ai/dsh-tools' import type { ToolExecutionResult } from '@deepseek-ai/dsh-tools' import { registerScheduleTools } from '../src/tools.ts' import { runScheduleTransaction } from '../src/transaction.ts' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' const signal = new AbortController().signal const contexts: Context[] = [] @@ -24,12 +25,11 @@ interface ToolHarness { function stubAgent(ctx: Context, id: string): Agent { const session = ctx.sessions.create(SessionId(id)) - const inbox = new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }) - return { + const agent: Agent = { id: session.id, options: {}, session, - inbox, + inbox: unsupportedInbox(), status: 'idle', ctx: new Context(), send(_message: UserMessage, _target: InboxTarget, _wakeup: boolean) {}, @@ -40,6 +40,7 @@ function stubAgent(ctx: Context, id: string): Agent { steer(_message: UserMessage) {}, inject(_message: UserMessage) {}, } + return agent } async function harness(withPersistence = true): Promise { diff --git a/packages/sdk/server/tests/built-scope-carrier.e2e.ts b/packages/sdk/server/tests/built-scope-carrier.e2e.ts index 803198ab32..e55bf76bb2 100644 --- a/packages/sdk/server/tests/built-scope-carrier.e2e.ts +++ b/packages/sdk/server/tests/built-scope-carrier.e2e.ts @@ -28,7 +28,6 @@ const [ { Context }, { default: AgentLoop }, { mountAgentLoopTestDependencies }, - { default: SessionProjectionRegistry }, { default: SubagentRuntime }, { default: JsonlSessionPersistence }, { HarnessSdkJsonRpcServer }, @@ -37,7 +36,6 @@ const [ load("vendor/cordis/lib/index.js"), load("packages/core/agent-loop/lib/index.js"), load("packages/test-support/agent-loop-testkit/lib/index.js"), - load("packages/session/session-projection/lib/index.js"), load("packages/subagent/subagent/lib/index.js"), load("packages/session/session-persistence-jsonl/lib/index.js"), load("packages/sdk/server/lib/index.js"), @@ -48,7 +46,6 @@ const storageRoot = await mkdtemp(join(tmpdir(), "jsonrpc-built-scope-")); const ctx = new Context(); try { await mountAgentLoopTestDependencies(ctx); - await ctx.plugin(SessionProjectionRegistry); await ctx.plugin(AgentLoop, { agents: [] }); await ctx.plugin(SubagentRuntime); await ctx.plugin(JsonlSessionPersistence, { root: storageRoot }); diff --git a/packages/sdk/server/tests/plugin-apply.spec.ts b/packages/sdk/server/tests/plugin-apply.spec.ts index 8190a25eb3..e51324c019 100644 --- a/packages/sdk/server/tests/plugin-apply.spec.ts +++ b/packages/sdk/server/tests/plugin-apply.spec.ts @@ -11,7 +11,6 @@ import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import { LlmAdapter } from '@deepseek-ai/dsh-llm' import type { GenerateOptions, StreamChunk } from '@deepseek-ai/dsh-llm' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl' import * as jsonrpc from '../src/index.ts' @@ -77,7 +76,6 @@ async function mountPlugin( ): Promise { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(JsonlSessionPersistence, { root: storageDir }) await new Promise(resolve => setTimeout(resolve, 50)) diff --git a/packages/sdk/server/tests/server.spec.ts b/packages/sdk/server/tests/server.spec.ts index ac3af4f47b..d0bb7e1c8d 100644 --- a/packages/sdk/server/tests/server.spec.ts +++ b/packages/sdk/server/tests/server.spec.ts @@ -12,7 +12,6 @@ import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import SessionStore, { SessionId } from '@deepseek-ai/dsh-session' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl' import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek' import SubagentRuntime, { type SubagentResult, type SubagentRunEndInfo } from '@deepseek-ai/dsh-subagent' @@ -65,7 +64,6 @@ async function mockCompletionServer(): Promise<{ url: string; requests: unknown[ async function makeHarness(storageDir: string) { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(SubagentRuntime) await ctx.plugin(JsonlSessionPersistence, { root: storageDir }) diff --git a/packages/session/session-checkpoint-policy/tests/fixtures/crash-child.ts b/packages/session/session-checkpoint-policy/tests/fixtures/crash-child.ts index 37caecb8dc..83826090dd 100644 --- a/packages/session/session-checkpoint-policy/tests/fixtures/crash-child.ts +++ b/packages/session/session-checkpoint-policy/tests/fixtures/crash-child.ts @@ -2,7 +2,6 @@ import { writeFile } from 'node:fs/promises' import { Context } from '@deepseek-ai/cordis' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { createUserMessage, ToolCallId, type GenerateOptions, LlmAdapter, type StreamChunk } from '@deepseek-ai/dsh-llm' import { SessionId } from '@deepseek-ai/dsh-session' import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl' @@ -38,7 +37,6 @@ class CrashAdapter extends LlmAdapter { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) -await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(JsonlSessionPersistence, { root: persistenceRoot, compression: 'none' }) await ctx.plugin(checkpointPolicy) diff --git a/packages/session/session-projection/README.i18n.yaml b/packages/session/session-projection/README.i18n.yaml index b740065e58..1762a3a480 100644 --- a/packages/session/session-projection/README.i18n.yaml +++ b/packages/session/session-projection/README.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 packages/session/session-projection/README.md -README.md: 79902cca815da1ee93916ece82cf89bb3624a3a8 -README.zh.md: 749934419cfc94a83abcb59013776fbb8b55ac19 +README.md: a837f41db31dd7db5abf721acfcb7970950056ef +README.zh.md: fa4cdf32f5b2a9ce4944502363cca5788609372f diff --git a/packages/session/session-projection/README.md b/packages/session/session-projection/README.md index 79902cca81..a837f41db3 100644 --- a/packages/session/session-projection/README.md +++ b/packages/session/session-projection/README.md @@ -55,13 +55,15 @@ const definition = { ### Register and read -`register(definition)` installs the unit; the registration is an effect on the calling fiber, so unloading the domain removes its key. Carriers read a consistent synchronous cut over every client-visible unit with `snapshot(session)` — `{ asOfSeq, values }`, where `asOfSeq` is the seq of the last event every value reflects — and subscribe to per-change notifications with `onChanged(listener)`. `stateOf(session, key)` reads one unit's host state without computing unrelated views. +`register(definition)` installs the unit; registrants with the same key and `stateVersion` share its cells, while an incompatible version or invalid `stateVersion` throws. Registration is an effect on the calling fiber, so the last unload removes the key and its cached cells. Carriers read a consistent synchronous cut over every client-visible unit with `snapshot(session)` — `{ asOfSeq, values }`, where `asOfSeq` is the seq of the last event every value reflects — and subscribe to per-change notifications with `onChanged(listener)`. `stateOf(session, key)` reads one unit's live read-only host state without computing unrelated views. ```text const dispose = ctx.sessionProjections.register(definition) const { asOfSeq, values } = ctx.sessionProjections.snapshot(session) ``` +A domain that requires projected state declares `sessionProjections` as a Cordis service dependency; optional contributors may register under `ctx.inject(['sessionProjections'], …)`. Carriers use `ctx.get('sessionProjections')` and omit their block or frames when the registry is absent. + ### Persisted checkpoints Every unit's state is checkpointed — client-visible and host-only alike — through `checkpoint(session)`, and the sibling [session-projection-cache](../session-projection-cache/README.md) persists those checkpoints so cold reads skip full log loads. Checkpoint watermarks use `SessionSeqCursor` (`-1` for an empty log), while replay starts use `SessionLogOffset`; `restoreFloor` and `restore` implement the read recipe without conflating an existing event with a log gap. diff --git a/packages/session/session-projection/README.zh.md b/packages/session/session-projection/README.zh.md index 749934419c..fa4cdf32f5 100644 --- a/packages/session/session-projection/README.zh.md +++ b/packages/session/session-projection/README.zh.md @@ -55,13 +55,15 @@ const definition = { ### 注册与读取 -`register(definition)` 安装单元;注册是挂在调用方 fiber 上的 effect,因此卸载领域即移除其 key。载体用 `snapshot(session)` 对每个客户端可见单元读取一致的同步切面——`{ asOfSeq, values }`,其中 `asOfSeq` 是所有值共同反映到的最后一个事件的 seq——并用 `onChanged(listener)` 订阅逐变更通知。`stateOf(session, key)` 读取一个单元的主机状态,不计算无关视图。 +`register(definition)` 安装单元;具有相同 key 和 `stateVersion` 的注册方共享其 cell,版本不兼容或 `stateVersion` 非法时会 throw。注册是挂在调用方 fiber 上的 effect,因此最后一个注册方卸载后会移除 key 及其缓存 cell。载体用 `snapshot(session)` 对每个客户端可见单元读取一致的同步切面——`{ asOfSeq, values }`,其中 `asOfSeq` 是所有值共同反映到的最后一个事件的 seq——并用 `onChanged(listener)` 订阅逐变更通知。`stateOf(session, key)` 读取一个单元的实时只读 host 状态,不计算无关视图。 ```text const dispose = ctx.sessionProjections.register(definition) const { asOfSeq, values } = ctx.sessionProjections.snapshot(session) ``` +必须使用投影状态的领域把 `sessionProjections` 声明为 Cordis 服务依赖;可选贡献方可以在 `ctx.inject(['sessionProjections'], …)` 下注册。载体使用 `ctx.get('sessionProjections')`,注册表缺席时省略自己的块或帧。 + ### 持久检查点 每个单元的状态都会被检查点化——client-visible 与 host-only 一视同仁——通过 `checkpoint(session)`,同级包 [session-projection-cache](../session-projection-cache/README.zh.md) 持久化这些检查点,使冷读跳过全量日志加载。检查点水位使用 `SessionSeqCursor`(空日志为 `-1`),回放起点使用 `SessionLogOffset`;`restoreFloor` 与 `restore` 在无活动会话的情况下实现读取配方,且不会混淆已有事件与日志间隙。 diff --git a/packages/shell/tool-bash-persistent/package.json b/packages/shell/tool-bash-persistent/package.json index f341afd520..8eae8597a3 100644 --- a/packages/shell/tool-bash-persistent/package.json +++ b/packages/shell/tool-bash-persistent/package.json @@ -39,6 +39,7 @@ "@deepseek-ai/cordis-plugin-include": "workspace:^", "@deepseek-ai/cordis-plugin-loader": "workspace:^", "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", "@deepseek-ai/dsh-terminal": "workspace:^", "@deepseek-ai/dsh-terminal-bash": "workspace:^", diff --git a/packages/shell/tool-bash-persistent/tests/loader-composition.spec.ts b/packages/shell/tool-bash-persistent/tests/loader-composition.spec.ts index a9f4d613ca..82895a008c 100644 --- a/packages/shell/tool-bash-persistent/tests/loader-composition.spec.ts +++ b/packages/shell/tool-bash-persistent/tests/loader-composition.spec.ts @@ -8,7 +8,7 @@ import Loader from '@deepseek-ai/cordis-plugin-loader' import Include from '@deepseek-ai/cordis-plugin-include' import { ToolCallId } from '@deepseek-ai/dsh-llm' import { SESSION_FORMAT_VERSION, Session, SessionId } from '@deepseek-ai/dsh-session' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import TerminalSessionService from '@deepseek-ai/dsh-terminal' import * as TerminalLocal from '@deepseek-ai/dsh-terminal-bash' @@ -20,6 +20,7 @@ import LocalSubprocessRuntime from '@deepseek-ai/dsh-subprocess-local' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRuntime from '@deepseek-ai/dsh-tools' import * as ToolBashPersistent from '@deepseek-ai/dsh-tool-bash-persistent' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' let root: string | undefined let context: Context | undefined @@ -47,7 +48,7 @@ function agent(ctx: Context, cwd: string): Agent { id, options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: unsupportedInbox(), status: 'idle', ctx: scope.ctx, send: () => {}, diff --git a/packages/shell/tool-bash-persistent/tests/tools.spec.ts b/packages/shell/tool-bash-persistent/tests/tools.spec.ts index c7a406663f..3afbecfec3 100644 --- a/packages/shell/tool-bash-persistent/tests/tools.spec.ts +++ b/packages/shell/tool-bash-persistent/tests/tools.spec.ts @@ -2,7 +2,7 @@ import { afterEach, describe, expect, it } from 'vitest' import { Context } from '@deepseek-ai/cordis' import { ToolCallId } from '@deepseek-ai/dsh-llm' import { SESSION_FORMAT_VERSION, Session, SessionId } from '@deepseek-ai/dsh-session' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import TerminalSessionService from '@deepseek-ai/dsh-terminal' import type { @@ -18,6 +18,7 @@ import type { import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRuntime from '@deepseek-ai/dsh-tools' import * as ToolBashPersistent from '@deepseek-ai/dsh-tool-bash-persistent' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' const contexts: Context[] = [] let callNumber = 0 @@ -40,7 +41,7 @@ function agent(ctx: Context, cwd: string | undefined): Agent { id, options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: unsupportedInbox(), status: 'idle', ctx: scope.ctx, send: () => {}, diff --git a/packages/shell/tool-bash/tests/integration.spec.ts b/packages/shell/tool-bash/tests/integration.spec.ts index f06f4d22f3..cf2a4e1d2e 100644 --- a/packages/shell/tool-bash/tests/integration.spec.ts +++ b/packages/shell/tool-bash/tests/integration.spec.ts @@ -6,7 +6,6 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { SessionId, type SessionEvent } from '@deepseek-ai/dsh-session' import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import type { Agent } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' @@ -27,8 +26,6 @@ import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent async function harness(adapter: MockAdapter, sessionRoot?: string, dshHome?: string) { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - // AgentLoop declares the registry as a required injection. - await ctx.plugin(SessionProjectionRegistry) if (sessionRoot !== undefined) { await ctx.plugin(JsonlSessionPersistence, { root: sessionRoot, compression: 'none' }) } diff --git a/packages/shell/tool-pwsh-persistent/package.json b/packages/shell/tool-pwsh-persistent/package.json index 227c349963..5089ec937b 100644 --- a/packages/shell/tool-pwsh-persistent/package.json +++ b/packages/shell/tool-pwsh-persistent/package.json @@ -39,6 +39,7 @@ "@deepseek-ai/cordis-plugin-include": "workspace:^", "@deepseek-ai/cordis-plugin-loader": "workspace:^", "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", "@deepseek-ai/dsh-terminal": "workspace:^", "@deepseek-ai/dsh-terminal-bash": "workspace:^", diff --git a/packages/shell/tool-pwsh-persistent/tests/loader-composition.spec.ts b/packages/shell/tool-pwsh-persistent/tests/loader-composition.spec.ts index 74bc8c0514..05116a6b5c 100644 --- a/packages/shell/tool-pwsh-persistent/tests/loader-composition.spec.ts +++ b/packages/shell/tool-pwsh-persistent/tests/loader-composition.spec.ts @@ -9,8 +9,8 @@ import Loader from '@deepseek-ai/cordis-plugin-loader' import Include from '@deepseek-ai/cordis-plugin-include' import { ToolCallId } from '@deepseek-ai/dsh-llm' import { SESSION_FORMAT_VERSION, Session, SessionId } from '@deepseek-ai/dsh-session' +import AgentRegistry from '@deepseek-ai/dsh-agent' import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import TerminalSessionService from '@deepseek-ai/dsh-terminal' import * as TerminalBash from '@deepseek-ai/dsh-terminal-bash' @@ -22,6 +22,7 @@ import { resolvePwshPath } from '@deepseek-ai/dsh-pwsh-local/src/resolve.ts' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRegistry from '@deepseek-ai/dsh-tools' import * as ToolPwshPersistent from '@deepseek-ai/dsh-tool-pwsh-persistent' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' const hasPwsh = spawnSync( resolvePwshPath(), ['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', '$true'], @@ -54,7 +55,7 @@ function agent(ctx: Context, cwd: string): Agent { id, options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: unsupportedInbox(), status: 'idle', ctx: scope.ctx, send: () => {}, diff --git a/packages/shell/tool-pwsh-persistent/tests/tools.spec.ts b/packages/shell/tool-pwsh-persistent/tests/tools.spec.ts index 4190bdb604..e65859e4c8 100644 --- a/packages/shell/tool-pwsh-persistent/tests/tools.spec.ts +++ b/packages/shell/tool-pwsh-persistent/tests/tools.spec.ts @@ -2,7 +2,7 @@ import { afterEach, describe, expect, it } from 'vitest' import { Context } from '@deepseek-ai/cordis' import { ToolCallId } from '@deepseek-ai/dsh-llm' import { SESSION_FORMAT_VERSION, Session, SessionId } from '@deepseek-ai/dsh-session' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import TerminalSessionService from '@deepseek-ai/dsh-terminal' import type { @@ -18,6 +18,7 @@ import type { import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRegistry from '@deepseek-ai/dsh-tools' import * as ToolPwshPersistent from '@deepseek-ai/dsh-tool-pwsh-persistent' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' const contexts: Context[] = [] let callNumber = 0 @@ -40,7 +41,7 @@ function agent(ctx: Context, cwd: string | undefined): Agent { id, options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: unsupportedInbox(), status: 'idle', ctx: scope.ctx, send: () => {}, diff --git a/packages/skill/tool-skill/package.json b/packages/skill/tool-skill/package.json index d028a3ca8c..49be90ea89 100644 --- a/packages/skill/tool-skill/package.json +++ b/packages/skill/tool-skill/package.json @@ -38,6 +38,7 @@ }, "devDependencies": { "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", "@deepseek-ai/dsh-scope": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", diff --git a/packages/skill/tool-skill/tests/tool-skill.spec.ts b/packages/skill/tool-skill/tests/tool-skill.spec.ts index ffcb54268b..29de7d0bd7 100644 --- a/packages/skill/tool-skill/tests/tool-skill.spec.ts +++ b/packages/skill/tool-skill/tests/tool-skill.spec.ts @@ -10,10 +10,11 @@ import { } from '@deepseek-ai/dsh-session' import SystemPrompt, { renderPrompt } from '@deepseek-ai/dsh-system-prompt' import ToolRuntime, { defineContentToolFixture } from '@deepseek-ai/dsh-tools' -import AgentRegistry, { agentEvents, Inbox, type Agent, type PreStepDecision } from '@deepseek-ai/dsh-agent' +import AgentRegistry, { agentEvents, type Agent, type PreStepDecision } from '@deepseek-ai/dsh-agent' import SkillRegistry from '@deepseek-ai/dsh-skill' import * as SkillFileSystem from '@deepseek-ai/dsh-skill-filesystem' import * as toolSkill from '@deepseek-ai/dsh-tool-skill' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' const testToolSignal = new AbortController().signal @@ -56,7 +57,7 @@ function agentForCwd(cwd: string): Agent { id, options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: unsupportedInbox(), status: 'idle', send: () => {}, followup: () => {}, @@ -69,11 +70,11 @@ function agentForCwd(cwd: string): Agent { } function sessionAgent(session: Session, id = 'tool-skill-agent'): Agent { - return { + const agent: Agent = { id: SessionId(id), options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: unsupportedInbox(), status: 'running', ctx: new Context(), send: () => {}, @@ -84,6 +85,7 @@ function sessionAgent(session: Session, id = 'tool-skill-agent'): Agent { runMaintenance: task => task(new AbortController().signal), whenIdle: () => Promise.resolve(), } + return agent } function openMessageTurn(session: Session, turn = 1): void { diff --git a/packages/subagent/subagent-fork-in-process/tests/multi-subagent.spec.ts b/packages/subagent/subagent-fork-in-process/tests/multi-subagent.spec.ts index e761f070af..35c8af4df3 100644 --- a/packages/subagent/subagent-fork-in-process/tests/multi-subagent.spec.ts +++ b/packages/subagent/subagent-fork-in-process/tests/multi-subagent.spec.ts @@ -9,7 +9,6 @@ import * as SessionInvariant from '@deepseek-ai/dsh-session/invariant' import * as AgentInvariant from '@deepseek-ai/dsh-agent/invariant' import * as AgentLoopInvariant from '@deepseek-ai/dsh-agent-loop/invariant' import SubagentRuntime, { type SubagentStartRequest } from '@deepseek-ai/dsh-subagent' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import * as Spawn from '@deepseek-ai/dsh-subagent-spawn-in-process' import { MockAdapter, textResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' import * as fork from '../src/index.ts' @@ -38,7 +37,6 @@ async function setup(script: Script) { await mountAgentLoopTestDependencies(ctx) await mountInvariants(ctx) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SubagentRuntime) await ctx.plugin(Spawn, { providerName: 'spawn' }) await ctx.plugin(fork, { providerName: 'fork' }) diff --git a/packages/subagent/subagent-fork-in-process/tests/subagent-fork-in-process.spec.ts b/packages/subagent/subagent-fork-in-process/tests/subagent-fork-in-process.spec.ts index ac9a994c29..6f7080cd31 100644 --- a/packages/subagent/subagent-fork-in-process/tests/subagent-fork-in-process.spec.ts +++ b/packages/subagent/subagent-fork-in-process/tests/subagent-fork-in-process.spec.ts @@ -45,7 +45,6 @@ async function setup(script: Script) { await mountAgentLoopTestDependencies(ctx) await mountInvariants(ctx) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SubagentRuntime) await ctx.plugin(fork, { providerName: 'fork' }) ctx.llm.registerAdapter(['mock'], new MockAdapter(script)) diff --git a/packages/subagent/subagent-in-process-driver/tests/inheritance.spec.ts b/packages/subagent/subagent-in-process-driver/tests/inheritance.spec.ts index 4d86cb3705..abc09ff53d 100644 --- a/packages/subagent/subagent-in-process-driver/tests/inheritance.spec.ts +++ b/packages/subagent/subagent-in-process-driver/tests/inheritance.spec.ts @@ -18,7 +18,6 @@ import { SessionId, type SessionEvent } from '@deepseek-ai/dsh-session' import * as ToolFs from '@deepseek-ai/dsh-tool-fs' import ApprovalService from '@deepseek-ai/dsh-user-approval' import { snapshotSubagentDescriptor } from '@deepseek-ai/dsh-subagent' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' import { startInProcessRun } from '../src/index.ts' @@ -41,7 +40,6 @@ async function setupWalled(script: Script): Promise<{ ctx: Context; parent: Agen const ctx = new Context() contexts.push(ctx) await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SandboxPolicyService, { mode: 'workspace-write', workspaceRoot: workspace }) await ctx.plugin(SandboxedFileSystem, { cwd: workspace }) await ctx.plugin(ToolFs) diff --git a/packages/subagent/subagent-in-process-driver/tests/preset-inheritance.spec.ts b/packages/subagent/subagent-in-process-driver/tests/preset-inheritance.spec.ts index d34729037c..d3a88e5572 100644 --- a/packages/subagent/subagent-in-process-driver/tests/preset-inheritance.spec.ts +++ b/packages/subagent/subagent-in-process-driver/tests/preset-inheritance.spec.ts @@ -18,7 +18,6 @@ import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import AgentPresets from '@deepseek-ai/dsh-agent-presets' import { SessionId } from '@deepseek-ai/dsh-session' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { snapshotSubagentDescriptor } from '@deepseek-ai/dsh-subagent' import { MockAdapter, textResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' import { startInProcessRun } from '../src/index.ts' @@ -40,7 +39,6 @@ async function setupPresetHost(): Promise<{ ctx: Context; adapter: MockAdapter; await ctx.plugin(Loader) ctx.loader.builtins.include = Include await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(AgentPresets, { default: 'coding', roots: ROOTS, includeShippedRoot: false, includeUserRoot: false }) const adapter = new MockAdapter([textResponse('parent idle'), textResponse('child done')]) diff --git a/packages/subagent/subagent-in-process-driver/tests/structured.spec.ts b/packages/subagent/subagent-in-process-driver/tests/structured.spec.ts index 2b5350d951..c529459b82 100644 --- a/packages/subagent/subagent-in-process-driver/tests/structured.spec.ts +++ b/packages/subagent/subagent-in-process-driver/tests/structured.spec.ts @@ -13,7 +13,6 @@ import SubagentRuntime, { type ResolvedSubagentStartRequest, type SubagentStartRequest, } from '@deepseek-ai/dsh-subagent' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import type { Config as ToolConfig, ObjectJsonSchema } from '@deepseek-ai/dsh-tools' import { defineContentToolFixture, RUN_CODE_NAME } from '@deepseek-ai/dsh-tools' import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' @@ -69,7 +68,6 @@ async function setup(script: Script, options: SetupOptions = {}) { } await mountInvariants(ctx) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SubagentRuntime) const disposeProvider = ctx.subagents.registerProvider({ name: 'spawn', diff --git a/packages/subagent/subagent-in-process-driver/tests/subagent-in-process-driver.spec.ts b/packages/subagent/subagent-in-process-driver/tests/subagent-in-process-driver.spec.ts index c669e614b6..d257c3d442 100644 --- a/packages/subagent/subagent-in-process-driver/tests/subagent-in-process-driver.spec.ts +++ b/packages/subagent/subagent-in-process-driver/tests/subagent-in-process-driver.spec.ts @@ -10,7 +10,6 @@ import * as SessionInvariant from '@deepseek-ai/dsh-session/invariant' import * as AgentInvariant from '@deepseek-ai/dsh-agent/invariant' import * as AgentLoopInvariant from '@deepseek-ai/dsh-agent-loop/invariant' import SubagentRuntime, { snapshotSubagentDescriptor } from '@deepseek-ai/dsh-subagent' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { defineContentToolFixture } from '@deepseek-ai/dsh-tools' import { maxTokensResponse, MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' import { startInProcessRun } from '../src/index.ts' @@ -29,7 +28,6 @@ async function setup(script: Script, parentOptions: Partial = {}) await mountAgentLoopTestDependencies(ctx) await mountInvariants(ctx) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SubagentRuntime) const adapter = new MockAdapter(script) ctx.llm.registerAdapter(['mock'], adapter) diff --git a/packages/subagent/subagent-spawn-in-process/tests/harness.ts b/packages/subagent/subagent-spawn-in-process/tests/harness.ts index 89606e923b..03442bfb11 100644 --- a/packages/subagent/subagent-spawn-in-process/tests/harness.ts +++ b/packages/subagent/subagent-spawn-in-process/tests/harness.ts @@ -1,7 +1,6 @@ import { Context } from '@deepseek-ai/cordis' import type { Agent } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local' import * as BashEnvPlugin from '@deepseek-ai/dsh-shell-env' @@ -25,7 +24,6 @@ export async function spawnHarness(workdir: string): Promise { // 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(SessionProjectionRegistry) await mountAgentLoopTestDependencies(ctx, { systemPrompt: { personaPrefix: 'You are a coding agent. Report only when the requested work is done.' }, }) diff --git a/packages/subagent/subagent-spawn-in-process/tests/subagent-spawn-in-process.spec.ts b/packages/subagent/subagent-spawn-in-process/tests/subagent-spawn-in-process.spec.ts index dc9d3ed4d1..21081b417c 100644 --- a/packages/subagent/subagent-spawn-in-process/tests/subagent-spawn-in-process.spec.ts +++ b/packages/subagent/subagent-spawn-in-process/tests/subagent-spawn-in-process.spec.ts @@ -39,7 +39,6 @@ async function setup(script: Script) { await mountAgentLoopTestDependencies(ctx) await mountInvariants(ctx) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SubagentRuntime) await ctx.plugin(spawn, { providerName: 'spawn' }) ctx.llm.registerAdapter(['mock'], adapter) @@ -330,7 +329,6 @@ describe('dsh-subagent-spawn-in-process', () => { await mountAgentLoopTestDependencies(ctx) await mountInvariants(ctx) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SubagentRuntime) const fiber = await ctx.plugin(spawn, { providerName: 'spawn' }) ctx.llm.registerAdapter(['mock'], adapter) @@ -359,7 +357,6 @@ describe('dsh-subagent-spawn-in-process', () => { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SubagentRuntime) const fiber = await ctx.plugin(spawn, { providerName: 'spawn' }) const parent = await ctx.agentLoop.create(SessionId('parent'), { provider: 'mock', model: 'mock' }) diff --git a/packages/subagent/subagent/src/inbox.ts b/packages/subagent/subagent/src/inbox.ts index 8800e3ce2b..e610db2014 100644 --- a/packages/subagent/subagent/src/inbox.ts +++ b/packages/subagent/subagent/src/inbox.ts @@ -35,7 +35,7 @@ export class SubagentInbox { * @returns whether either Agent inbox destination is non-empty. */ get hasPending(): boolean { - return this.agent.inbox.hasPending + return this.agent.inbox.nextTurn.length > 0 || this.agent.inbox.nextStep.length > 0 } /** diff --git a/packages/subagent/subagent/tests/continuation-inheritance.spec.ts b/packages/subagent/subagent/tests/continuation-inheritance.spec.ts index d1dde79a41..9e3a6989c6 100644 --- a/packages/subagent/subagent/tests/continuation-inheritance.spec.ts +++ b/packages/subagent/subagent/tests/continuation-inheritance.spec.ts @@ -19,7 +19,6 @@ import SandboxPolicyService, { setSandboxMode } from '@deepseek-ai/dsh-sandbox-p import { Session, SessionId } from '@deepseek-ai/dsh-session' import type { SessionEvent } from '@deepseek-ai/dsh-session' import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import { queueHostSubagentPrompt } from '@deepseek-ai/dsh-subagent/internal' import * as SubagentFork from '@deepseek-ai/dsh-subagent-fork-in-process' import * as SubagentSpawn from '@deepseek-ai/dsh-subagent-spawn-in-process' @@ -43,7 +42,6 @@ async function setup(script: Script) { const ctx = new Context() contexts.push(ctx) await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) const root = mkdtempSync(join(tmpdir(), 'dsh-continuation-inherit-')) roots.push(root) await ctx.plugin(JsonlSessionPersistence, { root }) diff --git a/packages/subagent/subagent/tests/continuation.spec.ts b/packages/subagent/subagent/tests/continuation.spec.ts index 8e082debe4..a99ac93e71 100644 --- a/packages/subagent/subagent/tests/continuation.spec.ts +++ b/packages/subagent/subagent/tests/continuation.spec.ts @@ -9,7 +9,6 @@ import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-test import { SessionId } from '@deepseek-ai/dsh-session' import type { SessionEvent } from '@deepseek-ai/dsh-session' import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import * as SubagentSpawn from '@deepseek-ai/dsh-subagent-spawn-in-process' import * as SubagentFork from '@deepseek-ai/dsh-subagent-fork-in-process' import type { ContentBlock, GenerateOptions, MessageId, StreamChunk } from '@deepseek-ai/dsh-llm' @@ -80,9 +79,6 @@ async function setupWith( ) { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - // The registry is a required injection of AgentLoop and SubagentRuntime - // (both register projection units on activation). - await ctx.plugin(SessionProjectionRegistry) let disposePersistence: (() => Promise) | undefined let root: string | undefined if (options.persistence !== false) { @@ -527,7 +523,6 @@ describe('SubagentRuntime.startContinuable', () => { const fresh = new Context() await mountAgentLoopTestDependencies(fresh) - await fresh.plugin(SessionProjectionRegistry) const freshPersistence = await fresh.plugin(JsonlSessionPersistence, { root: root! }) // This context opened a second handle on the same root; register it so // afterEach closes it before removing the root (even on a failure path). @@ -3235,7 +3230,6 @@ describe('continuable errors', () => { const adapter = new GatedAdapter([{ chunks: textResponse('child'), gate: hold.promise }]) const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) const root = mkdtempSync(join(tmpdir(), 'dsh-subagent-continuation-')) const persistenceFiber = await ctx.plugin(JsonlSessionPersistence, { root }) cleanups.push(async () => { diff --git a/packages/subagent/subagent/tests/list-children.spec.ts b/packages/subagent/subagent/tests/list-children.spec.ts index c8b526920d..a9e34f9ffc 100644 --- a/packages/subagent/subagent/tests/list-children.spec.ts +++ b/packages/subagent/subagent/tests/list-children.spec.ts @@ -47,7 +47,7 @@ afterEach(async () => { /** Boot the continuable stack with real JSONL session persistence. */ async function setup( script: Script, - options: { sessionProjections?: boolean; projectionCache?: boolean } = {}, + options: { projectionCache?: boolean } = {}, ) { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) @@ -56,7 +56,6 @@ async function setup( const persistence = await ctx.plugin(JsonlSessionPersistence, { root }) persistenceDisposers.push(() => persistence.dispose()) await ctx.plugin(AgentLoop, { agents: [] }) - if (options.sessionProjections !== false) await ctx.plugin(SessionProjectionRegistry) if (options.projectionCache === true) { const root = mkdtempSync(join(tmpdir(), 'dsh-subagent-projcache-')) projCacheRoots.push(root) @@ -82,6 +81,13 @@ async function setup( return { ctx, parent } } +async function setupWithoutProjections(): Promise { + const ctx = new Context() + await ctx.plugin(SessionStore) + await ctx.plugin(SubagentRuntime) + return ctx +} + const testSignal = new AbortController().signal /** Start one continuable child through the real service path and await Activation release. */ @@ -246,8 +252,8 @@ describe('SubagentRuntime.listChildren', () => { }) it('fails loud when the projection registry is not mounted, even with no children', async () => { - const { ctx, parent } = await setup([], { sessionProjections: false }) - await expect(ctx.subagents.listChildren(parent.id)).rejects.toThrow( + const ctx = await setupWithoutProjections() + await expect(ctx.subagents.listChildren(SessionId('no-projections-parent'))).rejects.toThrow( expect.objectContaining({ code: 'SUBAGENT_CONTROL_PROJECTIONS_UNAVAILABLE' }) as Error, ) }) @@ -1108,8 +1114,9 @@ describe('SubagentRuntime.listChildren', () => { }) it('SubagentError from listChildren is typed with its stable code', async () => { - const { ctx, parent } = await setup([], { sessionProjections: false }) - const caught: unknown = await ctx.subagents.listChildren(parent.id).catch((error: unknown) => error) + const ctx = await setupWithoutProjections() + const caught: unknown = await ctx.subagents.listChildren(SessionId('typed-no-projections-parent')) + .catch((error: unknown) => error) expect(caught).toBeInstanceOf(SubagentError) expect((caught as SubagentError).code).toBe('SUBAGENT_CONTROL_PROJECTIONS_UNAVAILABLE') }) @@ -1344,8 +1351,8 @@ describe('SubagentRuntime.listDescendants', () => { }) it('fails loud when the projection registry is not mounted', async () => { - const { ctx, parent } = await setup([], { sessionProjections: false }) - await expect(ctx.subagents.listDescendants(parent.id)).rejects.toThrow( + const ctx = await setupWithoutProjections() + await expect(ctx.subagents.listDescendants(SessionId('no-projections-root'))).rejects.toThrow( expect.objectContaining({ code: 'SUBAGENT_CONTROL_PROJECTIONS_UNAVAILABLE' }) as Error, ) }) diff --git a/packages/subagent/tool-subagent-control/tests/list-agents.spec.ts b/packages/subagent/tool-subagent-control/tests/list-agents.spec.ts index 8b190d8092..b21de72a90 100644 --- a/packages/subagent/tool-subagent-control/tests/list-agents.spec.ts +++ b/packages/subagent/tool-subagent-control/tests/list-agents.spec.ts @@ -8,7 +8,6 @@ import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import { SessionId } from '@deepseek-ai/dsh-session' import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import SubagentRuntime from '@deepseek-ai/dsh-subagent' import type { SubagentListEntry } from '@deepseek-ai/dsh-subagent' import * as SubagentSpawn from '@deepseek-ai/dsh-subagent-spawn-in-process' @@ -60,7 +59,6 @@ async function setupWith(adapter: MockAdapter | GatedAdapter) { await ctx.plugin(JsonlSessionPersistence, { root }) await ctx.plugin(TestSessionQuery) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SubagentRuntime) await ctx.plugin(SubagentSpawn, { providerName: 'spawn' }) await ctx.plugin(tool) @@ -251,7 +249,6 @@ describe('dsh-tool-subagent-control/list-agents', () => { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SubagentRuntime) const fiber = await ctx.plugin(tool) expect(ctx.tools.schemas().some(schema => schema.name === 'list_agents')).toBe(true) diff --git a/packages/subagent/tool-subagent-control/tests/tool-subagent-control.spec.ts b/packages/subagent/tool-subagent-control/tests/tool-subagent-control.spec.ts index da7fe7a51b..c1cb00e114 100644 --- a/packages/subagent/tool-subagent-control/tests/tool-subagent-control.spec.ts +++ b/packages/subagent/tool-subagent-control/tests/tool-subagent-control.spec.ts @@ -9,7 +9,6 @@ import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import { SessionId } from '@deepseek-ai/dsh-session' import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import SubagentRuntime from '@deepseek-ai/dsh-subagent' import * as SubagentFork from '@deepseek-ai/dsh-subagent-fork-in-process' import * as SubagentSpawn from '@deepseek-ai/dsh-subagent-spawn-in-process' @@ -62,7 +61,6 @@ async function setupWith(adapter: MockAdapter | GatedAdapter, park = true) { await ctx.plugin(JsonlSessionPersistence, { root }) await ctx.plugin(TestSessionQuery) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SubagentRuntime) await ctx.plugin(SubagentSpawn, { providerName: 'spawn' }) await ctx.plugin(SubagentFork, { providerName: 'fork' }) @@ -333,7 +331,6 @@ describe('dsh-tool-subagent-control', () => { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) await ctx.plugin(AgentLoop, { agents: [] }) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SubagentRuntime) const fiber = await ctx.plugin(tool) expect(ctx.tools.schemas().some(schema => schema.name === 'send_message')).toBe(true) diff --git a/packages/subagent/tool-subagent/tests/harness.ts b/packages/subagent/tool-subagent/tests/harness.ts index acd1b02c7c..bda0a5ae63 100644 --- a/packages/subagent/tool-subagent/tests/harness.ts +++ b/packages/subagent/tool-subagent/tests/harness.ts @@ -50,7 +50,6 @@ export async function setup(toolConfig: SetupConfig, mockConfig: Partial { await ctx.plugin(MemorySettings) await ctx.plugin(SubagentModelSelectionConfig) await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(SubagentRuntime) await ctx.plugin(SubagentSpawn, { providerName: 'spawn' }) @@ -327,7 +326,6 @@ describe('SubagentModelSelectionConfig', () => { it('requires both the Host setting owner and a composition scope', async () => { const withoutSettings = new Context() await mountAgentLoopTestDependencies(withoutSettings) - await withoutSettings.plugin(SessionProjectionRegistry) await withoutSettings.plugin(SubagentRuntime) expect(() => { tool.apply(withoutSettings, { diff --git a/packages/subagent/tool-subagent/tests/tool-subagent.spec.ts b/packages/subagent/tool-subagent/tests/tool-subagent.spec.ts index e5f9502a43..1e79190deb 100644 --- a/packages/subagent/tool-subagent/tests/tool-subagent.spec.ts +++ b/packages/subagent/tool-subagent/tests/tool-subagent.spec.ts @@ -1182,7 +1182,7 @@ describe('dsh-tool-subagent continuable background mode', () => { /** Boot the real continuable stack without any model-facing follow-up adapter. */ async function continuableSetup() { - const ctx = await projectedContext() + const ctx = new Context() await mountAgentLoopTestDependencies(ctx) const root = mkdtempSync(path.join(tmpdir(), 'dsh-tool-subagent-continuable-')) roots.push(root) diff --git a/packages/terminal/terminal-bash/package.json b/packages/terminal/terminal-bash/package.json index b9e15ff0d1..e378385dc5 100644 --- a/packages/terminal/terminal-bash/package.json +++ b/packages/terminal/terminal-bash/package.json @@ -43,6 +43,7 @@ }, "devDependencies": { "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-terminal": "workspace:^", "@deepseek-ai/dsh-sandbox": "workspace:^", "@deepseek-ai/dsh-sandbox-policy": "workspace:^", diff --git a/packages/terminal/terminal-bash/tests/index.spec.ts b/packages/terminal/terminal-bash/tests/index.spec.ts index ff9bb91afb..af707d8269 100644 --- a/packages/terminal/terminal-bash/tests/index.spec.ts +++ b/packages/terminal/terminal-bash/tests/index.spec.ts @@ -4,7 +4,7 @@ import { resolve } from 'node:path' import { Context } from '@deepseek-ai/cordis' import Loader from '@deepseek-ai/cordis-plugin-loader' import SessionStore, { SESSION_FORMAT_VERSION, Session, SessionId } from '@deepseek-ai/dsh-session' -import AgentRegistry, { Inbox, type Agent } from '@deepseek-ai/dsh-agent' +import AgentRegistry, { type Agent } from '@deepseek-ai/dsh-agent' import SandboxProvider from '@deepseek-ai/dsh-sandbox' import type { ConfinedArgv, SandboxPolicy } from '@deepseek-ai/dsh-sandbox' import SandboxPolicyService, { setSandboxMode } from '@deepseek-ai/dsh-sandbox-policy' @@ -23,6 +23,7 @@ import type { SubprocessTerminalHandle, SubprocessTerminalSpawnSpec, } from '@deepseek-ai/dsh-subprocess' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' class EmptySandbox extends SandboxProvider { confine(_argv: readonly string[], _policy: SandboxPolicy): ConfinedArgv { @@ -54,7 +55,7 @@ function agent(ctx: Context, cwd?: string): Agent { version: SESSION_FORMAT_VERSION, id, createdAt: 0, isSeeded: false, ...cwd === undefined ? {} : { cwd }, }) return { - id, options: {}, session, inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + id, options: {}, session, inbox: unsupportedInbox(), status: 'idle', ctx, send: () => {}, @@ -593,7 +594,7 @@ describe('terminal-bash plugin shape', () => { const session = ctx.sessions.create(SessionId('mode-owner')) const ownerFiber = await ctx.plugin(() => {}) const owner: Agent = { - id: session.id, options: {}, session, inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + id: session.id, options: {}, session, inbox: unsupportedInbox(), status: 'idle', ctx: ownerFiber.ctx, send: () => {}, @@ -643,7 +644,7 @@ describe('terminal-bash plugin shape', () => { const session = ctx.sessions.create(SessionId('pending-mode-owner')) const ownerFiber = await ctx.plugin(() => {}) const owner: Agent = { - id: session.id, options: {}, session, inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + id: session.id, options: {}, session, inbox: unsupportedInbox(), status: 'idle', ctx: ownerFiber.ctx, send: () => {}, diff --git a/packages/terminal/terminal-bash/tests/local.spec.ts b/packages/terminal/terminal-bash/tests/local.spec.ts index 1df9b7cb9e..9800a79d54 100644 --- a/packages/terminal/terminal-bash/tests/local.spec.ts +++ b/packages/terminal/terminal-bash/tests/local.spec.ts @@ -5,7 +5,7 @@ import { join } from 'node:path' import { afterEach, describe, expect, it } from 'vitest' import { Context } from '@deepseek-ai/cordis' import { Session, SessionId } from '@deepseek-ai/dsh-session' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import TerminalSessionService from '@deepseek-ai/dsh-terminal' import type { TerminalSendOperation } from '@deepseek-ai/dsh-terminal' @@ -16,6 +16,7 @@ import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import LocalSubprocessRuntime from '@deepseek-ai/dsh-subprocess-local' import { resolvePwshPath } from '@deepseek-ai/dsh-pwsh-local/src/resolve.ts' import * as ptyLocal from '@deepseek-ai/dsh-terminal-bash' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' const roots: string[] = [] const contexts: Context[] = [] @@ -38,8 +39,8 @@ function stubAgent(ctx: Context, rawId: string): Agent { const id = SessionId(rawId) const scope = ctx.plugin(() => {}) const session = Session.create(id) - return { - id, options: {}, session, inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + const agent: Agent = { + id, options: {}, session, inbox: unsupportedInbox(), status: 'idle', ctx: scope.ctx, send: () => {}, @@ -47,6 +48,7 @@ function stubAgent(ctx: Context, rawId: string): Agent { runMaintenance: task => task(new AbortController().signal), whenIdle: () => Promise.resolve(), } + return agent } async function harness( diff --git a/packages/terminal/terminal/package.json b/packages/terminal/terminal/package.json index 60b671464e..48ca98a847 100644 --- a/packages/terminal/terminal/package.json +++ b/packages/terminal/terminal/package.json @@ -33,6 +33,7 @@ }, "devDependencies": { "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-brand": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", "@deepseek-ai/cordis": "workspace:^" diff --git a/packages/terminal/terminal/tests/service.spec.ts b/packages/terminal/terminal/tests/service.spec.ts index 10484d3c5c..c5c552d18f 100644 --- a/packages/terminal/terminal/tests/service.spec.ts +++ b/packages/terminal/terminal/tests/service.spec.ts @@ -1,7 +1,7 @@ import { describe, expect, expectTypeOf, it } from 'vitest' import { Context } from '@deepseek-ai/cordis' import { Session, SessionId } from '@deepseek-ai/dsh-session' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import TerminalSessionService, { TerminalBackendCleanupError, TerminalError, TerminalSessionId } from '@deepseek-ai/dsh-terminal' import type { @@ -14,6 +14,7 @@ import type { TerminalSessionStatus, TerminalSignal, } from '@deepseek-ai/dsh-terminal' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' const agentScopeDisposers = new WeakMap Promise>() const ptyServiceDisposers = new WeakMap Promise>() @@ -26,7 +27,7 @@ function stubAgent(ctx: Context, rawId: string): Agent { id, options: {}, session, - inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + inbox: unsupportedInbox(), status: 'idle', ctx: scopeFiber.ctx, send: () => {}, diff --git a/packages/terminal/tool-terminal/package.json b/packages/terminal/tool-terminal/package.json index 5a69158158..dd09568343 100644 --- a/packages/terminal/tool-terminal/package.json +++ b/packages/terminal/tool-terminal/package.json @@ -43,6 +43,7 @@ "@deepseek-ai/cordis-plugin-include": "workspace:^", "@deepseek-ai/cordis-plugin-loader": "workspace:^", "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop-testkit": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", "@deepseek-ai/dsh-terminal": "workspace:^", "@deepseek-ai/dsh-terminal-bash": "workspace:^", diff --git a/packages/terminal/tool-terminal/tests/loader-composition.spec.ts b/packages/terminal/tool-terminal/tests/loader-composition.spec.ts index 6c4fe65e6a..c6f8f494de 100644 --- a/packages/terminal/tool-terminal/tests/loader-composition.spec.ts +++ b/packages/terminal/tool-terminal/tests/loader-composition.spec.ts @@ -8,7 +8,7 @@ import Loader from '@deepseek-ai/cordis-plugin-loader' import Include from '@deepseek-ai/cordis-plugin-include' import { ToolCallId } from '@deepseek-ai/dsh-llm' import { Session, SessionId } from '@deepseek-ai/dsh-session' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRuntime from '@deepseek-ai/dsh-tools' @@ -20,6 +20,7 @@ import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import LocalSubprocessRuntime from '@deepseek-ai/dsh-subprocess-local' import * as TerminalLocal from '@deepseek-ai/dsh-terminal-bash' import * as ToolPty from '@deepseek-ai/dsh-tool-terminal' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' let root: string | undefined let context: Context | undefined @@ -42,7 +43,7 @@ function agent(ctx: Context): Agent { const id = SessionId('pty-loader-agent') const session = Session.create(id) const value: Agent = { - id, options: {}, session, inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + id, options: {}, session, inbox: unsupportedInbox(), status: 'idle', ctx: scope.ctx, send: () => {}, diff --git a/packages/terminal/tool-terminal/tests/tools.spec.ts b/packages/terminal/tool-terminal/tests/tools.spec.ts index 15151f7bd2..8c0c7f6292 100644 --- a/packages/terminal/tool-terminal/tests/tools.spec.ts +++ b/packages/terminal/tool-terminal/tests/tools.spec.ts @@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest' import { Context } from '@deepseek-ai/cordis' import { ToolCallId } from '@deepseek-ai/dsh-llm' import { Session, SessionId } from '@deepseek-ai/dsh-session' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRuntime, { renderToolsSdk } from '@deepseek-ai/dsh-tools' @@ -12,13 +12,14 @@ import type { TerminalBackend, TerminalBackendSession, TerminalSendOperation, Te import LocalJobRegistry from '@deepseek-ai/dsh-jobs-local' import * as ToolTasks from '@deepseek-ai/dsh-tool-jobs' import * as ToolPty from '@deepseek-ai/dsh-tool-terminal' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' function fakeAgent(ctx: Context, rawId: string): Agent { const scope = ctx.plugin(() => {}) const id = SessionId(rawId) const session = Session.create(id) const agent: Agent = { - id, options: {}, session, inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + id, options: {}, session, inbox: unsupportedInbox(), status: 'idle', ctx: scope.ctx, send: () => {}, diff --git a/packages/test-support/agent-loop-testkit/README.i18n.yaml b/packages/test-support/agent-loop-testkit/README.i18n.yaml index 7a4efa8491..3f0298285b 100644 --- a/packages/test-support/agent-loop-testkit/README.i18n.yaml +++ b/packages/test-support/agent-loop-testkit/README.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 packages/test-support/agent-loop-testkit/README.md -README.md: 7b935594636e265bf4f808f692bba2fae0f83ddf -README.zh.md: fc9dc688a1e7d4303f7744ed47c7da2d0fbbf67f +README.md: 7c77cf77cf20817795f43c3493bff8e5c7ccd900 +README.zh.md: 0862c60e7b40a8f63925b87bc41377b12ac2d788 diff --git a/packages/test-support/agent-loop-testkit/README.md b/packages/test-support/agent-loop-testkit/README.md index 7b93559463..7c77cf77cf 100644 --- a/packages/test-support/agent-loop-testkit/README.md +++ b/packages/test-support/agent-loop-testkit/README.md @@ -1,5 +1,5 @@ --- -description: "Shared service mounting for tests that exercise the concrete AgentLoop, for test authors wiring real loop prerequisites." +description: "Prerequisite mounting, production AgentLoop drivers, and explicit Inbox stubs for agent-loop tests." kind: "package-library" --- @@ -9,7 +9,7 @@ English | [中文](README.zh.md) ## Summary -`dsh-agent-loop-testkit` mounts the standard prerequisite services a test needs before loading the concrete `AgentLoop` — the LLM runtime, session store, system-prompt registry, tool registry, and agent registry — in dependency order, with one call. The loop itself, adapters, optional plugins, agents, and teardown stay in the test's hands, so each scenario keeps its own load order and topology. Use it when a test's subject is loop behavior rather than service wiring; tests that probe injection failures or partial topologies mount their dependencies directly. It registers no model-facing behavior of its own. +`dsh-agent-loop-testkit` mounts the standard prerequisite services a test needs before loading the concrete `AgentLoop` — the LLM runtime, session store, session-projection registry, system-prompt registry, tool registry, and agent registry — in dependency order, with one call. A second helper mounts the production loop and returns a narrow driver for creating real Agents and claiming their real Inbox input. Consumer tests that need only the public queue operations can instead use an explicitly process-local Inbox stub, while tests with no pending-input behavior can use a fail-fast unsupported Inbox. Adapters, optional plugins, load order, and teardown stay in the test's hands. The package registers no model-facing behavior of its own. ## Table of Contents @@ -25,31 +25,54 @@ English | [中文](README.zh.md) ## Use this package -This package gives an AgentLoop test a working service topology before the loop is mounted: call the helper on your test context, then mount `AgentLoop` with the configuration under test and register your adapter and optional plugins. +This package gives an AgentLoop test a working service topology and keeps the choice between production Inbox behavior and a structural stub explicit. -### Minimal example +### Drive a production Agent + +Use `mountAgentLoopTestHarness()` when the test covers durable Inbox events, projection recovery or validation, live Inbox notifications, or loop-driver claims. Mount any load-order-sensitive consumers after the prerequisites and before creating the Agent. The context owns the loop and every Agent returned by the harness. ```ts import { Context } from '@deepseek-ai/cordis' -import AgentLoop from '@deepseek-ai/dsh-agent-loop' -import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' +import { SessionId, type UserMessage } from '@deepseek-ai/dsh-session' +import { + mountAgentLoopTestDependencies, + mountAgentLoopTestHarness, +} from '@deepseek-ai/dsh-agent-loop-testkit' const ctx = new Context() await mountAgentLoopTestDependencies(ctx) -// Register the test adapter and any optional plugins here. -await ctx.plugin(AgentLoop, { agents: [] }) +// Register the test adapter and any load-order-sensitive plugins here. +const harness = await mountAgentLoopTestHarness(ctx) +const agent = await harness.create(SessionId('test-agent')) +declare const message: UserMessage + +agent.inbox.append('next-turn', message) +const admitted = harness.claim(agent, 'next-turn', 1) ``` -The helper activates the LLM, session, system-prompt, tool, and agent services in dependency order and returns before the loop is mounted. System-prompt and tool-registry configuration can be forwarded through `options`; the helper provides no test defaults beyond those the services own. +The dependency helper forwards system-prompt and tool-registry configuration through `options` and provides no test defaults beyond those services' own defaults. A plugin-load failure rejects the helper call; services activated earlier in the sequence remain context-owned and unwind when the context is disposed. + +### Build a structural Agent stub + +Use `createInboxStub()` when the test subject needs mutable pending lists but does not exercise durability, projection validation, live Inbox notifications, or the driver's claim policy. The stub implements the public queue operations with two process-local arrays and never writes to a Session. Use `unsupportedInbox()` when the test subject must not touch pending input; every mutation throws at the first unexpected dependency. + +```ts +import { createInboxStub } from '@deepseek-ai/dsh-agent-loop-testkit' + +const agent = { + // ... + inbox: createInboxStub(), +} +``` ### When to use it -Use the helper for tests whose subject is the loop: load order, retries, tool execution, or session behavior on a real prerequisite stack. Mount dependencies directly when a test probes service load order, injection failures, partial topologies, or teardown — the helper hides exactly the wiring such tests must control. +Use the dependency and loop helpers for tests whose subject is production loop or durable Inbox behavior. Use the structural stub for consumer-domain tests that only need queue editing. Mount dependencies directly when a test probes service injection failures or partial topologies, because the helper hides exactly the wiring those tests must control. ### What can go wrong -A plugin-load failure rejects the helper call; services activated earlier in the sequence remain owned by your context and unwind with it. The context owns every mounted service, so dispose it after the test. +The harness mounts no LLM adapter. Register an adapter before sending work that would start a model request. Dispose the owning context after every test so Agents reach quiescence and their scoped registrations unwind. ----- @@ -59,11 +82,11 @@ A plugin-load failure rejects the helper call; services activated earlier in the
Implementation internals — click to expand -This section explains the design of the helper; the observable behavior is fully covered in [Use this package](#use-this-package). +This section explains the design of the test utilities; the observable behavior is fully covered in [Use this package](#use-this-package). ### Design -**Runtime invariant:** No companion is published. This test-support package owns no production event stream or mutable data; consuming test suites exercise its behavior. +`mountAgentLoopTestDependencies` mounts six service plugins in a fixed dependency order — LLM, session, session-projection registry, system-prompt registry, tool registry, then agent registry — and stops before `AgentLoop`, so the caller controls loop load order. `mountAgentLoopTestHarness` mounts the public production plugin, creates Agents through its service, and exposes the production driver's claim operation without exporting the loop's concrete Inbox class or projection definition. [`src/inbox.ts`](src/inbox.ts) contains only the process-local mutable stub and the fail-fast unsupported placeholder; it owns no projection or durable event implementation. The mounting and driver implementation lives in [`src/index.ts`](src/index.ts). No invariant companion is published because the package owns only test helpers and has no independent production observations that can diverge.
@@ -72,11 +95,11 @@ This section explains the design of the helper; the observable behavior is fully ## Further Exploration -Read these pages when the package-level contract is not enough. They move from the loop to the services the helper mounts and the tests that use it. +Read these pages when the package-level behavior is not enough. They move from the loop to the services the helper mounts and the tests that use it. -- [Agent loop package](../../core/agent-loop/README.md) — the concrete loop this helper prepares tests for. -- [Session package](../../core/session/README.md) — the session store the helper mounts. -- [LLM package](../../llm/llm/README.md) — the LLM runtime and adapter contract the helper mounts. +- [Agent loop package](../../core/agent-loop/README.md) — the concrete loop this helper mounts for production behavior. +- [Session package](../../core/session/README.md) — the durable event log used by production Inbox behavior. +- [LLM package](../../llm/llm/README.md) — the LLM runtime and adapter interface the helper prepares. - [Testing policy](../../../docs/testing.md) — the coverage tiers these tests serve. - [Test-support group map](../README.md) — sibling harnesses and support packages. @@ -85,20 +108,22 @@ Read these pages when the package-level contract is not enough. They move from t ## Model Experience -None, as this test-only composition helper neither drives nor modifies model requests. +None, as these test-only utilities neither assemble nor modify model requests. #### KV Cache effect -None; this package neither assembles nor sends a provider request. +None; the package itself sends no provider request. ## Known Limitations and Deferred Work +These limits define what the utilities do not share. They are current package constraints, not a task backlog. -These limits define what the helper does not share. They are current package constraints, not a task backlog. - -- **Only the mandatory prerequisite spine is shared** — adapters, optional plugins, `AgentLoop`, agents, and context teardown remain caller-owned so scenario-specific ordering stays visible. +- **Only the mandatory prerequisite spine is shared** — adapters, optional plugins, scenario-specific load order, and context teardown remain caller-owned. +- **The production harness has no adapter default** — tests that start the loop must register the route they exercise. +- **The mutable Inbox stub is process-local only** — use a harness-created Agent whenever durable events, projection recovery or validation, live notifications, or claim policy matter. +- **The unsupported Inbox accepts no mutations** — use the mutable stub or a harness-created Agent whenever pending input is part of the test subject. ### Dev Note diff --git a/packages/test-support/agent-loop-testkit/README.zh.md b/packages/test-support/agent-loop-testkit/README.zh.md index fc9dc688a1..0862c60e7b 100644 --- a/packages/test-support/agent-loop-testkit/README.zh.md +++ b/packages/test-support/agent-loop-testkit/README.zh.md @@ -1,5 +1,5 @@ --- -description: "为运行具体 AgentLoop 的测试挂载共享服务先决依赖,面向接线真实循环前置依赖的测试作者。" +description: "为 agent-loop 测试提供先决依赖挂载、生产 AgentLoop 驱动与职责明确的 Inbox 桩。" kind: "package-library" --- @@ -9,7 +9,7 @@ kind: "package-library" ## 概述 -`dsh-agent-loop-testkit` 为测试在加载具体 `AgentLoop` 之前所需的全部标准先决服务——LLM(大语言模型)运行时、会话存储、系统提示词注册表、工具注册表与 agent(智能体)注册表——按依赖顺序一键挂载。loop 本身、适配器、可选插件、agent 与清理仍由测试掌控,因此每个场景都保持自己的加载顺序与拓扑。当测试对象是 loop 行为而非服务接线时使用它;针对注入失败或部分拓扑的测试会直接挂载其依赖。它自身不注册任何模型可见行为。 +`dsh-agent-loop-testkit` 为测试在加载具体 `AgentLoop` 之前所需的标准先决服务——LLM(大语言模型)运行时、会话存储、会话投影注册表、系统提示词注册表、工具注册表与 agent(智能体)注册表——按依赖顺序一键挂载。另一个辅助函数会挂载生产 loop,并返回一个精简驱动,用于创建真实 Agent 和通过真实 Inbox 认领输入。只需要公开队列操作的消费方测试可以改用明确标记为进程内实现的 Inbox 桩;不涉及待处理输入的测试则可以使用快速失败且不支持操作的 Inbox。适配器、可选插件、加载顺序与清理由测试掌控。本包自身不注册任何模型可见行为。 ## 目录 @@ -25,31 +25,54 @@ kind: "package-library" ## 使用本包 -本包在 loop 挂载前为 AgentLoop 测试提供可用的服务拓扑:在测试上下文上调用此辅助函数,然后用待测配置挂载 `AgentLoop`,并注册你的适配器与可选插件。 +本包为 AgentLoop 测试提供可用的服务拓扑,并要求测试明确选择生产 Inbox 行为或结构化桩。 -### 最小示例 +### 驱动生产 Agent + +当测试覆盖持久 Inbox 事件、投影恢复或校验、实时 Inbox 通知,或 loop 驱动的认领策略时,使用 `mountAgentLoopTestHarness()`。应在挂载先决依赖后、创建 Agent 前挂载所有对加载顺序敏感的消费方。上下文拥有 loop 以及该 harness 返回的每个 Agent。 ```ts import { Context } from '@deepseek-ai/cordis' -import AgentLoop from '@deepseek-ai/dsh-agent-loop' -import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' +import { SessionId, type UserMessage } from '@deepseek-ai/dsh-session' +import { + mountAgentLoopTestDependencies, + mountAgentLoopTestHarness, +} from '@deepseek-ai/dsh-agent-loop-testkit' const ctx = new Context() await mountAgentLoopTestDependencies(ctx) -// Register the test adapter and any optional plugins here. -await ctx.plugin(AgentLoop, { agents: [] }) +// Register the test adapter and any load-order-sensitive plugins here. +const harness = await mountAgentLoopTestHarness(ctx) +const agent = await harness.create(SessionId('test-agent')) +declare const message: UserMessage + +agent.inbox.append('next-turn', message) +const admitted = harness.claim(agent, 'next-turn', 1) ``` -该辅助函数按依赖顺序激活 LLM、会话、系统提示词、工具与 agent 服务,并在 loop 挂载前返回。系统提示词与工具注册表配置可通过 `options` 转发;除服务自有的默认值外,本辅助函数不提供测试默认值。 +依赖辅助函数通过 `options` 转发系统提示词与工具注册表配置,除这些服务自有的默认值外不提供测试默认值。插件加载失败会使辅助函数调用被拒绝;顺序中较早激活的服务仍归上下文所有,并在上下文释放时一并解除。 + +### 构造结构化 Agent 桩 + +当测试对象需要可变的待处理列表,但不测试持久性、投影校验、实时 Inbox 通知或驱动的认领策略时,使用 `createInboxStub()`。该桩通过两个进程内数组实现公开队列操作,且绝不会写入 Session。当测试对象不应访问待处理输入时,使用 `unsupportedInbox()`;每次变更都会在首个意外依赖处抛错。 + +```ts +import { createInboxStub } from '@deepseek-ai/dsh-agent-loop-testkit' + +const agent = { + // ... + inbox: createInboxStub(), +} +``` ### 何时使用 -当测试对象是 loop 本身——在真实先决依赖栈上的加载顺序、重试、工具执行或会话行为——时使用此辅助函数。当测试要探测服务加载顺序、注入失败、部分拓扑或清理时,请直接挂载依赖——辅助函数隐藏的正是这类测试必须控制的接线。 +当测试对象是生产 loop 或持久 Inbox 行为时,使用依赖与 loop 辅助函数。只需要编辑队列的消费方领域测试使用结构化桩。当测试探测服务注入失败或部分拓扑时,请直接挂载依赖,因为辅助函数隐藏的正是这类测试必须控制的接线。 ### 可能出什么问题 -插件加载失败会使辅助函数调用被拒绝;顺序中较早激活的服务仍归你的上下文所有,并随上下文一起解除。上下文拥有所有已挂载服务,因此测试结束后请 dispose(资源释放)它。 +harness 不会挂载任何 LLM 适配器。若测试发送的任务会启动模型请求,请先注册被测路由的适配器。每个测试结束后都应释放所属上下文,使 Agent 达到静止状态并解除其作用域注册。 ----- @@ -59,11 +82,11 @@ await ctx.plugin(AgentLoop, { agents: [] })
实现细节——点击展开 -本节解释辅助函数的设计;可观察行为已在[使用本包](#use-this-package)中完整说明。 +本节解释测试辅助工具的设计;可观察行为已在[使用本包](#use-this-package)中完整说明。 ### 设计 -**运行时不变式:** 不发布伴生入口。本包不持有生产事件流或可变数据;消费它的测试套件会直接检验 harness 行为。 +`mountAgentLoopTestDependencies` 按固定依赖顺序——LLM、会话、会话投影注册表、系统提示词注册表、工具注册表、agent 注册表——挂载六个服务插件,并在 `AgentLoop` 之前停下,使调用方控制 loop 加载顺序。`mountAgentLoopTestHarness` 挂载公开的生产插件,通过其服务创建 Agent,并公开生产驱动的认领操作,而不导出 loop 的具体 Inbox 类或投影定义。[`src/inbox.ts`](src/inbox.ts) 仅包含进程内可变桩和快速失败且不支持操作的占位值;它不持有投影或持久事件实现。挂载与驱动实现位于 [`src/index.ts`](src/index.ts)。本包不发布 invariant companion,因为它只持有测试辅助工具,不存在可能相互偏离的独立生产观测。
@@ -72,11 +95,11 @@ await ctx.plugin(AgentLoop, { agents: [] }) ## 进一步探索 -当包级约定不够用时阅读以下页面。它们从 loop 逐步进入辅助函数挂载的服务以及使用它的测试。 +当包级行为不够用时阅读以下页面。它们从 loop 逐步进入辅助函数挂载的服务以及使用它的测试。 -- [Agent loop 包](../../core/agent-loop/README.zh.md)——本辅助函数为之准备测试的具体 loop。 -- [会话包](../../core/session/README.zh.md)——辅助函数挂载的会话存储。 -- [LLM 包](../../llm/llm/README.zh.md)——辅助函数挂载的 LLM 运行时与适配器约定。 +- [Agent loop 包](../../core/agent-loop/README.zh.md)——本辅助函数为生产行为挂载的具体 loop。 +- [会话包](../../core/session/README.zh.md)——生产 Inbox 行为使用的持久事件日志。 +- [LLM 包](../../llm/llm/README.zh.md)——本辅助函数准备的 LLM 运行时与适配器接口。 - [测试策略](../../../docs/testing.zh.md)——这些测试所服务的覆盖层级。 - [test-support 组地图](../README.zh.md)——兄弟 harness 与支持包。 @@ -85,20 +108,22 @@ await ctx.plugin(AgentLoop, { agents: [] }) ## 模型体验 -无。该测试专用组合辅助函数既不驱动也不修改模型请求。 +无。这些测试专用辅助工具既不组装也不修改模型请求。 #### KV Cache 影响 -无;本包既不组装也不发送提供方请求。 +无;本包自身不发送提供方请求。 ## 已知限制与延期工作 +这些限制说明辅助工具不共享什么。它们是当前包约束,不是任务积压。 -这些限制说明辅助函数不共享什么。它们是当前包约束,不是任务积压。 - -- **只共享必需的先决主干**——适配器、可选插件、`AgentLoop`、agent 与上下文清理仍由调用方负责,以使特定场景的挂载顺序清晰可见。 +- **只共享必需的先决主干**——适配器、可选插件、场景特定的加载顺序与上下文清理仍由调用方负责。 +- **生产 harness 没有适配器默认值**——启动 loop 的测试必须注册其实际使用的路由。 +- **可变 Inbox 桩仅存在于进程内**——只要持久事件、投影恢复或校验、实时通知或认领策略属于测试对象,就应使用 harness 创建的 Agent。 +- **不支持操作的 Inbox 不接受变更**——只要待处理输入属于测试对象,就应使用可变桩或 harness 创建的 Agent。 ### 开发备注 diff --git a/packages/test-support/agent-loop-testkit/package.json b/packages/test-support/agent-loop-testkit/package.json index 2ea2941896..fc123b8bac 100644 --- a/packages/test-support/agent-loop-testkit/package.json +++ b/packages/test-support/agent-loop-testkit/package.json @@ -1,6 +1,6 @@ { "name": "@deepseek-ai/dsh-agent-loop-testkit", - "description": "Shared prerequisite mounting for tests that exercise the concrete agent loop", + "description": "Prerequisite mounting, production AgentLoop drivers, and Inbox stubs for tests", "version": "0.1.3-alpha.2", "publishConfig": { "access": "public" @@ -28,17 +28,21 @@ "license": "MIT", "peerDependencies": { "@deepseek-ai/dsh-agent": "workspace:^", + "@deepseek-ai/dsh-agent-loop": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", + "@deepseek-ai/dsh-session-projection": "workspace:^", "@deepseek-ai/dsh-system-prompt": "workspace:^", "@deepseek-ai/dsh-tools": "workspace:^", "@deepseek-ai/cordis": "workspace:^" }, + "dependencies": {}, "devDependencies": { "@deepseek-ai/dsh-agent": "workspace:^", "@deepseek-ai/dsh-agent-loop": "workspace:^", "@deepseek-ai/dsh-llm": "workspace:^", "@deepseek-ai/dsh-session": "workspace:^", + "@deepseek-ai/dsh-session-projection": "workspace:^", "@deepseek-ai/dsh-system-prompt": "workspace:^", "@deepseek-ai/dsh-tools": "workspace:^", "@deepseek-ai/cordis": "workspace:^" diff --git a/packages/test-support/agent-loop-testkit/src/inbox.ts b/packages/test-support/agent-loop-testkit/src/inbox.ts new file mode 100644 index 0000000000..dbb1dbff57 --- /dev/null +++ b/packages/test-support/agent-loop-testkit/src/inbox.ts @@ -0,0 +1,74 @@ +import type { Inbox, InboxTarget } from '@deepseek-ai/dsh-agent' +import type { MessageId } from '@deepseek-ai/dsh-llm' +import type { UserMessage } from '@deepseek-ai/dsh-session' + +/** + * Create a mutable in-memory Inbox stub for tests that exercise only the public + * queue operations. Durable events, projection validation, and live Inbox + * notifications require a real Agent created by the AgentLoop test harness. + * @returns an Inbox backed by two process-local arrays. + */ +export function createInboxStub(): Inbox { + const pending: Record = { + 'next-turn': [], + 'next-step': [], + } + + const locate = (messageId: MessageId): { target: InboxTarget; index: number } | undefined => { + for (const target of ['next-turn', 'next-step'] as const) { + const index = pending[target].findIndex(message => message.id === messageId) + if (index >= 0) return { target, index } + } + return undefined + } + + return { + get nextTurn() { return pending['next-turn'] }, + get nextStep() { return pending['next-step'] }, + clear() { + pending['next-step'].splice(0) + pending['next-turn'].splice(0) + }, + append(target, message) { + pending[target].push(message) + }, + prepend(target, message) { + pending[target].unshift(message) + }, + replace(messageId, message) { + const location = locate(messageId) + if (location === undefined) return false + pending[location.target].splice(location.index, 1, message) + return true + }, + remove(messageId) { + const location = locate(messageId) + if (location === undefined) return false + pending[location.target].splice(location.index, 1) + return true + }, + splice(target, start, deleteCount, inserted) { + return pending[target].splice(start, deleteCount, ...inserted) + }, + } +} + +/** + * Create an unsupported Inbox placeholder for Agent stubs whose tests do not exercise Inbox behavior. + * @returns an Inbox whose pending lists are empty and whose mutation methods throw. + */ +export function unsupportedInbox(): Inbox { + const rejectMutation = (): never => { + throw new Error('this test Agent does not support Inbox mutations') + } + return { + nextTurn: [], + nextStep: [], + clear: rejectMutation, + append: rejectMutation, + prepend: rejectMutation, + replace: rejectMutation, + remove: rejectMutation, + splice: rejectMutation, + } +} diff --git a/packages/test-support/agent-loop-testkit/src/index.ts b/packages/test-support/agent-loop-testkit/src/index.ts index e2ae19653d..0e9b57e666 100644 --- a/packages/test-support/agent-loop-testkit/src/index.ts +++ b/packages/test-support/agent-loop-testkit/src/index.ts @@ -1,19 +1,49 @@ /** - * Shared mounting for the services required before tests load the concrete - * agent loop. The caller retains ownership of the context, loop, adapters, - * optional plugins, and teardown. + * Shared service mounting, real AgentLoop drivers, and structural Inbox stubs + * for agent-loop tests. Callers retain ownership of their contexts, adapters, + * optional plugins, agents, and teardown. * @module @deepseek-ai/dsh-agent-loop-testkit */ import type { Context } from '@deepseek-ai/cordis' import AgentRegistry from '@deepseek-ai/dsh-agent' +import type { Agent, AgentOptions, Inbox, InboxTarget } from '@deepseek-ai/dsh-agent' +import AgentLoop from '@deepseek-ai/dsh-agent-loop' import LlmRuntime from '@deepseek-ai/dsh-llm' import SessionStore from '@deepseek-ai/dsh-session' +import type { SessionHeader, SessionId, UserMessage } from '@deepseek-ai/dsh-session' +import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import type { Config as SystemPromptConfig } from '@deepseek-ai/dsh-system-prompt' import ToolRuntime from '@deepseek-ai/dsh-tools' import type { Config as ToolRuntimeConfig } from '@deepseek-ai/dsh-tools' +export { createInboxStub, unsupportedInbox } from './inbox.ts' + +interface DriverInbox extends Inbox { + claim(target: InboxTarget, turn: number): UserMessage[] +} + +/** Test driver for production Agents created by a mounted AgentLoop. */ +export interface AgentLoopTestHarness { + /** + * Create a production Agent and fresh Session owned by the harness context. + * @param id - shared Agent and Session identity. + * @param options - concrete loop options. + * @param meta - optional fresh-session workspace metadata. + * @returns the published production Agent after creation completes. + */ + create(id: SessionId, options?: AgentOptions, meta?: Pick): Promise + /** + * Admit pending messages through the production loop driver's claim operation. + * @param agent - Agent returned by this harness's `create` method. + * @param target - boundary whose pending input is admitted. + * @param turn - turn that owns the admitted messages. + * @returns next-step messages followed by one next-turn message when requested. + */ + claim(agent: Agent, target: InboxTarget, turn: number): UserMessage[] +} + /** Configuration forwarded to the prerequisite service plugins. */ export interface AgentLoopTestDependenciesOptions { /** Configuration for the system-prompt registry. */ @@ -40,7 +70,24 @@ export async function mountAgentLoopTestDependencies( ): Promise { await ctx.plugin(LlmRuntime) await ctx.plugin(SessionStore) + await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(SystemPrompt, options.systemPrompt ?? {}) await ctx.plugin(ToolRuntime, options.tools ?? {}) await ctx.plugin(AgentRegistry) } + +/** + * Mount the production AgentLoop and expose its narrow test-driver operations. + * Mount {@link mountAgentLoopTestDependencies} and any load-order-sensitive + * consumers before calling this helper. The context owns the loop and every + * Agent returned by the harness. + * @param ctx - test context with the AgentLoop prerequisite services active. + * @returns a driver that creates production Agents and claims their real Inbox. + */ +export async function mountAgentLoopTestHarness(ctx: Context): Promise { + await ctx.plugin(AgentLoop, { agents: [] }) + return { + create: async (id, options = {}, meta = {}) => ctx.agentLoop.create(id, options, meta), + claim: (agent, target, turn) => (agent.inbox as DriverInbox).claim(target, turn), + } +} diff --git a/packages/test-support/agent-loop-testkit/tests/agent-loop-testkit.spec.ts b/packages/test-support/agent-loop-testkit/tests/agent-loop-testkit.spec.ts index c715ac03cc..f4235aad18 100644 --- a/packages/test-support/agent-loop-testkit/tests/agent-loop-testkit.spec.ts +++ b/packages/test-support/agent-loop-testkit/tests/agent-loop-testkit.spec.ts @@ -1,11 +1,29 @@ import { describe, expect, it } from 'vitest' import { Context } from '@deepseek-ai/cordis' -import AgentLoop from '@deepseek-ai/dsh-agent-loop' +import { createUserMessage } from '@deepseek-ai/dsh-llm' +import { Session, SessionId, SessionSeq } from '@deepseek-ai/dsh-session' import { renderPrompt } from '@deepseek-ai/dsh-system-prompt' -import { mountAgentLoopTestDependencies } from '../src/index.ts' +import { + createInboxStub, + mountAgentLoopTestDependencies, + mountAgentLoopTestHarness, + unsupportedInbox, +} from '../src/index.ts' + +function message(text: string) { + return createUserMessage({ content: [{ type: 'text', text }], source: { kind: 'user' } }) +} describe('dsh-agent-loop-testkit', () => { - it('mounts a configurable prerequisite spine that can activate AgentLoop', async () => { + it('rejects mutations through an unsupported Agent stub Inbox', () => { + const inbox = unsupportedInbox() + + expect(inbox.nextTurn).toEqual([]) + expect(inbox.nextStep).toEqual([]) + expect(() => { inbox.clear() }).toThrow('this test Agent does not support Inbox mutations') + }) + + it('mounts a configurable prerequisite spine and the production AgentLoop', async () => { const ctx = new Context() await mountAgentLoopTestDependencies(ctx, { systemPrompt: { personaPrefix: 'Test persona.' }, @@ -13,7 +31,77 @@ describe('dsh-agent-loop-testkit', () => { }) expect(renderPrompt(await ctx.systemPrompt.assemble())).toContain('Test persona.') - await expect(ctx.plugin(AgentLoop, { agents: [] })).resolves.toBeDefined() + await expect(mountAgentLoopTestHarness(ctx)).resolves.toBeDefined() + + await ctx.fiber.dispose() + }) + + it('provides a mutable in-memory Inbox stub for structural Agent tests', () => { + const inbox = createInboxStub() + const firstTurn = message('first turn') + const secondTurn = message('second turn') + const firstStep = message('first step') + const editedTurn = message('edited turn') + const editedStep = message('edited step') + + inbox.append('next-turn', firstTurn) + inbox.prepend('next-turn', secondTurn) + inbox.append('next-step', firstStep) + expect(inbox.nextTurn).toEqual([secondTurn, firstTurn]) + expect(inbox.nextStep).toEqual([firstStep]) + + expect(inbox.replace(firstTurn.id, editedTurn)).toBe(true) + expect(inbox.replace(firstStep.id, editedStep)).toBe(true) + expect(inbox.replace(firstTurn.id, message('missing replacement'))).toBe(false) + expect(inbox.remove(firstTurn.id)).toBe(false) + expect(inbox.splice('next-turn', -1, 1, [])).toEqual([editedTurn]) + expect(inbox.remove(editedStep.id)).toBe(true) + + inbox.clear() + expect(inbox.nextTurn).toEqual([]) + expect(inbox.nextStep).toEqual([]) + }) + + it('drives durable Inbox behavior through a production Agent', async () => { + const ctx = new Context() + await mountAgentLoopTestDependencies(ctx) + const harness = await mountAgentLoopTestHarness(ctx) + const agent = await harness.create(SessionId('agent-loop-testkit-inbox')) + const turn = message('turn') + const step = message('step') + const inserted: string[] = [] + const claimed: Array<{ id: string; turn: number }> = [] + ctx.on('agent/inbox/inserted', ({ agent: subject, message: pending }) => { + if (subject === agent) inserted.push(pending.id) + }) + ctx.on('agent/inbox/claimed', ({ agent: subject, message: pending, turn: ownerTurn }) => { + if (subject === agent) claimed.push({ id: pending.id, turn: ownerTurn }) + }) + + agent.inbox.append('next-turn', turn) + agent.inbox.append('next-step', step) + + expect(inserted).toEqual([turn.id, step.id]) + expect(() => { agent.inbox.append('next-step', turn) }).toThrow(`message "${turn.id}" is already pending`) + const invalid = Session.create(SessionId('invalid-persisted-inbox'), [{ + type: 'agent/inbox/spliced', + seq: SessionSeq(0), + time: 1, + data: { target: 'next-turn', start: 99, inserted: [] }, + }]) + expect(() => ctx.sessionProjections.stateOf(invalid, 'inbox')) + .toThrow(/invalid persisted inbox splice/) + expect(harness.claim(agent, 'next-turn', 3)).toEqual([step, turn]) + expect(claimed).toEqual([ + { id: step.id, turn: 3 }, + { id: turn.id, turn: 3 }, + ]) + expect(agent.session.snapshotEvents().map(event => event.type)).toEqual([ + 'agent/inbox/spliced', + 'agent/inbox/spliced', + 'agent/inbox/spliced', + 'agent/inbox/spliced', + ]) await ctx.fiber.dispose() }) diff --git a/packages/test-support/agent-loop-testkit/tsconfig.json b/packages/test-support/agent-loop-testkit/tsconfig.json index 5e5b3c47f2..1a07c30bd3 100644 --- a/packages/test-support/agent-loop-testkit/tsconfig.json +++ b/packages/test-support/agent-loop-testkit/tsconfig.json @@ -17,6 +17,9 @@ { "path": "../../core/agent" }, + { + "path": "../../core/agent-loop" + }, { "path": "../../llm/llm" }, @@ -28,6 +31,9 @@ }, { "path": "../../core/tools" + }, + { + "path": "../../session/session-projection" } ] } diff --git a/packages/todo/tool-todo/tests/integration.spec.ts b/packages/todo/tool-todo/tests/integration.spec.ts index 696a1ef99e..3384372a65 100644 --- a/packages/todo/tool-todo/tests/integration.spec.ts +++ b/packages/todo/tool-todo/tests/integration.spec.ts @@ -5,7 +5,6 @@ import { SessionId, type SessionEvent } from '@deepseek-ai/dsh-session' import type { Agent } from '@deepseek-ai/dsh-agent' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import * as ToolTodo from '@deepseek-ai/dsh-tool-todo' import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' @@ -18,7 +17,6 @@ import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent async function harness(adapter: MockAdapter): Promise { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(ToolTodo, { allowParallelInProgress: true }) ctx.llm.registerAdapter(['mock'], adapter) diff --git a/packages/todo/tool-todo/tests/loader-composition.spec.ts b/packages/todo/tool-todo/tests/loader-composition.spec.ts index d25d02bba9..b99909fa2a 100644 --- a/packages/todo/tool-todo/tests/loader-composition.spec.ts +++ b/packages/todo/tool-todo/tests/loader-composition.spec.ts @@ -11,12 +11,13 @@ import Loader from '@deepseek-ai/cordis-plugin-loader' import Include from '@deepseek-ai/cordis-plugin-include' import { ToolCallId } from '@deepseek-ai/dsh-llm' import { Session, SessionId } from '@deepseek-ai/dsh-session' -import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent' +import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent } from '@deepseek-ai/dsh-agent' import SystemPrompt from '@deepseek-ai/dsh-system-prompt' import ToolRuntime from '@deepseek-ai/dsh-tools' import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import * as ToolTodo from '@deepseek-ai/dsh-tool-todo' +import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit' let root: string | undefined let context: Context | undefined @@ -33,7 +34,7 @@ function agent(ctx: Context): Agent { const id = SessionId('todo-loader-agent') const session = Session.create(id) const value: Agent = { - id, options: {}, session, inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), + id, options: {}, session, inbox: unsupportedInbox(), status: 'idle', ctx: scope.ctx, followup: () => {}, steer: () => {}, inject: () => {}, send: () => {}, cancel() {}, runMaintenance: task => task(new AbortController().signal), diff --git a/packages/workflow/tool-ralph/tests/integration.spec.ts b/packages/workflow/tool-ralph/tests/integration.spec.ts index 6d14949aba..6293e0ff55 100644 --- a/packages/workflow/tool-ralph/tests/integration.spec.ts +++ b/packages/workflow/tool-ralph/tests/integration.spec.ts @@ -5,7 +5,6 @@ import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import { createUserMessage, ToolCallId } from '@deepseek-ai/dsh-llm' import { SessionId } from '@deepseek-ai/dsh-session' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import SubagentRuntime from '@deepseek-ai/dsh-subagent' import { STRUCTURED_OUTPUT_TOOL } from '@deepseek-ai/dsh-subagent-in-process-driver' import * as spawn from '@deepseek-ai/dsh-subagent-spawn-in-process' @@ -21,7 +20,6 @@ async function mountRalph(script: MockScript, config: toolRalph.Config) { const ctx = new Context() const adapter = new MockAdapter(script) await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(SubagentRuntime) await ctx.plugin(spawn, { providerName: 'spawn' }) @@ -59,7 +57,6 @@ describe('dsh-tool-ralph over the real spawn and worker-thread stack', () => { toolCallResponse('round-2', STRUCTURED_OUTPUT_TOOL, finalReport), ]) await mountAgentLoopTestDependencies(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(SubagentRuntime) await ctx.plugin(spawn, { providerName: 'spawn' }) diff --git a/packages/workflow/workflow-worker-thread/tests/integration.spec.ts b/packages/workflow/workflow-worker-thread/tests/integration.spec.ts index ed67dccc0a..06bbdd277e 100644 --- a/packages/workflow/workflow-worker-thread/tests/integration.spec.ts +++ b/packages/workflow/workflow-worker-thread/tests/integration.spec.ts @@ -7,7 +7,6 @@ import InvariantRegistry from '@deepseek-ai/dsh-invariants' import * as SessionInvariant from '@deepseek-ai/dsh-session/invariant' import * as AgentInvariant from '@deepseek-ai/dsh-agent/invariant' import * as AgentLoopInvariant from '@deepseek-ai/dsh-agent-loop/invariant' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' import SubagentRuntime from '@deepseek-ai/dsh-subagent' import * as spawn from '@deepseek-ai/dsh-subagent-spawn-in-process' import { STRUCTURED_OUTPUT_TOOL } from '@deepseek-ai/dsh-subagent-in-process-driver' @@ -36,7 +35,6 @@ async function setup(script: Script) { const adapter = new MockAdapter(script) await mountAgentLoopTestDependencies(ctx) await mountInvariants(ctx) - await ctx.plugin(SessionProjectionRegistry) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(SubagentRuntime) await ctx.plugin(spawn, { providerName: 'spawn' }) diff --git a/packages/workflow/workflow-worker-thread/tests/workflow-worker-thread.e2e.ts b/packages/workflow/workflow-worker-thread/tests/workflow-worker-thread.e2e.ts index daf7516ca2..d9a118c7bb 100644 --- a/packages/workflow/workflow-worker-thread/tests/workflow-worker-thread.e2e.ts +++ b/packages/workflow/workflow-worker-thread/tests/workflow-worker-thread.e2e.ts @@ -1,13 +1,9 @@ import { afterEach, describe, expect, it } from 'vitest' import { Context } from '@deepseek-ai/cordis' -import LlmRuntime from '@deepseek-ai/dsh-llm' -import SessionStore, { SessionId } from '@deepseek-ai/dsh-session' -import SystemPrompt from '@deepseek-ai/dsh-system-prompt' -import ToolRuntime from '@deepseek-ai/dsh-tools' -import AgentRegistry from '@deepseek-ai/dsh-agent' +import { SessionId } from '@deepseek-ai/dsh-session' import AgentLoop from '@deepseek-ai/dsh-agent-loop' -import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection' +import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek' import SubagentRuntime from '@deepseek-ai/dsh-subagent' import * as Spawn from '@deepseek-ai/dsh-subagent-spawn-in-process' @@ -31,12 +27,7 @@ afterEach(async () => { async function harness(): Promise { const built = new Context() - await built.plugin(LlmRuntime) - await built.plugin(SessionStore) - await built.plugin(SessionProjectionRegistry) - await built.plugin(SystemPrompt) - await built.plugin(ToolRuntime) - await built.plugin(AgentRegistry) + await mountAgentLoopTestDependencies(built) await built.plugin(AgentLoop, { agents: [] }) await built.plugin(LlmDeepSeek) await built.plugin(SubagentRuntime) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e53bb78fd8..43de75d65a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -356,6 +356,12 @@ importers: '@deepseek-ai/dsh-agent': specifier: workspace:^ version: link:../../packages/core/agent + '@deepseek-ai/dsh-agent-loop': + specifier: workspace:^ + version: link:../../packages/core/agent-loop + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../packages/test-support/agent-loop-testkit '@deepseek-ai/dsh-attachment-local': specifier: workspace:^ version: link:../../packages/attachment/attachment-local @@ -962,6 +968,12 @@ importers: '@deepseek-ai/dsh-agent-default-model': specifier: workspace:^ version: link:../../core/agent-default-model + '@deepseek-ai/dsh-agent-loop': + specifier: workspace:^ + version: link:../../core/agent-loop + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-agent-presets': specifier: workspace:^ version: link:../../preset/agent-presets @@ -1521,6 +1533,12 @@ importers: '@deepseek-ai/dsh-agent-default-model': specifier: workspace:^ version: link:../../core/agent-default-model + '@deepseek-ai/dsh-agent-loop': + specifier: workspace:^ + version: link:../../core/agent-loop + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm @@ -4310,6 +4328,9 @@ importers: '@deepseek-ai/dsh-agent-loop': specifier: workspace:^ version: link:../../core/agent-loop + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-fs': specifier: workspace:^ version: link:../../fs/fs @@ -4524,6 +4545,9 @@ importers: '@deepseek-ai/dsh-agent': specifier: workspace:^ version: link:../../core/agent + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm @@ -4569,6 +4593,9 @@ importers: '@deepseek-ai/dsh-typert-registry': specifier: workspace:^ version: link:../../typert/registry + '@deepseek-ai/dsh-util-values': + specifier: workspace:^ + version: link:../../util/values packages/core/agent-default-model: dependencies: @@ -4839,6 +4866,9 @@ importers: '@deepseek-ai/dsh-agent': specifier: workspace:^ version: link:../../core/agent + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-app-boot': specifier: workspace:^ version: link:../../boot/app-boot @@ -5450,6 +5480,9 @@ importers: '@deepseek-ai/dsh-agent': specifier: workspace:^ version: link:../../core/agent + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-anonymous-user-id': specifier: workspace:^ version: link:../../identity/anonymous-user-id @@ -5686,6 +5719,9 @@ importers: '@deepseek-ai/dsh-agent': specifier: workspace:^ version: link:../../core/agent + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-fs': specifier: workspace:^ version: link:../fs @@ -5731,6 +5767,12 @@ importers: '@deepseek-ai/dsh-agent': specifier: workspace:^ version: link:../../core/agent + '@deepseek-ai/dsh-agent-loop': + specifier: workspace:^ + version: link:../../core/agent-loop + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-commands': specifier: workspace:^ version: link:../../interaction/commands @@ -5762,6 +5804,12 @@ importers: '@deepseek-ai/dsh-agent': specifier: workspace:^ version: link:../../core/agent + '@deepseek-ai/dsh-agent-loop': + specifier: workspace:^ + version: link:../../core/agent-loop + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-bash-local': specifier: workspace:^ version: link:../../shell/bash-local @@ -5853,6 +5901,9 @@ importers: '@deepseek-ai/dsh-agent-loop': specifier: workspace:^ version: link:../../core/agent-loop + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-goal': specifier: workspace:^ version: link:../goal @@ -6376,6 +6427,9 @@ importers: '@deepseek-ai/dsh-agent': specifier: workspace:^ version: link:../../core/agent + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-brand': specifier: workspace:^ version: link:../../util/brand @@ -8214,6 +8268,9 @@ importers: '@deepseek-ai/dsh-agent': specifier: workspace:^ version: link:../../core/agent + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm @@ -8330,6 +8387,9 @@ importers: '@deepseek-ai/dsh-agent': specifier: workspace:^ version: link:../../core/agent + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm @@ -8432,6 +8492,9 @@ importers: '@deepseek-ai/dsh-agent': specifier: workspace:^ version: link:../../core/agent + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-llm': specifier: workspace:^ version: link:../../llm/llm @@ -9253,6 +9316,9 @@ importers: '@deepseek-ai/dsh-agent': specifier: workspace:^ version: link:../../core/agent + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-brand': specifier: workspace:^ version: link:../../util/brand @@ -9281,6 +9347,9 @@ importers: '@deepseek-ai/dsh-agent': specifier: workspace:^ version: link:../../core/agent + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-sandbox': specifier: workspace:^ version: link:../../sandbox/sandbox @@ -9318,6 +9387,9 @@ importers: '@deepseek-ai/dsh-agent': specifier: workspace:^ version: link:../../core/agent + '@deepseek-ai/dsh-agent-loop-testkit': + specifier: workspace:^ + version: link:../../test-support/agent-loop-testkit '@deepseek-ai/dsh-jobs': specifier: workspace:^ version: link:../../jobs/jobs @@ -9378,6 +9450,9 @@ importers: '@deepseek-ai/dsh-session': specifier: workspace:^ version: link:../../core/session + '@deepseek-ai/dsh-session-projection': + specifier: workspace:^ + version: link:../../session/session-projection '@deepseek-ai/dsh-system-prompt': specifier: workspace:^ version: link:../../core/system-prompt @@ -10575,6 +10650,9 @@ importers: '@deepseek-ai/dsh-util-time': specifier: workspace:^ version: link:../../packages/util/time + '@deepseek-ai/dsh-util-values': + specifier: workspace:^ + version: link:../../packages/util/values '@deepseek-ai/dsh-web': specifier: workspace:^ version: link:../../packages/web/web diff --git a/python/sdk-runtime/package.json b/python/sdk-runtime/package.json index 97c1a6bc68..d34b14bec4 100644 --- a/python/sdk-runtime/package.json +++ b/python/sdk-runtime/package.json @@ -121,6 +121,7 @@ "@deepseek-ai/dsh-user-approval": "workspace:^", "@deepseek-ai/dsh-user-questions": "workspace:^", "@deepseek-ai/dsh-util-time": "workspace:^", + "@deepseek-ai/dsh-util-values": "workspace:^", "@deepseek-ai/dsh-web": "workspace:^", "@deepseek-ai/dsh-web-fetch-http": "workspace:^", "@deepseek-ai/dsh-web-search-deepseek": "workspace:^", diff --git a/scripts/gen-cordis-catalog.ts b/scripts/gen-cordis-catalog.ts index 7bf8205c80..2300ddf7b3 100644 --- a/scripts/gen-cordis-catalog.ts +++ b/scripts/gen-cordis-catalog.ts @@ -253,6 +253,7 @@ export const LINK_MAP: Readonly> = { ContentBlock: 'llm-streaming.md', CreateAgentOptions: 'core.md', GenerateOptions: 'llm-streaming.md', + Inbox: 'core.md', InboxItem: 'core.md', InboxPlacement: 'core.md', InspectorJsonValue: 'extensions.md', diff --git a/scripts/type-equiv.manifest.json b/scripts/type-equiv.manifest.json index f929c88512..c843a901ed 100644 --- a/scripts/type-equiv.manifest.json +++ b/scripts/type-equiv.manifest.json @@ -116,6 +116,11 @@ "symbol": "LlmCallConfigAdapterDefaults", "source": "packages/llm/llm/src/call-config.ts" }, + { + "doc": "docs/subsystems/core.md", + "symbol": "Inbox", + "source": "packages/core/agent/src/runtime-types.ts" + }, { "doc": "docs/subsystems/core.md", "symbol": "InboxTarget",