@defrex/autobuild 0.13.0 → 1.0.0-beta.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 (210) hide show
  1. package/README.md +5 -3
  2. package/SPEC.md +673 -99
  3. package/bin/ab-dispatch-kernel.ts +4 -6
  4. package/docs/architecture.md +322 -75
  5. package/docs/assets/headline-wide.png +0 -0
  6. package/docs/configuration.md +112 -12
  7. package/docs/hosted-service.md +20 -2
  8. package/docs/integration-tree.md +117 -0
  9. package/docs/landing-policy.md +150 -0
  10. package/docs/mcp.md +24 -0
  11. package/docs/migration-1.0.md +68 -0
  12. package/docs/operator-api.md +20 -9
  13. package/docs/remote-store-protocol.md +114 -9
  14. package/docs/remote-ticket-protocol.md +26 -5
  15. package/docs/restacking.md +79 -0
  16. package/docs/setup.md +49 -6
  17. package/docs/ticket-dependencies.md +69 -0
  18. package/docs/ticket-migration.md +8 -0
  19. package/package.json +4 -3
  20. package/packages/core/src/cli/attention.ts +40 -163
  21. package/packages/core/src/cli/build-control.ts +75 -55
  22. package/packages/core/src/cli/bulk-control.ts +3 -2
  23. package/packages/core/src/cli/context.ts +68 -78
  24. package/packages/core/src/cli/dashboard/actions.ts +6 -3
  25. package/packages/core/src/cli/dashboard/landing-display.ts +6 -0
  26. package/packages/core/src/cli/dashboard/live.ts +5 -0
  27. package/packages/core/src/cli/dashboard/model.ts +178 -40
  28. package/packages/core/src/cli/dashboard/poll.ts +15 -9
  29. package/packages/core/src/cli/dashboard/refresh.ts +176 -0
  30. package/packages/core/src/cli/dashboard/render.ts +78 -28
  31. package/packages/core/src/cli/dashboard/repository-poll.ts +57 -0
  32. package/packages/core/src/cli/dashboard/selection.ts +7 -1
  33. package/packages/core/src/cli/dashboard/tree.ts +107 -0
  34. package/packages/core/src/cli/dispatch-frontend.ts +173 -169
  35. package/packages/core/src/cli/dispatch-process.ts +14 -7
  36. package/packages/core/src/cli/dispatch-repository-feed.ts +196 -0
  37. package/packages/core/src/cli/dispatch.ts +397 -214
  38. package/packages/core/src/cli/dispatcher-liveness.ts +41 -21
  39. package/packages/core/src/cli/harvest.ts +71 -39
  40. package/packages/core/src/cli/help.ts +49 -32
  41. package/packages/core/src/cli/historical-build-state.ts +27 -0
  42. package/packages/core/src/cli/init-validation.ts +43 -2
  43. package/packages/core/src/cli/keyboard.ts +6 -0
  44. package/packages/core/src/cli/landings.ts +18 -0
  45. package/packages/core/src/cli/main.ts +52 -18
  46. package/packages/core/src/cli/mcp.ts +9 -0
  47. package/packages/core/src/cli/pr-attachments.ts +10 -13
  48. package/packages/core/src/cli/pr-summary-publication.ts +27 -60
  49. package/packages/core/src/cli/pr-summary.ts +43 -13
  50. package/packages/core/src/cli/publication-settlement.ts +177 -21
  51. package/packages/core/src/cli/repository-status.ts +49 -2
  52. package/packages/core/src/cli/status.ts +133 -36
  53. package/packages/core/src/cli/terminal-restore.ts +54 -0
  54. package/packages/core/src/cli/terminal.ts +16 -2
  55. package/packages/core/src/cli/terminals.ts +121 -219
  56. package/packages/core/src/cli/ticket-editor.ts +10 -3
  57. package/packages/core/src/cli/ticket.ts +115 -20
  58. package/packages/core/src/cli/upgrade-commit.ts +4 -0
  59. package/packages/core/src/cli/upgrade-compatibility.ts +2 -2
  60. package/packages/core/src/cli/upgrade-landing.ts +69 -0
  61. package/packages/core/src/cli/upgrade.ts +16 -0
  62. package/packages/core/src/cli/wait.ts +194 -91
  63. package/packages/core/src/cli/watch.ts +111 -109
  64. package/packages/core/src/config/landing.ts +22 -0
  65. package/packages/core/src/config/live.ts +1 -0
  66. package/packages/core/src/config/pipeline-source.ts +4 -3
  67. package/packages/core/src/config/schema.ts +3 -0
  68. package/packages/core/src/events/catalog.ts +48 -3
  69. package/packages/core/src/events/payloads.ts +295 -6
  70. package/packages/core/src/events/repository.ts +38 -3
  71. package/packages/core/src/integration/harness.ts +11 -2
  72. package/packages/core/src/kernel/__snapshots__/reducer-registry.test.ts.snap +3330 -119
  73. package/packages/core/src/kernel/auto-merge-default.ts +1 -0
  74. package/packages/core/src/kernel/auto-merge.ts +29 -4
  75. package/packages/core/src/kernel/build-identity.ts +94 -0
  76. package/packages/core/src/kernel/change-presence.ts +179 -0
  77. package/packages/core/src/kernel/dispatch-status.ts +17 -1
  78. package/packages/core/src/kernel/dispatcher-liveness.ts +10 -4
  79. package/packages/core/src/kernel/engine.ts +89 -2
  80. package/packages/core/src/kernel/generators/build-log.ts +23 -0
  81. package/packages/core/src/kernel/generators/leaf-restack-log.ts +110 -0
  82. package/packages/core/src/kernel/generators/node-restack-log.ts +94 -0
  83. package/packages/core/src/kernel/generators/repository-journals.ts +25 -3
  84. package/packages/core/src/kernel/harvest.ts +25 -23
  85. package/packages/core/src/kernel/integration-completeness.ts +81 -0
  86. package/packages/core/src/kernel/integration-landing.ts +263 -0
  87. package/packages/core/src/kernel/integration-stack.ts +36 -0
  88. package/packages/core/src/kernel/integration-state.ts +135 -0
  89. package/packages/core/src/kernel/landing-policy.ts +47 -0
  90. package/packages/core/src/kernel/landing-publication.ts +41 -0
  91. package/packages/core/src/kernel/landing-readiness.ts +67 -0
  92. package/packages/core/src/kernel/landing-state.ts +96 -0
  93. package/packages/core/src/kernel/leaf-restack-completion.ts +24 -0
  94. package/packages/core/src/kernel/leaf-restack-state.ts +73 -0
  95. package/packages/core/src/kernel/log-index-value.ts +38 -0
  96. package/packages/core/src/kernel/log-index.ts +35 -5
  97. package/packages/core/src/kernel/node-landing.ts +439 -0
  98. package/packages/core/src/kernel/node-parent.ts +184 -0
  99. package/packages/core/src/kernel/node-restack-state.ts +191 -0
  100. package/packages/core/src/kernel/node-restack-transaction.ts +210 -0
  101. package/packages/core/src/kernel/node-restack.ts +274 -0
  102. package/packages/core/src/kernel/read-facts.ts +704 -0
  103. package/packages/core/src/kernel/reconcile-head.ts +51 -0
  104. package/packages/core/src/kernel/reconcile-restack.ts +560 -0
  105. package/packages/core/src/kernel/reducer-registry.ts +153 -3
  106. package/packages/core/src/kernel/reducer.ts +121 -6
  107. package/packages/core/src/kernel/repository-attention.ts +179 -0
  108. package/packages/core/src/kernel/service-facts.ts +135 -0
  109. package/packages/core/src/kernel/snapshot-parent.ts +33 -0
  110. package/packages/core/src/kernel/terminal-guard.ts +133 -0
  111. package/packages/core/src/ontology.ts +9 -1
  112. package/packages/core/src/operator/annotations.ts +12 -0
  113. package/packages/core/src/operator/control.ts +11 -6
  114. package/packages/core/src/operator/index.ts +6 -0
  115. package/packages/core/src/operator/landings.ts +35 -0
  116. package/packages/core/src/operator/presentation.ts +1 -0
  117. package/packages/core/src/operator/protocol.ts +12 -3
  118. package/packages/core/src/operator/query.ts +40 -21
  119. package/packages/core/src/operator/read-view.ts +4 -13
  120. package/packages/core/src/operator/registry.ts +48 -19
  121. package/packages/core/src/operator/requests.ts +3 -22
  122. package/packages/core/src/operator/sandbox.ts +10 -9
  123. package/packages/core/src/operator/tickets.ts +57 -11
  124. package/packages/core/src/plugin-sdk/index.ts +13 -0
  125. package/packages/core/src/ports/forge/contract.ts +91 -3
  126. package/packages/core/src/ports/forge/fake.ts +142 -4
  127. package/packages/core/src/ports/forge/git-rewrite.ts +232 -0
  128. package/packages/core/src/ports/forge/github-readiness.ts +421 -0
  129. package/packages/core/src/ports/forge/github.ts +631 -35
  130. package/packages/core/src/ports/forge/local-git.ts +119 -5
  131. package/packages/core/src/ports/forge/readiness.ts +99 -0
  132. package/packages/core/src/ports/tickets/contract.ts +41 -0
  133. package/packages/core/src/ports/tickets/create.ts +8 -1
  134. package/packages/core/src/ports/tickets/fake.ts +32 -1
  135. package/packages/core/src/ports/tickets/file-codec.ts +7 -0
  136. package/packages/core/src/ports/tickets/file.ts +18 -1
  137. package/packages/core/src/ports/tickets/hosted-index.ts +2 -0
  138. package/packages/core/src/ports/tickets/linear.ts +124 -4
  139. package/packages/core/src/ports/tickets/migration.ts +25 -1
  140. package/packages/core/src/ports/tickets/operations.ts +7 -2
  141. package/packages/core/src/ports/tickets/remote-protocol.ts +19 -0
  142. package/packages/core/src/ports/tickets/remote.ts +10 -1
  143. package/packages/core/src/ports/tickets/revision.ts +3 -1
  144. package/packages/core/src/ports/tickets/tree.ts +148 -0
  145. package/packages/core/src/ports/tickets/update.ts +15 -2
  146. package/packages/core/src/ports/types.ts +135 -5
  147. package/packages/core/src/ports/workspace/git-worktree.ts +2 -1
  148. package/packages/core/src/ports/workspace/provider-capabilities.ts +1 -1
  149. package/packages/core/src/ports/workspace/vercel-sandbox.ts +60 -3
  150. package/packages/core/src/processes/build-child.ts +6 -6
  151. package/packages/core/src/processes/build-runner.ts +586 -87
  152. package/packages/core/src/processes/dispatch-failure-budget.ts +5 -4
  153. package/packages/core/src/processes/dispatcher-selectors.ts +15 -9
  154. package/packages/core/src/processes/dispatcher.ts +2239 -357
  155. package/packages/core/src/processes/execution-no-progress.ts +46 -1
  156. package/packages/core/src/processes/harvest-execution-state.ts +15 -9
  157. package/packages/core/src/processes/harvest-runner.ts +34 -70
  158. package/packages/core/src/processes/harvest.ts +38 -15
  159. package/packages/core/src/processes/infrastructure-failure-budget.ts +13 -15
  160. package/packages/core/src/processes/orchestrator-tick.ts +2 -2
  161. package/packages/core/src/processes/publication-loss.ts +35 -21
  162. package/packages/core/src/processes/publication-state.ts +30 -5
  163. package/packages/core/src/processes/repo-view.ts +244 -1130
  164. package/packages/core/src/processes/tick-publication.ts +110 -0
  165. package/packages/core/src/store/adapter.ts +23 -0
  166. package/packages/core/src/store/build-scope.ts +35 -0
  167. package/packages/core/src/store/contract.ts +909 -0
  168. package/packages/core/src/store/digest.ts +52 -4
  169. package/packages/core/src/store/local/schema.ts +26 -13
  170. package/packages/core/src/store/local/store.ts +296 -55
  171. package/packages/core/src/store/memory.ts +181 -30
  172. package/packages/core/src/store/phase-session.ts +34 -0
  173. package/packages/core/src/store/reduced-reads.ts +240 -0
  174. package/packages/core/src/store/reducer-value-shapes.json +18323 -0
  175. package/packages/core/src/store/reducer-value-validation.ts +96 -0
  176. package/packages/core/src/store/reducer-values.ts +145 -0
  177. package/packages/core/src/store/remote/client.ts +85 -0
  178. package/packages/core/src/store/remote/compatibility.ts +21 -9
  179. package/packages/core/src/store/remote/index.ts +19 -0
  180. package/packages/core/src/store/remote/protocol.ts +27 -0
  181. package/packages/core/src/store/remote/version.ts +3 -1
  182. package/packages/core/src/store/repo-state-events.ts +3 -1
  183. package/packages/core/src/store/repo-tick.ts +72 -0
  184. package/packages/core/src/store/session-handle.ts +34 -0
  185. package/packages/core/src/store/snapshots.ts +10 -0
  186. package/packages/core/src/store/subscribe.ts +1 -1
  187. package/packages/core/src/store/ticket-assets.ts +4 -4
  188. package/packages/core/src/store/types.ts +71 -6
  189. package/packages/core/src/testing/dispatch-read-budget.ts +215 -0
  190. package/packages/core/src/testing/index.ts +10 -0
  191. package/packages/core/src/testing/reduced-read-budget.ts +502 -0
  192. package/packages/core/src/testing/refresh-scheduler.ts +32 -0
  193. package/skills/guide/SKILL.md +116 -62
  194. package/skills/guide/references/integration-tree.md +117 -0
  195. package/skills/guide/references/landing-policy.md +150 -0
  196. package/skills/guide/references/migration-1.0.md +68 -0
  197. package/skills/guide/references/operator-api.md +13 -6
  198. package/skills/guide/references/plugin-authoring.md +34 -0
  199. package/skills/guide/references/remote-store-protocol.md +114 -9
  200. package/skills/guide/references/remote-ticket-protocol.md +26 -5
  201. package/skills/guide/references/restacking.md +79 -0
  202. package/skills/guide/references/setup.md +49 -6
  203. package/skills/guide/references/ticket-dependencies.md +69 -0
  204. package/skills/guide/references/ticket-migration.md +8 -0
  205. package/skills/operate/SKILL.md +55 -5
  206. package/skills/reconcile/SKILL.md +55 -48
  207. package/skills/spec/SKILL.md +67 -1
  208. package/skills/tickets/SKILL.md +55 -2
  209. package/templates/autobuild.toml +3 -0
  210. package/templates/hosted-service/package.json +2 -2
@@ -1,3 +1,11 @@
1
+ import { awaitsConsent } from '../kernel/landing-readiness'
2
+ import { leafRestackReducer, type LeafRestackState } from '../kernel/leaf-restack-state'
3
+ import { consentReady, consentIdentity } from './attention'
4
+ import { readBuildDigests } from '../store/reduced-reads'
5
+ import { readBuildStreamSeed } from '../store/reduced-reads'
6
+ import { buildStreamSeedReducer, type BuildStreamSeed } from '../kernel/read-facts'
7
+ import { historicalBuildState } from './historical-build-state'
8
+ import { currentDeferralObservationReducer, type DeferralLedger } from '../kernel/auto-merge'
1
9
  /**
2
10
  * `ab wait` — the blocking sibling of `ab watch` (§8.2, §7.2). It follows the
3
11
  * event logs of one, several, or all active builds in the repository and
@@ -31,11 +39,11 @@
31
39
  * Read-only: it appends no event, claims or renews no lease, and creates no
32
40
  * build or repository record. Output carries no ANSI, ever, in either form.
33
41
  */
34
- import { buildNeedsAttention, mergeDecision, mergeIdentity } from './attention'
42
+ import { mergeDecisionFromFacts, mergeIdentity } from './attention'
35
43
  import type { AbEvent } from '../events/catalog'
36
44
  import type { EventType } from '../events/payloads'
37
45
  import type { BuildState, PrLifecycle } from '../kernel/reducer'
38
- import { reduceBuild } from '../kernel/reducer'
46
+ import { buildReducer, reduceBuild } from '../kernel/reducer'
39
47
  import { isPhase, type BuildStatus, type Phase } from '../ontology'
40
48
  import type { Exec } from '../ports/workspace/git-worktree'
41
49
  import {
@@ -68,13 +76,19 @@ export const WAIT_USAGE =
68
76
  /** Default poll cadence and wait horizon — identical to `ab watch`. */
69
77
  const DEFAULT_TIMEOUT_MS = 30 * 60 * 1000
70
78
 
71
- const NONTERMINAL_STATUSES: readonly BuildStatus[] = ['queued', 'running', 'paused', 'blocked']
79
+ const NONTERMINAL_STATUSES: readonly BuildStatus[] = [
80
+ 'queued',
81
+ 'running',
82
+ 'collecting',
83
+ 'paused',
84
+ 'blocked',
85
+ ]
72
86
  const TERMINAL_STATUSES: readonly BuildStatus[] = ['done', 'aborted']
73
87
 
74
88
  /** The `--for` condition vocabulary, in the order the help documents it. */
75
89
  const CONDITION_VOCABULARY =
76
- 'blocked, paused, running, done, aborted, terminal, pr=open, pr=merged, pr=closed, ' +
77
- 'pr=conflicted, phase=<phase> (a core phase or verify:<step>), attention'
90
+ 'blocked, paused, running, collecting, done, aborted, terminal, pr=open, pr=merged, pr=closed, ' +
91
+ 'consent, node-landed, restack-conflicted, pr=conflicted, phase=<phase> (a core phase or verify:<step>), attention'
78
92
 
79
93
  // ── Conditions ───────────────────────────────────────────────────────────────
80
94
 
@@ -84,6 +98,7 @@ type ConditionBody =
84
98
  | { kind: 'pr'; prState: PrLifecycle }
85
99
  | { kind: 'phase'; phase: Phase }
86
100
  | { kind: 'attention' }
101
+ | { kind: 'consent' | 'node-landed' | 'restack-conflicted' }
87
102
 
88
103
  interface WaitCondition {
89
104
  /** The original `--for` value — what a matching record's `condition` names. */
@@ -119,6 +134,8 @@ export function parseConditions(
119
134
  ` — ${WAIT_USAGE}`,
120
135
  )
121
136
  const conditions = forValues.map((value): WaitCondition => {
137
+ if (value === 'consent' || value === 'node-landed' || value === 'restack-conflicted')
138
+ return { text: value, cond: { kind: value } }
122
139
  if (value === 'terminal') return { text: value, cond: { kind: 'terminal' } }
123
140
  if (value === 'attention') return { text: value, cond: { kind: 'attention' } }
124
141
  if (value.startsWith('pr=')) {
@@ -143,6 +160,7 @@ export function parseConditions(
143
160
  status === 'blocked' ||
144
161
  status === 'paused' ||
145
162
  status === 'running' ||
163
+ status === 'collecting' ||
146
164
  status === 'done' ||
147
165
  status === 'aborted'
148
166
  ) {
@@ -156,8 +174,30 @@ export function parseConditions(
156
174
  return { conditions, globs }
157
175
  }
158
176
 
177
+ function attentionFrom(
178
+ input: AbEvent[] | { state: BuildState; deferral: DeferralLedger; restack?: LeafRestackState },
179
+ state?: BuildState,
180
+ ) {
181
+ return Array.isArray(input)
182
+ ? mergeDecisionFromFacts(
183
+ state ?? reduceBuild(input),
184
+ currentDeferralObservationReducer.reduce(input),
185
+ new Date(0),
186
+ )
187
+ : mergeDecisionFromFacts(input.state, input.deferral, new Date(0))
188
+ }
189
+
159
190
  /** Does one condition body hold over a reduction? Attention includes current escalation and merge state. */
160
- function stateSatisfies(cond: ConditionBody, state: BuildState, events: AbEvent[] = []): boolean {
191
+ function stateSatisfies(
192
+ cond: ConditionBody,
193
+ state: BuildState,
194
+ events:
195
+ | AbEvent[]
196
+ | { state: BuildState; deferral: DeferralLedger; restack?: LeafRestackState } = [],
197
+ ): boolean {
198
+ const restack = Array.isArray(events)
199
+ ? leafRestackReducer.reduce(events)
200
+ : (events.restack ?? { gates: {} })
161
201
  switch (cond.kind) {
162
202
  case 'status':
163
203
  return state.status === cond.status
@@ -167,8 +207,20 @@ function stateSatisfies(cond: ConditionBody, state: BuildState, events: AbEvent[
167
207
  return state.prState === cond.prState
168
208
  case 'phase':
169
209
  return (state.phase ?? null) === cond.phase
210
+ case 'consent':
211
+ return awaitsConsent(state, restack)
212
+ case 'node-landed':
213
+ return !!state.integration?.landing
214
+ case 'restack-conflicted':
215
+ return !!state.nodeRestack?.conflict || !!(restack.active && restack.replay?.conflicted)
170
216
  case 'attention':
171
- return buildNeedsAttention(events, state)
217
+ return (
218
+ awaitsConsent(state, restack) ||
219
+ !!state.nodeRestack?.conflict ||
220
+ !!(restack.active && restack.replay?.conflicted) ||
221
+ (state.openEscalations?.length ?? 0) > 0 ||
222
+ attentionFrom(events, state) !== null
223
+ )
172
224
  }
173
225
  }
174
226
 
@@ -180,7 +232,9 @@ function stateSatisfies(cond: ConditionBody, state: BuildState, events: AbEvent[
180
232
  export function matchState(
181
233
  conditions: readonly WaitCondition[],
182
234
  state: BuildState,
183
- events: AbEvent[] = [],
235
+ events:
236
+ | AbEvent[]
237
+ | { state: BuildState; deferral: DeferralLedger; restack?: LeafRestackState } = [],
184
238
  ): string | null {
185
239
  for (const { text, cond } of conditions) {
186
240
  if (stateSatisfies(cond, state, events)) return text
@@ -192,16 +246,35 @@ export function matchState(
192
246
  * globs in supply order. Used on a cursor-resumed backlog, where state
193
247
  * conditions must not fire. */
194
248
  export function matchEvent(
195
- event: Pick<AbEvent, 'type'>,
249
+ event: Pick<AbEvent, 'type'> & { payload?: unknown },
196
250
  conditions: readonly WaitCondition[],
197
251
  globs: readonly EventGlob[],
198
- events: AbEvent[] = [],
252
+ events:
253
+ | AbEvent[]
254
+ | { state: BuildState; deferral: DeferralLedger; restack?: LeafRestackState } = [],
199
255
  ): string | null {
200
256
  for (const { text, cond } of conditions) {
257
+ if (cond.kind === 'node-landed' && event.type === 'integration.landed') return text
258
+ if (
259
+ cond.kind === 'restack-conflicted' &&
260
+ (event.type === 'node.item-conflicted' ||
261
+ (event.type === 'restack.replayed' &&
262
+ (event.payload as { conflicted?: boolean })?.conflicted))
263
+ )
264
+ return text
265
+ if (
266
+ cond.kind === 'consent' &&
267
+ !Array.isArray(events) &&
268
+ awaitsConsent(events.state, events.restack ?? { gates: {} })
269
+ )
270
+ return text
201
271
  if (
202
272
  cond.kind === 'attention' &&
203
273
  (isAttentionEvent(event.type) ||
204
- (events.length > 0 && mergeDecision(events, new Date(0)) !== null))
274
+ (event.type === 'restack.replayed' &&
275
+ !!(event.payload as { conflicted?: boolean })?.conflicted) ||
276
+ (!Array.isArray(events) && stateSatisfies(cond, events.state, events)) ||
277
+ attentionFrom(events) !== null)
205
278
  )
206
279
  return text
207
280
  }
@@ -218,18 +291,37 @@ export function matchEvent(
218
291
  * original string becomes the record's `condition` field.
219
292
  */
220
293
  function matchFirst(
221
- event: Pick<AbEvent, 'type'>,
294
+ event: Pick<AbEvent, 'type'> & { payload?: unknown },
222
295
  state: BuildState,
223
296
  conditions: readonly WaitCondition[],
224
297
  globs: readonly EventGlob[],
225
- events: AbEvent[] = [],
298
+ events:
299
+ | AbEvent[]
300
+ | { state: BuildState; deferral: DeferralLedger; restack?: LeafRestackState } = [],
226
301
  ): string | null {
227
302
  for (const { text, cond } of conditions) {
228
303
  if (stateSatisfies(cond, state, events)) return text
304
+ if (cond.kind === 'node-landed' && event.type === 'integration.landed') return text
305
+ if (
306
+ cond.kind === 'restack-conflicted' &&
307
+ (event.type === 'node.item-conflicted' ||
308
+ (event.type === 'restack.replayed' &&
309
+ (event.payload as { conflicted?: boolean })?.conflicted))
310
+ )
311
+ return text
312
+ if (
313
+ cond.kind === 'consent' &&
314
+ !Array.isArray(events) &&
315
+ awaitsConsent(events.state, events.restack ?? { gates: {} })
316
+ )
317
+ return text
229
318
  if (
230
319
  cond.kind === 'attention' &&
231
320
  (isAttentionEvent(event.type) ||
232
- (events.length > 0 && mergeDecision(events, new Date(0)) !== null))
321
+ (event.type === 'restack.replayed' &&
322
+ !!(event.payload as { conflicted?: boolean })?.conflicted) ||
323
+ (!Array.isArray(events) && stateSatisfies(cond, events.state, events)) ||
324
+ attentionFrom(events) !== null)
233
325
  )
234
326
  return text
235
327
  }
@@ -276,7 +368,7 @@ interface WaitStream {
276
368
  slug: string
277
369
  named: boolean
278
370
  /** The full prefix, appended in seq order — what `reduceBuild` runs over. */
279
- events: AbEvent[]
371
+ seed: BuildStreamSeed
280
372
  lastSeq: number
281
373
  status: BuildStatus
282
374
  }
@@ -355,15 +447,17 @@ export async function abWait(opts: AbWaitOpts): Promise<number> {
355
447
  * exactly one condition-satisfying record; the exit-2 record carries
356
448
  * `condition` null so the caller can see how the build ended. */
357
449
  const finish = (condition: string | null, event: AbEvent, state: BuildState): number => {
450
+ const seed = streams.get(event.build)!.seed
451
+ const consent =
452
+ condition === 'consent' || condition === 'attention' ? consentReady(seed, now()) : null
358
453
  const observation =
359
- condition === 'attention' && event.type !== 'escalation.raised'
360
- ? mergeDecision(
361
- streams.get(event.build)?.events.filter((e) => e.seq <= event.seq) ?? [],
362
- now(),
363
- state,
364
- )
365
- : null
366
- if (observation) deliveredAttention.add(mergeIdentity(event.build, observation.pr.number))
454
+ consent ??
455
+ (condition === 'attention' && event.type !== 'escalation.raised'
456
+ ? mergeDecisionFromFacts(state, seed.deferral, now())
457
+ : null)
458
+ if (consent) deliveredAttention.add(consentIdentity(event.build, consent.position))
459
+ else if (observation && 'pr' in observation)
460
+ deliveredAttention.add(mergeIdentity(event.build, observation.pr.number))
367
461
  const record = {
368
462
  ...(observation
369
463
  ? projectAttentionRecord(observation, state, encodeCurrent(), { build: event.build })
@@ -394,22 +488,41 @@ export async function abWait(opts: AbWaitOpts): Promise<number> {
394
488
  if (satisfied === null) satisfied = { event, state, condition }
395
489
  }
396
490
 
397
- const eventConditions = (event: AbEvent, prefix: AbEvent[]): readonly WaitCondition[] => {
398
- if (event.type === 'escalation.raised') return conditions
399
- const observation = mergeDecision(prefix, now())
491
+ const eventConditions = (event: AbEvent, seed: BuildStreamSeed): readonly WaitCondition[] => {
492
+ if (
493
+ event.type === 'escalation.raised' ||
494
+ event.type === 'integration.landed' ||
495
+ event.type === 'node.item-conflicted' ||
496
+ (event.type === 'restack.replayed' && event.payload.conflicted)
497
+ )
498
+ return conditions
499
+ if (
500
+ seed.consentEpisode &&
501
+ !deliveredAttention.has(consentIdentity(event.build, seed.consentEpisode.seq))
502
+ )
503
+ return conditions
504
+ const observation = mergeDecisionFromFacts(
505
+ buildReducer.finish(seed.build),
506
+ seed.deferral,
507
+ now(),
508
+ )
400
509
  return observation &&
401
510
  !deliveredAttention.has(mergeIdentity(event.build, observation.pr.number))
402
511
  ? conditions
403
512
  : conditions.filter(({ cond }) => cond.kind !== 'attention')
404
513
  }
405
514
 
406
- const registerStream = (slug: string, events: AbEvent[], named: boolean): WaitStream => {
407
- const state = reduceBuild(events)
515
+ const registerStream = (
516
+ slug: string,
517
+ current: { value: BuildStreamSeed; cursor: number },
518
+ named: boolean,
519
+ ): WaitStream => {
520
+ const state = buildReducer.finish(current.value.build)
408
521
  const stream: WaitStream = {
409
522
  slug,
410
523
  named,
411
- events,
412
- lastSeq: events.at(-1)?.seq ?? 0,
524
+ seed: current.value,
525
+ lastSeq: current.cursor,
413
526
  status: state.status,
414
527
  }
415
528
  streams.set(slug, stream)
@@ -421,10 +534,14 @@ export async function abWait(opts: AbWaitOpts): Promise<number> {
421
534
  * already satisfying when it becomes targeted ends the wait with its
422
535
  * latest event. Not cursor-relative — `--since` does not suppress it. */
423
536
  const checkRegistrationState = (stream: WaitStream): void => {
424
- const state = reduceBuild(stream.events)
425
- const condition = matchState(conditions, state, stream.events)
426
- if (condition !== null && stream.events.length > 0) {
427
- match(stream.events.at(-1)!, state, condition)
537
+ const state = buildReducer.finish(stream.seed.build)
538
+ const condition = matchState(conditions, state, {
539
+ state,
540
+ deferral: stream.seed.deferral,
541
+ restack: stream.seed.restack,
542
+ })
543
+ if (condition !== null && state.lastEvent !== undefined) {
544
+ match(state.lastEvent!, state, condition)
428
545
  }
429
546
  }
430
547
  /**
@@ -438,42 +555,45 @@ export async function abWait(opts: AbWaitOpts): Promise<number> {
438
555
  if (streams.has(slug)) return
439
556
  const record = await store.getBuild(slug)
440
557
  if (record === null) return
441
- // AUT-545: this full-log read is deliberate, accepted growth — the
442
- // retained prefix is consumed at full replay depth by every later
443
- // reduction (approvals, findings, answered escalations, PR facts,
444
- // observations), so bounding it would need reducer-partition store
445
- // machinery out of AUT-545's scope. Required for `--since` replay and
446
- // the registration state check over the full reduction.
447
- const events = await store.getEvents(slug)
448
- const stream = registerStream(slug, events, slugs.includes(slug))
558
+ const current = await readBuildStreamSeed(store, slug)
559
+ const stream = registerStream(slug, current, slugs.includes(slug))
449
560
  if (resumeSeq !== undefined) {
561
+ stream.seed = await historicalBuildState(store, slug, resumeSeq, current)
450
562
  stream.lastSeq = resumeSeq
451
563
  positions[slug] = resumeSeq
452
- // Replay the backlog into a scratch prefix: each evaluated state is
453
- // the reduction of the prefix ENDING at that event, never the
454
- // reduction of the full loaded prefix.
455
- const replay: AbEvent[] = events.filter((event) => event.seq <= resumeSeq)
456
564
  if (resumedCursor?.deliveredAttention === undefined) {
457
- const historical: AbEvent[] = []
458
- for (const event of replay) {
459
- historical.push(event)
460
- const observation = mergeDecision(historical, now())
461
- if (observation) deliveredAttention.add(mergeIdentity(slug, observation.pr.number))
462
- }
565
+ for (const pr of stream.seed.mergeAttentionPrs)
566
+ deliveredAttention.add(mergeIdentity(slug, pr))
463
567
  }
464
- for (const event of events) {
465
- if (event.seq <= resumeSeq) continue
466
- replay.push(event)
568
+ for (const event of await store.getEvents(slug, resumeSeq)) {
569
+ stream.seed = buildStreamSeedReducer.advance(stream.seed, [event])
467
570
  stream.lastSeq = event.seq
468
571
  positions[slug] = event.seq
469
- const condition = matchEvent(event, eventConditions(event, replay), globs, replay)
572
+ const state = buildReducer.finish(stream.seed.build)
573
+ const condition = matchEvent(event, eventConditions(event, stream.seed), globs, {
574
+ state,
575
+ deferral: stream.seed.deferral,
576
+ restack: stream.seed.restack,
577
+ })
470
578
  if (condition !== null) {
471
- match(event, reduceBuild(replay), condition)
579
+ match(event, state, condition)
472
580
  break
473
581
  }
474
582
  }
475
583
  }
476
- if (satisfied === null) checkRegistrationState(stream)
584
+ if (satisfied === null) {
585
+ // Registration checks current state independently from historical replay.
586
+ const latest = buildReducer.finish(current.value.build)
587
+ const condition = matchState(conditions, latest, {
588
+ state: latest,
589
+ deferral: current.value.deferral,
590
+ restack: current.value.restack,
591
+ })
592
+ if (condition !== null && latest.lastEvent) {
593
+ stream.seed = current.value
594
+ match(latest.lastEvent, latest, condition)
595
+ }
596
+ }
477
597
  }
478
598
 
479
599
  /** Builds discovery read in full and found terminal; never re-read. */
@@ -488,33 +608,20 @@ export async function abWait(opts: AbWaitOpts): Promise<number> {
488
608
  * build to appear.
489
609
  */
490
610
  const discoverBuilds = async (): Promise<void> => {
491
- // AUT-545: the terminal filter is the AUT-487 digest, not a full-log read —
492
- // one flat batch read replaces a full getEvents per candidate build, per
493
- // discovery pass; `terminal` is reduceBuild's exact terminal rule
494
- // (store/digest.ts, pinned by contract). The per-build full read below
495
- // remains deliberate for builds the digest cannot retire: its retained
496
- // prefix is consumed at full replay depth by every later reduction
497
- // (approvals, findings, answered escalations, PR facts, observations), so
498
- // bounding it would need reducer-partition store machinery out of
499
- // AUT-545's scope. The reduceBuild re-check stays: it is the
500
- // legacy-record fallback (a record whose `repo` predates origin
501
- // normalization is in `mine` via `repoOrigin` but absent from the digest
502
- // map, which keys on `record.repo`) and it keeps this filter unable to
503
- // diverge from reduceBuild.
504
- const digests = await store.getRepoBuildDigests(repo)
611
+ const digests = await readBuildDigests(store, repo)
505
612
  for (const record of (await store.listBuilds()).filter(mine)) {
506
613
  if (streams.has(record.slug) || retired.has(record.slug)) continue
507
614
  const digest = digests.get(record.slug)
508
615
  if (digest !== undefined && digest.terminal !== undefined) continue
509
- const events = await store.getEvents(record.slug)
510
- const status = reduceBuild(events).status
616
+ const current = await readBuildStreamSeed(store, record.slug)
617
+ const status = buildReducer.finish(current.value.build).status
511
618
  if (!NONTERMINAL_STATUSES.includes(status)) {
512
619
  // Terminal is permanent (reduceBuild never clears it): a legacy
513
620
  // record the digest cannot retire is read once, not every pass.
514
621
  retired.add(record.slug)
515
622
  continue
516
623
  }
517
- const stream = registerStream(record.slug, events, false)
624
+ const stream = registerStream(record.slug, current, false)
518
625
  if (satisfied === null) checkRegistrationState(stream)
519
626
  }
520
627
  }
@@ -525,18 +632,16 @@ export async function abWait(opts: AbWaitOpts): Promise<number> {
525
632
  ): Promise<void> => {
526
633
  const fresh = await store.getEvents(stream.slug, stream.lastSeq, readOpts)
527
634
  for (const event of fresh) {
528
- stream.events.push(event)
635
+ stream.seed = buildStreamSeedReducer.advance(stream.seed, [event])
529
636
  stream.lastSeq = event.seq
530
637
  positions[stream.slug] = event.seq
531
- const state = reduceBuild(stream.events)
638
+ const state = buildReducer.finish(stream.seed.build)
532
639
  stream.status = state.status
533
- const condition = matchFirst(
534
- event,
640
+ const condition = matchFirst(event, state, eventConditions(event, stream.seed), globs, {
535
641
  state,
536
- eventConditions(event, stream.events),
537
- globs,
538
- stream.events,
539
- )
642
+ deferral: stream.seed.deferral,
643
+ restack: stream.seed.restack,
644
+ })
540
645
  if (condition !== null) {
541
646
  match(event, state, condition)
542
647
  return
@@ -556,21 +661,19 @@ export async function abWait(opts: AbWaitOpts): Promise<number> {
556
661
  /** The exit-2 record: among the named builds' terminal events, the one
557
662
  * with the latest `ts` (ties broken by slug order) — the event that made
558
663
  * the final named build terminal. */
559
- const pickTerminalRecord = (): { event: AbEvent; state: BuildState } => {
664
+ const pickTerminalRecord = async (): Promise<{ event: AbEvent; state: BuildState }> => {
560
665
  let best: { event: AbEvent; state: BuildState; slug: string } | null = null
561
666
  for (const slug of slugs) {
562
667
  const stream = streams.get(slug)
563
668
  if (stream === undefined) continue
564
- const terminalEvent = [...stream.events]
565
- .reverse()
566
- .find((event) => event.type === 'build.completed' || event.type === 'build.aborted')
669
+ const terminalEvent = stream.seed.terminalEvent
567
670
  if (terminalEvent === undefined) continue
568
671
  if (
569
672
  best === null ||
570
673
  terminalEvent.ts > best.event.ts ||
571
674
  (terminalEvent.ts === best.event.ts && slug < best.slug)
572
675
  ) {
573
- best = { event: terminalEvent, state: reduceBuild(stream.events), slug }
676
+ best = { event: terminalEvent, state: buildReducer.finish(stream.seed.build), slug }
574
677
  }
575
678
  }
576
679
  // A named build is terminal only through its terminal event, so
@@ -647,7 +750,7 @@ export async function abWait(opts: AbWaitOpts): Promise<number> {
647
750
  } else if (namedAllTerminal()) {
648
751
  // Decision 5, start form: every named build is already terminal and
649
752
  // no condition held — show how the build ended.
650
- const terminal = pickTerminalRecord()
753
+ const terminal = await pickTerminalRecord()
651
754
  return finish(null, terminal.event, terminal.state)
652
755
  }
653
756
 
@@ -664,7 +767,7 @@ export async function abWait(opts: AbWaitOpts): Promise<number> {
664
767
  if (aborted()) break
665
768
  await tick()
666
769
  if (!hasMatch() && namedAllTerminal()) {
667
- const terminal = pickTerminalRecord()
770
+ const terminal = await pickTerminalRecord()
668
771
  return finish(null, terminal.event, terminal.state)
669
772
  }
670
773
  }
@@ -752,7 +855,7 @@ export async function abWait(opts: AbWaitOpts): Promise<number> {
752
855
  // conditions live — so after the quiesce drain this decision re-reads
753
856
  // the same live predicates (`hasMatch`, `namedAllTerminal`, `aborted`,
754
857
  // the deadline) to pick the exit.
755
- const terminal = pickTerminalRecord()
858
+ const terminal = await pickTerminalRecord()
756
859
  return finish(null, terminal.event, terminal.state)
757
860
  }
758
861
  if (aborted()) {