Skip to content

feat: add context management configuration (caching and compaction)#1298

Open
dobesv wants to merge 6 commits intokagent-dev:mainfrom
dobesv:feat/context-management
Open

feat: add context management configuration (caching and compaction)#1298
dobesv wants to merge 6 commits intokagent-dev:mainfrom
dobesv:feat/context-management

Conversation

@dobesv
Copy link

@dobesv dobesv commented Feb 14, 2026

Summary

Add context management configuration (event compaction and context caching) for kagent agents, wiring the feature through the full pipeline: CRD → Go controller → config.json → Python runtime → Google ADK App().

This completes the work started in #1198 (stalled since January) with a clean implementation from scratch, addressing all identified wiring gaps.

Changes

Go CRD Types (go/api/v1alpha2/agent_types.go)

  • Add Context *ContextConfig field to DeclarativeAgentSpec
  • New types: ContextConfig, ContextCompressionConfig, ContextCacheConfig, ContextSummarizerConfig
  • CEL validation for required compaction fields
  • Kubebuilder default values matching ADK defaults

Go ADK Config Types (go/pkg/adk/types.go)

  • Add AgentContextConfig, AgentCompressionConfig, AgentCacheConfig types for config.json serialization
  • Critical: Update UnmarshalJSON to include new ContextConfig field (prevents silent data loss)

Go Translator (go/internal/controller/translator/agent/adk_api_translator.go)

  • Extract context config from Agent CRD in translateInlineAgent()
  • Resolve summarizer model name from ModelConfig CRD (falls back to agent's own model)
  • 6 table-driven test cases covering all combinations

Python Types (python/packages/kagent-adk/src/kagent/adk/types.py)

  • Add ContextCompressionSettings, ContextCacheSettings, ContextConfig Pydantic models
  • Add build_adk_context_configs() builder function that converts kagent types to Google ADK EventsCompactionConfig and ContextCacheConfig
  • Uses LiteLlm for universal summarizer model support

Python Runtime Wiring

  • KAgentApp accepts and passes events_compaction_config and context_cache_config to ADK App()
  • All 3 CLI commands (static, run, test) wire context configs from AgentConfig

Tests

  • Go: 6 translator test cases (no config, compaction only, cache only, both, summarizer with agent model, summarizer with separate model)
  • Python: 17 test cases covering type parsing, builder function, JSON round-trip with Go output format, edge cases

Example Usage

apiVersion: kagent.dev/v1alpha2
kind: Agent
metadata:
  name: my-agent
spec:
  type: Declarative
  declarative:
    modelConfig: default-model-config
    systemMessage: "You are a helpful assistant"
    context:
      compaction:
        compactionInterval: 10
        overlapSize: 3
        summarizer:
          promptTemplate: "Summarize the conversation so far"
      cache:
        cacheIntervals: 10
        ttlSeconds: 1800

Related

Copilot AI review requested due to automatic review settings February 14, 2026 08:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds context management configuration (event compaction and context caching) for kagent agents, completing work from stalled PR #1198. The feature is wired through the complete pipeline: CRD definitions → Go controller → config.json → Python runtime → Google ADK App().

Changes:

  • Added CRD types for context management (compaction and caching) with CEL validation and kubebuilder defaults
  • Extended Go ADK config types and translator to handle context configuration, including model resolution for summarizers
  • Implemented Python types and builder function to convert kagent configs to Google ADK objects (EventsCompactionConfig and ContextCacheConfig)
  • Wired context configs through all Python CLI commands (static, run, test) to KAgentApp and ADK App()

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
go/api/v1alpha2/agent_types.go Defines CRD types for context management: ContextConfig, ContextCompressionConfig, ContextCacheConfig, ContextSummarizerConfig with validation
go/api/v1alpha2/zz_generated.deepcopy.go Auto-generated DeepCopy methods for new context config types
go/config/crd/bases/kagent.dev_agents.yaml Generated CRD YAML schema with validation rules and default values
go/pkg/adk/types.go ADK config types for JSON serialization and critical UnmarshalJSON update to include ContextConfig
go/internal/controller/translator/agent/adk_api_translator.go Translates CRD context config to ADK config, resolves summarizer models with fallback to agent model
go/internal/controller/translator/agent/adk_api_translator_test.go 6 comprehensive test cases covering all context config scenarios
python/packages/kagent-adk/src/kagent/adk/types.py Pydantic models and builder function to convert kagent types to Google ADK configs
python/packages/kagent-adk/src/kagent/adk/_a2a.py KAgentApp accepts and passes context configs to ADK App()
python/packages/kagent-adk/src/kagent/adk/cli.py All CLI commands wire context configs via helper function
python/packages/kagent-adk/tests/unittests/test_context_config.py 17 test cases covering parsing, builder function, and JSON round-trip validation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 218 to 224
class ContextSummarizerSettings(BaseModel):
"""Settings for the LLM-based event summarizer."""

summarizer_model_name: str | None = None
prompt_template: str | None = None


Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ContextSummarizerSettings class is defined but never used anywhere in the codebase. The summarizer_model_name and prompt_template fields are duplicated in ContextCompressionSettings instead. This class should either be removed as dead code, or ContextCompressionSettings should be refactored to use it as a nested field (e.g., summarizer: ContextSummarizerSettings | None = None).

Suggested change
class ContextSummarizerSettings(BaseModel):
"""Settings for the LLM-based event summarizer."""
summarizer_model_name: str | None = None
prompt_template: str | None = None

Copilot uses AI. Check for mistakes.
"""Tests for context management configuration types and builder."""

import json
from unittest.mock import MagicMock, patch
Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused imports: MagicMock and patch are imported from unittest.mock but never used in the test file. These should be removed to keep the code clean.

Suggested change
from unittest.mock import MagicMock, patch

Copilot uses AI. Check for mistakes.
Comment on lines 470 to 475
events_compaction_config = EventsCompactionConfig(
compaction_interval=comp.compaction_interval,
overlap_size=comp.overlap_size,
summarizer=summarizer,
)

Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EventsCompactionConfig is created but token_threshold and event_retention_size fields from comp are not passed to it. These fields are defined in the CRD (agent_types.go), collected by the Go translator (adk_api_translator.go), and present in ContextCompressionSettings, but are silently dropped here.

If EventsCompactionConfig supports these parameters, they should be passed through. If not, either remove them from the earlier stages or document why they're being ignored.

Suggested change
events_compaction_config = EventsCompactionConfig(
compaction_interval=comp.compaction_interval,
overlap_size=comp.overlap_size,
summarizer=summarizer,
)
compaction_kwargs = {
"compaction_interval": comp.compaction_interval,
"overlap_size": comp.overlap_size,
"summarizer": summarizer,
}
# Optionally pass through additional compaction settings if available.
token_threshold = getattr(comp, "token_threshold", None)
if token_threshold is not None:
compaction_kwargs["token_threshold"] = token_threshold
event_retention_size = getattr(comp, "event_retention_size", None)
if event_retention_size is not None:
compaction_kwargs["event_retention_size"] = event_retention_size
events_compaction_config = EventsCompactionConfig(**compaction_kwargs)

Copilot uses AI. Check for mistakes.
@dobesv dobesv force-pushed the feat/context-management branch from 1c7f2da to 3fcce22 Compare February 14, 2026 10:17
Add ContextConfig, ContextCompressionConfig, ContextCacheConfig, and
ContextSummarizerConfig types to the v1alpha2 Agent CRD. Add a Context
field to DeclarativeAgentSpec for configuring event compaction and
prefix context caching.

Includes kubebuilder validation markers and CEL validation rules for
required compaction fields.

Signed-off-by: Dobes Vandermeer <dobes.vandermeer@newsela.com>
Add AgentContextConfig, AgentCompressionConfig, and AgentCacheConfig
types that flow through config.json to the Python runtime. Update
UnmarshalJSON to include the new ContextConfig field, preventing
silent data loss during deserialization.

Signed-off-by: Dobes Vandermeer <dobes.vandermeer@newsela.com>
Extract context config (compaction + cache) from Agent CRD in the
translator and populate AgentConfig.ContextConfig. Supports summarizer
model resolution (falls back to agent's own model if not specified).

Add table-driven tests covering: no context config, compaction only,
cache only, both combined, summarizer with agent model, and summarizer
with separate model config.

Signed-off-by: Dobes Vandermeer <dobes.vandermeer@newsela.com>
Add ContextCompressionSettings, ContextCacheSettings, ContextConfig
Pydantic models and build_adk_context_configs() function that converts
kagent config to Google ADK EventsCompactionConfig and ContextCacheConfig.

Includes 17 unit tests covering type parsing, builder function,
JSON round-trip compatibility with Go output, and edge cases.

Signed-off-by: Dobes Vandermeer <dobes.vandermeer@newsela.com>
…d CLI

Add events_compaction_config and context_cache_config params to
KAgentApp and pass them through to the Google ADK App() constructor.

Wire all 3 CLI command KAgentApp instantiation sites (static, run,
test_agent) to extract context configs from AgentConfig and pass
them via _build_context_kwargs helper.

Signed-off-by: Dobes Vandermeer <dobes.vandermeer@newsela.com>
Signed-off-by: Dobes Vandermeer <dobes.vandermeer@newsela.com>
@dobesv dobesv force-pushed the feat/context-management branch from 3fcce22 to 4ed663f Compare February 15, 2026 05:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant