Skip to content

feat(config): make the auto-compaction threshold configurable (compaction_threshold)#3448

Merged
Sayt-0 merged 1 commit into
mainfrom
feat/configurable-compaction-threshold
Jul 3, 2026
Merged

feat(config): make the auto-compaction threshold configurable (compaction_threshold)#3448
Sayt-0 merged 1 commit into
mainfrom
feat/configurable-compaction-threshold

Conversation

@Sayt-0

@Sayt-0 Sayt-0 commented Jul 3, 2026

Copy link
Copy Markdown
Member

Closes #3433

Summary

The proactive auto-compaction trigger was a hardcoded contextThreshold = 0.9 constant in pkg/compaction. It is now configurable through a new compaction_threshold config key (per agent, with a per-model override), and an explicit session_compaction on/off knob is surfaced in YAML. Behavior is unchanged unless the new keys are set.

Issue expectations vs implementation

Issue expectation Implementation
compaction_threshold key, range 0 < x <= 1, default 0.9 AgentConfig.CompactionThreshold *float64, validated at config load (validateCompactionThreshold); default lives in compaction.DefaultThreshold
Per-agent placement with per-model override next to compaction_model ModelConfig.CompactionThreshold; compactionThresholdForAgent in teamloader mirrors getCompactionModelForAgent (first of the agent's models that sets it wins, agent value is the fallback)
session_compaction: false knob mapping to WithSessionCompaction semantics AgentConfig.SessionCompaction *bool (tri-state, default on) wired to a new agent.WithSessionCompaction; runtime gates automatic triggers on r.sessionCompaction && a.SessionCompaction()
Replace the package const, thread the value through ShouldCompact ShouldCompact(..., threshold float64); values outside (0, 1] (including 0 for "not configured") fall back to DefaultThreshold
Runtime trigger sites All three automatic sites covered: pre-turn trigger (loop.go), compactIfNeeded (loop.go), post-overflow recovery (loop_steps.go)
Config additions only in pkg/config/latest + schema + example latest/types.go + latest/validate.go only (v0 to v11 untouched), agent-schema.json, new examples/compaction_threshold.yaml
Extend compaction_trigger_test.go TestCompactIfNeeded_CustomThreshold (3 subcases) and TestCompactIfNeeded_AgentSessionCompactionDisabled

Value flow

YAML: models.<m>.compaction_threshold | agents.<a>.compaction_threshold
        -> teamloader.compactionThresholdForAgent   (model wins, else agent, else nil)
        -> agent.WithCompactionThreshold            (stores only (0, 1])
        -> runtime trigger sites
        -> compaction.ShouldCompact(..., threshold) (<= 0 or > 1 falls back to 0.9)

YAML: agents.<a>.session_compaction (default true)
        -> agent.WithSessionCompaction
        -> runtime.sessionCompactionEnabled = runtime opt AND agent flag
        -> gates proactive trigger + overflow recovery (manual /compact never gated)

Behavior notes

Case Behavior
Keys unset Identical to before (0.9, compaction on)
Threshold out of range in YAML Rejected at validation with agents.<name> / models.<name> context
first_available combined with compaction_threshold Rejected, same rule as compaction_model / title_model
session_compaction: false Disables proactive trigger and post-overflow auto-compaction for that agent; manual /compact still works
Runtime model switching Threshold resolved at load time, unaffected, consistent with compaction_model
Integer YAML value compaction_threshold: 1 Decodes correctly into *float64 (strict goccy decoding verified)

Validation

Check Result
go build ./..., go vet pass
golangci-lint run + go run ./lint . 0 issues
Tests: compaction, agent, runtime, teamloader, config, schema, docs snippets, examples pass
New tests threshold table cases, 4 runtime trigger tests, 7 teamloader subtests, 12 validation subtests, first_available exclusion
Pre-existing environmental failures (SSRF tests, DMR example pull) unrelated, also fail on a clean main checkout

Docs updated: docs/configuration/agents/index.md, docs/configuration/models/index.md, new example examples/compaction_threshold.yaml.

Replace the hardcoded contextThreshold constant in pkg/compaction with an
exported DefaultThreshold and thread the threshold value through
ShouldCompact. Add a compaction_threshold config key (range (0, 1], default
0.9) on agents, with a per-model override next to compaction_model; the
model-level value takes precedence. Surface an explicit session_compaction
knob on agents that disables the proactive threshold trigger and the
post-overflow auto-recovery while keeping manual /compact available.

Closes #3433
@Sayt-0 Sayt-0 requested a review from a team as a code owner July 3, 2026 13:09
@Sayt-0 Sayt-0 added kind/feat PR adds a new feature (maps to feat:). Use on PRs only. area/config For configuration parsing, YAML, environment variables area/runtime Runtime engine, agent loop execution, tool dispatch, loop detection area/agent For work that has to do with the general agent loop/agentic features of the app area/sessions For features/issues/fixes related to session lifecycle (resume, persistence, export) labels Jul 3, 2026
@Sayt-0 Sayt-0 merged commit 2cd6214 into main Jul 3, 2026
16 checks passed
@Sayt-0 Sayt-0 deleted the feat/configurable-compaction-threshold branch July 3, 2026 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent For work that has to do with the general agent loop/agentic features of the app area/config For configuration parsing, YAML, environment variables area/runtime Runtime engine, agent loop execution, tool dispatch, loop detection area/sessions For features/issues/fixes related to session lifecycle (resume, persistence, export) kind/feat PR adds a new feature (maps to feat:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(config): make the auto-compaction threshold configurable (compaction_threshold)

2 participants