AFAIK with_mutex/2 is not needed in add_port_scan_result/2 because there should only ever be one fact for each port and once the facts are generated after the first time I don’t ever expect them to change. Also only one thread will be adding the new fact for the specific port once, that thread will not be competing with other threads and thus no lock is needed. Since this was created to test the open ports of a specific site if something changes then there is something very wrong somewhere.
Since there are so few examples of working library(persistency) code to be found, I am leaving with_mutex/2 for the write in because people are likely to copy this and if the with_mutex/2 were missing they would be wondering why on rare days their code does not work.
EDIT
Starting with SWI-Prolog 8.3.3 library(persistency) (GitHub Commit) was made more thread friendly by wrapping the four predicates with with_mutex/2.
So
add_port_scan_result(Request,Response) :-
with_mutex(port_scan_result_journal, assert_port_scan_result(Request,Response)).
can be changed to
add_port_scan_result(Request,Response) :-
assert_port_scan_result(Request,Response).
From documentation
This module requires the same thread-synchronization as the normal Prolog database. This implies that if each individual assert or retract takes the database from one consistent state to the next, no additional locking is required. If more than one elementary database operation is required to get from one consistent state to the next, both updating and querying the database must be locked using with_mutex/2.