realUserCtx is not directly accessible from the sync function, only indirectly available via the require… functions.
When a Sync Gateway _resync is run against a DB all the documents in the DB are run through the sync function again, but there is no real user context during this process. If a user defined sync function was to directly test the value of realUserCtx it would reject the documents on _resync, which is not the desired behaviour.
The require… functions will always evaluate true during a _resync allowing docs to be reprocessed by the Sync Function.
Sync functions are not supposed to be able to access the realUserCtx. I had a look at the source code and it looks like it’s not very securely hidden, which is why your hack works.
Just understand that this is a hack, it’s unsupported, and that realUserCtx will be null in some situations. You can use it for debugging if it helps you, but it should never be used in production.