feat: add context management configuration (caching and compaction)#1298
feat: add context management configuration (caching and compaction)#1298dobesv wants to merge 6 commits intokagent-dev:mainfrom
Conversation
There was a problem hiding this comment.
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.
| class ContextSummarizerSettings(BaseModel): | ||
| """Settings for the LLM-based event summarizer.""" | ||
|
|
||
| summarizer_model_name: str | None = None | ||
| prompt_template: str | None = None | ||
|
|
||
|
|
There was a problem hiding this comment.
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).
| class ContextSummarizerSettings(BaseModel): | |
| """Settings for the LLM-based event summarizer.""" | |
| summarizer_model_name: str | None = None | |
| prompt_template: str | None = None |
| """Tests for context management configuration types and builder.""" | ||
|
|
||
| import json | ||
| from unittest.mock import MagicMock, patch |
There was a problem hiding this comment.
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.
| from unittest.mock import MagicMock, patch |
| events_compaction_config = EventsCompactionConfig( | ||
| compaction_interval=comp.compaction_interval, | ||
| overlap_size=comp.overlap_size, | ||
| summarizer=summarizer, | ||
| ) | ||
|
|
There was a problem hiding this comment.
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.
| 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) |
1c7f2da to
3fcce22
Compare
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>
3fcce22 to
4ed663f
Compare
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)Context *ContextConfigfield toDeclarativeAgentSpecContextConfig,ContextCompressionConfig,ContextCacheConfig,ContextSummarizerConfigGo ADK Config Types (
go/pkg/adk/types.go)AgentContextConfig,AgentCompressionConfig,AgentCacheConfigtypes for config.json serializationUnmarshalJSONto include newContextConfigfield (prevents silent data loss)Go Translator (
go/internal/controller/translator/agent/adk_api_translator.go)translateInlineAgent()Python Types (
python/packages/kagent-adk/src/kagent/adk/types.py)ContextCompressionSettings,ContextCacheSettings,ContextConfigPydantic modelsbuild_adk_context_configs()builder function that converts kagent types to Google ADKEventsCompactionConfigandContextCacheConfigLiteLlmfor universal summarizer model supportPython Runtime Wiring
KAgentAppaccepts and passesevents_compaction_configandcontext_cache_configto ADKApp()static,run,test) wire context configs fromAgentConfigTests
Example Usage
Related
EventsCompactionConfigandContextCacheConfigAPIs