Infinite loop when `clone_model` callback changes a layer to a functional model
@SuryanarayanaY   I was able to replicate this issue on colab, please find the [gist](https://colab.research.google.com/gist/sushreebarsa/9f28b8e2cbd814bbf962b191bfe58cf5/61492.ipynb#scrollTo=ah8YR0tL_DdN) here. Thank you! Hi @christian-steinmeyer ,

I have checked the code with keras_core which is now a multi backend support library. In `keras_core` there is no reported behaviour. Please refer to attached [gist](https://colab.sandbox.google.com/gist/SuryanarayanaY/0451166bfed11cb4020bb43e323a3b6c/61492_keras-core.ipynb).

`keras_core` subsequently will release as `Keras3` and tf.keras module will become legacy code. Thanks! LGTM! Thanks! Hi @christian-steinmeyer ,

Can we mark this issue as closed ? Please feel free to close the issue if resolved for you.

Thanks! This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61492">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61492">No</a>

### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

No

### Source

binary

### TensorFlow version

2.13.0

### Custom code

Yes

### OS platform and distribution

_No response_

### Mobile device

_No response_

### Python version

3.10.6

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

While the below code works, when using the sequential model, it doesn't for the functional one. With the functional model, the [while loop in `reconstruct_from_config()`](https://github.com/tensorflow/tensorflow/blob/ae5e557a555e2aa5f236af6cfdb83dc568ea38e1/tensorflow/python/keras/engine/functional.py#L779) loops indefinitely.

I don't fully understand the code there, but my hunch is that either I'm doing wrong on the usage side, or there might be a bug close to the `node_count_by_layer`, which is set to the return value of `should_skip_first_node` although that seems like a different concept.

### Standalone code to reproduce the issue

```shell
import tensorflow as tf
import numpy as np


def replace_conv2d_with_sub_model(layer: tf.keras.layers.Layer) -> tf.keras.layers.Layer:
    if isinstance(layer, tf.keras.layers.Conv2D):
        inputs = layer.input
        x = layer(inputs)
        y = tf.keras.layers.Lambda(lambda t: t * 2)(x)
        outputs = tf.keras.layers.Add()([x, y])
        layer = tf.keras.models.Model(inputs=inputs, outputs=outputs, name=layer.name)
    return layer


def get_sequential_model() -> tf.keras.Model:
    return tf.keras.Sequential(
        [
            tf.keras.layers.InputLayer(input_shape=(28, 28, 1)),
            tf.keras.layers.Conv2D(32, 3, activation='relu'),
            tf.keras.layers.Flatten(),
            tf.keras.layers.Dense(10, activation='softmax'),
        ]
    )


def get_functional_model() -> tf.keras.Model:
    inputs = tf.keras.Input(shape=(28, 28, 1))
    x = tf.keras.layers.Conv2D(32, 3, activation='relu')(inputs)
    x = tf.keras.layers.Flatten()(x)
    outputs = tf.keras.layers.Dense(10, activation='softmax')(x)
    return tf.keras.Model(inputs=inputs, outputs=outputs)


if __name__ == '__main__':
    model = get_functional_model()  # get_sequential_model() works
    model.compile()
    sample_input = np.random.uniform(size=(1, 28, 28, 1))
    o1 = model.predict(sample_input)

    new_model = tf.keras.models.clone_model(
        model, input_tensors=model.inputs, clone_function=replace_conv2d_with_sub_model
    )  # <-- results in an infinite loop

    o2 = new_model.predict(sample_input)
```


### Relevant log output

_No response_