carlos / platform Public

assetcache: Delete and page keys the asset sweep leaves alone

Store.Delete looks the entry up under s.mu, releases it, then calls drop.
IsPageKey marks keys whose Gen starts with "page:"; sweepAssets keeps them.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev e8043ac6d20c4446842af6ff6e8ec39453319271 parent 507193b
5 files changed, +106 −0
  • internal/assetcache/key.go +10 −0
  • internal/assetcache/store.go +12 −0
  • internal/assetcache/store_test.go +51 −0
  • internal/edge/assetarm.go +3 −0
  • internal/edge/assetarm_test.go +30 −0

CI failed — run details

Intent

Attributed to this commit: 33.2 g CO₂e (4,272,798 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on edge-page-cache-1, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/internal/assetcache/key.go b/internal/assetcache/key.go
index 111f017e8..a9b68bfdd 100644
--- a/internal/assetcache/key.go
+++ b/internal/assetcache/key.go
@@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
+ "strings"
)
// Key identifies one cached response: which app generation served it, on
@@ -29,3 +30,12 @@ func (k Key) ID() string {
func (k Key) AppScope() string {
return k.Account + "/" + k.App
}
+
+// PageGenPrefix starts the Gen of every key the page index owns. Page keys
+// are not tied to an asset generation, so the asset sweep leaves them alone.
+const PageGenPrefix = "page:"
+
+// IsPageKey reports whether k belongs to the page index.
+func IsPageKey(k Key) bool {
+ return strings.HasPrefix(k.Gen, PageGenPrefix)
+}
diff --git a/internal/assetcache/store.go b/internal/assetcache/store.go
index 0289c5303..b295a6af4 100644
--- a/internal/assetcache/store.go
+++ b/internal/assetcache/store.go
@@ -410,6 +410,18 @@ func (s *Store) drop(e *entry) bool {
return true
}
+// Delete drops the entry stored for k and reports whether there was one. It
+// takes s.mu only to find the entry, then lets drop do the work without it.
+func (s *Store) Delete(k Key) bool {
+ s.mu.Lock()
+ e := s.entries[k.ID()]
+ s.mu.Unlock()
+ if e == nil || e.meta.Key != k {
+ return false
+ }
+ return s.drop(e)
+}
+
// Entry is a cache hit: the metadata as stored, and the means to open its
// bodies. Its Header is the caller's own copy.
type Entry struct {
diff --git a/internal/assetcache/store_test.go b/internal/assetcache/store_test.go
index 51e3ed379..0b6cccbbd 100644
--- a/internal/assetcache/store_test.go
+++ b/internal/assetcache/store_test.go
@@ -9,6 +9,7 @@ import (
"path/filepath"
"sort"
"strings"
+ "sync"
"testing"
"time"
)
@@ -528,3 +529,53 @@ func TestOpenOverBudgetRefusesFillsUntilBackUnder(t *testing.T) {
t.Fatalf("Stats = %+v, want within the budget %d", st, s.cfg.Budget)
}
}
+
+func TestDeleteDropsEntryAndFiles(t *testing.T) {
+ dir := t.TempDir()
+ s := openStore(t, dir)
+ k := testKey("shop", "/a.css")
+ commit(t, s, k, []byte("body"), cssHeader("1"), false)
+ if !s.Delete(k) {
+ t.Fatal("Delete = false for a stored entry")
+ }
+ if _, ok := s.Get(k); ok {
+ t.Fatal("Get hit after Delete")
+ }
+ if got := listDir(t, dir); len(got) != 0 {
+ t.Fatalf("files left after Delete: %v", got)
+ }
+ if s.Delete(k) {
+ t.Fatal("second Delete = true")
+ }
+}
+
+func TestDeleteConcurrentWithGet(t *testing.T) {
+ s := openStore(t, t.TempDir())
+ keys := make([]Key, 20)
+ for i := range keys {
+ keys[i] = testKey("shop", fmt.Sprintf("/f%d.css", i))
+ commit(t, s, keys[i], []byte("body"), cssHeader("1"), false)
+ }
+ done := make(chan struct{})
+ var wg sync.WaitGroup
+ for g := 0; g < 4; g++ {
+ wg.Add(1)
+ go func(g int) {
+ defer wg.Done()
+ for i := 0; i < 200; i++ {
+ k := keys[(i+g)%len(keys)]
+ if (i+g)%2 == 0 {
+ s.Get(k)
+ } else {
+ s.Delete(k)
+ }
+ }
+ }(g)
+ }
+ go func() { wg.Wait(); close(done) }()
+ select {
+ case <-done:
+ case <-time.After(5 * time.Second):
+ t.Fatal("Get and Delete did not finish within 5s: deadlock")
+ }
+}
diff --git a/internal/edge/assetarm.go b/internal/edge/assetarm.go
index 15b2522fb..e539c12ee 100644
--- a/internal/edge/assetarm.go
+++ b/internal/edge/assetarm.go
@@ -94,6 +94,9 @@ func (e *Edge) sweepAssets(now time.Time) int {
return a.Account == k.Account && a.App == k.App && a.Gen == k.Gen
}
return st.store.Retain(func(k assetcache.Key) bool {
+ if assetcache.IsPageKey(k) {
+ return true
+ }
if st.local != nil {
if a, ok := st.local.For(k.Host); ok && same(a, k) {
return true
diff --git a/internal/edge/assetarm_test.go b/internal/edge/assetarm_test.go
index 696e2c105..6deb3dc9c 100644
--- a/internal/edge/assetarm_test.go
+++ b/internal/edge/assetarm_test.go
@@ -334,3 +334,33 @@ func TestServesNothingBeforeTheFirstHomeMapRead(t *testing.T) {
t.Fatalf("home saw n=%d fill=%q, want the uncaught path forwarded as a plain hop", seen.n, seen.fill)
}
}
+
+func TestSweepAssetsKeepsPageKeys(t *testing.T) {
+ s := newLAScene(t, assetOK(laBody))
+ armForTest(t, s.e, s.store, s.local)
+ // Both authorities are ready and name neither key below.
+ asset := assetcache.Key{Account: "abc", App: "shop", Gen: "oldgen", Host: laHost, Path: "/assets/old.js"}
+ page := assetcache.Key{Account: "abc", App: "shop", Gen: "page:x:1:a", Host: laHost, Path: "/"}
+ for _, k := range []assetcache.Key{asset, page} {
+ f, err := s.store.Reserve(k, 4)
+ if err != nil {
+ t.Fatalf("Reserve: %v", err)
+ }
+ if _, err := f.Write([]byte("body")); err != nil {
+ t.Fatalf("Write: %v", err)
+ }
+ m := assetcache.Meta{Header: serveHeader(), Lifetime: time.Hour, Expires: time.Now().Add(time.Hour)}
+ if err := f.Commit(m, false); err != nil {
+ t.Fatalf("Commit: %v", err)
+ }
+ }
+ if n := s.e.sweepAssets(time.Now()); n != 1 {
+ t.Fatalf("sweep dropped %d entries, want only the asset key", n)
+ }
+ if _, ok := s.store.Get(asset); ok {
+ t.Fatal("the asset key survived the sweep")
+ }
+ if _, ok := s.store.Get(page); !ok {
+ t.Fatal("the page key was swept")
+ }
+}