From 6f1c8bf7a36eb7044ed7aad30f84664cbbf0d303 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 17 Mar 2026 11:28:19 +0200 Subject: Fix bugs, remove duplication, and clean up code quality issues - Log swallowed JSON unmarshal errors in stats and LSP handlers - Fix debug log file handle leak in tmuxedit (return closer from initDebugLog) - Check f.Close() errors on write paths in promptstore and tmuxedit - Fix cacheGet TOCTOU race by using single write lock - Fix readInput to use passed stdin reader instead of os.Stdin.Stat() - Remove 45 'moved to' comment tombstones from lsp/handlers.go - Deduplicate canonicalProvider wrappers (use llmutils.CanonicalProvider directly) - Remove SetWindow side effect from stats.TakeSnapshot (pure read now) - Move duplicated splitLines to textutil.SplitLinesBytes - Collapse StatusSink.SetGlobal 10 params into GlobalStatus struct - Simplify LRU touchLocked to in-place delete-and-append Co-Authored-By: Claude Opus 4.6 (1M context) --- internal/tmuxedit/history.go | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) (limited to 'internal/tmuxedit/history.go') diff --git a/internal/tmuxedit/history.go b/internal/tmuxedit/history.go index eab4db2..eac3114 100644 --- a/internal/tmuxedit/history.go +++ b/internal/tmuxedit/history.go @@ -9,6 +9,7 @@ import ( "time" "codeberg.org/snonux/hexai/internal/appconfig" + "codeberg.org/snonux/hexai/internal/textutil" ) // HistoryEntry represents a single submission to the AI agent via tmux popup. @@ -52,14 +53,15 @@ func AppendHistory(text, agent, cwd string) error { if err != nil { return fmt.Errorf("cannot open history file: %w", err) } - defer f.Close() + defer func() { _ = f.Close() }() // best-effort on error paths // Write entry if _, err := f.Write(data); err != nil { return fmt.Errorf("cannot write history entry: %w", err) } - return nil + // Check Close error to catch deferred-write failures (e.g. disk full). + return f.Close() } // GetHistory retrieves the most recent history entries (up to limit). @@ -84,7 +86,7 @@ func GetHistory(limit int) ([]HistoryEntry, error) { // Parse JSONL line by line var entries []HistoryEntry - lines := splitLines(data) + lines := textutil.SplitLinesBytes(data) for i, line := range lines { if len(line) == 0 { continue // Skip empty lines @@ -107,25 +109,3 @@ func GetHistory(limit int) ([]HistoryEntry, error) { return entries, nil } - -// splitLines splits data into lines (handles both \n and \r\n) -func splitLines(data []byte) [][]byte { - var lines [][]byte - start := 0 - for i := 0; i < len(data); i++ { - if data[i] == '\n' { - // Include everything before the newline (excluding \r if present) - end := i - if end > start && data[end-1] == '\r' { - end-- - } - lines = append(lines, data[start:end]) - start = i + 1 - } - } - // Handle last line if it doesn't end with newline - if start < len(data) { - lines = append(lines, data[start:]) - } - return lines -} -- cgit v1.2.3