Author corrections without baking them into motion

Constant, ramp, and return offsets now append ordinary channel layers to a lane or cel in explicit owner frames. The inspector exposes the commands as one undoable transaction, shows conflicts, and offers removal or retry while preserving generated bases through regeneration.

Ordered-stack compatibility is shared by validation, conflict reporting, and regeneration, including adjacent replacement coverage. Cel-sheet gaps and headers select their lane, so commands cannot fall through to another column's stale selection.

437 tests, 5,804 assertions; both browser flows; 56 Django tests; optimized frontend build.
This commit is contained in:
Your Name 2026-09-30 22:00:48 -04:00
parent 3dbbe285fc
commit 815ce449ea
14 changed files with 806 additions and 60 deletions

View file

@ -178,27 +178,61 @@
(when (= :offset (:op l))
(shape-conflict (value-shape base) (value-shape (:values l)))))
(defn- stack-conflict
"Why layer `i` can encounter a value of the wrong shape after the layers
before it. A replace covering all of this layer's support becomes the only
possible input; a partly overlapping replace adds another possible input."
[ch i l]
(when (and (= :offset (:op l))
(vector? (:support l)) (= 2 (count (:support l))))
(let [[a b] (:support l)
shapes (reduce
(fn [possible prior]
(let [[c d] (when (and (vector? (:support prior))
(= 2 (count (:support prior))))
(:support prior))]
(if (and c d (not (:conflict prior)) (= :replace (:op prior))
(< a d) (< c b))
(let [s (value-shape (:values prior))]
(if (and (<= c a) (<= b d)) #{s} (conj possible s)))
possible)))
#{(value-shape ch)} (take i (:over ch)))
v (value-shape (:values l))]
(some #(shape-conflict % v) shapes))))
(defn- support-of [l]
(let [s (:support l)]
(when (and (vector? s) (= 2 (count s))
(every? number? s) (< (first s) (second s)))
s)))
(defn stack-conflict
"Why layer `i` can encounter a value of the wrong shape after the active
layers before it, or nil.
Replacement coverage is considered at every interval boundary. This matters
when adjacent replacements jointly cover an offset: neither covers its whole
support, but the base can never reach it. A conflicted replacement is skipped,
exactly as the evaluator skips it."
[ch i]
(let [l (nth (:over ch) i nil)]
(when (and (= :offset (:op l)) (support-of l))
(let [[a b] (support-of l)
prior (take i (:over ch))
cuts (->> prior
(keep support-of)
(mapcat identity)
(filter #(< a % b))
(into [a b])
distinct sort)
;; Shape at a point is the last active, nonempty replacement's
;; shape, or the base shape when no replacement supplies a value.
at (fn [f]
(or (last (keep (fn [p]
(let [s (support-of p)
v (value-shape (:values p))]
(when (and (= :replace (:op p))
(not (:conflict p)) v s
(covers? s f))
v)))
prior))
(value-shape ch)))
shapes (into #{} (map (fn [[x y]] (at (/ (+ x y) 2))))
(partition 2 1 cuts))
v (value-shape (:values l))]
(some #(shape-conflict % v) shapes)))))
(defn reconcile
"Recheck an ordered layer stack against this channel's base.
Old conflict marks are findings from an earlier base, so they are cleared and
recomputed in order. A newly conflicted replacement is then invisible to the
layers after it, matching evaluation. Nothing is dropped or reordered."
[ch]
(let [layers (mapv #(dissoc % :conflict) (:over ch))]
(reduce (fn [out l]
(let [candidate (assoc ch :over (conj out l))
why (stack-conflict candidate (count out))]
(conj out (cond-> l why (assoc :conflict why)))))
[] layers)))
(defn conflicts
"Corrections on `ch` that cannot apply to its base, as `[{:id :why}]`.
@ -209,8 +243,8 @@
not load. `flow/regenerate` records one on the layer, a conflicted layer is not
applied, and this is how a view finds them to offer."
[ch]
(vec (for [l (:over ch)
:let [why (or (:conflict l) (conflict-with ch l))]
(vec (for [[i l] (map-indexed vector (:over ch))
:let [why (or (:conflict l) (stack-conflict ch i))]
:when why]
{:id (:id l) :why why})))
@ -558,7 +592,7 @@
(and (map? ch) (vector? (:over ch)))
(into (for [[i l] (map-indexed vector (:over ch))
:when (not (:conflict l))
:let [why (stack-conflict ch i l)]
:let [why (stack-conflict ch i)]
:when why]
(str "correction " (pr-str (:id l)) " " why)))

View file

@ -0,0 +1,114 @@
(ns arthur.domain.correction
"Pure commands that author and resolve correction layers.
Evaluation belongs to `channel`; this namespace only constructs a layer,
places it on its owning node, and refuses a document that would not be valid."
(:require [arthur.domain.channel :as ch]
[arthur.domain.clip :as clip]
[arthur.domain.node :as node]))
(def ^:private supported-paths #{[:xform :rot] [:xform :pos]})
(def ^:private motions #{:constant :ramp :return})
(defn- finite? [x] (and (number? x) (js/Number.isFinite x)))
(defn- numeric-value? [v]
(or (finite? v)
(and (vector? v) (pos? (count v)) (every? finite? v))))
(defn- same-shape? [a b]
(or (and (number? a) (number? b))
(and (vector? a) (vector? b) (= (count a) (count b)))))
(defn- expected-value? [path v]
(case path
[:xform :rot] (finite? v)
[:xform :pos] (and (vector? v) (= 2 (count v)) (every? finite? v))
false))
(defn- values-channel
[{:keys [motion support delta start end peak peak-frame]}]
(let [[a b] support]
(case motion
:constant (ch/framed delta)
:ramp (ch/keyed {a start, (dec b) end} :linear)
:return (ch/keyed {a start, peak-frame peak, (dec b) start} :linear)
nil)))
(defn- invalid
[path {:keys [id support motion delta start end peak peak-frame]} existing]
(let [[a b] (when (and (vector? support) (= 2 (count support))) support)
samples (case motion :constant [delta] :ramp [start end]
:return [start peak] [])]
(cond
(nil? id) "a correction needs an ID"
(some #(= id (:id %)) existing) "the correction ID is already used on this channel"
(not (contains? supported-paths path)) "that property does not support correction authoring"
(not (contains? motions motion)) "choose constant, ramp, or return motion"
(not (and (integer? a) (integer? b) (< a b)))
"support must be an increasing [in out) of whole owner frames"
(not-every? numeric-value? samples) "correction values must be finite numbers"
(not-every? #(expected-value? path %) samples)
"correction values do not have the property's shape"
(and (= :ramp motion) (< (- b a) 2)) "a ramp needs at least two samples"
(and (= :return motion) (< (- b a) 3)) "return motion needs at least three samples"
(and (= :return motion)
(not (and (integer? peak-frame) (< a peak-frame (dec b)))))
"the return peak must be a whole owner frame inside both endpoints"
(and (#{:ramp :return} motion) (not (same-shape? start (if (= :ramp motion) end peak))))
"motion endpoints must have the same shape")))
(defn- finish [candidate selection]
(if-let [why (first (clip/problems candidate))]
{:refused why}
{:clip candidate :selection selection}))
(defn add
"Append one offset correction to a node channel.
Support and value keys are in the selected node's own frames. Defaults are
materialized through `node/channels`, so correcting an unkeyed transform does
not need a special representation."
[document sid node-id path spec]
(let [n (get-in document [:symbols sid :nodes node-id])
base (when n (get (node/channels n) path))
existing (:over base)
why (cond
(nil? (clip/symbol document sid)) "the owning symbol does not exist"
(nil? n) "the correction target does not exist"
(nil? base) "the correction target has no such channel"
:else (invalid path spec existing))]
(if why
{:refused why}
(let [layer (ch/layer (:id spec) (:support spec) :offset (values-channel spec))
corrected (update base :over (fnil conj []) layer)
candidate (assoc-in document [:symbols sid :nodes node-id :channels path] corrected)]
(finish candidate node-id)))))
(defn remove-layer
"Remove one named layer, refusing when a later layer depended on its shape."
[document sid node-id path layer-id]
(let [at [:symbols sid :nodes node-id :channels path]
c (get-in document at)
layers (:over c)]
(cond
(nil? c) {:refused "the correction channel does not exist"}
(not-any? #(= layer-id (:id %)) layers) {:refused "the correction does not exist"}
:else (finish (assoc-in document at
(assoc c :over (vec (remove #(= layer-id (:id %)) layers))))
node-id))))
(defn retry-layer
"Clear one recorded conflict when the complete resulting stack is valid."
[document sid node-id path layer-id]
(let [at [:symbols sid :nodes node-id :channels path]
c (get-in document at)
found (some #(when (= layer-id (:id %)) %) (:over c))]
(cond
(nil? found) {:refused "the correction does not exist"}
(nil? (:conflict found)) {:refused "the correction has no recorded conflict"}
:else
(finish (update-in document (conj at :over)
(fn [layers]
(mapv #(if (= layer-id (:id %)) (dissoc % :conflict) %) layers)))
node-id))))

View file

@ -6,6 +6,7 @@
there should not be one: an editor's own state is the cheapest thing in the
app to change and the most expensive to have two copies of."
(:require [arthur.domain.clip :as clip]
[arthur.domain.correction :as correction]
[arthur.domain.gesture :as gesture]
[arthur.domain.nest :as nest]
[arthur.domain.node :as node]
@ -58,6 +59,37 @@
(assoc-in [:ui :selection] [:node sid (:selection result) (conj prefix (:selection result))])
(update :ui dissoc :lane-retry)))))
(defn apply-correction-command
"Commit one correction command while keeping the complete row address that
selected its owner. A refusal changes only the visible status."
[db result]
(if-let [why (:refused result)]
(assoc-in db [:project :status] why)
(-> db
(edit/transaction (constantly (:clip result)))
(update :project merge {:status "edited · unsaved"}))))
(rf/reg-event-db
::add-correction
(fn [db [_ sid id path spec]]
(let [clip (:clip (store/entry (:clip/current db)))]
(apply-correction-command
db (correction/add clip sid id path (assoc spec :id (random-uuid)))))))
(rf/reg-event-db
::remove-correction
(fn [db [_ sid id path layer-id]]
(let [clip (:clip (store/entry (:clip/current db)))]
(apply-correction-command
db (correction/remove-layer clip sid id path layer-id)))))
(rf/reg-event-db
::retry-correction
(fn [db [_ sid id path layer-id]]
(let [clip (:clip (store/entry (:clip/current db)))]
(apply-correction-command
db (correction/retry-layer clip sid id path layer-id)))))
(rf/reg-event-db
::new-lane
(fn [db _]

View file

@ -45,12 +45,7 @@
topology has resolved it."
[old fresh]
(if-let [over (seq (:over old))]
(assoc fresh :over
(mapv (fn [l]
(if-let [why (ch/conflict-with fresh l)]
(assoc l :conflict why)
(dissoc l :conflict)))
over))
(assoc fresh :over (ch/reconcile (assoc fresh :over (vec over))))
fresh))
(defn- bases

View file

@ -235,6 +235,139 @@
[channel-control sid id path ch frame]
[:dd (channel-state ch)])]))])]))
;; ---------------------------------------------------------------------------
;; corrections
(defn- correction-initial [clip sid id]
(let [n (get-in clip [:symbols sid :nodes id])
[a b] (or (:span n) [0 3])
a (if (integer? a) a 0)
through (max a (min (dec (if (integer? b) b 3)) (+ a 2)))]
{:target id :path [:xform :rot] :motion :constant
:from a :through through :peak-frame (min (dec through) (inc a))
:delta 0 :delta-x 0 :delta-y 0
:start 0 :start-x 0 :start-y 0
:end 0 :end-x 0 :end-y 0
:peak 0 :peak-x 0 :peak-y 0}))
(defn- draft-number [draft key label integer?]
[:label.inspector-field label
[:input {:type "number" :step (if integer? 1 "any")
:value (or (get @draft key) "")
:on-change (fn [e]
(let [s (.. e -target -value)
n ((if integer? js/parseInt js/parseFloat) s 10)]
(swap! draft assoc key (when-not (js/isNaN n) n))))}]])
(defn- value-inputs [draft prefix label]
(if (= [:xform :rot] (:path @draft))
[draft-number draft prefix (str label " (degrees)") false]
[:<>
[draft-number draft (keyword (str (name prefix) "-x")) (str label " x") false]
[draft-number draft (keyword (str (name prefix) "-y")) (str label " y") false]]))
(defn- correction-value [d prefix]
(if (= [:xform :rot] (:path d))
(some-> (get d prefix) (* (/ js/Math.PI 180)))
[(get d (keyword (str (name prefix) "-x")))
(get d (keyword (str (name prefix) "-y")))]))
(defn- correction-spec [d]
(let [base {:support [(:from d) (when (number? (:through d)) (inc (:through d)))]
:motion (:motion d)}]
(case (:motion d)
:constant (assoc base :delta (correction-value d :delta))
:ramp (assoc base :start (correction-value d :start)
:end (correction-value d :end))
:return (assoc base :start (correction-value d :start)
:peak (correction-value d :peak)
:peak-frame (:peak-frame d))
base)))
(defn- correction-layers [clip sid id]
(for [[path c] (get-in clip [:symbols sid :nodes id :channels])
l (:over c)]
{:path path :layer l}))
(defn- correction-section [[sid selected-id selected]]
(let [clip @(rf/subscribe [::render/clip])
parent (get-in clip [:symbols sid :nodes (:parent selected)])
targets (cond
(node/lane? selected) [selected-id]
(node/lane? parent) [selected-id (:id parent)]
:else [])]
(when (seq targets)
(r/with-let [draft (r/atom (correction-initial clip sid selected-id))]
(let [target (:target @draft)
motion (:motion @draft)
conflicts (clip-domain/conflicts clip)
layers (correction-layers clip sid target)]
[section "corrections"
[:div.correction-grid
[:label.inspector-field "owner"
[:select {:value (or (first (keep-indexed #(when (= %2 target) %1) targets)) 0)
:on-change (fn [e]
(let [i (js/parseInt (.. e -target -value) 10)
id (nth targets i)]
(reset! draft (correction-initial clip sid id))))}
(doall (for [[i id] (map-indexed vector targets)]
^{:key (str id)}
[:option {:value i}
(str (if (= id selected-id) "selected · " "lane · ") (brief id))]))]]
[:label.inspector-field "property"
[:select {:value (if (= [:xform :rot] (:path @draft)) "rotation" "position")
:on-change #(swap! draft assoc :path
(if (= "rotation" (.. % -target -value))
[:xform :rot] [:xform :pos]))}
[:option {:value "rotation"} "rotation offset"]
[:option {:value "position"} "position offset"]]]
[:label.inspector-field "motion"
[:select {:value (name motion)
:on-change #(swap! draft assoc :motion (keyword (.. % -target -value)))}
[:option {:value "constant"} "constant"]
[:option {:value "ramp"} "ramp"]
[:option {:value "return"} "return"]]]
[:div]
[draft-number draft :from "from owner frame" true]
[draft-number draft :through "through owner frame" true]
(case motion
:constant [value-inputs draft :delta "offset"]
:ramp [:<> [value-inputs draft :start "start offset"]
[value-inputs draft :end "end offset"]]
:return [:<> [value-inputs draft :start "start offset"]
[value-inputs draft :peak "peak offset"]
[draft-number draft :peak-frame "peak owner frame" true]]
nil)]
[:div.row {:style {:margin-top "6px"}}
[:button {:on-click #(rf/dispatch [::ui/add-correction sid target
(:path @draft) (correction-spec @draft)])}
"apply correction"]]
(when (seq layers)
[:div.correction-list
(doall
(for [{:keys [path layer]} layers]
^{:key (str path (:id layer))}
[:div.correction-item
[:span {:title (pr-str (:id layer))}
(str (str/join " " (map name path)) " · "
(pr-str (:support layer))
(when (:conflict layer) " · conflict"))]
(when (:conflict layer)
[:button {:on-click #(rf/dispatch [::ui/retry-correction
sid target path (:id layer)])}
"retry"])
[:button {:on-click #(rf/dispatch [::ui/remove-correction
sid target path (:id layer)])}
"remove"]]))])
(when (seq conflicts)
[:div.correction-conflicts
[:div.dim "document conflicts"]
(doall
(for [{:keys [symbol node channel id why]} conflicts]
^{:key (str symbol node channel id)}
[:div {:title why} (str (brief node) " · "
(str/join " " (map name channel)) " · " why)]))])])))))
;; ---------------------------------------------------------------------------
;; tracing a face
;;
@ -455,6 +588,8 @@
[:div {:style {:min-height 0}}
[clip-section]
(when node [node-section node])
(when node ^{:key (str (first node) "/" (second node))}
[correction-section node])
(when (and face (or (trace/traceable? clip face) (seq faces)))
[tracing-section face faces path])
(when (= :symbol (first selection)) [symbol-section (second selection)])

View file

@ -682,14 +682,16 @@
It reuses `rows`, so its spans and selection addresses are exactly the ones
the timeline presents rather than a second interpretation of the document."
[clip sid frames]
(mapv (fn [{:keys [path label cels]}]
(mapv (fn [{:keys [path label cels select]}]
{:id (peek path)
:path path
:select select
:label label
:cells (mapv (fn [f]
(let [cel (some (fn [{[in out] :span :as cel}]
(when (and (<= in f) (< f out)) cel))
cels)]
{:frame f :lane (peek path) :cel cel}))
{:frame f :lane (peek path) :lane-select select :cel cel}))
(range frames))})
(filter :cels (rows clip sid #{}))))
@ -704,8 +706,11 @@
(str "52px repeat(" (max 1 (count columns)) ", minmax(110px, 1fr))")}]
[:div.cel-sheet {:style style}
[:div.cs-head.cs-frame "frame"]
(doall (for [{:keys [id label]} columns]
^{:key (str "head-" id)} [:div.cs-head label]))
(doall (for [{:keys [path label select]} columns]
^{:key (str "head-" path)}
[:button.cs-head {:class (when (= select selection) "selected")
:on-click #(rf/dispatch [::ui/select select])}
label]))
(doall
(for [f (range frames)
item (cons {:frame-label? true}
@ -714,7 +719,8 @@
^{:key (str "frame-" f)}
[:button.cs-frame {:class (when (= f frame) "on")
:on-click #(rf/dispatch [::pb/seek f])} f]
(let [{:keys [id label select]} (:cel item)]
(let [{:keys [id label select]} (:cel item)
target (or select (:lane-select item))]
^{:key (str f "-" (:lane item) "-" (or id "gap"))}
[:button.cs-cell
{:class (str (when (= f frame) " current")
@ -722,7 +728,7 @@
:title (if id (str label " · frame " f) (str "gap · frame " f))
:on-click (fn []
(rf/dispatch [::pb/seek f])
(when select (rf/dispatch [::ui/select select])))}
(rf/dispatch [::ui/select target]))}
(or label "—")]))))]))
(defn view []