Preview a process change before applying it
Require reviewed process identity and preserve state until execution is explicit.
A reviewed action is a complete identity
A process name is useful for searching, but it is not enough to authorize a stop. This simulation requires a host, PID and creation instant. Validate the entire request list before performing any action, including duplicates. Otherwise an invalid final item could be discovered only after earlier processes had already changed.
The default execute=False asks LabFleet for a preview. It returns would_stop for an eligible running instance without creating or changing a state file. Explicit execution changes only the virtual fleet-state.json file. Protected, denied, missing and replaced process instances retain distinct statuses.
Recheck at the action boundary
The adapter compares the reviewed creation time with the current process instance. Reusing a PID after a restart must produce identity_changed, not stop the new process. An already-stopped matching instance returns already_stopped, making a repeated request observable without repeating a mutation.
Normalize the requested time to a UTC ISO representation before dispatch. Validate every host and PID, sort the prepared identities, then isolate ordinary per-host failures into error statuses. Do not turn KeyboardInterrupt into a normal process result.
This model does not remove real operating-system races or grant permissions. It is an explicit teaching adapter with recorded access rules and a virtual state file. Normal Run preserves completed state in saved lab files; Check replaces it with original fixtures. The capstone will use previews only, so its assessment cannot silently apply maintenance.
from lab_fleet import LabFleet
fleet = LabFleet()
print(fleet.stop("node-a", 410, "2026-11-17T10:00:00Z", execute=False))
Put it into practice.
Define reviewed_stops(fleet, requests, execute=False). Require boolean execute. Validate all requests before any action: host normalizes to [a-z0-9][a-z0-9-]*, pid is a positive integer excluding bool, started_at is aware ISO time normalized to UTC ending Z. Reject missing/bad fields and duplicate normalized host/PID pairs with ValueError. Sort by host then PID. Call fleet.stop(host, pid, started_at, execute=execute); preserve its status. Ordinary exceptions yield status error:<class>; cancellation propagates. Return host, pid, started_at, status. Preview must never execute a change.
See an explained solution
from datetime import datetime, timezone
import re
def reviewed_stops(fleet, requests, execute=False):
if type(execute) is not bool:raise ValueError("execute must be a boolean")
prepared=[];seen=set()
for row in requests:
try:
host=row["host"].strip().casefold();pid=row["pid"]
stamp=datetime.fromisoformat(row["started_at"])
if not re.fullmatch(r"[a-z0-9][a-z0-9-]*",host) or type(pid) is not int or pid<=0 or stamp.tzinfo is None:raise ValueError("Invalid process identity")
start=stamp.astimezone(timezone.utc).isoformat().replace("+00:00","Z")
except (KeyError,TypeError,AttributeError,ValueError) as error:
raise ValueError("Invalid process request") from error
if (host,pid) in seen:raise ValueError("Duplicate process request")
seen.add((host,pid));prepared.append((host,pid,start))
rows=[]
for host,pid,start in sorted(prepared):
try:status=fleet.stop(host,pid,start,execute=execute)
except Exception as error:status="error:"+type(error).__name__
rows.append({"host":host,"pid":pid,"started_at":start,"status":status})
return rows
if __name__ == "__main__":
from lab_fleet import LabFleet
print(reviewed_stops(LabFleet(), [{"host":"node-a","pid":410,"started_at":"2026-11-17T10:00:00Z"}]))
The tool separates review from execution, validates the whole batch, and retains process identity at the mutation boundary. State is virtual and saved only after explicit successful changes.