Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(#6712 PR 4b): core guard on the legacy-twin merge; file-drift removal exempt with its reason

garrytan8e2448774952
diff --git a/scripts/check-core-guard-coverage.mjs b/scripts/check-core-guard-coverage.mjs
index 20c3a9c162..b5b5868ce1 100644
--- a/scripts/check-core-guard-coverage.mjs
+++ b/scripts/check-core-guard-coverage.mjs
@@ -36,6 +36,7 @@ const EXEMPT = {
'src/core/persistence/consumer.ts': 'dispatches to preparers; no page content of its own',
'src/core/persistence/coordinator.ts': 'takes the ordered source lock from exclusiveSources; prepares nothing itself',
'src/core/persistence/effect-journal.ts': 'post-publication effects; no page text',
+ 'src/core/repair/file-drift.ts': 'removes a held file whose page is deleted: no live page records the path (re-read under the publication guard), the page row is untouched, and core counts only live pages; --prefer file publishes through file-repair.ts',
'src/core/repair/captured-facts.ts': 'owner-run repair that expires fact rows (shrinks only)',
'src/core/repair/extractor-facts.ts': 'owner-run repair restoring expired fact rows',
'src/core/repair/conversation-labels.ts': 'owner-run repair expiring fact rows and writing an outcome row (shrinks only; page text unchanged)',
diff --git a/src/core/repair/legacy-twins.ts b/src/core/repair/legacy-twins.ts
index 1dcbf79cfb..dcde8ead59 100644
--- a/src/core/repair/legacy-twins.ts
+++ b/src/core/repair/legacy-twins.ts
@@ -49,6 +49,7 @@ import { slugifyPath } from '../sync.ts';
import { noteBelief } from '../belief/ledger.ts';
import { recordRenameAlias } from '../page-state/rename-alias.ts';
import { authorizeWrite } from '../persistence/authority.ts';
+import { prepareCoreGuard } from '../persistence/core-guard.ts';
import { existingLocalHostId } from '../persistence/identity.ts';
import { getWorktreeBinding, managedPersistenceEnabled } from '../persistence/ownership.ts';
import { maintenanceTransaction } from '../persistence/attribution.ts';
@@ -410,7 +411,9 @@ export async function prepareLegacyTwinMerge(engine: BrainEngine, row: WriteRequ
const moved = await pairNow(engine, pair, owner);
if (moved) throw refuse(moved);
await authorizeWrite(engine, row.authority, 'submit_job', row.slug);
+ const core = await prepareCoreGuard(engine, { row, snapshot: await engine.readPageSnapshot(row.slug, { sourceId: row.source_id }), incoming: null });
const prepared: PreparedMutation = { observedRevision: pair.twin.revision, additionalPageKeys: [{ sourceId: row.source_id, slug: pair.legacy.slug }],
+ ...(core ? { exclusiveSources: core.exclusiveSources } : {}),
databaseOnlyReason: 'shared_origin',
validate: async tx => {
await authorizeWrite(tx, row.authority, 'submit_job', row.slug);
diff --git a/test/repair-legacy-twins-6885.test.ts b/test/repair-legacy-twins-6885.test.ts
index 8360a3fa95..d81c65b850 100644
--- a/test/repair-legacy-twins-6885.test.ts
+++ b/test/repair-legacy-twins-6885.test.ts
@@ -28,6 +28,9 @@ import { readShadowLevel } from '../src/core/persistence/sync-pass.ts';
import { purgeDeletedPagesCoordinated } from '../src/core/persistence/purge-deleted.ts';
import { resolveRepairScope, type RepairKind } from '../src/core/repair/core.ts';
import { EXPLICIT_REPAIR_REGISTRY, repairRunner } from '../src/core/repair/registry.ts';
+import { LEGACY_TWINS_INTENT, prepareLegacyTwinMerge, scanLegacyTwins } from '../src/core/repair/legacy-twins.ts';
+import { maintenancePreflight } from '../src/core/persistence/prepared-maintenance.ts';
+import { registerLocalWriter } from '../src/core/persistence/identity.ts';
import { runRepairCommand } from '../src/commands/repair.ts';
import { _resetCliExitVerdictForTests, currentExitCode } from '../src/core/cli-force-exit.ts';
import { withEnv } from './helpers/with-env.ts';
@@ -268,6 +271,22 @@ for (const backend of testBackends()) describe(`#6885 repair legacy-twins (${bac
});
}, 180_000);
+ test('a core twin: a remote merge request is refused core_delete_owner_only and nothing changes; the owner merge proceeds', () => fixture(async f => {
+ const scan = await scanLegacyTwins(engine, f.sourceId);
+ expect(scan.pairs.map(pair => pair.twin.slug)).toEqual([TWIN]);
+ const authority = (await maintenancePreflight(engine, f.sourceId))!;
+ const stdio = await registerLocalWriter(engine, 'stdio', { sourceIds: ['*'], operations: null, scopes: ['read', 'write'], slugPrefixes: null });
+ const row = { request_id: randomUUID(), source_id: f.sourceId, slug: TWIN, page_id: String(f.ids.twin), source_incarnation: authority.writer.sourceIncarnation,
+ authority: { ...authority.writer, remote: true, principal: { kind: 'local_stdio', id: stdio.id } }, intent: { kind: LEGACY_TWINS_INTENT, expected_revision: scan.pairs[0]!.twin.revision, pair: scan.pairs[0], owner: scan.owner } } as never;
+ expect((await refusal(() => prepareLegacyTwinMerge(engine, row, {} as never))).code).toBe('core_delete_owner_only');
+ expect(await pageRow(f.sourceId, f.ids.twin)).toMatchObject({ deleted: false, source_path: FILE });
+ expect((await pageRow(f.sourceId, f.ids.legacy))!.source_path).toBeNull();
+ const preview = await repair(f.sourceId);
+ expect(await repair(f.sourceId, { apply: true, expect: preview.preview_hash })).toMatchObject({ applied: 1, outcomes: { merged: 1 } });
+ expect(await pageRow(f.sourceId, f.ids.twin)).toMatchObject({ deleted: true, source_path: null });
+ expect(await pageRow(f.sourceId, f.ids.legacy)).toMatchObject({ deleted: false, source_path: FILE });
+ }, { extra: { [FILE]: '---\ntitle: Foo Bar\nalways_load: true\n---\nA synthetic observation about Foo Bar.\n' } }), 180_000);
+
test('several legacy pages folding to one twin are one decision; nothing is merged', () => fixture(async f => {
await raw(() => engine.executeRaw(`INSERT INTO pages(source_id, slug, type, title, compiled_truth, timeline, frontmatter, content_hash)
SELECT source_id, 'notes/foo bar', type, title, compiled_truth, timeline, frontmatter, content_hash FROM pages WHERE id=$1`, [f.ids.legacy]));