Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions bin/agy-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,11 @@ class AgyWorker {
this._fail(turnError(err.message, { isSpawnError: true }));
});
child.on('close', (code, signal) => {
// Flush any unterminated final line before judging the exit
// (#683): a partial delta that never got its newline is real
// output, and the resume-retry gate reads hasOutput from the
// deltas this flush delivers.
this._flushLineBuffer();
// Failure is judged by the child's exit (#678): the last stderr
// line names the cause when there is one, then the signal, then
// the exit code — the same detail ladder the cold path used.
Expand All @@ -554,12 +559,23 @@ class AgyWorker {
this.lineBuffer += chunk.toString();
const lines = this.lineBuffer.split('\n');
this.lineBuffer = lines.pop() || '';
for (const line of lines) {
if (!line.trim()) continue;
let obj;
try { obj = JSON.parse(line); } catch { continue; }
this._onEvent(obj);
}
for (const line of lines) this._processLine(line);
}

_processLine(line) {
if (!line.trim()) return;
let obj;
try { obj = JSON.parse(line); } catch { return; }
this._onEvent(obj);
}

// The child can exit mid-line: whatever tail is still buffered is a
// complete final line for judging purposes (#683) -- run it through
// the same path a terminated line takes before any close judgment.
_flushLineBuffer() {
const tail = this.lineBuffer;
this.lineBuffer = '';
if (tail.trim()) this._processLine(tail);
}

_onEvent(obj) {
Expand Down
29 changes: 29 additions & 0 deletions bin/agy-proxy.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ while IFS= read -r line; do
printf '%s\\n' '{"event":"step_update","step_update":{"text_delta":"partial answer"}}'
echo "stub agy: run failed midway" >&2
exit 3 ;;
*FAILTAIL*)
if [ -n "$conv" ]; then
printf '%s' '{"event":"step_update","step_update":{"text_delta":"partial tail"}}'
echo "stub agy: tail run failed" >&2
exit 3
fi ;;
*SIGKILLME*)
kill -9 $$ ;;
esac
Expand Down Expand Up @@ -220,6 +226,29 @@ test('failed resume still retries once as a fresh conversation (#678)', async (t
assert.match(sse, /"finish_reason":"stop"/);
});

test('unterminated final delta counts as output: failed resume is NOT retried fresh (#683)', async (t) => {
const { port, stop } = await startProxy();
t.after(stop);
const messages = [{ role: 'user', content: 'please FAILTAIL now' }];
const headers = { 'x-conversation-id': 'conv-456' };

const plainRes = await post(port, { messages }, headers);
assert.equal(plainRes.status, 502, 'a resumed turn whose only output is an unterminated tail delta must fail, not silently retry as a fresh conversation');
const plainBody = JSON.parse(await plainRes.text());
assert.match(plainBody.stderr, /stub agy: tail run failed/);

const streamRes = await post(port, { messages, stream: true }, headers);
assert.equal(streamRes.status, 200);
const events = parseSse(await streamRes.text());
const deltaChunk = events.find(e => e.choices && e.choices[0].delta && e.choices[0].delta.content === 'partial tail');
assert.ok(deltaChunk, 'the unterminated tail delta must be delivered exactly like a terminated line');
const errorEvent = events.find(e => e.error);
assert.ok(errorEvent, 'stream must carry an error event');
assert.match(errorEvent.error.message, /tail run failed/);
const stopChunk = events.find(e => e.choices && e.choices[0].finish_reason === 'stop');
assert.equal(stopChunk, undefined, "no chunk may claim finish_reason 'stop'");
});

test('session requests reuse one resident worker (same pid across turns)', async (t) => {
const { port, stop } = await startProxy();
t.after(stop);
Expand Down