Skip to content

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

fix(chat): chat always ends its turn with a reply (#16517)

Hazem Adele63cefd5011c
diff --git a/packages/server/api/test/unit/app/ee/agent/chat-turn-charge.test.ts b/packages/server/api/test/unit/app/ee/agent/chat-turn-charge.test.ts
index a48a4770d7a..818697908f6 100644
--- a/packages/server/api/test/unit/app/ee/agent/chat-turn-charge.test.ts
+++ b/packages/server/api/test/unit/app/ee/agent/chat-turn-charge.test.ts
@@ -3,9 +3,9 @@ import { AgentConversation, AgentConversationStatus, AgentRunSource, apId, Persi
import { FastifyBaseLogger } from 'fastify'
import { beforeEach, describe, expect, it, vi } from 'vitest'
-const { trackBillingAndSendTelemetry, resolveChatProviderName } = vi.hoisted(() => ({
+const { trackBillingAndSendTelemetry, resolveConversationProviderName } = vi.hoisted(() => ({
trackBillingAndSendTelemetry: vi.fn(),
- resolveChatProviderName: vi.fn(),
+ resolveConversationProviderName: vi.fn(),
}))
vi.mock('../../../../../src/app/platform/billing-and-telemetry', () => ({
@@ -20,7 +20,7 @@ vi.mock('../../../../../src/app/ee/platform/platform-plan/platform-plan.service'
vi.mock('../../../../../src/app/ee/agent/agent-helpers', () => ({
agentHelpers: {
- resolveChatProviderName: (...args: unknown[]) => resolveChatProviderName(...args),
+ resolveConversationProviderName: (...args: unknown[]) => resolveConversationProviderName(...args),
surfaceOf: () => 'chat',
resolveModelIdForAnalytics: () => 'model',
resolveTier: () => ({ id: 'smart' }),
@@ -38,11 +38,11 @@ import { chatToolBilling } from '../../../../../src/app/ee/agent/chat-tool-billi
describe('chatToolBilling.chargeForLatestTurn with task agents', () => {
beforeEach(() => {
trackBillingAndSendTelemetry.mockReset()
- resolveChatProviderName.mockReset()
+ resolveConversationProviderName.mockReset()
})
it('charges a managed-key turn no message credit, only the paid calls its tasks made', async () => {
- resolveChatProviderName.mockResolvedValue(AIProviderName.ACTIVEPIECES)
+ resolveConversationProviderName.mockResolvedValue(AIProviderName.ACTIVEPIECES)
await chatToolBilling.chargeForLatestTurn({ conversation: conversationWithTwoTasks(), log: LOG })
@@ -52,7 +52,7 @@ describe('chatToolBilling.chargeForLatestTurn with task agents', () => {
})
it('charges an own-key turn one message credit, once, plus the paid calls its tasks made', async () => {
- resolveChatProviderName.mockResolvedValue(AIProviderName.OPENAI)
+ resolveConversationProviderName.mockResolvedValue(AIProviderName.OPENAI)
await chatToolBilling.chargeForLatestTurn({ conversation: conversationWithTwoTasks(), log: LOG })
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.ts
index 71cd4daf811..1abecc3b161 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
@@ -25,21 +25,25 @@ const EMPTY_OUTPUT_NUDGE = '[system note — not from the user] Your previous st
const FINAL_STEP_MESSAGE: ModelMessage = { role: 'user', content: FINAL_STEP_NOTE }
-export function decideLoopAction({ finishReason, producedVisibleOutput, continuations, emptyContinuations }: {
+export function decideLoopAction({ finishReason, endedWithReply, continuations, emptyContinuations }: {
finishReason: string
- producedVisibleOutput: boolean
+ endedWithReply: boolean
continuations: number
emptyContinuations: number
}): LoopDecision {
if (finishReason === 'length') {
return continuations >= MAX_AUTO_CONTINUATIONS ? 'finish' : 'continue_truncation'
}
- if (!producedVisibleOutput && emptyContinuations < MAX_EMPTY_CONTINUATIONS) {
+ if (!endedWithReply && emptyContinuations < MAX_EMPTY_CONTINUATIONS) {
return 'continue_empty'
}
return 'finish'
}
+export function stepEndsWithReply({ text, toolCallCount }: { text: string, toolCallCount: number }): boolean {
+ return text.trim().length > 0 || toolCallCount > 0
+}
+
export function shouldRetryStream({ producedVisibleOutput, streamRetries }: {
producedVisibleOutput: boolean
streamRetries: number
@@ -333,7 +337,8 @@ export async function runAgentTurn({ models, fastModel, systemPrompt, messages,
break
}
- const decision = decideLoopAction({ finishReason, producedVisibleOutput, continuations, emptyContinuations })
+ const endedWithReply = stepEndsWithReply({ text: finalStep.text, toolCallCount: finalStep.toolCalls.length })
+ const decision = decideLoopAction({ finishReason, endedWithReply, continuations, emptyContinuations })
if (decision === 'finish') {
accumulatedResponseMessages.push(...stepMessages)
diff --git a/packages/server/worker/test/lib/execute/jobs/ee/agent/execute-agent-run.test.ts b/packages/server/worker/test/lib/execute/jobs/ee/agent/execute-agent-run.test.ts
index 62faf3eb7eb..37b7f894ff4 100644
--- a/packages/server/worker/test/lib/execute/jobs/ee/agent/execute-agent-run.test.ts
+++ b/packages/server/worker/test/lib/execute/jobs/ee/agent/execute-agent-run.test.ts
@@ -1,34 +1,46 @@
import { describe, expect, it } from 'vitest'
import { UNATTENDED_WEB_TOOLS } from '../../../../../../src/lib/execute/jobs/ee/agent/agent-tool-policy'
import { stepResultFrom } from '../../../../../../src/lib/execute/jobs/ee/agent/agent-step-result'
-import { decideLoopAction, shouldRetryStream } from '../../../../../../src/lib/execute/jobs/ee/agent/run-agent-turn'
+import { decideLoopAction, shouldRetryStream, stepEndsWithReply } from '../../../../../../src/lib/execute/jobs/ee/agent/run-agent-turn'
describe('decideLoopAction', () => {
it('finishes when a normal step produced visible output', () => {
- expect(decideLoopAction({ finishReason: 'stop', producedVisibleOutput: true, continuations: 0, emptyContinuations: 0 })).toBe('finish')
- expect(decideLoopAction({ finishReason: 'tool-calls', producedVisibleOutput: true, continuations: 0, emptyContinuations: 0 })).toBe('finish')
+ expect(decideLoopAction({ finishReason: 'stop', endedWithReply: true, continuations: 0, emptyContinuations: 0 })).toBe('finish')
+ expect(decideLoopAction({ finishReason: 'tool-calls', endedWithReply: true, continuations: 0, emptyContinuations: 0 })).toBe('finish')
})
it('nudges (continue_empty) when a step produced no visible output, within the cap', () => {
- expect(decideLoopAction({ finishReason: 'stop', producedVisibleOutput: false, continuations: 0, emptyContinuations: 0 })).toBe('continue_empty')
- expect(decideLoopAction({ finishReason: 'stop', producedVisibleOutput: false, continuations: 0, emptyContinuations: 1 })).toBe('continue_empty')
+ expect(decideLoopAction({ finishReason: 'stop', endedWithReply: false, continuations: 0, emptyContinuations: 0 })).toBe('continue_empty')
+ expect(decideLoopAction({ finishReason: 'stop', endedWithReply: false, continuations: 0, emptyContinuations: 1 })).toBe('continue_empty')
})
it('stops nudging empty steps once the empty cap is reached', () => {
- expect(decideLoopAction({ finishReason: 'stop', producedVisibleOutput: false, continuations: 0, emptyContinuations: 2 })).toBe('finish')
+ expect(decideLoopAction({ finishReason: 'stop', endedWithReply: false, continuations: 0, emptyContinuations: 2 })).toBe('finish')
})
it('auto-continues on truncation until the truncation cap', () => {
- expect(decideLoopAction({ finishReason: 'length', producedVisibleOutput: false, continuations: 0, emptyContinuations: 0 })).toBe('continue_truncation')
- expect(decideLoopAction({ finishReason: 'length', producedVisibleOutput: true, continuations: 2, emptyContinuations: 0 })).toBe('continue_truncation')
+ expect(decideLoopAction({ finishReason: 'length', endedWithReply: false, continuations: 0, emptyContinuations: 0 })).toBe('continue_truncation')
+ expect(decideLoopAction({ finishReason: 'length', endedWithReply: true, continuations: 2, emptyContinuations: 0 })).toBe('continue_truncation')
})
it('finishes once the truncation cap is reached', () => {
- expect(decideLoopAction({ finishReason: 'length', producedVisibleOutput: false, continuations: 3, emptyContinuations: 0 })).toBe('finish')
+ expect(decideLoopAction({ finishReason: 'length', endedWithReply: false, continuations: 3, emptyContinuations: 0 })).toBe('finish')
})
it('treats truncation as higher priority than emptiness', () => {
- expect(decideLoopAction({ finishReason: 'length', producedVisibleOutput: false, continuations: 0, emptyContinuations: 2 })).toBe('continue_truncation')
+ expect(decideLoopAction({ finishReason: 'length', endedWithReply: false, continuations: 0, emptyContinuations: 2 })).toBe('continue_truncation')
+ })
+})
+
+describe('stepEndsWithReply', () => {
+ it('is false for a thinking-only final step after earlier tool calls', () => {
+ expect(stepEndsWithReply({ text: '', toolCallCount: 0 })).toBe(false)
+ expect(stepEndsWithReply({ text: ' \n', toolCallCount: 0 })).toBe(false)
+ })
+
+ it('is true when the final step has text or calls a tool', () => {
+ expect(stepEndsWithReply({ text: 'Renamed the step.', toolCallCount: 0 })).toBe(true)
+ expect(stepEndsWithReply({ text: '', toolCallCount: 1 })).toBe(true)
})
})
diff --git a/packages/server/worker/test/lib/execute/jobs/ee/agent/run-agent-turn-history.test.ts b/packages/server/worker/test/lib/execute/jobs/ee/agent/run-agent-turn-history.test.ts
index 12bf3ed8f95..417a78097fe 100644
--- a/packages/server/worker/test/lib/execute/jobs/ee/agent/run-agent-turn-history.test.ts
+++ b/packages/server/worker/test/lib/execute/jobs/ee/agent/run-agent-turn-history.test.ts
@@ -90,6 +90,29 @@ describe('the history a turn leaves behind', () => {
: message.content.flatMap((part) => part.type === 'tool-call' ? [part.toolCallId] : []))
expect(toolCalls).toEqual(['update-1', 'update-2'])
})
+ it('nudges a turn whose last step is only thinking after a tool call, so it still ends with a reply', async () => {
+ let saves = 0
+
+ const turn = await runAgentTurn({
+ models: [{ model: updateThenThinkThenConfirm(), provider: AIProviderName.ANTHROPIC, modelId: TIER.modelId, thinkingBudget: TIER.thinkingBudget }],
+ systemPrompt: 'You are a test agent.',
+ messages: [{ role: 'user', content: 'Change the instructions of New agent to: Reply in Arabic.' }],
+ tools: {
+ ap_update_agent: tool({ description: 'change an agent', inputSchema: z.object({ instructions: z.string() }), execute: async () => { saves++; return { saved: true } } }),
+ },
+ allToolNames: ['ap_update_agent'],
+ tier: TIER,
+ phaseState: { phase: 'build' },
+ abortSignal: new AbortController().signal,
+ log: SILENT_LOG,
+ creditsLeft: async () => 100,
+ sinks: { drainStream: async (result) => { await result.consumeStream(); return undefined } },
+ })
+
+ expect(saves).toBe(1)
+ expect(rolesAndParts(turn.accumulatedResponseMessages).at(-1)).toBe('assistant:text')
+ expect(JSON.stringify(turn.accumulatedResponseMessages)).toContain('Done, New agent now replies in Arabic.')
+ })
})
function rolesAndParts(messages: ModelMessage[]): string[] {
@@ -141,3 +164,36 @@ function keepsUpdating(): MockLanguageModelV3 {
},
})
}
+
+function updateThenThinkThenConfirm(): MockLanguageModelV3 {
+ let calls = 0
+ return new MockLanguageModelV3({
+ doStream: async () => {
+ calls++
+ const finish = { type: 'finish' as const, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 } }
+ if (calls === 1) {
+ return { stream: convertArrayToReadableStream([
+ { type: 'stream-start' as const, warnings: [] },
+ { type: 'tool-call' as const, toolCallId: 'update-1', toolName: 'ap_update_agent', input: '{"instructions":"Reply in Arabic."}' },
+ { ...finish, finishReason: 'tool-calls' as const },
+ ]) }
+ }
+ if (calls === 2) {
+ return { stream: convertArrayToReadableStream([
+ { type: 'stream-start' as const, warnings: [] },
+ { type: 'reasoning-start' as const, id: 'think' },
+ { type: 'reasoning-delta' as const, id: 'think', delta: 'The update worked.' },
+ { type: 'reasoning-end' as const, id: 'think' },
+ { ...finish, finishReason: 'stop' as const },
+ ]) }
+ }
+ return { stream: convertArrayToReadableStream([
+ { type: 'stream-start' as const, warnings: [] },
+ { type: 'text-start' as const, id: 'done' },
+ { type: 'text-delta' as const, id: 'done', delta: 'Done, New agent now replies in Arabic.' },
+ { type: 'text-end' as const, id: 'done' },
+ { ...finish, finishReason: 'stop' as const },
+ ]) }
+ },
+ })
+}