fix(models): give an actionable diagnostic when output hits the token ceiling

A response stopped at finish_reason=length (or Messages max_tokens /
Responses max_output_tokens) is still rejected whole - a complete leading
tool call is never applied because a later call may be cut mid-argument.
Previously the model only saw a generic incompleteness message and would
repeat the same oversized multi-tool turn. Now these paths raise a shared
output_truncated diagnostic telling the model to split proposals into one
file per response. Regression covers streaming/non-streaming Chat,
Responses and Messages, including the AB sample shape.
This commit is contained in:
Agent Workbench Dev
2026-11-05 04:47:42 +08:00
parent 7ab242dfec
commit dfb6029f33
3 changed files with 79 additions and 5 deletions
+2 -1
View File
@@ -1,5 +1,5 @@
import { createHash } from 'node:crypto';
import { protocolFailure, normalizeProtocolResponse, validateModelResponse, reportedUsage } from './protocols.mjs';
import { protocolFailure, outputTruncatedFailure, normalizeProtocolResponse, validateModelResponse, reportedUsage } from './protocols.mjs';
import { httpFailure } from '../../server/diagnostics.mjs';
import { upstreamCapture } from './upstream-error.mjs';
import { messagesStream } from './messages-stream.mjs';
@@ -155,6 +155,7 @@ export async function readModelStream(response, config, signal, onProgress, tool
}
value = terminal;
} else {
if (done && finish === 'length') throw outputTruncatedFailure('stream_interrupted');
if (!done || !['stop', 'tool_calls'].includes(finish)) throw protocolFailure('Chat流未完整终结或达到输出上限,未执行工具。', 'stream_interrupted');
value = { choices: [{ message: { role: 'assistant', content: text, ...(hasReasoning ? { reasoning_content: reasoning } : {}), ...(calls.size ? { tool_calls: [...calls.entries()].sort((a, b) => a[0] - b[0]).map(([, item]) => item) } : {}) }, finish_reason: finish }], usage };
}
+14 -4
View File
@@ -1,6 +1,13 @@
import { ModelRequestError } from '../../server/diagnostics.mjs';
export const protocolFailure = (message, kind = 'protocol') => new ModelRequestError({ kind, source: 'model_connection', origin: 'local', stage: 'model_request',
title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString() });
export const protocolFailure = (message, kind = 'protocol', extra = null) => new ModelRequestError({ kind, source: 'model_connection', origin: 'local', stage: 'model_request',
title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString(), ...(extra ? extra : {}) });
// A response stopped at the model's output-token ceiling is never partially executed:
// even a syntactically complete leading tool call is dropped, because any later call in
// the same turn may be cut mid-argument. The diagnostic must tell the model what happened
// and how to proceed, instead of a generic incompleteness message it cannot act on.
export const OUTPUT_TRUNCATED_HINT = '模型输出达到单次 output token 上限(finish_reason=length),本次响应整体未执行任何工具(即使其中一个工具参数已完整也不执行,避免应用被截断的半成品)。请减少单次响应的工具数量:把多文件提案拆成每次一个文件的 propose_file/propose_patch,或分多轮 prepare_change_set,不要在一个响应里堆叠多个大参数工具。';
export const outputTruncatedFailure = (kind = 'protocol') => protocolFailure(OUTPUT_TRUNCATED_HINT, kind, { reason: 'output_truncated' });
export { protocolRoute } from '../../shared/contracts/model-endpoint.mjs';
export function reportedUsage(config, value) {
@@ -33,7 +40,8 @@ function acceptsEmptyObject(tools, name) {
export function validateModelResponse(value, tools) {
const choice = value.choices?.[0], message = choice?.message;
if (!message || choice.finish_reason === 'length' || choice.finish_reason === 'content_filter') throw protocolFailure('模型响应缺少完整message或没有完整结束,未执行工具。');
if (choice?.finish_reason === 'length') throw outputTruncatedFailure();
if (!message || choice.finish_reason === 'content_filter') throw protocolFailure('模型响应缺少完整message或因内容过滤未完整结束,未执行工具。');
const calls = message.tool_calls;
if (calls !== undefined && (!Array.isArray(calls) || calls.length > 12)) throw protocolFailure('模型工具调用数量或格式无效。');
const ids = new Set();
@@ -52,8 +60,9 @@ export function validateModelResponse(value, tools) {
export function normalizeProtocolResponse(config, value) {
if (config.protocol === 'anthropic-messages') {
if (value?.type === 'message' && value.stop_reason === 'max_tokens') throw outputTruncatedFailure();
if (value?.type !== 'message' || typeof value.id !== 'string' || !value.id || value.role !== 'assistant' || !Array.isArray(value.content)
|| !['end_turn', 'tool_use', 'stop_sequence', 'refusal'].includes(value.stop_reason)) throw protocolFailure('Messages 未完整结束或停止原因为输出/上下文上限、暂停或未知;原响应未用于执行工具。');
|| !['end_turn', 'tool_use', 'stop_sequence', 'refusal'].includes(value.stop_reason)) throw protocolFailure('Messages 未完整结束或停止原因为上下文上限、暂停或未知;原响应未用于执行工具。');
const calls = [], texts = [];
for (const block of value.content) {
if (!block || typeof block !== 'object' || Array.isArray(block)) throw protocolFailure('Messages 内容块格式无效。');
@@ -83,6 +92,7 @@ export function normalizeProtocolResponse(config, value) {
connectionId: config.modelConnectionId ?? null, credentialRevision: config.modelCredentialRevision ?? null, reasoningContent: received.reasoning_content };
return normalized;
}
if (value.status === 'incomplete' && value.incomplete_details?.reason === 'max_output_tokens') throw outputTruncatedFailure();
if (value.status !== 'completed' || !Array.isArray(value.output)) throw protocolFailure('Responses未完整结束,原响应未用于执行工具。');
const calls = [], texts = [], ids = new Set();
for (const item of value.output) {