/* Skills: discovery, Pi's validation and fallbacks, symlinks, the prompt
* listing and /skill:name expansion. */
#include <assert.h>
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "ignore.h"
#include "skills.h"
static char root[512], home[600], ws[600];
static void put(const char *dir, const char *file, const char *text) {
char path[1000];
FILE *f;
(void)snprintf(path, sizeof path, "%s/%s", dir, file);
f = fopen(path, "w");
assert(f && fputs(text, f) >= 0);
fclose(f);
}
static void dirs(const char *base, const char *sub) {
char path[1000];
(void)snprintf(path, sizeof path, "%s/%s", base, sub);
assert(mkdir(path, 0700) == 0);
}
static const pic_skill *has(const pic_skills *s, const char *name) {
const pic_skill *k = pic_skills_find(s, name);
if (!k) {
for (size_t i = 0; i < s->n; i++) {
fprintf(stderr, "have %s\n", s->v[i].name);
}
fprintf(stderr, "want %s\n", name);
}
assert(k);
return k;
}
static void test_load(void) {
char path[1000], link[1000];
FILE *wf;
char wbuf[4096];
pic_skills s;
const pic_skill *k;
size_t n;
char *out, err[256], *prompt;
(void)snprintf(root, sizeof root, "%s/pic-skills-XXXXXX", getenv("TMPDIR") && *getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
assert(mkdtemp(root));
(void)snprintf(home, sizeof home, "%s/home", root);
(void)snprintf(ws, sizeof ws, "%s/ws", root);
assert(mkdir(home, 0700) == 0 && mkdir(ws, 0700) == 0);
dirs(home, ".pic");
dirs(home, ".pic/skills");
dirs(ws, ".pic");
dirs(ws, ".pic/skills");
assert(setenv("HOME", home, 1) == 0);
assert(pic_skills_load(&s, ws, 1, NULL) == 0 && s.n == 0);
prompt = pic_skills_prompt(&s);
assert(prompt && *prompt == '\0');
free(prompt);
pic_skills_free(&s);
/* a skill reached through a symlink, with every frontmatter form */
dirs(root, "store");
dirs(root, "store/alpha");
put(root, "store/alpha/SKILL.md",
"---\nname: alpha\ndescription: \"Does <alpha> & more\"\n---\n\n# Alpha\nUse it.\n\n");
(void)snprintf(path, sizeof path, "%s/store/alpha", root);
(void)snprintf(link, sizeof link, "%s/.pic/skills/alpha", home);
assert(symlink(path, link) == 0);
/* a folder without a name: the folder's name; hidden from the model */
dirs(home, ".pic/skills/beta");
put(home, ".pic/skills/beta/SKILL.md", "---\ndescription: Beta skill\ndisable-model-invocation: true\n---\nBeta body\n");
/* no description: not loaded; invalid name: loaded with a warning */
dirs(home, ".pic/skills/nodesc");
put(home, ".pic/skills/nodesc/SKILL.md", "---\nname: nodesc\n---\nbody\n");
dirs(home, ".pic/skills/Bad_Name");
put(home, ".pic/skills/Bad_Name/SKILL.md", "---\ndescription: odd name\n---\nbody\n");
/* a root .md file counts only with a description; nested folders are searched */
put(home, ".pic/skills/rootfile.md", "---\nname: rootfile\ndescription: a loose file\n---\nloose\n");
put(home, ".pic/skills/plain.md", "no frontmatter\n");
dirs(home, ".pic/skills/group");
dirs(home, ".pic/skills/group/nested");
put(home, ".pic/skills/group/nested/SKILL.md", "---\ndescription: nested one\n---\nnested\n");
put(home, ".pic/skills/group/loose.md", "---\ndescription: not at the root\n---\nx\n");
/* a SKILL.md ends the search below it */
dirs(home, ".pic/skills/beta/inner");
put(home, ".pic/skills/beta/inner/SKILL.md", "---\ndescription: inside a skill\n---\nx\n");
/* a broken symlink is skipped quietly */
(void)snprintf(link, sizeof link, "%s/.pic/skills/dangling", home);
assert(symlink("/nonexistent/skill/dir", link) == 0);
/* a project skill with the user's name loses */
dirs(ws, ".pic/skills/alpha");
put(ws, ".pic/skills/alpha/SKILL.md", "---\ndescription: project alpha\n---\nx\n");
dirs(ws, ".pic/skills/proj");
put(ws, ".pic/skills/proj/SKILL.md", "---\ndescription: only the project has it\n---\nProject body\n");
wf = tmpfile();
assert(wf);
assert(pic_skills_load(&s, ws, 0, wf) == 0);
assert(s.n == 5); /* alpha, Bad_Name, beta, nested, rootfile (sorted by discovery) */
assert(!pic_skills_find(&s, "proj") && !pic_skills_find(&s, "nodesc") && !pic_skills_find(&s, "plain"));
assert(!pic_skills_find(&s, "inner") && !pic_skills_find(&s, "loose") && !pic_skills_find(&s, "dangling"));
k = has(&s, "alpha");
assert(!k->project && !k->hidden && strcmp(k->description, "Does <alpha> & more") == 0);
assert(strstr(k->path, "/home/.pic/skills/alpha/SKILL.md") && strstr(k->dir, "/home/.pic/skills/alpha"));
assert(has(&s, "beta")->hidden && strcmp(has(&s, "beta")->description, "Beta skill") == 0);
assert(strcmp(has(&s, "rootfile")->description, "a loose file") == 0);
assert(has(&s, "nested") && has(&s, "Bad_Name"));
rewind(wf);
n = fread(wbuf, 1, sizeof wbuf - 1, wf);
wbuf[n] = '\0';
fclose(wf);
assert(strstr(wbuf, "nodesc/SKILL.md: description is required"));
assert(strstr(wbuf, "name contains invalid characters"));
assert(!strstr(wbuf, "dangling"));
pic_skills_free(&s);
/* the project's skills are added when trusted; the user's name wins a collision */
wf = tmpfile();
assert(pic_skills_load(&s, ws, 1, wf) == 0 && s.n == 6);
assert(has(&s, "proj")->project && !has(&s, "alpha")->project);
rewind(wf);
n = fread(wbuf, 1, sizeof wbuf - 1, wf);
wbuf[n] = '\0';
fclose(wf);
assert(strstr(wbuf, "name \"alpha\" collision"));
/* Pi's listing: hidden skills left out, XML escaped */
prompt = pic_skills_prompt(&s);
assert(prompt);
{
static const char head[] =
"The following skills provide specialized instructions for specific tasks.\n"
"Use the read tool to load a skill's file when the task matches its description.\n"
"Resolve relative paths against the skill's directory (parent of SKILL.md). Use absolute paths in tool calls.\n"
"\n<available_skills>\n <skill>\n";
assert(strncmp(prompt, head, sizeof head - 1) == 0);
}
assert(strstr(prompt, " <skill>\n <name>alpha</name>\n <description>Does <alpha> & more</description>\n <location>"));
assert(strstr(prompt, "/.pic/skills/alpha/SKILL.md</location>\n </skill>"));
assert(!strstr(prompt, "<name>beta</name>"));
assert(strstr(prompt, "<name>proj</name>"));
assert(strcmp(prompt + strlen(prompt) - 20, "\n</available_skills>") == 0);
free(prompt);
/* expansion */
assert(pic_skills_expand(&s, "hello", &out, err, sizeof err) == 0 && !out);
assert(pic_skills_expand(&s, "/skill:unknown x", &out, err, sizeof err) == 0 && !out);
assert(pic_skills_expand(&s, "/other", &out, err, sizeof err) == 0 && !out);
assert(pic_skills_expand(&s, "/skill:proj", &out, err, sizeof err) == 1);
(void)snprintf(path, sizeof path, "<skill name=\"proj\" location=\"%s/.pic/skills/proj/SKILL.md\">\n"
"References are relative to %s/.pic/skills/proj.\n\nProject body\n</skill>", ws, ws);
assert(strcmp(out, path) == 0);
free(out);
assert(pic_skills_expand(&s, "/skill:alpha do the\nthing ", &out, err, sizeof err) == 1);
assert(strstr(out, "\n\n# Alpha\nUse it.\n</skill>\n\ndo the\nthing"));
assert(strcmp(out + strlen(out) - 9, "the\nthing") == 0);
free(out);
assert(pic_skills_expand(&s, "/skill:beta", &out, err, sizeof err) == 1); /* hidden is still a command */
free(out);
/* a skill that vanishes after listing is reported at use */
(void)snprintf(path, sizeof path, "%s/.pic/skills/proj/SKILL.md", ws);
assert(unlink(path) == 0);
assert(pic_skills_expand(&s, "/skill:proj", &out, err, sizeof err) == -1 && !out);
assert(strstr(err, "proj") && strstr(err, "gone"));
pic_skills_free(&s);
/* and one gone before loading is just not there */
assert(pic_skills_load(&s, ws, 1, NULL) == 0 && !pic_skills_find(&s, "proj"));
pic_skills_free(&s);
}
static int remove_one(const char *path, const struct stat *st, int flag, struct FTW *ftw) {
(void)st;
(void)flag;
(void)ftw;
return remove(path);
}
static int ign(const char *rules, const char *prefix, const char *path, int isdir) {
pic_ignore ig = {0};
int r;
assert(pic_ignore_add(&ig, rules, strlen(rules), prefix) == 0);
r = pic_ignore_ignored(&ig, path, isdir);
pic_ignore_free(&ig);
return r;
}
static void test_ignore(void) {
assert(ign("foo\n", "", "foo", 1) && ign("foo\n", "", "a/b/foo", 1));
assert(!ign("# foo\n\n", "", "foo", 0) && ign("\\#foo\n", "", "#foo", 0));
assert(ign("build/\n", "", "build", 1) && !ign("build/\n", "", "build", 0));
assert(ign("build/\n", "", "build/x/SKILL.md", 0));
assert(ign("*.md\n!keep.md\n", "", "a.md", 0) && !ign("*.md\n!keep.md\n", "", "keep.md", 0));
assert(ign("a/*\n!a/keep\n", "", "a/x", 1) && !ign("a/*\n!a/keep\n", "", "a/keep", 1));
assert(ign("/foo\n", "", "foo", 1)); /* Pi drops the leading slash */
assert(ign("a/**/z\n", "", "a/z", 0) && ign("a/**/z\n", "", "a/b/c/z", 0) && !ign("a/**/z\n", "", "b/a/z", 0));
assert(ign("**/x\n", "", "p/q/x", 0) && ign("a/**\n", "", "a/b", 0) && !ign("a/**\n", "", "a", 0));
assert(ign("s?.md\n", "", "s1.md", 0) && !ign("s?.md\n", "", "s/.md", 0));
assert(ign("[a-c]x\n", "", "bx", 0) && !ign("[a-c]x\n", "", "dx", 0) && ign("[!a]x\n", "", "bx", 0));
assert(ign("a*b\n", "", "axxb", 0) && !ign("a*b\n", "", "a/b", 0));
assert(ign("x \r\ny\r\n", "", "x", 0) && ign("x \r\ny\r\n", "", "y", 0));
assert(ign("\\!x\n", "", "!x", 0));
/* a nested file's rules are prefixed by its folder: anchored there */
assert(ign("tmp\n", "pkg/", "pkg/tmp", 1) && !ign("tmp\n", "pkg/", "tmp", 1) && !ign("tmp\n", "pkg/", "pkg/a/tmp", 1));
assert(ign("*.md\n", "pkg/", "pkg/r.md", 0) && !ign("*.md\n", "pkg/", "other/r.md", 0));
}
static int fig(const char *rules, const char *prefix, const char *path, int isdir) {
pic_ignore ig = {0};
int r;
assert(pic_ignore_add_glob(&ig, rules, strlen(rules), prefix) == 0);
r = pic_ignore_ignored(&ig, path, isdir);
pic_ignore_free(&ig);
return r;
}
static void test_fossil_glob(void) {
/* separators: commas, spaces, tabs, newlines, in any mix */
assert(fig("a,b c\td\ne", "", "a", 0) && fig("a,b c\td\ne", "", "b", 0) && fig("a,b c\td\ne", "", "d", 0) &&
fig("a,b c\td\ne", "", "e", 0) && !fig("a,b c\td\ne", "", "c d", 0));
assert(fig(" ,\n a,,\r\n", "", "a", 0) && !fig(" ,\n", "", "x", 0));
/* quotes keep spaces and commas inside one pattern */
assert(fig("\"my file.txt\" other", "", "my file.txt", 0) && fig("\"my file.txt\" other", "", "other", 0) &&
!fig("\"my file.txt\" other", "", "my", 0));
assert(fig("\"a,b\"", "", "a,b", 0) && !fig("\"a,b\"", "", "a", 0));
/* '*' crosses '/'; unlike gitignore a bare name is matched against the whole path */
assert(fig("*.o", "", "src/deep/x.o", 0) && fig("src/*.c", "", "src/a/b.c", 0));
assert(!fig("x.o", "", "src/x.o", 0) && fig("x.o", "", "x.o", 0));
assert(fig("a?c,[xy]z", "", "abc", 0) && fig("a?c,[xy]z", "", "yz", 0) && !fig("a?c", "", "ac", 0));
/* a pattern naming a folder hides what is under it; a dir star pattern matches files there */
assert(fig("build", "", "build/out/x.o", 0) && fig("dir/*", "", "dir/f", 0) && fig("dir/*", "", "dir/s/f", 0));
assert(fig("build/", "", "build", 1) && !fig("build/", "", "build", 0));
/* no negation, no last-match-wins: any match ignores */
assert(fig("!x", "", "!x", 0) && !fig("!x", "", "x", 0));
{
pic_ignore ig = {0};
assert(pic_ignore_add(&ig, "!k.md\n", 6, "") == 0 && pic_ignore_add_glob(&ig, "*.md", 4, "") == 0);
assert(pic_ignore_ignored(&ig, "k.md", 0));
pic_ignore_free(&ig);
}
/* a subfolder's .fslignore is relative to that folder */
assert(fig("tmp *.o", "pkg/", "pkg/tmp", 1) && fig("tmp *.o", "pkg/", "pkg/a/b.o", 0));
assert(!fig("tmp", "pkg/", "tmp", 1) && !fig("tmp", "pkg/", "pkg/a/tmp", 1) && !fig("*.o", "pkg/", "other/x.o", 0));
/* dispatch by file name */
{
pic_ignore ig = {0};
assert(pic_ignore_add_named(&ig, ".fslignore", "a b", 3, "") == 0);
assert(pic_ignore_ignored(&ig, "b", 0) && !pic_ignore_ignored(&ig, "x/b", 0));
pic_ignore_free(&ig);
assert(pic_ignore_add_named(&ig, ".zitignore", "b/\n", 3, "") == 0);
assert(pic_ignore_ignored(&ig, "x/b", 1));
pic_ignore_free(&ig);
}
}
int main(void) {
test_ignore();
test_fossil_glob();
test_load();
assert(nftw(root, remove_one, 16, FTW_DEPTH | FTW_PHYS) == 0); /* symlinks removed, not followed */
puts("PASS: skills: discovery, symlinks, validation, prompt listing, /skill:name expansion");
return 0;
}