Files
Agent-Workbench/tests/output-truncation.test.mjs
2026-11-06 06:21:59 +08:00

64 lines
3.9 KiB
JavaScript

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, /上游模型.*输出额度/);
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);
});