Repository navigation
fix(agent): 长任务压缩后不丢结论,重复调用能识别、循环能停下(#5) - #41
Merged
Merged
Conversation
… loops (#5) Long tasks restarted the analysis from scratch after a context compaction and could then loop for good (#5). Causes found in AgentLoop: Compaction - The handoff summary only saw the first 60,000 characters of the flattened record, so on a long segment the latest progress (the real next step) was never summarised. It now keeps the head and the tail. - Everything before the task anchor was dropped without being summarised. With "继续" as the anchor that is the task itself and the earlier conclusions. The summary input now has the user's messages verbatim, the conversation before the anchor and the previous checkpoint, and the user's earlier messages stay in the context after compaction (appended to the resume note). - After an automatic restart the engine's own resume note became the task anchor, so the next compaction dropped the previous turn's work and the user's task. The anchor is now carried in a per-run state and stays the user's message; the previous turn's work is summarised with the rest. - A truncated summary was thrown away and a failed one fell back to a mechanical record that had none of the earlier findings. A truncated summary is kept and completed, and the mechanical record carries the previous checkpoint without its outdated next step. Its parts are separated by headings so LedgerBackend.mergeHandoff does not file narration under the last "rejected" section. Repeats and loops - The result fingerprint read count/requests/... from the top of the envelope, but tool results live under `data`, so every successful call had the fingerprint "ok": paged fs_read calls (arguments differing only in digits share a call signature) were refused from the 11th call as "same arguments, same result". It is now a hash of the returned data. - A call made before with identical arguments and an identical result is flagged in its tool result. When 8 of the last 12 tracked calls are such repeats the model is warned once; if the next window is the same the turn stops with stopReason "loop" and AgentSession does not restart it. The state is shared by all turns of a run, so it survives compaction and automatic restarts. Failures reported in the tool's return value - Many tools report failure by returning { ok: false, ... } (run_node with a non-zero exit code, net_get for an unknown request). The envelope is still ok: true, so the same-error and consecutive-failure hints never fired for them and a failed write counted as progress. They now count as failures; the no-progress check reads `data` as well. dev/selftest-loop-guard.mjs covers each of these, including AgentSession sharing the run state across automatic restarts. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Unreleased section: what changed in context compaction, the repeated-call and loop detection, the repeat fuse no longer refusing paged reads, and tool results that report ok:false counting as failures. One line in the Agent feature list. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…es (#5) Loop detection - Only repeats of read-only queries count as redoing the analysis. Repeating a mutating tool with the same receipt (trace start/stop, clear site data, click, run a script) is neither a repeat nor new work, so a sampling workflow that toggles a tracer for every sample is no longer warned and stopped. - A call refused by the repeat breaker counts as a repeat only if that exact call was made before. The breaker matches on digit-normalised arguments, so calls that had never been made were reported as identical repeats and the run was stopped. The refusal text now says which case it is. - The result fingerprint ignores `ref` and `scannedAt`, which change on every page_snapshot / page_find / page_automation_scan call. A return value with ok:false and an error text is fingerprinted like a thrown error. Call signatures no longer depend on argument key order. - The loop reminder is carried across a compaction, expires after 36 counted calls and no longer latches the no-progress reminder. A text-only report after the reminder ends the turn instead of being nudged as drift. An abort during the final report request ends the turn as aborted. - The error signature of process output is taken from the last error line, not from the first line that looks like one (a log line, the source line node echoes, the raise line of a python traceback). A truncated envelope keeps ok:false. Compaction - The persistent ledger receives only what the model wrote in this handoff, complete lines only. Mechanical records, the carried ledger and quoted conversation are not merged. LedgerBackend.mergeHandoff is additive: it never replaces a stored row with an equal or shorter one, and it limits new rows per kind instead of taking the first 40 bullets. - The mechanical checkpoint carries the last complete ledger, the last truncated one and the previous segment records from the run state, clipped at line boundaries, instead of nesting the whole previous checkpoint. Quoted conversation is block-quoted. The stale "next step" section is recognised with numbering, emphasis, emoji, no space after #, CRLF; code fences are skipped. - Only the latest four user messages before the task are kept as notes, labelled as reference; a message whose task was stopped by the user is marked. - An abort during the handoff request ends the turn without compacting. Other - An auto-restarted turn resends the previous turn's messages unchanged and attaches a changed runtime context to the resume note, so the provider prefix cache still matches. - Text is never cut inside a surrogate pair; LlmClient replaces lone surrogates when it serialises the request body. - Stop notices are returned as `notice`; AgentSession appends them to the step list, which is what the sidebar renders. Tests: selftest-loop-guard rewritten (143 checks), shared AgentSession harness, one more check each in selftest-toolrouter and selftest-panel-live. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
… breaker signer_trace, closure_read, debug_breakpoint, hook_inject and whitebox_diff are used as start -> trigger -> query -> stop per sample; their start/stop receipts are identical every time, so within one turn the 11th start was refused by the repeat breaker as "same arguments, same result". The breaker is meant for read-only queries that keep returning the same thing, not for switches. `repeatExempt` in the tool declaration now also accepts a list of action names: the call is exempt only when args.action is one of them. The five tracer tools declare ["start", "stop"]; cookies declares ["set", "remove"]; clear_site_data (a per-sample reset) is exempt entirely. Their query/list actions stay under the breaker. ToolRouter.isRepeatExempt(name, args) does the check; AgentLoop passes the parsed arguments. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
#5:复杂分析跑着跑着,像是忘了之前的结论和既定步骤,从头开始分析,最后陷进死循环。
读
AgentLoop查到的原因分三类,每一条都在旧代码上复现过:run_node退出码非0、net_get没找到请求这类情况返回{ ok:false, … },信封还是ok:true,“同类错误”“连续失败”两条提示对它们不起作用。改动(按提交)
第一轮
fix(agent)压缩后不丢结论,重复调用能识别、循环能停下(5b78083)createRunState),AgentSession每次run建一个,传给每个回合:自动续跑的回合靠它认出用户发的那条任务,循环检测的记录跨压缩、跨续跑。stopReason为loop,不再自动续跑)。ok:false和返回值里ok:false都算失败。docs(edfa410):readme条目。第二轮:独立审查之后的修正
第一轮开出pr后做了一次独立代码审查,15条发现里12条复现确认,多数是第一轮改动引入的。下面逐条修了,修法都有对应的自测。
fix(agent)审查发现的修正(a7dc211)signer_trace/hook_inject的start和stop、clear_site_data、cookies这类改动型工具每次回执都一样,按工具说明“start→触发→query→stop”采12组样本,会在第20次调用后被提醒、第36次调用后被停掉。现在只有只读工具(工具表里声明READ_ONLY的)的重复才算;改动型工具用同样的参数再做一次、回执也一样,既不算重复也不算新进展。page_snapshot和page_find每次扫描都给元素重新发ref,page_automation_scan带scannedAt。第一轮按整个返回值算指纹,页面一点没变时这几个工具的结果也永远“不一样”,重复熔断、重复提示、循环检测、无进展提醒对它们全部失效。现在算指纹时不看这两个字段。ok:false并带错误文本的失败,和抛错一样按归一化的错误文本算指纹。run_node/run_python的输出里,真正的报错在最后;从头取第一条“像报错”的行,会取到脚本自己打印的日志、node回显的throw new Error("…")那一行、python回溯里的raise行,互不相干的失败得到同一个签名,第3次起每次都被提示“撞同一类错误”。现在进程输出从后往前找。ToolRouter截断时,信封里保留ok:false,输出很大的失败照样计入。LedgerBackend.mergeHandoff。存档里有锚点之前的对话原文:上一条回复里列着“### 已否决 / - 方向A / - 方向B”让用户选,用户选了b,摘要一失败,“方向B”就被记成已否决,写进按工作目录持久化的账本。存档里还有被截过的旧账本:半句会把库里完整的那条顶掉。现在只把模型这一次写出来的账本(完整的行)交给账本库,机械记录、带过来的旧账本、引用的对话都不交;引用的对话每行加>。mergeHandoff改成只增不减:库里已有相同的、或更完整的一条就跳过,不删也不重插。事实和已否决假设各自限量(每类一次最多新写入40条)。以前自上而下只取前40条,账本是逐次累积的,过了40条后新结论和排在后面的已否决假设进不了库。#后没空格、crlf换行都认得出,代码块里以#开头的注释不算标题。\ud83d转义,校验严格的json解析器会拒收。现在截断不切开代理对;LlmClient序列化请求体时把残留的孤立代理项换成U+FFFD(main上工具结果折叠等几处截断也有同样的问题,一并盖住)。content里,而侧栏只要有步骤记录就只渲染步骤,用户看不到。现在各个“引擎主动停下”的出口都另外返回notice,AgentSession收尾时把它追加成步骤里的一段正文。main上原有的几条说明(连续只输出文字、到续跑上限等)同样看不到,一并修了。dev/selftest-loop-guard.mjs重写,143项;AgentSession的加载器抽成dev/agent-session-harness.mjs,selftest-global-system-prompt改用它;selftest-toolrouter、selftest-panel-live各加一项。docs(469a177):readme“main(尚未发布)”改成和实际行为一致。第三轮:开关型工具的重复熔断
fix(agent)开关动作豁免重复熔断(3b27ba0)signer_trace、closure_read、debug_breakpoint、hook_inject、whitebox_diff按工具说明是“start→触发→query→stop”每组样本来一遍,start和stop的回执每次都一样,同一回合第11次start会被重复熔断按“同参数同结果”拒绝执行(v0.27.1就有,上面真实工具表的复现里main被拒6次)。repeatExempt除了true,现在可以写action列表,只有args.action在列表里的调用豁免。五个开关型工具声明["start","stop"],cookies声明["set","remove"],clear_site_data(每组样本重置一次)整个豁免;它们的query/list照旧按空转熔断。ToolRouter.isRepeatExempt(name, args)做判断,AgentLoop把解析后的参数传进去。selftest-tool-metadata加4项、selftest-toolrouter加2项、selftest-loop-guard加2项(同一回合12组样本48次调用全部执行,反复query同一个空结果照旧第11次被拒);审查时留下的真实工具表复现脚本重跑,12组样本被拒次数从6(main)变成0。行为变化
page_snapshot:第2次起有重复提示,第11次被拒绝执行(和v0.27.1一样),另外会触发循环提醒。验证
bash scripts/selftest-agent-tools.sh通过,共48步。CodeBackend/页面扫描代码、真实sqlite)在修复后的代码上重跑,结论都翻转了:采12组样本全部采完、没有循环事件;库里完整的结论不再被半句顶掉;对话里的列表不再入库;46条事实+12条已否决的账本全部交给了账本库(以前只取前40条);60种截断位置没有孤立代理项。AgentSession、工具、页面和记忆库,47项全部通过:run_node真实输出:同一类报错第3次提示换路线;之后3次各不相同的报错(node都回显了throw那一行)不被说成同一类;hook_injectstart→页面发请求→query→stop,9组全部采完,9次start的回执逐字相同,没有重复提示、没有提醒;page_snapshot:每次的ref都不一样,第2次起提示重复,第11次被拒绝;progress.md是机械存档,对话作为引用带着,记忆库里一条都没有;ref/scannedAt上不同,没有别的易变字段。scripts/verify-page-interaction.mjs,78项通过,退出码0。LedgerBackend,三方合并没有冲突;合并后的文件上,账本关闭14项、账本sql 20项、循环自测143项都通过,真实引擎47项通过,浏览器0.2秒正常退出。已知问题(本pr未处理)
net_list,每次结果都确实不同,不算重复,也没有别的保护(v0.27.1上第11次起会被拒,但那是因为它把所有结果都当成相同)。ok显示,返回ok:false的调用在界面上还是成功的样式。🤖 Generated with Claude Code