Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): a full episode admits an already-current page without a reparse

garrytan9c58df0396dc
diff --git a/src/core/persistence/sync-classify.ts b/src/core/persistence/sync-classify.ts
index ee7157ad4d..4dbbffacbf 100644
--- a/src/core/persistence/sync-classify.ts
+++ b/src/core/persistence/sync-classify.ts
@@ -8,7 +8,12 @@
* bytes' blob id), presence on disk, the candidate dirtiness `Dp`, the page and its import record, the attestation of
* its current revision, its hold, deletion marker, baseline blob and open work. Definitions (v3.1 §2.3, v3.2 §2.3):
* `fileSame := L ∈ {r.blob_oid, r.disk_blob_oid}`, `dbSame := r.revision = page.revision`, `proof := dbSame ∨ att(R)`,
- * `obligation := ¬sameParser ∨ origin ≠ page.source_path ∨ episodeUnsatisfied`.
+ * `obligation := ¬sameParser ∨ origin ≠ page.source_path ∨ (episodeUnsatisfied ∧ ¬inPlace)`, where
+ * `inPlace := fileSame ∧ dbSame ∧ derivedCurrent ∧ no hold`: a page the open full episode has not re-imported yet
+ * satisfies it without a reparse when it already is the import of `L` and everything derived from it is current. The
+ * waiver only lifts the episode's term, so it can turn a `reparse` into `matched` and nothing else: any state that keeps
+ * a page from `matched` outside an episode (a stale parser, a moved origin, or any later force-import flag, which must
+ * defeat `matched` to force anything) still reparses it.
*
* G3 of git-canonical memory (docs/designs/GIT_CANONICAL_MEMORY.md): for a strict source (`config.canonical = "git"`) the
* classifier reads the commit alone and Git wins. The working tree is invisible (every path in `T` is clean, a path absent
@@ -68,6 +73,12 @@ export interface SyncPathState {
spelling: boolean | 'legacy';
/** An open full episode has not re-imported this page yet. */
episodeUnsatisfied: boolean;
+ /**
+ * Everything the page derives is current under this binary (sync-pass.ts `readPages`): an inline Markdown page chunked at
+ * the current chunker version, its text projection at its revision and its contextual mode stamped, so a reparse of the
+ * same bytes would change nothing. Absent: unknown, and an open episode reparses the page.
+ */
+ derivedCurrent?: boolean;
/** `--full` or an open episode: every path, page-less ones included, owes a re-import (v3.1 §2.7). */
fullObligation?: boolean;
/** Under a `storage.keep_db` prefix, on a source that is not mirror-read-only. */
@@ -155,7 +166,8 @@ function afterHold(s: SyncPathState): Matched {
const fileSame = s.level !== null && !!s.record && s.record.blobOids.includes(s.level);
const dbSame = !!s.page && !!s.record && s.record.revision !== null && s.record.revision === s.page.revision;
const proof = dbSame || s.attested !== null;
- const obligation = !s.record?.parserSame || !s.page?.originMatches || s.episodeUnsatisfied;
+ const inPlace = fileSame && dbSame && s.derivedCurrent === true && s.hold === null;
+ const obligation = !s.record?.parserSame || !s.page?.originMatches || (s.episodeUnsatisfied && !inPlace);
if (s.spelling) return { cls: 'spelling', settled: s.spelling === 'legacy', action: s.spelling === 'legacy' ? 'none' : 'refuse' };
if (s.mode === 'committed' && s.tree === null && !live) return { cls: 'untracked', settled: true, action: 'none' };
if (live && s.level === null) {
diff --git a/src/core/persistence/sync-pass-episode.ts b/src/core/persistence/sync-pass-episode.ts
index f141632356..375fe06932 100644
--- a/src/core/persistence/sync-pass-episode.ts
+++ b/src/core/persistence/sync-pass-episode.ts
@@ -4,9 +4,11 @@
* **Closure** (§6, dx-outside-1, eng-outside-2). An episode stores its selection, so its universe is `U_E = eligible_E(T)`.
* A converged round closes it in the pass's checkpoint when either (a) `U_E ⊆ U_pass`, a set inclusion over the paths
* of `T`, or (b) the pass is at the configured selection, which releases the paths of `U_E` the source does not sync.
- * `chunker_version` is stamped only when (b) holds and `U_E ⊇ U_config`. So a scoped `--full --exclude X` closes its own
- * episode by (a) without advancing `last_commit`; a plain sync closes any episode by (b); a narrower takeover satisfies
- * neither and leaves it open. There is no abandon command: the plain sync (`resume_argv`) always terminates.
+ * `chunker_version` is stamped only when (b) holds and `U_E ⊇ U_config`. Every page of `U_E` was re-imported by the episode
+ * or admitted without a reparse because it already was current (sync-classify.ts `inPlace`: the import of its blob, at
+ * this parser and chunker, with its derived state current and no hold), so the stamp is true of every page. So a scoped
+ * `--full --exclude X` closes its own episode by (a) without advancing `last_commit`; a plain sync closes any episode by
+ * (b); a narrower takeover satisfies neither and leaves it open. There is no abandon command: the plain sync (`resume_argv`) always terminates.
*
* **Cost** (§7). Each admitted group reserves its priced embedding spend in its own row, `op='sync-pass-cost'`,
* fingerprint `<source>:<incarnation>:<episode_or_pass_id>:<group_id>`, value `{source_id, incarnation, usd}`, inserted
diff --git a/src/core/persistence/sync-pass.ts b/src/core/persistence/sync-pass.ts
index 8215eeba62..aa38ac8723 100644
--- a/src/core/persistence/sync-pass.ts
+++ b/src/core/persistence/sync-pass.ts
@@ -44,6 +44,7 @@ import { buildConvergenceReceipt, readOnlySameContentScreen, type ConvergenceRec
import { readManagedMode } from './ownership.ts';
import { MANAGED_SYNC_PASS_OP, SYNC_ORIGIN_DELETED_OP, type SyncSelection } from './sync-pass-row.ts';
import { faultPoint } from './fault-points.ts';
+import { MARKDOWN_CHUNKER_VERSION } from '../chunkers/recursive.ts';
import { filterYielding, loopYield, progressYield, sortYielding, turnYield, whileActive, type LoopYield } from './sync-yield.ts';
import { countsMatch, proveConfirm, readLevelMark, type ConfirmProof, type LevelMark } from './sync-pass-confirm.ts';
import { domainCache, levelDomain, narrowedWalk, walkAttestations, type LevelDomainCache } from './sync-pass-narrow.ts';
@@ -52,7 +53,10 @@ export { MANAGED_SYNC_PASS_OP, SYNC_ORIGIN_DELETED_OP };
/** What a live pass feeds the level read (sync-pass-run.ts): its episode, the retry paths it already re-screened, and its selection. */
export interface PassLevelInput {
- /** The open episode's `full_id`: a live page whose record carries another is `episodeUnsatisfied`. Null: no full obligation. */
+ /**
+ * The open episode's `full_id`: a live page whose record carries another is `episodeUnsatisfied` (a current one satisfies it
+ * in place, sync-classify.ts `inPlace`, and keeps its record). Null: no full obligation.
+ */
episodeFullId: string | null;
/** Hold paths (source-relative) whose retry row this pass already re-screened: no longer `retried` in its later rounds. */
retryConsumed?: ReadonlySet<string>;
@@ -308,7 +312,7 @@ const recordOids = (record: PageRow['record'], level?: string | null): string[]
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);
-const PAGE_DELETED = 1, PAGE_RECORD = 2, PAGE_REVISION_NULL = 4, PAGE_PARSER_EXPECTED = 8, PAGE_WORKING = 16, PAGE_WORKING_TRUE = 32;
+const PAGE_DELETED = 1, PAGE_RECORD = 2, PAGE_REVISION_NULL = 4, PAGE_PARSER_EXPECTED = 8, PAGE_WORKING = 16, PAGE_WORKING_TRUE = 32, PAGE_DERIVED_CURRENT = 64;
/**
* `I⋈P` as columns, one slot per page, instead of a `PageRow` and a record object per page (about 200 B a page at 600k). A
@@ -324,6 +328,8 @@ export class PageTable {
get length(): number { return this.id.length; }
deleted(i: number): boolean { return (this.bits[i]! & PAGE_DELETED) !== 0; }
hasRecord(i: number): boolean { return (this.bits[i]! & PAGE_RECORD) !== 0; }
+ /** `SyncPathState.derivedCurrent`: the page's chunks, text projection and contextual mode are current (`readPages`). */
+ derivedCurrent(i: number): boolean { return (this.bits[i]! & PAGE_DERIVED_CURRENT) !== 0; }
/** The record's fields `row(i).record` would carry (undefined without a record), read without building it: the classifier's per-path reads. */
diskBlobOidOf(i: number): string | null | undefined { return !this.hasRecord(i) ? undefined : this.diskBlobOid.has(i) ? this.diskBlobOid.get(i)! : this.blobOid[i] ?? null; }
recordRevisionOf(i: number): string | undefined { return !this.hasRecord(i) || this.bits[i]! & PAGE_REVISION_NULL ? undefined : this.recordRevision.get(i) ?? this.revision[i]!; }
@@ -392,7 +398,9 @@ async function readPages(exec: Exec, kind: 'postgres' | 'pglite', sourceId: stri
c.revision IS NULL AS rec_revision_null,
NULLIF(c.revision, p.knowledge_revision::text) AS rec_revision,
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
+ c.full_episode AS rec_full_episode, c.working AS rec_working,
+ (p.page_kind = 'markdown' AND p.chunker_version = ${MARKDOWN_CHUNKER_VERSION} AND p.body_storage IS DISTINCT FROM 'segmented'
+ AND p.text_projection_revision IS NOT DISTINCT FROM p.knowledge_revision AND p.contextual_retrieval_mode IS NOT NULL) AS derived_current
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' AS parser, completed_keys->0->>'full_episode' AS full_episode, (completed_keys->0->'working')::text AS working
@@ -400,7 +408,7 @@ async function readPages(exec: Exec, kind: 'postgres' | 'pglite', sourceId: stri
WHERE p.source_id=$1 AND p.source_path IS NOT NULL`;
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_parser_expected: boolean | 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; derived_current: boolean | null };
const pages = only?.table ?? new PageTable(expected);
const parsers = new Map<string, SyncImportProvenance['parser'] | undefined>(), episodes = new Map<string, string>();
const parserOf = (text: string | null) => {
@@ -413,7 +421,8 @@ async function readPages(exec: Exec, kind: 'postgres' | 'pglite', sourceId: stri
const working = raw.rec_working === 'true' || raw.rec_working === 'false';
pages.id[i] = raw.id; pages.slug[i] = raw.slug; pages.sourcePath[i] = raw.source_path; pages.revision[i] = raw.revision; pages.requestId[i] = raw.request_id;
pages.bits[i] = (raw.deleted ? PAGE_DELETED : 0) | (raw.has_record ? PAGE_RECORD : 0) | (raw.rec_revision_null ? PAGE_REVISION_NULL : 0)
- | (raw.rec_parser_expected ? PAGE_PARSER_EXPECTED : 0) | (working ? PAGE_WORKING : 0) | (raw.rec_working === 'true' ? PAGE_WORKING_TRUE : 0);
+ | (raw.rec_parser_expected ? PAGE_PARSER_EXPECTED : 0) | (working ? PAGE_WORKING : 0) | (raw.rec_working === 'true' ? PAGE_WORKING_TRUE : 0)
+ | (raw.derived_current ? PAGE_DERIVED_CURRENT : 0);
const episode = raw.has_record ? raw.rec_full_episode : null;
if (typeof episode === 'string' && !episodes.has(episode)) episodes.set(episode, episode);
pages.blobOid[i] = raw.has_record ? raw.rec_blob_oid ?? undefined : undefined; pages.fullEpisode[i] = typeof episode === 'string' ? episodes.get(episode)! : episode;
@@ -800,6 +809,7 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: st
spelling: legacySpelling.has(path) ? 'legacy' : twins.has(path),
episodeUnsatisfied: input.pass ? !!input.pass.episodeFullId && live && (record ? pages.fullEpisode[at!] : undefined) !== input.pass.episodeFullId
: input.full === true && live,
+ derivedCurrent: live && pages.derivedCurrent(at!),
fullObligation: input.pass ? !!input.pass.episodeFullId : input.full === true,
keepDb: !mirror && keepDbPrefixes.length > 0 && row && isKeepDbSlug(pages.slug[at!]!, keepDbPrefixes),
openWork: row && open.pageIds.has(Number(pages.id[at!])) || open.origins.has(origin),
@@ -1092,7 +1102,8 @@ export async function passWritesMatched(exec: Exec, input: { kind: 'postgres' |
page: { live: true, revision: pages.revision[i]!, originMatches: syncOriginPath(pages.origin.get(i) ?? sourcePath) === syncOriginPath(sourcePath) },
record: { blobOids: pages.oids(i, level), revision: recordRevision != null ? String(recordRevision) : null, parserSame: sameParser(pages.parserOf(i), parserFor(writer.path)) },
attested: null, levelSha256: null, published: false, hold: null, marker: null, baseline: null, spelling: false,
- episodeUnsatisfied: !!input.episodeFullId && pages.fullEpisode[i] !== input.episodeFullId, fullObligation: !!input.episodeFullId, keepDb: false, openWork: false };
+ episodeUnsatisfied: !!input.episodeFullId && pages.fullEpisode[i] !== input.episodeFullId, derivedCurrent: pages.derivedCurrent(i), fullObligation: !!input.episodeFullId,
+ keepDb: false, openWork: false };
if (classifySyncPath(state).cls !== 'matched') return false;
}
return true;
diff --git a/src/core/persistence/sync-prepare.ts b/src/core/persistence/sync-prepare.ts
index 1e307ead4f..52b34e4b60 100644
--- a/src/core/persistence/sync-prepare.ts
+++ b/src/core/persistence/sync-prepare.ts
@@ -548,6 +548,7 @@ export async function publishPassStamp(tx: BrainEngine, row: WriteRequest, p: Sy
if (pinned) throw syncPublicationRefusal('slug_root_mode_mismatch', `The source pins slug_root_mode=${pinned}, but this sync ran under ${p.slugMode}.`, row, p, slugModeMismatchCause(row.source_id, pinned, p.slugMode));
if (!changed.length) throw syncPublicationRefusal('revision_conflict', CHECKPOINT_MOVED_MESSAGE, row, p,
`Another sync moved the commit checkpoint of ${row.source_id} while this pass published.`);
+ // The closed episode re-imported every page except those already at the current chunker (sync-classify.ts `inPlace`).
if (stamp.stampChunker) await tx.executeRaw('UPDATE sources SET chunker_version=$2 WHERE id=$1', [row.source_id, chunkerStamp()]);
}
await recordPassTail(tx, row.source_id, p.passId!, { changed: read.changed, extract: stamp.extract, ...(stamp.since ? { since: stamp.since } : {}) });
@@ -620,7 +621,8 @@ async function applyManagedSyncCheckpoint(tx: BrainEngine, row: WriteRequest, p:
if (!changed.length) throw syncPublicationRefusal('revision_conflict', CHECKPOINT_MOVED_MESSAGE, row, p,
`Another sync moved the commit checkpoint of ${row.source_id} while this run published.`);
await recordSliceCursor(tx, row, p, settled);
- // #5566: a full walk re-chunked every stale page, so acknowledge the chunker version as the legacy gate does.
+ // #5566: a full walk re-chunked every stale page, so acknowledge the chunker version as the legacy gate does. (A pass's
+ // full episode stamps through its own checkpoint, above; the pages it admits without a reparse are already current.)
if (settled && (p.from === null || p.syncOptions?.full === true)) await tx.executeRaw('UPDATE sources SET chunker_version=$2 WHERE id=$1', [row.source_id, chunkerStamp()]);
await tx.executeRaw("UPDATE op_checkpoints SET completed_keys=jsonb_set(completed_keys,'{0,done}','true'::jsonb),updated_at=now() WHERE op='managed-sync' AND fingerprint=$1", [p.cursorKey]);
// #6712 PR 2 (L5): the tail (statistics, links) runs after the lease is released; it is owed from this commit on.
diff --git a/test/e2e/sync-pass-episode-in-place-postgres.test.ts b/test/e2e/sync-pass-episode-in-place-postgres.test.ts
new file mode 100644
index 0000000000..8a261214ab
--- /dev/null
+++ b/test/e2e/sync-pass-episode-in-place-postgres.test.ts
@@ -0,0 +1,3 @@
+import { registerPostgresTests } from '../helpers/test-backends.ts';
+
+await registerPostgresTests(() => import('../sync-pass-episode-in-place.test.ts'));
diff --git a/test/sync-pass-episode-in-place.test.ts b/test/sync-pass-episode-in-place.test.ts
new file mode 100644
index 0000000000..09188a4f8f
--- /dev/null
+++ b/test/sync-pass-episode-in-place.test.ts
@@ -0,0 +1,130 @@
+/**
+ * A full episode admits a page that is already current without a reparse (sync-classify.ts `inPlace`): the import record
+ * names the committed blob and the page's revision, the parser and chunker the record carries are this binary's, its
+ * chunks, text projection and contextual mode are current (`derivedCurrent`) and no hold names it. Any other page
+ * reparses exactly as before.
+ *
+ * Protects:
+ * - classifier: an all-current page under an open episode is `matched` (it was `reparse`); a stale parser, a stale derived
+ * state, a blob the record does not name, a hold, or a force-import state (a moved origin) each keep today's class.
+ * - classifier, any force-import flag: across every state below, lifting the episode's term turns a path `matched` only
+ * when the same path outside an episode is `matched`. A state that forces an import must keep a page from `matched`
+ * outside an episode to force anything, so it keeps its reparse inside one too.
+ * - full pass: an all-current page gets no request and no record write (its import record stays byte-identical, so it
+ * was neither imported nor waived); a page with a stale parser, a stale chunker version, a stale contextual mode, a
+ * stale text projection, an edited committed file, a re-screen-due hold or a moved origin is re-imported (its record
+ * carries the episode). The pass converges and the episode closes as before.
+ *
+ * Each test fails before this change: there every live page an open episode had not re-imported was `reparse`, so a full
+ * pass re-imported (or waived) the all-current page and rewrote its import record with the episode.
+ *
+ * PGLite here; Postgres through test/e2e/sync-pass-episode-in-place-postgres.test.ts.
+ */
+import { describe, expect, test } from 'bun:test';
+import { execFileSync } from 'node:child_process';
+import { mkdirSync, writeFileSync } from 'node:fs';
+import { join } from 'node:path';
+import type { BrainEngine } from '../src/core/engine.ts';
+import { classifySyncPath, type SyncClass, type SyncPathState } from '../src/core/persistence/sync-classify.ts';
+import { SYNC_IMPORT_PROVENANCE_OP, writeGitHold } from '../src/core/persistence/sync-holds.ts';
+import { performManagedSync } from '../src/core/persistence/sync-run.ts';
+import { RECOVERY_VERSION } from '../src/core/markdown.ts';
+import { managedBrain } from './helpers/managed-brain.ts';
+import { testBackends } from './helpers/test-backends.ts';
+
+const base: SyncPathState = { mode: 'committed', tree: 'A', level: 'A', present: true, dirty: 'clean', diskOid: null,
+ page: { live: true, revision: 'R1', originMatches: true }, record: { blobOids: ['A'], revision: 'R1', parserSame: true },
+ attested: null, levelSha256: null, published: false, hold: null, marker: null, baseline: null, spelling: false,
+ episodeUnsatisfied: true, derivedCurrent: true, fullObligation: true, keepDb: false, openWork: false };
+const at = (patch: Partial<SyncPathState>): SyncPathState => ({ ...base, ...patch });
+const rescreenHold = { atLevel: true, rescreenDue: true, retried: false };
+
+describe('classifier: a current page satisfies an open episode in place', () => {
+ test('an all-current page is matched, with nothing to do', () => {
+ expect(classifySyncPath(at({}))).toMatchObject({ cls: 'matched', settled: true, action: 'none' });
+ });
+
+ const stale: Array<[string, Partial<SyncPathState>, SyncClass]> = [
+ ['a stale parser', { record: { blobOids: ['A'], revision: 'R1', parserSame: false } }, 'reparse'],
+ ['a stale derived state (chunker, projection or contextual mode)', { derivedCurrent: false }, 'reparse'],
+ ['an unknown derived state', { derivedCurrent: undefined }, 'reparse'],
+ ['a committed blob the record does not name', { tree: 'B', level: 'B' }, 'import'],
+ ['a re-screen-due hold', { hold: rescreenHold }, 'reparse'],
+ ['a force-import state (the origin moved)', { page: { live: true, revision: 'R1', originMatches: false } }, 'reparse'],
+ ];
+ for (const [name, patch, cls] of stale) test(`${name} keeps today's class (${cls})`, () => {
+ expect(classifySyncPath(at(patch)).cls).toBe(cls);
+ expect(classifySyncPath(at(patch))).toEqual(classifySyncPath(at({ ...patch, derivedCurrent: false })));
+ });
+
+ test('lifting the episode turns a path matched only when the same path outside an episode is matched', () => {
+ const variants: Array<Partial<SyncPathState>> = [{}, ...stale.map(([, patch]) => patch),
+ { record: { blobOids: ['A'], revision: 'R0', parserSame: true } }, { hold: { atLevel: true, rescreenDue: false, retried: false } },
+ { spelling: true }, { keepDb: true }, { openWork: true }, { page: { live: false, revision: 'R1', originMatches: true } },
+ { attested: { hash: 'h', arm1: true }, levelSha256: 'h', tree: 'B', level: 'B' }, { mode: 'working', dirty: null }];
+ for (const patch of variants) for (const strict of [false, true]) {
+ const inside = classifySyncPath(at(patch), { strict }), before = classifySyncPath(at({ ...patch, derivedCurrent: false }), { strict });
+ const outside = classifySyncPath(at({ ...patch, episodeUnsatisfied: false }), { strict });
+ if (inside.cls !== before.cls) expect({ patch, strict, outside: outside.cls, inside: inside.cls }).toEqual({ patch, strict, outside: 'matched', inside: 'matched' });
+ }
+ });
+});
+
+const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();
+const commit = (root: string, message: string) => { git(root, 'add', '-A'); git(root, '-c', 'user.name=Example', '-c', '[email protected]', 'commit', '-qm', message); };
+const note = (title: string, body: string) => `---\ntype: concept\ntitle: ${title}\n---\n\n${body}\n`;
+const OPTS = { sourceId: 'default', noPull: true, noEmbed: true, noExtract: true };
+const CASES = ['current', 'parser', 'chunker', 'contextual', 'projection', 'edited', 'held', 'origin'] as const;
+const setup = ({ root }: { root: string }) => {
+ mkdirSync(join(root, 'notes'), { recursive: true }); git(root, 'init', '-q');
+ for (const name of CASES) writeFileSync(join(root, `notes/${name}.md`), note(`notes/${name}`, `The committed body of notes/${name}.`));
+ commit(root, 'notes');
+};
+const pageId = async (engine: BrainEngine, slug: string) => Number((await engine.readPageSnapshot(slug, { sourceId: 'default' }))!.page.id);
+const recordOf = async (engine: BrainEngine, slug: string) => (await engine.executeRaw<{ keys: string; updated_at: string; episode: string | null }>(
+ `SELECT completed_keys::text AS keys, updated_at::text AS updated_at, completed_keys->0->>'full_episode' AS episode FROM op_checkpoints WHERE op=$1 AND fingerprint LIKE '%:' || $2`,
+ [SYNC_IMPORT_PROVENANCE_OP, String(await pageId(engine, slug))]))[0]!;
+const requestsFor = async (engine: BrainEngine, slug: string) => Number((await engine.executeRaw<{ n: number }>(
+ "SELECT count(*)::int AS n FROM persistence_requests WHERE source_id='default' AND slug=$1", [slug]))[0]!.n);
+const setRecord = (engine: BrainEngine, slug: string, path: string, value: unknown) => pageId(engine, slug).then(id => engine.executeRaw(
+ `UPDATE op_checkpoints SET completed_keys=jsonb_set(completed_keys, $3::text[], $4::text::jsonb) WHERE op=$1 AND fingerprint LIKE '%:' || $2`,
+ [SYNC_IMPORT_PROVENANCE_OP, String(id), `{0,${path}}`, JSON.stringify(value)]));
+
+for (const backend of testBackends()) {
+ const databaseUrl = backend === 'postgres' ? process.env.DATABASE_URL : undefined;
+ const brain = (run: Parameters<typeof managedBrain>[0]) => managedBrain(run, { ...(databaseUrl ? { databaseUrl } : {}), setup });
+
+ describe(`full pass: a current page is admitted without a reparse (${backend})`, () => {
+ test('the all-current page is neither imported nor waived; every stale page is re-imported', () => brain(async ({ engine, root }) => {
+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');
+ const [{ incarnation }] = await engine.executeRaw<{ incarnation: string }>("SELECT incarnation::text FROM sources WHERE id='default'");
+ await setRecord(engine, 'notes/parser', 'parser', { parser_version: 0, markdown_chunker: 0, schema_pack: null });
+ await engine.executeRaw("UPDATE pages SET chunker_version=3 WHERE source_id='default' AND slug='notes/chunker'");
+ await engine.executeRaw("UPDATE pages SET contextual_retrieval_mode=NULL WHERE source_id='default' AND slug='notes/contextual'");
+ await engine.executeRaw("UPDATE pages SET text_projection_revision=NULL WHERE source_id='default' AND slug='notes/projection'");
+ await setRecord(engine, 'notes/origin', 'origin', 'notes/elsewhere.md');
+ writeFileSync(join(root, 'notes/edited.md'), note('notes/edited', 'An edited body of notes/edited.')); commit(root, 'edit');
+ const held = (await engine.readPageSnapshot('notes/held', { sourceId: 'default' }))!;
+ await engine.transaction(tx => writeGitHold(tx, { source_id: 'default', incarnation, path: 'notes/held.md', source_path: 'notes/held.md', slug: 'notes/held', page_id: Number(held.page.id),
+ code: 'frontmatter_slug_conflict', message: 'Synthetic re-screen-due hold.', upstream_version: null, observed_at: new Date().toISOString(), run_id: null, mode: 'managed',
+ meta: { recovery_version: RECOVERY_VERSION, blob_oid: git(root, 'rev-parse', 'HEAD:notes/held.md'), page_revision: held.revision } } as never));
+ const before = Object.fromEntries(await Promise.all(CASES.map(async name => [name, { record: await recordOf(engine, `notes/${name}`), requests: await requestsFor(engine, `notes/${name}`) }] as const)));
+
+ const run = await performManagedSync(engine, { ...OPTS, full: true });
+ expect(run.status).toBe('synced');
+ const [pass] = await engine.executeRaw<{ episode: unknown }>("SELECT completed_keys->0->'obligations'->'episode' AS episode FROM op_checkpoints WHERE op='managed-sync-pass'");
+ expect(pass?.episode ?? null).toBeNull();
+
+ const current = await recordOf(engine, 'notes/current');
+ expect({ record: current, requests: await requestsFor(engine, 'notes/current') }).toEqual(before.current);
+ for (const name of CASES.filter(name => name !== 'current')) {
+ const after = await recordOf(engine, `notes/${name}`);
+ expect({ name, episode: after.episode !== null, rewritten: after.keys !== before[name].record.keys }).toEqual({ name, episode: true, rewritten: true });
+ }
+ const [chunker] = await engine.executeRaw<{ v: number }>("SELECT chunker_version AS v FROM pages WHERE source_id='default' AND slug='notes/chunker'");
+ expect(Number(chunker!.v)).toBeGreaterThan(3);
+ expect((await engine.getPage('notes/edited', { sourceId: 'default' }))?.compiled_truth).toContain('An edited body');
+ expect((await performManagedSync(engine, OPTS)).status).toBe('up_to_date');
+ }));
+ });
+}