Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

8.6 KB
Raw Blame
"""Durable receipts and independent transcript streams for continuous voice."""

from __future__ import annotations

import json
import sqlite3
import threading
import time
from dataclasses import dataclass
from pathlib import Path
from typing import Literal


@dataclass(frozen=True, slots=True)
class TranscriptFragment:
    session_id: str
    event_id: str
    role: Literal["user", "assistant"]
    delta: str
    start_ms: int
    end_ms: int


class LiveLedger:
    """Claim before execution; uncertain receipts are never automatically replayed."""

    def __init__(self, path: Path | str) -> None:
        self._lock = threading.RLock()
        self._db = sqlite3.connect(str(path), check_same_thread=False)
        with self._db:
            self._db.executescript("""
                CREATE TABLE IF NOT EXISTS live_operations (
                    session_id TEXT NOT NULL, call_id TEXT NOT NULL,
                    tool TEXT NOT NULL, arguments TEXT NOT NULL, revision INTEGER NOT NULL,
                    status TEXT NOT NULL, result TEXT, updated REAL NOT NULL,
                    PRIMARY KEY(session_id, call_id));
                CREATE TABLE IF NOT EXISTS live_transcripts (
                    session_id TEXT NOT NULL, event_id TEXT NOT NULL, role TEXT NOT NULL,
                    delta TEXT NOT NULL, start_ms INTEGER NOT NULL, end_ms INTEGER NOT NULL,
                    PRIMARY KEY(session_id, event_id));
                CREATE TABLE IF NOT EXISTS live_usage (
                    session_id TEXT PRIMARY KEY, seconds REAL NOT NULL,
                    finalized INTEGER NOT NULL DEFAULT 0);
                CREATE TABLE IF NOT EXISTS live_backend_usage (
                    session_id TEXT NOT NULL, response_id TEXT PRIMARY KEY,
                    model TEXT NOT NULL, usage TEXT NOT NULL);
            """)

    def claim(
        self, session: str, call: str, tool: str, arguments: dict, revision: int,
        *, deduplicate_unconfirmed: bool = False,
    ) -> dict | None:
        encoded = json.dumps(arguments, sort_keys=True)
        with self._lock, self._db:
            row = self._db.execute(
                "SELECT tool, arguments, status, result FROM live_operations "
                "WHERE session_id=? AND call_id=?",
                (session, call),
            ).fetchone()
            if row:
                if row[0] != tool or row[1] != encoded:
                    return {"success": False, "error": "Call ID reused with different arguments."}
                return (
                    json.loads(row[3])
                    if row[3]
                    else {
                        "success": False,
                        "status": "uncertain",
                        "error": "Earlier execution unconfirmed. Reconcile before retrying.",
                    }
                )
            if deduplicate_unconfirmed:
                earlier = self._db.execute(
                    "SELECT result FROM live_operations WHERE session_id=? AND tool=? "
                    "AND arguments=? ORDER BY updated DESC",
                    (session, tool, encoded),
                ).fetchall()
                for (raw,) in earlier:
                    receipt = json.loads(raw) if raw else None
                    if receipt is None or receipt.get("status") == "uncertain":
                        return receipt or {
                            "success": False, "status": "uncertain",
                            "error": "Earlier delivery is unconfirmed; do not resend or reassign.",
                        }
            self._db.execute(
                "INSERT INTO live_operations VALUES (?, ?, ?, ?, ?, 'running', NULL, ?)",
                (session, call, tool, encoded, revision, time.time()),
            )
        return None

    def operation(self, session: str, call: str) -> dict | None:
        """Read a durable claim without executing or repairing any action."""
        with self._lock:
            row = self._db.execute(
                "SELECT result FROM live_operations WHERE session_id=? AND call_id=?",
                (session, call),
            ).fetchone()
        if row is None:
            return None
        return json.loads(row[0]) if row[0] else {
            "status": "uncertain", "success": False,
            "reason": "Creation or delivery is still unconfirmed. Do not recreate or reassign.",
        }

    def finish(self, session: str, call: str, result: dict) -> None:
        with self._lock, self._db:
            self._db.execute(
                "UPDATE live_operations SET status='finished', result=?, updated=? "
                "WHERE session_id=? AND call_id=?",
                (json.dumps(result, default=str), time.time(), session, call),
            )

    def append(self, fragment: TranscriptFragment) -> bool:
        with self._lock, self._db:
            return (
                self._db.execute(
                    "INSERT OR IGNORE INTO live_transcripts VALUES (?, ?, ?, ?, ?, ?)",
                    (
                        fragment.session_id,
                        fragment.event_id,
                        fragment.role,
                        fragment.delta,
                        fragment.start_ms,
                        fragment.end_ms,
                    ),
                ).rowcount
                == 1
            )

    def usage(self, session: str, seconds: float, *, finalized: bool = False) -> None:
        with self._lock, self._db:
            self._db.execute(
                "INSERT INTO live_usage VALUES (?, ?, ?) ON CONFLICT(session_id) DO UPDATE SET "
                "seconds=MAX(seconds, excluded.seconds), "
                "finalized=MAX(finalized, excluded.finalized)",
                (session, max(0, seconds), int(finalized)),
            )

    def transcript_snapshot(self, fragment: TranscriptFragment) -> None:
        """Replace one provider-owned caption segment, including final corrections."""
        with self._lock, self._db:
            self._db.execute(
                "INSERT INTO live_transcripts VALUES (?, ?, ?, ?, ?, ?) "
                "ON CONFLICT(session_id,event_id) DO UPDATE SET "
                "delta=excluded.delta, end_ms=excluded.end_ms",
                (fragment.session_id, fragment.event_id, fragment.role, fragment.delta,
                 fragment.start_ms, fragment.end_ms),
            )

    def close(self) -> None:
        with self._lock:
            self._db.close()

    def backend_usage(self, session: str, response: str, model: str, usage: dict) -> None:
        with self._lock, self._db:
            self._db.execute(
                "INSERT OR IGNORE INTO live_backend_usage VALUES (?, ?, ?, ?)",
                (session, response, model, json.dumps(usage)),
            )

    def transcript(self, session: str) -> list[dict]:
        with self._lock:
            rows = self._db.execute(
                "SELECT role, delta, start_ms, end_ms FROM live_transcripts "
                "WHERE session_id=? ORDER BY start_ms, rowid",
                (session,),
            ).fetchall()
        return [
            dict(zip(("role", "delta", "start_ms", "end_ms"), row, strict=True)) for row in rows
        ]

    def recovery_state(self, session: str) -> tuple[bool, list[dict]]:
        with self._lock:
            rows = self._db.execute(
                "SELECT tool, result, status FROM live_operations WHERE session_id=? "
                "ORDER BY updated",
                (session,),
            ).fetchall()
        receipts = []
        for tool, raw, status in rows:
            if status != "finished" or not raw:
                return False, []
            result = json.loads(raw)
            if result.get("confirmation_required") or result.get("blocked"):
                continue
            # A refusal that ran nothing (schema error, unapproved yes, refused
            # hang-up) leaves nothing half-done; it must not end the call.
            if result.get("executed") is False:
                continue
            if result.get("status") in {
                "superseded",
                "voice_closed_before_execution",
                "voice_closed_before_confirmation",
                "cancelled",
            }:
                continue
            if result.get("success") is False or result.get("verified") is False:
                return False, []
            if result.get("success") is True:
                receipts.append(
                    {
                        "tool": tool,
                        "success": True,
                        "output": str(result.get("output", ""))[:200],
                        "superseded": bool(result.get("superseded", False)),
                    }
                )
        return True, receipts[-8:]