Branch and scope globally unique identifiers

Say we are working with a Prolog-like system where variables are dynamically created in different branch contexts and scopes, yet these variables are also globally viewable by the system regardless of the current context. Is there a way to label each variable with a unique scope ID which can be used to determine if the variable assignment is considered live (i.e. not discarded by backtracking) in the current context.

Naively, you could create a bitstream of left/right branch decisions, but the memory usage would be really excessive and also have bad query time for liveness. Can anything better be done?