Skip to content

Visual Studio Code, rebuilt in Rust

Rust

docs: add the workbench, editor, extension host and testing documents

rokartur9321c1b19a9f
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md
index e2870ae9556..08f3603a777 100644
--- a/ARCHITECTURE.md
+++ b/ARCHITECTURE.md
@@ -184,7 +184,7 @@ The tools in `port/tools` each start with a header that states their use. The ma
- Build inputs. The build needs the upstream checkout: `native/vscode_src.rs` resolves it for the build scripts. `app/build.rs` makes the NLS index from upstream's `localize` calls and renders the Dock icons. A few modules embed upstream files, and tests read upstream fixtures. A binary outside a package also reads the checkout at run time as its app root: `product.json`, icons and the built-in extensions.
- Build order. Compile upstream once, run `bun port/tools/build-exthost`, then `cargo build --release -p app`. `README.md` has the commands. The debug profile builds dependencies with optimization and the port's crates without (`native/Cargo.toml`).
- Packaging. `port/tools/package-app` makes the signed macOS app and its update files. `port/tools/package-linux` makes the Linux archive. `port/tools/app-files.ts` lists the files a package ships.
-- CI. `.github/workflows/linux.yml` and `.github/workflows/windows.yml` build by hand. A release is a manual step.
+- CI. `.github/workflows/ci.yml` runs the repository checks and every crate's tests on macOS, Linux and Windows for each push to `main` and each pull request. `.github/workflows/linux.yml` and `.github/workflows/windows.yml` build a package by hand. A release is a manual step.
## Cross-cutting rules
diff --git a/docs/README.md b/docs/README.md
index 4d958e0ce9d..198410abd50 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -8,7 +8,12 @@ The state of the work, per upstream folder, is not here. It is in `port/ledger.m
| File | Area |
|---|---|
+| [workbench.md](workbench.md) | The `Workbench` entity, the grid of parts, part views, the path of a key and of a command, context keys. |
+| [editor.md](editor.md) | The text model, cursors and edits, tokenization, how the editor view draws, what runs per frame. |
+| [extension-host.md](extension-host.md) | The Node process, the protocol, the main-thread side in `exthost`, how state reaches the workbench. |
+| [testing.md](testing.md) | Where tests live, the GPUI harness, upstream's tests, the comparison tools against the reference app, CI. |
| [memory-safety.md](memory-safety.md) | Where `unsafe` code is allowed, the inventory of every file that has it, how to add a block, and the checks that fail. |
+| [performance.md](performance.md) | The budgets, the rules for hot paths, how to measure, and the gates that fail on a regression. |
## What is missing
@@ -16,11 +21,6 @@ These areas have only their section in ARCHITECTURE.md and the `//!` headers of
| Area | Start reading at | Note |
|---|---|---|
-| Performance | `port/results/benchmark.md` | The budgets, the rules for hot paths and the regression gate. |
-| Workbench layout, parts, keys and commands | `app/src/workbench.rs`, `app/src/workbench/layout.rs`, `app/src/workbench/keyboard.rs` | |
-| Editor and text model | `app/src/editor.rs`, `app/src/workbench/editor_view.rs` | |
-| Extension host bridge | `exthost/src/session.rs`, `app/src/host_sync.rs` | |
-| Testing | `.agents/skills/gpui-test` | Test layout, the GPUI test harness, the comparison tools. |
| Terminal | `app/src/terminal.rs` | The backend moves to libghostty-vt on another branch. Write this file after that lands. |
| Webviews | `app/src/webview.rs` | The three backends merge on another branch. Write this file after that lands. |
| Files, search and watchers | `files/src/lib.rs`, `search/src/text_search.rs` | |
diff --git a/docs/editor.md b/docs/editor.md
new file mode 100644
index 00000000000..509ef5da234
--- /dev/null
+++ b/docs/editor.md
@@ -0,0 +1,62 @@
+# Editor and text model
+
+The editor is the text model, the cursors and edits on it, the tokens, and the view that draws them. This file goes one level under [ARCHITECTURE.md](../ARCHITECTURE.md). Upstream is `src/vs/editor`.
+
+## Model, editor, group
+
+Three types in `app/src/editor.rs` carry the state:
+
+- `app/src/editor.rs::TextDocument`: the text of one resource as lines (`lines`), its line ending, encoding and language, and its tokens.
+- `app/src/editor.rs::TextModel`: a document plus what belongs to the resource and not to one editor: the version id, the undo stack, indent guides, marker decorations, the edit log. Every editor of a resource shares one model through `app/src/editor.rs::SharedModel`.
+- `app/src/editor.rs::OpenEditor`: one editor of a model in one group, with its own view state: selections, folding, scroll position, word wrap, view zones, injected text, decorations.
+
+`app/src/editor.rs::EditorGroup` holds the editors of the active group and the other groups ([workbench.md](workbench.md), "Editor groups").
+
+**Invariant:** text changes go through the edit functions of `app/src/editor/edit.rs` (`execute_edits`, `execute_commands`). They update the version id, the undo stack, the tokens and the edit log, and they return a `app/src/editor/edit.rs::ContentChanged`. Code that writes `lines` directly leaves all of those stale.
+
+**Invariant:** the caller of an edit sends the returned `ContentChanged` to the extension host (`app/src/host_sync.rs::send_content_change`). An edit the host does not see breaks every language feature of the document.
+
+An editor that shares its model with another editor follows the other's edits from the model's edit log: `app/src/editor/edit.rs::sync_with_model` moves its cursors and folds to the new version.
+
+## Positions and units
+
+A position is a 0-based line and a column counted in chars. Upstream counts 1-based lines and UTF-16 units. Each module header in `app/src/editor` states its unit. Folding and sticky scroll keep upstream's 1-based lines because their tests do.
+
+Conversion to UTF-16 happens where a position leaves for the extension host or arrives from it (`app/src/host_sync.rs`, `app/src/workbench/host_requests.rs`). Do not convert inside the editor.
+
+## Editing features
+
+Each editor feature is one file in `app/src/editor`, named after its upstream contribution, with upstream's tests beside it: `app/src/editor/cursor.rs`, `app/src/editor/word_operations.rs`, `app/src/editor/multicursor.rs`, `app/src/editor/auto_closing.rs`, `app/src/editor/auto_indent.rs`, `app/src/editor/folding.rs`, `app/src/editor/word_wrap.rs`, `app/src/editor/snippet_session.rs`, `app/src/editor/find.rs`. Almost none of these files import GPUI. A feature takes the editor and returns edits or selections.
+
+The commands that bind these features to upstream's command ids are in `app/src/workbench/editor_commands.rs` and `app/src/editor/commands.rs`.
+
+Undo is one service for the window (`app/src/undo_redo.rs`). A model pushes its edit stack elements into it (`app/src/editor/edit_stack.rs`), beside the file operations of the Explorer, so one undo order covers text and files.
+
+## Tokens and colors
+
+- Syntax tokens come from the `textmate` crate: `textmate/src/grammar.rs::Grammar` tokenizes a line from the state at the end of the line before.
+- `app/src/syntax.rs::Tokenization` keeps the tokens per line. Lines are tokenized as far as the view reads them, and the rest in the background, as upstream's `textModelTokens.ts`. After an edit, a line keeps its old tokens until it is tokenized again.
+- Semantic tokens from extensions are in `app/src/semantic_tokens.rs`.
+- Bracket pair colors and matching are in `app/src/brackets`.
+- A token's color comes from the theme: `textmate/src/theme.rs::Theme` resolves scopes to styles, and `app/src/theme.rs` loads the theme file with upstream's color defaults.
+
+**Invariant:** tokenization never runs for the whole file on the main thread. A command that needs tokens for a far line uses the cheap paths upstream uses (`tokenizeIfCheap`). Comment at line 49000 of a 52000-line file took 5.3 s before this rule and 13 ms after (`port/notes.md`, "Scheduling differences").
+
+## The view
+
+`app/src/workbench/editor_view.rs` draws the editor part: tabs, breadcrumbs and the code editor's view parts in upstream's order (margin, current line, selections, indent guides, text, cursors, overview ruler). `app/src/editor/view.rs` computes the viewport: which lines are in view, the scroll size, reveal. The minimap is `app/src/editor/minimap.rs`.
+
+- The view shapes and draws only the lines in view (`app/src/workbench/editor_view.rs::editor_lines`).
+- Injected text (inlay hints, ghost text, color boxes) moves every later x of its line. Every column-to-x conversion goes through the line layout of `app/src/editor/injected_text.rs`.
+- Text input from the OS (IME, dead keys) arrives through GPUI's input handler in `app/src/workbench/text_input.rs`, after keybindings.
+- A scroll of the editor notifies only the editor part view ([workbench.md](workbench.md), "Layout").
+
+## Language features
+
+Hover, completion, code actions, rename and the others are requests to the extension host. The workbench asks through `app/src/host_sync.rs::call` and draws the answer: `app/src/workbench/content_hover.rs`, `app/src/workbench/suggest_controller.rs`, `app/src/workbench/code_actions.rs`, `app/src/workbench/rename.rs`. Request timing follows upstream's debounce (`app/src/language_feature_debounce.rs`).
+
+A late answer is handled as the feature's upstream controller handles it: dropped after a version check, or moved through the model's edit log. A request made with `app/src/host_sync.rs::call_cancellable` is cancelled in the host when its receiver is dropped.
+
+## The budget
+
+Typing in a 52000-line file must take under 16 ms per edit at p95. The check is `app/src/syntax.rs::typing_latency_on_a_large_file`. A scroll frame of the editor must take under 8 ms: `app/src/workbench/scroll_frame_time.rs::editor_scroll_frame_time`. [performance.md](performance.md) has the commands.
diff --git a/docs/extension-host.md b/docs/extension-host.md
new file mode 100644
index 00000000000..2b63518471e
--- /dev/null
+++ b/docs/extension-host.md
@@ -0,0 +1,74 @@
+# Extension host bridge
+
+Extensions run in VS Code's own extension host, unchanged. The port is the other end of that connection: it plays the role of the workbench's "main thread" classes. This file goes one level under [ARCHITECTURE.md](../ARCHITECTURE.md), section "Extension host bridge". Upstream is `src/vs/workbench/api` and `src/vs/workbench/services/extensions`.
+
+## The host process
+
+- The code is upstream's `src/vs/workbench/api/node` tree. `port/tools/build-exthost` copies upstream's `src`, applies `port/patches`, and bundles it into `out-exthost/`. The entry is `bootstrap-exthost.js`, added by `port/patches/src-bootstrap-exthost.patch`.
+- The patches are small and each has one reason. Most make the host run on Bun (`port/patches/src-extHostExtensionService.patch`, `port/patches/src-extensionHostProcess.patch`). `port/patches/src-extHostInertModules.patch` gives extensions inert telemetry modules. `port/patches/src-uncFsGuard.patch` adds the UNC allowlist check that Electron's Node has. `port/notes.md` ("Bun (1.4.5) as the extension host runtime") explains each Bun change.
+- The runtime is Bun. `--node` starts the same bundle on Node, which is the control when a problem can come from Bun. `exthost/src/lib.rs::Runtime` selects the executable.
+- One host per window. `exthost/src/lib.rs::ExtensionHost` owns the child process. Dropping it sends Terminate and kills the process.
+
+**Do not** edit files in `upstream/`. To change host code, edit the file there, write the diff to `port/patches`, and restore the file (`port/notes.md`, "Layout").
+
+## Wire format
+
+Three layers, each a port of one upstream file, so a byte on the wire means what it means in VS Code:
+
+1. Transport: a Unix socket whose path the host gets in `VSCODE_EXTHOST_IPC_HOOK`. On Windows it is a named pipe (`exthost/src/pipe.rs`).
+2. Frames: `exthost/src/frame.rs` is `PersistentProtocol` of `ipc.net.ts`: a 13-byte header with type, id and ack.
+3. Messages: `exthost/src/rpc.rs` is `rpcProtocol.ts`: requests, replies, errors and cancel, with JSON and binary buffer arguments.
+
+A call names a proxy and a method. A proxy is a number on the wire. `exthost/src/proxy_ids.rs` lists them in the order upstream creates them, and `port/tools/gen-proxy-ids.ts` writes that file from upstream's `extHost.protocol.ts`. Run the generator again after an upstream update.
+
+The handshake is in `exthost/src/lib.rs::start`: the host sends Ready, the parent sends the init data (`exthost/src/session.rs::init_data`), the host sends Initialized. Then `exthost/src/session.rs::start` makes the calls upstream's main thread classes make in their constructors, and starts the extensions.
+
+## Threads
+
+Two threads share the work for one window:
+
+- The `exthost-session` thread runs `app/src/main.rs::run_extension_host`. It owns the `exthost/src/session.rs::Session` and blocks in `exthost/src/session.rs::pump` on the host's next message.
+- The GPUI main thread owns the `Workbench`.
+
+Anything else that has work for the session (the workbench, a file watcher, a search) queues it and wakes the session with an `exthost/src/lib.rs::Waker`.
+
+## Host calls the port answers
+
+A request from the host for `MainThreadX.$method` takes one of three paths:
+
+1. The session answers it on its own thread. This is for state that needs no window: the disk and file watchers (`exthost/src/main_thread_files.rs`), settings (`exthost/src/main_thread_configuration.rs`), extension storage (`exthost/src/main_thread_storage.rs`), secrets (`exthost/src/main_thread_secret_state.rs`), workspace search (`exthost/src/main_thread_workspace.rs`).
+2. The workbench answers it while the session thread waits. This is for calls whose answer depends on the document state at that moment: `app/src/workbench/host_requests.rs::HOST_REQUESTS` lists them (open a document, apply an edit, set selections, the clipboard). They cross through `app/src/main_thread.rs::WorkbenchMainThread`.
+3. The session forwards it and goes on. The proxies named in the `forward` call of `run_extension_host` go to the main thread as `exthost/src/session.rs::ForwardedRequest`, and `app/src/main_thread_commands.rs::handle_host_request` answers them later with `exthost/src/session.rs::reply`. Most of the API is here: commands, languages, webviews, debug, SCM, terminals, tree views.
+
+A method that no path implements answers `undefined`, as a missing optional feature does upstream. The ledger row of the main thread class lists each such method that an extension can observe.
+
+**Trap:** on path 2 the session thread blocks until the main thread answers (`app/src/main_thread.rs::ask`). A handler there must be short and must not wait for an answer from the host: the thread that reads the host is the one that waits. A save is the one exception. It runs format on save and dialogs, so it uses `app/src/main_thread.rs::ask_while_pumping`, which keeps the session sending and reading.
+
+## Calls the port makes
+
+`app/src/host_sync.rs::HostSync` is the workbench's only door to the host.
+
+- State: `send_changes`, `send_tabs`, `send_content_change` and their siblings tell the host about documents, editors and tabs, as `mainThreadDocumentsAndEditors.ts` does.
+- Requests: `app/src/host_sync.rs::call` returns a receiver for the answer. `app/src/host_sync.rs::call_cancellable` cancels the request in the host when the receiver is dropped.
+- Activation: `app/src/host_sync.rs::activate_by_event` sends one `$activateByEvent` per event and shares its answer.
+
+Everything goes into one queue. The session thread sends the queue in order, and it sends what is queued before it sends a reply to the host (`exthost/src/session.rs::flush_main_thread`).
+
+**Invariant:** one queue, one order. The host must see "document opened" before a request about that document, and an edit before the reply that depends on it. A second channel to the host breaks this.
+
+**Invariant:** positions and ranges change units at this boundary, and nowhere else: chars in the port, UTF-16 units in the host.
+
+## Extensions as data
+
+The workbench does not need a running host to know what extensions contribute. `exthost/src/scan.rs` reads each `package.json` into a `ScannedExtension`, and the workbench builds menus, keybindings, settings, languages, grammars, themes and views from that at startup. The host is needed only when extension code must run.
+
+- Install, uninstall and update: `exthost/src/extension_management.rs`, against Open VSX (`exthost/src/gallery.rs`) with signature checks (`exthost/src/sign.rs`).
+- Which extensions run: enablement (`app/src/extension_enablement.rs`), workspace trust (`app/src/workspace_trust.rs`), proposed API (`exthost/src/proposed_api.rs`).
+- Restart: `app/src/workbench/extension_host_restart.rs` ends the host, disposes what it registered in the workbench, and starts a new session.
+
+## Check a change
+
+- `cargo test -p exthost` runs the protocol and session tests without a host.
+- `exthost/src/bin/exthost-probe.rs` starts a real host without a window, activates every built-in extension and prints a report.
+- `port/tools/api-tests` runs upstream's `vscode-api-tests` against the port. This is the check for any change to a main thread class.
+- `port/tools/extension-tests` runs the integration tests of one built-in extension.
diff --git a/docs/testing.md b/docs/testing.md
new file mode 100644
index 00000000000..79a65a7cbbd
--- /dev/null
+++ b/docs/testing.md
@@ -0,0 +1,73 @@
+# Testing
+
+The port's bar is "the same as VS Code", so most checks compare with upstream: its test cases, its API tests, its pixels. This file says which check proves what and where each runs. [ARCHITECTURE.md](../ARCHITECTURE.md) has the map of the code.
+
+## Where tests run
+
+Builds and tests run on the build machine (the Studio), not on a laptop: `port/tools/studio 'cd native && cargo test -p app <name>'`. The tool mirrors the checkout, uncommitted changes included, and runs the command there. A tool that opens a window runs in a GUI lane: `port/tools/studio --lane '<command>'`. `port/tools/linux-build` and `port/tools/windows-build` run a command for the other two systems.
+
+`.github/workflows/ci.yml` runs on each push to `main` and each pull request: the repository checks (`ledger-check`, the tests of `port/tools`, `docs-check`, licenses, advisories) and `cargo test --workspace` on macOS, Linux and Windows. The Linux and Windows jobs are `expected-red` until milestones M23 and M24 close their test gaps. The checks that need the reference app and a window do not run there.
+
+The workspace has about 4000 tests. The `app` suite takes about a minute after the build.
+
+## The kinds of check
+
+| Check | Proves | Where |
+|---|---|---|
+| Unit tests of a model | The logic matches upstream's test cases. | `#[test]` beside the module, in a `tests.rs` file or a `tests` module |
+| Workbench tests | A feature works through keys, clicks and commands in a window. | `#[gpui::test]` in `app/src/workbench` |
+| Extension host tests | The protocol and the session without a host. | `cargo test -p exthost` |
+| API tests | The extension API behaves as in VS Code. | `port/tools/api-tests`, `port/tools/extension-tests` |
+| Pixel checks | A still window looks the same. | `port/tools/pixel-check` |
+| Paired UI runs | A state reached by input looks the same. | `port/tools/ui-pair` with step files in `port/fixtures/ui-steps` |
+| Performance gates | Frame times and startup stay in budget. | ignored tests and `port/tools/bench` ([performance.md](performance.md)) |
+| Repository checks | The ledger, the licenses and the documents agree with the code. | `port/tools/ledger-check.ts`, `port/tools/check-licenses`, `port/tools/docs-check.ts` |
+
+Each unit row of `port/ledger.md` names the narrowest check that proves the unit. Run that one first.
+
+## Ported unit tests
+
+A ported module ports the tests of its upstream file, under names that say what each case checks. The module header names the upstream test file when the cases come from one. Fixtures that upstream keeps as files are read from the checkout at test time (`env!("VSCODE_SRC")`), so they are not copied into the repository.
+
+A fix comes with a test that fails without the fix. Show it red once, with the fix reverted, before you trust it.
+
+## Workbench tests
+
+A workbench test builds a real `Workbench` in a GPUI test window and drives it as a user does.
+
+- `#[gpui::test]` gives a `TestAppContext` with a deterministic scheduler and a fake clock. Time moves only when the test advances it, so a debounce of 300 ms costs no real time.
+- Each test file has a small `workbench` helper that makes the entity: a theme from the checkout, settings in a temp folder, and a `app/src/host_sync.rs::HostSync` over a plain channel. The test reads that channel to assert what the workbench sent to the extension host, and answers as the host. `app/src/workbench/notifications_view/tests.rs` is a short example.
+- Input goes through the same paths as in the app: `simulate_keystrokes`, `simulate_click`, or `execute_command`.
+- Assertions read `Workbench` state, or the bounds of a named element with `debug_bounds`. An element gets its name from `.debug_selector`. Assert on a size or a position only when upstream's CSS gives it.
+- `run_until_parked` runs every task that is ready. A test that ends with a task still pending fails ("parking forbidden").
+
+The skill in `.agents/skills/gpui-test` has the seed and iteration variables for a flaky test, and how to read a pending task trace.
+
+**Do not** wait for workbench work with `std::thread::sleep` or the real clock. Advance the test clock. A test that waits for the real file system or for a child process is the exception.
+
+**Do not** assert on a full element tree or a screenshot in a unit test. Pixels are the job of `port/tools/pixel-check`.
+
+## Tests that need more than a test window
+
+Some tests are `#[ignore]` because they need something a normal run does not have. Each says what in its attribute or its doc comment.
+
+- A real extension host: `live_host` in `app/src/host_sync.rs`.
+- A release build and a quiet machine: the performance gates in `app/src/syntax.rs` and `app/src/workbench/scroll_frame_time.rs`.
+- Signed packages: the update tests in `app/src/update/install/tests.rs`, fed by `port/tools/update-check`.
+
+A test with `#[ignore = "test.skip upstream"]` mirrors a test that upstream skips.
+
+## Comparing with VS Code
+
+- `port/tools/pixel-check` captures the reference window and the port's window in the same state and diffs them with the `pixeldiff` crate. Differences that stay are listed with their cause in `port/results`.
+- `port/tools/ui-pair` drives both apps through one step file and saves both screenshots, for states a still frame cannot reach: a drag, an inline editor, a dialog.
+- `port/tools/ref-dom`, `port/tools/ref-hover` and `port/tools/ref-contextmenu` read boxes and computed styles from the reference. Use them to get a size before you write it.
+- `port/tools/api-tests` writes its result to `port/results/api-tests.md`.
+
+## Before a report or a commit
+
+1. The narrowest check of the unit you changed.
+2. `cargo test` for each crate you touched.
+3. `bun port/tools/ledger-check.ts` and `bun port/tools/docs-check.ts`.
+4. For `unsafe` code: the checks of [memory-safety.md](memory-safety.md).
+5. For a hot path: the gate of [performance.md](performance.md), before and after.
diff --git a/docs/workbench.md b/docs/workbench.md
new file mode 100644
index 00000000000..0a777950260
--- /dev/null
+++ b/docs/workbench.md
@@ -0,0 +1,92 @@
+# Workbench
+
+The workbench is everything in a window around the editors: the layout, the parts, the views, keys, commands and menus. This file goes one level under [ARCHITECTURE.md](../ARCHITECTURE.md), section "Layers inside `app`". Upstream is `src/vs/workbench/browser` and `src/vs/workbench/services`.
+
+## One entity, many files
+
+`app/src/workbench.rs::Workbench` is the one GPUI entity of a window. It has a field for every model of the window: the file tree, the editor groups, the terminals, the settings, the storage, the extension host queue.
+
+The 280 files in `app/src/workbench` do not define types of their own that own state. Each one is an `impl Workbench` block for one upstream feature, named after it: `app/src/workbench/panel.rs` for `panelPart.ts`, `app/src/workbench/view_locations.rs` for `viewDescriptorService.ts`. A file holds the drawing of the feature, its input handling, its commands and its storage keys. Small state structs of a feature are fields of `Workbench`, defined in the feature's file.
+
+This shape replaces upstream's services and dependency injection. A feature that needs another feature calls its method on `self`. Jump-to-definition finds every call.
+
+**Invariant:** no file in `app/src/workbench` keeps a copy of state that another file owns. Read it from the `Workbench` field each time.
+
+**Do not** add a new entity for a feature because it "feels like a component". A new entity is right only for a separate window (`app/src/workbench/auxiliary_windows.rs::AuxiliaryWindow`) or for caching a part (`app/src/workbench/part_view.rs::PartView`).
+
+## Layout
+
+The window is a title bar, a grid of parts, and a status bar.
+
+- `app/src/workbench/layout.rs::Part` names the grid parts: activity bar, side bar, editor, panel, auxiliary bar. `app/src/workbench/layout.rs::Grid` is upstream's grid of `layout.ts` for the floating (modern) layout. It computes each part's rectangle from the window size, the part sizes and the positions of the side bar and the panel.
+- `app/src/workbench/part_grid.rs::PartGrid` holds part sizes and visibility, stores them under upstream's `LayoutStateModel` keys, and has the layout commands and the sashes between parts.
+- `app/src/workbench/part_view.rs::CachedPart` lists what draws as its own cached view: each grid part, the title bar and the status bar. `Workbench::render` places these views. A cached view draws again only after a `notify`.
+- Sizes are upstream's CSS pixels, taken from the reference's CSS and checked with `port/tools/pixel-check` and `port/tools/ref-dom`. Do not tune a size by eye.
+
+**Invariant:** a `notify` of the `Workbench` draws every part. A change that stays inside one part must notify that part's view: `app/src/workbench/part_view.rs::notify_editor_view` is the pattern. `app/src/workbench/part_view/tests.rs` counts renders per part and fails when a scroll in one part draws another.
+
+**Trap:** a size measured during a draw cannot call `Window::refresh` in that draw. Use `app/src/workbench/part_view.rs::redraw_after_frame`.
+
+## Parts
+
+| Part | Code |
+|---|---|
+| Title bar and command center | `app/src/workbench.rs`, `app/src/workbench/title_bar_actions.rs`, `app/src/workbench/update_title_bar.rs` |
+| Native menu bar (macOS) | `app/src/workbench/menubar.rs` |
+| Activity bar | `app/src/workbench/activity.rs`, `app/src/workbench/activity_bar_menu.rs` |
+| Side bar | `app/src/workbench/side_bar.rs` |
+| Panel and secondary side bar | `app/src/workbench/panel.rs` |
+| Editor part | `app/src/workbench/editor_groups.rs`, `app/src/editor_grid.rs`, `app/src/workbench/editor_view.rs` ([editor.md](editor.md)) |
+| Status bar | `app/src/workbench/status_entries.rs`, `app/src/workbench/editor_status.rs`, `app/src/main_thread_status_bar.rs` |
+
+## Views and view containers
+
+A view container is an icon in the activity bar or a tab in the panel. A view is a pane inside a container.
+
+- `app/src/workbench/view_containers.rs`: the containers, built in ones and the ones extensions contribute.
+- `app/src/workbench/view_locations.rs`: which container each view is in, after the user moved views (`views.customizations`).
+- `app/src/workbench/view_visibility.rs`: which views are hidden.
+- `app/src/workbench/view_panes.rs`: the collapsible panes of a container with their sizes.
+- `app/src/workbench/tree_view.rs` and `app/src/workbench/webview_views.rs`: the two kinds of views an extension contributes.
+
+Each built-in view has a model in `app/src` and a view file: `app/src/explorer.rs` with `app/src/workbench/explorer_actions.rs`, `app/src/search_model.rs` with `app/src/workbench/search_view.rs`, `app/src/scm.rs` with `app/src/workbench/scm_view.rs`.
+
+## Editor groups
+
+`app/src/editor.rs::EditorGroup` is the whole editor part. Its direct fields are the active group. The other groups wait in `other_groups`. `app/src/editor_grid.rs::EditorGrid` is the split layout.
+
+Code that draws or changes a group that is not the active one runs inside `app/src/workbench/editor_groups.rs::with_group`. It swaps that group in as the active one, runs the closure and swaps it back. `app/src/workbench/code_editors.rs::with_code_editor` does the same for the second code editor of one tab (a diff side, a notebook cell).
+
+**Trap:** inside `with_group` the "active" group is not the group the user sees as active. Code that reads focus or sends the active editor to the extension host must not run there.
+
+## Keys, commands, context keys, menus
+
+One path serves the keyboard, the mouse, menus and extensions:
+
+1. `app/src/workbench.rs` intercepts every keystroke of the window before the focused element gets it.
+2. `app/src/workbench/keyboard.rs::dispatch_key` asks `app/src/keybindings.rs::Resolver` for the command. The resolver holds the default table of the OS, extension keybindings and the user's `keybindings.json`, in upstream's order. Chords of two keys and their status messages are handled there.
+3. Each keybinding has a `when` clause. `app/src/contextkey.rs::Expr` evaluates it against the context keys that `app/src/workbench/keyboard.rs::key_context` builds from the workbench state and the focus.
+4. `app/src/commands.rs::execute_command` runs the command. A feature registers its commands in `Workbench::new` through `app/src/commands.rs::register`, under upstream's command ids. A command of an extension is a handler in the same registry that calls the host, and `execute_command` first waits for the `onCommand` activation of the id, as upstream does.
+5. Menus are data. `app/src/menus.rs` holds upstream's menu ids with their items, groups, order and `when` clauses. A context menu (`app/src/workbench/context_menu.rs`), the menu bar and a view title read the same registry and call `execute_command`.
+
+**Invariant:** `key_context` is cheap. It runs for every key and several times per frame for menus and toolbars. It must not read the disk, compile a regex or walk a whole list. A value that is costly to compute is cached where the state changes.
+
+**Do not** handle a key in an element's `on_key_down` for a command that upstream binds through a keybinding. The user cannot rebind it, and the `when` clause is lost.
+
+## Overlays
+
+Quick input (`app/src/quick_input.rs`, `app/src/workbench/quick_input_view.rs`), the command palette (`app/src/command_palette.rs`), hovers (`app/src/workbench/hover.rs`), notifications (`app/src/notifications.rs`, `app/src/workbench/notifications_view.rs`) and dialogs (`app/src/dialog.rs`) draw over the grid from `Workbench::render`. Each follows its upstream widget for position, size and keyboard handling.
+
+## Lists
+
+A list draws only the rows in view, as upstream's `listView.ts`. `app/src/workbench/list_scroll.rs` holds the shared scroll behavior: wheel sensitivity, smooth scrolling, scrollbars, the `list.scroll*` commands. A new list must not lay out rows outside the view: 800 Explorer rows cost 190 ms per frame before the Explorer drew only the visible ones (`port/notes.md`, "Benchmarks").
+
+## Add a workbench feature
+
+1. Find the unit in the ledger (`rg -n '^\| workbench/contrib/<name> ' port/ledger.md`) and read the upstream files it names.
+2. Put the logic that needs no window in a model in `app/src`, with upstream's tests.
+3. Add a file in `app/src/workbench` with the `impl Workbench` block: drawing, input, commands under upstream's ids, storage under upstream's keys.
+4. If the extension host must know, send through `app/src/host_sync.rs::HostSync` ([extension-host.md](extension-host.md)).
+5. Add `#[gpui::test]` tests that drive the feature with keys and clicks ([testing.md](testing.md)).
+6. Compare with VS Code: `port/tools/pixel-check` for a still window, `port/tools/ui-pair` for a state reached by input.
+7. Update the unit's row in the ledger in the same commit.