fix(memory): bound in-memory per-session tracking against unclean session ends#64
Merged
alexgreensh merged 1 commit intoJun 16, 2026
Merged
Conversation
…sion ends Why: three continuity/fresh-nudge collections in index.ts (_continuityInjectedSessions, _freshNudgeFiredSessions, _freshNudgePriorScores) and the states Map in checkpoint-policy.ts are pruned on session:end. A session that ends uncleanly (process killed, network disconnect) never fires that event, so its entries leak for the gateway's entire lifetime. On an always-on gateway these grow without bound. What: add oldest-first eviction caps (MAX_TRACKED_SESSIONS = 2000). index.ts gets boundedSetAdd()/boundedMapSet() helpers (the Map variant refreshes recency on update so an actively-scored session is not evicted mid-use); checkpoint- policy.ts gets rememberState(). Evicting a checkpoint state is safe -- every mutation is written through to disk by persistState(), and getCheckpointState() re-hydrates an evicted entry from disk on the next access. The session:end cleanup is unchanged and remains the normal release path; the cap is only a backstop for sessions that never get one. How: verified the eviction algorithm -- inserting 2500 sessions caps size at 2000, evicts the oldest, retains the newest, and a refreshed key survives a later eviction wave. tsc build green; dist rebuilt.
Owner
|
Merged in v5.11.13 — good catch on the map growth when sessions end uncleanly. Thanks, Dani. |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Why
Three continuity/fresh-nudge collections in
index.ts(_continuityInjectedSessions,_freshNudgeFiredSessions,_freshNudgePriorScores) and thestatesMap incheckpoint-policy.tsare pruned onsession:end. A session that ends uncleanly (process killed, network disconnect) never fires that event, so its entries leak for the gateway's entire lifetime. On an always-on gateway these grow without bound.What
Add oldest-first eviction caps (
MAX_TRACKED_SESSIONS = 2000):index.tsgetsboundedSetAdd()/boundedMapSet()helpers. The Map variant refreshes recency on update (_freshNudgePriorScoresis re-set every patch, so this keeps an actively-scored session from being evicted). The Set guards are add-once fire-guards, so they intentionally do not refresh — a refresh would be dead code.checkpoint-policy.tsgetsrememberState().session:endcleanup is unchanged and remains the normal release path; the cap is only a backstop for sessions that never get one.Evicting a checkpoint state is safe: the dedup-critical fields (
capturedFillBands,capturedQualityThresholds,capturedMilestones,lastCheckpointAt) are written through to disk on everyrecordCheckpointDecision(), andgetCheckpointState()re-hydrates an evicted entry from disk — so a band/threshold/milestone is never re-captured. The transient counters (editWriteCount,editedFiles,lastEvaluatedAt) are not persisted; evicting an uncheckpointed session resets them, which only delays a milestone-edit-batch checkpoint (conservative, never spurious). These trade-offs are documented in the code.How (verified)
Verified the eviction algorithm: inserting 2500 sessions caps size at 2000, evicts the oldest, retains the newest, and a refreshed key survives a later eviction wave.
tscbuild is green; committeddist/rebuilt.Deliberately not addressed
The cap is hardcoded (not a new
TOKEN_OPTIMIZER_*env knob) and the eviction helper is not extracted to a shared module — both kept out to keep this a minimal, focused backstop rather than an API/structure change.