Spaces:
Sleeping
Sleeping
Update lineage_tracker.py
Browse files- lineage_tracker.py +38 -5
lineage_tracker.py
CHANGED
|
@@ -1,15 +1,48 @@
|
|
|
|
|
| 1 |
# Author: Liam Grinstead
|
| 2 |
-
# Tracks symbolic agent lineage
|
| 3 |
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
if parent_id not in LINEAGE:
|
| 8 |
LINEAGE[parent_id] = []
|
| 9 |
LINEAGE[parent_id].append({
|
| 10 |
"child_id": child_id,
|
| 11 |
"mutation": mutation_profile
|
| 12 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# lineage_tracker.py
|
| 2 |
# Author: Liam Grinstead
|
| 3 |
+
# Tracks symbolic agent lineage, mutation history, and parent mapping
|
| 4 |
|
| 5 |
+
from typing import Dict, List
|
| 6 |
|
| 7 |
+
LINEAGE: Dict[str, List[dict]] = {}
|
| 8 |
+
PARENT_OF: Dict[str, str] = {} # child_id -> parent_id
|
| 9 |
+
|
| 10 |
+
def register_lineage(parent_id: str, child_id: str, mutation_profile: dict):
|
| 11 |
if parent_id not in LINEAGE:
|
| 12 |
LINEAGE[parent_id] = []
|
| 13 |
LINEAGE[parent_id].append({
|
| 14 |
"child_id": child_id,
|
| 15 |
"mutation": mutation_profile
|
| 16 |
})
|
| 17 |
+
PARENT_OF[child_id] = parent_id
|
| 18 |
+
|
| 19 |
+
def get_lineage(agent_id: str) -> List[dict]:
|
| 20 |
+
return LINEAGE.get(agent_id, [])
|
| 21 |
+
|
| 22 |
+
def get_parent(child_id: str) -> str:
|
| 23 |
+
return PARENT_OF.get(child_id, "")
|
| 24 |
+
|
| 25 |
+
def get_ancestors(agent_id: str) -> List[str]:
|
| 26 |
+
chain = []
|
| 27 |
+
cur = agent_id
|
| 28 |
+
while cur in PARENT_OF:
|
| 29 |
+
p = PARENT_OF[cur]
|
| 30 |
+
chain.append(p)
|
| 31 |
+
cur = p
|
| 32 |
+
return chain[::-1] # oldest → newest
|
| 33 |
|
| 34 |
+
def get_descendants(agent_id: str, depth: int = 5) -> Dict[str, List[str]]:
|
| 35 |
+
# returns a dict level -> list of child ids
|
| 36 |
+
levels: Dict[int, List[str]] = {}
|
| 37 |
+
frontier = [agent_id]
|
| 38 |
+
for d in range(1, depth + 1):
|
| 39 |
+
next_frontier = []
|
| 40 |
+
for node in frontier:
|
| 41 |
+
children = [c["child_id"] for c in LINEAGE.get(node, [])]
|
| 42 |
+
if children:
|
| 43 |
+
levels.setdefault(d, []).extend(children)
|
| 44 |
+
next_frontier.extend(children)
|
| 45 |
+
frontier = next_frontier
|
| 46 |
+
if not frontier:
|
| 47 |
+
break
|
| 48 |
+
return {f"level_{k}": v for k, v in levels.items()}
|