Repository navigation
fix(pi-subagents): reclaim idle delegate-notification hosts from the enumeration registry (#2390) - #2392
Conversation
[bot] 🏷 已核对 PR #2392 分支( 结构化八问:
需要 owner 知晓的一点(非缺陷):核心 判定:方向对、实现最小化、测试有牙、行为变更已披露(清理时机从「永不清理」→「空闲后一个宏任务清理,再次访问即重新注册」,投递语义零变化)。无拦截项;待 #2389 落地后 rebase + 核心 CI 绿即可合。 |
d07c13a to
b3c4039
Compare
…enumeration registry (#2390) knownHosts (the strong-ref set a target-less flush enumerates) had no removal path, so every session's host was pinned forever in a long-lived process. Prune a host from enumeration once it is fully idle (no in-flight runs, empty coalescing queue, no pending flush) and re-add it on every state access; the per-host state object itself is never deleted, so captured handlers cannot desync. Deterministic regression test asserts the registry count returns to baseline after idle hosts settle.
657479e to
cecabf0
Compare
[bot] 🏷 结构化八问:
判定:冲突已解, |
9714145
into
2026-10-08_delegate-notify-boundary
Closes #2390
Stacked on #2389. Base is #2389's branch (
2026-10-08_delegate-notify-boundary) because the registry this fixes (knownHosts/hostNotificationStatesinpi-subagents/src/delegate-tool.ts) only exists there — it is not yet onmaster. Stacking keeps #2389 fully mergeable on its own (per "低优先级,不阻塞 PR #2389 合并") and keeps this diff isolated to one concern. After #2389 merges, rebase this ontomaster.What / why
#2389 introduced per-host notification state keyed off the host: a
WeakMap<ExtensionAPI, HostNotificationState>(weak keys — not a leak) plus a strong-refSet<ExtensionAPI> knownHostsused so a target-lessflushDelegateNotifications()can enumerate hosts. That set had no removal path, so every session's host was pinned forever in a long-lived process (TUI/new, RPC multi-session) — monotonic growth proportional to sessions created. No correctness impact (flushing a dead host is a no-op), purely memory.Confirmed against #2389 head
d07c13ab: exactly threeknownHostsreferences — declaration,.add()inside the getter, andArray.from(knownHosts)for enumeration — and zero.delete()/.clear(). So the set could only grow.Fix (a refinement of the issue's option 1)
The issue's option 1 proposed deleting from both tables plus refactoring every state access through the getter so handlers stop capturing
st(to avoid queue split-brain). That full refactor is unnecessary here: the split-brain risk comes from deleting the state object, not from deleting the enumeration entry. So this fix prunes only the enumeration set and never touches the WeakMap state:pruneKnownHostIfIdle(pi)drops a host fromknownHostsonly when it is fully idle —activeRuns === 0 && queue.length === 0 && !flushScheduled. It reads the state map directly (not the creating getter, whose re-add would undo the delete).knownHosts.add(pi)out of theif(!st)block), so a pruned-idle host rejoins enumeration the instant it is touched again — a later target-less flush cannot miss its pending delivery.maybeFlushsetsflushScheduled=truesynchronously on settle, the effective prune is the one in the timeout callback (next macrotask, after the flush drains the queue).Since the per-host state object lives as long as the host does (WeakMap) and is never recreated, a handler that captured it at registration can never desync — the safety goal of option 1 is met without the getter-indirection rewrite, for less churn and lower regression risk.
Behavior change (flagged)
knownHostsentries went from never removed to removed once the host is idle (one macrotask after its settle/flush). A host re-registers on next access. No change to what is delivered or when — delivery still goes through the same settle/context/idle tiers, and the globalfindUndeliveredRunsrecovery is unchanged and host-agnostic.New surface
knownDelegateHostCount(): number— a test/diagnostic hook asserting the boundedness guarantee. Not user-facing, not a config field. No config/env/CLI surface added.Component version
pi-subagents/package.jsonbumped0.1.3 → 0.1.4(Pi-Subagents Boundary rule 1: anysrc/change must bump the component version; CIpi-subagents-guardenforces it). Rootpackage.jsonuntouched.Tests
pi-subagents/tests/delegate-host-reclaim.test.ts(no--expose-gcneeded): asserts the registry count returns to baseline after N idle hosts settle (fails on the pre-fix no-prune code), a pruned host rejoins enumeration on access and still delivers, plus per-host isolation and target-less enumeration regression guards.tsc --noEmitclean; targeted notification-path tests green (delegate-host-reclaim / delegate-settle-notify / delegate-context-notify / delegate-tool — 114 pass, 0 fail). Full suite + e2e left to CI.