75 lines
1.9 KiB
Python
75 lines
1.9 KiB
Python
# SPDX-FileCopyrightText: 2026 Art Sea LLC
|
|
#
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
"""Canonical path evidence shared by managed source and test selection."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
from band_aider.context.class_meta import ClassMeta
|
|
from band_aider.judgement.file_manager import FileManager
|
|
|
|
|
|
def is_referenced(
|
|
*,
|
|
file_manager: FileManager,
|
|
source: ClassMeta,
|
|
evidence: str,
|
|
) -> bool:
|
|
"""Return whether evidence contains one complete canonical managed file path."""
|
|
|
|
return first_reference_position(
|
|
file_manager=file_manager,
|
|
source=source,
|
|
evidence=evidence,
|
|
) is not None
|
|
|
|
|
|
def first_reference_position(
|
|
*,
|
|
file_manager: FileManager,
|
|
source: ClassMeta,
|
|
evidence: str,
|
|
) -> int | None:
|
|
"""Return the first exact managed file path position in normalized failure evidence."""
|
|
|
|
path = file_manager.repo_path(source.location)
|
|
paths = sorted(
|
|
{
|
|
path.relative_to(file_manager.repo_root).as_posix(),
|
|
path.as_posix(),
|
|
},
|
|
key=len,
|
|
reverse=True,
|
|
)
|
|
pattern = re.compile(
|
|
r"(?<![\w./\\-])(?:"
|
|
+ "|".join(map(re.escape, paths))
|
|
+ r")(?![\w./\\-])"
|
|
)
|
|
match = pattern.search(evidence.replace("\\", "/"))
|
|
return None if match is None else match.start()
|
|
|
|
|
|
def in_trace_order(
|
|
*,
|
|
file_manager: FileManager,
|
|
sources: list[ClassMeta],
|
|
evidence: str,
|
|
) -> list[ClassMeta]:
|
|
"""Return referenced sources in their first captured trace order."""
|
|
|
|
positioned = (
|
|
(position, index, source)
|
|
for index, source in enumerate(sources)
|
|
if (
|
|
position := first_reference_position(
|
|
file_manager=file_manager,
|
|
source=source,
|
|
evidence=evidence,
|
|
)
|
|
) is not None
|
|
)
|
|
return [source for _position, _index, source in sorted(positioned)]
|