feat(platform): graceful shutdown and restart recovery (#259) #301

Merged
sharang merged 1 commits from feat/graceful-shutdown into main 2026-09-21 15:35:54 +00:00
Owner

MVP-0.6 of #253. Closes #259, #65, #68.

Changes

Graceful shutdown. SIGTERM or Ctrl+C now stops the agent API, the webhook server and the MCP HTTP server from accepting new connections, lets in-flight requests finish, and drops anything still open after 8 s. That stays under Docker's default 10 s stop timeout; no Orca service sets a longer one. The shared helper is compliance_core::shutdown (signal, fan-out handle, bounded serve). The agent's entrypoint already execs, so the signal reaches the process.

Restart recovery. At startup, before the scheduler or servers start, the agent closes every run the previous process left open, in every tenant database:

Record Open states Closed as
scan runs running interrupted (new ScanRunStatus)
graph / embedding builds running failed
DAST runs running failed
pentest sessions running, paused failed

Every closed record gets error_message: "interrupted by agent restart" and completed_at. Paused pentest sessions are included because pause/resume lives in memory and cannot be resumed after a restart.

Werkbank sweep. JobQueue::sweep_expired had no caller outside tests. It now runs every 60 s across tenants, with at most 3 lease attempts per job before the job is marked expired.

Webhook port (#68). Set by WEBHOOK_PORT, default 3002.

Decision worth a second look

Only code scans get the new interrupted status; everything else becomes failed with the message. I asked Jev (neutral description, no code): it picked this option at 0.62 against 0.37 for "new status everywhere", with low confidence (0.42), so it was a close call. The reasoning: customers see code scans, and a restart isn't their code failing. DAST and pentest are off in production. Graph and embedding builds are steps inside a scan.

Assumption

There is one agent per environment, which is true for every Orca deployment today. With two replicas, one agent's startup would close runs the other is still working on.

Verification

  • cargo fmt --check, cargo audit, and clippy -D warnings for every CI target (agent, dashboard server + web, mcp, werkbank-exec, control-map) all pass.
  • Lib tests: core 74, agent 359, werkbank-exec 9, control-map 28. Dashboard server 21, MCP 34.
  • New unit tests in compliance-core/src/shutdown.rs cover three cases: an in-flight request finishes after shutdown fires; a request that never ends is cut off at the grace period; dropping the trigger counts as shutdown.
  • New Mongo integration test tests/lifecycle.rs:
    • Recovery closes the open records in two tenants and leaves finished ones alone; a second run closes nothing.
    • The sweep requeues expired leases in both tenants and leaves unexpired ones.
  • Existing Mongo suites (werkbank_queue, werkbank_api, tenant_isolation, demo_seed) pass when run one at a time. Run together, my local Mongo container crashes with "Too many open files" (fd limit 1024), which is environmental.
  • The real binary was run with a seeded orphaned scan run:
    • The run came back interrupted with the message.
    • Both ports came from AGENT_PORT / WEBHOOK_PORT.
    • SIGTERM gave a clean exit 0 within 60 ms, with "Webhook server stopped", "REST API stopped" and "Agent stopped" logged.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p

MVP-0.6 of #253. Closes #259, #65, #68. ## Changes **Graceful shutdown.** SIGTERM or Ctrl+C now stops the agent API, the webhook server and the MCP HTTP server from accepting new connections, lets in-flight requests finish, and drops anything still open after **8 s**. That stays under Docker's default 10 s stop timeout; no Orca service sets a longer one. The shared helper is `compliance_core::shutdown` (signal, fan-out handle, bounded `serve`). The agent's entrypoint already `exec`s, so the signal reaches the process. **Restart recovery.** At startup, before the scheduler or servers start, the agent closes every run the previous process left open, in every tenant database: | Record | Open states | Closed as | |---|---|---| | scan runs | running | **`interrupted`** (new `ScanRunStatus`) | | graph / embedding builds | running | failed | | DAST runs | running | failed | | pentest sessions | running, **paused** | failed | Every closed record gets `error_message: "interrupted by agent restart"` and `completed_at`. Paused pentest sessions are included because pause/resume lives in memory and cannot be resumed after a restart. **Werkbank sweep.** `JobQueue::sweep_expired` had no caller outside tests. It now runs every 60 s across tenants, with at most 3 lease attempts per job before the job is marked `expired`. **Webhook port (#68).** Set by `WEBHOOK_PORT`, default 3002. ## Decision worth a second look Only code scans get the new `interrupted` status; everything else becomes `failed` with the message. I asked Jev (neutral description, no code): it picked this option at 0.62 against 0.37 for "new status everywhere", with **low confidence (0.42)**, so it was a close call. The reasoning: customers see code scans, and a restart isn't their code failing. DAST and pentest are off in production. Graph and embedding builds are steps inside a scan. ## Assumption There is one agent per environment, which is true for every Orca deployment today. With two replicas, one agent's startup would close runs the other is still working on. ## Verification - `cargo fmt --check`, `cargo audit`, and clippy `-D warnings` for every CI target (agent, dashboard server + web, mcp, werkbank-exec, control-map) all pass. - Lib tests: core 74, agent 359, werkbank-exec 9, control-map 28. Dashboard server 21, MCP 34. - New unit tests in `compliance-core/src/shutdown.rs` cover three cases: an in-flight request finishes after shutdown fires; a request that never ends is cut off at the grace period; dropping the trigger counts as shutdown. - New Mongo integration test `tests/lifecycle.rs`: - Recovery closes the open records in two tenants and leaves finished ones alone; a second run closes nothing. - The sweep requeues expired leases in both tenants and leaves unexpired ones. - Existing Mongo suites (`werkbank_queue`, `werkbank_api`, `tenant_isolation`, `demo_seed`) pass when run one at a time. Run together, my local Mongo container crashes with "Too many open files" (fd limit 1024), which is environmental. - The real binary was run with a seeded orphaned scan run: - The run came back `interrupted` with the message. - Both ports came from `AGENT_PORT` / `WEBHOOK_PORT`. - SIGTERM gave a clean exit 0 within 60 ms, with "Webhook server stopped", "REST API stopped" and "Agent stopped" logged. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang added 1 commit 2026-09-21 15:26:42 +00:00
feat(platform): graceful shutdown and restart recovery (#259)
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 5m53s
e4f7fb540b
A redeploy used to cut every request mid-flight and leave the scans it
killed marked `running` forever.

- The agent API, webhook and MCP servers stop accepting connections on
  SIGTERM/Ctrl+C, let in-flight requests finish, and drop what is still
  open after 8 s (under Docker's 10 s stop timeout).
  `compliance_core::shutdown` holds the shared signal/drain helper.
- At startup, before anything can start a run, runs the previous process
  left open are closed in every tenant. Scan runs get a new `interrupted`
  status. DAST runs, pentest sessions (running or paused: pause state lives
  in memory), and graph/embedding builds become `failed` with the message
  "interrupted by agent restart".
- `JobQueue::sweep_expired` now runs every 60 s across tenants (at most 3
  lease attempts per job).
- The webhook port comes from `WEBHOOK_PORT` (default 3002).

Assumes one agent per environment, as in every Orca deployment today.

Closes #65, closes #68.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang merged commit 85959907b7 into main 2026-09-21 15:35:54 +00:00
sharang deleted branch feat/graceful-shutdown 2026-09-21 15:35:55 +00:00
Sign in to join this conversation.