# Wired failure on step.apply(doc) on same set of steps

**URL:** https://discuss.prosemirror.net/t/wired-failure-on-step-apply-doc-on-same-set-of-steps/3489
**Category:** Uncategorized
**Created:** [February 10, 2021, 5:56pm UTC](https://discuss.prosemirror.net/t/wired-failure-on-step-apply-doc-on-same-set-of-steps/3489 "2021-02-10T17:56:58Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![mortyccp](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/mortyccp/32/1980_2.png) [@mortyccp](https://discuss.prosemirror.net/u/mortyccp)
#### Post date: [February 10, 2021, 5:56pm UTC](https://discuss.prosemirror.net/t/wired-failure-on-step-apply-doc-on-same-set-of-steps/3489/1 "2021-02-10T17:56:58Z")

</div>

Hi. I have encounter a wired behaviour that I have an array of steps in JSON format being saved in the database. For persisting the steps I have a validation check that will load all steps from the database and call `step.apply` for all past steps and the incoming steps.

For unknown reason, I encounter that for the same set of steps, the validation passed without error. But later on will have exception throw when calling `step.apply`.

Do you have any idea to this wired situation?

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [February 10, 2021, 7:55pm UTC](https://discuss.prosemirror.net/t/wired-failure-on-step-apply-doc-on-same-set-of-steps/3489/2 "2021-02-10T19:55:34Z")

</div>

Nope, that shouldn’t happen (applying steps is a pure operation, and applying the same step to the same document should give the same result). You may have some data corruption somewhere—for example accidental mutation of an object or array that is shared between data structures because it is supposed to be immutable.

If you can reduce the issue to a minimal example, that might help.

---

<div class="post-metadata">

### Author: ![mortyccp](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/mortyccp/32/1980_2.png) [@mortyccp](https://discuss.prosemirror.net/u/mortyccp)
#### Post date: [February 11, 2021, 1:08am UTC](https://discuss.prosemirror.net/t/wired-failure-on-step-apply-doc-on-same-set-of-steps/3489/3 "2021-02-11T01:08:53Z")

</div>

Ya. I think it’s not possible too. Will try to see I can record one example if it happens again.

---

<div class="post-metadata">

### Author: ![mortyccp](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/mortyccp/32/1980_2.png) [@mortyccp](https://discuss.prosemirror.net/u/mortyccp)
#### Post date: [February 18, 2021, 4:08pm UTC](https://discuss.prosemirror.net/t/wired-failure-on-step-apply-doc-on-same-set-of-steps/3489/4 "2021-02-18T16:08:38Z")

</div>

> [@Merging steps in collab](https://discuss.prosemirror.net/t/merging-steps-in-collab/602):
>
> I wrote a mergeStepsArray that receives an array of steps and returns an array of steps with as much merging as possible: function mergeStepsArray(steps) { let res = [], current = steps[0]; for (let i = 1; i \< steps.length; i++) { let newStep=current.merge(steps[i]); if (newStep) { current = newStep; } else { res.push(current); current = steps[i]; } } res.push(current) return res; } (as a side note, writing this was trick…

I found out the reason. Seem there is a `mergeSteps` function being run when saving to the mongo. And the merged steps are invalid when replay. After removing that, the problem is solved.

btw, if we really want to merge the steps before saving into mongo in order to reduce the number of step. Any suggestion on how to do that?

```auto
input
{
    "payload": [
        {
            "stepType": "replace",
            "from": 69,
            "to": 70
        },
        {
            "stepType": "replace",
            "from": 69,
            "to": 70
        },
        {
            "stepType": "replace",
            "from": 68,
            "to": 71,
            "slice": {
                "content": [
                    {
                        "type": "text",
                        "text": " "
                    }
                ]
            }
        },
        {
            "stepType": "replace",
            "from": 69,
            "to": 71,
            "structure": true
        },
        {
            "stepType": "replace",
            "from": 69,
            "to": 71,
            "structure": true
        }
    ]
}

merged
{
    "payload": [
        {
            "stepType": "replace",
            "from": 69,
            "to": 71
        },
        {
            "stepType": "replace",
            "from": 68,
            "to": 71,
            "slice": {
                "content": [
                    {
                        "type": "text",
                        "text": " "
                    }
                ]
            }
        },
        {
            "stepType": "replace",
            "from": 69,
            "to": 73,
            "structure": true
        }
    ]
}

```

Is that because of the `structure`?

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [February 19, 2021, 8:54am UTC](https://discuss.prosemirror.net/t/wired-failure-on-step-apply-doc-on-same-set-of-steps/3489/5 "2021-02-19T08:54:18Z")

</div>

Merging steps should generally be safe, if you can make sure that the document versions between the merged steps aren’t going to be used (which can be tricky to do with collaborative editing). If you find a sequence of steps that is valid individually, but can’t be applied when merged, let me know, since that would be a bug.

---

<div class="post-metadata">

### Author: ![mortyccp](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/mortyccp/32/1980_2.png) [@mortyccp](https://discuss.prosemirror.net/u/mortyccp)
#### Post date: [February 19, 2021, 10:17pm UTC](https://discuss.prosemirror.net/t/wired-failure-on-step-apply-doc-on-same-set-of-steps/3489/6 "2021-02-19T22:17:46Z")

</div>

Let me explain the flow that we’re having right now. Not sure it contribute the the bug or not.

1. Web will emit steps with collab.senable.
2. Server will merge those steps with the following function for persistence storage.

Created a repo to reproduce the bug

> **[mortyccp/reproduces](https://github.com/mortyccp/reproduces/tree/prosemirror-merge-steps)**
>
> prosemirror-merge-steps

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [February 20, 2021, 9:12pm UTC](https://discuss.prosemirror.net/t/wired-failure-on-step-apply-doc-on-same-set-of-steps/3489/7 "2021-02-20T21:12:04Z")

</div>

Thanks for the test case. It did indeed have to do with the `structure` flag—you hit a rare case where two structure-changing steps merged into a step that couldn’t actually be applied anymore. [This patch](https://github.com/ProseMirror/prosemirror-transform/commit/8f5d74a8efa2309c5eeb0e1967c1e9f1a877d027) should prevent such merging from happening (released as prosemirror-transform 1.2.12).

---

<div class="post-metadata">

### Author: ![mortyccp](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/mortyccp/32/1980_2.png) [@mortyccp](https://discuss.prosemirror.net/u/mortyccp)
#### Post date: [February 21, 2021, 3:51am UTC](https://discuss.prosemirror.net/t/wired-failure-on-step-apply-doc-on-same-set-of-steps/3489/8 "2021-02-21T03:51:07Z")

</div>

Thanks for the fix.

---

<div class="post-metadata">

### Author: ![mortyccp](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/mortyccp/32/1980_2.png) [@mortyccp](https://discuss.prosemirror.net/u/mortyccp)
#### Post date: [February 22, 2021, 3:05am UTC](https://discuss.prosemirror.net/t/wired-failure-on-step-apply-doc-on-same-set-of-steps/3489/9 "2021-02-22T03:05:14Z")

</div>

btw, would like to know the correctness of the `mergeSteps` function. Is this the correct way to merge steps?

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [February 22, 2021, 7:41am UTC](https://discuss.prosemirror.net/t/wired-failure-on-step-apply-doc-on-same-set-of-steps/3489/10 "2021-02-22T07:41:53Z")

</div>

Yes, the function looks good.
