Files
roboco/tests/integration/test_metrics_task.py
T
cea3e56628 feat(lifecycle): revision findings ledger — structured failure feedback, persisted and delivered down the chain (#486)
* feat(lifecycle): revision findings ledger — structured QA/PR/PM/CEO failure feedback, persisted and delivered down the chain

Every bounce used to survive only as flattened prose: rounds overwrote each
other in notes_structured, request_changes persisted nothing, two raw
dev_notes appends were silently destroyed by the next handoff note, and the
dev prompt pointed at fields (qa_notes via evidence(), pm_notes) the API
never delivered. Agents re-interpreted and re-discovered every failure
before they could start fixing it.

- task_review_findings (migration 071, append-only): file/line/severity/
  criterion(AC-id-validated)/expected/actual/fix/evidence per finding, with
  origin (qa|pr_gate|pm|ceo), round, and an open->addressed->verified
  lifecycle (waived reserved); new tasks.pm_notes + PmReviewContent give
  request_changes a structured home
- producers: fail_review/pr_fail/request_changes take findings=[...] (prose
  issues shimmed+merged for one release, deprecation-logged); ceo_reject
  validates its reason (no 500), lands an origin=ceo finding, and bumps
  round+audit on branchless coordination roots; guardrails at the verb
  chokepoint (nudge >5, hard reject >10, field caps, traversal-safe file);
  the dev_notes data-loss appends are removed; new task.request_changes +
  task.ceo_reject audit events close rework attribution
- delivery: qa_notes/pr_reviewer_notes/pm_notes carry the deterministic
  [F-id8] rendering; claim briefings, evidence(), the REVISION_REQUIRED
  spawn prompt, PM triage bounced-blocks, and A2A bodies deliver open
  findings; round-N+1 QA and gate reviewers get the full prior ledger;
  panel Findings tab + bounced-xN chip; metrics pm_rejects/ceo_rejects +
  findings counts; vault task notes render a Findings section (fail-open)
- resolution closes for every origin: i_am_done and submit_up/submit_root
  take resolved_findings gated by FINDINGS_ADDRESSED (owner-gated so a
  stale non-owner PM can never mutate the ledger); pass_review/pr_pass/
  complete verify-stamp same-transaction; ceo_approve stamps best-effort
- 24 real-DB integration tests drive the full loop through the real
  choreographer; full suite 12856 green

* docs: revision findings ledger sweep — CLAUDE.md, map, RAG corpus

- CLAUDE.md: new ledger section + corrected request_changes row
- docs/map/review-findings.md (new subsystem map) + surgical updates to
  task-service/pr-gate-review/metrics-observability/vault/panel maps
- docs/rag: producers' findings contract across qa/pr-reviewer/developer/
  cell-pm/main-pm/ceo role docs (the PM docs were missing request_changes
  entirely), verb references, and a new architecture/review-findings.md
  disambiguating ledger findings from convention findings

* test(e2e): resubmit resolves the pr_fail finding per the ledger contract

The scripted pr_fail revision loop resubmitted submit_up without
resolved_findings — correctly rejected now that FINDINGS_ADDRESSED gates
the PM resubmit verbs (green locally, red only in CI since the e2e suite
skips without ROBOCO_E2E_SMOKE=1). The scripted PM now reads the open
ledger row pr_fail persisted (new open_finding_ids arc helper) and
resolves it on resubmit, asserting the open set drains — exercising the
coordinator half of the new contract end to end.

---------

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
2026-07-11 22:54:42 +02:00

318 lines
9.2 KiB
Python

"""get_task_metrics — granular per-task effort against a real Postgres.
Seeds a task's audit-log journey + agent spawn stints (with turns/tool_calls/
tokens/cost) + named qa/pr fail events, then asserts the composed metrics:
summed effort vs wall-clock, turns/tool_calls/tokens/cost, per-stage
active-vs-wait, and who-caused-rework.
"""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from typing import TYPE_CHECKING, Any, NamedTuple
from uuid import uuid4
import pytest
import pytest_asyncio
from roboco.db.tables import (
AgentSpawnSessionTable,
AgentTable,
AuditLogTable,
ProjectTable,
TaskReviewFindingTable,
TaskTable,
)
from roboco.models.base import (
AgentRole,
AgentStatus,
Complexity,
TaskNature,
TaskStatus,
TaskType,
Team,
)
from roboco.services.metrics import MetricsService
if TYPE_CHECKING:
from collections.abc import AsyncIterator
from sqlalchemy.ext.asyncio import AsyncSession
_T0 = datetime(2026, 6, 20, 12, 0, 0, tzinfo=UTC)
def _sec(n: int) -> datetime:
return _T0 + timedelta(seconds=n)
def _agent(role: AgentRole, slug: str) -> AgentTable:
return AgentTable(
id=uuid4(),
name=slug,
slug=slug,
role=role,
team=Team.BACKEND,
status=AgentStatus.ACTIVE,
model_config={},
system_prompt="x",
capabilities=[],
permissions={},
metrics={},
)
def _audit(
task_id: Any,
status: str,
ts: datetime,
*,
agent_id: Any = None,
event_type: str | None = None,
) -> AuditLogTable:
return AuditLogTable(
id=uuid4(),
event_type=event_type or f"task.{status}",
agent_id=agent_id,
target_type="task",
target_id=task_id,
severity="info",
details={"to_status": status, "from_status": "prev", "team": "backend"},
timestamp=ts,
)
class _Usage(NamedTuple):
turns: int
tool_calls: int
tokens_in: int
tokens_out: int
cost: float
def _spawn(
task_id: str,
started: datetime,
ended: datetime | None,
usage: _Usage,
) -> AgentSpawnSessionTable:
return AgentSpawnSessionTable(
id=uuid4(),
agent_slug="be-dev-1",
team="backend",
role="developer",
model="claude",
task_id=task_id,
started_at=started,
ended_at=ended,
turns=usage.turns,
tool_calls=usage.tool_calls,
tokens_input=usage.tokens_in,
tokens_output=usage.tokens_out,
estimated_cost_usd=usage.cost,
)
@pytest_asyncio.fixture
async def setup(db_session: AsyncSession) -> AsyncIterator[dict]:
dev = _agent(AgentRole.DEVELOPER, f"be-dev-{uuid4().hex[:6]}")
qa = _agent(AgentRole.QA, f"be-qa-{uuid4().hex[:6]}")
db_session.add_all([dev, qa])
await db_session.flush()
project = ProjectTable(
id=uuid4(),
name="P",
slug=f"p-{uuid4().hex[:6]}",
git_url="https://example.com/r.git",
assigned_cell=Team.BACKEND,
created_by=dev.id,
)
db_session.add(project)
await db_session.flush()
yield {
"svc": MetricsService(db_session),
"db": db_session,
"project_id": project.id,
"dev_id": dev.id,
"qa_id": qa.id,
}
@pytest.mark.asyncio
async def test_returns_none_for_missing_task(setup: dict) -> None:
assert await setup["svc"].get_task_metrics(uuid4()) is None
@pytest.mark.asyncio
async def test_composes_effort_turns_stages_and_rework(setup: dict) -> None:
db = setup["db"]
tid = uuid4()
db.add(
TaskTable(
id=tid,
title="t",
description="d",
acceptance_criteria=["ac"],
task_type=TaskType.CODE,
nature=TaskNature.TECHNICAL,
status=TaskStatus.COMPLETED,
team=Team.BACKEND,
project_id=setup["project_id"],
created_by=setup["dev_id"],
assigned_to=setup["dev_id"],
revision_count=2,
estimated_complexity=Complexity.MEDIUM,
started_at=_T0,
completed_at=_sec(7200),
)
)
db.add_all(
[
_audit(tid, "claimed", _T0),
_audit(tid, "in_progress", _sec(60)),
_audit(tid, "awaiting_qa", _sec(3660)),
_audit(tid, "completed", _sec(7200)),
_audit(tid, "needs_revision", _sec(3660), event_type="task.qa_fail"),
_audit(tid, "needs_revision", _sec(3000), event_type="task.pr_fail"),
]
)
db.add_all(
[
_spawn(str(tid), _T0, _sec(600), _Usage(5, 10, 100, 50, 1.0)),
_spawn(str(tid), _sec(3600), _sec(3660), _Usage(3, 4, 20, 10, 0.5)),
]
)
await db.flush()
m = await setup["svc"].get_task_metrics(tid)
assert m is not None
# summed effort (600 + 60) vs wall-clock (2h).
expected_active_s = 660
expected_wall_s = 7200
assert m.active_runtime_seconds == expected_active_s
assert m.wall_clock_seconds == expected_wall_s
assert (m.turns, m.tool_calls, m.tokens) == (8, 14, 180)
assert m.cost_usd == pytest.approx(1.5)
assert (m.revision_count, m.qa_fails, m.pr_fails, m.stints) == (2, 1, 1, 2)
stages = {s.status: s for s in m.stages}
# claimed [0,60): stint1 covers it fully.
assert (stages["claimed"].active_seconds, stages["claimed"].wait_seconds) == (60, 0)
# in_progress [60,3660): stint1 60..600 (540) + stint2 3600..3660 (60) = 600 active.
assert (
stages["in_progress"].active_seconds,
stages["in_progress"].wait_seconds,
) == (600, 3000)
# awaiting_qa [3660,7200): no stint running -> all wait.
assert (
stages["awaiting_qa"].active_seconds,
stages["awaiting_qa"].wait_seconds,
) == (0, 3540)
@pytest.mark.asyncio
async def test_in_flight_open_stint_and_open_window_decompose(setup: dict) -> None:
db = setup["db"]
tid = uuid4()
db.add(
TaskTable(
id=tid,
title="t",
description="d",
acceptance_criteria=["ac"],
task_type=TaskType.CODE,
nature=TaskNature.TECHNICAL,
status=TaskStatus.IN_PROGRESS,
team=Team.BACKEND,
project_id=setup["project_id"],
created_by=setup["dev_id"],
assigned_to=setup["dev_id"],
estimated_complexity=Complexity.MEDIUM,
started_at=_T0,
completed_at=None,
)
)
db.add_all([_audit(tid, "claimed", _T0), _audit(tid, "in_progress", _sec(60))])
# An OPEN stint (ended_at=None) -> runs to now.
db.add(_spawn(str(tid), _T0, None, _Usage(2, 3, 1, 1, 0.1)))
await db.flush()
m = await setup["svc"].get_task_metrics(tid)
assert m is not None
assert m.stints == 1
assert m.active_runtime_seconds > 0 # open stint ran to now
assert m.wall_clock_seconds > 0 # open task -> now
# The open final window (in_progress) still decomposes.
assert "in_progress" in {s.status for s in m.stages}
@pytest.mark.asyncio
async def test_task_metrics_includes_pm_ceo_rejects_and_findings_counts(
setup: dict,
) -> None:
"""pm_rejects/ceo_rejects mirror qa_fails/pr_fails for the other two named
bounce events; findings_open/findings_total read the revision-findings
ledger (open vs total rows) for this task."""
db = setup["db"]
tid = uuid4()
db.add(
TaskTable(
id=tid,
title="t",
description="d",
acceptance_criteria=["ac"],
task_type=TaskType.CODE,
nature=TaskNature.TECHNICAL,
status=TaskStatus.NEEDS_REVISION,
team=Team.BACKEND,
project_id=setup["project_id"],
created_by=setup["dev_id"],
assigned_to=setup["dev_id"],
revision_count=2,
estimated_complexity=Complexity.MEDIUM,
started_at=_T0,
)
)
db.add_all(
[
_audit(tid, "claimed", _T0),
_audit(tid, "needs_revision", _sec(60), event_type="task.request_changes"),
_audit(tid, "needs_revision", _sec(120), event_type="task.ceo_reject"),
]
)
# The ledger's task_id carries a real FK (unlike audit_log.target_id /
# spawn_session.task_id, both plain columns) — the referenced task must
# be flushed first.
await db.flush()
db.add_all(
[
TaskReviewFindingTable(
id=uuid4(),
task_id=tid,
origin="qa",
round=1,
author_slug="be-qa",
severity="major",
expected="x",
actual="y",
status="verified",
),
TaskReviewFindingTable(
id=uuid4(),
task_id=tid,
origin="pm",
round=2,
author_slug="be-pm",
severity="blocker",
expected="x",
actual="y",
status="open",
),
]
)
await db.flush()
m = await setup["svc"].get_task_metrics(tid)
assert m is not None
assert (m.pm_rejects, m.ceo_rejects) == (1, 1)
assert (m.findings_open, m.findings_total) == (1, 2)