mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 15:54:32 +00:00
perf(test): reuse managers in embedding token limit suite
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { getMemorySearchManager, type MemoryIndexManager } from "./index.js";
|
import { getMemorySearchManager, type MemoryIndexManager } from "./index.js";
|
||||||
|
|
||||||
const embedBatch = vi.fn(async (texts: string[]) => texts.map(() => [0, 1, 0]));
|
const embedBatch = vi.fn(async (texts: string[]) => texts.map(() => [0, 1, 0]));
|
||||||
@@ -33,41 +33,24 @@ vi.mock("./embeddings.js", () => ({
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
describe("memory embedding token limits", () => {
|
describe("memory embedding token limits", () => {
|
||||||
|
let fixtureRoot: string;
|
||||||
let workspaceDir: string;
|
let workspaceDir: string;
|
||||||
let indexPath: string;
|
let memoryDir: string;
|
||||||
let manager: MemoryIndexManager | null = null;
|
let indexPathLarge: string;
|
||||||
|
let indexPathSmall: string;
|
||||||
|
let managerLarge: MemoryIndexManager | null = null;
|
||||||
|
let managerSmall: MemoryIndexManager | null = null;
|
||||||
|
|
||||||
beforeEach(async () => {
|
function createCfg(params: { indexPath: string; tokens: number }) {
|
||||||
embedBatch.mockReset();
|
return {
|
||||||
embedQuery.mockReset();
|
|
||||||
embedBatch.mockImplementation(async (texts: string[]) => texts.map(() => [0, 1, 0]));
|
|
||||||
embedQuery.mockImplementation(async () => [0, 1, 0]);
|
|
||||||
workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-mem-token-"));
|
|
||||||
indexPath = path.join(workspaceDir, "index.sqlite");
|
|
||||||
await fs.mkdir(path.join(workspaceDir, "memory"));
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(async () => {
|
|
||||||
if (manager) {
|
|
||||||
await manager.close();
|
|
||||||
manager = null;
|
|
||||||
}
|
|
||||||
await fs.rm(workspaceDir, { recursive: true, force: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
it("splits oversized chunks so each embedding input stays <= 8192 UTF-8 bytes", async () => {
|
|
||||||
const content = "x".repeat(9500);
|
|
||||||
await fs.writeFile(path.join(workspaceDir, "memory", "2026-01-09.md"), content);
|
|
||||||
|
|
||||||
const cfg = {
|
|
||||||
agents: {
|
agents: {
|
||||||
defaults: {
|
defaults: {
|
||||||
workspace: workspaceDir,
|
workspace: workspaceDir,
|
||||||
memorySearch: {
|
memorySearch: {
|
||||||
provider: "openai",
|
provider: "openai",
|
||||||
model: "mock-embed",
|
model: "mock-embed",
|
||||||
store: { path: indexPath },
|
store: { path: params.indexPath, vector: { enabled: false } },
|
||||||
chunking: { tokens: 10_000, overlap: 0 },
|
chunking: { tokens: params.tokens, overlap: 0 },
|
||||||
sync: { watch: false, onSessionStart: false, onSearch: false },
|
sync: { watch: false, onSessionStart: false, onSearch: false },
|
||||||
query: { minScore: 0 },
|
query: { minScore: 0 },
|
||||||
},
|
},
|
||||||
@@ -75,14 +58,76 @@ describe("memory embedding token limits", () => {
|
|||||||
list: [{ id: "main", default: true }],
|
list: [{ id: "main", default: true }],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const result = await getMemorySearchManager({ cfg, agentId: "main" });
|
beforeAll(async () => {
|
||||||
expect(result.manager).not.toBeNull();
|
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-mem-token-"));
|
||||||
if (!result.manager) {
|
workspaceDir = path.join(fixtureRoot, "workspace");
|
||||||
|
memoryDir = path.join(workspaceDir, "memory");
|
||||||
|
indexPathLarge = path.join(fixtureRoot, "index.large.sqlite");
|
||||||
|
indexPathSmall = path.join(fixtureRoot, "index.small.sqlite");
|
||||||
|
await fs.mkdir(memoryDir, { recursive: true });
|
||||||
|
|
||||||
|
const large = await getMemorySearchManager({
|
||||||
|
cfg: createCfg({ indexPath: indexPathLarge, tokens: 10_000 }),
|
||||||
|
agentId: "main",
|
||||||
|
});
|
||||||
|
expect(large.manager).not.toBeNull();
|
||||||
|
if (!large.manager) {
|
||||||
throw new Error("manager missing");
|
throw new Error("manager missing");
|
||||||
}
|
}
|
||||||
manager = result.manager;
|
managerLarge = large.manager;
|
||||||
await manager.sync({ reason: "test" });
|
|
||||||
|
const small = await getMemorySearchManager({
|
||||||
|
cfg: createCfg({ indexPath: indexPathSmall, tokens: 1000 }),
|
||||||
|
agentId: "main",
|
||||||
|
});
|
||||||
|
expect(small.manager).not.toBeNull();
|
||||||
|
if (!small.manager) {
|
||||||
|
throw new Error("manager missing");
|
||||||
|
}
|
||||||
|
managerSmall = small.manager;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
if (managerLarge) {
|
||||||
|
await managerLarge.close();
|
||||||
|
managerLarge = null;
|
||||||
|
}
|
||||||
|
if (managerSmall) {
|
||||||
|
await managerSmall.close();
|
||||||
|
managerSmall = null;
|
||||||
|
}
|
||||||
|
await fs.rm(fixtureRoot, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
embedBatch.mockReset();
|
||||||
|
embedQuery.mockReset();
|
||||||
|
embedBatch.mockImplementation(async (texts: string[]) => texts.map(() => [0, 1, 0]));
|
||||||
|
embedQuery.mockImplementation(async () => [0, 1, 0]);
|
||||||
|
|
||||||
|
await fs.rm(memoryDir, { recursive: true, force: true });
|
||||||
|
await fs.mkdir(memoryDir, { recursive: true });
|
||||||
|
|
||||||
|
const reset = (manager: MemoryIndexManager | null) => {
|
||||||
|
if (!manager) {
|
||||||
|
throw new Error("manager missing");
|
||||||
|
}
|
||||||
|
(manager as unknown as { resetIndex: () => void }).resetIndex();
|
||||||
|
(manager as unknown as { dirty: boolean }).dirty = true;
|
||||||
|
};
|
||||||
|
reset(managerLarge);
|
||||||
|
reset(managerSmall);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("splits oversized chunks so each embedding input stays <= 8192 UTF-8 bytes", async () => {
|
||||||
|
const content = "x".repeat(9500);
|
||||||
|
await fs.writeFile(path.join(memoryDir, "2026-01-09.md"), content);
|
||||||
|
if (!managerLarge) {
|
||||||
|
throw new Error("manager missing");
|
||||||
|
}
|
||||||
|
await managerLarge.sync({ reason: "test" });
|
||||||
|
|
||||||
const inputs = embedBatch.mock.calls.flatMap((call) => call[0] ?? []);
|
const inputs = embedBatch.mock.calls.flatMap((call) => call[0] ?? []);
|
||||||
expect(inputs.length).toBeGreaterThan(1);
|
expect(inputs.length).toBeGreaterThan(1);
|
||||||
@@ -94,32 +139,11 @@ describe("memory embedding token limits", () => {
|
|||||||
it("uses UTF-8 byte estimates when batching multibyte chunks", async () => {
|
it("uses UTF-8 byte estimates when batching multibyte chunks", async () => {
|
||||||
const line = "😀".repeat(1800);
|
const line = "😀".repeat(1800);
|
||||||
const content = `${line}\n${line}\n${line}`;
|
const content = `${line}\n${line}\n${line}`;
|
||||||
await fs.writeFile(path.join(workspaceDir, "memory", "2026-01-10.md"), content);
|
await fs.writeFile(path.join(memoryDir, "2026-01-10.md"), content);
|
||||||
|
if (!managerSmall) {
|
||||||
const cfg = {
|
|
||||||
agents: {
|
|
||||||
defaults: {
|
|
||||||
workspace: workspaceDir,
|
|
||||||
memorySearch: {
|
|
||||||
provider: "openai",
|
|
||||||
model: "mock-embed",
|
|
||||||
store: { path: indexPath },
|
|
||||||
chunking: { tokens: 1000, overlap: 0 },
|
|
||||||
sync: { watch: false, onSessionStart: false, onSearch: false },
|
|
||||||
query: { minScore: 0 },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
list: [{ id: "main", default: true }],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const result = await getMemorySearchManager({ cfg, agentId: "main" });
|
|
||||||
expect(result.manager).not.toBeNull();
|
|
||||||
if (!result.manager) {
|
|
||||||
throw new Error("manager missing");
|
throw new Error("manager missing");
|
||||||
}
|
}
|
||||||
manager = result.manager;
|
await managerSmall.sync({ reason: "test" });
|
||||||
await manager.sync({ reason: "test" });
|
|
||||||
|
|
||||||
const batchSizes = embedBatch.mock.calls.map(
|
const batchSizes = embedBatch.mock.calls.map(
|
||||||
(call) => (call[0] as string[] | undefined)?.length ?? 0,
|
(call) => (call[0] as string[] | undefined)?.length ?? 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user