Perhaps someone has the source.
The game has several levels (let there be 5). For each completed level you can get 3 stars (there is such a source). There is a certain variable that is responsible for the number of stars earned. Since there are 5 levels, then when completing all levels and completing all tasks, the maximum number of stars is 15.
Interested in the following.
When passing, for example, level 1, the player receives 1 star. 1 is added to the Global variable. Then we go through the level again for 1 star. The global variable remains the same. We go through the level again, but this time with 2 stars. The global variable becomes equal to 2 (since 1 star was already earned, but when replaying, they got another one). Accordingly, when passing a level by 3 stars, the value of the GV becomes equal to 3. And at the same time, stars could be spent on an upgrade, but after the number of stars decreased in a certain GV, then when passing the same level they would no longer be added.