Runs & threads
A run is what you start, list, and open in a debugger. Inside it, threads form a tree: a root agent or workflow, plus any children it spawned. Agents, DAG workflows, and custom kinds share the same model; nesting is normal.
Run#
The durability boundary and event-stream container for one logical operation. All child threads, signals, and events belong to one canonical log identified by runId. Crash recovery means replaying that log.
Thread#
The unit of computation inside a run. A thread owns a state machine, may have a parent, and has a thread kind: built-in agent / workflow, or a custom kind from a module.
Definitions live on modules (defineAgent, defineWorkflow, or your own). You start work with looms.start(definition, input); the runtime creates the root thread and begins appending events.
Custom thread kinds#
A custom kind is a module that registers a thread definition and implements m.thread. Stamp the startable definition with createKind (the same shape defineAgent and defineWorkflow already produce). The definition is what you pass to start; the thread is the state machine that handles events for that kind:
import { z } from 'zod'import { createKind, defineEventCatalog, defineModule } from '@swirls/looms/core'const AuctionInput = z.object({ item: z.string(), reservePrice: z.number(),})const auctionCatalog = defineEventCatalog('auction', { bid: z.object({ bidder: z.string(), amount: z.number() }), close: z.object({ reason: z.string().optional() }),})const auction = createKind('auction')const vintageWatch = auction.define({ name: 'vintage-watch', input: AuctionInput,})const auctionModule = defineModule( { namespace: 'auction', protocolVersion: '1.0.0', events: auctionCatalog }, (m) => ({ definitions: [vintageWatch], threads: { auction: m.thread({ kind: 'auction', input: AuctionInput, initialState: (ctx) => ({ item: ctx.input.item, reservePrice: ctx.input.reservePrice, highestBid: 0, status: 'open' as const, }), step(state, event) { switch (event.type) { case 'auction.bid': return event.payload.amount > state.highestBid ? { ...state, highestBid: event.payload.amount } : state case 'auction.close': return { ...state, status: 'closed' as const } default: return state } }, }), }, }),)Start the definition on a host that includes the module. The input type follows the definition's schema:
import { createLooms } from '@swirls/looms/runtime'const looms = createLooms({ modules: [auctionModule] })const { runId } = await looms.start(vintageWatch, { item: '1968 Chronograph', reservePrice: 250,})Wait handling, signaling, and a bids projection live in examples/custom-thread.ts (bun run --filter @looms/examples custom-thread).
Ids#
Runs, threads, events, and waits are named by an id format. The default is TypeID: a prefix, an underscore, and 26 characters that encode a UUID.
run_01k7z3m2v8f4xtq9c0e5hn6b1dfor a run,thr_for a thread,evt_for an event,wait_for a wait- Ids Looms draws carry a UUIDv7, so they sort by creation time within a process and index well as a primary key
- Ids a reducer derives for a child carry a UUIDv8 hashed from the parent and the child's position, so replay names the same child every time
Effect ids are not drawn. They are <threadId>:<tag>, and a wait a reducer names with createWaitId(scope, tag) is wait_<scope>_<tag>.
You can always pass your own runId or threadId to a start, launch, or fork. To change what Looms draws when you do not, pass an id format.
Bring your own id format#
An id format is two synchronous functions. create draws a fresh id. derive turns a list of parts into the same id on every call. Use one when run data is replicated into a system that already has an id convention, such as a Postgres schema keyed by UUID.
import { createHash, randomUUID } from 'node:crypto'import { createLooms } from '@swirls/looms'import type { LoomsIds } from '@swirls/looms/core'const uuids: LoomsIds = { create: () => randomUUID(), derive: (kind, parts) => { const hex = createHash('sha256').update(JSON.stringify([kind, ...parts])).digest('hex') return [hex.slice(0, 8), hex.slice(8, 12), '8' + hex.slice(13, 16), 'a' + hex.slice(17, 20), hex.slice(20, 32)].join('-') },}const looms = createLooms({ ids: uuids, modules })A format must keep two rules, checked when you pass it and again for every id it returns. A format that breaks one throws InvalidIdFormatError, which names the rule.
- An id is a non-empty string.
- An id contains no
:, because effect ids are<threadId>:<tag>.
A third rule cannot be checked: derive must be a pure function of its kind and parts. Reducers call it on every replay, in every process, and must get the same child id each time. Do not read a clock, a counter, or random bytes in it.
Use one format per deployment and pass it everywhere ids are drawn: createLooms, createRuntime, createActorCell, createLocalActorHost, the Durable Object config, routeToDurableObject, the queue consumer, createLoomsClient, LoomsProvider or createLoomsStore, and DebuggerShell. Reducers and effect handlers read the configured format as ctx.ids:
effects: (state, ctx) => [ spawn({ // Derived, never drawn: this reducer runs again on every replay. childThreadId: ctx.ids.derive('thread', [ctx.threadId, 'review', state.round]), kind: 'agent', definitionName: 'reviewer', }),]Changing the format, or upgrading across a release that changes the default, renames every derived child. A run that is in flight across the change asks for children under new names and no longer matches the ones in its log, so it will not replay. Let in-flight runs finish on the old format first; see versioning.
Status#
A run (and each thread) is in one of:
running— actively reducing or dispatching effectswaiting— parked on a timer, approval, child completion, or matching event- Terminal:
completed,failed, orcancelled
Three orthogonal structures#
A run coordinates heterogeneous work by keeping structure, history, and causation separate:
Thread tree#
Parent/child invocation hierarchy (agent → checkout workflow → approval gate).
Run stream#
One append-only chronological log across all threads in the run.
Causation graph#
Provenance via causationId and effectId linking events to the effects that produced them.
Hierarchy lives in metadata on the same log, so arbitrary nesting stays on one coherent timeline. Formal treatment: Math. Next: Events & effects.