@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,67 @@
|
|
|
1
|
+
import { type Run, type RunEvent, type RunId, type RunState } from "../contract/index.js";
|
|
2
|
+
import { type DerivedRun } from "./derive.js";
|
|
3
|
+
import { type Liveness, type LivenessOptions } from "./liveness.js";
|
|
4
|
+
/** Create a run's directory (0700) and its record (0600). Ids are never reused: an existing run is an error. */
|
|
5
|
+
export declare function createRun(stateDir: string, record: Run): string;
|
|
6
|
+
export declare function readRecord(dir: string): Run;
|
|
7
|
+
/** Replace the record atomically (validated first). */
|
|
8
|
+
export declare function writeRecord(dir: string, record: Run, durable?: boolean): void;
|
|
9
|
+
/** Every run id with a directory in the store (unordered). */
|
|
10
|
+
export declare function listRunIds(stateDir: string): RunId[];
|
|
11
|
+
export interface ShowOptions extends LivenessOptions {
|
|
12
|
+
/** Whether the caller holds live control of this run's worker (false after a core restart). */
|
|
13
|
+
controllable?: boolean;
|
|
14
|
+
/** Append `run.state` -> `unknown` when the run is orphaned (the core's duty as the only writer left). */
|
|
15
|
+
settle?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface ShownRun extends DerivedRun {
|
|
18
|
+
liveness: Liveness;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Append `run.state` -> `unknown` to an orphaned run's log. Re-checks first: it writes only when
|
|
22
|
+
* the log has no terminal event and the worker is not alive. Returns the event, or null. Throws
|
|
23
|
+
* `LivenessUnknownError` (and writes nothing) when the worker's liveness cannot be read.
|
|
24
|
+
*/
|
|
25
|
+
export declare function settleOrphan(dir: string, runId: RunId, options?: LivenessOptions): RunEvent | null;
|
|
26
|
+
/**
|
|
27
|
+
* The derived run for one run directory; null when the run is not established (no event 1). Throws
|
|
28
|
+
* when the record, the log or the worker's liveness cannot be read: the caller reports the run as
|
|
29
|
+
* unreadable for that request, and nothing is settled.
|
|
30
|
+
*/
|
|
31
|
+
export declare function showRunAt(dir: string, options?: ShowOptions): ShownRun | null;
|
|
32
|
+
export declare function showRun(stateDir: string, runId: RunId, options?: ShowOptions): ShownRun | null;
|
|
33
|
+
/** Sort order of a run list: newest first by `created_at`, then by id, descending. */
|
|
34
|
+
export declare function newestFirst(a: Run, b: Run): number;
|
|
35
|
+
export interface ListOptions extends Omit<ShowOptions, "controllable"> {
|
|
36
|
+
state?: RunState;
|
|
37
|
+
agentId?: string;
|
|
38
|
+
limit?: number;
|
|
39
|
+
/** Which runs the caller holds live control of. */
|
|
40
|
+
controllable?: (runId: RunId) => boolean;
|
|
41
|
+
}
|
|
42
|
+
/** Derived runs, newest first by `created_at`, filtered and limited (README: runs.list). */
|
|
43
|
+
export declare function listRuns(stateDir: string, options?: ListOptions): Run[];
|
|
44
|
+
export interface EventsPage {
|
|
45
|
+
events: RunEvent[];
|
|
46
|
+
/** The log's newest cursor at read time. */
|
|
47
|
+
lastCursor: number;
|
|
48
|
+
/** True when events after the last returned one exist. */
|
|
49
|
+
more: boolean;
|
|
50
|
+
}
|
|
51
|
+
/** Events with cursor greater than `after`, ascending, at most `limit` (README: runs.logs). */
|
|
52
|
+
export declare function readEvents(dir: string, runId: RunId, after?: number, limit?: number): EventsPage;
|
|
53
|
+
export interface FollowOptions extends LivenessOptions {
|
|
54
|
+
signal?: AbortSignal;
|
|
55
|
+
/** Poll interval while idle. Default 20 ms. */
|
|
56
|
+
pollMs?: number;
|
|
57
|
+
/**
|
|
58
|
+
* While idle, check the worker every this many ms and settle an orphaned run (append
|
|
59
|
+
* `unknown`) so the stream ends honestly. 0 disables. Default 250 ms.
|
|
60
|
+
*/
|
|
61
|
+
orphanCheckMs?: number;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Every event with cursor greater than `after`, then live events as they are appended, with no
|
|
65
|
+
* gap and no duplicate. Ends right after the terminal `run.state` event, or when `signal` aborts.
|
|
66
|
+
*/
|
|
67
|
+
export declare function followEvents(dir: string, runId: RunId, after?: number, options?: FollowOptions): AsyncGenerator<RunEvent>;
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The run store: run directories under `<stateDir>/runs/<run-id>/`, the run record, and the
|
|
3
|
+
* readers the core uses (list, show, read events after a cursor, follow events). Every state a
|
|
4
|
+
* reader reports comes from `deriveRun` over the on-disk log and the worker's liveness.
|
|
5
|
+
*/
|
|
6
|
+
import { readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { open } from "node:fs/promises";
|
|
8
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
9
|
+
import { assertValid, isRunId, isTerminal } from "../contract/index.js";
|
|
10
|
+
import { deriveRun, ORPHANED_REASON } from "./derive.js";
|
|
11
|
+
import { EventLogWriter, checkNext, readLog } from "./event-log.js";
|
|
12
|
+
import { FILE_MODE, atomicWriteJson, ensureDir, fsyncDir, nowIso, runDir, runFile, runsDir } from "./files.js";
|
|
13
|
+
import { LivenessUnknownError, checkLiveness } from "./liveness.js";
|
|
14
|
+
/** Create a run's directory (0700) and its record (0600). Ids are never reused: an existing run is an error. */
|
|
15
|
+
export function createRun(stateDir, record) {
|
|
16
|
+
const run = assertValid("run", record);
|
|
17
|
+
const dir = runDir(stateDir, run.id);
|
|
18
|
+
ensureDir(runsDir(stateDir));
|
|
19
|
+
ensureDir(dir);
|
|
20
|
+
writeFileSync(runFile(dir, "record"), `${JSON.stringify(run)}\n`, { mode: FILE_MODE, flag: "wx" });
|
|
21
|
+
fsyncDir(dir);
|
|
22
|
+
return dir;
|
|
23
|
+
}
|
|
24
|
+
export function readRecord(dir) {
|
|
25
|
+
return assertValid("run", JSON.parse(readFileSync(runFile(dir, "record"), "utf8")));
|
|
26
|
+
}
|
|
27
|
+
/** Replace the record atomically (validated first). */
|
|
28
|
+
export function writeRecord(dir, record, durable = false) {
|
|
29
|
+
atomicWriteJson(runFile(dir, "record"), assertValid("run", record), durable);
|
|
30
|
+
}
|
|
31
|
+
/** Every run id with a directory in the store (unordered). */
|
|
32
|
+
export function listRunIds(stateDir) {
|
|
33
|
+
try {
|
|
34
|
+
return readdirSync(runsDir(stateDir)).filter((name) => isRunId(name));
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
if (err.code === "ENOENT")
|
|
38
|
+
return [];
|
|
39
|
+
throw err;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Append `run.state` -> `unknown` to an orphaned run's log. Re-checks first: it writes only when
|
|
44
|
+
* the log has no terminal event and the worker is not alive. Returns the event, or null. Throws
|
|
45
|
+
* `LivenessUnknownError` (and writes nothing) when the worker's liveness cannot be read.
|
|
46
|
+
*/
|
|
47
|
+
export function settleOrphan(dir, runId, options = {}) {
|
|
48
|
+
const liveness = checkLiveness(dir, options);
|
|
49
|
+
if (liveness.alive)
|
|
50
|
+
return null;
|
|
51
|
+
const writer = EventLogWriter.open(dir, runId, options.clock);
|
|
52
|
+
try {
|
|
53
|
+
if (writer.state === null || isTerminal(writer.state))
|
|
54
|
+
return null;
|
|
55
|
+
return writer.transition("unknown", `${ORPHANED_REASON} (${liveness.reason})`, true);
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
writer.close();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The derived run for one run directory; null when the run is not established (no event 1). Throws
|
|
63
|
+
* when the record, the log or the worker's liveness cannot be read: the caller reports the run as
|
|
64
|
+
* unreadable for that request, and nothing is settled.
|
|
65
|
+
*/
|
|
66
|
+
export function showRunAt(dir, options = {}) {
|
|
67
|
+
const record = readRecord(dir);
|
|
68
|
+
let liveness = checkLiveness(dir, options);
|
|
69
|
+
let log = readLog(dir, record.id);
|
|
70
|
+
const derive = () => deriveRun({
|
|
71
|
+
record,
|
|
72
|
+
events: log.events,
|
|
73
|
+
alive: liveness.alive,
|
|
74
|
+
controllable: options.controllable ?? false,
|
|
75
|
+
now: nowIso(options.clock),
|
|
76
|
+
goneReason: liveness.reason,
|
|
77
|
+
});
|
|
78
|
+
let derived = derive();
|
|
79
|
+
if (derived?.orphaned && options.settle) {
|
|
80
|
+
settleOrphan(dir, record.id, options);
|
|
81
|
+
liveness = checkLiveness(dir, options);
|
|
82
|
+
log = readLog(dir, record.id);
|
|
83
|
+
derived = derive();
|
|
84
|
+
}
|
|
85
|
+
return derived === null ? null : { ...derived, liveness };
|
|
86
|
+
}
|
|
87
|
+
export function showRun(stateDir, runId, options = {}) {
|
|
88
|
+
return showRunAt(runDir(stateDir, runId), options);
|
|
89
|
+
}
|
|
90
|
+
/** Sort order of a run list: newest first by `created_at`, then by id, descending. */
|
|
91
|
+
export function newestFirst(a, b) {
|
|
92
|
+
if (a.created_at < b.created_at)
|
|
93
|
+
return 1;
|
|
94
|
+
if (a.created_at > b.created_at)
|
|
95
|
+
return -1;
|
|
96
|
+
return a.id < b.id ? 1 : -1;
|
|
97
|
+
}
|
|
98
|
+
/** Derived runs, newest first by `created_at`, filtered and limited (README: runs.list). */
|
|
99
|
+
export function listRuns(stateDir, options = {}) {
|
|
100
|
+
const runs = [];
|
|
101
|
+
for (const id of listRunIds(stateDir)) {
|
|
102
|
+
const shown = showRunAt(runDir(stateDir, id), { ...options, controllable: options.controllable?.(id) ?? false });
|
|
103
|
+
if (shown === null)
|
|
104
|
+
continue;
|
|
105
|
+
if (options.state !== undefined && shown.run.state !== options.state)
|
|
106
|
+
continue;
|
|
107
|
+
if (options.agentId !== undefined && shown.run.agent_id !== options.agentId)
|
|
108
|
+
continue;
|
|
109
|
+
runs.push(shown.run);
|
|
110
|
+
}
|
|
111
|
+
runs.sort(newestFirst);
|
|
112
|
+
return runs.slice(0, options.limit ?? 50);
|
|
113
|
+
}
|
|
114
|
+
/** Events with cursor greater than `after`, ascending, at most `limit` (README: runs.logs). */
|
|
115
|
+
export function readEvents(dir, runId, after = 0, limit = 500) {
|
|
116
|
+
const log = readLog(dir, runId);
|
|
117
|
+
if (!Number.isInteger(after) || after < 0 || after > log.tail.cursor) {
|
|
118
|
+
throw new RangeError(`after ${after} is past the run's cursor ${log.tail.cursor}`);
|
|
119
|
+
}
|
|
120
|
+
const rest = log.events.slice(after);
|
|
121
|
+
return { events: rest.slice(0, limit), lastCursor: log.tail.cursor, more: rest.length > limit };
|
|
122
|
+
}
|
|
123
|
+
/** The bytes of the file at `path` from offset `pos` on; empty when there are none or the file does not exist yet. */
|
|
124
|
+
async function readFrom(path, pos) {
|
|
125
|
+
let chunk = Buffer.alloc(0);
|
|
126
|
+
try {
|
|
127
|
+
const fh = await open(path, "r");
|
|
128
|
+
try {
|
|
129
|
+
const { size } = await fh.stat();
|
|
130
|
+
if (size > pos) {
|
|
131
|
+
chunk = Buffer.alloc(size - pos);
|
|
132
|
+
const { bytesRead } = await fh.read(chunk, 0, chunk.length, pos);
|
|
133
|
+
chunk = chunk.subarray(0, bytesRead);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
await fh.close();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
if (err.code !== "ENOENT")
|
|
142
|
+
throw err;
|
|
143
|
+
}
|
|
144
|
+
return chunk;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* While a follower is idle: when a check is due, settle an orphaned run. Returns when the worker
|
|
148
|
+
* was last checked (`last` when no check was due).
|
|
149
|
+
*/
|
|
150
|
+
function checkOrphanWhenDue(dir, runId, options, everyMs, last) {
|
|
151
|
+
if (everyMs <= 0 || Date.now() - last < everyMs)
|
|
152
|
+
return last;
|
|
153
|
+
const checked = Date.now();
|
|
154
|
+
try {
|
|
155
|
+
settleOrphan(dir, runId, options);
|
|
156
|
+
}
|
|
157
|
+
catch (err) {
|
|
158
|
+
// The worker is not known to be dead, so nothing is settled; the next tick checks again.
|
|
159
|
+
if (!(err instanceof LivenessUnknownError))
|
|
160
|
+
throw err;
|
|
161
|
+
}
|
|
162
|
+
return checked;
|
|
163
|
+
}
|
|
164
|
+
/** Wait `ms`. False when `signal` aborted the wait. */
|
|
165
|
+
async function pause(ms, signal) {
|
|
166
|
+
try {
|
|
167
|
+
await delay(ms, undefined, signal ? { signal } : {});
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Every event with cursor greater than `after`, then live events as they are appended, with no
|
|
176
|
+
* gap and no duplicate. Ends right after the terminal `run.state` event, or when `signal` aborts.
|
|
177
|
+
*/
|
|
178
|
+
export async function* followEvents(dir, runId, after = 0, options = {}) {
|
|
179
|
+
const pollMs = options.pollMs ?? 20;
|
|
180
|
+
const orphanCheckMs = options.orphanCheckMs ?? 250;
|
|
181
|
+
/** Offset just past the last complete line consumed; a partial final line is re-read, never kept. */
|
|
182
|
+
let pos = 0;
|
|
183
|
+
let tail = { cursor: 0, state: null };
|
|
184
|
+
let lastOrphanCheck = Date.now();
|
|
185
|
+
const path = runFile(dir, "events");
|
|
186
|
+
while (!options.signal?.aborted) {
|
|
187
|
+
const chunk = await readFrom(path, pos);
|
|
188
|
+
// Only newline-terminated lines count. A partial final line is a write in progress or a torn
|
|
189
|
+
// append that the log's writer will truncate, so it is read again from its start next time.
|
|
190
|
+
const complete = chunk.subarray(0, chunk.lastIndexOf(0x0a) + 1);
|
|
191
|
+
pos += complete.length;
|
|
192
|
+
let from = 0;
|
|
193
|
+
let nl;
|
|
194
|
+
while ((nl = complete.indexOf(0x0a, from)) >= 0) {
|
|
195
|
+
const line = complete.subarray(from, nl).toString("utf8");
|
|
196
|
+
from = nl + 1;
|
|
197
|
+
const next = checkNext(runId, tail, JSON.parse(line), tail.cursor + 1);
|
|
198
|
+
tail = next.tail;
|
|
199
|
+
if (next.event.cursor > after)
|
|
200
|
+
yield next.event;
|
|
201
|
+
if (tail.state !== null && isTerminal(tail.state))
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (complete.length > 0)
|
|
205
|
+
continue;
|
|
206
|
+
lastOrphanCheck = checkOrphanWhenDue(dir, runId, options, orphanCheckMs, lastOrphanCheck);
|
|
207
|
+
if (!(await pause(pollMs, options.signal)))
|
|
208
|
+
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AAC,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;YAC1D,IAAI,GAAG,EAAE,GAAG,CAAC,CAAC;YACd,MAAM,IAAI,GAAG,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;YACvE,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;YACjB,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,KAAK;gBAAE,MAAM,IAAI,CAAC,KAAK,CAAC;YAChD,IAAI,IAAI,CAAC,KAAK,KAAK,IAAI,IAAI,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC;gBAAE,OAAO;QAC5D,CAAC;QACD,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC;YAAE,SAAS;QAClC,eAAe,GAAG,kBAAkB,CAAC,GAAG,EAAE,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,eAAe,CAAC,CAAC;QAC1F,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;YAAE,OAAO;IACrD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sequential work made explicit: polls, retries and ordered writes that must run one step after
|
|
3
|
+
* another, never side by side.
|
|
4
|
+
*
|
|
5
|
+
* Everything here is driven by `stepUntil`. It holds one promise for the whole sequence and starts
|
|
6
|
+
* each step from a timer or from the settling of the step before, so a poll that runs for hours
|
|
7
|
+
* keeps no chain of pending promises and no growing stack. It imports nothing, so any layer may
|
|
8
|
+
* use it.
|
|
9
|
+
*/
|
|
10
|
+
/** What one step decided: the sequence is over with `value`, or it runs again after `waitMs`. */
|
|
11
|
+
export type Step<T> = {
|
|
12
|
+
readonly done: true;
|
|
13
|
+
readonly value: T;
|
|
14
|
+
} | {
|
|
15
|
+
readonly done: false;
|
|
16
|
+
readonly waitMs: number | undefined;
|
|
17
|
+
};
|
|
18
|
+
/** End the sequence with `value`. */
|
|
19
|
+
export declare const finish: <T>(value: T) => Step<T>;
|
|
20
|
+
/** Run the step again: after `waitMs`, or right away when no wait is given. */
|
|
21
|
+
export declare const again: (waitMs?: number) => Step<never>;
|
|
22
|
+
export interface StepOptions<T> {
|
|
23
|
+
/**
|
|
24
|
+
* Checked before every step, and cuts a wait short. A step that is already running is left to
|
|
25
|
+
* finish, and what it returns still counts.
|
|
26
|
+
*/
|
|
27
|
+
signal?: AbortSignal;
|
|
28
|
+
/** The sequence's value once `signal` has aborted. Without it the sequence rejects with the signal's reason. */
|
|
29
|
+
onAbort?: () => T;
|
|
30
|
+
/** Stands in for the timer between steps (tests that must not sleep). An abort ends the wait early, as with the timer. */
|
|
31
|
+
wait?: (ms: number) => Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Call `step` until it returns `finish(value)`, waiting between calls as each `again(ms)` asks.
|
|
35
|
+
* The first step runs before this returns. A step that throws or rejects ends the sequence with
|
|
36
|
+
* that error. Two steps never overlap.
|
|
37
|
+
*/
|
|
38
|
+
export declare function stepUntil<T>(step: () => Step<T> | Promise<Step<T>>, options?: StepOptions<T>): Promise<T>;
|
|
39
|
+
export interface PollOptions {
|
|
40
|
+
/** Pause between checks. */
|
|
41
|
+
everyMs: number;
|
|
42
|
+
/** Give up once this much time has passed, counted from the call. Looked at after a failed check. */
|
|
43
|
+
timeoutMs?: number;
|
|
44
|
+
/** Rejects the poll with the signal's reason, also in the middle of a pause. */
|
|
45
|
+
signal?: AbortSignal;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Call `check` every `everyMs` until it returns something truthy, and resolve with that. Resolves
|
|
49
|
+
* `undefined` when `timeoutMs` has passed after a failed check (there is always a first check). A
|
|
50
|
+
* rejection from `check` rejects the poll.
|
|
51
|
+
*/
|
|
52
|
+
export declare function pollUntil<T>(check: () => T | Promise<T>, options: PollOptions): Promise<T | undefined>;
|
|
53
|
+
/**
|
|
54
|
+
* Run `fn` on each item strictly one after another. The first call is made before this returns,
|
|
55
|
+
* the items are read as they are reached, and the first throw or rejection ends it: later items
|
|
56
|
+
* are not touched.
|
|
57
|
+
*/
|
|
58
|
+
export declare function inOrder<T>(items: Iterable<T>, fn: (item: T, index: number) => void | Promise<void>): Promise<void>;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sequential work made explicit: polls, retries and ordered writes that must run one step after
|
|
3
|
+
* another, never side by side.
|
|
4
|
+
*
|
|
5
|
+
* Everything here is driven by `stepUntil`. It holds one promise for the whole sequence and starts
|
|
6
|
+
* each step from a timer or from the settling of the step before, so a poll that runs for hours
|
|
7
|
+
* keeps no chain of pending promises and no growing stack. It imports nothing, so any layer may
|
|
8
|
+
* use it.
|
|
9
|
+
*/
|
|
10
|
+
/** End the sequence with `value`. */
|
|
11
|
+
export const finish = (value) => ({ done: true, value });
|
|
12
|
+
/** Run the step again: after `waitMs`, or right away when no wait is given. */
|
|
13
|
+
export const again = (waitMs) => ({ done: false, waitMs });
|
|
14
|
+
/**
|
|
15
|
+
* Call `step` until it returns `finish(value)`, waiting between calls as each `again(ms)` asks.
|
|
16
|
+
* The first step runs before this returns. A step that throws or rejects ends the sequence with
|
|
17
|
+
* that error. Two steps never overlap.
|
|
18
|
+
*/
|
|
19
|
+
export function stepUntil(step, options = {}) {
|
|
20
|
+
const { signal, onAbort, wait } = options;
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
const stop = () => {
|
|
23
|
+
try {
|
|
24
|
+
// Only reached once the signal has aborted, and an aborted signal always carries a reason.
|
|
25
|
+
if (onAbort === undefined)
|
|
26
|
+
reject(signal?.reason);
|
|
27
|
+
else
|
|
28
|
+
resolve(onAbort());
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
reject(error);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const pause = (ms) => {
|
|
35
|
+
// Whichever comes first, the end of the wait or an abort, starts the next step: once.
|
|
36
|
+
let woken = false;
|
|
37
|
+
const timer = wait === undefined ? setTimeout(wake, ms) : undefined;
|
|
38
|
+
function wake() {
|
|
39
|
+
if (woken)
|
|
40
|
+
return;
|
|
41
|
+
woken = true;
|
|
42
|
+
clearTimeout(timer);
|
|
43
|
+
signal?.removeEventListener("abort", wake);
|
|
44
|
+
tick();
|
|
45
|
+
}
|
|
46
|
+
signal?.addEventListener("abort", wake, { once: true });
|
|
47
|
+
wait?.(ms).then(wake, (error) => {
|
|
48
|
+
if (!woken)
|
|
49
|
+
reject(error);
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
const settle = (verdict) => {
|
|
53
|
+
if (verdict.done)
|
|
54
|
+
resolve(verdict.value);
|
|
55
|
+
// An abort while the step ran fired no listener of ours, so look before starting a wait.
|
|
56
|
+
else if (verdict.waitMs === undefined || signal?.aborted)
|
|
57
|
+
tick();
|
|
58
|
+
else
|
|
59
|
+
pause(verdict.waitMs);
|
|
60
|
+
};
|
|
61
|
+
function tick() {
|
|
62
|
+
if (signal?.aborted) {
|
|
63
|
+
stop();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
// The step runs now and a throw becomes a rejection; `settle` always runs from a fresh
|
|
67
|
+
// microtask, so steps that ask for no wait do not nest on the stack.
|
|
68
|
+
let ran;
|
|
69
|
+
try {
|
|
70
|
+
ran = Promise.resolve(step());
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
reject(err);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
ran.then(settle).catch(reject);
|
|
77
|
+
}
|
|
78
|
+
tick();
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Call `check` every `everyMs` until it returns something truthy, and resolve with that. Resolves
|
|
83
|
+
* `undefined` when `timeoutMs` has passed after a failed check (there is always a first check). A
|
|
84
|
+
* rejection from `check` rejects the poll.
|
|
85
|
+
*/
|
|
86
|
+
export function pollUntil(check, options) {
|
|
87
|
+
const until = options.timeoutMs === undefined ? undefined : Date.now() + options.timeoutMs;
|
|
88
|
+
const verdict = (value) => {
|
|
89
|
+
if (value)
|
|
90
|
+
return finish(value);
|
|
91
|
+
if (until !== undefined && Date.now() > until)
|
|
92
|
+
return finish(undefined);
|
|
93
|
+
return again(options.everyMs);
|
|
94
|
+
};
|
|
95
|
+
const step = () => {
|
|
96
|
+
const value = check();
|
|
97
|
+
return value instanceof Promise ? value.then(verdict) : verdict(value);
|
|
98
|
+
};
|
|
99
|
+
return stepUntil(step, options.signal ? { signal: options.signal } : {});
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Run `fn` on each item strictly one after another. The first call is made before this returns,
|
|
103
|
+
* the items are read as they are reached, and the first throw or rejection ends it: later items
|
|
104
|
+
* are not touched.
|
|
105
|
+
*/
|
|
106
|
+
export function inOrder(items, fn) {
|
|
107
|
+
const iterator = items[Symbol.iterator]();
|
|
108
|
+
let index = 0;
|
|
109
|
+
const proceed = () => again();
|
|
110
|
+
return stepUntil(() => {
|
|
111
|
+
const next = iterator.next();
|
|
112
|
+
if (next.done)
|
|
113
|
+
return finish(undefined);
|
|
114
|
+
const ran = fn(next.value, index);
|
|
115
|
+
index += 1;
|
|
116
|
+
return ran instanceof Promise ? ran.then(proceed) : proceed();
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=sequence.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sequence.js","sourceRoot":"","sources":["../../../src/util/sequence.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,qCAAqC;AACrC,MAAM,CAAC,MAAM,MAAM,GAAG,CAAI,KAAQ,EAAW,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;AAExE,+EAA+E;AAC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O,EAAE,CAAC;IAChE,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type Server } from "node:net";
|
|
2
|
+
import { type ActionRecord, type ErrorBody, type RunState } from "../contract/index.js";
|
|
3
|
+
export type ControlRequest = {
|
|
4
|
+
op: "ping";
|
|
5
|
+
} | {
|
|
6
|
+
op: "steer";
|
|
7
|
+
action: ActionRecord;
|
|
8
|
+
message: string;
|
|
9
|
+
} | {
|
|
10
|
+
op: "cancel";
|
|
11
|
+
action: ActionRecord;
|
|
12
|
+
reason?: string;
|
|
13
|
+
}
|
|
14
|
+
/** Record a refusal decided by the core as `run.refused` (the worker is the log's only writer). */
|
|
15
|
+
| {
|
|
16
|
+
op: "refuse";
|
|
17
|
+
action: ActionRecord;
|
|
18
|
+
};
|
|
19
|
+
export type ControlReply = {
|
|
20
|
+
ok: true;
|
|
21
|
+
pid: number;
|
|
22
|
+
state: RunState | null;
|
|
23
|
+
cursor: number;
|
|
24
|
+
} | {
|
|
25
|
+
ok: false;
|
|
26
|
+
error: ErrorBody;
|
|
27
|
+
};
|
|
28
|
+
export type ControlHandler = (request: ControlRequest) => ControlReply | Promise<ControlReply>;
|
|
29
|
+
/** Parse and validate one request line. Payload text is data, never interpreted. */
|
|
30
|
+
export declare function parseRequest(line: string): ControlRequest | ControlReply;
|
|
31
|
+
/** Listen on the run's control socket (replacing a stale socket file). */
|
|
32
|
+
export declare function listenControl(dir: string, handler: ControlHandler): Promise<Server>;
|
|
33
|
+
/**
|
|
34
|
+
* Stop accepting connections, then wait until every accepted request has had its reply written
|
|
35
|
+
* and its connection closed, and remove the socket file. A connection that has not sent a complete
|
|
36
|
+
* request within `graceMs` is dropped unanswered: its request never reached the worker, which the
|
|
37
|
+
* client reports as `no-reply` (see `ControlUnavailableError`).
|
|
38
|
+
*/
|
|
39
|
+
export declare function closeControl(dir: string, server: Server, options?: {
|
|
40
|
+
graceMs?: number;
|
|
41
|
+
}): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Why a control request got no reply:
|
|
44
|
+
* - `unreachable`: no worker accepted the connection (no socket, or nothing listening). The
|
|
45
|
+
* request was not delivered.
|
|
46
|
+
* - `no-reply`: the connection was dropped before any reply byte. A live worker answers every
|
|
47
|
+
* request it has read before it closes the channel, so this means the request was not read,
|
|
48
|
+
* unless the worker died while handling it.
|
|
49
|
+
* - `timeout`: the worker did not answer in time; the request may still take effect.
|
|
50
|
+
* - `bad-reply`: the reply could not be read.
|
|
51
|
+
* Callers settle the outcome from the run's log (`settleFromLog`), never by retrying.
|
|
52
|
+
*/
|
|
53
|
+
export type ControlFailure = "unreachable" | "no-reply" | "timeout" | "bad-reply";
|
|
54
|
+
/** The control socket could not be reached (no worker listening, or it did not answer). */
|
|
55
|
+
export declare class ControlUnavailableError extends Error {
|
|
56
|
+
readonly failure: ControlFailure;
|
|
57
|
+
constructor(message: string, failure?: ControlFailure);
|
|
58
|
+
}
|
|
59
|
+
/** Send one request to a run's worker and wait for its reply. */
|
|
60
|
+
export declare function controlRequest(dir: string, request: ControlRequest, timeoutMs?: number): Promise<ControlReply>;
|