Skip to content

Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.

TypeScript

fix(runtime): scope context reads to agent membership (#77)

OKNOTOK3661d7797a49
diff --git a/server/src/__integration__/runtime-server.test.ts b/server/src/__integration__/runtime-server.test.ts
index 09d2f29..45ba1bc 100644
--- a/server/src/__integration__/runtime-server.test.ts
+++ b/server/src/__integration__/runtime-server.test.ts
@@ -12,6 +12,8 @@
* - Identity pin: when a request body claims `agentId: <spoof>` while
* the JWT pins someone else, the row that lands in Postgres uses the
* JWT subject, not the body claim.
+ * - Conversation scope: context reads return only conversations in the
+ * JWT-pinned agent's tenant where that agent is a current member.
* - Payload validation: 400s for missing required fields, without ever
* touching the DB.
*
@@ -92,6 +94,26 @@ async function seedAgent(): Promise<{ agentId: string; companyId: string; token:
return { agentId, companyId, token }
}
+async function seedContextConversation(opts: {
+ companyId: string
+ members: string[]
+ authorId: string
+ body: string
+}): Promise<string> {
+ const conversationId = `cv-${randomUUID().slice(0, 8)}`
+ await pool.query(
+ `INSERT INTO conversations (id, kind, title, members, company_id)
+ VALUES ($1, 'group', $2, $3::jsonb, $4)`,
+ [conversationId, `Context ${conversationId}`, JSON.stringify(opts.members), opts.companyId],
+ )
+ await pool.query(
+ `INSERT INTO messages (id, conversation_id, author_id, kind, body, sequence, company_id)
+ VALUES ($1, $2, $3, 'text', $4, 1, $5)`,
+ [`m-${randomUUID()}`, conversationId, opts.authorId, opts.body, opts.companyId],
+ )
+ return conversationId
+}
+
// ── auth gate ──────────────────────────────────────────────────────────
test('[integration] runtime: missing Authorization → 401', async () => {
@@ -132,6 +154,53 @@ test('[integration] runtime: expired token → 401', async () => {
assert.match(String(r.body?.error ?? ''), /expired/i)
})
+test('[integration] runtime: /context enforces tenant and conversation membership', async () => {
+ const { agentId, companyId, token } = await seedAgent()
+ const peerId = `a-${randomUUID().slice(0, 8)}`
+ await pool.query(
+ `INSERT INTO participants (id, company_id, kind, name, role, initial, avatar_bg, status)
+ VALUES ($1, $2, 'agent', $3, 'tester', 'P', '#abcdef', 'avail')`,
+ [peerId, companyId, peerId],
+ )
+
+ const allowedId = await seedContextConversation({
+ companyId,
+ members: [agentId, peerId],
+ authorId: peerId,
+ body: 'member-visible',
+ })
+ const nonMemberId = await seedContextConversation({
+ companyId,
+ members: [peerId],
+ authorId: peerId,
+ body: 'same-tenant-private',
+ })
+ const otherTenant = await seedAgent()
+ const crossTenantId = await seedContextConversation({
+ companyId: otherTenant.companyId,
+ // Deliberately include the caller's globally unique id: tenant binding
+ // must still reject a corrupt or forged cross-company members array.
+ members: [agentId, otherTenant.agentId],
+ authorId: otherTenant.agentId,
+ body: 'cross-tenant-private',
+ })
+
+ const r = await call('/runtime/context', {
+ token,
+ body: { conversationIds: [allowedId, nonMemberId, crossTenantId] },
+ })
+
+ assert.equal(r.status, 200)
+ assert.deepEqual(
+ (r.body?.rows ?? []).map((row: { conversation_id: string; company_id: string; body: string }) => ({
+ conversationId: row.conversation_id,
+ companyId: row.company_id,
+ body: row.body,
+ })),
+ [{ conversationId: allowedId, companyId, body: 'member-visible' }],
+ )
+})
+
// ── identity pin: agentId comes from JWT, not request body ────────────
test('[integration] runtime: /runs records the JWT subject, not whatever the body claims', async () => {
diff --git a/server/src/agents/runtime/client.ts b/server/src/agents/runtime/client.ts
index 59f0e91..61cd5cb 100644
--- a/server/src/agents/runtime/client.ts
+++ b/server/src/agents/runtime/client.ts
@@ -209,7 +209,9 @@ export interface AgentRuntimeClient {
projectIds?: readonly string[];
conversationIds?: readonly string[];
}): Promise<MemoryRow[]>
- /** Per-conversation recent history for every convo with unread items. */
+ /** Per-conversation recent history for every convo with unread items.
+ * Implementations must treat conversationIds as selectors, not proof of
+ * access, and enforce the agent's tenant + current membership. */
loadContext(agentId: string, conversationIds: string[]): Promise<ContextRow[]>
/** Agent's feelings about people (the climate model). */
loadClimate(agentId: string): Promise<ClimateRow[]>
diff --git a/server/src/agents/runtime/inproc-client.ts b/server/src/agents/runtime/inproc-client.ts
index 483fb40..4ff46cd 100644
--- a/server/src/agents/runtime/inproc-client.ts
+++ b/server/src/agents/runtime/inproc-client.ts
@@ -353,6 +353,9 @@ export class InProcRuntimeClient implements AgentRuntimeClient {
* JSON array so the prompt renderer doesn't need a second roundtrip. */
async loadContext(agentId: string, conversationIds: string[]): Promise<ContextRow[]> {
if (conversationIds.length === 0) return []
+ // conversationIds can come directly from an authenticated runtime caller.
+ // They narrow the read but do not authorize it: bind every conversation to
+ // the active agent's tenant and require current conversation membership.
// Drive from conversations (PK lookup on the given ids) + a LATERAL that pulls
// each conversation's most-recent 25 messages via idx_messages_convo_created.
// The old form scanned ALL messages of each conversation (big "allhands" rooms
@@ -391,6 +394,11 @@ export class InProcRuntimeClient implements AgentRuntimeClient {
WHERE cr.conversation_id = c.id AND hp.kind = 'human'
) AS human_last_read_at
FROM conversations c
+ JOIN participants requesting_agent
+ ON requesting_agent.id = $1
+ AND requesting_agent.company_id = c.company_id
+ AND requesting_agent.kind = 'agent'
+ AND requesting_agent.departed_at IS NULL
LEFT JOIN projects pr ON pr.id = c.project_id
JOIN LATERAL (
SELECT * FROM messages mm
@@ -400,6 +408,7 @@ export class InProcRuntimeClient implements AgentRuntimeClient {
) m ON true
LEFT JOIN participants p ON p.id = m.author_id AND p.company_id = c.company_id
WHERE c.id = ANY($2::text[])
+ AND c.members @> to_jsonb(ARRAY[$1::text])
)
SELECT id, conversation_id, company_id, conversation_title, conversation_kind, conversation_topic,
project_name,