Here's how npcs currently work (if I've read the code right):
If they don't have a target (or a 10% chance if they do), they set their target to the closest hostile in their field of view (out to their sight range, usually 10, but blocked by walls), regardless of stealth/invisibility/etc.
When an npc loses sight of their target (from walking behind a wall, stealth, or whatever), they try to guess its position. They have a radius X that goes up by 1 each turn, starting from 1 on the first turn they fail to see you (Or 3 if they've never seen you before). They pick a random position within X spaces of your true position, and average that with whatever their last guess was. That's where they treat you as being.
I didn't go deep into target passing, but it seems pretty good to me when I look at it.

Here's what I don't like about it:
- NPCs always know you're there if you enter their field of view, even if you have 9999 stealth. It starts out slightly inaccurate, but they are still alerted to your presence.
- They can divine your location if you teleport halfway across the map.
- The averaging doesn't work for large jumps. If I make a jump they'll target the space roughly halfway between for a turn, which doesn't make much sense.
Here's my suggestions for making stealth better:
- NPCs have a confidence level, which starts at 50. Every turn, this is decreased by the distance from their estimated target to the target's actual position. So in an actual combat it'll slowly decrease, but if you teleport away they'll quickly lose confidence.
- NPCs with at least double confidence and the same target can pass their target info on. So a group of enemies that can't see the target will still spread out over time, but if one enemy can see and the rest can't, they'll periodically retarget based on what the seeing enemy tells them.
- NPCs that run out of confidence lose their target.
- When an NPC first targets something they can't see, they start with a confidence of 10.
- The retargeting chance is increased when confidence is low. Note that this can retarget the same thing, which will raise the confidence back up to 10 and reset their estimated position. This is essentially the npc noticing a "new" enemy and forgetting about the old one.
- The estimated position does not take the real position into account. Instead, it deviates by sqrt(51 - confidence) every turn. We'll rely on the retargeting to deal with updating the true position.