Skip to content

feat(compiler): reject Strong writes to DOM the template renders - #1491

Closed
trueadm wants to merge 7 commits into
mainfrom
feat/strong-dom-writes
Closed

trueadm wants to merge 7 commits into
mainfrom
feat/strong-dom-writes

Conversation

@trueadm

@trueadm trueadm commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Strong mode now rejects imperative writes, through a ref, to DOM that the template renders (issue #1027, item 1). The checks apply only to Strong modules; compatibility mode and emitted code are unchanged.

  • OCTANE_STRONG_MANAGED_DOM_WRITE reports a write to anything the template owns on the element:
    • textContent, innerText, append, appendChild, insertBefore, prepend, removeChild, replaceChild, or replaceChildren on an element with rendered children;
    • className, classList mutators, or classList.value when the template sets a class;
    • setAttribute, removeAttribute, or toggleAttribute for an attribute the template sets, using Octane's own prop-to-attribute table;
    • style, style.cssText, or a style property the template sets.
  • OCTANE_STRONG_RAW_HTML_WRITE reports innerHTML, outerHTML, insertAdjacentHTML(), or setHTMLUnsafe() on a rendered element, and names dangerouslySetInnerHTML={trustHTML(html)} as the replacement.

Why

A probe on main @ 85cb387 found that all three of these compiled under Strong:

  • r.current.textContent = label.toUpperCase() on <p ref={r}>{label}</p>
  • r.current.className = … on <p ref={r} className="base" />
  • r.current.innerHTML = html

The write overwrites or corrupts nodes that Octane reconciles. The server HTML never contains the result either, because effects don't run there.

Changes

  • New packages/octane/src/compiler/strong-dom.js, a standalone pass that reuses Strong's existing hook-scope analysis. strong-mode.js gains one import and one call.

  • A write is reported only when the ref provably names exactly one intrinsic DOM element in the same component, or the same keyed @for row.

  • The ref may be used only in these ways, or the proof is withdrawn:

    • as that element's ref prop, including inside a ref={[a, b]} list;
    • through .current reads and const { current } = ref;
    • through unreassigned local aliases.

    A component ref prop, a helper argument, a .current write, or a second element all withdraw it. An inline callback ref's parameter counts as that element.

  • Writes to DOM the template doesn't own stay valid: textContent on an element without rendered children, mounting a widget into an empty container, a style property the template's style doesn't set, focus(), and measurement.

  • New packages/octane/tests/compiler/strong-dom.test.ts covers:

    • aliases, shadowed names, and namespace and aliased imports;
    • optional chains, computed literal keys, and layout effects, event handlers, and callback refs;
    • .tsrx, including refs declared in @for rows and the {ref} attribute shorthand;
    • plain .ts modules;
    • Volar parity, every compile mode, and pinned unchanged client and server output.
  • Docs: a "DOM ownership" section in docs/strong-compiler-checks.md, plus tsrx-basics.md, differences-from-react.md, llms.txt, and build-tools.mdx. Changeset added.

Validation

  • pnpm format:files:check on the diff

  • pnpm typecheck (full run passed on the combined local work), plus pnpm typecheck:files on this diff

  • pnpm test: a full local run with a 12 GB heap passed 82,876 tests with 2 failures. Both pass in isolation: one is a load-sensitive browser timeout, the other a pristine upstream Base UI test. The machine was heavily loaded, and with the default heap the root Vitest process ran out of memory. CI is the authority here.

  • Targeted: all strong-* compiler suites plus volar.test.ts, 1,275 tests passing.

  • Breaking each rule on purpose (no escape tracking, ignoring the component/row boundary, assuming children, ignoring style keys) turns tests red.

  • Compiled 3,479 real repo files under Strong. Both new write diagnostics are true positives:

    • packages/react-map-gl: appendChild into a <div> that renders an @if block;
    • packages/xyflow Viewport: style.transform where the template's style also sets transform.

    Both bindings stay in compatibility mode, so nothing breaks.

  • Throughput: the pass is gated per top-level statement and walks 550 of 40,517 statements in that corpus. With the companion render checks, the Strong analyzer is +4.8% on real code, about +1% of a whole Strong compile. The paired benchmarks/strong-compiler-checks audit stays within noise.

Risk / follow-ups

  • Needs a maintainer decision: xyflow's Viewport seeds transform in the template for first paint and SSR, then writes it imperatively every frame. Per the brief this is rejected under Strong, and there is no Strong-clean equivalent today.
  • PR feat(compiler): close Strong render determinism gaps #1489 (render determinism) is independent of this one and merges cleanly alongside it. A follow-up PR adds OCTANE_STRONG_OWN_MARKUP_QUERY on top of this one.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


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-only compile-time diagnostics can break builds for apps that imperatively mutate template-owned DOM via refs; runtime and compatibility mode are unchanged.

Overview
Strong mode now errors when imperative ref writes touch DOM that the template owns, without changing compatibility mode or emitted code.

A new compiler pass (analyzeStrongDOM, wired from strong-mode.js) tracks refs that provably name a single intrinsic element in the component or @for row, then flags OCTANE_STRONG_MANAGED_DOM_WRITE for child-list, class, attribute, and style mutations the template already controls, and OCTANE_STRONG_RAW_HTML_WRITE for innerHTML / adjacent HTML APIs (with guidance toward dangerouslySetInnerHTML={trustHTML(...)}). Ref proof is withdrawn if the binding escapes (component ref prop, reassignment, multiple elements, etc.). Tests cover .ts / .tsrx, Volar, and pinned output; docs add a DOM-ownership section to the Strong checks reference.

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

When a useRef provably names exactly one intrinsic element in the same
component (or keyed @for row), Strong mode now rejects imperative writes to
what the template owns on that element:

- OCTANE_STRONG_MANAGED_DOM_WRITE: textContent, innerText, or the child-list
  methods on an element with rendered children; className, classList mutators,
  or classList.value when the template sets a class; setAttribute,
  removeAttribute, or toggleAttribute for an attribute the template sets; and
  style, style.cssText, or a style property the template sets.
- OCTANE_STRONG_RAW_HTML_WRITE: innerHTML, outerHTML, insertAdjacentHTML, or
  setHTMLUnsafe on a rendered element; the message names
  dangerouslySetInnerHTML={trustHTML(html)}.

The proof follows .current reads, `const { current } = ref`, unreassigned local
aliases, ref lists, and inline callback refs. Any other use of the ref (a
component ref prop, a helper argument, a .current write, a second element)
withdraws it. Writes to DOM the template does not own, such as mounting a widget
into an empty container, stay valid.

The analysis is a standalone pass in strong-dom.js that reuses Strong's hook
scope analysis and is skipped for top-level statements without a ref prop and
a DOM write spelling. Diagnostics only: compatibility mode and emitted code are
unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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 Oct 1, 2026
@github-actions
github-actions Bot marked this pull request as draft October 1, 2026 08:38

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Autofix Details

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Empty hosts treated as having children
    • Added an explicit child.value !== '' check in rendersChild so that empty strings left by normalizeAuthoredJsxLiterals (collapsed from whitespace-only text with linebreaks) are correctly treated as non-rendering, preventing false OCTANE_STRONG_MANAGED_DOM_WRITE diagnostics on multiline empty hosts.

Create PR

Or push these changes by commenting:

@cursor push 620ffd0ffb
Preview (620ffd0ffb)
diff --git a/packages/octane/src/compiler/strong-dom.js b/packages/octane/src/compiler/strong-dom.js
--- a/packages/octane/src/compiler/strong-dom.js
+++ b/packages/octane/src/compiler/strong-dom.js
@@ -154,9 +154,12 @@
 	return keys;
 }
 
-// JSX drops whitespace-only text that contains a line break.
+// JSX drops whitespace-only text that contains a line break. After
+// normalizeAuthoredJsxLiterals those nodes are collapsed to '' but left in
+// children, so an empty value is never rendered content.
 function rendersChild(child) {
-	if (child.type === 'JSXText') return !/^\s*$/.test(child.value) || !/[\r\n]/.test(child.value);
+	if (child.type === 'JSXText')
+		return child.value !== '' && (!/^\s*$/.test(child.value) || !/[\r\n]/.test(child.value));
 	if (child.type === 'JSXExpressionContainer')
 		return child.expression?.type !== 'JSXEmptyExpression';
 	return true;

diff --git a/packages/octane/tests/compiler/strong-dom.test.ts b/packages/octane/tests/compiler/strong-dom.test.ts
--- a/packages/octane/tests/compiler/strong-dom.test.ts
+++ b/packages/octane/tests/compiler/strong-dom.test.ts
@@ -313,6 +313,16 @@
 			'<span ref={r} />',
 		],
 		[
+			'textContent on a multiline empty host',
+			'r.current.textContent = label;',
+			'<span ref={r}>\n</span>',
+		],
+		[
+			'mounting into a multiline empty container',
+			"r.current.appendChild(document.createElement('canvas'));",
+			'<div ref={r}>\n</div>',
+		],
+		[
 			'mounting into an empty container',
 			"r.current.appendChild(document.createElement('canvas'));",
 			'<div ref={r} class="chart" />',

You can send follow-ups to the cloud agent here.

Literal normalization rewrites text removed by JSX whitespace rules to '', which
renders nothing. rendersChild now honours that, so an empty host never counts
as owning children. Today's parsers drop line-break-only text before
normalization, so this guards the contract rather than a reachable case; the
new fixture pins a formatted empty container as a valid mount target.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
trueadm added a commit that referenced this pull request Oct 1, 2026
Brings in #1497 (Strong state values, updaters, and write-only state) and
resolves the same conflicts as #1489 in strong-mode.js and the Strong docs,
keeping the DOM ownership and query additions from #1491 and #1492.

The workaround ledger records that #1497 now rejects five workarounds that
previously compiled and kept the bug: both force-update click tallies,
setN(n + 1) after await, mutate-then-set, and the mutating updater
(27 → 22 compiler gaps). Prompts, references, and the recorded alternative are
unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trueadm
trueadm marked this pull request as ready for review October 1, 2026 12:04

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Style children treated as rendered DOM
    • Added a JSXStyleElement check returning false in the rendersChild function, consistent with every other child-content check in the codebase, so scoped style blocks no longer make element.children true and valid DOM writes into style-only containers are allowed.

Create PR

Or push these changes by commenting:

@cursor push 119f9c9f1c
Preview (119f9c9f1c)
diff --git a/packages/octane/src/compiler/strong-dom.js b/packages/octane/src/compiler/strong-dom.js
--- a/packages/octane/src/compiler/strong-dom.js
+++ b/packages/octane/src/compiler/strong-dom.js
@@ -162,6 +162,7 @@
 	}
 	if (child.type === 'JSXExpressionContainer')
 		return child.expression?.type !== 'JSXEmptyExpression';
+	if (child.type === 'JSXStyleElement') return false;
 	return true;
 }
 

diff --git a/packages/octane/tests/compiler/strong-dom.test.ts b/packages/octane/tests/compiler/strong-dom.test.ts
--- a/packages/octane/tests/compiler/strong-dom.test.ts
+++ b/packages/octane/tests/compiler/strong-dom.test.ts
@@ -399,6 +399,19 @@
 		expectStrongValid(source, '/src/App.tsx');
 	});
 
+	it('allows mounting into a container whose only children are scoped style blocks', () => {
+		expectStrongValid(
+			`${IMPORTS}export function Chart({ data }) @{
+  const container = useRef(null);
+  useEffect(() => { container.current.appendChild(document.createElement('canvas')); });
+  <div ref={container} class="chart">
+    <style>.chart { display: block; }</style>
+  </div>
+}`,
+			'/src/Chart.tsrx',
+		);
+	});
+
 	it('does not prove a ref declared outside the @for row that attaches it', () => {
 		expectStrongValid(
 			`${IMPORTS}export function List({ items }) @{

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit f001359. Configure here.

A TSRX sibling-scoped <style> block is extracted from the template rather than
rendered into its host, so a host whose only child is a style block is empty.
rendersChild treated the JSXStyleElement as rendered content, and Strong
rejected valid widget mounts such as appendChild into that host. Hosts with a
style block and real rendered children are still checked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

No benchmark report for this commit yet: PR bench is still running or skipped this change.

CI job durations

  • 🟡 7 job(s) slower than every recent main run: heavy integration (package builds, eval corpus, runtime loaders, Astro), heavy integration (browser - chromium), React parity shard (3/4), React parity shard (1/4), example apps (3/3), typecheck checks, validate publishable packages

Compares the CI run for e43047660 (run) with the last 5 successful main push runs. A job is only slower or faster outside the whole main range, by at least 10% and 15s.

job main median main range this PR Δ
wall clock (first job start to last job end) 17m 05s 12m 35s–23m 15s 56m 09s +39m 04s (+229%) 🟡 slower
total job time 184m 21s 144m 08s–242m 49s 254m 36s +70m 15s (+38%) 🟡 slower
heavy integration (package builds, eval corpus, runtime loaders, Astro) 5m 28s 4m 36s–16m 52s 30m 02s +24m 34s (+449%) 🟡 slower
heavy integration (browser - chromium) 12m 52s 11m 12s–19m 08s 21m 59s +9m 07s (+71%) 🟡 slower
React parity shard (3/4) 13m 10s 10m 59s–18m 01s 20m 02s +6m 52s (+52%) 🟡 slower
React parity shard (1/4) 14m 25s 10m 05s–18m 11s 21m 03s +6m 38s (+46%) 🟡 slower
example apps (3/3) 3m 49s 3m 34s–5m 33s 8m 12s +4m 23s (+115%) 🟡 slower
typecheck checks 9m 23s 7m 20s–11m 01s 13m 36s +4m 13s (+45%) 🟡 slower
validate publishable packages 4m 32s 3m 45s–5m 20s 8m 23s +3m 51s (+85%) 🟡 slower
All 26 timed jobs
job main median main range this PR Δ
heavy integration (package builds, eval corpus, runtime loaders, Astro) 5m 28s 4m 36s–16m 52s 30m 02s +24m 34s (+449%) 🟡 slower
heavy integration (browser - chromium) 12m 52s 11m 12s–19m 08s 21m 59s +9m 07s (+71%) 🟡 slower
React parity shard (2/4) 10m 50s 9m 32s–19m 40s 19m 22s +8m 32s (+79%) ⚪ within noise
React parity shard (3/4) 13m 10s 10m 59s–18m 01s 20m 02s +6m 52s (+52%) 🟡 slower
React parity shard (1/4) 14m 25s 10m 05s–18m 11s 21m 03s +6m 38s (+46%) 🟡 slower
React parity shard (4/4) 13m 38s 11m 39s–22m 17s 19m 48s +6m 10s (+45%) ⚪ within noise
example apps (2/3) 4m 15s 3m 26s–9m 58s 9m 14s +4m 59s (+117%) ⚪ within noise
example apps (3/3) 3m 49s 3m 34s–5m 33s 8m 12s +4m 23s (+115%) 🟡 slower
typecheck checks 9m 23s 7m 20s–11m 01s 13m 36s +4m 13s (+45%) 🟡 slower
validate publishable packages 4m 32s 3m 45s–5m 20s 8m 23s +3m 51s (+85%) 🟡 slower
website integration suite 7m 24s 5m 45s–11m 41s 10m 32s +3m 08s (+42%) ⚪ within noise
test shard (Node 24, 4/4) 12m 54s 10m 24s–16m 16s 14m 23s +1m 29s (+11%) ⚪ within noise
lint checks 8m 49s 7m 43s–12m 27s 10m 06s +1m 17s (+15%) ⚪ within noise
test shard (Node 24, 1/4) 14m 01s 11m 10s–17m 32s 15m 10s +1m 09s (+8%) ⚪ within noise
Lynx compatibility 2m 00s 1m 34s–2m 55s 2m 27s +27s (+23%) ⚪ within noise
example apps (1/3) 3m 36s 3m 07s–9m 27s 3m 38s +2s (+1%) ⚪ within noise
classify changeset release 16s 14s–21s 17s +1s (+6%) ⚪ within noise
typecheck 4s 3s–4s 5s +1s (+25%) ⚪ within noise
test (24) 4s 3s–5s 5s +1s (+25%) ⚪ within noise
CI provenance 4s 3s–4s 5s +1s (+25%) ⚪ within noise
lint 4s 4s–5s 4s ±0s (±0%) ⚪ within noise
examples 4s 4s–4s 4s ±0s (±0%) ⚪ within noise
React parity checks 15s 14s–16s 15s ±0s (±0%) ⚪ within noise
Three compatibility 3m 28s 2m 51s–4m 23s 3m 08s −20s (−10%) ⚪ within noise
test shard (Node 24, 3/4) 11m 56s 9m 44s–15m 12s 10m 48s −1m 08s (−9%) ⚪ within noise
test shard (Node 24, 2/4) 13m 32s 10m 24s–15m 53s 11m 48s −1m 44s (−13%) ⚪ within noise

…trong-dom-writes

# Conflicts:
#	docs/differences-from-react.md
#	docs/strong-compiler-checks.md
#	docs/tsrx-basics.md
#	packages/octane/src/compiler/strong-dom.js
#	packages/octane/tests/compiler/strong-dom.test.ts
#	website/public/llms.txt
#	website/src/content/docs/build-tools.mdx
@trueadm

trueadm commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as superseded: #1492 was stacked on this PR and merged with all of its commits (b6a414a). After merging main, this branch has no changes of its own.

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