Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(daemon): print daemon status pid without exponent notation (#11595)
Paul Philippov04b7afc30034
diff --git a/server/cmd/multica/cmd_daemon.go b/server/cmd/multica/cmd_daemon.goindex ce96e7f8fe..654b4e6113 100644--- a/server/cmd/multica/cmd_daemon.go+++ b/server/cmd/multica/cmd_daemon.go@@ -1574,7 +1574,7 @@ func runDaemonStatus(cmd *cobra.Command, _ []string) error {case "running":printDaemonStatusReport(os.Stdout, label, health)case "starting":- fmt.Fprintf(os.Stdout, "%s: starting (pid %v)\n", label, health["pid"])+ fmt.Fprintf(os.Stdout, "%s: starting (pid %s)\n", label, formatHealthPID(health["pid"]))default:fmt.Fprintf(os.Stdout, "%s: stopped\n", label)}@@ -1619,13 +1619,39 @@ func describeDaemonManager(launchedBy string) string {return launchedBy}+// formatHealthPID renders the pid from a decoded daemon health response. The+// map comes from encoding/json, so an integer pid arrives as a float64, and+// fmt's %v switches to exponent notation once it needs seven digits: a pid of+// 1531839 prints as "1.531839e+06". Converting to int first keeps the pid+// readable, which matters because a daemon that has been up a while is exactly+// the one a reader is trying to identify. Anything that is not a number is+// passed through unchanged so an older or newer daemon still shows something.+func formatHealthPID(v any) string {+ switch pid := v.(type) {+ case float64:+ return strconv.FormatInt(int64(pid), 10)+ case float32:+ return strconv.FormatInt(int64(pid), 10)+ case int:+ return strconv.Itoa(pid)+ case int64:+ return strconv.FormatInt(pid, 10)+ case string:+ return pid+ case nil:+ return "?"+ default:+ return fmt.Sprint(pid)+ }+}+// printDaemonStatusReport renders a key/value summary of the daemon health// response. The value column is aligned to the widest label so the dynamic// "Daemon [profile]" row stays in step with the static rows below it.func printDaemonStatusReport(w io.Writer, label string, health map[string]any) {type row struct{ key, value string }rows := []row{- {label, fmt.Sprintf("running (pid %v, uptime %v)", health["pid"], health["uptime"])},+ {label, fmt.Sprintf("running (pid %s, uptime %v)", formatHealthPID(health["pid"]), health["uptime"])},}if version, ok := health["cli_version"].(string); ok && version != "" {rows = append(rows, row{"Version", version})diff --git a/server/cmd/multica/cmd_daemon_identity_test.go b/server/cmd/multica/cmd_daemon_identity_test.goindex df04786454..4b4709e6a8 100644--- a/server/cmd/multica/cmd_daemon_identity_test.go+++ b/server/cmd/multica/cmd_daemon_identity_test.go@@ -336,6 +336,23 @@ func TestDaemonStatusShowsWhoManagesTheDaemon(t *testing.T) {})}+func TestDaemonStatusRendersStartingPIDPlainly(t *testing.T) {+ clearDaemonTaskEnv(t)+ const profile = "starting-pid-test"+ mkProfiles(t, profile)+ serveHealthAsPID(t, profile, map[string]any{"status": "starting", "profile": profile}, 1531839)++ out, err := captureStdout(t, func() error {+ return runDaemonStatus(daemonStatusCmdFor(t, profile, ""), nil)+ })+ if err != nil {+ t.Fatalf("runDaemonStatus: %v", err)+ }+ if want := "Daemon [starting-pid-test]: starting (pid 1531839)\n"; out != want {+ t.Fatalf("daemon status output = %q, want %q", out, want)+ }+}+// Inside a daemon-managed task the health port is injected by the host daemon// rather than derived from a profile hash, so there is no collision to detect.// The task's own profile is necessarily empty (--profile is rejected there),diff --git a/server/cmd/multica/cmd_daemon_test.go b/server/cmd/multica/cmd_daemon_test.goindex faec92313e..c874bb390c 100644--- a/server/cmd/multica/cmd_daemon_test.go+++ b/server/cmd/multica/cmd_daemon_test.go@@ -1054,3 +1054,55 @@ func TestPrintDaemonStatusShowsSkippedAgents(t *testing.T) {t.Fatalf("daemon status output = %q, want it to contain %q", got, want)}}++func TestPrintDaemonStatusRendersLargePIDPlainly(t *testing.T) {+ t.Parallel()++ // A pid arrives from the health response as a float64. Above 999999,+ // fmt's %v switches to exponent notation, so a long-running daemon used+ // to print "pid 1.531839e+06" — unreadable, and unsearchable in ps.+ // 1531839 is a real pid from a daemon that had been up for 31 hours.+ health := map[string]any{+ "status": "running",+ "pid": float64(1531839),+ "uptime": "31h0m0s",+ }++ var out bytes.Buffer+ printDaemonStatusReport(&out, "Daemon", health)++ got := out.String()+ if !strings.Contains(got, "running (pid 1531839, uptime 31h0m0s)") {+ t.Fatalf("daemon status output = %q, want the pid printed as decimal digits", got)+ }+ if strings.Contains(got, "e+06") {+ t.Fatalf("daemon status output = %q, must not use exponent notation for the pid", got)+ }+}++func TestFormatHealthPID(t *testing.T) {+ t.Parallel()++ cases := []struct {+ name string+ in any+ want string+ }{+ {"float64 below exponent threshold", float64(4242), "4242"},+ {"float64 at exponent threshold", float64(1000000), "1000000"},+ {"float64 well above threshold", float64(1531839), "1531839"},+ {"float32", float32(4242), "4242"},+ {"int", 4242, "4242"},+ {"int64", int64(4242), "4242"},+ {"string passthrough", "4242", "4242"},+ {"missing pid", nil, "?"},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ t.Parallel()+ if got := formatHealthPID(tc.in); got != tc.want {+ t.Fatalf("formatHealthPID(%#v) = %q, want %q", tc.in, got, tc.want)+ }+ })+ }+}