Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Merge remote-tracking branch 'origin/capy/wave18-lane2-s1' into capy/wave18a

garrytaneab43b7bb138
diff --cc src/core/persistence/sync-discovery.ts
index 822668a40d,b2bd66c45a..f07b20a9ab
--- a/src/core/persistence/sync-discovery.ts
+++ b/src/core/persistence/sync-discovery.ts
@@@ -9,9 -9,8 +9,10 @@@ import { managedForbiddenFlag, managedS
import type { Action } from '../agent-output.ts';
import type { RegistryCode } from '../error-registry.ts';
import { buildDetachedWorkingTreeManifest, computeSyncDelta } from '../sync-delta.ts';
- import { isSyncable, isCodeFilePath, isImageFilePath, matchesAnyGlob, resolveSlugForPath, slugifyPath } from '../sync.ts';
+ import { confirmedSyncDelta, confirmRenameIdentities, explicitIdVeto, frontmatterIdentity, identityHeadDigest } from '../sync-rename-identity.ts';
-import { isSyncable, isCodeFilePath, isImageFilePath, matchesAnyGlob, resolveSlugForPath, type DowngradedRename, type SyncManifest } from '../sync.ts';
++import { isSyncable, isCodeFilePath, isImageFilePath, matchesAnyGlob, resolveSlugForPath, slugifyPath, type DowngradedRename, type SyncManifest } from '../sync.ts';
+import { databaseOnlyPageSql } from './unbound-pages.ts';
+import { validateSlug } from '../utils.ts';
import { resolveSlugRootMode } from '../sync-anchor.ts';
import { isWriteTargetContained } from '../path-confine.ts';
import { getWorktreeBinding, type WorktreeBinding } from './ownership.ts';
@@@ -566,36 -567,29 +572,59 @@@ export async function discoverManagedSy
}
return discovered;
}
+type LegacyIdentity = { id: number; slug: string; source_path: string | null; deleted_at: Date | string | null };
+/**
+ * #6727: per discovery, its live pages with no recorded origin whose slug the current fold changes, by folded slug. Built on
+ * first need; database-only pages (never imported from a file) are left out by one read over just those candidates, so a
+ * source without legacy pages issues no extra statement.
+ */
+const legacyByFold = new WeakMap<LegacyIdentity[], Promise<Map<string, string[]>>>();
+async function foldLegacyPages(engine: BrainEngine, identities: LegacyIdentity[]): Promise<Map<string, string[]>> {
+ const candidates = identities.filter(page => page.deleted_at == null && page.source_path == null && slugifyPath(page.slug) !== page.slug);
+ const databaseOnly = new Set(candidates.length ? (await engine.executeRaw<{ id: number }>(
+ `SELECT p.id FROM pages p WHERE p.id=ANY($1::bigint[]) AND ${databaseOnlyPageSql('p')}`, [candidates.map(page => Number(page.id))])).map(row => Number(row.id)) : []);
+ return candidates.filter(page => !databaseOnly.has(Number(page.id)))
+ .reduce((map, page) => map.set(slugifyPath(page.slug), [...map.get(slugifyPath(page.slug)) ?? [], page.slug]), new Map<string, string[]>());
+}
+/**
+ * #6727: the slug a file with no recorded origin imports as. With no live page at its folded slug, exactly one legacy page
+ * (an older gbrain imported it before the current fold) folding to it is the file's: it imports into that page (same id,
+ * links and history). Several, or one whose slug the importer cannot write as it stands (`validateSlug` lowercases), keep
+ * the folded slug and are named on the entry, so the freeze holds the file `legacy_slug_conflict`.
+ */
+async function adoptLegacyPage(engine: BrainEngine, identities: LegacyIdentity[], holder: LegacyIdentity | undefined, slug: string, entry: SyncEntry): Promise<string> {
+ if (holder && holder.deleted_at == null) return slug;
+ let folded = legacyByFold.get(identities);
+ if (!folded) legacyByFold.set(identities, folded = foldLegacyPages(engine, identities));
+ const legacy = (await folded).get(slug) ?? [];
+ const writable = () => { try { return validateSlug(legacy[0]!) === legacy[0]; } catch { return false; } };
+ if (legacy.length === 1 && writable()) return legacy[0]!;
+ if (legacy.length) entry.legacySlugs = [...legacy].sort();
+ return slug;
+}
+ /** #6729: the inexact Git renames this discovery split into a delete plus an add, in origin form, for the sync report and its durable record. */
+ function identityDowngradesOf(manifests: Array<SyncManifest | null>, eligible: (path: string) => boolean, sourcePath: (path: string) => string): Pick<SyncDiscovery, 'identityDowngrades'> {
+ const pairs = manifests.flatMap(manifest => manifest?.downgradedRenames ?? []).filter(pair => eligible(pair.from) || eligible(pair.to))
+ .map(pair => ({ ...pair, from: sourcePath(pair.from), to: sourcePath(pair.to) }));
+ return pairs.length ? { identityDowngrades: pairs } : {};
+ }
+ /**
+ * #6729: a file that takes over the page holding its slug (a vanished or deleted origin) inherits that page's identity,
+ * so it must not when both carry an explicit frontmatter id and the ids differ: they are two different documents that
+ * happen to map to one slug. The sync is refused before any write, naming both.
+ */
+ function assertTakeoverIdentity(discovery: SyncDiscovery, winner: SyncEntry, holder: { slug: string; fm_id: string | null }): void {
+ const incoming = holder.fm_id ? incomingId(discovery, winner) : null;
+ if (!explicitIdVeto(incoming, holder.fm_id)) return;
+ throw opError('page_identity_changed', 'A file would take over a page whose frontmatter id differs from its own.',
+ `${winner.sourcePath} in ${discovery.sourceId} maps to page ${holder.slug}, whose file is gone, but its frontmatter id '${incoming}' differs from the page's id '${holder.fm_id}', so it is a different document and must not inherit that page's facts, links and history; nothing was written. `
+ + `Give the file the page's id if it is the same document, or rename it so its slug differs, commit, then run gbrain sync --no-pull --source ${discovery.sourceId}.`,
+ { detail: 'rename_identity_conflict' });
+ }
+ /** #6729: the frontmatter id of the file an entry imports (null when it has none or cannot be read). */
+ function incomingId(discovery: SyncDiscovery, entry: SyncEntry): string | null {
+ try { return frontmatterIdentity(readSyncContent(discovery, entry)).id; } catch { return null; }
+ }
/** #6517: several live pages (the soft-delete exit is named) or several tombstones record one imported origin. */
function ambiguousOriginRefusal(sourceId: string, sourcePath: string, named: Array<{ slug: string }>, ambiguous: 'several_live' | 'several_tombstones'): OperationError {
return opError('page_identity_changed', 'Several pages claim the same imported origin.',