From 8b9dbf55df58098e405264e3570432e20e932329 Mon Sep 17 00:00:00 2001 From: mesalogo Date: Thu, 3 Sep 2026 17:17:09 +0800 Subject: [PATCH] fix: allow concurrent and unrestricted Runtime sessions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Local embedded OpenCode serialized otherwise independent conversations, and GoodBuddy could stop long Runtime tasks after fixed tool, activity, model-call, or accumulated display-output limits. Different conversations can now run concurrently while requests in the same conversation stay ordered. Dynamic MCP tools remain isolated to the current request. OpenCode, Continue, managed ACP, and the Agent model gateway no longer stop native execution at GoodBuddy-defined call counts or prompt-wide output totals; display copies truncate and transport queues apply backpressure instead. Prompt-wide quota fields are removed from the current contracts. Per-call model settings, provider request timeouts, cancellation, and same-conversation ordering remain unchanged. Release note: 修复本机 OpenCode 不同会话互相等待的问题,并移除 GoodBuddy 对单次 Runtime 工具、活动和模型调用次数的人为上限;长输出现在只截断展示或施加传输背压,不再取消原生任务。 --- FEATURES.md | 3 + FEATURES.zh-CN.md | 2 +- docs/features/remote-host/technical-design.md | 25 ++- src/agent-daemon/agent-model-gateway.test.ts | 30 +++- src/agent-daemon/agent-model-gateway.ts | 51 +----- .../direct-linux-stdio-process-owner.test.ts | 50 ++++++ .../direct-linux-stdio-process-owner.ts | 35 ++-- src/agent-daemon/runtime-acp-backend.test.ts | 84 ++++++++- src/agent-daemon/runtime-acp-backend.ts | 91 ++++------ src/main/agent/acp-remote-runtime.test.ts | 54 +++--- src/main/agent/acp-remote-runtime.ts | 59 +------ src/main/agent/continue-host-adapter.test.ts | 119 +++++++------ src/main/agent/continue-host-adapter.ts | 75 ++------ src/main/agent/continue-runtime.test.ts | 16 +- src/main/agent/continue-runtime.ts | 26 ++- src/main/agent/opencode-runtime.test.ts | 110 ++++++++---- src/main/agent/opencode-runtime.ts | 162 ++++++++---------- src/main/agent/runtime-e2e.manual.test.ts | 5 + src/main/assistant/assistant-database.test.ts | 37 ++++ src/main/assistant/assistant-database.ts | 31 ++-- .../remote-agent/managed-model-bridge.test.ts | 4 +- src/main/remote-agent/managed-model-bridge.ts | 6 - .../managed-remote-acp-runtime.test.ts | 2 - .../protocol-remote-runtime-channel.test.ts | 2 - src/renderer/src/App.test.tsx | 31 ++++ src/renderer/src/App.tsx | 23 +-- src/shared/assistant-contracts.test.ts | 47 +++++ src/shared/assistant-contracts.ts | 19 +- src/shared/model-bridge-contracts.test.ts | 4 +- src/shared/model-bridge-contracts.ts | 17 -- .../model-call-operation-contracts.test.ts | 6 +- src/shared/model-call-operation-contracts.ts | 10 +- 32 files changed, 687 insertions(+), 549 deletions(-) diff --git a/FEATURES.md b/FEATURES.md index 92b0daec..bb0ef5dd 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -128,6 +128,9 @@ otherwise. Workspace/Runtime is first used, then reused by other projects in the same process. Managed SSH conversations expose only supported OpenCode choices. Multiple projects and conversations can run concurrently on one Host; + Agent-owned prompts are not stopped at a fixed model-call count or + prompt-wide output-token total, and Runtime output uses transport + backpressure instead of cancellation when GoodBuddy buffers fill; recovery separately reports network, Agent, Runtime, committed-event cursor, completion, or failure/retry and blocks only the affected project. A real Linux x64 Host has covered short and long detach, forced local harness exit, diff --git a/FEATURES.zh-CN.md b/FEATURES.zh-CN.md index 63d6d768..15fdeb75 100644 --- a/FEATURES.zh-CN.md +++ b/FEATURES.zh-CN.md @@ -29,7 +29,7 @@ - [x] **直连模型 Runtime**:支持问答、知识总结、受控工具执行和图像生成。 - [x] **直连模型编程 Agent**:本机直连文本模型可在 Execute 模式运行平台 Shell,并可按父请求模式、模型、工作区和能力范围委派一层编程 Subagent;OpenCode、Continue、DeepSeek Harness 和托管 SSH 不重复注入这两个工具。Windows 本机命令与真实模型“修改、测试、修复、复核”闭环已通过,macOS 与 Linux 真机命令仍待对应平台验收。 - [x] **OpenCode 与 Continue**:使用隔离子进程、环境变量白名单、统一配置、取消、总执行时限、有界流式输出和活动记录;共享进程回收逻辑保留 Windows 完整进程树终止,并对采用独立进程组的 POSIX 子进程执行组回收。聊天状态检查使用一次性探测 Runtime 并在返回后立即回收,不进入按模型与项目隔离的执行 Runtime 缓存;执行 Runtime 仍按项目复用。本机 GoodBuddy 管理的 OpenCode 只对同一会话内的请求保持顺序,不同会话即使属于同一项目也可并行,不同项目同样可并行;请求级动态 MCP 通过默认通配禁用和当前请求精确放行保持隔离。OpenCode 与 Continue 不再按固定工具调用数或活动数中止单次运行,本机展示副本达到容量时只截断后续展示,不取消原生任务。交互提问只由前台对话回答,定时任务、远程通道和委派等后台执行遇到提问时会立即失败并提示改为前台运行,避免无限等待。本机 OpenCode 启动时直接使用已选择的绝对路径,不预先检查文件或运行 `--version`,路径失效时由实际启动返回错误;托管 Linux ARM Host 激活 Runtime 时复用准备阶段已验证的 registry 和 manifest,不重新哈希或检查完整 OpenCode 二进制。 -- [x] **托管 SSH OpenCode 闭环(技术预览)**:该能力由“设置 > 平台功能”中的独立“远程项目(技术预览)”页签控制并默认关闭;关闭时不影响任何本地项目、普通桌面功能或桌面发布。启用后可管理固定 Host Key 的 SSH Host、浏览有界远端目录并创建 Ask/Execute 项目;两种模式都直接启动签名 Runtime,不依赖 bubblewrap。Ask 在 Agent 工具分发边界只允许读取工具,Execute 使用所选 SSH 账号的完整权限。Agent 通过私有 Unix socket 和 ACP v4 持有 accepted Prompt、Provider/工具轮次、Runtime 进程、稳定模型 ledger 与有界语义 transcript;Desktop 退出、网络中断或本机进程结束后 Host 继续执行,重启后只 attach 原 controller/binding/operation 并把 provenance 原子归并回原对话,不重发 Prompt。模型 profile 与 API Key 只在当前 accepted operation 生命周期内进入 Agent 内存,不写入 Renderer、SSH 参数、远端环境或磁盘。远端组件不嵌入桌面包,项目切换只更新本地选择且不会连接或下载;设置页只读取小型签名目录并按 Linux x64/arm64 显示本地版本、在线最新兼容版本和“有更新/已是最新”,用户点击后才下载签名 `.gbagent`,也可导入/导出离线包。每个复合包包含 Agent、固定 Node 与由桌面源码统一维护适配的 OpenCode Runtime;在线来源跟随“关于与更新”的 GitHub/北京 OSS 选择,签名累计目录绑定最低桌面版本、Agent 协议、架构、大小、SHA-256 和固定 URL。公钥 registry 接受等价 JSON 空白和换行,但仍严格校验 schema、Ed25519 key、环境与撤销;目录、包、manifest、payload 签名和流式 SHA-256 不变。缺少某架构包只使该架构托管 SSH 不可用。项目只保存 Host、远端路径、Runtime 选择和模式;首次实际使用 Workspace/Runtime 时读取 Host current identity,同一进程内其他项目复用已确认 identity 和 Agent 连接。托管 SSH 会话只显示支持的 OpenCode 选择。同一 Host 可并行运行多个项目和对话;恢复进度分别显示网络、Agent、Runtime、已提交事件 cursor、完成或失败/重试,只阻塞受影响项目。Linux x64 真实 Host 已覆盖短/长 detach、本机 harness 强制结束、并发、SSH relay 丢失、取消、确定/不确定 Provider 失败、Agent `SIGKILL`/重启和 reopened Desktop SQLite 恢复;成功工具 START/END 各一次,未发现 Prompt、Provider 或工具重放。当前 Agent 源码 lock 为 `0.11.16`,Desktop 发布候选为 `0.12.2`;正式发布状态以 Agent 与 Desktop 的独立发布渠道为准。 +- [x] **托管 SSH OpenCode 闭环(技术预览)**:该能力由“设置 > 平台功能”中的独立“远程项目(技术预览)”页签控制并默认关闭;关闭时不影响任何本地项目、普通桌面功能或桌面发布。启用后可管理固定 Host Key 的 SSH Host、浏览有界远端目录并创建 Ask/Execute 项目;两种模式都直接启动签名 Runtime,不依赖 bubblewrap。Ask 在 Agent 工具分发边界只允许读取工具,Execute 使用所选 SSH 账号的完整权限。Agent 通过私有 Unix socket 和 ACP v4 持有 accepted Prompt、Provider/工具轮次、Runtime 进程、稳定模型 ledger 与有界语义 transcript;Desktop 退出、网络中断或本机进程结束后 Host 继续执行,重启后只 attach 原 controller/binding/operation 并把 provenance 原子归并回原对话,不重发 Prompt。模型 profile 与 API Key 只在当前 accepted operation 生命周期内进入 Agent 内存,不写入 Renderer、SSH 参数、远端环境或磁盘。远端组件不嵌入桌面包,项目切换只更新本地选择且不会连接或下载;设置页只读取小型签名目录并按 Linux x64/arm64 显示本地版本、在线最新兼容版本和“有更新/已是最新”,用户点击后才下载签名 `.gbagent`,也可导入/导出离线包。每个复合包包含 Agent、固定 Node 与由桌面源码统一维护适配的 OpenCode Runtime;在线来源跟随“关于与更新”的 GitHub/北京 OSS 选择,签名累计目录绑定最低桌面版本、Agent 协议、架构、大小、SHA-256 和固定 URL。公钥 registry 接受等价 JSON 空白和换行,但仍严格校验 schema、Ed25519 key、环境与撤销;目录、包、manifest、payload 签名和流式 SHA-256 不变。缺少某架构包只使该架构托管 SSH 不可用。项目只保存 Host、远端路径、Runtime 选择和模式;首次实际使用 Workspace/Runtime 时读取 Host current identity,同一进程内其他项目复用已确认 identity 和 Agent 连接。托管 SSH 会话只显示支持的 OpenCode 选择。同一 Host 可并行运行多个项目和对话;Agent-owned Prompt 不因固定模型调用次数或 Prompt 累计输出 Token 数停止,GoodBuddy 输出缓冲区达到容量时使用传输背压而不取消 Runtime;恢复进度分别显示网络、Agent、Runtime、已提交事件 cursor、完成或失败/重试,只阻塞受影响项目。Linux x64 真实 Host 已覆盖短/长 detach、本机 harness 强制结束、并发、SSH relay 丢失、取消、确定/不确定 Provider 失败、Agent `SIGKILL`/重启和 reopened Desktop SQLite 恢复;成功工具 START/END 各一次,未发现 Prompt、Provider 或工具重放。当前 Agent 源码 lock 为 `0.11.16`,Desktop 发布候选为 `0.12.2`;正式发布状态以 Agent 与 Desktop 的独立发布渠道为准。 - [x] **SSH Host 手动环境准备源码链路**:Host Key、认证和系统探针成功后先保存 Host,并只读探测共享 Agent/Runtime;保存 Host、打开项目都不自动安装。Host 卡片只有一个按版本事实显示“安装远程环境”“更新远程环境”或“重新安装”的主按钮,次级 SegmentedControl 选择默认且不持久化的“自动”、Host 下载或 GoodBuddy 传输;“版本匹配”badge 不等同环境健康。自动模式只在 operation/prepare 前探测并择一,显式选择保持有效,任何 prepare、commit 或 adoption 失败都不跨 acquisition 自动 fallback。两种方式把同一签名 compound `.gbagent` 交付到固定 staging 后,共用 control-plane prepare、commit、Agent activate/health、Runtime activate、finalize 与显式 cleanup。GoodBuddy 路径可在同一次操作下载并验证缺失候选、缓存并取得 lease,再有界流式 SFTP 上传一个归档和其中已验证的 bootstrap Node,不把约 294 MiB 整包读入 Main `Buffer`;Host 在解包时完成一次完整 payload 校验。未完成操作只记录暂存 cleanup 所需的 operation ID;下次更新尽力清理旧暂存后重新 prepare,不保存远端 metadata 副本,也不让 cleanup 失败阻塞新更新或回滚健康环境。已有项目在实际使用 Workspace/Runtime 时按需解析 Host current identity 并执行固定 `attach-or-bootstrap`,注册后的 health、capabilities 和 prompt 启动不扫描完整 payload。详见 [设计说明](./docs/features/remote-host/environment-provisioning-technical-design.md)。 - [ ] **SSH Host 环境准备真实 Host 验收**:源码已可使用当前 format v1 包;仍需完成 GitHub、北京镜像、Linux x64/arm64、取消和离线 GoodBuddy 传输的真实 Host 验收。当前状态不表示已发布或已完成真实 Host 测试。 - [x] **DeepSeek Harness(预览)**:使用 GoodBuddy 固定 Host 和 OpenAI 兼容模型连接;优先使用管理员提供的连接,否则跟随兼容的默认模型或首个兼容连接,无需单独重复选择。Ask 只允许调用 Host 中真实注册的 `read`、`skill` 以及 Main 管理的 Web Search/Fetch 代理,拒绝插件同名冒充,Execute 放行全部已启用内置及插件工具,并以当前用户权限运行。图像输入跟随所选模型连接的能力声明,文本模型在 Host 或模型调用前拒绝图片,图片模型通过有界内联内容和临时 Attachment Store 接收 JPEG/PNG。 diff --git a/docs/features/remote-host/technical-design.md b/docs/features/remote-host/technical-design.md index 673ed738..3f35fa11 100644 --- a/docs/features/remote-host/technical-design.md +++ b/docs/features/remote-host/technical-design.md @@ -202,10 +202,11 @@ Execute 直接启动已签名 Runtime: - 继承 SSH 账号的正常环境、文件系统、进程和网络能力; - 不进行 T2/T3、confinement attestation、approval bridge 或逐工具批准。 -两种模式都保留输入/输出字节上限、用户取消和进程组清理。生产 Prompt 不设置固定墙钟 -总时限;只要 Runtime 尚未按自身协议结束,Main 和 Agent 就允许其持续运行。启动、握手、 -单个控制 RPC、重连尝试和关闭清理仍使用独立的有界超时,测试可以显式注入短 Prompt -时限验证取消升级,但该覆盖值不是生产默认。无界 Prompt 继续复用已发布的必填 +两种模式都保留输入字节上限、用户取消和进程组清理;输出只用有界内存队列与 journal +配合背压,不按 Prompt 累计输出量停止 Runtime。生产 Prompt 不设置固定墙钟总时限; +只要 Runtime 尚未按自身协议结束,Main 和 Agent 就允许其持续运行。启动、握手、单个 +控制 RPC、重连尝试和关闭清理仍使用独立的有界超时,测试可以显式注入短 Prompt 时限 +验证取消升级,但该覆盖值不是生产默认。无界 Prompt 继续复用已发布的必填 `deadlineAt` 字段,以 year-9999 哨兵表达;Main 与 Agent 必须同时理解该哨兵,Agent `0.11.10` 及更早版本仍会按其 manifest 上限夹紧,因此桌面启用这一语义前必须配套更新 到 Agent `0.11.13` 或更高版本。 @@ -234,8 +235,9 @@ Execute 直接启动已签名 Runtime: - 无法确认外部 Provider 是否已处理的模型调用保持结果未知,避免重复计费或重复副作用。 - 只有远端返回 identity 匹配且 `closed: true` 时,Main 才删除持久化 binding;传输失败或未确认 close 会保留 recovery identity。 - 终态 close 由 Agent 在一个事务中为两个方向记录 sequence high-water tombstone,删除剩余 frame 和 active channel,并归零对应 journal quota。tombstone 用于拒绝迟到的旧 epoch frame,因此当前不会按时间自动裁剪。 -- 断线不主动终止正在运行的 Runtime。用户取消、显式关闭、输出边界或 Agent shutdown - 才触发停止;连接恢复只受当前用户取消信号和单次重连控制超时约束,不把 Prompt 已运行 +- 断线不主动终止正在运行的 Runtime。用户取消、显式关闭或 Agent shutdown 才触发停止; + 输出队列和 journal 达到容量时暂停读取并等待 ACK 释放容量,不把展示或传输容量转换为 + Runtime 取消。连接恢复只受当前用户取消信号和单次重连控制超时约束,不把 Prompt 已运行 时长当作停止条件。 - Agent 失联期间继续把 Runtime 输出写入本机 journal;重新连接只校验 controller identity、恢复 binding,并从 Main 最后确认的 cursor 后同步缺失数据。已接受的 @@ -285,9 +287,10 @@ Execute 直接启动已签名 Runtime: 各限制 768 KiB,响应同时核对声明长度与短读。每个 `(binding, operation, roundIndex)` 只 dispatch 一次;已 dispatch 但未证明完整交付的调用 变为 `outcome-unknown`,不会自动重试。 -- Prompt 最多 100 个模型调用,并受单次与累计输出 Token 上限保护;这些是资源边界,不是 - Prompt 墙钟总时限。Provider 单次请求仍有独立超时。实际 usage 在语义 transcript 中同步 - 回 Desktop 的 usage/task 数据。Agent 在所有进程终止路径统一清除 Prompt token 计数。 +- Agent 不按固定模型调用次数或 Prompt 累计输出 Token 数停止 Runtime;每次 Provider + 请求仍遵循所选模型 profile 的单次输出设置和独立请求超时。模型桥协议不包含 Prompt + 调用次数或累计 Token 配额字段。实际 usage 在语义 transcript 中同步回 Desktop 的 + usage/task 数据。 - helper 可以接收同一 Prompt 内并发到达的模型桥请求;它在单一稳定模型桥上按到达 顺序等待并交付,不返回本地 `bridge-busy`,每个响应只有在 HTTP 完整 flush 后才 发送 delivery ACK。 @@ -400,6 +403,10 @@ model bridge,以及生命周期和恢复逻辑。单元测试、mock、fixture 都未经过外部进程隔离命令。Runtime owner 和隔离目录均已清理,未修改 Host current Agent/Runtime。本次只验证能力声明和真实进程启动,没有发送 Prompt,真实文本模型调用 0 次。 +- 2026-09-03 使用包含本次模型轮次与输出背压修复的当前源码 Agent,在共享 Linux x64 + Host 的唯一 `/tmp/goodbuddy-e2e-*` 目录完成 Runtime 激活和 Agent 本机模型 gateway + 验证。模型 gateway 通过所选 Anthropic 连接返回预期随机哨兵,HTTP 状态为 200,实际 + 文本模型调用恰好 1 次;Host 临时目录随后删除,共享 Agent/Runtime 安装和进程未修改。 - 正常 Host 更新路径把 Linux x64 Host 的 Agent 更新为 `0.11.2-e2e.12`,并确认 OpenCode Runtime 已安装版本与所需版本均为 `1.18.9`。 - 一条新的 Ask 用户操作只提交一次。OpenCode 先在 build 模型轮次请求一个原生 diff --git a/src/agent-daemon/agent-model-gateway.test.ts b/src/agent-daemon/agent-model-gateway.test.ts index 702980a3..be797f28 100644 --- a/src/agent-daemon/agent-model-gateway.test.ts +++ b/src/agent-daemon/agent-model-gateway.test.ts @@ -36,8 +36,6 @@ const profile: AgentPromptModelProfile = { capabilities: { imageInput: false }, limits: { maximumOutputTokens: 4_096, - maximumModelCalls: 8, - maximumTotalOutputTokens: 32_768, requestTimeoutMilliseconds: 5_000 } } @@ -124,6 +122,34 @@ describe('AgentModelGateway', () => { ledger.close() }) + it('does not impose prompt-wide model-call or output-token quotas', async () => { + const fetcher = vi.fn(async () => + new Response( + JSON.stringify({ + id: 'response', + usage: { output_tokens: 4_096 } + }), + { + status: 200, + headers: { 'content-type': 'application/json' } + } + ) + ) + const { gateway, ledger } = setup(fetcher) + + for (let roundIndex = 0; roundIndex < 101; roundIndex += 1) { + const result = await gateway.dispatch( + { ...context, roundIndex }, + request, + new AbortController().signal + ) + await result.acknowledgeDelivery() + } + + expect(fetcher).toHaveBeenCalledTimes(101) + ledger.close() + }) + it('caps OpenCode chat-completions max_tokens before dispatch', async () => { const fetcher = vi.fn(async (_url, init) => { expect(JSON.parse(String(init?.body))).toMatchObject({ diff --git a/src/agent-daemon/agent-model-gateway.ts b/src/agent-daemon/agent-model-gateway.ts index fb4f84ff..ee71bb47 100644 --- a/src/agent-daemon/agent-model-gateway.ts +++ b/src/agent-daemon/agent-model-gateway.ts @@ -313,7 +313,6 @@ export type AgentModelGatewayContext = { export class AgentModelGateway { readonly #ledger: AgentModelCallLedger readonly #fetch: typeof fetch - readonly #promptOutputTokens = new Map() constructor(options: { ledger: AgentModelCallLedger @@ -334,24 +333,10 @@ export class AgentModelGateway { }> { signal.throwIfAborted() const request = remoteModelGatewayRequestSchema.parse(requestInput) - const promptKey = `${context.bindingId}\0${context.operationId}` - const usedOutputTokens = this.#promptOutputTokens.get(promptKey) ?? 0 - const remainingOutputTokens = - context.profile.limits.maximumTotalOutputTokens - usedOutputTokens - if (remainingOutputTokens < 1) { - throw new AgentModelGatewayError( - 'policy', - 'Prompt total output token limit is exhausted' - ) - } - const reservedOutputTokens = Math.min( - context.profile.limits.maximumOutputTokens, - remainingOutputTokens - ) const prepared = prepareProviderRequest( context.profile, request, - reservedOutputTokens + context.profile.limits.maximumOutputTokens ) const requestDigest = sha256(prepared.canonicalRequest) const callId = createAgentModelCallId(context) @@ -444,14 +429,6 @@ export class AgentModelGateway { headers, bodyBase64: Buffer.from(bytes).toString('base64') }) - this.#promptOutputTokens.set( - promptKey, - usedOutputTokens + - Math.min( - reservedOutputTokens, - responseOutputTokens(bytes) ?? reservedOutputTokens - ) - ) this.#ledger.complete(callId) let finalized = false return { @@ -474,9 +451,6 @@ export class AgentModelGateway { } } - finalizePrompt(bindingId: string, operationId: string): void { - this.#promptOutputTokens.delete(`${bindingId}\0${operationId}`) - } } export function createAgentModelCallId( @@ -598,29 +572,6 @@ function prepareProviderRequest( } } -function responseOutputTokens(body: Uint8Array): number | undefined { - let value: unknown - try { - value = JSON.parse(new TextDecoder().decode(body)) - } catch { - return undefined - } - if (value === null || typeof value !== 'object' || Array.isArray(value)) { - return undefined - } - const usage = (value as { usage?: unknown }).usage - if (usage === null || typeof usage !== 'object' || Array.isArray(usage)) { - return undefined - } - const record = usage as Record - const tokens = record.output_tokens ?? record.completion_tokens - return typeof tokens === 'number' && - Number.isSafeInteger(tokens) && - tokens >= 0 - ? tokens - : undefined -} - function providerUrl(profile: AgentPromptModelProfile): URL { const url = new URL(profile.baseUrl) const path = url.pathname.replace(/\/+$/u, '') diff --git a/src/agent-daemon/direct-linux-stdio-process-owner.test.ts b/src/agent-daemon/direct-linux-stdio-process-owner.test.ts index 108a17ce..6ba44af6 100644 --- a/src/agent-daemon/direct-linux-stdio-process-owner.test.ts +++ b/src/agent-daemon/direct-linux-stdio-process-owner.test.ts @@ -254,6 +254,56 @@ describe('direct Linux stdio Runtime ownership', () => { } }) + it('uses stdout backpressure instead of stopping when output queues fill', async () => { + const registry = createRegistry() + const child = fakeChild() + const launched = identity() + const sendSignal = vi.fn(() => true) + let releaseFirst!: () => void + const firstConsumed = new Promise((resolve) => { + releaseFirst = resolve + }) + const owner = await launchDirectLinuxStdioProcessOwner({ + manifest: manifest(), + profile: profile(), + identity: { launchId: 'launch-output', processId: 'process-output' }, + installationId: 'installation-1', + registry, + deadlineAt: UNBOUNDED_REMOTE_PROMPT_DEADLINE, + maximumInputBytes: 1024, + maximumOutputQueueChunks: 1, + platform: 'linux', + spawn: () => { + queueMicrotask(() => child.emit('spawn')) + return child + }, + randomOwnerToken: () => 'a'.repeat(32), + readProcessIdentity: async () => launched, + sendSignal + }) + const pause = vi.spyOn(child.stdout, 'pause') + const resume = vi.spyOn(child.stdout, 'resume') + const received: string[] = [] + owner.subscribeOutput(async ({ data }) => { + received.push(Buffer.from(data).toString()) + if (received.length === 1) { + await firstConsumed + } + }) + + child.stdout.write('one') + await vi.waitFor(() => expect(pause).toHaveBeenCalled()) + child.stdout.write('two') + releaseFirst() + await vi.waitFor(() => expect(received).toEqual(['one', 'two'])) + + expect(resume).toHaveBeenCalled() + expect(sendSignal).not.toHaveBeenCalled() + child.exitCode = 0 + child.emit('close', 0, null) + registry.close() + }) + it('startup reconciles only owners from the current installation', async () => { const registry = createRegistry() const reservation = { diff --git a/src/agent-daemon/direct-linux-stdio-process-owner.ts b/src/agent-daemon/direct-linux-stdio-process-owner.ts index 90ff0e8e..f98177f2 100644 --- a/src/agent-daemon/direct-linux-stdio-process-owner.ts +++ b/src/agent-daemon/direct-linux-stdio-process-owner.ts @@ -170,13 +170,13 @@ export class DirectLinuxStdioProcessOwner { #maximumPendingStdinBytes: number #inputBytes = 0 #pendingInputBytes = 0 - #outputBytes = 0 #queuedOutputBytes = 0 #writeTail: Promise = Promise.resolve() #deadlineTimer?: NodeJS.Timeout #promptGeneration = 1 #promptActive = true #draining = false + #outputPaused = false #closed = false #stdinClosed = false #stopPromise?: Promise @@ -280,7 +280,6 @@ export class DirectLinuxStdioProcessOwner { this.#maximumPendingStdinBytes = this.#maximumInputBytes this.#inputBytes = 0 this.#pendingInputBytes = 0 - this.#outputBytes = 0 this.#promptActive = true this.#promptGeneration += 1 this.#scheduleDeadline( @@ -449,24 +448,17 @@ export class DirectLinuxStdioProcessOwner { #acceptOutput(chunk: Buffer | Uint8Array): void { if (this.#closed || chunk.byteLength === 0) return - if ( - this.#outputBytes + chunk.byteLength > this.#maximumOutputBytes || - this.#outputQueue.length >= this.#maximumOutputQueueChunks || - this.#queuedOutputBytes + chunk.byteLength > this.#maximumOutputBytes - ) { - this.#child.stdout.pause() - this.#outputQueue.splice(0) - this.#queuedOutputBytes = 0 - void this.stop({ - reason: 'output-quota', - deadlineAt: new Date(this.#now() + DEFAULT_STOP_TIMEOUT_MS).toISOString() - }).catch(() => undefined) - return - } const copy = Uint8Array.from(chunk) - this.#outputBytes += copy.byteLength this.#queuedOutputBytes += copy.byteLength this.#outputQueue.push(copy) + if ( + this.#outputQueue.length >= this.#maximumOutputQueueChunks || + (this.#maximumOutputBytes > 0 && + this.#queuedOutputBytes >= this.#maximumOutputBytes) + ) { + this.#child.stdout.pause() + this.#outputPaused = true + } this.#scheduleDrain() } @@ -484,6 +476,15 @@ export class DirectLinuxStdioProcessOwner { for (const listener of [...this.#outputListeners]) { await listener({ stream: 'stdout', data: data.slice() }) } + if ( + this.#outputPaused && + this.#outputQueue.length < this.#maximumOutputQueueChunks && + (this.#maximumOutputBytes === 0 || + this.#queuedOutputBytes < this.#maximumOutputBytes) + ) { + this.#outputPaused = false + this.#child.stdout.resume() + } } } catch { void this.stop({ diff --git a/src/agent-daemon/runtime-acp-backend.test.ts b/src/agent-daemon/runtime-acp-backend.test.ts index 3dd1ab5b..ddb4193b 100644 --- a/src/agent-daemon/runtime-acp-backend.test.ts +++ b/src/agent-daemon/runtime-acp-backend.test.ts @@ -7,6 +7,7 @@ import { import type { RemoteRuntimeBundleManifest } from '../shared/remote-runtime-launch-contracts' import type { ControllerLease } from './controller-registry' import { AgentModelGatewayError } from './agent-model-gateway' +import { EventJournalCapacityError } from './event-journal' import type { ProtocolMethodContext } from './protocol-server' import type { ModelBridgeBrokerDispatch, @@ -43,8 +44,16 @@ class MemoryAcpJournal implements RuntimeAcpJournal { }> = [] mainAckSequence = '0' failAppend = false + capacityBlocked = false appendAcpFrame(input: (typeof this.frames)[number]): { created: boolean } { + if (this.capacityBlocked) { + throw new EventJournalCapacityError( + 'journal capacity', + 'stream', + false + ) + } if (this.failAppend) { throw new Error('journal capacity') } @@ -415,8 +424,6 @@ function harness(input: { capabilities: { imageInput: false }, limits: { maximumOutputTokens: 4_096, - maximumModelCalls: 100, - maximumTotalOutputTokens: 409_600, requestTimeoutMilliseconds: 60_000 } }, @@ -541,6 +548,52 @@ describe('RuntimeAcpBackend', () => { expect(fixture.blobSink).not.toHaveBeenCalled() }) + it('allows more than 100 Agent-local model calls in one prompt', async () => { + const dispatch = vi.fn(async () => ({ + response: { + status: 200, + headers: {}, + bodyBase64: Buffer.from('{}').toString('base64') + }, + acknowledgeDelivery: vi.fn(async () => undefined), + failDelivery: vi.fn() + })) + const fixture = harness({ + agentOwned: true, + modelGateway: { + dispatch, + finalizePrompt: vi.fn() + } + }) + await open(fixture) + await invoke( + fixture, + 'runtime/preparePrompt', + fixture.preparation() + ) + + for (let index = 0; index < 101; index += 1) { + const exchange = await fixture.bridgeDispatch!( + { + method: 'POST', + path: '/v1/responses', + headers: { 'content-type': 'application/json' }, + bodyBase64: Buffer.from('{}').toString('base64') + }, + { + requestId: `provider-request-${index}`, + signal: new AbortController().signal + } + ) + if (!('acknowledgeDelivery' in exchange)) { + throw new Error('Expected an acknowledged model exchange') + } + await exchange.acknowledgeDelivery() + } + + expect(dispatch).toHaveBeenCalledTimes(101) + }) + it('runs open, prepare, ACP delivery, durable output, cursors, and close', async () => { const fixture = harness() const opened = await invoke( @@ -1162,7 +1215,7 @@ describe('RuntimeAcpBackend', () => { } }) - it('stops the process when its output quota is exceeded', async () => { + it('does not stop the process when cumulative output exceeds the display budget', async () => { const fixture = harness({ maximumOutputBytes: 3 }) await open(fixture) await invoke( @@ -1172,9 +1225,30 @@ describe('RuntimeAcpBackend', () => { ) await expect( fixture.process.emit('stdout', 'four') - ).rejects.toMatchObject({ code: 'output-quota' }) - expect(fixture.process.stops).toEqual(['output-quota']) + ).resolves.toBeUndefined() + expect(fixture.process.stops).toEqual([]) + expect(fixture.outputFrames).toEqual(['four']) + }) + + it('waits for durable journal capacity instead of stopping Runtime output', async () => { + const fixture = harness() + await open(fixture) + await invoke( + fixture, + 'runtime/preparePrompt', + fixture.preparation() + ) + fixture.journal.capacityBlocked = true + const output = fixture.process.emit('stdout', 'later') + await new Promise((resolve) => setTimeout(resolve, 10)) + expect(fixture.process.stops).toEqual([]) expect(fixture.outputFrames).toEqual([]) + + fixture.journal.capacityBlocked = false + await output + + expect(fixture.process.stops).toEqual([]) + expect(fixture.outputFrames).toEqual(['later']) }) it('stops with output quota when the detached durable journal is exhausted', async () => { diff --git a/src/agent-daemon/runtime-acp-backend.ts b/src/agent-daemon/runtime-acp-backend.ts index b85866ab..2560da0c 100644 --- a/src/agent-daemon/runtime-acp-backend.ts +++ b/src/agent-daemon/runtime-acp-backend.ts @@ -64,6 +64,7 @@ import { type AgentModelGateway } from './agent-model-gateway' import { openCodeModelBridgeModelId } from './model-bridge-helper' +import { EventJournalCapacityError } from './event-journal' const ZERO_CURSOR = '0' const DEFAULT_MAXIMUM_BINDINGS = 128 @@ -82,6 +83,7 @@ const DEFAULT_MAXIMUM_OPERATIONS_PER_BINDING = 1_000 const DEFAULT_DISPOSE_TIMEOUT_MS = 10_000 const OWNED_PROMPT_CANCEL_GRACE_MS = 1_000 const OWNED_PROMPT_START_TIMEOUT_MS = 2 * 60_000 +const ACP_JOURNAL_RETRY_MILLISECONDS = 25 export type RuntimeAcpBackendErrorCode = | 'capacity' @@ -317,8 +319,6 @@ type BindingState = { activeOperationId?: string terminalState?: 'completed' | 'failed' | 'cancelled' | 'interrupted' inputBytes: number - outputBytes: number - maximumOutputBytes?: number nextOutputSequence: bigint pendingInput: number pendingOutput: number @@ -721,8 +721,6 @@ export class RuntimeAcpBackend { existing.stopRequested = false existing.terminalState = undefined existing.inputBytes = 0 - existing.outputBytes = 0 - existing.maximumOutputBytes = undefined existing.nextOutputSequence = 1n existing.pendingInput = 0 existing.pendingOutput = 0 @@ -774,7 +772,6 @@ export class RuntimeAcpBackend { stopRequested: false, operations: new Map(), inputBytes: 0, - outputBytes: 0, nextOutputSequence: 1n, pendingInput: 0, pendingOutput: 0, @@ -1234,11 +1231,6 @@ export class RuntimeAcpBackend { } } binding.inputBytes = 0 - binding.outputBytes = 0 - binding.maximumOutputBytes = Math.min( - preparation.budget.maximumOutputBytes, - verified.manifest.limits.maximumPromptOutputBytes - ) if (!binding.operations.has(preparation.operationId)) { binding.operations.set(preparation.operationId, { preparationDigest, @@ -1308,10 +1300,6 @@ export class RuntimeAcpBackend { ) } await this.#closeModelBridge(binding, true) - this.#options.modelGateway?.finalizePrompt( - binding.request.bindingId, - operation.operationId - ) if (binding.poisoned) { throw new RuntimeAcpBackendError( 'Model response delivery was not proven before prompt completion', @@ -1336,8 +1324,6 @@ export class RuntimeAcpBackend { prepared.completion = completion binding.activeOperationId = undefined binding.inputBytes = 0 - binding.outputBytes = 0 - binding.maximumOutputBytes = undefined if (binding.deadlineTimer !== undefined) { clearTimeout(binding.deadlineTimer) binding.deadlineTimer = undefined @@ -1527,10 +1513,6 @@ export class RuntimeAcpBackend { return } await this.#closeModelBridge(binding, true) - this.#options.modelGateway?.finalizePrompt( - binding.request.bindingId, - operationId - ) try { await binding.process.completePrompt() } catch { @@ -1568,8 +1550,6 @@ export class RuntimeAcpBackend { prepared.modelProfile = undefined binding.activeOperationId = undefined binding.inputBytes = 0 - binding.outputBytes = 0 - binding.maximumOutputBytes = undefined if (binding.deadlineTimer !== undefined) { clearTimeout(binding.deadlineTimer) binding.deadlineTimer = undefined @@ -1673,12 +1653,6 @@ export class RuntimeAcpBackend { 'closed' ) } - if (binding.nextModelRound >= profile.limits.maximumModelCalls) { - throw new RuntimeAcpBackendError( - 'Prompt model call limit reached', - 'capacity' - ) - } const roundIndex = binding.nextModelRound let exchanged try { @@ -1990,21 +1964,6 @@ export class RuntimeAcpBackend { ) } this.#assertBeforeDeadline(binding) - const budget = this.#activeBudget(binding) - const maximumOutputBytes = Math.min( - budget.maximumOutputBytes, - binding.maximumOutputBytes ?? budget.maximumOutputBytes - ) - if (binding.outputBytes + output.data.byteLength > maximumOutputBytes) { - await this.#stopAndReconcile(binding, 'output-quota').catch( - () => undefined - ) - throw new RuntimeAcpBackendError( - 'Runtime output quota reached', - 'output-quota' - ) - } - binding.outputBytes += output.data.byteLength for ( let offset = 0; offset < output.data.byteLength; @@ -2017,14 +1976,31 @@ export class RuntimeAcpBackend { try { await this.#enqueueOutput(binding, payload, async () => { const sequence = binding.nextOutputSequence.toString() - this.#options.journal.appendAcpFrame({ - controllerId: binding.controllerId, - bindingId: binding.request.bindingId, - channelEpoch: binding.openResult.channelEpoch, - direction: 'runtime-to-main', - sequence, - payload - }) + while (true) { + try { + this.#options.journal.appendAcpFrame({ + controllerId: binding.controllerId, + bindingId: binding.request.bindingId, + channelEpoch: binding.openResult.channelEpoch, + direction: 'runtime-to-main', + sequence, + payload + }) + break + } catch (error) { + if (!(error instanceof EventJournalCapacityError)) { + throw error + } + if (binding.state !== 'running') { + throw new RuntimeAcpBackendError( + 'Runtime output arrived after the binding stopped', + 'closed' + ) + } + this.#assertBeforeDeadline(binding) + await delay(ACP_JOURNAL_RETRY_MILLISECONDS) + } + } binding.nextOutputSequence += 1n if (binding.transportState === 'live') { try { @@ -2287,12 +2263,6 @@ export class RuntimeAcpBackend { } #finishProcess(binding: BindingState): void { - if (binding.activeOperationId !== undefined) { - this.#options.modelGateway?.finalizePrompt( - binding.request.bindingId, - binding.activeOperationId - ) - } if (binding.process !== undefined) { this.#processIdentityBindings.delete( processIdentityKey(binding.process.identity) @@ -2750,6 +2720,13 @@ function processIdentityKey(identity: RuntimeAcpProcessIdentity): string { ].join('\0') } +async function delay(milliseconds: number): Promise { + await new Promise((resolve) => { + const timer = setTimeout(resolve, milliseconds) + timer.unref?.() + }) +} + function digestSecretBearingPreparation( preparation: RemotePromptOperationPreparation ): string { diff --git a/src/main/agent/acp-remote-runtime.test.ts b/src/main/agent/acp-remote-runtime.test.ts index f380571d..e6763e82 100644 --- a/src/main/agent/acp-remote-runtime.test.ts +++ b/src/main/agent/acp-remote-runtime.test.ts @@ -1544,7 +1544,7 @@ describe('AcpRemoteRuntime', () => { }) }) - it('bounds native subagent output per event and per request', async () => { + it('bounds native subagent output per event without a request-wide stop', async () => { const client: { value?: AgentSideConnection } = {} const server = fakeServer({ prompt: async ({ sessionId }) => { @@ -1620,10 +1620,12 @@ describe('AcpRemoteRuntime', () => { async () => 'once' ) ) - ).rejects.toThrow('远端 Runtime 请求输出超过安全上限') + ).resolves.toContainEqual( + expect.objectContaining({ type: 'done' }) + ) }) - it('rejects more than 100 distinct remote tool calls', async () => { + it('accepts more than 100 distinct remote tool calls', async () => { const client: { value?: AgentSideConnection } = {} const server = fakeServer({ prompt: async ({ sessionId }) => { @@ -1634,7 +1636,7 @@ describe('AcpRemoteRuntime', () => { sessionUpdate: 'tool_call', toolCallId: `call-${index}`, title: 'tool', - status: 'in_progress' + status: 'completed' } }) } @@ -1643,17 +1645,18 @@ describe('AcpRemoteRuntime', () => { }) client.value = server.client - await expect( - collect( - runtime(server).run( - request, - new AbortController().signal, - async () => 'once' - ) + const events = await collect( + runtime(server).run( + request, + new AbortController().signal, + async () => 'once' ) - ).rejects.toThrow( - '远端 Runtime 单次运行的工具调用超过 100 个' ) + + expect( + events.filter((event) => event.type === 'tool') + ).toHaveLength(101) + expect(events.at(-1)).toMatchObject({ type: 'done' }) }) it('does not impose a default wall-clock deadline on remote prompts', async () => { @@ -2235,7 +2238,7 @@ describe('AcpRemoteRuntime', () => { ]) }) - it('cancels and truncates a flood when the consumer is slow', async () => { + it('backpressures a flood without cancelling the Runtime', async () => { const store = new MemoryRuntimeSessionBindingStore() const client: { value?: AgentSideConnection } = {} const cancel = vi.fn(async () => {}) @@ -2276,15 +2279,26 @@ describe('AcpRemoteRuntime', () => { done: false }) await new Promise((resolve) => setTimeout(resolve, 10)) - await expect(stream.next()).rejects.toThrow('输出已截断') + const events = [] + while (true) { + const next = await stream.next() + if (next.done) { + break + } + events.push(next.value) + } + expect( + events.filter((event) => event.type === 'text') + ).toHaveLength(20) + expect(events.at(-1)).toMatchObject({ type: 'done' }) expect(server.setInboundPaused).toHaveBeenCalledWith(true) - expect(server.setInboundPaused).not.toHaveBeenCalledWith(false) - expect(cancel).toHaveBeenCalledOnce() + expect(server.setInboundPaused).toHaveBeenCalledWith(false) + expect(cancel).not.toHaveBeenCalled() await expect( store.getByConversation(request.conversationId) ).resolves.toMatchObject({ - state: 'outcome-unknown', - activePromptOperationId: request.requestId + state: 'ready', + activePromptOperationId: undefined }) }) @@ -2504,8 +2518,6 @@ describe('AcpRemoteRuntime Agent-owned prompts', () => { capabilities: { imageInput: false }, limits: { maximumOutputTokens: 4_096, - maximumModelCalls: 100, - maximumTotalOutputTokens: 409_600, requestTimeoutMilliseconds: 60_000 } } diff --git a/src/main/agent/acp-remote-runtime.ts b/src/main/agent/acp-remote-runtime.ts index e44417b1..9fb80cc3 100644 --- a/src/main/agent/acp-remote-runtime.ts +++ b/src/main/agent/acp-remote-runtime.ts @@ -15,9 +15,6 @@ import { import type { AgentRuntimeStatus } from '../../shared/contracts' -import { - maximumConversationToolActivities -} from '../../shared/assistant-contracts' import { assertRemotePromptAcceptanceMatchesPreparation, REMOTE_SEMANTIC_TRANSCRIPT_LIMITS, @@ -148,7 +145,6 @@ type ActivePrompt = { updates: QueuedSessionUpdate[] pendingUpdateBytes: number inboundPaused: boolean - outputCharacters: number toolCalls: Map< string, { @@ -1846,19 +1842,6 @@ export class AcpRemoteRuntime implements AgentRuntime { this.options.maxPendingUpdateBytes ?? DEFAULT_MAX_PENDING_UPDATE_BYTES ) - if ( - prompt.updates.length >= maximumItems || - prompt.pendingUpdateBytes + bytes > maximumBytes - ) { - const overflow = new Error( - '远端 Runtime 待消费事件超过安全上限,输出已截断且请求状态未知' - ) - prompt.interruption = overflow - prompt.interrupt?.(overflow) - prompt.wake?.() - prompt.wake = undefined - throw overflow - } let consume: (() => void) | undefined const consumed = new Promise((resolve) => { consume = resolve @@ -2030,19 +2013,13 @@ export class AcpRemoteRuntime implements AgentRuntime { }) } - private limitEvent( - prompt: ActivePrompt, - event: RuntimePublicEvent - ): RuntimePublicEvent { + private limitEvent(event: RuntimePublicEvent): RuntimePublicEvent { const maximum = this.options.maxEventCharacters ?? DEFAULT_MAX_EVENT_CHARACTERS - let characters = 0 if (event.type === 'text' || event.type === 'reasoning') { - characters = event.delta.length - if (characters > maximum) { + if (event.delta.length > maximum) { event = { ...event, delta: `${event.delta.slice(0, maximum)}…` } - characters = maximum + 1 } } else if (event.type === 'tool') { const fieldMaximum = Math.max(1, Math.floor(maximum / 3)) @@ -2056,10 +2033,6 @@ export class AcpRemoteRuntime implements AgentRuntime { output: bounded(event.output), error: bounded(event.error) } - characters = - (event.input?.length ?? 0) + - (event.output?.length ?? 0) + - (event.error?.length ?? 0) } else if (event.type === 'subagent') { const fieldMaximum = Math.max(1, Math.floor(maximum / 2)) const bounded = (value: string | undefined): string | undefined => @@ -2071,10 +2044,6 @@ export class AcpRemoteRuntime implements AgentRuntime { output: bounded(event.output), error: bounded(event.error) } - characters = - (event.reason?.length ?? 0) + - (event.output?.length ?? 0) + - (event.error?.length ?? 0) } else if (event.type === 'status') { if (event.message.length > maximum) { event = { @@ -2082,15 +2051,6 @@ export class AcpRemoteRuntime implements AgentRuntime { message: `${event.message.slice(0, maximum)}…` } } - characters = event.message.length - } - prompt.outputCharacters += characters - if ( - prompt.outputCharacters > - (this.options.maxRequestOutputBytes ?? - DEFAULT_MAX_REQUEST_OUTPUT_BYTES) - ) { - throw new Error('远端 Runtime 请求输出超过安全上限') } return event } @@ -2120,15 +2080,6 @@ export class AcpRemoteRuntime implements AgentRuntime { ) { const callId = update.toolCallId.slice(0, 256) const previous = prompt.toolCalls.get(callId) - if ( - !previous && - prompt.toolCalls.size >= - maximumConversationToolActivities - ) { - throw new Error( - '远端 Runtime 单次运行的工具调用超过 100 个' - ) - } const retainedMaximum = this.options.maxEventCharacters ?? DEFAULT_MAX_EVENT_CHARACTERS @@ -2359,7 +2310,6 @@ export class AcpRemoteRuntime implements AgentRuntime { updates: [], pendingUpdateBytes: 0, inboundPaused: false, - outputCharacters: 0, toolCalls: new Map(), open: true, completed: false, @@ -2559,7 +2509,7 @@ export class AcpRemoteRuntime implements AgentRuntime { ) const mapped = this.mapUpdate(prompt, notification.update) if (mapped !== undefined) { - publicEvents.push(this.limitEvent(prompt, mapped)) + publicEvents.push(this.limitEvent(mapped)) } } else if (transcriptEvent.kind === 'prompt-terminal') { const terminal = transcriptTerminal( @@ -2764,7 +2714,6 @@ export class AcpRemoteRuntime implements AgentRuntime { updates: [], pendingUpdateBytes: 0, inboundPaused: false, - outputCharacters: 0, toolCalls: new Map(), open: true, completed: false, @@ -2867,7 +2816,7 @@ export class AcpRemoteRuntime implements AgentRuntime { } const event = this.mapUpdate(prompt, update) if (event) { - yield this.limitEvent(prompt, event) + yield this.limitEvent(event) } } await this.awaitOperation( diff --git a/src/main/agent/continue-host-adapter.test.ts b/src/main/agent/continue-host-adapter.test.ts index 00141543..84247a36 100644 --- a/src/main/agent/continue-host-adapter.test.ts +++ b/src/main/agent/continue-host-adapter.test.ts @@ -1176,7 +1176,7 @@ describe('ContinueHostAdapter', () => { expect(killed).toBe(true) }) - it('fails when the patched host reports dropped stream events', async () => { + it('completes from final history when the patched stream is truncated', async () => { const distribution = await createDistribution() let stateRequests = 0 vi.stubGlobal( @@ -1185,11 +1185,23 @@ describe('ContinueHostAdapter', () => { if (String(input).endsWith('/state')) { stateRequests += 1 return Response.json({ - session: { history: [] }, - isProcessing: stateRequests > 1, + session: { + history: + stateRequests === 1 + ? [] + : [ + { + message: { + role: 'assistant', + content: 'CONTINUE_OK' + } + } + ] + }, + isProcessing: false, messageQueueLength: 0, pendingPermission: null, - goodbuddyEventsOverflow: stateRequests > 1 + goodbuddyEventsOverflow: stateRequests === 2 }) } return Response.json({}) @@ -1224,27 +1236,15 @@ describe('ContinueHostAdapter', () => { new AbortController().signal, async () => 'deny' ) - ).rejects.toThrow('流式事件超过安全限制') + ).resolves.toMatchObject({ + text: 'CONTINUE_OK', + streamTruncated: true + }) }) - it.each([ - { - label: 'event count', - limits: { - maximumStreamEvents: 1, - maximumStreamEventBytes: 10_000 - } - }, - { - label: 'event bytes', - limits: { - maximumStreamEvents: 10, - maximumStreamEventBytes: 60 - } - } - ])( - 'enforces cumulative streamed $label across state polls', - async ({ limits }) => { + it( + 'truncates cumulative streamed bytes without stopping the run', + async () => { const distribution = await createDistribution() let stateRequests = 0 vi.stubGlobal( @@ -1253,8 +1253,20 @@ describe('ContinueHostAdapter', () => { if (String(input).endsWith('/state')) { stateRequests += 1 return Response.json({ - session: { history: [] }, - isProcessing: stateRequests > 1, + session: { + history: + stateRequests < 3 + ? [] + : [ + { + message: { + role: 'assistant', + content: '1234567890CONTINUE_OK' + } + } + ] + }, + isProcessing: stateRequests === 2, messageQueueLength: 0, pendingPermission: null, goodbuddyEvents: @@ -1291,8 +1303,7 @@ describe('ContinueHostAdapter', () => { } }, { - ...limits, - maximumToolCalls: 100, + maximumStreamEventBytes: 60, terminateProcessTree: vi.fn().mockResolvedValue(undefined) } ) @@ -1308,14 +1319,18 @@ describe('ContinueHostAdapter', () => { } } ) - ).rejects.toThrow('流式事件超过安全限制') + ).resolves.toMatchObject({ + text: '1234567890CONTINUE_OK', + streamedText: true, + streamTruncated: true + }) expect(forwarded).toEqual([ { type: 'text', delta: '1234567890' } ]) } ) - it('enforces cumulative unique tool calls before forwarding a later batch', async () => { + it('forwards more than 100 unique tool calls', async () => { const distribution = await createDistribution() let stateRequests = 0 vi.stubGlobal( @@ -1324,20 +1339,30 @@ describe('ContinueHostAdapter', () => { if (String(input).endsWith('/state')) { stateRequests += 1 return Response.json({ - session: { history: [] }, - isProcessing: stateRequests > 1, + session: { + history: + stateRequests === 1 + ? [] + : [ + { + message: { + role: 'assistant', + content: 'TOOLS_OK' + } + } + ] + }, + isProcessing: false, messageQueueLength: 0, pendingPermission: null, goodbuddyEvents: - stateRequests > 1 - ? [ - { - type: 'tool', - callId: `call-${stateRequests}`, - name: 'Bash', - state: 'running' - } - ] + stateRequests === 2 + ? Array.from({ length: 101 }, (_, index) => ({ + type: 'tool', + callId: `call-${index + 1}`, + name: 'Bash', + state: 'completed' + })) : [] }) } @@ -1369,9 +1394,7 @@ describe('ContinueHostAdapter', () => { } }, { - maximumStreamEvents: 10, - maximumStreamEventBytes: 10_000, - maximumToolCalls: 1, + maximumStreamEventBytes: 1_000_000, terminateProcessTree: vi.fn().mockResolvedValue(undefined) } ) @@ -1387,11 +1410,11 @@ describe('ContinueHostAdapter', () => { } } ) - ).rejects.toThrow('工具调用超过 100 个') - expect(forwarded).toHaveLength(1) - expect(forwarded[0]).toMatchObject({ + ).resolves.toMatchObject({ text: 'TOOLS_OK' }) + expect(forwarded).toHaveLength(101) + expect(forwarded.at(-1)).toMatchObject({ type: 'tool', - tool: { callId: 'call-2' } + tool: { callId: 'call-101' } }) }) @@ -1461,9 +1484,7 @@ describe('ContinueHostAdapter', () => { } }, { - maximumStreamEvents: 10, maximumStreamEventBytes: 10_000, - maximumToolCalls: 10, terminateProcessTree } ) diff --git a/src/main/agent/continue-host-adapter.ts b/src/main/agent/continue-host-adapter.ts index 7368b8be..10f5815e 100644 --- a/src/main/agent/continue-host-adapter.ts +++ b/src/main/agent/continue-host-adapter.ts @@ -26,9 +26,6 @@ import type { RuntimeNativeSnapshot, RuntimeSettings } from '../../shared/contracts' -import { - maximumConversationToolActivities -} from '../../shared/assistant-contracts' import { continueConfigurationPresetSchema, runtimeNativeInventoryLimits, @@ -69,9 +66,7 @@ const maximumConfiguredMcpServers = runtimeNativeInventoryLimits.mcpServers const maximumConfiguredRules = runtimeNativeInventoryLimits.rules const maximumConfiguredPrompts = runtimeNativeInventoryLimits.prompts -const maximumStreamEvents = 5_000 const maximumStreamEventBytes = 2 * 1024 * 1024 -const maximumToolCalls = maximumConversationToolActivities const defaultControlRequestTimeoutMs = 30_000 const knowledgeMcpName = 'goodbuddy-knowledge' const customMcpName = 'goodbuddy-custom-mcp' @@ -142,7 +137,7 @@ const continueHostQuestionSchema = z const stateSchema = z.object({ session: z.object({ - history: z.array(z.unknown()).max(5_000), + history: z.array(z.unknown()), usage: sessionUsageSchema.optional() }), isProcessing: z.boolean(), @@ -152,12 +147,11 @@ const stateSchema = z.object({ toolName: z.string().min(1).max(128), toolArgs: z.record(z.string(), z.unknown()), requestId: z.string().min(1).max(256), - toolCallPreview: z.array(z.unknown()).max(100).optional() + toolCallPreview: z.array(z.unknown()).optional() }) .nullable(), goodbuddyEvents: z .array(continueHostStreamEventSchema) - .max(maximumStreamEvents) .optional(), goodbuddyEventsOverflow: z.boolean().optional(), goodbuddyQuestion: continueHostQuestionSchema.nullable().optional() @@ -199,6 +193,7 @@ export type ContinueHostTool = { export type ContinueHostRunResult = { text: string streamedText?: true + streamTruncated?: true usage?: ContinueHostUsage tools?: ContinueHostTool[] } @@ -249,9 +244,7 @@ export type ContinueHostAdapterOptions = { export type ContinueHostAdapterDependencies = { terminateProcessTree: typeof terminateProcessTreeAndWait - maximumStreamEvents: number maximumStreamEventBytes: number - maximumToolCalls: number controlRequestTimeoutMs: number } @@ -713,9 +706,6 @@ function extractContinueTools( if (!callId || typeof name !== 'string' || !name.trim()) { continue } - if (!tools.has(callId) && tools.size >= 100) { - throw new Error('Continue 单次运行的工具调用超过 100 个') - } const status = state.status const normalizedState = status === 'done' || status === 'completed' @@ -834,9 +824,7 @@ export class ContinueHostAdapter { ) { this.dependencies = { terminateProcessTree: terminateProcessTreeAndWait, - maximumStreamEvents, maximumStreamEventBytes, - maximumToolCalls, controlRequestTimeoutMs: defaultControlRequestTimeoutMs, ...dependencies } @@ -985,7 +973,7 @@ export class ContinueHostAdapter { patched = replaceExactly( patched, streamCallbacksMarker, - 'a={onContent:u=>{if(!u)return;let l=String(u);e.goodbuddyEventsBytes+=Buffer.byteLength(l);l.length<=1e5&&e.goodbuddyEvents.length<5e3&&e.goodbuddyEventsBytes<=2097152?e.goodbuddyEvents.push({type:"text",delta:l}):e.goodbuddyEventsOverflow=!0},onContentComplete:u=>{},onToolStart:(u,l,c)=>{if(!c)return;let d={type:"tool",callId:c,name:u,state:"running",input:(()=>{try{return JSON.stringify(l).slice(0,4e3)}catch{return"[无法序列化]"}})()};e.goodbuddyEventsBytes+=Buffer.byteLength(JSON.stringify(d));e.goodbuddyEvents.length<5e3&&e.goodbuddyEventsBytes<=2097152?e.goodbuddyEvents.push(d):e.goodbuddyEventsOverflow=!0},onToolResult:(u,l,c,d)=>{if(!d)return;let p={type:"tool",callId:d,name:l,state:c==="done"?"completed":"failed",output:String(u).slice(0,16e3)};e.goodbuddyEventsBytes+=Buffer.byteLength(JSON.stringify(p));e.goodbuddyEvents.length<5e3&&e.goodbuddyEventsBytes<=2097152?e.goodbuddyEvents.push(p):e.goodbuddyEventsOverflow=!0},onToolError:(u,l,c)=>{if(!c)return;let d={type:"tool",callId:c,name:l??"unknown",state:"failed",error:String(u).slice(0,1e3)};e.goodbuddyEventsBytes+=Buffer.byteLength(JSON.stringify(d));e.goodbuddyEvents.length<5e3&&e.goodbuddyEventsBytes<=2097152?e.goodbuddyEvents.push(d):e.goodbuddyEventsOverflow=!0},onToolPermissionRequest:' + 'a={onContent:u=>{if(!u)return;let l=String(u);e.goodbuddyEventsBytes+=Buffer.byteLength(l);l.length<=1e5&&e.goodbuddyEventsBytes<=2097152?e.goodbuddyEvents.push({type:"text",delta:l}):e.goodbuddyEventsOverflow=!0},onContentComplete:u=>{},onToolStart:(u,l,c)=>{if(!c)return;let d={type:"tool",callId:c,name:u,state:"running",input:(()=>{try{return JSON.stringify(l).slice(0,4e3)}catch{return"[无法序列化]"}})()};e.goodbuddyEventsBytes+=Buffer.byteLength(JSON.stringify(d));e.goodbuddyEventsBytes<=2097152?e.goodbuddyEvents.push(d):e.goodbuddyEventsOverflow=!0},onToolResult:(u,l,c,d)=>{if(!d)return;let p={type:"tool",callId:d,name:l,state:c==="done"?"completed":"failed",output:String(u).slice(0,16e3)};e.goodbuddyEventsBytes+=Buffer.byteLength(JSON.stringify(p));e.goodbuddyEventsBytes<=2097152?e.goodbuddyEvents.push(p):e.goodbuddyEventsOverflow=!0},onToolError:(u,l,c)=>{if(!c)return;let d={type:"tool",callId:c,name:l??"unknown",state:"failed",error:String(u).slice(0,1e3)};e.goodbuddyEventsBytes+=Buffer.byteLength(JSON.stringify(d));e.goodbuddyEventsBytes<=2097152?e.goodbuddyEvents.push(d):e.goodbuddyEventsOverflow=!0},onToolPermissionRequest:' ) patched = replaceExactly( patched, @@ -1584,7 +1572,7 @@ export class ContinueHostAdapter { let observedTools: ContinueHostTool[] = [] const reportedQuestionIds = new Set() let streamedText = false - let streamEventCount = 0 + let streamTruncated = false let streamEventBytes = 0 const observedToolCallIds = new Set() try { @@ -1635,38 +1623,28 @@ export class ContinueHostAdapter { signal: executionSignal }) ) - if (state.goodbuddyEventsOverflow) { - throw new Error('Continue 宿主流式事件超过安全限制') - } - const streamEvents = state.goodbuddyEvents ?? [] + streamTruncated ||= state.goodbuddyEventsOverflow === true + let streamEvents = state.goodbuddyEvents ?? [] const batchStreamEventBytes = Buffer.byteLength( JSON.stringify(streamEvents) ) - const nextStreamEventCount = - streamEventCount + streamEvents.length const nextStreamEventBytes = streamEventBytes + batchStreamEventBytes + if ( + nextStreamEventBytes > + this.dependencies.maximumStreamEventBytes + ) { + streamTruncated = true + streamEvents = [] + } else { + streamEventBytes = nextStreamEventBytes + } const nextToolCallIds = new Set(observedToolCallIds) for (const event of streamEvents) { if (event.type === 'tool') { nextToolCallIds.add(event.callId) } } - if ( - nextStreamEventCount > - this.dependencies.maximumStreamEvents || - nextStreamEventBytes > - this.dependencies.maximumStreamEventBytes - ) { - throw new Error('Continue 宿主流式事件超过安全限制') - } - if ( - nextToolCallIds.size > this.dependencies.maximumToolCalls - ) { - throw new Error('Continue 单次运行的工具调用超过 100 个') - } - streamEventCount = nextStreamEventCount - streamEventBytes = nextStreamEventBytes for (const callId of nextToolCallIds) { observedToolCallIds.add(callId) } @@ -1677,11 +1655,6 @@ export class ContinueHostAdapter { for (const tool of historyTools) { observedToolCallIds.add(tool.callId) } - if ( - observedToolCallIds.size > this.dependencies.maximumToolCalls - ) { - throw new Error('Continue 单次运行的工具调用超过 100 个') - } observedTools = mergeContinueTools( observedTools, historyTools @@ -1744,12 +1717,6 @@ export class ContinueHostAdapter { } const pending = state.pendingPermission if (pending && !handledPermissionIds.has(pending.requestId)) { - if ( - handledPermissionIds.size >= - this.dependencies.maximumToolCalls - ) { - throw new Error('Continue 单次运行的工具调用超过 100 个') - } handledPermissionIds.add(pending.requestId) const pendingCallId = observedTools.find( @@ -1761,13 +1728,6 @@ export class ContinueHostAdapter { if ( !observedTools.some((tool) => tool.callId === pendingCallId) ) { - if ( - !observedToolCallIds.has(pendingCallId) && - observedToolCallIds.size >= - this.dependencies.maximumToolCalls - ) { - throw new Error('Continue 单次运行的工具调用超过 100 个') - } observedToolCallIds.add(pendingCallId) observedTools = [ ...observedTools, @@ -1829,6 +1789,9 @@ export class ContinueHostAdapter { return { text, ...(streamedText ? { streamedText: true as const } : {}), + ...(streamTruncated + ? { streamTruncated: true as const } + : {}), ...(usage ? { usage } : {}), ...(observedTools.length > 0 ? { tools: observedTools } diff --git a/src/main/agent/continue-runtime.test.ts b/src/main/agent/continue-runtime.test.ts index ceea44d4..de94bd15 100644 --- a/src/main/agent/continue-runtime.test.ts +++ b/src/main/agent/continue-runtime.test.ts @@ -1134,7 +1134,7 @@ describe('ContinueAgentRuntime', () => { ).rejects.toThrow('已失效或不存在') }) - it('fails instead of silently dropping an overflowing stream queue', async () => { + it('drains more than 1,000 queued stream events without stopping the run', async () => { mocks.runHost.mockImplementation( async ( _prompt, @@ -1160,11 +1160,15 @@ describe('ContinueAgentRuntime', () => { new AbortController().signal ) - await expect(async () => { - for await (const _event of stream) { - void _event - } - }).rejects.toThrow('流式事件积压超过安全限制') + const events: RuntimeEvent[] = [] + for await (const event of stream) { + events.push(event) + } + + expect( + events.filter((event) => event.type === 'text') + ).toHaveLength(1_001) + expect(events.at(-1)).toMatchObject({ type: 'done' }) }) it('aborts the host run when stream consumption ends early', async () => { diff --git a/src/main/agent/continue-runtime.ts b/src/main/agent/continue-runtime.ts index eac24666..80da003a 100644 --- a/src/main/agent/continue-runtime.ts +++ b/src/main/agent/continue-runtime.ts @@ -64,7 +64,6 @@ export type ContinueRuntimeOptions = { // The prompt reaches the Continue host through a local HTTP POST body, so no // platform command-line limit applies to it. const MAX_CONTINUE_PROMPT_CHARACTERS = 128_000 -const MAX_QUEUED_STREAM_EVENTS = 1_000 const scopedReadToolNameSet = new Set(scopedReadToolNames) function continueToolFailureMessage(tool: ContinueHostTool): string { @@ -548,6 +547,7 @@ export class ContinueAgentRuntime implements AgentRuntime { } let result: ContinueHostRunResult const emittedTools = new Map() + let emittedText = '' const requestQuestionIds = new Set() try { const host = this.getHostAdapter( @@ -574,9 +574,6 @@ export class ContinueAgentRuntime implements AgentRuntime { const hostController = new AbortController() const hostSignal = AbortSignal.any([signal, hostController.signal]) const onEvent = (event: ContinueHostStreamEvent): void => { - if (queuedEvents.length >= MAX_QUEUED_STREAM_EVENTS) { - throw new Error('Continue 流式事件积压超过安全限制') - } queuedEvents.push(event) wakeStream?.() wakeStream = undefined @@ -623,6 +620,7 @@ export class ContinueAgentRuntime implements AgentRuntime { emittedTools.set(event.tool.callId, event.tool) } if (event.type === 'text') { + emittedText += event.delta yield { requestId: request.requestId, type: 'text', @@ -742,11 +740,25 @@ export class ContinueAgentRuntime implements AgentRuntime { yield finalEvent } } - if (!result.streamedText) { + if (result.streamTruncated) { yield { requestId: request.requestId, - type: 'text', - delta: result.text + type: 'status', + message: + 'Continue 输出较长,已截断部分流式展示;任务已继续完成' + } + } + if (!result.streamedText || result.streamTruncated) { + const remainingText = + result.streamTruncated && result.text.startsWith(emittedText) + ? result.text.slice(emittedText.length) + : result.text + if (remainingText) { + yield { + requestId: request.requestId, + type: 'text', + delta: remainingText + } } } if (result.usage) { diff --git a/src/main/agent/opencode-runtime.test.ts b/src/main/agent/opencode-runtime.test.ts index 7143fe39..eeb47efb 100644 --- a/src/main/agent/opencode-runtime.test.ts +++ b/src/main/agent/opencode-runtime.test.ts @@ -2035,7 +2035,7 @@ describe('OpenCodeRuntime embedded permission mediation', () => { await runtime.dispose() }) - it('fails before emitting text that exceeds the aggregate output budget', async () => { + it('truncates oversized text display without stopping the run', async () => { const setup = runClient([ { id: 'first-text', @@ -2056,29 +2056,50 @@ describe('OpenCodeRuntime embedded permission mediation', () => { field: 'text', delta: 'b'.repeat(600_000) } + }, + { + id: 'idle', + type: 'session.idle', + properties: { sessionID: 'session-1' } } ]) const runtime = embeddedRuntime(setup.client) - const stream = runtime.run( - { - requestId: '3f496642-f47d-4e0a-8944-a32c77b0d6ef', - conversationId: 'conversation-1', - prompt: 'test', - workMode: 'execute' - }, - new AbortController().signal - ) + const events = await collectRun(runtime, 'execute') + const text = events + .flatMap((event) => event.type === 'text' ? [event.delta] : []) + .join('') - await stream.next() - const firstText = await stream.next() - expect(firstText.value).toMatchObject({ - type: 'text', - delta: expect.stringMatching(/^a+$/u) + expect(Buffer.byteLength(text)).toBe(1024 * 1024) + expect(events).toContainEqual({ + requestId: '3f496642-f47d-4e0a-8944-a32c77b0d6ef', + type: 'status', + message: + 'OpenCode 输出较长,已停止继续展示文本;任务仍在继续' }) - await expect(stream.next()).rejects.toThrow( - '文本与推理输出超过 1 MB 安全限制' - ) - expect(setup.session.abort).toHaveBeenCalled() + expect(events.at(-1)).toMatchObject({ type: 'done' }) + expect(setup.session.abort).not.toHaveBeenCalled() + await runtime.dispose() + }) + + it('completes more than 100 distinct tool calls in one run', async () => { + const setup = runClient([ + ...Array.from({ length: 101 }, (_, index) => + completedToolEvent(`call-${index + 1}`) + ), + { + id: 'idle', + type: 'session.idle', + properties: { sessionID: 'session-1' } + } + ]) + const runtime = embeddedRuntime(setup.client) + + const events = await collectRun(runtime, 'execute') + + expect( + events.filter((event) => event.type === 'tool') + ).toHaveLength(101) + expect(events.at(-1)).toMatchObject({ type: 'done' }) await runtime.dispose() }) @@ -2184,6 +2205,8 @@ describe('OpenCodeRuntime embedded permission mediation', () => { read: false, write: false, bash: false, + 'goodbuddy-data-*': false, + 'goodbuddy-custom-*': false, [knowledgeToolId]: true } }), @@ -2264,7 +2287,7 @@ describe('OpenCodeRuntime embedded permission mediation', () => { await runtime.dispose() }) - it('serializes overlapping embedded MCP registration and discovery', async () => { + it('runs embedded conversations concurrently with request-scoped MCP registrations', async () => { const setup = runClient([ { id: 'idle', @@ -2357,13 +2380,19 @@ describe('OpenCodeRuntime embedded permission mediation', () => { 'conversation-two', 'second-token' ) - await new Promise((resolve) => setTimeout(resolve, 10)) - expect(mcpAdd).toHaveBeenCalledTimes(1) - - resolveFirstAdd() - await first - await vi.waitFor(() => expect(mcpAdd).toHaveBeenCalledTimes(2)) - await second + try { + await vi.waitFor( + () => expect(mcpAdd).toHaveBeenCalledTimes(2), + { timeout: 500 } + ) + await vi.waitFor( + () => expect(setup.session.promptAsync).toHaveBeenCalledOnce(), + { timeout: 500 } + ) + } finally { + resolveFirstAdd() + } + await Promise.all([first, second]) expect( mcpAdd.mock.calls.map( ([input]) => @@ -2483,6 +2512,8 @@ describe('OpenCodeRuntime embedded permission mediation', () => { write: false, bash: false, task: false, + 'goodbuddy-data-*': false, + 'goodbuddy-custom-*': false, skill: true } }), @@ -2557,11 +2588,30 @@ describe('OpenCodeRuntime embedded permission mediation', () => { title: 'GoodBuddy 对话', directory: process.cwd(), permission: [ - { permission: '*', pattern: '*', action: 'allow' } + { permission: '*', pattern: '*', action: 'allow' }, + { + permission: 'goodbuddy-data-*', + pattern: '*', + action: 'deny' + }, + { + permission: 'goodbuddy-custom-*', + pattern: '*', + action: 'deny' + } ] }, { signal: expect.any(AbortSignal) } ) + expect(session.promptAsync).toHaveBeenCalledWith( + expect.objectContaining({ + tools: { + 'goodbuddy-data-*': false, + 'goodbuddy-custom-*': false + } + }), + expect.anything() + ) expect(permissionReply).toHaveBeenCalledOnce() expect(permissionReply).toHaveBeenCalledWith( { @@ -2926,7 +2976,9 @@ describe('OpenCodeRuntime embedded permission mediation', () => { read: false, write: false, bash: false, - task: false + task: false, + 'goodbuddy-data-*': false, + 'goodbuddy-custom-*': false } }), expect.anything() diff --git a/src/main/agent/opencode-runtime.ts b/src/main/agent/opencode-runtime.ts index fd38a05c..61e3f24c 100644 --- a/src/main/agent/opencode-runtime.ts +++ b/src/main/agent/opencode-runtime.ts @@ -22,9 +22,6 @@ import type { AgentRuntimeStatus, SubagentEvent } from '../../shared/contracts' -import { - maximumConversationToolActivities -} from '../../shared/assistant-contracts' import { boundedRuntimeIdentifierSchema, runtimeNativeInventoryLimits, @@ -78,7 +75,6 @@ const MAX_PERMISSION_PATTERNS = 32 const MAX_PERMISSION_PATTERN_LENGTH = 1_024 const MAX_PERMISSION_PATTERNS_BYTES = 8 * 1_024 const MAX_PERMISSION_METADATA_BYTES = 8 * 1_024 -const MAX_TOOL_CALLS_PER_RUN = maximumConversationToolActivities const MAX_EXECUTION_OUTPUT_BYTES = 1024 * 1024 const MAX_QUESTION_REQUEST_BYTES = 32 * 1_024 const MAX_QUESTIONS_PER_REQUEST = 4 @@ -99,6 +95,15 @@ const TEMPORARY_MCP_PREFIXES = [ 'goodbuddy-data-', 'goodbuddy-custom-' ] as const +const temporaryMcpToolOverrides = Object.fromEntries( + TEMPORARY_MCP_PREFIXES.map((prefix) => [`${prefix}*`, false]) +) +const temporaryMcpDenyRules: PermissionRuleset = + TEMPORARY_MCP_PREFIXES.map((prefix) => ({ + permission: `${prefix}*`, + pattern: '*', + action: 'deny' + })) const OPENCODE_INTERNAL_TOOL_IDS = new Set(['invalid']) const OPENCODE_BUILTIN_TOOL_KINDS: Readonly< Partial> @@ -719,7 +724,6 @@ export class OpenCodeRuntime implements AgentRuntime { upstreamQuestionId: string } >() - private embeddedRunTail: Promise = Promise.resolve() private readonly conversationRunTails = new Map>() private readonly dependencies: OpenCodeRuntimeDependencies @@ -751,36 +755,6 @@ export class OpenCodeRuntime implements AgentRuntime { return this.usesEmbeddedPermissionMediation() } - private async acquireEmbeddedRun( - signal: AbortSignal - ): Promise<() => void> { - signal.throwIfAborted() - const previous = this.embeddedRunTail - let release!: () => void - const current = new Promise((resolve) => { - release = resolve - }) - this.embeddedRunTail = previous.then( - () => current, - () => current - ) - let abort!: () => void - const aborted = new Promise((_resolve, reject) => { - abort = () => reject(signal.reason) - }) - signal.addEventListener('abort', abort, { once: true }) - try { - await Promise.race([previous, aborted]) - signal.throwIfAborted() - return release - } catch (error) { - release() - throw error - } finally { - signal.removeEventListener('abort', abort) - } - } - private async acquireConversationRun( conversationId: string, signal: AbortSignal @@ -1835,12 +1809,8 @@ export class OpenCodeRuntime implements AgentRuntime { const executionSignal = deadline ? AbortSignal.any([signal, deadline.signal]) : signal - let releaseEmbedded: (() => void) | undefined let releaseConversation: (() => void) | undefined try { - releaseEmbedded = this.usesEmbeddedPermissionMediation() - ? await this.acquireEmbeddedRun(executionSignal) - : undefined releaseConversation = await this.acquireConversationRun( request.conversationId, executionSignal @@ -1856,7 +1826,6 @@ export class OpenCodeRuntime implements AgentRuntime { clearTimeout(deadlineTimer) } releaseConversation?.() - releaseEmbedded?.() } } @@ -2054,6 +2023,9 @@ export class OpenCodeRuntime implements AgentRuntime { request.workMode === 'execute' ? [ ...executePermissionRules, + ...(this.usesEmbeddedPermissionMediation() + ? temporaryMcpDenyRules + : []), ...nativeSkillPermissionRules, ...knowledgeToolIds.map((toolId) => ({ permission: toolId, @@ -2075,7 +2047,7 @@ export class OpenCodeRuntime implements AgentRuntime { ...readOnlyPermissionRules, ...nativeSkillPermissionRules ] - let disabledTools: Record | undefined + let toolOverrides: Record | undefined if (request.workMode !== 'execute') { const tools = await this.controlRequest( '读取工具清单', @@ -2089,15 +2061,23 @@ export class OpenCodeRuntime implements AgentRuntime { if (tools.error || !tools.data) { throw new Error('OpenCode 无法确认工具已禁用,已阻止只读请求') } - disabledTools = { + toolOverrides = { ...Object.fromEntries( tools.data.map((toolId) => [toolId, false]) ), + ...temporaryMcpToolOverrides, ...Object.fromEntries( knowledgeToolIds.map((toolId) => [toolId, true]) ), ...(nativeSkillIds.length > 0 ? { skill: true } : {}) } + } else if (this.usesEmbeddedPermissionMediation()) { + toolOverrides = { + ...temporaryMcpToolOverrides, + ...Object.fromEntries( + knowledgeToolIds.map((toolId) => [toolId, true]) + ) + } } const session = await this.getSessionId( client, @@ -2165,12 +2145,28 @@ export class OpenCodeRuntime implements AgentRuntime { const reasoningPartIds = new Set() const reportedQuestionIds = new Map() let aggregateOutputBytes = 0 - const consumeOutputBudget = (delta: string): void => { - aggregateOutputBytes += Buffer.byteLength(delta) - if (aggregateOutputBytes > MAX_EXECUTION_OUTPUT_BYTES) { - throw new Error( - 'OpenCode 单次运行的文本与推理输出超过 1 MB 安全限制' - ) + let outputTruncated = false + const retainOutputDelta = ( + delta: string + ): { delta: string; truncated: boolean } => { + if (outputTruncated) { + return { delta: '', truncated: false } + } + const bytes = Buffer.from(delta) + const remaining = + MAX_EXECUTION_OUTPUT_BYTES - aggregateOutputBytes + if (bytes.byteLength <= remaining) { + aggregateOutputBytes += bytes.byteLength + return { delta, truncated: false } + } + outputTruncated = true + aggregateOutputBytes = MAX_EXECUTION_OUTPUT_BYTES + return { + delta: bytes + .subarray(0, Math.max(0, remaining)) + .toString('utf8') + .replace(/\uFFFD+$/u, ''), + truncated: true } } let hasResponseTextAfterFailure = false @@ -2228,8 +2224,8 @@ export class OpenCodeRuntime implements AgentRuntime { nativeSkillIds.length > 0 ? undefined : this.options.skillInstructions || undefined, - ...(disabledTools - ? { tools: disabledTools } + ...(toolOverrides + ? { tools: toolOverrides } : {}), parts: [ { type: 'text' as const, text: promptText }, @@ -2276,7 +2272,6 @@ export class OpenCodeRuntime implements AgentRuntime { 'thinking' ].includes(event.properties.field) if (reasoning || event.properties.field === 'text') { - consumeOutputBudget(event.properties.delta) if ( !reasoning && /\S/u.test(event.properties.delta) && @@ -2286,10 +2281,21 @@ export class OpenCodeRuntime implements AgentRuntime { ) { hasResponseTextAfterFailure = true } - yield { - requestId: request.requestId, - type: reasoning ? 'reasoning' : 'text', - delta: event.properties.delta + const retained = retainOutputDelta(event.properties.delta) + if (retained.delta) { + yield { + requestId: request.requestId, + type: reasoning ? 'reasoning' : 'text', + delta: retained.delta + } + } + if (retained.truncated) { + yield { + requestId: request.requestId, + type: 'status', + message: + 'OpenCode 输出较长,已停止继续展示文本;任务仍在继续' + } } } } @@ -2307,12 +2313,6 @@ export class OpenCodeRuntime implements AgentRuntime { throw new Error('OpenCode 工具调用 ID 格式无效') } const toolName = part.tool.slice(0, 200) - if ( - !toolStates.has(callId) && - toolStates.size >= MAX_TOOL_CALLS_PER_RUN - ) { - throw new Error('OpenCode 单次运行的工具调用超过 100 个') - } const state = part.state.status === 'error' ? 'failed' : part.state.status if (state === 'failed') { @@ -2372,11 +2372,21 @@ export class OpenCodeRuntime implements AgentRuntime { event.properties.sessionID === sessionId && event.properties.delta ) { - consumeOutputBudget(event.properties.delta) - yield { - requestId: request.requestId, - type: 'reasoning', - delta: event.properties.delta + const retained = retainOutputDelta(event.properties.delta) + if (retained.delta) { + yield { + requestId: request.requestId, + type: 'reasoning', + delta: retained.delta + } + } + if (retained.truncated) { + yield { + requestId: request.requestId, + type: 'status', + message: + 'OpenCode 输出较长,已停止继续展示文本;任务仍在继续' + } } } @@ -2469,14 +2479,6 @@ export class OpenCodeRuntime implements AgentRuntime { properties.id.length <= MAX_PERMISSION_NAME_LENGTH && !repliedPermissionIds.has(properties.id) ) { - if ( - repliedPermissionIds.size >= MAX_TOOL_CALLS_PER_RUN - ) { - throw new Error( - 'OpenCode 单次运行的权限请求超过 100 个', - { cause: error } - ) - } const permissionId = properties.id repliedPermissionIds.add(properties.id) const rejection = await this.controlRequest( @@ -2505,21 +2507,12 @@ export class OpenCodeRuntime implements AgentRuntime { if (repliedPermissionIds.has(permissionRequest.id)) { continue } - if (repliedPermissionIds.size >= MAX_TOOL_CALLS_PER_RUN) { - throw new Error('OpenCode 单次运行的权限请求超过 100 个') - } repliedPermissionIds.add(permissionRequest.id) const callId = ( permissionRequest.tool?.callID ?? permissionRequest.id ) const toolName = permissionRequest.permission.slice(0, 200) - if ( - !toolStates.has(callId) && - toolStates.size >= MAX_TOOL_CALLS_PER_RUN - ) { - throw new Error('OpenCode 单次运行的工具调用超过 100 个') - } toolStates.set(callId, { name: toolName, state: 'pending' }) yield { requestId: request.requestId, @@ -2772,10 +2765,8 @@ export class OpenCodeRuntime implements AgentRuntime { const executionSignal = deadline ? AbortSignal.any([signal, deadline.signal]) : signal - let releaseEmbedded: (() => void) | undefined let releaseConversation: (() => void) | undefined try { - releaseEmbedded = await this.acquireEmbeddedRun(executionSignal) releaseConversation = await this.acquireConversationRun( request.conversationId, executionSignal @@ -2932,7 +2923,6 @@ export class OpenCodeRuntime implements AgentRuntime { clearTimeout(deadlineTimer) } releaseConversation?.() - releaseEmbedded?.() } } diff --git a/src/main/agent/runtime-e2e.manual.test.ts b/src/main/agent/runtime-e2e.manual.test.ts index ed6edad0..3c7740e8 100644 --- a/src/main/agent/runtime-e2e.manual.test.ts +++ b/src/main/agent/runtime-e2e.manual.test.ts @@ -1256,6 +1256,11 @@ describe.runIf(enabled)('runtime end-to-end', () => { ) .join('') ).toContain('Prism Relay') + const modelCalls = events.filter( + (event) => event.type === 'model-usage' + ).length + expect(modelCalls).toBe(2) + console.info(`REAL_OPENCODE_MCP_MODEL_CALLS=${modelCalls}`) } finally { await runtime.dispose() await gateway.dispose() diff --git a/src/main/assistant/assistant-database.test.ts b/src/main/assistant/assistant-database.test.ts index afc2c062..2f394dbb 100644 --- a/src/main/assistant/assistant-database.test.ts +++ b/src/main/assistant/assistant-database.test.ts @@ -159,6 +159,43 @@ describe('AssistantDatabase', () => { unchanged.close() }) + it('persists more than 100 tool activities in one message', async () => { + const database = await createDatabase() + const conversationId = '00000000-0000-4000-8000-000000000901' + const messageId = '00000000-0000-4000-8000-000000000902' + const tools = Array.from({ length: 101 }, (_, index) => ({ + callId: `call-${index + 1}`, + name: 'read', + state: 'completed' as const, + summary: `Tool ${index + 1}` + })) + + database.saveLocalConversations([ + { + header: { + id: conversationId, + title: '长工具链', + updatedAt: 1 + }, + messages: [ + { + id: messageId, + role: 'assistant', + content: '', + createdAt: 1, + state: 'complete', + tools + } + ] + } + ]) + + expect( + database.getConversation(conversationId)?.messages[0]?.tools + ).toHaveLength(101) + database.close() + }) + it('lists projects by creation time with newer projects last', async () => { vi.useFakeTimers() vi.setSystemTime(new Date('2026-08-07T00:00:00.000Z')) diff --git a/src/main/assistant/assistant-database.ts b/src/main/assistant/assistant-database.ts index b05db781..aacfb134 100644 --- a/src/main/assistant/assistant-database.ts +++ b/src/main/assistant/assistant-database.ts @@ -7,8 +7,6 @@ import { conversationMessageSchema, conversationSnapshotSchema, expertCreateSchema, - maximumConversationSubagentActivities, - maximumConversationToolActivities, normalizeInteractiveWorkMode, persistedProjectExecutionSpaceSchema, projectCreateSchema, @@ -1170,7 +1168,6 @@ function serializeConversationMessageMetadata( } const maximumRecoveredMessageLength = 1_000_000 -const maximumRecoveredMessageBlocks = 500 function appendRecoveredMessageBlock( blocks: ConversationMessageBlock[] | undefined, @@ -1190,7 +1187,7 @@ function appendRecoveredMessageBlock( maximumRecoveredMessageLength ) } - } else if (current.length < maximumRecoveredMessageBlocks) { + } else { current.push({ id: randomUUID(), type, @@ -1221,12 +1218,10 @@ function upsertRecoveredToolBlock( : block ) } - return blocks.length >= maximumRecoveredMessageBlocks - ? blocks - : [ - ...blocks, - { id: randomUUID(), type: 'tool' as const, tool } - ] + return [ + ...blocks, + { id: randomUUID(), type: 'tool' as const, tool } + ] } function upsertRecoveredSubagentBlock( @@ -1262,12 +1257,10 @@ function upsertRecoveredSubagentBlock( : block ) } - return blocks.length >= maximumRecoveredMessageBlocks - ? blocks - : [ - ...blocks, - { id: randomUUID(), type: 'subagent' as const, childTaskId } - ] + return [ + ...blocks, + { id: randomUUID(), type: 'subagent' as const, childTaskId } + ] } function terminalizeRecoveredToolBlocks( @@ -1377,7 +1370,7 @@ function reduceRecoveredAgentEvent( ) if (index >= 0) { tools[index] = tool - } else if (tools.length < maximumConversationToolActivities) { + } else { tools.push(tool) } next = { @@ -1410,9 +1403,7 @@ function reduceRecoveredAgentEvent( ) if (index >= 0) { subagents[index] = subagent - } else if ( - subagents.length < maximumConversationSubagentActivities - ) { + } else { subagents.push(subagent) } next = { diff --git a/src/main/remote-agent/managed-model-bridge.test.ts b/src/main/remote-agent/managed-model-bridge.test.ts index ca0972e3..57f5d5b7 100644 --- a/src/main/remote-agent/managed-model-bridge.test.ts +++ b/src/main/remote-agent/managed-model-bridge.test.ts @@ -15,7 +15,7 @@ const profile: ResolvedModelProfile = { } describe('managed Agent prompt model setup', () => { - it('validates and clones a bounded Agent prompt profile', () => { + it('validates and clones an Agent prompt profile without prompt-wide quotas', () => { const setup = createManagedModelBridge({ profile }) expect(setup.profile).toEqual({ @@ -32,8 +32,6 @@ describe('managed Agent prompt model setup', () => { capabilities: { imageInput: true }, limits: { maximumOutputTokens: 8_192, - maximumModelCalls: 100, - maximumTotalOutputTokens: 819_200, requestTimeoutMilliseconds: 60_000 } }) diff --git a/src/main/remote-agent/managed-model-bridge.ts b/src/main/remote-agent/managed-model-bridge.ts index 8e8c966f..1a215e38 100644 --- a/src/main/remote-agent/managed-model-bridge.ts +++ b/src/main/remote-agent/managed-model-bridge.ts @@ -8,7 +8,6 @@ import { createResolvedModelProfileDigest } from '../agent/remote-model-gateway' import type { ResolvedModelProfile } from '../runtime-settings-store' const DEFAULT_MAXIMUM_OUTPUT_TOKENS = 32_000 -const MAXIMUM_MODEL_CALLS_PER_PROMPT = 100 const MODEL_REQUEST_TIMEOUT_MILLISECONDS = 60_000 /** @@ -61,11 +60,6 @@ export function createManagedModelBridge(options: { }, limits: { maximumOutputTokens, - maximumModelCalls: MAXIMUM_MODEL_CALLS_PER_PROMPT, - maximumTotalOutputTokens: Math.min( - 10_000_000, - maximumOutputTokens * MAXIMUM_MODEL_CALLS_PER_PROMPT - ), requestTimeoutMilliseconds: MODEL_REQUEST_TIMEOUT_MILLISECONDS } diff --git a/src/main/remote-agent/managed-remote-acp-runtime.test.ts b/src/main/remote-agent/managed-remote-acp-runtime.test.ts index 66f7364b..09b2db47 100644 --- a/src/main/remote-agent/managed-remote-acp-runtime.test.ts +++ b/src/main/remote-agent/managed-remote-acp-runtime.test.ts @@ -226,8 +226,6 @@ function harness( capabilities: { imageInput: false }, limits: { maximumOutputTokens: 4_096, - maximumModelCalls: 100, - maximumTotalOutputTokens: 409_600, requestTimeoutMilliseconds: 60_000 } } diff --git a/src/main/remote-agent/protocol-remote-runtime-channel.test.ts b/src/main/remote-agent/protocol-remote-runtime-channel.test.ts index e0b268aa..bc5d6147 100644 --- a/src/main/remote-agent/protocol-remote-runtime-channel.test.ts +++ b/src/main/remote-agent/protocol-remote-runtime-channel.test.ts @@ -729,8 +729,6 @@ describe('ProtocolRemoteRuntimeChannel', () => { capabilities: { imageInput: false }, limits: { maximumOutputTokens: 4_096, - maximumModelCalls: 100, - maximumTotalOutputTokens: 409_600, requestTimeoutMilliseconds: 60_000 } }, diff --git a/src/renderer/src/App.test.tsx b/src/renderer/src/App.test.tsx index 532e29c5..2857e118 100644 --- a/src/renderer/src/App.test.tsx +++ b/src/renderer/src/App.test.tsx @@ -3694,6 +3694,37 @@ describe('App', () => { expect(screen.getByText('项目:默认项目')).toHaveClass('scope-badge') }) + it('keeps more than 100 tool activities in one response', async () => { + render() + + fireEvent.change(screen.getByLabelText('向 GoodBuddy 提问'), { + target: { value: '执行长工具链' } + }) + fireEvent.click(await screen.findByLabelText('发送')) + await waitFor(() => expect(run).toHaveBeenCalledOnce()) + const request = run.mock.calls[0]?.[0] + if (!request) { + throw new Error('Missing request') + } + + act(() => { + for (let index = 0; index < 101; index += 1) { + agentListener?.({ + requestId: request.requestId, + type: 'tool', + callId: `call-${index + 1}`, + name: 'read', + state: 'completed', + summary: `OpenCode 工具:read ${index + 1}` + }) + } + }) + + expect( + screen.getByRole('region', { name: '工具执行,共 101 项' }) + ).toBeInTheDocument() + }) + it('keeps adjacent streamed text blocks exact in StrictMode', async () => { render( diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index 0091761e..1a369a06 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -128,7 +128,6 @@ import { conversationMessageBlocksSchema, conversationSubagentActivitySchema, interactiveWorkModes, - maximumConversationSubagentActivities, normalizeInteractiveWorkMode, projectChannelLabels } from '../../shared/assistant-contracts' @@ -568,7 +567,6 @@ const minimumPrimaryWorkspaceWidth = 480 const primarySidebarKeyboardResizeStep = 16 const maxMessageContentLength = 1_000_000 -const maxMessageBlocks = 500 function getPrimarySidebarWidthLimits(viewportWidth: number): { minimum: number @@ -638,9 +636,6 @@ function appendMessageContentBlock( } return current } - if (current.length >= maxMessageBlocks) { - return current - } current.push({ id: crypto.randomUUID(), type, @@ -670,9 +665,6 @@ function upsertMessageToolBlock( : block ) } - if (blocks.length >= maxMessageBlocks) { - return blocks - } return [ ...blocks, { @@ -718,9 +710,6 @@ function upsertMessageSubagentBlock( : block ) } - if (blocks.length >= maxMessageBlocks) { - return blocks - } return [ ...blocks, { @@ -1327,8 +1316,6 @@ function isConversation(value: unknown): value is Conversation { ))) && (entry.subagents === undefined || (Array.isArray(entry.subagents) && - entry.subagents.length <= - maximumConversationSubagentActivities && entry.subagents.every( (subagent) => conversationSubagentActivitySchema.safeParse(subagent) @@ -4564,10 +4551,7 @@ function App(): React.JSX.Element { ? current.map((task) => task.id === event.childTaskId ? childTask : task ) - : [...current, childTask].slice( - 0, - maximumConversationSubagentActivities - ) + : [...current, childTask] }) } if (event.runtimeCallId) { @@ -4638,10 +4622,7 @@ function App(): React.JSX.Element { } if (index >= 0) { subagents[index] = subagent - } else if ( - subagents.length < - maximumConversationSubagentActivities - ) { + } else { subagents.push(subagent) } const runtimeCallId = event.runtimeCallId diff --git a/src/shared/assistant-contracts.test.ts b/src/shared/assistant-contracts.test.ts index ca93efed..c8661d75 100644 --- a/src/shared/assistant-contracts.test.ts +++ b/src/shared/assistant-contracts.test.ts @@ -4,6 +4,7 @@ import { builtInDefaultProjectSeedDescription, builtInDefaultProjectSeedName, conversationBranchInputSchema, + conversationMessageSchema, conversationSubagentActivitySchema, conversationSnapshotSchema, isUntouchedBuiltInDefaultProject, @@ -172,6 +173,52 @@ describe('project execution space contracts', () => { }) }) +describe('conversation activity contracts', () => { + it('accepts more than 100 tool and subagent activities', () => { + const tools = Array.from({ length: 101 }, (_, index) => ({ + callId: `call-${index + 1}`, + name: 'read', + state: 'completed' as const, + summary: `Tool ${index + 1}` + })) + const subagents = Array.from({ length: 101 }, (_, index) => ({ + childTaskId: `00000000-0000-4000-8000-${String(index + 1).padStart(12, '0')}`, + routingMode: 'native' as const, + actor: { + kind: 'direct-model' as const, + label: '编程 Subagent' as const + }, + state: 'completed' as const + })) + + expect( + conversationMessageSchema.parse({ + id: '00000000-0000-4000-8000-000000000999', + role: 'assistant', + content: '', + blocks: tools.map((tool, index) => ({ + id: `10000000-0000-4000-8000-${String(index + 1).padStart(12, '0')}`, + type: 'tool', + tool + })), + createdAt: 1, + state: 'complete', + tools, + subagents + }) + ).toMatchObject({ + tools: expect.arrayContaining([ + expect.objectContaining({ callId: 'call-101' }) + ]), + subagents: expect.arrayContaining([ + expect.objectContaining({ + childTaskId: '00000000-0000-4000-8000-000000000101' + }) + ]) + }) + }) +}) + describe('conversation branch contracts', () => { it('accepts bounded branch provenance and rejects renderer extras', () => { const sourceConversationId = diff --git a/src/shared/assistant-contracts.ts b/src/shared/assistant-contracts.ts index ffa59841..edefa951 100644 --- a/src/shared/assistant-contracts.ts +++ b/src/shared/assistant-contracts.ts @@ -182,9 +182,9 @@ export const conversationMessageBlockSchema = z.discriminatedUnion('type', [ .strict() ]) -export const conversationMessageBlocksSchema = z - .array(conversationMessageBlockSchema) - .max(500) +export const conversationMessageBlocksSchema = z.array( + conversationMessageBlockSchema +) export type ConversationToolActivity = z.infer< typeof conversationToolActivitySchema @@ -193,9 +193,6 @@ export type ConversationMessageBlock = z.infer< typeof conversationMessageBlockSchema > -export const maximumConversationToolActivities = 100 -export const maximumConversationSubagentActivities = 100 - export const subagentActorSchema = z .object({ kind: z.literal('direct-model'), @@ -277,14 +274,8 @@ export const conversationMessageSchema = z .array(conversationContextCompressionMarkerSchema) .max(2) .optional(), - tools: z - .array(conversationToolActivitySchema) - .max(maximumConversationToolActivities) - .optional(), - subagents: z - .array(conversationSubagentActivitySchema) - .max(maximumConversationSubagentActivities) - .optional(), + tools: z.array(conversationToolActivitySchema).optional(), + subagents: z.array(conversationSubagentActivitySchema).optional(), sources: z.array(z.string().max(8_192)).max(100).optional(), sourceReferences: z .array( diff --git a/src/shared/model-bridge-contracts.test.ts b/src/shared/model-bridge-contracts.test.ts index 014f0780..cb3239bd 100644 --- a/src/shared/model-bridge-contracts.test.ts +++ b/src/shared/model-bridge-contracts.test.ts @@ -191,14 +191,14 @@ describe('model bridge contracts and codec', () => { apiKey: 'must-not-cross' }) ).toThrow() - expect( + expect(() => modelBridgePolicySchema.parse({ ...policy, maximumOutputTokens: 65_537, maximumModelCalls: 8, maximumTotalOutputTokens: 32_768 }) - ).toMatchObject(policy) + ).toThrow() await expect( createModelBridgeRequestMessage({ identity, diff --git a/src/shared/model-bridge-contracts.ts b/src/shared/model-bridge-contracts.ts index 2c21e04f..af0230f8 100644 --- a/src/shared/model-bridge-contracts.ts +++ b/src/shared/model-bridge-contracts.ts @@ -126,8 +126,6 @@ export const agentPromptModelProfileSchema = z limits: z .object({ maximumOutputTokens: z.number().int().min(1).max(1_000_000), - maximumModelCalls: z.number().int().min(1).max(10_000), - maximumTotalOutputTokens: z.number().int().min(1).max(10_000_000), requestTimeoutMilliseconds: z .number() .int() @@ -158,16 +156,6 @@ export const agentPromptModelProfileSchema = z message: 'Model provider does not match its wire protocol' }) } - if ( - profile.limits.maximumOutputTokens > - profile.limits.maximumTotalOutputTokens - ) { - context.addIssue({ - code: 'custom', - path: ['limits', 'maximumOutputTokens'], - message: 'Per-call output limit exceeds the prompt total' - }) - } }) export type AgentPromptModelProfile = z.infer< typeof agentPromptModelProfileSchema @@ -178,11 +166,6 @@ export const modelBridgePolicySchema = z protocol: modelBridgeModelProtocolSchema, model: modelNameSchema, modelProfileDigest: sha256DigestSchema, - // Accepted only so persisted pre-removal bindings can be read and - // retired normally. Current code neither emits nor enforces these fields. - maximumOutputTokens: z.number().int().min(1).optional(), - maximumModelCalls: z.number().int().min(1).optional(), - maximumTotalOutputTokens: z.number().int().min(1).optional(), supportsImageInput: z.boolean() }) .strict() diff --git a/src/shared/model-call-operation-contracts.test.ts b/src/shared/model-call-operation-contracts.test.ts index 5b64e284..8d095634 100644 --- a/src/shared/model-call-operation-contracts.test.ts +++ b/src/shared/model-call-operation-contracts.test.ts @@ -116,20 +116,20 @@ describe('model call operation contracts', () => { ).toThrow() }) - it('accepts legacy quota fields without requiring them', () => { + it('rejects prompt-wide quota fields', () => { const request = { identity, requestDigest: `sha256:${'a'.repeat(64)}`, ...policy } expect(prepareModelCallSchema.parse(request)).toEqual(request) - expect( + expect(() => prepareModelCallSchema.parse({ ...request, maximumOutputTokens: 65_537, maximumModelCalls: 100_000, maximumTotalOutputTokens: 1_000_000_000 }) - ).toMatchObject(request) + ).toThrow() }) }) diff --git a/src/shared/model-call-operation-contracts.ts b/src/shared/model-call-operation-contracts.ts index 504b1eeb..583442f1 100644 --- a/src/shared/model-call-operation-contracts.ts +++ b/src/shared/model-call-operation-contracts.ts @@ -60,12 +60,7 @@ export const prepareModelCallSchema = z .object({ identity: modelCallIdentitySchema, requestDigest: modelCallDigestSchema, - modelProfileDigest: modelCallDigestSchema, - // Legacy ledger fields are accepted during migration but no longer - // constrain dispatch. - maximumOutputTokens: positiveCountSchema.optional(), - maximumModelCalls: positiveCountSchema.optional(), - maximumTotalOutputTokens: positiveCountSchema.optional() + modelProfileDigest: modelCallDigestSchema }) .strict() export type PrepareModelCall = z.infer @@ -164,9 +159,6 @@ export const modelCallRecordSchema = z identity: modelCallIdentitySchema, requestDigest: modelCallDigestSchema, modelProfileDigest: modelCallDigestSchema, - maximumOutputTokens: positiveCountSchema.optional(), - maximumModelCalls: positiveCountSchema.optional(), - maximumTotalOutputTokens: positiveCountSchema.optional(), status: modelCallStatusSchema, dispatchMetadata: modelCallDispatchMetadataSchema.optional(), terminalEvidence: modelCallTerminalEvidenceSchema.optional(),