@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,221 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import { dirname } from 'node:path'
|
|
3
|
+
import type { RuntimeIdentity } from '../events/runtime'
|
|
4
|
+
import type { RepositoryEventWrite } from '../events/repository'
|
|
5
|
+
import { DISPATCHER } from '../events/envelope'
|
|
6
|
+
import {
|
|
7
|
+
TARGET_CHECK_INTERVAL_MS,
|
|
8
|
+
TargetResolutionError,
|
|
9
|
+
type UpgradeTarget,
|
|
10
|
+
} from './dispatch-upgrade-target'
|
|
11
|
+
import type { CachedRuntime } from './dispatch-runtime-cache'
|
|
12
|
+
import { releasePreparedRuntime, reservePreparedRuntime } from './dispatch-runtime-use'
|
|
13
|
+
import { captureRuntimeIdentity } from '../runtime-identity'
|
|
14
|
+
import type { DispatchUpgradeState, Handoff } from '../kernel/dispatch-upgrade'
|
|
15
|
+
|
|
16
|
+
export function publicTarget(target: UpgradeTarget) {
|
|
17
|
+
const { mode, key, source, version, base, sha } = target
|
|
18
|
+
return {
|
|
19
|
+
mode,
|
|
20
|
+
key,
|
|
21
|
+
source,
|
|
22
|
+
...(version ? { version } : {}),
|
|
23
|
+
...(base ? { base } : {}),
|
|
24
|
+
...(sha ? { sha } : {}),
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** Bun locks may abbreviate commits. Prefer the captured Bun tag over a
|
|
28
|
+
* package's gitHead, which can describe an earlier npm publication. */
|
|
29
|
+
function matchesRunningForge(
|
|
30
|
+
target: UpgradeTarget,
|
|
31
|
+
from: RuntimeIdentity,
|
|
32
|
+
bunTag?: string,
|
|
33
|
+
): boolean {
|
|
34
|
+
const record = target.record?.[0]
|
|
35
|
+
const locked =
|
|
36
|
+
typeof record === 'string'
|
|
37
|
+
? /^@defrex\/autobuild@github:([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)#([a-f0-9]{7,40})$/i.exec(
|
|
38
|
+
record,
|
|
39
|
+
)
|
|
40
|
+
: null
|
|
41
|
+
if (!locked) return false
|
|
42
|
+
const commit = locked[3]!.toLowerCase()
|
|
43
|
+
if (bunTag !== undefined) {
|
|
44
|
+
const prefix = `${locked[1]}-${locked[2]}-`
|
|
45
|
+
if (!bunTag.startsWith(prefix)) return false
|
|
46
|
+
const running = bunTag.slice(prefix.length).toLowerCase()
|
|
47
|
+
return (
|
|
48
|
+
/^[a-f0-9]{7,40}$/.test(running) && (running.startsWith(commit) || commit.startsWith(running))
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
return (
|
|
52
|
+
/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/i.test(from.commit) &&
|
|
53
|
+
from.commit.toLowerCase().startsWith(commit)
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface UpgradeControllerInput {
|
|
58
|
+
run: string
|
|
59
|
+
lineage: string
|
|
60
|
+
from: RuntimeIdentity
|
|
61
|
+
failedKeys: string[]
|
|
62
|
+
hold?: DispatchUpgradeState['lastRollback']
|
|
63
|
+
clearHold?(root: string): void
|
|
64
|
+
resolved?: DispatchUpgradeState['runs'][string]['resolved']
|
|
65
|
+
currentKey?: string
|
|
66
|
+
storeChangedSinceFailure?(): Promise<boolean>
|
|
67
|
+
runningBunTag?: string
|
|
68
|
+
reservationOwner?: number
|
|
69
|
+
enabled(): boolean
|
|
70
|
+
resolve(signal: AbortSignal): Promise<UpgradeTarget>
|
|
71
|
+
prepare(target: UpgradeTarget, signal: AbortSignal): Promise<CachedRuntime>
|
|
72
|
+
append(event: RepositoryEventWrite): Promise<unknown>
|
|
73
|
+
cutover(handoff: Handoff): Promise<boolean>
|
|
74
|
+
redact?(message: string): string
|
|
75
|
+
intervalMs?: number
|
|
76
|
+
}
|
|
77
|
+
/** One preparation at a time. Discovery continues after failure; installation does not. */
|
|
78
|
+
export class DispatchUpgradeController {
|
|
79
|
+
private readonly abort = new AbortController()
|
|
80
|
+
private timer?: ReturnType<typeof setTimeout>
|
|
81
|
+
private pending?: Promise<void>
|
|
82
|
+
private preparation?: AbortController
|
|
83
|
+
private readonly failed: Set<string>
|
|
84
|
+
private resolved?: string
|
|
85
|
+
private hold?: DispatchUpgradeState['lastRollback']
|
|
86
|
+
constructor(private readonly input: UpgradeControllerInput) {
|
|
87
|
+
this.failed = new Set(input.failedKeys)
|
|
88
|
+
this.hold = input.hold?.payload.lineage === input.lineage ? input.hold : undefined
|
|
89
|
+
this.resolved = input.resolved ? JSON.stringify(input.resolved.payload.target) : undefined
|
|
90
|
+
}
|
|
91
|
+
start(): void {
|
|
92
|
+
this.schedule(0)
|
|
93
|
+
}
|
|
94
|
+
changed(): void {
|
|
95
|
+
if (!this.input.enabled()) this.preparation?.abort()
|
|
96
|
+
}
|
|
97
|
+
private schedule(ms: number): void {
|
|
98
|
+
if (this.abort.signal.aborted) return
|
|
99
|
+
this.timer = setTimeout(() => {
|
|
100
|
+
const startedAt = Date.now()
|
|
101
|
+
this.pending = this.check()
|
|
102
|
+
.catch(() => {})
|
|
103
|
+
.finally(() =>
|
|
104
|
+
this.schedule(
|
|
105
|
+
Math.max(
|
|
106
|
+
0,
|
|
107
|
+
(this.input.intervalMs ?? TARGET_CHECK_INTERVAL_MS) - (Date.now() - startedAt),
|
|
108
|
+
),
|
|
109
|
+
),
|
|
110
|
+
)
|
|
111
|
+
}, ms)
|
|
112
|
+
}
|
|
113
|
+
async stop(): Promise<void> {
|
|
114
|
+
this.abort.abort()
|
|
115
|
+
this.preparation?.abort()
|
|
116
|
+
clearTimeout(this.timer)
|
|
117
|
+
await this.pending
|
|
118
|
+
}
|
|
119
|
+
async check(): Promise<void> {
|
|
120
|
+
if (this.abort.signal.aborted || this.preparation || !this.input.enabled()) return
|
|
121
|
+
const preparation = new AbortController()
|
|
122
|
+
this.preparation = preparation
|
|
123
|
+
let target: UpgradeTarget | undefined
|
|
124
|
+
let runtime: CachedRuntime | undefined
|
|
125
|
+
const correlation = randomUUID()
|
|
126
|
+
const identity = () => ({
|
|
127
|
+
correlation,
|
|
128
|
+
run: this.input.run,
|
|
129
|
+
lineage: this.input.lineage,
|
|
130
|
+
from: this.input.from,
|
|
131
|
+
target: publicTarget(target!),
|
|
132
|
+
})
|
|
133
|
+
try {
|
|
134
|
+
target = await this.input.resolve(
|
|
135
|
+
AbortSignal.any([preparation.signal, AbortSignal.timeout(30_000)]),
|
|
136
|
+
)
|
|
137
|
+
if (preparation.signal.aborted || !this.input.enabled()) return
|
|
138
|
+
const observation = JSON.stringify(publicTarget(target))
|
|
139
|
+
if (observation !== this.resolved) {
|
|
140
|
+
await this.input.append({
|
|
141
|
+
actor: DISPATCHER,
|
|
142
|
+
type: 'dispatcher.upgrade-target-resolved',
|
|
143
|
+
payload: {
|
|
144
|
+
run: this.input.run,
|
|
145
|
+
lineage: this.input.lineage,
|
|
146
|
+
target: publicTarget(target),
|
|
147
|
+
},
|
|
148
|
+
})
|
|
149
|
+
this.resolved = observation
|
|
150
|
+
}
|
|
151
|
+
if (this.hold) {
|
|
152
|
+
if (this.hold.payload.target.key === target.key) return
|
|
153
|
+
await this.input.append({
|
|
154
|
+
actor: DISPATCHER,
|
|
155
|
+
type: 'dispatcher.rollback-hold-cleared',
|
|
156
|
+
payload: {
|
|
157
|
+
correlation: this.hold.payload.correlation,
|
|
158
|
+
run: this.input.run,
|
|
159
|
+
lineage: this.input.lineage,
|
|
160
|
+
target: publicTarget(target),
|
|
161
|
+
},
|
|
162
|
+
})
|
|
163
|
+
this.input.clearHold?.(this.hold.payload.failedRoot)
|
|
164
|
+
this.hold = undefined
|
|
165
|
+
}
|
|
166
|
+
if (this.failed.has(target.key)) {
|
|
167
|
+
if (!(await this.input.storeChangedSinceFailure?.())) return
|
|
168
|
+
this.failed.delete(target.key)
|
|
169
|
+
}
|
|
170
|
+
if (target.key === this.input.currentKey) return
|
|
171
|
+
if (matchesRunningForge(target, this.input.from, this.input.runningBunTag)) return
|
|
172
|
+
if (target.mode !== 'source' && target.version === this.input.from.version) return
|
|
173
|
+
await this.input.append({
|
|
174
|
+
actor: DISPATCHER,
|
|
175
|
+
type: 'dispatcher.upgrade-detected',
|
|
176
|
+
payload: identity(),
|
|
177
|
+
})
|
|
178
|
+
runtime = await this.input.prepare(target, preparation.signal)
|
|
179
|
+
if (preparation.signal.aborted || !this.input.enabled()) return
|
|
180
|
+
const handoff: Handoff = {
|
|
181
|
+
...identity(),
|
|
182
|
+
to: captureRuntimeIdentity(runtime.root),
|
|
183
|
+
root: runtime.root,
|
|
184
|
+
cacheId: runtime.id,
|
|
185
|
+
}
|
|
186
|
+
reservePreparedRuntime(
|
|
187
|
+
dirname(dirname(runtime.root)),
|
|
188
|
+
runtime.id,
|
|
189
|
+
this.input.reservationOwner,
|
|
190
|
+
)
|
|
191
|
+
if (await this.input.cutover(handoff)) {
|
|
192
|
+
// Retain the reservation through old-generation exit. The launcher transfers it under the cache lock.
|
|
193
|
+
runtime = undefined
|
|
194
|
+
this.abort.abort()
|
|
195
|
+
}
|
|
196
|
+
} catch (error) {
|
|
197
|
+
if (preparation.signal.aborted || this.abort.signal.aborted) return
|
|
198
|
+
if (error instanceof TargetResolutionError) target = error.target
|
|
199
|
+
if (target && !this.failed.has(target.key)) {
|
|
200
|
+
await this.input.append({
|
|
201
|
+
actor: DISPATCHER,
|
|
202
|
+
type: 'dispatcher.upgrade-failed',
|
|
203
|
+
payload: {
|
|
204
|
+
...identity(),
|
|
205
|
+
reason:
|
|
206
|
+
this.input.redact?.(
|
|
207
|
+
error instanceof Error ? error.message : 'target preparation failed',
|
|
208
|
+
) ?? 'target preparation failed',
|
|
209
|
+
},
|
|
210
|
+
})
|
|
211
|
+
this.failed.add(target.key)
|
|
212
|
+
}
|
|
213
|
+
} finally {
|
|
214
|
+
if (runtime) {
|
|
215
|
+
releasePreparedRuntime(runtime.root, this.input.reservationOwner)
|
|
216
|
+
releasePreparedRuntime(runtime.root)
|
|
217
|
+
}
|
|
218
|
+
if (this.preparation === preparation) this.preparation = undefined
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { captureRuntimeIdentity } from '../runtime-identity'
|
|
2
|
+
import { withSourceExport, sourceContent } from './dispatch-upgrade-source'
|
|
3
|
+
import { mkdtemp, readFile, readdir, rm, writeFile, copyFile } from 'node:fs/promises'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { tmpdir } from 'node:os'
|
|
6
|
+
import {
|
|
7
|
+
cachedRuntime,
|
|
8
|
+
validateRuntime,
|
|
9
|
+
prepareRuntime,
|
|
10
|
+
guestArchiveProvenance,
|
|
11
|
+
type CachedRuntime,
|
|
12
|
+
} from './dispatch-runtime-cache'
|
|
13
|
+
import { readDistributionIdentity, lockPackageRecord } from './installation'
|
|
14
|
+
import { reserveExistingRuntime, releasePreparedRuntime } from './dispatch-runtime-use'
|
|
15
|
+
import {
|
|
16
|
+
DISPATCH_LAUNCH_VERSION,
|
|
17
|
+
DISPATCH_GENERATION_ENV,
|
|
18
|
+
generationArguments,
|
|
19
|
+
} from './dispatch-launch-contract'
|
|
20
|
+
import type { UpgradeTarget } from './dispatch-upgrade-target'
|
|
21
|
+
|
|
22
|
+
/** Compare the staging lock receipt with the previously validated base lock.
|
|
23
|
+
* Registry records carry a URL at index 1; forge records carry dependency
|
|
24
|
+
* metadata there and identify their source by resolution and Bun tag instead. */
|
|
25
|
+
export function validateUpgradeLockReceipt(
|
|
26
|
+
authoritative: unknown[],
|
|
27
|
+
received: unknown[] | undefined,
|
|
28
|
+
): void {
|
|
29
|
+
if (!received || received[0] !== authoritative[0])
|
|
30
|
+
throw new Error('installed package contradicts authoritative lock')
|
|
31
|
+
if (String(authoritative[0]).includes('@github:')) {
|
|
32
|
+
if (received[2] !== authoritative[2])
|
|
33
|
+
throw new Error('installed forge identity contradicts authoritative lock')
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
if (received[1] !== authoritative[1])
|
|
37
|
+
throw new Error('installed package contradicts authoritative lock')
|
|
38
|
+
const integrity = authoritative.at(-1)
|
|
39
|
+
if (typeof integrity === 'string' && integrity.startsWith('sha') && received.at(-1) !== integrity)
|
|
40
|
+
throw new Error('installed integrity contradicts authoritative lock')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function prepareUpgrade(input: {
|
|
44
|
+
target: UpgradeTarget
|
|
45
|
+
checkout: string
|
|
46
|
+
signal: AbortSignal
|
|
47
|
+
env: Record<string, string | undefined>
|
|
48
|
+
}): Promise<CachedRuntime> {
|
|
49
|
+
const { target, checkout, signal } = input
|
|
50
|
+
signal.throwIfAborted()
|
|
51
|
+
const validate = async (runtime: CachedRuntime) => {
|
|
52
|
+
const contract = await readFile(
|
|
53
|
+
join(runtime.root, 'packages/core/src/cli/dispatch-launch-contract.ts'),
|
|
54
|
+
'utf8',
|
|
55
|
+
)
|
|
56
|
+
if (!new RegExp(`DISPATCH_LAUNCH_VERSION = ${DISPATCH_LAUNCH_VERSION}\\b`).test(contract))
|
|
57
|
+
throw new Error('launcher contract changed; manual restart required')
|
|
58
|
+
if (!contract.includes('DISPATCH_OVERLAP_VERSION = 1'))
|
|
59
|
+
throw new Error('target lacks validated overlap preflight; manual restart required')
|
|
60
|
+
await readFile(join(runtime.root, 'packages/core/src/cli/dispatch-handoff.ts'), 'utf8').catch(
|
|
61
|
+
() => {
|
|
62
|
+
throw new Error('target lacks durable handoff support; manual restart required')
|
|
63
|
+
},
|
|
64
|
+
)
|
|
65
|
+
const probe = Bun.spawn(
|
|
66
|
+
[process.execPath, ...generationArguments(runtime.root, ['--read-handoff', 'dispatch'])],
|
|
67
|
+
{
|
|
68
|
+
cwd: checkout,
|
|
69
|
+
env: {
|
|
70
|
+
...input.env,
|
|
71
|
+
[DISPATCH_GENERATION_ENV]: JSON.stringify({
|
|
72
|
+
version: DISPATCH_LAUNCH_VERSION,
|
|
73
|
+
runtimeRoot: runtime.root,
|
|
74
|
+
checkout,
|
|
75
|
+
}),
|
|
76
|
+
},
|
|
77
|
+
stdin: 'ignore',
|
|
78
|
+
stdout: 'pipe',
|
|
79
|
+
stderr: 'pipe',
|
|
80
|
+
signal: AbortSignal.any([signal, AbortSignal.timeout(30_000)]),
|
|
81
|
+
},
|
|
82
|
+
)
|
|
83
|
+
const [code, output] = await Promise.all([
|
|
84
|
+
probe.exited,
|
|
85
|
+
new Response(probe.stdout).text(),
|
|
86
|
+
new Response(probe.stderr).text(),
|
|
87
|
+
])
|
|
88
|
+
let receipt: { version?: number; handoff?: unknown }
|
|
89
|
+
try {
|
|
90
|
+
receipt = JSON.parse(output)
|
|
91
|
+
} catch {
|
|
92
|
+
throw new Error('target generation handoff reader validation failed')
|
|
93
|
+
}
|
|
94
|
+
if (code !== 0 || receipt.version !== DISPATCH_LAUNCH_VERSION || receipt.handoff !== null)
|
|
95
|
+
throw new Error('target generation handoff reader is incompatible; manual restart required')
|
|
96
|
+
return runtime
|
|
97
|
+
}
|
|
98
|
+
const cache = join(checkout, '.autobuild/runtime')
|
|
99
|
+
// Cached targets retain their authoritative lock fingerprint, without registry credentials.
|
|
100
|
+
if (target.mode === 'pinned' || target.mode === 'source') {
|
|
101
|
+
for (const name of await readdir(cache).catch(() => [] as string[])) {
|
|
102
|
+
let key: string
|
|
103
|
+
try {
|
|
104
|
+
if (target.mode === 'source') {
|
|
105
|
+
const receipt = JSON.parse(
|
|
106
|
+
await readFile(join(cache, name, 'source-receipt.json'), 'utf8'),
|
|
107
|
+
)
|
|
108
|
+
if (receipt.commit !== target.sha) continue
|
|
109
|
+
key = receipt.upgradeKey
|
|
110
|
+
} else key = await readFile(join(cache, name, 'upgrade-key'), 'utf8')
|
|
111
|
+
} catch {
|
|
112
|
+
continue
|
|
113
|
+
}
|
|
114
|
+
if (key !== target.key) continue
|
|
115
|
+
const runtime = await cachedRuntime(join(cache, name, 'distribution'))
|
|
116
|
+
if (!runtime?.guestArchive || !reserveExistingRuntime(runtime.root)) continue
|
|
117
|
+
try {
|
|
118
|
+
await validateRuntime(runtime)
|
|
119
|
+
if (target.mode === 'source' && captureRuntimeIdentity(runtime.root).commit !== target.sha)
|
|
120
|
+
throw new Error('cached source runtime commit contradicts target')
|
|
121
|
+
signal.throwIfAborted()
|
|
122
|
+
return await validate(runtime)
|
|
123
|
+
} catch (error) {
|
|
124
|
+
releasePreparedRuntime(runtime.root)
|
|
125
|
+
throw error
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (target.mode === 'source') {
|
|
130
|
+
return withSourceExport({ checkout, sha: target.sha!, signal }, async (root) => {
|
|
131
|
+
const content = await sourceContent(root, target.source, signal)
|
|
132
|
+
if (content.key !== target.key) throw new Error('source target changed during preparation')
|
|
133
|
+
// Require a committed lock explicitly: frozen-lockfile alone permits a missing lock.
|
|
134
|
+
await readFile(join(root, 'bun.lock'))
|
|
135
|
+
const child = Bun.spawn(
|
|
136
|
+
[process.execPath, 'install', '--frozen-lockfile', '--ignore-scripts', '--no-progress'],
|
|
137
|
+
{
|
|
138
|
+
cwd: root,
|
|
139
|
+
env: input.env,
|
|
140
|
+
stdout: 'ignore',
|
|
141
|
+
stderr: 'pipe',
|
|
142
|
+
signal: AbortSignal.any([signal, AbortSignal.timeout(120_000)]),
|
|
143
|
+
},
|
|
144
|
+
)
|
|
145
|
+
const [code] = await Promise.all([child.exited, new Response(child.stderr).text()])
|
|
146
|
+
signal.throwIfAborted()
|
|
147
|
+
if (code !== 0) throw new Error('source dependency installation failed')
|
|
148
|
+
const path = join(root, 'package.json')
|
|
149
|
+
const manifest = JSON.parse(await readFile(path, 'utf8'))
|
|
150
|
+
manifest.gitHead = target.sha
|
|
151
|
+
await writeFile(path, JSON.stringify(manifest))
|
|
152
|
+
let runtime: CachedRuntime | undefined
|
|
153
|
+
try {
|
|
154
|
+
runtime = await prepareRuntime(
|
|
155
|
+
root,
|
|
156
|
+
checkout,
|
|
157
|
+
{
|
|
158
|
+
upgradeKey: target.key,
|
|
159
|
+
commit: target.sha!,
|
|
160
|
+
},
|
|
161
|
+
{ provenance: 'source' },
|
|
162
|
+
)
|
|
163
|
+
if (captureRuntimeIdentity(runtime.root).commit !== target.sha)
|
|
164
|
+
throw new Error('source runtime commit contradicts target')
|
|
165
|
+
signal.throwIfAborted()
|
|
166
|
+
return await validate(runtime)
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (runtime) releasePreparedRuntime(runtime.root)
|
|
169
|
+
throw error
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
if (target.mode === 'installed') {
|
|
174
|
+
const identity = await readDistributionIdentity(target.installRoot!)
|
|
175
|
+
if (identity.version !== target.version)
|
|
176
|
+
throw new Error('original installation changed during preparation')
|
|
177
|
+
const runtime = await prepareRuntime(identity.root, checkout, undefined, {
|
|
178
|
+
provenance: await guestArchiveProvenance(identity.root),
|
|
179
|
+
})
|
|
180
|
+
try {
|
|
181
|
+
signal.throwIfAborted()
|
|
182
|
+
return await validate(runtime)
|
|
183
|
+
} catch (error) {
|
|
184
|
+
releasePreparedRuntime(runtime.root)
|
|
185
|
+
throw error
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const owner = await mkdtemp(join(tmpdir(), 'ab-upgrade-'))
|
|
189
|
+
let runtime: CachedRuntime | undefined
|
|
190
|
+
try {
|
|
191
|
+
const name = '@defrex/autobuild'
|
|
192
|
+
const locked = String(target.record![0]).slice(`${name}@`.length)
|
|
193
|
+
await writeFile(
|
|
194
|
+
join(owner, 'package.json'),
|
|
195
|
+
JSON.stringify({ private: true, dependencies: { [name]: locked } }),
|
|
196
|
+
)
|
|
197
|
+
for (const config of ['.npmrc', 'bunfig.toml']) {
|
|
198
|
+
try {
|
|
199
|
+
await copyFile(join(checkout, config), join(owner, config))
|
|
200
|
+
} catch (error) {
|
|
201
|
+
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const child = Bun.spawn([process.execPath, 'install', '--ignore-scripts', '--no-progress'], {
|
|
205
|
+
cwd: owner,
|
|
206
|
+
env: input.env,
|
|
207
|
+
stdout: 'ignore',
|
|
208
|
+
stderr: 'pipe',
|
|
209
|
+
})
|
|
210
|
+
const abort = () => child.kill('SIGTERM')
|
|
211
|
+
signal.addEventListener('abort', abort, { once: true })
|
|
212
|
+
const timer = setTimeout(abort, 120_000)
|
|
213
|
+
try {
|
|
214
|
+
// Installer diagnostics can contain registry credentials. Only emit a classified error.
|
|
215
|
+
const drain = new Response(child.stderr).text()
|
|
216
|
+
const code = await child.exited
|
|
217
|
+
const diagnostic = await drain
|
|
218
|
+
signal.throwIfAborted()
|
|
219
|
+
if (code !== 0) {
|
|
220
|
+
const reason = /401|403|unauthorized|forbidden/i.test(diagnostic)
|
|
221
|
+
? 'registry authentication failed'
|
|
222
|
+
: /404|no version matching|not found/i.test(diagnostic)
|
|
223
|
+
? 'target package version is unavailable'
|
|
224
|
+
: /ENOTFOUND|ECONN|unable to connect|connection refused/i.test(diagnostic)
|
|
225
|
+
? 'registry is unreachable'
|
|
226
|
+
: /integrity|checksum/i.test(diagnostic)
|
|
227
|
+
? 'package integrity validation failed'
|
|
228
|
+
: `target package installation failed (status ${code})`
|
|
229
|
+
throw new Error(reason)
|
|
230
|
+
}
|
|
231
|
+
} finally {
|
|
232
|
+
clearTimeout(timer)
|
|
233
|
+
signal.removeEventListener('abort', abort)
|
|
234
|
+
}
|
|
235
|
+
const installed = await readDistributionIdentity(join(owner, 'node_modules', name))
|
|
236
|
+
const generated = Bun.JSONC.parse(await readFile(join(owner, 'bun.lock'), 'utf8')) as Record<
|
|
237
|
+
string,
|
|
238
|
+
unknown
|
|
239
|
+
>
|
|
240
|
+
const record = lockPackageRecord(generated, name)
|
|
241
|
+
// The generated lock is only a receipt for the previously validated authoritative record.
|
|
242
|
+
validateUpgradeLockReceipt(target.record!, record)
|
|
243
|
+
if (target.version !== undefined && installed.version !== target.version)
|
|
244
|
+
throw new Error('installed version contradicts target')
|
|
245
|
+
runtime = await prepareRuntime(installed.root, checkout, undefined, {
|
|
246
|
+
provenance: String(target.record![0]).includes('@github:') ? 'source' : 'registry',
|
|
247
|
+
})
|
|
248
|
+
await validate(runtime)
|
|
249
|
+
signal.throwIfAborted()
|
|
250
|
+
await writeFile(join(cache, runtime.id, 'upgrade-key'), target.key)
|
|
251
|
+
return runtime
|
|
252
|
+
} catch (error) {
|
|
253
|
+
if (runtime) releasePreparedRuntime(runtime.root)
|
|
254
|
+
throw error
|
|
255
|
+
} finally {
|
|
256
|
+
await rm(owner, { recursive: true, force: true })
|
|
257
|
+
}
|
|
258
|
+
}
|