Skip to content

Commit 76e4e9d

Browse files
committed
perf(test): reduce skills + update + memory suite overhead
1 parent 684c184 commit 76e4e9d

6 files changed

Lines changed: 90 additions & 43 deletions

File tree

‎src/agents/skills/bundled-context.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { resolveBundledSkillsDir, type BundledSkillsResolveOptions } from "./bun
44

55
const skillsLogger = createSubsystemLogger("skills");
66
let hasWarnedMissingBundledDir = false;
7+
let cachedBundledContext: { dir: string; names: Set<string> } | null = null;
78

89
export type BundledSkillsContext = {
910
dir?: string;
@@ -24,11 +25,16 @@ export function resolveBundledSkillsContext(
2425
}
2526
return { dir, names };
2627
}
28+
29+
if (cachedBundledContext?.dir === dir) {
30+
return { dir, names: new Set(cachedBundledContext.names) };
31+
}
2732
const result = loadSkillsFromDir({ dir, source: "openclaw-bundled" });
2833
for (const skill of result.skills) {
2934
if (skill.name.trim()) {
3035
names.add(skill.name);
3136
}
3237
}
38+
cachedBundledContext = { dir, names: new Set(names) };
3339
return { dir, names };
3440
}

‎src/cli/skills-cli.test.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import fs from "node:fs";
2+
import os from "node:os";
23
import path from "node:path";
34
import { fileURLToPath } from "node:url";
4-
import { describe, expect, it } from "vitest";
5+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
56
import {
67
buildWorkspaceSkillStatus,
78
type SkillStatusEntry,
@@ -214,6 +215,18 @@ describe("skills-cli", () => {
214215
});
215216

216217
describe("integration: loads real skills from bundled directory", () => {
218+
let tempWorkspaceDir = "";
219+
220+
beforeAll(() => {
221+
tempWorkspaceDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-skills-test-"));
222+
});
223+
224+
afterAll(() => {
225+
if (tempWorkspaceDir) {
226+
fs.rmSync(tempWorkspaceDir, { recursive: true, force: true });
227+
}
228+
});
229+
217230
function resolveBundledSkillsDir(): string | undefined {
218231
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
219232
const root = path.resolve(moduleDir, "..", "..");
@@ -231,7 +244,7 @@ describe("skills-cli", () => {
231244
return;
232245
}
233246

234-
const report = buildWorkspaceSkillStatus("/tmp", {
247+
const report = buildWorkspaceSkillStatus(tempWorkspaceDir, {
235248
managedSkillsDir: "/nonexistent",
236249
});
237250

@@ -257,7 +270,7 @@ describe("skills-cli", () => {
257270
return;
258271
}
259272

260-
const report = buildWorkspaceSkillStatus("/tmp", {
273+
const report = buildWorkspaceSkillStatus(tempWorkspaceDir, {
261274
managedSkillsDir: "/nonexistent",
262275
});
263276

‎src/cli/update-cli.test.ts‎

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const select = vi.fn();
99
const spinner = vi.fn(() => ({ start: vi.fn(), stop: vi.fn() }));
1010
const isCancel = (value: unknown) => value === "cancel";
1111

12+
const readPackageName = vi.fn();
13+
const readPackageVersion = vi.fn();
14+
const resolveGlobalManager = vi.fn();
15+
1216
vi.mock("@clack/prompts", () => ({
1317
confirm,
1418
select,
@@ -61,6 +65,16 @@ vi.mock("../process/exec.js", () => ({
6165
runCommandWithTimeout: vi.fn(),
6266
}));
6367

68+
vi.mock("./update-cli/shared.js", async (importOriginal) => {
69+
const actual = await importOriginal<typeof import("./update-cli/shared.js")>();
70+
return {
71+
...actual,
72+
readPackageName,
73+
readPackageVersion,
74+
resolveGlobalManager,
75+
};
76+
});
77+
6478
// Mock doctor (heavy module; should not run in unit tests)
6579
vi.mock("../commands/doctor.js", () => ({
6680
doctorCommand: vi.fn(),
@@ -129,7 +143,23 @@ describe("update-cli", () => {
129143
};
130144

131145
beforeEach(() => {
132-
vi.clearAllMocks();
146+
confirm.mockReset();
147+
select.mockReset();
148+
vi.mocked(runGatewayUpdate).mockReset();
149+
vi.mocked(resolveOpenClawPackageRoot).mockReset();
150+
vi.mocked(readConfigFileSnapshot).mockReset();
151+
vi.mocked(writeConfigFile).mockReset();
152+
vi.mocked(checkUpdateStatus).mockReset();
153+
vi.mocked(fetchNpmTagVersion).mockReset();
154+
vi.mocked(resolveNpmChannelTag).mockReset();
155+
vi.mocked(runCommandWithTimeout).mockReset();
156+
vi.mocked(runDaemonRestart).mockReset();
157+
vi.mocked(defaultRuntime.log).mockReset();
158+
vi.mocked(defaultRuntime.error).mockReset();
159+
vi.mocked(defaultRuntime.exit).mockReset();
160+
readPackageName.mockReset();
161+
readPackageVersion.mockReset();
162+
resolveGlobalManager.mockReset();
133163
vi.mocked(resolveOpenClawPackageRoot).mockResolvedValue(process.cwd());
134164
vi.mocked(readConfigFileSnapshot).mockResolvedValue(baseSnapshot);
135165
vi.mocked(fetchNpmTagVersion).mockResolvedValue({
@@ -172,6 +202,9 @@ describe("update-cli", () => {
172202
signal: null,
173203
killed: false,
174204
});
205+
readPackageName.mockResolvedValue("openclaw");
206+
readPackageVersion.mockResolvedValue("1.0.0");
207+
resolveGlobalManager.mockResolvedValue("npm");
175208
setTty(false);
176209
setStdoutTty(false);
177210
});
@@ -241,11 +274,6 @@ describe("update-cli", () => {
241274

242275
it("defaults to stable channel for package installs when unset", async () => {
243276
const tempDir = await createCaseDir("openclaw-update");
244-
await fs.writeFile(
245-
path.join(tempDir, "package.json"),
246-
JSON.stringify({ name: "openclaw", version: "1.0.0" }),
247-
"utf-8",
248-
);
249277

250278
vi.mocked(resolveOpenClawPackageRoot).mockResolvedValue(tempDir);
251279
vi.mocked(checkUpdateStatus).mockResolvedValue({
@@ -293,11 +321,6 @@ describe("update-cli", () => {
293321

294322
it("falls back to latest when beta tag is older than release", async () => {
295323
const tempDir = await createCaseDir("openclaw-update");
296-
await fs.writeFile(
297-
path.join(tempDir, "package.json"),
298-
JSON.stringify({ name: "openclaw", version: "1.0.0" }),
299-
"utf-8",
300-
);
301324

302325
vi.mocked(resolveOpenClawPackageRoot).mockResolvedValue(tempDir);
303326
vi.mocked(readConfigFileSnapshot).mockResolvedValue({
@@ -335,11 +358,6 @@ describe("update-cli", () => {
335358

336359
it("honors --tag override", async () => {
337360
const tempDir = await createCaseDir("openclaw-update");
338-
await fs.writeFile(
339-
path.join(tempDir, "package.json"),
340-
JSON.stringify({ name: "openclaw", version: "1.0.0" }),
341-
"utf-8",
342-
);
343361

344362
vi.mocked(resolveOpenClawPackageRoot).mockResolvedValue(tempDir);
345363
vi.mocked(runGatewayUpdate).mockResolvedValue({
@@ -478,11 +496,7 @@ describe("update-cli", () => {
478496
it("requires confirmation on downgrade when non-interactive", async () => {
479497
const tempDir = await createCaseDir("openclaw-update");
480498
setTty(false);
481-
await fs.writeFile(
482-
path.join(tempDir, "package.json"),
483-
JSON.stringify({ name: "openclaw", version: "2.0.0" }),
484-
"utf-8",
485-
);
499+
readPackageVersion.mockResolvedValue("2.0.0");
486500

487501
vi.mocked(resolveOpenClawPackageRoot).mockResolvedValue(tempDir);
488502
vi.mocked(checkUpdateStatus).mockResolvedValue({
@@ -520,11 +534,7 @@ describe("update-cli", () => {
520534
it("allows downgrade with --yes in non-interactive mode", async () => {
521535
const tempDir = await createCaseDir("openclaw-update");
522536
setTty(false);
523-
await fs.writeFile(
524-
path.join(tempDir, "package.json"),
525-
JSON.stringify({ name: "openclaw", version: "2.0.0" }),
526-
"utf-8",
527-
);
537+
readPackageVersion.mockResolvedValue("2.0.0");
528538

529539
vi.mocked(resolveOpenClawPackageRoot).mockResolvedValue(tempDir);
530540
vi.mocked(checkUpdateStatus).mockResolvedValue({

‎src/memory/index.test.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ describe("memory index", () => {
7575
memorySearch: {
7676
provider: "openai",
7777
model: "mock-embed",
78-
store: { path: indexPath },
78+
store: { path: indexPath, vector: { enabled: false } },
7979
sync: { watch: false, onSessionStart: false, onSearch: true },
80-
query: { minScore: 0 },
80+
query: { minScore: 0, hybrid: { enabled: false } },
8181
},
8282
},
8383
list: [{ id: "main", default: true }],
@@ -114,7 +114,7 @@ describe("memory index", () => {
114114
provider: "openai",
115115
store: { path: indexPath },
116116
sync: { watch: false, onSessionStart: false, onSearch: true },
117-
query: { minScore: 0 },
117+
query: { minScore: 0, hybrid: { enabled: false } },
118118
},
119119
},
120120
list: [{ id: "main", default: true }],
@@ -182,7 +182,7 @@ describe("memory index", () => {
182182
model: "mock-embed",
183183
store: { path: indexPath, vector: { enabled: false } },
184184
sync: { watch: false, onSessionStart: false, onSearch: false },
185-
query: { minScore: 0 },
185+
query: { minScore: 0, hybrid: { enabled: false } },
186186
cache: { enabled: true },
187187
},
188188
},
@@ -276,8 +276,9 @@ describe("memory index", () => {
276276
memorySearch: {
277277
provider: "openai",
278278
model: "mock-embed",
279-
store: { path: indexPath },
279+
store: { path: indexPath, vector: { enabled: false } },
280280
sync: { watch: false, onSessionStart: false, onSearch: true },
281+
query: { minScore: 0, hybrid: { enabled: false } },
281282
},
282283
},
283284
list: [{ id: "main", default: true }],
@@ -304,8 +305,9 @@ describe("memory index", () => {
304305
memorySearch: {
305306
provider: "openai",
306307
model: "mock-embed",
307-
store: { path: indexPath },
308+
store: { path: indexPath, vector: { enabled: false } },
308309
sync: { watch: false, onSessionStart: false, onSearch: true },
310+
query: { minScore: 0, hybrid: { enabled: false } },
309311
extraPaths: [extraDir],
310312
},
311313
},

‎src/memory/manager.embedding-batches.test.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ describe("memory embedding batches", () => {
6767
memorySearch: {
6868
provider: "openai",
6969
model: "mock-embed",
70-
store: { path: indexPath },
70+
store: { path: indexPath, vector: { enabled: false } },
7171
chunking: { tokens: 1250, overlap: 0 },
7272
sync: { watch: false, onSessionStart: false, onSearch: false },
73-
query: { minScore: 0 },
73+
query: { minScore: 0, hybrid: { enabled: false } },
7474
},
7575
},
7676
list: [{ id: "main", default: true }],
@@ -114,10 +114,10 @@ describe("memory embedding batches", () => {
114114
memorySearch: {
115115
provider: "openai",
116116
model: "mock-embed",
117-
store: { path: indexPath },
117+
store: { path: indexPath, vector: { enabled: false } },
118118
chunking: { tokens: 200, overlap: 0 },
119119
sync: { watch: false, onSessionStart: false, onSearch: false },
120-
query: { minScore: 0 },
120+
query: { minScore: 0, hybrid: { enabled: false } },
121121
},
122122
},
123123
list: [{ id: "main", default: true }],
@@ -174,10 +174,10 @@ describe("memory embedding batches", () => {
174174
memorySearch: {
175175
provider: "openai",
176176
model: "mock-embed",
177-
store: { path: indexPath },
177+
store: { path: indexPath, vector: { enabled: false } },
178178
chunking: { tokens: 200, overlap: 0 },
179179
sync: { watch: false, onSessionStart: false, onSearch: false },
180-
query: { minScore: 0 },
180+
query: { minScore: 0, hybrid: { enabled: false } },
181181
},
182182
},
183183
list: [{ id: "main", default: true }],
@@ -209,9 +209,9 @@ describe("memory embedding batches", () => {
209209
memorySearch: {
210210
provider: "openai",
211211
model: "mock-embed",
212-
store: { path: indexPath },
212+
store: { path: indexPath, vector: { enabled: false } },
213213
sync: { watch: false, onSessionStart: false, onSearch: false },
214-
query: { minScore: 0 },
214+
query: { minScore: 0, hybrid: { enabled: false } },
215215
},
216216
},
217217
list: [{ id: "main", default: true }],

‎src/shared/config-eval.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,36 @@ function windowsPathExtensions(): string[] {
5252
return ["", ...list.filter(Boolean)];
5353
}
5454

55+
let cachedHasBinaryPath: string | undefined;
56+
let cachedHasBinaryPathExt: string | undefined;
57+
const hasBinaryCache = new Map<string, boolean>();
58+
5559
export function hasBinary(bin: string): boolean {
5660
const pathEnv = process.env.PATH ?? "";
61+
const pathExt = process.platform === "win32" ? (process.env.PATHEXT ?? "") : "";
62+
if (cachedHasBinaryPath !== pathEnv || cachedHasBinaryPathExt !== pathExt) {
63+
cachedHasBinaryPath = pathEnv;
64+
cachedHasBinaryPathExt = pathExt;
65+
hasBinaryCache.clear();
66+
}
67+
if (hasBinaryCache.has(bin)) {
68+
return hasBinaryCache.get(bin)!;
69+
}
70+
5771
const parts = pathEnv.split(path.delimiter).filter(Boolean);
5872
const extensions = process.platform === "win32" ? windowsPathExtensions() : [""];
5973
for (const part of parts) {
6074
for (const ext of extensions) {
6175
const candidate = path.join(part, bin + ext);
6276
try {
6377
fs.accessSync(candidate, fs.constants.X_OK);
78+
hasBinaryCache.set(bin, true);
6479
return true;
6580
} catch {
6681
// keep scanning
6782
}
6883
}
6984
}
85+
hasBinaryCache.set(bin, false);
7086
return false;
7187
}

0 commit comments

Comments
 (0)