One suggestion here...I think this can be simplified with an alternate approch:

* Have a module like `RSpec::Core::TopLevelDSL` that is _always_ extended onto `main` and included in `Module`.
* When the `expose_globally` config changes, it can define or undefine the appropriate methods in that module, which will automatically make it available (or not) from `main` and from modules.

That way, you don't have to apply all of this to two different contexts.