 (defn refine-pn [pn input-summary min-reward]
   (let [{:keys [sub-osn excluded-child-set reward-bound]} pn]
     (refine-osn! sub-osn min-reward excluded-child-set)
-    (assoc pn :output-summary
-           (compute-plan-node-output-summary input-summary sub-osn excluded-child-set reward-bound))))
+    (let [new-summary (compute-plan-node-output-summary input-summary sub-osn
+                                                        excluded-child-set reward-bound)]
+      (assert-valid-summary-change (:output-summary pn) new-summary)
+      (assoc pn :output-summary new-summary))))
+
 
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;; Splitting Plan Nodes ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;; misc accessors
(def osn-output-set :output-set)
(def osn-action     (comp first :input-pair))




(defn expand-plan [plan min-reward]
  (if (not (refinable-summary? (:summary plan) min-reward)) [plan]
      (let [{:keys [input-constraint input-set plan-nodes output-set summary]} plan
            ref-index                           (rand-int (count plan-nodes))
            [pre-nodes [ref-node & post-nodes]] (split-at ref-index plan-nodes)
;            _ (println plan  (count pre-nodes) (class ref-node) (count post-nodes))       
            [pre-set pre-summary]  (if (seq pre-nodes) (plan-node-output-set-and-summary (last pre-nodes)) [input-set zero-summary])
            sub-min-reward (- min-reward
                              (- (max-reward summary)
                                 (- (max-reward (:output-summary ref-node)) (max-reward pre-summary))))]
        (filter identity
                (for [next-pn (split-pn-output (refine-pn ref-node pre-summary sub-min-reward) pre-set pre-summary sub-min-reward)]
                  (do (assert-valid-summary-change (:output-summary ref-node) (:output-summary next-pn))
                   (propagate-plan-changes input-constraint input-set (concat pre-nodes [next-pn]) post-nodes
                                           (not= (:output-set next-pn) (:output-set ref-node)))))))))



