mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-19 05:50:47 +00:00
fix: add safety timeout to session.compact() to prevent lane deadlock (#16533)
Merged via /review-pr -> /prepare-pr -> /merge-pr.
Prepared head SHA: 21e4045add
Co-authored-by: BinHPdev <219093083+BinHPdev@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
This commit is contained in:
114
src/auto-reply/reply/commands-compact.test.ts
Normal file
114
src/auto-reply/reply/commands-compact.test.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { OpenClawConfig } from "../../config/config.js";
|
||||
import { compactEmbeddedPiSession } from "../../agents/pi-embedded.js";
|
||||
import { handleCompactCommand } from "./commands-compact.js";
|
||||
import { buildCommandTestParams } from "./commands.test-harness.js";
|
||||
|
||||
vi.mock("../../agents/pi-embedded.js", () => ({
|
||||
abortEmbeddedPiRun: vi.fn(),
|
||||
compactEmbeddedPiSession: vi.fn(),
|
||||
isEmbeddedPiRunActive: vi.fn().mockReturnValue(false),
|
||||
waitForEmbeddedPiRunEnd: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
vi.mock("../../infra/system-events.js", () => ({
|
||||
enqueueSystemEvent: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("./session-updates.js", () => ({
|
||||
incrementCompactionCount: vi.fn(),
|
||||
}));
|
||||
|
||||
describe("/compact command", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("returns null when command is not /compact", async () => {
|
||||
const cfg = {
|
||||
commands: { text: true },
|
||||
channels: { whatsapp: { allowFrom: ["*"] } },
|
||||
} as OpenClawConfig;
|
||||
const params = buildCommandTestParams("/status", cfg);
|
||||
|
||||
const result = await handleCompactCommand(
|
||||
{
|
||||
...params,
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
expect(result).toBeNull();
|
||||
expect(vi.mocked(compactEmbeddedPiSession)).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects unauthorized /compact commands", async () => {
|
||||
const cfg = {
|
||||
commands: { text: true },
|
||||
channels: { whatsapp: { allowFrom: ["*"] } },
|
||||
} as OpenClawConfig;
|
||||
const params = buildCommandTestParams("/compact", cfg);
|
||||
|
||||
const result = await handleCompactCommand(
|
||||
{
|
||||
...params,
|
||||
command: {
|
||||
...params.command,
|
||||
isAuthorizedSender: false,
|
||||
senderId: "unauthorized",
|
||||
},
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
expect(result).toEqual({ shouldContinue: false });
|
||||
expect(vi.mocked(compactEmbeddedPiSession)).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("routes manual compaction with explicit trigger and context metadata", async () => {
|
||||
const cfg = {
|
||||
commands: { text: true },
|
||||
channels: { whatsapp: { allowFrom: ["*"] } },
|
||||
session: { store: "/tmp/openclaw-session-store.json" },
|
||||
} as OpenClawConfig;
|
||||
const params = buildCommandTestParams("/compact: focus on decisions", cfg, {
|
||||
From: "+15550001",
|
||||
To: "+15550002",
|
||||
});
|
||||
vi.mocked(compactEmbeddedPiSession).mockResolvedValueOnce({
|
||||
ok: true,
|
||||
compacted: false,
|
||||
});
|
||||
|
||||
const result = await handleCompactCommand(
|
||||
{
|
||||
...params,
|
||||
sessionEntry: {
|
||||
sessionId: "session-1",
|
||||
groupId: "group-1",
|
||||
groupChannel: "#general",
|
||||
space: "workspace-1",
|
||||
spawnedBy: "agent:main:parent",
|
||||
totalTokens: 12345,
|
||||
},
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
expect(result?.shouldContinue).toBe(false);
|
||||
expect(vi.mocked(compactEmbeddedPiSession)).toHaveBeenCalledOnce();
|
||||
expect(vi.mocked(compactEmbeddedPiSession)).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
sessionId: "session-1",
|
||||
sessionKey: "agent:main:main",
|
||||
trigger: "manual",
|
||||
customInstructions: "focus on decisions",
|
||||
messageChannel: "whatsapp",
|
||||
groupId: "group-1",
|
||||
groupChannel: "#general",
|
||||
groupSpace: "workspace-1",
|
||||
spawnedBy: "agent:main:parent",
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -103,6 +103,7 @@ export const handleCompactCommand: CommandHandler = async (params) => {
|
||||
defaultLevel: "off",
|
||||
},
|
||||
customInstructions,
|
||||
trigger: "manual",
|
||||
senderIsOwner: params.command.senderIsOwner,
|
||||
ownerNumbers: params.command.ownerList.length > 0 ? params.command.ownerList : undefined,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user