@@ -1346,25 +1346,31 @@ func (c *Cortex) backfillTraceUsage() error {
13461346 return nil
13471347}
13481348
1349- // detectCopiedDirectory refuses to start when events this cortex *authored*
1350- // (origin == its display name) are recorded under a cortex_id other than the
1351- // one cortex.md now declares. That is the signature of a directory copied or
1352- // re-identified from another instance: its own history lives under a stale
1353- // identity, and running it would create two physical Cortexes claiming the same
1354- // id in any federation they joined, silently merging vector clocks.
1349+ // detectCopiedDirectory refuses to start when events that appear locally
1350+ // authored are recorded under a cortex_id other than the one cortex.md now
1351+ // declares. That is the signature of a directory copied or re-identified from
1352+ // another instance: its own history lives under a stale identity, and running
1353+ // it would create two physical Cortexes claiming the same id in any federation
1354+ // they joined, silently merging vector clocks.
13551355//
1356- // Crucially, the check keys on origin, NOT on "any foreign cortex_id". Events
1357- // replayed from peers via federation legitimately carry the originating
1358- // cortex's id, so a receiver or subscribe cortex normally holds many
1359- // foreign-id events with none of its own — that is expected, not a copy. The
1360- // earlier id-only heuristic flagged exactly that case, making such a cortex
1361- // unopenable (serve restart and every CLI command, including the reset-peer
1362- // recovery) after its first sync. Scoping to origin matches precisely the rows
1363- // `noema migrate cortex-id --reset` re-keys, so the guard and its remedy agree.
1356+ // The check cannot rely on origin alone. It is a display label, and real
1357+ // federations may contain multiple distinct cortex IDs with the same name. A
1358+ // peer event with origin == c.Name is legitimate when the foreign cortex_id is
1359+ // one of our pinned peers, so those IDs are excluded from the copy count.
13641360func (c * Cortex ) detectCopiedDirectory () error {
13651361 var ownUnderForeignID int
13661362 if err := c .DB .QueryRow (
1367- `SELECT COUNT(*) FROM events WHERE origin = ? AND cortex_id != '' AND cortex_id != ?` ,
1363+ `SELECT COUNT(*)
1364+ FROM events
1365+ WHERE origin = ?
1366+ AND cortex_id != ''
1367+ AND cortex_id != ?
1368+ AND cortex_id NOT IN (
1369+ SELECT value
1370+ FROM federation_state
1371+ WHERE key LIKE 'peer:%:cortex_id'
1372+ AND value != ''
1373+ )` ,
13681374 c .Name , c .ID ,
13691375 ).Scan (& ownUnderForeignID ); err != nil {
13701376 return nil // table missing or unreadable — treat as fresh, don't block
0 commit comments