Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(backend): a command revokes terminal contexts only when it changes someone's access (#1919)

0xken1590f3d20a279a
diff --git a/backend/integration/superTabIndexes.test.ts b/backend/integration/superTabIndexes.test.ts
index 991df6d5e..02b9b7479 100644
--- a/backend/integration/superTabIndexes.test.ts
+++ b/backend/integration/superTabIndexes.test.ts
@@ -8,6 +8,9 @@ import { MongoMemoryReplSet } from 'mongodb-memory-server-core'
import { PrismaClient } from '@prisma/client'
import { pageDeliveries, pageReactions } from '../src/superTabs/pageReads.js'
import { recentDecisions } from '../src/superTabs/routeDecisions.js'
+import { revokeNativeContexts } from '../src/superTabs/native.js'
+import { outsider, peer, swarm } from '../src/superTabs/read/fixture.test-support.js'
+import type { SuperTabState } from '../src/superTabs/model.js'
// A fresh loopback replica set, created here. This suite never reads DATABASE_URL or an existing database.
// Prod Mongo spent hours an hour on the message page's delivery read (2026-10-28): Prisma sends `messageId in [...]` as an
@@ -159,3 +162,61 @@ describe('route decision list', () => {
expect(await recentDecisions(db, 'B', [])).toEqual([])
})
})
+
+// Every command revokes the terminal contexts its change of access ends (native.ts). On 2026-10-28 that read ran on every
+// command, rebuilt from who could not read what rather than from what changed: each viewer, and each member outside each
+// private conversation, became an $or branch tested against every open context of the Swarm (prod mean 965 ms).
+describe('terminal context revoke', () => {
+ const context = (id: string, conversationId: string, userId: string) => ({ id, tabId: 'tab', conversationId, agentId: 'agent', userId, binding: {} })
+ const revoked = async () => (await db.superTabNativeContext.findMany({ where: { tabId: 'tab', revoked: true }, select: { id: true } })).map(row => row.id).sort()
+
+ /** A Swarm with a viewer and a private project its other members cannot read: what made every command revoke before. */
+ function scene() {
+ const s = swarm()
+ s.join(peer)
+ s.join(outsider)
+ const open = s.group('Open work'), done = s.group('Done work')
+ const secret = s.run({ type: 'project.create', name: 'Secret', visibility: 'private', brief: '' }).projectId as string
+ const hidden = s.group('Secret work', secret)
+ return { s, open, done, hidden, projectId: s.state.conversations.find(c => c.id === open)!.projectId as string }
+ }
+
+ it('sends no query for a command that changes no one\'s access', async () => {
+ await db.superTabNativeContext.deleteMany({})
+ const { s, open, hidden } = scene()
+ s.edit(state => { state.people.find(p => p.id === outsider.userId)!.role = 'viewer' })
+ await db.superTabNativeContext.createMany({ data: [context('owner-open', open, s.state.ownerId), context('owner-secret', hidden, s.state.ownerId)] })
+ const before = structuredClone(s.state) as SuperTabState
+ s.run({ type: 'message.send', conversationId: open, text: 'Status?', replyTo: null, recipientIds: [] })
+ const [, profile] = await profiled('SuperTabNativeContext', () => revokeNativeContexts(db, before, s.state))
+ expect(profile).toEqual({ plans: [], docsExamined: 0, returned: 0 })
+ expect(await revoked()).toEqual([])
+ })
+
+ it('revokes at once a removal, a lost conversation and an archive made by one change, and cancels their waiting turns', async () => {
+ await db.superTabNativeContext.deleteMany({})
+ await db.superTabDelivery.deleteMany({ where: { tabId: 'tab' } })
+ const { s, open, done, projectId } = scene()
+ await db.superTabNativeContext.createMany({ data: [context('peer-open', open, peer.userId), context('outsider-open', open, outsider.userId),
+ context('owner-done', done, s.state.ownerId), context('owner-open', open, s.state.ownerId)] })
+ await db.superTabDelivery.create({ data: { id: 'turn', tabId: 'tab', conversationId: open, messageId: 'm', nativeContextId: 'peer-open', agentId: 'agent',
+ machineId: 'host', membershipGeneration: 'gen', authorKind: 'person', authorId: peer.userId, authorGeneration: 'gen', status: 'queued' } })
+ const before = structuredClone(s.state) as SuperTabState
+ s.run({ type: 'member.remove', userId: peer.userId })
+ s.run({ type: 'project.sharing', projectId, visibility: 'private', memberIds: [] })
+ s.run({ type: 'conversation.archive', conversationId: done, archived: true })
+ await revokeNativeContexts(db, before, s.state)
+ expect(await revoked()).toEqual(['outsider-open', 'owner-done', 'peer-open'])
+ expect((await db.superTabDelivery.findUniqueOrThrow({ where: { id: 'turn' } })).status).toBe('cancelled')
+ })
+
+ it('revokes the contexts of a person made a viewer', async () => {
+ await db.superTabNativeContext.deleteMany({})
+ const { s, open } = scene()
+ await db.superTabNativeContext.createMany({ data: [context('peer-open', open, peer.userId), context('owner-open', open, s.state.ownerId)] })
+ const before = structuredClone(s.state) as SuperTabState
+ s.edit(state => { state.people.find(p => p.id === peer.userId)!.role = 'viewer' })
+ await revokeNativeContexts(db, before, s.state)
+ expect(await revoked()).toEqual(['peer-open'])
+ })
+})
diff --git a/backend/src/superTabs/native.ts b/backend/src/superTabs/native.ts
index 9fe1da378..04794a5af 100644
--- a/backend/src/superTabs/native.ts
+++ b/backend/src/superTabs/native.ts
@@ -248,12 +248,15 @@ export class SuperTabNativeStore {
}
}
-/** Removal followed by rejoining must not revive an old terminal binding. */
+/** Removal followed by rejoining must not revive an old terminal binding. Only what this change ended is revoked: what
+ * ended earlier was revoked by the change that ended it, and no context opens without access (open; validate checks it
+ * again at every use). Most commands change no access and read nothing here: rebuilding every viewer and every member
+ * outside every private conversation on each command kept prod Mongo busy (2026-10-28). */
export async function revokeNativeContexts(tx: Transaction, before: SuperTabState, after: SuperTabState) {
const conditions: Prisma.SuperTabNativeContextWhereInput[] = []
for (const member of before.people) {
const current = after.people.find(p => p.id === member.id)
- if (!current || current.generation !== member.generation || current.role === 'viewer') conditions.push({ userId: member.id })
+ if (!current || current.generation !== member.generation || (current.role === 'viewer' && member.role !== 'viewer')) conditions.push({ userId: member.id })
}
// A removed agent's contexts, and those of an agent given a replacement terminal: they name the old terminal, and its
// waiting turns are cancelled with them, so a claim racing the change conflicts on their rows (rule b).
@@ -263,13 +266,12 @@ export async function revokeNativeContexts(tx: Transaction, before: SuperTabStat
}
for (const group of before.conversations) {
const current = after.conversations.find(c => c.id === group.id)
- if (!current || current.archived) conditions.push({ conversationId: group.id })
+ if (!current || (current.archived && !group.archived)) conditions.push({ conversationId: group.id })
// No terminal opens in the Leads conversation (open), so it holds no context to revoke.
else if (!isLeads(current)) {
for (const member of after.people) {
- if (!canReadGroup(after, { kind: 'person', userId: member.id, email: member.email }, current)) {
- conditions.push({ conversationId: group.id, userId: member.id })
- }
+ const reader = { kind: 'person' as const, userId: member.id, email: member.email }
+ if (canReadGroup(before, reader, group) && !canReadGroup(after, reader, current)) conditions.push({ conversationId: group.id, userId: member.id })
}
const removed = group.agentIds.filter(id => !current.agentIds.includes(id))
if (removed.length) conditions.push({ conversationId: group.id, agentId: { in: removed } })