Files
openclaw/src/agents/skills.compact-skill-paths.test.ts
mac26ai 4f2c57eb4e feat(skills): compact skill paths with ~ to reduce prompt tokens
Replace absolute home directory prefix with ~ in skill <location> tags
injected into the system prompt. Models understand ~ expansion and the
read tool resolves it, so this is a safe, backward-compatible change.

Saves ~5-6 tokens per skill path. For a workspace with 90+ skills,
this reduces system prompt size by ~400-600 tokens.

Changes:
- Add compactSkillPaths() helper in workspace.ts
- Apply in buildWorkspaceSkillSnapshot and buildWorkspaceSkillsPrompt
- Add test for path compaction behavior

Before: /Users/alice/.bun/install/global/node_modules/openclaw/skills/github/SKILL.md
After:  ~/.bun/install/global/node_modules/openclaw/skills/github/SKILL.md
2026-02-18 01:35:37 +01:00

81 lines
2.5 KiB
TypeScript

import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { buildWorkspaceSkillsPrompt } from "./skills.js";
async function writeSkill(params: {
dir: string;
name: string;
description: string;
body?: string;
}) {
const { dir, name, description, body } = params;
await fs.mkdir(dir, { recursive: true });
await fs.writeFile(
path.join(dir, "SKILL.md"),
`---
name: ${name}
description: ${description}
---
${body ?? `# ${name}\n`}
`,
"utf-8",
);
}
describe("compactSkillPaths", () => {
it("replaces home directory prefix with ~ in skill locations", async () => {
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-compact-"));
const skillDir = path.join(workspaceDir, "skills", "test-skill");
await writeSkill({
dir: skillDir,
name: "test-skill",
description: "A test skill for path compaction",
});
const prompt = buildWorkspaceSkillsPrompt(workspaceDir, {
bundledSkillsDir: path.join(workspaceDir, ".bundled-empty"),
managedSkillsDir: path.join(workspaceDir, ".managed-empty"),
});
const home = os.homedir();
// The prompt should NOT contain the absolute home directory path
// when the skill is under the home directory (which tmpdir usually is on macOS)
if (workspaceDir.startsWith(home)) {
expect(prompt).not.toContain(home + path.sep);
expect(prompt).toContain("~/");
}
// The skill name and description should still be present
expect(prompt).toContain("test-skill");
expect(prompt).toContain("A test skill for path compaction");
await fs.rm(workspaceDir, { recursive: true, force: true });
});
it("preserves paths outside home directory", async () => {
// Skills outside ~ should keep their absolute paths
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-compact-"));
const skillDir = path.join(workspaceDir, "skills", "ext-skill");
await writeSkill({
dir: skillDir,
name: "ext-skill",
description: "External skill",
});
const prompt = buildWorkspaceSkillsPrompt(workspaceDir, {
bundledSkillsDir: path.join(workspaceDir, ".bundled-empty"),
managedSkillsDir: path.join(workspaceDir, ".managed-empty"),
});
// Should still contain a valid location tag
expect(prompt).toMatch(/<location>[^<]+SKILL\.md<\/location>/);
await fs.rm(workspaceDir, { recursive: true, force: true });
});
});