@defrex/autobuild 1.2.0 → 1.3.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 (82) hide show
  1. package/README.md +1 -1
  2. package/SPEC.md +118 -14
  3. package/docs/accounts.md +12 -1
  4. package/docs/answering-blocked-builds.md +4 -1
  5. package/docs/architecture.md +120 -7
  6. package/docs/builds.md +2 -0
  7. package/docs/configuration-overview.md +6 -0
  8. package/docs/configuration.md +6 -0
  9. package/docs/integration-tree.md +16 -5
  10. package/docs/operator-api.md +44 -6
  11. package/docs/operator-surfaces.md +29 -0
  12. package/docs/plugin-authoring.md +6 -1
  13. package/docs/remote-store-protocol.md +69 -10
  14. package/docs/restacking.md +45 -0
  15. package/docs/spec-standard.md +4 -2
  16. package/docs/ticket-dependencies.md +8 -0
  17. package/package.json +3 -3
  18. package/packages/core/src/cli/dispatch-frontend.ts +1 -0
  19. package/packages/core/src/cli/dispatch-repository-feed.ts +107 -26
  20. package/packages/core/src/cli/dispatch.ts +64 -25
  21. package/packages/core/src/cli/dispatcher-liveness.ts +6 -1
  22. package/packages/core/src/cli/forge-clear.ts +30 -0
  23. package/packages/core/src/cli/help.ts +1 -0
  24. package/packages/core/src/cli/main.ts +28 -0
  25. package/packages/core/src/cli/watch.ts +9 -13
  26. package/packages/core/src/events/catalog.ts +1 -0
  27. package/packages/core/src/events/forge-degradation-policy.ts +11 -0
  28. package/packages/core/src/events/payloads.ts +10 -0
  29. package/packages/core/src/events/policy.ts +9 -0
  30. package/packages/core/src/events/repository.ts +73 -2
  31. package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +275 -0
  32. package/packages/core/src/kernel/change-presence.ts +74 -8
  33. package/packages/core/src/kernel/engine.ts +8 -0
  34. package/packages/core/src/kernel/generators/node-restack-log.ts +14 -0
  35. package/packages/core/src/kernel/node-polling.ts +40 -7
  36. package/packages/core/src/kernel/node-restack-demand.ts +74 -0
  37. package/packages/core/src/kernel/node-restack-state.ts +5 -1
  38. package/packages/core/src/kernel/node-restack-transaction.ts +33 -1
  39. package/packages/core/src/kernel/read-facts.ts +6 -0
  40. package/packages/core/src/kernel/reducer-registry.ts +3 -0
  41. package/packages/core/src/kernel/reducer.ts +1 -1
  42. package/packages/core/src/kernel/ticket-preparation.ts +66 -0
  43. package/packages/core/src/operator/annotations.ts +6 -0
  44. package/packages/core/src/operator/control.ts +20 -0
  45. package/packages/core/src/operator/index.ts +2 -0
  46. package/packages/core/src/operator/registry.ts +20 -0
  47. package/packages/core/src/ports/runner/account-env.ts +60 -9
  48. package/packages/core/src/ports/runner/claude.ts +49 -45
  49. package/packages/core/src/processes/dispatcher.ts +470 -121
  50. package/packages/core/src/processes/execution-no-progress.ts +1 -0
  51. package/packages/core/src/processes/harvest.ts +11 -16
  52. package/packages/core/src/processes/repo-view.ts +3 -0
  53. package/packages/core/src/store/artifact-meta.ts +7 -0
  54. package/packages/core/src/store/contract.ts +57 -0
  55. package/packages/core/src/store/memory.ts +4 -0
  56. package/packages/core/src/store/reducer-value-shapes.json +7302 -6668
  57. package/packages/core/src/store/reducer-value-validation.ts +106 -6
  58. package/packages/core/src/store/remote/client.ts +48 -8
  59. package/packages/core/src/store/remote/compatibility.ts +39 -0
  60. package/packages/core/src/store/remote/index.ts +6 -0
  61. package/packages/core/src/store/remote/protocol.ts +8 -1
  62. package/packages/core/src/store/remote/released-reducer-values/1.2.0.json +8129 -0
  63. package/packages/core/src/store/remote/version.ts +4 -0
  64. package/packages/core/src/store/repo-state-events.ts +4 -2
  65. package/packages/core/src/store/snapshots.ts +16 -3
  66. package/packages/core/src/store/types.ts +2 -0
  67. package/packages/core/src/testing/dispatch-read-budget.ts +144 -5
  68. package/packages/core/src/testing/index.ts +1 -1
  69. package/packages/core/src/testing/reduced-read-budget.ts +513 -5
  70. package/skills/guide/SKILL.md +44 -7
  71. package/skills/guide/references/accounts.md +12 -1
  72. package/skills/guide/references/integration-tree.md +16 -5
  73. package/skills/guide/references/operator-api.md +44 -6
  74. package/skills/guide/references/plugin-authoring.md +6 -1
  75. package/skills/guide/references/remote-store-protocol.md +69 -10
  76. package/skills/guide/references/restacking.md +45 -0
  77. package/skills/guide/references/spec-standard.md +4 -2
  78. package/skills/guide/references/ticket-dependencies.md +8 -0
  79. package/skills/operate/SKILL.md +21 -5
  80. package/skills/reconcile/SKILL.md +6 -0
  81. package/skills/tickets/SKILL.md +7 -3
  82. package/templates/hosted-service/package.json +2 -2
@@ -10,6 +10,13 @@ type Shape = {
10
10
  }
11
11
  const schemas: Record<string, Shape> = shapes.schemas
12
12
 
13
+ /** A finished-value shape catalog: this build's generated one, or a frozen
14
+ * catalog from a published release line. */
15
+ export interface ReducerValueCatalog {
16
+ roots: Record<string, { value: string }>
17
+ schemas: Record<string, Shape>
18
+ }
19
+
13
20
  /** Generated structural shapes include nested event payload unions, records,
14
21
  * optional fields and literal discriminants. No accumulator is trusted merely
15
22
  * because finish() happens to accept it. */
@@ -37,8 +44,8 @@ function json(value: unknown, seen = new Set<object>()): boolean {
37
44
  Object.values(value).every((item) => json(item, new Set(seen)))
38
45
  )
39
46
  }
40
- function matches(id: string, value: unknown): boolean {
41
- const shape = schemas[id]!
47
+ function matches(id: string, value: unknown, table = schemas): boolean {
48
+ const shape = table[id]!
42
49
  switch (shape.kind) {
43
50
  case 'json':
44
51
  return json(value)
@@ -53,9 +60,11 @@ function matches(id: string, value: unknown): boolean {
53
60
  case 'boolean':
54
61
  return typeof value === 'boolean'
55
62
  case 'union':
56
- return (shape.items as string[]).some((item) => matches(item, value))
63
+ return (shape.items as string[]).some((item) => matches(item, value, table))
57
64
  case 'array':
58
- return Array.isArray(value) && value.every((item) => matches(shape.items as string, item))
65
+ return (
66
+ Array.isArray(value) && value.every((item) => matches(shape.items as string, item, table))
67
+ )
59
68
  case 'object': {
60
69
  if (
61
70
  value === null ||
@@ -67,12 +76,12 @@ function matches(id: string, value: unknown): boolean {
67
76
  const record = value as Record<string, unknown>
68
77
  for (const [key, prop] of Object.entries(shape.props!)) {
69
78
  if (!(key in record) && prop.optional) continue
70
- if (!matches(prop.type, record[key])) return false
79
+ if (!matches(prop.type, record[key], table)) return false
71
80
  }
72
81
  return Object.keys(record).every(
73
82
  (key) =>
74
83
  Object.hasOwn(shape.props!, key) ||
75
- (shape.index !== undefined && matches(shape.index, record[key])),
84
+ (shape.index !== undefined && matches(shape.index, record[key], table)),
76
85
  )
77
86
  }
78
87
  default:
@@ -174,6 +183,97 @@ function diagnose(id: string, value: unknown, path: string, depth: number): Diag
174
183
  return failure
175
184
  }
176
185
 
186
+ type Projected = { value: unknown; dropped: number }
187
+ type Unprojectable = Diagnostic
188
+ type Projection = Projected | Unprojectable
189
+ // Plugin consumers may compile without strict narrowing; `in` guards narrow everywhere.
190
+ const failed = (result: Projection): result is Unprojectable => 'path' in result
191
+
192
+ /** Project a value onto a catalog's shape, dropping object properties the shape
193
+ * does not know. Additive server fields then reach older readers as the value
194
+ * they were built for, while a missing field or unknown variant still fails.
195
+ * Among union members, the one dropping the fewest properties wins. */
196
+ function project(
197
+ table: Record<string, Shape>,
198
+ id: string,
199
+ value: unknown,
200
+ path: string,
201
+ depth: number,
202
+ ): Projection {
203
+ if (matches(id, value, table)) return { value, dropped: 0 }
204
+ const shape = table[id]!
205
+ const failure: Unprojectable = {
206
+ path,
207
+ depth,
208
+ expected: shape.kind === 'literal' ? JSON.stringify(shape.value) : shape.kind,
209
+ }
210
+ if (shape.kind === 'union') {
211
+ let best: Projection | undefined
212
+ for (const item of shape.items as string[]) {
213
+ const next = project(table, item, value, path, depth)
214
+ if (!failed(next)) {
215
+ if (!best || failed(best) || next.dropped < best.dropped) best = next
216
+ } else if (!best || (failed(best) && next.depth > best.depth)) best = next
217
+ }
218
+ return best ?? failure
219
+ }
220
+ if (shape.kind === 'array' && Array.isArray(value)) {
221
+ const items: unknown[] = []
222
+ let dropped = 0
223
+ for (let i = 0; i < value.length; i++) {
224
+ const next = project(table, shape.items as string, value[i], `${path}[${i}]`, depth + 1)
225
+ if (failed(next)) return next
226
+ items.push(next.value)
227
+ dropped += next.dropped
228
+ }
229
+ return { value: items, dropped }
230
+ }
231
+ if (
232
+ shape.kind === 'object' &&
233
+ value !== null &&
234
+ typeof value === 'object' &&
235
+ Object.getPrototypeOf(value) === Object.prototype
236
+ ) {
237
+ const record = value as Record<string, unknown>
238
+ const out: Record<string, unknown> = {}
239
+ let dropped = 0
240
+ for (const [key, prop] of Object.entries(shape.props!)) {
241
+ if (!(key in record) && prop.optional) continue
242
+ const next = project(table, prop.type, record[key], property(path, key), depth + 1)
243
+ if (failed(next)) return next
244
+ if (key in record) out[key] = next.value
245
+ dropped += next.dropped
246
+ }
247
+ for (const key of Object.keys(record).sort()) {
248
+ if (Object.hasOwn(shape.props!, key)) continue
249
+ if (!shape.index) {
250
+ dropped += 1
251
+ continue
252
+ }
253
+ const next = project(table, shape.index, record[key], property(path, key), depth + 1)
254
+ if (failed(next)) return next
255
+ out[key] = next.value
256
+ dropped += next.dropped
257
+ }
258
+ return { value: out, dropped }
259
+ }
260
+ return failure
261
+ }
262
+
263
+ /** The reader-side contract for a server's canonical answer: the value as this
264
+ * reader's shape can represent it, or a structural diagnostic. Reducer versions
265
+ * are cache identities, not compatibility gates. */
266
+ export function projectReducerValue(
267
+ name: string,
268
+ value: unknown,
269
+ catalog: ReducerValueCatalog = shapes,
270
+ ): { value: unknown } | ValidationFailure {
271
+ const root = Object.hasOwn(catalog.roots, name) ? catalog.roots[name] : undefined
272
+ if (!root) return { path: '$', expected: 'known reducer' }
273
+ const result = project(catalog.schemas, root.value, value, '$', 0)
274
+ return failed(result) ? { path: result.path, expected: result.expected } : { value: result.value }
275
+ }
276
+
177
277
  /** Existing optional object fields keep their ordinary JSON omission semantics.
178
278
  * Collections, functions, nonfinite numbers and undefined array entries cannot
179
279
  * silently become a different public contract during JSON serialization. */
@@ -1,3 +1,4 @@
1
+ import { ArtifactMetaUnsupportedError } from '../artifact-meta'
1
2
  import {
2
3
  operatorPresenceBodySchema,
3
4
  operatorPresenceWireSchema,
@@ -17,8 +18,8 @@ import {
17
18
  } from './compatibility'
18
19
  import { stampCompatibility, buildEventPolicy, repositoryEventPolicy } from '../../events/policy'
19
20
  import { READ_CATALOG_IDENTITY } from '../../events/compatibility'
20
- import { CATALOG_IDENTITY_HEADER } from './version'
21
- import { diagnoseReducerValue } from '../reducer-value-validation'
21
+ import { CATALOG_IDENTITY_HEADER, REDUCER_VALUE_PROJECTION_HEADER } from './version'
22
+ import { projectReducerValue } from '../reducer-value-validation'
22
23
  import { prepareEventWrite as validateEventWrite } from '../../events/catalog'
23
24
  import { prepareRepositoryEventWrite as validateRepositoryEventWrite } from '../../events/repository'
24
25
  import {
@@ -273,8 +274,10 @@ export class RemoteBuildStore implements BuildStore {
273
274
  path: string,
274
275
  body?: unknown,
275
276
  signal?: AbortSignal,
277
+ capabilities: Record<string, string> = {},
276
278
  ): Promise<Response> {
277
279
  const headers: Record<string, string> = {
280
+ ...capabilities,
278
281
  [AUTOBUILD_VERSION_HEADER]: this.identity.autobuildVersion,
279
282
  [REMOTE_STORE_PROTOCOL_VERSION_HEADER]: this.identity.protocolVersion,
280
283
  }
@@ -311,7 +314,8 @@ export class RemoteBuildStore implements BuildStore {
311
314
  ) {
312
315
  return new TicketAssetValidationError(message)
313
316
  }
314
- if (response.status === 422) return new EventValidationError(message)
317
+ if (response.status === 422 || (response.status === 400 && body?.kind === 'validation'))
318
+ return new EventValidationError(message)
315
319
  // Typed stream rejections rehydrate by the server's message shape: the
316
320
  // ceiling names its bytes, the closed-append names its stream (a 409 is
317
321
  // otherwise the generic already-exists conflict).
@@ -558,6 +562,38 @@ export class RemoteBuildStore implements BuildStore {
558
562
  return { meta: result.meta, content: decodeBase64(result.contentBase64) }
559
563
  }
560
564
 
565
+ private artifactMetaUnsupported = false
566
+
567
+ async getLatestArtifactMeta(slug: string, kind: string): Promise<ArtifactMeta | null> {
568
+ if (this.artifactMetaUnsupported) throw new ArtifactMetaUnsupportedError()
569
+ const response = await this.raw(
570
+ 'GET',
571
+ `${this.buildPath(slug)}/artifact-meta?${new URLSearchParams({ kind })}`,
572
+ )
573
+ if (!response.ok) {
574
+ const body = errorBodySchema.safeParse(
575
+ await response
576
+ .clone()
577
+ .json()
578
+ .catch(() => null),
579
+ )
580
+ if (
581
+ body.success &&
582
+ ((response.status === 501 && body.data.kind === 'unsupported-operation') ||
583
+ (response.status === 404 &&
584
+ body.data.kind === 'not-found' &&
585
+ (body.data.error === 'no route: GET /builds/:slug/artifact-meta' ||
586
+ body.data.error ===
587
+ `no route: GET ${new URL(`${this.base}${this.buildPath(slug)}/artifact-meta`).pathname}`)))
588
+ ) {
589
+ this.artifactMetaUnsupported = true
590
+ throw new ArtifactMetaUnsupportedError()
591
+ }
592
+ throw await this.toError(response)
593
+ }
594
+ return artifactMetaWireSchema.nullable().parse(await response.json())
595
+ }
596
+
561
597
  async listArtifacts(slug: string, kind?: string): Promise<ArtifactMeta[]> {
562
598
  const query = kind !== undefined ? `?kind=${encodeURIComponent(kind)}` : ''
563
599
  return this.requestJson(
@@ -757,7 +793,9 @@ export class RemoteBuildStore implements BuildStore {
757
793
  async getReducerValue(scope: SnapshotScope, request: ReducerValueRequest): Promise<ReducerValue> {
758
794
  const { reducer } = resolveReducerValue(scope, request)
759
795
  const root = scope.kind === 'build' ? this.buildPath(scope.slug) : this.repoPath(scope.repo)
760
- const response = await this.raw('POST', `${root}/reducer-values`, request)
796
+ const response = await this.raw('POST', `${root}/reducer-values`, request, undefined, {
797
+ [REDUCER_VALUE_PROJECTION_HEADER]: '1',
798
+ })
761
799
  if (!response.ok) throw await this.toError(response)
762
800
  let body: unknown
763
801
  try {
@@ -788,14 +826,16 @@ export class RemoteBuildStore implements BuildStore {
788
826
  }
789
827
  if (!parsed.success) fail(`invalid reducer envelope: ${parsed.error.message}`)
790
828
  const result = parsed.data!
791
- if (result.version !== reducer.version) fail('reducer version mismatch')
792
- const failure = diagnoseReducerValue(request.reducer, result.value)
793
- if (failure) fail(`invalid finished value at ${failure.path}: expected ${failure.expected}`)
829
+ // The shape this reader can represent is the contract. A different reducer
830
+ // version is the server's cache identity, so release lines may skew freely.
831
+ const projected = projectReducerValue(request.reducer, result.value)
832
+ if ('path' in projected)
833
+ fail(`invalid finished value at ${projected.path}: expected ${projected.expected}`)
794
834
  const identity = response.headers.get(CATALOG_IDENTITY_HEADER)
795
835
  return {
796
836
  version: result.version,
797
837
  cursor: result.cursor,
798
- value: result.value as ReducerValue['value'],
838
+ value: (projected as { value: unknown }).value as ReducerValue['value'],
799
839
  ...(identity === null ? {} : { catalogIdentity: identity }),
800
840
  }
801
841
  }
@@ -1,6 +1,8 @@
1
1
  import semver from 'semver'
2
2
  import { z } from 'zod'
3
3
  import { AUTOBUILD_VERSION, REMOTE_STORE_PROTOCOL_VERSION } from './version'
4
+ import { projectReducerValue, type ReducerValueCatalog } from '../reducer-value-validation'
5
+ import released120 from './released-reducer-values/1.2.0.json'
4
6
 
5
7
  /** Stable releases only: prefixes, whitespace, prereleases and build suffixes are not identities. */
6
8
  export function stableVersion(value: unknown): semver.SemVer | undefined {
@@ -82,6 +84,8 @@ export const RELEASE_LINE_PROTOCOLS: Readonly<Record<string, string>> = {
82
84
  '1.0': '4',
83
85
  // v1.1.x shipped protocol 6; retain it when this line becomes historical.
84
86
  '1.1': '6',
87
+ // v1.2.x shipped protocol 6; retain it when this line becomes historical.
88
+ '1.2': '6',
85
89
  '0.9': '3',
86
90
  '0.10': '3',
87
91
  '0.11': '3',
@@ -126,3 +130,38 @@ export class ReducerValueIncompatibilityError extends Error {
126
130
  )
127
131
  }
128
132
  }
133
+
134
+ interface ReleasedReducerValues extends ReducerValueCatalog {
135
+ versions: Record<string, number>
136
+ }
137
+
138
+ /** Published releases whose clients accept only their own reducer version and
139
+ * finished shape. Each catalog is frozen from its release and retained while
140
+ * that release is in the supported window, like `RELEASE_LINE_PROTOCOLS`. Later
141
+ * clients send `REDUCER_VALUE_PROJECTION_HEADER` and project for themselves. */
142
+ const EXACT_REDUCER_VALUE_RELEASES: Readonly<Record<string, ReleasedReducerValues>> = {
143
+ '1.2.0': released120 as ReleasedReducerValues,
144
+ }
145
+
146
+ /** Answer an exact-version client as its release can read: project the canonical
147
+ * value onto the release's frozen shape and stamp the version it expects. A value
148
+ * the release cannot represent is returned unchanged, so the client raises its
149
+ * typed incompatibility error rather than misreading it. */
150
+ export function reducerValueForClient<T extends { version: number; value: unknown }>(
151
+ client: { version: string | null; projects: boolean },
152
+ reducer: string,
153
+ answer: T,
154
+ ): T {
155
+ const { version: release } = client
156
+ if (client.projects || release === null) return answer
157
+ if (!Object.hasOwn(EXACT_REDUCER_VALUE_RELEASES, release)) return answer
158
+ const catalog = EXACT_REDUCER_VALUE_RELEASES[release]!
159
+ if (!Object.hasOwn(catalog.versions, reducer)) return answer
160
+ const projected = projectReducerValue(reducer, answer.value, catalog)
161
+ if ('path' in projected) return answer
162
+ return {
163
+ ...answer,
164
+ version: catalog.versions[reducer]!,
165
+ value: (projected as { value: unknown }).value,
166
+ }
167
+ }
@@ -14,6 +14,7 @@ export type {
14
14
  } from './token'
15
15
  export {
16
16
  CATALOG_IDENTITY_HEADER,
17
+ REDUCER_VALUE_PROJECTION_HEADER,
17
18
  AUTOBUILD_VERSION,
18
19
  AUTOBUILD_VERSION_HEADER,
19
20
  REMOTE_STORE_PROTOCOL_VERSION,
@@ -56,3 +57,8 @@ export type { BuildDelegationFacts, HarvestDelegationFacts } from '../../kernel/
56
57
  export { delegationFactKey } from '../../kernel/service-facts'
57
58
 
58
59
  export { assertPublicSnapshotReducer } from '../snapshots'
60
+
61
+ export {
62
+ validateForgeDegradation,
63
+ MAX_FORGE_DEGRADATION_MS,
64
+ } from '../../events/forge-degradation-policy'
@@ -39,7 +39,14 @@ import type { ArtifactMeta, RepositoryArtifactMeta, SessionArtifactMeta } from '
39
39
 
40
40
  // ── Errors (D6: errors as feedback over the wire) ────────────────────────────
41
41
 
42
- export const errorKindSchema = z.enum(['validation', 'not-found', 'auth', 'conflict', 'internal'])
42
+ export const errorKindSchema = z.enum([
43
+ 'validation',
44
+ 'not-found',
45
+ 'auth',
46
+ 'conflict',
47
+ 'internal',
48
+ 'unsupported-operation',
49
+ ])
43
50
  export type ErrorKind = z.infer<typeof errorKindSchema>
44
51
 
45
52
  export const errorBodySchema = z.object({