# AGENTS.md Guidance for AI coding agents working in `zag`, the Zig port of the [barn](../barn) Java job daemon. See `README.md` for the user-facing description. ## Toolchain - Zig 0.16.0 (`zig build`, `zig build test`). The 0.16 std API differs from older releases (`std.Io`, `std.process.Init`); check the scaffold in `src/main.zig` first. - Library code lives in `src/` and is exposed through `src/root.zig`; `src/main.zig` is the CLI. ## Porting rules - Reference implementation: `../barn/src/main/java/com/samsonmedia/barn/`. - Implement the *documented* behaviour (README, `../barn/docs/`), not barn's bugs. - Keep CLI flags, JSON/XML output field order, config defaults and the on-disk job directory layout compatible with barn (`zag` replaces `barn` in names and paths). - Port specs from barn's unit tests alongside each module (`test` blocks). ## Layout | Path | Purpose | |------|---------| | `src/duration.zig` | `\d+[mhdwMHDW]` duration parser | | `src/timestamp.zig` | ISO-8601 instant formatting/parsing (barn-compatible) | | `src/state.zig` | Job states and the transition state machine | | `src/jobid.zig` | `job-` + 8 hex job ids | | `src/fsutil.zig` | Atomic writes, trimmed reads, advisory `FileLock` | | `src/jsonpretty.zig` | Streaming JSON writer matching Jackson `INDENT_OUTPUT` layout | | `src/toml.zig` | Parser for barn's TOML subset (`[section]`, `key = value`, arrays) | | `src/loadlevel.zig` | `high`/`medium`/`low` load levels | | `src/job.zig` | `Job`, `Manifest`, `ExitCode` types | | `src/dirs.zig` | On-disk layout under the base dir | | `src/statefiles.zig` | One-value-per-file job state (atomic) | | `src/repository.zig` | Job create/find/transition/retry persistence, `manifest.json` I/O | | `src/config.zig` | Config structs, defaults, TOML loading/merging, `ZAG_*` env overrides, validation | | `src/loadclass.zig` | `.load` whitelist parsing and load-level classifier | | `src/defaults.zig`, `src/defaults/` | Default `zag.conf` and `*.load` files embedded in the binary | | `src/retry.zig` | Retry decision and backoff/jitter delay | | `src/proc.zig` | Spawn with log redirection, process-group signals, liveness | | `src/runner.zig` | Runs one job: heartbeat, timeout, kill, completion, retry | | `src/recovery.zig` | Startup crash recovery of orphaned running jobs | | `src/scheduler.zig` | Per-load-level concurrency, thread per job, `killJob` | | `src/usage.zig` | `ps`/`nvidia-smi` sampling, `usage.csv`, summaries | | `src/cleanup.zig` | Automatic and manual (`zag clean`) cleanup planning | | `src/jobjson.zig` | `Job` JSON (shared by CLI `--output=json` and IPC) | | `src/ipc.zig` | NDJSON-over-Unix-socket server and client | | `src/service.zig` | Daemon core: recovery, scheduler, IPC handlers, cleanup timer, reload | | `src/daemon.zig` | pid file, foreground run loop + signals, detached start, stop (TERM/KILL) | | `src/output.zig` | JSON/XML rendering of a value tree, human helpers (bytes, ages, local time, colour) | ## Build notes - `build.zig` links libc (for `localtime_r`) and defaults Linux builds to `x86_64-linux-musl` so the binary is static and does not depend on the host's crt files (the host linker fails on newer gcc `.sframe` sections). - Width/fill on a signed integer prints a leading `+` (`{d:0>2}` of an `i64`); cast to unsigned first. | `src/args.zig` | Option parser (long/short, `=`/separate values, `--`, stop-at-positional) | | `src/query.zig` | Job listing filter shared by the daemon and offline CLI | | `src/logging.zig` | Timestamped stderr logging with a runtime level | | `src/cli.zig` | CLI dispatch, shared `Context`, help text | | `src/cli_jobs.zig` | `run`, `status`, `describe`, `usage`, `kill`, `clean` | | `src/cli_admin.zig` | `service ...`, `config ...`, `autocompletion` | | `src/main.zig` | Binary entry point | | `test/e2e.sh` | End-to-end smoke test (`zig build e2e`) |