Repository navigation
Serve Leaf's stylesheets from their sources, so a CSS edit needs no build - #3886
Conversation
…uild
An edit to a source sheet such as `browser/styles/components/controls.css` or
`packages/default/styles/tabs.css` reached no page until `pnpm run build:browser`
rewrote the committed copies (`browser/theme.css`, `browser/shadow.css`,
`browser/runtime/chrome.css`, and each package's `theme.css`/`shadow.css`). Until
then `leaf-dev probe`, previews and the browser tests served the old rules without
saying so, and agents concluded correct fixes were wrong.
The runtime already avoids this for JavaScript: a kernel whose recorded inputs no
longer match the tree is passed over for the source modules. That fallback can't
extend to CSS, because the browser can't run the sources (composition needs complete
sheets, adopted sheets can't `@import`, and the kernel theme imported Web Awesome from
`node_modules`, which an install doesn't have). So instead of detecting a stale copy,
this removes the copies:
- `layer.stylesheet(source, root)` is now the one reading of a sheet. It validates the
sheet, drops its comments, and replaces each `@import "relative.css"` (optionally
`layer(name)`) of a file inside the package root with that file's sheet, each file
keeping its own EOF recovery. Composition reads every root sheet through it, and
`runtime_selection` serves each runtime `.css` through it, so `runtime/chrome.css`
imports `../styles/floating-panel.css` directly.
- The import entries moved over their generated copies: `browser/theme.css`,
`browser/shadow.css`, `browser/runtime/chrome.css`, and the theme and shadow sheets
of `default`, `diff` and `visual-review` are now the authored files.
- `build/styles.mjs` bundles only the third-party sheet, Web Awesome's theme, into
`browser/styles/vendor/webawesome-theme.css`, which `browser/theme.css` imports in
`layer(lf-vendor)` as before. The Dependabot publisher accepts that one output.
- `layer.input_paths` and `payload_server_inputs` include the files a sheet imports,
so a live preview re-vendors (or restarts) on an edit to one.
- A package author can now split a sheet into files with `@import`
(`references/packages.md`, "Styles and composition"). An import outside the package,
of a URL, or with a media or supports condition is refused with a message.
Evidence
- Repro on the base: append `:root { --grind-controls-marker: fresh }` to
`controls.css` and `:scope { --grind-tabs-marker: fresh }` to `tabs.css`, then
`leaf-dev probe examples/alert-review.html --js` reading both properties returned
`{"controls": "", "tabs": ""}`; after `pnpm run build:browser`, `"fresh"` for both.
With this change and no build, the same probe returns `"fresh"` for both, and a
marker in `styles/floating-panel.css` reaches the adopted chrome sheet. A live
`leaf-dev preview alert-review` re-vendored its page within 8 s of a `tabs.css` edit
(load 30).
- What a page receives is unchanged: the composed `theme.css` and `shadow.css` of the
kernel, default, diff and visual-review packages, and every runtime sheet, are
token-for-token equal to the base's, ignoring comments, whitespace, quoting that
CSS treats alike (`[a="b"]`/`[a=b]`, `url("x")`/`url(x)`), and esbuild splitting
Web Awesome's `@layer lf-vendor` block per file.
- Bytes: composed `theme.css` 330,752 to 302,720 and `shadow.css` 78,874 to 76,967,
since comments no longer reach a page; the runtime sheets carried in each document
drop by about 21 KB (annotation-chrome 31,373 to 15,780) while `chrome.css` grows
58,799 to 59,622 from source indentation.
- Cost, median of 15 uncached runs on a shared host at load 20-30: composing the
default layer 97-110 ms on the base and 100-112 ms here; `read_runtime`, once per
server process, 17-19 ms on the base and 46-58 ms here, since it now parses about
145 KB of runtime CSS.
Tests changed: a composition test of imports in place and in a layer, EOF recovery
per file and their appearing among the layer's inputs; refusals of imports that leave
the package or carry a condition; an edit to an imported runtime sheet reaching the
served `chrome.css`. Tests that read the generated sheets now read the whole sheet
through `layer.stylesheet`, and two that ordered packages by a comment in their sheets
order them by a rule, since comments no longer reach the composed sheet.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent
left a comment
There was a problem hiding this comment.
The new stylesheet inputs need to cross the package snapshot boundary too. A package with theme.css containing @import "./styles/tabs.css"; and a valid styles/tabs.css passes package check, but package install refuses it after staging: the imported file is missing from the staged package. I reproduced this through check_package followed by cmd_package_install.
copy_package_contract in src/leaf/packages.py still copies only VENDORED_FILES, PACKAGE_DIRS, and scripts; styles/ and other newly supported import locations are outside those sets. The same copier stages package init --widget candidates. The package snapshot needs to preserve the stylesheet dependency closure so the newly documented split-sheet capability works for installed packages as well as project-relative ones.
A sheet importing one that imports it back made `stylesheet()` and `imported_sheets()` recurse until RecursionError, and `input_paths`, which vendoring and a live preview call, reads the latter. Each now carries the chain of sheets it has open: composition refuses a cycle with the chain (`a.css -> b.css -> a.css`), and `imported_sheets` leaves the repeat out as it does any import composition refuses, so a reader following the inputs still finishes. `layer.py` now imports tinycss2 in the two functions that use it rather than at module level, so this change and one that drops the module-level import merge in either order. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Main renamed the package sheets (#4205): each package's import entry is now its widgets.css or widget-shadow.css, so those move over their generated copies as theme.css and shadow.css did. The import entries take main's current content with their imports rewritten; layer.py keeps main's role-based composition and reads each sheet through stylesheet(). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A package whose sheet imported a file passed package check but failed package install: copy_package_contract staged only the package's top-level files and directories, so the imported file was missing from the staged copy. It now stages each file a copied sheet imports, at its path in the package. Co-Authored-By: Claude Opus 5.5 <[email protected]>
leaf-agent
left a comment
There was a problem hiding this comment.
Imported stylesheets can still make a valid package fail during installation; the inline finding reproduces the remaining staging collision.
An import under widgets/ or scripts/ made copy_package_contract create that directory before copytree, which then refused it. Imports are now staged last, and one a directory copy already brought is left alone. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This week's duplicate sweep of `dev/`, `build/`, `tests/` and `worker/` (week 47) moves six duplicated helpers onto one home each and moves every caller. Behavior stays the same. The touched model tests, the dev js_facts tests and the Worker's routing, storage and index tests pass. A script compared the old and new outputs of the consolidated `leaf_dev` functions and the seeded-log reads across the corpus, and they match. | Question | Home | Copies removed (introducing PR) | |---|---|---| | Which stylesheets do the core and the bundled packages ship, leaving out vendored files? | new `interact_support.shipped_sheets` | `sheets()` in `test_interface_fonts.py` (#4804) and `test_theme_tokens.py` (#6025). Five inline loops in `test_interact_layer.py` (#3138, #3886) | | Run `tests/js_facts.mjs` | `interact_support.js_facts` (#2666) | its own subprocess call in `dev/tests/test_js_facts.py` (#5118) | | Lowercase hex of bytes in the Worker | `routing.hex`, which is `newSessionId` (#313) renamed, plus a `sha256Hex` in `storage.ts` | the publication-identity and chunk-digest copies in `storage.ts` (#2652) | | Is this pid alive? | `leaf.machine.pid_alive` | `_alive` in `tests/process/test_codex_tool.py` (#3331). `running()` in `test_interact_layer.py` (#1774) | | Read an example's seeded log | `read_event_lines(seed_log(source))` (#4484) | inline parsers in `test_render_pages.py`, `test_interact_product.py` (#924) and `test_render_gate.py` (#509). The last one split lines with `splitlines()`, which also breaks at U+2028 | | Paths of the bundled registries and packages; which registry keys name elements | `leaf.schema.ASSETS` / `BUNDLED_PACKAGES` / `DEFAULT_PACKAGE`, `leaf.registry.contract.is_element_name`, `docs.registries()` | `docs.py`'s own `ASSETS`/`PACKAGES` (#3593) and its `startswith("lf-")` checks (#2660). `logs usage`'s hand-written regex and registry list (#5970). `ROOT / "packages" / "default"` in `stills.py` (#3927) and `test_product_page.py` (#3593) | `is_element_name` and the two checks it replaces give the same answer for every key in the bundled registries today. A selector key such as `lf-options.selected` would have passed the `lf-` prefix test. <details><summary>Sets found and left alone</summary> - **Sets an open PR is changing.** Nothing in this diff overlaps another open PR's hunks. #6732 moves tests out of `test_render_gate.py`, `test_render_pages.py` and `test_interact_layer.py`, but none of the tests changed here. - **Changes to browser tests with nothing run to check them.** The WCAG contrast computation exists as about 11 copies across browser, journey and model tests. Other copies: about 9 `EDITOR_ROOTS` shadow-root keepers, the `/api/interaction` batch predicates, CDP `touch()` closures, one-line patch templates, reply-holding route handlers, and the `data-lf-entry` runtime-module import snippet in `dev/leaf_dev/states.py` and in tests. Each would mean editing browser tests that this sweep can't run on a shared host. They suit a sweep that can run those tests. - **Vendor bundling.** `build/browser/build.mjs` builds signals-core and Lit with esbuild calls that repeat `build/vendor.mjs`'s `bundle()`. Merging them changes build options and needs a full `check:browser` byte comparison, so it was left for a change that runs that. - **Copies that are near but not the same.** `_build.py`'s `check_links` repeats `leaf_website.released_path`, but it also needs the matched page's root and kind. The list of API-key variables an eval drops is repeated across Python and the Promptfoo configs, as subsets and supersets of one another. The Codex token-expiry check has different thresholds, 1 h and 60 s. - **Too small to be worth it.** Picking a free loopback port (`eval_user.py`, `interact_support.available_loopback_port`). The XDG state root in `leaf_dev.state_home` and `leaf.state.state_home`. The `settled` helper in two `tests/runtime` files. </details> Co-authored-by: leaf-agent <[email protected]>
An edit to a source sheet such as
browser/styles/components/controls.cssorpackages/default/styles/tabs.cssreached no page untilpnpm run build:browserrewrote the committed copies (
browser/theme.css,browser/shadow.css,browser/runtime/chrome.css, and thewidgets.css/widget-shadow.cssof thedefault,diffandvisual-reviewpackages). Until thenleaf-dev probe, previewsand the browser tests served the old rules without saying so, and agents concluded
correct fixes were wrong.
The runtime already avoids this for JavaScript: a kernel whose recorded inputs no
longer match the tree is passed over for the source modules. That fallback can't
extend to CSS, because the browser can't run the sources (composition needs complete
sheets, adopted sheets can't
@import, and the kernel theme imported Web Awesome fromnode_modules, which an install doesn't have). So instead of detecting a stale copy,this removes the copies:
layer.stylesheet(source, root)is now the one reading of a sheet. It validates thesheet, drops its comments, and replaces each
@import "relative.css"(optionallylayer(name)) of a file inside the package root with that file's sheet, each filekeeping its own EOF recovery. Composition reads every root sheet through it, and
runtime_selectionserves each runtime.cssthrough it, soruntime/chrome.cssimports
../styles/floating-panel.cssdirectly.browser/theme.css,browser/shadow.css,browser/runtime/chrome.css,packages/default/widgets.css,packages/diff/widgets.css,packages/diff/widget-shadow.cssandpackages/visual-review/widgets.cssare now the authored files. The old entries(
browser/styles/theme.css,browser/styles/shadow.css,browser/styles/chrome.cssand each package's
styles/widgets.css/styles/widget-shadow.css) are gone.build/styles.mjsbundles only the third-party sheet, Web Awesome's theme, intobrowser/styles/vendor/webawesome-theme.css, whichbrowser/theme.cssimports inlayer(lf-vendor)as before. The Dependabot publisher accepts that one output.layer.input_pathsandpayload_server_inputsinclude the files a sheet imports,so a live preview re-vendors (or restarts) on an edit to one, and
packages.copy_package_contractstages them, sopackage installandpackage init --widgetkeep a split sheet whole(
test_an_installed_theme_keeps_the_files_its_sheet_imports).@import(
references/packages.md, "Styles and composition"). An import outside the package,of a URL, or with a media or supports condition is refused with a message, and so is
a cycle of imports, naming its files.
To edit Leaf's CSS after this, edit the authored sheet or the file it imports; there
is nothing to rebuild. A branch that still edits a removed entry moves the edit to
the file that replaced it, and one that rebuilt a generated copy drops that hunk.
Evidence
:root { --grind-controls-marker: fresh }tocontrols.cssand:scope { --grind-tabs-marker: fresh }totabs.css, thenleaf-dev probe examples/alert-review.html --jsreading both properties returned{"controls": "", "tabs": ""}; afterpnpm run build:browser,"fresh"for both.With this change and no build, the same probe returns
"fresh"for both, and amarker in
styles/floating-panel.cssreaches the adopted chrome sheet. A liveleaf-dev preview alert-reviewre-vendored its page within 8 s of atabs.cssedit(load 30).
merging it: the composed
theme.cssandshadow.cssof the kernel with the default,diff and visual-review packages, and every runtime sheet, are token-for-token equal
to main's, ignoring comments, whitespace, quoting that CSS treats alike
(
url("x")/url(x)), and esbuild splitting Web Awesome's@layer lf-vendorblockper file.
theme.css307,761to 279,268 and
shadow.css68,071 to 66,179, since comments no longer reach a page;the annotation sheets carried in each document drop by about 24 KB
(annotation-chrome 35,330 to 17,112) while
runtime/chrome.cssgrows 62,945 to63,901 from source indentation.
load 20-30: composing the default layer 97-110 ms before and 100-112 ms after;
read_runtime, once per server process, 17-19 ms before and 46-58 ms after, sinceit now parses about 145 KB of runtime CSS.
Tests changed: a composition test of imports in place and in a layer, EOF recovery
per file and their appearing among the layer's inputs; refusals of imports that leave
the package or carry a condition; an edit to an imported runtime sheet reaching the
served
chrome.css. Tests that read the generated sheets now read the whole sheetthrough
layer.stylesheet, and two that ordered packages by a comment in their sheetsorder them by a rule, since comments no longer reach the composed sheet.
Calls Max may reverse
offered serving from sources (a) or refusing (b); (a) by fallback would have needed
this same import resolver beside esbuild's, so the generated copies go instead.
custom packages' included; esbuild already dropped them from the bundled ones.
@importfiles inside their package (a new authoringcapability), with
layer(name)and nothing else.Not verified here: the journey-tier
tests/journeys/test_site.pyedit, the browserrender gate as a whole, and Firefox.
Ran after the last merge of main:
tests/model/test_interact_layer.py,test_distribution.py,test_revision_delivery.py,test_themes.py,interact_contract/test_layer_contract.py,interact_contract/test_revendoring.py,dev/tests/model/test_interact_preview.py(itstest_preview_file_subscription_follows_atomic_replacementsfails on main too), thepalette tests of
test_interact_document.py, the streamed-host test oftests/browser/test_render_initial_delivery.py, the idiom catalog test oftests/browser/test_render_gate.py, andpnpm run check(check:browseragain after the last merge).