@hearsay-tools/squawk 0.18.0-dev.7825 → 0.18.0-dev.7830
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/dist/activity/read-model.d.ts +3 -1
- package/dist/activity/read-model.js +17 -15
- package/dist/activity/read-model.js.map +1 -1
- package/dist/activity/requests.d.ts +3 -1
- package/dist/activity/requests.js +1 -1
- package/dist/activity/requests.js.map +1 -1
- package/dist/automations/service.js +1 -1
- package/dist/automations/service.js.map +1 -1
- package/dist/automations/task-template.js +3 -3
- package/dist/automations/task-template.js.map +1 -1
- package/dist/ci-wait/controller.d.ts +46 -46
- package/dist/delegation/conversations.d.ts +6 -2
- package/dist/delegation/conversations.js +1 -1
- package/dist/delegation/conversations.js.map +1 -1
- package/dist/delegation/family-end.d.ts +4 -2
- package/dist/delegation/family-end.js +1 -1
- package/dist/delegation/family-end.js.map +1 -1
- package/dist/delegation/routes.d.ts +75 -75
- package/dist/delegation/scratch-cleanup.js +2 -2
- package/dist/delegation/scratch-cleanup.js.map +1 -1
- package/dist/delegation/task-discovery.js +1 -1
- package/dist/delegation/task-discovery.js.map +1 -1
- package/dist/delegation/transport.d.ts +14 -14
- package/dist/delegation/worker-cleanup.js +4 -4
- package/dist/delegation/worker-cleanup.js.map +1 -1
- package/dist/project-commands.js +1 -1
- package/dist/project-commands.js.map +1 -1
- package/dist/project-migration/live.js +1 -1
- package/dist/project-migration/live.js.map +1 -1
- package/dist/runs/branch-owners.d.ts +2 -2
- package/dist/runs/cold-reads.d.ts +1 -1
- package/dist/runs/cold-reads.js +1 -1
- package/dist/runs/live-tasks.js +1 -1
- package/dist/runs/live-tasks.js.map +1 -1
- package/dist/runs/ports.d.ts +50 -28
- package/dist/runs/retention.d.ts +4 -3
- package/dist/runs/retention.js +2 -2
- package/dist/runs/retention.js.map +1 -1
- package/dist/runs/run-cold-reads.d.ts +98 -0
- package/dist/runs/run-cold-reads.js +194 -0
- package/dist/runs/run-cold-reads.js.map +1 -0
- package/dist/runs/run-queries.d.ts +190 -0
- package/dist/runs/run-queries.js +480 -0
- package/dist/runs/run-queries.js.map +1 -0
- package/dist/runs/run-repo-scope.d.ts +38 -0
- package/dist/runs/run-repo-scope.js +78 -0
- package/dist/runs/run-repo-scope.js.map +1 -0
- package/dist/runs/store.d.ts +9 -222
- package/dist/runs/store.js +82 -738
- package/dist/runs/store.js.map +1 -1
- package/dist/serve-boot.d.ts +3 -1
- package/dist/serve-boot.js +2 -2
- package/dist/serve-boot.js.map +1 -1
- package/dist/server/models-routes.d.ts +13 -13
- package/dist/server/project-context.js +3 -3
- package/dist/server/project-context.js.map +1 -1
- package/dist/server/repo-branches.d.ts +5 -3
- package/dist/server/repo-branches.js +1 -1
- package/dist/server/repo-branches.js.map +1 -1
- package/dist/server/reports-api.d.ts +2 -2
- package/dist/server/run-checkout.d.ts +3 -1
- package/dist/server/run-checkout.js +2 -2
- package/dist/server/run-checkout.js.map +1 -1
- package/dist/server/run-relationships.d.ts +3 -1
- package/dist/server/run-relationships.js +1 -1
- package/dist/server/run-relationships.js.map +1 -1
- package/dist/server/server.js +12 -12
- package/dist/server/server.js.map +1 -1
- package/dist/server/workspace-live-routes.d.ts +6 -6
- package/dist/workflows/run/auto-resume.js +2 -2
- package/dist/workflows/run/auto-resume.js.map +1 -1
- package/dist/workflows/run/completion-gate.js +1 -1
- package/dist/workflows/run/completion-gate.js.map +1 -1
- package/dist/workflows/run/family-reconciler.js +8 -8
- package/dist/workflows/run/family-reconciler.js.map +1 -1
- package/dist/workflows/run/maintenance-claims.js +2 -2
- package/dist/workflows/run/maintenance-claims.js.map +1 -1
- package/dist/workflows/run/run-cancel.js +1 -1
- package/dist/workflows/run/run-cancel.js.map +1 -1
- package/dist/workflows/run/run-line.js +2 -2
- package/dist/workflows/run/run-line.js.map +1 -1
- package/dist/workflows/run/run-recovery.js +6 -6
- package/dist/workflows/run/run-recovery.js.map +1 -1
- package/dist/workflows/run/run-start.js +1 -1
- package/dist/workflows/run/run-start.js.map +1 -1
- package/dist/workflows/run/wake-admission.js +1 -1
- package/dist/workflows/run/wake-admission.js.map +1 -1
- package/dist/workflows/run/worker-questions.js +3 -3
- package/dist/workflows/run/worker-questions.js.map +1 -1
- package/dist/workflows/run.js +1 -1
- package/dist/workflows/run.js.map +1 -1
- package/package.json +4 -4
- package/scripts/benchmark-automation-lookups.ts +2 -2
- package/scripts/benchmark-conversation-lookup.ts +2 -2
- package/scripts/benchmark-family-reconcile.ts +1 -1
- package/scripts/benchmark-worktree-retention.ts +1 -1
- package/scripts/seed-archive-profile.ts +2 -2
package/dist/runs/store.js
CHANGED
|
@@ -3,8 +3,6 @@ import { addRunSource, fallbackRunSource } from './project-run-sources.js';
|
|
|
3
3
|
import { registeredWaitRecord, registeredReceiptRecord } from './wait-registration-write.js';
|
|
4
4
|
import { stageDurableUpdate, stageDebouncedUpdate, stageConversation, stageStepUpdate, stepTotals, redactPatch } from './record-write-rules.js';
|
|
5
5
|
import { RunLifecycle, snapshotRun } from './lifecycle.js';
|
|
6
|
-
import { listPeerTaskSummaries, parseStoredSummary } from './run-summary-page.js';
|
|
7
|
-
export { parseStoredSummary } from './run-summary-page.js';
|
|
8
6
|
import { workerExecutionsFactory } from './worker-executions-registry.js';
|
|
9
7
|
import { randomUUID } from 'node:crypto';
|
|
10
8
|
import { isDeepStrictEqual } from 'node:util';
|
|
@@ -13,13 +11,11 @@ import { join } from 'node:path';
|
|
|
13
11
|
import { z } from 'zod';
|
|
14
12
|
import { delegationStateSchema, workerDestroyRetrySchema, } from '../contract/index.js';
|
|
15
13
|
import { agentInputSchema, inboxClaimSchema } from '../contract/index.js';
|
|
16
|
-
import { stepStateSchema as contractStepStateSchema,
|
|
14
|
+
import { stepStateSchema as contractStepStateSchema, runRecordSchema as contractRunRecordSchema, runPullRequestSchema, } from '../contract/index.js';
|
|
17
15
|
import { queuedMessageSchema } from '../contract/index.js';
|
|
18
16
|
import { previewServerSchema } from '../contract/index.js';
|
|
19
17
|
import { waitSchema } from '../contract/index.js';
|
|
20
18
|
import { isUsageLimitHold } from '../contract/index.js';
|
|
21
|
-
import { REFERENCED_RUN_RULE } from '../contract/index.js';
|
|
22
|
-
import { matchesRunSearch, sqlPrefilterTokens } from '../contract/index.js';
|
|
23
19
|
import { storedDelegationStateSchema } from './delegation-state.js';
|
|
24
20
|
import { LiveItemSnapshots } from './live-item-snapshots.js';
|
|
25
21
|
import { RunTranscriptFacts } from './transcript-facts-topic.js';
|
|
@@ -27,22 +23,17 @@ import { restoreHistory } from './history-file.js';
|
|
|
27
23
|
// Type-only module (zod + nothing else), so this cannot cycle back into the store.
|
|
28
24
|
import { workflowDefSchema } from '../workflows/types.js';
|
|
29
25
|
import { applyOutcomes } from './outcome-commit.js';
|
|
30
|
-
import {
|
|
26
|
+
import { trackReferencedIssues, trackReferencedPrs } from './run-references.js';
|
|
31
27
|
import { applyExit, doneCommits, exitDurability, RUN_EXITS } from './run-exits.js';
|
|
32
28
|
import { assertConversationIdFree } from './conversation-identity.js';
|
|
33
29
|
export { ConversationIdInUseError } from './conversation-identity.js';
|
|
34
30
|
import { LEGACY_RUNNER_IDS } from '../core/runner-ids.js';
|
|
35
|
-
import {
|
|
36
|
-
import { listBranchOwners } from './branch-owners.js';
|
|
37
|
-
import { guardedJsonSql } from './guarded-json-sql.js';
|
|
38
|
-
import { reconcileLoadedRun } from './loaded-run-exits.js';
|
|
31
|
+
import { RUNS_DB_FILE, RUNS_IMPORT_COMPLETE_KEY, RunDatabase, RunDatabaseBusyError, } from './run-database.js';
|
|
39
32
|
import { hasLegacyWaitFields, UNREADABLE_WAIT } from './legacy-waits.js';
|
|
40
33
|
export { ciWaitEntry, holdsWorkerWait, upgradeLegacyWaits, workerWaitEntry } from './legacy-waits.js';
|
|
41
34
|
import { ExitEvents } from './exit-events.js';
|
|
42
|
-
import { familyRootOf, isLiveRecord, materializedWorktreePath, newestFirst } from './run-row.js';
|
|
43
35
|
import { RunRetentionTopic } from './run-retention.js';
|
|
44
36
|
import { collectRawExtras } from './raw-record.js';
|
|
45
|
-
import { ColdReadCache, storedRow } from './cold-reads.js';
|
|
46
37
|
import { LEGACY_INDEX_FILE, LegacyWriterError } from './legacy-index.js';
|
|
47
38
|
import { toRunStoreOpenError } from './store-open-error.js';
|
|
48
39
|
import { importLegacyIndex, OPEN_RETRY_DELAYS_MS, sleepSync } from './store-open.js';
|
|
@@ -55,7 +46,10 @@ import { RunRedaction } from './run-redaction.js';
|
|
|
55
46
|
import { RunArchiveTopic } from './run-archive.js';
|
|
56
47
|
import { RunReadFlagsTopic } from './run-read-flags.js';
|
|
57
48
|
import { replaceRecord, RunPersistenceTopic } from './run-persistence.js';
|
|
58
|
-
import { closeClaimSession,
|
|
49
|
+
import { closeClaimSession, openClaimSession } from './run-claims.js';
|
|
50
|
+
import { RunColdReadsTopic } from './run-cold-reads.js';
|
|
51
|
+
import { RunQueriesTopic, sortNewestFirst } from './run-queries.js';
|
|
52
|
+
import { RunRepoScopeTopic } from './run-repo-scope.js';
|
|
59
53
|
/**
|
|
60
54
|
* A runner id as it may appear in a PERSISTED record (#547), spec 2026-11-18-runner-definitions.
|
|
61
55
|
*
|
|
@@ -181,7 +175,6 @@ export { LEGACY_INDEX_BACKUP_FILE, LEGACY_INDEX_FILE } from './legacy-index.js';
|
|
|
181
175
|
* and `GET /workspace/runs-index`. Unarchived runs are never cut; older archived runs page in
|
|
182
176
|
* through `GET /run-summaries/archived` and the workspace search. */
|
|
183
177
|
export const ARCHIVED_WINDOW = 200;
|
|
184
|
-
export { branchOwnerOf } from './branch-owners.js';
|
|
185
178
|
/**
|
|
186
179
|
* A worker's destroy moving between two unfinished phases, and nothing else changing: nothing the
|
|
187
180
|
* family reconcile reads changed, so it may skip that pass (#880). The first request and the
|
|
@@ -194,38 +187,6 @@ function isDestroyProgress(before, after) {
|
|
|
194
187
|
return false;
|
|
195
188
|
return isDeepStrictEqual({ ...before, destroy: undefined }, { ...after, destroy: undefined });
|
|
196
189
|
}
|
|
197
|
-
/** An archived page's cursor: the last row's order, opaque to clients. A held run with no row yet
|
|
198
|
-
* lists after every row (`seq` is infinite), which JSON cannot carry, so it travels as null. */
|
|
199
|
-
function encodeArchivedCursor(key) {
|
|
200
|
-
return Buffer.from(JSON.stringify({ c: key.createdAt, s: Number.isFinite(key.seq) ? key.seq : null })).toString('base64url');
|
|
201
|
-
}
|
|
202
|
-
function decodeArchivedCursor(cursor) {
|
|
203
|
-
try {
|
|
204
|
-
const parsed = JSON.parse(Buffer.from(cursor, 'base64url').toString('utf8'));
|
|
205
|
-
if (typeof parsed !== 'object' || parsed === null)
|
|
206
|
-
return undefined;
|
|
207
|
-
const { c, s } = parsed;
|
|
208
|
-
if (typeof c !== 'string' || !(s === null || (typeof s === 'number' && Number.isInteger(s))))
|
|
209
|
-
return undefined;
|
|
210
|
-
return { createdAt: c, seq: s ?? Number.POSITIVE_INFINITY };
|
|
211
|
-
}
|
|
212
|
-
catch {
|
|
213
|
-
return undefined;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
/** The items newest first, each by the order beside it. Stable: ties keep their input order. */
|
|
217
|
-
function sortNewestFirst(entries) {
|
|
218
|
-
return [...entries].sort(([, a], [, b]) => newestFirst(a, b)).map(([item]) => item);
|
|
219
|
-
}
|
|
220
|
-
/** Tests freeze the copies of cold runs, so an in-place write to one throws instead of vanishing. */
|
|
221
|
-
function deepFreeze(value) {
|
|
222
|
-
if (value && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
223
|
-
Object.freeze(value);
|
|
224
|
-
for (const child of Object.values(value))
|
|
225
|
-
deepFreeze(child);
|
|
226
|
-
}
|
|
227
|
-
return value;
|
|
228
|
-
}
|
|
229
190
|
/**
|
|
230
191
|
* Run rows in runs.db plus append-only NDJSON events and an in-process event bus. Hold live,
|
|
231
192
|
* pinned, related and unsettled records only. Optimistic writes debounce; durable commits
|
|
@@ -256,10 +217,6 @@ export class RunStore {
|
|
|
256
217
|
/** Deleted runs whose history files (events, handoff, images, artifacts) go once the delete of
|
|
257
218
|
* their row commits: until then a conflict can undo the delete and bring the row back. */
|
|
258
219
|
historyOwed = new Set();
|
|
259
|
-
/** The repository this project IS (#945), armed after `open()` by `setRepoHandle`. Undefined
|
|
260
|
-
* until it arrives and `null` when it cannot be known — both mean "unscoped", which is
|
|
261
|
-
* exactly the pre-#945 behavior. */
|
|
262
|
-
repoHandle;
|
|
263
220
|
/** `open()`'s `keepLive`, which a run read later from `runs.db` is reconciled with too. */
|
|
264
221
|
keepLive = false;
|
|
265
222
|
/** This store's identity in `run_claims`; undefined without a database. */
|
|
@@ -274,16 +231,8 @@ export class RunStore {
|
|
|
274
231
|
* created the row and has not written it yet. `extras` is what that `data` holds beyond the
|
|
275
232
|
* schema, which the next write of the row puts back (raw-record.ts). */
|
|
276
233
|
base = new Map();
|
|
277
|
-
/** Rows read cold in the current synchronous turn, so a commit staged from one is fenced against
|
|
278
|
-
* the version it was computed from. `owned`: its family was already this store's when read.
|
|
279
|
-
* Cleared at the next microtask: no commit spans a turn. */
|
|
280
|
-
coldBase = new Map();
|
|
281
|
-
coldReads = new ColdReadCache();
|
|
282
234
|
/** Rows a conflicting write excluded: this store refuses to write them until it is reopened. */
|
|
283
235
|
quarantined = new Set();
|
|
284
|
-
/** Rows whose `data` this store tried to decode and could not: left in the database untouched,
|
|
285
|
-
* and left out of the list rows from then on, as the cold reader leaves them out (run-index.ts). */
|
|
286
|
-
unreadable = new Set();
|
|
287
236
|
/** The archive flag, archive on merge, completion actions and the transcript compressor (hearsay-tools/squawk#4041). */
|
|
288
237
|
archive;
|
|
289
238
|
/** The pinned and read flags (hearsay-tools/squawk#4041). */
|
|
@@ -304,12 +253,18 @@ export class RunStore {
|
|
|
304
253
|
runPins;
|
|
305
254
|
/** Flush, persist, the fence, conflicts and the save debounce (hearsay-tools/squawk#4043). */
|
|
306
255
|
persistence;
|
|
256
|
+
/** The held set's cold reads, holds and eviction (hearsay-tools/squawk#4044). */
|
|
257
|
+
rows;
|
|
258
|
+
/** The list and find queries (hearsay-tools/squawk#4044). */
|
|
259
|
+
queries;
|
|
260
|
+
/** The repository this project is, and the repair of the runs it rescopes (hearsay-tools/squawk#4044). */
|
|
261
|
+
repoScope;
|
|
307
262
|
executions;
|
|
308
263
|
/** A worker's execution, process, identity and scope records (hearsay-tools/squawk#4039), built on
|
|
309
264
|
* first use by the factory this process registered; throws when none is registered. */
|
|
310
265
|
get workerExecutions() {
|
|
311
266
|
return this.executions ??= workerExecutionsFactory()(this.dataDir, {
|
|
312
|
-
peek: (id) => this.peek(id), getRun: (id) => this.getRun(id), writeDir: (id) => this.claims.writeDir(id),
|
|
267
|
+
peek: (id) => this.rows.peek(id), getRun: (id) => this.getRun(id), writeDir: (id) => this.claims.writeDir(id),
|
|
313
268
|
sourceDataDir: (id) => this.claims.sourceDataDir(id), commitIndex: (input) => this.persistence.commitIndex(input),
|
|
314
269
|
published: this.published, workerResults: this.workerResults,
|
|
315
270
|
});
|
|
@@ -321,40 +276,45 @@ export class RunStore {
|
|
|
321
276
|
}
|
|
322
277
|
constructor(dataDir, persist) {
|
|
323
278
|
this.dataDir = dataDir;
|
|
279
|
+
this.rows = new RunColdReadsTopic({
|
|
280
|
+
db: () => this.db, keepLive: () => this.keepLive, repoHandle: () => this.repoScope.repoHandle, held: this.held, pins: this.pins,
|
|
281
|
+
committing: this.committing, dirty: this.dirty, deleted: this.deleted, quarantined: this.quarantined, claimed: this.claimed,
|
|
282
|
+
pendingClaims: this.pendingClaims, base: this.base, claims: () => this.claims, persistence: () => this.persistence, decodeRunRow,
|
|
283
|
+
});
|
|
324
284
|
this.redaction = new RunRedaction();
|
|
325
285
|
this.claims = new RunClaimsTopic(dataDir, {
|
|
326
286
|
db: () => this.db, owner: () => this.owner, keepLive: () => this.keepLive, held: this.held, base: this.base, dirty: this.dirty,
|
|
327
|
-
deleted: this.deleted, quarantined: this.quarantined, unreadable: this.unreadable, claimed: this.claimed, pendingClaims: this.pendingClaims,
|
|
328
|
-
exitEvents: this.exitEvents, coldReads: this.coldReads, decodeRunRow, anchoredFamilies: () => this.anchoredFamilies(), scheduleSave: () => this.persistence.scheduleSave(),
|
|
287
|
+
deleted: this.deleted, quarantined: this.quarantined, unreadable: this.rows.unreadable, claimed: this.claimed, pendingClaims: this.pendingClaims,
|
|
288
|
+
exitEvents: this.exitEvents, coldReads: this.rows.coldReads, decodeRunRow, anchoredFamilies: () => this.rows.anchoredFamilies(), scheduleSave: () => this.persistence.scheduleSave(),
|
|
329
289
|
});
|
|
330
|
-
this.runPins = new RunPinsTopic({ pins: this.pins, published: this.published, record: (id) => this.record(id), peek: (id) => this.peek(id), sweepIfSettled: (ids) => this.persistence.sweepIfSettled(ids) });
|
|
290
|
+
this.runPins = new RunPinsTopic({ pins: this.pins, published: this.published, record: (id) => this.rows.record(id), peek: (id) => this.rows.peek(id), sweepIfSettled: (ids) => this.persistence.sweepIfSettled(ids) });
|
|
331
291
|
this.transcriptFacts = new RunTranscriptFacts(dataDir, {
|
|
332
292
|
isFallbackRun: (id) => this.claims.isFallbackRun(id),
|
|
333
293
|
sourceDataDir: (id) => this.claims.sourceDataDir(id),
|
|
334
294
|
isOpen: () => this.db !== null,
|
|
335
295
|
held: this.held,
|
|
336
|
-
listFamilyRunIds: (rootId) => this.listFamilyRunIds(rootId),
|
|
337
|
-
listConversationRootIds: () => this.listConversationRootIds(),
|
|
338
|
-
listRunIds: () => this.listRunIds(),
|
|
296
|
+
listFamilyRunIds: (rootId) => this.queries.listFamilyRunIds(rootId),
|
|
297
|
+
listConversationRootIds: () => this.queries.listConversationRootIds(),
|
|
298
|
+
listRunIds: () => this.queries.listRunIds(),
|
|
339
299
|
});
|
|
340
300
|
this.workerResults = new WorkerResultsTopic(dataDir, {
|
|
341
|
-
peek: (id) => this.peek(id), hasRun: (id) => this.hasRun(id), hasFallbackRun: (id) => this.claims.hasFallbackRun(id),
|
|
301
|
+
peek: (id) => this.rows.peek(id), hasRun: (id) => this.rows.hasRun(id), hasFallbackRun: (id) => this.claims.hasFallbackRun(id),
|
|
342
302
|
writeDir: (id) => this.claims.writeDir(id), sourceDataDir: (id) => this.claims.sourceDataDir(id),
|
|
343
303
|
redactResult: (result) => this.redaction.redact({ type: 'worker-result', seq: 0, ts: result.observedAt, result }).result,
|
|
344
304
|
redactText: (text) => this.redaction.redactText(text), commitDelegation: (patches) => this.commitDelegation(patches),
|
|
345
305
|
});
|
|
346
306
|
this.eventLog = new RunEventLogTopic(dataDir, {
|
|
347
307
|
seqs: this.seqs, published: this.published, liveItems: this.liveItems, transcriptFacts: this.transcriptFacts, redaction: this.redaction,
|
|
348
|
-
record: (id) => this.record(id), missingRunError: (runId) => this.claims.missingRunError(runId), sourceDataDir: (id) => this.claims.sourceDataDir(id),
|
|
349
|
-
touch: (run, before) => this.persistence.touch(run, before), change: (id, apply) => this.change(id, apply), repoHandle: () => this.repoHandle,
|
|
308
|
+
record: (id) => this.rows.record(id), missingRunError: (runId) => this.claims.missingRunError(runId), sourceDataDir: (id) => this.claims.sourceDataDir(id),
|
|
309
|
+
touch: (run, before) => this.persistence.touch(run, before), change: (id, apply) => this.change(id, apply), repoHandle: () => this.repoScope.repoHandle,
|
|
350
310
|
maybeEnqueueHistoryCompress: (run) => this.archive.maybeEnqueueHistoryCompress(run),
|
|
351
311
|
});
|
|
352
312
|
this.retention = new RunRetentionTopic(dataDir, {
|
|
353
313
|
db: () => this.db, held: this.held, deleted: this.deleted, quarantined: this.quarantined,
|
|
354
314
|
historyOwed: this.historyOwed, seqs: this.seqs, published: this.published, transcriptFacts: this.transcriptFacts,
|
|
355
|
-
peek: (id) => this.peek(id), hasRun: (id) => this.hasRun(id), record: (id) => this.record(id),
|
|
356
|
-
listWorkersOf: (parentId) => this.listWorkersOf(parentId), isFallbackRun: (id) => this.claims.isFallbackRun(id),
|
|
357
|
-
hasFallbackRun: (id) => this.claims.hasFallbackRun(id), listOrder: (run) => this.listOrder(run),
|
|
315
|
+
peek: (id) => this.rows.peek(id), hasRun: (id) => this.rows.hasRun(id), record: (id) => this.rows.record(id),
|
|
316
|
+
listWorkersOf: (parentId) => this.queries.listWorkersOf(parentId), isFallbackRun: (id) => this.claims.isFallbackRun(id),
|
|
317
|
+
hasFallbackRun: (id) => this.claims.hasFallbackRun(id), listOrder: (run) => this.queries.listOrder(run),
|
|
358
318
|
claimFamilies: (families) => this.claims.claimFamilies(families), markDeleted: (id, family) => this.persistence.markDeleted(id, family),
|
|
359
319
|
scheduleSave: () => this.persistence.scheduleSave(), commitIndex: (input) => this.persistence.commitIndex(input),
|
|
360
320
|
commitDelegation: (patches) => this.commitDelegation(patches), applyArchived: (run, archived) => this.archive.applyArchived(run, archived),
|
|
@@ -367,27 +327,30 @@ export class RunStore {
|
|
|
367
327
|
this.archive = new RunArchiveTopic(dataDir, {
|
|
368
328
|
db: () => this.db, held: this.held, deleted: this.deleted, quarantined: this.quarantined, pins: this.pins,
|
|
369
329
|
claimed: this.claimed, base: this.base, owner: () => this.owner, transcriptFacts: this.transcriptFacts,
|
|
370
|
-
decodeRunRow, sortNewestFirst, peek: (id) => this.peek(id), record: (id) => this.record(id),
|
|
371
|
-
listWorkersOf: (parentId) => this.listWorkersOf(parentId), familyOf: (id) => this.persistence.familyOf(id),
|
|
330
|
+
decodeRunRow, sortNewestFirst, peek: (id) => this.rows.peek(id), record: (id) => this.rows.record(id),
|
|
331
|
+
listWorkersOf: (parentId) => this.queries.listWorkersOf(parentId), familyOf: (id) => this.persistence.familyOf(id),
|
|
372
332
|
claimFamilies: (families, opts) => this.claims.claimFamilies(families, opts), writeRefusal: (id) => this.claims.writeRefusal(id),
|
|
373
|
-
assertWritable: (staged) => this.persistence.assertWritable(staged), decodeCold: (row) => this.decodeCold(row),
|
|
374
|
-
listOrder: (run) => this.listOrder(run), commitIndex: (input, source, write) => this.persistence.commitIndex(input, source, write),
|
|
333
|
+
assertWritable: (staged) => this.persistence.assertWritable(staged), decodeCold: (row) => this.rows.decodeCold(row),
|
|
334
|
+
listOrder: (run) => this.queries.listOrder(run), commitIndex: (input, source, write) => this.persistence.commitIndex(input, source, write),
|
|
375
335
|
touch: (run, before, exit) => this.persistence.touch(run, before, exit),
|
|
376
336
|
readWorkerExecution: (id) => this.workerExecutions.readWorkerExecution(id),
|
|
377
337
|
});
|
|
378
338
|
this.readFlags = new RunReadFlagsTopic({
|
|
379
|
-
db: () => this.db, held: this.held, deleted: this.deleted, record: (id) => this.record(id),
|
|
339
|
+
db: () => this.db, held: this.held, deleted: this.deleted, record: (id) => this.rows.record(id),
|
|
380
340
|
claimFamilies: (families) => this.claims.claimFamilies(families), touch: (run, before) => this.persistence.touch(run, before),
|
|
381
341
|
change: (id, apply) => this.change(id, apply),
|
|
382
342
|
});
|
|
383
343
|
this.persistence = new RunPersistenceTopic(dataDir, persist, {
|
|
384
|
-
db: () => this.db, owner: () => this.owner, openFailure: () => this.openFailure, repoHandle: () => this.repoHandle,
|
|
344
|
+
db: () => this.db, owner: () => this.owner, openFailure: () => this.openFailure, repoHandle: () => this.repoScope.repoHandle,
|
|
385
345
|
held: this.held, committing: this.committing, dirty: this.dirty, deleted: this.deleted, historyOwed: this.historyOwed,
|
|
386
|
-
quarantined: this.quarantined, claimed: this.claimed, pendingClaims: this.pendingClaims, base: this.base, coldBase: this.coldBase,
|
|
387
|
-
coldReads: this.coldReads, liveItems: this.liveItems, published: this.published, archive: this.archive, claims: this.claims,
|
|
388
|
-
retention: this.retention, decodeRunRow, peek: (id) => this.peek(id), isAnchor: (id, run) => this.isAnchor(id, run),
|
|
389
|
-
evictSettled: () => this.evictSettled(),
|
|
346
|
+
quarantined: this.quarantined, claimed: this.claimed, pendingClaims: this.pendingClaims, base: this.base, coldBase: this.rows.coldBase,
|
|
347
|
+
coldReads: this.rows.coldReads, liveItems: this.liveItems, published: this.published, archive: this.archive, claims: this.claims,
|
|
348
|
+
retention: this.retention, decodeRunRow, peek: (id) => this.rows.peek(id), isAnchor: (id, run) => this.rows.isAnchor(id, run),
|
|
349
|
+
evictSettled: () => this.rows.evictSettled(),
|
|
390
350
|
});
|
|
351
|
+
this.queries = new RunQueriesTopic({ db: () => this.db, held: this.held, pins: this.pins, deleted: this.deleted, unreadable: this.rows.unreadable,
|
|
352
|
+
base: this.base, coldReads: this.rows.coldReads, rows: this.rows, getRun: (id) => this.getRun(id) });
|
|
353
|
+
this.repoScope = new RunRepoScopeTopic({ db: () => this.db, held: this.held, deleted: this.deleted, claims: this.claims, persistence: this.persistence, rows: this.rows });
|
|
391
354
|
}
|
|
392
355
|
/**
|
|
393
356
|
* Open the project's run store. See `reconcileLoadedRun` for what `keepLive` (#367) decides
|
|
@@ -480,396 +443,6 @@ export class RunStore {
|
|
|
480
443
|
/** Why this store has no database, when it is `RunStore.unavailable`'s: its runs are unknown,
|
|
481
444
|
* not absent. */
|
|
482
445
|
get unavailable() { return this.openFailure; }
|
|
483
|
-
/**
|
|
484
|
-
* Whether a held run anchors itself (and its family) in memory. Anything else held is a family
|
|
485
|
-
* member or a settled write, and leaves at the next sweep (`evictSettled`).
|
|
486
|
-
*
|
|
487
|
-
* The held set is: every run that is live (`isLiveRecord`), pinned by a RunManager (`pin`),
|
|
488
|
-
* dirty, or inside a durable commit; plus the whole delegation family (root and direct
|
|
489
|
-
* workers) of any such run. Families are held because the RunManager reads a live run's family
|
|
490
|
-
* on every run event — `GET /runs/:id/relationships`, the global `reconcileWorkerWaits` pass
|
|
491
|
-
* (about twenty call sites), completion blockers and conversations — and decoding a cold
|
|
492
|
-
* record costs about 200 µs (JSON.parse plus the schema). Measured on a copy of a real
|
|
493
|
-
* cockpit's `runs.json` (958 runs, 957 of them delegated): a running root with 26 workers
|
|
494
|
-
* paid about 5 ms per family read, and the live families together about 10 ms per global
|
|
495
|
-
* pass, all on the event loop. Holding them costs memory in proportion to active work, not to
|
|
496
|
-
* history.
|
|
497
|
-
*/
|
|
498
|
-
isAnchor(id, run) {
|
|
499
|
-
return isLiveRecord(run) || this.pins.has(id) || this.dirty.has(id) || this.committing.has(id);
|
|
500
|
-
}
|
|
501
|
-
/** The family roots with an anchoring member in memory. */
|
|
502
|
-
anchoredFamilies() {
|
|
503
|
-
const roots = new Set();
|
|
504
|
-
for (const [id, run] of this.held) {
|
|
505
|
-
const root = familyRootOf(run);
|
|
506
|
-
if (root !== undefined && this.isAnchor(id, run))
|
|
507
|
-
roots.add(root);
|
|
508
|
-
}
|
|
509
|
-
return roots;
|
|
510
|
-
}
|
|
511
|
-
/** Drop every held run that is neither an anchor nor in an anchored family. A store without a
|
|
512
|
-
* database keeps everything: memory is the only copy it has. */
|
|
513
|
-
evictSettled() {
|
|
514
|
-
if (!this.db)
|
|
515
|
-
return;
|
|
516
|
-
const families = this.anchoredFamilies();
|
|
517
|
-
for (const [id, run] of this.held) {
|
|
518
|
-
if (this.isAnchor(id, run))
|
|
519
|
-
continue;
|
|
520
|
-
const root = familyRootOf(run);
|
|
521
|
-
if (root !== undefined && families.has(root))
|
|
522
|
-
continue;
|
|
523
|
-
this.held.delete(id);
|
|
524
|
-
this.base.delete(id);
|
|
525
|
-
this.coldReads.forget(id);
|
|
526
|
-
}
|
|
527
|
-
this.claims.releaseUnheldClaims();
|
|
528
|
-
}
|
|
529
|
-
/** A run read from `runs.db`, normalized like a loaded one (`reconcileLoadedRun`, then the
|
|
530
|
-
* repository scope) but held by nobody. Undefined when absent, deleted or unreadable. */
|
|
531
|
-
loadCold(id) {
|
|
532
|
-
if (!this.db || this.deleted.has(id))
|
|
533
|
-
return undefined;
|
|
534
|
-
const row = this.db.get(id);
|
|
535
|
-
return row ? this.decodeCold(row) : undefined;
|
|
536
|
-
}
|
|
537
|
-
/** `loaded` sees the record once the load has normalized it, before it is scoped: a heal's `before`. */
|
|
538
|
-
decodeCold(row, loaded) {
|
|
539
|
-
this.coldReads.at(row.id, row.revision); // the fence below moves to this revision
|
|
540
|
-
const decoded = decodeRunRow(row.data);
|
|
541
|
-
if (!decoded) {
|
|
542
|
-
this.unreadable.add(row.id);
|
|
543
|
-
return undefined;
|
|
544
|
-
}
|
|
545
|
-
this.unreadable.delete(row.id);
|
|
546
|
-
const { run, extras } = decoded;
|
|
547
|
-
if (this.coldBase.size === 0)
|
|
548
|
-
queueMicrotask(() => this.coldBase.clear());
|
|
549
|
-
const family = rowFamily(row);
|
|
550
|
-
this.coldBase.set(row.id, { revision: row.revision, seq: row.seq, data: row.data, extras, owned: this.claimed.has(family) || this.pendingClaims.has(family) });
|
|
551
|
-
reconcileLoadedRun(run, { keepLive: this.keepLive });
|
|
552
|
-
loaded?.(run);
|
|
553
|
-
rescopeRun(run, this.repoHandle);
|
|
554
|
-
return run;
|
|
555
|
-
}
|
|
556
|
-
/** Hold a writable row. A fallback family's reads stay detached (the cold-read contract). */
|
|
557
|
-
holdDecoded(run) {
|
|
558
|
-
if (this.db?.familyHasFallback(familyKey(run)))
|
|
559
|
-
return process.env.VITEST ? deepFreeze(run) : run;
|
|
560
|
-
const read = this.coldBase.get(run.id);
|
|
561
|
-
this.held.set(run.id, run);
|
|
562
|
-
if (read)
|
|
563
|
-
this.base.set(run.id, storedRow(read));
|
|
564
|
-
return run;
|
|
565
|
-
}
|
|
566
|
-
/**
|
|
567
|
-
* Hold a claimed run for a write; leave after settlement unless anchored.
|
|
568
|
-
* Take the family claim before reading its row. Return undefined for a live foreign owner
|
|
569
|
-
* or a fallback family: neither claim is granted.
|
|
570
|
-
*/
|
|
571
|
-
hold(id) {
|
|
572
|
-
const held = this.held.get(id);
|
|
573
|
-
if (held)
|
|
574
|
-
return held;
|
|
575
|
-
const family = this.db && !this.deleted.has(id) ? this.db.familyOf(id) : undefined;
|
|
576
|
-
if (family === undefined || !this.claims.claimFamilies([family]).has(family))
|
|
577
|
-
return undefined;
|
|
578
|
-
const run = this.coldReads.claimed(id, this.coldBase.get(id), () => this.db?.revisionOf(id)) ?? this.loadCold(id);
|
|
579
|
-
if (!run)
|
|
580
|
-
return undefined;
|
|
581
|
-
this.holdDecoded(run);
|
|
582
|
-
this.persistence.scheduleSave();
|
|
583
|
-
return run;
|
|
584
|
-
}
|
|
585
|
-
/** The record a store write changes in place: held, or loaded to be. Undefined for a run this
|
|
586
|
-
* store may not write (another process's, or quarantined), so the write changes nothing. */
|
|
587
|
-
record(id) {
|
|
588
|
-
if (this.quarantined.has(id))
|
|
589
|
-
return undefined;
|
|
590
|
-
return this.held.get(id) ?? this.hold(id);
|
|
591
|
-
}
|
|
592
|
-
/** Read held legacy family members, or a detached cold record. */
|
|
593
|
-
peek(id) {
|
|
594
|
-
const held = this.held.get(id);
|
|
595
|
-
if (held)
|
|
596
|
-
return held;
|
|
597
|
-
const run = this.loadCold(id);
|
|
598
|
-
return run && this.answerCold(run, false);
|
|
599
|
-
}
|
|
600
|
-
/**
|
|
601
|
-
* A run just read off its row, as reads answer it: held when its family is in memory (a live
|
|
602
|
-
* family's members are read on every run event), else a copy nobody holds — frozen under
|
|
603
|
-
* vitest when it leaves the store (`freeze`).
|
|
604
|
-
*/
|
|
605
|
-
answerCold(run, freeze) {
|
|
606
|
-
const root = familyRootOf(run);
|
|
607
|
-
if (root !== undefined && this.anchoredFamilies().has(root))
|
|
608
|
-
return this.holdDecoded(run);
|
|
609
|
-
return freeze && process.env.VITEST ? deepFreeze(run) : run;
|
|
610
|
-
}
|
|
611
|
-
/** `getRun` for a row a query already read: the held object, else decoded from `row`. */
|
|
612
|
-
fromRow(row) {
|
|
613
|
-
const held = this.held.get(row.id);
|
|
614
|
-
if (held)
|
|
615
|
-
return held;
|
|
616
|
-
if (this.deleted.has(row.id))
|
|
617
|
-
return undefined;
|
|
618
|
-
const cached = this.coldReads.at(row.id, row.revision);
|
|
619
|
-
if (cached)
|
|
620
|
-
return cached;
|
|
621
|
-
const run = this.decodeCold(row);
|
|
622
|
-
return run && this.coldReads.keep(this.answerCold(run, true), this.coldBase.get(row.id), this.held.has(row.id));
|
|
623
|
-
}
|
|
624
|
-
/** Run `fn` reading each run that is not held at most once (`ColdReadCache`, hearsay-tools/cezarion#2378). */
|
|
625
|
-
withReadCache(fn) { return this.coldReads.scope(fn); }
|
|
626
|
-
/** Whether the run exists, held or not, without decoding it. */
|
|
627
|
-
hasRun(id) {
|
|
628
|
-
return this.held.has(id) || (!!this.db && !this.deleted.has(id) && this.db.has(id));
|
|
629
|
-
}
|
|
630
|
-
/**
|
|
631
|
-
* Set discovered repository identity without blocking boot. Recompute conclusions from retained
|
|
632
|
-
* evidence, including cold records; null retains unknown-handle behavior.
|
|
633
|
-
*/
|
|
634
|
-
setRepoHandle(handle) {
|
|
635
|
-
this.repoHandle = handle;
|
|
636
|
-
if (!handle)
|
|
637
|
-
return; // nothing to prove foreign against
|
|
638
|
-
// `touch` per healed run: the cockpit is already live when the handle lands, so a corrected
|
|
639
|
-
// chip has to reach the open page over SSE, not just the next `runs.json` write.
|
|
640
|
-
let healed = false;
|
|
641
|
-
const stored = this.db?.listWhere(guardedJsonSql(`${REFERENCED_RUN_RULE.sql} OR json_extract(summary, '$.pullRequests') IS NOT NULL`, 0)).filter((row) => {
|
|
642
|
-
if (this.held.has(row.id) || this.deleted.has(row.id))
|
|
643
|
-
return false;
|
|
644
|
-
const summary = parseStoredSummary(row.summary);
|
|
645
|
-
return !summary || namesForeignReference(summary, handle);
|
|
646
|
-
}) ?? [];
|
|
647
|
-
for (const run of [...this.held.values()]) {
|
|
648
|
-
const before = snapshotRun(run);
|
|
649
|
-
if (rescopeRun(run, this.repoHandle)) {
|
|
650
|
-
this.persistence.touch(run, before);
|
|
651
|
-
healed = true;
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
// Only rows this store may write: another process's runs heal when their owner arms its handle.
|
|
655
|
-
const writable = this.claims.claimFamilies(stored.map(rowFamily));
|
|
656
|
-
for (const row of stored) {
|
|
657
|
-
if (!writable.has(rowFamily(row)))
|
|
658
|
-
continue;
|
|
659
|
-
let before;
|
|
660
|
-
const run = this.decodeCold(row, (loaded) => { before = snapshotRun(loaded); });
|
|
661
|
-
// `decodeCold` already scoped it with the handle just armed; write it when that changed it.
|
|
662
|
-
if (!run || !this.storedScopeDiffers(row, run))
|
|
663
|
-
continue;
|
|
664
|
-
this.holdDecoded(run);
|
|
665
|
-
this.persistence.touch(run, before ?? snapshotRun(run));
|
|
666
|
-
healed = true;
|
|
667
|
-
}
|
|
668
|
-
// Discovery may finish after headless shutdown's final flush. Persist repairs now: the
|
|
669
|
-
// debounced save is unref'd, so it cannot keep the CLI alive once the lookup completes.
|
|
670
|
-
if (healed)
|
|
671
|
-
this.persistence.flush();
|
|
672
|
-
}
|
|
673
|
-
/** Whether the stored summary still names a reference the scoped record no longer has. */
|
|
674
|
-
storedScopeDiffers(row, run) {
|
|
675
|
-
const summary = parseStoredSummary(row.summary);
|
|
676
|
-
return summary?.referencedPullRequestUrl !== run.referencedPullRequestUrl ||
|
|
677
|
-
summary?.referencedIssueUrl !== run.referencedIssueUrl || summary?.issueNumber !== run.issueNumber || !isDeepStrictEqual(summary?.pullRequests, run.pullRequests);
|
|
678
|
-
}
|
|
679
|
-
/**
|
|
680
|
-
* The live set, newest first: runs whose record is live (`isLiveRecord`) or that a RunManager
|
|
681
|
-
* pins. Finished runs are not here — read one by id (`getRun`), by an indexed query
|
|
682
|
-
* (`listWorkersOf`, `findRunByClientRequestId`, `listRunsWithWorktree`,
|
|
683
|
-
* `listBranchOwners`), or as list rows (`listRunSummaries`).
|
|
684
|
-
*/
|
|
685
|
-
listRuns() {
|
|
686
|
-
return sortNewestFirst([...this.held].filter(([id, run]) => isLiveRecord(run) || this.pins.has(id)).map(([, run]) => [run, this.listOrder(run)]));
|
|
687
|
-
}
|
|
688
|
-
/** Where a held run lists: its row's insertion order, or after every row while this store has
|
|
689
|
-
* not written it yet. The held set keeps such runs in creation order, and so does every list
|
|
690
|
-
* that sorts them in held-set order (`sortNewestFirst` is stable). */
|
|
691
|
-
listOrder(run) {
|
|
692
|
-
return { createdAt: run.createdAt, seq: this.base.get(run.id)?.seq ?? Number.POSITIVE_INFINITY };
|
|
693
|
-
}
|
|
694
|
-
/**
|
|
695
|
-
* EVERY run as a full record, newest first — decoded row by row. Only the legacy `GET /runs`
|
|
696
|
-
* (older clients; the cockpit reads `GET /run-summaries`) may call this: it is exactly the
|
|
697
|
-
* whole-history parse #779 exists to remove from everything else.
|
|
698
|
-
*/
|
|
699
|
-
listAllRunsForLegacyRoute() {
|
|
700
|
-
const runs = new Map();
|
|
701
|
-
for (const row of this.db?.listAll() ?? []) {
|
|
702
|
-
if (this.deleted.has(row.id) || this.held.has(row.id))
|
|
703
|
-
continue;
|
|
704
|
-
const run = this.decodeCold(row);
|
|
705
|
-
if (run)
|
|
706
|
-
runs.set(row.id, [run, row]);
|
|
707
|
-
}
|
|
708
|
-
for (const [id, run] of this.held)
|
|
709
|
-
runs.set(id, [run, this.listOrder(run)]);
|
|
710
|
-
return sortNewestFirst(runs.values());
|
|
711
|
-
}
|
|
712
|
-
/** A bounded page of discoverable peer roots, including the current held overlay. */
|
|
713
|
-
listPeerTaskSummaries(options) {
|
|
714
|
-
return listPeerTaskSummaries({ db: this.db, held: this.held, skipped: this.deleted,
|
|
715
|
-
order: run => this.listOrder(run), cold: id => this.coldSummary(id) }, options);
|
|
716
|
-
}
|
|
717
|
-
/**
|
|
718
|
-
* Every run as its list row, newest first: the stored `summary` column, with each held run's
|
|
719
|
-
* own projection laid over it — a debounced save keeps memory up to 300 ms ahead of the row,
|
|
720
|
-
* and a run created since the last save has no row yet. `usage` is the caller's to attach.
|
|
721
|
-
*
|
|
722
|
-
* With `archivedWindow` (#864), the window the cockpit's lists read: every unarchived run, plus
|
|
723
|
-
* the newest `archivedWindow` archived ROOT runs, and whether older archived roots were left
|
|
724
|
-
* out. Archived workers are never in it (see `RunDatabase.listWindowSummaries`); with `roots`,
|
|
725
|
-
* no worker is — the runs index, whose readers list no workers. A held run
|
|
726
|
-
* lists by its record, so one archived or unarchived since the last save is ranked as it is now.
|
|
727
|
-
*
|
|
728
|
-
* A row this store found unreadable is left out, as the cold reader leaves out a row it had to
|
|
729
|
-
* decode and could not. A finished row nobody has decoded is served from its summary by both,
|
|
730
|
-
* which decodes nothing; reading it is what finds out (`isUnreadable`).
|
|
731
|
-
*/
|
|
732
|
-
listRunSummaries(options = {}) {
|
|
733
|
-
const { archivedWindow, roots = false } = options;
|
|
734
|
-
// Every skipped row may be an archived root, so ask for that many more: one past the window
|
|
735
|
-
// after skipping is how "older ones were left out" is known without counting them.
|
|
736
|
-
const rows = (archivedWindow === undefined
|
|
737
|
-
? this.db?.listSummaries()
|
|
738
|
-
: this.db?.listWindowSummaries(archivedWindow + 1 + this.deleted.size + this.unreadable.size + this.held.size, { roots })) ?? [];
|
|
739
|
-
const summaries = new Map();
|
|
740
|
-
for (const row of rows) {
|
|
741
|
-
if (this.deleted.has(row.id) || this.held.has(row.id) || this.unreadable.has(row.id))
|
|
742
|
-
continue;
|
|
743
|
-
const summary = parseStoredSummary(row.summary) ?? this.coldSummary(row.id);
|
|
744
|
-
if (summary)
|
|
745
|
-
summaries.set(row.id, [summary, row]);
|
|
746
|
-
}
|
|
747
|
-
for (const [id, run] of this.held) {
|
|
748
|
-
if (archivedWindow !== undefined && (run.archived || roots) && run.delegation?.role === 'worker')
|
|
749
|
-
continue;
|
|
750
|
-
summaries.set(id, [toRunSummary(run), this.listOrder(run)]);
|
|
751
|
-
}
|
|
752
|
-
const runs = sortNewestFirst(summaries.values());
|
|
753
|
-
if (archivedWindow === undefined)
|
|
754
|
-
return { runs, truncated: false };
|
|
755
|
-
let archived = 0;
|
|
756
|
-
const kept = runs.filter((run) => !run.archived || ++archived <= archivedWindow);
|
|
757
|
-
return { runs: kept, truncated: archived > archivedWindow };
|
|
758
|
-
}
|
|
759
|
-
/**
|
|
760
|
-
* One page of archived ROOT runs, newest first (#864): `GET /run-summaries/archived`, the
|
|
761
|
-
* archived runs past the window. Held runs list by their record, as in `listRunSummaries`.
|
|
762
|
-
* `before` is the previous page's `nextCursor`; with `q`, only runs `matchesRunSearch` keeps, and
|
|
763
|
-
* `total` counts those. `automationNames` lets `q` match a task by its automation's name, as the
|
|
764
|
-
* list's origin line shows it (hearsay-tools/cezarion#1616). A cursor this store did not write
|
|
765
|
-
* answers `{ error }`.
|
|
766
|
-
*
|
|
767
|
-
* Ranking reads every archived root's key (id, created_at, seq: no record, no summary), so the
|
|
768
|
-
* page and `total` agree with the held overlay; only the page's own rows are read in full.
|
|
769
|
-
*/
|
|
770
|
-
listArchivedRuns(options) {
|
|
771
|
-
let after;
|
|
772
|
-
if (options.before !== undefined) {
|
|
773
|
-
after = decodeArchivedCursor(options.before);
|
|
774
|
-
if (!after)
|
|
775
|
-
return { error: 'before is not a cursor this server wrote' };
|
|
776
|
-
}
|
|
777
|
-
const q = options.q?.trim() ?? '';
|
|
778
|
-
const ranked = this.rankArchivedRows(q, options.automationNames);
|
|
779
|
-
this.overlayHeldArchived(ranked, q, options.automationNames);
|
|
780
|
-
const ordered = [...ranked.entries()].sort(([, a], [, b]) => newestFirst(a, b));
|
|
781
|
-
return this.archivedPage(ordered, after, options.limit);
|
|
782
|
-
}
|
|
783
|
-
/** The archived roots in `runs.db`, each by its key: every one without `q` (no record, no
|
|
784
|
-
* summary read), and with `q` the searched rows `matchesRunSearch` keeps, which carry the summary
|
|
785
|
-
* the search read. A deleted, held or unreadable run's row is left out; a held run joins in
|
|
786
|
-
* `overlayHeldArchived`. */
|
|
787
|
-
rankArchivedRows(q, automationNames) {
|
|
788
|
-
const skipped = (id) => this.deleted.has(id) || this.held.has(id) || this.unreadable.has(id);
|
|
789
|
-
const ranked = new Map();
|
|
790
|
-
if (q === '') {
|
|
791
|
-
for (const key of this.db?.listKeysWhere(ARCHIVED_ROOT) ?? [])
|
|
792
|
-
if (!skipped(key.id))
|
|
793
|
-
ranked.set(key.id, key);
|
|
794
|
-
}
|
|
795
|
-
else {
|
|
796
|
-
for (const row of this.db?.searchRootSummaries(sqlPrefilterTokens(q, { automationNames })) ?? []) {
|
|
797
|
-
if (skipped(row.id))
|
|
798
|
-
continue;
|
|
799
|
-
const summary = parseStoredSummary(row.summary) ?? this.coldSummary(row.id);
|
|
800
|
-
if (summary?.archived && matchesRunSearch(summary, q, { automationNames }))
|
|
801
|
-
ranked.set(row.id, { ...row, summary });
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
return ranked;
|
|
805
|
-
}
|
|
806
|
-
/** Lay the held archived roots over `ranked`, each by its record: it replaces its stored row
|
|
807
|
-
* (already left out of `ranked`), and with `q` it must match. A held worker never lists. */
|
|
808
|
-
overlayHeldArchived(ranked, q, automationNames) {
|
|
809
|
-
for (const [id, run] of this.held) {
|
|
810
|
-
if (!run.archived || run.delegation?.role === 'worker')
|
|
811
|
-
continue;
|
|
812
|
-
const summary = toRunSummary(run);
|
|
813
|
-
if (q === '' || matchesRunSearch(summary, q, { automationNames }))
|
|
814
|
-
ranked.set(id, { ...this.listOrder(run), summary });
|
|
815
|
-
}
|
|
816
|
-
}
|
|
817
|
-
/** The page of `ordered` after the cursor `after`: its summaries, the cursor of the next page,
|
|
818
|
-
* and `total`, the count of all of `ordered`. */
|
|
819
|
-
archivedPage(ordered, after, limit) {
|
|
820
|
-
const rest = after ? ordered.filter(([, key]) => newestFirst(key, after) > 0) : ordered;
|
|
821
|
-
const page = rest.slice(0, limit);
|
|
822
|
-
const runs = this.hydrateArchivedPage(page);
|
|
823
|
-
const last = page.at(-1)?.[1];
|
|
824
|
-
return {
|
|
825
|
-
runs,
|
|
826
|
-
nextCursor: last && rest.length > page.length ? encodeArchivedCursor(last) : null,
|
|
827
|
-
total: ordered.length,
|
|
828
|
-
};
|
|
829
|
-
}
|
|
830
|
-
/** The page's summaries: a ranked key's own, else its stored one, read for the rows that have
|
|
831
|
-
* none in a single query. A row whose stored summary does not fit the contract is projected from
|
|
832
|
-
* its record; a row that cannot be read at all is left out. */
|
|
833
|
-
hydrateArchivedPage(page) {
|
|
834
|
-
const cold = new Map((this.db?.getSummaries(page.filter(([, key]) => !key.summary).map(([id]) => id)) ?? [])
|
|
835
|
-
.map((row) => [row.id, parseStoredSummary(row.summary) ?? this.coldSummary(row.id)]));
|
|
836
|
-
return page.flatMap(([id, key]) => {
|
|
837
|
-
const summary = key.summary ?? cold.get(id);
|
|
838
|
-
return summary ? [summary] : [];
|
|
839
|
-
});
|
|
840
|
-
}
|
|
841
|
-
/**
|
|
842
|
-
* The ROOT runs, archived or not, that match `query` (`matchesRunSearch`), newest first: at most
|
|
843
|
-
* `limit`, and whether more matched. The owned half of the workspace search (#864); held runs
|
|
844
|
-
* match by their record.
|
|
845
|
-
*/
|
|
846
|
-
searchRunSummaries(query, limit) {
|
|
847
|
-
const matched = new Map();
|
|
848
|
-
// Rows come newest first, so the newest `limit + 1` row matches are all a page can use; held
|
|
849
|
-
// runs, which may be newer than any row, are ranked in below.
|
|
850
|
-
this.db?.visitRootSummaries(sqlPrefilterTokens(query), (row) => {
|
|
851
|
-
if (this.deleted.has(row.id) || this.held.has(row.id) || this.unreadable.has(row.id))
|
|
852
|
-
return true;
|
|
853
|
-
const summary = parseStoredSummary(row.summary) ?? this.coldSummary(row.id);
|
|
854
|
-
if (summary && matchesRunSearch(summary, query))
|
|
855
|
-
matched.set(row.id, [summary, row]);
|
|
856
|
-
return matched.size <= limit;
|
|
857
|
-
});
|
|
858
|
-
for (const [id, run] of this.held) {
|
|
859
|
-
if (run.delegation?.role === 'worker')
|
|
860
|
-
continue;
|
|
861
|
-
const summary = toRunSummary(run);
|
|
862
|
-
if (matchesRunSearch(summary, query))
|
|
863
|
-
matched.set(id, [summary, this.listOrder(run)]);
|
|
864
|
-
}
|
|
865
|
-
const runs = sortNewestFirst(matched.values());
|
|
866
|
-
return { runs: runs.slice(0, limit), truncated: runs.length > limit };
|
|
867
|
-
}
|
|
868
|
-
/** A row whose stored summary does not fit the contract any more, projected from its record. */
|
|
869
|
-
coldSummary(id) {
|
|
870
|
-
const run = this.loadCold(id);
|
|
871
|
-
return run ? toRunSummary(run) : undefined;
|
|
872
|
-
}
|
|
873
446
|
/**
|
|
874
447
|
* The run, or undefined. A held run is the held object (writes install into it). Any other run
|
|
875
448
|
* is a fresh copy of its row each call, normalized like a loaded record — frozen under vitest —
|
|
@@ -879,232 +452,16 @@ export class RunStore {
|
|
|
879
452
|
const held = this.held.get(id);
|
|
880
453
|
if (held)
|
|
881
454
|
return held;
|
|
882
|
-
const cached = this.deleted.has(id) || !this.db ? undefined : this.coldReads.at(id, this.db.revisionOf(id));
|
|
455
|
+
const cached = this.deleted.has(id) || !this.db ? undefined : this.rows.coldReads.at(id, this.db.revisionOf(id));
|
|
883
456
|
if (cached)
|
|
884
457
|
return cached;
|
|
885
|
-
const run = this.loadCold(id);
|
|
886
|
-
return run && this.coldReads.keep(this.answerCold(run, true), this.coldBase.get(id), this.held.has(id));
|
|
458
|
+
const run = this.rows.loadCold(id);
|
|
459
|
+
return run && this.rows.coldReads.keep(this.rows.answerCold(run, true), this.rows.coldBase.get(id), this.held.has(id));
|
|
887
460
|
}
|
|
888
461
|
/** Whether the run's row is in `runs.db` but its record could not be read here: what tells a
|
|
889
462
|
* run that cannot open from one that does not exist. Known once something tried to read it. */
|
|
890
463
|
isUnreadable(id) {
|
|
891
|
-
return this.unreadable.has(id) && !this.held.has(id) && !this.deleted.has(id);
|
|
892
|
-
}
|
|
893
|
-
/** The ids in memory right now: the held set, for diagnostics and the benchmark's heap pass. */
|
|
894
|
-
heldIds() {
|
|
895
|
-
return [...this.held.keys()];
|
|
896
|
-
}
|
|
897
|
-
/**
|
|
898
|
-
* The workers a root run owns, newest first, read off its own `receipts` (#659). Every
|
|
899
|
-
* `createOwnedRun` writes the receipt and the worker record in one transaction, and the
|
|
900
|
-
* receipt list is capped at 1,024 creations (#816), so this is a bounded lookup rather than a walk of the whole
|
|
901
|
-
* project index. An ordinary or worker run owns nothing and costs one map read; a receipt
|
|
902
|
-
* whose record has been deleted is skipped, never invented. A parent quarantined to
|
|
903
|
-
* `invalid` has no readable receipts and reports none, which is also what the cockpit
|
|
904
|
-
* assumes for that role.
|
|
905
|
-
*/
|
|
906
|
-
listOwnedWorkers(parentId) {
|
|
907
|
-
const parent = this.getRun(parentId);
|
|
908
|
-
if (parent?.delegation?.role !== 'root')
|
|
909
|
-
return [];
|
|
910
|
-
const workers = [];
|
|
911
|
-
for (const receipt of parent.delegation.receipts) {
|
|
912
|
-
const worker = this.getRun(receipt.workerId);
|
|
913
|
-
if (worker?.delegation?.role === 'worker' && worker.delegation.parentRunId === parentId)
|
|
914
|
-
workers.push(worker);
|
|
915
|
-
}
|
|
916
|
-
return workers.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
|
917
|
-
}
|
|
918
|
-
/** Every worker filed under `parentId` (the `parent_run_id` index plus memory), newest first:
|
|
919
|
-
* receipts or not, which is what the reconcile and deletion checks ask. */
|
|
920
|
-
listWorkersOf(parentId) {
|
|
921
|
-
const workers = new Map();
|
|
922
|
-
for (const run of this.held.values()) {
|
|
923
|
-
if (run.delegation?.role === 'worker' && run.delegation.parentRunId === parentId)
|
|
924
|
-
workers.set(run.id, [run, this.listOrder(run)]);
|
|
925
|
-
}
|
|
926
|
-
// Ids first: a live family's workers are usually all held, and then no record is read at all.
|
|
927
|
-
const cold = (this.db?.listIdsByParent(parentId) ?? []).filter((id) => !this.held.has(id) && !this.deleted.has(id));
|
|
928
|
-
for (const row of cold.length > 0 ? this.db.getMany(cold) : []) {
|
|
929
|
-
const run = this.fromRow(row);
|
|
930
|
-
if (run?.delegation?.role === 'worker' && run.delegation.parentRunId === parentId)
|
|
931
|
-
workers.set(row.id, [run, row]);
|
|
932
|
-
}
|
|
933
|
-
return sortNewestFirst(workers.values());
|
|
934
|
-
}
|
|
935
|
-
/** The id of every worker, held or stored, without decoding any (recovery's worker passes). */
|
|
936
|
-
listWorkerIds() {
|
|
937
|
-
const ids = new Set(this.db?.listWorkerIds() ?? []);
|
|
938
|
-
for (const id of this.deleted)
|
|
939
|
-
ids.delete(id);
|
|
940
|
-
for (const run of this.held.values())
|
|
941
|
-
if (run.delegation?.role === 'worker')
|
|
942
|
-
ids.add(run.id);
|
|
943
|
-
return [...ids];
|
|
944
|
-
}
|
|
945
|
-
/** The id of every run whose delegation is quarantined (`invalid`), held or stored: its stored
|
|
946
|
-
* summary says so, so nothing is decoded (scratch cleanup). */
|
|
947
|
-
listQuarantinedRunIds() {
|
|
948
|
-
const ids = new Set((this.db?.listKeysWhere(guardedJsonSql("json_extract(summary, '$.delegation.role') = 'invalid'", 0)) ?? []).map((key) => key.id));
|
|
949
|
-
for (const id of this.deleted)
|
|
950
|
-
ids.delete(id);
|
|
951
|
-
for (const [id, run] of this.held) {
|
|
952
|
-
if (run.delegation?.role === 'invalid')
|
|
953
|
-
ids.add(id);
|
|
954
|
-
else
|
|
955
|
-
ids.delete(id);
|
|
956
|
-
}
|
|
957
|
-
return [...ids];
|
|
958
|
-
}
|
|
959
|
-
/** All retained tasks this automation started, including variants, finished and archived runs.
|
|
960
|
-
* Logs and receipts may compact independently. Read only matching keys in SQLite and overlay
|
|
961
|
-
* held records so a debounced provenance update or deletion is reflected immediately. */
|
|
962
|
-
countAutomationRuns(automationId) {
|
|
963
|
-
const ids = new Set(this.db?.listAutomationRuns(automationId) ?? []);
|
|
964
|
-
for (const id of this.deleted)
|
|
965
|
-
ids.delete(id);
|
|
966
|
-
for (const [id, run] of this.held) {
|
|
967
|
-
if (run.automation?.automationId === automationId || run.automationTrigger?.automationId === automationId)
|
|
968
|
-
ids.add(id);
|
|
969
|
-
else
|
|
970
|
-
ids.delete(id);
|
|
971
|
-
}
|
|
972
|
-
return ids.size;
|
|
973
|
-
}
|
|
974
|
-
/** First non-archived task referencing a project issue, including secondary references. */
|
|
975
|
-
findNonArchivedRunByIssue(repo, number) {
|
|
976
|
-
const url = `https://github.com/${repo}/issues/${number}`.toLowerCase();
|
|
977
|
-
const matches = (run) => {
|
|
978
|
-
if (run.archived)
|
|
979
|
-
return false;
|
|
980
|
-
const urls = [run.referencedIssueUrl, ...(run.referencedIssueCandidates ?? []),
|
|
981
|
-
...(run.automation?.event.startsWith('issue.') ? [run.automation.githubUrl] : []),
|
|
982
|
-
...[...run.task.matchAll(new RegExp(ISSUE_URL_RE.source, 'gi'))].map((match) => match[0])]
|
|
983
|
-
.filter((value) => value !== undefined)
|
|
984
|
-
.map((value) => value.replace(/\/+$/, '').toLowerCase())
|
|
985
|
-
.filter((value) => value.endsWith(`/issues/${number}`));
|
|
986
|
-
// The legacy issueNumber is display-only and cannot establish repository identity.
|
|
987
|
-
// Explicit task markers remain project-scoped unless a qualified URL contradicts them.
|
|
988
|
-
return urls.includes(url) || (urls.length === 0 && run.markerRefs?.issue === number);
|
|
989
|
-
};
|
|
990
|
-
for (const run of this.held.values())
|
|
991
|
-
if (matches(run))
|
|
992
|
-
return run.id;
|
|
993
|
-
// Secondary references are record-only: query them in SQLite rather than hydrating every
|
|
994
|
-
// finished transcript or relying on the slim summary's primary issue number.
|
|
995
|
-
const where = `archived = 0 AND ${guardedJsonSql(`
|
|
996
|
-
json_extract(data, '$.issueNumber') = ? OR json_extract(data, '$.markerRefs.issue') = ?
|
|
997
|
-
OR lower(rtrim(json_extract(data, '$.referencedIssueUrl'), '/')) = ?
|
|
998
|
-
OR EXISTS (SELECT 1 FROM json_each(data, '$.referencedIssueCandidates') WHERE lower(rtrim(value, '/')) = ?)
|
|
999
|
-
OR (json_extract(data, '$.automation.event') LIKE 'issue.%' AND lower(rtrim(json_extract(data, '$.automation.githubUrl'), '/')) = ?)
|
|
1000
|
-
OR instr(lower(json_extract(data, '$.task')), ?) > 0
|
|
1001
|
-
`, 0, ['data'])}`;
|
|
1002
|
-
for (const key of this.db?.listKeysWhere(where, [number, number, url, url, url, url]) ?? []) {
|
|
1003
|
-
if (this.held.has(key.id) || this.deleted.has(key.id) || this.unreadable.has(key.id))
|
|
1004
|
-
continue;
|
|
1005
|
-
const run = this.getRun(key.id);
|
|
1006
|
-
if (run && matches(run))
|
|
1007
|
-
return key.id;
|
|
1008
|
-
}
|
|
1009
|
-
return undefined;
|
|
1010
|
-
}
|
|
1011
|
-
/**
|
|
1012
|
-
* The runs automation receipts launched, by receipt id (`automation.receiptId` or
|
|
1013
|
-
* `automationTrigger.receiptId`). Memory answers first; a receipt it does not know costs one
|
|
1014
|
-
* pass over `runs.db` that reads the records' JSON in SQLite — only a crash leftover (a
|
|
1015
|
-
* receipt still `reserved` at startup) ever asks.
|
|
1016
|
-
*/
|
|
1017
|
-
findRunIdsByAutomationReceipt(receiptIds) {
|
|
1018
|
-
const wanted = new Set(receiptIds);
|
|
1019
|
-
const found = new Map();
|
|
1020
|
-
const receiptOf = (run) => run.automation?.receiptId ?? run.automationTrigger?.receiptId;
|
|
1021
|
-
for (const run of this.held.values()) {
|
|
1022
|
-
const receipt = receiptOf(run);
|
|
1023
|
-
if (receipt !== undefined && wanted.has(receipt))
|
|
1024
|
-
found.set(receipt, run.id);
|
|
1025
|
-
}
|
|
1026
|
-
const missing = [...wanted].filter((receipt) => !found.has(receipt));
|
|
1027
|
-
if (missing.length === 0 || !this.db)
|
|
1028
|
-
return found;
|
|
1029
|
-
for (const id of this.db.listRunsByAutomationReceipt(missing)) {
|
|
1030
|
-
if (this.held.has(id) || this.deleted.has(id))
|
|
1031
|
-
continue;
|
|
1032
|
-
const run = this.getRun(id);
|
|
1033
|
-
const receipt = run && receiptOf(run);
|
|
1034
|
-
if (receipt !== undefined && wanted.has(receipt) && !found.has(receipt))
|
|
1035
|
-
found.set(receipt, id);
|
|
1036
|
-
}
|
|
1037
|
-
return found;
|
|
1038
|
-
}
|
|
1039
|
-
/** The id of every root whose delegation carries a conversation, held or stored: recovery's full
|
|
1040
|
-
* reconcile pass (#661). `naming` keeps the ledgers whose messages name that run id as sender or
|
|
1041
|
-
* recipient. Stored roots come from the `conversation_members` index, so no record is read. */
|
|
1042
|
-
listConversationRootIds(naming) {
|
|
1043
|
-
const ids = new Set(this.db?.listConversationRoots(naming) ?? []);
|
|
1044
|
-
for (const id of this.deleted)
|
|
1045
|
-
ids.delete(id);
|
|
1046
|
-
for (const [id, run] of this.held) {
|
|
1047
|
-
if (run.delegation?.role === 'root' && run.delegation.conversation && (!naming || run.delegation.conversation.messages.some(message => [message.senderRunId, message.recipientRunId].includes(naming))))
|
|
1048
|
-
ids.add(id);
|
|
1049
|
-
else
|
|
1050
|
-
ids.delete(id);
|
|
1051
|
-
}
|
|
1052
|
-
return [...ids];
|
|
1053
|
-
}
|
|
1054
|
-
/** The id of every run, held or stored, without decoding any (orphan-worktree sweeps). */
|
|
1055
|
-
listRunIds() {
|
|
1056
|
-
const ids = new Set(this.db?.listIds() ?? []);
|
|
1057
|
-
for (const id of this.deleted)
|
|
1058
|
-
ids.delete(id);
|
|
1059
|
-
for (const id of this.held.keys())
|
|
1060
|
-
ids.add(id);
|
|
1061
|
-
return [...ids];
|
|
1062
|
-
}
|
|
1063
|
-
/** Runs with a materialized worktree directory (`worktreePath`, not reclaimed), most recently
|
|
1064
|
-
* finished first, then newest first: worktree retention and the worktrees panel. */
|
|
1065
|
-
listRunsWithWorktree() {
|
|
1066
|
-
const runs = this.queryRuns(this.db?.listWithWorktree() ?? [], (run) => materializedWorktreePath(run) !== undefined);
|
|
1067
|
-
const recency = ([run]) => run.finishedAt ?? run.createdAt;
|
|
1068
|
-
return runs.sort((a, b) => (recency(a) < recency(b) ? 1 : recency(a) > recency(b) ? -1 : newestFirst(a[1], b[1]))).map(([run]) => run);
|
|
1069
|
-
}
|
|
1070
|
-
/**
|
|
1071
|
-
* Every run that owns a branch or names the PR it created, as the few fields branch cleanup and
|
|
1072
|
-
* git-log attribution read (issue 08 §B3, §B5; a squash commit is attributed by its PR number):
|
|
1073
|
-
* the stored summary plus the `branch` and `base_branch` columns, so a Git-tab request decodes
|
|
1074
|
-
* no record. Held runs answer from memory.
|
|
1075
|
-
*/
|
|
1076
|
-
listBranchOwners() {
|
|
1077
|
-
const rows = (this.db?.listBranchOwners(`branch IS NOT NULL OR ${guardedJsonSql("json_extract(summary, '$.pullRequestUrl') IS NOT NULL", 0)}`) ?? [])
|
|
1078
|
-
.filter((row) => !this.deleted.has(row.id) && !this.held.has(row.id));
|
|
1079
|
-
return listBranchOwners(rows, [...this.held.values()], (id) => this.getRun(id));
|
|
1080
|
-
}
|
|
1081
|
-
/** The rows a column query returned, as records, with memory laid over them: a held run
|
|
1082
|
-
* answers from memory (matching `matches` there, not on its maybe-older row). */
|
|
1083
|
-
queryRuns(rows, matches) {
|
|
1084
|
-
const runs = new Map();
|
|
1085
|
-
for (const run of this.held.values())
|
|
1086
|
-
if (matches(run))
|
|
1087
|
-
runs.set(run.id, [run, this.listOrder(run)]);
|
|
1088
|
-
for (const row of rows) {
|
|
1089
|
-
if (runs.has(row.id) || this.held.has(row.id) || this.deleted.has(row.id))
|
|
1090
|
-
continue;
|
|
1091
|
-
const run = this.fromRow(row);
|
|
1092
|
-
if (run && matches(run))
|
|
1093
|
-
runs.set(row.id, [run, row]);
|
|
1094
|
-
}
|
|
1095
|
-
return [...runs.values()];
|
|
1096
|
-
}
|
|
1097
|
-
/** The run an idempotent start already created (#504), archived or not. A deleted run is gone
|
|
1098
|
-
* with its record, so a retry after deletion honestly creates a fresh one. */
|
|
1099
|
-
findRunByClientRequestId(clientRequestId) {
|
|
1100
|
-
for (const run of this.held.values())
|
|
1101
|
-
if (run.clientRequestId === clientRequestId)
|
|
1102
|
-
return run;
|
|
1103
|
-
const row = this.db?.findByClientRequestId(clientRequestId);
|
|
1104
|
-
if (!row || this.held.has(row.id))
|
|
1105
|
-
return undefined;
|
|
1106
|
-
const run = this.getRun(row.id);
|
|
1107
|
-
return run?.clientRequestId === clientRequestId ? run : undefined;
|
|
464
|
+
return this.rows.unreadable.has(id) && !this.held.has(id) && !this.deleted.has(id);
|
|
1108
465
|
}
|
|
1109
466
|
createRun(input) {
|
|
1110
467
|
if (this.openFailure)
|
|
@@ -1161,33 +518,33 @@ export class RunStore {
|
|
|
1161
518
|
// A prompt that pastes a PR or issue URL is already about that item — seed
|
|
1162
519
|
// both referenced tiers so queued runs can expose the reference before the
|
|
1163
520
|
// first agent event (#407, #554).
|
|
1164
|
-
trackReferencedPrs(run, input.task, this.repoHandle);
|
|
1165
|
-
trackReferencedIssues(run, input.task, this.repoHandle);
|
|
521
|
+
trackReferencedPrs(run, input.task, this.repoScope.repoHandle);
|
|
522
|
+
trackReferencedIssues(run, input.task, this.repoScope.repoHandle);
|
|
1166
523
|
return run;
|
|
1167
524
|
}
|
|
1168
525
|
/** Accept the family ledger and its recipient queue in a single durable index replacement. A new
|
|
1169
526
|
* message (`message`, or the delivered input) whose ID is in use writes nothing; `recipientDelegation`
|
|
1170
527
|
* makes a peer recipient with no delegation record a root task in the same write. */
|
|
1171
528
|
commitConversation(rootId, conversation, delivery, message) {
|
|
1172
|
-
const root = this.peek(rootId);
|
|
529
|
+
const root = this.rows.peek(rootId);
|
|
1173
530
|
if (root?.delegation?.role !== 'root')
|
|
1174
531
|
throw new Error('missing conversation root');
|
|
1175
|
-
assertConversationIdFree(root, message ?? delivery?.input, id => this.peek(id), (runId, id) => this.eventLog.hasProjection(runId, id));
|
|
532
|
+
assertConversationIdFree(root, message ?? delivery?.input, id => this.rows.peek(id), (runId, id) => this.eventLog.hasProjection(runId, id));
|
|
1176
533
|
const staged = new Map();
|
|
1177
|
-
stageConversation(staged, root, conversation, delivery, id => this.peek(id), text => this.redaction.redactText(text));
|
|
534
|
+
stageConversation(staged, root, conversation, delivery, id => this.rows.peek(id), text => this.redaction.redactText(text));
|
|
1178
535
|
this.persistence.commitIndex(staged);
|
|
1179
536
|
}
|
|
1180
537
|
/** Crash recovery (#505): input a harness accepted but was never seen reading goes
|
|
1181
538
|
* back to the queue. Returns the requeued IDs. */
|
|
1182
539
|
requeueAwaitingReadInputs(id) {
|
|
1183
|
-
const ids = (this.peek(id)?.agentInputs ?? []).filter(input => input.awaitingRead && input.deliveredAt && !input.consumedAt).map(input => input.id);
|
|
540
|
+
const ids = (this.rows.peek(id)?.agentInputs ?? []).filter(input => input.awaitingRead && input.deliveredAt && !input.consumedAt).map(input => input.id);
|
|
1184
541
|
if (ids.length)
|
|
1185
542
|
this.commitOutcome(id, { kind: 'requeued', inputIds: ids });
|
|
1186
543
|
return ids;
|
|
1187
544
|
}
|
|
1188
545
|
/** Delivered, but the harness never confirmed reading it (#505): stop awaiting a read. */
|
|
1189
546
|
commitAgentInputsUnconfirmed(id, ids) {
|
|
1190
|
-
const run = this.peek(id);
|
|
547
|
+
const run = this.rows.peek(id);
|
|
1191
548
|
if (!run?.agentInputs || !ids.length)
|
|
1192
549
|
return;
|
|
1193
550
|
this.commitAgentInputs(id, run.agentInputs.map(input => {
|
|
@@ -1200,7 +557,7 @@ export class RunStore {
|
|
|
1200
557
|
/** Observable atomic input checkpoint for the queue itself: enqueue, retire, reorder. Delivery
|
|
1201
558
|
* and reads are outcomes (`commitOutcome`). */
|
|
1202
559
|
commitAgentInputs(id, inputs) {
|
|
1203
|
-
const run = this.peek(id);
|
|
560
|
+
const run = this.rows.peek(id);
|
|
1204
561
|
if (!run)
|
|
1205
562
|
throw new Error('missing agent input target');
|
|
1206
563
|
const agentInputs = inputs.map(input => agentInputSchema.parse(input));
|
|
@@ -1223,7 +580,7 @@ export class RunStore {
|
|
|
1223
580
|
}
|
|
1224
581
|
/** Reserve only unread conversation inputs in one durable index replacement. */
|
|
1225
582
|
claimInboxInputs(runId, ids, claim) {
|
|
1226
|
-
const run = this.peek(runId);
|
|
583
|
+
const run = this.rows.peek(runId);
|
|
1227
584
|
if (!run)
|
|
1228
585
|
throw new Error('missing inbox recipient');
|
|
1229
586
|
const selected = z.array(z.uuid()).min(1).max(32).refine(values => new Set(values).size === values.length).parse(ids);
|
|
@@ -1246,7 +603,7 @@ export class RunStore {
|
|
|
1246
603
|
}
|
|
1247
604
|
/** A receipt owns every matching input or none; exact ACK retries preserve the first timestamp. */
|
|
1248
605
|
ackInboxInputs(runId, receiptId, generation, at) {
|
|
1249
|
-
const run = this.peek(runId);
|
|
606
|
+
const run = this.rows.peek(runId);
|
|
1250
607
|
if (!run)
|
|
1251
608
|
throw new Error('missing inbox recipient');
|
|
1252
609
|
inboxClaimSchema.shape.receiptId.parse(receiptId);
|
|
@@ -1271,7 +628,7 @@ export class RunStore {
|
|
|
1271
628
|
return 'acknowledged';
|
|
1272
629
|
}
|
|
1273
630
|
releaseInboxInputs(runId, receiptId, generation) {
|
|
1274
|
-
const run = this.peek(runId);
|
|
631
|
+
const run = this.rows.peek(runId);
|
|
1275
632
|
if (!run)
|
|
1276
633
|
throw new Error('missing inbox recipient');
|
|
1277
634
|
inboxClaimSchema.shape.receiptId.parse(receiptId);
|
|
@@ -1289,7 +646,7 @@ export class RunStore {
|
|
|
1289
646
|
return 'released';
|
|
1290
647
|
}
|
|
1291
648
|
clearExpiredInboxClaims(runId, now) {
|
|
1292
|
-
const run = this.peek(runId);
|
|
649
|
+
const run = this.rows.peek(runId);
|
|
1293
650
|
if (!run)
|
|
1294
651
|
throw new Error('missing inbox recipient');
|
|
1295
652
|
inboxClaimSchema.shape.expiresAt.parse(now);
|
|
@@ -1310,14 +667,14 @@ export class RunStore {
|
|
|
1310
667
|
* past `WAIT_LIMIT` open ones is refused, so the list stays bounded whatever the caller checked.
|
|
1311
668
|
*/
|
|
1312
669
|
commitWait(id, wait, input, registration) {
|
|
1313
|
-
const run = this.peek(id);
|
|
670
|
+
const run = this.rows.peek(id);
|
|
1314
671
|
if (!run)
|
|
1315
672
|
throw new Error('missing wait target');
|
|
1316
673
|
this.persistence.commitIndex(new Map([[id, registeredWaitRecord(run, wait, input, registration)]]));
|
|
1317
674
|
}
|
|
1318
675
|
/** Record a deduplicated registration without mutating its wait. */
|
|
1319
676
|
commitWaitRegistration(id, receipt) {
|
|
1320
|
-
const run = this.peek(id);
|
|
677
|
+
const run = this.rows.peek(id);
|
|
1321
678
|
if (!run)
|
|
1322
679
|
throw new Error('missing wait target');
|
|
1323
680
|
this.persistence.commitIndex(new Map([[id, registeredReceiptRecord(run, receipt)]]));
|
|
@@ -1327,7 +684,7 @@ export class RunStore {
|
|
|
1327
684
|
* or recovery re-arms work the user cancelled. A run held for its resume settles `failed` by
|
|
1328
685
|
* the `withdraw-hold` exit; any other run only loses its resume fields. */
|
|
1329
686
|
commitAutoResumeWithdrawal(id) {
|
|
1330
|
-
const run = this.peek(id);
|
|
687
|
+
const run = this.rows.peek(id);
|
|
1331
688
|
if (!run || (run.autoResumeAt === undefined && run.autoResumeAttempts === undefined))
|
|
1332
689
|
return;
|
|
1333
690
|
if (isUsageLimitHold(run))
|
|
@@ -1340,7 +697,7 @@ export class RunStore {
|
|
|
1340
697
|
* the human's and keeps its wake text, so the message opens the continuation (and answers a
|
|
1341
698
|
* pending question) and the owed results still reach the agent. */
|
|
1342
699
|
commitHumanMessageBesideWaits(id, acceptedHumanMessage) {
|
|
1343
|
-
const run = this.peek(id);
|
|
700
|
+
const run = this.rows.peek(id);
|
|
1344
701
|
if (!run)
|
|
1345
702
|
throw new Error('missing queued message target');
|
|
1346
703
|
this.persistence.commitIndex(new Map([[id, { ...run, queuedMessages: [...(run.queuedMessages ?? []), queuedMessageSchema.parse(acceptedHumanMessage)],
|
|
@@ -1358,7 +715,7 @@ export class RunStore {
|
|
|
1358
715
|
* A conversation request outcome is the other sanctioned write: `commitConversation`.
|
|
1359
716
|
*/
|
|
1360
717
|
commitOutcome(id, outcomes, deps = {}, opts = {}) {
|
|
1361
|
-
const run = this.peek(id);
|
|
718
|
+
const run = this.rows.peek(id);
|
|
1362
719
|
if (!run)
|
|
1363
720
|
return { committed: false, answered: [], settled: [] };
|
|
1364
721
|
const staged = applyOutcomes(run, Array.isArray(outcomes) ? outcomes : [outcomes], deps);
|
|
@@ -1371,7 +728,7 @@ export class RunStore {
|
|
|
1371
728
|
if (!opts.bestEffort)
|
|
1372
729
|
throw error;
|
|
1373
730
|
console.warn(`[hsq] outcome checkpoint failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
1374
|
-
const live = this.record(id);
|
|
731
|
+
const live = this.rows.record(id);
|
|
1375
732
|
if (!live)
|
|
1376
733
|
return { committed: false, answered: staged.answered, settled: staged.settled };
|
|
1377
734
|
const before = snapshotRun(live);
|
|
@@ -1383,7 +740,7 @@ export class RunStore {
|
|
|
1383
740
|
}
|
|
1384
741
|
/** An acknowledged inactive-root Finish must survive the async diff and restart. */
|
|
1385
742
|
commitRootFinishIntent(id) {
|
|
1386
|
-
const run = this.peek(id);
|
|
743
|
+
const run = this.rows.peek(id);
|
|
1387
744
|
if (run?.status !== 'waiting' || run.delegation?.role !== 'root')
|
|
1388
745
|
throw new Error('root is not waiting');
|
|
1389
746
|
if (run.delegation.finishRequestedAt)
|
|
@@ -1397,7 +754,7 @@ export class RunStore {
|
|
|
1397
754
|
const staged = new Map();
|
|
1398
755
|
let destroyProgress = true;
|
|
1399
756
|
for (const patch of patches) {
|
|
1400
|
-
const run = this.peek(patch.id);
|
|
757
|
+
const run = this.rows.peek(patch.id);
|
|
1401
758
|
if (!run || staged.has(patch.id))
|
|
1402
759
|
throw new Error('missing or duplicate delegation patch target');
|
|
1403
760
|
const delegation = delegationStateSchema.parse(patch.delegation);
|
|
@@ -1413,7 +770,7 @@ export class RunStore {
|
|
|
1413
770
|
* the record beside the strict delegation, so an older Squawk strips it rather than quarantining the
|
|
1414
771
|
* worker. Destroy progress: nothing the family reconcile reads changes. */
|
|
1415
772
|
commitDestroyRetry(id, retry) {
|
|
1416
|
-
const run = this.peek(id);
|
|
773
|
+
const run = this.rows.peek(id);
|
|
1417
774
|
if (run?.delegation?.role !== 'worker' || !run.delegation.destroy)
|
|
1418
775
|
throw new Error('missing pending destroy');
|
|
1419
776
|
if (!retry && !run.destroyRetry)
|
|
@@ -1423,7 +780,7 @@ export class RunStore {
|
|
|
1423
780
|
}
|
|
1424
781
|
/** Accepted execution revision is public lifecycle identity, separate from process generations. */
|
|
1425
782
|
commitWorkerContinuation(id, exit, patch = {}, step, conversation) {
|
|
1426
|
-
const run = this.peek(id);
|
|
783
|
+
const run = this.rows.peek(id);
|
|
1427
784
|
if (run?.delegation?.role !== 'worker')
|
|
1428
785
|
throw new Error('missing worker continuation target');
|
|
1429
786
|
const delegation = delegationStateSchema.parse({ ...run.delegation,
|
|
@@ -1439,18 +796,18 @@ export class RunStore {
|
|
|
1439
796
|
if (Object.prototype.hasOwnProperty.call(patch, 'archived'))
|
|
1440
797
|
this.archive.syncTranscriptForm(id, next.archived);
|
|
1441
798
|
if (conversation) {
|
|
1442
|
-
const root = this.peek(conversation.rootId);
|
|
799
|
+
const root = this.rows.peek(conversation.rootId);
|
|
1443
800
|
if (root?.delegation?.role !== 'root' || run.delegation.parentRunId !== root.id)
|
|
1444
801
|
throw new Error('missing conversation ownership');
|
|
1445
|
-
stageConversation(staged, root, conversation.state, { recipientRunId: id, input: conversation.input }, runId => this.peek(runId), text => this.redaction.redactText(text));
|
|
802
|
+
stageConversation(staged, root, conversation.state, { recipientRunId: id, input: conversation.input }, runId => this.rows.peek(runId), text => this.redaction.redactText(text));
|
|
1446
803
|
}
|
|
1447
804
|
this.persistence.commitIndex(staged, undefined, { exits: new Map([[id, exit]]) });
|
|
1448
805
|
}
|
|
1449
806
|
/** Create an owned worker run and its parent receipt; `createStoreOwnedRun` holds the checks. */
|
|
1450
807
|
createOwnedRun(input, parentId, requestId, worker, requestHash, spawnCapacityRefusal, executionIdentity = { kind: 'internal' }) {
|
|
1451
808
|
return createStoreOwnedRun({
|
|
1452
|
-
peek: (id) => this.peek(id),
|
|
1453
|
-
hasRun: (id) => this.hasRun(id),
|
|
809
|
+
peek: (id) => this.rows.peek(id),
|
|
810
|
+
hasRun: (id) => this.rows.hasRun(id),
|
|
1454
811
|
held: this.held, deleted: this.deleted, db: this.db,
|
|
1455
812
|
buildRun: (ownedInput, id) => this.buildRun(ownedInput, id),
|
|
1456
813
|
writeWorkerIdentity: (id, identity) => this.workerExecutions.writeWorkerIdentity(id, identity),
|
|
@@ -1467,7 +824,7 @@ export class RunStore {
|
|
|
1467
824
|
move(id, exit, extras = {}, call = {}) {
|
|
1468
825
|
const spec = RUN_EXITS[exit];
|
|
1469
826
|
const guarded = Boolean(spec.guard || spec.settle);
|
|
1470
|
-
const run = guarded ? this.peek(id) : this.record(id);
|
|
827
|
+
const run = guarded ? this.rows.peek(id) : this.rows.record(id);
|
|
1471
828
|
if (!run || spec.guard?.(run) === false) {
|
|
1472
829
|
if (spec.refused)
|
|
1473
830
|
throw new Error(spec.refused);
|
|
@@ -1482,7 +839,7 @@ export class RunStore {
|
|
|
1482
839
|
else {
|
|
1483
840
|
const input = { ...run, ...redacted };
|
|
1484
841
|
this.persistence.commitIndex(new Map([[id, { ...(spec.settle?.(input, { ...call, now: new Date().toISOString() }) ?? input), ...applyExit(run, exit, {}, call) }]]), undefined, { exits: new Map([[id, exit]]) });
|
|
1485
|
-
moved = this.peek(id);
|
|
842
|
+
moved = this.rows.peek(id);
|
|
1486
843
|
}
|
|
1487
844
|
if (event)
|
|
1488
845
|
this.exitEvents.write(id, event);
|
|
@@ -1492,7 +849,7 @@ export class RunStore {
|
|
|
1492
849
|
* completion option or task access, else the next save writes it. A status in the patch, which only test
|
|
1493
850
|
* fixtures still write, takes the exit's record rule (`doneCommits`). */
|
|
1494
851
|
updateRun(id, patch) {
|
|
1495
|
-
const run = this.record(id);
|
|
852
|
+
const run = this.rows.record(id);
|
|
1496
853
|
if (!run)
|
|
1497
854
|
return undefined;
|
|
1498
855
|
const committed = (patch.status === 'done' && doneCommits(run)) || patch.handoffRecords !== undefined ||
|
|
@@ -1505,7 +862,7 @@ export class RunStore {
|
|
|
1505
862
|
const before = snapshotRun(run);
|
|
1506
863
|
const hooks = {
|
|
1507
864
|
redact: (text) => this.redaction.redactText(text),
|
|
1508
|
-
repoHandle: this.repoHandle,
|
|
865
|
+
repoHandle: this.repoScope.repoHandle,
|
|
1509
866
|
restore: () => restoreHistory(this.dataDir, id),
|
|
1510
867
|
archive: (record, archived) => this.archive.applyArchived(record, archived, before, exit),
|
|
1511
868
|
touch: (record) => this.persistence.touch(record, before, exit),
|
|
@@ -1513,7 +870,7 @@ export class RunStore {
|
|
|
1513
870
|
if (durability === 'committed') {
|
|
1514
871
|
const staged = stageDurableUpdate(run, patch, hooks, exit);
|
|
1515
872
|
this.persistence.commitIndex(new Map([[id, staged.run]]), undefined, { before: new Map([[id, before]]), exits: new Map([[id, staged.exit]]) });
|
|
1516
|
-
return this.record(id);
|
|
873
|
+
return this.rows.record(id);
|
|
1517
874
|
}
|
|
1518
875
|
stageDebouncedUpdate(run, patch, hooks);
|
|
1519
876
|
return run;
|
|
@@ -1523,7 +880,7 @@ export class RunStore {
|
|
|
1523
880
|
}
|
|
1524
881
|
/** Append a step to an existing run (used by "Continue" — spec 003). */
|
|
1525
882
|
addStep(runId, step) {
|
|
1526
|
-
const run = this.record(runId);
|
|
883
|
+
const run = this.rows.record(runId);
|
|
1527
884
|
if (!run || run.steps.some((s) => s.id === step.id))
|
|
1528
885
|
return;
|
|
1529
886
|
const before = snapshotRun(run);
|
|
@@ -1531,7 +888,7 @@ export class RunStore {
|
|
|
1531
888
|
this.persistence.touch(run, before);
|
|
1532
889
|
}
|
|
1533
890
|
updateStep(runId, stepId, patch) {
|
|
1534
|
-
const run = this.record(runId);
|
|
891
|
+
const run = this.rows.record(runId);
|
|
1535
892
|
const step = run?.steps.find((s) => s.id === stepId);
|
|
1536
893
|
if (!run || !step)
|
|
1537
894
|
return;
|
|
@@ -1543,26 +900,13 @@ export class RunStore {
|
|
|
1543
900
|
}
|
|
1544
901
|
/** A debounced write of the held record `id`: `apply` changes it in place, then `touch` saves it. */
|
|
1545
902
|
change(id, apply) {
|
|
1546
|
-
const run = this.record(id), before = snapshotRun(run);
|
|
903
|
+
const run = this.rows.record(id), before = snapshotRun(run);
|
|
1547
904
|
if (!run)
|
|
1548
905
|
return undefined;
|
|
1549
906
|
apply(run);
|
|
1550
907
|
this.persistence.touch(run, before);
|
|
1551
908
|
return run;
|
|
1552
909
|
}
|
|
1553
|
-
/** Indexed family enumeration, including members not yet flushed to SQLite. */
|
|
1554
|
-
listFamilyRunIds(rootId) {
|
|
1555
|
-
const ids = new Set([rootId, ...(this.db?.listIdsByParent(rootId) ?? [])]);
|
|
1556
|
-
for (const [id, run] of this.held) {
|
|
1557
|
-
if (familyRootOf(run) === rootId)
|
|
1558
|
-
ids.add(id);
|
|
1559
|
-
else if (id !== rootId)
|
|
1560
|
-
ids.delete(id);
|
|
1561
|
-
}
|
|
1562
|
-
for (const id of this.deleted)
|
|
1563
|
-
ids.delete(id);
|
|
1564
|
-
return [...ids];
|
|
1565
|
-
}
|
|
1566
910
|
// ---- internals -----------------------------------------------------------
|
|
1567
911
|
seqs = new Map();
|
|
1568
912
|
/**
|