Skip to content

Hold keys pressed before a page presents, and speed up the cold first state read - #1213

Merged
max-sixty merged 7 commits into
mainfrom
early-keys
Sep 26, 2026
Merged

max-sixty merged 7 commits into
mainfrom
early-keys

Conversation

@max-sixty

@max-sixty max-sixty commented Sep 26, 2026 •

Copy link
Copy Markdown
Owner

A key pressed while a page was starting went nowhere. Before the ~160-module graph evaluated nothing listened; after it, the keyboard dispatched against a page with no state yet, so t (live only once the first /api/state answer brings threads) did nothing. On a cold local server the words painted at ~0.5 s and the page presented at ~2.1 s.

Held keys. The prepaint bootstrap (runtime/bootstrap.js) now holds keys from first parse, swallowing them so a half-started runtime never also runs them: an unmodified printed key pressed on the page starts the queue, and every printed key after it joins, Space included, since a held c may open the box the rest is text for. After 150 ms it shows the held keys where the bottom status stands ("Page still loading — t runs when it's ready"). At lf-presentation the keyboard controller takes them through a synchronous lf-held-keys event and works through them a frame apart via nextFrame: a key whose turn comes in a text box an earlier key opened is typed into it with insertText (the hold prevented its own insertion), and any other is pressed through the controller's own handler; a key pressed while it does joins the same queue, so nothing runs ahead of a key pressed before it, and the controller releases the hold once the queue is empty. Any key that is not printed (Enter, Backspace, Tab, an arrow, Escape, or a Ctrl, Alt or Meta chord), or a pointer press, ends the run: what was held is dropped and that press keeps its native meaning, which no replay through the keyboard owner could reproduce. So c h i Enter before presentation opens and sends nothing. A startup failure, or a page still unpresented ten seconds in, ends the hold and drops what it held, so a faulted first read cannot leave the keyboard swallowed. This is the early-event-capture pattern Angular's event replay (JSAction) uses.

Cold state read. _PassageParser._write and _OwnedWords.write grew their text with self.text += ch. CPython only extends a string in place for a local name, so on an attribute each append copied the whole run and the reading was quadratic: 274 ms for corpus.html, now 13 ms, with byte-identical readings across all 33 example and fixture pages. The first /api/state on a fresh server projects each neighbouring serving page through this; a cold read went from 1.42 s to 0.28 s and presentation from 2.09 s to 0.88 s in a local measurement.

Tests moved: the keydown inventory in test_interact_layer.py allows the bootstrap's hold as a third listener, and the presentation-wait test skips the bootstrap's timer when it shortens the first long one. New nightly cases in test_render_startup.py cover a key held behind the module and behind the first state read, a key pressed during the handover (g held, Shift+T at presentation still opens Threads), keys held behind c (c h i Space x leaves "hi x" in the comment box), Enter or a chord ending a run, Escape and pointer drops, and the ten-second release; each goes red with the hold or the hand-off removed.

Left for a separate change: the first HTML response on a fresh server still spends ~360 ms re-running the full source gate on an unchanged index.html (revisioning.activate_source).

This was written by Claude Code on behalf of max-sixty

🤖 Generated with Claude Code

max-sixty and others added 3 commits September 26, 2026 08:50
`_PassageParser._write` and `_OwnedWords.write` grew their text with
`self.text += ch`. CPython only extends a string in place for a local
name, so on an attribute each append copied the whole run and the
reading was quadratic in the page's length: 274 ms for corpus.html's
102k characters, now 13 ms. The first `/api/state` on a fresh server
projects every neighbouring serving page through this, which took a
cold read from 1.4 s to 0.3 s in a local measurement.

The readings are byte-identical across all 33 example and fixture pages.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A key pressed while a page was starting went nowhere. Before the module
graph evaluated nothing listened; after it, the keyboard dispatched
against a page without state, so `t`, whose command is live only when
the first `/api/state` answer brings threads, did nothing. On a cold
server that window was about 1.5 s after the words had painted.

The prepaint bootstrap now holds each unmodified printable key pressed
outside a text field from first parse, and after 150 ms shows the held
keys where the bottom status stands ("Page still loading — t runs when
it's ready"). At `lf-presentation` the keyboard controller takes them
through a synchronous `lf-held-keys` event and presses them through its
own handler, a frame apart. Escape or a pointer press drops them; a
startup failure, or a page still unpresented ten seconds in, ends the
hold and drops what it held.

Behaviour moved in tests: the keydown inventory allows the bootstrap's
hold as a third listener, and the presentation-wait test skips the
bootstrap's timer when it shortens the first long one.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The handoff can run a newer key before a held one. take() removes the bootstrap's capture listener immediately, while nextFrame(next) defers the first held press. A key arriving between those steps goes through the live document handler first. With the first state read held, I pressed g before presentation and sent Shift+T on lf-presentation: the Threads panel stayed closed because T ran before replayed g. Holding both keys before presentation opened it. Keep incoming keys in the same ordered queue until the held presses drain, so the advertised ordering survives the handoff.

The review on #1213 found the handover could reorder keys: `take()` removed
the bootstrap's capture listener at presentation, while the controller
pressed the first held key a frame later. A key arriving in that gap ran
on the live handler first, so `g` held and `Shift+T` pressed at
presentation ran as `T` then `g`, and the Threads panel stayed shut.

The hold now stands until the controller has pressed its last key: the
controller drains the bootstrap's own queue, a key pressed meanwhile
(Escape included) joins it, and the controller releases the hold when
the queue is empty. The loading notice comes down at the handover.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A held shortcut can open a text field, but later held characters never reach that field. I held the first state read, pressed c and then x, and released the read: the page comment box opened with an empty draft. bootstrap.js prevents the native keydown, and controller.js replays each key by calling press() directly; once c focuses the composer, x is claimed as text entry but no browser text insertion follows. The same loss applies to characters typed during the new replay queue. The handoff needs to preserve text entry when an earlier queued command changes focus, or stop promising that every held printable key will run.

The review on #1213 found that held `c` then `x` opened the page comment
box with an empty draft: the hold prevented `x`'s own insertion, and the
controller pressed it as a command, which the box claimed as text entry
without inserting anything.

A held key whose turn comes while focus is in a text box is now typed
into it with `insertText`, as the browser would have. Once a key is held,
every printed key after it joins the queue, Space and keys typed in a
field included, so the text keeps its order; a lone Space still scrolls
and a field keeps its own keys while nothing is held. `typesText` splits
out of `takesLetters` so the controller asks the same question focus.js
already answers.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The held comment can lose its submit press. With the first /api/state read held, I pressed c, h, i, then Enter; after releasing the read, the composer opened with hi still in it. hold() queues the printed keys but lets Enter reach the pre-presentation page immediately, before c can focus the composer. Enter is the desktop text.send binding, so the handoff needs to preserve its order behind the queued text and apply it at the focus reached by the earlier keys.

The review on #1213 found that after held `c h i`, Enter reached the
unpresented page at once instead of waiting behind the run, so the
comment never sent. Queueing Enter would fix that case, but Backspace,
Tab and the arrows act natively where focus stands, and pressing them
later through the keyboard controller cannot reproduce that.

So the hold holds a run of printed keys, and any other key ends it, as a
pointer press already did: what was held is dropped and the new press
keeps its native meaning. `c h i Enter` before presentation now opens
and sends nothing, and the notice comes down at the Enter. A modifier
alone is half a press and leaves the run standing. Escape needs no case
of its own any more.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A complete modified shortcut leaves an earlier held run pending. With c held during the first state read, pressing Ctrl+A leaves c in the queue; after the read completes, c opens the comment box. The shortcut has acted ahead of the earlier key. End the run when a Ctrl/Alt/Meta chord arrives, while letting that chord keep its native action. A standalone modifier can still leave the run pending.

Comment thread skills/leaf/assets/runtime/bootstrap.js Resolved
The review on #1213 found that the hold skipped modified keys before it
looked at the run, so with `c` held, Ctrl+A acted natively and the stale
`c` still opened the comment box after presentation, behind the chord it
was pressed before.

A printed key is now a single character with no Ctrl, Alt or Meta; Shift
still chooses the character. A chord therefore takes the same path as
any other key that is not printed: it ends an open run, drops what was
held, and keeps its native action. It never starts a run, and a modifier
pressed alone still neither starts nor ends one.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 793777e into main Sep 26, 2026
7 checks passed
@max-sixty
max-sixty deleted the early-keys branch September 26, 2026 17:16
max-sixty added a commit that referenced this pull request Nov 4, 2026
Leaf holds printed keys pressed before its runtime loads and shows "Page
still loading — `t` runs when it's ready". Since #1213 it also dropped
them if the page hadn't upgraded ten seconds after it started, reading
such a page as faulted. On a loaded Mac a healthy page takes longer than
that, so keys the notice promised to run were dropped:

- `test_held_keys_after_one_that_opens_a_box_are_typed_into_it` failed
at load ~107. A page-side trace showed the keys held at 2.6 s, the
notice gone at 15.4 s, and the page upgrading at 15.6 s with nothing
left to take.
- The phone variant of
`test_a_key_pressed_before_presentation_runs_once_the_page_presents[True-module]`
failed the same way at load ~90.
- 8× CPU throttle reproduces both. Over the same hour `dynamic-widgets`
took 18.8 s to upgrade under `leaf-dev probe`.

This reverses #1213's deadline. The hold now ends when the runtime takes
it, when the page reports it cannot start, or when the user lets go, and
never on a clock. On a page that truly hangs the hold costs little: only
printed page keys are held, any other key or a pointer press already
ends the run and acts natively, and "Page still loading" is then true.

Every mode that runs the hold now ends it:

- **Upgrade:** the keyboard controller takes the run, as before.
- **A startup fault:** a failed entry module or stylesheet, an uncaught
error before upgrade, or `boot()` rejecting all reach `recover`, which
ends the hold, as before.
- **A document the runtime gives up on:** `prepareDocument` returns
false when the registry names another layer, and `boot` returns without
upgrading. Only the deadline ended the hold here. It now ends at that
return, through `letHeldKeysGo` in `keyboard/controller.js`.
- **A sample opened as a window of its own:** no containing page will
connect it, so `boot` waits on `lfSample.ready` forever. It now holds
nothing. This one is confirmed by reading the code rather than running
it.
- **Quick shortcuts off:** each key lets go at once, as before.
- **Exports:** they don't run `bootstrap.js`.

The tests follow:

- `test_upgrade_relinquishes_the_bootstrap_timeout_for_held_keys`
becomes `test_held_keys_outlast_any_wait_for_the_runtime`, which
advances the page's clock 30 s past a held `t` and checks the `t` still
walks to the thread.
- The journey `test_a_page_that_never_presents_lets_its_held_keys_go`
waited ten real seconds for the deadline. It becomes
`test_a_page_that_cannot_start_lets_its_held_keys_go` in
`browser/startup/test_keys.py`, where a failed runtime module ends the
hold and later presses reach the page.
- `test_a_document_the_runtime_gives_up_on_lets_its_held_keys_go` covers
the give-up path. With `letHeldKeysGo` taken out it fails with the
notice still standing.
- The tests that hold the runtime module now press their keys once the
body is parsed, since holding the module's request doesn't prove
bootstrap is listening. One local run lost its first key that way, with
Playwright's fake clock installed; that hasn't reproduced without the
clock, and the wait costs nothing either way.

## Testing

After merging `main`: the six held-key tests above and
`test_a_key_that_is_not_printed_ends_the_held_run` (10 items), serially
under `taskpolicy -b`, pass; `pnpm run test:runtime` (813) and `pnpm run
check:browser` pass. Before the merge, B and the phone's `[True-module]`
also passed at `--cpu-throttle 8`, where both failed with the deadline.
`main` has since fixed the phone variant's stale locator itself (#5350),
so this branch keeps main's reading of it.

> _This was written by Claude Code on behalf of max-sixty_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants