harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0339harbor download openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0339Harder task: Implement a thread-safe, in-memory tree with dynamic re-rooting and full subtree registration under a Python package located at /app/.
Overview
- Create a self-contained Python package at /app/trio_vis with:
- __init__.py
- trio_fake.py (defining FakeNode)
- Tests expect to import FakeNode from trio_vis.trio_fake, and rely on internal attributes (_nodes and _tree_root) being accessible.
What to implement
- Class and constructor
- class FakeNode
- def __init__(self, name: str)
- Attributes (accessible by tests):
- name: str
- _nodes: Dict[str, FakeNode] # descendants visible under this node
- _tree_root: FakeNode # reference to the current root of the tree this node belongs to (initially self)
- _lock: threading.RLock # per-tree lock for thread-safety
- Core operations and behavior
1) tree_add(self, node: "FakeNode", parent_name: str) -> None
- Attach the given node under the parent identified by parent_name within the same root’s tree.
- Deterministic behavior required by tests:
- root = self._tree_root
- Find parent in the root:
- If parent_name == root.name, parent = root
- Else, parent = root._nodes.get(parent_name)
- Validation and error handling:
- If parent cannot be found, raise an appropriate error (ValueError recommended).
- If node.name already exists in root._nodes, raise ValueError to avoid name collisions.
- If attaching would create a cycle (e.g., attaching an ancestor under its descendant), raise ValueError.
- Attach:
- parent._nodes[node.name] = node
- node._tree_root = root
- Register the entire subtree under the new node in the root’s _nodes so that the root view includes all descendants:
- _register_in_root(root, node)
- Thread-safety:
- Acquire root._lock (and use a reentrant lock) during the operation to ensure consistency.
2) tree_remove(self, name: str) -> None
- Remove the node with the given name (and its descendants) from the root’s tree, removing entries from root._nodes.
- Implementation:
- root = self._tree_root
- If name in root._nodes:
- node = root._nodes.pop(name)
- _remove_subtree_from_root(root, node)
- Helper:
- _remove_subtree_from_root(root, node) recursively removes node and its descendants from root._nodes
- Thread-safety:
- Acquire root._lock during removal.
3) Property tree_root with getter and setter
- Getter: return self._tree_root
- Setter: assign a new root for this node’s subtree with the following semantics:
- If new_root is None: raise RuntimeError
- old_root = self._tree_root
- If new_root is self (re-root under itself):
- If old_root is not None and old_root is not self:
- _remove_subtree_from_root(old_root, self)
- self._tree_root = self
- Else (re-root under some other node new_root):
- If old_root is not None and old_root is not new_root:
- _remove_subtree_from_root(old_root, self)
- self._tree_root = new_root
- _register_in_root(new_root, self)
- Thread-safety:
- Setter must acquire the appropriate lock to ensure consistency during re-rooting.
Helpers (internal; not part of public API)
- _register_in_root(root: FakeNode, node: FakeNode) -> None
- Recursively add the node and all its descendants to root._nodes
- _remove_subtree_from_root(root: FakeNode, node: FakeNode) -> None
- Recursively remove the node and all its descendants from root._nodes
- Optional: maintain or clear _tree_root references for removed nodes (tests do not rely on this; behavior is flexible)
constraints, edge cases, and requirements
- Performance and complexity
- tree_add: locating parent is O(depth); registering the subtree is O(size_of_subtree) in the worst case
- tree_remove: removal is O(size_of_subtree)
- tree_root re-rooting may involve recursive registration/removal across the entire affected subtree
- Use per-tree locking to ensure operations are thread-safe; aim for minimal contention on the rest of the tree
- Thread-safety
- All mutating operations (tree_add, tree_remove, tree_root setter) must acquire a per-tree lock to prevent data races
- Robustness and edge cases
- Unicode and special characters in names must be supported (use as dict keys)
- Duplicate names under the same root must raise ValueError
- Attempting to attach a node in a way that would create a cycle must raise ValueError
- tree_root setter None must raise RuntimeError
- Design choices
- The root view (root._nodes) must reflect the entire set of visible descendants after each operation
- Re-rooting must detach from the old root view and attach to the new root view, updating _tree_root on all affected nodes
- Public API and imports
- Package path must be /app/trio_vis/
- trio_fake.py must define class FakeNode
- Tests import FakeNode from trio_vis.trio_fake, so ensure module path is correct
- Language and tooling
- Implement in Python 3.8+ (type hints allowed)
- The solution should be self-contained within /app/
Deliverables
- A fully implemented Python package at /app/trio_vis/
- __init__.py
- trio_fake.py (defining FakeNode with the interface and behavior described)
- The implementation must satisfy the tests described above, including internal state access to _nodes and _tree_root, and the re-rooting semantics with subtree registration/removal.