Skip to content

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.

refactor(domain): extract analyzeDiagnosticTools from AnalyzeAssistantDiagnostics (#527) (#528)

fx-kurumic455800bf966
diff --git a/internal/domain/assistant_diagnostic_analysis.go b/internal/domain/assistant_diagnostic_analysis.go
index 67832e9e..96328a3a 100644
--- a/internal/domain/assistant_diagnostic_analysis.go
+++ b/internal/domain/assistant_diagnostic_analysis.go
@@ -20,33 +20,7 @@ func AnalyzeAssistantDiagnostics(d *AssistantDiagnostics) {
}
requestIDs := map[string]bool{}
traceIDs := map[string]bool{}
- toolFailures, pending := 0, 0
- firstFailedTool := ""
- for i := range d.Tools {
- t := &d.Tools[i]
- if t.Status == "failed" {
- toolFailures++
- if firstFailedTool == "" {
- firstFailedTool = fmt.Sprintf("%s(来源:%s)", t.Name, t.Source)
- }
- }
- if t.Status == "running" || t.Status == "unfinished" {
- if d.Status != "running" {
- t.Status = "unfinished"
- }
- pending++
- a.Evidence = append(a.Evidence, fmt.Sprintf("工具 %s(%s,来源:%s)有开始记录,未收到结束记录", t.Name, t.ID, t.Source))
- }
- if t.RequestID != "" {
- requestIDs[t.RequestID] = true
- }
- if t.TraceID != "" {
- traceIDs[t.TraceID] = true
- }
- }
- if toolFailures > 0 {
- a.Evidence = append(a.Evidence, fmt.Sprintf("记录到 %d 次工具失败(首个:%s);工具错误本身不直接等同于整轮生成失败", toolFailures, firstFailedTool))
- }
+ pending := analyzeDiagnosticTools(d, a, requestIDs, traceIDs)
hasEarlyError := false
for i := range d.Attempts {
attempt := &d.Attempts[i]
@@ -162,6 +136,40 @@ func AnalyzeAssistantDiagnostics(d *AssistantDiagnostics) {
}
}
+// analyzeDiagnosticTools records tool failure and unfinished-tool evidence,
+// collects tool request/trace IDs and returns the number of pending tools.
+// Tools still running after the generation stopped are marked unfinished.
+func analyzeDiagnosticTools(d *AssistantDiagnostics, a *AssistantDiagnosticAssessment, requestIDs, traceIDs map[string]bool) int {
+ toolFailures, pending := 0, 0
+ firstFailedTool := ""
+ for i := range d.Tools {
+ t := &d.Tools[i]
+ if t.Status == "failed" {
+ toolFailures++
+ if firstFailedTool == "" {
+ firstFailedTool = fmt.Sprintf("%s(来源:%s)", t.Name, t.Source)
+ }
+ }
+ if t.Status == "running" || t.Status == "unfinished" {
+ if d.Status != "running" {
+ t.Status = "unfinished"
+ }
+ pending++
+ a.Evidence = append(a.Evidence, fmt.Sprintf("工具 %s(%s,来源:%s)有开始记录,未收到结束记录", t.Name, t.ID, t.Source))
+ }
+ if t.RequestID != "" {
+ requestIDs[t.RequestID] = true
+ }
+ if t.TraceID != "" {
+ traceIDs[t.TraceID] = true
+ }
+ }
+ if toolFailures > 0 {
+ a.Evidence = append(a.Evidence, fmt.Sprintf("记录到 %d 次工具失败(首个:%s);工具错误本身不直接等同于整轮生成失败", toolFailures, firstFailedTool))
+ }
+ return pending
+}
+
func appendUniqueDiagnostic(items []string, value string) []string {
for _, item := range items {
if strings.EqualFold(item, value) {
diff --git a/internal/domain/assistant_diagnostic_tools_test.go b/internal/domain/assistant_diagnostic_tools_test.go
new file mode 100644
index 00000000..56e23c1e
--- /dev/null
+++ b/internal/domain/assistant_diagnostic_tools_test.go
@@ -0,0 +1,63 @@
+package domain
+
+import (
+ "reflect"
+ "testing"
+)
+
+func TestAnalyzeDiagnosticToolsCollectsEvidenceAndIDs(t *testing.T) {
+ d := &AssistantDiagnostics{Status: "failed", Tools: []AssistantDiagnosticTool{
+ {ID: "t1", Name: "Bash", Source: "cli", Status: "failed", RequestID: "req-1"},
+ {ID: "t2", Name: "Read", Source: "mcp", Status: "running", TraceID: "trace-1"},
+ {ID: "t3", Name: "Edit", Source: "cli", Status: "failed"},
+ {ID: "t4", Name: "Grep", Source: "cli", Status: "completed", RequestID: "req-2"},
+ }}
+ a := &AssistantDiagnosticAssessment{}
+ requestIDs, traceIDs := map[string]bool{}, map[string]bool{}
+
+ pending := analyzeDiagnosticTools(d, a, requestIDs, traceIDs)
+
+ if pending != 1 {
+ t.Fatalf("pending = %d, want 1", pending)
+ }
+ if d.Tools[1].Status != "unfinished" {
+ t.Fatalf("running tool after generation stopped = %q, want unfinished", d.Tools[1].Status)
+ }
+ wantEvidence := []string{
+ "工具 Read(t2,来源:mcp)有开始记录,未收到结束记录",
+ "记录到 2 次工具失败(首个:Bash(来源:cli));工具错误本身不直接等同于整轮生成失败",
+ }
+ if !reflect.DeepEqual(a.Evidence, wantEvidence) {
+ t.Fatalf("evidence = %#v, want %#v", a.Evidence, wantEvidence)
+ }
+ if !reflect.DeepEqual(requestIDs, map[string]bool{"req-1": true, "req-2": true}) {
+ t.Fatalf("requestIDs = %#v", requestIDs)
+ }
+ if !reflect.DeepEqual(traceIDs, map[string]bool{"trace-1": true}) {
+ t.Fatalf("traceIDs = %#v", traceIDs)
+ }
+}
+
+func TestAnalyzeDiagnosticToolsKeepsRunningToolWhileGenerationRuns(t *testing.T) {
+ d := &AssistantDiagnostics{Status: "running", Tools: []AssistantDiagnosticTool{
+ {ID: "t1", Name: "Bash", Source: "cli", Status: "running"},
+ }}
+ a := &AssistantDiagnosticAssessment{}
+
+ pending := analyzeDiagnosticTools(d, a, map[string]bool{}, map[string]bool{})
+
+ if pending != 1 || d.Tools[0].Status != "running" {
+ t.Fatalf("pending = %d status = %q, want 1 running", pending, d.Tools[0].Status)
+ }
+ if len(a.Evidence) != 1 {
+ t.Fatalf("evidence = %#v, want only the unfinished-tool entry", a.Evidence)
+ }
+}
+
+func TestAnalyzeDiagnosticToolsWithoutToolsAddsNothing(t *testing.T) {
+ d := &AssistantDiagnostics{Status: "completed"}
+ a := &AssistantDiagnosticAssessment{}
+ if pending := analyzeDiagnosticTools(d, a, map[string]bool{}, map[string]bool{}); pending != 0 || len(a.Evidence) != 0 {
+ t.Fatalf("pending = %d evidence = %#v, want none", pending, a.Evidence)
+ }
+}