mirror of
https://github.com/rennf93/roboco.git
synced 2026-08-03 07:23:24 +02:00
fix(tg): cockpit data correctness — real GLM pricing, display timezone, agent activity tracking (#666)
* fix(tg): cockpit data correctness — real GLM pricing, display timezone, agent activity tracking
Three root causes behind the Mini App/bot showing wrong numbers:
Pricing: glm-5.2 gets a grounded per-token rate (z.ai published pricing,
$1.40/$4.40/$0.26 per 1M, source+date in the table comment) so a GLM
fleet day stops reporting $0.00 for half a million tokens; ungrounded
Ollama-Cloud models render "subscription (untracked)" instead of a bare
zero (is_ollama_cloud_model, consumed directly by the cockpit). Side
effect, intended and documented: honestly-priced GLM now trips the
downgrade-only comparator for new qa/documenter complexity pins.
Display timezone: the cockpit bucketed days in UTC for a GMT+2 operator.
New pure foundation module display_time (resolve_zone/local_date/
trailing_dates/day_bounds_utc, DST-correct with tests for the 23h/25h
days) + ROBOCO_DISPLAY_TIMEZONE (IANA-validated, default UTC); the
cockpit's spend/velocity series bucket raw session/completion rows by
the display zone. The UTC-keyed rollup table and the main dashboard are
deliberately untouched.
Agent activity: AgentTable.status was never set to ACTIVE and
current_task_id was never written anywhere — "active: 0, working: []"
was structurally permanent. Every claim path now marks the claimant
ACTIVE with rollback symmetry (_finalize_claim for dev/PM claims,
_qa_or_doc_claim for QA/doc/PR-gate claims, pr_review_claim for external
review) and every release path clears it (pass/fail QA, pr_pass/pr_fail,
complete_review, advance-to-PM-review, reaper unclaim, voluntary
unclaim, reassign retarget, pool divert, admin transitions, unblock
restore-to-in-progress). The bot's /status shares the cockpit's fleet
derivation so the two surfaces can't disagree. Known ceiling, commented:
one current_task_id column shows a multi-root coordinator PM's most
recent claim only.
Drill: sonnet develop -> sonnet adversarial (refuted the original
chokepoint coverage claim; QA/doc/reviewer paths were unwired) ->
correction round (wired them all + restored a dropped assertion, deleted
a dead helper and the dead subscription_billed field) -> review.
* fix(db): post_update on AgentTable.current_task breaks the flush cycle
agents.current_task_id and tasks.assigned_to reference each other, so a
flush touching both rows — every claim now marks its agent ACTIVE — is
an instance-level circular dependency SQLAlchemy cannot topologically
sort. The e2e smoke's full verb paths (12 tests) hit it; the unit and
integration suites never flush both dirty rows with relationships
loaded. post_update emits the FK as a second UPDATE, the canonical fix
for mutually-referencing rows.
* fix(budgets): enforce only explicitly-set budgets — no per-TaskType defaults
The per-TaskType default cap table blocked an unbudgeted coordination
root one opus planning turn in ($1.50 PLANNING default vs. real
coordination spend) — a false positive by design the moment the fleet
runs a priced model. Budgets are now explicit-input only:
effective_task_budget_usd returns None for an unset budget_usd, the
budget sweep skips enforcement (and never prices spend) on None, and
the unblock re-check passes on None so clearing the budget field is
itself a valid resolution. The project monthly cap stays as the
explicit-input fleet-wide backstop. Panel copy tells the truth
("No cap" placeholder; empty = uncapped), and the TaskType default
table plus its resolver are deleted.
---------
Co-authored-by: Renn F <rennf93@users.noreply.github.com>
This commit is contained in:
@@ -201,6 +201,9 @@ async def test_fail_qa_emits_auditor_alert(
|
||||
"""``fail_qa`` calls the auditor rework producer with QA attribution."""
|
||||
session = MagicMock()
|
||||
session.flush = AsyncMock()
|
||||
# fail_qa releases the QA agent's fleet marker via session.get — default
|
||||
# to "no matching row" for a test that doesn't care about that side effect.
|
||||
session.get = AsyncMock(return_value=None)
|
||||
task = _mock_task(status=TaskStatus.AWAITING_QA)
|
||||
task.orchestration_markers = {"original_developer": str(uuid4())}
|
||||
|
||||
@@ -235,6 +238,9 @@ async def test_pr_fail_emits_auditor_alert(
|
||||
"""``pr_fail`` calls the auditor rework producer with reviewer attribution."""
|
||||
session = MagicMock()
|
||||
session.flush = AsyncMock()
|
||||
# pr_fail releases the reviewer's fleet marker via session.get — default
|
||||
# to "no matching row" for a test that doesn't care about that side effect.
|
||||
session.get = AsyncMock(return_value=None)
|
||||
reviewer_id = uuid4()
|
||||
pm_id = uuid4()
|
||||
task = _mock_task(status=TaskStatus.AWAITING_PR_REVIEW)
|
||||
|
||||
@@ -34,6 +34,11 @@ def _bind(svc: TaskService, name: str, value: object) -> None:
|
||||
def _service() -> TaskService:
|
||||
session = MagicMock()
|
||||
session.flush = AsyncMock()
|
||||
# reassign_active_claim now retargets the agent-side claim marker
|
||||
# (_retarget_agent_claim), which reads agent rows via session.get —
|
||||
# default to "no matching row" so tests that don't care about the
|
||||
# agent side effect stay a no-op there.
|
||||
session.get = AsyncMock(return_value=None)
|
||||
return TaskService(session)
|
||||
|
||||
|
||||
@@ -611,6 +616,9 @@ async def test_unblock_with_restore_emits_audit_event() -> None:
|
||||
``AuditLogTable`` added to the session."""
|
||||
session = MagicMock()
|
||||
session.flush = AsyncMock()
|
||||
# An IN_PROGRESS restore now looks up the restored owner via session.get
|
||||
# to flip its ACTIVE marker — default to "no matching row".
|
||||
session.get = AsyncMock(return_value=None)
|
||||
added: list[object] = []
|
||||
session.add.side_effect = added.append
|
||||
svc = TaskService(session)
|
||||
|
||||
@@ -253,6 +253,12 @@ async def test_pr_review_claim_and_complete(db_session: AsyncSession) -> None:
|
||||
assert claimed.active_claimant_id is not None
|
||||
assert UUID(str(claimed.active_claimant_id)) == reviewer_id
|
||||
assert claimed.claimed_at is not None
|
||||
# The external-PR-review claim chokepoint flips the reviewer's fleet
|
||||
# marker too — otherwise pr_reviewer agents never show as active.
|
||||
reviewer_row = await db_session.get(AgentTable, reviewer_id)
|
||||
assert reviewer_row is not None
|
||||
assert reviewer_row.status == AgentStatus.ACTIVE
|
||||
assert reviewer_row.current_task_id == task_id
|
||||
|
||||
# Re-claiming a non-pending task is a no-op.
|
||||
assert await svc.pr_review_claim(reviewer_id, task_id) is None
|
||||
@@ -269,6 +275,10 @@ async def test_pr_review_claim_and_complete(db_session: AsyncSession) -> None:
|
||||
assert done.claimed_by is None
|
||||
# Single-claimant lock cleared on completion (the review hand-off is done).
|
||||
assert done.active_claimant_id is None
|
||||
# complete_review releases the reviewer's fleet marker too.
|
||||
reviewer_row = await db_session.get(AgentTable, reviewer_id)
|
||||
assert reviewer_row is not None
|
||||
assert reviewer_row.current_task_id is None
|
||||
|
||||
# Re-completing a completed task is a no-op.
|
||||
assert await svc.complete_review(reviewer_id, task_id) is None
|
||||
|
||||
@@ -567,12 +567,15 @@ async def test_wire_sibling_collision_dag_notifies_only_for_new_edges() -> None:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mark_agent_idle_sets_status_idle() -> None:
|
||||
agent = MagicMock(id=uuid4(), status=AgentStatus.ACTIVE)
|
||||
agent = MagicMock(id=uuid4(), status=AgentStatus.ACTIVE, current_task_id=uuid4())
|
||||
result = MagicMock()
|
||||
result.scalar_one_or_none.return_value = agent
|
||||
svc = _service_with(result)
|
||||
await svc.mark_agent_idle(agent.id)
|
||||
assert agent.status == AgentStatus.IDLE
|
||||
# Otherwise the agent keeps reporting its last task as "currently
|
||||
# working" forever — nothing else ever clears this column.
|
||||
assert agent.current_task_id is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -587,6 +590,10 @@ async def test_qa_claim_sets_assignment_on_awaiting_qa() -> None:
|
||||
result.scalar_one_or_none.return_value = task
|
||||
session = MagicMock(flush=AsyncMock())
|
||||
session.execute = AsyncMock(return_value=result)
|
||||
# _qa_or_doc_claim now looks up the claiming agent via session.get to
|
||||
# flip its ACTIVE marker — default to "no matching row" for a test that
|
||||
# doesn't care about that side effect.
|
||||
session.get = AsyncMock(return_value=None)
|
||||
svc = TaskService(session)
|
||||
qa_id = uuid4()
|
||||
out = await svc.qa_claim(qa_id, task.id)
|
||||
@@ -617,6 +624,7 @@ async def test_doc_claim_sets_assignment_on_awaiting_documentation() -> None:
|
||||
result.scalar_one_or_none.return_value = task
|
||||
session = MagicMock(flush=AsyncMock())
|
||||
session.execute = AsyncMock(return_value=result)
|
||||
session.get = AsyncMock(return_value=None)
|
||||
svc = TaskService(session)
|
||||
doc_id = uuid4()
|
||||
out = await svc.doc_claim(doc_id, task.id)
|
||||
@@ -677,7 +685,10 @@ async def test_unblock_with_restore_returns_to_pre_block_state() -> None:
|
||||
blocker_resolver_type=BlockerResolverType.AGENT,
|
||||
blocker_raised_by=pre_assignee,
|
||||
)
|
||||
svc = TaskService(MagicMock(flush=AsyncMock()))
|
||||
# An IN_PROGRESS restore now looks up the restored owner via session.get
|
||||
# to flip its ACTIVE marker — default to "no matching row".
|
||||
session = MagicMock(flush=AsyncMock(), get=AsyncMock(return_value=None))
|
||||
svc = TaskService(session)
|
||||
_bind(svc, "get", AsyncMock(return_value=task))
|
||||
out = await svc.unblock_with_restore(uuid4(), task.id, restore=True)
|
||||
assert out is task
|
||||
@@ -721,13 +732,17 @@ async def test_unblock_no_branch_returns_to_pending() -> None:
|
||||
task = _build_task(
|
||||
status=TaskStatus.BLOCKED, branch_name=None, blocker_raised_by=raiser
|
||||
)
|
||||
svc = TaskService(MagicMock(flush=AsyncMock()))
|
||||
session = MagicMock(flush=AsyncMock())
|
||||
svc = TaskService(session)
|
||||
_bind(svc, "get", AsyncMock(return_value=task))
|
||||
_bind(svc, "_index_lifecycle_event_background", AsyncMock())
|
||||
out = await svc.unblock(task.id)
|
||||
assert out is task
|
||||
assert task.status == TaskStatus.PENDING
|
||||
assert task.assigned_to == raiser
|
||||
# A PENDING restore is NOT a resume — the owner isn't marked active here;
|
||||
# a fresh claim() is what actually resumes it, so no agent lookup runs.
|
||||
session.get.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -747,7 +762,10 @@ async def test_admin_set_status_out_of_blocked_restores_pre_block_owner() -> Non
|
||||
pre_block_state="in_progress",
|
||||
pre_block_assignee=dev,
|
||||
)
|
||||
svc = TaskService(MagicMock(flush=AsyncMock()))
|
||||
# An IN_PROGRESS restore now looks up the restored owner (dev) via
|
||||
# session.get to flip its ACTIVE marker.
|
||||
session = MagicMock(flush=AsyncMock(), get=AsyncMock(return_value=None))
|
||||
svc = TaskService(session)
|
||||
_bind(svc, "get", AsyncMock(return_value=task))
|
||||
out = await svc.admin_set_status(task.id, TaskStatus.IN_PROGRESS)
|
||||
assert out is task
|
||||
@@ -791,7 +809,10 @@ async def test_admin_set_status_into_review_queue_clears_active_claimant() -> No
|
||||
claimed_by=dev,
|
||||
active_claimant_id=dev,
|
||||
)
|
||||
svc = TaskService(MagicMock(flush=AsyncMock()))
|
||||
# Clearing the stale claimant now looks it up via session.get to release
|
||||
# its ACTIVE marker too.
|
||||
session = MagicMock(flush=AsyncMock(), get=AsyncMock(return_value=None))
|
||||
svc = TaskService(session)
|
||||
_bind(svc, "get", AsyncMock(return_value=task))
|
||||
out = await svc.admin_set_status(task.id, TaskStatus.AWAITING_QA)
|
||||
assert out is task
|
||||
@@ -943,7 +964,10 @@ async def test_admin_set_status_blocked_to_review_state_clears_claim() -> None:
|
||||
pre_block_state="awaiting_pm_review",
|
||||
pre_block_assignee=pm,
|
||||
)
|
||||
svc = TaskService(MagicMock(flush=AsyncMock()))
|
||||
# Clearing the stale claim now looks it up via session.get to release
|
||||
# its ACTIVE marker too.
|
||||
session = MagicMock(flush=AsyncMock(), get=AsyncMock(return_value=None))
|
||||
svc = TaskService(session)
|
||||
_bind(svc, "get", AsyncMock(return_value=task))
|
||||
out = await svc.admin_set_status(task.id, TaskStatus.AWAITING_PM_REVIEW)
|
||||
assert out is task
|
||||
@@ -970,7 +994,10 @@ async def test_admin_set_status_blocked_to_needs_revision_clears_claim() -> None
|
||||
pre_block_state="awaiting_pm_review",
|
||||
pre_block_assignee=pm,
|
||||
)
|
||||
svc = TaskService(MagicMock(flush=AsyncMock()))
|
||||
# Clearing the stale claim now looks it up via session.get to release
|
||||
# its ACTIVE marker too.
|
||||
session = MagicMock(flush=AsyncMock(), get=AsyncMock(return_value=None))
|
||||
svc = TaskService(session)
|
||||
_bind(svc, "get", AsyncMock(return_value=task))
|
||||
out = await svc.admin_set_status(task.id, TaskStatus.NEEDS_REVISION)
|
||||
assert out is task
|
||||
@@ -1085,7 +1112,10 @@ async def test_admin_set_status_pre_block_restore_syncs_active_claimant() -> Non
|
||||
pre_block_state="in_progress",
|
||||
pre_block_assignee=dev,
|
||||
)
|
||||
svc = TaskService(MagicMock(flush=AsyncMock()))
|
||||
# An IN_PROGRESS restore now looks up the restored owner (dev) via
|
||||
# session.get to flip its ACTIVE marker.
|
||||
session = MagicMock(flush=AsyncMock(), get=AsyncMock(return_value=None))
|
||||
svc = TaskService(session)
|
||||
_bind(svc, "get", AsyncMock(return_value=task))
|
||||
out = await svc.admin_set_status(task.id, TaskStatus.IN_PROGRESS)
|
||||
assert out is task
|
||||
@@ -1566,7 +1596,10 @@ async def test_unblock_with_branch_resumes_in_progress() -> None:
|
||||
branch_name="feature/backend/abc12345",
|
||||
blocker_raised_by=uuid4(),
|
||||
)
|
||||
svc = TaskService(MagicMock(flush=AsyncMock()))
|
||||
# Resuming IN_PROGRESS now looks up the restored owner via session.get
|
||||
# to flip its ACTIVE marker.
|
||||
session = MagicMock(flush=AsyncMock(), get=AsyncMock(return_value=None))
|
||||
svc = TaskService(session)
|
||||
_bind(svc, "get", AsyncMock(return_value=task))
|
||||
_bind(svc, "_index_lifecycle_event_background", AsyncMock())
|
||||
out = await svc.unblock(task.id)
|
||||
@@ -1809,6 +1842,56 @@ async def test_finalize_claim_rollback_emits_reversal_audit() -> None:
|
||||
assert {"from": "claimed", "to": "pending"} in audit_calls
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_finalize_claim_sets_agent_active_then_rolls_back_on_failure() -> None:
|
||||
"""_finalize_claim must flip agent.status/current_task_id to ACTIVE/this
|
||||
task BEFORE the branch step runs (previously nothing ever wrote these
|
||||
fields at all — the fleet-status bug), and roll them back to their
|
||||
pre-claim values on a branch-creation failure, same as the task fields.
|
||||
"""
|
||||
session = MagicMock()
|
||||
session.flush = AsyncMock()
|
||||
svc = TaskService(session)
|
||||
|
||||
task = _build_task(
|
||||
status=TaskStatus.PENDING,
|
||||
branch_name=None,
|
||||
project_id=uuid4(),
|
||||
product_id=None,
|
||||
batch_id=None,
|
||||
parent_task_id=None,
|
||||
cell_projects=[],
|
||||
pr_created=False,
|
||||
pr_number=None,
|
||||
)
|
||||
agent = MagicMock(
|
||||
id=uuid4(),
|
||||
role=AgentRole.DEVELOPER,
|
||||
status=AgentStatus.IDLE,
|
||||
current_task_id=None,
|
||||
)
|
||||
_bind(svc, "_emit_status_transition_audit", MagicMock())
|
||||
|
||||
captured: dict[str, object] = {}
|
||||
|
||||
async def _boom(_task: object, _agent_id: object) -> str:
|
||||
captured["status"] = agent.status
|
||||
captured["current_task_id"] = agent.current_task_id
|
||||
raise RuntimeError("branch boom")
|
||||
|
||||
_bind(svc, "_ensure_branch_for_task", _boom)
|
||||
|
||||
with pytest.raises(RuntimeError, match="branch boom"):
|
||||
await svc._finalize_claim(task, agent, agent.id)
|
||||
|
||||
# Set to ACTIVE/this-task before the branch step ran...
|
||||
assert captured["status"] == AgentStatus.ACTIVE
|
||||
assert captured["current_task_id"] == task.id
|
||||
# ...and rolled back to the pre-claim values once branch creation failed.
|
||||
assert agent.status == AgentStatus.IDLE
|
||||
assert agent.current_task_id is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_emit_status_transition_audit_writes_in_session_atomically() -> None:
|
||||
"""The status-transition audit row is written into the CALLER's session (same
|
||||
|
||||
@@ -30,6 +30,11 @@ def _bind(svc: TaskService, name: str, value: object) -> None:
|
||||
def _service() -> TaskService:
|
||||
session = MagicMock()
|
||||
session.flush = AsyncMock()
|
||||
# reassign_active_claim now retargets the agent-side claim marker
|
||||
# (_retarget_agent_claim), which reads agent rows via session.get —
|
||||
# default to "no matching row" so tests that don't care about the
|
||||
# agent side effect stay a no-op there.
|
||||
session.get = AsyncMock(return_value=None)
|
||||
return TaskService(session)
|
||||
|
||||
|
||||
|
||||
@@ -96,6 +96,33 @@ async def test_run_cycle_syncs_commands_exactly_once(
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_render_status_shares_fleet_derivation_with_render_agents(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""The /status fleet line must come from the SAME by_status breakdown
|
||||
/agents and the Today brief use (TgCockpitService.fleet) — a second,
|
||||
independent agent-status query previously could (and did) disagree."""
|
||||
fleet: dict[str, Any] = {
|
||||
"total": 27,
|
||||
"by_status": {"active": 3, "idle": 20, "offline": 4},
|
||||
"working": [],
|
||||
}
|
||||
cockpit = MagicMock(fleet=AsyncMock(return_value=fleet))
|
||||
monkeypatch.setattr(ti, "get_tg_cockpit_service", lambda _s: cockpit)
|
||||
tasks = MagicMock(
|
||||
count_by_status=AsyncMock(return_value={"in_progress": 5, "pending": 2})
|
||||
)
|
||||
monkeypatch.setattr(ti, "get_task_service", lambda _s: tasks)
|
||||
|
||||
text = await _engine()._render_status()
|
||||
|
||||
assert "3</b> active" in text
|
||||
assert "20</b> idle" in text
|
||||
assert "4</b> offline" in text
|
||||
assert "in_progress" in text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_render_agents_lists_working_agents(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
@@ -124,12 +151,16 @@ async def test_render_agents_lists_working_agents(
|
||||
async def test_render_usage_formats_today_summary(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
usage = MagicMock(
|
||||
get_today_summary=AsyncMock(
|
||||
return_value={"tokens_today": 2_400_000, "cost_today_usd": 18.7}
|
||||
cockpit = MagicMock(
|
||||
today_spend=AsyncMock(
|
||||
return_value={
|
||||
"tokens_today": 2_400_000,
|
||||
"cost_today_usd": 18.7,
|
||||
"subscription_billed": False,
|
||||
}
|
||||
)
|
||||
)
|
||||
monkeypatch.setattr(ti, "get_usage_service", lambda _s: usage)
|
||||
monkeypatch.setattr(ti, "get_tg_cockpit_service", lambda _s: cockpit)
|
||||
|
||||
text = await _engine()._render_usage()
|
||||
|
||||
@@ -137,6 +168,30 @@ async def test_render_usage_formats_today_summary(
|
||||
assert "2,400,000 tokens" in text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_render_usage_labels_subscription_billed_spend(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""An untracked-subscription spend day (Ollama Cloud, no grounded rate)
|
||||
must never render as a bare, misleading '$0.00'."""
|
||||
cockpit = MagicMock(
|
||||
today_spend=AsyncMock(
|
||||
return_value={
|
||||
"tokens_today": 456_221,
|
||||
"cost_today_usd": 0.0,
|
||||
"subscription_billed": True,
|
||||
}
|
||||
)
|
||||
)
|
||||
monkeypatch.setattr(ti, "get_tg_cockpit_service", lambda _s: cockpit)
|
||||
|
||||
text = await _engine()._render_usage()
|
||||
|
||||
assert "subscription (untracked)" in text
|
||||
assert "456,221 tokens" in text
|
||||
assert "$0.00" not in text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_render_blocked_sections_and_links(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
|
||||
@@ -9,12 +9,13 @@ assumed.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, date, datetime, timedelta
|
||||
from typing import TYPE_CHECKING, cast
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
from roboco.config import settings
|
||||
from roboco.db.tables import AgentTable, TaskTable
|
||||
from roboco.db.tables import AgentSpawnSessionTable, AgentTable, TaskTable
|
||||
from roboco.foundation import identity as _foundation
|
||||
from roboco.foundation.policy.content import markers
|
||||
from roboco.models.base import (
|
||||
@@ -43,6 +44,28 @@ SYSTEM_UUID = _foundation.AGENTS["system"].uuid
|
||||
|
||||
CI_WATCH_SOURCE = "ci_watch"
|
||||
|
||||
_COST_TOL = 0.005
|
||||
_TOKENS_FLOOR = 1
|
||||
|
||||
|
||||
def _spawn_session(
|
||||
*, started_at: datetime, model: str, cost: float, tokens_input: int = 1000
|
||||
) -> AgentSpawnSessionTable:
|
||||
return AgentSpawnSessionTable(
|
||||
id=uuid4(),
|
||||
agent_slug=f"be-dev-{uuid4().hex[:6]}",
|
||||
team="backend",
|
||||
role="developer",
|
||||
model=model,
|
||||
task_id=None,
|
||||
started_at=started_at,
|
||||
ended_at=started_at + timedelta(minutes=5),
|
||||
tokens_input=tokens_input,
|
||||
tokens_output=0,
|
||||
estimated_cost_usd=cost,
|
||||
)
|
||||
|
||||
|
||||
# 1 awaiting + 1 blocked + 4 held drafts (release/x/video/roadmap-item).
|
||||
EXPECTED_NEEDS_YOU_TOTAL = 6
|
||||
|
||||
@@ -195,3 +218,110 @@ async def test_today_composes_needs_you_fleet_and_ship(
|
||||
|
||||
assert brief["ship"]["open_release_proposal"] is True
|
||||
assert brief["ship"]["ci_fix_tasks"] == baseline["ship"]["ci_fix_tasks"] + 1
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Display-timezone bucketing (Issue 2) — "today" is display_timezone-aware,
|
||||
# not always the server's UTC day. `_session_metrics_by_day` is called
|
||||
# directly with an explicit historical `days` window so the test is fully
|
||||
# deterministic (disconnected from the real "now").
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_session_metrics_by_day_buckets_by_display_timezone(
|
||||
db_session: AsyncSession, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""23:30 UTC on the 15th is already 00:30 on the 16th in Europe/Berlin
|
||||
(winter, CET = UTC+1) — the exact 'evening activity lands on the wrong
|
||||
display day' bug this fix targets."""
|
||||
started = datetime(2026, 1, 15, 23, 30, tzinfo=UTC)
|
||||
session_row = _spawn_session(started_at=started, model="claude-sonnet-5", cost=1.23)
|
||||
db_session.add(session_row)
|
||||
await db_session.flush()
|
||||
|
||||
svc = get_tg_cockpit_service(db_session)
|
||||
|
||||
monkeypatch.setattr(settings, "display_timezone", "UTC")
|
||||
cost_utc, tokens_utc, _ = await svc._session_metrics_by_day([date(2026, 1, 15)])
|
||||
assert cost_utc.get(date(2026, 1, 15), 0.0) >= _COST_TOL
|
||||
assert tokens_utc.get(date(2026, 1, 15), 0) >= _TOKENS_FLOOR
|
||||
|
||||
monkeypatch.setattr(settings, "display_timezone", "Europe/Berlin")
|
||||
cost_berlin, tokens_berlin, _ = await svc._session_metrics_by_day(
|
||||
[date(2026, 1, 16)]
|
||||
)
|
||||
assert cost_berlin.get(date(2026, 1, 16), 0.0) >= _COST_TOL
|
||||
assert tokens_berlin.get(date(2026, 1, 16), 0) >= _TOKENS_FLOOR
|
||||
# And the SAME row must NOT double-count into the UTC calendar day under
|
||||
# the Berlin bucketing — the 15th should now come up empty for this row.
|
||||
cost_berlin_15, _, _ = await svc._session_metrics_by_day([date(2026, 1, 15)])
|
||||
assert cost_berlin_15.get(date(2026, 1, 15), 0.0) < _COST_TOL
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_window_dates_shifts_with_display_timezone(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""`_window_dates` (real 'now') must reflect the configured display
|
||||
timezone, not always UTC."""
|
||||
svc = get_tg_cockpit_service(cast("AsyncSession", None))
|
||||
monkeypatch.setattr(settings, "display_timezone", "UTC")
|
||||
utc_dates = svc._window_dates()
|
||||
monkeypatch.setattr(settings, "display_timezone", "Pacific/Kiritimati")
|
||||
# UTC+14 — the furthest-ahead real timezone; "today" there is never
|
||||
# earlier, and is later whenever UTC hasn't crossed its own midnight yet.
|
||||
kiritimati_dates = svc._window_dates()
|
||||
assert kiritimati_dates[-1] >= utc_dates[-1]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Ollama Cloud honesty-labeling (Issue 1) — an ungrounded ':cloud' model's $0
|
||||
# is flagged subscription_billed, never rendered as a bare misleading "$0".
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_today_spend_flags_ungrounded_ollama_cloud_as_subscription_billed(
|
||||
db_session: AsyncSession,
|
||||
) -> None:
|
||||
now = datetime.now(UTC)
|
||||
db_session.add(
|
||||
_spawn_session(started_at=now, model="some-future-model:cloud", cost=0.0)
|
||||
)
|
||||
await db_session.flush()
|
||||
|
||||
summary = await get_tg_cockpit_service(db_session).today_spend()
|
||||
|
||||
assert summary["cost_today_usd"] == pytest.approx(0.0)
|
||||
assert summary["subscription_billed"] is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_today_spend_not_subscription_billed_when_priced(
|
||||
db_session: AsyncSession,
|
||||
) -> None:
|
||||
"""A real per-token cost (even from a priced Ollama Cloud model like
|
||||
GLM-5.2) is never mislabeled as an untracked subscription figure."""
|
||||
now = datetime.now(UTC)
|
||||
db_session.add(_spawn_session(started_at=now, model="glm-5.2:cloud", cost=2.5))
|
||||
await db_session.flush()
|
||||
|
||||
summary = await get_tg_cockpit_service(db_session).today_spend()
|
||||
|
||||
assert summary["subscription_billed"] is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_today_spend_not_subscription_billed_for_local_ollama(
|
||||
db_session: AsyncSession,
|
||||
) -> None:
|
||||
"""A genuinely-free self-hosted model (no ':cloud' tag) at $0 is just
|
||||
free, not an untracked subscription."""
|
||||
now = datetime.now(UTC)
|
||||
db_session.add(_spawn_session(started_at=now, model="ollama/llama3", cost=0.0))
|
||||
await db_session.flush()
|
||||
|
||||
summary = await get_tg_cockpit_service(db_session).today_spend()
|
||||
|
||||
assert summary["subscription_billed"] is False
|
||||
|
||||
Reference in New Issue
Block a user