We keep generated images in a small vault: each file is moved to a fixed path, given a number, and recorded in a SQLite ledger. A routine check-and-debug pass over it started from a clean state, with the existing test suite passing. The first thing it turned up was in the part of registration that is supposed to make failures harmless.
Cleaning up someone else’s file
When a registration is rejected, the code cleans up after itself: whatever it put in the vault for that attempt is removed, so a failed registration leaves nothing behind. The cleanup decided what to remove by looking at the target path. If a file was there, it was treated as this attempt’s file.
That holds only if the path was empty before the attempt. When a file already existed at the target path, the registration was rejected, and the cleanup then deleted the file that had been there all along. A step written to undo a failure was destroying something the failed attempt had never touched. A regression test in a temporary vault reproduced it: the existing file was really deleted.
The fix was to stop inferring ownership from the path. Registration now keeps a flag for whether it actually completed the move, and on failure it removes only files this registration created. Two cases were checked: on a conflict, the existing file and its number are kept; when registration fails at a later step, only the new copy is cleaned up.
What the rest of the check said
The test suite went from 22 tests to 24, all passing, with no warnings. A deep verification compared the SHA-256 of all 4,283 assets in the vault against the ledger: missing files, size mismatches, hash mismatches and orphan files were all 0. The database integrity and foreign-key checks came back clean.
The same pass found smaller problems in the listing API. A non-numeric limit returned a 500, and limit=-1 returned a 200 with all 4,283 entries, 1,172,302 bytes in one response. Bad values now return 400 and a valid limit is capped at 300. Two write endpoints also gained input checks: malformed JSON gets a 400, and a body over 1 MiB gets a 413.
The clean verification says the vault is whole today. It does not say whether the deleting path was ever hit in normal use; the log records the repro, not a past loss. The check covered this defect and the main operating paths, not every concurrency or failure case, and behavior after a reboot was not verified.
Cleanup code tends to get less attention than the code it cleans up after, because it only runs when something has already gone wrong. It should delete what it can prove it made, not whatever happens to be at the address.