A couple of other things:
You asked about excluding all descendants of a @done project.
One way of doing that is to begin by specifying projects which are not marked @done
//(@type=project and not @done)
Once that seems to be working, you can extend the path from that base to ask for task descendants which are also not done:
//(@type=project and not @done)/descendant::(@type=task and not @done)
Also, as a kind of explanation in response to some of your particular questions, I have edited a slightly more verbose version of the script, which breaks a few sections down into more stages, with more named variables. I’ll post it in a minute, in the original thread.