/* Skills: discovery, Pi's validation and fallbacks, symlinks, the prompt * listing and /skill:name expansion. */ #include #include #include #include #include #include #include #include "ignore.h" #include "skills.h" #include "userdb.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, 0, NULL); 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 & 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 & 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, 0, NULL); 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\n \n"; assert(strncmp(prompt, head, sizeof head - 1) == 0); } assert(strstr(prompt, " \n alpha\n Does <alpha> & more\n ")); assert(strstr(prompt, "/.pic/skills/alpha/SKILL.md\n ")); assert(!strstr(prompt, "beta")); assert(strstr(prompt, "proj")); assert(strcmp(prompt + strlen(prompt) - 20, "\n") == 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, "\n" "References are relative to %s/.pic/skills/proj.\n\nProject body\n", 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\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); } /* ---- the listing's budget, its ranking and its warning --------------------- */ static char broot[512], bhome[600], bws[600]; static char *words(size_t n) { /* about N characters of 4-letter words and spaces */ char *w = malloc(n + 6); size_t i = 0; assert(w); while (i < n) { memcpy(w + i, "word ", 5); i += 5; } w[n] = '\0'; return w; } /* A new home holding skills big (a long description), u1..u5 (200 characters each) and * hid (long, and hidden from the model); HOME and the user database follow. */ static void budget_home(int with_big) { char path[1000], desc[300], *w; pic_userdb_close(); (void)snprintf(broot, sizeof broot, "%s/pic-budget-XXXXXX", getenv("TMPDIR") && *getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); assert(mkdtemp(broot)); (void)snprintf(bhome, sizeof bhome, "%s/home", broot); (void)snprintf(bws, sizeof bws, "%s/ws", broot); assert(mkdir(bhome, 0700) == 0 && mkdir(bws, 0700) == 0); dirs(bhome, ".pic"); dirs(bhome, ".pic/skills"); assert(setenv("HOME", bhome, 1) == 0); for (int i = 1; i <= 5; i++) { char name[64], body[600]; (void)snprintf(name, sizeof name, ".pic/skills/u%d", i); dirs(bhome, name); (void)snprintf(name, sizeof name, ".pic/skills/u%d/SKILL.md", i); (void)snprintf(desc, sizeof desc, "u%d %0*d", i, 197, 0); (void)snprintf(body, sizeof body, "---\ndescription: %s\n---\nbody %d\n", desc, i); put(bhome, name, body); } if (with_big) { dirs(bhome, ".pic/skills/big"); w = words(12000); (void)snprintf(path, sizeof path, ".pic/skills/big/SKILL.md"); { char *body = malloc(13000); assert(body); (void)snprintf(body, 13000, "---\ndescription: %s\n---\nbig body\n", w); put(bhome, path, body); free(body); } free(w); dirs(bhome, ".pic/skills/hid"); w = words(12000); { char *body = malloc(13100); assert(body); (void)snprintf(body, 13100, "---\ndescription: %s\ndisable-model-invocation: true\n---\nhid body\n", w); put(bhome, ".pic/skills/hid/SKILL.md", body); free(body); } free(w); } } /* The description of skill NAME in listing P, or NULL when it is shown by name only. */ static const char *entry_of(const char *p, const char *name, int *has_desc) { char key[64]; const char *e; (void)snprintf(key, sizeof key, "%s\n <", name); e = strstr(p, key); assert(e); *has_desc = strncmp(e + strlen(key), "description>", 12) == 0; return e; } static int named_only(const char *p, const char *name) { int d; (void)entry_of(p, name, &d); return !d; } static size_t count_of(const char *p, const char *needle) { size_t n = 0; for (; (p = strstr(p, needle)); p += strlen(needle)) { n++; } return n; } static void test_budget(void) { static const char note[] = "Some skills are shown by name only to save context: read the SKILL.md at its location, or run /skill:NAME, to load one.\n"; pic_skills s; char *whole, *trimmed, *p, *w = NULL, want[600], *out, err[256], path[1000]; size_t full_len, trim_len, dl = strlen(" ") + 200 + strlen("\n"); const char *e; int d; /* a fitting listing is untouched: the same bytes whatever the budget, no warning */ budget_home(1); assert(unsetenv("PIC_SKILLS_NOW_MS") == 0); assert(pic_skills_load(&s, bws, 0, NULL) == 0 && s.n == 7); assert(pic_setting_set("skillBudget", "0") == 0); whole = pic_skills_prompt(&s, 1000, &w); assert(whole && !w); full_len = strlen(whole); assert(!strstr(whole, "hid") && !strstr(whole, "shown by name only") && !strstr(whole, "\xe2\x80\xa6")); assert(pic_setting_set("skillBudget", NULL) == 0); p = pic_skills_prompt(&s, 100000000, &w); /* 1% of the window x 4 is far above it */ assert(p && !w && strcmp(p, whole) == 0); free(p); /* the default is 1% of the window x 4 characters: 8000 for 200000 tokens (the listing is 14000+) */ p = pic_skills_prompt(&s, 200000, &w); assert(p && w && strstr(w, "budget of 8000 characters")); free(p); free(w); p = pic_skills_prompt(&s, 0, &w); /* a window not known is taken as 200000 */ assert(p && w && strstr(w, "budget of 8000 characters")); free(p); free(w); /* the setting overrides it, an unusable value is ignored */ assert(pic_setting_set("skillBudget", "9000") == 0); p = pic_skills_prompt(&s, 1000000, &w); assert(p && w && strstr(w, "budget of 9000 characters") && strlen(p) <= 9000); free(p); free(w); assert(pic_setting_set("skillBudget", "lots") == 0); p = pic_skills_prompt(&s, 200000, &w); assert(p && w && strstr(w, "budget of 8000 characters")); free(p); free(w); /* first the long descriptions: cut at a word, with an ellipsis; the others and every skill stay */ assert(pic_setting_set("skillBudget", "9000") == 0); trimmed = pic_skills_prompt(&s, 1, &w); assert(trimmed && w && strlen(trimmed) <= 9000 && !strstr(trimmed, "shown by name only")); trim_len = strlen(trimmed); e = entry_of(trimmed, "big", &d); assert(d); { const char *from = strstr(e, "") + 13, *to = strstr(from, ""); assert(to - from <= 1536 + 3 && memcmp(to - 3, "\xe2\x80\xa6", 3) == 0 && to[-4] == 'd'); /* "...word" then the ellipsis */ assert(memcmp(from, "word word ", 10) == 0); } assert(!named_only(trimmed, "u1") && !named_only(trimmed, "u5") && count_of(trimmed, "") == 6); (void)snprintf(want, sizeof want, "the skill listing is over its budget of 9000 characters (skillBudget): 1 with a shortened description (big). " "Every skill still loads with /skill:NAME."); assert(strcmp(w, want) == 0); free(w); w = NULL; /* then the least used skills by name only: with no use, the last in the listing go first */ (void)snprintf(want, sizeof want, "%zu", trim_len + strlen(note) - 2 * dl); assert(pic_setting_set("skillBudget", want) == 0); p = pic_skills_prompt(&s, 1, &w); assert(p && strlen(p) <= trim_len + strlen(note) - 2 * dl && strstr(p, note)); assert(named_only(p, "u5") && named_only(p, "u4") && !named_only(p, "u3") && !named_only(p, "u1") && !named_only(p, "big")); assert(count_of(p, "") == 6); (void)snprintf(want, sizeof want, "the skill listing is over its budget of %zu characters (skillBudget): 2 shown by name only (u4, u5); " "1 with a shortened description (big). Every skill still loads with /skill:NAME.", trim_len + strlen(note) - 2 * dl); assert(w && strcmp(w, want) == 0); free(p); free(w); w = NULL; /* a skill loaded by /skill:NAME or by a read of its file moves up; hidden skills are never counted */ assert(pic_skills_expand(&s, "/skill:u5", &out, err, sizeof err) == 1); free(out); assert(pic_skills_expand(&s, "/skill:u5 again", &out, err, sizeof err) == 1); free(out); (void)snprintf(path, sizeof path, "%s/.pic/skills/u4/SKILL.md", bhome); pic_skills_note_read(path); (void)snprintf(path, sizeof path, "%s/.pic/skills/u4/notes.md", bhome); /* another file of a skill is not a use */ pic_skills_note_read(path); (void)snprintf(path, sizeof path, "%s/elsewhere/SKILL.md", bhome); /* nor is a file outside the skills folder */ pic_skills_note_read(path); (void)snprintf(want, sizeof want, "%zu", trim_len + strlen(note) - 2 * dl); p = pic_skills_prompt(&s, 1, NULL); assert(p && named_only(p, "u2") && named_only(p, "u3") && !named_only(p, "u4") && !named_only(p, "u5") && !named_only(p, "u1")); free(p); /* nothing is dropped: with no room at all every skill is listed by name and location */ assert(pic_setting_set("skillBudget", "10") == 0); p = pic_skills_prompt(&s, 1, &w); assert(p && count_of(p, "") == 6 && count_of(p, "") == 0 && strstr(w, "6 shown by name only")); free(p); free(w); free(whole); free(trimmed); pic_skills_free(&s); /* the clock: use decays by half a week, to a floor of a tenth */ budget_home(0); assert(pic_skills_load(&s, bws, 0, NULL) == 0 && s.n == 5); assert(pic_setting_set("skillBudget", "0") == 0); whole = pic_skills_prompt(&s, 1, NULL); full_len = strlen(whole); free(whole); (void)snprintf(want, sizeof want, "%zu", full_len + strlen(note) - 4 * dl); /* room for one description */ assert(pic_setting_set("skillBudget", want) == 0); assert(setenv("PIC_SKILLS_NOW_MS", "1000000000000", 1) == 0); for (int i = 0; i < 3; i++) { assert(pic_skills_expand(&s, "/skill:u1", &out, err, sizeof err) == 1); free(out); } assert(setenv("PIC_SKILLS_NOW_MS", "1002592000000", 1) == 0); /* 30 days on: u1 is worth 3 x 0.1 */ assert(pic_skills_expand(&s, "/skill:u2", &out, err, sizeof err) == 1); /* and one use today is worth 1 */ free(out); p = pic_skills_prompt(&s, 1, NULL); assert(p && named_only(p, "u1") && !named_only(p, "u2") && named_only(p, "u3") && named_only(p, "u5")); free(p); assert(setenv("PIC_SKILLS_NOW_MS", "1000000000000", 1) == 0); /* before the decay: u1's 3 uses win */ p = pic_skills_prompt(&s, 1, NULL); assert(p && !named_only(p, "u1") && named_only(p, "u2")); free(p); assert(unsetenv("PIC_SKILLS_NOW_MS") == 0); pic_skills_free(&s); pic_userdb_close(); } 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(); test_budget(); assert(nftw(root, remove_one, 16, FTW_DEPTH | FTW_PHYS) == 0); /* symlinks removed, not followed */ assert(nftw(broot, remove_one, 16, FTW_DEPTH | FTW_PHYS) == 0); puts("PASS: skills: discovery, symlinks, validation, prompt listing, its budget and ranking, /skill:name expansion"); return 0; }