From 946bbf059fe6c5ad2078fc6f736646c125515779 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 3 Nov 2026 10:08:02 -0600 Subject: [PATCH] fix(mcpx): keep hook stderr out of the capped stdout, stream the CI log formatter (#2880) Two CodeRabbit minors deferred from PR #2840, both still live on main. hooks: execute() pointed cmd.Stdout and cmd.Stderr at one cappedWriter, so a hook that filled the bound with stdout before writing its diagnostic had truncated stdout recorded as the failure text. Each stream now has its own capped writer and the failure text is stderr's first line, falling back to stdout's when stderr is empty (a hook that exits nonzero silently). Two tests: a hook that writes 70KB of stdout without a newline and then one stderr line reports the diagnostic, never the stdout; a quiet hook still reports its first stdout line. CI log formatter: the readable-output step appended every parsed go test -json event to a list and printed from it in two passes, retaining memory proportional to the whole stream. It now re-reads the file per pass through a generator. Output is byte-identical (checked against a fixture with a passing package, a failing test, and a non-JSON line). --- .github/workflows/mcpx.yml | 31 +++++++++------- pkgs/mcpx/internal/hooks/runner.go | 22 +++++++++--- pkgs/mcpx/internal/hooks/runner_test.go | 48 +++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 16 deletions(-) diff --git a/.github/workflows/mcpx.yml b/.github/workflows/mcpx.yml index 560098b263..844a7730bf 100644 --- a/.github/workflows/mcpx.yml +++ b/.github/workflows/mcpx.yml @@ -199,20 +199,27 @@ jobs: # in the log instead of only in the JSON artifact. python3 - "$RUNNER_TEMP/gotest.json" <<'EOF' import json, sys - failed = set() - lines = [] - for line in open(sys.argv[1], errors="replace"): - try: - e = json.loads(line) - except ValueError: - continue - lines.append(e) - if e.get("Action") == "fail" and e.get("Test"): - failed.add((e.get("Package", ""), e.get("Test", ""))) - for e in lines: + + # Passes over the file, never a retained event list: the stream is as + # long as the suite is chatty, and the log needs package-level output + # first, then the full output of every failing test (#2880). + def events(): + with open(sys.argv[1], errors="replace") as fh: + for line in fh: + try: + yield json.loads(line) + except ValueError: + continue + + failed = { + (e.get("Package", ""), e.get("Test", "")) + for e in events() + if e.get("Action") == "fail" and e.get("Test") + } + for e in events(): if e.get("Action") == "output" and not e.get("Test"): print(e.get("Output", ""), end="") - for e in lines: + for e in events(): if e.get("Action") == "output" and (e.get("Package", ""), e.get("Test", "")) in failed: print(e.get("Output", ""), end="") EOF diff --git a/pkgs/mcpx/internal/hooks/runner.go b/pkgs/mcpx/internal/hooks/runner.go index 15344f4b08..687aca80b2 100644 --- a/pkgs/mcpx/internal/hooks/runner.go +++ b/pkgs/mcpx/internal/hooks/runner.go @@ -191,9 +191,10 @@ func (r *Runner) execute(h Hook, e events.Event, dropped uint64) { "MCPX_HOOK_EVENT_SESSION="+e.Session, "MCPX_HOOK_EVENT_URI="+e.URI, ) - var out = limitedHookOutput() - cmd.Stdout = out - cmd.Stderr = out + var stdout = limitedHookOutput() + var stderr = limitedHookOutput() + cmd.Stdout = stdout + cmd.Stderr = stderr start := time.Now() if err := cmd.Start(); err != nil { r.recordFailure(h, e, 0, err) @@ -229,7 +230,7 @@ func (r *Runner) execute(h Hook, e events.Event, dropped uint64) { return } if runErr != nil { - r.recordFailure(h, e, dur, fmt.Errorf("%v: %s", runErr, firstLine(out.String()))) + r.recordFailure(h, e, dur, fmt.Errorf("%v: %s", runErr, failureText(stderr.String(), stdout.String()))) return } if st := r.statsFor(h.Name); st.Slow > 0 && dur >= h.Timeout*4/5 { @@ -304,3 +305,16 @@ func firstLine(s string) string { } return s } + +// failureText names why a hook failed. The hook's own stderr diagnostic wins: +// stdout and stderr share a size bound but not a meaning, and a hook that +// fills the bound with stdout (say, a progress dump with no newline) before +// writing its one-line error to stderr would otherwise have that stdout +// recorded as the failure. A hook that exits nonzero without a stderr +// diagnostic still reports its first stdout line. +func failureText(stderr, stdout string) string { + if line := firstLine(stderr); line != "" { + return line + } + return firstLine(stdout) +} diff --git a/pkgs/mcpx/internal/hooks/runner_test.go b/pkgs/mcpx/internal/hooks/runner_test.go index e3a78b9dd5..273543eed0 100644 --- a/pkgs/mcpx/internal/hooks/runner_test.go +++ b/pkgs/mcpx/internal/hooks/runner_test.go @@ -134,3 +134,51 @@ func TestFailingHookPublishesHookFailedAndIsCounted(t *testing.T) { return len(st) == 1 && st[0].Failed == 1 }) } + +func TestHookFailureTextPrefersTheStderrDiagnostic(t *testing.T) { + // #2880: stdout and stderr shared one capped writer, so a hook that filled + // the bound with stdout before writing its diagnostic had truncated stdout + // recorded as the failure. Each stream has its own bound now, and the + // failure text is stderr's first line. + hs, err := hooks.Parse(`[{"name":"noisy","on":"progress","run":["sh","-c","head -c 70000 /dev/zero | tr '\\0' x; echo 'boom: the diagnostic' >&2; exit 1"]}]`, 2*time.Second) + if err != nil { + t.Fatal(err) + } + bus := events.New(16) + r := hooks.NewRunner(bus, hs, nil) + r.Start() + defer r.Stop() + bus.Publish(events.Event{Kind: events.Progress}) + waitFor(t, "failure recorded", func() bool { + st := r.Stats() + return len(st) == 1 && st[0].Failed == 1 + }) + last := r.Stats()[0].LastError + if !strings.Contains(last, "boom: the diagnostic") { + t.Fatalf("failure text must name the stderr diagnostic, got %.120q", last) + } + if strings.Contains(last, "xxxx") { + t.Fatalf("capped stdout polluted the failure text: %.120q", last) + } +} + +func TestHookFailureTextFallsBackToStdout(t *testing.T) { + // A hook that exits nonzero with nothing on stderr still reports its first + // stdout line, as before. + hs, err := hooks.Parse(`[{"name":"quiet","on":"progress","run":["sh","-c","echo 'stdout says why'; exit 1"]}]`, 2*time.Second) + if err != nil { + t.Fatal(err) + } + bus := events.New(16) + r := hooks.NewRunner(bus, hs, nil) + r.Start() + defer r.Stop() + bus.Publish(events.Event{Kind: events.Progress}) + waitFor(t, "failure recorded", func() bool { + st := r.Stats() + return len(st) == 1 && st[0].Failed == 1 + }) + if last := r.Stats()[0].LastError; !strings.Contains(last, "stdout says why") { + t.Fatalf("failure text must fall back to stdout, got %.120q", last) + } +}