fix: prod triage 2026-07-08 — MCP auth residue, gateway envelopes, verb-loop cap, A2A interjection, manual spawn UX (#334)

* fix(auth): pass agent UUID to CLI-arg MCP servers (optimal/docs/search)

The container token is HMAC-signed over the agent UUID (#314), but the
optimal/docs/search MCP servers received the slug as their CLI arg and
sent X-Agent-ID=<slug>, so every research/RAG/docs call 401ed with
signature mismatch under enforced auth. Pass the already-computed
agent_uuid in the three args lists instead.

* fix(gateway): include remediate in gateway.rejected audit details

Conventions-gate rejections carry the offending file:line listing only
in the envelope's remediate field, which the audit row dropped -- ops
logs showed just the violation count with no way to see what blocked.

* fix(gateway): return envelope on do/commit git failure

A GitError from the commit verb propagated to the generic middleware
handler, so agents got a raw error blob with no remediate/next. Catch
it and return an error envelope; 'no changes added to commit' with an
explicit files list now names the mismatch and the omit-files fallback.

* fix(agent-sdk): absolute rejection cap breaks slow-drip verb loops

The verb circuit breaker only counted rejections inside a 60s sliding
window, so an agent retrying i_am_done every 3-4 minutes looped for 30+
minutes without tripping it. Add a session-scoped cumulative per-(verb,
task) cap at 3x the windowed limit that trips regardless of pacing.

* feat(a2a): CEO chime-in interjects into the viewed conversation

Previously reply_as_ceo re-homed the message into a canonical CEO<->target
conversation with no panel surface, so a chime-in reported success but was
invisible and only opportunistically delivered. interject_as_ceo now inserts
the message into the conversation being viewed (from_agent=ceo, directed via
an @target content prefix), bumps that conversation's counters with the
unread ping keyed to the addressed participant, and both participants see it
in transcript and read_a2a.

* feat(panel): manual spawn carries task + message, surfaces refusals

The agent detail page spawned with no request body (task/message impossible),
the spawn button could double-fire (2.5ms double-POST seen live), and refusal
reasons never reached the UI: readiness refusals were generic 500s and the
already-running no-op looked like success. Detail page now uses
SpawnAgentDialog, a synchronous ref guard blocks re-entry, AgentReadinessError
maps to 409 with its reason shown, already_running is signalled and toasted,
and a task_id builds a task-aware prompt instructing the claim (task_id alone
never did), with the CEO's message appended as a note.

* test(panel): align a2a page test with the interjection footer copy

The chime-in rebuild changed the composer footer; the page-level test
asserting the old copy was outside the rebuild's scoped vitest run.

* fix(api): commit the request DB session before the response is sent

FastAPI unwinds yield-dependencies after the response bytes go out, so
get_db's post-yield commit raced the client's next request -- a verb
could return ok while its claim/status write was still uncommitted (the
e2e ok-without-effect flake family), and a failed commit was silently
lost behind an already-sent 200. DbCommitMiddleware (innermost, pure
ASGI) commits the session stashed by get_db_committed before forwarding
http.response.start; commit failure now surfaces as a 5xx. get_db is
untouched for its direct non-request callers.

* fix(db): invalidate, not rollback, the session on request cancellation

With the commit moved into the send path, the flow-verb timeout can
cancel mid-commit; rolling back then issues another command over an
asyncpg connection stranded mid-wire-protocol, and the poisoned
connection segfaults uvloop/asyncpg when a later checkout recycles it
(3/3 identical CI faulthandler dumps). On CancelledError discard the
connection via session.invalidate() -- SQLAlchemy's documented handling
for a timeout during commit -- and keep rollback for plain exceptions.

---------

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
This commit is contained in:
Renzo F
2026-07-08 10:41:02 +02:00
committed by GitHub
co-authored by Renn F
parent 60f571bc02
commit 312ec990dd
33 changed files with 1923 additions and 148 deletions
+121
View File
@@ -731,6 +731,127 @@ async def test_get_conversation_admin_returns_none_for_unknown(
assert await svc.get_conversation_admin(uuid4()) is None
# ---------------------------------------------------------------------------
# interject_as_ceo — the CEO's one-directional interjection into a watched
# agent<->agent conversation (not a re-homed CEO<->target DM).
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_interject_as_ceo_lands_in_viewed_conversation_with_prefix(
a2a_setup: dict,
) -> None:
svc = a2a_setup["svc"]
conv = await svc.get_or_create_conversation("be-dev-1", "be-qa")
msg = await svc.interject_as_ceo(UUID(conv.id), "be-qa", "ship it")
assert msg.conversation_id == conv.id
assert msg.from_agent == "ceo"
assert msg.content == "@be-qa: ship it"
@pytest.mark.asyncio
async def test_interject_as_ceo_bumps_target_unread_when_target_is_agent_b(
a2a_setup: dict,
) -> None:
"""Canonical order makes "be-qa" agent_b — its counter, not agent_a's,
must move; the other participant gets no ping."""
svc = a2a_setup["svc"]
db = a2a_setup["db"]
conv = await svc.get_or_create_conversation("be-dev-1", "be-qa")
assert conv.agent_a == "be-dev-1"
assert conv.agent_b == "be-qa"
await svc.interject_as_ceo(UUID(conv.id), "be-qa", "ship it")
row = await db.get(A2AConversationTable, UUID(conv.id))
assert row is not None
assert row.unread_by_b == 1
assert row.unread_by_a == 0
@pytest.mark.asyncio
async def test_interject_as_ceo_bumps_target_unread_when_target_is_agent_a(
a2a_setup: dict,
) -> None:
svc = a2a_setup["svc"]
db = a2a_setup["db"]
conv = await svc.get_or_create_conversation("be-dev-1", "be-qa")
await svc.interject_as_ceo(UUID(conv.id), "be-dev-1", "ship it")
row = await db.get(A2AConversationTable, UUID(conv.id))
assert row is not None
assert row.unread_by_a == 1
assert row.unread_by_b == 0
@pytest.mark.asyncio
async def test_interject_as_ceo_bumps_message_count_and_last_message_at(
a2a_setup: dict,
) -> None:
svc = a2a_setup["svc"]
db = a2a_setup["db"]
conv = await svc.get_or_create_conversation("be-dev-1", "be-qa")
await svc.send_chat_message(UUID(conv.id), "be-dev-1", "hello")
await svc.interject_as_ceo(UUID(conv.id), "be-qa", "ship it")
row = await db.get(A2AConversationTable, UUID(conv.id))
assert row is not None
_EXPECTED_MESSAGE_COUNT = 2
assert row.message_count == _EXPECTED_MESSAGE_COUNT
assert row.last_message_at is not None
@pytest.mark.asyncio
async def test_interject_as_ceo_rejects_non_participant_target(
a2a_setup: dict,
) -> None:
svc = a2a_setup["svc"]
conv = await svc.get_or_create_conversation("be-dev-1", "be-qa")
with pytest.raises(ValueError, match="not a participant"):
await svc.interject_as_ceo(UUID(conv.id), "ghost-agent", "hi")
@pytest.mark.asyncio
async def test_interject_as_ceo_unknown_conversation_raises(
a2a_setup: dict,
) -> None:
svc = a2a_setup["svc"]
with pytest.raises(ValueError, match="Conversation not found"):
await svc.interject_as_ceo(uuid4(), "be-qa", "hi")
@pytest.mark.asyncio
async def test_interject_as_ceo_publishes_a2a_message_sent_event(
a2a_setup: dict,
) -> None:
"""Same operator-live-view chokepoint as send()/send_chat_message() —
the panel's /ws/system invalidation must fire for an interjection too."""
svc = a2a_setup["svc"]
task_id = a2a_setup["task_id"]
conv = await svc.get_or_create_conversation("be-dev-1", "be-qa", task_id=task_id)
mock_bus = AsyncMock()
mock_bus.is_connected = lambda: True
mock_bus.publish = AsyncMock(return_value=None)
with patch("roboco.services.a2a.get_event_bus", return_value=mock_bus):
sent = await svc.interject_as_ceo(UUID(conv.id), "be-qa", "ship it")
mock_bus.publish.assert_awaited_once()
published = mock_bus.publish.await_args.args[0]
assert published.type is EventType.A2A_MESSAGE_SENT
data = published.data
# Points at the VIEWED conversation, not a re-homed ceo<->target one.
assert data["conversation_id"] == conv.id
assert data["conversation_id"] == sent.conversation_id
assert data["task_id"] == str(task_id)
assert data["from_agent"] == "ceo"
assert data["to_agent"] == "be-qa"
# ---------------------------------------------------------------------------
# list_admin_pairs — the A2A switchboard's static-matrix + DB join
# ---------------------------------------------------------------------------