|
1 | | -import { createHash } from "node:crypto"; |
2 | | -import { stableStringify } from "@openclaw/normalization-core"; |
3 | | -import { isRecord } from "@openclaw/normalization-core/record-coerce"; |
4 | 1 | import { |
5 | | - parseWorkerLaunchDescriptor, |
6 | | - type WorkerLaunchDescriptor, |
7 | | -} from "../worker/launch-descriptor.js"; |
| 2 | + parseNodeWorkerSupervisorReceipt, |
| 3 | + type NodeWorkerLaunchInput, |
| 4 | + type NodeWorkerSupervisorIdentity, |
| 5 | + type NodeWorkerSupervisorReceipt, |
| 6 | +} from "../worker/node-supervisor-protocol.js"; |
8 | 7 | import type { NodeWorkerLaunchReceipt } from "./node-worker-launch-store.js"; |
9 | | -import type { NodeWorkerSupervisorIdentity } from "./node-worker-supervisor-identity.js"; |
10 | 8 |
|
11 | | -export type { NodeWorkerSupervisorIdentity } from "./node-worker-supervisor-identity.js"; |
12 | | - |
13 | | -const IDENTIFIER_MAX_CHARS = 256; |
14 | | -const GATEWAY_NAMESPACE_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/u; |
15 | | -const NODE_WORKER_SUPERVISOR_CANCEL_REQUEST_MAX_BYTES = 4 * 1024; |
16 | | - |
17 | | -export type NodeWorkerLaunchInput = { |
18 | | - launchId: string; |
19 | | - gatewayNamespace: string; |
20 | | - bundleHash: string; |
21 | | - placementGeneration: number; |
22 | | - descriptor: WorkerLaunchDescriptor; |
23 | | -}; |
24 | | - |
25 | | -export type NodeWorkerSupervisorReceipt = NodeWorkerSupervisorIdentity & { |
26 | | - state: "pending" | "running" | "completed" | "failed" | "interrupted" | "cancelled"; |
27 | | -}; |
| 9 | +export { |
| 10 | + nodeWorkerPlanHash, |
| 11 | + parseNodeWorkerCancelInput, |
| 12 | + parseNodeWorkerLaunchInput, |
| 13 | + parseNodeWorkerLookupInput, |
| 14 | +} from "../worker/node-supervisor-protocol.js"; |
| 15 | +export type { |
| 16 | + NodeWorkerLaunchInput, |
| 17 | + NodeWorkerSupervisorIdentity, |
| 18 | + NodeWorkerSupervisorReceipt, |
| 19 | +} from "../worker/node-supervisor-protocol.js"; |
28 | 20 |
|
29 | 21 | export type NodeWorkerSupervisorControl = { |
30 | 22 | launch(input: NodeWorkerLaunchInput): Promise<NodeWorkerLaunchReceipt>; |
31 | 23 | status(launchId: string): Promise<NodeWorkerLaunchReceipt | undefined>; |
32 | 24 | cancel(expected: NodeWorkerSupervisorIdentity): Promise<NodeWorkerLaunchReceipt | undefined>; |
33 | 25 | }; |
34 | 26 |
|
35 | | -function hasExactKeys(value: Record<string, unknown>, keys: readonly string[]): boolean { |
36 | | - return ( |
37 | | - Object.keys(value).length === keys.length && keys.every((key) => Object.hasOwn(value, key)) |
38 | | - ); |
39 | | -} |
40 | | - |
41 | | -function requireIdentifier(value: unknown, label: string): string { |
42 | | - if ( |
43 | | - typeof value !== "string" || |
44 | | - value.length === 0 || |
45 | | - value.length > IDENTIFIER_MAX_CHARS || |
46 | | - value.trim() !== value || |
47 | | - value.includes("\0") |
48 | | - ) { |
49 | | - throw new Error(`INVALID_REQUEST: ${label} must be a bounded non-empty identifier`); |
50 | | - } |
51 | | - return value; |
52 | | -} |
53 | | - |
54 | | -function requireNonNegativeInteger(value: unknown, label: string): number { |
55 | | - if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) { |
56 | | - throw new Error(`INVALID_REQUEST: ${label} must be a non-negative safe integer`); |
57 | | - } |
58 | | - return value; |
59 | | -} |
60 | | - |
61 | | -function decodeRequest(raw?: string | null): unknown { |
62 | | - if (!raw) { |
63 | | - throw new Error("INVALID_REQUEST: paramsJSON required"); |
64 | | - } |
65 | | - try { |
66 | | - return JSON.parse(raw) as unknown; |
67 | | - } catch { |
68 | | - throw new Error("INVALID_REQUEST: paramsJSON malformed JSON"); |
69 | | - } |
70 | | -} |
71 | | - |
72 | | -export function parseNodeWorkerLaunchInput(raw?: string | null): NodeWorkerLaunchInput { |
73 | | - const value = decodeRequest(raw); |
74 | | - if ( |
75 | | - !isRecord(value) || |
76 | | - !hasExactKeys(value, [ |
77 | | - "launchId", |
78 | | - "gatewayNamespace", |
79 | | - "bundleHash", |
80 | | - "placementGeneration", |
81 | | - "descriptor", |
82 | | - ]) |
83 | | - ) { |
84 | | - throw new Error("INVALID_REQUEST: invalid node worker launch request"); |
85 | | - } |
86 | | - const launchId = requireIdentifier(value.launchId, "launchId"); |
87 | | - const gatewayNamespace = requireIdentifier(value.gatewayNamespace, "gatewayNamespace"); |
88 | | - if (!GATEWAY_NAMESPACE_PATTERN.test(gatewayNamespace)) { |
89 | | - throw new Error("INVALID_REQUEST: gatewayNamespace must be a safe bounded path component"); |
90 | | - } |
91 | | - if (typeof value.bundleHash !== "string" || !/^[a-f0-9]{64}$/u.test(value.bundleHash)) { |
92 | | - throw new Error("INVALID_REQUEST: bundleHash must be 64 lowercase hexadecimal characters"); |
93 | | - } |
94 | | - let descriptor: WorkerLaunchDescriptor; |
95 | | - try { |
96 | | - descriptor = parseWorkerLaunchDescriptor(value.descriptor); |
97 | | - } catch { |
98 | | - throw new Error("INVALID_REQUEST: invalid worker launch descriptor"); |
99 | | - } |
100 | | - if (descriptor.admission.handshake.bundleHash !== value.bundleHash) { |
101 | | - throw new Error("INVALID_REQUEST: descriptor bundle hash does not match bundleHash"); |
102 | | - } |
103 | | - return { |
104 | | - launchId, |
105 | | - gatewayNamespace, |
106 | | - bundleHash: value.bundleHash, |
107 | | - placementGeneration: requireNonNegativeInteger( |
108 | | - value.placementGeneration, |
109 | | - "placementGeneration", |
110 | | - ), |
111 | | - descriptor, |
112 | | - }; |
113 | | -} |
114 | | - |
115 | | -export function parseNodeWorkerLookupInput(raw?: string | null): { launchId: string } { |
116 | | - const value = decodeRequest(raw); |
117 | | - if (!isRecord(value) || !hasExactKeys(value, ["launchId"])) { |
118 | | - throw new Error("INVALID_REQUEST: invalid node worker lookup request"); |
119 | | - } |
120 | | - return { launchId: requireIdentifier(value.launchId, "launchId") }; |
121 | | -} |
122 | | - |
123 | | -export function parseNodeWorkerCancelInput(raw?: string | null): NodeWorkerSupervisorIdentity { |
124 | | - if (!raw || Buffer.byteLength(raw, "utf8") > NODE_WORKER_SUPERVISOR_CANCEL_REQUEST_MAX_BYTES) { |
125 | | - throw new Error("INVALID_REQUEST: invalid node worker cancel request"); |
126 | | - } |
127 | | - const value = decodeRequest(raw); |
128 | | - if ( |
129 | | - !isRecord(value) || |
130 | | - !hasExactKeys(value, [ |
131 | | - "launchId", |
132 | | - "planHash", |
133 | | - "environmentId", |
134 | | - "sessionId", |
135 | | - "ownerEpoch", |
136 | | - "placementGeneration", |
137 | | - "runId", |
138 | | - ]) |
139 | | - ) { |
140 | | - throw new Error("INVALID_REQUEST: invalid node worker cancel request"); |
141 | | - } |
142 | | - if (typeof value.planHash !== "string" || !/^[a-f0-9]{64}$/u.test(value.planHash)) { |
143 | | - throw new Error("INVALID_REQUEST: planHash must be 64 lowercase hexadecimal characters"); |
144 | | - } |
145 | | - return { |
146 | | - launchId: requireIdentifier(value.launchId, "launchId"), |
147 | | - planHash: value.planHash, |
148 | | - environmentId: requireIdentifier(value.environmentId, "environmentId"), |
149 | | - sessionId: requireIdentifier(value.sessionId, "sessionId"), |
150 | | - ownerEpoch: requireNonNegativeInteger(value.ownerEpoch, "ownerEpoch"), |
151 | | - placementGeneration: requireNonNegativeInteger( |
152 | | - value.placementGeneration, |
153 | | - "placementGeneration", |
154 | | - ), |
155 | | - runId: requireIdentifier(value.runId, "runId"), |
156 | | - }; |
157 | | -} |
158 | | - |
159 | | -export function nodeWorkerPlanHash( |
160 | | - input: Pick< |
161 | | - NodeWorkerLaunchInput, |
162 | | - "bundleHash" | "descriptor" | "gatewayNamespace" | "placementGeneration" |
163 | | - >, |
164 | | -): string { |
165 | | - return createHash("sha256") |
166 | | - .update( |
167 | | - stableStringify({ |
168 | | - bundleHash: input.bundleHash, |
169 | | - descriptor: input.descriptor, |
170 | | - gatewayNamespace: input.gatewayNamespace, |
171 | | - placementGeneration: input.placementGeneration, |
172 | | - }), |
173 | | - ) |
174 | | - .digest("hex"); |
175 | | -} |
176 | | - |
177 | 27 | export function projectNodeWorkerSupervisorReceipt( |
178 | 28 | receipt: NodeWorkerLaunchReceipt, |
179 | 29 | ): NodeWorkerSupervisorReceipt { |
180 | | - return { |
| 30 | + const identity = { |
181 | 31 | launchId: receipt.launchId, |
182 | 32 | planHash: receipt.planHash, |
183 | 33 | environmentId: receipt.environmentId, |
184 | 34 | sessionId: receipt.sessionId, |
185 | 35 | ownerEpoch: receipt.ownerEpoch, |
186 | 36 | placementGeneration: receipt.placementGeneration, |
187 | 37 | runId: receipt.runId, |
188 | | - state: receipt.state, |
189 | 38 | }; |
| 39 | + const projected = |
| 40 | + receipt.state === "completed" |
| 41 | + ? { ...identity, state: receipt.state, resultJson: receipt.resultJson } |
| 42 | + : receipt.state === "failed" || |
| 43 | + receipt.state === "interrupted" || |
| 44 | + receipt.state === "cancelled" |
| 45 | + ? { ...identity, state: receipt.state, errorText: receipt.errorText } |
| 46 | + : { ...identity, state: receipt.state }; |
| 47 | + const parsed = parseNodeWorkerSupervisorReceipt(projected); |
| 48 | + if (!parsed) { |
| 49 | + throw new Error("node worker supervisor durable receipt is inconsistent"); |
| 50 | + } |
| 51 | + return parsed; |
190 | 52 | } |
0 commit comments