Apps
Deftmon
# Deftmon
`deft/deftmon` is a long-running background daemon hosted by the `deft`
binary. It provides lifecycle, SQLite state, a heartbeat, a status/control
surface, a durable scheduler, child-runtime agents, workspace supervision
and managed cron.
Hydra is used only to launch window-based workspaces. It is not the daemon
host.
## Running
```sh
deft mon
DEFTMON_DB=/tmp/deftmon.db deft mon
DEFTMON_PORT=4712 deft mon # control server
````
Equivalent explicit entry:
```sh
deft run packages/deftmon/src/deftmon.dft
````
`deft --debug-port 4712 mon` also exposes a control server. SIGTERM/SIGINT
stop the service cleanly.
The database path is `$DEFTMON_DB`, else config `deftmon/db-path`, else
`[app/data-dir "deftmon"]/deftmon.db`.
## Status
The daemon publishes on `deftmon/status`:
%{ :app "deftmon" :version "0.1.0"
:pid 1234
:started-at 1762339200
:now 1762339260
:uptime 60
:tick 3
:db %{ :ready true :path "/home/me/.deft/deftmon/deftmon.db" }
:scheduler %{ :started true :jobs 2 :enabled 2 :running 0 :next-at 1762340400 }
:agents %{ :started true :agents 1 :running 1 :busy 0 :error 0 }
:workspaces %{ :started true :total 1 :running 1 :error 0 } }
Subscribe from any runtime in the daemon process:
def on-deftmon {|ev| echo "deftmon tick" [get $ev "data"]~tick } [pub/subscribe "deftmon/status" "on-deftmon"]
## Control
The daemon methods live on the daemon runtime. `deftmonctl` evaluates them
directly over the debug port:
```sh
deft --prelude .deft/prelude.dft run packages/deftmon/tools/deftmonctl.dft \
4712 status
deft --prelude .deft/prelude.dft run packages/deftmon/tools/deftmonctl.dft \
4712 list-jobs
deft --prelude .deft/prelude.dft run packages/deftmon/tools/deftmonctl.dft \
4712 add-job '%{ :id "tick" :schedule %{ :every 60 }
:action %{ :kind :shell :command "date" } }'
````
Trailing arguments are inserted as Deft source. `DEFTMON_PORT` supplies
the default port.
Methods include:
| Method | Result |
|---|---|
| `status` | status snapshot |
| `ping` | `"pong"` |
| `tick-now` | force one heartbeat |
| `add-job`, `list-jobs`, `run-job`, `remove-job`, `set-job-enabled`, `job-runs` | scheduler |
| `start-agent`, `stop-agent`, `task-agent`, `cancel-agent`, `list-agents` | agents |
| `open-workspace`, `close-workspace`, `list-workspaces` | workspaces |
| `prune-runs`, `cron-export`, `cron-write` | maintenance |
| `shutdown` | stop the daemon |
## TUI client
`deft/dmon` is a panel app for a running daemon:
```sh
deft mon # control server defaults to 4712
retro app deft/dmon # GL host
retra app deft/dmon # ANSI host
````
It reads the control port from config `deftmon/control-port`, else
`$DEFTMON_PORT`, else 4712. The old `deft/deftmon/client.dft` path is a
compatibility symlink. Keys:
| Key | Action |
|---|---|
| `1` / `2` / `3` | jobs / agents / workspaces |
| `a` | add a scheduled shell job (dialogs) |
| `enter` | run selected job / task selected agent |
| `d` | enable/disable selected job |
| `x` | remove selected job / close selected workspace |
| `t` / `s` | task / stop selected agent |
| `c` | change control port |
| `r` | refresh |
| up/down, j/k | move selection |
## Scheduler
Job spec:
%{ :id "backup"
:schedule "0 3 * * *" # or %{ :every 300 } / %{ :at epoch }
:action %{ :kind :shell :command "backup.sh" }
:enabled true
:overlap false
:catch-up false }
Schedules:
- 5-field cron: minute hour day-of-month month day-of-week
- fields support `*`, `*/n`, `a-b`, `a-b/n` and comma lists
- `%{ :every seconds }` for fixed intervals
- `%{ :at epoch }` for a one-shot run
Actions:
- `%{ :kind :shell :command "..." }` runs through `sh -c`
- `%{ :kind :publish :topic "..." :payload ... }` publishes to pub/sub
- `%{ :kind :agent :agent "echo" :task ... }` dispatches an agent task
- `%{ :kind :workspace :workspace "name" }` opens a known workspace (or
pass a full `:spec` map)
A due job advances its next run before the action starts. With `:overlap`
false` a run is skipped while the previous one is active; with `:catch-up
false` a run missed by more than 60 seconds is skipped instead of replayed.`
## Agents
Agent spec:
%{ :id "echo"
:source "deft/deftmon/agents/echo.dft"
:runtime "agent-echo" # optional
:restart :on-failure # :never | :on-failure | :always
:autostart true
:args %{ } }
The agent file exposes the RPC protocol `agent-status`, `agent-task`,
`agent-cancel` and `agent-stop`. `deft/deftmon/agents/echo.dft` is the
reference implementation.
`task-agent` is asynchronous: it marks the agent busy and the result
arrives on `deftmon/event` as
`%{ :kind :agent-task :agent "echo" :ok true :result ... }`. Only one task
per agent runs at a time. `:autostart` agents start with the service;
`:on-failure`/`:always` retries startup failures up to 3 times.
Agents are child runtimes of deftmon: own VM/heap/scheduler/thread, shared
pub/sub and RPC registry. A standalone daemon cannot `rpc` into UI apps in
another process; UI-driving agents need a UI-side worker or a bridge.
## Workspaces
A workspace is one named hydra instance:
%{ :name "research"
:cwd "/home/me/project"
:workspace "research" # .deft/workspaces/research.dft
:instance "ws-research" # optional; default ws-<slug>
:prelude ".deft/prelude.dft" # optional
:autostart true }
`open-workspace` starts the detached hydra client and waits (asynchronously)
for `hydra --ping` to succeed. `close-workspace` probes first and only then
sends `--kill`, so it never spawns an instance just to close it.
`deftmon/event` reports `workspace-opening`, `workspace-opened`,
`workspace-closed` and `workspace-error`.
`hydra --ping` never spawns an instance. The supervisor is
compositor-agnostic: it only starts named hydra instances, leaving window
placement to the compositor.
## One-shot runner and cron
Run a persisted `:shell` job without the service:
```sh
deft run packages/deftmon/src/job-runner.dft <deftmon.db> <job-id>
````
It records a normal run and updates last-run/status, but does not touch the
job's `running` counter. Use it for jobs the live scheduler is not also
managing.
Jobs with `:export-cron true` and a 5-field cron schedule render into a
managed block:
```sh
deft run packages/deftmon/src/cron-export.dft <deftmon.db>
````
The block invokes `job-runner.dft`; command text stays out of the crontab.
Config keys:
- `deftmon/deft-path` — `deft` executable used by cron (default `deft`)
- `deftmon/job-runner-path` — path to `job-runner.dft`
- `deftmon/prelude-path` — optional `--prelude` path
If deftmon is also running, do not export a job the live scheduler owns or
it may execute twice.
## Maintenance
The scheduler keeps the newest 200 runs per job and prunes at startup and
every 50 completed runs. The service calls `gc/collect` every 60 seconds.
`prune-runs` is the manual hook.
## Scope
The daemon deliberately excludes desktop integration: no Hyprland, no
bar/tray/launcher/SNI, no distro session wiring. Workspace supervision
uses named hydra instances and is compositor-agnostic.