From 18a4384903ba381169019a35aa492f17d459f214 Mon Sep 17 00:00:00 2001 From: Micio Dev Date: Fri, 20 Nov 2026 09:09:46 +0000 Subject: [PATCH 01/14] test(live): pin the failed-tick carry and cover the sweeper loop and maintenance gate --- server/src/live/sweeper.rs | 44 +++++++++++++++++++++ server/tests/live_billing.rs | 77 ++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/server/src/live/sweeper.rs b/server/src/live/sweeper.rs index 2f2c26e4..90f2a52b 100644 --- a/server/src/live/sweeper.rs +++ b/server/src/live/sweeper.rs @@ -159,3 +159,47 @@ pub async fn sweep_sessions(state: &AppState, only: Option<&[Uuid]>) -> Result AppState { + let cfg = crate::config::Config::test_with_billing("", "x".repeat(32).as_str(), 0.0); + AppState::new(cfg) + } + + #[tokio::test] + async fn without_a_database_a_sweep_and_the_maintenance_pass_do_nothing_and_do_not_fail() { + let s = state(); + assert!(s.pool.is_none()); + assert_eq!(sweep_once(&s).await.unwrap(), 0); + assert_eq!(run_maintenance(&s, None).await, (0, 0)); + } + + #[tokio::test] + async fn the_hourly_maintenance_runs_once_per_hour_however_often_the_sweep_calls_it() { + let s = state(); + // The first call claims the hour... + maintenance_if_due(&s).await; + // ...so the stamp is taken and a second claim in the same hour is refused. + assert!(!s.live.maintenance_due(MAINTENANCE_EVERY)); + // Calling it again is a harmless no-op. + maintenance_if_due(&s).await; + assert!(!s.live.maintenance_due(MAINTENANCE_EVERY)); + } + + #[tokio::test] + async fn the_sweeper_loop_does_its_first_pass_at_once_and_keeps_running() { + let s = state(); + let task = tokio::spawn(run(s.clone())); + // An interval ticks immediately: the first sweep and the maintenance claim happen now. + tokio::time::sleep(Duration::from_millis(150)).await; + assert!(!task.is_finished(), "the loop must not return"); + assert!( + !s.live.maintenance_due(MAINTENANCE_EVERY), + "the first pass claimed the hour" + ); + task.abort(); + } +} diff --git a/server/tests/live_billing.rs b/server/tests/live_billing.rs index 5a233236..3b60f789 100644 --- a/server/tests/live_billing.rs +++ b/server/tests/live_billing.rs @@ -638,3 +638,80 @@ async fn credits_of_a_duplicate_tick_go_back_to_the_carry() { let _ = (rows, dup_rows); assert!(spent >= 6, "credits of duplicate ticks were lost from the carry: only {spent} charged for ~7.2 credits of audio"); } + +/// A tick whose charge FAILS (database error) must hand the credits taken from the carry back, so +/// the next successful tick recovers them. Kills the "no put_back on Err" mutant. +#[tokio::test] +async fn credits_of_a_failed_tick_go_back_to_the_carry() { + let pool = pool!(); + let owner = make_user(&pool, usd(1.0)).await; + let org = make_org(&pool, owner, 1000).await; + let (session, peer, call) = make_session(&pool, None, Some(org)).await; + let live = Arc::new(LiveState::new(LiveConfig::disabled())); + let rooms = Arc::new(RoomManager::new()); + let room = format!("live-{call}"); + seat(&rooms, &room, &peer.to_string(), "it"); + seat(&rooms, &room, "the-other-phone", "es"); + // 1.2 credits per 1 s tick. + // The first ticks fail: a trigger rejects every tick insert of this session, then is dropped. + let fname = format!("fail_ticks_{}", session.simple()); + sqlx::query(&format!("CREATE FUNCTION {fname}() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'tick refused'; END $$")).execute(&pool).await.unwrap(); + sqlx::query(&format!("CREATE TRIGGER {fname} BEFORE INSERT ON live_usage_ticks FOR EACH ROW WHEN (NEW.session_id = '{session}') EXECUTE FUNCTION {fname}()")).execute(&pool).await.unwrap(); + let activity = Arc::new(AtomicU64::new(0)); + let (end_tx, _end_rx) = mpsc::unbounded_channel(); + let (cancel, cancel_rx) = oneshot::channel(); + let meter = LiveMeter { + pool: pool.clone(), + live: live.clone(), + rooms: rooms.clone(), + payer: LivePayer::Org { + org_id: org, + call_session: call, + }, + session, + peer, + room: room.clone(), + speaker_lang: "it".into(), + interval_secs: 1, + rate_per_second: 0.012, + scale_by_target_count: false, + low_balance_threshold: 0.0, + activity: activity.clone(), + }; + let run = tokio::spawn(meter.run(end_tx, cancel_rx)); + let feeder = { + let a = activity.clone(); + tokio::spawn(async move { + loop { + a.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + tokio::time::sleep(Duration::from_millis(50)).await; + } + }) + }; + tokio::time::sleep(Duration::from_millis(2500)).await; + sqlx::query(&format!("DROP TRIGGER {fname} ON live_usage_ticks")) + .execute(&pool) + .await + .unwrap(); + sqlx::query(&format!("DROP FUNCTION {fname}()")) + .execute(&pool) + .await + .unwrap(); + tokio::time::sleep(Duration::from_millis(4500)).await; + let _ = cancel.send(()); + run.await.unwrap(); + feeder.abort(); + let (rows, spent): (i64, i64) = sqlx::query_as("SELECT count(*), COALESCE(sum(-amount),0)::bigint FROM organization_credits_transactions WHERE session_id=$1 AND type='live_connect'") + .bind(call).fetch_one(&pool).await.unwrap(); + let dup_rows: i64 = sqlx::query_scalar( + "SELECT count(*) FROM live_usage_ticks WHERE session_id=$1 AND amount = 0", + ) + .bind(session) + .fetch_one(&pool) + .await + .unwrap(); + // ~7 intervals of 1.2 credits = 8.4 credits of cost; the three bucketed ticks that were + // duplicates must be recovered by a later tick, so close to floor(8.4)=8 is charged (6 s run: ~7.2) + let _ = (rows, dup_rows); + assert!(spent >= 8, "credits of failed ticks were lost from the carry: only {spent} charged for ~7.2 credits of audio"); +} From a7a87c655efb8ba284a21d6f0250e8835522aec4 Mon Sep 17 00:00:00 2001 From: Micio Dev Date: Fri, 20 Nov 2026 12:19:36 +0000 Subject: [PATCH 02/14] docs(odd): track live-connect multi-guest, tier choice and cloned voice --- odd/tasks/live-connect-multi-tiers.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 odd/tasks/live-connect-multi-tiers.md diff --git a/odd/tasks/live-connect-multi-tiers.md b/odd/tasks/live-connect-multi-tiers.md new file mode 100644 index 00000000..562ec549 --- /dev/null +++ b/odd/tasks/live-connect-multi-tiers.md @@ -0,0 +1,25 @@ +# Live Connect — more guests per link, tier choice, cloned voice + +Objective: Live Connect sessions with several guests on the same link (different languages allowed), a tier choice for the host (Standard / Premium / Enhanced), and the host's cloned voice for Enhanced. Branch: `feature/live-connect-multi-guest-tiers` (off develop). Delivery strategy: ask-on-risk; slices of about 400 lines, merged to develop by PR, released as 1.63.0. Test runner: `cargo test` (DB-gated suites need the local pgvector Postgres), `npx vitest` for the client. + +## Specs + +- S1 — Several guests on the same link. User: "non so se con lo stesso qr code possono partecipare più ospiti ma sarebbe da fare, anche con lingue diverse". One invitation (link/QR) admits up to `max_guests` guests (host chooses 1–3 at creation; default 1 keeps today's behaviour; hard cap 3 because rooms hold 4 peers). The invitation stays valid until the session ends or is full; a full session answers `session_full`. Each guest picks their own language; guests may share a language. +- S2 — Everybody hears everybody, each in their own language; every phone shows who is speaking (captions per speaker) and who is connected. No new turn-taking. +- S3 — Billing. User: "pagherà per se stesso l'host e per anche l'ospite" / "il calcolo ovviamente va gestito sul numero di lingue diverse che si parlano (come per i webinar più o meno)". The owner (host) pays for every seat. Each speaker's meter bills one stream per DISTINCT target language present (so two guests in the same language cost one stream, three languages cost three), at the session tier's rate. Nothing is charged when everyone shares the speaker's language. +- S4 — Tier choice. User: "la scelta del tier non l'ho vista, dove la faccio se voglio cambiare con enhanced o premium?". The host picks the tier when creating the session, reusing what Meet shows (`/api/engines`, cost note per tier). Persisted as `live_sessions.engine_id`; guests inherit it; the per-minute rate of the tier applies to every seat. Premium at capacity falls back to Standard with an `engine_downgraded` notice and is re-metered at the Standard rate. Desk (B2B) sessions stay Standard. +- S5 — Enhanced + the host's cloned voice. User: "come enhanced c'è anche la questione cloning della mia voce che va riutilizzato, vedi come abbiamo fatto per i meet". Reuse the Meet voice-clone flow (`users.cartesia_voice_id`, "Voice preparation"). The listener's phone speaks the translation with the speaker's cloned voice when the speaker has one (the host), the default Enhanced voice otherwise (guests have no account). No server-side Cartesia synthesis exists: the client-direct pieces follow the webinar pattern (TTS-only token authorised by the live token). The guest notice mentions voice cloning. Not available to guests as speakers of a clone and not on desks. +- S6 — Everything already built keeps working: 1:1 sessions, desks, billing idempotency, erasure, search, the echo filter (which must work with 3+ seats), the live spend line for the host. + +## Tasks + +- [ ] T1 · S1,S3,S6 · delegated (worktree) · server multi-guest core (migration 069, create/join, ws with N peers, per-peer mic levels for the echo filter, capacity, billing per distinct language) · commit: — +- [ ] T2 · S1,S2 · delegated · client multi-guest (host chooses guests, invite panel, partners list, captions per speaker) · commit: — +- [ ] T3 · S4 · delegated · tier choice Standard/Premium (create body, persist, ws engine resolve, capacity fallback, rate, selector UI with cost note, i18n 84 locales) · commit: — +- [ ] T4 · S5 · delegated · Enhanced + cloned voice (client-direct speaker STT, `translate_text` over /ws/live, TTS on the listener, TTS-only token, Voice preparation in the host setup, consent line) · commit: — + +## Log + +- L1 (2026-11-20): user, verbatim: "E poi la scelta del tier non l' ho vista, dove la faccio se voglio cambiare con enhanced o premium? Com enhanced c'è anche la questione cloning della mia voce che va riutilizzato, vedi come abbiamo fatto per i meet." +- L2 (2026-11-20): user, verbatim: "certo e tieni conto che pagherà per se stesso l' host e per anche l'ospite, non so se con lo stesso qr code possono partecipare più ospiti ma sarebbe da fare, anche con lingue diverse, lì il calcolo ovviamente va gestito sul numero di lingue diverse che si parlano (come per i webinar più o meno)" +- L3 (2026-11-20): mapping of Meet (Explore agent): Enhanced (`cartesia`) is client-direct and listener-side and works only under LISTENER_PAYS for signed-in listeners; the server has no Cartesia TTS/STT; the cloned voice is `users.cartesia_voice_id` (`has_voice_clone`), not "Vox Voices" (on-device Kokoro). Live Connect hard-wires `state.engines.default()` at `live/ws.rs` and never uses `live_sessions.engine_id`; `live_busy` is fatal on the client; Premium (`gemini_live_translate`) shares Standard's `start_session`/PCM/`TranslatedAudio`. Decisions taken without asking: cap 4 peers (host + 3 guests); desks Standard-only; Premium/Enhanced for signed-in hosts only. From 13b5492b4adda0bec95881509390efb4547c1a03 Mon Sep 17 00:00:00 2001 From: Micio Dev Date: Fri, 20 Nov 2026 12:45:06 +0000 Subject: [PATCH 03/14] feat(live): one host and up to three guests on the same invitation A Live Connect link used to admit exactly one guest. The host can now ask for `max_guests` 1-3 (default 1, `LIVE_MAX_GUESTS` lowers the ceiling) and the same link/QR admits guests one after the other, languages free. - migration 069: `live_sessions.max_guests` (guarded NOT VALID check); the guest-side unique index becomes a plain index under the same name so that re-running 065 stays a no-op. The owner-side unique index stays. - join_guest counts guests under FOR UPDATE on the session row, so N simultaneous joins admit exactly `max_guests`; `session_full` past that, the join window only closes a session nobody joined, and a join the engine cannot serve (each guest adds streams for every speaker) is refused `live_busy` inside the same transaction. - a guest hanging up in a multi-guest session leaves instead of ending it. - billing: every phone bills one stream per distinct target language, always, whatever the engine metadata says (the scale flag is gone from LiveMeter). - echo filter and latency probe: per-seat (4) mic levels, last-voiced and stamps; the pump resolves a speaker id to its seat; one utterance is one latency observation however many phones hear it. - dashboard list/detail: first guest plus `guest_count` instead of a scalar subquery that would 500 with several guests. - docs, spec and .env.example updated. --- docs/live-connect.md | 166 ++++-- server/.env.example | 5 + server/migrations/069_live_multi_guest.sql | 60 +++ server/src/live/billing.rs | 20 +- server/src/live/config.rs | 34 ++ server/src/live/dashboard.rs | 13 +- server/src/live/echo.rs | 8 +- server/src/live/mod.rs | 6 +- server/src/live/routes.rs | 100 +++- server/src/live/sessions.rs | 162 ++++-- server/src/live/state.rs | 163 ++++-- server/src/live/ws.rs | 69 ++- server/tests/live_admin.rs | 56 +++ server/tests/live_billing.rs | 188 ++++++- server/tests/live_dashboard.rs | 96 ++++ server/tests/live_desks.rs | 9 + server/tests/live_intelligence.rs | 152 ++++++ server/tests/live_rest.rs | 378 ++++++++++++++ server/tests/live_schema.rs | 104 +++- server/tests/live_summary.rs | 69 +++ server/tests/live_ws.rs | 556 +++++++++++++++++++++ specs/0121-live-connect/spec.md | 41 +- 22 files changed, 2265 insertions(+), 190 deletions(-) create mode 100644 server/migrations/069_live_multi_guest.sql diff --git a/docs/live-connect.md b/docs/live-connect.md index 915a6376..3ad5e216 100644 --- a/docs/live-connect.md +++ b/docs/live-connect.md @@ -1,6 +1,7 @@ # Live Connect -Two phones, two microphones, two independent translation streams at the same time. +A host and up to three guests on one link, each with a phone and a microphone, every phone an +independent translation stream at the same time (a conversation is a pair by default). Decisions and requirements: [`specs/0121-live-connect/spec.md`](../specs/0121-live-connect/spec.md). > **Status (2026-11-20): built, live behind `LIVE_CONNECT_ENABLED`, not yet verified on real devices.** Backend (sessions, @@ -19,15 +20,17 @@ Decisions and requirements: [`specs/0121-live-connect/spec.md`](../specs/0121-li ``` phone A ──PCM16 24 kHz──▶ ┐ ┌─▶ translated audio ──▶ phone B - │ /ws/live │ -phone B ──PCM16 24 kHz──▶ ┤ one private room ├─▶ translated audio ──▶ phone A - │ live- │ +phone B ──PCM16 24 kHz──▶ ┤ /ws/live ├─▶ translated audio ──▶ phone C +phone C ──PCM16 24 kHz──▶ ┤ one private room ├─▶ translated audio ──▶ phone D +phone D ──PCM16 24 kHz──▶ ┤ live- │ (each phone hears every OTHER + │ │ speaker, in its own language) └─▶ engine: one upstream session per speaker per target language (Standard/Qwen by default; capacity fallback as in meetings) ``` -No WebRTC. Each phone sends only its own microphone and receives only the **other** side's -translation. Rooms, engines, the usage meter, speech events and the org ledger are the existing +No WebRTC. Each phone sends only its own microphone and receives the translation of **every +other** speaker, in its own language. A conversation is the host plus 1 to 3 guests +(`max_guests`, default 1); the room holds four peers. Rooms, engines, the usage meter, speech events and the org ledger are the existing meeting machinery; Live Connect adds sessions, credentials and a payer who is not on the call. ## Session lifecycle @@ -35,17 +38,31 @@ meeting machinery; Live Connect adds sessions, credentials and a payer who is no ```mermaid stateDiagram-v2 [*] --> waiting: create (B2C) / scan (B2B desk) - waiting --> active: second side joins (guest, or staff answers) + waiting --> active: first guest joins (or staff answers a desk) waiting --> expired: join window / desk wait elapsed - active --> ended: either side ends, payer runs out, max duration, both gone past grace + active --> active: more guests join the same link, up to max_guests + active --> ended: host ends, payer runs out, max duration, everybody gone past grace ended --> [*] expired --> [*] ``` -- B2C: the owner is the `host`; the invited person is the `guest`. +- B2C: the owner is the `host`; the invited people are the `guest`s. - B2B desk: the customer who scanned is the `guest`; the member of staff who answers is `staff`. -- A session holds at most **two** participants, one per side. The database enforces it - (`live_participants` partial unique indexes), not only the application. + A desk conversation is always a pair. +- **One host (or staff) and up to `max_guests` guests** (1-3, chosen at create, default 1). The owner + side is still a database fact (a partial unique index: exactly one `host`/`staff`). The number of + guests is enforced **in code** (`sessions::join_guest`), because a unique index cannot say "at + most N": the session row is locked `FOR UPDATE` while the guests are counted and the new one is + inserted, so N simultaneous joins on one link admit exactly `max_guests`. +- **The link is reusable** while the session is `waiting` or `active`: each guest who opens it takes + the next seat, with their own participant row, token and consent. `LIVE_JOIN_WINDOW_SECS` applies + to a session nobody has joined yet; once it has a guest it is `active` and the link stays good + until the session ends or fills (`session_full`). A seat is never handed on: a guest who dropped + reconnects with the token they were given. +- A guest hanging up (`POST …/end` with a guest token) ends a **two-person** conversation as before, + but in a conversation that admits several guests it only means "I am leaving": the answer is + `{"ended": false, "left": true}` and the others carry on. The host's end is the end for everyone; + the sweeper still ends a conversation once the room has been empty for the reconnect grace. ## Credentials *(implemented: `server/src/live/tokens.rs`)* @@ -64,13 +81,13 @@ stateDiagram-v2 creates a new session**. - Rotating `LIVE_TOKEN_KEY` invalidates every printed desk QR and every guest token in flight. -## Data model *(implemented: migration `065_live_connect.sql`)* +## Data model *(implemented: migrations `065_live_connect.sql`, `069_live_multi_guest.sql`)* | Table | Purpose | |---|---| | `live_desks` | permanent QR entry points: org, optional project, name/location, default language, public handle, `token_version`, status | -| `live_sessions` | lifecycle, ownership (user **or** org), project and desk binding, expiry, `save_transcript`, link to `call_sessions` | -| `live_participants` | who is on the call; `peer_id` is the room peer id, server-minted | +| `live_sessions` | lifecycle, ownership (user **or** org), project and desk binding, expiry, `save_transcript`, `max_guests` *(069, 1-3, default 1)*, link to `call_sessions` | +| `live_participants` | who is on the call (one host/staff and up to three guests); `peer_id` is the room peer id, server-minted. `idx_live_participants_guest_side` is a plain index since 069 (see below) | | `live_usage_ticks` | one row per (session, peer, tick): the idempotency ledger for billing | | `live_session_events` | bounded diagnostics (join, leave, reconnect, error, capacity, echo, exhausted) | | `live_session_summaries` *(067)* | one AI summary per (conversation, language); the row is also the idempotency and concurrency guard of the paid call. Content: erased with the conversation | @@ -81,6 +98,15 @@ The conversation itself uses the existing `call_sessions` (`kind = 'live_connect `live_sessions.call_session_id` is `ON DELETE SET NULL`, so purging a conversation never erases the billing record. All tables are RLS default-deny; tenancy is enforced in Rust like everywhere else. +**Migration 069 (multi-guest).** Adds `live_sessions.max_guests SMALLINT NOT NULL DEFAULT 1` with a +guarded `CHECK (max_guests BETWEEN 1 AND 3) NOT VALID`; existing rows keep 1, which is what they +were. It turns the guest-side **unique** index into a plain one **under the same name**: migration 065 +says `CREATE UNIQUE INDEX IF NOT EXISTS idx_live_participants_guest_side`, and `IF NOT EXISTS` +matches by name, so re-running 065 (the schema suite does; a drifted database may) finds the name +taken and leaves it alone instead of rebuilding a unique index over a three-guest conversation and +failing. The owner-side unique index stays. Additive and idempotent; a build that predates it +simply keeps treating every session as a pair. + ## Configuration *(implemented: `server/src/live/config.rs`)* See the Live Connect block in `server/.env.example`. The feature is off unless @@ -88,7 +114,8 @@ See the Live Connect block in `server/.env.example`. The feature is off unless error, never a fallback. Two limits against abuse of the public surface are tunable: `LIVE_DESK_MAX_WAITING_PER_IP` (default 2: waiting sessions one client address may hold at one desk) and `LIVE_AUDIO_MAX_BYTES_PER_SEC` (default 72 000: the microphone budget of one phone, see -*`/ws/live`*). The echo filter has two knobs, `LIVE_ECHO_FILTER` (default on) and `LIVE_ECHO_SIM` +*`/ws/live`*). `LIVE_MAX_GUESTS` (default and ceiling 3, clamped to 1-3, junk keeps the default) is the +most guests a host may ask one link to admit; a room holds four peers, so it can only be lowered. The echo filter has two knobs, `LIVE_ECHO_FILTER` (default on) and `LIVE_ECHO_SIM` (default 0.6, clamped to 0.3-0.95; junk falls back to the default instead of switching the feature off), see *Echo filter* below. @@ -96,8 +123,8 @@ off), see *Echo filter* below. | Route | Who | Status | |---|---|---| -| `POST /api/live/sessions` | signed-in owner (B2C), or org member with `org_id` + `project_id` (B2B) | implemented | -| `POST /api/live/join` | anyone with an invite code (no account) | implemented | +| `POST /api/live/sessions` body `{lang, save_transcript?, org_id?, project_id?, max_guests?}` | signed-in owner (B2C), or org member with `org_id` + `project_id` (B2B) | implemented | +| `POST /api/live/join` body `{code, name, lang, consent}` | anyone with an invite code (no account); the same code admits guests one after the other | implemented | | `GET /api/live/sessions/{id}`, `POST …/end` | a participant, with its Live Connect token (`Authorization: Bearer`) | implemented | | `GET /ws/live?token=…` | a participant | implemented | | `DELETE /api/live/sessions/{id}` | the signed-in **owner** of a consumer conversation (account JWT); 404 for anyone else, 409 while open | implemented | @@ -112,7 +139,7 @@ off), see *Echo filter* below. | `POST /api/admin/live/{desk-revoke,desk-regenerate,session-end,session-reconcile,session-transcript}` | platform operators, through Directus Flows (shared `ADMIN_API_SECRET`, like every `/api/admin/*` route) | implemented, see *Administration (Directus)* | Errors are `{"code","message"}` with a stable code: `invite_not_found`, `invite_expired`, -`session_full`, `session_ended`, `consent_required`, `bad_lang`, `too_many_sessions`, +`session_full`, `session_ended`, `consent_required`, `bad_lang`, `bad_max_guests`, `too_many_sessions`, `insufficient_balance`, `insufficient_credits`, `subscription_required`, `project_required`, `project_not_found`, `live_busy`, `rate_limited`; summaries add `session_open`, `nothing_to_summarize` (both 409), `summary_unavailable` (502), `summary_in_progress` (409), `summary_not_found` (404). @@ -121,13 +148,25 @@ Errors are `{"code","message"}` with a stable code: `invite_not_found`, `invite_ - Creating needs an account that can pay. A business session also needs a project of **that** org (a foreign or archived project answers like a missing one), an active subscription and credit. + `max_guests` is optional (omitted or `null` = 1, a pair) and must be a whole number from 1 to + `LIVE_MAX_GUESTS`; anything else is `400 bad_max_guests` and creates nothing. It is stored, and + returned by create (`max_guests`), by join and by `GET …/sessions/{id}` (`session.max_guests`). - Joining needs consent, a real language (never `auto`) and a well-formed code; a malformed and an - unknown code answer identically. The guest seat is taken **once**; a second device is refused - (`session_full`) and the first reconnects with its token. + unknown code answer identically. Each guest takes a seat of their own; once `max_guests` are taken + the next one is refused (`409 session_full`, and it takes nothing) and a guest who dropped + reconnects with their token. - An owner holds at most `LIVE_MAX_CONCURRENT_PER_OWNER` open sessions; creates are serialised with an advisory lock so a burst cannot beat the cap. -- `live_busy` (503) is returned at create/join when the default engine has fewer than two free - upstream sessions: Standard never reports `AtCapacity`, it silently skips a direction. +- `live_busy` (503) is returned at create when the default engine has fewer than two free upstream + sessions, and at join when it cannot open what the new guest's arrival needs: Standard never + reports `AtCapacity`, it silently skips a direction. Every participant opens one upstream session + per distinct OTHER language in the room, so a guest adds their own streams plus one for their + language to every speaker already there (host `it`, guest `es`, new guest `en`: 6 sessions where + there were 2, i.e. 4 more; a guest in a language already present adds only their own). A join + needs at least the two a pair has always needed and the engine's free sessions to cover that; + otherwise it is refused `live_busy` **inside the same transaction** that would have seated the + guest, so nobody is seated into a direction that would stay silent. A snapshot, not a + reservation. ### `/ws/live` @@ -136,6 +175,11 @@ Errors are `{"code","message"}` with a stable code: `invite_not_found`, `invite_ `live-`, private, and unguessable. - Client → server: `start`, `stop`, `mute_audio{muted}` and binary PCM16 mono 24 kHz frames. Mute is **enforced server-side**: a muted phone sends nothing to the engine. +- A room of up to four phones: each phone is told who is already there (`room_joined.peers`), who + arrives (`peer_joined`), who leaves (`peer_left`) and who mutes (`peer_muted`, per peer). A + reconnect with the same token replaces the stale socket (no `peer_left`). Captions and + `translated_audio` carry the `speaker_id` of whoever spoke and arrive in the listener's language; + two phones that share a language hear each other natively (nothing is translated, nothing billed). - Server → client: the existing meeting frames (`room_joined`, `peer_joined`, `peer_left`, `subtitle_*`, `translated_audio`, `peer_muted`, `balance_*`, `capture_format`) plus `session_state{status, reason}` before the socket closes, and `error{code}`: @@ -154,7 +198,7 @@ Errors are `{"code","message"}` with a stable code: `invite_not_found`, `invite_ one. Drops are reported at most once a second as a `diagnostics` event (`{audio_dropped:{frames,bytes,oversized}}`, counts only) and at most 10 times per socket. The budget assumes the 24 kHz capture the client uses; a client capturing at 48 kHz would be throttled. -- Each phone re-checks the session every 3 s, so an end by the other phone, the sweeper, an admin or +- Each phone re-checks the session every 3 s, so an end by the host, the sweeper, an admin or the hard ceiling reaches it with the reason. - Persistence follows policy: a business session always has its `call_sessions` row (`kind='live_connect'`, org and project set); a consumer one only when the owner opted in to saving. @@ -220,9 +264,9 @@ Live Connect conversations are not counted there; adding them is a product decis ## Billing *(implemented: `server/src/live/billing.rs`)* -The owner pays for **both** directions: a user's personal balance (B2C) or the org wallet (B2B). The -guest is never charged, needs no credit, and never sees the owner's balance (balance frames go to the -owner's socket only). There is no new plan, tier or wallet: the meter reuses `billing::deduct_usage_tx` +The owner pays for **every seat**: a user's personal balance (B2C) or the org wallet (B2B). Guests are +never charged, need no credit, and never see the owner's balance (balance frames go to the owner's +socket only). There is no new plan, tier or wallet: the meter reuses `billing::deduct_usage_tx` and `business::credits::deduct_org_credits_tx`. The backend is the only authority; nothing the client reports is trusted. @@ -235,6 +279,16 @@ reports is trusted. - **One meter per phone**, started with that phone's `start` and cancelled by `stop`, mute, or the socket closing. Each tick covers `LiveMeter.interval_secs` (`USAGE_UPDATE_INTERVAL`, default 5 s). +- **Per DISTINCT target language, as for webinars.** A phone's tick bills one stream for every + distinct language present in the room other than its own speaker's, at the session tier's rate, + and nothing when everyone shares the speaker's language. The rule is Live Connect's own and does + **not** read the engine's `cost_scales_per_language` (the upstream cost of a seat is per speaker + per target language whatever the tier's metadata says). Host `it` with guests `es`, `es`, `en`: + the host's tick is 2 streams (es, en), a Spanish guest's 2 (it, en), the English guest's 2 + (it, es). Two guests in the same language never double a cost (host `it`, guests `es`, `es`, + `es`: every seat 1 stream); four languages cost 3 streams per seat. The idempotency key + `(session, peer, tick_seq)`, the opening charge and the per-seat org carry are unchanged: they + were already per peer. - **A charge and its `live_usage_ticks` row commit together**, keyed `(session, peer, tick_seq)` with `ON CONFLICT DO NOTHING`. The tick number comes from the wall clock, so two meters covering the same interval (a phone that reconnects before its old socket is torn down) agree and only one charge @@ -254,7 +308,7 @@ reports is trusted. (less than one cent per seat) is not charged, exactly as for one uninterrupted run. Two meters of one seat covering the same interval add its cost once. The carry is in memory (the server is single-replica, see *Operations*); a restart forgets less than a credit per open seat. -- **Insufficient credit.** When a tick cannot be covered nothing is debited, both phones get +- **Insufficient credit.** When a tick cannot be covered nothing is debited, every phone gets `balance_exhausted` and then `session_state{ended, balance_exhausted}`, and the session ends cleanly. A start is refused up front (`insufficient_balance` / `insufficient_credits`) when the payer cannot cover it. Six consecutive failed charges end the conversation (`billing_error`) @@ -364,9 +418,12 @@ and leaves the events, which this pass then erases. phones reconnect within `LIVE_RECONNECT_GRACE_SECS`. The sweeper reads presence from this process, so a second replica would end conversations whose phones are connected to the other one. - Capacity: the Standard engine is capped at 32 upstream sessions per process - (`QWEN_REALTIME_MAX_SESSIONS`), and one conversation opens two (one per speaking direction), so - about 16 simultaneous pairs. Standard never reports `AtCapacity`, it silently skips a direction, - so the gate is `engine_has_room` at create/join (`live_busy`, 503). It cannot see sessions that + (`QWEN_REALTIME_MAX_SESSIONS`), and a pair opens two (one per speaking direction), so about 16 + simultaneous pairs; a bigger conversation needs one per speaker per distinct other language (host + `it` with guests `es`, `es`, `en` is up to 8 while all four talk, four languages 12). Standard + never reports `AtCapacity`, it silently skips a direction, so the gate is `engine_has_room` at + create (two free) and the same check inside the join transaction against what the new guest adds + (`live_busy`, 503). It cannot see sessions that have been created but are not speaking yet, so a burst past ~16 pairs shows up as phones that never hear a direction, not as `live_busy`. `loadtest/live-connect.js` counts both. - Metrics (`/metrics`, unauthenticated aggregate, no ids): `voxtranslate_live_translation_ms`, @@ -384,9 +441,12 @@ many audio chunks answer it. gaps in the frames to detect. A frame is "voiced" when its mean absolute sample is at least 80 of 32 768 (about -44 dBFS); an utterance starts at the first voiced frame after a `start`/unmute or after one second without one. One pass over a 4.8 kB frame, no allocation. -- *Probe.* A per-session pair of atomics (`SessionLatency`): the speaker's socket stamps its seat at - onset, the other seat's writer takes the stamp when it sends the first `translated_audio` and - records the difference. No locks on the hot path. A stamp older than 30 s is dropped, and a newer +- *Probe.* A per-session set of atomics (`SessionLatency`), one slot per **seat**, four at most: each + socket claims a slot for its peer id when it connects (the same one again on a reconnect). The + speaker's socket stamps its slot at onset; whichever listener's writer first sends a + `translated_audio` of that speaker (the frame names its `speaker_id`) takes the stamp and records + the difference, so one utterance is one observation however many phones hear it. No locks on the + hot path. A stamp older than 30 s is dropped, and a newer onset overwrites an older one whose audio never came (same-language speech is not translated). - *What it excludes.* Everything on the phones: capture buffering, the network to and from the server, playback start. It also includes the provider's own latency, so with the mock upstream it @@ -439,15 +499,20 @@ clearly louder than the speaker's own** (smoothed mean absolute sample of the au feeds the engine, ratio above 5/4; equal or unknown levels never suppress). Of any two seats at most one can be the louder. It also means a seat that is talking holds the other's new utterance until its own caption has arrived (still at most 700 ms), because the echo can be heard before this -seat's caption. Today the pump knows two microphones ("me" and "the other phone"); a conversation -with more seats needs a per-peer level (`SessionLatency` keeps one pair), which is the only -piece that does not yet scale: `echo.rs` already asks by speaker id. +seat's caption. The pump answers the filter's level question **per peer**: every speaker id on a frame +is a peer id, each peer's socket holds a seat in the conversation's `SessionLatency` (up to four, +each with its own smoothed microphone level and last-voiced time), and a speaker whose phone has not +connected reads as 0, which is "unknown" and never suppresses. So with a host and two guests side +by side, guest 2 is not handed guest 1's reading of the host's voice either (the host's microphone is +the louder one), which a two-microphone lookup could not tell. **What is verified, and what is not.** Verified: the similarity rules and the state machine (unit tests in `echo.rs`), the config knobs, the metric, the diagnostics event, and the whole path through the real socket and the mock engine (`tests/live_ws.rs`: the echo never reaches the speaker, the -other phone still gets the translation, a real reply still gets through, kill switch); the 3-seat cases (guest B hearing guest A, two -simultaneous real speakers, a reply sharing a few words with someone else) are unit-level. **Not +other phone still gets the translation, a real reply still gets through, kill switch; and with a +host and two guests the echo reaches neither the host nor the other guest); the other 3-seat +cases (two simultaneous real speakers, a reply sharing a few words with someone else) are +unit-level. **Not verified: anything acoustic or about the real provider.** In particular (a) what Qwen's ASR returns for Italian forced as Spanish, how close it is to the real words, and whether it returns `original` early enough, (b) the relative loudness of two real phones (browser auto-gain and noise @@ -488,8 +553,9 @@ the dashboard session detail shows the timeline to org admins. For billing, the | Feature unreachable / `/live` says unavailable | `LIVE_CONNECT_ENABLED` truthy and `LIVE_TOKEN_KEY` ≥ 32 chars; the server logs why it stayed off (`live connect misconfigured`). Billing must also be configured (`DATABASE_URL`, `GOOGLE_CLIENT_ID`, `JWT_SECRET`) | | Every printed desk QR stopped working | was `LIVE_TOKEN_KEY` rotated? was the desk regenerated or revoked? (`live_desks.status`, `token_version`) | | Guest gets `invite_expired` | the invitation outlived `LIVE_JOIN_WINDOW_SECS` before anyone joined; create a new one | -| Guest gets `session_full` | the single guest seat is taken (the first device can reconnect with its token); a second device needs a new invitation | -| Create/join answers `live_busy` (503) | fewer than two free upstream sessions: check `QWEN_REALTIME_MAX_SESSIONS`, the Qwen connect metrics and how many conversations are active | +| Guest gets `session_full` | the link's `max_guests` seats are all taken (`SELECT count(*) FROM live_participants WHERE session_id = '' AND role = 'guest'` against `live_sessions.max_guests`); a guest who dropped reconnects with their token, anyone else needs the host to create a new conversation with more seats (1-3, `LIVE_MAX_GUESTS`) | +| Host's `max_guests` is refused `bad_max_guests` | not a whole number from 1 to `LIVE_MAX_GUESTS` (default 3) | +| Create/join answers `live_busy` (503) | create: fewer than two free upstream sessions; join: fewer than the new guest's arrival needs (each guest adds their own streams plus their language to every speaker). Check `QWEN_REALTIME_MAX_SESSIONS`, the Qwen connect metrics and how many conversations are active | | One direction is silent | the engine skipped it at capacity, or its upstream session dropped (`audio_session_closed` / `engine_unavailable` frames, retried by the client a few times); check `voxtranslate_qwen_connect_ms` and the server log for `qwen connect failed` | | `insufficient_balance` / `insufficient_credits` | the **owner's** balance or the org wallet, not the guest's: top up, or check the org's subscription | | Conversation ended on its own | `live_sessions.end_reason`: `balance_exhausted`, `max_duration`, `abandoned` (both phones gone past the grace), `expired`, `desk_revoked`/`desk_regenerated`, `ended_by_admin`, or an `ended_by_*` | @@ -499,6 +565,7 @@ the dashboard session detail shows the timeline to org admins. For billing, the | A finished conversation is not in search | `transcripts` row present? (`transcript_status = 'ready'` on its latest `call_sessions` row.) No embedding provider or a provider error leaves a transcript without chunks: run `/internal/embeddings/backfill`. A conversation with no speech is never finalised | | Guest words survive an erase | only rows named by the conversation's `room_session` events and pointer are deleted, and only of `kind='live_connect'` in room `live-`; check `live_session_events` for `content_erased` | | Retention stopped | it runs from the sweeper, which runs only while `LIVE_CONNECT_ENABLED` is truthy | +| A seat is charged more (or less) than expected | each seat bills one stream per distinct language in the room other than its speaker's: `SELECT peer_id, amount FROM live_usage_ticks WHERE session_id = ''`; two guests in one language are one stream for everybody else | | Billing mismatch | Reconcile: `missing_ticks` (a tick without a debit), `duplicate_ticks` (a debit no tick accounts for), `unlinked` (the meeting rows were erased, nothing can be claimed) | | DB-gated tests "pass" in milliseconds | they skipped: the database is missing or lacks pgvector — use `docker compose up -d --wait postgres` and `voxtranslate_voip_test` | @@ -607,14 +674,14 @@ The decision record is in the spec (*Key decisions* 1-3). In short: | Suite | Where | What it pins | Run it | |---|---|---|---| -| Schema | `server/tests/live_schema.rs` | RLS, owner check, vocabularies, two-seat cap, tick idempotency, purge keeps usage, migration re-run | `cargo test --test live_schema` | +| Schema | `server/tests/live_schema.rs` | RLS, owner check, vocabularies, one owner-side seat and no database cap on guests, `max_guests` check and default, tick idempotency, purge keeps usage, migration re-run (069 included, and 065 after it) | `cargo test --test live_schema` | | Tokens / config | unit tests in `live/{tokens,config}.rs` | forgery, expiry, audience, JWT confusion, desk MAC version binding, flag/key/limit validation | `cargo test --lib live::` | -| REST | `tests/live_rest.rs` | create/join/get/end, caps, consent, project validation, error codes | `cargo test --test live_rest` | -| `/ws/live` | `tests/live_ws.rs` | both phones translated at once, server-side mute, reconnect takeover, sweeper, billing ticks and exhaustion, no account id leak, latency metric, audio frame cap and 1.5x real-time budget (honest sender untouched) | `cargo test --test live_ws` | -| Billing | `tests/live_billing.rs` | idempotent ticks, insufficient funds, concurrent sessions on one wallet, the org carry across Start/Stop cycles | `cargo test --test live_billing` | +| REST | `tests/live_rest.rs` | create/join/get/end, caps, consent, project validation, error codes; several guests per link (create validation, `session_full` at the limit, simultaneous joins admit exactly `max_guests`, reusable link and join window, guest leaving, `live_busy` on a join the engine cannot serve) | `cargo test --test live_rest` | +| `/ws/live` | `tests/live_ws.rs` | both phones translated at once, four phones in three languages hearing every other speaker in their own, peer joined/left/muted per peer, server-side mute, reconnect takeover, sweeper, billing ticks and exhaustion, no account id leak, latency metric (one observation per utterance with three listeners), billing per distinct language across seats, the 3-seat echo filter, audio frame cap and 1.5x real-time budget (honest sender untouched) | `cargo test --test live_ws` | +| Billing | `tests/live_billing.rs` | idempotent ticks, insufficient funds, concurrent sessions on one wallet, the org carry across Start/Stop cycles, one stream per distinct language for every seat (user and org, to the last digit) | `cargo test --test live_billing` | | Desks | `tests/live_desks.rs` | scan creates a new session each time, revoke/regenerate, exactly one answerer, queue and per-address limits, scan-vs-revoke ordering, no existence oracle, a real WS conversation | `cargo test --test live_desks` | -| Intelligence | `tests/live_intelligence.rs` | finalisation, search filters and scope, retention, erasure | `cargo test --test live_intelligence` | -| Dashboard / admin | `tests/live_dashboard.rs`, `tests/live_admin.rs` | list/detail/usage scope, admin routes and audit | `cargo test --test live_dashboard --test live_admin` | +| Intelligence | `tests/live_intelligence.rs` | finalisation, search filters and scope, retention, erasure (also for a conversation with three guests) | `cargo test --test live_intelligence` | +| Dashboard / admin | `tests/live_dashboard.rs`, `tests/live_admin.rs` | list/detail/usage scope (first guest and `guest_count` for a conversation with several), admin routes and audit | `cargo test --test live_dashboard --test live_admin` | | Client unit | `client/src/scripts/live/*.test.ts` | state machine, protocol, view, pages (fake socket and fake media) | `cd client && npx vitest run --coverage.enabled=false` | | **Browser e2e, real server** | `client/e2e/live.spec.ts` | the user journeys below, in real browsers against the real server and database | `cd client && npm run test:e2e:live` | | Load | `loadtest/live-connect.js` | connect time, errors, `live_busy`, silent directions at N conversations | `loadtest/README.md`, "Live Connect" | @@ -674,9 +741,12 @@ is idempotent (`IF NOT EXISTS`, guarded constraints, safe to re-run): | 066 | `live_participants.user_id` becomes `ON DELETE SET NULL` | only acts while the foreign key still cascades; keeps the tick ledger when an account is deleted | | 067 | `live_session_summaries` (one AI summary per conversation and language) | content: removed with the conversation | | 068 | `live_desk_usage` view and `live_desk_usage_daily` table | metadata only; exists so Directus (which ignores composite-key tables) can chart credits per desk | +| 069 | `live_sessions.max_guests` (1-3, default 1) and the guest-side index made non-unique | additive column, guarded `NOT VALID` check. The one thing it removes is the database's old cap of one guest per session (the application enforces `max_guests` now), so **a server binary that predates it, run against a migrated database, would let a second guest in**: roll the schema forward with the binary, do not run an old build on a 069 database while sessions admit several guests. Existing sessions keep `max_guests = 1` | -All additive: an older server binary ignores the new tables, so a rollback of the application does -not require a rollback of the schema. There is no destructive migration to undo. +All additive but for that last point: an older server binary ignores the new tables, so a rollback of +the application does not require a rollback of the schema, and there is no destructive migration to +undo (the guest-side index it relaxes can be made unique again only while no session has several +guests). **Constraints.** A single replica (rooms and presence are in memory; see *Operations*). Rotating `LIVE_TOKEN_KEY` invalidates printed desk QR codes and guest tokens in flight, so treat it as a diff --git a/server/.env.example b/server/.env.example index 41ff36cd..3f828c92 100644 --- a/server/.env.example +++ b/server/.env.example @@ -346,6 +346,11 @@ ORG_PRICE_ENTERPRISE_ANNUAL=price_... # LIVE_MAX_SESSION_MIN=120 # Open sessions one signed-in owner may have at the same time (default 3). # LIVE_MAX_CONCURRENT_PER_OWNER=3 +# Most guests one invitation (one link / QR) may admit, 1-3 (default 3; a room holds four peers, so +# 3 is the ceiling). The host picks 1..this at creation (`max_guests`, default 1 = a two-person +# conversation). The owner pays for every seat, per distinct language spoken. Out of range is +# clamped, junk keeps the default. +# LIVE_MAX_GUESTS=3 # Seconds a dropped phone has to come back before its seat is released (default 60). # LIVE_RECONNECT_GRACE_SECS=60 # Seconds to wait after a conversation ends before its transcript is assembled for search, so the diff --git a/server/migrations/069_live_multi_guest.sql b/server/migrations/069_live_multi_guest.sql new file mode 100644 index 00000000..278a8f13 --- /dev/null +++ b/server/migrations/069_live_multi_guest.sql @@ -0,0 +1,60 @@ +-- Live Connect (spec 0121): one host, up to three guests on the SAME invitation. +-- +-- A conversation used to be exactly two seats. The invitation was single-use and a partial unique +-- index on `live_participants` allowed one guest row per session (`idx_live_participants_guest_side`, +-- migration 065). Now the host picks how many guests the link admits (1-3; three because a room +-- holds four peers) and the limit is enforced IN CODE, under a row lock on the session, because a +-- unique index cannot express "at most N". +-- +-- live_sessions.max_guests how many guests this session admits. Existing rows keep 1, which is +-- exactly what they were: nothing about a conversation already in +-- progress changes. +-- idx_live_participants_guest_side no longer UNIQUE (see below). The owner-side unique index +-- stays: a conversation still has exactly one host (or one staff member). +-- +-- Additive and idempotent: `ADD COLUMN IF NOT EXISTS`, a guarded NOT VALID constraint in its own +-- DO block, an index dropped only while it is still the unique one; every ALTER is guarded with +-- `to_regclass`. Other checkouts share a development database, so nothing here may fail when run +-- twice or when run by a build that does not know about the column. +DO $$ +BEGIN + IF to_regclass('public.live_sessions') IS NOT NULL THEN + ALTER TABLE live_sessions ADD COLUMN IF NOT EXISTS max_guests SMALLINT NOT NULL DEFAULT 1; + END IF; +END +$$; + +DO $$ +BEGIN + IF to_regclass('public.live_sessions') IS NOT NULL + AND NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'live_sessions_max_guests_check') THEN + ALTER TABLE live_sessions ADD CONSTRAINT live_sessions_max_guests_check + CHECK (max_guests BETWEEN 1 AND 3) NOT VALID; + END IF; +END +$$; + +-- The guest-side UNIQUE index goes. It is replaced by a plain index under the SAME name, on purpose: +-- `065_live_connect.sql` says `CREATE UNIQUE INDEX IF NOT EXISTS idx_live_participants_guest_side`, +-- and `IF NOT EXISTS` matches by name, so re-running 065 (the schema suite does, to prove it is +-- idempotent; a drifted database may) finds the name taken and does nothing, instead of rebuilding +-- the unique index and failing on a conversation that has two guests. The plain index also serves +-- the capacity count (`guests in this session`). +DO $$ +BEGIN + IF to_regclass('public.live_participants') IS NOT NULL THEN + IF EXISTS ( + SELECT 1 + FROM pg_class c + JOIN pg_index i ON i.indexrelid = c.oid + WHERE c.relname = 'idx_live_participants_guest_side' + AND c.relnamespace = 'public'::regnamespace + AND i.indisunique + ) THEN + DROP INDEX public.idx_live_participants_guest_side; + END IF; + CREATE INDEX IF NOT EXISTS idx_live_participants_guest_side + ON live_participants (session_id) WHERE role = 'guest'; + END IF; +END +$$; diff --git a/server/src/live/billing.rs b/server/src/live/billing.rs index d7cf6548..494b015f 100644 --- a/server/src/live/billing.rs +++ b/server/src/live/billing.rs @@ -1,9 +1,11 @@ //! Billing for Live Connect: who pays, and how a charge can never happen twice. //! -//! The **owner** pays for both directions — a user's personal balance (B2C) or the org wallet -//! (B2B). The guest is never charged and never sees the owner's balance. Every translated -//! direction is its own meter (one per phone), exactly as a two-person Talk session bills two -//! streams. +//! The **owner** pays for every seat — a user's personal balance (B2C) or the org wallet (B2B). +//! Guests are never charged and never see the owner's balance. Every phone is its own meter, and +//! each meter bills one stream per DISTINCT target language present in the room other than the +//! speaker's own, as a webinar does: host `it` with guests `es`, `es`, `en` pays two streams for +//! the host's speech (es, en), two for a Spanish guest (it, en), two for the English one (it, es); +//! two guests in one language never double a cost, and a room that all speaks one language is free. //! //! ## Why a tick can only be charged once //! @@ -185,7 +187,6 @@ pub struct LiveMeter { pub speaker_lang: String, pub interval_secs: u64, pub rate_per_second: f64, - pub scale_by_target_count: bool, pub low_balance_threshold: f64, /// Audio frames this socket has forwarded to the engine since the last tick. What is billed /// is audio that actually reached the engine, not a flag the client can flip: gating on mute @@ -231,10 +232,13 @@ impl LiveMeter { if !is_first && forwarded == 0 { continue; } - // Nothing to translate while everyone shares this speaker's language. + // Nothing to translate while everyone shares this speaker's language. Otherwise + // one stream per DISTINCT target language present, whatever the engine's + // metadata says about its own pricing: the upstream cost of a Live Connect + // phone is per speaker per target language, and the host pays for every seat + // (two guests in the same language cost one stream, not two). let targets = self.rooms.get_room_languages(&self.room, &self.peer.to_string()); - let Some(streams) = - billable_streams(&targets, &self.speaker_lang, self.scale_by_target_count) + let Some(streams) = billable_streams(&targets, &self.speaker_lang, true) else { continue; }; diff --git a/server/src/live/config.rs b/server/src/live/config.rs index 568af584..d7669c05 100644 --- a/server/src/live/config.rs +++ b/server/src/live/config.rs @@ -27,6 +27,11 @@ const DEFAULT_AUDIO_MAX_BYTES_PER_SEC: i64 = 72_000; const DEFAULT_MAX_SESSION_MIN: i64 = 120; /// Open sessions one signed-in owner may have at once. const DEFAULT_MAX_CONCURRENT_PER_OWNER: i64 = 3; +/// Guests one invitation may admit, at most. A room holds four peers (`rooms::MAX_PEERS`): the +/// host plus three guests. The hard ceiling; `LIVE_MAX_GUESTS` can only lower it. +pub const MAX_GUESTS_CEILING: i64 = 3; +/// Default for `LIVE_MAX_GUESTS`: what a host may ask for at most. +const DEFAULT_MAX_GUESTS: i64 = MAX_GUESTS_CEILING; /// How long a dropped phone has to come back before its seat is given up. const DEFAULT_RECONNECT_GRACE_SECS: i64 = 60; /// Days an ended business conversation's content is kept when its org sets no `retention_days`. @@ -84,6 +89,9 @@ pub struct LiveConfig { pub audio_max_bytes_per_sec: i64, pub max_session_min: i64, pub max_concurrent_per_owner: i64, + /// Most guests a host may ask one invitation to admit (1..=[`MAX_GUESTS_CEILING`]). A session + /// created without `max_guests` still takes one. + pub max_guests: i64, pub reconnect_grace_secs: i64, /// Retention for business conversations whose org has no `retention_days` of its own. pub retention_default_days: i64, @@ -110,6 +118,7 @@ impl LiveConfig { audio_max_bytes_per_sec: DEFAULT_AUDIO_MAX_BYTES_PER_SEC, max_session_min: DEFAULT_MAX_SESSION_MIN, max_concurrent_per_owner: DEFAULT_MAX_CONCURRENT_PER_OWNER, + max_guests: DEFAULT_MAX_GUESTS, reconnect_grace_secs: DEFAULT_RECONNECT_GRACE_SECS, retention_default_days: DEFAULT_RETENTION_DAYS, b2c_retention_days: DEFAULT_B2C_RETENTION_DAYS, @@ -172,6 +181,11 @@ impl LiveConfig { 1, 100, )?, + // A capacity knob must never take the feature down either: junk falls back to the + // default and a value outside 1..=3 is clamped (a room cannot hold more than four). + max_guests: get("LIVE_MAX_GUESTS") + .and_then(|v| v.trim().parse::().ok()) + .map_or(DEFAULT_MAX_GUESTS, |n| n.clamp(1, MAX_GUESTS_CEILING)), reconnect_grace_secs: number( "LIVE_RECONNECT_GRACE_SECS", DEFAULT_RECONNECT_GRACE_SECS, @@ -271,6 +285,25 @@ mod tests { assert!(matches!(err, ConfigError::BadKey(_))); } + #[test] + fn the_guest_limit_is_clamped_to_what_a_room_holds_and_junk_falls_back() { + let load = |v: &str| { + LiveConfig::load(env(&[ + ("LIVE_CONNECT_ENABLED", "1"), + ("LIVE_TOKEN_KEY", KEY), + ("LIVE_MAX_GUESTS", v), + ])) + .unwrap() + .max_guests + }; + assert_eq!(load("2"), 2); + assert_eq!(load(" 1 "), 1); + // Out of range is clamped, never an error and never above what a room holds. + assert_eq!((load("0"), load("-4"), load("4"), load("99")), (1, 1, 3, 3)); + // Junk keeps the default instead of switching the feature off. + assert_eq!((load("many"), load("")), (3, 3)); + } + #[test] fn defaults_apply_and_overrides_are_range_checked() { let cfg = LiveConfig::load(env(&[ @@ -281,6 +314,7 @@ mod tests { assert!(cfg.enabled && cfg.keys.is_some()); assert_eq!(cfg.max_session_min, 120); assert_eq!(cfg.max_concurrent_per_owner, 3); + assert_eq!(cfg.max_guests, 3); assert_eq!(cfg.desk_wait_secs, 120); assert_eq!(cfg.desk_max_waiting_per_ip, 2); assert_eq!(cfg.audio_max_bytes_per_sec, 72_000); diff --git a/server/src/live/dashboard.rs b/server/src/live/dashboard.rs index 74c4da84..36d1d77e 100644 --- a/server/src/live/dashboard.rs +++ b/server/src/live/dashboard.rs @@ -93,6 +93,7 @@ struct SessionRow { project_name: Option, guest_name: Option, guest_lang: Option, + guest_count: i64, staff_name: Option, duration_secs: Option, credits: i64, @@ -112,7 +113,10 @@ impl SessionRow { "host_lang": self.host_lang, "desk": self.desk_id.map(|id| json!({ "id": id, "name": self.desk_name, "location_label": self.location_label })), "project": self.project_id.map(|id| json!({ "id": id, "name": self.project_name })), + // The first guest to join, and how many there are: a conversation can hold up to + // three, and a column of a table has room for one name. "guest": json!({ "name": self.guest_name, "lang": self.guest_lang }), + "guest_count": self.guest_count, "staff_name": self.staff_name, "credits": self.credits, "has_transcript": self.has_transcript, @@ -124,8 +128,11 @@ const SESSION_SELECT: &str = " SELECT s.id, s.status, s.created_at, s.started_at, s.ended_at, s.end_reason, s.host_lang, s.desk_id, d.name AS desk_name, d.location_label, s.project_id, p.name AS project_name, - (SELECT display_name FROM live_participants WHERE session_id = s.id AND role = 'guest') AS guest_name, - (SELECT lang FROM live_participants WHERE session_id = s.id AND role = 'guest') AS guest_lang, + (SELECT display_name FROM live_participants WHERE session_id = s.id AND role = 'guest' + ORDER BY joined_at, peer_id LIMIT 1) AS guest_name, + (SELECT lang FROM live_participants WHERE session_id = s.id AND role = 'guest' + ORDER BY joined_at, peer_id LIMIT 1) AS guest_lang, + (SELECT count(*) FROM live_participants WHERE session_id = s.id AND role = 'guest') AS guest_count, (SELECT display_name FROM live_participants WHERE session_id = s.id AND role IN ('host', 'staff')) AS staff_name, CASE WHEN s.started_at IS NULL THEN NULL ELSE extract(epoch FROM (COALESCE(s.ended_at, now()) - s.started_at))::bigint END AS duration_secs, @@ -136,7 +143,7 @@ const SESSION_SELECT: &str = " LEFT JOIN live_desks d ON d.id = s.desk_id LEFT JOIN projects p ON p.id = s.project_id"; -/// A member sees a conversation only if they sat in one of its two seats. +/// A member sees a conversation only if they sat in one of its seats. pub(super) const MEMBER_VISIBLE: &str = "EXISTS (SELECT 1 FROM live_participants mp WHERE mp.session_id = s.id AND mp.user_id = $2)"; diff --git a/server/src/live/echo.rs b/server/src/live/echo.rs index c2eb4d27..5f3adb11 100644 --- a/server/src/live/echo.rs +++ b/server/src/live/echo.rs @@ -12,7 +12,7 @@ //! A pure state machine, one per outbound seat, fed every frame destined to that phone, which //! returns the frames to actually send. No I/O, no clock of its own (the caller passes `now`), //! no allocation on the hot path while nobody spoke recently. It is written for N seats (one -//! host and up to three guests), although a conversation has two today. +//! host and up to three guests), and a conversation has that many. //! //! * It keeps a ledger of the recent ORIGINAL words of every speaker this seat has seen, mine //! included, each entry tagged with its speaker (my own captions come back to my phone: @@ -317,9 +317,9 @@ fn same_lang(a: &str, b: &str) -> bool { // ---- the state machine ------------------------------------------------------------------- /// Smoothed microphone level of a speaker's own phone, by speaker id (`0` = unknown or quiet). -/// The pump owns the real numbers; the filter only asks. Today a conversation has two seats, so -/// the pump answers for "me" and "everyone else"; with more seats this is the one place that -/// becomes a per-peer lookup. +/// The pump owns the real numbers; the filter only asks. A conversation has up to four seats, and +/// the pump answers per peer: each peer id maps to the seat its socket holds in the conversation's +/// `SessionLatency`, and a speaker whose phone is not connected reads as 0 (unknown). pub type Levels<'a> = &'a dyn Fn(&str) -> u32; #[derive(Debug)] diff --git a/server/src/live/mod.rs b/server/src/live/mod.rs index e46ef493..d3cf50ce 100644 --- a/server/src/live/mod.rs +++ b/server/src/live/mod.rs @@ -1,5 +1,5 @@ -//! Live Connect (spec 0121): two phones, two microphones, two independent translation -//! streams running at the same time. +//! Live Connect (spec 0121): a host and up to three guests, each with a phone and a microphone, +//! every phone an independent translation stream running at the same time. //! //! This module owns what meetings do not have — server-minted sessions, credentials for a //! person with no account, permanent desk QR codes, and billing a payer who is not on the @@ -26,7 +26,7 @@ pub use state::LiveState; /// True for a room name Live Connect mints itself (`live-`). /// -/// Those rooms are private to the two seats of one conversation, so no other door into the room +/// Those rooms are private to the seats of one conversation, so no other door into the room /// machinery may open them: not the public `/ws` (a stranger who learned the session id would /// receive the subtitles and translated audio) and not the business binding. Only the exact shape /// is reserved — an existing user-chosen name such as `live-room` keeps working. diff --git a/server/src/live/routes.rs b/server/src/live/routes.rs index c10c0bc8..26cebfbb 100644 --- a/server/src/live/routes.rs +++ b/server/src/live/routes.rs @@ -73,6 +73,14 @@ pub(super) fn engine_has_room(state: &AppState) -> bool { .is_none_or(|free| free >= 2) } +/// The engine's spare upstream sessions right now (`None` = it does not meter itself). A guest's +/// arrival at a room that already has speakers needs more than two (see +/// [`sessions::engine_sessions_for_join`]), so `join` hands this to the transaction that seats +/// them rather than a yes/no. +fn engine_free_capacity(state: &AppState) -> Option { + state.engines.default().free_capacity() +} + pub(super) fn busy() -> Response { api_err( StatusCode::SERVICE_UNAVAILABLE, @@ -161,6 +169,28 @@ pub struct CreateBody { /// Present for a business session; the org pays and the project is mandatory. pub org_id: Option, pub project_id: Option, + /// How many guests the link/QR admits: 1 to `LIVE_MAX_GUESTS` (3 at most). Omitted = 1, the + /// two-person conversation. Kept as raw JSON so a wrong type or an out-of-range number + /// answers the same stable `bad_max_guests` as any other bad value, not a framework 422. + #[serde(default)] + pub max_guests: Option, +} + +/// The validated guest count of a create request. +fn parse_max_guests(raw: Option<&serde_json::Value>, ceiling: i64) -> Result { + let Some(v) = raw else { + return Ok(1); + }; + v.as_i64() + .filter(|n| (1..=ceiling).contains(n)) + .and_then(|n| i16::try_from(n).ok()) + .ok_or_else(|| { + api_err( + StatusCode::BAD_REQUEST, + "bad_max_guests", + "max_guests must be a whole number from 1 to the allowed maximum", + ) + }) } pub async fn create( @@ -179,6 +209,8 @@ pub async fn create( ) })?; + let max_guests = parse_max_guests(body.max_guests.as_ref(), cfg.max_guests)?; + if !state.rate_limiter.allow( &format!("livecreate:{}", user.user_id), 20, @@ -256,6 +288,7 @@ pub async fn create( host_lang: &lang, save_transcript, engine_id: None, + max_guests, }, ) .await @@ -274,7 +307,7 @@ pub async fn create( pool, created.session_id, "created", - json!({ "b2b": body.org_id.is_some() }), + json!({ "b2b": body.org_id.is_some(), "max_guests": max_guests }), ) .await; @@ -292,6 +325,7 @@ pub async fn create( "peer_id": created.host_peer, "token": token, "ws_path": WS_PATH, + "max_guests": max_guests, })) .into_response()) } @@ -351,27 +385,34 @@ pub async fn join( return Err(busy()); } - let joined = sessions::join_guest(pool, &body.code, &body.name, &lang) - .await - .map_err(|e| match e { - JoinError::NotFound => not_found(), - JoinError::Expired => api_err( - StatusCode::GONE, - "invite_expired", - "this invitation has expired", - ), - JoinError::Ended => api_err( - StatusCode::GONE, - "session_ended", - "this conversation has ended", - ), - JoinError::Full => api_err( - StatusCode::CONFLICT, - "session_full", - "someone has already joined this conversation", - ), - JoinError::Db(e) => db_err(e), - })?; + let joined = sessions::join_guest( + pool, + &body.code, + &body.name, + &lang, + engine_free_capacity(&state), + ) + .await + .map_err(|e| match e { + JoinError::NotFound => not_found(), + JoinError::Expired => api_err( + StatusCode::GONE, + "invite_expired", + "this invitation has expired", + ), + JoinError::Ended => api_err( + StatusCode::GONE, + "session_ended", + "this conversation has ended", + ), + JoinError::Full => api_err( + StatusCode::CONFLICT, + "session_full", + "this conversation is full", + ), + JoinError::Busy => busy(), + JoinError::Db(e) => db_err(e), + })?; let token = sessions::issue_token( &state.live.config, @@ -396,6 +437,7 @@ pub async fn join( "token": token, "ws_path": WS_PATH, "host": { "name": joined.host_name, "lang": joined.host_lang }, + "max_guests": joined.max_guests, })) .into_response()) } @@ -432,6 +474,20 @@ pub async fn end( ) -> Result { let auth = authorize(&state, &claims, id).await?; let pool = require_pool(&state)?; + // With several guests on one link, one of them hanging up must not end the conversation for + // the others: that is the host's call (or the last phone gone). A guest "ends" by leaving. + if auth.role == PeerRole::Guest { + let max_guests: Option = + sqlx::query_scalar("SELECT max_guests FROM live_sessions WHERE id = $1") + .bind(id) + .fetch_optional(pool) + .await + .map_err(db_err)?; + if max_guests.is_some_and(|n| n > 1) { + sessions::record_event(pool, id, "guest_left", json!({ "peer": auth.peer_id })).await; + return Ok(Json(json!({ "ended": false, "left": true })).into_response()); + } + } let reason = match auth.role { PeerRole::Guest => "ended_by_guest", PeerRole::Host => "ended_by_host", diff --git a/server/src/live/sessions.rs b/server/src/live/sessions.rs index ab88cf93..b4afc4b0 100644 --- a/server/src/live/sessions.rs +++ b/server/src/live/sessions.rs @@ -4,10 +4,12 @@ //! (who may take a seat, when a session is full, how many a user may hold open) are tested //! against a real database without a server. //! -//! A session has two seats. The **owner side** is the `host` (B2C owner, or a member who -//! created a B2B session) or `staff` (a member who answered a desk scan). The other seat is the -//! `guest`. Both partial unique indexes on `live_participants` make "one per side" a database -//! fact, so the checks below can lose a race and the database still says no. +//! A session has one **owner side** seat and up to `max_guests` guest seats. The owner side is +//! the `host` (B2C owner, or a member who created a B2B session) or `staff` (a member who +//! answered a desk scan); a partial unique index on `live_participants` makes "one owner side" a +//! database fact. Guests are capped IN CODE: a unique index cannot say "at most N", so +//! [`join_guest`] counts them under a `FOR UPDATE` lock on the session row, which is what makes +//! simultaneous joins admit exactly `max_guests` and no more. use chrono::{DateTime, Utc}; use sha2::{Digest, Sha256}; @@ -106,6 +108,8 @@ pub struct NewSession<'a> { pub host_lang: &'a str, pub save_transcript: bool, pub engine_id: Option<&'a str>, + /// How many guests the invitation admits (1..=3). Validated by the caller. + pub max_guests: i16, } #[derive(Debug)] @@ -138,6 +142,8 @@ pub struct Joined { pub host_name: String, pub host_lang: String, pub created_at: DateTime, + /// How many guests the session admits in all (this guest included). + pub max_guests: i16, } #[derive(Debug)] @@ -148,8 +154,10 @@ pub enum JoinError { Expired, /// The session has ended. Ended, - /// The guest seat is taken. A guest who already joined reconnects with their token. + /// Every guest seat is taken. A guest who already joined reconnects with their token. Full, + /// The translation engine cannot open the upstream sessions this guest's arrival needs. + Busy, Db(sqlx::Error), } @@ -232,8 +240,8 @@ pub async fn create_session( sqlx::query_as( "INSERT INTO live_sessions (owner_user_id, org_id, project_id, status, invite_hash, host_lang, - save_transcript, engine_id, expires_at) - VALUES ($1, $2, $3, 'waiting', $4, $5, $6, $7, + save_transcript, engine_id, max_guests, expires_at) + VALUES ($1, $2, $3, 'waiting', $4, $5, $6, $7, $9, now() + make_interval(secs => $8::int)) RETURNING id, created_at, expires_at", ) @@ -245,6 +253,7 @@ pub async fn create_session( .bind(new.save_transcript) .bind(new.engine_id) .bind(cfg.join_window_secs as i32) + .bind(new.max_guests) .fetch_one(&mut *tx) .await?; @@ -272,26 +281,63 @@ pub async fn create_session( // ---- join ------------------------------------------------------------------------------ -/// Take the guest seat with an invitation code. Succeeds once per session: a second device -/// presenting the same code gets [`JoinError::Full`], and the first one reconnects with the -/// token it was given. +/// Upstream translation sessions a room needs when everybody speaks: each participant opens one +/// per distinct OTHER language present, so `p` participants over `d` distinct languages need +/// `sum over participants of (d - 1)`. An upper bound (not everybody speaks all the time) and the +/// figure the capacity gate reasons with. +pub fn engine_sessions_needed(langs: &[&str]) -> usize { + let mut distinct: Vec<&str> = Vec::with_capacity(langs.len()); + for l in langs { + if !distinct.contains(l) { + distinct.push(l); + } + } + langs.len() * distinct.len().saturating_sub(1) +} + +/// Engine sessions a guest of language `newcomer` adds to a room of `present`: their own streams +/// plus the new language's stream for every speaker already there. Never below the two a +/// conversation has always needed, so a first guest is judged exactly as before. +pub fn engine_sessions_for_join(present: &[&str], newcomer: &str) -> usize { + let mut after: Vec<&str> = present.to_vec(); + after.push(newcomer); + engine_sessions_needed(&after) + .saturating_sub(engine_sessions_needed(present)) + .max(2) +} + +/// Take a guest seat with an invitation code. The same code admits guests one after the other +/// while the session is `waiting` or `active`, until it holds `max_guests` of them. +/// +/// The session row is locked (`FOR UPDATE`) for the whole check-and-insert, so N simultaneous +/// joins queue behind each other and exactly `max_guests` get in; the rest get +/// [`JoinError::Full`]. Each guest has a seat of their own (id, token, consent), and a seat is +/// never handed on: a guest who dropped reconnects with the token they were given. +/// +/// The join window only matters to a session nobody has joined yet (`waiting`): once it has a +/// guest it is `active` and the invitation stays good until the session ends or fills. +/// +/// `free_engine_sessions` is the engine's spare upstream capacity (`None` = unmetered). When the +/// new guest's arrival needs more than that, the join is refused with [`JoinError::Busy`] instead +/// of seating somebody whose direction the engine would silently skip. pub async fn join_guest( pool: &Pool, code: &str, name: &str, lang: &str, + free_engine_sessions: Option, ) -> Result { let hash = tokens::hash_secret(code); let mut tx = pool.begin().await?; - let row: Option<(Uuid, String, bool, DateTime, String)> = sqlx::query_as( - "SELECT id, status, (expires_at <= now()), created_at, host_lang + let row: Option<(Uuid, String, bool, DateTime, String, i16)> = sqlx::query_as( + "SELECT id, status, (expires_at <= now()), created_at, host_lang, max_guests FROM live_sessions WHERE invite_hash = $1 FOR UPDATE", ) .bind(hash.as_slice()) .fetch_optional(&mut *tx) .await?; - let Some((session_id, status, past_expiry, created_at, host_lang)) = row else { + let Some((session_id, status, past_expiry, created_at, host_lang, max_guests)) = row else { return Err(JoinError::NotFound); }; @@ -312,21 +358,35 @@ pub async fn join_guest( _ => {} } - let guest_peer: Result = sqlx::query_scalar( - "INSERT INTO live_participants (session_id, role, display_name, lang, consent_at) - VALUES ($1, 'guest', $2, $3, now()) + let present: Vec<(String, String)> = + sqlx::query_as("SELECT role, lang FROM live_participants WHERE session_id = $1") + .bind(session_id) + .fetch_all(&mut *tx) + .await?; + let guests = present.iter().filter(|(role, _)| role == "guest").count(); + if guests >= usize::try_from(max_guests).unwrap_or(1) { + return Err(JoinError::Full); + } + if let Some(free) = free_engine_sessions { + let langs: Vec<&str> = present.iter().map(|(_, l)| l.as_str()).collect(); + if free < engine_sessions_for_join(&langs, lang) { + return Err(JoinError::Busy); + } + } + + // `clock_timestamp()`, not `now()`: `now()` is the START of this transaction, and a join that + // began before a colleague's committed would sort ahead of it. Seats are read back in join + // order, and the lock above guarantees this call runs after the previous join's commit. + let guest_peer: Uuid = sqlx::query_scalar( + "INSERT INTO live_participants (session_id, role, display_name, lang, consent_at, joined_at) + VALUES ($1, 'guest', $2, $3, now(), clock_timestamp()) RETURNING peer_id", ) .bind(session_id) .bind(clean_name(name)) .bind(lang) .fetch_one(&mut *tx) - .await; - let guest_peer = match guest_peer { - Ok(p) => p, - Err(e) if is_unique_violation(&e) => return Err(JoinError::Full), - Err(e) => return Err(e.into()), - }; + .await?; sqlx::query( "UPDATE live_sessions @@ -353,13 +413,10 @@ pub async fn join_guest( host_name, host_lang, created_at, + max_guests, }) } -fn is_unique_violation(e: &sqlx::Error) -> bool { - e.as_database_error().and_then(|d| d.code()).as_deref() == Some("23505") -} - // ---- tokens ---------------------------------------------------------------------------- /// Mint the token a participant presents from now on. It outlives the join window — it @@ -517,6 +574,8 @@ pub struct SessionView { pub id: Uuid, pub status: String, pub host_lang: String, + /// How many guests the invitation admits in all. + pub max_guests: i16, pub created_at: DateTime, pub expires_at: DateTime, pub participants: Vec, @@ -528,13 +587,28 @@ pub async fn session_view( pool: &Pool, session_id: Uuid, ) -> Result, sqlx::Error> { - let head: Option<(String, String, DateTime, DateTime)> = sqlx::query_as( - "SELECT status, host_lang, created_at, expires_at FROM live_sessions WHERE id = $1", + #[derive(sqlx::FromRow)] + struct Head { + status: String, + host_lang: String, + max_guests: i16, + created_at: DateTime, + expires_at: DateTime, + } + let head: Option = sqlx::query_as( + "SELECT status, host_lang, max_guests, created_at, expires_at FROM live_sessions WHERE id = $1", ) .bind(session_id) .fetch_optional(pool) .await?; - let Some((status, host_lang, created_at, expires_at)) = head else { + let Some(Head { + status, + host_lang, + max_guests, + created_at, + expires_at, + }) = head + else { return Ok(None); }; let rows: Vec<(Uuid, String, String, String)> = sqlx::query_as( @@ -548,6 +622,7 @@ pub async fn session_view( id: session_id, status, host_lang, + max_guests, created_at, expires_at, participants: rows @@ -591,6 +666,35 @@ pub fn token_hash(token: &str) -> Vec { mod tests { use super::*; + #[test] + fn a_room_needs_one_upstream_session_per_speaker_per_other_language() { + // Alone, or all in one language: nothing to translate. + assert_eq!(engine_sessions_needed(&[]), 0); + assert_eq!(engine_sessions_needed(&["it"]), 0); + assert_eq!(engine_sessions_needed(&["it", "it", "it"]), 0); + // The classic pair: one stream each way. + assert_eq!(engine_sessions_needed(&["it", "es"]), 2); + // Three languages, four phones (two share one): each phone translates into the 2 others. + assert_eq!(engine_sessions_needed(&["it", "es", "es", "en"]), 8); + // Four phones, four languages: 4 x 3. + assert_eq!(engine_sessions_needed(&["it", "es", "en", "fr"]), 12); + } + + #[test] + fn a_join_is_judged_by_what_the_newcomer_adds_and_never_below_the_two_of_a_pair() { + // A first guest in another language: exactly the pair's two, as before. + assert_eq!(engine_sessions_for_join(&["it"], "es"), 2); + // A first guest in the host's own language adds nothing, but the old gate still wanted two. + assert_eq!(engine_sessions_for_join(&["it"], "it"), 2); + // A second guest in a third language: 3 phones x 2 languages = 6, minus the pair's 2. + assert_eq!(engine_sessions_for_join(&["it", "es"], "en"), 4); + // A second guest in a language already there: 3 phones x 1 other language = 3, minus the + // pair's 2 is 1, floored at the two a conversation has always needed. + assert_eq!(engine_sessions_for_join(&["it", "es"], "es"), 2); + // The fourth phone of four languages: 12 minus the three phones' 6. + assert_eq!(engine_sessions_for_join(&["it", "es", "en"], "fr"), 6); + } + #[test] fn languages_are_normalised_and_auto_is_refused() { assert_eq!(normalize_lang(" IT "), Some("it".into())); diff --git a/server/src/live/state.rs b/server/src/live/state.rs index 03be992c..951dc5fb 100644 --- a/server/src/live/state.rs +++ b/server/src/live/state.rs @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet}; use std::sync::atomic::{AtomicU32, AtomicU64, Ordering}; -use std::sync::{Arc, Mutex, Weak}; +use std::sync::{Arc, Mutex, OnceLock, Weak}; use std::time::Duration; use rust_decimal::Decimal; @@ -13,12 +13,18 @@ use super::config::LiveConfig; use crate::business::credits::CreditAccumulator; use crate::rooms::PeerTx; -/// Speech-to-translated-audio latency probes for one conversation (spec 0121, S18). +/// Seats a conversation can have: the host plus up to three guests (`rooms::MAX_PEERS`). +pub const MAX_SEATS: usize = 4; + +/// Speech-to-translated-audio latency probes and microphone levels for one conversation (spec +/// 0121, S18, and the echo filter). /// -/// One slot per seat (`0` = owner side, `1` = guest side). The speaker's socket *stamps* its slot -/// when speech starts; the **other** phone's socket *takes* the stamp when the first translated -/// audio of that utterance is delivered to it, and the difference is the number the product is -/// judged on: how long after somebody starts talking the other person starts hearing them. +/// One slot per **seat**, up to [`MAX_SEATS`]. A seat is a phone: [`seat_for`](Self::seat_for) +/// hands each peer id the first free slot when its socket connects, and the same slot again when +/// it reconnects. The speaker's socket *stamps* its slot when speech starts; whichever listener's +/// socket first delivers that speaker's translated audio *takes* the stamp, and the difference +/// is the number the product is judged on: how long after somebody starts talking somebody else +/// starts hearing them. One observation per utterance, however many listeners there are. /// /// Lock-free on purpose. Stamping happens once per utterance and taking costs one atomic swap /// per delivered audio chunk, so the hot path neither locks nor allocates. Stamps are nanoseconds @@ -26,13 +32,16 @@ use crate::rooms::PeerTx; #[derive(Debug)] pub struct SessionLatency { epoch: tokio::time::Instant, - stamps: [AtomicU64; 2], + /// Which peer id holds each seat. Set once, never changed: a seat is for the life of the + /// probe set, which lives as long as any socket of the conversation does. + seats: [OnceLock>; MAX_SEATS], + stamps: [AtomicU64; MAX_SEATS], /// Per seat, smoothed microphone level of the audio it feeds the engine (mean absolute PCM16 /// sample) and when it was last updated. Written only by that seat's socket. - mic_level: [AtomicU32; 2], - mic_level_at: [AtomicU64; 2], + mic_level: [AtomicU32; MAX_SEATS], + mic_level_at: [AtomicU64; MAX_SEATS], /// Per seat, when its microphone last carried speech (same nanosecond encoding as `stamps`). - mic_voiced_at: [AtomicU64; 2], + mic_voiced_at: [AtomicU64; MAX_SEATS], /// When the echo filter last wrote a diagnostics event for this conversation. echo_report_at: AtomicU64, } @@ -51,14 +60,46 @@ impl SessionLatency { fn new() -> Self { Self { epoch: tokio::time::Instant::now(), - stamps: [AtomicU64::new(0), AtomicU64::new(0)], - mic_level: [AtomicU32::new(0), AtomicU32::new(0)], - mic_level_at: [AtomicU64::new(0), AtomicU64::new(0)], - mic_voiced_at: [AtomicU64::new(0), AtomicU64::new(0)], + seats: std::array::from_fn(|_| OnceLock::new()), + stamps: std::array::from_fn(|_| AtomicU64::new(0)), + mic_level: std::array::from_fn(|_| AtomicU32::new(0)), + mic_level_at: std::array::from_fn(|_| AtomicU64::new(0)), + mic_voiced_at: std::array::from_fn(|_| AtomicU64::new(0)), echo_report_at: AtomicU64::new(0), } } + /// A probe set with no conversation behind it, for tests in other modules. + #[cfg(test)] + pub(crate) fn new_for_test() -> Self { + Self::new() + } + + /// The seat of `peer`: the slot it already holds, else the first free one. Called once per + /// socket at connect, never per frame. A conversation never has more than [`MAX_SEATS`] + /// peers, so running out is a bug elsewhere; the last slot is shared rather than panicking. + pub fn seat_for(&self, peer: &str) -> usize { + for (i, slot) in self.seats.iter().enumerate() { + if slot.get().is_some_and(|p| &**p == peer) { + return i; + } + // `set` can lose a race to another socket claiming the same free slot; then look at + // what it holds (it may even be this very peer, reconnecting twice). + if slot.set(peer.into()).is_ok() || slot.get().is_some_and(|p| &**p == peer) { + return i; + } + } + MAX_SEATS - 1 + } + + /// The seat `peer` holds, if its socket has connected. What the echo filter's level lookup + /// uses to turn a speaker id on a frame into a microphone. + pub fn seat_of(&self, peer: &str) -> Option { + self.seats + .iter() + .position(|s| s.get().is_some_and(|p| &**p == peer)) + } + fn encode(&self, at: tokio::time::Instant) -> u64 { (at.saturating_duration_since(self.epoch).as_nanos() as u64).saturating_add(1) } @@ -70,7 +111,7 @@ impl SessionLatency { /// time constant at 10 frames/s) rather than the last frame, so a pause between words does not /// flip the answer. Only the seat's own socket writes it, so a plain load/store is enough. pub fn mic_frame(&self, seat: usize, at: tokio::time::Instant, mean_abs: u32, voiced: bool) { - let i = seat & 1; + let i = seat % MAX_SEATS; // A stale level reads as 0, so speech after a long silence starts from silence. let old = self.mic_level(i, at); // Fast attack, slow release: speech that has just started reads as most of its real level @@ -91,7 +132,7 @@ impl SessionLatency { /// The smoothed microphone level of `seat` at `now`; `0` when it has been quiet for a while. pub fn mic_level(&self, seat: usize, now: tokio::time::Instant) -> u32 { - let i = seat & 1; + let i = seat % MAX_SEATS; let stamp = self.mic_level_at[i].load(Ordering::Relaxed); if stamp == 0 { return 0; @@ -107,7 +148,7 @@ impl SessionLatency { /// When `seat`'s microphone last carried speech, if ever. pub fn mic_last_voiced(&self, seat: usize) -> Option { - let stamp = self.mic_voiced_at[seat & 1].load(Ordering::Relaxed); + let stamp = self.mic_voiced_at[seat % MAX_SEATS].load(Ordering::Relaxed); (stamp != 0).then(|| self.epoch + Duration::from_nanos(stamp - 1)) } @@ -124,23 +165,27 @@ impl SessionLatency { .is_ok() } - /// The seat index of a role: the owner side (host, staff) is `0`, the guest `1`. - pub fn seat(is_guest: bool) -> usize { - usize::from(is_guest) - } - /// `seat` started speaking at `at`. Overwrites whatever was pending: a new utterance /// supersedes one whose audio never arrived. pub fn speech_started(&self, seat: usize, at: tokio::time::Instant) { let ns = at.saturating_duration_since(self.epoch).as_nanos() as u64; - self.stamps[seat & 1].store(ns.saturating_add(1), Ordering::Relaxed); + self.stamps[seat % MAX_SEATS].store(ns.saturating_add(1), Ordering::Relaxed); } - /// Translated audio of the **other** seat's speech was delivered to `listener` at `at`. - /// Returns the latency of the utterance it answers, exactly once per utterance: a second - /// chunk of the same utterance finds nothing pending. - pub fn audio_delivered(&self, listener: usize, at: tokio::time::Instant) -> Option { - let stamp = self.stamps[(listener & 1) ^ 1].swap(0, Ordering::Relaxed); + /// Translated audio of `speaker`'s speech was delivered to `listener` at `at`. Returns the + /// latency of the utterance it answers, exactly once per utterance: a second chunk of the + /// same utterance (or the same chunk reaching another listener) finds nothing pending. A seat + /// never measures its own speech. + pub fn audio_delivered( + &self, + listener: usize, + speaker: usize, + at: tokio::time::Instant, + ) -> Option { + if listener % MAX_SEATS == speaker % MAX_SEATS { + return None; + } + let stamp = self.stamps[speaker % MAX_SEATS].swap(0, Ordering::Relaxed); if stamp == 0 { return None; } @@ -505,16 +550,16 @@ mod tests { let l = SessionLatency::new(); let t0 = l.epoch; // Nothing was said: nothing to measure. - assert_eq!(l.audio_delivered(1, at(t0, 50)), None); + assert_eq!(l.audio_delivered(1, 0, at(t0, 50)), None); // The owner side (seat 0) speaks; the guest (seat 1) hears the translation 240 ms later. l.speech_started(0, at(t0, 100)); assert_eq!( - l.audio_delivered(1, at(t0, 340)), + l.audio_delivered(1, 0, at(t0, 340)), Some(Duration::from_millis(240)) ); // The second chunk of the same utterance is not a second sample. - assert_eq!(l.audio_delivered(1, at(t0, 360)), None); + assert_eq!(l.audio_delivered(1, 0, at(t0, 360)), None); } #[test] @@ -523,9 +568,9 @@ mod tests { let t0 = l.epoch; l.speech_started(0, at(t0, 10)); // Audio delivered to the speaker itself (it would be an echo) is not a sample. - assert_eq!(l.audio_delivered(0, at(t0, 200)), None); + assert_eq!(l.audio_delivered(0, 0, at(t0, 200)), None); // And it did not consume the pending stamp of the real listener. - assert!(l.audio_delivered(1, at(t0, 250)).is_some()); + assert!(l.audio_delivered(1, 0, at(t0, 250)).is_some()); } #[test] @@ -535,11 +580,11 @@ mod tests { l.speech_started(0, at(t0, 0)); l.speech_started(1, at(t0, 50)); assert_eq!( - l.audio_delivered(0, at(t0, 400)), + l.audio_delivered(0, 1, at(t0, 400)), Some(Duration::from_millis(350)) ); assert_eq!( - l.audio_delivered(1, at(t0, 420)), + l.audio_delivered(1, 0, at(t0, 420)), Some(Duration::from_millis(420)) ); } @@ -552,12 +597,56 @@ mod tests { // Never translated; the speaker starts again later and that is what the audio answers. l.speech_started(0, at(t0, 5_000)); assert_eq!( - l.audio_delivered(1, at(t0, 5_300)), + l.audio_delivered(1, 0, at(t0, 5_300)), Some(Duration::from_millis(300)) ); // A stamp older than the plausibility window is discarded, not recorded. l.speech_started(0, at(t0, 6_000)); - assert_eq!(l.audio_delivered(1, at(t0, 6_000 + 31_000)), None); + assert_eq!(l.audio_delivered(1, 0, at(t0, 6_000 + 31_000)), None); + } + + #[test] + fn each_peer_gets_a_seat_of_its_own_and_keeps_it_across_a_reconnect() { + let l = SessionLatency::new(); + let ids = ["host", "g1", "g2", "g3"]; + let seats: Vec = ids.iter().map(|p| l.seat_for(p)).collect(); + assert_eq!(seats, vec![0, 1, 2, 3], "four peers, four distinct seats"); + // The same peer asking again (a reconnect) gets the seat it had, not a new one. + assert_eq!(l.seat_for("g2"), 2); + assert_eq!(l.seat_of("g3"), Some(3)); + assert_eq!(l.seat_of("stranger"), None); + } + + #[test] + fn a_microphone_level_belongs_to_one_of_four_seats() { + let l = SessionLatency::new(); + let t0 = l.epoch; + for seat in 0..MAX_SEATS { + l.mic_frame(seat, at(t0, 0), 100 * (seat as u32 + 1), true); + } + // Each seat reads back its own level, never a neighbour's (the old code folded seats + // onto two slots, so seat 2 read seat 0). + let levels: Vec = (0..MAX_SEATS) + .map(|seat| l.mic_level(seat, at(t0, 0))) + .collect(); + assert_eq!(levels, vec![50, 100, 150, 200]); + assert_eq!(l.mic_last_voiced(2), Some(at(t0, 0))); + } + + #[test] + fn with_three_listeners_one_utterance_is_observed_once_and_for_its_own_speaker() { + let l = SessionLatency::new(); + let t0 = l.epoch; + l.speech_started(1, at(t0, 100)); + // Seat 2's utterance is a different stream: its audio must not consume seat 1's stamp. + assert_eq!(l.audio_delivered(0, 2, at(t0, 200)), None); + // Seat 1's audio reaches three phones; only the first delivery is a sample. + assert_eq!( + l.audio_delivered(0, 1, at(t0, 340)), + Some(Duration::from_millis(240)) + ); + assert_eq!(l.audio_delivered(2, 1, at(t0, 345)), None); + assert_eq!(l.audio_delivered(3, 1, at(t0, 350)), None); } #[test] diff --git a/server/src/live/ws.rs b/server/src/live/ws.rs index 5d82291b..ffa1c0c0 100644 --- a/server/src/live/ws.rs +++ b/server/src/live/ws.rs @@ -1,9 +1,9 @@ //! `/ws/live` — one phone's socket in a Live Connect conversation. //! -//! Each phone opens its own socket with the token it was issued. The server puts both in a +//! Each phone opens its own socket with the token it was issued. The server puts all of them in a //! private room named after the session and reuses the meeting machinery for everything //! after that: the engine opens one upstream session per speaker **per target language**, so -//! two phones speaking different languages are two independent concurrent translation streams +//! phones speaking different languages are independent concurrent translation streams //! with nothing here coordinating them. This module adds what meetings do not have: a seat //! that already exists (no client-chosen identity), a payer who is not on the call, and an //! explicit end-of-conversation signal. @@ -223,10 +223,14 @@ async fn pump_live( match msg { Some(m) if echo.enabled() => { echo.note_own_voice(latency.mic_last_voiced(seat)); - // Two seats today: "me" and "the other phone". More seats make this the - // one place that looks a speaker up. - let (mine, theirs) = (latency.mic_level(seat, now), latency.mic_level(seat ^ 1, now)); - let level = |id: &str| if id == my_id { mine } else { theirs }; + // Every speaker id on a frame is a peer id; each peer has a seat, and each + // seat its own smoothed microphone. A speaker whose phone has not + // connected has no seat and reads as 0, which is "unknown" and never + // suppresses (the filter fails open). + let level = |id: &str| { + let s = if id == my_id { Some(seat) } else { latency.seat_of(id) }; + s.map_or(0, |s| latency.mic_level(s, now)) + }; echo.on_frame(now, m, &level, &mut out); if echo.has_report() && latency.allow_echo_report(now) { let (suppressed, failed_open) = echo.take_report(); @@ -280,11 +284,17 @@ async fn send_frame( m: String, ) -> Result<(), ()> { let audio = m.starts_with(TRANSLATED_AUDIO_PREFIX); + // Whose speech this audio answers, read before the frame is handed to the socket. + let speaker = if audio { + speaker_seat(latency, &m) + } else { + None + }; if ws_tx.send(Message::Text(m.into())).await.is_err() { return Err(()); } - if audio { - if let Some(d) = latency.audio_delivered(seat, tokio::time::Instant::now()) { + if let Some(speaker) = speaker { + if let Some(d) = latency.audio_delivered(seat, speaker, tokio::time::Instant::now()) { crate::metrics::record_live_translation_ms( u64::try_from(d.as_micros().div_ceil(1000)) .unwrap_or(u64::MAX) @@ -295,7 +305,20 @@ async fn send_frame( Ok(()) } -/// The room a session's two phones share. Server-minted and unguessable, so it can never +/// The seat of the speaker a `translated_audio` frame answers. The frame is +/// `{"type":"translated_audio","speaker_id":"",...}` (`speaker_id` is the first field +/// after the tag), so the id is the text between the quotes that follow it. Once per audio chunk, +/// no allocation; `None` for anything that does not parse (the sample is simply not taken). +fn speaker_seat(latency: &SessionLatency, frame: &str) -> Option { + const KEY: &str = r#""speaker_id":""#; + let rest = frame + .get(TRANSLATED_AUDIO_PREFIX.len()..)? + .strip_prefix(',')?; + let id = rest.strip_prefix(KEY)?.split_once('"')?.0; + latency.seat_of(id) +} + +/// The room a session's phones share. Server-minted and unguessable, so it can never /// collide with (or be joined through) a public `/ws` room. pub fn room_name(session_id: Uuid) -> String { format!("live-{session_id}") @@ -532,7 +555,9 @@ async fn handle_live_peer(socket: WebSocket, state: AppState, auth: PeerAuth) { } let latency = state.live.latency_for(auth.session_id); - let seat = SessionLatency::seat(auth.role == PeerRole::Guest); + // This phone's seat for the probes and the echo filter's microphone levels: its own slot, the + // same one again if it reconnects. + let seat = latency.seat_for(&peer_id); let echo = EchoFilter::new( EchoConfig { enabled: state.live.config.echo_filter, @@ -918,7 +943,6 @@ async fn start_speaking( speaker_lang: auth.lang.clone(), interval_secs: pricing.usage_update_interval, rate_per_second: engine.metadata().user_rate_per_second(), - scale_by_target_count: engine.metadata().capabilities.cost_scales_per_language, low_balance_threshold: pricing.low_balance_threshold, activity: sent.clone(), } @@ -1054,6 +1078,29 @@ mod tests { assert_eq!(reports, MAX_THROTTLE_REPORTS); } + #[test] + fn the_speaker_of_an_audio_frame_is_read_from_the_frame_the_protocol_really_emits() { + let l = SessionLatency::new_for_test(); + let (host, g1, g2) = (l.seat_for("host-peer"), l.seat_for("g1"), l.seat_for("g2")); + let audio = |who: &str| { + ServerMessage::TranslatedAudio { + speaker_id: who.into(), + lang: "es".into(), + seq: 1, + pcm16_b64: "AA==".into(), + } + .to_json() + }; + assert_eq!(speaker_seat(&l, &audio("host-peer")), Some(host)); + assert_eq!(speaker_seat(&l, &audio("g1")), Some(g1)); + assert_eq!(speaker_seat(&l, &audio("g2")), Some(g2)); + // A speaker whose phone never connected, and text that is not an audio frame at all, take + // no sample rather than guessing a seat. + assert_eq!(speaker_seat(&l, &audio("nobody")), None); + assert_eq!(speaker_seat(&l, r#"{"type":"translated_audio"}"#), None); + assert_eq!(speaker_seat(&l, "{}"), None); + } + #[test] fn the_prefix_the_pump_looks_for_is_what_the_protocol_really_emits() { let frame = ServerMessage::TranslatedAudio { diff --git a/server/tests/live_admin.rs b/server/tests/live_admin.rs index 743b8631..e0fd3ff2 100644 --- a/server/tests/live_admin.rs +++ b/server/tests/live_admin.rs @@ -1540,3 +1540,59 @@ async fn reconcile_compares_each_debit_with_its_tick_not_just_the_totals() { // ...and the verdict is a mismatch all the same. assert_eq!(v["status"], "mismatch", "{v}"); } + +// ---- several guests in one conversation (spec 0121, S1) -------------------------------- + +#[tokio::test] +async fn an_operator_reads_and_ends_a_conversation_with_several_guests() { + let srv = srv!(); + let org = org(&srv).await; + let conv = seed(&srv, Owner::Org(&org), "active").await; + for (name, lang) in [("Ben Guest", "en"), ("Carla Guest", "pt")] { + sqlx::query( + "INSERT INTO live_participants (session_id, role, display_name, lang, token_hash, consent_at, joined_at) + VALUES ($1, 'guest', $2, $3, '\\xfeedface', now(), now() + interval '1 minute')", + ) + .bind(conv.live) + .bind(name) + .bind(lang) + .execute(&srv.pool) + .await + .unwrap(); + } + + let (s, v, text) = srv + .admin( + "/api/admin/live/session-transcript", + json!({ "session_id": conv.live, "actor": OPERATOR, "reason": REASON }), + ) + .await; + assert_eq!(s, StatusCode::OK, "{text}"); + let who: Vec<(&str, &str)> = v["participants"] + .as_array() + .unwrap() + .iter() + .map(|p| { + ( + p["role"].as_str().unwrap(), + p["display_name"].as_str().unwrap(), + ) + }) + .collect(); + assert_eq!(who.len(), 4, "{who:?}"); + assert_eq!(who.iter().filter(|(role, _)| *role == "guest").count(), 3); + assert!(who.contains(&("guest", "Carla Guest"))); + assert!(!text.contains("feedface"), "token hashes never leave"); + + let (s, v, text) = srv + .admin( + "/api/admin/live/session-end", + json!({ "session_id": conv.live, "actor": OPERATOR, "reason": REASON }), + ) + .await; + assert_eq!( + (s, v["end_reason"].as_str()), + (StatusCode::OK, Some("ended_by_admin")), + "{text}" + ); +} diff --git a/server/tests/live_billing.rs b/server/tests/live_billing.rs index 3b60f789..605603bc 100644 --- a/server/tests/live_billing.rs +++ b/server/tests/live_billing.rs @@ -535,7 +535,6 @@ async fn short_start_stop_cycles_of_an_org_seat_add_up_to_what_they_cost() { interval_secs: 1, // 0.4 of a credit per tick. rate_per_second: 0.004, - scale_by_target_count: false, low_balance_threshold: 0.0, activity: Arc::new(AtomicU64::new(0)), }; @@ -606,7 +605,6 @@ async fn credits_of_a_duplicate_tick_go_back_to_the_carry() { speaker_lang: "it".into(), interval_secs: 1, rate_per_second: 0.012, - scale_by_target_count: false, low_balance_threshold: 0.0, activity: activity.clone(), }; @@ -674,7 +672,6 @@ async fn credits_of_a_failed_tick_go_back_to_the_carry() { speaker_lang: "it".into(), interval_secs: 1, rate_per_second: 0.012, - scale_by_target_count: false, low_balance_threshold: 0.0, activity: activity.clone(), }; @@ -715,3 +712,188 @@ async fn credits_of_a_failed_tick_go_back_to_the_carry() { let _ = (rows, dup_rows); assert!(spent >= 8, "credits of failed ticks were lost from the carry: only {spent} charged for ~7.2 credits of audio"); } + +// ---- the owner pays for every seat, per DISTINCT target language (spec 0121, S3) -------- + +/// One participant row for `session`, a guest unless it is the first (the host already exists). +async fn seat_row(pool: &Pool, session: Uuid, lang: &str) -> Uuid { + sqlx::query_scalar( + "INSERT INTO live_participants (session_id, role, display_name, lang) VALUES ($1, 'guest', 'G', $2) RETURNING peer_id", + ) + .bind(session) + .bind(lang) + .fetch_one(pool) + .await + .unwrap() +} + +/// Start one seat's meter, let it take its opening charge, stop it. +#[allow(clippy::too_many_arguments)] +async fn opening_charge( + pool: &Pool, + live: &Arc, + rooms: &Arc, + payer: LivePayer, + session: Uuid, + peer: Uuid, + room: &str, + lang: &str, +) { + let (end_tx, _end_rx) = mpsc::unbounded_channel(); + let (cancel, cancel_rx) = oneshot::channel(); + let meter = LiveMeter { + pool: pool.clone(), + live: live.clone(), + rooms: rooms.clone(), + payer, + session, + peer, + room: room.into(), + speaker_lang: lang.into(), + interval_secs: 1, + rate_per_second: 0.01, + low_balance_threshold: 0.0, + activity: Arc::new(AtomicU64::new(0)), + }; + let run = tokio::spawn(meter.run(end_tx, cancel_rx)); + tokio::time::sleep(Duration::from_millis(250)).await; + let _ = cancel.send(()); + run.await.unwrap(); +} + +/// What each seat's opening charge cost, in streams, for rooms of different languages. The first +/// language is the host's. `None` = nobody to translate for, so no tick at all. +const LANGUAGE_MIXES: &[(&[&str], &[Option])] = &[ + // The case the product owner described: host it, guests es, es, en. The host's speech goes to + // two languages (es, en) however many Spanish speakers there are; a Spanish guest's to two + // (it, en); the English guest's to two (it, es). + ( + &["it", "es", "es", "en"], + &[Some(2), Some(2), Some(2), Some(2)], + ), + // Two guests in one language never double a cost: everybody has exactly one other language. + ( + &["it", "es", "es", "es"], + &[Some(1), Some(1), Some(1), Some(1)], + ), + // Four languages: each seat translates into the three others. + ( + &["it", "es", "en", "fr"], + &[Some(3), Some(3), Some(3), Some(3)], + ), + // The host shares a language with a guest: that guest is nobody to translate for. + (&["it", "it", "es"], &[Some(1), Some(1), Some(1)]), + // Everybody speaks the same language: nothing is translated, nothing is charged. + (&["it", "it", "it", "it"], &[None, None, None, None]), +]; + +#[tokio::test] +async fn a_user_pays_one_stream_per_distinct_language_for_every_seat_to_the_last_digit() { + let pool = pool!(); + for (langs, streams) in LANGUAGE_MIXES { + let owner = make_user(&pool, usd(5.0)).await; + let (session, host, call) = make_session(&pool, Some(owner), None).await; + let mut peers = vec![host]; + for l in &langs[1..] { + peers.push(seat_row(&pool, session, l).await); + } + let live = Arc::new(LiveState::new(LiveConfig::disabled())); + let rooms = Arc::new(RoomManager::new()); + let room = format!("live-{call}"); + for (p, l) in peers.iter().zip(*langs) { + seat(&rooms, &room, &p.to_string(), l); + } + let payer = user_payer(&pool, owner).await; + for (p, l) in peers.iter().zip(*langs) { + opening_charge(&pool, &live, &rooms, payer, session, *p, &room, l).await; + } + + let mut expected_total = Decimal::ZERO; + for ((p, l), n) in peers.iter().zip(*langs).zip(*streams) { + let amounts: Vec = sqlx::query_scalar( + "SELECT amount FROM live_usage_ticks WHERE session_id = $1 AND peer_id = $2", + ) + .bind(session) + .bind(p) + .fetch_all(&pool) + .await + .unwrap(); + match n { + None => assert!(amounts.is_empty(), "{langs:?}: {l} was charged {amounts:?}"), + Some(n) => { + let unit = usd(0.01 * 1.0 * f64::from(*n)); + assert_eq!(amounts, vec![unit], "{langs:?}: seat speaking {l}"); + expected_total += unit; + } + } + } + // The owner's balance and ledger moved by exactly the ticks, no more, no less. + assert_eq!( + balance(&pool, owner).await, + usd(5.0) - expected_total, + "{langs:?}" + ); + let (rows, ledger): (i64, Decimal) = sqlx::query_as( + "SELECT count(*), COALESCE(sum(-amount), 0) FROM credit_transactions WHERE user_id = $1 AND kind = 'usage'", + ) + .bind(owner) + .fetch_one(&pool) + .await + .unwrap(); + let charged_seats = streams.iter().flatten().count() as i64; + assert_eq!((rows, ledger), (charged_seats, expected_total), "{langs:?}"); + } +} + +#[tokio::test] +async fn an_org_pays_the_same_streams_in_whole_credits_and_every_seat_has_its_own_tick() { + let pool = pool!(); + let owner = make_user(&pool, usd(1.0)).await; + let org = make_org(&pool, owner, 1000).await; + let (session, host, call) = make_session(&pool, None, Some(org)).await; + // host it, guests es, es, en: two streams each, and a credit is a cent, so 2 credits a seat. + let langs = ["it", "es", "es", "en"]; + let mut peers = vec![host]; + for l in &langs[1..] { + peers.push(seat_row(&pool, session, l).await); + } + let live = Arc::new(LiveState::new(LiveConfig::disabled())); + let rooms = Arc::new(RoomManager::new()); + let room = format!("live-{call}"); + for (p, l) in peers.iter().zip(langs) { + seat(&rooms, &room, &p.to_string(), l); + } + let payer = LivePayer::Org { + org_id: org, + call_session: call, + }; + for (p, l) in peers.iter().zip(langs) { + opening_charge(&pool, &live, &rooms, payer, session, *p, &room, l).await; + } + + // One tick per seat (the key includes the peer, so four seats never collide), 2 credits each. + let ticks: Vec<(Uuid, Decimal)> = sqlx::query_as( + "SELECT peer_id, amount FROM live_usage_ticks WHERE session_id = $1 AND payer_kind = 'org'", + ) + .bind(session) + .fetch_all(&pool) + .await + .unwrap(); + assert_eq!(ticks.len(), 4, "{ticks:?}"); + for p in &peers { + let mine: Vec<_> = ticks.iter().filter(|(id, _)| id == p).collect(); + assert_eq!(mine.len(), 1); + assert_eq!(mine[0].1, Decimal::from(2)); + } + assert_eq!(credits(&pool, org).await, 1000 - 8); + assert_eq!( + count( + &pool, + "SELECT -sum(amount)::bigint FROM organization_credits_transactions WHERE session_id = $1 AND type = 'live_connect'", + call + ) + .await, + 8, + "the ledger agrees with the wallet" + ); +} diff --git a/server/tests/live_dashboard.rs b/server/tests/live_dashboard.rs index d1392018..baa11d36 100644 --- a/server/tests/live_dashboard.rs +++ b/server/tests/live_dashboard.rs @@ -712,3 +712,99 @@ async fn usage_is_for_admins_and_counts_only_the_orgs_own_metered_ticks() { StatusCode::NOT_FOUND ); } + +// ---- several guests in one conversation (spec 0121, S1) -------------------------------- + +async fn add_guest(srv: &Srv, s: &Seeded, name: &str, lang: &str, minutes_later: i32) -> Uuid { + sqlx::query_scalar( + "INSERT INTO live_participants (session_id, role, display_name, lang, joined_at) + VALUES ($1, 'guest', $2, $3, now() + make_interval(mins => $4)) RETURNING peer_id", + ) + .bind(s.session) + .bind(name) + .bind(lang) + .bind(minutes_later) + .fetch_one(&srv.pool) + .await + .unwrap() +} + +#[tokio::test] +async fn a_conversation_with_several_guests_still_lists_and_opens_and_says_how_many_there_were() { + let srv = srv!(); + let w = world(&srv).await; + let t0 = Utc::now() - Duration::minutes(10); + let s = seed( + &srv, + &w, + w.desk1, + w.project_a, + "ended", + "Lucia", + Some((w.member_a.1, "Anna")), + t0, + ) + .await; + add_guest(&srv, &s, "Pablo", "es", 1).await; + let ann = add_guest(&srv, &s, "Ann", "en", 2).await; + says( + &srv, + &s, + ann, + "Ann", + "en", + "Where is the parking?", + json!({ "it": "Dov'è il parcheggio?", "es": "¿Dónde está el parking?" }), + t0, + ) + .await; + + let base = format!("/api/business/organizations/{}/live/sessions", w.org); + // The list used to ask for THE guest with a scalar subquery: two rows, a 500. + let (st, list) = srv.get(&base, &w.owner.0).await; + assert_eq!(st, StatusCode::OK, "{list}"); + let row = list["sessions"] + .as_array() + .unwrap() + .iter() + .find(|r| r["id"] == s.session.to_string()) + .expect("the conversation is listed"); + assert_eq!( + row["guest"], + json!({ "name": "Lucia", "lang": "es" }), + "the column shows the first guest to join" + ); + assert_eq!(row["guest_count"], 3); + assert_eq!(row["staff_name"], "Anna"); + + // A member who sat in it reads it, with every seat and every speaker. + let (st, detail) = srv + .get(&format!("{base}/{}?lang=it", s.session), &w.member_a.0) + .await; + assert_eq!(st, StatusCode::OK, "{detail}"); + assert_eq!(detail["session"]["guest_count"], 3); + let roles: Vec<&str> = detail["participants"] + .as_array() + .unwrap() + .iter() + .map(|p| p["role"].as_str().unwrap()) + .collect(); + assert_eq!(roles.iter().filter(|r| **r == "guest").count(), 3); + assert_eq!(roles.len(), 4); + assert_eq!(detail["utterances"][0]["speaker_name"], "Ann"); + assert_eq!( + detail["utterances"][0]["translation"], + "Dov'è il parcheggio?" + ); + + // A conversation with one guest says one. + let single = seed(&srv, &w, w.desk1, w.project_a, "ended", "Solo", None, t0).await; + let (_, list) = srv.get(&base, &w.owner.0).await; + let row = list["sessions"] + .as_array() + .unwrap() + .iter() + .find(|r| r["id"] == single.session.to_string()) + .unwrap(); + assert_eq!(row["guest_count"], 1); +} diff --git a/server/tests/live_desks.rs b/server/tests/live_desks.rs index ca67c38a..fdcec19b 100644 --- a/server/tests/live_desks.rs +++ b/server/tests/live_desks.rs @@ -405,6 +405,15 @@ async fn every_scan_creates_a_new_session_and_the_qr_never_names_one() { .await .unwrap(); assert_eq!(rows.len(), 2); + // A desk conversation is always a pair, whatever the multi-guest feature allows elsewhere: a + // scan seats ONE guest, there is no invitation to share, and the row says so. + let caps: Vec = + sqlx::query_scalar("SELECT max_guests FROM live_sessions WHERE desk_id = $1") + .bind(desk) + .fetch_all(&srv.pool) + .await + .unwrap(); + assert_eq!(caps, vec![1, 1]); for (status, org_id, project_id, owner_user, invite, save) in rows { assert_eq!( ( diff --git a/server/tests/live_intelligence.rs b/server/tests/live_intelligence.rs index 788e3623..41c6bd60 100644 --- a/server/tests/live_intelligence.rs +++ b/server/tests/live_intelligence.rs @@ -2418,3 +2418,155 @@ async fn erasing_stays_possible_when_the_feature_is_switched_off() { ); assert_eq!(counts(&srv, &c, Some(org.id)).await["call_sessions"], 0); } + +// ===== several guests in one conversation (spec 0121, S1) ================================ + +/// Seat one more guest the way `join_guest` does and let them say one thing in the conversation's +/// last room, recorded like the others. Returns their participant id. +async fn extra_guest(srv: &Srv, c: &Conv, name: &str, lang: &str, said: &str, at: i64) -> Uuid { + let peer: Uuid = sqlx::query_scalar( + "INSERT INTO live_participants (session_id, role, display_name, lang, token_hash, consent_at, joined_at) + VALUES ($1, 'guest', $2, $3, '\\xabad1dea', now(), now() + interval '1 minute') RETURNING peer_id", + ) + .bind(c.live) + .bind(name) + .bind(lang) + .fetch_one(&srv.pool) + .await + .unwrap(); + let call = *c.calls.last().unwrap(); + sqlx::query( + "INSERT INTO session_participants (session_id, peer_id, name, lang) VALUES ($1, $2, $3, $4)", + ) + .bind(call) + .bind(peer.to_string()) + .bind(name) + .bind(lang) + .execute(&srv.pool) + .await + .unwrap(); + sqlx::query( + "INSERT INTO transcript_events (session_id, event_type, speaker_peer_id, speaker_name, original_text, original_lang, translations, ts) + SELECT $1, 'speech', $2, $3, $4, $5, '{}'::jsonb, started_at + make_interval(secs => $6) + FROM live_sessions WHERE id = $7", + ) + .bind(call) + .bind(peer.to_string()) + .bind(name) + .bind(said) + .bind(lang) + .bind(at as f64) + .bind(c.live) + .execute(&srv.pool) + .await + .unwrap(); + peer +} + +#[tokio::test] +async fn a_conversation_with_several_guests_is_finalised_searched_and_erased_whole() { + let srv = srv!(); + let org = org(&srv).await; + let c = seed(&srv, Owner::Org(&org), "ended", 0, &[exchange()]).await; + let ben = extra_guest( + &srv, + &c, + "Ben Guest", + "en", + "Is breakfast included in the room", + 26, + ) + .await; + let carla = extra_guest( + &srv, + &c, + "Carla Guest", + "pt", + "Posso pagar com cartao de credito", + 32, + ) + .await; + let call = c.calls[0]; + + // Every speaker is its own voice in the stored transcript, in order of speech. + let out = finalize::finalize(&srv.state, c.live).await.unwrap(); + assert!( + matches!(out, Outcome::Finalized { segments: 5, .. }), + "{out:?}" + ); + let rows = transcripts_of(&srv, &c.calls).await; + assert_eq!(rows.len(), 1); + let speakers: Vec<(&str, &str)> = rows[0] + .segments + .iter() + .map(|s| (s.speaker_id.as_str(), s.speaker_name.as_str())) + .collect(); + let (staff, guest) = (c.staff_peer.to_string(), c.guest_peer.to_string()); + let (ben, carla) = (ben.to_string(), carla.to_string()); + assert_eq!( + speakers, + vec![ + (staff.as_str(), STAFF_NAME), + (guest.as_str(), GUEST_NAME), + (staff.as_str(), STAFF_NAME), + (ben.as_str(), "Ben Guest"), + (carla.as_str(), "Carla Guest"), + ] + ); + index_without_provider(&srv, &org, call).await; + + // Search: any participant's language finds the conversation, and a speaker filter finds only + // what that speaker said (the filters were written for a pair). + let find = |f: Filters| run(&srv, &org, org.owner, &Visible::Everything, None, f); + for lang in ["it", "es", "en", "pt"] { + let hits = sessions_of( + &find(Filters { + lang: Some(lang.into()), + kind: Some("live_connect".into()), + ..Default::default() + }) + .await, + ); + assert!(hits.contains(&call), "lang {lang}: {hits:?}"); + } + let none = find(Filters { + lang: Some("ja".into()), + kind: Some("live_connect".into()), + ..Default::default() + }) + .await; + assert!(!sessions_of(&none).contains(&call)); + let carla_hits = find(Filters { + speaker_like: Some("carla".into()), + session_id: Some(c.live), + ..Default::default() + }) + .await; + assert!(!carla_hits.is_empty()); + assert!( + carla_hits + .iter() + .all(|r| r.speaker_name.as_deref() == Some("Carla Guest")), + "{:?}", + carla_hits + .iter() + .map(|r| &r.speaker_name) + .collect::>() + ); + + // Erasure scrubs every guest's name and credential, not just the first. + let out = erase::erase_content(&srv.pool, c.live).await.unwrap(); + assert_eq!(out.participants_scrubbed, 4); + let left: Vec<(String, bool)> = sqlx::query_as( + "SELECT display_name, token_hash IS NOT NULL FROM live_participants WHERE session_id = $1", + ) + .bind(c.live) + .fetch_all(&srv.pool) + .await + .unwrap(); + assert_eq!(left.len(), 4); + assert!( + left.iter().all(|(name, hash)| name == "Erased" && !hash), + "{left:?}" + ); +} diff --git a/server/tests/live_rest.rs b/server/tests/live_rest.rs index 0fd4f669..8d2f9445 100644 --- a/server/tests/live_rest.rs +++ b/server/tests/live_rest.rs @@ -38,6 +38,8 @@ struct Opts { /// Upstream session cap; 1 makes the capacity gate refuse. qwen_max_sessions: Option, max_concurrent: Option<&'static str>, + /// `LIVE_MAX_GUESTS`: the most guests a host may ask for. + max_guests: Option<&'static str>, } async fn setup(opts: Opts) -> Option { @@ -69,6 +71,7 @@ async fn setup(opts: Opts) -> Option { "LIVE_CONNECT_ENABLED" => Some("1".into()), "LIVE_TOKEN_KEY" => Some(LIVE_KEY.into()), "LIVE_MAX_CONCURRENT_PER_OWNER" => opts.max_concurrent.map(Into::into), + "LIVE_MAX_GUESTS" => opts.max_guests.map(Into::into), _ => None, }) .unwrap() @@ -761,3 +764,378 @@ async fn a_live_conversations_room_cannot_be_the_subject_of_an_email_invite() { StatusCode::SERVICE_UNAVAILABLE ); } + +// ---- several guests on one invitation (spec 0121, S1) ----------------------------------- + +async fn create_with(srv: &Srv, jwt: &str, lang: &str, max_guests: Value) -> (StatusCode, Value) { + srv.post( + "/api/live/sessions", + Some(jwt), + json!({ "lang": lang, "max_guests": max_guests }), + ) + .await +} + +async fn guest_rows(srv: &Srv, session: &str) -> i64 { + sqlx::query_scalar( + "SELECT count(*) FROM live_participants WHERE session_id = $1::uuid AND role = 'guest'", + ) + .bind(session) + .fetch_one(&srv.pool) + .await + .unwrap() +} + +#[tokio::test] +async fn a_session_defaults_to_one_guest_and_says_how_many_it_admits() { + let srv = srv!(Opts::default()); + let (jwt, _) = login(&srv, "Marco").await; + + let (s, created) = create_b2c(&srv, &jwt, "it").await; + assert_eq!(s, StatusCode::OK, "{created}"); + assert_eq!(created["max_guests"], 1, "today's behaviour is the default"); + let sid = created["session_id"].as_str().unwrap(); + let stored: i16 = + sqlx::query_scalar("SELECT max_guests FROM live_sessions WHERE id = $1::uuid") + .bind(sid) + .fetch_one(&srv.pool) + .await + .unwrap(); + assert_eq!(stored, 1); + + // `null` is "not given". + let (s, created) = create_with(&srv, &jwt, "it", Value::Null).await; + assert_eq!( + (s, created["max_guests"].as_i64()), + (StatusCode::OK, Some(1)) + ); + + // Asking for three is persisted, returned, and visible to every participant. + let (s, created) = create_with(&srv, &jwt, "it", json!(3)).await; + assert_eq!(s, StatusCode::OK, "{created}"); + assert_eq!(created["max_guests"], 3); + let (_, joined) = join(&srv, &code_of(&created), "Lucia", "es").await; + assert_eq!(joined["max_guests"], 3, "the guest is told too"); + let id = created["session_id"].as_str().unwrap(); + let (_, view) = srv + .get( + &format!("/api/live/sessions/{id}"), + joined["token"].as_str().unwrap(), + ) + .await; + assert_eq!(view["session"]["max_guests"], 3); +} + +#[tokio::test] +async fn a_bad_guest_count_is_refused_with_a_stable_code_and_creates_nothing() { + let srv = srv!(Opts::default()); + let (jwt, user) = login(&srv, "Marco").await; + for bad in [ + json!(0), + json!(4), + json!(-1), + json!(99), + json!("2"), + json!(1.5), + json!(true), + json!([2]), + ] { + let (s, b) = create_with(&srv, &jwt, "it", bad.clone()).await; + assert_eq!( + (s, b["code"].as_str()), + (StatusCode::BAD_REQUEST, Some("bad_max_guests")), + "{bad}" + ); + } + let made: i64 = + sqlx::query_scalar("SELECT count(*) FROM live_sessions WHERE owner_user_id = $1") + .bind(user) + .fetch_one(&srv.pool) + .await + .unwrap(); + assert_eq!(made, 0, "a refused create leaves no session behind"); +} + +#[tokio::test] +async fn the_operator_can_lower_how_many_guests_a_host_may_ask_for() { + let srv = srv!(Opts { + max_guests: Some("2"), + ..Opts::default() + }); + let (jwt, _) = login(&srv, "Marco").await; + assert_eq!( + create_with(&srv, &jwt, "it", json!(2)).await.0, + StatusCode::OK + ); + let (s, b) = create_with(&srv, &jwt, "it", json!(3)).await; + assert_eq!( + (s, b["code"].as_str()), + (StatusCode::BAD_REQUEST, Some("bad_max_guests")) + ); +} + +#[tokio::test] +async fn one_invitation_admits_guests_one_after_the_other_each_with_a_seat_of_their_own() { + let srv = srv!(Opts::default()); + let (jwt, _) = login(&srv, "Marco").await; + let (_, created) = create_with(&srv, &jwt, "it", json!(3)).await; + let code = code_of(&created); + let sid = created["session_id"].as_str().unwrap(); + + let mut seats = Vec::new(); + for (name, lang) in [("Lucia", "es"), ("Pablo", "es"), ("Ann", "en")] { + let (s, joined) = join(&srv, &code, name, lang).await; + assert_eq!(s, StatusCode::OK, "{name}: {joined}"); + assert_eq!(joined["session_id"], created["session_id"]); + seats.push(( + joined["peer_id"].as_str().unwrap().to_string(), + joined["token"].as_str().unwrap().to_string(), + )); + } + // Three different seats and three different credentials, each with its own consent. + let mut peers: Vec<_> = seats.iter().map(|(p, _)| p.clone()).collect(); + let mut tokens: Vec<_> = seats.iter().map(|(_, t)| t.clone()).collect(); + peers.sort(); + peers.dedup(); + tokens.sort(); + tokens.dedup(); + assert_eq!((peers.len(), tokens.len()), (3, 3)); + let consented: i64 = sqlx::query_scalar( + "SELECT count(*) FROM live_participants + WHERE session_id = $1::uuid AND role = 'guest' AND consent_at IS NOT NULL AND token_hash IS NOT NULL", + ) + .bind(sid) + .fetch_one(&srv.pool) + .await + .unwrap(); + assert_eq!(consented, 3); + + // Everybody sees everybody, in join order, host first. + let (_, view) = srv + .get(&format!("/api/live/sessions/{sid}"), &seats[2].1) + .await; + let people: Vec<(&str, &str)> = view["session"]["participants"] + .as_array() + .unwrap() + .iter() + .map(|p| { + ( + p["display_name"].as_str().unwrap(), + p["lang"].as_str().unwrap(), + ) + }) + .collect(); + assert_eq!( + people, + vec![ + ("Marco", "it"), + ("Lucia", "es"), + ("Pablo", "es"), + ("Ann", "en") + ] + ); + assert_eq!(view["session"]["status"], "active"); + + // A fourth guest finds the conversation full, with the same stable code as before. + let (s, b) = join(&srv, &code, "Late", "fr").await; + assert_eq!( + (s, b["code"].as_str()), + (StatusCode::CONFLICT, Some("session_full")) + ); + assert_eq!(guest_rows(&srv, sid).await, 3, "and took no seat"); +} + +#[tokio::test] +async fn a_session_that_admits_two_is_full_at_two() { + let srv = srv!(Opts::default()); + let (jwt, _) = login(&srv, "Marco").await; + let (_, created) = create_with(&srv, &jwt, "it", json!(2)).await; + let code = code_of(&created); + assert_eq!(join(&srv, &code, "A", "es").await.0, StatusCode::OK); + assert_eq!(join(&srv, &code, "B", "en").await.0, StatusCode::OK); + let (s, b) = join(&srv, &code, "C", "fr").await; + assert_eq!( + (s, b["code"].as_str()), + (StatusCode::CONFLICT, Some("session_full")) + ); +} + +#[tokio::test] +async fn simultaneous_joins_admit_exactly_as_many_guests_as_the_session_allows() { + let srv = Arc::new(srv!(Opts::default())); + let (jwt, _) = login(&srv, "Marco").await; + for max in [1i64, 3] { + let (_, created) = create_with(&srv, &jwt, "it", json!(max)).await; + let code = code_of(&created); + let sid = created["session_id"].as_str().unwrap().to_string(); + let mut tasks = Vec::new(); + for i in 0..10 { + let (srv, code) = (srv.clone(), code.clone()); + tasks.push(tokio::spawn(async move { + join(&srv, &code, &format!("G{i}"), "es").await + })); + } + let (mut ok, mut full) = (0, 0); + for t in tasks { + let (s, b) = t.await.unwrap(); + match (s, b["code"].as_str()) { + (StatusCode::OK, _) => ok += 1, + (StatusCode::CONFLICT, Some("session_full")) => full += 1, + other => panic!("unexpected answer {other:?}"), + } + } + assert_eq!((ok, full), (max, 10 - max), "max_guests = {max}"); + assert_eq!(guest_rows(&srv, &sid).await, max, "the database agrees"); + // Ending frees the owner's concurrent-session slot for the next round. + sqlx::query("UPDATE live_sessions SET status = 'ended' WHERE id = $1::uuid") + .bind(&sid) + .execute(&srv.pool) + .await + .unwrap(); + } +} + +#[tokio::test] +async fn the_join_window_closes_on_a_session_nobody_joined_but_not_on_one_in_progress() { + let srv = srv!(Opts::default()); + let (jwt, _) = login(&srv, "Marco").await; + let (_, created) = create_with(&srv, &jwt, "it", json!(3)).await; + let code = code_of(&created); + let sid = created["session_id"].as_str().unwrap(); + assert_eq!(join(&srv, &code, "First", "es").await.0, StatusCode::OK); + + // The window the first guest had is long over; the conversation is under way, so the same + // link still admits the next one. + sqlx::query( + "UPDATE live_sessions SET expires_at = now() - interval '1 hour' WHERE id = $1::uuid", + ) + .bind(sid) + .execute(&srv.pool) + .await + .unwrap(); + let (s, b) = join(&srv, &code, "Second", "en").await; + assert_eq!(s, StatusCode::OK, "{b}"); + + // Once it has ended, the link is dead, whatever seats were left. + let tok = created["token"].as_str().unwrap(); + assert_eq!( + srv.post( + &format!("/api/live/sessions/{sid}/end"), + Some(tok), + json!({}) + ) + .await + .0, + StatusCode::OK + ); + let (s, b) = join(&srv, &code, "Third", "fr").await; + assert_eq!( + (s, b["code"].as_str()), + (StatusCode::GONE, Some("session_ended")) + ); +} + +#[tokio::test] +async fn a_guest_leaving_a_shared_conversation_does_not_end_it_for_the_others() { + let srv = srv!(Opts::default()); + let (jwt, _) = login(&srv, "Marco").await; + let (_, created) = create_with(&srv, &jwt, "it", json!(2)).await; + let code = code_of(&created); + let sid = created["session_id"].as_str().unwrap(); + let (_, a) = join(&srv, &code, "A", "es").await; + let (_, b) = join(&srv, &code, "B", "en").await; + + let (s, left) = srv + .post( + &format!("/api/live/sessions/{sid}/end"), + Some(a["token"].as_str().unwrap()), + json!({}), + ) + .await; + assert_eq!(s, StatusCode::OK); + assert_eq!( + (left["ended"].as_bool(), left["left"].as_bool()), + (Some(false), Some(true)) + ); + let (s, view) = srv + .get( + &format!("/api/live/sessions/{sid}"), + b["token"].as_str().unwrap(), + ) + .await; + assert_eq!( + s, + StatusCode::OK, + "the other guest's seat is still good: {view}" + ); + assert_eq!(view["session"]["status"], "active"); + + // The host's end is the end for everyone. + let (_, ended) = srv + .post( + &format!("/api/live/sessions/{sid}/end"), + Some(created["token"].as_str().unwrap()), + json!({}), + ) + .await; + assert_eq!(ended["ended"], true); + let (s, _) = srv + .get( + &format!("/api/live/sessions/{sid}"), + b["token"].as_str().unwrap(), + ) + .await; + assert_eq!(s, StatusCode::GONE); +} + +#[tokio::test] +async fn a_join_the_engine_cannot_serve_is_refused_busy_instead_of_a_direction_going_silent() { + // Three upstream sessions in all. A pair needs two; a third language needs four more. + let srv = srv!(Opts { + qwen_max_sessions: Some(3), + ..Opts::default() + }); + let (jwt, _) = login(&srv, "Marco").await; + let (_, created) = create_with(&srv, &jwt, "it", json!(3)).await; + let code = code_of(&created); + let sid = created["session_id"].as_str().unwrap(); + + assert_eq!(join(&srv, &code, "Lucia", "es").await.0, StatusCode::OK); + let (s, b) = join(&srv, &code, "Ann", "en").await; + assert_eq!( + (s, b["code"].as_str()), + (StatusCode::SERVICE_UNAVAILABLE, Some("live_busy")), + "a third language would need more sessions than the engine has" + ); + assert_eq!( + guest_rows(&srv, sid).await, + 1, + "the refused join took no seat" + ); + + // The same language as a guest already there needs no new stream: it still fits. + assert_eq!(join(&srv, &code, "Pablo", "es").await.0, StatusCode::OK); + assert_eq!(guest_rows(&srv, sid).await, 2); +} + +#[tokio::test] +async fn a_business_session_takes_guests_too() { + let srv = srv!(Opts::default()); + let (_, owner) = login(&srv, "Owner").await; + let (jwt, member) = login(&srv, "Receptionist").await; + let org = org_with_member(&srv, owner, Some(member)).await; + + let (s, created) = srv + .post( + "/api/live/sessions", + Some(&jwt), + json!({ "lang": "it", "org_id": org.id, "project_id": org.project, "max_guests": 2 }), + ) + .await; + assert_eq!(s, StatusCode::OK, "{created}"); + assert_eq!(created["max_guests"], 2); + let code = code_of(&created); + assert_eq!(join(&srv, &code, "A", "es").await.0, StatusCode::OK); + assert_eq!(join(&srv, &code, "B", "en").await.0, StatusCode::OK); + assert_eq!(join(&srv, &code, "C", "fr").await.0, StatusCode::CONFLICT); +} diff --git a/server/tests/live_schema.rs b/server/tests/live_schema.rs index b9800801..a3c2c88d 100644 --- a/server/tests/live_schema.rs +++ b/server/tests/live_schema.rs @@ -1,9 +1,10 @@ -//! Schema guarantees for Live Connect (spec 0121), migration `065_live_connect.sql`. +//! Schema guarantees for Live Connect (spec 0121), migrations `065_live_connect.sql` and +//! `069_live_multi_guest.sql`. //! //! The properties that matter are the ones the application leans on and would otherwise -//! have to re-check in Rust: a session always has an owner, a session holds at most two -//! participants (one per side), a billing tick can be written once, and purging the -//! conversation never erases the usage record. +//! have to re-check in Rust: a session always has an owner, a session has one owner-side seat +//! (how many guests it admits is the application's rule, `max_guests`, enforced under a row lock), +//! a billing tick can be written once, and purging the conversation never erases the usage record. //! //! Every test is **DB-gated**: it no-ops when `DATABASE_URL` is unset. Run against the //! local pgvector Postgres (`docker compose up -d --wait postgres`): @@ -164,7 +165,7 @@ async fn status_and_role_vocabularies_are_enforced() { } #[tokio::test] -async fn a_session_holds_at_most_two_participants_one_per_side() { +async fn a_session_has_one_owner_side_and_the_database_no_longer_caps_guests() { let Some(pool) = setup().await else { eprintln!("skipping — no DATABASE_URL"); return; @@ -180,18 +181,99 @@ async fn a_session_holds_at_most_two_participants_one_per_side() { .expect_err("owner side is full"); assert!(is_violation(&err, "23505"), "{role}: {err:?}"); } - add_participant(&pool, session, "guest").await.unwrap(); - let err = add_participant(&pool, session, "guest") + // Guests are limited by the application, under a row lock (`join_guest`), because a unique + // index cannot say "at most N": the table itself takes as many as it is given. + for _ in 0..3 { + add_participant(&pool, session, "guest").await.unwrap(); + } + let n: i64 = sqlx::query_scalar("SELECT count(*) FROM live_participants WHERE session_id = $1") + .bind(session) + .fetch_one(&pool) .await - .expect_err("guest side is full"); - assert!(is_violation(&err, "23505")); + .unwrap(); + assert_eq!(n, 4); +} - let n: i64 = sqlx::query_scalar("SELECT count(*) FROM live_participants WHERE session_id = $1") +#[tokio::test] +async fn a_session_admits_one_to_three_guests_and_existing_sessions_keep_one() { + let Some(pool) = setup().await else { + eprintln!("skipping — no DATABASE_URL"); + return; + }; + let owner = make_user(&pool).await; + // A session created the way every session was before 069 (no mention of the column). + let session = make_session(&pool, owner).await; + let max: i16 = sqlx::query_scalar("SELECT max_guests FROM live_sessions WHERE id = $1") .bind(session) .fetch_one(&pool) .await .unwrap(); - assert_eq!(n, 2); + assert_eq!(max, 1, "what a conversation already was is what it stays"); + + for ok in [1i16, 2, 3] { + sqlx::query("UPDATE live_sessions SET max_guests = $2 WHERE id = $1") + .bind(session) + .bind(ok) + .execute(&pool) + .await + .unwrap(); + } + for bad in [0i16, 4, -1] { + let err = sqlx::query("UPDATE live_sessions SET max_guests = $2 WHERE id = $1") + .bind(session) + .bind(bad) + .execute(&pool) + .await + .expect_err("outside 1..=3"); + assert!(is_violation(&err, "23514"), "{bad}: {err:?}"); + } +} + +#[tokio::test] +async fn the_multi_guest_migration_is_idempotent_and_survives_065_being_run_again() { + let Some(pool) = setup().await else { + eprintln!("skipping — no DATABASE_URL"); + return; + }; + let _turn = DDL.lock().await; + let owner = make_user(&pool).await; + let session = make_session(&pool, owner).await; + for _ in 0..3 { + add_participant(&pool, session, "guest").await.unwrap(); + } + // 069 twice more, then 065 again (as `migration_is_idempotent` and a drifted database do): + // the second must find the guest-side index's NAME taken and leave it alone, rather than + // rebuild a unique index over a conversation that has three guests. + let m069 = include_str!("../migrations/069_live_multi_guest.sql"); + sqlx::raw_sql(m069) + .execute(&pool) + .await + .expect("069 must be re-runnable"); + // Not the whole file: 065 re-run end to end takes locks table by table and deadlocks with + // tests inserting rows (see `DDL`); the statement that matters is this one, verbatim. + let m065 = include_str!("../migrations/065_live_connect.sql"); + let stmt = m065 + .split(';') + .find(|s| s.contains("CREATE UNIQUE INDEX IF NOT EXISTS idx_live_participants_guest_side")) + .expect("065 still creates the guest-side index"); + sqlx::raw_sql(stmt) + .execute(&pool) + .await + .expect("065's index statement must stay a no-op once guests share a session"); + let unique: Option = sqlx::query_scalar( + "SELECT i.indisunique FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid + WHERE c.relname = 'idx_live_participants_guest_side'", + ) + .fetch_optional(&pool) + .await + .unwrap(); + assert_eq!(unique, Some(false), "the index stays, no longer unique"); + // The owner-side unique index is untouched: still exactly one host or staff. + add_participant(&pool, session, "host").await.unwrap(); + let err = add_participant(&pool, session, "staff") + .await + .expect_err("one owner side"); + assert!(is_violation(&err, "23505")); } #[tokio::test] diff --git a/server/tests/live_summary.rs b/server/tests/live_summary.rs index 57c3502d..23d73038 100644 --- a/server/tests/live_summary.rs +++ b/server/tests/live_summary.rs @@ -1378,3 +1378,72 @@ async fn the_conversation_never_reaches_the_logs_even_when_the_provider_quotes_i ); } } + +// ---- several guests in one conversation (spec 0121, S1) -------------------------------- + +#[tokio::test] +async fn a_summary_of_a_conversation_with_several_guests_reads_every_speaker_in_their_language() { + let srv = srv!(); + let w = world(&srv, 100).await; + let s = seed(&srv, &w, "ended").await; + small_talk(&srv, &s).await; + // Two more guests, one of them in a language already there. + let t0 = Utc::now() - Span::minutes(9); + for (i, (name, lang, text)) in [ + ("Ben", "en", "Is breakfast included?"), + ("Pablo", "es", "Y el aparcamiento?"), + ] + .into_iter() + .enumerate() + { + let peer: Uuid = sqlx::query_scalar( + "INSERT INTO live_participants (session_id, role, display_name, lang, joined_at) + VALUES ($1, 'guest', $2, $3, now() + make_interval(mins => $4)) RETURNING peer_id", + ) + .bind(s.session) + .bind(name) + .bind(lang) + .bind(i as i32 + 1) + .fetch_one(&srv.pool) + .await + .unwrap(); + sqlx::query( + "INSERT INTO transcript_events (session_id, event_type, speaker_peer_id, speaker_name, original_text, original_lang, translations, ts) + VALUES ($1, 'speech', $2, $3, $4, $5, '{}'::jsonb, $6)", + ) + .bind(s.call) + .bind(peer.to_string()) + .bind(name) + .bind(text) + .bind(lang) + .bind(t0 + Span::seconds(25 + i as i64 * 5)) + .execute(&srv.pool) + .await + .unwrap(); + } + + // The conversation is readable (a scalar subquery over "the" guest would have failed here) + // and everything each of the four said reaches the model, with who and in which language. + let (status, body) = srv + .post( + &path(w.org, s.session), + &w.admin.0, + Some(json!({"lang": "en"})), + ) + .await; + assert_eq!(status, StatusCode::OK, "{body}"); + let sent = srv.groq.last_user(); + for needle in [ + "Anna (it)", + "Lucía (es)", + "Ben (en)", + "Pablo (es)", + "Is breakfast included?", + "Y el aparcamiento?", + ] { + assert!(sent.contains(needle), "{needle:?} missing from {sent}"); + } + // The member who sat in it still gets a summary in their own language. + let (_, mine) = srv.post(&path(w.org, s.session), &w.sat.0, None).await; + assert_eq!(mine["lang"], "fr"); +} diff --git a/server/tests/live_ws.rs b/server/tests/live_ws.rs index 204df246..6ad84665 100644 --- a/server/tests/live_ws.rs +++ b/server/tests/live_ws.rs @@ -1566,3 +1566,559 @@ async fn with_the_echo_filter_off_the_same_scene_plays_the_echo_back() { "with the filter off the host hears the guest's session" ); } + +// ---- one host, several guests, languages free (spec 0121, S1/S2/S3) --------------------- + +use futures::stream::SplitSink; +use tokio::sync::mpsc; + +/// One phone of a many-seat conversation: its socket split in two so that a test can feed every +/// microphone and read every screen in the same loop. Everything it receives is kept in `seen`. +struct Phone { + peer: String, + lang: &'static str, + tx: SplitSink, + rx: mpsc::UnboundedReceiver, + seen: Vec, +} + +impl Phone { + async fn connect(srv: &Srv, token: &str, peer: &str, lang: &'static str) -> Phone { + let ws = connect(srv, token).await.expect("upgrade"); + let (tx, mut stream) = ws.split(); + let (out, rx) = mpsc::unbounded_channel(); + tokio::spawn(async move { + while let Some(Ok(frame)) = stream.next().await { + match frame { + Message::Text(t) => { + if let Ok(v) = serde_json::from_str::(t.as_str()) { + if out.send(v).is_err() { + return; + } + } + } + Message::Close(_) => return, + _ => {} + } + } + }); + Phone { + peer: peer.into(), + lang, + tx, + rx, + seen: Vec::new(), + } + } + + /// Move whatever has arrived into `seen`. + fn pull(&mut self) { + while let Ok(v) = self.rx.try_recv() { + self.seen.push(v); + } + } + + /// Wait (up to 8 s) for a frame matching `wanted`, whether it was already seen or not. + async fn wait_for(&mut self, wanted: impl Fn(&Value) -> bool) -> Option { + self.pull(); + if let Some(v) = self.seen.iter().find(|v| wanted(v)) { + return Some(v.clone()); + } + while let Ok(Some(v)) = tokio::time::timeout(Duration::from_secs(8), self.rx.recv()).await { + self.seen.push(v.clone()); + if wanted(&v) { + return Some(v); + } + } + None + } + + async fn say(&mut self, v: Value) { + let _ = self.tx.send(Message::text(v.to_string())).await; + } +} + +/// A conversation of `1 + guest_langs.len()` phones: a host (`it`) and one guest per language. +struct Many { + session: Uuid, + host_user: Uuid, + host_token: String, + host_peer: String, + /// `(token, peer, lang)` per guest, in join order. + guests: Vec<(String, String, &'static str)>, +} + +async fn many(srv: &Srv, guest_langs: &[&'static str], save_transcript: bool) -> Many { + let (jwt, host_user) = login(srv).await; + let (s, created) = srv + .post( + "/api/live/sessions", + Some(&jwt), + json!({ + "lang": "it", + "save_transcript": save_transcript, + "max_guests": guest_langs.len(), + }), + ) + .await; + assert_eq!(s, StatusCode::OK, "{created}"); + let code = created["invite_url"] + .as_str() + .unwrap() + .rsplit('/') + .next() + .unwrap() + .to_string(); + let mut guests = Vec::new(); + for (i, lang) in guest_langs.iter().enumerate() { + let (s, joined) = srv + .post( + "/api/live/join", + None, + json!({ "code": code, "name": format!("Guest{i}"), "lang": lang, "consent": true }), + ) + .await; + assert_eq!(s, StatusCode::OK, "{joined}"); + guests.push(( + joined["token"].as_str().unwrap().into(), + joined["peer_id"].as_str().unwrap().into(), + *lang, + )); + } + Many { + session: created["session_id"].as_str().unwrap().parse().unwrap(), + host_user, + host_token: created["token"].as_str().unwrap().into(), + host_peer: created["peer_id"].as_str().unwrap().into(), + guests, + } +} + +/// Every phone of `m`, connected in order (host first) and each told `room_joined`. +async fn open_all(srv: &Srv, m: &Many) -> Vec { + let mut phones = vec![Phone::connect(srv, &m.host_token, &m.host_peer, "it").await]; + for (token, peer, lang) in &m.guests { + phones.push(Phone::connect(srv, token, peer, lang).await); + } + for p in &mut phones { + p.wait_for(|v| v["type"] == "room_joined") + .await + .expect("room_joined"); + } + phones +} + +/// Every phone feeds its microphone for `ms` (`mic(i)` is phone `i`'s 100 ms frame, `None` = it +/// stays quiet) while every screen is read. +async fn all_speak(phones: &mut [Phone], ms: u64, mic: impl Fn(usize) -> Option>) { + let deadline = tokio::time::Instant::now() + Duration::from_millis(ms); + while tokio::time::Instant::now() < deadline { + for (i, p) in phones.iter_mut().enumerate() { + if let Some(frame) = mic(i) { + let _ = p.tx.send(Message::binary(frame)).await; + } + p.pull(); + } + tokio::time::sleep(Duration::from_millis(100)).await; + } + for p in phones.iter_mut() { + p.pull(); + } +} + +#[tokio::test] +async fn four_phones_in_three_languages_each_hear_every_other_speaker_in_their_own() { + let mock = RealtimeMock::start(Dialect::Qwen).await; + mock.reply_with(speech()); + let srv = srv!(&mock); + let m = many(&srv, &["es", "es", "en"], false).await; + let mut phones = open_all(&srv, &m).await; + for p in &mut phones { + p.say(json!({ "type": "start" })).await; + } + + // Nobody takes turns: all four talk at once. + all_speak(&mut phones, 6_000, |_| Some(vec![0u8; 640])).await; + + for (l, listener) in phones.iter().enumerate() { + for (s, speaker) in phones.iter().enumerate() { + let heard = audio_from(&listener.seen, &speaker.peer); + if l == s { + assert!(heard.is_empty(), "phone {l} was played its own voice"); + continue; + } + if listener.lang == speaker.lang { + // Two people who share a language just hear each other: nothing is translated, + // so nothing is played, and nothing is billed for it. + assert!( + heard.is_empty(), + "phone {l} and phone {s} share {} and need no translation", + listener.lang + ); + continue; + } + assert!( + !heard.is_empty(), + "phone {l} ({}) never heard phone {s} ({}): {:?}", + listener.lang, + speaker.lang, + listener + .seen + .iter() + .map(|v| v["type"].clone()) + .collect::>() + ); + assert!( + heard.iter().all(|v| v["lang"] == listener.lang), + "phone {l} must hear phone {s} in {}, got {:?}", + listener.lang, + heard.iter().map(|v| v["lang"].clone()).collect::>() + ); + } + // And sees who is speaking: a caption per other speaker. + for (s, speaker) in phones.iter().enumerate() { + if l != s && listener.lang != speaker.lang { + assert!( + listener.seen.iter().any(|v| v["type"] + .as_str() + .is_some_and(|t| t.starts_with("subtitle")) + && v["speaker_id"] == speaker.peer), + "phone {l} got no caption of phone {s}" + ); + } + } + } +} + +#[tokio::test] +async fn the_room_tells_every_phone_who_is_there_who_arrives_who_leaves_and_who_mutes() { + let mock = RealtimeMock::start(Dialect::Qwen).await; + let srv = srv!(&mock); + let m = many(&srv, &["es", "es", "en"], false).await; + let mut phones = open_all(&srv, &m).await; + + // Each phone learns of the ones already there from `room_joined`, and of later ones from + // `peer_joined`: the last to arrive sees three, the first sees three arrivals. + let joined = phones[3] + .seen + .iter() + .find(|v| v["type"] == "room_joined") + .unwrap()["peers"] + .as_array() + .unwrap() + .len(); + assert_eq!(joined, 3); + phones[0].pull(); + let arrivals: Vec<&str> = phones[0] + .seen + .iter() + .filter(|v| v["type"] == "peer_joined") + .map(|v| v["peer_id"].as_str().unwrap()) + .collect(); + assert_eq!(arrivals.len(), 3, "{arrivals:?}"); + + // A mute is told to everybody else, not just one partner. + phones[1] + .say(json!({ "type": "mute_audio", "muted": true })) + .await; + let want = phones[1].peer.clone(); + for i in [0usize, 2, 3] { + let v = phones[i] + .wait_for(|v| v["type"] == "peer_muted" && v["peer_id"] == want.as_str()) + .await + .unwrap_or_else(|| panic!("phone {i} was never told the guest muted")); + assert_eq!(v["muted"], true); + } + + // One guest leaves: the three others each hear about it, and the conversation goes on. + let mut leaving = phones.pop().unwrap(); + let gone = leaving.peer.clone(); + let _ = leaving.tx.close().await; + for (i, p) in phones.iter_mut().enumerate() { + p.wait_for(|v| v["type"] == "peer_left" && v["peer_id"] == gone.as_str()) + .await + .unwrap_or_else(|| panic!("phone {i} never learnt the guest left")); + } + let (status, _) = status_of(&srv, m.session).await; + assert_eq!(status, "active", "one guest leaving is not the end"); +} + +#[tokio::test] +async fn a_guest_who_reconnects_keeps_their_seat_and_the_others_never_see_them_leave() { + let mock = RealtimeMock::start(Dialect::Qwen).await; + let srv = srv!(&mock); + let m = many(&srv, &["es", "en"], false).await; + let mut phones = open_all(&srv, &m).await; + + // The second guest's phone drops and comes back on the same token before the stale socket timed out. + let (token, peer, lang) = m.guests[1].clone(); + let mut again = Phone::connect(&srv, &token, &peer, lang).await; + again + .wait_for(|v| v["type"] == "room_joined") + .await + .expect("reconnected"); + assert_eq!( + again + .seen + .iter() + .find(|v| v["type"] == "room_joined") + .unwrap()["peers"] + .as_array() + .unwrap() + .len(), + 2, + "back in the same room with the two others" + ); + + // A replaced socket is a replacement, not a departure. + tokio::time::sleep(Duration::from_millis(1500)).await; + for (i, p) in phones.iter_mut().enumerate().take(2) { + p.pull(); + assert!( + !p.seen.iter().any(|v| v["type"] == "peer_left"), + "phone {i} saw the reconnecting guest leave: {:?}", + p.seen + ); + } + // The stale socket is told to go (its reader ends once the server closes it). + let stale = &mut phones[2]; + let closed = tokio::time::timeout(Duration::from_secs(8), async { + while stale.rx.recv().await.is_some() {} + }) + .await; + assert!(closed.is_ok(), "the stale socket was never closed"); +} + +#[tokio::test] +async fn every_phone_has_its_own_ticks_and_the_owner_pays_one_stream_per_distinct_language() { + let mock = RealtimeMock::start(Dialect::Qwen).await; + mock.reply_with(speech()); + let srv = srv!(&mock); + let rate = srv + .state + .engines + .default() + .metadata() + .user_rate_per_second(); + + // host it, guests es, es, en: every speaker has two OTHER languages in the room. + // host it, guests es, es : every speaker has ONE other language (two Spanish guests are + // not two streams for the host). + for (guest_langs, expected_streams) in [ + (vec!["es", "es", "en"], vec![2.0, 2.0, 2.0, 2.0]), + (vec!["es", "es"], vec![1.0, 1.0, 1.0]), + ] { + let m = many(&srv, &guest_langs, false).await; + let mut phones = open_all(&srv, &m).await; + for p in &mut phones { + p.say(json!({ "type": "start" })).await; + } + all_speak(&mut phones, 3_500, |_| Some(vec![0u8; 640])).await; + for p in &mut phones { + p.say(json!({ "type": "stop" })).await; + } + tokio::time::sleep(Duration::from_millis(800)).await; + + let mut seats = vec![m.host_peer.clone()]; + seats.extend(m.guests.iter().map(|g| g.1.clone())); + for (peer, streams) in seats.iter().zip(&expected_streams) { + let (n, lo, hi): (i64, Option, Option) = sqlx::query_as( + "SELECT count(*), min(amount), max(amount) FROM live_usage_ticks + WHERE session_id = $1 AND peer_id = $2::uuid", + ) + .bind(m.session) + .bind(peer) + .fetch_one(&srv.pool) + .await + .unwrap(); + assert!(n >= 2, "{guest_langs:?}: {peer} was metered {n} times"); + let unit = usd(rate * 1.0 * streams); + assert_eq!( + (lo, hi), + (Some(unit), Some(unit)), + "{guest_langs:?}: {peer} must pay for {streams} stream(s) a second" + ); + } + + // Exactly the ticks were debited from the owner, to the last digit. + let (ticks, tick_sum): (i64, Decimal) = sqlx::query_as( + "SELECT count(*), COALESCE(sum(amount), 0) FROM live_usage_ticks + WHERE session_id = $1 AND payer_kind = 'user'", + ) + .bind(m.session) + .fetch_one(&srv.pool) + .await + .unwrap(); + let (rows, spent): (i64, Decimal) = sqlx::query_as( + "SELECT count(*), COALESCE(sum(-amount), 0) FROM credit_transactions + WHERE user_id = $1 AND kind = 'usage'", + ) + .bind(m.host_user) + .fetch_one(&srv.pool) + .await + .unwrap(); + assert_eq!((rows, spent), (ticks, tick_sum), "{guest_langs:?}"); + let left: Decimal = sqlx::query_scalar("SELECT balance FROM users WHERE id = $1") + .bind(m.host_user) + .fetch_one(&srv.pool) + .await + .unwrap(); + assert_eq!(left, Decimal::new(500, 2) - tick_sum, "{guest_langs:?}"); + // Guests are never debited and never see the owner's money. + for p in phones.iter().skip(1) { + assert!( + !p.seen.iter().any(|v| matches!( + v["type"].as_str(), + Some("balance_update" | "low_balance" | "session_spent") + )), + "a guest was told what the owner pays" + ); + } + } +} + +#[tokio::test] +async fn one_utterance_heard_by_three_phones_is_one_latency_observation() { + let _alone = VOICED_SPEECH.lock().await; + let mock = RealtimeMock::start(Dialect::Qwen).await; + mock.reply_with(speech()); + let srv = srv!(&mock); + let m = many(&srv, &["es", "es", "en"], false).await; + let (before, _) = live_latency(&srv).await; + let mut phones = open_all(&srv, &m).await; + phones[0].say(json!({ "type": "start" })).await; + + // Only the host talks (voiced, without a pause); three phones listen. + let voice: Vec = std::iter::repeat_n(1000i16.to_le_bytes(), 2400) + .flatten() + .collect(); + all_speak(&mut phones, 4_000, |i| (i == 0).then(|| voice.clone())).await; + let heard: usize = phones + .iter() + .skip(1) + .map(|p| audio_from(&p.seen, &m.host_peer).len()) + .sum(); + assert!( + heard >= 3, + "the three listeners should all hear the host: {heard}" + ); + + let (after, _) = live_latency(&srv).await; + assert_eq!( + after - before, + 1, + "one utterance is one observation, however many phones hear it" + ); +} + +#[tokio::test] +async fn with_three_seats_an_echo_is_dropped_whoever_it_reaches_and_a_real_reply_still_gets_through( +) { + let _alone = VOICED_SPEECH.lock().await; + let mock = RealtimeMock::start(Dialect::Qwen).await; + // Every upstream session hears the same sentence: three phones side by side all pick up the + // host's voice, and the guests' sessions "translate" it. + mock.reply_with(vec![ + Reply::Transcript { + original: "ciao come stai oggi".into(), + translated: "hola como estas hoy".into(), + }, + Reply::Audio(vec![7u8; 960]), + ]); + let srv = srv!(&mock); + let m = many(&srv, &["es", "en"], false).await; + let suppressed_before = echo_metric(&srv, "suppressed").await; + let mut phones = open_all(&srv, &m).await; + let (host, g1, g2) = ( + phones[0].peer.clone(), + phones[1].peer.clone(), + phones[2].peer.clone(), + ); + + // The host speaks first and their own caption is up before anything else happens. + phones[0].say(json!({ "type": "start" })).await; + all_speak(&mut phones, 2_500, |i| (i == 0).then(|| tone(1000, 4800))).await; + // Now both guests' microphones open (quietly: they only hear the host across the table). + phones[1].say(json!({ "type": "start" })).await; + phones[2].say(json!({ "type": "start" })).await; + all_speak(&mut phones, 4_500, |i| { + Some(if i == 0 { + tone(1000, 4800) + } else { + tone(200, 4800) + }) + }) + .await; + + // Both guests still hear the host's real translation... + for i in [1usize, 2] { + assert!( + !audio_from(&phones[i].seen, &host).is_empty(), + "guest {i} never heard the host" + ); + } + // ...the host sees their own words but none of the guests' sessions (the host's voice, read back)... + assert!(phones[0] + .seen + .iter() + .any(|v| v["type"] == "subtitle_interim" && v["speaker_id"] == host.as_str())); + for guest in [&g1, &g2] { + let echoed = speech_of(&phones[0].seen, guest); + assert!( + echoed.is_empty(), + "the host was played their own sentence back: {echoed:?}" + ); + } + // ...and neither guest is handed the OTHER guest's reading of the host: that is the case a + // two-microphone filter could not tell, because it only knew "me" and "the other". + let g2_hears_g1 = speech_of(&phones[2].seen, &g1); + assert!( + g2_hears_g1.is_empty(), + "guest 2 got guest 1's echo: {g2_hears_g1:?}" + ); + let g1_hears_g2 = speech_of(&phones[1].seen, &g2); + assert!( + g1_hears_g2.is_empty(), + "guest 1 got guest 2's echo: {g1_hears_g2:?}" + ); + assert!(echo_metric(&srv, "suppressed").await > suppressed_before); + + // A genuine reply from guest 1 is a different sentence and reaches both others. + let room = voxtranslate_server::live::ws::room_name(m.session); + for to in [&host, &g2] { + for frame in [ + ServerMessage::SubtitleInterim { + speaker_id: g1.clone(), + speaker_name: "Guest0".into(), + text: "sto bene grazie".into(), + lang: "es".into(), + original: Some("estoy bien gracias".into()), + }, + ServerMessage::TranslatedAudio { + speaker_id: g1.clone(), + lang: "it".into(), + seq: 99, + pcm16_b64: "AAAA".into(), + }, + ] { + assert!(srv.state.rooms.relay_to_peer(&room, to, &frame.to_json())); + } + } + all_speak(&mut phones, 2_500, |i| { + Some(if i == 0 { + tone(1000, 4800) + } else { + tone(200, 4800) + }) + }) + .await; + for i in [0usize, 2] { + assert!( + phones[i] + .seen + .iter() + .any(|v| v["type"] == "subtitle_interim" && v["text"] == "sto bene grazie"), + "the real reply never reached phone {i}" + ); + } +} diff --git a/specs/0121-live-connect/spec.md b/specs/0121-live-connect/spec.md index 3ac22b4e..66d876af 100644 --- a/specs/0121-live-connect/spec.md +++ b/specs/0121-live-connect/spec.md @@ -40,6 +40,8 @@ What the audit (2026-11-19, branch `develop`) established and this spec relies o **Goals** - Two phones converse in two languages simultaneously, with no accounts required for the guest. +- **Multi-guest (2026-11, release 1.63):** one host and up to three guests on the SAME link/QR, + languages free, the owner paying for every seat per distinct language spoken (R11-R13). - B2C: the signed-in owner creates the session and pays for both directions. - B2B: permanent, revocable, regenerable desk QR codes; every scan creates a new session bound to an existing project and billed to the org wallet; conversations searchable like meetings. @@ -48,7 +50,8 @@ What the audit (2026-11-19, branch `develop`) established and this spec relies o **Non-Goals** - WebRTC audio between the phones (the server must receive each microphone regardless). - The Enhanced (Cartesia, client-direct) engine: it cannot serve guests. -- More than two participants per session; video; permanent raw-audio storage. +- More than four participants per session (host + 3 guests, because a room holds four peers); + video; permanent raw-audio storage. - Any claim of hardware support before real-device verification (M8). ## 3. Requirements @@ -62,9 +65,10 @@ What the audit (2026-11-19, branch `develop`) established and this spec relies o - **R3 — Two directions, concurrently.** *Given* two joined peers with different languages, *when* both speak at once, *then* each hears only the other's translation and neither direction waits for the other. -- **R4 — One invitation, one guest.** *Given* a guest already joined, *when* a second device uses - the same invitation, *then* it is refused (`session_full`); the first device can reconnect with - its own token. +- **R4 — One invitation, `max_guests` guests.** *Given* a session that admits `max_guests` guests + (default 1), *when* more devices use the same invitation than it admits, *then* the surplus are + refused (`session_full`); a guest who dropped can reconnect with their own token. With the default + of one this is exactly the original single-use link. - **R5 — Permanent desk QR.** *Given* an active desk, *when* anyone scans its QR, *then* a **new** session is created every time; the QR never resolves to an existing conversation. - **R6 — Revoke and regenerate.** *Given* a printed QR, *when* the desk is revoked, *then* scans are @@ -75,6 +79,17 @@ What the audit (2026-11-19, branch `develop`) established and this spec relies o - **R8 — Billing.** *Given* a session with two different languages, *then* usage for **both** directions is billed to the owner (user wallet or org wallet), once per tick even if a meter restarts, and never to the guest. +- **R11 — Several guests on one link.** *Given* a host who asked for `max_guests` 1-3, *when* guests + open the same link one after the other (or all at once), *then* exactly `max_guests` are admitted, + each with a seat, token and consent of their own, until the session ends or fills; the join window + only closes a session nobody joined. A join the engine could not serve is refused `live_busy`. +- **R12 — Everybody hears everybody.** *Given* 2-4 phones, *then* each hears every other speaker + translated into its own language (two sharing a language hear each other natively), is told who + joins, leaves and mutes, and a phone that reconnects keeps its seat. +- **R13 — The owner pays per distinct language.** *Given* a room of several languages, *then* each + seat's meter bills one stream per distinct language present other than its speaker's, at the + session tier's rate (host `it`, guests `es`/`es`/`en`: 2 streams for each seat), nothing when all + share the speaker's language, idempotently per `(session, peer, tick)`. - **R9 — Insufficient credit.** *Given* the payer runs out mid-call, *then* both phones are told (`balance_exhausted`), audio stops and the session ends cleanly. - **R10 — Concurrent sessions cannot overspend.** *Given* N sessions on one org wallet, *then* the @@ -123,7 +138,10 @@ What the audit (2026-11-19, branch `develop`) established and this spec relies o durable record of a meeting is `call_sessions`. Linking keeps transcripts, search, history, the meter and the org ledger (whose `session_id` must reference `call_sessions`) working unchanged; `ON DELETE SET NULL` lets retention purge the conversation without erasing usage. -3. **Server-minted identity.** Room code `live-`, peer id = participant uuid, max two +3. **Server-minted identity.** Room code `live-`, peer id = participant uuid. One owner-side + seat is a partial unique index; the guest count (`live_sessions.max_guests`, 1-3) is enforced in + code under a `FOR UPDATE` lock on the session row (migration 069 turned the guest-side unique + index into a plain one under the same name, so re-running 065 stays a no-op). Originally two participants enforced by two partial unique indexes. This removes the client-chosen peer id that lets any joiner evict another today. 4. **Guest credential = signed token with its own key and audience.** Invite code (192 bits, only @@ -134,8 +152,8 @@ What the audit (2026-11-19, branch `develop`) established and this spec relies o bits; the database keeps only `token_version`. Revoke is a status flag, regenerate bumps the version. Chosen over a stored hash so printed assets can be re-downloaded at any time. The QR carries a public handle and the MAC only — never an org id or account identifier. -6. **The owner pays for both directions; ticks are idempotent.** Guest peers get a meter whose - payer is the owner. A debit and its `live_usage_ticks` row commit in one transaction with +6. **The owner pays for every seat; ticks are idempotent.** Guest peers get a meter whose + payer is the owner, billing one stream per distinct target language present (R13). A debit and its `live_usage_ticks` row commit in one transaction with `ON CONFLICT DO NOTHING`, so a duplicated meter charges nothing twice. One meter per peer. 7. **Language is declared, not guessed.** Each phone sets its language; `auto` is not allowed. A wrong-language check is advisory only and never delays audio. JEV is not on the realtime path: @@ -159,6 +177,7 @@ been run on real devices (S7/M8 below). | S0 (M1) | Spec, docs, migration 065, tokens, config | `specs/0121-*`, `docs/live-connect.md`, `065_live_connect.sql`, `live/{config,tokens}.rs` | built | | S1 (M2) | Sessions REST, `/ws/live`, sweeper, engine wiring, capacity gate | `live/{sessions,routes,ws,sweeper,state}.rs` | built. **Not built**: `presence.rs` (presence is read from the room registry), `echo.rs` / the server `EchoLedger`, the `language_mismatch` and `echo_flush` frames (deferred until device measurements, see the tracking file, L5) | | S2 (M5) | Per-phone owner-paid meters, idempotent ticks, caps | `live/billing.rs`, migration 066 | built, independently verified (L7) | +| S2b (1.63) | Several guests per link: `max_guests`, race-safe reusable join, 4-seat room, per-seat mic levels for the echo filter, billing per distinct language, N-guest audit of dashboard/finalise/search/erase | `069_live_multi_guest.sql`, `live/{sessions,routes,ws,state,billing,dashboard}.rs` | built (server); client work is a separate unit; **not verified on real devices** | | S3 (M3) | Home entry, host/guest/desk-scan pages, audio, 84 locales | `client/src/{pages,scripts/live,scripts/i18n}` | built; one client defect pinned by e2e (partner id read as `peer_id`, the server sends `id`); legal copy and low-confidence locales awaiting review | | S4 (M4) | Finalize, transcripts, embeddings, filters, retention, erasure | `live/{finalize,erase}.rs`, `business/search.rs` | built, independently verified (L9) | | S5 (M6) | Dashboard area, desks, QR assets, console, usage | `dashboard/src/…` (submodule), `live/{desks,dashboard}.rs` | built | @@ -167,7 +186,7 @@ been run on real devices (S7/M8 below). ## 6. Testing & Verification -- **Schema (S0):** `tests/live_schema.rs` — RLS, owner check, vocabularies, two-seat cap, tick +- **Schema (S0):** `tests/live_schema.rs` — RLS, owner check, vocabularies, one owner-side seat, `max_guests` check, tick idempotency, purge-keeps-usage, desk uniqueness, migration re-run. - **Tokens/config (S0):** unit tests for forgery, expiry, audience, JWT confusion, desk MAC version binding, shape checks, flag/key/limit validation. @@ -188,12 +207,12 @@ been run on real devices (S7/M8 below). - Env: `LIVE_CONNECT_ENABLED`, `LIVE_TOKEN_KEY`, `LIVE_JOIN_WINDOW_SECS`, `LIVE_DESK_WAIT_SECS`, `LIVE_MAX_SESSION_MIN`, `LIVE_MAX_CONCURRENT_PER_OWNER`, `LIVE_RECONNECT_GRACE_SECS` `LIVE_DESK_MAX_WAITING`, `LIVE_FINALIZE_GRACE_SECS`, `LIVE_RETENTION_DEFAULT_DAYS`, - `LIVE_B2C_RETENTION_DAYS` (`server/.env.example`). -- Migrations 065-068 are additive and idempotent. Flag off → no route is reachable (but the two + `LIVE_B2C_RETENTION_DAYS`, `LIVE_MAX_GUESTS` (`server/.env.example`). +- Migrations 065-069 are additive and idempotent. Flag off → no route is reachable (but the two erase endpoints stay mounted). Enable and rollback steps: `docs/live-connect.md`, "Deployment and migration". - Rooms are in memory and the server is single-replica: a deploy drops live sessions; phones - reconnect within the grace. Capacity: Qwen's 32-session cap ≈ 16 simultaneous pairs. + reconnect within the grace. Capacity: Qwen's 32-session cap ≈ 16 simultaneous pairs; a bigger room needs one upstream session per speaker per distinct other language. - Rotating `LIVE_TOKEN_KEY` invalidates every printed desk QR and every guest token in flight. ## 8. Risks / Open Items From 77603e56dc53e5a79c9ab98f510f7416a989d1ae Mon Sep 17 00:00:00 2001 From: Micio Dev Date: Fri, 20 Nov 2026 12:55:53 +0000 Subject: [PATCH 04/14] feat(live): host and guest screens for one host and up to three guests The host chooses 1-3 guests at setup (max_guests); the session machine, captions, partners panel, direction line and invitation panel now work with several partners, and a guest leaving a multi-guest conversation sees a "you left" screen without ending it for the others. 18 new i18n keys are copied in English to every locale until they are translated. --- client/src/pages/live.astro | 37 ++- client/src/scripts/i18n/af.json | 20 +- client/src/scripts/i18n/am.json | 20 +- client/src/scripts/i18n/ar.json | 20 +- client/src/scripts/i18n/az.json | 20 +- client/src/scripts/i18n/be.json | 20 +- client/src/scripts/i18n/bg.json | 20 +- client/src/scripts/i18n/bn.json | 20 +- client/src/scripts/i18n/bs.json | 20 +- client/src/scripts/i18n/ca.json | 20 +- client/src/scripts/i18n/ckb.json | 20 +- client/src/scripts/i18n/cs.json | 20 +- client/src/scripts/i18n/cy.json | 20 +- client/src/scripts/i18n/da.json | 20 +- client/src/scripts/i18n/de.json | 20 +- client/src/scripts/i18n/el.json | 20 +- client/src/scripts/i18n/en.json | 20 +- client/src/scripts/i18n/es.json | 20 +- client/src/scripts/i18n/et.json | 20 +- client/src/scripts/i18n/eu.json | 20 +- client/src/scripts/i18n/fa.json | 20 +- client/src/scripts/i18n/fi.json | 20 +- client/src/scripts/i18n/fil.json | 20 +- client/src/scripts/i18n/fr.json | 20 +- client/src/scripts/i18n/ga.json | 20 +- client/src/scripts/i18n/gl.json | 20 +- client/src/scripts/i18n/gu.json | 20 +- client/src/scripts/i18n/ha.json | 20 +- client/src/scripts/i18n/he.json | 20 +- client/src/scripts/i18n/hi.json | 20 +- client/src/scripts/i18n/hr.json | 20 +- client/src/scripts/i18n/ht.json | 20 +- client/src/scripts/i18n/hu.json | 20 +- client/src/scripts/i18n/hy.json | 20 +- client/src/scripts/i18n/id.json | 20 +- client/src/scripts/i18n/ig.json | 20 +- client/src/scripts/i18n/is.json | 20 +- client/src/scripts/i18n/it.json | 20 +- client/src/scripts/i18n/ja.json | 20 +- client/src/scripts/i18n/ka.json | 20 +- client/src/scripts/i18n/kk.json | 20 +- client/src/scripts/i18n/km.json | 20 +- client/src/scripts/i18n/kn.json | 20 +- client/src/scripts/i18n/ko.json | 20 +- client/src/scripts/i18n/lb.json | 20 +- client/src/scripts/i18n/lo.json | 20 +- client/src/scripts/i18n/lt.json | 20 +- client/src/scripts/i18n/lv.json | 20 +- client/src/scripts/i18n/mk.json | 20 +- client/src/scripts/i18n/ml.json | 20 +- client/src/scripts/i18n/mn.json | 20 +- client/src/scripts/i18n/mr.json | 20 +- client/src/scripts/i18n/ms.json | 20 +- client/src/scripts/i18n/mt.json | 20 +- client/src/scripts/i18n/my.json | 20 +- client/src/scripts/i18n/nb.json | 20 +- client/src/scripts/i18n/ne.json | 20 +- client/src/scripts/i18n/nl.json | 20 +- client/src/scripts/i18n/nn.json | 20 +- client/src/scripts/i18n/pa.json | 20 +- client/src/scripts/i18n/pl.json | 20 +- client/src/scripts/i18n/ps.json | 20 +- client/src/scripts/i18n/pt.json | 20 +- client/src/scripts/i18n/ro.json | 20 +- client/src/scripts/i18n/ru.json | 20 +- client/src/scripts/i18n/si.json | 20 +- client/src/scripts/i18n/sk.json | 20 +- client/src/scripts/i18n/sl.json | 20 +- client/src/scripts/i18n/so.json | 20 +- client/src/scripts/i18n/sq.json | 20 +- client/src/scripts/i18n/sr.json | 20 +- client/src/scripts/i18n/sv.json | 20 +- client/src/scripts/i18n/sw.json | 20 +- client/src/scripts/i18n/ta.json | 20 +- client/src/scripts/i18n/te.json | 20 +- client/src/scripts/i18n/th.json | 20 +- client/src/scripts/i18n/tr.json | 20 +- client/src/scripts/i18n/uk.json | 20 +- client/src/scripts/i18n/ur.json | 20 +- client/src/scripts/i18n/uz.json | 20 +- client/src/scripts/i18n/vi.json | 20 +- client/src/scripts/i18n/yo.json | 20 +- client/src/scripts/i18n/yue.json | 20 +- client/src/scripts/i18n/zh.json | 20 +- client/src/scripts/i18n/zu.json | 20 +- client/src/scripts/live/api.test.ts | 39 +++ client/src/scripts/live/api.ts | 21 +- client/src/scripts/live/conversation.test.ts | 229 +++++++++++++++++- client/src/scripts/live/conversation.ts | 224 ++++++++++++----- client/src/scripts/live/live.css | 28 +++ client/src/scripts/live/messages.test.ts | 13 +- client/src/scripts/live/messages.ts | 8 +- client/src/scripts/live/page-desk.test.ts | 8 +- client/src/scripts/live/page-desk.ts | 2 +- client/src/scripts/live/page-guest.test.ts | 59 ++++- client/src/scripts/live/page-guest.ts | 9 +- client/src/scripts/live/page-host.test.ts | 186 +++++++++++++- client/src/scripts/live/page-host.ts | 110 ++++++++- client/src/scripts/live/room.test.ts | 118 ++++++++- client/src/scripts/live/room.ts | 56 ++++- .../src/scripts/live/session-machine.test.ts | 101 +++++++- client/src/scripts/live/session-machine.ts | 77 ++++-- client/src/scripts/live/session-store.test.ts | 14 ++ client/src/scripts/live/session-store.ts | 5 + client/src/scripts/live/view.test.ts | 177 +++++++++++--- client/src/scripts/live/view.ts | 218 +++++++++++------ docs/live-connect.md | 22 ++ 107 files changed, 3104 insertions(+), 337 deletions(-) diff --git a/client/src/pages/live.astro b/client/src/pages/live.astro index 9658eb45..7a76dedc 100644 --- a/client/src/pages/live.astro +++ b/client/src/pages/live.astro @@ -49,6 +49,16 @@ const T = (k: string): string => (en as Record)[k] ?? '';
+
+ {T('liveGuests')} +
+ + + +
+ +
+