@defrex/autobuild 1.12.0 → 1.14.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 (167) hide show
  1. package/README.md +1 -1
  2. package/SPEC.md +465 -163
  3. package/bin/ab-build-runner.ts +8 -0
  4. package/docs/answering-blocked-builds.md +28 -4
  5. package/docs/architecture.md +136 -53
  6. package/docs/cli.md +1 -1
  7. package/docs/configuration.md +120 -17
  8. package/docs/dispatcher.md +19 -8
  9. package/docs/integration-overview.md +1 -1
  10. package/docs/integration-tree.md +12 -9
  11. package/docs/merge-policy.md +7 -2
  12. package/docs/operating.md +13 -3
  13. package/docs/operator-api.md +46 -23
  14. package/docs/operator-surfaces.md +7 -6
  15. package/docs/optimization-nodes.md +7 -6
  16. package/docs/plugin-authoring.md +46 -18
  17. package/docs/remote-store-protocol.md +52 -13
  18. package/docs/remote-ticket-protocol.md +85 -38
  19. package/docs/restacking.md +48 -3
  20. package/docs/setup.md +2 -1
  21. package/docs/ticket-migration.md +2 -2
  22. package/docs/ticket-sources.md +23 -8
  23. package/docs/tickets.md +4 -4
  24. package/docs/troubleshooting.md +32 -2
  25. package/docs/verification.md +3 -1
  26. package/package.json +3 -3
  27. package/packages/core/src/cli/build-control.ts +72 -6
  28. package/packages/core/src/cli/dashboard/actions.ts +4 -0
  29. package/packages/core/src/cli/dashboard/integration-row.ts +43 -19
  30. package/packages/core/src/cli/dashboard/model.ts +14 -8
  31. package/packages/core/src/cli/dashboard/poll.ts +218 -10
  32. package/packages/core/src/cli/dashboard/queue.ts +21 -10
  33. package/packages/core/src/cli/dashboard/row-text.ts +2 -1
  34. package/packages/core/src/cli/dashboard/vocabulary.ts +5 -1
  35. package/packages/core/src/cli/dispatch-frontend.ts +2 -1
  36. package/packages/core/src/cli/dispatch-tickets.ts +33 -12
  37. package/packages/core/src/cli/dispatch.ts +547 -145
  38. package/packages/core/src/cli/help.ts +38 -24
  39. package/packages/core/src/cli/main.ts +7 -5
  40. package/packages/core/src/cli/publication-settlement.ts +4 -0
  41. package/packages/core/src/cli/remote-poll.ts +22 -5
  42. package/packages/core/src/cli/repository-status.ts +19 -9
  43. package/packages/core/src/cli/status.ts +23 -11
  44. package/packages/core/src/cli/store-opening.ts +12 -0
  45. package/packages/core/src/cli/ticket-start.ts +3 -3
  46. package/packages/core/src/cli/ticket.ts +12 -11
  47. package/packages/core/src/cli/wait.ts +8 -28
  48. package/packages/core/src/cli/watch.ts +113 -50
  49. package/packages/core/src/config/reference-metadata.ts +7 -1
  50. package/packages/core/src/config/reference.ts +5 -2
  51. package/packages/core/src/config/schema.ts +51 -16
  52. package/packages/core/src/events/payloads.ts +74 -14
  53. package/packages/core/src/events/policy.ts +1 -0
  54. package/packages/core/src/events/repository.ts +33 -3
  55. package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +31 -0
  56. package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/be211f0e31a65006.json.gz +0 -0
  57. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/0a10010ee5cf2e47.json +577 -0
  58. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/227701361be6d1e0.json +490 -0
  59. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/312cc4090820a612.json +1078 -0
  60. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/76e5043bae7a80ab.json +86 -0
  61. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/87a917802870bbed.json +87 -0
  62. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/9d85616e5b9e24c1.json +500 -0
  63. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a2780567b4989fb6.json +105 -0
  64. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/c7c94fbe7f63cf07.json +84 -0
  65. package/packages/core/src/kernel/clean-restack-policy.ts +40 -0
  66. package/packages/core/src/kernel/dispatch-capacity.ts +243 -107
  67. package/packages/core/src/kernel/done-escalations.ts +273 -0
  68. package/packages/core/src/kernel/engine.ts +28 -9
  69. package/packages/core/src/kernel/integration-landing.ts +24 -4
  70. package/packages/core/src/kernel/integration-stack.ts +39 -0
  71. package/packages/core/src/kernel/janitor-failure.ts +20 -1
  72. package/packages/core/src/kernel/log-index-value.ts +3 -0
  73. package/packages/core/src/kernel/log-index.ts +41 -2
  74. package/packages/core/src/kernel/open-pr-backpressure.ts +3 -3
  75. package/packages/core/src/kernel/reducer-registry.ts +3 -0
  76. package/packages/core/src/kernel/reducer-revisions.ts +19 -0
  77. package/packages/core/src/kernel/reducer.ts +42 -8
  78. package/packages/core/src/kernel/repository-attention.ts +14 -5
  79. package/packages/core/src/kernel/repository-read-facts.ts +9 -0
  80. package/packages/core/src/kernel/ticket-starts.ts +15 -4
  81. package/packages/core/src/kernel/verify-stages.ts +46 -7
  82. package/packages/core/src/operator/attention.ts +28 -3
  83. package/packages/core/src/operator/done-escalations.ts +38 -0
  84. package/packages/core/src/operator/presentation.ts +1 -1
  85. package/packages/core/src/operator/protocol.ts +8 -5
  86. package/packages/core/src/operator/query.ts +33 -19
  87. package/packages/core/src/operator/registry.ts +17 -13
  88. package/packages/core/src/operator/ticket-start.ts +8 -7
  89. package/packages/core/src/operator/tickets.ts +28 -9
  90. package/packages/core/src/plugin-sdk/index.ts +10 -0
  91. package/packages/core/src/plugins/manifest.ts +14 -1
  92. package/packages/core/src/ports/forge/create.ts +8 -0
  93. package/packages/core/src/ports/forge/github-transport.ts +5 -1
  94. package/packages/core/src/ports/forge/github.ts +3 -0
  95. package/packages/core/src/ports/tickets/contract.ts +147 -76
  96. package/packages/core/src/ports/tickets/create.ts +47 -7
  97. package/packages/core/src/ports/tickets/fake.ts +8 -4
  98. package/packages/core/src/ports/tickets/file-codec.ts +4 -3
  99. package/packages/core/src/ports/tickets/file.ts +7 -3
  100. package/packages/core/src/ports/tickets/github.ts +1 -0
  101. package/packages/core/src/ports/tickets/grants.ts +11 -3
  102. package/packages/core/src/ports/tickets/hosted-index.ts +5 -0
  103. package/packages/core/src/ports/tickets/linear.ts +28 -9
  104. package/packages/core/src/ports/tickets/migration.ts +4 -4
  105. package/packages/core/src/ports/tickets/operations.ts +4 -2
  106. package/packages/core/src/ports/tickets/priority-labels.ts +213 -0
  107. package/packages/core/src/ports/tickets/priority.ts +127 -21
  108. package/packages/core/src/ports/tickets/remote-protocol.ts +10 -0
  109. package/packages/core/src/ports/tickets/remote.ts +104 -11
  110. package/packages/core/src/ports/tickets/revision.ts +5 -3
  111. package/packages/core/src/ports/tickets/search.ts +9 -0
  112. package/packages/core/src/ports/tickets/tree.ts +8 -6
  113. package/packages/core/src/ports/types.ts +30 -15
  114. package/packages/core/src/ports/workspace/build-execution.ts +13 -1
  115. package/packages/core/src/ports/workspace/builtin-capabilities.ts +4 -0
  116. package/packages/core/src/ports/workspace/create.ts +16 -3
  117. package/packages/core/src/ports/workspace/execution-exit-marker.ts +41 -0
  118. package/packages/core/src/ports/workspace/recovery-step.ts +101 -0
  119. package/packages/core/src/ports/workspace/vercel-sandbox.ts +596 -144
  120. package/packages/core/src/processes/build-execution-state.ts +4 -0
  121. package/packages/core/src/processes/build-runner.ts +14 -1
  122. package/packages/core/src/processes/dispatcher.ts +841 -230
  123. package/packages/core/src/processes/execution-settlement.ts +25 -3
  124. package/packages/core/src/processes/harvest-runner.ts +12 -7
  125. package/packages/core/src/processes/infrastructure-failure-budget.ts +7 -2
  126. package/packages/core/src/processes/invocation-budget.ts +176 -0
  127. package/packages/core/src/processes/publication-loss.ts +38 -3
  128. package/packages/core/src/processes/tick-publication.ts +1 -0
  129. package/packages/core/src/processes/workspace-preservation.ts +86 -15
  130. package/packages/core/src/store/contract.ts +88 -3
  131. package/packages/core/src/store/event-reads.ts +30 -1
  132. package/packages/core/src/store/reduced-reads.ts +23 -0
  133. package/packages/core/src/store/reducer-value-shapes.json +6759 -6490
  134. package/packages/core/src/store/reducer-value-validation.ts +188 -33
  135. package/packages/core/src/store/remote/client.ts +169 -29
  136. package/packages/core/src/store/remote/compatibility.ts +113 -56
  137. package/packages/core/src/store/remote/credentials.ts +4 -1
  138. package/packages/core/src/store/remote/protocol.ts +3 -0
  139. package/packages/core/src/store/remote/released-clients.json +22 -0
  140. package/packages/core/src/store/remote/released-reducer-values/1.13.0.json +35966 -0
  141. package/packages/core/src/store/remote/released-reducer-values/1.14.0.json +36076 -0
  142. package/packages/core/src/store/remote/released-reducer-values/index.ts +20 -16
  143. package/packages/core/src/store/remote/released-value-policy.ts +144 -0
  144. package/packages/core/src/store/remote/retry.ts +406 -0
  145. package/packages/core/src/store/repo-state-events.ts +1 -0
  146. package/packages/core/src/testing/dispatch-read-budget.ts +155 -11
  147. package/packages/core/src/testing/index.ts +2 -0
  148. package/packages/core/src/testing/invocation-budget.ts +42 -0
  149. package/packages/core/src/testing/reduced-read-budget.ts +589 -19
  150. package/packages/core/src/testing/released-value-coverage.ts +423 -0
  151. package/packages/core/src/web/overview-source.ts +19 -6
  152. package/skills/guide/SKILL.md +147 -67
  153. package/skills/guide/references/integration-tree.md +12 -9
  154. package/skills/guide/references/merge-policy.md +7 -2
  155. package/skills/guide/references/operator-api.md +46 -23
  156. package/skills/guide/references/optimization-nodes.md +7 -6
  157. package/skills/guide/references/plugin-authoring.md +46 -18
  158. package/skills/guide/references/remote-store-protocol.md +52 -13
  159. package/skills/guide/references/remote-ticket-protocol.md +85 -38
  160. package/skills/guide/references/restacking.md +48 -3
  161. package/skills/guide/references/setup.md +2 -1
  162. package/skills/guide/references/ticket-migration.md +2 -2
  163. package/skills/operate/SKILL.md +136 -55
  164. package/skills/plan/SKILL.md +4 -3
  165. package/skills/spec/SKILL.md +9 -5
  166. package/skills/tickets/SKILL.md +25 -17
  167. package/templates/hosted-service/package.json +2 -2
@@ -1,6 +1,8 @@
1
1
  import type { JsonValue } from './reducer-values'
2
2
  import shapes from './reducer-value-shapes.json'
3
+ import { REDUCER_REVISIONS } from '../kernel/reducer-revisions'
3
4
 
5
+ export type ReducerValueShape = Shape
4
6
  type Shape = {
5
7
  kind: string
6
8
  value?: unknown
@@ -8,11 +10,20 @@ type Shape = {
8
10
  props?: Record<string, { type: string; optional: boolean }>
9
11
  index?: string
10
12
  }
13
+ /** Names the value contract a projecting client reads: its shapes and the
14
+ * reducer versions it expects. The versions derive from the revision ledger, so
15
+ * a fold-only revision changes this identity even though no shape changes: a
16
+ * source client whose versions moved past a release never claims that release's
17
+ * frozen catalog and so is never answered at the release's older versions. */
11
18
  export const REDUCER_VALUE_CONTRACT_IDENTITY = new Bun.CryptoHasher('sha256')
12
19
  .update(JSON.stringify(shapes))
20
+ .update(JSON.stringify(Object.keys(REDUCER_REVISIONS).sort()))
13
21
  .digest('hex')
14
22
  const schemas: Record<string, Shape> = shapes.schemas
15
23
 
24
+ /** This build's own generated catalog: the canonical shape the server serves. */
25
+ export const CURRENT_REDUCER_VALUES: ReducerValueCatalog = shapes
26
+
16
27
  /** A finished-value shape catalog: this build's generated one, or a frozen
17
28
  * catalog from a published release line. */
18
29
  export interface ReducerValueCatalog {
@@ -63,7 +74,7 @@ function matches(id: string, value: unknown, table = schemas): boolean {
63
74
  case 'boolean':
64
75
  return typeof value === 'boolean'
65
76
  case 'union':
66
- return (shape.items as string[]).some((item) => matches(item, value, table))
77
+ return unionCandidates(table, shape, value).some((item) => matches(item, value, table))
67
78
  case 'array':
68
79
  return (
69
80
  Array.isArray(value) && value.every((item) => matches(shape.items as string, item, table))
@@ -77,7 +88,7 @@ function matches(id: string, value: unknown, table = schemas): boolean {
77
88
  )
78
89
  return false
79
90
  const record = value as Record<string, unknown>
80
- for (const [key, prop] of Object.entries(shape.props!)) {
91
+ for (const [key, prop] of propEntries(shape)) {
81
92
  if (!(key in record) && prop.optional) continue
82
93
  if (!matches(prop.type, record[key], table)) return false
83
94
  }
@@ -92,6 +103,109 @@ function matches(id: string, value: unknown, table = schemas): boolean {
92
103
  }
93
104
  }
94
105
 
106
+ /** True only when union member `id` provably cannot accept `value`: a differing
107
+ * literal, `undefined` against a defined value, a non-object against an object,
108
+ * or an object whose required literal `type`/`t` discriminant the value lacks or
109
+ * contradicts. A cheap filter before the full structural trial; it never
110
+ * excludes a member that could succeed. */
111
+ export function discriminantMismatch(
112
+ table: Record<string, Shape>,
113
+ id: string,
114
+ value: unknown,
115
+ ): boolean {
116
+ const shape = table[id]!
117
+ switch (shape.kind) {
118
+ case 'literal':
119
+ return value !== shape.value
120
+ case 'undefined':
121
+ return value !== undefined
122
+ case 'object': {
123
+ if (
124
+ value === null ||
125
+ typeof value !== 'object' ||
126
+ Array.isArray(value) ||
127
+ Object.getPrototypeOf(value) !== Object.prototype
128
+ )
129
+ return true
130
+ for (const [key, literal] of discriminants(table, shape))
131
+ if (!(key in value) || (value as Record<string, unknown>)[key] !== literal) return true
132
+ return false
133
+ }
134
+ default:
135
+ return false
136
+ }
137
+ }
138
+
139
+ /** What `discriminantMismatch` can observe about a value, as a cache key. */
140
+ function discriminantKey(value: unknown): string | undefined {
141
+ if (value === null || typeof value !== 'object')
142
+ return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean'
143
+ ? `${typeof value}:${value}`
144
+ : typeof value === 'undefined'
145
+ ? 'undefined'
146
+ : undefined
147
+ if (Array.isArray(value) || Object.getPrototypeOf(value) !== Object.prototype) return 'other'
148
+ const record = value as Record<string, unknown>
149
+ const part = (key: string) => {
150
+ if (!(key in record)) return '-'
151
+ const item = record[key]
152
+ return typeof item === 'string' ? JSON.stringify(item) : undefined
153
+ }
154
+ const type = part('type')
155
+ const t = part('t')
156
+ return type === undefined || t === undefined ? undefined : `object:${type}:${t}`
157
+ }
158
+ const unionCandidateCache = new WeakMap<Shape, Map<string, string[]>>()
159
+ /** Union members not excluded by `discriminantMismatch`, in declaration order,
160
+ * cached per union shape and discriminant key. */
161
+ export function unionCandidates(
162
+ table: Record<string, Shape>,
163
+ shape: Shape,
164
+ value: unknown,
165
+ ): readonly string[] {
166
+ const items = shape.items as string[]
167
+ const key = discriminantKey(value)
168
+ if (key === undefined) return items.filter((item) => !discriminantMismatch(table, item, value))
169
+ let cache = unionCandidateCache.get(shape)
170
+ if (!cache) {
171
+ cache = new Map()
172
+ unionCandidateCache.set(shape, cache)
173
+ }
174
+ let found = cache.get(key)
175
+ if (!found) {
176
+ found = items.filter((item) => !discriminantMismatch(table, item, value))
177
+ if (cache.size < 256) cache.set(key, found)
178
+ }
179
+ return found
180
+ }
181
+
182
+ const discriminantCache = new WeakMap<Shape, readonly (readonly [string, unknown])[]>()
183
+ /** An object shape's required literal `type`/`t` props, computed once per shape. */
184
+ function discriminants(table: Record<string, Shape>, shape: Shape) {
185
+ let found = discriminantCache.get(shape)
186
+ if (found) return found
187
+ found = (['type', 't'] as const).flatMap((key) => {
188
+ const prop = shape.props?.[key]
189
+ const literal = prop && !prop.optional ? table[prop.type] : undefined
190
+ return literal?.kind === 'literal' ? [[key, literal.value] as const] : []
191
+ })
192
+ discriminantCache.set(shape, found)
193
+ return found
194
+ }
195
+ const propEntryCache = new WeakMap<
196
+ Shape,
197
+ readonly [string, { type: string; optional: boolean }][]
198
+ >()
199
+ /** Declared properties of an object shape, computed once per (immutable) shape. */
200
+ export function propEntries(shape: Shape) {
201
+ let entries = propEntryCache.get(shape)
202
+ if (!entries) {
203
+ entries = Object.entries(shape.props ?? {})
204
+ propEntryCache.set(shape, entries)
205
+ }
206
+ return entries
207
+ }
208
+
95
209
  export interface ValidationFailure {
96
210
  path: string
97
211
  expected: string
@@ -170,7 +284,7 @@ function diagnose(id: string, value: unknown, path: string, depth: number): Diag
170
284
  Object.getPrototypeOf(value) === Object.prototype
171
285
  ) {
172
286
  const record = value as Record<string, unknown>
173
- for (const [key, prop] of Object.entries(shape.props!)) {
287
+ for (const [key, prop] of propEntries(shape)) {
174
288
  if (!(key in record) && prop.optional) continue
175
289
  const found = diagnose(prop.type, record[key], property(path, key), depth + 1)
176
290
  if (found) return found
@@ -178,7 +292,11 @@ function diagnose(id: string, value: unknown, path: string, depth: number): Diag
178
292
  for (const key of Object.keys(record).sort()) {
179
293
  if (Object.hasOwn(shape.props!, key)) continue
180
294
  if (!shape.index)
181
- return { path: property(path, key), expected: 'no additional property', depth: depth + 1 }
295
+ return {
296
+ path: property(path, key),
297
+ expected: 'no additional property',
298
+ depth: depth + 1,
299
+ }
182
300
  const found = diagnose(shape.index, record[key], property(path, key), depth + 1)
183
301
  if (found) return found
184
302
  }
@@ -187,49 +305,61 @@ function diagnose(id: string, value: unknown, path: string, depth: number): Diag
187
305
  }
188
306
 
189
307
  type Projected = { value: unknown; dropped: number }
190
- type Unprojectable = Diagnostic
308
+ /** A diagnostic location, rendered only when a projection fails. */
309
+ type At = { up: At; key: string | number } | null
310
+ function render(at: At): string {
311
+ return at === null
312
+ ? '$'
313
+ : typeof at.key === 'number'
314
+ ? `${render(at.up)}[${at.key}]`
315
+ : property(render(at.up), at.key)
316
+ }
317
+ /** `shape` names the expected kind; it is rendered only for the reported failure. */
318
+ type Unprojectable = { at: At; depth: number; shape: Shape }
319
+ const expected = (shape: Shape) =>
320
+ shape.kind === 'literal' ? JSON.stringify(shape.value) : shape.kind
191
321
  type Projection = Projected | Unprojectable
192
322
  // Plugin consumers may compile without strict narrowing; `in` guards narrow everywhere.
193
- const failed = (result: Projection): result is Unprojectable => 'path' in result
323
+ const failed = (result: Projection): result is Unprojectable => 'at' in result
194
324
 
195
325
  /** Project a value onto a catalog's shape, dropping object properties the shape
196
326
  * does not know. Additive server fields then reach older readers as the value
197
327
  * they were built for, while a missing field or unknown variant still fails.
198
- * Among union members, the one dropping the fewest properties wins. */
328
+ * Among union members, the one dropping the fewest properties wins.
329
+ *
330
+ * A projection that drops nothing is exactly a match, and returns the input
331
+ * itself; composites recurse once instead of revalidating every subtree at each
332
+ * level, so the cost stays linear in the value's size. */
199
333
  function project(
200
334
  table: Record<string, Shape>,
201
335
  id: string,
202
336
  value: unknown,
203
- path: string,
337
+ at: At,
204
338
  depth: number,
205
339
  ): Projection {
206
- if (matches(id, value, table)) return { value, dropped: 0 }
207
340
  const shape = table[id]!
208
- const failure: Unprojectable = {
209
- path,
210
- depth,
211
- expected: shape.kind === 'literal' ? JSON.stringify(shape.value) : shape.kind,
212
- }
341
+ const composite = shape.kind === 'union' || shape.kind === 'array' || shape.kind === 'object'
342
+ if (!composite && matches(id, value, table)) return { value, dropped: 0 }
343
+ const failure = (): Unprojectable => ({ at, depth, shape })
213
344
  if (shape.kind === 'union') {
214
- let best: Projection | undefined
215
- for (const item of shape.items as string[]) {
216
- const next = project(table, item, value, path, depth)
217
- if (!failed(next)) {
218
- if (!best || failed(best) || next.dropped < best.dropped) best = next
219
- } else if (!best || (failed(best) && next.depth > best.depth)) best = next
220
- }
221
- return best ?? failure
345
+ // Members that provably cannot succeed are skipped; when nothing succeeds,
346
+ // the unfiltered trial reproduces the deepest-failure diagnostic exactly.
347
+ const items = shape.items as string[]
348
+ const candidates = unionCandidates(table, shape, value)
349
+ const best = projectUnion(table, candidates, value, at, depth)
350
+ if (best && !failed(best)) return best
351
+ return projectUnion(table, items, value, at, depth) ?? failure()
222
352
  }
223
353
  if (shape.kind === 'array' && Array.isArray(value)) {
224
354
  const items: unknown[] = []
225
355
  let dropped = 0
226
356
  for (let i = 0; i < value.length; i++) {
227
- const next = project(table, shape.items as string, value[i], `${path}[${i}]`, depth + 1)
357
+ const next = project(table, shape.items as string, value[i], { up: at, key: i }, depth + 1)
228
358
  if (failed(next)) return next
229
359
  items.push(next.value)
230
360
  dropped += next.dropped
231
361
  }
232
- return { value: items, dropped }
362
+ return { value: dropped ? items : value, dropped }
233
363
  }
234
364
  if (
235
365
  shape.kind === 'object' &&
@@ -240,27 +370,50 @@ function project(
240
370
  const record = value as Record<string, unknown>
241
371
  const out: Record<string, unknown> = {}
242
372
  let dropped = 0
243
- for (const [key, prop] of Object.entries(shape.props!)) {
373
+ for (const [key, prop] of propEntries(shape)) {
244
374
  if (!(key in record) && prop.optional) continue
245
- const next = project(table, prop.type, record[key], property(path, key), depth + 1)
375
+ const next = project(table, prop.type, record[key], { up: at, key }, depth + 1)
246
376
  if (failed(next)) return next
247
377
  if (key in record) out[key] = next.value
248
378
  dropped += next.dropped
249
379
  }
250
- for (const key of Object.keys(record).sort()) {
251
- if (Object.hasOwn(shape.props!, key)) continue
380
+ const extra = Object.keys(record).filter((key) => !Object.hasOwn(shape.props!, key))
381
+ if (extra.length > 1) extra.sort()
382
+ for (const key of extra) {
252
383
  if (!shape.index) {
253
384
  dropped += 1
254
385
  continue
255
386
  }
256
- const next = project(table, shape.index, record[key], property(path, key), depth + 1)
387
+ const next = project(table, shape.index, record[key], { up: at, key }, depth + 1)
257
388
  if (failed(next)) return next
258
389
  out[key] = next.value
259
390
  dropped += next.dropped
260
391
  }
261
- return { value: out, dropped }
392
+ return { value: dropped ? out : value, dropped }
262
393
  }
263
- return failure
394
+ return failure()
395
+ }
396
+
397
+ function projectUnion(
398
+ table: Record<string, Shape>,
399
+ items: readonly string[],
400
+ value: unknown,
401
+ at: At,
402
+ depth: number,
403
+ ): Projection | undefined {
404
+ let best: Projection | undefined
405
+ for (const item of items) {
406
+ const next = project(table, item, value, at, depth)
407
+ if (!failed(next)) {
408
+ if (!best || failed(best) || next.dropped < best.dropped) best = next
409
+ } else if (!best || (failed(best) && next.depth > best.depth)) best = next
410
+ }
411
+ return best
412
+ }
413
+
414
+ /** Whether schema `id` of `catalog` can represent `value` after projection. */
415
+ export function projectsOnto(catalog: ReducerValueCatalog, id: string, value: unknown): boolean {
416
+ return !failed(project(catalog.schemas, id, value, null, 0))
264
417
  }
265
418
 
266
419
  /** The reader-side contract for a server's canonical answer: the value as this
@@ -273,8 +426,10 @@ export function projectReducerValue(
273
426
  ): { value: unknown } | ValidationFailure {
274
427
  const root = Object.hasOwn(catalog.roots, name) ? catalog.roots[name] : undefined
275
428
  if (!root) return { path: '$', expected: 'known reducer' }
276
- const result = project(catalog.schemas, root.value, value, '$', 0)
277
- return failed(result) ? { path: result.path, expected: result.expected } : { value: result.value }
429
+ const result = project(catalog.schemas, root.value, value, null, 0)
430
+ return failed(result)
431
+ ? { path: render(result.at), expected: expected(result.shape) }
432
+ : { value: result.value }
278
433
  }
279
434
 
280
435
  /** Existing optional object fields keep their ordinary JSON omission semantics.