Skip to content

Commit d464f60

Browse files
fix(cli): zip the publish archive to the same bytes every time (#3358)
adm-zip stamps every entry with `new Date()` as it is constructed, and a ZIP timestamp resolves to two seconds — so archiving identical content twice gave different bytes whenever the two runs landed either side of a boundary. The archive's digest was a function of the clock rather than of its contents, which is backwards for something `cloud render` uploads and addresses by content. It surfaced as a CI flake: publishProject.test.ts asserts two archives built back to back are byte-identical, and both sides are the same expression, so the only way it can fail is non-determinism. The window is narrow, which is why it survived since July and why re-running always cleared it. Entry times are now fixed. Built from local components deliberately: `fromDate2DOS` reads getFullYear/getMonth/getHours, so a fixed instant would still encode differently per timezone — verified identical bytes under UTC, America/Los_Angeles and Asia/Kolkata. The new test moves the clock across a boundary, which is what reproduces it; back-to-back builds land in the same bucket almost always, which is exactly how it hid.
1 parent 228eabd commit d464f60

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎packages/cli/src/utils/publishProject.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,33 @@ describe("createPublishArchive (U6 cloud-render regression guard)", () => {
304304
// `createPublishArchive` is exactly the thin composition of
305305
// `buildPublishFileMap` + `zipPublishFileMap` with no baking hook, and that
306306
// a local video asset's original bytes/HTML pass through unmodified.
307+
it("zips the same files to the same bytes across a two-second boundary", () => {
308+
// The flake this pins: adm-zip stamps each entry with `new Date()` as it is
309+
// constructed, and a ZIP timestamp resolves to two seconds — so two runs
310+
// either side of a boundary produced different bytes for identical content.
311+
// The byte-identity assertion below could only ever fail this way, and did,
312+
// rarely enough to survive since July.
313+
//
314+
// Moving the clock is what reproduces it: back-to-back builds land in the
315+
// same bucket almost always, which is exactly why it hid.
316+
const dir = makeProjectDir();
317+
try {
318+
writeFileSync(join(dir, "index.html"), "<html></html>", "utf-8");
319+
vi.useFakeTimers();
320+
try {
321+
vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
322+
const first = createPublishArchive(dir);
323+
vi.setSystemTime(new Date("2026-01-01T00:00:03.000Z"));
324+
const second = createPublishArchive(dir);
325+
expect(first.buffer.equals(second.buffer)).toBe(true);
326+
} finally {
327+
vi.useRealTimers();
328+
}
329+
} finally {
330+
rmSync(dir, { recursive: true, force: true });
331+
}
332+
});
333+
307334
it("keeps a source video byte-identical and excludes proxies from the cloud-render archive", () => {
308335
const dir = makeProjectDir();
309336
try {

‎packages/cli/src/utils/publishProject.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,11 +504,32 @@ export function buildPublishFileMap(projectDir: string): Map<string, Buffer> {
504504

505505
/** Zip an in-memory archive file map (from `buildPublishFileMap`, optionally
506506
* transformed in between, e.g. by proxy baking) into the final archive buffer. */
507+
/**
508+
* Fixed entry timestamp, so the same files always zip to the same bytes.
509+
*
510+
* adm-zip stamps every entry with `new Date()` as it is constructed, and a ZIP
511+
* timestamp has two-second granularity — so archiving identical content twice
512+
* produced different bytes whenever the two runs landed either side of a
513+
* two-second boundary. That makes the archive's digest a function of the clock
514+
* rather than of its contents, which is the opposite of what a
515+
* content-addressed artifact needs.
516+
*
517+
* Built from local components on purpose: `fromDate2DOS` reads `getFullYear`,
518+
* `getMonth`, `getHours` and friends, so a fixed *instant* would still encode
519+
* differently in different timezones. Fixing the wall-clock reading is what
520+
* makes the bytes match across machines. 1980-01-01 is the earliest a DOS
521+
* timestamp can represent.
522+
*/
523+
const ARCHIVE_ENTRY_TIME = new Date(1980, 0, 1, 0, 0, 0, 0);
524+
507525
export function zipPublishFileMap(fileContents: Map<string, Buffer>): PublishArchiveResult {
508526
const archive = new AdmZip();
509527
for (const [filePath, content] of fileContents) {
510528
archive.addFile(filePath, content);
511529
}
530+
for (const entry of archive.getEntries()) {
531+
entry.header.time = ARCHIVE_ENTRY_TIME;
532+
}
512533

513534
return {
514535
buffer: archive.toBuffer(),

0 commit comments

Comments
 (0)