Skip to content

fix(roadmap): halt implement step on consecutive command refusals #4043

Closed

Description

@dan-petty

Problem Description

In src/devops_cli/roadmap/implement/implement_step.py, when the shell refuses a command through implement_shell_policy.py, the refusal is returned as a tool message. The step continues executing turns until CONST_ROADMAP_IMPLEMENT_MAX_REFUSALS is reached across the entire step or until the token budget is exhausted.

When a model encounters a tool refusal (such as an unpermitted uv subcommand or command chaining), it can loop repeatedly across many turns. Because conversation history accumulates without compaction (growing past 150,000 tokens per request), the step burns through millions of tokens (for example, spending 8,022,108 tokens across 77 model calls on issue #3247) before stopping with LimitReached: The step spent its budget roadmap.implement.step_token_budget=8000000.

A consecutive refusal circuit breaker is needed to halt or change tier after a small threshold of consecutive refused commands (for example, 3 consecutive refusals), rather than allowing token budget exhaustion.

Reproduction Steps

  1. Run an implement step where the model produces commands that the shell policy refuses repeatedly.
  2. Inspect the step trace: the turn count grows, conversation history expands to 150k+ tokens per turn, and the step runs until roadmap.implement.step_token_budget=8000000 is consumed.
  3. Observe that no consecutive refusal threshold stops the looping attempt early.

Expected vs Actual Behavior

Expected: The step halts or switches tier when an attempt produces 3 consecutive command refusals without an intervening accepted tool execution.
Actual: The step continues calling the model until the 8,000,000 token budget is exhausted.

Environment Diagnostics

  • DevOps CLI Version: 0.83.2
  • Python Version: 3.14.10
  • OS & Architecture: Linux x86_64
  • Execution Environment: Kubernetes runner

Logs, Traces, or Terminal Output

group 1: attempt 1 on deepseek-v4.1-flash-1017, failed (limit_reached): LimitReached: The step spent its budget `roadmap.implement.step_token_budget=8000000`.
Refused commands: uv 3, other:da7901c7cec4 1. Other retries: 0., next: stop

Pre-Submission Checklist

  • I have verified that no sensitive credentials, private keys, or internal tokens are contained in this issue report.
  • I have checked existing issues and pull requests to ensure this is not a duplicate.

Proposed design

Problem statement

The implement step continues after the shell refuses a command. A model can repeat a refused command for many turns. The step then spends its full token budget before it stops. The change adds a circuit breaker. The breaker stops the run after 3 consecutive refusals with no accepted tool call between them.

Acceptance criteria

Requirement: Consecutive refusal stop

The implement step SHALL stop the run when the shell policy refuses 3 commands in a row with no accepted tool call between them.

Scenario: Three refusals in a row

  • WHEN The shell policy refuses 3 commands in a row and no tool call succeeds between them.
  • THEN The step stops with failure class refused_tool, the reason names the consecutive limit of 3, and the checkout resets to its last wip commit.

Requirement: Accepted call resets count

The consecutive refusal count SHALL reset to 0 after a tool call that the shell policy does not refuse.

Scenario: Refusals split by an accepted call

  • WHEN The shell policy refuses 2 commands, then a tool call succeeds, then the shell policy refuses 2 more commands.
  • THEN The step does not stop for the consecutive limit, and the count is 2 after the fourth refusal.

Requirement: Total refusal limit stays

The implement step SHALL still stop the run when the total shell refusals reach CONST_ROADMAP_IMPLEMENT_MAX_REFUSALS.

Scenario: Alternating refusals reach the total limit

  • WHEN The shell policy refuses commands with an accepted tool call between each pair until the total reaches 30.
  • THEN The step stops with the existing total-refusal reason and failure class refused_tool.

Requirement: Threshold constant

The project SHALL define CONST_ROADMAP_IMPLEMENT_MAX_CONSECUTIVE_REFUSALS with the value 3.

Scenario: Constant value

  • WHEN A test imports CONST_ROADMAP_IMPLEMENT_MAX_CONSECUTIVE_REFUSALS from devops_cli.config.constants.
  • THEN The value is 3.

Existing Tool Considered

No alternatives check has run. No open, maintained tool or library covers a consecutive-refusal circuit breaker inside this pydantic-ai implement step. The change adds a counter to the existing ToolCallLog and one constant, about 30 lines of code and two tests. Estimated cost: about 8,000 tokens to build, about 1,000 tokens per year to keep and fix, and about 3 hours elapsed. A new dependency would cost more than this code.

Key questions

  • What does the current total refusal limit do?: CONST_ROADMAP_IMPLEMENT_MAX_REFUSALS is 30. The 31st refusal ends the run with the class refused_tool, and that class maps to the wait action. [sources: openspec/changes/archive/2026-11-04-3271-shell-refusal-counts-no-retry/proposal.md, openspec/changes/archive/2026-11-04-3271-shell-refusal-counts-no-retry/design.md, docs/ROADMAP_WORKFLOW.md:252]
  • What threshold stops a run of consecutive refusals?: The threshold is 3 consecutive refusals. This value stops a loop after a small number of mistakes and matches the issue proposal. (best practice) [sources: src/devops_cli/config/constants.py, openspec/changes/archive/2026-11-04-3271-shell-refusal-counts-no-retry/proposal.md]
    • Chosen: 3 refusals. It stops a loop quickly and leaves room for one or two model mistakes.
    • Rejected: 5 refusals. It spends more tokens before the stop and adds no clear benefit.
    • Rejected: Reuse the total limit of 30. It is the defect: the step can burn millions of tokens before it stops.
  • Does the breaker halt the step or change the model tier?: The breaker halts the step. It uses the existing refused_tool class and the existing wait action. A new tier would meet the same shell policy and can repeat the loop. (best practice) [sources: docs/ROADMAP_WORKFLOW.md:252, openspec/changes/archive/2026-11-04-3271-shell-refusal-counts-no-retry/design.md, src/devops_cli/roadmap/failure.py]
    • Chosen: Halt with refused_tool. It matches the existing total-refusal stop and needs no new failure class.
    • Rejected: Change tier. The next tier uses the same shell policy, so it can repeat the same refusals.
  • Where does the step keep the consecutive refusal count?: The count lives in ToolCallLog, which ShellRefusal already uses. The log increments it on each refusal and resets it on each accepted tool call. The log raises the stop when the count reaches the limit. The count spans the whole step, extra turns included. (best practice) [sources: src/devops_cli/roadmap/implement/implement_tool_log.py, src/devops_cli/roadmap/implement/implement_shell_refusal.py, src/devops_cli/roadmap/implement/implement_step.py]
    • Chosen: Keep it in ToolCallLog. The log already holds the refusal total and ends a run on UnexpectedModelBehavior.
    • Rejected: Count it in implement_step.py after each turn. It misses refusals inside a turn and duplicates state that the log owns.
    • Rejected: Count it only in ShellRefusal. The log is the shared record of tool calls and already enforces the total limit.
  • What error text and failure class does the breaker use?: The log raises UnexpectedModelBehavior with the text Too many consecutive commands refused by policy: the limit is 3. The text keeps the phrase refused by policy, so the existing classifier maps it to refused_tool. The change adds no failure class. (best practice) [sources: openspec/changes/archive/2026-11-04-3271-shell-refusal-counts-no-retry/design.md, openspec/changes/archive/2026-11-04-3271-shell-refusal-counts-no-retry/specs/roadmap-implement-step/spec.md, src/devops_cli/roadmap/failure.py]
    • Chosen: Reuse refused_tool. It is the existing class for shell-policy refusals and maps to the wait action.
    • Rejected: Add a new consecutive_refusals class. It adds a class and an action mapping for the same root cause.
  • Does the existing total refusal limit stay?: Yes. The total limit of 30 stays. The consecutive limit is an additional early stop. The total limit still catches a model that alternates refused commands with accepted tool calls. (best practice) [sources: docs/ROADMAP_WORKFLOW.md:252, openspec/changes/archive/2026-11-04-3271-shell-refusal-counts-no-retry/proposal.md]
    • Chosen: Keep both limits. The consecutive limit stops a tight loop; the total limit stops a slow loop.
    • Rejected: Replace the total limit with the consecutive limit. A model can alternate refusals and accepted calls and still burn tokens.
  • What tool call resets the consecutive refusal count?: Any tool call that ShellRefusal does not refuse resets the count to 0. This includes a read-only shell command and a non-shell tool call. (best practice) [sources: src/devops_cli/roadmap/implement/implement_tool_log.py, src/devops_cli/roadmap/implement/implement_shell_refusal.py]
    • Chosen: Any accepted tool call. It shows that the model made progress and ended the refusal run.
    • Rejected: Only a shell command that exits 0. The shell policy refusal is the signal, and a non-shell tool call also breaks the run.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Alternatives check of 2026-11-10: none.
Tool: none found.

Activity

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

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. The fix prefix of the title gives the type.
    • Scope: scope/roadmap. The (roadmap) scope of the title gives the scope.
    • Priority: P1-High. The step burns 8 million tokens and 77 model calls before stopping, which wastes significant compute and delays delivery.
    • Value: High. Preventing an 8-million-token loop saves cost and unblocks the implement pipeline for every affected run.
    • Effort: Low. The fix adds a consecutive-refusal counter and a threshold check inside the existing step loop in implement_step.py.
    • Related: feat(roadmap): a step stops at its budgets and records attempts #3568, which asks for a different change. Both concern the implement step stopping early, but #3568 adds a token budget while the candidate adds a consecutive-refusal count threshold as an additional stop condition.
  3. added
    priority/p1-high
    on Nov 7, 2026
  4. dan-petty commented on Nov 7, 2026

    @dan-petty
    OwnerAuthor

    This failure also caused pull request #3981 (for issue #3251) to fail.

    The implement job used deepseek-v4.1-flash-1017 on group 1. The shell policy refused commands (uv 4, git 1). The agent continued calling the refused commands until it hit the step token budget (roadmap.implement.step_token_budget=8000000) after 77 turns and spent 8,022,108 tokens.

    A circuit breaker after consecutive refusals will prevent this token waste.

  5. added
    status/ready
    and removed
    status/new
    on Nov 7, 2026
  6. devops-cli-service commented on Nov 7, 2026

    @devops-cli-service
    Contributor

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

  7. devops-cli-service commented on Nov 10, 2026

    @devops-cli-service
    Contributor

    Alternatives check: none

    Reason: The searches found no library for a consecutive-refusal circuit breaker. pydantic-ai, which the step already uses, has per-tool retry limits and usage limits, but these count total tool calls or tokens. They do not count consecutive refusals from the project's own shell policy, which returns each refusal as a tool message. The change is a small counter in the project's own ToolCallLog, plus one constant.

    Tool: none found

    Covers:

    • pydantic-ai offers general usage and tool-call caps, but only as totals

    Lacks:

    • a counter of consecutive shell-policy refusals
    • a reset of that count on an accepted tool call
    • mapping the stop to the refused_tool failure class

    Tools considered:

    Searches run:

    • pydantic-ai circuit breaker consecutive tool call failures stop agent run, results: 5
    • pydantic-ai UsageLimits tool_calls_limit retries max tool retries, results: 5

    Sources:

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

  8. added
    owner/agent
    status/in-review
    and removed
    status/ready
    on Nov 11, 2026
  9. removed
    status/in-review
    owner/agent
    on Nov 11, 2026
  10. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor
  11. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-11 17:36 UTC

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/lowpriority/p1-highscope/roadmapsource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions