Skip to content

fix(studio-server): sub-composition previews build off the request thread within a deadline - #7857

Merged
miguel-heygen merged 6 commits into
mainfrom
fix/subcomposition-build-deadline
Nov 16, 2026
Merged

miguel-heygen merged 6 commits into
mainfrom
fix/subcomposition-build-deadline

Conversation

@miguel-heygen

@miguel-heygen miguel-heygen commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

What

GET /projects/:id/preview/comp/* now builds the sub-composition page on a worker thread, within a deadline (30 s by default). A page still building at the deadline is answered 503 with " took too long to build", and its worker is ended and replaced.

Why

A sub-composition page is the project's own HTML, and a shared or imported project can carry any page. The route built it on the server's request thread with no time limit, in three steps: the hf-id pin on the raw file, buildSubCompositionHtml, then the hf-id pass on the transformed page. A page big enough to take seconds held every other request to the server for that long. One that never finished held the server for good. The media-codec probes for the page's media had no limit either.

Related work

The root preview build already runs through the host's adapter.bundle, which a host can run off its main thread. This gives the sub-composition build the same separation inside studio-server itself, for every host.

How

  • helpers/subCompositionWorker.ts: the worker. A build job pins hf-ids on the raw file, as pinSubCompHfIds did, and runs buildSubCompositionHtml. An ids job pins hf-ids on the page the host transformed.
  • helpers/subCompositionBuild.ts: one long-lived worker, so builds queue one at a time. Its entry is found beside the module that starts it: the built .js in dist, else the .ts source through tsx, as shaderTransitionWorkerPool does.
    • runOffThread(job, deadline, compPath) refuses with SubCompositionDeadlineError once the deadline passes. The worker is then ended, and the builds queued behind the refused one go to a fresh worker.
    • A worker crash fails every build it held, since any of them may have caused it. They are not resent, so a page that crashes the worker cannot loop.
    • withinDeadline puts the same deadline on the media-codec probes. They are child processes, so nothing blocks; the probes themselves finish and fill the cache.
  • The route keeps one deadline for the whole request, across the build, the host transform, the second hf-id pass and the probes.
  • createStudioApi(adapter, { subCompositionDeadlineMs }) sets the deadline; tests use a short one.
  • tsup.config.ts in studio-server and the CLI emit subCompositionWorker.js beside the chunks that start it, as the CLI already does for shaderTransitionWorker.

Test plan

  • Unit tests added: src/routes/preview.compDeadline.test.ts
    • A page of 300,000 elements, with a 200 ms deadline, is answered 503, and the request thread never goes 200 ms without running a timer while it builds.
    • A page that builds inside a patient deadline is served whole, and the request thread stays free for the build and the passes after it.
    • After a refused page, a small page still builds (the worker was replaced) and answers 200 with its content.
    • fails without the fix: on main with only this test file, the huge page answers 200 after about 80 s of building on the request thread; the refused-page tests fail (2 failed, 1 passed).
  • With the fix, preview.compDeadline.test.ts and the existing preview.compPath.test.ts pass (5 of 5); the deadline file passes 3 runs in a row, and 3 more beside one busy loop per CPU, as the flake gate runs it.
    • Those runs include hyperframes#7916 (hf-id minting in linear time). Without it the page that should build inside its deadline takes over 60 s under CPU contention, because its repeated elements mint ids in quadratic time; this PR merges after it.
  • Manual testing performed: not done. No CLI or Studio app was run against a real project.
  • Documentation updated: no user-facing docs name this route.
  • Comments follow CONTRIBUTING.md "Comments"

Not exercised: a packaged CLI build starting the worker from dist (the CI build compiles it; no hyperframes preview run was made), and Windows.

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 267 (base branch 267), smooth 121 of those

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

Quarantined, measured but not gated (8)

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Request changes at b4fb520d. Moving the sub-composition build onto a worker under a deadline is the right shape, and the deadline/replace logic reads correctly. But the head doesn't build, and the worker can't start from source, so the new tests and the existing sub-composition route tests fail.

Blocking

  1. Typecheck/build fails. src/helpers/subCompositionBuild.ts:73: started.on("error", (error) => replace(started, error)). The error parameter is unknown, which gives TS2345: Argument of type 'unknown' is not assignable to parameter of type 'Error | undefined'. This fails Build, Typecheck, and every job that builds the workspace first (Test (studio/cli/runtime/support), CLI smoke, Producer, SDK). Fix: (error) => replace(started, error instanceof Error ? error : new Error(String(error))).

  2. The worker can't load from source, so every sub-composition preview returns 500 there. subCompositionWorker.ts imports ./subComposition.js. When the .ts entry runs in a Node worker, that relative .js doesn't map to the .ts file. Node 22.18+ strips types itself, so the worker file loads, but its import doesn't resolve. Adding --import tsx/esm to the worker's execArgv doesn't fix it either. I tried it directly on Node 22.22.3 and got ERR_MODULE_NOT_FOUND …/src/helpers/subComposition.js. The precedent, shaderTransitionWorker, only works because it has no relative imports.

    • Locally at this head: vitest run src/routes/preview.compDeadline.test.ts src/routes/preview.compPath.test.ts fails 4/4. Each request returns 500 with that error in stderr, including the two existing preview.compPath tests.
    • The same path affects any source-mode host, e.g. Studio dev loading studio-server from src.
    • Possible fixes: in source mode, start a small .mjs bootstrap that calls register() from tsx/esm/api and then import()s the .ts worker. Or keep the worker free of relative imports. Then re-run both test files.
  3. Comments check: createStudioApi.ts comment share rose (the new doc line on subCompositionDeadlineMs). Cut it or move it, per the ratchet's message.

Non-blocking

  • A deadline on a queued job kills the running one. When a job times out while another job is running (e.g. one request's ids job behind a different request's slow build), replace() ends the worker. The running build is resent and starts over from scratch. The comment "after a deadline the culprit is gone" doesn't hold in that case. It's bounded by each job's own deadline, so it isn't a correctness bug. Tracking the in-flight id, and only replacing when the timed-out job is the one running, would avoid it.
  • The remaining request-thread passes aren't bounded. After the worker returns, the route still runs recordPreviewReferences (an element walk), transformPreviewHtml, injectStudioPreviewAugmentations (which does a parseHTML at preview.ts:218) and the codec-map scan. All of these run synchronously on the request thread, over a page the worker took up to 30 s to build. withinDeadline can't interrupt synchronous work. Either a test with a page that builds just inside the deadline, or a note on why those passes are cheap relative to the build, would close this.
  • The test plan's "fails without the fix" run and the existing preview.compPath run are still pending in the description. Point 2 above is what they would have shown.

Need: real (a project's own page could hold the request thread with no bound) · Size: right-sized · Reuse: follows the existing worker-entry pattern, but that pattern doesn't carry over to a worker with relative imports · Simpler: fine · Structure: good split (route / pool / worker)
Risk: as posted, every sub-composition preview 500s in source mode and the workspace doesn't build. Once 1–3 are fixed, the risk is low, with the packaged-CLI dist worker path still unexercised.

— Rames

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Answers to the review at b4fb520, all at 2386bc8:

  1. The worker's error handler narrows to an Error (subCompositionBuild.ts, startWorker).
  2. From source on Node the worker starts through subCompositionWorker.dev.mjs, which loads the .ts through tsx; Bun runs the .ts itself; the built .js in dist is used when present. preview.compPath.test.ts passes with this (2 of 2).
  3. The createStudioApi.ts doc line is gone, and the new worker file's comments are cut under its package's share.

Non-blocking:

  • A queued job's deadline no longer ends the worker: only the running job's does (runOffThread checks it is first in the queue), and a queued one past its own deadline is just dropped.
  • The passes after the build that parse the page (ensureHfIds, injectStudioPreviewAugmentations, variables) run on the worker as a second job; the host's transform and the codec probes stay on the request thread under the same deadline. The new "inside its deadline" test pins that the thread stays free for the whole request.
  • The red and green runs are in the body. Test (c)'s earlier red was its own stale assertion (pages now carry hf-ids, so <p>Hi</p> reads <p data-hf-id=…>Hi</p>); and the flake gate's timeout on test (b) was quadratic hf-id minting, fixed in perf(parsers): ids for many identical elements mint in linear time #7916, which this merges after.

@miguel-heygen
miguel-heygen force-pushed the fix/subcomposition-build-deadline branch from 4aa3747 to b497f5e Compare November 16, 2026 03:33
@miguel-heygen
miguel-heygen force-pushed the fix/subcomposition-build-deadline branch from b497f5e to 1d189fd Compare November 16, 2026 05:10

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approve at 1d189fd6. All three blockers from my request for changes at b4fb520d are fixed, and so are both non-blocking points.

The earlier blockers

  1. Typecheck. The error handler now narrows unknown (subCompositionBuild.ts). Build, Typecheck and the jobs that build first are green.
  2. The worker from source under Node. The new subCompositionWorker.dev.mjs calls register() from tsx/esm/api, then imports the .ts. Bun runs the .ts directly, and dist uses the built .js. Locally at this head, preview.compDeadline.test.ts + preview.compPath.test.ts pass 5/5, through a real Node worker started from src. At b4fb520d both files failed 4/4 with ERR_MODULE_NOT_FOUND.
  3. Comments check: green.

The earlier non-blocking points

  • A queued job's deadline no longer kills the running build. Only a timeout on the first pending job (the running one) replaces the worker. A queued job past its deadline is just dropped.
  • The passes after the build are off the request thread. ensureHfIds, the studio augmentations (with their parseHTML) and the preview variables now run as the worker's finish job. The new "inside its deadline" test holds the request thread to under 200 ms of stall across a page of 10,000 cells, the host transform and the codec scan included.

Control. I ran the new deadline file against the merge base's route and helpers, with the test file at head. It did not finish within 500 s (timeout exit 124): the huge page built on the thread that also runs vitest's own timers, so even the test timeout couldn't fire. That is the bug this fixes.

Non-blocking

  • One worker serves every project. A page that runs to its deadline delays every sub-composition preview queued behind it, and those can 503 when their own deadlines pass. That is bounded (30 s) and better than holding the request thread, but worth knowing for a server that previews several projects at once.
  • A dropped queued job still runs. The worker runs it anyway and its reply is ignored. So "first pending = running" can be off while the worker works through dropped jobs, and a later deadline may then end the worker in the middle of one. That is harmless and also bounded.
  • The codec scan (scanHtmlOpeningTags) still runs on the request thread over the built page. It is a linear tag scan, and the large-page test covers it.
  • Packaged CLI from dist and Windows are still not run, as the body says. The tsup entries for both studio-server and the CLI emit subCompositionWorker.js beside the chunk that looks for it.

Need: real (a project's own page could hold the request thread with no bound) · Size: right-sized · Reuse: follows the worker-entry pattern and now handles relative imports · Structure: good (route / pool / worker) · Risk: low.

— Rames

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 16, 2026
Merged via the queue into main with commit d159ec5 Nov 16, 2026
84 checks passed
@miguel-heygen
miguel-heygen deleted the fix/subcomposition-build-deadline branch November 16, 2026 07:51
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.

2 participants