AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
fix(agents): agent runs the server turns away no longer show up as system failures (#16452)
Hazem Adelf3c4a6e82e12
diff --git a/packages/core/utils/package.json b/packages/core/utils/package.jsonindex 03daeca95b6..9564c0de3c3 100644--- a/packages/core/utils/package.json+++ b/packages/core/utils/package.json@@ -1,6 +1,6 @@{"name": "@activepieces/core-utils",- "version": "0.10.2",+ "version": "0.10.3","type": "commonjs","main": "./dist/src/index.js","scripts": {diff --git a/packages/core/utils/src/lib/activepieces-error.ts b/packages/core/utils/src/lib/activepieces-error.tsindex fff7791bb7d..6b16d161a93 100644--- a/packages/core/utils/src/lib/activepieces-error.ts+++ b/packages/core/utils/src/lib/activepieces-error.ts@@ -51,6 +51,7 @@ export type ApErrorParams =| TriggerFailedErrorParams| ValidationErrorParams| AgentRunSupersededErrorParams+ | AgentRunBusyErrorParams| RequiredActionsMissingErrorParams| FileTooLargeErrorParams| InvitationOnlySignUpParams@@ -336,6 +337,13 @@ ErrorCode.AGENT_RUN_SUPERSEDED,}>+export type AgentRunBusyErrorParams = BaseErrorParams<+ErrorCode.AGENT_RUN_BUSY,+{+ message: string+}+>+export type RequiredActionsMissingErrorParams = BaseErrorParams<ErrorCode.REQUIRED_ACTIONS_MISSING,{@@ -623,6 +631,7 @@ export enum ErrorCode {USER_NOT_FOUND_ON_PLATFORM = 'USER_NOT_FOUND_ON_PLATFORM',VALIDATION = 'VALIDATION',AGENT_RUN_SUPERSEDED = 'AGENT_RUN_SUPERSEDED',+ AGENT_RUN_BUSY = 'AGENT_RUN_BUSY',REQUIRED_ACTIONS_MISSING = 'REQUIRED_ACTIONS_MISSING',FILE_TOO_LARGE = 'FILE_TOO_LARGE',INVALID_LICENSE_KEY = 'INVALID_LICENSE_KEY',diff --git a/packages/server/api/src/app/ee/agent/rpc/agent-config-rpc.ts b/packages/server/api/src/app/ee/agent/rpc/agent-config-rpc.tsindex dc08a74e521..7732a72d007 100644--- a/packages/server/api/src/app/ee/agent/rpc/agent-config-rpc.ts+++ b/packages/server/api/src/app/ee/agent/rpc/agent-config-rpc.ts@@ -126,7 +126,7 @@ export const agentConfigRpc = (log: FastifyBaseLogger) => ({if (lock === 'busy') {log.warn({ conversation: { id: conversationId } }, '[agentRpc#getAgentConfig] Concurrent run rejected (conversation already STREAMING)')throw new ActivepiecesError({- code: ErrorCode.VALIDATION,+ code: ErrorCode.AGENT_RUN_BUSY,params: { message: 'An agent is already running for this conversation' },})}diff --git a/packages/server/api/src/app/ee/agent/rpc/rpc-shared.ts b/packages/server/api/src/app/ee/agent/rpc/rpc-shared.tsindex 7081d0c2cb6..904da56b1ee 100644--- a/packages/server/api/src/app/ee/agent/rpc/rpc-shared.ts+++ b/packages/server/api/src/app/ee/agent/rpc/rpc-shared.ts@@ -87,26 +87,13 @@ export async function loadOrStartConversation({ conversationId, platformId, userif (source !== AgentRunSource.FLOW_STEP) {return agentHelpers.getConversationOrThrow({ id: conversationId, platformId, userId })}- const existing = await agentHelpers.conversationRepo().findOneBy({ id: conversationId })- if (!isNil(existing)) {- if (existing.platformId !== platformId || existing.userId !== userId || existing.source !== AgentRunSource.FLOW_STEP) {- throw new ActivepiecesError({ code: ErrorCode.AUTHORIZATION, params: { message: 'That conversation belongs to someone else' } })- }- return existing+ const conversation = await agentHelpers.conversationRepo().findOneBy({ id: conversationId })+ ?? await insertFlowStepConversation({ conversationId, platformId, userId, projectId, modelName, agentId, flowRunId })+ const ownedByThisRun = conversation.platformId === platformId && conversation.userId === userId && conversation.source === AgentRunSource.FLOW_STEP+ if (!ownedByThisRun) {+ throw new ActivepiecesError({ code: ErrorCode.AUTHORIZATION, params: { message: 'That conversation belongs to someone else' } })}- return agentHelpers.conversationRepo().save({- id: conversationId,- platformId,- projectId: projectId ?? null,- userId,- source: AgentRunSource.FLOW_STEP,- agentId: agentId ?? null,- flowRunId: flowRunId ?? null,- title: null,- modelName: modelName ?? null,- messages: [],- status: AgentConversationStatus.IDLE,- })+ return conversation}export async function confinedRunFor({ conversationId }: { conversationId?: string }): Promise<ConfinedRun> {@@ -296,6 +283,31 @@ export function recordAgentFlowToolUse({ run, conversationId, flow, tool, outcom})}+async function insertFlowStepConversation({ conversationId, platformId, userId, projectId, modelName, agentId, flowRunId }: {+ conversationId: string+ platformId: string+ userId: string+ projectId?: string | null+ modelName?: string | null+ agentId?: string+ flowRunId?: string+}): Promise<AgentConversation> {+ await agentHelpers.conversationRepo().createQueryBuilder().insert().values({+ id: conversationId,+ platformId,+ projectId: projectId ?? null,+ userId,+ source: AgentRunSource.FLOW_STEP,+ agentId: agentId ?? null,+ flowRunId: flowRunId ?? null,+ title: null,+ modelName: modelName ?? null,+ messages: [],+ status: AgentConversationStatus.IDLE,+ }).orIgnore().execute()+ return agentHelpers.conversationRepo().findOneByOrFail({ id: conversationId })+}+function turnReadKey({ conversationId, runId }: { conversationId: string, runId: string }): string {return `agent-turn-read:${conversationId}:${runId}`}diff --git a/packages/server/api/test/unit/app/ee/agent/agent-rpc-handlers.test.ts b/packages/server/api/test/unit/app/ee/agent/agent-rpc-handlers.test.tsindex a9064a83614..3b2168a7733 100644--- a/packages/server/api/test/unit/app/ee/agent/agent-rpc-handlers.test.ts+++ b/packages/server/api/test/unit/app/ee/agent/agent-rpc-handlers.test.ts@@ -14,8 +14,9 @@ vi.mock('../../../../../src/app/waitpoints/resume-service', () => ({resumeService: () => ({ resumeFromWaitpoint: mockResumeFromWaitpoint }),}))-const { mockSet, mockSetParameters, mockWhere, mockAndWhere, mockExecute, mockFindOneBy, mockFindOne, mockSave, mockTrack, mockSendConversationUpdate } = vi.hoisted(() => ({+const { mockSet, mockSetParameters, mockWhere, mockAndWhere, mockExecute, mockFindOneBy, mockFindOne, mockSave, mockInsertValues, mockTrack, mockSendConversationUpdate } = vi.hoisted(() => ({mockSave: vi.fn(),+ mockInsertValues: vi.fn(),mockSet: vi.fn(),mockSetParameters: vi.fn(),mockWhere: vi.fn(),@@ -107,6 +108,9 @@ type QueryBuilderMock = {where: (sql: string, params: unknown) => QueryBuilderMockandWhere: (sql: string, params: unknown) => QueryBuilderMockreturning: (columns: string) => QueryBuilderMock+ insert: () => QueryBuilderMock+ values: (values: unknown) => QueryBuilderMock+ orIgnore: () => QueryBuilderMockexecute: () => Promise<{ raw?: unknown[] }>}@@ -118,6 +122,7 @@ vi.mock('../../../../../src/app/ee/agent/agent-helpers', () => ({resolveEmbeddingModel: () => ({ model: {}, providerOptions: {} }),conversationRepo: () => ({findOneBy: mockFindOneBy,+ findOneByOrFail: mockFindOneBy,findOne: mockFindOne,save: mockSave,createQueryBuilder: (): QueryBuilderMock => {@@ -128,6 +133,9 @@ vi.mock('../../../../../src/app/ee/agent/agent-helpers', () => ({where: (_sql, params) => { mockWhere(params); return builder },andWhere: (_sql, params) => { mockAndWhere(params); return builder },returning: () => builder,+ insert: () => builder,+ values: (values) => { mockInsertValues(values); return builder },+ orIgnore: () => builder,execute: mockExecute,}return builder@@ -359,13 +367,12 @@ async function callGetAgentConfigFor(input: Record<string, unknown>): Promise<undescribe('agentRpcHandlers.getAgentConfig — a flow-step run creates its conversation on first use', () => {beforeEach(() => {- mockSave.mockClear()+ mockInsertValues.mockClear()mockFindOneBy.mockReset()})it('creates the row with the owner and project the job carried', async () => {- mockFindOneBy.mockResolvedValue(null)- mockSave.mockResolvedValue({ id: 'conv-1', source: 'FLOW_STEP', projectId: 'proj-1', messages: [] })+ mockFindOneBy.mockResolvedValueOnce(null).mockResolvedValue({ id: 'conv-1', source: 'FLOW_STEP', projectId: 'proj-1', platformId: 'plat-1', userId: 'owner-1', messages: [] })await callGetAgentConfigFor({conversationId: 'conv-1', platformId: 'plat-1', userId: 'owner-1',@@ -373,7 +380,7 @@ describe('agentRpcHandlers.getAgentConfig — a flow-step run creates its conversource: 'FLOW_STEP', projectId: 'proj-1',}).catch(() => undefined)- expect(mockSave).toHaveBeenCalledWith(expect.objectContaining({+ expect(mockInsertValues).toHaveBeenCalledWith(expect.objectContaining({id: 'conv-1',source: 'FLOW_STEP',projectId: 'proj-1',@@ -390,7 +397,7 @@ describe('agentRpcHandlers.getAgentConfig — a flow-step run creates its conversource: 'FLOW_STEP', projectId: 'proj-1',}).catch(() => undefined)- expect(mockSave).not.toHaveBeenCalled()+ expect(mockInsertValues).not.toHaveBeenCalled()})})diff --git a/packages/server/worker/src/lib/execute/jobs/ee/agent/run-agent-turn.ts b/packages/server/worker/src/lib/execute/jobs/ee/agent/run-agent-turn.tsindex 8f39edc55eb..3255ef47c10 100644--- a/packages/server/worker/src/lib/execute/jobs/ee/agent/run-agent-turn.ts+++ b/packages/server/worker/src/lib/execute/jobs/ee/agent/run-agent-turn.ts@@ -18,6 +18,7 @@ const CONTENT_CHUNK_TYPES = new Set(['text-delta', 'reasoning-delta', 'tool-inpuconst USER_FAULT_STATUS_CODES = new Set([401, 403, 404])const MODEL_UNAVAILABLE_PATTERNS = [/\bis deprecated\b/i, /no longer (available|supported)/i, /\bmodel_not_found\b/i, /\bunknown model\b/i, /\bdecommissioned\b/i]const USER_CONFIG_ENTITY_TYPES = new Set<string>(Object.values(AI_PROVIDER_ENTITY_TYPES))+const USER_FACING_ERROR_CODES = new Set<string>([ErrorCode.VALIDATION, ErrorCode.CHAT_CONTEXT_LIMIT_EXCEEDED])const CONTINUE_NUDGE = '[system note — not from the user] Your previous response was cut off by the output token limit before it finished. Continue exactly where you stopped. If a tool call was cut off, re-issue it in FULL. Do not repeat content you already produced.'const FINAL_STEP_NOTE = '[system note — not from the user] This is the last step of this run. Finish now with what you have: say what you finished and what is still left to do, and offer to carry on. Do not mention steps, tools or this note.'const EMPTY_OUTPUT_NUDGE = '[system note — not from the user] Your previous step produced no visible reply to the user. Continue the task now: either call the next tool, or write your reply to the user. Do not stop silently.'@@ -506,9 +507,9 @@ export function classifyAgentRunError({ error, provider }: { error: unknown, proreturn 'credit'}if (isNil(apiError)) {- return apError?.code === ErrorCode.ENTITY_NOT_FOUND && USER_CONFIG_ENTITY_TYPES.has(apError.entityType ?? '')- ? 'user'- : 'internal'+ const missingUserConfig = apError?.code === ErrorCode.ENTITY_NOT_FOUND && USER_CONFIG_ENTITY_TYPES.has(apError.entityType ?? '')+ const rejectedWithReason = USER_FACING_ERROR_CODES.has(apError?.code ?? '')+ return missingUserConfig || rejectedWithReason ? 'user' : 'internal'}if (apiError.statusCode === 400 && provider !== AIProviderName.ACTIVEPIECES && MODEL_UNAVAILABLE_PATTERNS.some((pattern) => pattern.test(message))) {return 'user'diff --git a/packages/server/worker/test/lib/execute/jobs/ee/agent/execute-agent-run-config-failure.test.ts b/packages/server/worker/test/lib/execute/jobs/ee/agent/execute-agent-run-config-failure.test.tsindex 25702cb0cb0..3614e58af7b 100644--- a/packages/server/worker/test/lib/execute/jobs/ee/agent/execute-agent-run-config-failure.test.ts+++ b/packages/server/worker/test/lib/execute/jobs/ee/agent/execute-agent-run-config-failure.test.ts@@ -114,6 +114,20 @@ describe('executeAgentRunJob — a config failure must not swallow the turn', ()expect(events.map((event) => event.type)).toEqual([AgentEventType.ERROR, AgentEventType.FINISHED])})+ for (const code of [ErrorCode.VALIDATION, ErrorCode.CHAT_CONTEXT_LIMIT_EXCEEDED]) {+ it(`completes as a user failure when the server rejects the turn with ${code}, so on-call is not paged`, async () => {+ const reason = 'No model in tier "Smart" can run'+ const { ctx, resumed } = buildContext(Object.assign(new Error(`RPC [getAgentConfig] handler threw: ${code}`), {+ apError: { code, message: reason },+ }))++ const result = await executeAgentRunJob.execute(ctx, buildJobData({ source: AgentRunSource.FLOW_STEP, flowRunId: 'flow-run-1', waitpointId: 'waitpoint-1' }))++ expect(result.status).toBe(EngineResponseStatus.USER_FAILURE)+ expect(resumed[0].output).toMatchObject({ failure: reason })+ })+ }+it('still fails the job on an unrecognised error, so our own bugs are not laundered', async () => {const { ctx, events } = buildContext(new Error('Cannot read properties of undefined'))diff --git a/packages/server/worker/test/lib/execute/jobs/ee/agent/run-agent-turn-guards.test.ts b/packages/server/worker/test/lib/execute/jobs/ee/agent/run-agent-turn-guards.test.tsindex 5d20ff995fb..5423c41509a 100644--- a/packages/server/worker/test/lib/execute/jobs/ee/agent/run-agent-turn-guards.test.ts+++ b/packages/server/worker/test/lib/execute/jobs/ee/agent/run-agent-turn-guards.test.ts@@ -168,7 +168,12 @@ describe('classifyAgentRunError', () => {})it('keeps a conversation stuck mid-stream visible instead of completing quietly', () => {- expect(classify(new ActivepiecesError({ code: ErrorCode.VALIDATION, params: { message: 'An agent is already running for this conversation' } }))).toBe('internal')+ expect(classify(new ActivepiecesError({ code: ErrorCode.AGENT_RUN_BUSY, params: { message: 'An agent is already running for this conversation' } }))).toBe('internal')+ })++ it('treats a rejection the server explains to the user as theirs, not ours', () => {+ expect(classify(new ActivepiecesError({ code: ErrorCode.VALIDATION, params: { message: 'No model in tier "Smart" can run' } }))).toBe('user')+ expect(classify(new ActivepiecesError({ code: ErrorCode.CHAT_CONTEXT_LIMIT_EXCEEDED, params: {} }))).toBe('user')})it('reads the error code an RPC failure now carries across the boundary', () => {