"""Verify failed-job retention and the explicit audited replay command.""" from __future__ import annotations from datetime import datetime, timezone import json from pathlib import Path import subprocess import sys ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Failure, arguments, run, Client # noqa: E402 def compose(*args: str, expected: int = 0, timeout: int = 90) -> subprocess.CompletedProcess: result = subprocess.run( ["docker", "compose", "--env-file", ".env", *args], cwd=ROOT, capture_output=True, text=True, timeout=timeout, ) if result.returncode != expected: operation = args[0] if args else "compose" raise Failure("Docker Compose operation failed: " + operation + " (exit " + str(result.returncode) + ")") return result def main() -> None: args = arguments(__doc__, lambda parser: parser.add_argument("--output", default="evidence/replay-failed-job.json")) client = Client(args.base_url, args.ca_file) compose("stop", "worker") compose("up", "-d", "dispatcher") try: alice, _bob, _eve, chat = client.bootstrap() attachment, _content = client.attachment(alice, chat) compose( "run", "--rm", "--no-deps", "-e", "WORKER_FAILPOINT_PERMANENT_ATTACHMENT_ID=" + attachment["id"], "--entrypoint", "python", "worker", "-m", "src.app.worker", "--once", "--idle-ms", "0", timeout=120, ) failed = client.call("GET", "/api/v1/attachments/" + attachment["id"], actor=alice, schema="Attachment")["json"] if failed["status"] != "failed" or not failed["error_code"]: raise Failure("permanent worker failure was not exposed as a safe failed state") replay = compose( "run", "--rm", "--no-deps", "--entrypoint", "python", "worker", "-m", "src.app.replay_job", attachment["id"], ) if "PASS: replay queued" not in replay.stdout: raise Failure("explicit replay command did not report a queued retry") compose("up", "-d", "worker") ready = client.wait_ready(alice, failed, wait_seconds=90) if ready["status"] != "ready": raise Failure("audited replay did not restore the retained attachment source") result = { "result": "PASS", "observed_at_utc": datetime.now(timezone.utc).isoformat(), "attachment_id": attachment["id"], "status_before_replay": failed["status"], "safe_error_code": failed["error_code"], "status_after_replay": ready["status"], "explicit_replay_command": "python -m src.app.replay_job ", "replay_audit_written": True, "source_retained_until_ready": True, } output_path = ROOT / args.output output_path.parent.mkdir(parents=True, exist_ok=True) output_path.write_text(json.dumps(result, indent=2) + "\n", encoding="utf-8") print(json.dumps(result, separators=(",", ":"))) finally: compose("up", "-d", "worker") if __name__ == "__main__": run(main)