mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-02 18:19:07 +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.
87 lines
2.5 KiB
Go
87 lines
2.5 KiB
Go
package dockeragent
|
|
|
|
import (
|
|
"strings"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// BoundFailureLogLines applies the failure-log bounds to already masked
|
|
// lines: control characters removed, each line and the whole capped, blank
|
|
// lines dropped and the newest lines kept. The agent applies it before
|
|
// sending and the server applies it again on receipt, so neither depends on
|
|
// the other having done so.
|
|
func BoundFailureLogLines(in []string) (lines []string, truncated bool) {
|
|
for _, line := range in {
|
|
line = strings.TrimRight(cleanFailureLogLine(strings.ToValidUTF8(line, "")), " ")
|
|
if strings.TrimSpace(line) == "" {
|
|
continue
|
|
}
|
|
if len(line) > FailureLogMaxLineBytes {
|
|
line = truncateUTF8(line, FailureLogMaxLineBytes-3) + "..."
|
|
truncated = true
|
|
}
|
|
lines = append(lines, line)
|
|
}
|
|
if len(lines) > FailureLogMaxLines {
|
|
lines = lines[len(lines)-FailureLogMaxLines:]
|
|
truncated = true
|
|
}
|
|
total := 0
|
|
for i := len(lines) - 1; i >= 0; i-- {
|
|
total += len(lines[i]) + 1
|
|
if total > FailureLogMaxBytes {
|
|
lines = lines[i+1:]
|
|
truncated = true
|
|
break
|
|
}
|
|
}
|
|
return lines, truncated
|
|
}
|
|
|
|
// CleanFailureLogText removes what is not text from raw log output and keeps
|
|
// its line breaks: invalid UTF-8, control characters, and anything a carriage
|
|
// return overwrote. It has to run before masking, never after: a secret split
|
|
// by a control character matches no masking rule until the character is gone,
|
|
// and removing it afterwards would rebuild the secret unmasked.
|
|
func CleanFailureLogText(raw string) string {
|
|
raw = strings.ToValidUTF8(raw, "")
|
|
raw = strings.ReplaceAll(raw, "\r\n", "\n")
|
|
lines := strings.Split(raw, "\n")
|
|
for i, line := range lines {
|
|
lines[i] = cleanFailureLogLine(line)
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
|
|
// cleanFailureLogLine drops control characters. A tab becomes a space, a line
|
|
// break inside a line becomes a space, and a carriage return ends the visible
|
|
// text before it, as a terminal would show a progress line.
|
|
func cleanFailureLogLine(line string) string {
|
|
line = strings.TrimRight(line, "\r\n")
|
|
if i := strings.LastIndexByte(line, '\r'); i >= 0 {
|
|
line = line[i+1:]
|
|
}
|
|
var b strings.Builder
|
|
b.Grow(len(line))
|
|
for _, r := range line {
|
|
switch {
|
|
case r == '\t' || r == '\n':
|
|
b.WriteByte(' ')
|
|
case r < 0x20 || r == 0x7f || (r >= 0x80 && r < 0xa0):
|
|
default:
|
|
b.WriteRune(r)
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func truncateUTF8(s string, max int) string {
|
|
if len(s) <= max {
|
|
return s
|
|
}
|
|
for max > 0 && !utf8.RuneStart(s[max]) {
|
|
max--
|
|
}
|
|
return s[:max]
|
|
}
|