Command host: user commands in a sandboxed web worker !526

merged merged by cmc on 2026-11-27 08:01 UTC · krz/orgstar:issue-244-command-host into main

Discussion

cmc 2026-11-27 08:00 UTC

Phase 1 of user commands (#173): CommandHost in Sources/OrgApp/CommandHost.swift, no user-visible feature. Closes #244.

  • run(module:function:context:timeout:) imports a module from commands/, calls the exported function with the JSON context and call(name, value), and returns its result as JSON or throws Failure.script, .timedOut or .stopped.
  • calls answers the command's calls to the app through a WKScriptMessageHandlerWithReply; #245 plugs prompts and messages in there. Time spent in a call doesn't count against the timeout.
  • Each run gets a new worker, started ahead of time; it is ended after the run, so no global state or cached module carries over and edited files are read again.
  • Scheme orgstar-commands://host/: the page, its script and the worker come from the app; everything under /commands/ from the folder, .js/.mjs only, symlinks resolved with realpath before the containment check.
  • Content-Security-Policy: default-src 'none'; script-src orgstar-commands: on every response; WKContentRuleList blocks every URL but the scheme; navigation to other schemes cancelled; fetch, XMLHttpRequest, WebSocket, EventSource, importScripts, indexedDB, caches, Worker, SharedWorker deleted from the worker's prototype chain before user code loads.
  • Timeout default 2 s, clamped to 30 s; timeout and cancel terminate the worker. webViewWebContentProcessDidTerminate fails the run with .stopped; the next run or prepare() creates a new web view. prepare() warms it; it unloads after 5 minutes without a run or prepare().

Measurements (median; CommandHostTests.measures on the Mac, the same through the public API on the iPhone 17 simulator):

Mac (debug) iPhone 17 simulator
First start 57–187 ms (187 ms as the process's first web view) 0.62–0.76 s in a fresh process (2.35 s on the first run after install); 0.48 s after unload()
Warm round trip 3–5 ms 5–7 ms
1 MB each way 100–104 ms 90–94 ms

Tests (CommandHostTests): results, errors, missing function; no state shared between runs; calls to the app and their errors; a 1.5 s call under a 1 s timeout; an endless loop stopped at the timeout and the next run working on the same web view; cancel; imports of ../, /, file: and data: refused; symlinks out of commands/ (file and directory) refused, inside allowed; no connection to a local listener from fetch, XHR, WebSocket or import in a command, from a module worker with its globals intact (fetch, XHR, WebSocket, EventSource, import) or from the page; the rule list alone blocks a fetch that connects without it; content process killed with SIGKILL fails the run and the next run works; idle unload.

Checked by removing each layer: without the rule list, a fetch from an isolated content world still connects (CSP doesn't cover it); without CSP too, a worker with its globals opens WebSocket and EventSource connections.

swift test, both Xcode builds (no warnings in Sources/) and ORGSTAR_GATES=1 swift test -c release --filter GateTests pass (1 MB open 97 ms at load 8.6).

Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form