       (when ((:cycle-to hierarchy) var dst-val)
         ((:var-levels hierarchy) var))))
 
-(defn- make-leaf-precond-hla [hierarchy var dst-val]
-  (SDH-Leaf-Precond-HLA hierarchy [:!P var dst-val] var dst-val 
-                   ((:dtg-to           hierarchy) var dst-val)
-                   ((:ancestor-var-set hierarchy) var)))
+(defn- make-inactive-leaf-precond-hla [hierarchy var dst-val]
+  (SDH-Inactive-Leaf-Precond-HLA hierarchy [:!P var dst-val] var dst-val 
+    ((:dtg-to           hierarchy) var dst-val)
+    ((:ancestor-var-set hierarchy) var)))
 
 ;; TODO: when can greedy sat arise?  After creation, other-sat, or finish
 

(defprotocol SDH-HLA
  (active-var-set  [a])
  (leaf-var-set    [a])
  (greedy?         [a s v av])    ; Has a descendent that can be done greedily.
  (do-refinement   [a s v av lv]) ;returns [action-seq new-hla]. av is active-var set, lv is leaf-var set
  ) 



;; TODO: fire more than one greedy action at once ...
; ;TODO: how do we keep track of preconditions moved up front ?


(declare make-action-hla)

;; NOTE: precond always needs expand after selection, even though it won't say so if satisfied.
;; this override is handled below.
(deftype SDH-Action-HLA [hierarchy name action precond-var-set precond-hlas expand-index] :as this
  SDH-HLA
    (active-var-set []  (apply clojure.set/union (map active-var-set precond-hlas)))
    (leaf-var-set   []  (apply clojure.set/union (map leaf-var-set precond-hlas)))
    (needs-expand?  []  expand-index)
    (expand         []
      (assert expand-index)
      (for [[prefix new-precond] (expand (precond-hlas expand-index))]
        (if (and (satisfied? new-precond) (every? #(and (active? %) (satisfied? % :dummy)) precond-hlas))
            [(conj prefix action) nil]
          [prefix (SDH-Action-HLA hierarchy name action precond-var-set
                                  (assoc precond-hlas expand-index new-precond) 
                                  (when (needs-expand? new-precond) expand-index))])))
    (greedy-select? [s v av] 
      (or (every? #(and (satisfied? % s) (or (active? %) (not (av (:var %))))) precond-hlas)
          (some #(greedy-select? % s v av) (util/make-safe (seq (filter #(contains? (leaf-var-set %) v) precond-hlas))))))
    (select-leaf    [s v av lv]
      (let [possible-vals  (filter #(contains? (leaf-var-set (precond-hlas %)) v) (range (count precond-hlas)))
            [deep shallow] (util/separate (comp active? precond-hlas) possible-vals)
            val-options    (or (and (:greedy-optimization? hierarchy) 
                                    (when-first [x (filter #(greedy-select? (precond-hlas %) s v av) deep)] [x])) 
                               (seq deep) 
                               (seq shallow))]
        (assert (seq val-options))
        (assert (<= (count shallow) 1))
        (for [idx         val-options,
              next        (select-leaf (precond-hlas idx) s v a v lv)
              :let [new-precond-hlas (assoc precond-hlas idx next)]]
          (SDH-Action-HLA. hierarchy [:!A* (env/action-name action) (map env/action-name new-precond-hlas)]
                           action precond-var-set new-precond-hlas idx))))
  env/Action
    (action-name     [] name)
    (primitive?      [] false)
