Files
leleka/docs/architecture/size-fn.md
T

16 KiB

Function size pass (STRA-338, batch size-fn)

Every .rs file of the crates below was already within 400 lines (batches size-split-*). This batch brings the functions within the limits of limits.json (30 lines, complexity 10, nesting 4, 120 columns), longest first, with the behaviour unchanged: one helper per rule, stage or item kind, a small context struct for the values a long function threads through, builder and assert helpers for tests, big literals as constants. Entries of limits-allowlist.json of the fixed functions are gone.

Crates: strata-control, strata-desktop-core, leleka-deploy, leleka-gateway-text, leleka-gateway-domain, leleka-worker-protocol, leleka-runtime-client, leleka-offline-tools, leleka-gptoss, leleka-cpu-sys, leleka-cpu-kernels, leleka-cpu-worker, leleka-igpu*, leleka-gptoss-igpu, leleka-gptoss-npu, leleka-npu-*, leleka-gpu-npu-worker, leleka-gguf, leleka-kar-codec, leleka-harmony, leleka-tokenizer, leleka-shm, leleka-openvino, leleka-serving-policy, leleka-sampler-ref, leleka-quant-core, leleka-experimental-ref, leleka-native-parity-bench.

Numbers

  • Allowlist entries: 429 (all "moved verbatim ... not yet decomposed") -> 133 (115 older entries that remain, 18 new ones for the functions this pass touched and could not finish; the entries of the unfixed deploy tests are the largest group).
  • Functions over a limit in these crates, from the first line of the table: production fn>30 486 -> 17, complexity 340 -> 11, nesting 46 -> 0; test code fn>30 507 -> 122, complexity 35 -> 3, nesting 47 -> 4. Total functions listed by the census tool 1037 -> 144.
  • limits-baseline.json and architecture-baseline.json were regenerated with --update-baseline only (nothing raised).

Per crate (before -> after; production columns, then test columns; "wide" counts lines over 120 columns):

Crate prod fn>30 cx>10 nest>4 wide test fn>30 cx>10 nest>4 wide
leleka-cpu-kernels 1 -> 0 0 -> 0 0 -> 0 0 -> 0 8 -> 6 3 -> 1 5 -> 3 1 -> 1
leleka-cpu-sys 11 -> 0 3 -> 0 2 -> 0 3 -> 3 16 -> 16 0 -> 0 0 -> 0 2 -> 2
leleka-cpu-worker 7 -> 0 5 -> 0 0 -> 0 6 -> 5 6 -> 0 1 -> 0 2 -> 0 11 -> 5
leleka-deploy 101 -> 0 48 -> 0 7 -> 0 305 -> 247 89 -> 64 0 -> 0 0 -> 0 63 -> 60
leleka-experimental-ref 15 -> 0 11 -> 0 1 -> 0 19 -> 4 14 -> 9 2 -> 1 2 -> 1 37 -> 37
leleka-gateway-domain 15 -> 0 12 -> 0 5 -> 0 45 -> 16 3 -> 0 0 -> 0 0 -> 0 1 -> 1
leleka-gateway-text 14 -> 0 16 -> 0 1 -> 0 64 -> 61 12 -> 0 0 -> 0 2 -> 0 20 -> 6
leleka-gguf 6 -> 0 6 -> 0 0 -> 0 3 -> 1 3 -> 0 0 -> 0 0 -> 0 1 -> 1
leleka-gptoss 12 -> 0 6 -> 0 1 -> 0 1 -> 1 14 -> 5 4 -> 0 0 -> 0 8 -> 7
leleka-gptoss-igpu 1 -> 0 1 -> 0 0 -> 0 2 -> 2 5 -> 1 0 -> 0 0 -> 0 3 -> 3
leleka-gptoss-npu 5 -> 0 3 -> 0 2 -> 0 3 -> 2 4 -> 1 0 -> 0 0 -> 0 6 -> 5
leleka-gpu-npu-worker 3 -> 0 1 -> 0 0 -> 0 3 -> 2 2 -> 0 1 -> 0 0 -> 0 4 -> 1
leleka-harmony 4 -> 0 6 -> 0 2 -> 0 1 -> 0 3 -> 0 0 -> 0 0 -> 0 5 -> 0
leleka-igpu 10 -> 1 1 -> 0 0 -> 0 5 -> 5 12 -> 12 1 -> 1 0 -> 0 0 -> 0
leleka-igpu-worker 2 -> 0 1 -> 0 0 -> 0 1 -> 1 0 -> 0 0 -> 0 0 -> 0 0 -> 0
leleka-kar-codec 4 -> 0 3 -> 0 0 -> 0 0 -> 0 0 -> 0 0 -> 0 0 -> 0 0 -> 0
leleka-native-parity-bench 0 -> 0 0 -> 0 0 -> 0 9 -> 9 0 -> 0 0 -> 0 0 -> 0 0 -> 0
leleka-npu-service 6 -> 0 5 -> 0 0 -> 0 14 -> 3 3 -> 0 0 -> 0 1 -> 0 1 -> 1
leleka-npu-worker 2 -> 0 1 -> 0 0 -> 0 2 -> 2 2 -> 2 0 -> 0 0 -> 0 0 -> 0
leleka-offline-tools 8 -> 0 5 -> 0 0 -> 0 1 -> 0 3 -> 0 0 -> 0 0 -> 0 0 -> 0
leleka-openvino 2 -> 1 0 -> 0 0 -> 0 1 -> 0 0 -> 0 0 -> 0 0 -> 0 0 -> 0
leleka-quant-core 1 -> 0 0 -> 0 2 -> 0 0 -> 0 11 -> 0 1 -> 0 0 -> 0 0 -> 0
leleka-runtime-client 9 -> 0 4 -> 0 0 -> 0 4 -> 3 6 -> 0 0 -> 0 0 -> 0 0 -> 0
leleka-sampler-ref 9 -> 0 4 -> 0 0 -> 0 0 -> 0 14 -> 0 1 -> 0 2 -> 0 0 -> 0
leleka-serving-policy 13 -> 0 10 -> 0 0 -> 0 6 -> 5 9 -> 0 0 -> 0 0 -> 0 1 -> 1
leleka-shm 1 -> 0 0 -> 0 0 -> 0 0 -> 0 0 -> 0 0 -> 0 0 -> 0 0 -> 0
leleka-tokenizer 13 -> 0 7 -> 0 0 -> 0 21 -> 14 10 -> 0 1 -> 0 0 -> 0 10 -> 2
leleka-worker-protocol 3 -> 0 3 -> 0 0 -> 0 0 -> 0 1 -> 0 0 -> 0 0 -> 0 0 -> 0
strata-control 146 -> 0 122 -> 0 17 -> 0 185 -> 50 194 -> 0 17 -> 0 30 -> 0 131 -> 17
strata-desktop-core 62 -> 15 56 -> 11 6 -> 0 52 -> 13 63 -> 6 3 -> 0 3 -> 0 27 -> 0
total 486 -> 17 340 -> 11 46 -> 0 756 -> 449 507 -> 122 35 -> 3 47 -> 4 332 -> 150

Pass 2 (drive the census of these crates to zero)

Same method, then the owner's single-responsibility rule: one function does one action, a helper is named verb + object, a name that needs "and" or "then" is split again, the decision (which branch) is separate from the execution (how), a check_* is a predicate and does not mutate or compute, a test is setup, action and assertion as named steps.

  • strata-desktop-core: the 15 production functions and the 6 test functions left by pass 1 (ToolResponse::complete and preflight with the new module runtime_tool_response/preflight.rs, hardware::inventory, node respond with the new module node/respond.rs, node_connection::session, node_proxy::respond, planner::plan, Pool::new, cli::node, main, managed_worker start/run, ...).
  • Test functions and examples over 30 lines in leleka-deploy (64), cpu-sys, cpu-kernels, igpu, experimental-ref, gptoss, gptoss-igpu, gptoss-npu, npu-worker, npu-service: builder, step and assert helpers; test names, ids and assertions unchanged. cpu-sys/tests/attention.rs keeps its test ids and moved its non-test helpers to tests/attention/kernel_checks.rs; leleka-deploy/tests/e2e_windows.rs moved its checks to tests/e2e_windows/checks.rs.
  • Lines over 120 columns: 599 -> 38 in these crates. Comments are re-filled by paragraph; a long message string is broken at a space with a trailing backslash (Rust strips the leading whitespace of the next line, so the evaluated text is byte-identical) or split with concat!; JSON macros are re-wrapped. A lexer comparison of the evaluated string literals of every changed file against the previous commit found identical sequences in 234 of 295 files and identical multisets in 34; the other 27 are files whose code changed (a helper took the format arguments, a literal moved to another function) and were read. The probe script of desktop-core/src/remote.rs is a raw string; its nvidia-smi line is continued with a shell backslash (the same command, not the same bytes).
  • Single responsibility (review of pass 1): about 50 two-action test helpers split (for example assert_response_status and assert_response_headers). check_* functions that mutated, computed or did I/O were made predicates or renamed for what they do. Pure predicates (take &self or plain values, no I/O, return Result<(), _>): trace_items::check_call, check_snapshot_is_attached, check_context_budget, check_pool_bounds, check_workers_fit_their_group, the per-field CreateRequest::check_* of gateway-domain/src/responses/validate.rs. Renamed because they act: cancel_stalled_generation (was check_stall), probe_host (check_host), fail_if_api_exited and fail_if_engine_exited (they reap a child), read_assistant_text, detach_if_unattached (an RPC, then it detaches), abort_cold_preparation (journal write); record_call_id was split out of check_call. Decision apart from execution in runtime_rpc (judge_load, admit_generation), the backend worker (count_failed_start, served_after_refusal, judge_request, report_stopping, report_engine_lost), harmony, gateway-text and gptoss (softmax_scores_in_place says it rewrites the scores; rotate_queries is pure and store_rotated_kv writes the cache; arithmetic and attributes unchanged). The three Supervisor types are now AttachLoop (backend_worker/attach.rs), SessionSupervisor (worker/run_supervised.rs) and ServiceSupervisor (leleka-deploy).
  • Regression of pass 1 fixed: spawn_engine turned a spawn error into StartupFailed with map_err, which cannot await, so the status stayed Starting; the original set Failed first. Restored, with the test a_failed_engine_spawn_marks_the_worker_failed.

Per crate (pass 1 end -> pass 2; production fn>30, cx>10, wide, then test fn>30, cx>10, nesting>4, wide):

Crate prod fn>30 cx>10 wide test fn>30 cx>10 nest>4 wide
leleka-cpu-kernels 0 -> 0 0 -> 0 0 -> 0 6 -> 0 1 -> 0 3 -> 0 1 -> 0
leleka-cpu-sys 0 -> 0 0 -> 0 3 -> 0 16 -> 0 0 -> 0 0 -> 0 2 -> 0
leleka-cpu-worker 0 -> 0 0 -> 0 5 -> 0 0 -> 0 0 -> 0 0 -> 0 5 -> 0
leleka-deploy 0 -> 0 0 -> 0 247 -> 33 64 -> 0 0 -> 0 0 -> 0 60 -> 0
leleka-experimental-ref 0 -> 0 0 -> 0 4 -> 0 9 -> 0 1 -> 0 1 -> 0 37 -> 0
leleka-gateway-domain 0 -> 0 0 -> 0 16 -> 0 0 -> 0 0 -> 0 0 -> 0 1 -> 0
leleka-gateway-text 0 -> 0 0 -> 0 61 -> 0 0 -> 0 0 -> 0 0 -> 0 6 -> 0
leleka-gguf 0 -> 0 0 -> 0 1 -> 0 0 -> 0 0 -> 0 0 -> 0 1 -> 0
leleka-gptoss 0 -> 0 0 -> 0 1 -> 0 5 -> 0 0 -> 0 0 -> 0 7 -> 0
leleka-gptoss-igpu 0 -> 0 0 -> 0 2 -> 0 1 -> 0 0 -> 0 0 -> 0 3 -> 0
leleka-gptoss-npu 0 -> 0 0 -> 0 2 -> 0 1 -> 0 0 -> 0 0 -> 0 5 -> 0
leleka-gpu-npu-worker 0 -> 0 0 -> 0 2 -> 0 0 -> 0 0 -> 0 0 -> 0 1 -> 0
leleka-igpu 1 -> 1 0 -> 0 5 -> 5 12 -> 0 1 -> 0 0 -> 0 0 -> 0
leleka-igpu-worker 0 -> 0 0 -> 0 1 -> 0 0 -> 0 0 -> 0 0 -> 0 0 -> 0
leleka-native-parity-bench 0 -> 0 0 -> 0 9 -> 0 0 -> 0 0 -> 0 0 -> 0 0 -> 0
leleka-npu-service 0 -> 0 0 -> 0 3 -> 0 0 -> 0 0 -> 0 0 -> 0 1 -> 0
leleka-npu-worker 0 -> 0 0 -> 0 2 -> 0 2 -> 0 0 -> 0 0 -> 0 0 -> 0
leleka-openvino 1 -> 1 0 -> 0 0 -> 0 0 -> 0 0 -> 0 0 -> 0 0 -> 0
leleka-runtime-client 0 -> 0 0 -> 0 3 -> 0 0 -> 0 0 -> 0 0 -> 0 0 -> 0
leleka-serving-policy 0 -> 0 0 -> 0 5 -> 0 0 -> 0 0 -> 0 0 -> 0 1 -> 0
leleka-tokenizer 0 -> 0 0 -> 0 14 -> 0 0 -> 0 0 -> 0 0 -> 0 2 -> 0
strata-control 0 -> 0 0 -> 0 50 -> 0 0 -> 0 0 -> 0 0 -> 0 17 -> 0
strata-desktop-core 15 -> 0 11 -> 0 13 -> 0 6 -> 0 0 -> 0 0 -> 0 0 -> 0
total 17 -> 2 11 -> 0 449 -> 38 122 -> 0 3 -> 0 4 -> 0 150 -> 0

Allowlist entries: 133 -> 1 (OpenVino::load); the entries of functions that only had wide lines went when the lines were wrapped. limits-baseline.json and architecture-baseline.json were regenerated with --update-baseline only (nothing raised).

What stays over a limit, and why

  • igpu/src/shaders/gemm.rs :: gemm (175 lines, five lines over 120 columns): WGSL shader text returned as one string; data, not logic. It has no allowlist entry because no diff touches it; the census baseline holds it.
  • openvino/src/library.rs :: OpenVino::load (84): FFI symbol table of about 25 dlsym lookups; the table is the point (allowlist entry, owner STRA-338).
  • leleka-deploy/src/cli.rs USAGE (33 lines over 120 columns): the --help text is user-visible and scripts/release/package-5090.sh parses it by line.
  • Open (single responsibility, not done): check_* that still do I/O or take &mut self and are not renamed: check_free_vram (reads the GPU), check_unit_not_failed, check_load_not_failed and check_load_progress (call the CLI), check_discovery, RuntimeGeneration::check_frame (advances the sequence state). Names that are composers or return a decision together with an effect: become_ready (flag, log line, status, answers to waiting loads), store_rotated_kv (rotates K and writes the cache), pin_engine_gpu (rechecks the GPU, pins it and records Failed on refusal), measure_checked_call (validates one call item and returns its size), encode_bounded_frame (returns the frame and whether it was replaced).

Kernels

The owner allowed the hot numeric kernels to be split. Rule used: helpers keep the arithmetic order (no reassociation, no change of FMA or accumulation order), keep the unsafe blocks and SAFETY comments with their code, and are #[inline(always)] where the original was one body in a hot loop (#[inline] with the same #[target_feature] for AVX2 and AVX-VNNI helpers, because inline(always) cannot be combined with target_feature). Tensor identity was proven per function with a throwaway test that compiled the original files from git next to the new ones and compared output bits on fixed xorshift inputs:

Crate Functions Proof
leleka-cpu-sys simd x86 attn_weighted_sum_avx2, attn_weighted_sum_f16_avx2, dot_i8i8_avxvnni, dot_i4i8_avxvnni, dot_mxfp4_q8r4_avxvnni, dot_mxfp4_q8r4_avx2, quantize_q8_0_avx2 792 cases bit-identical in debug and release (attention heads 1-4, hd 1-192, n 1-40; dots n 0-513; mxfp4 0-33 blocks; quantize 0-33 blocks incl. zero and -0.0). The host CPU has no AVX-VNNI: the three VNNI variants were compared with vpdpbusd replaced by a scalar emulation (proves the restructuring, not the instruction), and the real VNNI path was only compiled
leleka-cpu-kernels Dispatch::with_isa (three tables moved unchanged) existing parity tests
leleka-quant-core iq3_s::decode_block, iq3_xxs::decode_block 40000 random blocks per format bit-identical (iq3_s also 845 identical error texts)
leleka-gptoss attend_group_with_sink, kar_roundtrip 6 shapes x ISA levels (12 cases) and 46 round-trip cases, outputs and error paths identical

No performance claim is made. The codegen effect of the splits is not A/B-measured; the kernels on the serving path need a paired A/B (same workload, quality result) before anything is said about speed.

Verification

  • Test ids and outcomes: cargo test --no-fail-fast --all-targets on Linux (WSL) for the 28 crates and for `leleka-deploy
    --features test-fakes, before (0c594d351) and after, compared with scripts/dev/test-outcomes.py compare`
    0 missing, 0 new, 0 outcome changes (strata-control and the 27 other crates: 1067 ok before, 1078 ok after, the 11 differences are tests that the base log could not resolve or that failed once on the base; leleka-deploy: 213 ok / 5 failed / 2 ignored before and after). The five leleka-deploy e2e failures (dry_run_prints_the_plan_and_changes_nothing and four in reconfigure_flow) fail on the base too: they report the free VRAM of the host GPU. The strata-control lib suite had one flaky failure on the base run (responses::tests::history::durable_history_roundtrip_delete_eviction_and_faults).
  • cargo clippy --no-deps --all-targets -- -D warnings clean for the crates above; result_large_err is allowed in the modules of the /v1/responses handler and the gateway relay of strata-desktop-core because the error value is the finished HTTP response. cargo fmt --check for strata-control, strata-desktop-core, leleka-runtime-client and leleka-deploy differs only in files this batch did not change.
  • Gates: scripts/check-limits.py --base origin/claude/hexagonal-integ 0 errors, 0 stale; scripts/check-architecture.py 0 errors (the dyn-trait ratchet of strata-desktop-core is held by spelling dyn leleka_backend_api::Backend in the binaries); scripts/check-ci-coverage.py green.

Method note

The work was split by directory over parallel branches (claude/szfn-*), each in its own worktree with its own target directory, then merged here. Functions were fixed longest first; helpers are placed in new sibling modules where a file would pass 400 lines. New modules are listed in the commits.