Repository navigation
feat(infinity-agent-core): dispatch tool calls eagerly, before the model stream ends - #191
Draft
shadaj wants to merge 1 commit into
Conversation
…del stream ends Previously a step collected the model's `ExecuteToolCall` action and only dispatched it after the completion stream had fully ended (`Final` chunk received), then `sync()`ed, then fired the tool. The tool's latency was therefore added *after* the model's tail instead of overlapping it. ## `run_completion` (`event_processor.rs`) * Before yielding `CompletionAction::ExecuteToolCall`, the stream now explicitly `mark_inputs_model_validated()`s and `history.sync().await?`s (the same pattern the synchronous-tool path already used). A yielded tool call is therefore **already durable** and may be dispatched the moment it is received — the low-level API's "sync before dispatch" contract is now satisfied by construction rather than left to callers. * Doc comments on `CompletionAction`, `CompletionEvent::Action` and `execute_action` updated to describe the new contract: `ExecuteToolCall` is not a terminal event, it is yielded as soon as the call is complete. ## `Thread::step_no_defer` (`system/thread.rs`) * The "collect one action, dispatch after the stream" flow is replaced by a `tokio::select!` loop over the completion stream and a `FuturesUnordered` of in-flight dispatches. When the stream yields an `ExecuteToolCall`, the dispatch (`execute_action_with_error_result`) is pushed into the set and runs concurrently while the rest of the model stream (the `Final` chunk carrying usage, any ignored concurrent tool calls) is drained. * The stream *is* the queue of actions: a `FuturesUnordered` rather than a single `Option` slot means that once `run_completion` starts yielding several calls per turn (parallel tool calls), they will all dispatch eagerly with no further changes to the invoker. * All dispatches are drained before the step returns, so a step never leaves a dispatch in flight (matching the old guarantee that the step future completes only after the tool has been fired), and the post-stream `sync()` + `CompletionFinished` ordering is unchanged. * `handle_completion_event` now returns `Option<CompletionAction>` instead of writing into an `&mut Option` with a "multiple terminal actions" assertion. ## Tests * New `tool_call_dispatches_before_stream_finishes`: emits a tool call from the mock model *without* finishing the stream and asserts that the tool's `execute` runs, that the call is already in the conversation store at that point, and that no `CompletionFinished` has been emitted yet. Verified to fail against the old dispatch-after-stream behaviour. ## Docs * `low-level/completion-loop.md`, `low-level/history-manager.md`, `architecture.md`: describe eager dispatch and that `run_completion` syncs before yielding `ExecuteToolCall`. `./check.bash` (fmt, clippy, all tests, doctests) passes. Co-authored-by: Infinity 🤖 <infinity@hydro.run>
Deploying infinity with
|
| Latest commit: |
12db1f4
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://aec168cb.infinity-dc7.pages.dev |
| Branch Preview URL: | https://sandbox-28c32af1-38b5-40da-b.infinity-dc7.pages.dev |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously a step collected the model's
ExecuteToolCallaction and onlydispatched it after the completion stream had fully ended (
Finalchunkreceived), then
sync()ed, then fired the tool. The tool's latency wastherefore added after the model's tail instead of overlapping it.
run_completion(event_processor.rs)CompletionAction::ExecuteToolCall, the stream nowexplicitly
mark_inputs_model_validated()s andhistory.sync().await?s(the same pattern the synchronous-tool path already used). A yielded
tool call is therefore already durable and may be dispatched the
moment it is received — the low-level API's "sync before dispatch"
contract is now satisfied by construction rather than left to callers.
CompletionAction,CompletionEvent::Actionandexecute_actionupdated to describe the new contract:ExecuteToolCallis not a terminal event, it is yielded as soon as the call is complete.
Thread::step_no_defer(system/thread.rs)tokio::select!loop over the completion stream and aFuturesUnorderedof in-flight dispatches. When the stream yields an
ExecuteToolCall, thedispatch (
execute_action_with_error_result) is pushed into the set andruns concurrently while the rest of the model stream (the
Finalchunkcarrying usage, any ignored concurrent tool calls) is drained.
FuturesUnorderedrather than asingle
Optionslot means that oncerun_completionstarts yieldingseveral calls per turn (parallel tool calls), they will all dispatch
eagerly with no further changes to the invoker.
leaves a dispatch in flight (matching the old guarantee that the step
future completes only after the tool has been fired), and the post-stream
sync()+CompletionFinishedordering is unchanged.handle_completion_eventnow returnsOption<CompletionAction>insteadof writing into an
&mut Optionwith a "multiple terminal actions"assertion.
Tests
tool_call_dispatches_before_stream_finishes: emits a tool call fromthe mock model without finishing the stream and asserts that the tool's
executeruns, that the call is already in the conversation store at thatpoint, and that no
CompletionFinishedhas been emitted yet. Verified tofail against the old dispatch-after-stream behaviour.
Docs
low-level/completion-loop.md,low-level/history-manager.md,architecture.md: describe eager dispatch and thatrun_completionsyncsbefore yielding
ExecuteToolCall../check.bash(fmt, clippy, all tests, doctests) passes.