Resolves all same-document $refs inline so sandbox code never chases references.
Circular refs become %{"$circular" => name}; external or dangling refs
become %{"$unresolved" => ref}. Sandbox code can detect both markers.
A ref's expansion is memoized by pointer, but only cached when it completed
without producing a $circular marker anywhere inside it — a cycle-hitting
expansion is stack-dependent (its shape depends on which refs are currently
being expanded above it) and must be recomputed for every call site.