Skip to content

feat(cli): add template rebuild --refresh-envd - #1818

Closed
AdaAibaby wants to merge 1 commit into
e2b-dev:mainfrom
AdaAibaby:feat/cli-template-rebuild-refresh-envd
Closed

feat(cli): add template rebuild --refresh-envd#1818
AdaAibaby wants to merge 1 commit into
e2b-dev:mainfrom
AdaAibaby:feat/cli-template-rebuild-refresh-envd

Conversation

@AdaAibaby

Copy link
Copy Markdown

Summary

Old templates bake an old envd into their final snapshot, so a sandbox started from one resumes the old envd out of snapshot RAM. That blocks features gated on a newer envd — e.g. volume mounts need envd ≥ 0.5.14. The only way to lift such a template is a rebuild, and users have been scripting that by hand against the raw API, which has two footguns:

  • omitted specs silently default to 2 vCPU / 1024 MiB, shrinking the template;
  • registering under a fresh template id orphans the alias.

Change

Adds e2b template rebuild <template> --refresh-envd (alias rb). It calls the companion server endpoint POST /v2/templates/{templateID}/refresh-envd, which derives a new build FROM the template's own latest ready build (base layer cached, only the envd binary swapped) and inherits the source build's cpu/ram and alias in place. The command then streams build logs via the existing status endpoint until the new build is ready.

Scope is deliberately CLI-only: it calls the raw endpoint through client.api (the publish command's pattern) and polls with the already-public Template.getBuildStatus. No js-sdk / python-sdk public surface changes, so no JS/Python parity work.

Usage

$ e2b template rebuild my-old-template --refresh-envd

Refreshing envd for my-old-template (from v0.4.1); rebuilding...
  ... build logs ...

✅ my-old-template rebuilt with the current envd.

   Confirm the binary changed: start a sandbox from my-old-template,
   then run /usr/bin/envd -version.

Without the flag it refuses (the flag is the only rebuild mode today, and gates a destructive-ish rebuild behind an explicit opt-in):

$ e2b template rebuild my-old-template
Nothing to rebuild. Pass --refresh-envd to rebuild the template with the current envd.

Companion infra PR (server half)

This is the client half. The server endpoint it calls is added in e2b-dev/runtime#3624e2b-dev/runtime#3624. That PR traces the full root cause (memory-snapshot resume keeps the old envd; live-upgrade is gated at envd ≥ 0.6.12; offline-upgrade only runs on the cold-boot path) and implements the endpoint that derives the cached-source-layer build which swaps envd.

Merge order: infra#3624 first, then here.

⚠️ Preview files, blocked on the sync. spec/openapi.yml and packages/js-sdk/src/api/schema.gen.ts in this PR carry the new endpoint so the CLI typechecks and builds. But per AGENTS.md, spec/ is synced from e2b-dev/infra via Copybara at the pin in spec/infra-ref — it is not hand-authored. Once infra#3624 merges, the correct final step is: bump spec/infra-ref to the merged commit and run make codegen, which re-fetches the spec and regenerates schema.gen.ts identically, dropping the manual preview. This PR should not merge before that sync is possible.

Tests run

  • pnpm --filter @e2b/cli run typecheck — OK (new typed path resolves).
  • pnpm --filter @e2b/cli run build — OK.
  • pnpm --filter @e2b/cli run lint — 0 warnings / 0 errors.
  • pnpm --filter @e2b/cli run format — clean.
  • pnpm --filter e2b run typecheck (js-sdk) — OK (schema change doesn't break the SDK).
  • vitest run tests/commands/template/rebuild.test.ts — PASS (2 tests: refuses without --refresh-envd; command registered + documented). The rest of the CLI suite passes; the only failure in a full run is the pre-existing create.test.ts backend-integration test, which is gated on a live E2B_API_KEY and unrelated to this change.

A changeset (@e2b/cli: minor) is included per AGENTS.md.

AI assistance

AI assistance was used to trace the resume/build call chains, design the command, and draft the code and tests. A human submitter has reviewed every changed line.

Old templates bake an old envd into their final snapshot, so a sandbox started
from one resumes the old envd and can't use features gated on a newer envd
(e.g. volume mounts need envd >= 0.5.14). The only fix is a rebuild, which users
have been scripting by hand against the raw API -- easy to get wrong (specs
silently drop to 2 vCPU / 1024 MiB, alias orphaned under a fresh template id).

Add `e2b template rebuild <template> --refresh-envd`: one command that calls the
companion server endpoint, which derives a new build FROM the template's own
latest ready build (base layer cached, only the envd binary swapped) and
inherits the source specs and alias in place. Streams build logs until ready.

The command is CLI-only (raw endpoint via client.api + the public
Template.getBuildStatus for polling), so no js/python SDK public-surface change
and no parity burden.

Preview note: spec/openapi.yml and packages/js-sdk/src/api/schema.gen.ts carry
the new endpoint so this compiles, but spec/ is normally synced from
e2b-dev/infra via Copybara. Once the companion endpoint (e2b-dev/runtime#3624)
merges, bump spec/infra-ref and re-run `make codegen` -- that regenerates these
two files identically and the manual preview drops out.

Signed-off-by: AdaAibaby <shaolila@buaa.edu.cn>
@changeset-bot

changeset-bot Bot commented Sep 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9650937

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@e2b/cli Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

TASTE.md review

Checked the changed files (packages/cli/src/commands/template/rebuild.ts, index.ts, the test, changeset, spec/openapi.yml, schema.gen.ts) against TASTE.md — API shape (T-3, T-7, T-9, T-12, T-14), timeouts/defaults (T-47), errors (T-32, T-61, T-62), and SDK parity (T-1/T-2, not applicable since no SDK surface changes).

Violations: 1 (inline, T-14 — the required boolean --refresh-envd flag is really a mode selector).

Compliant / notable:

  • Poll interval in a named constant (buildStatusPollFrequencyMs) rather than a magic number — T-47.
  • Accepts template ID or alias in one argument — T-7.
  • Refusal message says what to do next — T-62.
  • Polls via public Template.getBuildStatus instead of adding new SDK surface, so no JS/Python parity work is required — T-1.

Not tied to a line:

  • spec/openapi.yml / schema.gen.ts are hand-authored previews; per AGENTS.md these must be replaced by bumping spec/infra-ref and running make codegen after e2b-dev/runtime#3624 merges (the PR already says so — flagging as a merge blocker, not a TASTE issue).
  • The for (;;) drain loop reimplements the js-sdk's internal waitForBuildFinish (packages/js-sdk/src/template/buildApi.ts). Not a TASTE violation, but if the SDK ever exposes a public wait/poll helper, the CLI should switch to it rather than carry a second copy of the terminal-status log-drain logic.

Comment thread packages/cli/src/commands/template/rebuild.ts
@mishushakov

Copy link
Copy Markdown
Member

Hey, thanks for the PR - we'll handle this one internally.

@mishushakov mishushakov closed this Sep 7, 2026
@AdaAibaby

AdaAibaby commented Sep 8, 2026

Copy link
Copy Markdown
Author

Hey, thanks for the PR - we'll handle this one internally.

@mishushakov Thanks for taking this over internally! Just a gentle follow-up — we currently have a lot of older templates running on low envd versions that require migration for volume mount compatibility. Would you have a rough timeline for when this --refresh-envd capability is expected to land in the main branch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants