@culture.dev/agents 0.0.0-stage → 0.15.0
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/LICENSE +201 -0
- package/README.md +59 -2
- package/dist/contract-schemas/agent.json +128 -0
- package/dist/contract-schemas/allowlist.json +25 -0
- package/dist/contract-schemas/api.json +501 -0
- package/dist/contract-schemas/capabilities.json +208 -0
- package/dist/contract-schemas/common.json +123 -0
- package/dist/contract-schemas/control-action.json +279 -0
- package/dist/contract-schemas/error.json +269 -0
- package/dist/contract-schemas/event.json +316 -0
- package/dist/contract-schemas/run.json +336 -0
- package/dist/contract-schemas/session.json +77 -0
- package/dist/contract-schemas/version.json +22 -0
- package/dist/src/contract/api.d.ts +41 -0
- package/dist/src/contract/api.js +35 -0
- package/dist/src/contract/api.js.map +1 -0
- package/dist/src/contract/errors.d.ts +21 -0
- package/dist/src/contract/errors.js +25 -0
- package/dist/src/contract/errors.js.map +1 -0
- package/dist/src/contract/generated/agent.d.ts +31 -0
- package/dist/src/contract/generated/agent.js +3 -0
- package/dist/src/contract/generated/agent.js.map +1 -0
- package/dist/src/contract/generated/allowlist.d.ts +10 -0
- package/dist/src/contract/generated/allowlist.js +3 -0
- package/dist/src/contract/generated/allowlist.js.map +1 -0
- package/dist/src/contract/generated/capabilities.d.ts +46 -0
- package/dist/src/contract/generated/capabilities.js +3 -0
- package/dist/src/contract/generated/capabilities.js.map +1 -0
- package/dist/src/contract/generated/common.d.ts +40 -0
- package/dist/src/contract/generated/common.js +3 -0
- package/dist/src/contract/generated/common.js.map +1 -0
- package/dist/src/contract/generated/control-action.d.ts +73 -0
- package/dist/src/contract/generated/control-action.js +3 -0
- package/dist/src/contract/generated/control-action.js.map +1 -0
- package/dist/src/contract/generated/error.d.ts +25 -0
- package/dist/src/contract/generated/error.js +3 -0
- package/dist/src/contract/generated/error.js.map +1 -0
- package/dist/src/contract/generated/event.d.ts +80 -0
- package/dist/src/contract/generated/event.js +3 -0
- package/dist/src/contract/generated/event.js.map +1 -0
- package/dist/src/contract/generated/index.d.ts +81 -0
- package/dist/src/contract/generated/index.js +3 -0
- package/dist/src/contract/generated/index.js.map +1 -0
- package/dist/src/contract/generated/run.d.ts +63 -0
- package/dist/src/contract/generated/run.js +3 -0
- package/dist/src/contract/generated/run.js.map +1 -0
- package/dist/src/contract/generated/session.d.ts +12 -0
- package/dist/src/contract/generated/session.js +3 -0
- package/dist/src/contract/generated/session.js.map +1 -0
- package/dist/src/contract/generated/version.d.ts +9 -0
- package/dist/src/contract/generated/version.js +3 -0
- package/dist/src/contract/generated/version.js.map +1 -0
- package/dist/src/contract/ids.d.ts +36 -0
- package/dist/src/contract/ids.js +85 -0
- package/dist/src/contract/ids.js.map +1 -0
- package/dist/src/contract/index.d.ts +17 -0
- package/dist/src/contract/index.js +13 -0
- package/dist/src/contract/index.js.map +1 -0
- package/dist/src/contract/run-state.d.ts +13 -0
- package/dist/src/contract/run-state.js +24 -0
- package/dist/src/contract/run-state.js.map +1 -0
- package/dist/src/contract/schemas.d.ts +18 -0
- package/dist/src/contract/schemas.js +37 -0
- package/dist/src/contract/schemas.js.map +1 -0
- package/dist/src/contract/validate.d.ts +17 -0
- package/dist/src/contract/validate.js +68 -0
- package/dist/src/contract/validate.js.map +1 -0
- package/dist/src/daemon/allowlist.d.ts +23 -0
- package/dist/src/daemon/allowlist.js +57 -0
- package/dist/src/daemon/allowlist.js.map +1 -0
- package/dist/src/daemon/daemon.d.ts +31 -0
- package/dist/src/daemon/daemon.js +141 -0
- package/dist/src/daemon/daemon.js.map +1 -0
- package/dist/src/daemon/errors.d.ts +14 -0
- package/dist/src/daemon/errors.js +26 -0
- package/dist/src/daemon/errors.js.map +1 -0
- package/dist/src/daemon/harness-catalog.d.ts +11 -0
- package/dist/src/daemon/harness-catalog.js +21 -0
- package/dist/src/daemon/harness-catalog.js.map +1 -0
- package/dist/src/daemon/http.d.ts +15 -0
- package/dist/src/daemon/http.js +76 -0
- package/dist/src/daemon/http.js.map +1 -0
- package/dist/src/daemon/lock.d.ts +47 -0
- package/dist/src/daemon/lock.js +167 -0
- package/dist/src/daemon/lock.js.map +1 -0
- package/dist/src/daemon/main.d.ts +32 -0
- package/dist/src/daemon/main.js +204 -0
- package/dist/src/daemon/main.js.map +1 -0
- package/dist/src/daemon/paths.d.ts +29 -0
- package/dist/src/daemon/paths.js +26 -0
- package/dist/src/daemon/paths.js.map +1 -0
- package/dist/src/daemon/ports.d.ts +58 -0
- package/dist/src/daemon/ports.js +2 -0
- package/dist/src/daemon/ports.js.map +1 -0
- package/dist/src/daemon/routes.d.ts +15 -0
- package/dist/src/daemon/routes.js +259 -0
- package/dist/src/daemon/routes.js.map +1 -0
- package/dist/src/daemon/sse.d.ts +8 -0
- package/dist/src/daemon/sse.js +56 -0
- package/dist/src/daemon/sse.js.map +1 -0
- package/dist/src/harness/availability.d.ts +9 -0
- package/dist/src/harness/availability.js +52 -0
- package/dist/src/harness/availability.js.map +1 -0
- package/dist/src/harness/claude-code/agent.d.ts +4 -0
- package/dist/src/harness/claude-code/agent.js +33 -0
- package/dist/src/harness/claude-code/agent.js.map +1 -0
- package/dist/src/harness/claude-code/backend.d.ts +48 -0
- package/dist/src/harness/claude-code/backend.js +83 -0
- package/dist/src/harness/claude-code/backend.js.map +1 -0
- package/dist/src/harness/claude-code/capabilities.d.ts +7 -0
- package/dist/src/harness/claude-code/capabilities.js +11 -0
- package/dist/src/harness/claude-code/capabilities.js.map +1 -0
- package/dist/src/harness/claude-code/index.d.ts +15 -0
- package/dist/src/harness/claude-code/index.js +20 -0
- package/dist/src/harness/claude-code/index.js.map +1 -0
- package/dist/src/harness/claude-code/map.d.ts +30 -0
- package/dist/src/harness/claude-code/map.js +108 -0
- package/dist/src/harness/claude-code/map.js.map +1 -0
- package/dist/src/harness/common/capabilities.d.ts +34 -0
- package/dist/src/harness/common/capabilities.js +60 -0
- package/dist/src/harness/common/capabilities.js.map +1 -0
- package/dist/src/harness/common/guard.d.ts +18 -0
- package/dist/src/harness/common/guard.js +31 -0
- package/dist/src/harness/common/guard.js.map +1 -0
- package/dist/src/harness/common/processes.d.ts +21 -0
- package/dist/src/harness/common/processes.js +46 -0
- package/dist/src/harness/common/processes.js.map +1 -0
- package/dist/src/harness/common/queue.d.ts +20 -0
- package/dist/src/harness/common/queue.js +76 -0
- package/dist/src/harness/common/queue.js.map +1 -0
- package/dist/src/harness/common/run-driver.d.ts +88 -0
- package/dist/src/harness/common/run-driver.js +317 -0
- package/dist/src/harness/common/run-driver.js.map +1 -0
- package/dist/src/harness/common/sandbox-workspace.d.ts +72 -0
- package/dist/src/harness/common/sandbox-workspace.js +155 -0
- package/dist/src/harness/common/sandbox-workspace.js.map +1 -0
- package/dist/src/harness/common/text.d.ts +9 -0
- package/dist/src/harness/common/text.js +49 -0
- package/dist/src/harness/common/text.js.map +1 -0
- package/dist/src/harness/common/types.d.ts +111 -0
- package/dist/src/harness/common/types.js +2 -0
- package/dist/src/harness/common/types.js.map +1 -0
- package/dist/src/harness/config.d.ts +43 -0
- package/dist/src/harness/config.js +200 -0
- package/dist/src/harness/config.js.map +1 -0
- package/dist/src/harness/index.d.ts +21 -0
- package/dist/src/harness/index.js +89 -0
- package/dist/src/harness/index.js.map +1 -0
- package/dist/src/harness/opencode/backend.d.ts +37 -0
- package/dist/src/harness/opencode/backend.js +114 -0
- package/dist/src/harness/opencode/backend.js.map +1 -0
- package/dist/src/harness/opencode/capabilities.d.ts +8 -0
- package/dist/src/harness/opencode/capabilities.js +11 -0
- package/dist/src/harness/opencode/capabilities.js.map +1 -0
- package/dist/src/harness/opencode/client.d.ts +70 -0
- package/dist/src/harness/opencode/client.js +189 -0
- package/dist/src/harness/opencode/client.js.map +1 -0
- package/dist/src/harness/opencode/index.d.ts +13 -0
- package/dist/src/harness/opencode/index.js +19 -0
- package/dist/src/harness/opencode/index.js.map +1 -0
- package/dist/src/harness/opencode/lines.d.ts +14 -0
- package/dist/src/harness/opencode/lines.js +99 -0
- package/dist/src/harness/opencode/lines.js.map +1 -0
- package/dist/src/harness/opencode/map.d.ts +13 -0
- package/dist/src/harness/opencode/map.js +84 -0
- package/dist/src/harness/opencode/map.js.map +1 -0
- package/dist/src/harness/opencode/turn.d.ts +23 -0
- package/dist/src/harness/opencode/turn.js +143 -0
- package/dist/src/harness/opencode/turn.js.map +1 -0
- package/dist/src/harness/reclaim.d.ts +13 -0
- package/dist/src/harness/reclaim.js +18 -0
- package/dist/src/harness/reclaim.js.map +1 -0
- package/dist/src/harness/stub/backend.d.ts +11 -0
- package/dist/src/harness/stub/backend.js +125 -0
- package/dist/src/harness/stub/backend.js.map +1 -0
- package/dist/src/harness/stub/index.d.ts +18 -0
- package/dist/src/harness/stub/index.js +21 -0
- package/dist/src/harness/stub/index.js.map +1 -0
- package/dist/src/harness/stub/workspace.d.ts +29 -0
- package/dist/src/harness/stub/workspace.js +92 -0
- package/dist/src/harness/stub/workspace.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +3 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/runs/agents.d.ts +10 -0
- package/dist/src/runs/agents.js +49 -0
- package/dist/src/runs/agents.js.map +1 -0
- package/dist/src/runs/control.d.ts +31 -0
- package/dist/src/runs/control.js +105 -0
- package/dist/src/runs/control.js.map +1 -0
- package/dist/src/runs/index.d.ts +8 -0
- package/dist/src/runs/index.js +7 -0
- package/dist/src/runs/index.js.map +1 -0
- package/dist/src/runs/ports.d.ts +31 -0
- package/dist/src/runs/ports.js +5 -0
- package/dist/src/runs/ports.js.map +1 -0
- package/dist/src/runs/scan.d.ts +38 -0
- package/dist/src/runs/scan.js +149 -0
- package/dist/src/runs/scan.js.map +1 -0
- package/dist/src/runs/service.d.ts +70 -0
- package/dist/src/runs/service.js +239 -0
- package/dist/src/runs/service.js.map +1 -0
- package/dist/src/sandbox/harness-session.d.ts +11 -0
- package/dist/src/sandbox/harness-session.js +170 -0
- package/dist/src/sandbox/harness-session.js.map +1 -0
- package/dist/src/sandbox/host-command.d.ts +19 -0
- package/dist/src/sandbox/host-command.js +41 -0
- package/dist/src/sandbox/host-command.js.map +1 -0
- package/dist/src/sandbox/index.d.ts +11 -0
- package/dist/src/sandbox/index.js +15 -0
- package/dist/src/sandbox/index.js.map +1 -0
- package/dist/src/sandbox/naming.d.ts +49 -0
- package/dist/src/sandbox/naming.js +106 -0
- package/dist/src/sandbox/naming.js.map +1 -0
- package/dist/src/sandbox/openshell-client.d.ts +81 -0
- package/dist/src/sandbox/openshell-client.js +49 -0
- package/dist/src/sandbox/openshell-client.js.map +1 -0
- package/dist/src/sandbox/policy.d.ts +26 -0
- package/dist/src/sandbox/policy.js +100 -0
- package/dist/src/sandbox/policy.js.map +1 -0
- package/dist/src/sandbox/run-sandbox.d.ts +38 -0
- package/dist/src/sandbox/run-sandbox.js +247 -0
- package/dist/src/sandbox/run-sandbox.js.map +1 -0
- package/dist/src/sandbox/run-sandboxes.d.ts +18 -0
- package/dist/src/sandbox/run-sandboxes.js +225 -0
- package/dist/src/sandbox/run-sandboxes.js.map +1 -0
- package/dist/src/sandbox/timeout.d.ts +12 -0
- package/dist/src/sandbox/timeout.js +39 -0
- package/dist/src/sandbox/timeout.js.map +1 -0
- package/dist/src/sandbox/types.d.ts +125 -0
- package/dist/src/sandbox/types.js +15 -0
- package/dist/src/sandbox/types.js.map +1 -0
- package/dist/src/sandbox/worktree.d.ts +72 -0
- package/dist/src/sandbox/worktree.js +157 -0
- package/dist/src/sandbox/worktree.js.map +1 -0
- package/dist/src/store/derive.d.ts +39 -0
- package/dist/src/store/derive.js +68 -0
- package/dist/src/store/derive.js.map +1 -0
- package/dist/src/store/event-log.d.ts +79 -0
- package/dist/src/store/event-log.js +202 -0
- package/dist/src/store/event-log.js.map +1 -0
- package/dist/src/store/files.d.ts +51 -0
- package/dist/src/store/files.js +103 -0
- package/dist/src/store/files.js.map +1 -0
- package/dist/src/store/index.d.ts +15 -0
- package/dist/src/store/index.js +11 -0
- package/dist/src/store/index.js.map +1 -0
- package/dist/src/store/liveness.d.ts +61 -0
- package/dist/src/store/liveness.js +150 -0
- package/dist/src/store/liveness.js.map +1 -0
- package/dist/src/store/run-store.d.ts +67 -0
- package/dist/src/store/run-store.js +211 -0
- package/dist/src/store/run-store.js.map +1 -0
- package/dist/src/util/sequence.d.ts +58 -0
- package/dist/src/util/sequence.js +119 -0
- package/dist/src/util/sequence.js.map +1 -0
- package/dist/src/worker/control.d.ts +60 -0
- package/dist/src/worker/control.js +205 -0
- package/dist/src/worker/control.js.map +1 -0
- package/dist/src/worker/delivery.d.ts +17 -0
- package/dist/src/worker/delivery.js +43 -0
- package/dist/src/worker/delivery.js.map +1 -0
- package/dist/src/worker/driver.d.ts +88 -0
- package/dist/src/worker/driver.js +6 -0
- package/dist/src/worker/driver.js.map +1 -0
- package/dist/src/worker/fake-driver.d.ts +65 -0
- package/dist/src/worker/fake-driver.js +123 -0
- package/dist/src/worker/fake-driver.js.map +1 -0
- package/dist/src/worker/index.d.ts +15 -0
- package/dist/src/worker/index.js +11 -0
- package/dist/src/worker/index.js.map +1 -0
- package/dist/src/worker/main.d.ts +1 -0
- package/dist/src/worker/main.js +57 -0
- package/dist/src/worker/main.js.map +1 -0
- package/dist/src/worker/retained.d.ts +13 -0
- package/dist/src/worker/retained.js +40 -0
- package/dist/src/worker/retained.js.map +1 -0
- package/dist/src/worker/run-loop.d.ts +63 -0
- package/dist/src/worker/run-loop.js +325 -0
- package/dist/src/worker/run-loop.js.map +1 -0
- package/dist/src/worker/spawn.d.ts +68 -0
- package/dist/src/worker/spawn.js +201 -0
- package/dist/src/worker/spawn.js.map +1 -0
- package/image/Dockerfile +81 -0
- package/image/build.sh +62 -0
- package/image/pnpm-shim.sh +25 -0
- package/package.json +54 -4
- package/policy/profiles/culture-agents-claude-subscription.yaml +59 -0
- package/policy/profiles/culture-agents-local-gateway.yaml +49 -0
- package/policy/run-sandbox.policy.json +10 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker process entry point. The daemon spawns it detached (own session, stdio to the run's
|
|
3
|
+
* `worker.log`, no IPC channel), so it survives the daemon:
|
|
4
|
+
*
|
|
5
|
+
* node dist/src/worker/main.js --state-dir <dir> --run-id <id> [--heartbeat-ms N] [--cancel-grace-ms N]
|
|
6
|
+
*
|
|
7
|
+
* Exit 0 once the run is terminal; exit 1 when the run could not be opened (nothing is
|
|
8
|
+
* acknowledged then, and the run stays without a live worker).
|
|
9
|
+
*/
|
|
10
|
+
import { parseArgs } from "node:util";
|
|
11
|
+
import { isRunId } from "../contract/index.js";
|
|
12
|
+
import { runWorker } from "./run-loop.js";
|
|
13
|
+
function positiveInt(value, name) {
|
|
14
|
+
if (value === undefined)
|
|
15
|
+
return undefined;
|
|
16
|
+
const n = Number(value);
|
|
17
|
+
if (!Number.isInteger(n) || n <= 0)
|
|
18
|
+
throw new Error(`--${name} must be a positive integer`);
|
|
19
|
+
return n;
|
|
20
|
+
}
|
|
21
|
+
async function main() {
|
|
22
|
+
const { values } = parseArgs({
|
|
23
|
+
options: {
|
|
24
|
+
"state-dir": { type: "string" },
|
|
25
|
+
"run-id": { type: "string" },
|
|
26
|
+
"heartbeat-ms": { type: "string" },
|
|
27
|
+
"cancel-grace-ms": { type: "string" },
|
|
28
|
+
},
|
|
29
|
+
strict: true,
|
|
30
|
+
});
|
|
31
|
+
const stateDir = values["state-dir"];
|
|
32
|
+
const runId = values["run-id"];
|
|
33
|
+
if (!stateDir || !runId || !isRunId(runId))
|
|
34
|
+
throw new Error("usage: main.js --state-dir <dir> --run-id <run id>");
|
|
35
|
+
// The worker outlives the daemon and any terminal: a hangup is not a reason to stop.
|
|
36
|
+
process.on("SIGHUP", () => undefined);
|
|
37
|
+
const heartbeatMs = positiveInt(values["heartbeat-ms"], "heartbeat-ms");
|
|
38
|
+
const cancelGraceMs = positiveInt(values["cancel-grace-ms"], "cancel-grace-ms");
|
|
39
|
+
const state = await runWorker({
|
|
40
|
+
stateDir,
|
|
41
|
+
runId,
|
|
42
|
+
...(heartbeatMs === undefined ? {} : { heartbeatMs }),
|
|
43
|
+
...(cancelGraceMs === undefined ? {} : { cancelGraceMs }),
|
|
44
|
+
});
|
|
45
|
+
process.stderr.write(`worker ${process.pid}: run ${runId} ended ${state}\n`);
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
let exitCode;
|
|
49
|
+
try {
|
|
50
|
+
exitCode = await main();
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
process.stderr.write(`worker ${process.pid}: ${err instanceof Error ? (err.stack ?? err.message) : String(err)}\n`);
|
|
54
|
+
exitCode = 1;
|
|
55
|
+
}
|
|
56
|
+
process.exit(exitCode);
|
|
57
|
+
//# sourceMappingURL=main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["../../../src/worker/main.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE1C,SAAS,WAAW,CAAC,KAAyB,EAAE,IAAY;IAC1D,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC1C,MAAM,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IACxB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,KAAK,IAAI,6BAA6B,CAAC,CAAC;IAC5F,OAAO,CAAC,CAAC;AACX,CAAC;AAED,KAAK,UAAU,IAAI;IACjB,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;QAC3B,OAAO,EAAE;YACP,WAAW,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;YAC/B,QAAQ,EAAE,EAAE,IAAI,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AC;IACb,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,OAAO,CAAC,GAAG,KAAK,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACpH,QAAQ,GAAG,CAAC,CAAC;AACf,CAAC;AACD,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type Retained } from "./driver.js";
|
|
2
|
+
/** Record the retention durably. Called before the run's terminal event is written. */
|
|
3
|
+
export declare function writeRetained(dir: string, retained: Retained): void;
|
|
4
|
+
/**
|
|
5
|
+
* The run's retention, or null when it has none. `until` is in epoch milliseconds. A record that
|
|
6
|
+
* exists but cannot be read counts as a retention for the default period from when the file was
|
|
7
|
+
* written (`sandbox` is then null): work is not lost to a damaged record, and nothing is kept
|
|
8
|
+
* forever because of one.
|
|
9
|
+
*/
|
|
10
|
+
export declare function readRetained(dir: string): {
|
|
11
|
+
sandbox: string | null;
|
|
12
|
+
until: number;
|
|
13
|
+
} | null;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The retention record of a run (`retained.json` in the run directory). A run whose turn
|
|
3
|
+
* completed but whose work could not be brought to the host keeps its sandbox, the only copy of
|
|
4
|
+
* that work, for a limited time. The worker writes the record before the run's terminal event;
|
|
5
|
+
* the daemon's reclaim of finished runs reads it and leaves the sandbox alone until it expires.
|
|
6
|
+
*/
|
|
7
|
+
import { readFileSync, statSync } from "node:fs";
|
|
8
|
+
import { atomicWriteJson, runFile } from "../store/files.js";
|
|
9
|
+
import { RETAIN_FAILED_SANDBOX_DAYS } from "./driver.js";
|
|
10
|
+
const DAY_MS = 86_400_000;
|
|
11
|
+
/** Record the retention durably. Called before the run's terminal event is written. */
|
|
12
|
+
export function writeRetained(dir, retained) {
|
|
13
|
+
atomicWriteJson(runFile(dir, "retained"), { sandbox: retained.sandbox, until: retained.until }, true);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The run's retention, or null when it has none. `until` is in epoch milliseconds. A record that
|
|
17
|
+
* exists but cannot be read counts as a retention for the default period from when the file was
|
|
18
|
+
* written (`sandbox` is then null): work is not lost to a damaged record, and nothing is kept
|
|
19
|
+
* forever because of one.
|
|
20
|
+
*/
|
|
21
|
+
export function readRetained(dir) {
|
|
22
|
+
const file = runFile(dir, "retained");
|
|
23
|
+
try {
|
|
24
|
+
const value = JSON.parse(readFileSync(file, "utf8"));
|
|
25
|
+
const until = typeof value?.until === "string" ? Date.parse(value.until) : Number.NaN;
|
|
26
|
+
if (typeof value?.sandbox === "string" && Number.isFinite(until))
|
|
27
|
+
return { sandbox: value.sandbox, until };
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
if (err.code === "ENOENT")
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
return { sandbox: null, until: statSync(file).mtimeMs + RETAIN_FAILED_SANDBOX_DAYS * DAY_MS };
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=retained.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retained.js","sourceRoot":"","sources":["../../../src/worker/retained.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEjD,OAAO,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,0BAA0B,EAAiB,MAAM,aAAa,CAAC;AAExE,MAAM,MAAM,GAAG,UAAU,CAAC;AAE1B,uFAAuF;AACvF,MAAM,UAAU,aAAa,CAAC,GAAW,EAAE,QAAkB;IAC3D,e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heartbeat interval. Default 1000 ms. */
|
|
6
|
+
heartbeatMs?: number;
|
|
7
|
+
/** How long a stop may take before the worker forces the harness down. Default 30 000 ms. */
|
|
8
|
+
cancelGraceMs?: number;
|
|
9
|
+
clock?: () => number;
|
|
10
|
+
}
|
|
11
|
+
export declare class RunWorker {
|
|
12
|
+
private readonly options;
|
|
13
|
+
private readonly dir;
|
|
14
|
+
private readonly clock;
|
|
15
|
+
private record;
|
|
16
|
+
private writer;
|
|
17
|
+
private driver;
|
|
18
|
+
private server;
|
|
19
|
+
private stopOrigin;
|
|
20
|
+
private forcing;
|
|
21
|
+
private timers;
|
|
22
|
+
private resolveDone;
|
|
23
|
+
readonly done: Promise<RunState>;
|
|
24
|
+
constructor(options: WorkerOptions);
|
|
25
|
+
get state(): RunState | null;
|
|
26
|
+
/**
|
|
27
|
+
* Make the run durable and controllable: identity, event 1, cursor and control socket, in
|
|
28
|
+
* that order. When this resolves, the daemon may acknowledge the submit.
|
|
29
|
+
*/
|
|
30
|
+
open(): Promise<void>;
|
|
31
|
+
/** Drive the run to a terminal state; resolves with it. */
|
|
32
|
+
run(): Promise<RunState>;
|
|
33
|
+
/** Create the run's driver from the module its `driver` file names. */
|
|
34
|
+
private loadDriver;
|
|
35
|
+
/** Release the driver, timers and socket. The log and record stay. */
|
|
36
|
+
close(): Promise<void>;
|
|
37
|
+
private terminal;
|
|
38
|
+
private append;
|
|
39
|
+
private transition;
|
|
40
|
+
/** Keep the record's cached state fields in step with the log (the log stays authoritative). */
|
|
41
|
+
private apply;
|
|
42
|
+
private fail;
|
|
43
|
+
private onDriverEvent;
|
|
44
|
+
/** Where the run's commits landed: the branch on the record, the SHAs beside it, both before the terminal event. */
|
|
45
|
+
private recordCollected;
|
|
46
|
+
private finishStop;
|
|
47
|
+
/** queued/running -> cancelling, ask the driver to stop, and force it down after the grace period. */
|
|
48
|
+
private requestStop;
|
|
49
|
+
private armDeadline;
|
|
50
|
+
private checkAction;
|
|
51
|
+
/** Record a refusal the core decided. A run that ended meanwhile takes no record, and the reply says so. */
|
|
52
|
+
private refuse;
|
|
53
|
+
private control;
|
|
54
|
+
/**
|
|
55
|
+
* Deliver a steer, then record it. `run.control` means accepted, so it is written only after the
|
|
56
|
+
* driver took the steer. A delivery failure is answered `unavailable` and logged as a non-fatal
|
|
57
|
+
* `run.error` (the contract's `run.refused` carries only `unsupported` or `conflict`). A run that
|
|
58
|
+
* ended during delivery gets no record (nothing follows a terminal event) and a `conflict`.
|
|
59
|
+
*/
|
|
60
|
+
private steer;
|
|
61
|
+
}
|
|
62
|
+
/** Open, run and close one worker; resolves with the terminal state. */
|
|
63
|
+
export declare function runWorker(options: WorkerOptions): Promise<RunState>;
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The worker's run loop: one process per run. It records its identity, writes event 1
|
|
3
|
+
* (`null` -> `queued`) durably, opens its control socket, then drives the harness through a
|
|
4
|
+
* `Driver`, appending every event to the run's log, enforcing the deadline and writing the
|
|
5
|
+
* terminal event. It is the log's only writer while it lives, and it does not depend on the
|
|
6
|
+
* daemon: the daemon disappearing changes nothing here.
|
|
7
|
+
*/
|
|
8
|
+
import { readFileSync } from "node:fs";
|
|
9
|
+
import { pathToFileURL } from "node:url";
|
|
10
|
+
import { ContractValidationError, isTerminal, } from "../contract/index.js";
|
|
11
|
+
import { EventLogWriter } from "../store/event-log.js";
|
|
12
|
+
import { atomicWriteJson, runDir, runFile } from "../store/files.js";
|
|
13
|
+
import { identityOf, writeHeartbeat, writeWorkerIdentity } from "../store/liveness.js";
|
|
14
|
+
import { readRecord, writeRecord } from "../store/run-store.js";
|
|
15
|
+
import { closeControl, listenControl } from "./control.js";
|
|
16
|
+
import { writeRetained } from "./retained.js";
|
|
17
|
+
const MAX_TIMER = 2 ** 31 - 1;
|
|
18
|
+
const clip = (text) => (text.length > 2000 ? `${text.slice(0, 1997)}...` : text);
|
|
19
|
+
const messageOf = (err) => (err instanceof Error ? err.message : String(err));
|
|
20
|
+
const internal = (message) => ({ code: "internal", message, retryable: false });
|
|
21
|
+
const conflict = (runId, message, retryable) => ({
|
|
22
|
+
ok: false,
|
|
23
|
+
error: { code: "conflict", message, retryable, run_id: runId },
|
|
24
|
+
});
|
|
25
|
+
const invalid = (message) => ({ ok: false, error: { code: "invalid", message, retryable: false } });
|
|
26
|
+
export class RunWorker {
|
|
27
|
+
options;
|
|
28
|
+
dir;
|
|
29
|
+
clock;
|
|
30
|
+
record;
|
|
31
|
+
writer;
|
|
32
|
+
driver = null;
|
|
33
|
+
server = null;
|
|
34
|
+
stopOrigin = null;
|
|
35
|
+
forcing = null;
|
|
36
|
+
timers = [];
|
|
37
|
+
resolveDone;
|
|
38
|
+
done = new Promise((resolve) => (this.resolveDone = resolve));
|
|
39
|
+
constructor(options) {
|
|
40
|
+
this.options = options;
|
|
41
|
+
this.dir = runDir(options.stateDir, options.runId);
|
|
42
|
+
this.clock = options.clock ?? Date.now;
|
|
43
|
+
}
|
|
44
|
+
get state() {
|
|
45
|
+
return this.writer.state;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Make the run durable and controllable: identity, event 1, cursor and control socket, in
|
|
49
|
+
* that order. When this resolves, the daemon may acknowledge the submit.
|
|
50
|
+
*/
|
|
51
|
+
async open() {
|
|
52
|
+
this.record = readRecord(this.dir);
|
|
53
|
+
if (this.record.id !== this.options.runId)
|
|
54
|
+
throw new Error("run record id does not match the run directory");
|
|
55
|
+
this.writer = EventLogWriter.open(this.dir, this.options.runId, this.clock);
|
|
56
|
+
if (this.writer.cursor !== 0) {
|
|
57
|
+
this.writer.close();
|
|
58
|
+
throw new Error("the run already has events: a run's worker starts once and is never restarted");
|
|
59
|
+
}
|
|
60
|
+
writeWorkerIdentity(this.dir, identityOf(process.pid, this.clock));
|
|
61
|
+
writeHeartbeat(this.dir, this.clock);
|
|
62
|
+
this.record = { ...this.record, worker_pid: process.pid };
|
|
63
|
+
this.apply(this.writer.transition("queued", null, true), true);
|
|
64
|
+
this.server = await listenControl(this.dir, (req) => this.control(req));
|
|
65
|
+
const beat = setInterval(() => writeHeartbeat(this.dir, this.clock), this.options.heartbeatMs ?? 1000);
|
|
66
|
+
this.timers.push(beat);
|
|
67
|
+
}
|
|
68
|
+
/** Drive the run to a terminal state; resolves with it. */
|
|
69
|
+
async run() {
|
|
70
|
+
this.armDeadline();
|
|
71
|
+
try {
|
|
72
|
+
this.driver = await this.loadDriver();
|
|
73
|
+
// A stop that arrived before the harness started: it never ran, so it has stopped.
|
|
74
|
+
if (this.writer.state === "cancelling")
|
|
75
|
+
this.finishStop(null);
|
|
76
|
+
if (!this.terminal()) {
|
|
77
|
+
for await (const event of this.driver.start()) {
|
|
78
|
+
this.onDriverEvent(event);
|
|
79
|
+
if (this.terminal())
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (this.forcing)
|
|
84
|
+
await this.forcing;
|
|
85
|
+
if (!this.terminal())
|
|
86
|
+
this.fail(internal("The harness driver ended without reporting an outcome."));
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
if (this.forcing)
|
|
90
|
+
await this.forcing;
|
|
91
|
+
if (!this.terminal())
|
|
92
|
+
this.fail(internal(`The harness driver failed: ${messageOf(err)}`));
|
|
93
|
+
}
|
|
94
|
+
return this.done;
|
|
95
|
+
}
|
|
96
|
+
/** Create the run's driver from the module its `driver` file names. */
|
|
97
|
+
async loadDriver() {
|
|
98
|
+
const selection = JSON.parse(readFileSync(runFile(this.dir, "driver"), "utf8"));
|
|
99
|
+
const url = selection.module.startsWith("file:") ? selection.module : pathToFileURL(selection.module).href;
|
|
100
|
+
const mod = (await import(url));
|
|
101
|
+
if (typeof mod.createDriver !== "function")
|
|
102
|
+
throw new Error("the driver module does not export createDriver");
|
|
103
|
+
return await mod.createDriver({ run: this.record, runDir: this.dir, options: selection.options });
|
|
104
|
+
}
|
|
105
|
+
/** Release the driver, timers and socket. The log and record stay. */
|
|
106
|
+
async close() {
|
|
107
|
+
for (const t of this.timers)
|
|
108
|
+
clearTimeout(t);
|
|
109
|
+
this.timers = [];
|
|
110
|
+
await this.driver?.dispose().catch(() => undefined);
|
|
111
|
+
if (this.server)
|
|
112
|
+
await closeControl(this.dir, this.server);
|
|
113
|
+
this.server = null;
|
|
114
|
+
this.writer.close();
|
|
115
|
+
}
|
|
116
|
+
terminal() {
|
|
117
|
+
return this.writer.state !== null && isTerminal(this.writer.state);
|
|
118
|
+
}
|
|
119
|
+
append(input) {
|
|
120
|
+
return this.terminal() ? null : this.writer.append(input);
|
|
121
|
+
}
|
|
122
|
+
transition(to, reason) {
|
|
123
|
+
if (this.terminal())
|
|
124
|
+
return;
|
|
125
|
+
const event = this.writer.transition(to, reason === null ? null : clip(reason));
|
|
126
|
+
this.apply(event, isTerminal(to));
|
|
127
|
+
if (isTerminal(to)) {
|
|
128
|
+
for (const t of this.timers)
|
|
129
|
+
clearTimeout(t);
|
|
130
|
+
this.timers = [];
|
|
131
|
+
this.resolveDone(to);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** Keep the record's cached state fields in step with the log (the log stays authoritative). */
|
|
135
|
+
apply(event, durable) {
|
|
136
|
+
const data = event.data;
|
|
137
|
+
const terminal = isTerminal(data.to);
|
|
138
|
+
this.record = {
|
|
139
|
+
...this.record,
|
|
140
|
+
state: data.to,
|
|
141
|
+
state_reason: data.reason,
|
|
142
|
+
terminal,
|
|
143
|
+
updated_at: event.time,
|
|
144
|
+
started_at: this.record.started_at ?? (data.to === "running" ? event.time : null),
|
|
145
|
+
ended_at: terminal ? event.time : null,
|
|
146
|
+
cursor: event.cursor,
|
|
147
|
+
live_control: false,
|
|
148
|
+
};
|
|
149
|
+
writeRecord(this.dir, this.record, durable);
|
|
150
|
+
}
|
|
151
|
+
fail(error) {
|
|
152
|
+
// A driver's message can be any length (a stack, captured stderr); the contract caps it, and an
|
|
153
|
+
// unclipped one would fail validation here and leave the run without its terminal event.
|
|
154
|
+
error = { ...error, message: clip(error.message) };
|
|
155
|
+
this.append({ kind: "run.error", data: { error, fatal: true } });
|
|
156
|
+
this.transition("failed", error.message);
|
|
157
|
+
}
|
|
158
|
+
onDriverEvent(event) {
|
|
159
|
+
const state = this.writer.state;
|
|
160
|
+
switch (event.type) {
|
|
161
|
+
case "started":
|
|
162
|
+
if (state === "queued")
|
|
163
|
+
this.transition("running", null);
|
|
164
|
+
return;
|
|
165
|
+
case "output":
|
|
166
|
+
this.append({ kind: "run.output", data: event.data });
|
|
167
|
+
return;
|
|
168
|
+
case "error":
|
|
169
|
+
this.append({ kind: "run.error", data: { error: { ...event.error, message: clip(event.error.message) }, fatal: false } });
|
|
170
|
+
return;
|
|
171
|
+
case "completed":
|
|
172
|
+
if (state === "queued")
|
|
173
|
+
this.transition("running", "the harness completed before reporting a start");
|
|
174
|
+
if (event.collected)
|
|
175
|
+
this.recordCollected(event.collected);
|
|
176
|
+
this.transition("succeeded", null);
|
|
177
|
+
return;
|
|
178
|
+
case "failed":
|
|
179
|
+
// Before the terminal event: once the run reads `failed`, the reclaim already finds the retention.
|
|
180
|
+
if (event.retained)
|
|
181
|
+
writeRetained(this.dir, event.retained);
|
|
182
|
+
this.fail(event.error);
|
|
183
|
+
return;
|
|
184
|
+
case "stopped":
|
|
185
|
+
if (state === "cancelling")
|
|
186
|
+
this.finishStop(null);
|
|
187
|
+
else
|
|
188
|
+
this.fail(internal("The harness stopped without a stop request."));
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/** Where the run's commits landed: the branch on the record, the SHAs beside it, both before the terminal event. */
|
|
193
|
+
recordCollected(collected) {
|
|
194
|
+
atomicWriteJson(runFile(this.dir, "collected"), { branch: collected.branch, commits: collected.commits }, true);
|
|
195
|
+
this.record = { ...this.record, branch: collected.branch };
|
|
196
|
+
writeRecord(this.dir, this.record, true); // before the terminal event: a reader that sees `succeeded` sees the branch
|
|
197
|
+
}
|
|
198
|
+
finishStop(note) {
|
|
199
|
+
if (this.stopOrigin === "deadline")
|
|
200
|
+
this.transition("timed_out", note ?? "deadline reached");
|
|
201
|
+
else
|
|
202
|
+
this.transition("cancelled", note);
|
|
203
|
+
}
|
|
204
|
+
/** queued/running -> cancelling, ask the driver to stop, and force it down after the grace period. */
|
|
205
|
+
requestStop(origin, reason) {
|
|
206
|
+
const state = this.writer.state;
|
|
207
|
+
if (state !== "queued" && state !== "running")
|
|
208
|
+
return;
|
|
209
|
+
this.stopOrigin = origin;
|
|
210
|
+
this.transition("cancelling", reason);
|
|
211
|
+
const grace = this.options.cancelGraceMs ?? 30_000;
|
|
212
|
+
this.timers.push(setTimeout(() => {
|
|
213
|
+
if (this.writer.state !== "cancelling")
|
|
214
|
+
return;
|
|
215
|
+
const note = `the harness did not confirm the stop within ${grace} ms; the worker forced it down`;
|
|
216
|
+
this.forcing = (this.driver?.dispose() ?? Promise.resolve())
|
|
217
|
+
.catch(() => undefined)
|
|
218
|
+
.then(() => this.finishStop(note));
|
|
219
|
+
}, grace));
|
|
220
|
+
void this.driver?.cancel(origin === "deadline" ? "deadline" : reason).catch((err) => {
|
|
221
|
+
this.append({ kind: "run.error", data: { error: internal(clip(`cancel failed: ${String(err)}`)), fatal: false } });
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
armDeadline() {
|
|
225
|
+
if (this.record.deadline === null)
|
|
226
|
+
return;
|
|
227
|
+
const at = Date.parse(this.record.deadline);
|
|
228
|
+
const arm = () => {
|
|
229
|
+
const left = at - this.clock();
|
|
230
|
+
if (left <= 0) {
|
|
231
|
+
this.requestStop("deadline", "deadline");
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
this.timers.push(setTimeout(arm, Math.min(left, MAX_TIMER)));
|
|
235
|
+
};
|
|
236
|
+
arm();
|
|
237
|
+
}
|
|
238
|
+
checkAction(action, kind) {
|
|
239
|
+
if (action.run_id !== this.record.id)
|
|
240
|
+
return "The action is for a different run.";
|
|
241
|
+
if (action.kind !== kind)
|
|
242
|
+
return `The action kind must be ${kind}.`;
|
|
243
|
+
if (action.outcome !== "accepted" || action.error !== null)
|
|
244
|
+
return "The action must be an accepted record.";
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
/** Record a refusal the core decided. A run that ended meanwhile takes no record, and the reply says so. */
|
|
248
|
+
refuse(action, ok) {
|
|
249
|
+
const id = this.record.id;
|
|
250
|
+
if (action.run_id !== id || action.outcome !== "refused")
|
|
251
|
+
return invalid("The refusal record does not match this run.");
|
|
252
|
+
try {
|
|
253
|
+
if (this.append({ kind: "run.refused", data: { action } }) === null)
|
|
254
|
+
return conflict(id, "The run ended before the refusal was recorded.", false);
|
|
255
|
+
}
|
|
256
|
+
catch (err) {
|
|
257
|
+
if (err instanceof ContractValidationError)
|
|
258
|
+
return invalid(err.message);
|
|
259
|
+
throw err;
|
|
260
|
+
}
|
|
261
|
+
return ok();
|
|
262
|
+
}
|
|
263
|
+
async control(req) {
|
|
264
|
+
const ok = () => ({ ok: true, pid: process.pid, state: this.writer.state, cursor: this.writer.cursor });
|
|
265
|
+
if (req.op === "ping")
|
|
266
|
+
return ok();
|
|
267
|
+
const id = this.record.id;
|
|
268
|
+
const state = this.writer.state;
|
|
269
|
+
if (state === null || isTerminal(state))
|
|
270
|
+
return conflict(id, `The run is already ${String(state)}.`, false);
|
|
271
|
+
if (req.op === "refuse")
|
|
272
|
+
return this.refuse(req.action, ok);
|
|
273
|
+
const bad = this.checkAction(req.action, req.op);
|
|
274
|
+
if (bad !== null)
|
|
275
|
+
return invalid(bad);
|
|
276
|
+
if (req.op === "steer") {
|
|
277
|
+
if (state !== "running")
|
|
278
|
+
return conflict(id, `A steer needs a running run; this run is ${state}.`, state === "queued");
|
|
279
|
+
return this.steer(req.action, req.message, ok);
|
|
280
|
+
}
|
|
281
|
+
// A cancel is accepted when the worker takes the stop decision, which it enforces itself
|
|
282
|
+
// (cancelling now, the driver asked to stop, forced down after the grace period), so it is
|
|
283
|
+
// recorded before the stop starts and holds even when the driver's cancel call fails.
|
|
284
|
+
this.append({ kind: "run.control", data: { action: req.action } });
|
|
285
|
+
const by = `cancel requested by ${req.action.caller.id}`;
|
|
286
|
+
this.requestStop("cancel", req.reason ? `${by}: ${req.reason}` : by);
|
|
287
|
+
return ok();
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Deliver a steer, then record it. `run.control` means accepted, so it is written only after the
|
|
291
|
+
* driver took the steer. A delivery failure is answered `unavailable` and logged as a non-fatal
|
|
292
|
+
* `run.error` (the contract's `run.refused` carries only `unsupported` or `conflict`). A run that
|
|
293
|
+
* ended during delivery gets no record (nothing follows a terminal event) and a `conflict`.
|
|
294
|
+
*/
|
|
295
|
+
async steer(action, message, ok) {
|
|
296
|
+
const id = this.record.id;
|
|
297
|
+
try {
|
|
298
|
+
if (this.driver === null)
|
|
299
|
+
throw new Error("the harness driver is not loaded");
|
|
300
|
+
await this.driver.steer(message);
|
|
301
|
+
}
|
|
302
|
+
catch (err) {
|
|
303
|
+
const why = `The steer ${action.id} could not be delivered to the harness: ${messageOf(err)}`;
|
|
304
|
+
const error = { code: "unavailable", message: clip(why), retryable: true };
|
|
305
|
+
this.append({ kind: "run.error", data: { error, fatal: false } });
|
|
306
|
+
return { ok: false, error };
|
|
307
|
+
}
|
|
308
|
+
if (this.append({ kind: "run.control", data: { action } }) === null) {
|
|
309
|
+
return conflict(id, `The run ended (${String(this.writer.state)}) while the steer was being delivered; it was not recorded as accepted.`, false);
|
|
310
|
+
}
|
|
311
|
+
return ok();
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
/** Open, run and close one worker; resolves with the terminal state. */
|
|
315
|
+
export async function runWorker(options) {
|
|
316
|
+
const worker = new RunWorker(options);
|
|
317
|
+
await worker.open();
|
|
318
|
+
try {
|
|
319
|
+
return await worker.run();
|
|
320
|
+
}
|
|
321
|
+
finally {
|
|
322
|
+
await worker.close();
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
//# sourceMappingURL=run-loop.js.map
|
|
@@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";
|
|
2
|
+
import { type Liveness } from "../store/liveness.js";
|
|
3
|
+
import { type ControlReply } from "./control.js";
|
|
4
|
+
import type { DriverSelection } from "./driver.js";
|
|
5
|
+
/** The compiled worker entry point. */
|
|
6
|
+
export declare const WORKER_ENTRY: string;
|
|
7
|
+
/** Live control of one worker over its control socket. */
|
|
8
|
+
export declare class WorkerControl {
|
|
9
|
+
readonly runDir: string;
|
|
10
|
+
private readonly timeoutMs;
|
|
11
|
+
/**
|
|
12
|
+
* A steer is answered only after the harness took the message, which can take seconds. Giving up
|
|
13
|
+
* earlier would tell the caller `unavailable` for a steer that is then delivered.
|
|
14
|
+
*/
|
|
15
|
+
private readonly steerTimeoutMs;
|
|
16
|
+
constructor(runDir: string, timeoutMs?: number,
|
|
17
|
+
/**
|
|
18
|
+
* A steer is answered only after the harness took the message, which can take seconds. Giving up
|
|
19
|
+
* earlier would tell the caller `unavailable` for a steer that is then delivered.
|
|
20
|
+
*/
|
|
21
|
+
steerTimeoutMs?: number);
|
|
22
|
+
ping(): Promise<ControlReply>;
|
|
23
|
+
steer(action: ActionRecord, message: string): Promise<ControlReply>;
|
|
24
|
+
cancel(action: ActionRecord, reason?: string): Promise<ControlReply>;
|
|
25
|
+
/** Have the worker record a refusal the core decided (`run.refused`). */
|
|
26
|
+
refuse(action: ActionRecord): Promise<ControlReply>;
|
|
27
|
+
/** Send an action; with no reply, settle its outcome from the run's log (see `settleFromLog`). */
|
|
28
|
+
private deliver;
|
|
29
|
+
}
|
|
30
|
+
export interface SpawnWorkerOptions {
|
|
31
|
+
stateDir: string;
|
|
32
|
+
/** The new run's record, in state `queued` with cursor 1 and no worker yet. */
|
|
33
|
+
run: Run;
|
|
34
|
+
driver: DriverSelection;
|
|
35
|
+
/** How long to wait for the durability condition. Default 10 000 ms. */
|
|
36
|
+
readyTimeoutMs?: number;
|
|
37
|
+
heartbeatMs?: number;
|
|
38
|
+
cancelGraceMs?: number;
|
|
39
|
+
/** Environment of the worker process. Default: this process's environment. */
|
|
40
|
+
env?: NodeJS.ProcessEnv;
|
|
41
|
+
}
|
|
42
|
+
export interface SpawnedWorker {
|
|
43
|
+
runId: RunId;
|
|
44
|
+
runDir: string;
|
|
45
|
+
pid: number;
|
|
46
|
+
control: WorkerControl;
|
|
47
|
+
}
|
|
48
|
+
/** The worker did not reach the durability condition; nothing may be acknowledged. */
|
|
49
|
+
export declare class WorkerSpawnError extends Error {
|
|
50
|
+
constructor(message: string);
|
|
51
|
+
}
|
|
52
|
+
/** Create the run, start its worker detached, and resolve once the worker's files are durable. */
|
|
53
|
+
export declare function spawnWorker(options: SpawnWorkerOptions): Promise<SpawnedWorker>;
|
|
54
|
+
export type Reconnect = {
|
|
55
|
+
live: true;
|
|
56
|
+
liveness: Liveness;
|
|
57
|
+
control: WorkerControl;
|
|
58
|
+
} | {
|
|
59
|
+
live: false;
|
|
60
|
+
liveness: Liveness;
|
|
61
|
+
reason: string;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Reach a surviving worker's control socket. `live: false` (with the reason) when the worker is
|
|
65
|
+
* not alive or does not answer. Note: in D1 a restarted core still reports such a run with
|
|
66
|
+
* `live_control: false` (README: after a core restart); this only establishes reachability.
|
|
67
|
+
*/
|
|
68
|
+
export declare function reconnectWorker(stateDir: string, runId: RunId, timeoutMs?: number): Promise<Reconnect>;
|