fix(builder): truncate large error output to prevent log overflow

Build errors from nix can be very large (100k+ chars). This truncates
error output to the first 50 and last 50 lines when it exceeds 100
lines, preventing journal and NATS message overflow.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-11 00:42:13 +01:00
parent a8aab16d0e
commit c13914bf5a
3 changed files with 130 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ import (
"log/slog"
"regexp"
"sort"
"strings"
"sync"
"time"
@@ -18,6 +19,18 @@ import (
// Allows: alphanumeric, dashes, underscores, dots.
var hostnameRegex = regexp.MustCompile(`^[a-zA-Z0-9._-]+$`)
// truncateOutput truncates output to the first and last N lines if it exceeds 2*N lines.
func truncateOutput(output string, keepLines int) string {
lines := strings.Split(output, "\n")
if len(lines) <= keepLines*2 {
return output
}
head := lines[:keepLines]
tail := lines[len(lines)-keepLines:]
omitted := len(lines) - keepLines*2
return strings.Join(head, "\n") + fmt.Sprintf("\n\n... (%d lines omitted) ...\n\n", omitted) + strings.Join(tail, "\n")
}
// BuilderConfig holds the configuration for the builder.
type BuilderConfig struct {
NATSUrl string
@@ -256,7 +269,7 @@ func (b *Builder) handleBuildRequest(subject string, data []byte) {
DurationSeconds: hostDuration,
}
if !result.Success {
hostResult.Error = result.Stderr
hostResult.Error = truncateOutput(result.Stderr, 50)
if hostResult.Error == "" && result.Error != nil {
hostResult.Error = result.Error.Error()
}