#1235

sandbox: the spec file saved by --use-spec is deleted right after it is written when a sandboxed hook or MCP server is alive

OpenFabioLeitaoopened 16d ago

Version / branch / commit

v0.14.0 (8d23195f), built from the tag with go1.27.4 -buildvcs=false (reports version dev). Also reproduced on a fork build (0.14.0+up.0.8d23195f...). Not verified on current main.

OS and environment

Linux (Debian-based, kernel 7.2), zero-linux-sandbox companion installed, a user config that defines hooks (6 command hooks) and MCP servers. Provider: any OpenAI-compatible endpoint.

Steps to reproduce

d=$(mktemp -d) && cd "$d" && git init -q && echo hi > a.txt && git add a.txt && git commit -qm init
zero exec --use-spec --auto medium --max-turns 8 \
  "Plan a one-line change: append the word world to a.txt"
ls -la .zero/specs                 # missing
zero spec show <spec-id printed above>
zero exec --resume <session-id> "continue"

Expected behavior

--use-spec ends with Spec draft saved for review ... path: .zero/specs/<id>.md, the file exists, and zero spec show / zero spec approve can use it.

Actual behavior

The run prints Spec draft saved for review with the path, but .zero/ does not exist afterwards (no .zero/specs). zero spec show fails with stat spec file: lstat .../.zero/specs/<id>.md: no such file or directory, so zero spec approve has nothing to approve; --resume of that session answers Zero session is not resumable. The run also ends with [zero] mcp_close_error: exit status 1. The text of the spec survives only in the session events (events.jsonl, field plan).

Root cause, measured with strace -f on the process and by reading the code: the host does write the spec (mkdirat .zero, mkdirat .zero/specs, openat O_CREAT|O_EXCL succeed), and right afterwards three zero-linux-sandbox processes run unlinkat(.zero) / AT_REMOVEDIR. protectedCreateMonitor in internal/sandbox/protected_create_linux.go watches the workspace with inotify for the creation of .zero / .agents and calls os.RemoveAll, without telling the sandboxed child apart from the host process. While any sandboxed hook or MCP server is alive, the host's own submit_spec is deleted too.

Control: with a minimal config (XDG_CONFIG_HOME=<tmp>, one provider, no hooks and no MCP servers) the same command leaves .zero/specs/<id>.md on disk.

Relevant logs, screenshots, or error messages

Spec draft saved for review.
  spec: 2026-11-19-append-world-to-a-txt
  path: .zero/specs/2026-11-19-append-world-to-a-txt.md
  draft session: spec-pure-1
[zero] mcp_close_error: exit status 1
zero spec show ...: [zero] stat spec file: lstat .../.zero/specs/...md: no such file or directory
zero exec --resume ...: [zero] Zero session is not resumable: spec-pure-1

Happy to test a fix or send a PR if the team wants one (I would first need the issue-approved label, per CONTRIBUTING).

Activity

2
FabioLeitao16d ago

Request: if the team agrees this is a bug worth fixing, could a maintainer add the issue-approved label here? Per CONTRIBUTING I will not open a pull request before that label is set. When it is, I can send a small fix with a regression test for protectedCreateMonitor (the sandboxed child and the host process must be told apart, so the host's own spec file is not removed), and I will quote the failing run of the test on the unfixed code in the PR.

If the preferred direction is different (for example, writing the spec outside the protected path), I am happy to follow it.

Vasanthdev200416d ago

Confirmed against main at 8288ae81 by reading the code. I have not run it, there is no Linux box on my side today.

protectedCreateMonitor cannot tell who created the path. Every zero-linux-sandbox helper watches the workspace for each fully protected metadata name, .zero among them, when it was absent at launch (the bwrap filesystem plan puts an absent protected name into ProtectedCreateTargets), and on any create or move-in it kills its own command and runs os.RemoveAll on the path. scanAndRemove does the same on every poll and once more at exit. So while a sandboxed hook or MCP server is alive, a .zero/ created by the host's submit_spec, by zero hooks add in another terminal, or by the user is deleted, and the long-lived child is killed with it. That also explains your mcp_close_error.

Labelled bug. I am holding issue-approved for one round, because this is a sandbox boundary and the approach matters more than the patch:

  • inotify carries no creator, so the monitor has no way to tell the host from the child on its own. Whatever signal a fix uses must be one the sandboxed command cannot produce or forge. Anything inside the workspace does not qualify.
  • A path the sandboxed command creates still has to be removed and still has to end that command. That is what the monitor is for, and a .zero/hooks.json planted by a child is the case it exists to stop.
  • Nothing the host or the user wrote may be removed.

Two shapes I can see. Keep the monitor and have the host record its own in-flight creations somewhere outside every write root, which the helper checks before it removes. Or stop needing the monitor for these names by giving the child a read-only placeholder at the absent path inside its mount namespace, so a create fails there and nothing on the host is ever touched. The second gives up the "path does not exist" view inside the sandbox, which the comment at the top of the file says was a deliberate choice, so it needs @anandh8x's view.

Could you say which way you would go, or a third, before writing it? Then I will add the label. The regression test you describe (the host's write survives while a sandboxed child is alive) is the right one; please keep a case where the child's own create is still removed.