Skip to content

feat(sandbox): support deleting a sandbox only if it has an expected ID - #1

Open
rodrigosetti wants to merge 2 commits into
mainfrom
feat/3210-conditional-sandbox-delete/rodrigosetti
Open

rodrigosetti wants to merge 2 commits into
mainfrom
feat/3210-conditional-sandbox-delete/rodrigosetti

Conversation

@rodrigosetti

@rodrigosetti rodrigosetti commented Oct 1, 2026 •

Copy link
Copy Markdown
Owner

Summary

DeleteSandbox only takes a name, so a client that looked up a sandbox and later deletes it by name can delete a same-name replacement created in between. This adds an optional expected_sandbox_id precondition: the gateway deletes only if the name still resolves to that ID, and otherwise fails with ABORTED and reason SANDBOX_IDENTITY_MISMATCH without deleting anything.

Related Issue

Closes ai-openshell#3210 (ai-openshell#3210)

Changes

  • API: DeleteSandboxRequest.expected_sandbox_id (optional string, field 5). Absent keeps today's name-based behavior. An explicitly empty value returns INVALID_ARGUMENT so it cannot silently become unconditional. A mismatch is an error even with allow_missing=true; a missing name still follows allow_missing.
  • Gateway: the check runs where the compute runtime resolves the name. From there the existing delete path is already pinned to the resolved ID (gate, revalidation by ID, CAS), so a replacement created after resolution is not deleted either. Returns ABORTED with a structured SANDBOX_IDENTITY_MISMATCH reason via a new rpc_error::aborted helper, matching how the API already reports conditional-write conflicts. Clients should match on the ErrorInfo reason, since other ABORTED errors exist on this path.
  • CLI: openshell sandbox delete --id ID deletes the sandbox with that ID. The API stays name-addressed (per proto/README.md), so the CLI looks up the name that currently has the ID and sends a delete conditioned on it; if the sandbox is deleted or replaced in between, the gateway refuses and the CLI reports the ID as already deleted. openshell sandbox delete NAME --expected-id ID also requires that the name still refers to that ID (one name only, conflicts with --all); a mismatch exits non-zero. sandbox create --no-keep cleanup now deletes by the created sandbox's ID and treats a replacement as the original already being gone. Port forwards are stopped only after a conditional delete is confirmed.
  • TUI: [d] sends the selected row's sandbox ID; a mismatch shows a status message and refreshes the list.
  • Rust SDK: new SandboxDeleteOptions { allow_missing, expected_sandbox_id }; delete_sandbox takes impl Into<SandboxDeleteOptions>, so existing DeleteOptions callers compile unchanged.
  • Python SDK: SandboxClient.delete(..., expected_sandbox_id=...). SandboxSession.delete and Sandbox context-manager cleanup now pass the session's own ID (behavior change); cleanup treats a mismatch as already absent. If the session's ID is empty, they raise SandboxError instead of deleting by name, matching the CLI --no-keep cleanup and the TUI.
  • TypeScript SDK: SandboxDeleteOptions.expectedSandboxId on sandbox.delete.
  • Go SDK: regenerated bindings expose ExpectedSandboxId on the raw request; no curated option, since DeleteOptions is shared across resource types.
  • Docs/skills: API errors reason table and a "Conditional sandbox deletion" section, sandbox overview CLI example, openshell-cli and tui-development skills.
  • Updates the reviewed public-schema fingerprint; the durable storage fingerprint is unchanged.

Gateways that predate the field ignore it and delete by name; the docs call this out.

Testing

  • Checks appropriate to the affected code and behavior pass
    • cargo test for openshell-server, -sdk, -cli, -tui, -core: 3,255 passed
    • cargo clippy --workspace --all-targets -D warnings, mise run pre-commit, mise run go:ci, mise run docs
    • Python: pytest for sandbox_test.py / sandbox_cleanup_test.py, ruff, ty
    • TypeScript: Biome, tsc, Vitest; buf lint
  • Unit tests added/updated: gateway mismatch (with and without allow_missing), match, empty value, and missing-name cases; Rust SDK request forwarding; CLI flag parsing, forwarding, stale-ID failure, --id lookup with missing and replaced sandboxes, and --no-keep cleanup with a replacement; TUI request binding; Python request presence, session binding, empty-ID refusal, and context-manager cleanup against a rich-status mismatch; TypeScript forwarding.
  • E2E tests added/updated: test_conditional_delete_preserves_same_name_replacement in e2e/python/test_sandbox_api.py.
    • Against a Docker-driver gateway built from this branch, a stale --expected-id was refused with SANDBOX_IDENTITY_MISMATCH and the replacement survived; a matching ID was accepted.
    • With the same gateway, sandbox delete --id <stale ID> reported the sandbox as already deleted and left the same-name replacement alone; --id <current ID> deleted it.
    • Limitation: on my macOS host, Docker sandboxes never reach Ready because the supervisor container cannot connect back to the gateway. The existing, untouched test_mutation_replay_preserves_sandbox_lifecycle_and_replacement fails the same way there, so the full e2e test still needs a Linux CI run.

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)
  • Architecture docs updated (if applicable)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Fail-open paths and name-based forward cleanup can still affect same-name replacements, while Python cleanup can rebind to a replacement.

Review effort: Balanced
Findings: 4 High severity · 3 Medium severity · 3 Low severity

Open (10)
What changed in this PR

Adds identity-conditioned sandbox deletion to prevent stale clients from deleting same-name replacements.

Changes:

  • Adds expected_sandbox_id validation and enforcement.
  • Integrates conditional deletion across CLI, TUI, and SDKs.
  • Adds tests, documentation, and schema updates.
File Description
skills/​openshell-cli/​SKILL.md Documents ID-based deletion.
sdk/​typescript/​src/​index.ts Exports deletion options.
sdk/​typescript/​src/​client.ts Forwards expected sandbox IDs.
sdk/​typescript/​src/​client.test.ts Tests TypeScript forwarding.
sdk/​go/​proto/​openshellv1/​openshell.pb.go Regenerates Go bindings.
python/​openshell/​sandbox.py Adds conditional session cleanup.
python/​openshell/​sandbox_test.py Tests Python request binding.
python/​openshell/​sandbox_cleanup_test.py Tests replacement-safe cleanup.
proto/​openshell.proto Defines the optional ID precondition.
e2e/​python/​test_sandbox_api.py Tests replacement preservation end-to-end.
docs/​sdk/​api-errors.mdx Documents mismatch errors and usage.
docs/​how-it-works/​sandboxes/​overview.mdx Documents CLI flags.
crates/​openshell-tui/​src/​lib.rs Handles conditional TUI deletion.
crates/​openshell-tui/​src/​app.rs Builds ID-bound delete requests.
crates/​openshell-server/​src/​storage_proto.rs Updates schema fingerprint.
crates/​openshell-server/​src/​grpc/​sandbox.rs Validates and forwards the condition.
crates/​openshell-server/​src/​grpc/​mutation_replay/​ordinary/​tests.rs Updates request fixtures.
crates/​openshell-server/​src/​compute/​mod.rs Enforces identity before deletion.
crates/​openshell-sdk/​tests/​client_mock.rs Tests Rust SDK forwarding.
crates/​openshell-sdk/​src/​types.rs Adds sandbox deletion options.
crates/​openshell-sdk/​src/​lib.rs Exports the new options.
crates/​openshell-sdk/​src/​client.rs Supports conditional Rust SDK deletion.
crates/​openshell-cli/​tests/​sandbox_create_lifecycle_integration.rs Tests CLI flags and cleanup.
crates/​openshell-cli/​src/​run.rs Implements ID lookup and conditional deletion.
crates/​openshell-cli/​src/​main.rs Adds --id and --expected-id.
.agents/​skills/​tui-development/​SKILL.md Updates TUI deletion guidance.
Files not reviewed (1)
  • sdk/go/proto/openshellv1/openshell.pb.go: Generated file

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread crates/openshell-cli/src/run.rs Resolved
Comment thread crates/openshell-tui/src/app.rs Resolved
Comment thread python/openshell/sandbox.py Resolved
Comment thread python/openshell/sandbox.py Resolved
Comment thread crates/openshell-cli/src/run.rs Resolved
Comment thread crates/openshell-tui/src/lib.rs Resolved
Comment thread crates/openshell-tui/src/lib.rs Resolved
Comment thread .agents/skills/tui-development/SKILL.md Resolved
Comment thread skills/openshell-cli/SKILL.md Resolved
DeleteSandbox addressed sandboxes only by name, so a client that observed
a sandbox and later deleted it by name could delete a same-name
replacement created in between. Add an optional expected_sandbox_id
precondition: the gateway deletes only if the name still resolves to
that ID and otherwise returns ABORTED with reason
SANDBOX_IDENTITY_MISMATCH in ErrorInfo, so clients can tell it apart
from other conflicts. The existing ID-pinned delete path then carries
the request through, so a replacement created after resolution is not
deleted either. An explicitly empty value is rejected so it cannot
silently become an unconditional delete. Requests without the field
keep name-based behavior.

Add `openshell sandbox delete --id <ID>`, which finds the name that
currently has the ID and sends a delete conditioned on it, reporting the
ID as already deleted if it was deleted or replaced in between. Add
`openshell sandbox delete NAME --expected-id <ID>`, which fails instead
of deleting a same-name replacement.

Expose the condition in the Rust, Python, and TypeScript SDKs and the
regenerated Go bindings. Python sandbox sessions and managed-sandbox
cleanup, CLI --no-keep cleanup, and TUI deletes now bind to the sandbox
they hold, treating a same-name replacement as the original already
being gone where appropriate. CLI --no-keep cleanup and TUI deletes
refuse to delete when the sandbox ID is unknown.

Document the condition, its error reason, and that gateways predating
the field ignore it and delete by name.

Closes ai-openshell#3210

Signed-off-by: rodrigosetti <[email protected]>
@rodrigosetti
rodrigosetti force-pushed the feat/3210-conditional-sandbox-delete/rodrigosetti branch from 4ade185 to 7608d2c Compare October 26, 2026 14:00
SandboxSession.delete fell back to an unconditional delete by name when
the session's sandbox ID was empty, so it could remove a same-name
replacement. The CLI --no-keep cleanup and TUI already refuse in that
case. Raise SandboxError instead, which also makes managed-sandbox
cleanup fail closed.

Refs ai-openshell#3210

Signed-off-by: rodrigosetti <[email protected]>
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

This pull request has had no activity for 14 days and is now marked stale.

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.

Support atomic delete-by-identity for sandboxes (no compare-and-delete primitive today)

2 participants