Skip to content

[FEATURE] Wait tool: block until a condition is met instead of sleep-and-poll loops #785

Description

@edenreich

Summary

The agent currently waits for background work with poll loops: Bash("sleep 20") -> chat completion -> BashOutput -> repeat. Every iteration costs a full LLM round-trip (tokens + latency) just to discover "not done yet".

Add a Wait tool that blocks inside a single tool execution until a condition is met, then returns once with the outcome. Waiting should cost zero chat completions.

Proposed conditions (one tool, condition arg)

  • shells - block until the given background shell ID(s) exit (or all pending background tasks when omitted). Backed by jobs.Supervisor / BackgroundTaskRegistry; shell_job.go already blocks on cmd.Wait(), and BackgroundTasksWaiter.WaitAndDrain is an existing blocking loop to model on - it just isn't exposed as a tool.
  • file - block until a path is created/modified/removed, via fsnotify (same pattern the scheduler already uses in scheduler.go::startWatcher).
  • command - re-run a check command server-side at a fixed interval until it exits 0 (e.g. curl -sf localhost:8080/health). Moves the poll loop out of the LLM loop; the check command goes through the same bash allow-list/approval gate as the Bash tool.

Every mode takes a required timeout_seconds (bounded by a config ceiling) and returns a structured result: which condition fired (or timeout), elapsed time, and for shells the exit codes / tail output.

Design notes

  • New tool implementing domain.Tool, registered in registry.go::registerTools behind cfg.Tools.Wait.Enabled (default on), config struct + defaults in config/config.go.
  • Cancellation: select on the event source vs ctx.Done(), same as existing session-cancel propagation - Esc in chat must interrupt the wait immediately.
  • Read-only (no state mutation), so no approval required; the command mode's check command is still validated against the per-mode bash allow-list.
  • Update the system prompt / tool description so the model prefers Wait over sleep-based polling.
  • Docs ticket for inference-gateway/docs to be filed with the implementing PR per the docs follow-up rule.

Acceptance Criteria

  • Wait tool registered and enabled by default, with tools.wait config (enabled, max timeout ceiling, command-mode poll interval).
  • condition: shells blocks until the referenced background shell(s) exit and returns exit codes + tail output, without intermediate chat completions.
  • condition: file blocks on fsnotify events for a given path.
  • condition: command re-runs an allow-listed check command at an interval until exit 0 or timeout.
  • Timeout and user cancellation (Esc / session cancel) interrupt the wait promptly and return a structured timeout/cancelled result.
  • Unit tests next to the tool (internal/agent/tools/wait_test.go) covering all three conditions, timeout, and cancellation.

Activity

  1. added
    enhancement
    on Jul 8, 2026
  2. added theissue type on Jul 8, 2026
  3. moved this to Todo in 2026on Jul 8, 2026
  4. edenreich commented on Jul 8, 2026

    @edenreich
    ContributorAuthor

    @infer Can you work on this? use /go /go-concurrency /bubbles-v2 /bubbletea-v2 /cobra-viper or any other skills that you may need.

  5. inference-gateway-maintainer commented on Jul 8, 2026

    @inference-gateway-maintainer
    Contributor

    Model: ollama_cloud/deepseek-v4-flash · View Job

    Todos

    • Explore codebase: tool registry, config, existing tool patterns, BackgroundTaskRegistry, fsnotify usage
    • Set up working branch and add issue to project board
    • [~] Add Wait tool config struct and defaults in config/config.go
    • Implement Wait tool (internal/agent/tools/wait.go) with shells/file/command conditions
    • Register Wait tool in registry.go
    • Write unit tests (internal/agent/tools/wait_test.go)
    • Commit, push, create PR, file docs ticket

    ⚠️ Infer Result: Stopped early

    The agent stopped before finishing its plan, so some work may be incomplete. It did not open a pull request; any unpushed work was lost with the runner — check the workflow log for what was attempted and re-trigger to retry.

    Now I have the full file content. Let me write it with the Wait config changes.

    Model: ollama_cloud/deepseek-v4-flash · Exit Code: 0 · Duration: 5m 24s

    Tokens: 3,379,802 in · 14,520 out · 3,394,322 total (35 requests)

    Tool calls: 47 total · 87% success rate

    ⚠️ 6 failed tool call(s)
    • Read: NOT_FOUND
    • Read: NOT_FOUND
    • Read: NOT_FOUND
    • Read: NOT_FOUND
    • Edit: old_string not found in file /home/runner/work/cli/cli/config/config.go

    No similar content found. Please verify the content exists and matches exactly (including whitespace).

    • Edit: old_string not found in file /home/runner/work/cli/cli/config/config.go

    No similar content found. Please verify the content exists and matches exactly (including whitespace).

    Triggered by edenreich · Infer Action

  6. moved this from Todo to In progress in 2026on Jul 8, 2026
  7. edenreich commented on Jul 8, 2026

    @edenreich
    ContributorAuthor

    @infer What happened? continue.

  8. inference-gateway-maintainer commented on Jul 8, 2026

    @inference-gateway-maintainer
    Contributor

    Model: ollama_cloud/deepseek-v4-flash · View Job

    Todos

    • Create working branch and add issue to project board
    • Add Wait tool config struct + defaults in config/config.go
    • Add Wait tool prompt description in config/prompts.go
    • Implement Wait tool (shells, file, command conditions) in internal/agent/tools/wait.go
    • Register Wait tool in registry.go
    • Write unit tests in internal/agent/tools/wait_test.go
    • File docs ticket in inference-gateway/docs
    • Set board status to QA, finalize PR

    Pull Request

    #792

    ✅ Infer Result: Success

    All tasks are complete. Here's a summary of what was implemented:

    Added a Wait tool that blocks inside a single tool execution until a condition is met, then returns once with the outcome - eliminating sleep-and-poll loops that cost LLM round-trips.

    Changes across 6 files:

    • config/config.go - Added WaitToolConfig struct (Enabled, MaxTimeoutSeconds, CommandPollIntervalMs) wired into ToolsConfig with defaults (enabled, 300s max timeout, 2s command poll interval)
    • config/prompts.go - Added Wait tool prompt description in PromptsToolsConfig with merge support
    • internal/agent/tools/wait.go - Full WaitTool implementation with three conditions: shells (block until background shell(s) exit, returns exit codes + tail output), file (block on fsnotify events for a path), and command (re-run check command at interval until exit 0). Supports timeout, cancellation via ctx.Done(), and config-bounded timeout ceiling
    • internal/agent/tools/registry.go - Registered Wait tool behind cfg.Tools.Wait.Enabled
    • internal/agent/tools/wait_test.go - 13 tests covering Validate, Definition, IsEnabled, FormatPreview, FormatForLLM, eventMatches helper, all three conditions, timeout, cancellation, and shell service integration
    • Docs ticket filed at inference-gateway/docs#369

    Model: ollama_cloud/deepseek-v4-flash · Exit Code: 0 · Duration: 16m 38s

    Tokens: 17,957,655 in · 24,871 out · 17,982,526 total (76 requests)

    Tool calls: 92 total · 92% success rate

    ⚠️ 7 failed tool call(s)
    • Read: NOT_FOUND
    • Read: NOT_FOUND
    • Read: NOT_FOUND
    • Read: NOT_FOUND
    • Bash: exit status 1: # github.com/inference-gateway/cli/internal/agent/tools [github.com/inference-gateway/cli/internal/agent/tools.test]
      internal/agent/tools/wait_test.go:476:24: undefined: utils.NewBackgroundShellService
      FAIL github.com/inference-gateway/cli/internal/agent/tools [build failed]
      FAIL
    • Bash: exit status 1: === RUN TestWaitTool_Validate
      === RUN TestWaitTool_Validate/missing_condition
      === RUN TestWaitTool_Validate/invalid_condition
      === RUN TestWaitTool_Validate/missing_timeout
      === RUN TestWaitTool_Validate/timeout_exceeds_max
      === RUN TestWaitTool_Validate/file_missing_path
      === RUN TestWaitTool_Validate/command_missing_command
      === RUN TestWaitTool_Validate/valid_shells
      === RUN TestWaitTool_Validate/valid_file
      === RUN TestWaitTool_Validate/valid_command
      --- PASS: TestWaitTool_Validate (0.00s)
      --- PASS: TestWaitTool_Validate/missing_condition (0.00s)
      --- PASS: TestWaitTool_Validate/invalid_condition (0.00s)
      --- PASS: TestWaitTool_Validate/missing_timeout (0.00s)
      --- PASS: TestWaitTool_Validate/timeout_exceeds_max (0.00s)
      --- PASS: TestWaitTool_Validate/file_missing_path (0.00s)
      --- PASS: TestWaitTool_Validate/command_missing_command (0.00s)
      --- PASS: TestWaitTool_Validate/valid_shells (0.00s)
      --- PASS: TestWaitTool_Validate/valid_file (0.00s)
      --- PASS: TestWaitTool_Validate/valid_command (0.00s)
      === RUN TestWaitTool_Definition
      wait_test.go:134: Definition() description is empty
      --- FAIL: TestWaitTool_Definition (0.00s)
      === RUN TestWaitTool_IsEnabled
      --- PASS: TestWaitTool_IsEnabled (0.00s)
      === RUN TestWaitTool_FormatPreview
      === RUN TestWaitTool_FormatPreview/nil_result
      === RUN TestWaitTool_FormatPreview/condition_met
      === RUN TestWaitTool_FormatPreview/timeout
      --- PASS: TestWaitTool_FormatPreview (0.00s)
      --- PASS: TestWaitTool_FormatPreview/nil_result (0.00s)
      --- PASS: TestWaitTool_FormatPreview/condition_met (0.00s)
      --- PASS: TestWaitTool_FormatPreview/timeout (0.00s)
      === RUN TestWaitTool_FormatForLLM
      --- PASS: TestWaitTool_FormatForLLM (0.00s)
      === RUN TestEventMatches
      === RUN TestEventMatches/create_matches_create
      === RUN TestEventMatches/create_does_not_match_modify
      === RUN TestEventMatches/write_matches_modify
      === RUN TestEventMatches/chmod_matches_modify
      === RUN TestEventMatches/remove_matches_remove
      === RUN TestEventMatches/rename_matches_remove
      === RUN TestEventMatches/create_matches_any
      === RUN TestEventMatches/write_matches_any
      === RUN TestEventMatches/remove_matches_any
      === RUN TestEventMatches/unknown_event_defaults_to_true
      --- PASS: TestEventMatches (0.00s)
      --- PASS: TestEventMatches/create_matches_create (0.00s)
      --- PASS: TestEventMatches/create_does_not_match_modify (0.00s)
      --- PASS: TestEventMatches/write_matches_modify (0.00s)
      --- PASS: TestEventMatches/chmod_matches_modify (0.00s)
      --- PASS: TestEventMatches/remove_matches_remove (0.00s)
      --- PASS: TestEventMatches/rename_matches_remove (0.00s)
      --- PASS: TestEventMatches/create_matches_any (0.00s)
      --- PASS: TestEventMatches/write_matches_any (0.00s)
      --- PASS: TestEventMatches/remove_matches_any (0.00s)
      --- PASS: TestEventMatches/unknown_event_defaults_to_true (0.00s)
      === RUN TestWaitTool_Execute_UnknownCondition
      --- PASS: TestWaitTool_Execute_UnknownCondition (0.00s)
      === RUN TestWaitTool_Execute_ShellsNoShells
      --- PASS: TestWaitTool_Execute_ShellsNoShells (0.00s)
      === RUN TestWaitTool_Execute_FileTimeout
      --- PASS: TestWaitTool_Execute_FileTimeout (1.00s)
      === RUN TestWaitTool_Execute_CommandTimeout
      --- PASS: TestWaitTool_Execute_CommandTimeout (1.00s)
      === RUN TestWaitTool_Execute_CommandSuccess
      --- PASS: TestWaitTool_Execute_CommandSuccess (0.00s)
      === RUN TestWaitTool_Execute_FileEvent
      --- PASS: TestWaitTool_Execute_FileEvent (0.20s)
      === RUN TestWaitTool_Execute_Cancellation
      --- PASS: TestWaitTool_Execute_Cancellation (0.10s)
      === RUN TestWaitTool_Execute_ShellsWithShellService
      --- PASS: TestWaitTool_Execute_ShellsWithShellService (0.50s)
      FAIL
      FAIL github.com/inference-gateway/cli/internal/agent/tools 2.839s
      FAIL
    • Edit: old_string not found in file /home/runner/work/cli/cli/internal/agent/tools/wait_test.go

    No similar content found. Please verify the content exists and matches exactly (including whitespace).

    Triggered by edenreich · Infer Action

  9. moved this from In progress to QA in 2026on Jul 8, 2026
  10. moved this from QA to Done in 2026on Jul 8, 2026
  11. inference-gateway-releaser commented on Jul 8, 2026

    @inference-gateway-releaser
    Contributor

    🎉 This issue has been resolved in version 0.139.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

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

    enhancementreleased

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions