Skip to content

fix(acp): trigger automatic compaction from provider context usage - #64

Open
yan-ad wants to merge 2 commits into
Blankeos:mainfrom
yan-ad:fix/acp-auto-compaction
Open

yan-ad wants to merge 2 commits into
Blankeos:mainfrom
yan-ad:fix/acp-auto-compaction

Conversation

@yan-ad

@yan-ad yan-ad commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix ACP automatic compaction using the context usage reported by the provider, rather than only the persisted transcript estimate. Sessions could show usage beyond the configured threshold while never automatically compacting; /compact still worked because it bypasses that threshold gate.

  • Count input, cached input, output, and context added after each provider usage observation; never sum repeated tool-step input billing into a context size.
  • Include the composed system prompt and pending user message before checking the threshold. Record the previous prompt estimate on usage parts so subsequent agent/prompt changes can replace it without double-counting.
  • Ignore pre-compaction usage and retained-tail billing as context measurements. Preserve the last valid provider input measurement when newer usage omits input tokens.
  • Notify ACP clients when automatic compaction succeeds and send refreshed context usage/cumulative cost; keep compaction summary billing in subsequent session totals without double-counting completed turns.
  • Filter archived history before inserting the fresh system prompt so it survives conversion after compaction.
  • Add regression coverage and a local mock-provider stdio test exercising automatic compaction and a second turn. Correct the existing command-dispatch test's offline model fixture so it does not require Ollama.

ACP versus TUI context and credit analysis

ACP does not have a separate larger model context limit or credit allowance. Both paths use the model's configured/discovered limits, the shared compaction threshold (context_window - reserved), and the same provider/tool loop.

Before this fix:

  • ACP usage notifications used provider observations, but its compaction gate used only transcript tokens, excluding request overhead. Large provider prompts could cross the threshold without triggering automatic compaction.
  • TUI includes MCP tool-prefix estimates in its threshold and usage display; ACP's gate did not include those estimates or its composed system prompt.
  • Auto-compaction changed persisted context silently, leaving ACP clients displaying pre-compaction usage. Summary billing also disappeared from the next prompt's cumulative cost base.

Delayed compaction can increase billed input because each model/tool request resends a larger context; cached input still occupies the context window and can carry provider-dependent charges. Cumulative billed usage is not the same thing as current context size. This change does not claim measured cost savings or change provider pricing.

Remaining differences outside this focused fix: TUI scopes prompt tool guidance to the active agent, whereas ACP composes it from the full registry. Both paths still check automatic compaction at turn boundaries, not midway through a running tool loop. Auto-compaction still requires known model limits and a sufficiently large compactable head, and compaction failures remain logged rather than making the prompt fail.

Validation

  • cargo test acp:: --bin crabcode: 75 passed.
  • cargo test session::compaction:: --bin crabcode: 21 passed.
  • cargo test --test acp_stdio: 8 passed, including the local mock-provider regression (also run repeatedly on its own).
  • cargo fmt --all -- --check and git diff --check: passed.
  • cargo test --bin crabcode -- --test-threads=1: 1,850 passed, 3 ignored, 1 unrelated existing failure: ui::components::chat::tests::test_edit_tool_renders_codex_style_diff_summary expects a basename from a hardcoded /Users/carlo/Desktop/Projects/crabcode path on another checkout. The parallel full-unit run additionally exposes existing catalog/Ollama shared-state test failures.

Built the required remote-client assets using the existing remote-client-build recipe's Bun commands before Rust validation; no dependency or generated-asset changes are included.

Follow-up: compaction mode tooltip

A configured visible compaction agent without a description now advertises Compaction agent. Summarizes conversation context for continued work. in both legacy availableModes and modern mode configOptions. Explicit descriptions remain unchanged. This does not add a built-in selectable compaction agent, expose hidden/subagents, or change how automatic compaction runs.

  • Added coverage for blank/whitespace descriptions, custom text, existing build descriptions, and hidden/subagent filtering.
  • Verified serialized descriptions over ACP stdio.
  • Updated validation: 76 ACP tests and 8 stdio tests pass; formatting and whitespace checks pass.

@yan-ad

yan-ad commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Added the configured compaction-mode tooltip in 4a4f25a, with custom descriptions preserved and both legacy modes and modern config options covered. Refactoring is separate in #67 and based on this PR. Both changes are synced to yan-ad/crabcode:yan-flavor at 62a9a4f; flavor validation passes (61 ACP, 21 compaction, 4 stdio tests, with five repeated parallel stdio runs). Existing flavor lifecycle behavior and concurrent install/upgrade commits are preserved.

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.

1 participant