// What OPENS a stale core in the shared store? Corestore does — on remote interest.
//
// This answers the gating unknown of hyper-social#22: a 4-block copy of loser-club's indexer writer
// sits in stortron's SHARED corestore, diverging from the authoritative line at block 1, and the
// conflict watcher proved it PARTICIPATES (store=store, length=4) even though loser-club's StateBase
// opens from resyncStore only. The standing explanation was that it could not participate, citing
// side-store-channel-priority.test.js: "an unopened on-disk copy claims nothing."
//
// That claim is not what the cited test measures. It asserts only that the SIDE store still reached
// length 3 — it never checks whether the shared store's copy was opened, and its on-disk copy is the
// SAME history as the writer's, so opening it would have been invisible either way. Production's copy
// is a DIFFERENT history, which is the case that was never exercised.
//
// The mechanism, read from corestore 7.11.0 (index.js):
//
// replicate() installs ondiscoverykey on every protocol stream (:487-491)
// -> _attachMaybe(muxer, discoveryKey) (:453)
// returns EARLY only if the core is neither already open NOR on disk:
// !this.cores.opened(dk) && !(await this.storage.hasCore(dk)) (:456-457)
// otherwise: _openCore(dk, { createIfMissing: false }) (:463)
// core.ready(); core.replicator.attachTo(muxer) (:466-469)
//
// So a core that exists ON DISK is opened and attached the moment a peer announces its discovery key.
// "Leftover files alone are harmless" is false. Nothing in hpp has to open it; the remote does.
//
// Run: bare test/shared-store-autoopen.test.js
import '../lib/bare-globals.js'
import test from 'brittle'
import fs from 'bare-fs'
import b4a from 'b4a'
import crypto from 'hypercore-crypto'
import Corestore from 'corestore'
const tmp = (p) => { const d = '/tmp/hpp-autoopen-' + p + '-' + Math.floor(Math.random() * 1e9); fs.mkdirSync(d, { recursive: true }); return d }
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
const dkHex = (key) => b4a.toString(crypto.discoveryKey(key), 'hex')
function pipe (t, a, b) {
a.pipe(b).pipe(a)
t.teardown(() => { try { a.destroy(); b.destroy() } catch {} })
}
test('an UNOPENED on-disk core IS auto-opened when a peer announces its discovery key', async (t) => {
const wStore = new Corestore(tmp('writer'))
await wStore.ready()
const wCore = wStore.get({ name: 'the-base' })
await wCore.ready()
await wCore.append([b4a.from('a'), b4a.from('b'), b4a.from('c')])
t.teardown(async () => { try { await wStore.close() } catch {} })
// Put the core on disk in the shared store, then close so nothing holds it open.
const sharedDir = tmp('shared')
{
const s = new Corestore(sharedDir); await s.ready()
const c = s.get({ key: wCore.key }); await c.ready()
const a = wStore.replicate(true); const b = s.replicate(false)
a.pipe(b).pipe(a)
await sleep(1200)
t.is(c.length, 3, 'precondition: the shared store has the core ON DISK')
try { a.destroy(); b.destroy() } catch {}
await sleep(200)
await s.close()
}
// Reopen. Deliberately never call shared.get() for that key — nothing in-process opens it.
const shared = new Corestore(sharedDir); await shared.ready()
t.teardown(async () => { try { await shared.close() } catch {} })
const dk = dkHex(wCore.key)
t.absent(shared.cores.opened(dk), 'precondition: after reopen the core is NOT open in-process')
// The writer holds it open, so replicate() announces it and the remote's ondiscoverykey fires.
pipe(t, wStore.replicate(true), shared.replicate(false))
await sleep(1500)
// `cores.opened(dk)` is the exact predicate corestore itself checks at index.js:456.
t.ok(shared.cores.opened(dk),
'corestore AUTO-OPENED the on-disk core on remote interest — "leftover files are harmless" is FALSE')
})
test('PRODUCTION SHAPE: a forked on-disk copy is auto-opened and served, and only the PEER sees the conflict', async (t) => {
// Two stores sharing a primaryKey derive the SAME key for the same name, so both can author under it
// — the duplicate-writer shape that produces a fork. (`unsafe` is required, and apt: this is the
// unsafe thing.) Mirrors production, where one key ended up with two signed histories.
const primaryKey = crypto.randomBytes(32)
const goodDir = tmp('good')
const forkDir = tmp('fork')
let key = null
// The FORK: 4 blocks, differing from the authoritative line at the very first block — exactly the
// measured shape in #22 (length 4, divergesAt 1). Written, then closed so it is merely ON DISK.
{
const s = new Corestore(forkDir, { primaryKey, unsafe: true }); await s.ready()
const c = s.get({ name: 'the-base' }); await c.ready()
await c.append([b4a.from('FORK-0'), b4a.from('FORK-1'), b4a.from('FORK-2'), b4a.from('FORK-3')])
key = c.key
await s.close()
}
// The authoritative line: same key, longer, different from block 0.
const good = new Corestore(goodDir, { primaryKey, unsafe: true }); await good.ready()
const goodCore = good.get({ name: 'the-base' }); await goodCore.ready()
await goodCore.append([b4a.from('GOOD-0'), b4a.from('GOOD-1'), b4a.from('GOOD-2'), b4a.from('GOOD-3'), b4a.from('GOOD-4')])
t.teardown(async () => { try { await good.close() } catch {} })
t.ok(b4a.equals(goodCore.key, key), 'both stores derived the SAME key — one key, two histories')
// The forked store reopens and never opens that core in-process (loser-club's StateBase uses the
// side store; nothing asks the shared store for this key).
const forked = new Corestore(forkDir, { primaryKey, unsafe: true }); await forked.ready()
t.teardown(async () => { try { await forked.close() } catch {} })
const dk = dkHex(key)
t.absent(forked.cores.opened(dk), 'precondition: the forked copy is NOT open in-process')
let peerSawConflict = false
goodCore.on('conflict', () => { peerSawConflict = true })
pipe(t, good.replicate(true), forked.replicate(false))
await sleep(2500)
t.ok(forked.cores.opened(dk), 'the forked on-disk copy was AUTO-OPENED by remote interest alone')
t.ok(peerSawConflict,
'and serving it made the PEER detect the conflict — the host holding the good line is the one that reports')
// The asymmetry measured in production on 2026-07-30: tabertron (good line) reported 6 detections,
// stortron (the 4-block copy) reported 0. The server of a fork has nothing to compare against, so it
// stays silent — which is why the quiet host looks healthy and is in fact the source.
const forkedCore = forked.get({ key })
await forkedCore.ready()
t.absent(forkedCore.length > 4 && peerSawConflict === false,
'the forked side does not advance past its own history by serving it')
})