 
 ; Note: right now, reward in parent-pair is being ignored. 
 
+;; TODO: don't clobber global cache.
+
 ;; TODO: remove expensive tests.
 (defn- add-monotonic! [result-pair-atom [new-result new-reward :as new-result-pair]]
   (assert (every? #(not (= (first %) new-result)) @result-pair-atom))

                (let [next-min-reward (max min-reward (queue-best-reward queue))]
                  (queues/pq-replace! partial-queue best (make-queue-priority best-reward))
                  (let [{:keys [result-pairs max-reward]} (expand-fn best next-min-reward)]
                    (if (= max-reward Double/NEGATIVE_INFINITY) 
                        (queues/pq-remove! partial-queue best)
                      (queues/pq-replace! partial-queue best (make-queue-priority max-reward)))
                    (doseq [[ni nr] result-pairs] 
 ;                    (println "NEW" max-reward (class ni) nr (:name ni))
                      (assert (not (= :re-added (queues/pq-add! new-queue ni (make-queue-priority nr))))))
                    (recur)))))))))

      (add-monotonic! result-pairs-atom outcome-pair)
      (mapcat #(notify-upward (first %) outcome-pair) @parent-pairs-atom)))

(declare make-inverted-node)

(defn connect-downward-cache [#^HashMap cc state action [new-parent new-reward :as new-parent-pair]]
  (let [context (env/precondition-context action state)
        ic      (util/cache-with cc [(env/extract-context state context) (env/action-name action)]
                  (InvertedCache (atom []) (atom [])))
        {:keys [result-pairs-atom parent-pairs-atom]} ic 
        first-visit?  (empty? @parent-pairs-atom)]
