From 185dbff38757e863370dd92e2966ce7a344a6e7c Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 24 Oct 2026 23:18:23 +1100 Subject: [PATCH] fix(workflow): keep the failing lines when clipping gate output --- internal/domain/workflow/clip_test.go | 38 +++++++++++++++++++++++++++ internal/domain/workflow/workflow.go | 19 +++++++++++--- 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 internal/domain/workflow/clip_test.go diff --git a/internal/domain/workflow/clip_test.go b/internal/domain/workflow/clip_test.go new file mode 100644 index 000000000..ecf142873 --- /dev/null +++ b/internal/domain/workflow/clip_test.go @@ -0,0 +1,38 @@ +package workflow + +import ( + "strings" + "testing" +) + +func TestClipTailKeepsFailures(t *testing.T) { + passing := strings.Repeat("ok \tgithub.com/x/pkg\t0.010s\n", 200) + tests := []struct { + name, out string + want []string + }{ + { + name: "a failing test early in long output", + out: "--- FAIL: TestHostLaunch (0.25s)\n runner_test.go:191: operation not permitted\n" + passing + "task: Failed to run task \"lint\": exit status 1\n", + want: []string{"--- FAIL: TestHostLaunch", "runner_test.go:191:", "exit status 1"}, + }, + { + name: "a lint finding early in long output", + out: "internal/a/b.go:17:21: os/exec.Command must not be called (noctx)\n" + passing + "1 issues:\n", + want: []string{"b.go:17:21: os/exec.Command", "1 issues:"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := clipTail(tt.out, 2048) + if len(got) > 2048+len("…(truncated)\n") { + t.Fatalf("clipped to %d bytes, want at most 2048 plus the marker", len(got)) + } + for _, w := range tt.want { + if !strings.Contains(got, w) { + t.Errorf("clipped output lacks %q:\n%s", w, got) + } + } + }) + } +} diff --git a/internal/domain/workflow/workflow.go b/internal/domain/workflow/workflow.go index cf3d3b86c..379048712 100644 --- a/internal/domain/workflow/workflow.go +++ b/internal/domain/workflow/workflow.go @@ -538,16 +538,29 @@ func clip(s string, n int) string { return s[:cut] + clipMarker } -// clipTail keeps the last n bytes of s, cut on a rune boundary. +// failureLine matches a line that reports what failed: a failing test or +// package, a panic, an error, or a file:line finding. +var failureLine = regexp.MustCompile(`(?i)(^|\s)(--- )?(fail|failed|error|panic)\b|\.\w+:\d+(:\d+)?:`) + +// clipTail bounds command output to n bytes for a park reason or log. Gate +// output names its failures early and ends in a summary, so the failure lines +// are kept first, up to half of n, and the rest of n holds the tail. func clipTail(s string, n int) string { if len(s) <= n { return s } - cut := len(s) - n + var failures strings.Builder + for line := range strings.SplitSeq(s, "\n") { + if failureLine.MatchString(line) && failures.Len()+len(line)+1 <= n/2 { + failures.WriteString(line + "\n") + } + } + keep := n - failures.Len() + cut := len(s) - keep for cut < len(s) && !utf8.RuneStart(s[cut]) { cut++ } - return "…(truncated)\n" + s[cut:] + return failures.String() + "…(truncated)\n" + s[cut:] } // formatTokenUsage renders a token total split into cached and fresh tokens,