Skip to content

Fix compactInFlight session leak, cache branch entry index, back off zero-chunk observer re-fires - #57

Open
kaushikvira wants to merge 3 commits into
elpapi42:masterfrom
kaushikvira:fix/compact-flag-and-perf
Open

Fix compactInFlight session leak, cache branch entry index, back off zero-chunk observer re-fires#57
kaushikvira wants to merge 3 commits into
elpapi42:masterfrom
kaushikvira:fix/compact-flag-and-perf

Conversation

@kaushikvira

Copy link
Copy Markdown

Fixes

  1. compactInFlight flag leak (real bug) — in the compaction trigger, the in-flight flag is reset only via ctx.compact()'s onComplete/onError callbacks and a catch. If the captured ctx goes stale (session replacement) and compaction neither completes nor throws, the flag leaks true for 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.

  2. Per-turn O(n) entry-index rebuilds (perf)entryIndexById rebuilt a full-branch id→index Map on every call; the per-turn consolidation trigger (agent_start/turn_end) and the agent_settled compaction 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 on master — the retry-detection block was removed when the trigger moved to agent_settled.)

  3. 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 same observerEmptyBackoff as the deliberate-empty verdict (Observer re-fires every turn (warning spam) when it returns no observations #23).

Verification

  • npm run typecheck — exit 0
  • npm test — 261/261 passing, including new regression tests for all three fixes
  • No changes to LLM stage thresholds/config defaults; no dependency changes

Addresses audit findings for the installed 3.0.4 package.

… 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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant