Tree Reconstruction
This script reconstructs the hierarchical path (branch) from the root node to a specified target node within a filesystem-based tree structure. It identifies nodes using UUIDs stored in `node.json` files and traces parent-child relationships to build the branch representation.
This script reconstructs the hierarchical path (branch) from the root node to a specified target node within a filesystem-based tree structure. It identifies nodes using UUIDs stored in node.json files and traces parent-child relationships to build the branch representation.
Purpose¶
The primary purpose of reconstructor.py is to extract and visualize a specific lineage within a larger, potentially complex tree structure stored across directories and JSON files. Given a target node’s UUID, it finds the node and traces its ancestry back to the root, outputting this specific branch as a structured JSON object.
Usage¶
Run the script from the command line:
python reconstructor.py <node_uuid> [search_directory] [output_json]
<node_uuid>: (Required) The unique identifier (UUID) of the target node from which to reconstruct the tree branch.[search_directory]: (Optional) The directory where the script should search recursively fornode.jsonfiles. Defaults tooutput/if not specified.[output_json]: (Optional) The file path where the resulting JSON output should be saved. If omitted, a path is automatically generated within theoutput/reconstructor/directory using a timestamp and a new UUID.
Examples:
# Reconstruct from UUID, search in default 'output/' directory
python reconstructor.py e2dd9b38ab194156
# Reconstruct from UUID, search in a specific directory
python reconstructor.py e2dd9b38ab194156 output/hallucinate-tree
# Reconstruct, search in specific directory, and save to a specific file
python reconstructor.py e2dd9b38ab194156 output/hallucinate-tree my_tree.json
Input (Filesystem)¶
The script expects a directory structure, typically generated by other scripts like hallucinate-tree.py or expand-node.py. This structure consists of:
- A base search directory (e.g.,
output/). - Nested subdirectories, where each directory potentially represents a node in the tree.
node.jsonfiles within these directories. Eachnode.jsonfile should contain at least:"uuid": A unique string identifier for the node."step": A string describing the node or step it represents.
- For nodes that are not the root of the tree, the
node.jsonfile must also contain:"parent_uuid": The UUID of the parent node, linking it in the hierarchy.
Key Functions¶
find_node_by_uuid(search_uuid, base_dir): Recursively walks thebase_dir, opensnode.jsonfiles, and returns the parsed data of the node matching thesearch_uuid.get_parent_chain(node_data, search_dir): Starts with the targetnode_dataand iteratively usesfind_node_by_uuidto find the parent node based onparent_uuid. It continues this process until it reaches a node without aparent_uuid(the root). Returns an ordered list of node data dictionaries representing the path from the root to the target node.build_tree_branch(node_chain): Takes the ordered list fromget_parent_chainand constructs a nested dictionary structure that mirrors the hierarchical branch. Each level contains thestepanduuidof a node and achildrenlist (which, in this reconstructed branch, will contain at most one child).parse_command_args(): Parses command-line arguments (sys.argv) to extract thenode_uuid,search_directory, andoutput_jsonpath, handling optional arguments and defaults.main(): The main execution function. It coordinates the process: callsparse_command_args, sets defaults, callsfind_node_by_uuid,get_parent_chain, andbuild_tree_branch, generates metadata usingutils.create_output_metadata, prepares the final output dictionary, determines the output file path usingutils.get_output_filepath, and saves the result usingutils.save_output.utilsfunctions: Relies on helper functions fromutils.pyfor saving output (save_output), creating standard metadata (create_output_metadata), and determining the output file path (get_output_filepath).
Reconstruction Logic¶
- Find Target Node: The script starts by searching the specified
search_directory(oroutput/by default) recursively for anode.jsonfile whoseuuidmatches the provided<node_uuid>. This is done by thefind_node_by_uuidfunction. - Trace Ancestry: Once the target node is found, the
get_parent_chainfunction takes its data. It looks for theparent_uuidfield and callsfind_node_by_uuidagain to locate the parent node’s data. This process repeats, moving up the hierarchy usingparent_uuidlinks, until a node without aparent_uuidis encountered (this is considered the root of the branch). This results in an ordered list of all nodes from the root down to the target node. - Build Branch Structure: The
build_tree_branchfunction takes the ordered list of nodes. It creates a nested dictionary structure where each node from the list becomes a level in the dictionary. The root node forms the base, and subsequent nodes are added as children to the previous node, effectively recreating the specific branch’s hierarchy.
Output¶
The script generates a JSON file containing the reconstructed tree branch and associated metadata. The structure of the JSON output is:
{
"uuid": "...", // UUID generated for this reconstruction process
"task": "Tree Reconstruction", // Name of the task
"date_created": "...", // Timestamp of creation
"time_taken": "...", // Time elapsed for the script execution
"source_uuid": "...", // The target node UUID provided as input
"search_directory": "...", // The directory searched for nodes
"path_length": N, // Integer: Number of nodes in the reconstructed branch
"reconstructed_tree": { // Nested dictionary representing the branch
"step": "Root Node Step",
"uuid": "root-node-uuid",
"children": [
{
"step": "Child Node Step",
"uuid": "child-node-uuid",
"children": [
// ... further nested nodes down to the target node
{
"step": "Target Node Step",
"uuid": "target-node-uuid",
"children": []
}
]
}
]
}
}
The reconstructed_tree provides a clear, hierarchical view of the path from the root node down to the specific node identified by the source_uuid.