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