Skip to content

fix(roadmap): limit each agent run of fix-plan below the budget margin #5379

Open

Description

@dan-petty

Problem

One agent run of the fix-plan step (a plan run or a review run) has no time limit of its own. The time budget of #5363 (PR #5380) is checked only before each run, and it keeps a margin of 1200 s below the Job deadline for one more run. On 2026-11-13 the longest run of the #5350 attempt took 1013 s, close to that margin. A longer run would pass the Job deadline, and the attempt would end with no result line again.

The first text of this item said that the attempt stalled for 39 minutes. That was false: the full Tempo trace (12.6 MB) shows continuous work until the Job deadline. A read through the Grafana MCP proxy stops at its 10 MiB limit, so the summary showed an early end. The evidence is in the comment above.

Proposed fix

Give each agent run of fix-plan an upper limit from a setting, below the margin that the step budget keeps (nested timeouts: run limit < margin < Job deadline). When a run reaches its limit, stop it, keep the plan branch as it is, and end the attempt with the timeout result line of #5363.

Acceptance criteria

  • A test with a run that does not end finds the timeout result line before the margin ends. It fails on main (devops ci fail-to-pass).
  • A test holds the order: run limit < step margin < Job deadline of the templates.

Dependencies

Depends on #5380 (the step budget).

Alternatives check of 2026-11-22: in-use.
The repository already uses this tool.
Tool: pydantic/pydantic-ai (https://github.com/pydantic/pydantic-ai), MIT, last activity 2026-11-22, maintained.
Reason: Searches found no dedicated tool for a per-run wall-clock limit; the project's pydantic-ai supplies the before_model_request hook seam and per-request timeouts, but no run limit or nested run/budget/deadline order.

Covers:

  • Hooks seam before each model request
  • Per-request timeout as the inner limit
  • UsageLimits for request and tool-call counts

Lacks:

  • Wall-clock limit for one whole agent run
  • Nested run limit, step margin and Job deadline order
  • Setting, validation and timeout result line

Proposed design

Problem statement

One agent run of the fix-plan step has no time limit of its own. The step budget of #5363 is checked only before each run and keeps a margin below the Job deadline for one more run. On 2026-11-13 the longest run of the #5350 attempt took 1013 s, close to that margin. A longer run can pass the Job deadline, and the attempt then ends with no result line.

Acceptance criteria

Requirement: Per-run limit ends a run

The fix-plan step SHALL stop one agent run when it reaches roadmap.fix_plan.run_time_limit_seconds and end the attempt with the timeout result line.

Scenario: A run past its limit writes the timeout line

  • WHEN A test runs fix-plan with roadmap.fix_plan.run_time_limit_seconds of 1 s and roadmap.fix_plan.time_budget_seconds of 5 s, and the first model reply advances the clock past the run limit and asks for another request.
  • THEN The run stops before the next request, the plan branch keeps its last commit, the result line holds result=timeout, class=timeout and run_limit=1, and uv run devops ci fail-to-pass --base origin/main shows the test fails on main.

Requirement: Defaults nest under the Job deadline

The default per-run limit MUST stay above the default model request timeout, below the default fix-plan time budget, and below the margin that budget leaves to the deadline of each Job template.

Scenario: The k8s gate check holds the nested order

  • WHEN uv run devops ci --check --only k8s runs the gate check of the rendered devops-cli and devops-cli-runner CronJobs and the default constants.
  • THEN The command exits 0, and it fails when the run limit is at or below DEFAULT_AI_REQUEST_TIMEOUT_SECONDS, at or above DEFAULT_ROADMAP_FIX_PLAN_TIME_BUDGET_SECONDS, or at or above activeDeadlineSeconds - DEFAULT_ROADMAP_FIX_PLAN_TIME_BUDGET_SECONDS of a template.

Requirement: Settings refuse a limit at or above the budget

The settings load MUST refuse a fix-plan run limit at or above the step time budget.

Scenario: A run limit at the budget is refused

  • WHEN A test loads the settings with roadmap.fix_plan.run_time_limit_seconds equal to roadmap.fix_plan.time_budget_seconds.
  • THEN The load raises a validation error that names both settings, and the same test passes when the run limit is below the budget.

Requirement: Generated reference lists the setting

The generated configuration reference MUST list roadmap.fix_plan.run_time_limit_seconds with its default.

Scenario: The generated docs are current

  • WHEN uv run devops docs check runs after uv run devops docs generate.
  • THEN The command exits 0 with no stale generated document, and docs/CONFIGURATION.md lists roadmap.fix_plan.run_time_limit_seconds=1500.

Existing Tool Considered

pydantic-ai 2.59.0 (pyproject.toml:40) is the open, maintained agent library. It gives the Hooks(before_model_request=...) seam (src/devops_cli/roadmap/implement/implement_limits.py:298-318) and UsageLimits for request and tool-call counts (src/devops_cli/roadmap/stage/stage_toolbox.py:24 and :83-88), but it has no wall-clock limit for one agent run and no nested run/budget/deadline ordering. The alternatives check of 2026-11-17 found no dedicated tool. The change builds the limit in the project's fix-plan runner: about 30k tokens to build, about 5k tokens for each later fix, and 1 to 2 days elapsed.

Key questions

  • Did the fix-plan attempt stall, and does the item still have work?: No call hung: drain_events streams each model request, and each request of the full trace ended. The trace shows continuous work until the Job deadline; a read through the Grafana proxy stops at 10 MiB and hid the end. The item still has work: one run has no limit of its own, and the longest run took 1013 s near the margin of fix(roadmap): fix-plan ends at the Job deadline with no result line #5363. (best practice) [sources: src/devops_cli/roadmap/agent_run.py:50-54, src/devops_cli/roadmap/steps/fix_plan.py:89-93, docs/roadmap/stage.md:116, openspec/changes/archive/2026-11-13-5363-fix-plan-time-budget/design.md:17]
  • How does the step stop one agent run at its limit?: Add a pydantic-ai Hooks(before_model_request=...) capability to the agent run, as implement_limits.limit_hooks does, and raise RoadmapTimeBudgetError before the next model request once the run limit is spent. The request timeout (default 1200 s) is the inner limit that ends a request already in flight. revise_plan already catches RoadmapTimeBudgetError and maps it to FailureClass.TIMEOUT, so the attempt ends with the timeout result line. (best practice) [sources: src/devops_cli/roadmap/refine/refine_investigation.py:630, src/devops_cli/roadmap/implement/implement_limits.py:298-318, src/devops_cli/roadmap/stage/stage.py:3593-3607, openspec/changes/archive/2026-11-13-5363-fix-plan-time-budget/design.md:17]
    • Chosen: pydantic-ai Hooks(before_model_request=...). It runs on every model request of the run and raises before the next request once the run limit is spent; revise_plan already maps the error to the timeout class.
    • Rejected: asyncio.timeout around each run. The planner calls agent.run_sync, so there is no event loop to cancel; the archived fix(roadmap): fix-plan ends at the Job deadline with no result line #5363 design rejected it for this reason.
    • Rejected: HTTP client read timeout. It applies to each read, so a reply that keeps streaming is never stopped.
    • Rejected: No run limit, keep the step budget only. A run that starts near the budget end passes the Job deadline, as the 1013 s run shows.
  • What default does the per-run limit take?: 1500 s: above the 1200 s model request timeout and the 1013 s longest run of docs(openspec): plan #5118 Restore the Status on a claim release #5350, and below the 1800 s that the 3600 s step budget leaves to the 5400 s hand-made Job deadline (and the 3600 s it leaves to the 7200 s template deadline), with 300 s for the push and the result line. (best practice) [sources: src/devops_cli/config/defaults.py:141, src/devops_cli/config/defaults.py:1127, docs/SERVICE_PATTERNS.md:96, ARCHITECTURE.md:153, k8s/devops/cronjob.yaml:30]
    • Rejected: 1080 s. It is below the 1200 s model request timeout, so a single request can overrun the run limit and the limit is not an upper bound.
    • Rejected: 1200 s. It equals the request timeout, so a request can use the whole run limit and leave no margin for the next request.
    • Chosen: 1500 s. It is above the request timeout and the 1013 s run, and below the 1800 s margin of the 3600 s budget, leaving 300 s for the push and the result line.
    • Rejected: 1800 s. It equals the margin that the 3600 s budget leaves to the 5400 s Job deadline, so a run that starts at the budget end leaves no time for the push and the result line.
    • Rejected: 3600 s. It equals the step budget, so it adds no limit below the budget.
  • Which setting holds the per-run limit?: roadmap.fix_plan.run_time_limit_seconds, in RoadmapFixPlanConfig, with the same precedence (config file, environment, default). The settings load refuses a value at or above roadmap.fix_plan.time_budget_seconds, as the stage job's validator does for its nested timeouts. (best practice) [sources: src/devops_cli/config/settings.py:1942-1960, src/devops_cli/config/settings.py:1534-1538, src/devops_cli/config/settings.py:2140-2148, docs/CONFIGURATION.md:315]
    • Chosen: roadmap.fix_plan.run_time_limit_seconds. It follows the _seconds suffix of time_budget_seconds and step_time_limit_seconds and sits in the fix-plan group with the other limits.
    • Rejected: roadmap.fix_plan.run_limit_s. The project's settings use _seconds, not _s, for a time limit.
    • Rejected: Constant in the step code. A constant gives no way to tune the limit for a slow model tier.
  • Which result line ends an attempt that the per-run limit stops?: The timeout result line of fix(roadmap): fix-plan ends at the Job deadline with no result line #5363, with result=timeout and class=timeout; the line adds run_limit=<seconds> to its counts, so the cause is visible. revise_plan already maps RoadmapTimeBudgetError to FailureClass.TIMEOUT. (best practice) [sources: src/devops_cli/roadmap/steps/fix_plan.py:253-262, src/devops_cli/roadmap/steps/fix_plan.py:840-841, src/devops_cli/roadmap/steps/fix_plan.py:1297-1307, src/devops_cli/roadmap/job_result.py:269-306]
    • Chosen: Reuse the timeout line. One cause, one class: the job grade counts the attempts by class, and the existing comment and label rules already handle timeout.
    • Rejected: New class run-timeout. Two classes for one cause split the grade count and the dashboards.
    • Rejected: failed. A run limit is a time stop, not a model or check failure, and the retry rule of timeout already applies.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

#5363

Activity

  1. added
    source/agent
    type/bug
    scope/roadmap
    status/new
    value/medium
    effort/medium
    on Nov 13, 2026
  2. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor
  3. added
    priority/p2-medium
    on Nov 13, 2026
  4. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor

    A lead for the cause: the worker node hawk hung at about the same time. Its last journal line in Loki is at 04:38:28Z, 10 s after the last span of this attempt (04:38:18Z). The kubelet stopped reporting at 04:40:08Z, and the node did not come back. hawk was cordoned and ran only DaemonSet pods. Check whether a request of the attempt went through hawk (a DaemonSet on that node in the path, a model tier, DNS or the pod network) and whether the call had a client timeout that should have fired.

  5. added
    owner/agent
    on Nov 13, 2026
  6. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor

    The premise of this item is false: the fix-plan attempt did not stall. No call hung, so no client timeout had to fire. I return the item to New with this evidence.

    Evidence (read on 2026-11-13 by trace ID from the Tempo API /api/v2/traces/fbef6e37a8678ee158d313142a3f044e, and from Prometheus):

    • The trace holds 7351 spans. The spans of the step process (one pod of Job fix-plan-5350, process.pid 1) continue to 05:15:34Z. The LLM gateway spans continue to 05:15:38Z.
    • The largest gap between two span ends of the step process is 531 s (04:55:10Z to 05:04:01Z). The second largest is 422 s (04:43:17Z to 04:50:19Z). Each gap is one model request in progress.
    • These two model requests are the longest of the trace. The request at 04:43:15Z took 424 s and gave 44,346 output tokens (43,649 reasoning tokens). The request at 04:55:07Z took 534 s and gave 39,729 output tokens (32,835 reasoning tokens). Both ended with the finish reason tool_call. The gateway sent HTTP 200 after about 2 s and then streamed the reply. All 217 model requests of the trace ended.
    • The attempt made 11 agent runs that ended, and one more run was in progress at the end. The runs after 04:33Z took 1013 s (04:33:25Z to 04:50:19Z), 821 s (04:50:19Z to 05:04:01Z), 500 s (05:04:10Z to 05:12:30Z) and 117 s (05:12:37Z to 05:14:34Z).
    • The pod started at 03:45:08Z (kube_pod_start_time). 5400 s after that is 05:15:08Z. The last span of the step ends 26 s later, in the termination grace period. Thus the Job deadline stopped a process that still did work.
    • The pod ran on a different worker node, not on the node that hung at 04:38:28Z (kube_pod_info). The spans of the step continue after 04:38:28Z. Thus the node failure did not stop the attempt.
    • Why the trace looked short: the Tempo search summary of this trace gives a duration of 3,172,907 ms, an end at 04:40:35Z. The full trace is 12.6 MB, and a read through the Grafana API proxy of the Grafana MCP server stops at 10 MiB. Read the trace by its ID from the Tempo API to get all spans.

    Client timeouts on origin/main: drain_events in src/devops_cli/roadmap/agent_run.py streams each model request (#2379). The read timeout of an HTTP client applies to each read, so it does not stop a reply that continues to send chunks. That is correct for these requests: each one sent chunks until it ended.

    What is left: the attempt needed more than 5400 s of work. #5363 (pull request #5380) covers this failure. With its default budget of 3600 s, the budget ends at about 04:45Z, the run in progress ends at 04:50:19Z, the step starts no new run, and it writes the result line with class timeout. One agent run has no upper limit of its own, and the longest run of this attempt (1013 s) is near the margin of 1200 s that #5380 keeps for one more run. If a limit on each agent run is necessary, write that as the new body of this item. Else close this item as covered by #5363.

  7. removed
    owner/agent
    on Nov 13, 2026
  8. changed the title [-]fix(roadmap): find the 39-minute stall of a fix-plan attempt[/-] [+]fix(roadmap): limit each agent run of fix-plan below the budget margin[/+] on Nov 13, 2026
  9. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    Alternatives check: none

    Reason: Searches for a library that bounds a single agent run (per-run timeouts, nested deadline budgets, streaming LLM run limits) returned only unrelated material — web-platform timer APIs (setTimeout, XMLHttpRequest.timeout), an HTTP 408 page and container images — and nothing that imposes an upper limit on one agent run of a job. The need is a change inside the project's own fix-plan runner (a per-run limit from a setting, the nested order run limit < step margin < Job deadline, stopping the run and writing the timeout result line of #5363, plus the two tests), which no external tool provides.

    Tool: none found

    Lacks:

    • An upper time limit for each agent run of the fix-plan step, taken from a setting
    • Nested timeout ordering: run limit < step budget margin < Job deadline
    • Stopping a run at its limit, keeping the plan branch as it is, and ending the attempt with the timeout result line of fix(roadmap): fix-plan ends at the Job deadline with no result line #5363
    • A test in the project's suite that an unending run yields the timeout line before the margin, and a test that holds the order of the three limits

    Searches run:

    • asyncio.timeout cancel scope per task deadline python 3.11, results: 5
    • kubernetes Job activeDeadlineSeconds nested timeout step budget, results: 5
    • python enforce per-run timeout agent run streaming LLM nested deadline library, results: 5
    • pydantic-ai run timeout usage limits request timeout, results: 5
    • timeout budget agent loop open source library python, results: 5
    • anyio fail_after move_on_after timeout cancel scope documentation, results: 5
    • pydantic-ai agent run timeout documentation, results: 5
    • python httpx read timeout streaming response not stopping long stream, results: 5
    • "step timeout" "job deadline" nested timeout kubernetes template test, results: 5
    • github pydantic-ai timeout agent run, results: 5

    Sources:

    Checked on 2026-11-17. A person decides what this means for the item. The check changes no label and closes no issue.

  10. added
    status/ready
    and removed
    status/new
    on Nov 17, 2026
  11. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 2620fae (see proposed design in issue body).

  12. dan-petty commented on Nov 20, 2026

    @dan-petty
    OwnerAuthor

    Frozen on 2026-11-20. The stage, implement and fix-checks jobs grade under 60%, so the owner rule asks for a re-architecture before more fixes inside the current design (tracker #6519). This item is incremental work on those jobs, so its Dependencies section now names #6521 first. Stage and implement leave this item while #6521 is open. When the experiments of #6519 decide, a person closes #6521 (this item can then continue) or closes this item as not planned.

  13. devops-cli-service commented on Nov 22, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: none, no tool. A person decides what the new result means for the item.

    Searches (10)

    Tools considered:

    Searches run:

    • pydantic-ai per-run wall clock timeout limit agent run, results: 5
    • python library enforce timeout on LLM agent run nested timeouts budget, results: 5
    • pydantic-ai UsageLimits timeout documentation agent run, results: 5
    • "pydantic-ai" request timeout model settings documentation, results: 5
    • pydantic-ai GitHub time limit per run issue timeout, results: 5
    • LLM agent framework wall-clock deadline per run budget guard library, results: 5
    • ai.pydantic.dev timeouts, results: 5
    • github pydantic pydantic-ai, results: 5
    • "before_model_request" pydantic-ai hooks, results: 5
    • pydantic-ai time budget plugin per-run deadline, results: 5

    Sources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/roadmapsource/agentstatus/readytype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions