feat: add per-model context compaction #21
No reviewers
Labels
No labels
code-review
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
sam/brainless!21
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/context-compaction"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Adds session context compaction to the harness: once a session's prompt grows past a token budget, ADK summarizes the older turns and later prompts carry the summary plus the most recent events. ADK v2.4.0 (already a dependency) ships the whole mechanism — tail retention, an LLM summarizer, and SQLite persistence of summary events — so brainless only wires the runner option and resolves the config.
Compaction is on by default (ADK tail retention: one rolling summary + the 10 most recent events). An optional
compactionblock inbrainless.jsontunes it:defaultThreshold— prompt token count that triggers summarization, for models without their own entry (built-in default 20000).compactionThreshold— per-model overrides keyed byprovider/model, because context windows differ: a small-context model (e.g. local Gemma 4 E2B ~4096 tokens) compacts well below its window, a 128k cloud model stays on the default.summarizerModel— the configured model that writes summaries; defaults to the session's own model, but a model too small to summarize its own history can point this at a larger-context model."disabled": trueturns compaction off. (Schema note: the toggle isdisabled, notenabled, so a plain bool keeps "absent means on" without a pointer-to-bool.)Because the threshold is per model and ADK reads it from a fixed runner config, a model switch rebuilds the runner with the new model's threshold. The runner is stateless (sessions live in the store) and the root agent is shared, so the rebuild is cheap; it happens under the existing turn lock in
Prompt, where the model selection already takes effect. Theprompt-before-initializecheck moved under the same lock since the runner is now mutable.Invalid settings fail at boot: negative thresholds, non-positive per-model thresholds, and threshold/summarizerModel keys that name no configured model. A failed summarization is logged and the turn continues; it never fails the user's answer.
Testing
Disabledflag, summarizer-model selection, validation errors, JSON parsing of the block.go build,go vet,gofmt, fullgo test ./...,golangci-lint(0 issues) all pass; theTestACP*e2e tests exercise the realPromptpath including the runner swap after a model switch.Reviewer notes
Review comments from the first pass are resolved (see
fix: resolve review comments on context compaction): no pointer-to-bool (disabledflag +omitzero), shortened comments, the runner-swap race fixed by moving the nil check underturnMu,okfromreg.Default()handled, and the(nil, nil)+nilnilnolint eliminated via acompactionEnabled()gate. The*compaction.Configpointer remains because ADK'srunner.Config.Compactionis a pointer and nil is its "disabled" value.AI-driven code review — context compaction (feat/context-compaction)
Reviewed with the samber/cc-skills-golang skill set (code-style/naming/docs → error-handling/safety/concurrency → security/dependency-management → testing/performance/observability/modernize), every claim verified in this session against the PR head
8a6d6c2(detached worktree, no local-checkout assumptions).Verification gate (commands actually run)
go build ./...→ exit 0go vet ./...→ exit 0go test ./... -count=1→ all packages okracedetector test ./internal/harness/ -count=1→ ok, no data racesgolangci-lint run ./internal/config/... ./internal/harness/...→ 0 issues (matches the PR claim)gofmt -l internal/→ cleango doc google.golang.org/adk/v2/session/compaction(+Config,LLMSummarizerConfig,NewLLMSummarizer,ErrCompaction) andgo doc .../runner.Config→ every ADK symbol the diff relies on exists with the exact shape used, in the version the PR actually builds against (go list -m google.golang.org/adk/v2→ v2.4.0; the diff changes no go.mod/go.sum lines, so no new dependency, no version bump, no replace directive)Findings
internal/harness/compaction_test.go— dangling//go:fix inlinedirective).Notes (verified, no action needed)
h.runner/h.runnerModelare touched only underturnMuinPromptand once inNew(grep of allh.runnersites); the nil check moved under the same lock; race detector is clean.errors.Is(err, compaction.ErrCompaction)+ log-and-continuepattern is exactly ADK's documented usage (thego docoutput forErrCompactionshows this loop verbatim); the failure never fails the user's answer.compactionForcannot produce an invalidcompaction.Configat runtime:TokenThresholdis always > 0 (0 → built-in default 20000, negatives and non-positive per-model values rejected at boot byvalidateCompaction), andEventRetentionSizeis always 10 — ADK'sConfig.Validaterejects a config enabling neither strategy, which is unreachable here.TestACPRoundTripEndToEndgenuinely exercises the swap: it switches modelp/m1→p/m2and then prompts through the realPromptpath.//nolint:ireturnonRegistry.LLMcarries an explanation on the same line (satisfiesnolintlint), consistent with the existingactivateprecedent.disabledbool:omitzero(Go 1.24+) on the bool/int/string/map fields is valid for the module'sgo 1.27.1toolchain.Solid PR: correct concurrency, faithful use of the ADK mechanism, strong test coverage, docs match the code. Approving — the single suggestion is optional cleanup.
@ -0,0 +215,4 @@}}//go:fix inline🟡 SUGGESTION — dangling
//go:fix inlinedirective at EOF, remove the line.go:fixis a real directive in the Go 1.27 toolchain (it is consumed by the x/toolsinlineanalysis pass, proposal #32816), but it only takes effect when placed in a declaration's doc comment — thegofixdirective.Findpass scansFuncDecl/GenDecldoc comments, and this line follows the last}ofTestValidateCompactionat file scope, so it annotates nothing.Verified in this session:
go build ./...,go vet ./..., andgo test ./internal/harness/all pass with it present, so it is harmless — but it reads as an accidental paste. Delete the line.Resolved: the dangling
//go:fix inlinedirective at the end ofinternal/harness/compaction_test.gois removed in36c0edf(the file now ends at the closing}ofTestValidateCompaction). It was an accidental artifact of the Go 1.27 auto-fixer, as the reviewer suspected. Verified:go build,go test ./internal/harness/,golangci-lint(0 issues),gofmtclean.