Repository navigation
feat(core): context compaction — summary entry, compacted wire view - #455
Merged
Merged
Conversation
…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).
🔍 Cora AI Code Review✅ No issues found. Code looks good! Review powered by cora-code · BYOK · MIT |
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.
ajianaz
added a commit
that referenced
this pull request
Oct 28, 2026
## 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 #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`. --------- Co-authored-by: ajianaz <[email protected]>
Merged
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.
What
Part 1/2 of #440: the core mechanism for context compaction. Nothing triggers it yet; the CLI wiring (window resolution from
/modelswith an explicit-setting override,--compact-at,--no-compaction, hooks in mission/chat/resume,tole status) follows in part 2.Refs #440 (closed by part 2).
Why
Long missions hit the model's context window regardless of remaining budget; the whole transcript went to the provider on every step.
Changes
tole-core/src/compaction.rs(new):compact(storage, provider, keep_turns): summarizes everything before the kept tail (the lastkeep_turnsturns, cut at a turn start so no tool result is separated from its call) with the session's own provider and a fixed instruction, then commits ONEcompactionentry —summary,from_entry(first kept entry),compacted_entries,prompt_tokens_before— with the summary call's usage row in the same commit (explicit entry id → one line, atomic). A failed / non-text / tool-calling summary call leaves the session unchanged. Never rewrites the transcript.view(entries): what the provider sees — borrowed transcript when nothing was compacted; otherwise the latest summary as one user message (framed as a record, not instructions, keeping tool-sourced content untrusted) followed by the kept tail. A compaction pointing at an unknown entry is ignored (full transcript).due(policy, storage)/last_prompt_tokens: the last provider call AFTER the last compaction againstPolicy { window, at (default 0.8), keep_turns }— the summary call (which saw the long transcript) never re-triggers it.history()fortole status.tole-core/src/turn.rs:drivesendscompaction::view(s.entries())(borrowed, zero-cost, until a compaction exists).docs/epics.md,docs/threat-model.md(atomicity, summary trust framing, residual risk).Testing
from_entrykeeps the full wire; the turn loop sends the compacted view on the next turn.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.