| 1 | // Command home is the Oficina suite home binary. |
| 2 | // |
| 3 | // It serves sign-in, the passkey second factor and recovery codes (§13 |
| 4 | // step 2), plus a placeholder front page, and refuses to start without |
| 5 | // the configuration those need. See |
| 6 | // docs/superpowers/specs/2026-08-30-home-design.md. |
| 7 | // |
| 8 | // The platform activation contract has no $PORT: rastrillo.Resolve and |
| 9 | // Serve own it end to end — activation argv, LISTEN_FDS, |
| 10 | // $STATE_DIRECTORY, GET /healthz, GET /api/version, SIGTERM drain, |
| 11 | // baseline security headers. |
| 12 | // |
| 13 | // THE DATABASE PATH IS A DEFAULT HERE, NOT A FLAG, and that is not a |
| 14 | // style choice. The home does not hibernate (AGENTS.md), so it runs as |
| 15 | // an ordinary unit tenant: the platform's carlos-app@.service has |
| 16 | // ExecStart=/opt/carlos/live/%i serve with NO arguments, and only the |
| 17 | // hibernate exec child is invoked with --socket and --db. A binary that |
| 18 | // required --db would therefore refuse every boot on the platform while |
| 19 | // working perfectly for a developer who typed the flag. Resolve |
| 20 | // overwrites Options.DBPath only when -db is actually passed, and joins |
| 21 | // a relative path to $STATE_DIRECTORY, so "home.db" below is the |
| 22 | // platform path and the flag still wins locally. |
| 23 | // |
| 24 | // THIS BINARY REFUSES TO START WITHOUT ITS SECRETS: HOME_DEK, |
| 25 | // HOME_PEPPER, HOME_SIGNIN_KEY, and a mail channel: CARLOS mail |
| 26 | // submission (CARLOS_MAIL=submit, set by the agent), or |
| 27 | // CARLOS_SMTP_PASS and the SMTP host and from address. That is |
| 28 | // deliberate — every one of those absences |
| 29 | // produces an app that answers /healthz and is broken or leaking. |
| 30 | // |
| 31 | // What that costs, stated plainly: the unit is Restart=always, and the |
| 32 | // activator deliberately does not treat a wedged unit as down. Deploying |
| 33 | // this build before those secrets exist would cycle the process into a |
| 34 | // binary that exits 1, systemd would respin it, and the site would 502 |
| 35 | // until a rollback. It is NOT true that "the old binary keeps serving" — |
| 36 | // that is what a promote does, not a deploy. Nothing in .amadan/ci.d |
| 37 | // deploys on merge, so merging is safe; deploying is the step that |
| 38 | // needs the secrets first. |
| 39 | // |
| 40 | // `home team-keygen` is not the platform's either: an offline command, |
| 41 | // run on Paul's machine, that generates |
| 42 | // both roles' team-provisioning keys from `alta keygen`'s output and checks |
| 43 | // a set through the loaders below (internal/home/team_keygen.go). It |
| 44 | // opens no database and reads none of this binary's secrets. |
| 45 | package main |
| 46 | |
| 47 | import ( |
| 48 | "context" |
| 49 | "log/slog" |
| 50 | "os" |
| 51 | |
| 52 | "amadan.net/rastrillo/rastrillo" |
| 53 | "amadan.net/rastrillo/rastrillo/migrate" |
| 54 | |
| 55 | "amadan.net/oficina/home/internal/home" |
| 56 | ) |
| 57 | |
| 58 | func main() { |
| 59 | if len(os.Args) > 1 && os.Args[1] == "team-keygen" { |
| 60 | os.Exit(home.RunTeamKeygen(os.Args[2:], os.Stdin, os.Stdout, os.Stderr)) |
| 61 | } |
| 62 | if len(os.Args) > 1 && os.Args[1] == "operator-v1" { |
| 63 | if len(os.Args) != 4 || os.Args[2] != "--db" { |
| 64 | os.Exit(2) |
| 65 | } |
| 66 | os.Exit(home.RunOperatorCommand(context.Background(), os.Args[3])) |
| 67 | } |
| 68 | logger := slog.Default() |
| 69 | |
| 70 | // DBPath is set here, not required as a flag — see the package |
| 71 | // comment. Relative, so Resolve joins it to $STATE_DIRECTORY. |
| 72 | opts, err := rastrillo.Resolve(rastrillo.Options{Logger: logger, DBPath: "home.db"}) |
| 73 | if err != nil { |
| 74 | logger.Error("resolve", "err", err) |
| 75 | os.Exit(1) |
| 76 | } |
| 77 | |
| 78 | // Every secret and setting whose absence is silent is validated |
| 79 | // before anything serves. Each refusal here is a case where |
| 80 | // continuing would produce an app that answers /healthz perfectly |
| 81 | // well and is broken or leaking: no DEK and addresses persist in the |
| 82 | // clear, no pepper and every person row is orphaned, no SMTP host |
| 83 | // and magic links are written to platform-held logs instead of |
| 84 | // emailed. The database path cannot be empty on this path — it is |
| 85 | // set below — but LoadSecrets checks it anyway, for the caller that |
| 86 | // builds its own Options. |
| 87 | runtime, err := home.LoadRuntime(opts.DBPath, logger) |
| 88 | if err != nil { |
| 89 | logger.Error("config", "err", err) |
| 90 | os.Exit(1) |
| 91 | } |
| 92 | |
| 93 | // Before db.Open, never after: Open calls Ping, which opens the |
| 94 | // writer pool's single long-lived connection there and then, and a |
| 95 | // hook registered afterwards misses it forever. That connection is |
| 96 | // the only one that performs DELETEs. |
| 97 | home.RegisterSecureDelete() |
| 98 | |
| 99 | // synchronous(FULL): see OpenDatabase. |
| 100 | d, err := home.OpenDatabase(opts.DBPath, logger) |
| 101 | if err != nil { |
| 102 | logger.Error("open db", "err", err) |
| 103 | os.Exit(1) |
| 104 | } |
| 105 | defer d.Close() |
| 106 | |
| 107 | // Ledgered, once at boot, forward-only. A boot that cannot reconcile |
| 108 | // its schema refuses rather than guessing, same as a failed open. |
| 109 | if _, err := migrate.Apply(context.Background(), d, runtime.ServingSchema()); err != nil { |
| 110 | logger.Error("migrate", "err", err) |
| 111 | os.Exit(1) |
| 112 | } |
| 113 | |
| 114 | home.LogSchemaInventory(context.Background(), d, runtime, logger) |
| 115 | app, err := home.NewRuntime(runtime, d, logger) |
| 116 | if err != nil { |
| 117 | logger.Error("wire app", "err", err) |
| 118 | os.Exit(1) |
| 119 | } |
| 120 | |
| 121 | // rastrillo.Options.Mux wants a *http.ServeMux; the app's own |
| 122 | // router is a chi.Router (an http.Handler), so it mounts under one. |
| 123 | // ServeOptions also turns off the framework's /_theme and |
| 124 | // speculation rules (serveopts.go). |
| 125 | opts = app.ServeOptions(opts) |
| 126 | |
| 127 | // Blanked because this file opened the handle itself: Serve would |
| 128 | // otherwise open a second one against the same file. |
| 129 | opts.DBPath = "" |
| 130 | |
| 131 | if err := rastrillo.Serve(opts); err != nil { |
| 132 | logger.Error("serve", "err", err) |
| 133 | os.Exit(1) |
| 134 | } |
| 135 | } |
| 136 | |