opencode-win-guard 以完整性級別構建 Windows AI 編碼智能體沙盒
Letting AI coding agents run unattended on Windows: a sandbox built on integrity levels
Yehor Cherednichenko 開發 opencode-win-guard,透過 Windows 完整性級別限制 opencode 編碼智能體對專案以外位置的寫入。
I like to give a coding agent a long task, go to sleep, and read the result in the morning. With opencode that works well, often with a lead agent and several subagents at the same time. What I did not like was the question that came with it: what can these agents do to my computer while nobody is watching?
An agent makes mistakes: it deletes the wrong folder, writes a file into another project, fills the disk with build output. And it can be talked into worse by text it reads: a README, an issue, a web page with instructions hidden in it. On Linux and macOS there are OS sandboxes for opencode (bubblewrap, Seatbelt). For opencode on native Windows I found none that works without Docker, WSL or administrator rights. So I built one: opencode-win-guard.
A real run of the plugins and the launcher in an isolated opencode serve. The agent is a scripted fake model, and the picture is drawn from the recorded tool calls and answers.
Why command filters are not enough
The first idea is always a deny list: refuse rm -rf /, format, Remove-Item C:\Users... Several opencode plugins do this, and they are useful. But a filter sees what a command says, not what it does:
python build.py # what does build.py do? the filter does not know
node -e "require('fs').rmSync(process.env.USERPROFILE, {recursive:true})"
An agent writes code. Any program it writes and runs can delete, overwrite or upload whatever your user account can. To limit that, the check has to happen where the write happens: in the operating system.
Windows already has the tool: integrity levels
Since Vista every process and every securable object on Windows has an integrity level: Low, Medium, High, System. Your programs run at Medium. The rule that matters here is no write up: a process may not write to an object with a higher level than its own, whatever the normal permissions say. Browsers have used this for their sandboxes for years.
So the plan:
- Run every shell command of an agent with a Low integrity token.
- Put the Low label on the project folder (and on a cache folder), so the agent can write there.
- Everything else on the disk is Medium by default, so Windows refuses every write, delete and rights change elsewhere. It does not matter whether the write comes from PowerShell, Python, a compiled program or a script the agent wrote a minute ago.
Reading stays possible: a Low process can read what you can read. That is on purpose, because an agent that cannot read your other projects, SDKs and docs is not much use. For the files that must not be read, there is a second label.
Secret files: no read up
A mandatory label can also say no read up. win-guard puts the label S:(ML;;NWNR;;;ME) (Medium, no write up, no read up) on every file listed in secrets.txt: SSH keys, cloud credentials, .npmrc, the key file of browsers, the sign-in data of other tools, and opencode's own service password. Your own programs run at Medium and notice nothing. A Low program gets "Access denied".
The list is applied before every command, and labels are put back when a program replaces its file. A line starting with ! means "this must be protected, or no command runs at all".
The launcher
opencode lets a plugin change the shell of a command before it starts (shell hook create.before). win-guard swaps the shell for a small launcher (bash.exe, powershell.exe, pwsh.exe, cmd.exe; one copy per name, because opencode picks the arguments by the program name). The launcher:
- makes a restricted copy of the token with
CreateRestrictedTokenand sets its integrity level to Low; - starts the real shell suspended with
CreateProcessAsUserW, puts it into a job object, and only then lets it run; - the job ends everything the command started when it ends, caps the memory of the whole job so that a set amount of RAM stays free for you, and takes away clipboard reading, system settings and shutdown;
- watches free disk space and the size of the project folder, and ends the command when a limit is reached (exit code 125);
- refuses to start the command at all when any part is missing (exit code 126). It never falls back to full rights.
It is written in C#, which may look odd in a Node.js plugin. The reason: all of this needs Win32 functions that no script can call, and the C# compiler of the .NET Framework is part of every Windows 10 and 11. The installer builds the launcher from the sources on your own computer, so there is no SDK to install and no unsigned .exe to download.
Git Bash was the hardest part: it would not start at Low integrity from its usual install folder, only from a copy of its own, so the installer copies Git for Windows (about 400 MB) into the sandbox folder. Without that copy the agents get PowerShell and cmd.
The edit and write tools of opencode run inside the opencode process, not in the shell, so the plugin checks their paths itself: the real path, with links and junctions resolved, must be inside the project. And tools that the guard does not know are refused, because a new plugin or MCP server would run with full rights.
The killswitch
The sandbox stops damage. It does not stop an agent from trying, again and again, or from reading and uploading what is not on the secret list. That is the job of the second plugin, which checks every call before it runs:
-
Stop for what is clearly harmful: naming key files, asking Windows for saved passwords (
cmdkey,CredRead...), wiping disks or backups, turning off Defender or the firewall, starting or controlling another opencode outside the sandbox, deleting a whole drive or user folder. -
Refuse for what is risky but sometimes harmless: piping a download into a shell, encoded PowerShell, uploads (
curl -T,scp), autostart entries. - It does not only read the command.
powershell -File build.ps1makes it readbuild.ps1, and the scripts that one calls;npm testmakes it read thetestscript inpackage.json. An agent cannot hide a command in a file it just wrote. - While commands run, it watches for a file named
STOPin the project and for a large part of the project's files disappearing. - Many refused calls in a short time also count as a stop: an agent trying variant after variant of a refused command is a bad sign.
A stop is meant to be loud. Every agent of the folder is interrupted, the leads first so that they cannot start new subagents. The lead gets a note, and AGENT_STOPPED.txt in the project says what happened and who did it. The stop is saved to disk, so it survives a restart of opencode. It is released only when you write a message in a session of that folder.
Emergency stop of the agents, 2026-11-26T00:34:33.365Z
Reason: it opens a file with keys or passwords
By: lead
What exactly:
read: C:\demo\secret\key.txt
The trade-off of a Low label
OpenAI wrote about building the Codex sandbox for Windows, and they considered integrity levels too. They chose a different design, for a fair reason: once the project folder carries the Low label, any Low-integrity process on the machine may write there, not only the agent.
That is true, and it is the price of this approach. What you get for it: no administrator rights, no extra user accounts, nothing changed outside the project folder, its cache, and the files you list as secret. And uninstall.ps1 takes every label off again. Only the folders you name get the label, and on a typical machine few programs run at Low integrity. If you use Codex, use its own sandbox; Microsoft is also working on isolation for agents in Windows itself. win-guard is for opencode, today, on the Windows you already have.
What it does not do
I wrote a security model with ten known gaps, and I think it is the most important page of the project. The main ones:
- Reading is free, and so is the network. Apart from the secret files, an agent can read what you can read and send it somewhere. Uploads are refused by the text check only.
-
secrets.txtmust be complete. A file that is not listed is open. The example covers common places, not your computer. - Windows Credential Manager can be read from Low integrity by a compiled program. Only commands that name it are stopped.
- Drives without NTFS rights (most USB sticks) are writable by Low programs.
- The plugins live in the opencode process. The sandbox protects their settings from the agents, but anything that can change your opencode configuration can switch them off.
-
Labels persist. A program built in the project also runs at Low integrity when you start it yourself. Usually that is fine; some programs fail if they cannot write their settings, and the
lowLabelsetting exists for that.
It is a seat belt, not a vault: it protects against the mistakes of an agent and against harmful commands it was talked into, not against code written to escape. Keep version control, keep secrets out of the project.
Trying it
You need Windows 10 or 11, opencode 2.2 or newer, and Node.js. No administrator rights.
Download and unpack the latest release (or clone the repository), then in PowerShell:
cd opencode-win-guard
.\install.ps1 -Folder C:\Users\me\Projects\my-project
Then read ~\.local\share\opencode\sandbox\secrets.txt and add the files with keys and passwords of your own computer. Restart opencode and start a session in the project folder; the log ~\.local\share\opencode\log\win-guard.log says on for <folder>. Everything else is in the README, including the settings, and uninstall.ps1 takes every label off again.
The tests run the real plugins in a real opencode serve, with a scripted fake model and temporary folders, so they need no provider or account.
A small companion: subagents that talk to each other
While building this I also wrote opencode-crew-mail: two tools, crew_list and crew_send, so that subagents running at the same time can message each other directly instead of sending everything through the lead. It works with win-guard (add the two tools to extraTools). More about it in a separate post.
Feedback welcome, especially the breaking kind
This is version 0.1. If you run agents on Windows, I would like to know what breaks in your builds inside the sandbox, and which harmless commands get refused. And if you find a way around the sandbox or the secret files, please report it privately. That is the most useful contribution of all.
The README is also available in Chinese, Japanese, Korean, Spanish, Portuguese, German, French and Russian, and the stop note speaks the same languages (the language setting). Corrections from native speakers are welcome.
來源:DEV Community · dev.to
