fix: use target agent dir for compaction

This commit is contained in:
Tak Hoffman
2026-04-10 19:56:28 -05:00
parent a775051ac6
commit bef2fde77f
2 changed files with 37 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { resolveAgentDir } from "../../agents/agent-scope.js";
import type { OpenClawConfig } from "../../config/config.js";
import { handleCompactCommand } from "./commands-compact.js";
import type { HandleCommandsParams } from "./commands-types.js";
@@ -12,6 +13,7 @@ vi.mock("../../agents/agent-scope.js", async () => {
return {
...actual,
resolveSessionAgentId: resolveSessionAgentIdMock,
resolveAgentDir: vi.fn(actual.resolveAgentDir),
};
});
@@ -193,4 +195,36 @@ describe("handleCompactCommand", () => {
storePath: undefined,
});
});
it("uses the canonical session agent directory for compaction runtime inputs", async () => {
vi.mocked(compactEmbeddedPiSession).mockResolvedValueOnce({
ok: true,
compacted: false,
});
resolveSessionAgentIdMock.mockReturnValue("target");
vi.mocked(resolveAgentDir).mockReturnValue("/tmp/target-agent");
await handleCompactCommand(
{
...buildCompactParams("/compact", {
commands: { text: true },
channels: { whatsapp: { allowFrom: ["*"] } },
} as OpenClawConfig),
agentId: "main",
agentDir: "/tmp/main-agent",
sessionKey: "agent:target:whatsapp:direct:12345",
sessionEntry: {
sessionId: "session-1",
updatedAt: Date.now(),
},
} as HandleCommandsParams,
true,
);
expect(vi.mocked(compactEmbeddedPiSession)).toHaveBeenCalledWith(
expect.objectContaining({
agentDir: "/tmp/target-agent",
}),
);
});
});

View File

@@ -1,4 +1,4 @@
import { resolveSessionAgentId } from "../../agents/agent-scope.js";
import { resolveAgentDir, resolveSessionAgentId } from "../../agents/agent-scope.js";
import type { OpenClawConfig } from "../../config/config.js";
import { logVerbose } from "../../globals.js";
import {
@@ -96,6 +96,7 @@ export const handleCompactCommand: CommandHandler = async (params) => {
const sessionAgentId = params.sessionKey
? resolveSessionAgentId({ sessionKey: params.sessionKey, config: params.cfg })
: params.agentId;
const sessionAgentDir = resolveAgentDir(params.cfg, sessionAgentId) ?? params.agentDir;
const customInstructions = extractCompactInstructions({
rawBody: params.ctx.CommandBody ?? params.ctx.RawBody ?? params.ctx.Body,
ctx: params.ctx,
@@ -125,7 +126,7 @@ export const handleCompactCommand: CommandHandler = async (params) => {
}),
),
workspaceDir: params.workspaceDir,
agentDir: params.agentDir,
agentDir: sessionAgentDir,
config: params.cfg,
skillsSnapshot: params.sessionEntry.skillsSnapshot,
provider: params.provider,