Skip to content

fix(agent): auto-handoff when context length exceeds model limit#147

Merged
frostming merged 1 commit intobubbuild:mainfrom
iodone:fix/auto-handoff-context-overflow
Apr 7, 2026
Merged

fix(agent): auto-handoff when context length exceeds model limit#147
frostming merged 1 commit intobubbuild:mainfrom
iodone:fix/auto-handoff-context-overflow

Conversation

@iodone
Copy link
Copy Markdown
Contributor

@iodone iodone commented Apr 7, 2026

Changes

  • _agent_loop 中新增 context 超限自动恢复机制
  • 当 LLM API 返回 prompt-too-long 错误时,自动执行 tape.handoff 创建 anchor 截断历史,然后重试
  • 添加 _is_context_length_error 辅助函数,通过正则匹配常见的 context 超限错误模式
  • 限制最多自动 handoff 1 次,防止无限循环

Motivation

当 tape 历史超过模型 context window(如 202K tokens > 200K 上限)时,LLM API 直接返回 400 错误。系统提示中虽然指示 LLM 使用 tape.handoff 压缩上下文,但由于 LLM 根本未被调用,形成死锁。session 永久损坏,后续每条消息都会失败。

Testing

  • 所有 128 个现有测试通过
  • 正则匹配覆盖实际错误消息和常见变体

When tape history exceeds the model's context window, the LLM API
returns a 400 error. Since the model never gets called, it cannot
invoke tape.handoff to compress context — a deadlock.

Add automatic handoff recovery in _agent_loop: detect context-length
errors from the ToolAutoResult, perform tape.handoff to create an
anchor that truncates visible history, then retry with the original
prompt. Limited to 1 auto-retry to prevent infinite loops.
@frostming frostming merged commit 8930c44 into bubbuild:main Apr 7, 2026
5 checks passed
@frostming
Copy link
Copy Markdown
Collaborator

Thank you for the contribution. Let's use this as a fallback approach.

@iodone
Copy link
Copy Markdown
Contributor Author

iodone commented Apr 7, 2026

Thank you for the contribution. Let's use this as a fallback approach.

Is this an automatic review and reply from your bot?

@iodone iodone deleted the fix/auto-handoff-context-overflow branch April 7, 2026 02:32
@frostming
Copy link
Copy Markdown
Collaborator

Is this an automatic review and reply from your bot?

Human, our bot uses a dedicated account @dagebot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants