feat(runs): agent limits per machine — start queue, runs.limits provider, subagent hook events #47

Closed
gabrielvidal wants to merge 0 commits from agent-limits into main
pull from: agent-limits
Owner

Summary

After the EVOX2 ran out of memory under about 20 conversations (28 Nov), this adds what the agent-limits plugin needs from the hub: a per-machine maximum of agents at once. A plugin providing runs.limits sets the numbers; the hub queues the starts that would go over, and passes each machine's limit down to its runs so the plugin's hooks can cap native subagents too. With no such plugin installed nothing changes.

Key changes

  • backend/plugins/run_limits.py: the runs.limits provider contract ({op: limits} → {limits: {runner: n}}, cached 30 s, dropped on the provider's writes).
  • backend/runs/queue.py: the queue. One budget per machine (live runs + subagents in flight per their transcripts), first in first out, persisted in /data/run-queue.json, pumped on a run's exit, on a limits change and every 5 s.
  • backend/runs/service.py: the gate in _send_message for a new conversation and for a turn that restarts a session with no live process. The answer carries queued and no pid; meta.queued is stamped. agentLimit rides /spawn, /resume and /fork.
  • backend/runs/routes.py: GET /api/runs/queue, POST …/{sid}/start, DELETE …/{sid} (permission table and golden matrix updated).
  • sidecar/launch.py, routes_runs.py, fork.py: agentLimit → the run's env (AIA_AGENT_LIMIT, AIA_RUN_PIDS_DIR), and Claude Code's own per-session subagent and workflow caps lowered to fit.
  • backend/plugins/harness_hooks.py: SubagentStart / SubagentStop are allowed script hook events.
  • Frontend: the composer's sent card says "queued — EVOX2 is at its limit of 6 agents (2 in line)"; a "queued" badge on a conversation whose turn waits. OpenAPI, generated types and the UI key list regenerated.
  • docs/plugins.md: the contract, the routes, the new hook events.

Key decisions

  • The hub holds the queue, not the plugin. It keeps the send's own arguments and replays the whole send path, so scope, account, attachments and guest runs come through unchanged. A plugin replaying through runs.start would lose most of them.
  • The plugin only gives numbers. No plugin script runs per start; the hub reads a cached map. A failing provider means no limit, never a blocked send.
  • Subagents are counted from transcripts on the hub side (a few seconds late). The exact, immediate cap is the plugin's hook on the machine.
  • Not gated: a follow-up into a live run (no new process) and forks (they count once running).
  • An offline machine keeps its queue until it is back, instead of failing each start.
  • A queued new conversation has no state yet, only its stamp, so whoever waits on its id (a parent's report-back) keeps waiting.

Changelog

  • Added: a machine at its agent limit queues new conversations and starts them by itself when an agent finishes (needs the Agent limits plugin).
  • Added: the composer says when a send was queued and where it stands; a queued turn shows a badge on its conversation.

Test notes

  • backend/test_run_queue.py: 17 tests (provider, gate, FIFO, subagent budget, resume, start now, cancel, failed start, offline machine, guest filter, routes, sidecar env).
  • Every backend and sidecar test module, one process each: 231 pass, 5 fail, and the same 5 fail on main on the EVOX2 (test_typed_plugins, test_claude_proxy, test_guard, test_guest_accounts, test_host_only_routes).
  • tsc --noEmit clean. The full vitest suite was not run (memory on the EVOX2); only src/i18n.
  • A local hub with the plugin installed and two fake machines: limits set, queue held at 6 of 6, screenshots below.
  • Not deployed from the EVOX2. Merge with ai-agent-plugins agent-limits; deploy from the Homelab, then update the workers so their sidecars pass the limit to runs.

🤖 Generated with Claude Code

Screenshots

settings-mobile.png
queue-desktop.png

## Summary After the EVOX2 ran out of memory under about 20 conversations (28 Nov), this adds what the `agent-limits` plugin needs from the hub: a per-machine maximum of agents at once. A plugin providing `runs.limits` sets the numbers; the hub queues the starts that would go over, and passes each machine's limit down to its runs so the plugin's hooks can cap native subagents too. With no such plugin installed nothing changes. ## Key changes - `backend/plugins/run_limits.py`: the `runs.limits` provider contract (`{op: limits}` → `{limits: {runner: n}}`, cached 30 s, dropped on the provider's writes). - `backend/runs/queue.py`: the queue. One budget per machine (live runs + subagents in flight per their transcripts), first in first out, persisted in `/data/run-queue.json`, pumped on a run's exit, on a limits change and every 5 s. - `backend/runs/service.py`: the gate in `_send_message` for a new conversation and for a turn that restarts a session with no live process. The answer carries `queued` and no `pid`; `meta.queued` is stamped. `agentLimit` rides `/spawn`, `/resume` and `/fork`. - `backend/runs/routes.py`: `GET /api/runs/queue`, `POST …/{sid}/start`, `DELETE …/{sid}` (permission table and golden matrix updated). - `sidecar/launch.py`, `routes_runs.py`, `fork.py`: `agentLimit` → the run's env (`AIA_AGENT_LIMIT`, `AIA_RUN_PIDS_DIR`), and Claude Code's own per-session subagent and workflow caps lowered to fit. - `backend/plugins/harness_hooks.py`: `SubagentStart` / `SubagentStop` are allowed script hook events. - Frontend: the composer's sent card says "queued — EVOX2 is at its limit of 6 agents (2 in line)"; a "queued" badge on a conversation whose turn waits. OpenAPI, generated types and the UI key list regenerated. - `docs/plugins.md`: the contract, the routes, the new hook events. ## Key decisions - **The hub holds the queue, not the plugin.** It keeps the send's own arguments and replays the whole send path, so scope, account, attachments and guest runs come through unchanged. A plugin replaying through `runs.start` would lose most of them. - **The plugin only gives numbers.** No plugin script runs per start; the hub reads a cached map. A failing provider means no limit, never a blocked send. - **Subagents are counted from transcripts on the hub side** (a few seconds late). The exact, immediate cap is the plugin's hook on the machine. - **Not gated:** a follow-up into a live run (no new process) and forks (they count once running). - **An offline machine keeps its queue** until it is back, instead of failing each start. - **A queued new conversation has no state yet**, only its stamp, so whoever waits on its id (a parent's report-back) keeps waiting. ## Changelog - Added: a machine at its agent limit queues new conversations and starts them by itself when an agent finishes (needs the Agent limits plugin). - Added: the composer says when a send was queued and where it stands; a queued turn shows a badge on its conversation. ## Test notes - `backend/test_run_queue.py`: 17 tests (provider, gate, FIFO, subagent budget, resume, start now, cancel, failed start, offline machine, guest filter, routes, sidecar env). - Every backend and sidecar test module, one process each: 231 pass, 5 fail, and the same 5 fail on `main` on the EVOX2 (`test_typed_plugins`, `test_claude_proxy`, `test_guard`, `test_guest_accounts`, `test_host_only_routes`). - `tsc --noEmit` clean. The full vitest suite was not run (memory on the EVOX2); only `src/i18n`. - A local hub with the plugin installed and two fake machines: limits set, queue held at 6 of 6, screenshots below. - Not deployed from the EVOX2. Merge with ai-agent-plugins `agent-limits`; deploy from the Homelab, then update the workers so their sidecars pass the limit to runs. 🤖 Generated with [Claude Code](https://claude.com/claude-code) ## Screenshots ![settings-mobile.png](https://git.gabvdl.xyz/attachments/69a7e6bd-663d-41a6-8bf7-e6523d8b87af) ![queue-desktop.png](https://git.gabvdl.xyz/attachments/82b0df7f-3d6c-4a9e-8243-907981ab90ec)
gabrielvidal self-assigned this 2026-11-29 12:11:14 +01:00
gabrielvidal added 1 commit 2026-11-29 12:11:14 +01:00
A plugin that provides runs.limits sets how many agents each machine runs
at once. A launch over the limit waits in the hub's queue (runs/queue.py)
and starts when the machine is back under it; the limit rides every launch
to the sidecar, which hands it to the run for the plugin's hooks.
SubagentStart / SubagentStop join the script hook events.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Author
Owner
Merged into `main` as b464b054 by the auto-ship batch https://ai-agent.lab.gabvdl.xyz/conversation/01a2287c-a099-7541-b95d-b0234cea19cb.
gabrielvidal closed this pull request 2026-11-29 19:07:09 +01:00

Pull request closed

Please reopen this pull request to perform a merge.
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: gabrielvidal/ai-agent#47