feat: harden agent-loop stability with shared budget recovery, metrics, and hang tracing - #50
Merged
Merged
Conversation
…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.
enkizan
approved these changes
Jul 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
78a1517)/metricsscrape endpoint + per-run activity tracing (d9cc360)1. Shared budget recovery
BudgetTrackerseeded 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.sumSessionTokenUsage()to the agent-run repository and aninitialUsedconstructor arg toBudgetTracker.infra/observability/.2. Metrics endpoint + hang tracing
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.infra/observability/prometheus.ymland updatesagent-loop.rules.ymlnow that the endpoint exists.RunActivityRegistrytracks per-run iteration/tool state and mints run-scoped loggers (agentRunId+sessionId) so concurrent sub-agent loop logs are individually greppable.