@indigoai-us/hq-cli 5.345.214 → 5.345.215-beta.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/CHANGELOG.md +29 -0
- package/dist/command-catalog.generated.d.ts +134 -7
- package/dist/command-catalog.generated.js +168 -7
- package/dist/commands/agents.d.ts +51 -1
- package/dist/commands/agents.js +154 -2
- package/dist/commands/bot-jobs.d.ts +2 -15
- package/dist/commands/bot-jobs.js +2 -94
- package/dist/commands/bot-runtime.d.ts +46 -5
- package/dist/commands/bot-runtime.js +228 -18
- package/dist/commands/bot.d.ts +16 -9
- package/dist/commands/bot.js +169 -51
- package/dist/commands/cloud.d.ts +66 -13
- package/dist/commands/cloud.js +120 -39
- package/dist/commands/company-restore.d.ts +23 -0
- package/dist/commands/company-restore.js +128 -0
- package/dist/commands/company.js +2 -0
- package/dist/commands/integrations-connect.d.ts +10 -3
- package/dist/commands/integrations-connect.js +87 -13
- package/dist/commands/lanes.js +26 -9
- package/dist/commands/sources.js +6 -0
- package/dist/commands/storage-offload.d.ts +12 -0
- package/dist/commands/storage-offload.js +41 -3
- package/dist/commands/sync-mode.js +5 -0
- package/dist/commands/whoami.js +4 -0
- package/dist/lib/bot/config.d.ts +1 -0
- package/dist/lib/bot/config.js +10 -0
- package/dist/lib/bot/eventbridge-cron.d.ts +22 -0
- package/dist/lib/bot/eventbridge-cron.js +104 -0
- package/dist/lib/bot/fleet-local.d.ts +33 -1
- package/dist/lib/bot/fleet-local.js +215 -31
- package/dist/lib/bot/fleet-python.d.ts +3 -0
- package/dist/lib/bot/fleet-python.js +34 -4
- package/dist/lib/bot/fleet-runtime-sources.d.ts +15 -0
- package/dist/lib/bot/fleet-runtime-sources.js +30 -0
- package/dist/lib/bot/legacy-provider-login.d.ts +8 -0
- package/dist/lib/bot/legacy-provider-login.js +19 -2
- package/dist/lib/bot/prompt.js +1 -1
- package/dist/lib/bot/readiness.d.ts +8 -0
- package/dist/lib/bot/readiness.js +17 -3
- package/dist/lib/bot/runtime-auth.d.ts +30 -0
- package/dist/lib/bot/runtime-auth.js +94 -3
- package/dist/lib/bot/status.d.ts +2 -0
- package/dist/lib/capacity/collectors/grok-refresh.d.ts +80 -0
- package/dist/lib/capacity/collectors/grok-refresh.js +189 -0
- package/dist/lib/capacity/collectors/grok.d.ts +5 -0
- package/dist/lib/capacity/collectors/grok.js +56 -5
- package/dist/lib/daemon/catalog.js +11 -1
- package/dist/lib/db/sqlite-binding.d.ts +1 -0
- package/dist/lib/db/sqlite-binding.js +1 -1
- package/dist/lib/delivery/artifacts.d.ts +99 -0
- package/dist/lib/delivery/artifacts.js +548 -0
- package/dist/lib/delivery/backup.d.ts +78 -0
- package/dist/lib/delivery/backup.js +343 -0
- package/dist/lib/delivery/cache.d.ts +29 -0
- package/dist/lib/delivery/cache.js +33 -0
- package/dist/lib/delivery/contracts.d.ts +142 -0
- package/dist/lib/delivery/contracts.js +289 -0
- package/dist/lib/delivery/credentials.d.ts +54 -0
- package/dist/lib/delivery/credentials.js +119 -0
- package/dist/lib/delivery/enrollment.d.ts +142 -0
- package/dist/lib/delivery/enrollment.js +382 -0
- package/dist/lib/delivery/identity.d.ts +25 -0
- package/dist/lib/delivery/identity.js +82 -0
- package/dist/lib/delivery/inbox.d.ts +116 -0
- package/dist/lib/delivery/inbox.js +260 -0
- package/dist/lib/delivery/migrations.d.ts +139 -0
- package/dist/lib/delivery/migrations.js +623 -0
- package/dist/lib/delivery/mqtt-adapter.d.ts +50 -0
- package/dist/lib/delivery/mqtt-adapter.js +242 -0
- package/dist/lib/delivery/mqtt.d.ts +61 -0
- package/dist/lib/delivery/mqtt.js +112 -0
- package/dist/lib/delivery/outbox.d.ts +95 -0
- package/dist/lib/delivery/outbox.js +187 -0
- package/dist/lib/delivery/provenance.d.ts +129 -0
- package/dist/lib/delivery/provenance.js +93 -0
- package/dist/lib/delivery/reconciler.d.ts +151 -0
- package/dist/lib/delivery/reconciler.js +492 -0
- package/dist/lib/delivery/restore.d.ts +35 -0
- package/dist/lib/delivery/restore.js +134 -0
- package/dist/lib/delivery/store.d.ts +161 -0
- package/dist/lib/delivery/store.js +940 -0
- package/dist/lib/delivery/workers/macos-job-helper.d.ts +103 -0
- package/dist/lib/delivery/workers/macos-job-helper.js +927 -0
- package/dist/lib/delivery/workers/macos.d.ts +176 -0
- package/dist/lib/delivery/workers/macos.js +901 -0
- package/dist/lib/delivery/workers/windows-boundary.d.ts +453 -0
- package/dist/lib/delivery/workers/windows-boundary.js +693 -0
- package/dist/lib/delivery/workers/windows-job-helper.d.ts +35 -0
- package/dist/lib/delivery/workers/windows-job-helper.js +264 -0
- package/dist/lib/delivery/workers/windows.d.ts +241 -0
- package/dist/lib/delivery/workers/windows.js +608 -0
- package/dist/lib/install/provider-wrappers.d.ts +2 -0
- package/dist/lib/install/provider-wrappers.js +68 -3
- package/dist/utils/cognito-callback-port.d.ts +14 -0
- package/dist/utils/cognito-callback-port.js +24 -0
- package/dist/utils/cognito-session.js +3 -3
- package/dist/utils/deleted-company-notice.d.ts +59 -0
- package/dist/utils/deleted-company-notice.js +156 -0
- package/dist/utils/login-browser.d.ts +1 -1
- package/dist/utils/login-browser.js +7 -5
- package/dist/utils/resident-package-preload.js +30 -15
- package/dist/utils/vault-api.js +7 -0
- package/package.json +7 -7
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run and contain jobs on a Windows worker (US-006).
|
|
3
|
+
*
|
|
4
|
+
* A job arrives as an ExecutionSpec: the command, the content-addressed
|
|
5
|
+
* inputs it may read, the exact environment it gets, the platform, toolchains
|
|
6
|
+
* and capabilities it needs, and its resource limits. WindowsWorker.start
|
|
7
|
+
* checks all of that before it claims the job or starts any process:
|
|
8
|
+
* 1. the spec is well formed (INVALID_SPEC),
|
|
9
|
+
* 2. the run and job exist for the spec's tenant (UNKNOWN_JOB, store codes),
|
|
10
|
+
* 3. the worker profile matches the requirements (PLATFORM_MISMATCH,
|
|
11
|
+
* TOOLCHAIN_MISMATCH, UNKNOWN_CAPABILITY, CAPABILITY_MISSING),
|
|
12
|
+
* 4. every input is admitted and unchanged (INPUT_MISSING, INPUT_CHANGED),
|
|
13
|
+
* 5. the launcher can contain the job (CONTAINMENT_UNAVAILABLE).
|
|
14
|
+
* A refusal claims nothing, so the job stays ready for a matching worker, and
|
|
15
|
+
* is recorded as exactly one terminal result.
|
|
16
|
+
*
|
|
17
|
+
* After the claim the inputs are copied read-only into a fresh attempt
|
|
18
|
+
* directory and the command runs there with only the declared environment.
|
|
19
|
+
* The terminal outcome is reported to the ledger through the access layer
|
|
20
|
+
* (passed or failed) and appended once to the result log. Nothing in the
|
|
21
|
+
* store is deleted. Results hold environment keys only, never values.
|
|
22
|
+
*
|
|
23
|
+
* Containment on Windows is the boundary service of proposal 039, reached
|
|
24
|
+
* through an injected probe and transport (windows-boundary.ts). There is no
|
|
25
|
+
* local compile and no same-user fallback: without a passing boundary the
|
|
26
|
+
* native launcher fails closed. This module imports only node built-ins and
|
|
27
|
+
* windows-boundary.ts at runtime so it can be bundled without node_modules.
|
|
28
|
+
*/
|
|
29
|
+
import { execFileSync } from "node:child_process";
|
|
30
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
31
|
+
import { chmodSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, writeFileSync, writeSync } from "node:fs";
|
|
32
|
+
import { isAbsolute, join } from "node:path";
|
|
33
|
+
import { evaluateBoundary } from "./windows-boundary.js";
|
|
34
|
+
export const EXECUTION_SPEC_VERSION = "hq-delivery.execution.v1";
|
|
35
|
+
export const WORKER_RESULT_VERSION = "hq-delivery.worker-result.v1";
|
|
36
|
+
/** What the native launcher contains. A job may require any of these. */
|
|
37
|
+
export const CONTAINMENT_CAPABILITIES = ["job-object-containment", "process-tree-kill", "memory-limit", "process-limit", "cpu-time-limit"];
|
|
38
|
+
export const KNOWN_CAPABILITIES = [...CONTAINMENT_CAPABILITIES, "posix-signals", "git"];
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Worker profile
|
|
41
|
+
let gitVersion;
|
|
42
|
+
function detectGit() {
|
|
43
|
+
if (gitVersion !== undefined)
|
|
44
|
+
return gitVersion;
|
|
45
|
+
try {
|
|
46
|
+
const out = execFileSync("git", ["--version"], { encoding: "utf8", timeout: 10_000, stdio: ["ignore", "pipe", "ignore"], windowsHide: true });
|
|
47
|
+
gitVersion = /(\d+\.\d+\.\d+)/.exec(out)?.[1] ?? null;
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
gitVersion = null;
|
|
51
|
+
}
|
|
52
|
+
return gitVersion;
|
|
53
|
+
}
|
|
54
|
+
let cachedProfile;
|
|
55
|
+
/** The profile of the host this process runs on. */
|
|
56
|
+
export function detectProfile() {
|
|
57
|
+
if (cachedProfile)
|
|
58
|
+
return cachedProfile;
|
|
59
|
+
const platform = process.platform === "win32" ? "windows" : process.platform === "darwin" ? "macos" : process.platform === "linux" ? "linux" : process.platform;
|
|
60
|
+
const toolchains = { node: process.versions.node };
|
|
61
|
+
const git = detectGit();
|
|
62
|
+
if (git)
|
|
63
|
+
toolchains.git = git;
|
|
64
|
+
const capabilities = platform === "windows" ? [...CONTAINMENT_CAPABILITIES] : ["posix-signals"];
|
|
65
|
+
if (git)
|
|
66
|
+
capabilities.push("git");
|
|
67
|
+
cachedProfile = { platform, arch: process.arch, toolchains, capabilities };
|
|
68
|
+
return cachedProfile;
|
|
69
|
+
}
|
|
70
|
+
const COMPARATOR = /^(>=|<=|>|<|=|\^|~)?v?(\d+|[xX*])(?:\.(\d+|[xX*]))?(?:\.(\d+|[xX*]))?(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/;
|
|
71
|
+
function cmp(a, b) {
|
|
72
|
+
return a[0] - b[0] || a[1] - b[1] || a[2] - b[2];
|
|
73
|
+
}
|
|
74
|
+
function parseVersion(text) {
|
|
75
|
+
const m = /^v?(\d+)(?:\.(\d+))?(?:\.(\d+))?/.exec(text.trim());
|
|
76
|
+
return m ? [Number(m[1]), Number(m[2] ?? 0), Number(m[3] ?? 0)] : null;
|
|
77
|
+
}
|
|
78
|
+
/** One comparator as lower and upper bounds. null means invalid; [] means any. */
|
|
79
|
+
function comparatorBounds(token) {
|
|
80
|
+
if (token === "*" || token.toLowerCase() === "x")
|
|
81
|
+
return [];
|
|
82
|
+
const m = COMPARATOR.exec(token);
|
|
83
|
+
if (!m)
|
|
84
|
+
return null;
|
|
85
|
+
const op = m[1] ?? "";
|
|
86
|
+
const raw = [m[2], m[3], m[4]];
|
|
87
|
+
const parts = [];
|
|
88
|
+
for (const p of raw) {
|
|
89
|
+
if (p === undefined || /^[xX*]$/.test(p))
|
|
90
|
+
break;
|
|
91
|
+
parts.push(Number(p));
|
|
92
|
+
}
|
|
93
|
+
if (parts.length === 0)
|
|
94
|
+
return [];
|
|
95
|
+
const full = parts.length === 3;
|
|
96
|
+
const [M, m1 = 0, p1 = 0] = parts;
|
|
97
|
+
const lo = [M, m1, p1];
|
|
98
|
+
const next = parts.length === 1 ? [M + 1, 0, 0] : parts.length === 2 ? [M, m1 + 1, 0] : [M, m1, p1 + 1];
|
|
99
|
+
switch (op) {
|
|
100
|
+
case "":
|
|
101
|
+
case "=":
|
|
102
|
+
return full ? [{ op: "=", v: lo }] : [{ op: ">=", v: lo }, { op: "<", v: next }];
|
|
103
|
+
case ">=":
|
|
104
|
+
return [{ op: ">=", v: lo }];
|
|
105
|
+
case ">":
|
|
106
|
+
return full ? [{ op: ">", v: lo }] : [{ op: ">=", v: next }];
|
|
107
|
+
case "<":
|
|
108
|
+
return [{ op: "<", v: lo }];
|
|
109
|
+
case "<=":
|
|
110
|
+
return full ? [{ op: "<=", v: lo }] : [{ op: "<", v: next }];
|
|
111
|
+
case "~":
|
|
112
|
+
return [{ op: ">=", v: lo }, { op: "<", v: parts.length === 1 ? [M + 1, 0, 0] : [M, m1 + 1, 0] }];
|
|
113
|
+
case "^": {
|
|
114
|
+
let hi;
|
|
115
|
+
if (M > 0 || parts.length === 1)
|
|
116
|
+
hi = [M + 1, 0, 0];
|
|
117
|
+
else if (m1 > 0 || parts.length === 2)
|
|
118
|
+
hi = [0, m1 + 1, 0];
|
|
119
|
+
else
|
|
120
|
+
hi = [0, 0, p1 + 1];
|
|
121
|
+
return [{ op: ">=", v: lo }, { op: "<", v: hi }];
|
|
122
|
+
}
|
|
123
|
+
default:
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/** A range as alternatives of bound sets, or null when it does not parse. */
|
|
128
|
+
function parseRange(range) {
|
|
129
|
+
const alternatives = range.split("||").map((s) => s.trim().replace(/(>=|<=|>|<|=|\^|~)\s+/g, "$1"));
|
|
130
|
+
const out = [];
|
|
131
|
+
for (const alt of alternatives) {
|
|
132
|
+
const bounds = [];
|
|
133
|
+
for (const token of alt === "" ? [] : alt.split(/\s+/)) {
|
|
134
|
+
const b = comparatorBounds(token);
|
|
135
|
+
if (!b)
|
|
136
|
+
return null;
|
|
137
|
+
bounds.push(...b);
|
|
138
|
+
}
|
|
139
|
+
out.push(bounds);
|
|
140
|
+
}
|
|
141
|
+
return out;
|
|
142
|
+
}
|
|
143
|
+
function satisfies(version, range) {
|
|
144
|
+
const v = parseVersion(version);
|
|
145
|
+
const alts = parseRange(range);
|
|
146
|
+
if (!v || !alts)
|
|
147
|
+
return false;
|
|
148
|
+
return alts.some((bounds) => bounds.every(({ op, v: b }) => {
|
|
149
|
+
const c = cmp(v, b);
|
|
150
|
+
return op === ">=" ? c >= 0 : op === ">" ? c > 0 : op === "<" ? c < 0 : op === "<=" ? c <= 0 : c === 0;
|
|
151
|
+
}));
|
|
152
|
+
}
|
|
153
|
+
// ---------------------------------------------------------------------------
|
|
154
|
+
// Matching and validation
|
|
155
|
+
/** Check a worker profile against a job's requirements. Platform first, then toolchains, then capabilities. */
|
|
156
|
+
export function matchCapabilities(profile, req) {
|
|
157
|
+
if (!req.platforms.includes(profile.platform)) {
|
|
158
|
+
return { ok: false, code: "PLATFORM_MISMATCH", detail: `job runs on ${req.platforms.join(", ")}; worker is ${profile.platform}` };
|
|
159
|
+
}
|
|
160
|
+
for (const [name, range] of Object.entries(req.toolchains ?? {})) {
|
|
161
|
+
const installed = profile.toolchains[name];
|
|
162
|
+
if (installed === undefined)
|
|
163
|
+
return { ok: false, code: "TOOLCHAIN_MISMATCH", detail: `job needs ${name} ${range}; worker has no ${name}` };
|
|
164
|
+
if (!satisfies(installed, range))
|
|
165
|
+
return { ok: false, code: "TOOLCHAIN_MISMATCH", detail: `job needs ${name} ${range}; worker has ${name} ${installed}` };
|
|
166
|
+
}
|
|
167
|
+
const caps = req.capabilities ?? [];
|
|
168
|
+
const unknown = caps.filter((c) => !KNOWN_CAPABILITIES.includes(c));
|
|
169
|
+
if (unknown.length > 0)
|
|
170
|
+
return { ok: false, code: "UNKNOWN_CAPABILITY", detail: `unknown capability ${unknown.join(", ")}` };
|
|
171
|
+
const missing = caps.filter((c) => !profile.capabilities.includes(c));
|
|
172
|
+
if (missing.length > 0)
|
|
173
|
+
return { ok: false, code: "CAPABILITY_MISSING", detail: `worker lacks ${missing.join(", ")}` };
|
|
174
|
+
return { ok: true };
|
|
175
|
+
}
|
|
176
|
+
const INPUT_NAME = /^[A-Za-z0-9._-]+$/;
|
|
177
|
+
const SHA256 = /^[0-9a-f]{64}$/;
|
|
178
|
+
function isRecord(x) {
|
|
179
|
+
return typeof x === "object" && x !== null && !Array.isArray(x);
|
|
180
|
+
}
|
|
181
|
+
function isStringArray(x) {
|
|
182
|
+
return Array.isArray(x) && x.every((s) => typeof s === "string");
|
|
183
|
+
}
|
|
184
|
+
function positiveInt(x) {
|
|
185
|
+
return typeof x === "number" && Number.isSafeInteger(x) && x > 0;
|
|
186
|
+
}
|
|
187
|
+
/** The first problem with a spec, or null when it is well formed. */
|
|
188
|
+
export function validateSpec(spec) {
|
|
189
|
+
const s = spec;
|
|
190
|
+
if (!isRecord(s))
|
|
191
|
+
return "spec is not an object";
|
|
192
|
+
if (s.schemaVersion !== EXECUTION_SPEC_VERSION)
|
|
193
|
+
return `schemaVersion must be ${EXECUTION_SPEC_VERSION}`;
|
|
194
|
+
for (const k of ["tenantId", "runId", "jobId"]) {
|
|
195
|
+
if (typeof s[k] !== "string" || s[k] === "")
|
|
196
|
+
return `${k} must be a non-empty string`;
|
|
197
|
+
}
|
|
198
|
+
const req = s.requires;
|
|
199
|
+
if (!isRecord(req))
|
|
200
|
+
return "requires must be an object";
|
|
201
|
+
if (!isStringArray(req.platforms) || req.platforms.length === 0)
|
|
202
|
+
return "requires.platforms must be a non-empty list of strings";
|
|
203
|
+
if (req.toolchains !== undefined) {
|
|
204
|
+
if (!isRecord(req.toolchains))
|
|
205
|
+
return "requires.toolchains must be an object";
|
|
206
|
+
for (const [name, range] of Object.entries(req.toolchains)) {
|
|
207
|
+
if (typeof range !== "string" || parseRange(range) === null)
|
|
208
|
+
return `requires.toolchains.${name} is not a valid version range`;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (req.capabilities !== undefined && !isStringArray(req.capabilities))
|
|
212
|
+
return "requires.capabilities must be a list of strings";
|
|
213
|
+
const cmd = s.command;
|
|
214
|
+
if (!isRecord(cmd) || typeof cmd.file !== "string" || !isAbsolute(cmd.file) || cmd.file.includes("\0"))
|
|
215
|
+
return "command.file must be an absolute path";
|
|
216
|
+
if (!isStringArray(cmd.args) || cmd.args.some((a) => a.includes("\0")))
|
|
217
|
+
return "command.args must be a list of strings without NUL";
|
|
218
|
+
if (!Array.isArray(s.inputs))
|
|
219
|
+
return "inputs must be a list";
|
|
220
|
+
const names = new Set();
|
|
221
|
+
for (const i of s.inputs) {
|
|
222
|
+
if (!isRecord(i) || typeof i.name !== "string" || typeof i.sha256 !== "string")
|
|
223
|
+
return "each input needs a name and a sha256";
|
|
224
|
+
if (!INPUT_NAME.test(i.name) || i.name === "." || i.name === "..")
|
|
225
|
+
return `input name ${i.name} is not a plain file name`;
|
|
226
|
+
if (names.has(i.name))
|
|
227
|
+
return `input name ${i.name} is repeated`;
|
|
228
|
+
if (!SHA256.test(i.sha256))
|
|
229
|
+
return `input ${i.name} sha256 must be 64 lowercase hex characters`;
|
|
230
|
+
names.add(i.name);
|
|
231
|
+
}
|
|
232
|
+
if (!isRecord(s.env))
|
|
233
|
+
return "env must be an object";
|
|
234
|
+
for (const [k, v] of Object.entries(s.env)) {
|
|
235
|
+
if (k === "" || k.includes("=") || k.includes("\0"))
|
|
236
|
+
return "env keys must be non-empty and contain no = or NUL";
|
|
237
|
+
if (typeof v !== "string" || v.includes("\0"))
|
|
238
|
+
return `env ${k} must be a string without NUL`;
|
|
239
|
+
}
|
|
240
|
+
const lim = s.limits;
|
|
241
|
+
if (!isRecord(lim) || !positiveInt(lim.memoryBytes) || !positiveInt(lim.maxProcesses) || !positiveInt(lim.cpuTimeMs)) {
|
|
242
|
+
return "limits.memoryBytes, limits.maxProcesses and limits.cpuTimeMs must be positive integers";
|
|
243
|
+
}
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
// ---------------------------------------------------------------------------
|
|
247
|
+
// Immutable inputs
|
|
248
|
+
function sha256(bytes) {
|
|
249
|
+
return createHash("sha256").update(bytes).digest("hex");
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Content-addressed input blobs. An admitted input is stored under its hash,
|
|
253
|
+
* read-only; a job names the hash, and the bytes are checked again before the
|
|
254
|
+
* claim and when they are copied into the attempt.
|
|
255
|
+
*/
|
|
256
|
+
export class InputStore {
|
|
257
|
+
dir;
|
|
258
|
+
constructor(dir) {
|
|
259
|
+
this.dir = dir;
|
|
260
|
+
}
|
|
261
|
+
static open(dir) {
|
|
262
|
+
mkdirSync(join(dir, "blobs"), { recursive: true });
|
|
263
|
+
return new InputStore(dir);
|
|
264
|
+
}
|
|
265
|
+
admit(name, path) {
|
|
266
|
+
if (!INPUT_NAME.test(name) || name === "." || name === "..")
|
|
267
|
+
throw new Error(`input name ${name} is not a plain file name`);
|
|
268
|
+
const bytes = readFileSync(path);
|
|
269
|
+
const hash = sha256(bytes);
|
|
270
|
+
const dest = this.blobPath(hash);
|
|
271
|
+
if (!existsSync(dest)) {
|
|
272
|
+
const tmp = `${dest}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
|
|
273
|
+
writeFileSync(tmp, bytes);
|
|
274
|
+
chmodSync(tmp, 0o444);
|
|
275
|
+
renameSync(tmp, dest);
|
|
276
|
+
}
|
|
277
|
+
return { name, sha256: hash };
|
|
278
|
+
}
|
|
279
|
+
blobPath(hash) {
|
|
280
|
+
return join(this.dir, "blobs", hash);
|
|
281
|
+
}
|
|
282
|
+
/** null when the blob is present and unchanged. */
|
|
283
|
+
verify(ref) {
|
|
284
|
+
const path = this.blobPath(ref.sha256);
|
|
285
|
+
if (!existsSync(path))
|
|
286
|
+
return "INPUT_MISSING";
|
|
287
|
+
return sha256(readFileSync(path)) === ref.sha256 ? null : "INPUT_CHANGED";
|
|
288
|
+
}
|
|
289
|
+
/** Copy a verified blob to destDir/name, read-only. */
|
|
290
|
+
materialize(ref, destDir) {
|
|
291
|
+
const path = this.blobPath(ref.sha256);
|
|
292
|
+
if (!existsSync(path))
|
|
293
|
+
throw new WorkerRefusal("INPUT_MISSING", `input ${ref.name} is not admitted`);
|
|
294
|
+
const bytes = readFileSync(path);
|
|
295
|
+
if (sha256(bytes) !== ref.sha256)
|
|
296
|
+
throw new WorkerRefusal("INPUT_CHANGED", `input ${ref.name} changed after admission`);
|
|
297
|
+
const dest = join(destDir, ref.name);
|
|
298
|
+
writeFileSync(dest, bytes);
|
|
299
|
+
chmodSync(dest, 0o444);
|
|
300
|
+
return dest;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
/** Append-only JSON lines of terminal results. One result per attempt. */
|
|
304
|
+
export class ResultLog {
|
|
305
|
+
path;
|
|
306
|
+
lines;
|
|
307
|
+
constructor(path) {
|
|
308
|
+
this.path = path;
|
|
309
|
+
this.lines = existsSync(path)
|
|
310
|
+
? readFileSync(path, "utf8")
|
|
311
|
+
.split("\n")
|
|
312
|
+
.filter((l) => l.trim() !== "")
|
|
313
|
+
.map((l) => JSON.parse(l))
|
|
314
|
+
: [];
|
|
315
|
+
}
|
|
316
|
+
static open(path) {
|
|
317
|
+
return new ResultLog(path);
|
|
318
|
+
}
|
|
319
|
+
/** Append a result. A second result for the same attempt is not written; the first is returned. */
|
|
320
|
+
append(result) {
|
|
321
|
+
if (result.attemptId !== null) {
|
|
322
|
+
const existing = this.lines.find((r) => r.attemptId === result.attemptId);
|
|
323
|
+
if (existing)
|
|
324
|
+
return existing;
|
|
325
|
+
}
|
|
326
|
+
const line = JSON.stringify(result);
|
|
327
|
+
const fd = openSync(this.path, "a");
|
|
328
|
+
try {
|
|
329
|
+
writeSync(fd, line + "\n");
|
|
330
|
+
fsyncSync(fd);
|
|
331
|
+
}
|
|
332
|
+
finally {
|
|
333
|
+
closeSync(fd);
|
|
334
|
+
}
|
|
335
|
+
const copy = JSON.parse(line);
|
|
336
|
+
this.lines.push(copy);
|
|
337
|
+
return copy;
|
|
338
|
+
}
|
|
339
|
+
all() {
|
|
340
|
+
return this.lines.map((r) => ({ ...r, envKeys: [...r.envKeys] }));
|
|
341
|
+
}
|
|
342
|
+
forJob(jobId) {
|
|
343
|
+
return this.all().filter((r) => r.jobId === jobId);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
export class WorkerRefusal extends Error {
|
|
347
|
+
code;
|
|
348
|
+
detail;
|
|
349
|
+
constructor(code, detail) {
|
|
350
|
+
super(`${code}: ${detail}`);
|
|
351
|
+
this.code = code;
|
|
352
|
+
this.detail = detail;
|
|
353
|
+
this.name = "WorkerRefusal";
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/** Quote one argument for a Windows command line, following the CommandLineToArgvW rules. */
|
|
357
|
+
export function quoteWindowsArg(arg) {
|
|
358
|
+
if (arg !== "" && !/[\s"]/.test(arg))
|
|
359
|
+
return arg;
|
|
360
|
+
let out = '"';
|
|
361
|
+
let slashes = 0;
|
|
362
|
+
for (const ch of arg) {
|
|
363
|
+
if (ch === "\\") {
|
|
364
|
+
slashes += 1;
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
out += ch === '"' ? "\\".repeat(slashes * 2 + 1) + '"' : "\\".repeat(slashes) + ch;
|
|
368
|
+
slashes = 0;
|
|
369
|
+
}
|
|
370
|
+
return out + "\\".repeat(slashes * 2) + '"';
|
|
371
|
+
}
|
|
372
|
+
const LIMIT_REASONS = { memory_limit: "memory", process_limit: "processes", cpu_limit: "cpu" };
|
|
373
|
+
/**
|
|
374
|
+
* Map what the job helper reported to a launch outcome. An error line before
|
|
375
|
+
* any "started" line (for example "error resume <code>", sent after the helper
|
|
376
|
+
* has terminated the job) is a spawn failure.
|
|
377
|
+
*/
|
|
378
|
+
export function helperOutcome(h) {
|
|
379
|
+
if (h.exitLine) {
|
|
380
|
+
const [, why, rawCode] = h.exitLine.split(" ");
|
|
381
|
+
const exitCode = Number(rawCode);
|
|
382
|
+
if (why === "exited")
|
|
383
|
+
return { reason: "exited", exitCode };
|
|
384
|
+
if (why === "cancelled")
|
|
385
|
+
return { reason: "cancelled", exitCode };
|
|
386
|
+
if (LIMIT_REASONS[why])
|
|
387
|
+
return { reason: "limit", limit: LIMIT_REASONS[why], exitCode };
|
|
388
|
+
return { reason: "supervisor_lost", detail: `job helper reported ${h.exitLine}` };
|
|
389
|
+
}
|
|
390
|
+
if (h.rootPid < 0) {
|
|
391
|
+
return { reason: "spawn_failed", detail: h.errorLine ?? h.spawnError ?? `job helper exited ${h.code} before starting the job${h.stderr ? `: ${h.stderr.trim().split("\n")[0]}` : ""}` };
|
|
392
|
+
}
|
|
393
|
+
return { reason: "supervisor_lost", detail: `job helper exited ${h.code} without a result` };
|
|
394
|
+
}
|
|
395
|
+
function unavailable(detail, reason) {
|
|
396
|
+
return new WorkerRefusal("CONTAINMENT_UNAVAILABLE", `${detail}: ${reason}`);
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* The native Windows launcher (proposal 039). A job runs only through the
|
|
400
|
+
* boundary service: the probe must report all seven predicates as passing,
|
|
401
|
+
* fresh and for this installation, and the job goes to the service over the
|
|
402
|
+
* transport. There is no local compile and no same-user fallback; any missing
|
|
403
|
+
* piece is CONTAINMENT_UNAVAILABLE before the claim.
|
|
404
|
+
*/
|
|
405
|
+
export function nativeWindowsLauncher(opts = {}) {
|
|
406
|
+
const platform = opts.platform ?? process.platform;
|
|
407
|
+
// One launch per admission. A worker passes the run to launch; a caller that
|
|
408
|
+
// does not gets the most recent admission.
|
|
409
|
+
const admitted = new Map();
|
|
410
|
+
let latest;
|
|
411
|
+
const key = (run) => JSON.stringify([run.tenantId, run.runId, run.jobId]);
|
|
412
|
+
const check = (run) => {
|
|
413
|
+
if (platform !== "win32")
|
|
414
|
+
throw unavailable("BOUNDARY_SERVICE_UNAVAILABLE", `native Windows containment needs Windows; this host is ${platform}`);
|
|
415
|
+
if (!run)
|
|
416
|
+
throw unavailable("BOUNDARY_RUN_SETUP", "no admitted run");
|
|
417
|
+
const { probe, context, transport } = opts;
|
|
418
|
+
if (!probe || !context)
|
|
419
|
+
throw unavailable("BOUNDARY_SERVICE_UNAVAILABLE", "no boundary service is configured");
|
|
420
|
+
let results;
|
|
421
|
+
try {
|
|
422
|
+
results = probe.probe({ tenantId: run.tenantId, runId: run.runId, jobId: run.jobId });
|
|
423
|
+
}
|
|
424
|
+
catch (err) {
|
|
425
|
+
throw unavailable("BOUNDARY_SERVICE_UNAVAILABLE", `the boundary probe failed: ${err.message.split("\n")[0]}`);
|
|
426
|
+
}
|
|
427
|
+
const verdict = evaluateBoundary(results, context);
|
|
428
|
+
if (!verdict.ok)
|
|
429
|
+
throw unavailable(verdict.detail, verdict.reason);
|
|
430
|
+
if (!transport)
|
|
431
|
+
throw unavailable("BOUNDARY_CHANNEL_IDENTITY", "no authenticated service channel");
|
|
432
|
+
return { run, transport };
|
|
433
|
+
};
|
|
434
|
+
return {
|
|
435
|
+
prepare(run) {
|
|
436
|
+
latest = undefined;
|
|
437
|
+
const ok = check(run).run;
|
|
438
|
+
latest = key(ok);
|
|
439
|
+
admitted.set(latest, { tenantId: ok.tenantId, runId: ok.runId, jobId: ok.jobId });
|
|
440
|
+
},
|
|
441
|
+
launch(req, run) {
|
|
442
|
+
const k = run ? key(run) : latest;
|
|
443
|
+
const entry = k === undefined ? undefined : admitted.get(k);
|
|
444
|
+
if (k !== undefined)
|
|
445
|
+
admitted.delete(k);
|
|
446
|
+
if (k === latest)
|
|
447
|
+
latest = undefined;
|
|
448
|
+
const checked = check(entry);
|
|
449
|
+
return checked.transport.submit(req, checked.run);
|
|
450
|
+
},
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
/** A store error carries a string code; checked by shape so no runtime import is needed. */
|
|
454
|
+
function storeCode(err) {
|
|
455
|
+
const code = err?.code;
|
|
456
|
+
return err instanceof Error && typeof code === "string" ? code : null;
|
|
457
|
+
}
|
|
458
|
+
function message(err) {
|
|
459
|
+
return (err instanceof Error ? err.message : String(err)).split("\n")[0];
|
|
460
|
+
}
|
|
461
|
+
export class WindowsWorker {
|
|
462
|
+
opts;
|
|
463
|
+
clock;
|
|
464
|
+
constructor(opts) {
|
|
465
|
+
this.opts = opts;
|
|
466
|
+
this.clock = opts.clock ?? (() => new Date());
|
|
467
|
+
}
|
|
468
|
+
now() {
|
|
469
|
+
return this.clock().toISOString();
|
|
470
|
+
}
|
|
471
|
+
result(spec, fields) {
|
|
472
|
+
const env = isRecord(spec?.env) ? spec.env : {};
|
|
473
|
+
return {
|
|
474
|
+
schemaVersion: WORKER_RESULT_VERSION,
|
|
475
|
+
tenantId: String(spec?.tenantId ?? ""),
|
|
476
|
+
runId: String(spec?.runId ?? ""),
|
|
477
|
+
jobId: String(spec?.jobId ?? ""),
|
|
478
|
+
attemptId: null,
|
|
479
|
+
pid: null,
|
|
480
|
+
exitCode: null,
|
|
481
|
+
envKeys: Object.keys(env).sort(),
|
|
482
|
+
startedAt: null,
|
|
483
|
+
finishedAt: this.now(),
|
|
484
|
+
...fields,
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
refuse(spec, code, detail) {
|
|
488
|
+
const result = this.result(spec, { kind: "refused", code, detail });
|
|
489
|
+
try {
|
|
490
|
+
return { kind: "refused", result: this.opts.results.append(result) };
|
|
491
|
+
}
|
|
492
|
+
catch (err) {
|
|
493
|
+
return { kind: "refused", result: { ...result, detail: `${detail}; result not recorded: ${storeCode(err) ?? message(err)}` } };
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
/** Report the ledger outcome, then append the one terminal result. A ledger or result-log failure is reported, never thrown. */
|
|
497
|
+
finish(result) {
|
|
498
|
+
let detail = result.detail;
|
|
499
|
+
if (result.attemptId !== null) {
|
|
500
|
+
try {
|
|
501
|
+
this.opts.access.recordOutcome(this.opts.identity, result.attemptId, result.kind === "succeeded" ? "passed" : "failed");
|
|
502
|
+
}
|
|
503
|
+
catch (err) {
|
|
504
|
+
detail += `; ledger outcome not recorded: ${storeCode(err) ?? message(err)}`;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
try {
|
|
508
|
+
return this.opts.results.append({ ...result, detail });
|
|
509
|
+
}
|
|
510
|
+
catch (err) {
|
|
511
|
+
// The job ran, but its one terminal result could not be written. Report
|
|
512
|
+
// that as the outcome rather than reject the result promise.
|
|
513
|
+
return { ...result, kind: "failed", code: "RESULT_NOT_RECORDED", detail: `${result.kind} ${result.code}: ${detail}; result not recorded: ${storeCode(err) ?? message(err)}` };
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
async start(spec) {
|
|
517
|
+
const invalid = validateSpec(spec);
|
|
518
|
+
if (invalid)
|
|
519
|
+
return this.refuse(spec, "INVALID_SPEC", invalid);
|
|
520
|
+
try {
|
|
521
|
+
const { run, jobs } = this.opts.access.readRun(this.opts.identity, spec.runId);
|
|
522
|
+
if (run.tenantId !== spec.tenantId)
|
|
523
|
+
return this.refuse(spec, "INVALID_SPEC", `run ${spec.runId} belongs to another tenant`);
|
|
524
|
+
if (!jobs.some((j) => j.job.jobId === spec.jobId))
|
|
525
|
+
return this.refuse(spec, "UNKNOWN_JOB", `run ${spec.runId} has no job ${spec.jobId}`);
|
|
526
|
+
}
|
|
527
|
+
catch (err) {
|
|
528
|
+
return this.refuse(spec, storeCode(err) ?? "UNKNOWN_JOB", message(err));
|
|
529
|
+
}
|
|
530
|
+
const match = matchCapabilities(this.opts.profile, spec.requires);
|
|
531
|
+
if (!match.ok)
|
|
532
|
+
return this.refuse(spec, match.code, match.detail);
|
|
533
|
+
for (const ref of spec.inputs) {
|
|
534
|
+
const problem = this.opts.inputs.verify(ref);
|
|
535
|
+
if (problem)
|
|
536
|
+
return this.refuse(spec, problem, problem === "INPUT_MISSING" ? `input ${ref.name} is not admitted` : `input ${ref.name} changed after admission`);
|
|
537
|
+
}
|
|
538
|
+
const launchRun = { tenantId: spec.tenantId, runId: spec.runId, jobId: spec.jobId };
|
|
539
|
+
try {
|
|
540
|
+
this.opts.launcher.prepare?.(launchRun);
|
|
541
|
+
}
|
|
542
|
+
catch (err) {
|
|
543
|
+
return err instanceof WorkerRefusal ? this.refuse(spec, err.code, err.detail) : this.refuse(spec, "CONTAINMENT_UNAVAILABLE", message(err));
|
|
544
|
+
}
|
|
545
|
+
let attemptId;
|
|
546
|
+
try {
|
|
547
|
+
attemptId = this.opts.access.claim(this.opts.identity, spec.jobId).attempt.attemptId;
|
|
548
|
+
}
|
|
549
|
+
catch (err) {
|
|
550
|
+
return this.refuse(spec, storeCode(err) ?? "CLAIM_FAILED", message(err));
|
|
551
|
+
}
|
|
552
|
+
const attemptDir = join(this.opts.workDir, attemptId);
|
|
553
|
+
const logPath = join(attemptDir, "output.log");
|
|
554
|
+
try {
|
|
555
|
+
mkdirSync(join(attemptDir, "inputs"), { recursive: true });
|
|
556
|
+
for (const ref of spec.inputs)
|
|
557
|
+
this.opts.inputs.materialize(ref, join(attemptDir, "inputs"));
|
|
558
|
+
}
|
|
559
|
+
catch (err) {
|
|
560
|
+
const code = err instanceof WorkerRefusal ? err.code : "INPUT_FAILED";
|
|
561
|
+
return { kind: "failed", result: this.finish(this.result(spec, { kind: "failed", code, detail: message(err), attemptId })) };
|
|
562
|
+
}
|
|
563
|
+
const startedAt = this.now();
|
|
564
|
+
let proc;
|
|
565
|
+
try {
|
|
566
|
+
proc = this.opts.launcher.launch({ file: spec.command.file, args: [...spec.command.args], cwd: attemptDir, env: { ...spec.env }, logPath, limits: { ...spec.limits } }, launchRun);
|
|
567
|
+
}
|
|
568
|
+
catch (err) {
|
|
569
|
+
return { kind: "failed", result: this.finish(this.result(spec, { kind: "failed", code: "SPAWN_FAILED", detail: message(err), attemptId, startedAt })) };
|
|
570
|
+
}
|
|
571
|
+
let cancelRequested = false;
|
|
572
|
+
// The result reads the root pid from ready itself, so a process that ends
|
|
573
|
+
// before this method resumes still reports its pid.
|
|
574
|
+
const rootPid = proc.ready.then((p) => (p > 0 ? p : null), () => null);
|
|
575
|
+
const result = proc.done.then(async (outcome) => this.finish(this.result(spec, { ...this.mapOutcome(outcome, cancelRequested), attemptId, pid: await rootPid, startedAt })), async (err) => this.finish(this.result(spec, { kind: "failed", code: "SUPERVISOR_LOST", detail: message(err), attemptId, pid: await rootPid, startedAt })));
|
|
576
|
+
const pid = await proc.ready;
|
|
577
|
+
if (pid <= 0)
|
|
578
|
+
return { kind: "failed", result: await result };
|
|
579
|
+
const cancel = () => {
|
|
580
|
+
if (cancelRequested)
|
|
581
|
+
return;
|
|
582
|
+
cancelRequested = true;
|
|
583
|
+
proc.cancel();
|
|
584
|
+
};
|
|
585
|
+
return { kind: "started", attemptId, pid, process: proc, logPath, cancel, result };
|
|
586
|
+
}
|
|
587
|
+
mapOutcome(o, cancelRequested) {
|
|
588
|
+
switch (o.reason) {
|
|
589
|
+
case "cancelled":
|
|
590
|
+
return { kind: "cancelled", code: "CANCELLED", detail: "cancelled; the contained tree was terminated", exitCode: o.exitCode ?? null };
|
|
591
|
+
case "limit": {
|
|
592
|
+
const code = o.limit === "memory" ? "MEMORY_LIMIT" : o.limit === "processes" ? "PROCESS_LIMIT" : "CPU_LIMIT";
|
|
593
|
+
return { kind: "limit_exceeded", code, detail: `${o.limit} limit exceeded; the contained tree was terminated`, exitCode: o.exitCode ?? null };
|
|
594
|
+
}
|
|
595
|
+
case "exited":
|
|
596
|
+
if (cancelRequested)
|
|
597
|
+
return { kind: "cancelled", code: "CANCELLED", detail: "cancelled; the job exited", exitCode: o.exitCode };
|
|
598
|
+
return o.exitCode === 0
|
|
599
|
+
? { kind: "succeeded", code: "EXITED", detail: "exited 0", exitCode: 0 }
|
|
600
|
+
: { kind: "failed", code: "EXIT_NONZERO", detail: `exited ${o.exitCode}`, exitCode: o.exitCode };
|
|
601
|
+
case "supervisor_lost":
|
|
602
|
+
return { kind: "failed", code: "SUPERVISOR_LOST", detail: o.detail, exitCode: null };
|
|
603
|
+
case "spawn_failed":
|
|
604
|
+
return { kind: "failed", code: "SPAWN_FAILED", detail: o.detail, exitCode: null };
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
//# sourceMappingURL=windows.js.map
|
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export declare function assertProviderWrappersInstallable(binDir: string): void;
|
|
6
6
|
export declare function providerWrapperInstallPaths(home: string): string[];
|
|
7
|
+
/** Env var the installed wrapper sets when it hands a run to another wrapper script. */
|
|
8
|
+
export declare function providerWrapperGuardVar(provider: 'claude' | 'codex'): string;
|
|
7
9
|
export declare function installProviderWrappers(binDir: string, home?: string): string[];
|
|
8
10
|
export declare function removeProviderWrappers(binDir: string): string[];
|
|
9
11
|
//# sourceMappingURL=provider-wrappers.d.ts.map
|