Artifact 4e9c6e239da0158b60b330af320f3e8c7e1aafef94105a560ea041596b2e9f5e:
- File docs/design/wakes.md — part of check-in [970716ec11] at 2026-11-24 20:08:10 on branch wake-2 — Loops with the terminal closed: pic remote fires them, and a phone lists, pauses, resumes and stops them (user: dmestas, size: 6260) [annotate] [blame] [check-ins using] [more...]
Loops, monitors and goals
Status: signed off 2026-11-24 (the proposal, with the owner's answers, is at
https://artifacts.craftdesign.group/v/JPi1WSWo57ggWxSnlNBtW#55b0e7bfbb4fe47e8b1ba9a173a7749e230ed87432e0dacceb5570c0dfb079b2).
Step 1, the record and /loop, built 2026-11-24; step 2, pic remote and the phone, built
2026-11-24. Steps 3 and 4 (/monitor, /goal with the stop policy and the model's tool) follow.
The shape
One journaled record, a wake: "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. People see loop, monitor and goal; "wake" is the internal word.
Tier: core (src/wake.c). Clock: durability. Record: a pic_wake row per wake, a pic_inbox row
per fire. Default: no wakes; nothing fires.
The commands (/loop, pic loop), the list and the footer are clients of the RPC commands
get_wakes, create_wake, pause_wake, resume_wake, stop_wake and pause_wakes, which are
core's, as the record is. Tier: client (src/tui/) and root (main.c). Clock: taste. Record: none
of their own. Default: on.
The fire
One IMMEDIATE transaction: the wake is read again (still active, its time come, no fire of it
waiting), its fire count, last fire and next due time advance, and the inbox row is inserted with
msg_key wake:ID:N and sender wake:ID. A crash before the commit leaves nothing; after it the
inbox row is pending and the inbox delivers it once. Two processes that both see
a due wake queue on SQLite's write lock; the second finds it advanced. The bell is rung after the
commit.
A fire is a prompt with streamingBehavior followUp: with a request running it is a follow-up
and becomes the next request; with none, a pic run takes it up as a request keyed
inbox:wake:ID:N. Its text is the prompt, then one bracketed line naming the loop, its interval
and its run, and saying it was not typed just now (after the prompt, so a conversation a loop
began is titled by the prompt); a fire that stands for missed runs says how many, and the last
says it is the last.
Who keeps time
No timer. Whoever has the journal open asks what is due (pic_wake_due): a read first, then a fire
per due wake. The terminal interface asks through get_wakes with fire: true, every 2 seconds
while its conversation has loops and every 15 otherwise; a running request's inbox poll asks for
its own conversation, so a due loop becomes its follow-up without starting anything; pic loop
fire asks once; pic remote asks for every journal it serves every 5 seconds, so loops fire with
the terminal closed. With no pic running, nothing fires, and the list says "not firing" once a
wake is 90 seconds overdue.
After firing, a conversation with a fire pending and no lease held gets a pic run --session in a
session of its own (pic_peer_start in src/peer.c, registered by the composition root as the
wake starter), with every descriptor past stderr closed so it holds nothing of its starter's. One
start per conversation per 30 seconds per process, so a run that cannot take the fire up (its
model has no key) is not started on every look.
Missed and overlapping ticks
A loop is due when its due time has passed, or when its last fire is ahead of the clock (the clock stepped back: it fires once and is anchored anew). Missed runs fire once: the next due time is the first slot after now on the loop's own grid, and the fire says how many runs came due. While a fire waits (its inbox row pending, or delivered as a follow-up still queued) the loop does not fire again; it fires once when the waiting one has been taken up.
Bounds
A wake expires 7 days after it was made: the fire that finds it expired is its last and says so,
and it ends. Eight wakes a conversation that have not ended. The shortest interval is one minute; a
loop given none runs every 10 minutes. There is no dollar cap: spend is shown, summed from the
journal's usage rows of the requests a wake started (PIC_WAKE_OF_SQL), never a counter. Rounds
(50) and stalls (3) bound goals (step 4).
A wake's fires are exempt from grapevine.maxWakes, which bounds messages between conversations.
Stopping
Cancelling a request a wake started (Esc, Ctrl-C, an inbox abort from a phone or attach) pauses
that wake, in the cancel's own transaction (pic_wake_cancelled). /stop pauses every wake of the
conversation and opens the list. Paused is never deleted: a mistaken Esc costs one resume.
Resuming keeps the due time, so a loop paused past it fires at once, once.
The record
pic_wake(id, session_id, kind, prompt, every_ms, command, options, state, why, creator, fires,
due_ms, fired_ms, created_ms, expires_ms, updated_ms, spool, read_offset, pid, pid_started_ms,
exit_code, rounds, max_rounds, stalls, max_stalls), schema 21, with every column the three kinds
need so later steps take no schema step. state is active, paused, needs (a person has to
look) or ended; why says why for any but active. pic check adds: a wake's conversation
exists; its fire count equals its inbox rows, keyed wake:ID:1 to wake:ID:N; an active loop has
an interval and a due time; a wake not active has a reason. Fault point wake_fire: arrival 1 just
before the fire's commit, 2 just after. test/pic/wake.sh crashes at both and at inbox_wake,
and counts the provider's requests to show each fire ran once.
The phone
The protocol (amendment "Loops") carries get_wakes, pause_wake,
resume_wake, stop_wake and pause_wakes as pic's own commands to a conversation, answered by
pic remote from the journal; the list of conversations carries each one's loops; a loop that
comes to need the person is one notification, found by the change time of its row past the newest
pic remote had seen when it started, so a restart repeats nothing.
Not built here
pic send --wake, the cost estimate from the price (the confirming line says the context's tokens),
the cache-lifetime note on intervals, the notification's own Resume and Stop actions (a tap opens
the conversation, where they are), and the mock-up round for the list and the phone.