@standardagents/code 0.14.2 → 0.14.3-canary.6cc75f
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/lib/artifact.mjs +172 -27
- package/lib/install.mjs +93 -0
- package/lib/launcher.mjs +78 -4
- package/package.json +20 -10
- package/postinstall.mjs +0 -12
package/lib/artifact.mjs
CHANGED
|
@@ -84,10 +84,114 @@ export function verifyArtifactTree(root, files, metadata = ['manifest.json', 'ma
|
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
// Check a native signed archive's listing before extraction: `names` from
|
|
88
|
+
// `tar -tzf` and the entry type letters from `tar -tvzf`. The archive must be
|
|
89
|
+
// exactly one of the signed layouts, with only regular files and the
|
|
90
|
+
// directories those files need. Returns the matching file list.
|
|
91
|
+
export function nativeArchiveLayout(names, types) {
|
|
92
|
+
const listed = names.map(name => name.replace(/^\.\//, '')).sort()
|
|
93
|
+
const layout = [ARTIFACT_FILES, BUNDLE_ARTIFACT_FILES, RUNNER_ARTIFACT_FILES, BUNDLE_RUNNER_ARTIFACT_FILES].find(files => {
|
|
94
|
+
const expected = [...artifactDirectories(files).map(path => `${path}/`), ...files, 'manifest.json', 'manifest.sig'].sort()
|
|
95
|
+
return listed.join('\n') === expected.join('\n')
|
|
96
|
+
})
|
|
97
|
+
if (!layout) throw new Error('native artifact archive has unexpected paths')
|
|
98
|
+
const expectedTypes = '-'.repeat(layout.length + 2) + 'd'.repeat(artifactDirectories(layout).length)
|
|
99
|
+
if ([...types].sort().join('') !== expectedTypes) {
|
|
100
|
+
throw new Error('native artifact archive must contain only regular files and expected directories')
|
|
101
|
+
}
|
|
102
|
+
return layout
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// The four native targets the launcher has ever installed. The two deferred
|
|
106
|
+
// ones stay so their published history verifies.
|
|
107
|
+
export const NATIVE_ARTIFACT_TARGETS = Object.freeze([
|
|
88
108
|
'aarch64-apple-darwin', 'x86_64-apple-darwin',
|
|
89
109
|
'aarch64-unknown-linux-gnu', 'x86_64-unknown-linux-gnu',
|
|
90
110
|
])
|
|
111
|
+
// The browser viewer runtime: a fifth, distinct artifact kind that the same
|
|
112
|
+
// build workers compile and sign. It never installs through the launcher.
|
|
113
|
+
export const WEB_ARTIFACT_TARGET = 'wasm32-unknown-unknown'
|
|
114
|
+
export const ARTIFACT_TARGETS = Object.freeze([...NATIVE_ARTIFACT_TARGETS, WEB_ARTIFACT_TARGET])
|
|
115
|
+
|
|
116
|
+
// Web artifact (schema 4): the wasm-bindgen output, the bundled fonts, their
|
|
117
|
+
// licenses, and the build record, at the artifact root. The font and license
|
|
118
|
+
// sets vary with the pinned font list, so the manifest enumerates what the
|
|
119
|
+
// build produced and the verifier checks each path against these rules.
|
|
120
|
+
export const WEB_ARTIFACT_FORMAT = 'standard-npm-web-target-v1'
|
|
121
|
+
export const WEB_ARTIFACT_SCHEMA = 4
|
|
122
|
+
export const WEB_ARTIFACT_REQUIRED_FILES = Object.freeze([
|
|
123
|
+
'build.json', 'pkg/standard_web.js', 'pkg/standard_web_bg.wasm',
|
|
124
|
+
])
|
|
125
|
+
export const WEB_ARTIFACT_OPTIONAL_FILES = Object.freeze([
|
|
126
|
+
'pkg/standard_web.d.ts', 'pkg/standard_web_bg.wasm.d.ts',
|
|
127
|
+
])
|
|
128
|
+
// One level deep, one file kind each; at least one entry of each.
|
|
129
|
+
export const WEB_ARTIFACT_DIRECTORIES = Object.freeze({ fonts: /\.ttf$/, licenses: /\.txt$/ })
|
|
130
|
+
export const WEB_ARTIFACT_FILE_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]*$/
|
|
131
|
+
// The sample plugins' browser artifacts (packages/web/scripts/build-plugins.mjs),
|
|
132
|
+
// optional: the Worker runtime and index at plugins/, and one directory per
|
|
133
|
+
// plugin id holding its manifest, component, transpiled glue and core modules.
|
|
134
|
+
export const WEB_ARTIFACT_PLUGIN_FILES = Object.freeze([
|
|
135
|
+
'plugins/worker.js', 'plugins/runtime.js', 'plugins/samples.json',
|
|
136
|
+
])
|
|
137
|
+
export const WEB_ARTIFACT_PLUGIN_ID = /^[a-z0-9][a-z0-9-]*$/
|
|
138
|
+
export const WEB_ARTIFACT_PLUGIN_FILE = /^[A-Za-z0-9][A-Za-z0-9._-]*\.(?:js|wasm|json)$/
|
|
139
|
+
export const MAX_WEB_ASSET_BYTES = 64 * 1024 * 1024
|
|
140
|
+
|
|
141
|
+
// Whether one artifact-relative path may appear in a web artifact.
|
|
142
|
+
export function webArtifactPathAllowed(path) {
|
|
143
|
+
if (typeof path !== 'string') return false
|
|
144
|
+
const parts = path.split('/')
|
|
145
|
+
if (!parts.every(part => WEB_ARTIFACT_FILE_NAME.test(part))) return false
|
|
146
|
+
if (WEB_ARTIFACT_REQUIRED_FILES.includes(path) || WEB_ARTIFACT_OPTIONAL_FILES.includes(path)) return true
|
|
147
|
+
if (WEB_ARTIFACT_PLUGIN_FILES.includes(path)) return true
|
|
148
|
+
if (parts.length === 3 && parts[0] === 'plugins') {
|
|
149
|
+
return WEB_ARTIFACT_PLUGIN_ID.test(parts[1]) && WEB_ARTIFACT_PLUGIN_FILE.test(parts[2])
|
|
150
|
+
}
|
|
151
|
+
return parts.length === 2 && Boolean(WEB_ARTIFACT_DIRECTORIES[parts[0]]?.test(parts[1]))
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Whether a directory (without its trailing slash) may appear in a web
|
|
155
|
+
// artifact: pkg, the fixed directories, plugins, and one per plugin id.
|
|
156
|
+
export function webArtifactDirectoryAllowed(path) {
|
|
157
|
+
if (path === 'pkg' || path === 'plugins' || Object.hasOwn(WEB_ARTIFACT_DIRECTORIES, path)) return true
|
|
158
|
+
const parts = path.split('/')
|
|
159
|
+
return parts.length === 2 && parts[0] === 'plugins' && WEB_ARTIFACT_PLUGIN_ID.test(parts[1])
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Check a complete web file list: every path allowed, every required file
|
|
163
|
+
// present, each directory non-empty, no duplicates. Returns the sorted list.
|
|
164
|
+
export function webArtifactFileList(paths) {
|
|
165
|
+
if (!Array.isArray(paths) || paths.some(path => !webArtifactPathAllowed(path))) {
|
|
166
|
+
throw new Error('Web artifact file manifest is invalid')
|
|
167
|
+
}
|
|
168
|
+
const files = [...paths].sort()
|
|
169
|
+
if (new Set(files).size !== files.length) throw new Error('Web artifact file manifest is invalid')
|
|
170
|
+
const missing = WEB_ARTIFACT_REQUIRED_FILES.filter(path => !files.includes(path))
|
|
171
|
+
for (const directory of Object.keys(WEB_ARTIFACT_DIRECTORIES)) {
|
|
172
|
+
if (!files.some(path => path.startsWith(`${directory}/`))) missing.push(`${directory}/`)
|
|
173
|
+
}
|
|
174
|
+
if (missing.length) throw new Error(`Web artifact is incomplete: missing ${missing.join(', ')}`)
|
|
175
|
+
return files
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Enumerate a built web artifact root as the file list its manifest binds.
|
|
179
|
+
// Every entry must be a regular file the rules allow; anything else fails.
|
|
180
|
+
export function webArtifactFilesOnDisk(root, metadata = ['manifest.json', 'manifest.sig']) {
|
|
181
|
+
const files = []
|
|
182
|
+
function visit(relative) {
|
|
183
|
+
for (const name of readdirSync(join(root, relative))) {
|
|
184
|
+
const child = relative ? `${relative}/${name}` : name
|
|
185
|
+
if (!relative && metadata.includes(child)) continue
|
|
186
|
+
const entry = lstatSync(join(root, child))
|
|
187
|
+
if (entry.isDirectory() && webArtifactDirectoryAllowed(child)) visit(child)
|
|
188
|
+
else if (!entry.isFile() || !webArtifactPathAllowed(child)) throw new Error(`Unexpected artifact content: ${child}`)
|
|
189
|
+
else files.push(child)
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
visit('')
|
|
193
|
+
return webArtifactFileList(files)
|
|
194
|
+
}
|
|
91
195
|
// Public trust anchor, not a credential. Never obtain the verification key
|
|
92
196
|
// from the package being verified or a mutable registry response.
|
|
93
197
|
export const ARTIFACT_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
|
|
@@ -124,20 +228,47 @@ function boundedBytes(path, limit) {
|
|
|
124
228
|
} finally { closeSync(descriptor) }
|
|
125
229
|
}
|
|
126
230
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
231
|
+
// Read and authenticate an artifact's manifest against the pinned key. The
|
|
232
|
+
// bytes that were signed are the bytes parsed; nothing is trusted before this.
|
|
233
|
+
function signedManifest(root, publicKey, label) {
|
|
234
|
+
if (!lstatSync(root).isDirectory()) throw new Error(`Invalid artifact directory: ${root}`)
|
|
131
235
|
const bytes = boundedBytes(join(root, 'manifest.json'), 64 * 1024)
|
|
132
236
|
const signature = boundedBytes(join(root, 'manifest.sig'), 256).toString('utf8').trim()
|
|
133
237
|
const key = createPublicKey(publicKey)
|
|
134
238
|
if (key.asymmetricKeyType !== 'ed25519' || !verify(null, bytes, key, Buffer.from(signature, 'base64'))) {
|
|
135
|
-
throw new Error(
|
|
239
|
+
throw new Error(`${label} artifact signature is invalid`)
|
|
136
240
|
}
|
|
137
|
-
|
|
241
|
+
return JSON.parse(bytes)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Every manifested file must exist with exactly its signed size and digest.
|
|
245
|
+
function verifyFileDigests(root, manifest, limitFor, label) {
|
|
246
|
+
const buffer = Buffer.alloc(1024 * 1024)
|
|
247
|
+
for (const file of manifest.files) {
|
|
248
|
+
if (!Number.isSafeInteger(file.size) || !/^[0-9a-f]{64}$/.test(file.sha256)) throw new Error(`${label} artifact file metadata is invalid`)
|
|
249
|
+
const path = join(root, file.path)
|
|
250
|
+
const { descriptor, metadata } = regularFile(path, limitFor(file.path))
|
|
251
|
+
try {
|
|
252
|
+
if (metadata.size !== file.size) throw new Error(`${label} artifact size mismatch: ${file.path}`)
|
|
253
|
+
const hash = createHash('sha256')
|
|
254
|
+
let size = 0
|
|
255
|
+
for (;;) {
|
|
256
|
+
const count = readSync(descriptor, buffer, 0, buffer.length, null)
|
|
257
|
+
if (!count) break
|
|
258
|
+
size += count
|
|
259
|
+
if (size > file.size) throw new Error(`${label} artifact grew while verifying: ${file.path}`)
|
|
260
|
+
hash.update(buffer.subarray(0, count))
|
|
261
|
+
}
|
|
262
|
+
if (size !== file.size || hash.digest('hex') !== file.sha256) throw new Error(`${label} artifact digest mismatch: ${file.path}`)
|
|
263
|
+
} finally { closeSync(descriptor) }
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function verifyNativeArtifact(root, expected = {}, publicKey = ARTIFACT_PUBLIC_KEY) {
|
|
268
|
+
const manifest = signedManifest(root, publicKey, 'Native')
|
|
138
269
|
const files = artifactFiles(manifest)
|
|
139
270
|
if (manifest.repository !== 'standardagents/code-rs'
|
|
140
|
-
|| !
|
|
271
|
+
|| !NATIVE_ARTIFACT_TARGETS.includes(manifest.target)
|
|
141
272
|
|| !/^[0-9a-f]{40}$/.test(manifest.commit)
|
|
142
273
|
|| !validArtifactVersion(manifest.version)
|
|
143
274
|
|| ['commit', 'version', 'target'].some(name => expected[name] != null && manifest[name] !== expected[name])
|
|
@@ -182,25 +313,39 @@ export function verifyNativeArtifact(root, expected = {}, publicKey = ARTIFACT_P
|
|
|
182
313
|
}
|
|
183
314
|
if (manifest.target.endsWith('-unknown-linux-gnu')
|
|
184
315
|
&& !/^\d+\.\d+(?:\.\d+)?$/.test(manifest.runtime.minimumGlibc ?? '')) throw new Error('Linux artifact lacks its glibc requirement')
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
316
|
+
verifyFileDigests(root, manifest, path => runnerFiles.includes(path) ? MAX_RUNNER_ASSET_BYTES : 256 * 1024 * 1024, 'Native')
|
|
317
|
+
return manifest
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Verify a web artifact root: the Ed25519 signature, the schema-4 identity,
|
|
321
|
+
// the file rules, the exact on-disk file set (nothing unmanifested, no
|
|
322
|
+
// links), the build record, and every digest. Returns the manifest.
|
|
323
|
+
export function verifyWebArtifact(root, expected = {}, publicKey = ARTIFACT_PUBLIC_KEY) {
|
|
324
|
+
const manifest = signedManifest(root, publicKey, 'Web')
|
|
325
|
+
if (manifest.schema !== WEB_ARTIFACT_SCHEMA || manifest.format !== WEB_ARTIFACT_FORMAT
|
|
326
|
+
|| manifest.target !== WEB_ARTIFACT_TARGET
|
|
327
|
+
|| manifest.repository !== 'standardagents/code-rs'
|
|
328
|
+
|| !/^[0-9a-f]{40}$/.test(manifest.commit)
|
|
329
|
+
|| !validArtifactVersion(manifest.version)
|
|
330
|
+
|| ['commit', 'version', 'target'].some(name => expected[name] != null && manifest[name] !== expected[name])
|
|
331
|
+
|| manifest.signing?.algorithm !== 'ed25519') throw new Error('Web artifact identity is invalid')
|
|
332
|
+
if (Object.hasOwn(manifest, 'backendProfile') && (manifest.backendProfile == null || manifest.backendProfile === '')) {
|
|
333
|
+
throw new Error('Web artifact backend profile cannot be null')
|
|
334
|
+
}
|
|
335
|
+
const backendProfile = normalizeBackendProfile(manifest.backendProfile)
|
|
336
|
+
if (backendProfile && !isPreviewBuild(manifest.version)) {
|
|
337
|
+
throw new Error('Web artifact preview backend profile is not valid for this version')
|
|
338
|
+
}
|
|
339
|
+
if (!Array.isArray(manifest.files)) throw new Error('Web artifact file manifest is invalid')
|
|
340
|
+
const files = webArtifactFileList(manifest.files.map(file => file?.path))
|
|
341
|
+
verifyArtifactTree(root, files)
|
|
342
|
+
verifyFileDigests(root, manifest, () => MAX_WEB_ASSET_BYTES, 'Web')
|
|
343
|
+
// The build record is the identity the bundle itself reports; it must name
|
|
344
|
+
// the signed build, not a local or foreign one.
|
|
345
|
+
let record
|
|
346
|
+
try { record = JSON.parse(boundedBytes(join(root, 'build.json'), 4096)) } catch { record = null }
|
|
347
|
+
if (record?.schema !== 1 || record.commit !== manifest.commit || record.version !== manifest.version) {
|
|
348
|
+
throw new Error('Web artifact build record does not name the signed build')
|
|
204
349
|
}
|
|
205
350
|
return manifest
|
|
206
351
|
}
|
package/lib/install.mjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process'
|
|
2
|
+
import { createHash, randomUUID } from 'node:crypto'
|
|
3
|
+
import { chmodSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { fileURLToPath } from 'node:url'
|
|
6
|
+
|
|
7
|
+
import { artifactBinaryPath, nativeArchiveLayout, verifyNativeArtifact } from './artifact.mjs'
|
|
8
|
+
|
|
9
|
+
// A release's root package names each native target's signed archive in
|
|
10
|
+
// `standardArtifacts`; npm only selects the platform. The archive downloads
|
|
11
|
+
// from the build coordinator, which serves it from R2, and installs under this
|
|
12
|
+
// package's `native/<target>/` once its size, digest, layout, and signed
|
|
13
|
+
// manifest all check out. The daemon's launcher repair finds it there too.
|
|
14
|
+
export const ARTIFACT_ORIGIN = 'https://builds.standardcode.ai'
|
|
15
|
+
const MAX_ARCHIVE = 128 * 1024 * 1024
|
|
16
|
+
const PACKAGE_ROOT = fileURLToPath(new URL('..', import.meta.url))
|
|
17
|
+
|
|
18
|
+
export function nativeDirectory(target, root = PACKAGE_ROOT) {
|
|
19
|
+
return join(root, 'native', target)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function packageMetadata(root = PACKAGE_ROOT) {
|
|
23
|
+
return JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// The signed archive a package names for `target`, or null when its binaries
|
|
27
|
+
// install as an npm platform package instead.
|
|
28
|
+
export function artifactPointer(metadata, target) {
|
|
29
|
+
const artifacts = metadata.standardArtifacts
|
|
30
|
+
if (artifacts === undefined) return null
|
|
31
|
+
const pointer = artifacts?.schema === 1 ? artifacts.targets?.[target] : undefined
|
|
32
|
+
if (!pointer
|
|
33
|
+
|| pointer.url !== `${ARTIFACT_ORIGIN}/artifacts/${metadata.version}/${target}.tar.gz`
|
|
34
|
+
|| !/^[0-9a-f]{64}$/.test(pointer.sha256 ?? '')
|
|
35
|
+
|| !Number.isSafeInteger(pointer.size) || pointer.size < 1 || pointer.size > MAX_ARCHIVE) {
|
|
36
|
+
throw new Error(`This package names no valid native archive for ${target}`)
|
|
37
|
+
}
|
|
38
|
+
return pointer
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// The installed, verified artifact directory for `target`, or null.
|
|
42
|
+
export function installedArtifact(target, root = PACKAGE_ROOT, publicKey = undefined) {
|
|
43
|
+
const directory = nativeDirectory(target, root)
|
|
44
|
+
try {
|
|
45
|
+
return { directory, manifest: verifyNativeArtifact(directory, { version: packageMetadata(root).version, target }, publicKey) }
|
|
46
|
+
} catch {
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function tar(run, args) {
|
|
52
|
+
const result = run('tar', args, { encoding: 'utf8', maxBuffer: 16 * 1024 * 1024 })
|
|
53
|
+
if (result.error) throw result.error
|
|
54
|
+
if (result.status !== 0) throw new Error(`tar ${args[0]} failed: ${result.stderr}`)
|
|
55
|
+
return result.stdout
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function installNativeArtifact({ target, root = PACKAGE_ROOT, fetchImplementation = fetch, run = spawnSync, publicKey = undefined }) {
|
|
59
|
+
const installed = installedArtifact(target, root, publicKey)
|
|
60
|
+
if (installed) return installed.directory
|
|
61
|
+
const metadata = packageMetadata(root)
|
|
62
|
+
const pointer = artifactPointer(metadata, target)
|
|
63
|
+
if (!pointer) throw new Error(`This package carries no native archive for ${target}`)
|
|
64
|
+
const response = await fetchImplementation(pointer.url)
|
|
65
|
+
if (!response.ok) throw new Error(`Downloading the native archive failed with HTTP ${response.status}`)
|
|
66
|
+
const bytes = Buffer.from(await response.arrayBuffer())
|
|
67
|
+
if (bytes.length !== pointer.size || createHash('sha256').update(bytes).digest('hex') !== pointer.sha256) {
|
|
68
|
+
throw new Error('The downloaded native archive does not match its published size and digest')
|
|
69
|
+
}
|
|
70
|
+
const work = join(root, 'native', `.install-${randomUUID()}`)
|
|
71
|
+
mkdirSync(work, { recursive: true, mode: 0o700 })
|
|
72
|
+
try {
|
|
73
|
+
const archive = join(work, 'archive.tar.gz')
|
|
74
|
+
writeFileSync(archive, bytes, { mode: 0o600, flag: 'wx' })
|
|
75
|
+
nativeArchiveLayout(
|
|
76
|
+
tar(run, ['-tzf', archive]).trim().split('\n'),
|
|
77
|
+
tar(run, ['-tvzf', archive]).trim().split('\n').map(line => line[0]),
|
|
78
|
+
)
|
|
79
|
+
const extracted = join(work, 'artifact')
|
|
80
|
+
mkdirSync(extracted, { mode: 0o700 })
|
|
81
|
+
tar(run, ['-xzf', archive, '-C', extracted])
|
|
82
|
+
const manifest = verifyNativeArtifact(extracted, { version: metadata.version, target }, publicKey)
|
|
83
|
+
for (const command of ['standard', 'standardd', 'tmux']) {
|
|
84
|
+
chmodSync(join(extracted, artifactBinaryPath(manifest, command)), 0o755)
|
|
85
|
+
}
|
|
86
|
+
const destination = nativeDirectory(target, root)
|
|
87
|
+
rmSync(destination, { recursive: true, force: true })
|
|
88
|
+
renameSync(extracted, destination)
|
|
89
|
+
return destination
|
|
90
|
+
} finally {
|
|
91
|
+
rmSync(work, { recursive: true, force: true })
|
|
92
|
+
}
|
|
93
|
+
}
|
package/lib/launcher.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { spawnSync } from 'node:child_process'
|
|
2
2
|
import { readFileSync, realpathSync, statSync } from 'node:fs'
|
|
3
3
|
import { createRequire } from 'node:module'
|
|
4
|
-
import { dirname, join } from 'node:path'
|
|
4
|
+
import { dirname, isAbsolute, join } from 'node:path'
|
|
5
5
|
import process from 'node:process'
|
|
6
6
|
|
|
7
7
|
import { platformPackage } from './platform.mjs'
|
|
8
8
|
import { artifactBinaryPath, requireCompatibleGlibc, verifyNativeArtifact } from './artifact.mjs'
|
|
9
|
+
import { artifactPointer, installNativeArtifact, installedArtifact, nativeDirectory, packageMetadata } from './install.mjs'
|
|
9
10
|
|
|
10
11
|
const require = createRequire(import.meta.url)
|
|
11
12
|
const COMMANDS = new Set(['standard', 'standardd'])
|
|
@@ -18,7 +19,7 @@ export function resolveBinary(command, options = {}) {
|
|
|
18
19
|
const overrideDirectory = environment.STANDARD_CODE_BINARY_DIR
|
|
19
20
|
const binaryPath = overrideDirectory
|
|
20
21
|
? join(overrideDirectory, command)
|
|
21
|
-
: resolvePackagedBinary(command, options)
|
|
22
|
+
: stableBinary(command, options) ?? resolvePackagedBinary(command, options)
|
|
22
23
|
const stat = statSync(binaryPath)
|
|
23
24
|
if (!stat.isFile()) {
|
|
24
25
|
throw new Error(`Resolved Standard executable is not a file: ${binaryPath}`)
|
|
@@ -26,6 +27,39 @@ export function resolveBinary(command, options = {}) {
|
|
|
26
27
|
return binaryPath
|
|
27
28
|
}
|
|
28
29
|
|
|
30
|
+
// The daemon records its last stably booted native release as
|
|
31
|
+
// native-releases/stable. Starting it makes this package's age irrelevant:
|
|
32
|
+
// that build selects the account's active release with its own current
|
|
33
|
+
// verification rules. The packaged build remains the first-install fallback.
|
|
34
|
+
export function stableBinary(command, options = {}) {
|
|
35
|
+
const environment = options.environment ?? process.env
|
|
36
|
+
const home = environment.HOME
|
|
37
|
+
const argumentsList = options.arguments ?? process.argv.slice(2)
|
|
38
|
+
if (!home || !isAbsolute(home)
|
|
39
|
+
|| argumentsList.some(argument => argument === '--state-dir' || argument.startsWith('--state-dir='))) {
|
|
40
|
+
return null
|
|
41
|
+
}
|
|
42
|
+
const { rustTarget } = platformPackage(options.platform, options.architecture, options.report)
|
|
43
|
+
const relative = rustTarget.endsWith('-unknown-linux-gnu')
|
|
44
|
+
? `bin/${command}`
|
|
45
|
+
: `${command === 'standard' ? 'Standard Code' : 'Standard Code Daemon'}.app/Contents/MacOS/${command}`
|
|
46
|
+
const uid = options.uid ?? process.getuid?.()
|
|
47
|
+
try {
|
|
48
|
+
const root = realpathSync(join(home, '.local/share/standard-code/state/native-releases'))
|
|
49
|
+
const slot = realpathSync(join(root, 'stable'))
|
|
50
|
+
if (dirname(slot) !== join(root, rustTarget)) return null
|
|
51
|
+
for (const directory of [root, slot]) {
|
|
52
|
+
const info = statSync(directory)
|
|
53
|
+
if (!info.isDirectory() || info.uid !== uid || (info.mode & 0o077) !== 0) return null
|
|
54
|
+
}
|
|
55
|
+
const binary = join(slot, relative)
|
|
56
|
+
const info = statSync(binary)
|
|
57
|
+
return info.isFile() && info.uid === uid ? binary : null
|
|
58
|
+
} catch {
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
29
63
|
function resolvePackagedBinary(command, options) {
|
|
30
64
|
const platform = platformPackage(
|
|
31
65
|
options.platform,
|
|
@@ -33,11 +67,18 @@ function resolvePackagedBinary(command, options) {
|
|
|
33
67
|
options.report,
|
|
34
68
|
)
|
|
35
69
|
const resolve = options.resolve ?? require.resolve
|
|
70
|
+
const report = options.report ?? process.report
|
|
71
|
+
// A release that carries no binaries on npm installs its signed R2 archive
|
|
72
|
+
// under this package instead (see install.mjs).
|
|
73
|
+
const downloaded = installedArtifact(platform.rustTarget, options.packageRoot)
|
|
74
|
+
if (downloaded) {
|
|
75
|
+
requireCompatibleGlibc(downloaded.manifest, report?.getReport()?.header?.glibcVersionRuntime)
|
|
76
|
+
return join(downloaded.directory, artifactBinaryPath(downloaded.manifest, command))
|
|
77
|
+
}
|
|
36
78
|
try {
|
|
37
79
|
const root = dirname(realpathSync(resolve(`${platform.packageName}/manifest.json`)))
|
|
38
80
|
const version = JSON.parse(readFileSync(new URL('../package.json', import.meta.url))).version
|
|
39
81
|
const artifact = verifyNativeArtifact(root, { version, target: platform.rustTarget })
|
|
40
|
-
const report = options.report ?? process.report
|
|
41
82
|
requireCompatibleGlibc(artifact, report?.getReport()?.header?.glibcVersionRuntime)
|
|
42
83
|
return join(root, artifactBinaryPath(artifact, command))
|
|
43
84
|
} catch (error) {
|
|
@@ -57,8 +98,41 @@ export function spawnBinary(command, args, options = {}) {
|
|
|
57
98
|
})
|
|
58
99
|
}
|
|
59
100
|
|
|
60
|
-
|
|
101
|
+
// Install this platform's signed R2 archive when the package names one and
|
|
102
|
+
// has no binaries installed yet. The package has no install script, so npm
|
|
103
|
+
// and pnpm install it without an allow-scripts prompt, and the first launch
|
|
104
|
+
// downloads and verifies the native build here (#684).
|
|
105
|
+
export async function ensureNativeBinaries(options = {}) {
|
|
106
|
+
if (options.environment?.STANDARD_CODE_BINARY_DIR ?? process.env.STANDARD_CODE_BINARY_DIR) return
|
|
107
|
+
const platform = platformPackage(options.platform, options.architecture, options.report)
|
|
108
|
+
if (installedArtifact(platform.rustTarget, options.packageRoot)) return
|
|
109
|
+
if (!artifactPointer(packageMetadata(options.packageRoot), platform.rustTarget)) return
|
|
110
|
+
try {
|
|
111
|
+
const resolve = options.resolve ?? require.resolve
|
|
112
|
+
resolve(`${platform.packageName}/manifest.json`)
|
|
113
|
+
return
|
|
114
|
+
} catch {
|
|
115
|
+
// No npm platform package: download the archive.
|
|
116
|
+
}
|
|
117
|
+
const log = options.log ?? (message => console.error(message))
|
|
118
|
+
log(`standard: installing the native build for ${platform.rustTarget} (first launch)`)
|
|
119
|
+
try {
|
|
120
|
+
await installNativeArtifact({ target: platform.rustTarget, root: options.packageRoot, fetchImplementation: options.fetchImplementation })
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if (error?.code === 'EACCES' || error?.code === 'EPERM') {
|
|
123
|
+
throw new Error(
|
|
124
|
+
`cannot write the native build under ${nativeDirectory(platform.rustTarget, options.packageRoot)}. `
|
|
125
|
+
+ 'The package belongs to another user (for example an npm install run with sudo); '
|
|
126
|
+
+ 'reinstall @standardagents/code with an npm prefix you own.',
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
throw error
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export async function main(command) {
|
|
61
134
|
try {
|
|
135
|
+
await ensureNativeBinaries()
|
|
62
136
|
const result = spawnBinary(command, process.argv.slice(2))
|
|
63
137
|
if (result.error) throw result.error
|
|
64
138
|
if (result.signal) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@standardagents/code",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.3-canary.6cc75f",
|
|
4
4
|
"description": "Platform launcher for the Standard universal terminal network.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -21,19 +21,29 @@
|
|
|
21
21
|
},
|
|
22
22
|
"files": [
|
|
23
23
|
"bin",
|
|
24
|
-
"lib"
|
|
25
|
-
"postinstall.mjs"
|
|
24
|
+
"lib"
|
|
26
25
|
],
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
"standardArtifacts": {
|
|
27
|
+
"schema": 1,
|
|
28
|
+
"targets": {
|
|
29
|
+
"aarch64-apple-darwin": {
|
|
30
|
+
"url": "https://builds.standardcode.ai/artifacts/0.14.3-canary.6cc75f/aarch64-apple-darwin.tar.gz",
|
|
31
|
+
"sha256": "45f9458727aae1a6ed80dad7f8bff778d271a56b5148e3cdc9fceb22fafa2410",
|
|
32
|
+
"size": 22931785
|
|
33
|
+
},
|
|
34
|
+
"x86_64-unknown-linux-gnu": {
|
|
35
|
+
"url": "https://builds.standardcode.ai/artifacts/0.14.3-canary.6cc75f/x86_64-unknown-linux-gnu.tar.gz",
|
|
36
|
+
"sha256": "ef44b1a6a2c3623143f2dcb0505b56cce65d8649d21441cfe4373ed59bf5f6de",
|
|
37
|
+
"size": 28560078
|
|
38
|
+
}
|
|
39
|
+
}
|
|
33
40
|
},
|
|
34
41
|
"standardUpgrade": {
|
|
35
42
|
"schema": 1,
|
|
36
43
|
"reader": 1,
|
|
37
|
-
"bridge":
|
|
44
|
+
"bridge": {
|
|
45
|
+
"version": "0.14.1-canary.76909e",
|
|
46
|
+
"commit": "76909e97ec56c2ba5490932135642962c4035596"
|
|
47
|
+
}
|
|
38
48
|
}
|
|
39
49
|
}
|
package/postinstall.mjs
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { chmodSync } from 'node:fs'
|
|
2
|
-
|
|
3
|
-
import { resolveBinary } from './lib/launcher.mjs'
|
|
4
|
-
|
|
5
|
-
for (const command of ['standard', 'standardd']) {
|
|
6
|
-
try {
|
|
7
|
-
chmodSync(resolveBinary(command), 0o755)
|
|
8
|
-
} catch (error) {
|
|
9
|
-
const detail = error instanceof Error ? error.message : String(error)
|
|
10
|
-
console.warn(`@standardagents/code: ${detail}`)
|
|
11
|
-
}
|
|
12
|
-
}
|