Skip to content

deploy: add staged server bundle to shrink the GAE install - #809

Merged
bpowers merged 3 commits into
mainfrom
deploy/staged-server-bundle
Jun 27, 2026
Merged

bpowers merged 3 commits into
mainfrom
deploy/staged-server-bundle

Conversation

@bpowers

@bpowers bpowers commented Jun 27, 2026

Copy link
Copy Markdown
Owner

Why

pnpm deploy:web runs gcloud app deploy from the workspace root, so the App Engine instance's pnpm install walks pnpm-workspace.yaml and installs every workspace package's dependency closure — React, slate, radix, rspress, vite, jest, @rsbuild/* — ~590 MB / 1171 packages, none of which the Express server needs at runtime.

App Engine standard always reinstalls from the deployed package.json + lockfile and has no vendored-node_modules escape hatch (confirmed against Google's docs), so the only lever that shrinks the install is the deployed manifest. The pnpm10 NODE_ENV=production devDep bug (buildpacks#591) is secondary: even a perfect --prod install of the whole workspace would still pull React/slate/radix/rspress/vite, because those are runtime deps of other packages. The real cost is installing the whole workspace at all.

What

Adds pnpm deploy:web:staged, which deploys from a self-contained deploy-staging/ whose package.json is exactly the server's prod closure — ~80 MB / 230 packages.

install on instance packages upload payload
deploy:web (today) 590 MB 1171 whole repo
deploy:web:staged 80 MB 230 28.7 MB

How it works

  • scripts/deploy-web-staged.sh mirrors deploy-web.sh (validate → clean → build → deploy:assemble → verify-deploy-build.sh), then runs scripts/build-deploy-staging.mjs and gcloud app deploy deploy-staging/app.yaml.
  • @simlin/core and @simlin/engine are not published to npm, so a pnpm deploy version-rewrite (workspace:* → 2.0.0) would 404 on the instance. Instead they are vendored into deploy-staging/vendor/ and referenced via file: deps; core's transitive @simlin/engine ref is rewritten too. The wasm keeps loading unchanged (the engine stays a real package dir; wasm.node.js resolves core/libsimlin.wasm via __dirname).
  • Only the full server-side libsimlin.wasm is vendored; the browser variants are deliberately omitted (plain Node require never selects the module/browser export condition), keeping the 4.8 MB slim wasm off the server image.
  • A pnpm-lock.yaml is generated for the minimal manifest with peer settings pinned (--config.auto-install-peers=true --config.strict-peer-dependencies=false) so the lockfile doesn't depend on the operator's ambient .npmrc.
  • The pure transforms live in scripts/deploy-staging-manifests.mjs (unit-tested via pnpm test:scripts); the builder self-verifies the artifact (full wasm with simlin_project_render_png, substituted index.html, no residual workspace: refs) and guards against an app.yaml entrypoint that wouldn't resolve in the flat layout. Unknown workspace deps and peerDependencies fail loud rather than ship a broken manifest.

Testing (local, end-to-end)

  • pnpm test:scripts: 33 unit tests green.
  • Built + assembled, ran the builder, then simulated the GAE instance: pnpm install --frozen-lockfile --prod in a copy outside the repo (no workspace/.npmrc ancestor) installs cleanly from the generated lockfile (74–80 MB / 230 pkgs); the full wasm materializes at the __dirname path; all server requires (incl. @simlin/core/common) resolve.
  • node lib/index.js boots (WASM module initialized successfully) and serves / → 200, /api/user → 401, /static/js/sd-component.js → 200, /robots.txt → 200.
  • Adversarially reviewed twice; no material findings outstanding.

Not yet verified / why deploy:web stays the default

The literal gcloud app deploy upload and the nodejs24 buildpack behavior can't be tested locally: whether it honors packageManager: [email protected] via corepack and accepts the frozen lockfileVersion. Run pnpm deploy:web:staged --no-promote, confirm the instance ran a successful frozen pnpm install, then the post-deploy smoke test — before this replaces deploy:web. deploy:web remains the proven fallback (untouched).

Relates to tech-debt #39 (and incidentally puts the upload well under GAE's 10k-file cap, #695, once this becomes the default).

`pnpm deploy:web` deploys from the workspace root, so App Engine's
buildpack reinstalls every workspace package's dependency closure on the
instance (~590 MB / 1171 packages) -- React, slate, radix, rspress, vite,
jest -- none of which the server needs at runtime. App Engine standard
always reinstalls from the deployed package.json + lockfile and has no
vendored-node_modules escape hatch, so the only lever is the deployed
manifest; the pnpm10 devDep bug is secondary, since the dominant cost is
installing the whole workspace at all.

Add `pnpm deploy:web:staged`, which assembles a self-contained
deploy-staging/ whose package.json is exactly the server's prod closure
(~80 MB / 230 packages) and deploys that. The two unpublished workspace
packages (@simlin/core, @simlin/engine) are vendored and referenced via
file: deps, since a registry install would 404 on them; core's transitive
@simlin/engine ref is rewritten too. A pnpm-lock.yaml is generated for the
minimal manifest with peer settings pinned so the lockfile does not depend
on the operator's ambient .npmrc.

The pure transforms (scripts/deploy-staging-manifests.mjs) are unit-tested
via pnpm test:scripts; the builder self-verifies the artifact (full wasm
with the simlin_project_render_png export, substituted index.html, no
residual workspace: refs) and guards against an app.yaml entrypoint that
would not resolve in the flat staging layout. Proven locally end to end:
an isolated frozen-lockfile install boots the server and serves /, the
embed component, and static assets. deploy:web stays the default and
fallback until a real gcloud --no-promote test confirms the nodejs24
buildpack accepts the result.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 27c9063ffb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +187 to +189
'install',
'--lockfile-only',
'--ignore-workspace',

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve checked-in pins for staged deploys

In staged deploys this runs in a freshly deleted deploy-staging/ and no committed root pnpm-lock.yaml is copied; I checked pnpm install --help, where --ignore-workspace treats the install as a normal non-workspace install and --lockfile-only only writes the lockfile. That means pnpm deploy:web:staged resolves the semver ranges in the generated package.json against the registry at deploy time, while the build/tests just ran against the checked-in workspace lockfile, so a new compatible direct or transitive release can be deployed untested even with a frozen install on GAE. Please derive the staged lock from the committed lockfile, or otherwise pin the same versions, before deploying.

Useful? React with 👍 / 👎.

@codecov

codecov Bot commented Jun 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.87%. Comparing base (6931636) to head (21d5106).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #809      +/-   ##
==========================================
- Coverage   90.87%   90.87%   -0.01%     
==========================================
  Files         224      224              
  Lines      143133   143133              
==========================================
- Hits       130072   130071       -1     
- Misses      13061    13062       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@claude

claude Bot commented Jun 27, 2026

Copy link
Copy Markdown

Code Review:

Reviewed scope: scripts/build-deploy-staging.mjs, scripts/deploy-staging-manifests.mjs, scripts/deploy-web-staged.sh, scripts/tests/deploy-staging-manifests.test.mjs, docs/package.json wiring.

No actionable bugs identified.

The pure transforms in deploy-staging-manifests.mjs are well-isolated and thoroughly tested (peer-dep rejection, optional-dep handling, no-mutation invariants, unknown-workspace-dep failure). The build script has appropriate fail-loud guards: missing source artifacts, the entrypoint compatibility check (correctly handles trailing whitespace via the non-greedy capture + trim pattern), the wasm export check (catches accidental shipping of the slim browser wasm), and the residual-workspace-protocol assertions in both build and verify paths. The flat layout (lib/ at staging root) is consistent across the package.json start script, the assertCompatibleEntrypoint guard, and the wasm.node.ts __dirname resolution. The two unpublished workspace packages are correctly vendored with their transitive workspace refs rewritten to file:../ siblings, and the files field is dropped to prevent the inherited list from masking the deliberately-minimal vendor contents.

Overall correctness verdict: correct.

The patch is additive (new scripts, npm scripts, doc/tech-debt updates), keeps deploy:web as the proven fallback per the PR description, and has been verified end-to-end locally against an isolated install simulating the GAE environment. The remaining unknown (whether the nodejs24 buildpack honors corepack + the frozen lockfile) is explicitly called out and gated on a real --no-promote test before flipping the default.

The staged deploy generated its pnpm-lock.yaml in a freshly deleted dir
with no lockfile seed, so `pnpm install --lockfile-only` re-resolved the
semver ranges against the registry at deploy time. The build and tests run
against the committed root lockfile, so a newer compatible release (direct
or transitive) could be deployed untested -- even though GAE then does a
frozen install, because that frozen lockfile was itself resolved fresh.
Flagged by Codex review on #809.

Seed the staging lockfile from the committed root pnpm-lock.yaml: copy its
packages/snapshots resolution cache with the importers dropped, so pnpm
reuses the already-locked, CI-tested versions and resolves only the new
file: vendored packages, then prunes to the staging closure. Dropping
importers is what triggers the prune; keeping them balloons the lockfile
back to the whole-workspace closure. A downgrade test confirmed the seed
pins (a seeded [email protected] is kept, not bumped to the newest 8.2.0).

A post-generation guard (untestedPackages) fails the build if any registry
version in the staging lockfile is absent from the root lockfile -- the
explicit guarantee that the deploy only ships versions the workspace
install actually resolved and tested.
@bpowers

bpowers commented Jun 27, 2026

Copy link
Copy Markdown
Owner Author

Addressed the Codex P2 (lockfile re-resolution) in 66b8ffe.

The staging pnpm-lock.yaml is now seeded from the committed root lockfile (its packages/snapshots resolution cache with importers dropped) before pnpm install --lockfile-only, so pnpm reuses the already-locked, CI-tested versions and only resolves the new file: vendored packages, then prunes to the staging closure. A downgrade test confirmed it pins rather than re-resolves (a seeded [email protected] is kept, not bumped to the newest 8.2.0). A post-generation guard (untestedPackages) additionally fails the build if any registry version in the staging lockfile is absent from the root lockfile, so an untested version can never ship. Verified end-to-end: an isolated pnpm install --frozen-lockfile --prod from the seeded lockfile still boots the server and serves all routes.

@bpowers

bpowers commented Jun 27, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 66b8ffe6b9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread .gitignore
/src/app/build
/src/app/build-component
/src/app/test-results
/deploy-staging

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Exclude deploy-staging from root gcloud uploads

Adding deploy-staging only to .gitignore does not protect the fallback deploy path: the root .gcloudignore explicitly does not import .gitignore, and deploy-web-staged.sh intentionally leaves this directory behind for inspection. After a pnpm deploy:web:staged --no-promote smoke test, a subsequent fallback pnpm deploy:web from the repo root will upload the whole staged artifact (including the copied app.yaml, vendor packages, lockfile, and public bundle) unless /deploy-staging/ is also ignored by the root .gcloudignore or the staged directory is removed during cleanup.

Useful? React with 👍 / 👎.

deploy-web-staged.sh leaves deploy-staging/ in place after a staged deploy
so a --no-promote smoke test can inspect it, and the .gitignore entry keeps
it out of git -- but the root .gcloudignore deliberately does NOT import
.gitignore (so gitignored build output like src/*/lib still uploads). So a
subsequent fallback `pnpm deploy:web`, which uploads from the repo root,
would sweep the ~28 MB leftover staged artifact into the upload -- including
a copy of app.yaml carrying the session secret -- and eat into GAE's
10k-file budget.

Exclude /deploy-staging/ in the root .gcloudignore. The staged deploy
uploads from deploy-staging/ with its own .gcloudignore, so this entry is
inert for it. Flagged by Codex review on #809.
@bpowers

bpowers commented Jun 27, 2026

Copy link
Copy Markdown
Owner Author

Addressed in the latest commit: /deploy-staging/ is now excluded in the root .gcloudignore so a fallback pnpm deploy:web from the repo root won't re-upload the leftover staged artifact (or its app.yaml copy). The staged deploy uploads from deploy-staging/ with its own .gcloudignore, so the entry is inert for it.

@codex review

@claude

claude Bot commented Jun 27, 2026

Copy link
Copy Markdown

Review

Reviewed all 8 files: the two new scripts, the test file, the shell driver, and the doc / package.json updates.

Findings

No actionable bugs identified.

The change is well-scoped and defensively constructed:

  • The pure transforms (deploy-staging-manifests.mjs) are fully unit-tested (33 cases), including non-mutation, peer-rejection, unknown-workspace-dep rejection, and lockfile seed/prune semantics.
  • The builder self-verifies several load-bearing invariants before exit: public/index.html template substitution, full wasm presence with the simlin_project_render_png export (rather than just size), no residual workspace: protocol in either the staging or vendored-core manifest, and the exact file:./vendor/{core,engine} rewrites.
  • seedLockfileFromRoot correctly drops importers (so pnpm prunes to the staging closure) while keeping packages/snapshots/settings; the untestedPackages belt-and-suspenders check catches any version drift past the CI-tested workspace lockfile.
  • Edge cases handled: the regex-based entrypoint check refuses both src/server-rooted entrypoints and any entrypoint missing lib/index.js; the wasm export check uses a literal-byte search that reliably distinguishes the slim browser build from the full server build; assertNoWorkspaceProtocol is reapplied at multiple stages (block-build, manifest-build, post-write verify); the staging-dir argument cannot accidentally equal REPO_ROOT.
  • rewriteVendoredManifest deliberately strips files, scripts, and devDependencies, with comments explaining why — the files removal avoids a stale package-pack include list once the builder copies an explicit minimal subset (e.g. omitting the browser wasm).
  • The shell driver mirrors deploy-web.sh's trap-based cleanup shape so the tracked public/ symlinks are restored on success, failure, or Ctrl-C.

Minor observations that are below the threshold for flagging individually:

  • assertCompatibleEntrypoint rejects valid-but-unusual entrypoints like entrypoint: npm start (which would still work, since the staging package.json defines scripts.start). Conservative is reasonable here.
  • The hardcoded file:./vendor/core/file:./vendor/engine equality check in verify() would silently miss a manifest built with a non-default vendorDir. The build script never passes one, so this is a non-issue in practice.

Overall correctness verdict

Correct. The changes are additive (new script + new package.json entry; deploy:web is untouched as the fallback), thoroughly tested, and the PR description explicitly gates promotion behind a real --no-promote gcloud test — which is the right posture for a deploy path that can't be exercised in CI.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 21d51069b7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@claude

claude Bot commented Jun 27, 2026

Copy link
Copy Markdown

Reviewed the PR end-to-end (manifest transforms, lockfile seeding/pruning logic, assertCompatibleEntrypoint regex, vendored package layout vs. the engine's __dirname-based wasm resolution, the shell cleanup trap, and the verify function). The design is sound and the test coverage for the pure transforms is good. I didn't find any blocking bugs.

A few minor observations (not blocking):

  • seedLockfileFromRoot whitelists {lockfileVersion, settings, packages, snapshots}. The current root lockfile only carries those, so this is correct today. If catalogs / overrides / patchedDependencies are ever added to the workspace, they'll need to be threaded through too (the untestedPackages drift guard would surface mismatches loudly, so this is fail-safe rather than silent).
  • assertCompatibleEntrypoint matches a single-line entrypoint: and requires the literal lib/index.js. Valid GAE entrypoints like npm start would be rejected, but the error message already steers the user to the right form, so this is fine for the audience.

Overall correctness: correct.

@bpowers
bpowers merged commit acb763c into main Jun 27, 2026
15 checks passed
@bpowers
bpowers deleted the deploy/staged-server-bundle branch June 27, 2026 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant