Repository navigation
Description
Context
An app tool registered with automation.Register (#1285) can only answer synchronously, from inside its handler, while that handler holds the graph goroutine (main at a34e7e7f):
core/automationtool.go:9-14:AutomationTool.Handlerisfunc(context.Context, json.RawMessage) (any, error);automation/automation.go:11-14documents that it "runs on the owning scope's graph goroutine".core/automationtool_on.go:52-89:CallAutomationToolruns the handler insidecx.rt.run(registered.scope, ...)(line 74) and marshals whatever it returns (line 80) as the call's result.core/reactive.go:621-647:runtime.runopens a batch (rt.batch++, line 629) and flushes only afterfnreturns (line 645), so signal writes made inside the handler do not run effects until it returns either.nsgotest/automation_apptool.go:52-79: the agentPosts the call to the graph goroutine and waits for the reply up tomaxWait(one minute,nsgotest/automation.go:92).docs/architecture/tool.md:57describes the same contract: the handler "returns a JSON-encodable value or an error", with no form for a result that arrives later.
Much of NSGo's own API delivers its answer later on the graph goroutine: Scope.Post (core/reactive.go:465) runs fn "later on the graph's goroutine", audio.LoadSound (audio/audio.go:82) and audio.OutputLatency call done back there, and so do timers (AfterFunc) and any work an app finishes on another goroutine and hands back with Post. A handler that needs one of those results before answering has no way to wait for it: the callback is queued behind the handler itself. If it blocks on a channel it freezes the app's UI and loop for the full minute and then fails with Timeout; if it blocks without watching ctx, the graph goroutine never comes back.
Steps to see it: register a tool whose handler calls cx.Post(func() { ch <- 1 }) (or audio.LoadSound(cx, src, func(s *audio.Sound, err error) { ch <- ... })) and then selects on ch and ctx.Done(); call it through nsgo mcp on an nsgo dev -automation build. The app stops rendering, the call returns Timeout after a minute, and the Posted function runs only after the handler has given up.
Why it matters
A game layer built on NSGo (nsgo-dev/game) exposes MCP tools such as "load this sound and report its duration", "advance the loop N frames and return the resulting state", "load a scene and return its entity tree" or "take a screenshot after the next frame". Each needs work that completes on a later turn of the graph goroutine, so today those tools cannot be written as one call: the game has to split them into a "start" tool and a "poll" tool and keep per-request state, and an agent has to poll. The same applies to any app tool that waits for a network request, a file picker's result, an animation's end or a frame to be committed.
Proposal
Let a handler answer later, without holding the graph goroutine. For example, an alternative field beside Handler:
// Start runs on the owning scope's graph goroutine and returns at once. It calls
// reply exactly once, from any goroutine, with a JSON-encodable result or an error.
Start func(ctx context.Context, args json.RawMessage, reply func(any, error))RegisterAutomationTool requires exactly one of Handler and Start. CallAutomationTool (or a new variant the agent uses) returns once Start has returned, and the agent's existing wait (nsgotest/automation_apptool.go:63) waits for reply instead of the posted function, still bounded by ctx and maxWait; a reply after the deadline, a second reply, or the registering scope's cleanup before a reply is ignored or reported as an error. Handler becomes the case of Start that replies before returning, so existing tools are unchanged.
automation.Call for tests would need a form that does not wait on the graph goroutine, such as returning a channel or taking a callback, or an nsgotest.App helper that keeps driving the graph until the reply arrives.
Acceptance
- A tool whose result comes from a
Scope.Postcallback (and one fromaudio.LoadSound'sdone) answers throughnsgo mcpwithout blocking the graph goroutine, and the app keeps rendering while it waits. - A reply that never comes ends the call with
Timeoutafter the existing limit, and the app keeps running. - Cleaning up the registering scope before the reply ends the pending call with an error.
- Headless and target tests can call such a tool and get its result.
-
automation/automation.go,docs/architecture/tool.mdand the public docs describe the deferred form and that a synchronousHandlermust not wait for work posted back to the graph.
Activity