From 9e69e3098705f701ebfb11e4b1c878a3a99eed95 Mon Sep 17 00:00:00 2001 From: pemila Date: Tue, 17 Nov 2026 17:02:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(runner):=20=E9=A2=84=E6=A3=80=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E4=B8=8D=E5=86=8D=E4=B8=8A=E6=8A=A5=20exit=200?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=20"=E2=80=A6=20(exit=200)"=20?= =?UTF-8?q?=E8=AF=AF=E5=AF=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 执行机器在任何进程启动之前就可能失败(CLI 未安装、模型未指定、凭据不可用)。 这些 pre-flight 错误一律上报 exitCode=0,控制面拼成「Pi 需要指定模型 (exit 0)」, 看日志的人会去找一个根本不存在的崩溃进程。 - execResult 增加 ran:只有真正 spawn 出进程(或 docker exec 成功执行)才置位。 - 新增 processRan(err):nil 或 *exec.ExitError 才算跑过,exec 找不到 / 权限不足 / 启动中断都不算。 - daemon 里抽出 reportedExitCode:成功照旧上报;失败只在 ran 时上报,pre-flight 失败不再带退出码。 - align / integrate / work_branch 这几个 helper 本身就会 spawn git 并以 exitCode=1 表示失败,显式置 ran,避免它们的退出码被新规则一起吞掉。 - 新增测试覆盖三种情形。 --- internal/runner/align.go | 4 ++- internal/runner/daemon.go | 17 ++++++--- internal/runner/exec.go | 23 +++++++++++-- internal/runner/exit_code_report_test.go | 44 ++++++++++++++++++++++++ internal/runner/integrate.go | 3 +- internal/runner/work_branch.go | 3 +- 6 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 internal/runner/exit_code_report_test.go diff --git a/internal/runner/align.go b/internal/runner/align.go index 5c2522b1d..f91209c6d 100644 --- a/internal/runner/align.go +++ b/internal/runner/align.go @@ -23,7 +23,9 @@ func (d *Daemon) executeAlignBaseline(ctx context.Context, task domain.RunnerTas defer os.RemoveAll(dir) result, err := alignBaseline(ctx, dir, task.Invocation) if err != nil { - return execResult{status: domain.RunnerTaskFailed, exitCode: 1, errMsg: err.Error()} + // alignBaseline shells out to git, so the helper did run: keep the exit + // status instead of letting reportedExitCode drop it as a pre-flight miss. + return execResult{status: domain.RunnerTaskFailed, exitCode: 1, ran: true, errMsg: err.Error()} } body, _ := json.Marshal(result) return execResult{status: domain.RunnerTaskSuccess, exitCode: 0, result: string(body)} diff --git a/internal/runner/daemon.go b/internal/runner/daemon.go index 3b70b3520..6353b76d9 100644 --- a/internal/runner/daemon.go +++ b/internal/runner/daemon.go @@ -613,16 +613,25 @@ func (d *Daemon) executeTask(ctx context.Context, task domain.RunnerTask) { completeCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() req := CompleteRequest{Status: result.status, Result: result.result, Error: result.errMsg, Artifacts: artifacts, Outputs: result.outputs} - if result.status == domain.RunnerTaskFailed || result.status == domain.RunnerTaskSuccess { - exit := result.exitCode - req.ExitCode = &exit - } + req.ExitCode = reportedExitCode(result) if err := d.client.Complete(completeCtx, task.ID, req); err != nil { log.Printf("task %s complete report: %v", task.ID, err) } log.Printf("task %s finished (%s)", task.ID, result.status) } +// reportedExitCode is the exit status the control plane should show. A task that +// never spawned its executor has none: reporting the zero value made every +// pre-flight failure read "… (exit 0)" next to the real error, which sent +// operators looking for a crashed process instead of a missing CLI or model. +func reportedExitCode(result execResult) *int { + if result.status == domain.RunnerTaskSuccess || (result.status == domain.RunnerTaskFailed && result.ran) { + code := result.exitCode + return &code + } + return nil +} + func (d *Daemon) executeRunCleanup(ctx context.Context, task domain.RunnerTask) execResult { if err := d.client.StartTask(ctx, task.ID); err != nil { return execResult{status: domain.RunnerTaskFailed, errMsg: fmt.Sprintf("start cleanup: %v", err)} diff --git a/internal/runner/exec.go b/internal/runner/exec.go index 4cb4de710..cee295d8b 100644 --- a/internal/runner/exec.go +++ b/internal/runner/exec.go @@ -98,6 +98,7 @@ func (d *Daemon) executeSandboxProcess(ctx context.Context, task domain.RunnerTa return result } result.exitCode = exitCode(completed.err) + result.ran = processRan(completed.err) if completed.err != nil { result.status = domain.RunnerTaskFailed result.errMsg = completed.err.Error() @@ -258,9 +259,13 @@ func (b *eventBatcher) taskResult(rawFallback string) string { type execResult struct { status string exitCode int - result string - errMsg string - outputs *domain.RunnerTaskOutputs + // ran reports that the executor actually spawned. A pre-flight failure + // (missing CLI, unusable credentials, missing image) never produced a + // process, so it has no exit status to report. + ran bool + result string + errMsg string + outputs *domain.RunnerTaskOutputs } // executeProcess spawns the Agent CLI in its own process group so a cancel @@ -372,6 +377,7 @@ func (d *Daemon) executeProcess(ctx context.Context, task domain.RunnerTask, arg return result } result.exitCode = code + result.ran = processRan(err) if err != nil { result.status = domain.RunnerTaskFailed result.errMsg = err.Error() @@ -444,6 +450,17 @@ func isBenignStreamClose(err error) bool { return strings.Contains(msg, "file already closed") || strings.Contains(msg, "use of closed file") } +// processRan reports whether err came from a process that actually ran: nil for +// a clean exit, *exec.ExitError for a non-zero one. Anything else (exec not +// found, permission denied, an aborted start) never produced an exit status. +func processRan(err error) bool { + if err == nil { + return true + } + var exitErr *exec.ExitError + return asExitError(err, &exitErr) +} + func exitCode(err error) int { if err == nil { return 0 diff --git a/internal/runner/exit_code_report_test.go b/internal/runner/exit_code_report_test.go new file mode 100644 index 000000000..08b712292 --- /dev/null +++ b/internal/runner/exit_code_report_test.go @@ -0,0 +1,44 @@ +package runner + +import ( + "errors" + "os/exec" + "testing" + + "agentflow/internal/domain" +) + +// A failure before the executor spawned — a missing CLI, unusable credentials, +// an absent model — has no exit status. Reporting the zero value made every +// such error read "… (exit 0)" in the run log, which points the investigation +// at a process that never existed. +func TestReportedExitCodeSkipsPreflightFailures(t *testing.T) { + preflight := execResult{status: domain.RunnerTaskFailed, errMsg: "Pi 需要指定模型"} + if got := reportedExitCode(preflight); got != nil { + t.Fatalf("pre-flight failure reported exit %d, want no exit code", *got) + } + crashed := execResult{status: domain.RunnerTaskFailed, exitCode: 127, ran: true} + if got := reportedExitCode(crashed); got == nil || *got != 127 { + t.Fatalf("executor that ran exit code = %v, want 127", got) + } + succeeded := execResult{status: domain.RunnerTaskSuccess, exitCode: 0, ran: true} + if got := reportedExitCode(succeeded); got == nil || *got != 0 { + t.Fatalf("successful task exit code = %v, want 0", got) + } +} + +func TestProcessRan(t *testing.T) { + if !processRan(nil) { + t.Fatal("a clean exit ran") + } + exitErr := exec.Command("false").Run() + if !processRan(exitErr) { + t.Fatal("a non-zero exit still ran") + } + if code := exitCode(exitErr); code != 1 { + t.Fatalf("exitCode = %d, want 1", code) + } + if processRan(errors.New("exec: \"pi\": executable file not found in $PATH")) { + t.Fatal("a spawn failure never ran") + } +} diff --git a/internal/runner/integrate.go b/internal/runner/integrate.go index fbce8d6f1..5d8e74df0 100644 --- a/internal/runner/integrate.go +++ b/internal/runner/integrate.go @@ -34,7 +34,8 @@ func (d *Daemon) executeIntegrate(ctx context.Context, task domain.RunnerTask) e _ = d.client.SendEvents(ctx, task.ID, lines) } if err != nil { - return execResult{status: domain.RunnerTaskFailed, exitCode: 1, errMsg: err.Error()} + // integrateTaskRepositories shells out to git, so the helper did run. + return execResult{status: domain.RunnerTaskFailed, exitCode: 1, ran: true, errMsg: err.Error()} } body, _ := json.Marshal(results) return execResult{status: domain.RunnerTaskSuccess, exitCode: 0, result: string(body)} diff --git a/internal/runner/work_branch.go b/internal/runner/work_branch.go index 6fe112629..ce0dcc614 100644 --- a/internal/runner/work_branch.go +++ b/internal/runner/work_branch.go @@ -42,7 +42,8 @@ func (d *Daemon) executeWorkBranchSync(ctx context.Context, task domain.RunnerTa _ = d.client.SendEvents(ctx, task.ID, lines) } if err != nil { - return execResult{status: domain.RunnerTaskFailed, exitCode: 1, errMsg: err.Error()} + // The work-branch sync shells out to git, so the helper did run. + return execResult{status: domain.RunnerTaskFailed, exitCode: 1, ran: true, errMsg: err.Error()} } body, _ := json.Marshal(heads) return execResult{status: domain.RunnerTaskSuccess, exitCode: 0, result: string(body)}