@defrex/autobuild 1.0.0-beta.3 → 1.0.0

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.
Files changed (168) hide show
  1. package/README.md +1 -1
  2. package/SPEC.md +362 -56
  3. package/bin/ab-dispatch-generation.ts +93 -0
  4. package/docs/agent-runtimes.md +157 -0
  5. package/docs/answering-blocked-builds.md +21 -3
  6. package/docs/architecture.md +166 -7
  7. package/docs/build-store.md +123 -0
  8. package/docs/builds.md +82 -0
  9. package/docs/cli.md +48 -0
  10. package/docs/configuration-overview.md +93 -0
  11. package/docs/configuration.md +1778 -188
  12. package/docs/dispatcher-hosting.md +115 -0
  13. package/docs/dispatcher.md +59 -0
  14. package/docs/forges.md +121 -0
  15. package/docs/getting-started.md +60 -0
  16. package/docs/glossary.md +60 -0
  17. package/docs/hosted-service.md +2 -2
  18. package/docs/intake.md +48 -0
  19. package/docs/integration-overview.md +81 -0
  20. package/docs/integration-tree.md +87 -2
  21. package/docs/integration.md +54 -0
  22. package/docs/operating.md +114 -0
  23. package/docs/operator-api.md +64 -11
  24. package/docs/operator-surfaces.md +90 -0
  25. package/docs/overview.md +49 -0
  26. package/docs/plugin-authoring.md +426 -0
  27. package/docs/remote-store-protocol.md +12 -9
  28. package/docs/restacking.md +4 -1
  29. package/docs/seams.md +66 -0
  30. package/docs/setup.md +28 -6
  31. package/docs/skills.md +119 -0
  32. package/docs/spec-standard.md +23 -5
  33. package/docs/ticket-sources.md +132 -0
  34. package/docs/tickets.md +69 -0
  35. package/docs/troubleshooting.md +111 -0
  36. package/docs/upgrading.md +50 -0
  37. package/docs/verification.md +146 -0
  38. package/docs/website.md +47 -5
  39. package/docs/workspaces.md +152 -0
  40. package/package.json +5 -4
  41. package/packages/core/src/cli/artifact.ts +5 -4
  42. package/packages/core/src/cli/attention.ts +2 -0
  43. package/packages/core/src/cli/binary-direct.ts +190 -0
  44. package/packages/core/src/cli/binary-lifecycle.ts +46 -0
  45. package/packages/core/src/cli/binary.ts +24 -210
  46. package/packages/core/src/cli/build-control.ts +105 -2
  47. package/packages/core/src/cli/context.ts +41 -1
  48. package/packages/core/src/cli/dashboard/hand-pushed.ts +24 -0
  49. package/packages/core/src/cli/dashboard/handoff.ts +64 -0
  50. package/packages/core/src/cli/dashboard/integration-detail.ts +3 -0
  51. package/packages/core/src/cli/dashboard/model.ts +34 -10
  52. package/packages/core/src/cli/dashboard/render.ts +24 -2
  53. package/packages/core/src/cli/dashboard/rollback.ts +30 -0
  54. package/packages/core/src/cli/dispatch-args.ts +53 -0
  55. package/packages/core/src/cli/dispatch-crash-budget.ts +15 -0
  56. package/packages/core/src/cli/dispatch-frontend.ts +52 -5
  57. package/packages/core/src/cli/dispatch-handoff.ts +276 -0
  58. package/packages/core/src/cli/dispatch-launch-contract.ts +170 -0
  59. package/packages/core/src/cli/dispatch-launcher.ts +580 -0
  60. package/packages/core/src/cli/dispatch-parent-watch.ts +44 -0
  61. package/packages/core/src/cli/dispatch-preflight.ts +272 -0
  62. package/packages/core/src/cli/dispatch-presentation-job.ts +46 -0
  63. package/packages/core/src/cli/dispatch-presentation-ownership.ts +9 -0
  64. package/packages/core/src/cli/dispatch-process.ts +22 -90
  65. package/packages/core/src/cli/dispatch-readiness.ts +47 -0
  66. package/packages/core/src/cli/dispatch-repository-feed.ts +5 -1
  67. package/packages/core/src/cli/dispatch-runtime-cache.ts +522 -0
  68. package/packages/core/src/cli/dispatch-runtime-dependencies.ts +291 -0
  69. package/packages/core/src/cli/dispatch-runtime-use.ts +336 -0
  70. package/packages/core/src/cli/dispatch-upgrade-controller.ts +221 -0
  71. package/packages/core/src/cli/dispatch-upgrade-install.ts +258 -0
  72. package/packages/core/src/cli/dispatch-upgrade-source.ts +309 -0
  73. package/packages/core/src/cli/dispatch-upgrade-target.ts +190 -0
  74. package/packages/core/src/cli/dispatch.ts +604 -60
  75. package/packages/core/src/cli/dispatcher-liveness.ts +1 -1
  76. package/packages/core/src/cli/env.ts +1 -1
  77. package/packages/core/src/cli/harvest.ts +28 -11
  78. package/packages/core/src/cli/help.ts +10 -5
  79. package/packages/core/src/cli/installation.ts +5 -3
  80. package/packages/core/src/cli/main-checkout.ts +59 -0
  81. package/packages/core/src/cli/main.ts +15 -45
  82. package/packages/core/src/cli/node-verification-provenance.ts +97 -0
  83. package/packages/core/src/cli/repo-state.ts +3 -58
  84. package/packages/core/src/cli/repository-status.ts +67 -3
  85. package/packages/core/src/cli/runtime-spawn.ts +16 -0
  86. package/packages/core/src/cli/status.ts +57 -3
  87. package/packages/core/src/cli/upgrade-followup.ts +25 -0
  88. package/packages/core/src/cli/wait.ts +3 -0
  89. package/packages/core/src/cli/watch.ts +7 -0
  90. package/packages/core/src/config/landing.ts +8 -0
  91. package/packages/core/src/config/live.ts +1 -0
  92. package/packages/core/src/config/reference-metadata.ts +31 -0
  93. package/packages/core/src/config/reference.ts +342 -0
  94. package/packages/core/src/config/roles.ts +1 -1
  95. package/packages/core/src/config/schema.ts +352 -10
  96. package/packages/core/src/distribution-packaging.ts +116 -0
  97. package/packages/core/src/events/catalog.ts +6 -0
  98. package/packages/core/src/events/payloads.ts +53 -9
  99. package/packages/core/src/events/policy.ts +25 -0
  100. package/packages/core/src/events/repository.ts +184 -4
  101. package/packages/core/src/events/runtime.ts +16 -0
  102. package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +2515 -78
  103. package/packages/core/src/kernel/dispatch-drain-recovery.ts +121 -0
  104. package/packages/core/src/kernel/dispatch-status.ts +4 -2
  105. package/packages/core/src/kernel/dispatch-upgrade.ts +171 -0
  106. package/packages/core/src/kernel/dispatcher-liveness.ts +7 -0
  107. package/packages/core/src/kernel/generators/repository-journals.ts +82 -0
  108. package/packages/core/src/kernel/guarded-forge.ts +1 -1
  109. package/packages/core/src/kernel/harvest.ts +98 -4
  110. package/packages/core/src/kernel/integration-attention.ts +20 -0
  111. package/packages/core/src/kernel/integration-state.ts +80 -11
  112. package/packages/core/src/kernel/landing-readiness.ts +6 -1
  113. package/packages/core/src/kernel/mixed-version.ts +34 -0
  114. package/packages/core/src/kernel/node-polling.ts +160 -0
  115. package/packages/core/src/kernel/read-facts.ts +40 -5
  116. package/packages/core/src/kernel/reducer-registry.ts +3 -0
  117. package/packages/core/src/kernel/reducer.ts +75 -14
  118. package/packages/core/src/kernel/service-facts.ts +11 -4
  119. package/packages/core/src/ontology.ts +2 -2
  120. package/packages/core/src/open-map.ts +3 -1
  121. package/packages/core/src/operator/presentation.ts +7 -0
  122. package/packages/core/src/operator/query.ts +6 -2
  123. package/packages/core/src/operator/registry.ts +1 -1
  124. package/packages/core/src/plugins/load.ts +4 -1
  125. package/packages/core/src/plugins/runtime-snapshot.ts +128 -0
  126. package/packages/core/src/ports/forge/contract.ts +29 -0
  127. package/packages/core/src/ports/forge/github.ts +189 -61
  128. package/packages/core/src/ports/forge-failure.ts +1 -0
  129. package/packages/core/src/ports/forge-telemetry.ts +3 -0
  130. package/packages/core/src/ports/runner/session-env.ts +1 -0
  131. package/packages/core/src/ports/types.ts +10 -0
  132. package/packages/core/src/ports/workspace/distribution-archive.ts +22 -149
  133. package/packages/core/src/ports/workspace/distribution-release.ts +56 -0
  134. package/packages/core/src/ports/workspace/git-worktree.ts +2 -1
  135. package/packages/core/src/ports/workspace/local-build-execution.ts +37 -17
  136. package/packages/core/src/processes/build-child-exit.ts +5 -26
  137. package/packages/core/src/processes/build-runner.ts +81 -11
  138. package/packages/core/src/processes/dispatcher.ts +254 -23
  139. package/packages/core/src/processes/execution-no-progress.ts +6 -0
  140. package/packages/core/src/processes/execution-relaunch.ts +30 -0
  141. package/packages/core/src/processes/execution-settlement.ts +16 -7
  142. package/packages/core/src/processes/harvest-runner.ts +328 -292
  143. package/packages/core/src/processes/process-group-reaper.ts +17 -10
  144. package/packages/core/src/runtime-identity.ts +22 -3
  145. package/packages/core/src/store/contract.ts +153 -0
  146. package/packages/core/src/store/local/store.ts +197 -72
  147. package/packages/core/src/store/reduced-reads.ts +8 -0
  148. package/packages/core/src/store/reducer-value-shapes.json +5783 -3305
  149. package/packages/core/src/store/remote/client.ts +43 -0
  150. package/packages/core/src/store/repo-state-events.ts +20 -3
  151. package/packages/core/src/store/types.ts +7 -0
  152. package/skills/finalize/SKILL.md +3 -1
  153. package/skills/guide/SKILL.md +273 -48
  154. package/skills/guide/references/integration-tree.md +87 -2
  155. package/skills/guide/references/operator-api.md +71 -14
  156. package/skills/guide/references/plugin-authoring.md +231 -266
  157. package/skills/guide/references/remote-store-protocol.md +12 -9
  158. package/skills/guide/references/restacking.md +4 -1
  159. package/skills/guide/references/setup.md +28 -6
  160. package/skills/guide/references/spec-standard.md +23 -5
  161. package/skills/harvest/SKILL.md +4 -0
  162. package/skills/harvest-review/SKILL.md +4 -0
  163. package/skills/implement/SKILL.md +4 -2
  164. package/skills/operate/SKILL.md +104 -35
  165. package/skills/plan/SKILL.md +12 -4
  166. package/skills/spec/SKILL.md +20 -2
  167. package/skills/tickets/SKILL.md +20 -1
  168. package/templates/hosted-service/package.json +2 -2
@@ -1,3 +1,10 @@
1
+ import {
2
+ nodeActivityKey,
3
+ nodePollingSchedule,
4
+ nodePollingUnsettled,
5
+ nodeTicketObservationKey,
6
+ } from '../kernel/node-polling'
7
+ import { reportIntegrationFailures } from '../kernel/integration-attention'
1
8
  import {
2
9
  resolveTicketRef,
3
10
  migrationIdentity,
@@ -690,7 +697,6 @@ export class Dispatcher {
690
697
  // and each admitted verification/publication samples the source this pass.
691
698
  private integrationInputs?: Promise<IntegrationObservationInputs>
692
699
  private integrationPublicationInputs?: Promise<IntegrationObservationInputs>
693
- private readonly integrationTargets = new Map<string, string | undefined>()
694
700
  private readonly integrationPolls = new Map<string, { key: string; at: number }>()
695
701
  private readonly integrationLandingPolls = new Map<string, { key: string; at: number }>()
696
702
  private integrationPollDue(
@@ -704,22 +710,61 @@ export class Dispatcher {
704
710
  polls.set(slug, { key, at: now })
705
711
  return true
706
712
  }
707
- private async integrationClaimKey(slug: string): Promise<string> {
708
- const children = []
713
+ // Work targets are frozen; settled activity is refreshed only when its
714
+ // compact readiness digest changes. Neither cache decides eligibility.
715
+ private readonly nodePollingBuilds = new Map<
716
+ string,
717
+ {
718
+ target?: string
719
+ activity: Pick<BuildState, 'pollingActivity'>
720
+ settledKey?: string
721
+ }
722
+ >()
723
+ private readonly nodePollingTickReads = new Map<string, Promise<BuildState>>()
724
+
725
+ private async integrationClaimKey(slug: string, state: BuildState): Promise<string> {
726
+ return nodeTicketObservationKey(state, await this.nodePollingChildren(slug))
727
+ }
728
+ private async nodePollingChildren(slug: string, refresh = false) {
729
+ const children: { slug: string; state: Pick<BuildState, 'pollingActivity'> }[] = []
709
730
  for (const { record, digest } of await this.repoBuildDigests()) {
710
- // Work targets are frozen. Node relationships can change, so refresh
711
- // those edges before computing this parent's scheduling key.
712
- if (!this.integrationTargets.has(record.slug) || digest.kind === 'node') {
713
- const state = (await this.store.buildFacts(record.slug)).state
714
- if (state.lastSeq > 0) this.integrationTargets.set(record.slug, state.target?.build)
731
+ if (record.slug === slug) continue
732
+ const known = this.nodePollingBuilds.get(record.slug)
733
+ // Only node relationships can move. Unrelated known work never needs
734
+ // another full-facts read, even while it is running.
735
+ if (digest.kind !== 'node' && known && known.target !== slug) continue
736
+ const settledKey =
737
+ digest.terminal && !digest.executionOpen
738
+ ? JSON.stringify([
739
+ digest.terminal,
740
+ digest.readinessChangeSeq,
741
+ digest.merged,
742
+ digest.prClosed,
743
+ ])
744
+ : undefined
745
+ let current = known
746
+ if (!current || settledKey === undefined || current.settledKey !== settledKey) {
747
+ let read =
748
+ refresh || (known && known.settledKey !== settledKey)
749
+ ? undefined
750
+ : this.nodePollingTickReads.get(record.slug)
751
+ if (!read) {
752
+ read = this.store.buildFacts(record.slug).then((facts) => facts.state)
753
+ this.nodePollingTickReads.set(record.slug, read)
754
+ }
755
+ const state = await read
756
+ current = {
757
+ target: state.target?.build,
758
+ activity: { pollingActivity: state.pollingActivity },
759
+ settledKey,
760
+ }
761
+ if (state.lastSeq > 0) this.nodePollingBuilds.set(record.slug, current)
715
762
  }
716
- if (this.integrationTargets.get(record.slug) === slug)
717
- children.push(
718
- `${record.slug}:${digest.terminal ?? ''}:${digest.merged ?? ''}:${digest.prClosed ?? ''}`,
719
- )
763
+ if (current.target === slug) children.push({ slug: record.slug, state: current.activity })
720
764
  }
721
- return children.sort().join('|')
765
+ return children
722
766
  }
767
+
723
768
  private observeIntegration(slug: string, publication = false) {
724
769
  return observeIntegration(this.store, this.deps.tickets, slug, () => {
725
770
  if (publication) {
@@ -860,14 +905,21 @@ export class Dispatcher {
860
905
  const builds = new Map<string, number>()
861
906
  let total = 0
862
907
  let unattributed = 0
908
+ const categories = { restPrimary: 0, restNotModified: 0, graphql: 0 }
909
+ let classified = this.deps.forge.name === 'github'
863
910
  const stop = this.deps.forge.observeRequests?.((request) => {
864
911
  const build = request.build ?? forgeRequestContext().build
865
912
  total++
913
+ if (request.outcome) {
914
+ categories[request.outcome]++
915
+ classified = true
916
+ }
866
917
  if (build) builds.set(build, (builds.get(build) ?? 0) + 1)
867
918
  else unattributed++
868
919
  })
869
920
  const counts = () => ({
870
921
  total,
922
+ ...(classified ? categories : {}),
871
923
  unattributed,
872
924
  builds: [...builds]
873
925
  .map(([build, requests]) => ({ build, requests }))
@@ -877,7 +929,9 @@ export class Dispatcher {
877
929
  })
878
930
  this.lastForgeRequests = undefined
879
931
  try {
880
- const report = await this.runTick(opts)
932
+ const report = await (this.deps.forge.withReadScope
933
+ ? this.deps.forge.withReadScope(() => this.runTick(opts))
934
+ : this.runTick(opts))
881
935
  if (stop || this.deps.config.forge === 'local-git') report.forgeRequests = counts()
882
936
  return report
883
937
  } finally {
@@ -889,6 +943,7 @@ export class Dispatcher {
889
943
  private async runTick(opts: TickOpts): Promise<TickReport> {
890
944
  this.integrationInputs = undefined
891
945
  this.integrationPublicationInputs = undefined
946
+ this.nodePollingTickReads.clear()
892
947
  // One immutable config for this complete decision pass. A reload racing the
893
948
  // tick is observed by the next tick, never half-way through this one.
894
949
  this.deps.config = this.deps.getConfig?.() ?? this.deps.config
@@ -1906,6 +1961,18 @@ export class Dispatcher {
1906
1961
  const entries = (await this.repoBuildDigests()).filter(
1907
1962
  ({ digest }) => isWorkDigest(digest) || digest.integrationAuditPending === true,
1908
1963
  )
1964
+ // Receipt reporting requires no forge call, capacity or workspace. Recover
1965
+ // every episode before the transport-budget and janitor-backoff gates.
1966
+ for (const { record, digest } of entries) {
1967
+ if (digest.kind !== 'node' || !digest.nodeOpened || digest.terminal) continue
1968
+ try {
1969
+ await reportIntegrationFailures(this.store, record.slug)
1970
+ } catch (error) {
1971
+ report.janitorDiagnostics.push(
1972
+ `build ${record.slug}: integration attention recovery failed — ${String(error)}`,
1973
+ )
1974
+ }
1975
+ }
1909
1976
  for (const { record, digest } of entries) {
1910
1977
  // Budget gate: the janitor's per-build path issues forge/store transport
1911
1978
  // calls (PR probes, closePr, deleteBranch); once the budget is spent,
@@ -1920,12 +1987,15 @@ export class Dispatcher {
1920
1987
  // Once a record is known, contain its complete janitor path: loading
1921
1988
  // and reducing facts, cleanup, forge/ticket calls, and store writes.
1922
1989
  // Existing partial facts leave the same work due on the next tick.
1923
- let { state, workspace, log } = await this.store.buildFacts(record.slug)
1990
+ const initialFacts = await this.store.buildFacts(record.slug)
1991
+ let { state, workspace, log } = initialFacts
1924
1992
  previous = state.janitor
1925
1993
  const controlDue =
1926
1994
  state.pendingCommands.length > 0 ||
1927
1995
  state.discardRequest !== undefined ||
1928
- (state.landing?.command && state.landing.command.seq !== state.landing.acknowledgedSeq)
1996
+ (state.pr &&
1997
+ state.landing?.command &&
1998
+ state.landing.command.seq !== state.landing.acknowledgedSeq)
1929
1999
  if (
1930
2000
  !controlDue &&
1931
2001
  previous &&
@@ -1945,6 +2015,80 @@ export class Dispatcher {
1945
2015
  loaded ??= (await readBuildFacts(this.store, record.slug)).value
1946
2016
  return loaded
1947
2017
  }
2018
+ const pollStart = state
2019
+ const pollChildren =
2020
+ state.kind === 'node' ? await this.nodePollingChildren(record.slug) : []
2021
+ const pollKey = nodeActivityKey(state, pollChildren)
2022
+ const pollSchedule =
2023
+ state.kind === 'node' && !nodePollingUnsettled(state)
2024
+ ? nodePollingSchedule(
2025
+ state,
2026
+ pollKey,
2027
+ this.deps.config.policy,
2028
+ this.deps.clock(),
2029
+ executionLeaseLive ||
2030
+ initialFacts.open !== null ||
2031
+ initialFacts.publication.pending() ||
2032
+ initialFacts.publication.abandonedPending() ||
2033
+ undefined,
2034
+ )
2035
+ : undefined
2036
+ if (state.kind === 'node' && !controlDue && pollSchedule && !pollSchedule.due) {
2037
+ attempted = false
2038
+ return
2039
+ }
2040
+ let incompleteObservation = false
2041
+ const diagnosticsBefore = report.janitorDiagnostics.length
2042
+ const completeNodePoll = async () => {
2043
+ if (
2044
+ pollStart.kind !== 'node' ||
2045
+ !pollStart.nodeOpened ||
2046
+ incompleteObservation ||
2047
+ executionLeaseLive ||
2048
+ report.janitorDiagnostics.length !== diagnosticsBefore ||
2049
+ this.outOfBudget(opts)
2050
+ )
2051
+ return
2052
+ const current = (await this.store.buildFacts(record.slug)).state
2053
+ if (nodePollingUnsettled(current) && current.status !== 'done') return
2054
+ const children = await this.nodePollingChildren(record.slug, true)
2055
+ const key = nodeActivityKey(current, children)
2056
+ // A command or child event arriving during observation stays due.
2057
+ const commandTypes = [
2058
+ 'build.land-requested',
2059
+ 'build.hold-requested',
2060
+ 'node.restack-requested',
2061
+ 'escalation.answered',
2062
+ 'node.parent-requested',
2063
+ ]
2064
+ if (
2065
+ commandTypes.some(
2066
+ (type) => current.pollingActivity?.[type] !== pollStart.pollingActivity?.[type],
2067
+ ) ||
2068
+ nodeActivityKey(pollStart, children) !== pollKey
2069
+ )
2070
+ return
2071
+ const now = this.deps.clock().toISOString()
2072
+ const previousCheck = pollStart.nodePolling
2073
+ await this.store.appendIfCurrent(record.slug, current.lastSeq, {
2074
+ actor: DISPATCHER,
2075
+ type: 'node.poll-checked',
2076
+ payload: {
2077
+ activityKey: key,
2078
+ unchangedSince:
2079
+ previousCheck?.activityKey === key &&
2080
+ previousCheck.nodeIdleAfterSeconds ===
2081
+ this.deps.config.policy.nodeIdleAfterSeconds &&
2082
+ previousCheck.nodeIdlePollSeconds ===
2083
+ this.deps.config.policy.nodeIdlePollSeconds &&
2084
+ Date.parse(previousCheck.unchangedSince) <= Date.parse(now)
2085
+ ? previousCheck.unchangedSince
2086
+ : now,
2087
+ nodeIdleAfterSeconds: this.deps.config.policy.nodeIdleAfterSeconds,
2088
+ nodeIdlePollSeconds: this.deps.config.policy.nodeIdlePollSeconds,
2089
+ },
2090
+ })
2091
+ }
1948
2092
  let observedPr: Awaited<ReturnType<Forge['getPrState']>> | undefined
1949
2093
  // A replacement head is followed first by a forge read and cancellation,
1950
2094
  // ahead of node cascade, controls, or any other action for this build.
@@ -1961,7 +2105,13 @@ export class Dispatcher {
1961
2105
  if (observed.state !== 'open') {
1962
2106
  await this.checkPr(
1963
2107
  record,
1964
- { history, workspace },
2108
+ {
2109
+ history,
2110
+ workspace,
2111
+ observed: (pr) => {
2112
+ incompleteObservation ||= pr.state === 'open' && pr.mergeable === null
2113
+ },
2114
+ },
1965
2115
  state,
1966
2116
  report,
1967
2117
  launched,
@@ -2023,9 +2173,48 @@ export class Dispatcher {
2023
2173
  return
2024
2174
  }
2025
2175
  if (state.nodeOpened) {
2176
+ let primed = false
2177
+ if (
2178
+ record.branch &&
2179
+ state.baseBranch &&
2180
+ !((state.status === 'paused' || state.status === 'blocked') && !state.pr)
2181
+ ) {
2182
+ primed = await this.primeNodeReads(record, state, report)
2183
+ }
2184
+ if (
2185
+ primed &&
2186
+ state.pr &&
2187
+ (
2188
+ await this.deps.forge.getPrState(
2189
+ this.deps.checkout ?? this.deps.repo,
2190
+ state.pr.number,
2191
+ )
2192
+ ).state !== 'open'
2193
+ ) {
2194
+ await this.checkPr(
2195
+ record,
2196
+ {
2197
+ history,
2198
+ workspace,
2199
+ observed: (pr) => {
2200
+ incompleteObservation ||= pr.state === 'open' && pr.mergeable === null
2201
+ },
2202
+ },
2203
+ state,
2204
+ report,
2205
+ launched,
2206
+ executionLeaseLive,
2207
+ )
2208
+ await completeNodePoll()
2209
+ return
2210
+ }
2211
+ const previousParent = state.baseBranch
2026
2212
  await this.observeTicketTarget(record, state, report)
2027
2213
  loaded = undefined
2028
2214
  ;({ state, workspace, log } = await this.store.buildFacts(record.slug))
2215
+ if (record.branch && state.baseBranch && previousParent !== state.baseBranch) {
2216
+ await this.primeNodeReads(record, state, report)
2217
+ }
2029
2218
  }
2030
2219
  if ((state.status === 'paused' || state.status === 'blocked') && !state.pr) return
2031
2220
  if (state.nodeOpened && state.baseBranch) {
@@ -2062,6 +2251,7 @@ export class Dispatcher {
2062
2251
  }
2063
2252
  if (state.nodeOpened !== undefined && state.pr === undefined) {
2064
2253
  await this.pollEmptyNode(record, state)
2254
+ await completeNodePoll()
2065
2255
  return
2066
2256
  }
2067
2257
  }
@@ -2112,7 +2302,13 @@ export class Dispatcher {
2112
2302
  if (state.pr) {
2113
2303
  await this.checkPr(
2114
2304
  record,
2115
- { history, workspace },
2305
+ {
2306
+ history,
2307
+ workspace,
2308
+ observed: (pr) => {
2309
+ incompleteObservation ||= pr.state === 'open' && pr.mergeable === null
2310
+ },
2311
+ },
2116
2312
  state,
2117
2313
  report,
2118
2314
  launched,
@@ -2120,6 +2316,7 @@ export class Dispatcher {
2120
2316
  observedPr,
2121
2317
  )
2122
2318
  }
2319
+ await completeNodePoll()
2123
2320
  } catch (error) {
2124
2321
  if (error instanceof ForgeCooldown) {
2125
2322
  attempted = false
@@ -2154,6 +2351,34 @@ export class Dispatcher {
2154
2351
  }
2155
2352
  }
2156
2353
 
2354
+ /** Priming saves requests but must not block parent recovery or ordinary observations. */
2355
+ private async primeNodeReads(
2356
+ record: BuildRecord,
2357
+ state: BuildState,
2358
+ report: TickReport,
2359
+ ): Promise<boolean> {
2360
+ if (!this.deps.forge.primeNodeReads || !record.branch || !state.baseBranch) return false
2361
+ try {
2362
+ await this.deps.forge.primeNodeReads({
2363
+ workspacePath: this.deps.checkout ?? this.deps.repo,
2364
+ parent: state.baseBranch,
2365
+ head: record.branch,
2366
+ ...(state.pr ? { prNumber: state.pr.number } : {}),
2367
+ })
2368
+ return true
2369
+ } catch (error) {
2370
+ if (
2371
+ error instanceof ForgeRequestDeferred ||
2372
+ (isForgeFailure(error) && error.details.kind === 'rate-limited')
2373
+ )
2374
+ throw error
2375
+ report.janitorDiagnostics.push(
2376
+ `build ${record.slug}: node read priming failed — ${error instanceof Error || isForgeFailure(error) ? error.message : String(error)}`,
2377
+ )
2378
+ return false
2379
+ }
2380
+ }
2381
+
2157
2382
  private parentContext(): ParentContext {
2158
2383
  return {
2159
2384
  store: this.store,
@@ -2749,7 +2974,11 @@ export class Dispatcher {
2749
2974
  record: BuildRecord,
2750
2975
  // `workspace` is the open workspace fact; `history` loads the raw log, which
2751
2976
  // only the branches that act on a PR transition need.
2752
- source: { history: () => Promise<CurrentBuildFacts>; workspace: OpenBuildWorkspace | null },
2977
+ source: {
2978
+ history: () => Promise<CurrentBuildFacts>
2979
+ workspace: OpenBuildWorkspace | null
2980
+ observed?: (pr: Awaited<ReturnType<Forge['getPrState']>>) => void
2981
+ },
2753
2982
  state: BuildState,
2754
2983
  report: TickReport,
2755
2984
  launched: Set<string>,
@@ -2779,6 +3008,7 @@ export class Dispatcher {
2779
3008
  if (!recoverLanding && state.prState !== 'merged') throw error
2780
3009
  prState = { state: 'open', mergeable: null }
2781
3010
  }
3011
+ source.observed?.(prState)
2782
3012
  if (state.prState === 'merged' && state.mergedSha)
2783
3013
  prState = { state: 'merged', sha: state.mergedSha }
2784
3014
  let nativeCancelled = false
@@ -3309,6 +3539,7 @@ export class Dispatcher {
3309
3539
  }),
3310
3540
  )
3311
3541
  state = (await store.buildFacts(record.slug)).state
3542
+ source.observed?.(prState)
3312
3543
  if (state.prState === 'merged' && state.mergedSha)
3313
3544
  prState = { state: 'merged', sha: state.mergedSha }
3314
3545
  }
@@ -4214,7 +4445,7 @@ export class Dispatcher {
4214
4445
  if (record.lease) {
4215
4446
  if (new Date(record.lease.expiresAt).getTime() > now) continue // healthy
4216
4447
  } else if (now - new Date(record.updatedAt).getTime() < this.leaseTtlMs) {
4217
- continue // absent lease within the first-claim grace (see DispatcherOpts)
4448
+ if ((await this.store.buildFacts(record.slug)).lastExecution !== 'relaunch') continue // absent lease within the first-claim grace (see DispatcherOpts)
4218
4449
  }
4219
4450
  const facts = await this.store.buildFacts(record.slug)
4220
4451
  const state = facts.state
@@ -4261,7 +4492,7 @@ export class Dispatcher {
4261
4492
  // place: the guest finished its work, the workspace is still the
4262
4493
  // build's, and replacing the environment would discard a healthy
4263
4494
  // generation. Only an unproven stale lease or a proved loss reaps.
4264
- let endedInPlace = facts.lastExecution === 'completed'
4495
+ let endedInPlace = facts.lastExecution === 'completed' || facts.lastExecution === 'relaunch'
4265
4496
  if (
4266
4497
  foreignExecution !== null &&
4267
4498
  foreignExecution.commandId !== undefined &&
@@ -5170,14 +5401,14 @@ export class Dispatcher {
5170
5401
  payload: comparison,
5171
5402
  })
5172
5403
  state = (await this.store.buildFacts(record.slug)).state
5173
- const claims = await this.integrationClaimKey(record.slug)
5404
+ const claims = await this.integrationClaimKey(record.slug, state)
5174
5405
  const unchangedFailure = integrationFailed(state)
5175
5406
  if (
5176
5407
  !unchangedFailure &&
5177
5408
  this.integrationPollDue(
5178
5409
  this.integrationPolls,
5179
5410
  record.slug,
5180
- `${comparison.headSha}:${comparison.parentSha}:${state.nodeRestack?.inventory?.head ?? ''}:${state.landing?.command?.seq ?? state.landing?.decision?.seq ?? ''}:${claims}`,
5411
+ `${state.restartSince}:${comparison.headSha}:${comparison.parentSha}:${state.nodeRestack?.inventory?.head ?? ''}:${state.landing?.command?.seq ?? state.landing?.decision?.seq ?? ''}:${claims}`,
5181
5412
  )
5182
5413
  ) {
5183
5414
  await this.observeIntegration(record.slug)
@@ -9,6 +9,9 @@ import type { RepoViewStore } from './repo-view'
9
9
 
10
10
  /** Every new event must explicitly choose whether it advances build work. */
11
11
  export const executionProgress = {
12
+ 'execution.relaunch-requested': 'bookkeeping',
13
+ 'execution.drain-authorized': 'bookkeeping',
14
+ 'execution.drain-settled': 'bookkeeping',
12
15
  'build.compatibility-deferred': 'bookkeeping',
13
16
  'build.janitor-attempt-failed': 'bookkeeping',
14
17
  'build.janitor-failing': 'bookkeeping',
@@ -34,6 +37,7 @@ export const executionProgress = {
34
37
  'integration.prepared': 'progress',
35
38
  'integration.landing-intended': 'progress',
36
39
  'integration.landed': 'progress',
40
+ 'integration.verification-failed': 'bookkeeping',
37
41
  'integration.audited': 'progress',
38
42
  'node.opening': 'progress',
39
43
  'node.writer-reserved': 'progress',
@@ -56,9 +60,11 @@ export const executionProgress = {
56
60
  'node.parent-rejection-cleared': 'bookkeeping',
57
61
  'node.parent-rejected': 'bookkeeping',
58
62
  'node.opened': 'progress',
63
+ 'node.poll-checked': 'bookkeeping',
59
64
  'node.compared': 'bookkeeping',
60
65
  'pr.opened': 'progress',
61
66
  'build.completed': 'progress',
67
+ 'runner.runtime-recorded': 'bookkeeping',
62
68
  'runner.attached': 'bookkeeping',
63
69
  'runner.setup-failed': 'failure',
64
70
  'workspace.provisioned': 'bookkeeping',
@@ -0,0 +1,30 @@
1
+ import type { RuntimeIdentity } from '../events/runtime'
2
+ import type { BuildState } from '../kernel/reducer'
3
+
4
+ export function sameRuntime(a: RuntimeIdentity, b: RuntimeIdentity): boolean {
5
+ return (
6
+ a.version === b.version &&
7
+ a.commit === b.commit &&
8
+ a.source.kind === b.source.kind &&
9
+ a.source.location === b.source.location
10
+ )
11
+ }
12
+
13
+ /** Pure boundary decision over facts already read by the runner. */
14
+ export function relaunchTarget(state: BuildState, instance: string, runtime: RuntimeIdentity) {
15
+ const execution = state.drainingExecutions?.[instance]
16
+ const authorization = execution?.authorization
17
+ if (
18
+ execution?.ended ||
19
+ !authorization?.targetRuntime ||
20
+ !sameRuntime(authorization.runtime, runtime) ||
21
+ sameRuntime(authorization.targetRuntime, runtime)
22
+ )
23
+ return undefined
24
+ return {
25
+ instance,
26
+ correlation: authorization.correlation,
27
+ from: runtime,
28
+ to: authorization.targetRuntime,
29
+ }
30
+ }
@@ -18,6 +18,7 @@ import type {
18
18
  BuildExecutionIdentity,
19
19
  ExecutionObservation,
20
20
  } from '../ports/workspace/build-execution'
21
+ import { readBuildFacts } from '../store/reduced-reads'
21
22
  import { defineReducer } from '../kernel/incremental'
22
23
  import type { BuildStore } from '../store/types'
23
24
 
@@ -65,24 +66,29 @@ export function openExecution(events: AbEvent[]): OpenExecution | null {
65
66
  return openExecutionReducer.reduce(events)
66
67
  }
67
68
 
68
- export type ExecutionOutcome = 'open' | 'completed' | 'stopped' | 'lost' | 'none'
69
+ export type ExecutionOutcome = 'open' | 'completed' | 'stopped' | 'lost' | 'relaunch' | 'none'
69
70
 
70
- export const LAST_EXECUTION_OUTCOME_REDUCER_VERSION = 1
71
+ export const LAST_EXECUTION_OUTCOME_REDUCER_VERSION = 2
71
72
 
72
73
  /** The lifecycle state of the latest recorded execution: `open` (no matching
73
- * end yet), `completed`/`stopped`/`lost` (its recorded end), or `none`. */
74
+ * end yet), `completed`/`stopped`/`lost`/`relaunch` (its recorded end), or `none`. */
74
75
  export const lastExecutionOutcomeReducer = defineReducer<
75
- { state: ExecutionOutcome },
76
+ { state: ExecutionOutcome; instance: string | null },
76
77
  AbEvent,
77
78
  ExecutionOutcome
78
79
  >({
79
80
  version: LAST_EXECUTION_OUTCOME_REDUCER_VERSION,
80
- initial: () => ({ state: 'none' }),
81
+ initial: () => ({ state: 'none', instance: null }),
81
82
  fold(acc, events) {
82
83
  for (const event of events) {
83
84
  if (event.type === 'execution.started') {
84
85
  acc.state = 'open'
85
- } else if (event.type === 'execution.ended' && acc.state === 'open') {
86
+ acc.instance = event.payload.instance
87
+ } else if (
88
+ event.type === 'execution.ended' &&
89
+ acc.state === 'open' &&
90
+ acc.instance === event.payload.instance
91
+ ) {
86
92
  acc.state = event.payload.outcome
87
93
  }
88
94
  }
@@ -173,6 +179,9 @@ export async function settleOpenExecution(
173
179
  }
174
180
  if (observation.state === 'running') return 'running'
175
181
 
182
+ const request = (await readBuildFacts(deps.store, slug)).value.state.drainingExecutions?.[
183
+ open.instance
184
+ ]?.relaunch
176
185
  const ended: EventWrite<'execution.ended'> =
177
186
  observation.state === 'ended'
178
187
  ? {
@@ -181,7 +190,7 @@ export async function settleOpenExecution(
181
190
  payload: {
182
191
  instance: open.instance,
183
192
  workspaceRef: open.workspaceRef,
184
- outcome: 'completed',
193
+ outcome: observation.exitCode === 0 && request ? 'relaunch' : 'completed',
185
194
  exitCode: observation.exitCode ?? null,
186
195
  },
187
196
  }