Skip to content

Commit bfe3a59

Browse files
fix(agents): post-timeout compensation in sessions_send (closes openclaw#68065)
sessions_send with a non-zero timeout previously returned status=timeout whenever the wait window expired, even when the target agent had already accepted the message and produced a reply after the window closed. The fix adds compensateAfterWaitTimeout() in run-wait.ts that reads the latest assistant snapshot after a timeout and checks whether a new reply materialized since the pre-send baseline. The timeout handler in sessions-send-tool.ts now uses this to return the correct status: - newReply found -> status=ok with the reply - accepted, no reply -> status=accepted - hard timeout -> status=timeout (unchanged)
1 parent be7a415 commit bfe3a59

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

‎src/agents/run-wait.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,50 @@ export async function waitForAgentRunsToDrain(params: {
218218
};
219219
}
220220

221+
/**
222+
* Default reply history limit used for post-timeout compensation reads.
223+
* Must match SESSIONS_SEND_REPLY_HISTORY_LIMIT in sessions-send-tool.ts.
224+
*/
225+
const COMPENSATE_REPLY_HISTORY_LIMIT = 50;
226+
227+
/**
228+
* After a wait timeout, check whether a new assistant reply has materialized
229+
* since the baseline snapshot was taken. Used to implement post-timeout
230+
* compensation so that callers can distinguish "accepted but reply not ready in
231+
* time" from genuine hard failures.
232+
*/
233+
export async function compensateAfterWaitTimeout(params: {
234+
sessionKey: string;
235+
baseline?: AssistantReplySnapshot;
236+
limit?: number;
237+
callGateway?: GatewayCaller;
238+
}): Promise<{
239+
newReply?: string;
240+
accepted: boolean;
241+
delivery: { status: "accepted" | "pending"; note?: string };
242+
}> {
243+
const latestReply = await readLatestAssistantReplySnapshot({
244+
sessionKey: params.sessionKey,
245+
limit: params.limit ?? COMPENSATE_REPLY_HISTORY_LIMIT,
246+
callGateway: params.callGateway,
247+
});
248+
249+
const hasNewReply =
250+
latestReply.text &&
251+
(!params.baseline?.fingerprint || latestReply.fingerprint !== params.baseline.fingerprint);
252+
253+
return {
254+
newReply: hasNewReply ? latestReply.text : undefined,
255+
accepted: true,
256+
delivery: {
257+
status: "accepted",
258+
note: hasNewReply
259+
? "reply arrived after timeout window"
260+
: "run accepted, no reply within timeout",
261+
},
262+
};
263+
}
264+
221265
export const __testing = {
222266
setDepsForTest(overrides?: Partial<{ callGateway: GatewayCaller }>) {
223267
runWaitDeps = overrides

‎src/agents/tools/sessions-send-tool.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { AGENT_LANE_NESTED } from "../lanes.js";
1414
import {
1515
readLatestAssistantReplySnapshot,
1616
waitForAgentRunAndReadUpdatedAssistantReply,
17+
compensateAfterWaitTimeout,
1718
} from "../run-wait.js";
1819
import {
1920
describeSessionsSendTool,
@@ -343,6 +344,34 @@ export function createSessionsSendTool(opts?: {
343344
});
344345

345346
if (result.status === "timeout") {
347+
// Post-timeout compensation: check whether the run was accepted and
348+
// a reply arrived after the wait window closed.
349+
const compensation = await compensateAfterWaitTimeout({
350+
sessionKey: resolvedKey,
351+
baseline: baselineReply,
352+
limit: SESSIONS_SEND_REPLY_HISTORY_LIMIT,
353+
callGateway: gatewayCall,
354+
});
355+
356+
if (compensation.newReply) {
357+
startA2AFlow(compensation.newReply);
358+
return jsonResult({
359+
runId,
360+
status: "ok",
361+
reply: compensation.newReply,
362+
sessionKey: displayKey,
363+
delivery: compensation.delivery,
364+
});
365+
}
366+
if (compensation.accepted) {
367+
return jsonResult({
368+
runId,
369+
status: "accepted",
370+
sessionKey: displayKey,
371+
delivery: compensation.delivery,
372+
});
373+
}
374+
// Hard timeout — run was not accepted or is truly stalled.
346375
return jsonResult({
347376
runId,
348377
status: "timeout",

0 commit comments

Comments
 (0)