@hearsay-tools/squawk 0.18.0-dev.7801 → 0.18.0-dev.7808
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/activity/read-model.js +1 -1
- package/dist/activity/read-model.js.map +1 -1
- package/dist/activity/requests.d.ts +3 -1
- package/dist/activity/requests.js +1 -1
- package/dist/activity/requests.js.map +1 -1
- package/dist/built-in-run-slots.d.ts +4 -3
- package/dist/built-in-run-slots.js +7 -3
- package/dist/built-in-run-slots.js.map +1 -1
- package/dist/ci-wait/tools.d.ts +4 -4
- package/dist/delegation/execution-identity.d.ts +7 -2
- package/dist/delegation/execution-identity.js +34 -3
- package/dist/delegation/execution-identity.js.map +1 -1
- package/dist/delegation/family-end.d.ts +3 -1
- package/dist/delegation/private-record-file.js.map +1 -0
- package/dist/delegation/removal-slot.js +1 -1
- package/dist/delegation/removal-slot.js.map +1 -1
- package/dist/delegation/results.js +6 -6
- package/dist/delegation/results.js.map +1 -1
- package/dist/delegation/routes.d.ts +15 -15
- package/dist/delegation/scratch-cleanup.js +5 -5
- package/dist/delegation/scratch-cleanup.js.map +1 -1
- package/dist/delegation/service.js +11 -10
- package/dist/delegation/service.js.map +1 -1
- package/dist/delegation/transport.d.ts +4 -4
- package/dist/delegation/worker-cleanup.js +13 -12
- package/dist/delegation/worker-cleanup.js.map +1 -1
- package/dist/{runs → delegation}/worker-deletion-proof.d.ts +1 -1
- package/dist/delegation/worker-deletion-proof.js.map +1 -0
- package/dist/delegation/worker-executions.d.ts +70 -0
- package/dist/delegation/worker-executions.js +248 -0
- package/dist/delegation/worker-executions.js.map +1 -0
- package/dist/{runs → delegation}/worker-resource-holders.d.ts +1 -1
- package/dist/delegation/worker-resource-holders.js.map +1 -0
- package/dist/preview/host.d.ts +3 -1
- package/dist/preview/host.js +1 -1
- package/dist/preview/host.js.map +1 -1
- package/dist/runs/owned-run-create.d.ts +3 -3
- package/dist/runs/owned-run-create.js +1 -2
- package/dist/runs/owned-run-create.js.map +1 -1
- package/dist/runs/ports.d.ts +48 -21
- package/dist/runs/removal-slots.d.ts +4 -2
- package/dist/runs/retention.d.ts +6 -2
- package/dist/runs/retention.js +3 -3
- package/dist/runs/retention.js.map +1 -1
- package/dist/runs/run-retention.d.ts +4 -7
- package/dist/runs/run-retention.js +7 -7
- package/dist/runs/run-retention.js.map +1 -1
- package/dist/runs/store.d.ts +10 -63
- package/dist/runs/store.js +34 -327
- package/dist/runs/store.js.map +1 -1
- package/dist/runs/worker-executions-registry.d.ts +31 -0
- package/dist/runs/worker-executions-registry.js +20 -0
- package/dist/runs/worker-executions-registry.js.map +1 -0
- package/dist/runs/worker-result-commit.js +1 -1
- package/dist/runs/worker-results.d.ts +38 -0
- package/dist/runs/worker-results.js +87 -0
- package/dist/runs/worker-results.js.map +1 -0
- package/dist/server/activity-routes.d.ts +23 -23
- package/dist/server/run-relationships.d.ts +3 -1
- package/dist/server/run-relationships.js +1 -1
- package/dist/server/run-relationships.js.map +1 -1
- package/dist/server/runners-routes.d.ts +10 -10
- package/dist/server/server.d.ts +6 -6
- package/dist/server/workspace-live-routes.d.ts +6 -6
- package/dist/workflows/run/completion-gate.js +2 -2
- package/dist/workflows/run/completion-gate.js.map +1 -1
- package/dist/workflows/run/continuation-session.js +1 -1
- package/dist/workflows/run/continuation-session.js.map +1 -1
- package/dist/workflows/run/family-reconciler.js +3 -3
- package/dist/workflows/run/family-reconciler.js.map +1 -1
- package/dist/workflows/run/launch-read-resolver.js +2 -2
- package/dist/workflows/run/launch-read-resolver.js.map +1 -1
- package/dist/workflows/run/run-cancel.js +4 -4
- package/dist/workflows/run/run-cancel.js.map +1 -1
- package/dist/workflows/run/run-recovery.js +2 -2
- package/dist/workflows/run/run-recovery.js.map +1 -1
- package/dist/workflows/run/step-loop.js +2 -2
- package/dist/workflows/run/step-loop.js.map +1 -1
- package/dist/workflows/run/step-session.js +1 -1
- package/dist/workflows/run/step-session.js.map +1 -1
- package/dist/workflows/run/worker-executions.js +6 -6
- package/dist/workflows/run/worker-executions.js.map +1 -1
- package/dist/workflows/run/worker-orphans.js +8 -8
- package/dist/workflows/run/worker-orphans.js.map +1 -1
- package/package.json +4 -4
- package/scripts/benchmark-stuck-destroys.ts +2 -2
- package/scripts/benchmark-worker-lifecycle.ts +3 -2
- package/dist/runs/private-record-file.js.map +0 -1
- package/dist/runs/worker-deletion-proof.js.map +0 -1
- package/dist/runs/worker-resource-holders.js.map +0 -1
- /package/dist/{runs → delegation}/private-record-file.d.ts +0 -0
- /package/dist/{runs → delegation}/private-record-file.js +0 -0
- /package/dist/{runs → delegation}/worker-deletion-proof.js +0 -0
- /package/dist/{runs → delegation}/worker-resource-holders.js +0 -0
package/dist/runs/store.js
CHANGED
|
@@ -1,27 +1,20 @@
|
|
|
1
|
-
import { legacyIndexRows, readableSavedRunner, upgradeLegacyRunRow,
|
|
1
|
+
import { legacyIndexRows, readableSavedRunner, upgradeLegacyRunRow, withKeptFields } from './legacy-run-rows.js';
|
|
2
2
|
import { dataReadDirs, runDataDir } from '../project-paths.js';
|
|
3
|
-
import { addRunSource, copyFallbackFamily, fallbackRunSource,
|
|
3
|
+
import { addRunSource, copyFallbackFamily, fallbackRunSource, fallbackRunDeleteError } from './project-run-sources.js';
|
|
4
4
|
import { productEnv } from '../core/product-env.js';
|
|
5
5
|
import { registeredWaitRecord, registeredReceiptRecord } from './wait-registration-write.js';
|
|
6
6
|
import { stageCommit, stageLoaded, stageDurableUpdate, stageDebouncedUpdate, stageConversation, stageArchiveFields, stagePullRequests, stageStepUpdate, stepTotals, redactPatch, redactText, clearPin } from './record-write-rules.js';
|
|
7
7
|
import { publishRunWrite, RunLifecycle, snapshotRun } from './lifecycle.js';
|
|
8
|
-
import { workerDeletionProof } from './worker-deletion-proof.js';
|
|
9
|
-
import { recordedHolders, workerResourceAuthorized } from './worker-resource-holders.js';
|
|
10
8
|
import { listPeerTaskSummaries, parseStoredSummary } from './run-summary-page.js';
|
|
11
9
|
export { parseStoredSummary } from './run-summary-page.js';
|
|
12
10
|
import { defined } from '../core/defined.js';
|
|
13
|
-
import {
|
|
14
|
-
import { admissibleExecution, holderRefusal, SPAWN_NEXT, WorkerAdmissionRefusal, workerExecutionSchema } from './worker-execution.js';
|
|
15
|
-
import { agentTmpDirMayExist, agentTmpDirOwnershipProven } from './agent-tmpdir.js';
|
|
11
|
+
import { workerExecutionsFactory } from './worker-executions-registry.js';
|
|
16
12
|
import { randomUUID, createHash } from 'node:crypto';
|
|
17
13
|
import { isDeepStrictEqual } from 'node:util';
|
|
18
|
-
import { appendFileSync,
|
|
19
|
-
import { join
|
|
14
|
+
import { appendFileSync, existsSync, mkdirSync } from 'node:fs';
|
|
15
|
+
import { join } from 'node:path';
|
|
20
16
|
import { z } from 'zod';
|
|
21
|
-
import {
|
|
22
|
-
import { WorkerScopes } from '../worker-scope/worker-scopes.js';
|
|
23
|
-
import { readPrivateRecord, writePrivateRecord } from './private-record-file.js';
|
|
24
|
-
import { delegationStateSchema, workerResultFileSchema, workerDestroyRetrySchema, } from '../contract/index.js';
|
|
17
|
+
import { delegationStateSchema, workerDestroyRetrySchema, } from '../contract/index.js';
|
|
25
18
|
import { agentInputSchema, inboxClaimSchema } from '../contract/index.js';
|
|
26
19
|
import { automationIdOf, stepStateSchema as contractStepStateSchema, toRunSummary, runRecordSchema as contractRunRecordSchema, runPullRequestSchema, mergedPullRequestSchema, } from '../contract/index.js';
|
|
27
20
|
import { queuedMessageSchema } from '../contract/index.js';
|
|
@@ -36,8 +29,6 @@ import { LiveItemSnapshots } from './live-item-snapshots.js';
|
|
|
36
29
|
import { stampOf } from './transcript-facts.js';
|
|
37
30
|
import { RunTranscriptFacts } from './transcript-facts-topic.js';
|
|
38
31
|
import { hasPlainHistory, historyPaths, readHistoryText, readHistoryTextAsync, restoreHistory } from './history-file.js';
|
|
39
|
-
import { workerExecutionIdentitySchema } from '../delegation/execution-identity.js';
|
|
40
|
-
import { startToken } from '../core/process-stat.js';
|
|
41
32
|
import { collectSecretValues, redactDeep } from '../core/secret-redaction.js';
|
|
42
33
|
// Pure, dependency-free reference helpers — the same sanity bound the marker parser applies.
|
|
43
34
|
import { archivePullRequests, pullRequestKey } from './pull-requests.js';
|
|
@@ -67,7 +58,7 @@ import { toRunStoreOpenError } from './store-open-error.js';
|
|
|
67
58
|
import { archiveStoreFamily } from './archive-family.js';
|
|
68
59
|
import { claimStoreFamilies } from './claim-families.js';
|
|
69
60
|
import { createStoreOwnedRun } from './owned-run-create.js';
|
|
70
|
-
import {
|
|
61
|
+
import { WorkerResultsTopic } from './worker-results.js';
|
|
71
62
|
import { claimOwnerLive, closeClaimSession, familyKey, foreignClaimedRunIds, liveForeignClaimedRunIds, openClaimSession, rowFamily } from './run-claims.js';
|
|
72
63
|
import { assertProjectWritable, projectWriteRefused } from '../server/cockpit-ownership.js';
|
|
73
64
|
/**
|
|
@@ -401,11 +392,6 @@ function deepFreeze(value) {
|
|
|
401
392
|
}
|
|
402
393
|
return value;
|
|
403
394
|
}
|
|
404
|
-
const WORKER_PROCESS_CAP = 32;
|
|
405
|
-
const recordedProcessSchema = z.object({ pid: z.number().int().positive(), startToken: z.string().min(1).max(128).optional(),
|
|
406
|
-
pgid: z.number().int().positive().optional() }).strict();
|
|
407
|
-
const workerProcessRecordSchema = z.object({ generation: z.string().uuid(), controller: recordedProcessSchema,
|
|
408
|
-
processes: z.array(recordedProcessSchema).max(WORKER_PROCESS_CAP) }).strict();
|
|
409
395
|
/**
|
|
410
396
|
* Run rows in runs.db plus append-only NDJSON events and an in-process event bus. Hold live,
|
|
411
397
|
* pinned, related and unsettled records only. Optimistic writes debounce; durable commits
|
|
@@ -435,8 +421,6 @@ export class RunStore {
|
|
|
435
421
|
lifecycle = this.published;
|
|
436
422
|
/** The lifecycle events run exits write (B12). */
|
|
437
423
|
exitEvents = new ExitEvents((id, event) => this.appendEvent(id, event));
|
|
438
|
-
/** Every worker's process scope (ADR 0032): spawn sites, Continue and destroy reach it here. */
|
|
439
|
-
scopes = new WorkerScopes(this);
|
|
440
424
|
/** Runs gone from memory whose rows the next save or commit deletes. */
|
|
441
425
|
deleted = new Set();
|
|
442
426
|
/** Deleted runs whose history files (events, handoff, images, artifacts) go once the delete of
|
|
@@ -478,6 +462,18 @@ export class RunStore {
|
|
|
478
462
|
transcriptFacts;
|
|
479
463
|
/** Whether a run may go, and its deletion; the count-based archive (hearsay-tools/squawk#4037). */
|
|
480
464
|
retention;
|
|
465
|
+
/** The saved snapshot of a worker's collected result (hearsay-tools/squawk#4038). */
|
|
466
|
+
workerResults;
|
|
467
|
+
executions;
|
|
468
|
+
/** A worker's execution, process, identity and scope records (hearsay-tools/squawk#4039), built on
|
|
469
|
+
* first use by the factory this process registered; throws when none is registered. */
|
|
470
|
+
get workerExecutions() {
|
|
471
|
+
return this.executions ??= workerExecutionsFactory()(this.dataDir, {
|
|
472
|
+
peek: (id) => this.peek(id), getRun: (id) => this.getRun(id), writeDir: (id) => this.writeDir(id),
|
|
473
|
+
sourceDataDir: (id) => this.sourceDataDir(id), commitIndex: (input) => this.commitIndex(input),
|
|
474
|
+
published: this.published, workerResults: this.workerResults,
|
|
475
|
+
});
|
|
476
|
+
}
|
|
481
477
|
liveItems = new LiveItemSnapshots();
|
|
482
478
|
/** Capture both sources on the same synchronous sequence boundary before history IO. */
|
|
483
479
|
liveReadSnapshot(runId) {
|
|
@@ -494,6 +490,12 @@ export class RunStore {
|
|
|
494
490
|
listConversationRootIds: () => this.listConversationRootIds(),
|
|
495
491
|
listRunIds: () => this.listRunIds(),
|
|
496
492
|
});
|
|
493
|
+
this.workerResults = new WorkerResultsTopic(dataDir, {
|
|
494
|
+
peek: (id) => this.peek(id), hasRun: (id) => this.hasRun(id), hasFallbackRun: (id) => this.hasFallbackRun(id),
|
|
495
|
+
writeDir: (id) => this.writeDir(id), sourceDataDir: (id) => this.sourceDataDir(id),
|
|
496
|
+
redactResult: (result) => this.redact({ type: 'worker-result', seq: 0, ts: result.observedAt, result }).result,
|
|
497
|
+
redactText: (text) => this.redactText(text), commitDelegation: (patches) => this.commitDelegation(patches),
|
|
498
|
+
});
|
|
497
499
|
this.retention = new RunRetentionTopic(dataDir, {
|
|
498
500
|
db: () => this.db, held: this.held, deleted: this.deleted, quarantined: this.quarantined,
|
|
499
501
|
historyOwed: this.historyOwed, seqs: this.seqs, published: this.published, transcriptFacts: this.transcriptFacts,
|
|
@@ -503,14 +505,11 @@ export class RunStore {
|
|
|
503
505
|
claimFamilies: (families) => this.claimFamilies(families), markDeleted: (id, family) => this.markDeleted(id, family),
|
|
504
506
|
scheduleSave: () => this.scheduleSave(), commitIndex: (input) => this.commitIndex(input),
|
|
505
507
|
commitDelegation: (patches) => this.commitDelegation(patches), applyArchived: (run, archived) => this.applyArchived(run, archived),
|
|
506
|
-
workerDeletionEvidence: (id) => this.workerDeletionEvidence(id),
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
readWorkerExecution: (id) => this.readWorkerExecution(id), identityPath: (id) => this.identityPath(id),
|
|
512
|
-
processesPath: (id) => this.processesPath(id), scopePath: (id) => this.scopePath(id),
|
|
513
|
-
executionPath: (id) => this.executionPath(id), handoffPath: (runId) => this.handoffPath(runId),
|
|
508
|
+
workerDeletionEvidence: (id) => this.workerExecutions.workerDeletionEvidence(id),
|
|
509
|
+
workerResults: this.workerResults,
|
|
510
|
+
readWorkerExecution: (id) => this.workerExecutions.readWorkerExecution(id), identityPath: (id) => this.workerExecutions.identityPath(id),
|
|
511
|
+
processesPath: (id) => this.workerExecutions.processesPath(id), scopePath: (id) => this.workerExecutions.scopePath(id),
|
|
512
|
+
executionPath: (id) => this.workerExecutions.executionPath(id), handoffPath: (runId) => this.handoffPath(runId),
|
|
514
513
|
imagesDir: (runId) => this.imagesDir(runId),
|
|
515
514
|
});
|
|
516
515
|
this.compressor = new HistoryCompressor(dataDir, (id) => this.isHistoryCompressEligible(id), (id, plain, archive) => this.transcriptFacts.index.adoptArchive(id, plain, stampOf(archive)));
|
|
@@ -1808,61 +1807,6 @@ export class RunStore {
|
|
|
1808
1807
|
}
|
|
1809
1808
|
this.commitIndex(staged, undefined, { exits: new Map([[id, exit]]) });
|
|
1810
1809
|
}
|
|
1811
|
-
workerResultsDir(parentId, write = false) { return workerResultsDirectory(write ? this.writeDir(parentId) : this.sourceDataDir(parentId), parentId, write); }
|
|
1812
|
-
workerResultSnapshotPath(parentId, workerId, snapshotId) {
|
|
1813
|
-
return join(runDataDir(this.dataDir, parentId), 'runs', `${z.uuid().parse(parentId)}-worker-results`, `${z.uuid().parse(workerId)}.${z.uuid().parse(snapshotId)}.json`);
|
|
1814
|
-
}
|
|
1815
|
-
readWorkerResultFile(parentId, workerId) {
|
|
1816
|
-
const parent = this.peek(parentId);
|
|
1817
|
-
if (parent?.delegation?.role !== 'root' || !parent.delegation.receipts.some(receipt => receipt.workerId === workerId))
|
|
1818
|
-
return undefined;
|
|
1819
|
-
const reference = parent.delegation.results?.find(result => result.workerId === workerId);
|
|
1820
|
-
if (!reference)
|
|
1821
|
-
return undefined;
|
|
1822
|
-
try {
|
|
1823
|
-
const path = join(this.workerResultsDir(parentId), `${z.uuid().parse(workerId)}.${z.uuid().parse(reference.snapshotId)}.json`);
|
|
1824
|
-
const fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
1825
|
-
let raw;
|
|
1826
|
-
try {
|
|
1827
|
-
const info = fstatSync(fd);
|
|
1828
|
-
if (!info.isFile() || info.size > 3_145_728)
|
|
1829
|
-
return undefined;
|
|
1830
|
-
raw = readFileSync(fd, 'utf8');
|
|
1831
|
-
}
|
|
1832
|
-
finally {
|
|
1833
|
-
closeSync(fd);
|
|
1834
|
-
}
|
|
1835
|
-
const file = workerResultFileSchema.parse(upgradeRemovedRunnerResult(JSON.parse(raw)));
|
|
1836
|
-
if (!bindWorkerResultPath(file.result.diff, this.dataDir, this.hasFallbackRun(parentId), parentId, path))
|
|
1837
|
-
return undefined;
|
|
1838
|
-
if (file.result.parentRunId !== parentId || file.result.workerId !== workerId || file.result.workspace.ownerRunId !== workerId || file.result.revision !== reference.revision || file.result.observedAt !== reference.observedAt ||
|
|
1839
|
-
(file.result.diff.state === 'available' && (file.result.diff.snapshotId !== reference.snapshotId || file.diffSnapshot === undefined)))
|
|
1840
|
-
return undefined;
|
|
1841
|
-
return file;
|
|
1842
|
-
}
|
|
1843
|
-
catch {
|
|
1844
|
-
return undefined;
|
|
1845
|
-
}
|
|
1846
|
-
}
|
|
1847
|
-
readWorkerResult(parentId, workerId) {
|
|
1848
|
-
return this.readWorkerResultFile(parentId, workerId)?.result;
|
|
1849
|
-
}
|
|
1850
|
-
readWorkerResultDiff(parentId, workerId) {
|
|
1851
|
-
return this.readWorkerResultFile(parentId, workerId)?.diffSnapshot;
|
|
1852
|
-
}
|
|
1853
|
-
/** Publish a pointer only after its redacted immutable snapshot reaches disk. */
|
|
1854
|
-
commitWorkerResult(parentId, value, diffSnapshot, opts = {}) {
|
|
1855
|
-
return commitStoreWorkerResult({
|
|
1856
|
-
redactResult: (result) => this.redact({ type: 'worker-result', seq: 0, ts: result.observedAt, result }).result,
|
|
1857
|
-
peek: (id) => this.peek(id),
|
|
1858
|
-
readWorkerResultFile: (id, workerId) => this.readWorkerResultFile(id, workerId),
|
|
1859
|
-
readWorkerResult: (id, workerId) => this.readWorkerResult(id, workerId),
|
|
1860
|
-
redactText: (text) => this.redactText(text),
|
|
1861
|
-
workerResultSnapshotPath: (id, workerId, snapshotId) => this.workerResultSnapshotPath(id, workerId, snapshotId),
|
|
1862
|
-
workerResultsDir: (id) => this.workerResultsDir(id, true),
|
|
1863
|
-
commitDelegation: (patches) => this.commitDelegation(patches),
|
|
1864
|
-
}, parentId, value, diffSnapshot, opts);
|
|
1865
|
-
}
|
|
1866
1810
|
/** Create an owned worker run and its parent receipt; `createStoreOwnedRun` holds the checks. */
|
|
1867
1811
|
createOwnedRun(input, parentId, requestId, worker, requestHash, spawnCapacityRefusal, executionIdentity = { kind: 'internal' }) {
|
|
1868
1812
|
return createStoreOwnedRun({
|
|
@@ -1870,8 +1814,8 @@ export class RunStore {
|
|
|
1870
1814
|
hasRun: (id) => this.hasRun(id),
|
|
1871
1815
|
held: this.held, deleted: this.deleted, db: this.db,
|
|
1872
1816
|
buildRun: (ownedInput, id) => this.buildRun(ownedInput, id),
|
|
1873
|
-
writeWorkerIdentity: (id, identity) => this.writeWorkerIdentity(id, identity),
|
|
1874
|
-
writeWorkerExecution: (id, proof, fresh) => this.writeWorkerExecution(id, proof, fresh),
|
|
1817
|
+
writeWorkerIdentity: (id, identity) => this.workerExecutions.writeWorkerIdentity(id, identity),
|
|
1818
|
+
writeWorkerExecution: (id, proof, fresh) => this.workerExecutions.writeWorkerExecution(id, proof, fresh),
|
|
1875
1819
|
commitIndex: (owned) => this.commitIndex(owned),
|
|
1876
1820
|
}, input, parentId, requestId, worker, requestHash, spawnCapacityRefusal, executionIdentity);
|
|
1877
1821
|
}
|
|
@@ -1932,7 +1876,7 @@ export class RunStore {
|
|
|
1932
1876
|
if (!facts || !['done', 'review', 'failed', 'cancelled'].includes(run.status) || run.waits?.some(wait => wait.phase === 'open') ||
|
|
1933
1877
|
run.hasPendingPermission || facts.pendingAsk !== undefined)
|
|
1934
1878
|
return 'Task or worker work still needs attention. Open the task or archive manually.';
|
|
1935
|
-
if (run.delegation?.role === 'invalid' || (run.delegation?.role === 'worker' && this.readWorkerExecution(run.id)?.phase !== 'complete'))
|
|
1879
|
+
if (run.delegation?.role === 'invalid' || (run.delegation?.role === 'worker' && this.workerExecutions.readWorkerExecution(run.id)?.phase !== 'complete'))
|
|
1936
1880
|
return 'Worker execution has not settled. Open the worker or archive manually.';
|
|
1937
1881
|
}
|
|
1938
1882
|
return undefined;
|
|
@@ -2136,7 +2080,7 @@ export class RunStore {
|
|
|
2136
2080
|
decodeCold: (row) => this.decodeCold(row),
|
|
2137
2081
|
writeRefusal: (memberId) => this.writeRefusal(memberId),
|
|
2138
2082
|
factsPeek: (memberId) => this.transcriptFacts.factsFor(memberId).peek(memberId),
|
|
2139
|
-
readWorkerExecution: (memberId) => this.readWorkerExecution(memberId),
|
|
2083
|
+
readWorkerExecution: (memberId) => this.workerExecutions.readWorkerExecution(memberId),
|
|
2140
2084
|
}, id, isActive);
|
|
2141
2085
|
}
|
|
2142
2086
|
/** Blockers are feedback, never archive authority or cleanup permission. */
|
|
@@ -2601,243 +2545,6 @@ export class RunStore {
|
|
|
2601
2545
|
return [];
|
|
2602
2546
|
}
|
|
2603
2547
|
}
|
|
2604
|
-
/** Private process-generation evidence; never part of run JSON or SSE. */
|
|
2605
|
-
identityPath(id, write = false) {
|
|
2606
|
-
return this.executionPath(id, write).replace(/\.execution\.json$/, '.identity.json');
|
|
2607
|
-
}
|
|
2608
|
-
readWorkerIdentity(id) {
|
|
2609
|
-
try {
|
|
2610
|
-
const fd = openSync(this.identityPath(id), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
2611
|
-
try {
|
|
2612
|
-
const info = fstatSync(fd);
|
|
2613
|
-
if (!info.isFile() || info.size > 16384 || (info.mode & 0o077))
|
|
2614
|
-
return undefined;
|
|
2615
|
-
const buffer = Buffer.alloc(16385);
|
|
2616
|
-
const count = readSync(fd, buffer, 0, buffer.length, 0);
|
|
2617
|
-
return workerExecutionIdentitySchema.parse(JSON.parse(buffer.subarray(0, count).toString('utf8')));
|
|
2618
|
-
}
|
|
2619
|
-
finally {
|
|
2620
|
-
closeSync(fd);
|
|
2621
|
-
}
|
|
2622
|
-
}
|
|
2623
|
-
catch {
|
|
2624
|
-
return undefined;
|
|
2625
|
-
}
|
|
2626
|
-
}
|
|
2627
|
-
writeWorkerIdentity(id, identity) {
|
|
2628
|
-
// Immutable acceptance evidence. An orphan from failed index publication cannot be adopted.
|
|
2629
|
-
const fd = openSync(this.identityPath(id, true), constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
|
2630
|
-
try {
|
|
2631
|
-
writeFileSync(fd, JSON.stringify(identity));
|
|
2632
|
-
fsyncSync(fd);
|
|
2633
|
-
}
|
|
2634
|
-
finally {
|
|
2635
|
-
closeSync(fd);
|
|
2636
|
-
}
|
|
2637
|
-
}
|
|
2638
|
-
executionPath(id, write = false) {
|
|
2639
|
-
z.string().uuid().parse(id);
|
|
2640
|
-
const dir = join(write ? this.writeDir(id) : this.sourceDataDir(id), 'runs');
|
|
2641
|
-
if (write)
|
|
2642
|
-
mkdirSync(dir, { recursive: true });
|
|
2643
|
-
if (lstatSync(dir).isSymbolicLink() || realpathSync(dir) !== resolve(dir))
|
|
2644
|
-
throw new Error('Unsafe execution directory');
|
|
2645
|
-
return join(dir, `${id}.execution.json`);
|
|
2646
|
-
}
|
|
2647
|
-
readWorkerExecution(id) {
|
|
2648
|
-
try {
|
|
2649
|
-
const proof = readPrivateRecord(this.executionPath(id), 16384, value => workerExecutionSchema.parse(value));
|
|
2650
|
-
return typeof proof === 'string' ? undefined : proof;
|
|
2651
|
-
}
|
|
2652
|
-
catch {
|
|
2653
|
-
return undefined;
|
|
2654
|
-
}
|
|
2655
|
-
}
|
|
2656
|
-
writeWorkerExecution(id, proof, fresh = false) {
|
|
2657
|
-
writePrivateRecord(this.executionPath(id, true), proof, { fresh });
|
|
2658
|
-
}
|
|
2659
|
-
/** Complete execution is not permission to reuse/delete persistent resources (#738).
|
|
2660
|
-
* Synchronous, fresh and generation/resource fenced; callers hold admission off across awaits. */
|
|
2661
|
-
workerResourcesSafe(id, generation, resourceId) {
|
|
2662
|
-
// Reclaim and history deletion also need an empty scope; admission does not (ADR 0032).
|
|
2663
|
-
return this.workerResourceHolders(id, generation, resourceId) === 'safe' && this.scopes.empty(id);
|
|
2664
|
-
}
|
|
2665
|
-
/** `workerResourcesSafe` with the refusal's live PIDs (empty when no PID explains it). */
|
|
2666
|
-
workerResourceHolders(id, generation, resourceId, opts = {}) {
|
|
2667
|
-
if (deletionRefusal(id, 'worktree', this.dataDir))
|
|
2668
|
-
return [];
|
|
2669
|
-
const run = this.peek(id);
|
|
2670
|
-
const proof = this.readWorkerExecution(id);
|
|
2671
|
-
if (!workerResourceAuthorized(run, proof, id, generation, resourceId, opts.admittingQueued))
|
|
2672
|
-
return [];
|
|
2673
|
-
const record = this.readWorkerProcesses(id, generation);
|
|
2674
|
-
if (record === 'unknown' || !agentTmpDirOwnershipProven(this.dataDir, id))
|
|
2675
|
-
return [];
|
|
2676
|
-
// Only the generation's recorded processes count; an outside process blocks nothing (ADR 0032).
|
|
2677
|
-
return recordedHolders(record);
|
|
2678
|
-
}
|
|
2679
|
-
/** Upgrade a legacy completed checkpoint while terminal task ownership is still known.
|
|
2680
|
-
* No holder scan here: settlement and scheduling must never wait on cleanup proof. */
|
|
2681
|
-
retainWorkerScratchCleanup(id) {
|
|
2682
|
-
const run = this.peek(id), proof = this.readWorkerExecution(id);
|
|
2683
|
-
if (run?.delegation?.role !== 'worker' || ['queued', 'running', 'waiting'].includes(run.status) || proof?.phase !== 'complete')
|
|
2684
|
-
return;
|
|
2685
|
-
const { resourceId, path } = run.delegation.workspace;
|
|
2686
|
-
if (proof.scratchCleanup?.resourceId === resourceId && proof.scratchCleanup.path === path)
|
|
2687
|
-
return;
|
|
2688
|
-
this.writeWorkerExecution(id, { ...proof, scratchCleanup: { resourceId, path } });
|
|
2689
|
-
}
|
|
2690
|
-
/** Cleanup can outlive its index row, but never its generation or terminal task intent. */
|
|
2691
|
-
workerScratchResourcesSafe(id, generation, resourceId) {
|
|
2692
|
-
if (deletionRefusal(id, 'worktree', this.dataDir))
|
|
2693
|
-
return false;
|
|
2694
|
-
const run = this.peek(id), proof = this.readWorkerExecution(id);
|
|
2695
|
-
if (proof?.phase !== 'complete' || proof.generation !== generation || proof.scratchCleanup?.resourceId !== resourceId ||
|
|
2696
|
-
(run && ['queued', 'running', 'waiting'].includes(run.status)))
|
|
2697
|
-
return false;
|
|
2698
|
-
// A process still in the worker's scope may use its scratch; one outside it is not the worker's.
|
|
2699
|
-
if (run?.delegation?.role === 'worker')
|
|
2700
|
-
return run.delegation.workspace.path === proof.scratchCleanup.path &&
|
|
2701
|
-
this.workerResourceHolders(id, generation, resourceId) === 'safe' && this.scopes.empty(id);
|
|
2702
|
-
// A valid different role contradicts the retained intent. Quarantined/missing metadata
|
|
2703
|
-
// supplies no new authority; only the private terminal checkpoint authorizes scratch.
|
|
2704
|
-
if (run && run.delegation?.role !== 'invalid')
|
|
2705
|
-
return false;
|
|
2706
|
-
const record = this.readWorkerProcesses(id, generation);
|
|
2707
|
-
if (record === 'unknown' || !agentTmpDirOwnershipProven(this.dataDir, id))
|
|
2708
|
-
return false;
|
|
2709
|
-
return recordedHolders(record) === 'safe' && this.scopes.empty(id);
|
|
2710
|
-
}
|
|
2711
|
-
commitWorkerExecutionStart(id) {
|
|
2712
|
-
const run = this.peek(id);
|
|
2713
|
-
if (run?.delegation?.role !== 'worker' || run.delegation.destroy)
|
|
2714
|
-
throw new WorkerAdmissionRefusal('the worker is being destroyed', SPAWN_NEXT);
|
|
2715
|
-
const prior = admissibleExecution(this.readWorkerExecution(id));
|
|
2716
|
-
const holders = this.workerResourceHolders(id, prior.generation, run.delegation.workspace.resourceId, { admittingQueued: true });
|
|
2717
|
-
if (holders !== 'safe')
|
|
2718
|
-
throw holderRefusal(holders, this.readWorkerProcesses(id, prior.generation));
|
|
2719
|
-
const generation = randomUUID();
|
|
2720
|
-
// #469: the controller is durable before `starting`, so a crash leaves a provable generation.
|
|
2721
|
-
this.writeWorkerProcesses(id, { generation, controller: { pid: process.pid, ...startToken(process.pid) }, processes: [] });
|
|
2722
|
-
this.writeWorkerExecution(id, { generation, phase: 'starting' });
|
|
2723
|
-
return generation;
|
|
2724
|
-
}
|
|
2725
|
-
/** Private process record (#469): which processes a generation spawned, never part of run JSON or SSE. */
|
|
2726
|
-
processesPath(id, write = false) {
|
|
2727
|
-
return this.executionPath(id, write).replace(/\.execution\.json$/, '.processes.json');
|
|
2728
|
-
}
|
|
2729
|
-
/** Tri-state: only ENOENT is `absent` (legacy, scan-only). A present record that is unreadable,
|
|
2730
|
-
* wrongly permissioned, malformed or of another generation is `unknown`: never finalized or reaped. */
|
|
2731
|
-
readWorkerProcesses(id, generation) {
|
|
2732
|
-
const record = readPrivateRecord(() => this.processesPath(id), 8192, value => workerProcessRecordSchema.parse(value));
|
|
2733
|
-
return typeof record === 'string' || record.generation === generation ? record : 'unknown';
|
|
2734
|
-
}
|
|
2735
|
-
writeWorkerProcesses(id, record) {
|
|
2736
|
-
writePrivateRecord(this.processesPath(id, true), workerProcessRecordSchema.parse(record));
|
|
2737
|
-
}
|
|
2738
|
-
/** Private scope record: the cgroups and process groups the worker owns (ADR 0032). */
|
|
2739
|
-
scopePath(id, write = false) {
|
|
2740
|
-
return this.executionPath(id, write).replace(/\.execution\.json$/, '.scope.json');
|
|
2741
|
-
}
|
|
2742
|
-
/** Tri-state like the process record: only ENOENT is `absent`. */
|
|
2743
|
-
readWorkerScope(id) {
|
|
2744
|
-
const record = readPrivateRecord(() => this.scopePath(id), 65_536, value => scopeRecordSchema.parse(value));
|
|
2745
|
-
return typeof record === 'string' || record.workerId === id ? record : 'unknown';
|
|
2746
|
-
}
|
|
2747
|
-
/** Durable before the caller acts on it. */
|
|
2748
|
-
writeWorkerScope(id, record) {
|
|
2749
|
-
writePrivateRecord(this.scopePath(id, true), scopeRecordSchema.parse(record));
|
|
2750
|
-
}
|
|
2751
|
-
/** The scope a run's processes start in: a worker's own, none for any other run (ADR 0032). */
|
|
2752
|
-
spawnScope(id) {
|
|
2753
|
-
return this.peek(id)?.delegation?.role === 'worker' ? this.scopes.spawnScope(id) : undefined;
|
|
2754
|
-
}
|
|
2755
|
-
/** Bound to the current generation. Past the cap the record never drops an entry: the
|
|
2756
|
-
* working-directory scan is the remaining evidence. */
|
|
2757
|
-
appendWorkerProcess(id, generation, pid, pgid) {
|
|
2758
|
-
const record = this.readWorkerProcesses(id, generation);
|
|
2759
|
-
if (typeof record === 'string' || this.readWorkerExecution(id)?.generation !== generation)
|
|
2760
|
-
return false;
|
|
2761
|
-
const entry = { pid, ...startToken(pid), ...(pgid === undefined ? {} : { pgid }) };
|
|
2762
|
-
if (record.processes.some(known => known.pid === entry.pid && known.startToken === entry.startToken))
|
|
2763
|
-
return true;
|
|
2764
|
-
if (record.processes.length >= WORKER_PROCESS_CAP)
|
|
2765
|
-
return false;
|
|
2766
|
-
this.writeWorkerProcesses(id, { ...record, processes: [...record.processes, entry] });
|
|
2767
|
-
return true;
|
|
2768
|
-
}
|
|
2769
|
-
commitWorkerExecutionComplete(id, generation, opts = {}) {
|
|
2770
|
-
const proof = this.readWorkerExecution(id);
|
|
2771
|
-
if (!proof || proof.generation !== generation)
|
|
2772
|
-
return false;
|
|
2773
|
-
if (proof.phase === 'queued' && this.peek(id)?.status !== 'cancelled')
|
|
2774
|
-
return false;
|
|
2775
|
-
try {
|
|
2776
|
-
const run = this.peek(id);
|
|
2777
|
-
if (run?.delegation?.role !== 'worker')
|
|
2778
|
-
return false;
|
|
2779
|
-
this.commitIndex(new Map([[id, run]]));
|
|
2780
|
-
if (this.readWorkerExecution(id)?.generation !== generation)
|
|
2781
|
-
return false;
|
|
2782
|
-
this.writeWorkerExecution(id, { generation, phase: 'complete',
|
|
2783
|
-
// Nothing abandons an execution any more (hearsay-tools/cezarion#889); keep a checkpoint that already was.
|
|
2784
|
-
...(proof.abandoned ? { abandoned: true } : {}),
|
|
2785
|
-
...(proof.interrupted || (opts.interrupted && proof.phase === 'starting') ? { interrupted: true } : {}),
|
|
2786
|
-
...(proof.phase === 'queued' || proof.neverMaterialized ? { neverMaterialized: true } : {}),
|
|
2787
|
-
...(!['queued', 'running', 'waiting'].includes(run.status) ? { scratchCleanup: {
|
|
2788
|
-
resourceId: run.delegation.workspace.resourceId, path: run.delegation.workspace.path
|
|
2789
|
-
} } : {}) });
|
|
2790
|
-
// The earlier index event cannot attest exit: subscribers must observe the
|
|
2791
|
-
// durable private proof before a terminal worker can satisfy a lifecycle wait.
|
|
2792
|
-
const settled = this.peek(id);
|
|
2793
|
-
this.published.publish('run.updated', { run: settled, before: snapshotRun(settled) });
|
|
2794
|
-
return true;
|
|
2795
|
-
}
|
|
2796
|
-
catch {
|
|
2797
|
-
return false;
|
|
2798
|
-
}
|
|
2799
|
-
}
|
|
2800
|
-
/** Complete replacement evidence can stand in for absent child history, never for a present execution. */
|
|
2801
|
-
readDeletedWorkerResult(parentId, workerId) {
|
|
2802
|
-
if (this.hasRun(workerId))
|
|
2803
|
-
return undefined;
|
|
2804
|
-
const parent = this.peek(parentId);
|
|
2805
|
-
if (parent?.delegation?.role !== 'root' || parent.delegation.historyDeletion)
|
|
2806
|
-
return undefined;
|
|
2807
|
-
const deletion = parent.delegation.receipts.find(receipt => receipt.workerId === workerId)?.deletion;
|
|
2808
|
-
if (deletion?.phase !== 'complete')
|
|
2809
|
-
return undefined;
|
|
2810
|
-
const result = this.readWorkerResult(parentId, workerId);
|
|
2811
|
-
return result?.settled && ['review', 'done', 'failed', 'cancelled'].includes(result.status) &&
|
|
2812
|
-
result.cleanup === 'complete' && result.revision === deletion.revision &&
|
|
2813
|
-
result.workspace.resourceId === deletion.resourceId ? result : undefined;
|
|
2814
|
-
}
|
|
2815
|
-
/** A deleted child's receipt replaces the private proof only after the deletion checkpoint. */
|
|
2816
|
-
workerDeletionEvidence(id) {
|
|
2817
|
-
const child = this.peek(id);
|
|
2818
|
-
if (child?.delegation?.role !== 'worker' || child.delegation.workspace.ownerRunId !== id ||
|
|
2819
|
-
child.delegation.destroy?.phase !== 'complete' || child.delegation.destroy.remaining.length ||
|
|
2820
|
-
!['review', 'done', 'failed', 'cancelled'].includes(child.status))
|
|
2821
|
-
return false;
|
|
2822
|
-
return this.workerDeletionResult(id, child);
|
|
2823
|
-
}
|
|
2824
|
-
workerDeletionResult(id, child) {
|
|
2825
|
-
const parent = this.peek(child.delegation.parentRunId);
|
|
2826
|
-
if (parent?.delegation?.role !== 'root' || parent.delegation.historyDeletion)
|
|
2827
|
-
return false;
|
|
2828
|
-
const receipt = parent.delegation.receipts.find(entry => entry.workerId === id);
|
|
2829
|
-
const result = this.readWorkerResult(parent.id, id);
|
|
2830
|
-
if (!receipt || !result?.settled || result.cleanup !== 'complete' || result.status !== child.status ||
|
|
2831
|
-
result.revision !== (child.delegation.executionRevision ?? 0))
|
|
2832
|
-
return false;
|
|
2833
|
-
const { state: _state, ...workspace } = result.workspace;
|
|
2834
|
-
if (JSON.stringify(workspace) !== JSON.stringify(child.delegation.workspace))
|
|
2835
|
-
return false;
|
|
2836
|
-
return workerDeletionProof(receipt.deletion, result.revision, workspace.resourceId, {
|
|
2837
|
-
readProof: () => this.readWorkerExecution(id), resourcesSafe: generation => this.workerResourcesSafe(id, generation, workspace.resourceId),
|
|
2838
|
-
scratchMayExist: () => agentTmpDirMayExist(this.dataDir, id), executionExists: () => existsSync(this.executionPath(id)),
|
|
2839
|
-
});
|
|
2840
|
-
}
|
|
2841
2548
|
/** Write the pending rows out now (used on shutdown). */
|
|
2842
2549
|
flush() {
|
|
2843
2550
|
if (this.saveTimer) {
|