From 3ee823b2298d8be7b325f25fa12a11cf309b1582 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 6 Apr 2026 03:10:29 +0100 Subject: [PATCH] perf(test): trim send-policy and abort hot paths --- src/auto-reply/reply/abort.test.ts | 23 ----------------------- src/sessions/send-policy.ts | 30 +++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/auto-reply/reply/abort.test.ts b/src/auto-reply/reply/abort.test.ts index a5a3a575214..a1cb96ea998 100644 --- a/src/auto-reply/reply/abort.test.ts +++ b/src/auto-reply/reply/abort.test.ts @@ -20,7 +20,6 @@ import { } from "./abort.js"; import { enqueueFollowupRun, getFollowupQueueDepth, type FollowupRun } from "./queue.js"; import { __testing as queueCleanupTesting } from "./queue/cleanup.js"; -import { initSessionState } from "./session.js"; import { buildTestCtx } from "./test-ctx.js"; vi.mock("../../agents/pi-embedded.js", () => ({ @@ -200,28 +199,6 @@ describe("abort detection", () => { subagentRegistryMocks.getLatestSubagentRunByChildSessionKey.mockReset().mockReturnValue(null); }); - it("triggerBodyNormalized extracts /stop from RawBody for abort detection", async () => { - const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-abort-")); - const storePath = path.join(root, "sessions.json"); - const cfg = { session: { store: storePath } } as OpenClawConfig; - - const groupMessageCtx = { - Body: `[Context]\nJake: /stop\n[from: Jake]`, - RawBody: "/stop", - ChatType: "group", - SessionKey: "agent:main:whatsapp:group:g1", - }; - - const result = await initSessionState({ - ctx: groupMessageCtx, - cfg, - commandAuthorized: true, - }); - - // /stop is detected via exact match in handleAbort, not isAbortTrigger - expect(result.triggerBodyNormalized).toBe("/stop"); - }); - it("isAbortTrigger matches standalone abort trigger phrases", () => { const positives = [ "stop", diff --git a/src/sessions/send-policy.ts b/src/sessions/send-policy.ts index dbc7dedd3b5..3e36288628c 100644 --- a/src/sessions/send-policy.ts +++ b/src/sessions/send-policy.ts @@ -1,7 +1,6 @@ import { normalizeChatType } from "../channels/chat-type.js"; import type { OpenClawConfig } from "../config/config.js"; import type { SessionChatType, SessionEntry } from "../config/sessions.js"; -import { deriveSessionChatType } from "./session-chat-type.js"; export type SessionSendPolicyDecision = "allow" | "deny"; @@ -46,8 +45,33 @@ function deriveChannelFromKey(key?: string) { } function deriveChatTypeFromKey(key?: string): SessionChatType | undefined { - const chatType = deriveSessionChatType(key); - return chatType === "unknown" ? undefined : chatType; + const normalizedKey = stripAgentSessionKeyPrefix(key)?.trim().toLowerCase(); + if (!normalizedKey) { + return undefined; + } + const tokens = new Set(normalizedKey.split(":").filter(Boolean)); + if (tokens.has("group")) { + return "group"; + } + if (tokens.has("channel")) { + return "channel"; + } + if (tokens.has("direct") || tokens.has("dm")) { + return "direct"; + } + if (/^group:[^:]+$/u.test(normalizedKey)) { + return "group"; + } + if (/^[0-9]+(?:-[0-9]+)*@g\.us$/u.test(normalizedKey)) { + return "group"; + } + if (/^whatsapp:(?!.*:group:).+@g\.us$/u.test(normalizedKey)) { + return "group"; + } + if (/^discord:(?:[^:]+:)?guild-[^:]+:channel-[^:]+$/u.test(normalizedKey)) { + return "channel"; + } + return undefined; } export function resolveSendPolicy(params: {