oficina / equipo Public

hosted controller: a hostedlive crash point after launch

For Task 17's "kill the controller between the provider call and
transaction 2": in a hostedlive build, EQUIPO_HOSTED_CRASH_AFTER_LAUNCH=1
wraps the runner so the process exits with 137 once a provider Launch
returns a VM, before the Ref is saved. main applies runnerHook, which
only the hostedlive file sets; TestNoCrashPointWithoutTag holds it nil
in a normal build. TestCrashAfterLaunch (hostedlive, fake runner,
injectable exit) shows the exit after the launch and before transaction
2, and one VM claimed by replay after a restart.

TestLiveKeygen (hostedlive) writes the key, verify_keys and TLS files a
live run needs, in the formats config.go reads.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 60a068225ea5422d343c67cfceb0b0afe1063731 parent 1ebf6e6
6 files changed, +322 −1
  • cmd/equipo-hosted-controller/crashpoint.go +50 −0
  • cmd/equipo-hosted-controller/crashpoint_off_test.go +15 −0
  • cmd/equipo-hosted-controller/crashpoint_test.go +111 −0
  • cmd/equipo-hosted-controller/keygen_live_test.go +112 −0
  • cmd/equipo-hosted-controller/main.go +11 −1
  • docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md +23 −0
diff --git a/cmd/equipo-hosted-controller/crashpoint.go b/cmd/equipo-hosted-controller/crashpoint.go
new file mode 100644
index 00000000..ebc08910
--- /dev/null
+++ b/cmd/equipo-hosted-controller/crashpoint.go
@@ -0,0 +1,50 @@
+//go:build hostedlive
+
+package main
+
+// The Task 17 crash point. A controller built with -tags hostedlive and run
+// with EQUIPO_HOSTED_CRASH_AFTER_LAUNCH=1 exits with status 137, as if
+// killed, as soon as a provider Launch returns a VM: after the provider call
+// and before transaction 2 saves the Ref. Restarted without the variable,
+// the reaper's first pass replays the intent with the same client token and
+// claims that VM. A build without the tag holds none of this
+// (TestNoCrashPointWithoutTag).
+
+import (
+ "context"
+ "log"
+ "os"
+
+ "amadan.net/oficina/equipo/internal/isolatedjob"
+)
+
+// crashAfterLaunchEnv switches the crash point on in a hostedlive build.
+const crashAfterLaunchEnv = "EQUIPO_HOSTED_CRASH_AFTER_LAUNCH"
+
+// crashExit ends the process; tests replace it.
+var crashExit = os.Exit
+
+func init() { runnerHook = crashAfterLaunch }
+
+// crashAfterLaunch wraps r with the crash point when the variable is "1",
+// and returns r itself otherwise.
+func crashAfterLaunch(r isolatedjob.Runner) isolatedjob.Runner {
+ if os.Getenv(crashAfterLaunchEnv) != "1" {
+ return r
+ }
+ log.Printf("equipo-hosted-controller: %s=1: the process exits after the next provider launch that returns a VM", crashAfterLaunchEnv)
+ return crashRunner{r}
+}
+
+// crashRunner is a Runner whose Launch exits the process once the provider
+// has returned a VM.
+type crashRunner struct{ isolatedjob.Runner }
+
+func (c crashRunner) Launch(ctx context.Context, s isolatedjob.LaunchSpec) (isolatedjob.Ref, error) {
+ ref, err := c.Runner.Launch(ctx, s)
+ if err == nil {
+ log.Printf("equipo-hosted-controller: crash point: the provider launched %s for %s; exiting before the Ref is saved", ref, s.AttemptID)
+ crashExit(137)
+ }
+ return ref, err
+}
diff --git a/cmd/equipo-hosted-controller/crashpoint_off_test.go b/cmd/equipo-hosted-controller/crashpoint_off_test.go
new file mode 100644
index 00000000..5795ec65
--- /dev/null
+++ b/cmd/equipo-hosted-controller/crashpoint_off_test.go
@@ -0,0 +1,15 @@
+//go:build !hostedlive
+
+package main
+
+import "testing"
+
+// TestNoCrashPointWithoutTag: a build without the hostedlive tag has no
+// runner hook, so EQUIPO_HOSTED_CRASH_AFTER_LAUNCH can change nothing in
+// it. This test binary is such a build.
+func TestNoCrashPointWithoutTag(t *testing.T) {
+ t.Setenv("EQUIPO_HOSTED_CRASH_AFTER_LAUNCH", "1")
+ if runnerHook != nil {
+ t.Fatal("runnerHook is set in a build without the hostedlive tag; the Task 17 crash point must not ship")
+ }
+}
diff --git a/cmd/equipo-hosted-controller/crashpoint_test.go b/cmd/equipo-hosted-controller/crashpoint_test.go
new file mode 100644
index 00000000..76cc273a
--- /dev/null
+++ b/cmd/equipo-hosted-controller/crashpoint_test.go
@@ -0,0 +1,111 @@
+//go:build hostedlive
+
+package main
+
+import (
+ "context"
+ "crypto/ed25519"
+ "errors"
+ "path/filepath"
+ "testing"
+ "time"
+
+ "amadan.net/oficina/equipo/internal/hostedctl"
+ "amadan.net/oficina/equipo/internal/hostedwire"
+ "amadan.net/oficina/equipo/internal/isolatedjob"
+ "amadan.net/oficina/equipo/internal/isolatedjob/isolatedjobtest"
+)
+
+var errCrashed = errors.New("crash point reached")
+
+// crashCtl is a controller over the store at path and the given runner,
+// with a passing network audit.
+func crashCtl(t *testing.T, path string, clk *isolatedjobtest.FakeClock, run isolatedjob.Runner, fake *isolatedjob.Fake) (*hostedctl.Controller, *hostedctl.Store) {
+ t.Helper()
+ ctx := context.Background()
+ store, err := hostedctl.Open(ctx, path, clk)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { _ = store.Close() }) // a second Close does nothing
+ c, err := hostedctl.New(hostedctl.Config{Enabled: true, Images: map[isolatedjob.ImageDigest]bool{testDigest: true},
+ ProviderNamespace: "test", ProviderFingerprint: func(isolatedjob.ImageDigest, isolatedjob.Size) string { return "" }},
+ hostedctl.Deps{Store: store, Runner: run, Auditor: fake, Alerter: nopAlerter{}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := c.RunAudit(ctx); err != nil {
+ t.Fatal(err)
+ }
+ return c, store
+}
+
+// TestCrashAfterLaunch: with EQUIPO_HOSTED_CRASH_AFTER_LAUNCH=1 the hook
+// exits with 137 after the provider created the VM and before transaction 2
+// saved its Ref. A controller restarted on the same database without the
+// variable replays the intent and claims that one VM.
+func TestCrashAfterLaunch(t *testing.T) {
+ ctx := context.Background()
+ exitCode := 0
+ saved := crashExit
+ t.Cleanup(func() { crashExit = saved })
+ crashExit = func(code int) {
+ exitCode = code
+ panic(errCrashed)
+ }
+
+ clk := isolatedjobtest.NewFakeClock(time.Date(2026, 11, 23, 12, 0, 0, 0, time.UTC))
+ fake := isolatedjob.NewFake(clk, isolatedjob.SpikeLimits())
+ path := filepath.Join(t.TempDir(), "hosted.db")
+ key := hostedwire.VerifyKey{Public: instKey.Public().(ed25519.PublicKey), Role: hostedwire.RoleInstance}
+ q := hostedctl.LaunchRequest{AttemptID: "crash-1", TaskID: "task-1", Account: "acct-1", Image: testDigest,
+ Size: isolatedjob.SizeSmall, MaxLifetime: time.Hour, InstanceCode: "code-1"}
+
+ t.Setenv(crashAfterLaunchEnv, "1")
+ if runnerHook == nil {
+ t.Fatal("runnerHook is nil in a hostedlive build")
+ }
+ c, store := crashCtl(t, path, clk, runnerHook(fake), fake)
+ crashed := func() (r any) {
+ defer func() { r = recover() }()
+ _, _ = c.Launch(ctx, instID, key, q)
+ return nil
+ }()
+ if crashed != errCrashed || exitCode != 137 {
+ t.Fatalf("Launch with the crash point on: recovered %v, exit code %d; want the crash point to exit with 137", crashed, exitCode)
+ }
+ if n := fake.LiveByAttempt()[q.AttemptID]; n != 1 {
+ t.Fatalf("provider VMs for %s at the crash = %d, want 1: the exit must come after the provider call", q.AttemptID, n)
+ }
+ v, err := c.Get(ctx, instID, q.AttemptID)
+ if err != nil {
+ t.Fatalf("Get after the crash = %v", err)
+ }
+ if v.State != hostedctl.JobWaiting || v.VMRef != "" {
+ t.Fatalf("job after the crash = %s with VM %q, want %s with no VM: the exit must come before transaction 2", v.State, v.VMRef, hostedctl.JobWaiting)
+ }
+ if err := store.Close(); err != nil {
+ t.Fatal(err)
+ }
+
+ // The restart, without the variable: the hook hands back the runner itself.
+ t.Setenv(crashAfterLaunchEnv, "")
+ run := runnerHook(fake)
+ if run != isolatedjob.Runner(fake) {
+ t.Fatalf("runnerHook without %s = %T, want the runner unwrapped", crashAfterLaunchEnv, run)
+ }
+ c, _ = crashCtl(t, path, clk, run, fake)
+ if err := c.Replay(ctx); err != nil {
+ t.Fatalf("Replay after the restart = %v", err)
+ }
+ v, err = c.Get(ctx, instID, q.AttemptID)
+ if err != nil {
+ t.Fatalf("Get after the replay = %v", err)
+ }
+ if v.State != hostedctl.JobStarting || v.VMRef == "" {
+ t.Fatalf("job after the replay = %s with VM %q, want %s with the VM", v.State, v.VMRef, hostedctl.JobStarting)
+ }
+ if n := fake.LiveByAttempt()[q.AttemptID]; n != 1 {
+ t.Fatalf("provider VMs for %s after the replay = %d, want 1", q.AttemptID, n)
+ }
+}
diff --git a/cmd/equipo-hosted-controller/keygen_live_test.go b/cmd/equipo-hosted-controller/keygen_live_test.go
new file mode 100644
index 00000000..587085bb
--- /dev/null
+++ b/cmd/equipo-hosted-controller/keygen_live_test.go
@@ -0,0 +1,112 @@
+//go:build hostedlive
+
+package main
+
+import (
+ "crypto/ecdsa"
+ "crypto/ed25519"
+ "crypto/elliptic"
+ "crypto/rand"
+ "crypto/x509"
+ "crypto/x509/pkix"
+ "encoding/base64"
+ "encoding/json"
+ "encoding/pem"
+ "math/big"
+ "net"
+ "os"
+ "path/filepath"
+ "testing"
+ "time"
+)
+
+// keygenDirEnv names the directory TestLiveKeygen writes to.
+const keygenDirEnv = "EQUIPO_HOSTED_KEYGEN_DIR"
+
+// TestLiveKeygen writes the key and certificate files a Task 17 run needs
+// into the existing directory EQUIPO_HOSTED_KEYGEN_DIR, refusing to replace
+// any file:
+//
+// controller.key the controller's signing_key_file: 32-byte ed25519 seed, standard base64, mode 0600
+// controller.pub its public key, standard base64 (EQUIPO_HOSTED_LIVE_CONTROLLER_KEY_FILE)
+// instance.key instance key seed, as controller.key (EQUIPO_HOSTED_LIVE_KEY_FILE)
+// operator.key operator key seed, as controller.key (EQUIPO_HOSTED_LIVE_OPERATOR_KEY_FILE)
+// verify_keys.json the config's verify_keys: task17-instance (instance), task17-operator (operator)
+// tls.crt, tls.key a self-signed P-256 certificate for 127.0.0.1, valid 7 days; tls.crt is also
+// EQUIPO_HOSTED_LIVE_CA_FILE
+//
+// It checks controller.key and verify_keys.json as the controller's config
+// loader reads them. It makes no network call.
+func TestLiveKeygen(t *testing.T) {
+ dir := os.Getenv(keygenDirEnv)
+ if dir == "" {
+ t.Skip(keygenDirEnv + " is not set")
+ }
+ write := func(name string, data []byte, mode os.FileMode) string {
+ t.Helper()
+ path := filepath.Join(dir, name)
+ f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, mode)
+ if err != nil {
+ t.Fatalf("write %s: %v", path, err)
+ }
+ if _, err := f.Write(data); err != nil {
+ t.Fatalf("write %s: %v", path, err)
+ }
+ if err := f.Close(); err != nil {
+ t.Fatalf("write %s: %v", path, err)
+ }
+ return path
+ }
+ seed := func(name string) ed25519.PrivateKey {
+ t.Helper()
+ s := make([]byte, ed25519.SeedSize)
+ if _, err := rand.Read(s); err != nil {
+ t.Fatal(err)
+ }
+ write(name+".key", []byte(base64.StdEncoding.EncodeToString(s)+"\n"), 0o600)
+ return ed25519.NewKeyFromSeed(s)
+ }
+ pub := func(k ed25519.PrivateKey) string {
+ return base64.StdEncoding.EncodeToString(k.Public().(ed25519.PublicKey))
+ }
+
+ ctl := seed("controller")
+ write("controller.pub", []byte(pub(ctl)+"\n"), 0o644)
+ inst, op := seed("instance"), seed("operator")
+ entries := []keyEntry{
+ {ID: "task17-instance", Role: "instance", PublicKey: pub(inst)},
+ {ID: "task17-operator", Role: "operator", PublicKey: pub(op)},
+ }
+ if _, err := verifyKeys(entries); err != nil {
+ t.Fatalf("the controller would refuse these verify keys: %v", err)
+ }
+ vk, err := json.MarshalIndent(entries, "", " ")
+ if err != nil {
+ t.Fatal(err)
+ }
+ write("verify_keys.json", append(vk, '\n'), 0o644)
+ if got, err := readSigningKey(filepath.Join(dir, "controller.key")); err != nil || !got.Equal(ctl) {
+ t.Fatalf("the controller reads controller.key as %v, %v", got.Public(), err)
+ }
+
+ tlsKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
+ if err != nil {
+ t.Fatal(err)
+ }
+ now := time.Now()
+ tmpl := &x509.Certificate{SerialNumber: big.NewInt(now.UnixNano()), Subject: pkix.Name{CommonName: "equipo-hosted-controller task17"},
+ NotBefore: now.Add(-time.Hour), NotAfter: now.Add(7 * 24 * time.Hour), IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1)},
+ KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
+ BasicConstraintsValid: true, IsCA: true}
+ der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &tlsKey.PublicKey, tlsKey)
+ if err != nil {
+ t.Fatal(err)
+ }
+ keyDER, err := x509.MarshalPKCS8PrivateKey(tlsKey)
+ if err != nil {
+ t.Fatal(err)
+ }
+ write("tls.crt", pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}), 0o644)
+ write("tls.key", pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyDER}), 0o600)
+ t.Logf("wrote controller.key, controller.pub, instance.key, operator.key, verify_keys.json, tls.crt and tls.key to %s", dir)
+}
diff --git a/cmd/equipo-hosted-controller/main.go b/cmd/equipo-hosted-controller/main.go
index 2cce51d3..daac4320 100644
--- a/cmd/equipo-hosted-controller/main.go
+++ b/cmd/equipo-hosted-controller/main.go
@@ -101,7 +101,11 @@ func run(path string) error {
}
metrics := hostedctl.NewMetrics()
alerter := metrics.CountAlerts(snsAlerter{pub: awsjob.SNSPublisher{TopicARN: cfg.topicARN, Client: sns.NewFromConfig(awsCfg)}})
- c, err := hostedctl.New(cfg.ctl, hostedctl.Deps{Store: store, Runner: runner, Auditor: runner, Alerter: alerter})
+ var jobs isolatedjob.Runner = runner
+ if runnerHook != nil {
+ jobs = runnerHook(jobs)
+ }
+ c, err := hostedctl.New(cfg.ctl, hostedctl.Deps{Store: store, Runner: jobs, Auditor: runner, Alerter: alerter})
if err != nil {
return err
}
@@ -113,6 +117,12 @@ func run(path string) error {
return serve(ctx, c, newAPIServer(api, cert), apiLn, newMetricsServer(metrics.Handler(c)), metricsLn, shutdownTimeout)
}
+// runnerHook, when set, wraps the runner the controller calls. Only a build
+// with the hostedlive tag sets it (crashpoint.go, the Task 17 crash point);
+// in every other build it is nil and the controller calls the AWS runner
+// directly.
+var runnerHook func(isolatedjob.Runner) isolatedjob.Runner
+
// callerIdentity is the STS call providerNamespace makes.
// *sts.Client satisfies it.
type callerIdentity interface {
diff --git a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
index 8b5dfc97..123a3d3c 100644
--- a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
+++ b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
@@ -1048,3 +1048,26 @@ Offline preparation for the live check; no AWS call was made. The live pieces co
stop with proof). Both skip without `EQUIPO_HOSTED_LIVE_URL` and stop their VM on failure.
- Checks: `go vet -tags hostedlive ./internal/hostedclient`, `go vet ./internal/hostedclient`,
`go test ./internal/hostedclient -count=1` ok. Not run (live). No guard to mutate.
+
+### The crash point after launch, and the key files
+
+- `cmd/equipo-hosted-controller/crashpoint.go` (`hostedlive` only) sets `runnerHook`, which `main`
+ applies to the runner it hands the controller. With `EQUIPO_HOSTED_CRASH_AFTER_LAUNCH=1` the
+ wrapped Launch exits with 137 once the provider has returned a VM, before transaction 2 saves the
+ Ref; otherwise the hook returns the runner itself. A normal build has `runnerHook` nil.
+- `TestNoCrashPointWithoutTag` (untagged builds only): `runnerHook` is nil.
+- `TestCrashAfterLaunch` (`hostedlive`, fake runner, injectable exit): the exit comes with 137, the
+ fake holds one VM for the attempt, the job is still `waiting` with no Ref; a controller reopened
+ on the same database without the variable replays the intent and holds that one VM, `starting`.
+- `TestLiveKeygen` (`hostedlive`, skips without `EQUIPO_HOSTED_KEYGEN_DIR`): writes the controller,
+ instance and operator keys, `verify_keys.json`, and a self-signed TLS pair for 127.0.0.1, refusing
+ to replace a file, and checks them with the config loader's own readers.
+- Runs: `go test ./cmd/equipo-hosted-controller -count=1` ok; `go test -tags hostedlive
+ ./cmd/equipo-hosted-controller -run 'TestCrashAfterLaunch$|TestLiveKeygen$' -count=1` ok (both
+ offline: the fake runner, and a keygen run into a scratch directory, since removed); `go vet` with
+ and without the tag; `make build vet`; `make budget`; the Task 7 guard.
+
+| Mutation | Command | Observed failure | Restored |
+| --- | --- | --- | --- |
+| Drop the `hostedlive` build line from `crashpoint.go` | `go test ./cmd/equipo-hosted-controller -run 'TestNoCrashPointWithoutTag' -count=1` | `crashpoint_off_test.go:13: runnerHook is set in a build without the hostedlive tag; the Task 17 crash point must not ship` | yes, passes |
+| Exit before the provider call (`crashExit(137)` first in `crashRunner.Launch`) | `go test -tags hostedlive ./cmd/equipo-hosted-controller -run 'TestCrashAfterLaunch$' -count=1` | `crashpoint_test.go:78: provider VMs for crash-1 at the crash = 0, want 1: the exit must come after the provider call` | yes, passes |