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:
@@ -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 };
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { validateModelResponse, normalizeProtocolResponse, OUTPUT_TRUNCATED_HINT } from '../src/core/models/protocols.mjs';
|
||||
import { readModelStream } from '../src/core/models/model-stream.mjs';
|
||||
|
||||
// Regression for the AB live sample: a Chat SSE response that emits one complete
|
||||
// propose_file tool call and then a second call cut off at the 2048 output-token
|
||||
// ceiling (finish_reason=length). The whole response must be rejected (never execute
|
||||
// the complete leading call, because a later call may be truncated mid-argument), and
|
||||
// the diagnostic must be actionable rather than a generic incompleteness message.
|
||||
|
||||
const sse = event => 'data: ' + (typeof event === 'string' ? event : JSON.stringify(event)) + '\r\n\r\n';
|
||||
const assertTruncated = error => {
|
||||
assert.ok(error instanceof Error);
|
||||
assert.equal(error.diagnostic.reason, 'output_truncated');
|
||||
assert.equal(error.diagnostic.message, OUTPUT_TRUNCATED_HINT);
|
||||
assert.match(error.diagnostic.message, /output token 上限/);
|
||||
assert.match(error.diagnostic.message, /拆成每次一个文件|分多轮/);
|
||||
return true;
|
||||
};
|
||||
|
||||
test('non-streaming Chat finish_reason=length yields an actionable output-truncated diagnostic', () => {
|
||||
const body = { choices: [{ finish_reason: 'length', message: { role: 'assistant',
|
||||
tool_calls: [{ id: 'call_1', type: 'function', function: { name: 'propose_file', arguments: '{"path":"src/amount.mjs","content":"export const x = 1;"}' } }] } }] };
|
||||
assert.throws(() => validateModelResponse(body, []), assertTruncated);
|
||||
});
|
||||
|
||||
test('content_filter stays a distinct non-truncation rejection', () => {
|
||||
const body = { choices: [{ finish_reason: 'content_filter', message: { role: 'assistant', content: '' } }] };
|
||||
assert.throws(() => validateModelResponse(body, []), error => {
|
||||
assert.notEqual(error.diagnostic.reason, 'output_truncated');
|
||||
assert.match(error.diagnostic.message, /内容过滤/);
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
test('Responses status=incomplete/max_output_tokens yields the output-truncated diagnostic', () => {
|
||||
const value = { status: 'incomplete', incomplete_details: { reason: 'max_output_tokens' },
|
||||
output: [{ type: 'function_call', call_id: 'c1', name: 'propose_file', arguments: '{"path":"a"' }] };
|
||||
assert.throws(() => normalizeProtocolResponse({ protocol: 'responses' }, value), assertTruncated);
|
||||
});
|
||||
|
||||
test('Messages stop_reason=max_tokens yields the output-truncated diagnostic', () => {
|
||||
const value = { type: 'message', id: 'm1', role: 'assistant', stop_reason: 'max_tokens',
|
||||
content: [{ type: 'tool_use', id: 't1', name: 'propose_file', input: { path: 'a' } }] };
|
||||
assert.throws(() => normalizeProtocolResponse({ protocol: 'anthropic-messages' }, value), assertTruncated);
|
||||
});
|
||||
|
||||
test('streaming Chat with complete then truncated tool call (finish_reason=length) is rejected whole with the actionable diagnostic', async () => {
|
||||
// First tool call arrives complete; the stream then terminates with finish_reason=length
|
||||
// as the model was mid-way through a second call. SSE still sends [DONE].
|
||||
const body =
|
||||
sse({ choices: [{ index: 0, delta: { role: 'assistant', tool_calls: [{ index: 0, id: 'call_1', type: 'function',
|
||||
function: { name: 'propose_file', arguments: '{"path":"src/amount.mjs","content":"export const x=1;"}' } }] }, finish_reason: null }] })
|
||||
+ sse({ choices: [{ index: 0, delta: { tool_calls: [{ index: 1, id: 'call_2', type: 'function',
|
||||
function: { name: 'propose_file', arguments: '{"path":"src/summary.mjs","content":"export const y=' } }] }, finish_reason: null }] })
|
||||
+ sse({ choices: [{ index: 0, delta: {}, finish_reason: 'length' }] })
|
||||
+ sse({ choices: [], usage: { prompt_tokens: 9958, completion_tokens: 2048 } })
|
||||
+ sse('[DONE]');
|
||||
await assert.rejects(
|
||||
readModelStream(new Response(body, { headers: { 'content-type': 'text/event-stream' } }), { protocol: 'chat-completions' }, undefined),
|
||||
assertTruncated);
|
||||
});
|
||||
Reference in New Issue
Block a user