fix(execd): stage content replacements so a failed write keeps the old file #2890
All Vouch review confidence checks passed
Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #2890"
general
weight: 0.35
JasonBuildAI
Summary
A failed
POST /files/replacecan destroy the sandbox's only copy of a file; this change stages the replacement so the endpoint returns an error with the original bytes intact.Fixes #2889
This is a follow-up to #2433 and #2478. #2433 made
/files/uploadatomic and explicitly left ordinary/files/replacein place; #2478 fixed the isolated-session file routes. The ordinary and identity-scoped replace routes inFilesystemControllerstill calledos.WriteFile, which opens the destination withO_TRUNCbefore writing.The handler now feeds the replacement bytes through the existing
writeUploadStream, so the content is written to a private staging file in the destination directory, synced, and renamed into place. A failed copy removes the staging file and leaves the destination unchanged. The helper already preserves the destination mode (including setuid/setgid/sticky), owner/group where possible, final-component symlink behavior, and read-only rejection, and the now-unusedos.Statmode capture is removed. Its status/code/message are propagated to the response. A writable destination that cannot be replaced by rename—because a sticky directory protects a file owned by another UID, or the destination is a bind-mounted file—reuses the staged bytes through the existing in-place fallback instead of returning 500. The fallback is limited to permission andEBUSYrename errors;ENOSPC,EDQUOT, andEIOreturn the rename error without opening the destination, so a full or failing filesystem cannot truncate the old bytes. Destinations that support rename remain atomic.On Windows, the staging handle is created with
FILE_SHARE_DELETEso the pinned descriptor can stay open through the rename. Go'sos.OpenFileomits that share mode, and Windows otherwise rejects every rename as a sharing violation.The Linux regression uses
RLIMIT_FSIZEto force a real partial write. The existing success test now uses0640and verifies the mode survives. The execd architecture page now states that content replacements are staged too.Testing
Pre-fix on base
f1e4ac0924b9cb0593076e1254ec79adfd51c86bwith the regression test inserted:Post-fix on reviewed HEAD:
Post-fix for the rename-fallback review on
b02f24d8e:Checks run locally:
go test -count=1 -run '^TestFilesystemControllerReplaceContent' ./pkg/web/controller(Linux and Windows): pass.go test -count=1 -run 'TestWriteUploadStream|TestUploadRenameUnsupported|TestUploadStagingUnsupported|TestFilesystemControllerReplaceContent' ./pkg/web/controller(Windows): pass.TestWriteUploadStreamRenameIOFailureKeepsDestinationcoversENOSPC,EDQUOT, andEIO;TestUploadRenameUnsupportedcovers the permission andEBUSYpredicate boundaries.GOOS=linux go test -c ./pkg/web/controllerandgo vet ./pkg/web/controller: pass.go test -count=1 -skip '^TestFilesystemControllerRenameFilesRejectsEmptyPaths$' ./pkg/web/controller(Linux container, Go 1.25.7): pass in 11.222s.TestFilesystemControllerRenameFilesRejectsEmptyPathsin the unfiltered Linux package suite; the failure sets are identical.go build ./...,go vet ./...,GOOS=linux go build ./...,GOOS=linux go vet ./...,GOOS=windows go build ./..., andGOOS=windows go vet ./...: pass.gofmt -lon the four touched Go files in an LF export of the commit: empty.GOOS=linux golangci-lint run -v ./...with golangci-lint 1.64.8: pass.git diff --check <base>...HEAD: clean.cd docs && pnpm docs:build:build complete in 32.90s(existing chunk-size warning only).Not run: a live sandbox request, macOS, and the Kind/e2e suites.
Breaking Changes
The wire contract, request fields, response fields, and error status are unchanged. The implementation inherits the staged-write behavior already documented for
/files/upload: a successful replacement changes the inode, so hard links and extended attributes follow the documented upload semantics. Destinations that cannot be committed by rename—non-writable directories, sticky-directory ownership, and bind mounts—fall back to the documented in-place write.Checklist
Fixes #2889)AI disclosure: this change was drafted with AI assistance at my request, under my direction and review.