Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(repair): restore plans from each page's latest clear; git-publish test pins its repo identity (GST-2059)

garrytan2779322328e7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 329f05c97f..3256cc56a1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,7 +22,10 @@ identifiers and attribution are available in the pre-removal Git revision
- `src/cli/as-preview.ts`: `--as <owner>` previews the owner's view.
- `src/core/repair/private-labels.ts` (new), `src/commands/repair-private-labels.ts` (new): `gbrain repair private-labels`, explicit-only and preview-bound; classes `alias`, `alias-and-visibility`, `git-publish` (named only) and `restore`; a ledger under the gbrain home records each change.
- Skills and docs: `skills/conventions/page-privacy.md` (new), `skills/bulk-ingestion` (template), `skills/brain-ops`, `skills/_AGENT_README.md`, `skills/frontmatter-guard`; `docs/guides/access-designations.md` (new), linked from multi-source brains, company brain, page anatomy and brains-and-sources; `docs/protocol/MCP_META_CHANNELS.md`.
-- Tests: `test/access-owner-reads.test.ts`, `test/repair-private-labels.test.ts`.
+- `src/core/persistence/page-visibility.ts`, `src/core/persistence/page-prepare.ts`, `src/core/persistence/upload-streamed.ts`: a new `default` page the enabled owner's credential creates through `put_page` or a streamed upload is not kept to the owner (no `readable_by: [member:<id>]`); a rewrite keeps its stored readers, and `POST /ingest` still refuses the owner.
+- `src/core/persistence/page-prepare.ts`: on a git-canonical source, a page that was private from its first write (no file ever recorded) writes its file for the first time when it stops being private, so `--class git-publish` and an ordinary edit no longer refuse it as a removed canonical file.
+- `src/core/repair/private-labels.ts`: restore plans from each page's latest clear and its current labels, so a restore that was accepted but never landed stays restorable; a ledger line that fails to save after a write reports the values to undo by hand; a git-publish clear is called one-way, since Git history keeps the page.
+- Tests: `test/access-owner-reads.test.ts`, `test/access-owner-default-writes.test.ts`, `test/repair-private-labels.test.ts`.
## [0.60.622.0] - 2026-11-09
diff --git a/src/core/repair/private-labels.ts b/src/core/repair/private-labels.ts
index b8137e1576..f95edac77a 100644
--- a/src/core/repair/private-labels.ts
+++ b/src/core/repair/private-labels.ts
@@ -141,15 +141,19 @@ async function planClear(engine: BrainEngine, scope: RepairScope, selection: Sel
/** The ledger's latest line per page decides: a removal whose page is live and still lacks every removed key is restorable. */
async function planRestore(engine: BrainEngine, scope: RepairScope, selection: Selection, ledger: PrivateLabelsLedger) {
const { lines, unreadable } = ledger;
- const latest = new Map<string, LedgerLine>();
+ // A page's latest clear decides; a later restored line is not proof, since a restore accepted pending can still fail.
+ const pages = new Set<string>();
+ const latestRemoval = new Map<string, LedgerLine>();
for (const line of lines) {
- if (line.brain_id === scope.brain_id && scope.source_ids.includes(line.source_id)) latest.set(`${line.source_id}:${line.page_id}`, line);
+ if (line.brain_id !== scope.brain_id || !scope.source_ids.includes(line.source_id)) continue;
+ pages.add(`${line.source_id}:${line.page_id}`);
+ if (line.removed) latestRemoval.set(`${line.source_id}:${line.page_id}`, line);
}
- const removals = [...latest.values()].filter(line => line.removed);
+ const removals = [...latestRemoval.values()];
const live = removals.length ? await engine.executeRaw<LabelRow>(`SELECT id, source_id, slug, type, frontmatter, knowledge_revision::text AS revision, COALESCE(body_bytes, 0) AS chars
FROM pages WHERE id = ANY($1::integer[]) AND deleted_at IS NULL`, [removals.map(line => line.page_id)], bounded) : [];
const byId = new Map(live.map(row => [Number(row.id), row]));
- const counts = { restorable: 0, already_restored: latest.size - removals.length, page_gone: 0 };
+ const counts = { restorable: 0, already_restored: pages.size - removals.length, page_gone: 0 };
const entries: Approved[] = [];
for (const line of removals) {
const page = byId.get(line.page_id);
diff --git a/test/repair-private-labels.test.ts b/test/repair-private-labels.test.ts
index e222e8c2db..e11a8345f6 100644
--- a/test/repair-private-labels.test.ts
+++ b/test/repair-private-labels.test.ts
@@ -16,7 +16,7 @@
*/
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
import { execFileSync } from 'node:child_process';
-import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
+import { appendFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
@@ -269,6 +269,24 @@ describe('repair private-labels', () => {
expect((await row('notes/readers')).frontmatter.access).toBe('private');
expect(ledgerLines(brainId).filter(l => l.restored && l.slug === 'notes/alias')).toHaveLength(1);
});
+
+ test('a restore recorded while its write was only accepted, whose write then failed, stays restorable', async () => {
+ // The accepted-pending branch records the restored line before the queued write settles; here the write never landed.
+ const page = await row('notes/readers');
+ expect(page.frontmatter.access).toBe('private');
+ const clear = await json(['private-labels', '--class', 'alias']);
+ expect(clear.listing.map((l: { item: string }) => l.item)).toContain('default:notes/readers');
+ const cleared = await json(['private-labels', '--class', 'alias', '--apply', '--expect', clear.preview_hash]);
+ expect(cleared.failed).toEqual([]);
+ const removal = ledgerLines(brainId).filter(l => l.slug === 'notes/readers' && l.removed).at(-1)!;
+ appendFileSync(ledgerPath(brainId), JSON.stringify({ ...removal, removed: undefined, restored: removal.removed, at: new Date().toISOString(), request_id: crypto.randomUUID() }) + NL);
+ expect('access' in (await row('notes/readers')).frontmatter).toBe(false);
+ const preview = await json(['private-labels', '--class', 'restore']);
+ expect(preview.listing.map((l: { item: string }) => l.item)).toContain('default:notes/readers');
+ const restored = await json(['private-labels', '--class', 'restore', '--apply', '--expect', preview.preview_hash]);
+ expect(restored.failed).toEqual([]);
+ expect((await row('notes/readers')).frontmatter.access).toBe('private');
+ });
});
/**
@@ -347,7 +365,9 @@ describe('repair private-labels --class git-publish on a git-canonical source',
const root = join(dir, 'git-checkout');
mkdirSync(root, { recursive: true });
execFileSync('git', ['init', '-q'], { cwd: root });
- execFileSync('git', ['-c', 'user.name=example', '-c', '[email protected]', 'commit', '-q', '--allow-empty', '-m', 'init'], { cwd: root });
+ execFileSync('git', ['config', 'user.name', 'Example Writer'], { cwd: root });
+ execFileSync('git', ['config', 'user.email', '[email protected]'], { cwd: root });
+ execFileSync('git', ['commit', '-q', '--allow-empty', '-m', 'init'], { cwd: root });
await engine.executeRaw('INSERT INTO sources (id, name, local_path) VALUES ($1, $1, $2) ON CONFLICT (id) DO NOTHING', [source, root]);
await seed('notes/nofile', 'note', { topic: 'shared' }, source);
await engine.executeRaw('UPDATE sources SET config = $2::jsonb WHERE id = $1', [source, JSON.stringify({ canonical: 'git' })]);