Make failure safe
External input can be missing, malformed, stale or hostile. Validate it at the boundary before it affects the system. Treat third-party metadata and agent output as claims to check, not as authority.
Access must be enforced where the action happens. In this illustrative service function, a denial returns before a write. Authentication, input validation and loading the current draft happen before calling it:
async function renameDraft(actor, draft, title, store) {
if (!canEditDraft(actor, draft)) {
return { status: 403, error: "draft-not-editable" };
}
await store.renameIfEditable({
id: draft.id,
expectedVersion: draft.version,
actorId: actor.id,
title,
});
return { status: 200 };
}The store operation must check current permission and version atomically with the write. Otherwise a draft could change after the first check. A conflict or storage failure must become an honest failure response at the service boundary, not a success message.
Protect the transition, not just the endpoint
Write down the invariants that matter for this operation: which actors may change which objects in which states, and which effects must happen together. Validate the complete input before applying a batch unless partial success is an explicit product behavior. Reject malformed types as well as bad values. Bound input before expensive parsing. Render untrusted content as data, never as executable markup or instructions.
Enforce current authority, lifecycle state and expected revision at the same boundary as the mutation. A transaction that returns an error normally may still commit earlier writes; understand the storage contract and roll back explicitly. Include associated audit records and idempotency records in the same atomic unit when they certify that unit’s success.
Scope retry keys to their actor and operation. Bind a key to its input, reject reuse for a different intent, and check current access before revealing a stored result. A version conflict requires fresh facts and another deliberate decision; blindly retrying with the newest version defeats the protection. Explain which decision facts changed when that helps the person reassess them. Once recovery succeeds, remove obsolete warnings and recovery controls so the next action is clear.
Plan for interruption
A retry should not accidentally send a second payment, create a second job or duplicate a notification. Use an idempotency key or another mechanism that fits the system. Check what happens when a request succeeds but its response never reaches the caller.
Keep ordinary actions reversible where practical. For consequential actions that cannot be undone, make the effect clear before commitment. Preserve people’s work when something fails. Record consequential changes with enough context to establish who acted, what changed and when, without logging secrets.
Distinguish refused, failed, partially completed and unknown outcomes. A timeout does not prove that nothing happened. Reconcile with the authoritative result before inviting a consequential retry. For a multi-step operation, show which parts completed and which can safely resume; do not call the whole batch done because its first request succeeded.
Keep errors useful without exposing private data or internal traces. Give the person a recovery action and operators a safe way to correlate the failure. Decide what must be logged, for how long and who may see it. Do not collect sensitive payloads just because debugging would be easier.
Keep delivery recoverable
A change may work locally and still fail during rollout. For changes to stored data or public contracts, consider old clients, mixed versions and existing records. Plan migrations, compatibility and recovery before removing the old path. Verify a rollback or forward repair against the actual failure mode; reverting code does not necessarily undo a data migration.
For a running service, make consequential failures observable and give someone ownership of responding. Check that timeouts, dependency outages and exhausted resources leave a diagnosable, bounded state. Scale the operational work to the risk: a local formatting change does not need a deployment playbook.
Give agents explicit authority
An agent is another client of the product. Prefer documented interfaces that humans can also inspect and use. Give connections only the access they need, and make that access visible and revocable.
Each product defines which actions an agent may perform and which require a person. Enforce those boundaries on the server. When people review an agent’s draft, distinguish its proposal from the verified effect and give them a way to correct or reject it.