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): batch Swarm message and delivery inserts (#2010)

0xken15d72944ef5f66
diff --git a/backend/integration/superTabs.test.ts b/backend/integration/superTabs.test.ts
index 1c2d1c1d3..bacf0e229 100644
--- a/backend/integration/superTabs.test.ts
+++ b/backend/integration/superTabs.test.ts
@@ -30,6 +30,7 @@ import { beforeFirstWrite } from './interleave.test-support.js'
// A fresh loopback replica set, created here. This suite never reads DATABASE_URL or an existing database.
let replica: MongoMemoryReplSet
let db: PrismaClient
+let recordQuery = (_query: string) => {}
let store: SuperTabStore
let app: FastifyInstance
const owner: AuthUser = { sub: '100000000000000000000001', email: '[email protected]', autonomousEnv: 'prod', role: 'user' }
@@ -100,7 +101,9 @@ beforeAll(async () => {
await promisify(execFile)(join(process.cwd(), 'node_modules/.bin/prisma'), ['db', 'push', '--skip-generate'], {
env: { ...process.env, DATABASE_URL: uri }, timeout: 60000,
})
- db = new PrismaClient({ datasources: { db: { url: uri } } })
+ const client = new PrismaClient({ datasources: { db: { url: uri } }, log: [{ emit: 'event', level: 'query' }] })
+ client.$on('query', event => recordQuery(event.query))
+ db = client
store = new SuperTabStore(db)
for (const user of [owner, peer, host]) await db.user.create({ data: { id: user.sub, externalId: `fixture-${user.sub}`,
email: user.email, autonomousEnv: user.autonomousEnv } })
@@ -1525,6 +1528,32 @@ describe('Super Tabs on a real Mongo replica set', () => {
}
} finally { await db.swarmWorkspaceMember.update({ where: { id: `${row.workspaceId}:${owner.sub}` }, data: { removedAt: null } }) }
})
+ it('writes a message to several recipients in two insert commands, preserving each delivery', async () => {
+ const { tab, lead } = await fixture()
+ const recipients = [lead]
+ for (const name of ['Parser', 'Renderer', 'Exporter']) recipients.push(String((await run(tab, {
+ type: 'agent.add', conversationId: 'lead', machineId: 'test-host', harnessId: randomUUID(), name, configuration: config, lead: false,
+ })).result.agentId))
+ const queries: string[] = []
+ recordQuery = query => { if (/SuperTab(Message|Delivery).*insert/i.test(query)) queries.push(query) }
+ let sent: Awaited<ReturnType<typeof run>>
+ try {
+ sent = await run(tab, { ...message('Batch to all four.'), recipientIds: recipients } as Command)
+ } finally { recordQuery = () => {} }
+ const messageId = String(sent.result.messageId)
+ const stored = await db.superTabMessage.findUniqueOrThrow({ where: { id: messageId } })
+ expect(stored).toMatchObject({ tabId: tab, conversationId: 'lead', text: 'Batch to all four.', recipientIds: recipients,
+ author: { kind: 'person', id: owner.sub }, replyTo: null, mentions: [] })
+ const deliveries = await db.superTabDelivery.findMany({ where: { tabId: tab, messageId } })
+ expect(deliveries).toHaveLength(4)
+ for (const recipient of recipients) expect(deliveries.find(row => row.agentId === recipient)).toMatchObject({
+ id: stableId(messageId, recipient), tabId: tab, conversationId: 'lead', messageId, machineId: 'test-host',
+ machineOwnerId: host.sub, membershipGeneration: tab, authorKind: 'person', authorId: owner.sub,
+ authorGeneration: tab, assignmentId: null, status: 'queued',
+ })
+ expect(queries).toHaveLength(2)
+ })
+
it('closes the replying agent\'s own deliveries of a message as answered, and nobody else\'s', async () => {
const { tab, lead } = await fixture()
const helper = (await run(tab, { type: 'agent.add', conversationId: 'lead', machineId: 'test-host', harnessId: randomUUID(),
diff --git a/backend/src/superTabs/store.ts b/backend/src/superTabs/store.ts
index b968c9db0..e4b345fb6 100644
--- a/backend/src/superTabs/store.ts
+++ b/backend/src/superTabs/store.ts
@@ -1624,23 +1624,27 @@ export class SuperTabStore {
/** Records committed messages, each at the sequence its conversation gave it, and one delivery per recipient. */
private async writeMessages(tx: Transaction, tabId: string, state: SuperTabState, messages: MessageDraft[], sequences: ReadonlyMap<string, number>) {
+ const rows: Prisma.SuperTabMessageCreateManyInput[] = []
+ const deliveries: Prisma.SuperTabDeliveryCreateManyInput[] = []
for (const message of messages) {
- await tx.superTabMessage.create({ data: { id: message.id, tabId, conversationId: message.conversationId,
+ rows.push({ id: message.id, tabId, conversationId: message.conversationId,
sequence: sequences.get(message.id)!, author: json(message.author), ...(message.source ? { source: json(message.source) } : {}),
...(message.attachments ? { attachments: json(message.attachments) } : {}),
- text: message.text, replyTo: message.replyTo, recipientIds: message.recipients, mentions: message.mentions ?? [] } })
+ text: message.text, replyTo: message.replyTo, recipientIds: message.recipients, mentions: message.mentions ?? [] })
for (const recipient of message.recipients) {
const target = agent(state, recipient)
const group = conversation(state, message.conversationId)
- await tx.superTabDelivery.create({ data: { id: stableId(message.id, recipient), tabId,
+ deliveries.push({ id: stableId(message.id, recipient), tabId,
conversationId: message.conversationId, messageId: message.id, agentId: recipient, machineId: target.machineId,
machineOwnerId: target.machineOwnerId,
assignmentId: activeWork(group) && group.assignment?.agentId === recipient ? group.assignment.id : null,
membershipGeneration: target.membershipGeneration, authorKind: message.author.kind,
- authorId: message.author.id, authorGeneration: message.author.membershipGeneration } })
- this.announce(tx, { machineId: target.machineId, tabId, agentId: recipient })
+ authorId: message.author.id, authorGeneration: message.author.membershipGeneration })
}
}
+ if (rows.length) await tx.superTabMessage.createMany({ data: rows })
+ if (deliveries.length) await tx.superTabDelivery.createMany({ data: deliveries })
+ for (const delivery of deliveries) this.announce(tx, { machineId: delivery.machineId, tabId, agentId: delivery.agentId })
}
/** Once `tx` commits, never for an attempt that failed or was retried: told earlier, a host could read before the delivery
diff --git a/backend/src/superTabs/transaction.test.ts b/backend/src/superTabs/transaction.test.ts
index 2b8850aff..bcad3d9a1 100644
--- a/backend/src/superTabs/transaction.test.ts
+++ b/backend/src/superTabs/transaction.test.ts
@@ -62,6 +62,12 @@ describe('Swarm transactions', () => {
await expect(transaction(other as never, async () => 1, async () => {})).rejects.toThrow('boom')
expect(other.$transaction).toHaveBeenCalledTimes(1)
})
+
+ it('retries a transient unique key conflict and returns the successful attempt', async () => {
+ const db = flaky(Object.assign(new Error('Unique constraint failed'), { code: 'P2002' }))
+ expect(await transaction(db as never, async () => 'committed', async () => {})).toBe('committed')
+ expect(db.$transaction).toHaveBeenCalledTimes(2)
+ })
})
/** A database whose commit fails with each of `failures` in turn, after the body has run, as a write conflict at commit does. */