# Timer

Reads or changes a workspace timer session.

- Tag: `timer`
- Input socket: `input`
- Output socket: `output`
- Operation fields render against canonical `input`, for example `{{ input.session_id }}`.
- The id fields — `session_id`, the selector's `timer`, and start's `timer` — take an id or a
  template that renders to one. Blank, `null` or a template always saves; the node checks the id
  when it runs.
- Every duration is a whole number of seconds.

## Timers and sessions

A **timer** is a named length the workspace keeps: `Email`, 1800 seconds. A **session** is one run of
one, live or finished. Both are rows in ordinary workspace tables — `Timers` and `Timer Sessions` —
so the `table` node reads and charts them like any other data; this node exists for the things a
table cannot do, which is starting a session and moving it through its lifecycle.

A session copies its timer's name into `label` and its length into `length_seconds` when it starts.
Renaming or deleting the timer afterwards changes nothing already recorded.

There are two kinds, `timer` and `pomodoro`, and one difference between them:

- a **pomodoro** session ends when it reaches zero. With auto-advance configured, a session in the
  opposite mode starts in its place.
- a plain **timer** keeps counting. `state` stays `running`, `zero_reached_at` is set, and
  `remaining_seconds` goes negative and keeps falling until something stops the session. Nothing
  auto-stops it, and reaching zero alerts once and only once.

One session per timer, and one pomodoro at a time. Starting `Email` while `Email` runs is refused;
starting `Email` and `Deep work` together is fine.

## Operations

### `list_timers`

No fields. Output is the workspace's timers, ordered by name, each `{"id", "name", "length_seconds"}`.

### `get_active`

- `kind`: `timer` or `pomodoro`.
- `timer`: which timer, for `kind: timer`. Empty, it matches the active session of any timer.

Output is the session, or `null` when none is active.

### `start`

One field, `timer`, tagged on `kind` so a kind can only carry its own fields:

- `{"kind": "timer", "timer": "<timer id>"}` — starts one of the workspace's timers.
- `{"kind": "pomodoro", "mode": "focus", "length": 1500}` — `mode` (`focus` or `break`) and `length`
  are both optional, falling back to the plugin's pomodoro settings and to `focus`.

Output is the new session. A blank `timer` for `kind: timer` fails the run with `timer is required`.
A timer that is already running is refused with `timer_session_active`.

### `pause`, `resume`, `finish`, `cancel`

Each takes `session_id`. **Leave `session_id` empty and the node acts on the active session of the
stated `kind`** — and of the stated `timer`, for a plain one — so a workflow reacting to a timer
event needs no id plumbing. `finish` additionally takes `note`, which is stored on the session.

Output is the changed session.

`pause` folds the stretch counted so far into `elapsed_seconds` and stops the clock; `resume` starts
a new stretch. `finish` and `cancel` both end the session and keep the row — `state: "finished"` and
`state: "cancelled"` respectively — set `ended_at`, and record `overtime_seconds`: the time counted
past zero, or `0` when the session was stopped before reaching it.

## Session output

A session is:

- `id`, `kind` (`timer` | `pomodoro`), `state` (`running` | `paused` | `finished` | `cancelled`)
- `label` and `length_seconds`, copied from the timer at start
- `started_at`, `ended_at` (`null` while the session is still active), `resumed_at` (the start of the
  current running stretch, `null` while paused)
- `elapsed_seconds`, counted in stretches before the current one
- `zero_reached_at`, `overtime_seconds`, `note`
- `details`, holding what only that kind has: `{"kind": "timer", "timer": "<timer id>"}` or
  `{"kind": "pomodoro", "mode": "focus"}`

## Example

```json
{
  "id": "start-deep-work",
  "node": {
    "timer": {
      "operation": {
        "op": "start",
        "timer": { "kind": "timer", "timer": "{{ input.timer_id }}" }
      }
    }
  },
  "position": { "x": 120, "y": 80 }
}
```

## Failure behavior

- Build and execution fail with `Timers plugin not installed` when the workspace has not installed
  Timers.
- An id field holding something that is not an id is reported on the saved workflow as
  `expected ULID session id` or `expected ULID timer id`; a template that renders to one fails the
  run with an error naming the field, such as `session_id is not usable`.
- `timer_session_active` when the timer already has a session running, `timer_not_found` and
  `timer_session_not_found` for an unknown id, and `timer_running` when a timer with a live session
  is deleted.
- `timer_session_not_running`, `timer_session_not_paused`, and `timer_session_not_active` when the
  operation does not apply to the session's current state.
- `timer_session_changed` when the session moved on while the operation was being applied.
