Repository navigation
Description
Summary
Three router handlers are each over 300 lines and hold the business logic of their feature. Move that logic into services so the routers return to request parsing, auth and response shaping.
Context
Found by the /refactor-audit run of 2026-11-10. Invariant #1 in docs/memory/architecture.md (Router → Service → DB) says routers hold no business logic.
| Handler | Lines | Complexity | Location |
|---|---|---|---|
update_setting |
454 | 36 | src/backend/routers/settings/generic.py |
voice_websocket |
312 | 34 | src/backend/routers/voice.py |
trigger_webhook |
304 | 40 | src/backend/routers/webhooks.py |
The same audit shows the pattern works: public_chat and deploy_system were 268 and 187 lines in June, were moved into services, and neither router handler is flagged now.
update_setting is the generic PUT /api/settings/{key} catch-all. Its length comes from per-key validation and side effects accumulated in one function.
#4418 adds an import contract for the layering. That catches a router importing db/ directly; it does not catch logic written inline in a handler, which is what these three are.
Acceptance Criteria
-
update_setting: per-key validation and side effects live in the settings service layer, dispatched by key; the handler is under 50 lines -
trigger_webhook: token resolution, idempotency derivation and dispatch live in a service; the handler is under 50 lines -
voice_websocket: session setup and the relay loop live in a service; the handler keeps only the socket accept, auth and teardown - Route paths, registration order, status codes and response bodies are unchanged (Invariant fix: add missing logging_config.py to backend Dockerfile #4; the settings
/{key}catch-all stays last, pinned bytests/unit/test_1028_settings_package.py) - The
db.set_settingsink guard for credential-shaped keys still rejects with 422 on every path through the moved code -
trigger_webhookstill accepts and auto-derivesIdempotency-Key(Invariant Unified Executions Dashboard (EXEC-022) #18) - Existing tests for the three endpoints pass without loosening an assertion
Technical Notes
- One PR per handler.
- Tests that monkeypatch a name on the router module will need to patch the service that now owns it. Per Invariant Fix: Add missing Docker labels to system agent container #1(a), do not re-export a private name from the router to keep an old patch target alive.
- Related: refactor: enforce Router → Service → DB with an import contract and a shrink-only baseline #4418 (layering import contract).
Activity
docs(reports): refactor audit 2026-11-10 — full src/ scan (#4461)