Skip to content

Commit fef5c61

Browse files
authored
docs(gateway): document restart and crash recovery behavior (#103985)
* docs(gateway): document restart and crash recovery behavior * chore(docs): allowlist intentional command:nwe hook example in spellcheck
1 parent 6d39ec5 commit fef5c61

4 files changed

Lines changed: 134 additions & 0 deletions

File tree

‎docs/docs.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,7 @@
16221622
"pages": [
16231623
"gateway/gateway-lock",
16241624
"gateway/background-process",
1625+
"gateway/restart-recovery",
16251626
"gateway/multiple-gateways"
16261627
]
16271628
},

‎docs/docs_map.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3672,6 +3672,20 @@ Do not edit it by hand; run `pnpm docs:map:gen`.
36723672
- H4: Troubleshooting
36733673
- H2: Related
36743674

3675+
## gateway/restart-recovery.md
3676+
3677+
- Route: /gateway/restart-recovery
3678+
- Headings:
3679+
- H2: What survives a restart
3680+
- H2: Graceful restarts drain first
3681+
- H2: How interrupted work is detected
3682+
- H2: Automatic resume
3683+
- H3: Subagents
3684+
- H3: Background tasks
3685+
- H3: Agent-requested restarts
3686+
- H2: Safety valves and observability
3687+
- H2: What is not resumed
3688+
36753689
## gateway/sandbox-vs-tool-policy-vs-elevated.md
36763690

36773691
- Route: /gateway/sandbox-vs-tool-policy-vs-elevated

‎docs/gateway/restart-recovery.md‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
summary: "What survives a gateway restart or crash: interrupted agent turns resume automatically, subagents and background tasks recover, queued deliveries drain"
3+
read_when:
4+
- You want to know whether restarting the gateway loses in-progress agent work
5+
- An agent run was interrupted by a restart, crash, or config reload
6+
- You are debugging automatic session recovery after the gateway comes back up
7+
title: "Restart recovery"
8+
---
9+
10+
Restarting the gateway does not lose agent state. Conversations, transcripts,
11+
scheduled jobs, background task records, and queued outbound messages all live
12+
on disk, and work that was interrupted mid-turn is detected and resumed
13+
automatically after the gateway comes back up. No manual intervention is
14+
required, and there is nothing to configure: recovery is always on.
15+
16+
This page describes what survives a restart, how interrupted work is detected,
17+
and what the automatic resume looks like.
18+
19+
## What survives a restart
20+
21+
| State | Storage | Behavior across restart |
22+
| ----------------------------- | --------------------------------------------------- | ----------------------------------------------------------------------- |
23+
| Conversation history | JSONL transcripts + per-agent session store on disk | Untouched; sessions continue from the stored transcript |
24+
| Interrupted main-session turn | Recovery markers in the session store | Automatically resumed a few seconds after startup |
25+
| Subagent runs | SQLite (shared state database) | Registry restored on boot; interrupted runs resumed |
26+
| Background tasks | SQLite (shared state database) | Reconciled on boot; orphaned runs recovered or marked lost |
27+
| Queued outbound deliveries | SQLite delivery queue | Drained after restart; undelivered replies are retried |
28+
| Scheduled (cron) jobs | SQLite cron store | Schedules persist; the scheduler re-arms on boot |
29+
| Restart continuation | SQLite restart sentinel | One-shot follow-up dispatched to the session that asked for the restart |
30+
31+
## Graceful restarts drain first
32+
33+
A requested restart (`openclaw gateway restart`, a config change that requires
34+
a restart, or a gateway update) does not kill in-flight work immediately. The
35+
gateway stops accepting new work, then waits for active agent turns and
36+
background tasks to finish, up to a drain budget (5 minutes by default). Most
37+
restarts therefore interrupt nothing at all.
38+
39+
Only work that cannot finish inside the drain budget (or any run interrupted
40+
by a forced restart or a crash) is aborted — and before that happens, each
41+
affected session is marked for recovery.
42+
43+
## How interrupted work is detected
44+
45+
Two complementary mechanisms mark sessions whose turn did not finish:
46+
47+
- **At shutdown:** during the restart drain, every session with an active run
48+
is stamped with a recovery marker in the session store before the run is
49+
aborted.
50+
- **At startup:** the gateway scans session stores for sessions that still
51+
claim to be running but have no live owner in the new process. This catches
52+
hard crashes and kills where no shutdown code ran. Stale transcript lock
53+
files are cleaned up at the same time.
54+
55+
## Automatic resume
56+
57+
A few seconds after startup, the gateway re-dispatches each marked session
58+
with a synthetic system message telling the agent its previous turn was
59+
interrupted by a restart and to continue from the existing transcript. If a
60+
final reply had already been produced but not delivered, its text is included
61+
so the agent can deliver it instead of redoing the work. Recovery retries up
62+
to 3 times with exponential backoff.
63+
64+
Before resuming, the gateway checks that the transcript tail is safe to
65+
continue from. If it is not (for example, the turn ended on a stale pending
66+
approval), the session is not blindly re-run; the agent instead posts a short
67+
notice asking the user to resend the last request.
68+
69+
### Subagents
70+
71+
Subagent runs are persisted in the shared SQLite state database, so the
72+
subagent registry survives the process. On boot the registry is restored and
73+
interrupted subagent sessions are resumed with their original task context.
74+
Two safety valves apply:
75+
76+
- Runs interrupted more than 2 hours ago are finalized instead of resumed, so
77+
a gateway that was down overnight does not resurrect stale work.
78+
- A session that repeatedly fails to recover is tombstoned as wedged so
79+
recovery cannot loop forever.
80+
81+
### Background tasks
82+
83+
The [background task registry](/automation/tasks) is SQLite-backed and
84+
reconciled on boot and on a periodic interval: durable outcomes recorded by
85+
finished runs are recovered, and runs whose owning process disappeared are
86+
marked lost after a grace period instead of hanging forever.
87+
88+
### Agent-requested restarts
89+
90+
When the agent itself triggers a restart (applying a config change, updating
91+
the gateway, or an explicit restart request), a restart sentinel is written to
92+
SQLite before the process exits. After boot the gateway posts the outcome back
93+
to the originating chat and dispatches a one-shot continuation turn so the
94+
agent picks up exactly where it left off, on the same channel and thread.
95+
96+
## Safety valves and observability
97+
98+
- **Crash-loop breaker:** 3 unclean boots within 5 minutes trip a breaker that
99+
suppresses auto-start side services on the next boot, so a crashing gateway
100+
does not amplify itself. It recovers once the unclean-boot window drains.
101+
- **Metrics:** recovery activity is exported via
102+
[Prometheus](/gateway/prometheus) as `openclaw_session_recovery_total` and
103+
`openclaw_session_recovery_age_seconds`.
104+
- **Logs:** recovery decisions are logged under the
105+
`main-session-restart-recovery` and `subagent-interrupted-resume`
106+
subsystems.
107+
108+
## What is not resumed
109+
110+
- Sessions excluded from main-session recovery because another owner already
111+
handles them: subagent sessions (subagent recovery), cron sessions (the
112+
scheduler re-runs on schedule), and ACP-managed sessions (the connected IDE
113+
or client owns the resume).
114+
- Sessions whose transcript tail cannot be safely continued; these get the
115+
resend notice described above instead of a silent re-run.
116+
- Work that was never admitted: messages arriving during the drain window are
117+
rejected with an explicit restart error rather than silently queued into a
118+
dying process.

‎scripts/codespell-ignore.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ Brining
1414
wit
1515
allowIn
1616
planText
17+
nwe

0 commit comments

Comments
 (0)