This commit is contained in:
JoelCooperPhD 2026-01-29 16:02:49 -07:00 committed by GitHub
commit 714b96880e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 100 additions and 14 deletions

View File

@ -12,6 +12,7 @@ import {
resolveStorePath, resolveStorePath,
} from "../config/sessions.js"; } from "../config/sessions.js";
import { buildAgentPeerSessionKey } from "../routing/session-key.js"; import { buildAgentPeerSessionKey } from "../routing/session-key.js";
import { enqueueSystemEvent, resetSystemEventsForTest } from "./system-events.js";
import { import {
isHeartbeatEnabledForAgent, isHeartbeatEnabledForAgent,
resolveHeartbeatIntervalMs, resolveHeartbeatIntervalMs,
@ -31,6 +32,7 @@ import { setWhatsAppRuntime } from "../../extensions/whatsapp/src/runtime.js";
vi.mock("jiti", () => ({ createJiti: () => () => ({}) })); vi.mock("jiti", () => ({ createJiti: () => () => ({}) }));
beforeEach(() => { beforeEach(() => {
resetSystemEventsForTest();
const runtime = createPluginRuntime(); const runtime = createPluginRuntime();
setTelegramRuntime(runtime); setTelegramRuntime(runtime);
setWhatsAppRuntime(runtime); setWhatsAppRuntime(runtime);
@ -871,6 +873,77 @@ describe("runHeartbeatOnce", () => {
} }
}); });
it("runs heartbeat when HEARTBEAT.md is effectively empty but system events are queued", async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "moltbot-hb-"));
const storePath = path.join(tmpDir, "sessions.json");
const workspaceDir = path.join(tmpDir, "workspace");
const replySpy = vi.spyOn(replyModule, "getReplyFromConfig");
try {
await fs.mkdir(workspaceDir, { recursive: true });
// Create effectively empty HEARTBEAT.md (only headers)
await fs.writeFile(
path.join(workspaceDir, "HEARTBEAT.md"),
"# HEARTBEAT.md\n\n## Tasks\n\n",
"utf-8",
);
const cfg: MoltbotConfig = {
agents: {
defaults: {
workspace: workspaceDir,
heartbeat: { every: "5m", target: "whatsapp" },
},
},
channels: { whatsapp: { allowFrom: ["*"] } },
session: { store: storePath },
};
const sessionKey = resolveMainSessionKey(cfg);
await fs.writeFile(
storePath,
JSON.stringify(
{
[sessionKey]: {
sessionId: "sid",
updatedAt: Date.now(),
lastChannel: "whatsapp",
lastTo: "+1555",
},
},
null,
2,
),
);
enqueueSystemEvent("Cron: reminder fired", { sessionKey });
replySpy.mockResolvedValue([{ text: "Cron handled" }]);
const sendWhatsApp = vi.fn().mockResolvedValue({
messageId: "m1",
toJid: "jid",
});
const res = await runHeartbeatOnce({
cfg,
deps: {
sendWhatsApp,
getQueueSize: () => 0,
nowMs: () => 0,
webAuthExists: async () => true,
hasActiveWebListener: () => true,
},
});
expect(res.status).toBe("ran");
expect(replySpy).toHaveBeenCalled();
expect(sendWhatsApp).toHaveBeenCalled();
} finally {
replySpy.mockRestore();
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
it("runs heartbeat when HEARTBEAT.md has actionable content", async () => { it("runs heartbeat when HEARTBEAT.md has actionable content", async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "moltbot-hb-")); const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "moltbot-hb-"));
const storePath = path.join(tmpDir, "sessions.json"); const storePath = path.join(tmpDir, "sessions.json");

View File

@ -35,7 +35,7 @@ import {
} from "../config/sessions.js"; } from "../config/sessions.js";
import type { AgentDefaultsConfig } from "../config/types.agent-defaults.js"; import type { AgentDefaultsConfig } from "../config/types.agent-defaults.js";
import { formatErrorMessage } from "../infra/errors.js"; import { formatErrorMessage } from "../infra/errors.js";
import { peekSystemEvents } from "../infra/system-events.js"; import { hasSystemEvents, peekSystemEvents } from "../infra/system-events.js";
import { createSubsystemLogger } from "../logging/subsystem.js"; import { createSubsystemLogger } from "../logging/subsystem.js";
import { getQueueSize } from "../process/command-queue.js"; import { getQueueSize } from "../process/command-queue.js";
import { CommandLane } from "../process/lanes.js"; import { CommandLane } from "../process/lanes.js";
@ -308,7 +308,7 @@ function resolveHeartbeatAckMaxChars(cfg: MoltbotConfig, heartbeat?: HeartbeatCo
); );
} }
function resolveHeartbeatSession( function resolveHeartbeatSessionKey(
cfg: MoltbotConfig, cfg: MoltbotConfig,
agentId?: string, agentId?: string,
heartbeat?: HeartbeatConfig, heartbeat?: HeartbeatConfig,
@ -318,23 +318,19 @@ function resolveHeartbeatSession(
const resolvedAgentId = normalizeAgentId(agentId ?? resolveDefaultAgentId(cfg)); const resolvedAgentId = normalizeAgentId(agentId ?? resolveDefaultAgentId(cfg));
const mainSessionKey = const mainSessionKey =
scope === "global" ? "global" : resolveAgentMainSessionKey({ cfg, agentId: resolvedAgentId }); scope === "global" ? "global" : resolveAgentMainSessionKey({ cfg, agentId: resolvedAgentId });
const storeAgentId = scope === "global" ? resolveDefaultAgentId(cfg) : resolvedAgentId;
const storePath = resolveStorePath(sessionCfg?.store, { agentId: storeAgentId });
const store = loadSessionStore(storePath);
const mainEntry = store[mainSessionKey];
if (scope === "global") { if (scope === "global") {
return { sessionKey: mainSessionKey, storePath, store, entry: mainEntry }; return mainSessionKey;
} }
const trimmed = heartbeat?.session?.trim() ?? ""; const trimmed = heartbeat?.session?.trim() ?? "";
if (!trimmed) { if (!trimmed) {
return { sessionKey: mainSessionKey, storePath, store, entry: mainEntry }; return mainSessionKey;
} }
const normalized = trimmed.toLowerCase(); const normalized = trimmed.toLowerCase();
if (normalized === "main" || normalized === "global") { if (normalized === "main" || normalized === "global") {
return { sessionKey: mainSessionKey, storePath, store, entry: mainEntry }; return mainSessionKey;
} }
const candidate = toAgentStoreSessionKey({ const candidate = toAgentStoreSessionKey({
@ -350,11 +346,27 @@ function resolveHeartbeatSession(
if (canonical !== "global") { if (canonical !== "global") {
const sessionAgentId = resolveAgentIdFromSessionKey(canonical); const sessionAgentId = resolveAgentIdFromSessionKey(canonical);
if (sessionAgentId === normalizeAgentId(resolvedAgentId)) { if (sessionAgentId === normalizeAgentId(resolvedAgentId)) {
return { sessionKey: canonical, storePath, store, entry: store[canonical] }; return canonical;
} }
} }
return { sessionKey: mainSessionKey, storePath, store, entry: mainEntry }; return mainSessionKey;
}
function resolveHeartbeatSession(
cfg: MoltbotConfig,
agentId?: string,
heartbeat?: HeartbeatConfig,
) {
const sessionCfg = cfg.session;
const scope = sessionCfg?.scope ?? "per-sender";
const resolvedAgentId = normalizeAgentId(agentId ?? resolveDefaultAgentId(cfg));
const storeAgentId = scope === "global" ? resolveDefaultAgentId(cfg) : resolvedAgentId;
const storePath = resolveStorePath(sessionCfg?.store, { agentId: storeAgentId });
const store = loadSessionStore(storePath);
const sessionKey = resolveHeartbeatSessionKey(cfg, resolvedAgentId, heartbeat);
const entry = store[sessionKey];
return { sessionKey, storePath, store, entry };
} }
function resolveHeartbeatReplyPayload( function resolveHeartbeatReplyPayload(
@ -459,13 +471,14 @@ export async function runHeartbeatOnce(opts: {
// Skip heartbeat if HEARTBEAT.md exists but has no actionable content. // Skip heartbeat if HEARTBEAT.md exists but has no actionable content.
// This saves API calls/costs when the file is effectively empty (only comments/headers). // This saves API calls/costs when the file is effectively empty (only comments/headers).
// EXCEPTION: Don't skip for exec events - they have pending system events to process. // EXCEPTION: Don't skip when pending system events exist.
const isExecEventReason = opts.reason === "exec-event"; const heartbeatSessionKey = resolveHeartbeatSessionKey(cfg, agentId, heartbeat);
const hasPendingSystemEvents = hasSystemEvents(heartbeatSessionKey);
const workspaceDir = resolveAgentWorkspaceDir(cfg, agentId); const workspaceDir = resolveAgentWorkspaceDir(cfg, agentId);
const heartbeatFilePath = path.join(workspaceDir, DEFAULT_HEARTBEAT_FILENAME); const heartbeatFilePath = path.join(workspaceDir, DEFAULT_HEARTBEAT_FILENAME);
try { try {
const heartbeatFileContent = await fs.readFile(heartbeatFilePath, "utf-8"); const heartbeatFileContent = await fs.readFile(heartbeatFilePath, "utf-8");
if (isHeartbeatContentEffectivelyEmpty(heartbeatFileContent) && !isExecEventReason) { if (isHeartbeatContentEffectivelyEmpty(heartbeatFileContent) && !hasPendingSystemEvents) {
emitHeartbeatEvent({ emitHeartbeatEvent({
status: "skipped", status: "skipped",
reason: "empty-heartbeat-file", reason: "empty-heartbeat-file",