Skip to content

Introduction to Runtime

An Effect is an inert description of work; the Effect runtime is the machine that executes it. The runtime schedules fibers, supplies services from a Context, propagates fiber-local configuration, coordinates interruption, and runs finalizers. VibeLang programs compile directly onto this runtime.

Execution infrastructure

effect function operation() {
  try sleep("10 millis")
  return "complete"
}
 
const outcome = Effect.runPromiseExit(operation())
// Run in an effect function:
//   const result = try promise(() => outcome)
//   try Console.log(result._tag)
//   if (Exit.isSuccess(result)) try Console.log(result.value)
// Output: Success
// Output: complete

Calling operation() builds a cold Effect without executing it. Passing it to a runner—here Effect.runPromiseExit—starts a root fiber that steps through try statements, suspends on async operations, and resolves to an Exit containing either the return value or the full failure Cause. Inside an effect function, try executes sequentially on the current fiber; concurrency requires an explicit operator such as fork, try.all, or for concurrent.

RunnerReturnsTypical boundary
Effect.runSync / runSyncWithA (throws on async or failure)Pure synchronous entry points
Effect.runPromise / runPromiseWithPromise<A> (rejects on failure)Async host APIs and HTTP handlers
Effect.runPromiseExitPromise<Exit<A, E>>Boundaries that inspect full Cause
Effect.runFork / runForkWithFiber<A, E>Long-lived processes with explicit cancellation

Run with an explicit service context

service Labels {
  format(value: string): string!!
}
 
const context = Context.make(Labels, {
  format: effect (value) => `[${value}]`
})
 
effect function label() {
  const labels = try Labels
  return try labels.format("queued")
}
 
const result = Effect.runSyncWith(context)(label())
// Run in an effect function: try Console.log(result)
// Output: [queued]

Plain runners accept only programs whose requirements are already satisfied (R = never). The run*With(context) variants supply a prebuilt Context to satisfy outstanding service requirements.

Runners belong strictly at the boundary where no fiber is executing: module top level, async functions, or plain (non-effect) callbacks. Calling a runner directly inside an effect function or effect { ... } block is compiler error EK1005—compose with try or fork instead.

Most applications assemble services from layers. Reach for an explicit Context when the host already holds one, or a ManagedRuntime when multiple entry points must share layer-allocated resources.

Change runtime behavior within a region

const Visible = Logger.layer([
  Logger.withConsoleLog(Logger.make(({ message }) => String(message)))
])
const Quiet = Logger.layer([])
 
effect function startup() {
  try log("visible before")
  provide Quiet {
    try log("suppressed in this region")
  }
  try log("visible after")
}
// Run in an effect function: provide Visible { try startup() }
// Output: visible before
// Output: visible after

provide scopes a layer to its block and restores the prior configuration on exit, whether the block succeeds, fails, or is interrupted. Child fibers forked inside the block inherit the active configuration at fork time.

Logger.withConsoleLog routes formatted output through Effect's Console service rather than a global console object. Swap formats the same way—for example, Logger.layer([Logger.consoleJson]) for structured JSON output.

Replace a runtime service for a block

const seeded = { nextIntUnsafe: (): number => 4, nextDoubleUnsafe: (): number => 0.5 }
 
effect function roll(): number!! {
  provide Random.Random = seeded {
    return try Random.nextInt
  }
}
// Run in an effect function: try Console.log(try roll())
// Output: 4

provide Key = value overrides a single service for one block. Built-in runtime references—Random.Random, Clock.Clock, and Scheduler.Scheduler—carry defaults, so they never appear in a function's dependency contract. Replacing them with provide affects both the block's statements and any child fibers they spawn. Layers and direct service bindings can share one provide header: provide Logger.layer([]), Random.Random = seeded { ... }.

Build and dispose managed runtimes

// notifications.vibe
export service Notifications {
  send(message: string): void!!
}
 
export const NotificationsLive = Layer.succeed(Notifications, {
  send: (message) => Console.log(message)
})
 
export effect function notify(message: string) {
  const notifications = try Notifications
  try notifications.send(message)
}
// Run in an effect function: provide NotificationsLive { try notify("direct event") }
// Output: direct event
// TypeScript (.ts)
// host.ts
import { ManagedRuntime } from "effect"
import { NotificationsLive, notify } from "./notifications.js"
 
const runtime = ManagedRuntime.make(NotificationsLive)
 
export async function example() {
  try {
    await runtime.runPromise(notify("first event"))
    await runtime.runPromise(notify("second event"))
  } finally {
    await runtime.dispose()
  }
}
// Run: return example()
// Output: first event
// Output: second event

ManagedRuntime.make(layer) builds its layer lazily on first execution and reuses the resulting context across every subsequent call until runtime.dispose() runs. Sibling provide blocks each build independent layer instances; a ManagedRuntime avoids rebuilding shared connection pools, caches, or clients across separate host invocations.

Always dispose a managed runtime when its owner shuts down, and create a fresh runtime for a new owner. Inside .vibe, clean up a managed runtime with try runtime.disposeEffect.

Connect runtime execution to frameworks

// TypeScript (.ts)
// notification-host.ts
import { ManagedRuntime } from "effect"
import { NotificationsLive, notify } from "./notifications.js"
 
export function makeNotificationHost() {
  const runtime = ManagedRuntime.make(NotificationsLive)
  return {
    onMessage: (message: string) => runtime.runPromise(notify(message)),
    close: () => runtime.dispose()
  }
}
// Run:
//   const host = makeNotificationHost()
//   return host.onMessage("framework event").finally(() => host.close())
// Output: framework event

Let the host framework own this thin adapter: call onMessage on incoming events and close on shutdown, fixture teardown, or component unmount. Keep the runtime instance stable across requests or renders so layer resources remain shared.

Framework adapters can live in TypeScript (importing compiled VibeLang functions as shown) or in plain callbacks registered at a .vibe module's top level, since top-level callbacks execute outside any fiber.

Coming from Effect

service declarations replace hand-written Context.Service boilerplate in .vibe. The runtime, Context, Layer, and ManagedRuntime remain standard Effect APIs; TypeScript entry points still pass cold Effects to runners.


The matching Effect page: effect.website/docs/v4/runtime