Artifact 3466cbca61404b7fbac49a78ef48868e6a41afde9d4fef035675d9bfd0a0f297:
- File
src/provider_fake.c
— part of check-in
[7e7550292a]
at
2026-11-18 12:30:18
on branch tracer-bullet
— Add the first durable agent loop in C
pic keeps its journal in its own libfossil-hosted SQLite database: an idempotent admission per request key, then one short transaction per state change, never held across provider or tool I/O. A replay-safe write tool reruns after a crash; a replay-unsafe exec records an unknown outcome and never reruns. exec runs under a supervisor that starts the command only after the started commit and kills its group if pic dies or is interrupted. A second pic on the same database is refused.
test/pic/crash.sh crashes at every commit and effect boundary and checks the journal against an external ledger, plus deadline, output redirection, background child, crash and interrupt orphaning, and second-writer cases. Vendored SQLite and libfossil match ctx byte for byte. (user: dmestas, size: 2126) [annotate] [blame] [check-ins using] [more...]
/* Scripted provider. The reply for a turn depends only on the script and the * turn number, so a restarted process asks for, and gets, the same reply. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "pic.h" #define PIC_SCRIPT_LINE_CAP 8192 static char *dup_string(const char *s) { size_t n = strlen(s) + 1; char *p = malloc(n); if (p) { memcpy(p, s, n); } return p; } void pic_reply_free(pic_reply *r) { free(r->tool); free(r->args); memset(r, 0, sizeof *r); } /* Lines: "text ANSWER", "tool write NAME TEXT", "tool exec COMMAND". */ static int parse(char *line, pic_reply *out, char *err, size_t errcap) { line[strcspn(line, "\n")] = '\0'; if (strncmp(line, "text ", 5) == 0) { out->kind = PIC_REPLY_TEXT; out->args = dup_string(line + 5); return out->args ? 0 : -1; } if (strncmp(line, "tool ", 5) == 0) { char *name = line + 5; char *space = strchr(name, ' '); if (!space) { (void)snprintf(err, errcap, "script tool line has no arguments"); return -1; } *space = '\0'; out->kind = PIC_REPLY_TOOL; out->tool = dup_string(name); out->args = dup_string(space + 1); return out->tool && out->args ? 0 : -1; } (void)snprintf(err, errcap, "unrecognized script line: %.60s", line); return -1; } int pic_provider_reply(const char *script, int64_t turn, pic_reply *out, char *err, size_t errcap) { char line[PIC_SCRIPT_LINE_CAP]; FILE *f = fopen(script, "r"); int64_t index = 0; int rc = -1; memset(out, 0, sizeof *out); if (!f) { (void)snprintf(err, errcap, "cannot open script %s", script); return -1; } while (fgets(line, sizeof line, f)) { if (index++ == turn) { rc = parse(line, out, err, errcap); break; } } if (index <= turn) { (void)snprintf(err, errcap, "script has no reply for turn %lld", (long long)turn); } fclose(f); if (rc != 0) { pic_reply_free(out); } return rc; }