|
Download plans/agent-context-compaction-2025-03-24-concise.md from SaylorTwift/forgecode: direct link, hf CLI and curl.
- Browser
- Download file 2.72 kB
-
https://huggingface.co/SaylorTwift/forgecode/resolve/main/plans/agent-context-compaction-2025-03-24-concise.md
- Command line
-
hf download hf://SaylorTwift/forgecode/plans/agent-context-compaction-2025-03-24-concise.md
-
curl -L -o agent-context-compaction-2025-03-24-concise.md https://huggingface.co/SaylorTwift/forgecode/resolve/main/plans/agent-context-compaction-2025-03-24-concise.md
2.72 kB
Agent Context Compaction Implementation Plan
Objective
Add automatic context compaction to the Agent struct based on configurable triggers (token count, turn count, message count) with a specified maximum token limit.
Implementation Plan
1. Define New Compaction Configuration Structure
Create a Compaction struct in crates/forge_domain/src/agent.rs with:
token_threshold: Optional maximum token count triggerturn_threshold: Optional maximum turns triggermessage_threshold: Optional maximum messages triggermax_tokens: Maximum allowed tokens after compactionshould_compact()method to determine when compaction is needed
2. Update Agent Struct
Add an optional compact field to the Agent struct that follows existing patterns and merging strategies.
3. Implement Context Compaction Logic
Modify the Orchestrator implementation to:
- Add
compact_context()method to handle the compaction process - Update
init_agent_with_event()to check compaction conditions before processing - Use existing
Summarizemechanism for the initial implementation
4. Enhance Token Counting
Implement a more accurate token counting function to support compaction decisions.
5. Handle Transform Deprecation
- Add deprecation notice to the
Transform::Assistantvariant - Add warning logs when the deprecated functionality is used
- Document migration path to the new approach
6. Testing Strategy
Add tests to verify:
- Compaction configuration works correctly
- Compaction triggers function as expected
- Merging behavior for the new field
- Context reduction achieves the token target
7. Documentation Updates
Update documentation with examples of the new configuration:
# Example in forge.yaml
agents:
myAgent:
id: myAgent
model: gpt-4-turbo
system_prompt: "You are a helpful assistant."
compact:
max_tokens: 4000
token_threshold: 6000
turn_threshold: 10
message_threshold: 30
Verification Criteria
- Proper implementation of
Compactionstruct with needed fields and methods Agentstruct correctly handles the newcompactfield- Automatic compaction triggers when configured conditions are met
- Appropriate deprecation of the transform-based approach
- All tests pass, verifying compaction logic works as expected
- Clear documentation explains how to use the new feature
Implementation Sequence
- Add
Compactionstruct toagent.rs - Add
compactfield toAgentstruct - Implement orchestrator compaction methods
- Add token counting enhancement
- Deprecate the transform-based approach
- Add tests for the new functionality
- Update documentation and examples