fix: stop shrinking conversations behind the user's back — both automatic context guards ship off
Nightly Build / build (push) Successful in 7m35s

The shipped default combined a sliding history window with no compaction, which
is the worse of the two available trades in both directions it is measured on.

`max_history_messages: 30` is a sliding tail window (`projection::window` —
`drain(..len - max)`). Past 30 messages it drops from the head on *every* turn,
so the prompt prefix changes on every single request and every provider that
caches one (Anthropic breakpoints, OpenAI automatic prefix caching) misses every
time. It also drops those messages with no summary standing in for them: silent
amnesia, not just a cold cache. Compaction rewrites the prefix once per
compaction and leaves a summary behind — yet it was the half that was commented
out, while the window's own doc-comment already said the two were exclusive.

Both are now `Option` and both ship unset, so nothing shrinks a conversation
unless a human types `/compact`.

Which surfaced the real bug: `/compact` did not work either. The compactor was
`Option<Arc<ContextCompactor>>` keyed on the config section existing, so
commenting out `compaction:` disabled the manual command too — `force_compact`
returned `Ok(false)` and the chat answered "compaction disabled". Manual
compaction is a command a user types; it cannot depend on an admin having filled
in a token threshold. The compactor is now built unconditionally and
`threshold_tokens: Option<u32>` arms only the automatic pass; `try_compact`
early-returns without it, `force_compact` deliberately never consults it.

The projection accordingly yields to the *automatic* pass rather than to the
compactor's existence (`LoopConfig.auto_compaction_enabled`), so a configured
message cap is not silently voided by `/compact` merely being available.
`CompactionConfig::Default` is hand-written for the same reason `RoleAttrs`'s is:
a derived one gives `keep_recent: 0`, which would compact away every recent
message on any box omitting the section — now the default.

Also fixes two documentation bugs in the same file: `event_triage` was documented
nested under `llm:`, where it parses fine and is then silently ignored (it is a
top-level field), and `datetime` was documented twice with conflicting examples.

A new test asserts the shipped default actually deserializes and that both guards
are off — a field the default omits must be genuinely optional, or a brand-new
install fails to boot.

Automatic compaction returns later, triggered off the resolved model's own
context window instead of a hand-tuned token count that cannot know which model
is answering.
This commit is contained in:
2026-08-02 21:21:14 +01:00
parent d4b34e6130
commit baf68878e4
16 changed files with 227 additions and 105 deletions
+39 -7
View File
@@ -7,7 +7,14 @@ pub use core_api::provider::LlmStrength;
/// LLM runtime settings (clients are managed via LlmManager / DB, not here).
#[derive(Debug, Deserialize)]
pub struct LlmConfig {
pub max_history_messages: usize,
/// Hard cap on the number of history messages projected into the context,
/// applied as a **sliding tail window**. Omit (the default) to disable it:
/// once history exceeds the cap, every turn shifts the window's start, which
/// changes the prompt prefix and costs a full prompt-cache miss on every
/// single request — while dropping the oldest messages with no summary to
/// stand in for them. Set it only when a hard message bound is worth both.
#[serde(default)]
pub max_history_messages: Option<usize>,
pub max_tool_rounds: Option<usize>,
/// Maximum number of synchronous sub-agents run concurrently when the LLM emits
/// a homogeneous batch of sub-agent calls in one response. Omit to use the
@@ -21,8 +28,11 @@ pub struct LlmConfig {
pub max_tool_result_chars: Option<usize>,
/// Request/response logging configuration. Omit or set `enabled: false` to disable.
pub requests_log: Option<LlmRequestsLogConfig>,
/// Context compaction settings. Omit to disable automatic compaction.
pub compaction: Option<CompactionConfig>,
/// Context compaction settings. Omitting the section leaves manual `/compact`
/// working on defaults — only the automatic trigger is opt-in, see
/// [`CompactionConfig::threshold_tokens`].
#[serde(default)]
pub compaction: CompactionConfig,
/// Controls how the current date/time is injected into each LLM request.
#[serde(default)]
pub datetime: DatetimeConfig,
@@ -48,12 +58,21 @@ impl Default for DatetimeConfig {
}
}
/// Context compaction: summarises conversation history when the LLM context
/// exceeds `threshold_tokens`.
/// Context compaction: summarises conversation history so the context stops
/// growing.
///
/// The compactor is **always built** — `/compact` is a manual command and must
/// work out of the box. This struct only tunes it, and `threshold_tokens` is
/// the one switch that arms the *automatic* trigger.
#[derive(Debug, Clone, Deserialize)]
pub struct CompactionConfig {
/// Trigger compaction when the previous turn consumed more than this many input tokens.
pub threshold_tokens: u32,
/// Trigger compaction when the previous turn consumed more than this many
/// input tokens. Omit (the default) to leave automatic compaction **off**:
/// history is then append-only, which is what keeps the prompt prefix — and
/// so the provider's prompt cache — stable across a whole conversation.
/// Manual `/compact` is unaffected either way.
#[serde(default)]
pub threshold_tokens: Option<u32>,
/// Number of recent messages to keep outside the summary. Defaults to 6.
#[serde(default = "default_keep_recent")]
pub keep_recent: usize,
@@ -61,6 +80,19 @@ pub struct CompactionConfig {
pub strength: Option<LlmStrength>,
}
/// Hand-written rather than derived: a derived `Default` would give
/// `keep_recent: 0`, silently compacting away every recent message on any box
/// that omits the section — which is now the shipped default.
impl Default for CompactionConfig {
fn default() -> Self {
Self {
threshold_tokens: None,
keep_recent: default_keep_recent(),
strength: None,
}
}
}
/// Event-triage background processor settings.
#[derive(Debug, Clone, Deserialize)]
pub struct EventTriageConfig {