Skip to content

Commit e49e9e2

Browse files
steipetePeter Steinberger
andauthored
fix(agents): honor monotonic subagent completion deadline (#117750)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 5cb0c9d commit e49e9e2

2 files changed

Lines changed: 109 additions & 10 deletions

File tree

‎src/agents/subagent-announce-capture.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ export async function readLatestSubagentOutputWithRetryUsing<Outcome = unknown>(
1313
readSubagentOutput: (sessionKey: string, outcome?: Outcome) => Promise<string | undefined>;
1414
}): Promise<string | undefined> {
1515
const maxWaitMs = Math.max(0, Math.min(params.maxWaitMs, 15_000));
16-
let waitedMs = 0;
17-
let result: string | undefined;
18-
while (waitedMs < maxWaitMs) {
19-
result = await params.readSubagentOutput(params.sessionKey, params.outcome);
16+
if (!(maxWaitMs > 0)) {
17+
return undefined;
18+
}
19+
const deadlineAt = performance.now() + maxWaitMs;
20+
for (;;) {
21+
const result = await params.readSubagentOutput(params.sessionKey, params.outcome);
2022
if (result?.trim()) {
2123
return result;
2224
}
23-
const remainingMs = maxWaitMs - waitedMs;
25+
const remainingMs = deadlineAt - performance.now();
2426
if (remainingMs <= 0) {
25-
break;
27+
return result;
2628
}
2729
const sleepMs = Math.min(params.retryIntervalMs, remainingMs);
2830
// Use real timers here; tests provide fake timers around this small retry loop.
2931
await new Promise((resolve) => {
3032
setTimeout(resolve, sleepMs);
3133
});
32-
waitedMs += sleepMs;
3334
}
34-
return result;
3535
}
3636

3737
/** Captures immediate output first, then optionally waits for a delayed completion reply. */

‎src/agents/subagent-announce.capture-completion-reply.test.ts‎

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Completion-reply capture tests cover the short polling window used to collect
22
// final child output after an announce waits for the subagent.
33
import { describe, expect, it, vi } from "vitest";
4-
import { captureSubagentCompletionReplyUsing } from "./subagent-announce-capture.js";
4+
import {
5+
captureSubagentCompletionReplyUsing,
6+
readLatestSubagentOutputWithRetryUsing,
7+
} from "./subagent-announce-capture.js";
58

69
describe("captureSubagentCompletionReply", () => {
710
it("returns immediate assistant output from history without polling", async () => {
@@ -44,6 +47,59 @@ describe("captureSubagentCompletionReply", () => {
4447
vi.useRealTimers();
4548
});
4649

50+
it("captures the final assistant reply when it becomes available at the deadline", async () => {
51+
vi.useFakeTimers();
52+
try {
53+
const readSubagentOutput = vi
54+
.fn<(sessionKey: string) => Promise<string | undefined>>()
55+
.mockResolvedValueOnce(undefined)
56+
.mockResolvedValueOnce(undefined)
57+
.mockResolvedValueOnce("Requester-visible final result");
58+
59+
const pending = captureSubagentCompletionReplyUsing({
60+
sessionKey: "agent:main:subagent:child",
61+
maxWaitMs: 5,
62+
retryIntervalMs: 5,
63+
readSubagentOutput,
64+
});
65+
await vi.runAllTimersAsync();
66+
67+
await expect(pending).resolves.toBe("Requester-visible final result");
68+
expect(readSubagentOutput).toHaveBeenCalledTimes(3);
69+
expect(vi.getTimerCount()).toBe(0);
70+
} finally {
71+
vi.useRealTimers();
72+
}
73+
});
74+
75+
it("charges slow output reads against the bounded retry deadline", async () => {
76+
vi.useFakeTimers();
77+
try {
78+
const startedAt = performance.now();
79+
const readSubagentOutput = vi.fn(async () => {
80+
await new Promise<void>((resolve) => {
81+
setTimeout(resolve, 15);
82+
});
83+
return undefined;
84+
});
85+
86+
const pending = readLatestSubagentOutputWithRetryUsing({
87+
sessionKey: "agent:main:subagent:child",
88+
maxWaitMs: 25,
89+
retryIntervalMs: 10,
90+
readSubagentOutput,
91+
});
92+
await vi.runAllTimersAsync();
93+
94+
await expect(pending).resolves.toBeUndefined();
95+
expect(readSubagentOutput).toHaveBeenCalledTimes(2);
96+
expect(performance.now() - startedAt).toBe(40);
97+
expect(vi.getTimerCount()).toBe(0);
98+
} finally {
99+
vi.useRealTimers();
100+
}
101+
});
102+
47103
it("returns undefined when no completion output arrives before retry window closes", async () => {
48104
vi.useFakeTimers();
49105
const readSubagentOutput = vi
@@ -60,10 +116,53 @@ describe("captureSubagentCompletionReply", () => {
60116
const result = await pending;
61117

62118
expect(result).toBeUndefined();
63-
expect(readSubagentOutput).toHaveBeenCalledTimes(8);
119+
expect(readSubagentOutput).toHaveBeenCalledTimes(9);
64120
vi.useRealTimers();
65121
});
66122

123+
it.each([0, -5])("does not poll when the retry budget is %i ms", async (maxWaitMs) => {
124+
vi.useFakeTimers();
125+
try {
126+
const readSubagentOutput = vi.fn().mockResolvedValue(undefined);
127+
128+
await expect(
129+
captureSubagentCompletionReplyUsing({
130+
sessionKey: "agent:main:subagent:child",
131+
maxWaitMs,
132+
retryIntervalMs: 5,
133+
readSubagentOutput,
134+
}),
135+
).resolves.toBeUndefined();
136+
137+
expect(readSubagentOutput).toHaveBeenCalledTimes(1);
138+
expect(vi.getTimerCount()).toBe(0);
139+
} finally {
140+
vi.useRealTimers();
141+
}
142+
});
143+
144+
it("does not poll when waiting for a completion reply is disabled", async () => {
145+
vi.useFakeTimers();
146+
try {
147+
const readSubagentOutput = vi.fn().mockResolvedValue(undefined);
148+
149+
await expect(
150+
captureSubagentCompletionReplyUsing({
151+
sessionKey: "agent:main:subagent:child",
152+
waitForReply: false,
153+
maxWaitMs: 25,
154+
retryIntervalMs: 5,
155+
readSubagentOutput,
156+
}),
157+
).resolves.toBeUndefined();
158+
159+
expect(readSubagentOutput).toHaveBeenCalledTimes(1);
160+
expect(vi.getTimerCount()).toBe(0);
161+
} finally {
162+
vi.useRealTimers();
163+
}
164+
});
165+
67166
it("returns partial assistant progress when the latest assistant turn is tool-only", async () => {
68167
const readSubagentOutput = vi
69168
.fn<(sessionKey: string) => Promise<string | undefined>>()

0 commit comments

Comments
 (0)