refactor(dashboard): H1 — extract chat_completion (+helper cluster) into routes/chat.py#166
Merged
Merged
Conversation
…routes/chat.py (SR-59)
codec_dashboard.py: 2,022 → 1,310 LOC (-712 / -35.2%).
Cumulative since pre-B6 baseline: 3,912 → 1,310 (-66.5%).
The largest remaining endpoint (the /api/chat LLM handler) plus its tightly-
coupled private helpers move as one verbatim cluster into routes/chat.py:
- _fetch_url_content URL strip-and-fetch for inline context
- _enrich_messages memory / URL / web-search context injection
- CHAT_SKILL_ALLOWLIST skills a chat message may auto-fire (53 entries)
- _try_skill pre-LLM hijack (shared w/ /api/command — re-exported)
- _try_skill_by_name post-LLM [SKILL:] resolver + calculator fallback
- _chat_vision_response image → vision-model branch (A-11 pending)
- _build_chat_system_prompt override + step-budget + observer suffixes
- chat_completion the POST /api/chat endpoint
Behavior preserved verbatim (script-extracted, not retyped). All safety
surfaces intact and pinned by a new regression test:
* pre-LLM hijack consumes 1 step + gates destructive skills on
codec_consent.chat_consent_ok
* post-LLM [SKILL:] tags stay allowlist-gated + budget-gated + DROPPED
(never leaked raw) on failure — both stream + non-stream paths
* the <think>/[SKILL:] streaming token machine stays codec_chat_stream.SkillTagBuffer
Back-compat: codec_dashboard re-exports all 7 helpers identity-equal to
routes.chat (verified in test), so /api/command's _try_skill call and the
existing test surface (codec_dashboard.CHAT_SKILL_ALLOWLIST, _build_chat_system_prompt,
etc.) keep working untouched. CHAT_SYSTEM_PROMPT stays in codec_dashboard
(routes.prompts also imports it) and is lazy-imported in routes/chat.py to
avoid a load-time cycle.
What stays in codec_dashboard: /api/command (safety-critical), the auto-
escalate classifier cluster (Step 10, independent of chat_completion),
/api/services/status, page renderers, startup/shutdown hooks, _bg_* daemons.
Test surface:
- new TestH1ChatExtraction (6 pins): endpoint registered, router export,
7-helper identity-equal re-export, safety-surfaces-preserved scan,
dashboard-doesn't-redefine, LOC floor < 1,400
- test_dashboard_llm: codec_llm.call >=3→>=2 (chat non-stream moved),
/chat/completions ==2→==1 (only _warmup_vision left), stream/KEEPALIVE
scan retargeted codec_dashboard → routes/chat
- test_a12_invariant: routes/chat.py allowlisted (chat vision branch, A-11 pending)
- test_route_extractions_d: lazy-import pin retargeted to routes/chat.py
- drive-by ruff F401: dropped now-unused `secrets` + SkillTagBuffer/SKILL_TAG_RE
imports from codec_dashboard
Full suite: 2,052 passed / 77 skipped (was 2,046 in G). +6 net.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Merged
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
8th wave of the codec_dashboard route-extraction series — and the big one. Moves the largest remaining endpoint, the
/api/chatLLM handler (chat_completion), plus its tightly-coupled private helper cluster, into a newroutes/chat.py. Behavior is preserved verbatim (script-extracted, not retyped).LOC reduction (cumulative since pre-B6 baseline)
codec_dashboard.py is now 1,310 LOC — a 66.5% cut from where this refactor began.
What moved to
routes/chat.py(798 LOC)chat_completionPOST /api/chatendpoint_fetch_url_content_enrich_messagesCHAT_SKILL_ALLOWLIST_try_skill/api/command)_try_skill_by_name[SKILL:...]resolver + calculator fallback_chat_vision_response_build_chat_system_promptSafety surfaces — preserved & pinned
A new
TestH1ChatExtraction::test_safety_surfaces_preservedscansroutes/chat.pyto lock these in:codec_consent.chat_consent_ok[SKILL:...]tags stay allowlist-gated + budget-gated + dropped (never leaked raw) on any failure — both stream and non-stream paths<think>/[SKILL:...]streaming token machine stayscodec_chat_stream.SkillTagBufferBack-compat (zero behavior change)
routes.chat(asserted in test), so/api/command's_try_skillcall and the existing test surface (codec_dashboard.CHAT_SKILL_ALLOWLIST,_build_chat_system_prompt, …) keep working untouched.CHAT_SYSTEM_PROMPTstays in codec_dashboard (routes.promptsalso imports it) and is lazy-imported inroutes/chat.pyto avoid a load-time cycle.What intentionally stays in codec_dashboard.py
/api/command(safety-critical) · the auto-escalate classifier cluster (Step 10, independent of chat_completion) ·/api/services/status· page renderers · startup/shutdown hooks ·_bg_*background daemons.Test plan
python3.13 -m pytest --ignore=tests/test_skills.py -q→ 2,052 passed, 77 skipped (was 2,046 in G; +6 net)test_full_product_audit::test_api_command_safety_gatestill green (reworded comments so itsindex("/api/command")heuristic still finds the real endpoint)ruff check: 0 issues (dropped now-deadsecrets+SkillTagBuffer/SKILL_TAG_REimports)python3 -c 'import codec_dashboard': clean (no import cycle)🤖 Generated with Claude Code