Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions core/forge/conditional_evidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
analyze_test_expectations,
)
from core.forge.execution import ProcessExecutor, SandboxProcessRequest
from core.forge.requirement_text import explicit_cli_argument_index



Expand Down Expand Up @@ -223,6 +224,7 @@ def _has_derivable_cli_probe(
"empty_input",
"numeric_argument_exceeds_input_length",
"invalid_positive_integer",
"invalid_integer",
"invalid_argument_count",
"file_read_failure",
"utf8_decode_failure",
Expand Down Expand Up @@ -278,9 +280,16 @@ def _build_cli_probe(
if (
numeric_index is None
and argument_count == 1
and obligation.witness_class == "invalid_positive_integer"
and obligation.witness_class in {"invalid_positive_integer", "invalid_integer"}
):
numeric_index = 0
if any(
index is not None and index >= argument_count
for index in (filename_index, numeric_index)
):
return None
if filename_index is not None and filename_index == numeric_index:
return None
fixture_path = workspace / ".forge_branch_probe_input"
if filename_index is not None and filename_index < len(args):
args[filename_index] = str(fixture_path)
Expand All @@ -298,6 +307,10 @@ def _build_cli_probe(
if filename_index is not None:
fixture_path.write_text("sample", encoding="utf-8")
args[numeric_index] = "0"
elif witness == "invalid_integer" and numeric_index is not None:
if filename_index is not None:
fixture_path.write_text("sample", encoding="utf-8")
args[numeric_index] = "not-an-integer"
elif witness == "invalid_argument_count":
args = args[:-1]
elif witness == "file_read_failure" and filename_index is not None:
Expand Down Expand Up @@ -348,20 +361,7 @@ def _declared_argv_count(requirement: str) -> int | None:

@staticmethod
def _argv_index(requirement: str, role_pattern: str) -> int | None:
forward = re.search(
rf"(?:{role_pattern}).{{0,80}}argv\s*\[\s*(\d+)\s*\]",
requirement,
re.IGNORECASE,
)
reverse = re.search(
rf"argv\s*\[\s*(\d+)\s*\].{{0,80}}(?:{role_pattern})",
requirement,
re.IGNORECASE,
)
match = forward or reverse
if match is None:
return None
return max(0, int(match.group(1)) - 1)
return explicit_cli_argument_index(requirement, role_pattern)

@staticmethod
def _decode_probe_result(stdout: str) -> dict[str, Any] | None:
Expand Down Expand Up @@ -411,6 +411,10 @@ def _coverage_directive_evidence(build_spec: BuildSpec) -> list[dict[str, Any]]:
for obligation in build_spec.conditional_obligations
if obligation.witness_class
}
# Every non-integer also violates a positive-integer precondition.
# Keep the coverage request distinct while retaining its stricter branch.
if "invalid_positive_integer" in known_witnesses:
known_witnesses.add("invalid_integer")
return [
{
"directive_id": directive.directive_id,
Expand Down
14 changes: 12 additions & 2 deletions core/forge/conditional_obligations.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ def _compile_coverage_directive(
obligation.obligation_id
for obligation in obligations
if obligation.witness_class in witness_classes
or (
"invalid_integer" in witness_classes
and obligation.witness_class == "invalid_positive_integer"
)
]
return CoverageDirective(
directive_id=f"{atom.requirement_id}.C01",
Expand All @@ -401,7 +405,8 @@ def _coverage_witness_classes(self, text: str) -> list[str]:
lowered = text.lower()
witnesses: list[str] = []
patterns = (
("invalid_positive_integer", r"invalid\s+[^,]*sizes?|non[- ]integer"),
("invalid_positive_integer", r"invalid\s+[^,]*sizes?"),
("invalid_integer", r"non[- ]integer"),
("empty_input", r"empty\s+(?:files?|inputs?)"),
("utf8_decode_failure", r"not\s+valid\s+utf[- ]?8|invalid\s+utf[- ]?8"),
("malformed_record", r"malformed\s+(?:rows?|records?)"),
Expand All @@ -425,9 +430,14 @@ def _predicate_contract(trigger: str) -> tuple[str, dict[str, Any]]:
("invalid_argument_count", r"\bnot\s+exactly\s+\w+\s+arguments?\b", {"kind": "invalid_argument_count"}),
(
"invalid_positive_integer",
r"\bnot\s+a\s+valid\s+positive\s+integer\b|\bnon[- ]?integer\b|\binvalid\s+.*size\b",
r"\bnot\s+a\s+valid\s+positive\s+integer\b|\binvalid\s+.*size\b",
{"kind": "invalid_positive_integer"},
),
(
"invalid_integer",
r"\bnon\s*integer\b",
{"kind": "invalid_integer"},
),
("file_read_failure", r"\bfile\s+(?:reading|read)\s+fails?\b", {"kind": "file_read_failure"}),
("utf8_decode_failure", r"\b(?:file\s+)?decod(?:e|ing)\s+fails?\b|\binvalid\s+utf\s*8\b", {"kind": "utf8_decode_failure"}),
("zero_value", r"\b(?:shift|value|size)\s+is\s+zero\b", {"kind": "zero_value"}),
Expand Down
68 changes: 61 additions & 7 deletions core/forge/conditional_test_evidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Any, Iterable, Mapping

from core.forge.contracts import BuildSpec, ConditionalObligation, FeasiblePlan
from core.forge.requirement_text import explicit_cli_argument_index


_LOSSY_METHODS = frozenset(
Expand Down Expand Up @@ -44,6 +45,7 @@ def analyze_test_expectations(
for function in _test_functions(tree):
source = ast.get_source_segment(content, function) or ast.unparse(function)
witness_classes = _infer_witness_classes(function, source, plan)
integer_predicates = _literal_integer_predicates(function, plan)
exercised = [
obligation
for obligation in obligations_by_id.values()
Expand All @@ -52,6 +54,7 @@ def analyze_test_expectations(
if witness_classes
else obligation.obligation_id in mapped_ids
)
and integer_predicates.get(obligation.witness_class) is not False
]
observations = _asserted_observations(function)
contradictions: list[dict[str, Any]] = []
Expand Down Expand Up @@ -116,6 +119,7 @@ def analyze_observation_fidelity(
for function in _test_functions(tree):
source = ast.get_source_segment(content, function) or ast.unparse(function)
witnesses = _infer_witness_classes(function, source, plan)
integer_predicates = _literal_integer_predicates(function, plan)
applicable = [
obligation
for obligation in exact_obligations.values()
Expand All @@ -124,6 +128,7 @@ def analyze_observation_fidelity(
if witnesses
else obligation.obligation_id in mapped_ids
)
and integer_predicates.get(obligation.witness_class) is not False
]
lossy: list[dict[str, Any]] = []
for assertion in (node for node in ast.walk(function) if isinstance(node, ast.Assert)):
Expand Down Expand Up @@ -174,7 +179,8 @@ def _infer_witness_classes(
patterns = (
("empty_input", r"empty_(?:file|input)|write_(?:text|bytes)\s*\(\s*(?:b)?[\"']{2}"),
("numeric_argument_exceeds_input_length", r"(?:greater|exceeds?|larger|longer)_than_(?:input|length)|size_exceeds"),
("invalid_positive_integer", r"invalid_(?:chunk_)?size|non_integer|not_integer|negative_size|zero_size"),
("invalid_positive_integer", r"invalid_(?:chunk_)?size|negative_size|zero_size"),
("invalid_integer", r"non_integer|not_integer"),
("invalid_argument_count", r"argument_count|missing_argument|too_(?:many|few)_arguments"),
("utf8_decode_failure", r"invalid_utf_?8|decode_failure"),
("file_read_failure", r"read_failure|missing_file|nonexistent_file"),
Expand All @@ -193,12 +199,14 @@ def _infer_witness_classes(
text_lengths = [len(value) for value in writes if isinstance(value, str) and value]
if numeric and text_lengths and max(numeric) > min(text_lengths):
witnesses.add("numeric_argument_exceeds_input_length")
if any(
isinstance(value, str)
and (not re.fullmatch(r"\d+", value) or int(value) <= 0)
for value in args[1:]
):
witnesses.add("invalid_positive_integer")
declared_classes = {item.witness_class for item in plan.build_spec.conditional_obligations}
if "invalid_integer" in witnesses and "invalid_positive_integer" in declared_classes:
witnesses.add("invalid_positive_integer")
for witness, applies in _literal_integer_predicates(function, plan).items():
if applies and witness in declared_classes:
witnesses.add(witness)
elif applies is False:
witnesses.discard(witness)
expected_count = next(
(
interface.explicit_argv_count
Expand All @@ -215,6 +223,52 @@ def _infer_witness_classes(
return witnesses


def _literal_integer_predicates(function: ast.AST, plan: FeasiblePlan) -> dict[str, bool]:
"""Literal inputs take precedence over names and planned integer-branch mappings.

Unknown calls/argument bindings leave the existing evidence fallback intact;
this is not semantic adjudication of arbitrary test programs.
"""
calls = _literal_main_calls(function)
count = next(
(
item.explicit_argv_count
for item in plan.interfaces
if item.interface_type == "cli_entrypoint"
and item.explicit_argv_count is not None
),
None,
)
if not calls or count is None:
return {}
if count == 1:
index = 0
else:
index = explicit_cli_argument_index(
plan.build_spec.normalized_requirement,
r"chunk\s+size|size|count|limit|shift",
)
if index is None:
return {}
invalid_integer: list[bool] = []
invalid_positive: list[bool] = []
for args in calls:
if len(args) != count or not 0 <= index < len(args) or not isinstance(args[index], str):
return {}
try:
number = int(args[index])
except ValueError:
invalid_integer.append(True)
invalid_positive.append(True)
else:
invalid_integer.append(False)
invalid_positive.append(number <= 0)
return {
"invalid_integer": any(invalid_integer),
"invalid_positive_integer": any(invalid_positive),
}


def _literal_file_writes(function: ast.AST) -> list[str | bytes]:
values: list[str | bytes] = []
for call in (node for node in ast.walk(function) if isinstance(node, ast.Call)):
Expand Down
22 changes: 22 additions & 0 deletions core/forge/requirement_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ def mask_quoted_literals(text: str) -> str:
return _QUOTED_LITERAL.sub(lambda match: "~" * len(match.group()), text)


def explicit_cli_argument_index(requirement: str, role_pattern: str) -> int | None:
"""Resolve one role to one user argv slot without borrowing another binding.

Both role-before-index and index-before-role forms must stay within the
same declaration. Conflicting indices remain unknown rather than picking
the first or last match. Quoted output examples are not declarations.
"""
text = mask_quoted_literals(requirement)
role = rf"\b(?:{role_pattern})\b"
argv = r"\bargv\s*\[\s*(?P<index>\d+)\s*\]"
bridge = r"(?:(?!\bargv\s*\[|\b(?:and|or)\b|[.,;:!?\n~]).){0,80}?"
indexes = {
int(match.group("index"))
for pattern in (rf"{role}{bridge}{argv}", rf"{argv}{bridge}{role}")
for match in re.finditer(pattern, text, re.IGNORECASE)
}
if len(indexes) != 1:
return None
index = indexes.pop()
return index - 1 if index > 0 else None


def split_requirement_text(text: str, boundary: re.Pattern[str]) -> list[str]:
masked = mask_quoted_literals(text)
chunks: list[str] = []
Expand Down
Loading
Loading