Skip to content

Commit 455d5e8

Browse files
fix(reply): keep visible turn admission unbounded (#87044)
Remove the hidden 15s default from reply-run idle waits so visible user turns do not inherit cleanup-settle behavior while waiting behind an active same-session reply operation. Keep the 15s timeout explicit for queued follow-up retry/defer paths and interrupt/reset cleanup waits, and add reply-admission regressions for both visible and queued follow-up behavior. Also preserve the original cause on a nearby sessions-send fallback error to keep current lint green after rebasing onto main. Thanks @keshavbotagent. Co-authored-by: Keshav's Bot <[email protected]>
1 parent 4b40197 commit 455d5e8

4 files changed

Lines changed: 81 additions & 7 deletions

File tree

‎src/auto-reply/reply/get-reply-run.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import { buildReplyPromptEnvelope, buildReplyPromptEnvelopeBase } from "./prompt
8484
import { resolveActiveRunQueueAction } from "./queue-policy.js";
8585
import { resolveQueueSettings } from "./queue/settings-runtime.js";
8686
import {
87+
REPLY_RUN_IDLE_SETTLE_TIMEOUT_MS,
8788
abortReplyRunBySessionId,
8889
isReplyRunActiveForSessionId,
8990
isReplyRunStreamingForSessionId,
@@ -1106,7 +1107,7 @@ export async function runPreparedReply(
11061107
},
11071108
waitForActiveRunEnd: (activeRunSessionId) =>
11081109
isReplyRunActiveForSessionId(activeRunSessionId)
1109-
? waitForReplyRunEndBySessionId(activeRunSessionId)
1110+
? waitForReplyRunEndBySessionId(activeRunSessionId, REPLY_RUN_IDLE_SETTLE_TIMEOUT_MS)
11101111
: (piRuntime?.waitForEmbeddedPiRunEnd(activeRunSessionId) ?? Promise.resolve(undefined)),
11111112
refreshPreparedState: async () => {
11121113
preparedSessionState = resolvePreparedSessionState();

‎src/auto-reply/reply/reply-run-registry.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ const replyRunState = resolveGlobalSingleton<ReplyRunState>(REPLY_RUN_STATE_KEY,
110110
waitersByKey: new Map<string, Set<ReplyRunWaiter>>(),
111111
}));
112112

113+
export const REPLY_RUN_IDLE_SETTLE_TIMEOUT_MS = 15_000;
114+
113115
export class ReplyRunAlreadyActiveError extends Error {
114116
constructor(sessionKey: string) {
115117
super(`Reply run already active for ${sessionKey}`);
@@ -440,7 +442,6 @@ export const replyRunRegistry: ReplyRunRegistry = {
440442
return true;
441443
},
442444
waitForIdle(sessionKey, timeoutMs, opts) {
443-
const effectiveTimeoutMs = timeoutMs ?? 15_000;
444445
const normalizedSessionKey = normalizeOptionalString(sessionKey);
445446
if (!normalizedSessionKey || !replyRunState.activeRunsByKey.has(normalizedSessionKey)) {
446447
return Promise.resolve(true);
@@ -471,8 +472,8 @@ export const replyRunRegistry: ReplyRunRegistry = {
471472
resolve(ended);
472473
},
473474
};
474-
if (Number.isFinite(effectiveTimeoutMs)) {
475-
waiter.timer = setTimeout(() => waiter.finish(false), Math.max(100, effectiveTimeoutMs));
475+
if (typeof timeoutMs === "number" && Number.isFinite(timeoutMs)) {
476+
waiter.timer = setTimeout(() => waiter.finish(false), Math.max(100, timeoutMs));
476477
}
477478
if (opts?.signal) {
478479
abortHandler = () => waiter.finish(false);
@@ -543,7 +544,7 @@ export function forceClearReplyRunBySessionId(sessionId: string, cause?: unknown
543544

544545
export function waitForReplyRunEndBySessionId(
545546
sessionId: string,
546-
timeoutMs = 15_000,
547+
timeoutMs: number,
547548
): Promise<boolean> {
548549
const waitKey = resolveReplyRunWaitKey(sessionId);
549550
if (!waitKey) {

‎src/auto-reply/reply/reply-turn-admission.test.ts‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterEach, describe, expect, it } from "vitest";
1+
import { afterEach, describe, expect, it, vi } from "vitest";
22
import { createReplyOperation, testing } from "./reply-run-registry.js";
33
import { admitReplyTurn } from "./reply-turn-admission.js";
44

@@ -39,6 +39,74 @@ describe("reply turn admission", () => {
3939
}
4040
});
4141

42+
it("does not apply cleanup settle timeout to visible turn admission", async () => {
43+
vi.useFakeTimers();
44+
try {
45+
const active = createReplyOperation({
46+
sessionKey: "agent:main:discord:channel:42",
47+
sessionId: "active-session",
48+
resetTriggered: false,
49+
});
50+
active.setPhase("running");
51+
52+
const admitted = admitReplyTurn({
53+
sessionKey: "agent:main:discord:channel:42",
54+
sessionId: "waiting-session",
55+
kind: "visible",
56+
resetTriggered: false,
57+
});
58+
59+
let settled = false;
60+
void admitted.then(() => {
61+
settled = true;
62+
});
63+
64+
await vi.advanceTimersByTimeAsync(15_000);
65+
expect(settled).toBe(false);
66+
67+
active.complete();
68+
const result = await admitted;
69+
expect(result.status).toBe("owned");
70+
if (result.status === "owned") {
71+
result.operation.complete();
72+
}
73+
} finally {
74+
await vi.runOnlyPendingTimersAsync();
75+
vi.useRealTimers();
76+
}
77+
});
78+
79+
it("keeps the cleanup settle timeout for queued follow-up retry", async () => {
80+
vi.useFakeTimers();
81+
try {
82+
const active = createReplyOperation({
83+
sessionKey: "agent:main:discord:channel:42",
84+
sessionId: "active-session",
85+
resetTriggered: false,
86+
});
87+
active.setPhase("running");
88+
89+
const admitted = admitReplyTurn({
90+
sessionKey: "agent:main:discord:channel:42",
91+
sessionId: "queued-session",
92+
kind: "queued_followup",
93+
resetTriggered: false,
94+
});
95+
96+
await vi.advanceTimersByTimeAsync(15_000);
97+
98+
await expect(admitted).resolves.toMatchObject({
99+
status: "skipped",
100+
reason: "active-run",
101+
activeOperation: active,
102+
});
103+
active.complete();
104+
} finally {
105+
await vi.runOnlyPendingTimersAsync();
106+
vi.useRealTimers();
107+
}
108+
});
109+
42110
it("uses the active run's final session id after waiting", async () => {
43111
const active = createReplyOperation({
44112
sessionKey: "agent:main:telegram:topic:42",

‎src/auto-reply/reply/reply-turn-admission.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
createReplyOperation,
3+
REPLY_RUN_IDLE_SETTLE_TIMEOUT_MS,
34
replyRunRegistry,
45
ReplyRunAlreadyActiveError,
56
type ReplyOperation,
@@ -54,7 +55,10 @@ export async function admitReplyTurn(params: {
5455
if (params.waitForActive === false) {
5556
return { status: "skipped", reason: "active-run", activeOperation };
5657
}
57-
const ended = await replyRunRegistry.waitForIdle(params.sessionKey, params.waitTimeoutMs, {
58+
const waitTimeoutMs =
59+
params.waitTimeoutMs ??
60+
(params.kind === "queued_followup" ? REPLY_RUN_IDLE_SETTLE_TIMEOUT_MS : undefined);
61+
const ended = await replyRunRegistry.waitForIdle(params.sessionKey, waitTimeoutMs, {
5862
signal: params.upstreamAbortSignal,
5963
});
6064
if (!ended) {

0 commit comments

Comments
 (0)