diff --git a/.nextchanges/air/7582.md b/.nextchanges/air/7582.md new file mode 100644 index 00000000000..a6306817810 --- /dev/null +++ b/.nextchanges/air/7582.md @@ -0,0 +1 @@ +* `air list` shortens MLflow run names and unnamed-run IDs by keeping their end behind a leading `…`, and its columns no longer overflow on wide (for example CJK) text; `air get` shows an unnamed run's full MLflow run ID. ([#7582](https://github.com/databricks/cli/pull/7582)) diff --git a/cmd/air/list.go b/cmd/air/list.go index 7c00d7f9c78..4c75b286b19 100644 --- a/cmd/air/list.go +++ b/cmd/air/list.go @@ -383,9 +383,9 @@ func setMLflowLinks(ctx context.Context, w *databricks.WorkspaceClient, host str } entries[i].row.MLflowURL = mlflowLogsURL(host, ids) name := fetchMLflowRunName(ctx, w, ids.RunID) - // Shorten the name, not the retry note, to fit the column. + // Shorten the label, not the retry note, to fit the column. note := ids.retryNote() - entries[i].row.MLflowLabel = truncate(mlflowRunLabel(name, ids.RunID), mlflowColWidth-len(note)) + note + entries[i].row.MLflowLabel = mlflowRunLabel(name, ids.RunID, mlflowColWidth-len(note)) + note entries[i].row.ExperimentURL = mlflowExperimentURL(host, ids) // The ETA needs a progress-metric history fetch, so it's computed only // for running rows (the only ones that can have one). diff --git a/cmd/air/list_detail_test.go b/cmd/air/list_detail_test.go index 8cbe2c8d3b6..997b8131c48 100644 --- a/cmd/air/list_detail_test.go +++ b/cmd/air/list_detail_test.go @@ -7,6 +7,7 @@ import ( "strings" "testing" + "github.com/charmbracelet/lipgloss" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/stretchr/testify/assert" @@ -95,3 +96,52 @@ func TestListRowAndPanesLinkTheSameOlderRetry(t *testing.T) { assert.Contains(t, info, "MLflow URL: "+srv.URL+"/ml/experiments/E1/runs/R1\n") assert.Contains(t, logs, "\nMLflow URL (retry 1): "+wantURL+"\n") } + +func TestUnnamedOlderRetryLabelFitsTheListColumnAndIsWholeInTheInfoPane(t *testing.T) { + // Given a failed run whose retry 0 logged to an MLflow run with no name and + // whose last retry failed before it started. + t.Setenv("DATABRICKS_CACHE_DIR", t.TempDir()) + const mlflowRunID = "0123456789abcdef0123456789abcdef" + run := indexRun(7, 1000_000) + run.State.ResultState = jobs.RunResultStateFailed + task := run.Tasks[0] + run.Tasks = nil + for attempt, taskRunID := range []int64{70, 71} { + task.RunId, task.AttemptNumber = taskRunID, attempt + run.Tasks = append(run.Tasks, task) + } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case aiTrainingWorkflowsPath: + _, _ = w.Write([]byte(`{"training_workflows": [{"job_run_id": "7", "submit_time": {"seconds": 1000}}]}`)) + case "/api/2.2/jobs/runs/get": + _ = json.NewEncoder(w).Encode(run) + case "/api/2.2/jobs/runs/get-output": + if r.URL.Query().Get("run_id") == "70" { + _, _ = w.Write([]byte(`{"ai_runtime_task_output": {"mlflow_experiment_id": "E1", "mlflow_run_id": "` + mlflowRunID + `"}}`)) + return + } + _, _ = w.Write([]byte(`{}`)) + default: + _, _ = w.Write([]byte(`{}`)) + } + })) + t.Cleanup(srv.Close) + ctx := cmdio.MockDiscard(t.Context()) + w := newTestWorkspaceClient(t, srv.URL) + + // When the list and the info pane render the run. + rows, err := newRunFetcher(ctx, w, listQuery{ + userFilter: "me@example.com", currentUser: "me@example.com", limit: 10, fetchMLflow: true, + }).next(10) + require.NoError(t, err) + info, err := runDetailText(ctx, w, 7) + require.NoError(t, err) + + // Then the list row keeps the end of the ID and the whole retry note within + // the column, and the info pane shows the whole ID. + require.Len(t, rows, 1) + assert.Equal(t, "…56789abcdef (retry 0)", rows[0].MLflowLabel) + assert.Equal(t, mlflowColWidth, lipgloss.Width(rows[0].MLflowLabel)) + assert.Contains(t, info, mlflowRunID+" (retry 0)") +} diff --git a/cmd/air/list_tui_render.go b/cmd/air/list_tui_render.go index 3f23535a8e2..7b9add62f47 100644 --- a/cmd/air/list_tui_render.go +++ b/cmd/air/list_tui_render.go @@ -4,6 +4,7 @@ import ( "strings" "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/x/ansi" "github.com/muesli/termenv" ) @@ -241,11 +242,29 @@ func pad(s string, n int, right bool) string { // truncate shortens s to a visible width of n, appending "…" on overflow. func truncate(s string, n int) string { - if lipgloss.Width(s) <= n { + return ansi.Truncate(s, n, "…") +} + +// truncateStart shortens s to a visible width of n, keeping its end behind a +// leading "…" on overflow. +func truncateStart(s string, n int) string { + width := lipgloss.Width(s) + if width <= n { return s } if n <= 1 { + return truncate(s, n) + } + // TruncateLeft never splits a character, so a cut that lands inside a + // two-cell character leaves the result one cell too wide. + cut := width - (n - 1) + out := ansi.TruncateLeft(s, cut, "…") + if lipgloss.Width(out) > n { + out = ansi.TruncateLeft(s, cut+1, "…") + } + // TruncateLeft drops the "…" when nothing of s is left. + if out == "" { return "…" } - return string([]rune(s)[:n-1]) + "…" + return out } diff --git a/cmd/air/list_tui_test.go b/cmd/air/list_tui_test.go index f195006f222..89964d14619 100644 --- a/cmd/air/list_tui_test.go +++ b/cmd/air/list_tui_test.go @@ -338,6 +338,37 @@ func TestListModelDetailLateMsgDropped(t *testing.T) { assert.NotContains(t, m.View(), "late result") } +func TestTruncateFitsDisplayWidth(t *testing.T) { + // A CJK character takes two cells and a combining mark none, so the cut is + // by display width and never splits a character. + const wide = "/Users/张伟/大模型微调实验第一百二十三次评估" + const accented = "xxxxxxxxxxxxxxxxxxxx\u0301yz" + tests := []struct { + name string + s string + n int + wantEnd string // truncate + wantStart string // truncateStart + }{ + {name: "fits", s: "abc", n: 5, wantEnd: "abc", wantStart: "abc"}, + {name: "exactly fits", s: "abcde", n: 5, wantEnd: "abcde", wantStart: "abcde"}, + {name: "one cell over", s: "abcdef", n: 5, wantEnd: "abcd…", wantStart: "…cdef"}, + {name: "wide characters", s: wide, n: 11, wantEnd: "/Users/张…", wantStart: "…十三次评估"}, + {name: "cut inside a wide character", s: wide, n: 12, wantEnd: "/Users/张伟…", wantStart: "…十三次评估"}, + {name: "wide character at width two", s: "评估评估", n: 2, wantEnd: "…", wantStart: "…"}, + {name: "combining mark stays with its letter", s: accented, n: 4, wantEnd: "xxx…", wantStart: "…x\u0301yz"}, + {name: "combining mark is dropped with its letter", s: accented, n: 3, wantEnd: "xx…", wantStart: "…yz"}, + {name: "width of one", s: "abcdef", n: 1, wantEnd: "…", wantStart: "…"}, + {name: "width of zero", s: "abcdef", n: 0, wantEnd: "", wantStart: ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.wantEnd, truncate(tt.s, tt.n)) + assert.Equal(t, tt.wantStart, truncateStart(tt.s, tt.n)) + }) + } +} + func TestPadAndTruncate(t *testing.T) { assert.Equal(t, "ab ", pad("ab", 5, false)) assert.Equal(t, " ab", pad("ab", 5, true)) diff --git a/cmd/air/mlflow.go b/cmd/air/mlflow.go index 7cf9f867630..82a0a96ca3e 100644 --- a/cmd/air/mlflow.go +++ b/cmd/air/mlflow.go @@ -156,14 +156,16 @@ func fetchMLflowRunName(ctx context.Context, w *databricks.WorkspaceClient, mlfl } // mlflowRunLabel is the text shown for the MLflow Run cell: the run's name, or -// "...{last 8 of run id}" when the name is unknown. Mirrors Python's -// _get_mlflow_run_name (cli_display.py). -func mlflowRunLabel(name, mlflowRunID string) string { - if name != "" { - return name +// its run ID when the name is unknown. A label wider than width keeps its end +// behind a leading "…", because run names often share a prefix and differ at +// the end. A width of 0 or less means no limit. +func mlflowRunLabel(name, mlflowRunID string, width int) string { + label := name + if label == "" { + label = mlflowRunID } - if len(mlflowRunID) > 8 { - return "..." + mlflowRunID[len(mlflowRunID)-8:] + if width <= 0 { + return label } - return "..." + mlflowRunID + return truncateStart(label, width) } diff --git a/cmd/air/mlflow_test.go b/cmd/air/mlflow_test.go index 2f316acaf74..1728ab22111 100644 --- a/cmd/air/mlflow_test.go +++ b/cmd/air/mlflow_test.go @@ -5,6 +5,7 @@ import ( "net/http/httptest" "testing" + "github.com/charmbracelet/lipgloss" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/stretchr/testify/assert" @@ -175,12 +176,33 @@ func TestMLflowURLs(t *testing.T) { } func TestMLflowRunLabel(t *testing.T) { - // Uses the run name when it is known. - assert.Equal(t, "sunny-cat-42", mlflowRunLabel("sunny-cat-42", "0123456789abcdef")) - // Falls back to the last 8 characters of a long run id. - assert.Equal(t, "...9abcdef0", mlflowRunLabel("", "0123456789abcdef0")) - // A short run id is shown in full behind the ellipsis. - assert.Equal(t, "...short", mlflowRunLabel("", "short")) + const runID = "0123456789abcdef0123456789abcdef" + tests := []struct { + name string + runName string + runID string + width int + want string + }{ + {name: "long name keeps its end", runName: "qwen-finetune-2026-10-23-lr-3e-5", runID: runID, width: 22, want: "…ne-2026-10-23-lr-3e-5"}, + {name: "short name is shown whole", runName: "sunny-cat-42", runID: runID, width: 22, want: "sunny-cat-42"}, + {name: "name at the width is shown whole", runName: "exactly-twenty-two-chr", runID: runID, width: 22, want: "exactly-twenty-two-chr"}, + {name: "unnamed run keeps the end of its ID", runID: runID, width: 22, want: "…bcdef0123456789abcdef"}, + {name: "short ID is shown whole", runID: "short", width: 22, want: "short"}, + {name: "wide characters count as two cells", runName: "训练运行-实验-第一百二十三次", runID: runID, width: 8, want: "…十三次"}, + {name: "width of one leaves only the ellipsis", runName: "sunny-cat-42", runID: runID, width: 1, want: "…"}, + {name: "no width limit shows the whole name", runName: "qwen-finetune-2026-10-23-lr-3e-5", runID: runID, want: "qwen-finetune-2026-10-23-lr-3e-5"}, + {name: "no width limit shows the whole ID", runID: runID, want: runID}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := mlflowRunLabel(tt.runName, tt.runID, tt.width) + assert.Equal(t, tt.want, got) + if tt.width > 0 { + assert.LessOrEqual(t, lipgloss.Width(got), tt.width) + } + }) + } } func TestFetchMLflowRunName(t *testing.T) { diff --git a/cmd/air/render.go b/cmd/air/render.go index 3985ab57588..755e8540fb9 100644 --- a/cmd/air/render.go +++ b/cmd/air/render.go @@ -124,7 +124,8 @@ func renderRunText(ctx context.Context, out io.Writer, w *databricks.WorkspaceCl } if ids != nil { - view.mlflowLabel = mlflowRunLabel(fetchMLflowRunName(ctx, w, ids.RunID), ids.RunID) + ids.retryNote() + // The box grows to fit its fields, so the label is shown whole. + view.mlflowLabel = mlflowRunLabel(fetchMLflowRunName(ctx, w, ids.RunID), ids.RunID, 0) + ids.retryNote() view.mlflowURL = mlflowRunURL(w.Config.Host, ids) }