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.

【架构优化】从 nativeMetadata 中拆出原生事件里程碑分类 nativeMilestone (#548)

fx-kurumi42b92ff77b9a
diff --git a/internal/httpapi/assistant_diagnostic_evidence.go b/internal/httpapi/assistant_diagnostic_evidence.go
index 5f3a047c..4c0c2fe6 100644
--- a/internal/httpapi/assistant_diagnostic_evidence.go
+++ b/internal/httpapi/assistant_diagnostic_evidence.go
@@ -312,6 +312,30 @@ func (a *assistantDiagnosticAttempt) tools(line string) {
}
}
+// nativeMilestone classifies one native CLI protocol event into a diagnostic
+// timeline milestone. dedupe reports whether the milestone should be recorded
+// at most once per attempt. An empty milestone means the event is not one.
+func nativeMilestone(raw map[string]any) (milestone string, dedupe bool) {
+ get := func(v map[string]any, key string) string { value, _ := v[key].(string); return value }
+ result, _ := raw["result"].(map[string]any)
+ eventSub, _ := raw["event"].(map[string]any)
+ method, kind := get(raw, "method"), get(raw, "type")
+ switch {
+ case kind == "system" && get(raw, "subtype") == "init" || get(result, "userAgent") != "" || get(raw, "event") == "init":
+ return "cli.initialized", true
+ case method == "turn/started" || kind == "turn_start":
+ return "model.turn_started", false
+ case method == "item/agentMessage/delta" || method == "item/reasoning/summaryTextDelta" || method == "item/reasoning/textDelta" ||
+ (kind == "stream_event" && get(eventSub, "type") == "content_block_delta") || kind == "model.streaming" || kind == "message_update":
+ return "model.first_output_delta", true
+ case method == "turn/completed":
+ return "model.turn_finished", false
+ case kind == "result" || kind == "final" || kind == "agent_end":
+ return "protocol.result_received", false
+ }
+ return "", false
+}
+
func (a *assistantDiagnosticAttempt) nativeMetadata(raw map[string]any) {
get := func(v map[string]any, key string) string { value, _ := v[key].(string); return value }
params, _ := raw["params"].(map[string]any)
@@ -329,8 +353,8 @@ func (a *assistantDiagnosticAttempt) nativeMetadata(raw map[string]any) {
version = match
}
}
- method, kind := get(raw, "method"), get(raw, "type")
- eventSub, _ := raw["event"].(map[string]any)
+ method := get(raw, "method")
+ milestone, dedupe := nativeMilestone(raw)
a.update(func(attempt *domain.AssistantCLIAttempt) {
if version != "" {
attempt.CLIVersion, attempt.VersionSource = version, "native_handshake"
@@ -349,21 +373,6 @@ func (a *assistantDiagnosticAttempt) nativeMetadata(raw map[string]any) {
if turn, ok := params["turn"].(map[string]any); ok && get(turn, "id") != "" {
attempt.NativeTurnID = sanitizeAssistantDiagnostic(get(turn, "id"), a.user, 160)
}
- milestone := ""
- dedupe := false
- switch {
- case kind == "system" && get(raw, "subtype") == "init" || get(result, "userAgent") != "" || get(raw, "event") == "init":
- milestone, dedupe = "cli.initialized", true
- case method == "turn/started" || kind == "turn_start":
- milestone = "model.turn_started"
- case method == "item/agentMessage/delta" || method == "item/reasoning/summaryTextDelta" || method == "item/reasoning/textDelta" ||
- (kind == "stream_event" && get(eventSub, "type") == "content_block_delta") || kind == "model.streaming" || kind == "message_update":
- milestone, dedupe = "model.first_output_delta", true
- case method == "turn/completed":
- milestone = "model.turn_finished"
- case kind == "result" || kind == "final" || kind == "agent_end":
- milestone = "protocol.result_received"
- }
if milestone != "" {
if dedupe {
for _, existing := range a.d.report.Timeline {
diff --git a/internal/httpapi/assistant_diagnostic_milestone_test.go b/internal/httpapi/assistant_diagnostic_milestone_test.go
new file mode 100644
index 00000000..de1958fe
--- /dev/null
+++ b/internal/httpapi/assistant_diagnostic_milestone_test.go
@@ -0,0 +1,93 @@
+package httpapi
+
+import (
+ "reflect"
+ "testing"
+ "time"
+
+ "agentflow/internal/domain"
+)
+
+func TestNativeMilestoneClassifiesProtocolEvents(t *testing.T) {
+ cases := []struct {
+ name string
+ raw map[string]any
+ milestone string
+ dedupe bool
+ }{
+ {"claude system init", map[string]any{"type": "system", "subtype": "init"}, "cli.initialized", true},
+ {"codex initialize result", map[string]any{"result": map[string]any{"userAgent": "codex_cli_rs/0.40.0"}}, "cli.initialized", true},
+ {"init event string", map[string]any{"event": "init"}, "cli.initialized", true},
+ {"system non-init", map[string]any{"type": "system", "subtype": "status"}, "", false},
+ {"codex turn started", map[string]any{"method": "turn/started"}, "model.turn_started", false},
+ {"pi turn start", map[string]any{"type": "turn_start"}, "model.turn_started", false},
+ {"codex agent delta", map[string]any{"method": "item/agentMessage/delta"}, "model.first_output_delta", true},
+ {"codex reasoning summary delta", map[string]any{"method": "item/reasoning/summaryTextDelta"}, "model.first_output_delta", true},
+ {"codex reasoning delta", map[string]any{"method": "item/reasoning/textDelta"}, "model.first_output_delta", true},
+ {"claude content delta", map[string]any{"type": "stream_event", "event": map[string]any{"type": "content_block_delta"}}, "model.first_output_delta", true},
+ {"claude other stream event", map[string]any{"type": "stream_event", "event": map[string]any{"type": "message_start"}}, "", false},
+ {"model streaming", map[string]any{"type": "model.streaming"}, "model.first_output_delta", true},
+ {"pi message update", map[string]any{"type": "message_update"}, "model.first_output_delta", true},
+ {"codex turn completed", map[string]any{"method": "turn/completed"}, "model.turn_finished", false},
+ {"claude result", map[string]any{"type": "result"}, "protocol.result_received", false},
+ {"final", map[string]any{"type": "final"}, "protocol.result_received", false},
+ {"pi agent end", map[string]any{"type": "agent_end"}, "protocol.result_received", false},
+ {"init wins over result type", map[string]any{"type": "result", "event": "init"}, "cli.initialized", true},
+ {"unrelated", map[string]any{"type": "assistant", "method": "item/started"}, "", false},
+ {"empty", map[string]any{}, "", false},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ milestone, dedupe := nativeMilestone(tc.raw)
+ if milestone != tc.milestone || dedupe != tc.dedupe {
+ t.Fatalf("nativeMilestone(%v) = (%q, %v), want (%q, %v)", tc.raw, milestone, dedupe, tc.milestone, tc.dedupe)
+ }
+ })
+ }
+}
+
+// nativeMetadata must keep honoring nativeMilestone's dedupe flag: one-shot
+// milestones are recorded once per attempt, repeating ones every time, and a
+// new attempt starts its own dedupe window.
+func TestNativeMetadataRecordsMilestonesWithPerAttemptDedupe(t *testing.T) {
+ g := domain.AssistantGeneration{ID: "milestone-turn", SessionID: "milestone-session", Status: "running", StartedAt: time.Now().UTC()}
+ d := newAssistantDiagnosticsCollector(g)
+ user := domain.User{ID: 1}
+ first := d.begin("codex", "model-a", false, user)
+ for _, line := range []string{
+ `{"type":"system","subtype":"init"}`,
+ `{"type":"system","subtype":"init"}`,
+ `{"method":"turn/started"}`,
+ `{"method":"item/agentMessage/delta"}`,
+ `{"method":"item/reasoning/textDelta"}`,
+ `{"method":"turn/completed"}`,
+ `{"method":"turn/started"}`,
+ `{"method":"item/agentMessage/delta"}`,
+ `{"method":"turn/completed"}`,
+ `{"type":"system","subtype":"status"}`,
+ } {
+ first.protocol(line)
+ }
+ second := d.begin("codex", "model-a", true, user)
+ second.protocol(`{"type":"system","subtype":"init"}`)
+ second.protocol(`{"method":"item/agentMessage/delta"}`)
+
+ counts := map[string]map[string]int{}
+ for _, e := range d.snapshot(g).Timeline {
+ if e.Source != "cli" {
+ continue
+ }
+ if counts[e.AttemptID] == nil {
+ counts[e.AttemptID] = map[string]int{}
+ }
+ counts[e.AttemptID][e.Kind]++
+ }
+ wantFirst := map[string]int{"cli.initialized": 1, "model.turn_started": 2, "model.first_output_delta": 1, "model.turn_finished": 2}
+ wantSecond := map[string]int{"cli.initialized": 1, "model.first_output_delta": 1}
+ if got := counts[first.id]; !reflect.DeepEqual(got, wantFirst) {
+ t.Fatalf("first attempt milestones = %v, want %v", got, wantFirst)
+ }
+ if got := counts[second.id]; !reflect.DeepEqual(got, wantSecond) {
+ t.Fatalf("second attempt milestones = %v, want %v", got, wantSecond)
+ }
+}