Files
zeekay fcae880a1c apps/x402: charge per request over HTTP 402 for the routes an operator prices
The x402 version 2 mechanism of hanzoai/commerce (payment/x402) mounted on
the router. Nothing is priced until CLOUD_X402_CONFIG names a JSON file of
assets, prices and a facilitator; with none named the app mounts nothing.
A bad file stops the start, so a price the process did not understand
never leaves its route free. Paths match the way the router reads them
(case, //, ., .., trailing slash) and HEAD is priced as GET. A payment
settles when the handler has answered below 400.

Replaces #54, which built its own protocol on a zip it no longer builds
against; the facilitator interface takes any independent facilitator.

Testing: go build ./..., go vet ./apps/... and go test ./apps/... . pass; new
tests cover challenge, paid retry, prefix prices, every spelling of a
priced path, HEAD, the price listing, config loading and refusal.
Risk: none by default: with CLOUD_X402_CONFIG unset the app mounts nothing.
go.mod takes hanzoai/commerce v1.50.245 and the minimum versions its go.mod
requires (luxfi/kms, luxfi/log, fasthttp, x/crypto).
How to revert: revert this commit.

Claude-Session: https://claude.ai/code/session_015XYKLn69gF4x75RWePpDBV
2026-11-20 21:53:43 -08:00

211 lines
10 KiB
Go

// Package apps is the composition root for the OSS Hanzo Cloud core: the single,
// explicit list of which subsystems are linked into the binary AND the order they
// mount in.
//
// Wire() returns []cloud.MountSpec in mount order (slice position == order). There
// is no init()-registry and no order-int: adding, removing, or reordering a
// subsystem is a one-line edit to Wire(), read top-to-bottom. cmd/cloud calls
// Wire() and threads the slice into cloud.Serve — the set is defined ONCE, here.
//
// This is the local dev server: every subsystem here runs on one machine with a
// data directory as its only dependency.
//
// (This package must NOT live in package cloud: the subsystems import cloud for
// Deps + Typed, so a root-package bundle would form an import cycle. As a sibling
// subpackage it composes them without one.)
package apps
import (
"context"
"database/sql"
"errors"
"fmt"
"os"
"path/filepath"
luxlog "github.com/luxfi/log"
"github.com/zap-proto/zip"
"github.com/hanzoai/cloud"
"github.com/hanzoai/cloud/apps/blueprint"
"github.com/hanzoai/cloud/apps/template"
"github.com/hanzoai/cloud/apps/x402"
"github.com/hanzoai/cloud/cek"
"github.com/hanzoai/cloud/clients/principal"
// In-repo subsystem packages (clients/*). Each exports a Mount (and, where it
// owns process-lifetime resources, a Shutdown); Wire references them directly.
"github.com/hanzoai/cloud/clients/auditlog"
"github.com/hanzoai/cloud/clients/base"
"github.com/hanzoai/cloud/clients/code"
"github.com/hanzoai/cloud/clients/dns"
"github.com/hanzoai/cloud/clients/exec"
"github.com/hanzoai/cloud/clients/flags"
"github.com/hanzoai/cloud/clients/functions"
"github.com/hanzoai/cloud/clients/gateway"
"github.com/hanzoai/cloud/clients/ingress"
"github.com/hanzoai/cloud/clients/kms"
"github.com/hanzoai/cloud/clients/kv"
"github.com/hanzoai/cloud/clients/plugin"
"github.com/hanzoai/cloud/clients/security"
"github.com/hanzoai/cloud/clients/session"
"github.com/hanzoai/cloud/clients/storage"
"github.com/hanzoai/cloud/clients/tasks"
)
// Wire returns every linked subsystem as a cloud.MountSpec, in mount order. The
// slice position IS the order: cloud.MountAll iterates it as-given, registering
// each subsystem's teardown as a zip shutdown hook so teardown runs in reverse
// (LIFO). Enablement is a separate axis: cloud.Serve mounts only the specs
// cfg.Enabled(name) admits, so a STAGED subsystem is linked but inert until named.
func Wire() []cloud.MountSpec {
return []cloud.MountSpec{
// Pay per request over HTTP 402, for the routes an operator prices. It
// wraps the routes mounted after it, so it comes first; with no
// configuration it mounts nothing and every route is served as before.
{Name: "x402", Mount: payments, Description: "Charge per request over HTTP 402 for the routes you price."},
// Insights feature-flag evaluation seam (no routes; a hot value plane).
{Name: "flags", Mount: flags.Mount, Shutdown: flags.Shutdown, OwnsHealth: true, Description: "Feature flags: ship it dark, then turn it on for who you pick."},
// Embedded KMS secrets plane /v1/kms/*. OwnsHealth: serves its own
// fail-closed /v1/kms/health. Fails closed until CLOUD_KMS_MASTER_KEY_REF.
{Name: "kms", Mount: kms.Mount, OwnsHealth: true, Description: "Secret custody: your org's secrets sealed at rest."},
// Embedded runtime edge (/v1/ingress/*). STAGED — edge listeners stay off
// unless the operator names "ingress" in CLOUD_ENABLE.
{Name: "ingress", Mount: ingress.Mount, Shutdown: ingress.Shutdown, Description: "Your edge: automatic TLS certificates and hostname routing."},
// /v1/s3/buckets/* + /v1/s3/health. OwnsHealth (real fail-closed probe).
{Name: "storage", Mount: storage.Mount, OwnsHealth: true, Prefix: "/v1/s3", Description: "Object storage: your buckets and the files in them."},
// The /v1/dns forward head: relays the console DNS dashboard to the DNS
// control plane under the caller's own validated bearer.
{Name: "dns", Mount: dns.Mount, Description: "Your DNS records, relayed to the DNS control plane under your own bearer."},
{Name: "code", Mount: code.Mount, Shutdown: code.Shutdown, Description: "Search and symbols across your repos, for you and your agents."},
{Name: "session", Mount: session.Mount, Shutdown: ctxShutdown(session.Shutdown), Prefix: "/v1/code/sessions", Description: "Live coding-agent runs: list them, watch them, steer them."},
{Name: "security", Mount: security.Mount, Shutdown: ctxShutdown(security.Shutdown), OwnsHealth: true, Description: "Secret scanning for your code: submit sources, get findings."},
{Name: "exec", Mount: exec.Mount, Description: "The code interpreter: run a snippet in a sandbox and move files."},
{Name: "gateway", Mount: gateway.Mount, Description: "Live control of the edge policy your API applies to every request."},
{Name: "audit", Mount: auditlog.Mount, Description: "Your org's tamper-evident audit trail."},
// The local apps. Each is a REAL implementation over the embedded store,
// not a relay to a cluster: the dev edition is meant to be a working
// product on a laptop with no network, and an app that only forwards
// somewhere else would make it a client for a thing you do not have.
//
// Every route in these is a typed op, which is why they need no
// per-app integration work: one declaration is simultaneously the REST
// route, the OpenAPI schema, the MCP tool and the generated SDK method.
{Name: "base", Mount: base.Mount, Shutdown: base.Shutdown, Description: "A document store per org: collections of JSON records."},
{Name: "kv", Mount: kv.Mount, Shutdown: kv.Shutdown, Description: "Your key-value store: versioned values your apps read and write."},
{Name: "tasks", Mount: tasks.Mount, Shutdown: tasks.Shutdown, Description: "Durable work: a lease and acknowledge queue."},
// STAGED (see stagedSubsystems): functions RUNS CODE, so it is linked in
// every build but mounts only when an operator names it.
{Name: "functions", Mount: functions.Mount, Shutdown: functions.Shutdown, Description: "Serverless code: publish it and call it over HTTP."},
// Apps that import zip and nothing of their host: the same package mounts in
// any host that hands it a router and the few things it reads. Each takes
// only what it names, so an entry here is an adapter from cloud.Deps to
// that and nothing more.
{Name: "blueprint", Mount: logged("blueprint", blueprint.Mount), OwnsHealth: true, Description: "What a template costs to run: its bill of images and a compute-cost estimate."},
{Name: "template", Mount: templates, Shutdown: template.Shutdown, Description: "Starter kits you can deploy as they come, and your org's own."},
// Runtime wasm/proxy plugins — mounts dead last.
{Name: "plugins", Mount: plugin.Mount, Description: "Runtime plugins, wasm and proxy, mounted under /v1/plugins."},
}
}
// ServeSingle is the ONE way to run a single app standalone: validate `name`
// against Wire(), then serve exactly it (cloud.Serve with a one-name enable list).
// Returns an error for an unknown name rather than booting a no-op.
func ServeSingle(name string) error {
if name == "" {
return fmt.Errorf("ServeSingle: empty app name")
}
for _, spec := range Wire() {
if spec.Name == name {
return cloud.Serve(Wire(), []string{name})
}
}
return fmt.Errorf("ServeSingle: unknown app %q", name)
}
// logged adapts an app that asks its host for a router and a logger to the mount
// contract: the logger is scoped to the app's name, as cloud.Mount scopes it.
func logged(name string, mount func(*zip.App, luxlog.Logger) error) cloud.MountFunc {
return func(app *zip.App, deps cloud.Deps) error {
return mount(app, deps.Logger.New("subsystem", name))
}
}
// payments adapts x402 to cloud.Deps: the operator names a JSON file in
// CLOUD_X402_CONFIG holding the assets, the prices and the facilitator. With none
// named nothing is priced and nothing mounts; a file that does not load stops the
// start, because a price the process did not understand must not leave its route
// free.
func payments(app *zip.App, deps cloud.Deps) error {
path := os.Getenv("CLOUD_X402_CONFIG")
if path == "" {
return nil
}
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("x402: %w", err)
}
defer f.Close()
cfg, err := x402.Load(f)
if err != nil {
return err
}
return x402.Mount(app, deps.Logger.New("subsystem", "x402"), cfg)
}
// templates adapts the starter-kit gallery to cloud.Deps: the caller is whoever the
// identity boundary validated, and the private kits live in a database under the
// data directory.
func templates(app *zip.App, deps cloud.Deps) error {
return template.Mount(app, template.Deps{
Log: deps.Logger.New("subsystem", "template"),
Org: principal.OrgFrom,
Acting: principal.Acting,
Open: opener(deps.DataDir, "templates"),
})
}
// opener opens {dataDir}/{name}.db through cek, so it is encrypted at rest under
// the deployment's key, and applies the app's schema in one transaction before
// handing the database over. One connection, because one writer owns the file.
func opener(dataDir, name string) func(context.Context, func(context.Context, *sql.Tx) error) (*sql.DB, error) {
return func(ctx context.Context, migrate func(context.Context, *sql.Tx) error) (*sql.DB, error) {
if dataDir == "" {
return nil, errors.New("empty DataDir")
}
db, err := cek.Open(filepath.Join(dataDir, name+".db"))
if err != nil {
return nil, err
}
db.SetMaxOpenConns(1)
tx, err := db.BeginTx(ctx, nil)
if err != nil {
_ = db.Close()
return nil, err
}
if err := migrate(ctx, tx); err != nil {
_ = tx.Rollback()
_ = db.Close()
return nil, err
}
if err := tx.Commit(); err != nil {
_ = db.Close()
return nil, err
}
return db, nil
}
}
// ctxShutdown adapts a subsystem's zero-arg Shutdown() error to the
// cloud.ShutdownFunc(ctx) signature. Several subsystems expose the simpler form
// (their teardown ignores the deadline); this bridges the impedance mismatch in
// ONE place so the Wire entries stay declarative — no inline closures.
func ctxShutdown(f func() error) cloud.ShutdownFunc {
return func(context.Context) error { return f() }
}