// PointerSubstrate.migrateInline — convert INLINE RECORDS in the autobase into fetchable pointers.
//
// A DID written through the LEGACY substrate but now running the pointer design has full records where
// { $oid, size } pointers belong. It reads perfectly on the node holding it — _resolve passes an
// undefined $oid to ensureLocal, which falls back to the local file — and is UNFETCHABLE BY EVERY OTHER
// NODE, because there is no oid to fetch by. A co-owner converges the keys and can never turn them into
// bytes.
//
// Live on loser-club (2026-07-27): all three records inline
// (app.bsky.feed.generator/loser-club, app.bsky.feed.post/self.hello, net.favela.federation/self),
// so that DID cannot be co-owned at all regardless of roster or config.
//
// backfill() cannot fix it: it skips any key that already has a value, which is correct for its job
// (never clobber a converged pointer or tombstone) but an inline record is such a value.
//
// The load-bearing property is that CONTENT IS PRESERVED. What the repo publishes today is the LOCAL
// FILE, not the inline copy, so the pointer must address the file's bytes — otherwise the signed repo
// changes underneath the migration.
//
// Run: brittle-node test/migrate-inline.test.js
import test from 'brittle'
import fs from 'fs'
import os from 'os'
import path from 'path'
import crypto from 'hypercore-crypto'
import * as dagCbor from '@ipld/dag-cbor'
import { OverlayRepoStore } from '../lib/overlay-repo-store.js'
import { PointerSubstrate } from '../lib/pointer-substrate.js'
const tmp = () => fs.mkdtempSync(path.join(os.tmpdir(), 'mig-'))
const GEN = 'app.bsky.feed.generator'
const POST = 'app.bsky.feed.post'
function memKV () {
const m = new Map()
return {
_m: m,
async set (k, v) { m.set(k, v) },
async del (k) { m.delete(k) },
async get (k) { return m.has(k) ? m.get(k) : null },
async list (prefix = '') { const o = {}; for (const [k, v] of m) if (k.startsWith(prefix)) o[k] = v; return o },
async listNow (prefix = '') { return this.list(prefix) }
}
}
function mkOverlay (net) {
return {
async registerFile (overlayPath, diskPath) {
const bytes = fs.readFileSync(diskPath)
const oid = crypto.data(bytes).toString('hex')
net.set(oid, bytes)
return { contentHash: oid }
},
async fetchFile (oid, o = {}) {
const bytes = net.get(oid)
if (!bytes) return null
fs.writeFileSync(o.destPath, bytes)
return { destPath: o.destPath, size: bytes.length }
},
async hashFile (p) {
let bytes
try { bytes = fs.readFileSync(p) } catch { return null }
return { contentHash: crypto.data(bytes).toString('hex'), size: bytes.length }
}
}
}
const GEN_REC = { $type: GEN, did: 'did:web:x', displayName: 'loser-club', description: 'a feed', createdAt: '2026-07-01T00:00:00.000Z' }
const POST_REC = { $type: POST, text: 'hello from loser-club', createdAt: '2026-07-01T00:00:00.000Z' }
// Reproduce the live shape: the INLINE RECORD is in the autobase and the record FILE is on disk.
async function seedInline (sb, store, collection, rkey, record) {
fs.mkdirSync(path.join(store._recordsDir, collection), { recursive: true })
fs.writeFileSync(path.join(store._recordsDir, collection, rkey + '.dagcbor'), Buffer.from(dagCbor.encode(record)))
await sb.set(collection + '/' + rkey, record) // the legacy substrate wrote the whole record here
}
test('inline records become pointers that address the SAME bytes (published content unchanged)', async (t) => {
const net = new Map()
const root = tmp()
const store = await new OverlayRepoStore(root, { overlay: mkOverlay(net), did: 'did:plc:lc' }).ready()
const sb = memKV()
const sub = new PointerSubstrate(sb, store)
await seedInline(sb, store, GEN, 'loser-club', GEN_REC)
await seedInline(sb, store, POST, 'self.hello', POST_REC)
// Precondition: the entries are records, not pointers, and nothing is fetchable by oid.
t.absent((await sb.get(GEN + '/loser-club')).$oid, 'precondition: the autobase holds a record, not a pointer')
const beforeOid = (await store.localOid(GEN, 'loser-club')).oid
const beforeRec = await sub.get(GEN + '/loser-club')
t.alike(beforeRec, GEN_REC, 'precondition: it resolves locally (via the file fallback)')
const r = await sub.migrateInline()
t.is(r.converted, 2, 'both inline records converted')
t.is(r.mismatched, 0, 'no key was refused for changing content')
t.is(r.failed, 0, 'and none failed')
const ptr = await sb.get(GEN + '/loser-club')
t.ok(ptr.$oid, 'the entry is now a pointer')
t.is(ptr.$oid, beforeOid, 'addressing EXACTLY the bytes that were already resolving — the repo does not change')
t.is(ptr.size, Buffer.from(dagCbor.encode(GEN_REC)).length, 'and carries the record\'s real byte length')
t.alike(await sub.get(GEN + '/loser-club'), GEN_REC, 'the record still resolves, byte-identical')
t.ok(net.has(ptr.$oid), 'the bytes are now registered with the overlay — a PEER can fetch them by oid')
})
test('a peer holding only the pointer can now resolve the record (the whole point)', async (t) => {
const net = new Map()
const store = await new OverlayRepoStore(tmp(), { overlay: mkOverlay(net), did: 'did:plc:lc' }).ready()
const sb = memKV()
const sub = new PointerSubstrate(sb, store)
await seedInline(sb, store, POST, 'self.hello', POST_REC)
// BEFORE: a co-owner replicates the entry. It has no oid, so it can never become bytes.
const peerStore = await new OverlayRepoStore(tmp(), { overlay: mkOverlay(net), did: 'did:plc:lc' }).ready()
const peerSb = memKV()
await peerSb.set(POST + '/self.hello', await sb.get(POST + '/self.hello'))
const peerSub = new PointerSubstrate(peerSb, peerStore)
t.is(await peerSub.get(POST + '/self.hello'), null, 'CONTROL: the peer cannot resolve an inline entry — no oid to fetch by')
await sub.migrateInline()
const peerSb2 = memKV()
await peerSb2.set(POST + '/self.hello', await sb.get(POST + '/self.hello')) // replicate the POINTER
const peerSub2 = new PointerSubstrate(peerSb2, await new OverlayRepoStore(tmp(), { overlay: mkOverlay(net), did: 'did:plc:lc' }).ready())
t.alike(await peerSub2.get(POST + '/self.hello'), POST_REC, 'after migration the peer fetches the record by oid')
})
test('idempotent: already-correct pointers and tombstones are left alone', async (t) => {
const net = new Map()
const store = await new OverlayRepoStore(tmp(), { overlay: mkOverlay(net), did: 'did:plc:lc' }).ready()
const sb = memKV()
const sub = new PointerSubstrate(sb, store)
await sub.set(POST + '/proper', POST_REC) // a real pointer
await sb.set(POST + '/dead', { $deleted: true }) // a tombstone
await seedInline(sb, store, GEN, 'loser-club', GEN_REC) // one inline record
const first = await sub.migrateInline()
t.is(first.converted, 1, 'only the inline record was converted')
t.is(first.alreadyPointer, 1, 'the healthy pointer was skipped')
t.is(first.tombstones, 1, 'the tombstone was skipped — never resurrected')
const second = await sub.migrateInline()
t.is(second.converted, 0, 'a second run converts nothing')
t.is(second.alreadyPointer, 2, 'everything is now a pointer')
t.alike(await sb.get(POST + '/dead'), { $deleted: true }, 'the tombstone is untouched')
})
test('reserved keys are never touched', async (t) => {
const net = new Map()
const store = await new OverlayRepoStore(tmp(), { overlay: mkOverlay(net), did: 'did:plc:lc' }).ready()
const sb = memKV()
const sub = new PointerSubstrate(sb, store)
await sb.set('__commit__/3kzzz', { rev: '3kzzz', ops: [] })
await sb.set('__members__', { a: 1 })
await sb.set('__blob__/bafkreiX', { $oid: 'abc', size: 1 })
const r = await sub.migrateInline()
t.is(r.total, 0, 'no reserved key was considered a record')
t.is(r.converted, 0, 'and nothing was rewritten')
t.alike(await sb.get('__commit__/3kzzz'), { rev: '3kzzz', ops: [] }, 'the firehose frame is intact')
})
test('with no local file the inline record itself is used (it is the only copy)', async (t) => {
const net = new Map()
const store = await new OverlayRepoStore(tmp(), { overlay: mkOverlay(net), did: 'did:plc:lc' }).ready()
const sb = memKV()
const sub = new PointerSubstrate(sb, store)
await sb.set(POST + '/orphan', POST_REC) // inline entry, NO file on disk
const r = await sub.migrateInline()
t.is(r.converted, 1, 'converted')
t.is(r.fromInline, 1, 'and reports that the content came from the inline copy, not a file')
t.alike(await sub.get(POST + '/orphan'), POST_REC, 'the record resolves afterwards')
})