返回 CodeWhale
BUILD_PERFORMANCE.md
根目录 / docs / BUILD_PERFORMANCE.md
1 # Build and test performance
2
3 > 阅读简体中文版:[zh_hans/BUILD_PERFORMANCE.md](zh_hans/BUILD_PERFORMANCE.md)。
4
5 Measured facts about how long Codewhale takes to build and test, what was
6 changed to make the contributor loop faster, and what is deferred. Numbers
7 are from one machine (Apple Silicon, 14 cores, rustc 1.97.0, Xcode 26.2
8 `ld-1230`) taken while four other cargo jobs were running (1-minute load
9 average 10–27, recorded next to each number), so treat them as relative
10 before/after evidence, not benchmarks.
11
12 > Split plan: [TUI deconstruction](design/TUI_DECONSTRUCTION.md) contains the
13 > current source audit, target crate map and extraction order. Measurements
14 > below are historical; the B3/deferred candidate lists are not an execution queue.
15
16 ## October 22, 2026: shared Engine future boundary
17
18 The 0.10.2 candidate's `crates/tui/src` has 1,040 Rust files and 1,279,727
19 physical lines including tests. Filtering test names still compiles the one
20 large library-test target. The older measurements below describe an earlier
21 source tree and compiler.
22
23 Two successive local builds on the 14-core Apple Silicon machine, with Rust
24 1.99.0, the same incremental cache, features and development profile:
25
26 | Candidate build | Wall time |
27 | --- | ---: |
28 | Shared Engine loop returned its full concrete future | 29m 31s |
29 | `Engine::run_owned` returned a boxed `Send` future | 2m 24s |
30
31 A native sample of the first build found recursive trait-obligation work during
32 monomorphization and MIR validation on one compiler thread. The existing shared
33 loop now erases its future type at that boundary; root and child engines retain
34 the same event loop. It adds one allocation per Engine invocation. The second
35 build also corrected three existing test assertions and collected Cargo
36 `--timings`. These are warm local observations, not an isolated benchmark or a
37 cold-build, release-build or cross-platform speedup promise. The 2,172 captured
38 inputs stayed unchanged during each build and its focused checks; 58 relevant
39 existing checks passed after the boundary change. Final release validation is
40 separate.
41
42 Keep incremental compilation and the existing line-table debug setting for the
43 local edit loop. Finish a coherent batch before compiling, then reuse its test
44 binary for relevant filters while inputs stay unchanged. Capture timings on a
45 build already needed for validation. The structural follow-up remains the
46 [TUI deconstruction](design/TUI_DECONSTRUCTION.md): move implementation and its
47 existing tests together so each subsystem can build independently.
48
49 ## Where the time goes (baseline, commit 533c530b)
50
51 | Step | Wall | Notes |
52 | --- | --- | --- |
53 | Cold `cargo build -p codewhale-tui` (empty target) | 94 s (user 270 s) | 543 units; `codewhale-tui` alone is 70 s and is the critical path; next longest units are `codewhale-config` 7.5 s, `jsonschema` 6.3 s, `codewhale-workflow` 5.6 s, `tokio` 5.4 s. Load 13. |
54 | Cold `cargo test -p codewhale-tui --lib --no-run` (empty target) | 148 s (user 347 s) | The 10.5k-test unit binary is 357 MB and trips the macOS linker's `__eh_frame > 16MB` compact-unwind warning (harmless). Load 20. |
55 | Incremental `cargo build -p codewhale-tui` after a one-line edit | 12.5 s | Load 11. |
56 | Incremental `cargo test -p codewhale-tui --lib --no-run` after a one-line edit | 19 s | Load 11. |
57 | `cargo test --workspace --all-features --locked --no-run` with deps warm | 155 s (user 366 s) | 61 test binaries. Load 6→11. |
58 | Cold-ish `cargo check --workspace --all-targets --locked` (deps built) | 82 s | Load 19. |
59 | Running the tui unit suite with libtest (`cargo test -p codewhale-tui --lib`) | 268 s | From the release gate log; 10,531 tests. Load ~10. |
60 | Running the same suite with `cargo nextest run -p codewhale-tui --lib` | 96–108 s | Same tests, one process per test, all cores busy. Load 12–20. |
61 | Running the whole workspace under `cargo nextest run --workspace --all-features` | 353 s | 12,744 tests, PTY suite serialized by the nextest config. Load 17. |
62
63 Structural facts behind those numbers:
64
65 - `crates/tui` is ~746k lines of Rust (609k non-test, 137k inline tests in
66 488 `#[cfg(test)]` modules, 10.6k `#[test]`/`#[tokio::test]` functions).
67 It compiles as one crate, so the frontend of that crate is the critical
68 path of every build and every unit-test run recompiles it with
69 `cfg(test)`.
70 - Dependencies are already trimmed (`reqwest` rustls-no-provider, `image`
71 png only, `syntect` default-fancy, `rmcp` no default features, `mimalloc`
72 no default features). `cargo tree -d` shows only routine duplicates
73 (`toml` 0.8/1.1, `thiserror` 1/2, `strum` 0.27/0.28, `syn` 2/3,
74 `sha2` 0.10/0.11) that come from third-party crates, not from workspace
75 choices. The global allocator is mimalloc by default; the off-by-default
76 `rusty-alloc` cargo feature on `codewhale-cli` swaps it for
77 the pure-Rust `rusty_alloc` remake (#5872). Use
78 `cargo build -p codewhale-cli --no-default-features --features rusty-alloc`
79 to exclude mimalloc and its C build dependency.
80 Cargo features are additive: `--features rusty-alloc` alone retains the
81 default mimalloc dependency even though the Rust allocator handles allocations.
82 This removes the allocator's C build path; other native dependencies may still
83 require a C toolchain. With neither allocator feature, the standard library
84 system allocator is used.
85 - `[profile.dev] debug = "line-tables-only"` is already set (#5246) and
86 Cargo already uses `split-debuginfo = unpacked` on macOS.
87 - `target/debug` grows past 50 GB only through accumulation across
88 feature sets and worktrees; a fresh test build is ~7 GB.
89
90 ## A0 receipts (commit 533c530b + hermeticity fixes; empty target dir)
91
92 `CARGO_TARGET_DIR=<target-dir>/compile-speed-baseline`, HTML
93 timing reports archived under
94 `backups/compile-speed-evidence-20260815/` (a0-cold-lib-test-timing.html,
95 a0-incremental-lib-test-timing.html, a0-llvm-lines-top40.txt).
96
97 | Receipt | Wall | Load (1 min) |
98 | --- | --- | --- |
99 | Cold `cargo test -p codewhale-tui --lib --locked --no-run --timings` | 127 s (user 329 s) | 8.9 |
100 | `touch crates/runtime/src/elapsed.rs` + same command | 21 s | 12.3 |
101 | `touch` + `cargo test -p codewhale-tui --lib --locked elapsed::` (the everyday loop) | 20 s (4 tests run) | 11.2 |
102 | Lib-test binary size | 357 MB (`codewhale_tui-<hash>`); links with the `__eh_frame section too large (max 16MB)` compact-unwind warning | — |
103 | `cargo check -p codewhale-tui --lib --tests` incremental after `touch` (frontend only) | 14 s | 6.2 |
104 | Incremental full lib-test build after `touch`, same conditions | 28 s | 6.2 |
105
106 Cold timing report, top units (605 units): `codewhale-tui` lib test
107 **106.0 s**, `codewhale-config` 7.9 s, `jsonschema` 5.8 s, `moxcms` 4.7 s,
108 `codewhale-protocol` 4.2 s, `tokio` 4.0 s, `rustls` 3.8 s, `schemaui`
109 3.4 s, `rmcp` 3.4 s, `h2` 3.3 s, `jsonschema` (second copy) 3.2 s,
110 `codewhale-workflow` 3.2 s, `syn` 3.1 s, `rio-vt` 3.1 s, `regex-automata`
111 3.0 s. The incremental report has exactly one non-zero unit: `codewhale-tui`
112 lib test 20.8 s. So the everyday tax is the tui crate itself, split
113 roughly half frontend (check --tests 14 s) and half codegen + link (28 s
114 total); dependencies and the linker are not where the time is.
115
116 `cargo llvm-lines -p codewhale-tui --lib`: **8,138,810 lines in 223,052
117 copies**. Largest single function is the `rust_i18n` backend closure
118 (`_RUST_I18N_BACKEND::{closure#0}`, 311,782 lines, 3.8 % of the crate on
119 its own — the 15 locale packs are compiled into a match by the `i18n!`
120 macro), then `run_event_loop` 27 k, `Engine::run_turn` 26 k,
121 `RuntimeThreadManager::monitor_turn` 16 k, then serde `Deserialize`
122 expansions for `Config`/`ProvidersConfig`/`Settings` (5–6 k each, several
123 copies per toml deserializer).
124
125 ### A0.1 dependency ratchet
126
127 `cargo metadata --locked` counted **690** packages and `cargo deny check
128 bans` warned on duplicate `fancy-regex`, `jsonschema`, `jsonschema-regex`,
129 `referencing` plus stale `jni`/`jni-sys`/`redox_syscall` skips. Cause: the
130 workspace `jsonschema` pin had been bumped to 0.49 while `schemaui` 0.12
131 (latest 0.12.4 included) still requires `^0.46`. Pinning the workspace
132 back to the 0.46 line removes the second jsonschema stack (**685**
133 packages; deny bans and advisories clean; `--locked` resolves;
134 codewhale-workflow-js 61 tests and the tui schema tests pass). Cold saving
135 is the two duplicated units (~9 s of unit time, ~3 s of wall).
136
137 ### A1 cache topology (desk-local, not committed)
138
139 New-worktree cold `cargo test -p codewhale-tui --lib --locked --no-run`,
140 same machine, back to back:
141
142 | Topology | Wall | CPU (user) | Notes |
143 | --- | --- | --- | --- |
144 | Per-worktree fresh target (control) | 127 s | 329 s | A0 |
145 | One shared `CARGO_TARGET_DIR` (warm from another worktree) | 121 s | 188 s | deps reused; every workspace crate recompiles (path-keyed); no lock waits observed; target 14 GB |
146 | `build.build-dir = ".../{workspace-path-hash}"` per workspace + warm shared `sccache` (`CARGO_INCREMENTAL=0`) | 107 s | 161 s | 73.6 % sccache hit rate (all 337 Rust dep units hit; the 125 misses are workspace crates); 2.7 GB build dir per workspace + 483 MB cache; the same command that *populated* the cache took 108 s / 157 s CPU |
147
148 Wall time is the tui crate in every topology; the topologies buy CPU
149 (~50 %), which is what matters when several checkouts build at once.
150 Recommended user-level `~/.cargo/config.toml` (adjust the two roots):
151
152 ```toml
153 [build]
154 # One build root for every checkout; each workspace gets its own subdir,
155 # so worktrees never wait on each other's target lock.
156 build-dir = "/path/to/cache/codewhale/build/{workspace-path-hash}"
157 # Optional: reuse dependency compilation across checkouts.
158 # rustc-wrapper = "sccache"
159 ```
160
161 `sccache` was installed with `brew install sccache` on this machine for the
162 measurement.
163
164 ### Public helper (A1/A5) — what `dev-test.sh` actually does now
165
166 `scripts/dev-test.sh` previously only mapped an area to `cargo test -p`.
167 It did **not** activate the measured build-dir + sccache topology, so a
168 new worktree still paid a cold compile into `./target`.
169
170 `scripts/dev-cache.sh` is the portable opt-in helper.
171 `scripts/dev-cargo.sh` and `scripts/dev-test.sh` source it.
172
173 | Class | What changed | What it is not |
174 | --- | --- | --- |
175 | **Compile-time** | `scripts/dev-test.sh` / `scripts/dev-cargo.sh` set `CARGO_BUILD_BUILD_DIR=$CODEWHALE_CACHE_ROOT/build/{workspace-path-hash}`, so concurrent worktrees do not share a Cargo lock. A leftover `./target` (Cargo still writes `CACHEDIR.TAG` there when build-dir is split) does **not** turn isolation off; `CODEWHALE_DEV_CACHE=local` keeps `./target` if you want that. Cargo older than 1.91 falls back to a per-workspace `CARGO_TARGET_DIR`. | Not a smaller rustc unit. Workspace crates still rebuild. |
176 | **Compile-time (sccache)** | `RUSTC_WRAPPER=sccache` and `SCCACHE_DIR=$CODEWHALE_CACHE_ROOT/sccache/<rustc-commit>` only when incremental is already off (`CARGO_INCREMENTAL=0` or `CODEWHALE_SCCACHE=1`) **and** `sccache` is on `PATH`. | Not enabled on the everyday incremental loop. sccache cannot cache incremental units; wrapping those builds adds overhead and 0% hits. Missing sccache is a printed fallback, not an error. |
177 | **Test-runtime** | `scripts/dev-test.sh` uses `cargo nextest run` when `cargo-nextest` is installed (`CODEWHALE_DEV_NEXTEST=0` forces libtest). Same binaries; process per test. Retries stay 0. `RUST_MIN_STACK=16MiB` is exported when unset. | Not a compile win. nextest does not run doctests; `cargo test --doc` remains a separate gate. |
178 | **Ergonomics** | `--list` and path mapping cover every workspace crate (`app-server`, `workflow-js`, …). `scripts/dev-cache.sh --status` / `--self-check` print the topology. | No product behavior change. |
179
180 Defaults never contain a machine-specific absolute path:
181
182 ```sh
183 # Portable default:
184 # ${XDG_CACHE_HOME:-$HOME/.cache}/codewhale
185 # Desk override, if you want the cache on a particular volume:
186 export CODEWHALE_CACHE_ROOT=/path/to/cache/codewhale
187
188 scripts/dev-cache.sh --self-check
189 scripts/dev-test.sh crates/command-contract/src/elapsed.rs
190 CARGO_INCREMENTAL=0 scripts/dev-cargo.sh test -p codewhale-config --lib --locked --no-run
191 ```
192
193 Hermetic script test (no rustc compile): `sh scripts/dev-cache.test.sh`.
194 When the repository build lock governs Cargo, the entry point disables the
195 external `cargowhale` governor for that process. Running both governors can
196 replace the selected cache and recreate deleted tracked files while forcing
197 their timestamps. Keep one build owner and a frozen source manifest; a build
198 helper must not change that source. This leaves the machine's global settings
199 unchanged.
200 `scripts/dev-test.sh --self-check` reports the helper's resolved cache
201 topology; its own script test, `scripts/dev-test.test.sh`, was removed in
202 `d64b9429b7`.
203
204 ### Helper verification (2026-08-15, this worktree)
205
206 Recorded after other lanes released the machine (load 3.2–5.6). rustc
207 1.97.0, cargo 1.97.0, sccache 0.17.0. `CODEWHALE_CACHE_ROOT` set to a
208 volume-local override for the run; no caches or targets were deleted.
209
210 Cargo expands `{workspace-path-hash}` to `build/d4/96565f96fb3682` for
211 this worktree. The first isolated `codewhale-config` `--no-run` created a
212 stub `./target` (`CACHEDIR.TAG`); treating that as a warm traditional
213 target made the next command recompile into `./target` (8.65 s). The
214 helper now stays isolated unless `CODEWHALE_DEV_CACHE=local` or `0`.
215
216 **Compile-time** (`scripts/dev-cargo.sh test … --locked --offline --no-run`):
217
218 | Step | Wall | Notes |
219 | --- | ---: | --- |
220 | First isolated `codewhale-config --lib --no-run` | 9.14 s (user 23.8 s) | 90 units into the hashed build-dir |
221 | Warm isolated same command (after the stub-target fix) | 0.13 s | `Finished` in 0.07 s |
222 | `touch crates/config/src/lib.rs` + isolated `--no-run` | 0.93 s | only `codewhale-config` rebuilt |
223 | First isolated `codewhale-tui --lib --no-run` | **121.5 s** (user 305 s) | 600 units; 340 MB binary; A0 empty-target was 127 s / 329 s |
224 | `touch crates/runtime/src/elapsed.rs` + isolated `--no-run` | **18.15 s** | everyday compile loop; A0 was 21 s / 19 s |
225 | `CODEWHALE_SCCACHE=1` config `--no-run` on the already-warm tree | 5.21 s then 0.14 s | wrapper and `SCCACHE_DIR=…/sccache/<rustc-commit>` set; 0 sccache hits because only workspace crates recompiled and the build-dir was not emptied |
226
227 **Test-runtime**:
228
229 | Step | Wall | Notes |
230 | --- | ---: | --- |
231 | `scripts/dev-test.sh config` (nextest, 557 tests) | run 0.479 s / real 2.40 s | includes a 0.85 s profile flip compile |
232 | `CODEWHALE_DEV_NEXTEST=0 scripts/dev-test.sh config` (libtest) | body 0.11 s / real 0.27 s | 557 tiny tests: process-per-test is slower here |
233 | `scripts/dev-test.sh crates/runtime/src/elapsed.rs` | run 0.023 s / real 2.81 s | 4 passed, 10,516 skipped; nextest filter works |
234
235 The 268 s → ~100 s nextest win remains the earlier tui-unit-suite receipt.
236 Config is too small for that win; nextest is still the right default for
237 unfiltered crate/workspace runs.
238
239 **Ergonomics:** `sh` and `dash` both passed `dev-cache.test.sh` (22) and
240 `dev-test.test.sh` (27) at the time; `dev-test.test.sh` has since been removed
241 (`d64b9429b7`). Missing sccache is a fallback. `--list` covers
242 every workspace crate.
243
244 ### A2 nextest in CI
245
246 `cargo test --workspace --all-features --locked --doc` inventoried
247 **3 passing / 8 ignored doctests across 21 crates**. All 8 ignored fences
248 were in codewhale-tui, which has no runnable doc example and now sets
249 `doctest = false`, so its doc pass is skipped (the new inventory is not
250 re-measured yet). CI keeps the doc run as a separate step next to
251 `cargo nextest run --workspace --all-features --locked --profile ci`.
252
253 ### A3/A4 (not adopted, measured)
254
255 Frontend and codegen split the tui unit roughly evenly (14 s / 14 s
256 incremental); the linker is a small part of that and dependencies are
257 already warm after the first build, so `[profile.dev.package."*"]
258 opt-level = 1` (paired result above), `-Wl,-dead_strip`, and other
259 `RUSTFLAGS` stay out of the repo (they would apply to shipped profiles);
260 `split-debuginfo` is already `unpacked` on macOS.
261
262 ## Peak memory (why OHOS/Windows builds see two ~4 GB rustc processes)
263
264 Sampled `ps -o rss` once a second for every rustc under this lane's target
265 dir (`backups/compile-speed-evidence-20260815/rss-sample.sh`,
266 `mem-incremental.log`, `mem-cold-cgu.log`); one rustc per row.
267
268 | Unit | Mode | Peak RSS | Wall | Load |
269 | --- | --- | --- | --- | --- |
270 | `codewhale-tui` lib (dev) | incremental, cgu 256 | 3.3 GB | 12–14 s | 6.3 |
271 | `codewhale-tui` lib test | incremental, cgu 256 | 6.0 GB | 21–28 s | 6.3 |
272 | `codewhale-tui` lib (dev) | non-incremental (`CARGO_INCREMENTAL=0`), cgu 16 | **6.0 GB** | 78 s | 5.5 |
273 | `codewhale-tui` lib test | non-incremental, cgu 16 | **8.0 GB** | 105 s | 5.5 |
274 | `codewhale-tui` lib test | non-incremental, `codegen-units = 4` | 6.1 GB (−24 %) | 145 s (+38 %) | 5.5 |
275 | `codewhale-tui` lib test | non-incremental, `codegen-units = 1` | 7.8 GB (−3 %) | 161 s (+53 %) | 5.5 |
276 | next-largest units (codewhale-config, rmcp, tokio, schemaui, codewhale-workflow) | either | 0.4–0.7 GB | — | — |
277
278 So a plain `cargo build -p codewhale-tui` needs ~6 GB for one rustc, the
279 unit-test build ~8 GB, and `cargo test --workspace` (or `--all-targets`)
280 schedules the lib and lib-test units of the tui crate concurrently with
281 the CLI, which is exactly the "two rustc processes at ~4 GB each" a
282 community member reported while cross-compiling for OHOS on Windows (RSS
283 accounting differs by OS; the shape is the same). The inline test modules
284 add ~2 GB (+33 %) to the crate's peak; generic bloat is the driver on both
285 axes (8.1 M LLVM lines, `rust_i18n` closure 312 k, serde `Deserialize`
286 expansions for the config structs). Fewer codegen units trade a little
287 peak for a lot of wall time and are not adopted by default.
288
289 ### Low-memory build recipe (machines with < 16 GB, cross-builds)
290
291 ```bash
292 # One rustc at a time: the tui lib and its unit-test build never overlap.
293 export CARGO_BUILD_JOBS=1 # or: cargo build -j1 ...
294 # Only the crate you are working on, only its library:
295 cargo build -p codewhale-tui
296 cargo test -p codewhale-tui --lib -- <filter>
297 # Do NOT use --workspace/--all-targets on a small machine; run crates one
298 # at a time (scripts/dev-test.sh <area> picks the narrowest command).
299 # Optional, if 8 GB for the unit-test build is still too much (slower):
300 export CARGO_PROFILE_DEV_CODEGEN_UNITS=4 # ~6 GB peak, ~+40 % wall
301 # Cross-builds (e.g. OHOS) inherit the same numbers: add -j1 to the
302 # cargo/ohrs invocation and build the release profile, which peaks lower
303 # than the unit-test build because it carries no test modules.
304 ```
305
306 ### B1 (megatest peel) — audited, not landable under the constraints
307
308 The six largest inline test files (tui/ui/tests.rs 22.1 k lines / 643
309 tests, tools/subagent/tests.rs 18.7 k / 448, core/engine/tests.rs 17.8 k /
310 358, config/tests.rs 12.7 k / 393, runtime_threads/tests.rs 9.3 k / 141,
311 runtime_api/tests.rs 9.1 k / 151) reference crate internals 826 / 226 /
312 627 / 85 / 152 / 217 times respectively (`crate::llm_client::mock`,
313 `crate::test_support::{EnvVarGuard, lock_test_env}`,
314 `core::engine::mock_engine_handle`, `crate::tui::app::App`, …), and the
315 codewhale-tui library exposes four `pub` items in total. Every one of them
316 is white-box; none can move to `crates/tui/tests/` without making the
317 module tree public, which this lane was told not to do. The lever this
318 would have bought — the ~2 GB / ~35 s that the test modules add to the
319 lib-test unit — needs a decision first: either a `#[doc(hidden)] pub mod
320 test_api` (a deliberately public, unstable surface for the ~30 symbols the
321 black-box subsets use) or accepting that the unit suite stays inside the
322 crate. Recorded here rather than done.
323
324 ### B2 landed (leaf types out of codewhale-tui)
325
326 | Move | Lines out of tui | Consumers changed |
327 | --- | --- | --- |
328 | `core/tool_parser.rs` → `codewhale_core::tool_parser` | 662 | 0 (re-export; integration harness imports instead of `#[path]`) |
329 | `tls.rs` → `codewhale_release::tls` | 21 | 0 (`use codewhale_release::tls;` at the crate root) |
330 | `AppMode` (+ pure impl) → `codewhale_config::AppMode`; localized picker strings stay as `AppModeUi` | ~150 | 3 files import the trait |
331 | `ApprovalMode` (+ pure impl) → `codewhale_execpolicy::ApprovalMode` | ~60 | 0 (re-export) |
332
333 Together ~0.9 k of the crate's 746 k lines: correct dependency direction
334 established, no measurable change to the tui unit's time or RAM yet (the
335 lib-test peak above, 8.0 GB, was sampled after these moves). Not moved,
336 with the reason: `ReasoningEffort` — its impl takes the TUI-defined
337 `ApiProvider` (`crates/tui/src/config.rs`) and calls
338 `crate::config::is_exact_*_k3_route` / `crate::provider_lake`, so
339 `ApiProvider` has to move first (B3, below); `approval/policy.rs` (risk
340 classify) depends on `command_safety` and `auto_review`; `hashing.rs` is
341 15 lines of sha2 wrappers with 53 call sites and no compile-time value on
342 its own; the uncompiled `core/runtime_contract/{budget,context,ledger,
343 manifest,profile,progress,retry,terminal,work}.rs` have zero consumers and
344 zero build cost (`core/mod.rs` documents them as staged scaffolding,
345 TUI-DOG-017) — left as they are.
346
347 ### B3 order
348
349 1. `ApiProvider` + the exact-route helpers (`is_exact_*_route`) out of
350 `crates/tui/src/config.rs` into codewhale-config, unblocking
351 `ReasoningEffort`. **Not started, and now the critical path** — see
352 item 4.
353 2. **Landed.** `localization` + `locales/*.json` →
354 `codewhale-localization` (the 312 k-line `rust_i18n` closure left the
355 tui unit; locale-only edits no longer rebuild the TUI).
356 3. **Landed.** `palette` → `codewhale-palette`; `command_safety` →
357 `codewhale-execpolicy` (it already owned `ApprovalMode`, so that move
358 removes a dependency edge rather than adding one).
359 4. `client/` (provider wire adapters) → `codewhale-client`: **blocked on
360 item 1, not merely ordered after it.** With doc comments and
361 `#[cfg(test)]` blocks excluded, `client` still has 20 production
362 `crate::` edges. Three of them are hard:
363 - `crate::config` — `Config`, `ProvidersConfig`, `ProviderConfig`,
364 `TuiConfig`, `ApiProvider`, `RetryPolicy`, `validate_route`,
365 `wire_model_for_provider_route` and ~130 provider base-URL / model-id
366 constants. `crates/tui/src/config` is 29.7 k lines and itself reaches
367 `config_persistence`, `oauth`, `credentials`, `tui`, `fleet`,
368 `goal_loop`, `sandbox`, `lsp` … in production, so it cannot follow
369 `client` out.
370 - `crate::tools` ⇄ `client` is a genuine cycle: `client` uses
371 `tools::schema_sanitize`, `tools::large_output_router` and
372 `tools::truncate`, while `tools/{spec,review,registry,rlm,verify,
373 speech,fim,web_search,web/backend,subagent/advisor}.rs` use
374 `client::{CodewhaleClient, ProviderNativeSearchClient,
375 ProviderNativeSearchRequest, SpeechSynthesisRequest,
376 RemoteControlInferencePermit}`.
377 - `crate::core` ⇄ `client` is the same shape: `client` uses
378 `core::events::bounded_tool_projection_warning_names`, while
379 `core/{engine,engine/preview,engine/dispatch,engine/turn_loop,
380 engine/reviewer,protocol_parity}.rs` use `client::{CodewhaleClient,
381 PreparedOutboundRequest, canonical_json, parse_usage,
382 is_reasoning_replay_placeholder, redact_url_for_display}`.
383 Item 1 is therefore the whole precondition: move `ApiProvider`, the
384 exact-route helpers and the provider constants into codewhale-config
385 first, then re-measure the `tools` and `core` cycles.
386 5. **Landed as the tractable part of item 4.** `models` +
387 `model_catalog` → `codewhale-models` (1,835 lines, 140 consumer files).
388 These sit directly under `client` on its dependency spine, had exactly
389 one production edge between them and none to the rest of the TUI, and
390 `models` was already half a re-export facade over
391 `codewhale_core::{request, role}`.
392 6. Then `fleet/`, `tools/`, `core/engine` — each behind the crate boundary
393 its tests already respect, measured with the A0 table.
394
395 ## Two builds per machine
396
397 `scripts/dev-cargo.sh` and `scripts/dev-test.sh` hold one of
398 `CODEWHALE_BUILD_SLOTS` (default 2) machine-wide build slots
399 (`<cache root>/build.lock`, then `build.lock.1`, ..., via
400 `scripts/build-lock.py`) while Cargo compiles. A `cargo test` or
401 `cargo nextest run` compiles with `--no-run` under the slot, then runs its test
402 binaries without it; a doctest run (`--doc`) holds it throughout, because
403 rustdoc compiles each doctest as it runs. Cargo's own lock is
404 per target directory, so agents building into different target dirs once ran
405 unbounded and exhausted memory. Two slots is the founder's 2026-10-25 setting
406 for a 36 GB M4 Max: the tui unit-test rustc peaks near 8 GB, and one huge
407 crate keeps most cores idle during its front end, so a second build roughly
408 doubles throughput. A build beyond the slot count waits and prints every
409 holder. Set `CODEWHALE_BUILD_SLOTS=1` on a smaller machine,
410 `CODEWHALE_BUILD_LOCK=0` to skip the lock, or `CODEWHALE_BUILD_LOCK_FILE` to
411 name the slot-0 lock file. A self-hosted CI runner on the same machine joins the lock when its
412 `.env` sets `CODEWHALE_BUILD_LOCK_FILE` to the same path: the macOS suite's
413 Build job (`ci-test-suite.yml`) then holds it from the first test build through
414 the doctests, with `CARGO_BUILD_JOBS=6`. That job and its single shard run on
415 the `codewhale-mac` runner only for a `ci.yml` dispatch in this repository with
416 the `macos_runner` input `self-hosted`, or `default` while the
417 `CW_SELF_HOSTED_MAC` repository variable is `true`.
418 `gh variable set CW_SELF_HOSTED_MAC -R codewhale-hq/Codewhale --body false`
419 sends every run back to hosted macOS (three shards, one Cargo job). There the
420 job keeps its target, `CARGO_HOME` and `RUSTUP_HOME` under the runner's work
421 directory (`ci-state/`), starts an empty target when `rustc -V` changes or the
422 disk has under 100 GiB free, and uses a per-job `TMPDIR` it removes at the end.
423 The lock is advisory: Cargo started
424 directly, outside these scripts, does not take it, and on platforms without
425 `fcntl` (Windows) the build runs unlocked after a warning.
426
427 ## What changed (this lane)
428
429 1. **`scripts/dev-cache.sh` / `scripts/dev-cargo.sh` activate the measured
430 isolated build-dir topology** from `scripts/dev-test.sh`. New worktrees
431 no longer compile into a private cold `./target` unless the helper is
432 disabled. sccache is opt-in and incremental-gated. Script self-checks
433 live in `scripts/dev-cache.test.sh` and `scripts/dev-test.sh --self-check`
434 (`scripts/dev-test.test.sh` was removed in `d64b9429b7`).
435 2. **`cargo nextest` is supported and documented** (`.config/nextest.toml`).
436 Same test binaries, one process per test, so the tui unit suite runs in
437 ~100 s instead of ~270 s here and slow or hanging tests are named instead
438 of stalling the binary. The PTY binary is pinned to one test at a time
439 (it drives pseudo-terminals and shared mock servers; today it serializes
440 on an in-process mutex, which nextest's process-per-test model would
441 otherwise bypass), and the integration binary that spawns the real
442 `codewhale` executable is capped at four concurrent tests so its 30 s
443 start-up budgets survive a fully loaded machine.
444 `cargo test --workspace --all-features --locked` remains the
445 authoritative gate; nextest is the local loop.
446 3. **Three tests depended on test order** and only passed because another
447 test in the same process had installed the rustls crypto provider first:
448 `codewhale-tui mcp::sse::endpoint_tests::message_before_endpoint_is_rejected_instead_of_buffered`,
449 `codewhale-app-server tests::failed_config_set_keeps_the_stdio_bridge`,
450 and `tests::successful_config_set_still_invalidates_the_stdio_bridge`.
451 Each now installs the provider itself, exactly as production does at
452 startup. No runtime code changed.
453 4. **CONTRIBUTING.md has a "Fast local loop" section**: `scripts/dev-cargo.sh`
454 / `scripts/dev-test.sh` first, targeted `-p` filters, nextest, isolated
455 per-worktree build dirs, and the optional accelerators below. A shared
456 `CARGO_TARGET_DIR` is documented only for serialized trunk work.
457
458 ## Measured and deliberately not adopted
459
460 - `[profile.dev.package."*"] opt-level = 1` (dependencies optimized once,
461 workspace crates untouched). Paired measurement, back to back, same
462 target layout: cold `cargo test -p codewhale-tui --lib --no-run` from an
463 empty target went 148 s → 193 s (user 347 s → 778 s); the tui unit suite
464 under nextest went 96 s → 81 s; incremental rebuilds are unchanged. A
465 ~15 % faster test run is not worth a 2.2× more expensive cold build for
466 people trying to build Codewhale for the first time. Contributors who
467 mostly re-run tests can opt in locally by adding that table to a
468 user-level `~/.cargo/config.toml` `[profile.dev.package."*"]` section.
469 - Extra `RUSTFLAGS`/linker flags in a repo `.cargo/config.toml`
470 (`-no_deduplicate`, alternative linkers). Rustflags apply to every
471 profile and would change shipped binaries; the macOS system linker is
472 already `ld-prime`, and the measured incremental link cost is inside the
473 12–19 s incremental numbers above. Documented as optional local
474 accelerators instead.
475
476 ## Deferred: split `codewhale-tui`
477
478 The single lever left that changes the shape of the numbers is splitting
479 the crate so a change to a leaf module does not re-typecheck 600k lines
480 and re-link a 357 MB test binary. Mechanical candidates, in dependency
481 order (each already only depends on `codewhale-config`/`codewhale-paths`
482 plus third-party crates, and each is consumed through a single module
483 path today):
484
485 | Candidate crate | From | Why it is a clean cut | Consumers to re-export from |
486 | --- | --- | --- | --- |
487 | `codewhale-glyphs` | `crates/tui/src/tui/glyphs.rs` | Constant tables + pure fns; no crate-internal deps. | `crate::tui::glyphs` |
488 | `codewhale-i18n` | `crates/localization/src/lib.rs` + `crates/localization/locales/*.json` | The `rust_i18n::i18n!` macro compiles all 15 packs into whichever crate hosts it; moving it out means locale-only edits no longer rebuild the TUI. `MessageId` is a plain enum. | `crate::localization` |
489 | `codewhale-mcp-transport` | `crates/engine/src/mcp/{sse,stdio,external_import}.rs` | Already talks to `codewhale-mcp`; the reviewed-launch binding is the only tui coupling. | `crate::mcp` |
490
491 Rules for the split: pure moves plus `pub use` re-exports at the old
492 paths, no behavior change, one crate per PR, each PR measured with the
493 table above (cold build, incremental build, incremental test build,
494 `cargo test -p codewhale-tui --lib --no-run`). Expected win: the tui
495 frontend time drops with lines removed; the test-binary link is unchanged
496 until the tests that live with those modules move with them.
497
498 ## Hosted CI: keep the rust-cache when a test fails (2026-11-16)
499
500 `Swatinem/rust-cache@v2` saves in a post step guarded by
501 `post-if: success() || env.CACHE_ON_FAILURE == 'true'`
502 ([action.yml](https://raw.githubusercontent.com/Swatinem/rust-cache/master/action.yml)).
503 A Test leg that compiles everything and then fails one test therefore
504 throws away a complete dependency build. `ci.yml` now sets
505 `cache-on-failure: true` on the Safety gate, Lint and Test rust-cache steps;
506 `save-if` stays main-only and `cache-workspace-crates` stays at its default
507 `false`, so entries remain dependencies-only.
508
509 Before the change, measured from `gh api actions/runs/<id>/jobs` and the Test
510 job logs of main `workflow_dispatch` runs 43879207835, 43879706605,
511 43896698899, 43932443327 and 43935781907 (hosted runners; raw data and
512 `extract.py` in `CW/artifacts/rust-20261116/b2/`):
513
514 | | Failed Test legs (n=10) | Linux leg, cache restored (n=2) | Linux leg, no cache (n=3) |
515 | --- | --- | --- | --- |
516 | Post rust-cache step | skipped 10/10 | ran, "Cache up-to-date" | skipped 3/3 |
517 | Dependency `Compiling` lines | 649-732 | 26 | 732 |
518 | Binary build step (min) | | 7.2, 12.3 | 14.1-15.7 |
519 | `Run tests` step (min) | | 40.2, 52.5 | 57.2-64.2 |
520
521 `gh api actions/caches?key=v0-rust` at 21:35Z listed Test entries for Linux
522 (0.90 GB) and Windows (0.82 GB) only; no macOS Test entry existed, and both
523 green macOS legs logged `Unable to reserve cache ... another job may be
524 creating this cache`. Repo cache usage was 10.57 GB across 1679 entries.
525
526 After: not yet measured. `save-if` is main-only, so the effect appears only
527 on main runs after this lands. Check the same three things there: a Test
528 entry per OS after a red main run, `Restored from cache key` on the next leg,
529 and the median binary-build and `Run tests` minutes.
530
531 ## Optional accelerators (not required)
532
533 - `cargo install cargo-nextest` — see above.
534 - `scripts/dev-cargo.sh` / `scripts/dev-test.sh` — isolated `build-dir`
535 per worktree plus optional sccache. Override the root with
536 `CODEWHALE_CACHE_ROOT`; do not commit a machine path.
537 - One shared `CARGO_TARGET_DIR` only for serialized trunk work (two
538 cargos on the same target flock). Prefer the helper above.
539 - `sccache` as `RUSTC_WRAPPER` caches dependency compilation across clean
540 checkouts when `CARGO_INCREMENTAL=0`, and matches what CI does
541 (`.github/workflows/ci.yml` uses `mozilla-actions/sccache-action` plus
542 `Swatinem/rust-cache`).
543
543 lines MARKDOWN