refactor(voice): extract _dispatch_inner helpers (A-5, PR-3D-b)#73
Merged
Conversation
Second of the PR-3D split. Behavior-preserving extraction of the voice-dispatch monolith codec._dispatch_inner into the two helpers the audit recommended: - _build_voice_system_prompt(task) -> str — the memory/identity/facts injection + CODEC_VOICE_PROMPT assembly (get_memory + CodecMemory context + memory_upgrade identity/facts + compress). Caller derives safe_sys. - _persist_voice_turn(task, answer, rid) — append the assistant message to the session, bump turn_count, update_session_response (WAL helper), and save the exchange to CodecMemory. _dispatch_inner is now a flow of named calls (188 -> 131 LOC). The LLM call was already codec_llm.call (A-12 tranche 1). Faithfulness detail: _persist_voice_turn carries its own `from codec_memory import CodecMemory`. The original persist block had no local import — it relied on the build block's `from codec_memory import CodecMemory` (both are in _dispatch_inner) leaving CodecMemory in function scope. Extracting the build block removed that, so the persist helper imports it itself. No other behavior change: prompt assembly, memory markers, DB-write truncation (answer[:500]), and the skill/draft/terminal branches are untouched. Tests: tests/test_dispatch_inner_helpers.py (7 — base prompt / get_memory / facts / identity injection; persist appends+increments+writes+saves; DB-write truncation; source invariant). Full suite 1451 passing, 23 known-baseline failures, zero new. Zero net-new ruff. No skills/ touched. A-6 (chat_completion / SkillTagBuffer) follows as PR-3D-c. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
5 tasks
AVADSA25
added a commit
that referenced
this pull request
May 22, 2026
…-6, PR-3D-c) (#74) Final piece of the PR-3D split. Extracts the streaming <think> + [SKILL:...] tag-machine out of codec_dashboard.chat_completion._stream_gen into a new, tested module — exactly as the audit recommended. New codec_chat_stream.py: - SkillTagBuffer — the stateful token processor. feed(token)/finish() are generators that yield clean text fragments to emit; it strips <think>…</think> across chunks and buffers [SKILL:name:query] tags char-by-char so a raw tag never leaks, resolving complete tags via an injected resolve_skill_tag(raw) callback (skill execution is I/O, hence injected). visible_chars lets the caller detect the all-tags-dropped blank-bubble case. - SKILL_TAG_RE — the shared tag pattern (buffer detection + the dashboard resolver). _stream_gen now keeps only the SSE/HTTP plumbing (POST, iter_lines, data:/[DONE] framing, keepalive, blank-bubble fallback) + the injected _resolve_skill_tag (budget + allowlist + dispatch). chat_completion 466 -> 379 LOC. Behavior preserved EXACTLY, including the subtle quirks: a same-chunk </think> is dropped (token zeroed after <think>), think-adjacent text is emitted but not counted toward visible_chars, dropped (resolved-to-empty) tags still emit their empty frame, the 5000-char safety cap, and cross-chunk tag assembly. The non-streaming post-LLM [SKILL:] path is untouched. Bonus: SkillTagBuffer is the tested unit the deferred A-12 dashboard-stream migration needs — the dashboard stream can now consume codec_llm.stream()'s raw tokens through it. Tests: tests/test_chat_stream.py (13 — passthrough, think cross-chunk + same-chunk quirk, tag resolved/dropped/assembled-across-tokens, non-tag bracket passthrough, prefix divergence, 5000 cap, finish flush, regex). Full suite 1464 passing, 23 known-baseline failures, zero new. Zero net-new ruff. No skills/ touched. PR-3D complete: all three monoliths decomposed (A-7 #72, A-5 #73, A-6 here). Co-authored-by: Mickael Farina <farina.mickael@gmail.com> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
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
Second of the PR-3D split (one sub-PR per monolith). Behavior-preserving extraction of the voice-dispatch core
codec._dispatch_innerinto the two helpers the audit recommended:_build_voice_system_prompt(task)→str— the memory/identity/facts injection +CODEC_VOICE_PROMPTassembly (get_memory+CodecMemorycontext +memory_upgradeidentity/facts + compress). Caller derivessafe_sys._persist_voice_turn(task, answer, rid)— append the assistant message to the session, bumpturn_count,update_session_response(WAL helper), save the exchange toCodecMemory._dispatch_inneris now a flow of named calls (188 → 131 LOC). The LLM call was alreadycodec_llm.call(A-12 tranche 1).Faithfulness detail:
_persist_voice_turncarries its ownfrom codec_memory import CodecMemory. The original persist block had no local import — it relied on the build block'sfrom codec_memory import CodecMemoryleavingCodecMemoryin_dispatch_inner's function scope. Extracting the build block removed that, so the persist helper imports it itself. No other behavior change — prompt assembly, memory markers, DB-write truncation (answer[:500]), and the skill/draft/terminal branches are untouched.Test plan
tests/test_dispatch_inner_helpers.py— 7 tests:_build_voice_system_prompt(base prompt /get_memory/ facts / identity injection);_persist_voice_turn(appends + increments + DB-write + CodecMemory save); DB-write truncation to 500; source invariant.skills/touched → no manifest regen.Follow-on
chat_completion: extractSkillTagBuffer→codec_chat_stream.py(also unblocks the deferred A-12 dashboard-stream migration). The last and biggest of the three.🤖 Generated with Claude Code