console, cli: operators mark house accounts, whose checkout carries the 100% coupon (plans 6b)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
12 files changed,
+278
−6
billing_cli.go+39 −1billing_cli_copy.go+12 −2billing_cli_test.go+57 −0internal/console/apibilling_copy.go+2 −0internal/console/apibillinghouse.go+59 −0internal/console/apibillinghouse_test.go+84 −0internal/console/authcli.go+2 −0internal/console/claim_invariant_fixtures_test.go+1 −0internal/console/claim_invariant_probes_nonapp_test.go+15 −0internal/console/svcroutes_test.go+5 −2internal/console/testdata/claim-routes.txt+1 −0main.go+1 −1
CI failed — run details
diff --git a/billing_cli.go b/billing_cli.go| index cd070527e..722c73662 100644 |
| --- a/billing_cli.go |
| +++ b/billing_cli.go |
| @@ -2,7 +2,8 @@ package main |
| // `carlos billing`: a deployment operator lists the billing problems the |
| // console could not settle, and closes one once it is settled at the |
| -// provider (internal/console/apibilling.go; plans 6a). |
| +// provider (internal/console/apibilling.go; plans 6a), and marks or clears |
| +// an account as a house account, whose checkout is free (plans 6b). |
| import ( |
| "context" |
| @@ -107,6 +108,43 @@ func runBillingWithSession(ctx context.Context, sess *apiSession, args []string, |
| } |
| fmt.Fprintln(out, colorFor(out).ok(fmt.Sprintf(cliBillingCopyClosed, *id))) |
| return nil |
| + case "house": |
| + account := fs.String("account", "", cliBillingCopyFlagAccount) |
| + on := fs.Bool("on", false, cliBillingCopyFlagOn) |
| + off := fs.Bool("off", false, cliBillingCopyFlagOff) |
| + note := fs.String("note", "", cliBillingCopyFlagHouseNote) |
| + if err := fs.Parse(rest); err != nil { |
| + return err |
| + } |
| + if fs.NArg() != 0 { |
| + return fmt.Errorf("unexpected argument %q", fs.Arg(0)) |
| + } |
| + if *on == *off { |
| + return errors.New(cliBillingCopyHouseOnOff) |
| + } |
| + if strings.TrimSpace(*account) == "" { |
| + return errors.New(cliBillingCopyHouseAccount) |
| + } |
| + if strings.TrimSpace(*note) == "" { |
| + return errors.New(cliBillingCopyNoteRequired) |
| + } |
| + if sess == nil { |
| + sess = requireBillingAPISession(*consoleFlag) |
| + printUsing(out, sess) |
| + } |
| + status, body, err := sess.postJSON(ctx, "/api/cli/billing/house", map[string]any{"account": *account, "on": *on, "note": *note}) |
| + if err != nil { |
| + return err |
| + } |
| + if status != http.StatusOK { |
| + return apiErrorFromBody(status, body) |
| + } |
| + line := cliBillingCopyHouseOff |
| + if *on { |
| + line = cliBillingCopyHouseOn |
| + } |
| + fmt.Fprintln(out, colorFor(out).ok(fmt.Sprintf(line, *account))) |
| + return nil |
| default: |
| return errors.New(cliBillingCopyUsage) |
| } |
diff --git a/billing_cli_copy.go b/billing_cli_copy.go| index da7294356..2fcbfade2 100644 |
| --- a/billing_cli_copy.go |
| +++ b/billing_cli_copy.go |
| @@ -4,7 +4,7 @@ package main |
| // 2026-10-25: docs/superpowers/specs/LUNA-COPY-RESULT-PLANS-6A.md. Apply |
| // later edits through a copy pass, not by hand. |
| const ( |
| - cliBillingCopyUsage = "usage: carlos billing problems [-all] | close -id N -note \"...\"" |
| + cliBillingCopyUsage = "usage: carlos billing problems [-all] | close -id N -note \"...\" | house -account A -on|-off -note \"...\"" |
| cliBillingCopyFlagAll = "include closed problems" |
| cliBillingCopyFlagID = "problem number from carlos billing problems" |
| cliBillingCopyFlagNote = "what you did in stripe. this note is kept with the problem" |
| @@ -12,5 +12,15 @@ const ( |
| cliBillingCopyIDRequired = "say which problem to close with -id" |
| cliBillingCopyNoProblems = "no open billing problems" |
| cliBillingCopyClosed = "problem %d closed" |
| - cliBillingCopyNeedsConsole = "billing works through a console, but this shell is in direct bucket mode\n(CARLOS_DEPLOYMENT_BUCKET/CARLOS_DEPLOYMENT_DIR is set). billing is kept by the console.\nunset the store env and run: carlos auth login --console <url>" |
| + // Plans 6b drafts (Task 14), for carlos billing house; the Luna pass |
| + // replaces them. |
| + cliBillingCopyHouseOn = "account %s is now the house: its checkout is free" |
| + cliBillingCopyHouseOff = "account %s pays like everyone else" |
| + cliBillingCopyHouseOnOff = "say -on or -off, one of them" |
| + cliBillingCopyHouseAccount = "say which account with -account" |
| + cliBillingCopyFlagAccount = "account to mark or clear" |
| + cliBillingCopyFlagOn = "make the account a house account" |
| + cliBillingCopyFlagOff = "clear the house mark" |
| + cliBillingCopyFlagHouseNote = "why. this note is kept in the console log" |
| + cliBillingCopyNeedsConsole = "billing works through a console, but this shell is in direct bucket mode\n(CARLOS_DEPLOYMENT_BUCKET/CARLOS_DEPLOYMENT_DIR is set). billing is kept by the console.\nunset the store env and run: carlos auth login --console <url>" |
| ) |
diff --git a/billing_cli_test.go b/billing_cli_test.go| index aa1733b25..e2a9df8b7 100644 |
| --- a/billing_cli_test.go |
| +++ b/billing_cli_test.go |
| @@ -4,6 +4,7 @@ import ( |
| "bytes" |
| "context" |
| "encoding/json" |
| + "fmt" |
| "io" |
| "net/http" |
| "net/http/httptest" |
| @@ -74,3 +75,59 @@ func TestBillingCLINoProblems(t *testing.T) { |
| t.Fatalf("output %q", out.String()) |
| } |
| } |
| + |
| +func TestBillingCLIHouse(t *testing.T) { |
| + var paths []string |
| + var bodies []map[string]any |
| + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + paths = append(paths, r.Method+" "+r.URL.RequestURI()) |
| + var b map[string]any |
| + _ = json.NewDecoder(r.Body).Decode(&b) |
| + bodies = append(bodies, b) |
| + io.WriteString(w, `{"account":"bab","house":`+map[bool]string{true: "true", false: "false"}[b["on"] == true]+`}`) |
| + })) |
| + defer ts.Close() |
| + s := newTestSession(ts.URL) |
| + |
| + var out bytes.Buffer |
| + if err := runBillingWithSession(context.Background(), s, []string{"house", "-account", "bab", "-on", "-note", "founders"}, &out); err != nil { |
| + t.Fatal(err) |
| + } |
| + if want := fmt.Sprintf(cliBillingCopyHouseOn, "bab"); !strings.Contains(out.String(), want) { |
| + t.Fatalf("on output %q lacks %q", out.String(), want) |
| + } |
| + out.Reset() |
| + if err := runBillingWithSession(context.Background(), s, []string{"house", "-account", "bab", "-off", "-note", "paying now"}, &out); err != nil { |
| + t.Fatal(err) |
| + } |
| + if want := fmt.Sprintf(cliBillingCopyHouseOff, "bab"); !strings.Contains(out.String(), want) { |
| + t.Fatalf("off output %q lacks %q", out.String(), want) |
| + } |
| + if strings.Join(paths, "|") != "POST /api/cli/billing/house|POST /api/cli/billing/house" { |
| + t.Fatalf("paths %v", paths) |
| + } |
| + if bodies[0]["on"] != true || bodies[0]["account"] != "bab" || bodies[0]["note"] != "founders" || bodies[1]["on"] != false { |
| + t.Fatalf("bodies %v", bodies) |
| + } |
| + |
| + // Neither flag, both flags, no account and no note are usage errors that |
| + // never reach the console. |
| + hits := len(paths) |
| + for _, c := range []struct { |
| + args []string |
| + want string |
| + }{ |
| + {[]string{"house", "-account", "bab", "-note", "x"}, cliBillingCopyHouseOnOff}, |
| + {[]string{"house", "-account", "bab", "-on", "-off", "-note", "x"}, cliBillingCopyHouseOnOff}, |
| + {[]string{"house", "-on", "-note", "x"}, cliBillingCopyHouseAccount}, |
| + {[]string{"house", "-account", "bab", "-on"}, cliBillingCopyNoteRequired}, |
| + } { |
| + err := runBillingWithSession(context.Background(), s, c.args, io.Discard) |
| + if err == nil || err.Error() != c.want { |
| + t.Errorf("%v: %v, want %q", c.args, err, c.want) |
| + } |
| + } |
| + if len(paths) != hits { |
| + t.Fatal("a refused command reached the console") |
| + } |
| +} |
diff --git a/internal/console/apibilling_copy.go b/internal/console/apibilling_copy.go| index 8b8bec397..d17ff6a28 100644 |
| --- a/internal/console/apibilling_copy.go |
| +++ b/internal/console/apibilling_copy.go |
| @@ -16,4 +16,6 @@ const ( |
| billingCopyOpExecutedUnseen = "the request log says it executed, but the subscription does not show it. put the subscription right in stripe first, then close" |
| // Plans 6b draft (Task 11), for the seats_unpaid close. |
| billingCopySeatsStillOver = "the account still has more members than paid seats. remove members first" |
| + // Plans 6b draft (Task 14), for the house door. |
| + billingCopyHouseNote = "say why with -note" |
| ) |
diff --git a/internal/console/apibillinghouse.go b/internal/console/apibillinghouse.go| new file mode 100644 |
| index 000000000..2512b932a |
| --- /dev/null |
| +++ b/internal/console/apibillinghouse.go |
| @@ -0,0 +1,59 @@ |
| +package console |
| + |
| +import ( |
| + "log" |
| + "net/http" |
| + "strings" |
| + "time" |
| +) |
| + |
| +// The house flag (plans 6b, Task 14): an operator marks an account as a |
| +// house account. Its checkout carries the configured house coupon (see |
| +// billingcheckout.go), so it pays nothing. The mark is the operator's call, |
| +// kept with who set it and why. |
| + |
| +// handleAPIBillingHouse is POST /api/cli/billing/house, body |
| +// {"account":"<sqid>","on":true,"note":"..."}. It goes through billingDoor |
| +// (signed in, operator, billing configured). An empty note is 400 and an |
| +// unknown account 404. It writes an audit line. Clearing an account that is |
| +// not marked is not an error. |
| +func (s *Server) handleAPIBillingHouse(w http.ResponseWriter, r *http.Request) { |
| + addr, ok := s.billingDoor(w, r) |
| + if !ok { |
| + return |
| + } |
| + var req struct { |
| + Account string `json:"account"` |
| + On bool `json:"on"` |
| + Note string `json:"note"` |
| + } |
| + if !readJSONStrictLimit(w, r, &req, 8<<10) { |
| + return |
| + } |
| + note := strings.TrimSpace(req.Note) |
| + if note == "" { |
| + jsonError(w, http.StatusBadRequest, billingCopyHouseNote) |
| + return |
| + } |
| + acct, err := s.store.AccountBySqid(req.Account) |
| + if err != nil { |
| + log.Printf("console: api billing house: account lookup: %v", err) |
| + jsonError(w, http.StatusInternalServerError, billingCopyFailed) |
| + return |
| + } |
| + if acct == nil { |
| + http.NotFound(w, r) |
| + return |
| + } |
| + if err := s.store.SetBillingHouse(acct.ID, req.On, addr, note, time.Now()); err != nil { |
| + log.Printf("console: api billing house %s: %v", acct.Sqid, err) |
| + jsonError(w, http.StatusInternalServerError, billingCopyFailed) |
| + return |
| + } |
| + state := "off" |
| + if req.On { |
| + state = "on" |
| + } |
| + log.Printf("console: billing house %s %s by %s: %s", acct.Sqid, state, addr, note) |
| + writeJSON(w, http.StatusOK, map[string]any{"account": acct.Sqid, "house": req.On}) |
| +} |
diff --git a/internal/console/apibillinghouse_test.go b/internal/console/apibillinghouse_test.go| new file mode 100644 |
| index 000000000..4f6ef02c3 |
| --- /dev/null |
| +++ b/internal/console/apibillinghouse_test.go |
| @@ -0,0 +1,84 @@ |
| +package console |
| + |
| +import ( |
| + "bytes" |
| + "log" |
| + "net/http" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +func billingHouseURL(fx *consoleFixture) string { return fx.ts.URL + "/api/cli/billing/house" } |
| + |
| +func TestBillingHouseDoor(t *testing.T) { |
| + bf := newBillingFixture(t) |
| + url := billingHouseURL(bf.consoleFixture) |
| + on := map[string]any{"account": bf.acct.Sqid, "on": true, "note": "the founders' own account"} |
| + |
| + // A member is not an operator: the stdlib 404, and nothing is marked. |
| + owner := "owner" + bf.acct.Sqid + "@example.com" |
| + member := cliToken(t, bf.consoleFixture, owner, "code-house-owner") |
| + if status, body := postRaw(t, url, on, member); status != http.StatusNotFound || body != "404 page not found\n" { |
| + t.Fatalf("member: %d %q, want the stdlib 404", status, body) |
| + } |
| + if house, err := bf.st.BillingHouse(bf.acct.ID); err != nil || house { |
| + t.Fatalf("a member marked the house: %v %v", house, err) |
| + } |
| + |
| + op := planOperatorToken(t, bf.consoleFixture, "ops@keymail.dev", "code-house-op") |
| + |
| + // An empty note is 400 (blank too), an unknown account 404; neither marks. |
| + for _, note := range []string{"", " "} { |
| + if status, out := postJSON(t, url, map[string]any{"account": bf.acct.Sqid, "on": true, "note": note}, op); status != http.StatusBadRequest || out["error"] != billingCopyHouseNote { |
| + t.Errorf("note %q: %d %v, want 400 %q", note, status, out, billingCopyHouseNote) |
| + } |
| + } |
| + if status, _ := postRaw(t, url, map[string]any{"account": "nope", "on": true, "note": "x"}, op); status != http.StatusNotFound { |
| + t.Errorf("unknown account: %d, want 404", status) |
| + } |
| + if house, _ := bf.st.BillingHouse(bf.acct.ID); house { |
| + t.Fatal("a refused request marked the house") |
| + } |
| + |
| + // On: marked, with an audit line naming the operator, account and note. |
| + var logs bytes.Buffer |
| + prev := log.Writer() |
| + log.SetOutput(&logs) |
| + status, out := postJSON(t, url, on, op) |
| + log.SetOutput(prev) |
| + if status != http.StatusOK || out["account"] != bf.acct.Sqid || out["house"] != true { |
| + t.Fatalf("on: %d %v", status, out) |
| + } |
| + if house, err := bf.st.BillingHouse(bf.acct.ID); err != nil || !house { |
| + t.Fatalf("BillingHouse = %v %v, want true", house, err) |
| + } |
| + for _, want := range []string{"billing house", bf.acct.Sqid, "ops@keymail.dev", "on", "the founders' own account"} { |
| + if !strings.Contains(logs.String(), want) { |
| + t.Errorf("audit line lacks %q:\n%s", want, logs.String()) |
| + } |
| + } |
| + |
| + // Off: cleared, audited, and clearing twice is not an error. |
| + for i := 0; i < 2; i++ { |
| + logs.Reset() |
| + log.SetOutput(&logs) |
| + status, out = postJSON(t, url, map[string]any{"account": bf.acct.Sqid, "on": false, "note": "back to paying"}, op) |
| + log.SetOutput(prev) |
| + if status != http.StatusOK || out["house"] != false { |
| + t.Fatalf("off #%d: %d %v", i, status, out) |
| + } |
| + if house, _ := bf.st.BillingHouse(bf.acct.ID); house { |
| + t.Fatalf("off #%d left the house marked", i) |
| + } |
| + if !strings.Contains(logs.String(), "back to paying") { |
| + t.Errorf("off audit line lacks the note:\n%s", logs.String()) |
| + } |
| + } |
| + |
| + // Billing off: 409, as the other billing doors answer. |
| + off := newConsoleFixture(t) |
| + opOff := planOperatorToken(t, off, "ops@keymail.dev", "code-house-off") |
| + if status, out := postJSON(t, billingHouseURL(off), on, opOff); status != http.StatusConflict || out["error"] != billingCopyOff { |
| + t.Fatalf("billing off: %d %v, want 409 %q", status, out, billingCopyOff) |
| + } |
| +} |
diff --git a/internal/console/authcli.go b/internal/console/authcli.go| index d2b3f84b2..9e602069e 100644 |
| --- a/internal/console/authcli.go |
| +++ b/internal/console/authcli.go |
| @@ -965,6 +965,8 @@ func (s *Server) registerCLIRoutes() { |
| s.cliRoute("POST /api/cli/accounts/{account}/plan", svcrole.Set{}, s.handleAPIAccountPlanSet) |
| s.cliRoute("GET /api/cli/billing/problems", svcrole.Set{}, s.handleAPIBillingProblems) |
| s.cliRoute("POST /api/cli/billing/problems/{id}/close", svcrole.Set{}, s.handleAPIBillingProblemClose) |
| + // Plans 6b (Task 14): an operator marks an account as a house account. |
| + s.cliRoute("POST /api/cli/billing/house", svcrole.Set{}, s.handleAPIBillingHouse) |
| s.cliRoute("POST /api/cli/accounts/{account}/store/decline", svcrole.Set{}, s.handleAPIStoreDecline) |
| s.cliRoute("GET /api/cli/store/approvals", svcrole.Set{}, s.handleAPIStoreApprovals) |
| // The scan family (internal/console/apiscan.go). `scan` itself is the |
diff --git a/internal/console/claim_invariant_fixtures_test.go b/internal/console/claim_invariant_fixtures_test.go| index 1cb3cb19f..dee979962 100644 |
| --- a/internal/console/claim_invariant_fixtures_test.go |
| +++ b/internal/console/claim_invariant_fixtures_test.go |
| @@ -1331,6 +1331,7 @@ var claimFixtures = map[string]claimFixture{ |
| "route POST /api/cli/accounts/{account}/store/apply": claimNonApp("applies for the account's storage entitlement"), |
| "route POST /api/cli/accounts/{account}/store/approve": claimNonApp("operator approves an account's storage entitlement"), |
| "route POST /api/cli/accounts/{account}/plan": claimNonApp("operator sets an account's plan, packs and sponsored limits; names no app"), |
| + "route POST /api/cli/billing/house": claimNonApp("operator marks or clears an account's house flag, the account whose checkout carries the house coupon, writing the flag row; names no app"), |
| "route POST /api/cli/billing/problems/{id}/close": claimNonApp("operator closes a billing problem with a note after a fresh provider read; names no app"), |
| "route POST /api/cli/accounts/{account}/store/decline": claimNonApp("operator declines an account's storage entitlement"), |
| "route POST /api/cli/fleets/{account}/new": claimNonApp("creates a customer fleet in the registry"), |
diff --git a/internal/console/claim_invariant_probes_nonapp_test.go b/internal/console/claim_invariant_probes_nonapp_test.go| index 92f0d4fd2..3e718046e 100644 |
| --- a/internal/console/claim_invariant_probes_nonapp_test.go |
| +++ b/internal/console/claim_invariant_probes_nonapp_test.go |
| @@ -808,6 +808,21 @@ var claimNonAppProbes = map[string]claimProbe{ |
| return claimBillingPlanIs(t, cx, "community") |
| }, |
| }, |
| + "route POST /api/cli/billing/house": { |
| + setup: claimBillingWorld, |
| + do: claimAPIReply(http.MethodPost, "/api/cli/billing/house", `{"account":"bab","on":true,"note":"census"}`), |
| + body: []string{`"house":true`}, |
| + effect: func(t *testing.T, cx *claimCase, r claimReply) string { |
| + acct, err := cx.srv.store.AccountBySqid("bab") |
| + if err != nil || acct == nil { |
| + t.Fatalf("bab: %v", err) |
| + } |
| + if house, err := cx.srv.store.BillingHouse(acct.ID); err != nil || !house { |
| + return "bab is not marked as a house account" |
| + } |
| + return "" |
| + }, |
| + }, |
| "route POST /api/cli/billing/problems/{id}/close": { |
| setup: claimBillingProblemWorld, |
| do: func(t *testing.T, cx *claimCase) claimReply { |
diff --git a/internal/console/svcroutes_test.go b/internal/console/svcroutes_test.go| index 35d161c60..6b623319a 100644 |
| --- a/internal/console/svcroutes_test.go |
| +++ b/internal/console/svcroutes_test.go |
| @@ -203,6 +203,9 @@ var closedDoors = []string{ |
| // closes them. |
| "GET /api/cli/billing/problems", |
| "POST /api/cli/billing/problems/{id}/close", |
| + // Marking an account as a house account makes its checkout free (plans |
| + // 6b, Task 14): a fleet operator's decision, never a machine credential's. |
| + "POST /api/cli/billing/house", |
| // Moving a route between channels decides what that route SERVES, which |
| // is release authority wearing a config-shaped hat: a credential the app |
| // itself holds must not be able to walk its own route onto a channel |
| @@ -1086,8 +1089,8 @@ func TestEveryClosedDoorRefusesAServiceCredential(t *testing.T) { |
| } |
| seen++ |
| } |
| - if seen != len(closedDoors) || seen != 62 { |
| - t.Errorf("walked %d closed doors, want all %d (and 62 today: instance-token, three door-key doors, the routes door, instances-manifest, the app update policy, sidecars-manifest, the route-channel move, three instance update settings doors, two db replica doors, the forwarded db read, six fleets, four services, two email credentials, email pause, the build probe, the swap policy, the live tenancy read, the layout read, the activation-cancel door, ten system doors, three deployment-wide antivirus, three platform geoip doors, two pointer digest doors, the channel resign, two release authorize doors, four store-entitlement doors, two account plan doors, and two billing problem doors)", |
| + if seen != len(closedDoors) || seen != 63 { |
| + t.Errorf("walked %d closed doors, want all %d (and 63 today: instance-token, three door-key doors, the routes door, instances-manifest, the app update policy, sidecars-manifest, the route-channel move, three instance update settings doors, two db replica doors, the forwarded db read, six fleets, four services, two email credentials, email pause, the build probe, the swap policy, the live tenancy read, the layout read, the activation-cancel door, ten system doors, three deployment-wide antivirus, three platform geoip doors, two pointer digest doors, the channel resign, two release authorize doors, four store-entitlement doors, two account plan doors, two billing problem doors, and the billing house door)", |
| seen, len(closedDoors)) |
| } |
| } |
diff --git a/internal/console/testdata/claim-routes.txt b/internal/console/testdata/claim-routes.txt| index f6134edce..77c4b60d7 100644 |
| --- a/internal/console/testdata/claim-routes.txt |
| +++ b/internal/console/testdata/claim-routes.txt |
| @@ -179,6 +179,7 @@ route POST /a/{sqid}/trash/delete |
| route POST /a/{sqid}/trash/restore |
| route POST /accounts/new |
| route POST /api/cli/accounts/{account}/plan |
| +route POST /api/cli/billing/house |
| route POST /api/cli/billing/problems/{id}/close |
| route POST /api/cli/accounts/{account}/store/apply |
| route POST /api/cli/accounts/{account}/store/approve |
diff --git a/main.go b/main.go| index a2a118d8e..31998ddb6 100644 |
| --- a/main.go |
| +++ b/main.go |
| @@ -309,7 +309,7 @@ Your apps — ship and run software on a CARLOS deployment |
| Your deployment — stand up and administer boxes, routes and accounts |
| bootstrap prepare a host: units, dirs, user |
| accounts the tenancy primitive: create | list | migrate |
| - billing billing problems the console left open: problems | close |
| + billing billing problems and house accounts: problems | close | house |
| fleets account-owned groups of remote boxes that dial this |
| console: create | add-box | rotate-token | tag-box | |
| detach-box | ls — box tokens print exactly once, at mint |