From 8c5f9046b5bf5678e5b0107e477af888c6c587d7 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 08:52:14 +0100 Subject: [PATCH 1/2] Clean up GitHub API payloads when staging fails Architecture-Review: unchanged; GitHub keeps the existing request staging boundary and scopes the temporary file to the operation. Co-Authored-By: Codex --- src/loopzero/github.py | 18 ++++++------------ tests/test_gh_contract.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/loopzero/github.py b/src/loopzero/github.py index 874b79fa..99692359 100644 --- a/src/loopzero/github.py +++ b/src/loopzero/github.py @@ -496,13 +496,6 @@ def _paged(endpoint: str, key: str = "check_runs") -> list: return list(_iter_paged(endpoint, key)) -def _with_file(content: str) -> Path: - fd, name = tempfile.mkstemp(suffix=".loopzero", text=True) - with open(fd, "w", encoding="utf-8") as handle: - handle.write(content) - return Path(name) - - def api_get(endpoint: str) -> object: """`gh api ` parsed as JSON (None on an empty body).""" return _gh_json("api", endpoint) @@ -530,11 +523,12 @@ def _not_found(number: int, exc: Exception) -> GhError: def _api(endpoint: str, payload: dict, method: str = "POST", token: str | None = None) -> object: - path = _with_file(json.dumps(payload)) - try: - return _gh_json("api", endpoint, "--method", method, "--input", str(path), token=token) - finally: - path.unlink(missing_ok=True) + with tempfile.NamedTemporaryFile( + mode="w", suffix=".loopzero", encoding="utf-8", delete_on_close=False + ) as handle: + handle.write(json.dumps(payload)) + handle.close() + return _gh_json("api", endpoint, "--method", method, "--input", handle.name, token=token) def merge_hold(number: int, labels: tuple[str, ...]) -> str | None: diff --git a/tests/test_gh_contract.py b/tests/test_gh_contract.py index 9f31898a..53182069 100644 --- a/tests/test_gh_contract.py +++ b/tests/test_gh_contract.py @@ -7,6 +7,7 @@ import re import shutil import subprocess +import sys from pathlib import Path import pytest @@ -27,6 +28,38 @@ REAL_GH = shutil.which("gh") +def test_api_removes_payload_when_staging_fails(tmp_path): + script = """ +import errno +import resource +import signal +import tempfile + +from loopzero import github + +def unexpected_dispatch(*args, **kwargs): + raise AssertionError("failed payload must not be dispatched") + +github._gh_json = unexpected_dispatch +tempfile.gettempdir() +signal.signal(signal.SIGXFSZ, signal.SIG_IGN) +_, hard = resource.getrlimit(resource.RLIMIT_FSIZE) +resource.setrlimit(resource.RLIMIT_FSIZE, (0, hard)) +try: + github._api("repos/example/project/issues", {"body": "synthetic"}) +except OSError as exc: + assert exc.errno == errno.EFBIG +else: + raise AssertionError("payload staging should fail") +""" + done = subprocess.run( + [sys.executable, "-c", script], env={**os.environ, "TMPDIR": str(tmp_path)}, + capture_output=True, text=True, check=False, + ) + assert done.returncode == 0, done.stderr + assert not list(tmp_path.glob("*.loopzero")) + + @pytest.fixture(scope="module") def captured_argv(tmp_path_factory: pytest.TempPathFactory) -> list[dict]: """Drive every public operation through production code and retain its exact argv.""" From 24105f1e851ad61469b4ef49a5997976d28a6597 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 09:02:52 +0100 Subject: [PATCH 2/2] Keep the staging failure regression within the test budget Co-Authored-By: Codex --- tests/test_gh_contract.py | 47 ++++++++++++++------------------------- 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/tests/test_gh_contract.py b/tests/test_gh_contract.py index 53182069..2869e0df 100644 --- a/tests/test_gh_contract.py +++ b/tests/test_gh_contract.py @@ -2,12 +2,15 @@ from __future__ import annotations +import errno import json import os import re +import resource import shutil +import signal import subprocess -import sys +import tempfile from pathlib import Path import pytest @@ -28,35 +31,19 @@ REAL_GH = shutil.which("gh") -def test_api_removes_payload_when_staging_fails(tmp_path): - script = """ -import errno -import resource -import signal -import tempfile - -from loopzero import github - -def unexpected_dispatch(*args, **kwargs): - raise AssertionError("failed payload must not be dispatched") - -github._gh_json = unexpected_dispatch -tempfile.gettempdir() -signal.signal(signal.SIGXFSZ, signal.SIG_IGN) -_, hard = resource.getrlimit(resource.RLIMIT_FSIZE) -resource.setrlimit(resource.RLIMIT_FSIZE, (0, hard)) -try: - github._api("repos/example/project/issues", {"body": "synthetic"}) -except OSError as exc: - assert exc.errno == errno.EFBIG -else: - raise AssertionError("payload staging should fail") -""" - done = subprocess.run( - [sys.executable, "-c", script], env={**os.environ, "TMPDIR": str(tmp_path)}, - capture_output=True, text=True, check=False, - ) - assert done.returncode == 0, done.stderr +def test_api_removes_payload_when_staging_fails(tmp_path, monkeypatch): + monkeypatch.setattr(tempfile, "tempdir", str(tmp_path)) + monkeypatch.setattr(github, "_gh_json", lambda *args, **kwargs: pytest.fail("unexpected dispatch")) + original_limit = resource.getrlimit(resource.RLIMIT_FSIZE) + handler = signal.signal(signal.SIGXFSZ, signal.SIG_IGN) + try: + resource.setrlimit(resource.RLIMIT_FSIZE, (0, original_limit[1])) + with pytest.raises(OSError) as failure: + github._api("repos/example/project/issues", {"body": "synthetic"}) + finally: + resource.setrlimit(resource.RLIMIT_FSIZE, original_limit) + signal.signal(signal.SIGXFSZ, handler) + assert failure.value.errno == errno.EFBIG assert not list(tmp_path.glob("*.loopzero"))