From b2e3f416bc51ad8b54a122e2326770500f6d76b5 Mon Sep 17 00:00:00 2001 From: "swe-agent[bot]" <0+swe-agent[bot]@users.noreply.github.com> Date: Fri, 12 Dec 2025 14:25:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(codeagent-wrapper):=20=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E9=80=80=E5=87=BA=E6=97=B6=E6=98=BE=E7=A4=BA=E6=9C=80=E8=BF=91?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 Logger.ExtractRecentErrors() 方法提取最近 ERROR/WARN 日志 - 修改退出逻辑:失败时先输出错误再删除日志文件 Closes #56 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- codeagent-wrapper/logger.go | 30 ++++++++++++++++++++++++++++++ codeagent-wrapper/main.go | 11 ++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/codeagent-wrapper/logger.go b/codeagent-wrapper/logger.go index a4da52e..f33299e 100644 --- a/codeagent-wrapper/logger.go +++ b/codeagent-wrapper/logger.go @@ -170,6 +170,36 @@ func (l *Logger) RemoveLogFile() error { return os.Remove(l.path) } +// ExtractRecentErrors reads the log file and returns the most recent ERROR and WARN entries. +// Returns up to maxEntries entries in chronological order. +func (l *Logger) ExtractRecentErrors(maxEntries int) []string { + if l == nil || l.path == "" { + return nil + } + + f, err := os.Open(l.path) + if err != nil { + return nil + } + defer f.Close() + + var entries []string + scanner := bufio.NewScanner(f) + for scanner.Scan() { + line := scanner.Text() + if strings.Contains(line, "] ERROR:") || strings.Contains(line, "] WARN:") { + entries = append(entries, line) + } + } + + // Keep only the last maxEntries + if len(entries) > maxEntries { + entries = entries[len(entries)-maxEntries:] + } + + return entries +} + // Flush waits for all pending log entries to be written. Primarily for tests. // Returns after a 5-second timeout to prevent indefinite blocking. func (l *Logger) Flush() { diff --git a/codeagent-wrapper/main.go b/codeagent-wrapper/main.go index e81f333..814250f 100644 --- a/codeagent-wrapper/main.go +++ b/codeagent-wrapper/main.go @@ -141,8 +141,17 @@ func run() (exitCode int) { if err := closeLogger(); err != nil { fmt.Fprintf(os.Stderr, "ERROR: failed to close logger: %v\n", err) } - // Always remove log file after completion + // On failure, extract and display recent errors before removing log if logger != nil { + if exitCode != 0 { + if errors := logger.ExtractRecentErrors(10); len(errors) > 0 { + fmt.Fprintln(os.Stderr, "\n=== Recent Errors ===") + for _, entry := range errors { + fmt.Fprintln(os.Stderr, entry) + } + fmt.Fprintf(os.Stderr, "Log file: %s (deleted)\n", logger.Path()) + } + } if err := logger.RemoveLogFile(); err != nil && !os.IsNotExist(err) { // Silently ignore removal errors }