Skip to main content

Module loops

Module loops 

Source
Expand description

#173: /loop — one prompt, re-entered into this session on an interval.

Not cron, and the difference is the whole point. A cron job is its own session on its own timer, owned by the systemd unit, and it survives this CLI exiting. A loop is a prompt sent back into the conversation that is already open, so every tick carries the history the last one produced — the session id never changes, and history is free.

Who owns the timer is therefore a fact about the process, not about the feature: whichever front is running the session drives it. In the full-screen interface and the dashboard the timer lives in that process’s event loop or in the gateway, and it keeps firing while nobody is typing. In the plain REPL the line editor owns the terminal, so a tick lands between turns and the readout says so. A loop that stops because its process exited is not a bug; it is the shape of the thing, and /help says it in one line.

Nothing here runs an agent and nothing here talks to a provider. parse turns a typed line into a request, State is the record of one session’s loop, and State::due decides with a clock the caller supplies — so the interesting parts are testable without a model, a terminal or a sleep. The caller runs, and reports back through Outcome.

The record lives in <root>/state/loops/<session>.json, one file per session: the CLI and the dashboard read the same bytes, and a second process cannot keep a private copy of what the first one is doing.

Structs§

Spec
What a loop was told to do: the prompt, how often, and its two bounds.
State
One session’s loop, as it is written to disk.

Enums§

Ended
Why a loop is over.
Outcome
What a tick turned out to be. The caller decides — this module only records it.
Owner
Which process is driving the timer. A loop is not resumable across processes by default: the front that started it owns it, and the record says whose it is so a reader can tell a running loop from a stale one left behind by a CLI that exited.
Request
What one typed /loop line asked for.
Status
Where a loop is. Ended keeps its reason rather than forgetting it: “it stopped” is never the answer someone wants, and a loop that ran out of --times is a different thing from one that was told to stop.
Verdict
What a --until answer means. Three answers, not two: an unreadable reply is not a “no”, and treating it as one would end a loop because a model was verbose.

Constants§

MAX_TIMES
A ceiling on --times, because a bound that large is a way of writing “forever” with a number that looks deliberate. An unbounded loop is spelled by saying nothing.

Functions§

all
Every loop this install has a record of, running or not — newest first.
clear
Forget a session’s loop.
dir
Where a loop’s record lives: <root>/state/loops.
duration
A duration as a person reads a countdown: 45s, 2m 10s, 1h 5m.
interval
An interval: cron’s grammar, or a bare one. 2m is every 2m; every 2m is unchanged.
load
Read a session’s loop, if it has one.
manage
Answer one /loop … line, managing a loop that already exists.
parse
Parse everything after the /loop word.
save
Write a session’s loop. The write is atomic, so a reader never sees half a record.
state_path
The record for one session: <root>/state/loops/<session>.json.
until_prompt
The exact request a --until check makes. One question, one word back.
verdict
Read one yes/no answer strictly.