diff --git a/apps/web/src/app/playground/PlaygroundClient.tsx b/apps/web/src/app/playground/PlaygroundClient.tsx index 2d81253..8de78cb 100644 --- a/apps/web/src/app/playground/PlaygroundClient.tsx +++ b/apps/web/src/app/playground/PlaygroundClient.tsx @@ -448,6 +448,7 @@ function ResultPanel({ } if (!result.ok) { + const sandboxBlocked = result.error === "sandbox_unavailable"; return (
{result.error} {result.message ? ` — ${result.message}` : ""} + {sandboxBlocked ? ( +
+ The Piston sandbox isn't reachable. On Docker Desktop for Windows this is a known + limitation (cgroup v2 isn't writable inside the container).{" "} + + Workarounds in docs/operations/SANDBOX.md → + +
+ ) : null}
); } diff --git a/apps/web/src/app/session/SessionClient.tsx b/apps/web/src/app/session/SessionClient.tsx index f348358..83ecc00 100644 --- a/apps/web/src/app/session/SessionClient.tsx +++ b/apps/web/src/app/session/SessionClient.tsx @@ -820,6 +820,7 @@ function InlineErrorBanner({ function RunResultPanel({ result }: { result: RunSandboxResult }) { if (!result.ok) { + const sandboxBlocked = result.error === "sandbox_unavailable"; return (
{result.error} {result.message ? ` — ${result.message}` : ""} + {sandboxBlocked ? ( +
+ The Piston sandbox isn't reachable. On Docker Desktop for Windows this is a known + limitation (cgroup v2 isn't writable inside the container).{" "} + + Workarounds in docs/operations/SANDBOX.md → + +
+ ) : null}
); } diff --git a/docs/operations/SANDBOX.md b/docs/operations/SANDBOX.md new file mode 100644 index 0000000..5d93512 --- /dev/null +++ b/docs/operations/SANDBOX.md @@ -0,0 +1,126 @@ +# Code execution sandbox (Piston) — operator guide + +LearnPro uses [Piston](https://github.com/engineer-man/piston) as the sandbox that runs +user-submitted Python and TypeScript. Inside its container Piston wires per-execution +isolation via cgroup v2 (`/sys/fs/cgroup/isolate/...`). + +This works fine on Linux Docker and macOS Docker. **It does NOT work on Docker Desktop +for Windows** — the WSL2 backend behind Docker Desktop doesn't expose a writable cgroup +v2 mount, so Piston's entrypoint hits + +``` +mkdir: cannot create directory 'isolate/': Read-only file system +``` + +…and the container goes into a restart loop. Every `Run` / `Submit` then surfaces + +``` +Run failed +sandbox_unavailable — Piston execute failed +``` + +This page is the operator guide for unblocking it. Tracked as +[STORY-065](../../project/stories/STORY-065-piston-windows-docker-desktop.md). + +--- + +## Option A — WSL-native Docker engine (recommended for Windows self-hosters) + +The fix that keeps the "your data stays on your machine" promise. ~30 min. + +1. Install a real Ubuntu (or Debian / Arch) WSL distro: + ```powershell + wsl --install -d Ubuntu-24.04 + ``` + Reboot if WSL was freshly enabled. Sign in to the distro once, set a user. + +2. Inside that distro, install the docker engine (NOT Docker Desktop): + ```bash + curl -fsSL https://get.docker.com | sh + sudo usermod -aG docker $USER + newgrp docker # or log out + back in + ``` + +3. Confirm cgroup v2 is mounted RW (this is the bit Docker Desktop's distro doesn't give us): + ```bash + mount | grep cgroup2 + # Expect: cgroup2 on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,relatime) + ``` + +4. Run the LearnPro docker compose stack from inside the WSL distro: + ```bash + cd /mnt/d/DEV/ClaudeProjects/LearnPro # or wherever your checkout is + docker compose -f infra/docker/docker-compose.dev.yaml \ + -f infra/docker/docker-compose.dev.override.yaml up -d + docker logs -f learnpro-piston # should NOT say "Read-only file system" + curl -sSf http://localhost:2000/api/v2/runtimes | head + ``` + +5. Run `bash scripts/dev.sh` from either Git Bash on Windows OR the WSL distro + itself — `PISTON_URL=http://localhost:2000` already points at the right port. + +Why this works: docker-engine running inside a real Linux WSL distro hands containers +the kernel's actual cgroup v2 hierarchy, which is writable. Docker Desktop's internal +distro is intentionally locked down. + +--- + +## Option B — point at a remote Piston instance + +If you have a Linux VM somewhere (a $5/month DigitalOcean droplet works), run Piston +there and point `PISTON_URL` at it. Your code leaves the local machine — the privacy +story is weaker — but no Windows-specific setup. + +On the VM: +```bash +docker run -d --name piston -p 2000:2000 ghcr.io/engineer-man/piston +``` + +On your dev box (`.env`): +```env +PISTON_URL=https://piston.your-vm.example.com +``` + +…then `bash scripts/dev.sh --down && bash scripts/dev.sh` to pick up the new URL. + +You'll want HTTPS in front of the VM (Caddy or a Cloudflare tunnel work) and IP-allowlist +the Piston port so randoms can't run code through your instance. + +--- + +## Option C — public Piston API (no longer viable) + +Before 2026-02-15 you could point `PISTON_URL` at `https://emkc.org/api/v2/piston` and +get free public execution. As of 2026-02-15 that API is whitelist-only — you have to +contact EngineerMan on Discord with a use-case justification. Not a path for casual +self-hosters anymore. + +--- + +## Option D — skip Run/Submit, use the rest of the app + +If you're poking at LearnPro for design/UX feedback and don't need real code execution, +everything else works on Docker Desktop Windows: + +- Auth, onboarding (deterministic fallback) +- Dashboard, /recommended, all settings pages +- Session page renders real problems with Monaco editor +- Paste-detect modal, "I got help" toggle +- /playground UI shell (just Run / Submit are blocked) + +The dev script already prints a warning when Piston isn't healthy, so this mode is the +default if you don't act on it. + +--- + +## Why isn't there a default fallback to a different sandbox? + +The `SandboxProvider` interface in `packages/sandbox/src/` is pluggable, but every +production-grade alternative we evaluated (judge0, nsjail) has the same cgroup-v2 +dependency on Docker Desktop Windows. The real fix is "don't run the sandbox under +Docker Desktop on Windows" — Options A and B both honor that. + +A lighter alternative — an in-process Pyodide for Python-only execution — would +unblock /playground but break feature parity with the curated TypeScript track and +the integration tests that assume Piston-compatible semantics. Filed as a v2 +consideration in the STORY-065 activity log; not on the v1 roadmap. diff --git a/project/stories/STORY-065-piston-windows-docker-desktop.md b/project/stories/STORY-065-piston-windows-docker-desktop.md index 4b6d89e..8a83a0f 100644 --- a/project/stories/STORY-065-piston-windows-docker-desktop.md +++ b/project/stories/STORY-065-piston-windows-docker-desktop.md @@ -40,3 +40,30 @@ Tested 2026-05-11 with Docker Desktop 29.3.0 on Windows 11 + WSL2 backend. Pisto ## Activity log - 2026-05-11 — created. Found during /option 1/ Chrome walkthrough. +- 2026-05-18 — user hit the bug again from the UI on a real Run click. Three updates + this round: + - **Verified the failure mode end-to-end** at the request level: API logs + `SandboxRequestError: Piston execute failed: fetch failed: ECONNREFUSED`, + Piston container is in `Restarting (1) Less than a second ago` with the + `mkdir 'isolate/': Read-only file system` loop. UI surfaces + `sandbox_unavailable — Piston execute failed`. + - **Public Piston API ruled out as a default fallback.** emkc.org/api/v2/piston + went whitelist-only on 2026-02-15: `"Public Piston API is now whitelist only as of + 2/15/2026. Please contact EngineerMan on Discord with use case justification or + consider hosting your own Piston instance."` So `PISTON_URL=https://emkc.org/...` + isn't a one-line fix anymore. Affects any documentation that suggested it. + - **Shipped two user-facing improvements** (this PR): + - `RunResultPanel` (both /playground and /session) now appends a coach-voice + explanation + link to `docs/operations/SANDBOX.md` when `result.error === + "sandbox_unavailable"`. Previously the UI just showed + `sandbox_unavailable — Piston execute failed` with no path forward. + - New `docs/operations/SANDBOX.md` with the four real fix paths: + (A) WSL-native docker engine inside a user Ubuntu distro — recommended, + preserves self-host promise; + (B) remote Piston on a Linux VM via `PISTON_URL`; + (C) public Piston — ruled out per above; + (D) skip Run/Submit and use the rest of the app. + - **Remaining AC unchanged**: a default sandbox backend that works on Docker + Desktop Windows is still open. Pluggable `SandboxProvider` interface is in place; + Pyodide-for-Python is the most likely v2 candidate but breaks parity with the + TypeScript track. diff --git a/scripts/dev.ps1 b/scripts/dev.ps1 index c139d2d..9234101 100644 --- a/scripts/dev.ps1 +++ b/scripts/dev.ps1 @@ -16,6 +16,9 @@ # <3>WSL (xxx - Relay) ERROR: CreateProcessCommon:800: execvpe(/bin/bash) failed: # No such file or directory # This wrapper bypasses that by calling Git for Windows' bash.exe directly. +# - PowerShell's Ctrl+C handling can kill the bash subprocess abruptly without giving +# bash a chance to fire its trap. We register a CancelKeyPress + finally block that +# calls `dev.sh --down` so containers + dev servers always get torn down cleanly. $ErrorActionPreference = "Stop" @@ -67,13 +70,41 @@ if (-not (Test-Path $bashScript -PathType Leaf)) { exit 1 } -# Translate the Windows-style path to a form Git Bash accepts. Git Bash handles both -# "D:/...\path" and Unix-style "/d/..." paths; we use forward-slashed Windows so users -# see a familiar location in any error messages. $bashScriptUnix = $bashScript -replace '\\', '/' -# Run the bash script with all of our args passed through. We deliberately avoid --login -# (skip the user's .bash_profile which may pollute the env) and -i (skip interactive -# rc loading which slows startup). The script itself manages its own environment. -& $bashExe $bashScriptUnix @args -exit $LASTEXITCODE +# If the user passed --down explicitly, just run it and exit — no cleanup-on-cleanup logic. +$isDownOnly = $args -contains '--down' + +# Register a CancelKeyPress handler so Ctrl+C in PowerShell triggers a graceful teardown +# instead of killing bash mid-flight. The handler: +# 1. Sets $script:cancelled = $true so the main flow knows to skip the post-run noise. +# 2. Cancels the default termination so PowerShell stays alive long enough to run the +# finally block — that's where the actual `dev.sh --down` invocation happens. +$script:cancelled = $false +$cancelHandler = [System.ConsoleCancelEventHandler] { + param($sender, $e) + $e.Cancel = $true # don't let .NET kill us before finally runs + $script:cancelled = $true + Write-Host "" + Write-Host "Ctrl+C received — tearing down…" -ForegroundColor Yellow +} +[System.Console]::add_CancelKeyPress($cancelHandler) + +try { + & $bashExe $bashScriptUnix @args + $rc = $LASTEXITCODE +} +finally { + [System.Console]::remove_CancelKeyPress($cancelHandler) + + # If Ctrl+C fired (or the bash side died without its own trap firing — e.g. Windows + # SIGKILL semantics on a non-graceful kill), run the explicit teardown. Skip the + # teardown when the user invoked --down themselves; that's already the teardown. + if ($script:cancelled -and -not $isDownOnly) { + Write-Host "Running dev.sh --down to clean up containers + dev servers…" -ForegroundColor Cyan + & $bashExe $bashScriptUnix --down + } +} + +if ($script:cancelled) { exit 130 } # 130 = killed by SIGINT, standard convention +exit $rc diff --git a/scripts/dev.sh b/scripts/dev.sh index 425e02d..5be76a9 100644 --- a/scripts/dev.sh +++ b/scripts/dev.sh @@ -133,12 +133,32 @@ esac open_url() { local url="$1" + local opened=0 if [ "$IS_WINDOWS" = "1" ]; then - cmd.exe /c "start \"\" $url" >/dev/null 2>&1 || true + # Try multiple openers in order — different bash flavors (Git Bash vs WSL) and different + # invocation contexts (terminal vs piped through PowerShell) behave inconsistently with + # cmd.exe; the redundancy is intentional. + if command -v explorer.exe >/dev/null 2>&1 && explorer.exe "$url" >/dev/null 2>&1; then + opened=1 + elif command -v rundll32.exe >/dev/null 2>&1 \ + && rundll32.exe url.dll,FileProtocolHandler "$url" >/dev/null 2>&1; then + opened=1 + elif command -v powershell.exe >/dev/null 2>&1 \ + && powershell.exe -NoProfile -Command "Start-Process '$url'" >/dev/null 2>&1; then + opened=1 + elif command -v cmd.exe >/dev/null 2>&1 \ + && cmd.exe /c "start \"\" \"$url\"" >/dev/null 2>&1; then + opened=1 + fi elif [ "$(uname -s)" = "Darwin" ]; then - open "$url" >/dev/null 2>&1 || true + open "$url" >/dev/null 2>&1 && opened=1 else - (xdg-open "$url" >/dev/null 2>&1 &) || true + (xdg-open "$url" >/dev/null 2>&1 &) && opened=1 + fi + if [ "$opened" = "0" ]; then + # All openers failed (or none available). Print the URL so the user can click/copy it. + warn "Couldn't auto-open the browser. Open this URL yourself:" + warn " $url" fi }