Skip to content

feat(permissions): add conversations namespace and update permissions… - #617

Merged
pikann merged 2 commits into
masterfrom
feature/add-conversation-event-namespace
Oct 21, 2026
Merged

pikann merged 2 commits into
masterfrom
feature/add-conversation-event-namespace

Conversation

@pikann

@pikann pikann commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #615. services/realtime routed every agent.* event to a project's tasks room, which anyone with tasks.read joins. Those events carry full conversation content (messages, reasoning, tool actions), so users without conversations.read, or excluded from a specific conversation, received it live even though the REST API denies them.

Changes (services/realtime)

  • conversations namespace gated on conversations:read (permissions.ts); agent.* events no longer route to tasks.
  • Per-conversation authorization (conversation-access.ts, subscriber.ts, api-client.ts): the conversations room only lists candidates (project-wide read). For each event, the service asks the API (GET /projects/:p/conversations/:id, as the socket's account, the same check the REST routes use) and emits only to accounts that can read that conversation. This covers IAM policies that exclude a single conversation (e.g. by environment), which a project-wide permission list cannot see.
    • Answers cached 30s per account + conversation.
    • Events of one conversation are delivered in order.
    • Fails closed on API errors; a 401 disconnects the socket so it reconnects with a fresh token.
  • Task boards keep refreshing: agent.session.started is also sent to the tasks room with only project_id and conversation_id. agent.acp_bridge.status (no conversation content) stays on tasks.
  • README documents the conversations room and the delivery model.
  • Tests (conversation-access.test.ts): caching, uncached failures, ordering, allowed/denied delivery, fail-closed and 401 handling, namespace mapping.

Owner-private and global-chat events are unaffected (they already route to the owner's per-user room).

Testing

  • tsc --noEmit, biome check and bun test (11 tests) pass in services/realtime.
  • Not exercised against a running stack.

Known limitations

  • Accounts whose conversations:read is only conditional (not project-wide) are not in the room, so they get no live events, same as the other namespaces.
  • Access is rechecked at most every 30s, so a revoked account can receive events for up to that long.
  • The cache and fetchSockets assume a single realtime instance; scaling out would need revisiting.
  • This only covers conversations. Other namespaces (tasks, docs, sprints, environments, workflows) still use project-wide room membership fixed at join, so a conditional Deny (a folder, a status, one environment) can still leak events, an account with only conditional grants gets none, and role changes apply only after a reconnect.

Planned follow-up

A separate PR series will replace the conversation-only check with one mechanism for every event type, so that realtime follows the IAM resource-level policies. Proposal: docs/architecture/realtime-authorization.md (to be added in that series). In short:

  1. Event manifest + contract test (no behaviour change): one table maps each event type to its namespace and permission, replacing the if chain in permissions.ts; a contract file pins the routing.
  2. Access classes + batched resource checks: the API classifies each account per project and namespace as all / scoped / none. all accounts keep using shared rooms (O(1) per event). Only scoped accounts get per-resource checks, answered by the Go IAM engine through a batched API endpoint, with a short-lived cache. This replaces conversation-access.ts and closes the gaps above.
  3. Fresh access: an iam.access_changed event makes realtime re-run joins and drop cached decisions when roles, policies or members change.
  4. Internal credential + replica-local delivery: checks no longer depend on the socket's expiring JWT, and each realtime replica delivers only to its own sockets, removing the single-replica constraint.

Open points: telling a client it lost access to a resource after an update, a bulk-authorize endpoint for hot resources with many scoped accounts, and whether plugin event topics need a namespace.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Minor suggestion inline — the security fix itself looks correct.

Reviewed changes

  • conversations namespace added — new EventNamespace value mapped to conversations:read in NAMESPACE_PERMISSIONS (permissions.ts:21, :31).
  • agent.* re-routed — eventNamespace() now sends project-scoped agent.* events to project:<id>:conversations instead of tasks (permissions.ts:70).
  • Join/leave need no change — server.ts iterates NAMESPACE_PERMISSIONS, so the new room is joined for users with conversations:read. Owner-private and global-chat agent.* events keep routing to the per-user room in subscriber.ts.

I verified the project-scoped branch actually carries conversation content: agent.conversation.*, agent.thinking/action/observation/message, and agent.agent_message_chunk are all correctly gated behind conversations:read now, closing the #615 leak. One agent.* event is not conversation content and is affected by the blanket rule — flagged inline.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

Comment thread services/realtime/src/permissions.ts Resolved

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

The agent.session.started tasks-room copy this PR adds is unreachable for the events the API actually publishes — the event instead lands in the whole conversations room with no per-conversation check. Details inline.

Reviewed changes

  • Per-conversation delivery added — conversation-access.ts checks each candidate socket's account against GET /projects/:id/conversations/:id (30s cache), serializes events per conversation, and fails closed; a 401 disconnects the socket so it re-handshakes.
  • API client extended — canReadConversation (api-client.ts) calls the REST route that enforces per-conversation conversations:read, returning false on 403/404 and throwing otherwise.
  • Project-scoped agent.* routing reworked — events with a top-level conversation_id go through the per-conversation check; agent.session.started is copied to the tasks room; agent.acp_bridge.status returns to tasks.
  • Prior review feedback addressed — agent.acp_bridge.status is now special-cased to the tasks room ahead of the generic agent.* rule (permissions.ts:69), resolving the earlier inline thread.
  • Docs and tests added — README documents the conversations room and delivery model; conversation-access.test.ts covers caching, ordering, allow/deny, fail-closed, 401 handling, and namespace mapping.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

): void {
// Task boards refresh on a conversation starting. The task-room copy
// carries no conversation content, only which conversation it was.
if (event.type === "agent.session.started") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tasks-room copy never runs for real traffic. The API fans agent.session.started out as a task activity whose payload keeps conversation_id inside the stringified content field ({"conversation_id":…,"agent_id":…}), not at the top level, so the guard at subscriber.ts:179 is false, routeConversationEvent is never entered, and the event falls through to the whole project:<id>:conversations room — bypassing the per-conversation check this PR introduces. The README's "payload-free … also goes to the tasks room" exception (README.md:116) therefore doesn't hold.

Technical details
# `agent.session.started` never reaches `routeConversationEvent`

## Affected sites
- `services/realtime/src/subscriber.ts:179-184` — guard requires a top-level string `conversation_id`; the activity payload has none.
- `services/realtime/src/subscriber.ts:225-230` — the dead tasks-room copy.
- `services/api/internal/service/activity/service.go:85-99` — `EntityPayload` puts `conversation_id` inside the stringified `content`.
- `services/api/internal/service/task/activity_service.go:232-243`, `services/api/internal/transport/http/handler/agent_handler.go:1684-1696`, `services/api/internal/worker/notification_consumer.go:355-366`, `services/api/internal/worker/automation_consumer.go:2238-2249` — all record `agent.session.started` with `content = {conversation_id, agent_id}`; `FanoutTo` publishes the payload verbatim.
- `services/realtime/README.md:116-118`, `services/realtime/src/subscriber.ts:20-21`, `:223-224` — document an exception that does not occur.

## Required outcome
- Task boards receive the payload-free `agent.session.started` signal as documented (or the README/PR body drops the claim), and the event is not broadcast to the whole `conversations` room unchecked.

## Suggested approach (optional)
- Lift `conversation_id` to the top level of the `agent.session.started` realtime payload (in the producer's `RecordActivity` payload shape), so the guard matches; or parse the nested `content` in `routeEvent`.
- Consider making the generic branch fail closed for `agent.*` events without a top-level `conversation_id`, so a future content-bearing agent event (or a plugin-emitted topic via `paca.event_emit`/`paca.activity_record`) cannot reach the whole room unchecked.
- Update the now-inaccurate "no per-message permission checks are needed" headers in `subscriber.ts:29-33` and `server.ts:29-32`.

## Open questions for the human
- Is the task-board refresh a real requirement? The web client's `applyRealtimeAgentEvent` (`apps/web/src/lib/agent-api.ts:1839`) also keys on a top-level `conversation_id`, so `use-project-realtime.ts:302`'s `invalidateTaskViews()` for this type was already dead before this PR.

@pikann
pikann merged commit 47e7b87 into master Oct 21, 2026
2 checks passed
@pikann
pikann deleted the feature/add-conversation-event-namespace branch October 21, 2026 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Realtime: agent.* conversation events are delivered to the project's tasks room (bypasses conversations.read and restricted-agent access)

1 participant