# Durable workflow crash recovery with SQLite

Recorded engine execution from Orch8 0.7.1, revision [c487ae114421423929fc82b7c52501eef524c03d](https://github.com/orch8-io/engine/blob/c487ae114421423929fc82b7c52501eef524c03d/orch8-cli/src/commands/demo.rs).

[Canonical HTML evidence](https://orch8.io/demo/crash-recovery)

The recording demonstrates SQLite stale-instance recovery. It verifies that one orphaned instance moves from running to scheduled and becomes eligible for a new claim. The fixture has no workers or external side effects.

## Recovery boundaries

- **Last persisted state.** Running. The fixture seeds an update time ten minutes before recovery.
- **Interruption.** The storage connection closes. No interruption timestamp was recorded.
- **Reclaim.** Recovery changes one instance to scheduled. Reclaim duration was not recorded.
- **First progress after reclaim.** The run is eligible for a worker claim. This fixture ends before a worker claims it.

## Recorded events

1. **Create a real SQLite database.** A fresh file-backed database in a temp directory, with a sequence and one workflow instance for tenant crash-demo.
2. **Leave the instance mid-flight.** The instance is written in the running state with its last update ten minutes ago, as if a worker had claimed it and never reported back.
3. **Simulate the crash.** The only storage connection pool is dropped without any cleanup, which simulates the engine process disappearing mid-run. No real process is killed.
4. **Restart and recover.** A new storage handle opens the same file, as a restarted engine would, and runs the engine's stale-instance recovery with a 60-second threshold.
5. **Check the invariant.** The command exits with an error unless exactly one instance was reclaimed and it is back in scheduled, ready to be claimed again.

## Reproduce the recording

```bash
git clone https://github.com/orch8-io/engine.git
cd engine
git checkout c487ae114421423929fc82b7c52501eef524c03d
cargo run -p orch8-cli --bin orch8 -- demo crash-recovery
```

## Read the recorded restart counts

```bash
curl -fsSL https://orch8.io/demo/crash-recovery/restart-trace.json |
  jq -r '.handlerCalls[] | [.phase, .block, .count] | @tsv'
```

```text
before restart	first	1
before restart	second	0
after restart	first	0
after restart	second	1
```

## Restart rehearsal timing

A separate run with the checksum-verified 0.7.1 release recorded these offsets from the first committed output. Readiness is the first successful health probe. The time to the second output includes the intentional eight-second delay. Individual claim and handler durations were unavailable.

- First output committed +0 ms
- Second step schedule observed +5 ms
- Process kill requested +5 ms
- Process exit observed +8 ms
- Restart readiness observed +318 ms
- Second output committed +8046 ms

[Recorded restart evidence (JSON)](https://orch8.io/demo/crash-recovery/restart-trace.json)

## External worker interruption

In this recorded Orch8 0.7.1 run, a separate worker process claimed the second step, saved checkpoint 1, and was killed before completion. A replacement reclaimed the same task and finished from the saved checkpoint. The first output kept its original ID.

- First committed output observed +0 ms
- Original worker saved its checkpoint +61 ms
- Original worker process exited +63 ms
- Replacement acquired the task +3879 ms
- Completed workflow observed +4008 ms

| Attempt (observed order) | Outcome | Failure | Observed duration |
| --- | --- | --- | --- |
| 1. Original claim | Interrupted | Process terminated before completion | Unavailable |
| 2. Replacement claim | Completed | None observed | 129 ms to observed workflow completion |

The gap from observed exit to replacement claim was 3,816 ms. The execution time of the replacement handler alone was unavailable.

The local fixture used a 3-second stale-heartbeat threshold and a one-second reaper tick. It performed no external provider calls. The observed timings include polling.

[Recorded worker interruption (JSON)](https://orch8.io/worker-interruption-recorded.json)

[Download the verification script](https://orch8.io/verify-worker-interruption.mjs)

```bash
node verify-worker-interruption.mjs /path/to/orch8-server
```

## Terminal transcript

Placeholders replace the generated instance IDs and local database path.

```text
orch8 demo crash-recovery
Crash recovery completed
  instance: <first-run-id>
  state:    running -> scheduled
  reclaimed: 1

orch8 demo crash-recovery --output json
{
  "database": "<temporary-directory>/crash-recovery.db",
  "instance_id": "<second-run-id>",
  "before_restart": "running",
  "recovered": 1,
  "after_restart": "scheduled"
}
```
