pi-herdsman 0.36.2 → 0.36.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/dist/index.js +1087 -594
- package/dist/index.js.map +4 -4
- package/dist/peer-bridge.js +192 -192
- package/dist/peer-bridge.js.map +4 -4
- package/docs/adr/0005-use-files-for-explicit-evidence-handoffs.md +7 -2
- package/docs/adr/0028-reference-historical-assignment-evidence.md +14 -6
- package/docs/adr/0035-separate-assignment-handle-from-checked-out-branch.md +63 -0
- package/docs/concepts/coordination.md +25 -15
- package/docs/guides/handoffs.md +9 -8
- package/docs/guides/project-orchestration.md +21 -11
- package/docs/reference/agent.md +4 -3
- package/docs/reference/staff.md +33 -22
- package/package.json +1 -1
package/dist/peer-bridge.js
CHANGED
|
@@ -40,189 +40,28 @@ function sameRuntimeBuild(left, right) {
|
|
|
40
40
|
// extension/storage.ts
|
|
41
41
|
import { homedir, tmpdir } from "node:os";
|
|
42
42
|
import {
|
|
43
|
-
chmodSync,
|
|
44
|
-
closeSync,
|
|
43
|
+
chmodSync as chmodSync2,
|
|
44
|
+
closeSync as closeSync2,
|
|
45
45
|
constants,
|
|
46
46
|
fstatSync,
|
|
47
|
-
fsyncSync,
|
|
48
|
-
mkdirSync,
|
|
49
|
-
openSync,
|
|
50
|
-
readdirSync,
|
|
51
|
-
readFileSync as
|
|
52
|
-
renameSync,
|
|
53
|
-
lstatSync,
|
|
54
|
-
unlinkSync,
|
|
47
|
+
fsyncSync as fsyncSync2,
|
|
48
|
+
mkdirSync as mkdirSync2,
|
|
49
|
+
openSync as openSync2,
|
|
50
|
+
readdirSync as readdirSync2,
|
|
51
|
+
readFileSync as readFileSync3,
|
|
52
|
+
renameSync as renameSync2,
|
|
53
|
+
lstatSync as lstatSync2,
|
|
54
|
+
unlinkSync as unlinkSync2,
|
|
55
55
|
writeFileSync
|
|
56
56
|
} from "node:fs";
|
|
57
|
-
import { createHash as
|
|
58
|
-
import { basename, dirname, join } from "node:path";
|
|
57
|
+
import { createHash as createHash3, randomUUID as randomUUID2 } from "node:crypto";
|
|
58
|
+
import { basename as basename2, dirname as dirname2, join as join2 } from "node:path";
|
|
59
59
|
import { fileURLToPath } from "node:url";
|
|
60
|
-
var RESULT_PREFIX = "result:";
|
|
61
|
-
var RESULT_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
62
|
-
var SEMANTIC_RESULT_REF = /^result:([a-z][a-z0-9_-]{0,31})#([1-9][0-9]*)$/;
|
|
63
|
-
var SEMANTIC_EVIDENCE_REF = /^evidence:([a-z][a-z0-9_-]{0,31})#([1-9][0-9]*)$/;
|
|
64
|
-
var INCIDENTAL_METADATA = /* @__PURE__ */ new Set([
|
|
65
|
-
".ds_store",
|
|
66
|
-
"desktop.ini",
|
|
67
|
-
"thumbs.db",
|
|
68
|
-
".directory"
|
|
69
|
-
]);
|
|
70
|
-
function isIncidentalMetadata(entry) {
|
|
71
|
-
return entry.isFile() && INCIDENTAL_METADATA.has(entry.name.toLowerCase());
|
|
72
|
-
}
|
|
73
|
-
var EVIDENCE_ID = RESULT_ID;
|
|
74
|
-
var EVIDENCE_SNAPSHOT_MAX_BYTES = 1024 * 1024;
|
|
75
|
-
function isRequiredEvidence(value) {
|
|
76
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
77
|
-
const r = value;
|
|
78
|
-
if (typeof r.id !== "string" || !EVIDENCE_ID.test(r.id) || typeof r.name !== "string" || !r.name || r.name.length > 255)
|
|
79
|
-
return false;
|
|
80
|
-
if (Object.hasOwn(r, "result"))
|
|
81
|
-
return Object.keys(r).length === 3 && isResultBinding(r.result);
|
|
82
|
-
const keys = Object.keys(r).sort();
|
|
83
|
-
const legacyKeys = ["id", "name", "path", "sha256", "sourceAssignmentId"];
|
|
84
|
-
const semanticKeys = [...legacyKeys, "ref"].sort();
|
|
85
|
-
return (keys.join("\0") === legacyKeys.join("\0") || keys.join("\0") === semanticKeys.join("\0")) && (!Object.hasOwn(r, "ref") || isCanonicalSemanticEvidenceRef(r.ref)) && typeof r.sourceAssignmentId === "string" && EVIDENCE_ID.test(r.sourceAssignmentId) && typeof r.path === "string" && r.path.length > 0 && typeof r.sha256 === "string" && /^[a-f0-9]{64}$/.test(r.sha256);
|
|
86
|
-
}
|
|
87
|
-
function isCanonicalSemanticEvidenceRef(value) {
|
|
88
|
-
if (typeof value !== "string") return false;
|
|
89
|
-
const match = SEMANTIC_EVIDENCE_REF.exec(value);
|
|
90
|
-
return !!match && Number.isSafeInteger(Number(match[2]));
|
|
91
|
-
}
|
|
92
|
-
function verifySemanticEvidenceReservation(evidenceId, ref) {
|
|
93
|
-
if (!EVIDENCE_ID.test(evidenceId) || !isCanonicalSemanticEvidenceRef(ref))
|
|
94
|
-
throw new Error("invalid semantic evidence reservation");
|
|
95
|
-
const match = SEMANTIC_EVIDENCE_REF.exec(ref);
|
|
96
|
-
const root = join(herdsmanDataRoot(), "evidence-ref-reservations");
|
|
97
|
-
const paths = [
|
|
98
|
-
join(root, "by-id", evidenceId),
|
|
99
|
-
join(root, "refs", match[1], match[2])
|
|
100
|
-
];
|
|
101
|
-
const contents = paths.map((path) => {
|
|
102
|
-
const stat = lstatSync(path);
|
|
103
|
-
if (!stat.isFile() || stat.isSymbolicLink())
|
|
104
|
-
throw new Error("invalid semantic evidence reservation");
|
|
105
|
-
return readFileSync2(path, "utf8");
|
|
106
|
-
});
|
|
107
|
-
const [byId, byRef] = contents;
|
|
108
|
-
if (byId !== `${ref}
|
|
109
|
-
` || byRef !== `${evidenceId}
|
|
110
|
-
`)
|
|
111
|
-
throw new Error("invalid semantic evidence reservation");
|
|
112
|
-
}
|
|
113
|
-
function parseSemanticResultRef(input) {
|
|
114
|
-
const match = SEMANTIC_RESULT_REF.exec(input);
|
|
115
|
-
if (!match) return void 0;
|
|
116
|
-
const index = Number(match[2]);
|
|
117
|
-
if (!Number.isSafeInteger(index)) return void 0;
|
|
118
|
-
return { agent: match[1], index };
|
|
119
|
-
}
|
|
120
|
-
function isCanonicalResultRef(value) {
|
|
121
|
-
return typeof value === "string" && value.startsWith(RESULT_PREFIX) && RESULT_ID.test(value.slice(RESULT_PREFIX.length));
|
|
122
|
-
}
|
|
123
|
-
function isResultBinding(value) {
|
|
124
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
125
|
-
const record = value;
|
|
126
|
-
return Object.keys(record).length === 2 && typeof record.ref === "string" && !!parseSemanticResultRef(record.ref) && isCanonicalResultRef(record.canonicalRef);
|
|
127
|
-
}
|
|
128
|
-
function herdsmanDataRoot() {
|
|
129
|
-
return join(nodeOnlyAgentDir(), "pi-herdsman");
|
|
130
|
-
}
|
|
131
|
-
function nodeOnlyAgentDir() {
|
|
132
|
-
let directory2 = process.env.PI_CODING_AGENT_DIR;
|
|
133
|
-
if (!directory2) return join(homedir(), ".pi", "agent");
|
|
134
|
-
if (process.platform === "win32" && directory2.startsWith("/") && !directory2.startsWith("//") && !directory2.includes("\\")) {
|
|
135
|
-
const match = directory2.match(
|
|
136
|
-
/^\/(?:mnt\/|cygdrive\/)?([a-z])(?:\/(.*))?$/i
|
|
137
|
-
);
|
|
138
|
-
if (match)
|
|
139
|
-
directory2 = `${match[1].toUpperCase()}:\\${(match[2] ?? "").replaceAll("/", "\\")}`;
|
|
140
|
-
}
|
|
141
|
-
if (directory2 === "~") return homedir();
|
|
142
|
-
if (directory2.startsWith("~/") || process.platform === "win32" && directory2.startsWith("~\\"))
|
|
143
|
-
return join(homedir(), directory2.slice(2));
|
|
144
|
-
if (/^file:\/\//.test(directory2)) return fileURLToPath(directory2);
|
|
145
|
-
return directory2;
|
|
146
|
-
}
|
|
147
|
-
function validateResultId(requestId) {
|
|
148
|
-
if (!RESULT_ID.test(requestId)) throw new Error("invalid result request id");
|
|
149
|
-
}
|
|
150
|
-
function resultPath(requestId) {
|
|
151
|
-
validateResultId(requestId);
|
|
152
|
-
return join(herdsmanDataRoot(), "results", requestId);
|
|
153
|
-
}
|
|
154
|
-
function peerAttachmentPath(name, text) {
|
|
155
|
-
if (typeof name !== "string" || !name || name.length > 255 || /[\\/\0]/u.test(name) || typeof text !== "string")
|
|
156
|
-
throw new Error("invalid peer attachment");
|
|
157
|
-
const bytes = Buffer.from(text, "utf8");
|
|
158
|
-
if (new TextDecoder("utf-8", { fatal: true }).decode(bytes) !== text)
|
|
159
|
-
throw new Error("invalid peer attachment text");
|
|
160
|
-
const directory2 = join(
|
|
161
|
-
herdsmanDataRoot(),
|
|
162
|
-
"runtime",
|
|
163
|
-
"peers-v2",
|
|
164
|
-
"received-evidence"
|
|
165
|
-
);
|
|
166
|
-
const digest = createHash2("sha256").update(bytes).digest("hex");
|
|
167
|
-
return join(directory2, `${digest}-${name}`);
|
|
168
|
-
}
|
|
169
|
-
function persistPeerAttachment(name, text) {
|
|
170
|
-
const path = peerAttachmentPath(name, text);
|
|
171
|
-
const bytes = Buffer.from(text, "utf8");
|
|
172
|
-
mkdirSync(dirname(path), { recursive: true, mode: 448 });
|
|
173
|
-
chmodSync(dirname(path), 448);
|
|
174
|
-
try {
|
|
175
|
-
writeFileSync(path, bytes, { flag: "wx", mode: 384, flush: true });
|
|
176
|
-
} catch (error) {
|
|
177
|
-
if (error.code !== "EEXIST") throw error;
|
|
178
|
-
if (!readFileSync2(path).equals(bytes))
|
|
179
|
-
throw new Error("peer attachment content identity collision");
|
|
180
|
-
}
|
|
181
|
-
return path;
|
|
182
|
-
}
|
|
183
|
-
function persistPeerResultArtifact(canonicalRef, text) {
|
|
184
|
-
if (!isCanonicalResultRef(canonicalRef) || typeof text !== "string")
|
|
185
|
-
throw new Error("invalid peer result artifact");
|
|
186
|
-
const path = resultPath(canonicalRef.slice(RESULT_PREFIX.length));
|
|
187
|
-
mkdirSync(dirname(path), { recursive: true, mode: 448 });
|
|
188
|
-
chmodSync(dirname(path), 448);
|
|
189
|
-
const bytes = Buffer.from(text, "utf8");
|
|
190
|
-
try {
|
|
191
|
-
writeFileSync(path, bytes, { flag: "wx", mode: 384, flush: true });
|
|
192
|
-
} catch (error) {
|
|
193
|
-
if (error.code !== "EEXIST") throw error;
|
|
194
|
-
if (!readFileSync2(path).equals(bytes))
|
|
195
|
-
throw new Error("peer result identity collision");
|
|
196
|
-
}
|
|
197
|
-
return path;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// extension/supervision.ts
|
|
201
|
-
import { constants as constants2 } from "node:fs";
|
|
202
|
-
import {
|
|
203
|
-
chmodSync as chmodSync3,
|
|
204
|
-
closeSync as closeSync3,
|
|
205
|
-
fsyncSync as fsyncSync3,
|
|
206
|
-
lstatSync as lstatSync3,
|
|
207
|
-
fstatSync as fstatSync2,
|
|
208
|
-
readdirSync as readdirSync3,
|
|
209
|
-
mkdirSync as mkdirSync3,
|
|
210
|
-
openSync as openSync3,
|
|
211
|
-
readFileSync as readFileSync4,
|
|
212
|
-
readSync,
|
|
213
|
-
statSync,
|
|
214
|
-
renameSync as renameSync3,
|
|
215
|
-
unlinkSync as unlinkSync3,
|
|
216
|
-
writeFileSync as writeFileSync2
|
|
217
|
-
} from "node:fs";
|
|
218
|
-
import { createHash as createHash4, randomUUID as randomUUID3 } from "node:crypto";
|
|
219
|
-
import { basename as basename3, dirname as dirname3, isAbsolute, join as join3, normalize } from "node:path";
|
|
220
|
-
import { isDeepStrictEqual } from "node:util";
|
|
221
60
|
|
|
222
61
|
// extension/lock.ts
|
|
223
62
|
import * as fs from "node:fs";
|
|
224
|
-
import { createHash as
|
|
225
|
-
import { basename
|
|
63
|
+
import { createHash as createHash2, randomUUID } from "node:crypto";
|
|
64
|
+
import { basename, dirname, join } from "node:path";
|
|
226
65
|
var ProcessLockOccupiedError = class extends Error {
|
|
227
66
|
code = "PROCESS_LOCK_OCCUPIED";
|
|
228
67
|
};
|
|
@@ -235,7 +74,7 @@ function isProcessLockClaim(value) {
|
|
|
235
74
|
return true;
|
|
236
75
|
}
|
|
237
76
|
function readProcessLockOwner(path, owner) {
|
|
238
|
-
const value = JSON.parse(fs.readFileSync(
|
|
77
|
+
const value = JSON.parse(fs.readFileSync(join(path, owner), "utf8"));
|
|
239
78
|
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
240
79
|
throw new Error("invalid process lock claim");
|
|
241
80
|
const record = value;
|
|
@@ -312,9 +151,9 @@ function processExists(pid) {
|
|
|
312
151
|
}
|
|
313
152
|
}
|
|
314
153
|
function publishClaim(parent, claimDir, owner, payload) {
|
|
315
|
-
const temporary =
|
|
154
|
+
const temporary = join(parent, `.${basename(claimDir)}.${randomUUID()}.tmp`);
|
|
316
155
|
fs.mkdirSync(temporary, 448);
|
|
317
|
-
const temporaryOwner =
|
|
156
|
+
const temporaryOwner = join(temporary, owner);
|
|
318
157
|
let fd;
|
|
319
158
|
try {
|
|
320
159
|
fd = fs.openSync(temporaryOwner, "wx", 384);
|
|
@@ -363,15 +202,15 @@ function acquireProcessLock(path, options = {}) {
|
|
|
363
202
|
const verifyMessage = `Unable to verify ${name}`;
|
|
364
203
|
const occupiedMessage = options.occupiedMessage ?? `${name[0].toUpperCase()}${name.slice(1)} is in progress`;
|
|
365
204
|
const recoverMessage = `Unable to recover ${name}`;
|
|
366
|
-
const parent =
|
|
205
|
+
const parent = dirname(path);
|
|
367
206
|
fs.mkdirSync(parent, { recursive: true, mode: 448 });
|
|
368
207
|
fs.chmodSync(parent, 448);
|
|
369
208
|
const claimDir = path;
|
|
370
|
-
const claim = { pid: process.pid, id:
|
|
209
|
+
const claim = { pid: process.pid, id: randomUUID() };
|
|
371
210
|
const lockOwner = process.platform === "linux" ? { ...claim, linuxBirth: readLinuxBirth(process.pid) } : claim;
|
|
372
211
|
const owner = `${claim.pid}-${claim.id}`;
|
|
373
212
|
const payload = JSON.stringify(lockOwner);
|
|
374
|
-
const ownerPath =
|
|
213
|
+
const ownerPath = join(claimDir, owner);
|
|
375
214
|
try {
|
|
376
215
|
lockOwnerEntry(claimDir);
|
|
377
216
|
} catch (error) {
|
|
@@ -423,9 +262,9 @@ function acquireProcessLock(path, options = {}) {
|
|
|
423
262
|
if (error2 instanceof ProcessLockOccupiedError) throw error2;
|
|
424
263
|
throw new Error(verifyMessage, { cause: error2 });
|
|
425
264
|
}
|
|
426
|
-
const quarantine =
|
|
265
|
+
const quarantine = join(
|
|
427
266
|
parent,
|
|
428
|
-
`.${
|
|
267
|
+
`.${basename(claimDir)}.${randomUUID()}.stale`
|
|
429
268
|
);
|
|
430
269
|
try {
|
|
431
270
|
const { owner: currentOwner } = lockOwnerEntry(claimDir);
|
|
@@ -466,7 +305,7 @@ function acquireProcessLock(path, options = {}) {
|
|
|
466
305
|
options.afterStaleOwnerRemoved?.();
|
|
467
306
|
try {
|
|
468
307
|
unlinkMetadata(quarantine, metadata);
|
|
469
|
-
fs.unlinkSync(
|
|
308
|
+
fs.unlinkSync(join(quarantine, observedOwner));
|
|
470
309
|
fs.rmdirSync(quarantine);
|
|
471
310
|
} catch (cleanupError) {
|
|
472
311
|
throw new Error(recoverMessage, { cause: cleanupError });
|
|
@@ -498,7 +337,7 @@ function releaseProcessLock(claimDir, ownerPath, claim, name) {
|
|
|
498
337
|
}
|
|
499
338
|
throw new Error(`Unable to verify ${name} ownership`, { cause: error });
|
|
500
339
|
}
|
|
501
|
-
if (owner !==
|
|
340
|
+
if (owner !== basename(ownerPath)) {
|
|
502
341
|
try {
|
|
503
342
|
if (ownerMissing(ownerPath)) return;
|
|
504
343
|
} catch (error) {
|
|
@@ -510,7 +349,7 @@ function releaseProcessLock(claimDir, ownerPath, claim, name) {
|
|
|
510
349
|
}
|
|
511
350
|
let current;
|
|
512
351
|
try {
|
|
513
|
-
current = readProcessLockOwner(claimDir,
|
|
352
|
+
current = readProcessLockOwner(claimDir, basename(ownerPath));
|
|
514
353
|
} catch (error) {
|
|
515
354
|
if (error.code === "ENOENT" && ownerMissing(ownerPath))
|
|
516
355
|
return;
|
|
@@ -520,16 +359,16 @@ function releaseProcessLock(claimDir, ownerPath, claim, name) {
|
|
|
520
359
|
throw new Error(
|
|
521
360
|
`${name[0].toUpperCase()}${name.slice(1)} ownership changed`
|
|
522
361
|
);
|
|
523
|
-
const retired =
|
|
524
|
-
|
|
525
|
-
`.${
|
|
362
|
+
const retired = join(
|
|
363
|
+
dirname(claimDir),
|
|
364
|
+
`.${basename(claimDir)}.${randomUUID()}.released`
|
|
526
365
|
);
|
|
527
366
|
fs.renameSync(claimDir, retired);
|
|
528
367
|
let retiredOwner;
|
|
529
368
|
let metadata;
|
|
530
369
|
try {
|
|
531
370
|
({ owner: retiredOwner, metadata } = lockOwnerEntry(retired));
|
|
532
|
-
if (retiredOwner !==
|
|
371
|
+
if (retiredOwner !== basename(ownerPath))
|
|
533
372
|
throw new Error("Process lock owner changed");
|
|
534
373
|
const relocated = readProcessLockOwner(retired, retiredOwner);
|
|
535
374
|
if (relocated.pid !== claim.pid || relocated.id !== claim.id)
|
|
@@ -538,7 +377,7 @@ function releaseProcessLock(claimDir, ownerPath, claim, name) {
|
|
|
538
377
|
throw new Error(`Unable to verify ${name} ownership`, { cause: error });
|
|
539
378
|
}
|
|
540
379
|
unlinkMetadata(retired, metadata);
|
|
541
|
-
fs.unlinkSync(
|
|
380
|
+
fs.unlinkSync(join(retired, retiredOwner));
|
|
542
381
|
try {
|
|
543
382
|
fs.rmdirSync(retired);
|
|
544
383
|
} catch (error) {
|
|
@@ -549,14 +388,175 @@ function releaseProcessLock(claimDir, ownerPath, claim, name) {
|
|
|
549
388
|
function unlinkMetadata(path, entries) {
|
|
550
389
|
for (const entry of entries) {
|
|
551
390
|
try {
|
|
552
|
-
fs.unlinkSync(
|
|
391
|
+
fs.unlinkSync(join(path, entry));
|
|
553
392
|
} catch (error) {
|
|
554
393
|
if (error.code !== "ENOENT") throw error;
|
|
555
394
|
}
|
|
556
395
|
}
|
|
557
396
|
}
|
|
558
397
|
|
|
398
|
+
// extension/storage.ts
|
|
399
|
+
var RESULT_PREFIX = "result:";
|
|
400
|
+
var RESULT_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
401
|
+
var SEMANTIC_RESULT_REF = /^result:([a-z][a-z0-9_-]{0,31})#([1-9][0-9]*)$/;
|
|
402
|
+
var SEMANTIC_EVIDENCE_REF = /^evidence:([a-z][a-z0-9_-]{0,31})#([1-9][0-9]*)$/;
|
|
403
|
+
var INCIDENTAL_METADATA = /* @__PURE__ */ new Set([
|
|
404
|
+
".ds_store",
|
|
405
|
+
"desktop.ini",
|
|
406
|
+
"thumbs.db",
|
|
407
|
+
".directory"
|
|
408
|
+
]);
|
|
409
|
+
function isIncidentalMetadata(entry) {
|
|
410
|
+
return entry.isFile() && INCIDENTAL_METADATA.has(entry.name.toLowerCase());
|
|
411
|
+
}
|
|
412
|
+
var EVIDENCE_ID = RESULT_ID;
|
|
413
|
+
var EVIDENCE_SNAPSHOT_MAX_BYTES = 1024 * 1024;
|
|
414
|
+
function isRequiredEvidence(value) {
|
|
415
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
416
|
+
const r = value;
|
|
417
|
+
if (typeof r.id !== "string" || !EVIDENCE_ID.test(r.id) || typeof r.name !== "string" || !r.name || r.name.length > 255)
|
|
418
|
+
return false;
|
|
419
|
+
if (Object.hasOwn(r, "result"))
|
|
420
|
+
return Object.keys(r).length === 3 && isResultBinding(r.result);
|
|
421
|
+
const keys = Object.keys(r).sort();
|
|
422
|
+
const legacyKeys = ["id", "name", "path", "sha256", "sourceAssignmentId"];
|
|
423
|
+
const semanticKeys = [...legacyKeys, "ref"].sort();
|
|
424
|
+
return (keys.join("\0") === legacyKeys.join("\0") || keys.join("\0") === semanticKeys.join("\0")) && (!Object.hasOwn(r, "ref") || isCanonicalSemanticEvidenceRef(r.ref)) && typeof r.sourceAssignmentId === "string" && EVIDENCE_ID.test(r.sourceAssignmentId) && typeof r.path === "string" && r.path.length > 0 && typeof r.sha256 === "string" && /^[a-f0-9]{64}$/.test(r.sha256);
|
|
425
|
+
}
|
|
426
|
+
function isCanonicalSemanticEvidenceRef(value) {
|
|
427
|
+
if (typeof value !== "string") return false;
|
|
428
|
+
const match = SEMANTIC_EVIDENCE_REF.exec(value);
|
|
429
|
+
return !!match && Number.isSafeInteger(Number(match[2]));
|
|
430
|
+
}
|
|
431
|
+
function verifySemanticEvidenceReservation(evidenceId, ref) {
|
|
432
|
+
if (!EVIDENCE_ID.test(evidenceId) || !isCanonicalSemanticEvidenceRef(ref))
|
|
433
|
+
throw new Error("invalid semantic evidence reservation");
|
|
434
|
+
const match = SEMANTIC_EVIDENCE_REF.exec(ref);
|
|
435
|
+
const root = join2(herdsmanDataRoot(), "evidence-ref-reservations");
|
|
436
|
+
const paths = [
|
|
437
|
+
join2(root, "by-id", evidenceId),
|
|
438
|
+
join2(root, "refs", match[1], match[2])
|
|
439
|
+
];
|
|
440
|
+
const contents = paths.map((path) => {
|
|
441
|
+
const stat = lstatSync2(path);
|
|
442
|
+
if (!stat.isFile() || stat.isSymbolicLink())
|
|
443
|
+
throw new Error("invalid semantic evidence reservation");
|
|
444
|
+
return readFileSync3(path, "utf8");
|
|
445
|
+
});
|
|
446
|
+
const [byId, byRef] = contents;
|
|
447
|
+
if (byId !== `${ref}
|
|
448
|
+
` || byRef !== `${evidenceId}
|
|
449
|
+
`)
|
|
450
|
+
throw new Error("invalid semantic evidence reservation");
|
|
451
|
+
}
|
|
452
|
+
function parseSemanticResultRef(input) {
|
|
453
|
+
const match = SEMANTIC_RESULT_REF.exec(input);
|
|
454
|
+
if (!match) return void 0;
|
|
455
|
+
const index = Number(match[2]);
|
|
456
|
+
if (!Number.isSafeInteger(index)) return void 0;
|
|
457
|
+
return { agent: match[1], index };
|
|
458
|
+
}
|
|
459
|
+
function isCanonicalResultRef(value) {
|
|
460
|
+
return typeof value === "string" && value.startsWith(RESULT_PREFIX) && RESULT_ID.test(value.slice(RESULT_PREFIX.length));
|
|
461
|
+
}
|
|
462
|
+
function isResultBinding(value) {
|
|
463
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
464
|
+
const record = value;
|
|
465
|
+
return Object.keys(record).length === 2 && typeof record.ref === "string" && !!parseSemanticResultRef(record.ref) && isCanonicalResultRef(record.canonicalRef);
|
|
466
|
+
}
|
|
467
|
+
function herdsmanDataRoot() {
|
|
468
|
+
return join2(nodeOnlyAgentDir(), "pi-herdsman");
|
|
469
|
+
}
|
|
470
|
+
function nodeOnlyAgentDir() {
|
|
471
|
+
let directory2 = process.env.PI_CODING_AGENT_DIR;
|
|
472
|
+
if (!directory2) return join2(homedir(), ".pi", "agent");
|
|
473
|
+
if (process.platform === "win32" && directory2.startsWith("/") && !directory2.startsWith("//") && !directory2.includes("\\")) {
|
|
474
|
+
const match = directory2.match(
|
|
475
|
+
/^\/(?:mnt\/|cygdrive\/)?([a-z])(?:\/(.*))?$/i
|
|
476
|
+
);
|
|
477
|
+
if (match)
|
|
478
|
+
directory2 = `${match[1].toUpperCase()}:\\${(match[2] ?? "").replaceAll("/", "\\")}`;
|
|
479
|
+
}
|
|
480
|
+
if (directory2 === "~") return homedir();
|
|
481
|
+
if (directory2.startsWith("~/") || process.platform === "win32" && directory2.startsWith("~\\"))
|
|
482
|
+
return join2(homedir(), directory2.slice(2));
|
|
483
|
+
if (/^file:\/\//.test(directory2)) return fileURLToPath(directory2);
|
|
484
|
+
return directory2;
|
|
485
|
+
}
|
|
486
|
+
function validateResultId(requestId) {
|
|
487
|
+
if (!RESULT_ID.test(requestId)) throw new Error("invalid result request id");
|
|
488
|
+
}
|
|
489
|
+
function resultPath(requestId) {
|
|
490
|
+
validateResultId(requestId);
|
|
491
|
+
return join2(herdsmanDataRoot(), "results", requestId);
|
|
492
|
+
}
|
|
493
|
+
function peerAttachmentPath(name, text) {
|
|
494
|
+
if (typeof name !== "string" || !name || name.length > 255 || /[\\/\0]/u.test(name) || typeof text !== "string")
|
|
495
|
+
throw new Error("invalid peer attachment");
|
|
496
|
+
const bytes = Buffer.from(text, "utf8");
|
|
497
|
+
if (new TextDecoder("utf-8", { fatal: true }).decode(bytes) !== text)
|
|
498
|
+
throw new Error("invalid peer attachment text");
|
|
499
|
+
const directory2 = join2(
|
|
500
|
+
herdsmanDataRoot(),
|
|
501
|
+
"runtime",
|
|
502
|
+
"peers-v2",
|
|
503
|
+
"received-evidence"
|
|
504
|
+
);
|
|
505
|
+
const digest = createHash3("sha256").update(bytes).digest("hex");
|
|
506
|
+
return join2(directory2, `${digest}-${name}`);
|
|
507
|
+
}
|
|
508
|
+
function persistPeerAttachment(name, text) {
|
|
509
|
+
const path = peerAttachmentPath(name, text);
|
|
510
|
+
const bytes = Buffer.from(text, "utf8");
|
|
511
|
+
mkdirSync2(dirname2(path), { recursive: true, mode: 448 });
|
|
512
|
+
chmodSync2(dirname2(path), 448);
|
|
513
|
+
try {
|
|
514
|
+
writeFileSync(path, bytes, { flag: "wx", mode: 384, flush: true });
|
|
515
|
+
} catch (error) {
|
|
516
|
+
if (error.code !== "EEXIST") throw error;
|
|
517
|
+
if (!readFileSync3(path).equals(bytes))
|
|
518
|
+
throw new Error("peer attachment content identity collision");
|
|
519
|
+
}
|
|
520
|
+
return path;
|
|
521
|
+
}
|
|
522
|
+
function persistPeerResultArtifact(canonicalRef, text) {
|
|
523
|
+
if (!isCanonicalResultRef(canonicalRef) || typeof text !== "string")
|
|
524
|
+
throw new Error("invalid peer result artifact");
|
|
525
|
+
const path = resultPath(canonicalRef.slice(RESULT_PREFIX.length));
|
|
526
|
+
mkdirSync2(dirname2(path), { recursive: true, mode: 448 });
|
|
527
|
+
chmodSync2(dirname2(path), 448);
|
|
528
|
+
const bytes = Buffer.from(text, "utf8");
|
|
529
|
+
try {
|
|
530
|
+
writeFileSync(path, bytes, { flag: "wx", mode: 384, flush: true });
|
|
531
|
+
} catch (error) {
|
|
532
|
+
if (error.code !== "EEXIST") throw error;
|
|
533
|
+
if (!readFileSync3(path).equals(bytes))
|
|
534
|
+
throw new Error("peer result identity collision");
|
|
535
|
+
}
|
|
536
|
+
return path;
|
|
537
|
+
}
|
|
538
|
+
|
|
559
539
|
// extension/supervision.ts
|
|
540
|
+
import { constants as constants2 } from "node:fs";
|
|
541
|
+
import {
|
|
542
|
+
chmodSync as chmodSync3,
|
|
543
|
+
closeSync as closeSync3,
|
|
544
|
+
fsyncSync as fsyncSync3,
|
|
545
|
+
lstatSync as lstatSync3,
|
|
546
|
+
fstatSync as fstatSync2,
|
|
547
|
+
readdirSync as readdirSync3,
|
|
548
|
+
mkdirSync as mkdirSync3,
|
|
549
|
+
openSync as openSync3,
|
|
550
|
+
readFileSync as readFileSync4,
|
|
551
|
+
readSync,
|
|
552
|
+
statSync,
|
|
553
|
+
renameSync as renameSync3,
|
|
554
|
+
unlinkSync as unlinkSync3,
|
|
555
|
+
writeFileSync as writeFileSync2
|
|
556
|
+
} from "node:fs";
|
|
557
|
+
import { createHash as createHash4, randomUUID as randomUUID3 } from "node:crypto";
|
|
558
|
+
import { basename as basename3, dirname as dirname3, isAbsolute, join as join3, normalize } from "node:path";
|
|
559
|
+
import { isDeepStrictEqual } from "node:util";
|
|
560
560
|
var COORDINATION_MESSAGE_KINDS = [
|
|
561
561
|
"chief_message",
|
|
562
562
|
"lead_message",
|