Golem parallel workers go

From golemcloud/golem · View interactive version

Golem parallel workers go is an AI agent skill from the GitHub repository golemcloud/golem. Fan out work to multiple parallel agents and collect results in a Go Golem project. Use when the user asks about parallel execution, fan-out/fan-in, spawning worker agents for parallel work, or aggregating results from multiple agents in a Go Golem project.

How to use this skill

Skill file

Parallel Workers — Fan-Out / Fan-In (Go)

Overview

A single Golem agent admits invocations sequentially. Within one invocation, goroutines can overlap I/O by interleaving at yield points; this does not promise CPU-thread parallelism. Distribute work across multiple agent instances for independent invocation execution. The Go SDK gives you three building blocks:

  1. CallAsync + Future.Get — start several cross-agent calls at once, then await each. Call / MustCall await one call at a time per goroutine; calls in separate goroutines can overlap at yield points. CallAsync returns immediately, so several calls are in flight together, and a goroutine blocked in Future.Get yields to the component-model event loop while the others proceed.
  2. Trigger + promises — fire-and-forget each worker, hand each a promise ID, then await all the promises. Best for long-running work where you don't want a call in flight the whole time.
  3. golem.ForkSelf — copy the running agent, state included, at the current point; the copy continues as a new agent, with a new agent ID and no key.

Platform contract: a single target instance handles one invocation at a time. Independent invocation execution comes from fanning out to different target instances (distinct keys or agent IDs), not from calling the same instance repeatedly. This does not prohibit goroutine concurrency within one invocation.

Approach 1: async fan-out with `CallAsync` / `Future.Get`

Start every worker call with CallAsync, collect the *golem.Future[Out] values in a slice, then Get() (or MustGet()) each. All calls are in flight while you await.

Worker definition (`agents/worker/worker.go`)

package worker

import "github.com/golemcloud/golem/sdks/go/golem"

// ID identifies a worker instance — distinct IDs run in parallel.
type ID struct{ Index int64 }

type ProcessIn struct{ Item string }

type RunReportIn struct {
    Region  string
    Promise golem.PromiseID
}

var Agent = golem.DefineAgent[ID](golem.Spec{
    Name:        "WorkerAgent",
    Description: "Processes one item",
})

var Process = Agent.Method[ProcessIn, string]("process", golem.Description("Process a single item and return the result"))
var RunReport = Agent.Method[RunReportIn, golem.Unit]("runReport")

Coordinator fan-out (`agents/coordinator/impl/impl.go`)

package impl

import (
    "myapp/agents/coordinator"
    "myapp/agents/worker"

    "github.com/golemcloud/golem/sdks/go/golem"
)

type state struct{}

var agent = coordinator.Agent.Implement(func(coordinator.ID) *state { return &state{} })

func init() {
    agent.Handle(coordinator.FanOut, func(_ *golem.Context[state], in coordinator.FanOutIn) []string {
        // 1. Start every call — each returns immediately with a future.
        futures := make([]*golem.Future[string], len(in.Items))
        for i, item := range in.Items {
            // A distinct worker instance per item → they run concurrently.
            c := worker.Agent.Upsert(worker.ID{Index: int64(i)})
            futures[i] = worker.Process.CallAsync(c, worker.ProcessIn{Item: item})
        }

        // 2. Await each. All calls were already in flight, so this fans in.
        results := make([]string, len(futures))
        for i, f := range futures {
            results[i] = f.MustGet() // MustGet() blocks (and yields) for this one result
        }
        return results
    })
}

Future.Get returns (Out, error); the error is a *golem.RemoteCallError when the worker failed or could not be reached. MustGet panics with it instead, and the panic traps the component, as in Rust. To keep going when one worker fails, use Get and collect the errors. Model expected per-item outcomes as a golem.Result in the worker method's output, then unwrap or inspect each result:

// worker.Process returns golem.Result[string, string]
res, err := futures[i].Get()     // golem.Result[string, string]
switch {
case err != nil:
    failures = append(failures, err.Error())
case res.IsErr():
    failures = append(failures, res.Err())
default:
    successes = append(successes, res.Ok())
}

Approach 2: fire-and-forget with promise collection

For long-running work, Trigger each worker (fire-and-forget, no call held open) and pass each a promise ID; the worker completes its promise when done, and the coordinator awaits them all.

Coordinator

agent.Handle(coordinator.DispatchAndCollect, func(ctx *golem.Context[state], in coordinator.RegionsIn) []string {
    // One promise per worker.
    promises := make([]*golem.Promise[string], len(in.Regions))
    for i := range in.Regions {
        promises[i] = golem.NewPromise[string]()
    }

    // Fire-and-forget: hand each worker its region and its promise id.
    for i, region := range in.Regions {
        c := worker.Agent.Upsert(worker.ID{Index: int64(i)})
        worker.RunReport.MustTrigger(c, worker.RunReportIn{Region: region, Promise: promises[i].ID()})
    }

    // Await all promises (each durably suspends until its worker completes it).
    results := make([]string, len(promises))
    for i, p := range promises {
        results[i] = p.MustAwait()
    }
    return results
})

Worker completes its promise

agent.Handle(worker.RunReport, func(_ *golem.Context[state], in worker.RunReportIn) golem.Unit {
    report := "Report for " + in.Region + ": OK"
    golem.CompletePromise(in.Promise, report)
    return golem.Unit{}
})

Trigger returns a golem.InvocationMetadata, whose InvocationID identifies the invocation (and an error if the invocation could not be enqueued). It does not wait; a failure after the invocation is accepted is not reported at the trigger site — the worker signals completion (or failure) through its promise.

Approach 3: `golem.ForkSelf`

golem.ForkSelf copies the running agent at the current point. Both copies continue from there: forked tells them apart, and forkedID is the copy's agent ID on both sides. Synchronize them with a promise:

agent.Handle(coordinator.Compute, func(ctx *golem.Context[state], _ golem.Unit) string {
	result := golem.NewPromise[string]()

	if forked, _ := golem.MustForkSelf(); forked {
		// The copy does its share of the work and hands the result over.
		golem.CompletePromise(result.ID(), "forked-result")
		return "forked done" // seen only by the copy's invocation
	}

	// The original waits for the copy.
	return "combined: original + " + result.MustAwait()
})

Fork in a loop for N-way fan-out: each iteration's copy takes slice i and returns, while the original awaits every promise. ForkSelf returns an error (a *golem.AgentOperationError) where the host refuses; MustForkSelf panics instead.

When to use which

| Criteria | CallAsync / Future | Trigger + promises |

|----------|------------------------|----------------------|

| Short calls, want results inline | ✅ Best fit | Works but heavier |

| Long-running work, don't hold a call open | ⚠️ Call stays in flight | ✅ Best fit |

| Need the invocation identity for other work | Future.Metadata (nil if the call was rejected) | Trigger returns InvocationMetadata |

| Per-item error as a value | Return golem.Result in the method | Encode outcome in the promise payload |

| Workers need the current state | Pass it as arguments | Pass it as arguments — or use golem.ForkSelf, whose copy inherits it |

Key Constraints

Related Skills

| Skill | When to Load |

|-------|--------------|

| golem-call-another-agent-go | The synchronous / async call mechanics (Call, CallAsync) |

| golem-fire-and-forget-go | Trigger details for fire-and-forget dispatch |

| golem-wait-for-external-input-go | Promises used to collect worker results |

| golem-multi-instance-agent-go | Address many worker instances by key or agent ID |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.