@defrex/autobuild 1.16.0 → 1.17.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 +91 -7
- package/docs/answering-blocked-builds.md +34 -1
- package/docs/architecture.md +15 -1
- package/docs/configuration.md +33 -4
- package/docs/operator-api.md +15 -5
- package/docs/plugin-authoring.md +1 -0
- package/docs/remote-store-protocol.md +13 -3
- package/docs/remote-ticket-protocol.md +3 -1
- package/docs/restacking.md +41 -1
- package/docs/setup.md +15 -6
- package/docs/troubleshooting.md +39 -2
- package/docs/workspaces.md +2 -0
- package/package.json +3 -3
- package/packages/core/src/cli/accounts.ts +3 -0
- package/packages/core/src/cli/binary-direct.ts +14 -1
- package/packages/core/src/cli/binary.ts +10 -7
- package/packages/core/src/cli/dispatch.ts +503 -334
- package/packages/core/src/cli/secrets.ts +2 -0
- package/packages/core/src/config/schema.ts +15 -2
- package/packages/core/src/events/payloads.ts +8 -5
- package/packages/core/src/kernel/engine.ts +118 -0
- package/packages/core/src/kernel/reconcile-restack.ts +72 -9
- package/packages/core/src/kernel/tracked-dirt.ts +173 -0
- package/packages/core/src/ontology.ts +1 -0
- package/packages/core/src/operator/index.ts +10 -0
- package/packages/core/src/operator/merged-builds.ts +51 -0
- package/packages/core/src/operator/registry.ts +190 -101
- package/packages/core/src/operator/repo-argument.ts +43 -0
- package/packages/core/src/ports/tickets/grants.ts +2 -0
- package/packages/core/src/ports/tickets/remote.ts +2 -0
- package/packages/core/src/ports/types.ts +15 -0
- package/packages/core/src/ports/workspace/vercel-command-diagnostics.ts +144 -0
- package/packages/core/src/ports/workspace/vercel-sandbox.ts +576 -140
- package/packages/core/src/processes/build-child.ts +15 -1
- package/packages/core/src/processes/build-runner.ts +349 -20
- package/packages/core/src/processes/dispatcher.ts +28 -7
- package/packages/core/src/processes/harvest-child.ts +21 -1
- package/packages/core/src/processes/harvest-runner.ts +82 -35
- package/packages/core/src/processes/phase-failure-budget.ts +1 -0
- package/packages/core/src/processes/provisioning-progress.ts +23 -0
- package/packages/core/src/processes/repo-view.ts +25 -4
- package/packages/core/src/processes/workspace-preparation.ts +1 -0
- package/packages/core/src/store/adapter.ts +34 -5
- package/packages/core/src/store/build-feed.ts +148 -22
- package/packages/core/src/store/build-scope.ts +12 -3
- package/packages/core/src/store/caller-attribution.ts +170 -0
- package/packages/core/src/store/contract.ts +1038 -165
- package/packages/core/src/store/local/schema.ts +12 -1
- package/packages/core/src/store/local/store.ts +260 -25
- package/packages/core/src/store/memory.ts +156 -9
- package/packages/core/src/store/phase-session.ts +23 -3
- package/packages/core/src/store/reducer-value-shapes.json +3184 -3175
- package/packages/core/src/store/remote/client.ts +90 -11
- package/packages/core/src/store/remote/credentials.ts +2 -0
- package/packages/core/src/store/remote/index.ts +31 -5
- package/packages/core/src/store/remote/protocol.ts +45 -8
- package/packages/core/src/store/remote/released-clients.json +11 -0
- package/packages/core/src/store/remote/released-reducer-values/1.17.0.json +36367 -0
- package/packages/core/src/store/remote/released-reducer-values/index.ts +18 -16
- package/packages/core/src/store/session-handle.ts +12 -3
- package/packages/core/src/store/types.ts +48 -3
- package/packages/core/src/testing/reduced-read-budget.ts +222 -4
- package/skills/guide/SKILL.md +46 -8
- package/skills/guide/references/operator-api.md +15 -5
- package/skills/guide/references/plugin-authoring.md +1 -0
- package/skills/guide/references/remote-store-protocol.md +13 -3
- package/skills/guide/references/remote-ticket-protocol.md +3 -1
- package/skills/guide/references/restacking.md +41 -1
- package/skills/guide/references/setup.md +15 -6
- package/skills/operate/SKILL.md +24 -3
- package/templates/hosted-service/package.json +2 -2
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { accountRedactor, redactAccountStore } from '../store/account-redaction'
|
|
2
|
+
import { type StoreCaller, withStoreCaller } from '../store/caller-attribution'
|
|
2
3
|
import { creationAssignees } from '../ports/tickets/assignees'
|
|
3
4
|
import { creationLabels } from '../ports/tickets/labels'
|
|
4
5
|
import {
|
|
@@ -116,13 +117,16 @@ export interface HarvestRunnerDeps {
|
|
|
116
117
|
* present, the runner skips its own claim and never releases at the end —
|
|
117
118
|
* the owning loop holds the lease for its lifetime. Without
|
|
118
119
|
* `holdsAuthority` it heartbeats with that holder; a prepared hosted guest
|
|
119
|
-
* instead uses it only to match its durable authorization. Absent
|
|
120
|
-
*
|
|
120
|
+
* instead uses it only to match its durable authorization. Absent, and
|
|
121
|
+
* without `holdsAuthority` or `preparedRun`, keeps the standalone
|
|
122
|
+
* self-claiming behavior. */
|
|
121
123
|
leaseHolder?: string
|
|
122
124
|
/** The owning DispatchLoop's repository authority: synchronous, no I/O.
|
|
123
125
|
* When present, the loop's lease resolver owns reclaiming the adopted
|
|
124
|
-
* lease, so the runner never claims
|
|
125
|
-
*
|
|
126
|
+
* lease, so the runner never claims, renews on its own behalf, or releases
|
|
127
|
+
* the repository lease — even when `leaseHolder` is omitted. A false answer
|
|
128
|
+
* before the run starts returns `held` with no store write; a false answer
|
|
129
|
+
* later stops the run before its next turn starts, as a lost lease. */
|
|
126
130
|
holdsAuthority?: () => boolean
|
|
127
131
|
/** Provider-native identity of the disposable environment this run
|
|
128
132
|
* executes in (hosted harvest). Stamped into the run's harvest.started
|
|
@@ -305,6 +309,22 @@ export class HarvestRunner {
|
|
|
305
309
|
return this.deps.preparedRun !== undefined && this.deps.holdsAuthority === undefined
|
|
306
310
|
}
|
|
307
311
|
|
|
312
|
+
/** Whether this runner claims and releases the repository lease itself.
|
|
313
|
+
* Three ownership modes, mirroring `ensureLease`:
|
|
314
|
+
* - owning authority (`holdsAuthority`, with or without `leaseHolder`): the
|
|
315
|
+
* dispatcher owns the lease; the runner never claims or releases it;
|
|
316
|
+
* - prepared hosted guest (`preparedRun` without `holdsAuthority`): its
|
|
317
|
+
* authority is a durable execution authorization, never the lease;
|
|
318
|
+
* - standalone `ab harvest` (none of the three): claims its own lease
|
|
319
|
+
* under `instance` and releases it at the end. */
|
|
320
|
+
private get selfOwnsLease(): boolean {
|
|
321
|
+
return (
|
|
322
|
+
this.deps.leaseHolder === undefined &&
|
|
323
|
+
this.deps.holdsAuthority === undefined &&
|
|
324
|
+
!this.executionAuthority
|
|
325
|
+
)
|
|
326
|
+
}
|
|
327
|
+
|
|
308
328
|
private currentConfig(): Config {
|
|
309
329
|
return this.deps.getConfig?.() ?? this.deps.config
|
|
310
330
|
}
|
|
@@ -319,14 +339,33 @@ export class HarvestRunner {
|
|
|
319
339
|
)
|
|
320
340
|
}
|
|
321
341
|
|
|
342
|
+
/** Hosted requests the runner makes are attributed to it. Preparation is
|
|
343
|
+
* the dispatcher's own work under its lease, so it keeps the ambient
|
|
344
|
+
* dispatcher attribution. */
|
|
322
345
|
async run(preparationOnly = false): Promise<HarvestRunnerResult> {
|
|
346
|
+
if (preparationOnly) return this.runInScope(true)
|
|
347
|
+
return withStoreCaller(this.runnerCaller(this.deps.preparedRun?.run), () =>
|
|
348
|
+
this.runInScope(false),
|
|
349
|
+
)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
private runnerCaller(harvest: string | undefined): StoreCaller {
|
|
353
|
+
return {
|
|
354
|
+
kind: 'runner',
|
|
355
|
+
runner: 'harvest',
|
|
356
|
+
execution: this.deps.instance,
|
|
357
|
+
...(harvest !== undefined ? { harvest } : {}),
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
private async runInScope(preparationOnly: boolean): Promise<HarvestRunnerResult> {
|
|
323
362
|
const { store, repo, instance } = this.deps
|
|
363
|
+
// An owning authority already lost (e.g. the dispatcher released its
|
|
364
|
+
// lease before this launch ran): exit without touching the store.
|
|
365
|
+
if (this.deps.holdsAuthority !== undefined && !this.deps.holdsAuthority())
|
|
366
|
+
return { outcome: 'held' }
|
|
324
367
|
if (!this.deps.preparedRun) await store.ensureRepo(repo)
|
|
325
|
-
if (
|
|
326
|
-
this.deps.leaseHolder === undefined &&
|
|
327
|
-
!this.executionAuthority &&
|
|
328
|
-
!(await store.claimRepoLease(repo, instance, this.leaseTtlMs))
|
|
329
|
-
) {
|
|
368
|
+
if (this.selfOwnsLease && !(await store.claimRepoLease(repo, instance, this.leaseTtlMs))) {
|
|
330
369
|
return { outcome: 'held' }
|
|
331
370
|
}
|
|
332
371
|
let initial: 'started' | 'resumed' = 'resumed'
|
|
@@ -462,28 +501,33 @@ export class HarvestRunner {
|
|
|
462
501
|
}
|
|
463
502
|
if (!run) throw new Error('prepared harvest run unavailable')
|
|
464
503
|
if (preparationOnly) return { outcome: 'prepared', run: run.run, launch: initial }
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
504
|
+
const claimed = run
|
|
505
|
+
const launch = initial
|
|
506
|
+
// Execution carries the run id: on the local path it is known only now.
|
|
507
|
+
return await withStoreCaller(this.runnerCaller(claimed.run), async () => {
|
|
508
|
+
try {
|
|
509
|
+
await this.ensureScanStep(claimed)
|
|
510
|
+
await this.controlBoundary(claimed.run)
|
|
511
|
+
const outcome = await this.executeWorkflow(claimed)
|
|
512
|
+
return { outcome, launch, run: claimed.run }
|
|
513
|
+
} catch (error) {
|
|
514
|
+
if (
|
|
515
|
+
error instanceof HarvestLeaseLostError ||
|
|
516
|
+
error instanceof HarvestParkedSignal ||
|
|
517
|
+
error instanceof HarvestSuspendedSignal ||
|
|
518
|
+
error instanceof HarvestPersistenceError
|
|
519
|
+
) {
|
|
520
|
+
throw error
|
|
521
|
+
}
|
|
522
|
+
if (error instanceof SessionFailure) {
|
|
523
|
+
await this.settleRecoveryExhaustion(claimed.run)
|
|
524
|
+
return { outcome: 'failed' as const, launch, run: claimed.run }
|
|
525
|
+
}
|
|
526
|
+
await this.recordWorkflowFailure(claimed.run, error)
|
|
527
|
+
await this.settleRecoveryExhaustion(claimed.run)
|
|
528
|
+
return { outcome: 'failed' as const, launch, run: claimed.run }
|
|
482
529
|
}
|
|
483
|
-
|
|
484
|
-
await this.settleRecoveryExhaustion(run.run)
|
|
485
|
-
return { outcome: 'failed', launch: initial, run: run.run }
|
|
486
|
-
}
|
|
530
|
+
})
|
|
487
531
|
} catch (error) {
|
|
488
532
|
if (error instanceof HarvestSuspendedSignal)
|
|
489
533
|
return { outcome: 'suspended', ...(run ? { run: run.run } : {}) }
|
|
@@ -508,10 +552,10 @@ export class HarvestRunner {
|
|
|
508
552
|
}
|
|
509
553
|
this.producer = undefined
|
|
510
554
|
}
|
|
511
|
-
// An adopted holder is released by the
|
|
512
|
-
// a prepared hosted guest never held
|
|
513
|
-
|
|
514
|
-
|
|
555
|
+
// An adopted holder or an owning authority's lease is released by the
|
|
556
|
+
// owning DispatchLoop, never here; a prepared hosted guest never held
|
|
557
|
+
// the lease.
|
|
558
|
+
if (this.selfOwnsLease) await store.releaseRepoLease(repo, instance)
|
|
515
559
|
}
|
|
516
560
|
}
|
|
517
561
|
|
|
@@ -2051,8 +2095,11 @@ export class HarvestRunner {
|
|
|
2051
2095
|
}
|
|
2052
2096
|
|
|
2053
2097
|
private async startHeartbeat(): Promise<void> {
|
|
2054
|
-
// A prepared hosted guest never renews the dispatcher's lease
|
|
2098
|
+
// A prepared hosted guest never renews the dispatcher's lease, and an
|
|
2099
|
+
// owning authority without an adopted holder has nothing of its own to
|
|
2100
|
+
// renew.
|
|
2055
2101
|
if (this.executionAuthority) return
|
|
2102
|
+
if (this.deps.holdsAuthority !== undefined && this.deps.leaseHolder === undefined) return
|
|
2056
2103
|
const { store, repo } = this.deps
|
|
2057
2104
|
const holder = this.deps.leaseHolder ?? this.deps.instance
|
|
2058
2105
|
if (!(await store.heartbeatRepo(repo, holder))) this.leaseLost = true
|
|
@@ -21,6 +21,7 @@ export const POLICY_PHASE_FAILURE_RESET_SCOPES = {
|
|
|
21
21
|
'wait-unresolvable': 'none',
|
|
22
22
|
'measurement-failed': 'none',
|
|
23
23
|
'target-rewritten': 'none',
|
|
24
|
+
'dirty-worktree': 'none',
|
|
24
25
|
} as const satisfies Record<PolicyEscalationCause, PhaseFailureBudgetResetScope>
|
|
25
26
|
|
|
26
27
|
export interface PhaseFailureResetEscalation {
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { WorkspaceProvisionProgress } from '../ports/types'
|
|
2
|
+
|
|
3
|
+
/** The literal recorded when teardown abandons a provision that reported no
|
|
4
|
+
* progress. */
|
|
5
|
+
const ABANDONED = 'provisioning abandoned at dispatcher teardown'
|
|
6
|
+
const ADOPTS = 'the next supervisor adopts the open marker'
|
|
7
|
+
|
|
8
|
+
/** One readable label for a provider's progress report. */
|
|
9
|
+
export function describeProvisionProgress(progress: WorkspaceProvisionProgress): string {
|
|
10
|
+
return progress.step === undefined ? progress.stage : `${progress.stage} ${progress.step}`
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** The `dispatch.failed` message for a provision teardown abandoned: the
|
|
14
|
+
* stage it was in and how long that stage had run, when the provider reported
|
|
15
|
+
* one (#1400). */
|
|
16
|
+
export function provisioningAbandonmentMessage(
|
|
17
|
+
progress: { label: string; since: number } | undefined,
|
|
18
|
+
at: number,
|
|
19
|
+
): string {
|
|
20
|
+
if (progress === undefined) return `${ABANDONED}; ${ADOPTS}`
|
|
21
|
+
const seconds = Math.max(0, Math.round((at - progress.since) / 1000))
|
|
22
|
+
return `${ABANDONED} during ${progress.label} after ${seconds}s; ${ADOPTS}`
|
|
23
|
+
}
|
|
@@ -66,7 +66,13 @@ import type {
|
|
|
66
66
|
SubscribeOptions,
|
|
67
67
|
Unsubscribe,
|
|
68
68
|
} from '../store/types'
|
|
69
|
-
import type {
|
|
69
|
+
import type {
|
|
70
|
+
BuildPage,
|
|
71
|
+
BuildPageOptions,
|
|
72
|
+
ChangedBuilds,
|
|
73
|
+
MergedBuildPage,
|
|
74
|
+
MergedBuildPageOptions,
|
|
75
|
+
} from '../store/types'
|
|
70
76
|
|
|
71
77
|
/** The write committed; only the subsequent local projection refresh failed. */
|
|
72
78
|
export class CommittedRefreshError<T = unknown> extends Error {
|
|
@@ -145,7 +151,12 @@ export class RepoViewStore implements BuildStore {
|
|
|
145
151
|
* `servedBuildFacts` serves them; `buildFacts` always reads. */
|
|
146
152
|
private retained?: Map<
|
|
147
153
|
string,
|
|
148
|
-
{
|
|
154
|
+
{
|
|
155
|
+
epoch: number
|
|
156
|
+
appends: number
|
|
157
|
+
updatedAt?: string
|
|
158
|
+
facts: CurrentBuildFacts
|
|
159
|
+
}
|
|
149
160
|
>
|
|
150
161
|
/** Appends this view made per build: a served read must postdate them. */
|
|
151
162
|
private readonly appends = new Map<string, number>()
|
|
@@ -310,6 +321,10 @@ export class RepoViewStore implements BuildStore {
|
|
|
310
321
|
return this.backing.listBuildsPage(repo, opts)
|
|
311
322
|
}
|
|
312
323
|
|
|
324
|
+
listMergedBuildsPage(repo: string, opts?: MergedBuildPageOptions): Promise<MergedBuildPage> {
|
|
325
|
+
return this.backing.listMergedBuildsPage(repo, opts)
|
|
326
|
+
}
|
|
327
|
+
|
|
313
328
|
listBuildsForRepo(repo: string): Promise<BuildRecord[]> {
|
|
314
329
|
return this.backing.listBuildsForRepo(repo)
|
|
315
330
|
}
|
|
@@ -587,7 +602,10 @@ export class RepoViewStore implements BuildStore {
|
|
|
587
602
|
repo: string,
|
|
588
603
|
artifacts: ArtifactInput[],
|
|
589
604
|
makeEvent: (deposited: RepositoryArtifactMeta[]) => RepositoryEventWrite<T>,
|
|
590
|
-
): Promise<{
|
|
605
|
+
): Promise<{
|
|
606
|
+
event: RepositoryEventEnvelope<T>
|
|
607
|
+
artifacts: RepositoryArtifactMeta[]
|
|
608
|
+
}> {
|
|
591
609
|
const result = await this.backing.appendRepoWithArtifacts(repo, artifacts, makeEvent)
|
|
592
610
|
if (repo === this.repo) {
|
|
593
611
|
this.foldJournalAppend(result.event as RepositoryEvent)
|
|
@@ -783,7 +801,10 @@ export class RepoViewStore implements BuildStore {
|
|
|
783
801
|
id: string,
|
|
784
802
|
artifacts: ArtifactInput[],
|
|
785
803
|
makeEvent: (deposited: SessionArtifactMeta[]) => SessionEventWrite<T>,
|
|
786
|
-
): Promise<{
|
|
804
|
+
): Promise<{
|
|
805
|
+
event: SessionEventEnvelope<T>
|
|
806
|
+
artifacts: SessionArtifactMeta[]
|
|
807
|
+
}> {
|
|
787
808
|
return this.backing.appendSessionWithArtifacts(id, artifacts, makeEvent)
|
|
788
809
|
}
|
|
789
810
|
putSessionArtifact(id: string, artifact: ArtifactInput): Promise<SessionArtifactMeta> {
|
|
@@ -55,6 +55,7 @@ export function decisionUsesWorkspace(decision: Decision): boolean {
|
|
|
55
55
|
case 'raise-escalation':
|
|
56
56
|
case 'recover-target-rewrite':
|
|
57
57
|
case 'accept-target-rewrite':
|
|
58
|
+
case 'supersede-dirty-preparation':
|
|
58
59
|
return false
|
|
59
60
|
default: {
|
|
60
61
|
const unclassified: never = decision
|
|
@@ -7,12 +7,19 @@ export {
|
|
|
7
7
|
repoTickCompletionSchema,
|
|
8
8
|
repoTickObservationSchema,
|
|
9
9
|
} from './repo-tick'
|
|
10
|
-
export type {
|
|
10
|
+
export type {
|
|
11
|
+
RepoTickCompletion,
|
|
12
|
+
RepoTickObservation,
|
|
13
|
+
RepoTickFinish,
|
|
14
|
+
} from './types'
|
|
11
15
|
export { validateEventWrite, prepareEventWrite } from '../events/catalog'
|
|
12
16
|
export { humanActor } from '../events/envelope'
|
|
13
17
|
export type { AbEvent, EventEnvelope, EventWrite } from '../events/catalog'
|
|
14
18
|
export type { EventType } from '../events/payloads'
|
|
15
|
-
export {
|
|
19
|
+
export {
|
|
20
|
+
validateRepositoryEventWrite,
|
|
21
|
+
prepareRepositoryEventWrite,
|
|
22
|
+
} from '../events/repository'
|
|
16
23
|
export type {
|
|
17
24
|
RepositoryEvent,
|
|
18
25
|
RepositoryEventEnvelope,
|
|
@@ -103,7 +110,12 @@ export type {
|
|
|
103
110
|
StreamStatus,
|
|
104
111
|
} from './streams/types'
|
|
105
112
|
export { pollingSubscribe } from './subscribe'
|
|
106
|
-
export {
|
|
113
|
+
export {
|
|
114
|
+
contentHash,
|
|
115
|
+
systemClock,
|
|
116
|
+
toBytes,
|
|
117
|
+
validateExpectedSeq,
|
|
118
|
+
} from './types'
|
|
107
119
|
export type {
|
|
108
120
|
Artifact,
|
|
109
121
|
ArtifactInput,
|
|
@@ -136,7 +148,12 @@ export {
|
|
|
136
148
|
snapshotScopeKey,
|
|
137
149
|
snapshotSupersedes,
|
|
138
150
|
} from './snapshots'
|
|
139
|
-
export type {
|
|
151
|
+
export type {
|
|
152
|
+
ReducerSnapshot,
|
|
153
|
+
ReducerValueScope,
|
|
154
|
+
RollupScope,
|
|
155
|
+
SnapshotScope,
|
|
156
|
+
} from './snapshots'
|
|
140
157
|
export { rollupScopeKey } from './snapshots'
|
|
141
158
|
export {
|
|
142
159
|
KeyedMutex,
|
|
@@ -175,7 +192,10 @@ export { assertPublicSnapshotReducer } from './snapshots'
|
|
|
175
192
|
export { readPersistedBuildDigests } from './reduced-reads'
|
|
176
193
|
export { OpenBuildsCapabilityError, openBuildMember } from './open-builds'
|
|
177
194
|
|
|
178
|
-
export {
|
|
195
|
+
export {
|
|
196
|
+
dispatchPlacementSchema,
|
|
197
|
+
dispatchPlacementReducer,
|
|
198
|
+
} from '../kernel/dispatch-placement'
|
|
179
199
|
export type { DispatchPlacement } from '../kernel/dispatch-placement'
|
|
180
200
|
export { dispatchPlacementFactSchema } from '../events/repository'
|
|
181
201
|
export type { DispatchPlacementFact } from '../events/repository'
|
|
@@ -197,9 +217,13 @@ export {
|
|
|
197
217
|
parseBuildPageFilterParams,
|
|
198
218
|
parseBuildPageLimitParam,
|
|
199
219
|
parseChangePosition,
|
|
220
|
+
parseListingTarget,
|
|
221
|
+
parseMergedBuildWindowParams,
|
|
200
222
|
requireBuildPageCursorOrder,
|
|
223
|
+
requireMergedCursor,
|
|
201
224
|
resolveBuildPageFilters,
|
|
202
225
|
resolveBuildPageLimit,
|
|
226
|
+
resolveMergedBuildWindow,
|
|
203
227
|
sameBuildPageFilters,
|
|
204
228
|
} from './build-feed'
|
|
205
229
|
export type {
|
|
@@ -211,4 +235,9 @@ export type {
|
|
|
211
235
|
BuildPageOrder,
|
|
212
236
|
ChangedBuild,
|
|
213
237
|
ChangedBuilds,
|
|
238
|
+
MergedBuild,
|
|
239
|
+
MergedBuildPage,
|
|
240
|
+
MergedBuildPageOptions,
|
|
241
|
+
MergedBuildTarget,
|
|
242
|
+
MergedBuildWindow,
|
|
214
243
|
} from './build-feed'
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
* Browser-safe: no `Buffer` and no Bun APIs, because the operator web app
|
|
12
12
|
* bundles the store contract and the remote protocol.
|
|
13
13
|
*/
|
|
14
|
+
import { z } from 'zod'
|
|
15
|
+
import { ticketRefSchema, type TicketRef } from '../ontology'
|
|
14
16
|
import type { BuildRecord } from './types'
|
|
15
17
|
|
|
16
18
|
/** One build whose latest change position is after the requested position. */
|
|
@@ -72,11 +74,12 @@ export interface BuildPageOptions extends BuildPageFilters {
|
|
|
72
74
|
limit?: number
|
|
73
75
|
}
|
|
74
76
|
|
|
75
|
-
/** The order a page walks: by creation, or by update under `activeSince
|
|
76
|
-
|
|
77
|
+
/** The order a page walks: by creation, or by update under `activeSince`;
|
|
78
|
+
* `merged` is the merged listing's merge-time order. */
|
|
79
|
+
export type BuildPageOrder = 'created' | 'updated' | 'merged'
|
|
77
80
|
|
|
78
|
-
/** The cursor's decoded sort key: the adapter's exact creation
|
|
79
|
-
* and slug, and which
|
|
81
|
+
/** The cursor's decoded sort key: the adapter's exact creation, update or
|
|
82
|
+
* merge key and slug, and which order it continues. */
|
|
80
83
|
export interface BuildPageCursor {
|
|
81
84
|
order: BuildPageOrder
|
|
82
85
|
at: string
|
|
@@ -95,11 +98,19 @@ export class BuildFeedValidationError extends Error {
|
|
|
95
98
|
* the paged listing. The client never falls back to listing builds or reading
|
|
96
99
|
* events to answer instead. */
|
|
97
100
|
export class BuildFeedUnsupportedError extends Error {
|
|
98
|
-
constructor(
|
|
101
|
+
constructor(
|
|
102
|
+
readonly capability: 'changedBuilds' | 'buildPage' | 'buildPageFilters' | 'mergedBuildPage',
|
|
103
|
+
) {
|
|
99
104
|
super(
|
|
100
105
|
capability === 'buildPageFilters'
|
|
101
106
|
? 'the remote store does not filter the paged build listing'
|
|
102
|
-
: `the remote store does not serve ${
|
|
107
|
+
: `the remote store does not serve ${
|
|
108
|
+
capability === 'changedBuilds'
|
|
109
|
+
? 'the changed-builds feed'
|
|
110
|
+
: capability === 'mergedBuildPage'
|
|
111
|
+
? 'the merged build listing'
|
|
112
|
+
: 'the paged build listing'
|
|
113
|
+
}`,
|
|
103
114
|
)
|
|
104
115
|
this.name = 'BuildFeedUnsupportedError'
|
|
105
116
|
}
|
|
@@ -157,8 +168,13 @@ export function parseBuildPageLimitParam(value: string | null): number {
|
|
|
157
168
|
}
|
|
158
169
|
|
|
159
170
|
/** Cursor format versions: v1 continues creation order (its bytes predate
|
|
160
|
-
* filters and stay unchanged); v2 continues update order
|
|
161
|
-
|
|
171
|
+
* filters and stay unchanged); v2 continues update order; v3 continues the
|
|
172
|
+
* merged listing's merge-time order. */
|
|
173
|
+
const CURSOR_VERSIONS: Record<BuildPageOrder, number> = {
|
|
174
|
+
created: 1,
|
|
175
|
+
updated: 2,
|
|
176
|
+
merged: 3,
|
|
177
|
+
}
|
|
162
178
|
const MAX_CURSOR_LENGTH = 2048
|
|
163
179
|
|
|
164
180
|
function toBase64Url(text: string): string {
|
|
@@ -177,7 +193,11 @@ function fromBase64Url(value: string): string {
|
|
|
177
193
|
/** Encode an adapter's sort key as an opaque cursor. */
|
|
178
194
|
export function encodeBuildPageCursor(cursor: BuildPageCursor): string {
|
|
179
195
|
return toBase64Url(
|
|
180
|
-
JSON.stringify({
|
|
196
|
+
JSON.stringify({
|
|
197
|
+
v: CURSOR_VERSIONS[cursor.order],
|
|
198
|
+
at: cursor.at,
|
|
199
|
+
slug: cursor.slug,
|
|
200
|
+
}),
|
|
181
201
|
)
|
|
182
202
|
}
|
|
183
203
|
|
|
@@ -205,14 +225,15 @@ export function decodeBuildPageCursor(cursor: unknown): BuildPageCursor {
|
|
|
205
225
|
)
|
|
206
226
|
throw invalid()
|
|
207
227
|
const { v, at, slug } = parsed as Record<string, unknown>
|
|
208
|
-
const order = v === 1 ? 'created' : v === 2 ? 'updated' : undefined
|
|
228
|
+
const order = v === 1 ? 'created' : v === 2 ? 'updated' : v === 3 ? 'merged' : undefined
|
|
209
229
|
if (order === undefined || typeof at !== 'string' || typeof slug !== 'string' || at === '')
|
|
210
230
|
throw invalid()
|
|
211
231
|
return { order, at, slug }
|
|
212
232
|
}
|
|
213
233
|
|
|
214
|
-
/** The order a request with these filters walks
|
|
215
|
-
|
|
234
|
+
/** The order a request with these filters walks; never `merged`, so a
|
|
235
|
+
* merged-listing cursor is refused by `listBuildsPage`. */
|
|
236
|
+
export function buildPageOrder(filters: BuildPageFilters): 'created' | 'updated' {
|
|
216
237
|
return filters.activeSince === undefined ? 'created' : 'updated'
|
|
217
238
|
}
|
|
218
239
|
|
|
@@ -230,6 +251,18 @@ export function requireBuildPageCursorOrder(
|
|
|
230
251
|
const ACTIVE_SINCE_PATTERN =
|
|
231
252
|
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d{1,3})?)?(Z|[+-]\d{2}:\d{2})$/
|
|
232
253
|
|
|
254
|
+
/** Parse an ISO instant with a zone into its `toISOString()` form; anything
|
|
255
|
+
* else rejects naming `field`. */
|
|
256
|
+
function resolveInstant(field: string, value: unknown): string {
|
|
257
|
+
const time =
|
|
258
|
+
typeof value === 'string' && ACTIVE_SINCE_PATTERN.test(value) ? Date.parse(value) : NaN
|
|
259
|
+
if (!Number.isFinite(time))
|
|
260
|
+
throw new BuildFeedValidationError(
|
|
261
|
+
`${field} must be an ISO instant with a zone, got ${JSON.stringify(value)}`,
|
|
262
|
+
)
|
|
263
|
+
return new Date(time).toISOString()
|
|
264
|
+
}
|
|
265
|
+
|
|
233
266
|
/**
|
|
234
267
|
* Validate and normalize a request's filters. `activeSince` becomes its
|
|
235
268
|
* `toISOString()` form, which is how every adapter writes `updatedAt`. Only
|
|
@@ -251,16 +284,8 @@ export function resolveBuildPageFilters(opts: BuildPageFilters = {}): BuildPageF
|
|
|
251
284
|
)
|
|
252
285
|
filters.outcome = opts.outcome
|
|
253
286
|
}
|
|
254
|
-
if (opts.activeSince !== undefined)
|
|
255
|
-
|
|
256
|
-
const time =
|
|
257
|
-
typeof value === 'string' && ACTIVE_SINCE_PATTERN.test(value) ? Date.parse(value) : NaN
|
|
258
|
-
if (!Number.isFinite(time))
|
|
259
|
-
throw new BuildFeedValidationError(
|
|
260
|
-
`build page activeSince must be an ISO instant with a zone, got ${JSON.stringify(value)}`,
|
|
261
|
-
)
|
|
262
|
-
filters.activeSince = new Date(time).toISOString()
|
|
263
|
-
}
|
|
287
|
+
if (opts.activeSince !== undefined)
|
|
288
|
+
filters.activeSince = resolveInstant('build page activeSince', opts.activeSince)
|
|
264
289
|
return filters
|
|
265
290
|
}
|
|
266
291
|
|
|
@@ -289,3 +314,104 @@ export function sameBuildPageFilters(
|
|
|
289
314
|
): boolean {
|
|
290
315
|
return FILTER_PARAMS.every((name) => a?.[name] === b?.[name])
|
|
291
316
|
}
|
|
317
|
+
|
|
318
|
+
// ── The merged build listing ────────────────────────────────────────────────
|
|
319
|
+
|
|
320
|
+
/** The merged listing's merge-time window. */
|
|
321
|
+
export interface MergedBuildWindow {
|
|
322
|
+
/** An ISO instant with a zone; builds merged at or after it (inclusive). */
|
|
323
|
+
since?: string
|
|
324
|
+
/** An ISO instant with a zone; builds merged before it (exclusive). */
|
|
325
|
+
until?: string
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export interface MergedBuildPageOptions extends MergedBuildWindow {
|
|
329
|
+
/** A `next` value from an earlier merged page of the same adapter,
|
|
330
|
+
* requested with the same window. */
|
|
331
|
+
cursor?: string
|
|
332
|
+
/** Page size, 1 to `BUILD_PAGE_MAX_LIMIT`; default `BUILD_PAGE_DEFAULT_LIMIT`. */
|
|
333
|
+
limit?: number
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** A merged build's node target, as its `build.created` or latest
|
|
337
|
+
* `node.parent-accepted` recorded it. */
|
|
338
|
+
export interface MergedBuildTarget {
|
|
339
|
+
build: string
|
|
340
|
+
ticket: TicketRef
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** One merged build and its listing facts. */
|
|
344
|
+
export interface MergedBuild {
|
|
345
|
+
record: BuildRecord
|
|
346
|
+
/** The latest `build.completed` (outcome merged) envelope `ts`, ISO. */
|
|
347
|
+
mergedAt: string
|
|
348
|
+
kind: BuildPageKind
|
|
349
|
+
/** From the latest `pr.opened` or `finalize.completed`; absent without one. */
|
|
350
|
+
pr?: { number: number; url: string }
|
|
351
|
+
/** From `build.created`, or a node's latest `node.parent-accepted`. */
|
|
352
|
+
target?: MergedBuildTarget
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** One page of a repository's merged builds, most recently merged first. */
|
|
356
|
+
export interface MergedBuildPage {
|
|
357
|
+
builds: MergedBuild[]
|
|
358
|
+
/** Opaque cursor for the next page; null on the last page. */
|
|
359
|
+
next: string | null
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** Validate and normalize a merged window: each bound becomes its
|
|
363
|
+
* `toISOString()` form, and `since` after `until` rejects. Only present
|
|
364
|
+
* bounds are returned. */
|
|
365
|
+
export function resolveMergedBuildWindow(opts: MergedBuildWindow = {}): MergedBuildWindow {
|
|
366
|
+
const window: MergedBuildWindow = {}
|
|
367
|
+
if (opts.since !== undefined) window.since = resolveInstant('merged builds since', opts.since)
|
|
368
|
+
if (opts.until !== undefined) window.until = resolveInstant('merged builds until', opts.until)
|
|
369
|
+
if (window.since !== undefined && window.until !== undefined && window.since > window.until)
|
|
370
|
+
throw new BuildFeedValidationError(
|
|
371
|
+
`merged builds since (${window.since}) is after until (${window.until})`,
|
|
372
|
+
)
|
|
373
|
+
return window
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** Parse `since` and `until` query parameters; an absent parameter is no
|
|
377
|
+
* bound, and an empty or invalid value rejects. */
|
|
378
|
+
export function parseMergedBuildWindowParams(params: URLSearchParams): MergedBuildWindow {
|
|
379
|
+
const raw: Record<string, string> = {}
|
|
380
|
+
for (const name of ['since', 'until'] as const) {
|
|
381
|
+
const value = params.get(name)
|
|
382
|
+
if (value !== null) raw[name] = value
|
|
383
|
+
}
|
|
384
|
+
return resolveMergedBuildWindow(raw)
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** Decode a merged-listing cursor; a malformed cursor or a cursor of another
|
|
388
|
+
* order rejects. */
|
|
389
|
+
export function requireMergedCursor(cursor: unknown): BuildPageCursor {
|
|
390
|
+
const decoded = decodeBuildPageCursor(cursor)
|
|
391
|
+
if (decoded.order !== 'merged')
|
|
392
|
+
throw new BuildFeedValidationError('build page cursor belongs to a different ordering')
|
|
393
|
+
return decoded
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const listingTargetSchema = z.strictObject({
|
|
397
|
+
build: z.string().min(1),
|
|
398
|
+
ticket: ticketRefSchema,
|
|
399
|
+
})
|
|
400
|
+
|
|
401
|
+
/** Parse a stored target listing fact (JSON text or an already-parsed value);
|
|
402
|
+
* null or undefined is no target, and a malformed value throws. */
|
|
403
|
+
export function parseListingTarget(raw: unknown): MergedBuildTarget | undefined {
|
|
404
|
+
if (raw === null || raw === undefined) return undefined
|
|
405
|
+
let value: unknown = raw
|
|
406
|
+
if (typeof raw === 'string') {
|
|
407
|
+
try {
|
|
408
|
+
value = JSON.parse(raw)
|
|
409
|
+
} catch {
|
|
410
|
+
throw new Error(`malformed build listing target ${JSON.stringify(raw)}`)
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
const parsed = listingTargetSchema.safeParse(value)
|
|
414
|
+
if (!parsed.success) throw new Error(`malformed build listing target ${JSON.stringify(raw)}`)
|
|
415
|
+
const { build, ticket } = parsed.data
|
|
416
|
+
return { build, ticket }
|
|
417
|
+
}
|
|
@@ -29,7 +29,7 @@ import type {
|
|
|
29
29
|
SubscribeOptions,
|
|
30
30
|
Unsubscribe,
|
|
31
31
|
} from './types'
|
|
32
|
-
import type { BuildPage, ChangedBuilds } from './types'
|
|
32
|
+
import type { BuildPage, ChangedBuilds, MergedBuildPage } from './types'
|
|
33
33
|
import type {
|
|
34
34
|
TicketAsset,
|
|
35
35
|
TicketAssetInput,
|
|
@@ -130,6 +130,9 @@ export function createBuildScopedStore(store: BuildStore, scope: string): BuildS
|
|
|
130
130
|
listBuildsPage(repo: string): Promise<BuildPage> {
|
|
131
131
|
return Promise.reject(new BuildScopeError(scope, 'listBuildsPage', repo))
|
|
132
132
|
},
|
|
133
|
+
listMergedBuildsPage(repo: string): Promise<MergedBuildPage> {
|
|
134
|
+
return Promise.reject(new BuildScopeError(scope, 'listMergedBuildsPage', repo))
|
|
135
|
+
},
|
|
133
136
|
listBuilds(): Promise<BuildRecord[]> {
|
|
134
137
|
return Promise.reject(new BuildScopeError(scope, 'listBuilds'))
|
|
135
138
|
},
|
|
@@ -262,7 +265,10 @@ export function createBuildScopedStore(store: BuildStore, scope: string): BuildS
|
|
|
262
265
|
repo: string,
|
|
263
266
|
_artifacts: ArtifactInput[],
|
|
264
267
|
_makeEvent: (deposited: RepositoryArtifactMeta[]) => RepositoryEventWrite<T>,
|
|
265
|
-
): Promise<{
|
|
268
|
+
): Promise<{
|
|
269
|
+
event: RepositoryEventEnvelope<T>
|
|
270
|
+
artifacts: RepositoryArtifactMeta[]
|
|
271
|
+
}> {
|
|
266
272
|
return Promise.reject(new BuildScopeError(scope, 'appendRepoWithArtifacts', repo))
|
|
267
273
|
},
|
|
268
274
|
getRepoEvents(repo: string, _sinceSeq?: number): Promise<RepositoryEvent[]> {
|
|
@@ -412,7 +418,10 @@ export function createBuildScopedStore(store: BuildStore, scope: string): BuildS
|
|
|
412
418
|
id: string,
|
|
413
419
|
_artifacts: ArtifactInput[],
|
|
414
420
|
_makeEvent: (deposited: SessionArtifactMeta[]) => SessionEventWrite<T>,
|
|
415
|
-
): Promise<{
|
|
421
|
+
): Promise<{
|
|
422
|
+
event: SessionEventEnvelope<T>
|
|
423
|
+
artifacts: SessionArtifactMeta[]
|
|
424
|
+
}> {
|
|
416
425
|
return Promise.reject(new BuildScopeError(scope, 'appendSessionWithArtifacts', id))
|
|
417
426
|
},
|
|
418
427
|
putSessionArtifact(id: string, _artifact: ArtifactInput): Promise<SessionArtifactMeta> {
|