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 1b529c379b..8e2fc94bf2 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: 737e3835263a3215a0fd2e52dad4ee05402bd888 -2026-07-31-claimed-pre-step-inbox-lifecycle.zh.md: ecb731df663e0d48b374a3118d7db7f6a34bfc18 +2026-07-31-claimed-pre-step-inbox-lifecycle.md: de58747423fe71a7f04cd9354d39e7eb215f048e +2026-07-31-claimed-pre-step-inbox-lifecycle.zh.md: 9103e5c6f5197ecb74269cde249d610c0396354f 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 737e383526..de58747423 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 @@ -18,13 +18,13 @@ Before every proposed step, the loop's package-internal `ReactLoopInbox` atomica 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. -`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. +`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 reads the definition registered by `AgentLoop` at service activation. This service lifetime keeps cold Inbox reads available without any live Agent. -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. +The two event surfaces have separate consumers. Observers following one message use `agent/inbox/inserted`, `claimed`, and `discarded`. `AgentLoop` contributes the standard `inbox` projection over the durable `agent/inbox/spliced` stream; 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 generic projection change feed and read the complete Inbox value directly. 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` 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. +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 `AgentLoop` 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 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 ecb731df66..9103e5c6f5 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 @@ -18,13 +18,13 @@ Status: implemented 持久 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` 只暴露用于读取和变更待处理工作的结构化 `Inbox` 接口;仅供循环使用的 `hasPending` 与领取操作不在该公开接口上。dsh-agent-loop 只构造一个 `ReactLoopInbox`,同时用于结构化命令与驱动器操作。具体构造函数直接接收 `SessionProjectionRegistry`,而不是更宽泛的 Cordis `Context`,并在首次读取前从 agent 作用域注册标准定义。`AgentLoop` 激活时要求该注册表服务存在,注册表则对多个 live agent 作用域贡献的定义进行引用计数。 +`Agent.inbox` 只暴露用于读取和变更待处理工作的结构化 `Inbox` 接口;仅供循环使用的 `hasPending` 与领取操作不在该公开接口上。dsh-agent-loop 只构造一个 `ReactLoopInbox`,同时用于结构化命令与驱动器操作。具体构造函数直接接收 `SessionProjectionRegistry`,而不是更宽泛的 Cordis `Context`,并读取 `AgentLoop` 在服务激活时注册的定义。该服务生命周期让没有 live Agent 时的冷 Inbox 读取仍然可用。 -两类事件接口服务不同消费方。跟踪单条消息的观察方使用 `agent/inbox/inserted`、`claimed` 与 `discarded`。每个 `ReactLoopInbox` 都从其 agent 作用域在持久 `agent/inbox/spliced` 流上贡献标准 `inbox` 投影;UI 编辑与移除通过 Inbox 变更方法处理,从而让同一投影记录所有变化。该投影重建持久历史时,会拒绝不安全或越界的坐标,以及跨两份列表重复的 `MessageId`,并报告出错事件的 seq。整体队列的 control 消费方使用投影变更流:Session controller 先发布 projection frame,再从同一份折叠后的 inbox 值派生 queue replacement。 +两类事件接口服务不同消费方。跟踪单条消息的观察方使用 `agent/inbox/inserted`、`claimed` 与 `discarded`。`AgentLoop` 在持久 `agent/inbox/spliced` 流上贡献标准 `inbox` 投影;UI 编辑与移除通过 Inbox 变更方法处理,从而让同一投影记录所有变化。该投影重建持久历史时,会拒绝不安全或越界的坐标,以及跨两份列表重复的 `MessageId`,并报告出错事件的 seq。整体队列的 control 消费方使用通用投影变更流,直接读取完整的 Inbox 值。 必须对当前步骤进行原子改写的插件从 `agent/pre-step` 返回消息。只需要稍后上下文的插件可以直接修改 `agent.inbox`。Workspace context 同时使用两条路径:异步文件系统投影会暂存一条可替换的 `next-step` 消息,而下一次进入步骤的 pre-step 会把该消息或新组合的基线折入最终批次,并移除仍待处理的副本。reject 会让该条目继续排队。 -已归档的[可寻址队列项决策](../../archived/feature/2026-07-29-addressable-queue-operations.md)描述了已被取代的单次出现包装层设计。`MessageId` 负责寻址,而 `ReactLoopInbox` 把 `inbox` 作为持久 splice 上的标准会话投影贡献给投影注册表。通用投影传输层会将该折叠结果用于实时更新、历史尾页的重连基线和冷进程重启恢复,无需 live Agent 镜像。 +已归档的[可寻址队列项决策](../../archived/feature/2026-07-29-addressable-queue-operations.md)描述了已被取代的单次出现包装层设计。`MessageId` 负责寻址,而 `AgentLoop` 把 `inbox` 作为持久 splice 上的标准会话投影贡献给投影注册表。通用投影传输层会将该折叠结果用于实时更新、历史尾页的重连基线和冷进程重启恢复,无需 live Agent 镜像。 ## 曾考虑的替代方案 diff --git a/.agents/notes/implemented/bug-fix/2026-08-17-durable-web-queue-recovery.i18n.yaml b/.agents/notes/implemented/bug-fix/2026-08-17-durable-web-queue-recovery.i18n.yaml index fd94133c62..96ad0e6d2a 100644 --- a/.agents/notes/implemented/bug-fix/2026-08-17-durable-web-queue-recovery.i18n.yaml +++ b/.agents/notes/implemented/bug-fix/2026-08-17-durable-web-queue-recovery.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/bug-fix/2026-08-17-durable-web-queue-recovery.md -2026-08-17-durable-web-queue-recovery.md: feea3b9c02fe5ce81bb6a8a453e83db658616ecd -2026-08-17-durable-web-queue-recovery.zh.md: 35c5bea725bb632a587aba407535192ccb5eca29 +2026-08-17-durable-web-queue-recovery.md: ffc699c7bc793da10e1f261be9ff6f7cfe079c59 +2026-08-17-durable-web-queue-recovery.zh.md: 789131c3ddfa944e43fccd71ac2ad4c39a418882 diff --git a/.agents/notes/implemented/bug-fix/2026-08-17-durable-web-queue-recovery.md b/.agents/notes/implemented/bug-fix/2026-08-17-durable-web-queue-recovery.md index feea3b9c02..ffc699c7bc 100644 --- a/.agents/notes/implemented/bug-fix/2026-08-17-durable-web-queue-recovery.md +++ b/.agents/notes/implemented/bug-fix/2026-08-17-durable-web-queue-recovery.md @@ -12,9 +12,9 @@ A reconnect-only repair would retain two recovery implementations: one for a liv ## Decision -`AgentRegistry` registers the standard `inbox` projection whenever the Session projection registry is composed. The projection folds normalized `agent/inbox/spliced` operations from the complete session log into the complete `{ 'next-turn', 'next-step' }` state and exposes that same state as its value. A fork therefore projects pending Inbox operations inherited in its seed, like every other projection. Its schema and `InboxState` type have one definition; message values rely on the existing typed `UserMessage` contract rather than a second runtime message validator. +`AgentLoop` registers the standard `inbox` projection whenever the Session projection registry is composed. The projection folds normalized `agent/inbox/spliced` operations from the complete session log into the complete `{ 'next-turn', 'next-step' }` state and exposes that same state as its value. A fork therefore projects pending Inbox operations inherited in its seed, like every other projection. Its schema and `InboxState` type have one definition; message values rely on the existing typed `UserMessage` contract rather than a second runtime message validator. -The projection registry owns both durable reconstruction and live state. Its existing `session/event` drive folds each committed splice before `Session.append()` returns. Each Agent's `Inbox` command facade reads that same registry state. Inbox normalizes splice coordinates and checks pending `MessageId` uniqueness before append. It does not traverse `session.events`, copy the folded arrays, or apply the committed transition again. It emits the per-message `inserted`, `discarded`, and `claimed` live notifications owned by its mutations. +The projection registry owns both durable reconstruction and live state. Its existing `session/event` drive folds each committed splice before `Session.append()` returns. Each Agent's `ReactLoopInbox` command facade reads that same registry state. Inbox normalizes splice coordinates and checks pending `MessageId` uniqueness before append. It does not traverse `session.events`, copy the folded arrays, or apply the committed transition again. It emits the per-message `inserted`, `discarded`, and `claimed` live notifications owned by its mutations. The generic session-projection carrier is the only Web transport. It sends higher-seq `session/projection` values, includes the complete values block on history tail pages, folds detached cold logs, and uses the projection cache when valid. There is no Host-owned `queue` projection, placement vocabulary, handoff list, dedicated queue frame, or live-Agent reconnect enumeration. diff --git a/.agents/notes/implemented/bug-fix/2026-08-17-durable-web-queue-recovery.zh.md b/.agents/notes/implemented/bug-fix/2026-08-17-durable-web-queue-recovery.zh.md index 35c5bea725..789131c3dd 100644 --- a/.agents/notes/implemented/bug-fix/2026-08-17-durable-web-queue-recovery.zh.md +++ b/.agents/notes/implemented/bug-fix/2026-08-17-durable-web-queue-recovery.zh.md @@ -12,9 +12,9 @@ Inbox 接受消息时会记录规范化的 `agent/inbox/spliced` 事件,但 We ## 决策 -`AgentRegistry` 会在 Session projection registry 已组合时注册标准 `inbox` 投影。该投影从完整会话日志中折叠规范化的 `agent/inbox/spliced` 操作,得到完整的 `{ 'next-turn', 'next-step' }` 状态,并直接暴露同一状态。因此 fork 会像其他投影一样,自然投影其 seed 中继承的待处理 Inbox 操作。其 schema 与 `InboxState` 类型只有一个定义;消息值依赖既有的类型化 `UserMessage` 约定,而不增加第二套运行时消息校验器。 +`AgentLoop` 会在 Session projection registry 已组合时注册标准 `inbox` 投影。该投影从完整会话日志中折叠规范化的 `agent/inbox/spliced` 操作,得到完整的 `{ 'next-turn', 'next-step' }` 状态,并直接暴露同一状态。因此 fork 会像其他投影一样,自然投影其 seed 中继承的待处理 Inbox 操作。其 schema 与 `InboxState` 类型只有一个定义;消息值依赖既有的类型化 `UserMessage` 约定,而不增加第二套运行时消息校验器。 -投影注册表同时拥有持久重建与 live 状态。其既有的 `session/event` 驱动会在 `Session.append()` 返回前折叠每个已提交 splice。每个 Agent 的 `Inbox` 命令 facade 都读取同一份注册表状态。Inbox 在 append 前规范化 splice 坐标并检查待处理 `MessageId` 唯一性。Inbox 不遍历 `session.events`,不复制已折叠数组,也不再次应用已提交 transition。它仍发出其变更拥有的逐消息 `inserted`、`discarded` 与 `claimed` live 通知。 +投影注册表同时拥有持久重建与 live 状态。其既有的 `session/event` 驱动会在 `Session.append()` 返回前折叠每个已提交 splice。每个 Agent 的 `ReactLoopInbox` 命令 facade 都读取同一份注册表状态。Inbox 在 append 前规范化 splice 坐标并检查待处理 `MessageId` 唯一性。Inbox 不遍历 `session.events`,不复制已折叠数组,也不再次应用已提交 transition。它仍发出其变更拥有的逐消息 `inserted`、`discarded` 与 `claimed` live 通知。 通用会话投影传输层是唯一 Web 传输。它发送 seq 更高的 `session/projection` 值,在历史尾页中包含完整 values 块,折叠已分离的冷日志,并在缓存有效时使用投影缓存。系统不存在 Host 拥有的 `queue` 投影、placement 词汇、handoff 列表、专用 queue 帧或枚举 live Agent 的重连逻辑。 diff --git a/apps/web/tests/cold-blank-session.e2e.ts b/apps/web/tests/cold-blank-session.e2e.ts new file mode 100644 index 0000000000..2f6241cc05 --- /dev/null +++ b/apps/web/tests/cold-blank-session.e2e.ts @@ -0,0 +1,75 @@ +/** Pending Inbox recovery from detached persistence through the shipped Web profile. */ + +import { fileURLToPath } from 'node:url' +import { chromium, type Browser, type Page } from 'playwright' +import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest' +import { createUserMessage } from '@deepseek-ai/dsh-llm' +import { SESSION_FORMAT_VERSION, SessionId, SessionSeq } from '@deepseek-ai/dsh-session' +import { + captureStableAria, compareOrRefreshGolden, launchWebScaffold, + watchConsole, webSnapshotMode, type WebScaffold, +} from './scaffold.ts' +import { newEnglishPage, saveFailureShot } from './support.ts' + +const SESSION_ID = SessionId('cold-inbox-web-e2e') +const PENDING_TEXT = 'Accepted before the Host restarted' +const EXPECTED = fileURLToPath(new URL('./expected/cold-blank-session/queue.expected.md', import.meta.url)) + +describe('web e2e: cold Inbox recovery', () => { + let scaffold: WebScaffold + let browser: Browser + let page: Page + let tripwire: ReturnType + + beforeAll(async () => { + scaffold = await launchWebScaffold({}) + const createdAt = Date.now() - 60_000 + const handle = await scaffold.ctx.sessionPersistence.create({ + version: SESSION_FORMAT_VERSION, id: SESSION_ID, createdAt, + cwd: scaffold.workspaceCwd, isSeeded: false, delegationDepth: 0, + }) + try { + await handle.append([{ + type: 'agent/inbox/spliced', seq: SessionSeq(0), time: createdAt, + data: { target: 'next-turn', start: 0, inserted: [createUserMessage({ + content: [{ type: 'text', text: PENDING_TEXT }], source: { kind: 'user' }, + })] }, + }]) + } finally { + await handle.close() + } + expect(scaffold.ctx.agents.get(SESSION_ID)).toBeUndefined() + expect(scaffold.ctx.sessions.get(SESSION_ID)).toBeUndefined() + browser = await chromium.launch() + page = await newEnglishPage(browser) + tripwire = watchConsole(page) + await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' }) + await page.waitForSelector('[class*="frame"]', { timeout: 30_000 }) + }, 120_000) + + afterAll(async () => { + await browser?.close() + await scaffold?.close() + }) + + it('restores a pending row on opening and reload, then edits and removes it', async () => { + onTestFailed(() => saveFailureShot(page, 'web-e2e-cold-inbox')) + const group = page.locator('[role="treeitem"]').first() + await group.waitFor({ timeout: 15_000 }) + await group.click() + await page.locator('[role="treeitem"]').nth(1).click() + const dock = page.locator('[data-queue-dock]') + await dock.getByText(PENDING_TEXT, { exact: true }).waitFor({ timeout: 15_000 }) + await compareOrRefreshGolden(EXPECTED, + await captureStableAria(page, '[data-queue-dock]', scaffold.workspaceCwd), webSnapshotMode()) + await page.reload({ waitUntil: 'load' }) + await dock.getByText(PENDING_TEXT, { exact: true }).waitFor({ timeout: 15_000 }) + await dock.getByRole('button', { name: 'Edit queued message', exact: true }).click() + await dock.getByRole('textbox').fill('Edited after recovery') + await dock.getByRole('button', { name: 'Save queued message', exact: true }).click() + await dock.getByText('Edited after recovery', { exact: true }).waitFor() + await dock.getByRole('button', { name: 'Remove queued message', exact: true }).click() + await dock.waitFor({ state: 'detached' }) + expect(tripwire.pageErrors).toEqual([]) + }) +}) diff --git a/apps/web/tsconfig.json b/apps/web/tsconfig.json index 8001c0a9c3..e3771f09d6 100644 --- a/apps/web/tsconfig.json +++ b/apps/web/tsconfig.json @@ -80,6 +80,7 @@ "tests/markdown-inline-code-links.e2e.ts", "tests/clickable-links-gallery.e2e.ts", "tests/queue-actions.e2e.ts", + "tests/cold-blank-session.e2e.ts", "tests/queue-image.e2e.ts", "tests/skill-invocation-policy.e2e.ts", "tests/skill-user-invoke.e2e.ts", diff --git a/docs/architecture.i18n.yaml b/docs/architecture.i18n.yaml index d9d1bd54f3..dd0f61c31f 100644 --- a/docs/architecture.i18n.yaml +++ b/docs/architecture.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/architecture.md -architecture.md: fed98f006471f86f02c43bcb0c6ea4dfe7850da8 -architecture.zh.md: b1ec7ddaf0278a14cd7c18d6cc272fc59c5c3781 +architecture.md: 2824b77e30f41fddf947d39448ce92879ea0c28a +architecture.zh.md: 6218f3cefae8fd22f3dc2a5c7fdc27f505233177 diff --git a/docs/architecture.md b/docs/architecture.md index fed98f0064..2824b77e30 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -104,7 +104,7 @@ turn/end `turn/*`, `step/*`, `system/message`, `user/message`, `assistant/message`, `assistant/attempt`, and `tool/*` are durable session events; the rest are live extension points across three domains. `agent/assistant-stream` publishes process-local start, transient chunk, and end frames. The loop commits the complete compact stream as one message or log-only attempt before a committed end frame, and the Web Session-follow adapter is the live event's only remote consumer. `agent/pre-step`, `agent/request`, `llm/stream`, and the three `tools/*` events are waterfalls, whose listeners must call `next()` to delegate; `agent/turn-stopping` is serial and has no `next()`. -Input reaches the driver through one inbox. Some messages wake it immediately; injected context waits in the inbox until another message does. +Input reaches the driver through one inbox. Some messages wake it immediately; injected context waits in the inbox until another message does. The AgentLoop service owns the durable `inbox` projection registration, making pending input readable before any Agent starts and after all Agents unload. `agent/pre-step` decides the accepted input. Listeners may rewrite or reject claimed messages; a rejected or empty first claim closes a durable turn without a step. An enter decision may set `startsRequestSeries`: the loop logs a fresh `request/header` (reason `series`, or `change` with `startsSeries: true` when the envelope also changed). Wrapping listeners preserve that declaration with `{ ...decision, messages }`. After assembly and `step/start`, `agent/request` and `prepareCall()` resolve the actual route before the system prompt and accepted users are committed; cancellation during either async phase commits neither. The prepared call capability governs prompt admission, not the preceding `request/context`. Every attempt synchronously reconciles the same rendered assembly, appends users only on the first attempt, logs header/context as needed, and derives and freezes the request before streaming the bound call. Retries do not repeat assembly or `agent/pre-step`. Surface replacements after attachment start a new request series, including during the first resumed pre-step; unchanged resume continues the series. The first admitted step reserves the system head before user messages even for an empty prompt (no wire message). The prompt travels only as `system/message` history: an empty rendering clears all active system nodes, leaving no old prompt model-visible; capable routes can append non-empty updates after the cached prefix; incapable routes and new request series consolidate non-empty prompt text at the first system node, with logged empty replacements for non-empty later system nodes ([decision](../.agents/notes/implemented/architecture/2026-09-02-system-prompt-as-surface-node.md); [decision rule](../packages/core/agent-loop/README.md#understand-the-implementation)). diff --git a/docs/architecture.zh.md b/docs/architecture.zh.md index b1ec7ddaf0..6218f3cefa 100644 --- a/docs/architecture.zh.md +++ b/docs/architecture.zh.md @@ -108,7 +108,7 @@ turn/end `turn/*`、`step/*`、`system/message`、`user/message`、`assistant/message`、`assistant/attempt` 和 `tool/*` 是持久会话事件;其余是分属三个事件域的实时扩展点。`agent/assistant-stream` 发布进程本地 start、瞬态 chunk 与 end frame。loop 会在 committed end frame 前把完整紧凑 stream 提交为一个 message 或仅日志 attempt;Web Session-follow adapter 是该 live event 唯一的远程消费方。`agent/pre-step`、`agent/request`、`llm/stream` 和三个 `tools/*` 事件是 waterfall(瀑布式事件),其监听器必须调用 `next()` 才能委托下去;`agent/turn-stopping` 是 serial 事件,没有 `next()`。 -输入通过同一个 inbox 到达驱动器。有些消息会立即唤醒它;注入的上下文会留在 inbox 中,直到另一条消息将其唤醒。 +输入通过同一个 inbox 到达驱动器。有些消息会立即唤醒它;注入的上下文会留在 inbox 中,直到另一条消息将其唤醒。AgentLoop 服务拥有持久 `inbox` 投影的注册,因此待处理输入在任何 Agent 启动前以及所有 Agent 卸载后都可读取。 `agent/pre-step` 决定接纳的输入。监听器可以改写或拒绝已领取消息;首次领取被拒绝或为空时,关闭不含步骤的持久轮次。enter 决策可设置 `startsRequestSeries`:循环记录新的 `request/header`(原因为 `series`,或在封装同时变化时为携带 `startsSeries: true` 的 `change`)。包装监听器通过 `{ ...decision, messages }` 保留该声明。组装与 `step/start` 之后,`agent/request` 和 `prepareCall()` 先解析实际路由,再提交系统提示词与已接纳用户消息;在任一异步阶段取消都不会提交这两者。提示词准入依据已准备调用的能力,而非先前的 `request/context`。每次尝试同步协调同一份已渲染组装结果、仅在首次尝试追加用户消息、按需记录 header/context、派生并冻结请求,再通过绑定调用发起流式请求。重试不重复组装或 `agent/pre-step`。附接后的 surface 替换开启新请求序列,包括恢复后的首次 pre-step 中发生的替换;未变化的恢复延续序列。首次接纳的步骤在用户消息之前预留系统头节点,即使提示词为空(不产生协议消息)。提示词仅通过 `system/message` 历史传递:空渲染文本清除所有生效的系统节点,模型不再看到旧提示词;具备能力的路由可在缓存前缀之后追加非空更新;不具备能力的路由与新请求序列将非空提示词文本归并到首个系统节点,并为非空的后续系统节点记录空内容替换([决策](../.agents/notes/implemented/architecture/2026-09-02-system-prompt-as-surface-node.zh.md);[决策规则](../packages/core/agent-loop/README.zh.md#understand-the-implementation))。 diff --git a/docs/config-catalog.i18n.yaml b/docs/config-catalog.i18n.yaml index bfc5221538..a05964a72d 100644 --- a/docs/config-catalog.i18n.yaml +++ b/docs/config-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/config-catalog.md -config-catalog.md: 50626a7d85aa54f6d2e3cdb6140b3af8081d1461 -config-catalog.zh.md: d2ae35b91a4a83da882e8ec5af82a505aa5d5bd1 +config-catalog.md: 72ab9a775077896e6f42af2574cf3b699160219c +config-catalog.zh.md: a3cabba2637e13e63388e712150a33f9d5f77968 diff --git a/docs/config-catalog.md b/docs/config-catalog.md index 50626a7d85..72ab9a7750 100644 --- a/docs/config-catalog.md +++ b/docs/config-catalog.md @@ -111,7 +111,7 @@ export interface Config { Depends on: [`AgentOptions`](subsystems/core.md) · [`SessionId`](subsystems/core.md) -Source: [`packages/core/agent-loop/src/index.ts:318`](../packages/core/agent-loop/src/index.ts) +Source: [`packages/core/agent-loop/src/index.ts:319`](../packages/core/agent-loop/src/index.ts) diff --git a/docs/config-catalog.zh.md b/docs/config-catalog.zh.md index d2ae35b91a..a3cabba263 100644 --- a/docs/config-catalog.zh.md +++ b/docs/config-catalog.zh.md @@ -113,7 +113,7 @@ export interface Config { 依赖:[`AgentOptions`](subsystems/core.zh.md) · [`SessionId`](subsystems/core.zh.md) -来源:[`packages/core/agent-loop/src/index.ts:318`](../packages/core/agent-loop/src/index.ts) +来源:[`packages/core/agent-loop/src/index.ts:319`](../packages/core/agent-loop/src/index.ts) diff --git a/docs/event-producer-consumer.i18n.yaml b/docs/event-producer-consumer.i18n.yaml index b1f10816f3..50577512df 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: 449b7d8f0fb55515e7f1f028e151ce1b26862e92 -event-producer-consumer.zh.md: 8120df6de6776bce8841f75311294583417532c2 +event-producer-consumer.md: 0adb3ce073c6b22f0c2934db499a4d88612631d3 +event-producer-consumer.zh.md: e6555a780ee071504a062fbe814da55d39da83ab diff --git a/docs/event-producer-consumer.md b/docs/event-producer-consumer.md index 449b7d8f0f..0adb3ce073 100644 --- a/docs/event-producer-consumer.md +++ b/docs/event-producer-consumer.md @@ -7,7 +7,7 @@ This matrix shows which packages dispatch each harness-owned event and which pac | Event | Mode | Declared in | Dispatchers | Listeners | | --- | --- | --- | --- | --- | -| `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-loop/config-start-failed` | `emit` | [`packages/core/agent-loop/src/index.ts:247`](../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:373`](../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) | @@ -22,11 +22,11 @@ This matrix shows which packages dispatch each harness-owned event and which pac | `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:391`](../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:597`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | -| `api-session/added` | `emit` | [`packages/api/session-controller/src/types.ts:577`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | -| `api-session/error` | `emit` | [`packages/api/session-controller/src/types.ts:604`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | -| `api-session/removed` | `emit` | [`packages/api/session-controller/src/types.ts:583`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | -| `api-session/status` | `emit` | [`packages/api/session-controller/src/types.ts:590`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | +| `api-session/activity` | `emit` | [`packages/api/session-controller/src/types.ts:582`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | +| `api-session/added` | `emit` | [`packages/api/session-controller/src/types.ts:562`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | +| `api-session/error` | `emit` | [`packages/api/session-controller/src/types.ts:589`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | +| `api-session/removed` | `emit` | [`packages/api/session-controller/src/types.ts:568`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | +| `api-session/status` | `emit` | [`packages/api/session-controller/src/types.ts:575`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | | `approval/request` | `waterfall` | [`packages/interaction/user-approval/src/types.ts:85`](../packages/interaction/user-approval/src/types.ts) | [`user-approval`](../packages/interaction/user-approval) (`waterfall`) | [`acp`](../packages/acp/acp), `remotes` | | `authorization/settled` | `emit` | [`packages/credentials/authorization/src/index.ts:57`](../packages/credentials/authorization/src/index.ts) | [`authorization`](../packages/credentials/authorization) (`events.dispatch`) | [`authorization`](../packages/credentials/authorization) | | `commands/change` | `emit` | [`packages/interaction/commands/src/types.ts:87`](../packages/interaction/commands/src/types.ts) | [`commands`](../packages/interaction/commands) (`events.dispatch`) | `remotes` | diff --git a/docs/event-producer-consumer.zh.md b/docs/event-producer-consumer.zh.md index 8120df6de6..e6555a780e 100644 --- a/docs/event-producer-consumer.zh.md +++ b/docs/event-producer-consumer.zh.md @@ -9,7 +9,7 @@ | 事件 | 模式 | 声明位置 | 派发方 | 监听方 | | --- | --- | --- | --- | --- | -| `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-loop/config-start-failed` | `emit` | [`packages/core/agent-loop/src/index.ts:247`](../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:373`](../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) | @@ -24,11 +24,11 @@ | `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:391`](../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:597`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | -| `api-session/added` | `emit` | [`packages/api/session-controller/src/types.ts:577`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | -| `api-session/error` | `emit` | [`packages/api/session-controller/src/types.ts:604`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | -| `api-session/removed` | `emit` | [`packages/api/session-controller/src/types.ts:583`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | -| `api-session/status` | `emit` | [`packages/api/session-controller/src/types.ts:590`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | +| `api-session/activity` | `emit` | [`packages/api/session-controller/src/types.ts:582`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | +| `api-session/added` | `emit` | [`packages/api/session-controller/src/types.ts:562`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | +| `api-session/error` | `emit` | [`packages/api/session-controller/src/types.ts:589`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | +| `api-session/removed` | `emit` | [`packages/api/session-controller/src/types.ts:568`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | +| `api-session/status` | `emit` | [`packages/api/session-controller/src/types.ts:575`](../packages/api/session-controller/src/types.ts) | `session-controller` (`emit`) | `remotes` | | `approval/request` | `waterfall` | [`packages/interaction/user-approval/src/types.ts:85`](../packages/interaction/user-approval/src/types.ts) | [`user-approval`](../packages/interaction/user-approval) (`waterfall`) | [`acp`](../packages/acp/acp), `remotes` | | `authorization/settled` | `emit` | [`packages/credentials/authorization/src/index.ts:57`](../packages/credentials/authorization/src/index.ts) | [`authorization`](../packages/credentials/authorization) (`events.dispatch`) | [`authorization`](../packages/credentials/authorization) | | `commands/change` | `emit` | [`packages/interaction/commands/src/types.ts:87`](../packages/interaction/commands/src/types.ts) | [`commands`](../packages/interaction/commands) (`events.dispatch`) | `remotes` | diff --git a/docs/subsystems/core.i18n.yaml b/docs/subsystems/core.i18n.yaml index 07faf5ab83..f0fe8aa811 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: 27ff30a9e63c86ffb54ccf57dea18ebf3fe39846 -core.zh.md: cedd113de5d5d551b8b558f9f33c7cacf6aa4953 +core.md: 271c2ddfb80186d1ac80244a48ccd3511b7755c3 +core.zh.md: 059304f5c6cbc8f4a8fe24aa9bf6519ec2abf8a0 diff --git a/docs/subsystems/core.md b/docs/subsystems/core.md index 27ff30a9e6..271c2ddfb8 100644 --- a/docs/subsystems/core.md +++ b/docs/subsystems/core.md @@ -272,7 +272,7 @@ interface Inbox { type InboxTarget = 'next-turn' | 'next-step' ``` -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. +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`. The `AgentLoop` service registers the standard `inbox` projection before publishing its factory; its cell is the sole live state, and the same fold serves cold consumers even when no Agent exists. 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 cedd113de5..059304f5c6 100644 --- a/docs/subsystems/core.zh.md +++ b/docs/subsystems/core.zh.md @@ -276,7 +276,7 @@ interface Inbox { type InboxTarget = 'next-turn' | 'next-step' ``` -每个待处理入队项就是其 `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` 通知。 +每个待处理入队项就是其 `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`。`AgentLoop` 服务在发布工厂之前注册标准 `inbox` 投影;其 cell 是唯一 live 状态,同一份折叠在没有 Agent 时也服务于冷消费方。该 fold 会拒绝不安全或越界的 splice 坐标,以及跨两份列表重复的标识,并通过事件 seq 指出格式错误的持久历史。跟踪单条消息的消费方使用精确的 `agent/inbox/inserted`、`claimed` 与 `discarded` 通知。 取消: diff --git a/packages/api/session-controller/README.i18n.yaml b/packages/api/session-controller/README.i18n.yaml index 91888af26a..398d37f80e 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: ef6ffb35636f9f1a87c3b30832f68540d5dcd0ea -README.zh.md: 97949e01379446e6171debb9fb1250e8088413f8 +README.md: dc1b2a4cf8c322e9cfd9981fd4adc29d1dc544d5 +README.zh.md: 118786581b2611f2dae19cdbb6b07dee91df48d0 diff --git a/packages/api/session-controller/README.md b/packages/api/session-controller/README.md index ef6ffb3563..dc1b2a4cf8 100644 --- a/packages/api/session-controller/README.md +++ b/packages/api/session-controller/README.md @@ -28,11 +28,11 @@ History pages and follow opening snapshots carry one `{ type: 'event', event: Se The Client journal validates exact V3 event envelopes before publishing follow snapshots, live entries, or history pages. It reuses the browser-safe Session validators for required surface markers, exact replacement endpoints, earlier unique source seqs, embedded Assistant provenance, request-header omissions, and tool-error consistency. Invalid records fail without field stripping or normalization; range membership and source existence remain durable-log checks on the Host. -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 rejects content with neither non-whitespace text nor an attachment before resolving the Agent or appending Session events; queue edits accept only non-empty text content. 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. +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. Cancellation requires live state; queue mutation, model, rename, prompt, and file-reference operations may resolve or resume an ordinary Session. Prompt rejects content with neither non-whitespace text nor an attachment before resolving the Agent or appending Session events; queue edits accept only non-empty text content. 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. 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 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 jobs and projection state instead of treating transient values as durable events. The durable `inbox` projection carries both pending lists through the same cold-read and reconnect path as other projections. 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. +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`, including pending messages in the `inbox` projection. 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 97949e0137..118786581b 100644 --- a/packages/api/session-controller/README.zh.md +++ b/packages/api/session-controller/README.zh.md @@ -28,11 +28,11 @@ kind: "package-reference" Client journal 在发布 follow snapshot、live entry 或历史页之前验证精确的 V3 event envelope。它复用浏览器安全的 Session validator,检查必需的 surface marker、精确的 replacement endpoint、更早且唯一的 source seq、内嵌 Assistant 来源、request header 可选字段的省略规则以及 tool error 一致性。无效 record 直接失败,不删除字段或归一化;范围成员与来源存在性仍由 Host 的持久日志检查。 -每个 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 会在解析 Agent 或追加 Session event 前,拒绝既没有非空白文本也没有附件的 content;queue edit 只接受非空文本 content。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。 +每个 endpoint 都声明自己的激活策略。列表只读取持久化 header 与 projection cache row,绝不调用逐 Session stat 或打开冷 Session body。当前格式 cache identity 可以提供全部列表 hint;生命周期匹配的 predecessor cache 只能提供版本兼容的 title,作为可能过时的展示事实,绝不能作为权威 fold seed。搜索、附件、历史页、日志跟随、skill 发现和工作区路径打开可以在不激活 Agent 的情况下检查 persistence;`canOpenWorkspacePath()` 无需指定 Session 即可报告原生打开能力。取消要求 live 状态;queue 变更、模型、重命名、prompt 和文件引用操作可以解析或恢复普通 Session。prompt 会在解析 Agent 或追加 Session event 前,拒绝既没有非空白文本也没有附件的 content;queue edit 只接受非空文本 content。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。每次 inbox 变更时,Host 会先发布 projection frame,再从同一份已校验的折叠后值派生 queue replacement,因此监听器注册顺序不会产生陈旧的 queue frame。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 开始,因此重连会替换 jobs 和 projection 状态,而不会把瞬态值当作 durable event。持久 `inbox` 投影通过与其他投影相同的冷读取和重连路径传输两份待处理列表。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 重建会话。 +Session 对象还承载本地提交回显:`session.beginSubmission` 在调用方序列化与 prompt 之前,同步把一条回显写入 `SessionSnapshot.pendingSubmissions`,会话 UI 因此能在点击提交的当帧显示消息。回显按顺序存放图片预览与持久文件引用。Session 根据当前运行状态与请求的投递模式推导其 `transcript`、`queued` 或 `steering` 位置,并在序列化期间保留该位置。prompt 的 `requestId` 是关联标识:Host 把它回显为 durable user source 的 `rpcId`,`inbox` 投影中的待处理消息也保留同一 source。回显在观察到其 durable event 或 queue occurrence 后延迟一个动画帧退休,带标识的 prompt 失败或被放弃时立即退休,销毁时按 failed 退休。每次退休恰好触发一次 `onRetire`;observed 退休还会携带有序的持久附件引用,让 composer 释放成功卡片并保留失败草稿。回显只存在于 Client 内存;刷新与重连只从 durable event 重建会话。 diff --git a/packages/api/session-controller/src/client/contract/snapshot.ts b/packages/api/session-controller/src/client/contract/snapshot.ts index aba257f893..c9ce1be4f5 100644 --- a/packages/api/session-controller/src/client/contract/snapshot.ts +++ b/packages/api/session-controller/src/client/contract/snapshot.ts @@ -1,24 +1,10 @@ /** Session-owned observable state excluding Conversation target data. */ -import type { ContentBlock } from '@deepseek-ai/dsh-llm/types' import type { FileAttachmentRef } from '@deepseek-ai/dsh-attachment' -import type { MessageId } from '@deepseek-ai/dsh-llm/brand' import type { SessionId } from '@deepseek-ai/dsh-session/types' import type { SubagentAddress } from '@deepseek-ai/dsh-subagent/client' import type { RemoteFailure } from '@deepseek-ai/dsh-typert-protocol' import type { SessionRequestId } from '../../types.ts' -/** One transient inbox occurrence from the authoritative queue snapshot. */ -export interface QueuedMessage { - readonly id: MessageId - readonly messageId: MessageId - readonly placement: 'queued' | 'steering' | 'context' - /** Prompt-RPC identity of a browser-submitted occurrence; correlates the local submission echo. */ - readonly rpcId?: SessionRequestId - readonly content: readonly ContentBlock[] - readonly preview: string - readonly text: string | null -} - /** One image displayed by a local submission echo before durable admission. */ export interface PendingSubmissionImage { /** Browser-owned preview URL; its lifecycle belongs to the submitter, never this snapshot. */ @@ -82,7 +68,6 @@ export interface PromptError { /** Immutable Session lifecycle and control snapshot. */ export interface SessionSnapshot { readonly sessionId: SessionId - readonly queue: readonly QueuedMessage[] /** Local prompt-submission echoes not yet observed as durable events or queue occurrences. */ readonly pendingSubmissions: readonly PendingSubmission[] readonly running: boolean diff --git a/packages/api/session-controller/src/client/sessions/manager.ts b/packages/api/session-controller/src/client/sessions/manager.ts index fd39fefedf..1ef550fb08 100644 --- a/packages/api/session-controller/src/client/sessions/manager.ts +++ b/packages/api/session-controller/src/client/sessions/manager.ts @@ -8,7 +8,6 @@ import type { WorkspaceId } from '@deepseek-ai/dsh-workspace/types' import type { SessionControlBaseline, SessionControlFrame, - SessionQueuedItem, SessionSummary, SessionJob as JobView, } from '../../types.ts' @@ -96,8 +95,6 @@ export class SessionManager { private readonly sessions = new Map() /** In-flight Session disposals remain here after instances leave `sessions`, so manager disposal can await quiescence. */ private readonly sessionDisposals = new Set>() - /** Latest transient queues, retained independently of Session object materialization. */ - private readonly queues = new Map() /** * Sessions that finished running while not selected — the sidebar's green * "done" reminder (manager-owned, survives connection generations; cleared @@ -290,11 +287,6 @@ export class SessionManager { if (session === undefined) { session = this.createSession(sessionId) this.sessions.set(sessionId, session) - // Install the latest control baseline before the running-bit sync: a - // not-running summary must sweep replayed queue - // rows the same way a live status flip would (their retirement events dropped - // while the session was uninstantiated). - session.replaceControl(this.queues.get(sessionId) ?? []) // Sync the running and blank bits from the list snapshot into the new // instance (consistency when the list precedes open). const summary = this.summaries.find(s => s.sessionId === sessionId) @@ -669,22 +661,12 @@ export class SessionManager { this.notifier.markDirty() return } - if (frame.type === 'jobs') { - if (frame.jobs.length === 0) this.jobsBySession.delete(frame.sessionId) - else this.jobsBySession.set(frame.sessionId, frame.jobs) - this.notifier.markDirty() - return - } - this.queues.set(frame.sessionId, frame.items) - this.sessions.get(frame.sessionId)?.handleControlFrame(frame) + if (frame.jobs.length === 0) this.jobsBySession.delete(frame.sessionId) + else this.jobsBySession.set(frame.sessionId, frame.jobs) + this.notifier.markDirty() } private replaceControlBaseline(baseline: SessionControlBaseline): void { - this.queues.clear() - for (const [sessionId, items] of Object.entries(baseline.queues)) { - this.queues.set(sessionId as SessionId, items) - } - this.jobsBySession.clear() for (const [sessionId, jobs] of Object.entries(baseline.jobs)) { if (jobs.length > 0) this.jobsBySession.set(sessionId as SessionId, jobs) @@ -696,9 +678,6 @@ export class SessionManager { store.truncate(asOfSeq) store.seed({ ...block, asOfSeq }) } - for (const [sessionId, session] of this.sessions) { - session.replaceControl(this.queues.get(sessionId) ?? []) - } this.notifier.markDirty() } @@ -738,7 +717,6 @@ export class SessionManager { this.updateCatalogActivity(sessionId, false) if (durableSubagent) this.sessions.get(sessionId)?.handleRunning(false) else this.sessions.get(sessionId)?.handleRemoved() - this.queues.delete(sessionId) this.jobsBySession.delete(sessionId) if (!durableSubagent) this.projectionStores.delete(sessionId) const inflightCatalog = this.catalogInflight.get(sessionId) diff --git a/packages/api/session-controller/src/client/sessions/queue-mirror.ts b/packages/api/session-controller/src/client/sessions/queue-mirror.ts deleted file mode 100644 index f0ed979c2b..0000000000 --- a/packages/api/session-controller/src/client/sessions/queue-mirror.ts +++ /dev/null @@ -1,71 +0,0 @@ -import type { ContentBlock } from '@deepseek-ai/dsh-llm/types' -import type { SessionQueuedItem } from '../../types.ts' -import type { SessionEvent } from '@deepseek-ai/dsh-session/types' -import type { QueuedMessage } from '../contract/snapshot.ts' - -const QUEUE_PREVIEW_CHARS = 200 - -// Attachment blocks are excluded: queue presentation renders them from -// `content`, so the text preview covers only what has no visual form. -function previewOf(content: readonly ContentBlock[]): string { - const flat = content - .filter(block => block.type !== 'image' && block.type !== 'file') - .map(block => (block.type === 'text' ? block.text : `[${block.type}]`)) - .join(' ').replace(/\s+/g, ' ').trim() - const chars = Array.from(flat) - return chars.length > QUEUE_PREVIEW_CHARS ? `${chars.slice(0, QUEUE_PREVIEW_CHARS).join('')}…` : flat -} - -function textOf(content: readonly ContentBlock[]): string | null { - if (!content.every(block => block.type === 'text')) return null - return content.map(block => block.text).join('') -} - -type QueueItems = readonly SessionQueuedItem[] - -/** Authoritative transient queue projection and durable steering handoff. */ -export class SessionQueueMirror { - private current: readonly QueuedMessage[] = [] - - /** - * Return the current immutable queue projection. - * @returns current queue rows. - */ - snapshot(): readonly QueuedMessage[] { - return this.current - } - - /** - * Replace from one authoritative stream queue frame. - * @param items - complete host queue snapshot. - */ - replace(items: QueueItems): void { - this.current = items.map((item) => { - const content = item.message.content as unknown as readonly ContentBlock[] - return { - id: item.id, - messageId: item.message.id, - placement: item.placement, - ...(item.rpcId === undefined ? {} : { rpcId: item.rpcId }), - content, - preview: previewOf(content), - text: textOf(content), - } - }) - } - - /** - * Retire a transient steering row once its durable message enters the log. - * @param event - newly contiguous durable Session event. - * @returns whether the projection changed. - */ - acceptDurable(event: SessionEvent): boolean { - if (event.type !== 'user/message') return false - const messageId = event.data.id - const index = this.current.findIndex(item => - item.placement === 'steering' && item.messageId === messageId) - if (index < 0) return false - this.current = this.current.filter((_item, candidate) => candidate !== index) - return true - } -} diff --git a/packages/api/session-controller/src/client/sessions/session.ts b/packages/api/session-controller/src/client/sessions/session.ts index 2945f0b5c4..3c401cc3e7 100644 --- a/packages/api/session-controller/src/client/sessions/session.ts +++ b/packages/api/session-controller/src/client/sessions/session.ts @@ -1,6 +1,7 @@ // Sessions remain resident after creation so their open Remote sources keep running off-screen. import type { Context } from '@deepseek-ai/cordis' +import type { InboxState } from '@deepseek-ai/dsh-agent/types' import { randomUUID } from '@deepseek-ai/dsh-util-crypto' import type { AttachmentIdType, FileAttachmentRef, ImageAttachmentRef } from '@deepseek-ai/dsh-attachment' import type { SubagentAddress } from '@deepseek-ai/dsh-subagent/client' @@ -13,9 +14,7 @@ import type { QueueAction, SessionAddress, SessionAssistantStreamBaseline, - SessionControlFrame, SessionProjectionBaseline, - SessionQueuedItem, SessionRequestId, } from '../../types.ts' import type { @@ -36,7 +35,6 @@ import type { SessionRemotes } from './remotes.ts' import { ProjectionValueStore } from './projection-store.ts' import type { ProjectionsBaseline } from './projection-store.ts' import { resolvedClientTimeZone } from '../time-zone.ts' -import { SessionQueueMirror } from './queue-mirror.ts' import { ClientAssistantStream, type ClientAssistantStreamResult, @@ -99,8 +97,7 @@ export class Session implements SessionFace { private jumpTargetSeq: SessionSeq | null = null /** The running jump loop's completion, shared by retargeting callers. */ private jumpPromise: Promise | null = null - /** Authoritative stream-only inbox snapshot; pending work never hits history. */ - private readonly queueMirror = new SessionQueueMirror() + private readonly stopObservingInbox: () => void private readonly assistantStream = new ClientAssistantStream() private running = false private address: SubagentAddress | undefined @@ -121,7 +118,7 @@ export class Session implements SessionFace { /** Local submission echoes, insertion-ordered (see SessionSnapshot.pendingSubmissions). */ private pendingSubmissions: readonly PendingSubmission[] = [] /** Per-echo settlement state; `retiring` latches the first observation so a - * queue frame and its durable event cannot both retire one echo. */ + * Inbox projection and its durable event cannot both retire one echo. */ private readonly submissionSettlements = new Map void) | undefined retiring: boolean @@ -173,6 +170,9 @@ export class Session implements SessionFace { this.snapshotCache = this.buildSnapshot() }) this.snapshotCache = this.buildSnapshot() + this.stopObservingInbox = this.projections.faceOf('inbox').subscribe(() => { + this.observeSubmissionInbox() + }) } /** @@ -453,7 +453,7 @@ export class Session implements SessionFace { } /** Rebuild an opened history source after address replacement. - * Invalidates any in-flight open first; queue state belongs to the independently + * Invalidates any in-flight open first; projection state belongs to the independently * reconnecting control stream and remains untouched. */ async resync(): Promise { if (this.openState === 'cold') return // never opened: no window to rebuild (doOpen flips to 'loading' synchronously, so cold implies no in-flight open) @@ -491,26 +491,6 @@ export class Session implements SessionFace { // ---- Manager-only entry points (@internal; never called by the UI) ---- - /** - * Replace every transient control value for this Session from one stream baseline. - * @param queue - complete pending queue for this Session. - */ - replaceControl(queue: readonly SessionQueuedItem[]): void { - this.queueMirror.replace(queue) - this.observeSubmissionQueue(queue) - this.notifier.markDirty() - } - - /** - * Apply one Session-addressed live control update. - * @param frame - queue replacement addressed to this Session. - */ - handleControlFrame(frame: Extract): void { - this.queueMirror.replace(frame.items) - this.observeSubmissionQueue(frame.items) - this.notifier.markDirty() - } - /** * Running-bit relay from the host stream (list entry and snapshot stay consistent). * @param running - the new running state. @@ -588,6 +568,7 @@ export class Session implements SessionFace { * @returns when the Remote iterator has completed teardown. */ async dispose(): Promise { + this.stopObservingInbox() // Unsettled echoes retire as failed so their owners can restore or // release browser resources; echoes already scheduled as observed keep // that settlement. @@ -713,18 +694,25 @@ export class Session implements SessionFace { const event = entry.event const awaitingFirstTurn = this.firstPromptPendingTurn if (event.type === 'turn/start') this.firstPromptPendingTurn = false - const queueChanged = this.queueMirror.acceptDurable(event) this.eventSource.append(entry) // After the feed append: the conversation assembly's animation frame is // registered by the feed subscribers above, so the echo-retirement frame // scheduled here always runs after the durable node became renderable. this.observeSubmissionEvent(event) - return queueChanged || awaitingFirstTurn !== this.firstPromptPendingTurn + return awaitingFirstTurn !== this.firstPromptPendingTurn } - /** Retire the matching echo when a durable browser-prompt `user/message` becomes visible. */ + /** Observe durable acceptance even when insertion and claim share one projection notification. */ private observeSubmissionEvent(event: { readonly type: string; readonly data?: unknown }): void { - if (this.submissionSettlements.size === 0 || event.type !== 'user/message') return + if (this.submissionSettlements.size === 0) return + if (event.type === 'agent/inbox/spliced') { + const splice = event.data as { readonly inserted?: unknown } | undefined + if (Array.isArray(splice?.inserted)) { + for (const message of splice.inserted) this.observeSubmissionEvent({ type: 'user/message', data: message }) + } + return + } + if (event.type !== 'user/message') return // Structural read: window entries may be compact history records, so the // fields are narrowed rather than trusted (same posture as Conversation // assembly matchers). @@ -734,12 +722,15 @@ export class Session implements SessionFace { this.scheduleObservedRetirement(source.rpcId as SessionRequestId, attachmentRefsIn(data?.content)) } - /** Retire echoes whose prompts landed in the host inbox instead of the log (running-turn submissions). */ - private observeSubmissionQueue(items: readonly SessionQueuedItem[]): void { + /** Retire local echoes when their accepted messages appear in the durable Inbox projection. */ + private observeSubmissionInbox(): void { if (this.submissionSettlements.size === 0) return - for (const item of items) { - if (item.rpcId !== undefined) { - this.scheduleObservedRetirement(item.rpcId, attachmentRefsIn(item.message.content)) + const inbox = this.projections.get('inbox') as InboxState | undefined + if (inbox === undefined) return + for (const message of [...inbox['next-turn'], ...inbox['next-step']]) { + const source = message.source + if (source.kind === 'user' && 'rpcId' in source) { + this.scheduleObservedRetirement(source.rpcId, attachmentRefsIn(message.content)) } } } @@ -795,7 +786,6 @@ export class Session implements SessionFace { private buildSnapshot(): SessionSnapshot { return { sessionId: this.sessionId, - queue: this.queueMirror.snapshot(), pendingSubmissions: this.pendingSubmissions, running: this.running, subagent: this.address === undefined diff --git a/packages/api/session-controller/src/commands.ts b/packages/api/session-controller/src/commands.ts index 35fc113408..3aa12ba5c8 100644 --- a/packages/api/session-controller/src/commands.ts +++ b/packages/api/session-controller/src/commands.ts @@ -417,11 +417,11 @@ export class SessionCommandController { } /** - * Mutate one still-pending queue occurrence without resuming a cold Agent. + * Mutate one pending Inbox occurrence, restoring an ordinary cold Agent when needed. * @param request - Session, queue item, and requested mutation. * @returns acknowledgement that the queue mutation was applied. */ - updateQueue(request: SessionUpdateQueueRequest): SessionUpdateQueueValue { + async updateQueue(request: SessionUpdateQueueRequest): Promise { if (request.action.kind === 'edit') { if (request.action.content.some(block => block.type !== 'text')) { throw new RemoteError( @@ -438,9 +438,14 @@ export class SessionCommandController { ) } } - const agent = this.ctx.agents.get(request.sessionId) + let agent = this.ctx.agents.get(request.sessionId) if (agent === undefined) { - throw new RemoteError('session/queue-item-not-found', 'queued item is no longer pending', { itemId: request.itemId }) + const found = await this.agents.resolveAgent(request.sessionId) + if ('error' in found) { + if (found.error.code !== 'session/not-found') throw found.error + throw new RemoteError('session/queue-item-not-found', 'queued item is no longer pending', { itemId: request.itemId }) + } + agent = found.agent } if (hasApiSessionSubagentOwner(this.ctx, agent.session, agent)) { const identity = this.ctx.sessionProjections diff --git a/packages/api/session-controller/src/control.ts b/packages/api/session-controller/src/control.ts index 9bdd5a05bf..3fb8d0032e 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. */ +/** Live Session jobs and projection state with reconnect baselines. */ import type { Context } from '@deepseek-ai/cordis' -import type { Agent, InboxState } from '@deepseek-ai/dsh-agent' +import type { Agent } from '@deepseek-ai/dsh-agent' import { Deque } from '@deepseek-ai/dsh-deque' import type { JobSnapshot } from '@deepseek-ai/dsh-jobs' import type { - Session, SessionId, UserMessage, + Session, SessionId, } from '@deepseek-ai/dsh-session' import type { JsonValue } from '@deepseek-ai/dsh-util-values' import type { @@ -14,7 +14,6 @@ import type { SessionJob, SessionProjectionBaseline, SessionProjectionValues, - SessionQueuedItem, } from './types.ts' /** Owns the Host-wide Session control stream. */ @@ -31,14 +30,6 @@ 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) }) @@ -73,15 +64,12 @@ export class SessionControlController { private baseline(): SessionControlBaseline { const sessions = this.ctx.sessions.list() - const queues = Object.create(null) as Record const jobs = Object.create(null) as Record for (const session of sessions) { const agent = this.ctx.agents.get(session.id) - queues[session.id] = agent?.session === session ? queueItems(agent) : [] jobs[session.id] = this.jobsFor(agent) } return { - queues, jobs, projections: this.projectionBaseline(sessions), } @@ -167,36 +155,6 @@ class ControlQueue { } } -function queueItems(agent: Agent): SessionQueuedItem[] { - return queueItemsFromInbox({ - 'next-turn': agent.inbox.nextTurn, - 'next-step': agent.inbox.nextStep, - }) -} - -function queueItemsFromInbox(inbox: InboxState): SessionQueuedItem[] { - return [ - ...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[] }, - })), - ...inbox['next-step'].map(message => ({ - id: message.id, - placement: message.source.kind === 'user' ? 'steering' as const : 'context' as const, - ...promptRpcId(message), - message: { id: message.id, content: message.content as unknown as JsonValue[] }, - })), - ] -} - -/** Prompt-RPC identity carried by a browser-submitted message's user source. */ -function promptRpcId(message: UserMessage): Pick { - const source = message.source - return source.kind === 'user' && 'rpcId' in source ? { rpcId: source.rpcId } : {} -} - function jobView(job: JobSnapshot): SessionJob { return { id: job.id, diff --git a/packages/api/session-controller/src/types.ts b/packages/api/session-controller/src/types.ts index 607baed3cf..eec5a27085 100644 --- a/packages/api/session-controller/src/types.ts +++ b/packages/api/session-controller/src/types.ts @@ -521,19 +521,6 @@ export type SessionFollowFrame = | SessionEventEntry | { readonly type: 'assistant-stream'; readonly frame: SessionAssistantStreamFrame } -/** One pending inbox occurrence in the authoritative queue snapshot. */ -export interface SessionQueuedItem { - readonly id: MessageId - readonly placement: 'queued' | 'steering' | 'context' - /** Prompt-RPC identity from the queued message's user source; clients retire the matching local submission echo on it. */ - readonly rpcId?: SessionRequestId - /** JSON-safe message fields consumed by pending-queue presentation. */ - readonly message: { - readonly id: MessageId - readonly content: readonly JsonValue[] - } -} - /** Browser-safe background-job row. */ export interface SessionJob { readonly id: JobId @@ -547,7 +534,6 @@ export interface SessionJob { /** Complete live control baseline emitted once per control stream generation. */ export interface SessionControlBaseline { - readonly queues: Readonly> readonly jobs: Readonly> readonly projections: Readonly> } @@ -563,7 +549,6 @@ export interface SessionProjectionUpdate { /** Host-wide live state stream. Each generation starts with exactly one baseline. */ export type SessionControlFrame = | { readonly type: 'baseline'; readonly value: SessionControlBaseline } - | { readonly type: 'queue'; readonly sessionId: SessionId; readonly items: readonly SessionQueuedItem[] } | { readonly type: 'jobs'; readonly sessionId: SessionId; readonly jobs: readonly SessionJob[] } | ({ readonly type: 'projection' } & SessionProjectionUpdate) 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 d7559bc047..3103350ad8 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,3 +1,4 @@ +import { RemoteError } from '@deepseek-ai/dsh-typert-protocol' import { Context } from '@deepseek-ai/cordis' import AgentRegistry from '@deepseek-ai/dsh-agent' import type { Agent, Inbox, ModelSelectionRef } from '@deepseek-ai/dsh-agent' @@ -93,7 +94,9 @@ async function commandHarness( assembled: undefined, } const agents = { - resolveAgent: () => Promise.resolve({ agent }), + resolveAgent: (id: SessionId) => Promise.resolve(id === agent.id + ? { agent } + : { error: new RemoteError('session/not-found', 'missing', { sessionId: id }) }), selectionFor: () => selection, serializeImageAdmission: (_agent: Agent, operation: () => Promise) => operation(), composeAgent: () => Promise.resolve({ setup: () => {} }), @@ -113,6 +116,23 @@ async function expectFailure(operation: Promise, code: string): Promise } describe('Session queue commands', () => { + it('preserves the cold Agent resolver rejection', async () => { + const ctx = new Context() + await ctx.plugin(SessionStore) + await ctx.plugin(AgentRegistry) + const error = new RemoteError('session/agent-busy', 'owned by a child', { reason: 'subagent-owned' }) + const controller = new SessionCommandController(ctx, { + resolveAgent: () => Promise.resolve({ error }), + } as unknown as ApiSessionAgentController, '/workspace') + try { + await expect(controller.updateQueue({ + sessionId: SessionId('cold-child'), itemId: MessageId('pending'), action: { kind: 'remove' }, + })).rejects.toBe(error) + } finally { + await ctx.fiber.dispose() + } + }) + it('edits, removes, steers, and rejects stale queue occurrences', async () => { const { ctx, controller, agent, inbox, steer, cancel } = await commandHarness() const queued = createUserMessage({ content: [{ type: 'text', text: 'queued' }], source: { kind: 'user' } }) @@ -155,7 +175,7 @@ describe('Session queue commands', () => { await expectFailure(Promise.resolve().then(() => controller.updateQueue({ sessionId: agent.id, itemId: queued.id, action: { kind: 'steer' }, })), 'session/steer-unavailable') - expect(controller.updateQueue({ + expect(await controller.updateQueue({ sessionId: agent.id, itemId: queued.id, action: { kind: 'edit', content: [{ type: 'text', text: 'edited' }] }, @@ -164,14 +184,14 @@ describe('Session queue commands', () => { // An edit rewrites content in place, so the occurrence a client addressed // by id stays addressable. expect(inbox.nextTurn[0]?.id).toBe(queued.id) - expect(controller.updateQueue({ + expect(await controller.updateQueue({ sessionId: agent.id, itemId: nextStep.id, action: { kind: 'remove' }, })).toEqual({ accepted: true }) Object.assign(agent, { status: 'running' }) const steered = inbox.nextTurn[0] if (steered === undefined) throw new Error('missing edited queue item') - expect(controller.updateQueue({ + expect(await controller.updateQueue({ sessionId: agent.id, itemId: steered.id, action: { kind: 'steer' }, })).toEqual({ accepted: true }) expect(steer).toHaveBeenCalledWith(steered) @@ -184,7 +204,7 @@ describe('Session queue commands', () => { source: { kind: 'user', rpcId: 'file-rpc' as never }, }) inbox.append('next-turn', queuedFile) - expect(controller.updateQueue({ + expect(await controller.updateQueue({ sessionId: agent.id, itemId: queuedFile.id, action: { kind: 'steer' }, })).toEqual({ accepted: true }) expect(steer).toHaveBeenLastCalledWith(queuedFile) @@ -214,7 +234,7 @@ describe('Session queue commands', () => { inbox.append('next-turn', queued) inbox.append('next-step', context) - expect(controller.updateQueue({ + expect(await controller.updateQueue({ sessionId: agent.id, itemId: context.id, action: { kind: 'edit', content: [{ type: 'text', text: 'edited context' }] }, @@ -226,10 +246,10 @@ describe('Session queue commands', () => { }) expect(editedContext?.id).toBe(context.id) if (editedContext === undefined) throw new Error('missing edited context') - expect(controller.updateQueue({ + expect(await controller.updateQueue({ sessionId: agent.id, itemId: editedContext.id, action: { kind: 'remove' }, })).toEqual({ accepted: true }) - expect(controller.updateQueue({ + expect(await controller.updateQueue({ sessionId: agent.id, itemId: queued.id, action: { kind: 'steer' }, })).toEqual({ accepted: true }) expect(steer).toHaveBeenCalledWith(queued) @@ -251,7 +271,7 @@ describe('Session queue commands', () => { // command must accept whichever boundary `Agent.steer()` selects. steer.mockImplementation((message: UserMessage) => { inbox.append('next-turn', message) }) - expect(controller.updateQueue({ + expect(await controller.updateQueue({ sessionId: agent.id, itemId: first.id, action: { kind: 'steer' }, })).toEqual({ accepted: true }) expect(steer).toHaveBeenCalledWith(first) 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 a6073c5f2a..c4d4d17e1e 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 @@ -426,7 +426,7 @@ describe('Session file uploads', () => { await controller.prompt(promptRequest([{ type: 'file', receiptId: receipt.receiptId }])) const queued = followup.mock.calls[0]?.[0] as UserMessage agent.inbox.append('next-turn', queued) - expect(controller.updateQueue({ + expect(await controller.updateQueue({ sessionId: SESSION, itemId: queued.id, action: { kind: 'remove' }, 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 b3f1d9b19d..3a44506ea6 100644 --- a/packages/api/session-controller/tests/control-queue.host.spec.ts +++ b/packages/api/session-controller/tests/control-queue.host.spec.ts @@ -1,5 +1,5 @@ import { Context } from '@deepseek-ai/cordis' -import type { Agent, Inbox } from '@deepseek-ai/dsh-agent' +import type { Agent, Inbox, InboxState } from '@deepseek-ai/dsh-agent' import { createUserMessage } from '@deepseek-ai/dsh-llm' import { SessionId } from '@deepseek-ai/dsh-session' import { afterEach, describe, expect, it } from 'vitest' @@ -37,15 +37,15 @@ 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( +describe('Session control Inbox projection', () => { + /** Consume frames until the next durable Inbox value. */ + async function nextInboxFrame( iterator: AsyncIterator, - ): Promise> { + ): 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 + if (next.done) throw new Error('stream ended before an Inbox value') + if (next.value.type === 'projection' && next.value.key === 'inbox') return next.value } } @@ -64,23 +64,21 @@ describe('Session control queue projection', () => { expect(opened.value).toMatchObject({ type: 'baseline', value: { - queues: { - 'queue-session': [ - { id: queued.id, placement: 'queued' }, - { id: steering.id, placement: 'steering' }, - { id: context.id, placement: 'context' }, - ], + projections: { + 'queue-session': { values: { inbox: { + 'next-turn': [queued], 'next-step': [steering, context], + } } }, }, }, }) const replacement = message('replacement') inbox.append('next-turn', replacement) - const replaced = await nextQueueFrame(iterator) - expect(replaced.items.map(item => item.id)).toContain(replacement.id) + const replaced = await nextInboxFrame(iterator) + expect(replaced.value).toMatchObject({ 'next-turn': [queued, replacement] }) inbox.remove(steering.id) - const removed = await nextQueueFrame(iterator) - expect(removed.items.map(item => item.id)).not.toContain(steering.id) + const removed = await nextInboxFrame(iterator) + expect(removed.value).toMatchObject({ 'next-step': [context] }) abort.abort() await iterator.next() @@ -108,9 +106,6 @@ describe('Session control queue projection', () => { 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() @@ -129,33 +124,28 @@ describe('Session control queue projection', () => { const iterator = control.control(abort.signal)[Symbol.asyncIterator]() const opened = await iterator.next() if (opened.done || opened.value.type !== 'baseline') throw new Error('missing baseline') - const items = opened.value.value.queues['queue-session' as SessionId] ?? [] - expect(items.map(item => ({ id: item.id, placement: item.placement, rpcId: item.rpcId }))).toEqual([ - { id: identified.id, placement: 'queued', rpcId: 'req-42' }, - { id: items[1]?.id, placement: 'steering', rpcId: undefined }, - ]) - expect('rpcId' in (items[1] ?? {})).toBe(false) + const inboxValue = opened.value.value.projections['queue-session' as SessionId]?.values.inbox as unknown as InboxState + expect(inboxValue['next-turn'][0]?.source).toMatchObject({ kind: 'user', rpcId: 'req-42' }) + expect(inboxValue['next-step'][0]?.source).toEqual({ kind: 'user' }) abort.abort() await iterator.next() }) - it('ignores inbox events without the exact live Agent session', async () => { - const { ctx, control, agent, inbox } = await harness() + it('publishes Inbox values for sessions without a live Agent', async () => { + const { ctx, control } = await harness() const abort = new AbortController() const iterator = control.control(abort.signal)[Symbol.asyncIterator]() await iterator.next() - - const unrelated = ctx.sessions.create(SessionId('unrelated-queue')) - unrelated.append('agent/inbox/spliced', { - target: 'next-turn', - start: 0, - inserted: [message('unrelated')], + const session = ctx.sessions.create(SessionId('unattached-inbox')) + const pending = message('unattached') + session.append('agent/inbox/spliced', { + target: 'next-turn', start: 0, inserted: [pending], + }) + expect(ctx.agents.get(session.id)).toBeUndefined() + await expect(nextInboxFrame(iterator)).resolves.toMatchObject({ + sessionId: session.id, value: { 'next-turn': [pending], 'next-step': [] }, }) - const replacement = ctx.sessions.create(SessionId('replacement-session')) - Object.defineProperty(agent, 'session', { configurable: true, value: replacement }) - inbox.append('next-turn', message('wrong-session')) - abort.abort() await iterator.next() }) @@ -183,14 +173,14 @@ describe('Session control queue projection', () => { inbox.append('next-turn', first) inbox.append('next-turn', second) - const queues: Extract[] = [] + const values: InboxState[] = [] ownedContexts.delete(ctx) await ctx.fiber.dispose() for (;;) { const next = await iterator.next() if (next.done) break - if (next.value.type === 'queue') queues.push(next.value) + if (next.value.type === 'projection' && next.value.key === 'inbox') values.push(next.value.value as unknown as InboxState) } - expect(queues.map(queue => queue.items.map(item => item.id))).toEqual([[first.id], [first.id, second.id]]) + expect(values.map(value => value['next-turn'].map(item => item.id))).toEqual([[first.id], [first.id, second.id]]) }) }) diff --git a/packages/api/session-controller/tests/queue-store.client.spec.ts b/packages/api/session-controller/tests/queue-store.client.spec.ts deleted file mode 100644 index 573e4cfa7f..0000000000 --- a/packages/api/session-controller/tests/queue-store.client.spec.ts +++ /dev/null @@ -1,297 +0,0 @@ -/** - * Queue snapshot semantics: authoritative replacement after every host-side - * change, reconnect re-baselining, pre-instantiation buffering, editable-text - * projection, and snapshot reference stability. - */ -import { describe, expect, it, vi } from 'vitest' -import { createUserMessage } from '@deepseek-ai/dsh-llm' -import type { ContentBlock, UserMessage } from '@deepseek-ai/dsh-llm/types' -import { SessionSeq, type SessionEvent } from '@deepseek-ai/dsh-session/types' -import type { MessageId, RpcId, SessionId } from '@deepseek-ai/dsh-api-remotes/client' -import type { SessionControlFrame } from '@deepseek-ai/dsh-api-session-controller/types' -import { Session } from '../src/client/sessions/session.ts' -import { SessionManager } from '../src/client/sessions/manager.ts' -import { FakeApiClient, fakeRemote } from './fake-api.client.ts' - -const SID = 'fk-q1' as SessionId -const text = (value: string): ContentBlock[] => [{ type: 'text', text: value }] -const rid = (id: string): RpcId => id as RpcId -const iid = (id: string): MessageId => id as MessageId - -interface QueueFixture { - id: string - body: string - content?: ContentBlock[] - placement?: 'queued' | 'steering' - message?: UserMessage -} - -/** Build one authoritative queue snapshot. */ -function queueFrame(items: QueueFixture[]): Extract { - return { - type: 'queue', - sessionId: SID, - items: items.map(item => ({ - id: iid(item.id), - placement: item.placement ?? 'queued', - message: (item.message ?? createUserMessage({ - content: item.content ?? text(item.body), - source: { kind: 'user', rpcId: rid(`rpc-${item.id}`) } as never, - })) as never, - })), - } -} - -function makeSession(): Session { - return makeBench().session -} - -function makeBench(): { api: FakeApiClient; session: Session } { - const api = new FakeApiClient() - return { api, session: new Session(SID, fakeRemote(api)) } -} - -function makeManager(): SessionManager { - const api = new FakeApiClient() - return new SessionManager(fakeRemote(api)) -} - -describe('Session queue snapshot intake', () => { - it('projects stable ids, flat previews, and complete text', () => { - const session = makeSession() - session.handleControlFrame(queueFrame([ - { id: 'q-1', body: '第一条 排队\n消息' }, - ])) - const queue = session.getSnapshot().queue - expect(typeof queue[0]?.messageId).toBe('string') - expect(queue).toMatchObject([ - { - id: 'q-1', placement: 'queued', - content: [{ type: 'text', text: '第一条 排队\n消息' }], - preview: '第一条 排队 消息', text: '第一条 排队\n消息', - }, - ]) - }) - - it('marks mixed-content messages non-editable and keeps attachment blocks out of the text preview', () => { - const session = makeSession() - session.handleControlFrame(queueFrame([{ - id: 'q-image', - body: '', - content: [ - { type: 'text', text: 'hi' }, - { type: 'image', data: 'x' } as never, - { type: 'file', attachment: { attachmentId: 'file-1', name: 'notes.txt', bytes: 5 } } as never, - ], - }])) - const queue = session.getSnapshot().queue - expect(typeof queue[0]?.messageId).toBe('string') - expect(queue).toMatchObject([ - { - id: 'q-image', placement: 'queued', - content: [ - { type: 'text', text: 'hi' }, - { type: 'image', data: 'x' }, - { type: 'file', attachment: { attachmentId: 'file-1', name: 'notes.txt', bytes: 5 } }, - ], - // Attachment blocks render from `content`, so the preview carries - // only text; other foreign blocks keep their marker. - preview: 'hi', text: null, - }, - ]) - }) - - it('caps previews at 200 code points and preserves the full editable text', () => { - const session = makeSession() - const body = '长'.repeat(201) - session.handleControlFrame(queueFrame([{ id: 'q-cap', body }])) - const row = session.getSnapshot().queue[0] - expect(Array.from(row?.preview ?? '')).toHaveLength(201) - expect(row?.preview.endsWith('…')).toBe(true) - expect(row?.text).toBe(body) - }) - - it('replaces content, order, and membership from each authoritative frame', () => { - const session = makeSession() - session.handleControlFrame(queueFrame([ - { id: 'q-1', body: 'one' }, - { id: 'q-2', body: 'two' }, - ])) - session.handleControlFrame(queueFrame([ - { id: 'q-2', body: 'two edited' }, - ])) - const queue = session.getSnapshot().queue - expect(typeof queue[0]?.messageId).toBe('string') - expect(queue).toMatchObject([ - { - id: 'q-2', placement: 'queued', - content: [{ type: 'text', text: 'two edited' }], - preview: 'two edited', text: 'two edited', - }, - ]) - session.handleControlFrame(queueFrame([])) - expect(session.getSnapshot().queue).toEqual([]) - }) - - it('keeps the queue array reference stable across unrelated snapshot swaps', () => { - const session = makeSession() - session.handleControlFrame(queueFrame([{ id: 'q-stable', body: '稳定' }])) - const before = session.getSnapshot().queue - session.handleAgentError('unrelated') - expect(session.getSnapshot().queue).toBe(before) - }) - - it('retains steering placement and complete content in the same authoritative snapshot', () => { - const session = makeSession() - session.handleControlFrame(queueFrame([ - { id: 'q-next', body: 'later' }, - { id: 's-now', body: 'interrupt now', placement: 'steering' }, - ])) - - expect(session.getSnapshot().queue.map(item => ({ - id: item.id, placement: item.placement, content: item.content, - }))).toEqual([ - { id: 'q-next', placement: 'queued', content: text('later') }, - { id: 's-now', placement: 'steering', content: text('interrupt now') }, - ]) - }) - - it('hands off exactly one current occurrence when live steering becomes durable', async () => { - const { api, session } = makeBench() - await session.open() - const message = createUserMessage({ - content: text('same message'), - source: { kind: 'user' }, - }) - session.handleControlFrame(queueFrame([ - { id: 's-first', body: '', placement: 'steering', message }, - { id: 's-second', body: '', placement: 'steering', message }, - ])) - const durable = { - seq: SessionSeq(0), - time: 1_700_000_000_000, - type: 'user/message', - surfaceOp: 'append', - data: message, - } satisfies SessionEvent - - await api.pushFollow(SID, { type: 'event', event: durable as never }) - await vi.waitFor(() => { - expect(session.getSnapshot().queue.map(item => item.id)).toEqual(['s-second']) - }) - - session.handleControlFrame(queueFrame([ - { id: 's-later', body: '', placement: 'steering', message }, - ])) - await api.pushFollow(SID, { type: 'event', event: durable as never }) - await vi.waitFor(() => { - expect(session.getSnapshot().queue.map(item => item.id)).toEqual(['s-later']) - }) - }) - - it('hands off live steering when the agent claims it as a user message', async () => { - const { api, session } = makeBench() - await session.open() - const message = createUserMessage({ - content: text('claimed steering'), - source: { kind: 'user' }, - }) - session.handleControlFrame(queueFrame([ - { id: 's-claimed', body: '', placement: 'steering', message }, - ])) - - await api.pushFollow(SID, { - type: 'event', - event: { - seq: 0, - time: 1_700_000_000_000, - type: 'user/message', - surfaceOp: 'append', - data: message, - } as never, - }) - - await vi.waitFor(() => { - expect(session.getSnapshot().queue).toEqual([]) - }) - }) -}) - -describe('queue operation transport', () => { - it('addresses the session.updateQueue RPC without optimistic local mutation', async () => { - const api = new FakeApiClient() - const session = new Session(SID, fakeRemote(api)) - session.handleControlFrame(queueFrame([{ id: 'q-op', body: 'pending' }])) - const before = session.getSnapshot().queue - - await expect(session.updateQueue(iid('q-op'), { kind: 'edit', content: text('next') })) - .resolves.toEqual({ ok: true, value: { accepted: true } }) - await expect(session.updateQueue(iid('q-op'), { kind: 'steer' })) - .resolves.toEqual({ ok: true, value: { accepted: true } }) - expect(api.callsOf('session.updateQueue')).toEqual([ - { - sessionId: SID, - itemId: 'q-op', - action: { kind: 'edit', content: text('next') }, - }, - { - sessionId: SID, - itemId: 'q-op', - action: { kind: 'steer' }, - }, - ]) - expect(session.getSnapshot().queue).toBe(before) - }) -}) - -describe('queue reconnect semantics', () => { - it('a control baseline clears stale state before a fresh update lands', () => { - const session = makeSession() - session.handleControlFrame(queueFrame([{ id: 'q-old', body: '旧连接' }])) - session.replaceControl([]) - expect(session.getSnapshot().queue).toEqual([]) - session.handleControlFrame(queueFrame([{ id: 'q-new', body: '新基线' }])) - expect(session.getSnapshot().queue.map(row => row.id)).toEqual(['q-new']) - }) - - it('resync does not clear a baseline that raced ahead of the host connection signal', async () => { - const session = makeSession() - await session.open() - session.handleControlFrame(queueFrame([{ id: 'q-fresh', body: '新基线' }])) - await session.resync() - expect(session.getSnapshot().queue.map(row => row.id)).toEqual(['q-fresh']) - }) - - it('running-status changes never guess at queue retirement', () => { - const session = makeSession() - session.handleControlFrame(queueFrame([{ id: 'q-live', body: '保留' }])) - session.handleRunning(true) - session.handleRunning(false) - expect(session.getSnapshot().queue.map(row => row.id)).toEqual(['q-live']) - }) -}) - -describe('manager buffering of queue snapshots', () => { - it('replays only the latest snapshot for an uninstantiated session', () => { - const manager = makeManager() - manager.handleControlFrame(queueFrame([{ id: 'q-old', body: '旧' }])) - manager.handleControlFrame(queueFrame([{ id: 'q-new', body: '新' }])) - expect(manager.get(SID).getSnapshot().queue.map(row => row.id)).toEqual(['q-new']) - }) - - it('a control baseline replaces the prior queue', () => { - const manager = makeManager() - manager.handleControlFrame(queueFrame([{ id: 'q-g1', body: '第一代' }])) - const nextQueue = queueFrame([{ id: 'q-g2', body: '第二代' }]).items - manager.handleControlFrame({ - type: 'baseline', - value: { - queues: { [SID]: nextQueue }, - jobs: {}, - projections: {}, - }, - }) - const snapshot = manager.get(SID).getSnapshot() - expect(snapshot.queue.map(row => row.id)).toEqual(['q-g2']) - }) -}) 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 7bfe9732eb..94e82c6556 100644 --- a/packages/api/session-controller/tests/session-cold.host.spec.ts +++ b/packages/api/session-controller/tests/session-cold.host.spec.ts @@ -8,13 +8,13 @@ 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, { agentEvents, 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 { createInboxStub } from '@deepseek-ai/dsh-agent-loop-testkit' +import { createInboxStub, mountAgentLoopTestDependencies, mountAgentLoopTestHarness } 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 AttachmentStore from '@deepseek-ai/dsh-attachment' @@ -22,6 +22,7 @@ import type { SessionPromptRequest, SessionRequestId } from '../src/types.ts' import { SessionPersistenceRevision, type SessionPersistenceSnapshot, + type SessionHandle, SessionAccess, } from '@deepseek-ai/dsh-session-persistence' import { createSessionTestRemote, @@ -273,8 +274,8 @@ describe('cold history recovery view', () => { describe('Remote Agent and Session lookup policy', () => { it('resumes a cold session before mutating a restored queue row', async () => { const ctx = new Context() - await ctx.plugin(SessionStore) - await ctx.plugin(AgentRegistry) + await mountAgentLoopTestDependencies(ctx) + await mountAgentLoopTestHarness(ctx) const sessionId = sid('session-cold-queue-mutation') const meta = header(sessionId, 1000) const message = createUserMessage({ @@ -290,35 +291,19 @@ describe('Remote Agent and Session lookup policy', () => { providePersistence(ctx, { list: () => Promise.resolve([meta]), inspect: () => Promise.resolve({ meta, events }), - locate: () => undefined, - }) - let resumedAgent: Agent | undefined - const resume = vi.spyOn(ctx.agents, 'resume').mockImplementation(async () => { - const session = ctx.sessions.create(sessionId, { - seed: events, - meta: { cwd: '/proj', createdAt: meta.createdAt }, - }) - resumedAgent = { - id: session.id, - options: {}, - session, - inbox: undefined as never, - status: 'idle', - ctx, - send() {}, - followup() {}, - steer() {}, - inject() {}, - cancel() {}, - runMaintenance: task => task(new AbortController().signal), - whenIdle: () => Promise.resolve(), - } satisfies Agent - Object.assign(resumedAgent, { - inbox: new Inbox(ctx, resumedAgent.session, agentEvents(ctx, resumedAgent)), - }) - ctx.agents.register(resumedAgent) - return { agent: resumedAgent, dispose: () => Promise.resolve() } + open: (_id: SessionId, access: SessionAccess): Promise => Promise.resolve({ + id: sessionId, + header: meta, + inheritedEventCount: SessionLogOffset(0), + access, + read: () => Promise.resolve({ eventState: 'detached', events: structuredClone(events) }), + append: (appended) => { events.push(...appended); return Promise.resolve() }, + flush: () => Promise.resolve(), + close: () => Promise.resolve(), + [Symbol.asyncDispose]: () => Promise.resolve(), + }), }) + const resume = vi.spyOn(ctx.agents, 'resume') const remote = createSessionTestRemote(ctx, { defaultModelSelection: () => ({ provider: 'p', model: 'm' }), cwd: '/tmp', @@ -332,11 +317,13 @@ describe('Remote Agent and Session lookup policy', () => { expect(response).toEqual({ ok: true, value: { accepted: true } }) expect(resume).toHaveBeenCalledOnce() + const resumedAgent = ctx.agents.get(sessionId) expect(resumedAgent?.inbox.nextTurn).toEqual([]) - expect(resumedAgent?.session.events.at(-1)).toMatchObject({ + expect(resumedAgent?.session.snapshotEvents().at(-1)).toMatchObject({ type: 'agent/inbox/spliced', data: { target: 'next-turn', start: 0, removedCount: 1, inserted: [], outcome: 'canceled' }, }) + await ctx.fiber.dispose() }) it('keeps queue-item-not-found for a cold session when no persistence backend is composed', async () => { @@ -355,7 +342,7 @@ describe('Remote Agent and Session lookup policy', () => { })) expect(response.ok).toBe(false) - if (!response.ok) expect(response.error.code).toBe('queue-item-not-found') + if (!response.ok) expect(response.error.code).toBe('session/queue-item-not-found') }) it('deduplicates a cold resume across Agent and Session parameters', async () => { @@ -473,8 +460,7 @@ describe('Remote Agent and Session lookup policy', () => { const resolution = lookup.resolve(sessionId) - await expect(resolution).rejects.toBeInstanceOf(TypertLookupFailure) - await expect(resolution).rejects.toMatchObject({ failure: { code: 'agent-busy' } }) + await expect(resolution).rejects.toMatchObject({ code: 'session/agent-busy' }) }) }) diff --git a/packages/api/session-controller/tests/session-pending-submissions.client.spec.ts b/packages/api/session-controller/tests/session-pending-submissions.client.spec.ts index 46cfe1c049..8eed0d1a9b 100644 --- a/packages/api/session-controller/tests/session-pending-submissions.client.spec.ts +++ b/packages/api/session-controller/tests/session-pending-submissions.client.spec.ts @@ -7,7 +7,7 @@ import { SessionSeq, type SessionEvent, type SessionId } from '@deepseek-ai/dsh- import { RemoteError } from '@deepseek-ai/dsh-typert-protocol' import { Session } from '../src/client/sessions/session.ts' import type { PendingSubmissionRetirement } from '../src/client/contract/session.ts' -import type { SessionQueuedItem, SessionRequestId } from '../src/types.ts' +import type { SessionRequestId } from '../src/types.ts' import { FakeApiClient, err, fakeRemote, ok } from './fake-api.client.ts' import { historyValue } from './event-script.client.ts' @@ -60,21 +60,17 @@ function promptEvent(seq: SessionSeq, rpcId: SessionRequestId, refs: readonly At } as unknown as SessionEvent } -function queuedItem(rpcId: SessionRequestId, refs: readonly AttachmentRef[] = []): SessionQueuedItem { - return { - id: 'm-queued' as SessionQueuedItem['id'], - placement: 'queued', - rpcId, - message: { - id: 'm-queued' as SessionQueuedItem['id'], - content: refs.map(attachmentBlock) as unknown as SessionQueuedItem['message']['content'], - }, - } +function queuedItem(rpcId: SessionRequestId, refs: readonly AttachmentRef[] = []) { + return createUserMessage({ + source: { kind: 'user', rpcId }, + content: refs.map(attachmentBlock), + }) } /** Let the frame-delayed retirement (setTimeout fallback in this node environment) run. */ -function settleFrames(): Promise { - return new Promise(resolve => setTimeout(resolve, 0)) +async function settleFrames(): Promise { + await Promise.resolve() + await new Promise(resolve => setTimeout(resolve, 0)) } describe('beginSubmission', () => { @@ -185,6 +181,25 @@ describe('observed retirement', () => { expect(retirements).toEqual([{ reason: 'observed', attachments: refs }]) }) + it('retires an accepted echo when a claim clears the projection before its notification', async () => { + const { api, session } = makeSession() + api.onHistory = () => Promise.resolve(ok(historyValue([]))) + await session.open() + const onRetire = vi.fn() + const handle = session.beginSubmission({ mode: 'steer', text: 'accepted', attachments: [], onRetire }) + const refs = [imageRef('claimed-image')] + const message = queuedItem(handle.requestId, refs) + session.projections.apply('inbox', { 'next-turn': [], 'next-step': [message] }, SessionSeq(0)) + session.projections.apply('inbox', { 'next-turn': [], 'next-step': [] }, SessionSeq(1)) + await api.pushFollow(SID, { type: 'event', event: { + type: 'agent/inbox/spliced', seq: SessionSeq(0), time: 1, + data: { target: 'next-step', start: 0, inserted: [message] }, + } as never }) + await settleFrames() + expect(session.getSnapshot().pendingSubmissions).toEqual([]) + expect(onRetire).toHaveBeenCalledExactlyOnceWith({ reason: 'observed', attachments: refs }) + }) + it('a queue occurrence carrying the rpcId retires the echo (running-turn submissions)', async () => { const { session } = makeSession() const retirements: PendingSubmissionRetirement[] = [] @@ -196,12 +211,14 @@ describe('observed retirement', () => { onRetire: retirement => retirements.push(retirement), }) const refs = [imageRef('att-q')] - session.handleControlFrame({ type: 'queue', sessionId: SID, items: [queuedItem(handle.requestId, refs)] }) + session.projections.apply('inbox', { 'next-turn': [queuedItem(handle.requestId, refs)], 'next-step': [] }, SessionSeq(1)) await settleFrames() expect(session.getSnapshot().pendingSubmissions).toEqual([]) expect(retirements).toEqual([{ reason: 'observed', attachments: refs }]) // The queue projection keeps the correlation id for render-time dedupe. - expect(session.getSnapshot().queue).toMatchObject([{ rpcId: handle.requestId }]) + expect(session.projections.get('inbox')).toMatchObject({ + 'next-turn': [{ source: { rpcId: handle.requestId } }], + }) }) it('retires a mixed echo with durable references in original selection order', async () => { @@ -263,9 +280,7 @@ describe('observed retirement', () => { attachments: [], onRetire: retirement => retirements.push(retirement), }) - session.handleControlFrame({ - type: 'queue', sessionId: SID, items: [queuedItem(handle.requestId, [])], - }) + session.projections.apply('inbox', { 'next-turn': [queuedItem(handle.requestId, [])], 'next-step': [] }, SessionSeq(1)) await api.pushFollow(SID, { type: 'event', event: promptEvent(SessionSeq(0), handle.requestId) as never, }) 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 90ffb10456..3339a97d14 100644 --- a/packages/api/session-controller/tests/session-projections.host.spec.ts +++ b/packages/api/session-controller/tests/session-projections.host.spec.ts @@ -236,7 +236,10 @@ describe('session.history projections block', () => { }) it('reconstructs a cold persisted queue without publishing or resuming an Agent', async () => { - const { ctx } = await harness(true) + const ctx = new Context() + ownedContexts.add(ctx) + await mountAgentLoopTestDependencies(ctx) + await mountAgentLoopTestHarness(ctx) const coldId = SessionId('cold-persisted-queue') const meta: SessionHeader = { version: SESSION_FORMAT_VERSION, id: coldId, createdAt: 1, cwd: '/tmp', isSeeded: false } const message = createUserMessage({ diff --git a/packages/client/ui-chat/src/client/chat/ChatView.tsx b/packages/client/ui-chat/src/client/chat/ChatView.tsx index 8ff8eca3e0..85949e7490 100644 --- a/packages/client/ui-chat/src/client/chat/ChatView.tsx +++ b/packages/client/ui-chat/src/client/chat/ChatView.tsx @@ -6,6 +6,7 @@ import type { ConversationTimelineSnapshot, RenderMessageImages, } from '@deepseek-ai/dsh-client-ui-conversation/client' import type { SessionSeq } from '@deepseek-ai/dsh-session/types' +import type { InboxState } from '@deepseek-ai/dsh-agent/types' import { Button, IconChevronDownOutline14, Modal } from '@deepseek-ai/dsh-client-ui-primitives' import type { ChatViewSlotProps, OpenFileOptions } from '../contract/slots.ts' import type { ChatSnapshot } from '../contract/snapshot.ts' @@ -139,7 +140,7 @@ function openFailureMessage(error: unknown, fallback: string): string { function observedRpcIds( order: readonly string[], nodes: ChatSnapshot['nodes'], - queue: readonly { readonly rpcId?: string }[], + inbox: InboxState | undefined, ): ReadonlySet { const observed = new Set() for (const key of order) { @@ -150,8 +151,8 @@ function observedRpcIds( | undefined if (source?.kind === 'user' && typeof source.rpcId === 'string') observed.add(source.rpcId) } - for (const item of queue) { - if (item.rpcId !== undefined) observed.add(item.rpcId) + for (const { source } of [...inbox?.['next-turn'] ?? [], ...inbox?.['next-step'] ?? []]) { + if (source.kind === 'user' && 'rpcId' in source) observed.add(source.rpcId) } return observed } @@ -233,7 +234,7 @@ export function ChatView({ [turnNavigationItems, turnOutline], ) const timeline = useChat(s => s.timeline) - const inbox = useSession(s => s.queue) + const inbox = useProjection('inbox') as unknown as InboxState | undefined // Workspace root off the session list row: path summaries display relative to it. const cwd = useSessions(s => s.byId[sessionId]?.cwd) const running = useSession(s => s.running) @@ -281,7 +282,7 @@ export function ChatView({ }, []) const pendingSteering = useMemo( - () => inbox.filter(item => item.placement === 'steering'), + () => inbox?.['next-step'].filter(message => message.source.kind === 'user') ?? [], [inbox], ) const pendingSubmissions = useSession(s => s.pendingSubmissions) diff --git a/packages/client/ui-chat/tests/chat-view.client.spec.tsx b/packages/client/ui-chat/tests/chat-view.client.spec.tsx index 5e9033bdd0..7da7ba213e 100644 --- a/packages/client/ui-chat/tests/chat-view.client.spec.tsx +++ b/packages/client/ui-chat/tests/chat-view.client.spec.tsx @@ -1,5 +1,6 @@ // @vitest-environment jsdom +import type { InboxState } from '@deepseek-ai/dsh-agent/types' import type { GlobalStandardProps } from '@deepseek-ai/dsh-client-ui-slots' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react' @@ -60,10 +61,13 @@ beforeEach(() => { const SID = 's1' as SessionId type RoutedChatNodeOwner = ChatNodeOwnerProps & { readonly node: ChatNode } -function sessionSnapshot(overrides: Partial = {}): SessionSnapshot { +interface TestSessionSnapshot extends SessionSnapshot { + readonly testInbox?: InboxState +} + +function sessionSnapshot(overrides: Partial = {}): TestSessionSnapshot { return { sessionId: SID, - queue: [], pendingSubmissions: [], running: false, removed: false, @@ -82,11 +86,11 @@ function sessionSnapshot(overrides: Partial = {}): SessionSnaps } /** Scripted Session source: set() swaps the top-level object like the real Controller binding. */ -function makeSessionSource(init: Partial = {}) { +function makeSessionSource(init: Partial = {}) { let snap = sessionSnapshot(init) const subs = new Set<() => void>() return { - set: (next: Partial) => { + set: (next: Partial) => { snap = { ...snap, ...next } for (const fn of [...subs]) fn() }, @@ -103,7 +107,7 @@ function makeSessionSource(init: Partial = {}) { type ChatSlice = Partial & { readonly turnUsages?: NonNullable[0]>['turnUsages'] } -type HarnessUpdate = ChatSlice & Partial & { readonly chat?: ChatSnapshot } +type HarnessUpdate = ChatSlice & Partial & { readonly chat?: ChatSnapshot } /** Scripted Chat target source, independent from Session lifecycle state. */ function makeChatSource(init: ChatSlice = {}, snapshot?: ChatSnapshot) { @@ -226,7 +230,7 @@ function bindKeyedSnapshotSelector( function makeHarness( init: HarnessUpdate = {}, - sessionOverrides: Partial = {}, + sessionOverrides: Partial = {}, chatSnapshot?: ChatSnapshot, ) { const { @@ -242,6 +246,7 @@ function makeHarness( ...(turnUsages === undefined ? {} : { turnUsages }), } const session = makeSessionSource({ ...sessionInit, ...sessionOverrides }) + const useTestSession = bindSnapshotSelector(session.source) const chatSource = makeChatSource(chatSlice, initialChat ?? chatSnapshot) const useChatNode = bindKeyedSnapshotSelector( key => chatSource.source.getSnapshot().nodes.source(key), @@ -378,7 +383,10 @@ function makeHarness( createSnapshotStore(new Map()), ), useWorkspaces: emptyWorkspaces(), - useProjection: () => outlineValue, + useProjection: (key: string) => { + const inbox = useTestSession(snapshot => snapshot.testInbox) + return key === 'inbox' ? inbox : outlineValue + }, useInput: (() => { throw new Error('unused') }), inputActions: { setDraft: () => {}, @@ -949,23 +957,23 @@ describe('ChatView', () => { }) const pending = { id: 'steer-occurrence' as never, - messageId: 'steer-message' as never, - placement: 'steering' as const, + role: 'user' as const, + source: { kind: 'user' as const }, content: [{ type: 'text' as const, text: 'interrupt now' }], preview: 'interrupt now', text: 'interrupt now', } const queued = { id: 'queued-occurrence' as never, - messageId: 'queued-message' as never, - placement: 'queued' as const, + role: 'user' as const, + source: { kind: 'user' as const }, content: [{ type: 'text' as const, text: 'later' }], preview: 'later', text: 'later', } const h = makeHarness( { nodes: [assistant(1, 'working')] }, - { queue: [queued, pending], running: true }, + { testInbox: { 'next-turn': [queued], 'next-step': [pending] }, running: true }, ) const view = render() @@ -980,12 +988,12 @@ describe('ChatView', () => { & Node.DOCUMENT_POSITION_FOLLOWING).not.toBe(0) act(() => { - h.setSession({ queue: [queued] }) + h.setSession({ testInbox: { 'next-turn': [queued], 'next-step': [] } }) h.setChat({ nodes: [ assistant(1, 'working'), { - kind: 'steering', messageId: pending.messageId, + kind: 'steering', messageId: pending.id, seq: 2, time: 2_000, content: [{ type: 'text', text: 'interrupt now' }], source: null, }, @@ -1017,8 +1025,8 @@ describe('ChatView', () => { it('keeps a later pending occurrence visible when it reuses a durable MessageId', () => { const pending = { id: 'steer-occurrence-later' as never, - messageId: 'shared-steer-message' as never, - placement: 'steering' as const, + role: 'user' as const, + source: { kind: 'user' as const }, content: [{ type: 'text' as const, text: 'same steering' }], preview: 'same steering', text: 'same steering', @@ -1028,7 +1036,7 @@ describe('ChatView', () => { kind: 'user', seq: 2, time: 2_000, content: pending.content, source: null, }], - }, { queue: [pending], running: true }) + }, { testInbox: { 'next-turn': [], 'next-step': [pending] }, running: true }) const view = render() expect(view.getAllByText('same steering')).toHaveLength(2) @@ -1094,15 +1102,12 @@ describe('ChatView', () => { act(() => { h.setSession({ - queue: [{ + testInbox: { 'next-turn': [], 'next-step': [{ id: 'steer-occurrence' as never, - messageId: 'steer-message' as never, - placement: 'steering', - rpcId: 'req-steer' as never, content: [{ type: 'text', text: '带图纠偏' }], - preview: '带图纠偏', - text: '带图纠偏', - }], + + role: 'user', source: { kind: 'user', rpcId: 'req-steer' as never }, + }] }, }) }) expect(view.getAllByText('带图纠偏')).toHaveLength(1) @@ -1127,15 +1132,12 @@ describe('ChatView', () => { expect(view.queryByText('排队中')).toBeNull() act(() => { h.setSession({ - queue: [{ + testInbox: { 'next-step': [], 'next-turn': [{ id: 'q-occurrence' as never, - messageId: 'q-message' as never, - placement: 'queued' as const, - rpcId: 'req-q' as never, content: [{ type: 'text' as const, text: '排队中' }], - preview: '排队中', - text: '排队中', - }], + + role: 'user', source: { kind: 'user', rpcId: 'req-q' as never }, + }] }, }) }) // The queued occurrence and its local predecessor both belong to the @@ -2152,14 +2154,12 @@ describe('ChatView', () => { expect(status.textContent).toMatch(/^深度求索中\.\.\.2分0\d秒$/) expect(status.querySelector('[aria-hidden="true"]')).not.toBeNull() act(() => { - h.setSession({ queue: [{ + h.setSession({ testInbox: { 'next-turn': [], 'next-step': [{ id: 'steering-occurrence' as never, - messageId: 'steering-message' as never, - placement: 'steering', content: [{ type: 'text', text: 'also' }], - preview: 'also', - text: 'also', - }] }) + + role: 'user', source: { kind: 'user' }, + }] } }) }) expect(status.textContent).toMatch(/^深度求索中\.\.\.2分0\d秒$/) }) diff --git a/packages/client/ui-conversation/README.i18n.yaml b/packages/client/ui-conversation/README.i18n.yaml index a1eea2e97a..cbcb952003 100644 --- a/packages/client/ui-conversation/README.i18n.yaml +++ b/packages/client/ui-conversation/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/client/ui-conversation/README.md -README.md: 19a210bb200dae359920e85ff944b68242f45b4b -README.zh.md: c5a56871eb99607c924b2b2e048de72d918e6a8b +README.md: 7d2b81362964793020581a09c5ff43e8a76129f5 +README.zh.md: 05eadc5e9d022d5b6876f788602fd1e700ce2f94 diff --git a/packages/client/ui-conversation/README.md b/packages/client/ui-conversation/README.md index 19a210bb20..7d2b813629 100644 --- a/packages/client/ui-conversation/README.md +++ b/packages/client/ui-conversation/README.md @@ -44,7 +44,7 @@ View selection is deterministic: a registered persisted selection wins, otherwis The shell reads the persisted View preference before rendering when a Session first binds or a cached Session becomes current, activates the registered preferred View or Chat fallback, and activates later tab or focus selections before committing them to the store. A blank Session still omits the `conversation.view` slot; no unselected target is activated. -The resident composer survives no-Session and Session transitions. The no-Session state keeps the same composer surface mounted but inert while the Workspace picker connects a blank Session. The surface is a shell-owned Lexical editor: reference chips are atomic decorator nodes carrying the owner's serialization identity (submission expands them through the owner codec), claimed slash commands stay styled leading text, folder text references carry the folder glyph as an icon prefix, and the draft's clipboard projection is mirrored into the per-Session Conversation store. Queue operations address exact queue occurrences through the scoped `ctx.conversation` service; queue previews render sent text through the shared inline reference projection from `ui-primitives` (wire session forms fold to their label) and show local or durable images and files in original attachment order. Images use thumbnails; files use compact name-and-size cards. An edit exposes the literal sent text, and durable thumbnails resolve through the session image URL cache. Busy Enter behavior is stored in the Host-backed `ui-conversation` settings namespace. +The resident composer survives no-Session and Session transitions. The no-Session state keeps the same composer surface mounted but inert while the Workspace picker connects a blank Session. The surface is a shell-owned Lexical editor: reference chips are atomic decorator nodes carrying the owner's serialization identity (submission expands them through the owner codec), claimed slash commands stay styled leading text, folder text references carry the folder glyph as an icon prefix, and the draft's clipboard projection is mirrored into the per-Session Conversation store. QueueDock reads `next-turn` directly from the Session `inbox` projection, including cold recovered messages. Queue operations address exact queue occurrences through the scoped `ctx.conversation` service; queue previews render sent text through the shared inline reference projection from `ui-primitives` (wire session forms fold to their label) and show local or durable images and files in original attachment order. Images use thumbnails; files use compact name-and-size cards. An edit exposes the literal sent text, and durable thumbnails resolve through the session image URL cache. Busy Enter behavior is stored in the Host-backed `ui-conversation` settings namespace. Default sends commit optimistically: Enter clears the draft, occurrence table, and undo history in the same transaction, keeps the composer in `plain`, and runs the send as a detached attempt, so typing and further sends continue during the flight. `sendSession` registers a Session submission echo (`session.beginSubmission`) with the delivery mode before serializing, preserving selected image and file order in `pendingSubmissions`; Session derives the placement from that mode and its current running state, so idle sends use the transcript, busy Queue sends use QueueDock, and busy Steer sends use the pending-steering surface. It then yields one paint, encodes images through the browser's native `FileReader` data-URL path, and cites staged file receipts. Command submissions use the same receipts for generic files, so sending `/goal` or `/plan` never reads those browser files again. The prompt reuses the submission `requestId`; queue and history observation by that `rpcId` retires the echo once. Concurrent failures are restored together in submission order until the user edits the restored content; command submissions keep the frozen `submitting` phase. Detached attempts retain their attachment ids through admission and Session scope disposal. An observed retirement immediately exposes each image preview through the durable cache, replaces it with the canonical URL after fetching the admitted attachment, revokes each URL after its use ends, and releases file cards. Selected generic files enter one FIFO background-upload queue; `maxConcurrentFileUploads` defaults to two active Worker transports, the Conversation service retains queued and active operations plus byte progress across Session navigation, and removing a draft skips its queued transfer or aborts its active transport. Continuable subagents disable attachment intake and skip local echoes because their transport does not preserve the browser request id. diff --git a/packages/client/ui-conversation/README.zh.md b/packages/client/ui-conversation/README.zh.md index c5a56871eb..05eadc5e9d 100644 --- a/packages/client/ui-conversation/README.zh.md +++ b/packages/client/ui-conversation/README.zh.md @@ -44,7 +44,7 @@ View 选择规则固定:有效且已注册的持久化选择优先,其次是 Session 首次绑定或缓存的 Session 成为 current 时,shell 会在渲染前读取持久化 View 偏好,激活已注册的偏好 View 或 Chat fallback,并在后续 tab 或 focus 选择写入 store 前先激活对应 target。blank Session 仍不渲染 `conversation.view` slot;未选中的 target 不会激活。 -常驻 composer 在无 Session 与有 Session 之间保持挂载。无 Session 时,同一个编辑器表面保持 inert,Workspace picker 连接 blank Session。该表面是 shell 所有的 Lexical 编辑器:引用 chip 是携带 owner 序列化身份的原子 decorator 节点(提交时经 owner codec 展开),已认领的 slash command 保持为带样式的行首文本,文件夹文本引用以图标前缀携带文件夹图形,草稿的剪贴板投影镜像到逐 Session Conversation store。Queue 操作通过 scoped `ctx.conversation` service 寻址准确的 queue occurrence;queue 预览经 `ui-primitives` 的共享行内引用投影渲染已发送文本(wire 会话形式折叠为其标签),并按原始附件顺序展示本地或持久化的图片和文件。图片使用缩略图,文件使用紧凑的名称与大小卡片。编辑态展示字面发送文本,持久化缩略图通过会话图片 URL 缓存解析。繁忙时 Enter 行为保存在 Host-backed `ui-conversation` settings namespace。 +常驻 composer 在无 Session 与有 Session 之间保持挂载。无 Session 时,同一个编辑器表面保持 inert,Workspace picker 连接 blank Session。该表面是 shell 所有的 Lexical 编辑器:引用 chip 是携带 owner 序列化身份的原子 decorator 节点(提交时经 owner codec 展开),已认领的 slash command 保持为带样式的行首文本,文件夹文本引用以图标前缀携带文件夹图形,草稿的剪贴板投影镜像到逐 Session Conversation store。QueueDock 直接从 Session 的 `inbox` 投影读取 `next-turn`,包含从冷状态恢复的消息。Queue 操作通过 scoped `ctx.conversation` service 寻址准确的 queue occurrence;queue 预览经 `ui-primitives` 的共享行内引用投影渲染已发送文本(wire 会话形式折叠为其标签),并按原始附件顺序展示本地或持久化的图片和文件。图片使用缩略图,文件使用紧凑的名称与大小卡片。编辑态展示字面发送文本,持久化缩略图通过会话图片 URL 缓存解析。繁忙时 Enter 行为保存在 Host-backed `ui-conversation` settings namespace。 默认发送采用乐观提交:Enter 在同一事务里清空草稿、occurrence 表和撤销历史,composer 保持 `plain`,发送作为 detached attempt 运行,发送期间可以继续输入和提交。`sendSession` 在序列化之前用投递模式注册 Session 提交回显(`session.beginSubmission`),并在 `pendingSubmissions` 中保留图片与文件的选择顺序;Session 根据该模式与当前运行状态推导位置,因此空闲发送进入 transcript,繁忙时 Queue 进入 QueueDock,繁忙时 Steer 进入 pending-steering 区域。随后让出一帧,图片经浏览器原生 `FileReader` data-URL 路径编码,文件则引用已暂存凭证。命令提交也用同一凭证表示通用文件,因此发送 `/goal` 或 `/plan` 时不会再次读取这些浏览器文件。prompt 复用提交 `requestId`;queue 或历史以同一 `rpcId` 被观察后,回显只退休一次。多个并发发送失败时,在用户编辑还原内容之前按提交顺序合并还原;命令提交保持冻结的 `submitting` 阶段。Detached attempt 持有附件 id,直到 admission 完成或 Session scope 销毁。回显以 observed 退休时,durable 图片缓存立即公开每个预览 URL,读取 admitted 附件后用规范化 URL 替换预览,并在各 URL 停止使用后撤销,同时释放文件卡。选中的通用文件进入同一个先进先出的后台上传队列;`maxConcurrentFileUploads` 默认允许两个 Worker transport 同时运行,Conversation service 在切换 Session 时继续持有排队和运行中的传输操作及字节进度,移除草稿会跳过排队中的传输或中止正在运行的传输。continuable 子代理禁用附件入口,也不创建本地回显,因为其 transport 不保留浏览器 request id。 diff --git a/packages/client/ui-conversation/package.json b/packages/client/ui-conversation/package.json index 987aa9574f..63959771be 100644 --- a/packages/client/ui-conversation/package.json +++ b/packages/client/ui-conversation/package.json @@ -93,7 +93,8 @@ "@lexical/utils": "^0.49.0", "@lexical/text": "^0.49.0", "@lexical/plain-text": "^0.49.0", - "@lexical/history": "^0.49.0" + "@lexical/history": "^0.49.0", + "@deepseek-ai/dsh-agent": "workspace:^" }, "files": [ "lib/index.js", diff --git a/packages/client/ui-conversation/src/client/contract/queue.ts b/packages/client/ui-conversation/src/client/contract/queue.ts deleted file mode 100644 index 4e54568448..0000000000 --- a/packages/client/ui-conversation/src/client/contract/queue.ts +++ /dev/null @@ -1,11 +0,0 @@ -/** Queue contracts derived from the Session Controller face. */ -import type { SessionFace, SessionSnapshot } from '@deepseek-ai/dsh-api-session-controller/client' - -/** One address accepted by the Session Controller's queue mutation verb. */ -export type QueueItemId = Parameters[0] - -/** One mutation accepted by the Session Controller's queue mutation verb. */ -export type QueueAction = Parameters[1] - -/** One row projected by the authoritative Session queue snapshot. */ -export type QueueRow = SessionSnapshot['queue'][number] diff --git a/packages/client/ui-conversation/src/client/input/facade.ts b/packages/client/ui-conversation/src/client/input/facade.ts index 1cacd83812..7dc12bc259 100644 --- a/packages/client/ui-conversation/src/client/input/facade.ts +++ b/packages/client/ui-conversation/src/client/input/facade.ts @@ -9,6 +9,7 @@ * listeners onto it. */ import type { Context } from '@deepseek-ai/cordis' +import type { InboxState } from '@deepseek-ai/dsh-agent/types' import { createSnapshotStore, type ObservableSnapshot, type SnapshotStore, } from '@deepseek-ai/dsh-client-store' @@ -23,7 +24,7 @@ import { mergeRegister } from '@lexical/utils' import type { ArbitrateKey, ArbitrateOutcome, CommandClaim, ConsumeTokenRequest, DraftAttachmentId, InputActions, InputEffect, InputNotice, InputState, InputTriggerController, PickOutcome, - Occurrence, QueuedMessage, ReferenceInsert, SessionInput, SubmitAttempt, SubmitAttachment, + Occurrence, ReferenceInsert, SessionInput, SubmitAttempt, SubmitAttachment, SubmitOutcome, TokenSpan, } from '../contract/input.ts' import type { InputSubmitMode } from '../contract/composer-submission.ts' @@ -53,8 +54,8 @@ export interface SessionInputDeps { inputTriggers?: (() => InputTriggerController | undefined) | undefined /** PopupSelect shell face resolver (dismissal on submit lock / escape). */ popup?: (() => PopupDismissFace | undefined) | undefined - /** Queue read face; overlaid onto InputState.queue (absent = empty). */ - queue?: ObservableSnapshot | undefined + /** Agent Inbox projection; its next-turn list is overlaid onto InputState.queue. */ + inbox?: ObservableSnapshot | undefined /** * Steer every still-pending queued message into the running turn, in FIFO * order (the empty-draft accelerated-Enter gesture); absent = unsupported. @@ -97,7 +98,7 @@ function projectionContentChanged(prev: EditorProjection, next: EditorProjection }) } -const EMPTY_QUEUE: readonly QueuedMessage[] = [] +const EMPTY_QUEUE: InboxState['next-turn'] = [] /** No-pipeline lexicon: zero text-ref decorations. */ const EMPTY_LEXICON: ReadonlyMap<'/' | '@', readonly string[]> = new Map() @@ -170,6 +171,8 @@ export class SessionInputShell implements SessionInput { readonly attachmentIds: readonly DraftAttachmentId[] }>() + private readonly unsubscribeInbox: (() => void) | undefined + constructor(private readonly deps: SessionInputDeps) { this.editor = createEditor({ namespace: 'dsh-composer', @@ -185,7 +188,7 @@ export class SessionInputShell implements SessionInput { () => { this.lexiconOff?.() }, ) this.state = createSnapshotStore(this.compose()) - deps.queue?.subscribe(() => { this.publish() }) + this.unsubscribeInbox = deps.inbox?.subscribe(() => { this.publish() }) } // ---- editor plumbing ---- @@ -579,6 +582,7 @@ export class SessionInputShell implements SessionInput { } this.disposed = true this.dispatchRun(({ type: 'release' })) + this.unsubscribeInbox?.() this.unregister() this.editor.setRootElement(null) this.detachedDrafts.clear() @@ -909,7 +913,7 @@ export class SessionInputShell implements SessionInput { phase: core.phase, ...(core.claim !== undefined ? { claim: core.claim } : {}), occurrences: this.projection.occurrences, - queue: this.deps.queue?.getSnapshot() ?? EMPTY_QUEUE, + queue: this.deps.inbox?.getSnapshot()?.['next-turn'] ?? EMPTY_QUEUE, } } diff --git a/packages/client/ui-conversation/src/client/input/hub.ts b/packages/client/ui-conversation/src/client/input/hub.ts index f255877ddb..d9e8f8fe52 100644 --- a/packages/client/ui-conversation/src/client/input/hub.ts +++ b/packages/client/ui-conversation/src/client/input/hub.ts @@ -13,7 +13,8 @@ import type { } from '@deepseek-ai/dsh-api-session-controller/client' import type { SessionId } from '@deepseek-ai/dsh-session/types' import type { TranslateNS } from '@deepseek-ai/dsh-client-locale/client' -import { queueReadFaceOf } from './queue-store.ts' +import type { InboxState } from '@deepseek-ai/dsh-agent/types' +import type { ObservableSnapshot } from '@deepseek-ai/dsh-client-store' import type { ComposerKeyboard, DraftAttachmentId, DraftAttachmentSerializationResult, InputTriggerController, SessionInputResolver, SessionInput, SubmitOutcome, @@ -87,7 +88,7 @@ export class InputHub implements SessionInputResolver { actx, inputTriggers: () => this.controller(actx), popup: () => this.popup(actx), - queue: queueReadFaceOf(session), + inbox: session.projections.faceOf('inbox') as ObservableSnapshot, defaultSink: (text, attachmentIds, mode, signal) => this.sink(session, text, attachmentIds, mode, signal), steerQueue: () => { void this.steerQueue(session, shell) }, commandAttachments: { @@ -199,7 +200,8 @@ export class InputHub implements SessionInputResolver { * @param shell - the resident shell (notice outlet). */ private async steerQueue(session: SessionFace, shell: SessionInputShell): Promise { - const queued = session.getSnapshot().queue.filter(item => item.placement === 'queued') + const inbox = session.projections.faceOf('inbox').getSnapshot() as InboxState | undefined + const queued = inbox?.['next-turn'] ?? [] if (queued.length === 0) return for (const item of queued) { const result = await session.updateQueue(item.id, { kind: 'steer' }) diff --git a/packages/client/ui-conversation/src/client/input/queue-store.ts b/packages/client/ui-conversation/src/client/input/queue-store.ts deleted file mode 100644 index 09b10da0d0..0000000000 --- a/packages/client/ui-conversation/src/client/input/queue-store.ts +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Queue read face for the InputState.queue projection (frozen contract in - * ../contract/input.ts): a uSES-compatible observable over one session's - * transient inbox rows. The Session snapshot already keeps the queue array - * reference-stable across unrelated snapshot swaps, so this is a pure - * projection — no second store, no copy. - */ -import type { SessionFace } from '@deepseek-ai/dsh-api-session-controller/client' -import type { ObservableSnapshot } from '@deepseek-ai/dsh-client-store' -import type { QueuedMessage } from '../contract/input.ts' - -/** - * Project a session's transient inbox rows as a bare observable (subscribe/getSnapshot). - * The wiring layer overlays this onto InputState.queue; the runtime - * QueuedMessage and the input-contract QueuedMessage are structurally - * identical. - * @param session - the resident session face. - * @returns the queue read face (snapshot reference stable while the queue is unchanged). - */ -export function queueReadFaceOf(session: SessionFace): ObservableSnapshot { - return { - getSnapshot: () => session.getSnapshot().queue, - subscribe: fn => session.subscribe(fn), - } -} diff --git a/packages/client/ui-conversation/src/client/queue/QueueDock.tsx b/packages/client/ui-conversation/src/client/queue/QueueDock.tsx index 0dcbdda890..d6be0140c7 100644 --- a/packages/client/ui-conversation/src/client/queue/QueueDock.tsx +++ b/packages/client/ui-conversation/src/client/queue/QueueDock.tsx @@ -8,26 +8,45 @@ import { DocumentFileIcon, fileSizeText, IconEditOutline16, IconQueueOutline14, IconSendOutline14, IconTrashOutline16, projectUserText, Tooltip, } from '@deepseek-ai/dsh-client-ui-primitives' -import type { QueueAction, QueueItemId, QueueRow } from '../contract/queue.ts' +import type { InboxState } from '@deepseek-ai/dsh-agent/types' +import type { QueueAction } from '@deepseek-ai/dsh-api-session-controller/types' +import type { MessageId } from '@deepseek-ai/dsh-llm/brand' import { NS } from '../locales.ts' import css from './QueueDock.module.css' +const EMPTY_QUEUE = [] as const +const QUEUE_PREVIEW_CHARS = 200 + +function previewOf(content: InboxState['next-turn'][number]['content']): string { + const flat = content + .filter(block => block.type !== 'image' && block.type !== 'file') + .map(block => (block.type === 'text' ? block.text : `[${block.type}]`)) + .join(' ').replace(/\s+/g, ' ').trim() + const chars = Array.from(flat) + return chars.length > QUEUE_PREVIEW_CHARS ? `${chars.slice(0, QUEUE_PREVIEW_CHARS).join('')}…` : flat +} + +function textOf(content: InboxState['next-turn'][number]['content']): string | null { + if (!content.every(block => block.type === 'text')) return null + return content.map(block => block.text).join('') +} + /** Queue operations injected by the session-scoped registration. */ export interface QueueDockInjected { - updateQueue: (itemId: QueueItemId, action: QueueAction) => Promise + updateQueue: (itemId: MessageId, action: QueueAction) => Promise notify: (level: 'info' | 'error', text: string) => void /** Resolve one durable queued image into a session-scoped browser URL. */ loadImage: (attachment: ImageAttachmentRef) => Promise } /** - * Durable references carried by one queued row. Queue frames are wire data + * Durable references carried by one queued row. Inbox projections are wire data * despite their typed face, so an image block without a reference is skipped * rather than trusted. * @param content - the row's wire content blocks. * @returns the row's durable image references in block order. */ -function queueAttachments(content: QueueRow['content']): Array< +function queueAttachments(content: InboxState['next-turn'][number]['content']): Array< | { readonly type: 'image'; readonly attachment: ImageAttachmentRef } | { readonly type: 'file'; readonly attachment: FileAttachmentRef } > { @@ -87,12 +106,14 @@ export type QueueDockProps = PropsRuntime<'conversation.input.dock'> & QueueDock * collapsible count header; an empty queue renders nothing. Local submissions * show sending status and disabled actions until their Host queue rows arrive. */ -export function QueueDock({ useSession, updateQueue, notify, loadImage, t }: QueueDockProps) { - const inbox = useSession(s => s.queue) - const queue = useMemo(() => inbox.filter(row => row.placement === 'queued'), [inbox]) +export function QueueDock({ useSession, useProjection, updateQueue, notify, loadImage, t }: QueueDockProps) { + const inbox = useProjection('inbox') as unknown as InboxState | undefined + const queue = inbox?.['next-turn'] ?? EMPTY_QUEUE const pendingSubmissions = useSession(s => s.pendingSubmissions) const pendingQueue = useMemo(() => { - const admitted = new Set(queue.flatMap(row => row.rpcId === undefined ? [] : [row.rpcId])) + const admitted = new Set(queue.flatMap(({ source }) => ( + source.kind === 'user' && 'rpcId' in source ? [source.rpcId] : [] + ))) return pendingSubmissions.filter(submission => ( submission.placement === 'queued' && !admitted.has(submission.requestId) )) @@ -100,8 +121,8 @@ export function QueueDock({ useSession, updateQueue, notify, loadImage, t }: Que const rowCount = queue.length + pendingQueue.length const running = useSession(s => s.running) const queueMutable = useSession(s => s.subagent === null || s.subagent.address.mode === 'continuable') - const [editing, setEditing] = useState<{ id: QueueItemId; text: string } | null>(null) - const [busy, setBusy] = useState(null) + const [editing, setEditing] = useState<{ id: MessageId; text: string } | null>(null) + const [busy, setBusy] = useState(null) const [collapsed, setCollapsed] = useState(true) const listId = useId() @@ -117,7 +138,7 @@ export function QueueDock({ useSession, updateQueue, notify, loadImage, t }: Que const listVisible = rowCount === 1 || expanded const applyAction = async ( - itemId: QueueItemId, + itemId: MessageId, action: QueueAction, failure: string, ): Promise => { @@ -167,6 +188,7 @@ export function QueueDock({ useSession, updateQueue, notify, loadImage, t }: Que