I use Bash’s PROMPT_COMMAND variable:
The value of the variable PROMPT_COMMAND is examined just before Bash prints each primary prompt. If PROMPT_COMMAND is set and has a non-null value, then the value is executed just as if it had been typed on the command line.
The source code should be pretty straight forward, but if not, please ask in the comments. Put this in your .bashrc or similar:
# per-directory Bash history