mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-02 10:09:02 +01:00
On a default install Pulse can see that a container failed and not why. The line that names the cause is in the log, and a default agent reads no logs. On the fault set, the restart loop from a bad DATABASE_URL stayed "Handed back" on a default agent through every replay for that reason. An operator can now turn on --enable-failure-logs (or PULSE_ENABLE_FAILURE_LOGS) on a host. The agent then reports the last lines of a container whose own state shows a failure: a non-zero exit, an out-of-memory kill, a restart after either, or a failing health check. It never reads a container that stopped cleanly, runs healthy or was never started, and with the switch off it never asks the runtime for logs at all. Nothing the server sends can turn the switch on: this is a local boundary like custom sensors, not a read-only command mode, and the server never chooses a container, a path or a line count. What leaves the host is at most 30 lines and 4 KB, read once per failure, with terminal codes and control characters removed and then credential-shaped values masked. Cleaning comes before masking because a secret split by a control character matches no rule until the character is gone. The server cleans, masks and bounds the lines again on receipt and accepts only the four known reasons, so it does not depend on the agent having done any of it. The masking rules moved from the AI safety package to pkg/secretredact, with no change to them, so the agent and the server apply one set. The lines show in the container's details as "Last log lines", in view on the overview, as plain text. Patrol and the Assistant get them on the single-container payload with a fixed note that they are untrusted text and never instructions. Masking is pattern-based and is not a guarantee. The lines are readable by anyone who can read the container, go to the AI provider when Patrol reads it, and can be quoted by Patrol in a finding. The docs say so. A read-only review before this was pushed found that masking ran before cleaning, that a restarted healthy container qualified for a read, that the stream reader trusted a frame's declared length and kept the oldest text, and that stale lines could be served for a new failure. All are fixed here with tests. It also found that the first docs promised the lines never reach a notification, which a quoted line in a finding can. The promise is corrected.
83 lines
3.0 KiB
Go
83 lines
3.0 KiB
Go
package dockeragent
|
||
|
||
import (
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
func TestBoundFailureLogLinesKeepsTheNewestWithinEveryBound(t *testing.T) {
|
||
// Control characters, blank lines and a progress line rewritten in place.
|
||
lines, truncated := BoundFailureLogLines([]string{
|
||
"starting\twith\ttabs",
|
||
" ",
|
||
"downloading 10%\rdownloading 100%",
|
||
"bell\x07 and escape\x1b gone",
|
||
"",
|
||
})
|
||
want := []string{"starting with tabs", "downloading 100%", "bell and escape gone"}
|
||
if truncated || strings.Join(lines, "|") != strings.Join(want, "|") {
|
||
t.Fatalf("lines = %q truncated=%t, want %q", lines, truncated, want)
|
||
}
|
||
|
||
// More lines than allowed: the newest stay.
|
||
var many []string
|
||
for i := 0; i < FailureLogMaxLines+12; i++ {
|
||
many = append(many, "line "+string(rune('a'+i%26)))
|
||
}
|
||
lines, truncated = BoundFailureLogLines(many)
|
||
if !truncated || len(lines) != FailureLogMaxLines || lines[len(lines)-1] != many[len(many)-1] || lines[0] != many[12] {
|
||
t.Fatalf("kept %d lines (truncated=%t), first %q last %q", len(lines), truncated, lines[0], lines[len(lines)-1])
|
||
}
|
||
|
||
// One line longer than allowed is cut on a character boundary.
|
||
long := strings.Repeat("é", FailureLogMaxLineBytes)
|
||
lines, truncated = BoundFailureLogLines([]string{long})
|
||
if !truncated || len(lines) != 1 || len(lines[0]) > FailureLogMaxLineBytes || !strings.HasSuffix(lines[0], "...") || !strings.HasPrefix(lines[0], "é") {
|
||
t.Fatalf("long line: %d bytes, truncated=%t", len(lines[0]), truncated)
|
||
}
|
||
if strings.ContainsRune(lines[0], '<27>') {
|
||
t.Fatal("the cut split a character")
|
||
}
|
||
|
||
// The whole stays under the total, dropping the oldest lines.
|
||
var heavy []string
|
||
for i := 0; i < 20; i++ {
|
||
heavy = append(heavy, strings.Repeat(string(rune('a'+i)), 400))
|
||
}
|
||
lines, truncated = BoundFailureLogLines(heavy)
|
||
total := 0
|
||
for _, line := range lines {
|
||
total += len(line) + 1
|
||
}
|
||
if !truncated || total > FailureLogMaxBytes || len(lines) == 0 || lines[len(lines)-1] != heavy[len(heavy)-1] {
|
||
t.Fatalf("total = %d bytes in %d lines, truncated=%t", total, len(lines), truncated)
|
||
}
|
||
|
||
if lines, truncated := BoundFailureLogLines(nil); len(lines) != 0 || truncated {
|
||
t.Fatalf("no input gave %q, %t", lines, truncated)
|
||
}
|
||
}
|
||
|
||
func TestValidFailureLogReason(t *testing.T) {
|
||
for _, reason := range []string{FailureLogReasonExited, FailureLogReasonOOMKilled, FailureLogReasonRestarting, FailureLogReasonUnhealthy} {
|
||
if !ValidFailureLogReason(reason) {
|
||
t.Errorf("%q should be valid", reason)
|
||
}
|
||
}
|
||
for _, reason := range []string{"", "running", "Exited", "exited; rm -rf"} {
|
||
if ValidFailureLogReason(reason) {
|
||
t.Errorf("%q should not be valid", reason)
|
||
}
|
||
}
|
||
}
|
||
|
||
// Cleaning keeps line breaks and removes what is not text, so that masking
|
||
// sees each line as it will be sent.
|
||
func TestCleanFailureLogTextKeepsLinesAndDropsControls(t *testing.T) {
|
||
got := CleanFailureLogText("one\r\ntwo\x00 split\nprogress 10%\rprogress 100%\nbad \xff utf8\n")
|
||
want := "one\ntwo split\nprogress 100%\nbad utf8\n"
|
||
if got != want {
|
||
t.Fatalf("cleaned = %q, want %q", got, want)
|
||
}
|
||
}
|