codewhale 0.10.0 → 0.10.1

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/scripts/run.js CHANGED
@@ -1,5 +1,6 @@
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");
3
4
 
4
5
  const pkg = require("../package.json");
5
6
 
@@ -7,22 +8,119 @@ function isVersionFlag(args = process.argv.slice(2)) {
7
8
  return args.includes("--version") || args.includes("-V");
8
9
  }
9
10
 
10
- function printVersionFallback(binaryName) {
11
+ // Print the install hint (mirror / release-base guidance) for a failure that
12
+ // looks like a download problem. Prints nothing for other failures.
13
+ function printInstallFailureHint(error, log = console.error) {
14
+ const hint = installFailureHint(error);
15
+ if (hint) {
16
+ log(hint);
17
+ }
18
+ }
19
+
20
+ // Shared by the `codewhale` and `codew` bin shims: the error and, when the
21
+ // binary could not be downloaded, the hint that says what to do about it.
22
+ function reportStartFailure(binaryName, error, log = console.error) {
23
+ log(`Failed to start ${binaryName}:`, error && error.message ? error.message : String(error));
24
+ printInstallFailureHint(error, log);
25
+ }
26
+
27
+ // `--version` must still answer when the native binary is missing, but it
28
+ // must not report a binary version that is not actually installed: the
29
+ // expected version goes to stdout labelled as such, the failure to stderr.
30
+ function printVersionFallback(binaryName, error) {
11
31
  const binVersion =
12
32
  process.env.CODEWHALE_VERSION ||
13
33
  process.env.DEEPSEEK_TUI_VERSION ||
14
34
  process.env.DEEPSEEK_VERSION ||
15
35
  pkg.codewhaleBinaryVersion || pkg.deepseekBinaryVersion || pkg.version;
16
36
  console.log(`${binaryName} (npm wrapper) v${pkg.version}`);
17
- console.log(`binary version: v${binVersion}`);
37
+ console.log(`binary: not installed (expected v${binVersion})`);
18
38
  console.log(`repo: ${pkg.repository?.url || "N/A"}`);
39
+ if (error) {
40
+ console.error(`${binaryName}: native binary unavailable: ${error.message || String(error)}`);
41
+ printInstallFailureHint(error);
42
+ }
43
+ }
44
+
45
+ // Signals that end the wrapper must end the native binary too. With a
46
+ // blocking spawn the wrapper died on SIGTERM and left the child running (still
47
+ // executing tools, still holding its port); as PID 1 in a container it ignored
48
+ // SIGTERM entirely. So SIGTERM, which a terminal never sends, is forwarded.
49
+ //
50
+ // SIGINT (Ctrl-C) and SIGHUP (terminal hangup) come from the terminal, which
51
+ // delivers them to the whole foreground process group, native child included.
52
+ // Forwarding them would deliver each one twice, and the native binary treats a
53
+ // second signal as "skip the graceful drain". On Windows `child.kill()` is a
54
+ // hard TerminateProcess, which would race the child's own Ctrl-C cleanup. The
55
+ // wrapper only has to outlive them, then mirror how the child ended.
56
+ const FORWARDED_SIGNALS = process.platform === "win32" ? [] : ["SIGTERM"];
57
+ const OUTLIVED_SIGNALS = ["SIGINT", "SIGHUP"];
58
+
59
+ function ignoreSignal() {}
60
+
61
+ // Run the native binary and settle with how it ended. While it runs, the
62
+ // forwarded signals go to the child and the outlived ones do not kill the
63
+ // wrapper.
64
+ function runChild(spawn, binaryPath, args, proc) {
65
+ return new Promise((resolve) => {
66
+ let child;
67
+ try {
68
+ child = spawn(binaryPath, args, { stdio: "inherit" });
69
+ } catch (error) {
70
+ resolve({ error });
71
+ return;
72
+ }
73
+ const listeners = [
74
+ ...FORWARDED_SIGNALS.map((signal) => [
75
+ signal,
76
+ () => {
77
+ try {
78
+ child.kill(signal);
79
+ } catch {
80
+ // The child already exited; its exit event settles the run.
81
+ }
82
+ },
83
+ ]),
84
+ ...OUTLIVED_SIGNALS.map((signal) => [signal, ignoreSignal]),
85
+ ];
86
+ for (const [signal, listener] of listeners) {
87
+ proc.on(signal, listener);
88
+ }
89
+ let settled = false;
90
+ const settle = (result) => {
91
+ if (settled) return;
92
+ settled = true;
93
+ for (const [signal, listener] of listeners) {
94
+ proc.removeListener(signal, listener);
95
+ }
96
+ resolve(result);
97
+ };
98
+ child.once("error", (error) => settle({ error }));
99
+ child.once("exit", (status, signal) => settle({ status, signal }));
100
+ });
101
+ }
102
+
103
+ // Mirror a signal death to the parent shell: re-raise it on the wrapper once
104
+ // our listeners are gone, and fall back to the conventional 128 + n.
105
+ function exitLikeChild(result, exit, proc) {
106
+ if (result.signal) {
107
+ try {
108
+ proc.kill(proc.pid, result.signal);
109
+ } catch {
110
+ // fall through to the numeric status
111
+ }
112
+ const number = os.constants.signals[result.signal];
113
+ return exit(number ? 128 + number : 1);
114
+ }
115
+ return exit(result.status ?? 1);
19
116
  }
20
117
 
21
118
  async function run(binaryName, options = {}) {
22
119
  const args = options.args || process.argv.slice(2);
23
120
  const resolveBinaryPath = options.getBinaryPath || getBinaryPath;
24
- const spawn = options.spawnSync || spawnSync;
121
+ const spawn = options.spawn || spawnChild;
25
122
  const exit = options.exit || process.exit;
123
+ const proc = options.process || process;
26
124
  const versionFlag = isVersionFlag(args);
27
125
 
28
126
  let binaryPath;
@@ -30,23 +128,21 @@ async function run(binaryName, options = {}) {
30
128
  binaryPath = await resolveBinaryPath(binaryName);
31
129
  } catch (error) {
32
130
  if (versionFlag) {
33
- printVersionFallback(binaryName);
131
+ printVersionFallback(binaryName, error);
34
132
  return exit(0);
35
133
  }
36
134
  throw error;
37
135
  }
38
136
 
39
- const result = spawn(binaryPath, args, {
40
- stdio: "inherit",
41
- });
137
+ const result = await runChild(spawn, binaryPath, args, proc);
42
138
  if (result.error) {
43
139
  if (versionFlag) {
44
- printVersionFallback(binaryName);
140
+ printVersionFallback(binaryName, result.error);
45
141
  return exit(0);
46
142
  }
47
143
  throw result.error;
48
144
  }
49
- return exit(result.status ?? 1);
145
+ return exitLikeChild(result, exit, proc);
50
146
  }
51
147
 
52
148
  async function runCodeWhale() {
@@ -65,14 +161,21 @@ module.exports = {
65
161
  run,
66
162
  runCodeWhale,
67
163
  runCodeWhaleTui,
68
- _internal: { isVersionFlag, printVersionFallback },
164
+ reportStartFailure,
165
+ _internal: { isVersionFlag, printVersionFallback, FORWARDED_SIGNALS, OUTLIVED_SIGNALS },
69
166
  };
70
167
 
71
168
  if (require.main === module) {
72
169
  const command = process.argv[1] || "";
73
170
  if (command.includes("tui")) {
74
- runCodeWhaleTui();
171
+ runCodeWhaleTui().catch((error) => {
172
+ reportStartFailure("codewhale", error);
173
+ process.exit(1);
174
+ });
75
175
  } else {
76
- runCodeWhale();
176
+ runCodeWhale().catch((error) => {
177
+ reportStartFailure("codewhale", error);
178
+ process.exit(1);
179
+ });
77
180
  }
78
181
  }
@@ -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,
@@ -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
+ });