Routines
Prompts on a schedule, delivered while the browser is closed.
You decide on Friday that you want a short project update on Monday morning. You save the prompt in the chat where you’ve been working, pick the time, and close the tab. When you return, the prompt and its reply can be waiting in that same conversation.
In this chapter
- Set a one-off or repeating prompt and check when it comes due.
- Follow a saved alarm through to one accepted message.
- Read the schedule and the last run when a chat is busy, archived, or waiting for you.
A schedule belongs to a chat
- Browser
- Hono app.ts
- Core
- Pi Durable
- SQLite
Browser → Hono · Save routine
You save the schedule in a chat
The browser posts the name, prompt, schedule, and a new routine ID to the chat’s routines route. Here this is the chat’s first routine, a one-off.
Hono → Core · Validate and save
Core resolves a future time
The route validates the request fields. Core checks that the chat can be scheduled and resolves the local time in the routine’s time zone.
Core → SQLite · Save alarm
Pipal saves the definition and creates the alarm
Core commits the definition and the background task together. The
pipal.routinesdocument records the due time and the task ID forpipal.routine-alarm.Pi Durable → Pi Durable · Sleep
Pi Durable parks the alarm
The alarm waits for the due time or a signal that the schedule changed. You can close the browser now.
Pi Durable → Core · Time due
The clock wakes the alarm
The dispatch phase takes a turn in the admission queue.
Core → SQLite · Check alarm
Core checks that this alarm still owns the schedule
An edit may have superseded the task. The ID still matches here, so core checks which occurrence is due and runs the shared admission checks.
Core → SQLite · Commit once
The prompt and receipt are saved together
The same commit marks the one-off finished and retires the alarm. A remaining routine would get the next alarm.
Core → Pi Durable · Ready to run
Pi Durable can run the accepted prompt
The chat’s current settings apply, and the reply can start once admission commits. Core arms the run deadline after that commit.
Browser → Hono · Open chat
You return to the conversation
The scheduled prompt appears in the transcript with its routine name. For a completed run, View in chat in this chat’s routine list points to the saved reply.
Browser → Hono
You save the schedule in a chat
The browser posts the name, prompt, schedule, and a new routine ID to the chat’s routines route. Here this is the chat’s first routine, a one-off.
Open Chat options → Routines in an existing chat. A routineA named prompt Pipal sends to an existing chat at a time or on a repeating schedule you choose. is a named prompt that Pipal sends to that chat for you. Give it a name, write what you want the assistant to do, and choose a schedule and time zone. SidebarChatActions.svelte
The editor previews the next occurrence for a new or changed schedule, so you can catch a time zone or weekday you picked by mistake. RoutineEditor.svelte
The sidebar’s Routines button lists chats that have routines, including archived chats and routines with no future occurrence. App.svelte routines.ts
| Schedule | What you give Pipal | What it resolves |
|---|---|---|
One-off (once) |
A local date and time plus a time zone | One future instant, for example 9 AM next Monday in Kolkata |
Repeating (cron) |
Five calendar fields plus a time zone | Successive matching times, for example 9 AM on weekdays |
For a one-off, Pipal turns your local date and time into one instant. If the clocks go back and that time occurs twice, pick a time that occurs once. Pick a different time if the clocks go forward and skip it. For a repeating routine, a cron expressionFive fields giving the minutes, hours, days of the month, months, and days of the week when a repeating routine is due. tells Pipal which times count. 0 9 * * 1-5 means minute zero of hour nine, in every month, on weekdays. The time zone is part of the schedule, so viewing it from a different laptop leaves the calendar rule intact. routine-schedule.ts RoutineEditor.svelte
Save the routine and the host keeps it in durable.sqlite. When it comes due, Pipal sends the prompt using the chat’s settings at that time. You can change the model between saving the schedule and Monday morning, and the next scheduled prompt will use the new selection. The chat keeps its workspace locationThe place a chat uses for project tools, either the Pipal host, an account reached over SSH, or a Docker project. and project. When Pipal accepts the prompt, it checks which workspace tools that chat can use now. The core runtime explains the admission checks shared with an ordinary Send. routines.ts index.ts
Deep diveThe editor and the server agree on the next occurrence
RoutineEditor.svelte turns the frequency presets into either once or cron. POST /api/v1/routines/preview runs nextOccurrence() against the server’s time and returns nextRunAt in Unix milliseconds. POST /api/v1/conversations/:id/routines validates and saves the routine. The same collection supports GET, with PUT to replace a definition, PATCH to pause or resume, and DELETE to remove a routine at /:routine. GET /api/v1/routines returns groups by chat. RoutineEditor.svelte app.ts routine-schedule.ts
Pipal accepts cron expressions of up to 128 characters with five numeric fields, including wildcards, lists, ranges and steps. Rejecting random placeholders means recomputing a schedule starts from the same rule after a restart. Core permits up to 50 routines per chat, counting paused and finished ones. The create route accepts a caller-supplied UUID, so repeating the same create request with that ID and definition returns the existing routine. routine-schedule.ts routines.ts app.ts
One alarm covers the chat’s next occurrence
Pipal keeps the list in the chat’s pipal.routines document. The earliest active time determines its current pipal.routine-alarm task. If you change which time comes first, Pipal can replace the alarm. The old one gets a signal and checks whether it still owns dispatch when it wakes. What Pi gives you explains how a saved task goes to sleep and wakes. routines.ts
If the Pipal host was off on Monday morning, start it again and an active one-off can still come due. When a repeating routine can finally send, it uses the most recent due occurrence and schedules the next future time. If the chat can accept a run when you restart on Wednesday afternoon after missing three weekday runs, expect Wednesday’s prompt. The next occurrence is Thursday at the usual time. routine-schedule.ts runtime.test.ts
If the chat is already answering you, the alarm waits on that run’s task when it has the task ID. If other chats fill the host’s run limit, the alarm tries again after five seconds. routines.ts
A run waiting for a workspace approval keeps its own chat busy and frees a slot for other chats. If Last run shows Waiting for approval, choose Open chat to make the decision. Approvals and YOLO follows the approval itself. index.ts Routines.svelte
Deep diveThe dispatch commit ties the receipt to the next alarm
pipal.routines-changed wakes a superseded alarm sleeping in its wait phase. In the dispatch phase, core takes the admission queue and prepares the admission callback, then checks the task ID against the document inside the commit. If the chat has a live run, the alarm waits on that run’s task ID. Capacity exhaustion, or a busy rejection without a task ID, uses the five-second retry. routines.ts
prepareRoutineAdmission() supplies the routine’s prompt to the shared admission code, using a request ID built from the routine ID and occurrence time. pipal.routine-message records the original name and time for the transcript. The old alarm’s retirement, the receipt reference in lastRun, any next alarm, and the run deadline commit with the accepted input. Core arms the deadline after the commit. An approval wait defers deadline aborts. When Pipal accepts a decision, it adds the time spent waiting back to the budget before checking the deadline again. If the process dies before commit, dispatch can be attempted again. After commit, the saved submission is the work to recover. The crash test kills the host during a routine reply and requires a single dispatch. index.ts routines.ts recovery.test.ts
// Admission, its receipt, the next alarm and retiring this task are a single Pi commit. delete state.alarm; await rearm(tx, conv.id, state); return done; }, ctx);If admission returns a runtime error other than a busy rejection, the alarm records the error and advances. If calendar calculation throws, the failed commit rolls back. A second commit records the failure and looks for a future occurrence. A repeating routine pauses if the next time cannot be calculated, leaving the chat’s other routines eligible for an alarm. routines.ts runtime.test.ts
The schedule and the reply have their own states
| State | What happens next |
|---|---|
| Active | An occurrence is scheduled, or due and waiting to start. |
| Paused | Future dispatch is on hold. Resume calculates a future occurrence. A one-off whose time passed needs a new time first. |
| Finished | The one-off was dispatched or hit a dispatch error that the alarm won’t retry. Change its schedule to give it a new time. |
Use Stop in the chat to stop a reply that already started. Use Pause or delete the routine to change what happens next time. The past prompt stays in the conversation under the name it had at dispatch. index.ts view.ts
In the chat’s routine list, View in chat leads to the saved answer when there is one, or falls back to the saved prompt. A run shown as Waiting for approval has the Open chat button instead. routines.ts Routines.svelte
Deep diveThe receipt tracks the run, and the revision protects the editor
The stored last run points at the submission receipt. A completed receipt yields completed, while a pending one can read running or waiting. To choose the label, Pipal checks live approval state in the chat’s own tools and delegated calls. A separate development worker’s approval can hold up a run that waits for it, while the routine list still reads running. If Stop or disabling the chat’s location aborts the routine’s run, the receipt reads stopped. If core marked the run as having exceeded its deadline, an aborted receipt reads failed with the time limit reason. routines.ts index.ts
Pipal starts the revision at 1 and increases it for definition changes, pause, resume, and when dispatch finishes a one-off or pauses a faulty schedule. A repeating dispatch can advance the next time and last run without incrementing it. The browser sends the loaded revision with PUT. A stale value returns HTTP 409 with routine_revision_conflict, so the editor keeps the input and offers Reload routine. routines.ts Routines.svelte
The prompt lands in a conversation that may already be months old. How do you find it later, and how much of that history still reaches the model? Search, context and cost follows those two views of a saved chat.
Recap
- A routine belongs to an existing chat and sends its prompt with that chat’s current settings.
- One current alarm per chat covers the earliest occurrence, and dispatch saves the receipt and the next alarm as part of the same commit.
- Schedule state tells you whether another prompt is due. Last run tells you what happened after the previous occurrence.
- Pipal was off for three weekday occurrences. The chat can take a run when the host restarts. What does the active repeating routine send?
- One prompt for each missed day
- One prompt for the most recent due occurrence
- Only a prompt at the next future time
Answer and explanation
One prompt for the most recent due occurrence
Pipal coalesces missed times into the latest due occurrence and calculates the next future one.
- A one-off says Finished and Last run says Waiting for approval. Where do you go to unblock it?
- Resume the routine
- Edit the occurrence time
- Open the chat and decide the approval
Answer and explanation
Open the chat and decide the approval
Finished describes the schedule. The placed prompt already has a run, and that run is waiting for a decision in the chat.
- The host dies just after committing dispatch, before the assistant answers. What lets it continue without sending the occurrence twice?
- The committed input, receipt reference, and alarm update
- The prompt in the browser’s draft
- The most recent calendar preview
Answer and explanation
The committed input, receipt reference, and alarm update
The work to answer the prompt was committed with the schedule update. Recovery has a saved submission to work on.
Pipal was off for three weekday occurrences. The chat can take a run when the host restarts. What does the active repeating routine send?
A one-off says Finished and Last run says Waiting for approval. Where do you go to unblock it?
The host dies just after committing dispatch, before the assistant answers. What lets it continue without sending the occurrence twice?