subconscious-cli 6.1.0 → 6.2.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.
@@ -1,182 +0,0 @@
1
- import { createHash, randomUUID } from 'node:crypto';
2
- import fs from 'node:fs/promises';
3
- import os from 'node:os';
4
- import path from 'node:path';
5
- import { agentById, installCommands } from '../agent-data.js';
6
- import { powershellCommand, runWindows } from './process.js';
7
-
8
- const marathon = agentById('subconscious-code').install;
9
- const repository = marathon.repository;
10
-
11
- function npmInstall(command) {
12
- const match = /^npm (?:i|install) -g ([@a-zA-Z0-9/._-]+)$/.exec(
13
- command || '',
14
- );
15
- return match ? { command: 'npm', args: ['install', '-g', match[1]] } : null;
16
- }
17
-
18
- export function windowsInstallSpec(id, env = process.env) {
19
- const install = installCommands(agentById(id)) || {};
20
- const command = install.win32;
21
- if (id === 'subconscious-code')
22
- return { nativeRelease: true, display: 'subc marathon install' };
23
- if (!command) return null; // Never fall back to a Linux installer.
24
- if (id === 'claude-code') {
25
- const primary = powershellCommand(
26
- "$ErrorActionPreference = 'Stop'; Invoke-RestMethod 'https://claude.ai/install.ps1' | Invoke-Expression; if (-not $?) { exit 1 }",
27
- env,
28
- );
29
- return {
30
- ...primary,
31
- display: command,
32
- fallback: npmInstall(install.fallback),
33
- };
34
- }
35
- const spec = npmInstall(command);
36
- if (!spec) throw new Error(`No safe Windows installer is defined for ${id}`);
37
- return { ...spec, display: command };
38
- }
39
-
40
- export function windowsReleaseAsset(release, arch = process.arch) {
41
- const target = marathon.windows_targets.find((triple) =>
42
- triple.startsWith({ x64: 'x86_64-', arm64: 'aarch64-' }[arch] ?? '-'),
43
- );
44
- if (!target)
45
- throw new Error(
46
- `Subconscious Code does not support Windows architecture ${arch}`,
47
- );
48
- // Pinned older releases are installed under the new executable name too.
49
- // Never resolve or overwrite Windows' own sc.exe.
50
- const names = marathon.windows_assets.map((name) =>
51
- name.replaceAll('{target}', target),
52
- );
53
- const asset = names
54
- .map((name) => release.assets?.find((asset) => asset.name === name))
55
- .find(Boolean);
56
- if (!asset)
57
- throw new Error(
58
- `Marathon ${release.tag_name || 'latest'} has no published native Windows ${arch} binary for subc marathon install.`,
59
- );
60
- const checksum = release.assets.find(
61
- (candidate) => candidate.name === `${asset.name}.sha256`,
62
- );
63
- if (!checksum)
64
- throw new Error(
65
- `The Windows release is missing ${asset.name}.sha256; refusing an unverified installation.`,
66
- );
67
- return { asset, checksum };
68
- }
69
-
70
- export async function installWindowsSC(
71
- env,
72
- {
73
- fetchImpl = fetch,
74
- run = runWindows,
75
- home = os.homedir(),
76
- arch = process.arch,
77
- log = console.log,
78
- } = {},
79
- ) {
80
- const version = env.SC_CODE_VERSION?.replace(/^v/, '');
81
- const endpoint = version
82
- ? `tags/${encodeURIComponent(`v${version}`)}`
83
- : 'latest';
84
- const response = await fetchImpl(
85
- `https://api.github.com/repos/${repository}/releases/${endpoint}`,
86
- {
87
- headers: { Accept: 'application/vnd.github+json' },
88
- signal: AbortSignal.timeout(15000),
89
- },
90
- );
91
- if (!response.ok)
92
- throw new Error(
93
- `Could not find a Subconscious Code release (HTTP ${response.status}).`,
94
- );
95
- const release = await response.json();
96
- const { asset, checksum } = windowsReleaseAsset(release, arch);
97
- const download = async (asset) => {
98
- const url = new URL(asset.browser_download_url);
99
- if (
100
- url.origin !== 'https://github.com' ||
101
- !url.pathname.startsWith(`/${repository}/releases/download/`)
102
- )
103
- throw new Error('Unexpected release download URL');
104
- const response = await fetchImpl(url.href, {
105
- signal: AbortSignal.timeout(120000),
106
- });
107
- if (!response.ok)
108
- throw new Error(
109
- `Download failed (HTTP ${response.status}): ${asset.name}`,
110
- );
111
- return Buffer.from(await response.arrayBuffer());
112
- };
113
- log(`Downloading Marathon ${release.tag_name} for Windows ${arch}...`);
114
- const [binary, checksumText] = await Promise.all([
115
- download(asset),
116
- download(checksum),
117
- ]);
118
- const expected = checksumText.toString('utf8').trim().split(/\s+/)[0];
119
- if (
120
- !/^[a-f0-9]{64}$/i.test(expected) ||
121
- createHash('sha256').update(binary).digest('hex') !== expected.toLowerCase()
122
- )
123
- throw new Error('Subconscious Code checksum verification failed');
124
- const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'subc-sc-install-'));
125
- const installDir = path.resolve(
126
- env.SC_INSTALL_DIR || path.join(home, '.local', 'bin'),
127
- );
128
- const destination = path.join(installDir, 'marathon.exe');
129
- const staging = path.join(installDir, `marathon-${randomUUID()}.tmp`);
130
- try {
131
- const extracted = path.join(dir, 'marathon.exe');
132
- if (asset.name.endsWith('.zip')) {
133
- const archive = path.join(dir, 'release.zip');
134
- await fs.writeFile(archive, binary, { flag: 'wx' });
135
- // Extract only the exact root executable. No archive-controlled paths.
136
- const executable = asset.name.startsWith('marathon-')
137
- ? 'marathon.exe'
138
- : 'sc.exe';
139
- const spec = powershellCommand(
140
- "$ErrorActionPreference = 'Stop'; Add-Type -AssemblyName System.IO.Compression.FileSystem; $z = [IO.Compression.ZipFile]::OpenRead($env:SUBC_ARCHIVE); try { $e = $z.GetEntry($env:SUBC_EXECUTABLE); if ($null -eq $e) { throw 'Archive has no expected root executable' }; [IO.Compression.ZipFileExtensions]::ExtractToFile($e, $env:SUBC_EXTRACTED, $false) } finally { $z.Dispose() }",
141
- {
142
- ...env,
143
- SUBC_ARCHIVE: archive,
144
- SUBC_EXTRACTED: extracted,
145
- SUBC_EXECUTABLE: executable,
146
- },
147
- );
148
- if (await run(spec.command, spec.args, { env: spec.env }))
149
- throw new Error('Could not extract the Windows release');
150
- } else await fs.writeFile(extracted, binary, { flag: 'wx' });
151
- const magic = (await fs.readFile(extracted))
152
- .subarray(0, 2)
153
- .toString('ascii');
154
- if (magic !== 'MZ') throw new Error('Release is not a Windows executable');
155
- await fs.mkdir(installDir, { recursive: true });
156
- await fs.copyFile(extracted, staging);
157
- await fs.rename(staging, destination);
158
- log(`Installed ${destination}. Run subc marathon to launch it.`);
159
- return 0;
160
- } finally {
161
- await fs.rm(staging, { force: true });
162
- await fs.rm(dir, { recursive: true, force: true });
163
- }
164
- }
165
-
166
- export async function installWindowsAgent(id, env, run = runWindows) {
167
- const spec = windowsInstallSpec(id, env);
168
- if (!spec) throw new Error('Install this agent separately, then rerun subc.');
169
- if (spec.nativeRelease) return installWindowsSC(env, { run });
170
- let code;
171
- try {
172
- code = await run(spec.command, spec.args, { env: spec.env || env });
173
- } catch (error) {
174
- if (!spec.fallback) throw error;
175
- code = 1;
176
- }
177
- if (code && spec.fallback) {
178
- console.error('Native installer failed; trying the npm package.');
179
- code = await run(spec.fallback.command, spec.fallback.args, { env });
180
- }
181
- return code;
182
- }