fix(snapshots): preserve workspace in committed snapshot images#1055
Merged
zbigniewsobiecki merged 1 commit intodevfrom Mar 25, 2026
Merged
fix(snapshots): preserve workspace in committed snapshot images#1055zbigniewsobiecki merged 1 commit intodevfrom
zbigniewsobiecki merged 1 commit intodevfrom
Conversation
The snapshot workspace was being deleted by the worker's cleanup step before the process exited, so every `docker commit` captured an empty /workspace. On the next trigger the router logged a snapshot hit but `findSnapshotWorkspaceDir` returned null and fell back to a full clone + setup, defeating the purpose of snapshots entirely. Root cause: `cleanupAgentResources` calls `cleanupTempDir(repoDir)` in a `finally` block; the router calls `docker commit` only after `container.wait()` resolves — after the workspace is already gone. Fix: introduce a `CASCADE_SNAPSHOT_ENABLED=true` worker env var (set by the router whenever `snapshotEnabled=true`). When the worker sees this flag it skips workspace deletion so the directory is present in the image when the router commits the container. - `worker-env.ts`: add `snapshotEnabled` param → push env var - `container-manager.ts`: pass `snapshotEnabled` to both the initial and 404-fallback `buildWorkerEnvWithProjectId` calls - `cleanup.ts`: skip `cleanupTempDir` when `CASCADE_SNAPSHOT_ENABLED=true` - tests: new `cleanup.test.ts` + expand `worker-env.test.ts` Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Root cause:
cleanupAgentResourcesdeletes the workspace directory in the worker'sfinallyblock before the process exits. The router callsdocker commitonly aftercontainer.wait()resolves — after cleanup — so every committed snapshot image bakes an empty/workspace. On the next trigger the router logs a snapshot hit, butfindSnapshotWorkspaceDirreturnsnulland falls back to a full clone + npm install + setup.sh, defeating the purpose of snapshots.Fix: introduce a
CASCADE_SNAPSHOT_ENABLED=trueworker env var (injected by the router wheneversnapshotEnabled=true). The worker skipscleanupTempDirwhen this flag is present, leaving the workspace intact fordocker commit.Container-manager fallback path (
fix(snapshots): gracefully recover when snapshot image is missing) also updated to forwardsnapshotEnabledso the fallback base-image run still bakes a valid workspace when it succeeds.Changes
src/router/worker-env.tssnapshotEnabledparam → pushCASCADE_SNAPSHOT_ENABLED=truesrc/router/container-manager.tssnapshotEnabledto initial and fallbackbuildWorkerEnvWithProjectIdcallssrc/agents/shared/cleanup.tscleanupTempDirwhenCASCADE_SNAPSHOT_ENABLED=truetests/unit/agents/cleanup.test.tstests/unit/router/worker-env.test.tsdescribeblock forsnapshotEnabledflagTest plan
npm run lint— cleannpm run typecheck— cleannpm test— 7164/7164 unit tests passsnapshotEnabled, observeCommitted container to snapshot image, re-trigger — logs should showSnapshot reuse detected — skipping cloneinstead ofSnapshot reuse requested but no workspace directory found — falling back to clone🤖 Generated with Claude Code