codewhale 0.10.0 → 0.10.2

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.
@@ -68,20 +68,20 @@ function buildFromSourceHint() {
68
68
  return [
69
69
  "You can still run codewhale by building from source with Cargo:",
70
70
  "",
71
- " # Requires Rust 1.88+ (https://rustup.rs)",
71
+ " # Requires Rust 1.89+ (https://rustup.rs)",
72
72
  " cargo install codewhale-cli --locked # provides `codewhale`",
73
73
  " bin=$(dirname \"$(command -v codewhale)\")",
74
74
  " ln -sf \"$bin/codewhale\" \"$bin/codew\" # optional short alias",
75
75
  "",
76
76
  "Or build from a checkout:",
77
77
  "",
78
- " git clone https://github.com/Hmbown/CodeWhale.git",
78
+ " git clone https://github.com/codewhale-hq/CodeWhale.git",
79
79
  " cd CodeWhale",
80
80
  " cargo install --path crates/cli --locked",
81
81
  " bin=$(dirname \"$(command -v codewhale)\")",
82
82
  " ln -sf \"$bin/codewhale\" \"$bin/codew\"",
83
83
  "",
84
- "See https://github.com/Hmbown/CodeWhale/blob/main/docs/INSTALL.md",
84
+ "See https://github.com/codewhale-hq/CodeWhale/blob/main/docs/INSTALL.md",
85
85
  ].join("\n");
86
86
  }
87
87
 
@@ -96,13 +96,13 @@ function skipGlibcCheck() {
96
96
  function glibcCompatibilityMessage(required, host) {
97
97
  const hostLine = host
98
98
  ? `this system has glibc ${formatVersion(host)}, which is too old for that asset.`
99
- : "this system does not appear to provide GNU libc.";
99
+ : "this system does not appear to provide glibc.";
100
100
  return [
101
- `Prebuilt Codewhale Linux binaries require GLIBC_${formatVersion(required)}, but ${hostLine}`,
101
+ `This Codewhale binary requires GLIBC_${formatVersion(required)}, but ${hostLine}`,
102
102
  "",
103
- "The Linux x64 release asset is a static (musl) build that runs on any glibc,",
104
- "but the Linux arm64 asset is a GNU libc build linked against",
105
- "Ubuntu 24.04/glibc 2.39, which Ubuntu 22.04 (glibc 2.35) cannot run.",
103
+ "Official Codewhale Linux release assets (x64 and arm64) are static musl builds",
104
+ "with no glibc dependency, so this binary is not an official release asset.",
105
+ "Check where it came from, or build from source on this host.",
106
106
  "",
107
107
  buildFromSourceHint(),
108
108
  "",
package/scripts/run.js CHANGED
@@ -1,5 +1,7 @@
1
- const { spawnSync } = require("child_process");
2
- const { getBinaryPath } = require("./install");
1
+ const { spawn: spawnChild } = require("child_process");
2
+ const os = require("os");
3
+ const { getBinaryPath, installFailureHint } = require("./install");
4
+ const { openHarmonyDirections } = require("./artifacts");
3
5
 
4
6
  const pkg = require("../package.json");
5
7
 
@@ -7,22 +9,128 @@ function isVersionFlag(args = process.argv.slice(2)) {
7
9
  return args.includes("--version") || args.includes("-V");
8
10
  }
9
11
 
10
- function printVersionFallback(binaryName) {
12
+ const OPEN_HARMONY_EXEC_ERRORS = ["ENOEXEC", "EACCES", "EPERM"];
13
+ const OPEN_HARMONY_STARTUP_SIGNALS = ["SIGKILL", "SIGSYS", "SIGSEGV", "SIGBUS", "SIGILL", "SIGABRT"];
14
+ const OPEN_HARMONY_STARTUP_MS = 5_000;
15
+
16
+ // Print the install hint (mirror / release-base guidance) for a failure that
17
+ // looks like a download problem, or the HarmonyOS directions when the Linux
18
+ // build cannot be executed on native HarmonyOS. Prints nothing otherwise.
19
+ function printInstallFailureHint(error, log = console.error) {
20
+ if (os.platform() === "openharmony" && OPEN_HARMONY_EXEC_ERRORS.includes(error && error.code)) {
21
+ log(`The Linux build could not start on native HarmonyOS/OpenHarmony.\n${openHarmonyDirections()}`);
22
+ return;
23
+ }
24
+ const hint = installFailureHint(error);
25
+ if (hint) {
26
+ log(hint);
27
+ }
28
+ }
29
+
30
+ // Shared by the `codewhale` and `codew` bin shims: the error and, when the
31
+ // binary could not be downloaded, the hint that says what to do about it.
32
+ function reportStartFailure(binaryName, error, log = console.error) {
33
+ log(`Failed to start ${binaryName}:`, error && error.message ? error.message : String(error));
34
+ printInstallFailureHint(error, log);
35
+ }
36
+
37
+ // `--version` must still answer when the native binary is missing, but it
38
+ // must not report a binary version that is not actually installed: the
39
+ // expected version goes to stdout labelled as such, the failure to stderr.
40
+ function printVersionFallback(binaryName, error) {
11
41
  const binVersion =
12
42
  process.env.CODEWHALE_VERSION ||
13
43
  process.env.DEEPSEEK_TUI_VERSION ||
14
44
  process.env.DEEPSEEK_VERSION ||
15
45
  pkg.codewhaleBinaryVersion || pkg.deepseekBinaryVersion || pkg.version;
16
46
  console.log(`${binaryName} (npm wrapper) v${pkg.version}`);
17
- console.log(`binary version: v${binVersion}`);
47
+ console.log(`binary: not installed (expected v${binVersion})`);
18
48
  console.log(`repo: ${pkg.repository?.url || "N/A"}`);
49
+ if (error) {
50
+ console.error(`${binaryName}: native binary unavailable: ${error.message || String(error)}`);
51
+ printInstallFailureHint(error);
52
+ }
53
+ }
54
+
55
+ // Signals that end the wrapper must end the native binary too. With a
56
+ // blocking spawn the wrapper died on SIGTERM and left the child running (still
57
+ // executing tools, still holding its port); as PID 1 in a container it ignored
58
+ // SIGTERM entirely. So SIGTERM, which a terminal never sends, is forwarded.
59
+ //
60
+ // SIGINT (Ctrl-C) and SIGHUP (terminal hangup) come from the terminal, which
61
+ // delivers them to the whole foreground process group, native child included.
62
+ // Forwarding them would deliver each one twice, and the native binary treats a
63
+ // second signal as "skip the graceful drain". On Windows `child.kill()` is a
64
+ // hard TerminateProcess, which would race the child's own Ctrl-C cleanup. The
65
+ // wrapper only has to outlive them, then mirror how the child ended.
66
+ const FORWARDED_SIGNALS = process.platform === "win32" ? [] : ["SIGTERM"];
67
+ const OUTLIVED_SIGNALS = ["SIGINT", "SIGHUP"];
68
+
69
+ function ignoreSignal() {}
70
+
71
+ // Run the native binary and settle with how it ended. While it runs, the
72
+ // forwarded signals go to the child and the outlived ones do not kill the
73
+ // wrapper.
74
+ function runChild(spawn, binaryPath, args, proc) {
75
+ return new Promise((resolve) => {
76
+ let child;
77
+ try {
78
+ child = spawn(binaryPath, args, { stdio: "inherit" });
79
+ } catch (error) {
80
+ resolve({ error });
81
+ return;
82
+ }
83
+ const listeners = [
84
+ ...FORWARDED_SIGNALS.map((signal) => [
85
+ signal,
86
+ () => {
87
+ try {
88
+ child.kill(signal);
89
+ } catch {
90
+ // The child already exited; its exit event settles the run.
91
+ }
92
+ },
93
+ ]),
94
+ ...OUTLIVED_SIGNALS.map((signal) => [signal, ignoreSignal]),
95
+ ];
96
+ for (const [signal, listener] of listeners) {
97
+ proc.on(signal, listener);
98
+ }
99
+ let settled = false;
100
+ const settle = (result) => {
101
+ if (settled) return;
102
+ settled = true;
103
+ for (const [signal, listener] of listeners) {
104
+ proc.removeListener(signal, listener);
105
+ }
106
+ resolve(result);
107
+ };
108
+ child.once("error", (error) => settle({ error }));
109
+ child.once("exit", (status, signal) => settle({ status, signal }));
110
+ });
111
+ }
112
+
113
+ // Mirror a signal death to the parent shell: re-raise it on the wrapper once
114
+ // our listeners are gone, and fall back to the conventional 128 + n.
115
+ function exitLikeChild(result, exit, proc) {
116
+ if (result.signal) {
117
+ try {
118
+ proc.kill(proc.pid, result.signal);
119
+ } catch {
120
+ // fall through to the numeric status
121
+ }
122
+ const number = os.constants.signals[result.signal];
123
+ return exit(number ? 128 + number : 1);
124
+ }
125
+ return exit(result.status ?? 1);
19
126
  }
20
127
 
21
128
  async function run(binaryName, options = {}) {
22
129
  const args = options.args || process.argv.slice(2);
23
130
  const resolveBinaryPath = options.getBinaryPath || getBinaryPath;
24
- const spawn = options.spawnSync || spawnSync;
131
+ const spawn = options.spawn || spawnChild;
25
132
  const exit = options.exit || process.exit;
133
+ const proc = options.process || process;
26
134
  const versionFlag = isVersionFlag(args);
27
135
 
28
136
  let binaryPath;
@@ -30,23 +138,32 @@ async function run(binaryName, options = {}) {
30
138
  binaryPath = await resolveBinaryPath(binaryName);
31
139
  } catch (error) {
32
140
  if (versionFlag) {
33
- printVersionFallback(binaryName);
141
+ printVersionFallback(binaryName, error);
34
142
  return exit(0);
35
143
  }
36
144
  throw error;
37
145
  }
38
146
 
39
- const result = spawn(binaryPath, args, {
40
- stdio: "inherit",
41
- });
147
+ const startedAt = Date.now();
148
+ const result = await runChild(spawn, binaryPath, args, proc);
42
149
  if (result.error) {
43
150
  if (versionFlag) {
44
- printVersionFallback(binaryName);
151
+ printVersionFallback(binaryName, result.error);
45
152
  return exit(0);
46
153
  }
47
154
  throw result.error;
48
155
  }
49
- return exit(result.status ?? 1);
156
+ if (
157
+ os.platform() === "openharmony" &&
158
+ OPEN_HARMONY_STARTUP_SIGNALS.includes(result.signal) &&
159
+ Date.now() - startedAt < OPEN_HARMONY_STARTUP_MS
160
+ ) {
161
+ console.error(
162
+ `${binaryName} stopped with ${result.signal} at startup; the Linux build may not run on native HarmonyOS/OpenHarmony.\n` +
163
+ openHarmonyDirections(),
164
+ );
165
+ }
166
+ return exitLikeChild(result, exit, proc);
50
167
  }
51
168
 
52
169
  async function runCodeWhale() {
@@ -65,14 +182,21 @@ module.exports = {
65
182
  run,
66
183
  runCodeWhale,
67
184
  runCodeWhaleTui,
68
- _internal: { isVersionFlag, printVersionFallback },
185
+ reportStartFailure,
186
+ _internal: { isVersionFlag, printVersionFallback, FORWARDED_SIGNALS, OUTLIVED_SIGNALS },
69
187
  };
70
188
 
71
189
  if (require.main === module) {
72
190
  const command = process.argv[1] || "";
73
191
  if (command.includes("tui")) {
74
- runCodeWhaleTui();
192
+ runCodeWhaleTui().catch((error) => {
193
+ reportStartFailure("codewhale", error);
194
+ process.exit(1);
195
+ });
75
196
  } else {
76
- runCodeWhale();
197
+ runCodeWhale().catch((error) => {
198
+ reportStartFailure("codewhale", error);
199
+ process.exit(1);
200
+ });
77
201
  }
78
202
  }
@@ -29,7 +29,7 @@ function resolveRepo() {
29
29
  process.env.CODEWHALE_GITHUB_REPO ||
30
30
  process.env.DEEPSEEK_TUI_GITHUB_REPO ||
31
31
  process.env.DEEPSEEK_GITHUB_REPO ||
32
- "Hmbown/CodeWhale"
32
+ "codewhale-hq/CodeWhale"
33
33
  );
34
34
  }
35
35
 
@@ -62,6 +62,38 @@ function assertPackageVersionMatchesBinaryVersion(version) {
62
62
  );
63
63
  }
64
64
 
65
+ // Every request is bounded: `idleMs` covers connecting and any stall (it is
66
+ // the socket timeout, which starts before the socket connects), `totalMs` is a
67
+ // hard ceiling for one request including a body that keeps trickling, and a
68
+ // body larger than `maxBodyBytes` is cut off. Redirects are limited to ten, so
69
+ // a chain is bounded too. Tests shrink these through the exported object.
70
+ const limits = {
71
+ idleMs: 30_000,
72
+ totalMs: 120_000,
73
+ maxBodyBytes: 8 * 1024 * 1024,
74
+ };
75
+
76
+ // Reject first, then tear the request down: an error passed to `destroy()` can
77
+ // surface on an emitter nobody listens to once a response has started.
78
+ function armTotalTimeout(req, url, reject) {
79
+ const fail = (error) => {
80
+ reject(error);
81
+ req.destroy();
82
+ };
83
+ const timer = setTimeout(
84
+ () => fail(new Error(`Request exceeded ${limits.totalMs} ms: ${url}`)),
85
+ limits.totalMs,
86
+ );
87
+ timer.unref();
88
+ const clear = () => clearTimeout(timer);
89
+ req.on("close", clear);
90
+ req.on("error", clear);
91
+ req.on("timeout", () =>
92
+ fail(new Error(`Request timed out after ${limits.idleMs} ms without data: ${url}`)),
93
+ );
94
+ return { clear, fail };
95
+ }
96
+
65
97
  function requestStatus(url, method = "HEAD", redirects = 0) {
66
98
  if (redirects > 10) {
67
99
  throw new Error(`Too many redirects while checking ${url}`);
@@ -72,6 +104,7 @@ function requestStatus(url, method = "HEAD", redirects = 0) {
72
104
  url,
73
105
  {
74
106
  method,
107
+ timeout: limits.idleMs,
75
108
  headers: {
76
109
  "User-Agent": "codewhale-npm-release-check",
77
110
  },
@@ -80,6 +113,7 @@ function requestStatus(url, method = "HEAD", redirects = 0) {
80
113
  const status = res.statusCode || 0;
81
114
  const location = res.headers.location;
82
115
  res.resume();
116
+ clear();
83
117
  if (status >= 300 && status < 400 && location) {
84
118
  const next = new URL(location, url).toString();
85
119
  resolve(requestStatus(next, method, redirects + 1));
@@ -88,6 +122,7 @@ function requestStatus(url, method = "HEAD", redirects = 0) {
88
122
  resolve(status);
89
123
  },
90
124
  );
125
+ const { clear } = armTotalTimeout(req, url, reject);
91
126
  req.on("error", reject);
92
127
  req.end();
93
128
  });
@@ -109,34 +144,44 @@ async function downloadText(url, redirects = 0) {
109
144
  }
110
145
  const client = url.startsWith("https:") ? https : http;
111
146
  return new Promise((resolve, reject) => {
112
- client
113
- .get(
114
- url,
115
- {
116
- headers: {
117
- "User-Agent": "codewhale-npm-release-check",
118
- },
147
+ const req = client.get(
148
+ url,
149
+ {
150
+ timeout: limits.idleMs,
151
+ headers: {
152
+ "User-Agent": "codewhale-npm-release-check",
119
153
  },
120
- (res) => {
121
- const status = res.statusCode || 0;
122
- if (status >= 300 && status < 400 && res.headers.location) {
123
- const next = new URL(res.headers.location, url).toString();
124
- res.resume();
125
- resolve(downloadText(next, redirects + 1));
126
- return;
127
- }
128
- if (status !== 200) {
129
- reject(new Error(`Request failed with status ${status}: ${url}`));
130
- res.resume();
154
+ },
155
+ (res) => {
156
+ const status = res.statusCode || 0;
157
+ if (status >= 300 && status < 400 && res.headers.location) {
158
+ const next = new URL(res.headers.location, url).toString();
159
+ res.resume();
160
+ resolve(downloadText(next, redirects + 1));
161
+ return;
162
+ }
163
+ if (status !== 200) {
164
+ reject(new Error(`Request failed with status ${status}: ${url}`));
165
+ res.resume();
166
+ return;
167
+ }
168
+ const chunks = [];
169
+ let size = 0;
170
+ res.setEncoding("utf8");
171
+ res.on("error", reject);
172
+ res.on("data", (chunk) => {
173
+ size += Buffer.byteLength(chunk);
174
+ if (size > limits.maxBodyBytes) {
175
+ guard.fail(new Error(`Response exceeds ${limits.maxBodyBytes} bytes: ${url}`));
131
176
  return;
132
177
  }
133
- const chunks = [];
134
- res.setEncoding("utf8");
135
- res.on("data", (chunk) => chunks.push(chunk));
136
- res.on("end", () => resolve(chunks.join("")));
137
- },
138
- )
139
- .on("error", reject);
178
+ chunks.push(chunk);
179
+ });
180
+ res.on("end", () => resolve(chunks.join("")));
181
+ },
182
+ );
183
+ const guard = armTotalTimeout(req, url, reject);
184
+ req.on("error", reject);
140
185
  });
141
186
  }
142
187
 
@@ -144,7 +189,10 @@ async function downloadJson(url, redirects = 0) {
144
189
  if (redirects > 10) {
145
190
  throw new Error(`Too many redirects while downloading ${url}`);
146
191
  }
147
- const client = url.startsWith("https:") ? https : http;
192
+ const parsedUrl = new URL(url);
193
+ if (parsedUrl.protocol !== "https:") {
194
+ throw new Error("Release metadata requires HTTPS");
195
+ }
148
196
  return new Promise((resolve, reject) => {
149
197
  const headers = {
150
198
  Accept: "application/vnd.github+json",
@@ -152,11 +200,11 @@ async function downloadJson(url, redirects = 0) {
152
200
  "X-GitHub-Api-Version": "2022-11-28",
153
201
  };
154
202
  const token = process.env.GITHUB_TOKEN || process.env.GH_TOKEN;
155
- if (token) {
203
+ if (token && parsedUrl.origin === "https://api.github.com") {
156
204
  headers.Authorization = `Bearer ${token}`;
157
205
  }
158
- client
159
- .get(url, { headers }, (res) => {
206
+ const req = https
207
+ .get(url, { headers, timeout: limits.idleMs }, (res) => {
160
208
  const status = res.statusCode || 0;
161
209
  if (status >= 300 && status < 400 && res.headers.location) {
162
210
  const next = new URL(res.headers.location, url).toString();
@@ -165,8 +213,17 @@ async function downloadJson(url, redirects = 0) {
165
213
  return;
166
214
  }
167
215
  const chunks = [];
216
+ let size = 0;
168
217
  res.setEncoding("utf8");
169
- res.on("data", (chunk) => chunks.push(chunk));
218
+ res.on("error", reject);
219
+ res.on("data", (chunk) => {
220
+ size += Buffer.byteLength(chunk);
221
+ if (size > limits.maxBodyBytes) {
222
+ guard.fail(new Error(`Response exceeds ${limits.maxBodyBytes} bytes: ${url}`));
223
+ return;
224
+ }
225
+ chunks.push(chunk);
226
+ });
170
227
  res.on("end", () => {
171
228
  const body = chunks.join("");
172
229
  let parsed;
@@ -183,8 +240,9 @@ async function downloadJson(url, redirects = 0) {
183
240
  }
184
241
  resolve(parsed);
185
242
  });
186
- })
187
- .on("error", reject);
243
+ });
244
+ const guard = armTotalTimeout(req, url, reject);
245
+ req.on("error", reject);
188
246
  });
189
247
  }
190
248
 
@@ -330,11 +388,21 @@ function assertChecksumManifestIncludes(checksums, expectedAssets, label) {
330
388
  }
331
389
  }
332
390
 
391
+ const compiledHosts = require("./compiled-hosts");
392
+
333
393
  async function run() {
334
394
  const version = resolveBinaryVersion();
335
395
  const repo = resolveRepo();
336
396
  const cnbMirror = usesCnbMirror();
337
- const assets = cnbMirror ? CNB_RELEASE_ASSET_NAMES : allReleaseAssetNames();
397
+ const checksums = parseChecksumManifest(await downloadText(checksumManifestUrl(version, repo)));
398
+ let catalog;
399
+ if (checksums.has(compiledHosts.HOST_CATALOG)) {
400
+ const text = await downloadText(releaseAssetUrl(compiledHosts.HOST_CATALOG, version, repo));
401
+ compiledHosts.verifyBytes(Buffer.from(text), checksums.get(compiledHosts.HOST_CATALOG), compiledHosts.HOST_CATALOG);
402
+ catalog = compiledHosts.parseCatalog(text, version);
403
+ }
404
+ if (compiledHosts.requested() && !catalog) throw new Error("compiled host requested but this source has no qualified catalog");
405
+ const assets = cnbMirror ? [...CNB_RELEASE_ASSET_NAMES, ...compiledHosts.assets(catalog)] : allReleaseAssetNames(catalog);
338
406
 
339
407
  assertPackageVersionMatchesBinaryVersion(version);
340
408
 
@@ -349,12 +417,9 @@ async function run() {
349
417
  await verifyAsset(url, asset);
350
418
  console.log(` ok ${asset}`);
351
419
  }
352
- const checksums = parseChecksumManifest(
353
- await downloadText(checksumManifestUrl(version, repo)),
354
- );
355
420
  assertChecksumManifestIncludes(
356
421
  checksums,
357
- cnbMirror ? CNB_BINARY_ASSET_NAMES : checksummedReleaseAssetNames(),
422
+ cnbMirror ? [...CNB_BINARY_ASSET_NAMES, ...compiledHosts.assets(catalog)] : checksummedReleaseAssetNames(catalog),
358
423
  "Canonical checksum manifest",
359
424
  );
360
425
  if (!cnbMirror) {
@@ -378,6 +443,10 @@ if (require.main === module) {
378
443
  }
379
444
 
380
445
  module.exports = {
446
+ downloadJson,
447
+ downloadText,
448
+ limits,
449
+ requestStatus,
381
450
  assertChecksumManifestIncludes,
382
451
  assertPackageVersionMatchesBinaryVersion,
383
452
  assertReleaseAssetsFresh,
@@ -64,6 +64,7 @@ test("genuinely unsupported platform throws with raw platform name", () => {
64
64
  test("known platforms are unaffected by alias map", () => {
65
65
  for (const [platform, arch, expectedCodeWhale] of [
66
66
  ["linux", "x64", "codewhale-linux-x64"],
67
+ ["linux", "arm64", "codewhale-linux-arm64"],
67
68
  ["darwin", "arm64", "codewhale-macos-arm64"],
68
69
  ["win32", "x64", "codewhale-windows-x64.exe"],
69
70
  ["win32", "arm64", "codewhale-windows-arm64.exe"],
@@ -0,0 +1,156 @@
1
+ const assert = require('node:assert/strict');
2
+ const fs = require('node:fs');
3
+ const os = require('node:os');
4
+ const path = require('node:path');
5
+ const test = require('node:test');
6
+ const hosts = require('../scripts/compiled-hosts');
7
+ const { run, _internal } = require('../scripts/install');
8
+
9
+ function fixture(target = 'macos-arm64', marker = 'one') {
10
+ const names = hosts.names(target);
11
+ const payloads = { [names.binary]: Buffer.from(`image-${marker}`), [names.notices]: Buffer.from(`actual-notice-${marker}`), [names.source]: Buffer.from(`source-fixture-${marker}`) };
12
+ const digest = 'a'.repeat(64), source = 'b'.repeat(40);
13
+ const catalog = { schema: 1, version: '0.10.1', source_sha: source, bundle_sha256: digest, hosts: [{
14
+ target, asset: names.binary, sha256: hosts.sha256(payloads[names.binary]),
15
+ notices_asset: names.notices, notices_sha256: hosts.sha256(payloads[names.notices]),
16
+ source_asset: names.source, source_sha256: hosts.sha256(payloads[names.source]),
17
+ runtime_version: '1.4.0', runtime_revision: 'c'.repeat(40), runtime_sha256: 'd'.repeat(64), webkit_revision: 'e'.repeat(40),
18
+ bundle_sha256: digest, source_commit: source, native_platform: hosts.TARGETS[target][0], native_arch: hosts.TARGETS[target][1], libc: target.startsWith('linux-') ? 'glibc' : 'none',
19
+ passed: hosts.compiledMinimum(target), failed: 0, skipped: 0, test_log_sha256: 'f'.repeat(64), native_passed: hosts.nativeMinimum(target), native_failed: 0, native_skipped: 0, native_log_sha256: '1'.repeat(64),
20
+ license_closure: 'complete', relink_source: 'complete',
21
+ }] };
22
+ const text = JSON.stringify(catalog);
23
+ payloads[hosts.HOST_CATALOG] = Buffer.from(text);
24
+ return { catalog, text, payloads, host: catalog.hosts[0] };
25
+ }
26
+ async function temporary(t) {
27
+ const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'cw-host-delivery-'));
28
+ t.after(() => fs.promises.rm(dir, { recursive: true, force: true }));
29
+ return dir;
30
+ }
31
+ function installOptions(releaseDir, f, extra = {}) {
32
+ const source = { baseUrl: 'https://fixture.invalid/', checksums: new Map(Object.entries(f.payloads).map(([name, bytes]) => [name, hosts.sha256(bytes)])) };
33
+ return { version: f.catalog.version, releaseDir, source, context: 'install', options: {
34
+ platform: hosts.TARGETS[f.host.target][0], arch: hosts.TARGETS[f.host.target][1],
35
+ fetchText: async () => f.text,
36
+ download: async (url, target) => fs.promises.writeFile(target, f.payloads[path.basename(new URL(url).pathname)]), ...extra,
37
+ } };
38
+ }
39
+ test('catalog refuses malformed, skipped, cross-native, duplicate and incomplete closure receipts', () => {
40
+ const good = fixture();
41
+ assert.equal(hosts.parseCatalog(Buffer.from(good.text), '0.10.1').hosts.length, 1);
42
+ for (const mutate of [c => c.hosts[0].native_skipped = 1, c => c.hosts[0].passed = 0, c => c.hosts[0].native_arch = 'x64', c => c.hosts[0].license_closure = 'pending', c => c.hosts[0].source_asset = '../source.tar.gz', c => c.hosts.push(c.hosts[0]), c => c.source_sha = 'bad']) {
43
+ const copy = structuredClone(good.catalog); mutate(copy); assert.throws(() => hosts.parseCatalog(copy));
44
+ }
45
+ assert.throws(() => hosts.parseCatalog(good.text, '0.10.2'), /differs/);
46
+ assert.throws(() => hosts.parseCatalog(' '.repeat(65537)), /64 KiB/);
47
+ assert.throws(() => hosts.selectedHost(good.catalog, 'android', 'arm64'), /Android/);
48
+ assert.throws(() => hosts.selectedHost(good.catalog, 'darwin', 'x64'), /no qualified image/);
49
+ assert.equal(hosts.requested({}), false);
50
+ });
51
+ test('payload directory rejects tampering and symbolic substitutions', async t => {
52
+ const dir = await temporary(t), f = fixture();
53
+ for (const [name, bytes] of Object.entries(f.payloads)) fs.writeFileSync(path.join(dir, name), bytes);
54
+ hosts.verifyDirectory(dir, f.catalog);
55
+ fs.writeFileSync(path.join(dir, f.host.source_asset), 'tampered');
56
+ assert.throws(() => hosts.verifyDirectory(dir, f.catalog), /SHA256/);
57
+ fs.unlinkSync(path.join(dir, f.host.source_asset));
58
+ fs.symlinkSync(path.join(dir, f.host.notices_asset), path.join(dir, f.host.source_asset));
59
+ assert.throws(() => hosts.verifyDirectory(dir, f.catalog), /regular payload/);
60
+ });
61
+ test('npm stages every companion before publication and keeps canonical file modes', async t => {
62
+ const dir = await temporary(t), f = fixture(), prepared = await _internal.prepareCompiledHost(installOptions(dir, f));
63
+ assert.equal(fs.existsSync(path.join(dir, hosts.HOST_NAME)), false);
64
+ try {
65
+ await prepared.publish();
66
+ assert.deepEqual(fs.readFileSync(path.join(dir, hosts.HOST_NAME)), f.payloads[f.host.asset]);
67
+ assert.equal(hosts.parseCatalog(fs.readFileSync(path.join(dir, hosts.HOST_NAME + '.release.json'))).version, '0.10.1');
68
+ if (process.platform !== 'win32') {
69
+ assert.equal(fs.statSync(path.join(dir, hosts.HOST_NAME)).mode & 0o777, 0o755);
70
+ assert.equal(fs.statSync(path.join(dir, hosts.HOST_NAME + '.LICENSES.txt')).mode & 0o777, 0o644);
71
+ }
72
+ } finally { await prepared.cleanup(); }
73
+ assert.equal(fs.readdirSync(dir).some(name => name.startsWith('.compiled-host-')), false);
74
+ });
75
+ test('npm rejects foreign companion ownership and destination changes without overwriting them', async t => {
76
+ const dir = await temporary(t), f = fixture();
77
+ fs.writeFileSync(path.join(dir, hosts.HOST_NAME), 'foreign');
78
+ await assert.rejects(_internal.prepareCompiledHost(installOptions(dir, f)), /unclaimed/);
79
+ assert.equal(fs.readFileSync(path.join(dir, hosts.HOST_NAME), 'utf8'), 'foreign');
80
+ fs.unlinkSync(path.join(dir, hosts.HOST_NAME));
81
+ const prepared = await _internal.prepareCompiledHost(installOptions(dir, f));
82
+ fs.writeFileSync(path.join(dir, hosts.HOST_NAME + '.LICENSES.txt'), 'concurrent');
83
+ try { await assert.rejects(prepared.publish(), /changed/); } finally { await prepared.cleanup(); }
84
+ assert.equal(fs.existsSync(path.join(dir, hosts.HOST_NAME)), false);
85
+ assert.equal(fs.readFileSync(path.join(dir, hosts.HOST_NAME + '.LICENSES.txt'), 'utf8'), 'concurrent');
86
+ });
87
+ test('npm updates an owned companion and rejects modified prior bytes', async t => {
88
+ const dir = await temporary(t), first = fixture(), second = fixture('macos-arm64', 'two');
89
+ await _internal.installCompiledHost(installOptions(dir, first));
90
+ await _internal.installCompiledHost(installOptions(dir, second));
91
+ assert.deepEqual(fs.readFileSync(path.join(dir, hosts.HOST_NAME)), second.payloads[second.host.asset]);
92
+ fs.writeFileSync(path.join(dir, hosts.HOST_NAME + '.LICENSES.txt'), 'changed');
93
+ await assert.rejects(_internal.installCompiledHost(installOptions(dir, first)), /modified/);
94
+ assert.deepEqual(fs.readFileSync(path.join(dir, hosts.HOST_NAME)), second.payloads[second.host.asset]);
95
+ });
96
+ test('explicit unavailable host fails before any CLI destination or download changes', async t => {
97
+ const dir = await temporary(t), f = fixture(), cli = path.join(dir, 'codewhale'), alias = path.join(dir, 'codew');
98
+ fs.writeFileSync(cli, 'old-cli'); fs.writeFileSync(alias, 'old-alias');
99
+ let downloads = 0;
100
+ const source = 'https://fixture.invalid/';
101
+ const checksums = new Map([['codewhale-macos-x64', 'a'.repeat(64)], ['codew-macos-x64', 'a'.repeat(64)], [hosts.HOST_CATALOG, hosts.sha256(Buffer.from(f.text))]]);
102
+ await assert.rejects(run({ releaseDir: dir, paths: { codewhale: { target: cli, asset: 'codewhale-macos-x64' }, codew: { target: alias, asset: 'codew-macos-x64' } }, platform: 'darwin', arch: 'x64', env: { CODEWHALE_VERSION: '0.10.1', CODEWHALE_RELEASE_BASE_URL: source, CODEWHALE_INSTALL_COMPILED_HOST: '1' }, fetchText: async url => url.endsWith(hosts.HOST_CATALOG) ? f.text : [...checksums].map(([name, hash]) => `${hash} ${name}`).join('\n'), download: async () => { downloads++; } }), /no qualified image/);
103
+ assert.equal(downloads, 0);
104
+ assert.equal(fs.readFileSync(cli, 'utf8'), 'old-cli'); assert.equal(fs.readFileSync(alias, 'utf8'), 'old-alias');
105
+ });
106
+ module.exports = { fixture };
107
+
108
+ test('Windows delivery requires containment and memory plus all seven LPAC cases', () => {
109
+ const f = fixture('windows-x64');
110
+ f.catalog.hosts[0].native_passed = 8;
111
+ assert.throws(() => hosts.parseCatalog(f.catalog), /Native compiled-image/);
112
+ f.catalog.hosts[0].native_passed = 9;
113
+ assert.equal(hosts.parseCatalog(f.catalog).hosts[0].passed, 5);
114
+ // Cross-source contract proof; actual PowerShell installation is separate.
115
+ const installer = fs.readFileSync(path.resolve(__dirname, '../../../scripts/release/install.ps1'), 'utf8');
116
+ for (const target of ['windows-x64', 'windows-arm64']) {
117
+ assert.match(installer, new RegExp(`\\$hostEntry\\.passed -ne ${hosts.compiledMinimum(target)}(?: |\\))`));
118
+ assert.match(installer, new RegExp(`\\$hostEntry\\.native_passed -lt ${hosts.nativeMinimum(target)}(?: |\\))`));
119
+ }
120
+ });
121
+
122
+ test('npm fresh publication never overwrites a file created after its identity check', async t => {
123
+ const dir = await temporary(t), f = fixture(), prepared = await _internal.prepareCompiledHost(installOptions(dir, f));
124
+ const original = fs.promises.link;
125
+ fs.promises.link = async (source, destination) => {
126
+ await fs.promises.writeFile(destination, 'another writer');
127
+ return original(source, destination);
128
+ };
129
+ try { await assert.rejects(prepared.publish(), /EEXIST/); }
130
+ finally { fs.promises.link = original; await prepared.cleanup(); }
131
+ assert.equal(fs.readFileSync(path.join(dir, hosts.HOST_NAME), 'utf8'), 'another writer');
132
+ });
133
+
134
+ // Fixture receipts verify contract refusal only; they are not Native OS proof.
135
+ test('every target requires its exact direct-image cases and separate Native memory proof without skips', () => {
136
+ for (const target of Object.keys(hosts.TARGETS)) {
137
+ const f = fixture(target), host = f.catalog.hosts[0];
138
+ const expectedDirect = target.startsWith('macos-') ? 6 : 5;
139
+ const expectedNative = target.startsWith('windows-') ? 9 : 2;
140
+ assert.equal(hosts.compiledMinimum(target), expectedDirect);
141
+ assert.equal(hosts.nativeMinimum(target), expectedNative);
142
+ assert.equal(hosts.parseCatalog(f.catalog).hosts[0].passed, expectedDirect);
143
+ for (const count of [expectedDirect - 1, expectedDirect + 1]) {
144
+ host.passed = count;
145
+ assert.throws(() => hosts.parseCatalog(f.catalog), /compiled-image qualification/);
146
+ }
147
+ host.passed = expectedDirect;
148
+ host.native_passed = expectedNative - 1;
149
+ assert.throws(() => hosts.parseCatalog(f.catalog), /Native compiled-image/);
150
+ host.native_passed = expectedNative;
151
+ host.skipped = 1;
152
+ assert.throws(() => hosts.parseCatalog(f.catalog), /compiled-image qualification/);
153
+ host.skipped = 0; host.native_skipped = 1;
154
+ assert.throws(() => hosts.parseCatalog(f.catalog), /Native compiled-image/);
155
+ }
156
+ });