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.

23.8 KB
Raw Blame
"""TaskStore — aiosqlite-based persistence for the task queue (ADR-0003).

The store is deliberately dumb: no scheduling, no event dispatch. Just
CRUD + transactional state transitions + startup cleanup.

Two tables:
- ``tasks``      — main row per scheduled task (TaskSpec as a JSON blob).
- ``task_steps`` — append-only step log (the runner writes observation/action/
  verify/log lines here, the UI renders them as a timeline).

Pattern: identical to ``dodaios/memory/recall.py`` (``aiosqlite`` + ``ensure_open``
lazy-init so the store instance can be constructed synchronously).
"""

from __future__ import annotations

import builtins
import json
import time
from collections.abc import Sequence
from pathlib import Path
from typing import Any

import aiosqlite

from .hook_inbox import COUNTER_TRIGGER, HookInbox, migrate_status_check
from .hook_inbox import SCHEMA as HOOK_SCHEMA
from .schema import TERMINAL_STATES, TRIGGER_TYPES, TaskSpec, TaskState

SCHEMA_FILE = Path(__file__).parent / "schema.sql"

# Rebuild target for the CHECK-constraint migrations (trigger_type gained
# `every` 2026-06-17, state gained `paused` 2026-08-24). Mirrors the `tasks`
# table in schema.sql but as `tasks_new`. Kept here (not in schema.sql)
# because it only runs during migration.
_TASKS_REBUILD_SQL = """
CREATE TABLE tasks_new (
    id              TEXT PRIMARY KEY,
    trace_id        TEXT NOT NULL,
    spec_json       TEXT NOT NULL,
    state           TEXT NOT NULL CHECK(state IN (
                        'pending','scheduled','paused','running','completed',
                        'failed','cancelled','interrupted')),
    trigger_type    TEXT NOT NULL CHECK(trigger_type IN (
                        'after_delay','at_time','on_event','every','calendar','webhook','event_hook','source','cron')),
    due_at_ns       INTEGER,
    event_selector  TEXT,
    title           TEXT NOT NULL DEFAULT '',
    created_at_ns   INTEGER NOT NULL,
    started_at_ns   INTEGER,
    finished_at_ns  INTEGER,
    attempts        INTEGER NOT NULL DEFAULT 0,
    last_error      TEXT,
    result_json     TEXT
)
"""


class TaskStore:
    """CRUD store for tasks + steps on the shared memory DB.

    The DB file is NOT initialized by this class as far as the memory
    schema is concerned — ``RecallStore`` handles that. We can still call
    init() independently, though, because our schema is additive and
    idempotent (``CREATE TABLE IF NOT EXISTS``).
    """

    name: str = "sqlite-tasks"

    def __init__(self, db_path: str | Path) -> None:
        self._db_path = Path(db_path)
        self._db_path.parent.mkdir(parents=True, exist_ok=True)
        self._conn: aiosqlite.Connection | None = None
        self._hooks: HookInbox | None = None

    # ------------------------------------------------------------------
    # Lifecycle
    # ------------------------------------------------------------------

    async def init(self) -> None:
        """Opens the DB connection + runs the additive schema."""
        if self._conn is not None:
            return
        self._conn = await aiosqlite.connect(self._db_path, isolation_level=None)
        self._conn.row_factory = aiosqlite.Row
        # PRAGMAs: WAL + busy_timeout come from the memory schema, but on a
        # still-empty DB we set them here too, just to be safe.
        await self._conn.execute("PRAGMA journal_mode = WAL")
        await self._conn.execute("PRAGMA busy_timeout = 5000")
        await self._conn.execute("PRAGMA foreign_keys = ON")
        schema = SCHEMA_FILE.read_text(encoding="utf-8")
        await self._conn.executescript(schema)
        # Migrate legacy DBs whose trigger_type CHECK predates `calendar`
        # (added 2026-09-08). CREATE TABLE IF NOT EXISTS never alters an
        # existing table, so this explicit migration is required.
        await self._migrate_trigger_type_check()
        # Same dance for the state CHECK, which predates `paused` (2026-08-24).
        await self._migrate_state_check()
        await self._conn.executescript(HOOK_SCHEMA + COUNTER_TRIGGER)
        async with self._conn.execute("PRAGMA table_info(task_hook_deliveries)") as cursor:
            columns = {row[1] for row in await cursor.fetchall()}
        if "lineage_json" not in columns:
            await self._conn.execute(
                "ALTER TABLE task_hook_deliveries ADD COLUMN lineage_json TEXT NOT NULL "
                "DEFAULT '[]'"
            )
        if await migrate_status_check(self._conn):
            # The index and the counter trigger went with the old table.
            await self._conn.executescript(HOOK_SCHEMA + COUNTER_TRIGGER)
        self._hooks = HookInbox(self._conn)
        from .source_store import SCHEMA as SOURCE_SCHEMA
        from .source_store import SourceStore

        await self._conn.executescript(SOURCE_SCHEMA)
        self.sources = SourceStore(self._conn)

    @property
    def hooks(self) -> HookInbox:
        if self._hooks is None:
            raise RuntimeError("TaskStore is not initialized")
        return self._hooks

    async def close(self) -> None:
        if self._conn is not None:
            await self._conn.close()
            self._conn = None

    async def __aenter__(self) -> TaskStore:
        await self.init()
        return self

    async def __aexit__(self, *_: Any) -> None:
        await self.close()

    def _require_conn(self) -> aiosqlite.Connection:
        if self._conn is None:
            raise RuntimeError("TaskStore not initialized — call init() or use 'async with'.")
        return self._conn

    async def _migrate_trigger_type_check(self) -> None:
        """Rebuild ``tasks`` if its trigger_type CHECK predates ``calendar``.

        SQLite cannot ALTER a CHECK constraint, so we do the standard
        create-copy-drop-rename dance. Guarded to run at most once: it is a
        no-op once the live CHECK already mentions ``'calendar'`` (or has no
        CHECK at all).
        """
        table_sql = await self._tasks_table_sql()
        if table_sql is None:
            return
        if "trigger_type" not in table_sql or "CHECK" not in table_sql:
            return  # loose schema — nothing to migrate
        if all(f"'{kind}'" in table_sql for kind in TRIGGER_TYPES):
            return  # fresh schema or already migrated
        await self._rebuild_tasks_table()

    async def _migrate_state_check(self) -> None:
        """Rebuild ``tasks`` if its state CHECK predates ``paused``.

        Mirrors :meth:`_migrate_trigger_type_check`; without it, pausing an
        automation on a DB created before 2026-08-24 fails the CHECK.
        """
        table_sql = await self._tasks_table_sql()
        if table_sql is None:
            return
        if "state" not in table_sql or "CHECK" not in table_sql:
            return  # loose schema — nothing to migrate
        if "'paused'" in table_sql:
            return  # fresh schema or already migrated
        await self._rebuild_tasks_table()

    async def last_log_event(self, task_id: str) -> str:
        """The ``event`` of the task's newest log step (``""`` without one).

        The scheduler reads it after a hook run to tell a skipped run (its
        last step is ``skipped``) from a failed one.
        """
        cursor = await self._require_conn().execute(
            "SELECT json_extract(payload_json, '$.event') FROM task_steps "
            "WHERE task_id=? AND kind='log' ORDER BY seq DESC LIMIT 1",
            (task_id,),
        )
        row = await cursor.fetchone()
        await cursor.close()
        return str(row[0] or "") if row else ""

    async def event_firings(self, task_id: str) -> int:
        """Persisted event deliveries survive a scheduler restart or pause."""
        cursor = await self._require_conn().execute(
            "SELECT COUNT(*) FROM task_steps WHERE task_id=? AND kind='log' "
            "AND json_extract(payload_json, '$.event')='event_fired'",
            (task_id,),
        )
        row = await cursor.fetchone()
        return int(row[0]) if row else 0

    async def _tasks_table_sql(self) -> str | None:
        conn = self._require_conn()
        cur = await conn.execute(
            "SELECT sql FROM sqlite_master WHERE type='table' AND name='tasks'"
        )
        row = await cur.fetchone()
        await cur.close()
        if row is None:
            return None
        return str(row["sql"] or "")

    async def _rebuild_tasks_table(self) -> None:
        """create-copy-drop-rename ``tasks`` onto :data:`_TASKS_REBUILD_SQL`."""
        conn = self._require_conn()
        await conn.execute("PRAGMA foreign_keys = OFF")
        try:
            await conn.execute("BEGIN")
            await conn.execute(_TASKS_REBUILD_SQL)
            await conn.execute(
                "INSERT INTO tasks_new SELECT id, trace_id, spec_json, state, "
                "trigger_type, due_at_ns, event_selector, title, created_at_ns, "
                "started_at_ns, finished_at_ns, attempts, last_error, result_json "
                "FROM tasks"
            )
            await conn.execute("DROP TABLE tasks")
            await conn.execute("ALTER TABLE tasks_new RENAME TO tasks")
            await conn.execute("COMMIT")
        except Exception:
            await conn.execute("ROLLBACK")
            raise
        finally:
            await conn.execute("PRAGMA foreign_keys = ON")

        # Indexes were dropped with the old table — recreate them.
        await conn.executescript(
            "CREATE INDEX IF NOT EXISTS idx_tasks_state_due ON tasks(state, due_at_ns);"
            "CREATE INDEX IF NOT EXISTS idx_tasks_trace ON tasks(trace_id);"
            "CREATE INDEX IF NOT EXISTS idx_tasks_event_sel ON tasks(event_selector);"
        )

    async def set_next_due(self, task_id: str, due_at_ns: int) -> None:
        """Update only the ``due_at_ns`` column — used by the scheduler to
        re-arm a recurring (``every``) task for its next interval.
        """
        conn = self._require_conn()
        await conn.execute("UPDATE tasks SET due_at_ns = ? WHERE id = ?", (due_at_ns, task_id))

    async def replace_spec(self, task_id: str, spec: TaskSpec) -> None:
        if str(spec.id) != task_id:
            raise ValueError("A replacement must preserve the task id")
        kind, due, selector = self._compute_trigger_fields(spec)
        cursor = await self._require_conn().execute(
            "UPDATE tasks SET spec_json=?,title=?,trigger_type=?,due_at_ns=?,event_selector=? "
            "WHERE id=? AND state='paused'",
            (spec.model_dump_json(), spec.title, kind, due, selector, task_id),
        )
        if cursor.rowcount != 1:
            raise ValueError("Only an existing paused task can be updated")

    # ------------------------------------------------------------------
    # Helpers
    # ------------------------------------------------------------------

    @staticmethod
    def _compute_trigger_fields(spec: TaskSpec) -> tuple[str, int | None, str | None]:
        """Extracts ``(trigger_type, due_at_ns, event_selector)`` from a spec.

        ``due_at_ns`` is UTC nanoseconds; the scheduler compares it against
        ``time.time_ns()``.
        """
        trig = spec.trigger
        if trig.type == "after_delay":
            due = time.time_ns() + int(trig.delay_seconds * 1e9)
            return "after_delay", due, None
        if trig.type == "at_time":
            # Parsing responsibility lives with the scheduler (ISO-8601 + TZ).
            # Here just a fallback: if parsing succeeds, we use it.
            from datetime import datetime

            try:
                dt = datetime.fromisoformat(trig.iso_timestamp.replace("Z", "+00:00"))
                if dt.tzinfo is None:
                    dt = dt.astimezone()
                due = int(dt.timestamp() * 1e9)
            except ValueError:
                due = 0
            return "at_time", due, None
        if trig.type == "cron":
            from .cron_schedule import next_cron_ns

            return "cron", next_cron_ns(trig.expression, trig.timezone, time.time_ns()), None
        if trig.type in ("webhook", "event_hook", "source"):
            return trig.type, None, getattr(trig, "event_name", None)
        if trig.type == "on_event":
            return "on_event", None, trig.event_name
        if trig.type == "calendar":
            from .calendar import next_calendar_due_ns

            return "calendar", next_calendar_due_ns(trig, time.time_ns()), None
        if trig.type == "every":
            # Recurring: anchor to start_at if given, else now + interval.
            if trig.start_at:
                from datetime import datetime

                try:
                    dt = datetime.fromisoformat(trig.start_at.replace("Z", "+00:00"))
                    if dt.tzinfo is None:
                        dt = dt.astimezone()
                    due = int(dt.timestamp() * 1e9)
                except ValueError:
                    due = time.time_ns() + int(trig.interval_seconds * 1e9)
            else:
                due = time.time_ns() + int(trig.interval_seconds * 1e9)
            return "every", due, None
        raise ValueError(f"Unknown trigger type: {trig.type}")  # pragma: no cover

    # ------------------------------------------------------------------
    # CRUD
    # ------------------------------------------------------------------

    async def insert(self, spec: TaskSpec, *, trace_id: str | None = None) -> str:
        """Creates a new task with ``state='scheduled'``.

        Returns the task ID (str). ``trace_id`` may optionally be passed in;
        otherwise the spec ID is used as the trace (one task = one trace, as
        long as no other scope overrides it).
        """
        conn = self._require_conn()
        trigger_type, due_at_ns, event_selector = self._compute_trigger_fields(spec)
        created_at_ns = spec.created_at_ns or time.time_ns()
        tid = str(spec.id)
        trace = trace_id or tid

        spec_json = spec.model_dump_json()

        await conn.execute(
            """
            INSERT INTO tasks (id, trace_id, spec_json, state, trigger_type,
                               due_at_ns, event_selector, title,
                               created_at_ns, attempts)
            VALUES (?, ?, ?, 'scheduled', ?, ?, ?, ?, ?, 0)
            """,
            (
                tid,
                trace,
                spec_json,
                trigger_type,
                due_at_ns,
                event_selector,
                spec.title,
                created_at_ns,
            ),
        )
        return tid

    async def update_state(
        self,
        task_id: str,
        state: TaskState,
        *,
        error: str | None = None,
        result: dict[str, Any] | None = None,
        increment_attempts: bool = False,
    ) -> None:
        """Transitions to a new state, atomically, with optional error/result info.

        Automatically sets ``started_at_ns`` on the transition to ``running``
        and ``finished_at_ns`` on terminal states.
        """
        conn = self._require_conn()
        now_ns = time.time_ns()
        sets = ["state = ?"]
        params: list[Any] = [state]

        if state == "running":
            sets.append("started_at_ns = ?")
            params.append(now_ns)
        # A run has finished when the state is terminal — or when a recurring
        # task hands back a ``result`` (or a failure ``error``) on its way to
        # ``scheduled``. Either way
        # ``finished_at_ns`` marks the end of the LAST run, and a successful
        # result clears the error of a previous failed run so the list view's
        # ``last_run_state`` reflects the latest run only.
        finished = state in TERMINAL_STATES or result is not None or error is not None
        if finished:
            sets.append("finished_at_ns = ?")
            params.append(now_ns)
        if error is not None:
            sets.append("last_error = ?")
            params.append(error)
        elif result is not None:
            sets.append("last_error = NULL")
        if result is not None:
            sets.append("result_json = ?")
            params.append(json.dumps(result, ensure_ascii=False))
        if increment_attempts:
            sets.append("attempts = attempts + 1")

        params.append(task_id)
        # `sets` is a whitelist of column assignments (built statically
        # above) — no user input flows into the SQL string.
        sql = f"UPDATE tasks SET {', '.join(sets)} WHERE id = ?"  # noqa: S608
        await conn.execute(sql, tuple(params))

    async def append_step(
        self,
        task_id: str,
        kind: str,
        payload: dict[str, Any],
    ) -> int:
        """Appends a step to ``task_steps``. Returns the new ``seq``.

        ``kind`` is ``'observation' | 'action' | 'verify' | 'log'``. No hard
        DB constraint, so the runner can also record other kinds (e.g.
        'retry').
        """
        conn = self._require_conn()
        cur = await conn.execute(
            "SELECT COALESCE(MAX(seq), 0) AS max_seq FROM task_steps WHERE task_id = ?",
            (task_id,),
        )
        row = await cur.fetchone()
        await cur.close()
        seq = int(row["max_seq"]) + 1 if row else 1
        await conn.execute(
            """
            INSERT INTO task_steps (task_id, seq, kind, payload_json, timestamp_ns)
            VALUES (?, ?, ?, ?, ?)
            """,
            (task_id, seq, kind, json.dumps(payload, ensure_ascii=False), time.time_ns()),
        )
        return seq

    async def list(
        self,
        state_filter: str | list[str] | None = None,
        *,
        limit: int = 100,
    ) -> list[dict[str, Any]]:
        """Returns a list of task rows (without steps), filtered by state.

        ``spec_json`` is included so the API can derive spec-level summary
        fields (tags, created_by, interval) without a second query per row.
        """
        conn = self._require_conn()
        sql = (
            "SELECT id, trace_id, state, trigger_type, due_at_ns, title, "
            "created_at_ns, started_at_ns, finished_at_ns, attempts, last_error, "
            "spec_json FROM tasks"
        )
        params: list[Any] = []
        if state_filter is not None:
            if isinstance(state_filter, str):
                sql += " WHERE state = ?"
                params.append(state_filter)
            else:
                placeholders = ",".join(["?"] * len(state_filter))
                sql += f" WHERE state IN ({placeholders})"
                params.extend(state_filter)
        sql += " ORDER BY created_at_ns DESC LIMIT ?"
        params.append(limit)
        cur = await conn.execute(sql, tuple(params))
        rows = await cur.fetchall()
        await cur.close()
        return [dict(r) for r in rows]

    async def get(self, task_id: str) -> dict[str, Any] | None:
        """Returns the full task (incl. steps), or None."""
        conn = self._require_conn()
        cur = await conn.execute("SELECT * FROM tasks WHERE id = ?", (task_id,))
        row = await cur.fetchone()
        await cur.close()
        if row is None:
            return None
        task = dict(row)

        cur = await conn.execute(
            "SELECT seq, kind, payload_json, timestamp_ns FROM task_steps "
            "WHERE task_id = ? ORDER BY seq ASC",
            (task_id,),
        )
        step_rows = await cur.fetchall()
        await cur.close()
        task["steps"] = [
            {
                "seq": int(r["seq"]),
                "kind": str(r["kind"]),
                "payload": json.loads(r["payload_json"]),
                "timestamp_ns": int(r["timestamp_ns"]),
            }
            for r in step_rows
        ]
        return task

    async def latest_agent_results(
        self,
        task_ids: Sequence[str] | None = None,
        *,
        max_chars: int = 400,
    ) -> dict[str, str]:
        """``{task_id: text}`` of the newest ``agent_result`` log step per
        task, truncated to ``max_chars`` — ONE query for the whole list view,
        no per-task round trip. ``task_ids=None`` covers every task.
        """
        conn = self._require_conn()
        sql = (
            "SELECT s.task_id AS task_id, s.payload_json AS payload_json "
            "FROM task_steps s JOIN ("
            "  SELECT task_id, MAX(seq) AS seq FROM task_steps"
            "  WHERE kind = 'log'"
            "    AND json_extract(payload_json, '$.event') = 'agent_result'"
            "  GROUP BY task_id"
            ") latest ON latest.task_id = s.task_id AND latest.seq = s.seq"
        )
        params: list[Any] = []
        if task_ids is not None:
            if not task_ids:
                return {}
            placeholders = ",".join(["?"] * len(task_ids))
            sql += f" WHERE s.task_id IN ({placeholders})"  # noqa: S608 — placeholders only
            params.extend(task_ids)
        cur = await conn.execute(sql, tuple(params))
        rows = await cur.fetchall()
        await cur.close()
        out: dict[str, str] = {}
        for r in rows:
            try:
                text = str(json.loads(r["payload_json"]).get("text") or "")
            except (ValueError, AttributeError):
                continue
            out[str(r["task_id"])] = text[:max_chars]
        return out

    async def get_spec(self, task_id: str) -> TaskSpec | None:
        """Deserializes ``spec_json`` back into a TaskSpec."""
        conn = self._require_conn()
        cur = await conn.execute("SELECT spec_json FROM tasks WHERE id = ?", (task_id,))
        row = await cur.fetchone()
        await cur.close()
        if row is None:
            return None
        return TaskSpec.model_validate_json(row["spec_json"])

    async def delete(self, task_id: str) -> bool:
        """Removes the task + steps (via ON DELETE CASCADE). Returns whether a row was hit."""
        conn = self._require_conn()
        cur = await conn.execute("DELETE FROM tasks WHERE id = ?", (task_id,))
        rowcount = cur.rowcount
        await cur.close()
        return rowcount > 0

    async def all_pending_scheduled(self) -> builtins.list[dict[str, Any]]:
        """For scheduler hydration: all tasks in state ``scheduled``."""
        return await self.list(state_filter="scheduled", limit=10_000)

    async def cleanup_interrupted(self) -> int:
        """Startup cleanup: all ``running`` → ``interrupted`` with an error log.

        Returns the number of affected tasks. Per ADR-0003 this must be
        called at app start, before the scheduler hydrates.
        """
        conn = self._require_conn()
        now_ns = time.time_ns()
        cur = await conn.execute(
            """
            UPDATE tasks
            SET state = 'interrupted',
                finished_at_ns = ?,
                last_error = 'App exit detected'
            WHERE state = 'running'
            """,
            (now_ns,),
        )
        rowcount = cur.rowcount
        await cur.close()
        return int(rowcount or 0)

    async def retire_catalogue_tasks(self) -> int:
        """Startup cleanup: cancel every live task the retired automations
        catalogue created (``created_by="template"``).

        Scheduling now lives on agents (``dodaios/society/routines.py``); the
        catalogue and the standalone Automations section are gone, so such a
        task would keep firing with nowhere left to see, pause or stop it.
        The row is kept, only its state changes. Returns the number of
        retired tasks; called at app start, before the scheduler hydrates.
        """
        conn = self._require_conn()
        now_ns = time.time_ns()
        cur = await conn.execute(
            """
            UPDATE tasks
            SET state = 'cancelled',
                finished_at_ns = ?,
                last_error = 'Standalone schedules were retired; give this job to an agent instead'
            WHERE state IN ('pending', 'scheduled', 'paused')
              AND json_extract(spec_json, '$.created_by') = 'template'
            """,
            (now_ns,),
        )
        rowcount = cur.rowcount
        await cur.close()
        return int(rowcount or 0)