Skip to content

Studio: optimize startup by deferring fine-tuning actions - #8624

Merged
danielhanchen merged 3 commits into
unslothai:mainfrom
oobabooga:audit/studio-lazy-settings-dialog
Aug 13, 2026
Merged

danielhanchen merged 3 commits into
unslothai:mainfrom
oobabooga:audit/studio-lazy-settings-dialog

Conversation

@oobabooga

Copy link
Copy Markdown
Member

Every Studio page load currently pulls fine-tuning workflow code into the initial frontend bundle because the Settings Data tab imports it eagerly. This loads Recipe Studio database and UI code plus training helpers even when the user never opens Settings or uses either fine-tuning action.

What changed

  • Load the fine-tuning workflow only when Open in Recipes or Load in Train tab is run.
  • Keep the Settings Data tab itself available immediately.
  • Keep the normal JSONL export path unchanged.
  • Preserve the existing loading indicators, errors, and navigation for both deferred actions.

Results

Measurements are medians from 10 interleaved fresh Chromium contexts against production builds with the browser cache disabled.

Startup metric Main This change Improvement
First contentful paint 508 ms 478 ms 30 ms sooner
Largest contentful paint 750 ms 714 ms 36 ms sooner
Decoded resources 9,586 KB 8,972 KB 614 KB less
Transferred resources 4,402 KB 4,217 KB 184 KB less
Main-thread work 1,501 ms 1,475 ms 26 ms less

A live browser run also confirmed that running Open in Recipes fetched the deferred fine-tuning and Recipe Studio chunks and continued into the existing chat export flow.

Testing

  • npm test (2,008 passed)
  • Focused deferred fine-tuning import coverage (1 passed)
  • npm run typecheck
  • npm run build
  • Focused Biome checks and git diff --check
  • Live production-build startup comparison and deferred action validation

oobabooga and others added 2 commits August 12, 2026 17:58
… the bundle

The new test read one file, so a static import of finetune-recipe added to any other
eagerly reached module would put the whole Recipe Studio chunk back into startup with
the assertion still green. It now walks src/ and fails on a static import anywhere,
ignoring the deferred import() forms this PR introduces. Verified against a negative
control: re-adding a static import to settings-section.tsx fails the test.

The second comment was also inaccurate. unsloth_training_config_v1 is in the eager
bundle on both sides, because __root.tsx and app-sidebar.tsx import the
@/features/training barrel that re-exports the training config store and datasets-api,
both of which finetune-recipe also pulls. What leaves is Recipe Studio, which is where
the whole 575 KB comes from, so the first comment is right and this one would have sent
the next person auditing startup looking for something that is not there.
@danielhanchen

Copy link
Copy Markdown
Member

Thanks, this one checks out. I verified it by building the bundles rather than reading the diff: data-tab.tsx really is in the startup graph (__root.tsx -> SettingsDialog -> DataTab), nothing else statically imports finetune-recipe on the branch, and eager JS drops 6,566 KB -> 5,991 KB. That is 575 KB against your claimed 614 KB decoded, which is just raw bytes versus decoded. The await import sits inside the existing try, so a chunk that 404s lands in the same catch and finally and the spinner still resets, finetune-recipe.ts has no module-level side effects, and the ArchivedMediaView hunk is pure formatter churn.

I also built this together with #8623: -934 KB and -575 KB alone, -1,509 KB merged, exactly the sum. Disjoint module sets, no shared files, both merge clean into current main, so landing order does not matter.

Pushed 9e466c9df with two small things.

The new test held one file rather than the property. A static import of finetune-recipe added to any other eagerly reached module would put the whole Recipe Studio chunk back into startup with the assertion still green, which is the regression this PR exists to prevent. It now walks src/ and fails on a static import anywhere, ignoring the import() forms this PR introduces. I checked it actually catches that: re-adding a static import to settings-section.tsx fails the test, and removing it passes again.

The second comment is inaccurate. unsloth_training_config_v1 is in the eager bundle on both sides, because __root.tsx:28 and app-sidebar.tsx:164 import the @/features/training barrel, which re-exports both the training config store and datasets-api, and finetune-recipe pulls those same two modules. What actually leaves is Recipe Studio (seed_source_type: eager before, absent after), which is where all 575 KB comes from. The first comment is right; this one would have sent the next person auditing the startup path looking for something that is not there.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9e466c9df8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +51 to +53
if (!/finetune-recipe/.test(line)) continue;
if (/\bimport\s*\(/.test(line)) continue;
if (/^\s*(import|export)\b/.test(line)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Parse multi-line imports before declaring the graph clean

When another source file uses a normally formatted multi-line static import such as the one removed from data-tab.tsx, the finetune-recipe path appears on a } from ... line rather than a line beginning with import or export. This loop therefore skips it and the test passes even though that import can restore the workflow to the eager bundle. Match complete import declarations or parse each file's import graph instead.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good catch, and it was a live hole rather than a theoretical one: the import this PR removes from data-tab.tsx is exactly the four-line shape, so the check could not have caught the regression it exists to prevent.

Fixed in 42624b684 by parsing instead of scanning. The test now builds a ts.SourceFile per file under src/ and collects the moduleSpecifier of every ImportDeclaration and ExportDeclaration, which is the edge set that actually fixes bundle membership. A deferred import(...) parses as a call expression, not a declaration, so the pattern this PR adopts stays allowed.

Proved both directions rather than assuming:

  • multi-line static import injected into settings-row.tsx (eagerly reached, and deliberately not data-tab.tsx since the first assertion would have caught that by another route): old line-based check passes 2/2, new check fails with features/settings/components/settings-row.tsx: ../components/finetune-recipe
  • the same import collapsed to one line: old check now fails too, which isolates the gap to line spanning and nothing else
  • injection removed: 2/2 green
  • all three dynamic forms injected at once (single-line, multi-line, bare non-awaited): 2/2 green, so the deferral pattern is not flagged
  • export * from "./finetune-recipe" and a nested declare module variant both fail, covering the ExportDeclaration arm and proving the recursion is live

Cost is 1.9s for the file, 608ms of which is parsing all 1138 sources. Suite 2009/2009, typecheck clean.

The repo-wide check walked each file line by line and keyed the match off the
line beginning with `import`, so a multi-line import left its specifier on the
closing `} from "..."` line and was skipped. That is exactly the shape of the
static import removed from data-tab.tsx, so the regression this test exists to
catch would have gone through green.

Collect module specifiers from the TypeScript AST instead. Import and export
declarations carry a moduleSpecifier, while a deferred `import(...)` parses as
a call expression and is never collected, so the pattern the two actions now
use stays allowed. Parsing all 1138 source files costs about 0.6s.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 42624b684b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +68 to +72
for await (const file of walk(SRC)) {
const text = await readFile(file, "utf8");
for (const specifier of staticSpecifiers(file, text)) {
if (RECIPE_MODULE.test(specifier))
offenders.push(`${path.relative(SRC, file)}: ${specifier}`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Limit the startup guard to eagerly reachable modules

When a lazy route or another action-scoped chunk statically imports this helper, the workflow remains deferred, but this repo-wide loop still reports it as part of startup because it examines every source file without tracing reachability from the eager entry points. This creates a false test failure for valid lazy-chunk reuse; restrict the check to the eager import graph or validate the generated bundle manifest instead.

Useful? React with 👍 / 👎.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 42624b684b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 710beb1 into unslothai:main Aug 13, 2026
36 of 41 checks passed
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