Syntax Tree node duplication bug

I’m running into a rare and extremely difficult bug with @codemirror/language and @codemirror/stream-parser where on long documents (currently testing with ~80k chars), typing almost at the end of the document causes the syntax tree to contain duplicate nodes from the previous syntax tree for the part of the document that is after the position of the cursor.

The problem is actually not easily reproducible if the editor started viewing (and parsing) at the beginning of the document - one must open the document and simultaneously scroll it somewhere further down (before more parsing runs) to trigger this bug.

Example:

[3k lines of simple text with no formatting]

$$1234$$

This generates 3 nodes when iterating the syntax tree.
Let’s pretend the first $ starts at position 80000:
[80000, 80002] = “math start”
[80002, 80006] = “math text”
[80006, 80008] = “math end”

When I type a character in the empty line preceding the math block:

[3k lines of simple text with no formatting]
d
$$1234$$

We expect the following nodes:
[80001, 80003] = “math start”
[80003, 80007] = “math text”
[80007, 80009] = “math end”

Except I get the following now:
[80000, 80002] = “math start”
[80002, 80006] = “math text”
[80006, 80008] = “math end”
[80001, 80003] = “math start”
[80003, 80007] = “math text”
[80007, 80009] = “math end”

An example of console log:
This seems to have generated 4 duplicate copies after typing 3 characters with a pause in between each.

I suspect this has something to do with various optimizations related to the chunking and tree cutting/reusing. Unfortunately the code around that is not something I could wrap my head around for now, but I suspect it might be something related to these two:

Thanks for reporting that. @codemirror/stream-parser 0.19.5 (via below patch) should work better.

Thank you! This was driving me crazy haha…