Skip to content

feat(core): context compaction — summary entry, compacted wire view - #455

Merged
ajianaz merged 2 commits into
developfrom
feat/compaction-core
Oct 28, 2026
Merged

ajianaz merged 2 commits into
developfrom
feat/compaction-core

Conversation

@ajianaz

@ajianaz ajianaz commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

What

Part 1/2 of #440: the core mechanism for context compaction. Nothing triggers it yet; the CLI wiring (window resolution from /models with 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 last keep_turns turns, 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 ONE compaction entry — 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 against Policy { window, at (default 0.8), keep_turns } — the summary call (which saw the long transcript) never re-triggers it. history() for tole status.
  • tole-core/src/turn.rs: drive sends compaction::view(s.entries()) (borrowed, zero-cost, until a compaction exists).
  • Docs: CHANGELOG, docs/epics.md, docs/threat-model.md (atomicity, summary trust framing, residual risk).

Testing

  • Unit with a recording provider over real JSONL storage: below the threshold nothing changes (and one turn alone is never compacted); compaction summarizes turns 1–2 and keeps turn 3, commits exactly one entry plus its usage row (counted as a provider step), and the view is summary + turn 3; the summary call's usage does not re-trigger; a second compaction folds summary 1 + following turns and the view shows only summary 2; tool-call / empty / failed summary calls leave entries and usage untouched; a dangling from_entry keeps 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.

…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).
@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

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
ajianaz merged commit eeb3bb6 into develop Oct 28, 2026
16 checks passed
@ajianaz
ajianaz deleted the feat/compaction-core branch October 28, 2026 03:22
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]>
@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.

1 participant