An instance updates itself. When you deploy a Release, the instance's deployment controller stores the artifact, assembles a new version of the release Worker with every installed Release, uploads it to Cloudflare and activates it.
The controller runs in its own Worker, scwrap-instance-<stage>-controller, which owns the instance hostname. It answers the routes below itself, hands builds to the build Worker and forwards every other request to the release Worker. Release code shares no isolate with it and has no binding to it, so it cannot reach the controller's Cloudflare token.
This page describes that service: its routes, its guarantees and how it recovers. To deploy, you only need the scwrap CLI or the deployer MCP server.
Routes
Every route needs the deployer token. The run token is refused with 401. A named deployer token limited to some Scrapers reads the catalog and deployment status, and may send only commands that deploy, remove or roll back those Scrapers; anything else, including every /platform-deployments route and PUT /secrets, answers 403.
| Request | Does |
|---|---|
GET /deployments | Returns the generation, the installed releases, the platformDigest and a pendingDeploymentId. Null fields mean the instance is not initialized. With ?include=isolation, each Release also names its isolation, always "dynamic-worker": every Release runs in its own sandbox. With ?include=runs, activeRuns counts the active Runs per Scraper, or is null when the release Worker does not answer. With ?include=deployed, each Release also has deployedAt, when it was installed, deploymentId, the command that installed it, and deployedBy, the name of the named deployer token that sent it; each is null when the instance did not record it, and deployedBy is null for the unnamed deployer token. They can be combined, ?include=deployed,isolation,runs. |
POST /deployments | Accepts a deployment command for one Release: deploymentId, expectedGeneration, an optional force: true that stops active Runs first, and either the artifact to install, the scraper ID to remove, or the scraper ID to rollback with an optional revision (Rolling back). An artifact with fields the protocol does not list, such as the nodeCode of a Node release, answers 400. Removing a Release the instance does not run answers 404. An artifact sent from outside the instance is deprecated; deploy with POST /builds instead. |
GET /deployments/history | Returns the retained platforms as digests, newest first, and releases, from each Scraper ID to its retained Releases, newest first, with the same deployedAt, deploymentId and deployedBy. These are the targets rolling back chooses from. |
GET /deployments/<deploymentId> | Reconciles that deployment with Cloudflare and returns its status. Unknown IDs answer 404. |
POST /platform-deployments | Accepts a new version of the instance's own platform code, platform, or rollbackPlatform, a retained platform's digest or null for the one the last upgrade replaced. Keeps every installed Release. |
POST /platform-deployments/check | Takes a platform's scwrap-sdk.mjs as the body and answers { "incompatible": [...] }: each installed Release that imports a name it lacks, with id, revision, digest and the missing names. Changes nothing (Compatibility check). |
POST /builds | Typechecks and bundles a Scraper's source on the instance, and with deploy: true deploys it (Builds). Any deployer token may send it, a named one included. |
GET /builds/<buildId> | Returns that build's status, and deploys a passed deploying build that is not deployed yet. Unknown IDs answer 404. |
POST /builds/<buildId>/deploy | Deploys the Release a passed build bundled, whether or not it was sent with deploy: true, without building it again. The optional body { "force": true } stops active Runs first. Answers the build status like GET /builds/<buildId>: a running build answers running and deploys nothing, so send it again once it passed. Repeating it never deploys the build twice. |
/deploy-mcp | The deployer MCP server, whose tools call /builds and /deployments. |
PUT /secrets/<name> | Sets BROWSER_USE_API_KEY, RELEASE_ENV or SCRAPER_SETTINGS to the request body and records the resulting Worker version (Rotating secrets). Other names answer 404. |
DELETE /secrets/<name> | Deletes a retired secret, BROWSER_ENDPOINT, BROWSER_HEADERS, INSTANCE_TOKEN, RUNNER_ENV or RUNNER_TOKEN, from the release Worker and records the resulting Worker version, under the same lock and guard as PUT. Other names answer 404. |
The command and status schemas are in @scwrap/deploy/protocol.
Errors
Every error answer has the same JSON body as the HTTP API, { "code", "error" }. error is the message. The controller's code names the kind of refusal by status:
| Status | code |
|---|---|
| 400 | invalid_request |
| 401 | unauthorized |
| 403 | out_of_scope |
| 404 | not_found |
| 409 | conflict |
| 413 | too_large |
| 503 | unavailable, or controller_not_initialized when the controller has no deployer token or Cloudflare token |
Any other status has deployment_failed. The scwrap CLI prints the status and the body in its failure.
Builds
POST /builds checks a Scraper's source with the instance's own SDKs, and with deploy also deploys it, so the caller needs no toolchain. The body names each file by its path, up to 100 .ts files and 1 MiB in all:
{ "files": { "hn-top.ts": "import { Deployment } from \"@scwrap/deploy\"; …" }, "deploy": true }| Field | Means |
|---|---|
files | The source, by path. |
entry | The file whose default export is the Release, export default Deployment.worker({ ... }). Optional for one file, which is its own entry. A deploying build of several files names it. |
deploy | Optional. true deploys the built Release once the build passes. |
force | Optional. true makes that deployment stop active Runs first. |
The answer is a build status, { "buildId", "status", "diagnostics" }, and for a build with an entry also scraper, the Release's { "id", "revision" }, and for a deploying build deployment, the deployment's { "deploymentId", "generation", "status" }:
status | Means |
|---|---|
running | The build has not ended within 20 seconds of the request, HTTP 202. Read it again with GET /builds/<buildId>. |
passed | Every file typechecks and the entry built. diagnostics is empty, or warns about a deprecated import or a capability the Scraper holds no grant for. |
failed | The source imports something it may not, does not typecheck, does not bundle, or its entry exports no Release. diagnostics holds the reason or the compiler's output, with each diagnostic on the file's own lines. |
A build that cannot end answers 503 unavailable with the reason, such as the build Container's output when the check crashed. Sending the same request again starts that build once more under a new buildId, up to three times, so scwrap retry recovers from a build the Container could not finish. A body that is not a file map answers 400, a larger one 413.
What a build checks.
- Its files import only
effect, its subpaths, the seven SDKs and their subpaths, the curated packages, and each other. The curated packages are a short list of npm packages at one pinned version each,curatedBuildPackagesinprotocol.ts, that the toolchain installs next to the SDKs; today it holdscheerio1.2.0, for parsing HTML. A Scraper that imports one depends on that exact version. Any other package, and any relative import that leaves the submitted files, fails the build without typechecking. - The files typecheck with the Effect diagnostics of
tsconfig.base.json, so an undeclared error or a missing Capability fails it. - The entry bundles against the instance's
scwrap-sdk.mjs(How releases are built). - The release Worker loads the bundle in a Dynamic Worker with no network and reads the Release's
id,revisionandmaxRunDurationfrom it. A passed build whose bundle imports a deprecated name fromscwrap-sdk.mjs, or calls the front desk for a capability the grants table does not give its Scraper (Grants), says so indiagnostics.
How a build deploys. A passed build with deploy: true becomes a deployment with the ID build-<buildId>: the controller submits the built artifact to its own POST /deployments at the generation it finds, under the same lock, journal and guarantees as any other deploy. It does this when it answers the build, so a build that answered running deploys on the GET /builds/<buildId> that first sees it pass. Reading the build again answers that deployment's status and never deploys it twice; a build whose Release the instance already runs answers active without a new deployment. The caller's deployer token must be allowed to deploy the Release's Scraper ID, otherwise the answer is 403 and nothing is deployed. A deployment the controller refuses with 409, such as while another deploy is pending, records nothing, and the next read of the build submits it again at the then current generation.
Where it runs. The controller authenticates the request and forwards it, with the digest of the instance's toolchain, to scwrap-instance-<stage>-builder, a Worker that holds no secrets and that nothing else can reach. Each build is a run of the Check workflow, Effect's effect/workflow on Durable Objects: a build that a restart cuts short runs again, and the same request on the same toolchain is the same build, so sending it twice returns the first build unless that build died. The workflow's one step runs in the BuildBox Container, basic and without internet access. On its first build after a start or an upgrade the Container unpacks the toolchain from the Toolchains bucket, which takes up to a minute; a warm Container answers in about 10 seconds. It sleeps 10 minutes after its last build.
The toolchain is built from the instance's own SDKs whenever the Stack applies a change to them (What the Stack creates), so a build always checks against the SDK version the instance runs.
Client-built artifacts
A POST /deployments with an artifact that a client built and sent from outside the instance still deploys, but is deprecated since 0.10.0, and 0.11.0 removes it. The controller Worker marks every request it forwards from outside, and the controller answers such an upload with two headers:
Deprecation: @1796342400
Link: <https://scwrap.dev/instance/deployment-service#builds>; rel="deprecation"; type="text/html"The handoff of a deploying build reaches the controller from inside the instance and carries neither header. Removals and rollbacks are not deprecated. Send the source to POST /builds with deploy: true instead, as scwrap deploy, the deployer MCP server and createDeployer().build do.
Guarantees
One deploy at a time. A single controller, the Durable Object named instance, handles every deployment and status request under one lock.
Intent before action. The controller durably records a deployment before it uploads anything. After a restart, a recorded deployment still blocks others until it is resolved.
Generations detect races. A command names the generation it expects. If the instance is at another generation, the answer is 409 and nothing changes.
Repeating is safe. The same command sent twice returns the saved outcome and deploys once. A known deploymentId with different content answers 409.
An incompatible deploy changes nothing. A deploy that would leave a Release next to a scwrap-sdk.mjs that lacks one of its imports answers 409 before anything is recorded or uploaded (Compatibility check).
A rejected upload changes nothing. If Cloudflare refuses an upload with a 4xx, the previous version stays active and nothing stays pending. Reading the rejected deployment answers 422; sending the same command again, under the same ID, plans and uploads it afresh, so a rejection caused by a token or account setting can be retried once that is fixed. A corrected command can be sent under a new ID.
Runs during a deploy
A deploy does not abort a Run. Every Run owner is a Runner Durable Object of the release Worker, so every new version, whatever it changes, would reset the owners of active Runs. The controller activates each version with a deferred code update strategy of 900 seconds instead. That covers every deploy, removal, rollback and platform upgrade, a secret rotation, and the one deploy that adds a Durable Object class, whose non-versioned upload carries the same strategy:
- A Run owner that is running a Run keeps the code it started on until the Run ends, and only then adopts the new version.
- A Run lasts at most 14 minutes (Run duration), and its Run owner's alarm handler at most 15 (Cloudflare limits), so 900 seconds covers every Run that was active when the version was activated.
- New Runs start on the new version, because a new Run owner is a Durable Object that is not running yet. For a few seconds after the controller answers
active, a new Run owner can still start on the previous version while the catalog already lists the new Release. Such an owner does not run the Run on code that lacks its Release: it sets its alarm again every 5 seconds, up to 12 times, and runs the Run once it wakes on the new version. An owner still on another version after that minute ends the RunfailedwithRelease <digest> of <scraper> was replaced before the Run started, which is also how a Run ends whose Release a later deploy really replaced before it started (Known gaps). - The deploy itself does not wait: the controller answers
activeas soon as Cloudflare accepted the version.
A Run started on a replaced Release ends with every item it produced, and its record carries the replaced Release's digest and revision.
Deferral is best effort on Cloudflare's side: it can still reset an object early, for example during a Workers runtime update, which no code update strategy delays. A Run owner reset before its Run ended restarts its alarm on the current code and ends the Run failed. When the instance's generation changed in between, the error says a deploy cut the Run; otherwise it says the owner stopped. Either way the items of its last unstored chunk are lost (Known gaps).
Active Runs
GET /deployments?include=runs counts as active every Run whose record is running and that started within the last 16 minutes, the longest a Run owner lives. It reads them from the results bucket's per-Scraper index, newest first, so it reads only recent Runs.
"activeRuns": [{ "count": 2, "scraper": "quotes" }]Forcing a deploy
A command with force: true stops the active Runs instead of letting them finish. Before it activates the version, the controller asks each active Run's owner to stop. The owner tells the Run's sandbox, which ends the Release's stream itself, so its scope closes and with it any browser session, as at the maximum duration. The owner then stores the items it had not stored yet and ends the Run failed with:
Run stopped by deploy <deploymentId> before it ended; the items it produced until then are storedA sandbox that has not ended its stream 30 seconds later is cut off by its owner, which then stops the Browser Use sessions the Run still holds (Browser Use sessions end with their Run). The controller waits up to 45 seconds per Run owner, then activates the version with the immediate strategy, which resets every Run owner that is still running. Forcing needs an unlimited deployer token: every version replaces the Run owners of every Scraper, so a named token that sends force gets 403.
Generation
The generation is the identity of everything installed: the platform code plus every Release identity. It is an opaque 64-character value that the instance computes.
It changes on every successful deploy, including a platform upgrade. Clients compare generations and never calculate them.
What is stored where
The controller keeps its state in its Durable Object storage and its artifacts in the R2 bucket.
| R2 key | Holds |
|---|---|
releases/<digest>.mjs | A Release's Worker module. |
platform/<digest>.json | A platform manifest. |
Artifacts are named by digest and never overwritten. Old ones are kept, which is what makes a rollback possible without a rebuild. The release Worker can write the same bucket, so the controller checks a platform or Release artifact against its digest every time it assembles a version, and refuses a mismatch with 503.
The Durable Object storage holds the active manifest (generation, platform, installed Release identities, Worker version and applied Durable Object migration), the pending deployment ID, one record per deployment ID, and the rollback history: history/releases/<scraper-id>, the last 20 identities a deploy replaced for that Scraper, and history/platform, the last 20 replaced platforms.
How a version is assembled
Every Worker version the controller uploads is made of:
| Module | From |
|---|---|
main.mjs | The platform: routes, MCP server, the front desk and the Runner and ProxyRelay Durable Object classes. |
scwrap-sdk.mjs | The shared copy of Effect and the domain SDK. |
installed-releases.mjs | Generated by the controller: a map from Scraper ID to the installed Release's identity (ID, digest and revision), plus the generation. |
A Release's code is not a module of the version. It stays in the bucket at releases/<digest>.mjs, and the release Worker loads it into its own Dynamic Worker when a Run first needs it. A platform cannot ship its own installed-releases.mjs.
Uploads after the first one keep the Worker's secret bindings and carry no migrations, except the one deploy that adds a Durable Object class. Secret values are never part of a platform manifest.
Compatibility check
An installed Release imports names from scwrap-sdk.mjs (a versioned contract), and a Release's sandbox cannot load when one of those imports is missing. The controller checks this before every deploy, upgrade and rollback: it reads the named imports of every Release the new version would contain, and compares them with the names the new version's scwrap-sdk.mjs exports. A missing name answers 409, with nothing recorded or uploaded, and names each Release:
Releases incompatible with platform 3f9a1c2e4b5d's scwrap-sdk.mjs, nothing changed: quotes v3 (a1b2c3d4e5f6) imports OldNamePOST /platform-deployments/check runs the same check against the installed Releases without deploying. The ScwrapInstance Stack calls it before it creates or changes any resource, so an upgrade that would break an installed Release stops at plan and at apply with that list. When the controller does not answer the route, as on an instance that does not exist yet, the Stack skips this early check with a warning; the controller still refuses the upgrade.
Only named imports can fail to link, so only they are checked. To upgrade anyway, first redeploy the Releases built against the new SDK, or keep exporting the names for one more version.
Rolling back
Every activation records what it replaced: each Release identity that left the installed set, and the previous platform. A rollback is a deployment command like any other, with a deployment ID and an expected generation, so it is serialized, recorded before it uploads, safe to repeat and reconciled the same way.
| Command | Reinstalls |
|---|---|
{ "rollback": "<scraper-id>" } | The most recent replaced identity of that Scraper that differs from the installed one. A removed Release comes back. |
{ "rollback": "<scraper-id>", "revision": "<revision>" } | The most recent replaced identity with that revision. This also rolls a rollback forward again. |
{ "rollbackPlatform": null } | The platform the last upgrade replaced, with every installed Release. |
{ "rollbackPlatform": "<digest>" } | That retained platform. |
The controller checks the target's artifacts against their digests and runs the compatibility check first. A Release rollback never picks a recorded Node release identity, one that carries a nodeDigest, because an instance runs only Worker releases. A target that is not recorded, or only as a Node release, answers 404, listing the recorded revisions and the Node releases it skipped. scwrap rollback and scwrap platform rollback send these commands.
A rollback lasts until the next deploy of the same thing. CI deploys the catalog with --changed and upgrades the platform from main on every push, so after a rollback, revert or fix the change on main too; otherwise the next push installs it again. To roll back a bad platform:
- Run
pnpm scwrap platform rollbackwith an unlimited deployer token. The previous platform is active once it prints"status":"active". - Revert the platform change on
mainand push. The Stack then installs the platformmainbuilds, which is the one already active unless something else changed.
A platform rollback restores that platform's code and its other plain-text bindings, and keeps the Worker's secrets and the current access bindings: RUN_TOKENS, RUN_TOKEN_SHA256 and GRANTS stay as the replaced platform had them, so a run token rotated or revoked since, or a grant removed since, stays that way. When those bindings differ, the controller retains the restored platform with the current ones as a new platform, so scwrap deployments then shows a platform digest of its own. The controller Worker is not part of a platform rollback: the Stack uploads it, so reverting main rolls it back on the next apply.
Durable Object classes
Cloudflare cannot add a Durable Object class through a Worker version upload: it refuses a version that changes a class's lifecycle, and only a non-versioned deploy applies one (Cloudflare). The controller handles both kinds:
A platform declares the classes it adds after the bootstrap's
Runner(tagv1) asmigrationsin its metadata, oldest first, each with a newtagand itsnew_sqlite_classes. It exports each class and may bind it in the same version.json"migrations": [{ "tag": "v2", "new_sqlite_classes": ["Scheduler"] }]The manifest records the tag the release Worker has applied. When a platform declares steps after it, the controller sends that one version as a non-versioned script deploy with
old_tag,new_tagand the pending steps, keeping the secret bindings, then finds the version by its tag and records the new tag. Every other deploy stays a version upload followed by an activation.Cloudflare refuses a migration whose
old_tagis not the Worker's current tag, so a manifest that disagrees with the Worker changes nothing.A Worker cannot roll back across a class change. A platform that does not declare the applied tag, which includes every platform from before the change, answers 409.
Only additions are supported. Removing or renaming a class deletes or moves its data, so it is not a platform deploy.
Interrupted deploys
A deploy has two calls to Cloudflare that can lose their answer: the upload of a new version and its activation.
| What was lost | How the controller recovers |
|---|---|
| The upload's answer | It looks for a Worker version annotated with the command's fingerprint. If it finds one, the upload happened; if not, it uploads again. |
| The activation's answer | It compares the version that is actually deployed with the one it uploaded. |
Cloudflare's read of the deployed version can lag its own writes by more than half a minute. Before the controller treats a version it did not expect as an outside change, and after it activates, it reads again every 2 seconds for up to 30 seconds. A deployed version that is neither the active one nor the uploaded one after that wait answers 409; an activation that is still not visible answers 503 and stays pending.
Until the outcome is known, the deployment is pending. GET /deployments/<deploymentId> and a repeated POST both trigger this reconciliation, which is why reading a pending deployment's status is not a read-only operation.
Limits
- An upload Cloudflare took after the controller looked for it is uploaded twice. Both versions carry the same fingerprint and code, and the controller activates the one it finds.
- A change made to the Worker outside the controller, or a split-traffic deployment, is refused and not overwritten.
activemeans Cloudflare's control plane accepted the version. It does not mean every edge location already serves it; a result record'sdigestsays which Release ran.- Activating a new version can interrupt the request that asked for it. Keep the deployment ID and ask again.
Known gaps lists the recovery cases that still need manual work.
Installing and upgrading the platform
installInstance from @scwrap/platform-cloudflare/deployments is the one entry point that installs, upgrades and repairs an instance's platform. The ScwrapInstance Stack calls it.
- Only a release Worker that does not exist gets a bootstrap upload, with the initial migration for the
Runnerclass and the bootstrap secrets.bootstrapInstancemakes only that upload; the Stack calls it before it uploads the controller, which binds the release Worker. - Then the platform goes through the controller like a Release, authenticated with the deployer token. Before any of this, the Stack runs the compatibility check with
checkPlatformCompatibility. - An unchanged platform is a no-op. An upgrade keeps every installed Release.
Nothing else uploads the release Worker. Alchemy uploads the controller Worker itself and declares the bucket, the hostname and the proxy relay's Container application around them. See Infrastructure.
Source
- deployment-service.ts: state, generations and reconciliation
- deployment-cloud.ts: Cloudflare's versions and deployments APIs
- deployment-http.ts: the routes
- build-check.ts: the check a build runs in the Container
- build-route.ts: the controller's
/buildsroute and its handoff to/deployments - instance-builder.ts and instance-build-checker.ts: the build Worker, its workflow and its Container
- instance-toolchain.ts: the toolchain
- deployments.ts:
installInstance - deployments.test.ts