Genealogy
genealogy ¶
Genealogy tracking and ancestor queries for merge operations.
This module provides functions for tracking parent-child relationships between candidates and finding common ancestors, enabling merge operations that combine improvements from different evolutionary branches.
| ATTRIBUTE | DESCRIPTION |
|---|---|
get_ancestors | Return all ancestor indices for a candidate. TYPE: |
find_common_ancestor | Find the most recent common ancestor of two candidates. TYPE: |
Examples:
Getting all ancestors of a candidate:
from gepa_adk.engine.genealogy import get_ancestors
parent_indices = {0: [None], 1: [0], 2: [1]}
ancestors = get_ancestors(2, parent_indices)
# Returns: {0, 1}
Finding common ancestor:
from gepa_adk.engine.genealogy import find_common_ancestor
parent_indices = {0: [None], 1: [0], 2: [0], 3: [1, 2]}
ancestor = find_common_ancestor(1, 2, parent_indices)
# Returns: 0
Note
This module provides genealogy tracking functions for merge operations. Genealogy tracking enables merge operations by identifying which candidates share common ancestry and can be safely combined.
get_ancestors ¶
Return all ancestor indices for a candidate.
Traverses the genealogy tree using breadth-first search to find all ancestors transitively. Seed candidates (with [None] parents) have no ancestors.
| PARAMETER | DESCRIPTION |
|---|---|
candidate_idx | Index of the candidate to trace. TYPE: |
parent_indices | Mapping of candidate index to parent indices list. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
set[int] | Set of all ancestor candidate indices (excluding the candidate itself). |
Examples:
Simple linear genealogy:
parent_indices = {0: [None], 1: [0], 2: [1]}
ancestors = get_ancestors(2, parent_indices)
# Returns: {0, 1}
Merge candidate with two parents:
parent_indices = {0: [None], 1: [0], 2: [0], 3: [1, 2]}
ancestors = get_ancestors(3, parent_indices)
# Returns: {0, 1, 2}
Note
Operations use BFS to avoid recursion depth issues with deep genealogies. Prevents cycles by tracking visited nodes.
Source code in src/gepa_adk/engine/genealogy.py
find_common_ancestor ¶
find_common_ancestor(
idx1: int,
idx2: int,
parent_indices: dict[int, list[int | None]],
) -> int | None
Find the most recent common ancestor of two candidates.
Identifies the common ancestor with the highest index (most recent) between two candidates. Returns None if no common ancestor exists (separate lineages).
| PARAMETER | DESCRIPTION |
|---|---|
idx1 | First candidate index. TYPE: |
idx2 | Second candidate index. TYPE: |
parent_indices | Mapping of candidate index to parent indices list. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
int | None | Index of the most recent common ancestor, or None if no common ancestor exists. |
Examples:
Candidates sharing a seed:
parent_indices = {0: [None], 1: [0], 2: [0]}
ancestor = find_common_ancestor(1, 2, parent_indices)
# Returns: 0
One candidate is ancestor of the other:
parent_indices = {0: [None], 1: [0], 2: [1]}
ancestor = find_common_ancestor(1, 2, parent_indices)
# Returns: 1 (1 is ancestor of 2)
No common ancestor:
parent_indices = {0: [None], 1: [0], 2: [None], 3: [2]}
ancestor = find_common_ancestor(1, 3, parent_indices)
# Returns: None (separate lineages)
Note
Operations return the highest-indexed common ancestor to ensure we find the most recent shared ancestor, which is most useful for merge operations.
Source code in src/gepa_adk/engine/genealogy.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
filter_ancestors_by_score ¶
filter_ancestors_by_score(
ancestors: set[int],
candidate_scores: dict[int, dict[int, float]],
min_avg_score: float,
) -> set[int]
Filter ancestors by minimum average score constraint.
Removes ancestors that don't meet the minimum average score requirement, ensuring only viable ancestors are considered for merge operations.
| PARAMETER | DESCRIPTION |
|---|---|
ancestors | Set of ancestor candidate indices to filter. TYPE: |
candidate_scores | Mapping of candidate index to per-example scores. TYPE: |
min_avg_score | Minimum average score threshold. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
set[int] | Set of ancestor indices that meet the score constraint. |
Examples:
Filtering ancestors by score:
ancestors = {0, 1, 2}
candidate_scores = {
0: {0: 0.5, 1: 0.5}, # avg: 0.5
1: {0: 0.7, 1: 0.7}, # avg: 0.7
2: {0: 0.9, 1: 0.9}, # avg: 0.9
}
filtered = filter_ancestors_by_score(
ancestors, candidate_scores, min_avg_score=0.6
)
# Returns: {1, 2} (0 filtered out)
Note
Operations exclude ancestors without scores from the result.
Source code in src/gepa_adk/engine/genealogy.py
detect_component_divergence ¶
detect_component_divergence(
ancestor_components: dict[str, str],
parent_components: dict[str, str],
) -> set[str]
Detect which components have diverged from ancestor to parent.
Identifies component keys where the parent's value differs from the ancestor's value, indicating where improvements or changes occurred.
| PARAMETER | DESCRIPTION |
|---|---|
ancestor_components | Component dictionary from ancestor candidate. TYPE: |
parent_components | Component dictionary from parent candidate. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
set[str] | Set of component keys that have diverged (changed values). |
Examples:
Detecting divergence:
ancestor = {"instruction": "A", "output_schema": "B"}
parent = {"instruction": "A", "output_schema": "C"}
divergence = detect_component_divergence(ancestor, parent)
# Returns: {"output_schema"}
Note
Only checks components present in the ancestor. Components added by the parent are ignored. Missing components are not considered diverged.
Source code in src/gepa_adk/engine/genealogy.py
has_desirable_predictors ¶
has_desirable_predictors(
ancestor_components: dict[str, str],
parent1_components: dict[str, str],
parent2_components: dict[str, str],
) -> bool
Check if merge has desirable complementary component changes.
A merge is desirable when parents have changed different components from the ancestor, indicating complementary improvements that can be combined.
| PARAMETER | DESCRIPTION |
|---|---|
ancestor_components | Component dictionary from common ancestor. TYPE: |
parent1_components | Component dictionary from first parent. TYPE: |
parent2_components | Component dictionary from second parent. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
bool | True if parents have complementary component changes, False otherwise. |
Examples:
Complementary changes (desirable):
ancestor = {"instruction": "A", "output_schema": "B"}
parent1 = {"instruction": "A", "output_schema": "C"} # output_schema changed
parent2 = {"instruction": "D", "output_schema": "B"} # instruction changed
assert has_desirable_predictors(ancestor, parent1, parent2) is True
Overlapping changes (less desirable):
ancestor = {"instruction": "A", "output_schema": "B"}
parent1 = {"instruction": "C", "output_schema": "B"}
parent2 = {"instruction": "C", "output_schema": "B"} # Same change
assert has_desirable_predictors(ancestor, parent1, parent2) is False
Note
Operations return False if no components have changed, or if both parents changed the same components identically.
Source code in src/gepa_adk/engine/genealogy.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |