Repository navigation
[Agent Core] 让上下文压缩请求遵守 Run 截止时间并有界清理后台任务 - #167
Merged
rojim666 merged 1 commit intoSep 22, 2026
Merged
Conversation
压缩摘要请求此前直接调用 provider.chat,不传 Run 剩余时间也没有外层时间边界, provider 默认 120 秒超时因此不受 Run deadline 约束:配置 10 秒的 Run 仍可能被 一次后台压缩拖到约 130 秒,循环也会阻塞在 wait_pending 而到不了自己的 deadline 分支。 - compact_messages 新增 remaining_s,入口与请求外层都以 Run 预算为界 - compact / compact_async 从 context.remaining_s() 取值,后台任务在真正发起 请求时读取而非在调用点读取 - wait_pending 新增有界等待,deadline 到达时取消后台请求并等其收尾 - deadline 截断不计入 compaction_failure_count,不因一次超时触发压缩熔断器 - 迟到摘要与清理期异常都归类为 deadline,不作为普通 LLM 失败安装或记账 - loop 顶部的压缩等待由 Run 剩余时间界定,不依赖 pending 任务自我界定 Refs rojim666#69 Signed-off-by: Ace <[email protected]>
rojim666
approved these changes
Sep 22, 2026
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.
Summary
关联 Issue #69。上下文压缩的摘要请求此前完全绕开了 PR-02 建立的 Run deadline 接缝:
Compactor._compact_chat()直接await provider.chat(...),不传remaining_s、不传
run_deadline_at、没有外层asyncio.timeout,因此 provider 默认的 120 秒超时不受 Run 预算约束。
后果与 Issue #69 标题描述的同构:配置
max_wall_clock_s=10的 Run 仍可能被一次后台压缩拖到约 130 秒;同时
AgentLoop每轮顶部的await wait_pending()没有时间边界,循环会阻塞在那里,到不了紧随其后的
wall_clock_exceeded()检查。本 PR 是 Issue #69 分阶段实现中的压缩阶段(PR-01 至 PR-05 已合并),只使用
Relates to #69,不在本 PR 合并时关闭父 Issue。Behavior
compact_messages()新增remaining_s;Run 已无剩余时间时,在创建 provider 协程之前就放弃本次压缩。
add_remaining_s()获得 Run 剩余时间,并由外层asyncio.timeout保证不会比 Run deadline 活得更久;有效预算为
min(provider timeout, run remaining)。compact()与compact_async()从context.remaining_s()取值;后台任务在真正发起请求时读取,而不是在
compact_async()调用点读取。wait_pending()新增remaining_s有界等待:deadline 在等待期间到达时取消后台摘要请求并等其收尾,不把无界等待留给调用方。
compaction_failure_count,不会因一次超时通过熔断器永久关闭本次 run 的自动压缩。
provider 在取消后的清理路径上抛出的异常也不会被记成普通 LLM 失败。
loop.py每轮顶部的压缩等待由 Run 剩余时间界定,不依赖 pending 压缩任务恰好自我界定。
/compact没有 run 上下文,行为不变(remaining_s保持None)。Validation
166 passed(test_compactor.py/test_loop.py/test_runner.py/test_session_manager.py/test_budget_admission.py/test_long_compaction.py/test_simulated_loop.py)。origin/main原样树上因
TimeoutError失败(run 挂住),在本分支通过。compactor.py+loop.py):Success: no issues found in 2 source files。compactor.py有 2 个既有告警(I001导入块格式、E501第 13 行105 字符),在
origin/main上逐一相同;按本 PR non-goals 未顺手清理。compileall:通过。git -c core.whitespace=cr-at-eol diff --check:通过。桌面端或构建配置。
Known limitations
tests/unit/test_loop.py::test_llm_request_is_cancelled_at_run_deadline在完整套件中会因浮点边界抖动失败(
0.05000000000291038 <= 0.05)。已核验:loop.py的改动位于if self._compactor is not None:内不会执行,compactor.py也不在其调用路径上;origin/main在同一完整套件下同样失败同一个测试(
156 passed / 1 failed);本分支为166 passed / 1 failed。runner.py收尾对非 success 终态仍不取消挂起压缩。这是 PR-05 之前就存在的行为,对 deadline 场景无害(该场景下压缩任务剩余预算≈0,入口 gate 直接放弃)。改变它会
让
max_steps/failed的 run 不再落盘压缩摘要,属行为扩张,留作独立改动。AgentLoop._chat的except asyncio.CancelledError分支未在压缩器内复刻:把一次外部取消转换为 deadline 信号只在外层 timeout 已到期时正确,而该路径无法构造
回归测试,故不加无测试代码。
Checklist
Signed-off-by(DCO)Relates
#69