# Scheduling

This guide covers running akm tasks (`akm improve` and other background work)
through the OS scheduler (cron / launchd / schtasks): how `akm setup` reviews
task definitions before touching the scheduler, how to verify the result, and
how to migrate or repair scheduler bindings after moving or reinstalling akm.

## Review tasks during setup

Interactive setup reviews every embedded task definition — both the
general-purpose core task-template set and the maintainer-oriented
multi-cadence improve task set — in one pass. See the
[task CLI reference](../reference/cli.md#task) for the full template list.

```sh
akm setup
```

Before any OS scheduler change, setup shows every reviewed task's schedule
and enabled state and asks one activation question. Confirming runs the
scheduler sync; declining leaves both task files and scheduler state
unchanged.

## Verify

```sh
akm doctor
```

`akm doctor` reports health and, in its `scheduler` field, the scheduler backend, paths, task state, and
warnings — run it after any setup pass, and again after a `--rebind` (below),
to confirm the scheduler state matches what you expect.

## Non-interactive setup skips schedules

`akm setup --yes`, config-file setup, and CI runs skip the task step
entirely: neither task definitions nor OS scheduler entries are created or
changed. Run interactive `akm setup` to review, prepare, and activate tasks.

## Task definitions vs. scheduler state

Task definitions live under `<bundle>/tasks/` as task source v4 `.yml` sources
(or the Experimental task source v5, which 0.11 makes the only format);
the [task source reference](../reference/tasks.md) defines their executable and
scheduling grammar. A definition cannot enable itself: the refs this host schedules live
separately in `config.json` under `scheduler.enabled`, a list of
fully-qualified refs. Use `akm task enable <bundle>//tasks/<id>` or `akm task
disable <ref>` to change it without editing bundle content. An unscoped `akm doctor scheduler sync` installs exactly the listed refs from every enabled configured
bundle and removes akm-written bindings for refs that are gone or no longer
listed. Removing a bundle drops its refs from the list; disabling a bundle
leaves them inert.

Native scheduler entries are separate OS state. Activation captures the
installed akm runtime so scheduled execution does not silently switch to a
different checkout or package. Editing definitions and running ordinary `akm doctor scheduler sync` preserves that captured runtime.

A scheduled fire reads the task's current source and freezes a fresh plan for
that run; the result of `akm doctor scheduler sync` or validation is not reused as the run
plan.

## A scheduled `akm improve` covers one bundle

An `akm improve` run improves one bundle, the one it writes to: `--bundle`,
else `defaultWriteTarget`, else the working bundle. A scheduled row sets
`AKM_BUNDLE_DIR` to the working bundle of the shell that synced it, so a
scheduled `akm improve` covers that bundle (or `defaultWriteTarget`, when set)
and leaves your other bundles alone, even the writable ones. To improve
another bundle on a schedule, give it a task of its own; the `--bundle` inside
the command is `akm improve`'s own:

```yaml
# improve-team.yml
version: 4
run: akm improve --bundle team
schedule: "15 3 * * *"
```

```sh
akm import ./improve-team.yml --type task
akm task enable <bundle>//tasks/improve-team
```

Leave `--skip-if-locked` off a once-a-night run: if a short-cadence improve task
holds the lock when it fires, the run fails with `IMPROVE_LOCK_HELD` (exit 75) and
is recorded, rather than being silently skipped. Put `--skip-if-locked` on the
frequent tasks, and give them a `--timeout` that ends before the nightly starts.
The shipped `improve/akm-improve-consolidate` task does both: it runs at `:20`
every four hours with a one-hour `--timeout`, so the 00:20 run is over by 01:20,
well before the 02:00 nightly.

## Rerunning setup preserves scheduler bindings

Rerunning `akm setup` preserves existing scheduler bindings by design — it
will not silently rebind entries that are already activated.

## Upgrades

A config written before 0.9.17 has no `scheduler.enabled` list. The first
`akm doctor scheduler sync` (or `setup`, `task enable`, `task disable`) after
the upgrade takes the akm-written rows already installed in the native
scheduler as this host's choice, writes the list, and says so — a scheduled
task survives an upgrade by any install method with no manual step. An
explicit empty list is a choice: sync then removes every akm-written row.
`akm migrate status` reports anything else that remains pending, and why.

## Dispatch mode (Experimental)

From 0.10.261135 a host can replace its per-task rows with one row that runs
`akm task dispatch` every minute:

```sh
akm doctor scheduler sync --dispatch --dry-run   # what would be removed and written
akm doctor scheduler sync --dispatch
```

Dispatch reads `scheduler.enabled` and the task files each minute, so enabling
a task or editing its schedule applies on the next minute without a sync. Each
run is started with the same command and log file its row had, and appears in
`akm doctor log --task <id>` as before. `akm doctor scheduler sync --no-dispatch`
switches back. Per-task rows are Deprecated: in 0.11 the dispatch row is the
only entry. See [`akm task dispatch`](../reference/cli.md#task-dispatch).

## Migrating or repairing scheduler bindings (`--rebind`)

If akm was moved, reinstalled under a different package prefix, or repaired
after an installation problem, migrate scheduler entries deliberately:

```sh
akm doctor scheduler sync --rebind
akm doctor
```

Use `--rebind` only for that explicit runtime migration or repair — it
captures the current installed runtime, replacing whatever runtime a prior
activation had captured.

If you change the AKM storage path during reconfiguration, or move or install
akm at a new runtime path, follow setup with `akm doctor scheduler sync --rebind`; setup
never silently rebinds existing entries on your behalf.

## See also

- [Getting Started](getting-started.md) — the first-run path
- [CLI Reference: task](../reference/cli.md#task) — the full `task` command
  group; `enable` and `disable` stay, and the deprecated `add`, `run`, `sync`,
  `doctor` and `history` name their replacements
- [Task source reference](../reference/tasks.md) — exact file grammar,
  targets, triggers, and migration
