feat(slack-relay): never answer a DM from outside the agent's relaygate group

Merged
KH
5d ago
+320-29

What

Defense in depth for slack-relay behind relaygate: a Slack thread agent may receive relay DMs from agents outside its group (e.g. local agents), but the host never sends anything back to them. relaygate's policy already refuses pit→outside writes; this makes the host never attempt a reply the gate has not allowed (an extra check before sending, against the same policy; not a separate same-group rule).

How

  • AgentHost gets a dmCheck(token, to, content) hook. main.ts wires it behind relaygate to POST /v1/check with the agent's own rg_ token: {"op": "w", "resource": {"kind": "stream", "id": "relaycast:inbox/<sender>"}, "content": ...} (per relaygate docs/contract.md: a DM is a write to the recipient's inbox).
  • The check runs before the turn (content "", so the prompt can tell the agent whether a reply can go back) and again with the exact answer text right before dm(). Only allow sends. A denial, unknown recipient, or gate error (after checkWithRetry) withholds the reply and logs <agent>: reply withheld: recipient outside group (<sender>; denied|gate unavailable), with names only and no text.
  • The turn still runs and the session still uploads. Behind relaygate the system prompt (DM and Slack thread turns) says outside senders can't be answered, and an outside DM's prompt says nothing goes back and not to promise a reply.
  • Automatic answers (agent.dm.reply) are still never answered, and they get no check.
  • Agent-initiated sends via mcp__relaycast go through relaygate's proxy with the agent's token, so they're already enforced. Documented; nothing added.
  • host.replyToOutside (REPLY_TO_OUTSIDE, default false): behind relaygate it is ignored (logged at startup). Without relaygate, DMs are answered exactly as before.

Tests

components/slack-relay/test/dm-outside.test.ts, against a fake gate:

  • outside sender → turn runs, session uploads, one pre-check, no send attempted, log has names only
  • pit sender → both checks ("", then the exact answer) as the agent on the sender's inbox, reply sent
  • the answer's own check fails → withheld
  • gate 503 / unreachable → no send
  • no relaygate → unchanged (and auto-answers still not answered)
  • auto-answer from outside → no check, no send

npm run typecheck and npm test are green locally (66 pass, e2e skipped without engine).

Chart / docs

  • slack-relay 0.3.8, agent-relay 0.7.46 (Chart.lock regenerated), host.replyToOutside: false + REPLY_TO_OUTSIDE env
  • docs/self-hosted-slack-relay.md: new "Messages from outside the pit"; registry entries + rendered requirements doc

Notes

  • The pre-turn check with empty content runs relaygate's classifier for a cross-group write the mode bits allow (an extra classifier call per such DM). The final check is the authoritative one.
  • The reply dm() carries data: {kind, session_id} metadata besides the checked text, as before. The proxy checks it again anyway.
  • Not deployed. Merged main after #437 (which took 0.3.7 / 0.7.45).

🤖 Generated with Claude Code


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.


Summary by cubic

Behind relaygate, a Slack thread agent now never answers a DM from outside its group, even though the message itself is still taken in.

  • Before any DM answer, the host asks relaygate's /v1/check whether the agent may write the exact answer text to the sender's inbox (relaycast:inbox/<sender>); only an allow sends, and a denial, unknown recipient, or gate error fails closed and logs names only.
  • The host check runs against the same policy relaygate's proxy already enforces on the send itself—it's an extra pre-send check, not a separate same-group rule. If the policy allows a pit-to-outside write and the classifier passes, the answer is sent.
  • The turn still runs and the session still uploads, and the agent's prompt says nothing will go back so it won't promise a reply.
  • Automatic answers remain never answered; agent-initiated sends via mcp__relaycast stay enforced by relaygate's proxy, with nothing added.
  • host.replyToOutside / REPLY_TO_OUTSIDE (default false) is ignored behind relaygate and only preserves current behavior on installs without it.
  • Charts bump to slack-relay 0.3.8 and agent-relay 0.7.46, docs gain a "Messages from outside the pit" section, and tests cover gate allow/down/absent and no-relaygate paths.