Is there a way to do iterative rendering modifying state and reading the final page counter?

If I’m getting the gist of your question correctly, you want to know whether some content can fit in a single page, and adjust the content until it can. I think you can achieve this in Typst; here are the fundamental steps:

  1. Put the content in a function that you can call with different actual-thresholds. You don’t need state for this; just do something like this:
    #let full-content(actual-threshold) = {
      let item-with-threshold(level: 0, ..args) = {
        if level <= actual-threshold [#args.pos().join()\ ]
      }
      item-with-threshold(level: 0)[This item has a very high priority]
      item-with-threshold(level:1)[This has lower priority]
      // ...
    }
    
  2. Use layout and measure to determine the height of the content on the actual page:
    #layout(size => {
      let measured-height = measure(full-content(2), width: size.width)
      // does it fit? check `measured-height < size.height`
    })
    
  3. Use binary or linear search to determine the largest actual-threshold that fits. Do all this inside layout(), or you’ll run into Why is the value I receive from context always content?