ToolCall.Timeout behaviour (ToolCall v0.4.0)

Copy Markdown

Per-invocation timeouts and the timeout policy behaviour.

run/3 executes a zero-arity function in a task for a finite timeout and answers {:error, {:timeout, ms}} when it does not finish in time. With :infinity it runs in the caller unless a :task_supervisor is given. The task is killed with :brutal_kill; it is not a cancellation: the function may already have produced its side effect. A tool that owns resources or relies on the caller's process (self/0, the process dictionary, Ecto sandbox ownership) should use a cooperative timeout instead.

A module implementing timeout/3 can be passed to use ToolCall, timeout: MyPolicy; ToolCall.run/4 with timeout: ms wins over the policy. timeout/3 receives the tool IR, the validated raw argument map and the caller's context. It runs when the call executes, after approval, not during resolve/4; its own execution is not covered by the timeout it computes.

Summary

Functions

Runs fun with a timeout.

Validates a timeout value, raising ArgumentError when it is not :infinity or a non-negative integer.

Callbacks

timeout(t, map, term)

@callback timeout(ToolCall.IR.Tool.t(), map(), term()) :: timeout()

Functions

run(fun, timeout, opts)

@spec run((-> term()), timeout(), keyword()) :: term()

Runs fun with a timeout.

Options:

  • :task_supervisor - a Task.Supervisor name. With a finite timeout the task is started under the supervisor; with :infinity a supervisor still starts a task. Without a supervisor, a finite timeout uses a private Task.Supervisor and a task unlinked from the caller; :infinity runs in the caller. An unreachable or invalid supervisor answers {:error, {:task_supervisor, reason}} instead of exiting the caller.

run_tagged(fun, timeout, opts)

@spec run_tagged((-> term()), timeout(), keyword()) ::
  {:ok, term()} | {:timeout, non_neg_integer()} | {:error, term()}

Internal timer used by ToolCall.Runner.execute/2.

Like run/3, but distinguishes a dispatcher timeout from a function result.

validate!(timeout)

@spec validate!(term()) :: timeout()

Validates a timeout value, raising ArgumentError when it is not :infinity or a non-negative integer.