Skip to content
pi-c Documentation
Login

Documentation

/* Wakes (docs/design/wakes.md): "send this conversation this prompt when that happens". A loop
 * fires on the clock; a monitor on new output from a command; a goal when a request ends with the
 * goal unmet. A fire is one IMMEDIATE transaction that advances the pic_wake row and inserts the
 * inbox row wake:ID:N (sender wake:ID), so the inbox's deliver-once rule is what stops a double
 * fire after a crash. Nothing keeps time: whoever has the journal open asks what is due (the
 * terminal interface, pic remote, a running request's poll, pic loop fire). People see loop,
 * monitor and goal; "wake" is the internal word. */
#ifndef PIC_WAKE_H
#define PIC_WAKE_H

#include <stddef.h>

#include "pic.h"
#include "rpc.h"

#define PIC_WAKE_MAX 8                           /* per conversation, not ended */
#define PIC_WAKE_EXPIRY_MS (7LL * 86400000LL)    /* a wake ends 7 days after it was made */
#define PIC_WAKE_MIN_EVERY_MS 60000LL            /* the shortest interval */
#define PIC_WAKE_SELF_PACED_MS 600000LL          /* a loop given no interval */
#define PIC_WAKE_LATE_MS 90000LL                 /* due this long ago and not fired: nothing is firing */

/* SQL: the wake that started request U (a pic_submission alias), or NULL: one a wake admitted
 * (inbox:wake:ID:N) or a wake's follow-up became (queued-ID, its inbox row from wake:ID). */
#define PIC_WAKE_OF_SQL(U)                                                                                             \
    "(CASE WHEN " U ".request_key LIKE 'inbox:wake:%' THEN CAST(substr(" U ".request_key, 12) AS INTEGER)"            \
    " WHEN " U ".request_key LIKE 'queued-%' THEN (SELECT CAST(substr(w_i.sender, 6) AS INTEGER) FROM pic_inbox w_i"   \
    " WHERE w_i.queued_id = CAST(substr(" U ".request_key, 8) AS INTEGER) AND w_i.sender LIKE 'wake:%') END)"

/* The wall clock wakes keep time by, in ms (dev builds: PIC_WAKE_NOW_MS when set, for tests). */
long long pic_wake_now_ms(void);

/* What core asks of the monitor runner (monitor.c, registered by the composition root): a
 * monitor's command started detached in a session of its own, holding none of the starter's
 * descriptors, its output appended to SPOOL; SPOOL.pid names it and SPOOL.exit gets its status. */
typedef struct pic_wake_monitor_ops {
    int (*start)(const char *command, const char *cwd, const char *spool, long *pid, long long *token); /* 0 or -1 */
    int (*alive)(long pid, long long token);       /* that process, still running */
    long long (*size)(const char *spool);          /* -1: no spool */
    char *(*read)(const char *spool, long long from, long long to, size_t *len); /* malloc'd */
    int (*exit_status)(const char *spool);         /* 0..255, or -1 when it has not exited so */
    long (*pidfile)(const char *spool);            /* the pid SPOOL.pid names, or -1 */
    void (*stop)(long pid, long long token);       /* SIGTERM to its group, if it is still that process */
    long long (*token)(long pid);                  /* a process's start token (procs.h), 0 when unknown */
} pic_wake_monitor_ops;
void pic_wake_monitors(const pic_wake_monitor_ops *ops);

#define PIC_WAKE_GOAL_ROUNDS 50 /* a goal's rounds before it needs the person */
#define PIC_WAKE_GOAL_STALLS 3  /* rounds in a row with no progress before it does */
#define PIC_WAKE_MONITOR_BYTES 8192 /* the most of a monitor's new output one fire sends */

/* A new wake. */
typedef struct {
    const char *session;  /* its conversation; NULL or "": a new, empty one */
    const char *kind;     /* "loop" or "monitor" */
    const char *command;  /* a monitor's, run with /bin/sh -c in WORKSPACE */
    const char *workspace;
    int done, restart;    /* a monitor: wake once when the command exits; start it again when it died with the computer */
    const char *check;    /* a goal's check command (NULL: the model says when it is met) */
    long rounds;          /* a goal's most rounds (0: PIC_WAKE_GOAL_ROUNDS) */
    const char *prompt;   /* what each fire sends */
    long long every_ms;   /* a loop's interval; 0: PIC_WAKE_SELF_PACED_MS */
    const char *creator;  /* "user" (the terminal), "cli", "remote:DEVICE", "model" */
    const char *model;    /* "provider/id" its runs use while the conversation has run none (NULL: the default) */
} pic_wake_new;

/* Makes W: a loop due at once; a monitor with its command started (fault point monitor_started:
 * the command runs, its process not yet recorded). 0 with its id in *ID and its conversation in SESSION_OUT; 1 refused
 * (s->err: a bad interval, too many in the conversation, no such conversation); -1. */
int pic_wake_create(pic_store *s, const pic_wake_new *w, long long *id, char *session_out, size_t cap);

/* At the stop gate of SUBMISSION, whose reply stands, outside any transaction: when its
 * conversation has an active goal and S has a stop policy, the goal's check runs (once, journaled
 * as a GoalCheck hook run; fault point after-goal-check-started) and the policy is asked
 * (StopPolicy, pic_hooks_ask: the first answer stands). 0 or -1. */
int pic_wake_goal_judge(pic_store *s, long long submission, const char *workspace, int exec_seconds);
/* Whether SUBMISSION's conversation has an active goal, so its reply waits for the stop gate. */
int pic_wake_goal_active(pic_store *s, long long submission);
/* Inside the transaction that makes SUBMISSION's reply the answer: the journaled answer acted on.
 * The goal ends (met, or declared done), needs the person (blocked, its rounds or stalls reached),
 * or fires its next round (the inbox row wake:ID:N). *FIRED 1 when it fired. 0 or -1. */
int pic_wake_goal_apply(pic_store *s, long long submission, int *fired);
/* The wake tool's mark (goal.c): the active goal of SUBMISSION's conversation declared done or
 * blocked, with REASON, for the stop policy to read at that request's end. 0; 1 when there is no
 * active goal (s->err); -1. */
int pic_wake_goal_declare(pic_store *s, long long submission, const char *state, const char *reason);

/* The wake tool's loop_next: the loop that started SUBMISSION runs next in MS (60 s to an hour). 0;
 * 1 when no loop started it, or MS is out of range (s->err); -1. */
int pic_wake_loop_next(pic_store *s, long long submission, long long ms);

/* ID (as "L1", "1") of S's journal, into *ID. 0, or 1 when it names none (s->err). */
int pic_wake_resolve(pic_store *s, const char *name, long long *id);

/* ACTION "pause", "resume" or "stop" on wake ID, with WHY (NULL: the action's own words). 0
 * done; 1 refused (ended, or no such wake; s->err); -1. */
int pic_wake_set(pic_store *s, long long id, const char *action, const char *why);

/* /stop: every active wake of SESSION paused, how many in *count. 0 or -1. */
int pic_wake_pause_all(pic_store *s, const char *session, const char *why, int *count);

/* Inside the cancel's transaction: the wake that started request SUBMISSION, if any and active,
 * is paused (Esc on work a wake started). 0 or -1. */
int pic_wake_cancelled(pic_store *s, long long submission);

/* What is due now: every active wake of S's journal (of SESSION alone when not NULL) whose time
 * has come fires, each in its own transaction. With START, a conversation that has a wake's fire
 * waiting in its inbox and no pic working it gets a pic run, started through the registered
 * starter (WORKSPACE is the journal's). How many fired in *fired; the next due time (wall ms,
 * 0: none) in *next_ms. 0 or -1. Fault point wake_fire: arrival 1 before a fire's commit, 2
 * after it. */
int pic_wake_due(pic_store *s, const char *workspace, const char *session, int start, int *fired, long long *next_ms);

/* Who starts a pic run for a conversation with a fire waiting: the composition root registers it
 * (peer.c's). DB is the journal's path. 0 when it started one. */
void pic_wake_starter(int (*start)(const char *db, const char *workspace, const char *session));

/* The wakes of SESSION (NULL: the journal's), not ended unless ALL, as a JSON array (malloc'd),
 * each {id, name, kind, session, prompt, everyMs, every, state, why, creator, fires, dueMs,
 * firedMs, createdMs, expiresMs, cost, tokens, late}; NULL with s->err. */
char *pic_wake_list(pic_store *s, const char *session, int all);

/* "5m", "90s", "2h", "1d" (a number alone is minutes) as ms. 0, or 1 when it is not one. */
int pic_wake_parse_every(const char *text, long long *ms);
/* MS as the shortest of those that says it exactly. */
void pic_wake_every_text(long long ms, char *out, size_t cap);

/* The RPC commands (core's, for every client): get_wakes, create_wake, pause_wake, resume_wake,
 * stop_wake, pause_wakes. */
extern const pic_rpc_command pic_get_wakes_rpc, pic_create_wake_rpc, pic_pause_wake_rpc, pic_resume_wake_rpc,
    pic_stop_wake_rpc, pic_pause_wakes_rpc;

/* pic usage --by-wake: fires, requests, tokens and cost per wake. */
int pic_usage_by_wake(pic_store *s, int json, FILE *out);

#endif