Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ coordinator/ Go control plane (packages live at top level, not internal
│ ├── enroll.go MDM enrollment profile generation
│ ├── invite_handlers.go invite code admin/user flows
│ ├── releases/ release HTTP, artifact validation and discovery (Controller)
│ ├── statearchive/ gated state archive download (Controller)
│ ├── admin_auth.go admin authorization and Privy OTP endpoints
│ ├── chunk_key_cache.go per-request X25519 shared-key memoization for chunk decrypt
│ ├── stats.go public network stats
Expand Down
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Darkbloom is a decentralized private inference network for Apple Silicon Macs. C
```
coordinator/ Go control plane (packages live at top level, not internal/)
├── cmd/coordinator/ main service entrypoint
├── api/ HTTP + WebSocket handlers (consumer.go, provider.go, billing_handlers.go, device_auth.go, invite_handlers.go, releases/, admin_auth.go, enroll.go, stats.go, server.go, chunk_key_cache.go, types/)
├── api/ HTTP + WebSocket handlers (consumer.go, provider.go, billing_handlers.go, device_auth.go, invite_handlers.go, releases/, statearchive/, admin_auth.go, enroll.go, stats.go, server.go, chunk_key_cache.go, types/)
├── apns/ APNs-push code-identity attestation
├── attestation/ Secure Enclave + MDA attestation verification
├── auth/ Privy JWT verification + user provisioning
Expand Down
2 changes: 1 addition & 1 deletion coordinator/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@ func (s *Server) routes() {
// inert (404) unless EIGENINFERENCE_STATE_EXPORT_ENABLED=true; admin-gated;
// encrypted to an age recipient by default. Auth + output protection are
// enforced inside the handler.
s.mux.HandleFunc("GET /v1/admin/state-export", s.handleAdminStateExport)
s.mux.HandleFunc("GET /v1/admin/state-export", s.newStateArchiveAPI().Download)

// Admin CLI auth — Privy email OTP for getting admin tokens without a browser.
s.mux.HandleFunc("POST /v1/admin/auth/init", s.handleAdminAuthInit) // no auth (sends OTP)
Expand Down
15 changes: 15 additions & 0 deletions coordinator/api/state_archive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package api

import (
"log/slog"

"github.com/eigeninference/d-inference/coordinator/api/statearchive"
)

func (s *Server) newStateArchiveAPI() *statearchive.Controller {
return statearchive.New(statearchive.Dependencies{
AdminKey: func() string { return s.adminKey },
Logger: func() *slog.Logger { return s.logger },
BearerToken: extractBearerToken,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func newStateExportServer(t *testing.T) (*httptest.Server, string) {
srv.SetAdminKey(stateExportAdminKey)

root := buildStateExportRoot(t)
t.Setenv(envStateExportRoot, root)
t.Setenv("EIGENINFERENCE_STATE_EXPORT_ROOT", root)

ts := httptest.NewServer(srv.Handler())
t.Cleanup(ts.Close)
Expand Down Expand Up @@ -147,7 +147,7 @@ func assertNoArchiveBytes(t *testing.T, resp *http.Response) {
// (a) 404 when the master switch is unset — and no archive bytes.
func TestStateExport_DisabledReturns404_DAR70(t *testing.T) {
ts, _ := newStateExportServer(t)
// envStateExportEnabled deliberately not set.
// "EIGENINFERENCE_STATE_EXPORT_ENABLED" deliberately not set.
resp := getExport(t, ts.URL, stateExportAdminKey)
defer resp.Body.Close()
if resp.StatusCode != http.StatusNotFound {
Expand All @@ -160,7 +160,7 @@ func TestStateExport_DisabledReturns404_DAR70(t *testing.T) {
// regardless of auth (master switch precedes auth).
func TestStateExport_DisabledMasksAuth404_DAR70(t *testing.T) {
ts, _ := newStateExportServer(t)
// envStateExportEnabled deliberately not set.
// "EIGENINFERENCE_STATE_EXPORT_ENABLED" deliberately not set.
for _, key := range []string{"", "wrong-key"} {
resp := getExport(t, ts.URL, key)
if resp.StatusCode != http.StatusNotFound {
Expand All @@ -176,8 +176,8 @@ func TestStateExport_DisabledMasksAuth404_DAR70(t *testing.T) {
// responses must contain zero archive bytes.
func TestStateExport_AdminAuth_DAR70(t *testing.T) {
ts, _ := newStateExportServer(t)
t.Setenv(envStateExportEnabled, "true")
t.Setenv(envStateExportAllowPlaintext, "true")
t.Setenv("EIGENINFERENCE_STATE_EXPORT_ENABLED", "true")
t.Setenv("EIGENINFERENCE_STATE_EXPORT_ALLOW_PLAINTEXT", "true")

// Missing key.
resp := getExport(t, ts.URL, "")
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestStateExport_AdminAuth_DAR70(t *testing.T) {
// (c) 412 when no recipient and plaintext not allowed — and no archive bytes.
func TestStateExport_PreconditionFailed_DAR70(t *testing.T) {
ts, _ := newStateExportServer(t)
t.Setenv(envStateExportEnabled, "true")
t.Setenv("EIGENINFERENCE_STATE_EXPORT_ENABLED", "true")
// No recipient, no plaintext allowance.
resp := getExport(t, ts.URL, stateExportAdminKey)
defer resp.Body.Close()
Expand All @@ -227,9 +227,9 @@ func TestStateExport_RootMissing500_DAR70(t *testing.T) {
ts := httptest.NewServer(srv.Handler())
t.Cleanup(ts.Close)

t.Setenv(envStateExportRoot, filepath.Join(t.TempDir(), "does-not-exist"))
t.Setenv(envStateExportEnabled, "true")
t.Setenv(envStateExportAllowPlaintext, "true")
t.Setenv("EIGENINFERENCE_STATE_EXPORT_ROOT", filepath.Join(t.TempDir(), "does-not-exist"))
t.Setenv("EIGENINFERENCE_STATE_EXPORT_ENABLED", "true")
t.Setenv("EIGENINFERENCE_STATE_EXPORT_ALLOW_PLAINTEXT", "true")

resp := getExport(t, ts.URL, stateExportAdminKey)
defer resp.Body.Close()
Expand All @@ -239,27 +239,6 @@ func TestStateExport_RootMissing500_DAR70(t *testing.T) {
assertNoArchiveBytes(t, resp)
}

// resolveStateExportRoot precedence:
// EIGENINFERENCE_STATE_EXPORT_ROOT -> USER_PERSISTENT_DATA_PATH -> /mnt/disks/userdata.
func TestResolveStateExportRootPrecedence_DAR70(t *testing.T) {
// Default when nothing is set.
t.Setenv(envStateExportRoot, "")
t.Setenv("USER_PERSISTENT_DATA_PATH", "")
if got := resolveStateExportRoot(); got != "/mnt/disks/userdata" {
t.Fatalf("default = %q, want /mnt/disks/userdata", got)
}
// USER_PERSISTENT_DATA_PATH wins over the hardcoded default.
t.Setenv("USER_PERSISTENT_DATA_PATH", "/persist")
if got := resolveStateExportRoot(); got != "/persist" {
t.Fatalf("with USER_PERSISTENT_DATA_PATH = %q, want /persist", got)
}
// Explicit override wins over everything.
t.Setenv(envStateExportRoot, "/explicit")
if got := resolveStateExportRoot(); got != "/explicit" {
t.Fatalf("with override = %q, want /explicit", got)
}
}

// HTTP-level encrypted export through a SYMLINKED root still captures children
// (mirrors prod start.sh `ln -sfn $PERSIST /data`).
func TestStateExport_SymlinkedRoot_DAR70(t *testing.T) {
Expand All @@ -274,9 +253,9 @@ func TestStateExport_SymlinkedRoot_DAR70(t *testing.T) {
if err := os.Symlink(real, link); err != nil {
t.Fatalf("symlink: %v", err)
}
t.Setenv(envStateExportRoot, link)
t.Setenv(envStateExportEnabled, "true")
t.Setenv(envStateExportAllowPlaintext, "true")
t.Setenv("EIGENINFERENCE_STATE_EXPORT_ROOT", link)
t.Setenv("EIGENINFERENCE_STATE_EXPORT_ENABLED", "true")
t.Setenv("EIGENINFERENCE_STATE_EXPORT_ALLOW_PLAINTEXT", "true")

resp := getExport(t, ts.URL, stateExportAdminKey)
defer resp.Body.Close()
Expand All @@ -297,8 +276,8 @@ func TestStateExport_SymlinkedRoot_DAR70(t *testing.T) {
// .push_imported included, bolt db opens ReadOnly with intact buckets/keys.
func TestStateExport_PlaintextZip_DAR70(t *testing.T) {
ts, _ := newStateExportServer(t)
t.Setenv(envStateExportEnabled, "true")
t.Setenv(envStateExportAllowPlaintext, "true")
t.Setenv("EIGENINFERENCE_STATE_EXPORT_ENABLED", "true")
t.Setenv("EIGENINFERENCE_STATE_EXPORT_ALLOW_PLAINTEXT", "true")

resp := getExport(t, ts.URL, stateExportAdminKey)
defer resp.Body.Close()
Expand Down Expand Up @@ -367,8 +346,8 @@ func TestStateExport_EncryptedToRecipient_DAR70(t *testing.T) {
if err != nil {
t.Fatal(err)
}
t.Setenv(envStateExportEnabled, "true")
t.Setenv(envStateExportRecipient, id.Recipient().String())
t.Setenv("EIGENINFERENCE_STATE_EXPORT_ENABLED", "true")
t.Setenv("EIGENINFERENCE_STATE_EXPORT_RECIPIENT", id.Recipient().String())

resp := getExport(t, ts.URL, stateExportAdminKey)
defer resp.Body.Close()
Expand Down Expand Up @@ -416,8 +395,8 @@ func TestStateExport_ConsistencyUnderWrites_DAR70(t *testing.T) {
if err != nil {
t.Fatal(err)
}
t.Setenv(envStateExportEnabled, "true")
t.Setenv(envStateExportRecipient, id.Recipient().String())
t.Setenv("EIGENINFERENCE_STATE_EXPORT_ENABLED", "true")
t.Setenv("EIGENINFERENCE_STATE_EXPORT_RECIPIENT", id.Recipient().String())

// Open the live db RW and hammer it with writes (mirrors MicroMDM).
dbPath := filepath.Join(root, "micromdm", "micromdm.db")
Expand Down
40 changes: 40 additions & 0 deletions coordinator/api/statearchive/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package statearchive

import (
"os"
"strconv"
"strings"

"github.com/eigeninference/d-inference/coordinator/env"
)

// State-export env vars (all namespaced under env.EnvPrefix == "EIGENINFERENCE").
const (
// envStateExportEnabled is the master switch. When != "true" the route 404s.
envStateExportEnabled = env.EnvPrefix + "_STATE_EXPORT_ENABLED"
// envStateExportRecipient is an age recipient ("age1..."). When set, output is
// encrypted to it; this is the default-secure path.
envStateExportRecipient = env.EnvPrefix + "_STATE_EXPORT_RECIPIENT"
// envStateExportAllowPlaintext permits a raw (unencrypted) zip ONLY when no
// recipient is configured. Must be explicitly "true".
envStateExportAllowPlaintext = env.EnvPrefix + "_STATE_EXPORT_ALLOW_PLAINTEXT"
// envStateExportRoot overrides the export root (primarily for tests).
envStateExportRoot = env.EnvPrefix + "_STATE_EXPORT_ROOT"
)

// resolveStateExportRoot picks the directory to archive:
// EIGENINFERENCE_STATE_EXPORT_ROOT -> USER_PERSISTENT_DATA_PATH -> /mnt/disks/userdata.
func resolveStateExportRoot() string {
return env.FirstNonEmpty(
os.Getenv(envStateExportRoot),
os.Getenv("USER_PERSISTENT_DATA_PATH"),
"/mnt/disks/userdata",
)
}

// envTrue trims the named variable and parses it with strconv.ParseBool.
// Invalid or unset values leave the state-export gate disabled.
func envTrue(name string) bool {
b, _ := strconv.ParseBool(strings.TrimSpace(os.Getenv(name)))
return b
}
24 changes: 24 additions & 0 deletions coordinator/api/statearchive/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package statearchive

import "testing"

// resolveStateExportRoot precedence:
// EIGENINFERENCE_STATE_EXPORT_ROOT -> USER_PERSISTENT_DATA_PATH -> /mnt/disks/userdata.
func TestResolveStateExportRootPrecedence_DAR70(t *testing.T) {
// Default when nothing is set.
t.Setenv(envStateExportRoot, "")
t.Setenv("USER_PERSISTENT_DATA_PATH", "")
if got := resolveStateExportRoot(); got != "/mnt/disks/userdata" {
t.Fatalf("default = %q, want /mnt/disks/userdata", got)
}
// USER_PERSISTENT_DATA_PATH wins over the hardcoded default.
t.Setenv("USER_PERSISTENT_DATA_PATH", "/persist")
if got := resolveStateExportRoot(); got != "/persist" {
t.Fatalf("with USER_PERSISTENT_DATA_PATH = %q, want /persist", got)
}
// Explicit override wins over everything.
t.Setenv(envStateExportRoot, "/explicit")
if got := resolveStateExportRoot(); got != "/explicit" {
t.Fatalf("with override = %q, want /explicit", got)
}
}
27 changes: 27 additions & 0 deletions coordinator/api/statearchive/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Package statearchive serves the coordinator's gated state archive download.
// Snapshot consistency and archive encryption remain in package stateexport.
package statearchive

import (
"log/slog"
"net/http"
)

// Dependencies keep credentials and diagnostics bound to current API settings.
type Dependencies struct {
AdminKey func() string
Logger func() *slog.Logger
BearerToken func(*http.Request) string
}

// Controller owns export authorization, response commitment and streaming.
// It retains no archive data or workers between requests.
type Controller struct {
adminKey func() string
logger func() *slog.Logger
bearerToken func(*http.Request) string
}

func New(d Dependencies) *Controller {
return &Controller{adminKey: d.AdminKey, logger: d.Logger, bearerToken: d.BearerToken}
}
Loading
Loading