mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 22:28:27 +00:00
refactor(agent): dedupe harness and command workflows
This commit is contained in:
@@ -25,6 +25,27 @@ vi.mock("../../agents/agent-scope.js", () => ({
|
||||
const { resolveSessionKeyForRequest } = await import("./session.js");
|
||||
|
||||
describe("resolveSessionKeyForRequest", () => {
|
||||
const MAIN_STORE_PATH = "/tmp/main-store.json";
|
||||
const MYBOT_STORE_PATH = "/tmp/mybot-store.json";
|
||||
type SessionStoreEntry = { sessionId: string; updatedAt: number };
|
||||
type SessionStoreMap = Record<string, SessionStoreEntry>;
|
||||
|
||||
const setupMainAndMybotStorePaths = () => {
|
||||
mocks.listAgentIds.mockReturnValue(["main", "mybot"]);
|
||||
mocks.resolveStorePath.mockImplementation(
|
||||
(_store: string | undefined, opts?: { agentId?: string }) => {
|
||||
if (opts?.agentId === "mybot") {
|
||||
return MYBOT_STORE_PATH;
|
||||
}
|
||||
return MAIN_STORE_PATH;
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
const mockStoresByPath = (stores: Partial<Record<string, SessionStoreMap>>) => {
|
||||
mocks.loadSessionStore.mockImplementation((storePath: string) => stores[storePath] ?? {});
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mocks.listAgentIds.mockReturnValue(["main"]);
|
||||
@@ -33,7 +54,7 @@ describe("resolveSessionKeyForRequest", () => {
|
||||
const baseCfg: OpenClawConfig = {};
|
||||
|
||||
it("returns sessionKey when --to resolves a session key via context", async () => {
|
||||
mocks.resolveStorePath.mockReturnValue("/tmp/main-store.json");
|
||||
mocks.resolveStorePath.mockReturnValue(MAIN_STORE_PATH);
|
||||
mocks.loadSessionStore.mockReturnValue({
|
||||
"agent:main:main": { sessionId: "sess-1", updatedAt: 0 },
|
||||
});
|
||||
@@ -46,7 +67,7 @@ describe("resolveSessionKeyForRequest", () => {
|
||||
});
|
||||
|
||||
it("finds session by sessionId via reverse lookup in primary store", async () => {
|
||||
mocks.resolveStorePath.mockReturnValue("/tmp/main-store.json");
|
||||
mocks.resolveStorePath.mockReturnValue(MAIN_STORE_PATH);
|
||||
mocks.loadSessionStore.mockReturnValue({
|
||||
"agent:main:main": { sessionId: "target-session-id", updatedAt: 0 },
|
||||
});
|
||||
@@ -59,22 +80,11 @@ describe("resolveSessionKeyForRequest", () => {
|
||||
});
|
||||
|
||||
it("finds session by sessionId in non-primary agent store", async () => {
|
||||
mocks.listAgentIds.mockReturnValue(["main", "mybot"]);
|
||||
mocks.resolveStorePath.mockImplementation(
|
||||
(_store: string | undefined, opts?: { agentId?: string }) => {
|
||||
if (opts?.agentId === "mybot") {
|
||||
return "/tmp/mybot-store.json";
|
||||
}
|
||||
return "/tmp/main-store.json";
|
||||
setupMainAndMybotStorePaths();
|
||||
mockStoresByPath({
|
||||
[MYBOT_STORE_PATH]: {
|
||||
"agent:mybot:main": { sessionId: "target-session-id", updatedAt: 0 },
|
||||
},
|
||||
);
|
||||
mocks.loadSessionStore.mockImplementation((storePath: string) => {
|
||||
if (storePath === "/tmp/mybot-store.json") {
|
||||
return {
|
||||
"agent:mybot:main": { sessionId: "target-session-id", updatedAt: 0 },
|
||||
};
|
||||
}
|
||||
return {};
|
||||
});
|
||||
|
||||
const result = resolveSessionKeyForRequest({
|
||||
@@ -82,27 +92,16 @@ describe("resolveSessionKeyForRequest", () => {
|
||||
sessionId: "target-session-id",
|
||||
});
|
||||
expect(result.sessionKey).toBe("agent:mybot:main");
|
||||
expect(result.storePath).toBe("/tmp/mybot-store.json");
|
||||
expect(result.storePath).toBe(MYBOT_STORE_PATH);
|
||||
});
|
||||
|
||||
it("returns correct sessionStore when session found in non-primary agent store", async () => {
|
||||
const mybotStore = {
|
||||
"agent:mybot:main": { sessionId: "target-session-id", updatedAt: 0 },
|
||||
};
|
||||
mocks.listAgentIds.mockReturnValue(["main", "mybot"]);
|
||||
mocks.resolveStorePath.mockImplementation(
|
||||
(_store: string | undefined, opts?: { agentId?: string }) => {
|
||||
if (opts?.agentId === "mybot") {
|
||||
return "/tmp/mybot-store.json";
|
||||
}
|
||||
return "/tmp/main-store.json";
|
||||
},
|
||||
);
|
||||
mocks.loadSessionStore.mockImplementation((storePath: string) => {
|
||||
if (storePath === "/tmp/mybot-store.json") {
|
||||
return { ...mybotStore };
|
||||
}
|
||||
return {};
|
||||
setupMainAndMybotStorePaths();
|
||||
mockStoresByPath({
|
||||
[MYBOT_STORE_PATH]: { ...mybotStore },
|
||||
});
|
||||
|
||||
const result = resolveSessionKeyForRequest({
|
||||
@@ -113,15 +112,7 @@ describe("resolveSessionKeyForRequest", () => {
|
||||
});
|
||||
|
||||
it("returns undefined sessionKey when sessionId not found in any store", async () => {
|
||||
mocks.listAgentIds.mockReturnValue(["main", "mybot"]);
|
||||
mocks.resolveStorePath.mockImplementation(
|
||||
(_store: string | undefined, opts?: { agentId?: string }) => {
|
||||
if (opts?.agentId === "mybot") {
|
||||
return "/tmp/mybot-store.json";
|
||||
}
|
||||
return "/tmp/main-store.json";
|
||||
},
|
||||
);
|
||||
setupMainAndMybotStorePaths();
|
||||
mocks.loadSessionStore.mockReturnValue({});
|
||||
|
||||
const result = resolveSessionKeyForRequest({
|
||||
@@ -133,7 +124,7 @@ describe("resolveSessionKeyForRequest", () => {
|
||||
|
||||
it("does not search other stores when explicitSessionKey is set", async () => {
|
||||
mocks.listAgentIds.mockReturnValue(["main", "mybot"]);
|
||||
mocks.resolveStorePath.mockReturnValue("/tmp/main-store.json");
|
||||
mocks.resolveStorePath.mockReturnValue(MAIN_STORE_PATH);
|
||||
mocks.loadSessionStore.mockReturnValue({
|
||||
"agent:main:main": { sessionId: "other-id", updatedAt: 0 },
|
||||
});
|
||||
@@ -148,27 +139,14 @@ describe("resolveSessionKeyForRequest", () => {
|
||||
});
|
||||
|
||||
it("searches other stores when --to derives a key that does not match --session-id", async () => {
|
||||
mocks.listAgentIds.mockReturnValue(["main", "mybot"]);
|
||||
mocks.resolveStorePath.mockImplementation(
|
||||
(_store: string | undefined, opts?: { agentId?: string }) => {
|
||||
if (opts?.agentId === "mybot") {
|
||||
return "/tmp/mybot-store.json";
|
||||
}
|
||||
return "/tmp/main-store.json";
|
||||
setupMainAndMybotStorePaths();
|
||||
mockStoresByPath({
|
||||
[MAIN_STORE_PATH]: {
|
||||
"agent:main:main": { sessionId: "other-session-id", updatedAt: 0 },
|
||||
},
|
||||
[MYBOT_STORE_PATH]: {
|
||||
"agent:mybot:main": { sessionId: "target-session-id", updatedAt: 0 },
|
||||
},
|
||||
);
|
||||
mocks.loadSessionStore.mockImplementation((storePath: string) => {
|
||||
if (storePath === "/tmp/main-store.json") {
|
||||
return {
|
||||
"agent:main:main": { sessionId: "other-session-id", updatedAt: 0 },
|
||||
};
|
||||
}
|
||||
if (storePath === "/tmp/mybot-store.json") {
|
||||
return {
|
||||
"agent:mybot:main": { sessionId: "target-session-id", updatedAt: 0 },
|
||||
};
|
||||
}
|
||||
return {};
|
||||
});
|
||||
|
||||
const result = resolveSessionKeyForRequest({
|
||||
@@ -179,19 +157,11 @@ describe("resolveSessionKeyForRequest", () => {
|
||||
// --to derives agent:main:main, but its sessionId doesn't match target-session-id,
|
||||
// so the cross-store search finds it in the mybot store
|
||||
expect(result.sessionKey).toBe("agent:mybot:main");
|
||||
expect(result.storePath).toBe("/tmp/mybot-store.json");
|
||||
expect(result.storePath).toBe(MYBOT_STORE_PATH);
|
||||
});
|
||||
|
||||
it("skips already-searched primary store when iterating agents", async () => {
|
||||
mocks.listAgentIds.mockReturnValue(["main", "mybot"]);
|
||||
mocks.resolveStorePath.mockImplementation(
|
||||
(_store: string | undefined, opts?: { agentId?: string }) => {
|
||||
if (opts?.agentId === "mybot") {
|
||||
return "/tmp/mybot-store.json";
|
||||
}
|
||||
return "/tmp/main-store.json";
|
||||
},
|
||||
);
|
||||
setupMainAndMybotStorePaths();
|
||||
mocks.loadSessionStore.mockReturnValue({});
|
||||
|
||||
resolveSessionKeyForRequest({
|
||||
@@ -203,7 +173,7 @@ describe("resolveSessionKeyForRequest", () => {
|
||||
// (not twice for main)
|
||||
const storePaths = mocks.loadSessionStore.mock.calls.map((call: [string]) => call[0]);
|
||||
expect(storePaths).toHaveLength(2);
|
||||
expect(storePaths).toContain("/tmp/main-store.json");
|
||||
expect(storePaths).toContain("/tmp/mybot-store.json");
|
||||
expect(storePaths).toContain(MAIN_STORE_PATH);
|
||||
expect(storePaths).toContain(MYBOT_STORE_PATH);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user