Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

wip(#6712 PR 4b lane A): live pass engine (pin, kinds, rounds, stamp)

garrytan5740eeb06d59
diff --git a/src/commands/sync.ts b/src/commands/sync.ts
index 7fa73fdb2f..eec0436fbd 100644
--- a/src/commands/sync.ts
+++ b/src/commands/sync.ts
@@ -110,7 +110,12 @@ export interface SyncResult {
* cron operators can disambiguate timeout vs pull-timeout in monitoring; connector item failures / early stop exit 1 (isFailedPartial).
*/
filesImported?: number;
- reason?: 'timeout' | 'pull_timeout' | 'pull_failed' | 'stall_timeout' | 'checkpoint_unavailable' | 'writer_pending' | 'writer_yield' | 'connector_item_failures' | 'connector_partial' | 'drain_lease_held';
+ reason?: 'timeout' | 'pull_timeout' | 'pull_failed' | 'stall_timeout' | 'checkpoint_unavailable' | 'writer_pending' | 'writer_yield' | 'connector_item_failures' | 'connector_partial' | 'drain_lease_held'
+ /** #6712 PR 4b (design v3.2 §9): a live pass's endings. `selection_scoped` rides a converged scoped pass (`synced`). */
+ | 'not_converged' | 'admission_paused' | 'reconcile_host_required' | 'grant_scope' | 'reconcile_read_incomplete' | 'selection_scoped';
+ /** #6712 PR 4b: the live pass that produced this result (non-company managed sources; sync-pass-run.ts). */
+ pass?: { pass_id: string; kind: 'idle' | 'hold_only' | 'full'; round: number; outcome: string; stop_reason: string | null; candidates: number; settled: number;
+ remaining: number; not_converged_streak: number };
/**
* v0.42.x (#1794): cumulative file paths durably banked to the checkpoint
* across THIS run + prior resumed runs. Surfaced on every partial/blocked
diff --git a/src/core/persistence/sync-cursor-key.ts b/src/core/persistence/sync-cursor-key.ts
index 1bfe6c5c23..58d5dba1e9 100644
--- a/src/core/persistence/sync-cursor-key.ts
+++ b/src/core/persistence/sync-cursor-key.ts
@@ -121,6 +121,27 @@ const waitOn = (request: WriteRequest): NonNullable<CursorResolution['waitFor']>
kind: (request.intent as { kind?: string } | null)?.kind ?? null, slug: request.slug, state: request.state, recovery: !!request.recovery });
/** The first request under a cursor key that is not merely queued: running, recovering, publishing or carrying recovery. */
+/**
+ * #6712 PR 4b (design v3.2 §3.5): the live pass retires the incarnation's chosen cursor too (`resolveManagedCursor` keeps it
+ * for a cursor pass): a finished one is deleted; an unfinished one is superseded like the others. Returns the open request
+ * to wait for, or null once it is gone (or a consumer kept it with nothing open: the next pass retries).
+ */
+export async function retireManagedCursor(engine: BrainEngine, input: { sourceId: string; incarnation: string; key: string; runId: string | null; done: boolean }):
+ Promise<NonNullable<CursorResolution['waitFor']> | null> {
+ const fence = (tx: BrainEngine) => assertDrainLease(tx, input.sourceId);
+ if (input.done) {
+ await engine.transaction(async tx => {
+ await fence(tx);
+ await tx.executeRaw("DELETE FROM op_checkpoints WHERE op='managed-sync' AND fingerprint=$1 AND COALESCE(completed_keys->0->>'done','false')='true'", [input.key]);
+ });
+ await clearManagedSyncFailureAfterSuccess(engine, input.key);
+ return null;
+ }
+ const open = await supersede(engine, { key: input.key, done: false, company_receipt_id: null, run_id: input.runId }, { sourceId: input.sourceId, incarnation: input.incarnation, fence });
+ if (open === null) return null;
+ const busy = open === 'kept' ? await busyRequest(engine, input.sourceId, input.key) : open;
+ return busy ? waitOn(busy) : null;
+}
async function busyRequest(engine: BrainEngine, sourceId: string, key: string): Promise<WriteRequest | null> {
const open = await engine.executeRaw<WriteRequest>(`SELECT * FROM persistence_requests WHERE source_id=$1 AND intent->>'cursorKey'=$2
AND (state IN ('queued','running','recovering') OR recovery IS NOT NULL) ORDER BY sequence`, [sourceId, key]);
diff --git a/src/core/persistence/sync-pass-run.ts b/src/core/persistence/sync-pass-run.ts
new file mode 100644
index 0000000000..1d46a9c7b8
--- /dev/null
+++ b/src/core/persistence/sync-pass-run.ts
@@ -0,0 +1,411 @@
+/**
+ * #6712 PR 4b (design v3.2 §2.2, §3.1-§3.5, §8, §9; v3 §3.1-§3.3, §3.5): the live level-triggered pass of a managed,
+ * non-company source. Under the source's drain lease it pins the target (`HEAD`), decides the pass kind, and runs rounds:
+ *
+ * - **Pin.** The pass row, `sync.admission`, `HEAD` and the mode (a detached `HEAD` forces working mode) are read; the
+ * row is written with a new `pass_id` and the lease's token (`writePassRow(..., { pin: true })` after `assertDrainLease`).
+ * An unfinished legacy cursor is superseded first (§3.5): its open requests are waited for, its options seed the episode.
+ * - **Idle** (§2.2): nothing moved since the converged stamp, so only `last_sync_at` is stamped (`up_to_date`).
+ * - **Rounds** (§3.1): round k reads the level (sync-pass.ts `readShadowLevel`) and classifies; `C_k = unsettled − S`.
+ * Candidates are admitted in batches of at most 10,000 (deletes, then imports) through the cursor machinery of
+ * sync-run.ts on an in-memory batch (sync-pass-batch.ts): freeze, screen, waive, group, admit, await and settle; every
+ * settled `(path, level)` joins `S` and is never admitted again in the pass. With nothing unsettled and nothing in
+ * flight, the stamp (sync-prepare.ts `applyPassCheckpoint`) publishes the converged state. Four rounds without
+ * converging stop `resumable` (`not_converged`).
+ */
+import { randomUUID } from 'node:crypto';
+import type { BrainEngine } from '../engine.ts';
+import type { SyncOpts, SyncResult } from '../../commands/sync.ts';
+import type { Cursor, RetryWait } from './sync-run.ts';
+import { syncExcludePatterns, syncGit, type ManagedSyncContext, type SyncSlugCollision } from './sync-discovery.ts';
+import type { SyncAuthority, SyncProcessingOptions } from './sync-authority.ts';
+import type { PassStamp, SyncCursorOptions } from './sync-prepare.ts';
+import { passFingerprint, readPassRow, SYNC_ORIGIN_DELETED_OP, writePassRow, type ManagedSyncPassValue, type PassEpisode,
+ type PassKind, type PassOutcome, type SyncSelection } from './sync-pass-row.ts';
+import { readSyncAdmission, type SyncAdmission } from './sync-admission.ts';
+import { passIdleProbe, readShadowLevel, type ShadowLevel, type ShadowPath } from './sync-pass.ts';
+import { syncPassKind } from './sync-classify.ts';
+import { PASS_BATCH_KEY_PREFIX, setPassBatch, SyncAdmissionPaused, type PassEntry } from './sync-pass-batch.ts';
+import { assertDrainLease, currentDrainLease } from './sync-lease.ts';
+import { digest } from './digest.ts';
+import { readSyncTail } from './sync-tail-record.ts';
+import { holdRescreenDue, readGitHoldRetryPaths, readGitSourceHolds, syncImportParserFor } from './sync-holds.ts';
+import { loadActivePackForEngine } from '../schema-pack/engine-resolution.ts';
+import { clearManagedSyncFailureAfterSuccess } from './sync-failures.ts';
+import { managedCursorKey, resolveManagedCursor, retireManagedCursor } from './sync-cursor-key.ts';
+import { withCoordinatedWrite } from './context.ts';
+import { principalAttribution } from './attribution.ts';
+import { resolveSlugForPath } from '../sync.ts';
+import { syncOriginPath } from './sync-origin.ts';
+import { originMarkerFingerprint } from './sync-markers.ts';
+
+/** Rounds a pass runs before it stops `not_converged` (design §3.1). */
+export const PASS_MAX_ROUNDS = 4;
+/** Candidates per round batch (design §3.1). */
+export const PASS_BATCH_MAX = 10_000;
+/** Waits for in-flight work a pass makes before it stops `writer_pending`, each bounded by the wait budget. */
+const IN_FLIGHT_WAITS = 4;
+
+export interface SyncPassInput {
+ opts: SyncOpts; slice?: { maxPages: number; maxMs: number }; context: ManagedSyncContext; authority: SyncAuthority;
+ processingOptions: SyncProcessingOptions; syncOptions: SyncCursorOptions; assertActive: () => void; signal?: AbortSignal;
+ state: { sourceId?: string; incarnation?: string; remote?: boolean }; runStartedAt: string;
+ /** Drains one round batch through sync-run.ts's cursor loop: the batch back when it drained, or the result it stopped with. */
+ drainBatch: (key: string, batch: Cursor, sliceBase: { started: number; pages: number }) => Promise<SyncResult | Cursor>;
+}
+
+/** The selection a pass syncs (design §6), with the configured one's test: no selection flag differs from the source's configuration. */
+async function passSelection(engine: BrainEngine, opts: SyncOpts, context: ManagedSyncContext, mode: SyncSelection['mode']): Promise<SyncSelection> {
+ const exclude = [...await syncExcludePatterns(engine, opts.exclude)].sort();
+ const includeHidden = [...new Set([...opts.includeHidden ?? [], ...(await engine.getConfig('sync.include_hidden') ?? '').split(/[\n,]/).map(v => v.trim()).filter(Boolean)]
+ .map(v => v.endsWith('/') ? `${v}**` : v))].sort();
+ return { exclude, includeHidden, strategy: opts.strategy ?? (context.source.config?.strategy as string | undefined) ?? 'markdown', srcSubpath: opts.srcSubpath ?? null, mode };
+}
+
+/** The parser fingerprint every import kind records (Markdown, code, image); a change makes the next pass a full round. */
+const parserFingerprint = () => digest(['x.md', 'x.ts', 'x.png'].map(path => syncImportParserFor(path, null)));
+
+/**
+ * Design §3.5: the source's legacy cursors go on the first pass of an incarnation. Every unfinished one is superseded under
+ * the lease (queued requests cancelled, a running, recovering or publishing one waited for); a finished one is deleted.
+ * The newest unfinished cursor's options seed the episode (`inherited.from = 'legacy_cursor'`; a `--full` cursor seeds a
+ * full episode). A remote caller retires nothing: an unfinished cursor leaves it to the brain host.
+ */
+async function retireLegacyCursors(engine: BrainEngine, input: { sourceId: string; incarnation: string; remote: boolean; assertActive: () => void }):
+ Promise<{ wait?: RetryWait; hostRequired?: true; episode?: PassEpisode }> {
+ const resolution = await resolveManagedCursor(engine, { sourceId: input.sourceId, incarnation: input.incarnation, company: null, assertActive: input.assertActive, foldIn: !input.remote });
+ if (resolution.waitFor) return { wait: { ...resolution.waitFor, retry_after_ms: 5000 } };
+ const [row] = await engine.executeRaw<{ header: { runId?: string; done?: boolean; processingOptions?: SyncProcessingOptions; syncOptions?: SyncCursorOptions & { exclude?: string[]; includeHidden?: string[]; strategy?: string; srcSubpath?: string; workingTree?: boolean } } | null }>(
+ "SELECT completed_keys->0 AS header FROM op_checkpoints WHERE op='managed-sync' AND fingerprint=$1", [resolution.key]);
+ const header = row?.header;
+ if (!header) return {};
+ if (input.remote) return header.done ? {} : { hostRequired: true };
+ const busy = await retireManagedCursor(engine, { sourceId: input.sourceId, incarnation: input.incarnation, key: resolution.key, runId: header.runId ?? null, done: header.done === true });
+ input.assertActive();
+ if (busy) return { wait: { ...busy, retry_after_ms: 5000 } };
+ if (header.done) return {};
+ const options = header.processingOptions, sync = header.syncOptions;
+ return { episode: { opened_at: new Date().toISOString(), ...(sync?.full ? { full_id: randomUUID() } : {}),
+ selection: { exclude: [...sync?.exclude ?? []].sort(), includeHidden: [...sync?.includeHidden ?? []].sort(), strategy: sync?.strategy ?? null, srcSubpath: sync?.srcSubpath ?? null,
+ mode: sync?.workingTree ? 'working' : 'committed' },
+ inherited: { noEmbed: options?.noEmbed === true, noExtract: options?.noExtract === true, noSchemaPack: options?.noSchemaPack === true, from: 'legacy_cursor' } } };
+}
+
+/** The path forms of one level path: root-relative (`SyncEntry.path`) and the recorded origin (`sourcePath`). */
+function entryPaths(level: ShadowLevel, gitPath: string): { path: string; sourcePath: string } {
+ const path = level.scope ? gitPath.slice(level.scope.length + 1) : gitPath;
+ return { path, sourcePath: level.slugMode === 'source-root' && level.scope ? path : gitPath };
+}
+
+/**
+ * The round's candidates as batch entries, with the identities discovery resolves (`discoverManagedSync`): a live page
+ * at the origin is the import's page; a new origin takes the slug its path names, and the page holding that slug is
+ * taken over when its own origin leaves in this pass (`renameFrom`, its deletion retired), a collision when its origin
+ * stays; one new origin whose bytes equal a page leaving at another origin is that page's Git rename (`renameFrom`, the
+ * page moves to the new slug). Deletes first, then imports.
+ */
+async function batchEntries(engine: BrainEngine, level: ShadowLevel, deletes: ShadowPath[], imports: ShadowPath[]):
+ Promise<{ entries: PassEntry[]; collisions: SyncSlugCollision[]; skipped: ShadowPath[] }> {
+ const working = (entry: ShadowPath) => level.mode === 'working' && entry.state.level !== entry.state.tree;
+ const deleteEntries = new Map<string, PassEntry>(deletes.map(entry => [entry.path, { ...entryPaths(level, entry.path), action: 'delete' as const, working: level.mode === 'working',
+ slug: entry.slug!, pageId: entry.pageId, revision: entry.revision, level: null }]));
+ const leaving = new Map(deletes.map(entry => [entry.path, entry]));
+ const fresh = imports.filter(entry => !entry.state.page);
+ const slugOf = new Map(fresh.map(entry => [entry.path, resolveSlugForPath(entryPaths(level, entry.path).sourcePath)]));
+ const slugs = [...new Set([...slugOf.values()].filter(Boolean))];
+ const holders = new Map((slugs.length ? await engine.executeRaw<{ id: number | string; slug: string; source_path: string | null; revision: string; deleted: boolean }>(
+ 'SELECT id, slug, source_path, knowledge_revision::text AS revision, deleted_at IS NOT NULL AS deleted FROM pages WHERE source_id=$1 AND slug=ANY($2::text[])', [level.sourceId, slugs]) : [])
+ .map(page => [page.slug, page]));
+ const gitPathOfOrigin = (origin: string) => level.slugMode === 'source-root' && level.scope ? `${level.scope}/${syncOriginPath(origin)}` : syncOriginPath(origin);
+ const entries: PassEntry[] = [], collisions: SyncSlugCollision[] = [], skipped: ShadowPath[] = [];
+ const claims = new Map<string, ShadowPath[]>();
+ for (const entry of imports) {
+ const paths = entryPaths(level, entry.path);
+ const base = { ...paths, action: 'import' as const, working: working(entry), level: entry.state.level, ...(entry.result.cls === 'drift' && entry.result.side && entry.result.side !== 'deleted' ? { drift: entry.result.side } : {}) };
+ if (entry.state.page) { entries.push({ ...base, slug: entry.slug!, pageId: entry.pageId, revision: entry.revision }); continue; }
+ const slug = slugOf.get(entry.path);
+ if (!slug) { skipped.push(entry); continue; }
+ claims.set(slug, [...claims.get(slug) ?? [], entry]);
+ }
+ const renamed = new Set<string>();
+ for (const [slug, claimants] of claims) {
+ const holder = holders.get(slug);
+ const holderPath = holder?.source_path ? gitPathOfOrigin(holder.source_path) : null;
+ const kept = holder && !holder.deleted && holderPath !== null && !leaving.has(holderPath) ? holderPath : null;
+ const winner = kept === null ? claimants.find(entry => /\.mdx?$/i.test(entry.path) && entryPaths(level, entry.path).sourcePath.replace(/\.mdx?$/i, '') === slug) ?? claimants[0]! : null;
+ const losers = claimants.filter(entry => entry !== winner);
+ if (losers.length) collisions.push({ slug, kept: kept ?? entryPaths(level, winner!.path).sourcePath, skipped: losers.map(entry => entryPaths(level, entry.path).sourcePath) });
+ if (!winner) continue;
+ const paths = entryPaths(level, winner.path);
+ const base = { ...paths, action: 'import' as const, working: working(winner), level: winner.state.level, slug };
+ if (holder) {
+ entries.push({ ...base, pageId: Number(holder.id), revision: holder.revision,
+ ...(holder.source_path !== null ? { renameFrom: { sourcePath: holder.source_path, slug, pageId: Number(holder.id), revision: holder.revision } } : {}) });
+ if (holderPath !== null && deleteEntries.delete(holderPath)) renamed.add(holderPath);
+ continue;
+ }
+ const moved = [...leaving.values()].filter(gone => deleteEntries.has(gone.path) && !renamed.has(gone.path) && gone.state.record?.blobOids.includes(winner.state.level ?? ''));
+ const twins = fresh.filter(other => other.state.level === winner.state.level);
+ if (moved.length === 1 && twins.length === 1) {
+ const gone = moved[0]!;
+ const origin = entryPaths(level, gone.path).sourcePath;
+ entries.push({ ...base, pageId: null, revision: null, renameFrom: { sourcePath: origin, slug: gone.slug!, pageId: gone.pageId!, revision: gone.revision! } });
+ deleteEntries.delete(gone.path); renamed.add(gone.path);
+ continue;
+ }
+ entries.push({ ...base, pageId: null, revision: null });
+ }
+ return { entries: [...deleteEntries.values(), ...entries.sort((a, b) => a.path.localeCompare(b.path))], collisions, skipped };
+}
+
+/** Waits (bounded) until no open request or Git effect of the worktree names one of the in-flight pages or origins. */
+async function awaitInFlight(engine: BrainEngine, level: ShadowLevel, inFlight: ShadowPath[], budgetMs: number, assertActive: () => void): Promise<void> {
+ const pageIds = inFlight.flatMap(entry => entry.pageId === null ? [] : [entry.pageId]);
+ const origins = inFlight.map(entry => entryPaths(level, entry.path).sourcePath);
+ const until = Date.now() + budgetMs;
+ while (Date.now() < until) {
+ const [open] = await engine.executeRaw(`SELECT 1 FROM persistence_requests r WHERE r.source_id=$1 AND (r.state IN ('queued','running','recovering')
+ OR EXISTS (SELECT 1 FROM persistence_effects e WHERE e.request_id=r.id AND e.kind='git' AND e.state IN ('queued','running')))
+ AND (r.page_id=ANY($2::int[]) OR r.intent->>'sourcePath'=ANY($3::text[])) LIMIT 1`, [level.sourceId, pageIds, origins]);
+ assertActive();
+ if (!open) return;
+ await new Promise(resolve => setTimeout(resolve, 100));
+ }
+}
+
+/** Design §2.7: the incarnation's first pass writes a `legacy` marker for each page-less path whose level is its baseline blob. */
+async function writeLegacyMarkers(engine: BrainEngine, level: ShadowLevel): Promise<number> {
+ const rows = level.paths.filter(entry => entry.legacyPageless && entry.state.level).map(entry => ({
+ fingerprint: originMarkerFingerprint(level.sourceId, level.incarnation, entryPaths(level, entry.path).sourcePath),
+ value: { source_id: level.sourceId, incarnation: level.incarnation, kind: 'legacy', blob_oids: [entry.state.level], at: new Date().toISOString() } }));
+ if (!rows.length) return 0;
+ await engine.transaction(async tx => {
+ await assertDrainLease(tx, level.sourceId);
+ await tx.executeRaw(`INSERT INTO op_checkpoints(op,fingerprint,completed_keys,updated_at)
+ SELECT $1, row->>'fingerprint', jsonb_build_array(row->'value'), now() FROM jsonb_array_elements($2::text::jsonb) AS row ON CONFLICT (op,fingerprint) DO NOTHING`,
+ [SYNC_ORIGIN_DELETED_OP, JSON.stringify(rows)]);
+ });
+ return rows.length;
+}
+
+/** The pass of one managed, non-company source (module header). */
+export async function runSyncPass(engine: BrainEngine, input: SyncPassInput): Promise<SyncResult> {
+ const { opts, context, authority, assertActive } = input;
+ const { sourceId, incarnation } = context;
+ const remote = authority.writer.remote;
+ const lease = currentDrainLease(sourceId);
+ if (!lease) throw new Error(`The live sync pass of ${sourceId} runs only under the source's drain lease.`);
+ const started = Date.now(), startedAt = new Date(started).toISOString();
+ const handover = await retireLegacyCursors(engine, { sourceId, incarnation, remote, assertActive });
+ const empty = (status: SyncResult['status'], reason?: SyncResult['reason']): SyncResult => ({ status, fromCommit: remote ? null : context.source.last_commit, toCommit: remote ? '' : context.source.last_commit ?? '',
+ added: 0, modified: 0, deleted: 0, renamed: 0, chunksCreated: 0, embedded: 0, pagesAffected: [], filesImported: 0, bankedFiles: 0, ...(reason ? { reason } : {}) });
+ if (handover.wait) return { ...empty('partial', 'writer_pending'), ...(remote ? {} : { retryWait: handover.wait }) };
+ if (handover.hostRequired) return empty('blocked_by_failures', 'reconcile_host_required');
+
+ // Pin (design §2.2): the row, sync.admission, HEAD and the mode.
+ const [prior, admission, configuredWorking] = await Promise.all([readPassRow(engine, sourceId, incarnation), readSyncAdmission(engine), engine.getConfig('sync.include_working_tree')]);
+ assertActive();
+ const head = syncGit(context.gitRoot, ['rev-parse', 'HEAD']).trim();
+ const detached = syncGit(context.gitRoot, ['rev-parse', '--abbrev-ref', 'HEAD']).trim() === 'HEAD';
+ const mode: SyncSelection['mode'] = detached || (opts.workingTree ?? configuredWorking === 'true') ? 'working' : 'committed';
+ const selection = await passSelection(engine, opts, context, mode);
+ const configured = !remote && !opts.exclude?.length && !opts.includeHidden?.length && !opts.strategy && opts.srcSubpath === undefined
+ && (detached || opts.workingTree === undefined || opts.workingTree === (configuredWorking === 'true'));
+ const selectionFp = digest(selection), parserFp = parserFingerprint();
+ const pack = (await loadActivePackForEngine(engine, { remote: false, sourceId }).catch(() => null))?.identity ?? null;
+ const obligations: ManagedSyncPassValue['obligations'] = prior ? structuredClone(prior.value.obligations)
+ : { baseline_commit: context.source.last_commit, mismatch: {}, not_converged_streak: 0, faults: {} };
+ obligations.mismatch ??= {}; obligations.faults ??= {}; obligations.not_converged_streak ??= 0;
+ if (handover.episode && !obligations.episode) obligations.episode = handover.episode;
+ const passId = randomUUID();
+ const requested = input.processingOptions;
+ if (opts.full && !obligations.episode?.full_id) obligations.episode = { opened_at: startedAt, full_id: passId, selection,
+ inherited: { noEmbed: requested.noEmbed, noExtract: requested.noExtract, noSchemaPack: requested.noSchemaPack, from: obligations.episode?.inherited.from ?? 'flag' } };
+ else if (!obligations.episode && (requested.noEmbed || requested.noExtract || requested.noSchemaPack)) obligations.episode = { opened_at: startedAt, selection,
+ inherited: { noEmbed: requested.noEmbed, noExtract: requested.noExtract, noSchemaPack: requested.noSchemaPack, from: 'flag' } };
+ const episode = obligations.episode;
+ // A flagless pass keeps the open episode's opt-outs (#6712 PR 1's inherited_options contract).
+ const processingOptions: SyncProcessingOptions = { noEmbed: requested.noEmbed || !!episode?.inherited.noEmbed, noExtract: requested.noExtract || !!episode?.inherited.noExtract,
+ noSchemaPack: requested.noSchemaPack || !!episode?.inherited.noSchemaPack };
+ const inherited = (['noEmbed', 'noExtract', 'noSchemaPack'] as const).filter(key => processingOptions[key] && !requested[key]);
+
+ // The kind (design §2.2), from the real converged state.
+ const converged = obligations.converged;
+ const [probe, tail, retry, sourceHolds] = await Promise.all([
+ converged ? passIdleProbe(engine, { sourceId, incarnation, worktreeId: context.binding.worktree_id, requestHwm: converged.request_hwm, openAtStamp: converged.open_at_stamp ?? [] }) : null,
+ readSyncTail(engine, sourceId), readGitHoldRetryPaths(engine, sourceId, incarnation), readGitSourceHolds(engine, { sourceIds: [sourceId] })]);
+ assertActive();
+ const holds = (sourceHolds[0]?.holds ?? []).filter(hold => hold.incarnation === incarnation);
+ const kindOf = syncPassKind({ converged: converged ? { target: converged.target, parserFp: converged.parser_fp, pack: converged.pack, selectionFp: converged.selection_fp,
+ passesSinceFull: converged.passes_since_full, lastFullAt: Date.parse(converged.last_full_at) } : null,
+ head, parserFp, pack, selectionFp, now: started, openEpisode: !!episode, owedTail: tail !== null, retryRow: retry.length > 0,
+ notConvergedStreak: obligations.not_converged_streak, mismatchStreak: Object.keys(obligations.mismatch).length,
+ requestNewer: probe?.newer === true, openAtStampFinished: !!converged && (converged.open_at_stamp === null || (probe?.stillOpen ?? 0) < converged.open_at_stamp.length),
+ // Lane W ships the working-mode stat cache; until then a working pass always reads the level.
+ workingChange: mode === 'working', rescreenDueHold: holds.some(hold => holdRescreenDue(hold, retry.includes(hold.path))), full: opts.full === true });
+ const kind: PassKind = admission === 'classify_only' && kindOf.kind === 'idle' ? 'idle' : kindOf.kind;
+ const value: ManagedSyncPassValue = { source_id: sourceId, incarnation, lease_token: lease.token, obligations,
+ telemetry: { pass_id: passId, target: head, started_at: startedAt, ended_at: null, kind, round: 0, outcome: 'open', stop_reason: null, candidates: 0, settled: 0, remaining: 0,
+ counts: {}, duration_ms: null, timings_ms: {} } };
+ const bootstrap = !prior;
+
+ if (kind === 'idle') {
+ // Master's up_to_date (a complete check that found nothing): only last_sync_at, plus the pass row's count.
+ const at = new Date().toISOString();
+ value.obligations.converged = { ...converged!, passes_since_full: converged!.passes_since_full + 1 };
+ Object.assign(value.obligations, { stuck_since: null });
+ value.telemetry = { ...value.telemetry, outcome: 'up_to_date', ended_at: at, duration_ms: Date.now() - started };
+ await engine.transaction(tx => withCoordinatedWrite(tx, [sourceId], async () => {
+ assertActive();
+ await assertDrainLease(tx, sourceId);
+ await tx.executeRaw('UPDATE sources SET last_sync_at=now() WHERE id=$1 AND incarnation=$2::uuid', [sourceId, incarnation]);
+ await writePassRow(tx, value, { pin: true });
+ }, principalAttribution(authority.writer.principal)));
+ await clearPassFailures(engine, sourceId, incarnation);
+ return { ...empty('up_to_date'), toCommit: remote ? '' : head, ...(inherited.length ? { inherited_options: inherited } : {}) };
+ }
+ await engine.transaction(async tx => { await assertDrainLease(tx, sourceId); await writePassRow(tx, value, { pin: true }); });
+ assertActive();
+
+ const counts = { added: 0, modified: 0, deleted: 0, renamed: 0, chunks: 0, waivedImports: 0, waivedDeletes: 0, held: 0, pausedDeletes: 0, collisions: 0 };
+ const collisions: SyncSlugCollision[] = [];
+ const S = new Map<string, string>();
+ const settledKey = (path: string, level: string | null) => `${path}\0${level ?? ''}`;
+ const retryConsumed = new Set<string>(retry);
+ const sliceBase = { started: performance.now(), pages: 0 };
+ let batchNo = 0, waits = 0, candidatesFirst = 0;
+ let lastMismatch: ShadowPath[] = [];
+
+ const save = async (outcome: PassOutcome, stopReason: string | null) => {
+ const ended = outcome !== 'open';
+ value.telemetry = { ...value.telemetry, outcome, stop_reason: stopReason, settled: S.size, counts: { ...counts },
+ ...(ended ? { ended_at: new Date().toISOString(), duration_ms: Date.now() - started } : {}) };
+ if (outcome === 'blocked' || stopReason === 'not_converged') Object.assign(value.obligations, { stuck_since: (value.obligations as { stuck_since?: string | null }).stuck_since ?? new Date().toISOString() });
+ await engine.transaction(async tx => { await assertDrainLease(tx, sourceId); await writePassRow(tx, value); });
+ };
+ const result = (status: SyncResult['status'], reason?: SyncResult['reason']): SyncResult => ({ status, fromCommit: remote ? null : context.source.last_commit, toCommit: remote ? '' : head,
+ added: counts.added, modified: counts.modified, deleted: counts.deleted, renamed: counts.renamed, chunksCreated: counts.chunks, embedded: 0, pagesAffected: [],
+ filesImported: sliceBase.pages, bankedFiles: sliceBase.pages, waived: { imports: counts.waivedImports, deletes: counts.waivedDeletes }, prescreened: 0,
+ ...(collisions.length ? { slugCollisions: collisions } : {}), ...(inherited.length ? { inherited_options: inherited } : {}), ...(reason ? { reason } : {}),
+ pass: { pass_id: passId, kind, round: value.telemetry.round, outcome: value.telemetry.outcome, stop_reason: value.telemetry.stop_reason, candidates: candidatesFirst,
+ settled: S.size, remaining: value.telemetry.remaining, not_converged_streak: value.obligations.not_converged_streak } });
+ /** The batch cursor sync-run.ts drains: the discovery shape with no cursor row behind it. */
+ const batchCursor = (entries: PassEntry[], stamp?: PassStamp): Cursor => ({ binding: { ...context.binding, owner_epoch: String(context.binding.owner_epoch), topology_generation: String(context.binding.topology_generation) },
+ root: context.root, gitRoot: context.gitRoot, sourceId, incarnation, from: context.source.last_commit, target: head, entries, slugMode: levelSlugMode, discoveredAt: startedAt,
+ runId: passId, passId, index: 0, authority, processingOptions, syncOptions: input.syncOptions, counts: { added: 0, modified: 0, deleted: 0, chunks: 0 },
+ failureKey: passFingerprint(sourceId, incarnation), ...(episode?.full_id ? { fullEpisode: episode.full_id } : {}), ...(stamp ? { stamp } : {}) });
+ let levelSlugMode: Cursor['slugMode'] = 'git-root';
+ /** Drains one batch; a stop is the pass's ending (the batch's counts are folded in either way). */
+ const drain = async (entries: PassEntry[], stamp?: PassStamp): Promise<SyncResult | null> => {
+ const key = `${PASS_BATCH_KEY_PREFIX}${passId}:${batchNo++}`;
+ const batch = batchCursor(entries, stamp);
+ setPassBatch(key, batch, JSON.stringify({ runId: passId, index: 0 }));
+ let drained: SyncResult | Cursor;
+ try { drained = await input.drainBatch(key, batch, sliceBase); }
+ finally { setPassBatch(key, null); }
+ if ('entries' in drained) {
+ const c = drained.counts;
+ Object.assign(counts, { added: counts.added + c.added, modified: counts.modified + c.modified, deleted: counts.deleted + c.deleted, renamed: counts.renamed + (c.renamed ?? 0),
+ chunks: counts.chunks + c.chunks, waivedImports: counts.waivedImports + (c.waived?.imports ?? 0), waivedDeletes: counts.waivedDeletes + (c.waived?.deletes ?? 0), held: counts.held + (c.held ?? 0) });
+ sliceBase.pages += drained.index;
+ return null;
+ }
+ return drained;
+ };
+ const stop = async (drained: SyncResult): Promise<SyncResult> => {
+ const outcome: PassOutcome = drained.status === 'blocked_by_failures' ? 'blocked' : 'resumable';
+ await save(outcome, drained.reason ?? (outcome === 'blocked' ? 'blocked_by_failures' : null));
+ const { runId: _run, managedCursor: _cursor, ...rest } = drained;
+ return { ...rest, ...result(drained.status, drained.reason) };
+ };
+
+ try {
+ for (let round = 1; round <= PASS_MAX_ROUNDS; round++) {
+ assertActive();
+ value.telemetry.round = round;
+ const level = await readShadowLevel(engine, { sourceId, context, workingTree: mode === 'working',
+ pass: { episodeFullId: episode?.full_id ?? null, retryConsumed: round > 1 ? retryConsumed : undefined, exclude: opts.exclude, includeHidden: opts.includeHidden, strategy: opts.strategy, bootstrap } });
+ assertActive();
+ if (!level) throw new Error(`Source ${sourceId} became a company-brain source during its sync pass.`);
+ levelSlugMode = level.slugMode;
+ value.telemetry.timings_ms = level.timings_ms;
+ if (bootstrap && round === 1) await writeLegacyMarkers(engine, level);
+ const deletes: ShadowPath[] = [], imports: ShadowPath[] = [], inFlight: ShadowPath[] = [], mismatch: ShadowPath[] = [], owed: ShadowPath[] = [];
+ lastMismatch = mismatch;
+ for (const entry of level.paths) {
+ const r = entry.result;
+ if (r.settled) continue;
+ if (r.inFlight) { inFlight.push(entry); continue; }
+ if (S.has(settledKey(entry.path, entry.state.level))) { mismatch.push(entry); continue; }
+ if (r.action === 'delete') {
+ if (admission !== 'normal') { S.set(settledKey(entry.path, entry.state.level), 'paused_deletes'); counts.pausedDeletes++; continue; }
+ deletes.push(entry);
+ } else if (r.action === 'import' || r.action === 'reparse' || r.cls === 'drift' || r.cls === 'dirty') imports.push(entry);
+ else owed.push(entry);
+ }
+ const candidates = deletes.length + imports.length;
+ if (round === 1) candidatesFirst = candidates + inFlight.length + owed.length;
+ value.telemetry.candidates = candidatesFirst;
+ value.telemetry.remaining = candidates + inFlight.length + owed.length + mismatch.length;
+ if (admission === 'classify_only') { await save('resumable', 'admission_paused'); return result('partial', 'admission_paused'); }
+ if (!candidates && inFlight.length && waits < IN_FLIGHT_WAITS) {
+ waits++; round--;
+ await awaitInFlight(engine, level, inFlight, opts.drainStartedAt ? 30_000 : 5_000, assertActive);
+ continue;
+ }
+ if (!candidates && inFlight.length) { await save('resumable', 'writer_pending'); return result('partial', 'writer_pending'); }
+ if (!candidates) {
+ if (mismatch.length || owed.length) break;
+ if (remote) { await save('blocked', 'reconcile_host_required'); return result('blocked_by_failures', 'reconcile_host_required'); }
+ const closesEpisode = !!episode && configured;
+ const stamp: PassStamp = { leaseToken: lease.token, incarnation, configured, stampChunker: configured && (context.source.last_commit === null || (closesEpisode && !!episode?.full_id)),
+ closesEpisode, retryTaken: [...retry], releasedHolds: [], selectionFp, parserFp, pack, full: kind === 'full',
+ changed: counts.added + counts.modified + counts.deleted + counts.renamed, extract: !processingOptions.noExtract };
+ await save('open', null);
+ const stopped = await drain([], stamp);
+ if (stopped) return stop(stopped);
+ await clearPassFailures(engine, sourceId, incarnation);
+ return result(configured && context.source.last_commit === null ? 'first_sync' : 'synced', configured ? undefined : 'selection_scoped');
+ }
+ const built = await batchEntries(engine, level, deletes, imports);
+ collisions.push(...built.collisions);
+ for (const entry of [...built.skipped, ...built.collisions.flatMap(c => c.skipped.map(sourcePath => imports.find(i => entryPaths(level, i.path).sourcePath === sourcePath)!))].filter(Boolean)) {
+ S.set(settledKey(entry.path, entry.state.level), 'collision'); counts.collisions++;
+ }
+ for (let i = 0; i < built.entries.length; i += PASS_BATCH_MAX) {
+ const chunk = built.entries.slice(i, i + PASS_BATCH_MAX);
+ const stopped = await drain(chunk);
+ if (stopped) return stop(stopped);
+ value.telemetry.remaining = Math.max(0, built.entries.length - i - chunk.length) + inFlight.length + owed.length + mismatch.length;
+ await save('open', null);
+ }
+ // Every candidate of the round settled: committed, waived, held, deferred to a hold, a collision, or (a rename's
+ // deletion) carried by its import.
+ for (const entry of [...deletes, ...imports]) S.set(settledKey(entry.path, entry.state.level), 'settled');
+ lastMismatch = [];
+ }
+ // Not converged (design §3.1): four rounds, or a settle mismatch or a path this release cannot settle.
+ // A path settled in this pass that does not classify settled is a settle mismatch: counted per (path, level) across
+ // passes; lane B turns a streak of SETTLE_MISMATCH_STREAK into a settle_mismatch hold.
+ const before = value.obligations.mismatch;
+ value.obligations.mismatch = Object.fromEntries(lastMismatch.map(entry => {
+ const seen = before[entry.path];
+ return [entry.path, { level: entry.state.level, streak: seen && seen.level === entry.state.level ? seen.streak + 1 : 1 }];
+ }));
+ value.obligations.not_converged_streak = (value.obligations.not_converged_streak ?? 0) + 1;
+ await save('resumable', 'not_converged');
+ return result('partial', 'not_converged');
+ } catch (error) {
+ if (error instanceof SyncAdmissionPaused) { await save('resumable', 'admission_paused').catch(() => undefined); return result('partial', 'admission_paused'); }
+ if (input.signal?.aborted && error instanceof Error && error.name === 'AbortError') { await save('resumable', 'timeout').catch(() => undefined); return result('partial', 'timeout'); }
+ throw error;
+ }
+}
+
+/** A converged or idle pass clears what earlier stops recorded: the pass row's failure key and the incarnation's cursor key. */
+async function clearPassFailures(engine: BrainEngine, sourceId: string, incarnation: string): Promise<void> {
+ await clearManagedSyncFailureAfterSuccess(engine, passFingerprint(sourceId, incarnation));
+ await clearManagedSyncFailureAfterSuccess(engine, managedCursorKey(incarnation, null));
+}
+
+export type { SyncAdmission };
diff --git a/src/core/persistence/sync-pass.ts b/src/core/persistence/sync-pass.ts
index 864bfea089..70ba8a08d4 100644
--- a/src/core/persistence/sync-pass.ts
+++ b/src/core/persistence/sync-pass.ts
@@ -50,6 +50,11 @@ export interface PassLevelInput {
exclude?: readonly string[];
includeHidden?: readonly string[];
strategy?: string;
+ /**
+ * The incarnation's first pass (no pass row yet): a page-less path whose level equals its baseline blob reads a virtual
+ * `legacy` marker, which the pass then writes (design §2.7). After it, only stored markers count.
+ */
+ bootstrap?: boolean;
}
/** PGLite's keyset page size (design v3.2 §2.1); Postgres reads `I⋈P` in one statement of its snapshot. */
const PGLITE_KEYSET_ROWS = 2_000;
@@ -352,7 +357,7 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: st
const marker = markers.get(originOf(path)) ?? null;
let virtualMarker: SyncPathState['marker'] = null;
let legacyPageless: true | undefined;
- if (!row && !marker && level !== null && level === await baselineOf(path)) { virtualMarker = { kind: 'legacy', blobOids: [level] }; legacyPageless = true; }
+ if (!row && !marker && level !== null && (!input.pass || input.pass.bootstrap) && level === await baselineOf(path)) { virtualMarker = { kind: 'legacy', blobOids: [level] }; legacyPageless = true; }
const needsBaseline = row && !row.deleted && (record?.revision == null) && level !== null && !recordBlobOids.includes(level);
const state: SyncPathState = {
mode, tree: committed?.oid ?? null, level, present: presentNow, dirty, diskOid: probe?.rawOid ?? null,
@@ -469,6 +474,65 @@ export async function idleTickProbe(engine: BrainEngine, input: { sourceId: stri
timings.rev_parse = t();
// The pass row, the request probe above its watermark and the open-set probe are one statement (one round trip).
t = timer();
- const [probe] = await engine.executeRaw<{ pass_row: Record<string, unknown> | null; newer: boolean; pending: boolean }>(`WITH pass AS (
+ const probe = await passIdleProbe(engine, { sourceId: context.sourceId, incarnation: context.incarnation, worktreeId: context.binding.worktree_id,
+ requestHwm: input.assumeConverged?.requestHwm ?? null, openAtStamp: [] });
+ timings.probes = t();
+ const row = probe.passRow;
+ const stored = (row?.obligations as { converged?: { target: string; request_hwm: string } } | undefined)?.converged;
+ const converged = input.assumeConverged ?? (stored ? { target: String(stored.target), requestHwm: Number(stored.request_hwm) } : null);
+ const newer = probe.newer, pending = probe.pending;
+ const kind = syncPassKind({ converged: converged ? { target: converged.target, parserFp: '', pack: null, selectionFp: '', passesSinceFull: 0, lastFullAt: Date.now() } : null,
+ head: head ?? '', parserFp: '', pack: null, selectionFp: '', now: Date.now(), openEpisode: false, owedTail: false, retryRow: false, notConvergedStreak: 0, mismatchStreak: 0,
+ requestNewer: newer, openAtStampFinished: pending && !input.assumeConverged, workingChange: false, rescreenDueHold: false, full: false });
+ timings.total = total();
+ return { idle: kind.kind === 'idle', reasons: kind.blockers, timings_ms: timings, rows: (newer ? 1 : 0) + (pending ? 1 : 0) + (row ? 1 : 0) };
+}
+
+/**
+ * #6712 PR 4b (design v3.2 §2.2): the idle tick's database reads in one statement: the pass row, whether a request of the
+ * worktree is newer than the converged watermark (a range on the unique `sequence` index; `requestHwm` overrides the
+ * row's), whether any request is open (`persistence_requests_pending`), and how many of the ids recorded in
+ * `converged.open_at_stamp` are still open (`r:<request>` requests, `e:<effect>` Git effects).
+ */
+export async function passIdleProbe(engine: Pick<BrainEngine, 'executeRaw'>, input: { sourceId: string; incarnation: string; worktreeId: string;
+ requestHwm: string | number | null; openAtStamp: readonly string[] }): Promise<{ passRow: Record<string, unknown> | null; newer: boolean; pending: boolean; stillOpen: number }> {
+ const [probe] = await engine.executeRaw<{ pass_row: Record<string, unknown> | null; newer: boolean; pending: boolean; still_open: number | string }>(`WITH pass AS (
SELECT completed_keys->0 AS value FROM op_checkpoints WHERE op=$1 AND fingerprint=$2)
- SELECT (SELECT value FROM pass) AS pass_row,
\ No newline at end of file
+ SELECT (SELECT value FROM pass) AS pass_row,
+ EXISTS (SELECT 1 FROM persistence_requests WHERE sequence > COALESCE($3::bigint, ((SELECT value FROM pass)->'obligations'->'converged'->>'request_hwm')::bigint, 0) AND worktree_id = $4) AS newer,
+ EXISTS (SELECT 1 FROM persistence_requests WHERE worktree_id = $4 AND state IN ('queued','running','recovering')) AS pending,
+ (SELECT count(*) FROM persistence_requests WHERE worktree_id = $4 AND state IN ('queued','running','recovering') AND 'r:'||request_id::text = ANY($5::text[]))
+ + (SELECT count(*) FROM persistence_effects e JOIN persistence_requests r ON r.id = e.request_id
+ WHERE r.worktree_id = $4 AND e.kind = 'git' AND e.state IN ('queued','running') AND 'e:'||e.id::text = ANY($5::text[])) AS still_open`,
+ [MANAGED_SYNC_PASS_OP, `${input.sourceId}:${input.incarnation}`, input.requestHwm, input.worktreeId, [...input.openAtStamp]]);
+ return { passRow: probe?.pass_row ?? null, newer: probe?.newer === true, pending: probe?.pending === true, stillOpen: Number(probe?.still_open ?? 0) };
+}
+
+/**
+ * `gbrain sync --dry-run --json`'s `convergence` for one source: the receipt with the read-only screen, undefined
+ * on a brain that is not managed or for a company-brain source; a read that cannot run reports why instead of failing
+ * the dry run.
+ */
+export async function dryRunConvergence(engine: BrainEngine, input: { sourceId: string; full: boolean; workingTree?: boolean; redactPaths: boolean;
+ noEmbed: boolean; noExtract: boolean; noSchemaPack: boolean }): Promise<ConvergenceReceipt | { dry_run: true; status: 'unavailable'; code: string; message: string } | undefined> {
+ if (!await readManagedMode(engine)) return undefined;
+ try {
+ const context = await resolveManagedSyncContext(engine, { sourceId: input.sourceId, noPull: true, dryRun: true });
+ return await shadowConvergence(engine, { sourceId: input.sourceId, full: input.full, workingTree: input.workingTree, redactPaths: input.redactPaths, context,
+ screen: readOnlySameContentScreen(engine, context, { noEmbed: input.noEmbed, noExtract: input.noExtract, noSchemaPack: input.noSchemaPack }) }) ?? undefined;
+ } catch (error) {
+ const code = (error as { code?: unknown }).code;
+ return { dry_run: true, status: 'unavailable', code: typeof code === 'string' ? code : 'convergence_read_failed', message: error instanceof Error ? error.message : String(error) };
+ }
+}
+
+/** \`sync status --detail\`'s \`convergence\`: the receipt without the screen (status never prepares an import). */
+export async function statusConvergence(engine: BrainEngine, sourceId: string): Promise<ConvergenceReceipt | { dry_run: true; status: 'unavailable'; code: string; message: string } | null> {
+ if (!await readManagedMode(engine)) return null;
+ try {
+ return await shadowConvergence(engine, { sourceId, screen: false });
+ } catch (error) {
+ const code = (error as { code?: unknown }).code;
+ return { dry_run: true, status: 'unavailable', code: typeof code === 'string' ? code : 'convergence_read_failed', message: error instanceof Error ? error.message : String(error) };
+ }
+}