Skip to content

Commit 05e534e

Browse files
authored
fix(cron): deliver final results after heartbeat acknowledgements (#113924)
1 parent d1b4ed8 commit 05e534e

11 files changed

Lines changed: 229 additions & 209 deletions

‎src/cron/heartbeat-policy.test.ts‎

Lines changed: 0 additions & 43 deletions
This file was deleted.

‎src/cron/heartbeat-policy.ts‎

Lines changed: 0 additions & 37 deletions
This file was deleted.

‎src/cron/isolated-agent.direct-delivery-core-channels.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
} from "../channels/plugins/types.adapters.js";
99
import type { CliDeps } from "../cli/deps.js";
1010
import { clearRuntimeConfigSnapshot, setRuntimeConfigSnapshot } from "../config/config.js";
11+
import { callGateway } from "../gateway/call.js";
1112
import { resolveOutboundSendDep } from "../infra/outbound/send-deps.js";
1213
import { setActivePluginRegistry } from "../plugins/runtime.js";
1314
import { createOutboundTestPlugin, createTestRegistry } from "../test-utils/channel-plugins.js";
@@ -72,13 +73,15 @@ async function runExplicitAnnounceTurn(params: {
7273
cfg: ReturnType<typeof makeCfg>;
7374
deps: CliDeps;
7475
channel: ChannelCase["channel"];
76+
deleteAfterRun?: boolean;
7577
to: string;
7678
}) {
7779
return await runCronIsolatedAgentTurn({
7880
cfg: params.cfg,
7981
deps: params.deps,
8082
job: {
8183
...makeJob({ kind: "agentTurn", message: "do it" }),
84+
...(params.deleteAfterRun === true ? { deleteAfterRun: true } : {}),
8285
delivery: {
8386
mode: "announce",
8487
channel: params.channel,
@@ -119,6 +122,7 @@ function expectCoreChannelSendCall({
119122

120123
async function expectCoreChannelAnnounceDelivery({
121124
assertSend,
125+
deleteAfterRun,
122126
meta,
123127
payloads,
124128
testCase,
@@ -127,6 +131,7 @@ async function expectCoreChannelAnnounceDelivery({
127131
meta?: Parameters<typeof mockAgentPayloads>[1];
128132
payloads: Parameters<typeof mockAgentPayloads>[0];
129133
testCase: ChannelCase;
134+
deleteAfterRun?: boolean;
130135
}): Promise<void> {
131136
await withTempCronHome(async (home) => {
132137
const storePath = await writeSessionStore(home, { lastProvider: "webchat", lastTo: "" });
@@ -142,6 +147,7 @@ async function expectCoreChannelAnnounceDelivery({
142147
cfg,
143148
deps,
144149
channel: testCase.channel,
150+
deleteAfterRun,
145151
to: testCase.to,
146152
});
147153

@@ -322,6 +328,33 @@ describe("runCronIsolatedAgentTurn core-channel direct delivery", () => {
322328
clearRuntimeConfigSnapshot();
323329
});
324330

331+
it("delivers only the final Slack result after an earlier heartbeat acknowledgement", async () => {
332+
const slack = CASES[0];
333+
if (!slack) {
334+
throw new Error("expected Slack channel case");
335+
}
336+
const finalResult = "Critical deployment failure: database unavailable.";
337+
await expectCoreChannelAnnounceDelivery({
338+
testCase: slack,
339+
deleteAfterRun: true,
340+
payloads: [{ text: "HEARTBEAT_OK" }, { text: finalResult }],
341+
meta: { meta: makeRunMeta(finalResult) },
342+
assertSend: (sendFn, cfg) => {
343+
expect(sendFn).toHaveBeenCalledTimes(1);
344+
expectCoreChannelSendCall({
345+
cfg,
346+
expectedText: finalResult,
347+
expectedTo: slack.expectedTo,
348+
sendFn,
349+
sentAt: 0,
350+
});
351+
},
352+
});
353+
expect(callGateway).toHaveBeenCalledWith(
354+
expect.objectContaining({ method: "sessions.delete" }),
355+
);
356+
});
357+
325358
for (const testCase of CASES) {
326359
it(`routes ${testCase.name} text-only announce delivery through the outbound adapter`, async () => {
327360
await expectCoreChannelAnnounceDelivery({

‎src/cron/isolated-agent.helpers.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,74 @@ describe("resolveCronPayloadOutcome", () => {
376376
]);
377377
});
378378

379+
it("removes an earlier heartbeat acknowledgement from a substantive final result", () => {
380+
const result = resolveCronPayloadOutcome({
381+
payloads: [{ text: "HEARTBEAT_OK" }, { text: "Critical deployment failure" }],
382+
finalAssistantVisibleText: "Critical deployment failure",
383+
});
384+
385+
expect(result.deliveryPayloads).toEqual([{ text: "Critical deployment failure" }]);
386+
expect(result.deliveryDisposition).toEqual({ kind: "visible" });
387+
});
388+
389+
it("uses producer-owned terminal text when trailing empty payloads follow a result", () => {
390+
const result = resolveCronPayloadOutcome({
391+
payloads: [{ text: "Critical deployment failure" }, { text: " " }],
392+
finalAssistantVisibleText: "Critical deployment failure",
393+
});
394+
395+
expect(result.deliveryPayloads).toEqual([{ text: "Critical deployment failure" }]);
396+
expect(result.deliveryDisposition).toEqual({ kind: "visible" });
397+
});
398+
399+
it("keeps a terminal heartbeat acknowledgement intentionally quiet", () => {
400+
const payloads = [{ text: "Checked inbox and calendar." }, { text: "HEARTBEAT_OK" }];
401+
const result = resolveCronPayloadOutcome({
402+
payloads,
403+
finalAssistantVisibleText: "HEARTBEAT_OK",
404+
});
405+
406+
expect(result.deliveryPayloads).toEqual(payloads);
407+
expect(result.deliveryDisposition).toEqual({ kind: "heartbeat", controlOnly: false });
408+
});
409+
410+
it("records a pure heartbeat acknowledgement as a control-only terminal", () => {
411+
const result = resolveCronPayloadOutcome({
412+
payloads: [{ text: "HEARTBEAT_OK" }],
413+
finalAssistantVisibleText: "HEARTBEAT_OK",
414+
});
415+
416+
expect(result.deliveryDisposition).toEqual({ kind: "heartbeat", controlOnly: true });
417+
});
418+
419+
it("preserves structured output while removing a sibling heartbeat acknowledgement", () => {
420+
const mediaPayload = {
421+
text: "Here's the report",
422+
mediaUrl: "https://example.com/report.png",
423+
};
424+
const result = resolveCronPayloadOutcome({
425+
payloads: [{ text: "HEARTBEAT_OK" }, mediaPayload],
426+
finalAssistantVisibleText: "HEARTBEAT_OK",
427+
});
428+
429+
expect(result.deliveryPayloads).toEqual([mediaPayload]);
430+
expect(result.deliveryDisposition).toEqual({ kind: "visible" });
431+
});
432+
433+
it("keeps a heartbeat-labelled payload when the same payload carries media", () => {
434+
const mediaPayload = {
435+
text: "HEARTBEAT_OK",
436+
mediaUrl: "https://example.com/report.png",
437+
};
438+
const result = resolveCronPayloadOutcome({
439+
payloads: [mediaPayload],
440+
finalAssistantVisibleText: "HEARTBEAT_OK",
441+
});
442+
443+
expect(result.deliveryPayloads).toEqual([mediaPayload]);
444+
expect(result.deliveryDisposition).toEqual({ kind: "visible" });
445+
});
446+
379447
it("does not promote narrated denial markers in summary text to fatal errors", () => {
380448
const result = resolveCronPayloadOutcome({
381449
payloads: [
Lines changed: 1 addition & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Isolated agent helper tests cover low-level cron agent utilities.
22
import { describe, expect, it } from "vitest";
3-
import { isHeartbeatOnlyResponse, pickLastNonEmptyTextFromPayloads } from "./helpers.js";
3+
import { pickLastNonEmptyTextFromPayloads } from "./helpers.js";
44

55
type TextPayload = { text?: string | undefined; isError?: boolean | undefined };
66

@@ -44,81 +44,3 @@ describe("text payload pickers", () => {
4444
expect(pick(payloads)).toBe(expected);
4545
});
4646
});
47-
48-
describe("isHeartbeatOnlyResponse", () => {
49-
const ACK_MAX = 300;
50-
51-
it("returns true for empty payloads", () => {
52-
expect(isHeartbeatOnlyResponse([], ACK_MAX)).toBe(true);
53-
});
54-
55-
it("returns true for a single HEARTBEAT_OK payload", () => {
56-
expect(isHeartbeatOnlyResponse([{ text: "HEARTBEAT_OK" }], ACK_MAX)).toBe(true);
57-
});
58-
59-
it("returns false for a single non-heartbeat payload", () => {
60-
expect(isHeartbeatOnlyResponse([{ text: "Something important happened" }], ACK_MAX)).toBe(
61-
false,
62-
);
63-
});
64-
65-
it("returns true when multiple payloads include narration followed by HEARTBEAT_OK", () => {
66-
// Agent narrates its work then signals nothing needs attention.
67-
expect(
68-
isHeartbeatOnlyResponse(
69-
[
70-
{ text: "It's 12:49 AM — quiet hours. Let me run the checks quickly." },
71-
{ text: "Emails: Just 2 calendar invites. Not urgent." },
72-
{ text: "HEARTBEAT_OK" },
73-
],
74-
ACK_MAX,
75-
),
76-
).toBe(true);
77-
});
78-
79-
it("returns false when media is present even with HEARTBEAT_OK text", () => {
80-
expect(
81-
isHeartbeatOnlyResponse(
82-
[{ text: "HEARTBEAT_OK", mediaUrl: "https://example.com/img.png" }],
83-
ACK_MAX,
84-
),
85-
).toBe(false);
86-
});
87-
88-
it("returns false when rich content is present even with HEARTBEAT_OK text", () => {
89-
expect(
90-
isHeartbeatOnlyResponse(
91-
[
92-
{
93-
text: "HEARTBEAT_OK",
94-
presentation: {
95-
blocks: [{ type: "buttons", buttons: [{ label: "Open", value: "open" }] }],
96-
},
97-
},
98-
],
99-
ACK_MAX,
100-
),
101-
).toBe(false);
102-
});
103-
104-
it("returns false when media is in a different payload than HEARTBEAT_OK", () => {
105-
expect(
106-
isHeartbeatOnlyResponse(
107-
[
108-
{ text: "HEARTBEAT_OK" },
109-
{ text: "Here's an image", mediaUrl: "https://example.com/img.png" },
110-
],
111-
ACK_MAX,
112-
),
113-
).toBe(false);
114-
});
115-
116-
it("returns false when no payload contains HEARTBEAT_OK", () => {
117-
expect(
118-
isHeartbeatOnlyResponse(
119-
[{ text: "Checked emails — found 3 urgent messages from your manager." }],
120-
ACK_MAX,
121-
),
122-
).toBe(false);
123-
});
124-
});

0 commit comments

Comments
 (0)