Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

perf(sync): #6712 PR 4b a 600k level read stops stalling and allocates less per path

garrytan215204089b9b
diff --git a/src/core/persistence/sync-holds.ts b/src/core/persistence/sync-holds.ts
index 630536ef13..6304b83171 100644
--- a/src/core/persistence/sync-holds.ts
+++ b/src/core/persistence/sync-holds.ts
@@ -963,13 +963,19 @@ export function syncImportParser(schemaPack: string | null): SyncImportParser {
return { parser_version: SYNC_IMPORT_PARSER_VERSION, markdown_chunker: MARKDOWN_CHUNKER_VERSION, schema_pack: schemaPack };
}
+/** The importer a page at `path` records (`syncImportParserFor`): every path of one kind records the same parser. */
+export function syncImportKind(path: string): 'image' | 'code' | 'markdown' {
+ return isImageFilePath(path) ? 'image' : isCodeFilePath(path) ? 'code' : 'markdown';
+}
+
/**
* #6712 PR 1: the parser fingerprint an import of `path` records and the discovery prescreen expects: a code page names
* the code chunker and its grammars (`chunkerStamp`), an image the image importer, a Markdown page `syncImportParser`.
*/
export function syncImportParserFor(path: string, schemaPack: string | null): SyncImportParser {
- if (isImageFilePath(path)) return { parser_version: 'image=1', markdown_chunker: MARKDOWN_CHUNKER_VERSION, schema_pack: null };
- if (isCodeFilePath(path)) return { parser_version: `code=1;chunker=${chunkerStamp()}`, markdown_chunker: MARKDOWN_CHUNKER_VERSION, schema_pack: null };
+ const kind = syncImportKind(path);
+ if (kind === 'image') return { parser_version: 'image=1', markdown_chunker: MARKDOWN_CHUNKER_VERSION, schema_pack: null };
+ if (kind === 'code') return { parser_version: `code=1;chunker=${chunkerStamp()}`, markdown_chunker: MARKDOWN_CHUNKER_VERSION, schema_pack: null };
return syncImportParser(schemaPack);
}
diff --git a/src/core/persistence/sync-origin.ts b/src/core/persistence/sync-origin.ts
index e17181f6dc..13cc413631 100644
--- a/src/core/persistence/sync-origin.ts
+++ b/src/core/persistence/sync-origin.ts
@@ -43,11 +43,14 @@ export function sameSyncOrigin(stored: string, origin: string, context?: SyncOri
return recorded === expected || !nested;
}
+/** An empty, `.` or `..` segment of a `/`-separated path (a per-call test: sync validates every origin of a large level, so no split). */
+const UNCONFINED_SEGMENT = /(?:^|\/)\.{0,2}(?:\/|$)/;
+
export function syncOriginPath(path: string, platform = process.platform): string {
const normalized = platform === 'win32' ? path.replaceAll('\\', '/') : path;
if (!normalized || normalized.includes('\0') || normalized.startsWith('/') ||
platform === 'win32' && normalized.split('/').some(part => /[<>:"|?*]|[. ]$/.test(part) || /^(con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i.test(part)) ||
- normalized.split('/').some(part => !part || part === '.' || part === '..')) {
+ UNCONFINED_SEGMENT.test(normalized)) {
throw opError('page_identity_changed', 'The stored sync origin is not a confined relative path.',
`The stored source path ${JSON.stringify(path)} is empty, absolute, or escapes the checkout, so sync refused to use it. Inspect the source's writer status and report it to the user; the page origin must be repaired, not overwritten.`);
}
diff --git a/src/core/persistence/sync-pass-apply.ts b/src/core/persistence/sync-pass-apply.ts
index e613958c44..758df6fec3 100644
--- a/src/core/persistence/sync-pass-apply.ts
+++ b/src/core/persistence/sync-pass-apply.ts
@@ -33,7 +33,7 @@ import type { BrainEngine } from '../engine.ts';
import { resolveSlugForPath } from '../sync.ts';
import { massReconcileAllowed, planReconcileDeletes } from '../sync-reconcile.ts';
import { filterYielding, loopYield } from './sync-yield.ts';
-import { admitKeepDbReconcile, freezeKeepDbReconcile, releaseMatchedDriftSkips } from '../repair/keep-db-policy.ts';
+import { admitKeepDbReconcile, driftSkippedSlugs, freezeKeepDbReconcile, releaseMatchedDriftSkips } from '../repair/keep-db-policy.ts';
const KEEP_DB_CAPACITY_REFUSALS = new Set(['capacity', 'write_capacity_backpressure', 'queue_capacity', 'write_quota_exhausted', 'writer_busy', 'writer_pool_capacity']);
import type { SyncProcessingOptions } from './sync-authority.ts';
@@ -47,7 +47,7 @@ import { readSyncImportProvenances, recordSyncImportProvenances, syncImportParse
type GitHoldCode, type GitHoldMeta, type SyncImportProvenance } from './sync-holds.ts';
import { assertDrainLease } from './sync-lease.ts';
import { clearDeletesWithheld, deleteSetHash, readDeletesWithheld, readOriginDeletedMarker, retireOriginDeletedMarker, writeDeletesWithheld, type DeletesWithheld } from './sync-markers.ts';
-import type { ShadowLevel, ShadowPath } from './sync-pass.ts';
+import { isCompactMatched, type ShadowLevel, type ShadowPath } from './sync-pass.ts';
import { SETTLE_MISMATCH_STREAK, type ManagedSyncPassValue, type PassRoundSummary } from './sync-pass-row.ts';
import { syncClassAction, type SyncClassAction } from './sync-classify.ts';
import type { SyncAdmission } from './sync-admission.ts';
@@ -137,10 +137,12 @@ export async function applyPassRound(engine: BrainEngine, level: ShadowLevel, in
await adoptRecords(engine, level, state);
await sweepLegacy(engine, level, input, state);
await admitKeepDb(engine, level, input, state);
+ // Only a matched page the concept side skipped for drift has a skip to release: a 600k level keeps no set of every slug.
+ const skipped = await driftSkippedSlugs(engine, level.sourceId);
const step = loopYield();
- for (const entry of level.paths) {
+ for (const entry of skipped.size ? level.paths : []) {
if (step.due()) await step.pause();
- if (entry.result.cls === 'matched' && entry.slug) state.matchedSlugs.add(entry.slug);
+ if (entry.result.cls === 'matched' && entry.slug && skipped.has(entry.slug)) state.matchedSlugs.add(entry.slug);
}
summarizeRound(level, state);
}
@@ -172,7 +174,7 @@ export function summarizeRound(level: ShadowLevel, state: PassApplyState): void
/** Design §2.5: retire markers whose origin left both `T` and the disk, or that a live page claims again (compare-and-delete). */
async function retireMarkers(engine: BrainEngine, level: ShadowLevel, state: PassApplyState): Promise<void> {
- const retire = await filterYielding(level.paths, entry => entry.state.marker && !entry.legacyPageless
+ const retire = await filterYielding(level.paths, entry => !isCompactMatched(entry) && entry.state.marker && !entry.legacyPageless
&& ((entry.state.tree === null && !entry.state.present) || entry.state.page?.live === true));
if (!retire.length) return;
await engine.transaction(async tx => {
@@ -198,7 +200,7 @@ async function applyDeleteValve(engine: BrainEngine, level: ShadowLevel, input:
// The valve plans over every live page only when the round deletes something (no delete never trips it).
const plan = deletes.length ? await (async () => {
const deleting = new Set(deletes.map(entry => entry.path));
- const live = await filterYielding(level.paths, entry => entry.state.page?.live === true && !!entry.slug);
+ const live = await filterYielding(level.paths, entry => (isCompactMatched(entry) || entry.state.page?.live === true) && !!entry.slug);
return planReconcileDeletes(live.map(entry => ({ slug: entry.slug!, source_path: entry.path })), live.filter(entry => !deleting.has(entry.path)).map(entry => entry.path), () => true);
})() : null;
const tripped = !!plan?.massDelete && (input.remote || !massReconcileAllowed());
@@ -330,8 +332,8 @@ async function sweepLegacy(engine: BrainEngine, level: ShadowLevel, input: PassA
/** Rows 15 and 18 under keep_db (§4): freeze to `T[p]`'s bytes and admit the DB-wins reconcile of the pass, bounded per pass. */
async function admitKeepDb(engine: BrainEngine, level: ShadowLevel, input: PassApplyInput, state: PassApplyState): Promise<void> {
const taken = new Set(state.keepDbTaken);
- const want = await filterYielding(level.paths, entry => !entry.result.inFlight && !taken.has(entry.path) && entry.pageId !== null && entry.revision !== null && entry.slug && entry.state.level !== null
- && ((entry.result.cls === 'keep_db' && !entry.result.settled) || (entry.result.cls === 'db_ahead' && entry.result.keepDbWrite === true)));
+ const want = await filterYielding(level.paths, entry => !entry.result.inFlight && ((entry.result.cls === 'keep_db' && !entry.result.settled) || (entry.result.cls === 'db_ahead' && entry.result.keepDbWrite === true))
+ && !taken.has(entry.path) && entry.pageId !== null && entry.revision !== null && entry.slug && entry.state.level !== null);
if (!want.length) return;
if (input.admission !== 'normal') {
for (const entry of want) { state.keepDb.paused++; settleAs(entry, { settled: true, action: 'none', paused: 'keep_db' }); }
diff --git a/src/core/persistence/sync-pass.ts b/src/core/persistence/sync-pass.ts
index e3d0516f3a..92c0733242 100644
--- a/src/core/persistence/sync-pass.ts
+++ b/src/core/persistence/sync-pass.ts
@@ -31,8 +31,8 @@ import { sourceMirrorReadOnly } from './mirror-read-only.ts';
import { colonFileRefusal, COLON_REFUSAL_MESSAGES, readSyncFile, resolveManagedSyncContext, syncExcludePatterns, syncGit, type ManagedSyncContext, type SyncFileRefusal } from './sync-discovery.ts';
import { isWindowsColonTarget } from './native-file-target.ts';
import { confirmedSyncDelta } from '../sync-rename-identity.ts';
-import { holdBasisMoved, holdRescreenDue, readGitHoldRetryPaths, readGitSourceHolds, SYNC_IMPORT_PROVENANCE_OP, syncImportParserFor,
- type GitHoldRecord, type SyncImportProvenance } from './sync-holds.ts';
+import { holdBasisMoved, holdRescreenDue, readGitHoldRetryPaths, readGitSourceHolds, SYNC_IMPORT_PROVENANCE_OP, syncImportKind, syncImportParser, syncImportParserFor,
+ type GitHoldRecord, type SyncImportParser, type SyncImportProvenance } from './sync-holds.ts';
import { movedDriftHolds, sameParser } from './sync-prescreen.ts';
import { sameSyncOrigin, selectOriginClaimant, syncOriginPath } from './sync-origin.ts';
import { hashWorkingLevel, probeDirtiness, readBlobs, readIndexDelta, readIndexLevel, readTreeLevel, workingChanged, type GitIndexEntry, type GitTreeEntry, type WorkingStatCache } from './sync-git-stream.ts';
@@ -40,7 +40,7 @@ import { classifySyncPath, syncPassKind, type SyncClassified, type SyncIdleBlock
import { buildConvergenceReceipt, readOnlySameContentScreen, type ConvergenceReceipt, type ConvergenceScreen } from './sync-convergence.ts';
import { readManagedMode } from './ownership.ts';
import { MANAGED_SYNC_PASS_OP, SYNC_ORIGIN_DELETED_OP, type SyncSelection } from './sync-pass-row.ts';
-import { loopYield, sortYielding } from './sync-yield.ts';
+import { filterYielding, loopYield, sortYielding, type LoopYield } from './sync-yield.ts';
export { MANAGED_SYNC_PASS_OP, SYNC_ORIGIN_DELETED_OP };
@@ -82,11 +82,28 @@ export interface PassLevelCache { tree?: { key: string; tree: Map<string, GitTre
* level holds a handful instead of one per path. Bounded; a result past the bound stays its own.
*/
const SHARED_RESULTS = new Map<string, SyncClassified>();
+/**
+ * The last shared result of each class with its keys in order: most paths of a level repeat it, and a match on the same
+ * keys in the same order with the same primitive values (what `JSON.stringify` would key alike) allocates nothing.
+ */
+const LAST_SHARED = new Map<string, { result: SyncClassified; keys: string[] }>();
+const sameFlat = (last: { result: SyncClassified; keys: string[] }, b: SyncClassified): boolean => {
+ const a = last.result as unknown as Record<string, unknown>, other = b as unknown as Record<string, unknown>;
+ let i = 0;
+ for (const key in other) {
+ const value = other[key];
+ if (last.keys[i++] !== key || value === undefined || typeof value === 'object' && value !== null || a[key] !== value) return false;
+ }
+ return i === last.keys.length;
+};
+const rememberShared = (result: SyncClassified) => LAST_SHARED.set(result.cls, { result, keys: Object.keys(result) });
function sharedResult(result: SyncClassified): SyncClassified {
+ const last = LAST_SHARED.get(result.cls);
+ if (last && sameFlat(last, result)) return last.result;
const key = JSON.stringify(result);
const shared = SHARED_RESULTS.get(key);
- if (shared) return shared;
- if (SHARED_RESULTS.size < 256) SHARED_RESULTS.set(key, Object.freeze(result) as SyncClassified);
+ if (shared) { rememberShared(shared); return shared; }
+ if (SHARED_RESULTS.size < 256) { SHARED_RESULTS.set(key, Object.freeze(result) as SyncClassified); rememberShared(result); }
return result;
}
@@ -129,6 +146,9 @@ class MatchedPath implements ShadowPath {
}
}
+/** Whether `entry` is a compact converged `matched` path: its state is a live page at its level, with no marker, hold or open work. */
+export const isCompactMatched = (entry: ShadowPath): boolean => entry instanceof MatchedPath;
+
export interface ShadowLevel {
sourceId: string; incarnation: string; root: string; gitRoot: string; scope: string; slugMode: 'git-root' | 'source-root';
mode: 'committed' | 'working'; head: string; baselineCommit: string | null; full: boolean;
@@ -164,6 +184,10 @@ const recordOids = (record: PageRow['record'], level?: string | null): string[]
return oids;
};
+/** Whether the record names `oid` among its blob ids (`recordOids(record).includes(oid)` without the array). */
+const recordNames = (record: PageRow['record'], oid: string | null): boolean => !!record && typeof oid === 'string' && oid.length > 0
+ && (record.blob_oid === oid || record.disk_blob_oid === oid);
+
type Exec = Pick<BrainEngine, 'executeRaw'>;
/**
@@ -186,10 +210,11 @@ async function withRoundSnapshot<T>(engine: BrainEngine, fn: (exec: Exec) => Pro
* stretch of about a second and held every raw row at once. `cursor_tuple_fraction = 1` keeps the whole-result plan.
* PGLite reads bounded keyset statements so writers interleave.
*/
-async function readPages(exec: Exec, kind: 'postgres' | 'pglite', sourceId: string, incarnation: string): Promise<{ rows: PageRow[]; statements: number }> {
+async function readPages(exec: Exec, kind: 'postgres' | 'pglite', sourceId: string, incarnation: string, expected: SyncImportParser): Promise<{ rows: PageRow[]; statements: number }> {
// A record's fields usually repeat the page's (origin = source_path, disk = blob, revision = the page's), so those come back
- // as a flag instead of a second copy of the string, and the parser as text parsed once per distinct value: a 600k-page
- // read then holds about one string per field per page.
+ // as a flag instead of a second copy of the string, and so does the parser when it is the Markdown one the level expects
+ // (`expected`, the same object for every such page; any other comes as text parsed once per distinct value): a 600k-page
+ // read then holds about one string per field per page and decodes no parser text per page.
// The record's fields come out of its JSON once, in a subquery. On Postgres `OFFSET 0` keeps it unflattened, so the hash
// join builds over those few text columns instead of the whole record (600k: about 3.1 s of serial server time to 2.0 s);
// PGLite's keyset statements need it flattened, so each probes the record by primary key.
@@ -202,15 +227,16 @@ async function readPages(exec: Exec, kind: 'postgres' | 'pglite', sourceId: stri
CASE WHEN c.disk_blob_oid IS DISTINCT FROM c.blob_oid THEN c.disk_blob_oid END AS rec_disk_blob_oid,
c.revision IS NULL AS rec_revision_null,
NULLIF(c.revision, p.knowledge_revision::text) AS rec_revision,
- c.parser AS rec_parser, c.full_episode AS rec_full_episode, c.working AS rec_working
+ CASE WHEN c.parser IS DISTINCT FROM $4::jsonb THEN c.parser::text END AS rec_parser, c.parser = $4::jsonb AS rec_parser_expected,
+ c.full_episode AS rec_full_episode, c.working AS rec_working
FROM pages p LEFT JOIN (SELECT fingerprint, completed_keys->0->>'origin' AS origin, completed_keys->0->>'blob_oid' AS blob_oid,
completed_keys->0->>'disk_blob_oid' AS disk_blob_oid, completed_keys->0->>'revision' AS revision,
- (completed_keys->0->'parser')::text AS parser, completed_keys->0->>'full_episode' AS full_episode, (completed_keys->0->'working')::text AS working
+ completed_keys->0->'parser' AS parser, completed_keys->0->>'full_episode' AS full_episode, (completed_keys->0->'working')::text AS working
FROM op_checkpoints WHERE op=$2${kind === 'postgres' ? ' OFFSET 0' : ''}) c ON c.fingerprint=$3||p.id::text
WHERE p.source_id=$1 AND p.source_path IS NOT NULL`;
- const params = [sourceId, SYNC_IMPORT_PROVENANCE_OP, `${sourceId}:${incarnation}:`];
+ const params = [sourceId, SYNC_IMPORT_PROVENANCE_OP, `${sourceId}:${incarnation}:`, JSON.stringify(expected)];
type Raw = Omit<PageRow, 'record' | 'arm1' | 'attested_hash'> & { has_record: boolean; rec_origin_same: boolean; rec_origin: string | null; rec_blob_oid: string | null;
- rec_disk_same: boolean; rec_disk_blob_oid: string | null; rec_revision_null: boolean; rec_revision: string | null; rec_parser: string | null; rec_full_episode: string | null; rec_working: string | null };
+ rec_disk_same: boolean; rec_disk_blob_oid: string | null; rec_revision_null: boolean; rec_revision: string | null; rec_parser: string | null; rec_parser_expected: boolean | null; rec_full_episode: string | null; rec_working: string | null };
const rows: PageRow[] = [];
const parsers = new Map<string, SyncImportProvenance['parser'] | undefined>();
const parserOf = (text: string | null) => {
@@ -222,7 +248,7 @@ async function readPages(exec: Exec, kind: 'postgres' | 'pglite', sourceId: stri
for (const raw of page) rows.push({ id: raw.id, slug: raw.slug, source_path: raw.source_path, revision: raw.revision, deleted: raw.deleted, request_id: raw.request_id,
arm1: null, attested_hash: null, record: raw.has_record ? { origin: raw.rec_origin_same ? raw.source_path : raw.rec_origin ?? '', blob_oid: raw.rec_blob_oid ?? undefined,
disk_blob_oid: raw.rec_disk_same ? raw.rec_blob_oid : raw.rec_disk_blob_oid, revision: raw.rec_revision_null ? undefined : raw.rec_revision ?? raw.revision,
- parser: parserOf(raw.rec_parser), full_episode: raw.rec_full_episode, ...(raw.rec_working === 'true' || raw.rec_working === 'false' ? { working: raw.rec_working === 'true' } : {}) } : null });
+ parser: raw.rec_parser_expected ? expected : parserOf(raw.rec_parser), full_episode: raw.rec_full_episode, ...(raw.rec_working === 'true' || raw.rec_working === 'false' ? { working: raw.rec_working === 'true' } : {}) } : null });
};
let statements = 0;
if (kind === 'postgres') {
@@ -242,7 +268,7 @@ async function readPages(exec: Exec, kind: 'postgres' | 'pglite', sourceId: stri
let after = 0;
for (;;) {
statements++;
- const page = await exec.executeRaw<Raw>(`${select} AND p.id>$4 ORDER BY p.id LIMIT ${PGLITE_KEYSET_ROWS}`, [...params, after]);
+ const page = await exec.executeRaw<Raw>(`${select} AND p.id>$5 ORDER BY p.id LIMIT ${PGLITE_KEYSET_ROWS}`, [...params, after]);
take(page);
if (page.length < PGLITE_KEYSET_ROWS) return { rows, statements };
after = Number(page[page.length - 1]!.id);
@@ -369,24 +395,11 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: st
const engineKind = engine.kind === 'pglite' ? 'pglite' : 'postgres';
const nativeRoot = realpathSync.native(root), nativeGitRoot = realpathSync.native(gitRoot);
const scope = relative(nativeGitRoot, nativeRoot).split(sep).join('/');
- // The setup reads are independent: one round trip each, all in flight together.
t = timer();
- const [company, slugModeRead, excludeRead, includeHiddenRead, workingRead, packRead, mirrorRead] = await Promise.allSettled([
- currentCompanyBrainSync(sourceId) ? Promise.resolve(true) : getCompanyBrainProfile(engine, sourceId).then(profile => !!profile),
- scope && input.pass?.slugMode ? Promise.resolve(input.pass.slugMode) : scope ? resolveSlugRootMode(engine, { sourceId, explicitGitRoot: false, slugPrefix: resolveSlugForPath(join(scope, 'x.md')).slice(0, -2), dryRun: true }) : Promise.resolve('git-root' as const),
- syncExcludePatterns(engine, [...input.pass?.exclude ?? []]),
- engine.getConfig('sync.include_hidden'),
- input.workingTree === undefined ? engine.getConfig('sync.include_working_tree') : Promise.resolve(null),
- input.pass?.noSchemaPack ? Promise.resolve(null) : loadActivePackForEngine(engine, { remote: false, sourceId }).catch(() => null),
- sourceMirrorReadOnly(engine, sourceId),
- ]);
+ const settings = await readLevelSettings(engine, context, scope, input);
timings.setup = t();
- const settled = <T>(read: PromiseSettledResult<T>): T => { if (read.status === 'rejected') throw read.reason; return read.value; };
- if (settled(company)) return null;
- const slugMode = settled(slugModeRead), exclude = settled(excludeRead), pack = settled(packRead), mirror = settled(mirrorRead);
- const strategy = input.pass?.strategy ?? (context.source.config?.strategy as string | undefined) ?? 'markdown';
- const includeHidden = [...new Set([...input.pass?.includeHidden ?? [], ...(settled(includeHiddenRead) ?? '').split(/[\n,]/).map(v => v.trim()).filter(Boolean)].map(v => v.endsWith('/') ? `${v}**` : v))];
- const keepDbPrefixes = (() => { try { return loadStorageConfig(root)?.keep_db ?? []; } catch { return []; } })();
+ if (!settings) return null;
+ const { slugMode, exclude, pack, mirror, strategy, includeHidden, keepDbPrefixes, workingConfig } = settings;
const sourceRelative = (gitPath: string) => (scope ? gitPath.slice(scope.length + 1) : gitPath);
// Eligibility of a path outside the cached tree (every path of the tree is eligible); origins validate per call: a
// 600k-path level keeps no per-path memo of either.
@@ -414,7 +427,7 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: st
if (!head) throw new Error(`source ${sourceId}: HEAD does not name a commit, so there is no level to read`);
// Master's rule (sync-discovery.ts): a detached HEAD forces working mode.
const detached = syncGit(gitRoot, ['rev-parse', '--abbrev-ref', 'HEAD']).trim() === 'HEAD';
- const working = detached || (input.workingTree ?? settled(workingRead) === 'true');
+ const working = detached || (input.workingTree ?? workingConfig === 'true');
const mode: 'committed' | 'working' = working ? 'working' : 'committed';
timings.rev_parse = t();
@@ -445,19 +458,8 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: st
mode === 'working' ? timed('X', () => readIndexLevel(gitRoot)) : Promise.resolve(new Map<string, GitIndexEntry>()),
timed('U', async () => (mode === 'working' ? (await readUntracked(gitRoot, scope)).filter(eligible) : []))]),
// Issued together, the snapshot's statements pipeline on its one connection (PGLite runs them in turn).
- timed('I⋈P', () => withRoundSnapshot(engine, async exec => {
- const [read, work, markers, passRow] = await Promise.all([timed('pages', () => readPages(exec, engineKind, sourceId, incarnation)),
- timed('open_work', () => readOpenWork(exec, sourceId, incarnation, context.binding.worktree_id)), timed('markers', () => readMarkers(exec, sourceId, incarnation)),
- timed('pass_row', () => readPassRow(exec, sourceId, incarnation))]);
- return { pages: read.rows, open: work, markers, passRow, statements: read.statements + work.statements + 2 };
- })),
- timed('H', async () => {
- const [sourceHolds, retryPaths] = await Promise.all([readGitSourceHolds(engine, { sourceIds: [sourceId] }), readGitHoldRetryPaths(engine, sourceId, incarnation)]);
- const holds = (sourceHolds[0]?.holds ?? []).filter(hold => hold.incarnation === incarnation);
- const holdByPath = new Map(holds.map(hold => [hold.path, hold]));
- const [basisMoved, driftMoved] = await Promise.all([holdBasisMoved(engine, sourceId, holdByPath, [...holdByPath.keys()]), movedDriftHolds(engine, sourceId, holds)]);
- return { holds, retry: new Set(retryPaths.filter(path => !input.pass?.retryConsumed?.has(path))), moved: [...basisMoved, ...driftMoved] };
- }),
+ timed('I⋈P', () => readRoundSnapshot(engine, { kind: engineKind, sourceId, incarnation, worktreeId: context.binding.worktree_id, parser: syncImportParser(pack?.identity ?? null) }, timed)),
+ timed('H', () => readLevelHolds(engine, sourceId, incarnation, input.pass?.retryConsumed)),
]);
timings.reads = t();
const [{ tree: eligibleT, colon: colonTree, indexDelta }, index, untrackedPaths] = gitLevel;
@@ -482,19 +484,10 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: st
const holdByGitPath = new Map<string, GitHoldRecord>(holds.map(hold => [toGit(hold.path), hold]));
const moved = new Set(held.moved.map(toGit));
- // The page claiming each origin: the live one first, else the newest tombstone. The rows leave the snapshot's result
- // here (the driver's transaction keeps that object until its connection's next one), so only `byOrigin` holds them.
+ // The rows leave the snapshot's result here (the driver's transaction keeps that object until its connection's next one),
+ // so only `byOrigin` (`claimOrigins`) holds them.
t = timer();
- // Every page recording an origin that more than one page records (#6517), so the level can name an ambiguous origin.
- const byOrigin = new Map<string, PageRow>(), claimants = new Map<string, PageRow[]>();
- for (const row of snapshot.pages) {
- if (step.due()) await step.pause();
- if (isWindowsColonTarget(row.source_path)) continue;
- const key = gitPathOf(pageOrigin(row));
- const prior = byOrigin.get(key);
- if (prior) { const all = claimants.get(key); if (all) all.push(row); else claimants.set(key, [prior, row]); }
- if (!prior || (prior.deleted && !row.deleted) || (prior.deleted === row.deleted && Number(row.id) > Number(prior.id))) byOrigin.set(key, row);
- }
+ const { byOrigin, claimants } = await claimOrigins(snapshot.pages, row => gitPathOf(pageOrigin(row)), tree, step);
snapshot.pages = [];
const domain = new Set<string>();
for (const path of tree.keys()) { domain.add(path); if (step.due()) await step.pause(); }
@@ -508,29 +501,7 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: st
if (step.due()) await step.pause();
}
for (const origin of markers.keys()) { const path = gitPathOf(origin); if (inLevel(path)) domain.add(path); }
- // Spellings of one origin that differ only by case or separator (the domain holds each path once).
- const fold = (path: string) => path.replaceAll('\\', '/').toLowerCase();
- const spellings = new Map<string, string | string[]>();
- // Files at L count, and so does a live page's separator-spelled origin; a case-only respelling whose old file this sync
- // removes is no refusal (discovery's one exception).
- for (const path of domain) {
- if (step.due()) await step.pause();
- const live = byOrigin.get(path);
- if (!tree.has(path) && !(mode === 'working' && (untracked.has(path) || index.has(path))) && !(live && !live.deleted && live.source_path.includes('\\'))) continue;
- const folded = fold(path);
- const prior = spellings.get(folded);
- spellings.set(folded, prior === undefined ? path : typeof prior === 'string' ? [prior, path] : [...prior, path]);
- }
- // As discovery does, only spellings that name one page are twins: a live origin's page slug, else the slug its path names.
- const twins = new Set<string>(), twinGroups: string[][] = [];
- for (const paths of spellings.values()) {
- if (typeof paths === 'string') continue;
- const slugOf = (path: string) => { const live = byOrigin.get(path); return live && !live.deleted ? live.slug : resolveSlugForPath(originOf(path)); };
- const bySlug = new Map<string, string[]>();
- for (const path of paths) bySlug.set(slugOf(path), [...bySlug.get(slugOf(path)) ?? [], path]);
- for (const group of bySlug.values()) if (group.length > 1) { twinGroups.push(group); for (const path of group) twins.add(path); }
- }
- spellings.clear();
+ const { twins, twinGroups } = await spellingTwins(domain, byOrigin, path => tree.has(path) || (mode === 'working' && (untracked.has(path) || index.has(path))), originOf, step);
timings.domain = t();
// Working mode rehashes every path (the full round's unconditional rehash); committed mode probes candidates only.
@@ -555,9 +526,9 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: st
for (const [path, row] of byOrigin) {
if (step.due()) await step.pause();
const level = levelOf(path, probes.get(path));
- if (!row.deleted && row.record?.revision == null && level !== null && !recordOids(row.record).includes(level) && inLevel(path)) wantBaseline = true;
+ if (!row.deleted && row.record?.revision == null && level !== null && !recordNames(row.record, level) && inLevel(path)) wantBaseline = true;
if (row.request_id === null) continue;
- if (row.deleted || level === null || !recordOids(row.record).includes(level) || row.record?.revision == null || String(row.record.revision) !== row.revision) unproven.push(row);
+ if (row.deleted || level === null || !recordNames(row.record, level) || row.record?.revision == null || String(row.record.revision) !== row.revision) unproven.push(row);
}
const attestationStatements = unproven.length ? await readAttestations(engine, engineKind, sourceId, incarnation, unproven) : 0;
timings.A = t();
@@ -574,25 +545,17 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: st
};
if (wantBaseline) await loadBaseline();
const baselineOf = (path: string): string | null => baselineTree?.get(path)?.oid ?? null;
- // Master refuses a spelling only among the files its sync claims (sync-discovery.ts:538-541): the live page's origin and the
- // page-less spellings that changed since the cursor. A page-less spelling unchanged since the baseline was never claimed
- // (#6865), so unless the claimed spellings still collide it settles as `legacy` and the others classify on their own.
- const legacySpelling = new Set<string>();
- for (const group of twinGroups) {
- const unchanged: string[] = [];
- for (const path of group) {
- const live = byOrigin.get(path);
- const level = levelOf(path, probes.get(path));
- if ((live && !live.deleted) || level === null) continue;
- await loadBaseline();
- if (level === baselineOf(path)) unchanged.push(path);
- }
- if (group.length - unchanged.length > 1) continue;
- for (const path of group) twins.delete(path);
- for (const path of unchanged) legacySpelling.add(path);
- }
+ // A page-less spelling twin unchanged since the baseline (#6865): see `settleUnchangedSpellings`.
+ const legacySpelling = await settleUnchangedSpellings(twinGroups, twins, async path => {
+ const live = byOrigin.get(path);
+ const level = levelOf(path, probes.get(path));
+ if ((live && !live.deleted) || level === null) return false;
+ await loadBaseline();
+ return level === baselineOf(path);
+ });
- const fp = (path: string) => syncImportParserFor(sourceRelative(path), pack?.identity ?? null);
+ const parserFor = parserByKind(pack?.identity ?? null);
+ const fp = (path: string) => parserFor(sourceRelative(path));
const stateOf = (path: string, dirty: SyncPathState['dirty'], probe: { dp: string; rawOid: string | null; rawSha256: string | null } | undefined): { state: SyncPathState; row?: PageRow; legacyPageless?: true } => {
const committed = tree.get(path) ?? null;
const row = byOrigin.get(path);
@@ -633,23 +596,15 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: st
// Bootstrap (the incarnation's first pass; for a read-only level, no pass row yet): page-less paths at their baseline blob
// whose slug a committed deletion names get a `legacy` marker (§2.7, b1).
t = timer();
- const legacyDeletions = new Map<string, string>();
- if (baselineCommit && (input.pass ? input.pass.bootstrap === true : !passRow)) {
- const pageless: Array<{ path: string; slug: string }> = [];
- for (const path of domain) {
- if (step.due()) await step.pause();
- if (byOrigin.has(path) || markers.has(originOf(path))) continue;
+ const legacyDeletions = baselineCommit && (input.pass ? input.pass.bootstrap === true : !passRow)
+ ? await bootstrapLegacyDeletions(engine, { sourceId, incarnation, domain, step }, async path => {
+ if (byOrigin.has(path) || markers.has(originOf(path))) return null;
const level = levelOf(path, probes.get(path));
- if (level === null) continue;
+ if (level === null) return null;
await loadBaseline();
- if (level !== baselineOf(path)) continue;
- const slug = resolveSlugForPath(originOf(path));
- if (slug) pageless.push({ path, slug });
- }
- const bySlug = await readLegacyDeletions(engine, sourceId, incarnation, [...new Set(pageless.map(entry => entry.slug))]);
- for (const { path, slug } of pageless) { const id = bySlug.get(slug); if (id) legacyDeletions.set(path, id); }
- timings.legacy = t();
- }
+ return level === baselineOf(path) ? resolveSlugForPath(originOf(path)) || null : null;
+ }).then(found => { timings.legacy = t(); return found; })
+ : new Map<string, string>();
t = timer();
const paths: ShadowPath[] = [];
@@ -697,8 +652,8 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: st
}
// Adoption by hash needs the level's bytes: committed mode reads the blobs of the candidates that could adopt.
- const needBytes = paths.filter(entry => entry.state.levelSha256 === null && entry.state.level !== null && entry.state.attested?.hash && entry.state.page?.live
- && !['matched', 'held', 'dirty'].includes(entry.result.cls));
+ const needBytes = await filterYielding(paths, entry => !['matched', 'held', 'dirty'].includes(entry.result.cls) && entry.state.levelSha256 === null && entry.state.level !== null
+ && entry.state.attested?.hash && entry.state.page?.live, step);
if (needBytes.length) {
t = timer();
const blobs = await readBlobs(gitRoot, [...new Set(needBytes.map(entry => entry.state.level!))], LEVEL_BLOB_MAX_BYTES);
@@ -723,6 +678,151 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: st
rescreenDue: holds.filter(hold => holdRescreenDue(hold, retry.has(hold.path))).map(hold => toGit(hold.path)), workingCache: hashed?.cache ?? null };
}
+/** The level's settings, one round trip each and all in flight together; null for a company-brain source. */
+async function readLevelSettings(engine: BrainEngine, context: ManagedSyncContext, scope: string, input: { workingTree?: boolean; pass?: PassLevelInput }) {
+ const { sourceId, root } = context;
+ const [company, slugModeRead, excludeRead, includeHiddenRead, workingRead, packRead, mirrorRead] = await Promise.allSettled([
+ currentCompanyBrainSync(sourceId) ? Promise.resolve(true) : getCompanyBrainProfile(engine, sourceId).then(profile => !!profile),
+ scope && input.pass?.slugMode ? Promise.resolve(input.pass.slugMode) : scope ? resolveSlugRootMode(engine, { sourceId, explicitGitRoot: false, slugPrefix: resolveSlugForPath(join(scope, 'x.md')).slice(0, -2), dryRun: true }) : Promise.resolve('git-root' as const),
+ syncExcludePatterns(engine, [...input.pass?.exclude ?? []]),
+ engine.getConfig('sync.include_hidden'),
+ input.workingTree === undefined ? engine.getConfig('sync.include_working_tree') : Promise.resolve(null),
+ input.pass?.noSchemaPack ? Promise.resolve(null) : loadActivePackForEngine(engine, { remote: false, sourceId }).catch(() => null),
+ sourceMirrorReadOnly(engine, sourceId),
+ ]);
+ const settled = <T>(read: PromiseSettledResult<T>): T => { if (read.status === 'rejected') throw read.reason; return read.value; };
+ if (settled(company)) return null;
+ return { slugMode: settled(slugModeRead), exclude: settled(excludeRead), pack: settled(packRead), mirror: settled(mirrorRead), workingConfig: settled(workingRead),
+ strategy: input.pass?.strategy ?? (context.source.config?.strategy as string | undefined) ?? 'markdown',
+ includeHidden: [...new Set([...input.pass?.includeHidden ?? [], ...(settled(includeHiddenRead) ?? '').split(/[\n,]/).map(v => v.trim()).filter(Boolean)].map(v => v.endsWith('/') ? `${v}**` : v))],
+ keepDbPrefixes: (() => { try { return loadStorageConfig(root)?.keep_db ?? []; } catch { return []; } })() };
+}
+
+/** The round's database snapshot (`I⋈P`, `U_db`, the markers and the pass row); issued together, its statements pipeline on its one connection (PGLite runs them in turn). */
+async function readRoundSnapshot(engine: BrainEngine, input: { kind: 'postgres' | 'pglite'; sourceId: string; incarnation: string; worktreeId: string; parser: SyncImportParser },
+ timed: <T>(key: string, read: () => Promise<T>) => Promise<T>) {
+ const { kind, sourceId, incarnation, worktreeId } = input;
+ return withRoundSnapshot(engine, async exec => {
+ const [read, work, markers, passRow] = await Promise.all([timed('pages', () => readPages(exec, kind, sourceId, incarnation, input.parser)),
+ timed('open_work', () => readOpenWork(exec, sourceId, incarnation, worktreeId)), timed('markers', () => readMarkers(exec, sourceId, incarnation)),
+ timed('pass_row', () => readPassRow(exec, sourceId, incarnation))]);
+ return { pages: read.rows, open: work, markers, passRow, statements: read.statements + work.statements + 2 };
+ });
+}
+
+/** `H`: the source's holds of this incarnation, the retry set less what the pass consumed, and the holds whose basis or drift moved. */
+async function readLevelHolds(engine: BrainEngine, sourceId: string, incarnation: string, retryConsumed: ReadonlySet<string> | undefined) {
+ const [sourceHolds, retryPaths] = await Promise.all([readGitSourceHolds(engine, { sourceIds: [sourceId] }), readGitHoldRetryPaths(engine, sourceId, incarnation)]);
+ const holds = (sourceHolds[0]?.holds ?? []).filter(hold => hold.incarnation === incarnation);
+ const holdByPath = new Map(holds.map(hold => [hold.path, hold]));
+ const [basisMoved, driftMoved] = await Promise.all([holdBasisMoved(engine, sourceId, holdByPath, [...holdByPath.keys()]), movedDriftHolds(engine, sourceId, holds)]);
+ return { holds, retry: new Set(retryPaths.filter(path => !retryConsumed?.has(path))), moved: [...basisMoved, ...driftMoved] };
+}
+
+/**
+ * Spellings of one origin that differ only by case or separator (the domain holds each path once). Files at L count
+ * (`isFile`), and so does a live page's separator-spelled origin; a case-only respelling whose old file this sync removes
+ * is no refusal (discovery's one exception). As discovery does, only spellings that name one page are twins: a live
+ * origin's page slug, else the slug its path names.
+ */
+/**
+ * The page claiming each origin (keyed by `keyOf`): the live one first, else the newest tombstone; and every page recording
+ * an origin that more than one page records (#6517), so the level can name an ambiguous origin. A record naming the tree's
+ * blob keeps the tree's copy of its id: a converged 600k level holds one string per blob.
+ */
+async function claimOrigins(pages: PageRow[], keyOf: (row: PageRow) => string, tree: Map<string, GitTreeEntry>, step: LoopYield) {
+ const byOrigin = new Map<string, PageRow>(), claimants = new Map<string, PageRow[]>();
+ for (const row of pages) {
+ if (step.due()) await step.pause();
+ if (isWindowsColonTarget(row.source_path)) continue;
+ const key = keyOf(row);
+ const prior = byOrigin.get(key);
+ if (prior) { const all = claimants.get(key); if (all) all.push(row); else claimants.set(key, [prior, row]); }
+ if (!prior || (prior.deleted && !row.deleted) || (prior.deleted === row.deleted && Number(row.id) > Number(prior.id))) byOrigin.set(key, row);
+ const committed = row.record && tree.get(key);
+ if (committed && row.record!.blob_oid === committed.oid) Object.assign(row.record!, { blob_oid: committed.oid, ...(row.record!.disk_blob_oid === committed.oid ? { disk_blob_oid: committed.oid } : {}) });
+ }
+ return { byOrigin, claimants };
+}
+
+/** One parser per importer kind (`syncImportParserFor`) for a whole level, not one object per path. */
+function parserByKind(schemaPack: string | null): (path: string) => SyncImportParser {
+ const parsers = new Map<string, SyncImportParser>();
+ return path => {
+ const kind = syncImportKind(path);
+ let parser = parsers.get(kind);
+ if (!parser) parsers.set(kind, parser = syncImportParserFor(path, schemaPack));
+ return parser;
+ };
+}
+
+async function spellingTwins(domain: Set<string>, byOrigin: Map<string, PageRow>, isFile: (path: string) => boolean, originOf: (gitPath: string) => string, step: LoopYield) {
+ const fold = (path: string) => path.replaceAll('\\', '/').toLowerCase();
+ const spelled = (path: string) => { const live = byOrigin.get(path); return isFile(path) || !!(live && !live.deleted && live.source_path.includes('\\')); };
+ // Two spellings fold alike only when one is not its own fold, so the first walk keeps only those folds; the second
+ // groups, in the domain's order, the spellings of those folds (a level with none keeps no map of every path).
+ const folds = new Set<string>();
+ for (const path of domain) {
+ if (step.due()) await step.pause();
+ const folded = fold(path);
+ if (folded !== path && spelled(path)) folds.add(folded);
+ }
+ const twins = new Set<string>(), twinGroups: string[][] = [];
+ if (!folds.size) return { twins, twinGroups };
+ const spellings = new Map<string, string[]>();
+ for (const path of domain) {
+ if (step.due()) await step.pause();
+ const folded = fold(path);
+ if (!folds.has(folded) || !spelled(path)) continue;
+ const prior = spellings.get(folded);
+ if (prior) prior.push(path); else spellings.set(folded, [path]);
+ }
+ for (const paths of spellings.values()) {
+ if (paths.length < 2) continue;
+ const slugOf = (path: string) => { const live = byOrigin.get(path); return live && !live.deleted ? live.slug : resolveSlugForPath(originOf(path)); };
+ const bySlug = new Map<string, string[]>();
+ for (const path of paths) bySlug.set(slugOf(path), [...bySlug.get(slugOf(path)) ?? [], path]);
+ for (const group of bySlug.values()) if (group.length > 1) { twinGroups.push(group); for (const path of group) twins.add(path); }
+ }
+ return { twins, twinGroups };
+}
+
+/**
+ * Master refuses a spelling only among the files its sync claims (sync-discovery.ts:538-541): the live page's origin and the
+ * page-less spellings that changed since the cursor. A page-less spelling unchanged since the baseline was never claimed
+ * (#6865), so unless the claimed spellings still collide it settles as `legacy` (returned) and the others leave `twins`
+ * to classify on their own.
+ */
+async function settleUnchangedSpellings(twinGroups: string[][], twins: Set<string>, unchangedPageless: (path: string) => Promise<boolean>): Promise<Set<string>> {
+ const legacySpelling = new Set<string>();
+ for (const group of twinGroups) {
+ const unchanged: string[] = [];
+ for (const path of group) if (await unchangedPageless(path)) unchanged.push(path);
+ if (group.length - unchanged.length > 1) continue;
+ for (const path of group) twins.delete(path);
+ for (const path of unchanged) legacySpelling.add(path);
+ }
+ return legacySpelling;
+}
+
+/**
+ * Bootstrap (the incarnation's first pass; for a read-only level, no pass row yet): page-less paths at their baseline blob
+ * (`pagelessSlug` names their slug) whose slug a committed deletion names get a `legacy` marker (§2.7, b1).
+ */
+async function bootstrapLegacyDeletions(engine: BrainEngine, input: { sourceId: string; incarnation: string; domain: Set<string>; step: LoopYield },
+ pagelessSlug: (path: string) => Promise<string | null>): Promise<Map<string, string>> {
+ const pageless: Array<{ path: string; slug: string }> = [];
+ for (const path of input.domain) {
+ if (input.step.due()) await input.step.pause();
+ const slug = await pagelessSlug(path);
+ if (slug) pageless.push({ path, slug });
+ }
+ const bySlug = await readLegacyDeletions(engine, input.sourceId, input.incarnation, [...new Set(pageless.map(entry => entry.slug))]);
+ const found = new Map<string, string>();
+ for (const { path, slug } of pageless) { const id = bySlug.get(slug); if (id) found.set(path, id); }
+ return found;
+}
+
/**
* #5032, as discovery refuses them: on Windows a ':' path has no file. Each selected one gets a named refusal and leaves
* the level: a file at the target is skipped, a live page whose file left keeps its page, and a Git rename (last_commit to
diff --git a/src/core/repair/keep-db-policy.ts b/src/core/repair/keep-db-policy.ts
index 1965f28346..d4870e23a7 100644
--- a/src/core/repair/keep-db-policy.ts
+++ b/src/core/repair/keep-db-policy.ts
@@ -190,16 +190,20 @@ export async function admitKeepDbReconcile(engine: BrainEngine, artifact: KeepDb
});
}
+/** The slugs of the source's pages the concept side skipped for file drift. */
+export async function driftSkippedSlugs(engine: BrainEngine, sourceId: string): Promise<Set<string>> {
+ const prefix = `${sourceId}:`;
+ return new Set(Object.keys(await readDriftSkips(engine)).filter(key => key.startsWith(prefix)).map(key => key.slice(prefix.length)));
+}
+
/**
* Releases the concept drift skips (`dream.concepts.drift_skips`) of pages the pass classified `matched` in `sourceId`:
* one read of the skips, and one release per matched page that has one. It walks the source's skips, not the matched
* slugs, so a 600k-page pass with no skip does no per-page work.
*/
export async function releaseMatchedDriftSkips(engine: BrainEngine, sourceId: string, matchedSlugs: Iterable<string>): Promise<number> {
- const skips = await readDriftSkips(engine);
- const prefix = `${sourceId}:`;
- const skipped = Object.keys(skips).filter(key => key.startsWith(prefix)).map(key => key.slice(prefix.length));
- if (!skipped.length) return 0;
+ const skipped = await driftSkippedSlugs(engine, sourceId);
+ if (!skipped.size) return 0;
const matched = matchedSlugs instanceof Set ? matchedSlugs as Set<string> : new Set(matchedSlugs);
let released = 0;
for (const slug of skipped) {