devbox mode (#474): the edge serves a local static tree
WithLocalStatic(root) makes staticHandlerFor serve <root>/<app>/current, a symlink resolved on every request so a registrar can swap it by rename and delete the old tree. Parsed _headers and _redirects are cached for the most recent target only. ExtractSite exports the site-tar extractor.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/edge/edge.go+8 −0internal/edge/localstatic.go+94 −0internal/edge/localstatic_test.go+180 −0internal/edge/static.go+5 −0
Intent
<task-notification> <task-id>ad2b0a2a829e54450</task-id> <tool-use-id>toolu_01GJvsej941a3qJrRuMMVmcT</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/9b370a04-693c-4bda-b2eb-9a4b08f7626d/tasks/ad2b0a2a829e54450.output</output-file> <status>completed</status> <summary>Agent "Implement Task 1: devbox copy deck" 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: b326d2f0 "devbox mode copy deck: add DBX-C50..C52, the developer notes from spec 5.4" (on top of e039231b) Tests: `go test -run 'TestDevboxCopy' .` passes. The pinned inventory is now DBX-C1..C52, all rows `pending`. As before, I ran it with the sandbox off because the default Go cache is read-only inside it. Changes: - **C50:** dev servers that check the Host header need the devbox domain added once per project (Vite `server.allowedHosts`, Rails `config.hosts`). - **C51:** live reload must connect on port 443 (Vite `hmr.clientPort`). - **C52:** loopback ports are shared by every account, so a link's owner controls where its name points but not who listens on that port. - **C19, C20, C22:** unchanged, as you ruled. Concerns: the deck is now 52 rows, further above the 25 to 40 aim. The three new rows sit at the end of the runbook table, after the smoke-check items, with no heading of their own. Report: /home/paulca/amadan.net/carlos/platform/.claude/worktrees/devbox-mode/.superpowers/sdd/2026-10-30-devbox-mode/task-1-report.md</result> <usage><subagent_tokens>83597</subagent_tokens><tool_uses>14</tool_uses><duration_ms>173597</duration_ms></usage> </task-notification>
Attributed to this commit: 16.8 g CO₂e (2,032,062 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on design-devbox-mode, 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/edge/edge.go b/internal/edge/edge.go| index f6a9f8421..ce819dc04 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -162,6 +162,9 @@ type Edge struct { |
| staticStore release.Store |
| staticPub ed25519.PublicKey |
| staticCacheDir string |
| + // localStaticRoot, when set (WithLocalStatic), is the directory static |
| + // routes are served from instead of staticStore. See localstatic.go. |
| + localStaticRoot string |
| // staticLock is this process's shared hold on the static cache |
| // (sweepStaticCache): a starting edge sweeps only when it is alone. |
| staticLock *os.File |
| @@ -698,6 +701,11 @@ func (e *Edge) SetStaticRecheck(d time.Duration) { e.staticRecheck = d } |
| // class when EnableStatic has configured it, else the pre-config 404 (the |
| // route exists — and its cert can be minted — before the bytes can flow). |
| func (e *Edge) staticHandlerFor(rt registry.Route) http.Handler { |
| + // Devbox mode (localstatic.go): a local spool answers every static route, |
| + // with no store, key or cache directory involved. |
| + if e.localStaticRoot != "" { |
| + return newLocalStaticHandler(rt, e.localStaticRoot) |
| + } |
| if e.staticStore == nil || len(e.staticPub) == 0 || rt.App == "" { |
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| http.Error(w, "static serving not configured", http.StatusNotFound) |
diff --git a/internal/edge/localstatic.go b/internal/edge/localstatic.go| new file mode 100644 |
| index 000000000..d20015348 |
| --- /dev/null |
| +++ b/internal/edge/localstatic.go |
| @@ -0,0 +1,94 @@ |
| +package edge |
| + |
| +// Devbox mode serves a static route from a directory on this machine instead |
| +// of the release store (devbox mode design, #474). Nothing here fetches, |
| +// verifies or extracts: a registrar, in another process, writes each snapshot |
| +// into <root>/<app>/<unix-nanos>/ and then points <root>/<app>/current at it |
| +// by renaming a fresh symlink over the old one, deleting the previous tree |
| +// afterwards. So the handler resolves "current" on every request and holds no |
| +// file handle past the request that opened it: the link can move between two |
| +// requests, and the tree it used to name can vanish under a slow one (which |
| +// then answers a 404 for the file it lost, as any deleted file would). |
| + |
| +import ( |
| + "net/http" |
| + "path/filepath" |
| + "strings" |
| + "sync" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// localStaticChannel is the channel name a local tree reports in log lines: |
| +// there is no release channel, only the workstation it serves from. |
| +const localStaticChannel = "devbox" |
| + |
| +// WithLocalStatic makes the edge serve every static route from the tree at |
| +// <root>/<app>/current rather than from the release store. A symlink |
| +// <root>/<app>/current names the tree; see the layout above. Without it |
| +// static routes behave exactly as before. |
| +func WithLocalStatic(root string) RunOption { |
| + return func(e *Edge) { e.localStaticRoot = root } |
| +} |
| + |
| +// localStatic serves one app's current local tree. |
| +type localStatic struct { |
| + site *staticSite // carries app and channel for serveTree's log lines only |
| + link string // <root>/<app>/current |
| + |
| + // The parsed _headers and _redirects of the most recent target. One entry |
| + // is enough (a request needs only the tree "current" names now) and keeps |
| + // a long run of swaps from growing the cache: each swap replaces it. |
| + mu sync.Mutex |
| + tree servedTree |
| + // target is the resolved directory tree was parsed from; "" before the |
| + // first request. |
| + target string |
| +} |
| + |
| +// newLocalStaticHandler builds the handler for one static route in devbox |
| +// mode. An app name that is not a single path element could point the link |
| +// outside the root, so it serves nothing. |
| +func newLocalStaticHandler(rt registry.Route, root string) http.Handler { |
| + if rt.App == "" || rt.App == "." || rt.App == ".." || strings.ContainsAny(rt.App, `/\`) { |
| + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + http.Error(w, "static serving not configured", http.StatusNotFound) |
| + }) |
| + } |
| + return &localStatic{ |
| + site: &staticSite{app: rt.App, channel: localStaticChannel}, |
| + link: filepath.Join(root, rt.App, "current"), |
| + } |
| +} |
| + |
| +func (l *localStatic) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| + // EvalSymlinks fails on a missing or dangling link, which is the state of |
| + // an app nothing has been published for yet, or one caught between |
| + // removal and the next publish. |
| + target, err := filepath.EvalSymlinks(l.link) |
| + if err != nil { |
| + http.NotFound(w, r) |
| + return |
| + } |
| + l.site.serveTree(l.treeFor(target), w, r) |
| +} |
| + |
| +// treeFor returns the served tree for a resolved target, parsing its rule |
| +// files only when the target is not the one parsed last. |
| +func (l *localStatic) treeFor(target string) servedTree { |
| + l.mu.Lock() |
| + defer l.mu.Unlock() |
| + if l.target != target { |
| + // The snapshot directory is named for the moment it was taken, so it |
| + // doubles as the version the response header reports. |
| + version := filepath.Base(target) |
| + l.tree = servedTree{ |
| + dir: target, |
| + version: version, |
| + redirects: l.site.loadRedirects(target, version), |
| + headers: l.site.loadHeaders(target, version), |
| + } |
| + l.target = target |
| + } |
| + return l.tree |
| +} |
diff --git a/internal/edge/localstatic_test.go b/internal/edge/localstatic_test.go| new file mode 100644 |
| index 000000000..223f1bab8 |
| --- /dev/null |
| +++ b/internal/edge/localstatic_test.go |
| @@ -0,0 +1,180 @@ |
| +package edge |
| + |
| +import ( |
| + "net/http" |
| + "net/http/httptest" |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// localTree writes files under <root>/<app>/<name>/ and returns the tree dir. |
| +func localTree(t *testing.T, root, app, name string, files map[string]string) string { |
| + t.Helper() |
| + dir := filepath.Join(root, app, name) |
| + for rel, body := range files { |
| + p := filepath.Join(dir, filepath.FromSlash(rel)) |
| + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(p, []byte(body), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + return dir |
| +} |
| + |
| +// pointCurrent swaps <root>/<app>/current onto name the way the registrar |
| +// does: a new symlink beside it, renamed over it. |
| +func pointCurrent(t *testing.T, root, app, name string) { |
| + t.Helper() |
| + cur := filepath.Join(root, app, "current") |
| + tmp := cur + ".new" |
| + os.Remove(tmp) |
| + if err := os.Symlink(name, tmp); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Rename(tmp, cur); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func localGet(t *testing.T, h http.Handler, path string) *httptest.ResponseRecorder { |
| + t.Helper() |
| + req := httptest.NewRequest("GET", "http://shop.example.com/", nil) |
| + req.URL.Path = path |
| + rec := httptest.NewRecorder() |
| + h.ServeHTTP(rec, req) |
| + return rec |
| +} |
| + |
| +func TestLocalStaticServesCurrentTree(t *testing.T) { |
| + root := t.TempDir() |
| + localTree(t, root, "shop", "t1", map[string]string{"index.html": "hello t1"}) |
| + pointCurrent(t, root, "shop", "t1") |
| + h := newLocalStaticHandler(registry.Route{Kind: "static", App: "shop"}, root) |
| + rec := localGet(t, h, "/") |
| + if rec.Code != 200 || rec.Body.String() != "hello t1" { |
| + t.Fatalf("GET / = %d %q, want 200 %q", rec.Code, rec.Body.String(), "hello t1") |
| + } |
| +} |
| + |
| +func TestLocalStaticAppliesHeaders(t *testing.T) { |
| + root := t.TempDir() |
| + localTree(t, root, "shop", "t1", map[string]string{ |
| + "index.html": "x", |
| + "_headers": "/*\n Access-Control-Allow-Origin: *\n", |
| + }) |
| + pointCurrent(t, root, "shop", "t1") |
| + h := newLocalStaticHandler(registry.Route{Kind: "static", App: "shop"}, root) |
| + rec := localGet(t, h, "/") |
| + if got := rec.Header().Get("Access-Control-Allow-Origin"); got != "*" { |
| + t.Fatalf("Access-Control-Allow-Origin = %q, want * (headers %v)", got, rec.Header()) |
| + } |
| +} |
| + |
| +func TestLocalStaticFollowsSwap(t *testing.T) { |
| + root := t.TempDir() |
| + localTree(t, root, "shop", "t1", map[string]string{ |
| + "index.html": "one", |
| + "_headers": "/*\n Cache-Control: max-age=60\n", |
| + }) |
| + localTree(t, root, "shop", "t2", map[string]string{ |
| + "index.html": "two", |
| + "_headers": "/*\n Cache-Control: max-age=120\n", |
| + }) |
| + pointCurrent(t, root, "shop", "t1") |
| + h := newLocalStaticHandler(registry.Route{Kind: "static", App: "shop"}, root) |
| + if rec := localGet(t, h, "/"); rec.Body.String() != "one" || rec.Header().Get("Cache-Control") != "max-age=60" { |
| + t.Fatalf("before swap: %q %q", rec.Body.String(), rec.Header().Get("Cache-Control")) |
| + } |
| + pointCurrent(t, root, "shop", "t2") |
| + rec := localGet(t, h, "/") |
| + if rec.Body.String() != "two" || rec.Header().Get("Cache-Control") != "max-age=120" { |
| + t.Fatalf("after swap: %q %q, want two/max-age=120", rec.Body.String(), rec.Header().Get("Cache-Control")) |
| + } |
| + // The registrar deletes the old tree once the swap is done; the next |
| + // request must not care. |
| + if err := os.RemoveAll(filepath.Join(root, "shop", "t1")); err != nil { |
| + t.Fatal(err) |
| + } |
| + if rec := localGet(t, h, "/"); rec.Code != 200 || rec.Body.String() != "two" { |
| + t.Fatalf("after old tree removed: %d %q", rec.Code, rec.Body.String()) |
| + } |
| +} |
| + |
| +func TestLocalStaticMissingIs404(t *testing.T) { |
| + root := t.TempDir() |
| + h := newLocalStaticHandler(registry.Route{Kind: "static", App: "shop"}, root) |
| + if rec := localGet(t, h, "/"); rec.Code != http.StatusNotFound { |
| + t.Fatalf("no current: %d, want 404", rec.Code) |
| + } |
| + // A dangling link (the tree was deleted under it) is the same answer. |
| + if err := os.MkdirAll(filepath.Join(root, "shop"), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + pointCurrent(t, root, "shop", "gone") |
| + if rec := localGet(t, h, "/"); rec.Code != http.StatusNotFound { |
| + t.Fatalf("dangling current: %d, want 404", rec.Code) |
| + } |
| +} |
| + |
| +func TestLocalStaticRefusesDotDot(t *testing.T) { |
| + root := t.TempDir() |
| + localTree(t, root, "shop", "t1", map[string]string{"index.html": "x"}) |
| + pointCurrent(t, root, "shop", "t1") |
| + h := newLocalStaticHandler(registry.Route{Kind: "static", App: "shop"}, root) |
| + if rec := localGet(t, h, "/../x"); rec.Code != http.StatusBadRequest { |
| + t.Fatalf("GET /../x = %d, want 400", rec.Code) |
| + } |
| +} |
| + |
| +// TestLocalStaticCacheIsBounded: a long run of swaps keeps one parsed rule |
| +// set, the latest target's, not one per snapshot ever served. |
| +func TestLocalStaticCacheIsBounded(t *testing.T) { |
| + root := t.TempDir() |
| + h := newLocalStaticHandler(registry.Route{Kind: "static", App: "shop"}, root).(*localStatic) |
| + for _, n := range []string{"t1", "t2", "t3"} { |
| + localTree(t, root, "shop", n, map[string]string{"index.html": n, "_headers": "/*\n Cache-Control: max-age=" + n[1:] + "0\n"}) |
| + pointCurrent(t, root, "shop", n) |
| + if rec := localGet(t, h, "/"); rec.Header().Get("Cache-Control") != "max-age="+n[1:]+"0" { |
| + t.Fatalf("%s: Cache-Control = %q", n, rec.Header().Get("Cache-Control")) |
| + } |
| + } |
| + h.mu.Lock() |
| + defer h.mu.Unlock() |
| + if !strings.HasSuffix(h.target, "t3") { |
| + t.Fatalf("cached target = %q, want the latest", h.target) |
| + } |
| +} |
| + |
| +// TestLocalStaticRouteThroughEdge: staticHandlerFor hands a static route to |
| +// the local tree when WithLocalStatic is set, with no store configured. |
| +func TestLocalStaticRouteThroughEdge(t *testing.T) { |
| + root := t.TempDir() |
| + localTree(t, root, "shop", "t1", map[string]string{"index.html": "via edge"}) |
| + pointCurrent(t, root, "shop", "t1") |
| + e, err := New(openReg(t), nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + WithLocalStatic(root)(e) |
| + rec := localGet(t, e.staticHandlerFor(registry.Route{Kind: "static", App: "shop"}), "/") |
| + if rec.Code != 200 || rec.Body.String() != "via edge" { |
| + t.Fatalf("GET / = %d %q", rec.Code, rec.Body.String()) |
| + } |
| +} |
| + |
| +func TestStoreStaticUnchangedWithoutLocalRoot(t *testing.T) { |
| + e, err := New(openReg(t), nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + rec := localGet(t, e.staticHandlerFor(registry.Route{Kind: "static", App: "shop"}), "/") |
| + if rec.Code != http.StatusNotFound || !strings.Contains(rec.Body.String(), "static serving not configured") { |
| + t.Fatalf("GET / = %d %q, want the 404 stub", rec.Code, rec.Body.String()) |
| + } |
| +} |
diff --git a/internal/edge/static.go b/internal/edge/static.go| index f2ff754c4..bddfeadbe 100644 |
| --- a/internal/edge/static.go |
| +++ b/internal/edge/static.go |
| @@ -423,6 +423,11 @@ func (s *staticSite) pointCurrent(name string) error { |
| return nil |
| } |
| +// ExtractSite is extractSiteTar for callers outside this package: the devbox |
| +// registrar (localstatic.go) fills its local tree with it, under the same |
| +// refusals. |
| +func ExtractSite(data []byte, dst string) error { return extractSiteTar(data, dst) } |
| + |
| // extractSiteTar unpacks a verified site tar into dst, rejecting anything |
| // that would write outside it: absolute names, ".." traversal, and non-file |
| // entry types (a symlink in a site tar could alias the extraction outside |