fix: use target agent dir for btw command

This commit is contained in:
Tak Hoffman
2026-04-10 20:06:47 -05:00
parent 29018b4af5
commit dcf49fa5d8
2 changed files with 34 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
import { resolveAgentDir } from "../../agents/agent-scope.js";
import type { OpenClawConfig } from "../../config/config.js";
import { buildCommandTestParams } from "./commands.test-harness.js";
import { createMockTypingController } from "./test-helpers.js";
@@ -13,6 +14,7 @@ vi.mock("../../agents/agent-scope.js", async () => {
return {
...actual,
resolveSessionAgentId: resolveSessionAgentIdMock,
resolveAgentDir: vi.fn(actual.resolveAgentDir),
};
});
@@ -193,4 +195,35 @@ describe("handleBtwCommand", () => {
reply: { text: "resolved fallback", btw: { question: "what changed?" } },
});
});
it("uses the canonical session agent dir even when the wrapper agentDir disagrees", async () => {
const params = buildParams("/btw what changed?");
params.agentId = "main";
params.agentDir = "/tmp/main-agent";
params.sessionKey = "agent:worker-1:whatsapp:direct:12345";
params.sessionEntry = {
sessionId: "session-1",
updatedAt: Date.now(),
};
resolveSessionAgentIdMock.mockReturnValue("worker-1");
vi.mocked(resolveAgentDir).mockReturnValue("/tmp/worker-1-agent");
runBtwSideQuestionMock.mockResolvedValue({ text: "resolved fallback" });
const result = await handleBtwCommand(params, true);
expect(resolveSessionAgentIdMock).toHaveBeenCalledWith({
sessionKey: "agent:worker-1:whatsapp:direct:12345",
config: expect.any(Object),
});
expect(vi.mocked(resolveAgentDir)).toHaveBeenCalledWith(expect.any(Object), "worker-1");
expect(runBtwSideQuestionMock).toHaveBeenCalledWith(
expect.objectContaining({
agentDir: "/tmp/worker-1-agent",
}),
);
expect(result).toEqual({
shouldContinue: false,
reply: { text: "resolved fallback", btw: { question: "what changed?" } },
});
});
});

View File

@@ -37,7 +37,7 @@ export const handleBtwCommand: CommandHandler = async (params, allowTextCommands
? resolveSessionAgentId({ sessionKey: params.sessionKey, config: params.cfg })
: params.agentId;
const agentDir =
params.agentDir ?? (sessionAgentId ? resolveAgentDir(params.cfg, sessionAgentId) : undefined);
(sessionAgentId ? resolveAgentDir(params.cfg, sessionAgentId) : undefined) ?? params.agentDir;
if (!agentDir) {
return {