Skip to content

feat(cli): context compaction in chat, resume and mission - #456

Merged
ajianaz merged 4 commits into
developfrom
feat/compaction-cli
Oct 28, 2026
Merged

ajianaz merged 4 commits into
developfrom
feat/compaction-cli

Conversation

@ajianaz

@ajianaz ajianaz commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

What

Part 2/2 of #440: wires the core compaction (#455) into chat, resume and mission, with the context window from an explicit setting or the provider's /models.

Closes #440.

Why

Long missions and chats hit the model's context window regardless of remaining budget; #455 added the mechanism, this decides when and against which window.

Changes

  • tole-cli/src/compaction_host.rs (new): Compactor::before_turn runs between settled turns. Window: TOLE_CONTEXT_WINDOW / config context_window when set (owner decision: an explicit setting wins over detection), else GET /models for the session's model (pinned fact/model wins, like the provider) — LAZY: only once a prompt passes 8k tokens, at most once per process (cached, also when none is reported). No window → off, said once. Due → compaction::compact; a failed summary call is reported and the session continues uncompacted. status_lines for tole status.
  • tole-core/src/openai.rs: context_window_of (reads context_length, context_window, max_context_length, max_model_len, max_input_tokens, top_provider.context_length) and fetch_context_window (same base URL / credentials as chat).
  • Settings: --compact-at <0..1] (validated), --no-compaction; config context_window, compact_at, no_compaction (range-checked, layered with the user config, shown by tole config check with sources); env TOLE_CONTEXT_WINDOW > config.
  • Hooks: chat (only in the settled branch before a new message), resume (before a new instruction; never before completing an unsettled turn), mission (after the budget checks, before each chained turn; the summary call's tokens count toward the budget).
  • tole status: one line per compaction.
  • Docs: README (env table), CHANGELOG, docs/epics.md (also corrects the feat(sessions): global session index + sessions --all #439 line: resume names the owning project's command), docs/threat-model.md (the /models lookup).

Testing

  • Unit: /models field parsing; Compactor — no lookup below 8k, detected window triggers once and is fetched once, the summary call does not re-trigger, explicit window wins, no window = off (looked up once), disabled = nothing; status lines; config range checks; settings resolution flag > env > config > default.
  • E2E (real binary, mock provider reporting 9 000 prompt tokens, SSE): run + resume ×2 compacts before the third turn with TOLE_CONTEXT_WINDOW=10000 and with the window from /models; the third request carries the fenced summary and turns 2–3 but not turn 1, while the session file keeps turn 1; --no-compaction, no window, and --compact-at 1.0 leave the wire whole; --compact-at 1.5 is refused.
  • cargo fmt, clippy (workspace all-targets, bare, shell-tools, mcp, tole-core bare), cargo test --workspace: green. Cora agent review (prepare + finalize): 0 findings, should_block: false.

…440)

tole_core::compaction: compact() summarizes the settled turns before the
kept tail with the session's provider and commits one `compaction`
entry together with the summary call's usage row (atomic, append-only;
the transcript is never rewritten). view() is what the provider sees:
the transcript as is, or the latest summary followed by the kept tail;
the turn loop now sends view(). due() only looks at calls made after the
last compaction, so the summary call never re-triggers it. Inactive
until the CLI wires a policy (follow-up PR).
Security review: a model-written summary of possibly-injected tool output
reached the wire as prose in the user's voice. The marking is now
structural: the summary goes inside the same untrusted-content fence as
tool results (forged fence markers neutralized), introduced as a record,
not instructions. The system role was not used: it would raise the
summary's authority.
chat, resume and mission compact between turns once a prompt reaches
--compact-at (default 0.8; config compact_at) of the context window:
TOLE_CONTEXT_WINDOW / config context_window, else the window the
provider's /models reports for the session's model, looked up lazily
(only past 8k prompt tokens, at most once per process). No window: off,
said once. A failed summary call is reported and the session continues
uncompacted. --no-compaction / config no_compaction turn it off;
tole status lists each compaction.
@ajianaz ajianaz added this to the 0.10.0 milestone Oct 28, 2026
@github-actions

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

@ajianaz
ajianaz merged commit 66f76d7 into develop Oct 28, 2026
16 checks passed
@ajianaz
ajianaz deleted the feat/compaction-cli branch October 28, 2026 03:48
@ajianaz ajianaz modified the milestones: 0.10.0, 0.9.0 Oct 28, 2026
@ajianaz ajianaz mentioned this pull request Oct 28, 2026
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.

feat(harness): context compaction for long-running missions

1 participant