Skip to content

feat(headless): add a --serve worker that runs one AG-UI run per user_message - #1410

Merged
edenreich merged 1 commit into
mainfrom
feat/issue-1398
Sep 29, 2026
Merged

edenreich merged 1 commit into
mainfrom
feat/issue-1398

Conversation

@edenreich

Copy link
Copy Markdown
Contributor

Resolves #1398
Part of #1396

Summary

The daemon needs one long-lived worker per thread instead of one infer headless process per prompt. infer headless --serve --session-id <id> stays alive, runs one agent turn per user_message it 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-run RunEncoder from #1397, and it reaches the browser extension through the ExtensionRequest seam from #1395 without binding a port.

Changes

  • cmd/headless gains --serve. The task argument becomes optional, and --serve implies --format ag-ui. validateOptions rejects 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.go holds 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 one RunEncoder. That gives RUN_STARTED with threadId set to the conversation id, then exactly one RUN_FINISHED or RUN_ERROR. A run that fails to start still renders as RUN_STARTED then RUN_ERROR. Only the first run of a resumed session carries the MESSAGES_SNAPSHOT.
  • headlessControl handles two new stdin frames. interrupt cancels the running turn, which then ends with outcome cancelled. browser_result resolves the pending browser command with the same id. user_message still 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. awaitTurn drops the frames that arrive between turns, so the stdin reader never blocks on a channel nobody drains.
  • browser_stdio.go is the worker's browser transport. It writes each browser_command as one stdout line and matches browser_result lines from stdin by id. Pending commands fail once stdin closes. The container's new RouteBrowserRequests re-registers the extension browser tools over it, and the worker never calls StartExtensionBridge, so it binds no port.
  • Stdin EOF lets the running turn and any queued messages finish, then Run returns and the existing deferred shutdown stops the gateway, MCP servers and containers.
  • docs/ag-ui-output.md documents the serve worker frames, and docs/commands-reference.md plus the browser and agui READMEs cover the new flag and seam.

Notes

  • tests/e2e/serve_test.go drives a live worker through scripted stdin under INFER_GATEWAY_MOCK=true. It asserts one run per turn with the thread id, a browser_command / browser_result round trip, an interrupted turn ending with outcome cancelled, 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.
  • Run went one point over the cognitive-complexity cap, so its existing //nolint gained gocognit instead of splitting the function.
  • Slash commands and @file references in a user_message reach the model as plain text. The threadId stays the session id resolved at startup, as in one-shot mode.

…_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.
@edenreich
edenreich requested a review from a team as a code owner September 29, 2026 10:30
@edenreich
edenreich merged commit fda79d0 into main Sep 29, 2026
12 checks passed
@edenreich
edenreich deleted the feat/issue-1398 branch September 29, 2026 10:44
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.219.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 Sep 30, 2026
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.

[FEATURE] Long-lived headless session worker that runs one AG-UI run per user_message

1 participant