diff --git a/site/files.js b/site/files.js index ac643d6be..ac4dab978 100644 --- a/site/files.js +++ b/site/files.js @@ -148,7 +148,20 @@ export async function mirror(env, rels, open, now = Date.now, attest = null) { else done.failed.push(`${tag}/${BUNDLES}`); } } - await prune(env, tags); + const kept = await prune(env, tags); + // a release the feeds named for one run only (the next tag came minutes + // later) can leave that run with its files copied and its budget spent; + // its bundles are filled in from the files R2 holds of it, while kept + for (const tag of kept) { + if (!attest || byVersion(tag, "v" + ATTESTED) < 0 || now() > until) continue; + const have = await held(env, tag); + if (!have || BUNDLES in have) continue; + const assets = {}; + for (const [name, sha256] of Object.entries(have)) if (sha256) assets[name] = { sha256 }; + if (!Object.keys(assets).length) continue; + if (await keepBundles(env, tag, { assets }, attest)) done.copied.push(`${tag}/${BUNDLES}`); + else done.failed.push(`${tag}/${BUNDLES}`); + } return done; } @@ -216,7 +229,8 @@ async function copy(env, tag, name, a, open) { } } -// prune deletes every tag but keep and the KEEP newest besides. +// prune deletes every tag but keep and the KEEP newest besides, and +// returns those KEEP newest. async function prune(env, keep) { let tags = [], cursor; try { @@ -227,7 +241,7 @@ async function prune(env, keep) { } while (cursor); } catch (e) { console.log("r2 prune: list", String(e)); - return; + return []; } const rest = tags.filter((t) => !keep.includes(t)).sort((a, b) => byVersion(b, a)); for (const tag of rest.slice(KEEP)) { @@ -240,6 +254,7 @@ async function prune(env, keep) { console.log("r2 prune: delete", tag, String(e)); } } + return rest.slice(0, KEEP); } function byVersion(a, b) { diff --git a/site/files.test.mjs b/site/files.test.mjs index b9b70509a..c153bc27d 100644 --- a/site/files.test.mjs +++ b/site/files.test.mjs @@ -179,6 +179,30 @@ test("mirror: a release's attestation bundles land in R2 as attestations.jsonl, assert.deepEqual(asked, []); }); +test("mirror: a kept release the feeds no longer name still gets its bundles, for the files R2 holds of it (v0.1.3978)", async () => { + const env = { RELEASES: bucket() }; + // v0.1.3978 was the newest for one run, which copied its files and spent + // its budget; by the next run v0.1.3979 had taken its place in the feed + await hold(env.RELEASES, "v0.1.3978", "a", "A"); + await hold(env.RELEASES, "v0.1.3978", "b", "B"); + await hold(env.RELEASES, "v0.1.3890", "a", "old"); + const rel = { version: "0.1.3979", assets: { c: { sha256: sha("C") } } }; + const asked = []; + const attest = async (h) => (asked.push(h), [{ for: h }]); + const done = await mirror(env, [rel], async () => new Response("C"), Date.now, attest); + assert.deepEqual(done.copied.sort(), ["v0.1.3978/attestations.jsonl", "v0.1.3979/attestations.jsonl", "v0.1.3979/c"]); + const res = await ask(env, "/files/v0.1.3978/attestations.jsonl"); + assert.equal(res.status, 200); + const lines = (await res.text()).trimEnd().split("\n").map((l) => JSON.parse(l)); + assert.deepEqual(lines.map((l) => l.for).sort(), [sha("A"), sha("B")].sort()); + // a kept release from before the attestations is not asked + assert.equal(asked.includes(sha("old")), false); + // written once: the next run asks nothing more + asked.length = 0; + await mirror(env, [rel], async () => new Response("C"), Date.now, attest); + assert.deepEqual(asked, []); +}); + test("mirror: a release from before the attestations is not asked for any", async () => { const env = { RELEASES: bucket() }; const rel = { version: "0.1.3890", assets: { a: { sha256: sha("A") } } };