elsmith 0.1.0

An Emacs Lisp language server with Lean-verified protocol and editing rules
Documentation
# Development

Elsmith is an Emacs Lisp language server. The executable synchronizes text over stdio, parses it into a lossless syntax tree, resolves Lisp-2 symbol identities and indexes the workspace. It answers navigation, completion, hover, signature help, semantic token, inlay hint and workspace symbol requests. It reads an Emacs catalog extracted by an isolated `emacs -Q --batch`, reads the `lexical-binding` cookie and manages the user trust list.

- Use English for code, tests, commit messages and documentation.
- Keep pure protocol and editing rules in Lean and call the generated Rust functions from the server.
- Keep JSON, stdio framing, document storage, the syntax tree, the macro recognizer, the atomic response claim, the workspace index, the catalog and filesystem operations in Rust.
- Treat project files (`elsmith.toml`, `Keg`, `Eask`, `Cask`, `.dir-locals.el`) as data parsed in Rust. Never evaluate them, and never let them choose the Emacs binary.
- Run the catalog extractor only with an absolute Emacs path from `--emacs`, the user `config.toml` or `PATH`, in an empty temporary directory with a cleared environment.
- Keep the parser faithful to `read0` in Emacs 31.1 `src/lread.c`. `make corpus` must report zero error nodes and zero form-count mismatches against Emacs `read`.
- Stage `lean/` and `.leanfran/cache/` with any source change to a `lean!` block.
- Add a useful theorem and a leanassay test in the same change as a new Rust-called generated function.
- Preserve the user allowlist as the sole authority for workspace trust.

## Diagnostics

- Keep static diagnostics and code actions in Rust. Both diagnostic delivery paths check the same Lean generation rule. Open epochs identify each open of a URI, even when the editor reuses its document version.
- Use push diagnostics for clients without pull support. Cleared push findings require an empty array. Send pull refresh requests only when the client declares refresh support.
- Start the Emacs diagnostic and macro expansion jobs only for allowlisted workspace roots and resolved dependency targets. Defer jobs while imported buffers differ from disk. Run one job at a time with a ten-second deadline.
- Use structured Emacs callbacks and a fresh temporary source snapshot. Keep compiled files out of the workspace, disable Checkdoc autofixes and distinguish package-lint availability from its findings.
- Offer only exact, validated quick fixes. Adding or moving a `lexical-binding` cookie changes how the file runs and belongs to an explicit refactor.

## Lean proofs

`src/position.rs` proves that position and offset round-trip at character boundaries, past-end positions clamp to the final boundary, and positions inside a character are identified exactly between the corresponding encoded boundaries. The tail-recursive traversals agree with the structural specification. The replacement theorems prove preservation of the prefix and suffix, insertion at the edit start, the resulting scalar length, identity on self-replacement and whole-buffer replacement. Rust tests compare sequential LSP edits with the Lean scalar-range specification.

`src/lifecycle.rs` proves the necessary and sufficient conditions for entering ready, running, stopped and exited states and for rejecting transitions. Ordinary requests require a running session. The reply theorems state when completion or cancellation can claim a response and show that two serialized claims yield exactly one response. Rust serializes the claims with an atomic swap and tests it with competing threads. M0's stdio requests are handled synchronously.

`src/capabilities.rs` proves that negotiation picks the first supported client preference, skips unknown encodings and defaults to UTF-16. The edit permission requires the declared property. The file-watcher and server progress declarations affect their associated permissions independently of the encoding and edit permission.

`src/elisp_header.rs` models `lisp_file_lexical_cookie` of Emacs 31.1 character by character. The proofs show that the tail-recursive scanner agrees with the structural one, that text after the scan extent never changes the result, and that a first line without a comment yields no cookie. A shebang line is skipped, and a trailing `Local Variables:` block never yields a cookie. `emacs_differential` checks a copy of every fixture in `tests/fixtures/lexical/` in the kernel. The expected cookies come from Emacs 31.1 through `make lexical-oracle`, and the expected extents pin the scanner's current output. A Rust test compares the same fixtures with `expected.json`. Rust passes a prefix that ends at a line break and uses the extent theorem to fall back to the whole text when the scan reaches the end of the prefix.

`src/scope_rule.rs` proves that value and function bindings never shadow each other, that the innermost binding of the same namespace wins, and that function bindings are lexical. A value binding is dynamic in a file without lexical binding and under a global or enclosing `defvar` declaration. A declaration made after the binding does not change it. `can_local_rename_iff` states that a rename is accepted exactly when no site resolves to a dynamic binding and every site keeps its resolution. The analysis in `src/analysis.rs` resolves every value and function occurrence through `resolve_symbol`, and `can_rename_local` collects the sites for `can_local_rename`.

`src/signature.rs` proves how a call argument maps to a parameter. An argument before the end of the positional parameters selects its own slot, `&rest` absorbs every later argument, and arguments beyond a fixed arity select nothing. After the positional slots, `&key` arguments alternate between keyword and value, and the keyword before a value selects the parameter with that name. `parse_usage` reads the argument list from an Emacs usage string or a source arglist, and `active_parameter` calls the generated `activeParameter`.

`src/semantic.rs` classifies each symbol with the role names of Emacs 31.1 `elisp-scope`. `tests/fixtures/scope/expected.json` holds the roles, positions and binder positions that `make scope-oracle` collects from Emacs. The test requires the same role and binder at every position Emacs reports. `extras.txt` pins the positions that only Elsmith reports, such as the body of `dolist` and the metadata of `defcustom`.

`src/diagnostic_generation.rs` proves that diagnostics are accepted exactly when the document is open and all five generation fields match. It rejects close, reopen, edits, dependency changes, configuration changes and trust revocation. Rust tests exercise generation invalidation and Emacs process cleanup through stdio.

The position and replacement proofs adapt the structural models from [lsp-touchstone](https://github.com/conao3/rust-lsp-touchstone), distributed under Apache-2.0.

## Mutation audit

`audit/Audit/` runs `#assay` for every generated function called by Rust and for the helpers of the cookie scanner, the rename check and the active parameter. The audit uses Lean 4.35.0 and the locked leanassay revision. `lake -d audit build --wfail` treats audit warnings as failures. Equivalent mutants are based on the inputs tested.

| Function | Mutants | Killed | Holes | Equivalent | Untyped |
| --- | ---: | ---: | ---: | ---: | ---: |
| `resolve` | 2 | 2 | 0 | 0 | 0 |
| `offset` | 4 | 4 | 0 | 0 | 0 |
| `replaceRange` | 3 | 3 | 0 | 0 | 0 |
| `transition` | 75 | 63 | 0 | 0 | 12 |
| `requestAllowed` | 3 | 3 | 0 | 0 | 0 |
| `completeOrCancel` | 6 | 6 | 0 | 0 | 0 |
| `negotiateSession` | 1 | 1 | 0 | 0 | 0 |
| `parseLexicalCookie` | 1 | 1 | 0 | 0 | 0 |
| `scanLoop` | 13 | 10 | 0 | 0 | 3 |
| `feed` | 90 | 61 | 0 | 1 | 28 |
| `readName` | 9 | 9 | 0 | 0 | 0 |
| `readValue` | 14 | 13 | 0 | 0 | 1 |
| `verdict` | 9 | 9 | 0 | 0 | 0 |
| `resolveSymbol` | 16 | 13 | 0 | 0 | 3 |
| `canLocalRename` | 5 | 5 | 0 | 0 | 0 |
| `declaredSpecial` | 11 | 8 | 0 | 0 | 3 |
| `renameSite` | 3 | 3 | 0 | 0 | 0 |
| `keepsResolution` | 7 | 7 | 0 | 0 | 0 |
| `activeParameter` | 3 | 3 | 0 | 0 | 0 |
| `activeFrom` | 23 | 12 | 0 | 0 | 11 |
| `keyIndex` | 16 | 13 | 0 | 0 | 3 |
| `governingKey` | 9 | 4 | 0 | 0 | 5 |
| `acceptDiagnostics` | 14 | 13 | 0 | 0 | 1 |

Every function has zero untested and zero timed-out mutants. Untyped mutants are reported by leanassay as stillborn.

## Verification

- `nix fmt -- --ci` checks formatting.
- `nix develop --command make check` runs format, lint, Lean proofs, generated-file checks and Rust tests. The tests include the Emacs 31.1 `lisp/` corpus and a catalog built from the devShell Emacs, so run them in the devShell. Tests with a fake Emacs installation cover an `.el.gz` library and installed and missing C sources, and another test runs with no Emacs at all.
- `nix develop --command make corpus` prints the corpus statistics: lossless files, error nodes, form-count mismatches against Emacs `read`, incremental edits and analysis counts.
- `nix develop --command make lexical-oracle` regenerates the expected cookies of `tests/fixtures/lexical/` with Emacs.
- `nix develop --command make scope-oracle` regenerates the `elisp-scope` roles of `tests/fixtures/scope/` with Emacs.
- `nix develop --command make bench-startup` measures the startup with and without the catalog cache and writes `test-out/startup.json`.
- `nix develop --command make audit` checks proof coverage against mutations.
- `nix develop --command make check-protocol` scores the strict-client session and two proxy sessions, one with no client capabilities and one with UTF-8, Markdown and resolve support.
- `nix build` builds the packaged CLI. `./result/bin/elsmith --version` checks the result.
- `gh actions-lock --verify` checks workflow action pins.