@defrex/autobuild 1.3.0 → 1.4.1
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 +307 -27
- package/docs/accounts.md +38 -0
- package/docs/architecture.md +198 -25
- 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 +262 -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 +187 -53
- 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/distribution-archive.ts +14 -4
- 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/tick-publication.ts +35 -47
- 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 +48 -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 +201 -18
- 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 +262 -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 +87 -15
- 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
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import type { AbEvent } from '../events/catalog'
|
|
2
|
+
import type { RepositoryEvent } from '../events/repository'
|
|
3
|
+
import type { SnapshotScope } from './snapshots'
|
|
4
|
+
import type { ReducerValue } from './reducer-values'
|
|
5
|
+
|
|
6
|
+
export const REPOSITORY_TAIL_WINDOW = 1_000
|
|
7
|
+
export const BUILD_EVENT_PAGE_SIZE = 256
|
|
8
|
+
export interface EventPage<E> {
|
|
9
|
+
events: E[]
|
|
10
|
+
cursor: number
|
|
11
|
+
hasMore: boolean
|
|
12
|
+
}
|
|
13
|
+
export interface EventReadOptions {
|
|
14
|
+
through?: number
|
|
15
|
+
waitSeconds?: number
|
|
16
|
+
signal?: AbortSignal
|
|
17
|
+
}
|
|
18
|
+
export interface BoundedEventReads {
|
|
19
|
+
getBuildEventPage?(
|
|
20
|
+
slug: string,
|
|
21
|
+
since?: number,
|
|
22
|
+
opts?: EventReadOptions,
|
|
23
|
+
): Promise<EventPage<AbEvent>>
|
|
24
|
+
getRepositoryTail?(
|
|
25
|
+
repo: string,
|
|
26
|
+
since?: number,
|
|
27
|
+
opts?: EventReadOptions,
|
|
28
|
+
): Promise<RepositoryEvent[]>
|
|
29
|
+
getStreamSeed?(scope: SnapshotScope, cursor: number): Promise<ReducerValue>
|
|
30
|
+
getHarvestHistoryPage?(repo: string, before?: number): Promise<EventPage<RepositoryEvent>>
|
|
31
|
+
}
|
|
32
|
+
export function validateEventCursor(cursor: number): void {
|
|
33
|
+
if (!Number.isSafeInteger(cursor) || cursor < 0)
|
|
34
|
+
throw new Error('event cursor must be a safe nonnegative integer')
|
|
35
|
+
}
|
|
36
|
+
export class CursorTooOldError extends Error {
|
|
37
|
+
override readonly name = 'CursorTooOldError'
|
|
38
|
+
readonly oldestCursor: number
|
|
39
|
+
constructor(
|
|
40
|
+
readonly scope: SnapshotScope,
|
|
41
|
+
readonly requestedCursor: number,
|
|
42
|
+
readonly head: number,
|
|
43
|
+
) {
|
|
44
|
+
const oldest = Math.max(0, head - REPOSITORY_TAIL_WINDOW)
|
|
45
|
+
super(
|
|
46
|
+
`cursor ${requestedCursor} is too old; oldest servable cursor is ${oldest}. Re-arm without --since and reconcile repository/build status; the historical gap is unavailable.`,
|
|
47
|
+
)
|
|
48
|
+
this.oldestCursor = oldest
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export class IncompleteEventReadError extends Error {
|
|
52
|
+
override readonly name = 'IncompleteEventReadError'
|
|
53
|
+
constructor(
|
|
54
|
+
readonly requestedCursor: number,
|
|
55
|
+
readonly continuationCursor: number,
|
|
56
|
+
readonly pageLimit = BUILD_EVENT_PAGE_SIZE,
|
|
57
|
+
) {
|
|
58
|
+
super(
|
|
59
|
+
`event suffix exceeds ${pageLimit} events; use getBuildEventPage and continue from ${continuationCursor}`,
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export class CheckpointIntegrityError extends Error {
|
|
64
|
+
override readonly name = 'CheckpointIntegrityError'
|
|
65
|
+
}
|
|
66
|
+
export class RetiredSeedGenerationError extends Error {
|
|
67
|
+
override readonly name = 'RetiredSeedGenerationError'
|
|
68
|
+
constructor() {
|
|
69
|
+
super(
|
|
70
|
+
'retired stream-seed generation; upgrade or restart, re-arm without --since and reconcile status',
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export function assertResumeWindow(scope: SnapshotScope, cursor: number, head: number): void {
|
|
75
|
+
validateEventCursor(cursor)
|
|
76
|
+
if (cursor < Math.max(0, head - REPOSITORY_TAIL_WINDOW))
|
|
77
|
+
throw new CursorTooOldError(scope, cursor, head)
|
|
78
|
+
}
|
|
79
|
+
export function isTerminalEventReadError(error: unknown): boolean {
|
|
80
|
+
return (
|
|
81
|
+
error instanceof CursorTooOldError ||
|
|
82
|
+
error instanceof CheckpointIntegrityError ||
|
|
83
|
+
error instanceof IncompleteEventReadError ||
|
|
84
|
+
error instanceof RetiredSeedGenerationError
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** One page for history UIs; local adapters keep their complete array method. */
|
|
89
|
+
export async function readBuildPage(
|
|
90
|
+
store: import('./types').BuildStore,
|
|
91
|
+
slug: string,
|
|
92
|
+
since = 0,
|
|
93
|
+
opts?: EventReadOptions,
|
|
94
|
+
): Promise<EventPage<AbEvent>> {
|
|
95
|
+
if (store.getBuildEventPage) return store.getBuildEventPage(slug, since, opts)
|
|
96
|
+
const events = (await store.getEvents(slug, since, opts)).filter(
|
|
97
|
+
(event) => event.seq <= (opts?.through ?? Number.MAX_SAFE_INTEGER),
|
|
98
|
+
)
|
|
99
|
+
const page = events.slice(0, BUILD_EVENT_PAGE_SIZE)
|
|
100
|
+
return {
|
|
101
|
+
events: page,
|
|
102
|
+
cursor: page.at(-1)?.seq ?? since,
|
|
103
|
+
hasMore: events.length > BUILD_EVENT_PAGE_SIZE,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** An explicitly required interval freezes its upper boundary before traversal. */
|
|
107
|
+
export async function readBuildRange(
|
|
108
|
+
store: import('./types').BuildStore,
|
|
109
|
+
slug: string,
|
|
110
|
+
since: number,
|
|
111
|
+
opts?: EventReadOptions,
|
|
112
|
+
): Promise<AbEvent[]> {
|
|
113
|
+
if (!store.getBuildEventPage) return store.getEvents(slug, since, opts)
|
|
114
|
+
const through =
|
|
115
|
+
opts?.through ??
|
|
116
|
+
(await store.getReducerValue({ kind: 'build', slug }, { reducer: 'buildStreamSeed' })).cursor
|
|
117
|
+
if (since >= through && opts?.waitSeconds) {
|
|
118
|
+
const first = await store.getBuildEventPage(slug, since, opts)
|
|
119
|
+
if (!first.hasMore) return first.events
|
|
120
|
+
return [
|
|
121
|
+
...first.events,
|
|
122
|
+
...(await readBuildRange(store, slug, first.cursor, { ...opts, waitSeconds: 0 })),
|
|
123
|
+
]
|
|
124
|
+
}
|
|
125
|
+
const events: AbEvent[] = []
|
|
126
|
+
let cursor = since
|
|
127
|
+
do {
|
|
128
|
+
const page = await store.getBuildEventPage(slug, cursor, { ...opts, through })
|
|
129
|
+
events.push(...page.events)
|
|
130
|
+
if (!page.hasMore) return events
|
|
131
|
+
if (page.cursor <= cursor) throw new CheckpointIntegrityError('nonadvancing event page')
|
|
132
|
+
cursor = page.cursor
|
|
133
|
+
} while (cursor < through)
|
|
134
|
+
return events
|
|
135
|
+
}
|
|
136
|
+
export async function readHarvestTail(
|
|
137
|
+
store: import('./types').BuildStore,
|
|
138
|
+
repo: string,
|
|
139
|
+
count: number,
|
|
140
|
+
): Promise<RepositoryEvent[]> {
|
|
141
|
+
if (count <= 0) return []
|
|
142
|
+
if (!store.getHarvestHistoryPage)
|
|
143
|
+
return (await store.getRepoEvents(repo))
|
|
144
|
+
.filter((e) => e.type.startsWith('harvest.'))
|
|
145
|
+
.slice(-count)
|
|
146
|
+
const events: RepositoryEvent[] = []
|
|
147
|
+
let cursor = Number.MAX_SAFE_INTEGER
|
|
148
|
+
while (events.length < count) {
|
|
149
|
+
const page = await store.getHarvestHistoryPage(repo, cursor)
|
|
150
|
+
events.push(...page.events.slice(0, count - events.length))
|
|
151
|
+
if (!page.hasMore) break
|
|
152
|
+
if (page.cursor >= cursor) throw new CheckpointIntegrityError('nonadvancing harvest page')
|
|
153
|
+
cursor = page.cursor
|
|
154
|
+
}
|
|
155
|
+
return events.reverse()
|
|
156
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { sql } from 'drizzle-orm'
|
|
1
2
|
/**
|
|
2
3
|
* Drizzle schema for the local SQLite BuildStore (SPEC §7.1): simple,
|
|
3
4
|
* normalized, boring. Lease + heartbeat are mutable liveness columns on
|
|
@@ -15,6 +16,13 @@ import type { TicketRef } from '../../ontology'
|
|
|
15
16
|
import type { TicketAssetEntry } from '../ticket-assets'
|
|
16
17
|
import type { StreamPart } from '../streams/types'
|
|
17
18
|
|
|
19
|
+
export const scanCadence = sqliteTable('scan_cadence', {
|
|
20
|
+
repo: text('repo')
|
|
21
|
+
.primaryKey()
|
|
22
|
+
.references(() => repoStreams.repo, { onDelete: 'cascade' }),
|
|
23
|
+
value: text('value').notNull(),
|
|
24
|
+
})
|
|
25
|
+
|
|
18
26
|
export const builds = sqliteTable(
|
|
19
27
|
'builds',
|
|
20
28
|
{
|
|
@@ -55,6 +63,9 @@ export const events = sqliteTable(
|
|
|
55
63
|
(t) => [
|
|
56
64
|
primaryKey({ columns: [t.build, t.seq] }),
|
|
57
65
|
index('events_type_build_seq').on(t.type, t.build, t.seq),
|
|
66
|
+
index('events_node_rewrite_previous')
|
|
67
|
+
.on(t.build, sql`json_extract(${t.payload}, '$.previous')`, t.seq)
|
|
68
|
+
.where(sql`${t.type} = 'node.rewritten'`),
|
|
58
69
|
],
|
|
59
70
|
)
|
|
60
71
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { assertResumeWindow, validateEventCursor, type EventReadOptions } from '../event-reads'
|
|
2
|
+
import { scanCadenceSchema, type ScanCadence } from '../scan-cadence'
|
|
1
3
|
import {
|
|
2
4
|
operatorIdentitySchema,
|
|
3
5
|
OPERATOR_PRESENCE_TTL_MS,
|
|
@@ -28,7 +30,7 @@ import type { RepoTickCompletion, RepoTickFinish, RepoTickObservation } from '..
|
|
|
28
30
|
* content-addressed.
|
|
29
31
|
*/
|
|
30
32
|
import { Database } from 'bun:sqlite'
|
|
31
|
-
import { and, asc, desc, eq, gt, gte, inArray, or, sql } from 'drizzle-orm'
|
|
33
|
+
import { and, asc, desc, eq, gt, gte, lte, inArray, or, sql } from 'drizzle-orm'
|
|
32
34
|
import { drizzle, type BunSQLiteDatabase } from 'drizzle-orm/bun-sqlite'
|
|
33
35
|
import { mkdirSync } from 'node:fs'
|
|
34
36
|
import { access, copyFile, mkdtemp, rm } from 'node:fs/promises'
|
|
@@ -41,7 +43,7 @@ import {
|
|
|
41
43
|
type EventEnvelope,
|
|
42
44
|
type EventWrite,
|
|
43
45
|
} from '../../events/catalog'
|
|
44
|
-
import type
|
|
46
|
+
import { eventPayloadSchemas, type EventType } from '../../events/payloads'
|
|
45
47
|
import {
|
|
46
48
|
prepareRepositoryEventWrite as validateRepositoryEventWrite,
|
|
47
49
|
type RepositoryEvent,
|
|
@@ -183,6 +185,10 @@ export const BOOTSTRAP_DDL = [
|
|
|
183
185
|
updated_at TEXT NOT NULL,
|
|
184
186
|
PRIMARY KEY (scope_kind, scope_key, reducer)
|
|
185
187
|
)`,
|
|
188
|
+
`CREATE TABLE IF NOT EXISTS scan_cadence (
|
|
189
|
+
repo TEXT PRIMARY KEY REFERENCES repo_streams(repo) ON DELETE CASCADE,
|
|
190
|
+
value TEXT NOT NULL
|
|
191
|
+
)`,
|
|
186
192
|
`CREATE TABLE IF NOT EXISTS operator_presence (
|
|
187
193
|
repo TEXT NOT NULL, id TEXT NOT NULL, host TEXT NOT NULL, pid INTEGER NOT NULL,
|
|
188
194
|
started_at TEXT NOT NULL, heartbeat_at TEXT NOT NULL, PRIMARY KEY (repo, id)
|
|
@@ -236,6 +242,9 @@ export const BOOTSTRAP_DDL = [
|
|
|
236
242
|
heartbeat_at TEXT,
|
|
237
243
|
tick TEXT
|
|
238
244
|
)`,
|
|
245
|
+
// Injected SQLite connections may leave foreign-key enforcement disabled.
|
|
246
|
+
`CREATE TRIGGER IF NOT EXISTS repo_scan_cadence_delete AFTER DELETE ON repo_streams
|
|
247
|
+
BEGIN DELETE FROM scan_cadence WHERE repo=old.repo; END`,
|
|
239
248
|
`CREATE TABLE IF NOT EXISTS repo_events (
|
|
240
249
|
repo TEXT NOT NULL,
|
|
241
250
|
seq INTEGER NOT NULL,
|
|
@@ -326,6 +335,8 @@ export const BOOTSTRAP_DDL = [
|
|
|
326
335
|
parts TEXT NOT NULL,
|
|
327
336
|
PRIMARY KEY (stream, seq)
|
|
328
337
|
)`,
|
|
338
|
+
`CREATE INDEX IF NOT EXISTS events_node_rewrite_previous ON events
|
|
339
|
+
(build, json_extract(payload, '$.previous'), seq) WHERE type = 'node.rewritten'`,
|
|
329
340
|
REPO_CHANGE_DDL,
|
|
330
341
|
] as const
|
|
331
342
|
|
|
@@ -704,6 +715,32 @@ export class SqliteBuildStore implements BuildStore {
|
|
|
704
715
|
return rows.map((row) => this.toRecord(row))
|
|
705
716
|
}
|
|
706
717
|
|
|
718
|
+
async getNodeRewrite(slug: string, previous?: string, beforePrevious?: string) {
|
|
719
|
+
this.requireBuild(slug)
|
|
720
|
+
const row = (
|
|
721
|
+
beforePrevious !== undefined
|
|
722
|
+
? this.sqlite
|
|
723
|
+
.query(`SELECT payload FROM events
|
|
724
|
+
WHERE type = 'node.rewritten' AND build = ? AND seq < (
|
|
725
|
+
SELECT seq FROM events WHERE type = 'node.rewritten' AND build = ?
|
|
726
|
+
AND json_extract(payload, '$.previous') = ? ORDER BY seq DESC LIMIT 1
|
|
727
|
+
) ORDER BY seq DESC LIMIT 1`)
|
|
728
|
+
.get(slug, slug, beforePrevious)
|
|
729
|
+
: previous === undefined
|
|
730
|
+
? this.sqlite
|
|
731
|
+
.query(`SELECT payload FROM events
|
|
732
|
+
WHERE type = 'node.rewritten' AND build = ? ORDER BY seq DESC LIMIT 1`)
|
|
733
|
+
.get(slug)
|
|
734
|
+
: this.sqlite
|
|
735
|
+
.query(`SELECT payload FROM events
|
|
736
|
+
WHERE type = 'node.rewritten' AND build = ?
|
|
737
|
+
AND json_extract(payload, '$.previous') = ?
|
|
738
|
+
ORDER BY seq DESC LIMIT 1`)
|
|
739
|
+
.get(slug, previous)
|
|
740
|
+
) as { payload: string } | null
|
|
741
|
+
return row ? eventPayloadSchemas['node.rewritten'].parse(JSON.parse(row.payload)) : null
|
|
742
|
+
}
|
|
743
|
+
|
|
707
744
|
async getRepoBuildDigests(repo: string): Promise<Map<string, BuildDigest>> {
|
|
708
745
|
return readPersistedBuildDigests(
|
|
709
746
|
this,
|
|
@@ -1187,6 +1224,41 @@ export class SqliteBuildStore implements BuildStore {
|
|
|
1187
1224
|
})
|
|
1188
1225
|
}
|
|
1189
1226
|
|
|
1227
|
+
async getScanCadence(repo: string): Promise<ScanCadence | null> {
|
|
1228
|
+
const row = this.sqlite.query('SELECT value FROM scan_cadence WHERE repo=?1').get(repo) as {
|
|
1229
|
+
value: string
|
|
1230
|
+
} | null
|
|
1231
|
+
if (!row) return null
|
|
1232
|
+
try {
|
|
1233
|
+
return scanCadenceSchema.safeParse(JSON.parse(row.value)).data ?? null
|
|
1234
|
+
} catch {
|
|
1235
|
+
return null
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
async putScanCadence(
|
|
1239
|
+
repo: string,
|
|
1240
|
+
input: ScanCadence,
|
|
1241
|
+
allowClockRollback = false,
|
|
1242
|
+
): Promise<void> {
|
|
1243
|
+
const cadence = scanCadenceSchema.parse(input)
|
|
1244
|
+
this.writeTx(() => {
|
|
1245
|
+
this.requireRepo(repo)
|
|
1246
|
+
this.sqlite
|
|
1247
|
+
.query(`INSERT INTO scan_cadence (repo,value) VALUES (?1,?2)
|
|
1248
|
+
ON CONFLICT(repo) DO UPDATE SET value=excluded.value
|
|
1249
|
+
WHERE CASE WHEN json_valid(scan_cadence.value) THEN COALESCE(
|
|
1250
|
+
json_extract(scan_cadence.value,'$.scanSeq') <> ?3 OR ?4
|
|
1251
|
+
OR json_extract(scan_cadence.value,'$.startedAt') <= ?5, 1) ELSE 1 END`)
|
|
1252
|
+
.run(
|
|
1253
|
+
repo,
|
|
1254
|
+
JSON.stringify(cadence),
|
|
1255
|
+
cadence.scanSeq,
|
|
1256
|
+
Number(allowClockRollback),
|
|
1257
|
+
cadence.startedAt,
|
|
1258
|
+
)
|
|
1259
|
+
})
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1190
1262
|
async upsertOperatorPresence(
|
|
1191
1263
|
repo: string,
|
|
1192
1264
|
input: OperatorIdentity,
|
|
@@ -1372,6 +1444,49 @@ export class SqliteBuildStore implements BuildStore {
|
|
|
1372
1444
|
})
|
|
1373
1445
|
}
|
|
1374
1446
|
|
|
1447
|
+
async getRepositoryTail(
|
|
1448
|
+
repo: string,
|
|
1449
|
+
since = 0,
|
|
1450
|
+
opts?: EventReadOptions,
|
|
1451
|
+
): Promise<RepositoryEvent[]> {
|
|
1452
|
+
validateEventCursor(since)
|
|
1453
|
+
if (opts?.through !== undefined) validateEventCursor(opts.through)
|
|
1454
|
+
return readEventsWithWait({
|
|
1455
|
+
read: async () => {
|
|
1456
|
+
this.requireRepo(repo)
|
|
1457
|
+
const head = this.snapshotTail({ kind: 'repo', repo })!
|
|
1458
|
+
assertResumeWindow({ kind: 'repo', repo }, since, head)
|
|
1459
|
+
const rows = this.db
|
|
1460
|
+
.select()
|
|
1461
|
+
.from(repoEvents)
|
|
1462
|
+
.where(
|
|
1463
|
+
and(
|
|
1464
|
+
eq(repoEvents.repo, repo),
|
|
1465
|
+
gt(repoEvents.seq, since),
|
|
1466
|
+
lte(repoEvents.seq, Math.min(head, opts?.through ?? head)),
|
|
1467
|
+
),
|
|
1468
|
+
)
|
|
1469
|
+
.orderBy(asc(repoEvents.seq))
|
|
1470
|
+
.all()
|
|
1471
|
+
return rows.map(
|
|
1472
|
+
(row) =>
|
|
1473
|
+
({
|
|
1474
|
+
repo: row.repo,
|
|
1475
|
+
seq: row.seq,
|
|
1476
|
+
ts: row.ts,
|
|
1477
|
+
actor: row.actor,
|
|
1478
|
+
type: row.type,
|
|
1479
|
+
payload: row.payload,
|
|
1480
|
+
...(row.compatibility ? { compatibility: row.compatibility } : {}),
|
|
1481
|
+
}) as RepositoryEvent,
|
|
1482
|
+
)
|
|
1483
|
+
},
|
|
1484
|
+
waitSeconds: opts?.waitSeconds,
|
|
1485
|
+
signal: opts?.signal,
|
|
1486
|
+
pollMs: EVENT_WAIT_POLL_MS,
|
|
1487
|
+
})
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1375
1490
|
async getRepoHighWater(repo: string): Promise<number> {
|
|
1376
1491
|
this.requireRepo(repo)
|
|
1377
1492
|
const row = this.db
|
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
import { scanCadenceSchema, type ScanCadence } from './scan-cadence'
|
|
2
|
+
import {
|
|
3
|
+
BUILD_EVENT_PAGE_SIZE,
|
|
4
|
+
REPOSITORY_TAIL_WINDOW,
|
|
5
|
+
assertResumeWindow,
|
|
6
|
+
validateEventCursor,
|
|
7
|
+
CheckpointIntegrityError,
|
|
8
|
+
RetiredSeedGenerationError,
|
|
9
|
+
type EventReadOptions,
|
|
10
|
+
type EventPage,
|
|
11
|
+
} from './event-reads'
|
|
1
12
|
import {
|
|
2
13
|
operatorIdentitySchema,
|
|
3
14
|
OPERATOR_PRESENCE_TTL_MS,
|
|
@@ -162,6 +173,9 @@ interface BuildState {
|
|
|
162
173
|
heartbeatAt?: string
|
|
163
174
|
}
|
|
164
175
|
lease?: Lease
|
|
176
|
+
latestRewrite?: import('../events/payloads').EventPayload<'node.rewritten'>
|
|
177
|
+
earlierRewrites?: Map<string, import('../events/payloads').EventPayload<'node.rewritten'> | null>
|
|
178
|
+
rewrites?: Map<string, import('../events/payloads').EventPayload<'node.rewritten'>>
|
|
165
179
|
events: AbEvent[]
|
|
166
180
|
/** kind → deposits in revision order (index = revision; 0-based, §6.3). */
|
|
167
181
|
artifacts: Map<string, ArtifactMeta[]>
|
|
@@ -189,6 +203,20 @@ export class MemoryBuildStore implements BuildStore {
|
|
|
189
203
|
private readonly streamLocks = new StreamLocks()
|
|
190
204
|
private readonly sessions = new Map<string, SessionState>()
|
|
191
205
|
/** Reducer snapshots (store/snapshots.ts), keyed by scope and reducer name. */
|
|
206
|
+
private readonly seedCheckpoints = new Map<string, Map<number, ReducerSnapshot>>()
|
|
207
|
+
private readonly seedGenerations = new Map<string, { live: string[]; retired: Set<string> }>()
|
|
208
|
+
private readonly seedLocks = new Map<string, Promise<unknown>>()
|
|
209
|
+
private async withSeedLock<T>(scope: SnapshotScope, body: () => Promise<T>): Promise<T> {
|
|
210
|
+
const key = snapshotScopeKey(scope)
|
|
211
|
+
const previous = this.seedLocks.get(key) ?? Promise.resolve()
|
|
212
|
+
const next = previous.catch(() => {}).then(body)
|
|
213
|
+
this.seedLocks.set(key, next)
|
|
214
|
+
try {
|
|
215
|
+
return await next
|
|
216
|
+
} finally {
|
|
217
|
+
if (this.seedLocks.get(key) === next) this.seedLocks.delete(key)
|
|
218
|
+
}
|
|
219
|
+
}
|
|
192
220
|
private readonly snapshots = new Map<string, ReducerSnapshot>()
|
|
193
221
|
/** `[repo, ticketId, kind, name]` → rows in revision order. A removal is a
|
|
194
222
|
* tombstone row (`removed`, empty manifest) taking the next revision. */
|
|
@@ -355,6 +383,17 @@ export class MemoryBuildStore implements BuildStore {
|
|
|
355
383
|
.sort((a, b) => a.createdAt.localeCompare(b.createdAt) || a.slug.localeCompare(b.slug))
|
|
356
384
|
}
|
|
357
385
|
|
|
386
|
+
async getNodeRewrite(slug: string, previous?: string, beforePrevious?: string) {
|
|
387
|
+
const state = this.state(slug)
|
|
388
|
+
return structuredClone(
|
|
389
|
+
(beforePrevious !== undefined
|
|
390
|
+
? state.earlierRewrites?.get(beforePrevious)
|
|
391
|
+
: previous === undefined
|
|
392
|
+
? state.latestRewrite
|
|
393
|
+
: state.rewrites?.get(previous)) ?? null,
|
|
394
|
+
)
|
|
395
|
+
}
|
|
396
|
+
|
|
358
397
|
async getRepoBuildDigests(repo: string): Promise<Map<string, BuildDigest>> {
|
|
359
398
|
return readPersistedBuildDigests(this, repo)
|
|
360
399
|
}
|
|
@@ -372,6 +411,15 @@ export class MemoryBuildStore implements BuildStore {
|
|
|
372
411
|
compatibility: validated.compatibility,
|
|
373
412
|
} as EventEnvelope<T>
|
|
374
413
|
state.events.push(structuredClone(envelope) as AbEvent)
|
|
414
|
+
if (envelope.type === 'node.rewritten') {
|
|
415
|
+
const receipt =
|
|
416
|
+
envelope.payload as import('../events/payloads').EventPayload<'node.rewritten'>
|
|
417
|
+
state.earlierRewrites ??= new Map()
|
|
418
|
+
state.earlierRewrites.set(receipt.previous, structuredClone(state.latestRewrite ?? null))
|
|
419
|
+
state.rewrites ??= new Map()
|
|
420
|
+
state.rewrites.set(receipt.previous, structuredClone(receipt))
|
|
421
|
+
state.latestRewrite = structuredClone(receipt)
|
|
422
|
+
}
|
|
375
423
|
this.advanceRepo(state.record.repo)
|
|
376
424
|
state.record.updatedAt = envelope.ts
|
|
377
425
|
return envelope
|
|
@@ -400,6 +448,15 @@ export class MemoryBuildStore implements BuildStore {
|
|
|
400
448
|
compatibility: validated.compatibility,
|
|
401
449
|
} as EventEnvelope<T>
|
|
402
450
|
state.events.push(structuredClone(envelope) as AbEvent)
|
|
451
|
+
if (envelope.type === 'node.rewritten') {
|
|
452
|
+
const receipt =
|
|
453
|
+
envelope.payload as import('../events/payloads').EventPayload<'node.rewritten'>
|
|
454
|
+
state.earlierRewrites ??= new Map()
|
|
455
|
+
state.earlierRewrites.set(receipt.previous, structuredClone(state.latestRewrite ?? null))
|
|
456
|
+
state.rewrites ??= new Map()
|
|
457
|
+
state.rewrites.set(receipt.previous, structuredClone(receipt))
|
|
458
|
+
state.latestRewrite = structuredClone(receipt)
|
|
459
|
+
}
|
|
403
460
|
this.advanceRepo(state.record.repo)
|
|
404
461
|
state.record.updatedAt = envelope.ts
|
|
405
462
|
return envelope
|
|
@@ -470,6 +527,15 @@ export class MemoryBuildStore implements BuildStore {
|
|
|
470
527
|
compatibility: validated.compatibility,
|
|
471
528
|
} as EventEnvelope<T>
|
|
472
529
|
state.events.push(structuredClone(envelope) as AbEvent)
|
|
530
|
+
if (envelope.type === 'node.rewritten') {
|
|
531
|
+
const receipt =
|
|
532
|
+
envelope.payload as import('../events/payloads').EventPayload<'node.rewritten'>
|
|
533
|
+
state.earlierRewrites ??= new Map()
|
|
534
|
+
state.earlierRewrites.set(receipt.previous, structuredClone(state.latestRewrite ?? null))
|
|
535
|
+
state.rewrites ??= new Map()
|
|
536
|
+
state.rewrites.set(receipt.previous, structuredClone(receipt))
|
|
537
|
+
state.latestRewrite = structuredClone(receipt)
|
|
538
|
+
}
|
|
473
539
|
this.advanceRepo(state.record.repo)
|
|
474
540
|
state.record.updatedAt = ts
|
|
475
541
|
return { event: envelope, artifacts: structuredClone(deposited) }
|
|
@@ -543,6 +609,15 @@ export class MemoryBuildStore implements BuildStore {
|
|
|
543
609
|
compatibility: validated.compatibility,
|
|
544
610
|
} as EventEnvelope<T>
|
|
545
611
|
state.events.push(structuredClone(envelope) as AbEvent)
|
|
612
|
+
if (envelope.type === 'node.rewritten') {
|
|
613
|
+
const receipt =
|
|
614
|
+
envelope.payload as import('../events/payloads').EventPayload<'node.rewritten'>
|
|
615
|
+
state.earlierRewrites ??= new Map()
|
|
616
|
+
state.earlierRewrites.set(receipt.previous, structuredClone(state.latestRewrite ?? null))
|
|
617
|
+
state.rewrites ??= new Map()
|
|
618
|
+
state.rewrites.set(receipt.previous, structuredClone(receipt))
|
|
619
|
+
state.latestRewrite = structuredClone(receipt)
|
|
620
|
+
}
|
|
546
621
|
this.advanceRepo(state.record.repo)
|
|
547
622
|
state.record.updatedAt = ts
|
|
548
623
|
return { event: envelope, artifacts: structuredClone(deposited) }
|
|
@@ -722,6 +797,27 @@ export class MemoryBuildStore implements BuildStore {
|
|
|
722
797
|
return this.repoSnapshot(state)
|
|
723
798
|
}
|
|
724
799
|
|
|
800
|
+
private readonly scanCadence = new Map<string, ScanCadence>()
|
|
801
|
+
async getScanCadence(repo: string): Promise<ScanCadence | null> {
|
|
802
|
+
return structuredClone(this.scanCadence.get(repo) ?? null)
|
|
803
|
+
}
|
|
804
|
+
async putScanCadence(
|
|
805
|
+
repo: string,
|
|
806
|
+
input: ScanCadence,
|
|
807
|
+
allowClockRollback = false,
|
|
808
|
+
): Promise<void> {
|
|
809
|
+
if (!this.repos.has(repo)) throw new Error(`unknown repo "${repo}"`)
|
|
810
|
+
const cadence = scanCadenceSchema.parse(input)
|
|
811
|
+
const previous = this.scanCadence.get(repo)
|
|
812
|
+
if (
|
|
813
|
+
!previous ||
|
|
814
|
+
previous.scanSeq !== cadence.scanSeq ||
|
|
815
|
+
(previous.scanSeq === cadence.scanSeq &&
|
|
816
|
+
(allowClockRollback || Date.parse(cadence.startedAt) >= Date.parse(previous.startedAt)))
|
|
817
|
+
)
|
|
818
|
+
this.scanCadence.set(repo, structuredClone(cadence))
|
|
819
|
+
}
|
|
820
|
+
|
|
725
821
|
private readonly operators = new Map<string, Map<string, OperatorPresence>>()
|
|
726
822
|
|
|
727
823
|
async upsertOperatorPresence(
|
|
@@ -879,6 +975,69 @@ export class MemoryBuildStore implements BuildStore {
|
|
|
879
975
|
return this.repoState(repo).events.at(-1)?.seq ?? 0
|
|
880
976
|
}
|
|
881
977
|
|
|
978
|
+
async getBuildEventPage(
|
|
979
|
+
slug: string,
|
|
980
|
+
since = 0,
|
|
981
|
+
opts?: EventReadOptions,
|
|
982
|
+
): Promise<EventPage<AbEvent>> {
|
|
983
|
+
validateEventCursor(since)
|
|
984
|
+
if (opts?.through !== undefined) validateEventCursor(opts.through)
|
|
985
|
+
let more = false
|
|
986
|
+
const page = await readEventsWithWait({
|
|
987
|
+
read: async () => {
|
|
988
|
+
const through = Math.min(
|
|
989
|
+
this.state(slug).events.length,
|
|
990
|
+
opts?.through ?? Number.MAX_SAFE_INTEGER,
|
|
991
|
+
)
|
|
992
|
+
const selected = this.state(slug).events.slice(
|
|
993
|
+
since,
|
|
994
|
+
Math.min(through, since + BUILD_EVENT_PAGE_SIZE + 1),
|
|
995
|
+
)
|
|
996
|
+
more = selected.length > BUILD_EVENT_PAGE_SIZE
|
|
997
|
+
return structuredClone(selected.slice(0, BUILD_EVENT_PAGE_SIZE))
|
|
998
|
+
},
|
|
999
|
+
waitSeconds: opts?.waitSeconds,
|
|
1000
|
+
signal: opts?.signal,
|
|
1001
|
+
pollMs: EVENT_WAIT_POLL_MS,
|
|
1002
|
+
})
|
|
1003
|
+
return { events: page, cursor: page.at(-1)?.seq ?? since, hasMore: more }
|
|
1004
|
+
}
|
|
1005
|
+
async getRepositoryTail(
|
|
1006
|
+
repo: string,
|
|
1007
|
+
since = 0,
|
|
1008
|
+
opts?: EventReadOptions,
|
|
1009
|
+
): Promise<RepositoryEvent[]> {
|
|
1010
|
+
validateEventCursor(since)
|
|
1011
|
+
if (opts?.through !== undefined) validateEventCursor(opts.through)
|
|
1012
|
+
return readEventsWithWait({
|
|
1013
|
+
read: async () => {
|
|
1014
|
+
const head = await this.getRepoHighWater(repo)
|
|
1015
|
+
assertResumeWindow({ kind: 'repo', repo }, since, head)
|
|
1016
|
+
return structuredClone(
|
|
1017
|
+
this.repoState(repo).events.slice(since, Math.min(head, opts?.through ?? head)),
|
|
1018
|
+
)
|
|
1019
|
+
},
|
|
1020
|
+
waitSeconds: opts?.waitSeconds,
|
|
1021
|
+
signal: opts?.signal,
|
|
1022
|
+
pollMs: EVENT_WAIT_POLL_MS,
|
|
1023
|
+
})
|
|
1024
|
+
}
|
|
1025
|
+
async getHarvestHistoryPage(
|
|
1026
|
+
repo: string,
|
|
1027
|
+
before = Number.MAX_SAFE_INTEGER,
|
|
1028
|
+
): Promise<EventPage<RepositoryEvent>> {
|
|
1029
|
+
validateEventCursor(before)
|
|
1030
|
+
const events = this.repoState(repo)
|
|
1031
|
+
.events.filter((event) => event.seq < before && event.type.startsWith('harvest.'))
|
|
1032
|
+
.reverse()
|
|
1033
|
+
const page = structuredClone(events.slice(0, BUILD_EVENT_PAGE_SIZE))
|
|
1034
|
+
return {
|
|
1035
|
+
events: page,
|
|
1036
|
+
cursor: page.at(-1)?.seq ?? before,
|
|
1037
|
+
hasMore: events.length > BUILD_EVENT_PAGE_SIZE,
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
|
|
882
1041
|
/** The scope's log tail, or `undefined` for a scope that does not exist. */
|
|
883
1042
|
private snapshotTail(scope: SnapshotScope): number | undefined {
|
|
884
1043
|
const events =
|
|
@@ -891,7 +1050,85 @@ export class MemoryBuildStore implements BuildStore {
|
|
|
891
1050
|
async getReducerValue(scope: SnapshotScope, request: ReducerValueRequest): Promise<ReducerValue> {
|
|
892
1051
|
// Reject invalid requests before touching storage.
|
|
893
1052
|
resolveReducerValue(scope, request)
|
|
1053
|
+
if (request.reducer === 'buildStreamSeed' || request.reducer === 'repositoryAttention')
|
|
1054
|
+
return this.withSeedLock(scope, () => this.readSeedValue(scope, request))
|
|
1055
|
+
return this.readValue(scope, request)
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
private async readSeedValue(
|
|
1059
|
+
scope: SnapshotScope,
|
|
1060
|
+
request: ReducerValueRequest,
|
|
1061
|
+
): Promise<ReducerValue> {
|
|
1062
|
+
const { key } = resolveReducerValue(scope, request)
|
|
1063
|
+
const scopeKey = snapshotScopeKey(scope)
|
|
1064
|
+
if (this.snapshotTail(scope) === undefined) throw new Error('unknown reducer scope')
|
|
1065
|
+
const generations = this.seedGenerations.get(scopeKey) ?? {
|
|
1066
|
+
live: [],
|
|
1067
|
+
retired: new Set<string>(),
|
|
1068
|
+
}
|
|
1069
|
+
if (generations.retired.has(key)) throw new RetiredSeedGenerationError()
|
|
1070
|
+
if (generations.live.includes(key) && !this.snapshots.has(`${scopeKey}\u0000${key}`))
|
|
1071
|
+
throw new CheckpointIntegrityError('missing canonical stream seed')
|
|
1072
|
+
if (!generations.live.includes(key)) {
|
|
1073
|
+
generations.live.push(key)
|
|
1074
|
+
if (generations.live.length > 2) {
|
|
1075
|
+
const retired = generations.live.shift()!
|
|
1076
|
+
generations.retired.add(retired)
|
|
1077
|
+
this.seedCheckpoints.delete(`${scopeKey}\u0000${retired}`)
|
|
1078
|
+
this.snapshots.delete(`${scopeKey}\u0000${retired}`)
|
|
1079
|
+
}
|
|
1080
|
+
this.seedGenerations.set(scopeKey, generations)
|
|
1081
|
+
}
|
|
1082
|
+
return this.readValue(scope, request, {
|
|
1083
|
+
latest: async (identity, through) => {
|
|
1084
|
+
const checkpoints = this.seedCheckpoints.get(`${scopeKey}\u0000${identity}`)
|
|
1085
|
+
const cursor = Math.max(-1, ...[...(checkpoints?.keys() ?? [])].filter((c) => c <= through))
|
|
1086
|
+
return checkpoints?.get(cursor) ?? null
|
|
1087
|
+
},
|
|
1088
|
+
coverage: async (identity, cursor) =>
|
|
1089
|
+
this.seedCheckpoints.get(`${scopeKey}\u0000${identity}`)?.has(cursor) ?? false,
|
|
1090
|
+
publish: async (identity, snapshot, checkpoints) => {
|
|
1091
|
+
const cacheKey = `${scopeKey}\u0000${identity}`
|
|
1092
|
+
const next = new Map(this.seedCheckpoints.get(cacheKey))
|
|
1093
|
+
for (const checkpoint of checkpoints)
|
|
1094
|
+
next.set(checkpoint.cursor, structuredClone(checkpoint))
|
|
1095
|
+
for (const cursor of next.keys())
|
|
1096
|
+
if (cursor < snapshot.cursor - REPOSITORY_TAIL_WINDOW) next.delete(cursor)
|
|
1097
|
+
this.seedCheckpoints.set(cacheKey, next)
|
|
1098
|
+
this.snapshots.set(cacheKey, structuredClone(snapshot))
|
|
1099
|
+
},
|
|
1100
|
+
})
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
async getStreamSeed(scope: SnapshotScope, cursor: number): Promise<ReducerValue> {
|
|
1104
|
+
validateEventCursor(cursor)
|
|
1105
|
+
return this.withSeedLock(scope, async () => {
|
|
1106
|
+
const head = this.snapshotTail(scope)
|
|
1107
|
+
if (head === undefined) throw new Error('unknown reducer scope')
|
|
1108
|
+
assertResumeWindow(scope, cursor, head)
|
|
1109
|
+
const request = {
|
|
1110
|
+
reducer: scope.kind === 'build' ? 'buildStreamSeed' : 'repositoryAttention',
|
|
1111
|
+
}
|
|
1112
|
+
const current = await this.readSeedValue(scope, request)
|
|
1113
|
+
const { key, entry, reducer } = resolveReducerValue(scope, request)
|
|
1114
|
+
const checkpoint = this.seedCheckpoints
|
|
1115
|
+
.get(`${snapshotScopeKey(scope)}\u0000${key}`)
|
|
1116
|
+
?.get(cursor)
|
|
1117
|
+
if (!checkpoint) throw new CheckpointIntegrityError('missing stream-seed checkpoint')
|
|
1118
|
+
const state = checkpoint.state as { acc: unknown }
|
|
1119
|
+
if (!entry.validate(state.acc))
|
|
1120
|
+
throw new CheckpointIntegrityError('corrupt stream-seed checkpoint')
|
|
1121
|
+
return { ...current, cursor, value: entry.value(reducer.finish(state.acc)) }
|
|
1122
|
+
})
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
private async readValue(
|
|
1126
|
+
scope: SnapshotScope,
|
|
1127
|
+
request: ReducerValueRequest,
|
|
1128
|
+
checkpoints?: import('./reducer-values').ReducerValueStorage['checkpoints'],
|
|
1129
|
+
): Promise<ReducerValue> {
|
|
894
1130
|
return readReducerValue(scope, request, {
|
|
1131
|
+
...(checkpoints ? { checkpoints } : {}),
|
|
895
1132
|
highWater: async () => {
|
|
896
1133
|
const tail = this.snapshotTail(scope)
|
|
897
1134
|
if (tail === undefined) throw new Error('unknown reducer scope')
|