Repository navigation
fix(agent-core-v2): truncate foreground output in bash detach acknowledgement - #5218
Conversation
…edgement - cap the model-visible foreground_output in backgroundStartedResult at DEFAULT_TOOL_RESULT_MAX_CHARS for both auto-timeout and manual detach - append a truncation notice with the produced total, a still-running hint, and a TaskOutput pointer when task tools are active - update the noisy-detach test to the new contract and add coverage for manual detach, auto-timeout detach, the exact cap boundary, and disabled task tools
🦋 Changeset detectedLatest commit: 11fb540 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 11fb5409e0
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const foregroundResult = builder.ok(''); | ||
| const foregroundOutput = foregroundResult.output.length > 0 ? foregroundResult.output : ''; | ||
| const retainedOutput = foregroundResult.output.length > 0 ? foregroundResult.output : ''; | ||
| const foregroundOutput = sliceOnCodePointBoundary(retainedOutput, DEFAULT_TOOL_RESULT_MAX_CHARS); |
There was a problem hiding this comment.
Keep the acknowledgement below the result cap
When retained foreground output reaches 50,000 characters, only the payload is capped; the task metadata, foreground_output: label, and truncation notice are then appended outside that budget. The exact-boundary case is also considered untruncated even though the complete acknowledgement already exceeds DEFAULT_TOOL_RESULT_MAX_CHARS. Because detach acknowledgements bypass truncateForModel(), this still violates the intended cap and can overflow a context near its limit; reserve space for the serialized scaffolding or cap the completed acknowledgement.
Useful? React with 👍 / 👎.
| const fullOutputHint = this.allowBackground() | ||
| ? `use TaskOutput(task_id="${taskId}") to read the full output, including output produced after this message` | ||
| : 'you will be automatically notified when it completes'; |
There was a problem hiding this comment.
Avoid contradicting the TaskOutput instruction
When a foreground command is detached with more than 50,000 characters and task tools are enabled, this notice tells the model to call TaskOutput, while the metadata in the same acknowledgement says do NOT wait, poll, or call TaskOutput on it. The conflicting instructions make recovery of the omitted output ambiguous and may cause the model to poll despite the intended continuation behavior; align this hint with nextStepLines() or state when TaskOutput may be used.
Useful? React with 👍 / 👎.
CI
|
…edgement (#5239) * refactor(core): run loop turns on an independent state machine (#5211) * refactor(core): run loop turns on an independent state machine - add an actor runtime for loop and turn execution - unify turn hooks and asynchronous lifecycle settlement - simplify loop contracts and cover turn lifecycle behavior * fix(core): park recorded notifications until intake * fix(core): settle forced aborts past stuck hooks and tools * docs(core): document loop emitted events and public API * fix(agent-core-v2): skip onDidFinishStep hooks for aborted steps * refactor(llm): legalize deferred tools in the resolver (#5142) * refactor(core): unify llm event stream and harden behavior tests - unify run boundaries, request summaries, delta events, retries, and outcomes - migrate Loop, agent-core-v2, and klient consumers to the unified event vocabulary - add aimock-backed transport coverage and restore historical LLM behavior tests - update the Nix dependency hash and affected snapshots * refactor(llm): legalize deferred tools in the resolver - strip deferred metadata during legalize for models without dynamic loading - stop convert from dropping deferred tools and dedupe declarations with top-level priority - merge protocol capabilities once at the encode pipeline entry - remove deferred tools in the dynamic loop resolver and announce their names first * refactor(llm): unify deferred tool request views - remove dynamic-only request branching from ToolSelect resolution - keep non-dynamic system schemas out of top-level tools - preserve baseline tools for side requests before capability shaping * refactor(llm): share the tool select request view - reuse deferred shaping in the resolver and request preview - preserve initial tool announcements in request previews - cover preview parity with deferred tools * refactor(llm): strip deferred metadata in tool select Remove deferred metadata from non-disclosure ToolSelect request views while preserving the loop baseline. Keep core legalization as the fallback for requests that bypass ToolSelect. * refactor(llm): contain tool disclosure in resolver * refactor(llm): hash the full request tool list - compute toolsHash over every input tool, including deferred ones - note the resolver and legalization timing on the llm run events * refactor(llm): remove the tool select experimental flag - drop the tool-select flag, env var, and [experimental] config entry - gate deferred tool disclosure solely on model capabilities - remove the desktop forced env and update the MCP and env-var docs - cover mid-loop active-tool requests with an empirical loop test * test(desktop): assert browser tool disclosure in the request view The previous assertions called loadableToolsAnnouncement() with no arguments, a calling form no production code uses; the model-facing disclosure is built per request by the ToolSelect resolver. Assert the request view instead: requestTools strips the deferred browser tool from the request tool list, shapeHistory injects the snapshot announcement naming it, and a reminder step after the control switch turns the removal into a <tools_removed> delta. * fix(core): recover compaction from empty text history (#5214) * test(core): cover empty text request recovery * fix(core): clean empty text before request encoding * chore: add empty text recovery changesets * test(core): cover cross-provider signed empty text * fix(core): drop unsupported signed blank text * fix(agent-core-v2): truncate foreground output in bash detach acknowledgement (#5218)
Requirement or Bug
前台 Bash 命令超时自动转后台、或被用户手动转到后台时,detach acknowledgement 会把截至转后台时刻积累的全部输出(上限 1,000 万字符)原样带进模型上下文;输出较大时,后续请求直接超过 1,048,576 token 上限,会话无法继续。本 PR 让 acknowledgement 中的前台输出遵守既有的 50,000 字符工具结果上限,完整输出仍可通过任务输出日志和 TaskOutput 读取。
Bug Reproduction Steps
Root Cause
BashTool.backgroundStartedResult()把ToolOutputAccumulator截至 detach 时刻的全部 retained 输出原样拼进 acknowledgement 的foreground_output:段;自动超时与手动转后台两条路径共用这一拼接。detach 通道(legacy adapter 的detach/onDetach→ coreCallControl.detach)不经过正常完成路径上的finalize()→truncateForModel(),50,000 字符截断因此被绕过。这是根本修复:在拼接处同步截断模型可见内容。截断刻意不做落盘——detach 不冻结
ctx.write的输出通道,全量输出(含转后台后新增部分)本就持续写入任务输出日志;任务在后台结束时,既有addOutputReference+truncateForModel已用同一日志指针统一收口,模型收到的完成通知只带引用、不内联输出。detach 时再写 spill 文件只会重复落盘。Code Changes
改动集中在一个函数(自动超时与手动转后台共用,一处生效):
run_in_background=true直接后台启动的场景 accumulator 为空,行为不变;前台正常结束路径完全不动。truncateForModel的「已保存到文件」提示刻意区分,明确任务仍在运行,避免模型误以为任务已结束。addOutputReference的<=边界一致。测试(
packages/agent-core-v2/test/os/backends/node-local/tools/bash.test.ts):keeps task metadata independent when noisy foreground output is detached钉住的是旧的(有 bug 的)全量输出行为,改写为新契约truncates noisy foreground output in the detach acknowledgement while keeping metadata and the full task log,并补「完整输出经任务输出日志可读」断言。Behavior Changes and Affected Users
foreground_output受影响模块:agent-core-v2 Bash 工具。CLI(TUI 后台任务)、desktop/web(内嵌 kap-server 的同一工具实现)行为同步变化。
测试覆盖:表中第一行由「手动 detach 大输出」「自动超时 detach 大输出」两个用例钉住,边界由「恰好 50,000 字符不截断」钉住,第二行由「任务工具禁用措辞」用例钉住;小输出不回归由既有 detach 用例(
can detach a foreground command through the background service等断言 acknowledgement 原有内容不变)保证。验证(macOS 本地,改动仅涉及字符串截断,不触及路径/子进程/终端语义):
tsc -p packages/agent-core-v2/tsconfig.json --noEmit通过。test/tool/tool.test.ts子代理 print 模式 deadline 用例的 30s 超时,与本改动无关(其中一例已在基线提交 38f4cf6 上复现同样失败)。未验证项与环境说明
bash-background.spec.ts断言小输出(2 行)acknowledgement,不受截断影响,未在本地运行 desktop e2e。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.