@defrex/autobuild 1.11.1 → 1.12.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 (102) hide show
  1. package/README.md +1 -1
  2. package/SPEC.md +177 -54
  3. package/docs/answering-blocked-builds.md +10 -2
  4. package/docs/architecture.md +66 -6
  5. package/docs/assets/headline-wide.png +0 -0
  6. package/docs/configuration.md +13 -11
  7. package/docs/dispatcher.md +1 -1
  8. package/docs/integration-tree.md +28 -16
  9. package/docs/operator-api.md +4 -4
  10. package/docs/operator-surfaces.md +35 -0
  11. package/docs/optimization-nodes.md +3 -2
  12. package/docs/tickets.md +1 -1
  13. package/docs/troubleshooting.md +1 -1
  14. package/package.json +3 -3
  15. package/packages/core/src/cli/ambient-errors.ts +17 -0
  16. package/packages/core/src/cli/bounded-io.ts +174 -0
  17. package/packages/core/src/cli/dashboard/model.ts +81 -23
  18. package/packages/core/src/cli/dashboard/poll.ts +20 -31
  19. package/packages/core/src/cli/dashboard/render.ts +14 -37
  20. package/packages/core/src/cli/dashboard/row-text.ts +24 -7
  21. package/packages/core/src/cli/dashboard/tone-ansi.ts +39 -0
  22. package/packages/core/src/cli/dashboard/vocabulary.ts +161 -20
  23. package/packages/core/src/cli/dispatch-handoff.ts +41 -10
  24. package/packages/core/src/cli/dispatch-launch-contract.ts +5 -0
  25. package/packages/core/src/cli/dispatch-launcher-cadence.ts +80 -0
  26. package/packages/core/src/cli/dispatch-launcher.ts +9 -10
  27. package/packages/core/src/cli/dispatch-preflight.ts +12 -3
  28. package/packages/core/src/cli/dispatch-release-observer.ts +53 -0
  29. package/packages/core/src/cli/dispatch-runtime-cache.ts +204 -82
  30. package/packages/core/src/cli/dispatch-runtime-dependencies.ts +250 -43
  31. package/packages/core/src/cli/dispatch-upgrade-install.ts +36 -16
  32. package/packages/core/src/cli/dispatch-upgrade-source.ts +8 -2
  33. package/packages/core/src/cli/dispatch-yield-observer.ts +82 -0
  34. package/packages/core/src/cli/dispatch.ts +68 -5
  35. package/packages/core/src/cli/env.ts +2 -15
  36. package/packages/core/src/cli/help.ts +5 -4
  37. package/packages/core/src/cli/main.ts +4 -2
  38. package/packages/core/src/cli/production-store.ts +102 -0
  39. package/packages/core/src/cli/repo-state.ts +2 -3
  40. package/packages/core/src/cli/repository-status.ts +28 -22
  41. package/packages/core/src/cli/sessionless-store.ts +26 -0
  42. package/packages/core/src/cli/status.ts +169 -40
  43. package/packages/core/src/cli/store-opening.ts +15 -58
  44. package/packages/core/src/cli/store-ref.ts +6 -1
  45. package/packages/core/src/config/schema.ts +1 -1
  46. package/packages/core/src/distribution-packaging.ts +156 -18
  47. package/packages/core/src/events/catalog-identity.ts +28 -0
  48. package/packages/core/src/events/catalog.ts +3 -9
  49. package/packages/core/src/events/compatibility.ts +2 -30
  50. package/packages/core/src/events/payloads.ts +11 -0
  51. package/packages/core/src/events/policy.ts +3 -0
  52. package/packages/core/src/events/repository.ts +1 -1
  53. package/packages/core/src/events/sessions.ts +1 -1
  54. package/packages/core/src/events/validation-error.ts +12 -0
  55. package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +9 -0
  56. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/3064e337cc75f8d6.json +310 -0
  57. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a1e90a969d32a367.json +307 -0
  58. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/da3bc91c49211eab.json +310 -0
  59. package/packages/core/src/kernel/auto-merge-default.ts +2 -1
  60. package/packages/core/src/kernel/capacity-build.ts +21 -0
  61. package/packages/core/src/kernel/discard-in-flight.ts +13 -0
  62. package/packages/core/src/kernel/dispatch-capacity.ts +354 -147
  63. package/packages/core/src/kernel/engine.ts +2 -8
  64. package/packages/core/src/kernel/measurement.ts +24 -0
  65. package/packages/core/src/kernel/read-facts.ts +8 -370
  66. package/packages/core/src/kernel/reducer-registry.ts +40 -125
  67. package/packages/core/src/kernel/reducer-revisions.ts +3 -0
  68. package/packages/core/src/kernel/reducer-value-entry.ts +98 -0
  69. package/packages/core/src/kernel/reducer-value-index.ts +75 -0
  70. package/packages/core/src/kernel/reducer.ts +57 -9
  71. package/packages/core/src/kernel/reopen-state.ts +10 -0
  72. package/packages/core/src/kernel/repository-read-facts.ts +375 -0
  73. package/packages/core/src/kernel/verify-stage-state.ts +145 -3
  74. package/packages/core/src/operator/presentation.ts +13 -0
  75. package/packages/core/src/operator/query.ts +40 -39
  76. package/packages/core/src/operator/registry.ts +3 -3
  77. package/packages/core/src/ports/workspace/fake.ts +15 -3
  78. package/packages/core/src/processes/dispatcher.ts +232 -75
  79. package/packages/core/src/processes/execution-no-progress.ts +1 -0
  80. package/packages/core/src/store/local/capabilities.ts +115 -0
  81. package/packages/core/src/store/local/store.ts +86 -56
  82. package/packages/core/src/store/reduced-reads.ts +9 -1
  83. package/packages/core/src/store/reducer-value-read.ts +235 -0
  84. package/packages/core/src/store/reducer-value-shapes.json +1437 -1349
  85. package/packages/core/src/store/reducer-values.ts +36 -204
  86. package/packages/core/src/store/remote/released-clients.json +11 -0
  87. package/packages/core/src/store/remote/released-reducer-values/1.12.0.json +35806 -0
  88. package/packages/core/src/store/remote/released-reducer-values/index.ts +18 -16
  89. package/packages/core/src/store/rollup-lease.ts +41 -0
  90. package/packages/core/src/store/rollups.ts +7 -38
  91. package/packages/core/src/store/snapshots.ts +1 -1
  92. package/packages/core/src/testing/dispatch-read-budget.ts +265 -4
  93. package/packages/core/src/testing/import-graph.ts +78 -0
  94. package/packages/core/src/testing/reduced-read-budget.ts +221 -22
  95. package/packages/core/src/testing/slot-agreement.ts +52 -0
  96. package/packages/core/src/web/assets/app.js +5 -5
  97. package/skills/guide/SKILL.md +115 -44
  98. package/skills/guide/references/integration-tree.md +28 -16
  99. package/skills/guide/references/operator-api.md +4 -4
  100. package/skills/guide/references/optimization-nodes.md +3 -2
  101. package/skills/operate/SKILL.md +38 -20
  102. package/templates/hosted-service/package.json +2 -2
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Store capabilities a short-lived reader may never use, initialized on first
3
+ * need. A process that only reads its repository record and change position
4
+ * never evaluates the event catalogs, the write validators or the reducers.
5
+ *
6
+ * Rule for every use: take `capability.value ?? (await capability.load())` as
7
+ * the method's first statement, before any transaction, read or mutation, and
8
+ * use the resolved functions synchronously where they were used before. Every
9
+ * write is therefore still validated before it mutates anything, and once a
10
+ * capability is loaded a method runs with no added await at all. A failed load
11
+ * is not remembered: the next use retries it, and the error reaches the caller.
12
+ */
13
+ export class Capability<T> {
14
+ value: T | undefined
15
+ private pending: Promise<T> | undefined
16
+
17
+ constructor(private readonly initialize: () => Promise<T>) {}
18
+
19
+ load(): Promise<T> {
20
+ this.pending ??= this.initialize().then(
21
+ (value) => {
22
+ this.value = value
23
+ return value
24
+ },
25
+ (error: unknown) => {
26
+ this.pending = undefined
27
+ throw error
28
+ },
29
+ )
30
+ return this.pending
31
+ }
32
+ }
33
+
34
+ /** Build, repository and session event validation, plus the build-creation and
35
+ * tick-finish checks. */
36
+ export const writeValidation = new Capability(async () => {
37
+ const [catalog, repository, sessions, newBuild, repoTick] = await Promise.all([
38
+ import('../../events/catalog'),
39
+ import('../../events/repository'),
40
+ import('../../events/sessions'),
41
+ import('../new-build'),
42
+ import('../repo-tick'),
43
+ ])
44
+ return {
45
+ validateEventWrite: catalog.prepareEventWrite,
46
+ validateRepositoryEventWrite: repository.prepareRepositoryEventWrite,
47
+ validateSessionEventWrite: sessions.validateSessionEventWrite,
48
+ validateCreatedEvent: newBuild.validateCreatedEvent,
49
+ validateTickFinish: repoTick.validateTickFinish,
50
+ }
51
+ })
52
+
53
+ /** Strict build-event payload schemas. */
54
+ export const payloadSchemas = new Capability(async () => {
55
+ const { eventPayloadSchemas } = await import('../../events/payloads')
56
+ return { eventPayloadSchemas }
57
+ })
58
+
59
+ /** Named reducer values over the selective index: only the requested reducer's
60
+ * module initializes. Never the complete catalog unless a request names a
61
+ * reducer outside the index. */
62
+ export const reducedValues = new Capability(async () => {
63
+ const [read, index] = await Promise.all([
64
+ import('../reducer-value-read'),
65
+ import('../../kernel/reducer-value-index'),
66
+ ])
67
+ return {
68
+ parseValueRequest: read.parseValueRequest,
69
+ resolveRegisteredValue: read.resolveRegisteredValue,
70
+ readResolvedReducerValue: read.readResolvedReducerValue,
71
+ loadValueRegistration: index.loadValueRegistration,
72
+ loadedValueRegistration: index.loadedValueRegistration,
73
+ }
74
+ })
75
+
76
+ /** Saved build digests for open-build and repository digest listings. */
77
+ export const buildDigests = new Capability(async () => {
78
+ const { readPersistedBuildDigests } = await import('../reduced-reads')
79
+ return { readPersistedBuildDigests }
80
+ })
81
+
82
+ /** Repository rollups (the derived-reducer engine). */
83
+ export const rollups = new Capability(async () => {
84
+ const { readRollupValue } = await import('../rollups')
85
+ return { readRollupValue }
86
+ })
87
+
88
+ /** The repository state-event selection. */
89
+ export const repositoryStateEvents = new Capability(async () => {
90
+ const module = await import('../repo-state-events')
91
+ return {
92
+ REPOSITORY_STATE_EVENT_TYPES: module.REPOSITORY_STATE_EVENT_TYPES,
93
+ REPOSITORY_LATEST_EVENT_TYPES: module.REPOSITORY_LATEST_EVENT_TYPES,
94
+ readRepoStateEventsWithAnchorRecheck: module.readRepoStateEventsWithAnchorRecheck,
95
+ }
96
+ })
97
+
98
+ /** Ticket asset storage and pinning. */
99
+ export const ticketAssetCapability = new Capability(async () => {
100
+ const module = await import('../ticket-assets')
101
+ return {
102
+ DEFAULT_TICKET_ASSET_LIMITS: module.DEFAULT_TICKET_ASSET_LIMITS,
103
+ loadTicketAsset: module.loadTicketAsset,
104
+ storeTicketAssetBlobs: module.storeTicketAssetBlobs,
105
+ resolvePinnedAsset: module.resolvePinnedAsset,
106
+ summarizeTicketAsset: module.summarizeTicketAsset,
107
+ validateTicketAssetInput: module.validateTicketAssetInput,
108
+ }
109
+ })
110
+
111
+ /** UI message stream assembly at close. */
112
+ export const streamAssembly = new Capability(async () => {
113
+ const { assembleUIMessageDocument } = await import('../streams/assemble')
114
+ return { assembleUIMessageDocument }
115
+ })
@@ -6,16 +6,9 @@ import {
6
6
  type OperatorIdentity,
7
7
  type OperatorPresence,
8
8
  } from '../operator-presence'
9
- import { READ_CATALOG_IDENTITY } from '../../events/compatibility'
10
- import { readPersistedBuildDigests } from '../reduced-reads'
9
+ import { READ_CATALOG_IDENTITY } from '../../events/catalog-identity'
11
10
  import { assertPublicSnapshotReducer } from '../snapshots'
12
- import {
13
- readReducerValue,
14
- resolveReducerValue,
15
- type ReducerValueRequest,
16
- type ReducerValue,
17
- } from '../reducer-values'
18
- import { validateTickFinish } from '../repo-tick'
11
+ import type { ReducerValueRequest, ReducerValue } from '../reducer-value-read'
19
12
  import {
20
13
  buildPageOrder,
21
14
  decodeBuildPageCursor,
@@ -37,7 +30,8 @@ import type { RepoTickCompletion, RepoTickFinish, RepoTickObservation } from '..
37
30
  * explicitly resolved local state root. Repository/default selection belongs
38
31
  * to the CLI; this adapter only opens the path it is given.
39
32
  *
40
- * Every write goes through `validateEventWrite` (§8 — the enforced ontology).
33
+ * Every write goes through `validateEventWrite` (§8 — the enforced ontology),
34
+ * loaded on first use (`./capabilities`) before the write's transaction.
41
35
  * Seq assignment and `appendWithArtifacts` are transactional: bun:sqlite
42
36
  * transactions are synchronous, so the atomic path (D6) runs inside one
43
37
  * `db.transaction` while blob writes happen *before* it — a rolled-back
@@ -52,26 +46,19 @@ import { access, copyFile, mkdtemp, rm } from 'node:fs/promises'
52
46
  import { tmpdir } from 'node:os'
53
47
  import { join } from 'node:path'
54
48
  import { humanActor } from '../../events/envelope'
55
- import {
56
- prepareEventWrite as validateEventWrite,
57
- type AbEvent,
58
- type EventEnvelope,
59
- type EventWrite,
60
- } from '../../events/catalog'
61
- import { eventPayloadSchemas, type EventType } from '../../events/payloads'
62
- import {
63
- prepareRepositoryEventWrite as validateRepositoryEventWrite,
64
- type RepositoryEvent,
65
- type RepositoryEventEnvelope,
66
- type RepositoryEventType,
67
- type RepositoryEventWrite,
49
+ import type { AbEvent, EventEnvelope, EventWrite } from '../../events/catalog'
50
+ import type { EventType } from '../../events/payloads'
51
+ import type {
52
+ RepositoryEvent,
53
+ RepositoryEventEnvelope,
54
+ RepositoryEventType,
55
+ RepositoryEventWrite,
68
56
  } from '../../events/repository'
69
- import {
70
- validateSessionEventWrite,
71
- type SessionEvent,
72
- type SessionEventEnvelope,
73
- type SessionEventType,
74
- type SessionEventWrite,
57
+ import type {
58
+ SessionEvent,
59
+ SessionEventEnvelope,
60
+ SessionEventType,
61
+ SessionEventWrite,
75
62
  } from '../../events/sessions'
76
63
  import { createBuildScopedStore } from '../build-scope'
77
64
  import type { ReducerSnapshot, ReducerValueScope, RollupScope, SnapshotScope } from '../snapshots'
@@ -81,17 +68,9 @@ import {
81
68
  ROLLUP_LEASE_WAIT_MS,
82
69
  RollupLeaseLostError,
83
70
  RollupLeaseTimeoutError,
84
- readRollupValue,
85
- type RollupAdvance,
86
- type RollupStorage,
87
- } from '../rollups'
88
- import { validateCreatedEvent } from '../new-build'
71
+ } from '../rollup-lease'
72
+ import type { RollupAdvance, RollupStorage } from '../rollups'
89
73
  import { createSessionScopedStore } from '../session-handle'
90
- import {
91
- REPOSITORY_STATE_EVENT_TYPES,
92
- REPOSITORY_LATEST_EVENT_TYPES,
93
- readRepoStateEventsWithAnchorRecheck,
94
- } from '../repo-state-events'
95
74
  import {
96
75
  DEFAULT_ARTIFACT_RETENTION_MAX_REVISIONS,
97
76
  isRetentionManagedKind,
@@ -99,7 +78,6 @@ import {
99
78
  } from '../retention'
100
79
  import { pollingSubscribe } from '../subscribe'
101
80
  import { StreamLocks } from '../streams/lock'
102
- import { assembleUIMessageDocument } from '../streams/assemble'
103
81
  import { EVENT_WAIT_POLL_MS, readEventsWithWait, readStreamWithWait } from '../streams/wait'
104
82
  import {
105
83
  serializedBatchSize,
@@ -150,19 +128,23 @@ import {
150
128
  HEAD as ASSET_HEAD,
151
129
  NEXT_NAME_SAME_KIND as ASSET_NEXT_NAME_SAME_KIND,
152
130
  } from './ticket-asset-heads'
153
- import {
154
- DEFAULT_TICKET_ASSET_LIMITS,
155
- loadTicketAsset,
156
- storeTicketAssetBlobs,
157
- resolvePinnedAsset,
158
- summarizeTicketAsset,
159
- validateTicketAssetInput,
160
- type TicketAsset,
161
- type TicketAssetInput,
162
- type TicketAssetLimits,
163
- type TicketAssetMeta,
164
- type TicketAssetSummary,
131
+ import type {
132
+ TicketAsset,
133
+ TicketAssetInput,
134
+ TicketAssetLimits,
135
+ TicketAssetMeta,
136
+ TicketAssetSummary,
165
137
  } from '../ticket-assets'
138
+ import {
139
+ buildDigests,
140
+ payloadSchemas,
141
+ reducedValues,
142
+ repositoryStateEvents,
143
+ rollups,
144
+ streamAssembly,
145
+ ticketAssetCapability,
146
+ writeValidation,
147
+ } from './capabilities'
166
148
  import { DirBlobStore } from './blobs'
167
149
  import {
168
150
  artifacts,
@@ -966,6 +948,7 @@ export class SqliteBuildStore implements BuildStore {
966
948
  }
967
949
 
968
950
  async createBuild(input: NewBuildInput): Promise<BuildRecord> {
951
+ const { validateCreatedEvent } = writeValidation.value ?? (await writeValidation.load())
969
952
  const created = validateCreatedEvent(input)
970
953
  const ts = this.now()
971
954
  return this.writeTx(() => {
@@ -1074,6 +1057,7 @@ export class SqliteBuildStore implements BuildStore {
1074
1057
  }
1075
1058
 
1076
1059
  async getNodeRewrite(slug: string, previous?: string, beforePrevious?: string) {
1060
+ const { eventPayloadSchemas } = payloadSchemas.value ?? (await payloadSchemas.load())
1077
1061
  this.requireBuild(slug)
1078
1062
  const row = (
1079
1063
  beforePrevious !== undefined
@@ -1100,6 +1084,7 @@ export class SqliteBuildStore implements BuildStore {
1100
1084
  }
1101
1085
 
1102
1086
  async listOpenBuilds(repo: string): Promise<OpenBuild[]> {
1087
+ const { readPersistedBuildDigests } = buildDigests.value ?? (await buildDigests.load())
1103
1088
  const records = this.db
1104
1089
  .select({ build: builds })
1105
1090
  .from(openBuilds)
@@ -1113,6 +1098,7 @@ export class SqliteBuildStore implements BuildStore {
1113
1098
  }
1114
1099
 
1115
1100
  async getRepoBuildDigests(repo: string): Promise<Map<string, BuildDigest>> {
1101
+ const { readPersistedBuildDigests } = buildDigests.value ?? (await buildDigests.load())
1116
1102
  return readPersistedBuildDigests(
1117
1103
  this,
1118
1104
  repo,
@@ -1169,6 +1155,7 @@ export class SqliteBuildStore implements BuildStore {
1169
1155
  }
1170
1156
 
1171
1157
  async append<T extends EventType>(slug: string, event: EventWrite<T>): Promise<EventEnvelope<T>> {
1158
+ const { validateEventWrite } = writeValidation.value ?? (await writeValidation.load())
1172
1159
  const validated = validateEventWrite(event)
1173
1160
  return this.writeTx(() => this.appendInTx(slug, validated)) as EventEnvelope<T>
1174
1161
  }
@@ -1178,6 +1165,7 @@ export class SqliteBuildStore implements BuildStore {
1178
1165
  expectedSeq: number,
1179
1166
  event: EventWrite<T>,
1180
1167
  ): Promise<EventEnvelope<T> | null> {
1168
+ const { validateEventWrite } = writeValidation.value ?? (await writeValidation.load())
1181
1169
  validateExpectedSeq(expectedSeq)
1182
1170
  const validated = validateEventWrite(event)
1183
1171
  return this.writeTx(() => {
@@ -1306,6 +1294,7 @@ export class SqliteBuildStore implements BuildStore {
1306
1294
  artifactInputs: ArtifactInput[],
1307
1295
  makeEvent: (deposited: ArtifactMeta[]) => EventWrite<T>,
1308
1296
  ): Promise<{ event: EventEnvelope<T>; artifacts: ArtifactMeta[] }> {
1297
+ const { validateEventWrite } = writeValidation.value ?? (await writeValidation.load())
1309
1298
  const prepared: PreparedArtifact[] = []
1310
1299
  for (const input of artifactInputs) {
1311
1300
  prepared.push(await this.prepareArtifact(input))
@@ -1339,6 +1328,7 @@ export class SqliteBuildStore implements BuildStore {
1339
1328
  artifactInputs: ArtifactInput[],
1340
1329
  makeEvent: (deposited: ArtifactMeta[]) => EventWrite<T>,
1341
1330
  ): Promise<{ event: EventEnvelope<T>; artifacts: ArtifactMeta[] } | null> {
1331
+ const { validateEventWrite } = writeValidation.value ?? (await writeValidation.load())
1342
1332
  validateExpectedSeq(expectedSeq)
1343
1333
  const prepared: PreparedArtifact[] = []
1344
1334
  for (const input of artifactInputs) {
@@ -1707,6 +1697,7 @@ export class SqliteBuildStore implements BuildStore {
1707
1697
  repo: string,
1708
1698
  event: RepositoryEventWrite<T>,
1709
1699
  ): Promise<RepositoryEventEnvelope<T>> {
1700
+ const { validateRepositoryEventWrite } = writeValidation.value ?? (await writeValidation.load())
1710
1701
  const validated = validateRepositoryEventWrite(event)
1711
1702
  return this.writeTx(() => this.appendRepoInTx(repo, validated)) as RepositoryEventEnvelope<T>
1712
1703
  }
@@ -1716,6 +1707,7 @@ export class SqliteBuildStore implements BuildStore {
1716
1707
  expectedSeq: number,
1717
1708
  event: RepositoryEventWrite<T>,
1718
1709
  ): Promise<RepositoryEventEnvelope<T> | null> {
1710
+ const { validateRepositoryEventWrite } = writeValidation.value ?? (await writeValidation.load())
1719
1711
  validateExpectedSeq(expectedSeq)
1720
1712
  const validated = validateRepositoryEventWrite(event)
1721
1713
  return this.writeTx(() => {
@@ -1781,6 +1773,7 @@ export class SqliteBuildStore implements BuildStore {
1781
1773
  event: RepositoryEventEnvelope<T>
1782
1774
  artifacts: RepositoryArtifactMeta[]
1783
1775
  }> {
1776
+ const { validateRepositoryEventWrite } = writeValidation.value ?? (await writeValidation.load())
1784
1777
  const prepared: PreparedArtifact[] = []
1785
1778
  for (const input of artifactInputs) {
1786
1779
  prepared.push(await this.prepareArtifact(input))
@@ -1911,10 +1904,27 @@ export class SqliteBuildStore implements BuildStore {
1911
1904
  scope: ReducerValueScope,
1912
1905
  request: ReducerValueRequest,
1913
1906
  ): Promise<ReducerValue> {
1914
- if (scope.kind === 'rollup') return readRollupValue(scope, request, this.rollupStorage(scope))
1915
- // Reject invalid requests before touching storage.
1916
- resolveReducerValue(scope, request)
1917
- return readReducerValue(scope, request, {
1907
+ if (scope.kind === 'rollup') {
1908
+ const { readRollupValue } = rollups.value ?? (await rollups.load())
1909
+ return readRollupValue(scope, request, this.rollupStorage(scope))
1910
+ }
1911
+ const {
1912
+ parseValueRequest,
1913
+ resolveRegisteredValue,
1914
+ readResolvedReducerValue,
1915
+ loadValueRegistration,
1916
+ loadedValueRegistration,
1917
+ } = reducedValues.value ?? (await reducedValues.load())
1918
+ // Reject invalid requests before touching storage. Only the requested
1919
+ // reducer's registration initializes; a loaded one adds no await.
1920
+ const parsed = parseValueRequest(request)
1921
+ const loaded = loadedValueRegistration(parsed.reducer)
1922
+ const resolved = resolveRegisteredValue(
1923
+ scope,
1924
+ parsed,
1925
+ loaded !== null ? loaded : await loadValueRegistration(parsed.reducer),
1926
+ )
1927
+ return readResolvedReducerValue(resolved, {
1918
1928
  highWater: async () => {
1919
1929
  const tail = this.snapshotTail(scope)
1920
1930
  if (tail === undefined) throw new Error('unknown reducer scope')
@@ -2231,6 +2241,11 @@ export class SqliteBuildStore implements BuildStore {
2231
2241
  }
2232
2242
 
2233
2243
  async getRepoStateEvents(repo: string): Promise<RepositoryEvent[]> {
2244
+ const {
2245
+ REPOSITORY_STATE_EVENT_TYPES,
2246
+ REPOSITORY_LATEST_EVENT_TYPES,
2247
+ readRepoStateEventsWithAnchorRecheck,
2248
+ } = repositoryStateEvents.value ?? (await repositoryStateEvents.load())
2234
2249
  this.requireRepo(repo)
2235
2250
  // Two queries, mirroring the oracle's derivation: the latest
2236
2251
  // run-started anchor, then one select of durable types plus the tail
@@ -2368,6 +2383,8 @@ export class SqliteBuildStore implements BuildStore {
2368
2383
  }
2369
2384
 
2370
2385
  async ticketAssetLimits(_repo: string): Promise<TicketAssetLimits> {
2386
+ const { DEFAULT_TICKET_ASSET_LIMITS } =
2387
+ ticketAssetCapability.value ?? (await ticketAssetCapability.load())
2371
2388
  return { ...DEFAULT_TICKET_ASSET_LIMITS }
2372
2389
  }
2373
2390
 
@@ -2390,6 +2407,8 @@ export class SqliteBuildStore implements BuildStore {
2390
2407
  ticketId: string,
2391
2408
  asset: TicketAssetInput,
2392
2409
  ): Promise<TicketAssetMeta> {
2410
+ const { validateTicketAssetInput, storeTicketAssetBlobs } =
2411
+ ticketAssetCapability.value ?? (await ticketAssetCapability.load())
2393
2412
  const size = validateTicketAssetInput(asset)
2394
2413
  const entries = await storeTicketAssetBlobs(this.blobs, asset.entries)
2395
2414
  return this.writeTx(() => {
@@ -2424,6 +2443,7 @@ export class SqliteBuildStore implements BuildStore {
2424
2443
  name: string,
2425
2444
  rev?: number,
2426
2445
  ): Promise<TicketAsset | null> {
2446
+ const { loadTicketAsset } = ticketAssetCapability.value ?? (await ticketAssetCapability.load())
2427
2447
  const scoped = ticketAssetKey(repo, ticketId, kind, name)
2428
2448
  const row =
2429
2449
  rev === undefined
@@ -2449,6 +2469,8 @@ export class SqliteBuildStore implements BuildStore {
2449
2469
  name: string,
2450
2470
  rev?: number,
2451
2471
  ): Promise<TicketAsset | null> {
2472
+ const { resolvePinnedAsset } =
2473
+ ticketAssetCapability.value ?? (await ticketAssetCapability.load())
2452
2474
  return resolvePinnedAsset(this, slug, kind, name, rev)
2453
2475
  }
2454
2476
 
@@ -2457,6 +2479,8 @@ export class SqliteBuildStore implements BuildStore {
2457
2479
  ticketId: string,
2458
2480
  opts?: { revisions?: boolean },
2459
2481
  ): Promise<TicketAssetSummary[]> {
2482
+ const { summarizeTicketAsset } =
2483
+ ticketAssetCapability.value ?? (await ticketAssetCapability.load())
2460
2484
  if (opts?.revisions) {
2461
2485
  return this.db
2462
2486
  .select()
@@ -2571,6 +2595,7 @@ export class SqliteBuildStore implements BuildStore {
2571
2595
  completion: RepoTickCompletion,
2572
2596
  events: RepositoryEventWrite[],
2573
2597
  ): Promise<RepoTickFinish | null> {
2598
+ const { validateTickFinish } = writeValidation.value ?? (await writeValidation.load())
2574
2599
  const validated = validateTickFinish(holder, completion, events)
2575
2600
  return this.writeTx(() => {
2576
2601
  const row = this.requireRepo(repo)
@@ -2718,6 +2743,7 @@ export class SqliteBuildStore implements BuildStore {
2718
2743
  }
2719
2744
 
2720
2745
  async createSession(input: NewSessionInput): Promise<SessionRecord> {
2746
+ const { validateSessionEventWrite } = writeValidation.value ?? (await writeValidation.load())
2721
2747
  const operator = normalizeOperator(input.operator)
2722
2748
  if (!input.repo) throw new Error('repo is required')
2723
2749
  const ts = this.now()
@@ -2789,6 +2815,7 @@ export class SqliteBuildStore implements BuildStore {
2789
2815
  id: string,
2790
2816
  event: SessionEventWrite<T>,
2791
2817
  ): Promise<SessionEventEnvelope<T>> {
2818
+ const { validateSessionEventWrite } = writeValidation.value ?? (await writeValidation.load())
2792
2819
  const validated = validateSessionEventWrite(event)
2793
2820
  return this.writeTx(() => this.appendSessionInTx(id, validated) as SessionEventEnvelope<T>)
2794
2821
  }
@@ -2798,6 +2825,7 @@ export class SqliteBuildStore implements BuildStore {
2798
2825
  expectedSeq: number,
2799
2826
  event: SessionEventWrite<T>,
2800
2827
  ): Promise<SessionEventEnvelope<T> | null> {
2828
+ const { validateSessionEventWrite } = writeValidation.value ?? (await writeValidation.load())
2801
2829
  validateExpectedSeq(expectedSeq)
2802
2830
  const validated = validateSessionEventWrite(event)
2803
2831
  return this.writeTx(() => {
@@ -2886,6 +2914,7 @@ export class SqliteBuildStore implements BuildStore {
2886
2914
  artifactInputs: ArtifactInput[],
2887
2915
  makeEvent: (deposited: SessionArtifactMeta[]) => SessionEventWrite<T>,
2888
2916
  ): Promise<{ event: SessionEventEnvelope<T>; artifacts: SessionArtifactMeta[] }> {
2917
+ const { validateSessionEventWrite } = writeValidation.value ?? (await writeValidation.load())
2889
2918
  const prepared: PreparedArtifact[] = []
2890
2919
  for (const input of artifactInputs) {
2891
2920
  prepared.push(await this.prepareArtifact(input))
@@ -3136,6 +3165,7 @@ export class SqliteBuildStore implements BuildStore {
3136
3165
  }
3137
3166
 
3138
3167
  async closeStream(streamId: string, outcome: StreamOutcome): Promise<StreamRecord> {
3168
+ const { assembleUIMessageDocument } = streamAssembly.value ?? (await streamAssembly.load())
3139
3169
  // The per-stream mutex holds across the entire prepare→commit loop
3140
3170
  // (AUT-348): a same-process append issued during this close waits for it
3141
3171
  // to finish and then rejects on the closed check with StreamClosedError.
@@ -11,7 +11,6 @@ import type {
11
11
  buildStreamSeedReducer,
12
12
  repositoryReadFactsReducer,
13
13
  } from '../kernel/read-facts'
14
- import { publicationStateReducer } from '../processes/publication-state'
15
14
  import type { SnapshotScope } from './snapshots'
16
15
 
17
16
  export class ReducerValueValidationError extends Error {
@@ -43,6 +42,9 @@ export async function readBuildFacts(store: Reader, slug: string) {
43
42
  'buildReadFacts',
44
43
  )
45
44
  const value = result.value
45
+ // A build reducer: repository readers of this module never initialize it.
46
+ // Once loaded, the read adds no await.
47
+ if (!publicationStateReducer) publicationStateReducer = await loadPublicationStateReducer()
46
48
  return {
47
49
  cursor: result.cursor,
48
50
  value: {
@@ -52,6 +54,12 @@ export async function readBuildFacts(store: Reader, slug: string) {
52
54
  },
53
55
  }
54
56
  }
57
+ let publicationStateReducer:
58
+ | typeof import('../processes/publication-state').publicationStateReducer
59
+ | undefined
60
+ async function loadPublicationStateReducer() {
61
+ return (await import('../processes/publication-state')).publicationStateReducer
62
+ }
55
63
  async function readValue<T>(
56
64
  store: Reader,
57
65
  scope: SnapshotScope,