Skip to content

Commit 37c9ac3

Browse files
author
OpenClaw Updater
committed
fix: preserve delivery metadata during send compensation
1 parent 6e43ccc commit 37c9ac3

3 files changed

Lines changed: 109 additions & 5 deletions

File tree

‎src/agents/run-wait.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,9 @@ export async function waitForAgentRunsToDrain(params: {
267267
}
268268

269269
/**
270-
* Default reply history limit used for post-timeout compensation reads.
271-
* Must match SESSIONS_SEND_REPLY_HISTORY_LIMIT in sessions-send-tool.ts.
270+
* Fallback reply history limit for post-timeout compensation reads when
271+
* the caller does not pass an explicit limit. The primary sessions_send
272+
* call site passes SESSIONS_SEND_REPLY_HISTORY_LIMIT directly.
272273
*/
273274
const COMPENSATE_REPLY_HISTORY_LIMIT = 50;
274275

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,10 @@ export function createSessionsSendTool(opts?: {
546546
status: "ok",
547547
reply: compensation.newReply,
548548
sessionKey: displayKey,
549-
delivery: compensation.delivery,
549+
delivery: {
550+
...delivery,
551+
note: compensation.delivery.note,
552+
},
550553
});
551554
}
552555
if (!isTerminalAgentWaitTimeout(result)) {
@@ -555,13 +558,20 @@ export function createSessionsSendTool(opts?: {
555558
runId,
556559
status: "accepted",
557560
sessionKey: displayKey,
558-
delivery: compensation.delivery,
561+
delivery: {
562+
...delivery,
563+
note: compensation.delivery.note,
564+
},
559565
});
560566
}
567+
const timeoutError =
568+
!compensation.accepted && compensation.error
569+
? `${result.error ?? "agent run timed out"}; post-timeout compensation failed: ${compensation.error}`
570+
: result.error;
561571
return jsonResult({
562572
runId,
563573
status: "timeout",
564-
error: result.error,
574+
error: timeoutError,
565575
sessionKey: displayKey,
566576
});
567577
}

‎src/agents/tools/sessions.test.ts‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,4 +864,97 @@ describe("sessions_send gating", () => {
864864
expect(details.reply).toBeUndefined();
865865
expect(details.sessionKey).toBe(MAIN_AGENT_SESSION_KEY);
866866
});
867+
868+
it("preserves announce delivery metadata on accepted timeout compensation", async () => {
869+
const tool = createMainSessionsSendTool();
870+
let historyCalls = 0;
871+
const staleAssistantMessage = {
872+
role: "assistant",
873+
content: [{ type: "text", text: "older reply" }],
874+
timestamp: 20,
875+
};
876+
877+
callGatewayMock.mockImplementation(async (opts: unknown) => {
878+
const request = opts as { method?: string };
879+
if (request.method === "sessions.list") {
880+
return {
881+
path: "/tmp/sessions.json",
882+
sessions: [{ key: MAIN_AGENT_SESSION_KEY, kind: "direct" }],
883+
};
884+
}
885+
if (request.method === "agent") {
886+
return { runId: "run-compensated", acceptedAt: 123 };
887+
}
888+
if (request.method === "agent.wait") {
889+
return { runId: "run-compensated", status: "timeout", error: "soft wait timed out" };
890+
}
891+
if (request.method === "chat.history") {
892+
historyCalls += 1;
893+
return { messages: [staleAssistantMessage] };
894+
}
895+
return {};
896+
});
897+
898+
const result = await tool.execute("call-compensated-timeout", {
899+
sessionKey: MAIN_AGENT_SESSION_KEY,
900+
message: "ping",
901+
timeoutSeconds: 1,
902+
});
903+
904+
expect(historyCalls).toBe(2);
905+
const details = requireDetails(result);
906+
expect(details.status).toBe("accepted");
907+
expect(details.delivery).toMatchObject({
908+
status: "pending",
909+
mode: "announce",
910+
note: "run accepted, no reply within timeout",
911+
});
912+
});
913+
914+
it("reports compensation read failures on terminal timeouts", async () => {
915+
const tool = createMainSessionsSendTool();
916+
let historyCalls = 0;
917+
918+
callGatewayMock.mockImplementation(async (opts: unknown) => {
919+
const request = opts as { method?: string };
920+
if (request.method === "sessions.list") {
921+
return {
922+
path: "/tmp/sessions.json",
923+
sessions: [{ key: MAIN_AGENT_SESSION_KEY, kind: "direct" }],
924+
};
925+
}
926+
if (request.method === "agent") {
927+
return { runId: "run-terminal-timeout", acceptedAt: 123 };
928+
}
929+
if (request.method === "agent.wait") {
930+
return {
931+
runId: "run-terminal-timeout",
932+
status: "timeout",
933+
error: "agent wait timed out",
934+
endedAt: 1_780_000_000_000,
935+
};
936+
}
937+
if (request.method === "chat.history") {
938+
historyCalls += 1;
939+
if (historyCalls === 1) {
940+
return { messages: [] };
941+
}
942+
throw new Error("history unavailable");
943+
}
944+
return {};
945+
});
946+
947+
const result = await tool.execute("call-terminal-timeout", {
948+
sessionKey: MAIN_AGENT_SESSION_KEY,
949+
message: "ping",
950+
timeoutSeconds: 1,
951+
});
952+
953+
expect(historyCalls).toBe(2);
954+
const details = requireDetails(result);
955+
expect(details.status).toBe("timeout");
956+
expect(details.error).toBe(
957+
"agent wait timed out; post-timeout compensation failed: history unavailable",
958+
);
959+
});
867960
});

0 commit comments

Comments
 (0)