Skip to content

feat(agent): bound repeated failed tool calls with a recovery checkpoint (#285) - #295

Merged
AetherAI3 merged 1 commit into
mainfrom
feat/issue-285-failure-budget
Oct 6, 2026
Merged

AetherAI3 merged 1 commit into
mainfrom
feat/issue-285-failure-budget

Conversation

@AetherAI3

Copy link
Copy Markdown
Owner

Closes #285.

A model stuck re-requesting the same invalid, denied or unavailable tool call used to burn its whole turn budget: every refused result reset the progress deadline, and fresh call ids made each repeat look new. The host-executed loops (hostLoop for aether agent, runLocalTurn for local chat) now share a per-turn repeated-failure budget that classifies failures, recognizes a repeat of the same operation under unchanged state, warns the model once, and then stops the cycle with a checkpoint that says what happened and how to recover.

What changed

  • src/core/tool_failure_budget.ts (new)
    • Seven failure classes: invalid arguments, permission refused, unavailable, stale precondition, transient, unknown outcome, execution failure. Cancellation is never counted.
    • Canonical fingerprint: a sha256 of the approval binding.
    • State binding: host decisions (policy, validation, operator denial) are keyed on the operation alone. Tool failures are keyed to the git tree revision, the file stat, the shell context, the test command and an edit epoch, comparing each attempt's pre-state with the previous post-state.
  • Refuse-then-stop, before executing or prompting. Once a class budget is spent (1–3 failed attempts per class), the next identical request gets a refusal that includes the recovery step. A request in a later model reply stops the cycle. Calls batched into one reply never stop a run before the model has seen a result; OllamaBrain reports reply boundaries through the new optional Brain.modelRound(). An 8-failure streak backstop covers the varied-call case and warns from the 6th failure.
  • Stop path
    • The stopped call still gets one result under its own id.
    • The brain is closed synchronously.
    • done{ok:false, reason:"no-progress"} flows through the normal event path.
    • The coding turn settles incomplete. A green host check still runs over the preserved work but cannot turn the stop into a success, and the session is filed no-progress.
    • --json adds a checkpoint record: operation, class and reason, attempts, preserved work, one recovery choice. A refusal never suggests a broader permission mode.
  • Unchanged behaviour
    • Successful calls are never deduplicated.
    • A read-only transient failure is retried once by the host.
    • A timed-out mutation is never replayed.
    • Turn, time, output and progress caps are untouched.
  • Git probe: one bounded (3 s), lock-free status --porcelain=v2 --branch, run only for failed shell/git calls. A timeout or error reads as "changed" and never as a stop.
  • Docs: contract added in docs/CONTRACTS.md → "Repeated tool-failure budget ([feature] Stop repeated failed tool calls and explain how to recover #285)".

Acceptance evidence (test/tool_failure_budget.test.ts, test/tool_failure_budget_local.test.ts: 50 tests, real hostLoop / runLocalTurn / OllamaBrain / ToolExecutor; assertions on execution counts, prompt counts and per-id delivery)

Acceptance item Proven by
Fake brain repeating an invalid / denied action reaches a bounded stop without repeated execution or approval prompts invalid call: 0 executions, 0 prompts, stop on request 4. Denied call: exactly 1 prompt, 0 executions. Real OllamaBrain stops in 4 model turns (cap is 24), on both paths
Checkpoint states operation, reason, attempt count, preserved work, one recovery choice checkpoint unit test plus --json record test
Corrected argument, refreshed capability, changed file revision or edited repo allows progress corrected path proceeds. Changed shell session lets the identical call run. External edit between stale patches lets the corrected digest apply. Operator edit in a git repo resets the count
Transient failures get bounded classified retries; unknown mutation outcomes are not replayed host retries a read conflict once under the same id. A timed-out run_shell runs once per model request, then refuse → stop
Local and coding tests cover invalid args, permission denial, stale patches, failed tests after edits, cancellation each case present on both hostLoop and runLocalTurn

Extra coverage

  • 3–4 identical calls batched in one reply are answered, not stopped.
  • 9-call failing batch is stopped only in the next reply.
  • An unrelated edit after a denial does not re-prompt.
  • A self-rewriting failing command is still a repeat.
  • ls alternated with a red test is still bounded; npm install resets the count.
  • Classification is checked against real executor output, including fix: guard write_file replacements with complete-read proof #294 write_file proof errors.
  • The git probe never runs for successes.

Verification

  • Full suite on the rebased head: 3101 tests, 3060 pass, 0 fail, 41 skipped (all 50 new tests present and green).
  • Baseline at origin/main before the change: 3037 tests, 2 failures in the win32 Job Object kill tests (pid-reuse-blocked). These are environmental and passed on every later run.
  • Mutation testing: 9 mutants, all killed by the new tests. The mutants cover budget off, no pre-state, decisions state-bound, no refuse step, no round gating (same-op and streak), every shell success bumping, unmeasured-as-constant, and no host transient retry. Disabling the budget turns 19 of the new tests red.
  • Two independent review passes (TypeScript reviewer). All HIGH/MEDIUM findings were addressed before this PR.

Out of scope (documented in the contract)

  • Hosted server-side tool enforcement.
  • The headless aether exec loop and smoke loop.
  • Web tools, which report failure with exit 0.
  • For remote and child-process brains, delivery of the final stop result is best-effort, because the stop tears their session down.

🤖 Generated with Claude Code

…int (#285)

Every refused or failed tool result reset the host progress deadline, so a
model could spend its whole turn budget re-requesting the same invalid call,
denied command or unavailable operation under fresh call ids.

The host-executed loops (hostLoop for `aether agent`, runLocalTurn for local
chat) now share a per-turn ToolFailureBudget (src/core/tool_failure_budget.ts):

- Classify failures: invalid_arguments, permission_refused, unavailable,
  stale_precondition, transient, unknown_outcome, execution_failure.
  Cancellation is never counted.
- Fingerprint the canonical operation (sha256 of the approval binding / raw
  args / policy tool name). Host decisions (policy, validation, operator
  denial) are bound to the operation alone; tool-produced failures are bound
  to relevant state (git working-tree revision, target file stat, shell
  context, configured test command, edit epoch), comparing each attempt's
  pre-state with the previous post-state.
- Per-class budget, then refuse-then-stop BEFORE executing or prompting: the
  first spent repeat is answered with a refusal that spells out recovery; a
  request in a later model reply stops the cycle. Calls batched into one
  reply (Brain.modelRound, implemented by OllamaBrain) never stop a run before
  the model has seen a result. An 8-failure streak backstop warns first.
- A stop delivers one result under the call's own id, closes the brain,
  emits done{ok:false, reason:"no-progress"}, and settles the coding turn
  incomplete (a green check cannot turn a stop into success; session filed
  no-progress). The checkpoint names operation, reason, attempts, preserved
  work and one recovery choice; --json gains a `checkpoint` object.
- Successful calls are never deduplicated; read-only transient failures are
  retried once by the host; unknown mutation outcomes are never replayed.
- The git probe is one bounded, lock-free `status --porcelain=v2 --branch`,
  run only for failed shell/git calls; unmeasurable state reads as changed.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@AetherAI3 AetherAI3 closed this Oct 6, 2026
@AetherAI3 AetherAI3 reopened this Oct 6, 2026
@AetherAI3
AetherAI3 merged commit 8751117 into main Oct 6, 2026
4 of 16 checks passed
@AetherAI3
AetherAI3 deleted the feat/issue-285-failure-budget branch October 6, 2026 13:59
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.

[feature] Stop repeated failed tool calls and explain how to recover

1 participant