Repository navigation
ADR 0016: extract package runtime and jobs lanes into separate workers #1383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
docs/contributing/decisions/0016-mono-worker-extraction.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # 0016: Extract the package runtime and jobs lanes into separate workers | ||
|
|
||
| - **Status:** accepted | ||
| - **Date:** 2026-08-11 | ||
|
|
||
| ## Context | ||
|
|
||
| The `kody` worker serves every surface — Remix app, MCP, OAuth, package | ||
| apps/invocations, webhooks, remote connectors, email, maintenance, and | ||
| jobs/scheduled work — from one script hosting thirteen Durable Object classes. | ||
| That mono-worker shape concentrates deploy blast radius (every change redeploys | ||
| everything), makes the full 600+-file test suite the gate for every change, and | ||
| puts the fastest-churning, riskiest subsystem (untrusted-code execution via the | ||
| Worker Loader) in the same failure domain as login and MCP. The repo already has | ||
| two separate-worker precedents chosen for failure-domain isolation: | ||
| `packages/status/` ([0004](./0004-status-page-separate-worker.md)) and | ||
| `packages/backup-control-plane/`. | ||
|
|
||
| A hot-path audit preceded this decision. The chatty per-request/per-run patterns | ||
| (entitlement checks → `UserMeter`, run appends → `RunLog`, storage queries → | ||
| `StorageRunner`) are Durable Object calls, which are location-addressed and | ||
| script-independent — moving a DO class to another script does not change its | ||
| call cost. The runtime and invocation paths read/write ~15 `APP_DB` tables | ||
| directly, so forcing relational access through service-binding RPC would rebuild | ||
| half a data layer as serialized contracts. The jobs lane is the one clean data | ||
| seam: `jobs` and `archived_job_artifacts` are effectively single-owner and join | ||
| with nothing else. | ||
|
|
||
| ## Decision | ||
|
|
||
| Extract two workers from the mono-worker, along failure-domain seams, deployed | ||
| independently: | ||
|
|
||
| 1. **Package runtime worker** (`packages/runtime-worker/`): package apps, | ||
| package invocation, and run execution. The `PackageServiceInstance`, | ||
| `PackageRealtimeSession`, `StorageRunner`, and `RunLog` Durable Object | ||
| classes move to this script (dashboards tolerate slower/cached RunLog reads; | ||
| run writes originate here). It keeps direct `APP_DB` access — the security | ||
| boundary that matters is the untrusted user code, which already runs in | ||
| zero-binding Worker Loader isolates; the runtime host code is the same trust | ||
| level as main. | ||
| 2. **Jobs worker** (`packages/jobs-worker/`): `JobManager`, scheduled lanes, and | ||
| job retention. It owns a **dedicated D1 database** (`kody-jobs`) holding | ||
| `jobs` and `archived_job_artifacts`, migrated from `APP_DB` by a bounded | ||
| manual copy. The cutover must be lossless: quiesce the scheduled lanes and | ||
| job mutation paths (brief write pause), export, import, verify row counts and | ||
| representative records (including archived artifacts) against the source, | ||
| then switch reads and writes together in one deploy — never a read/write | ||
| split across databases. `APP_DB` keeps the old tables untouched as the | ||
| rollback path until verification passes; only a later migration drops them. | ||
| Background write churn then leaves `APP_DB` entirely. | ||
|
|
||
| Everything else — Remix app, MCP, OAuth, account surfaces, email, connectors — | ||
| stays in the main worker for now; they share auth/session/D1 state too tightly | ||
| for a split to pay. Untangling MCP from the Remix app is a desired future | ||
| investigation, deliberately not part of this change. | ||
|
|
||
| Cross-worker calls go over Cloudflare service bindings with small, | ||
| coarse-grained, explicitly typed contracts in `packages/shared` — no chatty | ||
| per-row RPC. Table ownership is documented: post-split, `jobs` and | ||
| `archived_job_artifacts` belong to the jobs worker; a cross-worker write to | ||
| another worker's tables is a review smell. `UserMeter` stays in main and is | ||
| reachable from the extracted workers as a DO binding. | ||
|
|
||
| The split moves code between trust-equivalent hosts; it does not change any | ||
| isolation guarantee. Every moved surface keeps its existing authenticated user | ||
| context and owner-scoped queries (jobs, artifacts, run logs, and storage stay | ||
| keyed by user/package-derived identities, and moved DO classes keep their names | ||
| and id-derivation so instances resolve to the same objects). Migration | ||
| verification includes confirming records stay associated with the same user. | ||
| Resources staying in main (packages, secrets, values, memories, connectors, | ||
| inboxes) are untouched. | ||
|
|
||
| Deploys are fully independent per worker (nx-affected-narrowed CI, per-worker | ||
| SHA guards and healthchecks), following the status-worker pattern, with the | ||
| standard service-binding ordering rule: deploy the callee (with | ||
| backward-compatible changes) before the caller that depends on it. DO class | ||
| moves between scripts use Wrangler's cross-script transfer mechanism executed in | ||
| a documented runbook order — the receiving worker declares the incoming | ||
| transfer, and the source worker's deploy commits the handoff — verified on | ||
| preview deploys before production. | ||
|
|
||
| ## Consequences | ||
|
|
||
| A bad runtime or jobs deploy no longer takes down login/MCP; each worker's | ||
| bundle, cold-start weight, and test gravity shrink; rollback becomes | ||
| per-subsystem. Costs: more wrangler configs to keep coherent, per-worker deploy | ||
| workflows, and cross-cutting features that span a boundary become multi-PR | ||
| affairs with ordering. The DO script migration is the one genuinely risky | ||
| one-time step and gets a runbook plus preview verification rather than a | ||
| dual-running system. If `APP_DB` becomes a bottleneck again, the next valve is | ||
| more per-user DO placement per [0002](./0002-data-placement.md), not further D1 | ||
| splits — cross-database transactions do not exist, and the publish path welds | ||
| the core package tables to `APP_DB`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: kentcdodds/kody
Length of output: 5890
🏁 Script executed:
Repository: kentcdodds/kody
Length of output: 50372
🌐 Web query:
Cloudflare Durable Objects migrations cross-Worker transfer expecting-transfer transferred tombstone current documentation💡 Result:
Cloudflare has transitioned to a declarative approach for managing Durable Object (DO) class lifecycles using an exports map in your Wrangler configuration (e.g., wrangler.toml or wrangler.json), which replaces the older, imperative migrations array [1][2]. The cross-Worker transfer process uses two specific "tombstone" states to facilitate the move: 1. Target Worker Configuration: The target Worker must declare an expecting-transfer entry in its exports map. This entry requires the storage type and the name of the source Worker (transfer_from) [3][4]. 2. Source Worker Configuration: The source Worker must declare a transferred tombstone in its exports map, specifying the name of the target Worker (transferred_to) [3][4]. The actual handoff of the namespace commits when the source Worker's deployment is successfully landed [3][5]. It is recommended to keep the source class in the code during this process to ensure zero-downtime [1][2]. If the source Worker still needs to access the DO after the transfer, it should be updated to point at the target Worker using a durable_objects binding that specifies the script_name [5]. To cancel a pending transfer, you can simply remove the expecting-transfer entry on the target Worker (or replace it with a standard live entry), which causes Cloudflare to delete the pending record [3][4]. Declarative exports and the legacy migrations array are mutually exclusive within a single Worker [1][2].
Citations:
🏁 Script executed:
Repository: kentcdodds/kody
Length of output: 21231
🌐 Web query:
site:developers.cloudflare.com/durable-objects/reference/durable-objects-migrations expecting-transfer transferred deployment order script_name exports map💡 Result:
The terms "expecting-transfer", "transferred", "script_name", "exports", and "map" relate to the configuration and migration of Cloudflare Durable Objects [1][2]. These elements are used to define how stored Durable Object namespaces are moved between different Worker scripts during a deployment migration [1][3]. The lifecycle states and configuration fields function as follows: Lifecycle States These are used within the configuration to track the movement of a Durable Object class [1][2]: - expecting-transfer: A state declared by the target (destination) Worker, indicating it is prepared to receive a Durable Object namespace from a specified source Worker [1][2]. - transferred: A state declared by the source Worker (as a tombstone), indicating that the namespace has been moved to a specified target Worker [1][2]. Configuration Structure - exports: In the context of Durable Objects migrations, the exports field is a map (typically keyed by the Durable Object class name) used to define these lifecycle states and associated metadata [1][2]. - script_name: When a source Worker still needs to access a Durable Object after a transfer has committed, its durable_objects.bindings entry is updated to point to the target Worker using the script_name property [1][2]. Deployment Order Migrations are applied at deployment based on the migration list specified in the wrangler configuration file (wrangler.toml or wrangler.json) [3][4]. The handoff process requires careful sequencing: 1. Target Worker: Declares an expecting-transfer entry naming the source Worker. It should not yet add a durable_objects.bindings entry for the class to avoid routing conflicts [1][2]. 2. Source Worker: Commits a deployment that changes the Durable Object definition to a transferred tombstone, which names the target Worker [1][2]. The actual handoff occurs when this source Worker deployment lands [1][2].
Citations:
🏁 Script executed:
Repository: kentcdodds/kody
Length of output: 1338
Document the Durable Object transfer protocol.
At lines 60-63, define the target
expecting-transferexport, sourcetransferredtombstone, deployment order, binding consumers,script_namerouting, and preservation of existing per-user IDs such asRUN_LOG.idFromName(userId). Require preview reads from existing objects before production.🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 44258b3: the ADR now specifies DO moves use Wrangler's cross-script transfer mechanism (receiving worker declares the incoming transfer; the source worker's deploy commits the handoff) rather than implying the plain migrations array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.