Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Make repair visibility follow an atom origin a rename moved
garrytan6249d1b14503
diff --git a/changelog.d/concept-input-rename-hotfix.md b/changelog.d/concept-input-rename-hotfix.mdindex bea009a391..7fbd72795b 100644--- a/changelog.d/concept-input-rename-hotfix.md+++ b/changelog.d/concept-input-rename-hotfix.md@@ -6,5 +6,5 @@ important: security### Fixed- **Concepts from renamed private notes (GST-2687).** gbrain writes short claim pages (atoms) from your notes, then groups atoms into topic pages (concepts). When a private note was renamed and a later rename gave its old name to a different, public page, the concept built from that note's atoms could be read by people other than the brain owner through `get_page`, `search`, `query` and `list_pages`. The atoms themselves were already hidden in this case; the concept is now hidden the same way, because gbrain follows the note's rename history to the private page it moved to. The brain owner, trusted local commands and a credential allowed to read private pages in that source still read it, exactly as before. Nothing is rewritten: the rule is checked when a page is read.-- **Doctor.** `gbrain doctor`'s `derived_visibility` check now counts such atoms (`looser_atoms`) and concepts (`looser_concepts`), and `gbrain repair visibility` previews them as before.-- Schema migration v311 adds the database function `gbrain_concept_inputs_private`, which holds this check so search stays fast on large brains. It changes no rows. On a brain where the migration has not run yet, remote reads keep working and still hide these concepts.+- **Doctor and repair.** `gbrain doctor`'s `derived_visibility` check now counts such atoms (`looser_atoms`) and concepts (`looser_concepts`). `gbrain repair visibility` now follows the same rename history, so it previews them and stamps them `private`; before, it judged them by the public page that took the old name and could stamp an unlabelled one `world`.+- Schema migration v311 adds the database function `gbrain_concept_inputs_private`, which holds this check so search stays fast on large brains. It changes no rows. On a brain where the migration has not run yet, reads keep working and still hide these concepts.diff --git a/src/core/repair/visibility.ts b/src/core/repair/visibility.tsindex d231a5347f..28d86405a2 100644--- a/src/core/repair/visibility.ts+++ b/src/core/repair/visibility.ts@@ -2,7 +2,8 @@* `gbrain repair visibility` (#5525): tighten-only backfill that stamps an* explicit `visibility` on extracted atoms and synthesized concepts.*- * atom origin page -> effectiveVisibility(origin); transcript or missing origin -> private+ * atom origin page -> effectiveVisibility(origin); transcript or missing origin -> private;+ * a private page a rename moved its origin to (slug_rename_history, GST-2687) -> private* concept strictest visibility of its input atoms (provenance edges + atoms' `concepts:`);* a member without a provenance edge counts as private*@@ -52,7 +53,7 @@ import { managedPersistenceEnabled } from '../persistence/ownership.ts';import { maintenancePreflight, publishMaintenancePages, submitDatabaseMaintenanceIntent } from '../persistence/prepared-maintenance.ts';import { CANONICAL_FILE_MISSING } from '../persistence/publication-failure.ts';import { isSegmentedPage } from '../page-body/page-shape.ts';-import { atomOriginSlugSql } from '../persistence/atom-origin.ts';+import { atomMovedOriginPrivateSql, atomOriginSlugSql } from '../persistence/atom-origin.ts';import { afterCursor, repairRequestId, type RepairHandler, type RepairItem, type RepairItemOutcome, type RepairPlan, type RepairPlanOptions, type RepairScope, type RepairSettlement, type RepairCursor } from './core.ts';import { openTwins, openTwinsLines, openTwinsWarning, type OpenTwins } from '../access/open-twins.ts';import { ownerMemberId } from '../access/owner.ts';@@ -65,7 +66,7 @@ import { createProgress, startHeartbeat } from '../progress.ts';import { cliOptsToProgressOptions, getCliOptions } from '../cli-options.ts';interface DerivedRow { id: number; source_id: string; slug: string; type: string; visibility: string | null; chars: number }-interface AtomRow extends DerivedRow { source_slug: string | null; origin_id: number | null; origin_type: string | null; origin_frontmatter: Record<string, unknown> | null; origin_folder_restricted: boolean }+interface AtomRow extends DerivedRow { source_slug: string | null; origin_id: number | null; origin_type: string | null; origin_frontmatter: Record<string, unknown> | null; origin_folder_restricted: boolean; origin_moved_private: boolean }interface InputRow { concept_id: number; atom_id: number; linked: boolean }interface EventRow extends DerivedRow { origin_id: number | null; origin_type: string | null; origin_frontmatter: Record<string, unknown> | null; origin_folder_restricted: boolean }@@ -115,7 +116,7 @@ async function planLineage(engine: BrainEngine, sourceIds: string[], only?: { idconst atoms = atomIds?.length === 0 ? [] : await engine.executeRaw<AtomRow>(`SELECT a.id,a.source_id,a.slug,a.type,a.frontmatter->>'visibility' AS visibility,length(a.compiled_truth) AS chars, a.frontmatter->>'source_slug' AS source_slug,o.id AS origin_id,o.type AS origin_type,o.frontmatter AS origin_frontmatter,- (o.id IS NOT NULL AND ${folderRestrictedSql('o')}) AS origin_folder_restricted+ (o.id IS NOT NULL AND ${folderRestrictedSql('o')}) AS origin_folder_restricted, ${atomMovedOriginPrivateSql('a')} AS origin_moved_privateFROM pages a LEFT JOIN pages o ON o.source_id=a.source_id AND o.slug=${atomOriginSlugSql('a')} AND o.deleted_at IS NULLWHERE a.type='atom' AND a.deleted_at IS NULL AND a.source_id=ANY($1::text[])AND ($2::integer[] IS NULL OR a.id=ANY($2::integer[])) ORDER BY a.id`, [sourceIds, atomIds], bounded);@@ -126,7 +127,7 @@ async function planLineage(engine: BrainEngine, sourceIds: string[], only?: { idfor (const atom of atoms) {const origin = atom.source_slug ? { kind: 'page' as const, page: atom.origin_id == null ? null : { type: atom.origin_type, frontmatter: atom.origin_frontmatter, folder_restricted: atom.origin_folder_restricted } }: { kind: 'transcript' as const };- const target = effectiveVisibility(origin);+ const target = atom.origin_moved_private ? 'private' : effectiveVisibility(origin);const next = tightened(atom.visibility, target);if (next && origin.kind === 'page' && !origin.page) originGone++;const effective = next ?? (atom.visibility === 'world' ? 'world' : 'private');diff --git a/test/derived-visibility-repair.test.ts b/test/derived-visibility-repair.test.tsindex f0112a5527..1e0ee6234b 100644--- a/test/derived-visibility-repair.test.ts+++ b/test/derived-visibility-repair.test.ts@@ -23,6 +23,7 @@ import { derivedVisibilityCheck } from '../src/commands/doctor/checks/derived-viimport { runPhaseWithStoredPageFixtures } from './helpers/extract-atoms-page-fixtures.ts';import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';import { withEnv } from './helpers/with-env.ts';+import { renamePageOntoSlug } from '../src/commands/sync/rename-collision.ts';import { testBackends } from './helpers/test-backends.ts';const backends = testBackends();@@ -319,3 +320,30 @@ describe('#5525 tighten-only visibility backfill', () => {}}, 120_000);});++describe('GST-2687 the backfill follows an atom origin a rename moved under a dropped alias', () => {+ test('repair previews the moved-origin atoms and concept doctor reports and stamps them private, never world', async () => {+ for (const engine of engines) {+ await reset(engine);+ await engine.executeRaw("DELETE FROM slug_aliases"); await engine.executeRaw("DELETE FROM slug_rename_history");+ await seed(engine, 'notes/secret', 'note', { visibility: 'private' });+ await seed(engine, 'notes/other', 'note', { visibility: 'world' });+ await seed(engine, 'atoms/moved-world', 'atom', { visibility: 'world', source_slug: 'notes/secret' });+ await seed(engine, 'atoms/moved-unstamped', 'atom', { source_slug: 'notes/secret' });+ await seed(engine, 'concepts/moved', 'concept', { synthesized_by: 'synthesize_concepts-v0.41' });+ await engine.addLink('concepts/moved', 'atoms/moved-unstamped', 'input', 'synthesized_from', 'concept-provenance', undefined, undefined, { fromSourceId: 'default', toSourceId: 'default' });+ expect(await renamePageOntoSlug(engine, { sourceId: 'default', oldSlug: 'notes/secret', newSlug: 'archive/secret', to: 'archive/secret.md' })).toBe('renamed');+ expect(await renamePageOntoSlug(engine, { sourceId: 'default', oldSlug: 'notes/other', newSlug: 'notes/secret', to: 'notes/secret.md' })).toBe('renamed');+ expect((await derivedVisibilityCheck(engine)).details).toMatchObject({ unstamped_atoms: 1, unstamped_concepts: 1, looser_atoms: 1 });+ await withEnv({ GBRAIN_HOME: home }, async () => {+ const scope = await resolveRepairScope(engine);+ const preview = await runRepair(ctx(engine, false), visibilityRepair, scope, { apply: false });+ expect(preview.affected).toBe(3);+ expect(await runRepair(ctx(engine, false), visibilityRepair, scope, { apply: true })).toMatchObject({ applied: 3, complete: true });+ });+ expect({ world: await visibilityOf(engine, 'atoms/moved-world'), unstamped: await visibilityOf(engine, 'atoms/moved-unstamped'), concept: await visibilityOf(engine, 'concepts/moved') })+ .toEqual({ world: 'private', unstamped: 'private', concept: 'private' });+ expect((await derivedVisibilityCheck(engine)).details).toMatchObject({ unstamped_atoms: 0, unstamped_concepts: 0, looser_atoms: 0, looser_concepts: 0 });+ }+ }, 120_000);+});