fix: preserve reset hook sender policy context

This commit is contained in:
Tak Hoffman
2026-04-10 17:55:45 -05:00
parent f3abc0c076
commit 5d1f1d9362
4 changed files with 56 additions and 7 deletions

View File

@@ -6,6 +6,9 @@ import type { HandleCommandsParams } from "./commands-types.js";
import { parseInlineDirectives } from "./directive-handling.parse.js";
const triggerInternalHookMock = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
const routeReplyMock = vi.hoisted(() =>
vi.fn<(params: unknown) => Promise<{ ok: boolean }>>(async () => ({ ok: true })),
);
const resetMocks = vi.hoisted(() => ({
resetConfiguredBindingTargetInPlace: vi.fn().mockResolvedValue({ ok: true as const }),
resolveBoundAcpThreadSessionKey: vi.fn(() => undefined as string | undefined),
@@ -49,6 +52,10 @@ vi.mock("./commands-handlers.runtime.js", () => ({
loadCommandHandlers: () => [],
}));
vi.mock("./route-reply.runtime.js", () => ({
routeReply: (params: unknown) => routeReplyMock(params),
}));
function buildResetParams(
commandBody: string,
cfg: OpenClawConfig,
@@ -102,6 +109,7 @@ describe("handleCommands reset hooks", () => {
vi.clearAllMocks();
resetMocks.resetConfiguredBindingTargetInPlace.mockResolvedValue({ ok: true });
resetMocks.resolveBoundAcpThreadSessionKey.mockReturnValue(undefined);
triggerInternalHookMock.mockResolvedValue(undefined);
});
it("triggers hooks for /new commands", async () => {
@@ -213,4 +221,38 @@ describe("handleCommands reset hooks", () => {
expect(params.ctx.CommandBody).toBe("who are you");
expect(params.ctx.AcpDispatchTailAfterReset).toBe(true);
});
it("forwards non-id sender fields when reset hooks emit routed replies", async () => {
triggerInternalHookMock.mockImplementationOnce(async (event: { messages: string[] }) => {
event.messages.push("Reset hook says hi");
});
const params = buildResetParams(
"/new",
{
commands: { text: true },
channels: { whatsapp: { allowFrom: ["*"] } },
} as OpenClawConfig,
{
SenderId: "id:whatsapp:123",
SenderName: "Alice",
SenderUsername: "alice_u",
SenderE164: "+15551234567",
OriginatingChannel: "whatsapp",
OriginatingTo: "group:ops",
MessageThreadId: "thread-1",
},
);
await maybeHandleResetCommand(params);
expect(routeReplyMock).toHaveBeenCalledWith(
expect.objectContaining({
requesterSenderId: "id:whatsapp:123",
requesterSenderName: "Alice",
requesterSenderUsername: "alice_u",
requesterSenderE164: "+15551234567",
threadId: "thread-1",
}),
);
});
});

View File

@@ -128,6 +128,9 @@ export async function emitResetCommandHooks(params: {
sessionKey: params.sessionKey,
accountId: params.ctx.AccountId,
requesterSenderId: params.command.senderId,
requesterSenderName: params.ctx.SenderName,
requesterSenderUsername: params.ctx.SenderUsername,
requesterSenderE164: params.ctx.SenderE164,
threadId: params.ctx.MessageThreadId,
cfg: params.cfg,
});