                        [res p])]
     (not (graphs/dag? (concat parent-edges extra-edges)))))
 
-(defn activation-actions  "Return a list of all activation actions for var v"
-  [child-map v]
-  (for [c (child-map v)]
-    (make-set-parent-var-action v c)))
+(defn var-not-usable? [s ancestor-map target-var a-var]
+  (if-let [a  (env/get-var s (action-var a-var))]
+      (let [pm (:precond-map a)]
+        (every?
+         (fn [pv] (or (= (env/get-var s pv) (pm pv)) (var-not-usable? s ancestor-map target-var pv)))
+         (remove #{a-var} (keys pm))))
+    (not (contains? (ancestor-map a-var) target-var))))
+
+;; TODO: dynamic bottom-up pruning.
+ ; i.e., exists precondition of some current action, NOT currently achieved,
+ ; that has every child of a parent-link in its ancestor-set. 
+; Or, more simply, variables should "die off".  
+;; TODO: generalize.  This is just special case for most logistics,
+(defn dead-end? [s ancestor-map child-map extra-edges]
+  (let [gpm      (:precond-map (env/get-var s (action-var sas/goal-var-name)))
+        live-set (apply clojure.set/union #{sas/goal-var-name}
+                        (for [pv (remove #{sas/goal-var-name} (keys gpm))
+                              :when (not (= (env/get-var s pv) (gpm pv)))]
+                          (ancestor-map pv)))]
+    (some #(not (live-set (second %)))
+          (concat extra-edges
+            (for [[p cs] child-map, c cs :when (env/get-var s (parent-var p c))] [p c])))))
+
+(defn possible-activation-actions  
+   "Return a list of possible activation actions for var v possible in state s.
+   To be possible parent, must be supported from below, and not cause deadlock
+   (by pointing to action whose other precondition is needing it. "
+   [s ancestor-map child-map v]
+;   (activation-actions child-map v) #_
+   
+ (for [c (child-map v) :when (not (or (deadlocked? s child-map [[v c]])
+                                      (dead-end? s ancestor-map child-map [[ v c]])))]
+   (make-set-parent-var-action v c)))
+
 
 (defn current-child [s child-var-map p-var]
   (when-not (env/get-var s (free-var p-var))

(defn make-asplan-env [sas-problem] 
  (def *add-count* (util/sref 0))
  (let [causal-graph  (remove #(apply = %) (sas-analysis/standard-causal-graph sas-problem))
        child-var-map (util/map-vals #(map second %) (util/group-by first causal-graph))
        vars          (keys (:vars sas-problem))
        dtgs          (sas-analysis/domain-transition-graphs sas-problem)
        simple-dtgs   (util/map-vals (fn [dtg] (for [[pval emap] dtg, [eval _] emap] [pval eval])) dtgs)
        acyclic-succ-fn (partial possibly-acyclic-successors (HashMap.) simple-dtgs)]

      (precondition-context [s] pc) ;; perhaps can do better?
    hierarchy/HighLevelAction
      (immediate-refinements- [s] 
;        (Thread/sleep 10)
        (assert (= (env/get-var s (action-var effect-var)) a))
        (let [na-tuples         (for [nav   ancestor-vars
                                      :when (not #(env/get-var s (action-var %)))
                                      :let  [child (current-child s child-var-map nav)]
                                      :when (and child (not (env/get-var s (action-var child))))]
