Skip to content

fix(voice): Workspace read-aloud scrubs staged credential values before TTS, and the parity guard pins both remaining speech sinks (#4359) - #4361

Merged
vybe merged 13 commits into
devfrom
AndriiPasternak31/issue-4359
Nov 11, 2026
Merged

vybe merged 13 commits into
devfrom
AndriiPasternak31/issue-4359

Conversation

@AndriiPasternak31

@AndriiPasternak31 AndriiPasternak31 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #4356. This branch merges #4356's branch. Review only this PR's own commits: 67f615c8b, 41e926217, 8a0a96320, e0de9e31e, bd0f0f21c, 6aa923f05. Once #4356 merges, the diff shrinks to this change on its own.

What was wrong

  • Workspace read-aloud sent unscrubbed text to the TTS provider. client_portal/service.py::synthesize_portal_tts passed the text the client sends straight to tts_service.synthesize_mp3. The reply was scrubbed when it was saved, but a credential value staged after that save, or text that never went through a scrubbed save, reached the third-party provider as given.
  • The direct voice call's scrub was not pinned, and it ran on rewritten text. direct_voice_service._speak_sentence scrubs before streaming TTS, but _REQUIRED_SCRUBBERS in the parity guard didn't list it, so deleting that scrub turned nothing red. It also scrubbed each sentence after speakable() had rewritten it. speakable() drops **, __ and ~~ anywhere, so a value holding one no longer matched and was spoken.

What changed

  • Read-aloud. synthesize_portal_tts removes staged values before synthesis, after its refusals (empty text, voice unavailable, no voice), so a refused request never reads the store.
    • It scrubs before it strips the request text, so a value staged with edge whitespace still matches.
    • What is spoken carries the seam's spoken placeholder redacted (from fix(voice): scrub staged credential values from a voice note before synthesis (#4228) #4356), not ***REDACTED***, which would be read out as punctuation.
    • The cost cap judges the text as the client sent it. The placeholder is never longer than a staged value, so text that fits still fits after the scrub. The status a client gets (200 or 422) does not depend on what is staged.
    • It fails open on a store outage: the text is spoken as given, never a 5xx.
  • Direct call. DirectBridge.speak_execution scrubs each stream text block and the persisted reply before sentences() runs speakable().
    • A stored marker already in the saved reply is said as the spoken placeholder.
    • _speak_sentence keeps its scrub as the floor for every line, including the fixed lines and ask lines.
  • tests/unit/test_ent279_scrub_parity.py: _REQUIRED_SCRUBBERS now lists synthesize_portal_tts, _speak_sentence and speak_execution. Its comments no longer carry a sink count that goes stale with every addition.
  • Docs. runtime-secret-scrub.md lists every speech sink and explains the placeholder and the cap rule.
  • workspace-direct-voice.md says where the spoken reply is scrubbed (before speakable()) and what is said in its place.

Evidence

New tests/unit/test_4359_portal_tts_scrub.py, 14 tests:

  • Read-aloud cases drive the real synthesize_portal_tts and the real synthesize_mp3, so the cost cap is the real one, against the real scrub module over fakeredis, with Redis reads counted. Assertions read the JSON body the provider's HTTP client was sent.
  • Direct-call cases drive the real DirectBridge.speak_execution and run_speaker, with the real speakable() / sentences(), and read what the speech provider and the spoken frames received.
  • None of the staged values match a credential pattern. A precondition test shows the pattern sanitizer leaves them alone.
Case Result
read-aloud: staged value → provider receives redacted green
read-aloud: nothing staged → text byte-identical green
read-aloud: store outage → spoken as given green
read-aloud: text exactly at the cap with an 8-char value staged → spoken within the cap green
read-aloud: over-cap text → 422 whether or not the guess is staged, nothing sent green
read-aloud: voice unavailable / no voice → refused with zero store reads green
read-aloud: staged value with a leading or trailing space at the text's edge → scrubbed green
direct call: a streamed value holding __ → never spoken, raw or rewritten green
direct call: a value staged after the reply was saved → scrubbed from the spoken tail green
direct call: a stored marker in the saved reply → said as redacted green
direct call: nothing staged → spoken unchanged green

Mutation (each restored afterwards):

Mutation Result
direct call: stream scrub removed (sentences(item.text), the old shape) 1 red: the __ streamed-value test
direct call: tail scrub and marker swap removed 3 red: the streamed-value, staged-after-save and stored-marker tests
read-aloud: cap check on the text as sent removed 1 red: the over-cap status test
read-aloud: stored marker instead of the spoken placeholder 4 red: the provider, at-cap and both edge-whitespace tests
read-aloud: store read moved in front of the voice gates 2 red: both refusal store-read tests

Before the review round, deleting the scrub in synthesize_portal_tts turned 5 tests red and deleting the one in _speak_sentence turned the guard red, naming it. The guard now also pins speak_execution. It requires a scrub call in each pinned function, which speak_execution has twice: removing one leaves the guard green, and the behaviour tests above go red.

Validation:

Unverified: no call to the real ElevenLabs API, no read-aloud in a live Workspace and no live direct call. The providers are faked one layer below synthesize_mp3 and synthesize_stream.

Closes #4359

Journey Impact: none: a fix inside the Workspace read-aloud request, the direct-call speaker and a test guard; no journey's promise changes. Read-aloud and direct calls still speak the reply, only a staged credential value in it is no longer sent to the TTS provider

…ynthesis (#4228)

send_message removes every staged credential value (the ent#279
runtime_secret_scrub seam) from a body before it is delivered. Neither voice
send did: send_voice_reply (in-session) and send_proactive_voice (the
addressed path) handed the agent's raw text to text-to-speech, so a value an
agent had fetched could be sent to the TTS provider, spoken to the recipient,
and hosted as a WhatsApp voice.ogg. The addressed path also wrote the first
100 characters of the raw text into the append-only audit log as
message_preview, on every outcome past the gate: delivery, not_authorized,
rate_limited, recipient_not_linked and suppressed.

Both functions now scrub at the top, in the same shape send_message uses, so
everything downstream reads the scrubbed text: the audit preview, the
text_too_long cap (and the length its guidance quotes), and synthesis on
every channel. The cap therefore judges what synthesis receives: the 14-char
marker can make a text longer or shorter, and judging the raw text would let
a note that grew past the cap through to tts_service's own cost cap, which
refuses it before any provider call and surfaces it as synthesis_failed.
A store outage fails open, as it does for send_message.

The ent#279 parity guard now pins both voice sends as direct sinks. The tests
use a value no credential pattern matches, so only the staged-value store can
catch it.
…re TTS, and the parity guard pins both remaining speech sinks (#4359)

synthesize_portal_tts handed the text the Workspace client sent straight to
tts_service.synthesize_mp3. The reply was scrubbed when it was saved, but a value
staged after that save (or text that never went through a scrubbed save) reached
the third-party TTS provider as given. It now removes staged values first, the
same way send_message and the voice sends do: fail-open on a store outage, and the
shared cost cap inside synthesize_mp3 judges the scrubbed text.

The parity guard's _REQUIRED_SCRUBBERS now lists synthesize_portal_tts and
direct_voice_service._speak_sentence, so deleting either scrub turns CI red. Its
comments no longer carry a count that goes stale with each new sink.

Docs: the runtime-secret-scrub flow lists every speech sink, and the seam's
"behaviour-neutral for OSS" docstring is corrected — OSS code stages values too.

Closes #4359
…d value with edge whitespace still matches (#4359)

The Workspace secret field stages a value as typed, edge whitespace included.
synthesize_portal_tts stripped the request text first, which broke the exact
match when the value sat at the text's edge and sent it raw to the provider.
Scrub now runs on the text as sent; the strip and empty check follow.

Docs: the sessions bullet moves back above the speech-sink heading, and the
flow names services/gemini_voice.py tool results as an uncovered residual
instead of implying the speech-sink list is complete.
@AndriiPasternak31
AndriiPasternak31 marked this pull request as ready for review November 7, 2026 22:41
): "Workspace read-aloud text -> TTS provider (#4359)",
(
"services/direct_voice_service.py",
"_speak_sentence",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Direct-voice sink scrubs text that speakable() has already rewritten, so exact-match scrub can miss. _speak_sentence gets its input from sentences(item.text), and sentences() runs speakable() first. _MARKS removes __, ** and ~~ anywhere in a line, and _INLINE_CODE / _URL also change the text. So a staged value such as ab__cd1234efgh turns into abcd1234efgh before scrub_text sees it. The changed value then fails to match, goes to the streaming TTS provider, and goes back to the browser in the spoken frame. A second gap: each item.text is one stream delta, so a value split across two SSE entries never matches in full. This entry pins the function as covered, and the doc says it is, but the guard only checks that a scrub call exists, not what text it runs on. Scrub the raw reply (or the raw delta stream accumulated per turn) before speakable()/sentences().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in e0de9e3. speak_execution now scrubs each stream text block and the persisted reply before sentences() / speakable() run. _speak_sentence keeps its scrub as the floor for every line, including the fixed lines and ask lines, and speak_execution is now pinned in _REQUIRED_SCRUBBERS too. New tests drive DirectBridge.speak_execution with plum__orchid__velvet4359. Restoring the old sentences(item.text) turns test_a_streamed_value_that_speakable_would_rewrite_is_never_spoken red.

On a value split across two entries: normalize_stream_entry yields whole content blocks (the text block of a Claude Code assistant message, or a Codex item.completed message), not token deltas. A value spans two items only if the agent itself splits it across two text blocks, so I left that part as is.

result the caller reports so the agent replies with text)."""
# ent#279 / #4228: scrub staged credential values at the TOP, like
# send_message — before the cost cap judges the text and before synthesis.
_staged = get_staged_values()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The listener hears the redaction placeholder read out loud. scrub_text puts in ***REDACTED***, a marker meant for stored text. On a speech sink, ElevenLabs reads it aloud ("asterisk asterisk asterisk redacted", or similar). It can also make the text longer: an 8-char value becomes 14 chars, and each of the 3 renditions matches separately. A note that fits before the scrub can then fail _too_long, and the guidance tells the agent "this text is {len(text)}", a length it never wrote. Use a speech-specific replacement (e.g. "a redacted value"), or leave the value out of the cap count.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d374dea (the voice sends and synthesize_mp3) and e0de9e3 (read-aloud and the direct call). The seam now has SPOKEN_PLACEHOLDER ("redacted"), and scrub_text / scrub_staged take a marker. Every speech sink speaks it. The addressed note's audit preview keeps ***REDACTED***. The placeholder is 8 characters, the staging floor, so a scrub can never make spoken text longer. The cap now judges the text as the agent wrote it, and the guidance quotes that length.

Comment thread src/backend/client_portal/service.py Outdated

_staged = get_staged_values()
if _staged and text:
text = scrub_text(_staged, text)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Client-supplied text plus a global scrub store gives a membership oracle. In this route the client chooses text, and the staged set is global across all agents and owners. A Workspace user can POST a candidate string (for example an old leaked key) and tell whether it is currently staged anywhere on the instance. The answer shows in the audio, and often without listening: when a 14-char placeholder pushes the text over TTS_MAX_CHARS, the status flips between 200 and 422. Low severity, since it confirms a guess rather than revealing a secret, but it is new to this route. One option is to limit the scrub to values staged for this agent, or to a list of replies saved for the session.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partly fixed in e0de9e3. The status no longer changes. The cap judges the text as sent, before the scrub, so an over-cap text gets a 422 whether or not the guess is staged (test_the_status_does_not_depend_on_what_is_staged).

The audio still says "redacted" for a staged guess, and I left that for two reasons. First, the same client can already learn it from a voice call: persist_voice_turn scrubs its spoken turns against the same store when it saves them to the thread. Second, the store is global on purpose. It has no per-agent or per-session index, and the seam treats scrubbing a value wherever it appears as always correct, including text relayed between agents. Scoping it would weaken that for every sink. The flow doc now records this as a residual (bd0f0f2).

Comment thread src/backend/client_portal/service.py Outdated
# store down get_staged_values() returns [] and the text is spoken as given.
from services.runtime_secret_scrub import get_staged_values, scrub_text

_staged = get_staged_values()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated seam boilerplate; the and text guard is redundant. scrub_text already passes falsy text through and returns early on empty values, so if _staged and text: repeats both checks. The same 3-line _staged = get_staged_values(); if _staged: text = scrub_text(_staged, text) block now appears in send_message, both voice sends and here, while direct_voice_service / client_portal/voice.py use the one-line scrub_text(get_staged_values(), x). Add a scrub_staged(text) helper to runtime_secret_scrub and call it in all of them. The parity guard could then match one name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. scrub_staged(text, *, marker=...) is in the seam (d374dea). send_message, both voice sends, read-aloud and the direct call now use it (e0de9e3), and the parity guard counts it as a scrub call. The redundant and text check went with the old block.

- `voice_reply_service.send_voice_reply` (in-session voice note) and
`send_proactive_voice` (addressed voice note — also before its audit
`message_preview`) — #4228.
- `direct_voice_service._speak_sentence` — each sentence of a direct voice call,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Altitude: every speech sink is wired by hand, and #4362 is the gap that leaves. Each TTS caller scrubs itself (four sites, plus a residual in gemini_voice). All but Gemini Live go through tts_service.synthesize_mp3 / synthesize_voice_note or a provider's synthesize_stream. Scrubbing there would close the class for future callers too, and would no longer depend on remembering to add each one to _REQUIRED_SCRUBBERS. The callers would keep only the extra scrub needed before audit message_preview and the cap check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d374dea. tts_service.synthesize_mp3 scrubs before its cap and the request, so it and synthesize_voice_note cover any caller, and it is pinned in _REQUIRED_SCRUBBERS. Callers still scrub first where they also show or store the text (the audit preview, the cap guidance, the spoken frame). I did not wrap the streaming providers' synthesize_stream: its only caller is _speak_sentence, which has to scrub anyway for the spoken frame. Gemini Live stays with #4362.

# send_message — before the cost cap judges the text and before synthesis.
_staged = get_staged_values()
if _staged:
text = scrub_text(_staged, text)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The staged-value read runs before the cheap refusals, on the event loop. get_staged_values() makes a synchronous Redis HGETALL plus one decrypt per staged member. It now runs at the top of both functions, before the unsupported_channel / no_destination checks and before _gate's tts_unavailable / voice_disabled / channel_disabled refusals, which don't need the text. It also blocks the loop inside an async def. Move the scrub to after the channel and destination checks, right before _gate (the cap still sees scrubbed text), or wrap it in asyncio.to_thread. The same applies at L279 and in synthesize_portal_tts, which scrubs before the tts_service.is_available() check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Both voice sends (d374dea) and read-aloud (e0de9e3) now read the store only after their channel, destination and gate refusals. The tests count Redis reads on each refusal and assert zero. I did not move the read to asyncio.to_thread. Every other scrub sink (send_message, the execution appliers, chat persistence) reads the store on the loop, and this path now reads it only on a call that is about to synthesize. If the read cost matters, the fix belongs in get_staged_values, for every caller.

…b at synthesis (#4228)

Review follow-up on the voice-note scrub.

- runtime_secret_scrub gains SPOKEN_PLACEHOLDER ("redacted") for text that is
  read aloud, a marker argument on scrub_text, and scrub_staged(text) for a sink
  that scrubs one text. The placeholder is no longer than the staging floor, so a
  scrub never lengthens spoken text.
- Both voice sends scrub after the channel, destination and gate refusals, so a
  refused call never reads the store, and the cap judges the text as the agent
  wrote it. Its answer and the length it quotes no longer depend on what is
  staged, which closes a probe of the shared store through that length. The
  addressed path's audit preview keeps the stored marker.
- tts_service.synthesize_mp3 scrubs too, so any caller (and synthesize_voice_note)
  is covered; it is pinned in the parity guard, which now also counts
  scrub_staged as a scrub call.
- send_message uses scrub_staged; behaviour unchanged.
…to AndriiPasternak31/issue-4359

# Conflicts:
#	tests/unit/test_ent279_scrub_parity.py
…ud judges the cap as sent (#4359)

Review follow-up.

- DirectBridge.speak_execution scrubs each stream text block and the persisted
  reply before sentences() runs speakable(), which drops `__`, `**` and `~~`
  anywhere, so a value holding one no longer slips past an exact-match scrub on
  the rewritten text. A stored marker already in the saved reply is said as the
  spoken placeholder. _speak_sentence keeps its scrub as the floor for every
  line. speak_execution is pinned in the parity guard.
- synthesize_portal_tts runs its refusals before it reads the store, judges the
  cost cap on the text as the client sent it (the spoken placeholder never
  lengthens text), and speaks the spoken placeholder. The status a client gets
  no longer depends on what is staged.
… the scrub flow (#4228)

The ent#279 flow did not list the two voice sends or synthesize_mp3 as
scrub sinks, nor the marker parameter, scrub_staged or the spoken
placeholder this PR adds. It now does, with the rule that a cap judges
text as written, the positive pins in the parity guard, the
reformatted-value residual and the new test file.
…to AndriiPasternak31/issue-4359

# Conflicts:
#	docs/memory/feature-flows/runtime-secret-scrub.md
… is scrubbed (#4359)

"Spoken text is scrubbed of staged secrets" no longer described the
order that matters: the stream blocks and the saved reply are scrubbed
before speakable() rewrites them, and what is spoken says "redacted".
Links the scrub flow's speech-sink section and names the test file.
@AndriiPasternak31

Copy link
Copy Markdown
Contributor Author

The red regression diff on the previous head (6aa923f) was test_ent926_runtime_turn_scope::test_an_unreachable_platform_is_no_answer raising a raw httpcore.ConnectError. That failure is already on dev: the same test fails with the same error on the dev push runs at d389d26, e6d36a9 and 119264b, and none of those commits contain this branch. It is tracked in #4439, and the fix is in #4447. Here is what happens. When a test makes the first httpx request inside a patch.dict(sys.modules) block, httpcore gets dropped when the block exits. The next import loads a second copy of httpcore, and its ConnectError no longer matches the exception map httpx has cached, so it gets past except httpx.HTTPError. Whether a run hits this depends on which test files end up on the same xdist worker. Nothing in this PR patches sys.modules or makes a real request. I merged dev in, and the new head (80029c4) is green, including the regression diff.

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge-train: batch validated on train/20261111-0613 (#4463)

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.

3 participants