# Publish packages too big for Kody cloud

Kody publishes run in Cloudflare Worker isolates (about 128 MB of memory). A
package with a large npm graph can run out of memory or CPU while Kody bundles
it, and the publish fails with a resource-limit error that links here.

Kody also checks size before a cloud build starts. When the runtime code
reachable from a package's entry points is over 8 MiB, the publish stops before
bundling and the error names the largest files. That check covers the package's
own source only; npm dependencies are not known until the build runs. A local or
execution context build is not held to it.

You can move the heavy part, bundling, off Kody cloud:

```sh
# Build on this machine (commit first; the CLI pushes HEAD for you)
kody package publish @you/big-package --build local

# Build on an execution context you operate
git push
kody package publish @you/big-package --build context
```

In a terminal, `kody package publish` asks whether to build locally when Kody
cloud runs out of room. Agents and CI get these commands instead of a prompt.

> Building outside Kody cloud is part of the Execution contexts experiment. Turn
> it on at [Experiments](https://kody.codes/account/experiments); the same
> switch covers `--build local`, `--context`, and package bindings. See
> [try publishing from a context](/docs/execution-contexts#try-publishing-from-a-context).

To try the build before you publish, run `kody dev check --build local`. It runs
the same build on your machine over the working tree, uploads nothing, and works
without the experiment.

## Where a publish builds

For a standard publish, Kody checks the named context, then the package binding,
then your execution chain. To require an off-cloud build, use `--build local` or
[`--build context`](#execution-contexts).

With the experiment on, standard publish routing works in this order:

1. **The context you name.** `kody package publish @you/pkg --context build-box`
   (or `context` on `packagePublishExternalPush`) selects that context for this
   publish. If it cannot take the build, the publish fails. `--context cloud`
   builds on Kody cloud.
2. **The package's bound context.** A package can set one in `package.json`:

   ```json
   {
   	"kody": {
   		"publish": { "execution": { "context": "build-box" } }
   	}
   }
   ```

   If the bound context is offline, busy, or its build fails, that publish
   fails. Use `--context <name>` or `--context cloud` to choose a different
   builder for one publish. To change future publishes, edit or remove the
   binding and push the change. Set `"context": "cloud"` to bind to Kody cloud.

3. **Your execution context chain.** Kody tries eligible contexts in chain order
   and can select Kody cloud if the chain permits it. A context that takes the
   build returns its result, including any build error.

[Publish routing tests](https://raw.githubusercontent.com/kody-hq/kody/main/packages/worker/src/publish-offload/publish-routing.node.test.ts).

Auto-publish on push works the same way. A package that subscribes to the repo
pushed event and calls publish from its handler gets these same rules.

With the experiment off, every publish builds on Kody cloud, a bound package
included, and naming a context other than `cloud` is refused.

## What runs where

| Step                                                        | Where                                           |
| ----------------------------------------------------------- | ----------------------------------------------- |
| Typecheck and bundle export, job, handler, and app targets  | Your machine or context (same bundler as Kody)  |
| File manifest check, cheap publish checks, credential stamp | Kody                                            |
| Verify the upload and link the published exports it imports | Kody                                            |
| Writing the registry and switching the serving version      | Kody, only after every check and write succeeds |

Kody never publishes a commit it cannot see. `--build local` refuses uncommitted
changes, pushes HEAD to its upstream branch on the package's Kody remote if it
is not there yet (it never makes a commit), and builds only after Kody confirms
that exact commit is the head of the package repo. If Kody reports a different
commit, nothing is built or uploaded. `--build context` builds the commit Kody
sees, so push first.

What Kody checks before it serves an uploaded build:

- The build's per-file SHA-256 manifest must match the pushed commit's tree
  exactly.
- Every target must be one the commit's `package.json` declares, with the right
  hashes and sizes and no platform-reserved module names.
- Kody does not build your package again. Building elsewhere avoids Kody's
  bundling limits, but finalization still runs on Kody under its own CPU and
  memory limits. Kody checks that the build describes the pushed commit and
  declares the right targets; it does not prove the uploaded bundles were
  compiled from that source. Package info and the publish result show where each
  version was built.
- The builder leaves `kody:@` imports as linked placeholders. Kody resolves them
  to the dependencies' own published artifacts under the same access rules as a
  cloud build. See
  [uploaded target verification](https://raw.githubusercontent.com/kody-hq/kody/main/packages/worker/src/publish-offload/verify.ts)
  for the import and package-binding checks.
- Kody loads and links each distinct imported export once **per batch**. Targets
  in that batch can reuse the work; a later batch can load the same dependency
  again. Different exports from the same package are separate artifacts. If each
  bundles the same library, Kody still processes each copy; it does not extract
  shared code from finished bundles. Artifact size includes the transitive
  modules it carries.
- Kody checks the manifest once, then stores the targets in batches, each in its
  own isolate. Larger uploads start in smaller batches. If linked `kody:@`
  modules push a batch over the budget or an isolate hits its compute limit,
  Kody splits the batch and retries. The commit serves only after every batch is
  stored. A retry of the same publish skips targets already stored for that
  commit.
- [Upload verification](https://raw.githubusercontent.com/kody-hq/kody/main/packages/worker/src/publish-offload/verify.ts)
  rejects package-bound runtime calls for another package id and modules
  supplied at reserved linked-placeholder paths. It does not establish the
  provenance of ordinary JavaScript bundled into an upload.
- Server Wasm imports work in local and context builds when enabled for the
  publishing org. Kody checks the uploaded binaries again, including their
  content-addressed paths, size limits, and lack of shared memory. See
  [WebAssembly in package apps](/docs/package-apps#webassembly).
- A build rewrites every `kody:runtime` and `kody:@` import to a module Kody
  supplies when it loads the bundle. An uploaded bundle that still has a raw
  `kody:` import could not load, so Kody refuses it and names the module and the
  import. That means the builder is out of date: run `kody update` on the
  machine that built it (then `kody daemon restart` on an execution context) and
  build again, or publish with `--context cloud`.

## When a build is refused

The publish result says exactly why. The version that was serving keeps serving.
Usually the fix is a clean, pushed checkout:

```sh
git status        # must be clean
git push          # if the CLI could not push for you
kody package publish @you/big-package --build local
```

The CLI pushes to the branch's upstream. A detached HEAD or a branch without an
upstream is refused; set one with `git push -u <kody remote> HEAD`. The push
uses a write remote Kody mints for that publish with your CLI login, not the
checkout's remote URL or a stored git credential, so a clone whose remote token
has expired still publishes.

Retries within the same build reuse chunks already uploaded for that build.
Running the off-cloud build command again creates a new build.

## Limits

`kody dev --target mock` syncs the working tree over loopback Git, not
`POST /v1/packages`. The 5 MiB JSON request limit still applies to direct calls
to that API, but not to local dev. This does not change hosted publish limits.

For hosted publish, the persisted credential stamp uses the same chunk planner
and throwaway runners as the credential check for large source trees. The
[scan input](https://raw.githubusercontent.com/kody-hq/kody/main/packages/worker/src/credentials/credential-stamp.ts) excludes
files served only as static assets and keeps files reached by the server or
browser runtime graph. The stamp is bound to the published commit. A single
large module is scanned in one runner and can still hit an isolate limit.

Building off the worker avoids Kody's bundling limits, not the finalization or
storage limits: source stays capped at 32 MiB and each uploaded target at about
20 MiB. A single build is tried in its own isolate even when it exceeds the
batching budget. Finalization can still fail if that build and its linked
modules exceed the isolate's CPU or memory limit. The error identifies what Kody
tried to store and lists uploaded sizes before linking. Local and context build
time is not metered by Kody.

## Execution contexts

`--build context` uses the named context or your execution chain and requires a
context to take the build. A context build needs a context you operate that is
available and whose daemon reports `git`. Run `kody daemon update` if the daemon
is too old to report it.
[Routing and operator checks](https://raw.githubusercontent.com/kody-hq/kody/main/packages/worker/src/execution-contexts/routing.ts).

### What the daemon receives

Kody sends the daemon the pushed commit, a read-only token for that package's
repo that expires in about five minutes, and an upload ticket for the build. The
dispatch supplies an empty set of credential picks. A successful context build
reports the builder's name after Kody confirms the upload is sealed.
[Dispatch and seal check](https://raw.githubusercontent.com/kody-hq/kody/main/packages/worker/src/publish-offload/context-dispatch.ts),
[credential picks](https://raw.githubusercontent.com/kody-hq/kody/main/packages/worker/src/execution-contexts/routing.ts), and
[upload ticket check](https://raw.githubusercontent.com/kody-hq/kody/main/packages/worker/src/publish-offload/store.ts).

Related: [heavy work offload](/docs/heavy-work-offload) for runtime work that
never fits a Worker.
