Skip to content

feat: two-phase stateful linters - #14357

Merged
wkrozowski merged 13 commits into
leanprover:masterfrom
wkrozowski:wojciech/statefulLintersAttempt2
Jul 20, 2026
Merged

wkrozowski merged 13 commits into
leanprover:masterfrom
wkrozowski:wojciech/statefulLintersAttempt2

Conversation

@wkrozowski

@wkrozowski wkrozowski commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

This PR introduces stateful linters, which allow linters to persist and share state across command elaboration.

Each stateful linter operates in a two-phase architecture:

  • Early Phase: Reads the linter's previous state and produces an intermediate value passed to the late phase. This intermediate value is wrapped in an Option type; if the early phase fails, none is passed to the late phase.
  • Late Phase: Reads both the linter's previous state and the early phase's intermediate state to produce and save a new state. If this phase fails, the state remains unchanged.

Stateful linters must be initialized with a starting value (before the first top-level command is elaborated). Linters can only be initialized within initialize blocks.

State Access & Sharing

  • Global State: All stateful linters can read the global state of other linters (representing the state after the previous command, or the initial state if it's the first command).
  • Inter-phase Reading: The late phase of a linter can read the early-phase intermediate states of other linters.
  • Handles: Access to state is managed via a typed handle returned by the registerStatefulLinter method.

@wkrozowski wkrozowski added the changelog-language Language features and metaprograms label Jul 10, 2026
@github-actions github-actions Bot added toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN labels Jul 10, 2026
@leanprover-bot leanprover-bot added the builds-manual CI has verified that the Lean Language Reference builds against this PR label Jul 10, 2026
@leanprover-bot

leanprover-bot commented Jul 10, 2026 •

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ✅ Reference manual branch lean-pr-testing-14357 has successfully built against this PR. (2026-07-10 17:06:49) View Log
  • 🟡 Reference manual branch lean-pr-testing-14357 build against this PR didn't complete normally. (2026-07-10 17:07:53) View Log
  • ✅ Reference manual branch lean-pr-testing-14357 has successfully built against this PR. (2026-07-13 17:04:55) View Log
  • 🟡 Reference manual branch lean-pr-testing-14357 build against this PR didn't complete normally. (2026-07-13 17:05:04) View Log
  • ✅ Reference manual branch lean-pr-testing-14357 has successfully built against this PR. (2026-07-13 18:41:18) View Log
  • 🟡 Reference manual branch lean-pr-testing-14357 build against this PR didn't complete normally. (2026-07-13 18:42:25) View Log
  • ✅ Reference manual branch lean-pr-testing-14357 has successfully built against this PR. (2026-07-13 20:14:00) View Log
  • 🟡 Reference manual branch lean-pr-testing-14357 build against this PR didn't complete normally. (2026-07-13 20:15:35) View Log
  • ✅ Reference manual branch lean-pr-testing-14357 has successfully built against this PR. (2026-07-13 21:02:51) View Log
  • 🟡 Reference manual branch lean-pr-testing-14357 build against this PR didn't complete normally. (2026-07-13 21:03:28) View Log
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 46cf2b9ead5df46fbb91e45cf58e5764717767d5 --onto 7847ce51363794755252279e6732240bd4f27ca8. You can force reference manual CI using the force-manual-ci label. (2026-07-14 15:30:34)
  • ✅ Reference manual branch lean-pr-testing-14357 has successfully built against this PR. (2026-07-16 14:42:56) View Log
  • 🟡 Reference manual branch lean-pr-testing-14357 build against this PR didn't complete normally. (2026-07-16 14:44:55) View Log

@mathlib-lean-pr-testing

mathlib-lean-pr-testing Bot commented Jul 10, 2026 •

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ✅ Mathlib branch lean-pr-testing-14357 has successfully built against this PR. (2026-07-10 17:59:30) View Log
  • 💥 Mathlib branch lean-pr-testing-14357 build failed against this PR. (2026-07-13 17:53:30) View Log
  • 💥 Mathlib branch lean-pr-testing-14357 build failed against this PR. (2026-07-13 19:29:57) View Log
  • 💥 Mathlib branch lean-pr-testing-14357 build failed against this PR. (2026-07-13 20:59:13) View Log
  • 💥 Mathlib branch lean-pr-testing-14357 build failed against this PR. (2026-07-13 21:49:42) View Log
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 46cf2b9ead5df46fbb91e45cf58e5764717767d5 --onto 12c859a4d735fbcdb3ebbddb42dcc8619bcc7a7d. You can force Mathlib CI using the force-mathlib-ci label. (2026-07-14 15:30:32)
  • ✅ Mathlib branch lean-pr-testing-14357 has successfully built against this PR. (2026-07-16 15:37:50) View Log

@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Jul 10, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Jul 13, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Jul 13, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Jul 13, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan and removed builds-mathlib CI has verified that Mathlib builds against this PR labels Jul 13, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Jul 13, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Jul 13, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Jul 13, 2026
@wkrozowski
wkrozowski force-pushed the wojciech/statefulLintersAttempt2 branch from f4d3a66 to 8f7f747 Compare July 13, 2026 19:37
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Jul 13, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Jul 13, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Jul 13, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Jul 13, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Jul 13, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Jul 13, 2026
@wkrozowski
wkrozowski force-pushed the wojciech/statefulLintersAttempt2 branch from 53aa966 to 237c0f8 Compare July 14, 2026 14:40
@wkrozowski wkrozowski removed breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN builds-manual CI has verified that the Lean Language Reference builds against this PR mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN labels Jul 14, 2026
@wkrozowski
wkrozowski marked this pull request as ready for review July 14, 2026 14:41
@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Jul 14, 2026
@wkrozowski
wkrozowski force-pushed the wojciech/statefulLintersAttempt2 branch from 237c0f8 to e936a68 Compare July 16, 2026 14:07
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Jul 16, 2026
@github-actions github-actions Bot added the mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN label Jul 16, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Jul 16, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Jul 16, 2026
@leanprover-bot leanprover-bot added the builds-manual CI has verified that the Lean Language Reference builds against this PR label Jul 16, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Jul 16, 2026
@wkrozowski

Copy link
Copy Markdown
Contributor Author

!bench

@leanprover-radar

leanprover-radar commented Jul 20, 2026 •

Copy link
Copy Markdown

Benchmark results for e936a68 against a4b639c are in. No significant results found. @wkrozowski

  • 🟥 build//instructions: +4.9G (+0.04%)

Small changes (1🟥)

  • 🟥 build/module/Lean.Elab.Command//instructions: +5.0G (+32.55%) (reduced significance based on *//lines)

@wkrozowski
wkrozowski added this pull request to the merge queue Jul 20, 2026
Merged via the queue into leanprover:master with commit 9a61e83 Jul 20, 2026
29 checks passed
JJYYY-JJY pushed a commit to JJYYY-JJY/lean4 that referenced this pull request Jul 27, 2026
This PR adds the support for code actions to linters. When `Elab.async`
is enabled, before a linter task is dispatched, we create a promise for
the info tree node. Then, we accumulate newly added info trees through
the linter execution and we resolve the promise inside of the linter
task. Finally, on the main task, we modify the info tree (wrapped in
command context) and add a new leaf, with an mvar id, that will
eventually be filled with a promise value.

This PR also supports code actions in stateful linters (leanprover#14357), but
does not offer such support for module linters, that fire at terminal
commands and code actions from there do not surface to the info view.

Related to RFC leanprover#4363.
marcelolynch added a commit to marcelolynch/mathlib4 that referenced this pull request Jul 28, 2026
Rewrite the header linter with the stateful linter API
(leanprover/lean4#14357). The linter state caches the library-root
check, which replaces the Std.Mutex that guarded the cache against
concurrent async linter runs, and records that the checks ran, so the
linter checks the header of each module exactly once. A file without a
module doc-string gets one set of warnings at its first command; the
IO.Ref-era behavior repeated the full header parse and its warnings on
every command of such a file.

Linter behavior on well-formed files is unchanged; the header linter
tests pass as is (validated on a nightly-2026-07-27 toolchain).

NOTE: CI stays red until the toolchain bump to v4.34.0-rc1, which first
provides `registerStatefulLinter`.

Co-Authored-By: Claude Fable 5 <[email protected]>
wkrozowski pushed a commit to marcelolynch/lean4 that referenced this pull request Jul 29, 2026
This PR adds the support for code actions to linters. When `Elab.async`
is enabled, before a linter task is dispatched, we create a promise for
the info tree node. Then, we accumulate newly added info trees through
the linter execution and we resolve the promise inside of the linter
task. Finally, on the main task, we modify the info tree (wrapped in
command context) and add a new leaf, with an mvar id, that will
eventually be filled with a promise value.

This PR also supports code actions in stateful linters (leanprover#14357), but
does not offer such support for module linters, that fire at terminal
commands and code actions from there do not surface to the info view.

Related to RFC leanprover#4363.
wkrozowski pushed a commit to marcelolynch/lean4 that referenced this pull request Jul 29, 2026
The phases of a stateful linter (leanprover#14357) return values, so they cannot
use the CommandElab-only signature. All existing call sites instantiate
the result type with Unit and do not change.

Co-Authored-By: Claude Fable 5 <[email protected]>
wkrozowski pushed a commit to marcelolynch/lean4 that referenced this pull request Jul 29, 2026
The phases of a stateful linter (leanprover#14357) return values, so they cannot
use the CommandElab-only signature. All existing call sites instantiate
the result type with Unit and do not change.

Co-Authored-By: Claude Fable 5 <[email protected]>
wkrozowski pushed a commit to marcelolynch/lean4 that referenced this pull request Jul 29, 2026
The phases of a stateful linter (leanprover#14357) return values, so they cannot
use the CommandElab-only signature. All existing call sites instantiate
the result type with Unit and do not change.

Co-Authored-By: Claude Fable 5 <[email protected]>
JJYYY-JJY pushed a commit to JJYYY-JJY/lean4 that referenced this pull request Jul 29, 2026
…r#14581)

This PR generalizes `withSetOptionIn` over the result type of the
wrapped function. The previous signature only accepted a `CommandElab`,
which returns `Unit`. The phases of a stateful linter (leanprover#14357) return
values, so they could not use the helper (see for example
leanprover-community/mathlib4#42186). All existing call sites
instantiate the result type with `Unit` and do not change.

A new `resultType` section in `tests/elab/withSetOptionIn.lean` checks
the generalized signature.

The change preserves behavior. Type inference changes in one corner
case: a first-class use with no expected type, where the wrapped
function does not determine its result type, now needs a type
ascription. Core, Batteries, and Mathlib contain no such use.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Fable 5 <[email protected]>
robsimmons pushed a commit that referenced this pull request Jul 29, 2026
This PR introduces stateful linters, which allow linters to persist and
share state across command elaboration.

Each stateful linter operates in a two-phase architecture:

* **Early Phase**: Reads the linter's previous state and produces an
intermediate value passed to the late phase. This intermediate value is
wrapped in an `Option` type; if the early phase fails, `none` is passed
to the late phase.
* **Late Phase**: Reads both the linter's previous state and the early
phase's intermediate state to produce and save a new state. If this
phase fails, the state remains unchanged.

Stateful linters must be initialized with a starting value (before the
first top-level command is elaborated). Linters can only be initialized
within `initialize` blocks.

### State Access & Sharing
* **Global State**: All stateful linters can read the global state of
*other* linters (representing the state after the previous command, or
the initial state if it's the first command).
* **Inter-phase Reading**: The late phase of a linter can read the
early-phase intermediate states of other linters.
* **Handles**: Access to state is managed via a typed handle returned by
the `registerStatefulLinter` method.
robsimmons pushed a commit that referenced this pull request Jul 29, 2026
This PR adds the support for code actions to linters. When `Elab.async`
is enabled, before a linter task is dispatched, we create a promise for
the info tree node. Then, we accumulate newly added info trees through
the linter execution and we resolve the promise inside of the linter
task. Finally, on the main task, we modify the info tree (wrapped in
command context) and add a new leaf, with an mvar id, that will
eventually be filled with a promise value.

This PR also supports code actions in stateful linters (#14357), but
does not offer such support for module linters, that fire at terminal
commands and code actions from there do not surface to the info view.

Related to RFC #4363.
robsimmons pushed a commit that referenced this pull request Jul 29, 2026
This PR generalizes `withSetOptionIn` over the result type of the
wrapped function. The previous signature only accepted a `CommandElab`,
which returns `Unit`. The phases of a stateful linter (#14357) return
values, so they could not use the helper (see for example
leanprover-community/mathlib4#42186). All existing call sites
instantiate the result type with `Unit` and do not change.

A new `resultType` section in `tests/elab/withSetOptionIn.lean` checks
the generalized signature.

The change preserves behavior. Type inference changes in one corner
case: a first-class use with no expected type, where the wrapped
function does not determine its result type, now needs a type
ascription. Core, Batteries, and Mathlib contain no such use.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-manual CI has verified that the Lean Language Reference builds against this PR builds-mathlib CI has verified that Mathlib builds against this PR changelog-language Language features and metaprograms mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants