carlos / platform Public

cli: releases pull writes through temp files

#1389 item 1, task 3 review round 1. Each artifact now streams to an
exclusively created temp file in the output directory (<file>.partial-*),
so a symlink already there is never followed, and is renamed over its
name only after its size and sha256 match. manifest.json is written to a
temp file, synced and renamed in, so it never exists with partial bytes.
The manifest read has its own two-minute deadline. The CLI page now says
releases has three sub-verbs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 6ca21a4c6d443886eb540f7ef7cfda53288138c8 parent f47d0d8
3 files changed, +94 −21
  • docs/site/cli.md +1 −1
  • release_pull.go +54 −18
  • release_pull_test.go +39 −2

CI cancelled — superseded by a newer push — run details

CI log
=== gate ===
go: downloading go1.26.10 (linux/arm64)
go version go1.26.10 linux/arm64
opentofu: arm64, tofu 1.13.9, aws 6.73.0, archive 2.8.1
  tofu: downloading https://carlos-ci-mirror-640012953448.s3.eu-west-1.amazonaws.com/opentofu/tofu_1.13.9_linux_arm64.tar.gz
  tofu: downloaded, digest verified
  aws: downloading https://carlos-ci-mirror-640012953448.s3.eu-west-1.amazonaws.com/opentofu/terraform-provider-aws_6.73.0_linux_arm64.zip
  aws: downloaded, digest verified
  archive: downloading https://carlos-ci-mirror-640012953448.s3.eu-west-1.amazonaws.com/opentofu/terraform-provider-archive_2.8.1_linux_arm64.zip
  archive: downloaded, digest verified
  installed in /tmp/carlos-opentofu-ukVqzZ
.amadan/test-gate.sh
go: downloading github.com/aws/aws-sdk-go-v2 v1.44.0
go: downloading github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.68.0
go: downloading github.com/aws/aws-sdk-go-v2/service/costexplorer v1.67.5
go: downloading github.com/aws/aws-sdk-go-v2/service/s3 v1.105.0
go: downloading github.com/benbjohnson/litestream v0.5.14
go: downloading github.com/superfly/ltx v0.5.1
go: downloading golang.org/x/net v0.62.0
go: downloading modernc.org/sqlite v1.55.0
go: downloading golang.org/x/crypto v0.58.0
go: downloading github.com/aws/aws-sdk-go-v2/service/acm v1.45.0
go: downloading github.com/aws/aws-sdk-go-v2/service/ec2 v1.324.0
go: downloading github.com/aws/aws-sdk-go-v2/service/iam v1.58.2
go: downloading github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi v1.37.0
go: downloading github.com/aws/aws-sdk-go-v2/service/route53 v1.64.1
go: downloading github.com/aws/aws-sdk-go-v2/service/sts v1.45.6
go: downloading golang.org/x/sys v0.49.0
go: downloading github.com/oschwald/maxminddb-golang v1.13.1
go: downloading github.com/aws/aws-sdk-go-v2/service/sns v1.43.0
go: downloading github.com/pion/stun/v3 v3.1.5
go: downloading github.com/pion/turn/v4 v4.1.4
go: downloading github.com/aws/aws-sdk-go-v2/service/sesv2 v1.66.6
go: downloading github.com/pierrec/lz4/v4 v4.1.22
go: downloading github.com/klauspost/compress v1.20.2
go: downloading golang.org/x/text v0.43.0
go: downloading github.com/pion/transport/v4 v4.0.2
go: downloading github.com/pion/dtls/v3 v3.1.4
go: downloading github.com/wlynxg/anet v0.0.5
go: downloading github.com/prometheus/client_golang v1.17.0
go: downloading github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.20.18
go: downloading github.com/lmittmann/tint v1.1.3
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/prometheus/common v0.45.0
go: downloading github.com/cespare/xxhash/v2 v2.3.0
go: downloading github.com/prometheus/procfs v0.12.0
go: downloading google.golang.org/protobuf v1.36.10
go: downloading github.com/prometheus/client_model v0.5.0
go: downloading github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0
go: downloading github.com/klauspost/compress v1.20.2
go: downloading golang.org/x/crypto/x509roots/fallback v0.0.0-20260209214922-2f26647a795e
go: downloading modernc.org/sqlite v1.55.0
go: downloading github.com/pierrec/lz4/v4 v4.1.22
go: downloading github.com/aws/aws-sdk-go-v2/service/ecs v1.91.0
go: downloading modernc.org/libc v1.74.1
go: downloading modernc.org/libc v1.74.1
go: downloading github.com/dustin/go-humanize v1.0.1
go: downloading github.com/google/uuid v1.6.0
go: downloading modernc.org/mathutil v1.7.1
go: downloading modernc.org/memory v1.11.0
go: downloading modernc.org/mathutil v1.7.1

amadan: job cancelled — a newer push superseded it

Intent

Human

<task-notification> <task-id>a1c973892a95d9c82</task-id> <tool-use-id>toolu_01FAY5uYdc2wgTci3BuRLgej</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/a88005ac-72e4-481c-884f-581940b8c2c5/tasks/a1c973892a95d9c82.output</output-file> <status>completed</status> <summary>Agent "Implement Task 2: expect_artifacts" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: DONE Commit: ff974ce5 console: ship refuses artifacts that differ from expect_artifacts Tests: TDD done. RED had 5 assertion failures, and `TestAPIShipExpectMalformed` hung until the timeout because the old door reads the body. GREEN: all 7 `TestAPIShipExpect` tests pass, the whole `TestAPIShip` family passes (`ok 24.015s`), and `go vet` and `gofmt` are clean. Concerns: - An explicit empty `"expect_artifacts": {}` is not treated as missing. The door refuses every part as "sent but not expected". This may matter for the CLI task. - Go commands ran with the sandbox off, because the default Go cache is read-only inside it. `GOCACHE` stayed at its default. - `git commit` warned that `/home/paulca/amadan.net/carlos/platform/.git/packed-refs.lock` already exists. It is not mine and I left it alone. The commit still landed (HEAD is ff974ce5). - `apicli.go` is very large. My changes there are only the new field and two short checks. Report: /home/paulca/amadan.net/carlos/platform/.claude/worktrees/release-copy-across-consoles/.superpowers/sdd/2026-11-25-release-copy-across-consoles/task-2-report.md</result> <usage><subagent_tokens>84392</subagent_tokens><tool_uses>27</tool_uses><duration_ms>994926</duration_ms></usage> </task-notification>

Human

<task-notification> <task-id>ae5d037ff8a6bc6f7</task-id> <tool-use-id>toolu_01GLjrQjrhWDG1DSuUEjuwXB</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/a88005ac-72e4-481c-884f-581940b8c2c5/tasks/ae5d037ff8a6bc6f7.output</output-file> <status>completed</status> <summary>Agent "Implement Task 3: releases pull" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: DONE_WITH_CONCERNS. The concerns come from Tasks 1 and 2; this task's code passes. Commit: f47d0d8d cli: carlos releases pull copies a release out of a console Tests: the 6 new pull tests were RED, then GREEN. These also pass: TestCLIGolden, TestCLIProjection, the docsite and parity tests, vet and gofmt. It builds for darwin and windows. Concerns: - `make code-health` fails on two counts from Tasks 1 and 2: `handleAPIShip` is 386 lines against a record of 378, and `server.go` is 4225 against 4223. The ledger deferred code-health to the final review. - I added 2 lines to `testdata/clireg-golden.jsonl` for the new command. The brief didn't list it, but TestCLIGolden fails without them, and CI runs it. Task 4's `--from-release` flag will need the same treatment. - Two small changes beyond the brief, both found in self-review: - If writing `manifest.json` fails, the torn file is removed, so the folder never looks complete when it isn't. - The check on file names ignores case for `manifest.json`, `.partial` and duplicates, as the file systems on macOS and Windows do. - In the test, the ship uses `--account release.OpsAccount` (the sqid). With a service token, `--account ops` gets a 404, which kills the test binary. Report: /home/paulca/amadan.net/carlos/platform/.claude/worktrees/release-copy-across-consoles/.superpowers/sdd/2026-11-25-release-copy-across-consoles/task-3-report.md</result> <usage><subagent_tokens>150423</subagent_tokens><tool_uses>60</tool_uses><duration_ms>777704</duration_ms></usage> </task-notification>

diff --git a/docs/site/cli.md b/docs/site/cli.md
index 95825b5dc..dbd5e89e5 100644
--- a/docs/site/cli.md
+++ b/docs/site/cli.md
@@ -1001,7 +1001,7 @@ to run".
carlos releases --app hello
```
-There are two sub-verbs. `carlos releases retention --keep 20` sets an ambient
+There are three sub-verbs. `carlos releases retention --keep 20` sets an ambient
prune policy, and `--dry-run` shows you what it would remove before you commit
to it. The safety list always wins over the number: channel pointers,
rollback history, tagged releases, anything inside the bake window, and each
diff --git a/release_pull.go b/release_pull.go
index c5f662889..68d2c0f3a 100644
--- a/release_pull.go
+++ b/release_pull.go
@@ -15,6 +15,7 @@ import (
"path/filepath"
"sort"
"strings"
+ "time"
"github.com/carlosframework/platform/internal/release"
)
@@ -30,6 +31,9 @@ const (
// releaseManifestMaxBytes caps the manifest read: a manifest is a few
// KiB, and a body past this is not one.
releaseManifestMaxBytes = 1 << 20
+ // releaseManifestTimeout bounds the manifest read, headers and body:
+ // doStream's client has no timeout of its own.
+ releaseManifestTimeout = 2 * time.Minute
releasesPullUsage = "usage: carlos releases pull <version> --app <a> --out <dir> [--account <sqid-or-name>] [--account-id <sqid>] [--console <origin>]"
releasesPullNeedsConsole = "carlos releases pull reads from a console. set CARLOS_CONSOLE or run carlos login"
@@ -85,9 +89,11 @@ func runReleasesPull(args []string) error {
// pullRelease writes app's release version into out: every artifact first,
// each checked against the manifest, then manifest.json with the exact
-// bytes the console stores. manifest.json is written last and only once,
-// so a directory holding it always holds the whole release; a directory
-// without it is an interrupted pull, which a second pull finishes.
+// bytes the console stores. Every file is written to a fresh temp file in
+// out and renamed into place, and manifest.json is renamed in last, so a
+// directory holding it always holds the whole release, even after a crash;
+// a directory without it is an interrupted pull, which a second pull
+// finishes.
func pullRelease(ctx context.Context, sess *apiSession, a apiAcct, app, version, out string) (*release.Manifest, error) {
manifestPath := filepath.Join(out, "manifest.json")
if _, err := os.Lstat(manifestPath); err == nil {
@@ -134,20 +140,37 @@ func pullRelease(ctx context.Context, sess *apiSession, a apiAcct, app, version,
return nil, err
}
}
- f, err := os.OpenFile(manifestPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
- if err != nil {
+ if err := publishManifest(out, manifestPath, raw); err != nil {
return nil, err
}
+ return &m, nil
+}
+
+// publishManifest writes raw to a temp file in out, syncs it, and renames
+// it to manifestPath, so manifest.json never exists with partial bytes.
+func publishManifest(out, manifestPath string, raw []byte) error {
+ f, err := os.CreateTemp(out, "manifest.json.partial-*")
+ if err != nil {
+ return err
+ }
+ tmp := f.Name()
_, err = f.Write(raw)
+ if err == nil {
+ err = f.Sync()
+ }
if cerr := f.Close(); err == nil {
err = cerr
}
+ if err == nil {
+ err = os.Chmod(tmp, 0o644)
+ }
+ if err == nil {
+ err = os.Rename(tmp, manifestPath)
+ }
if err != nil {
- // A torn manifest.json would make the directory look complete.
- _ = os.Remove(manifestPath)
- return nil, err
+ _ = os.Remove(tmp)
}
- return &m, nil
+ return err
}
// pullArtifactBounded is pullArtifact under the deadline its kind ships
@@ -164,15 +187,17 @@ func pullArtifactBounded(ctx context.Context, sess *apiSession, a apiAcct, app,
// validPulledFile reports whether a manifest's File name can be written
// into the pull directory as itself: one plain name, not the manifest, not
-// a partial download's name. The last two compare without case, as macOS
-// and Windows file systems do.
+// a partial download's name (<file>.partial, or a temp file's
+// <file>.partial-*). Those compare without case, as macOS and Windows file
+// systems do.
func validPulledFile(name string) bool {
lower := strings.ToLower(name)
return name != "" && name == filepath.Base(name) &&
!strings.ContainsAny(name, `/\`) &&
name != "." && name != ".." &&
lower != "manifest.json" &&
- !strings.HasSuffix(lower, ".partial")
+ !strings.HasSuffix(lower, ".partial") &&
+ !strings.Contains(lower, ".partial-")
}
// releasePath is the read doors' path for one release of app.
@@ -183,6 +208,8 @@ func releasePath(a apiAcct, app, version, tail string) string {
// releaseManifestRaw reads GET .../releases/{version}/manifest: the exact
// bytes the console stores.
func (s *apiSession) releaseManifestRaw(ctx context.Context, a apiAcct, app, version string) ([]byte, error) {
+ ctx, cancel := context.WithTimeout(ctx, releaseManifestTimeout)
+ defer cancel()
res, err := s.c.doStream(ctx, http.MethodGet, releasePath(a, app, version, "/manifest"), nil, -1, nil)
if err != nil {
return nil, err
@@ -201,9 +228,11 @@ func (s *apiSession) releaseManifestRaw(ctx context.Context, a apiAcct, app, ver
return body, nil
}
-// pullArtifact streams one artifact to dst+".partial", hashing as it goes,
-// and renames it over dst only when its size and sha256 match art. A
-// mismatch or a failed read removes the partial file.
+// pullArtifact streams one artifact to a fresh temp file beside dst
+// (<file>.partial-*, created exclusively, so a symlink already in the
+// directory is never followed), hashing as it goes, and renames it over dst
+// only when its size and sha256 match art. A mismatch or a failed read
+// removes the temp file.
func (s *apiSession) pullArtifact(ctx context.Context, a apiAcct, app, version string, art release.Artifact, dst string) error {
res, err := s.c.doStream(ctx, http.MethodGet, releasePath(a, app, version, "/artifacts/"+url.PathEscape(art.File)), nil, -1, nil)
if err != nil {
@@ -214,11 +243,11 @@ func (s *apiSession) pullArtifact(ctx context.Context, a apiAcct, app, version s
body, _ := io.ReadAll(io.LimitReader(res.Body, releaseManifestMaxBytes))
return apiErrorFromBody(res.StatusCode, body)
}
- partial := dst + ".partial"
- f, err := os.OpenFile(partial, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
+ f, err := os.CreateTemp(filepath.Dir(dst), filepath.Base(dst)+".partial-*")
if err != nil {
return err
}
+ partial := f.Name()
h := sha256.New()
n, copyErr := io.Copy(io.MultiWriter(f, h), io.LimitReader(res.Body, art.Size+1))
closeErr := f.Close()
@@ -231,5 +260,12 @@ func (s *apiSession) pullArtifact(ctx context.Context, a apiAcct, app, version s
}
return mismatch
}
- return os.Rename(partial, dst)
+ err = os.Chmod(partial, 0o644)
+ if err == nil {
+ err = os.Rename(partial, dst)
+ }
+ if err != nil {
+ _ = os.Remove(partial)
+ }
+ return err
}
diff --git a/release_pull_test.go b/release_pull_test.go
index 149f7659d..79cce5207 100644
--- a/release_pull_test.go
+++ b/release_pull_test.go
@@ -40,7 +40,7 @@ func pullV12(t *testing.T, a copyConsole, dir string) error {
return runReleasesPull([]string{"v12", "--app", "shop", "--out", dir})
}
-// noPartials fails when dir holds a .partial file.
+// noPartials fails when dir holds a pull's temp file (<file>.partial-*).
func noPartials(t *testing.T, dir string) {
t.Helper()
ents, err := os.ReadDir(dir)
@@ -48,7 +48,7 @@ func noPartials(t *testing.T, dir string) {
t.Fatal(err)
}
for _, e := range ents {
- if strings.HasSuffix(e.Name(), ".partial") {
+ if strings.Contains(e.Name(), ".partial-") {
t.Fatalf("%s left behind in %s", e.Name(), dir)
}
}
@@ -145,6 +145,42 @@ func TestReleasesPullRemovesACorruptDownload(t *testing.T) {
t.Fatalf("%s is in %s after a refused pull (stat: %v)", name, dir, err)
}
}
+ noPartials(t, dir)
+}
+
+// A <file>.partial symlink left in the directory is never followed: the
+// file it points at keeps its bytes, and the pull still succeeds.
+func TestReleasesPullLeavesAPartialSymlinkAlone(t *testing.T) {
+ useTempCredentials(t)
+ clearDeploymentEnv(t)
+ a := shipPullSource(t)
+ outside := filepath.Join(t.TempDir(), "precious")
+ want := []byte("not yours to truncate")
+ if err := os.WriteFile(outside, want, 0o644); err != nil {
+ t.Fatal(err)
+ }
+ dir := t.TempDir()
+ if err := os.Symlink(outside, filepath.Join(dir, "linux-arm64.partial")); err != nil {
+ t.Skipf("no symlinks here: %v", err)
+ }
+ if err := pullV12(t, a, dir); err != nil {
+ t.Fatalf("pull beside a partial symlink: %v", err)
+ }
+ got, err := os.ReadFile(outside)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !bytes.Equal(got, want) {
+ t.Fatalf("the symlink's target now holds %d bytes %q, want it untouched", len(got), got)
+ }
+ bin, err := os.ReadFile(filepath.Join(dir, "linux-arm64"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !bytes.Equal(bin, pullPayload) {
+ t.Fatalf("linux-arm64 is %d bytes, not the shipped payload", len(bin))
+ }
+ noPartials(t, dir)
}
func TestReleasesPullNeedsAConsole(t *testing.T) {
@@ -175,6 +211,7 @@ func TestValidPulledFile(t *testing.T) {
{"linux-arm64.partial", false},
{"Manifest.JSON", false},
{"linux-arm64.PARTIAL", false},
+ {"linux-arm64.partial-123", false},
} {
if got := validPulledFile(tc.name); got != tc.want {
t.Errorf("validPulledFile(%q) = %v, want %v", tc.name, got, tc.want)