diff --git a/cmd/devbox/main.go b/cmd/devbox/main.go index 919133e..7888f1b 100644 --- a/cmd/devbox/main.go +++ b/cmd/devbox/main.go @@ -833,38 +833,42 @@ func statusCmd() *cobra.Command { return writeJSON(out, s) } - // Prefer the LIVE daemon when it's running AND stdout is a terminal: - // the socket can show paused state and the applied snapshot, which disk - // can't. Piped/non-TTY output must stay byte-identical to v1 (scripts - // parse it), so the enrichment is gated behind isTTY — the disk path is - // the unchanged v1 output. - if isTTY(cmd) { - if live, err := control.DialState(dir); err == nil { - fmt.Fprintf(out, "device: %s\n", id.Fingerprint()) - fmt.Fprintf(out, "hub: %s\n", orNone(d.Hub)) - paused := "" - if live.Paused { - paused = " ⏸️ PAUSED" - } - fmt.Fprintf(out, "mounts: %d (live)%s\n", len(live.Mounts), paused) - for _, m := range live.Mounts { - mode := "rw" - if m.ReadOnly { - mode = "ro" - } - if m.Pinned { - mode += ",pinned" - } - fmt.Fprintf(out, " - %s/%s -> %s [%s] @%s (%s)\n", - m.Share, m.Subpath, m.Local, mode, short(orNone(m.BaseSnapshot)), syncAge(m.LastSyncUnix)) - if m.LastErr != "" { - fmt.Fprintf(out, " ⚠️ last sync failed: %s\n", m.LastErr) - } - } - return nil + // Attempt live daemon state once; used for both TTY enrichment and the + // disk-path "daemon not running" warning. + live, dialErr := control.DialState(dir) + + // TTY + live: enriched output (paused state, applied snapshot, sync age). + // Piped/non-TTY callers skip this block — enrichment gated behind isTTY + // so script-parseable fields stay stable. + if isTTY(cmd) && dialErr == nil { + fmt.Fprintf(out, "device: %s\n", id.Fingerprint()) + fmt.Fprintf(out, "hub: %s\n", orNone(d.Hub)) + paused := "" + if live.Paused { + paused = " ⏸️ PAUSED" } + fmt.Fprintf(out, "mounts: %d (live)%s\n", len(live.Mounts), paused) + for _, m := range live.Mounts { + mode := "rw" + if m.ReadOnly { + mode = "ro" + } + if m.Pinned { + mode += ",pinned" + } + fmt.Fprintf(out, " - %s/%s -> %s [%s] @%s (%s)\n", + m.Share, m.Subpath, m.Local, mode, short(orNone(m.BaseSnapshot)), syncAge(m.LastSyncUnix)) + if m.LastErr != "" { + fmt.Fprintf(out, " ⚠️ last sync failed: %s\n", m.LastErr) + } + } + return nil } + // Disk path (non-TTY or TTY with daemon not running). + if dialErr != nil { + fmt.Fprintln(out, "⚠️ daemon not running — showing last-known disk state (run: devbox start)") + } fmt.Fprintf(out, "device: %s\n", id.Fingerprint()) fmt.Fprintf(out, "hub: %s\n", orNone(d.Hub)) fmt.Fprintf(out, "mounts: %d\n", len(d.Mounts)) diff --git a/cmd/devbox/main_test.go b/cmd/devbox/main_test.go index 239b355..59bd2f0 100644 --- a/cmd/devbox/main_test.go +++ b/cmd/devbox/main_test.go @@ -11,6 +11,7 @@ import ( "time" "github.com/shoemoney/devbox/internal/config" + "github.com/shoemoney/devbox/internal/identity" ) // TestStatusJSON proves `status --json` emits valid JSON and reports not-joined @@ -174,3 +175,35 @@ func TestWritePidArbitration(t *testing.T) { t.Fatal("pidfile still live after removePid") } } + +// TestStatusDaemonNotRunning proves that `status` (non-JSON) warns the user +// when the device is joined but the daemon is not running. +func TestStatusDaemonNotRunning(t *testing.T) { + cfg := t.TempDir() + t.Setenv("XDG_CONFIG_HOME", cfg) + dir := filepath.Join(cfg, "devbox") + + // Create identity so identity.Load succeeds (device is "joined"). + if _, err := identity.LoadOrCreate(dir); err != nil { + t.Fatalf("LoadOrCreate identity: %v", err) + } + // Write daemon config with a hub and one mount. + if err := config.SaveDaemon(dir, config.Daemon{ + Hub: "http://hub.local:8080", + Bearer: "tok", + Mounts: []config.Mount{{Share: "proj", Local: t.TempDir(), Hub: "http://hub.local:8080"}}, + }); err != nil { + t.Fatalf("SaveDaemon: %v", err) + } + + // No socket in dir → DialState returns ErrNotRunning → disk path with warning. + var buf bytes.Buffer + cmd := statusCmd() + cmd.SetOut(&buf) + if err := cmd.Execute(); err != nil { + t.Fatalf("status: %v", err) + } + if !strings.Contains(buf.String(), "daemon not running") { + t.Fatalf("expected 'daemon not running' in output; got:\n%s", buf.String()) + } +} diff --git a/internal/transport/client.go b/internal/transport/client.go index 889a635..41998e2 100644 --- a/internal/transport/client.go +++ b/internal/transport/client.go @@ -53,15 +53,18 @@ func New(base string) *Client { // phases that SHOULD be fast instead (connect, TLS, time-to-first-byte) and let // TCP keepalive reap a dead peer. ponytail: a mid-body stall isn't bounded here; // add a stall-timeout reader if a flaky link ever hangs transfers. - hc: &http.Client{ - Transport: &http.Transport{ - DialContext: (&net.Dialer{Timeout: 10 * time.Second, KeepAlive: 30 * time.Second}).DialContext, - TLSHandshakeTimeout: 10 * time.Second, - ResponseHeaderTimeout: 30 * time.Second, - DisableCompression: true, // we negotiate gzip explicitly so the rate limiter sees wire bytes - MaxIdleConnsPerHost: 16, // reuse connections for parallel blob transfers - }, - }, + hc: func() *http.Client { + // Clone preserves Proxy (ProxyFromEnvironment), ForceAttemptHTTP2, + // IdleConnTimeout and ExpectContinueTimeout from DefaultTransport, then + // we override only the fields we intentionally tune. + tr := http.DefaultTransport.(*http.Transport).Clone() + tr.DialContext = (&net.Dialer{Timeout: 10 * time.Second, KeepAlive: 30 * time.Second}).DialContext + tr.TLSHandshakeTimeout = 10 * time.Second + tr.ResponseHeaderTimeout = 30 * time.Second + tr.DisableCompression = true // we negotiate gzip explicitly so the rate limiter sees wire bytes + tr.MaxIdleConnsPerHost = 16 // reuse connections for parallel blob transfers + return &http.Client{Transport: tr} + }(), } } diff --git a/internal/transport/client_test.go b/internal/transport/client_test.go index 96159b4..bdcc424 100644 --- a/internal/transport/client_test.go +++ b/internal/transport/client_test.go @@ -392,6 +392,22 @@ func TestHubDateMissingHeader(t *testing.T) { } } +// TestTransportHonorsProxyEnv asserts that Clone()-based transport preserves +// Proxy (ProxyFromEnvironment) and ForceAttemptHTTP2 from DefaultTransport. +func TestTransportHonorsProxyEnv(t *testing.T) { + c := New("http://x") + tr, ok := c.hc.Transport.(*http.Transport) + if !ok { + t.Fatal("transport must be *http.Transport") + } + if tr.Proxy == nil { + t.Fatal("transport must honor HTTP_PROXY env (Proxy was nil)") + } + if !tr.ForceAttemptHTTP2 { + t.Fatal("transport must have ForceAttemptHTTP2 (lost from DefaultTransport)") + } +} + func TestErrorSurfaced(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { writeJSON(t, w, http.StatusConflict, proto.Error{Error: "share exists"})