Skip to main content

INVOKE BEHAVIORS FROM BEHAVIORS

This guide shows you how to use context.runModel() to invoke one behavior's method from inside another behavior's execute function. runModel is the TypeScript API name; there is no behavior-spelled alias.

Declare the dependency

Your extension's manifest must list the target behavior's extension in dependencies. Without this declaration, runModel rejects the call at runtime.

# manifest.yaml
name: "@mycollective/orchestrator"
version: "2026.07.10.1"
dependencies:
  - "@mycollective/worker-behavior"
behaviors:
  - orchestrator.ts

Invoke by definition name

If the target behavior already has a definition in the repository, pass its name as definition:

const result = await context.runModel!({
  definition: "my-worker",
  method: "process",
  arguments: { input: "some-value" },
});

if (result.ok) {
  context.logger.info(`Worker produced ${result.resources.length} resource(s)`);
} else {
  context.logger.error(`Worker failed: ${result.error.message}`);
}

Invoke by behavior type

If no definition exists, pass the behavior type as modelType and a definition name. Swamp auto-creates the definition in .swamp/auto-definitions/:

const result = await context.runModel!({
  modelType: "@mycollective/worker-behavior",
  name: "us-east-worker",
  method: "execute",
  arguments: { region: "us-east-1" },
});

Handle the result

runModel returns a discriminated union and does not throw. Check result.ok before accessing result.resources:

const result = await context.runModel!({
  definition: "target",
  method: "method",
  arguments: args,
});

if (!result.ok) {
  context.logger.error(result.error.message);
  return { dataHandles: [] };
}

// result.resources is DataHandle[] — references to the target's output
const records = await context.readModelData!("target");
for (const handle of result.resources) {
  const record = records.find((r) => r.name === handle.name);
  // process record?.attributes
}

Data ownership

Data written by the invoked behavior belongs to the target behavior's definition, not the caller's. The DataHandle references in result.resources point to data under the target's namespace, so context.readResource cannot read them. To use that data in the caller's output, read it with context.readModelData and write a derived resource under the caller's own spec.

Locking

A runModel call takes its target behavior's lock before the target's method runs, the same as a workflow step or a second swamp behavior run does:

  • The call waits for any other run on the target behavior, so the two never overlap and neither update is lost. The target's writes are pushed under that lock, which is released before your method continues.
  • Calling runModel on your own behavior, or on a behavior that a run further up the call chain holds, uses the lock already held and does not wait.
  • Two behaviors whose methods call each other through runModel while both are running wait on each other's lock. One of the calls fails at the lock timeout (SWAMP_LOCK_TIMEOUT_MS, default 60 seconds) and returns { ok: false }. Avoid such cycles, or call in one direction only.

runModel still cannot run on a remote worker; see Remote workers.

Limits

A single top-level method execution enforces:

  • Maximum call depth of 10
  • Maximum 100 total runModel invocations
  • Cycle detection — a call cannot invoke a behavior type and method that are already in its call chain. Calling a different method on the same behavior is allowed
  • Vault isolation — the invoked behavior sees only its own extension's vault bindings

Exceeding any limit causes runModel to return { ok: false, error: { message } }.

Remote workers

runModel is not available on remote workers. On a worker, the call returns { ok: false } with an error message instead of running the target. If your behavior's method may run on a worker, move the invocation to a local orchestrator behavior, or chain the behaviors as steps in a workflow.

Refer to the behavior reference for the full runModel signature, and Behaviors, Types, and Methods for the design rationale.