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 fromcommands/, calls the exported function with the JSON context andcall(name, value), and returns its result as JSON or throwsFailure.script,.timedOutor.stopped.callsanswers the command's calls to the app through aWKScriptMessageHandlerWithReply; #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/.mjsonly, symlinks resolved withrealpathbefore the containment check. Content-Security-Policy: default-src 'none'; script-src orgstar-commands:on every response;WKContentRuleListblocks every URL but the scheme; navigation to other schemes cancelled;fetch,XMLHttpRequest,WebSocket,EventSource,importScripts,indexedDB,caches,Worker,SharedWorkerdeleted from the worker's prototype chain before user code loads.- Timeout default 2 s, clamped to 30 s; timeout and cancel terminate the worker.
webViewWebContentProcessDidTerminatefails the run with.stopped; the next run orprepare()creates a new web view.prepare()warms it; it unloads after 5 minutes without a run orprepare().
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).