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.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(swarm): agents checked at once in a host read share one Workspace membership read (#2032)
0xken15a28c644dbea2
diff --git a/backend/integration/superTabs.test.ts b/backend/integration/superTabs.test.tsindex e107d6bdc..e9dfd3c80 100644--- a/backend/integration/superTabs.test.ts+++ b/backend/integration/superTabs.test.ts@@ -9,7 +9,8 @@ import { MongoMemoryReplSet } from 'mongodb-memory-server-core'import { PrismaClient } from '@prisma/client'import Fastify, { type FastifyInstance } from 'fastify'import type { AuthUser } from '../src/lib/ssoAuth.js'-import { ABANDONED_START_MS, SuperTabStore } from '../src/superTabs/store.js'+import { ABANDONED_START_MS, GrantReads, SuperTabStore } from '../src/superTabs/store.js'+import { HostReads } from '../src/superTabs/read/host.js'import { MemoryAttachmentStore } from '../src/superTabs/attachments.js'import { superTabRoutes } from '../src/routes/superTabs.js'import { errorHandler } from '../src/middlewares/errorHandler.js'@@ -2576,6 +2577,29 @@ describe('grant checks of the host roster', () => {memberships: count('SwarmWorkspaceMember.findUnique') }).toEqual({ machines: 1, shares: 1, contributors: 2, memberships: 2 })})+ it('checks agents of one contributor checked at once with one read of each lookup', async () => {+ const { tab, add } = await sharedMachine()+ const agents = [await add(tab, 'Peer worker 1', peer), await add(tab, 'Peer worker 2', peer), await add(tab, 'Peer worker 3', peer)]+ const { state } = await store.read(owner, tab)+ const { store: counting, count } = counted()+ const reads = new GrantReads()+ await Promise.all(agents.map(id => counting.agentGrant(counting.db, state, state.agents.find(a => a.id === id)!, reads)))+ expect({ machines: count('Machine.findFirst'), shares: count('MachineShare.findUnique'), contributors: count('User.findUnique'),+ memberships: count('SwarmWorkspaceMember.findUnique') }).toEqual({ machines: 1, shares: 1, contributors: 1, memberships: 1 })+ })++ it('keeps nothing between two host reads of one store: a share revoked between them drops the rows on the next', async () => {+ const { machineId, tab, add } = await sharedMachine()+ const mine = [await add(tab, 'Lead', owner, true), await add(tab, 'Owner worker', owner)]+ const theirs = [await add(tab, 'Peer worker 1', peer), await add(tab, 'Peer worker 2', peer)]+ const hosts = new HostReads(counted().store)+ const roster = async () => agentIds((await hosts.changes(host, machineId, { full: true })).tabs.flatMap(t => 'roster' in t ? t.roster : []), tab)+ expect(await roster()).toEqual([...mine, ...theirs].sort())+ const before = await db.machineShare.findUniqueOrThrow({ where: { machineId } })+ await db.machineShare.update({ where: { machineId }, data: { emails: [owner.email], grantVersions: nextMachineShareGrants(before, [owner.email], null) } })+ expect(await roster()).toEqual(mine.sort())+ })+it('still drops only the rows of a contributor whose share was revoked', async () => {const { machineId, tab, add } = await sharedMachine()const mine = [await add(tab, 'Lead', owner, true), await add(tab, 'Owner worker', owner)]diff --git a/backend/src/superTabs/store.ts b/backend/src/superTabs/store.tsindex 773b7a00a..4c99c79d6 100644--- a/backend/src/superTabs/store.ts+++ b/backend/src/superTabs/store.ts@@ -572,7 +572,7 @@ export class SuperTabStore {/** `reads`: one request's reads, shared by every agent it checks (GrantReads). */async agentGrant(tx: Transaction, state: SuperTabState, target: Agent, reads?: GrantReads): Promise<void> {const once = <T>(key: string, read: () => Promise<T>) => reads ? reads.once(key, read) : read()- await requireWorkspaceAccess(tx, state, target.contributorId, reads)+ await once(`workspace:${state.workspaceId}:${target.contributorId}`, () => requireWorkspaceAccess(tx, state, target.contributorId))const machine = await once(machineKey(state.environment, target.machineId, target.machineOwnerId), () => tx.machine.findFirst({where: { machineId: target.machineId, userId: target.machineOwnerId, autonomousEnv: state.environment, ...machineAlive } }))if (!machine || !machineBillingAllowsDataPlane(machine)) fail('HOST_UNAVAILABLE', 'This execution host is unavailable.', 403)diff --git a/backend/src/superTabs/workspaceStore.ts b/backend/src/superTabs/workspaceStore.tsindex 0192e66db..a3f5d40cf 100644--- a/backend/src/superTabs/workspaceStore.ts+++ b/backend/src/superTabs/workspaceStore.ts@@ -26,15 +26,14 @@ export async function activeMember(tx: Transaction | PrismaClient, workspaceId:const checked = new WeakMap<object, Map<string, boolean>>()/** Whether the person is still in the Swarm's Workspace; a Swarm from before Workspaces passes until the switch is on. Read once- * per person per transaction, which reads one snapshot, or per `request` when its caller names one (a host read's GrantReads). */-export async function hasWorkspaceAccess(tx: Transaction, state: Pick<SuperTabState, 'workspaceId'>, userId: string, request?: object): Promise<boolean> {+ * per person per transaction, which reads one snapshot. */+export async function hasWorkspaceAccess(tx: Transaction, state: Pick<SuperTabState, 'workspaceId'>, userId: string): Promise<boolean> {if (!state.workspaceId) return !workspacesRequired()- // Remembered only within a transaction or the request named. A read outside both (the read cache's, cache.ts) runs on the- // process's own client, which lives as long as the process: an answer kept on it would outlive the person's removal.- const scope = request ?? (typeof (tx as { $transaction?: unknown }).$transaction === 'function' ? null : tx)- if (!scope) return !!await activeMember(tx, state.workspaceId, userId)- const answers = checked.get(scope) ?? new Map<string, boolean>()- checked.set(scope, answers)+ // Remembered only within a transaction. A read outside one (the read cache's, cache.ts) runs on the process's own client,+ // which lives as long as the process: an answer kept on it would outlive the person's removal.+ if (typeof (tx as { $transaction?: unknown }).$transaction === 'function') return !!await activeMember(tx, state.workspaceId, userId)+ const answers = checked.get(tx) ?? new Map<string, boolean>()+ checked.set(tx, answers)const key = memberId(state.workspaceId, userId)const known = answers.get(key)if (known !== undefined) return known@@ -45,8 +44,8 @@ export async function hasWorkspaceAccess(tx: Transaction, state: Pick<SuperTabSt/** The one Workspace check of every Swarm read and command (design, Audience): the person must still be in the Swarm's Workspace.* Reading the Swarm needs its own membership as well, which the caller checks as before. */-export async function requireWorkspaceAccess(tx: Transaction, state: Pick<SuperTabState, 'workspaceId'>, userId: string, request?: object): Promise<void> {- if (await hasWorkspaceAccess(tx, state, userId, request)) return+export async function requireWorkspaceAccess(tx: Transaction, state: Pick<SuperTabState, 'workspaceId'>, userId: string): Promise<void> {+ if (await hasWorkspaceAccess(tx, state, userId)) returnfail(WORKSPACE_ACCESS_REVOKED, state.workspaceId ? `You are no longer in this Swarm's ${WORKSPACE_LABEL}.` : `This Swarm has no ${WORKSPACE_LABEL} yet.`, 403)}