feat(cache): cache-aware dedup with preserve_cache_prefix option#59
Merged
Conversation
Add PrefixPartition to pkg/cache that splits a chunk slice at the last cache_control marker. Wire into /v1/dedupe via options.preserve_cache_prefix: the frozen prefix is passed through unchanged; the dedup pipeline runs only on the suffix. Response stats include cache_prefix_frozen, cache_prefix_tokens, cache_prefix_hash, suffix_input_count, suffix_output_count. DedupeChunk gains cache_control field. DedupeOptions type added. Co-authored-by: Ona <no-reply@ona.com>
Add StabilityValidator to pkg/cache. Tracks prefix hashes per call site across requests and reports StabilityIssue when the rate drops below UnstableThreshold (default 0.8). Includes: - Runtime Check(callSite, chunks): records hash, detects changes after WarmupChecks (default 3), diagnoses likely cause from DynamicPatterns - Static ValidateText(text): one-shot scan for dynamic interpolation patterns (request id, timestamp, uuid, random, etc.) - Stats/AllStats/Reset for observability Co-authored-by: Ona <no-reply@ona.com>
handleDedupeStream was missing the partition logic added to handleDedupe. Both handlers now freeze the cache prefix, run dedup only on the suffix, and report cache_prefix_* fields in stats. Co-authored-by: Ona <no-reply@ona.com>
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 #50
What
Adds
preserve_cache_prefixto/v1/dedupeso the dedup pipeline cannot reorder or remove chunks that appear before acache_controlbreakpoint. Without this, Distill can silently invalidate Anthropic prompt cache prefixes while improving context quality.Changes
pkg/cache/prefix.go(new)PartitionForCacheAwareDedup(chunks)— detectscache_controlmarkers in chunk metadata, returnsPrefixPartitionwithPrefix(frozen),Suffix(dedup-eligible),PrefixHash,FrozenPrefixTokens,MarkerCounthasCacheControlhandles string, map, bool, and nil marker valuesPrefixAwareStatstype for response statscmd/api.goDedupeChunkgainscache_control stringfieldDedupeOptionstype withpreserve_cache_prefix boolDedupeRequestgainsOptions DedupeOptionsDedupeStatsgainscache_prefix_frozen,cache_prefix_tokens,cache_prefix_hash,suffix_input_count,suffix_output_counthandleDedupe: partitions chunks whenpreserve_cache_prefix=true, runs cluster/select/MMR only on the suffix, prepends frozen prefix to outputpkg/cache/prefix_test.go(new) — 6 tests covering no-marker passthrough, single/multiple markers, marker at end, hash stability, hash change detectionAPI
Response:
{ "chunks": [{"id": "sys", ...}, {"id": "msg1", ...}], "stats": { "cache_prefix_frozen": true, "cache_prefix_tokens": 64, "cache_prefix_hash": "a3f2c1d4e5b6f7a8", "suffix_input_count": 2, "suffix_output_count": 1 } }Why opt-in
Not all workloads use prompt caching. The current full-reorder behaviour produces better context quality when caching is not a concern.
preserve_cache_prefix=false(default) leaves existing behaviour unchanged.