@defrex/autobuild 1.20.0 → 1.21.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.
@@ -9,7 +9,13 @@
9
9
  * the work deadline has not passed, so one helper cannot starve its siblings
10
10
  * and no stage can consume a whole pass.
11
11
  */
12
- import { PASS_STAGE_NAMES, PASS_STAGE_PART_NAMES, ROTATED_PASS_PARTS } from '../events/repository'
12
+ import {
13
+ MAX_STAGE_ADMISSIONS,
14
+ PASS_STAGE_NAMES,
15
+ PASS_STAGE_PART_NAMES,
16
+ ROTATED_PASS_PARTS,
17
+ } from '../events/repository'
18
+ import type { BuildPriority } from '../ports/types'
13
19
 
14
20
  export { PASS_STAGE_NAMES, PASS_STAGE_PART_NAMES, ROTATED_PASS_PARTS }
15
21
 
@@ -87,6 +93,16 @@ export interface PassStagePartRecord {
87
93
  carried?: string[]
88
94
  }
89
95
 
96
+ /** One parked admission a stage decided (#1435): `admitted` took a slot,
97
+ * `deferred` waited for this pass's intake to evaluate claims, `carried`
98
+ * left the deferral to the next pass. */
99
+ export interface PassAdmission {
100
+ slug: string
101
+ level: BuildPriority
102
+ outcome: 'admitted' | 'deferred' | 'carried'
103
+ part?: PassStagePart
104
+ }
105
+
90
106
  export interface PassStageRecord {
91
107
  stage: PassStage
92
108
  ms: number
@@ -95,6 +111,12 @@ export interface PassStageRecord {
95
111
  stopped?: PassStop
96
112
  /** Tickets whose preparation the stage carried to the next pass (#1431). */
97
113
  carried?: string[]
114
+ /** Every parked admission the stage decided, each build once with its
115
+ * latest outcome. Uncapped in memory; `boundStageRecords` bounds the
116
+ * durable copy. */
117
+ admissions?: PassAdmission[]
118
+ /** Admissions the durable record omitted beyond its bound. */
119
+ admissionsOmitted?: number
98
120
  parts?: PassStagePartRecord[]
99
121
  }
100
122
 
@@ -114,6 +136,7 @@ export function aggregateStage(
114
136
  stage: PassStage,
115
137
  ms: number,
116
138
  parts: readonly PassStagePartRecord[],
139
+ admissions: readonly PassAdmission[] = [],
117
140
  ): PassStageRecord {
118
141
  let items = 0
119
142
  let total: number | undefined = 0
@@ -132,19 +155,53 @@ export function aggregateStage(
132
155
  ...(total !== undefined ? { total } : {}),
133
156
  ...(stopped !== undefined ? { stopped } : {}),
134
157
  ...(carried.size > 0 ? { carried: [...carried].slice(0, MAX_CARRIED) } : {}),
158
+ ...(admissions.length > 0 ? { admissions: admissions.map((entry) => ({ ...entry })) } : {}),
135
159
  ...(parts.length > 1 ? { parts: parts.map((part) => ({ ...part })) } : {}),
136
160
  }
137
161
  }
138
162
 
163
+ /** The records as they become durable: each keeps its first
164
+ * `MAX_STAGE_ADMISSIONS` admissions and counts the rest in
165
+ * `admissionsOmitted`. The in-memory records keep every admission. */
166
+ export function boundStageRecords(records: readonly PassStageRecord[]): PassStageRecord[] {
167
+ return records.map((record) => {
168
+ const admissions = record.admissions
169
+ if (admissions === undefined || admissions.length <= MAX_STAGE_ADMISSIONS) return record
170
+ return {
171
+ ...record,
172
+ admissions: admissions.slice(0, MAX_STAGE_ADMISSIONS),
173
+ admissionsOmitted: (record.admissionsOmitted ?? 0) + admissions.length - MAX_STAGE_ADMISSIONS,
174
+ }
175
+ })
176
+ }
177
+
139
178
  const progress = (items: number, total: number | undefined) =>
140
179
  total === undefined ? `${items}` : `${items}/${total}`
141
180
 
142
181
  const carriedText = (carried: readonly string[] | undefined) =>
143
182
  carried !== undefined && carried.length > 0 ? ` carried ${carried.join(', ')}` : ''
144
183
 
184
+ const ADMISSION_OUTCOMES = ['admitted', 'deferred', 'carried'] as const
185
+
186
+ /** A stage's parked admissions grouped by outcome, each `slug level`, for
187
+ * example ` admitted a high, b normal; deferred c normal`. */
188
+ function admissionsText(record: PassStageRecord): string {
189
+ const groups = ADMISSION_OUTCOMES.flatMap((outcome) => {
190
+ const named = (record.admissions ?? []).filter((entry) => entry.outcome === outcome)
191
+ return named.length === 0
192
+ ? []
193
+ : [`${outcome} ${named.map((entry) => `${entry.slug} ${entry.level}`).join(', ')}`]
194
+ })
195
+ if (record.admissionsOmitted !== undefined && record.admissionsOmitted > 0)
196
+ groups.push(`and ${record.admissionsOmitted} more admissions`)
197
+ return groups.length === 0 ? '' : ` ${groups.join('; ')}`
198
+ }
199
+
145
200
  /** The dispatch log's `stages:` summary, for example
146
201
  * `settlement 3.1s 6/11 (executions 5/9, harvest 1/2, sandboxes 0/0) · janitor 131.4s 4/12 (share)`.
147
- * A carried preparation reads `intake 2.4s 0/22 (tickets 0/22 carried FLINT-1490, …)`. */
202
+ * A carried preparation reads `intake 2.4s 0/22 (tickets 0/22 carried FLINT-1490, …)`.
203
+ * Parked admissions follow the stage, grouped by outcome:
204
+ * `sweep 41.0s 12/30 (waits 0/0, sweep 12/30) (share) admitted a high; deferred b normal`. */
148
205
  export function formatPassStages(records: readonly PassStageRecord[]): string {
149
206
  return records
150
207
  .map((record) => {
@@ -157,7 +214,7 @@ export function formatPassStages(records: readonly PassStageRecord[]): string {
157
214
  .join(', ')})`
158
215
  : carriedText(record.carried)
159
216
  const stopped = record.stopped ? ` (${record.stopped})` : ''
160
- return `${record.stage} ${(record.ms / 1000).toFixed(1)}s ${progress(record.items, record.total)}${parts}${stopped}`
217
+ return `${record.stage} ${(record.ms / 1000).toFixed(1)}s ${progress(record.items, record.total)}${parts}${stopped}${admissionsText(record)}`
161
218
  })
162
219
  .join(' · ')
163
220
  }
@@ -204,6 +261,8 @@ interface RunningStage {
204
261
  /** The part whose item started last: the one in flight when a helper
205
262
  * returns after the work deadline. */
206
263
  lastStarted?: PartState
264
+ /** Parked admissions decided in this stage, latest outcome per slug. */
265
+ admissions: Map<string, PassAdmission>
207
266
  }
208
267
 
209
268
  /**
@@ -219,7 +278,7 @@ export class PassStageTracker {
219
278
  constructor(private readonly clock: () => number) {}
220
279
 
221
280
  begin(stage: PassStage): void {
222
- this.running = { stage, startedAt: this.clock(), parts: [] }
281
+ this.running = { stage, startedAt: this.clock(), parts: [], admissions: new Map() }
223
282
  }
224
283
 
225
284
  /** `part` of the running stage, opened if new, and made the part in flight. */
@@ -267,6 +326,36 @@ export class PassStageTracker {
267
326
  state.carried.push(ticket)
268
327
  }
269
328
 
329
+ /** Record a parked admission the running stage decided, against the part
330
+ * in flight. Each slug keeps its latest outcome. */
331
+ admission(outcome: PassAdmission['outcome'], slug: string, level: BuildPriority): void {
332
+ const running = this.running
333
+ if (running === undefined) return
334
+ const part = running.current?.part
335
+ running.admissions.set(slug, { slug, level, outcome, ...(part !== undefined ? { part } : {}) })
336
+ }
337
+
338
+ /** Record that the pass carried `slug`'s parked admission to the next
339
+ * pass: on the intake stage, running or closed, where the retry would have
340
+ * run; otherwise on the stage still running, or the last one closed. */
341
+ carriedAdmission(slug: string, level: BuildPriority): void {
342
+ const entry: PassAdmission = { slug, level, outcome: 'carried' }
343
+ const running = this.running
344
+ if (running?.stage === 'intake') {
345
+ running.admissions.set(slug, entry)
346
+ return
347
+ }
348
+ const record =
349
+ this.records.find((candidate) => candidate.stage === 'intake') ??
350
+ (running === undefined ? this.records.at(-1) : undefined)
351
+ if (record === undefined) {
352
+ running?.admissions.set(slug, entry)
353
+ return
354
+ }
355
+ const admissions = (record.admissions ?? []).filter((candidate) => candidate.slug !== slug)
356
+ record.admissions = [...admissions, entry]
357
+ }
358
+
270
359
  stop(part: PassStagePart, stopped: PassStop): void {
271
360
  const state = this.enter(part)
272
361
  if (state !== undefined) state.stopped = strongest(state.stopped, stopped)
@@ -315,6 +404,7 @@ export class PassStageTracker {
315
404
  ...(stopped !== undefined ? { stopped } : {}),
316
405
  ...(carried !== undefined ? { carried: [...carried] } : {}),
317
406
  })),
407
+ [...running.admissions.values()],
318
408
  )
319
409
  }
320
410
  }
@@ -136,6 +136,10 @@ export class RepoViewStore implements BuildStore {
136
136
  private tickSample: RepoTickObservation['finished']
137
137
  private records: BuildRecord[] = []
138
138
  private recordsDirty = true
139
+ /** Records this view's own writes changed since the last listing: the next
140
+ * `listBuilds` re-reads each one (`getBuild`) instead of the whole roster,
141
+ * so a write costs one record read, whatever the fleet's size. */
142
+ private readonly dirtyRecords = new Set<string>()
139
143
  private discovery = new Map<string, BuildDigest>()
140
144
  private readonly dirtyDigests = new Set<string>()
141
145
  private discoveryDirty = true
@@ -186,6 +190,7 @@ export class RepoViewStore implements BuildStore {
186
190
  await this.refreshJournal()
187
191
  await this.metaChain.run(async () => {
188
192
  this.recordsDirty = false
193
+ this.dirtyRecords.clear()
189
194
  this.discoveryDirty = false
190
195
  this.dirtyDigests.clear()
191
196
  this.records = await this.backing.listBuilds()
@@ -335,7 +340,21 @@ export class RepoViewStore implements BuildStore {
335
340
  return this.metaChain.run(async () => {
336
341
  if (this.recordsDirty) {
337
342
  this.recordsDirty = false
343
+ this.dirtyRecords.clear()
338
344
  this.records = await this.backing.listBuilds()
345
+ } else {
346
+ // Own writes since the last listing: one record read each, never the roster.
347
+ for (const slug of [...this.dirtyRecords]) {
348
+ this.dirtyRecords.delete(slug)
349
+ let record: BuildRecord | null
350
+ try {
351
+ record = await this.backing.getBuild(slug)
352
+ } catch (error) {
353
+ this.dirtyRecords.add(slug)
354
+ throw error
355
+ }
356
+ this.placeRecord(slug, record)
357
+ }
339
358
  }
340
359
  return structuredClone(this.records)
341
360
  })
@@ -487,13 +506,33 @@ export class RepoViewStore implements BuildStore {
487
506
  window.events.push(event)
488
507
  window.cursor = event.seq
489
508
  }
490
- private invalidate(slug?: string): void {
491
- this.recordsDirty = true
492
- this.openDirty = true
493
- if (slug) {
494
- this.dirtyDigests.add(slug)
495
- this.appends.set(slug, (this.appends.get(slug) ?? 0) + 1)
509
+ /** Put a re-read record in place: replaced, removed when gone, or inserted
510
+ * in the listing's creation-then-slug order. */
511
+ private placeRecord(slug: string, record: BuildRecord | null): void {
512
+ const at = this.records.findIndex((entry) => entry.slug === slug)
513
+ if (record === null) {
514
+ if (at >= 0) this.records.splice(at, 1)
515
+ return
496
516
  }
517
+ if (at >= 0) {
518
+ this.records[at] = record
519
+ return
520
+ }
521
+ const after = this.records.findIndex(
522
+ (entry) =>
523
+ entry.createdAt.localeCompare(record.createdAt) > 0 ||
524
+ (entry.createdAt === record.createdAt && entry.slug.localeCompare(record.slug) > 0),
525
+ )
526
+ this.records.splice(after < 0 ? this.records.length : after, 0, record)
527
+ }
528
+ /** A write through this view changed `slug`'s record and, unless
529
+ * `recordOnly` (a lease change), its log. */
530
+ private invalidate(slug: string, recordOnly = false): void {
531
+ this.openDirty = true
532
+ this.dirtyRecords.add(slug)
533
+ if (recordOnly) return
534
+ this.dirtyDigests.add(slug)
535
+ this.appends.set(slug, (this.appends.get(slug) ?? 0) + 1)
497
536
  }
498
537
  async createBuild(input: NewBuildInput): Promise<BuildRecord> {
499
538
  const record = await this.backing.createBuild(input)
@@ -625,17 +664,17 @@ export class RepoViewStore implements BuildStore {
625
664
  }
626
665
  async claimLease(slug: string, holder: string, ttlMs: number): Promise<boolean> {
627
666
  const result = await this.backing.claimLease(slug, holder, ttlMs)
628
- this.invalidate()
667
+ this.invalidate(slug, true)
629
668
  return result
630
669
  }
631
670
  async heartbeat(slug: string, holder: string): Promise<boolean> {
632
671
  const result = await this.backing.heartbeat(slug, holder)
633
- this.invalidate()
672
+ this.invalidate(slug, true)
634
673
  return result
635
674
  }
636
675
  async releaseLease(slug: string, holder: string): Promise<void> {
637
676
  await this.backing.releaseLease(slug, holder)
638
- this.invalidate()
677
+ this.invalidate(slug, true)
639
678
  }
640
679
  getScanCadence(repo: string) {
641
680
  return this.backing.getScanCadence(repo)