Skip to content

refactor(voice): extract _dispatch_inner helpers (A-5, PR-3D-b)#73

Merged
AVADSA25 merged 1 commit into
mainfrom
fix/pr3d-b-dispatch-inner
May 22, 2026
Merged

refactor(voice): extract _dispatch_inner helpers (A-5, PR-3D-b)#73
AVADSA25 merged 1 commit into
mainfrom
fix/pr3d-b-dispatch-inner

Conversation

@AVADSA25
Copy link
Copy Markdown
Owner

Summary

Second of the PR-3D split (one sub-PR per monolith). Behavior-preserving extraction of the voice-dispatch core 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), 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 leaving CodecMemory in _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.
  • Full suite: 1451 passed, 23 known-baseline failures, zero new, 74 skipped.
  • Ruff: per-file delta vs origin/main = 0 (codec.py's 70 are its pre-existing E402/E741 from the signal-before-imports pattern; new test clean).
  • No skills/ touched → no manifest regen.
  • Manual (Mac Studio): a cold-start voice command still replies + persists; a warm session still defers TTS.

Follow-on

  • PR-3D-c — A-6 chat_completion: extract SkillTagBuffercodec_chat_stream.py (also unblocks the deferred A-12 dashboard-stream migration). The last and biggest of the three.

🤖 Generated with Claude Code

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>
@AVADSA25 AVADSA25 merged commit e3a561c into main May 22, 2026
1 check passed
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>
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.

2 participants