Skip to content

automation: app tool handlers can only answer synchronously on the graph goroutine, so a tool cannot wait for Post, LoadSound or other deferred results #2342

Backlog

Description

@houko

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.Handler is func(context.Context, json.RawMessage) (any, error); automation/automation.go:11-14 documents that it "runs on the owning scope's graph goroutine".
  • core/automationtool_on.go:52-89: CallAutomationTool runs the handler inside cx.rt.run(registered.scope, ...) (line 74) and marshals whatever it returns (line 80) as the call's result.
  • core/reactive.go:621-647: runtime.run opens a batch (rt.batch++, line 629) and flushes only after fn returns (line 645), so signal writes made inside the handler do not run effects until it returns either.
  • nsgotest/automation_apptool.go:52-79: the agent Posts the call to the graph goroutine and waits for the reply up to maxWait (one minute, nsgotest/automation.go:92).
  • docs/architecture/tool.md:57 describes 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.Post callback (and one from audio.LoadSound's done) answers through nsgo mcp without blocking the graph goroutine, and the app keeps rendering while it waits.
  • A reply that never comes ends the call with Timeout after 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.md and the public docs describe the deferred form and that a synchronous Handler must not wait for work posted back to the graph.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions