From 46a6d7978441275c1e49233c8e040e9ab070f361 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 12 Jan 2026 06:28:53 +0000 Subject: [PATCH] fix: sender fallback for command auth (#755) (thanks @juanpablodlc) --- CHANGELOG.md | 1 + src/agents/pi-embedded-runner.test.ts | 12 +++++++---- src/auto-reply/command-auth.test.ts | 30 +++++++++++++++++++++++++++ src/auto-reply/reply.triggers.test.ts | 15 +++++++++----- 4 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 src/auto-reply/command-auth.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index bd5732ae3..94852b1a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ - Agents: stop pre-creating session transcripts so first user messages persist in JSONL history. - Auto-reply: align `/think` default display with model reasoning defaults. (#751) — thanks @gabriel-trigo. - Auto-reply: flush block reply buffers on tool boundaries. (#750) — thanks @sebslight. +- Auto-reply: allow sender fallback for command authorization when `SenderId` is empty (WhatsApp self-chat). (#755) — thanks @juanpablodlc. - Heartbeat: refresh prompt text for updated defaults. - Agents/Tools: use PowerShell on Windows to capture system utility output. (#748) — thanks @myfunc. - Docker: tolerate unset optional env vars in docker-setup.sh under strict mode. (#725) — thanks @petradonka. diff --git a/src/agents/pi-embedded-runner.test.ts b/src/agents/pi-embedded-runner.test.ts index 52c1d64cd..eca180a80 100644 --- a/src/agents/pi-embedded-runner.test.ts +++ b/src/agents/pi-embedded-runner.test.ts @@ -726,7 +726,8 @@ describe("runEmbeddedPiAgent", () => { const messages = await readSessionMessages(sessionFile); const firstUserIndex = messages.findIndex( (message) => - message?.role === "user" && textFromContent(message.content) === "hello", + message?.role === "user" && + textFromContent(message.content) === "hello", ); const firstAssistantIndex = messages.findIndex( (message) => message?.role === "assistant", @@ -836,7 +837,8 @@ describe("runEmbeddedPiAgent", () => { ); const newUserIndex = messages.findIndex( (message) => - message?.role === "user" && textFromContent(message.content) === "hello", + message?.role === "user" && + textFromContent(message.content) === "hello", ); const newAssistantIndex = messages.findIndex( (message, index) => index > newUserIndex && message?.role === "assistant", @@ -887,10 +889,12 @@ describe("runEmbeddedPiAgent", () => { const messages = await readSessionMessages(sessionFile); const firstUserIndex = messages.findIndex( (message) => - message?.role === "user" && textFromContent(message.content) === "first", + message?.role === "user" && + textFromContent(message.content) === "first", ); const firstAssistantIndex = messages.findIndex( - (message, index) => index > firstUserIndex && message?.role === "assistant", + (message, index) => + index > firstUserIndex && message?.role === "assistant", ); const secondUserIndex = messages.findIndex( (message) => diff --git a/src/auto-reply/command-auth.test.ts b/src/auto-reply/command-auth.test.ts new file mode 100644 index 000000000..099cf552f --- /dev/null +++ b/src/auto-reply/command-auth.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from "vitest"; + +import type { ClawdbotConfig } from "../config/config.js"; +import { resolveCommandAuthorization } from "./command-auth.js"; +import type { MsgContext } from "./templating.js"; + +describe("resolveCommandAuthorization", () => { + it("falls back from empty SenderId to SenderE164", () => { + const cfg = { + whatsapp: { allowFrom: ["+123"] }, + } as ClawdbotConfig; + + const ctx = { + Provider: "whatsapp", + Surface: "whatsapp", + From: "whatsapp:+999", + SenderId: "", + SenderE164: "+123", + } as MsgContext; + + const auth = resolveCommandAuthorization({ + ctx, + cfg, + commandAuthorized: true, + }); + + expect(auth.senderId).toBe("+123"); + expect(auth.isAuthorizedSender).toBe(true); + }); +}); diff --git a/src/auto-reply/reply.triggers.test.ts b/src/auto-reply/reply.triggers.test.ts index 0f9662663..0c9582ea9 100644 --- a/src/auto-reply/reply.triggers.test.ts +++ b/src/auto-reply/reply.triggers.test.ts @@ -520,7 +520,7 @@ describe("trigger handling", () => { }); }); - it("handles inline /status and still runs the agent", async () => { + it("strips inline /status and still runs the agent", async () => { await withTempHome(async (home) => { vi.mocked(runEmbeddedPiAgent).mockResolvedValue({ payloads: [{ text: "ok" }], @@ -535,6 +535,9 @@ describe("trigger handling", () => { Body: "please /status now", From: "+1002", To: "+2000", + Provider: "whatsapp", + Surface: "whatsapp", + SenderE164: "+1002", }, { onBlockReply: async (payload) => { @@ -543,9 +546,11 @@ describe("trigger handling", () => { }, makeCfg(home), ); - expect(blockReplies.length).toBe(1); - expect(blockReplies[0]?.text).toBeTruthy(); expect(runEmbeddedPiAgent).toHaveBeenCalled(); + expect(blockReplies.length).toBe(0); + const prompt = + vi.mocked(runEmbeddedPiAgent).mock.calls[0]?.[0]?.prompt ?? ""; + expect(prompt).not.toContain("/status"); }); }); @@ -712,7 +717,7 @@ describe("trigger handling", () => { }); }); - it("keeps inline /status for unauthorized senders", async () => { + it("strips inline /status for unauthorized senders", async () => { await withTempHome(async (home) => { vi.mocked(runEmbeddedPiAgent).mockResolvedValue({ payloads: [{ text: "ok" }], @@ -749,7 +754,7 @@ describe("trigger handling", () => { expect(runEmbeddedPiAgent).toHaveBeenCalled(); const prompt = vi.mocked(runEmbeddedPiAgent).mock.calls[0]?.[0]?.prompt ?? ""; - expect(prompt).toContain("/status"); + expect(prompt).not.toContain("/status"); }); });