Introduces end-to-end token consumption tracking so agent work is visible as a cost/effort metric alongside tasks and workplans. - Migration o2j3k4l5m6n7: token_events table with FK indexes on task_id, workstream_id, repo_id, created_at - ORM model, Pydantic schemas (TokenEventCreate, TokenEventRead with computed tokens_total, TokenSummary) - Router: POST /token-events/, GET /token-events/ (7 filters), GET /token-events/summary/ (task|workstream|repo|commit|release scope) - MCP tools: record_token_event, get_token_summary (formatted table) - update_task_status enriched with optional tokens_in/tokens_out passthrough — one call creates status update + token event - Dashboard token-cost.md page: by-repo bar, by-workplan table, by-model bar, top-10 tasks by tokens - ralph-workplan skill updated with token reporting guidance and per-task heuristics for estimating counts - Tests: test_token_events.py + test_token_passthrough.py (182 pass) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
77 lines
2.5 KiB
Python
77 lines
2.5 KiB
Python
import uuid
|
|
from datetime import date, datetime
|
|
from typing import Self
|
|
|
|
from pydantic import BaseModel, ConfigDict, model_validator
|
|
|
|
from api.models.task import TaskPriority, TaskStatus
|
|
|
|
|
|
class TaskCreate(BaseModel):
|
|
workstream_id: uuid.UUID
|
|
title: str
|
|
description: str | None = None
|
|
status: TaskStatus = TaskStatus.todo
|
|
priority: TaskPriority = TaskPriority.medium
|
|
assignee: str | None = None
|
|
due_date: date | None = None
|
|
blocking_reason: str | None = None
|
|
needs_human: bool = False
|
|
intervention_note: str | None = None
|
|
parent_task_id: uuid.UUID | None = None
|
|
|
|
@model_validator(mode="after")
|
|
def intervention_note_required_when_flagged(self) -> Self:
|
|
if self.needs_human and not self.intervention_note:
|
|
raise ValueError("intervention_note is required when needs_human is True")
|
|
return self
|
|
|
|
|
|
class TaskUpdate(BaseModel):
|
|
title: str | None = None
|
|
description: str | None = None
|
|
status: TaskStatus | None = None
|
|
priority: TaskPriority | None = None
|
|
assignee: str | None = None
|
|
due_date: date | None = None
|
|
blocking_reason: str | None = None
|
|
needs_human: bool | None = None
|
|
intervention_note: str | None = None
|
|
parent_task_id: uuid.UUID | None = None
|
|
# Optional token passthrough — when provided, a token_event is created
|
|
tokens_in: int | None = None
|
|
tokens_out: int | None = None
|
|
model: str | None = None
|
|
agent: str | None = None
|
|
session_id: str | None = None
|
|
|
|
@model_validator(mode="after")
|
|
def blocking_reason_required_when_blocked(self) -> Self:
|
|
if self.status == TaskStatus.blocked and not self.blocking_reason:
|
|
raise ValueError("blocking_reason is required when status is blocked")
|
|
return self
|
|
|
|
@model_validator(mode="after")
|
|
def intervention_note_required_when_flagged(self) -> Self:
|
|
if self.needs_human and not self.intervention_note:
|
|
raise ValueError("intervention_note is required when needs_human is True")
|
|
return self
|
|
|
|
|
|
class TaskRead(BaseModel):
|
|
model_config = ConfigDict(from_attributes=True)
|
|
id: uuid.UUID
|
|
workstream_id: uuid.UUID
|
|
title: str
|
|
description: str | None = None
|
|
status: TaskStatus
|
|
priority: TaskPriority
|
|
assignee: str | None = None
|
|
due_date: date | None = None
|
|
blocking_reason: str | None = None
|
|
needs_human: bool
|
|
intervention_note: str | None = None
|
|
parent_task_id: uuid.UUID | None = None
|
|
created_at: datetime
|
|
updated_at: datetime
|