feat(memory): memory decay lifecycle events and cache boundary hints#58
Merged
Conversation
…54) Wire pkg/memory lifecycle transitions into the cache boundary layer via events and a CacheBoundaryHint on RecallResult. New file pkg/memory/cache_events.go: - MemoryEventType: EventStabilized, EventCompressed, EventEvicted - MemoryEvent: Type, EntryID, TokensBefore, TokensAfter, CompressionLevel, OccurredAt - MemoryEventHandler callback type - CacheBoundaryHint: StableEntryIDs, ConfidenceScore pkg/memory/store.go: - RecallResult gains CacheHint *CacheBoundaryHint - Store interface gains OnLifecycleEvent(MemoryEventHandler) pkg/memory/sqlite.go: - SQLiteStore gains handlers []MemoryEventHandler - OnLifecycleEvent registers a handler; emit dispatches to all handlers pkg/memory/decay.go: - evictRows (new): queries entries to evict, deletes them, emits EventEvicted per entry with TokensBefore set - decayRows: emits EventCompressed after each UPDATE with TokensBefore, TokensAfter, and CompressionLevel set pkg/memory/sqlite.go (Recall): - buildCacheBoundaryHint: entries with relevance >= 0.7 are treated as stable; ConfidenceScore is the mean relevance of all returned entries - RecallResult.CacheHint populated on every Recall call Tests (memory_test.go): - TestLifecycleEvents_Compression: verifies EventCompressed fires with correct token counts and compression level - TestLifecycleEvents_Eviction: verifies EventEvicted fires with TokensAfter=0 - TestRecall_CacheBoundaryHint: near-exact embedding match produces a hint with at least one stable entry ID Co-authored-by: Ona <no-reply@ona.com>
Co-authored-by: Ona <no-reply@ona.com>
d8fda17 to
fe0f3bd
Compare
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 #54
What
Connects
pkg/memory's hierarchical decay lifecycle to the cache boundary layer. When theDecayWorkercompresses or evicts a memory entry, it now emits a typed event so subscribers (e.g. aCacheBoundaryManager) can retreat the cached prefix before the next request sends stale content.Recallalso returns aCacheBoundaryHintderived from recall scores, giving the boundary manager early stability signal without waiting for the normal promotion cycle.Changes
pkg/memory/cache_events.go(new)MemoryEventType:EventStabilized,EventCompressed,EventEvictedMemoryEvent:Type,EntryID,TokensBefore,TokensAfter,CompressionLevel,OccurredAtMemoryEventHandlercallback type (non-blocking contract)CacheBoundaryHint:StableEntryIDs,ConfidenceScorepkg/memory/store.goRecallResultgainsCacheHint *CacheBoundaryHintStoreinterface gainsOnLifecycleEvent(MemoryEventHandler)pkg/memory/sqlite.goSQLiteStoregainshandlers []MemoryEventHandlerOnLifecycleEventappends a handler;emitdispatches to all in orderRecallcallsbuildCacheBoundaryHint— entries with relevance ≥ 0.7 are listed as stable candidates;ConfidenceScoreis the mean relevance of all returned entriespkg/memory/decay.goevictRows(new): queries entries atDecayKeywordslevel older than cutoff, deletes them, emitsEventEvictedper entrydecayRows: emitsEventCompressedafter eachUPDATEwithTokensBefore,TokensAfter, andCompressionLevelrunOncedelegates eviction toevictRowsinstead of a bareDELETEpkg/memory/memory_test.goTestLifecycleEvents_Compression:EventCompressedfires withTokensBefore > TokensAfterand correctCompressionLevelTestLifecycleEvents_Eviction:EventEvictedfires withTokensAfter == 0TestRecall_CacheBoundaryHint: near-exact embedding match produces a hint with ≥ 1 stable entry ID and positive confidence scoreWhy
Without co-evolution, a document that is stable for 24 turns and then compressed from full text to summary causes a cache miss on the next request — the boundary still points to the full-text prefix, which no longer matches.
EventCompressedlets the boundary manager retreat before that request is sent. Similarly,EventEvictedprevents the boundary from pointing to content that no longer exists.The
CacheBoundaryHintinRecallResultlets the boundary manager act on turn 1 for high-confidence recalled entries, rather than waitingMinStableTurnspushes to confirm stability independently.Dependency
Builds on the
CacheBoundaryManagerintroduced in PR #56. The events and hint are emitted regardless of whether a boundary manager is registered — zero overhead when no handlers are attached.