/* The system prompt, in Pi's section order: preamble, tools, rules, project
* context, skills, working directory. Project instructions are found as Pi finds
* them: in each directory the first of AGENTS.override.md, AGENTS.md,
* AGENTS.MD, CLAUDE.md, CLAUDE.MD, from the filesystem root down to the
* workspace, after an optional global file ~/.pic/AGENTS.md. */
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "input.h"
#include "pic.h"
#include "skills.h"
#include "userdb.h"
#define INSTRUCTIONS_CAP (256u * 1024u) /* bytes kept from one instructions file */
static const char preamble[] =
"You are a coding assistant running inside pic, a durable coding agent harness. Help users inspect files, run "
"commands, and modify code.";
static const char tools_section[] =
"<tools>\n"
"- read: Read file contents\n"
"- bash: Execute bash commands (ls, grep, find, etc.)\n"
"- edit: Make precise file edits with exact text replacement, including multiple disjoint edits in one call\n"
"- write: Create or overwrite files\n"
"</tools>";
static const char rules_section[] =
"<rules>\n"
"- Use bash for file operations like ls, rg, find\n"
"- Use read to examine files instead of cat or sed.\n"
"- Use edit for targeted changes. Batch changes to one file in a single call.\n"
"- Each edits[].oldText must exactly and uniquely match the original file. Keep matches short and "
"non-overlapping; merge nearby changes without including large unchanged regions.\n"
"- Use write only for new files or complete rewrites.\n"
"- Be concise in your responses\n"
"- Show file paths clearly when working with files\n"
"</rules>";
static const char *const names[] = {"AGENTS.override.md", "AGENTS.md", "AGENTS.MD", "CLAUDE.md", "CLAUDE.MD"};
typedef struct {
char *buf;
size_t len, cap;
int failed;
} text;
static void add(text *t, const char *s, size_t n) {
if (t->failed) {
return;
}
if (t->len + n + 1 > t->cap) {
size_t cap = t->cap ? t->cap : 4096;
char *p;
while (cap < t->len + n + 1) {
cap *= 2;
}
if (!(p = realloc(t->buf, cap))) {
t->failed = 1;
return;
}
t->buf = p;
t->cap = cap;
}
memcpy(t->buf + t->len, s, n);
t->len += n;
t->buf[t->len] = '\0';
}
static void adds(text *t, const char *s) { add(t, s, strlen(s)); }
/* Appends one instructions block if path is a readable regular file. */
static int add_file(text *t, const char *path, int *any) {
size_t len = 0;
int cut = 0;
char *buf = pic_input_read(path, INSTRUCTIONS_CAP + 3, PIC_INPUT_TRUNCATE, &len, &cut), *body;
if (!buf) {
return 0;
}
body = len >= 3 && memcmp(buf, "\xEF\xBB\xBF", 3) == 0 ? buf + 3 : buf; /* drop a byte-order mark */
len -= (size_t)(body - buf);
if (len > INSTRUCTIONS_CAP) {
len = INSTRUCTIONS_CAP;
cut = 1;
}
if (!*any) {
adds(t, "<project_context>\nProject-specific instructions and guidelines:\n");
*any = 1;
}
adds(t, "\n<project_instructions path=\"");
adds(t, path);
adds(t, "\">\n");
add(t, body, len);
if (cut) {
adds(t, "\n[pic: instructions truncated at 256KB]");
}
free(buf);
adds(t, "\n</project_instructions>\n");
return 1;
}
/* The first instructions file in dir, if any. */
static void add_dir(text *t, const char *dir, int *any) {
for (size_t i = 0; i < sizeof names / sizeof names[0]; i++) {
char path[PATH_MAX];
if (snprintf(path, sizeof path, "%s/%s", strcmp(dir, "/") == 0 ? "" : dir, names[i]) >= (int)sizeof path) {
return;
}
if (add_file(t, path, any)) {
return;
}
}
}
/* The project instructions block for a workspace (resolved path in real),
* appended to t; nothing when there are no instruction files. */
static void project_context(text *t, char *real) {
char *home = getenv("HOME");
int any = 0;
if (home && *home) {
char global[PATH_MAX];
if (snprintf(global, sizeof global, "%s/.pic/AGENTS.md", home) < (int)sizeof global) {
(void)add_file(t, global, &any);
}
}
/* Outermost directory first, the workspace last. */
add_dir(t, "/", &any);
for (size_t i = 1; real[0] == '/' && real[i]; i++) {
if (real[i] == '/') {
real[i] = '\0';
add_dir(t, real, &any);
real[i] = '/';
}
}
if (strcmp(real, "/") != 0) {
add_dir(t, real, &any);
}
if (any) {
adds(t, "</project_context>");
}
}
char *pic_project_instructions(const char *workspace) {
text t = {NULL, 0, 0, 0};
char real[PATH_MAX];
if (!realpath(workspace, real)) {
(void)snprintf(real, sizeof real, "%s", workspace);
}
adds(&t, "");
project_context(&t, real);
if (t.failed) {
free(t.buf);
return NULL;
}
return t.buf;
}
char *pic_system_prompt(const char *workspace) {
text t = {NULL, 0, 0, 0};
char real[PATH_MAX], *instructions = pic_project_instructions(workspace);
if (!instructions) {
return NULL;
}
if (!realpath(workspace, real)) {
(void)snprintf(real, sizeof real, "%s", workspace);
}
adds(&t, preamble);
adds(&t, "\n\n");
adds(&t, tools_section);
adds(&t, "\n\n");
adds(&t, rules_section);
if (*instructions) {
adds(&t, "\n\n");
adds(&t, instructions);
}
free(instructions);
{
pic_skills skills;
char *listing;
if (pic_skills_load(&skills, real, pic_trust_get(real) == 1, NULL) == 0) {
listing = pic_skills_prompt(&skills);
if (listing && *listing) {
adds(&t, "\n\n");
adds(&t, listing);
}
free(listing);
pic_skills_free(&skills);
}
}
adds(&t, "\n\n<cwd>\n");
adds(&t, real);
adds(&t, "\n</cwd>");
if (t.failed) {
free(t.buf);
return NULL;
}
return t.buf;
}