package display import ( "fmt" "io" "runtime/debug" "strings" "sync" ) const ( // DiagTag prefixes every diagnostic line so the journal can be // filtered to the stream with a plain grep. DiagTag = "display_diag" // DiagCap bounds the unique diagnostic records a process emits: the // stream is small by construction, and only the close summary passes // once the cap is reached. DiagCap = 64 // DiagCollectCommand is the documented collection command: current // boot, only diagnostic lines, capped output. DiagCollectCommand = "journalctl -u groundstation -b --no-pager | grep " + DiagTag + " | tail -60" ) // Diag emits compact, stable key=value diagnostics into the systemd // journal stream (standard output of the display process). The schema is a // fixed whitelist — process metadata, role and window identifiers, // geometry and shape extents, state enums, stack position, bounded // counters and operation errors — so diagnostics can never carry RC // channel values, telemetry payloads, file contents, environment or // credentials. Records are state-transition-driven: identical consecutive // transitions are deduplicated, the total number of records is capped, // and only the close summary bypasses the cap. type Diag struct { mu sync.Mutex out io.Writer cap int last map[string]string count int } // NewDiag builds a diagnostics emitter writing to out. func NewDiag(out io.Writer) *Diag { return &Diag{out: out, cap: DiagCap, last: map[string]string{}} } // FormatDiagLine renders one diagnostic record in the exact stable schema. func FormatDiagLine(kind, role string, fields ...string) string { b := &strings.Builder{} b.WriteString(DiagTag) b.WriteString(" kind=") b.WriteString(kind) if role != "" { b.WriteString(" role=") b.WriteString(role) } for _, field := range fields { if field != "" { b.WriteString(" ") b.WriteString(field) } } return b.String() } // Emit records a state transition or event; identical consecutive records // are deduplicated and the total is capped. func (d *Diag) Emit(kind, role string, fields ...string) { d.emit(kind, role, false, fields...) } // EmitForced records a record that must never be dropped by the cap, such // as the close summary. func (d *Diag) EmitForced(kind, role string, fields ...string) { d.emit(kind, role, true, fields...) } func (d *Diag) emit(kind, role string, forced bool, fields ...string) { if d == nil { return } line := FormatDiagLine(kind, role, fields...) d.mu.Lock() defer d.mu.Unlock() if !forced { if d.count >= d.cap { return } key := kind + "|" + role if d.last[key] == line { return } d.last[key] = line } d.count++ if d.out != nil { fmt.Fprintln(d.out, line) } } // diagValue sanitizes a free-text diagnostic value: line breaks are // collapsed so a hostile or multiline error can never inject journal lines // into the display_diag stream, and the value is length-bounded. Values // without free text (identifiers, dimensions, enums) bypass this and are // formatted from trusted numeric sources only. func diagValue(text string) string { if text == "" { return text } if len(text) > diagValueMaxLen { text = text[:diagValueMaxLen] } return strings.ReplaceAll(strings.ReplaceAll(text, "\n", " "), "\r", " ") } // diagValueMaxLen bounds free-text diagnostic values so a chatty error can // never bloat the journal stream. const diagValueMaxLen = 160 // VCSRevision returns the VCS revision the binary was built from, or // unknown when the build carried none. No other build or environment // metadata is exposed. func VCSRevision() string { if info, ok := debug.ReadBuildInfo(); ok { for _, setting := range info.Settings { if setting.Key == "vcs.revision" && setting.Value != "" { return setting.Value } } } return "unknown" }