@defrex/autobuild 1.0.0-beta.2 → 1.0.0-beta.4
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/SPEC.md +342 -23
- package/bin/ab-dispatch-generation.ts +93 -0
- package/docs/answering-blocked-builds.md +21 -3
- package/docs/architecture.md +170 -7
- package/docs/configuration.md +173 -5
- package/docs/integration-tree.md +91 -4
- package/docs/integration.md +54 -0
- package/docs/operator-api.md +58 -5
- package/docs/remote-store-protocol.md +9 -1
- package/docs/remote-ticket-protocol.md +5 -0
- package/docs/restacking.md +4 -1
- package/docs/setup.md +24 -0
- package/docs/spec-standard.md +94 -5
- package/docs/ticket-migration.md +50 -4
- package/package.json +3 -3
- package/packages/core/src/cli/artifact.ts +5 -4
- package/packages/core/src/cli/attention.ts +2 -0
- package/packages/core/src/cli/binary-direct.ts +190 -0
- package/packages/core/src/cli/binary-lifecycle.ts +46 -0
- package/packages/core/src/cli/binary.ts +24 -210
- package/packages/core/src/cli/build-control.ts +108 -5
- package/packages/core/src/cli/context.ts +41 -1
- package/packages/core/src/cli/dashboard/hand-pushed.ts +24 -0
- package/packages/core/src/cli/dashboard/handoff.ts +64 -0
- package/packages/core/src/cli/dashboard/integration-detail.ts +11 -6
- package/packages/core/src/cli/dashboard/integration-row.ts +15 -8
- package/packages/core/src/cli/dashboard/model.ts +41 -10
- package/packages/core/src/cli/dashboard/render.ts +24 -2
- package/packages/core/src/cli/dashboard/rollback.ts +30 -0
- package/packages/core/src/cli/dispatch-args.ts +53 -0
- package/packages/core/src/cli/dispatch-crash-budget.ts +15 -0
- package/packages/core/src/cli/dispatch-frontend.ts +57 -5
- package/packages/core/src/cli/dispatch-handoff.ts +276 -0
- package/packages/core/src/cli/dispatch-launch-contract.ts +170 -0
- package/packages/core/src/cli/dispatch-launcher.ts +580 -0
- package/packages/core/src/cli/dispatch-parent-watch.ts +44 -0
- package/packages/core/src/cli/dispatch-preflight.ts +272 -0
- package/packages/core/src/cli/dispatch-presentation-job.ts +46 -0
- package/packages/core/src/cli/dispatch-presentation-ownership.ts +9 -0
- package/packages/core/src/cli/dispatch-process.ts +22 -90
- package/packages/core/src/cli/dispatch-readiness.ts +47 -0
- package/packages/core/src/cli/dispatch-repository-feed.ts +6 -1
- package/packages/core/src/cli/dispatch-runtime-cache.ts +522 -0
- package/packages/core/src/cli/dispatch-runtime-dependencies.ts +291 -0
- package/packages/core/src/cli/dispatch-runtime-use.ts +336 -0
- package/packages/core/src/cli/dispatch-upgrade-controller.ts +221 -0
- package/packages/core/src/cli/dispatch-upgrade-install.ts +258 -0
- package/packages/core/src/cli/dispatch-upgrade-source.ts +309 -0
- package/packages/core/src/cli/dispatch-upgrade-target.ts +190 -0
- package/packages/core/src/cli/dispatch.ts +609 -60
- package/packages/core/src/cli/dispatcher-liveness.ts +1 -1
- package/packages/core/src/cli/harvest.ts +28 -11
- package/packages/core/src/cli/help.ts +7 -3
- package/packages/core/src/cli/installation.ts +5 -3
- package/packages/core/src/cli/main-checkout.ts +59 -0
- package/packages/core/src/cli/main.ts +26 -53
- package/packages/core/src/cli/node-verification-provenance.ts +97 -0
- package/packages/core/src/cli/repo-state.ts +3 -58
- package/packages/core/src/cli/repository-status.ts +74 -4
- package/packages/core/src/cli/runtime-spawn.ts +16 -0
- package/packages/core/src/cli/status.ts +57 -3
- package/packages/core/src/cli/ticket.ts +35 -11
- package/packages/core/src/cli/upgrade-followup.ts +25 -0
- package/packages/core/src/cli/wait.ts +3 -0
- package/packages/core/src/cli/watch.ts +7 -0
- package/packages/core/src/config/live.ts +1 -0
- package/packages/core/src/config/schema.ts +8 -4
- package/packages/core/src/distribution-packaging.ts +116 -0
- package/packages/core/src/events/catalog.ts +6 -0
- package/packages/core/src/events/payloads.ts +55 -9
- package/packages/core/src/events/policy.ts +26 -0
- package/packages/core/src/events/repository.ts +205 -4
- package/packages/core/src/events/runtime.ts +16 -0
- package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +6537 -4053
- package/packages/core/src/kernel/change-presence.ts +7 -1
- package/packages/core/src/kernel/dispatch-capacity.ts +12 -4
- package/packages/core/src/kernel/dispatch-drain-recovery.ts +121 -0
- package/packages/core/src/kernel/dispatch-status.ts +4 -2
- package/packages/core/src/kernel/dispatch-upgrade.ts +171 -0
- package/packages/core/src/kernel/dispatcher-liveness.ts +7 -0
- package/packages/core/src/kernel/generators/repository-journals.ts +82 -0
- package/packages/core/src/kernel/guarded-forge.ts +1 -1
- package/packages/core/src/kernel/harvest.ts +98 -4
- package/packages/core/src/kernel/integration-attention.ts +20 -0
- package/packages/core/src/kernel/integration-completeness.ts +54 -16
- package/packages/core/src/kernel/integration-state.ts +80 -11
- package/packages/core/src/kernel/landing-readiness.ts +6 -1
- package/packages/core/src/kernel/mixed-version.ts +34 -0
- package/packages/core/src/kernel/node-parent.ts +35 -10
- package/packages/core/src/kernel/node-polling.ts +160 -0
- package/packages/core/src/kernel/read-facts.ts +47 -5
- package/packages/core/src/kernel/reducer-registry.ts +6 -0
- package/packages/core/src/kernel/reducer.ts +75 -14
- package/packages/core/src/kernel/service-facts.ts +11 -4
- package/packages/core/src/kernel/ticket-migrations.ts +139 -0
- package/packages/core/src/operator/presentation.ts +7 -0
- package/packages/core/src/operator/query.ts +13 -3
- package/packages/core/src/operator/registry.ts +1 -1
- package/packages/core/src/operator/tickets.ts +30 -26
- package/packages/core/src/plugins/load.ts +4 -1
- package/packages/core/src/plugins/runtime-snapshot.ts +128 -0
- package/packages/core/src/ports/forge/contract.ts +29 -0
- package/packages/core/src/ports/forge/github.ts +189 -61
- package/packages/core/src/ports/forge-failure.ts +1 -0
- package/packages/core/src/ports/forge-telemetry.ts +3 -0
- package/packages/core/src/ports/runner/session-env.ts +1 -0
- package/packages/core/src/ports/tickets/linear.ts +180 -12
- package/packages/core/src/ports/tickets/migration.ts +92 -9
- package/packages/core/src/ports/types.ts +17 -0
- package/packages/core/src/ports/workspace/distribution-archive.ts +22 -149
- package/packages/core/src/ports/workspace/distribution-release.ts +56 -0
- package/packages/core/src/ports/workspace/git-worktree.ts +2 -1
- package/packages/core/src/ports/workspace/local-build-execution.ts +37 -17
- package/packages/core/src/processes/build-child-exit.ts +5 -26
- package/packages/core/src/processes/build-runner.ts +81 -11
- package/packages/core/src/processes/dispatcher.ts +418 -59
- package/packages/core/src/processes/execution-no-progress.ts +6 -0
- package/packages/core/src/processes/execution-relaunch.ts +30 -0
- package/packages/core/src/processes/execution-settlement.ts +16 -7
- package/packages/core/src/processes/harvest-runner.ts +328 -292
- package/packages/core/src/processes/process-group-reaper.ts +17 -10
- package/packages/core/src/runtime-identity.ts +22 -3
- package/packages/core/src/store/contract.ts +183 -0
- package/packages/core/src/store/local/store.ts +197 -72
- package/packages/core/src/store/reduced-reads.ts +8 -0
- package/packages/core/src/store/reducer-value-shapes.json +8915 -6344
- package/packages/core/src/store/remote/client.ts +43 -0
- package/packages/core/src/store/repo-state-events.ts +21 -3
- package/packages/core/src/store/types.ts +7 -0
- package/packages/core/src/testing/dispatch-read-budget.ts +3 -2
- package/skills/guide/SKILL.md +234 -24
- package/skills/guide/references/integration-tree.md +91 -4
- package/skills/guide/references/operator-api.md +53 -4
- package/skills/guide/references/plugin-authoring.md +45 -4
- package/skills/guide/references/remote-store-protocol.md +9 -1
- package/skills/guide/references/remote-ticket-protocol.md +5 -0
- package/skills/guide/references/restacking.md +4 -1
- package/skills/guide/references/setup.md +24 -0
- package/skills/guide/references/spec-standard.md +94 -5
- package/skills/guide/references/ticket-migration.md +50 -4
- package/skills/harvest/SKILL.md +4 -0
- package/skills/harvest-review/SKILL.md +4 -0
- package/skills/operate/SKILL.md +78 -8
- package/skills/spec/SKILL.md +35 -5
- package/skills/tickets/SKILL.md +19 -0
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { tmpdir } from 'node:os'
|
|
3
|
+
import {
|
|
4
|
+
mkdir,
|
|
5
|
+
mkdtemp,
|
|
6
|
+
readFile,
|
|
7
|
+
writeFile,
|
|
8
|
+
rm,
|
|
9
|
+
realpath,
|
|
10
|
+
lstat,
|
|
11
|
+
readdir,
|
|
12
|
+
readlink,
|
|
13
|
+
} from 'node:fs/promises'
|
|
14
|
+
import { join, resolve } from 'node:path'
|
|
15
|
+
import { packageDistribution } from '../distribution-packaging'
|
|
16
|
+
import { parse as parseToml } from 'smol-toml'
|
|
17
|
+
import { treeDigest, inside, type Manifest } from './dispatch-runtime-dependencies'
|
|
18
|
+
import type { Exec } from '../ports/workspace/git-worktree'
|
|
19
|
+
|
|
20
|
+
export interface SourceLaunch {
|
|
21
|
+
repository: string
|
|
22
|
+
commit: string
|
|
23
|
+
baseline: string
|
|
24
|
+
}
|
|
25
|
+
export const sourceHash = (value: unknown): string =>
|
|
26
|
+
createHash('sha256').update(JSON.stringify(value)).digest('hex')
|
|
27
|
+
const object = (value: unknown): Record<string, unknown> => {
|
|
28
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
29
|
+
throw new Error('invalid source package metadata')
|
|
30
|
+
return value as Record<string, unknown>
|
|
31
|
+
}
|
|
32
|
+
function canonical(value: unknown): unknown {
|
|
33
|
+
if (Array.isArray(value)) return value.map(canonical)
|
|
34
|
+
if (value && typeof value === 'object')
|
|
35
|
+
return Object.fromEntries(
|
|
36
|
+
Object.entries(value)
|
|
37
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
38
|
+
.map(([k, v]) => [k, canonical(v)]),
|
|
39
|
+
)
|
|
40
|
+
return value
|
|
41
|
+
}
|
|
42
|
+
export async function sourceCommand(
|
|
43
|
+
cmd: string[],
|
|
44
|
+
cwd: string,
|
|
45
|
+
signal?: AbortSignal,
|
|
46
|
+
): Promise<string> {
|
|
47
|
+
const child = Bun.spawn(cmd, {
|
|
48
|
+
cwd,
|
|
49
|
+
stdout: 'pipe',
|
|
50
|
+
stderr: 'pipe',
|
|
51
|
+
signal: signal
|
|
52
|
+
? AbortSignal.any([signal, AbortSignal.timeout(120_000)])
|
|
53
|
+
: AbortSignal.timeout(120_000),
|
|
54
|
+
})
|
|
55
|
+
const [code, stdout] = await Promise.all([
|
|
56
|
+
child.exited,
|
|
57
|
+
new Response(child.stdout).text(),
|
|
58
|
+
new Response(child.stderr).text(),
|
|
59
|
+
])
|
|
60
|
+
signal?.throwIfAborted()
|
|
61
|
+
if (code !== 0) throw new Error('source runtime subprocess failed')
|
|
62
|
+
return stdout
|
|
63
|
+
}
|
|
64
|
+
export async function sourceRepository(
|
|
65
|
+
root: string,
|
|
66
|
+
checkout: string,
|
|
67
|
+
): Promise<string | undefined> {
|
|
68
|
+
try {
|
|
69
|
+
const top = (await sourceCommand(['git', 'rev-parse', '--show-toplevel'], root)).trim()
|
|
70
|
+
if ((await realpath(top)) !== (await realpath(root))) return undefined
|
|
71
|
+
const common = async (cwd: string) =>
|
|
72
|
+
realpath(
|
|
73
|
+
(
|
|
74
|
+
await sourceCommand(
|
|
75
|
+
['git', 'rev-parse', '--path-format=absolute', '--git-common-dir'],
|
|
76
|
+
cwd,
|
|
77
|
+
)
|
|
78
|
+
).trim(),
|
|
79
|
+
)
|
|
80
|
+
const own = await common(root)
|
|
81
|
+
return own === (await common(checkout)) ? own : undefined
|
|
82
|
+
} catch {
|
|
83
|
+
return undefined
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** No linked worktree, checkout, reset, or mutable reference is created here. */
|
|
88
|
+
export async function withSourceExport<T>(
|
|
89
|
+
input: { checkout: string; sha: string; signal?: AbortSignal; exec?: Exec },
|
|
90
|
+
use: (root: string) => Promise<T>,
|
|
91
|
+
): Promise<T> {
|
|
92
|
+
if (!/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/.test(input.sha)) throw new Error('invalid source commit')
|
|
93
|
+
const cache = join(input.checkout, '.autobuild/runtime')
|
|
94
|
+
await mkdir(cache, { recursive: true })
|
|
95
|
+
const stage = await mkdtemp(join(cache, '.preparing-source-'))
|
|
96
|
+
try {
|
|
97
|
+
const archive = join(stage, 'source.tar')
|
|
98
|
+
const cmd = ['git', 'archive', '--format=tar', `--output=${archive}`, input.sha]
|
|
99
|
+
if (input.exec) {
|
|
100
|
+
const result = await input.exec(cmd, { cwd: input.checkout, signal: input.signal })
|
|
101
|
+
if (result.exitCode !== 0) throw new Error('source commit export failed')
|
|
102
|
+
} else await sourceCommand(cmd, input.checkout, input.signal)
|
|
103
|
+
const names = await sourceCommand(['tar', '-tf', archive], stage, input.signal)
|
|
104
|
+
for (const name of names.split('\n').filter(Boolean))
|
|
105
|
+
if (!inside(stage, resolve(stage, name)) || name.startsWith('/'))
|
|
106
|
+
throw new Error('unsafe source archive path')
|
|
107
|
+
const root = join(stage, 'checkout')
|
|
108
|
+
await mkdir(root)
|
|
109
|
+
await sourceCommand(['tar', '-xf', archive, '-C', root], stage, input.signal)
|
|
110
|
+
async function links(path: string): Promise<void> {
|
|
111
|
+
for (const entry of await readdir(path, { withFileTypes: true })) {
|
|
112
|
+
const file = join(path, entry.name)
|
|
113
|
+
if (entry.isSymbolicLink()) {
|
|
114
|
+
const target = resolve(path, await readlink(file))
|
|
115
|
+
if (!inside(root, target)) throw new Error('source archive link escapes staging')
|
|
116
|
+
} else if (entry.isDirectory()) await links(file)
|
|
117
|
+
else if (!(await lstat(file)).isFile()) throw new Error('unsupported source archive entry')
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
await links(root)
|
|
121
|
+
input.signal?.throwIfAborted()
|
|
122
|
+
return await use(root)
|
|
123
|
+
} finally {
|
|
124
|
+
await rm(stage, { recursive: true, force: true })
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Bun package keys are hoisted package paths; scoped names form one component.
|
|
129
|
+
* Resolve nearest nested record before walking the parent package contexts. */
|
|
130
|
+
export function productionLockClosure(
|
|
131
|
+
manifest: Manifest,
|
|
132
|
+
raw: unknown,
|
|
133
|
+
workspaces: Record<string, Manifest> = {},
|
|
134
|
+
): { records: Record<string, unknown>; graph: unknown } {
|
|
135
|
+
const lock = object(raw)
|
|
136
|
+
if (lock.lockfileVersion !== 1 && lock.lockfileVersion !== 2)
|
|
137
|
+
throw new Error('unsupported source Bun lock version')
|
|
138
|
+
const packages = object(lock.packages)
|
|
139
|
+
const root = object(object(lock.workspaces)[''])
|
|
140
|
+
const records: Record<string, unknown> = {}
|
|
141
|
+
const ids = new Map<string, number>()
|
|
142
|
+
const nodes: Array<{ record: unknown; edges: Record<string, number> }> = []
|
|
143
|
+
const roots: Record<string, number> = {}
|
|
144
|
+
const sections = ['dependencies', 'optionalDependencies', 'peerDependencies'] as const
|
|
145
|
+
for (const section of sections)
|
|
146
|
+
for (const [name, declaration] of Object.entries(manifest[section] ?? {}))
|
|
147
|
+
if (typeof declaration !== 'string' || object(root[section] ?? {})[name] !== declaration)
|
|
148
|
+
throw new Error('source manifest and lock disagree')
|
|
149
|
+
const components = (key: string) => key.match(/@[^/]+\/[^/]+|[^/]+/g) ?? []
|
|
150
|
+
function visit(name: string, context: string, optional: boolean): number | undefined {
|
|
151
|
+
const parents = components(context)
|
|
152
|
+
let key: string | undefined
|
|
153
|
+
for (;;) {
|
|
154
|
+
const candidate = [...parents, name].join('/')
|
|
155
|
+
if (Object.hasOwn(packages, candidate)) {
|
|
156
|
+
key = candidate
|
|
157
|
+
break
|
|
158
|
+
}
|
|
159
|
+
if (!parents.length) break
|
|
160
|
+
parents.pop()
|
|
161
|
+
}
|
|
162
|
+
if (!key) {
|
|
163
|
+
if (optional) return
|
|
164
|
+
throw new Error(`missing production source lock dependency ${name}`)
|
|
165
|
+
}
|
|
166
|
+
if (ids.has(key)) return ids.get(key)
|
|
167
|
+
const record = packages[key]
|
|
168
|
+
if (
|
|
169
|
+
!Array.isArray(record) ||
|
|
170
|
+
typeof record[0] !== 'string' ||
|
|
171
|
+
!record[0].startsWith(`${name}@`)
|
|
172
|
+
)
|
|
173
|
+
throw new Error('unsupported source lock record')
|
|
174
|
+
if (record[0].includes('@file:') || record[0].includes('@link:'))
|
|
175
|
+
throw new Error('unsupported local production source dependency')
|
|
176
|
+
records[key] = record
|
|
177
|
+
let meta = object(record[2] ?? {})
|
|
178
|
+
if (record[0].startsWith(`${name}@workspace:`)) {
|
|
179
|
+
const path = record[0].slice(`${name}@workspace:`.length)
|
|
180
|
+
const workspace = workspaces[path]
|
|
181
|
+
if (!workspace || workspace.name !== name)
|
|
182
|
+
throw new Error('missing reachable source workspace manifest')
|
|
183
|
+
const lockedWorkspace = object(object(lock.workspaces)[path])
|
|
184
|
+
for (const section of sections)
|
|
185
|
+
if (
|
|
186
|
+
JSON.stringify(canonical(workspace[section] ?? {})) !==
|
|
187
|
+
JSON.stringify(canonical(lockedWorkspace[section] ?? {}))
|
|
188
|
+
)
|
|
189
|
+
throw new Error('source workspace and lock disagree')
|
|
190
|
+
meta = workspace
|
|
191
|
+
records[key] = {
|
|
192
|
+
record,
|
|
193
|
+
manifest: canonical(
|
|
194
|
+
Object.fromEntries(
|
|
195
|
+
['name', 'version', ...sections].map((field) => [field, workspace[field] ?? null]),
|
|
196
|
+
),
|
|
197
|
+
),
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const id = nodes.length
|
|
201
|
+
ids.set(key, id)
|
|
202
|
+
const node = { record: records[key], edges: {} as Record<string, number> }
|
|
203
|
+
nodes.push(node)
|
|
204
|
+
for (const section of sections)
|
|
205
|
+
for (const dependency of Object.keys(object(meta[section] ?? {})).sort()) {
|
|
206
|
+
if (typeof object(meta[section])[dependency] !== 'string')
|
|
207
|
+
throw new Error('unsupported source dependency declaration')
|
|
208
|
+
const dependencyId = visit(
|
|
209
|
+
dependency,
|
|
210
|
+
key,
|
|
211
|
+
section === 'optionalDependencies' ||
|
|
212
|
+
(section === 'peerDependencies' &&
|
|
213
|
+
Array.isArray(meta.optionalPeers) &&
|
|
214
|
+
meta.optionalPeers.includes(dependency)),
|
|
215
|
+
)
|
|
216
|
+
if (dependencyId !== undefined) node.edges[dependency] = dependencyId
|
|
217
|
+
}
|
|
218
|
+
return id
|
|
219
|
+
}
|
|
220
|
+
for (const section of sections)
|
|
221
|
+
for (const name of Object.keys(manifest[section] ?? {}).sort()) {
|
|
222
|
+
const id = visit(
|
|
223
|
+
name,
|
|
224
|
+
'',
|
|
225
|
+
section === 'optionalDependencies' || !!manifest.peerDependenciesMeta?.[name]?.optional,
|
|
226
|
+
)
|
|
227
|
+
if (id !== undefined) roots[name] = id
|
|
228
|
+
}
|
|
229
|
+
return { records, graph: canonical({ roots, packages: nodes }) }
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Identity follows real pack rules, with provenance and transport timestamps removed. */
|
|
233
|
+
export async function sourceContent(
|
|
234
|
+
root: string,
|
|
235
|
+
repository: string,
|
|
236
|
+
signal?: AbortSignal,
|
|
237
|
+
): Promise<{ key: string; version: string }> {
|
|
238
|
+
signal?.throwIfAborted()
|
|
239
|
+
const manifest = JSON.parse(await readFile(join(root, 'package.json'), 'utf8')) as Manifest
|
|
240
|
+
if (typeof manifest.version !== 'string' || !manifest.version)
|
|
241
|
+
throw new Error('source manifest has no version')
|
|
242
|
+
const lock = object(Bun.JSONC.parse(await readFile(join(root, 'bun.lock'), 'utf8')))
|
|
243
|
+
const workspaces: Record<string, Manifest> = {}
|
|
244
|
+
for (const path of Object.keys(object(lock.workspaces))) {
|
|
245
|
+
if (!path) continue
|
|
246
|
+
if (!inside(root, resolve(root, path))) throw new Error('unsafe source workspace path')
|
|
247
|
+
workspaces[path] = JSON.parse(await readFile(join(root, path, 'package.json'), 'utf8'))
|
|
248
|
+
}
|
|
249
|
+
const closure = productionLockClosure(manifest, lock, workspaces)
|
|
250
|
+
const configuration: Record<string, unknown> = {}
|
|
251
|
+
try {
|
|
252
|
+
const config = parseToml(await readFile(join(root, 'bunfig.toml'), 'utf8'))
|
|
253
|
+
const install = object(config.install ?? {})
|
|
254
|
+
// Credentials, cache paths, logging and unrelated scopes cannot affect
|
|
255
|
+
// the bytes selected by a frozen production lock.
|
|
256
|
+
for (const key of ['peer', 'optional', 'linker'])
|
|
257
|
+
if (install[key] !== undefined) configuration[key] = install[key]
|
|
258
|
+
} catch (error) {
|
|
259
|
+
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error
|
|
260
|
+
}
|
|
261
|
+
const workspaceContent: Record<string, string> = {}
|
|
262
|
+
for (const value of Object.values(closure.records)) {
|
|
263
|
+
if (Array.isArray(value)) continue
|
|
264
|
+
const record = object(value).record as string[]
|
|
265
|
+
const path = record[0]!.slice(record[0]!.indexOf('@workspace:') + 11)
|
|
266
|
+
workspaceContent[path] = await packagedFingerprint(join(root, path), signal)
|
|
267
|
+
}
|
|
268
|
+
const packaged = await packagedFingerprint(root, signal)
|
|
269
|
+
// The frozen lock is the authority for registry resolution/integrity.
|
|
270
|
+
const key = sourceHash([
|
|
271
|
+
'source',
|
|
272
|
+
repository,
|
|
273
|
+
packaged,
|
|
274
|
+
closure.graph,
|
|
275
|
+
canonical(configuration),
|
|
276
|
+
canonical(workspaceContent),
|
|
277
|
+
])
|
|
278
|
+
signal?.throwIfAborted()
|
|
279
|
+
return { key, version: String(manifest.version) }
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async function packagedFingerprint(root: string, signal?: AbortSignal): Promise<string> {
|
|
283
|
+
const manifest = JSON.parse(await readFile(join(root, 'package.json'), 'utf8')) as Manifest
|
|
284
|
+
for (const path of manifest.files ?? [])
|
|
285
|
+
if (!path.startsWith('!') && !inside(root, resolve(root, path)))
|
|
286
|
+
throw new Error('unsafe packaged source path')
|
|
287
|
+
// Launch baselines also package the live checkout and its workspaces. Scratch
|
|
288
|
+
// must never require write access beside either source root.
|
|
289
|
+
const stage = await mkdtemp(join(tmpdir(), 'autobuild-source-fingerprint-'))
|
|
290
|
+
try {
|
|
291
|
+
await writeFile(
|
|
292
|
+
join(stage, 'package.tgz'),
|
|
293
|
+
await packageDistribution(root, undefined, signal ?? AbortSignal.timeout(120_000)),
|
|
294
|
+
)
|
|
295
|
+
const extracted = join(stage, 'packed')
|
|
296
|
+
await mkdir(extracted)
|
|
297
|
+
await sourceCommand(
|
|
298
|
+
['tar', '-xzf', join(stage, 'package.tgz'), '--strip-components=1', '-C', extracted],
|
|
299
|
+
stage,
|
|
300
|
+
signal,
|
|
301
|
+
)
|
|
302
|
+
const packed = JSON.parse(await readFile(join(extracted, 'package.json'), 'utf8'))
|
|
303
|
+
delete packed.gitHead
|
|
304
|
+
await writeFile(join(extracted, 'package.json'), JSON.stringify(canonical(packed)))
|
|
305
|
+
return await treeDigest(extracted)
|
|
306
|
+
} finally {
|
|
307
|
+
await rm(stage, { recursive: true, force: true })
|
|
308
|
+
}
|
|
309
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { withSourceExport, sourceContent, type SourceLaunch } from './dispatch-upgrade-source'
|
|
4
|
+
import { isValidGitBranchName } from '../kernel/origin'
|
|
5
|
+
import { createHash, randomUUID } from 'node:crypto'
|
|
6
|
+
import { readDistributionIdentity } from './installation'
|
|
7
|
+
import { lockDependency, lockPackageRecord, githubDependency } from './installation'
|
|
8
|
+
import semver from 'semver'
|
|
9
|
+
import type { Forge } from '../ports/types'
|
|
10
|
+
import type { Exec } from '../ports/workspace/git-worktree'
|
|
11
|
+
|
|
12
|
+
export const TARGET_CHECK_INTERVAL_MS = 60_000
|
|
13
|
+
export type UpgradeTarget = {
|
|
14
|
+
mode: 'pinned' | 'installed' | 'source'
|
|
15
|
+
key: string
|
|
16
|
+
version?: string
|
|
17
|
+
source: string
|
|
18
|
+
base?: string
|
|
19
|
+
sha?: string
|
|
20
|
+
dependency?: string
|
|
21
|
+
record?: unknown[]
|
|
22
|
+
installRoot?: string
|
|
23
|
+
}
|
|
24
|
+
export class TargetResolutionError extends Error {
|
|
25
|
+
constructor(
|
|
26
|
+
public readonly target: UpgradeTarget,
|
|
27
|
+
message: string,
|
|
28
|
+
) {
|
|
29
|
+
super(message)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const fingerprint = (value: unknown) =>
|
|
33
|
+
createHash('sha256').update(JSON.stringify(value)).digest('hex')
|
|
34
|
+
const object = (value: unknown): Record<string, unknown> =>
|
|
35
|
+
value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
36
|
+
? (value as Record<string, unknown>)
|
|
37
|
+
: {}
|
|
38
|
+
|
|
39
|
+
/** Both blobs come from one authoritative commit. No checkout or tracking-ref fallback. */
|
|
40
|
+
export async function resolveUpgradeTarget(input: {
|
|
41
|
+
checkout: string
|
|
42
|
+
base: string
|
|
43
|
+
sourceLaunch?: SourceLaunch
|
|
44
|
+
originalInstall: string
|
|
45
|
+
forge: Forge
|
|
46
|
+
exec: Exec
|
|
47
|
+
signal?: AbortSignal
|
|
48
|
+
}): Promise<UpgradeTarget> {
|
|
49
|
+
let target: UpgradeTarget = {
|
|
50
|
+
mode: 'pinned',
|
|
51
|
+
key: fingerprint([input.base]),
|
|
52
|
+
source: input.base,
|
|
53
|
+
base: input.base,
|
|
54
|
+
}
|
|
55
|
+
const exec = input.forge.kernelExec ?? input.exec
|
|
56
|
+
const destinationRef = `refs/autobuild/upgrade/${randomUUID()}`
|
|
57
|
+
try {
|
|
58
|
+
if (!isValidGitBranchName(input.base)) throw new Error('invalid upgrade base branch')
|
|
59
|
+
let sha: string
|
|
60
|
+
if (input.forge.snapshotBase) {
|
|
61
|
+
sha = await input.forge.snapshotBase({
|
|
62
|
+
workspacePath: input.checkout,
|
|
63
|
+
base: input.base,
|
|
64
|
+
destinationRef,
|
|
65
|
+
})
|
|
66
|
+
} else {
|
|
67
|
+
if (!input.forge.kernelExec)
|
|
68
|
+
throw new Error('forge has no authoritative base snapshot transport')
|
|
69
|
+
const fetch = await exec(
|
|
70
|
+
[
|
|
71
|
+
'git',
|
|
72
|
+
'fetch',
|
|
73
|
+
'--no-tags',
|
|
74
|
+
'--no-write-fetch-head',
|
|
75
|
+
'--refmap=',
|
|
76
|
+
'origin',
|
|
77
|
+
`+refs/heads/${input.base}:${destinationRef}`,
|
|
78
|
+
],
|
|
79
|
+
{ cwd: input.checkout, signal: input.signal },
|
|
80
|
+
)
|
|
81
|
+
if (fetch.exitCode !== 0) throw new Error('authoritative base fetch failed')
|
|
82
|
+
const tip = await exec(['git', 'rev-parse', '--verify', `${destinationRef}^{commit}`], {
|
|
83
|
+
cwd: input.checkout,
|
|
84
|
+
signal: input.signal,
|
|
85
|
+
})
|
|
86
|
+
if (tip.exitCode !== 0) throw new Error('authoritative base commit unavailable')
|
|
87
|
+
sha = tip.stdout.trim()
|
|
88
|
+
}
|
|
89
|
+
if (!/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/.test(sha))
|
|
90
|
+
throw new Error('invalid authoritative base commit')
|
|
91
|
+
target = { ...target, sha }
|
|
92
|
+
if (input.sourceLaunch) {
|
|
93
|
+
target = {
|
|
94
|
+
mode: 'source',
|
|
95
|
+
source: input.sourceLaunch.repository,
|
|
96
|
+
base: input.base,
|
|
97
|
+
sha,
|
|
98
|
+
key: fingerprint(['source', input.sourceLaunch.repository, sha]),
|
|
99
|
+
}
|
|
100
|
+
const content = await withSourceExport(
|
|
101
|
+
{ checkout: input.checkout, sha, signal: input.signal, exec },
|
|
102
|
+
async (root) => {
|
|
103
|
+
// Even malformed metadata has an input identity independent of SHA.
|
|
104
|
+
// Excluded-file advances must not repeatedly raise the same failure.
|
|
105
|
+
const metadata = await Promise.all(
|
|
106
|
+
['package.json', 'bun.lock', 'bunfig.toml'].map((name) =>
|
|
107
|
+
readFile(join(root, name), 'utf8').catch(() => null),
|
|
108
|
+
),
|
|
109
|
+
)
|
|
110
|
+
target.key = fingerprint(['source-input', input.sourceLaunch!.repository, metadata])
|
|
111
|
+
return sourceContent(root, input.sourceLaunch!.repository, input.signal)
|
|
112
|
+
},
|
|
113
|
+
)
|
|
114
|
+
return { ...target, ...content }
|
|
115
|
+
}
|
|
116
|
+
const blob = async (name: string, optional = false) => {
|
|
117
|
+
const result = await exec(['git', 'show', `${sha}:${name}`], { cwd: input.checkout })
|
|
118
|
+
if (result.exitCode === 0) return result.stdout
|
|
119
|
+
if (optional) {
|
|
120
|
+
const tree = await exec(['git', 'ls-tree', sha, '--', name], { cwd: input.checkout })
|
|
121
|
+
if (tree.exitCode === 0 && tree.stdout.trim() === '') return undefined
|
|
122
|
+
}
|
|
123
|
+
throw new Error(`cannot read authoritative ${name}`)
|
|
124
|
+
}
|
|
125
|
+
const manifestText = await blob('package.json', true)
|
|
126
|
+
target.key = fingerprint([input.base, manifestText])
|
|
127
|
+
const manifest = manifestText === undefined ? {} : object(JSON.parse(manifestText))
|
|
128
|
+
const name = '@defrex/autobuild'
|
|
129
|
+
const declarations = (['dependencies', 'devDependencies'] as const).flatMap((section) => {
|
|
130
|
+
const value = object(manifest[section])[name]
|
|
131
|
+
return value === undefined ? [] : [{ section, value }]
|
|
132
|
+
})
|
|
133
|
+
if (!declarations.length) {
|
|
134
|
+
target = {
|
|
135
|
+
mode: 'installed',
|
|
136
|
+
source: input.originalInstall,
|
|
137
|
+
installRoot: input.originalInstall,
|
|
138
|
+
key: fingerprint(input.originalInstall),
|
|
139
|
+
}
|
|
140
|
+
const installed = await readDistributionIdentity(input.originalInstall)
|
|
141
|
+
target.version = installed.version
|
|
142
|
+
target.key = fingerprint([target.source, installed.version, installed.commit])
|
|
143
|
+
return target
|
|
144
|
+
}
|
|
145
|
+
const lockText = await blob('bun.lock')
|
|
146
|
+
target.key = fingerprint([input.base, manifestText, lockText])
|
|
147
|
+
const lock = object(Bun.JSONC.parse(lockText!))
|
|
148
|
+
for (const { section, value } of declarations) {
|
|
149
|
+
if (typeof value !== 'string')
|
|
150
|
+
throw new Error('conflicting Autobuild dependency declarations')
|
|
151
|
+
if (lockDependency(lock, name, section) !== value)
|
|
152
|
+
throw new Error('authoritative manifest and Bun lock dependency disagree')
|
|
153
|
+
}
|
|
154
|
+
const dependency = declarations[0]!.value as string
|
|
155
|
+
const record = lockPackageRecord(lock, name)
|
|
156
|
+
if (!record || typeof record[0] !== 'string')
|
|
157
|
+
throw new Error('missing locked Autobuild package')
|
|
158
|
+
target = { ...target, dependency, record, key: fingerprint(['pinned', declarations, record]) }
|
|
159
|
+
const prefix = `${name}@`
|
|
160
|
+
if (!record[0].startsWith(prefix)) throw new Error('invalid locked Autobuild package')
|
|
161
|
+
const locked = record[0].slice(prefix.length)
|
|
162
|
+
const forge = githubDependency(dependency)
|
|
163
|
+
if (forge) {
|
|
164
|
+
if (
|
|
165
|
+
declarations.some(({ value }) => {
|
|
166
|
+
const other = githubDependency(value)
|
|
167
|
+
return !other || other.owner !== forge.owner || other.repository !== forge.repository
|
|
168
|
+
})
|
|
169
|
+
)
|
|
170
|
+
throw new Error('conflicting Autobuild forge declarations')
|
|
171
|
+
if (!new RegExp(`^github:${forge.owner}/${forge.repository}#[a-f0-9]{7,40}$`).test(locked))
|
|
172
|
+
throw new Error('invalid locked Autobuild forge commit')
|
|
173
|
+
return target
|
|
174
|
+
}
|
|
175
|
+
if (
|
|
176
|
+
typeof record[1] !== 'string' ||
|
|
177
|
+
semver.valid(locked) !== locked ||
|
|
178
|
+
declarations.some(
|
|
179
|
+
({ value }) => typeof value !== 'string' || !semver.satisfies(locked, value),
|
|
180
|
+
)
|
|
181
|
+
)
|
|
182
|
+
throw new Error('unsupported Autobuild dependency or manifest/lock version mismatch')
|
|
183
|
+
target.version = locked
|
|
184
|
+
return target
|
|
185
|
+
} catch (error) {
|
|
186
|
+
throw new TargetResolutionError(target, error instanceof Error ? error.message : String(error))
|
|
187
|
+
} finally {
|
|
188
|
+
await exec(['git', 'update-ref', '-d', destinationRef], { cwd: input.checkout }).catch(() => {})
|
|
189
|
+
}
|
|
190
|
+
}
|