Skip to content

Remove the cold-start install by trimming packages a calculation never imports - #46

Merged
MaxGhenis merged 5 commits into
mainfrom
claude/cold-start-install
Nov 5, 2026
Merged

MaxGhenis merged 5 commits into
mainfrom
claude/cold-start-install

Conversation

@MaxGhenis

@MaxGhenis MaxGhenis commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Production's /api/series returned 500 on every request for 28 minutes on 5 November, after two shorter bursts, with "No space left on device". A cold-start install that stops partway leaves too little room in /tmp for its retry. This PR removes the packages a household calculation never imports. The function then fits inside Vercel's bundle, so no install runs at cold start and /tmp stays empty.

No engine version and no displayed number changes: 48 requests to previews of main and of this PR returned identical responses.

The 5 November failures

What the logs show

Production's /api/series returned 500 in these windows on deployment dpl_H3rLyUj95jxUFREHUS66FH7GDFDE, which went live on 29 October, so no deploy caused them.

Window (UTC) /api/series
00:01:07 to 00:02:15 6 requests, all 500
00:46:43 to 00:47:09 6 requests, all 500
00:51:30 to 01:19:07 18 requests, every one 500
01:28:19 onward cold starts install successfully, every request 200

All 30 failing rows log the same error before any CliffWatch code runs. The failing package varies (pandas, blosc2, plotly, tables, h5py, numpy, pydantic-core), and each attempt writes to a new temporary directory:

Runtime dependency installation failed.
Command: /var/task/_uv/uv sync --inexact --active --frozen --no-dev --no-editable --no-install-project --no-build --no-cache --no-progress --link-mode hardlink --no-install-package policyengine-us
Exit code: 1
Installing runtime dependencies...
...
error: Failed to download `blosc2==4.14.1`
cause: Failed to extract archive: blosc2-4.14.1-cp311-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
cause: I/O operation failed during extraction
cause: failed to write to file `/tmp/.tmpGQToiv/.tmpUxEGjt/blosc2/groupby_ext.abi3.so`: No space left on device (os error 28)
...
Python process exited with exit status: 1. The logs above can help with debugging the issue.

Why an instance gets stuck

The runtime's source and preview deployments of main's tree show each step.

  1. The function exceeds the bundle limit. The builder counts main's functions at 307 to 343 MiB. Past 225 MiB, Vercel's Python runtime installs the public packages into /tmp when an instance starts, before any app code runs (vc_init.py). A failed install exits the process, and the request returns 500.
  2. An init that passes 10 seconds stops and restarts. AWS documents the 10-second limit and the retry under the function's own timeout. A probe function with a 45-second import showed the same thing on dpl_fBK2zhv7sdoyGi2D3D81eJKSc6cs: the first process logged four ticks and stopped between 9.75 and 10.75 seconds after it started, and a second process then started on the same instance and finished the import.
  3. /tmp survives the restart, and nothing removes a stopped install's files. The runtime only checks for its own completion marker, and each attempt creates a new temporary directory.
  4. /tmp has room for one install, not one and a half. It holds about 513 MiB, and each file takes whole 4 KiB blocks. main's install peaks at 406 MiB and takes 31,436 files and directories, so block rounding costs it far more than its 308 MiB of apparent size suggests.

So a stop that leaves about 186 MiB behind, 1.5 seconds into this test's install, leaves too little room for any retry, and every later request on that instance fails until the platform replaces it. A probe route stopped main's install at each of these times, then ran it again twice:

Stopped after In use in /tmp when the retry starts Retries
0.5 s 21.1 MiB succeed, peak 427 MiB
1 s 90.8 MiB succeed, peak 496 MiB
1.5 s 186.5 MiB both fail: no space
2 s 266.0 MiB both fail: no space
2.5 s 306.7 MiB both fail: no space
3 s 347.1 MiB both fail: no space
4 s 391.5 MiB both fail: no space
5 s 393.4 MiB both fail: no space

Reproducing production's failure

Slowing main's install in the platform's own init path reproduces production's error and its per-function pattern. Adding [tool.uv] concurrent-downloads = 1 to main's tree (dpl_DcSE4aRRtTVfmj9H5TLNXGRJwm39) made /api/probe return 500 FUNCTION_INVOCATION_FAILED in 3.2 to 4.1 seconds on all three rounds and /api/calculate return 500 in 3.88 seconds. The probe's log rows carry production's error text:

Downloading plotly (18.2MiB)
Downloaded plotly
Downloading numpy (16.0MiB)
error: Failed to download `numpy==2.5.4`
... No space left on device (os error 28)
Runtime dependency installation failed.
Python process exited with exit status: 1.

On the same deployment /api/series, /api/metadata and /api/households returned 200, and a later /api/probe request returned 200 too. Some functions stick and others don't, as in production.

What nobody has observed

No log shows a production install crossing the limit. A stopped init with no request attached writes no request log row, so the logs can't show it. uv's figures on 12 successful production cold starts come to 3.6 to 4.3 seconds, and main's tree reached app code after about 5.75 seconds on a preview, so an install needs to take roughly twice as long to cross. The same install took 8.2 to 8.8 seconds when a probe route ran it inside a request on an unslowed preview, so that much spread has precedent, though a request differs from an init. The logs don't show what slowed those installs or why the third window ended. The explanation above rests on the matching error text, the measured table of stops and the reproduction.

The fix

override-dependencies in pyproject.toml removes four packages and whatever only they require, 23 in all. The runtime set goes from 84 packages to 61, with nothing added and no version changed:

  • plotly, with tenacity
  • tables, with py-cpuinfo and blosc2, and through blosc2: httpx (with httpcore, h2, hpack and hyperframe), pydantic (with pydantic-core, annotated-types and typing-inspection), rich (with markdown-it-py and mdurl), msgpack, ndindex and threadpoolctl
  • hf-xet
  • jedi, with parso

A household calculation needs none of them. A trace of the code behind all four routes saw two import attempts, h2 and jedi, each inside an except ImportError, and none of the 23 loaded. plotly and tables keep importers in the engine, on chart and dataset paths: only policyengine_us/tools/documentation.py imports the charts module, nothing imports that file, and pd.HDFStore appears on dataset paths, while the app builds only Simulation(situation=...). The full suite and every preview route pass without them.

That takes every function from 307 to 343 MiB down to 188.74 MiB by the builder's own count (VERCEL_BUILDER_DEBUG=1, dpl_9DvwGdF4wLSeZkocADfCVoz3ZxTJ), which leaves 36 MiB under the 225 MiB limit. At or under that limit, shouldEnableRuntimeInstall() in the Python builder returns false, every dependency ships in the zip, and the builder fills the zip's remaining room up to 220 MiB with bytecode for the modules it ranks highest.

main (dpl_fBK2zhv7sdoyGi2D3D81eJKSc6cs) This PR (dpl_BKG12Jgi9r3zn8jvjVsAwXEs3xhV)
Function, by the builder's count 307 to 343 MiB 188.74 MiB
Install at cold start yes: 3.6 to 4.3 s of uv in production none
/tmp after a cold start 389.3 MiB, 31,436 files and directories 0.0 MiB
Process start to app import about 5.75 s 0.17 s
A stopped init leaves up to 393 MiB; from about 186 MiB, every retry fails nothing
Bytecode in the zip 150.1 MiB, covering the install too 31.3 MiB
Files in /var/task, apparent size 221.7 MiB 221.2 MiB
First /api/series after about 8 idle hours 34.54 s 32.82 s
First /api/metadata after about 8 idle hours 7.68 s 1.45 s

The preview GitHub built from this head (dpl_8BUGTmPVJepLwSbxVFSyUuhwKmnR) returned 200 on every route on all three rounds, and its logs show no install. Production's deployments come from the same GitHub integration, on main, and the project sets no environment variables for any target.

The two settings that break main's tree do nothing here, because nothing installs: concurrent-downloads = 1 (dpl_CNmrak8rMMWxA1gMuEcZH3hvU81a) and compile-bytecode = true (dpl_3A1dSwiBiMEx97wuXLf53h6S2dca) both leave /tmp at 0.0 MiB and every route at 200 on all three rounds.

What it costs

The zip holds 31.3 MiB of bytecode instead of 150.1 MiB, so loading the engine takes longer: a probe route that imports the engine and runs two series took 26.1 and 28.49 seconds on main, and 33.33 and 37.92 seconds here. Skipping the install saves about 5.6 seconds of that, and first requests come out about even: a mean of 36.6 seconds over 5 readings on main, against 34.6 and 36.3 seconds over 5 and 6 readings on the two builds here. /api/metadata, which doesn't load the engine, gets faster.

First request after about 8 idle hours main This PR with probe routes This PR as GitHub built it
/api/series 34.54 s 32.82 s 32.46 s
/api/calculate 35.64 s 32.88 s 39.62 s
/api/households 37.15 s 35.69 s 42.47 s
/api/metadata 7.68 s 1.45 s 1.68 s

The probe column comes from dpl_BKG12Jgi9r3zn8jvjVsAwXEs3xhV, built from 07521ec with probe routes, and the GitHub column from dpl_DJ9a7qURtDSLUoxzV31gRUo19Lvn at cb14ce5. Commits since 07521ec changed only the README, the tests and comments, and dropped the changelog fragment.

After 45 idle minutes, the first /api/series took 39.6 seconds on main and 38.38 here, and /api/calculate 36.05 and 33.21. Single readings like these vary by several seconds, as the two columns for this PR show.

The slowest of these readings, 42.47 seconds, leaves 17.5 seconds under the 60-second limit in vercel.json. A slowdown that outran that margin would time out the request in hand, and with nothing in /tmp the next request would start clean. That follows from the design; no preview has tested it.

If the function grows

Growth costs a little first-request time before it brings the install back. The builder compiles bytecode only into the room under 220 MiB, so the fill shrinks one for one as the function grows. With no room left (dpl_FSq4JGwxTPEq85VQXarQh9ZNBtYz, padded to 220.32 MiB by the builder's count, and "skipping bytecode precompilation" in its log), first requests took 38.39 to 41.53 seconds and every route returned 200. Fresh deployments with the fill took 33.36 to 35.29 seconds (dpl_DJ9a7qURtDSLUoxzV31gRUo19Lvn), 30.75 to 41.26 seconds (dpl_4pxD99DgedJsHkyf8qkeMWXvogCj) and 31.60 to 34.26 seconds (dpl_H9dbad3MWgmAaRCZ2kybebDoeztm). So losing the fill adds about 5 seconds on average, 3 to 7 across these three, and leaves those requests inside the 60-second limit. No preview without the fill has sat idle. Padding adds no code to load, so a bump that grows the engine would add its own load time too.

Past 225 MiB the install comes back. tests/test_deploy_footprint.py fails at that point, so the bump's author decides whether to trim again or accept the install. The trimmed set would install at 173.0 MiB in 4 KiB blocks, so a complete install fits on top of a stopped one (364 of 513.4 MiB, with peak overhead), and a second test holds that line.

main has no required checks, so a red run warns the author and can't block a merge. Making python a required check would close that gap; this PR leaves repo settings alone.

cliff-watch#45 keeps a smaller install. Its engine's dependencies come to 262 MiB even with this trim, and that PR holds its measurements.

No number moves

  • compare_deployed.py sent the same 48 requests to main's preview and this PR's: 4 households across 6 states, on /api/series and /api/calculate. Every response matched, and /api/metadata matched too. The same 48 requests matched again on dpl_H9dbad3MWgmAaRCZ2kybebDoeztm, the preview GitHub built from 5bcb048, which differs from this head by one README line. One of them needed a retry: main's preview logged a 200 for it, but no answer reached the client within 320 seconds.
  • Locally, the 17-household impact run on this lock equals main's exactly.
  • pytest on CI at this head: 20 passed, on the same engine versions, with all 5 footprint tests running on Linux wheels.

Invariants

These hold for the locked dependency set, and tests/test_deploy_footprint.py tests each one:

  • The function fits the bundle, so no install runs at cold start.
  • The trimmed packages stay out of the function.
  • Packages with a URL source would stay in the zip if an install came back.
  • A complete install, if one came back, fits on top of the leftovers of a stopped one.
  • The test's walk over uv.lock agrees with uv export on what the function contains.

The size tests skip on a Mac, whose wheels differ, and run on CI's manylinux wheels, and CI's job summary shows what they measure. They read the packages and the repo's tracked files fresh on each run. BUILDER_EXTRA_MIB stands for the rest of the builder's count, chiefly the built frontend, and comes from one comparison with its debug log, so the 188.8 MiB in CI's summary sits within 0.1 MiB of the builder's 188.74 MiB by construction. Each test also fails if its size walk finds implausibly little.

Landing

  • A merge to main deploys production.
  • Then read production's build log: it should show no "exceeds the standard size" line.
  • Then send a real request to each of the four routes and read the logs: every route should return 200 with no "Installing runtime dependencies" line.
  • If production misbehaves, vercel rollback dpl_H3rLyUj95jxUFREHUS66FH7GDFDE restores the previous build.

axiom: n/a: cliff-watch deployment packaging; no policy encoding change

🤖 Generated with Claude Code

Vercel's runtime installs public packages into /tmp when an instance starts.
The platform stops an init at about 10 seconds and starts it again on the same
instance with /tmp intact, and a stopped install leaves its files behind. main's
install peaks at 406 MiB of about 513, so after most stops no retry fits and every
request on that instance fails with "No space left on device" until the platform
replaces it. That matches the /api/series 500s on 5 November.

Remove packages a household calculation never imports, and add a test that fails
when a complete install stops fitting on top of a stopped one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
cliff-watch Ready Ready Preview Nov 5, 2026 4:59pm UTC

Request Review

With the unused packages gone, the function totals 188.74 MiB by the
builder's count, inside the 225 MiB it bundles, so the runtime installs
nothing at cold start. The README, the pyproject comment and the changelog
still described a smaller install. A new test fails when the function passes
225 MiB, and the install-fits-twice test stays as the guard for that case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MaxGhenis MaxGhenis changed the title Keep the cold-start install small enough to survive a stopped attempt Remove the cold-start install by trimming packages a calculation never imports Nov 5, 2026
An independent review of this PR found no blocking problem and a set of smaller
gaps, which this addresses:

- The size test now reads the repo's tracked files, so a large committed file
  counts as it does for the builder, and counts cliff_watch/ twice, as the
  builder did on a padded preview.
- Both size tests fail if their walk finds implausibly little, and CI's job
  summary shows what they measure.
- The pyproject comment names the right path for tables and says what leaves
  with it. The README says what happens to the public packages, what losing the
  bytecode fill costs (measured on a preview), and that main requires no checks.
- The changelog fragment goes: nothing in this repo reads changelog.d.

No dependency or runtime file changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A second fresh deployment with the fill took 30.75 to 41.26 seconds on first
requests, so the gap to the preview without it runs from 3 to 5 seconds on
average.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n overturn

A third fresh deployment with the fill took 31.60 to 34.26 seconds on first
requests, which took the average gap to 7 seconds on that set. The gap varies
with each set of readings, so the README states its size in words.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MaxGhenis

Copy link
Copy Markdown
Contributor Author

Review record: an independent Opus 5.5 agent approved 129789569f, then asked for one wording change in the README and the body at 5bcb048 and made its approval conditional on that change, which 98d51223 applies. GPT-6.1 Sol's review stayed queued for capacity, so this landing rests on the Opus review. All checks pass at 98d51223, with 20 tests passing and all 5 footprint tests running.

@MaxGhenis
MaxGhenis marked this pull request as ready for review November 5, 2026 17:03
@MaxGhenis
MaxGhenis merged commit 9b4aa76 into main Nov 5, 2026
5 checks passed
@MaxGhenis

MaxGhenis commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Production's logs for dpl_H6Rjpwe2JtXScjyCASPbaikD4RZN from 17:03 to 19:40 UTC hold 47 requests across /api/series, /api/calculate, /api/metadata and /api/households. All 47 returned 200, and none mention Installing runtime, No space left or FUNCTION_INVOCATION.

/api/metadata still reports policyengine-us 1.755.5, policyengine-core 3.32.1 and spm-calculator 0.3.1, so this PR left the engine alone.

Traffic stayed light in that window, so heavier load remains untested.

MaxGhenis added a commit that referenced this pull request Nov 6, 2026
Main now carries #40, whose files match this branch's base, and #46, which
removes the cold-start install on main's engine. This branch's engine takes
the function past Vercel's 225 MiB bundle again, so the port keeps its
install-fits-twice test and README section, adding #46's 4 KiB block wording,
its note that main requires no checks, the size walk's sanity floor and the
CI summary line. The pyproject comment now names policyengine-us as the
package that brings in tables, as uv.lock shows. The cold-start changelog
entry becomes a change from main rather than a fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MaxGhenis added a commit that referenced this pull request Nov 7, 2026
README.md conflicted: keep main's Cold-start install section and this
branch's County from ZIP section, in that order. Everything else merged
cleanly. The next commit adapts this branch to #40's Vitest tests,
payload invariants and variable-graph snapshot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MaxGhenis
MaxGhenis deleted the claude/cold-start-install branch November 8, 2026 19:48

This branch was successfully deployed

1 active deployment
Preview — 98d51223 Deployed Nov 5, 2026 by vercel[bot]
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