feat(voice): add authenticated Phase 2 live lifecycle

Add cookie authentication, owned conversations, durable Live reservations,
tool completion/replay fencing, revocation, and startup reconciliation.
Introduce forward lifecycle/owner-chain migrations and the temporary Gemini
adapter while preserving the existing OpenAI Realtime adapter.

Include the executable audio client, regression coverage, configuration,
API/database documentation, and recorded acceptance evidence.

Validation: focused review suite 82 passed; Python/TOML parsing, JavaScript
syntax, and whitespace checks passed. Full PostgreSQL/provider-free results
are recorded in the repair report; real-provider/browser acceptance is unrun.

Follow-up work remains for configurable OpenAI/Gemini selection, transport
admission, authority diagnostics, playback message bounds, and client replay
identity after ambiguous creation responses.
This commit is contained in:
11linear11
2026-10-09 07:59:02 -04:00
parent 07245cd5ca
commit 996435d2ae
42 changed files with 4551 additions and 88 deletions
+122
View File
@@ -0,0 +1,122 @@
"""Live voice session lifecycle, auth sessions, conversations and tool calls.
Revision ID: 0004_live_lifecycle
Revises: 0003_extraction_review
Create Date: 2026-10-09
"""
from alembic import op
revision = "0004_live_lifecycle"
down_revision = "0003_extraction_review"
branch_labels = None
depends_on = None
def upgrade(**kwargs):
# 1. auth_sessions table
op.execute("""
CREATE TABLE auth_sessions (
id uuid PRIMARY KEY,
organization_id uuid NOT NULL,
user_id uuid NOT NULL,
token_hash char(64) NOT NULL UNIQUE,
created_at timestamptz NOT NULL DEFAULT now(),
expires_at timestamptz NOT NULL,
last_seen_at timestamptz NOT NULL DEFAULT now(),
revoked_at timestamptz,
FOREIGN KEY (organization_id, user_id) REFERENCES users(organization_id, id) ON DELETE CASCADE,
CHECK (expires_at > created_at)
);
CREATE INDEX idx_auth_sessions_expires_at ON auth_sessions(expires_at);
CREATE INDEX idx_auth_sessions_user_id ON auth_sessions(user_id);
""")
# 2. conversations table (voice-first for MVP)
op.execute("""
CREATE TABLE conversations (
id uuid PRIMARY KEY,
organization_id uuid NOT NULL,
owner_user_id uuid NOT NULL,
client_request_id uuid NOT NULL,
request_hash char(64) NOT NULL,
mode text NOT NULL DEFAULT 'voice' CHECK (mode IN ('voice', 'text')),
title varchar(120),
status text NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'stale', 'closed')),
access_epoch bigint NOT NULL DEFAULT 1 CHECK (access_epoch >= 1),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
FOREIGN KEY (organization_id, owner_user_id) REFERENCES users(organization_id, id) ON DELETE CASCADE,
UNIQUE (owner_user_id, client_request_id),
UNIQUE (organization_id, id)
);
CREATE INDEX idx_conversations_owner_created ON conversations(owner_user_id, created_at, id);
""")
# 3. live_sessions table
op.execute("""
CREATE TABLE live_sessions (
id uuid PRIMARY KEY,
organization_id uuid NOT NULL,
conversation_id uuid NOT NULL,
owner_user_id uuid NOT NULL,
auth_session_id uuid NOT NULL,
client_request_id uuid NOT NULL,
request_hash char(64) NOT NULL,
provider text NOT NULL DEFAULT 'openai',
model text NOT NULL DEFAULT 'gpt-realtime-mini',
provider_session_id text,
status text NOT NULL DEFAULT 'creating' CHECK (status IN ('creating', 'connecting', 'connected', 'closed', 'failed')),
settings_revision bigint NOT NULL DEFAULT 1 CHECK (settings_revision >= 1),
knowledge_revision bigint NOT NULL DEFAULT 1 CHECK (knowledge_revision >= 1),
access_epoch bigint NOT NULL DEFAULT 1 CHECK (access_epoch >= 1),
execution_profile jsonb NOT NULL DEFAULT '{}'::jsonb,
expires_at timestamptz NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
connected_at timestamptz,
closed_at timestamptz,
close_reason text,
error_code text,
FOREIGN KEY (organization_id, conversation_id) REFERENCES conversations(organization_id, id) ON DELETE CASCADE,
FOREIGN KEY (organization_id, owner_user_id) REFERENCES users(organization_id, id) ON DELETE CASCADE,
FOREIGN KEY (auth_session_id) REFERENCES auth_sessions(id) ON DELETE CASCADE,
UNIQUE (owner_user_id, client_request_id),
UNIQUE (organization_id, id),
CHECK ((status IN ('closed', 'failed') AND closed_at IS NOT NULL) OR (status NOT IN ('closed', 'failed')))
);
CREATE UNIQUE INDEX uq_live_sessions_active_conversation
ON live_sessions (conversation_id)
WHERE status IN ('creating', 'connecting', 'connected');
CREATE INDEX idx_live_sessions_expires_at ON live_sessions(expires_at);
""")
# 4. tool_calls table (voice-only MVP)
op.execute("""
CREATE TABLE tool_calls (
id uuid PRIMARY KEY,
organization_id uuid NOT NULL,
live_session_id uuid NOT NULL,
external_call_id text NOT NULL,
tool_name text NOT NULL DEFAULT 'search_knowledge' CHECK (tool_name = 'search_knowledge'),
arguments jsonb NOT NULL,
request_hash char(64) NOT NULL,
status text NOT NULL DEFAULT 'running' CHECK (status IN ('running', 'succeeded', 'failed')),
knowledge_revision bigint NOT NULL DEFAULT 1 CHECK (knowledge_revision >= 1),
access_epoch bigint NOT NULL DEFAULT 1 CHECK (access_epoch >= 1),
result_json jsonb,
error_code text,
created_at timestamptz NOT NULL DEFAULT now(),
completed_at timestamptz,
FOREIGN KEY (organization_id, live_session_id) REFERENCES live_sessions(organization_id, id) ON DELETE CASCADE,
UNIQUE (live_session_id, external_call_id),
CHECK ((status IN ('succeeded', 'failed') AND completed_at IS NOT NULL) OR (status = 'running' AND completed_at IS NULL))
);
CREATE INDEX idx_tool_calls_session_created ON tool_calls(live_session_id, created_at);
""")
def downgrade(**kwargs):
op.execute("DROP TABLE IF EXISTS tool_calls CASCADE;")
op.execute("DROP TABLE IF EXISTS live_sessions CASCADE;")
op.execute("DROP TABLE IF EXISTS conversations CASCADE;")
op.execute("DROP TABLE IF EXISTS auth_sessions CASCADE;")
@@ -0,0 +1,31 @@
"""Enforce the Live owner chain without rewriting applied revision 0004."""
from alembic import op
revision = '0005_live_owner_chain'
down_revision = '0004_live_lifecycle'
branch_labels = None
depends_on = None
def upgrade(**kwargs):
op.execute('''
ALTER TABLE conversations ADD CONSTRAINT uq_conversation_owner UNIQUE (organization_id, owner_user_id, id);
ALTER TABLE auth_sessions ADD CONSTRAINT uq_auth_owner UNIQUE (organization_id, user_id, id);
ALTER TABLE live_sessions ADD CONSTRAINT fk_live_conversation_owner
FOREIGN KEY (organization_id, owner_user_id, conversation_id)
REFERENCES conversations (organization_id, owner_user_id, id) ON DELETE CASCADE;
ALTER TABLE live_sessions ADD CONSTRAINT fk_live_auth_owner
FOREIGN KEY (organization_id, owner_user_id, auth_session_id)
REFERENCES auth_sessions (organization_id, user_id, id) ON DELETE CASCADE;
ALTER TABLE tool_calls ADD COLUMN public_ref uuid;
UPDATE tool_calls SET public_ref=id;
ALTER TABLE tool_calls ALTER COLUMN public_ref SET NOT NULL;
ALTER TABLE tool_calls ADD CONSTRAINT uq_tool_public_ref UNIQUE (public_ref);
''')
def downgrade(**kwargs):
op.execute('''
ALTER TABLE tool_calls DROP COLUMN public_ref;
ALTER TABLE live_sessions DROP CONSTRAINT fk_live_auth_owner;
ALTER TABLE live_sessions DROP CONSTRAINT fk_live_conversation_owner;
ALTER TABLE auth_sessions DROP CONSTRAINT uq_auth_owner;
ALTER TABLE conversations DROP CONSTRAINT uq_conversation_owner;
''')