Skip to content

fix(inject): bound per-session state in a long-lived server - #77

Merged
abogartz merged 2 commits into
abogartz:mainfrom
WhollyMindless:fix/session-cap
Oct 24, 2026
Merged

abogartz merged 2 commits into
abogartz:mainfrom
WhollyMindless:fix/session-cap

Conversation

@WhollyMindless

@WhollyMindless WhollyMindless commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #75. This is the bounded-maps item from your #65 review. #65 merged before I pushed it, and since #65's latestPrompt made the leak worse, I'd like to close it promptly.

The problem: see #75. epoch, latestPrompt, sessionStats and measuredTurns in src/index.ts hold one entry per session for the life of the server, and they're only cleared at compaction. latestPrompt holds full prompt text.

Change:

  • New src/sessions.ts: SessionLru with SESSION_CAP = 64, the same cap as repokey.ts.
  • One recency list owns eviction, and evicting a session deletes it from all four maps together.
  • It's touched in the prompt hook, the context hook and the message.updated handler, before anything is inserted. memory_status can't recreate an entry for an evicted session, because buildPayload returns before statsFor when there's no epoch. Compaction only deletes.
  • Semantics: an evicted session gets no relevance block on its next request, and on its next prompt a fresh reason=first epoch. It never gets stale state. The cost is one recompute for a session that sat idle while 64 others were active.
  • systemDigest's signature isn't touched here. fix(inject): hold the frontier block and contextual pulls to injectBudget #66 converts it to the options object.

Tests:

  • test/sessions.test.ts: SessionLru honours its cap and LRU order, and calls onEvict once per eviction.
  • test/epoch.test.ts, through the real prompt/context hooks: after SESSION_CAP + 1 sessions, the first session gets no block on its next request and a second first epoch on its next prompt. A session that kept being touched keeps its byte-identical block.
  • The existing unchanged-store byte-identity test is still green.

With the eviction removed, both new tests fail.

Checks (Linux, Bun 1.4.4, on 0.8.4): bun test ./test/ 304 pass, 4 skip, 0 fail. bun run test:e2e against opencode 2.0.26: 9 pass. tsc --noEmit is clean, and biome lint exits 0 with warnings unchanged from main. Nothing here is platform-specific.

Merge order: independent of #62, #66, #67 and #76. I merged all of them together on 0.8.4 and they apply cleanly.

🤖 Generated with Claude Code

One 64-session LRU (same cap as repokey.ts) evicts epoch, latestPrompt,
sessionStats and measuredTurns together. It is touched in the prompt and
context hooks and in message.updated. An evicted session gets no relevance
block until its next prompt, then a fresh "first" epoch, so it never sees
stale state. Follow-up to abogartz#65, where latestPrompt made the leak worse.

Closes abogartz#75.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Verified independently on the merged batch (all five applied cleanly onto 0.8.4):

  • SessionLru semantics are exactly right, and I caught my own probe being wrong on the first pass, which is the best kind of confirmation: at cap=3 with the set already full, touch(a) evicts b immediately — the eviction loop is strict and never runs one behind.
  • One owner for eviction over all four maps, touched in all three entry points (prompt hook, context hook, message.updated) before anything inserts. I checked every write site of epoch / latestPrompt / sessionStats / measuredTurns individually: epoch.set and latestPrompt.set are downstream of touch, and the only statsFor call that could insert without a touch is inside buildPayload, which returns early when no epoch exists — so an evicted session cannot leave an orphan stats entry behind.
  • The semantics paragraph is the right trade: one recompute for a session idle behind 64 active ones, never stale state. For opencode serve residency that is the whole ballgame, and pairing with repokey.ts's cap is the right consistency.

Combined-batch gates: 324 pass / 4 skip / 0 fail, tsc --noEmit clean, biome exit 0.

@abogartz
abogartz merged commit 0177d22 into abogartz:main Oct 24, 2026
5 of 6 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.8.6 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

Per-session caches (epoch, latestPrompt, sessionStats) grow unbounded in a long-lived server

2 participants