- Introduced a new design document for research backend integration, outlining scope, architecture, and API specifications. - Added frontend API service functions for managing research agents, nodes, and reports. - Extended existing Go backend packages to support new research functionalities. feat: Implement PicoClaw context window improvements - Created a design document detailing seven context-window improvements to enhance performance and prevent errors. - Improvements include token estimation fixes, structured error handling, and proactive context management strategies. fix: Document known issues and resolutions - Added a known issues document detailing a critical compile error related to invalid Unicode escapes in Go, including the fix applied. docs: Add context management skill documentation - Introduced a new skill for context management, detailing usage scenarios, project mapping, state persistence, and integration with PicoClaw.
4 KiB
4 KiB
PicoClaw Context Window Improvements — Design Document
Date
2025-01-09
Scope
Implement 7 context-window improvements to prevent "Context window exceeded" errors, based on research comparing PicoClaw's current approach with OpenCode's proven conservative strategy.
Non-Goals
- No changes to provider adapters (Anthropic, OpenAI, etc.)
- No changes to the session store (JSONL) format
- No changes to the seahorse database schema
- No changes to tool execution behavior
Architecture
Improvement 1: Fix Token Estimation
- File:
pkg/tokenizer/estimator.go - Change: Remove
CharsPerTokenconfig (already at 4.0 chars/token). AddCharsPerTokenconfig struct topkg/tokenizerfor future tuning. - Status: Already partially implemented (4.0 chars/token). Need to add Config struct.
- Risk: Very low. The 4.0 heuristic is already the default.
Improvement 2: Add ContextOverflowError Type
- File: Create
pkg/agent/errors.go - Change: Add structured
ContextOverflowErrorwith Model, ContextWindow, RequestedTokens, Reason fields. Updatepipeline_llm.goto wrap/recognize this error. - Risk: Low. New file; minimal existing dependency changes.
Improvement 3: Token-Aware Fresh Tail Protection
- File:
pkg/seahorse/short_constants.go,pkg/seahorse/short_assembler.go - Change: Replace fixed
FreshTailCount(32 messages) with token-budget-awareCalculateFreshTailBudget(contextWindow)(OpenCode's proven approach: 25% of usable context, bounded by 2000–8000 tokens, with minimum 2 turns). - Risk: Medium. Changes core assembler logic. All seahorse tests will need updates for the new budget calculation.
Improvement 4: Structured Summary Template
- File:
pkg/seahorse/short_compaction.go - Change: Add
SummaryTemplateconstant andUseStructuredSummariestoggle. Apply template ingenerateLeafSummaryandgenerateCondensedSummary. - Risk: Low. Template is additive; old behavior preserved as fallback.
Improvement 5: Tool Output Pruning
- File: Create
pkg/utils/tool_pruner.go - Change: Add
PruneToolOutputs(messages, protectedTurns, pruneThreshold)that walks backward and replaces old tool outputs with stubs. - Risk: Medium. New utility; needs careful edge-case handling (orphaned tool results).
Improvement 6: Proactive vs Reactive Compaction Separation
- File:
pkg/agent/context_manager.go,pkg/agent/pipeline_setup.go,pkg/agent/pipeline_llm.go - Change: Add
CompactReasontype withproactive,retry,overflowconstants. RenameContextCompressReasonProactiveandContextCompressReasonRetryto use the new type. Update all references. - Risk: Medium. Refactors enum type used across pipeline.
Improvement 7: Tool Output Truncation with Disk Saving
- File: Create
pkg/utils/truncate.go - Change: Add
TruncatedResultstruct andTruncateToolOutputfunction. Saves full output to disk, returns preview + hint. - Risk: Low. New utility; no existing callers.
Testing Strategy
- Unit tests for new utilities (
pkg/utils/tool_pruner_test.go,pkg/utils/truncate_test.go) - Update
pkg/seahorse/short_assembler_test.gofor token-budget fresh tail - Update
pkg/agent/pipeline_llm_test.go(or create one) forContextOverflowErrorhandling - Ensure
make testpasses
Rollout Notes
- The
FreshTailCountconstant inshort_constants.gowill be deprecated but kept for backward compatibility (exported but unused). ContextCompressReasonProactiveandContextCompressReasonRetryconstants will be kept as deprecated aliases.
Failure Mode Analysis
- Fresh tail budget too large: Could reduce usable context. Mitigated by MinPreserveRecentTokens bound and 25% ratio.
- Structured summary too verbose: Could increase token count instead of saving. Mitigated by keeping old format as fallback.
- Tool pruning breaks tool-call chains: Could orphan tool results. Mitigated by protectedTurns parameter and turn-boundary safety.
Approved
Proceed to implementation.