Skip to content

fix(context): preserve tool exchanges during compaction - #72

Merged
ignorejjj merged 1 commit into
mainfrom
fix/70-compaction-tool-boundaries
Nov 3, 2026
Merged

ignorejjj merged 1 commit into
mainfrom
fix/70-compaction-tool-boundaries

Conversation

@ignorejjj

Copy link
Copy Markdown
Member

Summary / 概述

When a dashboard note or background-task notification changes message boundaries, context compaction can keep a tool result while summarizing away its call. Subsequent requests then fail with HTTP 400.

Use a shared safe-cut helper that tracks pending tool-call IDs and cuts only after completed exchanges. Apply it to normal summarization, failed-summary fallback, and emergency truncation. Support normalized tool blocks and native Responses function calls, preserving complete message content. Emergency truncation also preserves only a real summary prefix and avoids overlapping prefix/tail duplication.

Linked issue / 关联 issue

Closes #70

Type of change / 改动类型

  • 🐞 Bug fix / 缺陷修复 (fix)
  • ✅ Tests / 测试 (test)

How was this tested? / 如何验证?

  • Added 28 regression cases covering all three truncation paths, parallel calls, split results, interleaved notes, overlapping summary prefixes, small retention counts, and OpenAI request conversion. All 28 fail with the original context implementation and pass with the fix.
  • Agent integration tests use the real Anthropic SDK with mocked HTTP responses and cover both dashboard messages and background notifications. The issue's reproduction now finishes normally after compaction.
  • .venv/bin/pytest --tb=short: 544 passed, 3 skipped.
  • .venv/bin/ruff check src/ tests/: passed.
  • Mypy remains advisory: 238 existing errors in 33 files, with unchanged diagnostics apart from shifted line numbers.
  • git diff --check: passed.

Checklist / 检查清单

  • PR title follows Conventional Commits.
  • The change is focused and reasonably small.
  • Relevant tests and the full suite pass locally.
  • Documentation reviewed: this restores expected compaction behavior without introducing configuration or usage changes; no documentation update needed.
  • No secrets, API keys, or tokens are included in the diff.

@ignorejjj
ignorejjj merged commit a633f56 into main Nov 3, 2026
7 checks passed
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.

[Bug]: Context compaction can keep a tool_result without its tool_use, run then fails with 400

1 participant