Skip to content
Toolkit

ADR-0106: A channel key is built in one place, because it is the tenant boundary

Status: Accepted · Date: 2026-08-30 · Track: 006 phase 00

Context

The realtime gateway has been in this tree since 003. Every SSE connection is subscribed to user:{id} and workspace:{id}, and events are fanned out by Redis pub/sub when it is configured and by an in-process bus when it is not.

On that bus a subscriber receives exactly what is published to the names it asked for. Nothing else authorises anything: there is no per-message tenant check, no membership assertion between publish and deliver, and there cannot usefully be one — the bus is a string-keyed fan-out. The channel key is the whole access control mechanism.

Both existing call sites built that key by hand:

h.realtime.Subscribe(r.Context(),
    fmt.Sprintf("user:%d", userID),
    fmt.Sprintf("workspace:%d", workspaceID))
...
h.realtime.Publish(r.Context(), fmt.Sprintf("user:%d", userID), event)

Two copies of a format string is a coincidence. 006 adds five publishers, and each one is an opportunity to write workspace:%s over a slug, to publish on the user channel what belongs on the workspace channel, or to interpolate the acting member's id where the target workspace's was meant. Every one of those compiles, publishes, passes review and reads like the line above it. The failure is not an exception or a 500; it is a member of another workspace quietly receiving a document they cannot open.

This is why the charter puts the helper before the publishers, and why phase 00 moves both call sites while there are only two.

Decision

1. apps/api/internal/services/channels.go is the only place a channel key is constructed. It exports ChannelUser(int64) and ChannelWorkspace(int64) over unexported prefix constants, so a key cannot be assembled from a different scope's prefix by accident.

2. check:realtime rule T4 fails the build on a channel key built anywhere else. The rule reads the call sites rather than grepping for format strings: every channel argument handed to the realtime service — Publish's channel, all of Subscribe's — must be a Channel*() call. Grepping would have been both laxer and noisier; the cache builds user:%d keys of its own (services/cache.go), which are not tenant channels and are not this gate's business.

Test files are exempt: the bus's own tests address it with literals on purpose, because what they test is the bus, not the boundary.

3. The isolation property is a test with its own evidence file, not a line in a feature report — evidence/isolation-test.md, watched failing against a ChannelWorkspace that returns one shared key for every workspace.

4. The ledger's deferred-by-measurement state counts as neither wired nor unwired, and rule T3 requires such a row to cite the transport measurement as soon as reports/TRANSPORT-MEASUREMENT.md exists. Live cursors is minted in that state before phase 01 has measured anything, which is a claim the ledger would otherwise be making early; T3 is what stops the deferral outliving the measurement that is supposed to justify it. This is 008's redirect-by-design one concept over, with a citation requirement 008 did not need.

Consequences

A new publisher cannot invent a key. It either calls a constructor that exists or adds one to the helper — where the addition is a reviewable diff in the one file whose whole subject is the tenant boundary, rather than a Sprintf on line 190 of a handler about something else.

The gate is a floor, not a proof. It holds that keys come from one place; it does not hold that the right workspace id was passed to the constructor. That is what the isolation test asserts, and what each phase's two-context live spec asserts again from the outside.

Nothing observable changes in this commit: the keys produced are byte-identical to the ones the two call sites were already building, which is the point — the boundary was moved into one file without moving the wire.

Source: docs/decisions/0106-a-channel-key-is-built-in-one-place-because-it-is-the-tenant-boundary.md

Library