Skip to content

fix(agent): execute approved tools concurrently and freeze tickers on finish - #609

Merged
edenreich merged 5 commits into
mainfrom
fix/overlap-tool-approval-execution
Jun 6, 2026
Merged

edenreich merged 5 commits into
mainfrom
fix/overlap-tool-approval-execution

Conversation

@edenreich

@edenreich edenreich commented Jun 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR fixes the overlap between tool approval and execution - previously, tools only started executing after all approvals were collected, wasting time. Now each approved tool runs immediately as the user approves it, and tools run concurrently.

Changes

  1. fix(agent): execute approved tools concurrently while approving the rest (d5598e2)

    • Approval prompts stay sequential, but each approved tool"'s execution is spawned immediately
    • Tools run in parallel (bounded by agent.max_concurrent_tools)
    • Results are recorded in index-keyed slots and flushed to the conversation in tool-call order
  2. fix(agent): freeze per-tool ticker when an approved tool finishes (5caa035)

    • executeToolInternal publishes its own terminal completed/failed status via a defer
    • The per-tool elapsed-time ticker freezes the moment the tool finishes
    • Removes redundant external publishes from executeToolCallsParallel to avoid double-publish flickering
  3. fix(tui): refresh conversation history when a tool finishes executing (0906249)

    • Tool coordinator now emits an UpdateHistoryEvent on completed/failed tool execution
    • Freshly flushed tool results replace their "Approved" placeholder immediately
  4. refactor: inject agent config directly into EventDrivenAgent (44986f0)

    • Clean up how config is passed to the agent

edenreich added 4 commits June 6, 2026 03:21
Approval prompts stay sequential, but each approved tool's execution is now spawned immediately and the loop moves on to the next prompt, so tools genuinely run concurrently while the rest are being approved. Results are recorded in index-keyed slots and flushed to the conversation in tool-call order as each contiguous prefix completes, so each result appears as the user approves the next one. Bounded by agent.max_concurrent_tools.
executeToolInternal now publishes its own terminal completed/failed status (via a defer), so the approval path's per-tool elapsed-time ticker freezes the moment that tool finishes instead of counting up until the whole batch is approved. The now-redundant external publishes in executeToolCallsParallel are removed to avoid a double-publish flicker; result images are still forwarded.
On a completed/failed tool-execution progress event, the tool coordinator now also emits an UpdateHistoryEvent. This lets a freshly-flushed tool result replace its 'Approved' placeholder as soon as the tool finishes, instead of waiting for the next approval to trigger a refresh - which matters now that approved tools execute concurrently and can finish well before the user approves the next one.
@edenreich
edenreich requested a review from a team as a code owner June 6, 2026 01:34
Comment thread internal/agent/agent_test.go Resolved
@edenreich
edenreich merged commit a713306 into main Jun 6, 2026
@edenreich
edenreich deleted the fix/overlap-tool-approval-execution branch June 6, 2026 01:39
edenreich added a commit that referenced this pull request Jun 6, 2026
… finish (#609)

## Summary

This PR fixes the overlap between tool approval and execution -
previously, tools only started executing *after* all approvals were
collected, wasting time. Now each approved tool runs immediately as the
user approves it, and tools run concurrently.

### Changes

1. **fix(agent): execute approved tools concurrently while approving the
rest** (d5598e2)
- Approval prompts stay sequential, but each approved tool"'s execution
is spawned immediately
   - Tools run in parallel (bounded by `agent.max_concurrent_tools`)
- Results are recorded in index-keyed slots and flushed to the
conversation in tool-call order

2. **fix(agent): freeze per-tool ticker when an approved tool finishes**
(5caa035)
- `executeToolInternal` publishes its own terminal completed/failed
status via a `defer`
- The per-tool elapsed-time ticker freezes the moment the tool finishes
- Removes redundant external publishes from `executeToolCallsParallel`
to avoid double-publish flickering

3. **fix(tui): refresh conversation history when a tool finishes
executing** (0906249)
- Tool coordinator now emits an `UpdateHistoryEvent` on completed/failed
tool execution
- Freshly flushed tool results replace their "Approved" placeholder
immediately

4. **refactor: inject agent config directly into EventDrivenAgent**
(44986f0)
   - Clean up how config is passed to the agent
inference-gateway-releaser Bot pushed a commit that referenced this pull request Jun 6, 2026
## [0.119.0](v0.118.0...v0.119.0) (2026-06-06)

### 🚀 Features

* add npm wrapper to install and run the CLI via npx ([#610](#610)) ([8f24fd5](8f24fd5)), closes [inference-gateway/docs#182](inference-gateway/docs#182)
* **tools:** indentation-tolerant matching and stale-read detection for Edit/MultiEdit ([#608](#608)) ([14d7e98](14d7e98))

### 🐛 Bug Fixes

* **agent:** execute approved tools concurrently and freeze tickers on finish ([#609](#609)) ([a713306](a713306))

### 💄 Styles

* replace em dashes with hyphens ([91a3746](91a3746))

### 🧹 Maintenance

* **deps:** bump codex 0.133.0 -> 0.135.0 ([#607](#607)) ([86af5fb](86af5fb))
* enable skills by default ([9440593](9440593))
* regenerate config ([991cd77](991cd77))
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.119.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Jun 6, 2026
edenreich added a commit that referenced this pull request Sep 23, 2026
…ls (#1291)

Fixes #1290.

## Root cause

Tool calls from one assistant turn ran fully concurrently in both
schedulers: `ApprovingToolsState.spawnExecution` (approval path,
including auto-accept) and `Agent.executeToolCallsParallel` (no-approval
path, e.g. `--mode auto`). A `Read` issued after a `Write` in the same
batch could run before the `Write` finished and return `NOT_FOUND`.

## Fix

New `states.CallOrder` (`internal/agent/states/call_order.go`), used by
both schedulers:

- Read-only calls still run together.
- Any other call waits for every earlier call in the batch, and later
calls wait for it.
- Calls wait before taking a concurrency slot, so a waiting call never
holds one.
- Approval prompts still overlap execution (#609). Only execution order
is enforced.

The read-only set is the existing ReadOnly subagent toolset, moved to
`agentdomain.ReadOnlyTools` and reused by `ListToolsForMode`.

Trade-off: separate `Bash` or `Agent` calls in one batch now run in
sequence. Parallel subagents still run together via the `Agent` tool's
`tasks` array.

## Tests

- Unit: `TestApprovingToolsState_ReadWaitsForEarlierWrite`, an approved
`Write` followed by an ungated `Read`.
- E2E: `TestAgentReadAfterWriteInSameBatchSeesWrite`, headless `--mode
auto` against the tokenless mock; the `Read` result must contain what
the `Write` wrote.
- Both fail on `main` (the e2e test failed 3 out of 3 runs) and pass
with the fix. `internal/agent/...` passes under `-race`, the full e2e
suite passes, and lint reports 0 issues.
edenreich added a commit that referenced this pull request Nov 6, 2026
)

Part of #1713. Stacked on #1725.

## Summary

The agent now asks for every approval in a tool batch before it runs any
of the calls. Before, an approved call started at once, while the user
was still deciding on the next one, and its progress and result were
reported behind the open prompt.

That made the views disagree. The chat TUI reading the agent directly
showed the first tool's result while the second approval was open. Any
client of the AG-UI stream did not: an approval suspends the run,
`RunEncoder` waits for the answer, and whatever the running tool
reported in the meantime arrived only after it. It is the gap that kept
#1725 a draft.

## Changes

- `internal/agent/loop/states/approving_tools.go`: a call the user or
the judge approved, or one the policy does not gate, is recorded as
cleared instead of spawned. `finishApprovals` runs the cleared calls
once the round is decided. They still overlap, bounded by the same
semaphore, and their results are still flushed in tool-call order as
they complete.
- A cancelled session runs none of the cleared calls. The conversation
validator already answers calls left without a result.
- An approval that fails ends the turn before anything ran. Before,
calls approved earlier in the round were already executing.
- A round that ends early drops the approval entries of the calls it
will not run. Only running a call clears its entry, so the chat kept
showing a call as approved although it never ran.
- The status says `Tool approved: <tool>` instead of claiming the tool
is executing while the next prompt is open.

## Behavior change

This applies to every user and reverses behavior added on purpose: #609
made approved tools run concurrently while later prompts were open, and
#1289 asserted the first result is visible while the second approval is
open.

- With a gated call in a batch, no call of that batch runs until the
last prompt is answered. That includes ungated calls such as `Read` or
`TodoWrite` batched with a gated one, and a background job.
- The cost: a long first tool does not start until the user has answered
every prompt.
- Rejecting a later call leaves an earlier approval standing, so that
call still runs.
- A single gated call, auto-accept mode, and fully ungated batches
behave as before.

## What this does not change

Tools can still open a prompt during execution: sandbox access,
`RequestApproval` escalations and `AskUserQuestion`. A read-only sibling
can overlap such a prompt, so on the AG-UI stream its reports still wait
for the answer. This PR only covers the prompts of the approval round.

## Verification

- New `TestApprovingToolsState_RunsNothingWhileAPromptIsOpen` on the
fake clock, for an approved call and an ungated one. It fails on the old
behavior.
- New `TestApprovingToolsState_ForgetsApprovedCallsItWillNotRun`, for a
failed approval and a cancelled session. It fails without the fix.
- New `TestChatTUIBackgroundJobWaitsForItsBatch` fails on `main` with
`the job started while the second approval was open`, and passes here on
both paths.
- New `TestChatTUIRejectingACallStillRunsTheApprovedOne` passes on
`main` and here.
- `TestChatTUIApprovalBoxFollowsTail` now asserts the first Write has
not run while the second approval is open, and shows once both are
answered.
- The stale approval entry and the false status were reproduced by hand
in tmux with `INFER_AGENT_MAX_DURATION_MINUTES=1`, and are gone with the
fix.
- `go test -race ./...`, `task precommit:run`, and the full e2e suite
with `-race` on both paths.

## Seen while testing, not changed here

- After a run ended by its time budget, the next approval box stays
open.
- The `n` and `s` plan keys in `config/keybindings.go` do nothing on
`main` either. The screen offers arrows and Enter.

## Downstream

- Clients of the AG-UI stream need no change. They already saw nothing
between an interrupt and its answer.
- Docs follow-up: inference-gateway/docs#1110
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant