Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 56 additions & 44 deletions cmd/machine/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
maxRemoteDiagnosticsResponse = 2 * 1024 * 1024
unknownFreshness = "unknown"
unavailableAvailability = "unavailable"
localTimeLayout = "2006-01-02 15:04:05"
)

type boundedDiagnosticsBuffer struct {
Expand Down Expand Up @@ -271,42 +272,8 @@ func renderDiagnosticsText(w io.Writer, result MachineDiagnostics) error {
if result.Source.Message != "" {
_, _ = fmt.Fprintf(w, "Diagnostic detail %s\n", result.Source.Message)
}
if result.Daemon != nil { //nolint:nestif // daemon details are intentionally rendered as one group.
lastPatrol := "Not yet observed"
if result.Daemon.LastPatrolAt != nil {
lastPatrol = result.Daemon.LastPatrolAt.Local().Format("2006-01-02 15:04:05")
}
_, _ = fmt.Fprintf(
w,
"Devsy daemon %s\nSnapshot freshness %s\nLast patrol %s\nWorkspaces %d\n",
result.Daemon.Health,
result.Source.Freshness,
lastPatrol,
result.Daemon.WorkspaceCount,
)
if result.Daemon.LastError != nil {
_, _ = fmt.Fprintf(
w,
"Last daemon error %s (%s, %s)\n",
result.Daemon.LastError.Message,
result.Daemon.LastError.Code,
result.Daemon.LastError.Timestamp.Format(time.RFC3339),
)
}
if result.Daemon.ShutdownCandidate != nil {
eligible := "now"
if result.Daemon.ShutdownCandidate.EligibleAt != nil {
eligible = result.Daemon.ShutdownCandidate.EligibleAt.Local().
Format("2006-01-02 15:04:05")
}
_, _ = fmt.Fprintf(
w,
"Shutdown candidate %s (eligible %s)\n",
result.Daemon.ShutdownCandidate.WorkspaceID,
eligible,
)
}
renderWorkspaceDiagnostics(w, result.Daemon.Workspaces)
if result.Daemon != nil {
renderDaemonDiagnostics(w, result.Daemon, result.Source.Freshness)
}
for _, event := range result.Events {
_, _ = fmt.Fprintf(
Expand All @@ -321,6 +288,56 @@ func renderDiagnosticsText(w io.Writer, result MachineDiagnostics) error {
return nil
}

func renderDaemonDiagnostics(w io.Writer, daemon *machinediagnostics.Status, freshness string) {
_, _ = fmt.Fprintf(
w,
"Devsy daemon %s\nSnapshot freshness %s\nLast patrol %s\nWorkspaces %d\n",
daemon.Health,
freshness,
optionalLocalTime(daemon.LastPatrolAt, "Not yet observed"),
daemon.WorkspaceCount,
)
renderLastDaemonError(w, daemon.LastError)
renderShutdownCandidate(w, daemon.ShutdownCandidate)
renderWorkspaceDiagnostics(w, daemon.Workspaces)
}

func renderLastDaemonError(w io.Writer, lastErr *machinediagnostics.DiagnosticError) {
if lastErr == nil {
return
}
_, _ = fmt.Fprintf(
w,
"Last daemon error %s (%s, %s)\n",
lastErr.Message,
lastErr.Code,
lastErr.Timestamp.Format(time.RFC3339),
)
}

func renderShutdownCandidate(w io.Writer, candidate *machinediagnostics.ShutdownCandidate) {
if candidate == nil {
return
}
_, _ = fmt.Fprintf(
w,
"Shutdown candidate %s (eligible %s)\n",
candidate.WorkspaceID,
optionalLocalTime(candidate.EligibleAt, "now"),
)
}

func optionalLocalTime(at *time.Time, fallback string) string {
if at == nil {
return fallback
}
return formatLocalTime(*at)
}

func formatLocalTime(at time.Time) string {
return at.Local().Format(localTimeLayout)
}

func renderWorkspaceDiagnostics(w io.Writer, workspaces []machinediagnostics.WorkspaceStatus) {
if len(workspaces) == 0 {
return
Expand All @@ -329,10 +346,7 @@ func renderWorkspaceDiagnostics(w io.Writer, workspaces []machinediagnostics.Wor
tw := tabwriter.NewWriter(w, 0, 4, 2, ' ', 0)
_, _ = fmt.Fprintln(tw, "WORKSPACE\tSTATE\tLAST ACTIVITY\tMACHINE SHUTDOWN STATUS")
for _, workspace := range workspaces {
lastActivity := "-"
if workspace.LastActivityAt != nil {
lastActivity = workspace.LastActivityAt.Local().Format("2006-01-02 15:04:05")
}
lastActivity := optionalLocalTime(workspace.LastActivityAt, "-")
_, _ = fmt.Fprintf(
tw,
"%s\t%s\t%s\t%s\n",
Expand All @@ -359,15 +373,13 @@ func workspaceAutoStopDetail(

func workspaceBlockerDetail(workspace machinediagnostics.WorkspaceStatus) string {
if workspace.State == machinediagnostics.WorkspaceActive && workspace.IdleDeadlineAt != nil {
return workspace.BlockerReason + " (" + workspace.IdleDeadlineAt.Local().
Format("2006-01-02 15:04:05") +
")"
return workspace.BlockerReason + " (" + formatLocalTime(*workspace.IdleDeadlineAt) + ")"
}
return workspace.BlockerReason
}

func workspaceDeadlineDetail(workspace machinediagnostics.WorkspaceStatus) string {
deadline := workspace.IdleDeadlineAt.Local().Format("2006-01-02 15:04:05")
deadline := formatLocalTime(*workspace.IdleDeadlineAt)
if workspace.State == machinediagnostics.WorkspaceIdleDue {
return "Eligible now (" + deadline + ")"
}
Expand Down
174 changes: 174 additions & 0 deletions cmd/machine/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,180 @@ func TestRenderDiagnosticsTextIncludesWorkspaceInactivityDetails(t *testing.T) {
assert.Contains(t, text, "Workspace configuration is invalid")
}

type renderDiagnosticsTextCase struct {
name string
result MachineDiagnostics
want string
}

func runRenderDiagnosticsTextCases(t *testing.T, tests []renderDiagnosticsTextCase) {
t.Helper()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var output bytes.Buffer
require.NoError(t, renderDiagnosticsText(&output, tt.result))
assert.Equal(t, tt.want, output.String())
})
}
}

func diagnosticsTestTime(hour, minute int) time.Time {
return time.Date(2026, 10, 18, hour, minute, 0, 0, time.UTC)
}

func diagnosticsLocalClock(at time.Time) string {
return at.Local().Format("2006-01-02 15:04:05")
}

const diagnosticsTestWorkspaceID = "api"

func diagnosticsTestFixture() (MachineInfo, CollectionSource) {
return MachineInfo{ID: "machine-1", State: "running"},
CollectionSource{Availability: "available", Freshness: "fresh"}
}

func TestRenderDiagnosticsTextWithoutDaemon(t *testing.T) {
machine, source := diagnosticsTestFixture()
header := "Machine machine-1\nProvider state running\nDiagnostics available\n"

runRenderDiagnosticsTextCases(t, []renderDiagnosticsTextCase{
{
name: "no daemon",
result: MachineDiagnostics{Machine: machine, Source: source},
want: header,
},
{
name: "no daemon with diagnostic message",
result: MachineDiagnostics{
Machine: machine,
Source: CollectionSource{
Availability: "unavailable",
Freshness: "unknown",
Message: "agent offline",
},
},
want: "Machine machine-1\nProvider state running\nDiagnostics unavailable\n" +
"Diagnostic detail agent offline\n",
},
})
}

func TestRenderDiagnosticsTextWithDaemon(t *testing.T) {
machine, source := diagnosticsTestFixture()
lastPatrol := diagnosticsTestTime(10, 0)
daemonHeader := "Machine machine-1\nProvider state running\nDiagnostics available\n" +
"Devsy daemon healthy\nSnapshot freshness fresh\n"

runRenderDiagnosticsTextCases(t, []renderDiagnosticsTextCase{
{
name: "daemon without patrol",
result: MachineDiagnostics{
Machine: machine,
Source: source,
Daemon: &machinediagnostics.Status{Health: machinediagnostics.DaemonHealthy},
},
want: daemonHeader + "Last patrol Not yet observed\nWorkspaces 0\n",
},
{
name: "shutdown candidate without eligibility time",
result: MachineDiagnostics{
Machine: machine,
Source: source,
Daemon: &machinediagnostics.Status{
Health: machinediagnostics.DaemonHealthy,
LastPatrolAt: &lastPatrol,
WorkspaceCount: 1,
ShutdownCandidate: &machinediagnostics.ShutdownCandidate{
WorkspaceID: diagnosticsTestWorkspaceID,
},
},
},
want: daemonHeader + "Last patrol " + diagnosticsLocalClock(lastPatrol) + "\n" +
"Workspaces 1\n" +
"Shutdown candidate " + diagnosticsTestWorkspaceID + " (eligible now)\n",
},
})
}

func TestRenderDiagnosticsTextFullReport(t *testing.T) {
lastPatrol := diagnosticsTestTime(10, 0)
lastActivity := diagnosticsTestTime(9, 45)
deadline := diagnosticsTestTime(10, 30)
want := "Machine machine-1\nProvider state running\nDiagnostics available\n" +
"Devsy daemon degraded\n" +
"Snapshot freshness fresh\n" +
"Last patrol " + diagnosticsLocalClock(
lastPatrol,
) + "\n" +
"Workspaces 2\n" +
"Last daemon error provider timeout (patrol_failed, 2026-10-18T09:55:00Z)\n" +
"Shutdown candidate " + diagnosticsTestWorkspaceID + " (eligible " + diagnosticsLocalClock(
deadline,
) + ")\n" +
"\nWorkspace inactivity:\n" +
"WORKSPACE STATE LAST ACTIVITY MACHINE SHUTDOWN STATUS\n" +
diagnosticsTestWorkspaceID + " active " + diagnosticsLocalClock(
lastActivity,
) + " " +
diagnosticsLocalClock(
deadline,
) + "\n" +
"docs busy - Delayed while workspace is busy\n" +
"2026-10-18T10:05:00Z warn patrol.failed provider timeout\n"

runRenderDiagnosticsTextCases(t, []renderDiagnosticsTextCase{
{
name: "daemon with error, eligible candidate, workspaces, and events",
result: diagnosticsFullReportResult(lastPatrol, lastActivity, deadline),
want: want,
},
})
}

func diagnosticsFullReportResult(lastPatrol, lastActivity, deadline time.Time) MachineDiagnostics {
machine, source := diagnosticsTestFixture()
errorAt := diagnosticsTestTime(9, 55)
eventAt := diagnosticsTestTime(10, 5)
return MachineDiagnostics{
Machine: machine,
Source: source,
Daemon: &machinediagnostics.Status{
Health: machinediagnostics.DaemonDegraded,
LastPatrolAt: &lastPatrol,
WorkspaceCount: 2,
LastError: &machinediagnostics.DiagnosticError{
Code: "patrol_failed",
Message: "provider timeout",
Timestamp: errorAt,
},
ShutdownCandidate: &machinediagnostics.ShutdownCandidate{
WorkspaceID: diagnosticsTestWorkspaceID,
EligibleAt: &deadline,
},
Workspaces: []machinediagnostics.WorkspaceStatus{
{
ID: diagnosticsTestWorkspaceID,
State: machinediagnostics.WorkspaceActive,
LastActivityAt: &lastActivity,
IdleDeadlineAt: &deadline,
},
{ID: "docs", State: machinediagnostics.WorkspaceBusy},
},
},
Events: []machinediagnostics.Event{
{
SchemaVersion: machinediagnostics.SchemaVersion,
SessionID: "session-1",
Sequence: 1,
Timestamp: eventAt,
Level: machinediagnostics.LevelWarn,
Type: machinediagnostics.EventPatrolFailed,
Message: "provider timeout",
},
},
}
}

func TestWorkspaceAutoStopDetailMarksPastDeadlineEligibleNow(t *testing.T) {
deadline := time.Now().Add(-time.Minute)
detail := workspaceAutoStopDetail(machinediagnostics.WorkspaceStatus{
Expand Down
Loading