Skip to content
pi-c Artifact [3fc9883e7a]
Login

Artifact [3fc9883e7a]

Artifact 3fc9883e7a09bf6b61a7656c5d06ae4493ed1e14c0511fd5266efa72ffdf991e:


/* 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>");
    }
}

static const char *replace_arg;
static const char *append_arg[PIC_SYSTEM_APPEND_MAX];
static size_t append_n;

void pic_system_prompt_args(const char *replace, const char *const *append, size_t n) {
    replace_arg = replace;
    append_n = n < PIC_SYSTEM_APPEND_MAX ? n : PIC_SYSTEM_APPEND_MAX;
    for (size_t i = 0; i < append_n; i++) {
        append_arg[i] = append[i];
    }
}

/* The contents of the regular file at path (a byte-order mark dropped), or NULL. */
static char *read_text(const char *path) {
    size_t len = 0;
    int cut = 0;
    char *buf = pic_input_read(path, INSTRUCTIONS_CAP, PIC_INPUT_TRUNCATE, &len, &cut);
    if (buf && len >= 3 && memcmp(buf, "\xEF\xBB\xBF", 3) == 0) {
        memmove(buf, buf + 3, len - 2);
    }
    return buf;
}

/* Pi's resolvePromptInput: the file's contents when input names a readable
 * file, else input itself. NULL for an empty input. */
static char *prompt_input(const char *input) {
    char *text;
    if (!*input) {
        return NULL;
    }
    text = read_text(input);
    return text ? text : strdup(input);
}

/* Pi's discoverSystemPromptFile: WORKSPACE/.pic/NAME when the workspace is
 * trusted, else ~/.pic/NAME. */
static char *discovered(const char *real, const char *name) {
    char path[PATH_MAX], *home = getenv("HOME"), *text = NULL;
    if (pic_trust_get(real) == 1 && snprintf(path, sizeof path, "%s/.pic/%s", real, name) < (int)sizeof path) {
        text = read_text(path);
    }
    if (!text && home && *home && snprintf(path, sizeof path, "%s/.pic/%s", home, name) < (int)sizeof path) {
        text = read_text(path);
    }
    return text;
}

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);
    }
    {
        char *custom = replace_arg ? prompt_input(replace_arg) : discovered(real, "SYSTEM.md");
        if (custom) { /* Pi's custom prompt replaces the preamble, the tools and the rules */
            adds(&t, custom);
            free(custom);
        } else {
            adds(&t, preamble);
            adds(&t, "\n\n");
            adds(&t, tools_section);
            adds(&t, "\n\n");
            adds(&t, rules_section);
        }
    }
    {
        text extra = {NULL, 0, 0, 0};
        if (append_n == 0) {
            char *found = discovered(real, "APPEND_SYSTEM.md");
            if (found) {
                adds(&extra, found);
                free(found);
            }
        }
        for (size_t i = 0; i < append_n; i++) {
            char *one = prompt_input(append_arg[i]);
            if (one) {
                adds(&extra, extra.len ? "\n\n" : "");
                adds(&extra, one);
                free(one);
            }
        }
        if (extra.len) {
            adds(&t, "\n\n<addendum>\n");
            adds(&t, extra.buf);
            adds(&t, "\n</addendum>");
        }
        t.failed |= extra.failed;
        free(extra.buf);
    }
    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;
}