Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schema: spec-driven
created: 2026-11-12
skip_specs: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Design

## Decisions

- `run_step`, `run_fix_step` and `ToolboxClient` take one pydantic-ai `UsageLimits` as `usage_limits`, which is the name that `Agent.run_sync` uses. One value holds the request limit and the tool call limit, so the code adds no type of its own.
- The default of `usage_limits` is a module value that the signature shows, and not `None`. Thus one test reads the defaults of the three signatures and compares them with the constants, and no test reads a private name.
- The step passes `usage_limits` down to each run of the agent: the first turn, each send again and each extra turn. The extra turn continues the conversation, so it counts against the same limits as before.
- The tests that reach a limit keep their relative asserts and use small limits: 6 requests and 12 tool calls for a step, and 12 requests and 8 tool calls for a toolbox run. At four tool calls in each reply, the tool call limit of the step comes before its request limit. In a toolbox run, the call budget takes the tools before the request limit does, as with the constants (200 calls, 250 requests).
- Two tests keep the constants. Each sends one reply that holds one more tool call than the limit, so it makes one request and fails at once.
- The implement world sets `roadmap.implement.monitor_timeout_seconds` to three poll intervals (90 s). A watch that waits out the monitor polls the pull request 4 times and not 121. The longest wait of a test that settles is three intervals (`test_watch_progress_line_the_watch_writes_a_line_at_each_interval`).

## Rejected alternatives

- A `usage_limits` field on `ToolCallLog` or on `StepBudget`. The log holds the counts of a step, and `StepBudget` holds the token and time budgets of `roadmap.implement`. A field would carry the limits to the turn with fewer edits, but it would hide an input of the run in a value with another purpose.
- A setting for the limits. The limits are the loop guard of a job, and no person tunes them (`tests/roadmap/test_roadmap_limit_classes.py`, class `guard`). A setting would add a variable that no operator needs.
- A default of `None` that the code reads as the constants. The test of the defaults would then have to run a step to 500 requests, or read a private name.
- A probe tool through `mcp_toolset` that records `RunContext.usage_limits`. It proves the default of a step with two requests, but `ToolboxClient` takes only a `RoadmapMcpToolset`, so the toolbox would need a second method.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Proposal: #4688

## Why

The tests of the run limits drive the limits of the jobs through a scripted model (#4688). A step stops at 500 requests or 1,500 tool calls, and a toolbox run of the stage job takes its tools at 197 of 200 tool calls. A scripted model reaches these limits only after hundreds of requests, and each request of a step runs real git. The CI and test audit of 2026-11-11 measured 5.3 to 11.6 s for each of the 12 slowest of these tests, 108.5 s together, with no coverage. Nine tests of the implement world also wait out the watch of the checks: 3,600 s at a poll of 30 s, which is 121 polls of the pull request, 48.9 s together.

## What Changes

- `run_step` and `run_fix_step` take `usage_limits`, a pydantic-ai `UsageLimits`. Each run of the agent uses it, extra turns included. Its default is `CONST_ROADMAP_IMPLEMENT_MAX_REQUESTS` requests and `CONST_ROADMAP_IMPLEMENT_MAX_TOOL_CALLS` tool calls, as before.
- `ToolboxClient` takes `usage_limits` for each plan run and review run. Its default is `CONST_ROADMAP_STAGE_MAX_REQUESTS` requests and `CONST_ROADMAP_STAGE_TOOL_MAX_CALLS` tool calls, as before.
- The 13 tests that reach a limit pass small limits and keep their relative asserts. One test proves that the three defaults equal the constants.
- The implement world of the tests sets `roadmap.implement.monitor_timeout_seconds` to `WATCH_SECONDS`, three poll intervals. The watch tests that name the timeout read `WATCH_SECONDS`.
- The jobs pass no limits, so no consumer sees a change: the change has no spec delta (`skip_specs: true`).

## Capabilities

None. The jobs run at the same limits, and the new parameters only let a caller set lower limits.

## Impact

- The tests of `tests/roadmap/implement/` and `tests/roadmap/stage/` take less CI worker time. The before and after times are in a comment of the pull request.
- The tests no longer run at the numbers of the jobs. The test of the defaults covers the wiring of the constants, and the limit tests cover the wiring of a given value.
- The change fits one pull request.

## Feasibility

- pydantic-ai 2.59.0 (`uv pip show pydantic-ai-slim`): `Agent.run_sync` takes `usage_limits`, and `UsageLimits` is a dataclass with `request_limit` and `tool_calls_limit` that compares by value. `RunContext.usage_limits` holds the limits of the run, and `_limit_reached` of `src/devops_cli/roadmap/refine/refine_investigation.py` reads it, so the toolbox takes its tools at the given limit less 4 calls.
- `_run_turn` of `src/devops_cli/roadmap/implement/implement_step.py` built the one `UsageLimits` of a step from the constants, and `ToolboxClient.chat_structured` built the one of a toolbox run. No other code builds these limits.
- `monitor_pr` of `src/devops_cli/github/pr_monitor.py` polls at 0 s and after each interval until the timeout, so a timeout of 90 s and an interval of 30 s give 4 polls. The `config` fixture merges each call, so the setting of the world stays when a test adds its own.

## Existing Tool Considered

`UsageLimits` of pydantic-ai, which the code already uses. No new tool and no new code of our own: the change passes the existing value through. Cost: about 30,000 tokens and 2 hours. The change deletes the two inline `UsageLimits` builds of the step and of the toolbox.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Tasks

## 1. The caller gives the limits of a run (behavior)

- [x] 1.1 Write a failing test: the default `usage_limits` of `run_step`, `run_fix_step` and `ToolboxClient` equal the guards `CONST_ROADMAP_IMPLEMENT_MAX_REQUESTS`, `CONST_ROADMAP_IMPLEMENT_MAX_TOOL_CALLS`, `CONST_ROADMAP_STAGE_MAX_REQUESTS` and `CONST_ROADMAP_STAGE_TOOL_MAX_CALLS`.
- Verification: `$ uv run devops ci test tests/roadmap/test_roadmap_limit_classes.py`
- [x] 1.2 Add `usage_limits` to `run_step` and `run_fix_step` in `src/devops_cli/roadmap/implement/implement_step.py`, pass it to each run of the agent, and add `usage_limits` to `ToolboxClient` in `src/devops_cli/roadmap/stage/stage_toolbox.py`. The MCP test that calls `_attempt` in `tests/roadmap/test_roadmap_mcp.py` passes it too.
- Verification: `$ uv run devops ci test tests/roadmap/test_roadmap_limit_classes.py tests/roadmap/implement/test_roadmap_implement_step.py tests/roadmap/stage/test_roadmap_stage_toolbox.py tests/roadmap/test_roadmap_mcp.py`

## 2. The tests of the limits and of the watch run small (other)

- [x] 2.1 Give the tests that reach a limit small limits: `tests/roadmap/implement/test_roadmap_implement_step.py`, `tests/roadmap/implement/test_roadmap_implement_step_repair.py` and `tests/roadmap/stage/test_roadmap_stage_toolbox.py`. Each of these tests reaches its limit in 12 model requests or fewer.
- Verification: `$ uv run pytest tests/roadmap/implement/test_roadmap_implement_step.py tests/roadmap/implement/test_roadmap_implement_step_repair.py tests/roadmap/stage/test_roadmap_stage_toolbox.py --no-cov --durations=15`
- [x] 2.2 Set `roadmap.implement.monitor_timeout_seconds` to `WATCH_SECONDS`, three poll intervals, in the implement world of `tests/support/roadmap_implement.py`. A watch that waits out the monitor polls the pull request 4 times. The watch tests of `tests/core/test_roadmap_implement_checks.py` read `WATCH_SECONDS`.
- Verification: `$ uv run pytest tests/roadmap/implement/test_roadmap_implement_phase_marks.py tests/roadmap/implement/test_roadmap_implement_typed_result.py tests/core/test_roadmap_implement_checks.py --no-cov --durations=15`

- Pending a person: review the diff in tuicr (tuicr -r origin/main..HEAD).
69 changes: 47 additions & 22 deletions src/devops_cli/roadmap/implement/implement_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@
_REQUIREMENT_PREFIX = "requirement:"
# The start of an HTML comment, such as a marker of a job in an issue body.
_COMMENT_OPEN = "<!--"
# The limits of the run of a step when the caller gives none: the loop guard of the job (#3206).
_STEP_LIMITS: Final = UsageLimits(
request_limit=CONST_ROADMAP_IMPLEMENT_MAX_REQUESTS,
tool_calls_limit=CONST_ROADMAP_IMPLEMENT_MAX_TOOL_CALLS,
)


class StepClass(StrEnum):
Expand Down Expand Up @@ -843,17 +848,19 @@ def _run_agent(
log: ToolCallLog,
message: str,
conversation: Conversation | None = None,
*,
usage_limits: UsageLimits,
) -> _Run:
"""One turn of the agent, and each send again of a request that failed inside a stream (#2794).
A request is sent again at most `CONST_ROADMAP_IMPLEMENT_MODEL_RESENDS` times in a row. A send
that gets a reply gives the next failed request its own resends. The request limit of the
step ends the loop. The turn ends with the last send.
that gets a reply gives the next failed request its own resends. The request limit of
`usage_limits` ends the loop. The turn ends with the last send.

A failure of class `parser` is deterministic for one conversation, so its resend carries the
message that asks for a smaller edit, once on a tier. The next parser failure of the tier ends
the turn with no resend, and the step stops with class `parser` (#3053). A tier is a new
conversation, so the tier loop of `run_step` starts the count again."""
run = _run_turn(agent, model, log, message, conversation)
run = _run_turn(agent, model, log, message, conversation, usage_limits=usage_limits)
resends = CONST_ROADMAP_IMPLEMENT_MODEL_RESENDS
while run.resend is not None and resends > 0:
sent = run.resend
Expand All @@ -864,7 +871,7 @@ def _run_agent(
sent = _ask_smaller_edit(sent)
if log.trace is not None:
log.trace.retry(run.reason)
later = _run_turn(agent, model, log, None, sent)
later = _run_turn(agent, model, log, None, sent, usage_limits=usage_limits)
stalled = later.resend is not None and len(later.resend.messages) <= len(sent.messages)
resends = resends - 1 if stalled else CONST_ROADMAP_IMPLEMENT_MODEL_RESENDS
run = run.then(later)
Expand All @@ -877,16 +884,14 @@ def _run_turn(
log: ToolCallLog,
message: str | None,
conversation: Conversation | None = None,
*,
usage_limits: UsageLimits,
) -> _Run:
"""One send of a request to the agent under the limits of a step, and its spend as one record.
With `conversation`, the turn continues it: its messages and its usage carry over, so the
limits count both turns. Without `message`, it resumes the request that the conversation ends
with. A failed model call ends the turn as stopped, with the replies that came before
it, as a limit does. Raises `AICredentialsError` when the model refuses the key."""
limits = UsageLimits(
request_limit=CONST_ROADMAP_IMPLEMENT_MAX_REQUESTS,
tool_calls_limit=CONST_ROADMAP_IMPLEMENT_MAX_TOOL_CALLS,
)
"""One send of a request to the agent under `usage_limits`, the limits of a step, and its spend
as one record. With `conversation`, the turn continues it: its messages and its usage carry
over, so the limits count both turns. Without `message`, it resumes the request that the
conversation ends with. A failed model call ends the turn as stopped, with the replies that came
before it, as a limit does. Raises `AICredentialsError` when the model refuses the key."""
reason = last = ""
kept = resend = None
cut: list[ModelResponse] = []
Expand All @@ -899,7 +904,7 @@ def _run_turn(
result = agent.run_sync(
message,
conversation=conversation,
usage_limits=limits,
usage_limits=usage_limits,
event_stream_handler=drain_events,
)
last, kept = result.output, result.conversation
Expand Down Expand Up @@ -1722,11 +1727,13 @@ def _turn(
log: ToolCallLog,
message: str,
conversation: Conversation | None = None,
*,
usage_limits: UsageLimits,
) -> _Run:
"""One turn of the agent. When it ends, so does every process of the agent's shell, before
the job runs a program that gets the session's token."""
"""One turn of the agent under `usage_limits`. When it ends, so does every process of the
agent's shell, before the job runs a program that gets the session's token."""
try:
return _run_agent(agent, model, log, message, conversation)
return _run_agent(agent, model, log, message, conversation, usage_limits=usage_limits)
finally:
_end_agent_processes()

Expand Down Expand Up @@ -1787,6 +1794,8 @@ def _extra_turn(
keeper: WipKeeper,
first: _Run,
ask: _Ask,
*,
usage_limits: UsageLimits,
) -> tuple[_Run, _Failure | None]:
"""One extra turn of a step (#2721, #2795, #3043): the same conversation and the same limits as
the first turn, and the message of `ask`. The turns so far and this one together, and the
Expand All @@ -1800,7 +1809,9 @@ def _extra_turn(
# in it is no ground for a `wip:` commit (#3054).
before_request, log.before_request = log.before_request, None
try:
run = first.then(_turn(agent, model, log, ask.message, first.conversation))
run = first.then(
_turn(agent, model, log, ask.message, first.conversation, usage_limits=usage_limits)
)
finally:
log.before_request = before_request
if run.reason:
Expand Down Expand Up @@ -1859,6 +1870,7 @@ def _repair_rounds(
*,
tier: str,
trace: ImplementTrace | None,
usage_limits: UsageLimits,
) -> _Rounds:
"""Judge the attempt of `start`, and run an extra turn for its failure while a round is left
and the failure can be repaired. Each attempt is an event of the trace, with whether a round
Expand All @@ -1872,7 +1884,9 @@ def _repair_rounds(
return _Rounds(run, failure, used, number, start.allowed, spent=ask is not None)
if trace is not None:
trace.retry(failure.reason if failure else "")
run, failure = _extra_turn(agent, model, log, source, inputs, keeper, run, ask)
run, failure = _extra_turn(
agent, model, log, source, inputs, keeper, run, ask, usage_limits=usage_limits
)
used += 1
number += 1

Expand Down Expand Up @@ -1916,6 +1930,7 @@ def _attempt(
model_for_tier: Callable[[str], Any] | None,
tier_health: Callable[[Sequence[str]], Mapping[str, bool]],
limits: StepBudget,
usage_limits: UsageLimits,
trace: ImplementTrace | None = None,
mcp_toolset: AbstractToolset[Any] | None = None,
) -> StepOutcome:
Expand All @@ -1926,7 +1941,8 @@ def _attempt(
on a class that selects `change_tier` moves to the next healthy tier at once, spends no repair
round, and discards the checkout of the failed attempt, back to the last `wip:` commit of the
keeper (#3060, #3054). Each attempt is an event of the `trace`, and the agent stops before a
request when `limits` is spent."""
request when `limits` is spent. Each run of the agent has the requests and tool calls of
`usage_limits`, and its extra turns count against them."""
settings = load_settings()
budget = settings.roadmap.implement.repair_rounds
if mcp_toolset is None and settings.roadmap.implement.mcp:
Expand Down Expand Up @@ -1963,7 +1979,7 @@ def _attempt(
log.fail_to_pass = None
log.verified.clear()
log.parser_failures = 0
run = _turn(agent, tier_model, log, inputs.message)
run = _turn(agent, tier_model, log, inputs.message, usage_limits=usage_limits)
first_request = first_request or run.first_request_tokens
failure = (
_Failure(run.reason, klass=run.failure_class)
Expand All @@ -1980,6 +1996,7 @@ def _attempt(
_Rounds(run, failure, rounds, number, budget),
tier=tier,
trace=trace,
usage_limits=usage_limits,
)
run, failure, rounds, number = (
repaired.run,
Expand Down Expand Up @@ -2102,6 +2119,7 @@ def run_step(
recorded_tests: frozenset[str] = frozenset(),
mcp_toolset: AbstractToolset[Any] | None = None,
earlier: EarlierStop | None = None,
usage_limits: UsageLimits = _STEP_LIMITS,
) -> StepOutcome:
"""Run group `group` of item `item` in the checkout `source`, with the pydantic-ai `model`.

Expand Down Expand Up @@ -2134,6 +2152,9 @@ def run_step(
`recorded_tests` holds each test, as `path::name`, that the step may lose (#3490). The caller
reads it from a text that the agent cannot edit. With no value, each lost test stops the step.
`earlier` holds the stop of the earlier Job, which only the first step of a run gets (#4071).
`usage_limits` holds the requests and the tool calls of a run of the agent: the loop guard of
the job, `CONST_ROADMAP_IMPLEMENT_MAX_REQUESTS` and `CONST_ROADMAP_IMPLEMENT_MAX_TOOL_CALLS`,
when the caller gives none (#4688).
"""
inputs = replace(
build_step_inputs(
Expand Down Expand Up @@ -2161,6 +2182,7 @@ def run_step(
model_for_tier=model_for_tier,
tier_health=tier_health or _gateway_tier_health,
limits=StepBudget(spend, progress=log.progress),
usage_limits=usage_limits,
trace=trace,
mcp_toolset=mcp_toolset,
)
Expand All @@ -2186,11 +2208,13 @@ def run_fix_step(
trace: ImplementTrace | None = None,
spend: ItemSpend | None = None,
recorded_tests: frozenset[str] = frozenset(),
usage_limits: UsageLimits = _STEP_LIMITS,
) -> StepOutcome:
"""Run the fix of the failed check `fix` for item `item` in the checkout `source`: a step
whose group is "Make check <name> pass". It commits with the type and the scope of the item,
that title and the issue reference (`step_commit_title`) when the exit checks pass, and
`tasks.md` is as it was. Its ladder is the `repair` ladder (#3060)."""
`tasks.md` is as it was. Its ladder is the `repair` ladder (#3060). `usage_limits` is as in
`run_step`."""
return run_step(
store,
item=item,
Expand All @@ -2207,6 +2231,7 @@ def run_fix_step(
trace=trace,
spend=spend,
recorded_tests=recorded_tests,
usage_limits=usage_limits,
)


Expand Down
Loading