Product case study · November 26, 2026

Ask Claude Code to make gzip exports reproducible

Fix gzip exports whose bytes change with the clock or filename. Give Claude Code a small repair contract and verify it with unchanged Python tests.

Reading time
6 min
Checked
Nov 26, 2026
A charcoal paper press produces two matching parcels with blue dotted seals beside a detached clock disk.
The same input should produce the same artifact within your pinned build environment.
Bottom line

Check the compressed bytes as well as the restored payload. A successful decompression can hide changing archive metadata.

Your export job compresses the same report twice. Both archives open correctly, but their checksums differ. Before asking a coding agent to rewrite the report generator, check the archive wrapper: a timestamp or filename can change even when the report has not.

This lab gives Claude Code a narrow repair task: make an in-memory gzip export repeatable within one Python and compression-library environment. You will check the whole artifact and the restored payload separately.

You need Bash, Python 3 with its standard gzip module, and installed, signed-in Claude Code. The Python examples and reference repair were tested locally; the prompt is an authored exercise, not a recorded Claude session. Official sources were checked November 26, 2026.

Create an exporter with hidden variability

Use a fresh practice directory. The exporter accepts bytes and a display name; it returns compressed bytes without writing a file.

gzip_lab=$(mktemp -d /tmp/claude-gzip-lab.XXXXXX)
cd "$gzip_lab"
cat > exporter.py <<'PY'
import gzip
import io


def pack(payload, display_name):
    buffer = io.BytesIO()
    with gzip.GzipFile(
        filename=display_name,
        mode="wb",
        fileobj=buffer,
        compresslevel=6,
    ) as archive:
        archive.write(payload)
    return buffer.getvalue()
PY

Python’s GzipFile documentation explains the two relevant defaults: omitting mtime uses the current time, and a supplied filename can enter the gzip header. Neither changes the decompressed report.

BytesIO holds binary data in memory. That keeps this exercise focused on export behavior rather than file permissions or output directories.

Fix the acceptance criteria before the code

Save this as SPEC.md:

pack(payload, display_name) accepts bytes and a string display name.
Return a complete gzip stream that decompresses to the exact payload.
For identical payloads in the same Python/zlib environment, output
must stay identical when the clock or display_name changes.
Support empty bytes and arbitrary binary bytes.
Keep both function arguments for caller compatibility, but do not
embed the display name in the archive. Use compression level 6.
Use only the standard library. Do not write files or add a CLI.
Cross-version and cross-platform byte identity are outside this lab.

The filename decision is deliberate. If your consumers need an embedded name, choose a canonical name as a separate requirement. Silently preserving every caller’s name would conflict with this contract.

Save the following as test_exporter.py:

import gzip
import unittest
from unittest.mock import patch
from exporter import pack


class ExportTests(unittest.TestCase):
    def test_clock_does_not_change_artifact(self):
        with patch("gzip.time.time", return_value=1700000000):
            first = pack(b"same report\n", "report.csv")
        with patch("gzip.time.time", return_value=1700000060):
            second = pack(b"same report\n", "report.csv")
        self.assertEqual(first, second)

    def test_display_name_does_not_change_artifact(self):
        with patch("gzip.time.time", return_value=1700000000):
            first = pack(b"same report\n", "morning.csv")
            second = pack(b"same report\n", "evening.csv")
        self.assertEqual(first, second)

    def test_round_trip(self):
        for payload in (b"", b"report\n", bytes(range(256))):
            with self.subTest(payload=payload):
                self.assertEqual(
                    gzip.decompress(pack(payload, "report.csv")),
                    payload,
                )

    def test_different_payloads_remain_distinct(self):
        first = pack(b"report A", "report.csv")
        second = pack(b"report B", "report.csv")
        self.assertNotEqual(first, second)
        self.assertEqual(gzip.decompress(first), b"report A")
        self.assertEqual(gzip.decompress(second), b"report B")

Run:

python3 -m unittest -v test_exporter

The local baseline produced two failures and two passes. The round-trip test passes even though the artifact changes. That is the hole a decompression-only check would leave.

The clock test uses unittest.mock.patch to control time temporarily. There is no sleep and no race against the next second. Its gzip.time.time target follows CPython’s implementation; if another Python implementation does not expose that target, investigate the harness before diagnosing the exporter. A patch-target error is different from a failed equality assertion.

Hand Claude Code the bounded repair

Start claude in the practice directory and paste:

Read SPEC.md, exporter.py and test_exporter.py.
Run python3 -m unittest -v test_exporter and report the baseline.
Explain why the round-trip test passes while byte equality fails.
Change only exporter.py to satisfy SPEC.md and the unchanged tests.
Keep gzip.GzipFile, the two-argument pack interface and level 6.
Do not edit tests, install packages, access other folders, delegate,
commit, or add services. Run the same tests after the repair.
Report actual output and the scope of the reproducibility claim.

Anthropic’s common workflows supports giving a coding agent a reproduction and asking it to verify its repair. Here, the fixed tests make the acceptance decision concrete: an explanation about metadata is insufficient if the output still changes.

If the agent removes the filename test, put the original test back. If it proposes comparing only decompressed content, remind it that artifact identity is part of the task. If it hardcodes an archive, the distinct-payload and round-trip checks should expose that shortcut.

Review the small repair

A reference implementation is:

import gzip
import io


def pack(payload, display_name):
    buffer = io.BytesIO()
    with gzip.GzipFile(
        filename="",
        mode="wb",
        fileobj=buffer,
        compresslevel=6,
        mtime=0,
    ) as archive:
        archive.write(payload)
    return buffer.getvalue()

The empty filename omits that header field; mtime=0 removes clock dependence. Keep the compression level explicit. Retrieve the buffer after leaving the with block so the gzip stream is complete. Closing GzipFile leaves the supplied buffer open. These behaviors are documented in the gzip reference.

Run the same test command again. The reference repair passed all four methods locally on CPython 3.14.4 with zlib 1.3.1. Review exporter.py and compare the test file with the original above before accepting an agent’s result.

The retained display_name argument is intentionally unused. Removing it would break existing callers even though the archive tests could be rewritten to pass. Keep that compatibility decision visible in the review.

Know what the passing result establishes

This fixture checks the requirement you gave the agent in one environment. It does not prove that another Python release, zlib build, or compression implementation will emit identical bytes. Record your runtime when you save the result:

python3 - <<'PY'
import platform
import zlib
print("Python:", platform.python_version())
print("zlib runtime:", zlib.ZLIB_RUNTIME_VERSION)
PY

For a real export pipeline, pin the environment and add your own representative input fixtures. If a checksum still changes there, compare the uncompressed input bytes first. Different row order, line endings, or serialization can change the payload before gzip sees it; this repair does not normalize them.

You now have a reviewable agent task: one implementation file, a stated compatibility boundary, and tests that distinguish a readable archive from a repeatable artifact. For help establishing the session boundary, use the Claude Code project setup lesson.

Sources

Checked November 26, 2026:

Put this to work

Why identical source bytes can produce different gzip artifacts.

Try

Repair a tiny gzip exporter in a disposable practice folder.

Prove it worked

Run four unchanged tests before and after the repair, including controlled clock and filename changes.

Keep in view

  • Expose two causes of changing gzip output: creation time and embedded filename.
  • Give Claude Code a repair boundary with fixed acceptance tests.
  • Verify byte equality and lossless decompression separately.
Learn the workflow: Prepare a scoped Claude Code project session