@defrex/autobuild 1.14.0 → 1.16.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 (35) hide show
  1. package/README.md +1 -1
  2. package/SPEC.md +44 -5
  3. package/docs/architecture.md +43 -6
  4. package/docs/configuration.md +8 -3
  5. package/docs/dispatcher-hosting.md +13 -0
  6. package/docs/remote-store-protocol.md +1 -1
  7. package/docs/troubleshooting.md +33 -3
  8. package/docs/workspaces.md +2 -1
  9. package/package.json +3 -3
  10. package/packages/core/src/cli/dispatch.ts +171 -5
  11. package/packages/core/src/cli/repository-status.ts +15 -0
  12. package/packages/core/src/events/policy.ts +1 -0
  13. package/packages/core/src/events/repository.ts +95 -6
  14. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/23d33121de6d3325.json +107 -0
  15. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/e28c68e63cbd265f.json +108 -0
  16. package/packages/core/src/kernel/reducer-revisions.ts +3 -0
  17. package/packages/core/src/kernel/repository-read-facts.ts +9 -0
  18. package/packages/core/src/ports/types.ts +10 -4
  19. package/packages/core/src/ports/workspace/vercel-sandbox.ts +281 -75
  20. package/packages/core/src/processes/dispatcher.ts +1142 -265
  21. package/packages/core/src/processes/invocation-budget.ts +6 -0
  22. package/packages/core/src/processes/launch-cut.ts +37 -0
  23. package/packages/core/src/processes/pass-stages.ts +294 -0
  24. package/packages/core/src/store/reducer-value-shapes.json +5285 -5003
  25. package/packages/core/src/store/remote/released-clients.json +22 -0
  26. package/packages/core/src/store/remote/released-reducer-values/1.15.0.json +36354 -0
  27. package/packages/core/src/store/remote/released-reducer-values/1.16.0.json +36358 -0
  28. package/packages/core/src/store/remote/released-reducer-values/index.ts +20 -16
  29. package/packages/core/src/store/remote/released-value-policy.ts +7 -0
  30. package/packages/core/src/store/repo-state-events.ts +1 -0
  31. package/packages/core/src/testing/reduced-read-budget.ts +285 -8
  32. package/skills/guide/SKILL.md +25 -3
  33. package/skills/guide/references/remote-store-protocol.md +1 -1
  34. package/skills/operate/SKILL.md +16 -4
  35. package/templates/hosted-service/package.json +2 -2
@@ -174,3 +174,9 @@ export function untilAborted<T>(
174
174
  )
175
175
  })
176
176
  }
177
+
178
+ export {
179
+ LaunchCutByDeadline,
180
+ isLaunchCutByDeadline,
181
+ type CutLaunch,
182
+ } from './launch-cut'
@@ -0,0 +1,37 @@
1
+ /**
2
+ * A runner launch the bounded invocation's work deadline cut after the
3
+ * build's execution lease was claimed (GitHub #1415). Kept free of runtime
4
+ * imports so the dispatcher can recognise it without loading the
5
+ * server-only invocation budget.
6
+ */
7
+
8
+ export interface CutLaunch {
9
+ slug: string
10
+ /** The execution lease holder the cut launch claimed. */
11
+ instance: string
12
+ /** Whether the provider start was requested: only then can a guest be
13
+ * running, and the lease is released only after a confirmed fence. */
14
+ startRequested: boolean
15
+ /** The open workspace the launch targeted, when known. */
16
+ workspace?: { provider: string; ref: string }
17
+ }
18
+
19
+ /** Thrown by the CLI's launch when the work deadline cut it mid-flight with
20
+ * its execution lease claimed and no execution handle. Nothing was recorded:
21
+ * the dispatcher settles the lease in teardown and carries the launch in the
22
+ * deferred-work hint. */
23
+ export class LaunchCutByDeadline extends Error {
24
+ override readonly name = 'LaunchCutByDeadline'
25
+ constructor(readonly cut: CutLaunch) {
26
+ super(
27
+ `the work deadline cut the launch of build ${cut.slug} ` +
28
+ (cut.startRequested
29
+ ? 'after its provider start was requested'
30
+ : 'before its provider start'),
31
+ )
32
+ }
33
+ }
34
+
35
+ export function isLaunchCutByDeadline(error: unknown): error is LaunchCutByDeadline {
36
+ return error instanceof LaunchCutByDeadline
37
+ }
@@ -0,0 +1,294 @@
1
+ /**
2
+ * Stage fairness for a bounded dispatcher pass (#1409). Pure: shares, the
3
+ * rotation rule, the per-pass stage record and its rendering. The Dispatcher
4
+ * owns the clock reads and the store writes.
5
+ *
6
+ * A stage that starts at `t` with `R = deadlineAt − t` left may start new
7
+ * items until `t + share × R`; intake runs to the work deadline. Each part (a
8
+ * loop or single-call helper) inside a stage always gets its first item while
9
+ * the work deadline has not passed, so one helper cannot starve its siblings
10
+ * and no stage can consume a whole pass.
11
+ */
12
+ import { PASS_STAGE_NAMES, PASS_STAGE_PART_NAMES, ROTATED_PASS_PARTS } from '../events/repository'
13
+
14
+ export { PASS_STAGE_NAMES, PASS_STAGE_PART_NAMES, ROTATED_PASS_PARTS }
15
+
16
+ export type PassStage = (typeof PASS_STAGE_NAMES)[number]
17
+ export type PassStagePart = (typeof PASS_STAGE_PART_NAMES)[number]
18
+ export type RotatedPassPart = (typeof ROTATED_PASS_PARTS)[number]
19
+ export type PassStop = 'share' | 'deadline'
20
+ export type PassCursors = Partial<Record<RotatedPassPart, string>>
21
+
22
+ /** Each stage's parts, in the order its helpers run. */
23
+ export const PASS_STAGE_PARTS: Record<PassStage, readonly PassStagePart[]> = {
24
+ deferred: ['deferred'],
25
+ settlement: ['executions', 'harvest', 'sandboxes'],
26
+ preparations: ['preparations'],
27
+ janitor: ['janitor'],
28
+ recovery: ['handbacks', 'dispatches', 'resume'],
29
+ sweep: ['waits', 'sweep'],
30
+ admission: ['nodes', 'starts'],
31
+ intake: ['tickets', 'parked', 'starts', 'harvest-trigger', 'context'],
32
+ }
33
+
34
+ /** The share of the time left when a stage starts that it may spend starting
35
+ * new items. Intake's 1 runs it to the work deadline. */
36
+ export const PASS_STAGE_SHARES: Record<PassStage, number> = {
37
+ deferred: 1 / 2,
38
+ settlement: 1 / 4,
39
+ preparations: 1 / 4,
40
+ janitor: 1 / 2,
41
+ recovery: 1 / 4,
42
+ sweep: 1 / 2,
43
+ admission: 1 / 4,
44
+ intake: 1,
45
+ }
46
+
47
+ /** The instant after which `stage` starts no further item, beyond the first
48
+ * one of each of its parts. */
49
+ export function stageCut(stage: PassStage, startedAt: number, deadlineAt: number): number {
50
+ if (stage === 'intake') return deadlineAt
51
+ return startedAt + PASS_STAGE_SHARES[stage] * Math.max(0, deadlineAt - startedAt)
52
+ }
53
+
54
+ /** `items` in canonical key order (plain code-unit comparison), starting at
55
+ * the first key strictly greater than `after` and wrapping around. The
56
+ * listing's own order never matters. */
57
+ export function rotateFrom<T>(items: readonly T[], key: (item: T) => string, after?: string): T[] {
58
+ const sorted = [...items].sort((a, b) => {
59
+ const left = key(a)
60
+ const right = key(b)
61
+ return left < right ? -1 : left > right ? 1 : 0
62
+ })
63
+ if (after === undefined) return sorted
64
+ const start = sorted.findIndex((item) => key(item) > after)
65
+ if (start <= 0) return sorted
66
+ return [...sorted.slice(start), ...sorted.slice(0, start)]
67
+ }
68
+
69
+ /** A rotated loop's next position: cleared once the loop reached every item,
70
+ * the last item it started when it stopped early, and unchanged when it
71
+ * stopped before starting any. */
72
+ export function nextPosition(input: {
73
+ lastStarted: string | undefined
74
+ reachedAll: boolean
75
+ previous: string | undefined
76
+ }): string | undefined {
77
+ if (input.reachedAll) return undefined
78
+ return input.lastStarted ?? input.previous
79
+ }
80
+
81
+ export interface PassStagePartRecord {
82
+ part: PassStagePart
83
+ items: number
84
+ total?: number
85
+ stopped?: PassStop
86
+ }
87
+
88
+ export interface PassStageRecord {
89
+ stage: PassStage
90
+ ms: number
91
+ items: number
92
+ total?: number
93
+ stopped?: PassStop
94
+ parts?: PassStagePartRecord[]
95
+ }
96
+
97
+ function strongest(a: PassStop | undefined, b: PassStop | undefined): PassStop | undefined {
98
+ if (a === 'deadline' || b === 'deadline') return 'deadline'
99
+ return a ?? b
100
+ }
101
+
102
+ /** One stage's record from its parts: items and totals add, a part with no
103
+ * reported total leaves the stage total unknown, and the strongest stop
104
+ * marker wins. `parts` is kept only for a stage with more than one part. */
105
+ export function aggregateStage(
106
+ stage: PassStage,
107
+ ms: number,
108
+ parts: readonly PassStagePartRecord[],
109
+ ): PassStageRecord {
110
+ let items = 0
111
+ let total: number | undefined = 0
112
+ let stopped: PassStop | undefined
113
+ for (const part of parts) {
114
+ items += part.items
115
+ total = total === undefined || part.total === undefined ? undefined : total + part.total
116
+ stopped = strongest(stopped, part.stopped)
117
+ }
118
+ return {
119
+ stage,
120
+ ms: Math.max(0, Math.round(ms)),
121
+ items,
122
+ ...(total !== undefined ? { total } : {}),
123
+ ...(stopped !== undefined ? { stopped } : {}),
124
+ ...(parts.length > 1 ? { parts: parts.map((part) => ({ ...part })) } : {}),
125
+ }
126
+ }
127
+
128
+ const progress = (items: number, total: number | undefined) =>
129
+ total === undefined ? `${items}` : `${items}/${total}`
130
+
131
+ /** The dispatch log's `stages:` summary, for example
132
+ * `settlement 3.1s 6/11 (executions 5/9, harvest 1/2, sandboxes 0/0) · janitor 131.4s 4/12 (share)`. */
133
+ export function formatPassStages(records: readonly PassStageRecord[]): string {
134
+ return records
135
+ .map((record) => {
136
+ const parts = record.parts
137
+ ? ` (${record.parts
138
+ .map(
139
+ (part) =>
140
+ `${part.part} ${progress(part.items, part.total)}${part.stopped ? ` ${part.stopped}` : ''}`,
141
+ )
142
+ .join(', ')})`
143
+ : ''
144
+ const stopped = record.stopped ? ` (${record.stopped})` : ''
145
+ return `${record.stage} ${(record.ms / 1000).toFixed(1)}s ${progress(record.items, record.total)}${parts}${stopped}`
146
+ })
147
+ .join(' · ')
148
+ }
149
+
150
+ const ofTotal = (items: number, total: number | undefined) =>
151
+ total === undefined ? `${items} started` : `${items} of ${total}`
152
+
153
+ /** Where the work deadline stopped a pass: the stage and the part in
154
+ * flight, for example `settlement: harvest 1 of 2 (6 of 11 overall)`.
155
+ * Undefined when no stage stopped at the deadline. */
156
+ export function deadlineStopDetail(records: readonly PassStageRecord[]): string | undefined {
157
+ const record = records.findLast((candidate) => candidate.stopped === 'deadline')
158
+ if (record === undefined) return undefined
159
+ const part = record.parts?.findLast((candidate) => candidate.stopped === 'deadline')
160
+ if (part === undefined) return `${record.stage} (${ofTotal(record.items, record.total)})`
161
+ return (
162
+ `${record.stage}: ${part.part} ${ofTotal(part.items, part.total)} ` +
163
+ `(${ofTotal(record.items, record.total)} overall)`
164
+ )
165
+ }
166
+
167
+ /** The dispatch log's deadline line for a pass the work deadline cut short,
168
+ * for example `work deadline reached during janitor (1 of 1)`. */
169
+ export function formatDeadlineStop(records: readonly PassStageRecord[]): string | undefined {
170
+ const detail = deadlineStopDetail(records)
171
+ return detail === undefined ? undefined : `work deadline reached during ${detail}`
172
+ }
173
+
174
+ interface PartState {
175
+ part: PassStagePart
176
+ items: number
177
+ /** Items the part's gate let start: the first-item rule counts these. */
178
+ started: number
179
+ total?: number
180
+ stopped?: PassStop
181
+ }
182
+
183
+ interface RunningStage {
184
+ stage: PassStage
185
+ startedAt: number
186
+ parts: PartState[]
187
+ current?: PartState
188
+ /** The part whose item started last: the one in flight when a helper
189
+ * returns after the work deadline. */
190
+ lastStarted?: PartState
191
+ }
192
+
193
+ /**
194
+ * The per-pass stage tracker. A stage opens with `begin`; its loops report
195
+ * `total`, `item` and `stop` against a named part, which also becomes the
196
+ * part in flight. Calls made while no stage is open (an unbounded pass) are
197
+ * no-ops.
198
+ */
199
+ export class PassStageTracker {
200
+ readonly records: PassStageRecord[] = []
201
+ private running: RunningStage | undefined
202
+
203
+ constructor(private readonly clock: () => number) {}
204
+
205
+ begin(stage: PassStage): void {
206
+ this.running = { stage, startedAt: this.clock(), parts: [] }
207
+ }
208
+
209
+ /** `part` of the running stage, opened if new, and made the part in flight. */
210
+ private enter(part: PassStagePart): PartState | undefined {
211
+ const running = this.running
212
+ if (running === undefined) return undefined
213
+ let state = running.parts.find((candidate) => candidate.part === part)
214
+ if (state === undefined) {
215
+ state = { part, items: 0, started: 0 }
216
+ running.parts.push(state)
217
+ }
218
+ running.current = state
219
+ return state
220
+ }
221
+
222
+ /** Add `n` to `part`'s total. */
223
+ total(part: PassStagePart, n: number): void {
224
+ const state = this.enter(part)
225
+ if (state !== undefined) state.total = (state.total ?? 0) + n
226
+ }
227
+
228
+ /** Count one item `part` reached; `started` when its gate let it start (a
229
+ * cheap skip is reached, not started). */
230
+ item(part: PassStagePart, started = true): void {
231
+ const state = this.enter(part)
232
+ if (state === undefined) return
233
+ state.items++
234
+ if (started) {
235
+ state.started++
236
+ this.running!.lastStarted = state
237
+ }
238
+ }
239
+
240
+ /** Items `part` of the running stage has started this pass. */
241
+ started(part: PassStagePart): number {
242
+ return this.running?.parts.find((candidate) => candidate.part === part)?.started ?? 0
243
+ }
244
+
245
+ stop(part: PassStagePart, stopped: PassStop): void {
246
+ const state = this.enter(part)
247
+ if (state !== undefined) state.stopped = strongest(state.stopped, stopped)
248
+ }
249
+
250
+ /** Close the running stage into `records`. */
251
+ /** `deadline` when the stage's helpers returned after the work deadline:
252
+ * the item that crossed it, the last one started, had no later gate to
253
+ * mark it, so it is marked here and the stage closes stopped by the
254
+ * deadline. */
255
+ end(deadline = false): void {
256
+ const running = this.running
257
+ if (running === undefined) return
258
+ if (deadline && !running.parts.some((part) => part.stopped === 'deadline')) {
259
+ const crossed = running.lastStarted ?? running.current
260
+ if (crossed !== undefined) crossed.stopped = 'deadline'
261
+ }
262
+ const record = this.close(running)
263
+ this.records.push(deadline ? { ...record, stopped: 'deadline' } : record)
264
+ this.running = undefined
265
+ }
266
+
267
+ /** Every record, with a stage still running closed as stopped by the
268
+ * deadline (its in-flight part too). Never mutates the tracker. */
269
+ summary(): PassStageRecord[] {
270
+ const running = this.running
271
+ if (running === undefined) return this.records.map((record) => structuredClone(record))
272
+ const parts = running.parts.map((part) =>
273
+ part === running.current ? { ...part, stopped: 'deadline' as const } : { ...part },
274
+ )
275
+ const closed = this.close({ ...running, parts })
276
+ return [
277
+ ...this.records.map((record) => structuredClone(record)),
278
+ { ...closed, stopped: 'deadline' },
279
+ ]
280
+ }
281
+
282
+ private close(running: RunningStage): PassStageRecord {
283
+ return aggregateStage(
284
+ running.stage,
285
+ this.clock() - running.startedAt,
286
+ running.parts.map(({ part, items, total, stopped }) => ({
287
+ part,
288
+ items,
289
+ ...(total !== undefined ? { total } : {}),
290
+ ...(stopped !== undefined ? { stopped } : {}),
291
+ })),
292
+ )
293
+ }
294
+ }