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.

Harness shares: a new id for an existing recipient is a typed 409, not a 500 (#1864)

0xken15d193981ce153
diff --git a/backend/src/routes/harnessShares.test.ts b/backend/src/routes/harnessShares.test.ts
index 46f979903..cc3045577 100644
--- a/backend/src/routes/harnessShares.test.ts
+++ b/backend/src/routes/harnessShares.test.ts
@@ -84,6 +84,25 @@ describe('account-bound harness invitations', () => {
mocks.prisma.harnessShare.findUnique.mockResolvedValueOnce({ ...input(), ownerId: owner.sub })
expect((await put()).statusCode).toBe(200)
})
+ // Diego's issue file (2026-10-27): a daemon that lost its grants file sends a new id for a recipient it already invited;
+ // the unique machine, agent and recipient index refused it as a 500, which the CLI retried forever.
+ it('PUT with a new id for an existing recipient is 409, not 500', async () => {
+ const { Prisma } = await import('@prisma/client')
+ const taken = () => new Prisma.PrismaClientKnownRequestError('Unique constraint failed', { code: 'P2002', clientVersion: 'test' })
+ mocks.prisma.harnessShare.upsert.mockRejectedValue(taken())
+ mocks.prisma.harnessShare.findFirst.mockResolvedValue({ id: 'existing-id', ownerId: owner.sub })
+ const res = await put()
+ expect(res.statusCode).toBe(409)
+ expect(res.json().error).toMatchObject({ code: 'SHARE_EXISTS', id: 'existing-id' })
+ expect(mocks.prisma.harnessShare.findFirst).toHaveBeenCalledWith({ where: { machineId: 'm1', agentId: 'agent-1', recipientEmail: '[email protected]' },
+ select: { id: true, ownerId: true } })
+ expect(mocks.changed).not.toHaveBeenCalled()
+ // A row from the machine's earlier owner is theirs: still a conflict, without its id.
+ mocks.prisma.harnessShare.findFirst.mockResolvedValue({ id: 'earlier-id', ownerId: 'earlier-owner' })
+ const earlier = await put()
+ expect(earlier.statusCode).toBe(409)
+ expect(earlier.json().error).not.toHaveProperty('id')
+ })
it('discovers only active invitations for the signed-in email and environment, without machine secrets', async () => {
mocks.auth.mockResolvedValue(ken)
mocks.prisma.harnessShare.findMany.mockResolvedValue([
diff --git a/backend/src/routes/harnessShares.ts b/backend/src/routes/harnessShares.ts
index 125c69f0f..f9ad431af 100644
--- a/backend/src/routes/harnessShares.ts
+++ b/backend/src/routes/harnessShares.ts
@@ -1,5 +1,5 @@
import type { FastifyInstance } from 'fastify'
-import type { PrismaClient } from '@prisma/client'
+import { Prisma, type PrismaClient } from '@prisma/client'
import { z } from 'zod'
import { SuperTabStore } from '../superTabs/store.js'
import { prisma, machineAlive } from '../lib/prisma.js'
@@ -111,8 +111,18 @@ export async function harnessShareRoutes(app: FastifyInstance): Promise<void> {
}
const data = { ...input, superTabId: input.superTabId ?? null, expiresAt: new Date(input.expiresAt), revokedAt: null,
ownerId: req.user!.sub, autonomousEnv: req.user!.autonomousEnv }
- await prisma.harnessShare.upsert({ where: { id: req.params.id },
- create: { id: req.params.id, ...data }, update: data })
+ try {
+ await prisma.harnessShare.upsert({ where: { id: req.params.id },
+ create: { id: req.params.id, ...data }, update: data })
+ } catch (error) {
+ if (!(error instanceof Prisma.PrismaClientKnownRequestError) || error.code !== 'P2002') throw error
+ // This machine, agent and recipient already have an invitation under another id: a daemon that lost its grants
+ // file sends a new one, and a 500 had it retry forever (Diego's issue file, 2026-10-27). It is named only to its owner.
+ const taken = await prisma.harnessShare.findFirst({ where: { machineId: input.machineId, agentId: input.agentId,
+ recipientEmail: input.recipientEmail }, select: { id: true, ownerId: true } })
+ return sendError(reply, 'This harness is already shared with that person.', 'SHARE_EXISTS', 409,
+ taken?.ownerId === req.user!.sub ? { id: taken.id } : undefined)
+ }
// Told only when what the recipient sees changes. A renewal moves only the expiry, and an owner daemon that
// renewed every few seconds (diego@'s 0.4.55 dev build, 2026-10-26) poked every member's every app to re-read
// its machines and shares three times a second, with the backend pinned at its CPU limit.