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.

Machine shares: read every member's device keys in one query (#1862)

0xken155560a5cff345
diff --git a/backend/src/lib/deviceKeyLog.test.ts b/backend/src/lib/deviceKeyLog.test.ts
index 77a15c00a..4510efe82 100644
--- a/backend/src/lib/deviceKeyLog.test.ts
+++ b/backend/src/lib/deviceKeyLog.test.ts
@@ -18,9 +18,11 @@ const m = vi.hoisted(() => ({
vi.mock('./prisma.js', () => ({
prisma: {
deviceKeyLogEntry: {
- findMany: vi.fn(async ({ where }: { where: { userId: string; seq?: { gt: number; lte?: number } } }) =>
- m.rows.filter(r => r.userId === where.userId && r.seq > (where.seq?.gt ?? 0) && r.seq <= (where.seq?.lte ?? Infinity))
- .sort((a, b) => a.seq - b.seq)),
+ findMany: vi.fn(async ({ where }: { where: { userId: string; seq?: { gt: number; lte?: number }; OR?: Array<{ userId: string; seq: { gte: number } }> } }) =>
+ where.OR
+ ? m.rows.filter(r => where.OR!.some(w => r.userId === w.userId && r.seq >= w.seq.gte))
+ : m.rows.filter(r => r.userId === where.userId && r.seq > (where.seq?.gt ?? 0) && r.seq <= (where.seq?.lte ?? Infinity))
+ .sort((a, b) => a.seq - b.seq)),
findFirst: vi.fn(async ({ where }: { where: { userId: string } }) =>
m.rows.filter(r => r.userId === where.userId).sort((a, b) => b.seq - a.seq)[0] ?? null),
create: vi.fn(async ({ data }: { data: Row }) => {
@@ -30,6 +32,8 @@ vi.mock('./prisma.js', () => ({
},
deviceKeyLogState: {
findUnique: vi.fn(async ({ where }: { where: { userId: string } }) => structuredClone(m.snaps.get(where.userId) ?? null)),
+ findMany: vi.fn(async ({ where }: { where: { userId: { in: string[] } } }) =>
+ where.userId.in.flatMap(id => m.snaps.has(id) ? [structuredClone(m.snaps.get(id)!)] : [])),
updateMany: vi.fn(async ({ where, data }: { where: { userId: string; seq: number | { lt: number }; hash?: string }; data: Record<string, unknown> }) => {
if (m.failSnapshot) throw m.failSnapshot
const cur = m.snaps.get(where.userId)
@@ -76,7 +80,7 @@ vi.mock('./bus.js', () => ({
hgetall: async (key: string) => m.seen[key] ?? {},
},
}))
-import { appendDeviceAck, appendDeviceKey, deviceKeysSeen, readDeviceAcks, readDeviceKeyLog, touchDeviceKey } from './deviceKeyLog.js'
+import { activeMachineKeysOf, appendDeviceAck, appendDeviceKey, deviceKeysSeen, readDeviceAcks, readDeviceKeyLog, touchDeviceKey } from './deviceKeyLog.js'
import { signDevAck, type DevAck } from './deviceAckCore.js'
import { emptyDevLogState, nextDevLogEntry, signDevLogEntry, applyDevLogEntries, DEVLOG_MAX_ACTIVE, type DevLogState } from './deviceLogCore.js'
import { prisma } from './prisma.js'
@@ -433,3 +437,46 @@ describe('acknowledgements ("It\'s mine", once for the account)', () => {
expect((await readDeviceAcks('someone-else', 0)).acks).toEqual([])
})
})
+
+// Diego's issue file (2026-10-27): a machine shared with a 200-person domain replayed each member's log on every policy read
+// or save, 400+ parallel queries. Snapshots that match the head are read for everyone at once.
+describe('the machine keys of many accounts', () => {
+ beforeEach(() => {
+ m.rows.length = 0
+ m.snaps.clear()
+ m.failSnapshot = null
+ vi.clearAllMocks()
+ m.publishChanged.mockResolvedValue(1)
+ m.publishDown.mockResolvedValue(1)
+ })
+ const member = (id: string) => ({ pub: `pub-${id}`, kind: 'machine', machineId: `${id}-mac`, label: `${id}'s Mac`, seq: 1, addedAt: 1 })
+
+ it('reads current snapshots in one query, never one account at a time', async () => {
+ const ids = ['u1', 'u2', 'u3', 'u4', 'u5']
+ for (const id of ids) {
+ m.snaps.set(id, { userId: id, seq: 1, hash: `h-${id}`, active: { [`pub-${id}`]: member(id) }, removed: [], sessions: {} })
+ m.rows.push({ userId: id, seq: 1, hash: `h-${id}`, entry: '{}', sessionId: null })
+ }
+ const keys = await activeMachineKeysOf([...ids, 'no-keys'])
+ expect(ids.map(id => keys.get(id))).toEqual(ids.map(id => [member(id)]))
+ // An account with no log has no keys, and costs no query of its own either.
+ expect(keys.get('no-keys')).toEqual([])
+ expect(prisma.deviceKeyLogState.findMany).toHaveBeenCalledTimes(1)
+ expect(prisma.deviceKeyLogEntry.findMany).toHaveBeenCalledTimes(1)
+ expect(prisma.deviceKeyLogState.findUnique).not.toHaveBeenCalled()
+ expect(prisma.deviceKeyLogEntry.findFirst).not.toHaveBeenCalled()
+ })
+
+ it('verifies the log of an account whose snapshot is behind or missing, alone', async () => {
+ await appendDeviceKey(USER, await addEntry(box1, 'machine', MID1, 'box1'), { kind: 'machine', machineId: MID1 })
+ m.snaps.set('u1', { userId: 'u1', seq: 1, hash: 'h-u1', active: { 'pub-u1': member('u1') }, removed: [], sessions: {} })
+ m.rows.push({ userId: 'u1', seq: 1, hash: 'h-u1', entry: '{}', sessionId: null })
+ m.snaps.delete(USER)
+ vi.clearAllMocks()
+ const keys = await activeMachineKeysOf(['u1', USER])
+ expect(keys.get('u1')).toEqual([member('u1')])
+ expect(keys.get(USER)).toMatchObject([{ pub: box1.pub, machineId: MID1, label: 'box1' }])
+ expect(prisma.deviceKeyLogState.findUnique).toHaveBeenCalledTimes(1)
+ expect(prisma.deviceKeyLogState.findUnique).toHaveBeenCalledWith({ where: { userId: USER } })
+ })
+})
diff --git a/backend/src/lib/deviceKeyLog.ts b/backend/src/lib/deviceKeyLog.ts
index 7d067d626..2660ac40a 100644
--- a/backend/src/lib/deviceKeyLog.ts
+++ b/backend/src/lib/deviceKeyLog.ts
@@ -115,9 +115,41 @@ async function currentState(userId: string): Promise<{ state: DevLogState; sessi
* shared with it. The newest key wins when one machine has two.
*/
export async function activeMachineKeys(userId: string): Promise<DevLogMember[]> {
- const { state } = await currentState(userId)
+ return machineKeys((await currentState(userId)).state.active)
+}
+
+/**
+ * [activeMachineKeys] of many accounts, by account: every member of a machine shared with a domain, on each read or save
+ * of its policy. One log replay each made a 200-person domain 400+ parallel queries (Diego's issue file, 2026-10-27), so the
+ * snapshots and the entries from each one on are read for all of them at once. A snapshot fits its head when the only
+ * entry from its sequence on is its own; an account with neither has no keys. Any other account is verified alone, as
+ * [currentState] does.
+ */
+export async function activeMachineKeysOf(userIds: string[]): Promise<Map<string, DevLogMember[]>> {
+ if (!userIds.length) return new Map()
+ const snaps = new Map((await prisma.deviceKeyLogState.findMany({ where: { userId: { in: userIds } } })).map(s => [s.userId, s]))
+ const rows = await prisma.deviceKeyLogEntry.findMany({ where: { OR: userIds.map(userId => ({ userId, seq: { gte: snaps.get(userId)?.seq ?? 0 } })) },
+ select: { userId: true, seq: true, hash: true } })
+ const since = new Map<string, Array<{ seq: number; hash: string }>>()
+ for (const row of rows) {
+ const list = since.get(row.userId)
+ if (list) list.push(row)
+ else since.set(row.userId, [row])
+ }
+ return new Map(await Promise.all(userIds.map(async (userId): Promise<[string, DevLogMember[]]> => {
+ const snap = snaps.get(userId), after = since.get(userId) ?? []
+ if (!snap && !after.length) return [userId, []]
+ if (snap && after.length === 1 && after[0]!.seq === snap.seq && after[0]!.hash === snap.hash) {
+ return [userId, machineKeys(snap.active as unknown as DevLogState['active'])]
+ }
+ return [userId, await activeMachineKeys(userId)]
+ })))
+}
+
+/** The active machine keys by machine; the newest key wins when one machine has two. */
+function machineKeys(active: DevLogState['active']): DevLogMember[] {
const byMachine = new Map<string, DevLogMember>()
- for (const member of Object.values(state.active)) {
+ for (const member of Object.values(active)) {
if (member.kind !== 'machine' || !member.machineId) continue
const held = byMachine.get(member.machineId)
if (!held || held.seq < member.seq) byMachine.set(member.machineId, member)
diff --git a/backend/src/lib/machineAccess.test.ts b/backend/src/lib/machineAccess.test.ts
index 561dbb5af..5fd41eba2 100644
--- a/backend/src/lib/machineAccess.test.ts
+++ b/backend/src/lib/machineAccess.test.ts
@@ -6,11 +6,11 @@ const mocks = vi.hoisted(() => ({
machine: { findMany: vi.fn() },
user: { findMany: vi.fn() },
},
- presence: vi.fn(), keys: vi.fn(),
+ presence: vi.fn(), keys: vi.fn(), keysOf: vi.fn(),
}))
vi.mock('./prisma.js', () => ({ prisma: mocks.prisma, machineAlive: { OR: [{ deletedAt: null }, { deletedAt: { isSet: false } }] } }))
vi.mock('./bus.js', () => ({ getAgentPresenceMany: mocks.presence }))
-vi.mock('./deviceKeyLog.js', () => ({ activeMachineKeys: mocks.keys }))
+vi.mock('./deviceKeyLog.js', () => ({ activeMachineKeys: mocks.keys, activeMachineKeysOf: mocks.keysOf }))
import { machinesSharedWith, shareMembers, sharedMachineAccess, sharedVia } from './machineAccess.js'
import { emailDomain, isPublicEmailDomain } from './publicEmailDomains.js'
@@ -126,6 +126,17 @@ describe('shareMembers', () => {
beforeEach(() => {
vi.resetAllMocks()
mocks.keys.mockImplementation(async (id: string) => [{ machineId: `${id}-mac`, pub: pub(id.length), label: `${id}'s Mac`, kind: 'machine', addedAt: 1, seq: 1 }])
+ mocks.keysOf.mockImplementation(async (ids: string[]) => new Map(ids.map(id => [id, [{ machineId: `${id}-mac`, pub: pub(id.length), label: `${id}'s Mac`, kind: 'machine', addedAt: 1, seq: 1 }]])))
+ })
+
+ // Diego's issue file (2026-10-27): one log replay per member made a 200-person domain 400+ parallel queries a read.
+ it('shareMembers reads device key snapshots in one query', async () => {
+ mocks.prisma.user.findMany.mockResolvedValue(['a', 'b', 'c', 'd', 'e'].map(id => ({ id, email: `${id}@acme.io`, name: null })))
+ const members = await shareMembers(share({ domain: 'acme.io' }))
+ expect(members.map(m => m.machines)).toEqual(['a', 'b', 'c', 'd', 'e'].map(id => [{ machineId: `${id}-mac`, name: `${id}'s Mac`, pub: pub(1) }]))
+ expect(mocks.keysOf).toHaveBeenCalledTimes(1)
+ expect(mocks.keysOf).toHaveBeenCalledWith(['a', 'b', 'c', 'd', 'e'])
+ expect(mocks.keys).not.toHaveBeenCalled()
})
it('names the addresses, then everyone at the domain, with their machines’ keys', async () => {
diff --git a/backend/src/lib/machineAccess.ts b/backend/src/lib/machineAccess.ts
index ed805713a..417c8db63 100644
--- a/backend/src/lib/machineAccess.ts
+++ b/backend/src/lib/machineAccess.ts
@@ -14,7 +14,7 @@ import type { Machine, MachineShare } from '@prisma/client'
import { prisma, machineAlive } from './prisma.js'
import { getAgentPresenceMany } from './bus.js'
import { machineBillingAllowsDataPlane } from './billingState.js'
-import { activeMachineKeys } from './deviceKeyLog.js'
+import { activeMachineKeys, activeMachineKeysOf } from './deviceKeyLog.js'
import { emailDomain, isPublicEmailDomain } from './publicEmailDomains.js'
import { storedAutonomousEnvironment } from './autonomousEnvironment.js'
import type { AuthUser } from './ssoAuth.js'
@@ -120,8 +120,10 @@ export async function shareMembers(share: MachineShare): Promise<ShareMember[]>
const via = sharedVia(share, user.email)
return via ? [{ user, via }] : []
})
- return Promise.all(members.map(async ({ user, via }) => ({
+ // Every member's keys in one read (deviceKeyLog.ts activeMachineKeysOf), not one log replay each.
+ const keys = await activeMachineKeysOf(members.map(({ user }) => user.id))
+ return members.map(({ user, via }) => ({
email: normalized(user.email), name: user.name ?? null, via,
- machines: (await activeMachineKeys(user.id)).map(k => ({ machineId: k.machineId, name: k.label, pub: k.pub })),
- })))
+ machines: (keys.get(user.id) ?? []).map(k => ({ machineId: k.machineId, name: k.label, pub: k.pub })),
+ }))
}