 (defn- make-atomic-stub [name subsuming-sp inp-set function-set]
   (let [state (atom :ready) ;; set to [out-set reward] after first eval, then :go after second.
         ret
-        (traits/reify-traits
-         [(simple-stub name inp-set)
-          simple-watched simple-cached-node]
+        (traits/reify-traits [(simple-stub name inp-set)]
          summaries/Summarizable
          (summarize [s]
            (cond (= :ready @state) (summary/make-live-simple-summary (subsuming-bound s) s)

;; Here we properly deal with weirdness of stubs/inner-sps decreaseing as they add
;; children, by makig sure to do the updates synchronously.


;; TODO: pessimistic

;; TODO: pseudo-solve

;; All children are tree summarizers.
;; Non-subsuming children must be added by calling connect-child-sp!
(defprotocol TreeSummarizer
  (top-down-bound    [s])
  (add-top-down-bound! [s b]))

;; TODO: propagate top-down-bounds downward in smarter way using existing IS?
;; TODO: we need to make sure tree sums get called on add-output! first to ensure
;;       consistency with top-down-bounds ? 

(defn tree-summarizer? [s] (instance? angelic.search.implicit.fah_astar_eval2.TreeSummarizer s))

(defn make-tree-summarizer [init-bound]
 (let [tdb-atom (atom init-bound)]
   (traits/reify-traits [simple-cached-node]
     TreeSummarizer (top-down-bound [s] @tdb-atom)
                    (add-top-down-bound! [s b]
                      (when (< b @tdb-atom) ;; ;; Note: adding < current max-reward actually hurts...
                        (reset! tdb-atom b)
                        (doseq [c (summaries/node-ordinary-children s)]
                          (when (tree-summarizer? c)
                            (add-top-down-bound! c b)))
                        (summaries/summary-changed! s)))    
     summaries/Summarizable (summarize [s] (or-summary s @tdb-atom)))))

(defn init-tree-summarizer! [ts inner-sp subsuming-sp]
  (connect-and-watch! ts inner-sp
    (fn [child-sp]
      (summaries/connect! ts (tree-summarizer child-sp) false)
      (add-top-down-bound! (tree-summarizer child-sp) (top-down-bound ts))
      (summaries/summary-changed! ts))) ;; TODO: speedup? 
  (when subsuming-sp (summaries/connect! ts (tree-summarizer subsuming-sp) true)))

;; TODO: top-down bound business does not actually help at all ? 
;; TODO: look at this.
(comment         (let [my-sum (summaries/summary ret)
                child-sum (summaries/summary (tree-summarizer child-sp))]
            (when-not (summary/>= my-sum child-sum)
              (summaries/summary-changed! ret))))

(defn get-inner-sp [ts]
  (->> (summaries/node-ordinary-children ts)
       (remove tree-summarizer?)
       util/safe-singleton))

(defn ts-str [sp])
(defmethod print-method angelic.search.implicit.fah_astar_eval2.TreeSummarizer [ts o]
