feat(runtime): recover a bounded number of output-ceiling truncations
When a turn stops at the output-token ceiling (output_truncated), the run no longer dies. The truncated turn is still dropped whole - no tool is executed and the invalid assistant message is never appended - but the model is given a standalone instruction to split its proposals and the run continues. Recoveries are capped per run (2); past that the original diagnostic still fails the run. The connection is not cooled down for a truncation, since it responded fine and only the model overran its budget, so the recovery turn can reach it. Both the failed attempt and the recovering turn are accounted; neither is unknown. Fixture regression covers recover-then-continue and the exhausted cap.
This commit is contained in:
@@ -238,3 +238,52 @@ test('stream parser rejects truncation, finish length, malformed JSON, duplicate
|
||||
await assert.rejects(readModelStream(new Response(body, { headers: { 'content-type': 'text/event-stream' } }), { protocol }, undefined));
|
||||
});
|
||||
});
|
||||
|
||||
// A turn truncated at the output ceiling (one complete propose_file followed by a
|
||||
// second call cut off at finish_reason=length) is dropped whole; the run does not
|
||||
// fail but injects a split-the-work message and continues. On the next turn the model
|
||||
// obeys and the run completes. This is the AB sample shape, recovered end to end.
|
||||
const truncatedTurn = chat({ tool_calls: [
|
||||
{ index: 0, id: 'file-a', type: 'function', function: { name: 'propose_file', arguments: '{"path":"src/a.mjs","content":"export const a=1;"}' } },
|
||||
{ index: 1, id: 'file-b', type: 'function', function: { name: 'propose_file', arguments: '{"path":"src/b.mjs","content":"export const b=' } },
|
||||
] }) + chat({}, 'length') + sse({ choices: [], usage: { prompt_tokens: 9958, completion_tokens: 2048 } }) + sse('[DONE]');
|
||||
|
||||
test('an output-ceiling truncation is recovered: the run injects a split-the-work note and continues to completion', async t => {
|
||||
const f = fixture(t);
|
||||
const server = await streamingServer(t, (res, body) => {
|
||||
const sawTruncationNote = body.messages.some(message => message.role === 'user' && /finish_reason=length/.test(message.content ?? ''));
|
||||
res.end(sawTruncationNote ? chat({ content: '已拆分,本次不再堆叠工具。' }) + chatEnd() : truncatedTurn);
|
||||
});
|
||||
const { provider } = configure(f, server), runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
|
||||
runtime.start(f.session.id, '写入两个文件'); await until(() => !runtime.isActive(f.session.id), 15000);
|
||||
// The run recovered rather than failing on the truncation: the model saw the note
|
||||
// and produced a clean follow-up turn. (It then settles per the file contract.)
|
||||
assert.notEqual(f.session.status, 'failed', JSON.stringify(f.session.events.slice(-4)));
|
||||
assert.equal(server.requests.length, 2);
|
||||
// The second request carried the split-the-work instruction to the model.
|
||||
assert.ok(server.requests[1].body.messages.some(message => message.role === 'user' && /finish_reason=length/.test(message.content ?? '')));
|
||||
// The truncated turn executed no tools and was never appended as an assistant turn.
|
||||
assert.equal(f.store.core.tool.size, 0);
|
||||
assert.equal(f.session.providerMessages.filter(message => message.tool_calls?.length).length, 0);
|
||||
assert.equal(f.session.messages.filter(message => message.role === 'system' && /finish_reason=length/.test(message.content)).length, 1);
|
||||
// Both the failed attempt and the recovering turn are accounted; neither is unknown.
|
||||
const run = f.store.core.run.get(f.session.runId);
|
||||
assert.equal(run.usage.requests, 2); assert.equal(run.usage.unknownRequests, 0);
|
||||
assert.equal(run.outputTruncationRecoveries, 1);
|
||||
});
|
||||
|
||||
test('repeated output-ceiling truncations stop after the recovery limit with the output-truncated diagnostic and zero tools', async t => {
|
||||
const f = fixture(t);
|
||||
const server = await streamingServer(t, res => res.end(truncatedTurn));
|
||||
const { provider } = configure(f, server), runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
|
||||
runtime.start(f.session.id, '写入两个文件'); await until(() => !runtime.isActive(f.session.id), 15000);
|
||||
assert.equal(f.session.status, 'failed');
|
||||
assert.equal(f.store.core.tool.size, 0);
|
||||
// Initial attempt plus the capped recoveries, each a real accounted request.
|
||||
assert.equal(server.requests.length, 3);
|
||||
const run = f.store.core.run.get(f.session.runId);
|
||||
assert.equal(run.usage.requests, 3); assert.equal(run.usage.unknownRequests, 0);
|
||||
assert.equal(run.outputTruncationRecoveries, 3);
|
||||
assert.equal(f.session.failure.reason, 'output_truncated');
|
||||
assert.match(f.session.failure.message, /output token 上限|finish_reason=length/);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user