| 1 | # TUI deconstruction: crates as agent work partitions |
| 2 | |
| 3 | The deliverable is a workspace where one agent, possibly sandboxed, can be |
| 4 | handed one crate plus the contracts it depends on, change it, prove it with that |
| 5 | crate's own tests, and return a patch that cannot collide with any other |
| 6 | agent's patch. The terminal UI becomes one client of a headless runtime along |
| 7 | the way, but the organizing question for every boundary is: **where can an |
| 8 | agent work without negatively affecting another agent?** |
| 9 | |
| 10 | A lower `crates/tui` line count alone does not establish the split. A crate |
| 11 | boundary counts when it lets an agent build, test and land work without the |
| 12 | rest of the workspace. |
| 13 | |
| 14 | The founder's 2026-10-31 coarse split sets the current |
| 15 | sequence and crate sizes. [CW-7701](https://linear.app/shannonlabs/issue/CW-7701) |
| 16 | owns its Engine extraction; CW-6129 was canceled during replanning and its |
| 17 | receipts remain historical evidence. This document |
| 18 | describes later internal boundaries. |
| 19 | Dated audits below are rationale, not current assignments or permission to |
| 20 | create another checkout or build cache. |
| 21 | |
| 22 | The terminal UI becomes a presentation adapter over the existing headless |
| 23 | runtime. A boundary counts when it moves real ownership, migrates every |
| 24 | consumer and reduces dependency/rebuild closure. Moving a monolith or adding a |
| 25 | facade does not establish that result. The earlier 10,000-production-line |
| 26 | target and 50,000 ceiling are superseded by the coarse plan's approximately |
| 27 | 400–500k-line top-level crates; further splits follow measured ownership and |
| 28 | build needs inside those crates. |
| 29 | Production lines, physical lines, SCCs and build timings measure different things. |
| 30 | |
| 31 | ## Audited baseline (2026-10-09, `bd2a7b49c`) |
| 32 | |
| 33 | Physical Rust lines, tests included. These are ownership clues, not production |
| 34 | LOC. Regenerate before relying on them. |
| 35 | |
| 36 | | Source | Lines | |
| 37 | | --- | ---: | |
| 38 | | Whole workspace (29 crates) | ~1.47M | |
| 39 | | `crates/tui` | 1,280,430 in 1,077 files (~87%) | |
| 40 | | of which test code in test files / test dirs | ~331k | |
| 41 | | `crates/tui/src/tui` (terminal UI proper) | 314,291 | |
| 42 | | `crates/engine/src/tools` | 204,115 | |
| 43 | | `crates/engine/src/core` (engine, turn loop) | 89,384 | |
| 44 | | `crates/tui/src/commands` | 71,629 | |
| 45 | | `crates/tui/src/lib.rs` + `lib/` | 24,827 | |
| 46 | | Every other crate | under 54k each; most under 10k | |
| 47 | |
| 48 | Crates named for a subsystem whose implementation still lives in the TUI: |
| 49 | `tools` (1.6k; the implementations are `engine/src/tools`, 204k), `mcp` (109 |
| 50 | lines; `tui/src/mcp*` is 26k), `core` (5.8k; the engine is `engine/src/core`), |
| 51 | `agent` (1.6k model registry with one TUI consumer, beside the TUI's own |
| 52 | catalog). `cli` depends on `tui` for `run`, `route_preferences`, `config_keys`; |
| 53 | `tui` depends on `app-server` while the Runtime API |
| 54 | (`runtime_api`, 61k) lives inside `tui`. |
| 55 | |
| 56 | `scripts/split/module_graph.py --report` puts the runtime closure (everything |
| 57 | reachable from the runtime roots without passing through UI modules) at **116 |
| 58 | modules, about 813k lines**. A grep-based module graph (production paths, |
| 59 | approximate test stripping) shows the non-UI part of it as **one strongly |
| 60 | connected component of 85 modules**. Moving that component into one crate |
| 61 | would relocate the monolith, not partition it. |
| 62 | |
| 63 | ### Where agents collide |
| 64 | |
| 65 | Commits touching `crates/` from 2026-08-10 to 2026-10-09: 3,380. |
| 66 | |
| 67 | | Area | Commits | Why it collides | |
| 68 | | --- | ---: | --- | |
| 69 | | `tui/src/tui/ui` (event loop, dispatch, handlers) | 621 | one controller for every surface | |
| 70 | | `engine/src/core/engine` | 462 | features wire themselves into the turn loop | |
| 71 | | `tui/src/commands/groups` | 321 | every slash command in one tree | |
| 72 | | `tui/src/runtime_api` | 289 | routes, DTOs and logic in one module | |
| 73 | | `engine/src/tools/subagent` | 284 | | |
| 74 | | `crates/config` + `tui/src/config` | 283 + 199 | each feature adds fields to one schema | |
| 75 | | `tui/src/tui/app` | 275 | | |
| 76 | | `tui/src/lib.rs` | 270 | `mod` list plus composition root | |
| 77 | |
| 78 | Central registration lists cause much of this: `tools/registry.rs` has one |
| 79 | `with_*_tools()` builder per tool family, `config.rs` (13k) holds every |
| 80 | feature's fields, `lib.rs` lists every module, `commands/groups/mod.rs` lists |
| 81 | every group. Adding a feature edits all of them. |
| 82 | |
| 83 | ### Reference workspaces |
| 84 | |
| 85 | Local snapshots, inspected 2026-10-09; not claims about current upstream. |
| 86 | |
| 87 | | | Crates | Lines | Largest | |
| 88 | | --- | ---: | ---: | --- | |
| 89 | | Codex `codex-rs` (`bdda5da56c`, 2026-07-30) | 126 | 1.23M | core 294k, tui 235k, app-server 126k | |
| 90 | | Grok Build (`500129c`, 2026-07-29) | 76 | 1.46M | pager 462k, shell 362k, tools 127k | |
| 91 | |
| 92 | Patterns adopted here: |
| 93 | |
| 94 | - **Contract crate apart from implementation crate** (Grok): |
| 95 | `xai-tool-types` -> `xai-tool-protocol` -> `xai-grok-tools-api` ahead of the |
| 96 | heavy `xai-grok-tools`; likewise `config-types`, `workspace-types`, |
| 97 | `sampling-types`. `tools-api` exists so "host services ... must not depend on |
| 98 | the tools implementation crate." |
| 99 | - **Feature crates that plug in through contributor traits** (Codex `ext/`): |
| 100 | `extension-api` defines `ToolContributor`, `ConfigContributor`, |
| 101 | `ContextContributor`, `ThreadLifecycleContributor` and others; goal, |
| 102 | memories, web-search, skills, MCP, guardian and image generation are each a |
| 103 | crate. |
| 104 | - **Tiny utility crates** (Codex `utils/*`: `output-truncation`, `pty`, |
| 105 | `path`, `string`, `sleep-inhibitor`). |
| 106 | |
| 107 | Patterns rejected, because each defeats agent isolation: |
| 108 | |
| 109 | - Codex `core` depends on 65 workspace crates, including three `ext` crates |
| 110 | that themselves depend on `core`. The hub remains; every feature agent still |
| 111 | rebuilds it. |
| 112 | - Grok's `xai-grok-tools` implementation crate has 13 dependent crates, so a |
| 113 | tool edit rebuilds pager, shell and agent. |
| 114 | - Both keep 300k+ line crates. Crate count is not the goal; the rules below are. |
| 115 | |
| 116 | ## Rules |
| 117 | |
| 118 | 1. **Feature crates are leaves.** Nothing depends on a feature or adapter |
| 119 | implementation crate except the composition root (`codewhale-cli`, or a |
| 120 | thin host crate it owns). An edit there rebuilds that crate and the final |
| 121 | binary, never a sibling. |
| 122 | 2. **Contract crates are small, logic-free and rarely changed.** They carry |
| 123 | the high fan-in, so a contract edit is the one change that affects other |
| 124 | agents. Contract changes are their own slices, serialized, and each lists |
| 125 | the dependents it rebuilt. |
| 126 | 3. **No central lists.** Features register through contributor traits, and |
| 127 | the composition root names each crate once; adding a feature is one |
| 128 | appended line there. Adopting the contributor traits deletes the |
| 129 | `with_*_tools` builders, the per-feature `config.rs` fields and the command |
| 130 | group lists in the same migration. Do not run both systems. |
| 131 | 4. **Each crate owns its tests.** Unit tests move with their code; a crate's |
| 132 | tests build with only that crate's dependency closure. Cross-crate behavior |
| 133 | is tested at the host level. |
| 134 | 5. **Claims name exact paths and shared compiler inputs.** The target is a |
| 135 | crate that can be owned independently. Until a large crate is split, |
| 136 | coordinate disjoint files and one frozen build batch instead of creating |
| 137 | another checkout. |
| 138 | 6. **Dependencies point down only** (tiers below). Cargo enforces it; an |
| 139 | upward import is a compile error, not a ratchet count. |
| 140 | |
| 141 | ## Future sandbox qualification |
| 142 | |
| 143 | Small dependency closures should eventually permit a confined agent to change |
| 144 | one owner while consuming read-only contracts. That is a product qualification |
| 145 | target, not a separate local development workflow or authorization to create |
| 146 | cloud resources. Preserve path confinement, cancellation, credential boundaries |
| 147 | and the existing Engine authority in any such journey. |
| 148 | |
| 149 | Current work uses the canonical checkout on main, exact path claims and one |
| 150 | frozen batch per shared native input set. Use the existing shared build cache |
| 151 | and two-slot lock. Do not create sandbox copies, per-agent target directories, |
| 152 | worktrees or alternate integration lanes to bypass a conflict or build owner. |
| 153 | Contracts, workspace manifests, Cargo.lock and composition changes require the |
| 154 | same coordinated source review as their consumers. Run focused existing checks |
| 155 | locally; no new tests or code comments are implied by this design. |
| 156 | |
| 157 | ## Target map |
| 158 | |
| 159 | The historical unit-by-unit crate assignment and wave extraction notes |
| 160 | (2026-10-31) were retired once workspace crate extraction moved to live |
| 161 | manifests (`docs/reference/crates.md`). The protocol crate this |
| 162 | plan called `runtime-wire` is named `app-server-protocol` (founder, App Server |
| 163 | v2, 2026-10-31). |
| 164 | |
| 165 | Names are package names (`codewhale-` prefix omitted). Sizes are today's |
| 166 | source for those modules, tests included, measured at `bd2a7b49c`. Existing |
| 167 | crates are reused where their current content already fits the role. |
| 168 | |
| 169 | Every crate below `services` is a leaf in the sense of rule 1 unless it is a |
| 170 | contract. New crates live under grouped directories: |
| 171 | `crates/{contracts,util,services,providers,runtime,features,tui,hosts,test}/`. |
| 172 | Existing crates move into those directories in one rename-only change. |
| 173 | The map is a dated destination sketch: the founder's 2026-10-25 S7=(c) |
| 174 | decision below places the TUI configuration and OAuth cluster in the |
| 175 | existing `codewhale-runtime`, superseding the `config` and `credentials` |
| 176 | rows for P3. Every eventual crate targets about 10k production lines and |
| 177 | stays below 50k. |
| 178 | |
| 179 | ### Contracts (`crates/contracts/`) |
| 180 | |
| 181 | | Crate | Role | Source | |
| 182 | | --- | --- | --- | |
| 183 | | `protocol` | shared wire and domain records | exists | |
| 184 | | `core` | request construction, journal, ids, fragments, prefix cache | exists (5.8k) | |
| 185 | | `models` | provider wire DTOs and the model-client trait | exists; trait added | |
| 186 | | `tools` | tool contract: `ToolResult`, `ToolError`, `ToolCapability`, specs, plus capability ports (spawn agent, schedule work, work graph) that tools call and the runtime implements | exists (1.6k); ports added | |
| 187 | | `ext` | contributor traits: tool, config section, context/prompt fragment, command, thread lifecycle, approval review | new; replaces the central lists | |
| 188 | | `config-types` | config schema and route identity, no I/O | split from `config` (53k) | |
| 189 | | `command-contract` | portable command metadata and results | exists | |
| 190 | | `app-server-protocol` | Client Runtime wire DTOs and schema/SDK exports; Engine-internal `Op`/`Event` remain in Engine | extracted client contracts; native records/authority stay with their owner | |
| 191 | |
| 192 | ### Utilities (`crates/util/`) |
| 193 | |
| 194 | `paths`, `sanitize`, `release`, `build-support`, `localization` (exist); |
| 195 | `util-truncate` (from `tools::truncate`); `util-fslock` (from |
| 196 | `runtime_threads::try_lock_file_exclusive`); `util-hash` (`fast_hash`, |
| 197 | `hashing`, `regex_cache`, `safe_label`, now in `crates/runtime`); |
| 198 | `util-schema` (`tools::schema_sanitize`, 3k); `util-pty`. |
| 199 | |
| 200 | ### Services (`crates/services/`) |
| 201 | |
| 202 | | Crate | Source | Lines | |
| 203 | | --- | --- | ---: | |
| 204 | | `config` | loading, persistence, keys (`config`, `settings`, `config_persistence`, `config_keys`) | 41k + crate | |
| 205 | | `execpolicy` | exists; absorbs `core::authority::auto_review` and `tool_matches_any_rule` | 13k + moved | |
| 206 | | `secrets` | exists | 3k | |
| 207 | | `credentials` | `oauth`, `credentials` | 11k | |
| 208 | | `state` | exists; absorbs checkpoint records from `runtime_handoff` | 4k + moved | |
| 209 | | `session-store` | `session_manager`, `runtime_threads` store, `session_*` | 59k | |
| 210 | | `sandbox` | `sandbox` | 7k | |
| 211 | | `billing` | `pricing`, `route_billing`, `cost_status` | 15k | |
| 212 | | `telemetry` | exists | 5k | |
| 213 | |
| 214 | ### Providers (`crates/providers/`) |
| 215 | |
| 216 | | Crate | Source | Lines | |
| 217 | | --- | --- | ---: | |
| 218 | | `model-client` | shared dispatch, streaming, retry (`llm_client`, `client` core) | ~25k | |
| 219 | | `provider-openai-compat` | `client/chat*` | ~10k + tests | |
| 220 | | `provider-anthropic` | `client/anthropic.rs` | ~2.5k + tests | |
| 221 | | `provider-responses` | `client/responses*` | ~3k + tests | |
| 222 | | `provider-local` | `local_ollama` | small | |
| 223 | | `model-catalog` | live catalog, routing, inventory, receipts, tuning; absorbs `agent` | 24k | |
| 224 | |
| 225 | ### Runtime (`crates/runtime/`) |
| 226 | |
| 227 | | Crate | Source | Lines | |
| 228 | | --- | --- | ---: | |
| 229 | | `engine` | `Engine::run_turn` and the tool executor only | 89k today; shrinks as contributors leave | |
| 230 | | `context` | `compaction`, `prompts`, `project_context*`, `runtime_handoff`, `request_manifest`, `prompt_zones`, `context_budget` | 21k + moved | |
| 231 | | `approvals` | `core::authority` minus policy data | ~5k | |
| 232 | | `agents` | `tools/subagent`, `fleet`, `agent_roster`, `worker_profile` | 107k; split again by its own seams | |
| 233 | | `tasks` | `task_manager`, `automation_manager`, `work_graph` | 22k | |
| 234 | | `runtime` | the session host composing the above; implements the `tools` ports | exists (8k) | |
| 235 | |
| 236 | `BASE_PROMPT` and `Engine::run_turn` keep their single-owner contracts: one |
| 237 | base prompt in `context`, one turn loop in `engine`. `agents` adapts children |
| 238 | to that loop; it does not own another one. |
| 239 | |
| 240 | ### Features (`crates/features/`, all leaves) |
| 241 | |
| 242 | | Crate | Source | Lines | |
| 243 | | --- | --- | ---: | |
| 244 | | `tools-fs` | `tools/file`, `apply_patch` | 12k | |
| 245 | | `tools-shell` | `tools/shell`, `shell_dispatcher` | 16k | |
| 246 | | `tools-web` | `tools/web`, `web_search`, `web_run` | 17k | |
| 247 | | `tools-github` | `tools/github` | 4k | |
| 248 | | `tools-review` | `tools/review` | 3k | |
| 249 | | `goal` | `tools/goal`, `operate`, `goal_loop` | 5k + moved | |
| 250 | | `workflow`, `workflow-js` | exist; absorb `tools/workflow` (16k) | | |
| 251 | | `memory` | exists; absorbs `native_memory`, `tools/remember` | | |
| 252 | | `mcp` | exists; absorbs the TUI MCP pool | 26k | |
| 253 | | `skills` | `skills` | 14k | |
| 254 | | `plugins` | `plugins` | 28k | |
| 255 | | `extension-host` | `extension_host` | 31k | |
| 256 | | `hooks` | exists; absorbs `engine/src/hooks` | 9k | |
| 257 | | `lsp` | `lsp` | 4k | |
| 258 | | `rlm` | `rlm`, `repl` | 5k | |
| 259 | | `snapshot` | `snapshot` | 6k | |
| 260 | | `voice`, `vision` | `voice`; `vision`, `image_attach` | 1k, 3k | |
| 261 | | `remote-control` | `remote_control`, `runtime_chat_relay`, `remote_setup` | 13k | |
| 262 | | `integrations` | `integrations` | 5k | |
| 263 | |
| 264 | Each feature contributes its tools, config section, slash-command logic and |
| 265 | context fragments through `ext`. Its terminal presentation, if any, is a view |
| 266 | crate in the TUI tier. |
| 267 | |
| 268 | ### TUI (`crates/tui/`) |
| 269 | |
| 270 | | Crate | Source | Lines | |
| 271 | | --- | --- | ---: | |
| 272 | | `tui-kit` | widgets, markdown render, textarea, mouse, `palette`, the view trait | 30k + palette | |
| 273 | | `tui` | event loop and app-state reducer (`tui/ui`, `tui/app`) | 97k; split reducer from render first | |
| 274 | | `tui-view-transcript` | `tui/history` | 12k | |
| 275 | | `tui-view-work` | `tui/work_surface` | 13k | |
| 276 | | `tui-view-pickers` | `provider_picker`, `model_picker` | 17k | |
| 277 | | `tui-view-setup` | `tui/setup` | 9k | |
| 278 | | `tui-view-approval` | `tui/approval` | 6k | |
| 279 | | `tui-view-hotbar` | `tui/hotbar` | 4k | |
| 280 | | `tui-view-ambient` | `pet_watch`, `underwater`, `ambient_life` | 13k | |
| 281 | | remaining views | `tui/views` (35k), split by surface | | |
| 282 | | `tui-commands` | slash-command presentation (`commands`, 72k) | | |
| 283 | |
| 284 | The TUI depends on `app-server-protocol`, never on `runtime`, `engine` or a feature |
| 285 | crate. A TUI agent's sandbox therefore holds no runtime code. `cli` connects |
| 286 | the TUI to the runtime in process through the existing `EngineHandle`/`Op`/ |
| 287 | `Event` seam; HTTP stays a transport for external clients. |
| 288 | |
| 289 | ### Hosts and test support |
| 290 | |
| 291 | | Crate | Source | |
| 292 | | --- | --- | |
| 293 | | `app-server` | exists; absorbs `runtime_api`, `acp_server`, `mcp_server` (66k) | |
| 294 | | `cli` | the only composition root; absorbs `lib.rs::run`, `RuntimeOptions`, `route_preferences` and the wiring list (45k today in `lib.rs` + `lib/`) | |
| 295 | | `test-support` | mock provider, fixtures, `test_support` | |
| 296 | | `pty-harness`, `tui-goldens` | PTY and golden-frame tests | |
| 297 | |
| 298 | About 70 crates. That is a consequence of the rules, not a target; split |
| 299 | further wherever a crate's churn still makes agents collide (`agents`, |
| 300 | `engine` and `tui` are the known candidates). |
| 301 | |
| 302 | ## Breaking the current cycle |
| 303 | |
| 304 | The 85-module cycle is held together mostly by **types and pure functions in |
| 305 | the wrong module**, not by behavioral dependencies. Heaviest back-edges |
| 306 | (grep-based counts; a few are test-only references): |
| 307 | |
| 308 | | Back-edge (refs) | Referenced | Destination | |
| 309 | | --- | --- | --- | |
| 310 | | `tools -> fleet` (62) | `AgentProfile`, `ChildAuthority`, `FleetRoster` | profile/authority data to a contract; behavior stays in `agents` | |
| 311 | | `tools -> core` (55) | `tool_catalog::enforce_tool_denial`, `tool_matches_any_rule` | `execpolicy` | |
| 312 | | `config -> core` (31) | `AutoReviewPolicy`, `AutoReviewAction`, `StepBudgetSource` | `execpolicy`; budget type to `core` | |
| 313 | | `client -> tools` (27) | `truncate` spillover, `schema_sanitize` | `util-truncate`, `util-schema` | |
| 314 | | `client -> compaction` (14), `client -> runtime_handoff` (10) | checkpoint and summary builders | `core` / `state` | |
| 315 | | `mcp -> core` (12) | `HumanDecision`, rule matching | `protocol` / `execpolicy` | |
| 316 | | `session_manager -> runtime_threads` (11) | `try_lock_file_exclusive`, thread store | `util-fslock`, `session-store` | |
| 317 | |
| 318 | Moving these down is the same work as creating the contract and utility tiers. |
| 319 | |
| 320 | ## Sequence |
| 321 | |
| 322 | Every move is rename-only (`git mv` plus path updates) with one wiring line, |
| 323 | so in-flight work rebases across it. Behavioral changes land separately. |
| 324 | |
| 325 | 0. **Measure.** Add a closure report to `scripts/split/`: for each crate, the |
| 326 | lines in its dependency closure and in its reverse closure, plus a |
| 327 | generator for a minimal sandbox workspace. Record the baseline. Extend |
| 328 | `module_graph.py` with the tier table so in-TUI back-edges ratchet down |
| 329 | before their crates exist. |
| 330 | 1. **Contracts and utilities.** Create `ext`, `config-types`, `app-server-protocol`, |
| 331 | the `util-*` crates, and the `execpolicy`/`state`/`core` absorptions. This |
| 332 | cuts the back-edges above. |
| 333 | 2. **Contributor registration.** Migrate every `with_*_tools` builder, |
| 334 | per-feature config field and command group list to `ext` contributors |
| 335 | wired in `cli`, in one slice per list, deleting the old list in that slice. |
| 336 | 3. **Pilot leaf: `tools-web`.** Extract it, then run one sandboxed agent |
| 337 | against it end to end. Record payload size, cold and warm build time, test |
| 338 | result and whether the patch applied cleanly. Adjust this document from the |
| 339 | result before extracting more. |
| 340 | 4. **Remaining features**, least coupled and most churned first. |
| 341 | 5. **Providers**, one adapter per crate. |
| 342 | 6. **Runtime tier and hosts.** `engine`, `context`, `approvals`, `agents`, |
| 343 | `tasks`, `session-store`; move `runtime_api` into `app-server`; move the |
| 344 | composition root into `cli` and delete the `lib.rs` path alias. |
| 345 | 7. **TUI.** Separate the app-state reducer from rendering, then extract views. |
| 346 | Last, because it is the most entangled and benefits from every earlier |
| 347 | contract. |
| 348 | |
| 349 | The runtime -> UI ratchet (`scripts/split/module_graph.py`, |
| 350 | `scripts/runtime-boundary-baseline.json`, enforced by |
| 351 | `scripts/check-command-crate-boundaries.py`) stays the gate until the runtime |
| 352 | tier is out of `crates/tui`; then Cargo enforces the boundary and the ratchet |
| 353 | is deleted. The 0.10.2 X2 snapshot stands at 0 production references and |
| 354 | 30 test references, down from 11 and 32. The RS-11 roster split then removes |
| 355 | five test references and the final tools/crossterm test import, leaving 25 |
| 356 | test references and no UI-library imports. The following tools split moves the |
| 357 | Workflow panel assertions to its existing history test and removes duplicated |
| 358 | coordination pager checks, leaving 18 test references. The history split moves |
| 359 | persistence/render tests to history and uses the runtime MCP decoder in the |
| 360 | engine, leaving 13 test references. Moving the two unchanged App fixtures to |
| 361 | their UI owner and migrating all 114 consumers leaves 6 test references. |
| 362 | Moving redaction decisions onto Config, workspace trust into config, and the |
| 363 | unchanged constitution checkpoint into prompts removes the final 6. RS-11 |
| 364 | ends at production 0, test 0, late 1, UI-library 0 and doc 0. The late |
| 365 | engine/exec-agent test reference belongs to a later slice. The current baseline |
| 366 | is the authority. |
| 367 | `crates/runtime` must never depend on `tui`, `cli`, ratatui, crossterm, |
| 368 | `ansi-to-tui` or a terminal component kit; the runtime reaches the terminal |
| 369 | only through `host_terminal`. |
| 370 | |
| 371 | ## Operational plan: assignable slices |
| 372 | |
| 373 | Re-measured for X2 on 2026-10-12 with `python3 scripts/split/partition_report.py` (per-module |
| 374 | waves, sizes and blockers) and `--scc-edges 60` (the heaviest edges inside the |
| 375 | cycle). Regenerate before assigning; the numbers below are a snapshot. |
| 376 | |
| 377 | - Before X2 the largest runtime cycle had **98 modules**; after X2 it has **82**. |
| 378 | with `tools` 152k, `core` 50k, `fleet` 39k, `runtime_api` 31k, `client` 26k |
| 379 | production lines in the earlier size snapshot. X2 removes all 11 production |
| 380 | back-edges (`runtime_api -> commands` 9, `tools -> commands` 2); `tui`, |
| 381 | `commands` and `lib.rs` leave the largest cycle. These are source graph |
| 382 | measurements, not a claim that the runtime crate migration has shipped. |
| 383 | - 30 modules (about 17k production lines) are **wave 0**: nothing they depend on |
| 384 | is still waiting to move. They are claimable as crates now. |
| 385 | - Moving the cycle as a unit is the rejected pattern (see Rules). The open runtime |
| 386 | split issue that moves the whole core into `crates/runtime` in one change |
| 387 | (RS-14, #6941) conflicts with the Rules above and is replaced by the slices |
| 388 | below unless the founder decides otherwise; it should not start before that |
| 389 | decision. |
| 390 | |
| 391 | Each slice below was proposed by one analyst and challenged by another that |
| 392 | re-read the code. "Held" means the challenge confirmed it. The corrections are |
| 393 | part of the slice. |
| 394 | |
| 395 | | Slice | Claim unit (paths) | Size | Status of the analysis | Must happen first | Result | |
| 396 | | --- | --- | --- | --- | --- | --- | |
| 397 | | **X1 util-fslock** | `crates/fslock/` (new), `runtime_threads.rs`, `session_manager.rs`, `session_reconcile.rs`, `operate.rs` | S, ~190 lines | **Held.** Five call-site clusters in `runtime_threads.rs` (incl. `has_live_holder`), `session_manager.rs:1954` is production | none | removes the `session_manager <-> runtime_threads` lock edge; first leaf for `session-store` | |
| 398 | | **X2 command catalog port** | `crates/engine/src/command_catalog.rs`, `commands/mod.rs`, `runtime_api.rs` list handler, `tools/tui_help.rs`, `extension_host/command.rs` | M | **Landed in 0.10.2.** One host port supplies all three consumers; the Runtime API test server captures its thread-local guard | none | ratchet `prod` 11 -> 0, `test` 32 -> 30, largest cycle 98 -> 82 | |
| 399 | | **X3 tool contract, part 1** | `crates/execpolicy/src/canonical_action.rs`, `crates/tools/src/lib.rs`, `crates/tools/src/param_alias.rs`, `crates/tools/src/patch_preflight.rs`, `crates/engine/src/tools/mod.rs` | M, no `pub use` shim (callers rewired) | Direction right; **not rename-only** (visibility widening, split test module) | none | first tool-contract move; unblocks TUI views depending on a crate, not the runtime | |
| 400 | | **X4 util-schema** | `crates/util-schema/` (new), `tools/schema_sanitize.rs`, `schema_canonicalize.rs` | M | Needs `prepare_tools_for_strict_mode` moved next to its caller first (it is the only use of `codewhale_models::Tool`) | X3 not required | removes `client -> tools` schema refs | |
| 401 | | **X5 config-types, part 1** | `crates/config-types/` (new), `features.rs`, `route_receipt.rs`, `plugins/types.rs` | S-M, ~400 lines | **Corrected:** move `impl Display for PluginId` with `PluginId` (orphan rule); leave out `CustomLspDef` (already in `crates/config`); the `lsp` and `plugins` edges stay | none | removes the type-only up-edges from `config`/`settings` | |
| 402 | | **X6 app-server-protocol (was runtime-wire), part 1** | `crates/app-server-protocol/`, former `protocol/src/runtime`, client consumers and schema/SDK exports | existing Runtime client family | Engine `Op`/`Event`, private checkpoint/source receipts and pricing authority stay in Engine; other client projections remain separate work | migrate every extracted-family consumer without reexports | one owner for the extracted wire family; not whole Codex parity or Stage B closure | |
| 403 | | **X7 tool rules into execpolicy** | `crates/execpolicy/src/tool_rules.rs`, `core/engine/tool_catalog.rs` | S | Part of the fleet plan; `tool_denied`, `tool_matches_any_rule`, `requires_raw_shell`, `policy_tool_aliases` | none | cuts `tools -> core` and `config -> core` rule references | |
| 404 | | **X8 agent-contract** | `crates/agent-contract/` (new), `fleet/role.rs`, `fleet/worker_profile.rs`, `tools/subagent/mod.rs` | M | **Corrected:** the inherent `impl FleetRole` at `tools/subagent/mod.rs:639` becomes illegal after the move; convert to free functions first; resolve the two different `ChildAuthority` types | X7 | removes `tools -> fleet` (62 refs) | |
| 405 | | **X9 sandbox pilot** | `crates/sandbox/` (new), `tui/src/sandbox/` | S then M | **Superseded by S17:** sandbox and `shell_dispatcher` move together; the integration `#[path]` include is removed and the `cfg(test)` env-lock branch is preserved under the `test-support` feature. The config/oauth edges resolve through the existing `codewhale-runtime` dependency; no edge cut is needed | none | second feature leaf after `tools-web` | |
| 406 | | **W0-S1 runtime residents** | `crates/runtime/` ← `features`, `logging`, `startup_trace`, `resource_telemetry`, `runtime_policy`, `lane_control` | S, landed 2026-10-10 (8aea0265c) | **Landed.** `python3 scripts/split/move-modules.py` dry run is read-only and safe | none | six fewer tui modules; no `test_support` needed | |
| 407 | | **W0-S2.. test-support and the rest of wave 0** | the existing `codewhale-runtime` `test-support` feature, then remaining leaves | M each | **Environment helpers moved in 0.10.2:** one process environment lock in the Runtime test-support owner; no new test-support crate | W0-S1 | the TUI shim retains its resolver fences; App fixtures live in the UI owner; remaining residents follow RS-10 | |
| 408 | | **B billing/catalog** | new crate above `codewhale-runtime` ← `cost_status`, `pricing`, `route_billing`, `provider_catalog_live`, `provider_lake`, `models_dev_live`, `codex_model_cache`, `reasoning_preference` | M, 11,091 strict production lines (24,352 file-level; `partition_report.py`'s per-module figure is file-level) | **Re-measured 2026-10-31 on `1b0e51005b`.** The eight modules' only production edges into the 59-module cycle are 6 refs into `client` (`CodewhaleClient::for_catalog_refresh` in `provider_lake` and `codex_model_cache`, `client::ollama_catalog::load_model`, `client::AvailableModel`) and 1 into `provider_readiness`. 590 inbound production refs from the TUI follow the move. Tests reach out 176 times, 149 of them to `test_support`. Every module uses `codewhale_runtime::config`, so the crate depends on the runtime crate; the earlier "must not depend on the runtime tier" constraint predates `Config` moving there. Not rename-only: catalog refresh stays beside `client` (split `provider_lake`/`codex_model_cache` into store and refresh driver) | the refresh-driver split; open PRs that touch `session_manager.rs` or other `pricing`/`cost_status` callers merged first | measured, not started | |
| 409 | | **S0 closure report** | `scripts/split/closure_report.py` | S (report), M (sandbox generator) | Held as a tool; deletes no edges | decide where sandbox third-party sources come from | the numbers the sandbox contract needs | |
| 410 | |
| 411 | Running them in parallel: |
| 412 | |
| 413 | - Claim units are disjoint across X1, X3, X5, X6, X7 and W0-S1, so those can be |
| 414 | owned by six agents at once. |
| 415 | - Every new crate edits files only an integrator may touch: root `Cargo.toml`, |
| 416 | `Cargo.lock`, `crates/tui/Cargo.toml`, `scripts/release/crates.sh`, |
| 417 | `scripts/dev-test.sh`. Serialize those edits through one integrator; new |
| 418 | package names are a release-time human gate. |
| 419 | - Each slice holds one of the machine's two native build slots through |
| 420 | `scripts/dev-cargo.sh` and `scripts/build-lock.py` (founder, 2026-10-25). |
| 421 | Freeze shared compiler inputs with their owners before a build; do not |
| 422 | create another checkout or target directory to bypass contention. |
| 423 | - Each slice is verified by `cargo check` on both crates, the crate's own |
| 424 | tests, `python3 scripts/split/module_graph.py --check` with the counts it |
| 425 | should lower, and `python3 scripts/split/partition_report.py` before and after. |
| 426 | |
| 427 | Mapping of the open issues: RS-8 (#6935), RS-9 (#6936), RS-12 (#6939) and |
| 428 | RS-13 (#6940, first sub-slice) landed; X2 completes RS-13; W0-S1 and W0-S2 replace |
| 429 | RS-10 (#6937) and the test-support half of RS-11 (#6938); RS-14 (#6941) is |
| 430 | replaced by this table pending the founder's decision. |
| 431 | |
| 432 | ## Execution and current ownership |
| 433 | |
| 434 | Canonical source, CW-7701 and the historical receipts on CW-6129 establish what has landed. |
| 435 | S35 thread ownership, S52 terminal helpers, N13 error taxonomy, pure model |
| 436 | pricing and native skill ownership have moved with their consumers. Runtime |
| 437 | configuration now lives in `crates/runtime/src/config.rs`; do not recreate the |
| 438 | removed TUI config owner. Skill listing and bundled policy share the existing |
| 439 | registry/configuration rather than separate prompt or activation stores. |
| 440 | These moves do not complete the target: the remaining runtime/UI ownership |
| 441 | cycle must be cut through the current coarse split before finer extraction. |
| 442 | |
| 443 | Continue from the current dependency graph and owning children, in this order: |
| 444 | |
| 445 | 1. Preserve integrity, trust boundaries, durable writes and cancellation while |
| 446 | cutting actual contract/configuration ownership edges. Read the existing |
| 447 | runtime and config owners before proposing another abstraction. |
| 448 | 2. Move feature and provider implementations with their final callers and |
| 449 | existing checks. Preserve reviewed plugin authority, tool permission |
| 450 | ceilings, credentials, typed human decisions and model-visible receipts. |
| 451 | The remaining MCP/DSH, durable Tasks, isolated UI and gix work stays in 0.10.3. |
| 452 | 3. Complete Engine, agents, sessions, runtime API and tool ownership without a |
| 453 | second turn loop, event authority or session store. Runtime HTTP routing, |
| 454 | ACP and MCP hosts move into app-server; Engine retains the thread manager, |
| 455 | store and event authority. Handler-owned business logic moves to its existing |
| 456 | Engine owner so each transport calls the same implementation. |
| 457 | 4. Finish the terminal reducer/render boundary and make the CLI the composition |
| 458 | root. Remove superseded shims only after the last consumer migrates. |
| 459 | 5. Measure every resulting crate, dependency closure and affected edit/build |
| 460 | path. Complete internal splits where an ownership move still leaves an |
| 461 | oversized crate. Do not claim speedups from line counts or a passing compile. |
| 462 | |
| 463 | The target map above describes responsibilities, not a required new directory |
| 464 | layout or a second queue. Remaining RS/S/N and command/config/API children keep |
| 465 | their full acceptance on CW-6129. Former wave branches, fixed patch-release |
| 466 | ladders and the old RS-14 A/B narrative are historical proposals; none reduce |
| 467 | the admitted scope or authorize relocating the whole component as completion. |
| 468 | |
| 469 | ### Other product owners |
| 470 | |
| 471 | The desktop product is `codewhale-app`'s existing native root |
| 472 | `codewhale-gpui`, using GPUI/weGPUI and Codewhale-owned components. It is active |
| 473 | product source, not legacy code to freeze or delete. The founder's October 24/29 direction selects that native host and its |
| 474 | component migration. |
| 475 | `crates/client` is the separate browser client. The superseded Tauri |
| 476 | `crates/desktop-host` was deleted; no extraction or packaging should target it. |
| 477 | Coordinate App/weGPUI changes with their current writers and retain Engine, |
| 478 | account, accessibility, IME, focus, drafts and locale contracts. |
| 479 | |
| 480 | Platform route decomposition and shared protocol/client ownership remain with |
| 481 | their actual repositories and existing issues. Derive current file/crate sizes |
| 482 | from source rather than treating the October9/24 audit as today's inventory. |
| 483 | A native, browser, mobile or CU source change needs evidence for that surface; |
| 484 | Engine helper checks alone do not qualify it. |
| 485 | |
| 486 | ## Known limitations |
| 487 | |
| 488 | - **Native builds share two machine build slots.** Cargo also locks each |
| 489 | target directory. The isolation gain locally is smaller rebuilds. Freeze |
| 490 | shared compiler inputs before compiling, and use the build-lock wrapper. |
| 491 | Do not create per-agent target directories on a shared host; each one is a |
| 492 | full build cache. |
| 493 | - **The final binary still links everything.** Relinking `codewhale` after any |
| 494 | change is a fixed cost no crate split removes. |
| 495 | - **Contract edits still fan out.** The design makes them rare and visible, |
| 496 | not free. |
| 497 | - **Every workspace crate publishes to crates.io** |
| 498 | (`scripts/release/publish-crates.sh`, order derived by |
| 499 | `validate-crate-publish-order.py`). Each new crate is a new public package |
| 500 | whose name must be claimed at release. Publication remains a human gate. |
| 501 | - **An ownership move may leave an oversized crate.** Complete its internal |
| 502 | split within the admitted program, guided by current churn and closure data; |
| 503 | the initial map does not waive the 50,000 production-line ceiling. |
| 504 | - **No speedup has been measured.** Claims wait for the step 0 report and the |
| 505 | step 3 pilot. |
| 506 | |
| 507 | ## Verification |
| 508 | |
| 509 | - Preserve the model-facing runtime receipt, prompt and cache-prefix |
| 510 | semantics, tool names and order, serialized records, and public commands |
| 511 | across mechanical moves. A deliberate behavior change names and tests the |
| 512 | intended difference. |
| 513 | - Move private unit tests with their implementation. A `#[path]` split stays |
| 514 | in the same compilation unit and proves nothing about isolation. Do not make |
| 515 | internals public just to relocate tests. |
| 516 | - Exercise mock-provider parent and child turns, approval and denial, |
| 517 | streaming, cancel and steer, explicit goals, pause and resume, reconnect, |
| 518 | restart recovery and terminal fan-in at each affected boundary. |
| 519 | - For every extracted crate, use a focused canonical Cargo invocation and |
| 520 | record its actual dependency and reverse closures. Do not create another |
| 521 | source checkout to demonstrate isolation. Existing tests move with ownership; |
| 522 | new test functions and comments require a task-specific request. |
| 523 | - Measure warm edit/build/test cycles and peak memory for a provider edit, |
| 524 | tool edit, renderer edit and locale edit before and after, with compiler, |
| 525 | profile, features and cache state recorded. |
| 526 | - Local tests, full gates, hosted CI, installed artifacts and PTY behavior |
| 527 | remain separate evidence. Exhaustive and hosted checks run only against the |
| 528 | completed immutable candidate, with only invalidated evidence repeated. |
| 529 | |
| 530 | ## Model judgment and test-time compute |
| 531 | |
| 532 | The founder's September 9 direction calls for model judgment about useful |
| 533 | planning, delegation, verification and durable continuation, including a useful |
| 534 | inferred objective within the user's authorized task. Ordinary multi-step work |
| 535 | need not create a goal. Preserve runtime-owned state, provenance, accounting, |
| 536 | cancellation and user controls. Goal, reasoning |
| 537 | and continuation policy must share the existing owners and preserve explicit |
| 538 | route choices and spending authority. There is no authorization here for a |
| 539 | classifier before every turn, another execution loop, a paid evaluation or an |
| 540 | automatic expansion of compute budgets. |
| 541 | |
| 542 | The [dated reference audit and implementation rationale](https://github.com/codewhale-hq/Codewhale/blob/2231d73df5d6d4538653a54920695a389d575451/docs/design/TUI_DECONSTRUCTION.md#model-judgment-and-test-time-compute) |
| 543 | records the inspected DSH/Codex/Kimi snapshots and original acceptance scenarios. |
| 544 | Its statements about the then-current goal implementation must be rechecked |
| 545 | against the owning source; they are not current findings or assignments. |
| 546 | Quality, correct stopping, continuity and authorized cost matter; merely |
| 547 | creating a goal or taking more steps is not evidence of improvement. |
| 548 |