Repository navigation
feat(headless): add a --serve worker that runs one AG-UI run per user_message - #1410
Merged
Merged
Conversation
…_message infer headless --serve --session-id <id> stays alive, runs one agent turn per user_message read on stdin and writes each turn to stdout as one AG-UI run with threadId set to the conversation id. It takes no task and implies --format ag-ui. Messages that arrive mid-turn are queued and drained as before, and anything still queued when a turn ends starts the next one. A new interrupt stdin frame cancels the running turn, which ends with outcome cancelled. With the extension browser backend the worker routes browser tools through a stdio relay that writes browser_command lines on stdout and resolves them with browser_result lines from stdin, so it binds no port. Stdin EOF lets the queued turns finish and then shuts the worker down with the services it started.
Contributor
|
🎉 This PR is included in version 0.219.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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.
Resolves #1398
Part of #1396
Summary
The daemon needs one long-lived worker per thread instead of one
infer headlessprocess per prompt.infer headless --serve --session-id <id>stays alive, runs one agent turn peruser_messageit reads on stdin and writes each turn to stdout as one AG-UI run. It builds on the existing stdin control loop and the per-runRunEncoderfrom #1397, and it reaches the browser extension through theExtensionRequestseam from #1395 without binding a port.Changes
cmd/headlessgains--serve. The task argument becomes optional, and--serveimplies--format ag-ui.validateOptionsrejects a task with--serve, a non-AG-UI format with--serve, and a missing task without it, all before any service starts.internal/presentation/headless/serve.goholds the worker loop. After the usual setup (gateway, MCP, agents, model, session resolution, rollover), each turn moves the queued messages into the conversation and drives oneRunEncoder. That givesRUN_STARTEDwiththreadIdset to the conversation id, then exactly oneRUN_FINISHEDorRUN_ERROR. A run that fails to start still renders asRUN_STARTEDthenRUN_ERROR. Only the first run of a resumed session carries theMESSAGES_SNAPSHOT.headlessControlhandles two new stdin frames.interruptcancels the running turn, which then ends with outcomecancelled.browser_resultresolves the pending browser command with the sameid.user_messagestill lands on the message queue, so a message sent mid-turn is drained by the running turn as before. It now also wakes the serve loop, and anything still queued when a turn ends starts the next turn.awaitTurndrops the frames that arrive between turns, so the stdin reader never blocks on a channel nobody drains.browser_stdio.gois the worker's browser transport. It writes eachbrowser_commandas one stdout line and matchesbrowser_resultlines from stdin by id. Pending commands fail once stdin closes. The container's newRouteBrowserRequestsre-registers the extension browser tools over it, and the worker never callsStartExtensionBridge, so it binds no port.Runreturns and the existing deferred shutdown stops the gateway, MCP servers and containers.docs/ag-ui-output.mddocuments the serve worker frames, anddocs/commands-reference.mdplus the browser and agui READMEs cover the new flag and seam.Notes
tests/e2e/serve_test.godrives a live worker through scripted stdin underINFER_GATEWAY_MOCK=true. It asserts one run per turn with the thread id, abrowser_command/browser_resultround trip, an interrupted turn ending with outcomecancelled, that the configured extension port stays unbound, and a clean exit on stdin EOF. Unit tests cover the new frames,awaitTurn, the stdio relay and flag validation.Runwent one point over the cognitive-complexity cap, so its existing//nolintgainedgocognitinstead of splitting the function.@filereferences in auser_messagereach the model as plain text. ThethreadIdstays the session id resolved at startup, as in one-shot mode.