fix(agent): Python's private temp folders work in the Windows sandbox #33

Merged
HumanBean merged 1 commit from HumanBean/Beanstalk:fix/win-mkdtemp into main 2026-11-16 00:20:17 +00:00
Owner

Why

Every Windows job fails before it builds anything. The tester's PC runs make_game.py, its compiler check stops with PermissionError: [WinError 5] Access is denied: '...\Beanstalk\work\longshot\tmpXXXX', and the job reports an error (Longshot #910, RTX 4070, Windows 11). #25 and #28 didn't fix it, but #28's probe lines in the run on nightly-52 (rp_d7a616581e156950) show the cause. There are two problems:

  1. Python's private folders lock the sandbox out. Since Python 3.13, a folder made with mode 0o700 on Windows gets an access list of its own: SYSTEM, the Administrators and the owner, protected from inheritance. That is what tempfile.mkdtemp and TemporaryDirectory ask for. A process in an AppContainer is let in only when the list also names its AppContainer. So in the sandbox Python makes the folder, then can't write its first file in it (t.c), and can't clean it up either. The probe shows the pattern: a plain os.mkdir in the checkout works ("dir ok"), the 0o700 one doesn't.
  2. TEMP points at a folder that isn't there. Windows sets an AppContainer process's TEMP and TMP to LOCALAPPDATA\Packages\beanstalk.sandbox\AC\Temp, whatever the environment says, and that folder was never made. Python found no usable temp folder and fell back to the current one, the Longshot checkout.

What changed

  • WinPythonFix (Core/WindowsPlan.cs): a .pth file and a short module the agent writes into its own builder Python's site-packages (tools/python). Only where BEANSTALK_SANDBOX_PYTHON=1 (the Windows sandbox's environment) and only on Windows, os.mkdir(path, 0o700) makes the folder with the plain mode instead, so it inherits the sandbox's grants like any other folder in the job. Other modes are unchanged, and outside the sandbox nothing changes.
  • Workspace.InstallPythonFixAsync: writes those files after the Windows tools are fetched, only into the agent's own tools/python. It finds site-packages with sysconfig and refuses a path outside the tools folder. A Python the tester named in the setup is theirs and is left alone, with a line in the log.
  • WinEnv.Folders: also makes <home>\AppData\Local\Packages\beanstalk.sandbox\AC\Temp before each start, so Python's temp files go to the job's home and not the checkout. The AppContainer's name has one home, WinSandboxName.Name, which WinAppContainer now uses.
  • The temp probe also makes a TemporaryDirectory, writes a file in it, and says whether the fix was loaded (@@probe mkdtemp ok fix).

Testing

  • Agent checks: 1674 passed, on Linux. New checks cover the AppContainer temp folder's path, the environment variable, and the fix's text: only 0o700, only on Windows, only in the sandbox.
  • The module itself, run on Linux with sys.platform faked as win32: with the variable set, tempfile.mkdtemp() asks for 0o777; without it, 0o700; os.mkdir(..., 0o755) stays 0o755 either way.
  • Not tested: a Windows job, since I'm on Linux and the AppContainer only exists on Windows. Once this is in a nightly and the tester has updated, @Beanstalk on Longshot #910 should get past "the main base". The report's probe lines should say @@probe mkdtemp ok fix, and gettempdir should be the AC\Temp folder.

Risks

The fix only touches the agent's own copy of Python, and only inside the sandbox. There, temp folders are no longer private to the job's account. That's fine: the sandbox's job folder is the job's alone. If the fix doesn't load, nothing changes from today, and the probe says so.

## Why Every Windows job fails before it builds anything. The tester's PC runs `make_game.py`, its compiler check stops with `PermissionError: [WinError 5] Access is denied: '...\Beanstalk\work\longshot\tmpXXXX'`, and the job reports an error ([Longshot #910](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/910), RTX 4070, Windows 11). #25 and #28 didn't fix it, but #28's probe lines in the run on nightly-52 ([rp_d7a616581e156950](https://beanstalk.magicbean.dev/reports/rp_d7a616581e156950)) show the cause. There are two problems: 1. **Python's private folders lock the sandbox out.** Since Python 3.13, a folder made with mode `0o700` on Windows gets an access list of its own: SYSTEM, the Administrators and the owner, protected from inheritance. That is what `tempfile.mkdtemp` and `TemporaryDirectory` ask for. A process in an AppContainer is let in only when the list also names its AppContainer. So in the sandbox Python makes the folder, then can't write its first file in it (`t.c`), and can't clean it up either. The probe shows the pattern: a plain `os.mkdir` in the checkout works ("dir ok"), the `0o700` one doesn't. 2. **TEMP points at a folder that isn't there.** Windows sets an AppContainer process's TEMP and TMP to `LOCALAPPDATA\Packages\beanstalk.sandbox\AC\Temp`, whatever the environment says, and that folder was never made. Python found no usable temp folder and fell back to the current one, the Longshot checkout. ## What changed - `WinPythonFix` (`Core/WindowsPlan.cs`): a `.pth` file and a short module the agent writes into its own builder Python's site-packages (`tools/python`). Only where `BEANSTALK_SANDBOX_PYTHON=1` (the Windows sandbox's environment) and only on Windows, `os.mkdir(path, 0o700)` makes the folder with the plain mode instead, so it inherits the sandbox's grants like any other folder in the job. Other modes are unchanged, and outside the sandbox nothing changes. - `Workspace.InstallPythonFixAsync`: writes those files after the Windows tools are fetched, only into the agent's own `tools/python`. It finds site-packages with `sysconfig` and refuses a path outside the tools folder. A Python the tester named in the setup is theirs and is left alone, with a line in the log. - `WinEnv.Folders`: also makes `<home>\AppData\Local\Packages\beanstalk.sandbox\AC\Temp` before each start, so Python's temp files go to the job's home and not the checkout. The AppContainer's name has one home, `WinSandboxName.Name`, which `WinAppContainer` now uses. - The temp probe also makes a `TemporaryDirectory`, writes a file in it, and says whether the fix was loaded (`@@probe mkdtemp ok fix`). ## Testing - Agent checks: 1674 passed, on Linux. New checks cover the AppContainer temp folder's path, the environment variable, and the fix's text: only `0o700`, only on Windows, only in the sandbox. - The module itself, run on Linux with `sys.platform` faked as `win32`: with the variable set, `tempfile.mkdtemp()` asks for `0o777`; without it, `0o700`; `os.mkdir(..., 0o755)` stays `0o755` either way. - **Not tested:** a Windows job, since I'm on Linux and the AppContainer only exists on Windows. Once this is in a nightly and the tester has updated, `@Beanstalk` on Longshot #910 should get past "the main base". The report's probe lines should say `@@probe mkdtemp ok fix`, and `gettempdir` should be the `AC\Temp` folder. ## Risks The fix only touches the agent's own copy of Python, and only inside the sandbox. There, temp folders are no longer private to the job's account. That's fine: the sandbox's job folder is the job's alone. If the fix doesn't load, nothing changes from today, and the probe says so.
fix(agent): Python's private temp folders work in the Windows sandbox
Some checks are pending
CI / Coordinator tests (pull_request) Waiting to run
CI / Agent build and checks (pull_request) Waiting to run
CI / Coordinator image (pull_request) Waiting to run
CI / Conventional Commit title (pull_request) Successful in 3s
CI / Workflow lint (actionlint) (pull_request) Successful in 3s
197c337bf2
Python 3.13 and later make a folder asked for with mode 0o700 (tempfile's
mkdtemp and TemporaryDirectory) with a protected access list naming SYSTEM,
the Administrators and the owner only. An AppContainer is let in only when
the list also names it, so in the sandbox Python made the folder and was then
refused in it: make_game.py's compiler check failed with "Access is denied"
on every Windows job (Longshot #910, nightly-52's probe lines).

The agent now writes a .pth file and a small module into its own builder
Python (tools/python): where the sandbox sets BEANSTALK_SANDBOX_PYTHON=1,
such folders are made with the plain mode and inherit the sandbox's grants.
Outside the sandbox nothing changes, and a Python the tester chose in the
setup is left alone.

Windows also points an AppContainer's TEMP at
LOCALAPPDATA\Packages\beanstalk.sandbox\AC\Temp, which was never made, so
Python fell back to the Longshot checkout. That folder is now made before
each start. The probe also tries a TemporaryDirectory and says whether the
fix was loaded.

Assisted-by: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Beanstalk!33
No description provided.