pi-better-server 0.6.0 → 0.7.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.
- package/package.json +4 -3
- package/scripts/measure-full-runtime/frontend.ts +10 -2
- package/scripts/measure-full-runtime.mjs +2 -0
- package/src/codex/return.ts +3 -2
- package/src/detach/extension.ts +20 -3
- package/src/event-log.ts +40 -12
- package/src/hosted-session.ts +11 -5
- package/src/remote-client/packages.ts +1 -4
- package/src/runtime/background-tools.ts +54 -8
- package/src/runtime/background.ts +16 -4
- package/src/runtime/domain/background/process.mjs +49 -11
- package/src/runtime/domain/background/supervisor/store.mjs +3 -3
- package/src/runtime/domain/background/supervisor/supervisor.mjs +9 -3
- package/src/runtime/domain/background-service/index.domain.mjs +19 -0
- package/src/runtime/domain/background-service/index.ts +19 -0
- package/src/runtime/domain/background-service/managed-frontend.domain.mjs +87 -5
- package/src/runtime/domain/background-service/managed-frontend.ts +76 -5
- package/src/runtime/domain/background-service/navigator-provider.domain.mjs +5 -0
- package/src/runtime/domain/background-service/navigator-provider.ts +4 -0
- package/src/runtime/domain/background-service/observed-waits.domain.mjs +203 -31
- package/src/runtime/domain/background-service/observed-waits.ts +168 -24
- package/src/runtime/domain/background-service/process.domain.mjs +49 -11
- package/src/runtime/domain/background-service/process.ts +45 -11
- package/src/runtime/domain/background-service/review-settings.domain.mjs +16 -0
- package/src/runtime/domain/background-service/review-settings.ts +18 -0
- package/src/runtime/domain/background-service/runtime.domain.mjs +8 -70
- package/src/runtime/domain/background-service/runtime.ts +8 -73
- package/src/runtime/domain/background-service/shared-harness-settings.domain.mjs +2 -3
- package/src/runtime/domain/background-service/shared-harness-settings.ts +2 -5
- package/src/runtime/domain/background-service/shared-navigator.domain.mjs +55 -28
- package/src/runtime/domain/background-service/shared-navigator.ts +47 -26
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/outbox.domain.mjs +7 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/outbox.ts +9 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.domain.mjs +5 -1
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.ts +6 -0
- package/src/runtime/domain/background-service/shared-ssh-core/index.domain.mjs +1 -1
- package/src/runtime/domain/background-service/shared-ssh-core/index.ts +1 -1
- package/src/runtime/domain/background-service/stall.domain.mjs +5 -3
- package/src/runtime/domain/background-service/stall.ts +7 -4
- package/src/runtime/domain/background-service/supervised-progress.domain.mjs +42 -0
- package/src/runtime/domain/background-service/supervised-progress.ts +43 -0
- package/src/runtime/domain/background-service/supervisor/README.md +1 -4
- package/src/runtime/domain/background-service/supervisor/emit-runner.mjs +1 -0
- package/src/runtime/domain/background-service/supervisor/generated/process.mjs +49 -11
- package/src/runtime/domain/background-service/supervisor/generated/runner.mjs +47 -4
- package/src/runtime/domain/background-service/supervisor/generated/store.mjs +3 -3
- package/src/runtime/domain/background-service/supervisor/generated/watch-fingerprint.mjs +69 -0
- package/src/runtime/domain/background-service/supervisor/runner.domain.mjs +47 -4
- package/src/runtime/domain/background-service/supervisor/runner.ts +38 -3
- package/src/runtime/domain/background-service/supervisor/store.domain.mjs +3 -3
- package/src/runtime/domain/background-service/supervisor/store.ts +3 -3
- package/src/runtime/domain/background-service/supervisor/supervisor.domain.mjs +9 -3
- package/src/runtime/domain/background-service/supervisor/supervisor.ts +6 -3
- package/src/runtime/domain/background-service/tools.domain.mjs +22 -16
- package/src/runtime/domain/background-service/tools.ts +26 -16
- package/src/runtime/domain/background-service/types.ts +4 -2
- package/src/runtime/domain/background-service/watch-fingerprint.domain.mjs +67 -0
- package/src/runtime/domain/background-service/watch-fingerprint.ts +70 -0
- package/src/runtime/domain/catalog/catalog-preferences.mjs +1 -3
- package/src/runtime/domain/coordinator/src/outbox.mjs +7 -0
- package/src/runtime/domain/coordinator/src/store.mjs +5 -1
- package/src/runtime/domain/log-paths.mjs +8 -1
- package/src/runtime/domain/parse.mjs +9 -5
- package/src/runtime/domain/sandbox-policy/README.md +3 -3
- package/src/runtime/domain/sandbox-policy/package.json +1 -1
- package/src/runtime/domain/sandbox-policy/shared-harness-settings.domain.mjs +2 -3
- package/src/runtime/domain/sandbox-policy/shared-harness-settings.ts +2 -5
- package/src/runtime/domain/session-store.mjs +7 -0
- package/src/runtime/domain/shared-harness-settings.mjs +2 -3
- package/src/runtime/domain/subagents/README.md +12 -10
- package/src/runtime/domain/subagents/assignment-outcomes.domain.mjs +2 -5
- package/src/runtime/domain/subagents/assignment-outcomes.ts +2 -5
- package/src/runtime/domain/subagents/assignment-report.domain.mjs +22 -0
- package/src/runtime/domain/subagents/assignment-report.ts +20 -0
- package/src/runtime/domain/subagents/batch.mjs +13 -5
- package/src/runtime/domain/subagents/catalog-preferences.domain.mjs +1 -3
- package/src/runtime/domain/subagents/catalog-preferences.ts +1 -5
- package/src/runtime/domain/subagents/cleanup.domain.mjs +11 -2
- package/src/runtime/domain/subagents/cleanup.ts +10 -2
- package/src/runtime/domain/subagents/config.domain.mjs +5 -0
- package/src/runtime/domain/subagents/config.ts +8 -2
- package/src/runtime/domain/subagents/continuation.domain.mjs +4 -2
- package/src/runtime/domain/subagents/continuation.ts +4 -2
- package/src/runtime/domain/subagents/extensions.mjs +1 -0
- package/src/runtime/domain/subagents/failures.domain.mjs +11 -2
- package/src/runtime/domain/subagents/failures.ts +10 -2
- package/src/runtime/domain/subagents/finalization.domain.mjs +39 -32
- package/src/runtime/domain/subagents/finalization.ts +37 -30
- package/src/runtime/domain/subagents/git-workspace.domain.mjs +51 -6
- package/src/runtime/domain/subagents/git-workspace.ts +48 -6
- package/src/runtime/domain/subagents/index.domain.mjs +208 -75
- package/src/runtime/domain/subagents/index.ts +213 -75
- package/src/runtime/domain/subagents/isolated-workspace.domain.mjs +508 -0
- package/src/runtime/domain/subagents/isolated-workspace.ts +551 -0
- package/src/runtime/domain/subagents/list.mjs +2 -2
- package/src/runtime/domain/subagents/managed-launch-client.domain.mjs +22 -5
- package/src/runtime/domain/subagents/managed-launch-client.ts +23 -6
- package/src/runtime/domain/subagents/output-payload.domain.mjs +24 -9
- package/src/runtime/domain/subagents/output-payload.ts +26 -12
- package/src/runtime/domain/subagents/package.json +4 -2
- package/src/runtime/domain/subagents/parse.domain.mjs +9 -5
- package/src/runtime/domain/subagents/parse.ts +10 -5
- package/src/runtime/domain/subagents/registry.domain.mjs +31 -25
- package/src/runtime/domain/subagents/registry.ts +31 -13
- package/src/runtime/domain/subagents/retained-result.domain.mjs +56 -0
- package/src/runtime/domain/subagents/retained-result.ts +65 -0
- package/src/runtime/domain/subagents/session-selection.domain.mjs +3 -1
- package/src/runtime/domain/subagents/session-selection.ts +2 -1
- package/src/runtime/domain/subagents/session-store.domain.mjs +7 -0
- package/src/runtime/domain/subagents/session-store.ts +9 -0
- package/src/runtime/domain/subagents/settings-page.domain.mjs +151 -107
- package/src/runtime/domain/subagents/settings-page.ts +147 -108
- package/src/runtime/domain/subagents/shared-catalog.domain.mjs +23 -7
- package/src/runtime/domain/subagents/shared-catalog.ts +23 -7
- package/src/runtime/domain/subagents/shared-harness-settings.domain.mjs +2 -3
- package/src/runtime/domain/subagents/shared-harness-settings.ts +2 -5
- package/src/runtime/domain/subagents/shared-navigator.domain.mjs +55 -28
- package/src/runtime/domain/subagents/shared-navigator.ts +47 -26
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/outbox.domain.mjs +7 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/outbox.ts +9 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.domain.mjs +5 -1
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.ts +6 -0
- package/src/runtime/domain/subagents/task-guard.domain.mjs +14 -1
- package/src/runtime/domain/subagents/task-guard.ts +13 -1
- package/src/runtime/domain/subagents/task-policy.domain.mjs +2 -1
- package/src/runtime/domain/subagents/task-policy.ts +5 -1
- package/src/runtime/domain/subagents/tools.domain.mjs +4 -8
- package/src/runtime/domain/subagents/tools.ts +4 -7
- package/src/runtime/domain/subagents/work-observation.domain.mjs +25 -1
- package/src/runtime/domain/subagents/work-observation.ts +20 -1
- package/src/runtime/domain/subagents/workspace-files.domain.mjs +103 -0
- package/src/runtime/domain/subagents/workspace-files.ts +114 -0
- package/src/runtime/domain/subagents/workspace-prepare-worker.mjs +12 -0
- package/src/runtime/domain/subagents/workspace-scope.domain.d.mts +38 -0
- package/src/runtime/domain/subagents/workspace-scope.domain.mjs +5 -0
- package/src/runtime/domain/subagents/workspace-scope.ts +6 -0
- package/src/runtime/domain/subagents/worktree-feedback.domain.mjs +55 -0
- package/src/runtime/domain/subagents/worktree-feedback.ts +77 -0
- package/src/runtime/read-model.ts +4 -2
- package/src/runtime/root.ts +5 -2
- package/src/shared-ssh-core/index.ts +1 -1
- package/src/version.ts +1 -1
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { reclaimWorkspace } from "./isolated-workspace.ts";
|
|
2
|
+
import { retainResult } from "./retained-result.ts";
|
|
1
3
|
/**
|
|
2
4
|
* Run registry — durable metadata for each spawned subagent.
|
|
3
5
|
*
|
|
@@ -30,7 +32,7 @@ import {
|
|
|
30
32
|
durableSessionsDir,
|
|
31
33
|
listArchivedAttempts,
|
|
32
34
|
readArchivedAttempt,
|
|
33
|
-
recordArchiveStatusTombstone,
|
|
35
|
+
recordArchiveStatusTombstone, retainedLogPath,
|
|
34
36
|
} from "./session-store.ts";
|
|
35
37
|
import { failureIdentity, observeFailures, readFailureState } from "./shared-failure-observations.ts";
|
|
36
38
|
|
|
@@ -129,6 +131,8 @@ export interface LaunchRecipe {
|
|
|
129
131
|
/** Confinement root when the attempt was sandboxed. */
|
|
130
132
|
sandboxDir?: string;
|
|
131
133
|
gitCloneWorkspace: boolean;
|
|
134
|
+
workspace?: import("./isolated-workspace.ts").IsolatedWorkspace;
|
|
135
|
+
workspaceAuthority?: Pick<import("./worker-waits.ts").WorkerRunPolicy, "sandbox" | "mandatoryPermissions">;
|
|
132
136
|
callback: boolean;
|
|
133
137
|
name?: string;
|
|
134
138
|
agent?: string;
|
|
@@ -327,6 +331,11 @@ export function runDir(id: string): string {
|
|
|
327
331
|
export function logPathFor(id: string): string {
|
|
328
332
|
return join(runDir(id), "output.log");
|
|
329
333
|
}
|
|
334
|
+
export function evidenceLogPath(id: string): string {
|
|
335
|
+
const live = logPathFor(id);
|
|
336
|
+
if (existsSync(live) || existsSync(runDir(id))) return live;
|
|
337
|
+
return retainedLogPath(id) ?? live;
|
|
338
|
+
}
|
|
330
339
|
export function promptPathFor(id: string): string {
|
|
331
340
|
return join(runDir(id), "prompt.md");
|
|
332
341
|
}
|
|
@@ -1083,7 +1092,8 @@ function takeToken(lockPath: string, payload: string, deadline: number, id: stri
|
|
|
1083
1092
|
* decides who may recover or release it. A live pid — even one paused well
|
|
1084
1093
|
* past two seconds — is not stolen. Release renames the lock aside and deletes
|
|
1085
1094
|
* it only when the moved bytes are still ours, so a newer owner's lock stays.
|
|
1086
|
-
* The lock is per run
|
|
1095
|
+
* The lock is per run. Metadata transitions that depend on the current record
|
|
1096
|
+
* use a patch so that the read and preconditions are repeated after lock loss.
|
|
1087
1097
|
*/
|
|
1088
1098
|
function withRunMetaLock(id: string, body: (owns: () => boolean) => void): void {
|
|
1089
1099
|
const directory = runDir(id);
|
|
@@ -1261,7 +1271,7 @@ export function writeMeta(meta: RunMeta, options: {
|
|
|
1261
1271
|
ifAbsent?: boolean;
|
|
1262
1272
|
/** Build a field update from current metadata under the existing run lock. */
|
|
1263
1273
|
patch?: (current: Readonly<RunMeta>) => Partial<RunMeta> | undefined;
|
|
1264
|
-
} = {}):
|
|
1274
|
+
} = {}): RunMeta | undefined {
|
|
1265
1275
|
if (options.patch && !readMeta(meta.id)) return;
|
|
1266
1276
|
mkdirSync(runDir(meta.id), { recursive: true });
|
|
1267
1277
|
let written = false;
|
|
@@ -1294,10 +1304,11 @@ export function writeMeta(meta: RunMeta, options: {
|
|
|
1294
1304
|
owns();
|
|
1295
1305
|
written = true;
|
|
1296
1306
|
});
|
|
1297
|
-
if (options.patch && !written) return;
|
|
1307
|
+
if (options.patch && !written) return undefined;
|
|
1298
1308
|
for (const listener of metaChangedListeners) {
|
|
1299
1309
|
try { listener(meta); } catch { /* best effort */ }
|
|
1300
1310
|
}
|
|
1311
|
+
return written ? meta : undefined;
|
|
1301
1312
|
}
|
|
1302
1313
|
|
|
1303
1314
|
export function onMetaChanged(listener: (meta?: RunMeta) => void): () => void {
|
|
@@ -1338,6 +1349,7 @@ export interface RemoveMetaArtifactsOptions {
|
|
|
1338
1349
|
* only its volatile log, metadata, and task scratch are reclaimed.
|
|
1339
1350
|
*/
|
|
1340
1351
|
keepWorkspace?: boolean;
|
|
1352
|
+
errors?: string[];
|
|
1341
1353
|
}
|
|
1342
1354
|
|
|
1343
1355
|
export function removeMetaArtifacts(meta: RunMeta, options: RemoveMetaArtifactsOptions = {}): boolean {
|
|
@@ -1348,14 +1360,21 @@ export function removeMetaArtifacts(meta: RunMeta, options: RemoveMetaArtifactsO
|
|
|
1348
1360
|
// conversation's visible last word and could be continued as if this
|
|
1349
1361
|
// attempt had never happened. A marker that cannot be published retains
|
|
1350
1362
|
// the run (the throw below): the unsafe direction is deleting both.
|
|
1363
|
+
// Called under the conversation mutation lock by both retention sweeps.
|
|
1364
|
+
if (assignmentRetainsEvidence(meta)) throw new Error("Assignment outcome or uncertain writer still retains this run's evidence.");
|
|
1365
|
+
const scratch = meta.taskRuntime && meta.taskScratch && dirname(meta.taskScratch) === realpathSync(tmpdir()) &&
|
|
1366
|
+
/^pi-task-scratch-[a-z0-9]{6}$/i.test(basename(meta.taskScratch)) ? meta.taskScratch : undefined;
|
|
1367
|
+
if (meta.timing?.stopReason || meta.recipe?.workspace || scratch) retainResult(meta, logPathFor(meta.id), scratch);
|
|
1368
|
+
const workspace = meta.recipe?.workspace;
|
|
1369
|
+
if (workspace) reclaimWorkspace(workspace, meta.id);
|
|
1351
1370
|
recordArchiveStatusTombstone(meta);
|
|
1352
1371
|
removeRunDirectory(meta.id);
|
|
1353
|
-
if (!options.keepWorkspace
|
|
1372
|
+
if (!workspace && !meta.timing?.stopReason && !options.keepWorkspace
|
|
1354
1373
|
&& meta.taskRuntime && /^sa_[a-z0-9]+_[a-z0-9]+$/i.test(meta.id) && meta.cwd === taskWorkspaceDir(meta.id)) {
|
|
1355
1374
|
rmSync(taskWorkspaceDir(meta.id), { recursive: true, force: true });
|
|
1356
1375
|
}
|
|
1357
|
-
if (
|
|
1358
|
-
rmSync(
|
|
1376
|
+
if (scratch) {
|
|
1377
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
1359
1378
|
}
|
|
1360
1379
|
metaCache.delete(meta.id);
|
|
1361
1380
|
removeIndexEntry(join(baseDir(), "by-parent", String(meta.spawnPid)), meta.id);
|
|
@@ -1365,7 +1384,8 @@ export function removeMetaArtifacts(meta: RunMeta, options: RemoveMetaArtifactsO
|
|
|
1365
1384
|
try { listener(meta); } catch { /* best effort */ }
|
|
1366
1385
|
}
|
|
1367
1386
|
return true;
|
|
1368
|
-
} catch {
|
|
1387
|
+
} catch (error) {
|
|
1388
|
+
options.errors?.push(meta.cwd + ": " + String(error));
|
|
1369
1389
|
return false;
|
|
1370
1390
|
}
|
|
1371
1391
|
}
|
|
@@ -1875,11 +1895,9 @@ export function isDismissed(meta: Pick<RunMeta, "dismissedAt">): boolean {
|
|
|
1875
1895
|
export function dismissRun(id: string, at: number = Date.now()): RunMeta | undefined {
|
|
1876
1896
|
const meta = readMeta(id);
|
|
1877
1897
|
if (!meta) return undefined;
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
}
|
|
1882
|
-
return meta;
|
|
1898
|
+
return writeMeta(meta, {
|
|
1899
|
+
patch: current => current.dismissedAt === undefined ? { dismissedAt: at } : undefined,
|
|
1900
|
+
}) ?? readMeta(id);
|
|
1883
1901
|
}
|
|
1884
1902
|
|
|
1885
1903
|
/**
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// Generated fixed launch domain from packages/pi-better-subagents/retained-result.ts. Do not edit directly.
|
|
2
|
+
import { copyFileSync, cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync, } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { snapshotTree, verifyTree } from "./workspace-files.domain.mjs";
|
|
5
|
+
import { resultDir } from "./session-store.domain.mjs";
|
|
6
|
+
// Published only while retiring a quiescent attempt. A result record can answer reads,
|
|
7
|
+
// but cannot authorize admission, revive a writer or make a failed attempt resumable.
|
|
8
|
+
export function retainResult(meta, logPath, scratch) {
|
|
9
|
+
const dir = resultDir(meta.id);
|
|
10
|
+
if (!dir)
|
|
11
|
+
throw new Error("Invalid result identity");
|
|
12
|
+
if (existsSync(dir)) {
|
|
13
|
+
if (readRetainedResult(meta.id)?.sessionId !== meta.sessionId)
|
|
14
|
+
throw new Error("Retained result identity changed");
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
mkdirSync(dirname(dir), { recursive: true, mode: 0o700 });
|
|
18
|
+
const temp = mkdtempSync(dir + "-");
|
|
19
|
+
try {
|
|
20
|
+
for (const name of ["output.log", "failures.jsonl"]) {
|
|
21
|
+
const source = join(dirname(logPath), name);
|
|
22
|
+
if (existsSync(source))
|
|
23
|
+
copyFileSync(source, join(temp, name));
|
|
24
|
+
}
|
|
25
|
+
let savedScratch;
|
|
26
|
+
if (scratch && existsSync(scratch)) {
|
|
27
|
+
const files = snapshotTree(scratch);
|
|
28
|
+
if (files.some((entry) => entry.name !== ".sandbox-anchor")) {
|
|
29
|
+
cpSync(scratch, join(temp, "scratch"), { recursive: true, verbatimSymlinks: true });
|
|
30
|
+
verifyTree(join(temp, "scratch"), files);
|
|
31
|
+
verifyTree(scratch, files);
|
|
32
|
+
savedScratch = join(dir, "scratch");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
writeFileSync(join(temp, "result.json"), JSON.stringify({ ...meta, taskScratch: savedScratch, logPath: join(dir, "output.log") }), { mode: 0o600 });
|
|
36
|
+
renameSync(temp, dir);
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
rmSync(temp, { recursive: true, force: true });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export function readRetainedResult(id) {
|
|
43
|
+
const dir = resultDir(id);
|
|
44
|
+
if (!dir)
|
|
45
|
+
return;
|
|
46
|
+
try {
|
|
47
|
+
const meta = JSON.parse(readFileSync(join(dir, "result.json"), "utf8"));
|
|
48
|
+
if (meta.id === id && ["completed", "killed", "failed", "lost"].includes(meta.status))
|
|
49
|
+
return meta;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (error.code !== "ENOENT")
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {
|
|
2
|
+
copyFileSync,
|
|
3
|
+
cpSync,
|
|
4
|
+
existsSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
mkdtempSync,
|
|
7
|
+
readFileSync,
|
|
8
|
+
renameSync,
|
|
9
|
+
rmSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from "node:fs";
|
|
12
|
+
import { dirname, join } from "node:path";
|
|
13
|
+
import { snapshotTree, verifyTree } from "./workspace-files.ts";
|
|
14
|
+
import type { RunMeta } from "./registry.ts";
|
|
15
|
+
import { resultDir } from "./session-store.ts";
|
|
16
|
+
|
|
17
|
+
// Published only while retiring a quiescent attempt. A result record can answer reads,
|
|
18
|
+
// but cannot authorize admission, revive a writer or make a failed attempt resumable.
|
|
19
|
+
export function retainResult(meta: RunMeta, logPath: string, scratch?: string): void {
|
|
20
|
+
const dir = resultDir(meta.id);
|
|
21
|
+
if (!dir) throw new Error("Invalid result identity");
|
|
22
|
+
if (existsSync(dir)) {
|
|
23
|
+
if (readRetainedResult(meta.id)?.sessionId !== meta.sessionId)
|
|
24
|
+
throw new Error("Retained result identity changed");
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
mkdirSync(dirname(dir), { recursive: true, mode: 0o700 });
|
|
28
|
+
const temp = mkdtempSync(dir + "-");
|
|
29
|
+
try {
|
|
30
|
+
for (const name of ["output.log", "failures.jsonl"]) {
|
|
31
|
+
const source = join(dirname(logPath), name);
|
|
32
|
+
if (existsSync(source)) copyFileSync(source, join(temp, name));
|
|
33
|
+
}
|
|
34
|
+
let savedScratch: string | undefined;
|
|
35
|
+
if (scratch && existsSync(scratch)) {
|
|
36
|
+
const files = snapshotTree(scratch);
|
|
37
|
+
if (files.some((entry) => entry.name !== ".sandbox-anchor")) {
|
|
38
|
+
cpSync(scratch, join(temp, "scratch"), { recursive: true, verbatimSymlinks: true });
|
|
39
|
+
verifyTree(join(temp, "scratch"), files);
|
|
40
|
+
verifyTree(scratch, files);
|
|
41
|
+
savedScratch = join(dir, "scratch");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
writeFileSync(
|
|
45
|
+
join(temp, "result.json"),
|
|
46
|
+
JSON.stringify({ ...meta, taskScratch: savedScratch, logPath: join(dir, "output.log") }),
|
|
47
|
+
{ mode: 0o600 },
|
|
48
|
+
);
|
|
49
|
+
renameSync(temp, dir);
|
|
50
|
+
} finally {
|
|
51
|
+
rmSync(temp, { recursive: true, force: true });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function readRetainedResult(id: string): RunMeta | undefined {
|
|
56
|
+
const dir = resultDir(id);
|
|
57
|
+
if (!dir) return;
|
|
58
|
+
try {
|
|
59
|
+
const meta = JSON.parse(readFileSync(join(dir, "result.json"), "utf8")) as RunMeta;
|
|
60
|
+
if (meta.id === id && ["completed", "killed", "failed", "lost"].includes(meta.status)) return meta;
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
// Generated fixed launch domain from packages/pi-better-subagents/session-selection.ts. Do not edit directly.
|
|
2
|
+
/** Exact-name selection of saved conversations; attempt IDs never become session IDs. */
|
|
3
|
+
import { discoveryWorkspace } from "./workspace-scope.domain.mjs";
|
|
2
4
|
/**
|
|
3
5
|
* Select a single conversation in the caller's workspace scope. Several attempts
|
|
4
6
|
* of one conversation count once; distinct conversations with the same display
|
|
@@ -18,7 +20,7 @@ export function selectSessionByName(metas, name, inWorkspace) {
|
|
|
18
20
|
latest.set(meta.sessionId, meta);
|
|
19
21
|
}
|
|
20
22
|
const matches = [...latest.values()]
|
|
21
|
-
.filter((meta) => meta.name === requested && inWorkspace(meta
|
|
23
|
+
.filter((meta) => meta.name === requested && inWorkspace(discoveryWorkspace(meta)))
|
|
22
24
|
.sort((a, b) => a.sessionId.localeCompare(b.sessionId));
|
|
23
25
|
if (matches.length === 0) {
|
|
24
26
|
throw new Error(`No saved subagent conversation named ${JSON.stringify(requested)} in the current folder. Use subagent_list({all:true}) to find its session_id.`);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/** Exact-name selection of saved conversations; attempt IDs never become session IDs. */
|
|
2
|
+
import { discoveryWorkspace } from "./workspace-scope.ts";
|
|
2
3
|
import type { RunMeta } from "./registry.ts";
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -21,7 +22,7 @@ export function selectSessionByName(
|
|
|
21
22
|
if (!previous || meta.startedAt > previous.startedAt) latest.set(meta.sessionId, meta);
|
|
22
23
|
}
|
|
23
24
|
const matches = [...latest.values()]
|
|
24
|
-
.filter((meta) => meta.name === requested && inWorkspace(meta
|
|
25
|
+
.filter((meta) => meta.name === requested && inWorkspace(discoveryWorkspace(meta)))
|
|
25
26
|
.sort((a, b) => a.sessionId.localeCompare(b.sessionId));
|
|
26
27
|
if (matches.length === 0) {
|
|
27
28
|
throw new Error(`No saved subagent conversation named ${JSON.stringify(requested)} in the current folder. Use subagent_list({all:true}) to find its session_id.`);
|
|
@@ -72,6 +72,13 @@ export function durableSubagentRoot(env = process.env, home = homedir()) {
|
|
|
72
72
|
const agentDir = env.PI_CODING_AGENT_DIR?.trim();
|
|
73
73
|
return join(agentDir || join(home, ".pi", "agent"), "subagents");
|
|
74
74
|
}
|
|
75
|
+
export function resultDir(id) {
|
|
76
|
+
return /^sa_[a-z0-9]+_[a-z0-9]+$/i.test(id) ? join(durableSubagentRoot(), "results", id) : undefined;
|
|
77
|
+
}
|
|
78
|
+
export function retainedLogPath(id) {
|
|
79
|
+
const dir = resultDir(id);
|
|
80
|
+
return dir && join(dir, "output.log");
|
|
81
|
+
}
|
|
75
82
|
/** Durable `--session-dir` for new child launches. */
|
|
76
83
|
export function durableSessionsDir() {
|
|
77
84
|
return join(durableSubagentRoot(), "sessions");
|
|
@@ -83,6 +83,15 @@ export function durableSubagentRoot(env: NodeJS.ProcessEnv = process.env, home:
|
|
|
83
83
|
return join(agentDir || join(home, ".pi", "agent"), "subagents");
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
export function resultDir(id: string): string | undefined {
|
|
87
|
+
return /^sa_[a-z0-9]+_[a-z0-9]+$/i.test(id) ? join(durableSubagentRoot(), "results", id) : undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function retainedLogPath(id: string): string | undefined {
|
|
91
|
+
const dir = resultDir(id);
|
|
92
|
+
return dir && join(dir, "output.log");
|
|
93
|
+
}
|
|
94
|
+
|
|
86
95
|
/** Durable `--session-dir` for new child launches. */
|
|
87
96
|
export function durableSessionsDir(): string {
|
|
88
97
|
return join(durableSubagentRoot(), "sessions");
|