fix(workflow): keep the failing lines when clipping gate output

This commit is contained in:
sam committed 2026-10-24 23:18:23 +11:00
1 parent 1c6281d098
commit 185dbff387
2 files changed
+54 -3

No files matched your search

+38
View File
@@ -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)
}
}
})
}
}
+16 -3
View File
@@ -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,