Artifact c8376043a26a06369b80252049da8422cbbb838ad6c6f2981d840b3abe75eca0:
- File
test/pic/test_skills.c
— part of check-in
[ea7e1fa24c]
at
2026-11-19 21:20:10
on branch skills
— Skills, as Pi's
Each folder with a SKILL.md under ~/.pic/skills/ (and, once the workspace is trusted, WORKSPACE/.pic/skills/) is a skill: Pi's frontmatter, validation and discovery rules, symlinks followed (suit places them as links), nothing ever written there. The system prompt lists them in Pi's wording, frozen with the conversation; /skill:name expands to Pi's skill block where templates expand, so ask, chat and the terminal agree, and the slash menu lists them. pic skills [--json] lists them. The frontmatter parser is now shared with prompt templates. (user: dmestas, size: 8287) [annotate] [blame] [check-ins using] [more...]
/* Skills: discovery, Pi's validation and fallbacks, symlinks, the prompt * listing and /skill:name expansion. */ #include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.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); } int main(void) { test_load(); puts("PASS: skills: discovery, symlinks, validation, prompt listing, /skill:name expansion"); return 0; }