|
| 1 | +## Context |
| 2 | + |
| 3 | +The runtime architecture has converged on a universal `NodeRuntime` orchestrator that composes a capability-side `SystemDriver` with an execution-side `RuntimeDriverFactory`. During the boundary refactor, browser execution was intentionally disabled and `BrowserSandbox` was left as a throwing compatibility surface. |
| 4 | + |
| 5 | +The next step is to restore browser execution without reintroducing split APIs or duplicated orchestration logic. The design target is one runtime API (`NodeRuntime`) with driver-only variation, plus reusable integration tests that assert runtime-contract behavior across `node` and `browser` execution targets. |
| 6 | + |
| 7 | +## Goals / Non-Goals |
| 8 | + |
| 9 | +**Goals:** |
| 10 | +- Restore browser execution using the existing `NodeRuntime` + runtime-driver architecture. |
| 11 | +- Remove `BrowserSandbox` and keep one universal runtime API. |
| 12 | +- Keep capability ownership in `SystemDriver` and move browser execution heavy lifting into a browser runtime driver implementation. |
| 13 | +- Enforce deterministic validation for Node-only options when browser runtime is selected. |
| 14 | +- Introduce reusable integration suites (`run*` functions) that can be executed with a shared `TestContext` class for both node and browser targets. |
| 15 | + |
| 16 | +**Non-Goals:** |
| 17 | +- Backward compatibility for `BrowserSandbox` or legacy constructor aliases. |
| 18 | +- Adding new capability domains beyond existing fs/network/child-process/env/runtime behavior. |
| 19 | +- Full cross-browser matrix expansion in this change (single supported browser engine is sufficient initially). |
| 20 | + |
| 21 | +## Decisions |
| 22 | + |
| 23 | +### 1. Keep `NodeRuntime` as the only execution API |
| 24 | + |
| 25 | +- **Choice:** Browser execution is restored by adding a browser `RuntimeDriverFactory`, not by reintroducing a separate browser runtime class. |
| 26 | +- **Rationale:** This preserves the architecture goal that only the driver changes across targets. |
| 27 | +- **Alternative considered:** Keep `BrowserSandbox` as a wrapper around `NodeRuntime`; rejected because it keeps parallel public runtime surfaces and API drift risk. |
| 28 | + |
| 29 | +### 2. Move browser runtime heavy lifting into browser runtime driver |
| 30 | + |
| 31 | +- **Choice:** Implement browser execution lifecycle in browser runtime-driver code (worker spin-up/teardown, message protocol, runtime marshalling, and browser bridge shims). |
| 32 | +- **Rationale:** Mirrors Node driver ownership and keeps `NodeRuntime` focused on orchestration. |
| 33 | +- **Alternative considered:** Put browser worker orchestration directly in `NodeRuntime`; rejected because it breaks driver boundary consistency. |
| 34 | + |
| 35 | +### 3. Keep `createBrowserDriver` as capability-side `SystemDriver` factory |
| 36 | + |
| 37 | +- **Choice:** `createBrowserDriver` returns a `SystemDriver` (OPFS/in-memory filesystem, browser fetch-backed network adapter, permission wrappers) and pairs with a separate browser runtime-driver factory. |
| 38 | +- **Rationale:** Capability composition remains symmetric across node and browser targets. |
| 39 | +- **Alternative considered:** Make browser runtime-driver implicitly create capability adapters; rejected because it conflates capability and execution layers. |
| 40 | + |
| 41 | +### 4. Reject Node-only execution options for browser target |
| 42 | + |
| 43 | +- **Choice:** Browser runtime-driver factory performs explicit validation and rejects `memoryLimit`, `cpuTimeLimitMs`, `timingMitigation`, and payload-limit overrides. |
| 44 | +- **Rationale:** These options are Node/isolated-vm-specific and silently ignoring them would create unsafe ambiguity. |
| 45 | +- **Alternative considered:** Ignore unsupported fields for browser; rejected because misconfiguration would pass silently. |
| 46 | + |
| 47 | +### 5. Shared integration harness with class-based context |
| 48 | + |
| 49 | +- **Choice:** Add `tests/integration/` suites exporting `runX(ctx: TestContext)` functions, and one orchestrator test that loops targets (`node`, `browser`) and executes all shared suites under `describe` blocks. |
| 50 | +- **Rationale:** Reuses existing runtime-contract tests while preventing node/browser drift. |
| 51 | +- **Alternative considered:** Duplicate separate node and browser spec files; rejected due to maintenance overhead and parity gaps. |
| 52 | + |
| 53 | +### 6. Run browser integration through a browser test runner profile |
| 54 | + |
| 55 | +- **Choice:** Add a dedicated browser integration runner configuration and script that executes the same suite in a real browser engine. |
| 56 | +- **Rationale:** Browser runtime behavior (Worker/OPFS/fetch semantics) needs real-browser validation. |
| 57 | +- **Alternative considered:** Node-only emulation for browser tests; rejected because it cannot validate critical browser runtime primitives. |
| 58 | + |
| 59 | +## Risks / Trade-offs |
| 60 | + |
| 61 | +- [Browser runtime contract mismatch with Node semantics] -> Mitigation: keep shared `run*` integration suites as parity gates and document intentional deviations in friction docs. |
| 62 | +- [Browser tests are slower/flakier than node tests] -> Mitigation: keep browser suite targeted and under one minute, run full matrix only in dedicated CI lanes. |
| 63 | +- [Removal of `BrowserSandbox` breaks existing consumers] -> Mitigation: treat as explicit breaking change with updated docs/examples in the same change. |
| 64 | +- [Capability gaps in browser adapters (for example DNS/HTTP2)] -> Mitigation: keep deterministic `ENOSYS` contracts and spec coverage for unsupported operations. |
| 65 | + |
| 66 | +## Migration Plan |
| 67 | + |
| 68 | +1. Implement browser runtime driver factory and runtime-driver class that satisfy `RuntimeDriver` contract. |
| 69 | +2. Re-enable `createBrowserDriver` capability factory and remove/retire `BrowserSandbox` exports. |
| 70 | +3. Add browser target wiring in test utilities via a class-based `TestContext` abstraction. |
| 71 | +4. Migrate selected existing runtime-contract tests into reusable `tests/integration/run*.ts` suites. |
| 72 | +5. Add node and browser integration entrypoints that execute the same `run*` suites by target. |
| 73 | +6. Update docs/specs/friction notes and run targeted typecheck/test commands for node and browser profiles. |
| 74 | + |
| 75 | +## Open Questions |
| 76 | + |
| 77 | +- Which single browser engine should be the default CI gate in phase one (Chromium recommended)? |
| 78 | +- Should unsupported browser options fail at `NodeRuntime` construction time or runtime-driver factory creation time (factory-time preferred for clearer ownership)? |
0 commit comments