pi-better-goal 0.13.1 → 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.1",
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",
@@ -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";
@@ -66,31 +66,135 @@ function readAttempts(options) {
66
66
  const attempts = [];
67
67
  try {
68
68
  for (const id of readdirSync(join(base, "runs"))) {
69
- try {
70
- const candidate = JSON.parse(readFileSync(join(base, "runs", id, "meta.json"), "utf8"));
71
- if (candidate && typeof candidate === "object") attempts.push(candidate);
72
- }
73
- catch { /* A partial unrelated record must not hide a continuation. */ }
69
+ const candidate = readAttempt(base, id);
70
+ if (candidate) attempts.push(candidate);
74
71
  }
75
72
  } catch { /* Registry gaps cannot establish a newer attempt. */ }
76
73
  return attempts;
77
74
  }
78
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
+
79
187
  /** One synchronous UI refresh shares a snapshot; the next refresh reads disk again. */
80
188
  export function createAssignmentProjector(options = {}) {
81
- let snapshot;
189
+ let prepared;
190
+ const snapshots = new Map();
82
191
  const readSnapshot = (meta) => {
83
- if (!snapshot) {
84
- snapshot = new Map();
85
- for (const candidate of readAttempts(options)) {
86
- let workspaces = snapshot.get(candidate.sessionId);
87
- if (!workspaces) snapshot.set(candidate.sessionId, workspaces = new Map());
88
- let attempts = workspaces.get(candidate.cwd);
89
- if (!attempts) workspaces.set(candidate.cwd, attempts = []);
90
- attempts.push(candidate);
91
- }
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));
92
196
  }
93
- return snapshot.get(meta.sessionId)?.get(meta.cwd) ?? [];
197
+ return snapshots.get(key);
94
198
  };
95
199
  return (meta, processStatus = meta.status) => assignmentFromSnapshot(meta, processStatus, options,
96
200
  () => readSnapshot(meta));