Files
lucisv/docs/features/lsp-viewport-folding-r9
Guoliang ChenandClaude Mythos 5.2 dc54b23572 Round 9: exact semantic-token refresh after rescans and cross-file edits (#59, #60)
A range answer is remembered with its tokens and compared at the settle
decision, so a rescan that changed nothing draws no refresh. An edit that
changes the kind of something another open buffer references sends one
workspace/semanticTokens/refresh when that buffer's colours really differ.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-26 17:50:05 -08:00
..

lsp viewport, folding & sticky scroll (Round 9)

Goal

Close docs/topo.md §2.9 rows 59 and 60 in lucisv_server, and the open polish items of TODO §67 / §72 (folding and sticky scroll). Row 59: a semanticTokens/range answered during a rescan always counted as a guess (Viewport::guessed) and bought one workspace/semanticTokens/refresh at settle even when the settled answer was the same. Row 60: a save in one file that changes how another open file colours (a package type renamed, a class or interface declared or removed) sent no refresh, so the other file kept stale colours until its own next edit. A bug-fix round: no new features; the direction to fail in is always "one refresh too many", never a missed one.

Assumptions

  • What a colour answer depends on. tokens::semantic_tokens(doc) and tokens::range_answer(doc, range) read the Document alone: its text, line index, lexer tokens, tree, Semantics, large flag and overlay (ExternalResolutions). Not the index, not another buffer, not a setting. True by signature today; the #59 check relies on it (it re-asks the same function), and so does the claim that only the overlay can carry an edit in another file into a buffer's colours.
  • What colouring reads of the overlay. Only resolved_kind (identifier_type, and the instantiation walk's bind and expression checks), which reads only definition_of — the kind of the first workspace declaration of a reference the file does not resolve itself, or nothing. tokens::overlays_colour_alike compares exactly that projection. A new reader of the overlay inside tokens/ must extend it.
  • Macros. A macro use always colours macro and a directive always colours macro, whatever is defined anywhere, so a `define changed in another file cannot recolour a buffer and draws no refresh (tested).
  • A client draws the latest answer for a range. When the same range is asked twice, only the second answer is remembered. Distinct ranges are all kept (a client may merge them, as VS Code does), up to 32 per buffer; past that the record counts as stale whenever checked.
  • Every index mutation bumps hierarchy_revision (as the viewport-highlight README already assumes). The decision also runs when a buffer's colour mark is set without a bump, so a future mutation that forgets the bump still refreshes.
  • A range answer is remembered only for a client with refreshSupport: one without can never be told again, so the record would buy nothing.

Handshakes

  • Consumed, unchanged: vlog_semantic::ExternalResolutions::iter, ExternalTarget::best, Def::kind, SymbolKind: PartialEq. No change to vlog_semantic or any crate outside lucisv_server.
  • Inside lucisv_server, changed:
    • state::Document gains a private colours_moved mark, set by refresh_overlay when overlays_colour_alike(old, new) is false; read by take_colours_moved / colours_moved. Workspace gains take_moved_colours and any_colours_moved. Every overlay refresh site (state/incremental.rs refresh_overlays, state/rescan.rs publish, finish and widen) goes through Document::refresh_overlay, so all of them mark — none was edited.
    • tokens::range_answer (new) is the range request's one function — dispatch.rs now calls Server::semantic_tokens_range (semantic_tokens.rs), which answers through it and records the answer (Viewport::answered_range). tokens::overlays_colour_alike (new).
    • viewport.rs: Server::coloured_ahead is removed — the range answer is recorded where it is made, ahead or in order, exactly as remember_tokens has always done for whole answers. ahead.rs no longer calls it (fast-actions' file; one call deleted).
    • didClose (notification.rs) also calls Viewport::forget.
  • No requests to other modules.

Status / TODO

  • #59 failing tests, then fix.
  • #60 failing tests, then fix.
  • TODO §67 / §72 open items listed and dispositioned (nothing to build; see the work log).
  • Per-edit overhead measured on OpenTitan.
  • OpenTitan folding and viewport sweeps run, compared with trunk.
  • fmt, clippy, guarded cargo test -p lucisv_server, stamp check.

Work log

2026-11-26 — dev

#59 — the exact "nothing changed". A range answer is a pure function of the buffer and the range (tokens::range_answer; Assumptions). So the client's colours for a range are current exactly when that function, asked again now for the same range, returns the same tokens. Each range answer is now remembered with its tokens (viewport/painted.rs, Painted), and a buffer answered a range off an unsettled index (guessed) is stale at the decision iff some remembered range re-answers differently, or the record overflowed. This compares outputs, so no input can be forgotten: an overlay that moved, a text that changed, a large-file flip all show up as a different answer. The whole-answer half (early) was already exact and is unchanged. The record is dropped by a whole answer for the buffer (which supersedes it, as before) and by didClose.

#60 — the trigger. A buffer's colours read the index only through its overlay, and the overlay changes only in Document::refresh_overlay, which every index mutation runs for every open buffer. It now compares the old and new overlay on the one projection colouring reads — (reference, kind of the first workspace declaration) — and marks the buffer when it differs. The refresh decision checks the marked buffers too (open, not large, not dirty): re-classify, compare with the remembered whole answer and every remembered range, one refresh if any differs. So the condition is: the edited file's record moved (update_facts said so; an unchanged record refreshes no overlay at all) and some open buffer's overlay now gives a reference a different kind and that buffer's re-classified answer differs from what its client was sent. Each stage is exact for what it decides; only the last sends anything. Gated on refreshSupport as before.

Removed coloured_ahead. The range answer is recorded in semantic_tokens_range, whoever calls the dispatch; guessed is set there when index_unsettled(), the same test remember_tokens uses for early. Equivalent for every answer given ahead (nothing in-order is answered while unsettled), and it removes the one place the response JSON was inspected.

Tests (written first). server/viewport/refresh_tests.rs (20) and tokens/overlay_tests.rs (6). On a trunk snapshot (git archive 532a645), the 14 that compile against trunk's API: 10 fail (both #59 no-refresh cases; every #60 recolouring case — rename, add, class removed, package in a new file, interface declared, watcher one-file update, whole rescan, range-only client) and 4 pass (the refresh-still-happens and no-refresh guards). The other 12 use the new record/mark and pass only with the fix. One existing test, a_guessed_range_is_refreshed_and_a_client_that_cannot_be_asked_is_not, encoded the #59 bug (a guessed range with nothing changed drew a refresh); it now makes the remembered range differ first, as its whole-answer siblings already did.

Measured, OpenTitan hw (release, colour_perf.rs, whole tree indexed, prim_mubi_pkg.sv plus its first 200 users open, medians of 5):

save of prim_mubi_pkg.sv update_one, trunk update_one, R9 buffers marked re-classify marked
no change 122 µs 121–124 µs 0 0
comment appended 135 µs 114–118 µs 0 0
localparam added (record moves, no colour) 98.7 ms 100.9–102.4 ms 0 0
mubi4_t renamed (every user recolours) 103.1 ms 101.8–102.3 ms 188 26.2 ms

The comparison itself over all 201 open buffers: 1.87 ms per save that moves the record (it runs only then), ~9 µs a buffer, inside the run-to-run noise of the ~100 ms overlay refresh it rides on. The re-classification is paid only for marked buffers with a remembered answer, i.e. exactly when a refresh is owed; 26 ms is its upper bound here (188 buffers, all checked). A save that changes no colour re-classifies nothing (tested: an_edit_elsewhere_that_changes_no_colour_draws_no_refresh asserts no mark).

TODO §67 / §72 — open items found:

  • §67 [ ] Seen in a running VS Code (owner) — the owner's check; not something a dev can do. Untouched.
  • §67 known limit: a layer with no begin (one-statement if, a case item without begin) has no fold and so no pinned line; a begin on its own line pins that line, not the always above. Fixing either adds folds or moves fold start lines — a behaviour change to every client's folding (collapse would hide different lines), i.e. a feature needing an owner decision. Not built.
  • §72: every item checked; nothing open. Its feature README (docs/features/instance-folding/README.md) still shows the VS Code box unchecked although TODO §72 records it seen on 2026-11-19 — a stale box in another feature's README, left for the lead. Its review's "2-D instance arrays need the parser to read them as instantiations first" is a vlog_parser feature, not polish. Not built.
  • §65.8 (tb. completion) is WON'T FIX; not touched.

So TODO.md is unchanged.

Deviation. One filtered test run (cargo test ... an_interface_declared, one test, <1 s) was run without tools/guard.sh by mistake; every other run was guarded.

QA log

2026-11-26 — QA (Opus 5.5)

Verdict: PASS. No missed refresh found and no refresh storm. Two LOW and a few INFO notes follow.

Purity claim (#59). Read tokens.rs, tokens/instantiation.rs and large::window. The range answer and the whole answer read only the Document: its text, line index, tokens, tree, semantics, external and large. No setting, index, other buffer, include table, macro table or clock is read. large_file_threshold is the only client setting that can change colours. A threshold change re-installs the flipped buffers and sends its own refresh (server/large_file.rs:210), as it did before this round.

Projection (#60). Every overlay read in colouring goes through resolved_kind → definition_of → defs.first().kind. overlays_colour_alike compares best().kind per ReferenceId, which is the same value, and iter() is sorted. Modifiers read only Semantics. Every index mutation (update_facts, remove_file, three absorb sites) ends in refresh_overlay for every open document. The one exception is update_one returning early when the record did not move, which is correct. Documents are created only by set_document, and the client asks again after that.

Scenario attacks. 23 temporary tests in server/viewport/qa_attack_tests.rs, now removed. Each one asserts refreshes == (answer before != answer after) for the user file:

  • These drew 1 refresh, and the colours did differ:
    • typedef → class with the same name (whole and range);
    • a closed package deleted on disk via the watcher (type 3), whole and range;
    • a new file created on disk (type 1) that declares the name;
    • a wildcard import p::* name resolving later;
    • an enum member that becomes a localparam;
    • a class member that changes from var to function;
    • the first of two declarations (by path) deleted, with the second of another kind;
    • two saves before one decision with a net change;
    • an open package edited and saved, then closed so it reverts to disk.
  • These drew 0 refreshes, and the colours did not differ:
    • parameter → localparam;
    • module → interface;
    • two saves that revert each other before the decision;
    • a large user;
    • an unsaved (flushed) package edit, since the index takes saves only;
    • an unchanged didChange on the user between the range answer and the settle.
  • These were exercised and behave as expected:
    • a second edit after the client re-fetched draws another refresh, and the next idle decision draws 0;
    • close and reopen;
    • whole then range: a stale range alone is enough;
    • user dirty at the decision;
    • a client without refreshSupport keeps no painted and no guessed, and its marks are still taken.

Findings.

  • LOW (this round, documented limit): server/viewport/painted.rs:46-49. Once a buffer passes 32 distinct ranges, overflowed stays set until a whole answer or didClose, and later ranges are not recorded. A range-only client therefore gets one refresh at every unchanged rescan that answered a range mid-scan. Measured: 3 unchanged rescans gave [1, 1, 1]. This is the #59 behaviour coming back in that regime. It is no worse than trunk and bounded by rescans. VS Code is unaffected, because its first whole answer clears the record.
  • LOW (this round): painted is not cleared when a buffer is edited. If the user edits A between a mid-scan range answer and the settle, the old ranges compare against the new text and can buy one unneeded refresh. The dev's test a_range_coloured_before_its_buffer_changed_is_refreshed covers this, and it does not repeat.
  • INFO: painted is also kept for large buffers. They are never checked (stale_token_buffers filters large), so those records are dead memory until 32 windows overflow them.
    • Bound: 32 × (tokens in the asked range) × 20 B per buffer, released on a whole answer, on overflow and on close.
    • The colours_moved comparison runs on every record-moving save, even for a client without refreshSupport: about 1.8 ms with 201 buffers.
  • INFO: cost.
    • A guessed range costs O(window) at settle.
    • A marked buffer with a remembered whole answer costs O(file), measured at 25.6 ms for 188 buffers.
    • The projection costs O(references) per open buffer per moved record.

#59 / ahead.rs. On trunk, coloured_ahead had one caller (ahead.rs:210). Recording now happens in semantic_tokens_range for every range answer. guessed is set from index_unsettled() after the flush, which is a superset of trunk's ahead-only marking and is then compared exactly, so no behaviour was dropped. The changed pre-existing test still asserts a real difference ⇒ 1 and a non-refresh client ⇒ 0. The real-rescan versions are a_range_coloured_before_a_real_rescan_that_changed_nothing_draws_no_refresh (0) and …before_the_package_was_indexed_is_refreshed (1).

§67 ruling. I agree with the dev. TODO §67 itself records the no-begin and begin-on-its-own-line cases as a "Known limit". Fixing either would change fold start lines or add folds for every client, which is a behaviour change that needs the owner. It is not a bug.

Hygiene.

  • No unwrap/expect/panic!/unsafe in changed production lines.
  • Production lines, current (trunk): dispatch.rs 515 (522), tokens.rs 299 (284), notification.rs 288 (287), ahead.rs 242 (245), document.rs 146 (133). All are under 600.
  • The pub(super) widening is confined to the viewport/tests.rs helpers and Painted fields used by tests.
  • The colour_perf #[ignore] carries a reason string.
  • stamp_author.py --check: 0 to stamp.
  • The README has all seven sections.

Gates. All were guarded, and every OpenTitan run was a real run, not a skip.

  • cargo fmt --check: clean.

  • cargo clippy --workspace --all-targets: clean.

  • cargo test -p lucisv_server: 2112 passed / 48 ignored; e2e 106 passed.

  • cargo test -p lucisv_mcp -p lucisv: green (562 passed / 3 ignored).

  • opentitan_sweep: 4047 files, ranges 1066, present 34450, missing 0, unexpected 0.

  • viewport_sweep: 1265 files, 7590 windows, 539603/557977 agree.

  • opentitan_references --ignored: 2 passed (11.9 s).

  • colour_perf (release, 201 buffers):

    • comparison 1.77 ms;
    • update_one 115 µs for no change, 116 µs for a comment, 99.4 ms for a localparam, 100.9 ms for a rename;
    • 188 buffers marked, re-classified in 25.6 ms.

    These match the dev's numbers.

  • Not run: tools/perf/bench.py. It has no viewport or refresh-count phase. I did not build a trunk binary, so trunk sweep numbers are the dev's. The OpenTitan checkout was left clean.

Review

2026-11-26 — lead: accepted on the first QA pass.

  • Delivered: §2.9 #59 and #60 (closed in docs/topo.md), both inside lucisv_server. QA read every input of the range and whole answers, confirmed all five index mutation sites reach refresh_overlay, and ran 23 scenario tests asserting a refresh exactly when the answer differs: no missed refresh, no storm.
  • Accepted limits: a range-only client past 32 distinct ranges on one buffer gets the old refresh-per-rescan behaviour until a whole answer or close; an edit to the buffer between a mid-scan range answer and the settle can cost one unneeded refresh.
  • TODO §67 / §72: nothing built. The no-begin fold limit and 2-D instance arrays are features that need the owner's decision; the remaining §67 box is the owner's own VS Code check.
  • Process: the dev ran one single-test filter without tools/guard.sh (under a second, recorded in the work log).
  • OpenTitan folding and viewport sweeps are identical to trunk.