@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,125 @@
|
|
|
1
|
+
export interface ExecOptions {
|
|
2
|
+
/** Working directory; relative paths resolve against the sandbox home. */
|
|
3
|
+
readonly cwd?: string;
|
|
4
|
+
/** Extra environment. GH_TOKEN, GITHUB_TOKEN and SSH_AUTH_SOCK are always dropped. */
|
|
5
|
+
readonly env?: Readonly<Record<string, string>>;
|
|
6
|
+
readonly stdin?: Uint8Array;
|
|
7
|
+
readonly signal?: AbortSignal;
|
|
8
|
+
}
|
|
9
|
+
export interface ExecResult {
|
|
10
|
+
readonly exitCode: number;
|
|
11
|
+
readonly stdout: Uint8Array;
|
|
12
|
+
readonly stderr: Uint8Array;
|
|
13
|
+
}
|
|
14
|
+
export interface SpawnOptions {
|
|
15
|
+
readonly cwd?: string;
|
|
16
|
+
readonly env?: Readonly<Record<string, string>>;
|
|
17
|
+
/** Keep stdin open for `write`; otherwise it is closed at once. */
|
|
18
|
+
readonly stdin?: boolean;
|
|
19
|
+
/** Aborting cancels the process. */
|
|
20
|
+
readonly signal?: AbortSignal;
|
|
21
|
+
}
|
|
22
|
+
export interface RunProcess {
|
|
23
|
+
readonly stdout: ReadableStream<Uint8Array>;
|
|
24
|
+
readonly stderr: ReadableStream<Uint8Array>;
|
|
25
|
+
/** Write to stdin; throws when the process was spawned without stdin. */
|
|
26
|
+
write(data: Uint8Array): void;
|
|
27
|
+
closeInput(): void;
|
|
28
|
+
/** Resolves when the remote process ends. A cancelled process reports 137 when no code arrives. */
|
|
29
|
+
wait(): Promise<{
|
|
30
|
+
exitCode: number;
|
|
31
|
+
}>;
|
|
32
|
+
/** End the remote process. Idempotent. */
|
|
33
|
+
cancel(): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
/** A host-side TCP endpoint that tunnels to a port inside the sandbox. */
|
|
36
|
+
export interface PortEndpoint {
|
|
37
|
+
readonly host: string;
|
|
38
|
+
readonly port: number;
|
|
39
|
+
}
|
|
40
|
+
export type RunSandboxState = "ready" | "stopped" | "destroyed";
|
|
41
|
+
/** One run's sandbox. Obtained from {@link RunSandboxes}; never constructed directly. */
|
|
42
|
+
export interface RunSandbox {
|
|
43
|
+
readonly runId: string;
|
|
44
|
+
/** Sandbox name on the gateway (deterministic from the run id). */
|
|
45
|
+
readonly name: string;
|
|
46
|
+
/** The sandbox's home / default working directory, read from the live sandbox. */
|
|
47
|
+
readonly home: string;
|
|
48
|
+
readonly state: RunSandboxState;
|
|
49
|
+
/** Run a shell script (`sh -c`) to completion. */
|
|
50
|
+
exec(script: string, options?: ExecOptions): Promise<ExecResult>;
|
|
51
|
+
/** File bytes, or null when the path does not exist. */
|
|
52
|
+
readFile(path: string, options?: {
|
|
53
|
+
signal?: AbortSignal;
|
|
54
|
+
}): Promise<Uint8Array | null>;
|
|
55
|
+
/** Write bytes, creating parent directories. Large contents are sent in chunks. */
|
|
56
|
+
writeFile(path: string, content: Uint8Array, options?: {
|
|
57
|
+
signal?: AbortSignal;
|
|
58
|
+
}): Promise<void>;
|
|
59
|
+
spawn(command: string, options?: SpawnOptions): Promise<RunProcess>;
|
|
60
|
+
/** Forward a host loopback port to `port` in the sandbox (reused per port). */
|
|
61
|
+
endpoint(port: number): Promise<PortEndpoint>;
|
|
62
|
+
/** Cancel tracked processes and close forwards; the sandbox stays on the gateway. */
|
|
63
|
+
stop(): Promise<void>;
|
|
64
|
+
/** Stop, then delete the sandbox and wait until the gateway reports it gone. Idempotent. */
|
|
65
|
+
destroy(): Promise<void>;
|
|
66
|
+
}
|
|
67
|
+
export interface RunSandboxes {
|
|
68
|
+
/**
|
|
69
|
+
* Create and wait for the run's sandbox. If anything after the create call
|
|
70
|
+
* fails (readiness, abort, binding), the sandbox is deleted before the error
|
|
71
|
+
* propagates. Refuses a run id that already has a live sandbox in this process.
|
|
72
|
+
*/
|
|
73
|
+
create(runId: string, options?: {
|
|
74
|
+
signal?: AbortSignal;
|
|
75
|
+
}): Promise<RunSandbox>;
|
|
76
|
+
/** Rebind to the run's existing sandbox (another process may have created it). */
|
|
77
|
+
resume(runId: string, options?: {
|
|
78
|
+
signal?: AbortSignal;
|
|
79
|
+
}): Promise<RunSandbox>;
|
|
80
|
+
/**
|
|
81
|
+
* Delete the run's sandbox by run id alone, whoever created it. Works when no
|
|
82
|
+
* handle exists (crash recovery, a leaked adapter session). Idempotent.
|
|
83
|
+
*/
|
|
84
|
+
destroy(runId: string): Promise<void>;
|
|
85
|
+
/** Create, run `fn`, and always destroy, whether `fn` resolves or throws. */
|
|
86
|
+
withRun<T>(runId: string, fn: (sandbox: RunSandbox) => Promise<T>, options?: {
|
|
87
|
+
signal?: AbortSignal;
|
|
88
|
+
}): Promise<T>;
|
|
89
|
+
/** Run ids with a live sandbox handle in this process. */
|
|
90
|
+
active(): string[];
|
|
91
|
+
/**
|
|
92
|
+
* Delete every sandbox this core manages (by label and prefix) whose run id is
|
|
93
|
+
* not in `keep`. With `only`, sandboxes of other run ids are left alone, so a core that shares
|
|
94
|
+
* a gateway with another core reclaims its own runs and nothing else. Returns the deleted run ids.
|
|
95
|
+
*/
|
|
96
|
+
sweep(keep?: Iterable<string>, only?: Iterable<string>): Promise<string[]>;
|
|
97
|
+
}
|
|
98
|
+
export interface RunSandboxesConfig {
|
|
99
|
+
/** Container image with git, Node and the harness CLIs. */
|
|
100
|
+
readonly image: string;
|
|
101
|
+
/** Names of credential providers already configured on the gateway. */
|
|
102
|
+
readonly providers?: readonly string[];
|
|
103
|
+
/** Sandbox-name prefix (default `ca-`). */
|
|
104
|
+
readonly prefix?: string;
|
|
105
|
+
/** Path to an authored policy JSON file; default `core/policy/run-sandbox.policy.json`. */
|
|
106
|
+
readonly policyPath?: string;
|
|
107
|
+
readonly readyTimeoutSecs?: number;
|
|
108
|
+
readonly deleteTimeoutSecs?: number;
|
|
109
|
+
/** Overall bound on one deletion, every attempt and wait together (default 90 s). */
|
|
110
|
+
readonly deleteDeadlineMs?: number;
|
|
111
|
+
/** Diagnostics hook; receives no secrets. */
|
|
112
|
+
readonly log?: (message: string) => void;
|
|
113
|
+
}
|
|
114
|
+
/** Thrown by `create` when this process already holds a live sandbox for the run. */
|
|
115
|
+
export declare class SandboxBusyError extends Error {
|
|
116
|
+
readonly name = "SandboxBusyError";
|
|
117
|
+
}
|
|
118
|
+
/** Thrown when `resume` finds no sandbox, or one whose run-id label does not match. */
|
|
119
|
+
export declare class SandboxNotFoundError extends Error {
|
|
120
|
+
readonly name = "SandboxNotFoundError";
|
|
121
|
+
}
|
|
122
|
+
/** Thrown when deletion could not be confirmed; the run id stays queued for `sweep`. */
|
|
123
|
+
export declare class SandboxCleanupError extends Error {
|
|
124
|
+
readonly name = "SandboxCleanupError";
|
|
125
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Harness-neutral sandbox surface. Other core modules import these types only;
|
|
2
|
+
// nothing here mentions the AI SDK or the OpenShell SDK.
|
|
3
|
+
/** Thrown by `create` when this process already holds a live sandbox for the run. */
|
|
4
|
+
export class SandboxBusyError extends Error {
|
|
5
|
+
name = "SandboxBusyError";
|
|
6
|
+
}
|
|
7
|
+
/** Thrown when `resume` finds no sandbox, or one whose run-id label does not match. */
|
|
8
|
+
export class SandboxNotFoundError extends Error {
|
|
9
|
+
name = "SandboxNotFoundError";
|
|
10
|
+
}
|
|
11
|
+
/** Thrown when deletion could not be confirmed; the run id stays queued for `sweep`. */
|
|
12
|
+
export class SandboxCleanupError extends Error {
|
|
13
|
+
name = "SandboxCleanupError";
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/sandbox/types.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,yDAAyD;AAgHzD,qFAAqF;AACrF,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IACvB,IAAI,GAAG,kBAAkB,CAAC;CAC7C;AAED,uFAAuF;AACvF,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAC3B,IAAI,GAAG,sBAAsB,CAAC;CACjD;AAED,wFAAwF;AACxF,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC1B,IAAI,GAAG,qBAAqB,CAAC;CAChD"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { type CommandRunner } from "./host-command.js";
|
|
2
|
+
import type { RunSandbox } from "./types.js";
|
|
3
|
+
/** Where the bundle lands inside the sandbox, relative to its home. */
|
|
4
|
+
export declare const SANDBOX_BUNDLE = ".culture-agents/run.bundle";
|
|
5
|
+
export declare const DEFAULT_WORKDIR = "work";
|
|
6
|
+
/**
|
|
7
|
+
* Options for the ssh that `collect`'s fetch runs, on top of OpenShell's generated config. The
|
|
8
|
+
* sandbox's sshd is not trusted: the operator's keys and agent are never offered or forwarded to
|
|
9
|
+
* it, nothing prompts, and host keys go to a throwaway file (`UserKnownHostsFile`, added per call
|
|
10
|
+
* inside the temp dir) instead of ~/.ssh/known_hosts. `IdentitiesOnly` alone still tries the
|
|
11
|
+
* default ~/.ssh/id_* files when the config names no key (`ssh -G` shows them), so
|
|
12
|
+
* `IdentityFile=none` drops those defaults; a key the generated config names is still used.
|
|
13
|
+
* `StrictHostKeyChecking` is deliberately absent: the generated config decides it.
|
|
14
|
+
*/
|
|
15
|
+
export declare const FETCH_SSH_OPTIONS: readonly ["BatchMode=yes", "IdentitiesOnly=yes", "IdentityFile=none", "IdentityAgent=none", "ForwardAgent=no"];
|
|
16
|
+
/** Name of the throwaway known-hosts file inside the fetch's temp dir. */
|
|
17
|
+
export declare const FETCH_KNOWN_HOSTS = "known_hosts";
|
|
18
|
+
/** The `GIT_SSH_COMMAND` for a fetch: the generated config plus FETCH_SSH_OPTIONS. */
|
|
19
|
+
export declare function fetchSshCommand(configPath: string, knownHostsPath: string): string;
|
|
20
|
+
export declare class HandoffError extends Error {
|
|
21
|
+
readonly name = "HandoffError";
|
|
22
|
+
}
|
|
23
|
+
export interface PrepareOptions {
|
|
24
|
+
/** Path to the host repository (any worktree of it). Read only. */
|
|
25
|
+
readonly hostRepo: string;
|
|
26
|
+
/** Branch name or commit to start the run from. */
|
|
27
|
+
readonly ref: string;
|
|
28
|
+
readonly runId: string;
|
|
29
|
+
/** Clone directory name under the sandbox home (default `work`). */
|
|
30
|
+
readonly workdir?: string;
|
|
31
|
+
/** Commit identity configured in the sandbox clone (not the operator's). */
|
|
32
|
+
readonly identity?: {
|
|
33
|
+
readonly name: string;
|
|
34
|
+
readonly email: string;
|
|
35
|
+
};
|
|
36
|
+
readonly runner?: CommandRunner;
|
|
37
|
+
readonly signal?: AbortSignal;
|
|
38
|
+
}
|
|
39
|
+
export interface PreparedTree {
|
|
40
|
+
/** Branch checked out in the sandbox clone: `runs/<id>`. */
|
|
41
|
+
readonly branch: string;
|
|
42
|
+
/** Commit the run starts from. */
|
|
43
|
+
readonly baseCommit: string;
|
|
44
|
+
/** Absolute path of the clone inside the sandbox. */
|
|
45
|
+
readonly path: string;
|
|
46
|
+
}
|
|
47
|
+
export interface CollectOptions {
|
|
48
|
+
readonly hostRepo: string;
|
|
49
|
+
readonly runId: string;
|
|
50
|
+
/** Absolute clone path inside the sandbox (from prepare); default `<home>/work`. */
|
|
51
|
+
readonly remotePath?: string;
|
|
52
|
+
/** `openshell` CLI used for `sandbox ssh-config` (default: `openshell` on PATH). */
|
|
53
|
+
readonly openshellBin?: string;
|
|
54
|
+
/** Gateway name passed as `-g` to the CLI, when not the CLI's default. */
|
|
55
|
+
readonly gatewayName?: string;
|
|
56
|
+
/** The commit the run started from; when given, the result lists the commits after it. */
|
|
57
|
+
readonly baseCommit?: string;
|
|
58
|
+
readonly runner?: CommandRunner;
|
|
59
|
+
}
|
|
60
|
+
export interface CollectedTree {
|
|
61
|
+
readonly branch: string;
|
|
62
|
+
/** Tip of `runs/<id>` in the host repository after the fetch. */
|
|
63
|
+
readonly commit: string;
|
|
64
|
+
/** Commits after `baseCommit` on the branch, oldest first; empty when no base was given. */
|
|
65
|
+
readonly commits: string[];
|
|
66
|
+
}
|
|
67
|
+
/** Bundle `ref` of the host repo, upload it, and clone it as `runs/<id>` inside the sandbox. */
|
|
68
|
+
export declare function prepare(sandbox: RunSandbox, options: PrepareOptions): Promise<PreparedTree>;
|
|
69
|
+
/** Parse the `Host` alias from `openshell sandbox ssh-config` output. */
|
|
70
|
+
export declare function sshHostAlias(config: string): string;
|
|
71
|
+
/** Fetch `runs/<id>` from the sandbox over OpenShell's SSH into the host repo. */
|
|
72
|
+
export declare function collect(sandbox: RunSandbox, options: CollectOptions): Promise<CollectedTree>;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// Working-tree handoff (decided in docs/d0/decisions.md): the repository enters
|
|
2
|
+
// the sandbox as a git bundle and is cloned there; the host fetches the run's
|
|
3
|
+
// commits straight from the sandbox over SSH into `runs/<id>`.
|
|
4
|
+
//
|
|
5
|
+
// The host repository is only ever read (rev-parse, symbolic-ref) and fetched
|
|
6
|
+
// into (one non-forced refspec onto runs/<id>). No command here checks out,
|
|
7
|
+
// resets, switches, merges, commits, or writes the host working tree or the
|
|
8
|
+
// host's current branch. The bundle is built in a throwaway bare repository
|
|
9
|
+
// that borrows the host's objects through `objects/info/alternates`, so not
|
|
10
|
+
// even a temporary ref is written to the host.
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
13
|
+
import { tmpdir } from "node:os";
|
|
14
|
+
import { join, posix } from "node:path";
|
|
15
|
+
import { hostEnv, runCommand } from "./host-command.js";
|
|
16
|
+
import { runBranch } from "./naming.js";
|
|
17
|
+
import { shellQuote } from "./run-sandbox.js";
|
|
18
|
+
/** Where the bundle lands inside the sandbox, relative to its home. */
|
|
19
|
+
export const SANDBOX_BUNDLE = ".culture-agents/run.bundle";
|
|
20
|
+
export const DEFAULT_WORKDIR = "work";
|
|
21
|
+
/**
|
|
22
|
+
* Options for the ssh that `collect`'s fetch runs, on top of OpenShell's generated config. The
|
|
23
|
+
* sandbox's sshd is not trusted: the operator's keys and agent are never offered or forwarded to
|
|
24
|
+
* it, nothing prompts, and host keys go to a throwaway file (`UserKnownHostsFile`, added per call
|
|
25
|
+
* inside the temp dir) instead of ~/.ssh/known_hosts. `IdentitiesOnly` alone still tries the
|
|
26
|
+
* default ~/.ssh/id_* files when the config names no key (`ssh -G` shows them), so
|
|
27
|
+
* `IdentityFile=none` drops those defaults; a key the generated config names is still used.
|
|
28
|
+
* `StrictHostKeyChecking` is deliberately absent: the generated config decides it.
|
|
29
|
+
*/
|
|
30
|
+
export const FETCH_SSH_OPTIONS = ["BatchMode=yes", "IdentitiesOnly=yes", "IdentityFile=none", "IdentityAgent=none", "ForwardAgent=no"];
|
|
31
|
+
/** Name of the throwaway known-hosts file inside the fetch's temp dir. */
|
|
32
|
+
export const FETCH_KNOWN_HOSTS = "known_hosts";
|
|
33
|
+
/** The `GIT_SSH_COMMAND` for a fetch: the generated config plus FETCH_SSH_OPTIONS. */
|
|
34
|
+
export function fetchSshCommand(configPath, knownHostsPath) {
|
|
35
|
+
const options = [...FETCH_SSH_OPTIONS, `UserKnownHostsFile=${knownHostsPath}`];
|
|
36
|
+
return ["ssh", "-F", shellQuote(configPath), ...options.flatMap((o) => ["-o", shellQuote(o)])].join(" ");
|
|
37
|
+
}
|
|
38
|
+
export class HandoffError extends Error {
|
|
39
|
+
name = "HandoffError";
|
|
40
|
+
}
|
|
41
|
+
async function git(runner, cwd, args, env) {
|
|
42
|
+
const res = await runner("git", args, { cwd, env: env ?? hostEnv() });
|
|
43
|
+
if (res.code !== 0) {
|
|
44
|
+
throw new HandoffError(`git ${args.join(" ")} failed (exit ${res.code}): ${res.stderr.trim()}`);
|
|
45
|
+
}
|
|
46
|
+
return res.stdout.trim();
|
|
47
|
+
}
|
|
48
|
+
/** Bundle `ref` of the host repo, upload it, and clone it as `runs/<id>` inside the sandbox. */
|
|
49
|
+
export async function prepare(sandbox, options) {
|
|
50
|
+
const runner = options.runner ?? runCommand;
|
|
51
|
+
const branch = runBranch(options.runId);
|
|
52
|
+
const host = options.hostRepo;
|
|
53
|
+
options.signal?.throwIfAborted();
|
|
54
|
+
const base = await git(runner, host, [
|
|
55
|
+
"rev-parse",
|
|
56
|
+
"--verify",
|
|
57
|
+
"--quiet",
|
|
58
|
+
"--end-of-options",
|
|
59
|
+
`${options.ref}^{commit}`,
|
|
60
|
+
]);
|
|
61
|
+
const commonDir = await git(runner, host, ["rev-parse", "--path-format=absolute", "--git-common-dir"]);
|
|
62
|
+
const stage = await mkdtemp(join(tmpdir(), "culture-agents-bundle-"));
|
|
63
|
+
try {
|
|
64
|
+
const bare = join(stage, "stage.git");
|
|
65
|
+
const bundlePath = join(stage, "run.bundle");
|
|
66
|
+
await git(runner, stage, ["init", "--bare", "--quiet", bare]);
|
|
67
|
+
await writeFile(join(bare, "objects", "info", "alternates"), `${join(commonDir, "objects")}\n`);
|
|
68
|
+
await git(runner, bare, ["update-ref", `refs/heads/${branch}`, base]);
|
|
69
|
+
await git(runner, bare, ["bundle", "create", bundlePath, `refs/heads/${branch}`]);
|
|
70
|
+
const bytes = await readFile(bundlePath);
|
|
71
|
+
const digest = createHash("sha256").update(bytes).digest("hex");
|
|
72
|
+
options.signal?.throwIfAborted();
|
|
73
|
+
await sandbox.writeFile(SANDBOX_BUNDLE, bytes, options.signal ? { signal: options.signal } : {});
|
|
74
|
+
const path = posix.join(sandbox.home, options.workdir ?? DEFAULT_WORKDIR);
|
|
75
|
+
const b = shellQuote(posix.join(sandbox.home, SANDBOX_BUNDLE));
|
|
76
|
+
const w = shellQuote(path);
|
|
77
|
+
const identity = options.identity
|
|
78
|
+
? `git config user.name ${shellQuote(options.identity.name)} && ` +
|
|
79
|
+
`git config user.email ${shellQuote(options.identity.email)} && `
|
|
80
|
+
: "";
|
|
81
|
+
// A bundle made from a branch carries no HEAD, so the clone names the branch.
|
|
82
|
+
const checksumLine = `${digest} ${posix.join(sandbox.home, SANDBOX_BUNDLE)}`;
|
|
83
|
+
const script = `set -e; echo ${shellQuote(checksumLine)} | sha256sum -c --quiet; ` +
|
|
84
|
+
`rm -rf ${w}; git clone --quiet -b ${shellQuote(branch)} ${b} ${w}; cd ${w}; ` +
|
|
85
|
+
`git remote remove origin; ${identity}rm -f ${b}; git rev-parse HEAD`;
|
|
86
|
+
const res = await sandbox.exec(script, options.signal ? { signal: options.signal } : {});
|
|
87
|
+
const head = Buffer.from(res.stdout).toString("utf8").trim().split("\n").pop();
|
|
88
|
+
if (res.exitCode !== 0 || head !== base) {
|
|
89
|
+
throw new HandoffError(`clone in sandbox failed (exit ${res.exitCode}, head ${head ?? "?"}): ` +
|
|
90
|
+
Buffer.from(res.stderr).toString("utf8").trim());
|
|
91
|
+
}
|
|
92
|
+
return { branch, baseCommit: base, path };
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
await rm(stage, { recursive: true, force: true });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Parse the `Host` alias from `openshell sandbox ssh-config` output. */
|
|
99
|
+
export function sshHostAlias(config) {
|
|
100
|
+
const match = /^Host\s+(\S+)\s*$/m.exec(config);
|
|
101
|
+
if (!match?.[1])
|
|
102
|
+
throw new HandoffError("openshell ssh-config printed no Host entry");
|
|
103
|
+
return match[1];
|
|
104
|
+
}
|
|
105
|
+
/** Fetch `runs/<id>` from the sandbox over OpenShell's SSH into the host repo. */
|
|
106
|
+
export async function collect(sandbox, options) {
|
|
107
|
+
const runner = options.runner ?? runCommand;
|
|
108
|
+
const branch = runBranch(options.runId);
|
|
109
|
+
const host = options.hostRepo;
|
|
110
|
+
const target = `refs/heads/${branch}`;
|
|
111
|
+
const head = await runner("git", ["symbolic-ref", "--quiet", "HEAD"], { cwd: host, env: hostEnv() });
|
|
112
|
+
if (head.code === 0 && head.stdout.trim() === target) {
|
|
113
|
+
throw new HandoffError(`host repository has ${branch} checked out; refusing to fetch into it`);
|
|
114
|
+
}
|
|
115
|
+
const cli = options.openshellBin ?? "openshell";
|
|
116
|
+
const gw = options.gatewayName ? ["-g", options.gatewayName] : [];
|
|
117
|
+
const cfg = await runner(cli, [...gw, "sandbox", "ssh-config", sandbox.name], { env: hostEnv() });
|
|
118
|
+
if (cfg.code !== 0)
|
|
119
|
+
throw new HandoffError(`openshell sandbox ssh-config failed: ${cfg.stderr.trim()}`);
|
|
120
|
+
const alias = sshHostAlias(cfg.stdout);
|
|
121
|
+
const dir = await mkdtemp(join(tmpdir(), "culture-agents-ssh-"));
|
|
122
|
+
try {
|
|
123
|
+
const cfgPath = join(dir, "ssh_config");
|
|
124
|
+
await writeFile(cfgPath, cfg.stdout, { mode: 0o600 });
|
|
125
|
+
const remotePath = options.remotePath ?? posix.join(sandbox.home, DEFAULT_WORKDIR);
|
|
126
|
+
const env = hostEnv({ GIT_SSH_COMMAND: fetchSshCommand(cfgPath, join(dir, FETCH_KNOWN_HOSTS)), GIT_SSH_VARIANT: "ssh" });
|
|
127
|
+
// Non-forced refspec: a runs/<id> that has diverged is refused, not overwritten.
|
|
128
|
+
await git(runner, host, [
|
|
129
|
+
"-c", "gc.auto=0",
|
|
130
|
+
"-c", "maintenance.auto=false",
|
|
131
|
+
"-c", "protocol.allow=never",
|
|
132
|
+
"-c", "protocol.ssh.allow=always",
|
|
133
|
+
// The sandbox builds the pack: malformed objects are refused, not stored.
|
|
134
|
+
"-c", "fetch.fsckObjects=true",
|
|
135
|
+
"-c", "transfer.fsckObjects=true",
|
|
136
|
+
"fetch", "--no-tags", "--no-write-fetch-head", "--no-recurse-submodules",
|
|
137
|
+
"--end-of-options",
|
|
138
|
+
`ssh://${alias}${remotePath}`,
|
|
139
|
+
`${target}:${target}`,
|
|
140
|
+
], env);
|
|
141
|
+
const commit = await git(runner, host, ["rev-parse", "--verify", "--quiet", target]);
|
|
142
|
+
if (options.baseCommit) {
|
|
143
|
+
// The sandbox is not trusted: the branch it hands back must continue the commit the run started from.
|
|
144
|
+
const descends = await runner("git", ["merge-base", "--is-ancestor", options.baseCommit, target], { cwd: host, env: hostEnv() });
|
|
145
|
+
if (descends.code !== 0) {
|
|
146
|
+
await runner("git", ["update-ref", "-d", target, commit], { cwd: host, env: hostEnv() });
|
|
147
|
+
throw new HandoffError(`${branch} from the sandbox does not descend from the run's base commit ${options.baseCommit}; the branch was not kept`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const listed = options.baseCommit ? await git(runner, host, ["rev-list", "--reverse", `${options.baseCommit}..${target}`]) : "";
|
|
151
|
+
return { branch, commit, commits: listed.split("\n").filter(Boolean) };
|
|
152
|
+
}
|
|
153
|
+
finally {
|
|
154
|
+
await rm(dir, { recursive: true, force: true });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
//# sourceMappingURL=worktree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worktree.js","sourceRoot":"","sources":["../../../src/sandbox/worktree.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,8EAA8E;AAC9E,+DAA+D;AAC/D,EAAE;AACF,8EAA8E;AAC9E,4EAA4E;AAC5E,4EAA4E;AAC5E,4EAA4E;AAC5E,4EAA4E;AAC5E,+CAA+C;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACpE,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAExC,OAAO,EAAE,OAAO,EAAE,UAAU,EAAsB,MAAM,mBAAmB,CAAC;AAC5E,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAG9C,uEAAuE;AACvE,MAAM,CAAC,MAAM,cAAc,GAAG,4BAA4B,CAAC;AAC3D,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC;AAEtC;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,eAAe,EAAE,oBAAoB,EAAE,m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E,OAAO,EAAE,EAAE,CAAC,CAAC;YACjI,IAAI,QAAQ,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;gBACxB,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,CAAC,CAAC;gBACzF,MAAM,IAAI,YAAY,CAAC,GAAG,MAAM,iEAAiE,OAAO,CAAC,UAAU,2BAA2B,CAAC,CAAC;YAClJ,CAAC;QACH,CAAC;QACD,MAAM,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,UAAU,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,UAAU,KAAK,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAChI,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;IACzE,CAAC;YAAS,CAAC;QACT,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAClD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The reported state of a run, derived only from its on-disk event log and its worker's
|
|
3
|
+
* liveness (README: "The state of a run is derived only from its on-disk event log or its live
|
|
4
|
+
* worker"). The rule:
|
|
5
|
+
*
|
|
6
|
+
* - a terminal `run.state` is in the log: that state;
|
|
7
|
+
* - no terminal event and the worker is alive: the last recorded state;
|
|
8
|
+
* - no terminal event and no live worker: `unknown` (never `succeeded`). The log does not yet
|
|
9
|
+
* say so; `orphaned` tells the caller (the core, now the log's only writer) to append it.
|
|
10
|
+
*
|
|
11
|
+
* Pure: no I/O, the clock is a parameter.
|
|
12
|
+
*/
|
|
13
|
+
import { type Run, type RunEvent } from "../contract/index.js";
|
|
14
|
+
export interface DeriveInput {
|
|
15
|
+
/** The run record as stored (identity, task, owner, deadline, ...). Its state fields are ignored. */
|
|
16
|
+
record: Run;
|
|
17
|
+
/** The complete, validated event log. */
|
|
18
|
+
events: readonly RunEvent[];
|
|
19
|
+
/** Whether the run's recorded worker is alive now. */
|
|
20
|
+
alive: boolean;
|
|
21
|
+
/** Whether the caller can deliver steer and cancel to that worker right now. */
|
|
22
|
+
controllable: boolean;
|
|
23
|
+
/** Timestamp used for `ended_at`/`updated_at` of an orphaned run. */
|
|
24
|
+
now: string;
|
|
25
|
+
/** Why the worker is considered gone (used in the orphaned reason). */
|
|
26
|
+
goneReason?: string;
|
|
27
|
+
}
|
|
28
|
+
export interface DerivedRun {
|
|
29
|
+
run: Run;
|
|
30
|
+
/** True when the run reads `unknown` but the log has no `unknown` event yet. */
|
|
31
|
+
orphaned: boolean;
|
|
32
|
+
}
|
|
33
|
+
/** The reason recorded when a run without a terminal event has no live worker. */
|
|
34
|
+
export declare const ORPHANED_REASON = "worker exited without a terminal event";
|
|
35
|
+
/**
|
|
36
|
+
* Derive the reported run. Returns null for a run whose log has no event at all: its worker
|
|
37
|
+
* never wrote event 1, so the submit was never acknowledged and the run is not established.
|
|
38
|
+
*/
|
|
39
|
+
export declare function deriveRun(input: DeriveInput): DerivedRun | null;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The reported state of a run, derived only from its on-disk event log and its worker's
|
|
3
|
+
* liveness (README: "The state of a run is derived only from its on-disk event log or its live
|
|
4
|
+
* worker"). The rule:
|
|
5
|
+
*
|
|
6
|
+
* - a terminal `run.state` is in the log: that state;
|
|
7
|
+
* - no terminal event and the worker is alive: the last recorded state;
|
|
8
|
+
* - no terminal event and no live worker: `unknown` (never `succeeded`). The log does not yet
|
|
9
|
+
* say so; `orphaned` tells the caller (the core, now the log's only writer) to append it.
|
|
10
|
+
*
|
|
11
|
+
* Pure: no I/O, the clock is a parameter.
|
|
12
|
+
*/
|
|
13
|
+
import { assertValid, isTerminal } from "../contract/index.js";
|
|
14
|
+
/** The reason recorded when a run without a terminal event has no live worker. */
|
|
15
|
+
export const ORPHANED_REASON = "worker exited without a terminal event";
|
|
16
|
+
function recordedLifecycle(events) {
|
|
17
|
+
const seen = { state: null, reason: null, startedAt: null, endedAt: null };
|
|
18
|
+
for (const event of events) {
|
|
19
|
+
if (event.kind !== "run.state")
|
|
20
|
+
continue;
|
|
21
|
+
const data = event.data;
|
|
22
|
+
seen.state = data.to;
|
|
23
|
+
seen.reason = data.reason;
|
|
24
|
+
if (data.to === "running" && seen.startedAt === null)
|
|
25
|
+
seen.startedAt = event.time;
|
|
26
|
+
if (isTerminal(data.to))
|
|
27
|
+
seen.endedAt = event.time;
|
|
28
|
+
}
|
|
29
|
+
return seen;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Derive the reported run. Returns null for a run whose log has no event at all: its worker
|
|
33
|
+
* never wrote event 1, so the submit was never acknowledged and the run is not established.
|
|
34
|
+
*/
|
|
35
|
+
export function deriveRun(input) {
|
|
36
|
+
const { record, events } = input;
|
|
37
|
+
const last = events.at(-1);
|
|
38
|
+
if (last === undefined)
|
|
39
|
+
return null;
|
|
40
|
+
const recorded = recordedLifecycle(events);
|
|
41
|
+
const startedAt = recorded.startedAt;
|
|
42
|
+
let { state, reason, endedAt } = recorded;
|
|
43
|
+
if (state === null)
|
|
44
|
+
return null;
|
|
45
|
+
let updatedAt = last.time;
|
|
46
|
+
let orphaned = false;
|
|
47
|
+
if (!isTerminal(state) && !input.alive) {
|
|
48
|
+
orphaned = true;
|
|
49
|
+
state = "unknown";
|
|
50
|
+
reason = input.goneReason ? `${ORPHANED_REASON} (${input.goneReason})` : ORPHANED_REASON;
|
|
51
|
+
endedAt = input.now;
|
|
52
|
+
updatedAt = input.now;
|
|
53
|
+
}
|
|
54
|
+
const terminal = isTerminal(state);
|
|
55
|
+
const run = {
|
|
56
|
+
...record,
|
|
57
|
+
state,
|
|
58
|
+
state_reason: reason,
|
|
59
|
+
terminal,
|
|
60
|
+
updated_at: updatedAt,
|
|
61
|
+
started_at: startedAt,
|
|
62
|
+
ended_at: terminal ? endedAt : null,
|
|
63
|
+
cursor: last.cursor,
|
|
64
|
+
live_control: !terminal && input.alive && input.controllable,
|
|
65
|
+
};
|
|
66
|
+
return { run: assertValid("run", run), orphaned };
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=derive.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"derive.js","sourceRoot":"","sources":["../../../src/store/derive.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,WAAW,EAAE,UAAU,EAA0D,MAAM,sBAAsB,CAAC;AAuBvH,kFAAkF;AAClF,MAAM,CAAC,MAAM,eAAe,GAAG,wCAAwC,CAAC;AAUxE,SAAS,iBAAiB,CAAC,MAA2B;IACpD,MAAM,IAAI,GAAc,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IACtF,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC3B,IAAI,KAAK,CAAC,IAAI,KAAK,WAAW;YAAE,SAAS;QACzC,MAAM,IAAI,GAAG,KAAK,CAAC,IAAiB,CAAC;QACrC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,EAAE,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,IAAI,CAAC,EAAE,KAAK,SAAS,IAAI,IAAI,CAAC,SAAS,KAAK,IAAI;YAAE,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC;QAClF,IAAI,UAAU,CAAC,IAAI,CAAC,EAAE,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QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI;QACnC,MAAM,EAAE,IAAI,CAAC,MAAM;QACnB,YAAY,EAAE,CAAC,QAAQ,IAAI,KAAK,CAAC,KAAK,IAAI,KAAK,CAAC,YAAY;KAC7D,CAAC;IACF,OAAO,EAAE,GAAG,EAAE,WAAW,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,QAAQ,EAAE,CAAC;AACpD,CAAC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { type ActionRecord, type ControlData, type ErrorData, type EventKind, type OutputData, type RefusedData, type RunEvent, type RunId, type RunState, type StateData } from "../contract/index.js";
|
|
2
|
+
/** The event log breaks a contract rule somewhere other than a torn final line. */
|
|
3
|
+
export declare class EventLogCorruptError extends Error {
|
|
4
|
+
readonly line: number;
|
|
5
|
+
constructor(line: number, detail: string);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* An event without its envelope bookkeeping; the writer adds run_id, cursor and time. Loosely
|
|
9
|
+
* typed on purpose: `append` validates the whole event against `event.json` before writing.
|
|
10
|
+
*/
|
|
11
|
+
export interface EventInput {
|
|
12
|
+
kind: EventKind;
|
|
13
|
+
data: StateData | OutputData | ControlData | RefusedData | ErrorData | {
|
|
14
|
+
action: ActionRecord;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/** Running summary of a log, enough to validate the next event. */
|
|
18
|
+
export interface LogTail {
|
|
19
|
+
cursor: number;
|
|
20
|
+
state: RunState | null;
|
|
21
|
+
}
|
|
22
|
+
/** Check one parsed event against the previous tail; returns the new tail. */
|
|
23
|
+
export declare function checkNext(runId: RunId, tail: LogTail, value: unknown, line: number): {
|
|
24
|
+
event: RunEvent;
|
|
25
|
+
tail: LogTail;
|
|
26
|
+
};
|
|
27
|
+
/** Result of parsing a log's bytes. */
|
|
28
|
+
export interface ParsedLog {
|
|
29
|
+
events: RunEvent[];
|
|
30
|
+
tail: LogTail;
|
|
31
|
+
/** Bytes of complete lines; anything after this offset is a torn append. */
|
|
32
|
+
completeBytes: number;
|
|
33
|
+
torn: boolean;
|
|
34
|
+
}
|
|
35
|
+
/** Parse log content. Only newline-terminated lines count; a trailing partial line is reported as torn. */
|
|
36
|
+
export declare function parseLog(runId: RunId, content: Buffer | string): ParsedLog;
|
|
37
|
+
/** Read and parse a run's whole log (missing file = empty log). */
|
|
38
|
+
export declare function readLog(dir: string, runId: RunId): ParsedLog;
|
|
39
|
+
/** Read the cursor file; null when absent or unreadable. */
|
|
40
|
+
export declare function readCursorFile(dir: string): number | null;
|
|
41
|
+
/**
|
|
42
|
+
* The single writer of a run's log. Opening it validates the existing log and drops a torn
|
|
43
|
+
* final line. Appends are synchronous, so a crash leaves at most one torn line. `durable` appends
|
|
44
|
+
* are fsynced together with the cursor file.
|
|
45
|
+
*
|
|
46
|
+
* A line counts only once every byte of it, newline included, is in the file. An append that fails
|
|
47
|
+
* before that (a short write followed by ENOSPC, for example) leaves at most a partial line with
|
|
48
|
+
* no newline, which readers already ignore as a torn tail; the writer truncates it, at once when
|
|
49
|
+
* it can and otherwise before its next append, so a later line never joins onto it.
|
|
50
|
+
*/
|
|
51
|
+
export declare class EventLogWriter {
|
|
52
|
+
private readonly dir;
|
|
53
|
+
readonly runId: RunId;
|
|
54
|
+
private readonly clock;
|
|
55
|
+
private readonly fd;
|
|
56
|
+
private tail;
|
|
57
|
+
/** Bytes of complete lines: where the next line starts. */
|
|
58
|
+
private bytes;
|
|
59
|
+
/** True when a failed append may have left bytes past `bytes`. */
|
|
60
|
+
private torn;
|
|
61
|
+
private constructor();
|
|
62
|
+
static open(dir: string, runId: RunId, clock?: () => number): EventLogWriter;
|
|
63
|
+
get cursor(): number;
|
|
64
|
+
get state(): RunState | null;
|
|
65
|
+
/** Drop what a failed append left after the last complete line. Throws when it cannot. */
|
|
66
|
+
private repair;
|
|
67
|
+
/** Write every byte of `line` (a short write is continued); on failure the partial line is dropped. */
|
|
68
|
+
private writeLine;
|
|
69
|
+
/**
|
|
70
|
+
* Append one event at the next cursor; throws (and writes nothing) if it breaks the contract.
|
|
71
|
+
* When the write itself fails, nothing is appended and the cursor does not advance. Once the
|
|
72
|
+
* whole line is in the file the cursor advances, before the fsync: if the fsync or the cursor
|
|
73
|
+
* file then fails, this throws but the event stays in the log and its cursor is never reused.
|
|
74
|
+
*/
|
|
75
|
+
append(input: EventInput, durable?: boolean): RunEvent;
|
|
76
|
+
/** Append a `run.state` transition from the current state. */
|
|
77
|
+
transition(to: RunState, reason: string | null, durable?: boolean): RunEvent;
|
|
78
|
+
close(): void;
|
|
79
|
+
}
|