pi-better-goal 0.13.1 → 0.13.3
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 +1 -1
- package/src/shared-assignment-projection.mjs +115 -23
package/package.json
CHANGED
|
@@ -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,39 +58,131 @@ 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
|
|
61
|
+
return createAssignmentProjector(options)(meta, processStatus);
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
function
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
function readAttempt(base, id) {
|
|
65
|
+
try {
|
|
66
|
+
const candidate = JSON.parse(readFileSync(join(base, "runs", id, "meta.json"), "utf8"));
|
|
67
|
+
return candidate && typeof candidate === "object" ? candidate : undefined;
|
|
68
|
+
} catch { /* A partial unrelated record must not hide a continuation. */ }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const attemptIndexes = new Map();
|
|
72
|
+
const UNCOMMITTED_RETRY_MS = 1_000;
|
|
73
|
+
const conversationKey = (sessionId, cwd) => JSON.stringify([sessionId, cwd]);
|
|
74
|
+
|
|
75
|
+
function removeIdentity(index, id) {
|
|
76
|
+
const key = index.identities.get(id);
|
|
77
|
+
if (key === undefined) return;
|
|
78
|
+
index.identities.delete(id);
|
|
79
|
+
const group = index.byConversation.get(key);
|
|
80
|
+
group.delete(id);
|
|
81
|
+
if (!group.size) index.byConversation.delete(key);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function setIdentity(index, id, candidate) {
|
|
85
|
+
const key = conversationKey(candidate.sessionId, candidate.cwd);
|
|
86
|
+
if (index.identities.get(id) === key) return;
|
|
87
|
+
removeIdentity(index, id);
|
|
88
|
+
index.identities.set(id, key);
|
|
89
|
+
let group = index.byConversation.get(key);
|
|
90
|
+
if (!group) index.byConversation.set(key, group = new Set());
|
|
91
|
+
group.add(id);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function runsSignature(base) {
|
|
67
95
|
try {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
96
|
+
const stat = statSync(join(base, "runs"), { bigint: true });
|
|
97
|
+
return `${stat.dev}:${stat.ino}:${stat.size}:${stat.mtimeNs}:${stat.ctimeNs}`;
|
|
98
|
+
} catch { return undefined; }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function prepareAttemptIndex(options) {
|
|
102
|
+
const base = options.baseDir ?? join(tmpdir(), "pi-better-subagents");
|
|
103
|
+
const signature = runsSignature(base);
|
|
104
|
+
if (!signature) {
|
|
105
|
+
attemptIndexes.delete(base);
|
|
106
|
+
return { index: { base, byConversation: new Map() } };
|
|
107
|
+
}
|
|
108
|
+
let index = attemptIndexes.get(base);
|
|
109
|
+
let initialAttempts;
|
|
110
|
+
if (!index) {
|
|
111
|
+
let ids;
|
|
112
|
+
try { ids = new Set(readdirSync(join(base, "runs"))); }
|
|
113
|
+
catch { return { index: { base, byConversation: new Map() } }; }
|
|
114
|
+
index = { base, signature, ids, identities: new Map(), byConversation: new Map(),
|
|
115
|
+
uncommitted: new Set(), nextRetryAt: Date.now() + UNCOMMITTED_RETRY_MS };
|
|
116
|
+
initialAttempts = new Map();
|
|
117
|
+
for (const id of ids) {
|
|
118
|
+
const candidate = readAttempt(base, id);
|
|
119
|
+
if (candidate) {
|
|
120
|
+
setIdentity(index, id, candidate);
|
|
121
|
+
initialAttempts.set(id, candidate);
|
|
72
122
|
}
|
|
73
|
-
|
|
123
|
+
else index.uncommitted.add(id);
|
|
124
|
+
}
|
|
125
|
+
attemptIndexes.set(base, index);
|
|
126
|
+
if (attemptIndexes.size > 8) attemptIndexes.delete(attemptIndexes.keys().next().value);
|
|
127
|
+
} else if (index.signature !== signature) {
|
|
128
|
+
let ids;
|
|
129
|
+
try { ids = new Set(readdirSync(join(base, "runs"))); }
|
|
130
|
+
catch {
|
|
131
|
+
attemptIndexes.delete(base);
|
|
132
|
+
return { index: { base, byConversation: new Map() } };
|
|
133
|
+
}
|
|
134
|
+
for (const id of index.ids) {
|
|
135
|
+
if (ids.has(id)) continue;
|
|
136
|
+
removeIdentity(index, id);
|
|
137
|
+
index.uncommitted.delete(id);
|
|
138
|
+
}
|
|
139
|
+
for (const id of ids) {
|
|
140
|
+
if (index.ids.has(id)) continue;
|
|
141
|
+
const candidate = readAttempt(base, id);
|
|
142
|
+
if (candidate) setIdentity(index, id, candidate);
|
|
143
|
+
else index.uncommitted.add(id);
|
|
144
|
+
}
|
|
145
|
+
index.ids = ids;
|
|
146
|
+
index.signature = signature;
|
|
147
|
+
}
|
|
148
|
+
if (Date.now() >= index.nextRetryAt) {
|
|
149
|
+
for (const id of index.uncommitted) {
|
|
150
|
+
const candidate = readAttempt(base, id);
|
|
151
|
+
if (!candidate) continue;
|
|
152
|
+
setIdentity(index, id, candidate);
|
|
153
|
+
index.uncommitted.delete(id);
|
|
74
154
|
}
|
|
75
|
-
|
|
76
|
-
|
|
155
|
+
index.nextRetryAt = Date.now() + UNCOMMITTED_RETRY_MS;
|
|
156
|
+
}
|
|
157
|
+
return { index, initialAttempts };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function readConversationAttempts(index, meta, initialAttempts) {
|
|
161
|
+
const matches = [];
|
|
162
|
+
for (const id of index.byConversation.get(conversationKey(meta.sessionId, meta.cwd)) ?? []) {
|
|
163
|
+
const fresh = initialAttempts?.get(id) ?? readAttempt(index.base, id);
|
|
164
|
+
if (!fresh) {
|
|
165
|
+
removeIdentity(index, id);
|
|
166
|
+
index.uncommitted.add(id);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
setIdentity(index, id, fresh);
|
|
170
|
+
if (fresh.sessionId === meta.sessionId && fresh.cwd === meta.cwd) matches.push(fresh);
|
|
171
|
+
}
|
|
172
|
+
return matches;
|
|
77
173
|
}
|
|
78
174
|
|
|
79
175
|
/** One synchronous UI refresh shares a snapshot; the next refresh reads disk again. */
|
|
80
176
|
export function createAssignmentProjector(options = {}) {
|
|
81
|
-
let
|
|
177
|
+
let prepared;
|
|
178
|
+
const snapshots = new Map();
|
|
82
179
|
const readSnapshot = (meta) => {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
-
}
|
|
180
|
+
const key = conversationKey(meta.sessionId, meta.cwd);
|
|
181
|
+
if (!snapshots.has(key)) {
|
|
182
|
+
prepared ??= prepareAttemptIndex(options);
|
|
183
|
+
snapshots.set(key, readConversationAttempts(prepared.index, meta, prepared.initialAttempts));
|
|
92
184
|
}
|
|
93
|
-
return
|
|
185
|
+
return snapshots.get(key);
|
|
94
186
|
};
|
|
95
187
|
return (meta, processStatus = meta.status) => assignmentFromSnapshot(meta, processStatus, options,
|
|
96
188
|
() => readSnapshot(meta));
|