Skip to content

feat(session): session-aware cache boundary manager#56

Merged
Siddhant-K-code merged 2 commits into
mainfrom
feat/51-session-cache-boundary-manager
May 2, 2026
Merged

feat(session): session-aware cache boundary manager#56
Siddhant-K-code merged 2 commits into
mainfrom
feat/51-session-cache-boundary-manager

Conversation

@Siddhant-K-code

Copy link
Copy Markdown
Owner

Closes #51

What

Adds CacheBoundaryManager to pkg/session. After each Push, the manager promotes stable entries and evaluates the optimal cache_control placement for the next request, returning the result in PushResult.CacheBoundary.

Changes

pkg/session/cache_boundary.go (new)

  • CacheBoundaryManager: RecordPush, Evaluate, InvalidateEntry, Stats
  • CacheBoundaryConfig: Enabled, MinStableTurns (default 2), MinPrefixTokens (default 1024), MaxMarkers (default 4)
  • CacheBoundaryResult: Markers []CacheBoundaryMarker, TotalStableTokens, Advanced, Retreated

pkg/session/sqlite.go

  • Schema: session_entries gains inserted_at_push, stable_since_turn, content_hash; sessions gains push_count, cache_boundary_tokens
  • Push calls RecordPush then Evaluate after budget enforcement; result carries CacheBoundary
  • Get returns PushCount and CacheBoundaryTokens

pkg/session/session.go

  • Session exposes CacheBoundaryTokens, PushCount
  • PushResult carries CacheBoundary *CacheBoundaryResult
  • Config includes CacheBoundary CacheBoundaryConfig

Why

pkg/session already tracks which entries are present across turns and classifies them by stability (via preserve_recent and importance scoring). This change uses that existing signal to place cache_control markers automatically. Engineers stop needing to know what a cache boundary is — the session layer handles it.

At 50 turns with 10K tokens of stable docs, the boundary manager reduces effective input cost from ~$15K/day to ~$2.5K/day at 10K sessions/day (83% reduction on the stable portion).

Siddhant-K-code and others added 2 commits May 2, 2026 13:04
Add CacheBoundaryManager to pkg/session. After each Push, the manager
promotes stable entries and evaluates the optimal cache_control placement
for the next request.

Schema changes (session_entries):
- inserted_at_push: push count when the entry was first added
- stable_since_turn: push count when the entry became stable (0 = not yet)
- content_hash: FNV hash for change detection

Schema changes (sessions):
- push_count: incremented on every Push call
- cache_boundary_tokens: last recorded boundary position

New types:
- CacheBoundaryConfig: Enabled, MinStableTurns (default 2),
  MinPrefixTokens (default 1024), MaxMarkers (default 4)
- CacheBoundaryResult: Markers, TotalStableTokens, Advanced, Retreated
- CacheBoundaryMarker: EntryID, TokensUpToHere, StableSinceTurn
- BoundaryStats: PushCount, BoundaryTokens, StableEntryCount

Behaviour:
- RecordPush increments push_count and promotes entries that have
  survived >= MinStableTurns pushes without modification
- Evaluate walks entries in sequence order, finds stable entries whose
  cumulative token count meets MinPrefixTokens, selects up to MaxMarkers
  by highest token count, detects advance/retreat vs previous boundary
- PushResult now includes CacheBoundary *CacheBoundaryResult
- Session now exposes CacheBoundaryTokens and PushCount fields
- Config.CacheBoundary wires DefaultCacheBoundaryConfig into the store

Co-authored-by: Ona <[email protected]>
@Siddhant-K-code Siddhant-K-code force-pushed the feat/51-session-cache-boundary-manager branch from 2a00de4 to bd409eb Compare May 2, 2026 13:05
@Siddhant-K-code Siddhant-K-code merged commit 4d44256 into main May 2, 2026
2 checks passed
@Siddhant-K-code Siddhant-K-code deleted the feat/51-session-cache-boundary-manager branch May 2, 2026 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Session-aware cache boundary manager — auto-advance cache_control as sessions evolve

1 participant