---
title: "Builds and runtime"
description: "How Forge's compiler bundles extensions, resolves npm packages, enforces sandbox limits, and reports build errors."
---

> Documentation Index
> Fetch the complete documentation index at: https://buildrforge.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Builds and runtime

Whenever a commit changes an extension, its publication has Forge's dedicated **compiler** build the extension's source files into a self-contained ES module and source map stored in Forge's object storage. The compiler reads the source, checks it, runs npm, esbuild, and Elm's compiler on it, and loads the result in a sandbox. Calls and UIs run the build that a version records in the registry and never build anything themselves.

## The five build stages

Every extension build proceeds through five sequential stages. Publishing a version first packs it as an npm package: its files, the `package.json` that Forge writes, and the `package-lock.json` that its npm packages resolved to, as its shrinkwrap. Every build of a published version comes from that tarball and the tarballs of the peers it links, and is named by the compiler version and those tarballs. If storage already holds a build under that name, the build finishes immediately without recompiling.

| Stage | What happens | Error tag on failure |
| --- | --- | --- |
| `source` | Checks file count and byte limits, reads binary files' bytes from storage, verifies that no file sits under `node_modules/`, and ensures that `package.json` declares `@buildr/sdk` in `dependencies`. | `SourceRefused` |
| `dependencies` | Installs the packages declared in `package.json` with npm, which resolves them into a `package-lock.json` and checks each tarball's integrity. npm resolves `@buildr/sdk` from the stage's own registry, and the build links the version that it resolves. npm lifecycle scripts never run. | `DependencyFailed` |
| `compile` | Bundles `index.ts` into a single ES2022 module with esbuild (leaving `@buildr/sdk` and `node:*` built-ins external), stripping TypeScript's types without checking them, generates a source map, bundles `ui/web/index.html` if present (compiling any Elm module its scripts import with Elm 0.19.3 and the Elm packages that the module's `elm.json` pins; see [Elm in a UI](https://buildrforge.com/docs/extensions/extension-ui/#elm-in-a-ui)), and bundles any MCP UI modules under `ui/mcp/*`. | `CompileFailed`, or `DependencyFailed` for an Elm package that fails to download |
| `declaration` | Loads the compiled bundle alongside its linked `@buildr/sdk` version inside a zero-network sandbox and extracts the catalog of tools, schedules, triggers, webhooks, mailboxes, workflows, MCP UIs, providers, and requirements. | `DeclarationFailed` |
| `retain` | Writes the bundled module, source map, UI files, and MCP UI scripts to storage under the build hash. | Platform defect |

No stage checks TypeScript types, in `index.ts` or in UI scripts, so code with a type error still builds.

## npm packages and Node built-ins

Every extension starts with a `package.json` at its root that declares `@buildr/sdk`, which its code needs. To use third-party npm packages, add them to its `dependencies` object:

```json
{
  "dependencies": {
    "@buildr/sdk": "^1.0.0",
    "date-fns": "^4.1.0",
    "zod": "3.23.8"
  }
}
```

### Module resolution rules

The compiler resolves imports in your extension files using these rules:

- **Relative imports**: Resolve like TypeScript's `NodeNext` resolver. Importing `./lib/client.js` resolves to `./lib/client.ts` (or `.tsx`, `.js`, `.jsx`), and extensionless imports (`./lib/client`) or folder imports (`./lib` with `./lib/index.ts`) work as well. Relative imports cannot climb above the extension root (`..`).
- **Declared npm dependencies**: Bare specifiers in your own files must match a package declared in `package.json`'s `dependencies` (exact versions, semver ranges, and `npm:<package>@<range>` aliases are supported). Importing an undeclared package fails `compile`, and committing any file under `node_modules/` fails `source`.
- **Peer and transitive dependencies**: When an installed npm package imports one of its own dependencies or `peerDependencies`, the installer resolves the version you declared at the top level (or the highest version allowed by the range) and nests conflicting versions under `node_modules/<pkg>/node_modules/<dep>` just like npm.
- **Server export conditions**: Server bundles resolve package `exports` using `["workerd", "worker", "browser", "module", "import", "default"]` (never `"node"`). UI bundles resolve using `["browser", "module", "import", "default"]`.
- **`cloudflare:` modules**: Forge doesn't support Cloudflare's runtime modules, such as `cloudflare:sockets` and `cloudflare:workers`, in extensions. Extensions reach Forge through `@buildr/sdk` and `ctx`. The compiler refuses every import of a `cloudflare:` module, and the import fails `compile` with `CompileFailed`, which names the package that imports it. Server bundles prefer a package's `"workerd"` export, so packages whose Workers build imports one, such as `postgres`, are refused too. Reach such a service through a package that talks to it over HTTP instead.
- **`@buildr/sdk` modules**: Server code imports `@buildr/sdk`, which stays external, and `@buildr/sdk/mcp`, which the compiler bundles. A web UI imports only `@buildr/sdk/client`, and an MCP UI imports only `@buildr/sdk/mcp-ui`. Any other `@buildr/sdk` subpath, or a module imported from the wrong side, fails `compile`.
- **Node.js built-ins (`nodejs_compat`)**: The extension sandbox runs on `workerd` with its `nodejs_compat` compatibility flag enabled. Both `node:<name>` and bare specifiers for Node built-ins (such as `node:buffer`, `node:crypto`, `node:path`, `node:stream`, `node:util`, `node:events`, `node:url`, `async_hooks`, and `process`) resolve to the runtime's built-in modules and take precedence over npm packages of the same name.

## Source, bundle, and execution limits

Forge enforces explicit bounds on source files, bundles, and runtime payloads so that a runaway build or tool call returns a clear typed error rather than a server crash:

| Resource | Limit | What happens when exceeded |
| --- | --- | --- |
| Source file size | 2 MB (1,999,000 UTF-8 bytes) per text file, and its request body at most 4 MiB of JSON | `createRepoExtensionFile` / `updateRepoExtensionFile` fails with `ExtensionFileTooLarge`, or `RequestBodyTooLarge` when escaping (such as control characters) grows the body past 4 MiB |
| Total source files | 1,000 files per extension | Build fails at `source` with `SourceRefused` |
| Total source size | 8 MiB (8,388,608 UTF-8 bytes), including linked peers on install | Build fails at `source` with `SourceRefused` |
| Binary file size | 16 MiB (16,777,216 bytes) per binary file's attachment blob, and 16 MiB of them in all | `createRepoExtensionFile` / `updateRepoExtensionFile` fails with `ExtensionFileTooLarge` for one file, and the build fails at `source` with `SourceRefused` for the total |
| Server bundle size | 10 MiB of JavaScript | Build fails at `compile` with `CompileFailed` |
| Source map size | 20 MiB | Build fails at `compile` with `CompileFailed` |
| UI / MCP UI bundle size | 2 MiB of browser JavaScript for `ui/web`, and 2 MiB per `ui/mcp` script | Build fails at `compile` with `CompileFailed` |
| CPU time | 10 seconds per tool call or health check. Every step of a workflow attempt shares one 10-second budget. Time spent awaiting `ctx.forge` doesn't count | The run fails, for a tool with `ExtensionToolFailed`, and the message says that it exceeded its CPU limit |
| Wall-clock time | 30 seconds per tool call, its `ctx.forge` calls included. A workflow step's work gets its `timeout`, up to 5 minutes, or 30 seconds without one, and each stretch of the workflow body between steps gets 30 seconds | The run fails, and the message says how long it ran past |
| Outbound `fetch` calls | 50 subrequests per tool/health/step run | Further `fetch` calls in the same run fail |
| `ctx.browser` actions | 50 per tool call, beside its 50 `fetch` calls; a workflow step's actions share its 50 with its `fetch` calls | Further actions reject with a `TypeError` |
| `ctx.email` sends | 20 per tool call or workflow step | Further sends in the same run reject with a `TypeError` |
| `ctx.systemOne` requests | 50 per tool call or workflow step | Further requests in the same run reject with a `TypeError` |
| Browser screenshot or PDF | 16 MiB | Rejects with `BrowserAttachmentTooLarge` |
| Tool / workflow JSON depth | 256 levels of array/object nesting | Refused with `ExtensionToolInputInvalid`, `ExtensionToolFailed`, `ExtensionWorkflowInputTooDeep`, or `ExtensionWorkflowFailed` |
| Tool JSON result | 10 MiB (10,485,760 characters) | Refused with `ExtensionToolFailed` |
| Workflow run input | 4 MB (4,000,000 UTF-8 bytes of JSON) | `ctx.workflows.start` rejects with `ExtensionWorkflowInputTooLarge` (HTTP 400) |
| Workflow run output | 4 MB (4,000,000 UTF-8 bytes of JSON) | The run ends `errored` with `ExtensionWorkflowFailed` |
| Workflow `step.do` result | 1 MiB (1,048,576 UTF-8 bytes) | Step fails permanently with `WorkflowStepError` |
| Workflow signal answer | 1 MiB (1,048,576 UTF-8 bytes of JSON) | Rejected with `ExtensionWorkflowSignalPayloadTooLarge` |
| Error / failure message | 4,000 characters for tools (16,384 for workflows; 1,000 for `health`) | Truncated to the character limit |

## Sandboxes and module state

A tool call runs in a sandbox that its caller's other calls to the same version share. Forge keeps one sandbox for each org, version, and caller, so the bundle's top level runs once for many calls, and what it keeps in module scope, such as a parsed configuration or a client, carries over from one call to the next.

- **One caller's calls share a sandbox, and another caller's never do.** Two members who call the same tool each run in a sandbox of their own. Schedules, triggers, webhooks, and mailboxes run as the installation's extension, in a sandbox that they share only with each other. A member's calls share one sandbox across every repo in the org that runs that version.
- **Module state is a cache, not storage.** Forge can start a fresh copy of the sandbox at any time, and calls that run at once may each run in a copy of their own. Keep nothing in module scope that a call must find; store it through `ctx.forge`.
- **Calls that run at once share module scope.** Keep a call's input and results in its handler, not in a module variable that another call could read or overwrite.
- **A call that throws doesn't reset the sandbox.** A module variable that one call leaves broken stays broken for its caller's next calls, until Forge replaces the sandbox or a commit changes the extension's code.
- **`fetch` works only inside a handler.** A `fetch` at the bundle's top level, or from a timer that fires after the handler returns, rejects with a `TypeError`, because the sandbox doesn't belong to any one call. Each request counts toward the limits of the call whose handler sent it, and follows redirects as [Requirements and outbound `fetch` calls](https://buildrforge.com/docs/extensions/providers-and-connections/#requirements-and-outbound-fetch-calls) describes.

Connection health checks, webhook and mailbox handlers, and workflow attempts each run in a fresh sandbox of their own.

## Build failure and runtime error diagnostics

When a commit's build fails, its publication is `failed` with `ExtensionBuildFailed` as its `failure` (`GET /repos/:repo_id/commits/:commit_id/extension-publications`), and `listRepoExtensionToolsAtBranch`, `callRepoExtensionTool`, and the branch's UI answer it until a commit fixes the source:

```json
{
  "_tag": "ExtensionBuildFailed",
  "extension_name": "directory",
  "message": "index.ts:8:14: The tool's input is not a value type: use the SDK's object(), string(), and the rest",
  "reason": {
    "_tag": "DeclarationFailed",
    "location": {
      "path": "index.ts",
      "line": 8,
      "column": 14
    }
  }
}
```

- `reason._tag` identifies the stage: `SourceRefused`, `DependencyFailed` (which includes `dependency`, the package name that failed to install), `CompileFailed`, or `DeclarationFailed`.
- When the error originates in one of your extension files, `reason.location` gives the exact `path`, `line`, and `column`, mapped back through the bundle's source map for both compile errors and runtime exceptions thrown during `declaration` or `callTool`.

Source: https://buildrforge.com/docs/extensions/builds-and-runtime/index.mdx
