diff --git a/document/format/src/migrate.rs b/document/format/src/migrate.rs index 32836f69c41..86db8bf239e 100644 --- a/document/format/src/migrate.rs +++ b/document/format/src/migrate.rs @@ -104,7 +104,7 @@ impl Gdd { Ok(rewritten) => rewritten, Err(error) => { log::warn!("Migrating this document's history failed, so undo can't reach before this point: {error}"); - (self.session_with_truncated_history(migrated)?, remap_metadata(&self.metadata, |_| None)) + (session_with_truncated_history(&self.session, migrated)?, remap_metadata(&self.metadata, |_| None)) } }; self.keep_blobs_durably(blobs.into_added()).await?; @@ -189,19 +189,24 @@ impl Gdd { session.absorb_retired_marks(previous.retired_marks()); Ok((session, remap_metadata(&self.metadata, remap))) } +} - /// A session whose history restarts from `migrated`. - fn session_with_truncated_history(&self, migrated: Registry) -> Result { - let previous = &self.session; - let restarted = Session::bootstrap_from_registry(previous.peer(), previous.user(), migrated.clone())?; - - let history: Vec = restarted.history().cloned().collect(); - let mut session = Session::load(previous.peer(), previous.user(), migrated, history, restarted.head_rev(), Vec::new(), previous.next_node_counter()); - session.restore_hot_sequence(previous.last_hot_sequence()); - session.restore_clock_counter(previous.clock_counter().max(restarted.clock_counter())); - session.absorb_retired_marks(previous.retired_marks()); - Ok(session) - } +/// A session continuing `previous` whose history restarts from `migrated`. +fn session_with_truncated_history(previous: &Session, migrated: Registry) -> Result { + let peer_users = migrated.peer_users.clone(); + let restarted = Session::bootstrap_from_registry(previous.peer(), previous.user(), migrated)?; + + // The bootstrap re-stamps every value, so its registry is the one matching its history. Its deltas leave out + // which user each device belongs to, so those registrations carry over directly. + let mut registry = restarted.retired_registry().clone(); + registry.peer_users = peer_users; + let history: Vec = restarted.history().cloned().collect(); + + let mut session = Session::load(previous.peer(), previous.user(), registry, history, restarted.head_rev(), Vec::new(), previous.next_node_counter()); + session.restore_hot_sequence(previous.last_hot_sequence()); + session.restore_clock_counter(previous.clock_counter().max(restarted.clock_counter())); + session.absorb_retired_marks(previous.retired_marks()); + Ok(session) } /// Resolves declaration resources to the declarations migrations name. A declaration's resource derives from its identifier and version @@ -269,3 +274,41 @@ impl DeclarationResolver for DocumentDeclarations { Some(self.encoded.get(self.known.get(&resource)?)?.proto.clone()) } } + +#[cfg(test)] +mod tests { + use super::*; + use document_graph_storage::{AttributeDelta, RegistryDelta, Value}; + + #[test] + fn a_truncated_history_matches_its_registry_and_keeps_device_registrations() { + let other_device = PeerId(2); + let mut previous = Session::with_peer(PeerId(1)); + previous + .stage_computed_ops(vec![RegistryDelta::RegisterPeer { + peer: other_device, + user: previous.user(), + }]) + .unwrap(); + previous.retire_all().unwrap(); + + // Each edit stamps the value later than a fresh bootstrap would + for edit in 0..10 { + let delta = AttributeDelta { + key: "edit".to_string(), + value: Some(Value::Str(edit.to_string())), + }; + previous.stage_computed_ops(vec![RegistryDelta::ChangeDocumentAttribute { delta }]).unwrap(); + previous.retire_all().unwrap(); + } + + let session = session_with_truncated_history(&previous, previous.retired_registry().clone()).unwrap(); + assert_eq!(session.user_of(other_device), Some(previous.user())); + + // Device registrations carry over apart from the history, so everything else must match its replay + let replayed = Session::replay_from_history(session.peer(), session.user(), session.history().cloned(), session.next_node_counter()).unwrap(); + let mut registry = session.retired_registry().clone(); + registry.peer_users = replayed.retired_registry().peer_users.clone(); + assert_eq!(®istry, replayed.retired_registry()); + } +} diff --git a/document/graph-storage/src/attributes.rs b/document/graph-storage/src/attributes.rs index 29fbdb481dd..5ba175be39b 100644 --- a/document/graph-storage/src/attributes.rs +++ b/document/graph-storage/src/attributes.rs @@ -7,6 +7,15 @@ use std::collections::{BTreeMap, btree_map}; /// keys live on `Node.inputs_attributes[i]`; per-network keys live on `Network.attributes`. pub mod attr; +/// Unified storage-side position. The valid variants depend on `attr::node::ui::IS_LAYER`: +/// layers use `Absolute` or `Stack`; non-layer nodes use `Absolute` or `Chain`. +#[derive(Copy, Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub enum Position { + Absolute([i32; 2]), + Chain, + Stack(u32), +} + impl AttributeValue { pub fn new(value: Value, timestamp: TimeStamp) -> Self { Self { value, timestamp, deleted: false } diff --git a/document/graph-storage/src/lib.rs b/document/graph-storage/src/lib.rs index 2e2a05b44ff..bdc5bde5c79 100644 --- a/document/graph-storage/src/lib.rs +++ b/document/graph-storage/src/lib.rs @@ -44,7 +44,7 @@ pub use from_runtime::{ encode_node_ui_attributes, node_value_resource_refs, storage_location_id, storage_node_id, value_resource_ref, }; #[cfg(any(feature = "conversion", test))] -pub use metadata_source::{InputMetadataEntry, NetworkMetadataEntry, NoMetadata, NodeMetadataEntry, NodeMetadataSource, Position}; +pub use metadata_source::{InputMetadataEntry, NetworkMetadataEntry, NoMetadata, NodeMetadataEntry, NodeMetadataSource}; #[cfg(any(feature = "conversion", test))] pub use to_runtime::{Declarations, ProjectedNetwork, ProjectedNode, RuntimeProjection}; diff --git a/document/graph-storage/src/metadata_source.rs b/document/graph-storage/src/metadata_source.rs index 0d2fea05604..495d3fa7cb2 100644 --- a/document/graph-storage/src/metadata_source.rs +++ b/document/graph-storage/src/metadata_source.rs @@ -8,7 +8,7 @@ use std::collections::{BTreeMap, HashMap}; use core_types::uuid::NodeId as RuntimeNodeId; use graph_craft::document::NetworkLocation; -use crate::{NetworkReference, Value}; +use crate::{NetworkReference, Position, Value}; /// One node's editor-side metadata, produced by `Registry::to_runtime_with_metadata`. One entry per /// node, since every node carries an identity to restore even when it carries no `ui::*` attribute. @@ -124,12 +124,3 @@ pub trait NodeMetadataSource { pub struct NoMetadata; impl NodeMetadataSource for NoMetadata {} - -/// Unified storage-side position. The valid variants depend on `attr::node::ui::IS_LAYER`: -/// layers use `Absolute` or `Stack`; non-layer nodes use `Absolute` or `Chain`. -#[derive(Copy, Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -pub enum Position { - Absolute([i32; 2]), - Chain, - Stack(u32), -} diff --git a/document/graph-storage/src/model.rs b/document/graph-storage/src/model.rs index 5750aa741b7..4a3a614fb38 100644 --- a/document/graph-storage/src/model.rs +++ b/document/graph-storage/src/model.rs @@ -22,6 +22,9 @@ impl Node { pub fn inputs_mut(&mut self) -> &mut Vec { &mut self.inputs } + pub fn attributes_mut(&mut self) -> &mut Attributes { + &mut self.attributes + } /// True if both nodes agree on every value-bearing field, ignoring slot/attribute timestamps. pub fn value_equal(&self, other: &Self) -> bool { diff --git a/document/migrations/compiled/src/vary_nodes_from_copy_to_points.rs b/document/migrations/compiled/src/vary_nodes_from_copy_to_points.rs index d47b47a67f2..179e0c35779 100644 --- a/document/migrations/compiled/src/vary_nodes_from_copy_to_points.rs +++ b/document/migrations/compiled/src/vary_nodes_from_copy_to_points.rs @@ -1,9 +1,10 @@ use document_graph_storage::attr::node::VISIBLE; +use document_graph_storage::attr::node::ui::DISPLAY_NAME; use graphene_std::Color; use graphene_std::raster_types::{CPU, Raster}; use graphene_std::vector::Vector; use graphene_std::vector::style::Gradient; -use migration_core::{Compiler, Inputs, MigrationError, NewNode, Node, NodeInput, Target, Upstream, decl, literal, literal_of, migration}; +use migration_core::{Attributes, Compiler, Inputs, MigrationError, NewNode, Node, NodeInput, Target, Upstream, decl, literal, literal_of, migration}; use serde::{Deserialize, Serialize}; const CONTENT: usize = 1; @@ -29,11 +30,23 @@ fn number(input: &NodeInput) -> Option { /// Repeat on Points overwrites. Content that doesn't compile takes Flatten Graphic, which can't make that state any worse. A hidden node only passes /// its points through, so it needs nothing past the repeat, and its unused scale and rotation are dropped. #[migration("core_types::vector::CopyToPointsNode"@1 => "transform_nodes::transform_nodes::VaryRotationsNode"@2)] -pub fn vary_nodes_from_copy_to_points(node: &Node, compiler: Option<&dyn Compiler>, inputs: &mut Inputs, upstream: &mut Upstream, target: &mut Target) -> Result<(), MigrationError> { +pub fn vary_nodes_from_copy_to_points( + node: &Node, + compiler: Option<&dyn Compiler>, + inputs: &mut Inputs, + upstream: &mut Upstream, + target: &mut Target, + attributes: &mut Attributes, +) -> Result<(), MigrationError> { let slots: Vec = node.inputs().iter().map(|slot| slot.input.clone()).collect(); let [points, content, scale_min, scale_max, scale_bias, scale_seed, rotation, rotation_seed] = <[NodeInput; 8]>::try_from(slots).map_err(|_| MigrationError::invariant("Copy to Points has eight inputs"))?; + // A stored default name would misname the new node + if node.attributes().get_typed::(DISPLAY_NAME).as_deref() == Some("Copy to Points") { + attributes.remove(node.id(), DISPLAY_NAME); + } + let repeat = decl!("repeat_nodes::repeat_nodes::RepeatOnPointsNode"@1); let unset = || literal(Tagged::Bool(false)); @@ -170,6 +183,8 @@ fn bias_keeping_average_scale(bias: f64) -> f64 { mod tests { use super::*; use crate::CompilerHost; + use document_graph_storage::attr::node::ui::POSITION; + use document_graph_storage::{NodeId, Position, TimeStamp, Value, to_value}; use migration_core::Declaration; use migration_runner::testing::{Fixture, Random, SourceContract, check_commutation_with, check_undo_with}; @@ -184,10 +199,13 @@ mod tests { } fn contract() -> SourceContract { - SourceContract::new(8, |random: &mut Random, slot| match slot { - 2 | 3 | 4 | 6 => literal(Tagged::Number(random.below(3) as f64)), - _ => literal(Tagged::Integer(random.below(3) as i64)), - }) + SourceContract { + display_names: &["Copy to Points"], + ..SourceContract::new(8, |random: &mut Random, slot| match slot { + 2 | 3 | 4 | 6 => literal(Tagged::Number(random.below(3) as f64)), + _ => literal(Tagged::Integer(random.below(3) as i64)), + }) + } } fn star_inputs() -> Vec { @@ -262,6 +280,48 @@ mod tests { assert_eq!(number(&fixture.inputs(&migrated, rotations)[2]), Some(45.)); } + #[test] + fn the_default_name_is_cleared() { + let mut fixture = Fixture::new(&VaryNodesFromCopyToPoints); + let star = fixture.add(STAR, star_inputs()); + let copies = fixture.add(COPY_TO_POINTS, inputs(NodeInput::Node { id: star, index: 0 }, 2., 90.)); + let name = Value::Str("Copy to Points".to_string()); + fixture.registry.node_instances.get_mut(&copies).unwrap().attributes_mut().set(DISPLAY_NAME, name, TimeStamp::ORIGIN); + + let migrated = fixture.migrate_with(&VaryNodesFromCopyToPoints, Some(&host())).unwrap(); + + assert!(migrated.node_instances[&copies].attributes().get_typed::(DISPLAY_NAME).is_none()); + } + + /// Matches the layout the legacy migration gives a star copied at a scale of 2 with random rotations spanning 90°. + #[test] + fn stages_lead_up_to_a_placed_node_in_a_row() { + let mut fixture = Fixture::new(&VaryNodesFromCopyToPoints); + let star = fixture.add(STAR, star_inputs()); + let copies = fixture.add(COPY_TO_POINTS, inputs(NodeInput::Node { id: star, index: 0 }, 2., 90.)); + let position = to_value(&Position::Absolute([20, 10])).unwrap(); + fixture.registry.node_instances.get_mut(&copies).unwrap().attributes_mut().set(POSITION, position, TimeStamp::ORIGIN); + + let migrated = fixture.migrate_with(&VaryNodesFromCopyToPoints, Some(&host())).unwrap(); + let position_of = |node: NodeId| migrated.node_instances[&node].attributes().get_typed::(POSITION); + + // Walk back from the anchor through each stage's points + let mut stages = Vec::new(); + let mut stage = copies; + while let NodeInput::Node { id, .. } = fixture.inputs(&migrated, stage)[0] + && id != star + { + stages.insert(0, position_of(id)); + stage = id; + } + let NodeInput::Node { id: group, .. } = fixture.inputs(&migrated, stage)[1] else { + panic!("the repeat reads the group") + }; + + assert_eq!(stages, [-1, 6, 13].map(|x| Some(Position::Absolute([x, 10])))); + assert_eq!(position_of(group), Some(Position::Absolute([-8, 13]))); + } + #[test] fn bias_keeps_its_average() { assert_eq!(bias_keeping_average_scale(0.), 0.5); diff --git a/document/migrations/core/src/lib.rs b/document/migrations/core/src/lib.rs index 123c1011213..4d7ebba0640 100644 --- a/document/migrations/core/src/lib.rs +++ b/document/migrations/core/src/lib.rs @@ -29,7 +29,7 @@ pub use connector::{InputConnector, NewNode, OutputConnector}; #[cfg(feature = "typed")] pub use declaration::{Declaration, DeclarationKind, DeclarationResolver}; #[cfg(feature = "typed")] -pub use document_graph_storage::NodeInput; +pub use document_graph_storage::{NodeInput, Position, attr}; pub use error::MigrationError; pub use format::{DocumentPayloads, FormatMigration, HistoryPolicy, Upgrade}; #[cfg(feature = "typed")] @@ -44,7 +44,7 @@ pub use registry::{register_migration, registered_migrations}; #[cfg(feature = "typed")] pub use value::{encode, literal, literal_of}; #[cfg(feature = "typed")] -pub use view::{AddedNode, AnchorContext, Downstream, Edits, Graph, Inputs, InputsEdit, Node, Region, Target, Upstream, View, implements}; +pub use view::{AddedNode, AnchorContext, AttributeWrite, Attributes, CHAIN_WIDTH, Downstream, Edits, FEEDER_ROW_HEIGHT, Graph, Inputs, InputsEdit, Node, Region, Target, Upstream, View, implements}; // For the registration `#[migration]`, `rename!`, and `format_migration!` generate #[doc(hidden)] diff --git a/document/migrations/core/src/view.rs b/document/migrations/core/src/view.rs index 3b72ab220a1..7ae84502683 100644 --- a/document/migrations/core/src/view.rs +++ b/document/migrations/core/src/view.rs @@ -1,9 +1,14 @@ use crate::{Compiler, Declaration, DeclarationResolver, InputConnector, MigrationError, NewNode, OutputConnector, encode}; use document_graph_storage::attributes::attr::node::REFLECTION_METADATA; -use document_graph_storage::{Implementation, InputSlot, NetworkId, NodeId, NodeInput, Registry, TimeStamp}; +use document_graph_storage::{Implementation, InputSlot, NetworkId, NodeId, NodeInput, Registry, TimeStamp, Value}; use serde::Serialize; use std::ops::Deref; +/// How many grid cells the editor's layout steps left for each node of a layer's chain. +pub const CHAIN_WIDTH: i32 = 7; +/// How many grid cells apart the rows of nodes feeding a node from below are. +pub const FEEDER_ROW_HEIGHT: i32 = 3; + /// Which part of the registry around an anchor a view reads or edits. A migration's footprint is the union of its views' regions. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Region(u8); @@ -16,6 +21,8 @@ impl Region { pub const DOWNSTREAM: Self = Self(1 << 3); /// The anchor's whole network, as read through [`Graph`]. pub const NETWORK: Self = Self(1 << 4); + /// Attributes of the anchor and of the nodes the run adds, such as their positions and display names. + pub const ATTRIBUTES: Self = Self(1 << 5); pub const fn union(self, other: Self) -> Self { Self(self.0 | other.0) @@ -225,6 +232,8 @@ pub struct Edits { pub connections: Vec<(OutputConnector, InputConnector)>, /// The declaration this anchor switches to, when not the migration's target. pub target: Option, + /// Attribute writes to the anchor and the nodes this run adds. + pub attributes: Vec, } impl Edits { @@ -236,6 +245,57 @@ impl Edits { self.removed_nodes.extend(other.removed_nodes); self.connections.extend(other.connections); self.target = other.target.or(self.target); + self.attributes.extend(other.attributes); + } +} + +/// One attribute of the anchor or of a node the run adds. A run's writes apply in the order it recorded them. +#[derive(Debug)] +pub struct AttributeWrite { + pub node: NodeId, + pub key: String, + /// `None` deletes the key. + pub value: Option, +} + +/// Attributes of the anchor and of the nodes this run adds, such as where they sit and what they're named. +#[derive(Default)] +pub struct Attributes { + writes: Vec, +} + +impl Attributes { + /// Write `value` under `key` on the anchor or on a node this run adds. + pub fn set(&mut self, node: NodeId, key: &str, value: &T) { + self.writes.push(AttributeWrite { + node, + key: key.to_string(), + value: Some(encode(value)), + }); + } + + /// Delete `key` from the anchor or from a node this run adds. + pub fn remove(&mut self, node: NodeId, key: &str) { + self.writes.push(AttributeWrite { + node, + key: key.to_string(), + value: None, + }); + } +} + +impl<'a> View<'a> for Attributes { + const REGION: Region = Region::ATTRIBUTES; + + fn extract(_context: &AnchorContext<'a>) -> Self { + Self::default() + } + + fn into_edits(self) -> Result { + Ok(Edits { + attributes: self.writes, + ..Default::default() + }) } } diff --git a/document/migrations/macros/src/lib.rs b/document/migrations/macros/src/lib.rs index 633dfb99dd1..0b112dc1827 100644 --- a/document/migrations/macros/src/lib.rs +++ b/document/migrations/macros/src/lib.rs @@ -16,7 +16,7 @@ use syn::{Error, FnArg, Ident, ItemFn, ReturnType, Type, parse_macro_input}; /// A `network` prefix names a built-in network definition, as in `#[migration(network "Merge"@1 => "module::merge"@2)]`. /// /// Parameters state what the migration reads and writes: `&Node` and `&Graph` read the source state, `Option<&dyn Compiler>` asks what only the -/// compiler can tell about it, and each `&mut` parameter is a view (`Inputs`, `Upstream`, `Downstream`, `Target`) recording edits to its region. The function may return `()` or `Result<(), MigrationError>`. +/// compiler can tell about it, and each `&mut` parameter is a view (`Inputs`, `Upstream`, `Downstream`, `Target`, `Attributes`) recording edits to its region. The function may return `()` or `Result<(), MigrationError>`. /// /// Generates a unit struct named after the function in Pascal case implementing `NodeMigration`, and registers it. #[proc_macro_attribute] diff --git a/document/migrations/runner/src/lib.rs b/document/migrations/runner/src/lib.rs index 63c1d959464..3867e4eb76e 100644 --- a/document/migrations/runner/src/lib.rs +++ b/document/migrations/runner/src/lib.rs @@ -40,6 +40,8 @@ pub enum RunnerError { DuplicateAddedNode { anchor: NodeId, declaration: Declaration, key: String }, #[error("added node {0:?} already exists")] AddedNodeExists(NodeId), + #[error("node {anchor:?} writes attributes of {node:?}, which is neither it nor a node it adds")] + AttributesOutsideRegion { anchor: NodeId, node: NodeId }, #[error("{0:?} is rewired by one anchor while its own migration replaces or removes it")] ConflictingSlot(InputConnector), #[error("{0:?} does not exist in the migrated state")] diff --git a/document/migrations/runner/src/node.rs b/document/migrations/runner/src/node.rs index 0521026d127..131f9742673 100644 --- a/document/migrations/runner/src/node.rs +++ b/document/migrations/runner/src/node.rs @@ -2,10 +2,12 @@ use crate::{MigrationBlobs, RunnerError}; use document_graph_storage::TimeStamp; -use document_graph_storage::attributes::attr::node::ui::REFERENCE; +use document_graph_storage::attributes::attr::node::ui::{IS_LAYER, POSITION, REFERENCE}; use document_graph_storage::delta::compute_deltas; -use document_graph_storage::{Delta, Implementation, NetworkId, Node, NodeId, NodeInput, ProtoNode, Registry, RegistryDelta, ResourceHash, ResourceId, Rev, apply_retired_op}; -use migration_core::{AnchorContext, Compiler, Declaration, DeclarationResolver, Edits, InputConnector, InputsEdit, MigrationHost, NodeMigration, OutputConnector, implements}; +use document_graph_storage::{Delta, Implementation, NetworkId, Node, NodeId, NodeInput, Position, ProtoNode, Registry, RegistryDelta, ResourceHash, ResourceId, Rev, apply_retired_op}; +use migration_core::{ + AnchorContext, CHAIN_WIDTH, Compiler, Declaration, DeclarationResolver, Edits, FEEDER_ROW_HEIGHT, InputConnector, InputsEdit, MigrationHost, NodeMigration, OutputConnector, encode, implements, +}; use std::collections::{HashMap, HashSet}; /// A [`DeclarationResolver`] over a fixed set of declarations for tests, whose resource IDs derive from `identifier@version` rather than the encoded declarations documents store. @@ -173,6 +175,10 @@ fn apply_runs(migration: &dyn NodeMigration, source: &Registry, anchors: &[NodeI } add_nodes(&mut target, &runs, resolver)?; + for (anchor, edits) in &runs { + place_added_nodes(&mut target, *anchor, edits); + write_attributes(&mut target, *anchor, edits)?; + } let input_edits: HashMap = runs.iter().filter_map(|(anchor, edits)| Some((*anchor, edits.inputs.as_ref()?))).collect(); for (anchor, inputs) in &input_edits { @@ -328,6 +334,90 @@ fn add_nodes(target: &mut Registry, runs: &[(NodeId, Edits)], resolver: &dyn Dec Ok(()) } +/// Default positions for the nodes one run adds, read from the anchor's own position only. The nodes leading up to the anchor's +/// chain input form a row, or join the chain of a chained or layer anchor. Each other node sits below the first one reading it, +/// or on the anchor when none does. Without absolute coordinates for the anchor, only the chain is placed. +fn place_added_nodes(target: &mut Registry, anchor: NodeId, edits: &Edits) { + let Some(anchor_node) = target.node_instances.get(&anchor) else { return }; + let stored = anchor_node.attributes().get_typed::(POSITION); + let is_layer = anchor_node.attributes().get_or(IS_LAYER, false); + let anchor_inputs: Vec = match &edits.inputs { + Some(inputs) => inputs.slots.iter().map(|slot| slot.input.clone()).collect(), + None => anchor_node.inputs().iter().map(|slot| slot.input.clone()).collect(), + }; + + let added_inputs: HashMap = edits.added_nodes.iter().map(|added| (added.id, added.node.inputs.as_slice())).collect(); + let reads = |inputs: &[NodeInput], node: NodeId| inputs.iter().any(|input| matches!(input, NodeInput::Node { id, .. } if *id == node)); + + let chain_input = if is_layer { 1 } else { 0 }; + let mut chain = Vec::new(); + let mut next = anchor_inputs.get(chain_input); + while let Some(NodeInput::Node { id, .. }) = next + && let Some(inputs) = added_inputs.get(id) + && !chain.contains(id) + { + chain.push(*id); + next = inputs.first(); + } + + let joins_chain = is_layer || stored == Some(Position::Chain); + let coordinates = match stored { + Some(Position::Absolute(coordinates)) => Some(coordinates), + _ => None, + }; + + let mut positions = Vec::new(); + for (step, id) in chain.iter().enumerate() { + match coordinates { + _ if joins_chain => positions.push((*id, Position::Chain)), + Some([x, y]) => positions.push((*id, Position::Absolute([x - CHAIN_WIDTH * (step as i32 + 1), y]))), + None => {} + } + } + + if let Some([x, y]) = coordinates { + let readers: Vec<(&[NodeInput], i32)> = std::iter::once((anchor_inputs.as_slice(), x)) + .chain(chain.iter().enumerate().map(|(step, id)| (added_inputs[id], x - CHAIN_WIDTH * (step as i32 + 1)))) + .collect(); + let mut feeders_below = vec![0; readers.len()]; + + for added in edits.added_nodes.iter().filter(|added| !chain.contains(&added.id)) { + let position = match readers.iter().position(|(inputs, _)| reads(inputs, added.id)) { + Some(reader) => { + feeders_below[reader] += 1; + [readers[reader].1 - CHAIN_WIDTH, y + FEEDER_ROW_HEIGHT * feeders_below[reader]] + } + None => [x, y], + }; + positions.push((added.id, Position::Absolute(position))); + } + } + + for (id, position) in positions { + if let Some(node) = target.node_instances.get_mut(&id) { + node.attributes_mut().set(POSITION, encode(&position), TimeStamp::ORIGIN); + } + } +} + +/// Apply one run's attribute writes, which may only name its anchor and the nodes it adds. +fn write_attributes(target: &mut Registry, anchor: NodeId, edits: &Edits) -> Result<(), RunnerError> { + for write in &edits.attributes { + let in_region = write.node == anchor || edits.added_nodes.iter().any(|added| added.id == write.node); + let node = target.node_instances.get_mut(&write.node).filter(|_| in_region); + let node = node.ok_or(RunnerError::AttributesOutsideRegion { anchor, node: write.node })?; + + match &write.value { + Some(value) => node.attributes_mut().set(&write.key, value.clone(), TimeStamp::ORIGIN), + None => { + node.attributes_mut().remove(&write.key); + } + } + } + + Ok(()) +} + /// Make `consumer`, named in the source layout, read `output` in the target layout. Fails when the consumer's own migration replaced or /// removed that slot, since two runs then disagree on what it should read. fn connect(target: &mut Registry, output: OutputConnector, consumer: InputConnector, input_edits: &HashMap) -> Result<(), RunnerError> { diff --git a/document/migrations/runner/src/testing.rs b/document/migrations/runner/src/testing.rs index c009680cd18..dfd86b9fe8c 100644 --- a/document/migrations/runner/src/testing.rs +++ b/document/migrations/runner/src/testing.rs @@ -3,9 +3,10 @@ use crate::node::{apply_migration, needs_whole_states, rewrite_history}; use crate::{DeclarationTable, MigrationBlobs, RunnerError, references, registry_at}; -use document_graph_storage::attributes::attr::node::ui::REFERENCE; +use document_graph_storage::attributes::attr::node::ui::{DISPLAY_NAME, IS_LAYER, POSITION, REFERENCE}; use document_graph_storage::{ - Delta, ExportSlot, Implementation, Network, NetworkId, Node, NodeId, NodeInput, PeerId, ROOT_NETWORK, Registry, RegistryDelta, Session, TimeStamp, UserId, apply_retired_op, + AttributeDelta, Delta, ExportSlot, Implementation, Network, NetworkId, Node, NodeId, NodeInput, PeerId, Position, ROOT_NETWORK, Registry, RegistryDelta, Session, TimeStamp, UserId, Value, + apply_retired_op, to_value, }; use migration_core::{Declaration, DeclarationResolver, MigrationHost, NodeMigration, decl, implements, literal, registered_migrations}; use std::collections::HashSet; @@ -138,6 +139,8 @@ pub struct SourceContract { pub literal: fn(&mut Random, usize) -> NodeInput, /// Whether a slot may read an output of another node of the source declaration. pub reads_own_output: fn(usize) -> bool, + /// Display names, besides a custom one, that generated histories give nodes, such as a default the migration clears. + pub display_names: &'static [&'static str], } impl SourceContract { @@ -146,6 +149,7 @@ impl SourceContract { input_count, literal, reads_own_output: |_| true, + display_names: &[], } } } @@ -276,11 +280,36 @@ impl Generator<'_> { self.commit_interaction(ops); } + /// A name, position, or layer flag, written or deleted. + fn attribute(&mut self) -> AttributeDelta { + let (key, value) = match self.random.below(3) { + 0 => { + let names = self.contract.display_names; + let name = match self.random.below(names.len() + 2) { + index if index < names.len() => Some(names[index]), + index if index == names.len() => Some("Renamed"), + _ => None, + }; + (DISPLAY_NAME, name.map(|name| Value::Str(name.to_string()))) + } + 1 => { + let position = match self.random.below(3) { + 0 => Position::Chain, + _ => Position::Absolute([self.random.below(20) as i32, self.random.below(20) as i32]), + }; + (POSITION, Some(to_value(&position).expect("positions serialize"))) + } + _ => (IS_LAYER, (self.random.below(2) == 0).then_some(Value::Bool(true))), + }; + + AttributeDelta { key: key.to_string(), value } + } + fn step(&mut self) { let nodes = self.live_nodes(); let root_nodes = self.live_nodes_in(ROOT_NETWORK); - match self.random.below(11) { + match self.random.below(13) { 0..=3 => { let (declaration, input_count) = if self.random.below(3) == 0 { (self.source, self.contract.input_count) @@ -335,6 +364,11 @@ impl Generator<'_> { self.session.undo().expect("an interaction is left to undo"); self.undoable -= 1; } + 11 | 12 if !nodes.is_empty() => { + let id = self.random.pick(&nodes); + let delta = self.attribute(); + self.commit(RegistryDelta::ChangeNodeAttribute { id, delta }); + } _ => {} } } diff --git a/document/migrations/runner/tests/node_migrations/attributes.rs b/document/migrations/runner/tests/node_migrations/attributes.rs new file mode 100644 index 00000000000..7255e82326f --- /dev/null +++ b/document/migrations/runner/tests/node_migrations/attributes.rs @@ -0,0 +1,137 @@ +//! Attribute writes on the anchor and the nodes it adds, and the positions added nodes get by default. + +use document_graph_storage::attr::node::ui::{DISPLAY_NAME, POSITION}; +use document_graph_storage::{NodeId, NodeInput, Position, Value, to_value}; +use migration_core::{Attributes, CHAIN_WIDTH, Downstream, FEEDER_ROW_HEIGHT, Inputs, NewNode, Node, Upstream, decl, migration}; +use migration_runner::RunnerError; +use migration_runner::testing::Fixture; + +/// Clears the anchor's old default name and places a node it adds. +#[migration("attributes::renamed"@1 => @2)] +pub fn renamed(node: &Node, downstream: &mut Downstream, attributes: &mut Attributes) { + if node.attributes().get_typed::(DISPLAY_NAME).as_deref() == Some("Old name") { + attributes.remove(node.id(), DISPLAY_NAME); + } + + let added = downstream.add_node(NewNode { + declaration: decl!("math_nodes::add"@1), + inputs: vec![], + }); + attributes.set(added, POSITION, &Position::Absolute([3, 4])); +} + +/// Renames the node its first input reads, which it neither is nor adds. +#[migration("attributes::outside"@1 => @2)] +pub fn outside(node: &Node, attributes: &mut Attributes) { + if let Some(NodeInput::Node { id, .. }) = node.inputs().first().map(|slot| &slot.input) { + attributes.set(*id, DISPLAY_NAME, &"Renamed"); + } +} + +/// Adds two nodes leading up to the anchor's first input, and one feeding the nearer of them from below. +#[migration("attributes::placed"@1 => @2)] +pub fn placed(node: &Node, inputs: &mut Inputs, upstream: &mut Upstream) { + let add = decl!("math_nodes::add"@1); + let source = node.inputs().first().map_or(NodeInput::Import { index: 0 }, |slot| slot.input.clone()); + + let far = upstream.add_node_keyed( + NewNode { + declaration: add, + inputs: vec![source], + }, + "far", + ); + let below = upstream.add_node_keyed(NewNode { declaration: add, inputs: vec![] }, "below"); + let near = upstream.add_node_keyed( + NewNode { + declaration: add, + inputs: vec![wire(far), wire(below)], + }, + "near", + ); + inputs.set(0, wire(near)); +} + +fn set_name(fixture: &mut Fixture, node: NodeId, name: &str) { + let name = Value::Str(name.to_string()); + fixture.registry.node_instances.get_mut(&node).unwrap().attributes_mut().set(DISPLAY_NAME, name, Default::default()); +} + +/// The positions of the far, near, and below nodes `Placed` adds for an anchor stored at `position`. +fn placed_around(position: Position) -> [Option; 3] { + let migration = &Placed; + let mut fixture = Fixture::new(migration); + let anchor = fixture.add(decl!("attributes::placed"@1), vec![NodeInput::Import { index: 0 }]); + let position = to_value(&position).unwrap(); + fixture.registry.node_instances.get_mut(&anchor).unwrap().attributes_mut().set(POSITION, position, Default::default()); + + let migrated = fixture.migrate(migration).unwrap(); + let position_of = |id: NodeId| migrated.node_instances[&id].attributes().get_typed::(POSITION); + + let NodeInput::Node { id: near, .. } = fixture.inputs(&migrated, anchor)[0] else { + panic!("the anchor reads the near node") + }; + let [NodeInput::Node { id: far, .. }, NodeInput::Node { id: below, .. }] = fixture.inputs(&migrated, near)[..] else { + panic!("the near node reads the far and below nodes") + }; + [position_of(far), position_of(near), position_of(below)] +} + +fn wire(id: NodeId) -> NodeInput { + NodeInput::Node { id, index: 0 } +} + +#[test] +fn a_run_writes_its_anchor_and_the_nodes_it_adds() { + let migration = &Renamed; + let mut fixture = Fixture::new(migration); + let anchor = fixture.add(decl!("attributes::renamed"@1), vec![]); + set_name(&mut fixture, anchor, "Old name"); + fixture.export(wire(anchor)); + + let migrated = fixture.migrate(migration).unwrap(); + + assert!(migrated.node_instances[&anchor].attributes().get(DISPLAY_NAME).is_none()); + let added = migrated.node_instances.iter().find(|(id, _)| **id != anchor).map(|(_, node)| node).expect("the run adds a node"); + assert_eq!(added.attributes().get_typed::(POSITION), Some(Position::Absolute([3, 4]))); +} + +#[test] +fn a_name_given_by_the_user_is_kept() { + let migration = &Renamed; + let mut fixture = Fixture::new(migration); + let anchor = fixture.add(decl!("attributes::renamed"@1), vec![]); + set_name(&mut fixture, anchor, "Mine"); + + let migrated = fixture.migrate(migration).unwrap(); + + assert_eq!(migrated.node_instances[&anchor].attributes().get_typed::(DISPLAY_NAME).as_deref(), Some("Mine")); +} + +#[test] +fn added_nodes_lead_up_to_a_placed_anchor() { + let [far, near, below] = placed_around(Position::Absolute([20, 10])); + + assert_eq!(far, Some(Position::Absolute([20 - 2 * CHAIN_WIDTH, 10]))); + assert_eq!(near, Some(Position::Absolute([20 - CHAIN_WIDTH, 10]))); + assert_eq!(below, Some(Position::Absolute([20 - 2 * CHAIN_WIDTH, 10 + FEEDER_ROW_HEIGHT]))); +} + +#[test] +fn added_nodes_join_a_chained_anchors_chain() { + let [far, near, below] = placed_around(Position::Chain); + + assert_eq!([far, near], [Some(Position::Chain); 2]); + assert_eq!(below, None); +} + +#[test] +fn writing_another_nodes_attributes_fails() { + let migration = &Outside; + let mut fixture = Fixture::new(migration); + let other = fixture.add(decl!("math_nodes::add"@1), vec![]); + fixture.add(decl!("attributes::outside"@1), vec![wire(other)]); + + let error = fixture.migrate(migration).unwrap_err(); + assert!(matches!(error, RunnerError::AttributesOutsideRegion { node, .. } if node == other), "{error}"); +} diff --git a/document/migrations/runner/tests/node_migrations/main.rs b/document/migrations/runner/tests/node_migrations/main.rs index 73e00f26f9c..373a5f68156 100644 --- a/document/migrations/runner/tests/node_migrations/main.rs +++ b/document/migrations/runner/tests/node_migrations/main.rs @@ -1,3 +1,4 @@ +mod attributes; mod blobs; mod compiler; mod examples; diff --git a/document/migrations/runner/tests/node_migrations/registration.rs b/document/migrations/runner/tests/node_migrations/registration.rs index 636d558916e..f3ee7180860 100644 --- a/document/migrations/runner/tests/node_migrations/registration.rs +++ b/document/migrations/runner/tests/node_migrations/registration.rs @@ -13,6 +13,9 @@ fn every_migration_registers_once() { sources.sort(); let mut expected = vec![ + decl!("attributes::outside"@1), + decl!("attributes::placed"@1), + decl!("attributes::renamed"@1), decl!("blending_nodes::opacity"@1), decl!("blob_nodes::text"@1), decl!("compiler::anchor"@1), diff --git a/document/migrations/snapshot/src/scaffold.rs b/document/migrations/snapshot/src/scaffold.rs index 0d53cc59c70..565ddee09f8 100644 --- a/document/migrations/snapshot/src/scaffold.rs +++ b/document/migrations/snapshot/src/scaffold.rs @@ -168,7 +168,7 @@ fn write_step(migrations: &Path, node: &NodeSource, name: &str, step: &str, from /// Describe what changed from version {from}.\n\ #[migration({step})]\n\ pub fn {name}(inputs: &mut Inputs) {{\n\ - \tcompile_error!(\"Migrate from version {from} here. Read the source state with `&Node` or `&Graph`, and edit it through `&mut Inputs`, `&mut Upstream`, or `&mut Downstream`\");\n\ + \tcompile_error!(\"Migrate from version {from} here. Read the source state with `&Node` or `&Graph`, and edit it through `&mut Inputs`, `&mut Upstream`, `&mut Downstream`, or `&mut Attributes`\");\n\ }}\n\n\ #[cfg(test)]\n\ mod tests {{\n\ diff --git a/editor/src/messages/portfolio/document/data_panel/data_panel_message_handler.rs b/editor/src/messages/portfolio/document/data_panel/data_panel_message_handler.rs index 37c788ed16d..4cea6462595 100644 --- a/editor/src/messages/portfolio/document/data_panel/data_panel_message_handler.rs +++ b/editor/src/messages/portfolio/document/data_panel/data_panel_message_handler.rs @@ -618,7 +618,7 @@ impl TableItemLayout for Item { } } - let attribute_keys: Vec = self.attributes().keys().map(str::to_string).collect(); + let attribute_keys: Vec = self.attributes().keys().filter(|key| !key.starts_with(INTERNAL_ATTRIBUTE_PREFIX)).map(str::to_string).collect(); // A single element, so no leading ID column, unlike the `List` table let saved_gradient_settings = data.gradient_settings; diff --git a/node-graph/rfcs/document-format-migrations.md b/node-graph/rfcs/document-format-migrations.md index 06d452ceec7..12583e8fc29 100644 --- a/node-graph/rfcs/document-format-migrations.md +++ b/node-graph/rfcs/document-format-migrations.md @@ -74,10 +74,13 @@ The function runs once per **anchor**: a node of the source declaration. The run | `&mut Inputs` | the anchor's input slots | `set`, `insert`, `push`, `remove`, `truncate` | | `&mut Upstream` | new and existing nodes upstream of the anchor | `add_node`, `add_node_keyed`, `remove_node` | | `&mut Downstream` | the slots and exports reading the anchor's outputs, plus new downstream nodes | `consumers`, `add_node`, `add_node_keyed`, `connect` | +| `&mut Attributes` | attributes of the anchor and of the nodes the run adds | `set`, `remove` | | `&mut Resources` | document resources | `add_embedded` | `Inputs` operations apply in order to the target layout being built. `node.inputs()` is always the source layout. Reads through a mutable view (such as `Downstream::consumers`) also see the source state, so nodes added by the migration never appear among the anchor's consumers. +The runner places added nodes from the anchor's own position. Nodes leading up to the anchor's chain input form a row, or join the chain when the anchor is chained or a layer. Other nodes feeding the anchor or that row sit below the first one reading them, and the rest sit on the anchor. A chained anchor has no coordinates of its own, so only its chain is placed, and an anchor with no stored position gets nothing placed. A migration overrides these positions, or clears a stored name that no longer fits the anchor, through `Attributes`. + A migration addresses declarations with `decl!`, values with a frozen mirror of the value variants it uses, and connections with plain connector types: ```rust @@ -538,7 +541,7 @@ document/migrations/ - **Input attributes of inserted nodes.** `NewNode` takes bare `NodeInput`s, so inserted nodes start with empty per-input attributes. Taking `InputSlot`s would let a migration carry them over. - **Removal safety.** `Upstream::remove_node` is only correct when nothing else reads the removed node, which the region system can't check. - **`unrepresentable` in CI.** Whether reaching it in the commutation test fails CI outright. -- **Layout.** Whether migrations may write node positions and chain placement, or leave inserted nodes to a host-side layout pass. +- **Layout.** Migrations position the nodes they add and can move the anchor, but can't move other existing nodes, so an inserted node can overlap what sits upstream. Positions stored relative to each node's consumers would move the upstream without writing it. - **Multiple pending migrations.** Whether consecutive pending node migrations run as separate rewrite passes or are fused into one replay. - **Migrations reading a subgraph.** A migration that inspects an instance's inner nodes depends on their versions, which change independently of the definition's. The planned answer is a cascading bump: the API dump lists a definition's inner `identifier@version`s, so bumping an inner node also requires bumping every definition containing it, and pins order that node's migrations around the definition's. Nothing reads a subgraph yet, so neither a read view for it nor the cascading bump exists. - **Detaching.** Whether a migration may clear an edited instance's reference, keeping it as a custom subgraph, instead of collapsing it. diff --git a/website/content/volunteer/guide/graphene/document-migrations.md b/website/content/volunteer/guide/graphene/document-migrations.md index de52d7aa06e..3a31b2592c7 100644 --- a/website/content/volunteer/guide/graphene/document-migrations.md +++ b/website/content/volunteer/guide/graphene/document-migrations.md @@ -88,6 +88,7 @@ You ask for what you need through the function's parameters, in any order. The f | `&mut Upstream` | Nodes feeding the anchor | `add_node`, `add_node_keyed`, `remove_node` | | `&mut Downstream` | Whatever reads the anchor's outputs | `consumers`, `add_node`, `add_node_keyed`, `connect` | | `&mut Target` | Which declaration the anchor becomes, see [Choosing a target](#choosing-a-target) | `set` | +| `&mut Attributes` | Attributes of the anchor and of the nodes you add, see [Placing and naming nodes](#placing-and-naming-nodes) | `set`, `remove` | Reads always see the old document. `node.inputs()` returns the old layout however many edits you've made, and nodes you add never show up in `consumers`. `Inputs` edits apply in order to the layout being built. After `inputs.insert(1, ...)`, later `Inputs` calls see the old slot 1 as slot 2. `node.inputs()[1]` is still the old slot 1. @@ -204,6 +205,18 @@ pub fn vary_from_style(node: &Node, target: &mut Target) { A wire written to a removed node brings that node back, so avoid switching a wire on and off depending on state. Keep the slot reading the same thing, and add nodes downstream of the anchor instead. +### Placing and naming nodes + +Nodes you add are placed beside the anchor for you, from the anchor's own stored position. Those leading up to the anchor's primary input form a row to its left, or join its layer's chain. Others feeding the anchor or that row sit below the first one reading them, and the rest start on the anchor. A chained anchor has no coordinates of its own, so only its chain is placed, and an anchor with no stored position gets nothing placed. `Attributes::set` writes an attribute of the anchor or of a node this run adds, named by the ID `add_node` returned, and `remove` deletes one. A display name stored as the old node's default would misname the new one, so clear it: + +```rust +if node.attributes().get_typed::(DISPLAY_NAME).as_deref() == Some("Copy to Points") { + attributes.remove(node.id(), DISPLAY_NAME); +} +``` + +Writing to any other node fails, since another anchor's run may be editing it. + ### Network definitions Some built-in nodes, like Merge and Artboard, are subgraphs. Each instance holds its own copy of a network defined in `document_node_definitions.rs`, and stores the definition's identifier and `version` next to it. Bump `version` on the `DocumentNodeDefinition` when instances saved before your change would load wrongly, and register a migration from the old version with the `network` prefix: