Files
leleka/docs/architecture/tech-debt.md
T
Pavlo Ratushnyi 7699138a6e STRA-338 S5b: decompose serve_load into ordered phase functions
serve_load (about 1000 lines, 1170 after rustfmt) becomes load.rs (a 20-line order of
the phases) plus: load_admit (every refusal that needs no model byte, the memory
governor start), load_sources + load_bind (verify, bind, identity digest chain, expert
bank), load_identity, load_cuda (driver, context, probes, hand-over to the Original
serve stack), load_weights + load_moe + load_moe_gates (output head, KAR PLE, dense and
MoE caches; the adaptive and learned-main witness gates), serve_loop + serve_generate +
serve_run + serve_report (the command loop and one Generate), serve_close (teardown).
owner() is split the same way. Each phase takes a small context (LoadEnv, Bound,
Identity, Weights, Serving, Exchange) and returns a typed result.

Order and side effects are kept: the admission checks, the env reads
(LELEKA_ORIGINAL_SERVE_GGUF_PLE is still read in gguf_ple_load and in the refusal
detail line), the log texts (including the load-gate detail line and the
KAR_MTP_PROFILE / NATIVE_MTP_PROFILE logic of the kar-off-ab branch), the cancellation
checks, the lap marks, and the drop order of the CUDA objects (Weights drops dense, MoE,
KAR PLE, output head; the context is closed after the weights and outlives the binding).
The three conditions of the former execution gates are one match, the policy flags one
PolicyFlags::of. No GPU or numerical path was touched.

New tests (backend/load_tests.rs): PolicyFlags against the former expressions, the
execution gates against the former three sequential checks for all 6 executions x 8
boolean combinations, and the weight-cache log line against its exact text and the
saturating subtraction. Test ids: 689 before the S5 work, 694 now (the five added), the
same 2 environment-dependent failures and 58 ignored; cargo check --all-targets for
leleka-runtime and leleka-deploy gives the same warning counts, clippy reports nothing
in backend/. All new and changed functions are within 30 lines, complexity 10, nesting 4,
except one 127-column line that is a 119-character MoeWeightCache constructor name
(docs/architecture/tech-debt.md, which also lists what S5a moved verbatim and still misses
the limits). moe_weight_cache.rs gains three pub(crate) use lines for the witness types.
The architecture baseline loses the transient backend/load.rs entry.
2026-11-05 21:39:26 +01:00

3.0 KiB

Architecture tech debt (STRA-367)

Debt that a hexagonal step moved but did not remove. A line here names the file, the limit it misses, and what removes it. The size and ratchet numbers themselves live in architecture-baseline.json.

Limits for new and changed code: function at most 30 lines, cyclomatic complexity at most 10, nesting at most 4, line at most 120 columns, file at most 400 lines.

crates/leleka-runtime backend/ (STRA-338 S5a, S5b)

backend.rs (3797 lines) is a parent of 258 lines plus responsibility-named children. serve_load is decomposed into load.rs (the order of the phases) and load_admit, load_sources, load_identity, load_bind, load_cuda, load_weights, load_moe, load_moe_gates, serve_loop, serve_generate, serve_run, serve_report, serve_close; all of them are inside the limits except the one line named first. What was moved verbatim in S5a and is still outside the limits:

Where Misses Removed by
load_moe_gates.rs, the learned_main_lru constructor call one line of 127 columns: the method name of MoeWeightCache is 119 characters renaming the constructor in qwen_forward/moe_weight_cache (its own step)
generate_mtp.rs generate_mtp_resume 250 lines, complexity 44 splitting the MTP window loop into phases (engine-core step; GPU identity and A/B gates apply)
generate.rs prefill_chunk, generate, step, target_step 90, 60, 55, 34 lines; complexity 14 and 10 same step as above
spawn.rs spawn_backend_configured 100 lines; recomputes the policy flags that PolicyFlags::of now owns use PolicyFlags::of, split the owner hand-over
spawn_optimized.rs seven factories of 32 to 39 lines repeating one precondition block one OptimizedSelection check shared by the factories
sources.rs verify_sources_parallel, read_ranked_expert_profile 42 and 51 lines splitting the scoped verification and the profile read
proxy.rs start_generation, validate.rs validate_handle 44 lines; 46 lines and complexity 14 splitting the checks into predicates
config.rs the *_EXECUTION identity strings are single lines of 120 to 200 columns string continuation, with the golden digest test as the guard

Process-wide state and reads that the policy file architecture.json counts and that this directory still holds: RECONFIGURING, INSTANCE and GPU_ORDINAL (process globals, S14), the LELEKA_ORIGINAL_SERVE_GGUF_PLE, LELEKA_PUBLIC_EVALUATION and similar environment reads in load_admit.rs, spawn_optimized.rs, spawn_original.rs (Settings, S13), Instant::now() in generate*.rs, utf8.rs and sources.rs (Clock, S14), and the Box<dyn Backend> returned by the factories (startup composition only).

qwen_forward/moe_weight_cache.rs gained three pub(crate) use lines (AdaptiveWitness, LearnedMainWitness, SharedPermanentWitness) so the witness gates in load_moe_gates.rs can name the types; no code changed there.