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.

【架构优化】从 buildLoopRetro 中拆出遥测信号统计 collectLoopRetroSignals (#529) (#530)

fx-kurumi4c077fa550ee
diff --git a/internal/engine/loop_retro_signals_test.go b/internal/engine/loop_retro_signals_test.go
new file mode 100644
index 00000000..3460ce62
--- /dev/null
+++ b/internal/engine/loop_retro_signals_test.go
@@ -0,0 +1,60 @@
+package engine
+
+import (
+ "errors"
+ "reflect"
+ "strings"
+ "testing"
+
+ "agentflow/internal/domain"
+)
+
+func loopRetroSignalRun() domain.Run {
+ executor := domain.TelemetrySource{Layer: "executor"}
+ return domain.Run{Telemetry: []domain.TelemetryEvent{
+ {NodeID: "node", Category: "process", Name: "process.finished", Source: executor, Message: "ok"},
+ {NodeID: "node", Category: "process", Name: "process.finished", Source: executor, Message: "执行失败"},
+ {NodeID: "node", Name: "agent.loop", Message: "第 1 轮"},
+ {NodeID: "node", Category: "tool", Name: "tool.call", Phase: "finish"},
+ {NodeID: "node", Category: "tool", Name: "tool.call", Phase: "start"},
+ {NodeID: "node", Category: "verification", Name: "process.finished", Level: "error"},
+ {NodeID: "node", Name: "agent.completed"},
+ {NodeID: "node", Category: "process", Name: "process.finished", Source: executor, Message: "ok"},
+ {NodeID: "node", Category: "verification", Name: "process.finished", Level: "info"},
+ {NodeID: "node", Name: "agent.loop", Message: "第 2 轮"},
+ {NodeID: "other", Name: "agent.completed"},
+ {NodeID: "other", Name: "agent.loop", Message: "别的节点"},
+ {NodeID: "other", Category: "tool", Name: "tool.call", Phase: "finish"},
+ }}
+}
+
+func TestCollectLoopRetroSignalsCountsOnlyTheNode(t *testing.T) {
+ got := collectLoopRetroSignals(loopRetroSignalRun(), "node")
+ want := loopRetroSignals{
+ completed: 1,
+ missingResult: 1, // the executor finish after agent.completed no longer counts
+ gatePass: 1,
+ gateFail: 1,
+ loopStarts: 2,
+ toolFinishes: 1,
+ loopIssues: []string{"- Loop:第 1 轮", "- Loop:第 2 轮"},
+ }
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("signals = %+v, want %+v", got, want)
+ }
+ if empty := collectLoopRetroSignals(loopRetroSignalRun(), "missing"); !reflect.DeepEqual(empty, loopRetroSignals{}) {
+ t.Fatalf("unknown node signals = %+v, want zero", empty)
+ }
+}
+
+func TestBuildLoopRetroRendersCollectedSignals(t *testing.T) {
+ body := buildLoopRetro(loopRetroSignalRun(), "node", 2, errors.New("gate failed"), "loop")
+ for _, want := range []string{
+ "- agent.completed:1\n- 门禁通过 / 失败:1 / 1\n- 工具调用(finish):1\n- 进入自修正:2\n",
+ "## 问题\n\n- Loop:第 1 轮\n- Loop:第 2 轮\n- 执行器有退出 0 但没有 `agent.completed`(可能撞 max-turns / 协议中断)\n",
+ } {
+ if !strings.Contains(body, want) {
+ t.Fatalf("retro missing %q:\n%s", want, body)
+ }
+ }
+}
diff --git a/internal/engine/observability.go b/internal/engine/observability.go
index 5e48707f..4d925d29 100644
--- a/internal/engine/observability.go
+++ b/internal/engine/observability.go
@@ -96,40 +96,52 @@ func shouldSelfCorrect(onFailure string) bool {
return resolveGateOnFailure(onFailure, "") == "loop"
}
-func buildLoopRetro(run domain.Run, nodeID string, iterations int, lastErr error, onFailure string) string {
- var b strings.Builder
- fmt.Fprintf(&b, "# Loop 复盘 — `%s`\n\n", nodeID)
- fmt.Fprintf(&b, "- 自修正轮次:%d\n", iterations)
- if lastErr != nil {
- fmt.Fprintf(&b, "- 最终结果:未通过\n- 最后错误:`%s`\n\n", strings.ReplaceAll(lastErr.Error(), "\n", " "))
- } else {
- b.WriteString("- 最终结果:通过\n\n")
- }
- var completed, missingResult, gatePass, gateFail, loopStarts, toolFinishes int
- var issues []string
+// loopRetroSignals counts one node's telemetry for the loop retro.
+type loopRetroSignals struct {
+ completed, missingResult, gatePass, gateFail, loopStarts, toolFinishes int
+ // loopIssues holds one "- Loop:" line per agent.loop event, in order.
+ loopIssues []string
+}
+
+func collectLoopRetroSignals(run domain.Run, nodeID string) loopRetroSignals {
+ var s loopRetroSignals
for _, event := range run.Telemetry {
if event.NodeID != nodeID {
continue
}
switch {
case event.Name == "agent.completed":
- completed++
+ s.completed++
case event.Category == "process" && event.Name == "process.finished" && event.Source.Layer == "executor" && !strings.Contains(event.Message, "失败"):
- if completed == 0 {
- missingResult++
+ if s.completed == 0 {
+ s.missingResult++
}
case event.Category == "verification" && event.Name == "process.finished" && event.Level != "error":
- gatePass++
+ s.gatePass++
case event.Category == "verification" && event.Name == "process.finished" && event.Level == "error":
- gateFail++
+ s.gateFail++
case event.Name == "agent.loop":
- loopStarts++
- issues = append(issues, "- Loop:"+event.Message)
+ s.loopStarts++
+ s.loopIssues = append(s.loopIssues, "- Loop:"+event.Message)
case event.Category == "tool" && event.Name == "tool.call" && event.Phase == "finish":
- toolFinishes++
+ s.toolFinishes++
}
}
- if missingResult > 0 {
+ return s
+}
+
+func buildLoopRetro(run domain.Run, nodeID string, iterations int, lastErr error, onFailure string) string {
+ var b strings.Builder
+ fmt.Fprintf(&b, "# Loop 复盘 — `%s`\n\n", nodeID)
+ fmt.Fprintf(&b, "- 自修正轮次:%d\n", iterations)
+ if lastErr != nil {
+ fmt.Fprintf(&b, "- 最终结果:未通过\n- 最后错误:`%s`\n\n", strings.ReplaceAll(lastErr.Error(), "\n", " "))
+ } else {
+ b.WriteString("- 最终结果:通过\n\n")
+ }
+ signals := collectLoopRetroSignals(run, nodeID)
+ issues := signals.loopIssues
+ if signals.missingResult > 0 {
if lastErr != nil && strings.Contains(lastErr.Error(), "without a terminal result") {
issues = append(issues, "- 执行器退出 0,但 stream-json 没有 `type:result` / `agent.completed`(协议中断,不是门禁失败,也不要先当成 max-turns)")
} else {
@@ -148,7 +160,7 @@ func buildLoopRetro(run domain.Run, nodeID string, iterations int, lastErr error
} else if lastErr != nil && strings.Contains(lastErr.Error(), "No such file") {
issues = append(issues, "- 产物未落地或会话提前结束")
}
- fmt.Fprintf(&b, "## 信号\n\n- agent.completed:%d\n- 门禁通过 / 失败:%d / %d\n- 工具调用(finish):%d\n- 进入自修正:%d\n\n", completed, gatePass, gateFail, toolFinishes, loopStarts)
+ fmt.Fprintf(&b, "## 信号\n\n- agent.completed:%d\n- 门禁通过 / 失败:%d / %d\n- 工具调用(finish):%d\n- 进入自修正:%d\n\n", signals.completed, signals.gatePass, signals.gateFail, signals.toolFinishes, signals.loopStarts)
b.WriteString("## 问题\n\n")
if len(issues) == 0 {
b.WriteString("- 本节点未发现需要改 Loop 的异常。\n")