@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,16 +1,15 @@
1
1
  /**
2
2
  * The operator read paths' repository view: every poll-driven route (dashboard,
3
3
  * repository and harvest status, build listings) derives its state from a
4
- * `RepoViewStore` cold-started from stored reducer snapshots and advanced with
5
- * delta reads, so a poll's event-row cost follows the events appended since the
6
- * last one, never the history, the open tabs, or the watchers.
4
+ * `RepoViewStore` backed by persisted reduced values. Each pass refreshes
5
+ * repository facts and metadata without hydrating raw history.
7
6
  *
8
7
  * Passes are single-flight per `(store, repo)` within one serving process: at
9
8
  * most one pass runs at a time, and requests arriving while it runs join the
10
9
  * one trailing pass (each needs state at least as new as its arrival, which the
11
10
  * running pass cannot promise). That trailing pass restores from the snapshot
12
- * the previous pass persisted, so N concurrent pollers cost one delta read per
13
- * pass, not N. The coordination is scheduling inside this process; every datum
11
+ * the previous pass persisted for builds, so N concurrent pollers cost one
12
+ * build delta read per pass, not N. The coordination is scheduling inside this process; every datum
14
13
  * still comes from durable state, and a second serving process simply reads the
15
14
  * same delta once itself (duplicate reads are bounded by the process count).
16
15
  */
@@ -58,14 +57,6 @@ async function pass(store: BuildStore, repo: string, waiters: Waiter[]): Promise
58
57
  return
59
58
  }
60
59
  await Promise.all(waiters.map((waiter) => waiter.run(view).then(waiter.resolve, waiter.reject)))
61
- // Leave the advanced state behind for the next pass (this process's or
62
- // another's). `held` also writes a settled build read for a listing. The
63
- // cache never fails a read.
64
- try {
65
- await view.persistSnapshots({ force: true, held: true })
66
- } catch {
67
- // A cache: the next pass retries.
68
- }
69
60
  }
70
61
 
71
62
  async function drain(store: BuildStore, repo: string, flight: Flight): Promise<void> {
@@ -1,3 +1,6 @@
1
+ import { listOperatorLandings } from './landings'
2
+ import { getOperatorTicketTree } from './tickets'
3
+ import { openOperatorTickets } from './tickets'
1
4
  import { TicketRevisionConflict, TicketRevisionUnsupported } from '../ports/tickets/revision'
2
5
  /**
3
6
  * The agent tool registry — one typed table that is the single source of
@@ -262,17 +265,9 @@ const buildControlInput = z.strictObject({
262
265
  repo: repoField,
263
266
  slug: slugField,
264
267
  action: z
265
- .enum([
266
- 'pause',
267
- 'cancel-pause',
268
- 'resume',
269
- 'auto-merge-on',
270
- 'auto-merge-off',
271
- 'abort',
272
- 'discard',
273
- ])
268
+ .enum(['pause', 'cancel-pause', 'resume', 'land', 'hold', 'abort', 'discard'])
274
269
  .describe(
275
- 'Control to request: "pause" (or "cancel-pause" to revoke a pending pause), "resume" a paused build, "auto-merge-on"/"auto-merge-off" for native auto-merge — or the destructive "abort" (cancels the build, cleans up its PR and workspace, returns the ticket to triage) and "discard" (dequeue a queued build).',
270
+ 'Control to request: "pause" (or "cancel-pause" to revoke a pending pause), "resume" a paused build, "land" for landing consent or "hold" to withdraw consent — or the destructive "abort" (cancels the build, cleans up its PR and workspace, returns the ticket to triage) and "discard" (dequeue a queued build).',
276
271
  ),
277
272
  })
278
273
 
@@ -282,11 +277,7 @@ const repositoryStatusInput = z.strictObject({ repo: repoField })
282
277
 
283
278
  const repositorySettingsInput = z.strictObject({
284
279
  repo: repoField,
285
- setting: z
286
- .enum(['intake', 'auto-merge-default'])
287
- .describe(
288
- '"intake" gates whether new tickets may be claimed; "auto-merge-default" is the claim-time auto-merge default for newly claimed builds.',
289
- ),
280
+ setting: z.enum(['intake']).describe('"intake" gates whether new tickets may be claimed.'),
290
281
  enabled: z
291
282
  .boolean()
292
283
  .optional()
@@ -326,7 +317,12 @@ const ticketsCreateInput = ticketCreateRequestSchema.extend({ repo: repoField })
326
317
  const ticketsUpdateInput = ticketUpdateRequestSchema
327
318
  .extend({ repo: repoField, id: ticketIdField })
328
319
  .refine(
329
- (value) => value.title !== undefined || value.body !== undefined || value.labels !== undefined,
320
+ (value) =>
321
+ value.title !== undefined ||
322
+ value.body !== undefined ||
323
+ value.labels !== undefined ||
324
+ value.kind !== undefined ||
325
+ value.target !== undefined,
330
326
  'update must name at least one field',
331
327
  )
332
328
 
@@ -481,6 +477,25 @@ function defineTool(
481
477
  * here and a row in `operator/annotations.ts`; the contract suite proves the
482
478
  * two stay in lockstep. */
483
479
  export const TOOLS: readonly ToolEntry[] = [
480
+ defineTool(
481
+ 'landings.list',
482
+ 'List ready landings awaiting consent or held, with frozen effective targets and verification evidence.',
483
+ repositoryStatusInput,
484
+ 'JSON array ordered by slug: kind, target, branch, candidate, reason, verification and node completeness.',
485
+ async (raw, ctx) => listOperatorLandings({ store: ctx.store, ...(raw as { repo: string }) }),
486
+ ),
487
+ defineTool(
488
+ 'tickets.tree',
489
+ 'Read the exhaustive source ticket tree, including completed tickets and empty nodes.',
490
+ repositoryStatusInput,
491
+ '{root, children, diagnostics}: source metadata ancestry, distinct from frozen build targets.',
492
+ async (raw, ctx) =>
493
+ getOperatorTicketTree({
494
+ store: ctx.store,
495
+ backend: ticketsOf(ctx),
496
+ ...(raw as { repo: string }),
497
+ }),
498
+ ),
484
499
  defineTool(
485
500
  'builds.list',
486
501
  'List one repository’s builds, newest first, with effective status derived from the build log.',
@@ -560,7 +575,7 @@ export const TOOLS: readonly ToolEntry[] = [
560
575
  ),
561
576
  defineTool(
562
577
  'builds.control',
563
- 'Request a durable control for one active build: pause/cancel-pause, resume, auto-merge on/off — or the destructive abort and discard.',
578
+ 'Request a durable control for one active build: pause/cancel-pause, resume, hold or land — or the destructive abort and discard. Land requires a ready candidate; hold withdraws landing consent.',
564
579
  buildControlInput,
565
580
  'Either {kind: "command", slug, command, event} for an accepted control, or {kind: "answer-required", slug, escalationIds} when a resume needs its open escalations answered first.',
566
581
  async (raw, ctx) => {
@@ -578,6 +593,12 @@ export const TOOLS: readonly ToolEntry[] = [
578
593
  : { kind: input.action }
579
594
  return controlBuild({
580
595
  store: ctx.store,
596
+ ticketSource: async () => {
597
+ if (!ctx.tickets) throw new Error('ticket source unavailable')
598
+ return (
599
+ await openOperatorTickets({ store: ctx.store, repo: input.repo, backend: ctx.tickets })
600
+ ).source
601
+ },
581
602
  repo: input.repo,
582
603
  slug: input.slug,
583
604
  user: attributed(ctx),
@@ -597,6 +618,12 @@ export const TOOLS: readonly ToolEntry[] = [
597
618
  const { action, readTicketBody } = answerAction(input)
598
619
  return controlBuild({
599
620
  store: ctx.store,
621
+ ticketSource: async () => {
622
+ if (!ctx.tickets) throw new Error('ticket source unavailable')
623
+ return (
624
+ await openOperatorTickets({ store: ctx.store, repo: input.repo, backend: ctx.tickets })
625
+ ).source
626
+ },
600
627
  repo: input.repo,
601
628
  slug: input.slug,
602
629
  user: attributed(ctx),
@@ -763,13 +790,15 @@ export const TOOLS: readonly ToolEntry[] = [
763
790
  '{ticket, blockers, build}: the created ticket and its (usually empty) dependency states.',
764
791
  async (raw, ctx) => {
765
792
  const input = raw as z.infer<typeof ticketsCreateInput>
766
- const { repo, title, body, labels, state, blockedBy } = input
793
+ const { repo, title, body, labels, state, blockedBy, kind, target } = input
767
794
  return mutateOperatorTicket({
768
795
  store: ctx.store,
769
796
  repo,
770
797
  backend: ticketsOf(ctx),
771
798
  operation: {
772
799
  kind: 'create',
800
+ ...(kind !== undefined ? { ticketKind: kind } : {}),
801
+ ...(target !== undefined ? { target } : {}),
773
802
  title,
774
803
  body,
775
804
  ...(labels !== undefined ? { labels } : {}),
@@ -781,7 +810,7 @@ export const TOOLS: readonly ToolEntry[] = [
781
810
  ),
782
811
  defineTool(
783
812
  'tickets.update',
784
- 'Partially replace a ticket’s editable fields (title, body, labels); state is never changed here.',
813
+ 'Partially replace a ticket’s editable fields (title, body, labels, kind, target); state is never changed here.',
785
814
  ticketsUpdateInput,
786
815
  '{ticket, blockers, build}: the updated ticket.',
787
816
  async (raw, ctx) => {
@@ -1,16 +1,4 @@
1
- /**
2
- * The route-level glue the operator HTTP server applies around the shared
3
- * services, re-derived for the agent tool registry (operator/registry.ts).
4
- *
5
- * This module is registry-owned on purpose: the operator API routes were
6
- * declared out of scope for the registry ticket, so the parity between a
7
- * tool and its route is *proven* by the contract suite
8
- * (packages/hosted-store-service/src/registry.contract.test.ts) rather than
9
- * obtained by extraction. Every refusal text here is load-bearing — it must
10
- * stay byte-identical to the hosted package's `operator-server.ts` so the
11
- * suite's full-body comparisons hold.
12
- */
13
- import { reduceBuild } from '../kernel/reducer'
1
+ import { readBuildFacts } from '../store/reduced-reads'
14
2
  import { effectiveStatus } from '../cli/dashboard/model'
15
3
  import { BuildControlError, type BuildControlAction } from '../cli/build-control'
16
4
  import type { BuildStore } from '../store/types'
@@ -69,18 +57,11 @@ export async function controlPrechecks(
69
57
  store: BuildStore,
70
58
  repo: string,
71
59
  slug: string,
72
- action:
73
- | 'pause'
74
- | 'cancel-pause'
75
- | 'resume'
76
- | 'auto-merge-on'
77
- | 'auto-merge-off'
78
- | 'abort'
79
- | 'discard',
60
+ action: 'pause' | 'cancel-pause' | 'resume' | 'land' | 'hold' | 'abort' | 'discard',
80
61
  ): Promise<void> {
81
62
  await requireRouteBuild(store, repo, slug)
82
63
  if (action === 'pause' || action === 'cancel-pause') {
83
- const state = reduceBuild(await store.getEvents(slug))
64
+ const state = (await readBuildFacts(store, slug)).value.state
84
65
  const display = effectiveStatus(state)
85
66
  if (action === 'pause' && display === 'pausing') {
86
67
  throw new BuildControlError(
@@ -13,7 +13,6 @@
13
13
  import { createHash } from 'node:crypto'
14
14
  import { humanActor } from '../events/envelope'
15
15
  import type { Via } from '../events/envelope'
16
- import type { RepositoryEvent } from '../events/repository'
17
16
  import type { Forge } from '../ports/types'
18
17
  import {
19
18
  SANDBOX_FORBIDDEN_ENV,
@@ -23,8 +22,7 @@ import {
23
22
  type SandboxWaitResult,
24
23
  } from '../ports/workspace/operator-sandbox'
25
24
  import type { WorkspaceProvider } from '../ports/types'
26
- import { readRepoEventsIfRecorded } from '../processes/harvest'
27
- import { sandboxStates } from '../processes/sandbox-state'
25
+ import { readRepositoryFacts } from '../store/reduced-reads'
28
26
  import { systemClock, type BuildStore, type Clock } from '../store/types'
29
27
 
30
28
  /** Hard bound on one sandbox command wait, in seconds — shared with the
@@ -253,8 +251,11 @@ export async function createOperatorSandboxService(
253
251
  }
254
252
 
255
253
  /** The operator's journal state, or undefined when no environment exists. */
256
- const operatorState = (identity: string, events: RepositoryEvent[]) =>
257
- sandboxStates(events)
254
+ const operatorState = (
255
+ identity: string,
256
+ events: import('../store/reduced-reads').RepositoryReadFacts,
257
+ ) =>
258
+ events.sandboxes
258
259
  .filter((state) => state.operator === identity && state.state !== 'released')
259
260
  .at(-1)
260
261
 
@@ -327,7 +328,7 @@ export async function createOperatorSandboxService(
327
328
  ): Promise<T> => {
328
329
  const key = `${repo}\0${identity}`
329
330
  return serialize(key, async () => {
330
- const events = await readRepoEventsIfRecorded(store, repo)
331
+ const events = (await readRepositoryFacts(store, repo)).value
331
332
  const state = operatorState(identity, events)
332
333
  const resolved = await ensureWithFacts(identity, state).catch((error: unknown) => {
333
334
  if (error instanceof SandboxOperationError) throw error
@@ -337,7 +338,7 @@ export async function createOperatorSandboxService(
337
338
  { cause: error },
338
339
  )
339
340
  })
340
- const freshEvents = await readRepoEventsIfRecorded(store, repo)
341
+ const freshEvents = (await readRepositoryFacts(store, repo)).value
341
342
  const freshState = operatorState(identity, freshEvents)
342
343
  try {
343
344
  const result = await operation(resolved, freshState)
@@ -630,7 +631,7 @@ export async function createOperatorSandboxService(
630
631
  requireRepo(input.repo)
631
632
  const key = `${repo}\0${identity}`
632
633
  await serialize(key, async () => {
633
- const events = await readRepoEventsIfRecorded(store, repo)
634
+ const events = (await readRepositoryFacts(store, repo)).value
634
635
  const state = operatorState(identity, events)
635
636
  const resolved = await capability.describe({ repo, operator: identity })
636
637
  await append(identity, 'orchestrator.sandbox.reset', {
@@ -662,7 +663,7 @@ export async function createOperatorSandboxService(
662
663
  requireRepo(input.repo)
663
664
  const key = `${repo}\0${identity}`
664
665
  await serialize(key, async () => {
665
- const events = await readRepoEventsIfRecorded(store, repo)
666
+ const events = (await readRepositoryFacts(store, repo)).value
666
667
  const state = operatorState(identity, events)
667
668
  // No environment ever provisioned: a no-op, with no fact and no
668
669
  // provider traffic beyond the pure describe.
@@ -1,7 +1,9 @@
1
+ import { updateTicketTarget } from '../kernel/node-parent'
2
+ import { projectTicketTree, compatibleTicketSource } from '../ports/tickets/tree'
3
+ import { readBuildFacts } from '../store/reduced-reads'
1
4
  import { samplePinnedAssets, preparePinnedAssets } from '../store/ticket-assets'
2
5
  import type { TicketRef } from '../ontology'
3
6
  import type { Config } from '../config/schema'
4
- import { reduceBuild } from '../kernel/reducer'
5
7
  import {
6
8
  TicketOperationError,
7
9
  changeBlockers,
@@ -18,12 +20,15 @@ import { effectiveConfig } from './query'
18
20
  export interface OperatorTicketContext {
19
21
  repo: string
20
22
  tickets: Config['tickets']
23
+ baseBranch?: string
21
24
  teamKey: string
22
25
  claimedState?: string
23
26
  createState?: string
24
27
  }
25
28
 
26
29
  export interface OperatorTicketBackend {
30
+ supportsNodeRetarget?(context: OperatorTicketContext): Promise<boolean>
31
+
27
32
  contextFor?(repo: string): Promise<OperatorTicketContext>
28
33
  sourceFor(context: OperatorTicketContext): TicketSource | Promise<TicketSource>
29
34
  statesFor(context: OperatorTicketContext, source: TicketSource): string[] | Promise<string[]>
@@ -56,6 +61,7 @@ function contextFor(config: Config, repo: string): OperatorTicketContext {
56
61
  const tickets = config.tickets
57
62
  return {
58
63
  repo,
64
+ baseBranch: config.baseBranch,
59
65
  tickets,
60
66
  teamKey: tickets.teamKey ?? '',
61
67
  ...(tickets.claimedState !== undefined ? { claimedState: tickets.claimedState } : {}),
@@ -71,7 +77,7 @@ export async function openOperatorTickets(opts: {
71
77
  const context = opts.backend.contextFor
72
78
  ? await opts.backend.contextFor(opts.repo)
73
79
  : contextFor((await effectiveConfig(opts.store, opts.repo)).config, opts.repo)
74
- return { context, source: await opts.backend.sourceFor(context) }
80
+ return { context, source: compatibleTicketSource(await opts.backend.sourceFor(context)) }
75
81
  }
76
82
 
77
83
  export async function listOperatorTickets(opts: {
@@ -141,7 +147,7 @@ async function matchingBuild(
141
147
  const projected = await Promise.all(
142
148
  matches.map(async (record) => ({
143
149
  record,
144
- state: reduceBuild(await store.getEvents(record.slug)),
150
+ state: (await readBuildFacts(store, record.slug)).value.state,
145
151
  })),
146
152
  )
147
153
  projected.sort((a, b) => {
@@ -190,6 +196,8 @@ export async function mutateOperatorTicket(opts: {
190
196
  operation:
191
197
  | {
192
198
  kind: 'create'
199
+ ticketKind?: import('../ports/types').TicketKind
200
+ target?: string | null
193
201
  title: string
194
202
  body: string
195
203
  labels?: string[]
@@ -205,15 +213,32 @@ export async function mutateOperatorTicket(opts: {
205
213
  if (operation.kind === 'update') await requireTicket(source, operation.id)
206
214
  const ticket =
207
215
  operation.kind === 'create'
208
- ? await createTicket(source, operation)
216
+ ? await createTicket(source, { ...operation, kind: operation.ticketKind })
209
217
  : operation.kind === 'update'
210
- ? (await updateTicket(
211
- source,
212
- operation.id,
213
- operation.patch,
214
- true,
215
- operation.expectedRevision,
216
- ))!
218
+ ? (await (operation.patch.target === undefined
219
+ ? updateTicket(source, operation.id, operation.patch, true, operation.expectedRevision)
220
+ : updateTicketTarget(
221
+ {
222
+ store: opts.store,
223
+ repo: opts.repo,
224
+ source,
225
+ configuredSource: context.tickets.source,
226
+ root:
227
+ context.baseBranch ??
228
+ (
229
+ await effectiveConfig(opts.store, opts.repo)
230
+ ).config.baseBranch,
231
+ supportsRetarget: async () => {
232
+ if (opts.backend.supportsNodeRetarget)
233
+ return opts.backend.supportsNodeRetarget(context)
234
+ const config = (await effectiveConfig(opts.store, opts.repo)).config
235
+ return config.forge === 'github' || config.forge === 'local-git'
236
+ },
237
+ },
238
+ operation.id,
239
+ operation.patch,
240
+ operation.expectedRevision,
241
+ )))!
217
242
  : operation.kind === 'move'
218
243
  ? await moveTicket(source, operation.id, operation.state, operation.expectedRevision)
219
244
  : await changeBlockers(
@@ -283,3 +308,24 @@ export async function createOperatorTicketState(opts: {
283
308
  )
284
309
  }
285
310
  }
311
+
312
+ /** Exhaustive source tree, including resolved tickets and listing diagnostics. */
313
+ export async function readTicketTree(source: TicketSource, root: string) {
314
+ source = compatibleTicketSource(source)
315
+ if (!source.listAll)
316
+ throw new Error(
317
+ `${source.name} cannot enumerate all tickets; use a source with listAll support`,
318
+ )
319
+ const listing = await source.listAll()
320
+ return { ...projectTicketTree(listing.tickets, root), diagnostics: listing.diagnostics }
321
+ }
322
+
323
+ export async function getOperatorTicketTree(opts: {
324
+ store: BuildStore
325
+ repo: string
326
+ backend: OperatorTicketBackend
327
+ }) {
328
+ const { context, source } = await openOperatorTickets(opts)
329
+ if (!context.baseBranch) throw new Error('Repository base branch unavailable')
330
+ return readTicketTree(source, context.baseBranch)
331
+ }
@@ -229,3 +229,16 @@ export type {
229
229
  TicketAssetMeta,
230
230
  TicketAssetSummary,
231
231
  } from '../store/ticket-assets'
232
+
233
+ export {
234
+ readReducerValue,
235
+ resolveReducerValue,
236
+ reducerValueRequestSchema,
237
+ reducerValueSchema,
238
+ } from '../store/reducer-values'
239
+ export type {
240
+ ReducerValue,
241
+ ReducerValueRequest,
242
+ ReducerValueStorage,
243
+ ValueCacheRow,
244
+ } from '../store/reducer-values'
@@ -90,6 +90,78 @@ async function pushAndOpen(harness: ForgeContractHarness): Promise<{
90
90
  */
91
91
  export function describeForgeContract(name: string, factory: ForgeContractFactory): void {
92
92
  describe(`Forge contract: ${name}`, () => {
93
+ test('optional node refs open empty without a PR and retries preserve their head', async () => {
94
+ await withForge(factory, undefined, async (h) => {
95
+ const forge = h.forge
96
+ if (!forge.createNodeBranch || !forge.compareBranches || !forge.remoteBranchSha) return
97
+ await forge.pushBranch(h.workspacePath, h.head)
98
+ const opening = await forge.remoteBranchSha(h.head)
99
+ const branch = `ab-node/contract-${crypto.randomUUID()}`
100
+ try {
101
+ expect(await forge.createNodeBranch(branch, opening)).toBe(opening)
102
+ expect(await forge.createNodeBranch(branch, opening)).toBe(opening)
103
+ expect(await forge.compareBranches(h.head, branch)).toEqual({
104
+ parentSha: opening,
105
+ headSha: opening,
106
+ ahead: 0,
107
+ })
108
+ } finally {
109
+ if (forge.deleteBranch) await forge.deleteBranch(h.workspacePath, branch)
110
+ }
111
+ })
112
+ })
113
+ test('optional retarget preserves review identity and acknowledges the new base idempotently', async () => {
114
+ await withForge(factory, undefined, async (h) => {
115
+ const forge = h.forge
116
+ if (
117
+ !forge.retargetPr ||
118
+ !forge.createNodeBranch ||
119
+ !forge.inspectLandingPr ||
120
+ !forge.remoteBranchSha
121
+ )
122
+ return
123
+ const { pr } = await pushAndOpen(h)
124
+ const branch = `ab-node/retarget-${crypto.randomUUID()}`
125
+ await forge.createNodeBranch(branch, await forge.remoteBranchSha(h.base))
126
+ try {
127
+ await forge.setAutoMerge(h.workspacePath, pr.number, false)
128
+ await forge.retargetPr(h.workspacePath, pr.number, branch)
129
+ await forge.retargetPr(h.workspacePath, pr.number, branch)
130
+ expect(await forge.inspectLandingPr(h.workspacePath, pr.number)).toMatchObject({
131
+ base: branch,
132
+ head: h.head,
133
+ open: true,
134
+ })
135
+ } finally {
136
+ await h.controls.closePr(pr.number)
137
+ await forge.deleteBranch?.(h.workspacePath, branch)
138
+ }
139
+ })
140
+ })
141
+ test('optional rewrite snapshots the authoritative head and refuses a stale lease', async () => {
142
+ await withForge(factory, undefined, async (harness) => {
143
+ const capability = harness.forge.rewrite
144
+ if (capability === undefined) return
145
+ const { remoteHead } = await pushAndOpen(harness)
146
+ expect(
147
+ await capability.snapshot({
148
+ workspacePath: harness.workspacePath,
149
+ branch: harness.head,
150
+ destinationRef: 'refs/autobuild/reconcile/contract-probe/head',
151
+ }),
152
+ ).toBe(remoteHead)
153
+ expect(
154
+ await capability.replace({
155
+ workspacePath: harness.workspacePath,
156
+ branch: harness.head,
157
+ expected: '0'.repeat(40),
158
+ sha: 'e'.repeat(40),
159
+ }),
160
+ ).toEqual({ kind: 'lease-mismatch', actual: remoteHead })
161
+ expect(await harness.controls.remoteHead(harness.head)).toBe(remoteHead)
162
+ })
163
+ })
164
+
93
165
  test('pushBranch publishes the head and openPr returns/adopts the actual PR', async () => {
94
166
  await withForge(factory, undefined, async (harness) => {
95
167
  const { pr, remoteHead } = await pushAndOpen(harness)
@@ -113,7 +185,7 @@ export function describeForgeContract(name: string, factory: ForgeContractFactor
113
185
  await withForge(factory, undefined, async (harness) => {
114
186
  const { pr } = await pushAndOpen(harness)
115
187
  await harness.controls.prepareMergeable(pr.number)
116
- expect(await harness.forge.getPrState(harness.workspacePath, pr.number)).toEqual({
188
+ expect(await harness.forge.getPrState(harness.workspacePath, pr.number)).toMatchObject({
117
189
  state: 'open',
118
190
  mergeable: true,
119
191
  })
@@ -124,7 +196,7 @@ export function describeForgeContract(name: string, factory: ForgeContractFactor
124
196
  await withForge(factory, undefined, async (harness) => {
125
197
  const { pr } = await pushAndOpen(harness)
126
198
  await harness.controls.makeConflict(pr.number)
127
- expect(await harness.forge.getPrState(harness.workspacePath, pr.number)).toEqual({
199
+ expect(await harness.forge.getPrState(harness.workspacePath, pr.number)).toMatchObject({
128
200
  state: 'open',
129
201
  mergeable: false,
130
202
  })
@@ -156,6 +228,22 @@ export function describeForgeContract(name: string, factory: ForgeContractFactor
156
228
  })
157
229
  })
158
230
 
231
+ test('identity-aware squash exposes the actual landed trailer when inspection is supported', async () => {
232
+ await withForge(factory, undefined, async (harness) => {
233
+ const { pr } = await pushAndOpen(harness)
234
+ await harness.controls.prepareMergeable(pr.number)
235
+ await harness.forge.squashMerge(harness.workspacePath, pr.number, pr.headSha, {
236
+ buildSlug: 'contract-build',
237
+ })
238
+ const sha = await harness.controls.mergeSha(pr.number)
239
+ if (harness.forge.readCommitMessage !== undefined) {
240
+ expect(await harness.forge.readCommitMessage(harness.workspacePath, sha)).toContain(
241
+ 'Autobuild-Build: contract-build',
242
+ )
243
+ }
244
+ })
245
+ })
246
+
159
247
  test('setAutoMerge acknowledges native state and is idempotent', async () => {
160
248
  await withForge(factory, { gated: true }, async (harness) => {
161
249
  if (harness.nativeAutoMerge === false) return
@@ -192,7 +280,7 @@ export function describeForgeContract(name: string, factory: ForgeContractFactor
192
280
  await expect(
193
281
  harness.forge.squashMerge(harness.workspacePath, pr.number, pr.headSha),
194
282
  ).rejects.toThrow()
195
- expect(await harness.forge.getPrState(harness.workspacePath, pr.number)).toEqual({
283
+ expect(await harness.forge.getPrState(harness.workspacePath, pr.number)).toMatchObject({
196
284
  state: 'open',
197
285
  mergeable: true,
198
286
  })