Files
splinter/tests/test_agents.c
T
lotherkandClaude Sonnet 5.5 b8db78816a agent profiles: Tab switches build/plan/yours; files in ~/.splinter/agents, .agents/
build (all tools) and plan (read-only plus delegate/plan_task) are built in; a
markdown file with opencode-style front matter (description, model, tools,
allow, deny, autonomous) adds or replaces one. Tool policy is enforced in the
agent loop, the prompt is added to the system message, /agent and --agent.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-11 23:05:12 +00:00

110 lines
3.5 KiB
C

/*
* splinter -- agent profiles: files, precedence, policy, prompt.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "splinter.h"
#include "agents.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static void
put(const char *path, const char *text)
{
FILE *f = fopen(path, "w");
if (f == NULL) {
failures++;
return;
}
fputs(text, f);
fclose(f);
}
int
main(void)
{
char root[] = "/var/tmp/splinter-agents-XXXXXX";
char home[100], proj[100], sub[100], cmd[600], p[400];
struct sp_agents a;
struct sp_tool_policy pol;
struct sp_buf b;
int i;
CHECK(mkdtemp(root) != NULL);
snprintf(home, sizeof(home), "%s/home", root);
snprintf(proj, sizeof(proj), "%s/home/proj", root);
snprintf(sub, sizeof(sub), "%s/home/proj/src", root);
snprintf(cmd, sizeof(cmd), "mkdir -p '%s/.splinter/agents' '%s/.agents' '%s'",
home, proj, sub);
CHECK(system(cmd) == 0);
/* user level: an agent, and a plan that replaces the built-in one */
snprintf(p, sizeof(p), "%s/.splinter/agents/review.md", home);
put(p, "---\ndescription: Reviews code\nmodel: analyst\ntools: read\n"
"allow: [delegate*, plan_task_add]\ndeny: [run]\nautonomous: false\n"
"---\nYou review. Be strict.\n");
snprintf(p, sizeof(p), "%s/.splinter/agents/helper.md", home);
put(p, "---\nmode: subagent\ndescription: not for the chat\n---\nx\n");
/* project level: wins over the user's, and a plain file without front matter */
snprintf(p, sizeof(p), "%s/.agents/review.md", proj);
put(p, "---\ndescription: Project review\ntools: mutate\n---\nProject rules.\n");
snprintf(p, sizeof(p), "%s/.agents/docs.md", proj);
put(p, "Write docs only.\n");
snprintf(p, sizeof(p), "%s/.agents/README.md", proj);
put(p, "not an agent\n");
CHECK(sp_agents_load(&a, sub, home) == 0);
CHECK(strcmp(a.v[0].name, "build") == 0 && a.v[0].max_auth == -1);
CHECK(strcmp(a.v[1].name, "plan") == 0 && a.v[1].max_auth == 0);
i = sp_agents_find(&a, "review");
CHECK(i >= 2);
CHECK(strcmp(a.v[i].description, "Project review") == 0);
CHECK(a.v[i].max_auth == 1);
CHECK(strstr(a.v[i].source, "/.agents/review.md") != NULL);
CHECK(sp_agents_find(&a, "helper") < 0);
CHECK(sp_agents_find(&a, "README") < 0);
i = sp_agents_find(&a, "docs");
CHECK(i >= 2 && strstr(a.v[i].prompt, "Write docs only") != NULL);
CHECK(a.v[i].max_auth == -1);
/* from the home directory alone the user's own review is seen */
sp_agents_free(&a);
CHECK(sp_agents_load(&a, home, home) == 0);
i = sp_agents_find(&a, "review");
CHECK(i >= 0 && strcmp(a.v[i].description, "Reviews code") == 0);
CHECK(a.v[i].max_auth == 0 && a.v[i].autonomous == 0);
CHECK(strcmp(a.v[i].model, "analyst") == 0);
CHECK(strcmp(a.v[i].allow, "delegate*,plan_task_add") == 0);
CHECK(strcmp(a.v[i].deny, "run") == 0);
sp_agent_policy(&a.v[i], &pol);
CHECK(pol.max_auth == 0 && strcmp(pol.name, "review") == 0);
/* the prompt is the base plus the profile's text */
sp_buf_init(&b);
CHECK(sp_agent_prompt(&a.v[i], "BASE", &b) == 0);
CHECK(strncmp(b.p, "BASE", 4) == 0 && strstr(b.p, "You review. Be strict.") != NULL);
sp_buf_free(&b);
sp_buf_init(&b);
CHECK(sp_agent_prompt(&a.v[0], "BASE", &b) == 0 && strcmp(b.p, "BASE") == 0);
sp_buf_free(&b);
sp_agents_free(&a);
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", root);
if (system(cmd) != 0)
failures++;
if (failures == 0)
puts("test_agents: ok");
return failures != 0;
}