Skip to content

feat: harden agent-loop stability with shared budget recovery, metrics, and hang tracing - #50

Merged
jasonli0226 merged 2 commits into
mainfrom
test/agent-loop-stability-races
Jul 3, 2026
Merged

feat: harden agent-loop stability with shared budget recovery, metrics, and hang tracing#50
jasonli0226 merged 2 commits into
mainfrom
test/agent-loop-stability-races

Conversation

@jasonli0226

@jasonli0226 jasonli0226 commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Hardens the agent loop against the failure modes that surface under concurrency and stress: swarm crash recovery, runaway token spend, silent hangs, and the lack of any scrape surface for the stability metrics we already emit. Two commits:

  1. Shared budget recovery + concurrency/backstop metrics (78a1517)
  2. /metrics scrape endpoint + per-run activity tracing (d9cc360)

1. Shared budget recovery

  • Recover a crashed swarm's pending runs into a single shared BudgetTracker seeded with the session's already-recorded token spend, so recovered siblings resume against the swarm's cumulative usage instead of each getting a fresh full allowance.
  • Adds sumSessionTokenUsage() to the agent-run repository and an initialUsed constructor arg to BudgetTracker.
  • Prometheus metrics for concurrency slots (active/pending/max) and the two force-stop backstops (watchdog, reaper), with alert rules under infra/observability/.

2. Metrics endpoint + hang tracing

  • Standalone Prometheus scrape server — opt-in via METRICS_ENABLED (off by default), serving the prom-client default registry on a dedicated port (METRICS_PORT, default 9091), never on the public API port. Bind address (METRICS_BIND_IP, default loopback) is the security boundary; three documented deployment recipes.
  • Ships a ready-to-use infra/observability/prometheus.yml and updates agent-loop.rules.yml now that the endpoint exists.
  • RunActivityRegistry tracks per-run iteration/tool state and mints run-scoped loggers (agentRunId + sessionId) so concurrent sub-agent loop logs are individually greppable.
  • Reasoning loop gains an observer hook plus heartbeat, slow-tool, and abort-ignored logging, so a hang surfaces before the executor watchdog fires. The watchdog death-log is enriched with the run's last-known tool/iteration instead of an opaque timeout.

…rics

Recover a crashed swarm's pending runs into a single shared BudgetTracker
seeded with the session's already-recorded token spend, so recovered
siblings resume against the swarm's cumulative usage instead of each
getting a full new allowance. Add sumSessionTokenUsage() to the repository
and a BudgetTracker initialUsed constructor arg to support this.

Add Prometheus metrics for concurrency slots (active/pending/max) and the
two force-stop backstops (watchdog, reaper), plus alert rules under
infra/observability, and chaos/unit tests covering recovery and races.
Add a standalone Prometheus scrape server (opt-in via METRICS_ENABLED, off
by default) that serves the prom-client default registry on a dedicated
port, separate from the public API port. Ship a ready-to-use prometheus.yml
scrape config and update agent-loop.rules.yml now that the endpoint exists.

Introduce RunActivityRegistry to track per-run iteration/tool state and mint
run-scoped loggers (agentRunId + sessionId), making concurrent sub-agent
loop logs individually greppable. Feed it from the reasoning loop via a new
observer hook, and add heartbeat, slow-tool, and abort-ignored logging so a
hang surfaces before the executor watchdog fires. Enrich the watchdog
death-log with the run's last-known tool/iteration.
@jasonli0226
jasonli0226 merged commit 5a2f02b into main Jul 3, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants