Skip to content

fix(workspace): a voice call sits where it happened, and the agent's next typed turn knows what was said (#2694) - #2697

Merged
vybe merged 2 commits into
devfrom
feature/2694-voice-call-thread-order
Sep 11, 2026
Merged

vybe merged 2 commits into
devfrom
feature/2694-voice-call-thread-order

Conversation

@vybe

@vybe vybe commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • The read side — the window, not the sort. The thread read counted the newest 100 ROWS; a 30-minute voice call is ~180 rows, so one call pushed every typed turn before it off the screen and the call block rendered as the head of the thread. The window is now counted in typed turns (db.get_portal_thread_window), the calls among them ride along whole, and a row ceiling reports itself as truncated (the chat says "Earlier messages in this chat aren't shown" instead of silently starting mid-call). The stored order was never wrong — reproduced with the real writers and verified on SQLite and PostgreSQL.
  • The reply poll pays for none of it. GET …/history?limit=N (1–50, row semantics) is the poll's narrow read; replyFromHistory finds the reply by identity (the newest typed reply's id), not by a count that a shifting window breaks. A spoken reply is never the typed one.
  • The turn side — what the live session never heard. A resumed turn is prefixed with the platform-written rows since the agent's newest typed reply (get_platform_rows_since_last_reply → _format_voice_delta); the cold replay (_format_history_context, rewritten) carries the same rows in the same form and the delta is never added to it (no double-send on a cold retry). One renderer: spoken rows labelled, the call's label as a bracketed platform marker (never You:), whitespace collapsed so a transcript line cannot forge a labelled line, one total 24k-char spoken budget (a whole 30-minute call fits) trimmed oldest-first with every cut named. The 12-rows-per-call counter is gone.
  • No reply lands mid-call, both directions. A call is refused (409) while a typed reply is in flight; a typed turn is refused (409, unbilled, retryable — new voice_call_active category) while a call is on, via a live-call marker set at call start and cleared by the bridge's close and the REST stop. Both reads fail open on a Redis outage.

Changes

  • src/backend/client_portal/db.py — ThreadWindow, get_portal_thread_window, get_platform_rows_since_last_reply, PORTAL_HISTORY_ROW_CEILING; id tiebreak on the existing read.
  • src/backend/client_portal/service.py — _context_lines / _format_history_context (rewritten) / _format_voice_delta, _SPOKEN_CONTEXT_MAX_CHARS, VOICE_DELTA_HEADER; portal_chat reads the window + the delta before persisting; get_history(limit=) + truncated; _refuse_turn_during_voice_call in both turn entries; voice_call_active category.
  • src/backend/client_portal/voice.py — 409 over an in-flight reply; live-call marker helpers, set at call start. src/backend/routers/voice.py — marker cleared in the bridge's finally and in /stop.
  • src/backend/client_portal/router.py, models.py — bounded limit query param; PortalHistory.truncated.
  • src/frontend/src/components/portal/portalUtils.js — latestTypedReply, replyBaseline, identity-based replyFromHistory. PortalConversation.vue — narrow poll read, identity baseline (also on reattach), truncated line (shares one class with the ent#523 system line — raw-colour ratchet flat). stores/clientPortal.js — fetchHistory(…, { limit }) + truncated.
  • Docs: workspace-voice-conversation.md (new section), workspace-absorbs-session.md, execution.md, workspace.md, requirements public-access.md FR-3b + runtimes.md, two learnings.md entries, docs/security-reports/cso-diff-2026-09-10-2694-* (no findings at the gate).

Test Plan

  • tests/unit/test_2694_voice_thread_window.py — real tables + real writers: typed→long call→typed→call→typed reads back as the timeline; window counts typed rows; ceiling keeps the newest and reports; dedup read keeps row semantics; tie determinism; delta cursor survives a failed typed turn; get_history window/narrow reads; route limit bounds.
  • tests/unit/test_2694_voice_delta_context.py — through the real portal_chat: resumed turn carries the call; cold turn carries it in the replay with no delta block; delta read before persist; read failure never blocks; budget across calls; label never You:; newline collapse; 409 over an in-flight reply after the uniform 404; 409 typed turn during a call; marker set/cleared/fails open.
  • Frontend: portalVoiceMode.spec.js (render order typed→call→typed→call→typed, truncated line, narrow poll), portalReplyRateable.spec.js (identity rule, window shift, spoken never mistaken, post-call null-id baseline).
  • Full suites: backend unit 15103 passed (the only failure is the pre-existing test_1920 guard on the enterprise submodule, unrelated); frontend npm run test:unit 2660 passed, raw-colour ratchet flat.
  • PostgreSQL: the three new statements run as-is against the local PG service with the expected rows (CI runs no text() SQL on PG; stated here).
  • Manual on dev: type → call (>15 min or a long thread) → type; the block sits between the typed turns after the call, after a reload, and after a chat switch; "so, what did we just agree on?" is answered from the call.

Reviewed by: /autoplan (strategy + engineering subagents) + /gemini-critic, /review (structural), /cso --diff. Root-cause premise (the row window) confirmed by the reporter at the plan gate.

Fixes #2694

🤖 Generated with Claude Code

https://claude.ai/code/session_01F2QYRXnk9XT8aFreKyk6na

…next typed turn knows what was said (#2694)

Two defects, one per side of the portal messages table.

The read side — the window, not the sort. The stored order was never wrong:
both writers stamp the same ISO-Z microsecond format and the read orders by
it. `get_history` returned the newest 100 ROWS, and a 30-minute voice call is
~180 rows, so one call filled the window, every typed turn before it fell off,
and the call block — anchored at the call's first row in the window — rendered
as the head of the thread. The window is now counted in TYPED turns
(`get_portal_thread_window`: the newest 100 for the endpoint, 20 for the cold
replay), the spoken rows of the calls among them ride along whole, and a row
ceiling (600, the newest survive) reports itself as `truncated` so the chat
can say "Earlier messages in this chat aren't shown" instead of silently
re-creating the symptom. Both statements order by `created_at DESC, id DESC`
(the uuid tiebreak is stable, not chronological); verified on SQLite and
PostgreSQL.

The reply poll pays for none of it. It now reads `?limit=N` (1–50, row
semantics) and finds the reply by identity — the newest typed assistant row's
id — instead of by a count that a shifting window breaks; a spoken reply is
never the typed one.

The turn side — what the live session never heard. A resumed turn dropped the
history replay (ent#358), which was right for typed turns and wrong for
spoken ones: the voice provider wrote the call straight into the thread and
the agent's own session was not there. A resumed turn is now prefixed with the
delta — the platform-written rows since the agent's newest typed reply
(`get_platform_rows_since_last_reply` → `_format_voice_delta`; the assistant
cursor survives a failed typed turn). The cold replay (`_format_history_context`,
rewritten) renders the same rows in the same form and the delta is never added
to it, so a cold retry cannot double-send. One renderer for both: spoken rows
labelled, the platform's own rows as bracketed markers (never `You:`),
whitespace collapsed so a transcript line cannot forge a labelled line, and
ONE total spoken budget (24k chars — a whole 30-minute call fits) trimmed
oldest-first across calls with every cut named. The 12-rows-per-call counter is
gone.

No reply lands mid-call, in both directions: a call is refused (409) while a
typed reply is in flight, and a typed turn is refused (409, unbilled,
retryable) while a call is on, through a live-call marker set at call start
and cleared by the bridge's close and the REST stop. Both reads fail open.

Tests: tests/unit/test_2694_voice_thread_window.py (real tables + real
writers), tests/unit/test_2694_voice_delta_context.py (through the real
portal_chat), frontend specs for the render order and the identity-based
reply picker; the ent#534 budget test and the #2320 raise-site table updated.
Docs: workspace-voice-conversation.md (new section), requirements FR-3b, the
workspace/execution area files, two learnings entries, a CSO diff report.

Fixes #2694

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01F2QYRXnk9XT8aFreKyk6na
@vybe

vybe commented Sep 11, 2026

Copy link
Copy Markdown
Contributor Author

merge-train 2026-09-10 (evening run): riding. No criticals; 173 backend + 119 frontend tests pass on the head, every new symbol has a live consumer, and the body's Fixes abilityai/trinity#2694 is normalised to Fixes #2694 so the status promotion actually fires (#2644 class).

One finding recorded as a follow-up rather than blocking, because the marker TTL already bounds it: #2700 — the live-call marker is set in POST /voice/start but only cleared by the WS bridge finally or /stop, and useVoiceSession.js ws.onerror resolves without calling stop(). A socket that never opens leaves every typed turn in that thread 409ing for up to ~32 minutes with nothing to end. Whether the fix is client-side stop-on-error or setting the marker at bridge connect is a design call, so it is filed rather than patched.

Also noted: the whole routers/voice.py diff (marker clear in voice_stop and in the bridge finally) is pinned by inspect.getsource only, and no spec mounts PortalConversation.vue, so historyTruncated / persistedReplyBaseline are asserted textually. Not counted against the PR — the repo has no mount harness — but it is where #2700's regression test should execute rather than match.

…l-thread-order

# Conflicts:
#	docs/memory/learnings.md
#	tests/registry.json

@trinity-ability trinity-ability 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/20260911-0925 (#2701)

@vybe
vybe merged commit 56fd9e3 into dev Sep 11, 2026
26 of 27 checks passed
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