This one was tricky! It is essential to check the "Run as Web user" checkbox in the "Security" tab of the LotusScript agent, otherwise you will get strange exceptions thrown (in my case it was a "TypeError" exception). I figured it out by looking at this very helpful blog post:
http://www.zarazaga.net/web/z.nsf/dx/notesagent.runwithdocumentcontext-behaviour
It would be great if IBM could include this in their reference documentation.