pi-better-subagents 0.28.1 → 0.28.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts CHANGED
@@ -1625,10 +1625,10 @@ function returnWidgetNavToInput(): void {
1625
1625
  exitWidgetNav();
1626
1626
  }
1627
1627
 
1628
- function viewWidgetNavSelection(ctx: ExtensionContext): void {
1628
+ function viewWidgetNavSelection(ctx: ExtensionContext, owner?: symbol): void {
1629
1629
  const selected = selectedWidgetNavRun();
1630
1630
  if (!selected) return;
1631
- openNavigator(ctx, selected.id);
1631
+ openNavigator(ctx, selected.id, owner);
1632
1632
  }
1633
1633
 
1634
1634
  function stopWidgetNavSelection(ctx: ExtensionContext): void {
@@ -1640,9 +1640,9 @@ function stopWidgetNavSelection(ctx: ExtensionContext): void {
1640
1640
  }
1641
1641
 
1642
1642
  /** Open the focused navigator overlay. No-op without a UI or visible runs. */
1643
- function openNavigator(ctx: ExtensionContext, initialDetailId?: string): void {
1643
+ function openNavigator(ctx: ExtensionContext, initialDetailId?: string, owner?: symbol): void {
1644
1644
  void initialDetailId;
1645
- ensureNavigator(ctx);
1645
+ ensureNavigator(ctx, owner);
1646
1646
  }
1647
1647
 
1648
1648
  /** Refresh the shared navigator after run changes. */
@@ -1651,7 +1651,7 @@ function updateNavigatorFooter(ctx: ExtensionContext | undefined): void {
1651
1651
  }
1652
1652
 
1653
1653
  /** Install the empty-editor ← wrapper once per UI (reload-safe, composable). */
1654
- function ensureNavigator(ctx: ExtensionContext): void {
1654
+ function ensureNavigator(ctx: ExtensionContext, owner?: symbol): void {
1655
1655
  if (!isNavigatorUiAvailable(ctx)) return;
1656
1656
  try {
1657
1657
  ensureSubagentProvider();
@@ -1662,7 +1662,7 @@ function ensureNavigator(ctx: ExtensionContext): void {
1662
1662
  matchKey: (data: string, keyId: string) => matchesKey(data, keyId),
1663
1663
  truncate: truncateToWidth,
1664
1664
  createTranscriptComponent: createSubagentTranscriptComponent,
1665
- });
1665
+ }, owner);
1666
1666
  } catch { /* ignore */ }
1667
1667
  }
1668
1668
 
@@ -2012,6 +2012,8 @@ export default function (pi: ExtensionAPI) {
2012
2012
  restrictContextFilesToWorktree(event.systemPromptOptions, PiCodingAgent.getAgentDir());
2013
2013
  });
2014
2014
  initializeNavigatorPanels(pi);
2015
+ // Each cached factory instance owns the navigator it installs (#460).
2016
+ const navigatorOwner = Symbol("subagents navigator factory");
2015
2017
  const agentSessionSettings = createAgentSessionSettings(pi);
2016
2018
  let catalogModelRegistry: ExtensionContext["modelRegistry"] | undefined;
2017
2019
  let delegationOverride: DelegationMode | undefined;
@@ -3932,10 +3934,10 @@ export default function (pi: ExtensionAPI) {
3932
3934
  // Adopt records left non-terminal by a pi that died without a clean
3933
3935
  // shutdown, so the age sweep can eventually retire them.
3934
3936
  reconcileAbandonedRuns();
3935
- disposeBackgroundWorkNavigator(ctx);
3937
+ disposeBackgroundWorkNavigator(ctx, navigatorOwner);
3936
3938
  widgetNavActive = false;
3937
3939
  widgetNavSelectedId = undefined;
3938
- ensureNavigator(ctx);
3940
+ ensureNavigator(ctx, navigatorOwner);
3939
3941
  publishAcceptanceHooks();
3940
3942
  updateNavigatorFooter(ctx);
3941
3943
  // Clear the retired legacy widget so the shared navigator is the only
@@ -3956,7 +3958,9 @@ export default function (pi: ExtensionAPI) {
3956
3958
  cancelCallbackBatch(pi);
3957
3959
  mainAgentStartedAt = undefined;
3958
3960
  mainAgentTools.clear();
3959
- disposeBackgroundWorkNavigator();
3961
+ // Owned disposal only: a headless child or a stale factory must not clear
3962
+ // the parent's navigator by omitting the context and owner.
3963
+ disposeBackgroundWorkNavigator(undefined, navigatorOwner);
3960
3964
  });
3961
3965
 
3962
3966
  // Tear down the timer and clear the widget when the session ends.
@@ -4008,7 +4012,7 @@ export default function (pi: ExtensionAPI) {
4008
4012
  resetParseRunCursor();
4009
4013
  // Always dispose navigator detail timers (no UI call — just clearInterval).
4010
4014
  // Safe in every mode; the dispose hook is only set when a TUI overlay opened.
4011
- disposeBackgroundWorkNavigator(ctx);
4015
+ disposeBackgroundWorkNavigator(ctx, navigatorOwner);
4012
4016
  // Widget clear is intentional in every mode that exposes ui, including RPC.
4013
4017
  try { ctx.ui.setWidget("subagents", WIDGET_CLEAR); } catch { /* ignore */ }
4014
4018
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-better-subagents",
3
- "version": "0.28.1",
3
+ "version": "0.28.2",
4
4
  "description": "Pi extension for detached, sandboxed subagent runs that keep the foreground session free.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -82,8 +82,10 @@ type HostDeps = {
82
82
 
83
83
  type NavigatorState = {
84
84
  providers: Map<string, BackgroundWorkProvider>;
85
+ registrationTokens?: Map<string, symbol>;
85
86
  unsubscribers: Map<string, () => void>;
86
87
  uiCtx?: ExtensionContext;
88
+ uiOwner?: symbol;
87
89
  deps?: HostDeps;
88
90
  lastHint?: string | null;
89
91
  lastMainListLines?: string[];
@@ -137,6 +139,8 @@ function state(): NavigatorState {
137
139
 
138
140
  export function registerBackgroundWorkProvider(provider: BackgroundWorkProvider): () => void {
139
141
  const s = state();
142
+ const token = Symbol(provider.id);
143
+ (s.registrationTokens ??= new Map()).set(provider.id, token);
140
144
  const previousUnsub = s.unsubscribers.get(provider.id);
141
145
  if (previousUnsub) {
142
146
  try { previousUnsub(); } catch { /* ignore */ }
@@ -152,7 +156,10 @@ export function registerBackgroundWorkProvider(provider: BackgroundWorkProvider)
152
156
  s.unsubscribers.delete(provider.id);
153
157
  }
154
158
  refreshBackgroundWorkNavigator();
155
- return () => unregisterBackgroundWorkProvider(provider.id);
159
+ // A stale factory must not unregister its replacement from another cached module.
160
+ return () => {
161
+ if (state().registrationTokens?.get(provider.id) === token) unregisterBackgroundWorkProvider(provider.id);
162
+ };
156
163
  }
157
164
 
158
165
  export function unregisterBackgroundWorkProvider(id: string): void {
@@ -163,6 +170,7 @@ export function unregisterBackgroundWorkProvider(id: string): void {
163
170
  }
164
171
  s.unsubscribers.delete(id);
165
172
  s.providers.delete(id);
173
+ s.registrationTokens?.delete(id);
166
174
  refreshBackgroundWorkNavigator();
167
175
  }
168
176
 
@@ -188,10 +196,11 @@ export function applyCloseConfirmFooter(ui: { setStatus(key: string, value: stri
188
196
  return hint ?? null;
189
197
  }
190
198
 
191
- export function ensureBackgroundWorkNavigator(ctx: ExtensionContext, deps: HostDeps): void {
199
+ export function ensureBackgroundWorkNavigator(ctx: ExtensionContext, deps: HostDeps, owner?: symbol): void {
192
200
  if (!isNavigatorUiAvailable(ctx)) return;
193
201
  const s = state();
194
202
  s.uiCtx = ctx;
203
+ s.uiOwner = owner;
195
204
  s.deps = {
196
205
  ...deps,
197
206
  createTranscriptComponent: deps.createTranscriptComponent ?? s.deps?.createTranscriptComponent,
@@ -207,14 +216,26 @@ export function ensureBackgroundWorkNavigator(ctx: ExtensionContext, deps: HostD
207
216
  refreshMainListWidget();
208
217
  }
209
218
 
210
- export function disposeBackgroundWorkNavigator(ctx?: ExtensionContext): void {
219
+ export function disposeBackgroundWorkNavigator(ctx?: ExtensionContext, owner?: symbol): void {
211
220
  const s = state();
221
+ if (owner !== undefined) {
222
+ // Only the factory that installed the current navigator may dispose it.
223
+ if (s.uiOwner !== owner) return;
224
+ } else if (s.uiOwner !== undefined) {
225
+ // Ownership is never inferred: an omitted owner must not clear a navigator
226
+ // another cached factory (or a headless child) owns.
227
+ return;
228
+ } else if (ctx !== undefined) {
229
+ // Unowned navigators stay releasable by a live, matching UI context.
230
+ if (!isNavigatorUiAvailable(ctx) || (s.uiCtx && ctx.ui !== s.uiCtx.ui)) return;
231
+ }
212
232
  try { s.dispose?.(); } catch { /* ignore */ }
213
233
  s.dispose = undefined;
214
234
  s.overlay = undefined;
215
235
  stopMainListWidget();
216
236
  const activeCtx = ctx ?? s.uiCtx;
217
237
  s.uiCtx = undefined;
238
+ s.uiOwner = undefined;
218
239
  if (activeCtx && isNavigatorUiAvailable(activeCtx)) {
219
240
  try { setNavigatorPanel(activeCtx, MAIN_LIST_WIDGET_KEY, undefined); } catch { /* ignore */ }
220
241
  }
@@ -11,6 +11,7 @@
11
11
  * reference channels (`continuationNote`, `reason`, `summary`, `authorityRef`)
12
12
  * that a caller could otherwise use to persist a secret.
13
13
  */
14
+ import { isAbsolute } from "node:path";
14
15
  import { StoreError } from "./errors.ts";
15
16
  import type { EvidenceKind, OwnerIdentity, OwnerKind } from "./types.ts";
16
17
 
@@ -143,7 +144,8 @@ export function requirePath(field: string, value: unknown): string {
143
144
  /** Workspace binding: an absolute path that may contain spaces. */
144
145
  export function requireWorkspace(value: unknown): string {
145
146
  const workspace = assertPlainString("owner.workspace", value, TEXT_LIMITS.workspace).trim();
146
- if (!workspace.startsWith("/")) fail("owner.workspace", "must be an absolute path");
147
+ // Platform-aware: a Windows workspace is "C:\...", which never starts with "/".
148
+ if (!isAbsolute(workspace)) fail("owner.workspace", "must be an absolute path");
147
149
  return workspace;
148
150
  }
149
151