Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.
JavaScript agent-orchestrationagentic-workflowai-agentsai-codingclaude-codecode-reviewcodebuddycodexcoding-agentscursordeveloper-toolshuman-on-the-loopllmmcpmulti-agentsdlcself-hostedsopworkflow-automationworkflow-engine
refactor(procwatch): extract stop classification and failure message from Run (#270)
fx-kurumi5c07611e521a
diff --git a/internal/procwatch/procwatch.go b/internal/procwatch/procwatch.goindex 7deeffb7..44cc6004 100644--- a/internal/procwatch/procwatch.go+++ b/internal/procwatch/procwatch.go@@ -125,21 +125,35 @@ func Run(ctx context.Context, build func(context.Context) *exec.Cmd, opts Optionemit(Completed, fmt.Sprintf("%s 完成,耗时 %s", opts.Label, roundDuration(elapsed)), elapsed)return tail, nil}- switch cause := context.Cause(runCtx); {+ err = stopError(context.Cause(runCtx), opts, elapsed, err)+ emit(Failed, failureMessage(opts.Label, elapsed, err, tail), elapsed)+ return tail, err+}++// stopError wraps err in a StoppedError when cause shows Run's watchdog, its+// own timeout or the caller's deadline stopped the command; any other failure+// is returned unchanged.+func stopError(cause error, opts Options, elapsed time.Duration, err error) error {+ switch {case errors.Is(cause, errStalled):- err = &StoppedError{fmt.Sprintf("%s 已 %s 没有任何输出,判定卡住并停止%s", opts.Label, roundDuration(opts.Stall), settingHint(opts.StallEnv)), err}+ return &StoppedError{fmt.Sprintf("%s 已 %s 没有任何输出,判定卡住并停止%s", opts.Label, roundDuration(opts.Stall), settingHint(opts.StallEnv)), err}case errors.Is(cause, errTimeout):- err = &StoppedError{fmt.Sprintf("%s 超过 %s 仍未完成,已停止%s", opts.Label, roundDuration(opts.Timeout), settingHint(opts.TimeoutEnv)), err}+ return &StoppedError{fmt.Sprintf("%s 超过 %s 仍未完成,已停止%s", opts.Label, roundDuration(opts.Timeout), settingHint(opts.TimeoutEnv)), err}case errors.Is(cause, context.DeadlineExceeded):// The caller's own limit (an assistant turn, a preparation request).- err = &StoppedError{fmt.Sprintf("%s 在调用方时限内没有完成(已运行 %s),已停止", opts.Label, roundDuration(elapsed)), err}+ return &StoppedError{fmt.Sprintf("%s 在调用方时限内没有完成(已运行 %s),已停止", opts.Label, roundDuration(elapsed)), err}}- message := fmt.Sprintf("%s 失败(耗时 %s):%v", opts.Label, roundDuration(elapsed), err)+ return err+}++// failureMessage is the Failed event text: the error plus the last few lines+// of output.+func failureMessage(label string, elapsed time.Duration, err error, tail string) string {+ message := fmt.Sprintf("%s 失败(耗时 %s):%v", label, roundDuration(elapsed), err)if last := lastLines(tail, 3); last != "" {message += "\n" + last}- emit(Failed, message, elapsed)- return tail, err+ return message}var (diff --git a/internal/procwatch/stop_error_test.go b/internal/procwatch/stop_error_test.gonew file mode 100644index 00000000..4034ca46--- /dev/null+++ b/internal/procwatch/stop_error_test.go@@ -0,0 +1,50 @@+package procwatch++import (+ "context"+ "errors"+ "testing"+ "time"+)++func TestStopErrorClassifiesCause(t *testing.T) {+ base := errors.New("signal: killed")+ opts := Options{Label: "克隆仓库 web", Stall: 90 * time.Second, StallEnv: "STALL_ENV", Timeout: 10 * time.Minute}+ cases := []struct {+ name string+ cause error+ stopped bool+ want string+ }{+ {"stalled", errStalled, true, "克隆仓库 web 已 1m30s 没有任何输出,判定卡住并停止(可用 STALL_ENV 调整): signal: killed"},+ {"timeout", errTimeout, true, "克隆仓库 web 超过 10m0s 仍未完成,已停止: signal: killed"},+ {"caller deadline", context.DeadlineExceeded, true, "克隆仓库 web 在调用方时限内没有完成(已运行 3s),已停止: signal: killed"},+ {"plain exit", nil, false, "signal: killed"},+ {"caller canceled", context.Canceled, false, "signal: killed"},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ got := stopError(tc.cause, opts, 3*time.Second, base)+ if Stopped(got) != tc.stopped {+ t.Fatalf("Stopped = %v, want %v (%v)", Stopped(got), tc.stopped, got)+ }+ if !errors.Is(got, base) {+ t.Fatalf("stopError lost the original error: %v", got)+ }+ if got.Error() != tc.want {+ t.Fatalf("error = %q, want %q", got.Error(), tc.want)+ }+ })+ }+}++func TestFailureMessageAppendsLastLines(t *testing.T) {+ err := errors.New("exit status 128")+ if got, want := failureMessage("构建镜像", 2*time.Second, err, ""), "构建镜像 失败(耗时 2s):exit status 128"; got != want {+ t.Fatalf("no tail = %q, want %q", got, want)+ }+ got := failureMessage("构建镜像", 2*time.Second, err, "a\nb\nc\nd")+ if want := "构建镜像 失败(耗时 2s):exit status 128\nb\nc\nd"; got != want {+ t.Fatalf("with tail = %q, want %q", got, want)+ }+}