package orchestration import ( "context" "git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/trace" ) // 本文件是调度层的「编排 span」:把每次子代理派发与收束记成一个 // orchestration 节点(design/19 §1 旁路节点,裁决 11.4.2)。它从 // scheduler.go 拆出,因为该文件已接近 AGENTS §10 的 500 行上限, // 而 span 的开启/收尾是一块独立职责。 // orchestration.dispatch 的 span 名与收尾事件类型:此前调度只发 // SpanKindSubagent,因此全仓没有任何 orchestration 节点。 const ( spanNameOrchestrationSpawn = "orchestration.spawn" orchestrationEventDone = "orchestration.done" ) // orchSpan 为一次子代理派发开一个 orchestration span。它是挂在主干上的 // 「派发标记」:与泳道根并列而不是它的父级 —— 若把它当作子代理 span 的父级, // 泳道链会多出一节(图构建器把非 subagent 祖先一路穿透解析泳道),既有泳道 // 契约就被改写了。span 只带非敏感属性(被派发的子代理 id、目标角色 id), // 声明了归属时同时发布一个「运行中」占位节点(真并行时落进派发它的那一路的图)。 // 未配置 tracer 时返回 nil。 func (s *Scheduler) orchSpan(ctx context.Context, sub *SubAgent) *trace.Span { if s.tracer == nil || sub == nil { return nil } _, span := s.tracer.StartSpan(ctx, spanNameOrchestrationSpawn, trace.SpanKindOrchestration) if sub.ID != "" { _ = span.SetProp("subagent", sub.ID) } if sub.PersonaID != "" { _ = span.SetProp("persona", sub.PersonaID) } if owner := sub.flowOwner(); owner != "" && s.flow != nil { _ = span.SetProp("session", owner) s.flow.SpanStarted(owner, span) } return span } // finishOrchSpan 收尾一次派发:记录耗时(失败另记原因)与一条脱敏事件, // 失败时终态为 error,然后把已关闭的 span 折回所属那一路的图。 func (s *Scheduler) finishOrchSpan(sub *SubAgent, span *trace.Span, err error) { if span == nil { return } props := map[string]any{"elapsed_ms": trace.ElapsedMS(span)} if err != nil { props["error"] = err.Error() _ = span.SetProp("error", err.Error()) } _ = span.Record(trace.Event{Type: orchestrationEventDone, Props: props}) status := trace.SpanStatusOK if err != nil { status = trace.SpanStatusError } span.Finish(status) if s.flow == nil || sub == nil { return } if owner := sub.flowOwner(); owner != "" { s.flow.SpanFinished(owner, span) } }