Skip to content

Commit 52a32c5

Browse files
author
Peter Steinberger
committed
fix(gateway): preserve sticky outcomes for short agent waits
1 parent abf9c24 commit 52a32c5

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

‎src/gateway/server-methods/agent-job.timeout-fallback.test.ts‎

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ describe("waitForAgentJob timeout fallback", () => {
160160

161161
it("keeps retryable lifecycle failures pending for the full retry grace", async () => {
162162
const runId = `run-timeout-fallback-retryable-${runSequence++}`;
163-
const waitPromise = waitForAgentJob({ runId, timeoutMs: 1_000 });
163+
const waitPromise = waitForAgentJob({ runId, timeoutMs: 5_000 });
164164

165165
emitAgentEvent({
166166
runId,
@@ -178,14 +178,14 @@ describe("waitForAgentJob timeout fallback", () => {
178178
},
179179
});
180180

181-
await vi.advanceTimersByTimeAsync(1_000);
181+
await vi.advanceTimersByTimeAsync(5_000);
182182
await expect(waitPromise).resolves.toMatchObject({
183183
status: "timeout",
184184
error: "Retryable provider failure",
185185
pendingError: true,
186186
});
187187

188-
await vi.advanceTimersByTimeAsync(14_000);
188+
await vi.advanceTimersByTimeAsync(10_000);
189189
await expect(waitForAgentJob({ runId, timeoutMs: 0 })).resolves.toMatchObject({
190190
status: "error",
191191
endedAt: 1_100,
@@ -390,6 +390,34 @@ describe("waitForAgentJob timeout fallback", () => {
390390
error: "RPC cancelled the run",
391391
},
392392
},
393+
{
394+
label: "provider timeout error before a later provider end",
395+
first: {
396+
phase: "error",
397+
endedAt: 1_100,
398+
aborted: true,
399+
stopReason: "timeout",
400+
timeoutPhase: "provider",
401+
providerStarted: true,
402+
error: "Provider timed out",
403+
},
404+
second: {
405+
phase: "end",
406+
endedAt: 1_200,
407+
aborted: true,
408+
stopReason: "timeout",
409+
timeoutPhase: "provider",
410+
providerStarted: true,
411+
},
412+
expected: {
413+
status: "timeout",
414+
endedAt: 1_100,
415+
stopReason: "timeout",
416+
timeoutPhase: "provider",
417+
providerStarted: true,
418+
error: "Provider timed out",
419+
},
420+
},
393421
{
394422
label: "restart error after an earlier provider end",
395423
first: {
@@ -464,7 +492,7 @@ describe("waitForAgentJob timeout fallback", () => {
464492
},
465493
])("preserves canonical terminal precedence for $label", async ({ first, second, expected }) => {
466494
const runId = `run-timeout-fallback-cross-phase-${runSequence++}`;
467-
const waitPromise = waitForAgentJob({ runId, timeoutMs: 20_000 });
495+
const waitPromise = waitForAgentJob({ runId, timeoutMs: 5_000 });
468496

469497
emitAgentEvent({
470498
runId,
@@ -478,7 +506,7 @@ describe("waitForAgentJob timeout fallback", () => {
478506
});
479507
const freshWaitPromise = waitForAgentJob({
480508
runId,
481-
timeoutMs: 20_000,
509+
timeoutMs: 5_000,
482510
ignoreCachedSnapshot: true,
483511
});
484512
emitAgentEvent({
@@ -487,9 +515,12 @@ describe("waitForAgentJob timeout fallback", () => {
487515
data: { startedAt: 1_000, ...second },
488516
});
489517

490-
await vi.advanceTimersByTimeAsync(15_000);
491-
await expect(waitPromise).resolves.toMatchObject({ startedAt: 1_000, ...expected });
492-
await expect(freshWaitPromise).resolves.toMatchObject({ startedAt: 1_000, ...expected });
518+
await vi.advanceTimersByTimeAsync(5_000);
519+
const terminalOutcome = { startedAt: 1_000, pendingError: undefined, ...expected };
520+
await expect(waitPromise).resolves.toMatchObject(terminalOutcome);
521+
await expect(freshWaitPromise).resolves.toMatchObject(terminalOutcome);
522+
await expect(waitForAgentJob({ runId, timeoutMs: 0 })).resolves.toBeNull();
523+
await vi.advanceTimersByTimeAsync(10_000);
493524
await expect(waitForAgentJob({ runId, timeoutMs: 0 })).resolves.toMatchObject(expected);
494525
});
495526

‎src/gateway/server-methods/agent-job.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,11 @@ export async function waitForAgentJob(params: {
549549
if (!params.source) {
550550
const pendingError = pendingAgentRunErrors.get(params.runId)?.snapshot;
551551
if (pendingError && pendingError.version > afterVersion) {
552-
finish(createPendingErrorTimeoutSnapshot(pendingError));
552+
finish(
553+
isStickyAgentRunTerminalOutcome(terminalOutcomeFromSnapshot(pendingError))
554+
? publicSnapshot(pendingError)
555+
: createPendingErrorTimeoutSnapshot(pendingError),
556+
);
553557
return;
554558
}
555559
const pendingTimeout = pendingAgentRunTimeouts.get(params.runId)?.snapshot;

0 commit comments

Comments
 (0)