@defrex/autobuild 1.10.0 → 1.11.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 (250) hide show
  1. package/README.md +1 -1
  2. package/SPEC.md +738 -68
  3. package/bin/ab-dispatch-generation.ts +3 -1
  4. package/docs/accounts.md +2 -1
  5. package/docs/answering-blocked-builds.md +20 -0
  6. package/docs/architecture.md +155 -25
  7. package/docs/build-store.md +10 -7
  8. package/docs/cli.md +33 -1
  9. package/docs/configuration-overview.md +4 -1
  10. package/docs/configuration.md +381 -29
  11. package/docs/dispatcher.md +63 -4
  12. package/docs/forges.md +7 -0
  13. package/docs/integration-tree.md +2 -2
  14. package/docs/operating.md +4 -2
  15. package/docs/operator-api.md +134 -6
  16. package/docs/operator-surfaces.md +85 -0
  17. package/docs/optimization-nodes.md +69 -11
  18. package/docs/plugin-authoring.md +37 -1
  19. package/docs/remote-store-protocol.md +94 -13
  20. package/docs/remote-ticket-protocol.md +40 -0
  21. package/docs/restacking.md +20 -4
  22. package/docs/setup.md +14 -2
  23. package/docs/ticket-dependencies.md +10 -3
  24. package/docs/ticket-sources.md +24 -2
  25. package/docs/tickets.md +7 -1
  26. package/docs/troubleshooting.md +8 -0
  27. package/docs/verification.md +4 -3
  28. package/docs/workspaces.md +3 -0
  29. package/package.json +9 -5
  30. package/packages/core/src/cli/accounts.ts +7 -2
  31. package/packages/core/src/cli/attention.ts +3 -1
  32. package/packages/core/src/cli/binary-direct.ts +2 -0
  33. package/packages/core/src/cli/binary-lifecycle.ts +2 -1
  34. package/packages/core/src/cli/build-control.ts +79 -1
  35. package/packages/core/src/cli/context.ts +4 -0
  36. package/packages/core/src/cli/dashboard/facts.ts +49 -53
  37. package/packages/core/src/cli/dashboard/header.ts +101 -0
  38. package/packages/core/src/cli/dashboard/model.ts +31 -39
  39. package/packages/core/src/cli/dashboard/objective-row.ts +46 -14
  40. package/packages/core/src/cli/dashboard/poll.ts +178 -76
  41. package/packages/core/src/cli/dashboard/render.ts +129 -230
  42. package/packages/core/src/cli/dashboard/row-text.ts +538 -0
  43. package/packages/core/src/cli/dashboard/timeline.ts +288 -0
  44. package/packages/core/src/cli/dashboard/vocabulary.ts +188 -0
  45. package/packages/core/src/cli/dispatch-args.ts +44 -1
  46. package/packages/core/src/cli/dispatch-frontend.ts +29 -38
  47. package/packages/core/src/cli/dispatch-launch-stop.ts +18 -0
  48. package/packages/core/src/cli/dispatch-launcher.ts +20 -7
  49. package/packages/core/src/cli/dispatch-web.ts +155 -0
  50. package/packages/core/src/cli/dispatch.ts +200 -34
  51. package/packages/core/src/cli/help.ts +87 -3
  52. package/packages/core/src/cli/init-validation.ts +3 -3
  53. package/packages/core/src/cli/main.ts +70 -6
  54. package/packages/core/src/cli/mcp.ts +35 -3
  55. package/packages/core/src/cli/objective.ts +1 -1
  56. package/packages/core/src/cli/operator-preference.ts +57 -0
  57. package/packages/core/src/cli/repo-lease.ts +2 -0
  58. package/packages/core/src/cli/repo-state.ts +56 -6
  59. package/packages/core/src/cli/repository-status.ts +97 -43
  60. package/packages/core/src/cli/status.ts +82 -1
  61. package/packages/core/src/cli/terminal-restore.ts +32 -7
  62. package/packages/core/src/cli/ticket.ts +98 -36
  63. package/packages/core/src/cli/wait.ts +14 -6
  64. package/packages/core/src/cli/watch.ts +20 -4
  65. package/packages/core/src/cli/web.ts +139 -0
  66. package/packages/core/src/config/live.ts +99 -14
  67. package/packages/core/src/config/load.ts +61 -20
  68. package/packages/core/src/config/local.ts +212 -0
  69. package/packages/core/src/config/pipeline-source.ts +2 -0
  70. package/packages/core/src/config/reference.ts +18 -0
  71. package/packages/core/src/config/schema.ts +45 -10
  72. package/packages/core/src/events/catalog.ts +4 -0
  73. package/packages/core/src/events/payloads.ts +97 -0
  74. package/packages/core/src/events/policy.ts +10 -0
  75. package/packages/core/src/events/repository.ts +46 -3
  76. package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +316 -0
  77. package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/017d8dd5bb6dd80b.json.gz +0 -0
  78. package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/178e12f1759b7552.json.gz +0 -0
  79. package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/4932c70a2af97faf.json.gz +0 -0
  80. package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/536085773daf6b58.json.gz +0 -0
  81. package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/6bd6275061aaffa5.json.gz +0 -0
  82. package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/89625654b02e9ac1.json.gz +0 -0
  83. package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/a98af134be2da8a9.json.gz +0 -0
  84. package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/d5d5ccca80a893d8.json.gz +0 -0
  85. package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/f8155387eb5f375c.json.gz +0 -0
  86. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/3c27f918b194e3b3.json +465 -0
  87. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/79f0e0f4ccd43ba2.json +455 -0
  88. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/859c09edac738823.json +1086 -0
  89. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a283c1f3462b1cc1.json +81 -0
  90. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a6e570fb126991ff.json +36 -0
  91. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a8acf22c968b6e3d.json +81 -0
  92. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/ba7b85895bf6698e.json +1153 -0
  93. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/fc66408cde0b6b04.json +637 -0
  94. package/packages/core/src/kernel/build-roster.ts +230 -0
  95. package/packages/core/src/kernel/dispatch-capacity.ts +4 -2
  96. package/packages/core/src/kernel/engine.ts +43 -5
  97. package/packages/core/src/kernel/execution-intervals.ts +61 -0
  98. package/packages/core/src/kernel/generators/optimization-log.ts +101 -0
  99. package/packages/core/src/kernel/generators/repository-journals.ts +13 -0
  100. package/packages/core/src/kernel/leaf-restack-base.ts +33 -7
  101. package/packages/core/src/kernel/leaf-restack-state.ts +82 -3
  102. package/packages/core/src/kernel/log-index.ts +19 -0
  103. package/packages/core/src/kernel/optimization-admission.ts +244 -3
  104. package/packages/core/src/kernel/optimization.ts +183 -4
  105. package/packages/core/src/kernel/phase-failures.ts +121 -0
  106. package/packages/core/src/kernel/published-branch-head.ts +60 -0
  107. package/packages/core/src/kernel/reducer-registry.ts +27 -8
  108. package/packages/core/src/kernel/reducer-revisions.ts +25 -0
  109. package/packages/core/src/kernel/reducer.ts +63 -4
  110. package/packages/core/src/kernel/rollup.ts +61 -0
  111. package/packages/core/src/kernel/setup-streak.ts +38 -0
  112. package/packages/core/src/kernel/step-durations.ts +257 -0
  113. package/packages/core/src/kernel/target-observation.ts +6 -1
  114. package/packages/core/src/kernel/target-rewrite-recovery.ts +116 -0
  115. package/packages/core/src/kernel/verify-diff-base.ts +5 -0
  116. package/packages/core/src/kernel/verify-stages.ts +13 -3
  117. package/packages/core/src/ontology.ts +5 -0
  118. package/packages/core/src/operator/agent-context-serve.ts +631 -0
  119. package/packages/core/src/operator/annotations.ts +75 -5
  120. package/packages/core/src/operator/attention.ts +470 -0
  121. package/packages/core/src/operator/canonical-skills.ts +35 -0
  122. package/packages/core/src/operator/cli-equivalents.ts +270 -0
  123. package/packages/core/src/operator/context-prompts.ts +61 -0
  124. package/packages/core/src/operator/index.ts +49 -1
  125. package/packages/core/src/operator/landings.ts +28 -19
  126. package/packages/core/src/operator/optimization.ts +7 -0
  127. package/packages/core/src/operator/protocol.ts +3 -0
  128. package/packages/core/src/operator/query.ts +122 -72
  129. package/packages/core/src/operator/read-view.ts +5 -2
  130. package/packages/core/src/operator/registry.ts +406 -46
  131. package/packages/core/src/operator/requests.ts +20 -18
  132. package/packages/core/src/operator/sandbox-host.ts +107 -11
  133. package/packages/core/src/operator/sandbox-resolver.ts +253 -0
  134. package/packages/core/src/operator/sandbox.ts +778 -82
  135. package/packages/core/src/operator/ticket-asset-list.ts +77 -0
  136. package/packages/core/src/operator/ticket-assets.ts +167 -0
  137. package/packages/core/src/operator/tickets.ts +33 -2
  138. package/packages/core/src/plugin-sdk/index.ts +22 -0
  139. package/packages/core/src/plugins/manifest.ts +1 -1
  140. package/packages/core/src/ports/forge/fake.ts +92 -1
  141. package/packages/core/src/ports/forge/git-blob.ts +15 -0
  142. package/packages/core/src/ports/forge/github.ts +82 -0
  143. package/packages/core/src/ports/forge/local-git.ts +61 -1
  144. package/packages/core/src/ports/forge-failure.ts +24 -0
  145. package/packages/core/src/ports/runner/antigravity-test.fixture.ts +149 -0
  146. package/packages/core/src/ports/runner/claude-test.fixture.ts +71 -0
  147. package/packages/core/src/ports/runner/codex-test.fixture.ts +63 -0
  148. package/packages/core/src/ports/tickets/contract.ts +71 -0
  149. package/packages/core/src/ports/tickets/fake.ts +11 -0
  150. package/packages/core/src/ports/tickets/file.ts +18 -0
  151. package/packages/core/src/ports/tickets/github-fake.ts +136 -4
  152. package/packages/core/src/ports/tickets/github.ts +387 -30
  153. package/packages/core/src/ports/tickets/hosted-index.ts +10 -0
  154. package/packages/core/src/ports/tickets/linear.ts +130 -0
  155. package/packages/core/src/ports/tickets/priority.ts +9 -0
  156. package/packages/core/src/ports/tickets/remote-protocol.ts +34 -0
  157. package/packages/core/src/ports/tickets/remote.ts +70 -6
  158. package/packages/core/src/ports/tickets/search.ts +139 -0
  159. package/packages/core/src/ports/types.ts +104 -2
  160. package/packages/core/src/ports/workspace/builtin-capabilities.ts +9 -0
  161. package/packages/core/src/ports/workspace/create.ts +15 -1
  162. package/packages/core/src/ports/workspace/fake.ts +340 -12
  163. package/packages/core/src/ports/workspace/git-worktree.ts +833 -42
  164. package/packages/core/src/ports/workspace/operator-sandbox.ts +129 -12
  165. package/packages/core/src/ports/workspace/process-group.ts +48 -0
  166. package/packages/core/src/ports/workspace/vercel-sandbox.ts +1260 -123
  167. package/packages/core/src/processes/agent-context-assemble.ts +576 -0
  168. package/packages/core/src/processes/agent-context.ts +469 -0
  169. package/packages/core/src/processes/build-runner.ts +387 -236
  170. package/packages/core/src/processes/dispatcher.ts +211 -55
  171. package/packages/core/src/processes/execution-no-progress.ts +4 -0
  172. package/packages/core/src/processes/optimization-activity.ts +120 -0
  173. package/packages/core/src/processes/phase-failure-budget.ts +1 -0
  174. package/packages/core/src/processes/repo-view.ts +78 -2
  175. package/packages/core/src/processes/sandbox-state.ts +53 -6
  176. package/packages/core/src/processes/tick-publication.ts +2 -0
  177. package/packages/core/src/processes/workspace-preparation.ts +76 -0
  178. package/packages/core/src/scenarios/harness.ts +0 -5
  179. package/packages/core/src/store/account-redaction.ts +1 -0
  180. package/packages/core/src/store/adapter.ts +52 -1
  181. package/packages/core/src/store/agent-context.ts +432 -0
  182. package/packages/core/src/store/build-feed.ts +291 -0
  183. package/packages/core/src/store/build-scope.ts +19 -1
  184. package/packages/core/src/store/contract.ts +911 -14
  185. package/packages/core/src/store/digest.ts +19 -6
  186. package/packages/core/src/store/histogram.ts +113 -0
  187. package/packages/core/src/store/local/schema.ts +62 -0
  188. package/packages/core/src/store/local/store.ts +628 -22
  189. package/packages/core/src/store/local/ticket-asset-heads.ts +23 -0
  190. package/packages/core/src/store/memory.ts +461 -69
  191. package/packages/core/src/store/open-builds.ts +19 -0
  192. package/packages/core/src/store/phase-session.ts +32 -4
  193. package/packages/core/src/store/reduced-reads.ts +14 -4
  194. package/packages/core/src/store/reducer-value-shapes.json +6987 -5749
  195. package/packages/core/src/store/reducer-values.ts +6 -1
  196. package/packages/core/src/store/remote/client.ts +208 -2
  197. package/packages/core/src/store/remote/compatibility.ts +1 -1
  198. package/packages/core/src/store/remote/index.ts +33 -0
  199. package/packages/core/src/store/remote/protocol.ts +69 -0
  200. package/packages/core/src/store/remote/released-clients.json +11 -0
  201. package/packages/core/src/store/remote/released-reducer-values/1.11.0.json +35680 -0
  202. package/packages/core/src/store/remote/released-reducer-values/index.ts +18 -16
  203. package/packages/core/src/store/retention.ts +23 -1
  204. package/packages/core/src/store/rollups.ts +337 -0
  205. package/packages/core/src/store/session-handle.ts +20 -2
  206. package/packages/core/src/store/snapshots.ts +10 -0
  207. package/packages/core/src/store/types.ts +108 -9
  208. package/packages/core/src/testing/dispatch-read-budget.ts +1092 -2
  209. package/packages/core/src/testing/git-config-isolation.ts +7 -5
  210. package/packages/core/src/testing/index.ts +27 -0
  211. package/packages/core/src/testing/memory-operator-sandbox.ts +346 -0
  212. package/packages/core/src/testing/reduced-read-budget.ts +1873 -98
  213. package/packages/core/src/web/analytics-source.ts +303 -0
  214. package/packages/core/src/web/assets/Atkinson-OFL.txt +93 -0
  215. package/packages/core/src/web/assets/AtkinsonHyperlegibleNext.latin.woff2 +0 -0
  216. package/packages/core/src/web/assets/JetBrains-OFL.txt +93 -0
  217. package/packages/core/src/web/assets/JetBrainsMono.latin.woff2 +0 -0
  218. package/packages/core/src/web/assets/app.css +1 -0
  219. package/packages/core/src/web/assets/app.js +9 -0
  220. package/packages/core/src/web/assets/manifest.json +31 -0
  221. package/packages/core/src/web/assets/theme.js +1 -0
  222. package/packages/core/src/web/bounded.ts +31 -0
  223. package/packages/core/src/web/http.ts +315 -0
  224. package/packages/core/src/web/hub.ts +160 -0
  225. package/packages/core/src/web/options.ts +38 -0
  226. package/packages/core/src/web/overview-source.ts +322 -0
  227. package/packages/core/src/web/page.ts +56 -0
  228. package/packages/core/src/web/protocol.ts +151 -0
  229. package/packages/core/src/web/read-only.ts +23 -0
  230. package/packages/core/src/web/repository-source.ts +35 -0
  231. package/packages/core/src/web/server.ts +97 -0
  232. package/skills/code-review/SKILL.md +10 -2
  233. package/skills/guide/SKILL.md +366 -52
  234. package/skills/guide/references/accounts.md +2 -1
  235. package/skills/guide/references/integration-tree.md +2 -2
  236. package/skills/guide/references/operator-api.md +134 -6
  237. package/skills/guide/references/optimization-nodes.md +69 -11
  238. package/skills/guide/references/plugin-authoring.md +37 -1
  239. package/skills/guide/references/remote-store-protocol.md +94 -13
  240. package/skills/guide/references/remote-ticket-protocol.md +40 -0
  241. package/skills/guide/references/restacking.md +20 -4
  242. package/skills/guide/references/setup.md +14 -2
  243. package/skills/guide/references/ticket-dependencies.md +10 -3
  244. package/skills/implement/SKILL.md +23 -4
  245. package/skills/operate/SKILL.md +134 -32
  246. package/skills/plan/SKILL.md +10 -4
  247. package/skills/plan-review/SKILL.md +9 -2
  248. package/skills/spec/SKILL.md +34 -8
  249. package/skills/tickets/SKILL.md +51 -5
  250. package/templates/hosted-service/package.json +2 -2
@@ -5,11 +5,18 @@
5
5
  * of the node's reduced digest, so it adds no store read to any surface.
6
6
  *
7
7
  * The row is independent of `now`, like the rest of the cached dashboard
8
- * model: it records when the budget opened and renderers evaluate elapsed
9
- * hours against their own clock.
8
+ * model: it carries the node's active-hours clock and renderers evaluate the
9
+ * hours against their own clock. Hours are active hours, the time the node's
10
+ * attempts, proposer or measurements ran, never time since opening.
10
11
  */
11
12
  import type { BuildState } from '../../kernel/reducer'
12
13
  import type { PendingCleanupView, UnmeasuredMergeView } from '../../kernel/optimization'
14
+ import {
15
+ activeHours,
16
+ clockSettling,
17
+ clockView,
18
+ type OptimizationClockView,
19
+ } from '../../kernel/optimization-admission'
13
20
  import type { OptimizationStopCause } from '../../events/payloads'
14
21
 
15
22
  export interface ObjectiveRow {
@@ -33,13 +40,24 @@ export interface ObjectiveRow {
33
40
  /** Running attempts that merged without a landing measurement and hold
34
41
  * their slot until an operator settles them. Present only when > 0. */
35
42
  unmeasuredMerges?: number
36
- budget: { maxAttempts?: number; maxHours?: number; openedAt: string }
43
+ budget: {
44
+ maxAttempts?: number
45
+ maxHours?: number
46
+ openedAt: string
47
+ /** The active-hours clock the hours budget reads. */
48
+ clock: OptimizationClockView
49
+ /** Stopped, and the value may change once more when the dispatcher
50
+ * accounts attempt runs from before the stop. */
51
+ settling?: true
52
+ }
37
53
  stopped?: { cause: OptimizationStopCause; mode: 'abort' | 'drain'; at: string }
38
54
  }
39
55
 
40
- /** The row with its elapsed hours evaluated, as the status, watch and
41
- * operator surfaces carry it under `objective`. */
42
- export type ObjectiveSummary = ObjectiveRow & { elapsedHours: number }
56
+ /** The row with its active hours evaluated, as the status, watch and
57
+ * operator surfaces carry it under `objective`. `elapsedHours` is active
58
+ * hours; `settling` marks a stopped node whose value may still rise once,
59
+ * when the dispatcher accounts attempt runs from before the stop. */
60
+ export type ObjectiveSummary = ObjectiveRow & { elapsedHours: number; settling?: true }
43
61
 
44
62
  /** The terms every objective surface uses. */
45
63
  export const objectiveVocabulary = {
@@ -56,7 +74,8 @@ export const objectiveVocabulary = {
56
74
  mergedUnmeasured: 'merged unmeasured',
57
75
  slotHeld: 'slot held',
58
76
  budget: 'budget',
59
- hours: 'hours',
77
+ hours: 'active hours',
78
+ settling: 'settling',
60
79
  stopped: 'stopped',
61
80
  } as const
62
81
 
@@ -85,24 +104,35 @@ export function projectObjectiveRow(state: BuildState): ObjectiveRow | undefined
85
104
  ...(maxAttempts !== undefined ? { maxAttempts } : {}),
86
105
  ...(maxHours !== undefined ? { maxHours } : {}),
87
106
  openedAt: digest.openedAt,
107
+ clock: clockView(digest.clock),
108
+ ...(clockSettling(digest) ? { settling: true as const } : {}),
88
109
  },
89
110
  ...(digest.stopped !== undefined ? { stopped: { ...digest.stopped } } : {}),
90
111
  }
91
112
  }
92
113
 
93
- const HOUR_MS = 3_600_000
94
-
95
- /** Hours the budget has consumed at `now`; a stopped node's stop at its stop. */
114
+ /** Active hours the budget has consumed at `now`; constant once the node
115
+ * stopped. */
96
116
  export function objectiveElapsedHours(row: ObjectiveRow, now: Date): number {
97
- const end = row.stopped !== undefined ? Date.parse(row.stopped.at) : now.getTime()
98
- return Math.max(0, (end - Date.parse(row.budget.openedAt)) / HOUR_MS)
117
+ return activeHours(row.budget.clock, now)
118
+ }
119
+
120
+ /** A stopped node whose clock the dispatcher has not settled yet. */
121
+ export function objectiveSettling(row: ObjectiveRow): boolean {
122
+ return row.budget.settling === true
99
123
  }
100
124
 
101
125
  /** The summary surfaces carry, or undefined for a build that is not an
102
126
  * optimization node. */
103
127
  export function objectiveSummary(state: BuildState, now: Date): ObjectiveSummary | undefined {
104
128
  const row = projectObjectiveRow(state)
105
- return row === undefined ? undefined : { ...row, elapsedHours: objectiveElapsedHours(row, now) }
129
+ return row === undefined
130
+ ? undefined
131
+ : {
132
+ ...row,
133
+ elapsedHours: objectiveElapsedHours(row, now),
134
+ ...(objectiveSettling(row) ? { settling: true as const } : {}),
135
+ }
106
136
  }
107
137
 
108
138
  /** A measured value, trimmed to at most six decimals. */
@@ -137,7 +167,9 @@ export function objectiveParts(row: ObjectiveRow, elapsedHours: number): string[
137
167
  if (budget.maxAttempts !== undefined)
138
168
  budgetParts.push(`${attempts.filed}/${budget.maxAttempts} ${v.attempts}`)
139
169
  if (budget.maxHours !== undefined)
140
- budgetParts.push(`${elapsedHours.toFixed(1)}/${budget.maxHours} ${v.hours}`)
170
+ budgetParts.push(
171
+ `${elapsedHours.toFixed(1)}/${budget.maxHours} ${v.hours}${objectiveSettling(row) ? ` (${v.settling})` : ''}`,
172
+ )
141
173
  return [
142
174
  value,
143
175
  [
@@ -1,5 +1,12 @@
1
1
  import { enrichIntegrationRows } from './integration-row'
2
- import type { CapacityBuild } from '../../kernel/dispatch-capacity'
2
+ import {
3
+ dispatchCapacity,
4
+ startClaimDemand,
5
+ urgentClaimDemand,
6
+ type CapacityBuild,
7
+ } from '../../kernel/dispatch-capacity'
8
+ import type { DispatchStatus } from '../../kernel/dispatch-status'
9
+ import { migrationIdentity } from '../../kernel/ticket-migrations'
3
10
  import {
4
11
  openBuildWorkspaceReducer,
5
12
  provisionMarkerReducer,
@@ -23,11 +30,17 @@ import type {
23
30
  StreamRecord,
24
31
  StreamScope,
25
32
  } from '../../store/types'
26
- import { readBuildFacts } from '../../store/reduced-reads'
33
+ import {
34
+ readBuildFacts,
35
+ type CurrentBuildFacts,
36
+ type RepositoryReadFacts,
37
+ } from '../../store/reduced-reads'
38
+ import { dispatchWarningLines } from './header'
27
39
  import {
28
40
  projectBuild,
29
41
  projectBuildFromFacts,
30
42
  type DashboardBuild,
43
+ type DashboardHeader,
31
44
  type DashboardRowFacts,
32
45
  } from './model'
33
46
 
@@ -91,12 +104,164 @@ interface ReducedEntry {
91
104
 
92
105
  type PollEntry = LiveEntry | TerminalEntry | ReducedEntry
93
106
 
94
- function isTerminal(state: BuildState): boolean {
95
- // `build.aborted` acknowledges cancellation but begins the checkpointed
96
- // cleanup saga. Keep polling until its final `build.completed` fact.
107
+ /** A build the dashboard no longer shows. `build.aborted` acknowledges
108
+ * cancellation but begins the checkpointed cleanup saga, so an aborted build
109
+ * stays visible until its final `build.completed` fact. */
110
+ export function isDashboardTerminal(state: BuildState): boolean {
97
111
  return state.status === 'done'
98
112
  }
99
113
 
114
+ /** One build's capacity facts from its saved `buildReadFacts` value. */
115
+ export function capacityBuildFromFacts(
116
+ record: BuildRecord,
117
+ value: CurrentBuildFacts,
118
+ ): Required<CapacityBuild> {
119
+ return {
120
+ record,
121
+ state: value.state,
122
+ workspace: value.workspace !== null,
123
+ provisioning: value.provisionMarker !== undefined,
124
+ restackDue: landingRestackDue(value.state, value.log),
125
+ }
126
+ }
127
+
128
+ /** The display-only reads that decorate one row: authoritative stream
129
+ * records (SPEC §9) and the pinned effective-config artifact (SPEC §16.1). */
130
+ export interface RowEnrichment {
131
+ streams: StreamRecord[] | undefined
132
+ artifact: { revision?: number; pipelineSource?: PipelineSourceMeta; config?: Config } | undefined
133
+ }
134
+
135
+ /** Read one build's row enrichment, when the reader exposes the stream and
136
+ * artifact primitives. Every failure is display-only: an absent enrichment
137
+ * degrades to the pairing-derived status and the live config. */
138
+ export async function readRowEnrichment(
139
+ reader: Pick<DashboardBuildReader, 'listStreams' | 'getArtifact'>,
140
+ slug: string,
141
+ pin?: BuildState['verify']['configPin'],
142
+ ): Promise<RowEnrichment> {
143
+ const [streams, artifact] = await Promise.all([
144
+ readStreams(reader, slug),
145
+ readEffectiveConfig(reader, slug, pin),
146
+ ])
147
+ return { streams, artifact }
148
+ }
149
+
150
+ async function readStreams(
151
+ reader: Pick<DashboardBuildReader, 'listStreams'>,
152
+ slug: string,
153
+ ): Promise<StreamRecord[] | undefined> {
154
+ if (reader.listStreams === undefined) return undefined
155
+ try {
156
+ return await reader.listStreams({ kind: 'build', build: slug })
157
+ } catch {
158
+ return undefined
159
+ }
160
+ }
161
+
162
+ /** The metadata projection plus the artifact's parsed content. A malformed
163
+ * artifact still yields its metadata (config absent). */
164
+ async function readEffectiveConfig(
165
+ reader: Pick<DashboardBuildReader, 'getArtifact'>,
166
+ slug: string,
167
+ pin?: BuildState['verify']['configPin'],
168
+ ): Promise<RowEnrichment['artifact']> {
169
+ const getArtifact = reader.getArtifact
170
+ if (getArtifact === undefined) return undefined
171
+ try {
172
+ const artifact = await getArtifact.call(
173
+ reader,
174
+ slug,
175
+ pin?.artifact.kind ?? BUILD_EFFECTIVE_CONFIG_ARTIFACT,
176
+ pin?.artifact.rev,
177
+ )
178
+ if (artifact === null) return undefined
179
+ let config: Config | undefined
180
+ try {
181
+ config = parseEffectiveBuildConfig(artifact)
182
+ } catch {
183
+ config = undefined
184
+ }
185
+ return { ...parseBuildConfigMetadata(artifact), config }
186
+ } catch {
187
+ return undefined
188
+ }
189
+ }
190
+
191
+ /** Project one build row against its PINNED pipeline (SPEC §16.1) and
192
+ * attach the effective-config metadata. The row's verify/finalize steps and
193
+ * next-action must describe the pipeline the build actually runs — the
194
+ * artifact's build-owned sections — never the dispatcher's live base-branch
195
+ * snapshot, which a pinned build may never execute. Deployment-owned
196
+ * sections (roles/policy) still come from the live snapshot, so a reload
197
+ * that reaches the build is reflected. A build with no artifact yet (queued
198
+ * or pre-pin) projects from the live config, the pre-pin behavior. Pure. */
199
+ export function projectRowWithEnrichment(
200
+ record: BuildRecord,
201
+ state: BuildState,
202
+ events: AbEvent[],
203
+ config: Config,
204
+ { streams, artifact }: RowEnrichment,
205
+ facts?: DashboardRowFacts,
206
+ ): DashboardBuild | null {
207
+ const pinnedConfig =
208
+ artifact?.config !== undefined ? composeBuildConfig(artifact.config, config) : undefined
209
+ const build = facts
210
+ ? projectBuildFromFacts(record, state, facts, config, streams, pinnedConfig)
211
+ : projectBuild(record, state, config, events, streams, pinnedConfig)
212
+ if (build !== null && artifact !== undefined) {
213
+ if (artifact.revision !== undefined) build.effectiveConfigRev = artifact.revision
214
+ if (artifact.pipelineSource !== undefined) build.pipelineSource = artifact.pipelineSource
215
+ }
216
+ return build
217
+ }
218
+
219
+ /**
220
+ * The dashboard header for one repository frame: the queue and capacity
221
+ * terms every frontend shows. `states` are the visible builds' reductions;
222
+ * acknowledged aborts and integration nodes do not count as active.
223
+ */
224
+ export function repositoryDashboardHeader(input: {
225
+ repo: string
226
+ status: DispatchStatus
227
+ config: Config
228
+ repositoryFacts: RepositoryReadFacts
229
+ capacityBuilds: readonly CapacityBuild[]
230
+ states: Iterable<BuildState>
231
+ now: Date
232
+ triage?: number
233
+ standby?: string
234
+ }): DashboardHeader {
235
+ const { status, config, repositoryFacts } = input
236
+ const warningLines = dispatchWarningLines(status)
237
+ return {
238
+ repo: input.repo,
239
+ ...(input.standby !== undefined ? { standby: input.standby } : {}),
240
+ queued: status.queued ?? 0,
241
+ activeCount: [...input.states].filter(
242
+ (state) => state.kind !== 'node' && state.status !== 'done' && state.status !== 'aborted',
243
+ ).length,
244
+ capacity: config.capacity,
245
+ capacityProjection: dispatchCapacity(
246
+ input.capacityBuilds,
247
+ config.capacity,
248
+ input.now,
249
+ migrationIdentity(repositoryFacts.ticketMigrations, config.tickets.source),
250
+ {
251
+ urgentClaims: urgentClaimDemand(repositoryFacts),
252
+ openPrs: {
253
+ policy: config.policy,
254
+ engaged: repositoryFacts.openPrBackpressure !== undefined,
255
+ },
256
+ startRequests: startClaimDemand(repositoryFacts),
257
+ },
258
+ ),
259
+ ...(input.triage !== undefined ? { triage: input.triage } : {}),
260
+ ...(status.availableUpgrade !== undefined ? { availableUpgrade: status.availableUpgrade } : {}),
261
+ ...(warningLines.length > 0 ? { warningLines } : {}),
262
+ }
263
+ }
264
+
100
265
  /**
101
266
  * Fail closed if a reader violates BuildStore's ordered, contiguous stream
102
267
  * contract. Advancing past a gap would make every later incremental read
@@ -146,63 +311,11 @@ export class DashboardBuildPollCache {
146
311
  private config: Config,
147
312
  ) {}
148
313
 
149
- /** Authoritative stream records for one build, when the reader supports
150
- * the stream primitive (SPEC §9). Failures are display-only: an absent
151
- * enrichment degrades to the pairing-derived status. */
152
- private async readStreams(slug: string): Promise<StreamRecord[] | undefined> {
153
- if (this.reader.listStreams === undefined) return undefined
154
- try {
155
- return await this.reader.listStreams({ kind: 'build', build: slug })
156
- } catch {
157
- return undefined
158
- }
159
- }
160
-
161
314
  /** True only while no later refresh has committed. */
162
315
  isCurrent(snapshot: DashboardPollSnapshot): boolean {
163
316
  return snapshot.revision === this.committedRevision
164
317
  }
165
318
 
166
- /** Effective-config artifact facts for one build (SPEC §16.1), when the
167
- * reader exposes the artifact primitive: the metadata projection plus the
168
- * artifact's parsed content. A malformed artifact still yields its metadata
169
- * (config absent); all failures are display-only. */
170
- private async readEffectiveConfig(
171
- slug: string,
172
- pin?: BuildState['verify']['configPin'],
173
- ): Promise<
174
- { revision?: number; pipelineSource?: PipelineSourceMeta; config?: Config } | undefined
175
- > {
176
- const getArtifact = this.reader.getArtifact
177
- if (getArtifact === undefined) return undefined
178
- try {
179
- const artifact = await getArtifact.call(
180
- this.reader,
181
- slug,
182
- pin?.artifact.kind ?? BUILD_EFFECTIVE_CONFIG_ARTIFACT,
183
- pin?.artifact.rev,
184
- )
185
- if (artifact === null) return undefined
186
- let config: Config | undefined
187
- try {
188
- config = parseEffectiveBuildConfig(artifact)
189
- } catch {
190
- config = undefined
191
- }
192
- return { ...parseBuildConfigMetadata(artifact), config }
193
- } catch {
194
- return undefined
195
- }
196
- }
197
-
198
- /** Project one build row against its PINNED pipeline (SPEC §16.1) and
199
- * attach the effective-config metadata. The row's verify/finalize steps and
200
- * next-action must describe the pipeline the build actually runs — the
201
- * artifact's build-owned sections — never the dispatcher's live base-branch
202
- * snapshot, which a pinned build may never execute. Deployment-owned
203
- * sections (roles/policy) still come from the live snapshot, so a reload
204
- * that reaches the build is reflected. A build with no artifact yet (queued
205
- * or pre-pin) projects from the live config, the pre-pin behavior. */
206
319
  private async projectRow(
207
320
  record: BuildRecord,
208
321
  state: BuildState,
@@ -210,20 +323,8 @@ export class DashboardBuildPollCache {
210
323
  config: Config,
211
324
  facts?: DashboardRowFacts,
212
325
  ): Promise<DashboardBuild | null> {
213
- const [streams, artifact] = await Promise.all([
214
- this.readStreams(record.slug),
215
- this.readEffectiveConfig(record.slug, state.verify.configPin),
216
- ])
217
- const pinnedConfig =
218
- artifact?.config !== undefined ? composeBuildConfig(artifact.config, config) : undefined
219
- const build = facts
220
- ? projectBuildFromFacts(record, state, facts, config, streams, pinnedConfig)
221
- : projectBuild(record, state, config, events, streams, pinnedConfig)
222
- if (build !== null && artifact !== undefined) {
223
- if (artifact.revision !== undefined) build.effectiveConfigRev = artifact.revision
224
- if (artifact.pipelineSource !== undefined) build.pipelineSource = artifact.pipelineSource
225
- }
226
- return build
326
+ const enrichment = await readRowEnrichment(this.reader, record.slug, state.verify.configPin)
327
+ return projectRowWithEnrichment(record, state, events, config, enrichment, facts)
227
328
  }
228
329
 
229
330
  refresh(
@@ -262,17 +363,18 @@ export class DashboardBuildPollCache {
262
363
  { getReducerValue: this.reader.getReducerValue.bind(this.reader) },
263
364
  record.slug,
264
365
  )
265
- if (isTerminal(value.state)) {
366
+ if (isDashboardTerminal(value.state)) {
266
367
  next.set(record.slug, { kind: 'terminal', record })
267
368
  } else {
369
+ const capacity = capacityBuildFromFacts(record, value)
268
370
  next.set(record.slug, {
269
371
  kind: 'reduced',
270
372
  cursor,
271
373
  record,
272
374
  state: value.state,
273
- workspace: value.workspace !== null,
274
- provisioning: value.provisionMarker !== undefined,
275
- restackDue: landingRestackDue(value.state, value.log),
375
+ workspace: capacity.workspace,
376
+ provisioning: capacity.provisioning,
377
+ restackDue: capacity.restackDue,
276
378
  build:
277
379
  current?.kind === 'reduced' &&
278
380
  current.cursor === cursor &&
@@ -310,7 +412,7 @@ export class DashboardBuildPollCache {
310
412
  const events = current === undefined ? delta : [...current.events, ...delta]
311
413
  const acc = buildReducer.advance(current?.acc ?? buildReducer.initial(), delta)
312
414
  const state = buildReducer.finish(acc)
313
- if (isTerminal(state)) {
415
+ if (isDashboardTerminal(state)) {
314
416
  next.set(record.slug, { kind: 'terminal', record })
315
417
  continue
316
418
  }