def find_ancestors_of_type(
    state: state_def.State, objkey: str, filter_type: r.Domain, seen: set = None
) -> set[str]:
    """
    Find objkeys of all ancestors of `objkey` which match `filter_type`

    Object `A` is a parent of `objkey` if there exists a sequence of objects `A, B, C, ..., objkey`
    where A is in B's relations, B is in C's relations, etc, AND only `A` matches the filter

    Returns
    -------
    parents: set(str)
        objkeys of objects of the given type which are parents in the relation graph

    """

    if seen is None:
        seen = set()

    seen.add(objkey)

    obj = state.objs[objkey]

    if domain_contains.domain_contains(filter_type, state, obj):
        return {objkey}

    result = set()
    for rel in obj.relations:
        if rel.target_name in seen:
            continue

        result.update(find_ancestors_of_type(state, rel.target_name, filter_type, seen))

    return result

    Find objkeys of all ancestors of `objkey` which match `filter_type`

    Object `A` is a parent of `objkey` if there exists a sequence of objects `A, B, C, ..., objkey`
    where A is in B's relations, B is in C's relations, etc, AND only `A` matches the filter

    Returns
    -------
    parents: set(str)
        objkeys of objects of the given type which are parents in the relation graph

    
infinigen/core/constraints/example_solver/greedy/active_for_stage.py