✨ feat(dvara): a machine-wide turn queue for heavy work, record-only first

Merged
AT
1d ago
+3479-14

dvara turn makes heavy work take turns through one machine-wide queue, beside dvara slot (which still never waits). Record-only is the default, so nothing waits yet. Each request writes a line, and the 2026-12-10 reading replays those lines to set the count. This is the dvara half of Decision: C on https://github.com/athya-hq/athya_mono/issues/4012, recorded in athya_mono ADR-0079.

dvara turn [--exclusive] [--class push|other] [--label <text>] -- <command> [args…]
dvara turn-replay [--count N]… [--wait-minutes M] [--since <ISO time>] [--file <turns.jsonl>]
  • Queue (src/turn-queue.ts, pure): two turns by default, and an exclusive request takes them all. The push class is served first, then first come first served. Nothing overtakes the head of the line, so a queued exclusive request holds back newcomers and is never starved. A request that has waited the bound is granted over the count. replay puts the logged requests through the same rule at any count.
  • Effects (src/turn.ts): state lives in ~/.dvara/turns/<hostname>/ (DVARA_TURN_DIR replaces it), with one ticket per request. A ticket is judged by isGone, the pid and start-time rule that dvara slot's lock uses, so a dead holder or waiter needs no cleanup. Each decision runs under dvara slot's lock, named queue.
  • Waiting: a ⏳ line at the start and every minute, naming the holders. At 30 minutes the command runs anyway with a warning. The exit status is always the command's; SIGINT or SIGTERM while waiting exits 130 or 143, cleans up and still writes the line.
  • Settings: DVARA_TURN_MODE (record, enforce or off), DVARA_TURNS and DVARA_TURN_WAIT_MINUTES, each winning over settings.json in the queue folder. CI is always off. No date in code switches the mode.
  • Nesting: DVARA_TURN_HELD passes to the command's children. A nested request runs at once and is never upgraded, so a nested --exclusive under a shared turn cannot deadlock against itself.
  • Watchdog (RFC-0060 S7): a request blocked in enforce mode writes waiters/<pid> with its start identity. decide finds no internal-deadlock in a tree with a live entry; only the external ceiling applies.
  • Evidence: one JSON line per request in turns.jsonl. Inside a hook there is also a turn record, which the history row lists as turns (always present, empty when no stage asked). The row's schema stays 1. A request's stage is read when it asks. A free turn is granted at the asking. A request cancelled while waiting has startedAt: null, so a replay skips it. The README has the field lists.
  • Shared format: tickets carry "schema": 1 and are read field by field, so a later format that only adds fields stays readable by the older dvara another repository pins. Settings live once per machine, beside the queue.

A two-holder run (the cli.test.ts case runs three real dvara turn processes, enforce mode, two turns). The waiter's stderr:

⏳ Waiting for a heavy-work turn (2 of 2 in use, you are 1st in line); waited 0 min of up to 30.
   veda  first holder   for 0 min  (worktree confident-mccarthy-2f88e0)
   veda  second holder  for 0 min  (worktree confident-mccarthy-2f88e0)

and its request line:

{"schema":1,"mode":"enforce","turns":2,"waitMinutes":30,"class":"other","exclusive":false,"width":1,"nested":false,"outer":null,"label":"waiter","repo":"veda","worktree":"confident-mccarthy-2f88e0","branch":"claude/confident-mccarthy-2f88e0","hook":null,"stage":null,"askedAt":1795735642214,"grantedAt":1795735643450,"startedAt":1795735643452,"endedAt":1795735643511,"waitedMs":1238,"wouldWaitMs":1236,"ranAnyway":false,"ahead":0,"holders":[{"label":"first holder","class":"other","exclusive":false,"heldMs":2252},{"label":"second holder","class":"other","exclusive":false,"heldMs":1092}],"exitCode":3,"signal":null,"error":null}

(ids, pids and cwd dropped for width.)

Release: pariksha's releases took v0.8.16 and v0.8.17 while this ran, so dvara is released from main in its own chore(release) PR after this merges, at the next free version (checked against the latest tag right before tagging). Main holds dvara at 0.8.17 meanwhile, a version the version rule counts as released.

Tests:

  • turn-queue.test.ts and turn.test.ts. These use a real directory with a fake clock, process table, git and command. Every env is built explicitly, HOME points at scratch, and each async case is bounded at 10 s, so nothing touches the machine's queue.
  • records, supervisor, watchdog and cli cases, plus 923 dvara tests passing.

Mutation (local, dvara lane, turn.ts and turn-queue.ts):

  • First run: 837 of 903.
  • Second: 870 of 873. Its three survivors are in the off path, now tested.
  • Third, after the line fixes: 874 of 874, none surviving.

Guard files: none change. packages/dvara/README.md is not one; niyam's README.md entry is the root README only.

Slice: #651

🤖 Generated with Claude Code