Fix compactInFlight session leak, cache branch entry index, back off zero-chunk observer re-fires - #57
Open
kaushikvira wants to merge 3 commits into
Open
Conversation
… flag leak When the captured extension ctx goes stale (session replacement/reload), a started ctx.compact() may neither complete nor throw, leaving runtime.compactInFlight stuck true and disabling proactive compaction for the process lifetime. Record the session identity (session id, falling back to session file) with the flag and discard a stale flag whose generation no longer matches the current session.
entryIndexById rebuilt a full-branch id->index Map on every call, which the per-turn consolidation trigger (agent_start/turn_end) and the compaction trigger (agent_settled) hit 3-4 times per event. Cache the Map keyed by branch tip + length: the branch only grows by appends, so a changed tip or length is the invalidation signal. Behavior is identical; callers read the Map only.
A backlog that serializes to an empty chunk (nothing renderable after coverage) returned 'continue' without arming the deliberate-empty backoff, so the observer pipeline re-ran every turn over the same span. Arm the same observerEmptyBackoff as the deliberate-empty verdict (elpapi42#23).
kaushikvira
marked this pull request as ready for review
August 23, 2026 01:37
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.
Fixes
compactInFlightflag leak (real bug) — in the compaction trigger, the in-flight flag is reset only viactx.compact()'sonComplete/onErrorcallbacks and a catch. If the captured ctx goes stale (session replacement) and compaction neither completes nor throws, the flag leakstruefor the process lifetime and proactive compaction is permanently disabled. The flag is now keyed to a session generation token (session id, falling back to session file); a stale flag whose generation no longer matches is discarded (with a UI note) instead of silently blocking.Per-turn O(n) entry-index rebuilds (perf) —
entryIndexByIdrebuilt a full-branch id→index Map on every call; the per-turn consolidation trigger (agent_start/turn_end) and theagent_settledcompaction trigger hit it 3-4 times per event. The Map is now cached keyed by branch tip + length (the branch only grows by appends, so tip/length change is the invalidation signal). Callers are read-only, behavior is identical. (The[...messages].reverse()copy called out in the audit no longer exists onmaster— the retry-detection block was removed when the trigger moved toagent_settled.)Observer zero-chunk re-fire (nit) — in the consolidation trigger, the empty-chunk path (
!chunk.trim()) returned "continue" without arming the deliberate-empty backoff, so a zero-chunk backlog re-fired the observer pipeline every turn. It now arms the sameobserverEmptyBackoffas the deliberate-empty verdict (Observer re-fires every turn (warning spam) when it returns no observations #23).Verification
npm run typecheck— exit 0npm test— 261/261 passing, including new regression tests for all three fixesAddresses audit findings for the installed 3.0.4 package.