Durable Runs and Recovery SOP
1. Confirm before execution
- Record the workspace, configuration file, fingerprint, entry point, and permission mode.
- Classify each tool effect as file, process, network, or external and state whether it is reversible or replay-safe.
- Confirm whether the business system accepts an idempotency or transaction key. Never promise safe replay without one.
- Use a copy of a legacy Session for acceptance; do not experiment on the only source.
2. Run lifecycle
- Allocate
runId,operationId, andcorrelationId, then persistACCEPT. - Transition to
runningbefore execution. - Transition to
pausedwith a stablefinishReasonwhen approval or human judgment is required. - On cancellation, enter
abortingfirst and close asfailedwith an aborted reason after cleanup. - Enter
completedonly after verification. Exceptions, timeouts, and exhausted budgets enterfailed. - Create
RunResult.snapshotafter the terminal transition. All four entry points must serialize and validate that snapshot instead of deriving a second status model.
3. Tools and effects
- Allocate a
callIdand derive anidempotencyKeyfrom run, step, and call. - Create a checkpoint before a write. Irreversible commands still require an explicit non-reversible record.
- Persist
not_startedwhen approval or policy denies execution; persiststartedonly after approval and immediately before execution. - Persist
committedafter confirmed success orunknownwhen an already-started result is uncertain. - During recovery, reconsider
not_started; skipcommittedonly with its stable completed step; pause forstarted,unknown, or a committed effect in an incomplete step.
4. Session migration
- Validate the legacy header, session id, JSON lines, and supported entry types.
- Create
<id>.jsonl.v3.backupin the same directory and compare bytes. - Create an incomplete migration marker in the versioned repository.
- Convert entries and commit through a temporary file plus atomic rename.
- Reopen the target and validate its completion marker and context.
- Publish the stable public path last. No earlier failure may replace the legacy file.
- Run migration again and confirm that no message or authoritative session is duplicated.
5. Crash decisions
| Evidence | Action |
|---|---|
| Only the final JSONL line is incomplete after valid records | Remove the tail and atomically republish |
| Parsing or field validation fails, sequence is broken, or persisted order is shuffled | Fail closed and recover from backup or human evidence; only a syntactically truncated JSON tail after complete records may be repaired automatically |
| A lock exists and its writer is active | Wait; do not delete the lock |
| A lock exists and the writer is proven absent | Back up lock and data, remove the lock manually, then retry |
| Operation is completed or failed | Do not resume; create a new task |
| Effect outcome is unknown | Query the external system and remain paused if still uncertain |
6. Verification
text
npx vitest run packages/coremind-runtime/src/operation-state.test.ts packages/coremind-runtime/src/run-state.test.ts packages/coremind-runtime/src/session-conformance.test.ts packages/coremind-runtime/src/session.test.ts packages/coremind-runtime/src/checkpoint.test.ts packages/coremind-runtime/src/snapshot.test.ts packages/coremind-runtime/src/runtime.test.ts packages/coremind-worker/src/server.test.ts --maxWorkers=1
npm run check:modulesUse the same command on Linux. Real process crashes, competing writers, and filesystem rename behavior require separate Windows and Linux acceptance.