The best way to control Claude Code, Codex and more from any computer or phone

main
ts 445 lines 17.1 KB
Raw
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { assert, it } from "@effect/vitest";
3 import * as Effect from "effect/Effect";
4 import * as FileSystem from "effect/FileSystem";
5 import * as Path from "effect/Path";
6
7 import { discoverAntigravitySkills } from "./AntigravitySkills.ts";
8 import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
9
10 const writeSkill = Effect.fn("writeSkill")(function* (directory: string, contents: string) {
11 const fileSystem = yield* FileSystem.FileSystem;
12 const path = yield* Path.Path;
13 yield* fileSystem.makeDirectory(directory, { recursive: true });
14 const skillPath = path.join(directory, "SKILL.md");
15 yield* fileSystem.writeFileString(skillPath, contents);
16 return skillPath;
17 });
18
19 const makeWorkspace = Effect.fn("makeWorkspace")(function* () {
20 const fileSystem = yield* FileSystem.FileSystem;
21 const path = yield* Path.Path;
22 const temporaryDirectory = yield* fileSystem.makeTempDirectoryScoped({
23 prefix: "t3-antigravity-skills-",
24 });
25 return {
26 cwd: path.join(temporaryDirectory, "workspace"),
27 userHome: path.join(temporaryDirectory, "home"),
28 profileDirectory: path.join(temporaryDirectory, "profile"),
29 };
30 });
31
32 it.layer(NodeServices.layer)("discoverAntigravitySkills", (it) => {
33 it.effect("does not read user skills from a nested project or from ~/.agents", () =>
34 Effect.gen(function* () {
35 const path = yield* Path.Path;
36 const input = yield* makeWorkspace();
37 const nested = { ...input, cwd: path.join(input.userHome, "AI", "Projects", "Something") };
38 const skillPath = yield* writeSkill(
39 path.join(input.userHome, ".gemini", "config", "skills", "review"),
40 "---\nname: review\ndescription: Review changes.\n---\n",
41 );
42 yield* writeSkill(
43 path.join(input.userHome, ".agents", "skills", "ignored"),
44 "---\nname: ignored\n---\n",
45 );
46
47 assert.deepEqual(yield* discoverAntigravitySkills(nested), [
48 {
49 name: "review",
50 description: "Review changes.",
51 path: skillPath,
52 scope: "user",
53 enabled: true,
54 },
55 ]);
56 assert.deepEqual(
57 (yield* discoverAntigravitySkills({ ...input, cwd: input.userHome })).map((skill) => [
58 skill.name,
59 skill.scope,
60 ]),
61 [
62 ["ignored", "project"],
63 ["review", "user"],
64 ],
65 );
66 }),
67 );
68
69 it.effect("reads skill names, descriptions and paths from the current native roots", () =>
70 Effect.gen(function* () {
71 const path = yield* Path.Path;
72 const input = yield* makeWorkspace();
73 const roots = [
74 { directory: path.join(input.userHome, ".gemini", "config", "skills"), scope: "user" },
75 { directory: path.join(input.cwd, ".gemini", "skills"), scope: "project" },
76 {
77 directory: path.join(input.userHome, ".gemini", "antigravity-cli", "skills"),
78 scope: "user",
79 },
80 { directory: path.join(input.cwd, ".agents", "skills"), scope: "project" },
81 ];
82 const expected = [];
83 for (const [index, root] of roots.entries()) {
84 const name = `review-${index}`;
85 const description = `Review changes in root ${index}.`;
86 const skillPath = yield* writeSkill(
87 path.join(root.directory, name),
88 `---\nname: ${name}\ndescription: ${description}\n---\n# Review\n`,
89 );
90 expected.push({ name, description, path: skillPath, scope: root.scope, enabled: true });
91 }
92
93 assert.deepEqual(yield* discoverAntigravitySkills(input), expected);
94 }),
95 );
96
97 it.effect("reads skills from enabled plugins in the instance's Antigravity profile", () =>
98 Effect.gen(function* () {
99 const fileSystem = yield* FileSystem.FileSystem;
100 const path = yield* Path.Path;
101 const workspace = yield* makeWorkspace();
102 const profileDirectory = path.join(workspace.userHome, "t3", "providers", "antigravity", "p");
103 const plugins = path.join(profileDirectory, "config", "plugins");
104 for (const plugin of ["demo", "off"]) {
105 yield* writeSkill(
106 path.join(plugins, plugin, "skills", `${plugin}-skill`),
107 `---\nname: ${plugin}-skill\ndescription: From ${plugin}.\n---\n`,
108 );
109 }
110 yield* fileSystem.writeFileString(
111 path.join(profileDirectory, "config", "config.json"),
112 JSON.stringify({ plugins: { off: { enabled: false } } }),
113 );
114 yield* fileSystem.writeFileString(path.join(plugins, ".DS_Store"), "x");
115 const input = { ...workspace, profileDirectory };
116
117 assert.deepEqual(
118 (yield* discoverAntigravitySkills(input)).map((skill) => skill.name),
119 ["demo-skill"],
120 );
121 }),
122 );
123
124 it.effect("uses native root order for duplicate names", () =>
125 Effect.gen(function* () {
126 const fileSystem = yield* FileSystem.FileSystem;
127 const path = yield* Path.Path;
128 const input = yield* makeWorkspace();
129 const roots = [
130 path.join(input.userHome, ".gemini", "config", "skills"),
131 path.join(input.cwd, ".gemini", "skills"),
132 path.join(input.userHome, ".gemini", "antigravity-cli", "skills"),
133 path.join(input.cwd, ".agents", "skills"),
134 path.join(input.cwd, ".agent", "skills"),
135 ];
136 for (const [index, root] of roots.entries()) {
137 yield* writeSkill(
138 path.join(root, `copy-${index}`),
139 `---\nname: review\ndescription: Copy ${index}.\n---\n`,
140 );
141 }
142
143 for (const [index, root] of roots.entries()) {
144 const skills = yield* discoverAntigravitySkills(input);
145 assert.equal(skills.length, 1);
146 assert.equal(skills[0]?.path, path.join(root, `copy-${index}`, "SKILL.md"));
147 yield* fileSystem.remove(root, { recursive: true });
148 }
149 }),
150 );
151
152 it.effect("loads a root skill without scanning its children", () =>
153 Effect.gen(function* () {
154 const fileSystem = yield* FileSystem.FileSystem;
155 const path = yield* Path.Path;
156 const input = yield* makeWorkspace();
157 const root = path.join(input.cwd, ".agents", "skills");
158 const skillPath = yield* writeSkill(root, "---\nname: root-skill\n---\n");
159 yield* writeSkill(path.join(root, "child"), "---\nname: child-skill\n---\n");
160
161 assert.deepEqual(yield* discoverAntigravitySkills(input), [
162 { name: "root-skill", path: skillPath, scope: "project", enabled: true },
163 ]);
164
165 yield* fileSystem.writeFileString(skillPath, "---\nname: [invalid\n---\n");
166 assert.deepEqual(yield* discoverAntigravitySkills(input), []);
167 }),
168 );
169
170 it.effect("uses the filename when valid frontmatter has no name", () =>
171 Effect.gen(function* () {
172 const fileSystem = yield* FileSystem.FileSystem;
173 const path = yield* Path.Path;
174 const input = yield* makeWorkspace();
175 const skillPath = yield* writeSkill(
176 path.join(input.cwd, ".agents", "skills", "not-the-name"),
177 "---\n---\n# Skill body\n",
178 );
179
180 assert.deepEqual(yield* discoverAntigravitySkills(input), [
181 { name: "SKILL", path: skillPath, scope: "project", enabled: true },
182 ]);
183
184 const lowerCasePath = path.join(path.dirname(skillPath), "skill.md");
185 yield* fileSystem.rename(skillPath, lowerCasePath);
186 assert.deepEqual(yield* discoverAntigravitySkills(input), [
187 { name: "skill", path: lowerCasePath, scope: "project", enabled: true },
188 ]);
189
190 yield* fileSystem.rename(lowerCasePath, path.join(path.dirname(skillPath), "SKILL.MD"));
191 assert.deepEqual(yield* discoverAntigravitySkills(input), []);
192 }),
193 );
194
195 it.effect("accepts native metadata delimiters after leading text", () =>
196 Effect.gen(function* () {
197 const path = yield* Path.Path;
198 const input = yield* makeWorkspace();
199 const skillPath = yield* writeSkill(
200 path.join(input.cwd, ".agents", "skills", "review"),
201 "Leading text.---\nname: review\ndescription: null\n---Skill body.",
202 );
203
204 assert.deepEqual(yield* discoverAntigravitySkills(input), [
205 { name: "review", path: skillPath, scope: "project", enabled: true },
206 ]);
207 }),
208 );
209
210 it.effect("ignores invalid files and deeper directories but keeps native hidden skills", () =>
211 Effect.gen(function* () {
212 const fileSystem = yield* FileSystem.FileSystem;
213 const path = yield* Path.Path;
214 const input = yield* makeWorkspace();
215 const root = path.join(input.cwd, ".agents", "skills");
216 const invalidSkills = [
217 ["plain", "# Missing frontmatter\n"],
218 ["broken", "---\nname: [unclosed\n---\n"],
219 ["scalar", "---\n42\n---\n"],
220 ["wrong-type", "---\nname: invalid\ndescription: {}\n---\n"],
221 ["blank-name", '---\nname: " "\n---\n'],
222 ] as const;
223 for (const [name, contents] of invalidSkills) {
224 yield* writeSkill(path.join(root, name), contents);
225 }
226 yield* writeSkill(path.join(root, "nested", "deep"), "---\nname: too-deep\n---\n");
227 yield* writeSkill(
228 path.join(input.cwd, ".claude", "skills", "wrong-provider"),
229 "---\nname: wrong-provider\n---\n",
230 );
231 yield* fileSystem.makeDirectory(path.join(root, ".not-a-skill"));
232 yield* fileSystem.writeFileString(path.join(root, "README.md"), "Not a skill.");
233 const skillPath = yield* writeSkill(
234 path.join(root, ".native-hidden-skill"),
235 "---\nname: native-name\ndescription: >\n Review the code\n and run tests.\n---\n",
236 );
237
238 assert.deepEqual(yield* discoverAntigravitySkills(input), [
239 {
240 name: "native-name",
241 description: "Review the code and run tests.",
242 path: skillPath,
243 scope: "project",
244 enabled: true,
245 },
246 ]);
247 }),
248 );
249
250 it.effect("uses native URI order within a root and skips an invalid higher root", () =>
251 Effect.gen(function* () {
252 const fileSystem = yield* FileSystem.FileSystem;
253 const path = yield* Path.Path;
254 const input = yield* makeWorkspace();
255 const root = path.join(input.cwd, ".agents", "skills");
256 yield* writeSkill(
257 path.join(input.userHome, ".gemini", "config", "skills", "review"),
258 "---\nname: [invalid\n---\n",
259 );
260 const nativeOrder = [" space-copy", "!-copy", "ø-copy", "a-copy"];
261 for (const name of nativeOrder) {
262 yield* writeSkill(path.join(root, name), "---\nname: review\n---\n");
263 }
264
265 for (const name of nativeOrder) {
266 assert.deepEqual(yield* discoverAntigravitySkills(input), [
267 {
268 name: "review",
269 path: path.join(root, name, "SKILL.md"),
270 scope: "project",
271 enabled: true,
272 },
273 ]);
274 yield* fileSystem.remove(path.join(root, name), { recursive: true });
275 }
276 }),
277 );
278
279 it.effect.skipIf(!symlinksSupported)(
280 "follows directory symlinks used to install shared skills",
281 () =>
282 Effect.gen(function* () {
283 const fileSystem = yield* FileSystem.FileSystem;
284 const path = yield* Path.Path;
285 const input = yield* makeWorkspace();
286 const sourceDirectory = path.join(input.userHome, "shared-review");
287 yield* writeSkill(sourceDirectory, "---\nname: review\n---\n");
288 const root = path.join(input.cwd, ".agents", "skills");
289 const linkedDirectory = path.join(root, "review");
290 yield* fileSystem.makeDirectory(root, { recursive: true });
291 yield* fileSystem.symlink(sourceDirectory, linkedDirectory);
292
293 assert.deepEqual(yield* discoverAntigravitySkills(input), [
294 {
295 name: "review",
296 path: path.join(linkedDirectory, "SKILL.md"),
297 scope: "project",
298 enabled: true,
299 },
300 ]);
301 }),
302 );
303
304 it.effect("does not spend the scan budget on skill bodies", () =>
305 Effect.gen(function* () {
306 const path = yield* Path.Path;
307 const input = yield* makeWorkspace();
308 const root = path.join(input.userHome, ".gemini", "config", "skills");
309 const expected = [];
310 for (let index = 0; index < 9; index += 1) {
311 const name = `large-${index}`;
312 const description = `Large skill ${index}. ${"y".repeat(2_000)}`;
313 const skillPath = yield* writeSkill(
314 path.join(root, name),
315 `---\nname: ${name}\ndescription: ${description}\n---\n${"x".repeat(950_000)}`,
316 );
317 expected.push({ name, description, path: skillPath, scope: "user", enabled: true });
318 }
319
320 assert.deepEqual(yield* discoverAntigravitySkills(input), expected);
321 }),
322 );
323
324 it.effect("rejects frontmatter that does not close within the per-skill limit", () =>
325 Effect.gen(function* () {
326 const path = yield* Path.Path;
327 const input = yield* makeWorkspace();
328 const root = path.join(input.cwd, ".agents", "skills");
329 const largeBodyPath = yield* writeSkill(
330 path.join(root, "large-body"),
331 `---\nname: large-body\n---\n${"x".repeat(2_000_000)}`,
332 );
333 assert.deepEqual(yield* discoverAntigravitySkills(input), [
334 { name: "large-body", path: largeBodyPath, scope: "project", enabled: true },
335 ]);
336
337 const skillPath = yield* writeSkill(
338 path.join(root, "oversized"),
339 `---\nname: oversized\ndescription: ${"x".repeat(1_000_000)}\n---\n`,
340 );
341 const result = yield* discoverAntigravitySkills(input).pipe(Effect.result);
342 assert.equal(result._tag, "Failure");
343 if (result._tag === "Failure") {
344 assert.equal(result.failure.reason, "scan-budget-exhausted");
345 assert.equal(result.failure.path, skillPath);
346 }
347 }),
348 );
349
350 it.effect("bounds the total frontmatter read size across skills", () =>
351 Effect.gen(function* () {
352 const path = yield* Path.Path;
353 const input = yield* makeWorkspace();
354 const root = path.join(input.cwd, ".agents", "skills");
355 for (let index = 0; index < 9; index += 1) {
356 yield* writeSkill(
357 path.join(root, `large-${index}`),
358 `---\nname: large-${index}\ndescription: ${"x".repeat(900_000)}\n---\n`,
359 );
360 }
361
362 const result = yield* discoverAntigravitySkills(input).pipe(Effect.result);
363 assert.equal(result._tag, "Failure");
364 if (result._tag === "Failure") {
365 assert.equal(result.failure.reason, "scan-budget-exhausted");
366 assert.equal(result.failure.path, path.join(root, "large-8", "SKILL.md"));
367 }
368 }),
369 );
370
371 it.effect(
372 "reads skills declared in global and workspace skills.json with filters and inherits",
373 () =>
374 Effect.gen(function* () {
375 const fileSystem = yield* FileSystem.FileSystem;
376 const path = yield* Path.Path;
377 const input = yield* makeWorkspace();
378
379 const sharedSkills = path.join(input.userHome, ".agents", "skills");
380 const keptPath = yield* writeSkill(
381 path.join(sharedSkills, "yagni"),
382 "---\nname: yagni\ndescription: Keep it simple.\n---\n",
383 );
384 yield* writeSkill(
385 path.join(sharedSkills, "experimental-draft"),
386 "---\nname: experimental-draft\n---\n",
387 );
388
389 const inheritedSkills = path.join(input.userHome, "inherited-skills");
390 const inheritedPath = yield* writeSkill(
391 path.join(inheritedSkills, "linter"),
392 "---\nname: linter\n---\n",
393 );
394 yield* writeSkill(path.join(inheritedSkills, "skipped"), "---\nname: skipped\n---\n");
395 // Backtracks exponentially under RegExp against the pattern below.
396 yield* writeSkill(
397 path.join(inheritedSkills, "a".repeat(40)),
398 "---\nname: backtrack\n---\n",
399 );
400
401 const projectSkills = path.join(input.cwd, "tools", "skills");
402 const projectPath = yield* writeSkill(
403 path.join(projectSkills, "deploy"),
404 "---\nname: deploy\n---\n",
405 );
406
407 const globalConfigDir = path.join(input.userHome, ".gemini", "config");
408 const inheritedConfigPath = path.join(input.userHome, "shared-skills.json");
409 yield* fileSystem.makeDirectory(globalConfigDir, { recursive: true });
410 yield* fileSystem.makeDirectory(path.join(input.cwd, ".agents"), { recursive: true });
411 yield* fileSystem.writeFileString(
412 inheritedConfigPath,
413 JSON.stringify({
414 inherits: [{ path: path.join(globalConfigDir, "skills.json") }],
415 entries: [{ path: "~/inherited-skills" }],
416 }),
417 );
418 yield* fileSystem.writeFileString(
419 path.join(globalConfigDir, "skills.json"),
420 JSON.stringify({
421 inherits: [{ path: inheritedConfigPath, include_only: ["linter", "(a*)*b"] }],
422 entries: [{ path: sharedSkills, exclude: ["experimental-.*"] }],
423 }),
424 );
425 yield* fileSystem.writeFileString(
426 path.join(input.cwd, ".agents", "skills.json"),
427 JSON.stringify({
428 entries: [{ path: "tools/skills" }],
429 }),
430 );
431
432 assert.deepEqual(yield* discoverAntigravitySkills(input), [
433 { name: "deploy", path: projectPath, scope: "project", enabled: true },
434 { name: "linter", path: inheritedPath, scope: "user", enabled: true },
435 {
436 name: "yagni",
437 description: "Keep it simple.",
438 path: keptPath,
439 scope: "user",
440 enabled: true,
441 },
442 ]);
443 }),
444 );
445 });