@defrex/autobuild 1.0.0-beta.1 → 1.0.0-beta.3

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 (172) hide show
  1. package/SPEC.md +310 -56
  2. package/bin/ab-build-runner.ts +10 -8
  3. package/docs/architecture.md +177 -23
  4. package/docs/configuration.md +35 -4
  5. package/docs/hosted-service.md +35 -0
  6. package/docs/integration-tree.md +83 -4
  7. package/docs/landing-policy.md +11 -0
  8. package/docs/migration-1.0.md +9 -0
  9. package/docs/operator-api.md +18 -0
  10. package/docs/remote-store-protocol.md +16 -0
  11. package/docs/remote-ticket-protocol.md +5 -0
  12. package/docs/restacking.md +42 -7
  13. package/docs/setup.md +25 -0
  14. package/docs/spec-standard.md +71 -0
  15. package/docs/ticket-dependencies.md +6 -45
  16. package/docs/ticket-migration.md +50 -4
  17. package/package.json +3 -3
  18. package/packages/core/src/cli/attention.ts +4 -1
  19. package/packages/core/src/cli/binary.ts +17 -1
  20. package/packages/core/src/cli/build-control.ts +3 -3
  21. package/packages/core/src/cli/context.ts +53 -1
  22. package/packages/core/src/cli/dashboard/actions.ts +4 -0
  23. package/packages/core/src/cli/dashboard/detail.ts +45 -1
  24. package/packages/core/src/cli/dashboard/facts.ts +7 -2
  25. package/packages/core/src/cli/dashboard/integration-detail.ts +144 -0
  26. package/packages/core/src/cli/dashboard/integration-row.ts +255 -0
  27. package/packages/core/src/cli/dashboard/landing-display.ts +64 -0
  28. package/packages/core/src/cli/dashboard/model.ts +147 -16
  29. package/packages/core/src/cli/dashboard/poll.ts +40 -4
  30. package/packages/core/src/cli/dashboard/render.ts +106 -5
  31. package/packages/core/src/cli/dashboard/transcript.ts +2 -1
  32. package/packages/core/src/cli/dispatch-frontend.ts +11 -0
  33. package/packages/core/src/cli/dispatch-repository-feed.ts +2 -1
  34. package/packages/core/src/cli/dispatch.ts +49 -14
  35. package/packages/core/src/cli/help.ts +17 -1
  36. package/packages/core/src/cli/main.ts +48 -8
  37. package/packages/core/src/cli/publication-settlement.ts +49 -5
  38. package/packages/core/src/cli/repository-status.ts +103 -2
  39. package/packages/core/src/cli/status.ts +24 -1
  40. package/packages/core/src/cli/terminals.ts +94 -93
  41. package/packages/core/src/cli/ticket.ts +35 -11
  42. package/packages/core/src/cli/wait.ts +11 -2
  43. package/packages/core/src/cli/watch.ts +12 -0
  44. package/packages/core/src/config/schema.ts +3 -0
  45. package/packages/core/src/events/catalog.ts +38 -0
  46. package/packages/core/src/events/compatibility.ts +183 -0
  47. package/packages/core/src/events/native-session.ts +13 -0
  48. package/packages/core/src/events/payloads.ts +150 -2
  49. package/packages/core/src/events/policy.ts +210 -0
  50. package/packages/core/src/events/repository.ts +69 -0
  51. package/packages/core/src/integration/harness.ts +4 -4
  52. package/packages/core/src/kernel/__snapshots__/{reducer-registry.test.ts.snap → reducer-accumulator-shapes.json} +4286 -4006
  53. package/packages/core/src/kernel/change-presence.ts +11 -3
  54. package/packages/core/src/kernel/dispatch-capacity.ts +127 -0
  55. package/packages/core/src/kernel/dispatcher-liveness.ts +5 -1
  56. package/packages/core/src/kernel/engine.ts +25 -5
  57. package/packages/core/src/kernel/forge-health.ts +19 -0
  58. package/packages/core/src/kernel/generators/build-log.ts +18 -0
  59. package/packages/core/src/kernel/generators/node-restack-log.ts +1 -1
  60. package/packages/core/src/kernel/generators/repository-journals.ts +10 -0
  61. package/packages/core/src/kernel/guarded-forge.ts +159 -0
  62. package/packages/core/src/kernel/incremental.ts +4 -2
  63. package/packages/core/src/kernel/integration-completeness.ts +108 -9
  64. package/packages/core/src/kernel/integration-landing.ts +5 -4
  65. package/packages/core/src/kernel/janitor-failure.ts +36 -0
  66. package/packages/core/src/kernel/landing-publication.ts +35 -15
  67. package/packages/core/src/kernel/landing-state.ts +7 -0
  68. package/packages/core/src/kernel/leaf-restack-state.ts +1 -1
  69. package/packages/core/src/kernel/log-index.ts +12 -1
  70. package/packages/core/src/kernel/mixed-version.ts +52 -0
  71. package/packages/core/src/kernel/node-landing.ts +14 -11
  72. package/packages/core/src/kernel/node-parent.ts +35 -10
  73. package/packages/core/src/kernel/node-restack-demand.ts +29 -0
  74. package/packages/core/src/kernel/node-restack-state.ts +46 -16
  75. package/packages/core/src/kernel/node-restack-transaction.ts +151 -8
  76. package/packages/core/src/kernel/node-restack.ts +34 -10
  77. package/packages/core/src/kernel/publication.ts +22 -0
  78. package/packages/core/src/kernel/read-deferrals.ts +111 -0
  79. package/packages/core/src/kernel/read-facts.ts +34 -6
  80. package/packages/core/src/kernel/reconcile-restack.ts +23 -2
  81. package/packages/core/src/kernel/reducer-registry.ts +18 -2
  82. package/packages/core/src/kernel/reducer.ts +196 -10
  83. package/packages/core/src/kernel/repository-attention.ts +38 -1
  84. package/packages/core/src/kernel/terminal-guard.ts +11 -4
  85. package/packages/core/src/kernel/ticket-migrations.ts +139 -0
  86. package/packages/core/src/operator/annotations.ts +6 -0
  87. package/packages/core/src/operator/index.ts +2 -0
  88. package/packages/core/src/operator/presentation.ts +5 -0
  89. package/packages/core/src/operator/query.ts +51 -11
  90. package/packages/core/src/operator/registry.ts +17 -1
  91. package/packages/core/src/operator/restack.ts +51 -0
  92. package/packages/core/src/operator/tickets.ts +30 -26
  93. package/packages/core/src/plugin-sdk/index.ts +10 -0
  94. package/packages/core/src/plugins/manifest.ts +1 -1
  95. package/packages/core/src/ports/forge/contract.ts +23 -0
  96. package/packages/core/src/ports/forge/fake.ts +6 -2
  97. package/packages/core/src/ports/forge/git-rewrite.ts +92 -64
  98. package/packages/core/src/ports/forge/github-transport.ts +45 -2
  99. package/packages/core/src/ports/forge/github.ts +56 -17
  100. package/packages/core/src/ports/forge/local-git.ts +7 -2
  101. package/packages/core/src/ports/forge-failure.ts +40 -0
  102. package/packages/core/src/ports/forge-request-context.ts +35 -0
  103. package/packages/core/src/ports/forge-telemetry.ts +16 -0
  104. package/packages/core/src/ports/runner/claude.ts +136 -9
  105. package/packages/core/src/ports/runner/codex.ts +112 -7
  106. package/packages/core/src/ports/runner/contract.ts +78 -0
  107. package/packages/core/src/ports/runner/fake.ts +59 -1
  108. package/packages/core/src/ports/runner/native-cli-fixture.ts +86 -0
  109. package/packages/core/src/ports/runner/native-process-fixture.ts +38 -0
  110. package/packages/core/src/ports/runner/native-session.ts +110 -0
  111. package/packages/core/src/ports/runner/pi-rpc.ts +7 -1
  112. package/packages/core/src/ports/runner/pi.ts +162 -2
  113. package/packages/core/src/ports/tickets/linear.ts +180 -12
  114. package/packages/core/src/ports/tickets/migration.ts +92 -9
  115. package/packages/core/src/ports/types.ts +60 -5
  116. package/packages/core/src/ports/workspace/git-worktree.ts +45 -51
  117. package/packages/core/src/ports/workspace/vercel-sandbox.ts +28 -12
  118. package/packages/core/src/processes/build-child-exit.ts +50 -2
  119. package/packages/core/src/processes/build-child.ts +15 -7
  120. package/packages/core/src/processes/build-runner.ts +909 -213
  121. package/packages/core/src/processes/dispatcher.ts +815 -369
  122. package/packages/core/src/processes/execution-no-progress.ts +15 -0
  123. package/packages/core/src/processes/pause-execution-fixture.ts +37 -0
  124. package/packages/core/src/processes/phase-failure-budget.ts +2 -1
  125. package/packages/core/src/processes/publication-loss.ts +7 -3
  126. package/packages/core/src/processes/repo-view.ts +53 -6
  127. package/packages/core/src/processes/tick-publication.ts +6 -2
  128. package/packages/core/src/runtime-identity.ts +37 -0
  129. package/packages/core/src/store/adapter.ts +4 -2
  130. package/packages/core/src/store/build-scope.ts +9 -0
  131. package/packages/core/src/store/contract.ts +233 -16
  132. package/packages/core/src/store/digest.ts +20 -2
  133. package/packages/core/src/store/local/schema.ts +6 -0
  134. package/packages/core/src/store/local/store.ts +57 -4
  135. package/packages/core/src/store/memory.ts +83 -2
  136. package/packages/core/src/store/new-build.ts +2 -2
  137. package/packages/core/src/store/phase-session.ts +13 -0
  138. package/packages/core/src/store/reduced-reads.ts +43 -4
  139. package/packages/core/src/store/reducer-value-shapes.json +8911 -6344
  140. package/packages/core/src/store/reducer-value-validation.ts +103 -3
  141. package/packages/core/src/store/reducer-values.ts +21 -6
  142. package/packages/core/src/store/remote/client.ts +145 -20
  143. package/packages/core/src/store/remote/index.ts +2 -0
  144. package/packages/core/src/store/remote/protocol.ts +19 -0
  145. package/packages/core/src/store/remote/version.ts +2 -0
  146. package/packages/core/src/store/repo-state-events.ts +5 -2
  147. package/packages/core/src/store/repo-tick.ts +6 -1
  148. package/packages/core/src/store/session-handle.ts +8 -0
  149. package/packages/core/src/store/snapshots.ts +21 -5
  150. package/packages/core/src/store/types.ts +17 -1
  151. package/packages/core/src/testing/dispatch-read-budget.ts +18 -2
  152. package/packages/core/src/testing/index.ts +7 -0
  153. package/packages/core/src/testing/newer-writer.ts +37 -0
  154. package/packages/core/src/testing/reduced-read-budget.ts +3 -0
  155. package/skills/guide/SKILL.md +140 -25
  156. package/skills/guide/references/integration-tree.md +83 -4
  157. package/skills/guide/references/landing-policy.md +11 -0
  158. package/skills/guide/references/migration-1.0.md +9 -0
  159. package/skills/guide/references/operator-api.md +18 -0
  160. package/skills/guide/references/plugin-authoring.md +47 -4
  161. package/skills/guide/references/remote-store-protocol.md +16 -0
  162. package/skills/guide/references/remote-ticket-protocol.md +5 -0
  163. package/skills/guide/references/restacking.md +42 -7
  164. package/skills/guide/references/setup.md +25 -0
  165. package/skills/guide/references/spec-standard.md +71 -0
  166. package/skills/guide/references/ticket-dependencies.md +6 -45
  167. package/skills/guide/references/ticket-migration.md +50 -4
  168. package/skills/implement/SKILL.md +4 -2
  169. package/skills/operate/SKILL.md +63 -6
  170. package/skills/reconcile/SKILL.md +3 -1
  171. package/skills/spec/SKILL.md +62 -40
  172. package/skills/tickets/SKILL.md +15 -43
@@ -0,0 +1,183 @@
1
+ /** Persisted facts are open on reads; producer schemas remain closed on writes. */
2
+ import type { z } from 'zod'
3
+ import type { Actor } from './envelope'
4
+ import type { AbEvent } from './catalog'
5
+ import { eventPayloadSchemas } from './payloads'
6
+ import { repositoryEventPayloadSchemas, type RepositoryEvent } from './repository'
7
+ import {
8
+ buildEventPolicy,
9
+ repositoryEventPolicy,
10
+ compatibilityMetadataSchema,
11
+ type CompatibilityMetadata,
12
+ } from './policy'
13
+
14
+ export interface StoredEvent {
15
+ build?: string
16
+ repo?: string
17
+ seq: number
18
+ ts: string
19
+ actor: Actor
20
+ type: string
21
+ payload: unknown
22
+ compatibility?: CompatibilityMetadata
23
+ }
24
+
25
+ /** Change this revision whenever projection semantics change without new types. */
26
+ export const READ_DECODER_REVISION = 2
27
+ export function catalogIdentity(
28
+ build: Record<string, string> = buildEventPolicy,
29
+ repository: Record<string, string> = repositoryEventPolicy,
30
+ ): string {
31
+ const sorted = (catalog: Record<string, string>) =>
32
+ Object.entries(catalog).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
33
+ return new Bun.CryptoHasher('sha256')
34
+ .update(JSON.stringify([READ_DECODER_REVISION, sorted(build), sorted(repository)]))
35
+ .digest('hex')
36
+ }
37
+ export const READ_CATALOG_IDENTITY = catalogIdentity()
38
+
39
+ /** Remove only keys reported as unknown by the original schema, then run that
40
+ * schema again. Its required fields, enums and refinements remain authoritative. */
41
+ export function projectPayload(schema: z.ZodType, payload: unknown): unknown {
42
+ const projected = structuredClone(payload)
43
+ for (;;) {
44
+ const result = schema.safeParse(projected)
45
+ if (result.success) return result.data
46
+ let removed = false
47
+ for (const issue of removableKeys(result.error.issues)) {
48
+ let object: unknown = projected
49
+ for (const key of issue.path) object = (object as Record<PropertyKey, unknown>)?.[key]
50
+ if (object === null || typeof object !== 'object') continue
51
+ for (const key of issue.keys) {
52
+ if (!Object.hasOwn(object, key)) continue
53
+ delete (object as Record<string, unknown>)[key]
54
+ removed = true
55
+ }
56
+ }
57
+ if (!removed) throw result.error
58
+ }
59
+ }
60
+ type UnknownKeys = { path: PropertyKey[]; keys: string[] }
61
+ function removableKeys(
62
+ issues: readonly z.core.$ZodIssue[],
63
+ prefix: PropertyKey[] = [],
64
+ ): UnknownKeys[] {
65
+ return issues.flatMap((issue): UnknownKeys[] => {
66
+ const path = [...prefix, ...issue.path]
67
+ if (issue.code === 'unrecognized_keys') return [{ path, keys: issue.keys }]
68
+ if (issue.code !== 'invalid_union') return []
69
+ // Choose one branch that would validate after dropping only added fields;
70
+ // never remove the known fields demanded by another union alternative.
71
+ const branches = issue.errors as unknown as readonly (readonly z.core.$ZodIssue[])[]
72
+ const branch = branches.find((errors) => errors.every(onlyAddedFields))
73
+ return branch ? removableKeys(branch, path) : []
74
+ })
75
+ }
76
+ function onlyAddedFields(issue: z.core.$ZodIssue): boolean {
77
+ return (
78
+ issue.code === 'unrecognized_keys' ||
79
+ (issue.code === 'invalid_union' &&
80
+ (issue.errors as unknown as readonly (readonly z.core.$ZodIssue[])[]).some((errors) =>
81
+ errors.every(onlyAddedFields),
82
+ ))
83
+ )
84
+ }
85
+ export function decodeStoredEvent(event: StoredEvent): AbEvent | RepositoryEvent | null {
86
+ const schemas = event.repo === undefined ? eventPayloadSchemas : repositoryEventPayloadSchemas
87
+ const schema = Object.hasOwn(schemas, event.type)
88
+ ? schemas[event.type as keyof typeof schemas]
89
+ : undefined
90
+ const result = schema
91
+ ? ({ ...event, payload: projectPayload(schema, event.payload) } as AbEvent | RepositoryEvent)
92
+ : null
93
+ return result
94
+ }
95
+ export function projectEvents<E>(events: readonly E[]): readonly E[] {
96
+ return events.flatMap((event) => {
97
+ // Session and already-adapted selector rows are a separate vocabulary.
98
+ if (
99
+ !event ||
100
+ typeof event !== 'object' ||
101
+ !('payload' in event) ||
102
+ !('actor' in event) ||
103
+ (!('build' in event) && !('repo' in event))
104
+ )
105
+ return [event]
106
+ const known = decodeStoredEvent(event as unknown as StoredEvent)
107
+ return known === null ? [] : [known as E]
108
+ })
109
+ }
110
+ export interface CompatibilityState {
111
+ unknownFacts: number
112
+ decisiveFacts: number
113
+ lastSeq: number
114
+ decisiveBoundary: number
115
+ types: string[]
116
+ writerVersions: string[]
117
+ legacyVersionUnavailable: boolean
118
+ decisiveTypes: string[]
119
+ decisiveWriterVersions: string[]
120
+ decisiveLegacyVersionUnavailable: boolean
121
+ }
122
+ export function initialCompatibility(): CompatibilityState {
123
+ return {
124
+ unknownFacts: 0,
125
+ decisiveFacts: 0,
126
+ lastSeq: 0,
127
+ decisiveBoundary: 0,
128
+ types: [],
129
+ writerVersions: [],
130
+ legacyVersionUnavailable: false,
131
+ decisiveTypes: [],
132
+ decisiveWriterVersions: [],
133
+ decisiveLegacyVersionUnavailable: false,
134
+ }
135
+ }
136
+ export function advanceCompatibility(
137
+ state: CompatibilityState,
138
+ events: readonly StoredEvent[],
139
+ ): void {
140
+ for (const event of events) {
141
+ if (decodeStoredEvent(event) !== null) continue
142
+ state.lastSeq = Math.max(state.lastSeq, event.seq)
143
+ state.unknownFacts++
144
+ const metadata =
145
+ event.compatibility === undefined
146
+ ? undefined
147
+ : compatibilityMetadataSchema.parse(event.compatibility)
148
+ if (!state.types.includes(event.type)) state.types.push(event.type)
149
+ if (metadata && !state.writerVersions.includes(metadata.writerVersion))
150
+ state.writerVersions.push(metadata.writerVersion)
151
+ if (!metadata) state.legacyVersionUnavailable = true
152
+ if (metadata?.classification !== 'advisory') {
153
+ if (!state.decisiveTypes.includes(event.type)) state.decisiveTypes.push(event.type)
154
+ if (metadata && !state.decisiveWriterVersions.includes(metadata.writerVersion))
155
+ state.decisiveWriterVersions.push(metadata.writerVersion)
156
+ if (!metadata) state.decisiveLegacyVersionUnavailable = true
157
+ state.decisiveFacts++
158
+ state.decisiveBoundary = event.seq
159
+ }
160
+ }
161
+ }
162
+
163
+ /** Selector folds also consume deliberately compact/adapted rows. Filter their
164
+ * vocabulary here; strict payload projection happens before adaptation at the
165
+ * persisted-read boundary and in the whole-build reduction. */
166
+ export function filterKnownEvents<E>(events: readonly E[]): readonly E[] {
167
+ return events.filter((event) => {
168
+ if (
169
+ !event ||
170
+ typeof event !== 'object' ||
171
+ !('type' in event) ||
172
+ (!('build' in event) && !('repo' in event))
173
+ )
174
+ return true
175
+ const schemas = 'repo' in event ? repositoryEventPayloadSchemas : eventPayloadSchemas
176
+ return typeof event.type === 'string' && Object.hasOwn(schemas, event.type)
177
+ })
178
+ }
179
+ /** Preserve opaque facts for compatibility-bearing folds after validating all
180
+ * known payloads, before any selector adapts or drops payload fields. */
181
+ export function projectStoredEvents<E extends StoredEvent>(events: readonly E[]): E[] {
182
+ return events.map((event) => (decodeStoredEvent(event) ?? event) as E)
183
+ }
@@ -0,0 +1,13 @@
1
+ import { isAbsolute } from 'node:path'
2
+ import { z } from 'zod'
3
+
4
+ const nonblank = z.string().refine((value) => value.trim().length > 0, 'Expected a nonblank string')
5
+ const absolutePath = nonblank.refine(isAbsolute, 'Expected an absolute path')
6
+ export const nativeSessionHandleSchema = z.strictObject({
7
+ version: z.literal(1),
8
+ runner: nonblank,
9
+ nativeId: nonblank,
10
+ host: nonblank,
11
+ workspacePath: absolutePath,
12
+ statePath: absolutePath.optional(),
13
+ })
@@ -1,3 +1,4 @@
1
+ import { nativeSessionHandleSchema } from './native-session'
1
2
  /**
2
3
  * The event vocabulary (SPEC §15.3), frozen as schemas — this closes open
3
4
  * thread §18.1. It is simultaneously the store schema, the kernel's I/O, the
@@ -283,6 +284,13 @@ const reviewVerdictPayload = z.strictObject({
283
284
  })
284
285
 
285
286
  export const eventPayloadSchemas = {
287
+ 'build.compatibility-deferred': z.strictObject({
288
+ boundary: z.number().int().positive(),
289
+ types: z.array(z.string().min(1)).min(1),
290
+ writerVersions: z.array(z.string().min(1)),
291
+ legacyVersionUnavailable: z.boolean(),
292
+ diagnostic: z.string().min(1),
293
+ }),
286
294
  // ── Build lifecycle ────────────────────────────────────────────────────────
287
295
  'build.created': z.strictObject({
288
296
  ticket: ticketRefSchema,
@@ -313,6 +321,34 @@ export const eventPayloadSchemas = {
313
321
  parent: z.string().min(1),
314
322
  sha: z.string().min(1),
315
323
  }),
324
+ 'node.restack-requested': z
325
+ .strictObject({
326
+ id: z.string().min(1),
327
+ reason: z.enum(['operator', 'presence']),
328
+ branch: z.string().min(1),
329
+ parent: z.string().min(1),
330
+ dependent: ticketRefSchema.optional(),
331
+ identities: z.array(z.string()).optional(),
332
+ })
333
+ .refine(
334
+ (request) =>
335
+ request.reason !== 'presence' ||
336
+ (request.dependent !== undefined &&
337
+ request.identities !== undefined &&
338
+ request.identities.length > 0),
339
+ 'presence demand requires a dependent ticket and nonempty identities',
340
+ ),
341
+ 'node.restack-satisfied': z.strictObject({
342
+ id: z.string().min(1),
343
+ parent: z.string().min(1),
344
+ outcome: z.enum(['covered', 'destination-changed']),
345
+ }),
346
+ 'node.parent-distance': z.strictObject({
347
+ base: z.string().min(1),
348
+ parent: z.string().min(1),
349
+ branch: z.string().min(1),
350
+ count: z.number().int().nonnegative().nullable(),
351
+ }),
316
352
  'node.parent-requested': parentChange,
317
353
  'node.parent-accepted': parentChange,
318
354
  'node.parent-pr-retargeted': z.strictObject({ id: z.string().min(1), base: z.string().min(1) }),
@@ -331,6 +367,7 @@ export const eventPayloadSchemas = {
331
367
  'node.writer-reserved': z.strictObject({
332
368
  id: z.string().min(1),
333
369
  operation: z.enum(['restack', 'landing']),
370
+ requests: z.array(z.string().min(1)).optional(),
334
371
  expected: z.string().min(1),
335
372
  parent: z.string().min(1),
336
373
  oldBase: z.string().min(1),
@@ -368,6 +405,7 @@ export const eventPayloadSchemas = {
368
405
  dropReason: z.enum(['upstream-identity', 'upstream-commit']).optional(),
369
406
  }),
370
407
  'node.item-conflicted': z.strictObject({
408
+ files: z.array(z.string()).optional(),
371
409
  id: z.string().min(1),
372
410
  index: z.number().int().nonnegative(),
373
411
  base: z.string().min(1),
@@ -393,15 +431,28 @@ export const eventPayloadSchemas = {
393
431
  }),
394
432
  'node.inventory': z.strictObject({
395
433
  head: z.string().min(1),
434
+ base: z.string().min(1).optional(),
396
435
  builds: z.array(z.string()),
397
436
  contributions: z.array(z.string()).optional(),
398
437
  foreign: z.array(z.string()),
399
438
  }),
400
439
  'integration.abandoned': z.strictObject({ id: z.string().min(1), reason: z.string().min(1) }),
401
440
  'integration.observed': z.strictObject({
441
+ membershipSource: z.string().optional(),
402
442
  openChildren: z.array(z.string()),
403
443
  diagnostics: z.array(z.string()),
404
444
  contributions: z.array(z.string()),
445
+ children: z
446
+ .array(
447
+ z.strictObject({
448
+ source: z.string(),
449
+ id: z.string(),
450
+ title: z.string().optional(),
451
+ builds: z.array(z.string()).optional(),
452
+ claims: z.array(z.string()).optional(),
453
+ }),
454
+ )
455
+ .optional(),
405
456
  }),
406
457
  'integration.prepared': z.strictObject({
407
458
  id: z.string().min(1),
@@ -643,6 +694,22 @@ export const eventPayloadSchemas = {
643
694
  'build.resume-requested': reasonOnly,
644
695
  'build.abort-requested': reasonOnly,
645
696
  'build.discard-requested': empty,
697
+ 'build.janitor-attempt-failed': z.strictObject({
698
+ firstFailureAt: z.iso.datetime(),
699
+ attempt: z.number().int().positive(),
700
+ message: z.string().min(1),
701
+ nextAttemptAt: z.iso.datetime(),
702
+ }),
703
+ 'build.janitor-failing': z.strictObject({ message: z.string().min(1) }),
704
+ 'build.janitor-recovered': empty,
705
+ 'landing.application-failed': z.strictObject({
706
+ candidate: landingCandidateSchema,
707
+ decisionSeq: z.number().int().positive(),
708
+ commandSeq: z.number().int().positive().optional(),
709
+ message: z.string().min(1),
710
+ attempt: z.number().int().positive(),
711
+ after: z.iso.datetime(),
712
+ }),
646
713
  'landing.decided': landingDecision,
647
714
  'build.hold-requested': empty,
648
715
  'build.land-requested': z.strictObject({ decisionSeq: z.number().int().positive() }),
@@ -665,6 +732,7 @@ export const eventPayloadSchemas = {
665
732
  decisionSeq: z.number().int().positive().optional(),
666
733
  commandSeq: z.number().int().positive().optional(),
667
734
  operation: z.string().min(1).optional(),
735
+ publicationHead: z.string().min(1).optional(),
668
736
  }),
669
737
  'build.auto-merge-requested': autoMergeCommand,
670
738
  'build.auto-merge-cancelled': autoMergeCommand,
@@ -693,6 +761,18 @@ export const eventPayloadSchemas = {
693
761
 
694
762
  // ── Sessions (every agent run is bracketed by these) ──────────────────────
695
763
  'session.started': z.strictObject({
764
+ /** Write-ahead continuation intent; independent of native acceptance. */
765
+ continuation: z
766
+ .strictObject({
767
+ predecessor: z.string().min(1),
768
+ root: z.string().min(1),
769
+ handle: nativeSessionHandleSchema,
770
+ cause: z.enum(['shutdown', 'pause', 'runner-loss']),
771
+ activeMs: z.number().nonnegative(),
772
+ budgetMs: z.number().positive(),
773
+ })
774
+ .optional(),
775
+ budgetMs: z.number().positive().optional(),
696
776
  session: z.string().min(1),
697
777
  role: z.string().min(1),
698
778
  runner: z.string().min(1),
@@ -707,10 +787,46 @@ export const eventPayloadSchemas = {
707
787
  /** Present only when this target substituted for the preceding failed one. */
708
788
  substitution: providerSubstitutionSchema.optional(),
709
789
  }),
790
+ 'session.suspended': z.strictObject({
791
+ session: z.string().min(1),
792
+ phase: phaseSchema,
793
+ round: round.optional(),
794
+ handle: nativeSessionHandleSchema.optional(),
795
+ cause: z.enum(['shutdown', 'pause']),
796
+ activeMs: z.number().nonnegative(),
797
+ transcript: artifactRefSchema,
798
+ usage: z
799
+ .strictObject({
800
+ inputTokens: z.number().int().nonnegative(),
801
+ outputTokens: z.number().int().nonnegative(),
802
+ turns: z.number().int().nonnegative(),
803
+ })
804
+ .optional(),
805
+ }),
806
+ 'session.resumed': z.strictObject({
807
+ session: z.string().min(1),
808
+ predecessor: z.string().min(1),
809
+ root: z.string().min(1),
810
+ handle: nativeSessionHandleSchema,
811
+ cause: z.enum(['shutdown', 'pause', 'runner-loss']),
812
+ stale: z.boolean(),
813
+ }),
814
+ 'session.resume-fallback': z.strictObject({
815
+ session: z.string().min(1),
816
+ stale: z.boolean().optional(),
817
+ reason: z.string().min(1),
818
+ }),
819
+ 'session.native-handle-recorded': z.strictObject({
820
+ session: z.string().min(1),
821
+ phase: phaseSchema,
822
+ round: round.optional(),
823
+ handle: nativeSessionHandleSchema,
824
+ }),
710
825
  'session.ended': z.union([
711
826
  /** Ordinary completion retains the historical transcript-bearing shape. */
712
827
  z.strictObject({
713
828
  session: z.string().min(1),
829
+ activeMs: z.number().nonnegative().optional(),
714
830
  transcript: artifactRefSchema,
715
831
  usage: z.strictObject({
716
832
  inputTokens: z.number().int().nonnegative(),
@@ -857,7 +973,11 @@ export const eventPayloadSchemas = {
857
973
  }),
858
974
  'target.moved': z.strictObject({ branch: z.string().min(1), sha: gitOid }),
859
975
  'restack.prepared': leafRestackSchema,
860
- 'restack.replayed': z.strictObject({ id: z.string().min(1), conflicted: z.boolean() }),
976
+ 'restack.replayed': z.strictObject({
977
+ id: z.string().min(1),
978
+ conflicted: z.boolean(),
979
+ files: z.array(z.string()).optional(),
980
+ }),
861
981
  'restack.publish-intended': leafRestackSchema.extend({ headSha: gitOid }),
862
982
  'restack.rewritten': leafRestackSchema.extend({
863
983
  headSha: gitOid,
@@ -881,7 +1001,11 @@ export const eventPayloadSchemas = {
881
1001
  request: z.number().int().positive(),
882
1002
  actual: gitOid.nullable(),
883
1003
  }),
884
- 'reconcile.replayed': z.strictObject({ id: z.string().min(1), conflicted: z.boolean() }),
1004
+ 'reconcile.replayed': z.strictObject({
1005
+ id: z.string().min(1),
1006
+ conflicted: z.boolean(),
1007
+ files: z.array(z.string()).optional(),
1008
+ }),
885
1009
  'reconcile.publish-intended': reconcileTransactionSchema.extend({
886
1010
  headSha: gitOid,
887
1011
  conflicted: z.boolean(),
@@ -931,6 +1055,30 @@ export const eventPayloadSchemas = {
931
1055
  reviewRoundCeiling: z.number().int().positive().optional(),
932
1056
  }),
933
1057
  /** Infra failure — distinct from verdicts (a verdict is a fact, not a failure). */
1058
+ 'session.build-read-failed': z.strictObject({
1059
+ session: z.string().min(1),
1060
+ phase: phaseSchema,
1061
+ round,
1062
+ operation: z.literal('context'),
1063
+ reason: z.literal('own-build-read'),
1064
+ diagnostic: z.string().trim().min(1),
1065
+ }),
1066
+ 'phase.deferred': z.strictObject({
1067
+ session: z.string().min(1),
1068
+ phase: phaseSchema,
1069
+ round,
1070
+ reason: z.literal('own-build-read'),
1071
+ diagnostic: z.string().trim().min(1),
1072
+ }),
1073
+ 'phase.deferral-escalated': z.strictObject({
1074
+ session: z.string().min(1),
1075
+ phase: phaseSchema,
1076
+ round,
1077
+ id: z.string().min(1),
1078
+ evidenceSeq: z.number().int().positive(),
1079
+ question: z.string().min(1),
1080
+ refs: z.array(z.string()),
1081
+ }),
934
1082
  'phase.failed': z.strictObject({
935
1083
  phase: phaseSchema,
936
1084
  round: round.optional(),
@@ -0,0 +1,210 @@
1
+ /** Exhaustive producer policy. New semantic control facts must be decisive. */
2
+ import { z } from 'zod'
3
+ import type { EventType } from './payloads'
4
+ import type { RepositoryEventType } from './repository'
5
+ import { AUTOBUILD_VERSION } from '../store/remote/version'
6
+ export type FactClassification = 'advisory' | 'decisive'
7
+ export const compatibilityMetadataSchema = z.strictObject({
8
+ writerVersion: z.string().min(1),
9
+ classification: z.enum(['advisory', 'decisive']),
10
+ })
11
+ export type CompatibilityMetadata = z.infer<typeof compatibilityMetadataSchema>
12
+ export const buildEventPolicy = {
13
+ 'build.compatibility-deferred': 'advisory',
14
+ 'build.janitor-attempt-failed': 'decisive',
15
+ 'build.janitor-recovered': 'decisive',
16
+ 'build.janitor-failing': 'advisory',
17
+ 'landing.application-failed': 'decisive',
18
+ 'landing.decided': 'decisive',
19
+ 'build.hold-requested': 'decisive',
20
+ 'build.land-requested': 'decisive',
21
+ 'landing.command-acknowledged': 'decisive',
22
+ 'landing.native-enable-started': 'decisive',
23
+ 'landing.forge-merged': 'decisive',
24
+ 'landing.native-applied': 'decisive',
25
+ 'target.moved': 'decisive',
26
+ 'restack.prepared': 'decisive',
27
+ 'restack.replayed': 'decisive',
28
+ 'restack.publish-intended': 'decisive',
29
+ 'restack.rewritten': 'decisive',
30
+ 'restack.contended': 'decisive',
31
+ 'restack.completed': 'decisive',
32
+ 'integration.abandoned': 'decisive',
33
+ 'integration.observed': 'decisive',
34
+ 'integration.prepared': 'decisive',
35
+ 'integration.landing-intended': 'decisive',
36
+ 'integration.landed': 'decisive',
37
+ 'integration.audited': 'decisive',
38
+ 'node.restack-requested': 'decisive',
39
+ 'node.restack-satisfied': 'decisive',
40
+ 'node.parent-distance': 'advisory',
41
+ 'node.parent-requested': 'decisive',
42
+ 'node.parent-accepted': 'decisive',
43
+ 'node.parent-pr-retargeted': 'decisive',
44
+ 'node.parent-completed': 'decisive',
45
+ 'node.parent-cancelled': 'decisive',
46
+ 'node.parent-rejection-cleared': 'decisive',
47
+ 'node.parent-rejected': 'decisive',
48
+ 'node.opening': 'decisive',
49
+ 'node.writer-reserved': 'decisive',
50
+ 'node.item-prepared': 'decisive',
51
+ 'node.item-replayed': 'decisive',
52
+ 'node.item-conflicted': 'decisive',
53
+ 'node.publish-intended': 'decisive',
54
+ 'node.rewritten': 'decisive',
55
+ 'node.writer-settled': 'decisive',
56
+ 'node.contribution-recorded': 'decisive',
57
+ 'node.inventory': 'decisive',
58
+ 'node.opened': 'decisive',
59
+ 'node.compared': 'decisive',
60
+ 'pr.opened': 'decisive',
61
+ 'build.created': 'decisive',
62
+ 'build.completed': 'decisive',
63
+ 'runner.attached': 'decisive',
64
+ 'runner.setup-failed': 'decisive',
65
+ 'workspace.provisioned': 'decisive',
66
+ 'workspace.provision-started': 'decisive',
67
+ 'workspace.released': 'decisive',
68
+ 'execution.started': 'decisive',
69
+ 'execution.ended': 'decisive',
70
+ 'infrastructure.failed': 'decisive',
71
+ 'infrastructure.cleanup-attempted': 'decisive',
72
+ 'pr-summary.requested': 'decisive',
73
+ 'pr-summary.attempted': 'advisory',
74
+ 'pr-summary.result': 'advisory',
75
+ 'pr-summary.settled': 'advisory',
76
+ 'finalize.step-started': 'decisive',
77
+ 'publication.requested': 'decisive',
78
+ 'publication.lost': 'decisive',
79
+ 'abort.remote-branch-deleted': 'decisive',
80
+ 'abort.local-branch-deleted': 'decisive',
81
+ 'abort.ticket-returned': 'decisive',
82
+ 'dispatch.failed': 'decisive',
83
+ 'dispatch.comment-posted': 'advisory',
84
+ 'build.pause-requested': 'decisive',
85
+ 'build.resume-requested': 'decisive',
86
+ 'build.abort-requested': 'decisive',
87
+ 'build.discard-requested': 'decisive',
88
+ 'build.auto-merge-requested': 'decisive',
89
+ 'build.auto-merge-cancelled': 'decisive',
90
+ 'build.auto-merge-default-observed': 'decisive',
91
+ 'build.paused': 'decisive',
92
+ 'build.resumed': 'decisive',
93
+ 'build.aborted': 'decisive',
94
+ 'spec.imported': 'decisive',
95
+ 'spec.authored': 'decisive',
96
+ 'spec.revised': 'decisive',
97
+ 'session.suspended': 'decisive',
98
+ 'session.resumed': 'decisive',
99
+ 'session.resume-fallback': 'advisory',
100
+ 'session.started': 'decisive',
101
+ 'session.native-handle-recorded': 'advisory',
102
+ 'session.ended': 'decisive',
103
+ 'plan.started': 'decisive',
104
+ 'plan.completed': 'decisive',
105
+ 'plan-review.started': 'decisive',
106
+ 'plan-review.verdict': 'decisive',
107
+ 'implement.started': 'decisive',
108
+ 'implement.completed': 'decisive',
109
+ 'code-review.started': 'decisive',
110
+ 'code-review.verdict': 'decisive',
111
+ 'verify.reconcile-policy-captured': 'decisive',
112
+ 'verify.started': 'decisive',
113
+ 'verify.completed': 'decisive',
114
+ 'finalize.started': 'decisive',
115
+ 'finalize.completed': 'decisive',
116
+ 'finalize.step-completed': 'decisive',
117
+ 'pr-attachment.designated': 'decisive',
118
+ 'pr-attachment.hosted': 'decisive',
119
+ 'pr-attachment.reclaimed': 'decisive',
120
+ 'pr-attachment.reclaim-failed': 'advisory',
121
+ 'pr.auto-merge-enabled': 'decisive',
122
+ 'pr.auto-merge-disabled': 'decisive',
123
+ 'pr.merged': 'decisive',
124
+ 'pr.closed': 'decisive',
125
+ 'pr.conflicted': 'decisive',
126
+ 'reconcile.progress-checked': 'decisive',
127
+ 'reconcile.started': 'decisive',
128
+ 'reconcile.prepared': 'decisive',
129
+ 'reconcile.contended': 'decisive',
130
+ 'reconcile.replayed': 'decisive',
131
+ 'reconcile.publish-intended': 'decisive',
132
+ 'reconcile.rewritten': 'decisive',
133
+ 'reconcile.completed': 'decisive',
134
+ 'observation.recorded': 'advisory',
135
+ 'escalation.raised': 'decisive',
136
+ 'escalation.answered': 'decisive',
137
+ 'phase.failed': 'decisive',
138
+ 'session.build-read-failed': 'decisive',
139
+ 'phase.deferred': 'decisive',
140
+ 'phase.deferral-escalated': 'decisive',
141
+ } satisfies Record<EventType, FactClassification>
142
+ export const repositoryEventPolicy = {
143
+ 'tickets.migration-recorded': 'decisive',
144
+ 'change-presence.observed': 'decisive',
145
+ 'harvest.pause-requested': 'decisive',
146
+ 'harvest.resume-requested': 'decisive',
147
+ 'harvest.paused': 'decisive',
148
+ 'harvest.resumed': 'decisive',
149
+ 'harvest.recovery-requested': 'decisive',
150
+ 'harvest.recovery-exhausted': 'decisive',
151
+ 'harvest.started': 'decisive',
152
+ 'harvest.execution.authorized': 'decisive',
153
+ 'harvest.execution.ended': 'decisive',
154
+ 'harvest.execution.started': 'decisive',
155
+ 'harvest.execution.released': 'decisive',
156
+ 'harvest.step.started': 'decisive',
157
+ 'harvest.step.completed': 'decisive',
158
+ 'harvest.session.started': 'decisive',
159
+ 'harvest.session.ended': 'decisive',
160
+ 'harvest.proposals.submitted': 'decisive',
161
+ 'harvest.review.verdict': 'decisive',
162
+ 'harvest.proposal.id-reserved': 'decisive',
163
+ 'harvest.proposal.filed': 'decisive',
164
+ 'harvest.completed': 'decisive',
165
+ 'harvest.escalated': 'decisive',
166
+ 'harvest.failed': 'decisive',
167
+ 'orchestrator.sandbox.provisioned': 'decisive',
168
+ 'orchestrator.sandbox.resumed': 'decisive',
169
+ 'orchestrator.sandbox.activity': 'advisory',
170
+ 'orchestrator.sandbox.stopped': 'decisive',
171
+ 'orchestrator.sandbox.released': 'decisive',
172
+ 'orchestrator.sandbox.reset': 'decisive',
173
+ 'orchestrator.sandbox.published': 'decisive',
174
+ 'orchestrator.sandbox.publish-failed': 'decisive',
175
+ 'dispatcher.run-started': 'decisive',
176
+ 'dispatcher.stop-requested': 'decisive',
177
+ 'dispatcher.run-stopped': 'decisive',
178
+ 'dispatcher.tick-yielded': 'advisory',
179
+ 'dispatcher.config-rejected': 'advisory',
180
+ 'dispatcher.forge-readiness-failed': 'advisory',
181
+ 'dispatcher.config-publication-failed': 'advisory',
182
+ 'dispatcher.tick-started': 'decisive',
183
+ 'dispatcher.tick-completed': 'advisory',
184
+ 'dispatcher.ticket-scan-completed': 'advisory',
185
+ 'dispatcher.ticket-source-dependency-limited': 'decisive',
186
+ 'dispatcher.forge-degraded': 'decisive',
187
+ 'dispatcher.forge-recovered': 'decisive',
188
+ 'dispatcher.ticket-source-degraded': 'decisive',
189
+ 'dispatcher.ticket-source-recovered': 'decisive',
190
+ 'dispatcher.tick-failed': 'decisive',
191
+ 'dispatcher.runner-settled': 'decisive',
192
+ 'dispatcher.harvest-runner-failed': 'decisive',
193
+ 'dispatcher.upgrade-available': 'advisory',
194
+ 'dispatcher.operator-reported': 'advisory',
195
+ 'dispatcher.config-reloaded': 'decisive',
196
+ 'dispatcher.intake-set': 'decisive',
197
+ 'dispatcher.pause-set': 'decisive',
198
+ 'dispatcher.auto-merge-default-set': 'decisive',
199
+ } satisfies Record<RepositoryEventType, FactClassification>
200
+
201
+ export function stampCompatibility(
202
+ classification: FactClassification,
203
+ supplied?: unknown,
204
+ ): CompatibilityMetadata {
205
+ if (supplied === undefined) return { writerVersion: AUTOBUILD_VERSION, classification }
206
+ const metadata = compatibilityMetadataSchema.parse(supplied)
207
+ if (metadata.classification !== classification)
208
+ throw new Error('event compatibility classification disagrees with catalog')
209
+ return metadata
210
+ }