                               (unchecked-multiply (int 13) (int (hash remaining-actions))))
                (atom 0)))
 
-;(defn change-depth [entry new-cycle-depth]
-;  (SANodeEntry (:state entry) (:sanode entry) (:reward-to-state entry) (:remaining-actions entry)
-;               new-cycle-depth (:hash-code entry)))
+(defn change-depth [entry new-cycle-depth]
+  (SANodeEntry (:state entry) (:sanode entry) (:reward-to-state entry) (:remaining-actions entry)
+               new-cycle-depth (:hash-code entry) (:rec-context-atom entry)))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Core Algorithm  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

          
          (doseq [entry (concat good-roots (map #(:entry (meta (key %))) dirtied))] 
            (assert (util/xor (not (:sanode entry)) (:cutoff (meta entry))))
            (queues/g-pq-replace! good-queue entry 
              (- (or (:cutoff (meta entry)) (:reward-to-state entry))))) ;(if-let [n  (:sanode entry)] (cutoff (:queue n)) 0)
          (.remove stack-node-depths node) ;; TODO: catchup!! 
;          (println "Returning from" (env/action-name (:action node)) (map #(env/get-var state %) '[[atx] [aty]]) "With cutoff" cut (cutoff good-queue) "and cdm" cutoff-depth-map "and states" (count new-results) (count catchup) "-" (count clean) (count dirtied) (count still-dirty) "and good roots" (count good-roots) (map :reward-to-state good-roots))
;          (println (apply str (repeat depth \ )) "Returning from" (env/action-name (:action node)) (map #(env/get-var state %) '[[atx] [aty]]) "With cutoffs" cut (cutoff good-queue) ", ns"  (util/map-keys spos (stitch-effect-map catchup state reward-to-state) ) (count catchup) (count new-results))   
          (assert (<= (cutoff good-queue) init-cutoff))
          
          (PartialResult (stitch-effect-map 

                                                    :else                                    :dbn)))
                                          bad-nodes)]
;              (println (apply str (repeat depth \ )) (count good-nodes) (count nbn) (count dbn) (count sbn))
              (util/assert-is (<=  (+ b-rts (:cutoff rec)) (- neg-reward)) "%s %s %s" neg-reward b-rts (cutoff (:queue b-sa)))
              (reset! (:rec-context-atom entry) (:rec-context rec))
              (doseq [n good-nodes]      
                (assert (<= (:reward-to-state n) (min init-cutoff (- neg-reward))))
                (queues/g-pq-add! good-queue n (- (:reward-to-state n))))
              (doseq [n (concat nbn sbn)] 
                (queues/g-pq-add! bad-queue  n (- (:reward-to-state n))))
              (when (> (:cutoff rec) Double/NEGATIVE_INFINITY)
                (when (>= cd depth) (assert (<= (+ b-rts (:cutoff rec)) (min init-cutoff (- neg-reward)))))
                (queues/g-pq-replace! (if (< cd depth) bad-queue good-queue) 
                                      (change-depth entry cd) ; ???
                                      (- 0 b-rts (:cutoff rec))))
              (recur new-results
                     (extend-cutoff-depth-map cutoff-depth-map (:cutoff rec) cd depth)
                     (concat (when (and (>= b-cd depth) (< cd depth)) 
                               (util/assert-is (<=  (+ b-rts (:cutoff rec))  rcut (- neg-reward)))
                               [(vary-meta entry assoc :cutoff rcut)])
                             (doall (for [n sbn] (queues/g-pq-remove! good-queue n)))
                             good-roots)))))))))

;; // TODO: now problem is that depths are actually relative.  good nodes should ahve no cycle depth.

;; ALMOST: except suboptimal states may get cached at higher levels too.
;; Such bad states cannot be cached.  
