From 20be50cccc3a83c9b8b5073a0707559fbdedb408 Mon Sep 17 00:00:00 2001 From: Geno Roupsky <185580+groupsky@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:19:39 +0200 Subject: [PATCH] fix: a failed check notice shows the end of the output, with short tree paths The notice of a failed check now replaces the tree paths (PULLGATE_TREE, PULLGATE_OLD_TREE) with NEW and OLD, keeps the last lines of the output (at most 10 lines and 1500 characters), and cuts a long line in the middle, so the reason at the end is shown. Fixes #25 Co-Authored-By: Claude Opus 5.5 --- DESIGN.md | 8 ++++-- THREAT-MODEL.md | 6 +++-- internal/gate/check_test.go | 45 +++++++++++++++++++++++++++++++ internal/gate/run.go | 16 ++++++++++- internal/message/message.go | 46 +++++++++++++++++++++++++------- internal/message/message_test.go | 21 +++++++++++++++ 6 files changed, 127 insertions(+), 15 deletions(-) diff --git a/DESIGN.md b/DESIGN.md index 03f4f72..5c2b37b 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -323,8 +323,12 @@ used again. 6. Export the tree of the tip, and the tree of the applied SHA (`PULLGATE_OLD_TREE`). 7. Run `check` as `check_user` (section 11). If it fails, mark the SHA - "check failed", send a notice with the last lines of its output, and do - not ask. The same SHA is not checked again. + "check failed", send a notice with the end of its output, and do not + ask. The same SHA is not checked again. In that output the tree paths + (`PULLGATE_TREE`, `PULLGATE_OLD_TREE`) become `NEW` and `OLD`. The + notice keeps the last lines, at most 10 and at most 1500 characters, + so the reason (most tools print it last) is shown. A line longer than + 300 characters keeps its start and its end, with `…` between. 8. Run `summary` as `check_user`, and build the message (section 12). 9. If a question is open, close it: root marks it superseded in its state, and asks the listener to edit it ("a newer commit replaced this diff --git a/THREAT-MODEL.md b/THREAT-MODEL.md index 6738c8d..6d5a74d 100644 --- a/THREAT-MODEL.md +++ b/THREAT-MODEL.md @@ -150,8 +150,10 @@ or during the review. are cleaned and escaped before they are shown, so a name cannot fake a line of the question. - **Commit subjects and summary output.** The same cleaning; HTML-escaped, - each line capped. The head of the question (host, instance, SHA, the - warnings) always comes first, before any text from the commit. + each line capped. The output of a failed `check` keeps its end, so the + reason is not cut away; root's tree paths in it become `NEW` and `OLD`. + The head of the question (host, instance, SHA, the warnings) always + comes first, before any text from the commit. - **Hostile text shown as a link in a trusted message.** A commit picks its own file names, subjects and command output. Telegram turns text that looks like a domain or a URL (`README.md`, `login.example.com`), an diff --git a/internal/gate/check_test.go b/internal/gate/check_test.go index 32163b9..2b1d0f8 100644 --- a/internal/gate/check_test.go +++ b/internal/gate/check_test.go @@ -332,3 +332,48 @@ func TestCheckWaiterStopsWhenRequestIsGone(t *testing.T) { t.Fatalf("the waiter must not reply: %d messages", len(out)) } } + +// TestCheckNoticeShowsTheReason: a check that prints a long tree path +// before its reason gives a notice that holds the reason, with NEW for the +// tree path (code6x/pullgate#25). +func TestCheckNoticeShowsTheReason(t *testing.T) { + f := newFixture(t, ``, "") + f.script("check.sh", `echo "broker check: config $PULLGATE_TREE/broker.toml: [secret.x] preapprove: is not allowed with ask = \"never\"" +echo "$PULLGATE_TREE" > `+f.ctrl+`/tree +exit 1 +`) + f.commit("first", "one", nil) + if r := f.run(); r != CheckFailed { + t.Fatalf("run: %s", r) + } + tree := f.read("tree") + ms := f.outbox() + if tree == "" || contains(ms, tree) { + t.Fatalf("the notice must not hold the tree path %q: %+v", tree, ms) + } + if !contains(ms, "NEW/broker.toml: [secret.x] preapprove: is not allowed with ask = "never"") { + t.Fatalf("the notice must hold the reason: %+v", ms) + } +} + +// TestCheckNoticeKeepsTheLastLine: output longer than the limit keeps its +// last line, and a long line keeps its end. +func TestCheckNoticeKeepsTheLastLine(t *testing.T) { + f := newFixture(t, ``, "") + f.script("check.sh", `i=0 +while [ $i -lt 60 ]; do printf 'noise %0250d\n' $i; i=$((i+1)); done +printf 'start %0900d the real reason\n' 0 +exit 1 +`) + f.commit("first", "one", nil) + if r := f.run(); r != CheckFailed { + t.Fatalf("run: %s", r) + } + ms := f.outbox() + if len(ms) != 1 || !strings.Contains(ms[0].Text, "the real reason") || !strings.Contains(ms[0].Text, "start 000") { + t.Fatalf("the notice must keep the start and the end of the last line: %+v", ms) + } + if message.Len(ms[0].Text) > 4096 || strings.Count(ms[0].Text, "noise") < 2 { + t.Fatalf("the notice must show more than one line and fit: %d", message.Len(ms[0].Text)) + } +} diff --git a/internal/gate/run.go b/internal/gate/run.go index 555014d..b71e1b0 100644 --- a/internal/gate/run.go +++ b/internal/gate/run.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "strings" "time" "github.com/code6x/pullgate/internal/config" @@ -281,7 +282,7 @@ func (e *Env) decide(ctx context.Context, st *state.State, tip string) (Result, if err != nil { st.SetMark(tip, state.MarkCheckFailed, now) e.Log.Warn("check failed; not asking", "sha", tip, "err", err) - e.notice("⚠ The check of " + code(tip) + " failed, so it is not asked and not checked again: " + message.Code(err.Error()) + "\n" + message.Output(out, 10)) + e.notice("⚠ The check of " + code(tip) + " failed, so it is not asked and not checked again: " + message.Code(err.Error()) + "\n" + message.Output(shortTrees(out, p.env.Tree, p.env.OldTree), 10)) return CheckFailed, nil } e.Log.Info("check passed", "sha", tip) @@ -347,3 +348,16 @@ func (e *Env) decide(ctx context.Context, st *state.State, tip string) (Result, e.Log.Info("asked", "sha", tip, "question", nq.ID) return Asked, nil } + +// shortTrees replaces the long tree paths in command output with NEW and +// OLD: they say nothing to the owner and push the reason out of the +// message (code6x/pullgate#25). +func shortTrees(out, tree, oldTree string) string { + if tree != "" { + out = strings.ReplaceAll(out, tree, "NEW") + } + if oldTree != "" { + out = strings.ReplaceAll(out, oldTree, "OLD") + } + return out +} diff --git a/internal/message/message.go b/internal/message/message.go index 8808348..a04f94d 100644 --- a/internal/message/message.go +++ b/internal/message/message.go @@ -72,27 +72,53 @@ func Code(s string) string { return "" + esc(s) + "" } // The full SHA is shown once per question, on the "Full:" line. func SHA(sha string) string { return Code(Short(sha)) } +// OutputLine is the most characters of one line of command output in a +// message. A longer line keeps its start and its end, with "…" between: +// most tools print the reason last (code6x/pullgate#25). +const OutputLine = 300 + +// OutputMax is the most characters of command output in one message. +const OutputMax = 1500 + // Output is the way to put the output of a command into a message: its -// last n lines, each cleaned and cut, in a
 block. It is "" for empty
-// output.
+// last lines (at most n, and at most OutputMax characters), each cleaned
+// and cut in the middle, in a 
 block. It is "" for empty output.
 func Output(out string, n int) string {
 	lines := strings.Split(strings.TrimRight(out, "\n"), "\n")
-	if len(lines) > n {
-		lines = lines[len(lines)-n:]
-	}
-	for i, l := range lines {
-		if r := []rune(l); len(r) > 100 {
-			l = string(r[:100]) + "…"
+	var keep []string
+	used := 0
+	for i := len(lines) - 1; i >= 0 && len(keep) < n; i-- {
+		l := cutMiddle(lines[i], OutputLine)
+		r := Len(l) + 1 // UTF-16 units, as Telegram counts
+		if used+r > OutputMax && len(keep) > 0 {
+			break
 		}
-		lines[i] = esc(l)
+		used += r
+		keep = append([]string{html.EscapeString(l)}, keep...)
 	}
-	s := strings.Join(lines, "\n")
+	s := strings.Join(keep, "\n")
 	if strings.TrimSpace(s) == "" {
 		return ""
 	}
 	return "
" + s + "
" } +// cutMiddle cleans one line like clean, but a line longer than max keeps +// its first max/4 and its last characters, with "…" between. +func cutMiddle(s string, max int) string { + r := []rune(s) + for i, c := range r { + if c == utf8.RuneError || unicode.IsControl(c) || isInvisible(c) { + r[i] = '?' + } + } + if len(r) > max { + head := max / 4 + r = append(append(r[:head:head], '…'), r[len(r)-(max-head-1):]...) + } + return string(r) +} + // Len is the length of s in UTF-16 units, as Telegram counts. func Len(s string) int { return len(utf16.Encode([]rune(s))) } diff --git a/internal/message/message_test.go b/internal/message/message_test.go index baf508f..ad0ae1f 100644 --- a/internal/message/message_test.go +++ b/internal/message/message_test.go @@ -5,6 +5,7 @@ import ( "regexp" "strings" "testing" + "unicode/utf8" ) func TestClean(t *testing.T) { @@ -244,3 +245,23 @@ func TestShortSHAsAndOneFullSHA(t *testing.T) { t.Fatalf("too long: %d", Len(text)) } } + +// TestOutputKeepsTheEnd: command output keeps its last lines, and a long +// line keeps its start and its end (code6x/pullgate#25). +func TestOutputKeepsTheEnd(t *testing.T) { + if Output("a\nb", 0) != "" || Output("\n\n", 10) != "" { + t.Error("no lines, no block") + } + long := "start " + strings.Repeat("ж", 1000) + " the \"reason\"\x07" + got := Output(strings.Repeat("noise\n", 50)+long, 10) + if !strings.HasSuffix(got, " the "reason"?
") || !strings.Contains(got, "start жж") || !strings.Contains(got, "…") { + t.Fatalf("the long line must keep its start and end, cleaned and escaped: %q", got) + } + if !utf8.ValidString(got) || strings.Count(got, "noise") != 9 { + t.Fatalf("want valid text and the last 10 lines: %q", got) + } + big := Output(strings.Repeat(strings.Repeat("x", 299)+"\n", 20)+"last", 20) + if !strings.HasSuffix(big, "\nlast
") || utf8.RuneCountInString(big) > OutputMax+20 { + t.Fatalf("output over OutputMax keeps its last line and stays short: %d", utf8.RuneCountInString(big)) + } +}