DEV Community

Alejandro Silva Mendez
Alejandro Silva Mendez

Posted on AI-assisted

Fail-closed Python automation: dry runs, single-use approvals, and safe retries

When an automation can create or update records, a successful script is not enough. The workflow should make unsafe states difficult to reach and make every decision auditable.

This article presents a small pattern I use for isolated automation assessments:

1. Make dry-run the default

A dry run should validate inputs, compute the intended payload, and produce a machine-readable outcome without performing a live write. A caller must opt in to live mode explicitly.

2. Approve the exact payload

The approval should bind to a canonical JSON representation of the payload. If any field changes after approval, the signature must no longer verify. This avoids approving one action and executing another.

3. Make the approval single-use

Store a nonce or approval identifier in a transactional table and consume it atomically. A retry with the same approval then returns a clear duplicate outcome instead of repeating the write.

4. Reserve work across workers

A unique reservation key plus a short database transaction prevents two workers from executing the same logical operation concurrently. The reservation is shared across processes, not kept only in memory.

5. Keep an append-only audit

Write sanitized JSON lines for validation, approval, reservation, execution, duplicate detection, and failures. Never put secrets or full customer payloads in the audit. Include a correlation ID and enough metadata to reproduce the decision.

6. Test the failure paths

The important tests are not only the happy path: verify that dry-run never writes, a modified payload fails approval, a nonce cannot be reused, and two worker instances share the same reservation.

I built a small synthetic reference implementation with these properties here: https://github.com/AlejandroSilvaMendez/fail-closed-python-assessment

For focused Playwright, API, or Python reliability work, I also offer small fixed-price scopes: https://alejandrosilvamendez.github.io/qa-automation-services/

AI coding tools helped draft the example, and I reviewed the design, tests, and disclosure personally.

Top comments (0)