From 0aee005b1faaa964ac77d195fedd0dbb2ec1a3c0 Mon Sep 17 00:00:00 2001 From: Dotta Date: Wed, 8 Jul 2026 01:46:34 +0000 Subject: [PATCH] Reject stale built-in approval setup retries --- server/src/__tests__/built-in-agents.test.ts | 11 +++++++++++ server/src/services/built-in-agents.ts | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/server/src/__tests__/built-in-agents.test.ts b/server/src/__tests__/built-in-agents.test.ts index 273830d0882..b1d6fc742e2 100644 --- a/server/src/__tests__/built-in-agents.test.ts +++ b/server/src/__tests__/built-in-agents.test.ts @@ -197,6 +197,17 @@ describeEmbeddedPostgres("built-in agents", () => { }, }); + await expect(builtIns.provision(companyId, "briefs", { + budgetMonthlyCents: 7500, + })).rejects.toMatchObject({ + status: 409, + details: { + code: "built_in_agent_pending_approval", + key: "briefs", + agentId: result.state.agentId, + }, + }); + await db .update(agents) .set({ diff --git a/server/src/services/built-in-agents.ts b/server/src/services/built-in-agents.ts index d86fefdb422..8ce039fd948 100644 --- a/server/src/services/built-in-agents.ts +++ b/server/src/services/built-in-agents.ts @@ -239,6 +239,10 @@ function builtInAgentNotConfiguredError(state: BuiltInAgentState) { }); } +function hasProvisionSetupInput(input: BuiltInAgentProvisionInput) { + return input.adapterType !== undefined || input.adapterConfig !== undefined || input.budgetMonthlyCents !== undefined; +} + function rowIsBuiltInAgent(row: typeof agents.$inferSelect, key: string) { const marker = readBuiltInAgentMarker(row.metadata); return marker?.key === key; @@ -368,6 +372,13 @@ export function builtInAgentService(db: Db) { const existing = await findSingleAgent(companyId, definition); if (existing) { if (existing.status === "pending_approval") { + if (hasProvisionSetupInput(input)) { + throw conflict("Built-in agent setup is already pending board approval.", { + code: "built_in_agent_pending_approval", + key: definition.key, + agentId: existing.id, + }); + } const approval = await approvalSvc.findOpenHireApprovalForAgent(companyId, existing.id); return { state: state(definition, existing),