aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / docs / plans / compaction / INVENTORY-397.md
| 1 | # Context compaction hardening (aien-sovereign-core#397): inventory and test matrix |
| 2 | |
| 3 | Owner: campaign session 1ea888, 2026-10-10. Branch `fix/context-compaction-safety-397`. |
| 4 | Motus (PyPI `lithosai-motus` 0.4.3, `memory/compaction_*.py`) was read for ideas only: keep tool call and |
| 5 | result together, store originals before summarizing, mark summaries as derived. No Python enters this repo. |
| 6 | |
| 7 | ## 1. Entry points and state ownership (verified by reading the code) |
| 8 | |
| 9 | | Site | What it does | Compaction? | |
| 10 | |---|---|---| |
| 11 | | `crates/aien-cli/src/main.rs`, interactive loop (`loop {` after the banner), after each user turn and after each assistant turn | the only production caller of `ContextCompactor::compact_if_needed`; the message list is a `Vec<serde_json::Value>` of `{role, content}`; the whole list is written to `basecamp/sessions/<session_id>.json` after every turn | yes, this PR | |
| 12 | | `crates/aien-cli/src/main.rs`, single-shot (`session-single-*`) and `auto-goal-*` modes | build their own short message lists; never call the compactor | no | |
| 13 | | `crates/aien-cli/src/hooks.rs` `AgentHook::on_compaction` / `run_on_compaction` | hook slot; no hook implements it and nothing calls `run_on_compaction` | no (unchanged) | |
| 14 | | `crates/aien-cli/src/client.rs` `stream_turn` | sends `role` and `content` only; extra fields on a message (`derived`, `authority`) are ignored by the wire | reads the list | |
| 15 | | `crates/aien-cli/src/client.rs` `extract_tool_calls` | the tool-call grammar (`<tool_call>` JSON, or a fenced JSON block); reused by the compactor to recognise an assistant tool request | shared | |
| 16 | | `crates/aien-runtime/src/context.rs` `ContextComposer`, `ContextRevision` | immutable tokenized revisions for the scheduler; a different object (token ids, not chat messages); not touched | no | |
| 17 | | compose daemon (`crates/aien-runtime/src/spine.rs`) | its own Cortex journal and record marks; not a chat transcript | no | |
| 18 | | `aegis-runtime/src/agent.rs` | read for the issue's "parallel compaction" question: at `aegis-runtime` main on 2026-10-10 there is no compaction routine there (grep `compact` finds nothing); AEGIS is out of release v1 (Drake, aien-architecture#190) | no | |
| 19 | |
| 20 | Tool results in this loop are `role: user` messages whose content starts with `<tool_response name="...">` |
| 21 | (the tokenizer refuses a `tool` role). The compose daemon's approval waits never pass through this loop today; |
| 22 | the approval marker the compactor protects (a tool result whose parsed JSON body has `status` equal to |
| 23 | `approval_pending`, `requires_approval` or `pending`) is a documented convention for the day they do. The |
| 24 | words in prose, in an assistant message or in any other field do not count. |
| 25 | |
| 26 | ## 2. What changed (`crates/aien-cli/src/compaction.rs`) |
| 27 | |
| 28 | - Tool-aware groups: an assistant tool request plus its following tool results is one group; groups are |
| 29 | pruned or recapped whole. A tool result with no request before it (detached) or a historical request with |
| 30 | missing results (malformed) refuses the whole compaction with a named reason. The last two groups and |
| 31 | everything from an approval wait onward are protected. |
| 32 | - Durable originals first: a `CompactionRecord` (every replaced message with its digest, the removed range |
| 33 | with its originals, transcript digests before and after, `after_len`) is written to |
| 34 | `basecamp/sessions/<session_id>-compaction/NNNNNN-<utc>.json` (dir 0o700, file 0o600, write to temp then |
| 35 | rename) before the list changes. Write failure leaves the list untouched and is reported. `reconstruct` |
| 36 | undoes one record and `CompactionStore::replay` undoes all of them newest first, verifying every digest; |
| 37 | the current transcript may have grown after the compaction. |
| 38 | - Tokens: `TokenCounter` trait; `TokenizerCounter` from `AIEN_TOKENIZER_JSON` (model tokenizer, falls back |
| 39 | per text on error); otherwise `CharEstimate` (one token per three bytes, rounded up, tested conservative). |
| 40 | Non-string content counts from its JSON serialization; four tokens of overhead per message; configurable |
| 41 | tool-schema overhead added once. |
| 42 | - Oversized single tool results (default over 24,000 bytes) are head and tail truncated wherever they are, |
| 43 | with the original in the record. |
| 44 | - Trust: the recap is `role: user` for wire compatibility but tagged `derived="true" authority="none"` with |
| 45 | `"derived": true` on the message; its lines have `<`, `>` and control characters removed, so a tool result |
| 46 | cannot open a `<system>` or close a `<tool_response>` through the recap. Recap lines and stubs pass through |
| 47 | the configured redactor (`vault::redact_secrets` in the chat loop). |
| 48 | - Statistics: `CompactionReport { applied, skipped: Option<SkipReason>, stats }` with token source, pruned |
| 49 | and oversized counts, saved bytes, removed segments, protected groups, recovery pointer and digest. Skips |
| 50 | are printed in the chat loop (except NotNeeded and TooShort), never silent. |
| 51 | |
| 52 | ## 3. Test matrix (`cargo test -p aien-cli compaction`) |
| 53 | |
| 54 | | Acceptance point (issue) | Before (main, 1 test) | After (24 tests) | |
| 55 | |---|---|---| |
| 56 | | tool request + results retained or compacted as one group | not tested | `tool_group_is_atomic_and_originals_reconstruct` | |
| 57 | | pending tool call protected | not tested | `pending_tool_call_in_tail_is_protected` | |
| 58 | | interrupted or detached tool response | not tested (would be recapped) | `detached_tool_result_refuses` | |
| 59 | | multi-tool turn with missing result | not tested | `historical_incomplete_group_refuses` | |
| 60 | | approval-waiting trace stays intact | not tested | `approval_wait_protects_everything_after_it` | |
| 61 | | original bytes reconstructable, digests verify | impossible (originals discarded) | `tool_group_is_atomic_and_originals_reconstruct`, `oversized_result_in_tail_is_truncated_and_recoverable` | |
| 62 | | compaction refuses without durable record | no | `no_store_refuses_lossy_compaction`, `store_write_failure_leaves_list_untouched` | |
| 63 | | restart and replay reproduce the transcript | no | `replay_after_restart_rebuilds_two_compactions` | |
| 64 | | hostile tool text cannot forge authority | no | `hostile_tool_text_cannot_forge_authority_in_recap`, `hostile_user_and_assistant_text_is_neutralised_in_recap`, `sanitize_line_strips_markup_control_and_length`, `approval_marker_counts_only_as_a_parsed_tool_result_status` | |
| 65 | | tampering detected | no | `tampered_record_is_rejected`, `recap_naming_another_transcript_is_rejected`, `replay_refuses_missing_or_empty_store` | |
| 66 | | no secret in compaction output; originals kept protected | no | `secrets_are_redacted_in_recap_and_stubs_but_kept_in_record`, `vault_redactor_strips_known_patterns`, `secret_straddling_the_oversized_cut_is_redacted` (redaction runs before every cut) | |
| 67 | | budgets enforced with mixed content | no (non-string content counted as 0) | `mixed_content_counts_toward_budget`, `estimate_is_conservative_against_known_counts`, `still_over_budget_is_reported_when_protected_tail_exceeds_max` | |
| 68 | | recap of a recap loses nothing | no | `recap_of_recap_keeps_earlier_lines_and_pointer` | |
| 69 | | semantics not only size | size only | the tests above assert structure, protection and reconstruction | |
| 70 | |
| 71 | ## 4. Limits (stated, not hidden) |
| 72 | |
| 73 | - Restart recovery (`CompactionStore::replay`) is implemented and unit tested across a serialized reload; it is |
| 74 | not integration tested, because the CLI has no session-resume command yet that would call it. |
| 75 | - The interactive chat loop is the only exercised entry point. The compose daemon and AEGIS are unchanged. |
| 76 | - The tokenizer path is exercised only when `AIEN_TOKENIZER_JSON` is set; the tests run on the estimate. |
| 77 | - Records are retained until the operator deletes the session folder; no automatic retention policy. |
| 78 | - Redaction uses the existing vault patterns; a secret the vault does not know and that matches no pattern |
| 79 | is not redacted from a recap line (it never leaves the compaction record otherwise). |
| 80 | - The recap keeps `role: user` because the wire sends only role and content; the derived marker is in the |
| 81 | text and on the message object. |