You're firing a ray from P, the shading point, to your defined viewpoint, and you're checking for occlusion: whether anything is hit within the distance from P to the viewpoint, relying on "maxdist".
There's a possibility of floating-point error, here, if your viewpoint is close to a surface, or the ray hits P on the way out; but I've tried fixing that with "mindist", to no effect, which is a bit mysterious.
I do think, though, it might be more positive to fire from the viewpoint to P, where you know there is something to be hit, and check whether anything else has been hit on the way...
shader Is_Visible(
point Viewpoint = (0),
output int IsVisible = 0
)
{
point hitpoint = (0);
vector vp_to_P = (P - Viewpoint);
trace (Viewpoint,vp_to_P);
getmessage ("trace","P",hitpoint);
IsVisible = (distance (hitpoint, P) < 0.00001);
}
which seems to work...

.. even then I feel an urge to wrap everything in an if hit {}