Files
roboco/tests/unit/enforcement/test_a2a_access.py
T
fc41dfa40e fix(security): active guard enforcement, CEO A2A target check, notification expiry (#595)
* fix(security): guard goes active; CEO A2A respects no-comms roles; ack notifications expire

ROBOCO_GUARD_PASSIVE_MODE defaults to false in both compose files — the
deferred post-calibration flip; fail_secure stays off and the env override
remains the rollback. can_a2a_direct no longer short-circuits the CEO past
the no-comms set (auditor/pr_reviewer/prompter/secretary), now canonical
in foundation.policy.communications.NO_COMMS_ROLES and shared with the
content-actions gate; the A2A service refuses at conversation creation
instead of silently suppressing the wake. Ack-required notifications get
expires_at stamped from ROBOCO_NOTIFICATION_ACK_TTL_HOURS (default 48,
0 disables), so the re-escalation sweeper's expires_at query matches rows
for the first time.

* refactor(notification): extract _ack_and_expiry — xenon rank back under B

The expires_at stamping pushed _create_notification_with_session to
rank C; the requires_ack + expiry derivation moves into a helper with
the same semantics and comments.

* test(conftest): dispose the global DB engine after every test

Production code reaching get_db_context()/get_engine() lazily creates the
process-global engine bound to the current event loop; with per-test
function-scoped loops, any later test touching the global path inherits a
dead-loop engine and dies with 'Future attached to a different loop' —
the order-dependent class that has been wandering the suite (cloud_auth
login, metrics, tasks-routes, full-lifecycle) whenever collection order
shifts. An autouse fixture now close_db()s after every test, keeping the
global path loop-local; no-op when untouched.

---------

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
2026-07-19 18:46:44 +02:00

122 lines
4.4 KiB
Python

"""enforcement.a2a_access coverage."""
from __future__ import annotations
import pytest
from roboco.agents_config import can_a2a_direct
from roboco.enforcement.a2a_access import (
A2AAccessDeniedError,
get_a2a_allowed_targets,
validate_a2a_access,
)
from roboco.foundation.policy.communications import NO_COMMS_ROLES
def test_validate_a2a_self_a2a_denied() -> None:
with pytest.raises(A2AAccessDeniedError, match="cannot A2A yourself"):
validate_a2a_access("be-dev-1", "be-dev-1")
def test_validate_a2a_to_ceo_denied() -> None:
with pytest.raises(A2AAccessDeniedError):
validate_a2a_access("be-dev-1", "ceo")
def test_validate_a2a_within_cell() -> None:
"""Cell members can A2A within their cell."""
result = validate_a2a_access("be-dev-1", "be-qa")
assert result is True
def test_a2a_access_denied_error_has_attributes() -> None:
err = A2AAccessDeniedError(
from_agent="be-dev-1",
to_agent="ceo",
reason="CEO is human",
)
assert err.from_agent == "be-dev-1"
assert err.to_agent == "ceo"
def test_get_a2a_allowed_targets_returns_list() -> None:
targets = get_a2a_allowed_targets("be-dev-1", ["be-qa", "be-pm", "fe-dev-1", "ceo"])
assert isinstance(targets, list)
def test_get_a2a_allowed_targets_excludes_ceo() -> None:
targets = get_a2a_allowed_targets("be-dev-1", ["ceo"])
assert "ceo" not in targets
def test_get_a2a_allowed_targets_excludes_self() -> None:
targets = get_a2a_allowed_targets("be-dev-1", ["be-dev-1", "be-qa"])
# Self should be filtered.
assert "be-dev-1" not in targets or "be-qa" in targets
# ---------------------------------------------------------------------------
# CEO-initiated A2A — the one asymmetric rule: CEO may send, nobody may
# target CEO (the block above must still hold).
# ---------------------------------------------------------------------------
def test_validate_a2a_access_ceo_to_agent_allowed() -> None:
result = validate_a2a_access("ceo", "be-dev-1")
assert result is True
def test_can_a2a_direct_ceo_to_main_pm() -> None:
assert can_a2a_direct("ceo", "main-pm") == (True, None)
def test_can_a2a_direct_ceo_to_board_member() -> None:
"""Board members are normally unreachable via direct A2A for everyone
else (routed through main-pm) — CEO is exempt from that restriction."""
assert can_a2a_direct("ceo", "product-owner") == (True, None)
def test_validate_a2a_to_ceo_still_denied_with_ceo_send_rule() -> None:
"""Regression: allowing CEO-initiated A2A must not loosen the inbound
block — nobody may target the CEO."""
with pytest.raises(A2AAccessDeniedError):
validate_a2a_access("be-dev-1", "ceo")
def test_get_a2a_allowed_targets_ceo_includes_all_roles() -> None:
targets = get_a2a_allowed_targets("ceo", ["be-dev-1", "be-qa", "main-pm"])
assert set(targets) == {"be-dev-1", "be-qa", "main-pm"}
def test_can_a2a_direct_to_ceo_message_explains_reply_only() -> None:
"""An agent can never INITIATE with the CEO (only reply inside a
conversation the CEO opened) — the matrix denial message must say so,
not point at the old blanket 'use notify()' framing."""
allowed, reason = can_a2a_direct("be-dev-1", "ceo")
assert allowed is False
assert reason is not None
assert "reply" in reason.lower()
@pytest.mark.parametrize(
"target_slug",
["auditor", "pr-reviewer-1", "intake-1", "secretary-1"],
)
def test_can_a2a_direct_ceo_to_no_comms_role_denied(target_slug: str) -> None:
"""The CEO's asymmetric reach still can't target a role with no dm/
read_a2a on its manifest (auditor, pr_reviewer, prompter, secretary) —
nothing on the other end could ever read or answer the DM. The panel's
New-DM dialog already filters these client-side (EXCLUDE_NON_DM_ROLES);
this is the server-side backstop for a direct API/A2A-service call."""
allowed, reason = can_a2a_direct("ceo", target_slug)
assert allowed is False
assert reason is not None
assert "comms" in reason.lower()
def test_can_a2a_direct_ceo_to_no_comms_role_reuses_canonical_set() -> None:
"""The refusal set must be exactly foundation.policy.communications'
NO_COMMS_ROLES — the same set services.gateway.content_actions uses to
gate the dm() sender side — so the two never drift apart."""
expected = {"auditor", "pr_reviewer", "prompter", "secretary"}
assert {role.value for role in NO_COMMS_ROLES} == expected