agentscm 0.0.0-stage → 0.1.0-alpha.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.
- package/LICENSE +105 -0
- package/NOTICE +7 -0
- package/README.md +21 -2
- package/SHA256SUMS +5 -0
- package/THIRD_PARTY_NOTICES.md +9813 -0
- package/bin/agentscm.js +9 -0
- package/lib/install.js +200 -0
- package/package.json +13 -4
package/bin/agentscm.js
ADDED
package/lib/install.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const crypto = require('node:crypto');
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const os = require('node:os');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const { execFileSync, spawnSync } = require('node:child_process');
|
|
7
|
+
|
|
8
|
+
const REPO = 'counterbranch/agentscm';
|
|
9
|
+
const SUMS_MAX = 64 * 1024;
|
|
10
|
+
const BINARY_MAX = 200 * 1024 * 1024;
|
|
11
|
+
const BUDGET_MS = 600_000; // overall, for both downloads
|
|
12
|
+
|
|
13
|
+
const NOTICE_TEXT = `npx agentscm downloads the AgentSCM release binary for your platform from
|
|
14
|
+
GitHub Releases, verifies its SHA-256 checksum and build provenance, and then
|
|
15
|
+
starts the setup guide. It does not run install scripts, send telemetry or
|
|
16
|
+
change anything without asking. Everything it writes is listed before it
|
|
17
|
+
writes it, and can be undone with \`agentscm uninstall\`.`;
|
|
18
|
+
|
|
19
|
+
const TARGETS = {
|
|
20
|
+
'linux/x64': 'x86_64-unknown-linux-musl',
|
|
21
|
+
'linux/arm64': 'aarch64-unknown-linux-musl',
|
|
22
|
+
'darwin/x64': 'x86_64-apple-darwin',
|
|
23
|
+
'darwin/arm64': 'aarch64-apple-darwin',
|
|
24
|
+
'win32/x64': 'x86_64-pc-windows-msvc',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const targetFor = (platform, arch) => Object.hasOwn(TARGETS, `${platform}/${arch}`) ? TARGETS[`${platform}/${arch}`] : null;
|
|
28
|
+
|
|
29
|
+
// The release asset for a target; the Windows binary carries an .exe suffix.
|
|
30
|
+
const assetNameFor = (target) => `agentscm-${target}${target.endsWith('-windows-msvc') ? '.exe' : ''}`;
|
|
31
|
+
|
|
32
|
+
// File names in the install dir. Windows has no unprivileged symlinks, so "latest" is a copy there.
|
|
33
|
+
const installNames = (version, platform) => platform === 'win32'
|
|
34
|
+
? { versioned: `agentscm-${version}.exe`, latest: 'agentscm.exe', symlink: false }
|
|
35
|
+
: { versioned: `agentscm-${version}`, latest: 'agentscm', symlink: true };
|
|
36
|
+
|
|
37
|
+
// The download host is fixed on purpose: no env var or flag may change it.
|
|
38
|
+
const urlFor = (version, asset) => `https://github.com/${REPO}/releases/download/v${version}/${asset}`;
|
|
39
|
+
|
|
40
|
+
// A bare `npx agentscm` or a leading flag starts the setup guide; any other first argument is a subcommand.
|
|
41
|
+
// The binary's own top-level flags pass through; any other leading flag goes to the setup guide.
|
|
42
|
+
const TOP_LEVEL_FLAGS = new Set(['--version', '-V', '--help', '-h']);
|
|
43
|
+
const launchArgs = (args) =>
|
|
44
|
+
args.length === 0 || (args[0].startsWith('-') && !TOP_LEVEL_FLAGS.has(args[0])) ? ['quickstart', ...args] : args;
|
|
45
|
+
|
|
46
|
+
// Strict: every line must be `<64 lowercase hex> agentscm-<target>[.exe]`, and the asset appears exactly once.
|
|
47
|
+
function parseSums(text, target) {
|
|
48
|
+
const hashes = [];
|
|
49
|
+
for (const line of text.replace(/\n$/, '').split('\n')) {
|
|
50
|
+
const m = /^([0-9a-f]{64}) {2}(agentscm-[a-z0-9_-]+(?:\.exe)?)$/.exec(line);
|
|
51
|
+
if (!m) throw new Error('SHA256SUMS has a malformed line');
|
|
52
|
+
if (m[2] === assetNameFor(target)) hashes.push(m[1]);
|
|
53
|
+
}
|
|
54
|
+
if (hashes.length !== 1) throw new Error(`SHA256SUMS must list ${assetNameFor(target)} exactly once`);
|
|
55
|
+
return hashes[0];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const sha256File = (file) => crypto.createHash('sha256').update(fs.readFileSync(file)).digest('hex');
|
|
59
|
+
const matches = (file, expected) => sha256File(file) === expected;
|
|
60
|
+
|
|
61
|
+
async function download(url, max, onChunk, signal) {
|
|
62
|
+
const res = await fetch(url, { signal });
|
|
63
|
+
if (!res.url.startsWith('https://')) throw new Error('download redirected to a non-https URL');
|
|
64
|
+
if (!res.ok) throw new Error(`download failed (HTTP ${res.status}): ${url}`);
|
|
65
|
+
let total = 0;
|
|
66
|
+
for await (const chunk of res.body) {
|
|
67
|
+
total += chunk.length;
|
|
68
|
+
if (total > max) throw new Error(`download exceeds the ${max} byte limit: ${url}`);
|
|
69
|
+
onChunk(chunk);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Checksums shipped inside the npm package come from the attested build artifact, not the release assets (see docs/install.md).
|
|
74
|
+
function localSums(pkgDir) {
|
|
75
|
+
try {
|
|
76
|
+
return fs.readFileSync(path.join(pkgDir, 'SHA256SUMS'), 'utf8');
|
|
77
|
+
} catch (err) {
|
|
78
|
+
if (err.code === 'ENOENT') return null;
|
|
79
|
+
throw err;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Pinned to this repository's release workflow and tag, so a binary attested by any other workflow fails.
|
|
84
|
+
const ghVerifyArgs = (file, version) => [
|
|
85
|
+
'attestation', 'verify', file, '--repo', REPO,
|
|
86
|
+
'--signer-workflow', `${REPO}/.github/workflows/release.yml`,
|
|
87
|
+
'--source-ref', `refs/tags/v${version}`,
|
|
88
|
+
'--deny-self-hosted-runners',
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
function unsupported(version) {
|
|
92
|
+
return `no AgentSCM release binary for ${process.platform}/${process.arch}.
|
|
93
|
+
Use Docker instead:
|
|
94
|
+
docker run --rm -it ghcr.io/${REPO}:${version} --help
|
|
95
|
+
See https://github.com/${REPO}/blob/v${version}/docs/install.md`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Why provenance cannot be checked, or null when gh is installed and signed in.
|
|
99
|
+
function provenanceGap(run = spawnSync) {
|
|
100
|
+
if (run('gh', ['--version'], { stdio: 'ignore' }).error) return 'the GitHub CLI (gh) is not installed';
|
|
101
|
+
if (run('gh', ['auth', 'status', '--hostname', 'github.com'], { stdio: 'ignore', timeout: 30_000 }).status !== 0) {
|
|
102
|
+
return 'the GitHub CLI (gh) is not signed in to github.com or cannot reach it (see `gh auth status`)';
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Throws if the attestation does not verify. With no usable gh it only warns, unless AGENTSCM_REQUIRE_PROVENANCE=1.
|
|
108
|
+
function checkProvenance(file, version, gap = provenanceGap()) {
|
|
109
|
+
if (gap) {
|
|
110
|
+
if (process.env.AGENTSCM_REQUIRE_PROVENANCE === '1') throw new Error(`AGENTSCM_REQUIRE_PROVENANCE=1 but ${gap}`);
|
|
111
|
+
console.log(`Checksum-verified only: provenance was not checked (${gap}).`);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
execFileSync('gh', ghVerifyArgs(file, version), { stdio: 'inherit' });
|
|
116
|
+
} catch {
|
|
117
|
+
throw new Error('build provenance verification failed; not running the binary');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function main(args) {
|
|
122
|
+
const version = require('../package.json').version;
|
|
123
|
+
console.log(`${NOTICE_TEXT}\n`);
|
|
124
|
+
const target = targetFor(process.platform, process.arch);
|
|
125
|
+
if (!target) {
|
|
126
|
+
console.error(`agentscm: ${unsupported(version)}`);
|
|
127
|
+
return 1;
|
|
128
|
+
}
|
|
129
|
+
const asset = assetNameFor(target);
|
|
130
|
+
const names = installNames(version, process.platform);
|
|
131
|
+
const signal = AbortSignal.timeout(BUDGET_MS);
|
|
132
|
+
const gap = provenanceGap(); // checked once: `gh auth status` goes over the network
|
|
133
|
+
if (gap && process.env.AGENTSCM_REQUIRE_PROVENANCE === '1') throw new Error(`AGENTSCM_REQUIRE_PROVENANCE=1 but ${gap}`);
|
|
134
|
+
|
|
135
|
+
let sums = localSums(path.join(__dirname, '..'));
|
|
136
|
+
if (sums === null) {
|
|
137
|
+
const parts = [];
|
|
138
|
+
await download(urlFor(version, 'SHA256SUMS'), SUMS_MAX, (c) => parts.push(c), signal);
|
|
139
|
+
sums = Buffer.concat(parts).toString('utf8');
|
|
140
|
+
console.log('Checksums from the GitHub release.');
|
|
141
|
+
} else {
|
|
142
|
+
console.log('Checksums from the npm package.');
|
|
143
|
+
}
|
|
144
|
+
const expected = parseSums(sums, target);
|
|
145
|
+
|
|
146
|
+
const dir = path.join(os.homedir(), '.agentscm', 'bin');
|
|
147
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
148
|
+
fs.chmodSync(dir, 0o700);
|
|
149
|
+
const link = path.join(dir, names.latest);
|
|
150
|
+
const lst = fs.lstatSync(link, { throwIfNoEntry: false });
|
|
151
|
+
if (names.symlink && lst && !lst.isSymbolicLink()) throw new Error(`${link} exists and is not a symlink; remove it`);
|
|
152
|
+
|
|
153
|
+
// Order: checksum, provenance, chmod, rename, link, exec. Nothing is installed or linked before both checks pass.
|
|
154
|
+
const binary = path.join(dir, names.versioned);
|
|
155
|
+
const rand = crypto.randomBytes(6).toString('hex');
|
|
156
|
+
if (fs.existsSync(binary)) {
|
|
157
|
+
if (!matches(binary, expected)) throw new Error(`${binary} does not match the release checksum; delete it and retry`);
|
|
158
|
+
checkProvenance(binary, version, gap);
|
|
159
|
+
console.log(`Using the verified ${binary}`);
|
|
160
|
+
} else {
|
|
161
|
+
const tmp = path.join(dir, `.agentscm-${version}-${rand}.tmp`);
|
|
162
|
+
try {
|
|
163
|
+
const fd = fs.openSync(tmp, 'wx', 0o700);
|
|
164
|
+
try {
|
|
165
|
+
await download(urlFor(version, asset), BINARY_MAX, (c) => fs.writeSync(fd, c), signal);
|
|
166
|
+
} finally {
|
|
167
|
+
fs.closeSync(fd);
|
|
168
|
+
}
|
|
169
|
+
if (!matches(tmp, expected)) throw new Error(`checksum mismatch for ${asset}; nothing was installed`);
|
|
170
|
+
checkProvenance(tmp, version, gap);
|
|
171
|
+
fs.chmodSync(tmp, 0o755);
|
|
172
|
+
fs.renameSync(tmp, binary);
|
|
173
|
+
} finally {
|
|
174
|
+
fs.rmSync(tmp, { force: true });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const tmpLink = path.join(dir, `.agentscm-link-${rand}`);
|
|
179
|
+
if (names.symlink) {
|
|
180
|
+
fs.symlinkSync(path.basename(binary), tmpLink);
|
|
181
|
+
fs.renameSync(tmpLink, link);
|
|
182
|
+
} else {
|
|
183
|
+
try {
|
|
184
|
+
fs.copyFileSync(binary, tmpLink);
|
|
185
|
+
fs.renameSync(tmpLink, link);
|
|
186
|
+
} catch (err) {
|
|
187
|
+
// A running agentscm.exe is locked; the versioned binary is what runs below.
|
|
188
|
+
if (!['EPERM', 'EBUSY', 'EACCES'].includes(err.code)) throw err;
|
|
189
|
+
console.warn(`agentscm: could not replace ${link} (in use?); it keeps the previous version`);
|
|
190
|
+
} finally {
|
|
191
|
+
fs.rmSync(tmpLink, { force: true });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const r = spawnSync(binary, launchArgs(args), { stdio: 'inherit' });
|
|
196
|
+
if (r.error) throw r.error;
|
|
197
|
+
return r.status ?? 128 + (os.constants.signals[r.signal] ?? 0);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
module.exports = { NOTICE_TEXT, targetFor, assetNameFor, installNames, urlFor, launchArgs, parseSums, sha256File, matches, localSums, ghVerifyArgs, provenanceGap, checkProvenance, main };
|
package/package.json
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agentscm",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
}
|
|
3
|
+
"version": "0.1.0-alpha.2",
|
|
4
|
+
"description": "Downloads, verifies and starts the AgentSCM release binary.",
|
|
5
|
+
"license": "FSL-1.1-ALv2",
|
|
6
|
+
"repository": {"type": "git", "url": "git+https://github.com/counterbranch/agentscm.git"},
|
|
7
|
+
"homepage": "https://github.com/counterbranch/agentscm",
|
|
8
|
+
"bin": {"agentscm": "bin/agentscm.js"},
|
|
9
|
+
"files": ["bin", "lib", "README.md", "LICENSE", "NOTICE", "THIRD_PARTY_NOTICES.md", "SHA256SUMS"],
|
|
10
|
+
"engines": {"node": ">=18"},
|
|
11
|
+
"publishConfig": {"provenance": true, "access": "public"},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "node --test"
|
|
14
|
+
}
|
|
15
|
+
}
|