Skip to content

feat(adk): Go runtime conversation-history compaction#2025

Draft
QuentinBisson wants to merge 2 commits into
kagent-dev:mainfrom
QuentinBisson:feat/go-adk-compaction
Draft

feat(adk): Go runtime conversation-history compaction#2025
QuentinBisson wants to merge 2 commits into
kagent-dev:mainfrom
QuentinBisson:feat/go-adk-compaction

Conversation

@QuentinBisson

@QuentinBisson QuentinBisson commented Jun 15, 2026

Copy link
Copy Markdown

What

Implement conversation-history compaction for the Go ADK runtime, bringing it to functional parity with the Python runtime's spec.declarative.context.compaction feature.

The Go runtime previously silently dropped the contextConfig.compaction field and raised UnsupportedFeatures on runtime: go declarative agents. This PR fixes that.

Approach

The compaction algorithm is ported from adk-python and runs entirely within the kagent adk/pkg/compaction package — no fork of google.golang.org/adk is required.

Because google.golang.org/adk v1.4.0 has no compaction event types, compaction markers are encoded in existing Event fields:

  • Author = "compaction"
  • InvocationID = "compaction_<startNano>_<endNano>"
  • Content holds the LLM-generated summary

A compactingService wrapper (session_wrapper.go) intercepts Get to replace the raw marker events with synthetic summary events before the runner sees them. The underlying storage keeps the markers intact so MaybeCompact can parse the watermark on subsequent invocations.

Algorithm

Mirrors adk-python's invocation-based sliding window:

  1. Group events by InvocationID; parse the watermark from the most recent compaction marker's InvocationID
  2. Count invocations whose latest timestamp is after the watermark; fire when ≥ compactionInterval
  3. Window start = max(0, firstNewIdx - overlapSize) (overlap reaches back into already-compacted invocations)
  4. Trim to the longest self-contained prefix — never compacts through an open function call or unresolved HITL confirmation

Supports both sliding-window and token-threshold modes. All model adapters (Anthropic, OpenAI, Bedrock, Ollama) are reused for summarization via the existing CreateLLM path.

Files changed

File Change
go/adk/pkg/compaction/compaction.go Rewritten: InvocationID-based marker encoding, BuildCompactedEventList, watermark parsing, filterCompactionMarkers, MaybeCompact
go/adk/pkg/compaction/session_wrapper.go New: compactingService + compactedSession wrapper; applies compacted view on Get, unwraps on AppendEvent
go/adk/pkg/compaction/compaction_test.go Updated: removed fork-type helpers, added makeCompactionMarkerEvent using the new encoding
go/adk/pkg/runner/adapter.go Wraps session service with NewCompactingService when compaction is configured
go/adk/pkg/session/local_session.go Remove ReplaceEvents (no callers)
go/core/internal/controller/reconciler/reconciler.go Remove UnsupportedFeatures guard for context.compaction on runtime: go
go/go.mod / go/go.sum Upgrade google.golang.org/adk to v1.4.0; no replace directive

Closes

#1173

@github-actions github-actions Bot added the enhancement New feature or request label Jun 15, 2026
Port adk-python's sliding-window + token-threshold compaction to the Go
runtime, bringing it to full feature parity with spec.declarative.context.compaction.

Changes:
- New go/adk/pkg/compaction package: watermark-based invocation counting,
  longestSelfContainedPrefix safety, token-threshold mode, and LLM
  summarization wired through existing model adapters.
- go/adk/pkg/runner/adapter.go: buildCompactionConfig translates
  AgentCompressionConfig into a Compactor, wiring SummarizerLLM from
  AgentCompressionConfig.SummarizerModel (or falling back to agent model).
- go/adk/pkg/a2a/executor.go: MaybeCompact called post-invocation
  (step 11), best-effort, never fails the request.
- go/adk/pkg/session/local_session.go: ReplaceEvents for forward compat.
- go/core/internal/controller/reconciler/reconciler.go: remove
  UnsupportedFeatures guard for context compaction.
- go/go.mod: replace directive pins google.golang.org/adk to
  github.com/QuentinBisson/adk-go feat/event-compaction (adds
  EventCompaction type, EventActions.Compaction field, and applyCompaction
  pass in buildContentsDefault).

Summary events are persisted as compaction marker events in the session
backend (idempotent / resumable), not as in-memory req.Content mutations.
The Anthropic null-system 400 (adk-python#5318) is structurally avoided:
the summarizer request carries no SystemInstruction.

Signed-off-by: QuentinBisson <quentin@giantswarm.io>
…nvocationID

Replace the personal adk-go fork (`github.com/QuentinBisson/adk-go`) with
the canonical `google.golang.org/adk` module at v1.4.0.

The fork added `EventActions.Compaction` and `EventCompaction` types to
persist compaction summaries.  Those types do not exist in upstream v1.4.0,
so the approach is changed: compaction marker events are encoded entirely
within the existing `Event` fields — `Author = "compaction"` and
`InvocationID = "compaction_<startNano>_<endNano>"` — with the summary in
`Event.Content`.

Key changes:
- compaction.go: rewritten to use InvocationID encoding; adds
  `buildCompactionEvent`, `parseCompactionInvocationID`, `findWatermark`,
  `filterCompactionMarkers`, and exported `BuildCompactedEventList`
- session_wrapper.go: new `compactingService` wrapper that applies
  `BuildCompactedEventList` on `Get` so the runner receives a compacted
  view while the raw marker events remain in storage for `MaybeCompact`
- adapter.go: wraps the session service with `NewCompactingService` when
  compaction is configured; unwrapping is transparent to the executor
- local_session.go: remove `ReplaceEvents` (no callers)
- go.mod: drop `replace google.golang.org/adk => …` directive; upgrade to
  `google.golang.org/adk v1.4.0`

Signed-off-by: QuentinBisson <quentin@giantswarm.io>
@github-actions github-actions Bot added enhancement New feature or request and removed enhancement New feature or request labels Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant