Guard against long ids.

The BuildInvalidator uses ids that most often come from targets and
these can be lengthy when the targets are synthetic.  If the id > ~250
characters, this will produce a hash filename that's too long for most
common filesystems.  Guard against this with safe_filename and shorten
to a hash iff needed to avoid this.