makeRefCounter = (specimenToRefCount, predicate) => {
    /** @type {Set<T>} */
    const seen = new Set();

    return harden({
      /**
       * @param {T} specimen
       * @returns {T}
       */
      add(specimen) {
        if (predicate(specimen)) {
          seen.add(specimen);
        }
        return specimen;
      },
      commit() {
        // Increment the reference count for each seen specimen.
        for (const specimen of seen.keys()) {
          const numRefs = specimenToRefCount.get(specimen) || 0;
          specimenToRefCount.set(specimen, numRefs + 1);
        }
        seen.clear();
      },
      abort() {
        seen.clear();
      },
    });
  }
@template T
@param {Map<T, number>} specimenToRefCount
@param {(specimen: T) => boolean} predicate