Public: anyone can read this; the crew can change it.

A deploy's build as a cluster Job #1906

Parked opened this
Closed without merging

A deploy's install step can now run as a cluster Job, and the built tree comes back to the forge through a token-gated door. I ran tsc --noEmit, the gate and the placement and apps tests (the new suite included) locally, and all pass. The forge's own check runs on this push.

Notes

stopped parked by the crew

opened this

A deploy's install step can now run as a cluster Job, and the built tree comes back to the forge through a token-gated door. I ran tsc --noEmit, the gate and the placement and apps tests (the new suite included) locally, and all pass. The forge's own check runs on this push. It has not run against a real cluster, and none of the new tests boot the runtime, so the wiring in src/apps/runtime.ts is covered only by the type check.

The issue pointed at src/cluster/lane.ts and a GET /-/lane/:id/tree door. Neither exists on this branch, so I followed the pattern of src/cluster/jobs.ts and src/checks/job_step.ts instead.

What changed

  • Setting: FORGE_BUILD_PLACEMENT=job with FORGE_BUILD_IMAGE, FORGE_BUILD_CLAIM, FORGE_BUILD_NODE_SELECTOR and FORGE_BUILD_TOLERATIONS. It reuses the checks parser through a prefix argument in src/checks/placement.ts. A malformed value leaves process in place, and the app itself still runs in the forge.
  • The Job (src/apps/build_job.ts): the forge copies the checkout to <data>/builds/<run>, and the Job installs there. It then pipes tar to curl, which POSTs to /-/build/<run>/tree. The Job gets the same hardening as a check and a forge-build=true label. Its environment is only CI, the door URL and the token. Install output goes into the deploy log, and the node the pod ran on is logged after the output.
  • The door (src/apps/build_door.ts, route in src/http/server.ts): a one-time bearer token that expires, compared in constant time, taking one tree of up to 512 MiB. Unknown id, expired door and wrong token all get the same 403. No cookie is read, so there is nothing for a cross-origin page to use.
  • Unpacking (src/apps/tar.ts): an in-process tar reader, with no tar binary involved. It refuses absolute or .. names and any write through a symlink, and it skips devices. Without these checks, a hostile install could write outside the checkout.
  • Wiring: StartInput.install lets the runtime swap in the Job for the install step. Warm starts and wakes never build, so they never use a Job.
  • Tests and docs: test/build-placement.test.ts uses a stand-in kube client. It covers a successful build, a failed install, a missing tree, a hostile tree, the door's token and size rules, and the tar reader against hostile archives. docs/APPS.md has a "Build placement" section and an env-table row, and the test is listed in scripts/gate.ts.

Left out

  • Egress on the build Job is not enforced by the forge, as with checks. A NetworkPolicy on forge-build=true has to do it.
  • The test step still runs in the forge. Only install moves to the Job.
  • The build timeout is fixed at 20 minutes; there is no per-app setting.
  • The Job's image must have sh, tar and curl and must be able to reach the forge's URL. The claim's ownership must let the non-root pod write to the build directory. Both are documented.

To try it: set the four FORGE_BUILD_* variables on a forge running in a cluster and push an app that has a bun install. The deploy log shows $ bun install (as a cluster job), then tree back from the job: N entries. Without the setting, nothing changes.

🤖 Generated with Claude Code

Closes #1427

1 earlier comment · 1 event
Changes requested — before the latest push

The door is well built: the token is checked before any body is read, the cap counts the bytes actually read, the comparison runs in constant time against a hash, the door takes one tree once, and no cookie is read. The tar reader refuses absolute and .. names, writes through links and devices, and the tests cover hostile archives. Three things stop it landing:

  1. Fix first: relative symlinks in the checkout break. cpSync(input.dir, work, { recursive: true }) leaves verbatimSymlinks false, so Node/Bun rewrites each relative link to an absolute path into the source checkout. I checked: a link sub/rel -> ../target copies as /…/src/target. Inside the pod that path does not exist (only work is mounted), so an install that follows a repo symlink (workspaces, shared config) fails. The tar then brings the absolute link back over the checkout, where it points at the forge's host path, not the cell's. Pass verbatimSymlinks: true, and add a test with a relative link in the checkout that still reads ../target after the round trip.
  2. The wiring has no test. process.ts routes to input.install and runtime.ts builds it through buildInstall, but no test drives startProcess or the runtime with buildPlacement/buildJob set. You could delete the hook and the suite would still pass, and RuntimeOptions.buildPlacement/buildJob (seams that exist for tests) are never used. Add one test that starts an environment with buildPlacement: {mode:'job',…} and a stand-in buildJob, and shows the install went to the Job, the in-cell runStep did not run, and a warm start did not build.
  3. The unpack blocks the forge. extractTar is fully synchronous over up to 512 MiB and 500k entries, all buffered in memory, on the forge's one event loop. The bytes come from a pod running repository code, so one deploy can stall every page and API request for seconds. Unpack in a Worker (or yield every N entries), and lower MAX_ENTRIES/TREE_MAX to what a real node_modules needs.

Smaller: the Job's env is only CI and the door, not the stepEnv the in-cell install gets, so an install that needs the app's step environment behaves differently under job. Either pass the same declared step env or say so in docs/APPS.md. I could not reach the preview from my sandbox; this is judged from the diff.

commented

This update addresses all three of sentinel's points and the smaller one about the Job's environment. The type check is clean. test/build-placement.test.ts and the new test/build-wiring.test.ts pass (13 tests). I did not run the whole suite.

  • Relative symlinks: cpSync in src/apps/build_job.ts now passes verbatimSymlinks: true. A new test puts sub/rel -> ../target in the checkout. It reads the link as the pod would see it and again after the tar comes back, and both read ../target. That test fails if the flag is removed.
  • Wiring tests: test/build-wiring.test.ts has two tests for this.
    • startProcess: the install hook gets the command, the in-cell runStep does not run, a warm start never calls the hook, and a failed hook fails the install stage.
    • Runtime: the runtime runs a full production deploy with buildPlacement: {mode:'job'} and a stand-in buildJob. Exactly one Job is created, and its URL carries the run id as the door id. The log says "(as a cluster job)", with no in-cell $ echo in-cell-install line. The build copy is removed afterwards.
  • Unpack on the event loop: I used the yield option rather than a Worker. extractTar is now async and yields to the event loop every 200 entries, so the forge keeps serving requests during an unpack. A single large file is still written in one synchronous call. I lowered MAX_ENTRIES from 500k to 150k and TREE_MAX from 512 MiB to 256 MiB. Those are my estimate of what a real node_modules needs, so tell me if a real install is larger.
  • Job env: the Job now gets FORGE_APP_ENV as well as CI, which is everything the in-cell install gets beyond path and locale basics. docs/APPS.md says so and gives the new caps.

I updated the existing extractTar tests to await it, and their expectations are otherwise unchanged. The only other assertion I changed is the Job's env key list, which now includes FORGE_APP_ENV.

commented

Parked by the crew: the preview's deploy failed and the decider saw no point in another try

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs