Skip to content

Bound how long a code line's colouring takes by its length instead of a 500 ms time limit - #6769

Merged
leaf-agent merged 3 commits into
mainfrom
fix/issue-6767-syntax-time-limit
Nov 23, 2026
Merged

leaf-agent merged 3 commits into
mainfrom
fix/issue-6767-syntax-time-limit

Conversation

@leaf-agent

@leaf-agent leaf-agent commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Shiki stops tokenizing a line after 500 ms by default (tokenizeTimeLimit) and leaves the rest of that line unstyled. Because that limit is measured in time, whether a line gets fully coloured depends on how loaded the machine is. A language's first tokenized line also pays to compile the grammar rules it reaches. On a loaded machine that can exceed 500 ms, so the first block in a language ends its line in plain ink. That's the language aliases failure in #6767: on the macOS full-tier run 45044712114, the js block was coloured first and lost its trailing // keep the source token, while the javascript block below it kept it.

tokenize in the syntax entry now bounds a line by its length instead: tokenizeTimeLimit: 0 with tokenizeMaxLineLength: 20000 (VS Code's cap). A shorter line is coloured to its end on any machine. A line at or past 20,000 characters, such as minified code, comes back as one plain token, with its text intact and the grammar state carried to the next line. The vendored syntax.esm.js is rebuilt from the entry. A worker would remove the bound altogether, but the maintainer chose the length cap in review.

Cost. A line just under the cap still takes about 0.8 s unloaded. Above the cap there's no wait: in Node, a 102 KB minified line takes 15 ms, against 518 ms on main and 3.4 s with no limit at all.

Verification.

  • tests/runtime/syntax-line-length.test.mjs (new): a line past the cap stays plain with its text intact and the next line is still coloured. It fails with the cap set to 0 and passes with it.
  • test_code_colouring_keeps_each_blocks_words_and_annotations under leaf-dev suite flake --copies 6 --workers 3 --cpu-throttle 20 on Linux failed 6 of 6 on main and passed 6 of 6 with the time limit off. The length cap doesn't affect that test's short lines.
  • pnpm run check:browser passes.

#6767 also lists four other one-off macOS failures, which this PR doesn't touch.

Refs #6767 — automated triage

Shiki stops tokenizing a line after 500 ms by default and returns the
rest of the line unstyled. The first line in a language also pays to
compile the grammar's rules, and on a loaded machine that can exceed
500 ms, so the first block's line kept its tail in plain ink. On the
macOS full-tier run 45044712114, the alias block's trailing comment came
out uncoloured. The syntax entry now passes tokenizeTimeLimit: 0.

test_code_colouring_keeps_each_blocks_words_and_annotations under
leaf-dev suite flake --cpu-throttle 20 failed 6/6 copies before the
change and passed 6/6 after it.

Refs #6767

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Removing Shiki's per-line time limit makes a long line freeze the page for seconds on every machine. That is a bigger regression than the uncoloured tail it fixes, which appears only on a loaded machine. The inline comment has the measurements and three candidate fixes. Choosing among them means deciding how much main-thread time a line may take, so I'm leaving that decision here rather than pushing one.

Comment thread build/entries/syntax/entry.mjs Resolved
…e-6767-syntax-time-limit

# Conflicts:
#	browser/vendor/syntax.esm.js
@leaf-agent

Copy link
Copy Markdown
Collaborator Author

This PR is waiting on a decision that the review's inline comment leaves open, so I haven't pushed a fix and it isn't landing. With tokenizeTimeLimit: 0, one long line holds the main thread for seconds on every machine: about 3.4 s for a 102 KB minified line, against 0.5 s at the current 500 ms limit. That is worse than the uncoloured tail it fixes, which only shows on a loaded machine. The candidates are a length cap such as VS Code's 20,000 characters, tokenizing in a worker, or keeping the time limit and warming the grammar first. Picking one means deciding how much main-thread time one line may take.

The red test at 2d9a9cab isn't from this change. test_every_skill_file_reads_in_one_command fails on main too, because skills/leaf/references/module-input.md is 30,004 characters there, and #6826 brings it back under the limit.

…tokenizing it

Shiki's 500 ms per-line time limit stays off, so a line under the cap is coloured to
its end however loaded the machine is. A line at or past the cap, such as minified
code, now comes back as one plain token instead of holding the main thread for
seconds (a 102 KB line took 3.4 s uncapped and now takes about 15 ms). The
maintainer chose the length cap over tokenizing in a worker (review on #6769). The
cap is VS Code's.

tests/runtime/syntax-line-length.test.mjs protects the bound: a line past the cap
stays plain with its text intact, and the line after it is still coloured.
@leaf-agent leaf-agent changed the title Colour a code line to its end however long its first tokenizing takes Bound how long a code line's colouring takes by its length instead of a 500 ms time limit Nov 23, 2026
@leaf-agent
leaf-agent merged commit 5ab55f1 into main Nov 23, 2026
8 of 9 checks passed
@leaf-agent
leaf-agent deleted the fix/issue-6767-syntax-time-limit branch November 23, 2026 07:00
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