carlos / platform Public

edge: unlink the artifact temp file at once; review minors

Co-Authored-By: Astra (gpt-6-astra) via Codex <noreply@openai.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 0d7b4a6c18016950efd5e9b5248c64de93c47f6c parent 8662f6a
9 files changed, +126 −20
  • docs/superpowers/notes/census-proposal-static-stream-extract.md +2 −2
  • internal/edge/static.go +15 −3
  • internal/edge/static_artifact_other.go +5 −0
  • internal/edge/static_artifact_unix.go +5 −0
  • internal/edge/static_stream_test.go +77 −10
  • internal/edge/static_test.go +8 −0
  • internal/provision/testdata/plan14-root-reads-chain.tsv +1 −1
  • internal/provision/testdata/plan14-root-reads-static-stream-extract.tsv +5 −4
  • internal/release/qualified.go +8 −0
diff --git a/docs/superpowers/notes/census-proposal-static-stream-extract.md b/docs/superpowers/notes/census-proposal-static-stream-extract.md
index 4c6eb7e05..909af810a 100644
--- a/docs/superpowers/notes/census-proposal-static-stream-extract.md
+++ b/docs/superpowers/notes/census-proposal-static-stream-extract.md
@@ -6,7 +6,7 @@ Ruling `census-ruling-static-stream-extract`. The independent reviewer approves
|---|---|---|---|
| `internal/edge/static.go` `extractSiteTar` | body | unchanged (1) | Accepts io.Reader instead of []byte; gzip reads the verified temporary file supplied by ensureExtracted or the bytes.NewReader adapter in ExtractSite. The extraction paths, file opens, path validation and entry-type refusals are unchanged. No new read site or authority. |
-Envelopes re-pinned: `internal/edge`.
+Envelopes re-pinned: `internal/edge`, `internal/release`.
Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner.
@@ -18,6 +18,6 @@ New dispositions, which census-propose wrote and the reviewer checks against the
- reads `internal/edge/static.go:staticSite.ensureExtracted`: +file:CreateTemp; keeps legacy, task accepted-legacy
-Seal: `b0fc198c53bb0dacba33a54c3d8bf8fe5ac07d94b76a31b8a69e9411115ecd85` → `aca033385333de05c1352babff3f88f4d3e6bf0a05e8d0a3fca3a4257db60f20`.
+Seal: `b0fc198c53bb0dacba33a54c3d8bf8fe5ac07d94b76a31b8a69e9411115ecd85` → `7fb932b118e67e23b33a374c6a4020b38976667c7ec4cfb855c0f8b598b1756b`.
Overlay: `internal/provision/testdata/plan14-root-reads-static-stream-extract.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`.
diff --git a/internal/edge/static.go b/internal/edge/static.go
index 1cd3512bd..e0057e5d0 100644
--- a/internal/edge/static.go
+++ b/internal/edge/static.go
@@ -414,7 +414,7 @@ func (s *staticSite) ensureExtracted(ctx context.Context, version, want string)
}
started := time.Now()
key := release.ArtifactKey(s.acct, s.app, version, art.File)
- body, _, err := release.OpenObjectStreaming(ctx, s.store, key)
+ body, _, err := release.OpenQualifiedStreaming(ctx, s.store, key)
if errors.Is(err, errors.ErrUnsupported) {
const getFallbackMax = 64 << 20
if art.Size > getFallbackMax {
@@ -434,16 +434,28 @@ func (s *staticSite) ensureExtracted(ctx context.Context, version, want string)
if err != nil {
return "", "", err
}
+ named := true
defer func() {
artifact.Close()
- os.Remove(artifact.Name())
+ if named {
+ os.Remove(artifact.Name())
+ }
}()
+ if unlinkOpenStaticArtifact {
+ if err := os.Remove(artifact.Name()); err != nil {
+ return "", "", err
+ }
+ named = false
+ }
hash := sha256.New()
n, err := io.Copy(io.MultiWriter(artifact, hash), io.LimitReader(body, art.Size+1))
if err != nil {
return "", "", err
}
- if n != art.Size {
+ if n > art.Size {
+ return "", "", fmt.Errorf("%s size mismatch (manifest %d, stored more than %d)", siteArtifactFile, art.Size, art.Size)
+ }
+ if n < art.Size {
return "", "", fmt.Errorf("%s size mismatch (manifest %d, stored %d)", siteArtifactFile, art.Size, n)
}
if sum := hex.EncodeToString(hash.Sum(nil)); sum != art.SHA256 {
diff --git a/internal/edge/static_artifact_other.go b/internal/edge/static_artifact_other.go
new file mode 100644
index 000000000..34cc8d6e7
--- /dev/null
+++ b/internal/edge/static_artifact_other.go
@@ -0,0 +1,5 @@
+//go:build !linux && !darwin
+
+package edge
+
+const unlinkOpenStaticArtifact = false
diff --git a/internal/edge/static_artifact_unix.go b/internal/edge/static_artifact_unix.go
new file mode 100644
index 000000000..2dad8093b
--- /dev/null
+++ b/internal/edge/static_artifact_unix.go
@@ -0,0 +1,5 @@
+//go:build linux || darwin
+
+package edge
+
+const unlinkOpenStaticArtifact = true
diff --git a/internal/edge/static_stream_test.go b/internal/edge/static_stream_test.go
index 50b99b2a8..cb1d746e9 100644
--- a/internal/edge/static_stream_test.go
+++ b/internal/edge/static_stream_test.go
@@ -9,6 +9,7 @@ import (
"encoding/hex"
"encoding/json"
"errors"
+ "fmt"
"io"
"math"
"os"
@@ -22,14 +23,15 @@ import (
type staticStreamStore struct {
release.Store
- artifact string
- key string
- gets int
- opens int
- read int64
- closed bool
- panicRead bool
- readErr error
+ artifact string
+ key string
+ gets int
+ opens int
+ read int64
+ closed bool
+ panicRead bool
+ readErr error
+ beforeRead func()
}
func (s *staticStreamStore) Get(ctx context.Context, key string) ([]byte, string, error) {
@@ -59,6 +61,9 @@ type staticStreamBody struct {
}
func (b *staticStreamBody) Read(p []byte) (int, error) {
+ if b.store.beforeRead != nil {
+ b.store.beforeRead()
+ }
if b.store.panicRead {
panic("stream panic")
}
@@ -197,10 +202,10 @@ func TestStaticStreamRefusals(t *testing.T) {
want = "sha256 mismatch"
case "overrun":
art.Size = 16
- want = "size mismatch"
+ want = "size mismatch (manifest 16, stored more than 16)"
case "short":
+ want = fmt.Sprintf("size mismatch (manifest %d, stored %d)", art.Size+1, art.Size)
art.Size++
- want = "size mismatch"
case "negative":
art.Size = -1
case "overflow":
@@ -299,3 +304,65 @@ func TestExtractSiteBytesCompatibility(t *testing.T) {
}
}
}
+
+func TestStaticStreamArtifactNameLifetime(t *testing.T) {
+ for _, fail := range []bool{false, true} {
+ t.Run(fmt.Sprintf("copy-error=%v", fail), func(t *testing.T) {
+ site, store, _ := staticStreamFixture(t, 128<<10)
+ checkNames := func() {
+ t.Helper()
+ entries, err := os.ReadDir(site.appDir())
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, entry := range entries {
+ if strings.HasPrefix(entry.Name(), ".artifact-") {
+ t.Errorf("artifact name still exists: %s", entry.Name())
+ }
+ }
+ }
+ reads := 0
+ copyErr := errors.New("forced mid-copy error")
+ store.beforeRead = func() {
+ reads++
+ if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
+ checkNames()
+ }
+ if fail && store.read > 0 {
+ store.readErr = copyErr
+ }
+ }
+ _, _, err := site.ensureExtracted(t.Context(), "v1", "")
+ if fail {
+ if !errors.Is(err, copyErr) || store.read == 0 {
+ t.Fatalf("read=%d err=%v", store.read, err)
+ }
+ staticStreamNoDebris(t, site)
+ } else if err != nil {
+ t.Fatal(err)
+ }
+ if reads < 2 || !store.closed {
+ t.Errorf("reads=%d closed=%v", reads, store.closed)
+ }
+ checkNames()
+ })
+ }
+}
+
+func TestStaticStreamMissingArtifactMigrationHint(t *testing.T) {
+ for _, getOnly := range []bool{false, true} {
+ t.Run(fmt.Sprintf("get-only=%v", getOnly), func(t *testing.T) {
+ site, store, _ := staticStreamFixture(t, 1024)
+ if getOnly {
+ site.store = struct{ release.Store }{store}
+ }
+ if err := os.Remove(store.artifact); err != nil {
+ t.Fatal(err)
+ }
+ _, _, err := site.ensureExtracted(t.Context(), "v1", "")
+ if !errors.Is(err, os.ErrNotExist) || !strings.Contains(err.Error(), "carlos accounts migrate --app site --from legacy --to bab") {
+ t.Fatalf("missing migration hint or not-found sentinel: %v", err)
+ }
+ })
+ }
+}
diff --git a/internal/edge/static_test.go b/internal/edge/static_test.go
index 05d0c4e28..4af384ff3 100644
--- a/internal/edge/static_test.go
+++ b/internal/edge/static_test.go
@@ -488,6 +488,11 @@ func (l *legacyTripwire) Get(ctx context.Context, key string) ([]byte, string, e
return l.Store.Get(ctx, key)
}
+func (l *legacyTripwire) Open(ctx context.Context, key string) (io.ReadCloser, string, error) {
+ l.trip("opened", key)
+ return release.OpenObject(ctx, l.Store, key)
+}
+
func (l *legacyTripwire) List(ctx context.Context, prefix string) ([]string, error) {
l.trip("listed", prefix)
return l.Store.List(ctx, prefix)
@@ -529,6 +534,9 @@ func TestStaticExtractNeverReadsALegacyArtifact(t *testing.T) {
}
s := &legacyTripwire{Store: dir, t: t, app: "site"}
+ if _, ok := any(s).(release.Opener); !ok {
+ t.Fatal("legacy tripwire does not guard streaming Open")
+ }
rt := registry.Route{Host: "site.example.com", Kind: "static", App: "site", Channel: "canary/x", Status: "ready"}
h := newStaticHandler(rt, s, pub, filepath.Join(t.TempDir(), "cache"))
h.(*staticSite).recheck = 0
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv
index c6243ccab..e109b21c8 100644
--- a/internal/provision/testdata/plan14-root-reads-chain.tsv
+++ b/internal/provision/testdata/plan14-root-reads-chain.tsv
@@ -71,4 +71,4 @@ plan14-root-reads-design-pdf-service.tsv 09ad1d7267b9598c64e89ec0faa7b18839c785c
plan14-root-reads-region-ha-placement-survivor.tsv 09ad1d7267b9598c64e89ec0faa7b18839c785c5c63905b398afe681cf9777c6
plan14-root-reads-domains-buy-nosub.tsv 09ad1d7267b9598c64e89ec0faa7b18839c785c5c63905b398afe681cf9777c6
plan14-root-reads-wake-storm-cap.tsv b0fc198c53bb0dacba33a54c3d8bf8fe5ac07d94b76a31b8a69e9411115ecd85
-plan14-root-reads-static-stream-extract.tsv aca033385333de05c1352babff3f88f4d3e6bf0a05e8d0a3fca3a4257db60f20
+plan14-root-reads-static-stream-extract.tsv 7fb932b118e67e23b33a374c6a4020b38976667c7ec4cfb855c0f8b598b1756b
diff --git a/internal/provision/testdata/plan14-root-reads-static-stream-extract.tsv b/internal/provision/testdata/plan14-root-reads-static-stream-extract.tsv
index 20e1bc6fa..864fc2be1 100644
--- a/internal/provision/testdata/plan14-root-reads-static-stream-extract.tsv
+++ b/internal/provision/testdata/plan14-root-reads-static-stream-extract.tsv
@@ -1,8 +1,9 @@
# Census overlay static-stream-extract, written by census-propose (#369). census-ruling-static-stream-extract, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-static-stream-extract.md.
-# 1 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended.
+# 1 body, 0 sites, 2 envelope: each changed unit re-frozen at its existing disposition with its claim appended.
# 1 new dispositions: 1 reads re-frozen at their disposition with the claim, 0 new units frozen accepted-legacy, 0 quiet units retired while they read nothing, 0 other.
@legacy 6ec53b85baeb153fff041965262373ce5bee8f9fae7c77a1543be9cf4dc44111 internal/edge/static.go extractSiteTar legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-static-stream-extract;basis=Accepts io.Reader instead of []byte; gzip reads the verified temporary file supplied by ensureExtracted or the bytes.NewReader adapter in ExtractSite. The extraction paths, file opens, path validation and entry-type refusals are unchanged. No new read site or authority. file:OpenFile@776 825ce7522436a7da19e5dc967f3e86276fb5ecac0ae7e283b516e3182b94864f
-@legacy 9ea59096792a4bd8970d0473c2518e5de9508c8f29a601c55b719d279c59691d internal/edge/static.go staticSite.ensureExtracted legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-static-stream-extract;basis=Streams the same account-qualified artifact into a private CreateTemp file under the existing site app directory; adds file:CreateTemp, then reads that same open file only after exact manifest size and SHA256 verification. Limits streaming reads to manifest size plus one and refuses Get fallback above a 64 MiB manifest size, including through wrappers. Keeps the existing mutex, manifest digest binding, cache destination and atomic rename; defers artifact and extraction cleanup and logs successful stages. No deployment record writes or root authority change. file:CreateTemp@1567,file:Stat@2527,file:Stat@346 1718281e1299a927bc4188ff53be39c2a2ed9a8ce47dd5a7848b9f66a941a0fc
+@legacy 1383b1549db9a213a9038ede7462d2b148e6a81763597564e37780fb8e25705a internal/edge/static.go staticSite.ensureExtracted legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-static-stream-extract;basis=Streams the same account-qualified artifact into a private CreateTemp file under the existing site app directory; adds file:CreateTemp, then reads that same open file only after exact manifest size and SHA256 verification. Limits streaming reads to manifest size plus one and refuses Get fallback above a 64 MiB manifest size, including through wrappers. Keeps the existing mutex, manifest digest binding, cache destination and atomic rename; On linux or darwin immediately unlinks only the name ensureExtracted itself just created, then works solely through the open handle; refuses an unlink failure and retains close/remove cleanup until unlink succeeds. Other platforms keep deferred close/remove. Defers extraction cleanup and logs successful stages. OpenQualifiedStreaming restores the existing qualified-miss migration hint without adding reads or permitting Get fallback; size overruns report more than the manifest size. No deployment record writes or root authority change. file:CreateTemp@1570,file:Stat@2834,file:Stat@346 4e31d41b0079ad039904a021683d49c61397226d2c91ea7bf4eee955bcef73fc
internal/edge/static.go extractSiteTar legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-static-stream-extract;basis=Accepts io.Reader instead of []byte; gzip reads the verified temporary file supplied by ensureExtracted or the bytes.NewReader adapter in ExtractSite. The extraction paths, file opens, path validation and entry-type refusals are unchanged. No new read site or authority. file:OpenFile@776 825ce7522436a7da19e5dc967f3e86276fb5ecac0ae7e283b516e3182b94864f
-internal/edge/static.go staticSite.ensureExtracted legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-static-stream-extract;basis=Streams the same account-qualified artifact into a private CreateTemp file under the existing site app directory; adds file:CreateTemp, then reads that same open file only after exact manifest size and SHA256 verification. Limits streaming reads to manifest size plus one and refuses Get fallback above a 64 MiB manifest size, including through wrappers. Keeps the existing mutex, manifest digest binding, cache destination and atomic rename; defers artifact and extraction cleanup and logs successful stages. No deployment record writes or root authority change. file:CreateTemp@1567,file:Stat@2527,file:Stat@346 1718281e1299a927bc4188ff53be39c2a2ed9a8ce47dd5a7848b9f66a941a0fc
-@coverage internal/edge 36a483b029cd3fca312109b913f3d1ba6f25fa6e9e9206461b02d8bd154b9575
+internal/edge/static.go staticSite.ensureExtracted legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-static-stream-extract;basis=Streams the same account-qualified artifact into a private CreateTemp file under the existing site app directory; adds file:CreateTemp, then reads that same open file only after exact manifest size and SHA256 verification. Limits streaming reads to manifest size plus one and refuses Get fallback above a 64 MiB manifest size, including through wrappers. Keeps the existing mutex, manifest digest binding, cache destination and atomic rename; On linux or darwin immediately unlinks only the name ensureExtracted itself just created, then works solely through the open handle; refuses an unlink failure and retains close/remove cleanup until unlink succeeds. Other platforms keep deferred close/remove. Defers extraction cleanup and logs successful stages. OpenQualifiedStreaming restores the existing qualified-miss migration hint without adding reads or permitting Get fallback; size overruns report more than the manifest size. No deployment record writes or root authority change. file:CreateTemp@1570,file:Stat@2834,file:Stat@346 4e31d41b0079ad039904a021683d49c61397226d2c91ea7bf4eee955bcef73fc
+@coverage internal/edge e130c816c1c8d34812ca2aabf3302735881d9d8049a46fc275cd3744028a6d5d
+@coverage internal/release 2d80d868274dbb67d8e06eb132f7af2f1f07a44c42d0fcc22a3f4dc4adf27f81
diff --git a/internal/release/qualified.go b/internal/release/qualified.go
index 72f71f2ca..8ba437fbb 100644
--- a/internal/release/qualified.go
+++ b/internal/release/qualified.go
@@ -100,6 +100,14 @@ func OpenQualified(ctx context.Context, s Store, key string) (io.ReadCloser, str
return rc, etag, err
}
+func OpenQualifiedStreaming(ctx context.Context, s Store, key string) (io.ReadCloser, string, error) {
+ rc, etag, err := OpenObjectStreaming(ctx, s, key)
+ if err != nil && errors.Is(err, os.ErrNotExist) {
+ return nil, "", fmt.Errorf("%w%s", err, migrationHint(key))
+ }
+ return rc, etag, err
+}
+
// migrationHint turns a miss on an account-qualified APP key into the exact
// command that fixes it, for the one deployment shape that produces a
// surprising 404: upgraded from a pre-accounts binary, objects still at