Skip to content

feat(voice): proactive voice notes — address send_voice_reply by role or email (ent#700) - #3768

Merged
vybe merged 4 commits into
devfrom
feature/ent700-proactive-voice
Nov 4, 2026
Merged

vybe merged 4 commits into
devfrom
feature/ent700-proactive-voice

Conversation

@trinity-ability

@trinity-ability trinity-ability commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Gives send_voice_reply an addressing surface so a run with no inbound conversation (a schedule, a loop, a reminder, an agent-to-agent task) can speak. This is shape (a) of the issue. Before this change a scheduled run got not_a_channel_turn from send_voice_reply, while send_message from the same run reached the same person on the same channel.

Fixes abilityai/trinity-enterprise#700
Partially addresses #2999 (AC 3, AC 5)

Merge timing: after 1.0.0 (not release-critical).

Behaviour

  • Addressing. send_voice_reply now takes an optional address plus channel (telegram | whatsapp). The address is either:

    • to, a role (primary | approver | viewer, the ent#606 shape), or
    • recipient_email (deprecated; logged when an agent key uses it).

    Use one or the other, never both. Without an address, the in-session reply is unchanged (FR-1..FR-9).

  • Same gates as proactive text. An addressed note must pass:

    • db.can_agent_message_email consent;
    • the per-recipient hourly rate limit. Voice uses the same Redis counter as send_message, so it is not a cheaper way around the limit;
    • a PROACTIVE_MESSAGE audit row with modality: voice.

    The agent-level and per-channel voice flags still apply.

  • One resolver. role_addressing.resolve_single_recipient is extracted from routers/messages.py (Invariant Fix: Add missing Docker labels to system agent container #1), and both routes use it. A role that several people fill, or that nobody fills, is refused by role and never names a person. The voice route returns these refusals fail-soft with guidance.

  • Destination. The recipient's verified chat link, found with the same lookup send_message uses. Never the execution row.

  • Dedup (feat: effect-scoped idempotency keys for outbound side effects #1084).

    • The effect key is the resolved (recipient email, channel).
    • Pre-send refusals (rate_limited, recipient_not_linked) and delivery_error release the claim on the addressed path, so a retry in the same run tries again.
    • A provider refusal (send_failed) and configuration misses (no_binding, …) stay recorded: a retry fails the same way and would pay for another synthesis. send_failed carries guidance, naming WhatsApp's 24-hour window.
    • The in-session path records every delivery outcome, as it did before this PR.
  • Idempotency key (ent#665). A scheduler retry is a new execution row, so the addressed path also takes idempotency_key / idempotency_ttl: one note per (agent, recipient, key) across runs. A suppressed call answers delivered: true, sent: false, so the agent does not repeat it in text. Only a delivered note spends the key, and voice keys do not collide with text keys.

  • Owner stand-in (ent#904). primary with no seat reaches the agent's owner, now in resolve_single_recipient, so text and voice share it. resolve is unchanged, so asks and gate approvals keep their rule.

  • Named refusals with guidance (bug(voice): agents tell Workspace clients the surface is text-only — portal TTS is never advertised, and the channel/portal voice gates disagree #2157 pattern):

  • Cost cap. Text over TTS_MAX_CHARS is refused before synthesis as text_too_long with max_chars, on both paths. It used to look the same as synthesis_failed (bug(voice): send_voice_reply cannot deliver from a scheduled execution — the destination is read only from an inbound channel stamp, and the refusal names the trigger instead of the constraint #2999 AC 5). Callers keep notes short; there is no chunked multi-note send.

  • Public helpers. The ProactiveMessageService gate helpers are now public: check_rate_limit, increment_rate_limit, audit_send. The private names are gone and every reference is updated.

  • MCP tool. voice.ts gains to, recipient_email and channel (Invariant feat: SMARTS trading pipeline with Telegram notifications and Miro visualization #13). The tool refuses a half or double address locally and passes guidance and max_chars through.

Deferred (follow-ups filed)

Docs

  • Requirements §48.1 FR-10/FR-11 (docs/memory/requirements/public-access.md)
  • architecture/{api-endpoints,mcp-server,backend}.md: which triggers can deliver voice
  • User doc user-docs/advanced/voice-replies.md

Test evidence

  • New file tests/unit/test_ent700_proactive_voice.py (58 tests on SQLite), including claim release, replay and key behaviour on the real effect_guard and idempotency store.

  • Mutation checks. Each change was made in a scratch copy, the named tests went red, and the file was restored byte-identical:

    Mutation Tests red
    skip consent 3
    skip rate limit 2
    pre-send refusal does not release the claim 2
    proactive transient failure does not release 1
    in-session transient failure does not release 1
    router ignores the address 3
    router ignores to 2
    resolver drops the several-people refusal 3 (incl. test_ent606_role_addressing.py)
  • Updated tests:

  • Affected unit files after merging dev (voice, proactive, role addressing, idempotency; -n 8): 2387 passed, 5 skipped, 1 failed. The failure is test_1310_auth_consolidation.py::test_admin_sites_admit_admin[sqlite-avatar.generate_default_avatars], the read-only /data failure already noted here as environmental; it was not re-run on dev this time.

  • Tier 1 at 92a715527: the required checks, pytest (head, seed 12345) and regression diff all passed.

  • MCP: npm test 1151 pass, 0 fail (includes src/tools/voice.test.ts); tsc --noEmit is clean.

  • Not run: live-stack tests.

🤖 Generated with Claude Code

… or email

send_voice_reply could only speak into the conversation an execution row was
stamped with, so a scheduled run got `not_a_channel_turn` while send_message
to the same person delivered. The tool now also takes an address — `to` (a
role, the ent#606 shape) or deprecated `recipient_email` — plus `channel`
(telegram | whatsapp), so any run can speak.

- Addressed sends pass the proactive-text gates: can_agent_message_email
  consent, the per-recipient hourly rate limit (one budget across text and
  voice), and a PROACTIVE_MESSAGE audit row (modality: voice).
- Recipient resolution is one service rule shared with send_message:
  role_addressing.resolve_single_recipient (extracted from routers/messages.py,
  Invariant #1); role refusals name the role, never a person.
- Effect dedup on the resolved (recipient, channel). Pre-send refusals and
  transient delivery failures (send_failed, delivery_error — both paths)
  release the claim so a retry in the same run tries again; config misses
  stay recorded.
- Over-TTS_MAX_CHARS text is refused before synthesis as `text_too_long` +
  `max_chars` (was indistinguishable from synthesis_failed); an unaddressed
  non-channel call keeps `not_a_channel_turn` but now carries guidance.
- ProactiveMessageService gate helpers are public: check_rate_limit,
  increment_rate_limit, audit_send.
- Slack proactive voice returns `proactive_channel_unsupported` (deferred).

Mutation-checked: removing the consent check, the rate-limit check, the
claim release (pre-send and transient, each path), the router's addressed
branch, the `to` branch, or the several-people refusal each turns tests in
tests/unit/test_ent700_proactive_voice.py red.

Related: Abilityai/trinity-enterprise#700
Partially addresses #2999 (AC 3, AC 5)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

⚠️ Nightly unit-suite check skipped — merge conflict against dev.

Resolve by running git merge dev locally and pushing the result. The next nightly run will re-test once the conflict is gone.

@github-actions

Copy link
Copy Markdown

⚠️ Live-instance suite skipped — merge conflict against dev.

Resolve by merging dev locally and pushing the result; the next nightly re-tests.

@obasilakis obasilakis 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.

Requesting changes. The head is 166 commits behind dev and conflicts in src/backend/routers/messages.py and tests/registry.json; two of the findings below turn CI red after the merge.

  1. High src/backend/services/role_addressing.py:178-212, src/backend/routers/messages.py:130: dev added the ent#904 rule (nobody holds primary → fall back to the agent's owner, or refuse with a specific message when the owner has no email). resolve_single_recipient lacks it, so taking the PR side of the conflict removes it from send_message and test_ent904_primary_unseated.py fails. Resolved correctly, voice still refuses to=primary as role_unassigned on a fresh install while text reaches the owner, which is the operator daily-brief case. Move the fallback into resolve_single_recipient so both paths get it.
  2. High src/backend/services/proactive_message_service.py:112 and the other two renames: dev's tests/unit/test_3746_telegram_long_replies.py:233-236 monkeypatches _check_rate_limit, _increment_rate_limit and _audit_send. setattr raises on the missing attributes after merge, with no textual conflict to flag it. Update that test or keep private aliases.
  3. Medium src/backend/services/voice_reply_service.py:68: send_failed releases the claim, and the rate counter increments only on delivery. WhatsApp outside Twilio's 24h window (63016) and a Telegram user who blocked the bot fail every time, so each retry pays for a new ElevenLabs synthesis and a hosted media file with no budget cap. The in-session path also changes to release on send_failed, which AC 4 says stays unchanged. Release only on delivery_error, or count failed sends against the budget; keep the in-session behaviour; add guidance for the 24h-window failure.
  4. Medium src/backend/services/voice_reply_service.py:249: dedup is keyed per execution, and a scheduler retry creates a new execution row (retry_of_execution_id). A brief that spoke and then failed later speaks again on retry, so AC 3 is open. Accept idempotency_key / idempotency_ttl on the addressed path as text does (ent#665), or document the limit in FR-10.
  5. Low src/backend/routers/agents.py:1612: the not_a_channel_turn guidance points agents at recipient_email. Name to (primary) first and assert on to in test_2157 / test_ent700.
  6. Low src/mcp-server/src/tools/voice.ts:109: channel description says "Requires recipient_email"; should read "Requires to or recipient_email".
  7. Low tests/unit/test_ent700_proactive_voice.py:79-140: claim release is tested against a fake _Guard. Add one test on the real effect_guard with the SQLite idempotency store for the rate-limit release and the replay.

Checked and fine: an agent key speaks only as itself and keeps ent#629 reach; the consent check runs before anything email-dependent; role refusals never name the resolved email; public docs carry ticket references only.

trinity-ability and others added 3 commits November 3, 2026 19:09
Keeps dev's can_open_agent / resolve_role_id_for_ask beside the extracted
resolver, and moves the ent#904 owner stand-in from routers/messages.py into
role_addressing.resolve_single_recipient, so send_message keeps it and
send_voice_reply gains it (review item 1). dev's #3746 test now patches the
public proactive helpers: its setattr on the private names would raise after
the merge, with no textual conflict to flag it (review item 2).
tests/registry.json rebuilt from the merge stages.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- A provider refusal (send_failed) stays recorded on the addressed path and
  says why with guidance, naming WhatsApp's 24-hour window; only delivery_error
  releases the claim, so a retry never pays for a synthesis that will be refused.
- The in-session path records every delivery outcome again, as it did before
  (AC 4).
- The addressed path takes idempotency_key / idempotency_ttl (ent#665) because
  a scheduler retry is a new execution row. A suppressed note answers
  delivered=true, sent=false; only a delivered note spends the key; voice keys
  do not collide with text keys. Model, route, service and MCP tool.
- not_a_channel_turn guidance names `to` first; the channel description names
  both address forms.
- Tests: the owner stand-in by voice, and claim release, replay and key
  behaviour on the real effect_guard and idempotency store.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vybe

vybe commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Addressed the requested changes from the 2026-10-28 review. Head is 92a715527, merged with dev at 538922cb2.

# Finding Status Commit Verified by
1 Owner stand-in missing from the shared resolver Done eeaba1743 (fallback), 7a77751bd (tests) New voice tests plus test_ent904_primary_unseated.py. Disabling the fallback turned 5 tests red
2 test_3746 patches the renamed helpers Done eeaba1743 test_3746 passes. The remaining _check_rate_limit hits belong to message_router's own function
3 send_failed releases the claim Done, with the limits below 7a77751bd Fake-guard and real-store tests; in-session pinned for both failures
4 Per-execution dedup misses a scheduler retry Done: key accepted, not only documented 7a77751bd Real-store tests across two executions; route and MCP tests
5 not_a_channel_turn guidance leads with recipient_email Done 7a77751bd test_ent700, test_2157
6 channel description Done 7a77751bd voice.test.ts
7 Claim release tested only on a fake guard Done 7a77751bd Real effect_guard and idempotency store, SQLite

Docs are in 92a715527.

Results: Tier 1 passed at this head, including pytest (head, seed 12345) and regression diff. Locally, 2387 affected unit tests passed and 1 failed: test_1310_auth_consolidation.py::test_admin_sites_admit_admin[sqlite-avatar.generate_default_avatars], the read-only /data failure this PR already lists as environmental. I did not re-run it on dev. MCP npm test passed 1151/1151 and tsc --noEmit is clean. PostgreSQL and live-stack tests were not run.

Choices to check

  • A suppressed note answers delivered: true, sent: false. delivered: false would set fallback_to_text and the agent would repeat the note in text.
  • Voice keys are separate from text keys, so a brief can be a voice note and a text under one name.
  • A key without an address is refused (422 from the model, and locally in the tool), which keeps the in-session path unchanged.

Limits on item 3

  • The adapters return None on a refusal and drop the provider's error code, so 63016 cannot be detected. The guidance names the 24-hour window as the usual cause, not the confirmed one. Exposing the code would mean an adapter change, which I did not make.
  • send_failed is recorded per execution and does not count against the rate budget. A new execution, or a keyed retry (a failed note does not spend the key), still pays for another synthesis. I took your "release only on delivery_error" option rather than the budget option.

Outside this review, not changed: proactive_message_service.send_message scrubs staged credential values from the body (ent#279), and neither voice path does, so a fetched value in a note's text would reach synthesis. In-session voice had this gap before this PR; the addressed path now shares it. Worth a follow-up.

Please re-review.

@vybe
vybe requested a review from obasilakis November 4, 2026 03:35
@vybe
vybe dismissed obasilakis’s stale review November 4, 2026 03:47

Dismissed at the operator's instruction to merge. Items 1-7 are addressed at 92a7155; see the item-by-item comment on this PR. Post-merge review is welcome.

@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: review items addressed at 92a7155, Tier 1 green; merging at the operator's instruction.

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