Skip to content

feat(gateway): explicit shutdown disposition to retain running sandboxes (Connect S5) - #44

Merged
raghusubramanyam merged 1 commit into
lightforge-mainfrom
fix/gateway-shutdown-disposition
Nov 26, 2026
Merged

raghusubramanyam merged 1 commit into
lightforge-mainfrom
fix/gateway-shutdown-disposition

Conversation

@raghusubramanyam

@raghusubramanyam raghusubramanyam commented Nov 26, 2026 •

Copy link
Copy Markdown

Summary

Connect S5 (shutdown half); companion to OpenShell #42; separate from ai-openshell#471 (lightforgedev/aegis-connect-phoenix#471, supervisor-network tunnel staleness).

Stacked on #42 (fix/gateway-restart-readopt, head 6efaf294). Review and merge #42 first.

A graceful gateway shutdown always ran the sandbox stop sweep (cleanup_on_shutdown calls stop_persisted_sandboxes_on_shutdown). The sweep's driver stop sends TerminateBoundary, so a later gateway can never re-adopt the sandbox: attach fails with "sandbox session is terminal". #42 proved re-adoption only after SIGKILL. Connect's runtime restart is graceful, so it always lost its sandboxes (WP-6 run H5).

Contract (Hugo, S5 option 1)

Rule Implementation
Separate, explicit disposition, default = today's sweep New ShutdownDisposition (crates/openshell-server/src/shutdown_disposition.rs): stop-running-sandboxes (default) or retain-running-sandboxes
Do not reuse or flip gateway_manages_lifecycle Untouched. It still gates the sweep and startup reconciliation (compute/mod.rs, start_persisted_sandboxes)
No generic skip_cleanup The only mode skips one named step, the stop sweep
Retain skips only the stop sweep cleanup_on_shutdown(disposition): retain does not call stop_persisted_sandboxes_on_shutdown. Listener shutdown, gateway_shutting_down, the managed driver process shutdown and the error merge are unchanged
Bounded CK receipt One CK_gateway_shutdown_disposition line per graceful shutdown: mode, source (default/static/file), retained (running-intent count), and sweep_ok for the sweep
Unknown or missing defaults to sweep Unset, any non-exact token, or a missing/unreadable/oversized (>64 B)/non-UTF-8 file selects the sweep, plus a CK_gateway_shutdown_disposition_fallback warning with reason

Sources:

  • --shutdown-disposition / OPENSHELL_GATEWAY_SHUTDOWN_DISPOSITION: a static value.
  • --shutdown-disposition-file / OPENSHELL_GATEWAY_SHUTDOWN_DISPOSITION_FILE: read when the shutdown signal arrives, so a host can choose for each shutdown. Connect needs this because one Gateway process can end either way: runtime restart (retain) or explicit Stop (sweep). When a file is configured it is the only source; a missing file sweeps and never falls back to the static value.

No TOML key was added: Connect pins gateway.toml with a content receipt, so it passes the file through the environment.

Tests

Unit (cargo test -p openshell-server: 1766 lib tests pass, 8 ignored, 0 failed; all other test targets pass). New tests (9):

  • shutdown_disposition::tests (5): missing defaults to sweep; exact parsing; unknown static values sweep ("", retain, upper-case, skip-cleanup, a trailing suffix); the file is read at resolve time (missing, then retain, then stop); unknown, non-UTF-8, oversized or directory files sweep.
  • compute::tests (3): cleanup_on_shutdown_default_disposition_runs_stop_sweep (default sweep unchanged); cleanup_on_shutdown_unknown_disposition_runs_stop_sweep; cleanup_on_shutdown_retain_skips_only_the_stop_sweep (0 driver stops, persisted Ready/Provisioning intent kept, the managed driver child still terminated, retained count 2).
  • cli::tests::command_reads_shutdown_disposition_from_flags_and_env (1).
  • The existing sweep tests (shutdown_stops_running_intent_without_changing_persisted_phase and the others) pass unchanged.

Docker E2E (e2e/rust/tests/docker_gateway_restart_readopt.rs, local Docker Desktop arm64, e2e/with-docker-gateway.sh): 2 passed, 0 failed, E2E_EXIT=0.

  • New docker_gateway_graceful_retain_restart_readopts_live_sandbox:
    1. Relaunch the gateway with --shutdown-disposition-file set to retain.
    2. Create a sandbox; it reaches Ready and baseline exec works.
    3. Graceful SIGTERM. The new ManagedGateway::terminate never escalates to SIGKILL.
    4. Receipt: mode="retain-running-sandboxes" source="file" retained=1.
    5. The container is still running, with the same StartedAt/RestartCount.
    6. The next gateway reaches healthy, the supervisor sidecar is replaced, the sandbox is Ready, and exec reads the pre-restart state file. Same container, sandbox id and volumes. CK_supervisor_handover_accepted previous_supervisor_generation=Some(1) supervisor_generation=Some(2).
    7. Remove the disposition file and SIGTERM again: receipt mode="stop-running-sandboxes" source="file" retained=0 sweep_ok=true, and the container stops. This is the E2E check that a missing disposition sweeps.
    8. Clean up through the wrapper gateway; no volumes leak.
  • The existing fix(docker): re-adopt live sandboxes across gateway restarts via supervisor generation handover #42 SIGKILL test (3 restarts, generations 2→3→4) still passes. Both tests now take a shared lock because they restart the same wrapper-owned gateway.
  • Images: local/readopt/{sandbox,supervisor}:allowlist, built from fix(docker): re-adopt live sandboxes across gateway restarts via supervisor generation handover #42 head 6efaf294. This PR does not change sandbox or supervisor code, so those images match its sandbox/supervisor sources. The gateway and CLI were built from this branch.

Gates: cargo fmt --all -- --check and e2e cargo fmt --check are clean. cargo clippy -p openshell-server --all-targets -D warnings is clean, and so is clippy on the e2e lib plus docker_gateway_restart_readopt. Other e2e test files have clippy lints from before this PR. git diff --check is clean.

Not in scope / unverified

  • The VM and Kubernetes drivers were not exercised in retain mode. For a driver without gateway_manages_lifecycle, the sweep was already a no-op.
  • No GitHub CI. All builds and runs were local.
  • Connect consumer: lightforgedev/aegis-connect-phoenix#472.

🤖 Generated with Claude Code

…s on request

A graceful gateway shutdown always ran the sandbox stop sweep, so an embedded
host that restarts its gateway (Connect's runtime restart) could never
re-adopt a live sandbox: the sweep's stop terminates the boundary and a later
attach is rejected as terminal. Re-adoption only worked after SIGKILL.

Add a separate shutdown disposition, independent of the driver's
gateway_manages_lifecycle capability (which also gates startup
reconciliation and must stay on for re-adoption):

- stop-running-sandboxes (default): today's stop sweep, unchanged.
- retain-running-sandboxes: skip only the stop sweep. Listener shutdown, the
  managed driver process shutdown and all other cleanup still run.

Sources: --shutdown-disposition / OPENSHELL_GATEWAY_SHUTDOWN_DISPOSITION, or
--shutdown-disposition-file / OPENSHELL_GATEWAY_SHUTDOWN_DISPOSITION_FILE,
read when the shutdown signal arrives so a host can choose per shutdown; a
configured file is the only source. Anything but an exact known token (unset,
unknown, missing/unreadable/oversized file) selects the stop sweep, with a
CK_gateway_shutdown_disposition_fallback warning. Every graceful shutdown
emits one bounded CK_gateway_shutdown_disposition receipt with mode, source
and the retained running-intent sandbox count.

Tests: unit coverage for the default sweep, unknown values, retain skipping
only the sweep (driver process still stopped, intent kept), file resolution
at shutdown time and CLI/env parsing. The Docker E2E gains a graceful-SIGTERM
retain variant: the container, its StartedAt and running intent survive, the
next gateway reaches Ready and exec through a generation-2 handover, and
removing the file makes the next graceful stop sweep the sandbox.

Connect S5 (shutdown half); stacked on #42; separate from ai-openshell#471.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@raghusubramanyam raghusubramanyam left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[vega]

Conformance: CONDITIONAL PASS (source-clean at 64fc5dcf; combined D1b contract gated on Connect ai-openshell#472 fixes + live WP-6 H6 runtime receipt)
Head SHA: 64fc5dcf9712642ad4d7ba71135ca4700c930ba6 (stacked on #42 @6efaf294) | Objective / Bar: D1b shutdown-disposition contract (OpenShell half, reviewed jointly with lightforgedev/aegis-connect-phoenix#472 @d773a67c); live WP-6 H6 run remains the runtime gate.

Cross-Repo D1b Contract Check (OpenShell#44 + aegis-connect-phoenix#472)

  • Env var & token handshake: OPENSHELL_GATEWAY_SHUTDOWN_DISPOSITION_FILE (cli.rs:166) matches Connect process.ex:361; exact tokens retain-running-sandboxes / stop-running-sandboxes (shutdown_disposition.rs:47-48) match Connect process.ex:31-32.
  • Signal-time resolution & fail-closed fallback: lib.rs:1049 resolves after shutdown_signal().await and listener_task.await. When a disposition file is configured it is the sole source — missing, unreadable, directory, >64 B, non-UTF-8, or non-exact tokens all resolve to StopRunningSandboxes with CK_gateway_shutdown_disposition_fallback (shutdown_disposition.rs:129-181, lib.rs:1050-1058).
  • Scope isolation: gateway_manages_lifecycle is untouched (compute/mod.rs:2807, 2942); no generic skip_cleanup flag exists; retain skips only stop_persisted_sandboxes_on_shutdown() while still shutting down the listener, setting gateway_shutting_down, terminating the managed driver_process, and propagating cleanup errors (compute/mod.rs:2716-2780).
  • Exit-status propagation: Sweep errors return Err from cleanup_on_shutdown -> run_server (lib.rs:1063) -> cli::run (cli.rs:700), exiting non-zero as Connect's explicit-stop wrapper requires.
  • Foreign-sandbox boundary: stop_persisted_sandboxes_on_shutdown() queries only self.store (list_persisted_sandbox_ids), never Docker directly, so sandboxes owned by other Gateways/runs on the same Docker engine are untouched.
  • Companion PR status: lightforgedev/aegis-connect-phoenix#472 (d773a67c) has 4 small REQUEST_CHANGES items in its new :stopping / RunOwnedCleanup / terminate/2 paths (separate comment posted on ai-openshell#472).

Findings

  • Required now: None in OpenShell#44 source at 64fc5dcf. Remaining gates before merge/integrated-D1 closure: (1) resolve Connect ai-openshell#472 review items so the one D1b contract is clean on both sides, and (2) exact-head WP-6 H6 receipt (plus merge ordering: merge #42 first and retarget #44 to lightforge-main).
  • Track next (non-blocking):
    • VM and Kubernetes drivers were not exercised in retain mode (documented in PR; for drivers with gateway_manages_lifecycle == false, the sweep was already a no-op).
    • On a failed stop sweep (sweep_ok=false), CK_gateway_shutdown_disposition logs retained=0 even if some sandboxes failed to stop (compute/mod.rs:2727); sweep_ok=false already flags failure, so cosmetic only.
    • Connect's Gateway wrapper discards child stderr (2>/dev/null), so in packaged Connect runs CK_gateway_shutdown_disposition is only observable if Gateway logging is routed to a file/sink; Connect emits its own CK_gateway_shutdown_disposition_selected.

Ownership / Reuse

Capability Existing owner/module@head Decision Evidence
Gateway shutdown disposition parsing/resolution OpenShell:crates/openshell-server (64fc5dcf, on #42 6efaf294) new (narrow module in owning server crate) No existing disposition parser; cleanly separated from gateway_manages_lifecycle
Graceful shutdown compute cleanup OpenShell:crates/openshell-server/src/compute/mod.rs@64fc5dcf extend Extends ComputeRuntime::cleanup_on_shutdown in place without duplicating driver teardown
Docker graceful retain + re-adopt E2E OpenShell:e2e/rust/tests/docker_gateway_restart_readopt.rs@64fc5dcf extend Extends #42's Docker re-adopt suite with ManagedGateway::terminate (no SIGKILL escalation)

Validation

  • Independently verified at exact head 64fc5dcf9712642ad4d7ba71135ca4700c930ba6:
    • git diff --check 6efaf2943e7218850b49d8fab24a00fef66c4ef6...HEAD: clean
    • cargo fmt --all -- --check and (cd e2e/rust && cargo fmt --check): clean
    • cargo clippy -p openshell-server --all-targets -- -D warnings: clean
    • cargo test -p openshell-server --lib -- shutdown_disposition cleanup_on_shutdown command_reads_shutdown_disposition: 9 passed, 0 failed
  • Author-reported Docker E2E (e2e/rust/tests/docker_gateway_restart_readopt.rs, e2e/with-docker-gateway.sh): 2/2 passed twice (graceful SIGTERM retain + generation Some(1)->Some(2) re-adopt + missing-file fallback sweep, plus #42 SIGKILL test); inspected test code — no stubs/mocks replacing Docker/Gateway boundaries.
  • CI status: no GitHub Actions check runs reported on 64fc5dcf (unknown / none configured).
  • Integrated runtime E2E: live WP-6 H6 run with #42 + #43 + #44 + Connect #472 remains open.

Blast Radius

  • crates/openshell-server/src/{cli.rs, lib.rs, compute/mod.rs, shutdown_disposition.rs}, e2e/rust/src/harness/gateway.rs, e2e/rust/tests/docker_gateway_restart_readopt.rs. Zero changes to sandbox/supervisor crates or gateway_manages_lifecycle startup reconciliation.

Simplification

  • Clean: compact 302-line module (including unit tests) with no speculative config schema or TOML churn.

@agents-lightforge

Copy link
Copy Markdown

[hugo]

Conformance: CONDITIONAL PASS
Reviewed head: 64fc5dcf9712642ad4d7ba71135ca4700c930ba6
Bar: D1b integration candidate, reviewed as one contract with Connect ai-openshell#472 at a57e3609; H6 remains the runtime gate.

Findings:

Ownership/reuse:

Validation:

  • git diff --check 6efaf294...64fc5dcf: PASS
  • root and E2E Rust format: PASS
  • targeted server tests: 9 passed, 0 failed
  • server clippy with -D warnings: PASS
  • Author Docker receipt reviewed: graceful retain/re-adopt and missing-file sweep, 2/2 twice; production Docker/Gateway boundary, no replacement mock. Not independently rerun in this review.
  • GitHub: OPEN, CLEAN, MERGEABLE; no check runs reported.

Blast radius: server CLI, shutdown resolution, compute cleanup, and Docker restart harness only; supervisor/security policy paths unchanged.

Simplification: one bounded disposition parser and one branch in existing cleanup; no generic skip-cleanup mode.

@raghusubramanyam

Copy link
Copy Markdown
Author

WP-6 H6 live runtime receipt: run wp6-agy-20261127h6d PASSED (2026-11-26T20:56–21:03Z)

Exact composition (local build, no GitHub CI is configured for these PRs):

Repo Pin
Connect integration integration/wp6-agy-h6b2 64fa3f38 = ai-openshell#472 a57e3609 + ai-openshell#474 52f1ebb2 + ai-openshell#461 af0133d4
OpenShell closure integration/wp6-agy-h6 e2e20ace = #42 6efaf294 + #43 f72b22d8 + #44 64fc5dcf. Images: gateway f31ba45a, sandbox 720f7bbd, supervisor 63dd0bd0
Shell b08afb13
Phoenix e89345f7
global-e2e control dcee6fe1
SDK linux-arm64 09de4309 (sha256 520f4a9a…)

The SDK differs from attempts 2 and 3, which used 2642ce34. That binary predates SDK ai-openshell#160 --lane, while Shell D-070 calls session-start --lane connect. h6c is kept as the record of that pin skew.

Diagnostics on: AEGIS_CONNECT_GATEWAY_DIAGNOSTIC_LOG=1, OPENSHELL_LOG_LEVEL=debug. Run with native-run/run-native-gates.sh --diag --restarts 3 (aegis-api-phoenix#3900).

Gate Result
1 inputs passed: pins equal the checkouts, artifact SHA-256 values match, 0 FIFO wrappers, umask 022
accept passed: runtime-setup, bootstrap, first-session, restart-session; mind smoke post-enroll and post-restart
D1 restart re-adoption, 3 restarts passed: same sandbox container 53f47c46 and StartedAt 21:00:04.395Z across all restarts; supervisor generation 2→3→4→5, advancing by exactly one each time; a distinct healthy sidecar per restart; Ready, and exec rc=0 after each restart
D1b shutdown disposition CK_gateway_shutdown_disposition_selected mode=retain-running-sandboxes reason=runtime_restart on all 5 Gateway stops (preflight, accept and 3 proof restarts); no sweep
ai-openshell#471 passed: first_session_passed, no sandbox_session_not_registered, no policy-generation stale close
ai-openshell#474 diagnostic log home/logs/gateway.log plus 3 rotations, each ≤1.05 MB (bounded), mode 0600; 4 [redacted] substitutions; 0 hits from a credential-pattern scan (Authorization/Cookie/Bearer/token/secret/password, JWT, sk-, ghp_, PEM); no CK_gateway_output_log_unavailable; readiness unchanged
5 down down result=verified, rc=0; no namespace containers or volumes left; custody-unproven agent/identity volumes kept (ai-openshell#85); 0 lingering wrappers

Receipts are private (0600) on the build host under out/wp6-agy-20261127h6d/ (inputs.txt, accept.log, restart/restart-proof-receipt.json, 471-receipt.json, postdown-receipt.txt, restart/gateway.log*).

@agents-lightforge

Copy link
Copy Markdown

[hugo]

Conformance: PASS
Reviewed head: 64fc5dcf9712642ad4d7ba71135ca4700c930ba6

The D1b runtime gate is closed by WP-6 H6d wp6-agy-20261127h6d on exact aggregate e2e20ace. All five Gateway stops selected retain-running-sandboxes for runtime restart; the three restart proofs retained the same sandbox and re-adopted it Ready with exec rc=0; explicit final down verified cleanup.

No required finding remains. GitHub reports no CI/check runs; prior exact-head local suites plus H6d satisfy this cross-repository contract's evidence bar.

Merge sequencing: after #42 and #43 merge, retarget #44 to lightforge-main, confirm head/mergeability, then merge as a merge commit. No technical or evidence blocker remains.

Comment-only review: no approval, merge, or label change performed.

@raghusubramanyam
raghusubramanyam changed the base branch from fix/gateway-restart-readopt to lightforge-main November 26, 2026 21:08
@raghusubramanyam
raghusubramanyam merged commit 4134052 into lightforge-main Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants