fix(runtime): Only apply the originating direction of a bidirectional bind (#13069) cf605e8083

Co-authored-by: hernan <hernan@rive.app>
diff --git a/.rive_head b/.rive_head
index 13d354f..6dc57e5 100644
--- a/.rive_head
+++ b/.rive_head
@@ -1 +1 @@
-7e203ecdbc503838486c262698f4f19a5d24823c
+cf605e8083b02b5517324dbc022faba0b5f03635
diff --git a/include/rive/data_bind/data_bind.hpp b/include/rive/data_bind/data_bind.hpp
index 82f6e2e..30fbcae 100644
--- a/include/rive/data_bind/data_bind.hpp
+++ b/include/rive/data_bind/data_bind.hpp
@@ -63,6 +63,13 @@
     {
         setFlag(Flag::InPersistingList, value);
     }
+    // Persisted direction of the change currently in flight. Set by addDirt
+    // from the Bindings / BindingsTarget bit it receives, and read by a
+    // converter's markConverterDirty so a multi-frame interpolation keeps
+    // re-asserting the SAME direction (per-frame dirt is cleared every update
+    // and can't carry the origin across frames). Defaults to source (false),
+    // matching the historical hardcoded markConverterDirty behavior.
+    bool targetOrigin() const { return hasFlag(Flag::TargetOrigin); }
 
     // Intrusive observer-list linkage. Used by Core to chain DataBinds that
     // have subscribed to push notifications for a given (target, propertyKey).
@@ -81,9 +88,9 @@
     }
 
 private:
-    // Four state bits packed into one byte. Each bool used to live in its own
+    // State bits packed into one byte. Each bool used to live in its own
     // 1B slot with up to 7B of padding between fields, costing ~16B per
-    // DataBind across the four flags. Packed they fit in one byte.
+    // DataBind across the flags. Packed they fit in one byte.
     enum class Flag : uint8_t
     {
         Collapsed = 1 << 0,
@@ -91,6 +98,11 @@
         InPersistingList = 1 << 2,
         SuppressDirt = 1 << 3,
         Observing = 1 << 4,
+        // Latched direction of the in-flight change: set means the change
+        // originated from the target (target→source), clear means the source
+        // (source→target). Persists across frames so interpolators re-assert
+        // it.
+        TargetOrigin = 1 << 5,
     };
     uint8_t m_flags = 0;
     bool hasFlag(Flag f) const
@@ -117,6 +129,9 @@
     DataBindContextValue* m_ContextValue = nullptr;
     DataConverter* m_dataConverter = nullptr;
     bool bindsOnce();
+    // Dirt to (re)sync a bind in both supported directions on bind/reconcile —
+    // Bindings for toTarget, BindingsTarget for toSource, both for TwoWay.
+    ComponentDirt reconcileDirt();
     File* m_file;
 #ifdef WITH_RIVE_TOOLS
 public:
diff --git a/src/data_bind/converters/data_converter.cpp b/src/data_bind/converters/data_converter.cpp
index b6aa9d3..913593f 100644
--- a/src/data_bind/converters/data_converter.cpp
+++ b/src/data_bind/converters/data_converter.cpp
@@ -35,8 +35,15 @@
 {
     if (m_parentDataBind != nullptr)
     {
+        // Re-assert the direction of the change currently in flight rather than
+        // hardcoding source→target: a target-originated interpolation must keep
+        // flowing target→source across the frames it advances. Dependents keeps
+        // the context value invalidated so the apply re-runs (and steps the
+        // interpolator) every frame.
         m_parentDataBind->addDirt(ComponentDirt::Dependents |
-                                      ComponentDirt::Bindings,
+                                      (m_parentDataBind->targetOrigin()
+                                           ? ComponentDirt::BindingsTarget
+                                           : ComponentDirt::Bindings),
                                   false);
     }
 }
diff --git a/src/data_bind/data_bind.cpp b/src/data_bind/data_bind.cpp
index 0e1a76a..221e362 100644
--- a/src/data_bind/data_bind.cpp
+++ b/src/data_bind/data_bind.cpp
@@ -322,7 +322,10 @@
         m_target->addPropertyObserver(this);
         setFlag(Flag::Observing, true);
     }
-    addDirt(ComponentDirt::Bindings, true);
+    // (Re)bind is a reconcile, not a one-sided change: mark every direction the
+    // bind supports so both sides sync in favor order (as before origin gating
+    // existed). A one-sided change instead marks only its own direction bit.
+    addDirt(reconcileDirt(), true);
 }
 
 void DataBind::target(Core* value)
@@ -490,6 +493,12 @@
            DataBindFlags::SourceToTargetRunsFirst;
 }
 
+ComponentDirt DataBind::reconcileDirt()
+{
+    return (toTarget() ? ComponentDirt::Bindings : ComponentDirt::None) |
+           (toSource() ? ComponentDirt::BindingsTarget : ComponentDirt::None);
+}
+
 void DataBind::addDirt(ComponentDirt value, bool recurse)
 {
     if (hasFlag(Flag::SuppressDirt) || (m_Dirt & value) == value)
@@ -498,6 +507,27 @@
         return;
     }
 
+    // Latch the direction this change came from so a converter that re-dirties
+    // us over multiple frames (interpolators) re-asserts the same direction —
+    // per-frame dirt gets cleared and can't carry the origin forward. Placed
+    // after the guards above so a suppressed self-notify (from applying either
+    // direction) can't flip the origin. Record a single origin, never the OR:
+    // a reconcile marks both bits, in which case the favored direction wins.
+    bool hasSource = enums::is_flag_set(value, ComponentDirt::Bindings);
+    bool hasTarget = enums::is_flag_set(value, ComponentDirt::BindingsTarget);
+    if (hasSource && hasTarget)
+    {
+        setFlag(Flag::TargetOrigin, !sourceToTargetRunsFirst());
+    }
+    else if (hasTarget)
+    {
+        setFlag(Flag::TargetOrigin, true);
+    }
+    else if (hasSource)
+    {
+        setFlag(Flag::TargetOrigin, false);
+    }
+
     m_Dirt |= value;
 #ifdef WITH_RIVE_TOOLS
     if (m_changedCallback != nullptr)
diff --git a/src/data_bind/data_bind_container.cpp b/src/data_bind/data_bind_container.cpp
index 2ff35e6..5a8a1da 100644
--- a/src/data_bind/data_bind_container.cpp
+++ b/src/data_bind/data_bind_container.cpp
@@ -125,7 +125,19 @@
         dataBind->updateDependents();
     }
 
-    if (applyTargetToSource && !dataBind->sourceToTargetRunsFirst())
+    // Only push target→source when this change actually came from the target
+    // (BindingsTarget), or when the target is polled every frame because it
+    // can't push (persisting list). Otherwise a source-originated change on a
+    // target-first bind would run target→source first and clobber the source's
+    // new value with the stale target value before update() propagates it.
+    // update() itself stays gated on Bindings (source-originated), so a
+    // target-only change is a no-op there.
+    bool wantsTargetToSource =
+        applyTargetToSource &&
+        (dataBind->inPersistingList() ||
+         (d & ComponentDirt::BindingsTarget) == ComponentDirt::BindingsTarget);
+
+    if (wantsTargetToSource && !dataBind->sourceToTargetRunsFirst())
     {
 
         dataBind->updateSourceBinding();
@@ -135,7 +147,7 @@
         dataBind->dirt(ComponentDirt::None);
         dataBind->update(d);
     }
-    if (applyTargetToSource && dataBind->sourceToTargetRunsFirst())
+    if (wantsTargetToSource && dataBind->sourceToTargetRunsFirst())
     {
 
         dataBind->updateSourceBinding();
diff --git a/src/data_bind/data_bind_context.cpp b/src/data_bind/data_bind_context.cpp
index 174521f..deee316 100644
--- a/src/data_bind/data_bind_context.cpp
+++ b/src/data_bind/data_bind_context.cpp
@@ -79,7 +79,9 @@
         }
         else
         {
-            addDirt(ComponentDirt::Bindings, true);
+            // Source unchanged — re-sync both supported directions in favor
+            // order (a reconcile, not a one-sided change). See bind().
+            addDirt(reconcileDirt(), true);
         }
         if (m_dataConverter != nullptr)
         {
diff --git a/tests/unit_tests/assets/bidirectional_binding_source.riv b/tests/unit_tests/assets/bidirectional_binding_source.riv
new file mode 100644
index 0000000..1e8390c
--- /dev/null
+++ b/tests/unit_tests/assets/bidirectional_binding_source.riv
Binary files differ
diff --git a/tests/unit_tests/assets/bidirectional_binding_target_1.riv b/tests/unit_tests/assets/bidirectional_binding_target_1.riv
new file mode 100644
index 0000000..5ea7e74
--- /dev/null
+++ b/tests/unit_tests/assets/bidirectional_binding_target_1.riv
Binary files differ
diff --git a/tests/unit_tests/assets/bidirectional_binding_target_2.riv b/tests/unit_tests/assets/bidirectional_binding_target_2.riv
new file mode 100644
index 0000000..aa22a29
--- /dev/null
+++ b/tests/unit_tests/assets/bidirectional_binding_target_2.riv
Binary files differ
diff --git a/tests/unit_tests/runtime/data_bind_container_test.cpp b/tests/unit_tests/runtime/data_bind_container_test.cpp
index 23680c2..1f629fe 100644
--- a/tests/unit_tests/runtime/data_bind_container_test.cpp
+++ b/tests/unit_tests/runtime/data_bind_container_test.cpp
@@ -280,3 +280,97 @@
 
     c.deleteDataBinds();
 }
+
+TEST_CASE("source-originated dirt does not run target->source",
+          "[data_bind_container]")
+{
+    // A non-persisting bind carrying only source-flavored dirt must NOT trigger
+    // the target->source apply — otherwise (once a converter's Dependents dirt
+    // invalidates the cached value) it clobbers the source's fresh value with
+    // the stale target before update() propagates it.
+    TestContainer c;
+    auto* b = makeBind(DataBindFlags::ToTarget);
+    c.addDataBind(b);
+    REQUIRE(b->inPersistingList() == false);
+    b->dirt(ComponentDirt::Bindings);
+    c.addDirtyDataBind(b);
+
+    c.updateDataBinds();
+
+    CHECK(b->updateCalls == 1);
+    CHECK(b->updateSourceBindingCalls == 0);
+
+    // Same, but for the interpolator-driven frame shape that markConverterDirty
+    // produces (Bindings | Dependents) — the Dependents bit must not re-open
+    // the target->source path for a source-originated change.
+    b->dirt(ComponentDirt::Bindings | ComponentDirt::Dependents);
+    c.addDirtyDataBind(b);
+    c.updateDataBinds();
+    CHECK(b->updateSourceBindingCalls == 0);
+
+    c.deleteDataBinds();
+}
+
+TEST_CASE("target-originated dirt runs target->source", "[data_bind_container]")
+{
+    TestContainer c;
+    auto* b = makeBind(DataBindFlags::ToTarget);
+    c.addDataBind(b);
+    b->dirt(ComponentDirt::BindingsTarget);
+    c.addDirtyDataBind(b);
+
+    c.updateDataBinds();
+
+    CHECK(b->updateSourceBindingCalls == 1);
+
+    c.deleteDataBinds();
+}
+
+TEST_CASE("persisting bind polls target->source regardless of origin",
+          "[data_bind_container]")
+{
+    // Polled (non-push) toSource targets never receive BindingsTarget, so the
+    // gate must still run target->source for them every frame.
+    TestContainer c;
+    auto* b = makeBind(DataBindFlags::ToSource);
+    c.addDataBind(b);
+    REQUIRE(b->inPersistingList() == true);
+    b->dirt(ComponentDirt::Bindings);
+
+    c.updateDataBinds();
+
+    CHECK(b->updateSourceBindingCalls == 1);
+
+    c.deleteDataBinds();
+}
+
+TEST_CASE("addDirt latches a single change origin", "[data_bind_container]")
+{
+    auto* b = makeBind(DataBindFlags::ToTarget);
+    b->addDirt(ComponentDirt::Bindings, false);
+    CHECK(b->targetOrigin() == false);
+    b->addDirt(ComponentDirt::BindingsTarget, false);
+    CHECK(b->targetOrigin() == true);
+    // Suppressed self-notify must not flip the latched origin.
+    b->suppressDirt(true);
+    b->addDirt(ComponentDirt::Bindings, false);
+    CHECK(b->targetOrigin() == true);
+    b->suppressDirt(false);
+    delete b;
+
+    // Reconcile marks both bits — the favored direction wins the origin.
+    auto* favorTarget = makeBind(DataBindFlags::TwoWay);
+    favorTarget->addDirt(ComponentDirt::Bindings |
+                             ComponentDirt::BindingsTarget,
+                         false);
+    CHECK(favorTarget->targetOrigin() == true);
+    delete favorTarget;
+
+    auto* favorSource = makeBind(DataBindFlags::TwoWay |
+                                 DataBindFlags::SourceToTargetRunsFirst);
+    favorSource->addDirt(ComponentDirt::Bindings |
+                             ComponentDirt::BindingsTarget,
+                         false);
+    CHECK(favorSource->targetOrigin() == false);
+    delete favorSource;
+}
diff --git a/tests/unit_tests/runtime/data_binding_artboards_test.cpp b/tests/unit_tests/runtime/data_binding_artboards_test.cpp
index 395ffbf..a01a0f9 100644
--- a/tests/unit_tests/runtime/data_binding_artboards_test.cpp
+++ b/tests/unit_tests/runtime/data_binding_artboards_test.cpp
@@ -474,4 +474,79 @@
     artboard->draw(renderer.get());
 
     CHECK(silver.matches("bindable_artboard_nesty"));
+}
+
+TEST_CASE(
+    "Data bound artboards with multiple targets bound to same property bidirectionally",
+    "[silver]")
+{
+    SerializingFactory silver;
+    auto file =
+        ReadRiveFile("assets/bidirectional_binding_source.riv", &silver);
+    auto file2 =
+        ReadRiveFile("assets/bidirectional_binding_target_1.riv", &silver);
+    auto file3 =
+        ReadRiveFile("assets/bidirectional_binding_target_2.riv", &silver);
+
+    auto artboard = file->artboardDefault();
+    REQUIRE(artboard != nullptr);
+
+    silver.frameSize(artboard->width(), artboard->height());
+
+    auto stateMachine = artboard->stateMachineAt(0);
+
+    auto vmi = file->createDefaultViewModelInstance(artboard.get());
+    auto renderer = silver.makeRenderer();
+
+    stateMachine->bindViewModelInstance(vmi);
+    stateMachine->advanceAndApply(0.0f);
+
+    auto abProp = vmi->propertyValue("costume_db_artboard")
+                      ->as<ViewModelInstanceArtboard>();
+
+    auto costume1 = file2->bindableArtboardNamed("costume_artboard");
+    auto costume2 = file3->bindableArtboardNamed("costume_artboard");
+
+    auto newProp =
+        vmi->propertyValue("costume_db_bool")->as<ViewModelInstanceBoolean>();
+    newProp->propertyValue(true);
+    artboard->draw(renderer.get());
+    silver.addFrame();
+
+    stateMachine->advanceAndApply(0.016f);
+    artboard->draw(renderer.get());
+
+    silver.addFrame();
+    abProp->asset(costume1);
+    stateMachine->advanceAndApply(0.016f);
+    artboard->draw(renderer.get());
+    silver.addFrame();
+    stateMachine->advanceAndApply(0.016f);
+    artboard->draw(renderer.get());
+
+    silver.addFrame();
+    abProp->asset(costume2);
+    stateMachine->advanceAndApply(0.016f);
+    artboard->draw(renderer.get());
+    silver.addFrame();
+    stateMachine->advanceAndApply(0.016f);
+    artboard->draw(renderer.get());
+
+    silver.addFrame();
+    abProp->asset(costume1);
+    stateMachine->advanceAndApply(0.016f);
+    artboard->draw(renderer.get());
+    silver.addFrame();
+    stateMachine->advanceAndApply(0.016f);
+    artboard->draw(renderer.get());
+
+    silver.addFrame();
+    abProp->asset(costume2);
+    stateMachine->advanceAndApply(0.016f);
+    artboard->draw(renderer.get());
+    silver.addFrame();
+    stateMachine->advanceAndApply(0.016f);
+    artboard->draw(renderer.get());
+
+    CHECK(silver.matches("bidirectional_binding_source"));
 }
\ No newline at end of file
diff --git a/tests/unit_tests/runtime/data_binding_test.cpp b/tests/unit_tests/runtime/data_binding_test.cpp
index 9fa10c0..9e4c8f3 100644
--- a/tests/unit_tests/runtime/data_binding_test.cpp
+++ b/tests/unit_tests/runtime/data_binding_test.cpp
@@ -2005,6 +2005,73 @@
     CHECK(silver.matches("bidirectional_precedence-target_first"));
 }
 
+// End-to-end sanity for a source change on a target-first TwoWay bind: the
+// source value must reach the target and must not be reverted. (The full
+// clobber only bites when a converter's Dependents dirt invalidates the cached
+// target, re-opening the target→source apply — see the container-level guard
+// "source-originated dirt does not run target->source" in
+// data_bind_container_test.cpp, which fails without the origin gate. This asset
+// has no converter, so it exercises the source→target path through the real
+// apply machinery.)
+TEST_CASE("TwoWay source change reaches target under target-first precedence",
+          "[data binding]")
+{
+    auto file = ReadRiveFile("assets/bidirectional_precedence.riv");
+    auto artboard = file->artboardNamed("target_first");
+    REQUIRE(artboard != nullptr);
+    auto stateMachine = artboard->stateMachineAt(0);
+    int viewModelId = artboard.get()->viewModelId();
+    auto vmi = viewModelId == -1
+                   ? file->createViewModelInstance(artboard.get())
+                   : file->createViewModelInstance(viewModelId, 0);
+
+    auto xProp = vmi->propertyValue("x")->as<rive::ViewModelInstanceNumber>();
+    auto yProp = vmi->propertyValue("y")->as<rive::ViewModelInstanceNumber>();
+    xProp->propertyValue(100.0f);
+    yProp->propertyValue(100.0f);
+
+    stateMachine->bindViewModelInstance(vmi);
+    // Settle the initial sync — target-first precedence makes the authored
+    // target values win and flow back into the source.
+    stateMachine->advanceAndApply(0.0f);
+    for (int i = 0; i < 10; i++)
+    {
+        stateMachine->advanceAndApply(0.016f);
+    }
+
+    rive::Node* targetNode = nullptr;
+    for (auto* db : artboard->dataBinds())
+    {
+        if (db->target() != nullptr && db->target()->is<rive::Node>())
+        {
+            targetNode = db->target()->as<rive::Node>();
+            break;
+        }
+    }
+    REQUIRE(targetNode != nullptr);
+
+    // After settling the source mirrors the target (they are two-way bound and
+    // the target won the initial sync).
+    REQUIRE(xProp->propertyValue() == targetNode->x());
+    REQUIRE(yProp->propertyValue() == targetNode->y());
+
+    // Change the SOURCE to clearly distinct values. With the fix this
+    // propagates source→target only; the buggy behavior ran target→source first
+    // and reverted the source to the (stale) target value.
+    xProp->propertyValue(500.0f);
+    yProp->propertyValue(600.0f);
+    for (int i = 0; i < 20; i++)
+    {
+        stateMachine->advanceAndApply(0.016f);
+    }
+
+    // Source keeps its new value (not clobbered) and reaches the target.
+    CHECK(xProp->propertyValue() == 500.0f);
+    CHECK(yProp->propertyValue() == 600.0f);
+    CHECK(targetNode->x() == 500.0f);
+    CHECK(targetNode->y() == 600.0f);
+}
+
 TEST_CASE("Artboards as conditions", "[silver]")
 {
     SerializingFactory silver;
diff --git a/tests/unit_tests/silvers/bidirectional_binding_source.sriv b/tests/unit_tests/silvers/bidirectional_binding_source.sriv
new file mode 100644
index 0000000..40335ed
--- /dev/null
+++ b/tests/unit_tests/silvers/bidirectional_binding_source.sriv
Binary files differ