Skip to content

feat: reject ambient browser reads in Strong renders - #1036

Merged
domgan-oai merged 1 commit into
octanejs:mainfrom
openai-oss-forks:codex/strong-render-ambient-read-1027
Sep 10, 2026
Merged

domgan-oai merged 1 commit into
octanejs:mainfrom
openai-oss-forks:codex/strong-render-ambient-read-1027

Conversation

@domgan-oai

@domgan-oai domgan-oai commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add OCTANE_STRONG_RENDER_AMBIENT_READ for render-time browser-global reads in Strong modules.
  • Preserve compatibility mode, shadowing, deferred work, subscribed snapshots, and lazy state initialization.
  • Add compiler/Volar regressions, public guidance, and patch changesets for octane and @octanejs/mcp-server.

Part of #1027 (ambient globals item).

Why

Reading live browser state during a cached render can produce stale output and break server rendering. The diagnostic points to useSyncExternalStore with a server snapshot, an effect, or a lazy initializer with server-rendering guards.

Changes

  • Recognize unshadowed window, document, localStorage, sessionStorage, navigator, location, and matchMedia, including typeof reads and known browser-handle aliases.
  • Check globalThis properties, destructuring, object/JSX spreads, JSX member tags, and compound/update reads. Permit the global object identity and known standard JavaScript language builtins.
  • Reuse the existing render phase and lazy-initializer exemption. Respect optional-chain and await evaluation order, local shadowing, and module constant declaration order.
  • Document subscribed viewport state and the bounded nature of alias analysis.

Validation

  • Full pnpm typecheck.
  • Compiler suite: pnpm exec vitest run packages/octane/tests/compiler --silent=passed-only — 55 files passed, 2,047 tests passed and 14 expected failures at the final source revision; six further regression cases then passed in the targeted run.
  • Final ambient diagnostic suite — 72/72 passed, including client/server emission parity and source-located Volar diagnostics.
  • Prettier check on all seven changed files; git diff --check.
  • pnpm sync — no additional generated changes.
  • Final paired compiler-throughput benchmark against afdc3618a: 100/1,000-component normal, legal ambient-callback, and module-alias workloads in Strong and compatibility modes. All 24 client/server output comparisons matched bytes and SHA256.

Compiler throughput

Node v26.4.0 on Apple M5 Max; one process alternated baseline/candidate order with 3 warmups and 17 pairs (100 components) or 11 pairs (1,000). Final candidate 90c024d3d, analyzer SHA256 a3eb2c64d86e26d93fe15715dc70de1b5168c6d2ca2144c9ca6a228ab63ff387.

Strong workload 100 components, baseline → candidate 1,000 components, baseline → candidate
Normal 38.02 → 38.78 ms (+2.0%) 408.49 → 416.89 ms (+2.1%)
Legal ambient callbacks 32.48 → 33.04 ms (+1.7%) 394.98 → 399.06 ms (+1.0%)
Module aliases 52.72 → 54.92 ms (+4.2%) 461.38 → 456.04 ms (−1.2%)

Paired median ratios were 0.972–1.036. Compatibility control medians ranged from −5.8% to +1.8%; sample tails were noisier than the observed deltas. No material throughput regression was established; small compiler overhead remains plausible. Generated runtime code was identical in the measured legal programs.

Local reproduction artifacts: /private/tmp/octane-ambient-paired.mjs and /private/tmp/octane-ambient-paired-final.json; command: node /private/tmp/octane-ambient-paired.mjs /private/tmp/octane-ambient-paired-final.json.

  • Current-head CI run 34502442267 passed on 90c024d3d: all test and React parity shards, browser and website integration, package validation, examples, lint, typecheck, and provenance. Cursor Bugbot passed with no findings. The PR is ready for review; maintainer approval is still required.

The full repository pnpm test and repository-wide format scan were not run locally; the compiler suite and changed-file formatting cover this change, and full relevant CI will be checked before handoff.

Risk / follow-ups

  • This intentionally adds Strong errors to previously accepted browser-state reads. Compatibility behavior and valid emitted client/server code are preserved.
  • This remains bounded static analysis: arbitrary imported code, deeper properties, returned/container values, and destructuring defaults with unknown inputs are not generally proven. Only known direct module const aliases are resolved independently of declaration order.
  • Standard global builtin access is not a purity guarantee for arbitrary calls through those objects; the existing separate clock/randomness detector remains bounded.
  • Lazy initializers still execute during SSR and must preserve matching initial output. The diagnostic does not make an unsafe initializer server-safe.

Provenance

  • An agent produced this diff (agent-authored)

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Medium Risk
Strong modules that previously read browser APIs during render will now fail at compile time; alias analysis is intentionally bounded and may miss or over-approximate some patterns.

Overview
Strong mode now fails compile when render code reads live browser globals, with a new OCTANE_STRONG_RENDER_AMBIENT_READ diagnostic.

The Strong compiler pass tracks unshadowed window, document, storage, navigator, location, matchMedia, and most globalThis property access (including typeof, aliases, destructuring, spreads, and JSX), while still allowing shadowed names, module-level scalar snapshots, the globalThis object itself, and standard language builtins. Lazy useState / useReducer initializers, useSyncExternalStore snapshot callbacks, and deferred work stay exempt.

Docs, MCP skill text, a changeset, and a large strong-mode.test.ts suite document useSyncExternalStore with a server snapshot and pin client/server emission parity plus Volar source locations.

Reviewed by Cursor Bugbot for commit 90c024d. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent feat PR: new feature or capability labels Sep 10, 2026
@domgan-oai
domgan-oai marked this pull request as ready for review September 10, 2026 16:29
@domgan-oai
domgan-oai merged commit 1298a69 into octanejs:main Sep 10, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent feat PR: new feature or capability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant