Artifact 1cf0849d95b7b326e6c5cb2ad0874c49bac54f4313f8e5db64c260e62bc294a8:
- File
src/tree.c
— part of check-in
[af92ae84b2]
at
2026-11-19 22:04:56
on branch steering
— Durable steering and follow-ups, as Pi's
With --json --commands, a running pic reads Pi's RPC command lines on stdin (steer, follow_up, clear_queue, abort, set_steering_mode, set_follow_up_mode) wherever it already waits, and answers as Pi does. Each accepted message is journaled (pic_queued, journal version 8) before it is acknowledged. Steering becomes a user entry after the turn's tool results, before the next model call (and after a compaction); follow-ups become the next request when the answer is recorded, in the same transaction. Messages belong to the request that accepted them; on a failure or an abort they are marked returned and listed in a queue_returned event, never run later. pic check verifies each delivered entry against its queued row. Bash commands get /dev/null on stdin, as in Pi. (user: dmestas, size: 2904) [annotate] [blame] [check-ins using] [more...]
/* The conversation tree (Pi's /tree): the requests of every branch that * shares a root with SESSION, one JSON line each, in tree order. Forks share * entries, so a branch point is a request with several follow-ups. */ #include <stdio.h> #include "pic.h" /* A request starts at its first user entry; steering delivered into it is * not a request of its own. seq: the request's user entry; end: its last entry (where a branch from it * continues); parent: the request before it; indent: how many branch points * lie above; active: on SESSION's path; sessions: ids whose head is in it. */ int pic_tree_json(pic_store *s, const char *session, FILE *out) { sqlite3_stmt *q = NULL; char id[64]; if (pic_session_resolve(s, session, id, sizeof id) != 0) { return -1; } if (sqlite3_prepare_v2( s->db, "WITH RECURSIVE cur(seq) AS (SELECT head_seq FROM pic_session WHERE id = ?1" " UNION ALL SELECT e.parent_seq FROM pic_entry e JOIN cur ON e.seq = cur.seq WHERE e.parent_seq IS NOT NULL)," " turn(seq, parent, path, indent) AS (" " SELECT seq, NULL, printf('%09d', seq), 0 FROM pic_entry WHERE kind = 'user' AND seq = (SELECT min(seq) FROM cur)" " UNION ALL" " SELECT c.seq, t.seq, t.path || printf('%09d', c.seq)," " t.indent + ((SELECT count(*) FROM pic_entry k WHERE k.kind = 'user' AND k.parent_seq IN" " (SELECT seq FROM pic_entry WHERE submission_id = (SELECT submission_id FROM pic_entry WHERE seq = t.seq))" " AND k.submission_id != (SELECT submission_id FROM pic_entry WHERE seq = t.seq)) > 1)" " FROM turn t JOIN pic_entry c ON c.kind = 'user' AND c.parent_seq IN" " (SELECT seq FROM pic_entry WHERE submission_id = (SELECT submission_id FROM pic_entry WHERE seq = t.seq))" " AND c.submission_id != (SELECT submission_id FROM pic_entry WHERE seq = t.seq))" " SELECT json_object('seq', t.seq, 'parent', t.parent," " 'end', (SELECT max(x.seq) FROM pic_entry x WHERE x.submission_id = e.submission_id)," " 'indent', t.indent, 'active', json(iif(t.seq IN (SELECT seq FROM cur), 'true', 'false'))," " 'text', e.body," " 'sessions', (SELECT group_concat(s2.id, ',') FROM pic_session s2 WHERE s2.head_seq IN" " (SELECT seq FROM pic_entry WHERE submission_id = e.submission_id)))" " FROM turn t JOIN pic_entry e ON e.seq = t.seq ORDER BY t.path", -1, &q, NULL) != SQLITE_OK) { (void)snprintf(s->err, sizeof s->err, "sql: %s", sqlite3_errmsg(s->db)); return -1; } sqlite3_bind_text(q, 1, id, -1, SQLITE_STATIC); while (sqlite3_step(q) == SQLITE_ROW) { fprintf(out, "%s\n", (const char *)sqlite3_column_text(q, 0)); } sqlite3_finalize(q); return 0; }