Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

64.4 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Profile statistics aggregation over local chat/training history."""

import json
import sqlite3
import threading
import time
from datetime import datetime, timedelta, timezone
from types import SimpleNamespace

import pytest

from storage import api_usage_db, profile_stats_db, studio_db
from core.inference.api_monitor import ApiMonitor
import core.inference.api_monitor as api_monitor_module
from storage.api_usage_db import (
    MAX_ENDPOINT_CHARS,
    MAX_MODEL_CHARS,
    MAX_RECEIPT_ID_CHARS,
    MAX_STATUS_CHARS,
    MAX_SUBJECT_CHARS,
    ApiUsageReceipt,
    ApiUsageWriter,
    acquire_api_usage_writer,
    enqueue_api_usage,
    record_api_usage,
    release_api_usage_writer,
)
from storage.profile_stats_db import compute_profile_stats, invalidate_profile_stats_cache


def _shared_setup_1(conn):
    conn.execute(
        "INSERT INTO training_runs (id, status, model_name, dataset_name, config_json, "
        "started_at, total_steps, final_step, duration_seconds, output_dir, resume_blocked) "
        "VALUES ('src', 'stopped', 'm', 'd', '{}', '2026-01-01T10:00:00', 20, 10, 600, "
        "'/runs/out', 1)",
    )


@pytest.fixture
def stats_db(tmp_path, monkeypatch):
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))
    monkeypatch.setenv("UNSLOTH_STUDIO_PROJECTS_HOME", str(tmp_path / "Projects"))
    monkeypatch.setattr(studio_db, "_schema_ready", set())
    invalidate_profile_stats_cache()
    yield
    invalidate_profile_stats_cache()


# _seed_thread's user-to-reply gap; fork_chat_thread copies created_at, so clones reuse it.
REPLY_DELAY = timedelta(seconds = 10)


def _api_receipt(
    receipt_id: str,
    when: datetime,
    *,
    prompt: int = 40,
    completion: int = 10,
    total: int = 50,
    model: str = "unsloth/api-model",
    status: str = "completed",
    via_api_key: bool = True,
    subject: str = "external-client",
) -> ApiUsageReceipt:
    return ApiUsageReceipt(
        id = receipt_id,
        subject = subject,
        endpoint = "/v1/chat/completions",
        model = model,
        status = status,
        prompt_tokens = prompt,
        completion_tokens = completion,
        total_tokens = total,
        created_at = _ms(when),
        via_api_key = via_api_key,
    )


def _ms(when: datetime) -> int:
    return int(when.timestamp() * 1000)


def _seed_thread(conn, thread_id: str, model_id: str, turns: list[tuple[datetime, dict]]):
    conn.execute(
        "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, updated_at) "
        "VALUES (?, ?, 'base', ?, ?, ?)",
        (thread_id, f"Thread {thread_id}", model_id, _ms(turns[0][0]), _ms(turns[-1][0])),
    )
    for index, (when, metadata) in enumerate(turns):
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, created_at) "
            "VALUES (?, ?, ?, ?, ?, ?)",
            (
                f"{thread_id}-u{index}",
                thread_id,
                "user",
                json.dumps([{"type": "text", "text": "hi"}]),
                None,
                _ms(when),
            ),
        )
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, created_at) "
            "VALUES (?, ?, ?, ?, ?, ?)",
            (
                f"{thread_id}-a{index}",
                thread_id,
                "assistant",
                json.dumps([{"type": "text", "text": "hello"}]),
                json.dumps(metadata),
                _ms(when + REPLY_DELAY),
            ),
        )


def _metadata(
    prompt: int,
    completion: int,
    *,
    speed: float = 40.0,
    tools: int = 0,
) -> dict:
    return {
        "contextUsage": {
            "promptTokens": prompt,
            "completionTokens": completion,
            "totalTokens": prompt + completion,
            "cachedTokens": 5,
            "modelId": "unsloth/gpt-oss-20b",
        },
        "timing": {
            "streamStartTime": 1_760_000_000_000,
            "firstTokenTime": 200,
            "totalStreamTime": 2000,
            "tokenCount": completion,
            "tokensPerSecond": speed,
            "toolCallCount": tools,
        },
    }


def test_empty_history_returns_zeroed_payload(stats_db):
    stats = compute_profile_stats(days = 30)

    assert stats["totals"]["messages"] == 0
    assert stats["totals"]["totalTokens"] == 0
    assert stats["totals"]["chatTokens"] == 0
    assert stats["totals"]["apiTokens"] == 0
    assert stats["streak"] == {"current": 0, "longest": 0, "lastActiveDay": None}
    assert stats["peakDay"] is None
    assert stats["longestChat"] is None
    assert len(stats["daily"]) == 30
    assert all(day["tokens"] == 0 for day in stats["daily"])


def test_mixed_chat_and_api_usage_combines_only_activity_metrics(stats_db):
    today = datetime.now(timezone.utc).replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(
            conn,
            "mixed",
            "unused-thread-model",
            [(today, _metadata(100, 25))],
        )
        conn.commit()
    finally:
        conn.close()

    assert record_api_usage(_api_receipt("api-mixed", today, model = "unsloth/gpt-oss-20b"))
    stats = compute_profile_stats(days = 7, tz_name = "UTC", subject = "external-client")
    totals = stats["totals"]

    assert totals["promptTokens"] == 140
    assert totals["completionTokens"] == 35
    assert totals["totalTokens"] == 175
    assert totals["chatPromptTokens"] == 100
    assert totals["chatCompletionTokens"] == 25
    assert totals["chatTokens"] == 125
    assert totals["apiPromptTokens"] == 40
    assert totals["apiCompletionTokens"] == 10
    assert totals["apiTokens"] == 50
    assert totals["threads"] == 1
    assert totals["messages"] == 2
    assert totals["cachedTokens"] == 5
    assert stats["daily"][-1] == {
        "date": today.date().isoformat(),
        "tokens": 175,
        "messages": 2,
        "chats": 1,
        "apiRequests": 1,
    }
    assert stats["peakDay"] == {"date": today.date().isoformat(), "tokens": 175}
    assert stats["models"][0]["id"] == "unsloth/gpt-oss-20b"
    assert stats["models"][0]["messages"] == 2
    assert stats["models"][0]["tokens"] == 175


def test_api_usage_and_cache_are_subject_scoped_while_legacy_chat_is_shared(stats_db):
    now = datetime.now(timezone.utc).replace(microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "shared-chat", "unused", [(now, _metadata(10, 5))])
        conn.commit()
    finally:
        conn.close()

    assert record_api_usage(
        _api_receipt("alice-api", now, total = 11, prompt = 7, completion = 4, subject = "alice")
    )
    assert record_api_usage(
        _api_receipt("bob-api", now, total = 23, prompt = 20, completion = 3, subject = "bob")
    )

    alice = compute_profile_stats(days = 1, tz_name = "UTC", subject = "alice")
    bob = compute_profile_stats(days = 1, tz_name = "UTC", subject = "bob")

    assert alice["totals"]["chatTokens"] == bob["totals"]["chatTokens"] == 15
    assert alice["totals"]["apiTokens"] == 11
    assert bob["totals"]["apiTokens"] == 23
    assert alice["totals"]["totalTokens"] == 26
    assert bob["totals"]["totalTokens"] == 38
    assert alice is not bob


def test_api_only_usage_is_durable_idempotent_and_invalidates_cache(stats_db):
    now = datetime.now(timezone.utc).replace(microsecond = 0)
    initial = compute_profile_stats(days = 7, tz_name = "UTC", subject = "external-client")
    receipt = _api_receipt("api-only", now)

    assert record_api_usage(receipt) is True
    assert record_api_usage(receipt) is False
    after_insert = compute_profile_stats(days = 7, tz_name = "UTC", subject = "external-client")
    assert after_insert is not initial
    assert after_insert["totals"]["messages"] == 0
    assert after_insert["totals"]["apiTokens"] == 50
    assert after_insert["totals"]["totalTokens"] == 50
    assert after_insert["models"][0]["id"] == "unsloth/api-model"
    assert after_insert["streak"]["current"] == 1

    studio_db._schema_ready = set()
    invalidate_profile_stats_cache()
    after_restart = compute_profile_stats(days = 7, tz_name = "UTC", subject = "external-client")
    assert after_restart["totals"]["apiTokens"] == 50
    assert after_restart["daily"][-1]["tokens"] == 50


def test_terminal_callback_returns_promptly_while_locked_db_eventually_persists(stats_db):
    lock_conn = studio_db.get_connection()
    callback_lease = None
    writer_lease = acquire_api_usage_writer()
    monitor = ApiMonitor()
    try:
        lock_conn.execute("BEGIN IMMEDIATE")
        callback_lease = monitor.acquire_terminal_callback(enqueue_api_usage)
        entry_id = monitor.start(
            endpoint = "/v1/responses",
            method = "POST",
            model = "m",
            prompt = "private",
            subject = "alice",
            via_api_key = True,
        )
        monitor.set_usage(entry_id, prompt_tokens = 2, completion_tokens = 3, total_tokens = 5)

        started = time.perf_counter()
        monitor.finish(entry_id)
        elapsed = time.perf_counter() - started
        # This file runs in the serial CI leg, so a 1s bound only rules out lock queueing.
        assert elapsed < 1.0, f"finish() blocked on the locked database: {elapsed:.3f}s"
        assert (
            lock_conn.execute(
                "SELECT COUNT(*) FROM api_usage_events WHERE id = ?", (entry_id,)
            ).fetchone()[0]
            == 0
        )

        lock_conn.rollback()
        deadline = time.monotonic() + 5
        row = None
        while time.monotonic() < deadline:
            check = studio_db.get_connection()
            try:
                row = check.execute(
                    "SELECT total_tokens FROM api_usage_events WHERE id = ?", (entry_id,)
                ).fetchone()
            finally:
                check.close()
            if row is not None:
                break
            time.sleep(0.02)
        assert row is not None
        assert row[0] == 5
    finally:
        if lock_conn.in_transaction:
            lock_conn.rollback()
        lock_conn.close()
        if callback_lease is not None:
            monitor.release_terminal_callback(callback_lease)
        release_api_usage_writer(writer_lease)


def test_writer_stop_drains_accepted_receipts_and_rejects_late_submit():
    entered = threading.Event()
    release = threading.Event()
    persisted = []

    def blocking_sink(receipt):
        entered.set()
        release.wait(timeout = 2)
        persisted.append(receipt.id)
        return True

    writer = ApiUsageWriter(sink = blocking_sink)
    first = _api_receipt("accepted-before-stop", datetime.now(timezone.utc))
    late = _api_receipt("late-after-stop", datetime.now(timezone.utc))
    assert writer.submit(first)
    assert entered.wait(timeout = 1)

    stopper = threading.Thread(target = writer.stop)
    stopper.start()
    try:
        deadline = time.monotonic() + 1
        while not writer._stopped and time.monotonic() < deadline:
            time.sleep(0.001)
        assert writer._stopped
        assert not writer.submit(late)
        assert stopper.is_alive()
    finally:
        release.set()
        stopper.join(timeout = 2)
    assert not stopper.is_alive()
    assert persisted == ["accepted-before-stop"]


def test_writer_retains_busy_receipt_after_inner_retry_budget(stats_db, monkeypatch):
    attempts = 0
    persisted = []

    def busy_then_success(receipt):
        nonlocal attempts
        attempts += 1
        if attempts <= api_usage_db._WRITE_RETRIES + 2:
            raise sqlite3.OperationalError("database is locked")
        persisted.append(receipt.id)
        return True

    monkeypatch.setattr(api_usage_db, "_insert_api_usage", busy_then_success)
    monkeypatch.setattr(api_usage_db, "_sleep_after_busy", lambda _delay: None)
    writer = ApiUsageWriter()
    receipt = _api_receipt("busy-past-inner-budget", datetime.now(timezone.utc))

    assert writer.submit(receipt)
    writer.stop()

    assert attempts == api_usage_db._WRITE_RETRIES + 3
    assert persisted == [receipt.id]


def test_writer_busy_shutdown_is_bounded_then_drains_after_unlock(monkeypatch, caplog):
    entered = threading.Event()
    unlocked = threading.Event()
    persisted = []

    def locked_sink(receipt):
        entered.set()
        if not unlocked.is_set():
            raise sqlite3.OperationalError("database is locked")
        persisted.append(receipt.id)
        return True

    monkeypatch.setattr(
        api_usage_db,
        "_sleep_after_busy",
        lambda _delay: unlocked.wait(timeout = 0.01),
    )
    writer = ApiUsageWriter(sink = locked_sink)
    receipt = _api_receipt("busy-through-shutdown", datetime.now(timezone.utc))
    assert writer.submit(receipt)
    assert entered.wait(timeout = 1)

    # Assert what stop() hands its join, not wall-clock time (too loose under -n 4).
    joins: list[float | None] = []
    joining = writer._thread.join

    def record_join(timeout = None):
        joins.append(timeout)
        return joining(timeout = timeout)

    monkeypatch.setattr(writer._thread, "join", record_join)

    try:
        started = time.perf_counter()
        assert writer.stop(timeout = 0.03) is False
        elapsed = time.perf_counter() - started
        assert joins == [0.03], f"stop() did not wait for exactly its own timeout: {joins}"
        assert elapsed < 1.0, f"stop() ignored its 0.03s timeout: {elapsed:.3f}s"
        assert not writer.submit(_api_receipt("rejected-after-stop", datetime.now(timezone.utc)))
        assert writer._thread.is_alive()
        assert "may be lost if the process exits" in caplog.text
    finally:
        unlocked.set()
        writer._thread.join(timeout = 1)
    assert not writer._thread.is_alive()
    assert writer.stop(timeout = 0) is True
    assert persisted == [receipt.id]


def test_timed_out_writer_release_allows_successor_lifespan(monkeypatch):
    created = []

    class TimedOutWriter:
        def __init__(self):
            self.stopped = False
            created.append(self)

        def submit(self, _receipt):
            return not self.stopped

        def stop(self):
            self.stopped = True
            return False

    monkeypatch.setattr(api_usage_db, "ApiUsageWriter", TimedOutWriter)
    first_lease = acquire_api_usage_writer()
    release_api_usage_writer(first_lease)
    assert api_usage_db._writer is None
    assert api_usage_db._writer_stopping is False

    second_lease = acquire_api_usage_writer()
    try:
        assert len(created) == 2
        assert created[0] is not created[1]
    finally:
        release_api_usage_writer(second_lease)


def test_full_uuid_request_ids_do_not_collapse_same_prefix(stats_db, monkeypatch):
    uuid_hexes = iter(
        [
            "0123456789ab00000000000000000001",
            "0123456789abffffffffffffffffffff",
        ]
    )
    monkeypatch.setattr(
        api_monitor_module.uuid,
        "uuid4",
        lambda: SimpleNamespace(hex = next(uuid_hexes)),
    )
    monitor = ApiMonitor(max_entries = 0, terminal_callback = record_api_usage)
    ids = []
    for total in (3, 7):
        entry_id = monitor.start(
            endpoint = "/v1/messages",
            method = "POST",
            model = "m",
            prompt = "private",
            subject = "alice",
            via_api_key = True,
        )
        ids.append(entry_id)
        monitor.set_usage(entry_id, total_tokens = total)
        monitor.finish(entry_id)

    assert ids[0] != ids[1]
    assert ids[0][:19] == ids[1][:19]
    conn = studio_db.get_connection()
    try:
        rows = conn.execute(
            "SELECT id, total_tokens FROM api_usage_events ORDER BY total_tokens"
        ).fetchall()
    finally:
        conn.close()
    assert [(row["id"], row["total_tokens"]) for row in rows] == [(ids[0], 3), (ids[1], 7)]


def test_terminal_receipt_canonicalizes_unpaired_model_surrogate():
    receipts = []
    monitor = ApiMonitor(terminal_callback = receipts.append)
    entry_id = monitor.start(
        endpoint = "/v1/chat/completions",
        method = "POST",
        model = "\ud800" + "m" * MAX_MODEL_CHARS,
        prompt = "private",
        subject = "alice",
        via_api_key = True,
    )
    monitor.set_usage(entry_id, total_tokens = 1)

    monitor.finish(entry_id)

    assert len(receipts) == 1
    assert len(receipts[0].model) == MAX_MODEL_CHARS
    receipts[0].model.encode("utf-8")


def test_api_usage_bounds_every_durable_text_field(stats_db):
    now = datetime.now(timezone.utc)
    long_model_prefix = "m" * (MAX_MODEL_CHARS + 100)
    receipt = ApiUsageReceipt(
        id = "i" * MAX_RECEIPT_ID_CHARS,
        subject = "s" * MAX_SUBJECT_CHARS,
        endpoint = "e" * (MAX_ENDPOINT_CHARS + 100),
        model = long_model_prefix + "a",
        status = "x" * (MAX_STATUS_CHARS + 100),
        prompt_tokens = 1,
        completion_tokens = 2,
        total_tokens = 3,
        created_at = _ms(now),
    )
    assert record_api_usage(receipt)
    assert not record_api_usage(_api_receipt("i" * (MAX_RECEIPT_ID_CHARS + 1), now))
    long_subject = "s" * (MAX_SUBJECT_CHARS + 100)
    assert record_api_usage(_api_receipt("oversized-subject", now, subject = long_subject))
    assert record_api_usage(_api_receipt("second-long-model", now, model = long_model_prefix + "b"))

    conn = studio_db.get_connection()
    try:
        row = conn.execute(
            "SELECT id, subject, endpoint, model, status FROM api_usage_events WHERE id = ?",
            (receipt.id,),
        ).fetchone()
        other_model = conn.execute(
            "SELECT model FROM api_usage_events WHERE id = 'second-long-model'"
        ).fetchone()[0]
        stored_long_subject = conn.execute(
            "SELECT subject FROM api_usage_events WHERE id = 'oversized-subject'"
        ).fetchone()[0]
    finally:
        conn.close()
    assert len(row["id"]) == MAX_RECEIPT_ID_CHARS
    assert len(row["subject"]) == MAX_SUBJECT_CHARS
    assert len(row["endpoint"]) == MAX_ENDPOINT_CHARS
    assert len(row["model"]) == MAX_MODEL_CHARS
    assert len(row["status"]) == MAX_STATUS_CHARS
    assert len(stored_long_subject) == MAX_SUBJECT_CHARS
    assert row["model"] != other_model
    assert (
        compute_profile_stats(days = 1, tz_name = "UTC", subject = long_subject)["totals"]["apiTokens"]
        == 50
    )


def test_api_fingerprint_invalidates_cache_for_independent_database_writers(stats_db):
    now = datetime.now(timezone.utc)
    cached = compute_profile_stats(days = 1, tz_name = "UTC", subject = "alice")
    conn = studio_db.get_connection()
    try:
        conn.execute(
            """
            INSERT INTO api_usage_events
                (id, subject, endpoint, model, status,
                 prompt_tokens, completion_tokens, total_tokens, created_at)
            VALUES ('external-writer', 'alice', '/v1/responses', 'm',
                    'completed', 4, 6, 10, ?)
            """,
            (_ms(now),),
        )
        conn.commit()
    finally:
        conn.close()

    refreshed = compute_profile_stats(days = 1, tz_name = "UTC", subject = "alice")
    assert refreshed is not cached
    assert refreshed["totals"]["apiTokens"] == 10


def test_api_usage_filters_internal_and_zero_receipts_but_keeps_partial_failures(stats_db):
    now = datetime.now(timezone.utc)
    assert record_api_usage(_api_receipt("internal", now, via_api_key = False)) is False
    assert (
        record_api_usage(_api_receipt("zero", now, prompt = 0, completion = 0, total = 0, status = "error"))
        is False
    )
    assert (
        record_api_usage(
            _api_receipt(
                "partial",
                now,
                prompt = 3,
                completion = 4,
                total = 9,
                status = "cancelled",
            )
        )
        is True
    )

    stats = compute_profile_stats(days = 1, tz_name = "UTC", subject = "external-client")
    assert stats["totals"]["apiPromptTokens"] == 3
    assert stats["totals"]["apiCompletionTokens"] == 4
    assert stats["totals"]["apiTokens"] == 9
    conn = studio_db.get_connection()
    try:
        columns = {row[1] for row in conn.execute("PRAGMA table_info(api_usage_events)")}
        assert "prompt" not in columns
        assert "reply" not in columns
        assert "api_key" not in columns
    finally:
        conn.close()


def test_api_usage_uses_iana_timezone_across_dst_dates(stats_db):
    winter = datetime(2026, 1, 15, 4, 30, tzinfo = timezone.utc)
    summer = datetime(2026, 7, 15, 3, 30, tzinfo = timezone.utc)
    assert record_api_usage(_api_receipt("winter", winter, total = 11))
    assert record_api_usage(_api_receipt("summer", summer, total = 13))

    stats = compute_profile_stats(
        days = 366,
        tz_name = "America/New_York",
        subject = "external-client",
    )
    by_date = {day["date"]: day["tokens"] for day in stats["daily"]}
    assert by_date["2026-01-14"] == 11
    assert by_date["2026-07-14"] == 13


def test_existing_database_gets_additive_api_usage_schema(stats_db):
    db_path = studio_db.studio_db_path()
    db_path.parent.mkdir(parents = True, exist_ok = True)
    legacy = sqlite3.connect(db_path)
    try:
        legacy.execute("CREATE TABLE legacy_marker (value TEXT)")
        legacy.execute("INSERT INTO legacy_marker VALUES ('kept')")
        legacy.commit()
    finally:
        legacy.close()

    studio_db._schema_ready = set()
    conn = studio_db.get_connection()
    try:
        assert conn.execute("SELECT value FROM legacy_marker").fetchone()[0] == "kept"
        assert conn.execute(
            "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'api_usage_events'"
        ).fetchone()
    finally:
        conn.close()


@pytest.mark.parametrize(
    "query",
    [
        "SELECT created_at FROM api_usage_events WHERE subject = ? ORDER BY created_at",
        "SELECT COUNT(*), MAX(created_at) FROM api_usage_events WHERE subject = ?",
    ],
)
def test_api_usage_subject_queries_use_composite_index(stats_db, query):
    conn = studio_db.get_connection()
    try:
        plan = conn.execute(f"EXPLAIN QUERY PLAN {query}", ("alice",)).fetchall()
    finally:
        conn.close()

    detail = " ".join(str(row["detail"]) for row in plan)
    assert "SEARCH" in detail
    assert "idx_api_usage_events_subject_created_at" in detail


def test_tokens_streaks_and_models_are_aggregated(stats_db):
    today = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(
            conn,
            "t1",
            "unsloth/gpt-oss-20b",
            [
                (today - timedelta(days = 2), _metadata(100, 50, speed = 30.0, tools = 2)),
                (today - timedelta(days = 1), _metadata(200, 80, speed = 55.0)),
                (today, _metadata(300, 120, speed = 120.0, tools = 1)),
            ],
        )
        conn.commit()
    finally:
        conn.close()

    stats = compute_profile_stats(days = 30)

    totals = stats["totals"]
    assert totals["threads"] == 1
    assert totals["messages"] == 6
    assert totals["userMessages"] == 3
    assert totals["assistantMessages"] == 3
    assert totals["promptTokens"] == 600
    assert totals["completionTokens"] == 250
    assert totals["totalTokens"] == 850
    assert totals["cachedTokens"] == 15
    assert totals["toolCalls"] == 3
    assert totals["activeDays"] == 3

    assert stats["streak"] == {
        "current": 3,
        "longest": 3,
        "lastActiveDay": today.date().isoformat(),
    }
    assert stats["peakDay"] == {"date": today.date().isoformat(), "tokens": 420}
    assert stats["models"][0]["id"] == "unsloth/gpt-oss-20b"
    assert stats["models"][0]["label"] == "gpt-oss-20b"
    assert stats["models"][0]["messages"] == 3
    assert stats["speed"]["bestTokensPerSecond"] == 120.0
    assert stats["speed"]["averageTokensPerSecond"] == pytest.approx(68.333, rel = 1e-3)

    assert stats["longestChat"]["seconds"] == 30
    assert stats["longestChat"]["messages"] == 6


def test_completion_tokens_fall_back_to_adapter_count(stats_db):
    """Local engines can omit the usage chunk; timing.tokenCount stands in."""
    now = datetime.now().replace(hour = 9, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(
            conn,
            "t2",
            "local-gguf",
            [(now, {"timing": {"tokenCount": 64, "tokensPerSecond": 12.0}})],
        )
        conn.commit()
    finally:
        conn.close()

    stats = compute_profile_stats(days = 7)

    assert stats["totals"]["completionTokens"] == 64
    assert stats["totals"]["totalTokens"] == 64
    # No modelId on the turn: thread model_id follows the current selection and would misattribute.
    assert stats["models"] == []


def test_session_time_ignores_long_idle_gaps(stats_db):
    """A thread reopened days later must not count the idle time as chatting."""
    start = datetime.now().replace(hour = 10, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(
            conn,
            "t3",
            "m",
            [(start - timedelta(days = 3), _metadata(10, 10)), (start, _metadata(10, 10))],
        )
        conn.commit()
    finally:
        conn.close()

    stats = compute_profile_stats(days = 30)

    assert stats["longestChat"]["seconds"] == 20
    assert stats["totals"]["chatSeconds"] == 20


def test_broken_metadata_does_not_break_aggregation(stats_db):
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        conn.execute(
            "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, updated_at) "
            "VALUES ('t4', 'Broken', 'base', 'm', ?, ?)",
            (_ms(now), _ms(now)),
        )
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, created_at) "
            "VALUES ('t4-a0', 't4', 'assistant', '[]', ?, ?)",
            ("{not json", _ms(now)),
        )
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, created_at) "
            "VALUES ('t4-a1', 't4', 'assistant', '[]', ?, ?)",
            (json.dumps({"contextUsage": {"totalTokens": "lots"}}), _ms(now)),
        )
        conn.commit()
    finally:
        conn.close()

    stats = compute_profile_stats(days = 7)

    assert stats["totals"]["assistantMessages"] == 2
    assert stats["totals"]["totalTokens"] == 0


def test_training_totals(stats_db):
    conn = studio_db.get_connection()
    try:
        conn.execute(
            "INSERT INTO training_runs (id, status, model_name, dataset_name, config_json, "
            "started_at, ended_at, total_steps, final_step, final_loss, duration_seconds) "
            "VALUES ('r1', 'completed', 'unsloth/llama-3-8b', 'tatsu-lab/alpaca', '{}', "
            "'2026-01-01T10:00:00', '2026-01-01T11:00:00', 100, 100, 0.42, 3600)",
        )
        conn.execute(
            "INSERT INTO training_runs (id, status, model_name, dataset_name, config_json, "
            "started_at, total_steps, final_step, final_loss, duration_seconds) "
            "VALUES ('r2', 'error', 'unsloth/qwen3-4b', 'my/dataset', '{}', "
            "'2026-01-02T10:00:00', 100, 20, 1.8, 600)",
        )
        conn.executemany(
            "INSERT INTO training_metrics (run_id, step, loss, num_tokens) VALUES (?, ?, ?, ?)",
            [("r1", step, 1.0, (step + 1) * 1000) for step in range(10)],
        )
        conn.commit()
    finally:
        conn.close()

    stats = compute_profile_stats(days = 7)

    training = stats["training"]
    assert training["runs"] == 2
    assert training["completed"] == 1
    assert training["steps"] == 120
    assert training["tokens"] == 10_000
    assert training["seconds"] == 4200
    assert training["models"] == 2
    assert training["bestLoss"] == pytest.approx(0.42)
    assert training["recent"][0]["id"] == "r2"
    assert training["recent"][0]["modelLabel"] == "qwen3-4b"


def test_first_token_time_is_read_as_a_duration(stats_db):
    """firstTokenTime is a duration (Date.now() minus streamStartTime), not a timestamp to subtract."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "tft", "m", [(now, _metadata(10, 10))])
        conn.commit()
    finally:
        conn.close()

    stats = compute_profile_stats(days = 7)

    assert stats["speed"]["averageFirstTokenMs"] == pytest.approx(200.0)


def test_forked_threads_do_not_double_count_copied_history(stats_db):
    """Forking clones the ancestry, so the copies must not be counted again."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "src", "m", [(now - timedelta(hours = 2), _metadata(100, 50))])
        conn.commit()
    finally:
        conn.close()

    before = compute_profile_stats(days = 7)
    assert before["totals"]["totalTokens"] == 150
    assert before["totals"]["messages"] == 2

    fork_at = now
    conn = studio_db.get_connection()
    try:
        conn.execute(
            "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, "
            "updated_at, forked_from_thread_id, forked_from_message_id) "
            "VALUES ('fork', 'fork of src', 'base', 'm', ?, ?, 'src', 'src-a0')",
            (_ms(fork_at), _ms(fork_at)),
        )
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, "
            "created_at) VALUES ('fork-a0', 'fork', 'assistant', '[]', ?, ?)",
            (json.dumps(_metadata(100, 50)), _ms(now - timedelta(hours = 2) + REPLY_DELAY)),
        )
        conn.commit()
    finally:
        conn.close()

    invalidate_profile_stats_cache()
    after = compute_profile_stats(days = 7)

    assert after["totals"]["totalTokens"] == 150
    assert after["totals"]["messages"] == 2

    conn = studio_db.get_connection()
    try:
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, "
            "created_at) VALUES ('fork-a1', 'fork', 'assistant', '[]', ?, ?)",
            (json.dumps(_metadata(10, 5)), _ms(fork_at + timedelta(minutes = 1))),
        )
        conn.commit()
    finally:
        conn.close()

    invalidate_profile_stats_cache()
    grown = compute_profile_stats(days = 7)
    assert grown["totals"]["totalTokens"] == 165
    assert grown["totals"]["messages"] == 3


def test_resumed_runs_do_not_double_count_steps_or_tokens(stats_db):
    """A resume continues the source's counters, so only the tail is counted."""
    conn = studio_db.get_connection()
    try:
        # The resume claim sets resume_blocked and keeps output_dir, unlike a cancel.
        _shared_setup_1(conn)
        conn.execute(
            "INSERT INTO training_runs (id, status, model_name, dataset_name, config_json, "
            "started_at, total_steps, final_step, duration_seconds, output_dir, resume_blocked) "
            "VALUES ('cont', 'completed', 'm', 'd', '{}', '2026-01-02T10:00:00', 20, 15, 300, "
            "'/runs/out', 0)",
        )
        conn.executemany(
            "INSERT INTO training_metrics (run_id, step, num_tokens) VALUES (?, ?, ?)",
            [("src", step, step * 100) for step in range(1, 11)]
            + [("cont", step, step * 100) for step in range(11, 16)],
        )
        conn.commit()
    finally:
        conn.close()

    training = compute_profile_stats(days = 7)["training"]

    assert training["steps"] == 15
    assert training["tokens"] == 1500
    assert training["runs"] == 2


def test_cancelled_runs_keep_the_work_they_did(stats_db):
    """Cancelling sets resume_blocked too, but nothing resumed from that run."""
    conn = studio_db.get_connection()
    try:
        conn.execute(
            "INSERT INTO training_runs (id, status, model_name, dataset_name, config_json, "
            "started_at, total_steps, final_step, duration_seconds, output_dir, resume_blocked) "
            "VALUES ('cancelled', 'stopped', 'm', 'd', '{}', '2026-01-01T10:00:00', 20, 8, "
            "400, NULL, 1)",
        )
        conn.executemany(
            "INSERT INTO training_metrics (run_id, step, num_tokens) VALUES (?, ?, ?)",
            [("cancelled", step, step * 100) for step in range(1, 9)],
        )
        conn.commit()
    finally:
        conn.close()

    training = compute_profile_stats(days = 7)["training"]

    assert training["runs"] == 1
    assert training["steps"] == 8
    assert training["tokens"] == 800


def test_forks_count_as_chats_before_their_first_new_turn(stats_db):
    """A fork is a visible thread the moment it exists."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "orig", "m", [(now - timedelta(hours = 2), _metadata(100, 50))])
        conn.execute(
            "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, "
            "updated_at, forked_from_thread_id, forked_from_message_id) "
            "VALUES ('branch', 'fork', 'base', 'm', ?, ?, 'orig', 'orig-a0')",
            (_ms(now), _ms(now)),
        )
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, "
            "created_at) VALUES ('branch-a0', 'branch', 'assistant', '[]', ?, ?)",
            (json.dumps(_metadata(100, 50)), _ms(now - timedelta(hours = 2) + REPLY_DELAY)),
        )
        conn.commit()
    finally:
        conn.close()

    stats = compute_profile_stats(days = 7)

    assert stats["totals"]["threads"] == 2
    assert stats["totals"]["messages"] == 2
    assert stats["totals"]["totalTokens"] == 150


def test_tokens_follow_the_model_that_answered(stats_db):
    """A routed provider records the real producer in responseDetails."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    metadata = _metadata(100, 50)
    metadata["contextUsage"]["modelId"] = "openrouter/auto"
    metadata["responseDetails"] = {"responseModelId": "anthropic/claude-sonnet-4"}
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "routed", "openrouter/auto", [(now, metadata)])
        conn.commit()
    finally:
        conn.close()

    stats = compute_profile_stats(days = 7)

    assert stats["models"][0]["id"] == "anthropic/claude-sonnet-4"
    assert stats["models"][0]["tokens"] == 150


def test_recent_run_name_prefers_the_users_rename(stats_db):
    conn = studio_db.get_connection()
    try:
        conn.execute(
            "INSERT INTO training_runs (id, status, model_name, dataset_name, config_json, "
            "started_at, display_name) VALUES ('named', 'completed', 'unsloth/llama-3-8b', "
            "'tatsu-lab/alpaca', '{}', '2026-01-02T10:00:00', 'Support triage v3')",
        )
        conn.execute(
            "INSERT INTO training_runs (id, status, model_name, dataset_name, config_json, "
            "started_at) VALUES ('plain', 'completed', 'unsloth/qwen3-4b', 'my/dataset', '{}', "
            "'2026-01-01T10:00:00')",
        )
        conn.commit()
    finally:
        conn.close()

    recent = {run["id"]: run for run in compute_profile_stats(days = 7)["training"]["recent"]}

    assert recent["named"]["name"] == "Support triage v3"
    assert recent["named"]["modelLabel"] == "llama-3-8b"
    assert recent["plain"]["name"] == "qwen3-4b"


def test_historical_daylight_saving_offsets_are_respected(stats_db):
    """Historical DST offsets matter: a fixed summer offset moves a winter message across midnight."""
    winter = datetime(2026, 1, 15, 4, 30, tzinfo = timezone.utc)
    conn = studio_db.get_connection()
    try:
        conn.execute(
            "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, updated_at) "
            "VALUES ('dst', 'dst', 'base', 'm', ?, ?)",
            (int(winter.timestamp() * 1000), int(winter.timestamp() * 1000)),
        )
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, "
            "created_at) VALUES ('dst-a0', 'dst', 'assistant', '[]', ?, ?)",
            (json.dumps(_metadata(10, 10)), int(winter.timestamp() * 1000)),
        )
        conn.commit()
    finally:
        conn.close()

    named = compute_profile_stats(days = 366, tz_offset_minutes = 240, tz_name = "America/New_York")
    invalidate_profile_stats_cache()
    offset_only = compute_profile_stats(days = 366, tz_offset_minutes = 240)

    assert {day["date"] for day in named["daily"] if day["messages"]} == {"2026-01-14"}

    assert {day["date"] for day in offset_only["daily"] if day["messages"]} == {"2026-01-15"}


def test_unknown_timezone_falls_back_to_the_offset(stats_db):
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "tzbad", "m", [(now, _metadata(10, 10))])
        conn.commit()
    finally:
        conn.close()

    stats = compute_profile_stats(days = 7, tz_offset_minutes = 0, tz_name = "Not/A/Zone")

    assert stats["totals"]["totalTokens"] == 20


def test_deleting_the_source_thread_keeps_the_forks_copies(stats_db):
    """forked_from_thread_id is not a foreign key, so it outlives the source."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "gone", "m", [(now - timedelta(hours = 2), _metadata(100, 50))])
        conn.execute(
            "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, "
            "updated_at, forked_from_thread_id, forked_from_message_id) "
            "VALUES ('kept', 'fork', 'base', 'm', ?, ?, 'gone', 'gone-a0')",
            (_ms(now), _ms(now)),
        )
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, "
            "created_at) VALUES ('kept-a0', 'kept', 'assistant', '[]', ?, ?)",
            (json.dumps(_metadata(100, 50)), _ms(now - timedelta(hours = 2) + REPLY_DELAY)),
        )
        conn.commit()
    finally:
        conn.close()

    assert compute_profile_stats(days = 7)["totals"]["totalTokens"] == 150

    conn = studio_db.get_connection()
    try:
        conn.execute("DELETE FROM chat_threads WHERE id = 'gone'")
        conn.commit()
    finally:
        conn.close()

    invalidate_profile_stats_cache()
    stats = compute_profile_stats(days = 7)

    assert stats["totals"]["totalTokens"] == 150
    assert stats["totals"]["messages"] == 1


def test_sibling_forks_do_not_multiply_a_deleted_source(stats_db):
    """Two forks of one thread must not both re-count the shared ancestry."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    older = now - timedelta(hours = 3)
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "root", "m", [(older, _metadata(100, 50))])
        for fork_id in ("forkA", "forkB"):
            conn.execute(
                "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, "
                "updated_at, forked_from_thread_id, forked_from_message_id) "
                "VALUES (?, 'fork', 'base', 'm', ?, ?, 'root', 'root-a0')",
                (fork_id, _ms(now), _ms(now)),
            )
            conn.execute(
                "INSERT INTO chat_messages (id, thread_id, role, content_json, "
                "metadata_json, created_at) VALUES (?, ?, 'assistant', '[]', ?, ?)",
                (
                    f"{fork_id}-a0",
                    fork_id,
                    json.dumps(_metadata(100, 50)),
                    _ms(older + REPLY_DELAY),
                ),
            )
        conn.commit()
    finally:
        conn.close()

    assert compute_profile_stats(days = 7)["totals"]["totalTokens"] == 150

    conn = studio_db.get_connection()
    try:
        conn.execute("DELETE FROM chat_threads WHERE id = 'root'")
        conn.commit()
    finally:
        conn.close()

    invalidate_profile_stats_cache()
    stats = compute_profile_stats(days = 7)

    assert stats["totals"]["totalTokens"] == 150
    assert stats["totals"]["messages"] == 1


def test_deleting_an_original_message_keeps_the_forks_clone(stats_db):
    """Pruning one pre-fork message leaves the clone as the only copy."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    older = now - timedelta(hours = 3)
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "orig", "m", [(older, _metadata(100, 50))])
        conn.execute(
            "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, "
            "updated_at, forked_from_thread_id, forked_from_message_id) "
            "VALUES ('branch', 'fork', 'base', 'm', ?, ?, 'orig', 'orig-a0')",
            (_ms(now), _ms(now)),
        )
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, "
            "created_at) VALUES ('branch-a0', 'branch', 'assistant', '[]', ?, ?)",
            (json.dumps(_metadata(100, 50)), _ms(older + timedelta(seconds = 10))),
        )
        conn.commit()
    finally:
        conn.close()

    assert compute_profile_stats(days = 7)["totals"]["totalTokens"] == 150

    conn = studio_db.get_connection()
    try:
        conn.execute("DELETE FROM chat_messages WHERE id = 'orig-a0'")
        conn.commit()
    finally:
        conn.close()

    invalidate_profile_stats_cache()
    stats = compute_profile_stats(days = 7)

    assert stats["totals"]["totalTokens"] == 150
    assert stats["totals"]["assistantMessages"] == 1


def test_future_history_cannot_pad_the_longest_streak(stats_db):
    """Only the current streak was guarded; longest and lastActiveDay were not."""
    base = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(
            conn,
            "skew",
            "m",
            [(base + timedelta(days = day), _metadata(10, 10)) for day in (3, 4, 5, 6)],
        )
        conn.commit()
    finally:
        conn.close()

    streak = compute_profile_stats(days = 366)["streak"]

    assert streak == {"current": 0, "longest": 0, "lastActiveDay": None}


def test_comparison_panes_count_as_one_chat(stats_db):
    """Compare mode stores a thread per pane; the sidebar shows one chat."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        for pane in ("left", "right"):
            conn.execute(
                "INSERT INTO chat_threads (id, title, model_type, model_id, pair_id, "
                "created_at, updated_at) VALUES (?, 'compare', 'base', 'm', 'pair-1', ?, ?)",
                (pane, _ms(now), _ms(now)),
            )
            conn.execute(
                "INSERT INTO chat_messages (id, thread_id, role, content_json, "
                "metadata_json, created_at) VALUES (?, ?, 'assistant', '[]', ?, ?)",
                (f"{pane}-a0", pane, json.dumps(_metadata(100, 50)), _ms(now)),
            )
        conn.commit()
    finally:
        conn.close()

    stats = compute_profile_stats(days = 7)

    assert stats["totals"]["threads"] == 1
    assert stats["totals"]["messages"] == 2
    assert stats["totals"]["totalTokens"] == 300


def test_deleting_a_continuation_restores_its_source(stats_db):
    """delete_run leaves resume_blocked set, so supersession needs a live tail."""
    conn = studio_db.get_connection()
    try:
        conn.execute(
            "INSERT INTO training_runs (id, status, model_name, dataset_name, config_json, "
            "started_at, total_steps, final_step, output_dir, resume_blocked) "
            "VALUES ('src', 'stopped', 'm', 'd', '{}', '2026-01-01T10:00:00', 20, 10, "
            "'/runs/out', 1)",
        )
        conn.execute(
            "INSERT INTO training_runs (id, status, model_name, dataset_name, config_json, "
            "started_at, total_steps, final_step, output_dir, resume_blocked) "
            "VALUES ('cont', 'completed', 'm', 'd', '{}', '2026-01-02T10:00:00', 20, 15, "
            "'/runs/out', 0)",
        )
        conn.commit()
    finally:
        conn.close()

    assert compute_profile_stats(days = 7)["training"]["steps"] == 15

    conn = studio_db.get_connection()
    try:
        conn.execute("DELETE FROM training_runs WHERE id = 'cont'")
        conn.commit()
    finally:
        conn.close()

    invalidate_profile_stats_cache()

    assert compute_profile_stats(days = 7)["training"]["steps"] == 10


def test_out_of_range_timestamps_do_not_break_the_panel(stats_db):
    """created_at is client supplied and SQLite stores it unchecked. Seeded at noon like the rest of the file:
    seeded at the wall clock, a run in the last REPLY_DELAY before midnight put the reply on tomorrow, outside
    the daily series."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "sane", "m", [(now, _metadata(10, 10))])
        conn.execute(
            "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, updated_at) "
            "VALUES ('bad', 'bad', 'base', 'm', 1, 1)",
        )
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, "
            "created_at) VALUES ('bad-a0', 'bad', 'assistant', '[]', ?, ?)",
            (json.dumps(_metadata(7, 3)), 99_999_999_999_999_999),
        )
        conn.commit()
    finally:
        conn.close()

    stats = compute_profile_stats(days = 7)

    assert stats["totals"]["totalTokens"] == 30
    assert stats["totals"]["messages"] == 3
    assert stats["totals"]["activeDays"] == 1
    assert sum(day["messages"] for day in stats["daily"]) == 2


def test_future_dated_history_is_not_a_current_streak(stats_db):
    """A client clock that ran ahead must not report a streak that has not happened."""
    future = datetime.now() + timedelta(days = 5)
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "ahead", "m", [(future, _metadata(10, 10))])
        conn.commit()
    finally:
        conn.close()

    streak = compute_profile_stats(days = 366)["streak"]

    assert streak["current"] == 0


def test_days_and_hours_use_the_callers_timezone(stats_db):
    """A remote browser must not be bucketed against the server's calendar."""
    when = datetime(2026, 3, 10, 1, 30, tzinfo = timezone.utc)
    conn = studio_db.get_connection()
    try:
        conn.execute(
            "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, updated_at) "
            "VALUES ('tz', 'tz', 'base', 'm', ?, ?)",
            (int(when.timestamp() * 1000), int(when.timestamp() * 1000)),
        )
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, "
            "created_at) VALUES ('tz-a0', 'tz', 'assistant', '[]', ?, ?)",
            (json.dumps(_metadata(10, 10)), int(when.timestamp() * 1000)),
        )
        conn.commit()
    finally:
        conn.close()

    at_utc = compute_profile_stats(days = 366, tz_offset_minutes = 0)
    invalidate_profile_stats_cache()
    at_minus_four = compute_profile_stats(days = 366, tz_offset_minutes = 240)

    utc_days = {day["date"] for day in at_utc["daily"] if day["messages"]}
    local_days = {day["date"] for day in at_minus_four["daily"] if day["messages"]}
    assert utc_days == {"2026-03-10"}
    assert local_days == {"2026-03-09"}


def test_repeat_calls_are_served_from_cache_until_history_changes(stats_db):
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "t5", "m", [(now, _metadata(10, 10))])
        conn.commit()
    finally:
        conn.close()

    first = compute_profile_stats(days = 7)
    second = compute_profile_stats(days = 7)
    assert first is second

    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "t6", "m", [(now, _metadata(20, 20))])
        conn.commit()
    finally:
        conn.close()

    third = compute_profile_stats(days = 7)
    assert third is not first
    assert third["totals"]["totalTokens"] == 60


def test_daily_series_is_dense_and_clamped(stats_db):
    stats = compute_profile_stats(days = 10_000)
    assert len(stats["daily"]) == profile_stats_db.MAX_DAILY_DAYS
    dates = [day["date"] for day in stats["daily"]]
    assert dates == sorted(dates)
    assert len(set(dates)) == len(dates)


def test_route_does_not_block_the_event_loop(stats_db, monkeypatch):
    """Stats aggregation runs in a worker thread so a cold pass cannot stall the event loop."""
    import asyncio

    from routes import profile_stats as route_module

    def slow_compute(
        days = 366,
        tz_offset_minutes = 0,
        tz_name = "",
        subject = "",
    ):
        time.sleep(0.5)
        return {"totals": {"messages": 0}}

    monkeypatch.setattr(route_module, "compute_profile_stats", slow_compute)

    async def drive() -> int:
        ticks = 0

        async def heartbeat() -> None:
            nonlocal ticks
            while True:
                await asyncio.sleep(0.01)
                ticks += 1

        beat = asyncio.create_task(heartbeat())
        try:
            await route_module.get_profile_stats(
                days = 366, tz_offset_minutes = 0, current_subject = "unsloth"
            )
        finally:
            beat.cancel()
        return ticks

    ticks = asyncio.run(drive())

    # ~50 ticks fit in 0.5s; a blocking call on the loop would yield 0.
    assert ticks > 10, f"event loop stalled during stats computation ({ticks} ticks)"


def test_an_oversized_token_counter_does_not_break_the_panel(stats_db):
    """json parses ints of any width; float() gives up long before that."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "sane", "m", [(now - timedelta(hours = 1), _metadata(100, 50))])
        conn.execute(
            "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, updated_at) "
            "VALUES ('bad', 'Bad', 'base', 'm', ?, ?)",
            (_ms(now), _ms(now)),
        )
        huge = _metadata(100, 50)
        oversized = int("9" * 309)
        for field in ("promptTokens", "completionTokens", "totalTokens"):
            huge["contextUsage"][field] = oversized
        huge["timing"]["tokenCount"] = oversized
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, "
            "created_at) VALUES ('bad-a0', 'bad', 'assistant', '[]', ?, ?)",
            (json.dumps(huge), _ms(now)),
        )
        conn.commit()
    finally:
        conn.close()

    stats = compute_profile_stats(days = 7)
    assert stats["totals"]["totalTokens"] == 150
    assert stats["totals"]["messages"] == 3


def test_divergent_sibling_forks_keep_their_own_branch_messages(stats_db):
    """fork_chat_thread copies one parent_id branch, so siblings differ."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    older = now - timedelta(hours = 3)
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "root", "m", [(older, _metadata(100, 50))])
        regenerated = older + REPLY_DELAY + timedelta(seconds = 1)
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, "
            "created_at) VALUES ('root-a1', 'root', 'assistant', '[]', ?, ?)",
            (json.dumps(_metadata(200, 100)), _ms(regenerated)),
        )
        for fork_id, stamp, meta in (
            ("forkA", older + REPLY_DELAY, _metadata(100, 50)),
            ("forkB", regenerated, _metadata(200, 100)),
        ):
            conn.execute(
                "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, "
                "updated_at, forked_from_thread_id, forked_from_message_id) "
                "VALUES (?, 'fork', 'base', 'm', ?, ?, 'root', 'root-a0')",
                (fork_id, _ms(now), _ms(now)),
            )
            conn.execute(
                "INSERT INTO chat_messages (id, thread_id, role, content_json, "
                "metadata_json, created_at) VALUES (?, ?, 'assistant', '[]', ?, ?)",
                (f"{fork_id}-a0", fork_id, json.dumps(meta), _ms(stamp)),
            )
        conn.commit()
    finally:
        conn.close()

    assert compute_profile_stats(days = 7)["totals"]["totalTokens"] == 450

    conn = studio_db.get_connection()
    try:
        conn.execute("DELETE FROM chat_threads WHERE id = 'root'")
        conn.commit()
    finally:
        conn.close()

    invalidate_profile_stats_cache()
    stats = compute_profile_stats(days = 7)

    assert stats["totals"]["totalTokens"] == 450
    assert stats["totals"]["messages"] == 2


def test_a_resume_that_never_logged_a_step_keeps_the_source_counters(stats_db):
    """create_run claims the source before the continuation flushes a metric."""
    conn = studio_db.get_connection()
    try:
        _shared_setup_1(conn)
        conn.execute(
            "INSERT INTO training_runs (id, status, model_name, dataset_name, config_json, "
            "started_at, total_steps, final_step, duration_seconds, output_dir, resume_blocked) "
            "VALUES ('cont', 'error', 'm', 'd', '{}', '2026-01-02T10:00:00', 20, NULL, 5, "
            "'/runs/out', 0)",
        )
        conn.executemany(
            "INSERT INTO training_metrics (run_id, step, num_tokens) VALUES (?, ?, ?)",
            [("src", step, step * 100) for step in range(1, 11)],
        )
        conn.commit()
    finally:
        conn.close()

    training = compute_profile_stats(days = 7)["training"]

    assert training["steps"] == 10
    assert training["tokens"] == 1000


def test_a_future_dated_message_cannot_become_the_peak_day(stats_db):
    """The grid stops at today, so the headlines have to as well."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        _seed_thread(conn, "past", "m", [(now - timedelta(days = 1), _metadata(100, 50))])
        _seed_thread(conn, "ahead", "m", [(now + timedelta(days = 7), _metadata(900, 900))])
        conn.commit()
    finally:
        conn.close()

    stats = compute_profile_stats(days = 30)

    assert stats["peakDay"] is not None
    assert stats["peakDay"]["date"] == (now - timedelta(days = 1)).date().isoformat()
    assert stats["totals"]["activeDays"] == 1


def test_cancelling_a_resumed_run_keeps_the_source_superseded(stats_db):
    """mark_run_cancel_requested nulls output_dir, so lineage carries it."""
    conn = studio_db.get_connection()
    try:
        _shared_setup_1(conn)
        conn.execute(
            "INSERT INTO training_runs (id, status, model_name, dataset_name, config_json, "
            "started_at, total_steps, final_step, duration_seconds, output_dir, resume_blocked, "
            "resumed_from_run_id) "
            "VALUES ('cont', 'stopped', 'm', 'd', '{}', '2026-01-02T10:00:00', 20, 15, 300, "
            "NULL, 1, 'src')",
        )
        conn.executemany(
            "INSERT INTO training_metrics (run_id, step, num_tokens) VALUES (?, ?, ?)",
            [("src", step, step * 100) for step in range(1, 11)]
            + [("cont", step, step * 100) for step in range(11, 16)],
        )
        conn.commit()
    finally:
        conn.close()

    training = compute_profile_stats(days = 7)["training"]

    assert training["steps"] == 15
    assert training["tokens"] == 1500


def test_server_timings_stand_in_for_a_missing_usage_chunk(stats_db):
    """llama.cpp reports its own counters; the details sheet already uses them."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    conn = studio_db.get_connection()
    try:
        conn.execute(
            "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, updated_at) "
            "VALUES ('local', 'Local', 'base', 'm', ?, ?)",
            (_ms(now), _ms(now)),
        )
        metadata = _metadata(0, 0)
        del metadata["contextUsage"]
        metadata["timing"]["tokenCount"] = 0
        metadata["serverTimings"] = {"prompt_n": 400, "predicted_n": 120, "cache_n": 90}
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, "
            "created_at) VALUES ('local-a0', 'local', 'assistant', '[]', ?, ?)",
            (json.dumps(metadata), _ms(now)),
        )
        conn.commit()
    finally:
        conn.close()

    totals = compute_profile_stats(days = 7)["totals"]

    # prompt_n is only what llama.cpp evaluated; the usage chunk it stands in for counts the cache hits too.
    assert totals["promptTokens"] == 490
    assert totals["completionTokens"] == 120
    assert totals["totalTokens"] == 610
    assert totals["cachedTokens"] == 90


def test_nested_fork_keeps_one_copy_after_the_middle_thread_is_deleted(stats_db):
    """Deleting a fork reconnects its descendants to the retained ancestor."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    root_time = now - timedelta(hours = 4)
    middle_time = now - timedelta(hours = 2)
    middle_new_time = now - timedelta(hours = 1)
    conn = studio_db.get_connection()
    try:
        conn.execute(
            "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, updated_at) "
            "VALUES ('root', 'root', 'base', 'm', ?, ?)",
            (_ms(root_time), _ms(root_time)),
        )
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, "
            "created_at) VALUES ('root-a0', 'root', 'assistant', '[]', ?, ?)",
            (json.dumps(_metadata(100, 50)), _ms(root_time)),
        )
        conn.execute(
            "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, updated_at, "
            "forked_from_thread_id, forked_from_message_id) "
            "VALUES ('middle', 'middle', 'base', 'm', ?, ?, 'root', 'root-a0')",
            (_ms(middle_time), _ms(middle_new_time)),
        )
        conn.executemany(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, "
            "created_at) VALUES (?, 'middle', 'assistant', '[]', ?, ?)",
            [
                ("middle-a0", json.dumps(_metadata(100, 50)), _ms(root_time)),
                ("middle-a1", json.dumps(_metadata(20, 10)), _ms(middle_new_time)),
            ],
        )
        conn.execute(
            "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, updated_at, "
            "forked_from_thread_id, forked_from_message_id) "
            "VALUES ('leaf', 'leaf', 'base', 'm', ?, ?, 'middle', 'middle-a1')",
            (_ms(now), _ms(now)),
        )
        conn.executemany(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, metadata_json, "
            "created_at) VALUES (?, 'leaf', 'assistant', '[]', ?, ?)",
            [
                ("leaf-a0", json.dumps(_metadata(100, 50)), _ms(root_time)),
                ("leaf-a1", json.dumps(_metadata(20, 10)), _ms(middle_new_time)),
            ],
        )
        conn.commit()
    finally:
        conn.close()

    before = compute_profile_stats(days = 7)["totals"]
    assert before["messages"] == 2
    assert before["totalTokens"] == 180

    studio_db.delete_chat_threads(["middle"])
    invalidate_profile_stats_cache()

    conn = studio_db.get_connection()
    try:
        leaf = conn.execute(
            "SELECT forked_from_thread_id FROM chat_threads WHERE id = 'leaf'"
        ).fetchone()
    finally:
        conn.close()

    assert leaf["forked_from_thread_id"] == "root"
    after = compute_profile_stats(days = 7)["totals"]
    assert after["messages"] == 2
    assert after["totalTokens"] == 180


def test_nested_resume_keeps_cumulative_totals_after_middle_run_deletion(stats_db):
    """Deleting a resumed run reconnects its continuation to the source."""
    conn = studio_db.get_connection()
    try:
        conn.executemany(
            "INSERT INTO training_runs (id, status, model_name, dataset_name, config_json, "
            "started_at, final_step, output_dir, resume_blocked, resumed_from_run_id) "
            "VALUES (?, ?, 'm', 'd', '{}', ?, ?, ?, ?, ?)",
            [
                ("source", "stopped", "2026-01-01T10:00:00", 10, "/runs/out", 1, None),
                ("middle", "stopped", "2026-01-02T10:00:00", 15, "/runs/out", 1, "source"),
                ("tail", "completed", "2026-01-03T10:00:00", 20, "/runs/out", 0, "middle"),
            ],
        )
        conn.executemany(
            "INSERT INTO training_metrics (run_id, step, num_tokens) VALUES (?, ?, ?)",
            [("source", 10, 1000), ("middle", 15, 1500), ("tail", 20, 2000)],
        )
        conn.commit()
    finally:
        conn.close()

    before = compute_profile_stats(days = 7)["training"]
    assert before["steps"] == 20
    assert before["tokens"] == 2000

    studio_db.delete_run("middle")
    invalidate_profile_stats_cache()

    conn = studio_db.get_connection()
    try:
        tail = conn.execute(
            "SELECT resumed_from_run_id FROM training_runs WHERE id = 'tail'"
        ).fetchone()
    finally:
        conn.close()

    assert tail["resumed_from_run_id"] == "source"
    after = compute_profile_stats(days = 7)["training"]
    assert after["steps"] == 20
    assert after["tokens"] == 2000


def test_inline_media_counts_as_attachments_without_double_counting(stats_db):
    """Compare messages keep uploads in content_json instead of attachments_json."""
    now = datetime.now().replace(hour = 12, minute = 0, second = 0, microsecond = 0)
    image = {"type": "image", "image": "data:image/png;base64,AAAA"}
    audio = {"type": "audio", "audio": "data:audio/wav;base64,BBBB"}
    conn = studio_db.get_connection()
    try:
        conn.execute(
            "INSERT INTO chat_threads (id, title, model_type, model_id, created_at, updated_at) "
            "VALUES ('media', 'media', 'base', 'm', ?, ?)",
            (_ms(now), _ms(now)),
        )
        conn.execute(
            "INSERT INTO chat_messages (id, thread_id, role, content_json, attachments_json, "
            "created_at) VALUES ('media-u0', 'media', 'user', ?, ?, ?)",
            (
                json.dumps([image, audio, image]),
                json.dumps([{"id": "image-upload", "content": [image]}]),
                _ms(now),
            ),
        )
        conn.commit()
    finally:
        conn.close()

    totals = compute_profile_stats(days = 7)["totals"]

    assert totals["attachments"] == 2


def test_usage_csv_export_matches_profile_totals_and_scopes_api_rows(stats_db):
    import csv
    import io

    from storage.profile_stats_db import USAGE_CSV_COLUMNS, export_usage_csv

    day = datetime(2026, 3, 4, 12, 0, tzinfo = timezone.utc)
    conn = studio_db.get_connection()
    try:
        _seed_thread(
            conn,
            "export",
            "unused-thread-model",
            [(day, _metadata(100, 25)), (day + timedelta(hours = 2), _metadata(300, 50))],
        )
        conn.commit()
    finally:
        conn.close()
    assert record_api_usage(_api_receipt("mine", day + timedelta(hours = 1), model = "=HYPERLINK(1)"))
    assert record_api_usage(_api_receipt("theirs", day, subject = "someone-else", total = 999))

    text = export_usage_csv(tz_name = "UTC", subject = "external-client")
    rows = list(csv.reader(io.StringIO(text)))

    assert tuple(rows[0]) == USAGE_CSV_COLUMNS
    assert rows[1:] == [
        [
            "2026-03-04 12:00:10",
            "2026-03-04",
            "chat",
            "unsloth/gpt-oss-20b",
            "100",
            "25",
            "125",
            "5",
        ],
        ["2026-03-04 13:00:00", "2026-03-04", "api", "'=HYPERLINK(1)", "40", "10", "50", "0"],
        [
            "2026-03-04 14:00:10",
            "2026-03-04",
            "chat",
            "unsloth/gpt-oss-20b",
            "300",
            "50",
            "350",
            "5",
        ],
    ]
    totals = compute_profile_stats(days = 7, tz_name = "UTC", subject = "external-client")["totals"]
    assert sum(int(row[6]) for row in rows[1:]) == totals["totalTokens"]
    assert export_usage_csv(tz_name = "UTC", subject = "external-client") == text


def test_usage_csv_export_is_header_only_without_history(stats_db):
    from storage.profile_stats_db import USAGE_CSV_COLUMNS, export_usage_csv
    assert export_usage_csv(subject = "external-client") == ",".join(USAGE_CSV_COLUMNS) + "\n"