# A session over time

> How a session lives past one exchange. What it waits on, what wakes it, what runs beside it, and what each of those leaves in the log when the process that started it exits.

A session is a log, not a process. Between one exchange and the next nothing
has to be running, and the next process that opens the log picks up where
the last left off. [The loop](/concepts/loop/) describes one run, and
[durability](/concepts/durability/) says what survives a crash. This page
follows a session through the time between runs: what it waits on, what
wakes it, what runs beside it, and what is left of each when a process
exits.

The diagram plays one session across an hour, in five scenes. Read it by
lane. The **session** is running, idle or suspended. The **process** is a
zelk process that has the session open, or a hatched gap where none does.
The **wakes** are things that want into the session, and the **work** is
background commands and watches running beside it. The line under the lanes
is what the log holds at that moment.

One session across an hour, with the processes that open it and the wakes that reach it. Times are minutes past the hour:

- **A cron, after a restart**. A time that came due while nothing ran still fires. The cron is in the log, with the last time it admitted, so the next process to open the session can work out which times it missed. It admits them as one prompt that carries the count, not one run for each.
   - 0 min · a zelk process opens the session
   - 1 min · a message starts a run
   - 2 min · the model schedules cron c, */15 * * * *; next times 15, 30, 45
   - 4 min · the run ends and the session is idle
   - 10 min · the process exits
   - 15 min · cron c comes due, and waits: no process has the session open
   - 30 min · cron c comes due, and waits: no process has the session open
   - 33 min · a zelk process opens the session
   - 33 min · the session is idle and a process has it, so it admits 2 times of cron c (15, 30) as one prompt
   - 33 min · the admitted wake starts a run
   - 35 min · the run ends and the session is idle
   - 45 min · cron c comes due
   - 45 min · the session is idle and a process has it, so it admits cron c’s time
   - 45 min · the admitted wake starts a run
   - 47 min · the run ends and the session is idle
- **A watch, on output**. A watch wakes an idle session, and joins a run that is under way. Each line the command prints is recorded in the session’s inbox and admitted under the lease. An idle session starts a run with it; a session mid-run takes it into its next model call. The command’s exit wakes it too.
   - 0 min · a zelk process opens the session
   - 1 min · a message starts a run
   - 2 min · the model starts watch w: the lines its command prints will wake the session
   - 4 min · the run ends and the session is idle
   - 10 min · watch w prints a line
   - 10 min · the session is idle and a process has it, so it admits a notice
   - 10 min · the admitted wake starts a run
   - 11 min · watch w prints a line
   - 11 min · the run under way takes it into its next model call
   - 12 min · the run ends and the session is idle
   - 25 min · watch w prints a line
   - 25 min · the session is idle and a process has it, so it admits a notice
   - 25 min · the admitted wake starts a run
   - 27 min · the run ends and the session is idle
   - 40 min · watch w’s command exits
   - 40 min · the end of w is a notice for the agent
   - 40 min · the session is idle and a process has it, so it admits a notice
   - 40 min · the admitted wake starts a run
   - 42 min · the run ends and the session is idle
- **Background work, past a run**. Only work kept for the conversation outlives the run. A job started with the default lifetime is stopped when its run ends. One kept for the conversation goes on, and when it ends it wakes the session to collect the result.
   - 0 min · a zelk process opens the session
   - 1 min · a message starts a run
   - 2 min · the model starts background job a, kept for this run only, the default
   - 2 min · the model starts background job b, kept for the conversation
   - 7 min · job a was kept for the run only, so it is stopped as the run ends or suspends
   - 7 min · the run ends and the session is idle
   - 24 min · job b finishes
   - 24 min · the end of b is a notice for the agent
   - 24 min · the session is idle and a process has it, so it admits a notice
   - 24 min · the admitted wake starts a run
   - 26 min · the run ends and the session is idle
- **Waiting on a person**. A suspended session waits in the log, not in a process. It records what it waits for, gives up its lease and returns. A wake that comes due meanwhile admits nothing. A later process opens it with the answer, the approved call runs once, and the wake that waited fires when the run ends.
   - 0 min · a zelk process opens the session
   - 1 min · a message starts a run
   - 2 min · the model schedules cron c, */20 * * * *; next times 20, 40
   - 4 min · the model asks a person: the session records the request and suspends, and releases its lease once its run-only jobs are stopped
   - 20 min · cron c comes due, and waits: a suspended session admits nothing
   - 25 min · the process exits
   - 34 min · a zelk process opens the session and delivers the answer with its start
   - 34 min · the run resumes where it waited; the approved call runs once
   - 36 min · the run ends and the session is idle
   - 36 min · the session is idle and a process has it, so it admits cron c’s time
   - 36 min · the admitted wake starts a run
   - 38 min · the run ends and the session is idle
   - 40 min · cron c comes due
   - 40 min · the session is idle and a process has it, so it admits cron c’s time
   - 40 min · the admitted wake starts a run
   - 42 min · the run ends and the session is idle
- **The process exits**. What a process ran dies with it, and the log learns that when the next one opens. The watch and the conversation job couldn’t write their own ends. The job a supervisor owns finished unseen. The next process records all three and tells the agent in one run; nothing is started again by itself.
   - 0 min · a zelk process opens the session
   - 1 min · a message starts a run
   - 2 min · the model starts watch w: the lines its command prints will wake the session
   - 2 min · the model starts background job c, kept by a process supervisor
   - 2 min · the model starts background job d, kept for the conversation
   - 5 min · the run ends and the session is idle
   - 8 min · watch w prints a line
   - 8 min · the session is idle and a process has it, so it admits a notice
   - 8 min · the admitted wake starts a run
   - 10 min · the run ends and the session is idle
   - 20 min · the process is killed
   - 20 min · watch w dies with the process, and nothing records it
   - 20 min · job d dies with the process, and nothing records it
   - 38 min · job c finishes while no process runs; its supervisor keeps the result
   - 38 min · the end of c is a notice for the agent, and waits: no process has the session open
   - 48 min · a zelk process opens the session
   - 48 min · the log records that watch w ended with the old process
   - 48 min · the agent is owed a notice that watch w ended
   - 48 min · the log records that job c finished while no process ran
   - 48 min · the agent is told that job d died with the old process; the log records it lost when the agent next inspects or stops it
   - 48 min · the agent is owed a notice that job d was lost
   - 48 min · the session is idle and a process has it, so it admits 3 notices
   - 48 min · the admitted wake starts a run
   - 50 min · the run ends and the session is idle

## Idle and suspended

A session at rest is in one of two states.

- **Idle.** No run is under way and no effect is in flight. A process may
  have the session open, or none may. An idle session takes a message, which
  starts a run, and the wakes below, which start one by themselves.
- **Suspended.** A run is part way through and waits for something only a
  person can give: an answer to a question, an approval, or a hook that
  returned `defer`. The session records what it waits for, with the earliest
  deadline, gives up its lease and returns; `Wait` returns
  [`ErrSuspended`](/reference/go/zelk/#variables). The requests stay waiting,
  and no process waits for them.

A suspended session isn't idle and it isn't over. Its run is open, so a wake
that comes due meanwhile admits nothing. The third scene shows it: a cron
time arrives while a process is up, and has to wait.

Suspending also settles what the run left running. Background work kept for
the run is stopped, as it would be when the run ends, and work kept for the
conversation stays with the agent. (A host whose environment can find
processes again may keep the run's work running too; the local environment
the `zelk` command uses can't.)

Waiting on background work is not suspension. The model can wait inside a
run, since `ProcessOutput` takes up to 270 seconds of `wait_ms`, or it can
end the run, and the job's end wakes the session later.

Stopping a background process withdraws its unseen completion notices before
requesting the kill. If the backend cannot confirm the exit, the process
stays listed. A run left parked by withdrawn notices ends interrupted,
without a model call, before an idle stop returns; if another process holds
the session, the stop says the session is busy, and that process ends the
run.

## Who resumes a suspended session

The host does. zelk starts no process of its own, and a suspended session
holds no goroutine to wake. [`zelk.NextWake`](/reference/go/zelk/#nextwake)
returns the session's earliest deadline, and a host that wants one to fire
on time calls `Start` by then.

`Start` takes the answer with it. It records the answer first, then fires
any timer that came due, then settles what is open, so an answer given at
resume wins over a deadline that passed while nobody ran. The run goes on
with the approved call, which runs once. Any process that shares the store
can do this, not only the one that suspended, as the diagram's fourth scene
does. A wall-time limit doesn't count the time spent suspended, unless you
ask it to with `WithSuspendedTimeCounted`.

Whether a session suspends depends on the host:

- The terminal UI asks you at once, in the same process.
- Print mode and `zelk exec` refuse what needs an answer, or ask on the
  terminal with `-input prompt`.
- `zelk acp` suspends. An approval waits for the editor's answer, and when
  the editor disconnects it stays pending until a client takes the session
  up again.
- A Go host picks with [`WithInputSuspend`](/go/approvals/#suspend-and-answer-later),
  and `WithInputTimeout` expires a request nobody answers.

## Wakes

A **wake** is something other than you that puts work into a session: a cron
time, a line from a command the agent watches, a background job ending. The
model starts each of them itself, with `CronCreate`, `Watch` and a
background `Bash` or `Task`.

Each cron and each watch is a **source**, recorded in the log when it is
created and when it ends. What a source reports is an **occurrence**,
numbered and fixed when it is made, so a retry sends the same payload under
the same ID and a duplicate is dropped.

- **Cron** posts a prompt at the times a five-field expression names, in
  your local time zone. Its times come from the expression and the clock.
  The log holds the last time admitted, so any process can work out which
  are due.
- **Watch** runs a command in the background and wakes the session with the
  lines it prints. Lines are recorded in the session's inbox, a file outside
  the log, by the process that reads the command. Admission later moves them
  into the log. The inbox keeps 64 occurrences or 256 KiB per source, and
  past that folds them into the newest one, which counts them.

A wake is admitted by a process that has the session open, under its lease,
and in one step that also records how far the source has got. An idle
session starts a run with it. A session already running takes a line or a
notice into its next model call, but a cron time is admitted only when the
session is idle. A suspended session admits nothing; the occurrence waits.

### When nothing was there

Nothing runs while zelk is closed, and no wake runs in a session of its own.
A cron time that came due with no process, or while the session was busy or
suspended, isn't lost. It waits, and when a process has the session open and
idle, every time that waited is admitted as **one** occurrence. The prompt
carries the count of the extra times, so one run follows, not one per time.
That is the first scene. A recurring cron lasts until it is deleted, and a
session with a live cron isn't removed by the 30-day cleanup.

A watch is different, because its command is a child of the process that
started it. When that process exits, the command dies, and the process
couldn't write that into the log. The log still says the watch is live. The
next process to open the session records the end and tells the agent, which
decides whether to start it again; zelk never runs a command again blindly.

A host keeps wakes going with `Agent.DispatchWakes`. The terminal UI calls it
every five seconds, and `zelk acp` on its own tick. It finds sessions with
live sources through the catalog, leaves unfinished runs to whoever resumes
them, and leaves a session another process holds alone. `zelk -p` and
`zelk exec` end with their run, so a cron they create never fires.

## Background work

The agent starts a command in the background with Bash, or a subagent with
`Task`, and follows it with `ProcessOutput`, `ProcessInput`, `ProcessKill`,
`TaskOutput` and `TaskStop`. Each job has a **lifetime**:

| Lifetime | Lives until | If the process exits |
|---|---|---|
| `execution` (the default) | the run that started it ends or suspends | it is already stopped |
| `conversation` | it ends, or is stopped | it dies with its host process. The local backend can confirm the loss after a restart of its host or machine. If another host cannot observe the process, its exit stays pending |
| `persistent` | it ends, or is stopped | it goes on under a supervisor, `zelk processes serve`, and its output waits |

Every job tells the agent when it ends, unasked, so the model needn't poll.
For a job kept past the run, that notice is a wake: it starts a run if none
is under way, or joins the one that is. A job the log records as killed
wakes nothing, even after a restart, because there is nothing to collect. A
notice the session already took in before the stop is still delivered, and a
job a stop found already gone is recorded as lost and reported like any
other.

The wake is owed, and it is in the log. It records that a job was started
with its completion owed, so a process that opens the session after a
restart delivers the notice. The fifth scene shows three at once: a watch,
a conversation job and a supervisor's job, reported in a single run. A job a
supervisor kept needs `-process-socket`; see
[background work](/cli/background/#jobs-that-outlive-zelk).

Output is bounded. `ProcessOutput` returns at most 30 KiB a call, and a
supervisor keeps at most 128 jobs with 1 MiB of output each. After a restart,
a reattached job's output cursor starts fresh, so a little of what it holds
may be read again.

## Limits

| What | At most |
|---|---|
| Background processes in one session | 16 |
| Subagents running at once | 5 |
| Subagent results not yet collected | 128 |
| Pending crons in one session | 128 |
| Live sources (crons and watches) in one session | 128 |
| A cron prompt | 4,000 characters |
| How far off a cron's next time may be | nine years; an expression that names none sooner is refused |
| A watch's notice | one per `min_interval`, 10 s by default and 1 s at the least |
| A watch's notices in an hour | 60, then it is stopped and the agent is told to narrow its filter |
| One notice's text | 16 KiB, oldest lines giving way and counted; a line over 4 KiB is cut |

## Where to go deeper

- [Cron and Watch](/cli/cron-watch/) and [background work](/cli/background/)
  show how to use them in the `zelk` command.
- [Durability and recovery](/concepts/durability/) says how a crash is
  settled, and [The loop](/concepts/loop/) lists what a session accepts in
  each state.
- [Subagents](/concepts/subagents/) covers a child's own suspension, and
  [Hooks](/concepts/hooks/) the `defer` decision.
- [Approvals](/go/approvals/) shows how a Go host suspends and answers, and
  [Sessions and executions](/go/executions/) how it starts one.
- The [wakes contract](/internals/wakes/) and the
  [background contract](/internals/background/) give the exact rules.
