MCPcopy Create free account
hub / github.com/docker/docker-agent / validateForceHandoffs

Function validateForceHandoffs

pkg/config/config.go:281–322  ·  view source on GitHub ↗

validateForceHandoffs checks every agent's force_handoff reference: the target must exist (or be an external reference), an agent cannot force-hand off to itself, and chains of force_handoff edges between local agents must not form a cycle — a cycle would make the run loop bounce between agents unti

(cfg *latest.Config, allNames map[string]bool)

Source from the content-addressed store, hash-verified

279// bounce between agents until max_iterations trips, which is never what
280// the user intended.
281func validateForceHandoffs(cfg *latest.Config, allNames map[string]bool) error {
282 for _, agent := range cfg.Agents {
283 ref := agent.ForceHandoff
284 if ref == "" {
285 continue
286 }
287 if ref == agent.Name {
288 return fmt.Errorf("agent '%s' cannot force_handoff to itself", agent.Name)
289 }
290 if _, exists := allNames[ref]; !exists && !IsExternalReference(ref) {
291 return fmt.Errorf("agent '%s' references non-existent force_handoff agent '%s'", agent.Name, ref)
292 }
293 if IsExternalReference(ref) {
294 name, _ := ParseExternalAgentRef(ref)
295 if allNames[name] {
296 return fmt.Errorf("agent '%s': external force_handoff '%s' resolves to name '%s' which conflicts with a locally-defined agent", agent.Name, ref, name)
297 }
298 }
299 }
300
301 // Cycle detection: each agent has at most one outgoing force_handoff
302 // edge, so walking the chain from every agent with a visited set is
303 // linear overall. External references are leaves — they can't point
304 // back into this config.
305 edges := make(map[string]string, len(cfg.Agents))
306 for _, agent := range cfg.Agents {
307 if agent.ForceHandoff != "" && !IsExternalReference(agent.ForceHandoff) {
308 edges[agent.Name] = agent.ForceHandoff
309 }
310 }
311 for start := range edges {
312 visited := map[string]bool{start: true}
313 for cur, ok := edges[start], true; ok; cur, ok = edges[cur] {
314 if visited[cur] {
315 return fmt.Errorf("force_handoff cycle detected involving agent '%s'", cur)
316 }
317 visited[cur] = true
318 }
319 }
320
321 return nil
322}
323
324// isHCLSource reports whether the configuration data should be parsed as HCL
325// rather than YAML. The decision is based first on the source name extension,

Callers 1

validateConfigFunction · 0.85

Calls 2

IsExternalReferenceFunction · 0.85
ParseExternalAgentRefFunction · 0.85

Tested by

no test coverage detected