feat(cache): PatternDetector emits cache_control annotations#55
Merged
Conversation
Extend PatternDetector to output a CacheAnnotation alongside each pattern classification, and add AnnotateChunksForCache to produce a CacheControlPlan for a chunk slice. - CacheAnnotation: Recommended, Reason, MinTokensMet, BoundaryAfter - Pattern → annotation mapping: system_prompt and tool_definition always recommended; code_block conditional on token count >= 512; document recommended; user messages not recommended - AnnotateChunksForCache: selects up to 4 markers (Anthropic limit) by highest token count; skips auto-placement when manual cache_control markers are already present in chunk metadata - DetectPattern now populates TokenCount on every DetectedPattern Co-authored-by: Ona <[email protected]>
Co-authored-by: Ona <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #53
What
Extends
PatternDetectorinpkg/cacheto output aCacheAnnotationalongside each pattern classification, and addsAnnotateChunksForCacheto produce aCacheControlPlanfor a chunk slice.Changes
pkg/cache/patterns.goCacheAnnotationstruct:Recommended,Reason,MinTokensMet,BoundaryAfterDetectedPatternnow carriesTokenCountandCacheAnnotationannotate(): pattern → annotation mappingsystem_prompt,tool_definition,document→ always recommendedcode_block→ conditional on token count ≥ 512AnnotateChunksForCache(chunks): walks chunks, selects up to 4 markers (Anthropic's limit) by highest token count, skips auto-placement when manualcache_controlmarkers are already present in chunk metadataCacheControlPlan/CacheMarkertypespkg/cache/cache_test.goTestPatternDetector_CacheAnnotation: verifies annotation per pattern typeTestPatternDetector_AnnotateChunksForCache: 4-marker cap, manual marker passthrough, nil inputWhy
PatternDetectoralready identifies cacheable content (system prompts, tool definitions, code blocks). This change connects that signal to Anthropic'scache_controlfield so callers get correct markers without needing to understand prompt caching mechanics. It also provides stability hints to the session-aware boundary manager (issue #51).