// ReleasePuller — when the overlay ALREADY holds the bytes, under another path.
//
// This is the case that stranded favela-android master-5d6d7e4 on stortron, the
// ACTIVE public backend, for a whole day. Measured there, not imagined:
//
// [release-pull] error: favela-android/versions/master-5d6d7e4/worklet.bundle:
// ENOENT: no such file or directory,
// rename ".../master-5d6d7e4/worklet.bundle.pull-tmp"
// -> ".../master-5d6d7e4/worklet.bundle"
//
// The rename's SOURCE is missing, which is the tell. `fetchFile` never wrote it,
// because hyper-overlay short-circuits a content hash it already has on disk:
//
// const localDisk = this._contentHashPaths.get(contentHash)
// if (localDisk) {
// const v = await verifyOnDisk(localDisk, contentHash)
// if (v.ok) return { destPath: localDisk, local: true, size: v.size }
// }
//
// It returns WHERE THE BYTES ALREADY ARE and ignores the caller's destPath. The
// puller assumed destPath was always written, renamed a temp that never existed,
// and failed — forever, since every retry takes the same branch.
//
// It bites exactly the artifacts that are UNCHANGED between builds, because those
// are the ones already present under an earlier version. Confirmed on the real
// data: master-5d6d7e4/worklet.bundle and master-53b10d2/worklet.bundle are both
// sha256 ca096cb2f6e7a907dbe2ada8ad27502156e252964b7295a0bf5eaa691f7aa43a.
//
// WHY THE EXISTING SUITE MISSED IT: `fakeOverlay` in release-puller.test.js
// always writes destPath and always returns `local: false`. Its comment states
// the contract as "give me this hash, write it to destPath" — which is the
// assumption under test here, and it is not what hyper-overlay promises. A
// fixture that encodes the same belief as the code cannot falsify it.
//
// The file DOES have to appear at the new path: downloads are served straight off
// disk by path (`GET /releases/:repo/:version/:artifact` -> createReadStream), so
// bytes sitting only under another version are unreachable at this one's URL.
// It does NOT have to be a second copy of those bytes — see the hardlink test.
import test from 'brittle'
import { mkdtempSync, readFileSync, existsSync, statSync } from '#node:fs'
import { join } from '#node:path'
import { tmpdir } from '#node:os'
import b4a from 'b4a'
import { ReleaseManager } from '../lib/release-manager.js'
import { ReleasePuller } from '../lib/services/release-puller.js'
function tmpDir (p) { return mkdtempSync(join(tmpdir(), p)) }
// The overlay's REAL behaviour for content it already holds: hand back the
// existing path, set `local`, and do NOT touch destPath.
function localHitOverlay (byHashLocal, publisherDir, index) {
const remote = new Map()
for (const f of index.files) remote.set(f.hash, join(publisherDir, f.path))
return {
localHits: [],
async fetchFile (hash, opts = {}) {
const already = byHashLocal.get(hash)
if (already) {
this.localHits.push(hash)
const size = statSync(already).size
// Deliberately does not write opts.destPath. This is the contract.
return { destPath: already, local: true, size }
}
const src = remote.get(hash)
if (!src) return null
const buf = readFileSync(src)
const { writeFileSync } = await import('#node:fs')
writeFileSync(opts.destPath, buf)
return { destPath: opts.destPath, local: false, size: buf.length }
}
}
}
async function scenario () {
// A publisher with two versions whose artifact is byte-identical, which is the
// normal case for a build input that did not change between commits.
const pubDir = tmpDir('rplh-pub-')
const pub = new ReleaseManager({ releaseDir: pubDir })
const payload = b4a.from('WORKLET-BUNDLE-BYTES')
await pub.publish({
repo: 'demo', version: 'v1',
artifacts: [{ path: 'worklet.bundle', data: payload }]
})
await pub.publish({
repo: 'demo', version: 'v2',
artifacts: [{ path: 'worklet.bundle', data: payload }]
})
const pubIndex = JSON.parse(readFileSync(join(pubDir, '_index.json'), 'utf8'))
// The peer already replicated v1, so it holds those exact bytes on disk.
const peerDir = tmpDir('rplh-peer-')
const peerRm = new ReleaseManager({ releaseDir: peerDir })
await peerRm.publish({
repo: 'demo', version: 'v1',
artifacts: [{ path: 'worklet.bundle', data: payload }]
})
const existing = join(peerDir, 'demo/versions/v1/worklet.bundle')
const v2 = pubIndex.files.find(f => f.path === 'demo/versions/v2/worklet.bundle')
const byHashLocal = new Map([[v2.hash, existing]])
const overlay = localHitOverlay(byHashLocal, pubDir, pubIndex)
const logs = []
const puller = new ReleasePuller({
releaseDir: peerDir, overlay, releases: peerRm, log: (l) => logs.push(l)
})
const client = { call: async () => pubIndex }
return { puller, client, peerDir, existing, payload, logs, overlay }
}
test('a locally-held artifact still lands at the new version path', async (t) => {
// THE test. Before the fix this failed with
// ENOENT ... rename "worklet.bundle.pull-tmp" -> "worklet.bundle"
// and the version stayed permanently incomplete on the mirror.
const { puller, client, peerDir, payload, logs } = await scenario()
const res = await puller.pullFrom(client)
const target = join(peerDir, 'demo/versions/v2/worklet.bundle')
t.ok(existsSync(target),
'the artifact must exist AT THIS VERSION\'S PATH — downloads are served by ' +
'path, so bytes under another version are unreachable here:\n ' +
logs.filter(l => l.includes('error')).join('\n '))
if (!existsSync(target)) return // the read below would abort the whole suite
t.alike(readFileSync(target), payload, 'and it is the right bytes')
t.is(puller.stats.errors.length, 0,
'no errors: ' + JSON.stringify(puller.stats.errors))
t.ok(res.ok, 'the pull reports success')
t.not(res.partial, true, 'and is not partial, so nothing is re-queued forever')
})
test('it is a hardlink, not a second copy of the bytes', async (t) => {
// The artifacts this fails on are precisely the ones that are UNCHANGED between
// builds, so they are exactly the ones a link makes free. Release versions are
// immutable by design and both paths are on one filesystem, and pgh's own tree
// store already hardlinks for this dedup — so this is the established pattern
// here, not a new trick.
const { puller, client, peerDir, existing } = await scenario()
await puller.pullFrom(client)
const target = join(peerDir, 'demo/versions/v2/worklet.bundle')
t.is(statSync(target).ino, statSync(existing).ino,
'same inode — the 37 MB is stored once, not twice')
})
test('a genuinely remote artifact is still fetched normally', async (t) => {
// The control. Without it, "lands at the path" could be satisfied by a fix that
// broke the ordinary remote path, which is the one that moves most bytes.
const { puller, client, peerDir, overlay } = await scenario()
await puller.pullFrom(client)
t.ok(existsSync(join(peerDir, 'demo/versions/v2/manifest.json')),
'the manifest, which the peer did NOT already hold, came over the wire')
t.is(overlay.localHits.length, 1,
'exactly one artifact took the local-hit branch; the rest were real fetches')
})
test('a local hit whose file vanished mid-pull is an error, not a crash', async (t) => {
// The overlay verifies on disk before reporting a local hit, but the file can
// still go between that check and the link. Fail closed and RECORD it, so the
// retry path sees a partial rather than a version silently marked complete.
const { peerDir } = await scenario()
const pubDir = tmpDir('rplh-pub2-')
const pub = new ReleaseManager({ releaseDir: pubDir })
await pub.publish({
repo: 'demo', version: 'v3',
artifacts: [{ path: 'gone.bin', data: b4a.from('X') }]
})
const idx = JSON.parse(readFileSync(join(pubDir, '_index.json'), 'utf8'))
const overlay = {
async fetchFile (hash, opts = {}) {
return { destPath: join(peerDir, 'does/not/exist.bin'), local: true, size: 1 }
}
}
const puller = new ReleasePuller({ releaseDir: peerDir, overlay })
const res = await puller.pullFrom({ call: async () => idx })
t.ok(puller.stats.errors.length > 0, 'the failure is recorded')
t.ok(res.partial || !res.ok, 'and reported as partial/failed so it is retried')
})