pi-better-subagents 0.32.0 → 0.32.1

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.
@@ -9,6 +9,7 @@ export interface AssignmentProjection {
9
9
  verified: boolean;
10
10
  }
11
11
  export function assignmentFor(meta: object, processStatus?: string, options?: { baseDir?: string | undefined; coordinatorPath?: string; observation?: object }): AssignmentProjection;
12
+ export function createAssignmentProjector(options?: { baseDir?: string | undefined; coordinatorPath?: string; observation?: object }): (meta: object, processStatus?: string) => AssignmentProjection;
12
13
  export function projectAssignment(meta: object, processStatus: string, observation?: object): AssignmentProjection;
13
14
  export function assignmentDiagnostic(assignment: AssignmentProjection): string;
14
15
  export const ASSIGNMENT_STATES: string[];
@@ -57,18 +57,53 @@ export function projectAssignment(meta, processStatus, observation = { verified:
57
57
 
58
58
  /** All consumers use the same lineage and authoritative wait read. Park caches are hints only. */
59
59
  export function assignmentFor(meta, processStatus = meta.status, options = {}) {
60
+ return assignmentFromSnapshot(meta, processStatus, options, () => readAttempts(options));
61
+ }
62
+
63
+ function readAttempts(options) {
60
64
  const base = options.baseDir ?? join(tmpdir(), "pi-better-subagents");
61
- const attempts = new Map([[meta.id, meta]]);
62
- let current = meta;
65
+ const attempts = [];
63
66
  try {
64
- for (const id of (meta.operations || meta.park ? readdirSync(join(base, "runs")) : [])) {
67
+ for (const id of readdirSync(join(base, "runs"))) {
65
68
  try {
66
69
  const candidate = JSON.parse(readFileSync(join(base, "runs", id, "meta.json"), "utf8"));
67
- if (candidate.sessionId === meta.sessionId && candidate.cwd === meta.cwd
68
- && (!meta.assignment || candidate.assignment?.assignmentId === meta.assignment.assignmentId)) attempts.set(candidate.id, candidate);
69
- } catch { /* A partial unrelated record must not hide a continuation. */ }
70
+ if (candidate && typeof candidate === "object") attempts.push(candidate);
71
+ }
72
+ catch { /* A partial unrelated record must not hide a continuation. */ }
70
73
  }
71
74
  } catch { /* Registry gaps cannot establish a newer attempt. */ }
75
+ return attempts;
76
+ }
77
+
78
+ /** One synchronous UI refresh shares a snapshot; the next refresh reads disk again. */
79
+ export function createAssignmentProjector(options = {}) {
80
+ let snapshot;
81
+ const readSnapshot = (meta) => {
82
+ if (!snapshot) {
83
+ snapshot = new Map();
84
+ for (const candidate of readAttempts(options)) {
85
+ let workspaces = snapshot.get(candidate.sessionId);
86
+ if (!workspaces) snapshot.set(candidate.sessionId, workspaces = new Map());
87
+ let attempts = workspaces.get(candidate.cwd);
88
+ if (!attempts) workspaces.set(candidate.cwd, attempts = []);
89
+ attempts.push(candidate);
90
+ }
91
+ }
92
+ return snapshot.get(meta.sessionId)?.get(meta.cwd) ?? [];
93
+ };
94
+ return (meta, processStatus = meta.status) => assignmentFromSnapshot(meta, processStatus, options,
95
+ () => readSnapshot(meta));
96
+ }
97
+
98
+ function assignmentFromSnapshot(meta, processStatus, options, readSnapshot) {
99
+ const attempts = new Map([[meta.id, meta]]);
100
+ let current = meta;
101
+ if (meta.operations || meta.park) {
102
+ for (const candidate of readSnapshot()) {
103
+ if (candidate.sessionId === meta.sessionId && candidate.cwd === meta.cwd
104
+ && (!meta.assignment || candidate.assignment?.assignmentId === meta.assignment.assignmentId)) attempts.set(candidate.id, candidate);
105
+ }
106
+ }
72
107
  // Conversation roots survive explicit follow-ups. Only automatic resume edges
73
108
  // join attempts into one assignment; manual continuation starts a new one.
74
109
  const lineage = new Map([[meta.id, meta]]);
package/index.ts CHANGED
@@ -11,7 +11,7 @@
11
11
  * launch is the result · completion posts back · the foreground never blocks
12
12
  */
13
13
 
14
- import { assignmentFor } from "./assignment-projection.mjs";
14
+ import { createAssignmentProjector } from "./assignment-projection.mjs";
15
15
  import { historicalWaits, attentionRevision, attentionDismissed, dismissAttention } from "./historical-attention.ts";
16
16
  import { registerAssignmentAttempt, commitAssignmentTerminal, readOutcome, receiptOutcome, outcomeTranscriptReceipt, inspectOutcome, createOutcomeHost, closeOutcomeHost, claimOutcome, type AssignmentBinding } from "./assignment-outcomes.ts";
17
17
  import { currentSandboxPermissions } from "./permission-policy.ts";
@@ -1391,7 +1391,8 @@ function navigatorRows(visible?: RunMeta[], at?: number) {
1391
1391
 
1392
1392
  /** Runs that should advertise/open the left-arrow navigator affordance. */
1393
1393
  function navigatorRunningRuns(): RunMeta[] {
1394
- return sessionVisibleNavigatorRuns().filter((m) => ["running", "waiting"].includes(assignmentFor(m, effectiveStatus(m)).state));
1394
+ const project = createAssignmentProjector();
1395
+ return sessionVisibleNavigatorRuns(Date.now(), project).filter((m) => ["running", "waiting"].includes(project(m, effectiveStatus(m)).state));
1395
1396
  }
1396
1397
 
1397
1398
  function navigatorRunningCount(): number {
@@ -1403,16 +1404,16 @@ function navigatorRunningCount(): number {
1403
1404
  * each rebuild still reads owned non-terminal metadata so external status
1404
1405
  * changes are visible immediately without a time-based memo.
1405
1406
  */
1406
- function sessionVisibleNavigatorRuns(now: number = Date.now()): RunMeta[] {
1407
+ function sessionVisibleNavigatorRuns(now: number = Date.now(), project = createAssignmentProjector()): RunMeta[] {
1407
1408
  const origin = activeCallbackOrigin;
1408
1409
  if (!origin) return [];
1409
1410
  return navigatorVisibleRuns(listMetasForOrigin(origin))
1410
1411
  .filter(belongsToActiveNavigatorSession)
1411
- .filter((m) => !isExpiredTerminalNavigatorRun(m, now));
1412
+ .filter((m) => !isExpiredTerminalNavigatorRun(m, now, project));
1412
1413
  }
1413
1414
 
1414
- function isExpiredTerminalNavigatorRun(meta: RunMeta, now: number): boolean {
1415
- const status = assignmentFor(meta, effectiveStatus(meta)).state;
1415
+ function isExpiredTerminalNavigatorRun(meta: RunMeta, now: number, project = createAssignmentProjector()): boolean {
1416
+ const status = project(meta, effectiveStatus(meta)).state;
1416
1417
  if (!isTerminalNavigatorStatus(status)) return false;
1417
1418
  const endedAt = meta.endedAt ?? meta.lostAt;
1418
1419
  return typeof endedAt === "number" && now - endedAt >= TERMINAL_NAVIGATOR_RETENTION_MS;
package/navigator.mjs CHANGED
@@ -24,7 +24,7 @@
24
24
  * with visible-width-aware truncation.
25
25
  */
26
26
 
27
- import { assignmentFor } from "./assignment-projection.mjs";
27
+ import { assignmentFor, createAssignmentProjector } from "./assignment-projection.mjs";
28
28
  import {
29
29
  formatNavigatorHealthFacts,
30
30
  statusThemeColor,
@@ -190,8 +190,9 @@ export function pendingNavigatorPark(meta) {
190
190
 
191
191
  export function buildNavigatorRows(metas, deps) {
192
192
  const now = deps.now ?? Date.now();
193
+ const project = createAssignmentProjector();
193
194
  return metas.map((m) => {
194
- const assignment = assignmentFor(m, deps.effectiveStatus(m));
195
+ const assignment = project(m, deps.effectiveStatus(m));
195
196
  const status = assignment.state;
196
197
  const health = typeof deps.healthFor === "function" ? deps.healthFor(m) : undefined;
197
198
  const effort = typeof deps.effortFor === "function" ? deps.effortFor(m) : (m.effort ?? m.modelEffort);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-better-subagents",
3
- "version": "0.32.0",
3
+ "version": "0.32.1",
4
4
  "description": "Pi extension for detached, sandboxed subagent runs that keep the foreground session free.",
5
5
  "license": "MIT",
6
6
  "type": "module",