/* The inbox (docs/design/grapevine.md): another process's command for a
* conversation, one Pi RPC command line in a pic_inbox row. A sender inserts
* it through the narrow open (pic_open_narrow) with no lease and rings the
* conversation's bell; the process running a request in that conversation
* delivers it through the command table (commands.c), in one transaction
* with what it made. Delivered once, or settled as refused or expired;
* never twice. prompt, steer, follow_up and abort, same workspace. */
#ifndef PIC_INBOX_H
#define PIC_INBOX_H
#include <stddef.h>
#include "pic.h"
#define PIC_INBOX_STEER_SECONDS 600L /* default expiry of a steer or abort */
#define PIC_INBOX_FOLLOW_UP_SECONDS 86400L /* of a follow-up */
#define PIC_INBOX_MAX_SECONDS (7L * 86400L) /* the ceiling on any */
#define PIC_INBOX_LINE_CAP (16u << 20) /* a command line, as on stdin */
#define PIC_INBOX_PENDING_ROWS 100 /* per conversation */
#define PIC_INBOX_PENDING_BYTES (64LL << 20) /* per conversation */
#define PIC_INBOX_KEEP_MS (7LL * 86400000LL) /* a settled row's command and response */
#define PIC_INBOX_MAX_HOPS 8 /* grapevine.maxHops's default */
#define PIC_INBOX_MAX_WAKES 3 /* grapevine.maxWakes's default */
/* Pi's answer to a prompt that does not say how to join the running request */
#define PIC_INBOX_BUSY "Agent is already processing. Specify streamingBehavior ('steer' or 'followUp') to queue the message."
typedef struct {
const char *session; /* the conversation's id, resolved by the caller */
const char *key; /* the idempotency key; NULL: a random one */
const char *line; /* one Pi RPC command line: prompt, steer, follow_up or abort */
const char *sender; /* "cli", "attach", "remote:DEVICE", "session:JOURNAL_ID/CONVERSATION" */
long expires_s; /* 0: the default for its type */
long hops; /* 0 from a person or script; a peer's request's hops plus one */
long max_hops; /* refused over this (grapevine.maxHops); 0: PIC_INBOX_MAX_HOPS */
} pic_inbox_msg;
/* Accepts M in one transaction and rings the bell. A known key with the same
* command changes nothing. 0: committed (or already was); its key in key
* and its state in state. 1: refused, nothing written (s->err): a key with
* another command, a command the inbox does not take, a full inbox, no
* such conversation. -1: an error (s->err). */
int pic_inbox_accept(pic_store *s, const pic_inbox_msg *m, char *key, size_t key_cap, char *state, size_t state_cap);
/* Rings SESSION's bell: a sender that committed a row inside a transaction of its own (a wake's
* fire) rings once that transaction has committed. */
void pic_inbox_ring(pic_store *s, const char *session);
/* KEY's state, and its response line (malloc'd, NULL while pending or
* pruned) when RESPONSE is not NULL. 0; 1 when no row has the key; -1. */
int pic_inbox_status(pic_store *s, const char *key, char *state, size_t cap, char **response);
/* KEY settled as expired if it is pending past its expiry (pic send
* --wait, when nothing runs to settle it). 0 or -1. */
int pic_inbox_expire(pic_store *s, const char *key);
/* KEY, while still pending, settled as refused with WHY by its sender (an
* attach whose own request ended before delivering it). 1: done; 0: it had
* already left pending; -1: an error. Changes no request, needs no lease. */
int pic_inbox_withdraw(pic_store *s, const char *key, const char *why);
/* The worker's side, called by pic_run at each step while it holds the
* running request's lease: watches that conversation's bell (the watch
* descriptor in net.h) and delivers its pending messages in order. */
void pic_inbox_poll(pic_store *s);
/* Stops the watch; pic_run calls it as it returns. */
void pic_inbox_stop(void);
/* The hops of request SUBMISSION: the most of the messages that started or joined it (0 when
* none did), so what it sends carries one more. -1 on an error. */
long long pic_inbox_hops(pic_store *s, long long submission);
/* pic run --session SESSION with nothing admitted there (waking, docs/design/grapevine.md): the
* oldest pending prompt becomes a request, keyed inbox:KEY, after the UserPromptSubmit hooks see
* it with its sender as origin. Refused instead (on its row, with the reason) when a hook blocks it
* or MAX_WAKES (0: PIC_INBOX_MAX_WAKES) requests were started this way since a person's last
* prompt (a wake's fires, sender wake:ID, are bounded by their own limits and pass); expired when
* its time has passed. 1: admitted, its request in s->running; 0: none to
* admit; -1: an error (s->err). */
int pic_inbox_wake(pic_store *s, const char *session, long max_wakes);
/* Retention, run where pic prunes: pending rows past their expiry become
* expired; settled rows older than PIC_INBOX_KEEP_MS lose their command
* and response, keeping key, hash and state. 0 or -1. */
int pic_inbox_retain(pic_store *s, long long now_ms);
#endif