@defrex/autobuild 1.3.0 → 1.4.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 +302 -26
- package/docs/accounts.md +38 -0
- package/docs/architecture.md +187 -19
- package/docs/build-store.md +18 -0
- package/docs/builds.md +5 -1
- package/docs/cli.md +1 -1
- package/docs/configuration.md +38 -19
- package/docs/dispatcher-hosting.md +42 -0
- package/docs/dispatcher.md +41 -0
- package/docs/forges.md +5 -0
- package/docs/integration-tree.md +8 -0
- package/docs/operating.md +11 -3
- package/docs/operator-api.md +37 -4
- package/docs/plugin-authoring.md +45 -1
- package/docs/remote-store-protocol.md +258 -42
- package/docs/remote-ticket-protocol.md +12 -10
- package/docs/restacking.md +60 -3
- package/docs/setup.md +37 -8
- package/docs/skills.md +1 -1
- package/docs/spec-standard.md +37 -3
- package/docs/troubleshooting.md +9 -0
- package/docs/workspaces.md +1 -1
- package/package.json +4 -3
- package/packages/core/src/accounts/availability.ts +96 -0
- package/packages/core/src/accounts/local-owner.ts +49 -2
- package/packages/core/src/cli/attention.ts +2 -0
- package/packages/core/src/cli/binary.ts +17 -1
- package/packages/core/src/cli/context.ts +62 -0
- package/packages/core/src/cli/dashboard/model.ts +12 -3
- package/packages/core/src/cli/dashboard/poll.ts +65 -8
- package/packages/core/src/cli/dashboard/refresh.ts +2 -0
- package/packages/core/src/cli/dashboard/render.ts +46 -5
- package/packages/core/src/cli/dispatch-args.ts +27 -3
- package/packages/core/src/cli/dispatch-frontend.ts +178 -52
- package/packages/core/src/cli/dispatch-mode.ts +94 -0
- package/packages/core/src/cli/dispatch-preflight.ts +43 -0
- package/packages/core/src/cli/dispatch-repository-feed.ts +45 -9
- package/packages/core/src/cli/dispatch.ts +145 -11
- package/packages/core/src/cli/harvest.ts +2 -1
- package/packages/core/src/cli/help.ts +16 -3
- package/packages/core/src/cli/historical-build-state.ts +6 -0
- package/packages/core/src/cli/main.ts +15 -2
- package/packages/core/src/cli/publication-settlement.ts +17 -0
- package/packages/core/src/cli/remote-poll.ts +11 -1
- package/packages/core/src/cli/repository-status.ts +11 -0
- package/packages/core/src/cli/status.ts +7 -1
- package/packages/core/src/cli/wait.ts +9 -2
- package/packages/core/src/cli/watch.ts +38 -3
- package/packages/core/src/config/roles.ts +16 -1
- package/packages/core/src/events/catalog.ts +7 -0
- package/packages/core/src/events/payloads.ts +85 -2
- package/packages/core/src/events/policy.ts +10 -0
- package/packages/core/src/events/repository.ts +35 -0
- package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +189 -0
- package/packages/core/src/kernel/dispatch-placement.ts +30 -0
- package/packages/core/src/kernel/engine.ts +19 -12
- package/packages/core/src/kernel/generators/leaf-restack-log.ts +17 -1
- package/packages/core/src/kernel/leaf-restack-state.ts +24 -2
- package/packages/core/src/kernel/log-index.ts +1 -1
- package/packages/core/src/kernel/node-landing.ts +27 -10
- package/packages/core/src/kernel/node-restack-demand.ts +1 -0
- package/packages/core/src/kernel/node-restack-state.ts +16 -1
- package/packages/core/src/kernel/node-restack-transaction.ts +14 -0
- package/packages/core/src/kernel/read-facts.ts +114 -9
- package/packages/core/src/kernel/reconcile-head.ts +34 -11
- package/packages/core/src/kernel/reconcile-restack.ts +106 -72
- package/packages/core/src/kernel/reducer-registry.ts +3 -0
- package/packages/core/src/kernel/reducer.ts +20 -2
- package/packages/core/src/kernel/replay-retention.ts +176 -0
- package/packages/core/src/kernel/target-lineage.ts +65 -0
- package/packages/core/src/kernel/target-observation.ts +117 -0
- package/packages/core/src/operator/registry.ts +5 -13
- package/packages/core/src/operator/tickets.ts +8 -2
- package/packages/core/src/plugin-sdk/index.ts +1 -0
- package/packages/core/src/ports/forge/fake.ts +22 -0
- package/packages/core/src/ports/forge/git-rewrite.ts +49 -12
- package/packages/core/src/ports/forge/github.ts +42 -10
- package/packages/core/src/ports/forge/local-git.ts +16 -1
- package/packages/core/src/ports/types.ts +58 -4
- package/packages/core/src/ports/workspace/account-policy.ts +2 -1
- package/packages/core/src/ports/workspace/commit-preservation.ts +175 -0
- package/packages/core/src/ports/workspace/create.ts +2 -0
- package/packages/core/src/ports/workspace/fake.ts +4 -0
- package/packages/core/src/ports/workspace/git-worktree.ts +22 -1
- package/packages/core/src/ports/workspace/vercel-sandbox.ts +304 -56
- package/packages/core/src/processes/build-runner.ts +240 -111
- package/packages/core/src/processes/dispatcher-selectors.ts +3 -2
- package/packages/core/src/processes/dispatcher.ts +262 -80
- package/packages/core/src/processes/execution-no-progress.ts +7 -0
- package/packages/core/src/processes/harvest-runner.ts +6 -7
- package/packages/core/src/processes/publication-state.ts +2 -1
- package/packages/core/src/processes/repo-view.ts +22 -0
- package/packages/core/src/processes/workspace-preservation.ts +164 -0
- package/packages/core/src/scenarios/harness.ts +7 -0
- package/packages/core/src/store/adapter.ts +9 -0
- package/packages/core/src/store/build-scope.ts +28 -0
- package/packages/core/src/store/contract.ts +125 -0
- package/packages/core/src/store/event-reads.ts +156 -0
- package/packages/core/src/store/local/schema.ts +11 -0
- package/packages/core/src/store/local/store.ts +117 -2
- package/packages/core/src/store/memory.ts +237 -0
- package/packages/core/src/store/phase-session.ts +47 -0
- package/packages/core/src/store/reduced-reads.ts +8 -0
- package/packages/core/src/store/reducer-value-shapes.json +6262 -5227
- package/packages/core/src/store/reducer-value-validation.ts +3 -0
- package/packages/core/src/store/reducer-values.ts +62 -2
- package/packages/core/src/store/remote/client.ts +161 -16
- package/packages/core/src/store/remote/compatibility.ts +177 -46
- package/packages/core/src/store/remote/errors.ts +16 -0
- package/packages/core/src/store/remote/index.ts +11 -0
- package/packages/core/src/store/remote/protocol.ts +22 -0
- package/packages/core/src/store/remote/released-clients.json +37 -0
- package/packages/core/src/store/remote/released-clients.ts +88 -0
- package/packages/core/src/store/remote/released-reducer-values/1.3.0.json +25093 -0
- package/packages/core/src/store/remote/released-reducer-values/1.4.0.json +26128 -0
- package/packages/core/src/store/remote/released-reducer-values/index.ts +13 -0
- package/packages/core/src/store/remote/version.ts +5 -2
- package/packages/core/src/store/repo-state-events.ts +4 -3
- package/packages/core/src/store/scan-cadence.ts +15 -0
- package/packages/core/src/store/session-handle.ts +18 -0
- package/packages/core/src/store/types.ts +27 -6
- package/packages/core/src/testing/dispatch-read-budget.ts +313 -2
- package/packages/core/src/testing/index.ts +5 -0
- package/packages/core/src/testing/reduced-read-budget.ts +728 -5
- package/skills/guide/SKILL.md +177 -16
- package/skills/guide/references/accounts.md +38 -0
- package/skills/guide/references/integration-tree.md +8 -0
- package/skills/guide/references/operator-api.md +37 -4
- package/skills/guide/references/plugin-authoring.md +45 -1
- package/skills/guide/references/remote-store-protocol.md +258 -42
- package/skills/guide/references/remote-ticket-protocol.md +12 -10
- package/skills/guide/references/restacking.md +60 -3
- package/skills/guide/references/setup.md +37 -8
- package/skills/guide/references/spec-standard.md +37 -3
- package/skills/implement/SKILL.md +12 -3
- package/skills/operate/SKILL.md +64 -7
- package/skills/plan/SKILL.md +27 -4
- package/skills/plan-review/SKILL.md +22 -2
- package/skills/reconcile/SKILL.md +10 -1
- package/skills/spec/SKILL.md +7 -4
- package/templates/hosted-service/package.json +2 -2
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
import { restoreReplay } from '../kernel/replay-retention'
|
|
2
|
+
import { reducerValueForClient } from '../store/remote/compatibility'
|
|
3
|
+
import { RELEASED_CLIENTS } from '../store/remote/released-clients'
|
|
4
|
+
import { missingAccountCredentials, reconcileMissingAccounts } from '../accounts/availability'
|
|
1
5
|
import { decideNextFromFacts } from '../kernel/engine'
|
|
6
|
+
import { proveTargetLineage } from '../kernel/target-lineage'
|
|
7
|
+
import { nodeRestackLog } from '../kernel/generators/node-restack-log'
|
|
8
|
+
import { observeTarget } from '../kernel/target-observation'
|
|
2
9
|
import { autoMergeDeferralObservation } from '../kernel/auto-merge'
|
|
3
10
|
import { mkdtemp, writeFile, rm } from 'node:fs/promises'
|
|
4
11
|
import { tmpdir } from 'node:os'
|
|
5
12
|
import { join } from 'node:path'
|
|
13
|
+
import { DashboardBuildPollCache } from '../cli/dashboard/poll'
|
|
6
14
|
import { abDispatch } from '../cli/dispatch'
|
|
7
15
|
import { createTerminalModeController } from '../cli/terminal-restore'
|
|
8
16
|
import { randomUuids } from '../ids'
|
|
@@ -81,6 +89,39 @@ export async function reducedReadBudget(opts: {
|
|
|
81
89
|
},
|
|
82
90
|
})
|
|
83
91
|
await store.createBuild({ slug, repo })
|
|
92
|
+
// Recovery artifacts must never be consulted for an already-present commit,
|
|
93
|
+
// including through fresh adapter instances.
|
|
94
|
+
for (const client of [store, await opts.fresh()]) {
|
|
95
|
+
let replayReads = 0
|
|
96
|
+
const recoveryStore = new Proxy(client, {
|
|
97
|
+
get(target, property) {
|
|
98
|
+
if (property === 'getArtifact')
|
|
99
|
+
return async (...args: Parameters<BuildStore['getArtifact']>) => {
|
|
100
|
+
replayReads++
|
|
101
|
+
return target.getArtifact(...args)
|
|
102
|
+
}
|
|
103
|
+
const value = Reflect.get(target, property)
|
|
104
|
+
return typeof value === 'function' ? value.bind(target) : value
|
|
105
|
+
},
|
|
106
|
+
})
|
|
107
|
+
const recovery = {
|
|
108
|
+
store: recoveryStore,
|
|
109
|
+
slug,
|
|
110
|
+
workspacePath: repo,
|
|
111
|
+
forge: new FakeForge(),
|
|
112
|
+
exec,
|
|
113
|
+
}
|
|
114
|
+
expect(await restoreReplay(recovery, 'a'.repeat(40))).toBe(true)
|
|
115
|
+
expect(replayReads).toBe(0)
|
|
116
|
+
expect(
|
|
117
|
+
await restoreReplay(
|
|
118
|
+
{ ...recovery, exec: async () => ({ stdout: '', stderr: '', exitCode: 1 }) },
|
|
119
|
+
'a'.repeat(40),
|
|
120
|
+
),
|
|
121
|
+
).toBe(false)
|
|
122
|
+
expect(replayReads).toBe(1)
|
|
123
|
+
}
|
|
124
|
+
|
|
84
125
|
await store.append(slug, {
|
|
85
126
|
actor: KERNEL,
|
|
86
127
|
type: 'runner.attached',
|
|
@@ -152,6 +193,16 @@ export async function reducedReadBudget(opts: {
|
|
|
152
193
|
type: 'spec.revised',
|
|
153
194
|
payload: { artifact: { kind: 'spec', rev: 1 }, escalation: escalation.seq },
|
|
154
195
|
})
|
|
196
|
+
const targetForge = new FakeForge()
|
|
197
|
+
const target = 'a'.repeat(40)
|
|
198
|
+
await observeTarget({
|
|
199
|
+
store,
|
|
200
|
+
forge: targetForge,
|
|
201
|
+
slug,
|
|
202
|
+
branch: 'main',
|
|
203
|
+
sha: target,
|
|
204
|
+
actor: KERNEL,
|
|
205
|
+
})
|
|
155
206
|
const reset = () => {
|
|
156
207
|
costs.events = 0
|
|
157
208
|
costs.metadata = 0
|
|
@@ -202,9 +253,14 @@ export async function reducedReadBudget(opts: {
|
|
|
202
253
|
effectiveConfig: { kind: configArtifact.kind, rev: configArtifact.revision },
|
|
203
254
|
},
|
|
204
255
|
})
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
256
|
+
const {
|
|
257
|
+
scan,
|
|
258
|
+
scanSeq: _scanSeq,
|
|
259
|
+
...stableFacts
|
|
260
|
+
} = (await readRepositoryFacts(store, repo)).value
|
|
261
|
+
// The first iteration introduces a scan; its fixed payload is budgeted independently.
|
|
262
|
+
expect(Buffer.byteLength(JSON.stringify(scan ?? null))).toBeLessThan(1_000_000)
|
|
263
|
+
const factsBytes = Buffer.byteLength(JSON.stringify(stableFacts))
|
|
208
264
|
if (repositoryBytes !== undefined) expect(factsBytes).toBeLessThan(repositoryBytes + 512)
|
|
209
265
|
repositoryBytes = factsBytes
|
|
210
266
|
// Warm persisted values independently of any consumer instance.
|
|
@@ -228,6 +284,31 @@ export async function reducedReadBudget(opts: {
|
|
|
228
284
|
for (const operations of [1, 8]) {
|
|
229
285
|
reset()
|
|
230
286
|
for (let n = 0; n < operations; n++) {
|
|
287
|
+
const canonical = await cold.getReducerValue(
|
|
288
|
+
{ kind: 'build', slug },
|
|
289
|
+
{ reducer: 'buildReadFacts' },
|
|
290
|
+
)
|
|
291
|
+
for (const release of RELEASED_CLIENTS) {
|
|
292
|
+
const unknownLatest = structuredClone(canonical)
|
|
293
|
+
if (
|
|
294
|
+
unknownLatest.value &&
|
|
295
|
+
typeof unknownLatest.value === 'object' &&
|
|
296
|
+
'state' in unknownLatest.value
|
|
297
|
+
) {
|
|
298
|
+
const state = unknownLatest.value.state as Record<string, unknown>
|
|
299
|
+
state.lastEvent = {
|
|
300
|
+
type: 'future.optional-display',
|
|
301
|
+
seq: canonical.cursor,
|
|
302
|
+
payload: {},
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const projected = reducerValueForClient(
|
|
306
|
+
{ version: release.version, projects: release.projects ?? false },
|
|
307
|
+
'buildReadFacts',
|
|
308
|
+
unknownLatest,
|
|
309
|
+
)
|
|
310
|
+
expect(projected.cursor).toBe(canonical.cursor)
|
|
311
|
+
}
|
|
231
312
|
await readBuildFacts(cold, slug)
|
|
232
313
|
const revised = (await readBuildFacts(cold, revisedSlug)).value
|
|
233
314
|
expect(decideNextFromFacts(revised.state, revised.log, config)).toEqual({
|
|
@@ -236,11 +317,67 @@ export async function reducedReadBudget(opts: {
|
|
|
236
317
|
round: 1,
|
|
237
318
|
reconcile: { attempt: 1 },
|
|
238
319
|
})
|
|
320
|
+
expect(
|
|
321
|
+
await observeTarget({
|
|
322
|
+
store: cold,
|
|
323
|
+
forge: targetForge,
|
|
324
|
+
slug,
|
|
325
|
+
branch: 'main',
|
|
326
|
+
sha: target,
|
|
327
|
+
actor: KERNEL,
|
|
328
|
+
}),
|
|
329
|
+
).toBe('unchanged')
|
|
239
330
|
await readRepositoryFacts(cold, repo)
|
|
240
331
|
}
|
|
241
332
|
expect(costs.events).toBe(0)
|
|
242
333
|
expect(costs.metadata).toBeLessThan(operations * 20)
|
|
243
334
|
}
|
|
335
|
+
// Account holds survive fresh consumers and reconcile without idle journal reads or writes.
|
|
336
|
+
const missing = missingAccountCredentials(
|
|
337
|
+
{ billing: { kind: 'claude-subscription', credentialEnv: 'BILLING_TOKEN' } },
|
|
338
|
+
{},
|
|
339
|
+
'project-secrets',
|
|
340
|
+
)
|
|
341
|
+
await reconcileMissingAccounts(
|
|
342
|
+
missing,
|
|
343
|
+
(await readRepositoryFacts(store, repo)).value.missingAccounts,
|
|
344
|
+
(event) => store.appendRepo(repo, event),
|
|
345
|
+
)
|
|
346
|
+
await readRepositoryFacts(store, repo) // durable reducer advances exactly once
|
|
347
|
+
const heldPosition = await store.getRepoHighWater(repo)
|
|
348
|
+
reset()
|
|
349
|
+
for (let tick = 0; tick < 8; tick++) {
|
|
350
|
+
const fresh = await opts.fresh()
|
|
351
|
+
// The harness owns the shared connection; these are fresh adapter facades.
|
|
352
|
+
const facts = (await readRepositoryFacts(fresh, repo)).value
|
|
353
|
+
expect(Object.keys(facts.missingAccounts)).toEqual(['billing'])
|
|
354
|
+
await reconcileMissingAccounts(missing, facts.missingAccounts, (event) =>
|
|
355
|
+
fresh.appendRepo(repo, event),
|
|
356
|
+
)
|
|
357
|
+
}
|
|
358
|
+
expect(costs.events).toBe(0)
|
|
359
|
+
expect(await store.getRepoHighWater(repo)).toBe(heldPosition)
|
|
360
|
+
await reconcileMissingAccounts(
|
|
361
|
+
[],
|
|
362
|
+
(await readRepositoryFacts(store, repo)).value.missingAccounts,
|
|
363
|
+
(event) => store.appendRepo(repo, event),
|
|
364
|
+
)
|
|
365
|
+
await readRepositoryFacts(store, repo)
|
|
366
|
+
await readRepositoryAttention(store, repo)
|
|
367
|
+
reset()
|
|
368
|
+
expect((await readRepositoryFacts(cold, repo)).value.missingAccounts).toEqual({})
|
|
369
|
+
expect(costs.events).toBe(0)
|
|
370
|
+
// A new dashboard on every invocation must use canonical persisted facts,
|
|
371
|
+
// even after the build exceeds the hosted page cap. Warm and cold polls
|
|
372
|
+
// select zero event rows at either history length.
|
|
373
|
+
reset()
|
|
374
|
+
for (let invocation = 0; invocation < 3; invocation++) {
|
|
375
|
+
const dashboard = new DashboardBuildPollCache(cold, repo, config)
|
|
376
|
+
expect((await dashboard.refresh()).states.get(slug)?.status).toBe('running')
|
|
377
|
+
await dashboard.refresh()
|
|
378
|
+
}
|
|
379
|
+
expect(costs.events).toBe(0)
|
|
380
|
+
expect(costs.metadata).toBeLessThan(100)
|
|
244
381
|
await coldDispatchOnceBudget(store, cold, length, costs)
|
|
245
382
|
await coldDispatchOnceBudget(store, cold, length, costs, true)
|
|
246
383
|
await coldDispatchOnceBudget(store, cold, length, costs, true, true)
|
|
@@ -252,6 +389,7 @@ export async function reducedReadBudget(opts: {
|
|
|
252
389
|
expect(costs.events).toBe(0)
|
|
253
390
|
expect(costs.metadata).toBeLessThan(100)
|
|
254
391
|
|
|
392
|
+
let scanTime = 1000
|
|
255
393
|
const dispatcher = () =>
|
|
256
394
|
new Dispatcher({
|
|
257
395
|
store: cold,
|
|
@@ -261,10 +399,59 @@ export async function reducedReadBudget(opts: {
|
|
|
261
399
|
workspaces: new FakeWorkspaceProvider({ root: '/budget-workspaces', mode: 'logical' }),
|
|
262
400
|
forge: new FakeForge(),
|
|
263
401
|
ids: sequentialIds(),
|
|
264
|
-
clock: () => new Date(
|
|
402
|
+
clock: () => new Date(scanTime),
|
|
265
403
|
exec,
|
|
266
404
|
launchRunner: async () => 'scheduled',
|
|
267
405
|
})
|
|
406
|
+
await dispatcher().observeReady()
|
|
407
|
+
const scanSeq = (await readRepositoryFacts(cold, repo)).value.scanSeq
|
|
408
|
+
let cadenceReads = 0
|
|
409
|
+
let cadenceWrites = 0
|
|
410
|
+
for (let interval = 0; interval < 3; interval++) {
|
|
411
|
+
const freshHandle = await opts.fresh()
|
|
412
|
+
const counted = new Proxy(freshHandle, {
|
|
413
|
+
get(target, key) {
|
|
414
|
+
if (key === 'getScanCadence')
|
|
415
|
+
return async (...args: Parameters<BuildStore['getScanCadence']>) => {
|
|
416
|
+
cadenceReads++
|
|
417
|
+
return target.getScanCadence(...args)
|
|
418
|
+
}
|
|
419
|
+
if (key === 'putScanCadence')
|
|
420
|
+
return async (...args: Parameters<BuildStore['putScanCadence']>) => {
|
|
421
|
+
cadenceWrites++
|
|
422
|
+
return target.putScanCadence(...args)
|
|
423
|
+
}
|
|
424
|
+
const value = Reflect.get(target, key)
|
|
425
|
+
return typeof value === 'function' ? value.bind(target) : value
|
|
426
|
+
},
|
|
427
|
+
})
|
|
428
|
+
const observe = () =>
|
|
429
|
+
new Dispatcher({
|
|
430
|
+
store: counted,
|
|
431
|
+
repo,
|
|
432
|
+
config,
|
|
433
|
+
tickets: new FakeTicketSource([]),
|
|
434
|
+
workspaces: new FakeWorkspaceProvider({ root: '/budget-workspaces', mode: 'logical' }),
|
|
435
|
+
forge: new FakeForge(),
|
|
436
|
+
ids: sequentialIds(),
|
|
437
|
+
clock: () => new Date(scanTime),
|
|
438
|
+
exec,
|
|
439
|
+
launchRunner: async () => 'scheduled',
|
|
440
|
+
}).observeReady()
|
|
441
|
+
scanTime += 30_000
|
|
442
|
+
reset()
|
|
443
|
+
await observe()
|
|
444
|
+
expect(costs.events).toBe(0)
|
|
445
|
+
expect(costs.metadata).toBeLessThan(100)
|
|
446
|
+
scanTime += 30_000
|
|
447
|
+
reset()
|
|
448
|
+
await observe()
|
|
449
|
+
expect(costs.events).toBe(0)
|
|
450
|
+
expect(costs.metadata).toBeLessThan(100)
|
|
451
|
+
expect((await readRepositoryFacts(freshHandle, repo)).value.scanSeq).toBe(scanSeq)
|
|
452
|
+
}
|
|
453
|
+
expect(cadenceReads).toBe(6)
|
|
454
|
+
expect(cadenceWrites).toBe(3)
|
|
268
455
|
await dispatcher().tick({ acceptNewWork: false })
|
|
269
456
|
await getOperatorDashboard({ store: cold, repo, clock: () => new Date(1000) })
|
|
270
457
|
reset()
|
|
@@ -447,6 +634,101 @@ export async function reducedReadBudget(opts: {
|
|
|
447
634
|
}
|
|
448
635
|
}
|
|
449
636
|
|
|
637
|
+
/** Same SQL-instrumented harness, with indexed receipt reads rather than a
|
|
638
|
+
* growing reduced receipt map. Both warmed and fresh processes pay per gap. */
|
|
639
|
+
export async function targetLineageReadBudget(opts: {
|
|
640
|
+
store: BuildStore
|
|
641
|
+
costs: ReducedReadCosts
|
|
642
|
+
fresh: () => Promise<BuildStore>
|
|
643
|
+
}) {
|
|
644
|
+
const { store, costs } = opts
|
|
645
|
+
const node = 'lineage-budget'
|
|
646
|
+
const oid = (n: number) => n.toString(16).padStart(40, '0')
|
|
647
|
+
await store.createBuild({ slug: node, repo, branch: 'ab-node/lineage-budget' })
|
|
648
|
+
const log = nodeRestackLog()
|
|
649
|
+
for (const event of log.slice(0, 2)) await store.append(node, event)
|
|
650
|
+
for (let i = 1; i <= 3; i++)
|
|
651
|
+
for (const event of log.slice(2)) {
|
|
652
|
+
const payload = JSON.parse(
|
|
653
|
+
JSON.stringify(event.payload)
|
|
654
|
+
.replaceAll('node-tx', `tx-${i}`)
|
|
655
|
+
.replaceAll('a'.repeat(40), oid(i))
|
|
656
|
+
.replaceAll('c'.repeat(40), oid(i + 1)),
|
|
657
|
+
)
|
|
658
|
+
await store.append(node, { actor: event.actor, type: event.type, payload })
|
|
659
|
+
}
|
|
660
|
+
const forge = new FakeForge()
|
|
661
|
+
for (let i = 1; i <= 4; i++) forge.seedCommit(oid(i))
|
|
662
|
+
const bytes = new Map<number, number>()
|
|
663
|
+
let paddingEvents = 0
|
|
664
|
+
let paddingTransaction = 100
|
|
665
|
+
for (const length of [100, 10_000]) {
|
|
666
|
+
// Grow receipt count as well as journal length. Every padding publication
|
|
667
|
+
// is a complete valid transaction with a disjoint previous-head key.
|
|
668
|
+
while (paddingEvents < length) {
|
|
669
|
+
const id = paddingTransaction++
|
|
670
|
+
for (const event of log.slice(2)) {
|
|
671
|
+
const payload = JSON.parse(
|
|
672
|
+
JSON.stringify(event.payload)
|
|
673
|
+
.replaceAll('node-tx', `padding-${id}`)
|
|
674
|
+
.replaceAll('a'.repeat(40), oid(id * 2))
|
|
675
|
+
.replaceAll('c'.repeat(40), oid(id * 2 + 1)),
|
|
676
|
+
)
|
|
677
|
+
await store.append(node, { actor: event.actor, type: event.type, payload })
|
|
678
|
+
paddingEvents++
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
const gapStart = 50_000 + length
|
|
682
|
+
for (let i = 0; i < 3; i++) {
|
|
683
|
+
for (const event of log.slice(2)) {
|
|
684
|
+
const payload = JSON.parse(
|
|
685
|
+
JSON.stringify(event.payload)
|
|
686
|
+
.replaceAll('node-tx', `gap-${length}-${i}`)
|
|
687
|
+
.replaceAll('a'.repeat(40), oid(gapStart + i * 2))
|
|
688
|
+
.replaceAll('c'.repeat(40), oid(gapStart + i * 2 + 1)),
|
|
689
|
+
)
|
|
690
|
+
await store.append(node, { actor: event.actor, type: event.type, payload })
|
|
691
|
+
}
|
|
692
|
+
forge.seedCommit(oid(gapStart + i * 2))
|
|
693
|
+
forge.seedCommit(oid(gapStart + i * 2 + 1))
|
|
694
|
+
if (i > 0) forge.seedCommit(oid(gapStart + i * 2), [oid(gapStart + i * 2 - 1)])
|
|
695
|
+
}
|
|
696
|
+
for (const reader of [store, await opts.fresh()]) {
|
|
697
|
+
costs.events = costs.metadata = costs.bytes = 0
|
|
698
|
+
const proof = await proveTargetLineage({
|
|
699
|
+
store: reader,
|
|
700
|
+
forge,
|
|
701
|
+
node,
|
|
702
|
+
branch: 'ab-node/lineage-budget',
|
|
703
|
+
from: oid(gapStart),
|
|
704
|
+
sha: oid(gapStart + 5),
|
|
705
|
+
})
|
|
706
|
+
expect(proof?.receipts).toHaveLength(3)
|
|
707
|
+
expect(costs.events).toBe(4) // exact first receipt, then three reverse cursor reads
|
|
708
|
+
expect(costs.metadata).toBeLessThan(12)
|
|
709
|
+
if (length === 100) bytes.set(0, costs.bytes)
|
|
710
|
+
else expect(costs.bytes).toBeLessThanOrEqual(bytes.get(0)! + 100)
|
|
711
|
+
}
|
|
712
|
+
for (const reader of [store, await opts.fresh()])
|
|
713
|
+
for (const gap of [1, 3]) {
|
|
714
|
+
costs.events = costs.metadata = costs.bytes = 0
|
|
715
|
+
const proof = await proveTargetLineage({
|
|
716
|
+
store: reader,
|
|
717
|
+
forge,
|
|
718
|
+
node,
|
|
719
|
+
branch: 'ab-node/lineage-budget',
|
|
720
|
+
from: oid(4 - gap),
|
|
721
|
+
sha: oid(4),
|
|
722
|
+
})
|
|
723
|
+
expect(proof?.receipts).toHaveLength(gap)
|
|
724
|
+
expect(costs.events).toBe(gap)
|
|
725
|
+
expect(costs.metadata).toBeLessThan(12)
|
|
726
|
+
if (length === 100) bytes.set(gap, costs.bytes)
|
|
727
|
+
else expect(costs.bytes).toBeLessThanOrEqual(bytes.get(gap)! + 100)
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
450
732
|
/** Exercise the real outer DispatchLoop with a new invocation identity, either
|
|
451
733
|
* once or for cold and warmed interactive ticks. Only shared values are warmed:
|
|
452
734
|
* no parameterized run value may hide a cold replay. */
|
|
@@ -664,6 +946,10 @@ export async function catalogSkewReadBudget(opts: {
|
|
|
664
946
|
{ kind: 'build', slug: opts.slug },
|
|
665
947
|
{ reducer: 'phaseFailures', args: { phase: 'plan', round: 1 } },
|
|
666
948
|
],
|
|
949
|
+
[
|
|
950
|
+
{ kind: 'build', slug: opts.slug },
|
|
951
|
+
{ reducer: 'phaseFailures', args: { phase: 'verify:unit', round: 1 } },
|
|
952
|
+
],
|
|
667
953
|
] as const
|
|
668
954
|
const expected = []
|
|
669
955
|
for (const [scope, request] of reads)
|
|
@@ -692,6 +978,7 @@ export async function parkedSweepReadBudget(opts: {
|
|
|
692
978
|
dispatcher: () => Dispatcher
|
|
693
979
|
pin: (slug: string) => Promise<void>
|
|
694
980
|
advance: () => void
|
|
981
|
+
repairBoundary?: () => Promise<void>
|
|
695
982
|
}) {
|
|
696
983
|
const { store, slugs } = opts
|
|
697
984
|
const kind = 'build-runner-effective-config'
|
|
@@ -723,6 +1010,55 @@ export async function parkedSweepReadBudget(opts: {
|
|
|
723
1010
|
const reset = () => {
|
|
724
1011
|
metrics.meta = metrics.bodies = metrics.bytes = metrics.historicalRows = blobReads = 0
|
|
725
1012
|
}
|
|
1013
|
+
const recoverySlug = 'lost-candidate-read-budget'
|
|
1014
|
+
await store.ensureRepo('/lost-candidate-read-budget')
|
|
1015
|
+
await store.createBuild({ slug: recoverySlug, repo: '/lost-candidate-read-budget' })
|
|
1016
|
+
const missing = '9'.repeat(40)
|
|
1017
|
+
const transaction = {
|
|
1018
|
+
id: 'lost',
|
|
1019
|
+
attempt: 1,
|
|
1020
|
+
branch: `ab/${recoverySlug}`,
|
|
1021
|
+
oldHead: 'a'.repeat(40),
|
|
1022
|
+
oldBase: 'b'.repeat(40),
|
|
1023
|
+
targetBase: 'c'.repeat(40),
|
|
1024
|
+
changeSha: 'd'.repeat(40),
|
|
1025
|
+
workspaceHead: 'a'.repeat(40),
|
|
1026
|
+
localBranchHead: 'a'.repeat(40),
|
|
1027
|
+
conflicted: true,
|
|
1028
|
+
}
|
|
1029
|
+
const request = await store.append(recoverySlug, {
|
|
1030
|
+
actor: KERNEL,
|
|
1031
|
+
type: 'publication.requested',
|
|
1032
|
+
payload: {
|
|
1033
|
+
operation: 'reconcile',
|
|
1034
|
+
branch: transaction.branch,
|
|
1035
|
+
sha: missing,
|
|
1036
|
+
rewrite: transaction,
|
|
1037
|
+
artifact: { kind: 'reconcile-notes', rev: 0 },
|
|
1038
|
+
},
|
|
1039
|
+
})
|
|
1040
|
+
await store.append(recoverySlug, {
|
|
1041
|
+
actor: DISPATCHER,
|
|
1042
|
+
type: 'publication.lost',
|
|
1043
|
+
payload: {
|
|
1044
|
+
request: request.seq,
|
|
1045
|
+
operation: 'reconcile',
|
|
1046
|
+
branch: transaction.branch,
|
|
1047
|
+
sha: missing,
|
|
1048
|
+
reason: 'replacement',
|
|
1049
|
+
},
|
|
1050
|
+
})
|
|
1051
|
+
await store.append(recoverySlug, {
|
|
1052
|
+
actor: KERNEL,
|
|
1053
|
+
type: 'publication.candidate-retired',
|
|
1054
|
+
payload: {
|
|
1055
|
+
request: request.seq,
|
|
1056
|
+
operation: 'reconcile',
|
|
1057
|
+
id: transaction.id,
|
|
1058
|
+
sha: missing,
|
|
1059
|
+
replacementId: 'replacement',
|
|
1060
|
+
},
|
|
1061
|
+
})
|
|
726
1062
|
let seeded = 0
|
|
727
1063
|
for (const length of [100, 100_000]) {
|
|
728
1064
|
for (; seeded < length; seeded++) {
|
|
@@ -732,11 +1068,32 @@ export async function parkedSweepReadBudget(opts: {
|
|
|
732
1068
|
payload: {},
|
|
733
1069
|
})
|
|
734
1070
|
}
|
|
1071
|
+
const recoveryEvents = Reflect.get(store, 'builds').get(recoverySlug)
|
|
1072
|
+
.events as import('../events/catalog').AbEvent[]
|
|
1073
|
+
while (recoveryEvents.length < length)
|
|
1074
|
+
await store.append(recoverySlug, {
|
|
1075
|
+
actor: humanActor('budget'),
|
|
1076
|
+
type: 'build.auto-merge-cancelled',
|
|
1077
|
+
payload: {},
|
|
1078
|
+
})
|
|
1079
|
+
await readBuildFacts(store, recoverySlug)
|
|
1080
|
+
const recoverySlice = Array.prototype.slice
|
|
1081
|
+
const recoveryBoundary = recoveryEvents.at(-1)!.seq
|
|
1082
|
+
recoveryEvents.slice = function (start?: number, end?: number) {
|
|
1083
|
+
const result = recoverySlice.call(this, start, end) as typeof recoveryEvents
|
|
1084
|
+
metrics.historicalRows += result.filter((event) => event.seq <= recoveryBoundary).length
|
|
1085
|
+
return result
|
|
1086
|
+
}
|
|
1087
|
+
const seededTail = (await store.getEvents(slugs[0]!)).at(-1)!.seq
|
|
1088
|
+
// Materialize runner-only reducer keys once, before measuring the next
|
|
1089
|
+
// repair. Canonical first materialization is allowed; subsequent repairs
|
|
1090
|
+
// must advance those durable values over new tail rows only.
|
|
1091
|
+
await opts.repairBoundary?.()
|
|
735
1092
|
// Instrument returned event rows, including getReducerValue's private
|
|
736
1093
|
// snapshot-tail reads (instrumenting getEvents alone would miss these).
|
|
737
1094
|
const build = Reflect.get(store, 'builds').get(slugs[0]!)
|
|
738
1095
|
const events = build.events as import('../events/catalog').AbEvent[]
|
|
739
|
-
const historyTail =
|
|
1096
|
+
const historyTail = seededTail
|
|
740
1097
|
const slice = Array.prototype.slice
|
|
741
1098
|
events.slice = function (start?: number, end?: number) {
|
|
742
1099
|
const result = slice.call(this, start, end) as typeof events
|
|
@@ -745,9 +1102,27 @@ export async function parkedSweepReadBudget(opts: {
|
|
|
745
1102
|
}
|
|
746
1103
|
opts.advance()
|
|
747
1104
|
await opts.dispatcher().tick({ acceptNewWork: false }) // warm durable projections
|
|
1105
|
+
reset()
|
|
1106
|
+
await opts.repairBoundary?.()
|
|
1107
|
+
expect(metrics.historicalRows).toBe(0)
|
|
748
1108
|
const dispatcher = opts.dispatcher()
|
|
749
1109
|
reset()
|
|
750
1110
|
for (let tick = 0; tick < 4; tick++) {
|
|
1111
|
+
const { recoverLostCandidate } = await import('../kernel/reconcile-restack')
|
|
1112
|
+
expect(
|
|
1113
|
+
await recoverLostCandidate({
|
|
1114
|
+
store,
|
|
1115
|
+
slug: recoverySlug,
|
|
1116
|
+
workspacePath: '/unused',
|
|
1117
|
+
forge: new FakeForge(),
|
|
1118
|
+
exec: async () => {
|
|
1119
|
+
throw new Error('retired candidates must not be probed on idle reads')
|
|
1120
|
+
},
|
|
1121
|
+
}),
|
|
1122
|
+
).toBe('none')
|
|
1123
|
+
expect(
|
|
1124
|
+
(await readBuildFacts(store, recoverySlug)).value.candidateRecoveries.reconcile?.sha,
|
|
1125
|
+
).toBe(missing)
|
|
751
1126
|
await dispatcher.tick({ acceptNewWork: false })
|
|
752
1127
|
}
|
|
753
1128
|
expect(metrics).toEqual({
|
|
@@ -1058,3 +1433,351 @@ export async function stoppedOwnerWatchReadBudget() {
|
|
|
1058
1433
|
previousBytes = bytes
|
|
1059
1434
|
}
|
|
1060
1435
|
}
|
|
1436
|
+
|
|
1437
|
+
/** Fresh operator backends must not turn canonical build history into ticket requests. */
|
|
1438
|
+
export async function operatorTicketAssociationBudget() {
|
|
1439
|
+
const { MemoryBuildStore } = await import('../store/memory')
|
|
1440
|
+
const { getOperatorTicket, mutateOperatorTicket } = await import('../operator/tickets')
|
|
1441
|
+
const legacy = 'ticket-12345678-1234-1234-1234-123456789abc'
|
|
1442
|
+
const missing = 'ticket-87654321-4321-4321-4321-cba987654321'
|
|
1443
|
+
const config = parseConfig('[tickets]\nsource="hosted"\nteamKey="AUT"\nreadyState="Todo"')
|
|
1444
|
+
for (const size of [1, 200]) {
|
|
1445
|
+
for (const withLegacy of [false, true]) {
|
|
1446
|
+
let tick = 0
|
|
1447
|
+
const store = new MemoryBuildStore({ clock: () => new Date(++tick * 1000) })
|
|
1448
|
+
await store.ensureRepo(repo)
|
|
1449
|
+
for (let i = 0; i < size; i++)
|
|
1450
|
+
await store.createBuild({
|
|
1451
|
+
slug: `canonical-${i}`,
|
|
1452
|
+
repo,
|
|
1453
|
+
ticket: { source: 'hosted', id: i === 0 ? 'ticket-1' : `OLD-${i}` },
|
|
1454
|
+
})
|
|
1455
|
+
if (withLegacy) {
|
|
1456
|
+
for (const [slug, id] of [
|
|
1457
|
+
['missing', missing],
|
|
1458
|
+
['missing-again', missing],
|
|
1459
|
+
['alias-old', legacy],
|
|
1460
|
+
['alias-middle', legacy],
|
|
1461
|
+
['alias-new', legacy],
|
|
1462
|
+
])
|
|
1463
|
+
await store.createBuild({
|
|
1464
|
+
slug: slug!,
|
|
1465
|
+
repo,
|
|
1466
|
+
ticket: { source: 'hosted', id: id! },
|
|
1467
|
+
})
|
|
1468
|
+
await store.append('alias-old', {
|
|
1469
|
+
actor: DISPATCHER,
|
|
1470
|
+
type: 'build.completed',
|
|
1471
|
+
payload: { outcome: 'merged' },
|
|
1472
|
+
})
|
|
1473
|
+
}
|
|
1474
|
+
// Count actual public event reads, including reads made by reduced-value recovery.
|
|
1475
|
+
let eventReads = 0
|
|
1476
|
+
const checked = new Proxy(store, {
|
|
1477
|
+
get(target, key) {
|
|
1478
|
+
const value = Reflect.get(target, key, target)
|
|
1479
|
+
if (key === 'getEvents' || key === 'getRepoEvents')
|
|
1480
|
+
return (...args: unknown[]) => {
|
|
1481
|
+
eventReads++
|
|
1482
|
+
return value.apply(target, args)
|
|
1483
|
+
}
|
|
1484
|
+
return typeof value === 'function' ? value.bind(target) : value
|
|
1485
|
+
},
|
|
1486
|
+
})
|
|
1487
|
+
for (const operation of ['get', 'update', 'move'] as const) {
|
|
1488
|
+
// A new source and backend per call rules out cross-request caching.
|
|
1489
|
+
const source = new FakeTicketSource([
|
|
1490
|
+
{
|
|
1491
|
+
ref: { source: 'hosted', id: 'AUT-1' },
|
|
1492
|
+
revision: 'fixture',
|
|
1493
|
+
title: 'Undispatched',
|
|
1494
|
+
body: '',
|
|
1495
|
+
state: 'Triage',
|
|
1496
|
+
labels: [],
|
|
1497
|
+
resolved: false,
|
|
1498
|
+
blockedBy: [],
|
|
1499
|
+
},
|
|
1500
|
+
])
|
|
1501
|
+
Object.defineProperty(source, 'name', { value: 'hosted' })
|
|
1502
|
+
const get = source.get.bind(source)
|
|
1503
|
+
const calls: string[] = []
|
|
1504
|
+
source.get = async (id) => {
|
|
1505
|
+
calls.push(id)
|
|
1506
|
+
return get(id === legacy ? 'AUT-1' : id)
|
|
1507
|
+
}
|
|
1508
|
+
const backend = {
|
|
1509
|
+
contextFor: async () => ({
|
|
1510
|
+
repo,
|
|
1511
|
+
tickets: config.tickets,
|
|
1512
|
+
teamKey: 'AUT',
|
|
1513
|
+
}),
|
|
1514
|
+
sourceFor: () => source,
|
|
1515
|
+
statesFor: () => ['Triage', 'Todo'],
|
|
1516
|
+
}
|
|
1517
|
+
const detail =
|
|
1518
|
+
operation === 'get'
|
|
1519
|
+
? await getOperatorTicket({
|
|
1520
|
+
store: checked,
|
|
1521
|
+
repo,
|
|
1522
|
+
backend,
|
|
1523
|
+
id: 'AUT-1',
|
|
1524
|
+
})
|
|
1525
|
+
: await mutateOperatorTicket({
|
|
1526
|
+
store: checked,
|
|
1527
|
+
repo,
|
|
1528
|
+
backend,
|
|
1529
|
+
operation:
|
|
1530
|
+
operation === 'update'
|
|
1531
|
+
? {
|
|
1532
|
+
kind: 'update',
|
|
1533
|
+
id: 'AUT-1',
|
|
1534
|
+
patch: { title: 'Updated' },
|
|
1535
|
+
}
|
|
1536
|
+
: { kind: 'move', id: 'AUT-1', state: 'Todo' },
|
|
1537
|
+
})
|
|
1538
|
+
expect(detail.build?.slug ?? null).toBe(withLegacy ? 'alias-new' : null)
|
|
1539
|
+
expect(calls.filter((id) => id !== 'AUT-1').sort()).toEqual(
|
|
1540
|
+
withLegacy ? [missing, legacy].sort() : [],
|
|
1541
|
+
)
|
|
1542
|
+
expect(calls.filter((id) => id === 'AUT-1')).toHaveLength(
|
|
1543
|
+
operation === 'get' ? 1 : operation === 'update' ? 3 : 2,
|
|
1544
|
+
)
|
|
1545
|
+
expect(eventReads).toBe(0)
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
/** Canonical placement stays warm across fresh consumers and long journals. */
|
|
1552
|
+
export async function placementReadBudget(opts: {
|
|
1553
|
+
select?: (repo: string) => Promise<{ viewer: boolean; dispatchedBy?: string }>
|
|
1554
|
+
store: import('../store/memory').MemoryBuildStore
|
|
1555
|
+
fresh: () => Promise<{
|
|
1556
|
+
getDispatchPlacement(
|
|
1557
|
+
repo: string,
|
|
1558
|
+
): Promise<import('../kernel/dispatch-placement').DispatchPlacement>
|
|
1559
|
+
}>
|
|
1560
|
+
}) {
|
|
1561
|
+
const { readDispatchPlacement } = await import('../store/reduced-reads')
|
|
1562
|
+
const repo = 'https://github.com/acme/placement-budget'
|
|
1563
|
+
await opts.store.ensureRepo(repo)
|
|
1564
|
+
let seeded = 0
|
|
1565
|
+
for (const length of [100, 100_000]) {
|
|
1566
|
+
for (; seeded < length; seeded++)
|
|
1567
|
+
await opts.store.appendRepo(repo, {
|
|
1568
|
+
actor: humanActor('test'),
|
|
1569
|
+
type: 'dispatcher.intake-set',
|
|
1570
|
+
payload: { enabled: true },
|
|
1571
|
+
})
|
|
1572
|
+
await opts.store.appendRepo(repo, {
|
|
1573
|
+
actor: KERNEL,
|
|
1574
|
+
type: 'dispatcher.placement-set',
|
|
1575
|
+
payload: { hosted: false },
|
|
1576
|
+
})
|
|
1577
|
+
await readDispatchPlacement(opts.store, repo)
|
|
1578
|
+
const counter = memoryRepositoryReadCounter(opts.store, repo)
|
|
1579
|
+
counter.reset()
|
|
1580
|
+
for (let i = 0; i < 5; i++) {
|
|
1581
|
+
const fresh = await opts.fresh()
|
|
1582
|
+
expect(await fresh.getDispatchPlacement(repo)).toEqual({
|
|
1583
|
+
status: 'known',
|
|
1584
|
+
placement: { hosted: false },
|
|
1585
|
+
})
|
|
1586
|
+
}
|
|
1587
|
+
expect(counter.rows).toBe(0)
|
|
1588
|
+
expect(counter.reducedCalls).toBe(5)
|
|
1589
|
+
if (opts.select) {
|
|
1590
|
+
counter.reset()
|
|
1591
|
+
for (let i = 0; i < 5; i++) expect(await opts.select(repo)).toEqual({ viewer: false })
|
|
1592
|
+
expect(counter.rows).toBe(0)
|
|
1593
|
+
expect(counter.reducedCalls).toBe(5)
|
|
1594
|
+
}
|
|
1595
|
+
await opts.store.appendRepo(repo, {
|
|
1596
|
+
actor: KERNEL,
|
|
1597
|
+
type: 'dispatcher.placement-set',
|
|
1598
|
+
payload: { hosted: true, origin: 'https://store.test' },
|
|
1599
|
+
})
|
|
1600
|
+
counter.reset()
|
|
1601
|
+
if (opts.select) {
|
|
1602
|
+
expect(await opts.select(repo)).toEqual({ viewer: true, dispatchedBy: 'store.test' })
|
|
1603
|
+
expect(counter.rows).toBe(1)
|
|
1604
|
+
counter.reset()
|
|
1605
|
+
}
|
|
1606
|
+
expect(await (await opts.fresh()).getDispatchPlacement(repo)).toEqual({
|
|
1607
|
+
status: 'known',
|
|
1608
|
+
placement: { hosted: true, origin: 'https://store.test' },
|
|
1609
|
+
})
|
|
1610
|
+
expect(counter.rows).toBe(opts.select ? 0 : 1)
|
|
1611
|
+
counter.reset()
|
|
1612
|
+
if (opts.select) {
|
|
1613
|
+
expect(await opts.select(repo)).toEqual({ viewer: true, dispatchedBy: 'store.test' })
|
|
1614
|
+
expect(counter.rows).toBe(0)
|
|
1615
|
+
}
|
|
1616
|
+
counter.reset()
|
|
1617
|
+
await (await opts.fresh()).getDispatchPlacement(repo)
|
|
1618
|
+
expect(counter.rows).toBe(0)
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
/** Hosted historical seeds: count underlying selections, including the private
|
|
1623
|
+
* canonical fold, rather than response lengths. Run at both exact history sizes. */
|
|
1624
|
+
export async function hostedSeedReadBudget() {
|
|
1625
|
+
const { MemoryBuildStore } = await import('../store/memory')
|
|
1626
|
+
const { CursorTooOldError, RetiredSeedGenerationError, REPOSITORY_TAIL_WINDOW } = await import(
|
|
1627
|
+
'../store/event-reads'
|
|
1628
|
+
)
|
|
1629
|
+
const { REDUCERS } = await import('../kernel/reducer-registry')
|
|
1630
|
+
const { repositoryAttentionReducer } = await import('../kernel/repository-attention')
|
|
1631
|
+
const { buildStreamSeedReducer } = await import('../kernel/read-facts')
|
|
1632
|
+
const gapCosts: number[] = []
|
|
1633
|
+
for (const length of [100, 10_000]) {
|
|
1634
|
+
const store = new MemoryBuildStore()
|
|
1635
|
+
const repo = 'seed-budget',
|
|
1636
|
+
slug = 'seed-budget'
|
|
1637
|
+
await store.ensureRepo(repo)
|
|
1638
|
+
await store.createBuild({ slug, repo })
|
|
1639
|
+
for (let i = 0; i < length; i++) {
|
|
1640
|
+
await store.append(slug, { actor: KERNEL, type: 'build.paused', payload: {} })
|
|
1641
|
+
await store.appendRepo(repo, {
|
|
1642
|
+
actor: humanActor('budget'),
|
|
1643
|
+
type: 'dispatcher.intake-set',
|
|
1644
|
+
payload: { enabled: true },
|
|
1645
|
+
})
|
|
1646
|
+
}
|
|
1647
|
+
const counter = memoryRepositoryReadCounter(store, repo)
|
|
1648
|
+
const scopes = [
|
|
1649
|
+
{ kind: 'build', slug },
|
|
1650
|
+
{ kind: 'repo', repo },
|
|
1651
|
+
] as const
|
|
1652
|
+
const requested = length - 7
|
|
1653
|
+
const { readReducerValue } = await import('../store/reducer-values')
|
|
1654
|
+
const canonicalControl = async (scope: (typeof scopes)[number], name: string) => {
|
|
1655
|
+
const events =
|
|
1656
|
+
scope.kind === 'build' ? await store.getEvents(slug) : await store.getRepoEvents(repo)
|
|
1657
|
+
let rows = 0
|
|
1658
|
+
const answer = await readReducerValue(
|
|
1659
|
+
scope,
|
|
1660
|
+
{ reducer: name },
|
|
1661
|
+
{
|
|
1662
|
+
highWater: async () => length,
|
|
1663
|
+
read: async () => null,
|
|
1664
|
+
events: async (since, through) => {
|
|
1665
|
+
const selected = events.slice(since, through)
|
|
1666
|
+
rows += selected.length
|
|
1667
|
+
return selected
|
|
1668
|
+
},
|
|
1669
|
+
put: async () => true,
|
|
1670
|
+
repair: async () => true,
|
|
1671
|
+
},
|
|
1672
|
+
)
|
|
1673
|
+
expect(rows).toBe(length)
|
|
1674
|
+
return answer
|
|
1675
|
+
}
|
|
1676
|
+
for (const scope of scopes)
|
|
1677
|
+
await canonicalControl(
|
|
1678
|
+
scope,
|
|
1679
|
+
scope.kind === 'build' ? 'buildStreamSeed' : 'repositoryAttention',
|
|
1680
|
+
)
|
|
1681
|
+
await Promise.all(
|
|
1682
|
+
Array.from({ length: 8 }, () =>
|
|
1683
|
+
Promise.all(scopes.map((scope) => store.getStreamSeed(scope, requested))),
|
|
1684
|
+
),
|
|
1685
|
+
)
|
|
1686
|
+
// One serialized durable canonical rebuild per scope, even with cold races.
|
|
1687
|
+
expect(counter.rows).toBe(length)
|
|
1688
|
+
expect(counter.buildRows).toBe(length)
|
|
1689
|
+
counter.reset()
|
|
1690
|
+
for (const scope of scopes) await store.getStreamSeed(scope, requested)
|
|
1691
|
+
const fresh = new RepoViewStore(store, { repo })
|
|
1692
|
+
for (const scope of scopes) await fresh.getStreamSeed!(scope, requested)
|
|
1693
|
+
expect(counter.rows + counter.buildRows).toBe(0)
|
|
1694
|
+
const build = await store.getBuildEventPage(slug, requested)
|
|
1695
|
+
const repository = await store.getRepositoryTail(repo, requested)
|
|
1696
|
+
expect(build.events).toHaveLength(7)
|
|
1697
|
+
expect(repository).toHaveLength(7)
|
|
1698
|
+
gapCosts.push(counter.rows + counter.buildRows)
|
|
1699
|
+
expect(gapCosts.at(-1)).toBe(14)
|
|
1700
|
+
counter.reset()
|
|
1701
|
+
await store.getRepositoryTail(repo, length)
|
|
1702
|
+
await store.getBuildEventPage(slug, length)
|
|
1703
|
+
expect(counter.rows + counter.buildRows).toBe(0)
|
|
1704
|
+
if (length > REPOSITORY_TAIL_WINDOW) {
|
|
1705
|
+
for (const scope of scopes)
|
|
1706
|
+
await expect(
|
|
1707
|
+
store.getStreamSeed(scope, length - REPOSITORY_TAIL_WINDOW - 1),
|
|
1708
|
+
).rejects.toBeInstanceOf(CursorTooOldError)
|
|
1709
|
+
await expect(store.getRepositoryTail(repo, 0)).rejects.toBeInstanceOf(CursorTooOldError)
|
|
1710
|
+
expect(counter.rows + counter.buildRows).toBe(0)
|
|
1711
|
+
for (const scope of scopes)
|
|
1712
|
+
expect((await store.getStreamSeed(scope, length - REPOSITORY_TAIL_WINDOW)).cursor).toBe(
|
|
1713
|
+
length - REPOSITORY_TAIL_WINDOW,
|
|
1714
|
+
)
|
|
1715
|
+
}
|
|
1716
|
+
for (const [name, scope, control] of [
|
|
1717
|
+
['buildStreamSeed', scopes[0], buildStreamSeedReducer],
|
|
1718
|
+
['repositoryAttention', scopes[1], repositoryAttentionReducer],
|
|
1719
|
+
] as const) {
|
|
1720
|
+
const entry = REDUCERS[name]!.valueRead
|
|
1721
|
+
if ('excluded' in entry) throw new Error('missing seed entry')
|
|
1722
|
+
const create = entry.create
|
|
1723
|
+
const validate = entry.validate
|
|
1724
|
+
entry.validate = (state) => {
|
|
1725
|
+
if (!state || typeof state !== 'object') return false
|
|
1726
|
+
const { addedByNewVersion, ...prior } = state as Record<string, unknown>
|
|
1727
|
+
return typeof addedByNewVersion === 'number' && validate(prior)
|
|
1728
|
+
}
|
|
1729
|
+
try {
|
|
1730
|
+
// A new reducer requires a field absent from the old accumulator. No
|
|
1731
|
+
// upgrader or old implementation is supplied to historical lookup.
|
|
1732
|
+
for (let generation = 1; generation <= 2; generation++) {
|
|
1733
|
+
const reducer = create({})
|
|
1734
|
+
entry.create = () => ({
|
|
1735
|
+
...reducer,
|
|
1736
|
+
version: reducer.version + generation,
|
|
1737
|
+
initial: () => ({ ...reducer.initial(), addedByNewVersion: 0 }),
|
|
1738
|
+
finish: (acc) => ({ ...reducer.finish(acc), addedByNewVersion: acc.addedByNewVersion }),
|
|
1739
|
+
advance: (acc, events) => ({
|
|
1740
|
+
...reducer.advance(acc, events),
|
|
1741
|
+
addedByNewVersion: (acc.addedByNewVersion ?? 0) + events.length,
|
|
1742
|
+
}),
|
|
1743
|
+
})
|
|
1744
|
+
const baseline = await canonicalControl(scope, name)
|
|
1745
|
+
counter.reset()
|
|
1746
|
+
const seed = await store.getStreamSeed(scope, requested)
|
|
1747
|
+
expect(counter.rows + counter.buildRows).toBe(length)
|
|
1748
|
+
const events =
|
|
1749
|
+
scope.kind === 'build' ? await store.getEvents(slug) : await store.getRepoEvents(repo)
|
|
1750
|
+
expect(seed.value).toMatchObject(
|
|
1751
|
+
JSON.parse(JSON.stringify(control.reduce(events.slice(0, requested) as never))),
|
|
1752
|
+
)
|
|
1753
|
+
expect(seed.value).toMatchObject({ addedByNewVersion: requested })
|
|
1754
|
+
expect((await store.getStreamSeed(scope, length)).value).toEqual(baseline.value)
|
|
1755
|
+
counter.reset()
|
|
1756
|
+
await store.getStreamSeed(scope, requested)
|
|
1757
|
+
expect(counter.rows + counter.buildRows).toBe(0)
|
|
1758
|
+
}
|
|
1759
|
+
entry.create = create
|
|
1760
|
+
counter.reset()
|
|
1761
|
+
await expect(store.getStreamSeed(scope, requested)).rejects.toBeInstanceOf(
|
|
1762
|
+
RetiredSeedGenerationError,
|
|
1763
|
+
)
|
|
1764
|
+
expect(counter.rows + counter.buildRows).toBe(0)
|
|
1765
|
+
const checkpoints = Reflect.get(store, 'seedCheckpoints') as Map<
|
|
1766
|
+
string,
|
|
1767
|
+
Map<number, unknown>
|
|
1768
|
+
>
|
|
1769
|
+
for (const values of checkpoints.values()) {
|
|
1770
|
+
expect(values.size).toBeLessThanOrEqual(REPOSITORY_TAIL_WINDOW + 1)
|
|
1771
|
+
// Representative fixture checkpoints contain state, never an event prefix.
|
|
1772
|
+
expect(Buffer.byteLength(JSON.stringify(values.values().next().value))).toBeLessThan(
|
|
1773
|
+
64_000,
|
|
1774
|
+
)
|
|
1775
|
+
}
|
|
1776
|
+
} finally {
|
|
1777
|
+
entry.create = create
|
|
1778
|
+
entry.validate = validate
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
expect(gapCosts).toEqual([14, 14])
|
|
1783
|
+
}
|