|
| 1 | +import os from "node:os"; |
| 2 | +import { formatSkillsForPrompt, type Skill } from "@mariozechner/pi-coding-agent"; |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import type { SkillEntry } from "./types.js"; |
| 5 | +import { |
| 6 | + formatSkillsCompact, |
| 7 | + buildWorkspaceSkillsPrompt, |
| 8 | + buildWorkspaceSkillSnapshot, |
| 9 | +} from "./workspace.js"; |
| 10 | + |
| 11 | +function makeSkill(name: string, desc = "A skill", filePath = `/skills/${name}/SKILL.md`): Skill { |
| 12 | + return { |
| 13 | + name, |
| 14 | + description: desc, |
| 15 | + filePath, |
| 16 | + baseDir: `/skills/${name}`, |
| 17 | + source: "workspace", |
| 18 | + disableModelInvocation: false, |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +function makeEntry(skill: Skill): SkillEntry { |
| 23 | + return { skill, frontmatter: {} }; |
| 24 | +} |
| 25 | + |
| 26 | +function buildPrompt( |
| 27 | + skills: Skill[], |
| 28 | + limits: { maxChars?: number; maxCount?: number } = {}, |
| 29 | +): string { |
| 30 | + return buildWorkspaceSkillsPrompt("/fake", { |
| 31 | + entries: skills.map(makeEntry), |
| 32 | + config: { |
| 33 | + skills: { |
| 34 | + limits: { |
| 35 | + ...(limits.maxChars !== undefined && { maxSkillsPromptChars: limits.maxChars }), |
| 36 | + ...(limits.maxCount !== undefined && { maxSkillsInPrompt: limits.maxCount }), |
| 37 | + }, |
| 38 | + }, |
| 39 | + } as any, |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +describe("formatSkillsCompact", () => { |
| 44 | + it("returns empty string for no skills", () => { |
| 45 | + expect(formatSkillsCompact([])).toBe(""); |
| 46 | + }); |
| 47 | + |
| 48 | + it("omits description, keeps name and location", () => { |
| 49 | + const out = formatSkillsCompact([makeSkill("weather", "Get weather data")]); |
| 50 | + expect(out).toContain("<name>weather</name>"); |
| 51 | + expect(out).toContain("<location>/skills/weather/SKILL.md</location>"); |
| 52 | + expect(out).not.toContain("Get weather data"); |
| 53 | + expect(out).not.toContain("<description>"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("filters out disableModelInvocation skills", () => { |
| 57 | + const hidden: Skill = { ...makeSkill("hidden"), disableModelInvocation: true }; |
| 58 | + const out = formatSkillsCompact([makeSkill("visible"), hidden]); |
| 59 | + expect(out).toContain("visible"); |
| 60 | + expect(out).not.toContain("hidden"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("escapes XML special characters", () => { |
| 64 | + const out = formatSkillsCompact([makeSkill("a<b&c")]); |
| 65 | + expect(out).toContain("a<b&c"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("is significantly smaller than full format", () => { |
| 69 | + const skills = Array.from({ length: 50 }, (_, i) => |
| 70 | + makeSkill(`skill-${i}`, "A moderately long description that takes up space in the prompt"), |
| 71 | + ); |
| 72 | + const compact = formatSkillsCompact(skills); |
| 73 | + expect(compact.length).toBeLessThan(6000); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +describe("applySkillsPromptLimits (via buildWorkspaceSkillsPrompt)", () => { |
| 78 | + it("tier 1: uses full format when under budget", () => { |
| 79 | + const skills = [makeSkill("weather", "Get weather data")]; |
| 80 | + const prompt = buildPrompt(skills, { maxChars: 50_000 }); |
| 81 | + expect(prompt).toContain("<description>"); |
| 82 | + expect(prompt).toContain("Get weather data"); |
| 83 | + expect(prompt).not.toContain("⚠️"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("tier 2: compact when full exceeds budget but compact fits", () => { |
| 87 | + const skills = Array.from({ length: 20 }, (_, i) => makeSkill(`skill-${i}`, "A".repeat(200))); |
| 88 | + const fullLen = formatSkillsForPrompt(skills).length; |
| 89 | + const compactLen = formatSkillsCompact(skills).length; |
| 90 | + const budget = Math.floor((fullLen + compactLen) / 2); |
| 91 | + // Verify preconditions: full exceeds budget, compact fits within overhead-adjusted budget |
| 92 | + expect(fullLen).toBeGreaterThan(budget); |
| 93 | + expect(compactLen + 150).toBeLessThan(budget); |
| 94 | + const prompt = buildPrompt(skills, { maxChars: budget }); |
| 95 | + expect(prompt).not.toContain("<description>"); |
| 96 | + // All skills preserved — distinct message, no "included X of Y" |
| 97 | + expect(prompt).toContain("compact format (descriptions omitted)"); |
| 98 | + expect(prompt).not.toContain("included"); |
| 99 | + expect(prompt).toContain("skill-0"); |
| 100 | + expect(prompt).toContain("skill-19"); |
| 101 | + }); |
| 102 | + |
| 103 | + it("tier 3: compact + binary search when compact also exceeds budget", () => { |
| 104 | + const skills = Array.from({ length: 100 }, (_, i) => makeSkill(`skill-${i}`, "description")); |
| 105 | + const prompt = buildPrompt(skills, { maxChars: 2000 }); |
| 106 | + expect(prompt).toContain("compact format, descriptions omitted"); |
| 107 | + expect(prompt).not.toContain("<description>"); |
| 108 | + expect(prompt).toContain("skill-0"); |
| 109 | + const match = prompt.match(/included (\d+) of (\d+)/); |
| 110 | + expect(match).toBeTruthy(); |
| 111 | + expect(Number(match![1])).toBeLessThan(Number(match![2])); |
| 112 | + expect(Number(match![1])).toBeGreaterThan(0); |
| 113 | + }); |
| 114 | + |
| 115 | + it("compact preserves all skills where full format would drop some", () => { |
| 116 | + const skills = Array.from({ length: 50 }, (_, i) => makeSkill(`skill-${i}`, "A".repeat(200))); |
| 117 | + const compactLen = formatSkillsCompact(skills).length; |
| 118 | + const budget = compactLen + 250; |
| 119 | + // Verify precondition: full format must not fit so tier 2 is actually exercised |
| 120 | + expect(formatSkillsForPrompt(skills).length).toBeGreaterThan(budget); |
| 121 | + const prompt = buildPrompt(skills, { maxChars: budget }); |
| 122 | + // All 50 fit in compact — no truncation, just compact notice |
| 123 | + expect(prompt).toContain("compact format"); |
| 124 | + expect(prompt).not.toContain("included"); |
| 125 | + expect(prompt).toContain("skill-0"); |
| 126 | + expect(prompt).toContain("skill-49"); |
| 127 | + }); |
| 128 | + |
| 129 | + it("count truncation + compact: shows included X of Y with compact note", () => { |
| 130 | + // 30 skills but maxCount=10, and full format of 10 exceeds budget |
| 131 | + const skills = Array.from({ length: 30 }, (_, i) => makeSkill(`skill-${i}`, "A".repeat(200))); |
| 132 | + const tenSkills = skills.slice(0, 10); |
| 133 | + const fullLen = formatSkillsForPrompt(tenSkills).length; |
| 134 | + const compactLen = formatSkillsCompact(tenSkills).length; |
| 135 | + const budget = compactLen + 200; |
| 136 | + // Verify precondition: full format of 10 skills exceeds budget |
| 137 | + expect(fullLen).toBeGreaterThan(budget); |
| 138 | + const prompt = buildPrompt(skills, { maxChars: budget, maxCount: 10 }); |
| 139 | + // Count-truncated (30→10) AND compact (full format of 10 exceeds budget) |
| 140 | + expect(prompt).toContain("included 10 of 30"); |
| 141 | + expect(prompt).toContain("compact format, descriptions omitted"); |
| 142 | + expect(prompt).not.toContain("<description>"); |
| 143 | + }); |
| 144 | + |
| 145 | + it("extreme budget: even a single compact skill overflows", () => { |
| 146 | + const skills = [makeSkill("only-one", "desc")]; |
| 147 | + // Budget so small that even one compact skill can't fit |
| 148 | + const prompt = buildPrompt(skills, { maxChars: 10 }); |
| 149 | + expect(prompt).not.toContain("only-one"); |
| 150 | + const match = prompt.match(/included (\d+) of (\d+)/); |
| 151 | + expect(match).toBeTruthy(); |
| 152 | + expect(Number(match![1])).toBe(0); |
| 153 | + }); |
| 154 | + |
| 155 | + it("count truncation only: shows included X of Y without compact note", () => { |
| 156 | + const skills = Array.from({ length: 20 }, (_, i) => makeSkill(`skill-${i}`, "short")); |
| 157 | + const prompt = buildPrompt(skills, { maxChars: 50_000, maxCount: 5 }); |
| 158 | + expect(prompt).toContain("included 5 of 20"); |
| 159 | + expect(prompt).not.toContain("compact"); |
| 160 | + expect(prompt).toContain("<description>"); |
| 161 | + }); |
| 162 | + |
| 163 | + it("compact budget reserves space for the warning line", () => { |
| 164 | + // Build skills whose compact output exactly equals the char budget. |
| 165 | + // Without overhead reservation the compact block would fit, but the |
| 166 | + // warning line prepended by the caller would push the total over budget. |
| 167 | + const skills = Array.from({ length: 50 }, (_, i) => makeSkill(`s-${i}`, "A".repeat(200))); |
| 168 | + const compactLen = formatSkillsCompact(skills).length; |
| 169 | + // Set budget = compactLen + 50 — less than the 150-char overhead reserve. |
| 170 | + // The function should NOT choose compact-only because the warning wouldn't fit. |
| 171 | + const prompt = buildPrompt(skills, { maxChars: compactLen + 50 }); |
| 172 | + // Should fall through to compact + binary search (some skills dropped) |
| 173 | + expect(prompt).toContain("included"); |
| 174 | + expect(prompt).not.toContain("<description>"); |
| 175 | + }); |
| 176 | + |
| 177 | + it("budget check uses compacted home-dir paths, not canonical paths", () => { |
| 178 | + // Skills with home-dir prefix get compacted (e.g. /home/user/... → ~/...). |
| 179 | + // Budget check must use the compacted length, not the longer canonical path. |
| 180 | + // If it used canonical paths, it would overestimate and potentially drop |
| 181 | + // skills that actually fit after compaction. |
| 182 | + const home = os.homedir(); |
| 183 | + const skills = Array.from({ length: 30 }, (_, i) => |
| 184 | + makeSkill( |
| 185 | + `skill-${i}`, |
| 186 | + "A".repeat(200), |
| 187 | + `${home}/.openclaw/workspace/skills/skill-${i}/SKILL.md`, |
| 188 | + ), |
| 189 | + ); |
| 190 | + // Compute compacted lengths (what the prompt will actually contain) |
| 191 | + const compactedSkills = skills.map((s) => ({ |
| 192 | + ...s, |
| 193 | + filePath: s.filePath.replace(home, "~"), |
| 194 | + })); |
| 195 | + const compactedCompactLen = formatSkillsCompact(compactedSkills).length; |
| 196 | + const canonicalCompactLen = formatSkillsCompact(skills).length; |
| 197 | + // Sanity: canonical paths are longer than compacted paths |
| 198 | + expect(canonicalCompactLen).toBeGreaterThan(compactedCompactLen); |
| 199 | + // Set budget between compacted and canonical lengths — only fits if |
| 200 | + // budget check uses compacted paths (correct) not canonical (wrong). |
| 201 | + const budget = Math.floor((compactedCompactLen + canonicalCompactLen) / 2) + 150; |
| 202 | + const prompt = buildPrompt(skills, { maxChars: budget }); |
| 203 | + // All 30 skills should be preserved in compact form (tier 2, no dropping) |
| 204 | + expect(prompt).toContain("skill-0"); |
| 205 | + expect(prompt).toContain("skill-29"); |
| 206 | + expect(prompt).not.toContain("included"); |
| 207 | + expect(prompt).toContain("compact format"); |
| 208 | + // Verify paths in output are compacted |
| 209 | + expect(prompt).toContain("~/"); |
| 210 | + expect(prompt).not.toContain(home); |
| 211 | + }); |
| 212 | + |
| 213 | + it("resolvedSkills in snapshot keeps canonical paths, not compacted", () => { |
| 214 | + const home = os.homedir(); |
| 215 | + const skills = Array.from({ length: 5 }, (_, i) => |
| 216 | + makeSkill(`skill-${i}`, "A skill", `${home}/.openclaw/workspace/skills/skill-${i}/SKILL.md`), |
| 217 | + ); |
| 218 | + const snapshot = buildWorkspaceSkillSnapshot("/fake", { |
| 219 | + entries: skills.map(makeEntry), |
| 220 | + }); |
| 221 | + // Prompt should use compacted paths |
| 222 | + expect(snapshot.prompt).toContain("~/"); |
| 223 | + // resolvedSkills should preserve canonical (absolute) paths |
| 224 | + expect(snapshot.resolvedSkills).toBeDefined(); |
| 225 | + for (const skill of snapshot.resolvedSkills!) { |
| 226 | + expect(skill.filePath).toContain(home); |
| 227 | + expect(skill.filePath).not.toMatch(/^~\//); |
| 228 | + } |
| 229 | + }); |
| 230 | +}); |
0 commit comments