Skip to content

review: whole-repo round — comment budget, layout, ownership, claim-path allocations - #73

Merged
CMGS merged 17 commits into
mainfrom
review/whole-repo-2026-09-09
Sep 9, 2026
Merged

review: whole-repo round — comment budget, layout, ownership, claim-path allocations#73
CMGS merged 17 commits into
mainfrom
review/whole-repo-2026-09-09

Conversation

@CMGS

@CMGS CMGS commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

A whole-repository quality round over every Rust file (65 production, 9 integration test files, all read in full), followed by the loc-justify audit. Fifteen commits, each gated; nothing here changes a documented contract except where stated.

Comments, layout, naming

  • Inline comments lead with a lowercase word across 26 sites; four lines that restated code or a neighbouring doc are cut; test modules import the this-crate group last.
  • Predicates read as predicates (is_stopping, is_complete, is_blocklisted, defers_ledger_writes, distributes_batches, can_seal); items nothing outside their file uses become private; two single-use wrappers are inlined.

Ownership and the request path

  • Engines move the bodies they own: the Vertex engine takes its messages like its siblings; family_outcome owns the model name; anthropic_native_chunks takes the content blocks (the views tail moves them, the engine copies once instead of twice); the DashScope stream callback takes the frame; the MCP proxy takes method and tool out of the owned envelope and an unparsable segment no longer copies the whole reply.
  • The billing ledger takes its row by value (a billed request stops cloning nine Strings); select_healthy probes without materializing; vendor prices come from a hash index; the memory ledger takes the rollup lock only when a cap is set; the streaming DLP walk reuses one path buffer.
  • AppState sits behind one Arc (axum's two per-request clones cost one refcount each instead of fifteen); a UserHint extractor replaces the cloned HeaderMap on eleven REST handlers; a GlobalAdmin extractor replaces nine admin prologues and rejects before the config body is buffered; the model protocol kind is parsed once at load.
  • The blocklist is compiled into an Aho-Corasick automaton at load (the old scan was a windowed compare per word per string leaf), same matching semantics.

Correctness

  • Bug: the Postgres key store never stored a key's MCP servers and tool allowlists, so with storage.postgres_url set the MCP proxy answered 404 for every key. Two columns with ADD COLUMN IF NOT EXISTS; round-tripped by the Postgres-gated test.
  • The Converse engine skips a content block or tool whose container is not an object instead of panicking the request task (serde_json's IndexMut); a malformed image part no longer gains a stray null key.
  • A fail-loud DagContext::model_param replaces four inconsistent guards (one would have silently allowed an empty model name).
  • An age index on video_jobs; batch items insert in one statement per chunk.

Measured (cocoon-test1, main vs branch, 5 interleaved rounds, order swapped)

body rps main → branch p50 main → branch
small256 208.5k → 218.6k (+4.8%) 0.89 → 0.35 ms
big256 159.6k → 168.3k (+5.5%) 0.88 → 0.38 ms
msgstream256 5.72k both (upstream-paced) 44.0 ms both

Gates

cargo fmt --check, clippy --all-targets -D warnings, cargo test --workspace clean locally (593/0) and on Linux with GW_TEST_REDIS_URL (593/0); the Postgres-gated state and key-store suites ran against a throwaway Postgres (91/0, columns and index verified server-side).

Dependencies

grpc-go 1.83.2 (control plane, indirect, xDS DoS advisory); vitest 4.1.11 (web dev dependency).

Added after an external review pass

  • Bug: the per-key cost counter was keyed by the access key, so a Redis read error logged the credential; keyed by the fingerprint now (a budget in flight at deploy starts over).
  • Bug: MCP result review skipped any data/blob field at any depth; the payload is skipped only inside an image/audio block or a blob resource.
  • Bug: a fallback-served request sampled one failure and one success against the requested model; only the client-visible outcome samples now.
  • The in-process usage rollup keeps 62 days and at most one million buckets (oldest first); Postgres unchanged.

CMGS added 17 commits September 9, 2026 19:05
An indirect dependency; the control plane runs no xDS server, so the
advisory is not reachable here, but the pinned range is the flagged one.
Inline comments lead with a lowercase word (identifiers keep their case)
across 26 sites; four lines that restated the code or a neighbouring doc
are cut; two narrative comments are rephrased to one fact per line.
The this-crate group (use super::*) comes last, blank-line separated,
in the five test modules that had it first; a function-body use in the
offline handler moves to the file's std group.
Boolean fns read as predicates: is_stopping, is_complete, is_blocklisted,
defers_ledger_writes, distributes_batches, can_seal. Items nothing outside
their file names become private: whole_seconds, signing_key, the two token
encoders, the offline handler's inner field, three engine re-exports; an
unused Clone derive goes. default_moderator and is_response_create are
inlined at their one caller.
Engines move the bodies they own instead of copying them: the Vertex
engine takes its messages like its siblings and gemini_parts consumes
each turn; family_outcome owns the model name so four callers move it;
anthropic_native_chunks takes the content blocks so the views tail moves
them and the engine copies once instead of twice; the DashScope stream
callback takes the frame; the OpenAI system prepend inserts in place.
The MCP proxy takes method and tool name out of the owned envelope, and
an unparsable segment carries no payload (the whole non-JSON reply was
copied and then discarded). Smaller borrow-first fixes: the realtime
subprotocol key, tool-block start events, the terminal-retention subject
(one Arc instead of two Strings), the Bedrock invoke URI, a search
summary, two Default restatements, one json! deep copy, and Json(job).

Behaviour: a Converse content block or tool whose container is not an
object is skipped instead of panicking the request task (serde_json's
IndexMut panics on a non-object), and a malformed image part no longer
gains a stray null key on its way upstream.
The billing ledger takes its row by value: the unattributed deferred path
moves it into the queue and recovers it from a refused send, so a billed
request no longer clones nine Strings; settle_and_bill returns the two
totals its callers read. select_healthy probes the serving accounts
without materializing them first. The serving account's vendor prices
come from a hash index instead of a scan of every account (account names
must now be unique, which the pool, health and latency tables already
assumed). The memory ledger takes the rollup lock only when a row cap is
set. The blocklist scan returns before lower-casing when the list is
empty, and the streaming DLP walk reuses one path buffer per delta
instead of formatting a String per key.
AppState sits behind one Arc, so the two clones axum makes per request
cost one refcount each instead of fifteen. A UserHint extractor reads
x-gw-user from the request parts, so eleven REST handlers no longer take
a cloned HeaderMap they read one header from; a GlobalAdmin extractor
replaces the three-line token prologue on nine admin handlers and, as a
FromRequestParts extractor, rejects before the config body is buffered.
ModelConf parses its protocol kind once at load instead of scanning the
wire table on every lookup.
One fail-loud DagContext::model_param accessor replaces four guards on a
state resolve_model already rules out (the tenant-entitlement guard would
have silently allowed an empty model name); weighted_pair is inlined at
its one caller; the Responses reasoning-replay scan is written once; the
security-scan rows are extended without a temporary Vec; batch items
take the owner override in place; the realtime relay bills a turn from
one call site and the mock session from one; the redacted stream tail
delegates to synth_chunks after clearing the raw deltas; the Converse
mock loses a dead Cohere branch and scans for the last user turn once;
walk_native_event dispatches once on the event type; ScanCounts::visit
returns nothing; a redundant drop and an always-true streamed_live go.
The MCP client's unreachable fallback, which would have followed
redirects, is an expected invariant instead; the SIGTERM install error
logs its value.
The access_keys table had no columns for a key's MCP servers and tool
allowlists, so every key read back from Postgres carried an empty grant
and the MCP proxy answered 404 for all of them whenever storage.postgres_url
was set. Two JSON text columns, added with ADD COLUMN IF NOT EXISTS for
existing tables, ride through the upsert and the three reads; the
Postgres-gated test round-trips a config-declared grant and an admin put.
The retention DELETE that runs on every async video submit scanned a
table with no index on created_at_epoch_secs; both dialects now carry
one. Postgres batch_enqueue inserted items one round trip at a time
inside the submit transaction; it now uses the same push_values shape as
ledger_add_batch, chunked under the parameter limit.
Every string leaf of every request and response ran a windowed
case-insensitive compare per blocklist word (a 20-word list over a 100 KB
prompt is millions of comparisons per request). The list is built into an
Aho-Corasick automaton where it is lower-cased at load, so a leaf costs
one pass; a list that fails to build is logged and ignored, as a failing
regex rule already is. SecurityConf::compiled runs the same step for a
policy built outside the loader. Semantics are unchanged: ASCII text
matches case-insensitively, other text is lower-cased first.
Setup calls that bind, serve, build a transport, a mock response or a
request carry a short expect message, as the suite's own helpers already
did; the batch-pricing test asserts the batch actually completed instead
of falling through its poll loop; the two QPM warm-up loops say which
call failed.
validate() already rejected duplicate account names further down; the
second check added with the account index was redundant.
The per-key cost counter was keyed by the access key itself, so the
fail-open warning on a Redis read error logged the credential; it is now
keyed by the key's fingerprint like every other log field (a budget in
flight at deploy time starts over).

The MCP result review skipped any field named data or blob at any depth,
so structuredContent: {"data": ...} prose escaped it; the payload is
skipped only inside an image/audio block or a blob resource.

A request served by a fallback model recorded one failure for the first
attempt and one success for the fallback, both against the requested
name, so one successful call read as a 50% error rate. The handler now
tells the pipeline when a fallback is still ahead and a failed attempt
samples only when it is the client-visible outcome.
The memory store's minute buckets were insert-only and keyed by the
client-supplied user id, so a caller rotating that id grew the map for
the life of the process. Each rollup pass now drops buckets older than
62 days (the monthly-budget window) and, above one million buckets, the
oldest minutes first. Postgres keeps its table until an operator prunes
it, as before.
@CMGS
CMGS merged commit c99669a into main Sep 9, 2026
2 checks passed
@CMGS
CMGS deleted the review/whole-repo-2026-09-09 branch September 9, 2026 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant