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(chat): a looping chat turn now stops at its usage limit (#16838)
Hazem Adelebc75f9ea79d
diff --git a/brain/knowledge/flows-execution/chat.md b/brain/knowledge/flows-execution/chat.mdindex 8a68aeca0f4..c440c2c83f4 100644--- a/brain/knowledge/flows-execution/chat.md+++ b/brain/knowledge/flows-execution/chat.md@@ -38,6 +38,7 @@ A turn is kept alive / reclaimed by three separate mechanisms in `execute-agent-- **The per-user tail breaks the prompt cache.** The date, memory, connection inventory and `{{PROJECT_LIST}}` sit inside the same single system block as the ~22k-token static prompt. Any change re-writes the whole cached prefix, and the prompt is effectively unique per user.- **A step's `response.messages` holds only that step's messages (AI SDK v7).** The older SDK made it cumulative, so `collectStepMessages` once took the last step; under v7 that saved only a turn's closing text and dropped every tool call and result. The next turn then saw replies like "New agent is updated" with no tool call behind them, and repeated the pattern by claiming changes it never made. It now joins every step, and `run-agent-turn-history.test.ts` runs a real `streamText` turn so a future SDK change to these semantics fails loudly.- **Chat has no fixed step cap.** A chat turn is bounded by credits (checked after every step), the runaway-token ceiling (`RUNAWAY_TURN_CONTEXT_MULTIPLE` × the context window), the identical-failure guard and the 2-hour `MAX_AGENT_TURN_WALL_CLOCK_MS`, so it finishes long jobs. A saved agent's author-set `maxSteps` is still honored on its last step: an agent with structured output is forced to call `TASK_COMPLETION_TOOL_NAME` so the flow gets a result, and any other agent runs with `toolChoice: 'none'` plus a wrap-up note, so it replies with what's done and what's left. Never a silent stop or an error.+- **A turn limit only works as a stop condition.** One `streamText` call runs every step of a turn, so a check after it returns can't stop a runaway. The token ceiling used to be checked only there, and a turn on 24 Oct 2026 ran 181 steps and 50.8M input tokens (AI-86). Put every per-turn bound in `loopStopCondition`. Test mocks must report usage in the V3 shape (`inputTokens: { total, ... }`); flat numbers read as 0 tokens.- **The streaming lock is per run, not per conversation.** A re-send can claim `activeRunId` before the first job reaches `getAgentConfig`. `agentHelpers.acquireStreamingLock` only lets the owning run set STREAMING; an older run gets `AGENT_RUN_SUPERSEDED` and the worker exits quietly, with no ERROR save and no events. A lock that ignored `runId` let the old run stream unowned while the new one was rejected as busy and marked ERROR, which is how users saw nothing after tapping a starter card twice.- **A redelivered run is a duplicate, not busy.** A worker socket blip makes the app requeue a chat job whose worker is still running it (`releaseConnectionJobs`). The second delivery finds its own `activeRunId` STREAMING, so `acquireStreamingLock` returns `duplicate` and it exits quietly like a superseded run. When this came back as `busy` it saved ERROR, and users saw "An agent is already running for this conversation" in the middle of a working turn (prod, 2026-10-13). If the first worker really died, the 90s `CHAT_STALE_SWEEP` frees the conversation.- **A failed turn used to leave status ERROR with an empty reply.** The error text only travelled as a websocket `ERROR` event, so a reload, another tab, or a user who was away saw a dead conversation (and often no user message either when the job died inside `getAgentConfig`, before it persists the turn). The `execute-agent-run.ts` catch now sends `failure: { message, userMessage? }` on the empty `saveAgentMessages` call and `conversation-rpc.ts` appends it to `uiMessages` as an assistant text reply; `use-chat.ts` shows the Retry banner when a loaded conversation is ERROR. Stale STREAMING to IDLE recovery, a worker that never picks the job up, and `assertAgentMessageRateLimitNotExceeded` still leave no reply of their own.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 edf59a86162..3104516c08d 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@@ -88,10 +88,15 @@ export async function runAgentTurn({ models, fastModel, systemPrompt, messages,paidToolsAffordable = isNil(left) || left >= CHAT_CREDITS_PER_TOOL_CALLreturn creditsExhausted}+ const maxTurnTokens = runawayTokenCeiling(models[0].provider)+ let totalInputTokens = 0+ let totalOutputTokens = 0+ const tokenBudgetSpent = (): boolean => totalInputTokens + totalOutputTokens >= maxTurnTokensconst loopStopCondition = [...(Array.isArray(baseStopCondition) ? baseStopCondition : [baseStopCondition]),...(isNil(stepCeiling) ? [] : [isStepCount(stepCeiling)]),creditsRanOut,+ tokenBudgetSpent,]const reroutedCallIds = new Set<string>()const skillSurface = isNil(skills) ? null : buildSkillSurface({@@ -108,7 +113,6 @@ export async function runAgentTurn({ models, fastModel, systemPrompt, messages,})const turnSystemPrompt = systemPrompt + (skillSurface?.catalogNote ?? '')const guardedTools = wrapToolsWithFailureGuard({ tools: skillSurface?.tools ?? tools, log })- const maxTurnTokens = runawayTokenCeiling(models[0].provider)const runsATier = models.some((candidate) => !isNil(candidate.key))const peers = [...models, ...(isNil(fastModel) ? [] : [fastModel])]const ranked = await Promise.all(models.map((turnModel) => withOutputBudget({ turnModel, peers })))@@ -132,8 +136,6 @@ export async function runAgentTurn({ models, fastModel, systemPrompt, messages,let streamRetries = 0let truncatedAfterRetries = falselet usage: LanguageModelUsage | undefined- let totalInputTokens = 0- let totalOutputTokens = 0let lastFinishReason = ''let budgetExceeded = false@@ -330,7 +332,7 @@ export async function runAgentTurn({ models, fastModel, systemPrompt, messages,break}- if (totalInputTokens + totalOutputTokens >= maxTurnTokens) {+ if (tokenBudgetSpent()) {accumulatedResponseMessages.push(...stepMessages)budgetExceeded = truelog.error({ totalInputTokens, totalOutputTokens, maxTurnTokens }, 'Chat turn hit per-turn token budget — stopping to prevent runaway cost')diff --git a/packages/server/worker/test/lib/agent-eval/agent-turn-credits.test.ts b/packages/server/worker/test/lib/agent-eval/agent-turn-credits.test.tsindex 0f754e86f51..f82ae212bdf 100644--- a/packages/server/worker/test/lib/agent-eval/agent-turn-credits.test.ts+++ b/packages/server/worker/test/lib/agent-eval/agent-turn-credits.test.ts@@ -87,6 +87,18 @@ describe('a turn with many steps', () => {})})+describe('a turn that hits its token budget', () => {+ it('stops at the step that crossed the budget, instead of running every step', async () => {+ const search = vi.fn(async () => SEARCH_RESULT)+ const model = alwaysSearchingModel({ usage: OVER_BUDGET_USAGE })++ const turn = await runTurn({ search, creditsLeft: async () => 1_000, model })++ expect(search).toHaveBeenCalledTimes(1)+ expect(turn.budgetExceeded).toBe(true)+ })+})+async function runTurn({ search, creditsLeft, stepCeiling = 20, model = alwaysSearchingModel(), drainsStream = false, completion, phase = 'discovery' }: {search: () => Promise<unknown>creditsLeft: (pendingCredits: number) => Promise<number | null>@@ -123,7 +135,7 @@ async function failingCreditCheck(): Promise<number | null> {throw new Error('rpc down')}-function alwaysSearchingModel(): MockLanguageModelV3 {+function alwaysSearchingModel({ usage = USAGE }: { usage?: typeof USAGE } = {}): MockLanguageModelV3 {let calls = 0return new MockLanguageModelV3({doStream: async () => {@@ -132,7 +144,7 @@ function alwaysSearchingModel(): MockLanguageModelV3 {stream: convertArrayToReadableStream([{ type: 'stream-start' as const, warnings: [] },{ type: 'tool-call' as const, toolCallId: `call-${calls}`, toolName: 'ap_web_search', input: '{"query":"more"}' },- { type: 'finish' as const, finishReason: 'tool-calls' as const, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 } },+ { type: 'finish' as const, finishReason: 'tool-calls' as const, usage },]),}},@@ -150,7 +162,7 @@ function searchUntilToldToAnswer({ searches }: { searches: number }): MockLanguastream: convertArrayToReadableStream([{ type: 'stream-start' as const, warnings: [] },{ type: 'tool-call' as const, toolCallId: `call-${calls}`, toolName: toolChoice.toolName, input: '{"output":{"summary":"partial"}}' },- { type: 'finish' as const, finishReason: 'tool-calls' as const, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 } },+ { type: 'finish' as const, finishReason: 'tool-calls' as const, usage: USAGE },]),}}@@ -161,12 +173,12 @@ function searchUntilToldToAnswer({ searches }: { searches: number }): MockLangua{ type: 'text-start' as const, id: 'answer' },{ type: 'text-delta' as const, id: 'answer', delta: 'Here is what I found so far.' },{ type: 'text-end' as const, id: 'answer' },- { type: 'finish' as const, finishReason: 'stop' as const, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 } },+ { type: 'finish' as const, finishReason: 'stop' as const, usage: USAGE },]: [{ type: 'stream-start' as const, warnings: [] },{ type: 'tool-call' as const, toolCallId: `call-${calls}`, toolName: 'ap_web_search', input: '{"query":"more"}' },- { type: 'finish' as const, finishReason: 'tool-calls' as const, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 } },+ { type: 'finish' as const, finishReason: 'tool-calls' as const, usage: USAGE },]),}},@@ -178,3 +190,7 @@ const SEARCH_RESULT = { content: [{ type: 'text', text: 'ok' }] }const TIER = { id: 'fast', thinkingBudget: 5_000, modelId: 'anthropic/claude-haiku-4.5' }const SILENT_LOG = { debug: () => undefined, info: () => undefined, warn: () => undefined, error: () => undefined }++const USAGE = { inputTokens: { total: 1, noCache: 1, cacheRead: 0, cacheWrite: 0 }, outputTokens: { total: 1, text: 1, reasoning: 0 } }++const OVER_BUDGET_USAGE = { ...USAGE, inputTokens: { total: Number.MAX_SAFE_INTEGER, noCache: Number.MAX_SAFE_INTEGER, cacheRead: 0, cacheWrite: 0 } }