fix: report actual output fields in upstream truncation diagnostics

This commit is contained in:
Agent Workbench Dev
2026-11-06 20:23:56 +08:00
parent ae5123c569
commit 341f5f69f6
2 changed files with 109 additions and 4 deletions
+17 -4
View File
@@ -210,7 +210,7 @@ export class Provider {
...(body?{localEstimate:localModelEstimate(body)}:{}) };
const context = { endpoint: base, requestUrl, model: config.model, route, attempt, attemptId: receipt.attemptId };
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, endpoint: base, route, requestUrl, wireAttemptId: receipt.attemptId });
let responseDetails = null;
let responseDetails = null, wireBody;
const identityObserver = body ? modelIdentityObserver(body.model, { ...config, ...options }) : null;
const publishIdentity = complete => {
if (!identityObserver) return;
@@ -234,7 +234,7 @@ export class Provider {
dispatched = true;
const transport=this.fetcher?null:(this.transport??=new ModelTransport());
if(transport)onProgress?.({phase:'running',transport:transport.profile,attempt,maxAttempts:policy.attempts,wireAttemptId:receipt.attemptId});
const wireBody = body ? JSON.stringify(body) : undefined;
wireBody = body ? JSON.stringify(body) : undefined;
const submitted = (this.fetcher??transport.fetch.bind(transport))(requestUrl, { method: body ? 'POST' : 'GET', headers,
body: wireBody, signal: requestSignal, redirect: 'manual' });
if (wireBody && options.onSubmitted) {
@@ -290,7 +290,10 @@ export class Provider {
if (result.error || config.protocol === 'responses' && ['failed', 'incomplete', 'cancelled'].includes(result.status)) {
const upstream = captureResponse(response, { raw: Buffer.from(text, 'utf8') });
const failure = httpFailure(response.status, (upstream.message ?? upstream.reason ?? upstream.rawPreview).slice(0, 600), { ...context, upstream });
if (result.status === 'incomplete') { failure.diagnostic.kind = 'incomplete'; failure.diagnostic.title = '上游模型响应未完整结束'; }
if (result.status === 'incomplete') {
failure.diagnostic.kind = 'incomplete'; failure.diagnostic.title = '上游模型响应未完整结束';
if (config.protocol === 'responses' && result.incomplete_details?.reason === 'max_output_tokens') failure.diagnostic.reason = 'output_truncated';
}
throw failure;
}
identityObserver?.observe(JSON.parse(this.redact(JSON.stringify(result), key))); publishIdentity(true);
@@ -314,7 +317,17 @@ export class Provider {
signal?.throwIfAborted();
let diagnostic = connectionFailure(error, { ...context, method:body?'POST':'GET',responseReceived:receipt.httpStatus!==undefined,
transport:this.transport?.profile,timedOut: !!timeout.timeout, timeout: timeout.timeout, redact: value => this.redact(value, key) });
if (config.capacityMode === 'passthrough' && diagnostic.reason === 'output_truncated') diagnostic = { ...diagnostic, origin: 'upstream', title: '上游响应因输出额度结束', message: '上游返回结束原因 ' + (diagnostic.upstream?.reason ?? (config.protocol === 'anthropic-messages' ? 'max_tokens' : 'length')) + '。工作台未附加输出额度,原响应及用量已保存;不完整的工具批次未执行。' };
if (config.capacityMode === 'passthrough' && diagnostic.reason === 'output_truncated') {
// Describe the serialized request actually sent, including native user
// settings and Messages' mandatory field, rather than a local default.
const sent = wireBody ? JSON.parse(wireBody) : {};
const fields = ['max_tokens', 'max_completion_tokens', 'max_output_tokens'].filter(field => Object.hasOwn(sent, field));
const output = fields.length ? '实际请求携带 ' + fields.map(field => field + '=' + (Number.isFinite(sent[field]) ? sent[field] : '非数值(见原请求)')).join('、')
+ (config.protocol === 'anthropic-messages' ? '(Messages 协议要求 max_tokens)' : '') : '实际请求未携带输出 token 上限';
const reason = diagnostic.upstream?.reason ?? responseDetails?.reason ?? (config.protocol === 'anthropic-messages' ? 'max_tokens' : config.protocol === 'responses' ? 'max_output_tokens' : 'length');
diagnostic = { ...diagnostic, origin: 'upstream', title: '上游响应因输出额度结束',
message: '上游返回结束原因 ' + reason + '。' + output + '。原响应及用量已保存;不完整的工具批次未执行。' };
}
diagnostic = { ...diagnostic, requestUrl, ...(receipt.responseUrl ? { responseUrl: receipt.responseUrl } : {}),
...(receipt.redirectMode ? { redirected: receipt.redirected, redirectMode: receipt.redirectMode } : {}),
origin: diagnostic.origin ?? 'local', wireAttemptId: receipt.attemptId, upstreamRequestId: diagnostic.upstreamRequestId ?? receipt.upstreamRequestId,
@@ -0,0 +1,92 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import { compatibleFixture } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
const model = 'owned-output';
const truncated = protocol => protocol === 'anthropic-messages'
? { id: 'msg_owned', type: 'message', role: 'assistant', model, stop_reason: 'max_tokens', content: [{ type: 'text', text: 'Owned unfinished response' }], usage: { input_tokens: 10, output_tokens: 20 } }
: protocol === 'responses'
? { id: 'resp_owned', model, status: 'incomplete', incomplete_details: { reason: 'max_output_tokens' }, output: [], usage: { input_tokens: 10, output_tokens: 20, total_tokens: 30 } }
: { model, choices: [{ finish_reason: 'length', message: { role: 'assistant', content: 'Owned unfinished response' } }], usage: { prompt_tokens: 10, completion_tokens: 20, total_tokens: 30 } };
test('passthrough truncation describes actual HTTP output fields and preserves upstream body, usage and one physical attempt', async t => {
const scenarios = [
{ protocol: 'anthropic-messages', value: 2048 },
{ protocol: 'anthropic-messages', upstreamModel: { id: model, maxOutputTokens: 128000 }, value: 128000 },
{ protocol: 'anthropic-messages', nativeParameters: { max_tokens: 65536 }, value: 65536 },
{ protocol: 'chat-completions' },
{ protocol: 'chat-completions', nativeParameters: { max_tokens: 96000 }, value: 96000 },
{ protocol: 'chat-completions', nativeParameters: { max_completion_tokens: 128000 }, field: 'max_completion_tokens', value: 128000 },
{ protocol: 'responses' },
{ protocol: 'responses', nativeParameters: { max_output_tokens: 48000 }, value: 48000 },
];
for (const scenario of scenarios) await t.test(scenario.protocol + ' ' + (scenario.value ?? 'no output field'), async child => {
const response = truncated(scenario.protocol), upstream = await compatibleFixture(child, () => ({ body: response }));
const provider = new Provider(), events = [];
child.after(() => provider.abortRequests());
const config = { executionMode: 'unrestricted', capacityMode: 'passthrough', mode: 'compatible',
maxOutputTokens: 2048, model, baseUrl: upstream.baseUrl, ...scenario };
const field = scenario.field ?? (scenario.protocol === 'responses' ? 'max_output_tokens' : 'max_tokens');
await assert.rejects(provider.complete(config, [{ role: 'user', content: 'ORIGINAL FULL INPUT' }], null, undefined, event => events.push(event), null, []), cause => {
const diagnostic = cause.diagnostic;
assert.equal(diagnostic.reason, 'output_truncated'); assert.equal(diagnostic.origin, 'upstream');
assert.equal(diagnostic.retryDecision, 'passthrough');
assert.equal(diagnostic.upstream.rawPreview, JSON.stringify(response));
assert.ok(diagnostic.wireAttemptId); assert.equal(diagnostic.upstream.httpStatus, 200);
assert.doesNotMatch(diagnostic.message, /工作台未附加输出额度/);
if (scenario.value !== undefined) assert.ok(diagnostic.message.includes(field + '=' + scenario.value), diagnostic.message);
else assert.match(diagnostic.message, /实际请求未携带输出 token 上限/);
return true;
});
assert.equal(upstream.requests.length, 1); assert.ok(JSON.stringify(upstream.requests[0].input).includes('ORIGINAL FULL INPUT'));
if (scenario.value !== undefined) assert.equal(upstream.requests[0].input[field], scenario.value);
else for (const key of ['max_tokens', 'max_completion_tokens', 'max_output_tokens']) assert.equal(Object.hasOwn(upstream.requests[0].input, key), false);
const receipt = events.find(event => event.consumption)?.consumption;
assert.equal(receipt.usageStatus, 'reported'); assert.equal(receipt.usage.total_tokens, 30);
assert.equal(events.filter(event => event.consumption).length, 1);
});
});
test('streaming truncation also reports the actual output field without retrying', async t => {
const requests = [];
const server = http.createServer(async (request, response) => {
const chunks = []; for await (const bytes of request) chunks.push(bytes);
requests.push(JSON.parse(Buffer.concat(chunks).toString('utf8')));
response.writeHead(200, { 'Content-Type': 'text/event-stream' });
response.end('data: ' + JSON.stringify({ choices: [{ delta: { content: 'Owned partial' }, index: 0, finish_reason: 'length' }] })
+ '\n\ndata: ' + JSON.stringify({ choices: [], usage: { prompt_tokens: 10, completion_tokens: 20, total_tokens: 30 } }) + '\n\ndata: [DONE]\n\n');
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(() => new Promise(resolve => { server.closeAllConnections(); server.close(resolve); }));
const provider = new Provider(), events = []; t.after(() => provider.abortRequests());
await assert.rejects(provider.complete({ executionMode: 'unrestricted', capacityMode: 'passthrough', mode: 'compatible',
protocol: 'chat-completions', model, streaming: true, nativeParameters: { max_completion_tokens: 99999 },
baseUrl: 'http://127.0.0.1:' + server.address().port + '/v1' }, [{ role: 'user', content: 'Owned' }], null, undefined,
event => events.push(event), null, []), cause => {
assert.equal(cause.diagnostic.reason, 'output_truncated');
assert.match(cause.diagnostic.message, /max_completion_tokens=99999/);
assert.equal(cause.diagnostic.retryDecision, 'passthrough'); return true;
});
assert.equal(requests.length, 1); assert.equal(requests[0].max_completion_tokens, 99999);
assert.equal(events.filter(event => event.consumption).length, 1);
});
test('Responses incomplete reasons other than max_output_tokens retain their upstream meaning', async t => {
for (const reason of ['content_filter', undefined]) await t.test(reason ?? 'missing reason', async child => {
const response = { ...truncated('responses'), incomplete_details: reason ? { reason } : {} };
const upstream = await compatibleFixture(child, () => ({ body: response }));
const provider = new Provider(), events = []; child.after(() => provider.abortRequests());
await assert.rejects(provider.complete({ executionMode: 'unrestricted', capacityMode: 'passthrough', mode: 'compatible',
protocol: 'responses', model, baseUrl: upstream.baseUrl }, [{ role: 'user', content: 'Owned input' }], null, undefined,
event => events.push(event), null, []), cause => {
assert.notEqual(cause.diagnostic.reason, 'output_truncated');
assert.equal(cause.diagnostic.kind, 'incomplete'); assert.equal(cause.diagnostic.origin, 'upstream');
assert.equal(cause.diagnostic.upstream.rawPreview, JSON.stringify(response));
assert.equal(cause.diagnostic.upstream.reason, reason ?? null); return true;
});
assert.equal(upstream.requests.length, 1);
assert.equal(events.filter(event => event.consumption).length, 1);
});
});