@defrex/autobuild 1.0.0-beta.1 → 1.0.0-beta.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/SPEC.md +285 -53
- package/bin/ab-build-runner.ts +10 -8
- package/docs/architecture.md +173 -23
- package/docs/configuration.md +35 -4
- package/docs/hosted-service.md +35 -0
- package/docs/integration-tree.md +81 -4
- package/docs/landing-policy.md +11 -0
- package/docs/migration-1.0.md +9 -0
- package/docs/operator-api.md +18 -0
- package/docs/remote-store-protocol.md +11 -0
- package/docs/restacking.md +42 -7
- package/docs/setup.md +21 -0
- package/docs/ticket-dependencies.md +6 -45
- package/package.json +3 -3
- package/packages/core/src/cli/attention.ts +4 -1
- package/packages/core/src/cli/binary.ts +17 -1
- package/packages/core/src/cli/context.ts +53 -1
- package/packages/core/src/cli/dashboard/actions.ts +4 -0
- package/packages/core/src/cli/dashboard/detail.ts +45 -1
- package/packages/core/src/cli/dashboard/facts.ts +7 -2
- package/packages/core/src/cli/dashboard/integration-detail.ts +142 -0
- package/packages/core/src/cli/dashboard/integration-row.ts +248 -0
- package/packages/core/src/cli/dashboard/landing-display.ts +64 -0
- package/packages/core/src/cli/dashboard/model.ts +140 -16
- package/packages/core/src/cli/dashboard/poll.ts +40 -4
- package/packages/core/src/cli/dashboard/render.ts +106 -5
- package/packages/core/src/cli/dashboard/transcript.ts +2 -1
- package/packages/core/src/cli/dispatch-frontend.ts +6 -0
- package/packages/core/src/cli/dispatch.ts +44 -14
- package/packages/core/src/cli/help.ts +16 -1
- package/packages/core/src/cli/main.ts +37 -0
- package/packages/core/src/cli/publication-settlement.ts +49 -5
- package/packages/core/src/cli/repository-status.ts +97 -2
- package/packages/core/src/cli/status.ts +24 -1
- package/packages/core/src/cli/terminals.ts +94 -93
- package/packages/core/src/cli/wait.ts +11 -2
- package/packages/core/src/cli/watch.ts +12 -0
- package/packages/core/src/config/schema.ts +3 -0
- package/packages/core/src/events/catalog.ts +38 -0
- package/packages/core/src/events/compatibility.ts +183 -0
- package/packages/core/src/events/native-session.ts +13 -0
- package/packages/core/src/events/payloads.ts +148 -2
- package/packages/core/src/events/policy.ts +209 -0
- package/packages/core/src/events/repository.ts +48 -0
- package/packages/core/src/integration/harness.ts +4 -4
- package/packages/core/src/kernel/__snapshots__/{reducer-registry.test.ts.snap → reducer-accumulator-shapes.json} +771 -538
- package/packages/core/src/kernel/change-presence.ts +4 -2
- package/packages/core/src/kernel/dispatch-capacity.ts +119 -0
- package/packages/core/src/kernel/dispatcher-liveness.ts +5 -1
- package/packages/core/src/kernel/engine.ts +25 -5
- package/packages/core/src/kernel/forge-health.ts +19 -0
- package/packages/core/src/kernel/generators/build-log.ts +18 -0
- package/packages/core/src/kernel/generators/node-restack-log.ts +1 -1
- package/packages/core/src/kernel/generators/repository-journals.ts +10 -0
- package/packages/core/src/kernel/guarded-forge.ts +159 -0
- package/packages/core/src/kernel/incremental.ts +4 -2
- package/packages/core/src/kernel/integration-completeness.ts +61 -0
- package/packages/core/src/kernel/integration-landing.ts +5 -4
- package/packages/core/src/kernel/janitor-failure.ts +36 -0
- package/packages/core/src/kernel/landing-publication.ts +35 -15
- package/packages/core/src/kernel/landing-state.ts +7 -0
- package/packages/core/src/kernel/leaf-restack-state.ts +1 -1
- package/packages/core/src/kernel/log-index.ts +12 -1
- package/packages/core/src/kernel/mixed-version.ts +52 -0
- package/packages/core/src/kernel/node-landing.ts +14 -11
- package/packages/core/src/kernel/node-restack-demand.ts +29 -0
- package/packages/core/src/kernel/node-restack-state.ts +46 -16
- package/packages/core/src/kernel/node-restack-transaction.ts +151 -8
- package/packages/core/src/kernel/node-restack.ts +34 -10
- package/packages/core/src/kernel/publication.ts +22 -0
- package/packages/core/src/kernel/read-deferrals.ts +111 -0
- package/packages/core/src/kernel/read-facts.ts +27 -6
- package/packages/core/src/kernel/reconcile-restack.ts +23 -2
- package/packages/core/src/kernel/reducer-registry.ts +15 -2
- package/packages/core/src/kernel/reducer.ts +196 -10
- package/packages/core/src/kernel/repository-attention.ts +38 -1
- package/packages/core/src/kernel/terminal-guard.ts +11 -4
- package/packages/core/src/operator/annotations.ts +6 -0
- package/packages/core/src/operator/index.ts +2 -0
- package/packages/core/src/operator/presentation.ts +5 -0
- package/packages/core/src/operator/query.ts +45 -11
- package/packages/core/src/operator/registry.ts +17 -1
- package/packages/core/src/operator/restack.ts +51 -0
- package/packages/core/src/plugin-sdk/index.ts +10 -0
- package/packages/core/src/plugins/manifest.ts +1 -1
- package/packages/core/src/ports/forge/contract.ts +23 -0
- package/packages/core/src/ports/forge/fake.ts +6 -2
- package/packages/core/src/ports/forge/git-rewrite.ts +92 -64
- package/packages/core/src/ports/forge/github-transport.ts +45 -2
- package/packages/core/src/ports/forge/github.ts +56 -17
- package/packages/core/src/ports/forge/local-git.ts +7 -2
- package/packages/core/src/ports/forge-failure.ts +40 -0
- package/packages/core/src/ports/forge-request-context.ts +35 -0
- package/packages/core/src/ports/forge-telemetry.ts +16 -0
- package/packages/core/src/ports/runner/claude.ts +136 -9
- package/packages/core/src/ports/runner/codex.ts +112 -7
- package/packages/core/src/ports/runner/contract.ts +78 -0
- package/packages/core/src/ports/runner/fake.ts +59 -1
- package/packages/core/src/ports/runner/native-cli-fixture.ts +86 -0
- package/packages/core/src/ports/runner/native-process-fixture.ts +38 -0
- package/packages/core/src/ports/runner/native-session.ts +110 -0
- package/packages/core/src/ports/runner/pi-rpc.ts +7 -1
- package/packages/core/src/ports/runner/pi.ts +162 -2
- package/packages/core/src/ports/types.ts +53 -5
- package/packages/core/src/ports/workspace/git-worktree.ts +45 -51
- package/packages/core/src/ports/workspace/vercel-sandbox.ts +28 -12
- package/packages/core/src/processes/build-child-exit.ts +50 -2
- package/packages/core/src/processes/build-child.ts +15 -7
- package/packages/core/src/processes/build-runner.ts +909 -213
- package/packages/core/src/processes/dispatcher.ts +652 -334
- package/packages/core/src/processes/execution-no-progress.ts +15 -0
- package/packages/core/src/processes/pause-execution-fixture.ts +37 -0
- package/packages/core/src/processes/phase-failure-budget.ts +2 -1
- package/packages/core/src/processes/publication-loss.ts +7 -3
- package/packages/core/src/processes/repo-view.ts +53 -6
- package/packages/core/src/processes/tick-publication.ts +6 -2
- package/packages/core/src/runtime-identity.ts +37 -0
- package/packages/core/src/store/adapter.ts +4 -2
- package/packages/core/src/store/build-scope.ts +9 -0
- package/packages/core/src/store/contract.ts +203 -16
- package/packages/core/src/store/digest.ts +20 -2
- package/packages/core/src/store/local/schema.ts +6 -0
- package/packages/core/src/store/local/store.ts +57 -4
- package/packages/core/src/store/memory.ts +83 -2
- package/packages/core/src/store/new-build.ts +2 -2
- package/packages/core/src/store/phase-session.ts +13 -0
- package/packages/core/src/store/reduced-reads.ts +43 -4
- package/packages/core/src/store/reducer-value-shapes.json +8238 -5764
- package/packages/core/src/store/reducer-value-validation.ts +103 -3
- package/packages/core/src/store/reducer-values.ts +21 -6
- package/packages/core/src/store/remote/client.ts +145 -20
- package/packages/core/src/store/remote/index.ts +2 -0
- package/packages/core/src/store/remote/protocol.ts +19 -0
- package/packages/core/src/store/remote/version.ts +2 -0
- package/packages/core/src/store/repo-state-events.ts +3 -1
- package/packages/core/src/store/repo-tick.ts +6 -1
- package/packages/core/src/store/session-handle.ts +8 -0
- package/packages/core/src/store/snapshots.ts +21 -5
- package/packages/core/src/store/types.ts +17 -1
- package/packages/core/src/testing/dispatch-read-budget.ts +17 -2
- package/packages/core/src/testing/index.ts +7 -0
- package/packages/core/src/testing/newer-writer.ts +37 -0
- package/packages/core/src/testing/reduced-read-budget.ts +3 -0
- package/skills/guide/SKILL.md +137 -24
- package/skills/guide/references/integration-tree.md +81 -4
- package/skills/guide/references/landing-policy.md +11 -0
- package/skills/guide/references/migration-1.0.md +9 -0
- package/skills/guide/references/operator-api.md +18 -0
- package/skills/guide/references/plugin-authoring.md +47 -4
- package/skills/guide/references/remote-store-protocol.md +11 -0
- package/skills/guide/references/restacking.md +42 -7
- package/skills/guide/references/setup.md +21 -0
- package/skills/guide/references/ticket-dependencies.md +6 -45
- package/skills/implement/SKILL.md +4 -2
- package/skills/operate/SKILL.md +63 -6
- package/skills/reconcile/SKILL.md +3 -1
- package/skills/spec/SKILL.md +49 -39
- package/skills/tickets/SKILL.md +15 -43
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
import { publishExact } from '../kernel/publication'
|
|
2
|
+
import { deferMixedVersion, MixedVersionDeferral } from '../kernel/mixed-version'
|
|
3
|
+
import { hostname } from 'node:os'
|
|
4
|
+
import { resumeAgent } from '../ports/runner/native-session'
|
|
5
|
+
import type { AgentStartOpts } from '../ports/types'
|
|
6
|
+
import { readTerminalGuardBoundary } from '../store/reduced-reads'
|
|
7
|
+
import type { OpenSession } from '../kernel/reducer'
|
|
8
|
+
import { nativeSessionHandleSchema } from '../ports/runner/native-session'
|
|
9
|
+
import type { NativeSessionObserver } from '../ports/types'
|
|
1
10
|
import { suspendNativeLanding } from '../kernel/landing-publication'
|
|
2
11
|
import { integrationCurrent } from '../kernel/integration-state'
|
|
3
12
|
import { replayNodeChange } from '../kernel/node-restack'
|
|
@@ -23,8 +32,8 @@ import { refreshSummaryBoundary } from '../cli/pr-summary-publication'
|
|
|
23
32
|
* All judgment lives in the skills behind the AgentRunner; everything here is
|
|
24
33
|
* deterministic plumbing (constitution §2.1). That split makes the runner
|
|
25
34
|
* crash-safe by construction: killed mid-phase, a re-attached runner re-reads
|
|
26
|
-
* the log and
|
|
27
|
-
* from
|
|
35
|
+
* the log and continues an eligible local native session, or re-decides the
|
|
36
|
+
* same started-but-unterminated phase from durable inputs (§15.6-C).
|
|
28
37
|
*
|
|
29
38
|
* Ownership: `attach` claims or renews the build's execution lease (§7.4) so a
|
|
30
39
|
* second sandbox can never execute the same build. The local supervising kernel
|
|
@@ -62,7 +71,7 @@ import { refreshSummaryBoundary } from '../cli/pr-summary-publication'
|
|
|
62
71
|
* as every phase.
|
|
63
72
|
*/
|
|
64
73
|
import type { Config } from '../config/schema'
|
|
65
|
-
import type { AbEvent, EventWrite } from '../events/catalog'
|
|
74
|
+
import type { AbEvent, EventEnvelope, EventWrite } from '../events/catalog'
|
|
66
75
|
import type { EventPayload } from '../events/payloads'
|
|
67
76
|
import { KERNEL, agentActor, type Actor } from '../events/envelope'
|
|
68
77
|
import type { IdSource } from '../ids'
|
|
@@ -109,6 +118,10 @@ export { publicationPending } from './publication-state'
|
|
|
109
118
|
// ── Seams ────────────────────────────────────────────────────────────────────
|
|
110
119
|
|
|
111
120
|
class AutomaticRewriteDeferred extends Error {}
|
|
121
|
+
class SessionShutdown extends Error {}
|
|
122
|
+
class SessionPaused extends Error {}
|
|
123
|
+
class RecoveryBoundaryChanged extends Error {}
|
|
124
|
+
class FreshSessionRequired extends Error {}
|
|
112
125
|
|
|
113
126
|
/** Another sandbox owns the build (§7.4): its lease is live, so this runner
|
|
114
127
|
* must not execute. The dispatcher retries once the lease expires. */
|
|
@@ -173,6 +186,10 @@ export interface BuildRunnerDeps {
|
|
|
173
186
|
* applies `config.roles`, including its reserved `default` entry, to it. */
|
|
174
187
|
runtimes: RuntimeRegistry
|
|
175
188
|
workspacePath: string
|
|
189
|
+
/** Explicit provider eligibility; never inferred from a matching path. */
|
|
190
|
+
workspaceProvider?: string
|
|
191
|
+
workspaceRemote?: boolean
|
|
192
|
+
shutdownSignal?: AbortSignal
|
|
176
193
|
branch: string
|
|
177
194
|
slug: string
|
|
178
195
|
/** Shell seam for deterministic checks and finalize Git guards (§8.2, D7). */
|
|
@@ -207,6 +224,7 @@ type RaiseEscalationDecision = Extract<Decision, { kind: 'raise-escalation' }>
|
|
|
207
224
|
* engine-selected stop acknowledgement that deliberately needs no workspace. */
|
|
208
225
|
export type AttachmentResult =
|
|
209
226
|
| { kind: 'workspace-ready' }
|
|
227
|
+
| { kind: 'compatibility-deferred' }
|
|
210
228
|
| { kind: 'control-acknowledged'; command: 'pause' | 'abort' }
|
|
211
229
|
|
|
212
230
|
/** §10 memory: a producer's live runner session, kept across review rounds. */
|
|
@@ -340,7 +358,7 @@ export interface PhaseFailureTally {
|
|
|
340
358
|
lastProviderAttempts?: readonly ProviderAttempt[]
|
|
341
359
|
}
|
|
342
360
|
|
|
343
|
-
export const PHASE_FAILURES_REDUCER_VERSION =
|
|
361
|
+
export const PHASE_FAILURES_REDUCER_VERSION = 2
|
|
344
362
|
|
|
345
363
|
/**
|
|
346
364
|
* phase.failed tally for one phase+round — D5's retry-guard input. A factory
|
|
@@ -372,6 +390,14 @@ export function phaseFailuresReducer(phase: Phase, round: number) {
|
|
|
372
390
|
fold(acc, events) {
|
|
373
391
|
for (const event of events) {
|
|
374
392
|
switch (event.type) {
|
|
393
|
+
case 'phase.deferral-escalated':
|
|
394
|
+
acc.raised[event.payload.id] = {
|
|
395
|
+
phase: event.payload.phase,
|
|
396
|
+
round: event.payload.round,
|
|
397
|
+
source: 'policy',
|
|
398
|
+
deferral: 'own-build-read',
|
|
399
|
+
}
|
|
400
|
+
break
|
|
375
401
|
case 'escalation.raised':
|
|
376
402
|
acc.raised[event.payload.id] = {
|
|
377
403
|
phase: event.payload.phase,
|
|
@@ -614,7 +640,173 @@ function isPublicationBoundary(event: AbEvent, spec: SessionSpec, session: strin
|
|
|
614
640
|
return false
|
|
615
641
|
}
|
|
616
642
|
|
|
643
|
+
function recoveryMatches(decision: Decision, session: OpenSession): boolean {
|
|
644
|
+
return decision.kind === 'run-phase'
|
|
645
|
+
? session.phase === decision.phase && session.round === decision.round
|
|
646
|
+
: decision.kind === 'run-agent-verify'
|
|
647
|
+
? session.phase === verifyPhase(decision.step) && session.round === decision.attempt
|
|
648
|
+
: decision.kind === 'run-finalize-step'
|
|
649
|
+
? session.phase === 'finalize' && session.role === decision.step
|
|
650
|
+
: false
|
|
651
|
+
}
|
|
652
|
+
|
|
617
653
|
export class BuildRunner {
|
|
654
|
+
private recovery: OpenSession | undefined
|
|
655
|
+
private recoveryBoundaryMs = 0
|
|
656
|
+
private recoveryCause: 'shutdown' | 'pause' | 'runner-loss' = 'runner-loss'
|
|
657
|
+
private readonly continuationIntents = new Map<
|
|
658
|
+
string,
|
|
659
|
+
NonNullable<EventPayload<'session.started'>['continuation']>
|
|
660
|
+
>()
|
|
661
|
+
private readonly interruptionPrompts = new Map<string, string>()
|
|
662
|
+
private readonly bracketStreams = new Map<string, SessionBracketStream>()
|
|
663
|
+
private readonly activeDurations = new Map<string, number>()
|
|
664
|
+
|
|
665
|
+
private resolveSessionRole(role: string, ...aliases: string[]): ResolvedRole {
|
|
666
|
+
const resolved = this.boundaryResolver.resolve(role, ...aliases)
|
|
667
|
+
const old = this.recovery
|
|
668
|
+
if (!old || old.role !== role) return resolved
|
|
669
|
+
const registration = this.deps.runtimes[old.runner]
|
|
670
|
+
if (!registration) return resolved
|
|
671
|
+
return {
|
|
672
|
+
...resolved,
|
|
673
|
+
...registration,
|
|
674
|
+
runner: registration.runner,
|
|
675
|
+
runtime: old.runner,
|
|
676
|
+
model: old.model,
|
|
677
|
+
args: old.args ?? [],
|
|
678
|
+
sessionBudgetSeconds: (old.budgetMs ?? resolved.sessionBudgetSeconds * 1000) / 1000,
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
private async startBracket(write: EventWrite<'session.started'>) {
|
|
683
|
+
const { store, slug } = this.deps
|
|
684
|
+
const old = this.recovery
|
|
685
|
+
const budgetMs = this.resolveSessionRole(write.payload.role).sessionBudgetSeconds * 1000
|
|
686
|
+
if (
|
|
687
|
+
old &&
|
|
688
|
+
old.phase === write.payload.phase &&
|
|
689
|
+
old.round === write.payload.round &&
|
|
690
|
+
old.role === write.payload.role &&
|
|
691
|
+
old.nativeHandle
|
|
692
|
+
) {
|
|
693
|
+
const activeMs =
|
|
694
|
+
(old.continuation?.activeMs ?? 0) +
|
|
695
|
+
(old.suspendedActiveMs ?? Math.max(0, this.recoveryBoundaryMs - Date.parse(old.startedAt)))
|
|
696
|
+
write = {
|
|
697
|
+
...write,
|
|
698
|
+
payload: {
|
|
699
|
+
...write.payload,
|
|
700
|
+
budgetMs,
|
|
701
|
+
continuation: {
|
|
702
|
+
predecessor: old.session,
|
|
703
|
+
root: old.continuation?.root ?? old.session,
|
|
704
|
+
handle: old.nativeHandle,
|
|
705
|
+
cause: this.recoveryCause,
|
|
706
|
+
activeMs,
|
|
707
|
+
budgetMs,
|
|
708
|
+
},
|
|
709
|
+
},
|
|
710
|
+
}
|
|
711
|
+
} else write = { ...write, payload: { ...write.payload, budgetMs } }
|
|
712
|
+
let started: EventEnvelope<'session.started'> | null = null
|
|
713
|
+
if (write.payload.continuation) {
|
|
714
|
+
for (;;) {
|
|
715
|
+
const facts = await readBuildFacts(store, slug)
|
|
716
|
+
const decision = decideNextFromFacts(
|
|
717
|
+
facts.value.state,
|
|
718
|
+
facts.value.log,
|
|
719
|
+
this.boundaryConfig,
|
|
720
|
+
)
|
|
721
|
+
const current = [
|
|
722
|
+
...facts.value.state.sessions.open,
|
|
723
|
+
...facts.value.state.sessions.suspended,
|
|
724
|
+
].find((session) => session.session === old!.session)
|
|
725
|
+
if (!current?.nativeHandle || !recoveryMatches(decision, current)) {
|
|
726
|
+
await this.bracketStreams.get(write.payload.session)?.sink.close('aborted')
|
|
727
|
+
throw new RecoveryBoundaryChanged()
|
|
728
|
+
}
|
|
729
|
+
write = {
|
|
730
|
+
...write,
|
|
731
|
+
payload: {
|
|
732
|
+
...write.payload,
|
|
733
|
+
continuation: {
|
|
734
|
+
...write.payload.continuation!,
|
|
735
|
+
handle: current.nativeHandle,
|
|
736
|
+
cause:
|
|
737
|
+
current.suspensionCause ??
|
|
738
|
+
(current.suspendedActiveMs !== undefined ? 'shutdown' : 'runner-loss'),
|
|
739
|
+
activeMs:
|
|
740
|
+
(current.continuation?.activeMs ?? 0) +
|
|
741
|
+
(current.suspendedActiveMs ??
|
|
742
|
+
Math.max(0, this.recoveryBoundaryMs - Date.parse(current.startedAt))),
|
|
743
|
+
},
|
|
744
|
+
},
|
|
745
|
+
}
|
|
746
|
+
started = await store.appendIfCurrent(slug, facts.cursor, write)
|
|
747
|
+
if (started) break
|
|
748
|
+
}
|
|
749
|
+
} else started = await store.append(slug, write)
|
|
750
|
+
if (write.payload.continuation)
|
|
751
|
+
this.continuationIntents.set(write.payload.session, write.payload.continuation)
|
|
752
|
+
this.recovery = undefined
|
|
753
|
+
return started
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
private async startAgent(runner: AgentRunner, opts: AgentStartOpts) {
|
|
757
|
+
if (opts.signal?.aborted) throw opts.signal.reason
|
|
758
|
+
const continuation = this.continuationIntents.get(opts.env.AB_SESSION!)
|
|
759
|
+
this.continuationIntents.delete(opts.env.AB_SESSION!)
|
|
760
|
+
if (!continuation) return runner.start(opts)
|
|
761
|
+
const message = `This session was interrupted (${continuation.cause}). Re-check the worktree and re-run anything that was in flight. Continue the current phase and round; run ab context for its durable inputs.`
|
|
762
|
+
this.interruptionPrompts.set(opts.env.AB_SESSION!, message)
|
|
763
|
+
if (opts.stream)
|
|
764
|
+
opts.stream.append([
|
|
765
|
+
{ type: 'text-start', id: 'interruption' },
|
|
766
|
+
{ type: 'text-delta', id: 'interruption', delta: message },
|
|
767
|
+
{ type: 'text-end', id: 'interruption' },
|
|
768
|
+
])
|
|
769
|
+
const result = await resumeAgent(runner, continuation.handle, message, opts)
|
|
770
|
+
if (result.kind === 'resumed') return { session: result.session, result: result.result }
|
|
771
|
+
const session = opts.env.AB_SESSION!
|
|
772
|
+
for (;;) {
|
|
773
|
+
const facts = await readBuildFacts(this.deps.store, this.deps.slug)
|
|
774
|
+
const stale =
|
|
775
|
+
opts.signal?.aborted === true ||
|
|
776
|
+
!facts.value.state.sessions.open.some((s) => s.session === session)
|
|
777
|
+
const event = await this.deps.store.appendIfCurrent(this.deps.slug, facts.cursor, {
|
|
778
|
+
actor: KERNEL,
|
|
779
|
+
type: 'session.resume-fallback',
|
|
780
|
+
payload: {
|
|
781
|
+
session,
|
|
782
|
+
reason: `${result.reason}: ${result.message}`,
|
|
783
|
+
...(stale ? { stale: true } : {}),
|
|
784
|
+
},
|
|
785
|
+
})
|
|
786
|
+
if (!event) continue
|
|
787
|
+
if (stale) throw new Error('Native resume attempt is no longer current')
|
|
788
|
+
break
|
|
789
|
+
}
|
|
790
|
+
// The unavailable native attempt has no fresh turn to account for. Close
|
|
791
|
+
// its evidence and credentials before re-deciding the ordinary phase path.
|
|
792
|
+
// That path resolves today's policy and opens an independent budget/root.
|
|
793
|
+
await this.bracketStreams.get(session)?.sink.close('aborted')
|
|
794
|
+
await this.deps.store.append(this.deps.slug, {
|
|
795
|
+
actor: KERNEL,
|
|
796
|
+
type: 'session.ended',
|
|
797
|
+
payload: {
|
|
798
|
+
session,
|
|
799
|
+
outcome: 'reclaimed',
|
|
800
|
+
reclaimedBy: {
|
|
801
|
+
instance: this.deps.instance,
|
|
802
|
+
resumedFromSeq: (await readBuildFacts(this.deps.store, this.deps.slug)).value.state
|
|
803
|
+
.lastSeq,
|
|
804
|
+
},
|
|
805
|
+
},
|
|
806
|
+
})
|
|
807
|
+
throw new FreshSessionRequired()
|
|
808
|
+
}
|
|
809
|
+
|
|
618
810
|
private readonly maxPhaseAttempts: number
|
|
619
811
|
private readonly heartbeatMs: number
|
|
620
812
|
private readonly leaseTtlMs: number
|
|
@@ -692,10 +884,47 @@ export class BuildRunner {
|
|
|
692
884
|
* announce takeover against that same boundary. If a write is interrupted,
|
|
693
885
|
* no attachment lands; the next runner reduces the newer prefix and closes
|
|
694
886
|
* only sessions that remain open. */
|
|
695
|
-
private async reclaimSessionsAndAttach(
|
|
887
|
+
private async reclaimSessionsAndAttach(
|
|
888
|
+
facts: Awaited<ReturnType<typeof readBuildFacts>>['value'],
|
|
889
|
+
): Promise<void> {
|
|
696
890
|
const { store, slug, instance, host } = this.deps
|
|
891
|
+
const state = facts.state
|
|
697
892
|
const resumedFromSeq = state.lastSeq
|
|
698
|
-
|
|
893
|
+
const decision = decideNextFromFacts(state, facts.log, this.boundaryConfig)
|
|
894
|
+
const candidates = [...state.sessions.suspended, ...state.sessions.open]
|
|
895
|
+
.filter((session) => recoveryMatches(decision, session))
|
|
896
|
+
.sort((a, b) => b.seq - a.seq)
|
|
897
|
+
const candidate = candidates[0]
|
|
898
|
+
if (candidate) {
|
|
899
|
+
const handle = candidate.nativeHandle
|
|
900
|
+
const reason =
|
|
901
|
+
this.deps.workspaceProvider !== 'git-worktree' || this.deps.workspaceRemote === true
|
|
902
|
+
? 'workspace-not-local-git-worktree'
|
|
903
|
+
: !handle
|
|
904
|
+
? 'missing-handle'
|
|
905
|
+
: handle.host !== hostname()
|
|
906
|
+
? 'wrong-host'
|
|
907
|
+
: handle.workspacePath !== this.deps.workspacePath
|
|
908
|
+
? 'wrong-workspace'
|
|
909
|
+
: !this.deps.runtimes[candidate.runner]?.runner.resume
|
|
910
|
+
? 'unsupported-runtime'
|
|
911
|
+
: undefined
|
|
912
|
+
if (reason)
|
|
913
|
+
await store.append(slug, {
|
|
914
|
+
actor: KERNEL,
|
|
915
|
+
type: 'session.resume-fallback',
|
|
916
|
+
payload: { session: candidate.session, reason },
|
|
917
|
+
})
|
|
918
|
+
else {
|
|
919
|
+
this.recoveryBoundaryMs = this.deps.clock().getTime()
|
|
920
|
+
this.recovery = candidate
|
|
921
|
+
this.recoveryCause = state.sessions.suspended.some((s) => s.session === candidate.session)
|
|
922
|
+
? (candidate.suspensionCause ?? 'shutdown')
|
|
923
|
+
: 'runner-loss'
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
for (const session of [...state.sessions.open, ...state.sessions.suspended]) {
|
|
927
|
+
if (session.session === this.recovery?.session) continue
|
|
699
928
|
await store.append(slug, {
|
|
700
929
|
actor: KERNEL,
|
|
701
930
|
type: 'session.ended',
|
|
@@ -725,6 +954,8 @@ export class BuildRunner {
|
|
|
725
954
|
* setup succeeds; that fact then proves recovery and clears the projection.
|
|
726
955
|
*/
|
|
727
956
|
async attach(): Promise<AttachmentResult> {
|
|
957
|
+
if (await deferMixedVersion(this.deps.store, this.deps.slug, KERNEL))
|
|
958
|
+
return { kind: 'compatibility-deferred' }
|
|
728
959
|
const attaching = (await readBuildFacts(this.deps.store, this.deps.slug)).value.state
|
|
729
960
|
if (
|
|
730
961
|
attaching.kind === 'node' &&
|
|
@@ -747,6 +978,11 @@ export class BuildRunner {
|
|
|
747
978
|
return { kind: 'control-acknowledged', command: decision.command }
|
|
748
979
|
}
|
|
749
980
|
|
|
981
|
+
if (decision.kind === 'acknowledge' && decision.command === 'resume') {
|
|
982
|
+
await this.acknowledge('resume')
|
|
983
|
+
facts = (await readBuildFacts(store, slug)).value
|
|
984
|
+
}
|
|
985
|
+
|
|
750
986
|
const state = facts.state
|
|
751
987
|
if (state.kind === 'node' && state.integration?.cycle && !state.nodeRestack?.conflict) {
|
|
752
988
|
// Local worktree provisioning may create a new branch at its parent;
|
|
@@ -776,7 +1012,7 @@ export class BuildRunner {
|
|
|
776
1012
|
}
|
|
777
1013
|
|
|
778
1014
|
const recovering = currentFailure !== undefined
|
|
779
|
-
if (!recovering) await this.reclaimSessionsAndAttach(facts
|
|
1015
|
+
if (!recovering) await this.reclaimSessionsAndAttach(facts)
|
|
780
1016
|
|
|
781
1017
|
// First beat immediately (liveness visible without waiting an interval),
|
|
782
1018
|
// then keep-alive. Setup runs under this heartbeat so a slow install cannot
|
|
@@ -839,7 +1075,7 @@ export class BuildRunner {
|
|
|
839
1075
|
if (recovering) {
|
|
840
1076
|
try {
|
|
841
1077
|
facts = (await readBuildFacts(store, slug)).value
|
|
842
|
-
await this.reclaimSessionsAndAttach(facts
|
|
1078
|
+
await this.reclaimSessionsAndAttach(facts)
|
|
843
1079
|
} catch (error) {
|
|
844
1080
|
this.stopHeartbeat()
|
|
845
1081
|
this.attached = false
|
|
@@ -855,65 +1091,102 @@ export class BuildRunner {
|
|
|
855
1091
|
* decision it executed; `wait` decisions execute nothing.
|
|
856
1092
|
*/
|
|
857
1093
|
async step(): Promise<Decision> {
|
|
1094
|
+
if (await deferMixedVersion(this.deps.store, this.deps.slug, KERNEL))
|
|
1095
|
+
return { kind: 'wait', reason: 'mixed-version' }
|
|
858
1096
|
const config = await this.captureConfig()
|
|
859
1097
|
const { store, slug } = this.deps
|
|
860
1098
|
await this.settleLocalReconcile()
|
|
861
1099
|
await completeAutomaticReconcile(store, slug)
|
|
862
1100
|
const facts = (await readBuildFacts(store, slug)).value
|
|
863
1101
|
const decision = decideNextFromFacts(facts.state, facts.log, config)
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
1102
|
+
if (await deferMixedVersion(store, slug, KERNEL))
|
|
1103
|
+
return { kind: 'wait', reason: 'mixed-version' }
|
|
1104
|
+
if (this.recovery) {
|
|
1105
|
+
const current = [...facts.state.sessions.open, ...facts.state.sessions.suspended].find(
|
|
1106
|
+
(session) => session.session === this.recovery!.session,
|
|
1107
|
+
)
|
|
1108
|
+
if (
|
|
1109
|
+
!current?.nativeHandle ||
|
|
1110
|
+
(decision.kind !== 'wait' &&
|
|
1111
|
+
decision.kind !== 'acknowledge' &&
|
|
1112
|
+
!recoveryMatches(decision, current))
|
|
1113
|
+
) {
|
|
1114
|
+
this.recovery = undefined
|
|
1115
|
+
if (current)
|
|
1116
|
+
await store.append(slug, {
|
|
1117
|
+
actor: KERNEL,
|
|
1118
|
+
type: 'session.ended',
|
|
1119
|
+
payload: {
|
|
1120
|
+
session: current.session,
|
|
1121
|
+
outcome: 'reclaimed',
|
|
1122
|
+
reclaimedBy: { instance: this.deps.instance, resumedFromSeq: facts.state.lastSeq },
|
|
1123
|
+
},
|
|
1124
|
+
})
|
|
1125
|
+
} else this.recovery = current
|
|
1126
|
+
}
|
|
1127
|
+
try {
|
|
1128
|
+
switch (decision.kind) {
|
|
1129
|
+
case 'restack':
|
|
1130
|
+
await this.restack(decision)
|
|
1131
|
+
return decision
|
|
1132
|
+
case 'capture-reconcile-policy':
|
|
1133
|
+
await store.append(slug, {
|
|
1134
|
+
actor: KERNEL,
|
|
1135
|
+
type: 'verify.reconcile-policy-captured',
|
|
1136
|
+
payload: {
|
|
1137
|
+
reconcileSeq: decision.reconcileSeq,
|
|
1138
|
+
steps: config.verify.steps.map((step) => {
|
|
1139
|
+
const override = config.verify.stepConfigs[step]?.afterReconcile
|
|
1140
|
+
return {
|
|
1141
|
+
step,
|
|
1142
|
+
enabled: override ?? config.verify.afterReconcile,
|
|
1143
|
+
source:
|
|
1144
|
+
override === undefined
|
|
1145
|
+
? 'verify.afterReconcile'
|
|
1146
|
+
: `verify.${step}.afterReconcile`,
|
|
1147
|
+
}
|
|
1148
|
+
}),
|
|
1149
|
+
},
|
|
1150
|
+
})
|
|
1151
|
+
return decision
|
|
1152
|
+
case 'wait':
|
|
1153
|
+
return decision
|
|
1154
|
+
case 'acknowledge':
|
|
1155
|
+
await this.acknowledge(decision.command)
|
|
1156
|
+
return decision
|
|
1157
|
+
case 'raise-read-deferral':
|
|
1158
|
+
await this.raiseReadDeferral(decision)
|
|
1159
|
+
return decision
|
|
1160
|
+
case 'raise-escalation':
|
|
1161
|
+
await this.raiseEscalation(decision)
|
|
1162
|
+
return decision
|
|
1163
|
+
case 'run-phase':
|
|
1164
|
+
await this.runPhase(decision)
|
|
1165
|
+
return decision
|
|
1166
|
+
case 'run-check':
|
|
1167
|
+
await this.runCheck(decision)
|
|
1168
|
+
return decision
|
|
1169
|
+
case 'run-agent-verify':
|
|
1170
|
+
await this.runAgentVerify(decision)
|
|
1171
|
+
return decision
|
|
1172
|
+
case 'evaluate-verify':
|
|
1173
|
+
await this.evaluateVerify(decision)
|
|
1174
|
+
return decision
|
|
1175
|
+
case 'skip-verify':
|
|
1176
|
+
await this.skipVerify(decision)
|
|
1177
|
+
return decision
|
|
1178
|
+
case 'run-finalize-step':
|
|
1179
|
+
await this.runFinalizeStep(decision)
|
|
1180
|
+
return decision
|
|
1181
|
+
case 'check-reconcile-progress':
|
|
1182
|
+
await this.checkReconcileProgress(decision)
|
|
1183
|
+
return decision
|
|
1184
|
+
}
|
|
1185
|
+
} catch (error) {
|
|
1186
|
+
if (error instanceof MixedVersionDeferral) return { kind: 'wait', reason: 'mixed-version' }
|
|
1187
|
+
if (error instanceof FreshSessionRequired) return this.step()
|
|
1188
|
+
if (error instanceof RecoveryBoundaryChanged) return decision
|
|
1189
|
+
throw error
|
|
917
1190
|
}
|
|
918
1191
|
}
|
|
919
1192
|
|
|
@@ -925,12 +1198,23 @@ export class BuildRunner {
|
|
|
925
1198
|
* already deposited — see module doc).
|
|
926
1199
|
*/
|
|
927
1200
|
async run(): Promise<BuildState> {
|
|
928
|
-
|
|
929
|
-
|
|
1201
|
+
if (await deferMixedVersion(this.deps.store, this.deps.slug, KERNEL))
|
|
1202
|
+
return (await readBuildFacts(this.deps.store, this.deps.slug)).value.state
|
|
1203
|
+
const initialFacts = (await readBuildFacts(this.deps.store, this.deps.slug)).value
|
|
1204
|
+
const initial = initialFacts.state
|
|
1205
|
+
if (
|
|
1206
|
+
initial.kind === 'node' &&
|
|
1207
|
+
!initial.nodeRestack?.conflict &&
|
|
1208
|
+
!integrationCurrent(initial) &&
|
|
1209
|
+
initialFacts.log.readDeferrals.length === 0
|
|
1210
|
+
)
|
|
930
1211
|
return initial
|
|
931
1212
|
if (!this.attached) {
|
|
932
1213
|
const attachment = await this.attach()
|
|
933
|
-
if (
|
|
1214
|
+
if (
|
|
1215
|
+
attachment.kind === 'control-acknowledged' ||
|
|
1216
|
+
attachment.kind === 'compatibility-deferred'
|
|
1217
|
+
) {
|
|
934
1218
|
return (await readBuildFacts(this.deps.store, this.deps.slug)).value.state
|
|
935
1219
|
}
|
|
936
1220
|
}
|
|
@@ -939,6 +1223,8 @@ export class BuildRunner {
|
|
|
939
1223
|
if (await this.refreshSummary())
|
|
940
1224
|
return (await readBuildFacts(this.deps.store, this.deps.slug)).value.state
|
|
941
1225
|
for (;;) {
|
|
1226
|
+
if (this.deps.shutdownSignal?.aborted) break
|
|
1227
|
+
if (await deferMixedVersion(this.deps.store, this.deps.slug, KERNEL)) break
|
|
942
1228
|
await this.ensureLease()
|
|
943
1229
|
await this.settleLocalReconcile()
|
|
944
1230
|
const facts = (await readBuildFacts(this.deps.store, this.deps.slug)).value
|
|
@@ -957,18 +1243,46 @@ export class BuildRunner {
|
|
|
957
1243
|
)
|
|
958
1244
|
break
|
|
959
1245
|
}
|
|
960
|
-
|
|
1246
|
+
let decision: Decision
|
|
1247
|
+
try {
|
|
1248
|
+
decision = await this.step()
|
|
1249
|
+
} catch (error) {
|
|
1250
|
+
if (!(error instanceof SessionPaused)) throw error
|
|
1251
|
+
if (await this.settlePauseInterruption()) continue
|
|
1252
|
+
break
|
|
1253
|
+
}
|
|
961
1254
|
if (await this.refreshSummary()) break
|
|
962
1255
|
if (decision.kind === 'wait') break
|
|
963
1256
|
}
|
|
964
1257
|
return (await readBuildFacts(this.deps.store, this.deps.slug)).value.state
|
|
1258
|
+
} catch (error) {
|
|
1259
|
+
if (!(error instanceof SessionShutdown) && !(error instanceof MixedVersionDeferral))
|
|
1260
|
+
throw error
|
|
1261
|
+
return (await readBuildFacts(this.deps.store, this.deps.slug)).value.state
|
|
965
1262
|
} finally {
|
|
966
1263
|
this.stopHeartbeat()
|
|
967
1264
|
await this.closeProducerSessions()
|
|
968
1265
|
}
|
|
969
1266
|
}
|
|
970
1267
|
|
|
1268
|
+
/** Cancellation is irreversible, but the latest durable control selects
|
|
1269
|
+
* whether to park or recover the suspended bracket in this same runner. */
|
|
1270
|
+
private async settlePauseInterruption(): Promise<boolean> {
|
|
1271
|
+
for (;;) {
|
|
1272
|
+
const facts = await readBuildFacts(this.deps.store, this.deps.slug)
|
|
1273
|
+
const decision = decideNextFromFacts(facts.value.state, facts.value.log, this.boundaryConfig)
|
|
1274
|
+
if (decision.kind === 'acknowledge') {
|
|
1275
|
+
await this.acknowledge(decision.command)
|
|
1276
|
+
continue
|
|
1277
|
+
}
|
|
1278
|
+
if (decision.kind === 'wait') return false
|
|
1279
|
+
await this.reclaimSessionsAndAttach(facts.value)
|
|
1280
|
+
return true
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
|
|
971
1284
|
private async refreshSummary(): Promise<boolean> {
|
|
1285
|
+
if (await deferMixedVersion(this.deps.store, this.deps.slug, KERNEL)) return false
|
|
972
1286
|
try {
|
|
973
1287
|
await this.ensureLease()
|
|
974
1288
|
} catch {
|
|
@@ -996,6 +1310,7 @@ export class BuildRunner {
|
|
|
996
1310
|
private async ensureLease(): Promise<void> {
|
|
997
1311
|
if (!this.leaseLost) return
|
|
998
1312
|
const { store, slug, instance } = this.deps
|
|
1313
|
+
if (await deferMixedVersion(store, slug, KERNEL)) throw new MixedVersionDeferral()
|
|
999
1314
|
const reclaimed = await store.claimLease(slug, instance, this.leaseTtlMs)
|
|
1000
1315
|
if (!reclaimed) throw new LeaseHeldError(slug, instance)
|
|
1001
1316
|
this.leaseLost = false
|
|
@@ -1007,7 +1322,23 @@ export class BuildRunner {
|
|
|
1007
1322
|
private async acknowledge(command: 'pause' | 'resume' | 'abort'): Promise<void> {
|
|
1008
1323
|
const { store, slug } = this.deps
|
|
1009
1324
|
if (command === 'pause') {
|
|
1010
|
-
|
|
1325
|
+
for (;;) {
|
|
1326
|
+
const facts = await readBuildFacts(store, slug)
|
|
1327
|
+
const decision = decideNextFromFacts(
|
|
1328
|
+
facts.value.state,
|
|
1329
|
+
facts.value.log,
|
|
1330
|
+
this.boundaryConfig,
|
|
1331
|
+
)
|
|
1332
|
+
if (decision.kind !== 'acknowledge' || decision.command !== 'pause') return
|
|
1333
|
+
if (
|
|
1334
|
+
await store.appendIfCurrent(slug, facts.cursor, {
|
|
1335
|
+
actor: KERNEL,
|
|
1336
|
+
type: 'build.paused',
|
|
1337
|
+
payload: {},
|
|
1338
|
+
})
|
|
1339
|
+
)
|
|
1340
|
+
return
|
|
1341
|
+
}
|
|
1011
1342
|
} else if (command === 'resume') {
|
|
1012
1343
|
await store.append(slug, { actor: KERNEL, type: 'build.resumed', payload: {} })
|
|
1013
1344
|
} else {
|
|
@@ -1111,7 +1442,7 @@ export class BuildRunner {
|
|
|
1111
1442
|
// after the retry guard but before reconcile.started/the agent session.
|
|
1112
1443
|
// A crash re-enters this path at the same attempt and refreshes again.
|
|
1113
1444
|
let reconcileBaseSha: string | undefined
|
|
1114
|
-
if (phase === 'reconcile') {
|
|
1445
|
+
if (phase === 'reconcile' && !this.recovery) {
|
|
1115
1446
|
try {
|
|
1116
1447
|
requireRewrite(this.deps.forge)
|
|
1117
1448
|
const facts = (await readBuildFacts(store, slug)).value
|
|
@@ -1131,7 +1462,7 @@ export class BuildRunner {
|
|
|
1131
1462
|
}
|
|
1132
1463
|
}
|
|
1133
1464
|
|
|
1134
|
-
if (phase === 'reconcile') {
|
|
1465
|
+
if (phase === 'reconcile' && !this.recovery) {
|
|
1135
1466
|
const facts = (await readBuildFacts(store, slug)).value
|
|
1136
1467
|
const active = facts.log.restack.active
|
|
1137
1468
|
if (active !== undefined && facts.reconcileTransaction?.restackId !== active.id) {
|
|
@@ -1160,11 +1491,12 @@ export class BuildRunner {
|
|
|
1160
1491
|
}
|
|
1161
1492
|
}
|
|
1162
1493
|
if (
|
|
1163
|
-
|
|
1164
|
-
(
|
|
1494
|
+
!this.recovery &&
|
|
1495
|
+
(phase !== 'reconcile' ||
|
|
1496
|
+
(await readBuildFacts(store, slug)).value.reconcileTransaction?.attempt !== round)
|
|
1165
1497
|
)
|
|
1166
1498
|
await store.append(slug, this.startedWrite(decision, reconcileBaseSha))
|
|
1167
|
-
if (phase === 'reconcile') {
|
|
1499
|
+
if (phase === 'reconcile' && !this.recovery) {
|
|
1168
1500
|
try {
|
|
1169
1501
|
const oldBase = await readBuildSelector(store, slug, 'verifyDiffBase')
|
|
1170
1502
|
if (oldBase === undefined || reconcileBaseSha === undefined)
|
|
@@ -1213,77 +1545,79 @@ export class BuildRunner {
|
|
|
1213
1545
|
const active = node.active!
|
|
1214
1546
|
const conflict = node.conflict!
|
|
1215
1547
|
const change = active.changes[conflict.index]!
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
const
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1548
|
+
if (!this.recovery) {
|
|
1549
|
+
try {
|
|
1550
|
+
for (const sha of [active.expected, active.parent]) {
|
|
1551
|
+
const exists = await this.deps.exec(['git', 'cat-file', '-e', `${sha}^{commit}`], {
|
|
1552
|
+
cwd: this.deps.workspacePath,
|
|
1553
|
+
})
|
|
1554
|
+
if (exists.exitCode !== 0) {
|
|
1555
|
+
const fetched = await this.deps.exec(
|
|
1556
|
+
['git', 'fetch', '--no-tags', 'origin', `refs/autobuild/archive/${slug}/${sha}`],
|
|
1557
|
+
{ cwd: this.deps.workspacePath },
|
|
1558
|
+
)
|
|
1559
|
+
if (fetched.exitCode !== 0) throw new Error(`missing archived node input ${sha}`)
|
|
1560
|
+
}
|
|
1227
1561
|
}
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1562
|
+
if (change.build) {
|
|
1563
|
+
for (const kind of ['spec', 'plan', 'implement-notes']) {
|
|
1564
|
+
if (
|
|
1565
|
+
!(await store.getArtifact(slug, `node-source-${active.id}-${conflict.index}-${kind}`))
|
|
1566
|
+
)
|
|
1567
|
+
throw new Error(`missing ${kind} for source build ${change.build}`)
|
|
1568
|
+
}
|
|
1569
|
+
} else {
|
|
1570
|
+
const result = await this.deps.exec(
|
|
1571
|
+
[
|
|
1572
|
+
'git',
|
|
1573
|
+
'show',
|
|
1574
|
+
'--format=fuller',
|
|
1575
|
+
...(change.mainline ? ['-m', '--first-parent'] : []),
|
|
1576
|
+
change.sha,
|
|
1577
|
+
],
|
|
1578
|
+
{
|
|
1579
|
+
cwd: this.deps.workspacePath,
|
|
1580
|
+
},
|
|
1233
1581
|
)
|
|
1234
|
-
|
|
1582
|
+
if (result.exitCode !== 0) throw new Error(`missing foreign commit ${change.sha}`)
|
|
1583
|
+
await store.putArtifact(slug, {
|
|
1584
|
+
kind: `node-source-${active.id}-${conflict.index}-foreign`,
|
|
1585
|
+
content: result.stdout,
|
|
1586
|
+
})
|
|
1235
1587
|
}
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
},
|
|
1588
|
+
for (const item of node.items) if (!item.dropped) await restoreReplay(this.deps, item.head)
|
|
1589
|
+
await this.deps.exec(['git', 'cherry-pick', '--abort'], { cwd: this.deps.workspacePath })
|
|
1590
|
+
const reset = await this.deps.exec(['git', 'checkout', '--detach', conflict.base], {
|
|
1591
|
+
cwd: this.deps.workspacePath,
|
|
1592
|
+
})
|
|
1593
|
+
if (reset.exitCode !== 0) throw new Error(reset.stderr)
|
|
1594
|
+
const result = await replayNodeChange(
|
|
1595
|
+
this.deps,
|
|
1596
|
+
{ ...active, oldHead: active.expected },
|
|
1597
|
+
conflict.index,
|
|
1598
|
+
conflict.base,
|
|
1248
1599
|
)
|
|
1249
|
-
if (result.
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1600
|
+
if (result.kind !== 'conflict')
|
|
1601
|
+
throw new Error('reserved conflict no longer reproduces from frozen inputs')
|
|
1602
|
+
} catch (error) {
|
|
1603
|
+
await store.append(slug, {
|
|
1604
|
+
actor: KERNEL,
|
|
1605
|
+
type: 'escalation.raised',
|
|
1606
|
+
payload: {
|
|
1607
|
+
id: this.deps.ids('esc'),
|
|
1608
|
+
phase: 'reconcile',
|
|
1609
|
+
source: 'agent',
|
|
1610
|
+
question: `Cannot prepare node change ${change.sha}: ${errorMessage(error)}`,
|
|
1611
|
+
},
|
|
1253
1612
|
})
|
|
1613
|
+
return
|
|
1254
1614
|
}
|
|
1255
|
-
for (const item of node.items) if (!item.dropped) await restoreReplay(this.deps, item.head)
|
|
1256
|
-
await this.deps.exec(['git', 'cherry-pick', '--abort'], { cwd: this.deps.workspacePath })
|
|
1257
|
-
const reset = await this.deps.exec(['git', 'checkout', '--detach', conflict.base], {
|
|
1258
|
-
cwd: this.deps.workspacePath,
|
|
1259
|
-
})
|
|
1260
|
-
if (reset.exitCode !== 0) throw new Error(reset.stderr)
|
|
1261
|
-
const result = await replayNodeChange(
|
|
1262
|
-
this.deps,
|
|
1263
|
-
{ ...active, oldHead: active.expected },
|
|
1264
|
-
conflict.index,
|
|
1265
|
-
conflict.base,
|
|
1266
|
-
)
|
|
1267
|
-
if (result.kind !== 'conflict')
|
|
1268
|
-
throw new Error('reserved conflict no longer reproduces from frozen inputs')
|
|
1269
|
-
} catch (error) {
|
|
1270
1615
|
await store.append(slug, {
|
|
1271
1616
|
actor: KERNEL,
|
|
1272
|
-
type: '
|
|
1273
|
-
payload: {
|
|
1274
|
-
id: this.deps.ids('esc'),
|
|
1275
|
-
phase: 'reconcile',
|
|
1276
|
-
source: 'agent',
|
|
1277
|
-
question: `Cannot prepare node change ${change.sha}: ${errorMessage(error)}`,
|
|
1278
|
-
},
|
|
1617
|
+
type: 'reconcile.started',
|
|
1618
|
+
payload: { attempt: round, baseSha: conflict.base },
|
|
1279
1619
|
})
|
|
1280
|
-
return
|
|
1281
1620
|
}
|
|
1282
|
-
await store.append(slug, {
|
|
1283
|
-
actor: KERNEL,
|
|
1284
|
-
type: 'reconcile.started',
|
|
1285
|
-
payload: { attempt: round, baseSha: conflict.base },
|
|
1286
|
-
})
|
|
1287
1621
|
const failures = await readPhaseFailures(store, slug, 'reconcile', round)
|
|
1288
1622
|
if (failures.count >= this.maxPhaseAttempts) {
|
|
1289
1623
|
await this.raisePolicyExhausted('reconcile', round, failures)
|
|
@@ -1304,6 +1638,7 @@ export class BuildRunner {
|
|
|
1304
1638
|
/** Re-read durable ownership and controls at the actual rewrite boundary. */
|
|
1305
1639
|
private async assertAutomaticRewriteOwnership(allowPublication = false) {
|
|
1306
1640
|
const { store, slug, instance, clock } = this.deps
|
|
1641
|
+
if (await deferMixedVersion(store, slug, KERNEL)) throw new MixedVersionDeferral()
|
|
1307
1642
|
const record = await store.getBuild(slug)
|
|
1308
1643
|
if (record?.lease?.holder !== instance) throw new LeaseHeldError(slug, instance)
|
|
1309
1644
|
if (
|
|
@@ -1333,6 +1668,7 @@ export class BuildRunner {
|
|
|
1333
1668
|
|
|
1334
1669
|
/** A resolver deposits intent while its turn is live. Publish after it ends. */
|
|
1335
1670
|
private async settleLocalReconcile() {
|
|
1671
|
+
if (await deferMixedVersion(this.deps.store, this.deps.slug, KERNEL)) return
|
|
1336
1672
|
const { store, slug } = this.deps
|
|
1337
1673
|
const facts = (await readBuildFacts(store, slug)).value
|
|
1338
1674
|
if (facts.workspace !== null && isRemoteWorkspace(facts.workspace)) return
|
|
@@ -1545,7 +1881,7 @@ export class BuildRunner {
|
|
|
1545
1881
|
}
|
|
1546
1882
|
await store.append(slug, { actor: KERNEL, type: 'restack.completed', payload: intent })
|
|
1547
1883
|
} catch (error) {
|
|
1548
|
-
if (error instanceof LeaseHeldError) throw error
|
|
1884
|
+
if (error instanceof LeaseHeldError || error instanceof MixedVersionDeferral) throw error
|
|
1549
1885
|
if (error instanceof AutomaticRewriteDeferred) return
|
|
1550
1886
|
await this.failPhase(
|
|
1551
1887
|
'reconcile',
|
|
@@ -1705,11 +2041,12 @@ export class BuildRunner {
|
|
|
1705
2041
|
const { store, slug, exec, workspacePath } = this.deps
|
|
1706
2042
|
const { step, command, attempt } = decision
|
|
1707
2043
|
const evidence = await this.verifyEvidence()
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
2044
|
+
if (!this.recovery)
|
|
2045
|
+
await store.append(slug, {
|
|
2046
|
+
actor: KERNEL,
|
|
2047
|
+
type: 'verify.started',
|
|
2048
|
+
payload: { ...evidence, step, attempt },
|
|
2049
|
+
} satisfies EventWrite<'verify.started'>)
|
|
1713
2050
|
|
|
1714
2051
|
const result = await exec(['sh', '-c', command], { cwd: workspacePath })
|
|
1715
2052
|
if (result.exitCode === 0) {
|
|
@@ -1802,18 +2139,20 @@ export class BuildRunner {
|
|
|
1802
2139
|
* observation and never route a green build backward.
|
|
1803
2140
|
*/
|
|
1804
2141
|
private async runFinalizeStep(decision: RunFinalizeStepDecision): Promise<void> {
|
|
2142
|
+
if (await deferMixedVersion(this.deps.store, this.deps.slug, KERNEL)) return
|
|
1805
2143
|
const { step, action } = decision
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
2144
|
+
if (!this.recovery)
|
|
2145
|
+
await this.deps.store.append(this.deps.slug, {
|
|
2146
|
+
actor: KERNEL,
|
|
2147
|
+
type: 'finalize.step-started',
|
|
2148
|
+
payload: { step },
|
|
2149
|
+
})
|
|
1811
2150
|
let actor: Actor = KERNEL
|
|
1812
2151
|
let durableHead: string | undefined
|
|
1813
2152
|
let failureNote: string | undefined
|
|
1814
2153
|
|
|
1815
2154
|
try {
|
|
1816
|
-
await this.requireFinalizeWorktreeClean('before the post-step starts')
|
|
2155
|
+
if (!this.recovery) await this.requireFinalizeWorktreeClean('before the post-step starts')
|
|
1817
2156
|
durableHead = await readBuildSelector(this.deps.store, this.deps.slug, 'publishedBranchHead')
|
|
1818
2157
|
if (durableHead === undefined)
|
|
1819
2158
|
throw new Error(
|
|
@@ -1865,10 +2204,11 @@ export class BuildRunner {
|
|
|
1865
2204
|
return
|
|
1866
2205
|
}
|
|
1867
2206
|
await suspendNativeLanding(this.deps, `finalize-step:${step}`)
|
|
1868
|
-
await this.deps.forge
|
|
2207
|
+
await publishExact(this.deps.forge, this.deps.workspacePath, this.deps.branch, head)
|
|
1869
2208
|
pushedHead = head
|
|
1870
2209
|
}
|
|
1871
2210
|
} catch (error) {
|
|
2211
|
+
if (error instanceof MixedVersionDeferral) throw error
|
|
1872
2212
|
failureNote = `finalize publication failed: ${errorMessage(error)}`
|
|
1873
2213
|
}
|
|
1874
2214
|
}
|
|
@@ -1895,10 +2235,10 @@ export class BuildRunner {
|
|
|
1895
2235
|
step: string,
|
|
1896
2236
|
skill: string,
|
|
1897
2237
|
): Promise<{ actor: Actor; failureNote: string | undefined; cancelled: boolean }> {
|
|
1898
|
-
const resolved = this.
|
|
2238
|
+
const resolved = this.resolveSessionRole(step)
|
|
1899
2239
|
if (resolved.alternates.length === 0) return this.runFinalizeAgentSingle(step, skill)
|
|
1900
2240
|
|
|
1901
|
-
const {
|
|
2241
|
+
const { slug, ids, workspacePath } = this.deps
|
|
1902
2242
|
const targets: readonly ResolvedRuntime[] = [resolved, ...resolved.alternates]
|
|
1903
2243
|
let substitution: ProviderAttempt | undefined
|
|
1904
2244
|
for (let index = 0; index < targets.length; index += 1) {
|
|
@@ -1919,7 +2259,7 @@ export class BuildRunner {
|
|
|
1919
2259
|
round: 1,
|
|
1920
2260
|
})
|
|
1921
2261
|
const stream = streamBracket?.sink
|
|
1922
|
-
const started = await
|
|
2262
|
+
const started = await this.startBracket({
|
|
1923
2263
|
actor: KERNEL,
|
|
1924
2264
|
type: 'session.started',
|
|
1925
2265
|
payload: {
|
|
@@ -1940,10 +2280,11 @@ export class BuildRunner {
|
|
|
1940
2280
|
let result: AgentTurnResult | undefined
|
|
1941
2281
|
let turnError: unknown
|
|
1942
2282
|
const turn = await this.runBudgetedTurn(
|
|
1943
|
-
started
|
|
2283
|
+
started,
|
|
1944
2284
|
resolved.sessionBudgetSeconds,
|
|
1945
|
-
async (signal) =>
|
|
1946
|
-
target.runner
|
|
2285
|
+
async (signal, onNativeSession) =>
|
|
2286
|
+
this.startAgent(target.runner, {
|
|
2287
|
+
onNativeSession,
|
|
1947
2288
|
skill,
|
|
1948
2289
|
invocation: slug,
|
|
1949
2290
|
buildSlug: slug,
|
|
@@ -1979,7 +2320,7 @@ export class BuildRunner {
|
|
|
1979
2320
|
return {
|
|
1980
2321
|
actor: agentActor(step, session),
|
|
1981
2322
|
failureNote: sessionBudgetError(resolved.sessionBudgetSeconds),
|
|
1982
|
-
cancelled:
|
|
2323
|
+
cancelled: await this.settleReadDeferral(session),
|
|
1983
2324
|
}
|
|
1984
2325
|
}
|
|
1985
2326
|
if (handle !== undefined) {
|
|
@@ -1999,6 +2340,10 @@ export class BuildRunner {
|
|
|
1999
2340
|
// The failure-tolerant post-step still records its outcome below.
|
|
2000
2341
|
}
|
|
2001
2342
|
}
|
|
2343
|
+
if (await this.settleReadDeferral(session)) {
|
|
2344
|
+
this.closeBracketStream(streamBracket, 'aborted')
|
|
2345
|
+
return { actor: agentActor(step, session), failureNote: undefined, cancelled: true }
|
|
2346
|
+
}
|
|
2002
2347
|
if (turn.abortCause === 'operator') {
|
|
2003
2348
|
this.closeBracketStream(streamBracket, 'aborted')
|
|
2004
2349
|
return { actor: agentActor(step, session), failureNote: undefined, cancelled: true }
|
|
@@ -2045,9 +2390,9 @@ export class BuildRunner {
|
|
|
2045
2390
|
step: string,
|
|
2046
2391
|
skill: string,
|
|
2047
2392
|
): Promise<{ actor: Actor; failureNote: string | undefined; cancelled: boolean }> {
|
|
2048
|
-
const {
|
|
2393
|
+
const { slug, ids, workspacePath } = this.deps
|
|
2049
2394
|
const session = ids('s')
|
|
2050
|
-
const resolved = this.
|
|
2395
|
+
const resolved = this.resolveSessionRole(step)
|
|
2051
2396
|
const { runner, runtime: runnerName, model, args, sessionBudgetSeconds } = resolved
|
|
2052
2397
|
const finalizeBracket = {
|
|
2053
2398
|
phase: 'finalize' as const,
|
|
@@ -2065,7 +2410,7 @@ export class BuildRunner {
|
|
|
2065
2410
|
})
|
|
2066
2411
|
const stream = streamBracket?.sink
|
|
2067
2412
|
|
|
2068
|
-
const started = await
|
|
2413
|
+
const started = await this.startBracket({
|
|
2069
2414
|
actor: KERNEL,
|
|
2070
2415
|
type: 'session.started',
|
|
2071
2416
|
payload: {
|
|
@@ -2082,18 +2427,22 @@ export class BuildRunner {
|
|
|
2082
2427
|
|
|
2083
2428
|
let handle: AgentSessionHandle | undefined
|
|
2084
2429
|
let failureNote: string | undefined
|
|
2085
|
-
const turn = await this.runBudgetedTurn(
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2430
|
+
const turn = await this.runBudgetedTurn(
|
|
2431
|
+
started,
|
|
2432
|
+
sessionBudgetSeconds,
|
|
2433
|
+
async (signal, onNativeSession) =>
|
|
2434
|
+
this.startAgent(runner, {
|
|
2435
|
+
onNativeSession,
|
|
2436
|
+
skill,
|
|
2437
|
+
invocation: slug,
|
|
2438
|
+
buildSlug: slug,
|
|
2439
|
+
workspacePath,
|
|
2440
|
+
...(model !== undefined ? { model } : {}),
|
|
2441
|
+
...(args !== undefined ? { args } : {}),
|
|
2442
|
+
env: await this.sessionEnvFor('finalize@1', session),
|
|
2443
|
+
signal,
|
|
2444
|
+
...(stream !== undefined ? { stream } : {}),
|
|
2445
|
+
}),
|
|
2097
2446
|
)
|
|
2098
2447
|
if (turn.settlement.kind === 'value') {
|
|
2099
2448
|
handle = turn.settlement.value.session
|
|
@@ -2114,7 +2463,7 @@ export class BuildRunner {
|
|
|
2114
2463
|
return {
|
|
2115
2464
|
actor: agentActor(step, session),
|
|
2116
2465
|
failureNote: sessionBudgetError(sessionBudgetSeconds),
|
|
2117
|
-
cancelled:
|
|
2466
|
+
cancelled: await this.settleReadDeferral(session),
|
|
2118
2467
|
}
|
|
2119
2468
|
}
|
|
2120
2469
|
|
|
@@ -2143,10 +2492,11 @@ export class BuildRunner {
|
|
|
2143
2492
|
failureNote === undefined && turn.abortCause !== 'operator' ? 'completed' : 'aborted',
|
|
2144
2493
|
)
|
|
2145
2494
|
|
|
2495
|
+
const deferred = await this.settleReadDeferral(session)
|
|
2146
2496
|
return {
|
|
2147
2497
|
actor: agentActor(step, session),
|
|
2148
2498
|
failureNote,
|
|
2149
|
-
cancelled: turn.abortCause === 'operator',
|
|
2499
|
+
cancelled: deferred || turn.abortCause === 'operator',
|
|
2150
2500
|
}
|
|
2151
2501
|
}
|
|
2152
2502
|
|
|
@@ -2286,29 +2636,130 @@ export class BuildRunner {
|
|
|
2286
2636
|
* classification, but does not disarm this backstop for an adapter that
|
|
2287
2637
|
* ignores cancellation. */
|
|
2288
2638
|
private async runBudgetedTurn<T>(
|
|
2289
|
-
|
|
2639
|
+
started: AbEvent,
|
|
2290
2640
|
budgetSeconds: number,
|
|
2291
|
-
run: (signal: AbortSignal) => Promise<T>,
|
|
2641
|
+
run: (signal: AbortSignal, onNativeSession: NativeSessionObserver) => Promise<T>,
|
|
2292
2642
|
): Promise<BudgetedTurn<T>> {
|
|
2293
2643
|
const { store, slug } = this.deps
|
|
2294
2644
|
const controller = new AbortController()
|
|
2645
|
+
const preSeq = started.seq
|
|
2646
|
+
if (started.type !== 'session.started')
|
|
2647
|
+
throw new Error('Native observation requires an open session bracket')
|
|
2648
|
+
if (this.deps.shutdownSignal?.aborted) throw new SessionShutdown()
|
|
2649
|
+
const turnStarted = performance.now()
|
|
2650
|
+
const continuation = started.payload.continuation
|
|
2651
|
+
const remainingMs = Math.max(
|
|
2652
|
+
0,
|
|
2653
|
+
(started.payload.budgetMs ?? budgetSeconds * 1000) - (continuation?.activeMs ?? 0),
|
|
2654
|
+
)
|
|
2655
|
+
let closed = false
|
|
2656
|
+
let recorded: string | undefined
|
|
2657
|
+
let recording = Promise.resolve()
|
|
2658
|
+
const onNativeSession: NativeSessionObserver = (handle) => {
|
|
2659
|
+
if ((closed || controller.signal.aborted) && !continuation) return Promise.resolve()
|
|
2660
|
+
recording = recording.then(async () => {
|
|
2661
|
+
if ((closed || controller.signal.aborted) && !continuation) return
|
|
2662
|
+
const validated = nativeSessionHandleSchema.parse(handle)
|
|
2663
|
+
if (validated.host !== hostname() || validated.workspacePath !== this.deps.workspacePath)
|
|
2664
|
+
throw new Error('Native identity execution location mismatch')
|
|
2665
|
+
const bytes = JSON.stringify(validated)
|
|
2666
|
+
if (recorded === bytes) return
|
|
2667
|
+
if (recorded !== undefined)
|
|
2668
|
+
throw new Error('Conflicting native identities in session bracket')
|
|
2669
|
+
if (continuation) {
|
|
2670
|
+
for (;;) {
|
|
2671
|
+
const facts = await readBuildFacts(store, slug)
|
|
2672
|
+
const current = facts.value.state.sessions.open.some(
|
|
2673
|
+
(s) => s.session === started.payload.session,
|
|
2674
|
+
)
|
|
2675
|
+
const accepted = await store.appendIfCurrent(slug, facts.cursor, {
|
|
2676
|
+
actor: KERNEL,
|
|
2677
|
+
type: 'session.resumed',
|
|
2678
|
+
payload: {
|
|
2679
|
+
session: started.payload.session,
|
|
2680
|
+
predecessor: continuation.predecessor,
|
|
2681
|
+
root: continuation.root,
|
|
2682
|
+
cause: continuation.cause,
|
|
2683
|
+
handle: validated,
|
|
2684
|
+
stale: !current,
|
|
2685
|
+
},
|
|
2686
|
+
})
|
|
2687
|
+
if (accepted) break
|
|
2688
|
+
}
|
|
2689
|
+
}
|
|
2690
|
+
await store.append(slug, {
|
|
2691
|
+
actor: KERNEL,
|
|
2692
|
+
type: 'session.native-handle-recorded',
|
|
2693
|
+
payload: {
|
|
2694
|
+
session: started.payload.session,
|
|
2695
|
+
phase: started.payload.phase,
|
|
2696
|
+
round: started.payload.round,
|
|
2697
|
+
handle: validated,
|
|
2698
|
+
},
|
|
2699
|
+
} satisfies EventWrite<'session.native-handle-recorded'>)
|
|
2700
|
+
recorded = bytes
|
|
2701
|
+
})
|
|
2702
|
+
|
|
2703
|
+
// Observe the rejection even if a nonconforming adapter discards its callback promise.
|
|
2704
|
+
void recording.catch(() => {})
|
|
2705
|
+
return recording
|
|
2706
|
+
}
|
|
2707
|
+
let pauseInterrupted = false
|
|
2295
2708
|
let abortCause: 'operator' | 'budget' | undefined
|
|
2296
2709
|
let resolveExpiry!: (settlement: { kind: 'expired' }) => void
|
|
2297
2710
|
const expiry = new Promise<{ kind: 'expired' }>((resolve) => {
|
|
2298
2711
|
resolveExpiry = resolve
|
|
2299
2712
|
})
|
|
2713
|
+
const shutdown = () => {
|
|
2714
|
+
if (!controller.signal.aborted) controller.abort(new Error('dispatcher shutdown'))
|
|
2715
|
+
}
|
|
2716
|
+
this.deps.shutdownSignal?.addEventListener('abort', shutdown, { once: true })
|
|
2717
|
+
if (this.deps.shutdownSignal?.aborted) shutdown()
|
|
2718
|
+
if (remainingMs <= 0) {
|
|
2719
|
+
abortCause = 'budget'
|
|
2720
|
+
controller.abort(new Error(sessionBudgetError(budgetSeconds)))
|
|
2721
|
+
}
|
|
2300
2722
|
const pending = Promise.resolve()
|
|
2301
|
-
.then(() =>
|
|
2723
|
+
.then(() => {
|
|
2724
|
+
if (remainingMs <= 0) throw new Error(sessionBudgetError(budgetSeconds))
|
|
2725
|
+
return run(controller.signal, onNativeSession)
|
|
2726
|
+
})
|
|
2302
2727
|
.then(
|
|
2303
2728
|
(value): { kind: 'value'; value: T } => ({ kind: 'value', value }),
|
|
2304
2729
|
(error): { kind: 'error'; error: unknown } => ({ kind: 'error', error }),
|
|
2305
2730
|
)
|
|
2731
|
+
const pauseRetries = new Set<ReturnType<typeof setTimeout>>()
|
|
2732
|
+
const interruptForPause = () => {
|
|
2733
|
+
if (closed || controller.signal.aborted) return
|
|
2734
|
+
void readBuildFacts(store, slug)
|
|
2735
|
+
.then((facts) => {
|
|
2736
|
+
if (closed || controller.signal.aborted) return
|
|
2737
|
+
const next = decideNextFromFacts(facts.value.state, facts.value.log, this.boundaryConfig)
|
|
2738
|
+
if (next.kind !== 'acknowledge' || next.command !== 'pause') return
|
|
2739
|
+
pauseInterrupted = true
|
|
2740
|
+
controller.abort(new Error('build paused by operator'))
|
|
2741
|
+
})
|
|
2742
|
+
.catch(() => {
|
|
2743
|
+
// Retry the authoritative read so one transient failure cannot lose
|
|
2744
|
+
// a stop, including a request preceding the subscription boundary.
|
|
2745
|
+
if (closed || controller.signal.aborted) return
|
|
2746
|
+
const timer = setTimeout(() => {
|
|
2747
|
+
pauseRetries.delete(timer)
|
|
2748
|
+
interruptForPause()
|
|
2749
|
+
}, 25)
|
|
2750
|
+
pauseRetries.add(timer)
|
|
2751
|
+
})
|
|
2752
|
+
}
|
|
2306
2753
|
const unsubscribe = store.subscribe(slug, { fromSeq: preSeq, pollMs: 25 }, (event) => {
|
|
2307
|
-
if (event.type === 'build.
|
|
2754
|
+
if (event.type === 'build.pause-requested') interruptForPause()
|
|
2755
|
+
if (event.type === 'build.abort-requested') {
|
|
2308
2756
|
abortCause = 'operator'
|
|
2309
2757
|
controller.abort(new Error(`build ${slug} aborted by operator`))
|
|
2310
2758
|
}
|
|
2311
2759
|
})
|
|
2760
|
+
// A pause may have landed after phase selection but before this bracket.
|
|
2761
|
+
// Arm future delivery first, then check the current durable command too.
|
|
2762
|
+
interruptForPause()
|
|
2312
2763
|
const schedule = this.deps.opts?.scheduleSessionBudget ?? defaultSessionBudgetScheduler
|
|
2313
2764
|
const cancelTimer = schedule(() => {
|
|
2314
2765
|
if (!controller.signal.aborted) {
|
|
@@ -2316,21 +2767,169 @@ export class BuildRunner {
|
|
|
2316
2767
|
controller.abort(new Error(sessionBudgetError(budgetSeconds)))
|
|
2317
2768
|
}
|
|
2318
2769
|
queueMicrotask(() => resolveExpiry({ kind: 'expired' }))
|
|
2319
|
-
},
|
|
2770
|
+
}, remainingMs)
|
|
2320
2771
|
|
|
2321
2772
|
try {
|
|
2322
2773
|
const settlement = await Promise.race([pending, expiry])
|
|
2774
|
+
await recording
|
|
2775
|
+
if (settlement.kind === 'error' && settlement.error instanceof FreshSessionRequired)
|
|
2776
|
+
throw settlement.error
|
|
2777
|
+
this.activeDurations.set(
|
|
2778
|
+
started.payload.session,
|
|
2779
|
+
Math.max(0, performance.now() - turnStarted),
|
|
2780
|
+
)
|
|
2781
|
+
if (pauseInterrupted && abortCause !== 'operator') {
|
|
2782
|
+
await this.suspendTurn(
|
|
2783
|
+
started,
|
|
2784
|
+
settlement,
|
|
2785
|
+
Math.max(0, performance.now() - turnStarted),
|
|
2786
|
+
'pause',
|
|
2787
|
+
)
|
|
2788
|
+
throw new SessionPaused()
|
|
2789
|
+
}
|
|
2790
|
+
if (this.deps.shutdownSignal?.aborted && abortCause === undefined) {
|
|
2791
|
+
await this.suspendTurn(
|
|
2792
|
+
started,
|
|
2793
|
+
settlement,
|
|
2794
|
+
Math.max(0, performance.now() - turnStarted),
|
|
2795
|
+
'shutdown',
|
|
2796
|
+
)
|
|
2797
|
+
throw new SessionShutdown()
|
|
2798
|
+
}
|
|
2323
2799
|
return {
|
|
2324
2800
|
settlement,
|
|
2325
2801
|
...(abortCause !== undefined ? { abortCause } : {}),
|
|
2326
2802
|
pending,
|
|
2327
2803
|
}
|
|
2328
2804
|
} finally {
|
|
2805
|
+
closed = true
|
|
2806
|
+
for (const timer of pauseRetries) clearTimeout(timer)
|
|
2807
|
+
this.deps.shutdownSignal?.removeEventListener('abort', shutdown)
|
|
2329
2808
|
cancelTimer()
|
|
2330
2809
|
unsubscribe()
|
|
2331
2810
|
}
|
|
2332
2811
|
}
|
|
2333
2812
|
|
|
2813
|
+
/** Cleanup cannot delay acknowledgement indefinitely after the adapter stopped. */
|
|
2814
|
+
private async boundedCleanup<T>(cleanup: Promise<T>): Promise<T | undefined> {
|
|
2815
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
2816
|
+
try {
|
|
2817
|
+
return await Promise.race([
|
|
2818
|
+
cleanup,
|
|
2819
|
+
new Promise<undefined>((resolve) => {
|
|
2820
|
+
timer = setTimeout(() => resolve(undefined), 1000)
|
|
2821
|
+
}),
|
|
2822
|
+
])
|
|
2823
|
+
} finally {
|
|
2824
|
+
if (timer !== undefined) clearTimeout(timer)
|
|
2825
|
+
}
|
|
2826
|
+
}
|
|
2827
|
+
|
|
2828
|
+
private async suspendTurn<T>(
|
|
2829
|
+
started: Extract<AbEvent, { type: 'session.started' }>,
|
|
2830
|
+
settlement: TurnSettlement<T>,
|
|
2831
|
+
activeMs: number,
|
|
2832
|
+
cause: 'shutdown' | 'pause',
|
|
2833
|
+
): Promise<void> {
|
|
2834
|
+
if (
|
|
2835
|
+
cause === 'shutdown' &&
|
|
2836
|
+
(this.deps.workspaceProvider !== 'git-worktree' || this.deps.workspaceRemote === true)
|
|
2837
|
+
)
|
|
2838
|
+
return
|
|
2839
|
+
const { store, slug } = this.deps
|
|
2840
|
+
const session = started.payload.session
|
|
2841
|
+
const state = (await readBuildFacts(store, slug)).value.state
|
|
2842
|
+
const native = state.sessions.open.find((s) => s.session === session)?.nativeHandle
|
|
2843
|
+
if (!native && cause === 'shutdown') return
|
|
2844
|
+
const producer = [...this.producerSessions.entries()].find(
|
|
2845
|
+
([phase]) => phase === started.payload.phase,
|
|
2846
|
+
)
|
|
2847
|
+
if (producer) this.producerSessions.delete(producer[0])
|
|
2848
|
+
let content: string | undefined
|
|
2849
|
+
let usage: { inputTokens: number; outputTokens: number; turns: number } | undefined
|
|
2850
|
+
const value =
|
|
2851
|
+
settlement.kind === 'value'
|
|
2852
|
+
? (settlement.value as { session?: AgentSessionHandle; result?: AgentTurnResult })
|
|
2853
|
+
: undefined
|
|
2854
|
+
const handle = value?.session ?? producer?.[1].handle
|
|
2855
|
+
if (handle) {
|
|
2856
|
+
try {
|
|
2857
|
+
const owner = producer?.[1].runner ?? this.deps.runtimes[started.payload.runner]!.runner
|
|
2858
|
+
const transcript = await this.boundedCleanup(owner.end(handle))
|
|
2859
|
+
content = transcript?.content
|
|
2860
|
+
usage = value?.result?.usage ?? (producer ? undefined : transcript?.metadata.usage)
|
|
2861
|
+
} catch {
|
|
2862
|
+
/* Preserve the structured stream if provider cleanup fails. */
|
|
2863
|
+
}
|
|
2864
|
+
}
|
|
2865
|
+
await this.bracketStreams.get(session)?.sink.close('aborted')
|
|
2866
|
+
if (!content && started.payload.stream)
|
|
2867
|
+
content = JSON.stringify(await store.readStream(started.payload.stream))
|
|
2868
|
+
content ??= JSON.stringify({ session, interrupted: true })
|
|
2869
|
+
for (;;) {
|
|
2870
|
+
const guard = await readTerminalGuardBoundary(
|
|
2871
|
+
store,
|
|
2872
|
+
slug,
|
|
2873
|
+
started.payload.phase,
|
|
2874
|
+
started.payload.round ?? 1,
|
|
2875
|
+
session,
|
|
2876
|
+
)
|
|
2877
|
+
const facts = await readBuildFacts(store, slug)
|
|
2878
|
+
if (facts.cursor !== guard.cursor) continue
|
|
2879
|
+
const postStep = started.payload.phase === 'finalize' && started.payload.role !== 'finalize'
|
|
2880
|
+
if (
|
|
2881
|
+
postStep
|
|
2882
|
+
? facts.value.log.finalizeStepsDone.has(started.payload.role)
|
|
2883
|
+
: guard.value.diagnostic !== undefined
|
|
2884
|
+
) {
|
|
2885
|
+
if (usage)
|
|
2886
|
+
await this.depositTranscriptAndEnd(
|
|
2887
|
+
session,
|
|
2888
|
+
{
|
|
2889
|
+
phase: started.payload.phase,
|
|
2890
|
+
round: started.payload.round ?? 1,
|
|
2891
|
+
role: started.payload.role,
|
|
2892
|
+
runnerName: started.payload.runner,
|
|
2893
|
+
},
|
|
2894
|
+
content,
|
|
2895
|
+
usage,
|
|
2896
|
+
started.payload.model,
|
|
2897
|
+
)
|
|
2898
|
+
return
|
|
2899
|
+
}
|
|
2900
|
+
if (!facts.value.state.sessions.open.some((s) => s.session === session)) return
|
|
2901
|
+
const suspended = await store.appendWithArtifactsIfCurrent(
|
|
2902
|
+
slug,
|
|
2903
|
+
guard.cursor,
|
|
2904
|
+
[
|
|
2905
|
+
{
|
|
2906
|
+
kind: 'transcript',
|
|
2907
|
+
content,
|
|
2908
|
+
metadata: { ...started.payload, usage, activeMs, interrupted: true },
|
|
2909
|
+
},
|
|
2910
|
+
],
|
|
2911
|
+
(deposited) => ({
|
|
2912
|
+
actor: KERNEL,
|
|
2913
|
+
type: 'session.suspended',
|
|
2914
|
+
payload: {
|
|
2915
|
+
session,
|
|
2916
|
+
phase: started.payload.phase,
|
|
2917
|
+
round: started.payload.round,
|
|
2918
|
+
handle: native,
|
|
2919
|
+
cause,
|
|
2920
|
+
activeMs,
|
|
2921
|
+
transcript: { kind: deposited[0]!.kind, rev: deposited[0]!.revision },
|
|
2922
|
+
...(usage ? { usage } : {}),
|
|
2923
|
+
},
|
|
2924
|
+
}),
|
|
2925
|
+
)
|
|
2926
|
+
if (suspended) {
|
|
2927
|
+
if (started.payload.stream) await store.closeStream(started.payload.stream, 'aborted')
|
|
2928
|
+
return
|
|
2929
|
+
}
|
|
2930
|
+
}
|
|
2931
|
+
}
|
|
2932
|
+
|
|
2334
2933
|
/** Start resource cleanup without allowing a stuck `end()` or transcript
|
|
2335
2934
|
* deposit to extend an already-expired phase. Conforming adapters still end
|
|
2336
2935
|
* immediately; every cleanup/deposit failure remains best-effort. The
|
|
@@ -2390,7 +2989,7 @@ export class BuildRunner {
|
|
|
2390
2989
|
* start fresh (fresh skeptic).
|
|
2391
2990
|
*/
|
|
2392
2991
|
private async executeSession(spec: SessionSpec): Promise<void> {
|
|
2393
|
-
const resolved = this.
|
|
2992
|
+
const resolved = this.resolveSessionRole(spec.role, ...(spec.roleAliases ?? []))
|
|
2394
2993
|
// Preserve the historical single-target path byte-for-byte when no chain
|
|
2395
2994
|
// is configured. Besides compatibility, this keeps its crash-gap and
|
|
2396
2995
|
// producer-continuation behavior independently well exercised.
|
|
@@ -2443,7 +3042,7 @@ export class BuildRunner {
|
|
|
2443
3042
|
round: spec.round,
|
|
2444
3043
|
})
|
|
2445
3044
|
const stream = streamBracket?.sink
|
|
2446
|
-
const startedEnvelope = await
|
|
3045
|
+
const startedEnvelope = await this.startBracket({
|
|
2447
3046
|
actor: KERNEL,
|
|
2448
3047
|
type: 'session.started',
|
|
2449
3048
|
payload: {
|
|
@@ -2467,18 +3066,20 @@ export class BuildRunner {
|
|
|
2467
3066
|
let result: AgentTurnResult | undefined
|
|
2468
3067
|
let turnError: unknown
|
|
2469
3068
|
const turn = await this.runBudgetedTurn(
|
|
2470
|
-
|
|
3069
|
+
startedEnvelope,
|
|
2471
3070
|
primary.sessionBudgetSeconds,
|
|
2472
|
-
async (signal) =>
|
|
3071
|
+
async (signal, onNativeSession) =>
|
|
2473
3072
|
live !== undefined
|
|
2474
3073
|
? live.runner
|
|
2475
3074
|
.continue(live.handle, continueMessage(spec), {
|
|
3075
|
+
onNativeSession,
|
|
2476
3076
|
env: await this.sessionEnvFor(spec.abPhase, session),
|
|
2477
3077
|
signal,
|
|
2478
3078
|
...(stream !== undefined ? { stream } : {}),
|
|
2479
3079
|
})
|
|
2480
3080
|
.then((continued) => ({ session: live.handle, result: continued }))
|
|
2481
|
-
: target.runner
|
|
3081
|
+
: this.startAgent(target.runner, {
|
|
3082
|
+
onNativeSession,
|
|
2482
3083
|
skill: spec.skill,
|
|
2483
3084
|
invocation: slug,
|
|
2484
3085
|
buildSlug: slug,
|
|
@@ -2568,7 +3169,11 @@ export class BuildRunner {
|
|
|
2568
3169
|
phase: spec.phase,
|
|
2569
3170
|
round: spec.round,
|
|
2570
3171
|
note: 'producer session kept live for §10 continuation; per-round turn transcript',
|
|
2571
|
-
turn: {
|
|
3172
|
+
turn: {
|
|
3173
|
+
text: result.text,
|
|
3174
|
+
usage: result.usage,
|
|
3175
|
+
prompt: this.interruptionPrompts.get(session),
|
|
3176
|
+
},
|
|
2572
3177
|
},
|
|
2573
3178
|
null,
|
|
2574
3179
|
2,
|
|
@@ -2678,6 +3283,8 @@ export class BuildRunner {
|
|
|
2678
3283
|
}
|
|
2679
3284
|
attempts.push(attempt)
|
|
2680
3285
|
|
|
3286
|
+
if (await this.settleReadDeferral(session)) return
|
|
3287
|
+
|
|
2681
3288
|
const eligibleForAlternate =
|
|
2682
3289
|
turnError !== undefined ||
|
|
2683
3290
|
(result?.kind === 'failed' && isAlternateEligible(result.failure))
|
|
@@ -2706,7 +3313,7 @@ export class BuildRunner {
|
|
|
2706
3313
|
// `session.started.role` records the LOGICAL name the pipeline dispatched
|
|
2707
3314
|
// in every case, including when a deprecated alias answered the lookup —
|
|
2708
3315
|
// the runtime/model on the same payload say what was resolved.
|
|
2709
|
-
const resolved = this.
|
|
3316
|
+
const resolved = this.resolveSessionRole(spec.role, ...(spec.roleAliases ?? []))
|
|
2710
3317
|
const { runner, runtime: runnerName, model, args, sessionBudgetSeconds } = resolved
|
|
2711
3318
|
const route = JSON.stringify({ runtime: runnerName, model, args })
|
|
2712
3319
|
// SPEC §9: the stream opens before the bracket's first turn — its id is
|
|
@@ -2734,7 +3341,7 @@ export class BuildRunner {
|
|
|
2734
3341
|
live = undefined
|
|
2735
3342
|
}
|
|
2736
3343
|
|
|
2737
|
-
const startedEnvelope = await
|
|
3344
|
+
const startedEnvelope = await this.startBracket({
|
|
2738
3345
|
actor: KERNEL,
|
|
2739
3346
|
type: 'session.started',
|
|
2740
3347
|
payload: {
|
|
@@ -2754,34 +3361,40 @@ export class BuildRunner {
|
|
|
2754
3361
|
let handle: AgentSessionHandle | undefined = live?.handle
|
|
2755
3362
|
let result: AgentTurnResult | undefined
|
|
2756
3363
|
let turnError: unknown
|
|
2757
|
-
const turn = await this.runBudgetedTurn(
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
3364
|
+
const turn = await this.runBudgetedTurn(
|
|
3365
|
+
startedEnvelope,
|
|
3366
|
+
sessionBudgetSeconds,
|
|
3367
|
+
async (signal, onNativeSession) => {
|
|
3368
|
+
if (live !== undefined) {
|
|
3369
|
+
// §10: the producer continues its session; the feedback message
|
|
3370
|
+
// points at the .ab/ inputs — `ab context` is the real carrier. The
|
|
3371
|
+
// turn re-issues ambient auth (D8) for the NEW bracket — AB_PHASE at
|
|
3372
|
+
// this round, AB_SESSION = this bracket's id — so the CLI's terminal
|
|
3373
|
+
// lands on the continued round instead of being rejected as round
|
|
3374
|
+
// 1's second terminal (§8.4 D5).
|
|
3375
|
+
return live.runner
|
|
3376
|
+
.continue(live.handle, continueMessage(spec), {
|
|
3377
|
+
onNativeSession,
|
|
3378
|
+
env: await this.sessionEnvFor(spec.abPhase, session),
|
|
3379
|
+
signal,
|
|
3380
|
+
...(stream !== undefined ? { stream } : {}),
|
|
3381
|
+
})
|
|
3382
|
+
.then((continued) => ({ session: live.handle, result: continued }))
|
|
3383
|
+
}
|
|
3384
|
+
return this.startAgent(runner, {
|
|
3385
|
+
onNativeSession,
|
|
3386
|
+
skill: spec.skill,
|
|
3387
|
+
invocation: slug,
|
|
3388
|
+
buildSlug: slug,
|
|
3389
|
+
workspacePath,
|
|
3390
|
+
...(model !== undefined ? { model } : {}),
|
|
3391
|
+
...(args !== undefined ? { args } : {}),
|
|
3392
|
+
env: await this.sessionEnvFor(spec.abPhase, session),
|
|
3393
|
+
signal,
|
|
3394
|
+
...(stream !== undefined ? { stream } : {}),
|
|
3395
|
+
})
|
|
3396
|
+
},
|
|
3397
|
+
)
|
|
2785
3398
|
if (turn.settlement.kind === 'value') {
|
|
2786
3399
|
handle = turn.settlement.value.session
|
|
2787
3400
|
result = turn.settlement.value.result
|
|
@@ -2872,7 +3485,11 @@ export class BuildRunner {
|
|
|
2872
3485
|
phase: spec.phase,
|
|
2873
3486
|
round: spec.round,
|
|
2874
3487
|
note: 'producer session kept live for §10 continuation; ' + 'per-round turn transcript',
|
|
2875
|
-
turn: {
|
|
3488
|
+
turn: {
|
|
3489
|
+
text: result.text,
|
|
3490
|
+
usage: result.usage,
|
|
3491
|
+
prompt: this.interruptionPrompts.get(session),
|
|
3492
|
+
},
|
|
2876
3493
|
},
|
|
2877
3494
|
null,
|
|
2878
3495
|
2,
|
|
@@ -2978,6 +3595,7 @@ export class BuildRunner {
|
|
|
2978
3595
|
mayRetry: !result.failure.permanent,
|
|
2979
3596
|
}
|
|
2980
3597
|
: { message: 'no-terminal', mayRetry: true }
|
|
3598
|
+
if (await this.settleReadDeferral(session)) return
|
|
2981
3599
|
await this.failPhase(
|
|
2982
3600
|
spec.phase,
|
|
2983
3601
|
spec.round,
|
|
@@ -3019,6 +3637,7 @@ export class BuildRunner {
|
|
|
3019
3637
|
runner: bracket.runnerName,
|
|
3020
3638
|
...(model !== undefined ? { model } : {}),
|
|
3021
3639
|
session,
|
|
3640
|
+
interruptionPrompt: this.interruptionPrompts.get(session),
|
|
3022
3641
|
usage,
|
|
3023
3642
|
},
|
|
3024
3643
|
},
|
|
@@ -3031,6 +3650,7 @@ export class BuildRunner {
|
|
|
3031
3650
|
type: 'session.ended',
|
|
3032
3651
|
payload: {
|
|
3033
3652
|
session,
|
|
3653
|
+
activeMs: this.activeDurations.get(session),
|
|
3034
3654
|
transcript: { kind: meta.kind, rev: meta.revision },
|
|
3035
3655
|
usage,
|
|
3036
3656
|
},
|
|
@@ -3101,7 +3721,9 @@ export class BuildRunner {
|
|
|
3101
3721
|
const streamId = await open(sink, info)
|
|
3102
3722
|
if (streamId === undefined) return undefined
|
|
3103
3723
|
sink.append([sessionPart(info)])
|
|
3104
|
-
|
|
3724
|
+
const bracket = { sink, streamId, info }
|
|
3725
|
+
this.bracketStreams.set(info.session, bracket)
|
|
3726
|
+
return bracket
|
|
3105
3727
|
} catch (error) {
|
|
3106
3728
|
this.onDiagnostic(
|
|
3107
3729
|
`session stream for "${info.session}" failed to open: ${errorMessage(error)}`,
|
|
@@ -3154,6 +3776,69 @@ export class BuildRunner {
|
|
|
3154
3776
|
* new evidence instead of deadlocking before its recovery session starts.
|
|
3155
3777
|
*/
|
|
3156
3778
|
|
|
3779
|
+
private async settleReadDeferral(session: string): Promise<boolean> {
|
|
3780
|
+
const { store, slug } = this.deps
|
|
3781
|
+
const item = (await readBuildFacts(store, slug)).value.log.readDeferrals.find(
|
|
3782
|
+
(item) => item.session === session,
|
|
3783
|
+
)
|
|
3784
|
+
if (!item) return false
|
|
3785
|
+
if (!item.ended) {
|
|
3786
|
+
await store.append(slug, {
|
|
3787
|
+
actor: KERNEL,
|
|
3788
|
+
type: 'session.ended',
|
|
3789
|
+
payload: {
|
|
3790
|
+
session,
|
|
3791
|
+
outcome: 'reclaimed',
|
|
3792
|
+
reclaimedBy: {
|
|
3793
|
+
instance: this.deps.instance,
|
|
3794
|
+
resumedFromSeq: (await readBuildFacts(store, slug)).value.state.lastSeq,
|
|
3795
|
+
},
|
|
3796
|
+
},
|
|
3797
|
+
})
|
|
3798
|
+
}
|
|
3799
|
+
await this.raiseReadDeferral(item)
|
|
3800
|
+
return true
|
|
3801
|
+
}
|
|
3802
|
+
|
|
3803
|
+
private async raiseReadDeferral(
|
|
3804
|
+
item: import('../kernel/read-deferrals').ReadDeferral,
|
|
3805
|
+
): Promise<void> {
|
|
3806
|
+
const { store, slug } = this.deps
|
|
3807
|
+
if (!item.settled)
|
|
3808
|
+
await store.append(slug, {
|
|
3809
|
+
actor: KERNEL,
|
|
3810
|
+
type: 'phase.deferred',
|
|
3811
|
+
payload: {
|
|
3812
|
+
session: item.session,
|
|
3813
|
+
phase: item.phase,
|
|
3814
|
+
round: item.round,
|
|
3815
|
+
reason: 'own-build-read',
|
|
3816
|
+
diagnostic: item.diagnostic,
|
|
3817
|
+
},
|
|
3818
|
+
})
|
|
3819
|
+
const state = (await readBuildFacts(store, slug)).value.state
|
|
3820
|
+
const ref = `own-build-read:${item.evidenceSeq}`
|
|
3821
|
+
if (
|
|
3822
|
+
[...state.openEscalations, ...state.answeredEscalations].some((raise) =>
|
|
3823
|
+
raise.refs?.includes(ref),
|
|
3824
|
+
)
|
|
3825
|
+
)
|
|
3826
|
+
return
|
|
3827
|
+
await store.append(slug, {
|
|
3828
|
+
actor: KERNEL,
|
|
3829
|
+
type: 'phase.deferral-escalated',
|
|
3830
|
+
payload: {
|
|
3831
|
+
id: this.deps.ids('esc'),
|
|
3832
|
+
session: item.session,
|
|
3833
|
+
phase: item.phase,
|
|
3834
|
+
round: item.round,
|
|
3835
|
+
evidenceSeq: item.evidenceSeq,
|
|
3836
|
+
refs: [ref, item.phase, String(item.round)],
|
|
3837
|
+
question: `${item.phase} round ${item.round} could not read its own build: ${item.diagnostic}. Repair CLI/store access, then answer retry or guidance to authorize a fresh session.`,
|
|
3838
|
+
},
|
|
3839
|
+
})
|
|
3840
|
+
}
|
|
3841
|
+
|
|
3157
3842
|
private async failPhase(
|
|
3158
3843
|
phase: Phase,
|
|
3159
3844
|
round: number,
|
|
@@ -3162,6 +3847,17 @@ export class BuildRunner {
|
|
|
3162
3847
|
mayRetry = true,
|
|
3163
3848
|
providerAttempts?: readonly ProviderAttempt[],
|
|
3164
3849
|
): Promise<void> {
|
|
3850
|
+
// A decisive fact may arrive while the agent is live (including while
|
|
3851
|
+
// its terminal is refused). Do not turn deferral into a consumed retry.
|
|
3852
|
+
if (await deferMixedVersion(this.deps.store, this.deps.slug, KERNEL))
|
|
3853
|
+
throw new MixedVersionDeferral()
|
|
3854
|
+
const deferred = (
|
|
3855
|
+
await readBuildFacts(this.deps.store, this.deps.slug)
|
|
3856
|
+
).value.log.readDeferrals.find((item) => item.phase === phase && item.round === round)
|
|
3857
|
+
if (deferred) {
|
|
3858
|
+
await this.settleReadDeferral(deferred.session)
|
|
3859
|
+
return
|
|
3860
|
+
}
|
|
3165
3861
|
const attempt = priorFailures + 1
|
|
3166
3862
|
await this.deps.store.append(this.deps.slug, {
|
|
3167
3863
|
actor: KERNEL,
|
|
@@ -3309,7 +4005,7 @@ export class BuildRunner {
|
|
|
3309
4005
|
private async closeProducerSessions(): Promise<void> {
|
|
3310
4006
|
for (const entry of this.producerSessions.values()) {
|
|
3311
4007
|
try {
|
|
3312
|
-
await entry.runner.end(entry.handle)
|
|
4008
|
+
await this.boundedCleanup(entry.runner.end(entry.handle))
|
|
3313
4009
|
} catch {
|
|
3314
4010
|
// Already dead — nothing to release.
|
|
3315
4011
|
}
|