Artifact 78a538a817af23135a561569a998b8f59b41d89d1ffc76a2309dbe131b055ef1:
- File
src/main.c
— part of check-in
[417d9dcb01]
at
2026-11-18 18:04:16
on branch stage-2
— Wire the Messages API provider into the durable loop
The journal moves to schema 2: assistant entries hold the provider content verbatim, one turn may carry several tool calls with their call ids, and every provider request is a journaled generation. An interrupted request fails that attempt and the turn is asked again, up to three attempts. The provider builds requests from the journal, retries only before a stream starts, and reads the key just before sending it. A local fake server drives fourteen end-to-end cases, including a crash mid-stream and a check that the key never reaches the journal or output; the crash matrix grows to cover generations and parallel calls. (user: dmestas, size: 4001) [annotate] [blame] [check-ins using]
/* pic command line: submit work, run it durably, inspect the journal. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include "pic.h" static int usage(void) { fputs("usage: pic --db PATH [--workspace DIR] submit KEY TEXT\n" " pic --db PATH --workspace DIR [--exec-timeout SECONDS] run --script FILE\n" " pic --db PATH --workspace DIR [--exec-timeout SECONDS] run --provider anthropic\n" " [--model MODEL] [--api-url URL] (key from ANTHROPIC_API_KEY)\n" " pic --db PATH dump | check\n", stderr); return 2; } int main(int argc, char **argv) { const char *db = NULL, *workspace = NULL, *script = NULL; int exec_seconds = 30; struct stat st; pic_store s; int i = 1, rc = 0; while (i + 1 < argc && strncmp(argv[i], "--", 2) == 0) { if (strcmp(argv[i], "--db") == 0) { db = argv[i + 1]; } else if (strcmp(argv[i], "--workspace") == 0) { workspace = argv[i + 1]; } else if (strcmp(argv[i], "--exec-timeout") == 0) { char *end = NULL; long v = strtol(argv[i + 1], &end, 10); if (!end || *end || v < 1 || v > 3600) { return usage(); } exec_seconds = (int)v; } else { return usage(); } i += 2; } if (!db || i >= argc) { return usage(); } /* A missing workspace would otherwise be recorded as a permanent tool error. */ if (workspace && (stat(workspace, &st) != 0 || !S_ISDIR(st.st_mode))) { fprintf(stderr, "pic: workspace is not a directory: %s\n", workspace); return 1; } if (pic_open(&s, db) != 0) { fprintf(stderr, "pic: %s\n", s.err); pic_close(&s); return 1; } if (strcmp(argv[i], "submit") == 0 && argc - i == 3) { int duplicate = 0; rc = pic_submit(&s, argv[i + 1], argv[i + 2], &duplicate); if (rc == 0) { puts(duplicate ? "duplicate: already admitted" : "admitted"); } } else if (strcmp(argv[i], "run") == 0 && workspace) { const char *provider_name = NULL, *model = "claude-opus-5-5"; const char *url = "https://api.anthropic.com/v1/messages"; pic_provider provider; memset(&provider, 0, sizeof provider); for (int j = i + 1; j < argc; j += 2) { if (j + 1 >= argc) { pic_close(&s); return usage(); } if (strcmp(argv[j], "--script") == 0) { script = argv[j + 1]; } else if (strcmp(argv[j], "--provider") == 0) { provider_name = argv[j + 1]; } else if (strcmp(argv[j], "--model") == 0) { model = argv[j + 1]; } else if (strcmp(argv[j], "--api-url") == 0) { url = argv[j + 1]; } else { pic_close(&s); return usage(); } } if (script && !provider_name) { rc = pic_provider_script(&provider, script); } else if (!script && provider_name && strcmp(provider_name, "anthropic") == 0) { rc = pic_provider_anthropic(&provider, url, model, s.err, sizeof s.err); } else { pic_close(&s); return usage(); } if (rc == 0) { rc = pic_run(&s, workspace, &provider, exec_seconds); } if (provider.free) { provider.free(&provider); } } else if (strcmp(argv[i], "dump") == 0 && argc - i == 1) { rc = pic_dump(&s); } else if (strcmp(argv[i], "check") == 0 && argc - i == 1) { rc = pic_check(&s); if (rc == 0) { puts("check: ok"); } } else { pic_close(&s); return usage(); } if (rc != 0) { fprintf(stderr, "pic: %s\n", s.err); } pic_close(&s); return rc == 0 ? 0 : 1; }