Artifact 78841d720de3df7fc856ec80379b1fc4f94068ecdfd7ee1dfc10d1aa880e20ee:
- File
src/context.c
— part of check-in
[ffaf5a4afb]
at
2026-11-19 02:04:10
on branch stage-5
— Add follow-up conversations, forks and in-place instruction updates
Entries form a tree: a trigger links each new entry to its conversation head and moves the head, inside the inserting transaction. Requests are built from the path, so pic ask -c and an explicit id continue a conversation and pic fork branches one from a point that ends a turn. Each conversation freezes its opening prompt and tools; edited project instructions are appended once as an update at that point. New commands list, name and print conversations. Schema 5, with journals of earlier versions stepped up in place. (user: dmestas, size: 6137) [annotate] [blame] [check-ins using] [more...]
/* The system prompt, in Pi's section order: preamble, tools, rules, project * context, 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 "pic.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) { struct stat st; char buf[8192]; size_t kept = 0; int fd; if (stat(path, &st) != 0 || !S_ISREG(st.st_mode) || (fd = open(path, O_RDONLY | O_CLOEXEC)) < 0) { return 0; } 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"); for (int first = 1;; first = 0) { ssize_t n = read(fd, buf, sizeof buf); size_t off = 0; if (n < 0 && errno == EINTR) { continue; } if (n <= 0) { break; } if (first && n >= 3 && memcmp(buf, "\xEF\xBB\xBF", 3) == 0) { off = 3; /* drop a byte-order mark */ } if (kept + (size_t)n - off > INSTRUCTIONS_CAP) { add(t, buf + off, INSTRUCTIONS_CAP - kept); adds(t, "\n[pic: instructions truncated at 256KB]"); break; } add(t, buf + off, (size_t)n - off); kept += (size_t)n - off; } close(fd); 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); adds(&t, "\n\n<cwd>\n"); adds(&t, real); adds(&t, "\n</cwd>"); if (t.failed) { free(t.buf); return NULL; } return t.buf; }