perf(test): trim send-policy and abort hot paths

This commit is contained in:
Peter Steinberger
2026-04-06 03:10:29 +01:00
parent 7cd813139b
commit 3ee823b229
2 changed files with 27 additions and 26 deletions

View File

@@ -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",

View File

@@ -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: {