Files
sglang/.claude/rules/general-code-style.md
Cheng Wan 99cfc90658 config: retire ServerArgs.override in favour of derive()
`ServerArgs.override(source, **fields)` was the last way to change a resolved
`ServerArgs` in place. Every remaining call-site was one of two things, and
neither wanted an in-place write:

- **A config for someone else.** A draft worker's context length, an encode
  worker's device, the compile script's watchdog, the client's port pick, a test
  fixture's backends. These already deepcopied first — the write was on the copy.
- **A launcher-stage resolution.** `resolve_auto_parsers` detected the chat
  template's parsers and wrote them back, to be inherited by the schedulers it
  spawns.

Both are "one config becomes another", so `derive(source, **fields)` returns the
variant and leaves the receiver — and any bags projected from it — untouched. It
deliberately is not `dataclasses.replace`: resolution does not re-run, because
the values being set are decided after it, from inputs it never had. Provenance
and the resolvable-field stash work as before, on the copy.

`resolve_auto_parsers` now computes the parsers and returns the config to launch
with; the detection helpers stop taking a config to mutate. `HiMambaRadixCache`
re-applied a HiCache layout normalization `__post_init__` already performs (the
same duplicate removed from `UnifiedRadixCache` in ebb1c88d23) and just goes.

With no in-place mutation left, `ServerArgs.__setattr__` raising after
resolution *is* the guarantee, so the textual writer ratchet retires and
`test_server_args_derive.py` pins the contract instead: the receiver survives
deriving, the published instance still refuses assignment, and deriving does not
publish. `SGLANG_STRICT_CONFIG_MUTATION` was already unused — the guard has been
unconditional since the mutation sweep — and goes with it.

The detection tests drop their `SimpleNamespace` stand-in for a real
`ServerArgs`; the test kit and the MLA chunk-metadata fixture publish a derived
variant instead of writing the runner's published config.
2026-08-05 19:30:53 -07:00

2.2 KiB

paths
paths
**/*.py

General Code Style

Default conventions for new and modified Python code. Prefer these unless there is a concrete reason not to; call out deviations in review.

  • Prefer stateless. Favor pure functions over methods that mutate instance state; pass inputs in, return outputs out.
  • Prefer immutable. Default to immutable data (frozen structs, tuples, read-only values); mutate only when there is a clear need.
  • Extract init-static values at construction. When a derived value's inputs are frozen for the object's lifetime (typically configuration: constructor args, env vars, server args), compute it once in __init__ and store it as a well-named attribute (self.mtp_enabled, self.needs_cpu_seq_lens); later code reads the attribute instead of re-deriving it. Input immutability is the hard precondition — if inputs can change, recompute in place or funnel mutation through a single override point (get_context().override() for resolved config). If you can't give the value a meaningful name, the boundary is wrong — don't cache unnameable subexpressions.
  • Functions stay small. Keep each function under ~100 LOC; split larger ones into named helpers.
  • Files stay small. Keep each file under ~2k LOC; split larger modules along cohesive boundaries.
  • Core functions read like pseudocode. The main / orchestration function of a unit should be short and read like algorithm pseudocode — push detail into well-named helpers so the top-level flow is obvious.
  • Avoid mixins. Don't add behavior via mixin classes; prefer explicit composition (hold a collaborator and call it) or plain functions.
  • Prefer protected over public. Default methods to protected (_name); expose only what callers actually use.
  • Prefer keyword arguments. Call functions of 2+ args by keyword, and design APIs to be called that way.
  • Pass what you need, not the god object. Give a callee the specific values it uses (by keyword), not a whole large object (ModelRunner, Scheduler); reserve passing the whole object for a leaf whose contract genuinely requires it. Even then, keep it read-only — read fields off it and return results for the caller to assign, rather than writing fields back through it.