I guess the best thing to do is to not use an agent. To redo that code - LotusScript I assume - into SSJS or Java. It sure would help performance in the long run.
If the agent code is too daunting could you not save a list of the unids to the doc you pass into the agent... then when the agent returns control you RE-GET the document you passed in and read the field that has any saved UNIDS... and then process against that?
That's how it's typically done I THINK.
Dave