Artifact 63b1620e7e6f235caff244570d0feb206e1ee4797f566ed9376d918b5306af16:
- File
src/provider_fake.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: 6495) [annotate] [blame] [check-ins using]
/* Scripted provider. The reply for a turn depends only on the script, the turn * number and the attempt, so a restarted process asks for, and gets, the same * reply. It produces Messages API content blocks, so the journal and the * request builder see the same shapes as with the real provider. * * Script lines, one per turn: * text ANSWER the final answer * tool write NAME TEXT one tool call; several calls in one turn * tool exec COMMAND are separated by TABs * flaky K LINE attempts 1..K fail (retryable), then LINE * reject MESSAGE a non-retryable provider failure * refuse stop_reason refusal */ #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; } /* SELECT returning one text value with up to four text arguments. */ static char *sql_text(pic_store *s, const char *sql, const char *a, const char *b, const char *c, const char *d) { sqlite3_stmt *q = NULL; char *out = NULL; const char *args[4] = {a, b, c, d}; if (sqlite3_prepare_v2(s->db, sql, -1, &q, NULL) != SQLITE_OK) { return NULL; } for (int i = 0; i < 4 && i < sqlite3_bind_parameter_count(q); i++) { sqlite3_bind_text(q, i + 1, args[i], -1, SQLITE_STATIC); } if (sqlite3_step(q) == SQLITE_ROW && sqlite3_column_text(q, 0)) { out = dup_string((const char *)sqlite3_column_text(q, 0)); } sqlite3_finalize(q); return out; } /* One "write NAME TEXT" or "exec COMMAND" call as a tool_use block. */ static char *tool_block(pic_store *s, char *call, const char *id) { char *space = strchr(call, ' '); if (!space) { return NULL; } *space = '\0'; if (strcmp(call, "write") == 0) { char *name = space + 1, *text = strchr(name, ' '); if (!text) { return NULL; } *text++ = '\0'; return sql_text(s, "SELECT json_object('type','tool_use','id',?1,'name','write','input'," "json_object('path',?2,'text',?3))", id, name, text, NULL); } return sql_text(s, "SELECT json_object('type','tool_use','id',?1,'name',?2,'input',json_object('command',?3))", id, call, space + 1, NULL); } static int reply_line(pic_store *s, char *line, int64_t turn, int64_t attempt, pic_reply *out, int *retryable, char *err, size_t errcap) { if (strncmp(line, "flaky ", 6) == 0) { char *end; long k = strtol(line + 6, &end, 10); if (end == line + 6 || *end != ' ') { (void)snprintf(err, errcap, "script flaky line needs a count"); return -1; } if (attempt <= k) { *retryable = 1; (void)snprintf(err, errcap, "scripted transient failure on attempt %lld", (long long)attempt); return -1; } return reply_line(s, end + 1, turn, attempt, out, retryable, err, errcap); } if (strncmp(line, "reject ", 7) == 0) { (void)snprintf(err, errcap, "scripted rejection: %.200s", line + 7); return -1; } if (strcmp(line, "refuse") == 0) { out->content = dup_string("[]"); out->stop_reason = dup_string("refusal"); return out->content && out->stop_reason ? 0 : -1; } if (strncmp(line, "text ", 5) == 0) { out->content = sql_text(s, "SELECT json_array(json_object('type','text','text',?1))", line + 5, NULL, NULL, NULL); out->stop_reason = dup_string("end_turn"); return out->content && out->stop_reason ? 0 : -1; } if (strncmp(line, "tool ", 5) == 0) { char *content = dup_string("[]"), *call = line + 5; for (int ordinal = 0; call && content; ordinal++) { char *next = strchr(call, '\t'), id[64], *block, *grown; if (next) { *next++ = '\0'; } (void)snprintf(id, sizeof id, "toolu_fake_%lld_%d", (long long)turn, ordinal); block = tool_block(s, call, id); if (!block) { free(content); (void)snprintf(err, errcap, "malformed script tool call"); return -1; } grown = sql_text(s, "SELECT json_insert(?1, '$[#]', json(?2))", content, block, NULL, NULL); free(block); free(content); content = grown; call = next; } out->content = content; out->stop_reason = dup_string("tool_use"); return out->content && out->stop_reason ? 0 : -1; } (void)snprintf(err, errcap, "unrecognized script line: %.60s", line); return -1; } static int script_reply(pic_provider *p, pic_store *s, int64_t submission, int64_t turn, pic_reply *out, int *retryable, char *err, size_t errcap) { char line[PIC_SCRIPT_LINE_CAP]; FILE *f = fopen(p->state, "r"); int64_t index = 0, attempt = 0; sqlite3_stmt *q = NULL; int rc = -1; *retryable = 0; if (!f) { (void)snprintf(err, errcap, "cannot open script %s", (const char *)p->state); return -1; } if (sqlite3_prepare_v2(s->db, "SELECT count(*) FROM pic_generation WHERE submission_id=? AND turn=?", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_int64(q, 1, submission); sqlite3_bind_int64(q, 2, turn); if (sqlite3_step(q) == SQLITE_ROW) { attempt = sqlite3_column_int64(q, 0); } sqlite3_finalize(q); } while (fgets(line, sizeof line, f)) { if (index++ == turn) { line[strcspn(line, "\n")] = '\0'; rc = reply_line(s, line, turn, attempt, out, retryable, 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; } static void script_free(pic_provider *p) { free(p->state); p->state = NULL; } int pic_provider_script(pic_provider *p, const char *script) { p->reply = script_reply; p->free = script_free; p->state = dup_string(script); return p->state ? 0 : -1; }