Repository navigation
Conversation
…after gateway restart Add a disk-backed active turn tracker that persists in-flight agent session markers. On unclean restart or crash, stale markers are detected and the affected user receives a recovery notification. A stuck turn watchdog runs every 2 minutes and aborts turns that exceed 20 minutes. New modules: - src/infra/active-turns.ts: marker file I/O (write/clear/load) - src/gateway/server-active-turn-recovery.ts: startup recovery - src/infra/stuck-turn-watchdog.ts: periodic stuck turn detection Modified: - src/agents/pi-embedded-runner/runs.ts: hook writeActiveTurn/clearActiveTurn - src/gateway/server-startup.ts: wire recovery + watchdog after startup
Greptile SummaryThis PR adds a 2-layer disk-backed defense against silent failures from gateway restarts: an active turn marker system ( Key issues found:
Confidence Score: 3/5
Last reviewed commit: 34c532f |
There was a problem hiding this comment.
Pull request overview
This PR adds a 2-layer defense mechanism to recover from silent failures when the gateway crashes or restarts with in-flight embedded PI runs. When a turn is active, a marker file is written to disk; on startup, stale markers are detected and recovery notifications are sent to users. A periodic watchdog also monitors for stuck turns and stale markers at runtime.
Changes:
- New disk-backed active turn tracker (
active-turns.ts) with fire-and-forget marker file I/O using atomic tmp+rename writes, plus a startup recovery module (server-active-turn-recovery.ts) that delivers notifications for interrupted turns. - New stuck-turn watchdog (
stuck-turn-watchdog.ts) that periodically cross-references disk markers against in-memory runs, cleaning orphans and aborting turns stuck >20 minutes. - Integration into the gateway lifecycle:
runs.tshooks marker write/clear into the embedded run set/clear lifecycle, andserver-startup.tswires recovery + watchdog on startup.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
src/infra/active-turns.ts |
New: marker file I/O — write, clear, load, remove turn markers with path sanitization and probe filtering |
src/infra/stuck-turn-watchdog.ts |
New: periodic watchdog that detects stale/stuck turns via disk marker + in-memory run cross-referencing |
src/gateway/server-active-turn-recovery.ts |
New: startup recovery that sends notifications to users for interrupted turns using the outbound delivery chain |
src/agents/pi-embedded-runner/runs.ts |
Modified: hooks writeActiveTurn/clearActiveTurn into set/clear embedded run lifecycle |
src/gateway/server-startup.ts |
Modified: wires recovery + watchdog on startup with dynamic imports and a 1500ms delay |
src/infra/active-turns.test.ts |
New: 9 unit tests for marker I/O |
src/infra/active-turns-crash-scenarios.test.ts |
New: 8 crash scenario tests |
src/infra/stuck-turn-watchdog.test.ts |
New: 5 watchdog tests |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 34c532fa6b
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bc80d844a4
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const pending = pendingWrites.get(sessionId); | ||
| const doUnlink = () => fs.promises.unlink(filePath).catch(() => {}); | ||
| if (pending) { | ||
| void pending.then(doUnlink); |
There was a problem hiding this comment.
Prevent clear from deleting a newer run marker
clearActiveTurn captures the current pending write and unlinks afterward without re-checking whether a newer writeActiveTurn has started for the same sessionId; if a turn ends and a new turn starts quickly, the old clear path can delete the new marker file, so the currently active run is no longer crash-recoverable or watchdog-visible. This race is separate from the write-before-clear case because it occurs when a newer write starts after clearActiveTurn has already scheduled its unlink.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed — clearActiveTurn now captures writeGeneration at call time and checks it before unlinking. If a newer writeActiveTurn bumped the generation, the unlink is skipped. Test added (Scenario 8).
| for (const marker of markers) { | ||
| // Always clear the marker first (consume-then-process pattern) to prevent | ||
| // infinite retry loops if the gateway crashes again during recovery. | ||
| await removeActiveTurnMarker(marker.sessionId); | ||
|
|
There was a problem hiding this comment.
Skip startup recovery for runs that are already active
This loop consumes every marker unconditionally, but startup schedules recovery with a 1.5s delay after channel startup (src/gateway/server-startup.ts), so a user message received during that window can create a fresh marker that gets misclassified as "interrupted" here. The code then removes that live marker and sends a false recovery prompt, and the active turn loses disk tracking until another write happens.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed — recoverInterruptedTurns now checks isEmbeddedPiRunActive() before processing each marker. Live runs are skipped.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c81f81e11f
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| }) | ||
| .finally(() => { | ||
| void import("../infra/stuck-turn-watchdog.js").then(({ startStuckTurnWatchdog }) => { | ||
| startStuckTurnWatchdog({ deps: params.deps }); |
There was a problem hiding this comment.
Stop watchdog interval when gateway lifecycle closes
startGatewaySidecars calls startStuckTurnWatchdog but discards the returned stop handle, so shutdown has no way to clear that interval. In the in-process restart path (src/cli/gateway-cli/run-loop.ts, where restart falls back without exiting), sidecars are started again and another watchdog timer is added, causing duplicate marker scans/abort attempts and accumulating background work after each restart.
Useful? React with 👍 / 👎.
|
|
||
| // Always clear the marker first (consume-then-process pattern) to prevent | ||
| // infinite retry loops if the gateway crashes again during recovery. | ||
| await removeActiveTurnMarker(marker.sessionId); |
There was a problem hiding this comment.
Revalidate activity before deleting startup recovery marker
The loop checks isEmbeddedPiRunActive and then immediately awaits removeActiveTurnMarker, but a new run for the same sessionId can start between those two steps and have its fresh marker unlinked. That race leads to a false "interrupted" recovery notification and removes crash-recovery/watchdog visibility for the now-active run, especially since recovery is intentionally delayed after channel startup.
Useful? React with 👍 / 👎.
|
Closing this PR because the author has more than 10 active PRs in this repo. Please reduce the active PR queue and reopen or resubmit once it is back under the limit. You can close your own PRs to get back under the limit. |
Summary
When the gateway restarts (crash, SIGUSR1, or compaction failure), in-flight embedded PI runs are lost because
ACTIVE_EMBEDDED_RUNSis an in-memory Map. This leaves users in a silent state where the bot never responds until the user sends a new message.This PR adds a 2-layer defense:
Disk-backed active turn tracker (
src/infra/active-turns.ts) — writes marker files to~/.openclaw/active-turns/{sessionId}.jsonwhen a turn starts, removes them when it completes. On startup,server-active-turn-recovery.tsscans for stale markers and sends a recovery message to the user via the same delivery resolution chain used by the restart sentinel.Stuck turn watchdog (
src/infra/stuck-turn-watchdog.ts) — periodic check (every 2 minutes) that cross-references disk markers against in-memory runs. Cleans orphaned markers (disk exists but no in-memory run) and aborts turns stuck >20 minutes.Design decisions
probe-prefix are skipped (health checks)server-restart-sentinel.tsFiles changed
src/infra/active-turns.tssrc/infra/stuck-turn-watchdog.tssrc/gateway/server-active-turn-recovery.tssrc/agents/pi-embedded-runner/runs.tswriteActiveTurn/clearActiveTurninto set/clear lifecyclesrc/gateway/server-startup.tssrc/infra/active-turns.test.tssrc/infra/active-turns-crash-scenarios.test.tssrc/infra/stuck-turn-watchdog.test.tsTest plan