Files
roboco/tests/unit/gateway/test_collision_context.py
T
bb3b4b0c6d W6: Telegram notifications bridge (V1) (#524)
* feat(gateway): reviewer/PM collision map (W5)

The collision surface (intends_to_touch / adds_migration / touches_shared)
is authored at delegate time, consumed once by SequencingService to wire
dependency edges, then never shown to a reviewer again. This surfaces it:

- Pure builder (services/gateway/choreographer/collision.py): for a task
  under review, the surfaced siblings (same parent) that would collide —
  file-overlap globs or a shared migration chain (both adds_migration) —
  with the overlapping globs and a declared-vs-actual drift check. No
  DB/IO; callers fetch siblings (one indexed get_subtasks query, mig 069)
  + actual files (git). Caps: 10 siblings, 5 globs.

- Evidence envelopes: collision_context block injected into QA
  claim_review, PR-gate claim_gate_review (both carry real touched files
  so drift is populated), and the PM i_will_plan briefing (no actual
  files at plan time, drift omitted). Best-effort — a failure omits the
  block, never breaks the verb/briefing. Empty block omitted (zero token
  cost via _EVIDENCE_OMIT_WHEN_EMPTY).

- Panel: GET /api/tasks/{id}/collision-map (declared surface + sibling
  overlap; no drift — the panel route resolves no workspace) + a Collision
  tab on the task detail (8th tab). Mock-mode returns an empty map.

- docs/map added to the RAG auto-index dirs so the collision-map concept
  is fleet-retrievable; skipped gracefully if the dir is absent.

19 new tests (15 unit on the pure builder + 4 integration on the route).
Gate green: ruff/mypy/xenon (module rank A)/pytest 13000/coverage 94.81%,
panel typecheck/lint/516 tests.

* [w6-telegram] Add Telegram notifications bridge (V1)

CEO-facing Telegram DM bridge, flag-gated off by default
(ROBOCO_TELEGRAM_ENABLED). Mirrors the X-credentials / X-client pattern:

- TelegramCredentialsTable (migration 073) — singleton Fernet-encrypted
  bot_token + chat_id, all-or-nothing set/clear; API never returns plaintext.
- TelegramClient ABC / NullTelegramClient (no-op, configured->False, never
  raises) / LiveTelegramClient (httpx POST sendMessage) / build_telegram_client
  factory (Null when creds unset).
- /telegram/credentials CEO-only routes (write-only, guard-decorated).
- Best-effort _notify_telegram fan-out from the two CEO-notify producers
  (notify_ceo_of_escalation, notify_ceo_of_completion) — guarded by the flag,
  never raises into the producer, carries a panel deep-link when
  panel_base_url is set.
- panel credentials card (2 fields) nested in the Telegram feature-flag row.
- panel_base_url + telegram_timeout_seconds config fields.

V1 scope only: credentials + flag + panel card + client + one-line fan-out.
Out of scope (V2): inbound commands, a TelegramEngine background loop, a
dedup ledger, a bus subscription.

* [w6-telegram] fix: slave mypy/xenon regression (product tests + helper extract)

Pre-existing on slave from prior session's merges — no PR's CI caught them
(squash merges don't re-CI the result; each branch was based on older slave).

- test_product: _product helper returned MagicMock -> list invariant error;
  cast to ProductTable, move import under TYPE_CHECKING.
- test_usage: svc.session.execute (AsyncSession) has no call_args_list;
  cast to MagicMock at the two call sites.
- product.progress_for_products: xenon rank C -> extract module-level
  _project_to_products_map helper (repo pattern: helper-extract).

---------

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
2026-07-15 05:45:57 +02:00

198 lines
6.7 KiB
Python

"""W5 collision map: the pure ``build_collision_context`` builder.
Pins the truth table — no parent → None, no surfaced siblings → None,
file-overlap sibling shown, both-migration shown without file overlap,
shared-only-without-overlap NOT shown, declared-vs-actual drift computed,
and the caps respected. Pure (no DB, no IO): duck-typed task/sibling rows.
"""
from __future__ import annotations
from types import SimpleNamespace
from uuid import uuid4
from roboco.services.gateway.choreographer.collision import (
COLLISION_GLOB_CAP,
COLLISION_SIBLING_CAP,
build_collision_context,
)
def _task(
*,
parent_task_id: str | None = "p1",
project_id: str = "proj",
intends_to_touch: list[str] | None = None,
adds_migration: bool = False,
touches_shared: bool = False,
priority: int = 2,
sequence: int = 0,
) -> SimpleNamespace:
return SimpleNamespace(
id=str(uuid4()),
parent_task_id=parent_task_id,
project_id=project_id,
intends_to_touch=intends_to_touch,
adds_migration=adds_migration,
touches_shared=touches_shared,
priority=priority,
sequence=sequence,
)
def _sib(
*,
project_id: str = "proj",
intends_to_touch: list[str] | None = None,
adds_migration: bool = False,
touches_shared: bool = False,
priority: int = 2,
sequence: int = 1,
status: str = "in_progress",
branch_name: str | None = "feature/x",
pr_number: int | None = 7,
) -> SimpleNamespace:
return SimpleNamespace(
id=str(uuid4()),
project_id=project_id,
intends_to_touch=intends_to_touch,
adds_migration=adds_migration,
touches_shared=touches_shared,
priority=priority,
sequence=sequence,
status=status,
branch_name=branch_name,
pr_number=pr_number,
title="sibling",
)
def test_no_parent_returns_none() -> None:
task = _task(parent_task_id=None, intends_to_touch=["a.py"])
assert build_collision_context(task=task, siblings=[_sib()]) is None
def test_no_surfaced_siblings_returns_none() -> None:
task = _task(intends_to_touch=["a.py"])
# sibling with no collision surface at all
assert (
build_collision_context(task=task, siblings=[_sib(intends_to_touch=None)])
is None
)
def test_file_overlap_sibling_shown() -> None:
task = _task(intends_to_touch=["roboco/services/git.py"])
sib = _sib(intends_to_touch=["roboco/services/git.py", "roboco/services/x.py"])
ctx = build_collision_context(task=task, siblings=[sib])
assert ctx is not None
assert len(ctx) == 1
assert "roboco/services/git.py" in ctx[0]["overlap"]
def test_no_overlap_no_migration_not_shown() -> None:
task = _task(intends_to_touch=["roboco/a.py"])
sib = _sib(intends_to_touch=["roboco/b.py"])
assert build_collision_context(task=task, siblings=[sib]) is None
def test_both_migration_shown_without_file_overlap() -> None:
task = _task(intends_to_touch=["roboco/a.py"], adds_migration=True)
sib = _sib(intends_to_touch=["roboco/b.py"], adds_migration=True)
ctx = build_collision_context(task=task, siblings=[sib])
assert ctx is not None
assert ctx[0]["adds_migration"] is True
assert ctx[0]["overlap"] == []
def test_one_migration_only_not_shown() -> None:
# migration-chain needs BOTH adders; one alone is parallel.
task = _task(intends_to_touch=["roboco/a.py"], adds_migration=True)
sib = _sib(intends_to_touch=["roboco/b.py"], adds_migration=False)
assert build_collision_context(task=task, siblings=[sib]) is None
def test_shared_only_without_overlap_not_shown() -> None:
# touches_shared alone is too broad; it rides the file-overlap path.
task = _task(intends_to_touch=["roboco/a.py"], touches_shared=True)
sib = _sib(intends_to_touch=["roboco/b.py"])
assert build_collision_context(task=task, siblings=[sib]) is None
def test_shared_with_overlap_shown_and_flagged() -> None:
task = _task(intends_to_touch=["roboco/a.py"], touches_shared=True)
sib = _sib(intends_to_touch=["roboco/a.py"], touches_shared=True)
ctx = build_collision_context(task=task, siblings=[sib])
assert ctx is not None
assert ctx[0]["touches_shared"] is True
def test_cross_project_sibling_not_shown() -> None:
# collisions are repo-scoped; a sibling in another repo can't collide.
task = _task(intends_to_touch=["a.py"], project_id="proj")
sib = _sib(intends_to_touch=["a.py"], project_id="other")
assert build_collision_context(task=task, siblings=[sib]) is None
def test_self_excluded_from_siblings() -> None:
task = _task(intends_to_touch=["a.py"])
self_sib = _sib(intends_to_touch=["a.py"])
self_sib.id = task.id
assert build_collision_context(task=task, siblings=[self_sib]) is None
def test_drift_undeclared_computed() -> None:
task = _task(intends_to_touch=["roboco/a.py"])
sib = _sib(intends_to_touch=["roboco/a.py"])
ctx = build_collision_context(
task=task,
siblings=[sib],
actual_files=["roboco/a.py", "roboco/secret.py"],
)
assert ctx is not None
assert ctx[0]["undeclared"] == ["roboco/secret.py"]
def test_drift_omitted_without_actual_files() -> None:
task = _task(intends_to_touch=["roboco/a.py"])
sib = _sib(intends_to_touch=["roboco/a.py"])
ctx = build_collision_context(task=task, siblings=[sib])
assert ctx is not None
assert "undeclared" not in ctx[0]
def test_sibling_cap() -> None:
task = _task(intends_to_touch=["a.py"])
sibs = [
_sib(intends_to_touch=["a.py"], sequence=i)
for i in range(COLLISION_SIBLING_CAP + 5)
]
ctx = build_collision_context(task=task, siblings=sibs)
assert ctx is not None
assert len(ctx) == COLLISION_SIBLING_CAP
def test_glob_cap() -> None:
task = _task(intends_to_touch=[f"f{i}.py" for i in range(COLLISION_GLOB_CAP + 5)])
sib = _sib(intends_to_touch=[f"f{i}.py" for i in range(COLLISION_GLOB_CAP + 5)])
ctx = build_collision_context(task=task, siblings=[sib])
assert ctx is not None
assert len(ctx[0]["overlap"]) <= COLLISION_GLOB_CAP
assert len(ctx[0]["intends_to_touch"]) <= COLLISION_GLOB_CAP
def test_sort_key_orders_by_priority_then_sequence() -> None:
task = _task(intends_to_touch=["a.py"])
low_prio = _sib(intends_to_touch=["a.py"], priority=3, sequence=0)
high_prio = _sib(intends_to_touch=["a.py"], priority=1, sequence=5)
mid = _sib(intends_to_touch=["a.py"], priority=2, sequence=1)
ctx = build_collision_context(task=task, siblings=[low_prio, high_prio, mid])
assert ctx is not None
assert [e["sequence"] for e in ctx] == [5, 1, 0]
if __name__ == "__main__":
import pytest
pytest.main([__file__, "-q"])