Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

repair private-labels: pages the write journal shows declined at their current revision count as tried

garrytan383537763e1b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f8edd2cc1d..53a6a51169 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,7 +16,7 @@ identifiers and attribution are available in the pre-removal Git revision
### Itemized changes
-A page whose own state declines the write (its file changed outside gbrain, `source_changed`, or its body is stored in segments) fails alone and the rest of its group lands. A preview-bound apply of any repair kind that completes with only such declines now exits 0 and counts them under `declined`. The next private-labels preview lists pages an earlier apply tried and that did not change after every page not tried yet, and such a page declining again no longer counts toward the `item_failures_systemic` stop, so windows that carry declines forward keep finishing; pages not tried yet that all fail the same way still stop the run. The apply result names its ledger (`details.ledger`, `details.ledger_rows_written`).
+A page whose own state declines the write (its file changed outside gbrain, `source_changed`, or its body is stored in segments) fails alone and the rest of its group lands. A preview-bound apply of any repair kind that completes with only such declines now exits 0 and counts them under `declined`. The next private-labels preview lists pages an earlier apply tried and that did not change (by the undo ledger, or on a managed brain by a declined write in the write journal at the page's current revision) after every page not tried yet, and such a page declining again no longer counts toward the `item_failures_systemic` stop, so windows that carry declines forward keep finishing; pages not tried yet that all fail the same way still stop the run. The apply result names its ledger (`details.ledger`, `details.ledger_rows_written`).
A preview-bound repair apply now waits out a `gbrain sources refresh` for up to 10 minutes per step instead of 10 seconds. A refresh that runs longer ends the apply with `worktree_refreshing`, naming the refresh and how long it has run; rerunning the same apply resumes it (GBRA-443).
diff --git a/docs/guides/repair.md b/docs/guides/repair.md
index 615a0ff571..222f37a91d 100644
--- a/docs/guides/repair.md
+++ b/docs/guides/repair.md
@@ -2199,6 +2199,13 @@ decline the command exits 0, so a script working through windows carries on.
Any other failure, or a stop, still exits 1. The next preview lists pages an
earlier apply tried and that didn't change after every page not tried yet
(`details.window.tried_before` counts them), so they stop heading each window.
+On a brain with managed persistence a page also counts as tried when its latest
+maintenance write in the write journal was declined at the revision the page
+still has, which covers pages that declined under a release whose ledger kept no
+line for them. A page whose journal row was already compacted or pruned isn't
+found that way: it's listed like a page not tried yet, and if five or more such
+pages fail first, the run stops with `item_failures_systemic` once; the stopped
+run leaves intent lines for them, so the next preview lists them last.
Clearing a declined page is a separate choice per page: see
[file drift](#file-drift). Such a page declining again doesn't count toward the
run's systemic-failure stop (`item_failures_systemic`), so a window whose tail,
diff --git a/src/core/repair/private-labels.ts b/src/core/repair/private-labels.ts
index 55f633bbc6..9071063c31 100644
--- a/src/core/repair/private-labels.ts
+++ b/src/core/repair/private-labels.ts
@@ -65,7 +65,7 @@ import { managedPersistenceEnabled } from '../persistence/ownership.ts';
import { clearApprovedSet, loadApprovedSet, previewChangedError, previewHash, saveApprovedSet } from '../persistence/preview-approval.ts';
import { PRIVACY_ALIASES, privacyAliasSql, privacyPartSql, privateSnapshotFilterFragment, readHonorAliases } from '../search/private-visibility.ts';
import { asciiLower } from '../edge-whitespace.ts';
-import { afterCursor, repairRequestId, type RepairHandler, type RepairItem, type RepairItemOutcome, type RepairPlan, type RepairPlanOptions, type RepairScope, type RepairSettlement } from './core.ts';
+import { afterCursor, REPAIR_DECLINE_CODES, repairRequestId, type RepairHandler, type RepairItem, type RepairItemOutcome, type RepairPlan, type RepairPlanOptions, type RepairScope, type RepairSettlement } from './core.ts';
export type ClearClass = 'alias' | 'alias-and-visibility' | 'git-publish';
export type KeepClass = 'pinned_question' | 'derived' | 'strong_alias' | 'explicit_visibility';
@@ -179,7 +179,8 @@ async function planClear(engine: BrainEngine, scope: RepairScope, selection: Sel
page_id: Number(row.id), revision: row.revision, labels: decided.removed, chars: Number(row.chars), selection });
});
const tried = triedBefore(ledger, scope.brain_id);
- for (const entry of entries) if (tried.has(entry.source_id + '\0' + entry.page_id)) entry.tried = true;
+ const declined = await declinedInJournal(engine, entries);
+ for (const entry of entries) if (tried.has(entry.source_id + '\0' + entry.page_id) || declined.has(entry.source_id + '\0' + entry.slug)) entry.tried = true;
const retry = (entry: Approved) => Number(entry.tried === true);
entries.sort((a, b) => retry(a) - retry(b) || CLEAR_CLASSES.indexOf(a.class as ClearClass) - CLEAR_CLASSES.indexOf(b.class as ClearClass) || a.page_id - b.page_id);
return { counts: counts as Record<string, number>, entries, unreadable: 0 };
@@ -326,6 +327,26 @@ function triedBefore(ledger: PrivateLabelsLedger | undefined, brainId: string):
return new Set([...latest].flatMap(([key, line]) => line.removed && line.phase === 'intent' ? [key] : []));
}
+/**
+ * GBRA-443: on a managed brain, the pages whose latest maintenance write in the journal ended in a decline
+ * (REPAIR_DECLINE_CODES) at the revision the page still has. A release before the write-ahead ledger left no ledger line
+ * for a declined page, so this is how its decline carries over. One query for the whole candidate set; a page whose
+ * journal row was compacted or pruned is not found here and is ordered like a page not tried yet.
+ */
+async function declinedInJournal(engine: BrainEngine, entries: Approved[]): Promise<Set<string>> {
+ if (!entries.length || !await managedPersistenceEnabled(engine)) return new Set();
+ const rows = await engine.executeRaw<{ source_id: string; slug: string; error_code: string | null; expected: string | null }>(
+ `SELECT DISTINCT ON (r.source_id, r.slug) r.source_id, r.slug, r.error_code, r.intent->>'expected_revision' AS expected
+ FROM persistence_requests r JOIN unnest($1::text[], $2::text[]) AS t(source_id, slug) ON r.source_id = t.source_id AND r.slug = t.slug
+ WHERE r.intent IS NOT NULL AND NOT r.compacted AND r.intent->>'kind' = 'managed_maintenance_page'
+ ORDER BY r.source_id, r.slug, r.sequence DESC`, [entries.map(entry => entry.source_id), entries.map(entry => entry.slug)], bounded);
+ const revision = new Map(entries.map(entry => [entry.source_id + '\0' + entry.slug, entry.revision]));
+ return new Set(rows.flatMap(row => {
+ const key = row.source_id + '\0' + row.slug;
+ return row.error_code && REPAIR_DECLINE_CODES.has(row.error_code) && row.expected !== null && row.expected === revision.get(key) ? [key] : [];
+ }));
+}
+
/** GBRA-443: the listed items whose write embeds: a label-only edit keeps every stored vector except on a page stored under per-chunk synopsis; a page with a chunk that has no vector yet is counted too. */
async function reembedding(engine: BrainEngine, items: LabelsItem[]): Promise<LabelsItem[]> {
if (!items.length) return [];
diff --git a/test/repair-private-labels-batch.test.ts b/test/repair-private-labels-batch.test.ts
index 3e6ee64cf3..981532694a 100644
--- a/test/repair-private-labels-batch.test.ts
+++ b/test/repair-private-labels-batch.test.ts
@@ -37,6 +37,7 @@ import { installFaultHook } from '../src/core/persistence/fault-points.ts';
import { getWorktreeBinding } from '../src/core/persistence/ownership.ts';
import { persistenceHome } from '../src/core/persistence/identity.ts';
import { tryAcquireNativeLock } from '../src/core/persistence/native-lock.ts';
+import { maintenancePreflight, publishMaintenancePage } from '../src/core/persistence/prepared-maintenance.ts';
import { _setRepairRefreshWaitForTests, _setRefreshFenceWaitForTests } from '../src/core/persistence/admission-retry.ts';
import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
import { durableGitRepo } from './helpers/git-publication.ts';
@@ -310,6 +311,45 @@ for (const backend of testBackends()) {
for (const slug of slugs) expect('access' in await frontmatter(engine, slug)).toBe(false);
}, { databaseUrl, setup }), 180_000);
+ test('after an upgrade from a ledger with no intent lines, pages the journal shows declined at their current revision are listed last and do not stop the window', () => managedBrain(async ({ engine, ctx, root }) => {
+ // The older release's state: five drifted pages at the head of the order whose maintenance writes were admitted and then
+ // declined at preparation (a conflict row, source_changed, at the page's revision), and no ledger line for them.
+ const carried = [0, 1, 2, 3, 4].map(i => 'notes/old-' + i);
+ const fresh = [0, 1, 2].map(i => 'notes/new-' + i);
+ for (const slug of [...carried, ...fresh]) await put(ctx, slug, labelled(slug));
+ await settleManagedGitEffects(engine);
+ const authority = (await maintenancePreflight(engine, 'default'))!;
+ installFaultHook(async point => { if (point === 'consumer:preparing') throw new OperationError('source_changed', 'The file changed before publication.', 'Fixture.'); });
+ for (const slug of carried) {
+ const snapshot = (await engine.readPageSnapshot(slug, { sourceId: 'default' }))!;
+ const declined = await publishMaintenancePage(engine, authority, slug, serializeMarkdown({ topic: 'kept' }, slug + ' body text.', '', { type: 'note' as never, title: slug, tags: ['t1'] }),
+ { expectedRevision: snapshot.revision, requestId: randomUUID(), keepTrust: true }).then(() => null, (error: unknown) => error as OperationError);
+ expect(declined?.code).toBe('source_changed');
+ }
+ installFaultHook(undefined);
+ // The ledger is in the older shape: one line per landed write, no phase (here one page an earlier run cleared).
+ await put(ctx, 'notes/landed', serializeMarkdown({ topic: 'kept' }, 'notes/landed body text.', '', { type: 'note' as never, title: 'notes/landed', tags: ['t1'] }));
+ await settleManagedGitEffects(engine);
+ const landed = (await engine.readPageSnapshot('notes/landed', { sourceId: 'default' }))!;
+ const { brain_id: brain } = await resolveRepairScope(engine, 'default');
+ const ledgerPath = privateLabelsLedger(brain).path;
+ mkdirSync(dirname(ledgerPath), { recursive: true });
+ appendFileSync(ledgerPath, JSON.stringify({ at: new Date().toISOString(), brain_id: brain, source_id: 'default', slug: 'notes/landed', page_id: Number(landed.page.id),
+ revision_before: 'r0', request_id: randomUUID(), removed: { access: 'private' } }) + NL);
+ for (const slug of carried) { const file = join(root, slug + '.md'); writeFileSync(file, readFileSync(file, 'utf8') + NL + 'An edit made outside gbrain.' + NL); }
+ expect((await engine.executeRaw<{ n: number }>("SELECT count(*)::int AS n FROM persistence_requests WHERE state='conflict' AND error_code='source_changed' AND slug LIKE 'notes/old-%'"))[0]!.n).toBe(5);
+ const preview = (await cli(engine, ['private-labels', '--limit', '16'])).result;
+ expect(preview.listing.map((entry: { item: string }) => entry.item)).toEqual([...fresh, ...carried].map(slug => 'default:' + slug));
+ expect(preview.details.window.tried_before).toBe(5);
+ const { result, exit } = await cli(engine, ['private-labels', '--apply', '--expect', preview.preview_hash]);
+ expect(result.stopped).toBeUndefined();
+ expect(result.complete).toBe(true);
+ expect(result.applied).toBe(3);
+ expect(result.failed.map((f: { slug: string }) => f.slug).sort()).toEqual([...carried].sort());
+ expect(result.declined).toEqual({ source_changed: 5 });
+ expect(exit).toBe(0);
+ }, { databaseUrl, setup }), 180_000);
+
test('every page of a run has its intent line before any of them is published, and a crash after the group published still restores each', () => managedBrain(async ({ engine, ctx, root }) => {
const slugs = [0, 1, 2].map(i => 'notes/crash-' + i);
for (const slug of slugs) await put(ctx, slug, labelled(slug));