pi-better-goal 0.13.0 → 0.13.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-better-goal",
3
- "version": "0.13.0",
3
+ "version": "0.13.2",
4
4
  "description": "Pi extension for goal tracking with background-aware continuation.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -10,6 +10,7 @@ export interface AssignmentProjection {
10
10
  verified: boolean;
11
11
  }
12
12
  export function assignmentFor(meta: object, processStatus?: string, options?: { baseDir?: string | undefined; coordinatorPath?: string; observation?: object }): AssignmentProjection;
13
+ export function createAssignmentProjector(options?: { baseDir?: string | undefined; coordinatorPath?: string; observation?: object }): (meta: object, processStatus?: string) => AssignmentProjection;
13
14
  export function projectAssignment(meta: object, processStatus: string, observation?: object): AssignmentProjection;
14
15
  export function assignmentDiagnostic(assignment: AssignmentProjection): string;
15
16
  export const ASSIGNMENT_STATES: string[];
@@ -1,6 +1,6 @@
1
1
  // Generated from packages/pi-better-subagents/assignment-projection.mjs. Do not edit directly.
2
2
  import { createHash } from "node:crypto";
3
- import { readFileSync, readdirSync } from "node:fs";
3
+ import { readFileSync, readdirSync, statSync } from "node:fs";
4
4
  import { homedir, tmpdir } from "node:os";
5
5
  import { join } from "node:path";
6
6
  import { readAssignmentWaits } from "./shared-assignment-observation.mjs";
@@ -58,18 +58,157 @@ export function projectAssignment(meta, processStatus, observation = { verified:
58
58
 
59
59
  /** All consumers use the same lineage and authoritative wait read. Park caches are hints only. */
60
60
  export function assignmentFor(meta, processStatus = meta.status, options = {}) {
61
+ return assignmentFromSnapshot(meta, processStatus, options, () => readAttempts(options));
62
+ }
63
+
64
+ function readAttempts(options) {
61
65
  const base = options.baseDir ?? join(tmpdir(), "pi-better-subagents");
62
- const attempts = new Map([[meta.id, meta]]);
63
- let current = meta;
66
+ const attempts = [];
64
67
  try {
65
- for (const id of (meta.operations || meta.park ? readdirSync(join(base, "runs")) : [])) {
66
- try {
67
- const candidate = JSON.parse(readFileSync(join(base, "runs", id, "meta.json"), "utf8"));
68
- if (candidate.sessionId === meta.sessionId && candidate.cwd === meta.cwd
69
- && (!meta.assignment || candidate.assignment?.assignmentId === meta.assignment.assignmentId)) attempts.set(candidate.id, candidate);
70
- } catch { /* A partial unrelated record must not hide a continuation. */ }
68
+ for (const id of readdirSync(join(base, "runs"))) {
69
+ const candidate = readAttempt(base, id);
70
+ if (candidate) attempts.push(candidate);
71
71
  }
72
72
  } catch { /* Registry gaps cannot establish a newer attempt. */ }
73
+ return attempts;
74
+ }
75
+
76
+ function readAttempt(base, id) {
77
+ try {
78
+ const candidate = JSON.parse(readFileSync(join(base, "runs", id, "meta.json"), "utf8"));
79
+ return candidate && typeof candidate === "object" ? candidate : undefined;
80
+ } catch { /* A partial unrelated record must not hide a continuation. */ }
81
+ }
82
+
83
+ const attemptIndexes = new Map();
84
+ const UNCOMMITTED_RETRY_MS = 1_000;
85
+ const conversationKey = (sessionId, cwd) => JSON.stringify([sessionId, cwd]);
86
+
87
+ function removeIdentity(index, id) {
88
+ const key = index.identities.get(id);
89
+ if (key === undefined) return;
90
+ index.identities.delete(id);
91
+ const group = index.byConversation.get(key);
92
+ group.delete(id);
93
+ if (!group.size) index.byConversation.delete(key);
94
+ }
95
+
96
+ function setIdentity(index, id, candidate) {
97
+ const key = conversationKey(candidate.sessionId, candidate.cwd);
98
+ if (index.identities.get(id) === key) return;
99
+ removeIdentity(index, id);
100
+ index.identities.set(id, key);
101
+ let group = index.byConversation.get(key);
102
+ if (!group) index.byConversation.set(key, group = new Set());
103
+ group.add(id);
104
+ }
105
+
106
+ function runsSignature(base) {
107
+ try {
108
+ const stat = statSync(join(base, "runs"), { bigint: true });
109
+ return `${stat.dev}:${stat.ino}:${stat.size}:${stat.mtimeNs}:${stat.ctimeNs}`;
110
+ } catch { return undefined; }
111
+ }
112
+
113
+ function prepareAttemptIndex(options) {
114
+ const base = options.baseDir ?? join(tmpdir(), "pi-better-subagents");
115
+ const signature = runsSignature(base);
116
+ if (!signature) {
117
+ attemptIndexes.delete(base);
118
+ return { index: { base, byConversation: new Map() } };
119
+ }
120
+ let index = attemptIndexes.get(base);
121
+ let initialAttempts;
122
+ if (!index) {
123
+ let ids;
124
+ try { ids = new Set(readdirSync(join(base, "runs"))); }
125
+ catch { return { index: { base, byConversation: new Map() } }; }
126
+ index = { base, signature, ids, identities: new Map(), byConversation: new Map(),
127
+ uncommitted: new Set(), nextRetryAt: Date.now() + UNCOMMITTED_RETRY_MS };
128
+ initialAttempts = new Map();
129
+ for (const id of ids) {
130
+ const candidate = readAttempt(base, id);
131
+ if (candidate) {
132
+ setIdentity(index, id, candidate);
133
+ initialAttempts.set(id, candidate);
134
+ }
135
+ else index.uncommitted.add(id);
136
+ }
137
+ attemptIndexes.set(base, index);
138
+ if (attemptIndexes.size > 8) attemptIndexes.delete(attemptIndexes.keys().next().value);
139
+ } else if (index.signature !== signature) {
140
+ let ids;
141
+ try { ids = new Set(readdirSync(join(base, "runs"))); }
142
+ catch {
143
+ attemptIndexes.delete(base);
144
+ return { index: { base, byConversation: new Map() } };
145
+ }
146
+ for (const id of index.ids) {
147
+ if (ids.has(id)) continue;
148
+ removeIdentity(index, id);
149
+ index.uncommitted.delete(id);
150
+ }
151
+ for (const id of ids) {
152
+ if (index.ids.has(id)) continue;
153
+ const candidate = readAttempt(base, id);
154
+ if (candidate) setIdentity(index, id, candidate);
155
+ else index.uncommitted.add(id);
156
+ }
157
+ index.ids = ids;
158
+ index.signature = signature;
159
+ }
160
+ if (Date.now() >= index.nextRetryAt) {
161
+ for (const id of index.uncommitted) {
162
+ const candidate = readAttempt(base, id);
163
+ if (!candidate) continue;
164
+ setIdentity(index, id, candidate);
165
+ index.uncommitted.delete(id);
166
+ }
167
+ index.nextRetryAt = Date.now() + UNCOMMITTED_RETRY_MS;
168
+ }
169
+ return { index, initialAttempts };
170
+ }
171
+
172
+ function readConversationAttempts(index, meta, initialAttempts) {
173
+ const matches = [];
174
+ for (const id of index.byConversation.get(conversationKey(meta.sessionId, meta.cwd)) ?? []) {
175
+ const fresh = initialAttempts?.get(id) ?? readAttempt(index.base, id);
176
+ if (!fresh) {
177
+ removeIdentity(index, id);
178
+ index.uncommitted.add(id);
179
+ continue;
180
+ }
181
+ setIdentity(index, id, fresh);
182
+ if (fresh.sessionId === meta.sessionId && fresh.cwd === meta.cwd) matches.push(fresh);
183
+ }
184
+ return matches;
185
+ }
186
+
187
+ /** One synchronous UI refresh shares a snapshot; the next refresh reads disk again. */
188
+ export function createAssignmentProjector(options = {}) {
189
+ let prepared;
190
+ const snapshots = new Map();
191
+ const readSnapshot = (meta) => {
192
+ const key = conversationKey(meta.sessionId, meta.cwd);
193
+ if (!snapshots.has(key)) {
194
+ prepared ??= prepareAttemptIndex(options);
195
+ snapshots.set(key, readConversationAttempts(prepared.index, meta, prepared.initialAttempts));
196
+ }
197
+ return snapshots.get(key);
198
+ };
199
+ return (meta, processStatus = meta.status) => assignmentFromSnapshot(meta, processStatus, options,
200
+ () => readSnapshot(meta));
201
+ }
202
+
203
+ function assignmentFromSnapshot(meta, processStatus, options, readSnapshot) {
204
+ const attempts = new Map([[meta.id, meta]]);
205
+ let current = meta;
206
+ if (meta.operations || meta.park) {
207
+ for (const candidate of readSnapshot()) {
208
+ if (candidate.sessionId === meta.sessionId && candidate.cwd === meta.cwd
209
+ && (!meta.assignment || candidate.assignment?.assignmentId === meta.assignment.assignmentId)) attempts.set(candidate.id, candidate);
210
+ }
211
+ }
73
212
  // Conversation roots survive explicit follow-ups. Only automatic resume edges
74
213
  // join attempts into one assignment; manual continuation starts a new one.
75
214
  const lineage = new Map([[meta.id, meta]]);
package/src/subagents.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { assignmentFor } from "./shared-assignment-projection.mjs";
1
+ import { createAssignmentProjector } from "./shared-assignment-projection.mjs";
2
2
  import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
3
3
  import { tmpdir } from "node:os";
4
4
  import { join } from "node:path";
@@ -127,8 +127,9 @@ export function subagentStatusToItem(
127
127
  meta: SubagentRunMeta,
128
128
  status: SubagentEffectiveStatus,
129
129
  baseDir?: string,
130
+ project = createAssignmentProjector({ baseDir }),
130
131
  ): BackgroundWorkItem {
131
- const assignment = assignmentFor(meta, status, { baseDir });
132
+ const assignment = project(meta, status);
132
133
  const active = assignment.state === "running" || assignment.state === "waiting";
133
134
  const unhealthy = status === "orphaned";
134
135
  const terminal = !active;
@@ -167,10 +168,11 @@ export function collectSubagentActivity(
167
168
  const parentPid = options.parentPid ?? process.pid;
168
169
  const processExists = options.processExists ?? defaultProcessExists;
169
170
  const metas = readParentSubagentMetas(options.baseDir, parentPid);
171
+ const project = createAssignmentProjector({ baseDir: options.baseDir });
170
172
 
171
173
  return {
172
174
  providerId: "subagents",
173
175
  label: "Subagents",
174
- items: metas.map((meta) => subagentStatusToItem(meta, effectiveSubagentStatus(meta, processExists), options.baseDir)),
176
+ items: metas.map((meta) => subagentStatusToItem(meta, effectiveSubagentStatus(meta, processExists), options.baseDir, project)),
175
177
  };
176
- }
178
+ }