Repository navigation
Description
Problem
desktop/macos/Backend-Rust/src/routes/chat_completions.rs is ~3,300 lines and the fragility magnet of the crate: ten commits touched it between 2026-07-08 and 2026-07-15, and the week's bug chains collided inside it — the retrieval-heuristic chain (#9384 → #9660 → #9692), SSE UTF-8 corruption (#9638, lossy decode per network chunk), stream termination (#9720). Its real contents: the route dispatcher, outbound request translation (including the OMI_CONTEXT_CACHE_BOUNDARY system-prompt cache splitting at :418), the retrieval gate and tool/instruction injection points (:187, :764 — classification itself already lives in routes/retrieval_policy.rs), SSE framing helpers (:667), and one large async_stream closure (:1274) where byte framing, Anthropic event parsing, OpenAI chunk translation, tool ordinals, usage accounting, and terminal state share mutable locals.
Every concern change risks the others because one file and one namespace hold them all. This is a structural smell, not a bug.
Fix: mechanical package split around the existing units, one PR, no behavior change
Split into a routes/chat/ module tree along the seams that actually exist today — the entangled streaming closure moves intact; decomposing it is explicitly not this PR:
| Module | Owns (moved as-is) |
|---|---|
route |
the dispatcher/handler (already a modest dispatcher) |
request_translation |
outbound Anthropic request construction, including context_cache (the OMI_CONTEXT_CACHE_BOUNDARY splitting) and the retrieval gate + tool/instruction injection points |
sse |
framing/termination/chunk helpers (#9638's domain, incl. drain_sse_events) |
streaming |
the async_stream closure, moved wholesale with its locals — behavior-preserving by construction |
transport |
client construction, connect/timeout plumbing |
Rules and honest limits:
- Outward API: the sole item used outside this file today is
chat_completions_routes(re-exported inroutes/mod.rs:28, merged inmain.rs:334). That is the singlepub(crate)boundary; everything else is private orpub(super). - What visibility enforces and what it doesn't:
pub(super)limits the outward surface; it cannot forbid sibling-to-sibling use insideroutes/chat/. Directed sibling rules ("provider translation may not reach into SSE framing") are review policy, recorded inroutes/chat/ARCHITECTURE.md(useful documentation — not claimed as CI-enforced; the arch guardrail doesn't inspect this path and five files don't hit its threshold). - Scoped lints: the new module gets
#![deny(dead_code, unreachable_pub)]following the existingservices/redis.rs/services/firestore.rsprecedent — the crate does not deny these globally, so this is a named, small, non-move change. - Retrieval classification stays in
routes::retrieval_policy— this PR does not pretend a new module owns retrieval; chat owns only its gate and injection call sites. - Tests: all 79 existing tests are inline
mod tests { use super::*; }, so test modules and imports move with their code — no assertion changes, but "suite unmodified" would be a false claim. One new hermetic test is added before the move: scripted byte chunks in → emitted SSE sequence out, driving the real streaming closure — the coverage that lets "behavior-preserving" be verified rather than asserted (today no test drives the full incremental stream). - Per the in-repo-consumers rule (FC-5: compiler-enforced voice-turn ownership — extract VoiceTurnDomain target, end the convergence churn #9832): no re-export shims for old paths beyond the single named outward API; call sites update in the same PR.
Follow-up (explicitly out of this PR)
Decomposing the streaming closure into a tested state machine (separating Anthropic event parsing from OpenAI chunk emission, reifying tool ordinals/usage/terminal state) is real behavior risk and its own issue, unblocked by this split and by the new byte-chunk→SSE test.
Proof
- Full test suite green with zero assertion edits (module/import moves only).
- The new byte-chunk→SSE sequence test passes identically before and after the move (written first, on the pre-split file).
#![deny(dead_code, unreachable_pub)]clean on the new module; single outward API verified.- A named smoke run of the real chat path (streaming + tool call) on a dev build.
Cut list
- No decomposition of the streaming closure (see follow-up) — bugs found during the move are filed, not fixed inline.
- No redesign of retrieval policy, provider translation, or timers.
- No moving
retrieval_policy.rs. - No split of other large files (
auth.rs,paywall.rs) — siblings follow only if they hit the FC-class trigger. - No new CI checks beyond the scoped lint attribute.
Sizing
One PR, large diff but mechanically reviewable: review reduces to the module API surfaces (small), the new stream test, and trust in the unmodified assertions. Sequenced strictly after the FC-4 deadline PR (#9835) so no behavioral change hides inside the move.
Activity
build(quality): ratchet oversized product source files (#9851)
undivisible commentedon Jul 20, 2026
chat_completions.rs split into routes/chat/ — confirmed: desktop/macos/Backend-Rust/src/routes/chat/route.rs exists on origin/main.
build(quality): ratchet oversized product source files (BasedHardware…