carlos / platform Public

system update: refuse an unreadable or job-less console answer

carlos system update discarded three decode errors. The status read
fed the preview, so an unreadable answer said "0 box(es) will roll".
The promote's answer was read for its job id, and a missing or
unreadable one was passed on as "", which watchSystemJob then polled
as /api/cli/system/jobs/ for 15 minutes. Its own poll loop also spun
for 15 minutes on a progress report it could not read.

Each now stops with a sentence a person can act on: the status read
refuses before promoting anything; a job-less answer says the promote
was accepted and names carlos system status --channel to follow it;
watchSystemJob refuses an empty id and stops on an unreadable report.

carlos system rollback had the same discarded decode, printing a
blank version. The rollback has already happened at that point, so it
still exits 0 (a failure would invite a second rollback) and says
where to read the version instead.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 24ff6c912f24c96ff9f72141f919f59f386861e9 parent 852fe7a
2 files changed, +133 −8
  • system.go +49 −8
  • system_test.go +84 −0

CI cancelled — superseded by a newer push — run details

diff --git a/system.go b/system.go
index 3dc87b9c9..4702c331f 100644
--- a/system.go
+++ b/system.go
@@ -385,7 +385,11 @@ func runSystemUpdate(args []string) {
log.Fatalf("carlos system update: %s", apiErrorFromBody(st, body))
}
var cur systemStatusResponse
- _ = json.Unmarshal(body, &cur)
+ if err := json.Unmarshal(body, &cur); err != nil {
+ // Read on, and the preview below would say "0 box(es) will roll"
+ // about a promote that may reach every box.
+ log.Fatalf("carlos system update: could not read the console's system status, so there is no telling which boxes this would reach; nothing was promoted (%v)", err)
+ }
var reach []string
for _, b := range cur.Boxes {
if b.Channel == *channel {
@@ -455,11 +459,11 @@ func runSystemUpdate(args []string) {
}
log.Fatalf("carlos system update: %s", apiErrorFromBody(st, body))
}
- var started struct {
- Job string `json:"job"`
+ job, err := systemUpdateJob(body, *channel)
+ if err != nil {
+ log.Fatal(err)
}
- _ = json.Unmarshal(body, &started)
- if err := sess.watchSystemJob(ctx, started.Job, stdout); err != nil {
+ if err := sess.watchSystemJob(ctx, job, stdout); err != nil {
log.Fatal(err)
}
fmt.Fprintf(stdout, "%s boxes on %s roll themselves; watch with: carlos system status --channel %s\n",
@@ -486,11 +490,31 @@ func systemLadderWarning(pointers map[string]*systemPointerRow, channel, version
return fmt.Sprintf("warning: %s has not been on canary; promoting straight to stable", version)
}
+// systemUpdateJob reads the job id out of the console's 200 answer to a
+// promote. The promote is already under way by then, so neither refusal
+// says it failed: each says the job cannot be watched from here and where
+// to look instead. An empty id used to be polled as /jobs/ for 15 minutes.
+func systemUpdateJob(body []byte, channel string) (string, error) {
+ var started struct {
+ Job string `json:"job"`
+ }
+ if err := json.Unmarshal(body, &started); err != nil {
+ return "", fmt.Errorf("carlos system update: the console accepted the promote but its answer could not be read (%v); follow it with: carlos system status --channel %s", err, channel)
+ }
+ if started.Job == "" {
+ return "", fmt.Errorf("carlos system update: the console accepted the promote but named no job to watch; follow it with: carlos system status --channel %s", channel)
+ }
+ return started.Job, nil
+}
+
// watchSystemJob polls the job and prints each step once, as deploy's
// watchVersion does for a route. The console runs the promote off the
// request precisely so a dropped connection cannot abandon it, so this is a
// view of work already under way, not the work itself.
func (s *apiSession) watchSystemJob(ctx context.Context, id string, w io.Writer) error {
+ if id == "" {
+ return errors.New("carlos system update: no job to watch")
+ }
printed := 0
deadline := time.Now().Add(15 * time.Minute)
for {
@@ -506,7 +530,11 @@ func (s *apiSession) watchSystemJob(ctx context.Context, id string, w io.Writer)
Done bool `json:"done"`
Error string `json:"error"`
}
- _ = json.Unmarshal(body, &j)
+ if err := json.Unmarshal(body, &j); err != nil {
+ // An unreadable answer never says done, so polling on would
+ // spin for the whole 15 minutes on a reply that will not change.
+ return fmt.Errorf("carlos system update: job %s: could not read the console's progress report (%v); the promote may still be running", id, err)
+ }
for ; printed < len(j.Steps); printed++ {
fmt.Fprintf(w, " %s\n", j.Steps[printed])
}
@@ -688,11 +716,24 @@ func runSystemRollback(args []string) {
if st != http.StatusOK {
log.Fatalf("carlos system rollback: %s", apiErrorFromBody(st, body))
}
+ printSystemRolledBack(stdout, *channel, body)
+}
+
+// printSystemRolledBack reports a rollback the console has already done
+// (it answered 200). An answer that cannot be read or names no version
+// still means the rollback happened, so this never fails the command: a
+// non-zero exit would invite a second rollback, one release further back.
+// It says the version is unknown and where to read it instead.
+func printSystemRolledBack(w io.Writer, channel string, body []byte) {
var out struct {
Version string `json:"version"`
}
- _ = json.Unmarshal(body, &out)
- fmt.Fprintf(stdout, "%s %s → %s\n", colorFor(stdout).ok("rolled back"), *channel, out.Version)
+ if err := json.Unmarshal(body, &out); err != nil || out.Version == "" {
+ fmt.Fprintf(w, "%s %s; the console did not say which release it is on now. check with: carlos system status --channel %s\n",
+ colorFor(w).ok("rolled back"), channel, channel)
+ return
+ }
+ fmt.Fprintf(w, "%s %s → %s\n", colorFor(w).ok("rolled back"), channel, out.Version)
}
// systemPDFCommand is `carlos system pdf status`. Bare is the usage and
diff --git a/system_test.go b/system_test.go
index bafb4f22e..cda1616c5 100644
--- a/system_test.go
+++ b/system_test.go
@@ -11,6 +11,7 @@ import (
"os/exec"
"path/filepath"
"strings"
+ "sync/atomic"
"testing"
"time"
)
@@ -432,3 +433,86 @@ func TestSystemGroupMaintenanceVerb(t *testing.T) {
}
}
}
+
+// TestSystemUpdateJobRefusesNoJob: the console's 200 answer to a promote
+// must name a job. An unreadable answer or one with no id is refused with
+// where to follow the promote instead; before, both came out as "" and the
+// CLI polled /api/cli/system/jobs/ for 15 minutes.
+func TestSystemUpdateJobRefusesNoJob(t *testing.T) {
+ for _, body := range []string{`{}`, `{"job":""}`, `not json`, ``} {
+ job, err := systemUpdateJob([]byte(body), "canary")
+ if err == nil {
+ t.Fatalf("answer %q gave job %q, want a refusal", body, job)
+ }
+ if !strings.Contains(err.Error(), "carlos system status --channel canary") {
+ t.Fatalf("answer %q: %v, want it to say where to follow the promote", body, err)
+ }
+ }
+ if job, err := systemUpdateJob([]byte(`{"job":"j1"}`), "canary"); err != nil || job != "j1" {
+ t.Fatalf("job = %q, %v; want j1", job, err)
+ }
+}
+
+// TestWatchSystemJobStopsOnNoJobOrUnreadableReport: watchSystemJob never polls
+// an empty id, and a progress report it cannot read ends the watch at once
+// instead of polling an answer that will never say done for 15 minutes.
+func TestWatchSystemJobStopsOnNoJobOrUnreadableReport(t *testing.T) {
+ var emptyPolls, jobPolls atomic.Int32
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch r.URL.Path {
+ case "/api/cli/system/jobs/":
+ emptyPolls.Add(1)
+ w.Write([]byte(`{}`))
+ case "/api/cli/system/jobs/j1":
+ jobPolls.Add(1)
+ w.Write([]byte(`<html>not a job</html>`))
+ default:
+ http.NotFound(w, r)
+ }
+ }))
+ t.Cleanup(ts.Close)
+ sess := &apiSession{c: newConsoleClient(ts.URL, "tok"), console: ts.URL}
+
+ watch := func(id string) error {
+ done := make(chan error, 1)
+ go func() { done <- sess.watchSystemJob(context.Background(), id, io.Discard) }()
+ select {
+ case err := <-done:
+ return err
+ case <-time.After(5 * time.Second):
+ t.Fatalf("watchSystemJob(%q) still polling after 5s", id)
+ return nil
+ }
+ }
+ if err := watch(""); err == nil {
+ t.Fatal("watched a job with no id")
+ }
+ if n := emptyPolls.Load(); n != 0 {
+ t.Fatalf("polled /api/cli/system/jobs/ %d times for an empty id", n)
+ }
+ err := watch("j1")
+ if err == nil || !strings.Contains(err.Error(), "could not read") {
+ t.Fatalf("unreadable report: %v, want a could-not-read error", err)
+ }
+ if n := jobPolls.Load(); n != 1 {
+ t.Fatalf("polled j1 %d times, want 1", n)
+ }
+}
+
+// TestSystemRollbackReportsAnUnreadableAnswer: a rollback the console has
+// done is reported as done even when its answer names no version, and the
+// line says where to read the version rather than ending in a blank arrow.
+func TestSystemRollbackReportsAnUnreadableAnswer(t *testing.T) {
+ for _, body := range []string{`{}`, `not json`} {
+ var out bytes.Buffer
+ printSystemRolledBack(&out, "canary", []byte(body))
+ if !strings.Contains(out.String(), "rolled back canary;") || !strings.Contains(out.String(), "carlos system status --channel canary") {
+ t.Fatalf("answer %q printed %q", body, out.String())
+ }
+ }
+ var out bytes.Buffer
+ printSystemRolledBack(&out, "canary", []byte(`{"version":"v0.31.0"}`))
+ if !strings.Contains(out.String(), "canary → v0.31.0") {
+ t.Fatalf("printed %q", out.String())
+ }
+}