# Limits gate (STRA-367) Lawbook chapter 301 limits the size of code: a function has at most 30 lines, cyclomatic complexity at most 10 and nesting at most 4, a function has at most 5 parameters (`self` counted) and a line is at most 120 columns wide. The workspace does not meet them today (see the census below), so the gate is erroring for **new and changed code** and a **falling ratchet for legacy**. It is offline tooling (stdlib Python, no cargo needed for the diff part, no network, no GPU); the production build does not depend on it. ``` scripts/check-limits.py diff gate + allowlist ratchet + census ratchet (what check-local.sh --product runs) scripts/check_limits_test.py the self-tests (64 tests) scripts/check-limits.py --report census per crate and the worst functions scripts/check-limits.py --base REF diff against REF instead of the default base (env LIMITS_BASE does the same) scripts/check-limits.py --update-baseline rewrite limits-baseline.json (never raises a total) and tighten the allowlist scripts/check-limits.py --allowlist-add --reason R --owner STRA-nnn allowlist what the diff gate rejects (moved verbatim legacy) scripts/check-clippy-limits.sh opt-in Clippy ratchet (check-local.sh --clippy-limits) ``` Files: `limits.json` (the policy), `limits-allowlist.json`, `limits-baseline.json`, `clippy-limits-baseline.json`, `clippy.toml`, `scripts/limits/` (the tool). The tool holds itself to the limits: `scripts/limits/`, `scripts/check-limits.py`, `scripts/check_limits_test.py` and the architecture gate scripts are in `python_include`, so they are measured with the same five measures (Python measures below) and the gate runs on its own commits. ## What is enforced | Part | Applies to | Result | | --- | --- | --- | | Diff gate | every function that is added or has a changed or deleted line inside its span, in a `.rs` file (and the Python of `python_include`); every added or changed line | an error for each limit missed, unless the function is allowlisted | | Allowlist ratchet | every entry of `limits-allowlist.json`, touched or not | an error when a recorded number grows; stale (reported, not failing) when the function is within the limits, gone, or smaller than recorded | | Census ratchet | the whole tree, per crate and scope (`prod`, `test`) | an error when the **total** over all crates of any count rises above `limits-baseline.json` | Out of scope (`limits.json`, `exclude`): `engine/strata/` (the imported Original, never edited), `target/`. Test code is in scope like production code (a function in `tests/`, `benches/`, `examples/`, `tests.rs`, `*_tests.rs` or under a `#[cfg(test)] mod` is a `test` function in the census, and the diff gate treats it the same). ### The diff The diff is `git diff --unified=0 -M BASE` against the **working tree** (so staged, unstaged and committed changes count) plus untracked files (all their lines are added). The default `BASE` is `git merge-base HEAD R` for the first ref `R` that exists in `claude/hexagonal-integ`, `main`, `origin/main`; `--base REF` or `LIMITS_BASE=REF` uses `REF` as given. A pure rename (`git mv`, no edit) has no changed lines and is not checked. Moving a function to another file is adding it there: allowlist it (below) or fix it. A function is **touched** when an added or changed line is within its span, or when lines were deleted between two of its own lines. Touching a legacy function that misses a limit is an error: fix it, or allowlist it with a reason. A line is checked for width only when it was added or changed. ### Measures (the exact rules) A function is found from the Rust tokens (the lexer of `scripts/dev/rustsplit`, no parser). Its **span** runs from the `fn` keyword to the closing brace of its body. * **fn_lines**: the number of lines of the span (signature plus body, comments and blank lines included). * **complexity**: 1 plus the decision points of the body: every `if` (an `else if` or `if let` counts once), `while`, `for` loop, `loop`, `&&`, `||`, `?`, and every `match` arm after the first of its `match`. `&&` and `||` count only when they follow an operand (so `&&x` and `|| body` do not). A nested `fn` is measured on its own and is left out of the function that contains it; closures belong to their function. * **nesting**: the deepest chain of blocks inside the body (the body is depth 0). The block of `if`, `else`, `while`, `for`, `loop`, `match`, `unsafe`, `async`, a closure `{ }` and a bare block each add one. A match arm block `=> { }`, a struct literal or pattern `Foo { .. }` and a macro `name! { .. }` add nothing. * **params**: the number of parameters in the signature, `self` (any form: `self`, `&self`, `&mut self`, `self: Box`) counted like the others. Only the top-level, non-empty comma-separated parts of the list count, so commas inside generics, tuples, arrays, patterns (`Foo { x, y }: Foo`) and `impl Fn(A, B)` types do not; a trailing comma is free; generics before the list are skipped. `extern "C" fn` with a body and test functions count like any other function (a declaration without a body, such as the ones in an `extern "C" { }` block, is not a function and is not counted). In Python it is the number of positional, keyword-only, `*args` and `**kwargs` parameters, `self` and `cls` included. * **width**: the columns of a line; a tab is 4, the line ending is not counted, 120 is allowed, 121 is not. For a function `width` is the number of its lines wider than 120. Python (`python_include` only) uses the same names: lines are `def` to the last line; complexity is 1 plus `if`, conditional expression, `for`, `while`, `except`, `match` case, every comprehension (and each of its `if`s) and every extra operand of `and`/`or`; nesting is the deepest chain of `if`, loop, `try`, `with`, `match` (an `elif` chain is flat). These counts include `?` and the boolean operators, so they are higher than the hand counts in `tech-debt.md` written before the gate existed (for example `render_request_with` is complexity 93 here, 58 there). A **key** names a function inside its file: the enclosing `impl`/`trait`/`mod`/`fn` names and its own name (`Type::method`, `Trait for Type::method`, `module::helper`, `outer::inner`); a repeated key gets `#2`, `#3` in order of appearance. The scanner is a heuristic (no macro expansion): a struct pattern directly after an uppercase constant in an `if let ... {` condition, `for` in an `impl .. for` inside a function body, `let .. else` and `match` arms written inside `macro_rules!` bodies are not modelled. It errs by a point or two, never by a function. ## The allowlist `limits-allowlist.json` lists functions that may miss a limit, with the numbers they have today: ```json {"path": "crates/x/src/a.rs", "name": "Type::method", "fn_lines": 120, "complexity": 31, "nesting": 5, "width": 2, "params": 7, "reason": "moved verbatim from backend.rs by S19", "owner": "STRA-367"} ``` * `reason` and `owner` (the work item that removes the debt) are mandatory; an entry without them is an error. * A recorded number may only fall. The function growing in any of the five measures is an error, touched or not; the entry also covers the wide lines inside the function. * The entry named `` covers the wide lines that are in no function (consts, attributes, imports); only `width`. * An entry whose function is within the limits (or gone) is **stale**: the gate says so and `scripts/check-limits.py --update-baseline` drops it. The same command lowers recorded numbers that fell. It never raises one. * A new entry is made with `scripts/check-limits.py --allowlist-add --reason "..." --owner STRA-nnn`, which writes an entry with today's measures for everything the diff gate rejects. Review the diff of `limits-allowlist.json` like code. On the default base (the tip of `claude/hexagonal-integ`) the allowlist is **empty**: nothing on this branch is rejected. Against a main-like base (`--base main`) the gate rejects 5382 lines and functions in 1533 files, because the whole hexagonal integration is new code relative to `main`; seeding the allowlist for that comparison is `scripts/check-limits.py --base main --allowlist-add --reason ... --owner STRA-338`: 2736 entries (2354 functions and 382 `` entries), after which the gate passes with `--base main`. Whoever merges the integration into `main` runs it once; it is not committed here, because the entries would only describe code that the integration's census already counts. ## The census (legacy) `limits-baseline.json` counts, per scope and crate, the functions over each limit (`fn_lines`, `complexity`, `nesting`, `params`) and the lines over the width. Only the **total over all crates** is ratcheted (code that moves between crates keeps its total, like the source ratchets of the architecture gate), and `--update-baseline` refuses to raise a total unless `--allow-increase` is given and justified in review. The tool reads the package list from `cargo metadata`; a file belongs to the deepest package directory that contains it; the Python of `python_include` is the crate `python`. Census at the commit that introduced the gate (production / test; functions are counted once per limit they miss, so a function can be in several columns; the last column counts lines, not functions): | Crate | Functions over 30 lines | Complexity over 10 | Nesting over 4 | Lines over 120 columns | | --- | --- | --- | --- | --- | | `leleka-backend-api` | 3 / 5 | 2 / 0 | 0 / 0 | 16 / 0 | | `leleka-context` | 6 / 12 | 6 / 1 | 0 / 0 | 10 / 1 | | `leleka-cpu-kernels` | 1 / 8 | 0 / 3 | 0 / 5 | 0 / 1 | | `leleka-cpu-sys` | 19 / 16 | 3 / 0 | 2 / 0 | 5 / 2 | | `leleka-cpu-worker` | 7 / 6 | 5 / 1 | 0 / 2 | 6 / 11 | | `leleka-cuda-driver` | 657 / 134 | 668 / 32 | 32 / 6 | 2561 / 188 | | `leleka-deploy` | 105 / 89 | 53 / 0 | 7 / 0 | 387 / 67 | | `leleka-experimental-ref` | 15 / 14 | 11 / 2 | 1 / 2 | 19 / 58 | | `leleka-gateway-domain` | 16 / 3 | 13 / 0 | 6 / 0 | 53 / 1 | | `leleka-gateway-text` | 15 / 13 | 16 / 0 | 1 / 2 | 64 / 21 | | `leleka-gguf` | 6 / 5 | 6 / 0 | 0 / 0 | 3 / 1 | | `leleka-gptoss` | 13 / 16 | 6 / 4 | 1 / 0 | 1 / 7 | | `leleka-gptoss-igpu` | 1 / 5 | 1 / 0 | 0 / 0 | 2 / 3 | | `leleka-gptoss-npu` | 5 / 4 | 3 / 0 | 2 / 0 | 3 / 6 | | `leleka-gpu-npu-worker` | 3 / 2 | 1 / 1 | 0 / 0 | 3 / 4 | | `leleka-harmony` | 4 / 3 | 6 / 0 | 2 / 0 | 1 / 5 | | `leleka-igpu` | 12 / 12 | 1 / 1 | 0 / 0 | 5 / 0 | | `leleka-igpu-worker` | 2 / 0 | 1 / 0 | 0 / 0 | 1 / 0 | | `leleka-kar-codec` | 4 / 0 | 3 / 0 | 0 / 0 | 0 / 0 | | `leleka-libc-shim` | 2 / 0 | 2 / 0 | 0 / 0 | 35 / 2 | | `leleka-memory` | 21 / 2 | 11 / 1 | 1 / 1 | 164 / 67 | | `leleka-model-format` | 84 / 83 | 62 / 14 | 4 / 4 | 100 / 70 | | `leleka-native-cpu-expert` | 6 / 26 | 84 / 22 | 24 / 6 | 1106 / 325 | | `leleka-native-parity-bench` | 0 / 0 | 0 / 0 | 0 / 0 | 9 / 0 | | `leleka-npu-service` | 6 / 3 | 5 / 0 | 0 / 1 | 14 / 1 | | `leleka-npu-worker` | 2 / 2 | 1 / 0 | 0 / 0 | 2 / 0 | | `leleka-obs` | 4 / 0 | 4 / 0 | 1 / 0 | 16 / 7 | | `leleka-offline-tools` | 8 / 3 | 5 / 0 | 0 / 0 | 1 / 0 | | `leleka-openvino` | 2 / 0 | 0 / 0 | 0 / 0 | 1 / 0 | | `leleka-quant-core` | 1 / 11 | 0 / 1 | 2 / 0 | 0 / 0 | | `leleka-runtime` | 633 / 131 | 616 / 27 | 30 / 6 | 1995 / 250 | | `leleka-runtime-client` | 9 / 9 | 4 / 0 | 0 / 0 | 4 / 0 | | `leleka-sampler-ref` | 9 / 14 | 4 / 1 | 0 / 2 | 0 / 0 | | `leleka-serving-policy` | 13 / 9 | 10 / 0 | 0 / 0 | 6 / 1 | | `leleka-shm` | 2 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | | `leleka-tokenizer` | 13 / 10 | 7 / 1 | 0 / 0 | 21 / 10 | | `leleka-worker-protocol` | 3 / 1 | 3 / 0 | 0 / 0 | 0 / 0 | | `python` | 0 / 0 | 1 / 0 | 0 / 0 | 2 / 1 | | `strata-control` | 187 / 214 | 135 / 17 | 17 / 30 | 199 / 133 | | `strata-desktop-core` | 81 / 65 | 59 / 3 | 6 / 3 | 54 / 30 | | **total** | **1980 / 930** | **1818 / 132** | **139 / 70** | **6869 / 1273** | The numbers come from `scripts/check-limits.py --report` and are the content of `limits-baseline.json`. The Clippy side, measured on Linux with the opt-in script, is in `clippy-limits-baseline.json` (3675 warnings in 36 crates: `too_many_lines` 2441, `cognitive_complexity` 730, `too_many_arguments` 504); Clippy skips blank and comment lines and counts arguments, so its numbers differ from the table. ## Clippy (opt-in) `scripts/check-clippy-limits.sh` runs `cargo clippy --workspace --all-targets --no-deps` with `-W clippy::too_many_lines -W clippy::cognitive_complexity -W clippy::too_many_arguments` on the command line (no `#![warn]` in any source), counts the distinct warning sites per crate, and fails when the total of a lint rises above `clippy-limits-baseline.json`. Run it with `scripts/check-clippy-limits.sh [--update-baseline [--allow-increase]]` or `scripts/check-local.sh --clippy-limits` (alone, or after `--product`). It takes about two minutes on a warm machine, but it is a full Clippy build of every target, so it is not part of the default gate. `CLIPPY_LIMITS_ARGS="-p crate"` restricts the packages. Three details keep the count honest. The extra `-W` flags are not part of cargo's fingerprint, so the script builds in its own `clippy-limits` directory under `CARGO_TARGET_DIR` (or `target/`) and every run uses the same flags; units cached by a run with other flags would replay old diagnostics and undercount. `--cap-lints warn` lets a crate that denies warnings compile with the extra lints on. And the counts are used only when `cargo clippy` succeeded: a failed build exits 1 and writes nothing, so a partial run can never become the baseline. The baseline was measured on Linux (WSL Ubuntu, Clippy 0.1.99); a Windows run differs by the `cfg(windows)` code. Clippy 0.1.99 keys: `too-many-lines-threshold`, `cognitive-complexity-threshold`, `too-many-arguments-threshold`. The root `clippy.toml` sets the first two to 30 and 10; they belong to allow-by-default lints, so they change nothing for the existing `-D warnings` gates. The third (5) lives in `scripts/limits/clippy/clippy.toml`, which the script passes as `CLIPPY_CONF_DIR`: `too_many_arguments` is **on by default**, and a root threshold of 5 makes `strata-control` and `strata-desktop-core` (functions with 6 and 7 arguments) fail the hard Clippy gates of `check-local.sh --product`. A self-test keeps the two files in step. Crates that carry `#![allow(clippy::too_many_arguments)]` are not counted. `rustfmt.toml` with `max_width = 120` is deliberately absent: it would reformat the workspace and conflict with every open branch. The 120-column rule is enforced by the diff gate on added and changed lines only. ## Tightening * Lower the numbers in `limits.json` (the census rises, so `--update-baseline --allow-increase` once, in a reviewed commit that also seeds the allowlist if a diff needs it). * Fix a legacy function (or split it): its allowlist entry becomes stale; `--update-baseline` drops it, and the census total falls with it. * Add a path to `python_include` to hold more Python to the limits. * When a crate is clean, nothing more is needed: the census row is gone and the total can only stay at zero for it. ## Decision (STRA-367) Owner order of 2026-11-06: encode the chapter 301 limits as machine-enforced gates, without asking. * **Erroring for new and changed code.** A function that is added or touched, and a line that is added or changed, must be within 30 lines, complexity 10, nesting 4, 5 parameters and 120 columns. Legacy that is moved verbatim is exempt only through an allowlist entry with a reason and an owner, and the entry can only shrink. * **Ratchet for legacy.** The census of what already misses the limits is recorded and its totals may only fall. * **No workspace reformat.** No `rustfmt.toml` with `max_width = 120`; the width rule applies to changed lines only. * **Clippy is an opt-in ratchet.** `too_many_lines`, `cognitive_complexity` and `too_many_arguments` are counted by an opt-in script, not turned into errors (every crate would fail) and not added as `#![warn]` attributes. The argument threshold is kept out of the root `clippy.toml` because the lint is on by default. * Not done here: seeding the allowlist for a `main` base; a CI job (the gate runs in `check-local.sh --product`).