Skip to content

state_mutation has no stated contract: three incompatible definitions across 46 languages, and it governs the corpus's two worst metrics #2765

Closed

Description

@squid-protocol

Same pass as #2763/#2764 (2026-09-05, engine 3b3568e0, corpus main 6183c4b). This is the #2730 treatment applied to the next rule that needs it, chosen because the two metrics it governs are the corpus's worst two by consistency score:

metric consistency (share of languages in the ±25% band)
risk_state_flux 42% — worst of 49
state_mutation 60% — second worst

Eight languages sit red on state_mutation against a planted intent of 2 (assembly 6, c 5, go 5, dockerfile 4, haskell 4, m4 4, matlab 4, perl 4), and twenty sit red on risk_state_flux. Every one of those cells is ledgered, but the ledger entries describe what each rule matched, never whether the rules agree on what a mutation is. They do not.

Three incompatible definitions, all live

Running each language's own rule over the corpus's identical planted intent:

(a) Named mutation verbs — only re-assignment counts.

m4       pushdef, m4_append, popdef, popdef
scheme   set!, vector-set!, ...
tcl      set|lappend|dict set|array set|incr|append
haskell  IORef, IORef, modifyIORef, writeIORef

(b) Mutable-binding declarations — declaring a variable counts, mutating it does not.

rust     \bmut\b|\.borrow_mut\(\)|Cell::|RefCell::|Atomic[A-Za-z0-9]+
kotlin   \b(var|MutableList|MutableMap|...)\b
swift    \b(var|inout|mutating|didSet|willSet)\b
zig      \bvar\b|\.\*[ \t]*=[^=]
scala    \b(var|scala\.collection\.mutable|Atomic...)\b

(c) The assignment operator — every = counts, including initialization.

c        (?<![=!<>])=(?![=])|\*(?!\s*const)\w+[ \t]*=|(?:\+\+|--)
go       :=|(?<![=!<>])=(?![=])|<-|\bappend\(|\batomic\.(?:Add|Store|Swap)
lua      \b[a-zA-Z_]\w*(?:\[[^\]]+\]|\.[a-zA-Z_]\w*)?\s*(?<![=<>~])=(?![=])
groovy   ^[ \t]*\w+(?:\.\w+)*[ \t]*=(?!=)
fortran  (?!\b(?:KIND|LEN|UNIT|FMT|FILE|STATUS|ACTION)\s*=)\b[A-Za-z_][...]{0,199}[ \t]*=[^=>]

These are not three renderings of one idea. A let x = 5 is invisible to (a), invisible to (b) if the binding is immutable, and a mutation to (c). A var x: Int with no write is a mutation to (b) and invisible to (a) and (c). The corpus plants exactly two mutations per language and reads 2 to 6 depending only on which family the language landed in.

Two outright false positives found while classifying

haskell counts a type signature. a.hs records state_mutation 2, and both hits are the word IORef in a declaration:

counter :: IORef Int          -- hit
region  :: IORef Int          -- hit

The rule's alternation \b(IORef|STRef|TVar|MVar|TMVar|...)\b matches the type name anywhere. A type annotation naming a mutable cell is not a mutation — modifyIORef/writeIORef in the same alternation are, and b.hs reads those correctly. haskell's state_mutation is therefore 4 against a plant of 2, and risk_state_flux 15.5 against a median of 7.751 (+100%), from two type signatures.

dockerfile double-counts its own globals plants. a.dockerfile records state_mutation 2, both from ENV:

ENV REGION=us-east
ENV HOME_ZONE=1

ENV is dockerfile's globals rule and the first arm of its state_mutation rule (^[ \t]*ENV[ \t]+[a-zA-Z0-9_]+). Declaring a build-time environment variable is a global declaration; it is a mutation only under family (c)'s reading, which dockerfile otherwise does not use (its second arm is export X=). Every Dockerfile in every scan pays state_mutation once per ENV line.

What a contract would have to say

docs/api_rule_contract.md (#2730/#2743) is the model: one sentence, then corollaries, then a 46-language audit table. The sentence for this rule is a real design choice, not a lookup — the two candidates are

  • "a write to state that outlives the expression" — family (a) and (c)-minus-initialization. Declarations never count; mut/var count only at the write site. This is what risk_state_flux reads as if it were true.
  • "the introduction or use of mutable state" — family (a), (b) and (c). Declarations count. This is what rust/kotlin/swift/scala/zig implement, and it makes the signal a style measure rather than a behaviour measure.

Either is defensible; today's mixture is not, and the mixture is what the ×3 cascading-flux amplifier (spatial_correlation.py:278) then multiplies. Whichever is chosen, the corollaries the audit needs:

  1. A type annotation naming a mutable container is not a mutation (haskell, and check scala's Atomic*/mutable arms and kotlin's Mutable* for the same shape).
  2. A token already owned by another rule for the same construct is not a second signal (dockerfile ENV ↔ globals; check c/go/lua/groovy/fortran = against their own globals plants for the initialization case).
  3. Initialization is a declaration, not a mutation, under contract 1 — which is the c/go/lua/groovy/fortran audit and the largest bucket.

Method, and why it is cheap

tools/screen_plant.py and _registry.py in keyword-rosetta run any candidate through the live compiled rules; language-crucible/data/<lang> gives the real-world incidence per language before anything is changed, the way #2743's audit table was built (c 8675 → 1141 was found exactly this way). Expect the same shape of result: a few languages widen, most narrow, and the golden-master diff is attributable per language.

Corpus pairing

state_mutation is planted (2 per language, SPEC.md's probe table), so every language's manifest re-blesses off one scan. Ledger entries that narrow or retire: state-flux-branch-weighting, batch4-dual-keyword-overlaps (dockerfile/matlab/m4 halves), probe-idiom-collides-with-measured-signal, and the state_mutation arm of string-literal-selective-shielding.

Part of #2669. Sibling of #2730 (api) — same shape, next rule.

Metadata

Metadata

Assignees

No one assigned

    Labels

    core-engineenhancementmetrics

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions