mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 23:18:28 +00:00
refactor(core): dedupe shared config and runtime helpers
This commit is contained in:
@@ -70,6 +70,31 @@ vi.mock("./manager.js", () => ({
|
||||
import { QmdMemoryManager } from "./qmd-manager.js";
|
||||
import { getMemorySearchManager } from "./search-manager.js";
|
||||
|
||||
type SearchManagerResult = Awaited<ReturnType<typeof getMemorySearchManager>>;
|
||||
type SearchManager = NonNullable<SearchManagerResult["manager"]>;
|
||||
|
||||
function createQmdCfg(agentId: string) {
|
||||
return {
|
||||
memory: { backend: "qmd", qmd: {} },
|
||||
agents: { list: [{ id: agentId, default: true, workspace: "/tmp/workspace" }] },
|
||||
} as const;
|
||||
}
|
||||
|
||||
function requireManager(result: SearchManagerResult): SearchManager {
|
||||
expect(result.manager).toBeTruthy();
|
||||
if (!result.manager) {
|
||||
throw new Error("manager missing");
|
||||
}
|
||||
return result.manager;
|
||||
}
|
||||
|
||||
async function createFailedQmdSearchHarness(params: { agentId: string; errorMessage: string }) {
|
||||
const cfg = createQmdCfg(params.agentId);
|
||||
mockPrimary.search.mockRejectedValueOnce(new Error(params.errorMessage));
|
||||
const first = await getMemorySearchManager({ cfg, agentId: params.agentId });
|
||||
return { cfg, manager: requireManager(first), firstResult: first };
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
mockPrimary.search.mockClear();
|
||||
mockPrimary.readFile.mockClear();
|
||||
@@ -92,10 +117,7 @@ beforeEach(() => {
|
||||
|
||||
describe("getMemorySearchManager caching", () => {
|
||||
it("reuses the same QMD manager instance for repeated calls", async () => {
|
||||
const cfg = {
|
||||
memory: { backend: "qmd", qmd: {} },
|
||||
agents: { list: [{ id: "main", default: true, workspace: "/tmp/workspace" }] },
|
||||
} as const;
|
||||
const cfg = createQmdCfg("main");
|
||||
|
||||
const first = await getMemorySearchManager({ cfg, agentId: "main" });
|
||||
const second = await getMemorySearchManager({ cfg, agentId: "main" });
|
||||
@@ -107,24 +129,21 @@ describe("getMemorySearchManager caching", () => {
|
||||
|
||||
it("evicts failed qmd wrapper so next call retries qmd", async () => {
|
||||
const retryAgentId = "retry-agent";
|
||||
const cfg = {
|
||||
memory: { backend: "qmd", qmd: {} },
|
||||
agents: { list: [{ id: retryAgentId, default: true, workspace: "/tmp/workspace" }] },
|
||||
} as const;
|
||||
const {
|
||||
cfg,
|
||||
manager: firstManager,
|
||||
firstResult: first,
|
||||
} = await createFailedQmdSearchHarness({
|
||||
agentId: retryAgentId,
|
||||
errorMessage: "qmd query failed",
|
||||
});
|
||||
|
||||
mockPrimary.search.mockRejectedValueOnce(new Error("qmd query failed"));
|
||||
const first = await getMemorySearchManager({ cfg, agentId: retryAgentId });
|
||||
expect(first.manager).toBeTruthy();
|
||||
if (!first.manager) {
|
||||
throw new Error("manager missing");
|
||||
}
|
||||
|
||||
const fallbackResults = await first.manager.search("hello");
|
||||
const fallbackResults = await firstManager.search("hello");
|
||||
expect(fallbackResults).toHaveLength(1);
|
||||
expect(fallbackResults[0]?.path).toBe("MEMORY.md");
|
||||
|
||||
const second = await getMemorySearchManager({ cfg, agentId: retryAgentId });
|
||||
expect(second.manager).toBeTruthy();
|
||||
requireManager(second);
|
||||
expect(second.manager).not.toBe(first.manager);
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
expect(QmdMemoryManager.create).toHaveBeenCalledTimes(2);
|
||||
@@ -132,16 +151,13 @@ describe("getMemorySearchManager caching", () => {
|
||||
|
||||
it("does not cache status-only qmd managers", async () => {
|
||||
const agentId = "status-agent";
|
||||
const cfg = {
|
||||
memory: { backend: "qmd", qmd: {} },
|
||||
agents: { list: [{ id: agentId, default: true, workspace: "/tmp/workspace" }] },
|
||||
} as const;
|
||||
const cfg = createQmdCfg(agentId);
|
||||
|
||||
const first = await getMemorySearchManager({ cfg, agentId, purpose: "status" });
|
||||
const second = await getMemorySearchManager({ cfg, agentId, purpose: "status" });
|
||||
|
||||
expect(first.manager).toBeTruthy();
|
||||
expect(second.manager).toBeTruthy();
|
||||
requireManager(first);
|
||||
requireManager(second);
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
expect(QmdMemoryManager.create).toHaveBeenCalledTimes(2);
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
@@ -158,53 +174,36 @@ describe("getMemorySearchManager caching", () => {
|
||||
|
||||
it("does not evict a newer cached wrapper when closing an older failed wrapper", async () => {
|
||||
const retryAgentId = "retry-agent-close";
|
||||
const cfg = {
|
||||
memory: { backend: "qmd", qmd: {} },
|
||||
agents: { list: [{ id: retryAgentId, default: true, workspace: "/tmp/workspace" }] },
|
||||
} as const;
|
||||
|
||||
mockPrimary.search.mockRejectedValueOnce(new Error("qmd query failed"));
|
||||
|
||||
const first = await getMemorySearchManager({ cfg, agentId: retryAgentId });
|
||||
expect(first.manager).toBeTruthy();
|
||||
if (!first.manager) {
|
||||
throw new Error("manager missing");
|
||||
}
|
||||
await first.manager.search("hello");
|
||||
const {
|
||||
cfg,
|
||||
manager: firstManager,
|
||||
firstResult: first,
|
||||
} = await createFailedQmdSearchHarness({
|
||||
agentId: retryAgentId,
|
||||
errorMessage: "qmd query failed",
|
||||
});
|
||||
await firstManager.search("hello");
|
||||
|
||||
const second = await getMemorySearchManager({ cfg, agentId: retryAgentId });
|
||||
expect(second.manager).toBeTruthy();
|
||||
if (!second.manager) {
|
||||
throw new Error("manager missing");
|
||||
}
|
||||
const secondManager = requireManager(second);
|
||||
expect(second.manager).not.toBe(first.manager);
|
||||
|
||||
await first.manager.close?.();
|
||||
await firstManager.close?.();
|
||||
|
||||
const third = await getMemorySearchManager({ cfg, agentId: retryAgentId });
|
||||
expect(third.manager).toBe(second.manager);
|
||||
expect(third.manager).toBe(secondManager);
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
expect(QmdMemoryManager.create).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("falls back to builtin search when qmd fails with sqlite busy", async () => {
|
||||
const retryAgentId = "retry-agent-busy";
|
||||
const cfg = {
|
||||
memory: { backend: "qmd", qmd: {} },
|
||||
agents: { list: [{ id: retryAgentId, default: true, workspace: "/tmp/workspace" }] },
|
||||
} as const;
|
||||
const { manager: firstManager } = await createFailedQmdSearchHarness({
|
||||
agentId: retryAgentId,
|
||||
errorMessage: "qmd index busy while reading results: SQLITE_BUSY: database is locked",
|
||||
});
|
||||
|
||||
mockPrimary.search.mockRejectedValueOnce(
|
||||
new Error("qmd index busy while reading results: SQLITE_BUSY: database is locked"),
|
||||
);
|
||||
|
||||
const first = await getMemorySearchManager({ cfg, agentId: retryAgentId });
|
||||
expect(first.manager).toBeTruthy();
|
||||
if (!first.manager) {
|
||||
throw new Error("manager missing");
|
||||
}
|
||||
|
||||
const results = await first.manager.search("hello");
|
||||
const results = await firstManager.search("hello");
|
||||
expect(results).toHaveLength(1);
|
||||
expect(results[0]?.path).toBe("MEMORY.md");
|
||||
expect(fallbackSearch).toHaveBeenCalledTimes(1);
|
||||
@@ -212,19 +211,12 @@ describe("getMemorySearchManager caching", () => {
|
||||
|
||||
it("keeps original qmd error when fallback manager initialization fails", async () => {
|
||||
const retryAgentId = "retry-agent-no-fallback-auth";
|
||||
const cfg = {
|
||||
memory: { backend: "qmd", qmd: {} },
|
||||
agents: { list: [{ id: retryAgentId, default: true, workspace: "/tmp/workspace" }] },
|
||||
} as const;
|
||||
|
||||
mockPrimary.search.mockRejectedValueOnce(new Error("qmd query failed"));
|
||||
const { manager: firstManager } = await createFailedQmdSearchHarness({
|
||||
agentId: retryAgentId,
|
||||
errorMessage: "qmd query failed",
|
||||
});
|
||||
mockMemoryIndexGet.mockRejectedValueOnce(new Error("No API key found for provider openai"));
|
||||
|
||||
const first = await getMemorySearchManager({ cfg, agentId: retryAgentId });
|
||||
if (!first.manager) {
|
||||
throw new Error("manager missing");
|
||||
}
|
||||
|
||||
await expect(first.manager.search("hello")).rejects.toThrow("qmd query failed");
|
||||
await expect(firstManager.search("hello")).rejects.toThrow("qmd query failed");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user