A step is idempotent when running it twice leaves the same state as running it once. Directory creation and fixed-content copies qualify, while bare appends do not.
Step | Idempotent | Why |
|---|---|---|
Create directory if missing | Yes | The second run finds the directory already there. |
Copy a fixed file over its target | Yes | The same bytes land on every run. |
Append a line to a log | No | Each run adds another line. |
Install the newest release | No | The newest release may differ between runs. |
Many tools preview work with a flag such as make -n or a --dry-run spelling. The exact spelling and output vary by tool, so check the local manual.
The transcript below is schematic, not output from a specific system. Nothing here is executed.
schematic transcript (not run here):
$ make -n clean
rm -f build/a.o build/b.o
the two listed paths are the whole planned effectThe program below models one setup step with a preview mode. The preview changes nothing, the live run sets four keys, and a rerun adds nothing.
import sys
def apply_step(state, dry_run):
planned = ["host_list", "report_dir", "index", "stamp"]
if dry_run:
return list(planned)
for key in planned:
state[key] = 1
return list(planned)
def main():
bad = 0
state = {}
preview = apply_step(state, True)
if len(preview) != 4:
bad += 1
if len(state) != 0:
bad += 1
apply_step(state, False)
if len(state) != 4:
bad += 1
before = sorted(state)
apply_step(state, False)
if sorted(state) != before:
bad += 1
if len(state) != 4:
bad += 1
if bad:
print("FAIL: dry-run counts wrong")
sys.exit(1)
print("PASS: dry run changes 0 keys, live run sets 4 keys, rerun sets 0")
main()Assume, as a stated example parameter, each unchecked rerun has a 0.8 chance of leaving state clean. Independence across reruns is also assumed.
assume clean chance per rerun = 0.8 (stated example parameter, independent reruns)
three reruns all clean: 0.8^3 = 0.8 x 0.8 x 0.8 = 0.512
about 51 of 100 such triples stay clean, and the rest need repairassume 1 million input tokens cost 3 units (stated example price)
one 20000-token log review costs 20000 / 1000000 x 3 = 0.06 units
ten such reviews cost 10 x 0.06 = 0.6 unitsNothing. The rerun finds all four keys already set and adds zero.
0.512. Three factors of 0.8 multiply to 0.512.