Skip to content

Commit 5ef7d45

Browse files
committed
fix(agents): preserve restart-unsafe delivery evidence
1 parent cae9eca commit 5ef7d45

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎src/agents/command/delivery.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { OpenClawConfig } from "../../config/config.js";
1010
import { setActivePluginRegistry } from "../../plugins/runtime.js";
1111
import { createOutboundTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js";
1212
import { normalizeSessionDeliveryState } from "../../utils/delivery-context.shared.js";
13+
import { buildRestartRecoveryTerminalDeliveryEvidence } from "../agent-command-restart-recovery.js";
1314
import { createAgentRunRestartAbortError } from "../run-termination.js";
1415
import { deliverAgentCommandResult } from "./delivery.js";
1516
import type { AgentCommandOpts } from "./types.js";
@@ -766,6 +767,48 @@ describe("deliverAgentCommandResult payload normalization", () => {
766767
expect(deliverOutboundPayloadsMock).not.toHaveBeenCalled();
767768
});
768769

770+
it.each([
771+
{
772+
name: "deterministic approval prompt",
773+
result: { didSendDeterministicApprovalPrompt: true },
774+
field: "didSendDeterministicApprovalPrompt",
775+
expected: true,
776+
},
777+
{
778+
name: "accepted session spawn",
779+
result: {
780+
acceptedSessionSpawns: [
781+
{ runId: "child-run", childSessionKey: "agent:main:subagent:child" },
782+
],
783+
},
784+
field: "acceptedSessionSpawns",
785+
expected: [{ runId: "child-run", childSessionKey: "agent:main:subagent:child" }],
786+
},
787+
{
788+
name: "successful cron add",
789+
result: { successfulCronAdds: 1 },
790+
field: "successfulCronAdds",
791+
expected: 1,
792+
},
793+
])("preserves $name as restart-unsafe delivery evidence", async ({ result, field, expected }) => {
794+
const onDeliveryResult = vi.fn();
795+
const delivered = await deliverAgentCommandResultForTest({
796+
omitReplyTarget: true,
797+
opts: { deliver: false },
798+
payloads: [],
799+
result,
800+
onDeliveryResult,
801+
});
802+
803+
expect(delivered).toHaveProperty(field, expected);
804+
expect(onDeliveryResult).toHaveBeenCalledOnce();
805+
expect(onDeliveryResult).toHaveBeenCalledWith(delivered);
806+
expect(buildRestartRecoveryTerminalDeliveryEvidence(delivered)).toEqual({
807+
captured: true,
808+
restartUnsafeSideEffectsDetected: true,
809+
});
810+
});
811+
769812
it("does not automatically redeliver text and media already sent to the same target", async () => {
770813
const delivered = await deliverAgentCommandResultForTest({
771814
opts: { threadId: "171.222" },

‎src/agents/command/delivery.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ type AgentCommandDeliveryResult = {
111111
messagingToolSentTexts?: string[];
112112
messagingToolSentMediaUrls?: string[];
113113
messagingToolSentTargets?: MessagingToolSend[];
114+
didSendDeterministicApprovalPrompt?: true;
115+
acceptedSessionSpawns?: NonNullable<RunResult["acceptedSessionSpawns"]>;
116+
successfulCronAdds?: number;
114117
deliverySucceeded?: boolean;
115118
deliveryStatus?: AgentCommandDeliveryStatus;
116119
};
@@ -209,6 +212,11 @@ function buildDeliveryResult(params: {
209212
deliverySucceeded?: boolean;
210213
deliveryStatus?: AgentCommandDeliveryStatus;
211214
}): AgentCommandDeliveryResult {
215+
const successfulCronAdds = params.result.successfulCronAdds;
216+
const hasSuccessfulCronAdds =
217+
typeof successfulCronAdds === "number" &&
218+
Number.isFinite(successfulCronAdds) &&
219+
successfulCronAdds > 0;
212220
return {
213221
payloads: params.payloads,
214222
meta: params.meta,
@@ -222,6 +230,13 @@ function buildDeliveryResult(params: {
222230
...(hasNonEmptyArray(params.result.messagingToolSentTargets)
223231
? { messagingToolSentTargets: params.result.messagingToolSentTargets }
224232
: {}),
233+
...(params.result.didSendDeterministicApprovalPrompt === true
234+
? { didSendDeterministicApprovalPrompt: true }
235+
: {}),
236+
...(hasNonEmptyArray(params.result.acceptedSessionSpawns)
237+
? { acceptedSessionSpawns: params.result.acceptedSessionSpawns }
238+
: {}),
239+
...(hasSuccessfulCronAdds ? { successfulCronAdds } : {}),
225240
...(params.deliverySucceeded !== undefined
226241
? { deliverySucceeded: params.deliverySucceeded }
227242
: {}),

0 commit comments

Comments
 (0)