carlos / platform Public

assetcache, host: witnesses that exercise their claimed boundary

TestDeleteConcurrentWithGet derived key and operation from one parity,
so no key ever saw both Get and Delete. It now races Get, Open and
Delete on the same keys each round and checks hit bodies, exactly-once
Delete and a store left with no entry, bytes or file. A same-key
mutation (Open's lease increment moved outside the store lock) fails
the new test under -race and passes the old one.

TestRunAgentWiresCurrentInputs reads runAgent, as the package's other
runAgent wiring witnesses do: wireCurrentInputs is called once, with
the edge.NewCache variable handed to edge.RunWithActivatorReady and the
&activator.ExecBackend variable, before the edge starts. Removing the
call, or passing another cache, fails it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev c6721710c1ec0df16187d8f0f61c53302228daa2 parent 9aba50f
2 files changed, +192 −19
  • internal/assetcache/store_test.go +92 −19
  • internal/host/pagearm_test.go +100 −0
diff --git a/internal/assetcache/store_test.go b/internal/assetcache/store_test.go
index 0b6cccbbd..f46b8537a 100644
--- a/internal/assetcache/store_test.go
+++ b/internal/assetcache/store_test.go
@@ -3,6 +3,7 @@ package assetcache
import (
"errors"
"fmt"
+ "io"
"math"
"net/http"
"os"
@@ -10,6 +11,7 @@ import (
"sort"
"strings"
"sync"
+ "sync/atomic"
"testing"
"time"
)
@@ -549,33 +551,104 @@ func TestDeleteDropsEntryAndFiles(t *testing.T) {
}
}
+// TestDeleteConcurrentWithGet races Get, Open and Delete on the SAME keys:
+// each round commits every key, then two readers and two deleters per key
+// start together. A hit's body is the committed one or cannot be opened,
+// never other bytes; exactly one Delete per key per round reports an entry;
+// and once every reader has closed, the store holds no entry, no bytes and
+// no file.
func TestDeleteConcurrentWithGet(t *testing.T) {
- s := openStore(t, t.TempDir())
- keys := make([]Key, 20)
+ dir := t.TempDir()
+ s := openStore(t, dir)
+ keys := make([]Key, 4)
for i := range keys {
keys[i] = testKey("shop", fmt.Sprintf("/f%d.css", i))
- commit(t, s, keys[i], []byte("body"), cssHeader("1"), false)
}
+ const rounds = 50
+ var bad atomic.Value
+ fail := func(msg string) { bad.CompareAndSwap(nil, msg) }
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)
+ go func() {
+ defer close(done)
+ for round := 0; round < rounds && bad.Load() == nil; round++ {
+ for _, k := range keys {
+ f, err := s.Reserve(k, 4)
+ if err != nil {
+ fail(fmt.Sprintf("round %d: reserve: %v", round, err))
+ return
+ }
+ if _, err := f.Write([]byte("body")); err != nil {
+ fail(fmt.Sprintf("round %d: write: %v", round, err))
+ return
+ }
+ if err := f.Commit(Meta{Header: cssHeader("1"), Lifetime: time.Hour, Expires: testNow.Add(time.Hour)}, false); err != nil {
+ fail(fmt.Sprintf("round %d: commit: %v", round, err))
+ return
}
}
- }(g)
- }
- go func() { wg.Wait(); close(done) }()
+ start := make(chan struct{})
+ var wg sync.WaitGroup
+ deleted := make([]atomic.Int32, len(keys))
+ for i, k := range keys {
+ for r := 0; r < 2; r++ {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ for n := 0; n < 10; n++ {
+ e, ok := s.Get(k)
+ if !ok {
+ continue
+ }
+ rd, err := e.Open(false)
+ if err != nil {
+ continue // deleted between the Get and the Open
+ }
+ b, err := io.ReadAll(rd)
+ rd.Close()
+ if err != nil || string(b) != "body" {
+ fail(fmt.Sprintf("a hit on %s read %q, %v", k.Path, b, err))
+ }
+ }
+ }()
+ }
+ for d := 0; d < 2; d++ {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ for n := 0; n < 3; n++ {
+ if s.Delete(k) {
+ deleted[i].Add(1)
+ }
+ }
+ }()
+ }
+ }
+ close(start)
+ wg.Wait()
+ for i, k := range keys {
+ if n := deleted[i].Load(); n != 1 {
+ fail(fmt.Sprintf("round %d: %d Deletes of %s reported an entry, want 1", round, n, k.Path))
+ }
+ if _, ok := s.Get(k); ok {
+ fail(fmt.Sprintf("round %d: Get hit %s after its Delete", round, k.Path))
+ }
+ }
+ }
+ }()
select {
case <-done:
- case <-time.After(5 * time.Second):
- t.Fatal("Get and Delete did not finish within 5s: deadlock")
+ case <-time.After(30 * time.Second):
+ t.Fatal("Get, Open and Delete did not finish within 30s: deadlock")
+ }
+ if msg := bad.Load(); msg != nil {
+ t.Fatal(msg)
+ }
+ if st := s.Stats(); st.Entries != 0 || st.Bytes != 0 || st.Retired != 0 || st.Reserved != 0 {
+ t.Fatalf("after every Delete the store holds %+v", st)
+ }
+ if got := listDir(t, dir); len(got) != 0 {
+ t.Fatalf("files left after every Delete: %v", got)
}
}
diff --git a/internal/host/pagearm_test.go b/internal/host/pagearm_test.go
index 5f3bbf778..5102bd981 100644
--- a/internal/host/pagearm_test.go
+++ b/internal/host/pagearm_test.go
@@ -1,6 +1,9 @@
package host
import (
+ "go/ast"
+ "go/parser"
+ "go/token"
"os"
"path/filepath"
"testing"
@@ -95,3 +98,100 @@ func TestAgentWiresCurrentInputs(t *testing.T) {
t.Fatalf("CurrentInputs = %q, %v after the wiring", got, ok)
}
}
+
+// TestRunAgentWiresCurrentInputs is the production-wired witness for the
+// hook above (final review finding 5). Booting runAgent needs a box, so,
+// as this package's other runAgent wiring witnesses do, it reads the
+// source: runAgent must call wireCurrentInputs exactly once, with the very
+// variable it built from edge.NewCache and hands edge.RunWithActivatorReady
+// (the cache the edge serves from), and the very variable it built as the
+// &activator.ExecBackend the activator runs, before the edge starts. The
+// identifiers are resolved to their declarations, so a shadowing variable
+// or another cache does not pass. Removing the call fails it.
+func TestRunAgentWiresCurrentInputs(t *testing.T) {
+ fset := token.NewFileSet()
+ f, err := parser.ParseFile(fset, "agent.go", nil, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var run *ast.BlockStmt
+ for _, d := range f.Decls {
+ if fd, ok := d.(*ast.FuncDecl); ok && fd.Recv == nil && fd.Name.Name == "runAgent" {
+ run = fd.Body
+ }
+ }
+ if run == nil {
+ t.Fatal("agent.go: no func runAgent")
+ }
+ // declaredBy reports whether id was declared by an assignment whose one
+ // right-hand side satisfies rhs.
+ declaredBy := func(e ast.Expr, rhs func(ast.Expr) bool) (*ast.Object, bool) {
+ id, ok := e.(*ast.Ident)
+ if !ok || id.Obj == nil {
+ return nil, false
+ }
+ as, ok := id.Obj.Decl.(*ast.AssignStmt)
+ if !ok || len(as.Rhs) != 1 {
+ return nil, false
+ }
+ return id.Obj, rhs(as.Rhs[0])
+ }
+ selector := func(e ast.Expr, pkg, name string) bool {
+ s, ok := e.(*ast.SelectorExpr)
+ if !ok {
+ return false
+ }
+ x, ok := s.X.(*ast.Ident)
+ return ok && x.Name == pkg && s.Sel.Name == name
+ }
+ isNewCache := func(e ast.Expr) bool {
+ c, ok := e.(*ast.CallExpr)
+ return ok && selector(c.Fun, "edge", "NewCache")
+ }
+ isExecBackend := func(e ast.Expr) bool {
+ u, ok := e.(*ast.UnaryExpr)
+ if !ok || u.Op != token.AND {
+ return false
+ }
+ cl, ok := u.X.(*ast.CompositeLit)
+ return ok && selector(cl.Type, "activator", "ExecBackend")
+ }
+ var wires []*ast.CallExpr
+ var served *ast.Object
+ var wiredAt, servedAt token.Pos
+ ast.Inspect(run, func(n ast.Node) bool {
+ c, ok := n.(*ast.CallExpr)
+ if !ok {
+ return true
+ }
+ if id, ok := c.Fun.(*ast.Ident); ok && id.Name == "wireCurrentInputs" {
+ wires = append(wires, c)
+ wiredAt = c.Pos()
+ }
+ if selector(c.Fun, "edge", "RunWithActivatorReady") && len(c.Args) >= 3 {
+ if obj, ok := declaredBy(c.Args[2], isNewCache); ok {
+ served, servedAt = obj, c.Pos()
+ }
+ }
+ return true
+ })
+ if served == nil {
+ t.Fatal("runAgent no longer hands edge.RunWithActivatorReady a cache from edge.NewCache; update this witness")
+ }
+ if len(wires) != 1 {
+ t.Fatalf("runAgent calls wireCurrentInputs %d times, want once", len(wires))
+ }
+ c := wires[0]
+ if len(c.Args) != 2 {
+ t.Fatalf("wireCurrentInputs takes %d arguments here", len(c.Args))
+ }
+ if obj, ok := declaredBy(c.Args[0], isNewCache); !ok || obj != served {
+ t.Errorf("wireCurrentInputs at %s is not given the cache the edge serves from", fset.Position(c.Pos()))
+ }
+ if _, ok := declaredBy(c.Args[1], isExecBackend); !ok {
+ t.Errorf("wireCurrentInputs at %s is not given the &activator.ExecBackend runAgent builds", fset.Position(c.Pos()))
+ }
+ if wiredAt > servedAt {
+ t.Errorf("the inputs hook is wired after the edge starts (%s)", fset.Position(wiredAt))
+ }
+}