Skip to content

perf(build): emit tsdown declarations in one native compiler command-line pass - #198661

Merged
obviyus merged 5 commits into
mainfrom
openclaw/kind-barnacle
Nov 14, 2026
Merged

obviyus merged 5 commits into
mainfrom
openclaw/kind-barnacle

Conversation

@obviyus

@obviyus obviyus commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

write-unified-entry-dts is the slowest step of a full build (~2 min on a 32-core Mac). The native declaration emitter routes every compiler read through the TypeScript 7 API's filesystem callbacks and forces singleThreaded emit. A plain tsgo pass over the same roots emits the same declarations in 17-27 s in parallel, versus 44-52 s single-threaded.

Solution

  • tsdown declaration builds run one tsgo -p <leaf config> --listFiles pass. The leaf config sits beside its base, so ${configDir}, relative overrides and default type roots keep the compiler's interpretation; the default type roots are bounded to the checkout.
  • Emit is parallel when stableTypeOrdering is on (the unified build sets it), otherwise single-threaded as before. The readonly-modifier race that motivated singleThreaded does not occur with stable type ordering: 12 emits of the full declaration graph (10 parallel at load 60-89, 2 single-threaded) produced 15,803 .d.ts files with one combined hash.
  • Before the pass, the bounded config reader admits the complete extends chain, so a checkout config cannot inherit emit-affecting options from outside the checkout (the API path denied that read; the build fails closed with TS5083: Cannot read file). Test: rejects a checkout config that inherits options from outside the checkout.
  • Inputs come from --listFiles. The checkout boundary is now a post-check: every file the compiler read must be physically inside the checkout, and escaped inputs are reported before type errors.
  • The writer's cache key now covers every package.json and tsconfig*.json, because the command line does not report consulted manifests or failed resolution probes. The lockfile is deliberately not keyed: --listFiles reports dependency files at their versioned node_modules/.pnpm/<pkg>@<version>/ paths (all 3,877 dependency inputs of tsconfig.core.json), so a dependency change already changes the inputs, and lockfile-only edits keep hitting the CI build-all cache.
  • A compile with nothing to emit no longer fails: the compiler creates its output directory only when it emits.
  • API baselines (diagnostics: "declarations", sometimes a historical compiler) keep the API path unchanged.

Behavior changes (intentional, pinned by tests)

  1. Parent-install shadowing now fails loudly. An optional SDK relative import that reaches a parent-directory node_modules used to be hidden by the virtual filesystem; it now fails with Declaration input escapes checkout: <path>. Test: fails closed when optional SDK relative imports reach a parent install.
  2. A parent-directory package link that resolves back into the checkout now builds. The compiler reports the real path it read, which is checkout content, so the output is correct; the old path refused the outside spelling. Test: builds through a parent package link that resolves back inside the checkout.

Test rewrites: receipts no longer list consulted package.json files; the directory-alias test now proves that editing such a manifest re-keys the cache (cache miss). The cold-sibling build-order test sequences on the compiler subprocess instead of patching Program.prototype.emitToString.

Impact

Full cache-off pnpm build, one run each at load 36-45: write-unified-entry-dts 2m 4.7s -> 1m 20.3s, tsdown-packages 59.9 s -> 32.7 s, total 6m 27s -> 5m 24s. Single runs on a shared host; the per-step deltas are larger than the run-to-run noise we observed.

Evidence

  • Byte-identical on this head (4d933d0, rebased on 3256fe1): full builds of the PR head and of 3256fe1. All 705 declaration files (dist, dist-runtime, packages/*/dist, extensions) are byte-identical. Across all 30,291 dist/dist-runtime files, 9 differ, all build identity: stamps, build-info.json, the commit in help text and its signature, step cache-input hashes (which hash the changed scripts), and the Control UI service worker.
  • Tests on this head: all 15 declaration/tsdown files in test/scripts pass (254 passed, 5 skipped), including native-declaration-boundary, tsdown-declaration-resolution, native-declaration-subprocess, declaration-stage and tsdown-config.
  • Determinism: 12 emits of the full declaration graph (10 parallel under load) produced one combined hash.

@clawsweeper

clawsweeper Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added scripts Repository scripts size: M labels Nov 14, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 14, 2026
@clawsweeper

clawsweeper Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

What this changes

This PR moves tsdown declaration emission to a native compiler CLI pass and enables parallel emission with stable type ordering.

Example: Run a cache-disabled pnpm build.

  • Before: Declaration emission routes compiler reads through filesystem callbacks and runs single-threaded.
  • After: Declaration emission reads files directly through tsgo and runs in parallel when stable type ordering is enabled.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) Reported real builds and declaration equivalence support the optimization, but inherited configuration admission still needs repair.
Proof confidence 🐚 platinum hermit (4/6) This maintainer-authored build-tooling change is outside the shipped-user-behavior proof gate; the body supplies full-build equivalence and repeated emission results, with the final strict-walker adjustment covered by the supplied successful checks.
Patch quality 🦐 gold shrimp (3/6) 1 actionable review finding remain.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Developers wait unnecessarily for declaration generation during full builds.
Reason: The maintainer-authored direction removes expensive compiler callbacks, reports equivalent declaration output, and preserves the historically required API-baseline path.

Merge readiness

⛔ Needs changes before merge - 2 items remain

Keep this PR open: the build optimization is useful, but the inherited-configuration boundary finding remains actionable.

Priority: P2
Reviewed head: 210284fd92851cea4a9b5c211d4ce375ec42a935

Before merge

  • Validate the complete inherited configuration chain (P2) - Only configFile itself is admitted before --showConfig, and the later --listFiles check contains source files rather than inherited configs. For example, a checkout-local tsconfig.json extending ../shared/base.json can inherit strictNullChecks or other emit-affecting options from outside the checkout and successfully publish declarations when all sources are local. The previous bounded API path denied that inherited read. Use the existing bounded configuration reader to validate the full extends chain before CLI compilation; this earlier review finding remains unresolved.
  • Explain or restore the original intent of Inherited configuration admission - 813169e: The bounded filesystem admitted configuration reads as part of the checkout-owned compiler-input contract; the new path does not explain allowing outside inherited configuration.

Findings

  • [P2] Validate the complete inherited configuration chain — scripts/lib/native-declaration-emitter.mts:324-331

Provenance

  • Inherited configuration admission changes intended behavior without addressing why it exists (813169e: The bounded filesystem admitted configuration reads as part of the checkout-owned compiler-input contract; the new path does not explain allowing outside inherited configuration.)
Agent review details

How this fits together

The build tooling turns selected TypeScript roots and compiler configuration into declarations, source maps, and compiler-input receipts before staged publication.

flowchart LR
  A[Build command] --> B[Declaration writer]
  B --> C[Cache snapshot]
  B --> D[Native declaration emitter]
  D --> E[tsgo compiler]
  E --> F[Input admission]
  F --> G[Staged declaration publication]
Loading

Technical review

Best possible solution:

Keep the CLI optimization while using the existing bounded configuration reader to admit the complete inherited configuration chain.

Do we have a high-confidence way to reproduce the issue?

Source inspection establishes that an outside inherited config containing only compiler options can affect a successful CLI emit without appearing in --listFiles; no target code was executed.

Is this the best way to solve the issue?

Removing filesystem callbacks from the expensive compile is reasonable, but source membership cannot replace admission of configuration inputs.

Full review comments:

  • [P2] Validate the complete inherited configuration chain — scripts/lib/native-declaration-emitter.mts:324-331
    Only configFile itself is admitted before --showConfig, and the later --listFiles check contains source files rather than inherited configs. For example, a checkout-local tsconfig.json extending ../shared/base.json can inherit strictNullChecks or other emit-affecting options from outside the checkout and successfully publish declarations when all sources are local. The previous bounded API path denied that inherited read. Use the existing bounded configuration reader to validate the full extends chain before CLI compilation; this earlier review finding remains unresolved.
    Confidence: 0.98

Overall correctness: patch is incorrect
Overall confidence: 0.96

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 897846bbe085.

Provenance checked

  • CLI emission and nested-checkout resolution changes intended behavior with a stated reason (813169e: Bounded in-memory compilation restored nested physical checkout builds while preventing ancestor installations from changing declarations.)
  • Single-threaded declaration emission changes intended behavior with a stated reason (cbb17e8: Deterministic native declaration visits prevented inferred readonly variation and preserved valid declaration receipts.)
  • Historical revision compiler selection keeps the original intent (17a8d22: Historical SDK declarations require their revision's compiler executable and matching API client.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

Acceptance criteria:

  • pnpm test test/scripts/native-declaration-boundary.test.ts test/scripts/tsdown-declaration-resolution.test.ts test/scripts/native-declaration-subprocess.test.ts
  • node scripts/run-tsgo.mjs -p test/tsconfig/tsconfig.test.root.tooling.json --incremental
  • pnpm tsgo:scripts
  • git diff --check

What I checked:

PR surface

Tests +16, Other +135. Total +151 across 4 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 0 0 0 0
Tests 2 98 82 +16
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 2 145 10 +135
Total 4 243 92 +151

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

  • add merge-risk: 🚨 compatibility: The CLI intentionally changes nested-build behavior for parent-install shadowing and parent links resolving inside the checkout; the collaborator-authored body explicitly accepts these tradeoffs.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🐚 platinum hermit and patch quality is 🦐 gold shrimp.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

Label justifications:

  • P2: This improves developer build performance and has a focused configuration-boundary defect with limited runtime impact.
  • merge-risk: 🚨 compatibility: The CLI intentionally changes nested-build behavior for parent-install shadowing and parent links resolving inside the checkout; the collaborator-authored body explicitly accepts these tradeoffs.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🐚 platinum hermit and patch quality is 🦐 gold shrimp.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (1 earlier review cycle)
  • reviewed 2026-11-14T05:13:45.963Z sha 5abbb7d :: needs changes before merge. :: [P2] Validate the complete inherited configuration chain | [P2] Track inherited configs independently of their filenames

Reviewed November 14, 2026, 2:36 AM ET / 07:36 UTC (Revision 2).

obviyus and others added 3 commits November 14, 2026 11:34
…line pass

The declaration emitter routed every compiler read through the native API's
filesystem callbacks and forced a single-threaded emit. tsdown declaration
builds now run one tsgo command-line pass, in parallel when stable type
ordering is on, and take their inputs from --listFiles.

Inputs outside the checkout still fail the build, now as a post-check on the
files the compiler read. The writer keys every package.json and tsconfig plus
the lockfile, which covers the manifests and failed probes the command line
does not report. Two boundary behaviors change: an optional import that
reaches a parent-directory install now fails with the escaped path instead of
being hidden, and a parent-directory link that resolves back into the
checkout builds from that checkout content.

API baselines keep the API path for their declaration-only diagnostics.

Co-authored-by: obviyus <[email protected]>
…line pass

OpenClaw-Publication: 710a6ce2-0938-8665-8f11-5f27040dc82e
…emits

Read dependency files resolve to versioned node_modules/.pnpm paths, so a
dependency change already changes the inputs; the lockfile key only caused
needless misses. The compiler creates its output directory only when it emits,
so a compile with nothing to emit no longer fails scanning it.

Co-authored-by: obviyus <[email protected]>
@obviyus
obviyus force-pushed the openclaw/kind-barnacle branch from 5abbb7d to 4d933d0 Compare November 14, 2026 06:04
@obviyus
obviyus marked this pull request as ready for review November 14, 2026 06:13
@clawsweeper clawsweeper Bot removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Nov 14, 2026
@obviyus

obviyus commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 14, 2026
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Nov 14, 2026
…ation pass

The command-line pass read inherited configs directly, so a checkout config
extending a base outside the checkout could change emit unnoticed. The bounded
config reader now admits the whole extends chain first, as the API path did.

Co-authored-by: obviyus <[email protected]>
@obviyus
obviyus merged commit 5a7d171 into main Nov 14, 2026
108 checks passed
@obviyus
obviyus deleted the openclaw/kind-barnacle branch November 14, 2026 08:17
@obviyus

obviyus commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator Author

Landed in 5a7d171. Declarations now come from one native compiler pass that reads the checkout directly (and checks/emits in parallel under stable type ordering) instead of routing every read through the API filesystem callbacks: write-unified-entry-dts 2m05s -> 1m20s, tsdown-packages 60s -> 33s. Proof: all 705 .d.ts files byte-identical to main, 119 emitter tests green, and the inherited-config boundary from the last review restored with a regression test.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 15, 2026
…line pass (openclaw#198661)

## Problem

`write-unified-entry-dts` is the slowest step of a full build (~2 min on a 32-core Mac). The native declaration emitter routes every compiler read through the TypeScript 7 API's filesystem callbacks and forces `singleThreaded` emit. A plain `tsgo` pass over the same roots emits the same declarations in 17-27 s in parallel, versus 44-52 s single-threaded.

## Solution

- tsdown declaration builds run one `tsgo -p <leaf config> --listFiles` pass. The leaf config sits beside its base, so `${configDir}`, relative overrides and default type roots keep the compiler's interpretation; the default type roots are bounded to the checkout.
- Emit is parallel when `stableTypeOrdering` is on (the unified build sets it), otherwise single-threaded as before. The readonly-modifier race that motivated `singleThreaded` does not occur with stable type ordering: 12 emits of the full declaration graph (10 parallel at load 60-89, 2 single-threaded) produced 15,803 `.d.ts` files with one combined hash.
- Before the pass, the bounded config reader admits the complete `extends` chain, so a checkout config cannot inherit emit-affecting options from outside the checkout (the API path denied that read; the build fails closed with `TS5083: Cannot read file`). Test: `rejects a checkout config that inherits options from outside the checkout`.
- Inputs come from `--listFiles`. The checkout boundary is now a post-check: every file the compiler read must be physically inside the checkout, and escaped inputs are reported before type errors.
- The writer's cache key now covers every `package.json` and `tsconfig*.json`, because the command line does not report consulted manifests or failed resolution probes. The lockfile is deliberately not keyed: `--listFiles` reports dependency files at their versioned `node_modules/.pnpm/<pkg>@<version>/` paths (all 3,877 dependency inputs of `tsconfig.core.json`), so a dependency change already changes the inputs, and lockfile-only edits keep hitting the CI build-all cache.
- A compile with nothing to emit no longer fails: the compiler creates its output directory only when it emits.
- API baselines (`diagnostics: "declarations"`, sometimes a historical compiler) keep the API path unchanged.

### Behavior changes (intentional, pinned by tests)

1. **Parent-install shadowing now fails loudly.** An optional SDK relative import that reaches a parent-directory `node_modules` used to be hidden by the virtual filesystem; it now fails with `Declaration input escapes checkout: <path>`. Test: `fails closed when optional SDK relative imports reach a parent install`.
2. **A parent-directory package link that resolves back into the checkout now builds.** The compiler reports the real path it read, which is checkout content, so the output is correct; the old path refused the outside spelling. Test: `builds through a parent package link that resolves back inside the checkout`.

Test rewrites: receipts no longer list consulted `package.json` files; the directory-alias test now proves that editing such a manifest re-keys the cache (`cache miss`). The cold-sibling build-order test sequences on the compiler subprocess instead of patching `Program.prototype.emitToString`.

## Impact

Full cache-off `pnpm build`, one run each at load 36-45: `write-unified-entry-dts` 2m 4.7s -> 1m 20.3s, `tsdown-packages` 59.9 s -> 32.7 s, total 6m 27s -> 5m 24s. Single runs on a shared host; the per-step deltas are larger than the run-to-run noise we observed.

## Evidence

- Byte-identical on this head (`4d933d0`, rebased on `4ff01c8`): full builds of the PR head and of `4ff01c8`. All 705 declaration files (`dist`, `dist-runtime`, `packages/*/dist`, extensions) are byte-identical. Across all 30,291 `dist`/`dist-runtime` files, 9 differ, all build identity: stamps, `build-info.json`, the commit in help text and its signature, step cache-input hashes (which hash the changed scripts), and the Control UI service worker.
- Tests on this head: all 15 declaration/tsdown files in `test/scripts` pass (254 passed, 5 skipped), including `native-declaration-boundary`, `tsdown-declaration-resolution`, `native-declaration-subprocess`, `declaration-stage` and `tsdown-config`.
- Determinism: 12 emits of the full declaration graph (10 parallel under load) produced one combined hash.

Co-authored-by: Ayaan Zaidi <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 19, 2026
Revert PR #198661 on the release candidate as explicitly requested by Dallin before the next FRV. Preserve the later #198774 relative-path optimization and all other release repairs.

This reverts commit 5a7d171.

Co-authored-by: RomneyDa <[email protected]>
fuller-stack-dev added a commit that referenced this pull request Nov 19, 2026
)

* fix(build): run the declaration pass single-threaded unless the build heap is uncapped

The parallel native declaration pass (#198661) holds Go memory outside
--max-old-space-size. A cold build of main is OOM-killed in that pass in
an 8 GiB container even with nothing else running; the single-threaded
pass completes there. Parallel emit now needs the build owner's uncapped
default heap, which it grants only when memory is ample.

* test(build): prove both declaration modes emit identical output

Compare the capped single-threaded and uncapped parallel passes in one
fixture, keep the compiles running on Windows, and document which hosts
stay single-threaded.

* fix(build): admit parallel declarations from the planned old space, not the total heap

V8's heap_size_limit adds the young generation, so a planner-capped
12287 MB old space still read as uncapped. Read the --max-old-space-size
the build owner wrote instead, and keep the test wrapper's paths out of
shell source.

* fix(build): take declaration threading from the planned heap budget

build-all hands every build child its frozen budget in
OPENCLAW_TSDOWN_MAX_OLD_SPACE_MB. Reading Node heap flags could still
admit the parallel pass when an inherited underscored alias survived the
planner's rewrite.

* refactor(build): let tsdown-build hand its frozen heap budget to every child

tsdown-build now exports OPENCLAW_TSDOWN_MAX_OLD_SPACE_MB itself, so
direct tsdown-build runs carry the budget that build-all used to add,
and the declaration emitter shares the owner's uncapped-heap threshold
instead of copying it.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 20, 2026
…claw#201901)

* fix(build): run the declaration pass single-threaded unless the build heap is uncapped

The parallel native declaration pass (openclaw#198661) holds Go memory outside
--max-old-space-size. A cold build of main is OOM-killed in that pass in
an 8 GiB container even with nothing else running; the single-threaded
pass completes there. Parallel emit now needs the build owner's uncapped
default heap, which it grants only when memory is ample.

* test(build): prove both declaration modes emit identical output

Compare the capped single-threaded and uncapped parallel passes in one
fixture, keep the compiles running on Windows, and document which hosts
stay single-threaded.

* fix(build): admit parallel declarations from the planned old space, not the total heap

V8's heap_size_limit adds the young generation, so a planner-capped
12287 MB old space still read as uncapped. Read the --max-old-space-size
the build owner wrote instead, and keep the test wrapper's paths out of
shell source.

* fix(build): take declaration threading from the planned heap budget

build-all hands every build child its frozen budget in
OPENCLAW_TSDOWN_MAX_OLD_SPACE_MB. Reading Node heap flags could still
admit the parallel pass when an inherited underscored alias survived the
planner's rewrite.

* refactor(build): let tsdown-build hand its frozen heap budget to every child

tsdown-build now exports OPENCLAW_TSDOWN_MAX_OLD_SPACE_MB itself, so
direct tsdown-build runs carry the budget that build-all used to add,
and the declaration emitter shares the owner's uncapped-heap threshold
instead of copying it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. scripts Repository scripts size: M status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant