Skip to content

Commit 0078b90

Browse files
fix(heartbeat): honor message-tool delivery policy
Keep scheduled heartbeat turns aligned with group/channel message-tool visibility and model-specific runtime policy so internal tool errors remain private. Co-authored-by: Cursor <[email protected]>
1 parent 111bad1 commit 0078b90

3 files changed

Lines changed: 83 additions & 3 deletions

File tree

‎src/infra/heartbeat-runner.tool-response.test.ts‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,34 @@ describe("runHeartbeatOnce heartbeat response tool", () => {
3333
tmpDir: string;
3434
storePath: string;
3535
visibleReplies?: "automatic" | "message_tool";
36+
groupVisibleReplies?: "automatic" | "message_tool";
3637
agentRuntimeId?: string;
38+
modelRuntimeId?: string;
3739
model?: string;
40+
target?: "telegram" | "last";
3841
}): OpenClawConfig {
3942
return {
4043
agents: {
4144
defaults: {
4245
workspace: params.tmpDir,
43-
heartbeat: { every: "5m", target: "telegram" },
46+
heartbeat: { every: "5m", target: params.target ?? "telegram" },
4447
...(params.model ? { model: params.model } : {}),
48+
...(params.model && params.modelRuntimeId
49+
? { models: { [params.model]: { agentRuntime: { id: params.modelRuntimeId } } } }
50+
: {}),
4551
...(params.agentRuntimeId ? { agentRuntime: { id: params.agentRuntimeId } } : {}),
4652
},
4753
},
48-
...(params.visibleReplies ? { messages: { visibleReplies: params.visibleReplies } } : {}),
54+
...(params.visibleReplies || params.groupVisibleReplies
55+
? {
56+
messages: {
57+
...(params.visibleReplies ? { visibleReplies: params.visibleReplies } : {}),
58+
...(params.groupVisibleReplies
59+
? { groupChat: { visibleReplies: params.groupVisibleReplies } }
60+
: {}),
61+
},
62+
}
63+
: {}),
4964
channels: {
5065
telegram: {
5166
token: "test-token",
@@ -228,6 +243,15 @@ describe("runHeartbeatOnce heartbeat response tool", () => {
228243
expectHeartbeatToolPrompt(result, ["notify=false"]);
229244
});
230245

246+
it("uses the heartbeat response tool prompt for group message-tool mode", async () => {
247+
const result = await runPromptScenario({
248+
config: { groupVisibleReplies: "message_tool", target: "last" },
249+
session: { lastTo: "group:redacted" },
250+
});
251+
252+
expectHeartbeatToolPrompt(result, ["notify=false"]);
253+
});
254+
231255
it("uses the heartbeat response tool prompt for Codex harness sessions by default", async () => {
232256
const result = await runPromptScenario({
233257
session: { agentHarnessId: "codex" },
@@ -316,6 +340,31 @@ describe("runHeartbeatOnce heartbeat response tool", () => {
316340
expectHeartbeatToolPrompt(result);
317341
});
318342

343+
it("uses the heartbeat response tool prompt for model-specific Codex runtimes", async () => {
344+
const result = await runPromptScenario({
345+
config: {
346+
model: "openai/gpt-5.5",
347+
modelRuntimeId: "codex",
348+
},
349+
});
350+
351+
expectHeartbeatToolPrompt(result);
352+
});
353+
354+
it("honors model-specific non-Codex runtimes over default Codex heartbeat mode", async () => {
355+
const result = await runPromptScenario({
356+
config: {
357+
agentRuntimeId: "codex",
358+
model: "openai/gpt-5.5",
359+
modelRuntimeId: "pi",
360+
},
361+
});
362+
363+
expect(result.calledCtx.Body).toContain("HEARTBEAT_OK");
364+
expect(result.calledCtx.Body).not.toContain("heartbeat_respond");
365+
expect(result.calledOpts.sourceReplyDeliveryMode).toBeUndefined();
366+
});
367+
319368
it("uses the heartbeat response tool prompt when the Codex runtime is env-forced", async () => {
320369
vi.stubEnv("OPENCLAW_AGENT_RUNTIME", "codex");
321370
const result = await runPromptScenario({

‎src/infra/heartbeat-runner.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
resolveDefaultAgentId,
1414
} from "../agents/agent-scope.js";
1515
import { appendCronStyleCurrentTimeLine } from "../agents/current-time.js";
16+
import { resolveAgentHarnessPolicy } from "../agents/harness/policy.js";
1617
import { resolveModelRefFromString, type ModelRef } from "../agents/model-selection.js";
1718
import { resolveEmbeddedSessionLane } from "../agents/pi-embedded-runner/lanes.js";
1819
import { formatReasoningMessage } from "../agents/pi-embedded-utils.js";
@@ -39,6 +40,7 @@ import { replyRunRegistry } from "../auto-reply/reply/reply-run-registry.js";
3940
import { resolveResponsePrefixTemplate } from "../auto-reply/reply/response-prefix-template.js";
4041
import { HEARTBEAT_TOKEN } from "../auto-reply/tokens.js";
4142
import type { ReplyPayload } from "../auto-reply/types.js";
43+
import { normalizeChatType, type ChatType } from "../channels/chat-type.js";
4244
import { sendDurableMessageBatch } from "../channels/message/runtime.js";
4345
import { getChannelPlugin } from "../channels/plugins/index.js";
4446
import type {
@@ -451,6 +453,7 @@ function usesCodexHarness(params: {
451453
normalizeHeartbeatRuntimeId(process.env.OPENCLAW_AGENT_RUNTIME) ||
452454
normalizeHeartbeatRuntimeId(agentEntry?.agentRuntime?.id) ||
453455
normalizeHeartbeatRuntimeId(agentEntry?.embeddedHarness?.runtime) ||
456+
resolveConfiguredHeartbeatModelRuntimeId(params) ||
454457
normalizeHeartbeatRuntimeId(params.cfg.agents?.defaults?.agentRuntime?.id) ||
455458
normalizeHeartbeatRuntimeId(params.cfg.agents?.defaults?.embeddedHarness?.runtime);
456459
if (runtimeId === "codex") {
@@ -462,13 +465,38 @@ function usesCodexHarness(params: {
462465
return normalizeLowercaseStringOrEmpty(resolveHeartbeatModelRef(params).provider) === "codex";
463466
}
464467

468+
function resolveConfiguredHeartbeatModelRuntimeId(params: {
469+
cfg: OpenClawConfig;
470+
agentId: string;
471+
heartbeat?: HeartbeatConfig;
472+
entry?: SessionEntry;
473+
}): string {
474+
const modelRef = resolveHeartbeatModelRef(params);
475+
const policy = resolveAgentHarnessPolicy({
476+
config: params.cfg,
477+
provider: modelRef.provider,
478+
modelId: modelRef.model,
479+
agentId: params.agentId,
480+
});
481+
if (policy.runtimeSource === "implicit") {
482+
return "";
483+
}
484+
const runtime = normalizeHeartbeatRuntimeId(policy.runtime);
485+
return runtime === "auto" ? "" : runtime;
486+
}
487+
465488
function shouldUseHeartbeatResponseToolPrompt(params: {
466489
cfg: OpenClawConfig;
467490
agentId: string;
468491
heartbeat?: HeartbeatConfig;
469492
entry?: SessionEntry;
493+
chatType?: ChatType;
470494
}): boolean {
471-
const visibleReplies = params.cfg.messages?.visibleReplies;
495+
const chatType = normalizeChatType(params.chatType);
496+
const visibleReplies =
497+
chatType === "group" || chatType === "channel"
498+
? (params.cfg.messages?.groupChat?.visibleReplies ?? params.cfg.messages?.visibleReplies)
499+
: params.cfg.messages?.visibleReplies;
472500
if (visibleReplies === "message_tool") {
473501
return true;
474502
}
@@ -1462,6 +1490,7 @@ export async function runHeartbeatOnce(opts: {
14621490
agentId,
14631491
heartbeat,
14641492
entry,
1493+
chatType: delivery.chatType,
14651494
});
14661495
const {
14671496
prompt,

‎src/infra/outbound/targets.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type HeartbeatTarget = OutboundChannel;
3434
export type OutboundTarget = {
3535
channel: OutboundChannel;
3636
to?: string;
37+
chatType?: ChatType;
3738
reason?: string;
3839
accountId?: string;
3940
threadId?: string | number;
@@ -236,6 +237,7 @@ export function resolveHeartbeatDeliveryTarget(params: {
236237
return {
237238
channel: resolvedTarget.channel,
238239
to: resolved.to,
240+
chatType: deliveryChatType,
239241
reason,
240242
accountId: effectiveAccountId,
241243
// Heartbeats normally avoid inheriting session reply-thread IDs, but some

0 commit comments

Comments
 (0)