Skip to content

fix(studio): a file is a composition only when a browser would build an element carrying data-composition-id - #8831

Open
jrusso1020 wants to merge 5 commits into
mainfrom
fix/captured-page-never-a-composition
Open

jrusso1020 wants to merge 5 commits into
mainfrom
fix/captured-page-never-a-composition

Conversation

@jrusso1020

@jrusso1020 jrusso1020 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

What

  • Studio counts an HTML file as a composition only when the page, parsed the way a browser with scripting on parses it, has an element whose data-composition-id attribute has a value. A template's content counts. Before, the text data-composition-id= anywhere in the file was enough.
  • The comp preview builder judges the file's own text, not the stamped text the route hands it.
  • The project listing finds compositions on the build worker, not the request thread.
  • hyperframes capture rewrites the marker in a saved page to hf-captured-composition-key, text that cannot join with what follows it to spell the marker again.

Behaviour changes

  1. Tag-shaped text is no element. A file whose only marker is a tag written inside a script, style, title, textarea, xmp, noscript, iframe, noembed, noframes or plaintext element, inside a comment, or inside another attribute's value is not a composition. It drops out of the Compositions list and its /preview/comp/ build answers as for any non-composition file. This replaces the earlier version of this PR, which still counted a full tag written inside a script or style.
  2. Inline SVG is read as a browser reads it. A composition whose root holds or follows inline SVG, with or without a style, title or script inside it, is a composition. A div written inside an SVG style or script is an element to a browser (it ends the SVG), so it counts.
  3. An empty value is not a composition. <div data-composition-id=""> and <div data-composition-id> are refused. When a tag repeats the attribute, the first one decides, as in a browser.
  4. Also not compositions: the marker as part of a longer attribute name, on an end tag, in a tag the file never closes, or on a tag a browser drops (inside a frameset, for one).
  5. The listing no longer parses on the request thread. It hands the pages to the existing build worker, one page at a time, and waits. One scan has 30 seconds from the moment the worker starts it; a page still parsing then gets 5 seconds more before the worker is replaced. When pages are left unread, or the scan does not end, the answer carries the files but no composition list, and the server logs one line saying how many of how many HTML files were not read (or the error). Studio keeps the list it already has when none is sent, so open compositions are not dropped. The worker keeps what it learned, so the next listing goes on from there.
  6. No rule by folder name: a user's own composition under a folder called extracted/ still works.

Why

A page saved by capture is not a composition, and capture strips the marker so Studio leaves it alone. The strip ran once, and its replacement ended in the letter the marker starts with, so data-composition-idata-composition-id= came out still holding the marker. Studio's text check then listed the saved page and built a preview from it.

Why the parser changed: the first versions of this PR scanned tags by hand, and then read the page with linkedom. Both disagree with a browser on where script text, comments and end tags stop, and each disagreement needed its own guard. linkedom (htmlparser2 underneath) is not a spec parser: it reads <script/>, </script/>, <![CDATA[, --!>, a quoted > inside an end tag and an unclosed noscript differently from a browser. parse5 implements the HTML parsing algorithm, so none of those need a guard.

Fails without the fix: "answers another request while a first listing still parses a large page" fails on the listing as it was one commit earlier, where the 12 MB page was parsed on the request thread and the listing answered first.

Also fails without the fix: at the head before the parser change of this PR the check accepts the rows of "refuses a tag that is only text to a browser" from the script string through the first plaintext row, and the empty value; "returns null for a page whose only marker is text a browser does not read as a tag" and "does not list a page whose only marker is a tag written inside a script" fail. On main the old text check also accepts 11 of the 12 earlier refusal cases, and the old strip leaves the marker in 3 of the 5 capture cases.

Related work

No issue. Touches only isCompositionSource and its three callers (listing, comp preview builder, hf-id stamping) and the capture strip.

How

  • The marker-text check stays as a cheap first step: a file without the text is never parsed.
  • Then parse5 with scripting on, and a walk over the document, template content included.
  • Dependency: packages/studio-server gains "parse5": "8.0.1". That exact version is already in bun.lock (jsdom uses it), so the lockfile changes by that one line and nothing new is downloaded. The pin is exact because the limits below use parse5's Parser class and a Tokenizer subclass, which a minor release may change. The CLI bundle builds with the parser inside it.
  • Limits. parse5 has none of its own, and each of these was slow or memory-hungry without one. A page past a limit is not a composition:
    • nesting deeper than 512, which is where a browser stops nesting;
    • more than 4,096 attribute names on one tag, repeats counted;
    • more elements than the page has characters. Closing tags out of order makes the parser repeat open elements, and a 24 KB page could ask for 2.4 million.
    • The tree adapter does not keep sibling order. Order does not change which elements exist, and keeping it cost a search through all siblings for each node moved.
  • The listing posts the file names to the build worker as one job. The worker reads and judges each page and stops judging at the limit, counting the rest. No new worker, no setting.
  • The answer is kept per page content (SHA-1), at most 2,048 entries, because the project listing asks about every page on every request.
  • The builder reads the file before it looks at the text it was handed, because stamping parses and rewrites the page and can decode text the file only quotes.
  • The capture replacement shares no first or last letter run with the marker, so one pass leaves no marker for any input.

Test plan

  • hfIdPersist.test.ts, 80 tests: 21 accepted shapes (plain, upper case, templates, inline SVG, a script that only names the marker before a real root, a root before <plaintext>), 14 earlier refusals, 25 rows of text that only looks like a tag, and the limits (500 deep accepted, 600 refused; 100,000 unclosed div, template, table cells, a+div and b each refused within a second; 4,000 attributes accepted, 100,000 refused; a repeated name; the 24 KB page above; a million siblings and 400,000 moved elements accepted).
  • Route level: the comp preview builder returns nothing for a page whose only marker is in script, style or noscript text, and the project listing leaves such a page out.
  • Listing: another route answers while a first listing still parses a 12 MB page; pages past the scan limit are counted and not judged; an incomplete scan and a failed scan each send the files, no composition list, and one log line.
  • packages/studio-server: full suite, 2616 pass, 6 skipped. Typecheck, oxlint, oxfmt and the comment check clean.
  • All 1,099 HTML files in this repo: the answer equals a plain parse5 parse with no limits (950 compositions, 0 differences). Two registry components whose only marker is in a usage comment are non-compositions, as they were at the previous head.
  • Timing on one machine: 1 MB in 60 ms, 10 MB in 0.5 s, a 20 MB page with inline base64 in 1.1 s the first time and 17 ms after.

Not exercised:

  • No live Studio run. Studio does not start in my checkout; the route tests above cover the listing and the preview builder.
  • Windows was not run.
  • A real browser was not driven; "as a browser reads it" rests on parse5 following the HTML standard.

Out of scope:

  • On a first load, when Studio has no earlier list, an incomplete scan shows the files and an empty Compositions panel. Nothing asks for the listing again by itself; the next listing (any file change or action that refreshes the tree, or a reload) picks up from the worker's cache. This needs more than 30 seconds of parsing, which at about 20 MB a second is several hundred MB of pages. No retry loop is added here.
  • The startup id-stamp pass still runs the check on the host thread. It did before this PR, parses each composition there anyway to stamp it, and yields between files.
  • The listing and preview builds share the one worker, so a preview build can wait behind a cold scan and the other way round, each against its own time limit.
  • A page that takes longer than 35 seconds to parse ends the worker and its cache each time.
  • The listing reads whatever the file walk names, as before.
  • Other request handlers in this PR that parse a page: none. The preview builder's check already runs on the build worker.

Not fixed here: the preview builder reads the file a second time after the check, so a file replaced between the two reads is judged on one text and built from the other. It was so before this PR.

  • Unit tests added/updated
  • Manual testing performed
  • Documentation updated (if applicable)
  • Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug

Self-review: read the full diff after the last change and ran a second review pass on it, which found three inputs that were slow or memory-hungry; all three have a limit and a test above. Rollout: ships with the next CLI and Studio release. Rollback: revert this commit.

Signed: Rames

🤖 Generated with Claude Code

jrusso1020 and others added 2 commits November 25, 2026 08:29
…a-composition-id

Studio listed and previewed any HTML file whose text held
data-composition-id=, so a page saved by capture could show up as a
composition. The check now needs a closed start tag with an attribute of
exactly that name and a value, read from the file's own text. Capture
rewrites the marker to text that cannot join with what follows it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ines

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@jrusso1020

Copy link
Copy Markdown
Collaborator Author

Live Studio check: not run. I tried to start Studio on 127.0.0.1 from this branch three ways (from source, from a fresh CLI build, and from the built binary) and it would not start in my checkout, for build reasons unrelated to this change. What stands in for it: the route tests in projects.test.ts exercise the composition listing, and subComposition.test.ts exercises the preview builder, both against files on disk. A reviewer with a working Studio can confirm by opening a project that holds a saved page with look-alike marker text and checking it is absent from the Compositions tab.

Signed: Rames

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 273 (base branch 271), smooth 54 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Newly passing (2)

  • seqscaletwice-scale-px-r0-nested-z100
  • seqscaletwice-scale-px-r0-root-z100

Not banked yet, reported and not gated (bank a full-grid run's baseline.json) (2)

  • seqscaletwice-scale-px-r0-nested-z100
  • seqscaletwice-scale-px-r0-root-z100

Quarantined, measured but not gated (0)

… check

Same result for every input; the attribute walk no longer uses patterns
that start with an optional run of whitespace.

Signed: Rames

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@jrusso1020 jrusso1020 added the needs-jerrai-review Request a review from Jerrai. Removed after the review posts; re-add to request another. label Nov 25, 2026

@jerry-heygen jerry-heygen 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.

Changes requested at 05249726. The capture marker rewrite is an improvement, but isCompositionSource still treats tag-shaped text in script or style content as an actual element. This predicate is used to admit files to composition preview, not just to populate the list, so it cannot rely on a text scan that disagrees with the HTML parser about what is an element.

Use a namespace-aware HTML parse for the admission check. Preserve the valid inline SVG case, and add a regression that raw-text content alone does not qualify a file for composition preview. Exact-head lint, CLI and Studio Server test steps ran and passed. This is a source review; I did not run Studio or reproduce the security behavior.

— Jerrai (pr-review)

@jerry-heygen jerry-heygen removed the needs-jerrai-review Request a review from Jerrai. Removed after the review posts; re-add to request another. label Nov 25, 2026
… build the marked element

The check now parses the page as a browser with scripting on does, so a tag
written inside a script, style, comment, noscript or other text is no
element, and inline SVG is read as a browser reads it. A page nested deeper
than a browser nests, with thousands of attributes on one tag, or that makes
more elements than it has characters is not a composition. An empty
data-composition-id is not a composition either. The answer is kept per page
content, so a listing parses each page once.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@jrusso1020 jrusso1020 changed the title fix(studio): a file is a composition only when an element carries data-composition-id fix(studio): a file is a composition only when a browser would build an element carrying data-composition-id Nov 25, 2026

@jerry-heygen jerry-heygen 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.

Changes requested at 90781438. The new parser addresses the raw-text issue and preserves inline SVG. The remaining blocker is that composition discovery now builds a full HTML tree synchronously on the Studio server's request thread (routes/projects.ts:9–18, helpers/hfIdPersist.ts:94–131). The async file reads do not move parsing off that thread. The depth and per-tag limits do not bound total work across files, and the cache does not help on a first scan or after files change. The timings in the PR already show a single large page taking about a second on a cold parse; a project scan can spend that time on multiple files while unrelated Studio requests wait.

Keep the parser, but run discovery parsing off the request thread with bounded concurrency and a limit on total work. Add a route-level check that an unrelated Studio request can still complete during a cold scan. Exact-head CI was still running at the review cut. This is a source review; I did not run Studio or independently benchmark it.

— Jerrai (pr-review)

…ld worker, not the request thread

Parsing a page holds a thread for as long as it takes, so the listing hands
the pages to the build worker and waits. One scan has 30 seconds from the
moment the worker starts it. When pages are left unread by then, or the scan
does not end, the answer carries the files but no composition list, with one
log line; Studio keeps the list it has, and the next listing goes on from
what the worker already learned.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@jerry-heygen jerry-heygen 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.

Changes requested at 351927c4. Discovery now parses off the request thread, and the new route test covers that improvement. The remaining issue is the shared preview worker's queue: runOffThread posts every job immediately, but a queued-job timeout only removes the promise from pending. It does not remove the worker message. The worker still runs the expired scan, and compositions starts a fresh time budget when it receives the message. Studio can therefore keep doing listing work after that listing has returned and delay a preview that is still waiting.

Dispatch only jobs that are still live, with a bound on queued discovery work, or isolate discovery from preview building. Add coverage that a listing which expires in the queue does no scan and cannot delay the next preview. Hosted CI was still running at the review cut. This is a source review; I did not run Studio.

— Jerrai (pr-review)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants