Skip to content

symfonic.agent.backend.selection_spans

selection_spans

Byte spans in final system messages; provenance remains candidate data.

WireCandidateSpan dataclass

WireCandidateSpan(publication: object, line: str, message_index: int, block_index: int, byte_start: int, byte_end: int, linked_edge: object = None)

One uniquely present full line at the model adapter's wire boundary.

wire_candidate_spans

wire_candidate_spans(wire: list[Any], candidates: tuple[object, ...]) -> tuple[WireCandidateSpan, ...]

Match unique complete UTF-8 lines, refusing duplicates or rewrites.

Source code in src/symfonic/agent/backend/selection_spans.py
def wire_candidate_spans(
    wire: list[Any],
    candidates: tuple[object, ...],
) -> tuple[WireCandidateSpan, ...]:
    """Match unique complete UTF-8 lines, refusing duplicates or rewrites."""
    texts = tuple(_system_texts(wire))
    lines = [getattr(candidate, "line", None) for candidate in candidates]
    counts = Counter(line for line in lines if type(line) is str)
    spans: list[WireCandidateSpan] = []
    for candidate, line in zip(candidates, lines, strict=True):
        publication = getattr(candidate, "publication", None)
        if (
            type(line) is not str
            or not line
            or line.splitlines() != [line]
            or publication is None
            or counts[line] != 1
        ):
            continue
        needle = line.encode("utf-8")
        found = [
            (message_index, block_index, start, end)
            for message_index, block_index, text in texts
            for start, end in _line_spans(text.encode("utf-8"), needle)
        ]
        if len(found) != 1:
            continue
        message_index, block_index, start, end = found[0]
        spans.append(
            WireCandidateSpan(
                publication,
                line,
                message_index,
                block_index,
                start,
                end,
                getattr(candidate, "linked_edge", None),
            )
        )
    return tuple(spans)