carlos / platform Public

cli: SIZE in status --layout; committed figures after a size change

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev e768cb45e0bc57aafe29f7f27ae83f65e3c6f0f1 parent 0e4962d
6 files changed, +229 −14
  • apps_policy.go +28 −0
  • apps_policy_test.go +54 −0
  • internal/console/apistatuslayout.go +6 −1
  • internal/console/apistatuslayout_test.go +53 −0
  • status_instances.go +29 −5
  • status_instances_test.go +59 −8
diff --git a/apps_policy.go b/apps_policy.go
index 3fb68eb3e..f24eb4d96 100644
--- a/apps_policy.go
+++ b/apps_policy.go
@@ -18,6 +18,7 @@ import (
"errors"
"flag"
"fmt"
+ "io"
"log"
"net/http"
"slices"
@@ -89,6 +90,31 @@ type apiAppPolicy struct {
// EgressConflicts is the allowed addresses the console's fleet-address
// report found on a fleet box (#328); absent from an older console.
EgressConflicts []string `json:"egress_conflicts"`
+ // Committed is each checked box's memory figures after a size change
+ // (#329); absent when nothing changed a size, or from an older console.
+ Committed []sizeCommittedBox `json:"committed"`
+}
+
+// sizeCommittedBox is one box in the reply's committed list (the console's
+// sizeCommittedBox, internal/console/policysize.go). Declared here rather
+// than imported: package main does not import console types.
+type sizeCommittedBox struct {
+ Box string `json:"box"`
+ CommittedMiB int64 `json:"committed_mib"`
+ TotalMiB int64 `json:"total_mib"`
+ FreeMiB int64 `json:"free_mib"`
+}
+
+// sizeCommittedLine is plan 3 Task 14's string 5, a draft until the copy
+// review approves it.
+const sizeCommittedLine = "%s: %d MiB committed to awake instances, of %d MiB. %d MiB free."
+
+// printSizeCommitted prints one line per box the console checked a size
+// change against, in the order the reply gives them.
+func printSizeCommitted(w io.Writer, boxes []sizeCommittedBox) {
+ for _, b := range boxes {
+ fmt.Fprintf(w, sizeCommittedLine+"\n", b.Box, b.CommittedMiB, b.TotalMiB, b.FreeMiB)
+ }
}
// apiPolicyBody is the part of the policy object (apppolicy.Policy) the CLI
@@ -253,6 +279,7 @@ func appsSetPolicyRun(ctx context.Context, sess *apiSession, acct apiAcct, app s
} else {
fmt.Fprintf(stdout, appsSetSizeDone+"\n", app, got.Policy.DefaultSize)
}
+ printSizeCommitted(stdout, got.Committed)
}
return nil
}
@@ -270,6 +297,7 @@ func instancesSetSizeRun(ctx context.Context, sess *apiSession, acct apiAcct, ap
} else {
fmt.Fprintf(stdout, instancesSetSizeDefault+"\n", host)
}
+ printSizeCommitted(stdout, got.Committed)
return nil
}
diff --git a/apps_policy_test.go b/apps_policy_test.go
index bffd18b06..cd705cb48 100644
--- a/apps_policy_test.go
+++ b/apps_policy_test.go
@@ -1,6 +1,7 @@
package main
import (
+ "bytes"
"context"
"flag"
"fmt"
@@ -440,3 +441,56 @@ func TestAppPolicyResolvesAnAccountNameToItsSqid(t *testing.T) {
t.Fatalf("foreign sqid: requests = %v, want %v", c.calls, want)
}
}
+
+// TestPrintSizeCommitted: one line per box the console checked, in the
+// order it sent them, with the box's committed, total and free MiB.
+func TestPrintSizeCommitted(t *testing.T) {
+ var buf bytes.Buffer
+ printSizeCommitted(&buf, []sizeCommittedBox{
+ {Box: "annie", CommittedMiB: 2304, TotalMiB: 3884, FreeMiB: 1200},
+ {Box: "bx-1", CommittedMiB: 1024, TotalMiB: 16384, FreeMiB: 9000},
+ })
+ want := "annie: 2304 MiB committed to awake instances, of 3884 MiB. 1200 MiB free.\n" +
+ "bx-1: 1024 MiB committed to awake instances, of 16384 MiB. 9000 MiB free.\n"
+ if buf.String() != want {
+ t.Fatalf("got %q, want %q", buf.String(), want)
+ }
+ buf.Reset()
+ printSizeCommitted(&buf, nil)
+ if buf.Len() != 0 {
+ t.Fatalf("no boxes printed %q", buf.String())
+ }
+}
+
+// After a size change, both verbs print the reply's committed lines under
+// the size line; an older console's reply, with no committed key, prints
+// none.
+func TestSizeChangePrintsCommitted(t *testing.T) {
+ out := captureStdout(t)
+ committed := `"committed":[{"box":"annie","committed_mib":2304,"total_mib":3884,"free_mib":1200}]`
+ line := "annie: 2304 MiB committed to awake instances, of 3884 MiB. 1200 MiB free.\n"
+
+ c := &policyConsole{getBody: `{"policy":{"v":1},"etag":""}`,
+ putBody: `{"policy":{"v":1,"default_size":"large"},"etag":"e1",` + committed + `}`}
+ change, err := policyChangeFor(t, "--size", "large")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := appsSetRun(context.Background(), c.session(t), apiAcct{Seg: "bab"}, "shop", nil, nil, change); err != nil {
+ t.Fatal(err)
+ }
+ if want := fmt.Sprintf(appsSetSizeDone+"\n", "shop", "large") + line; out.String() != want {
+ t.Fatalf("apps set: stdout = %q, want %q", out.String(), want)
+ }
+
+ out.Reset()
+ c = &policyConsole{getBody: `{"policy":{"v":1},"etag":"e1"}`,
+ putBody: `{"policy":{"v":1,"sizes":{"shop-a.example.com":{"size":"large","created_at":"2026-10-21T00:00:00Z"}}},"etag":"e2",` + committed + `}`}
+ large := "large"
+ if err := instancesSetRun(context.Background(), c.session(t), apiAcct{Seg: "bab"}, "shop", "shop-a.example.com", nil, nil, &large); err != nil {
+ t.Fatal(err)
+ }
+ if want := fmt.Sprintf(instancesSetSizeDone+"\n", "shop-a.example.com", "large") + line; out.String() != want {
+ t.Fatalf("instances set: stdout = %q, want %q", out.String(), want)
+ }
+}
diff --git a/internal/console/apistatuslayout.go b/internal/console/apistatuslayout.go
index 89f06fe92..bd48776c8 100644
--- a/internal/console/apistatuslayout.go
+++ b/internal/console/apistatuslayout.go
@@ -33,6 +33,11 @@ type apiStatusLayoutRow struct {
// (status.BoxMetrics.Egress, #328), nil when the box reports none.
// Additive; an old CLI ignores it.
Egress *apiEgress `json:"egress,omitempty"`
+ // Size is the host's size cell as its box last sent it
+ // (status.RowLayout.Size, #329): a size name, "in>want", or a word
+ // (swap, ?, -). Empty from a box that predates sizes. Additive; an old
+ // CLI ignores it.
+ Size string `json:"size,omitempty"`
}
// apiEgress is one host's egress allow as root read it back from its slice
@@ -144,7 +149,7 @@ func (s *Server) handleAPIStatusLayout(w http.ResponseWriter, r *http.Request) {
for host, rl := range b.Metrics.Layout.Rows {
eg, ok := b.Metrics.Egress[host]
rows = append(rows, apiStatusLayoutRow{Host: host, Layout: rl.Layout, Reason: rl.Reason, Note: rl.Note,
- Egress: egressView(eg, ok, conflicts)})
+ Egress: egressView(eg, ok, conflicts), Size: rl.Size})
}
sort.Slice(rows, func(i, j int) bool { return rows[i].Host < rows[j].Host })
out = append(out, apiStatusLayoutBox{
diff --git a/internal/console/apistatuslayout_test.go b/internal/console/apistatuslayout_test.go
index 9a65ab6c3..842997c4e 100644
--- a/internal/console/apistatuslayout_test.go
+++ b/internal/console/apistatuslayout_test.go
@@ -276,3 +276,56 @@ func TestAPIStatusLayoutCarriesEgressAndConflicts(t *testing.T) {
}
}
}
+
+// TestStatusLayoutDoorCarriesSize (#329): each row carries its size cell as
+// the box's layout report sent it; a row with no size (an older box, or no
+// size source armed) has no size key.
+func TestStatusLayoutDoorCarriesSize(t *testing.T) {
+ t.Parallel()
+ fx := newConsoleFixture(t)
+ token := cliToken(t, fx, "paul@keymail.dev", "code-apistatuslayoutsize")
+ if err := fx.st.SetOperator("paul@keymail.dev", true); err != nil {
+ t.Fatal(err)
+ }
+ seedLayoutBox(t, fx.rel, "box-a", time.Now(), &status.LayoutReport{Mode: "nested", Rows: map[string]status.RowLayout{
+ "shop-a.example.com": {Layout: "nested", Size: "xlarge"},
+ "shop-b.example.com": {Layout: "nested", Size: "large>small"},
+ "shop-c.example.com": {Layout: "nested"},
+ }})
+
+ code, body := getBearer(t, fx.ts.URL+"/api/cli/status/layout", token)
+ if code != http.StatusOK {
+ t.Fatalf("GET /api/cli/status/layout = %d %q", code, body)
+ }
+ var out struct {
+ Boxes []struct {
+ Rows []map[string]json.RawMessage `json:"rows"`
+ } `json:"boxes"`
+ }
+ if err := json.Unmarshal(body, &out); err != nil {
+ t.Fatalf("unreadable: %v: %s", err, body)
+ }
+ if len(out.Boxes) != 1 || len(out.Boxes[0].Rows) != 3 {
+ t.Fatalf("boxes = %s", body)
+ }
+ // "" is no size key at all, not an empty one.
+ want := map[string]string{
+ "shop-a.example.com": "xlarge",
+ "shop-b.example.com": "large>small",
+ "shop-c.example.com": "",
+ }
+ for _, r := range out.Boxes[0].Rows {
+ var host, size string
+ if err := json.Unmarshal(r["host"], &host); err != nil {
+ t.Fatal(err)
+ }
+ if raw, ok := r["size"]; ok {
+ if err := json.Unmarshal(raw, &size); err != nil || size == "" {
+ t.Fatalf("%s size = %s (%v), want a non-empty string or no key", host, raw, err)
+ }
+ }
+ if size != want[host] {
+ t.Errorf("%s size = %q, want %q", host, size, want[host])
+ }
+ }
+}
diff --git a/status_instances.go b/status_instances.go
index bc0aa0879..c186788f2 100644
--- a/status_instances.go
+++ b/status_instances.go
@@ -147,8 +147,19 @@ type statusLayoutRow struct {
// Egress is the host's egress read-back (#328); nil from an older
// console or when the box reports none.
Egress *apiEgress `json:"egress"`
+ // Size is the host's size cell (#329): a size name, "in>want" while a
+ // new size waits for a cold start, or swap, ? or -. Empty from an older
+ // console or box, which prints "-".
+ Size string `json:"size"`
}
+// Copy for `carlos status --layout` (plan 3 Task 14, drafts until the copy
+// review approves them).
+const (
+ statusLayoutHeading = "BOX HOST LAYOUT SIZE EGRESS REASON"
+ statusLayoutSizePending = "A size after > takes effect when the instance next starts cold. Restart the instance to apply it now."
+)
+
// statusLayoutBox is one box's layout report.
type statusLayoutBox struct {
Box string `json:"box"`
@@ -213,7 +224,8 @@ func statusLayout(w io.Writer, consoleFlag string) error {
// column widths would drift the heading away from what was reviewed the
// moment any cell is wider than its header word.
func printStatusLayout(w io.Writer, boxes []statusLayoutBox) {
- type line struct{ box, host, layout, egress, reason string }
+ type line struct{ box, host, layout, size, egress, reason string }
+ pending := false
var lines []line
for _, b := range boxes {
name := b.Box
@@ -239,7 +251,14 @@ func printStatusLayout(w io.Writer, boxes []statusLayoutBox) {
}
reason += more
}
- lines = append(lines, line{name, r.Host, r.Layout, egressCell(r.Egress), reason})
+ size := r.Size
+ if size == "" {
+ size = "-"
+ }
+ if strings.Contains(size, ">") {
+ pending = true
+ }
+ lines = append(lines, line{name, r.Host, r.Layout, size, egressCell(r.Egress), reason})
}
}
if len(lines) == 0 {
@@ -252,10 +271,15 @@ func printStatusLayout(w io.Writer, boxes []statusLayoutBox) {
}
return lines[i].host < lines[j].host
})
- // Plan 3's SIZE goes right before EGRESS.
- fmt.Fprintln(w, "BOX HOST LAYOUT EGRESS REASON")
+ fmt.Fprintln(w, statusLayoutHeading)
for _, l := range lines {
- fmt.Fprintf(w, "%s %s %s %s %s\n", l.box, l.host, l.layout, l.egress, dashIfEmpty(l.reason))
+ fmt.Fprintf(w, "%s %s %s %s %s %s\n", l.box, l.host, l.layout, l.size, l.egress, dashIfEmpty(l.reason))
+ }
+ // A size cell "in>want" is a new size waiting for a cold start (a
+ // populated slice is not shrunk live, and under host claims no size
+ // changes live). Said once, whatever the number of such rows.
+ if pending {
+ fmt.Fprintf(w, "\n%s\n", statusLayoutSizePending)
}
// A box with flat, foreign or external rows beside nested ones, or
// under a nested intent, is the loud case: each of those still shares
diff --git a/status_instances_test.go b/status_instances_test.go
index d66969b4e..a26a46266 100644
--- a/status_instances_test.go
+++ b/status_instances_test.go
@@ -65,7 +65,7 @@ func TestPrintStatusLayoutRendersSortedAndMarksStale(t *testing.T) {
printStatusLayout(&buf, boxes)
out := buf.String()
- if !strings.Contains(out, "BOX HOST LAYOUT EGRESS REASON") {
+ if !strings.Contains(out, "BOX HOST LAYOUT SIZE EGRESS REASON") {
t.Fatalf("missing the approved heading:\n%s", out)
}
if !strings.Contains(out, "alpha (stale)") {
@@ -155,8 +155,8 @@ func TestStatusLayoutListDecodesBoxes(t *testing.T) {
}
}
-// TestStatusLayoutShowsEgress (#328): the EGRESS column sits between LAYOUT
-// and REASON (plan 3 puts SIZE right before it), shows what each slice
+// TestStatusLayoutShowsEgress (#328): the EGRESS column sits between SIZE
+// and REASON, shows what each slice
// holds with a fleet box's address marked, and the REASON cell carries the
// refusal or detail after the layout's own reason. An old console's row,
// which has no egress key, reads "-".
@@ -175,11 +175,11 @@ func TestStatusLayoutShowsEgress(t *testing.T) {
printStatusLayout(&buf, boxes)
out := buf.String()
for _, want := range []string{
- "BOX HOST LAYOUT EGRESS REASON\n",
- "ie-1 shop-a.example.com nested !10.0.5.20,fd00:5::20 " + dashIfEmpty("") + "\n",
- "ie-1 shop-b.example.com foreign 10.0.5.20 !refused db and socket are neither default; egress: 10.0.5.1: an address of this box\n",
- "ie-1 shop-c.example.com nested - !box not wholly nested egress: flat row shop-d.example.com\n",
- "ie-1 shop-d.example.com flat - " + dashIfEmpty("") + "\n",
+ "BOX HOST LAYOUT SIZE EGRESS REASON\n",
+ "ie-1 shop-a.example.com nested - !10.0.5.20,fd00:5::20 " + dashIfEmpty("") + "\n",
+ "ie-1 shop-b.example.com foreign - 10.0.5.20 !refused db and socket are neither default; egress: 10.0.5.1: an address of this box\n",
+ "ie-1 shop-c.example.com nested - - !box not wholly nested egress: flat row shop-d.example.com\n",
+ "ie-1 shop-d.example.com flat - - " + dashIfEmpty("") + "\n",
} {
if !strings.Contains(out, want) {
t.Errorf("missing %q in:\n%s", want, out)
@@ -234,3 +234,54 @@ func TestPrintStatusLayoutSaysNestedByDefaultAndWhyNot(t *testing.T) {
t.Errorf("a stale box's reason was repeated:\n%s", out)
}
}
+
+// TestStatusLayoutShowsSize (#329): the SIZE column sits between LAYOUT and
+// EGRESS and shows each row's size cell as its box sent it: a size, an
+// in-force size and the pending one joined by ">", or a word. A row from an
+// older box, which has no size key, reads "-". One footnote explains ">",
+// and only when some cell holds one.
+func TestStatusLayoutShowsSize(t *testing.T) {
+ var boxes []statusLayoutBox
+ body := `[{"box":"ie-1","mode":"nested","rows":[
+ {"host":"shop-a.example.com","layout":"nested","size":"large"},
+ {"host":"shop-b.example.com","layout":"nested","size":"large>small"},
+ {"host":"shop-c.example.com","layout":"nested","size":"swap"},
+ {"host":"shop-d.example.com","layout":"flat"}]},
+ {"box":"ie-2","mode":"nested","rows":[
+ {"host":"shop-e.example.com","layout":"nested","size":"standard>xlarge"}]}]`
+ if err := json.Unmarshal([]byte(body), &boxes); err != nil {
+ t.Fatal(err)
+ }
+ var buf bytes.Buffer
+ printStatusLayout(&buf, boxes)
+ out := buf.String()
+ if !strings.HasPrefix(out, "BOX HOST LAYOUT SIZE EGRESS REASON\n") {
+ t.Fatalf("heading is not the draft, byte for byte:\n%s", out)
+ }
+ for _, want := range []string{
+ "ie-1 shop-a.example.com nested large - ",
+ "ie-1 shop-b.example.com nested large>small - ",
+ "ie-1 shop-c.example.com nested swap - ",
+ "ie-1 shop-d.example.com flat - - ",
+ "ie-2 shop-e.example.com nested standard>xlarge - ",
+ } {
+ if !strings.Contains(out, want) {
+ t.Errorf("missing %q in:\n%s", want, out)
+ }
+ }
+ footnote := "A size after > takes effect when the instance next starts cold. Restart the instance to apply it now.\n"
+ if n := strings.Count(out, footnote); n != 1 {
+ t.Errorf("the > footnote printed %d times, want once:\n%s", n, out)
+ }
+
+ // No ">" anywhere: no footnote.
+ buf.Reset()
+ printStatusLayout(&buf, []statusLayoutBox{{Box: "ie-1", Mode: "nested", Rows: []statusLayoutRow{
+ {Host: "shop-a.example.com", Layout: "nested", Size: "large"},
+ {Host: "shop-b.example.com", Layout: "nested", Size: "?"},
+ {Host: "shop-c.example.com", Layout: "flat"},
+ }}})
+ if strings.Contains(buf.String(), "takes effect") {
+ t.Errorf("a footnote with no > cell:\n%s", buf.String())
+ }
+}