Auto-reply: fix non-default agent session transcript path resolution (#15154)

* Auto-reply: fix non-default agent transcript path resolution

* Auto-reply: harden non-default agent transcript lookups

* Auto-reply: harden session path resolution across agent stores
This commit is contained in:
Gustavo Madeira Santana
2026-02-12 23:23:12 -05:00
committed by GitHub
parent 79a38858ae
commit ac41176532
13 changed files with 321 additions and 16 deletions

View File

@@ -524,6 +524,84 @@ describe("runHeartbeatOnce", () => {
}
});
it("reuses non-default agent sessionFile from templated stores", async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-hb-"));
const storeTemplate = path.join(tmpDir, "agents", "{agentId}", "sessions", "sessions.json");
const replySpy = vi.spyOn(replyModule, "getReplyFromConfig");
const agentId = "ops";
try {
const cfg: OpenClawConfig = {
agents: {
defaults: {
heartbeat: { every: "30m", prompt: "Default prompt" },
},
list: [
{ id: "main", default: true },
{
id: agentId,
heartbeat: { every: "5m", target: "whatsapp", prompt: "Ops check" },
},
],
},
channels: { whatsapp: { allowFrom: ["*"] } },
session: { store: storeTemplate },
};
const sessionKey = resolveAgentMainSessionKey({ cfg, agentId });
const storePath = resolveStorePath(storeTemplate, { agentId });
const sessionsDir = path.dirname(storePath);
const sessionId = "sid-ops";
const sessionFile = path.join(sessionsDir, `${sessionId}.jsonl`);
await fs.mkdir(sessionsDir, { recursive: true });
await fs.writeFile(sessionFile, "", "utf-8");
await fs.writeFile(
storePath,
JSON.stringify(
{
[sessionKey]: {
sessionId,
sessionFile,
updatedAt: Date.now(),
lastChannel: "whatsapp",
lastTo: "+1555",
},
},
null,
2,
),
);
replySpy.mockResolvedValue([{ text: "Final alert" }]);
const sendWhatsApp = vi.fn().mockResolvedValue({
messageId: "m1",
toJid: "jid",
});
const result = await runHeartbeatOnce({
cfg,
agentId,
deps: {
sendWhatsApp,
getQueueSize: () => 0,
nowMs: () => 0,
webAuthExists: async () => true,
hasActiveWebListener: () => true,
},
});
expect(result.status).toBe("ran");
expect(sendWhatsApp).toHaveBeenCalledTimes(1);
expect(sendWhatsApp).toHaveBeenCalledWith("+1555", "Final alert", expect.any(Object));
expect(replySpy).toHaveBeenCalledWith(
expect.objectContaining({ SessionKey: sessionKey }),
{ isHeartbeat: true },
cfg,
);
} finally {
replySpy.mockRestore();
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
it("runs heartbeats in the explicit session key when configured", async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-hb-"));
const storePath = path.join(tmpDir, "sessions.json");