Expansion kept a single flat macro table, so a \def inside {...} replaced the
outer definition for the rest of the document. Keep a scope stack in
expand_macros instead: '{' and \begingroup push a scope, '}' and \endgroup pop
it, and lookup walks the scopes newest-first before the document table. A
top-level \def and every \gdef update the document table; a \def inside a group
only shadows until the group closes.
Runtime \def\name{body} is now parsed during expansion (the pre-expansion inline
capture is gone), and grouping braces are dropped after command parsing so the
scope braces do not print: plain pieces, link labels, inline runs and the
fallback face path all strip them at that point.
A test previews \def\a{one}{\def\a{two}\a}\a as "twoone" and the
\begingroup form likewise; the existing inline-definition test still passes.
Catcode state and a real token stream remain open.