fix: the cell memory sees every write to a shape's semantic state, not a list of function names

apply_tag, set_metadata and auto_tag_faces change the shape they are given and return that same object. The memory
watched two of them by name and missed apply_tag, so after a cell tagged a shape it had received, the editor handed out
the producing cell's object with the tag on it: edit the tag in an annotate block and the shape kept the old tag too,
where a cold run has only the new one.

Every root of shapes now counts its writes (Registry::revision, bumped in the one write door Shape::writable_state and in
the removals that bypass it; AnyShape._semantic_revision). A memory entry records the counts of the shapes in its values
(a Part is its body, an Assembly its components) and is dropped when they moved. Reading a shape and consuming it in an
operator (cut, union, fillet, translate, make_part) do not count: the six-axis arm still recomputes only the two cells
that depend on the changed joint and restores the rest from memory.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NiJingzhe
2026-10-30 17:53:37 +08:00
parent e06a4db3d5
commit e0533ccd2a
8 changed files with 156 additions and 64 deletions
+4 -1
View File
@@ -133,7 +133,10 @@ void bind_common(Cls& cls) {
} // namespace
void bind_topo(py::module_& m) {
py::class_<Shape>(m, "AnyShape").def_property_readonly("topo_id", &Shape::topo_id);
py::class_<Shape>(m, "AnyShape")
.def_property_readonly("topo_id", &Shape::topo_id)
// How many writes to semantic state the root of this shape has had: equal before and after means unchanged.
.def("_semantic_revision", &Shape::semantic_revision);
auto vertex = py::class_<Vertex, Shape>(m, "Vertex");
vertex.def("get_coordinates", &Vertex::coordinates).def("get_incident_edges", &Vertex::incident_edges);
+5
View File
@@ -79,6 +79,9 @@ class Shape {
std::shared_ptr<const void> graph_node() const;
// A key equal for exactly the handles for which same_entity holds (the hash of a handle).
std::size_t entity_key() const noexcept;
// How many writes to semantic state (tags, bindings, lineage, metadata) the entities of this handle's root have had.
// Equal numbers before and after mean nobody changed the shape in between.
std::uint64_t semantic_revision() const noexcept;
template <class T>
T as() const {
@@ -200,6 +203,8 @@ class Shape {
TopoDS_Shape shape_;
std::shared_ptr<Entity> entity_;
std::shared_ptr<Registry> registry_;
// The write door of the semantic state: counts the write (see semantic_revision).
SemanticState& writable_state();
};
// ---------------------------------------------------------------------------
+9
View File
@@ -2,6 +2,8 @@
#pragma once
#include <algorithm>
#include <atomic>
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
@@ -271,6 +273,12 @@ class Registry {
explicit Registry(TopoDS_Shape root) : root_(std::move(root)) {}
const TopoDS_Shape& root() const noexcept { return root_; }
// Counts the writes to the semantic state (tags, bindings, lineage, metadata) of any entity of this root. A holder
// of a shape that must know whether somebody changed it since (the editor kernel's memory of what a cell made)
// compares the number it read before with the number now; an operation that only reads a shape leaves it alone.
std::uint64_t revision() const noexcept { return revision_.load(std::memory_order_relaxed); }
void touch() noexcept { revision_.fetch_add(1, std::memory_order_relaxed); }
std::shared_ptr<Entity> acquire(Kind kind, const TopoDS_Shape& shape);
// Ancestor relations of the root, built on first use: for each `child`
@@ -310,6 +318,7 @@ class Registry {
};
TopoDS_Shape root_;
std::atomic<std::uint64_t> revision_{0};
Table tables_[7];
std::vector<std::unique_ptr<AncestorTable>> ancestor_tables_;
};
+5 -1
View File
@@ -68,7 +68,7 @@ bool Shape::same_entity(const Shape& other) const noexcept {
}
void Shape::set_graph_node(std::shared_ptr<const void> node) const {
entity_->state().graph_node = std::move(node);
entity_->state().graph_node = std::move(node); // a link for the recorder, not a change a reader of the shape sees
}
std::shared_ptr<const void> Shape::graph_node() const {
const SemanticState* state = entity_->state_if();
@@ -79,6 +79,10 @@ std::size_t Shape::entity_key() const noexcept {
return std::hash<const void*>{}(entity_.get()) * 31u + std::hash<const void*>{}(registry_.get());
}
std::uint64_t Shape::semantic_revision() const noexcept {
return registry_->revision();
}
// Shape::clone_view lives with the rest of the semantic state, in shape_tags.cpp.
std::vector<Shape> Shape::known_parents() const {
+23 -12
View File
@@ -153,9 +153,14 @@ bool any_downward(const std::vector<TagBinding>& bindings) {
// ---------------------------------------------------------------------------
// Storing bindings
// ---------------------------------------------------------------------------
SemanticState& Shape::writable_state() {
registry_->touch();
return entity_->state();
}
void Shape::add_binding(TagBinding binding) {
binding.validate();
SemanticState& state = entity_->state();
SemanticState& state = writable_state();
const bool duplicate = std::any_of(state.bindings.begin(), state.bindings.end(),
[&](const TagBinding& b) { return b.binding_id == binding.binding_id; });
if (duplicate)
@@ -189,7 +194,7 @@ void Shape::add_tag_lineage(const TagBinding& binding, LineageDerivation derivat
void Shape::add_lineage_witness(TagLineageWitness witness) {
witness.validate();
SemanticState& state = entity_->state();
SemanticState& state = writable_state();
const bool duplicate = std::any_of(state.lineage.begin(), state.lineage.end(),
[&](const TagLineageWitness& w) { return same_witness(w, witness); });
if (!duplicate)
@@ -204,6 +209,7 @@ std::size_t Shape::remove_tag_bindings(std::string_view tag, TagProducerKind mat
const auto doomed = [&](const TagBinding& b) { return b.tag == normalized && b.producer().kind == matching; };
const auto removed = static_cast<std::size_t>(std::count_if(state->bindings.begin(), state->bindings.end(), doomed));
if (removed != 0) {
registry_->touch();
state->bindings.erase(std::remove_if(state->bindings.begin(), state->bindings.end(), doomed),
state->bindings.end());
}
@@ -218,6 +224,7 @@ std::size_t Shape::remove_tag_bindings(std::string_view tag, const TagProducer&
const auto doomed = [&](const TagBinding& b) { return b.tag == normalized && b.producer() == matching; };
const auto removed = static_cast<std::size_t>(std::count_if(state->bindings.begin(), state->bindings.end(), doomed));
if (removed != 0) {
registry_->touch();
state->bindings.erase(std::remove_if(state->bindings.begin(), state->bindings.end(), doomed),
state->bindings.end());
}
@@ -240,13 +247,16 @@ bool Shape::remove_tag_binding(std::string_view binding_id) {
state->bindings.erase(std::remove_if(state->bindings.begin(), state->bindings.end(),
[&](const TagBinding& b) { return b.binding_id == binding_id; }),
state->bindings.end());
return state->bindings.size() != before;
const bool removed = state->bindings.size() != before;
if (removed)
registry_->touch();
return removed;
}
void Shape::replace_local_tag_bindings(std::vector<TagBinding> bindings) {
for (const auto& b : bindings)
b.validate();
SemanticState& state = entity_->state();
SemanticState& state = writable_state();
state.bindings.erase(std::remove_if(state.bindings.begin(), state.bindings.end(), is_local), state.bindings.end());
if (any_downward(bindings))
registry_->has_downward_bindings = true;
@@ -259,7 +269,7 @@ void Shape::set_legacy_tags(const std::vector<std::string>& tags) {
imported.reserve(tags.size());
for (const auto& tag : tags)
imported.push_back(legacy_tag_binding(normalize_tag(tag, true), "Imported from a direct legacy _tags assignment."));
SemanticState& state = entity_->state();
SemanticState& state = writable_state();
state.bindings = std::move(imported);
state.lineage.clear();
}
@@ -510,7 +520,7 @@ std::vector<Value> Shape::explain_tag(std::string_view tag, TagScope scope) cons
// Metadata and runtime values
// ---------------------------------------------------------------------------
void Shape::set_metadata(std::string key, Value value) {
entity_->state().set_value(false, std::move(key), std::move(value));
writable_state().set_value(false, std::move(key), std::move(value));
}
bool Shape::has_metadata_key(std::string_view key) const {
@@ -519,7 +529,7 @@ bool Shape::has_metadata_key(std::string_view key) const {
}
void Shape::set_metadata_deferred(std::string_view key, const Value& value) {
SemanticState& state = entity_->state();
SemanticState& state = writable_state();
if (state.has_key(false, key))
state.metadata().set(std::string(key), value);
else
@@ -527,7 +537,7 @@ void Shape::set_metadata_deferred(std::string_view key, const Value& value) {
}
void Shape::set_metadata_recipe(std::string_view key, unsigned char recipe, const Value& args) {
SemanticState& state = entity_->state();
SemanticState& state = writable_state();
if (state.has_key(false, key))
state.metadata().set(std::string(key), PackedValue::from_recipe(recipe, args).unpack());
else
@@ -535,7 +545,7 @@ void Shape::set_metadata_recipe(std::string_view key, unsigned char recipe, cons
}
void Shape::set_runtime_deferred(std::string_view key, const Value& value) {
SemanticState& state = entity_->state();
SemanticState& state = writable_state();
if (state.has_key(true, key))
state.runtime().set(std::string(key), value);
else
@@ -559,7 +569,7 @@ const Value& Shape::metadata() const {
}
void Shape::set_runtime(std::string key, Value value) {
entity_->state().set_value(true, std::move(key), std::move(value));
writable_state().set_value(true, std::move(key), std::move(value));
}
Value Shape::get_runtime(std::string_view key, Value fallback) const {
@@ -585,12 +595,13 @@ void Shape::copy_tag_state_from(const Shape& source) {
const SemanticState* from = source.entity_->state_if();
if (from == nullptr) {
if (SemanticState* to = entity_->sem.get()) {
registry_->touch();
to->bindings.clear();
to->lineage.clear();
}
return;
}
SemanticState& to = entity_->state();
SemanticState& to = writable_state();
to.bindings = from->bindings;
to.lineage = from->lineage;
if (any_downward(to.bindings))
@@ -602,7 +613,7 @@ void Shape::copy_tag_state_from(const Shape& source) {
void Shape::copy_semantic_state_from(const Shape& source) {
copy_tag_state_from(source);
const SemanticState* from = source.entity_->state_if();
SemanticState& to = entity_->state();
SemanticState& to = writable_state();
to.clear_dictionaries();
if (from == nullptr)
return;