Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Purged change reasons can no longer come back through a late sync import (#8635) (#9562)

Garry Tan59e9c242b658
diff --git a/behavior-changes.d/purged_change_reason_8635.md b/behavior-changes.d/purged_change_reason_8635.md
new file mode 100644
index 0000000000..20a870c3b0
--- /dev/null
+++ b/behavior-changes.d/purged_change_reason_8635.md
@@ -0,0 +1,4 @@
+---
+kind: fix
+---
+A sync that read a page's change reason from a gbrain-reason commit trailer, then saved the page after a fact purge removed that text, used to store the purged text in the page's history as the reason for the change. The save now checks for the purge when it writes the history row and stores no reason instead; the page content still imports, and other files' reasons from the same commit are kept. Nothing to do. Reasons already stored before this release are not changed.
diff --git a/changelog.d/fix-purged-change-reason-8635.md b/changelog.d/fix-purged-change-reason-8635.md
new file mode 100644
index 0000000000..937a4c4fc5
--- /dev/null
+++ b/changelog.d/fix-purged-change-reason-8635.md
@@ -0,0 +1,9 @@
+---
+important: security
+---
+**A purged claim no longer comes back as a page's change reason when a sync publishes after the purge.**
+
+### Fixed
+- A sync reads each page's change reason from its `gbrain-reason` commit trailer before it saves the page. If a fact purge committed in between, the saved page history still stored the purged text as that page's reason. The save now checks the purge's tombstone for the exact commit and file when it writes the history row, so the reason is stored empty, while the page content still imports and other files' reasons from the same commit are kept. This applies to classic and managed sync (#8635, #8591).
+
+<!-- behavior-change: purged_change_reason_8635 -->
diff --git a/docs/architecture/key-files/page-history.md b/docs/architecture/key-files/page-history.md
index 207b1b74eb..2b1d1c56a5 100644
--- a/docs/architecture/key-files/page-history.md
+++ b/docs/architecture/key-files/page-history.md
@@ -5,6 +5,6 @@ Current behavior and load-bearing invariants; history belongs in Git and CHANGEL
Page-write reasons: admission, the snapshot column, the Git trailer and the remote read projection.
- `src/core/persistence/change-reason.ts` — GBRA-217 page-write reasons ([page history](../../guides/page-history.md#reasons)). `admitChangeReason` canonicalises a `reason` (one line, at most 500 code points) and refuses `change_reason_rejected` (`reason_too_long`, `reason_contains_secret` via `scanTextSpans`) before admission, so no journal row ever holds a refused reason; `page-mutations.ts` runs it before `callerIntent`, so the canonical reason joins replay identity, and `page-batch.ts` runs it per item (all or nothing). `withChangeReason` wraps a page preparer in `service.ts`: its apply stamps `page_versions.change_reason` on the snapshots that transaction created for the page, and, for a world-visible page whose file is published, appends `gbrain-reason: <path>: <text>` to `prepared.file.commit` (a `GitCommitNote` with `trailers[]`), which both the door (`commitThroughDoor`) and the effect commit (`commitMessage`, one trailer line each, `commitMessageArgs` switching to `-F` over 8 KiB) read after apply. The outcome's `reason_recorded.git` is `committed` (strict), `queued`, `private`, `not_file_backed`, `no_repository` (repo-less) or `none`; `settledReasonRecorded` turns `queued` into `superseded` with a `reason_not_committed` notice on the receipt when the Git effect was superseded. Read side: `replacedBecauseSql` in `engine-sql/pages.ts` (remote readers see a reason only when the successor snapshot is world-visible and not `successor_pruned`, which the prunes in `page-state/versions.ts` set). Tests: `test/page-change-reason-gbra217.test.ts`, `test/page-change-reason-git-gbra217.test.ts`, `test/page-change-reason-mcp-gbra217.test.ts`.
-- `src/core/reason-trailers.ts` — GBRA-217 reasons read back from Git ([reasons from Git](../../guides/page-history.md#reasons-from-git)). `readReasonTrailers` streams one `git log` through `hardenedGitStream` with `REASON_LOG_OPTIONS` (`hardened-git.ts`: `-z --first-parent --diff-merges=first-parent --root --name-only --no-renames` and a `%x00%H%x00%(trailers:key=gbrain-reason,valueonly,unfold)` format; no pathspec, no grep), parses records as they stream and stops at a commit cap. `scanSyncReasons` maps each repository path to its newest first-parent commit's trailer (canonicalised by `canonicalChangeReason`, over-long ones counted as malformed, secrets via `scanTextSpans` reported by commit and path, `reason_purge_tombstones` pairs skipped) and counts touches per path; `importHistoryFor` turns that into the `VersionHistory` an import passes to `createVersion` (more than one commit, or a failed or capped scan, sets `historyGap`). `gitTopLevel` returns null outside a repository, so a repo-less or plain-folder source runs no scan. `reasonScanNotices` builds `reason_scan_failed` / `reason_scan_capped` / `reason_trailer_secret`. `cachedSyncReasonScan` shares one scan per (source, root, range) for managed preparations; purge clears it. `scanTrailersCarrying` is purge's read (cap 200,000, or the user's `--reason-scan-limit`). Caps 10,000 (sync) and 200,000 (purge), 120 s timeout. Seams: `__setReasonScanCapsForTests`, `__setReasonGitStreamForTests`. Test: `test/reason-trailers-gbra217.test.ts` (both engines).
+- `src/core/reason-trailers.ts` — GBRA-217 reasons read back from Git ([reasons from Git](../../guides/page-history.md#reasons-from-git)). `readReasonTrailers` streams one `git log` through `hardenedGitStream` with `REASON_LOG_OPTIONS` (`hardened-git.ts`: `-z --first-parent --diff-merges=first-parent --root --name-only --no-renames` and a `%x00%H%x00%(trailers:key=gbrain-reason,valueonly,unfold)` format; no pathspec, no grep), parses records as they stream and stops at a commit cap. `scanSyncReasons` maps each repository path to its newest first-parent commit's trailer (canonicalised by `canonicalChangeReason`, over-long ones counted as malformed, secrets via `scanTextSpans` reported by commit and path, `reason_purge_tombstones` pairs skipped) and counts touches per path; `importHistoryFor` turns that into the `VersionHistory` an import passes to `createVersion` (more than one commit, or a failed or capped scan, sets `historyGap`), with the deciding commit and path as `reasonSource`, so the snapshot insert rechecks that pair's tombstone under the page guard (#8635). `gitTopLevel` returns null outside a repository, so a repo-less or plain-folder source runs no scan. `reasonScanNotices` builds `reason_scan_failed` / `reason_scan_capped` / `reason_trailer_secret`. `cachedSyncReasonScan` shares one scan per (source, root, range) for managed preparations; purge clears it. `scanTrailersCarrying` is purge's read (cap 200,000, or the user's `--reason-scan-limit`). Caps 10,000 (sync) and 200,000 (purge), 120 s timeout. Seams: `__setReasonScanCapsForTests`, `__setReasonGitStreamForTests`. Test: `test/reason-trailers-gbra217.test.ts` (both engines).
- `src/commands/sync/reasons.ts` + `src/core/persistence/sync-reasons.ts` — the sync wiring. Classic: `incremental.ts` scans `last_commit..pin` once per pass when it imports or renames files (not for a company brain), `full.ts` scans the whole history only when the source already has pages, and `imports.ts` / `renames.ts` / `runImport` pass each file's `history` to `importFile`; `withReasonNotices` puts the notices and `reason_trailers_malformed` on the `SyncResult` (`sync/run.ts` renders them). Managed: `sync-prepare.ts` calls `managedFileHistory` (claim step `reason_scan`) only when the page exists, and `performManagedPass` adds the pass's notices through `withManagedReasonNotices`. Content never waits on the scan.
- `src/core/facts/purge-reasons.ts` — A7 ([purge and reasons](../../guides/page-history.md#purge-and-reasons)). `scanPurgeTrailers` runs before the purge transaction (no checkout: nothing to scan; a failed read refuses `reason_purge_scan_failed`, `fix.next = wait`; over the cap `reason_purge_scan_capped`, ask_user with `--reason-scan-limit`). Inside the transaction `redactGitEffectReasons` rewrites `commit_line` / `commit_trailers` / legacy `commit_trailer` on queued, failed and committed git effects (a running one refuses `purge_blocked_pending_recovery`) and `recordReasonTombstones` rechecks `head..HEAD` and inserts text-free `reason_purge_tombstones` rows. `purge.ts` `redactPageVersions` sets a quoting `change_reason` to `[purged]`; `purge-verify.ts` checks it and reports pushed commits as the `git_commit_messages` residual.
diff --git a/docs/guides/page-history.md b/docs/guides/page-history.md
index bf0b6837f8..f398804058 100644
--- a/docs/guides/page-history.md
+++ b/docs/guides/page-history.md
@@ -169,7 +169,8 @@ git log --first-parent --format='%H %(trailers:key=gbrain-reason,valueonly)' <ra
```
A commit and file that a purge has tombstoned (below) never gives a reason
-again, not even on a full re-import.
+again, not even on a full re-import, or a sync that read the trailer before
+the purge and saved the page after it.
A source with no Git checkout (a repo-less brain, or a folder imported with
`gbrain import`) reads no trailers, prints none of these notices and marks no
diff --git a/src/core/page-state/versions.ts b/src/core/page-state/versions.ts
index f50fe5107b..8aeca6a3a6 100644
--- a/src/core/page-state/versions.ts
+++ b/src/core/page-state/versions.ts
@@ -12,10 +12,23 @@ export const HOLDING_PAGE_VERSIONS_PRUNED_PER_WRITE = 100;
/**
* GBRA-217: what a sync import knows about the write a snapshot records: the reason Git gives for it
- * (`page_versions.change_reason`), and whether the import consumed more than one commit for the page's file, or could
- * not tell (`pages.version_history_gap_at`).
+ * (`page_versions.change_reason`), the commit and repository path that reason came from, and whether the import consumed
+ * more than one commit for the page's file, or could not tell (`pages.version_history_gap_at`).
*/
-export interface VersionHistory { reason?: string | null; historyGap?: boolean }
+export interface VersionHistory { reason?: string | null; reasonSource?: { commit: string; path: string }; historyGap?: boolean }
+
+/**
+ * #8635: a Git reason is read before its publication, so a purge may tombstone its (commit, path) in between. The snapshot
+ * insert reads the tombstone again under the page guard, whose `sources FOR SHARE` waits for a purge holding the source
+ * `FOR UPDATE`: either the purge committed first and this read sees its tombstone, or it waits for this commit and
+ * redacts the stamped reason itself.
+ */
+async function stampableReason(tx: BrainEngine, sourceId: string, history: VersionHistory): Promise<string | null> {
+ if (!history.reason || !history.reasonSource) return history.reason ?? null;
+ const purged = await tx.executeRaw('SELECT 1 FROM reason_purge_tombstones WHERE source_id = $1 AND commit_sha = $2 AND path = $3',
+ [sourceId, history.reasonSource.commit, history.reasonSource.path]);
+ return purged.length ? null : history.reason;
+}
/**
* Version the same canonical view an API reader receives, including withdrawals.
@@ -26,11 +39,12 @@ export async function createPageVersion(engine: BrainEngine, slug: string, sourc
const insert = async (tx: BrainEngine, snapshot: PageSnapshot) => {
const { page, tags, revision } = snapshot;
if (history.historyGap) await tx.executeRaw('UPDATE pages SET version_history_gap_at = now() WHERE id = $1', [page.id]);
- if (isSegmentedPage(page)) return insertSegmentedVersion(tx, snapshot, history.reason ?? null);
+ const reason = await stampableReason(tx, sourceId, history);
+ if (isSegmentedPage(page)) return insertSegmentedVersion(tx, snapshot, reason);
const rows = await tx.executeRaw<PageVersion>(`INSERT INTO page_versions
(page_id,compiled_truth,frontmatter,knowledge_revision,timeline,title,type,tags,is_deleted,source_path,change_reason)
VALUES ($1,$2,$3::text::jsonb,$4::uuid,$5,$6,$7,$8::text::jsonb,$9,$10,$11) RETURNING *`,
- [page.id, page.compiled_truth, JSON.stringify(page.frontmatter), revision, page.timeline, page.title, page.type, JSON.stringify(tags), page.deleted_at != null, page.source_path ?? null, history.reason ?? null]);
+ [page.id, page.compiled_truth, JSON.stringify(page.frontmatter), revision, page.timeline, page.title, page.type, JSON.stringify(tags), page.deleted_at != null, page.source_path ?? null, reason]);
if (isLargePageBytes(Buffer.byteLength(page.compiled_truth ?? '', 'utf-8') + Buffer.byteLength(page.timeline ?? '', 'utf-8'))) {
const pruned = await tx.executeRaw<{ id: number }>(`DELETE FROM page_versions WHERE page_id = $1 AND id IN (SELECT id FROM page_versions WHERE page_id = $1
AND octet_length(compiled_truth) + octet_length(COALESCE(timeline, '')) > $2 ORDER BY id DESC OFFSET $3) RETURNING id`,
diff --git a/src/core/reason-trailers.ts b/src/core/reason-trailers.ts
index 67f84b8c8c..7de05f27cf 100644
--- a/src/core/reason-trailers.ts
+++ b/src/core/reason-trailers.ts
@@ -213,15 +213,17 @@ function repoPathOf(scan: SyncReasonScan, filePath: string): string {
}
/**
- * What the snapshot an import of one file (absolute path) archives carries: the deciding reason, and a history gap when
+ * What the snapshot an import of one file (absolute path) archives carries: the deciding reason with the commit and path it
+ * came from (the snapshot insert rechecks that pair's tombstone, #8635), and a history gap when
* the range held more than one commit for the file or the scan could not say. Nothing when the pass ran no scan (no Git
* checkout).
*/
export function importHistoryFor(scan: SyncReasonScan | null | undefined, filePath: string): VersionHistory | undefined {
if (!scan) return undefined;
if (scan.status !== 'complete') return { reason: null, historyGap: true };
- const decision = scan.byPath.get(repoPathOf(scan, filePath));
- return { reason: decision?.reason ?? null, historyGap: (decision?.commits ?? 0) > 1 };
+ const path = repoPathOf(scan, filePath);
+ const decision = scan.byPath.get(path);
+ return { reason: decision?.reason ?? null, ...(decision?.reason ? { reasonSource: { commit: decision.commit, path } } : {}), historyGap: (decision?.commits ?? 0) > 1 };
}
/** The pass's notices: a failed or capped scan (reasons stored as NULL, no backfill), and secrets found in pushed trailers. */
@@ -253,8 +255,8 @@ const CACHE_TTL_MS = 10 * 60_000;
/**
* Managed sync prepares one request at a time, each naming its pass's range: the pass's scan runs once per process and
* its later requests reuse it. The key carries the source's purge epoch, which every fact purge bumps in the transaction
- * that writes its tombstones, so a purge from any process makes the next request scan again and read them. The cache is
- * per engine: the scan holds that brain's tombstones, so another brain on the same checkout never reuses it.
+ * that writes its tombstones, so a purge from any process makes the next request scan again and read them; a request
+ * prepared before the purge is caught when its snapshot is inserted (`createPageVersion`). The cache is per engine: the scan holds that brain's tombstones, so another brain on the same checkout never reuses it.
*/
export async function cachedSyncReasonScan(engine: BrainEngine, input: Parameters<typeof scanSyncReasons>[1]): Promise<SyncReasonScan | null> {
const [source] = await engine.executeRaw<{ purge_epoch: string | number }>(SOURCE_PURGE_EPOCH_SQL, [input.sourceId]);
diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txt
index c111428d60..5de6205db0 100644
--- a/test/postgres-unit-arms.txt
+++ b/test/postgres-unit-arms.txt
@@ -562,6 +562,7 @@ test/purge-root-metafile-7613.test.ts
test/purge-scrub-followups-9068.test.ts
test/purge-slug-stores-9146.test.ts
test/purge-sync-tombstone-hold-7130.test.ts
+test/purged-change-reason-8635.test.ts
test/qm-attest-write-boundary.test.ts
test/qm-memory-propose.test.ts
test/qm-memory-tools.test.ts
diff --git a/test/purged-change-reason-8635.test.ts b/test/purged-change-reason-8635.test.ts
new file mode 100644
index 0000000000..0cddf34f12
--- /dev/null
+++ b/test/purged-change-reason-8635.test.ts
@@ -0,0 +1,200 @@
+/**
+ * #8635 (#8591): a change reason read from a Git trailer before a fact purge commits never reaches page history after it.
+ *
+ * 1. Protects: a sync pass, classic or managed, scans its range's `gbrain-reason` trailers (the managed preparation
+ * keeps that scan per request), then publishes each file. A purge committing between the scan and the publication
+ * tombstones the (commit, path) the reason came from; the snapshot the publication archives must not carry the purged
+ * text. The content still imports, and an unrelated reason from the same commit still lands on its own page.
+ * 2. Fails when: the archived snapshot's `change_reason` holds the purged claim, the edit is not imported, or the sibling
+ * path's reason is lost.
+ * 3. Real temp Git repositories, the real classic and managed sync and the real purge_fact handler; PGLite, plus
+ * Postgres when DATABASE_URL is set. The seams only choose when the real purge runs: classic, from the reason scan's
+ * Git runner once Git has answered; managed, at the consumer's `consumer:prepared` fault point, after the request
+ * was prepared from the pass's cached scan and before its publication.
+ */
+import { afterAll, afterEach, beforeAll, describe, expect, test } from 'bun:test';
+import { execFileSync } from 'node:child_process';
+import { randomUUID } from 'node:crypto';
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import type { BrainEngine } from '../src/core/engine.ts';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import type { OperationContext } from '../src/core/ops/contract.ts';
+import { operations } from '../src/core/operations.ts';
+import { importFromContent } from '../src/core/import-file.ts';
+import { runExtractFacts } from '../src/core/cycle/extract-facts.ts';
+import { registerLocalWriter } from '../src/core/persistence/identity.ts';
+import { runSources } from '../src/commands/sources.ts';
+import { performSync } from '../src/commands/sync.ts';
+import { claimWorktree } from '../src/core/persistence/ownership.ts';
+import { performManagedSync } from '../src/core/persistence/sync-run.ts';
+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
+import { hardenedGitStream } from '../src/core/hardened-git.ts';
+import { __setReasonGitStreamForTests, clearReasonScanCache } from '../src/core/reason-trailers.ts';
+import { installFaultHook } from '../src/core/persistence/fault-points.ts';
+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';
+import { withEnv } from './helpers/with-env.ts';
+
+const home = mkdtempSync(join(tmpdir(), 'gbrain-purged-reason-'));
+const engines: BrainEngine[] = [];
+let closePostgres: (() => Promise<void>) | undefined;
+let serial = 0;
+
+const page = (body: string) => `---\ntitle: Note\ntype: note\n---\n\n${body}\n`;
+const SIBLING = 'Routine tidy-up.';
+
+function git(root: string, ...args: string[]): string {
+ return execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();
+}
+function write(root: string, files: Record<string, string>): void {
+ for (const [path, body] of Object.entries(files)) { mkdirSync(join(root, path, '..'), { recursive: true }); writeFileSync(join(root, path), body); }
+}
+function commit(root: string, message: string, reasons: string[] = []): string {
+ git(root, 'add', '-A');
+ git(root, '-c', 'user.name=Example', '-c', '[email protected]', 'commit', '-q', '-m', message,
+ ...reasons.flatMap(reason => ['--trailer', `gbrain-reason: ${reason}`]));
+ return git(root, 'rev-parse', 'HEAD');
+}
+function newRepo(files: Record<string, string>): string {
+ const root = join(home, `repo-${++serial}`);
+ mkdirSync(root);
+ git(root, 'init', '-q', '-b', 'main');
+ write(root, files);
+ commit(root, 'initial content');
+ return root;
+}
+
+const ctx = (engine: BrainEngine, sourceId: string): OperationContext => ({ engine, sourceId, remote: false, dryRun: false,
+ config: { engine: engine.kind }, logger: { info() {}, warn() {}, error() {} } } as OperationContext);
+const run = (engine: BrainEngine, sourceId: string, name: string, params: Record<string, unknown>) =>
+ operations.find(o => o.name === name)!.handler(ctx(engine, sourceId), params) as Promise<Record<string, any>>;
+
+const factFence = (claim: string) => `<!--- gbrain:facts:begin -->
+| # | claim | kind | confidence | visibility | notability | valid_from | valid_until | source | context |
+|---|-------|------|------------|------------|------------|------------|-------------|--------|---------|
+| 1 | ${claim} | fact | 1.0 | world | medium | 2026-01-01 | | chat | |
+<!--- gbrain:facts:end -->`;
+/** A database-only page holding the claim as a fact, so purge_fact has a target in the source. */
+async function seedFact(engine: BrainEngine, sourceId: string, slug: string, claim: string): Promise<number> {
+ await importFromContent(engine, slug, `---\ntitle: Holder\ntype: person\n---\n# Holder\n\nProse.\n\n## Facts\n\n${factFence(claim)}\n`, { noEmbed: true, sourceId });
+ await runExtractFacts(engine, { slugs: [slug], sourceId });
+ const [row] = await engine.executeRaw<{ id: number }>('SELECT id FROM facts WHERE fact = $1 AND entity_slug = $2', [claim, slug]);
+ return Number(row!.id);
+}
+async function purge(engine: BrainEngine, sourceId: string, id: number): Promise<Record<string, any>> {
+ const dry = await run(engine, sourceId, 'purge_fact', { id, source_id: sourceId, dry_run: true });
+ return run(engine, sourceId, 'purge_fact', { id, source_id: sourceId, confirm: dry.confirm_token, expected_revision: dry.expected_revision, request_id: randomUUID() });
+}
+
+/** Classic: the next reason scan reads Git, then the purge runs to its commit before the scan's result reaches the pass. */
+function purgeAfterNextScan(purgeNow: () => Promise<Record<string, any>>): () => Promise<Record<string, any>> {
+ let started: Promise<Record<string, any>> | null = null;
+ __setReasonGitStreamForTests(async (root, args, opts) => {
+ const result = await hardenedGitStream(root, args, opts);
+ if (!started) { started = purgeNow(); await started; }
+ return result;
+ });
+ return () => started ?? Promise.reject(new Error('no reason scan ran'));
+}
+/** Managed: the purge runs to its commit once the request for `slug` is prepared, before its publication starts. */
+function purgeOncePrepared(engine: BrainEngine, slug: string, purgeNow: () => Promise<Record<string, any>>): () => Promise<Record<string, any>> {
+ let started: Promise<Record<string, any>> | null = null;
+ installFaultHook(async (point, detail) => {
+ if (started || point !== 'consumer:prepared' || !detail.requestId) return;
+ const [row] = await engine.executeRaw<{ slug: string }>('SELECT slug FROM persistence_requests WHERE request_id = $1::uuid', [detail.requestId]);
+ if (row?.slug !== slug) return;
+ started = purgeNow();
+ await started;
+ });
+ return () => started ?? Promise.reject(new Error(`no request for ${slug} was prepared`));
+}
+
+async function reasonsOf(engine: BrainEngine, sourceId: string, slug: string): Promise<Array<string | null>> {
+ return (await engine.executeRaw<{ reason: string | null }>(`SELECT v.change_reason AS reason FROM page_versions v JOIN pages p ON p.id = v.page_id
+ WHERE p.source_id = $1 AND p.slug = $2 ORDER BY v.id`, [sourceId, slug])).map(row => row.reason);
+}
+async function bodyOf(engine: BrainEngine, sourceId: string, slug: string): Promise<string> {
+ const [row] = await engine.executeRaw<{ body: string }>('SELECT compiled_truth AS body FROM pages WHERE source_id = $1 AND slug = $2', [sourceId, slug]);
+ return row?.body ?? '';
+}
+async function expectNoPurgedReason(engine: BrainEngine, sourceId: string, claim: string): Promise<void> {
+ expect(await engine.executeRaw(`SELECT v.change_reason FROM page_versions v JOIN pages p ON p.id = v.page_id
+ WHERE p.source_id = $1 AND strpos(lower(v.change_reason), $2) > 0`, [sourceId, claim.toLowerCase()])).toEqual([]);
+ expect([null, '[purged]']).toContain((await reasonsOf(engine, sourceId, 'notes/a')).at(-1) ?? null);
+ expect(await bodyOf(engine, sourceId, 'notes/a')).toContain('A two.');
+ expect(await bodyOf(engine, sourceId, 'notes/b')).toContain('B two.');
+ expect((await reasonsOf(engine, sourceId, 'notes/b')).at(-1)).toBe(SIBLING);
+}
+
+const inHome = <T>(fn: () => Promise<T>) => withEnv({ GBRAIN_HOME: home }, fn);
+
+beforeAll(async () => inHome(async () => {
+ const lite = new PGLiteEngine(); await lite.connect({}); await lite.initSchema(); engines.push(lite);
+ if (process.env.DATABASE_URL) {
+ const pg = await isolatedPersistencePostgres(process.env.DATABASE_URL);
+ engines.push(pg.engine); closePostgres = pg.close;
+ }
+ for (const engine of engines) await registerLocalWriter(engine, 'cli');
+}), 180_000);
+afterEach(() => { __setReasonGitStreamForTests(); installFaultHook(undefined); clearReasonScanCache(); });
+afterAll(async () => {
+ await inHome(async () => { for (const engine of engines) await disposePersistenceConsumer(engine).catch(() => undefined); });
+ await engines[0]?.disconnect();
+ await closePostgres?.();
+ rmSync(home, { recursive: true, force: true });
+});
+
+for (const backend of ['pglite', 'postgres'] as const) {
+ const engineOf = () => engines.find(engine => engine.kind === backend)!;
+ describe(`[${backend}] #8635: a purge that commits after a sync read its reasons`, () => {
+ test.skipIf(backend === 'postgres' && !process.env.DATABASE_URL)('classic sync: the pass scanned before the purge; the archived snapshot never carries the purged reason', async () => inHome(async () => {
+ const engine = engineOf();
+ const n = ++serial;
+ const claim = `Vault code is ${7100 + n}`;
+ const id = `pr-classic-${n}`;
+ const root = newRepo({ 'notes/a.md': page('A one.'), 'notes/b.md': page('B one.') });
+ await runSources(engine, ['add', id, '--path', root, '--no-federated']);
+ const sync = () => performSync(engine, { repoPath: root, sourceId: id, noPull: true, noEmbed: true, noExtract: true, concurrency: 1 });
+ await sync();
+ write(root, { 'notes/a.md': page('A two.'), 'notes/b.md': page('B two.') });
+ const pushed = commit(root, 'edit', [`notes/a.md: Changed after ${claim} was rotated.`, `notes/b.md: ${SIBLING}`]);
+ const fact = await seedFact(engine, id, `people/vault-example-${n}`, claim);
+
+ const purged = purgeAfterNextScan(() => purge(engine, id, fact));
+ await sync();
+ expect((await purged()).state).toBe('committed');
+ expect(await engine.executeRaw('SELECT commit_sha, path FROM reason_purge_tombstones WHERE source_id = $1', [id]))
+ .toEqual([{ commit_sha: pushed, path: 'notes/a.md' }]);
+ await expectNoPurgedReason(engine, id, claim);
+ }), 180_000);
+
+ test.skipIf(backend === 'postgres' && !process.env.DATABASE_URL)('managed sync: a preparation that kept its scan publishes after the purge without the purged reason', async () => inHome(async () => {
+ const engine = engineOf();
+ const n = ++serial;
+ const claim = `Alarm phrase is ${8200 + n}`;
+ const id = `pr-managed-${n}`;
+ const root = newRepo({ 'notes/a.md': page('A one.'), 'notes/b.md': page('B one.') });
+ await engine.executeRaw('UPDATE persistence_brain SET enabled = false WHERE singleton = 1');
+ await engine.executeRaw(`INSERT INTO sources(id, name, local_path, config) VALUES ($1, $1, $2, '{}')`, [id, root]);
+ const fact = await seedFact(engine, id, `people/alarm-example-${n}`, claim);
+ await claimWorktree(engine, id, root);
+ await engine.executeRaw('UPDATE persistence_brain SET enabled = true WHERE singleton = 1');
+ try {
+ expect((await performManagedSync(engine, { sourceId: id, noPull: true })).status).toBe('first_sync');
+ write(root, { 'notes/a.md': page('A two.'), 'notes/b.md': page('B two.') });
+ const pushed = commit(root, 'edit', [`notes/a.md: Changed after ${claim} was rotated.`, `notes/b.md: ${SIBLING}`]);
+
+ const purged = purgeOncePrepared(engine, 'notes/a', () => purge(engine, id, fact));
+ await performManagedSync(engine, { sourceId: id, noPull: true });
+ expect((await purged()).state).toBe('committed');
+ expect(await engine.executeRaw('SELECT commit_sha, path FROM reason_purge_tombstones WHERE source_id = $1', [id]))
+ .toEqual([{ commit_sha: pushed, path: 'notes/a.md' }]);
+ await expectNoPurgedReason(engine, id, claim);
+ } finally {
+ await disposePersistenceConsumer(engine).catch(() => undefined);
+ await engine.executeRaw('UPDATE persistence_brain SET enabled = false WHERE singleton = 1');
+ }
+ }), 180_000);
+ });
+}
diff --git a/test/reason-trailers-gbra217.test.ts b/test/reason-trailers-gbra217.test.ts
index bd1398d45f..3f77a8f696 100644
--- a/test/reason-trailers-gbra217.test.ts
+++ b/test/reason-trailers-gbra217.test.ts
@@ -272,7 +272,7 @@ describe('A6: sync mirrors gbrain-reason trailers', () => {
const scan = await scanSyncReasons(engines[0]!, { sourceId: 'none', sourceRoot: root, from: base, to: head });
expect(scan?.status).toBe('complete');
expect(scan!.byPath.size).toBe(5000);
- expect(importHistoryFor(scan, join(root, 'bulk/f4321.md'))).toEqual({ reason: 'Only this file has a reason.', historyGap: false });
+ expect(importHistoryFor(scan, join(root, 'bulk/f4321.md'))).toEqual({ reason: 'Only this file has a reason.', reasonSource: { commit: head, path: 'bulk/f4321.md' }, historyGap: false });
expect(importHistoryFor(scan, join(root, 'bulk/f0001.md'))).toEqual({ reason: null, historyGap: false });
}), 120_000);
@@ -310,7 +310,7 @@ describe('A6: sync mirrors gbrain-reason trailers', () => {
await otherBrain.executeRaw('INSERT INTO reason_purge_tombstones(source_id, commit_sha, path) VALUES ($1, $2, $3)', [id, to, 'notes/a.md']);
const range = { sourceId: id, sourceRoot: root, from, to };
const file = join(root, 'notes/a.md');
- expect(importHistoryFor(await cachedSyncReasonScan(engines[0]!, range), file)).toEqual({ reason: 'Canary reason for source-a.', historyGap: false });
+ expect(importHistoryFor(await cachedSyncReasonScan(engines[0]!, range), file)).toEqual({ reason: 'Canary reason for source-a.', reasonSource: { commit: to, path: 'notes/a.md' }, historyGap: false });
expect(importHistoryFor(await cachedSyncReasonScan(otherBrain, range), file)).toEqual({ reason: null, historyGap: false });
}), 120_000);
});
@@ -498,7 +498,7 @@ describe('A7: purge reaches change reasons', () => {
const to = commit(s.root, 'edit', [`notes/a.md: Changed once ${claim} was rotated.`]);
const range = { sourceId: s.id, sourceRoot: s.root, from, to };
const file = join(s.root, 'notes/a.md');
- expect(worker.importHistoryFor(await worker.cachedSyncReasonScan(engine, range), file)).toEqual({ reason: `Changed once ${claim} was rotated.`, historyGap: false });
+ expect(worker.importHistoryFor(await worker.cachedSyncReasonScan(engine, range), file)).toEqual({ reason: `Changed once ${claim} was rotated.`, reasonSource: { commit: to, path: 'notes/a.md' }, historyGap: false });
await sync(engine, s);
const id = await seedFact(engine, s.id, `people/safe-example-${engines.indexOf(engine)}`, claim);
expect((await purge(engine, s.id, id)).state).toBe('committed');