Skip to content

Add the Windows Sandbox state-aware lifecycle + host daemon (Phase 2) - #578

Merged
Gudge (MGudgin) merged 1 commit into
mainfrom
user/gudge/wsb-phase2-state-aware
Jul 17, 2026
Merged

Gudge (MGudgin) merged 1 commit into
mainfrom
user/gudge/wsb-phase2-state-aware

Conversation

@MGudgin

@MGudgin Gudge (MGudgin) commented Jun 26, 2026 •

Copy link
Copy Markdown
Member

Phase 2 of the Windows Sandbox rewrite stack: add the state-aware lifecycle
(provision -> start -> exec* -> stop -> deprovision) for Windows Sandbox on top
of the Phase 1 one-shot rewrite, holding a single live VM across separate
wxc-exec phase processes behind a persistent detached host-side daemon. The SDK
follows in Phase 3.

Details

  • windows_sandbox_lifecycle: re-enables state_aware.rs -- the
    StatefulSandboxBackend impl on WindowsSandboxRunner. provision validates +
    snapshots the filesystem policy and mints wsb:<token>; start spawns the
    detached daemon (which boots the VM and holds the guest control connection);
    exec relays a script over the held connection; stop/deprovision tear down.
    A process-global named mutex serialises start/stop/deprovision; the single host
    VM slot is enforced.
  • Daemon rewrite (wxc_windows_sandbox_daemon): control_server.rs serves a
    nonce-authenticated localhost line protocol (PING/STOP/EXEC), each
    connection on its own task so a long exec can't block a STOP. Auth nonce via
    stdin; sandbox token via --token. Re-added to the workspace members (dropped
    in Phase 1); the legacy pipe_server/sandbox_vm/tcp_bridge/rendezvous
    modules are deleted (rendezvous now lives in the lifecycle crate).
  • wxc dispatch: run_state_aware routes the wsb: prefix to
    WindowsSandboxRunner with the same --experimental gate as one-shot;
    state_aware_dispatch learns the wsb -> WindowsSandbox prefix mapping.
  • Docs: the full dual-mode rewrites land here with the behavior they describe --
    windows-sandbox.md/windows-sandbox-reference.md, schema.md (dual-mode
    backend row + state-aware envelope section), the state-aware API doc, and
    copilot-instructions.md. While touching schema.md I reconciled its version
    table to the canonical schemas/schema-version.json (>=0.4, <=0.8; stable
    0.7.0-alpha; state-aware 0.6.0-alpha) -- the rewrite had left it at the pre-0.8
    <=0.7.
  • Tests: adds run_windows_sandbox_state_aware_tests.ps1 (state-aware E2E).

Tests

  • cargo check --workspace --all-targets, cargo clippy --workspace --all-targets -- -D warnings, and cargo fmt --all -- --check: clean.
  • cargo test: windows_sandbox_lifecycle 172 (incl. the state-aware suite),
    wxc_windows_sandbox_daemon 24 (incl. the renamed classify_unusable_* /
    exec_against_unusable_slot_* cases), wxc_common 364 (incl. the new wsb
    resolver test), wxc 13 -- all passed.
  • node scripts/versioning/check-schema-versions.js: green.

Related Issues

@MGudgin
Gudge (MGudgin) requested a review from a team as a code owner June 26, 2026 19:08
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase2-state-aware branch from 91e0bab to 5d411b0 Compare June 26, 2026 19:26
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase1-one-shot branch from 9d5d7e5 to e3ccb02 Compare July 7, 2026 04:04
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase2-state-aware branch 2 times, most recently from 569651e to 1830960 Compare July 7, 2026 17:16
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase1-one-shot branch from 8e2cb9b to 22cfc41 Compare July 11, 2026 20:17
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase2-state-aware branch from 1830960 to 9728f3d Compare July 11, 2026 20:17
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase1-one-shot branch from 22cfc41 to d140ea9 Compare July 11, 2026 20:23
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase2-state-aware branch from 9728f3d to e7957bc Compare July 11, 2026 20:23
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase1-one-shot branch 2 times, most recently from daa4c4a to 153e927 Compare July 13, 2026 19:54
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase1-one-shot branch from 5c49b06 to 11367a7 Compare July 13, 2026 20:49
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase2-state-aware branch from e7957bc to e562263 Compare July 13, 2026 21:17
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase1-one-shot branch from 11367a7 to 501432b Compare July 13, 2026 21:25
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase2-state-aware branch from e562263 to b29b708 Compare July 13, 2026 21:28
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase1-one-shot branch from 501432b to 1124ea8 Compare July 14, 2026 17:48
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase1-one-shot branch 2 times, most recently from 618e389 to d2ae233 Compare July 15, 2026 18:26
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase2-state-aware branch from 1191675 to e4928e9 Compare July 15, 2026 18:27
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase1-one-shot branch from d2ae233 to 053d70a Compare July 15, 2026 18:28
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase2-state-aware branch from e4928e9 to d24b3e7 Compare July 15, 2026 18:29
Base automatically changed from user/gudge/wsb-phase1-one-shot to main July 15, 2026 20:27
Copilot AI review requested due to automatic review settings July 15, 2026 20:28
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase2-state-aware branch from d24b3e7 to b11bc6e Compare July 15, 2026 20:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds Phase 2 of the Windows Sandbox rewrite by introducing a state-aware lifecycle (provision/start/exec/stop/deprovision) backed by a detached host daemon that holds a single live Windows Sandbox VM across multiple wxc-exec invocations. This integrates into the existing state-aware dispatcher, tightens experimental gating, and updates docs + E2E scripts to cover the new mode.

Changes:

  • Add Windows Sandbox support to state-aware dispatch (wsb: prefix routing + --experimental gating) and propagate the CLI experimental flag into parsed state-aware requests.
  • Reintroduce and rewrite the Windows Sandbox daemon crate to hold the VM and serve a nonce-authenticated localhost control protocol (PING/STOP/EXEC) with single-flight exec.
  • Update Windows Sandbox and state-aware lifecycle documentation and add a new PowerShell E2E script for the state-aware path.
Show a summary per file
File Description
tests/scripts/run_windows_sandbox_state_aware_tests.ps1 New state-aware Windows Sandbox E2E test driver script.
tests/scripts/README.md Documents the new state-aware Windows Sandbox test script.
src/core/wxc/src/main.rs Adds --force-reclaim flag and ensures state-aware requests inherit CLI --experimental.
src/core/wxc_common/src/state_aware_dispatch.rs Routes wsb: sandbox IDs to the Windows Sandbox backend + adds resolver test.
src/core/mxc_engine/src/state_aware.rs Enforces --experimental for state-aware Windows Sandbox/IsolationSession and dispatches WSB runner.
src/Cargo.toml Re-adds Windows Sandbox daemon crate to the workspace members.
src/Cargo.lock Adds lock entries for the daemon crate and its deps.
src/backends/windows_sandbox/lifecycle/src/vm.rs Improves orphan/wedge warning guidance (incl. --force-reclaim).
src/backends/windows_sandbox/lifecycle/src/teardown.rs Adds force-reclaim reconcile path and avoids unreachable!() on unexpected kill-plan gaps.
src/backends/windows_sandbox/lifecycle/src/lib.rs Enables the lifecycle crate’s state-aware module.
src/backends/windows_sandbox/lifecycle/src/control_plane.rs Introduces WXC_WSB_FORCE_RECLAIM plumbing and force-reclaim startup classification + clarifies kill-set widening logic.
src/backends/windows_sandbox/lifecycle/src/bridge.rs Adds host watchdog + capped output capture helpers; simplifies exit-frame API; adds tests.
src/backends/windows_sandbox/daemon/src/tcp_bridge.rs Removes legacy bridge implementation (moved to lifecycle crate).
src/backends/windows_sandbox/daemon/src/sandbox_vm.rs Removes legacy VM management module (moved to lifecycle crate).
src/backends/windows_sandbox/daemon/src/rendezvous.rs Removes legacy rendezvous polling module (moved to lifecycle crate).
src/backends/windows_sandbox/daemon/src/pipe_server.rs Removes legacy “pipe server” (replaced by new control server).
src/backends/windows_sandbox/daemon/src/main.rs New daemon main: owns VM lifecycle, record publishing, IPC binding, crash watchdog, and teardown behavior.
src/backends/windows_sandbox/daemon/src/control_server.rs New nonce-authenticated localhost control protocol with per-connection tasks + single-flight exec.
src/backends/windows_sandbox/daemon/Cargo.toml Adds lifecycle dependency and tokio test-util for daemon tests.
docs/windows-sandbox/windows-sandbox.md Updates high-level doc for dual-mode backend (one-shot + state-aware) and adds state-aware guidance.
docs/windows-sandbox/windows-sandbox-reference.md Expands reference doc for daemon IPC + force-reclaim recovery and state-aware record layout.
docs/state-aware-lifecycle/mxc-state-aware-sandbox-api.md Extends API doc to include Windows Sandbox in state-aware-capable backend set and clarifies prefix error behavior.
docs/schema.md Documents state-aware envelope in schema.md (but currently has a wire-shape issue noted in review comments).
.gitignore Renames the .review/ comment description.
.github/copilot-instructions.md Updates repo instructions to reflect Windows Sandbox state-aware daemon/lifecycle and new test script.

Review details

  • Files reviewed: 24/26 changed files
  • Comments generated: 2
  • Review effort level: Low

Comment thread docs/schema.md Resolved
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase2-state-aware branch 3 times, most recently from 24e8f68 to 9b01ab5 Compare July 16, 2026 17:45
This PR adds the multi-invocation Windows Sandbox lifecycle on top of the
authenticated one-shot backend, together with the reviewer-driven hardening
for guest output and VM teardown.

Details

* Implements provision, start, repeated single-flight exec, stop, and
  deprovision through `WindowsSandboxRunner` and durable owner-only records.
* Adds a detached host daemon that owns the live VM, authenticates localhost
  IPC, reconnects guest data channels, and reclaims only provably-owned
  orphans.
* Routes state-aware backend selection through `mxc_engine` with experimental
  gating, keeping `wxc-exec` as a thin CLI shell.
* Enforces terminal-exit ordering with a daemon-local release token that can
  be created only after restoring and unlocking the guest slot.
* Caps one-shot guest stdout/stderr capture per stream at 64 MiB (drain
  continues to EOF so the guest still exits cleanly, excess discarded).
  Truncation is surfaced via a host WARNING and an error_message note so a
  capped run is never presented as a clean, complete capture.
* Adds a host-side execution watchdog (`timeout_ms` + 30s grace) so a frozen
  guest that never reports completion can no longer hang the host forever.
* Adds --force-reclaim: an operator escape hatch that tears down a running WSB
  VM mxc cannot prove it launched (live snapshot as kill set) instead of
  wedging the machine-wide singleton. It never overrides a proven reclaim, an
  active run, or a probe failure, and cannot manufacture liveness. Transported
  to the detached daemon via the WXC_WSB_FORCE_RECLAIM env var.
* Hardens teardown: documents the `plan_kill_set` Windows Sandbox singleton
  invariant, and replaces an `unreachable!()` in the Owned teardown arm with a
  logged fail-safe that preserves the marker for a later reconcile.
* Softens the teardown-on-exit guarantee in warning + docs to best-effort (a
  launcher hard-kill can orphan a VM).
* Adds state-aware documentation and Windows Sandbox lifecycle E2E coverage.

Tests

* `cargo test -p windows_sandbox_lifecycle` (172) and
  `-p wxc_windows_sandbox_daemon` (24) pass, including new coverage for the
  output cap boundary, the host watchdog firing when the guest never reports
  exit, force-reclaim precedence rules, and the renamed `classify_unusable_*`
  / `exec_against_unusable_slot_*` cases.
* `cargo fmt --all -- --check` clean; `cargo clippy --workspace --all-targets
  -- -D warnings` clean; `cargo doc -p windows_sandbox_lifecycle --no-deps`
  resolves all intra-doc links.
* `cargo check --workspace --all-targets`; `cargo check -p mxc_engine -p wxc
  --all-features`.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 5e1a34f4-0c05-4c65-b5ef-fb53eb6ff578
Generated-with: claude-opus-4.8
@MGudgin
Gudge (MGudgin) force-pushed the user/gudge/wsb-phase2-state-aware branch from 9b01ab5 to e193f9f Compare July 16, 2026 21:35
@MGudgin
Gudge (MGudgin) merged commit d271ac4 into main Jul 17, 2026
22 checks passed
@MGudgin
Gudge (MGudgin) deleted the user/gudge/wsb-phase2-state-aware branch July 17, 2026 04:14
Gudge (MGudgin) pushed a commit that referenced this pull request Jul 17, 2026
This PR fixes four robustness gaps in the Windows Sandbox state-aware daemon
and lifecycle that surfaced in post-merge review of #578.

Details

* Daemon diagnostics are no longer discarded. `spawn_daemon` redirects the
  detached daemon's stderr to `daemon.log` in the per-sandbox owner-only record
  directory (append), so boot / teardown / orphan-reclaim failures in the
  VM-owning process are observable. The start-timeout error now names the
  concrete log path instead of "see daemon logs".
* The streaming exec path gains a host-side watchdog. `handle_exec` wraps
  `stream_exec_on_guest` in `host_watchdog_deadline(timeout_ms + grace)` (the
  same helper the one-shot path uses, now `pub`); on expiry it marks the guest
  slot Unusable and sends the client a synthetic timeout exit frame, so a
  frozen-but-alive guest can no longer wedge the single-flight slot forever (the
  VM crash watchdog only fires once the VM processes are gone).
* VM-slot contention is reported coherently. A new `HostVmLock::try_acquire`
  distinguishes genuine contention (the wait timed out) from a real create/wait
  failure: on contention the daemon exits with a distinct
  `DAEMON_EXIT_VM_SLOT_BUSY` code that `start` maps to `backend_unavailable`,
  while a real mutex failure propagates as an opaque backend error (with
  daemon.log diagnostics) rather than being mis-reported as busy.
* The IPC wire protocol is versioned. `DaemonRecord` carries a
  `protocol_version` (independent of the on-disk `RECORD_SCHEMA_VERSION`, added
  with `#[serde(default)]` so a pre-versioning record reads back incompatible);
  `exec` refuses to drive a live daemon left by a different install whose wire
  version differs, rather than streaming frames against a mismatched peer.
  `stop`/`deprovision` keep using the stable single-line `STOP` so an orphaned
  VM can always be reclaimed.
* Docs: windows-sandbox-reference.md updated for the daemon.log sink, the exec
  watchdog, the VM-slot busy mapping, and protocol versioning.

Tests

* New unit tests: `protocol_compatible` plus a serde-default
  (pre-versioning record reads incompatible) case for the new record field,
  `ensure_daemon_protocol` accept/refuse, the `vm_slot_busy` exit-code
  classifier, and a two-thread `named_mutex_try_acquire` test asserting a
  contended mutex yields `Ok(None)` (busy) rather than `Err`. The M2 watchdog
  reuses the one-shot-tested `host_watchdog_deadline` helper and
  `tokio::time::timeout` pattern.
* `cargo fmt --all -- --check` clean; `cargo clippy --workspace --all-targets
  --target x86_64-pc-windows-msvc -- -D warnings` clean.
* `cargo test`: windows_sandbox_lifecycle 177 (+5), wxc_windows_sandbox_daemon
  24, mxc_engine 25, wxc 15, wxc_common 453 -- all pass.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 5e1a34f4-0c05-4c65-b5ef-fb53eb6ff578
Generated-with: claude-opus-4.8
Darren Hoehna (dhoehna) added a commit to dhoehna/mxc that referenced this pull request Jul 17, 2026
Resolve conflict in wxc_common/src/state_aware_dispatch.rs: register both the `lxc` (this PR) and `wsb` (upstream microsoft#578) state-aware backend prefixes in backend_from_prefix, and keep both resolve_backend unit tests. Added `correlation_vector: None` to the lxc test to match the ParsedStateAwareRequest field introduced upstream (microsoft#624).

Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants