#1332·eino

[Bug] compose: Workflow.Compile accepts conflicting nested field mappings

Author: vitolee-1018Created Sep 28, 2026Updated Sep 28, 2026

Describe the bug

WorkflowNode.checkAndAddMappedPath (compose/workflow.go:369) rejects two mappings to the same node when one target path is a prefix of the other, such as Key1 and Key1.Key2. The sub-test "both to map.any, and to map.any.field" in TestWorkflowWithNestedFieldMappings checks this. The check only works when the shorter path is a single field and is mapped first. These cases compile without an error:

  • Key1.Key2 mapped before Key1
  • Key1.Key2 and Key1.Key2.Key3, in either order
  • a field mapping to Key1.Key2 plus SetStaticValue(FieldPath{"Key1"}, ...)

The mapped paths are recorded as a tree. Two lines in the inner loop lose information:

go
if i < len(targetPath)-1 {
	m[path] = make(map[string]any) // line 395: replaces the subtree recorded so far
	m = m[path].(map[string]any)
} else {
	m[path] = struct{}{} // line 398: overwrites an existing subtree without an error
}

Line 395 runs for every intermediate segment, so anything recorded below the first segment is dropped before the new path is compared with it. Line 398 turns an intermediate node into a terminal one and does not report the longer path that was mapped earlier.

At runtime one of the two mappings is silently lost. When the two mappings come from different predecessors, which one wins can differ between invocations. Static values are passed to the same check in map iteration order (compose/workflow.go:476), so a node with SetStaticValue(FieldPath{"Key1"}, ...) and SetStaticValue(FieldPath{"Key1", "Key2"}, ...) compiles in some runs and fails in others.

To Reproduce

go
wf := compose.NewWorkflow[string, map[string]any]()
wf.End().AddInput(compose.START,
	compose.ToFieldPath([]string{"Key1", "Key2", "Key3"}),
	compose.ToFieldPath([]string{"Key1", "Key2"}))
r, err := wf.Compile(ctx)
fmt.Println("compile:", err)
if err == nil {
	out, _ := r.Invoke(ctx, "hello")
	fmt.Println("invoke: ", out)
}

accepted := 0
for i := 0; i < 200; i++ {
	wf := compose.NewWorkflow[string, map[string]any]()
	wf.End().AddInput(compose.START, compose.ToFieldPath([]string{"Key3"})).
		SetStaticValue(compose.FieldPath{"Key1"}, "v1").
		SetStaticValue(compose.FieldPath{"Key1", "Key2"}, "v2")
	if _, err := wf.Compile(ctx); err == nil {
		accepted++
	}
}
fmt.Printf("static values Key1 + Key1.Key2: %d of 200 compiles succeeded\n", accepted)

On alpha/10 @ 58daeda this prints:

compile: <nil>
invoke:  map[Key1:map[Key2:hello]]
static values Key1 + Key1.Key2: 25 of 200 compiles succeeded

The Key1.Key2.Key3 mapping is dropped. The number of successful compiles changes between runs (17 to 29 in my runs).

Expected behavior

Compile returns a two terminal field paths conflict error in all the cases above, regardless of the order of the mappings, as it already does for Key1 followed by Key1.Key2.

Version:

alpha/10 @ 58daeda (compose/workflow.go:395, compose/workflow.go:398). The same code exists on main @ ba04fde.