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.

Agent Messages: a removed member's earlier invitation goes with them (#1860)

0xken15c64158da1e1b
diff --git a/backend/src/lib/agentMessageShares.test.ts b/backend/src/lib/agentMessageShares.test.ts
index 3c34ef627..4bc62ac4d 100644
--- a/backend/src/lib/agentMessageShares.test.ts
+++ b/backend/src/lib/agentMessageShares.test.ts
@@ -303,7 +303,52 @@ describe('agent message shares', () => {
expect(await removeMember('bob', S, 'bob')).toBe(true)
expect(p.agentMessageMember.updateMany).toHaveBeenCalledWith({ where: { shareId: S, userId: 'bob', removedAt: null }, data: { removedAt: expect.any(Date) } })
expect(p.agentMessageShare.findUnique).toHaveBeenCalledWith({ where: { id: S }, select: { ownerUserId: true } })
- expect(p.agentMessageInvite.updateMany).toHaveBeenCalledWith({ where: { shareId: S, acceptedBy: 'bob', revokedAt: null }, data: { revokedAt: expect.any(Date) } })
+ expect(p.agentMessageInvite.updateMany).toHaveBeenCalledWith({ where: { shareId: S, revokedAt: null, OR: [{ acceptedBy: 'bob' }] }, data: { revokedAt: expect.any(Date) } })
+ })
+
+ // #1376: accept, the owner invites again, the owner removes bob: the invitation the re-invite reset to waiting brought
+ // bob back with no new invitation, and counted toward the tab's 8 twice.
+ it('a removed member cannot accept an earlier re-invite', async () => {
+ const invites: Record<string, unknown>[] = [], members: Record<string, unknown>[] = []
+ const matches = (row: Record<string, unknown>, where: Record<string, unknown>): boolean => Object.entries(where).every(([key, want]) => {
+ if (key === 'OR') return (want as Record<string, unknown>[]).some(w => matches(row, w))
+ if (want && typeof want === 'object' && 'gt' in want) return (row[key] as Date) > (want as { gt: Date }).gt
+ return (row[key] ?? null) === want
+ })
+ p.agentMessageShare.findFirst.mockResolvedValue({ id: S, ownerMachineId: 'm1' })
+ p.agentMessageShare.findUnique.mockResolvedValue({ ownerUserId: 'alice' })
+ p.agentMessageInvite.count.mockImplementation(async ({ where }) => invites.filter(r => matches(r, where)).length)
+ p.agentMessageMember.count.mockImplementation(async ({ where }) => members.filter(r => matches(r, where)).length)
+ p.agentMessageInvite.findFirst.mockImplementation(async ({ where }) => invites.find(r => matches(r, where)) ?? null)
+ p.agentMessageMember.findFirst.mockImplementation(async ({ where }) => members.find(r => matches(r, where)) ?? null)
+ p.agentMessageInvite.upsert.mockImplementation(async ({ where, create, update }) => {
+ const found = invites.find(r => r.shareId === where.shareId_email.shareId && r.email === where.shareId_email.email)
+ if (found) return Object.assign(found, update)
+ invites.push({ ...create }); return create
+ })
+ p.agentMessageInvite.update.mockImplementation(async ({ where, data }) => Object.assign(invites.find(r => r.id === where.id)!, data))
+ p.agentMessageMember.upsert.mockImplementation(async ({ where, create, update }) => {
+ const found = members.find(r => r.id === where.id)
+ if (found) return Object.assign(found, update)
+ members.push({ ...create }); return create
+ })
+ const updateMany = (rows: Record<string, unknown>[]) => async ({ where, data }: { where: Record<string, unknown>; data: Record<string, unknown> }) => {
+ const hit = rows.filter(r => matches(r, where)); hit.forEach(r => Object.assign(r, data)); return { count: hit.length }
+ }
+ p.agentMessageInvite.updateMany.mockImplementation(updateMany(invites))
+ p.agentMessageMember.updateMany.mockImplementation(updateMany(members))
+ const bob = { sub: 'bob', email: '[email protected]', autonomousEnv: 'prod' }
+ const first = await inv() as { id: string }
+ expect(await accept(bob, first.id, now)).toEqual({ sharedTabId: S })
+ // A member already: the owner's second invitation is refused, so it neither waits nor counts toward the 8.
+ expect(await inv()).toEqual({ error: 'ALREADY_MEMBER' })
+ expect(await removeMember('alice', S, 'bob')).toBe(true)
+ expect(await accept(bob, first.id, now)).toEqual({ error: 'NOT_FOUND' })
+ // An invitation still waiting for that address goes with the member too.
+ invites.push({ id: 'i2', shareId: S, email: '[email protected]', autonomousEnv: 'prod', expiresAt: new Date('2030-01-01T00:00:00Z'), acceptedAt: null, acceptedBy: null, revokedAt: null })
+ members[0]!.removedAt = null
+ expect(await removeMember('alice', S, 'bob')).toBe(true)
+ expect(await accept(bob, 'i2', now)).toEqual({ error: 'NOT_FOUND' })
})
it('reports no removal for a missing share or member', async () => {
diff --git a/backend/src/lib/agentMessageShares.ts b/backend/src/lib/agentMessageShares.ts
index 048c86abb..8241c73ed 100644
--- a/backend/src/lib/agentMessageShares.ts
+++ b/backend/src/lib/agentMessageShares.ts
@@ -83,11 +83,14 @@ export async function putRoster(input: { ownerUserId: string; tabId: string; mac
}
export async function invite(input: { ownerUserId: string; ownerEmail: string; sharedTabId: string; email: string;
- expiresAt: Date; autonomousEnv: string }): Promise<{ id: string } | { error: 'FORBIDDEN' | 'SELF_INVITE' | 'TAB_FULL' }> {
+ expiresAt: Date; autonomousEnv: string }): Promise<{ id: string } | { error: 'FORBIDDEN' | 'SELF_INVITE' | 'TAB_FULL' | 'ALREADY_MEMBER' }> {
const email = input.email.trim().toLowerCase()
if (email === input.ownerEmail.trim().toLowerCase()) return { error: 'SELF_INVITE' as const }
const share = await prisma.agentMessageShare.findFirst({ where: { id: input.sharedTabId, ownerUserId: input.ownerUserId, stoppedAt: null } })
if (!share) return { error: 'FORBIDDEN' as const }
+ // Inviting a member again would reset their accepted invitation to waiting, which outlived their removal and counted
+ // toward the 8 twice (#1376).
+ if (await prisma.agentMessageMember.findFirst({ where: { shareId: share.id, email, removedAt: null }, select: { id: true } })) return { error: 'ALREADY_MEMBER' as const }
const [members, pending] = await Promise.all([
prisma.agentMessageMember.count({ where: { shareId: share.id, removedAt: null } }),
prisma.agentMessageInvite.count({ where: { shareId: share.id, acceptedAt: null, revokedAt: null, expiresAt: { gt: new Date() } } }),
@@ -153,9 +156,12 @@ export async function accept(user: { sub: string; email: string; autonomousEnv:
export async function removeMember(actorUserId: string, shareId: string, userId: string): Promise<boolean> {
const share = await prisma.agentMessageShare.findUnique({ where: { id: shareId }, select: { ownerUserId: true } })
if (!share || (actorUserId !== share.ownerUserId && actorUserId !== userId)) return false
+ const member = await prisma.agentMessageMember.findFirst({ where: { shareId, userId, removedAt: null }, select: { email: true } })
const { count } = await prisma.agentMessageMember.updateMany({ where: { shareId, userId, removedAt: null }, data: { removedAt: new Date() } })
- // The accepted invitation goes with the member; only the owner's re-invite brings them back.
- await prisma.agentMessageInvite.updateMany({ where: { shareId, acceptedBy: userId, revokedAt: null }, data: { revokedAt: new Date() } })
+ // The accepted invitation goes with the member, and so does one still waiting for their address (#1376): only the
+ // owner's next invitation brings them back.
+ await prisma.agentMessageInvite.updateMany({ where: { shareId, revokedAt: null,
+ OR: [{ acceptedBy: userId }, ...member ? [{ email: member.email, acceptedAt: null }] : []] }, data: { revokedAt: new Date() } })
return count > 0
}
diff --git a/backend/src/routes/agentMessages.test.ts b/backend/src/routes/agentMessages.test.ts
index c72041a91..13e26ff47 100644
--- a/backend/src/routes/agentMessages.test.ts
+++ b/backend/src/routes/agentMessages.test.ts
@@ -369,7 +369,7 @@ describe('Agent Messages per tab', () => {
expect((await call('POST', `/api/agent-messages/shares/${SID}/invites`, { email: '[email protected]', days: 7 })).statusCode).toBe(200)
expectTold(['owner'])
})
- it.each([['SELF_INVITE', 400], ['FORBIDDEN', 403], ['TAB_FULL', 409]])('maps an invitation %s to %i', async (error, status) => {
+ it.each([['SELF_INVITE', 400], ['FORBIDDEN', 403], ['TAB_FULL', 409], ['ALREADY_MEMBER', 409]])('maps an invitation %s to %i', async (error, status) => {
mocks.shares.invite.mockResolvedValue({ error })
const r = await call('POST', `/api/agent-messages/shares/${SID}/invites`, { email: '[email protected]', days: 7 })
expect(r.statusCode).toBe(status)
diff --git a/backend/src/routes/agentMessages.ts b/backend/src/routes/agentMessages.ts
index 2500c6528..6aa358932 100644
--- a/backend/src/routes/agentMessages.ts
+++ b/backend/src/routes/agentMessages.ts
@@ -12,11 +12,11 @@ const sharedId = z.string().regex(/^[a-f0-9]{24}$/)
const DAY = 86_400_000
const STATUS: Record<string, number> = { FORBIDDEN: 403, STALE_ROSTER: 409, ROSTER_TOO_LARGE: 413, SELF_INVITE: 400,
- TAB_FULL: 409, NOT_FOUND: 404, EXPIRED: 410 }
+ TAB_FULL: 409, ALREADY_MEMBER: 409, NOT_FOUND: 404, EXPIRED: 410 }
const MESSAGE: Record<string, string> = {
FORBIDDEN: 'Only the tab\'s owner, from the computer that shares it, can do that.',
STALE_ROSTER: 'A newer roster is already shared.', ROSTER_TOO_LARGE: 'The roster is too large.',
- SELF_INVITE: 'You are already in this tab.', TAB_FULL: 'This tab has 8 people already.',
+ SELF_INVITE: 'You are already in this tab.', TAB_FULL: 'This tab has 8 people already.', ALREADY_MEMBER: 'They are already in this tab.',
NOT_FOUND: 'This invitation is no longer available.', EXPIRED: 'This invitation expired.',
}
/** A move's refusals mean something else than the same codes elsewhere: they are told in words of their own. */