Files
lucisv/crates/lucisv_server/src/state/mod.rs
T
Guoliang ChenandClaude Mythos 5.2 58d3db3e3c
ci / checks (push) Successful in 59s
ci / corpus (push) Successful in 16s
release / build-and-publish (push) Failing after 4m55s
Release 0.65.6: saving a file no longer re-indexes the workspace
TODO §71.1. On OpenTitan hw (4 058 files) every save cost ~160 ms of
whole-index work, twice (the save and VS Code's watcher echo), and watcher
events for files the index could not hold started whole rescans.

- vlog_semantic: resolution records every table lookup per reading file,
  misses included (workspace/dependents.rs, references.rs, replace.rs); an
  update re-resolves the edited file and the readers of keys whose values
  changed in what resolution reads. An unchanged record merges nothing. When
  the readers hold more than an eighth of all references, old results are
  removed in one pass over the postings.
- lucisv_server: watcher events are classified per file (state/watched.rs);
  the include graph keeps every include line as written and re-resolves the
  lines a named path could satisfy, in one pass per event; an event naming
  more paths than half the index takes the single rescan; an open buffer the
  index does not hold joins on its first save; completion tables are rebuilt
  only for the names a file declares. Every save still republishes every open
  buffer. The line-length lint follows the formatting style's width.
- Fixed on the way: removing an included header left other files' include
  edges pointing at it.

Typical saves: comment or watcher echo <0.3 ms, local signal 4 ms, package
member 12 ms; warm startup +40..120 ms for the readers table.

QA failed the first cut on four stale answers found with an independent
fresh-server oracle; all four are fixed and are differential tests. Dev
(three rounds), QA (two rounds) and lead review:
docs/features/localized-reindex/README.md. TODO.md files lane 71.7 from
what the QA rounds and an investigation found in earlier releases too.

Version bump (Cargo.toml, Cargo.lock, editors/vscode/package.json and both
package-lock.json spots); notes in CHANGELOG.md (## 0.65.6) and
editors/vscode/CHANGELOG.md.

Gates: tools/gates.sh PASS on this tree; vlog_semantic 435/0,
lucisv_server 2011/0, lucisv_mcp 384/0.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-19 08:52:01 -08:00

184 lines
7.7 KiB
Rust

//! What the server remembers: one analysis per open buffer, one index for the
//! workspace. The invalidation rules are in
//! `docs/features/lucisv-server/README.md`; the short version is that a buffer's
//! analysis is rebuilt on every change and the index only on save.
//!
//! This file holds [`Workspace`] itself and the readers of its tables. Each
//! part of its behaviour is a sibling module with one `impl Workspace`:
//! `document` (one buffer's analysis), `buffers` (the open buffers, and
//! which text speaks for a path), `views` (the closed-file view cache),
//! `scope` (roots, settings and the scope ladder), `discover` (the walk
//! that finds the files), `scan` (what a scan carries), `rescan` (driving a
//! scan to the landed tables), `incremental` (one file re-indexed in place)
//! and `sources` (the file sets handed to refactorings).
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use lsp_types::Uri;
use vlog_semantic::{FileFacts, WorkspaceIndex};
use crate::candidates::Candidates;
use crate::includes::IncludeGraph;
use crate::index_cache::IndexCache;
use crate::scan_trace::ScanTrace;
use crate::settings::ClientSettings;
mod buffers;
pub(crate) mod discover;
mod document;
mod incremental;
mod port_grace;
mod rescan;
mod scan;
mod scope;
mod sources;
mod views;
mod watched;
#[cfg(test)]
pub use document::mixed_buffer;
pub use document::{first_token_from, first_token_reaching, Document};
pub use incremental::OneFile;
pub use port_grace::PortGrace;
pub use scan::{IndexEvent, Scan};
pub use views::FileView;
pub use watched::Watched;
/// How many `workspace/symbol` hits are returned. Each one costs a file read
/// to turn its byte span into a line, so an unbounded answer to the empty
/// query would read the whole workspace.
pub const WORKSPACE_SYMBOL_LIMIT: usize = 256;
/// Whether the persistent store this session reads and writes through has
/// been opened yet, and where it will be opened from when something first
/// needs it.
///
/// **The re-open rule is unchanged**: every whole rescan still opens the store
/// afresh, because the directory it belongs in depends on settings that arrive
/// after startup and because a second session over the same workspace may have
/// written records this one has not seen
/// (`docs/features/index-persistence/README.md`). What moved is *when*: from
/// the top of [`Workspace::begin_scan`] to the first file that asks the store
/// a question. A scan whose work list is empty asks none — and a session's
/// first scan runs at the default settings, before any buffer is open, so its
/// list is empty and it used to read and rewrite 44 MiB it never looked at
/// (`docs/features/perf5-p4/README.md`, §P4b (b1)).
#[derive(Default)]
enum StoreOpen {
/// Opened, or deliberately absent: [`Workspace::cache`] is what a lookup
/// gets.
#[default]
Done,
/// A whole rescan asked for a re-open and nothing has needed the store
/// since. The directory is the one that scan resolved, so a settings
/// change between then and the first file cannot move it underneath.
Deferred(Option<PathBuf>),
}
/// Every open buffer plus the workspace index they are resolved against.
#[derive(Default)]
pub struct Workspace {
docs: HashMap<Uri, Document>,
roots: Vec<PathBuf>,
index: WorkspaceIndex,
/// The project-wide completion tables, rebuilt with `index` and from the
/// same file list. Precomputed because completion fires per keystroke and
/// nothing that walks the workspace may.
candidates: Candidates,
/// What every in-scope file `` `include``s, and the `+incdir+`s an
/// include is looked for in. Rebuilt with `index`, from the same list.
includes: IncludeGraph,
/// The last `lucisv.*` settings a client pushed. Default until it does.
client: ClientSettings,
/// What previous sessions' scans parsed, keyed by content: a file whose
/// bytes are in here is indexed without being parsed again. Re-opened for
/// every whole rescan, because that is when the directory it should live
/// in can have moved (`docs/features/index-persistence`) — at the first
/// file of that rescan rather than before it, which is [`StoreOpen`].
cache: IndexCache,
/// Whether that re-open has happened yet — see [`StoreOpen`].
store: StoreOpen,
/// `initializationOptions.storagePath`: the per-workspace folder the
/// editor offered for exactly this. One rung of the cache-directory
/// ladder, and the only one the server cannot work out for itself.
storage: Option<PathBuf>,
/// Every directory a buffer has been opened from this session, unraised
/// and deduped. The folder scope is derived from these rather than from
/// the currently-open buffers, so **closing a file never narrows the
/// index** — see `docs/features/index-scope/README.md`.
opened_dirs: Vec<PathBuf>,
/// The last few closed files any answer needed the text of, newest first.
/// A cache and nothing else: every entry can be dropped at any moment
/// without changing an answer, and every way a file's text can move drops
/// its entry (see [`Workspace::file_view`]).
views: RefCell<Vec<Rc<FileView>>>,
/// The trace of the scan that finished most recently, waiting for the
/// server to read it out and log it. Held here rather than returned from
/// [`Workspace::finish_scan`] so that no caller's signature moves for a
/// diagnostic; empty and zeroed unless `--verbose` asked for one.
last_trace: ScanTrace,
/// How many scans this workspace has run, so a session's lines can be
/// told apart — a `workspace` scope pays two (`docs/features/perf5-p4`).
scans: u32,
/// Where each buffer's last edit ended and the connection-list entry it
/// is in, while that entry's diagnostics are held back
/// (`docs/features/port-entry-grace/README.md`).
grace: PortGrace,
}
impl Workspace {
pub fn index(&self) -> &WorkspaceIndex {
&self.index
}
/// The index record this workspace already holds for **exactly these
/// bytes**, or nothing.
///
/// What `vlog_refactor::KnownFacts` asks a refactoring's caller. Exactly
/// one thing in this server can promise it: the persistent index cache
/// files each record under the FNV-1a of the bytes it was read off, so
/// record and key are one store entry written by one read
/// ([`crate::index_cache::IndexCache::facts_now`]). Not a modified time,
/// not a size, and **not the workspace index's own record**, which carries
/// no hash of the text it came from. An unsaved buffer misses for the same
/// reason a changed file does, so precedence needs no rule here
/// (`docs/features/perf5r-scope-reuse/README.md`).
pub fn known_facts(&self, path: &Path, text: &str) -> Option<FileFacts> {
self.cache.facts_now(path, text)
}
/// The project-wide completion tables, as of the last index rebuild.
pub fn candidates(&self) -> &Candidates {
&self.candidates
}
/// The `` `include`` graph, as of the last index rebuild.
pub fn includes(&self) -> &IncludeGraph {
&self.includes
}
/// The edits whose connection-list entries are being held back.
pub fn port_grace(&self) -> &PortGrace {
&self.grace
}
/// The same, to note an edit, forget a buffer or release what is due.
pub fn port_grace_mut(&mut self) -> &mut PortGrace {
&mut self.grace
}
/// Look `uri`'s last edit up in the analysis just built for it, finding
/// the connection-list entry it is in, if any.
pub fn locate_port_entry(&mut self, uri: &Uri) {
if let Some(doc) = self.docs.get(uri) {
self.grace.locate(uri, doc);
}
}
}
#[cfg(test)]
mod tests;