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
87 changes: 87 additions & 0 deletions core/forge/artifact_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""File confinement and content checks at artifact stage boundaries."""

import hashlib
from pathlib import Path, PureWindowsPath
from typing import Iterable

from core.forge.contracts import GeneratedFile


class ArtifactPathError(ValueError):
"""An artifact does not declare distinct, confined relative file paths."""


def artifact_file_targets(
files: Iterable[GeneratedFile],
root: Path,
reserved_names: Iterable[str] = (),
) -> dict[str, Path]:
"""Check the entire file set before any file is written."""
root = root.resolve()
reserved = {name.casefold() for name in reserved_names}
targets: dict[str, Path] = {}
names: set[str] = set()
device_names = {"con", "prn", "aux", "nul", "conin$", "conout$"} | {
prefix + number for prefix in ("com", "lpt") for number in "123456789¹²³"
}
for generated in files:
path = generated.path
if (
not isinstance(path, str)
or not path
or "\\" in path
or ":" in path
or "\x00" in path
or PureWindowsPath(path).drive
or any(part in {"", ".", ".."} for part in path.split("/"))
or any(ord(char) < 32 or char in '<>"|?*' for char in path)
or any(
part.endswith((".", " "))
or part.partition(".")[0].rstrip(" ").casefold() in device_names
for part in path.split("/")
)
):
raise ArtifactPathError(f"Invalid artifact relative path: {path!r}")
name = path.casefold()
if name in names or name.split("/")[0] in reserved:
raise ArtifactPathError(f"Duplicate or reserved artifact path: {path!r}")
target = root / path
if target.resolve() != target or not target.is_relative_to(root):
raise ArtifactPathError(f"Artifact path is not confined: {path!r}")
if target.is_symlink():
raise ArtifactPathError(f"Artifact file is a symbolic link: {path!r}")
names.add(name)
targets[path] = target
for name in names:
parts = name.split("/")
if any("/".join(parts[:index]) in names for index in range(1, len(parts))):
raise ArtifactPathError(f"Artifact file/directory conflict: {name!r}")
return targets


def artifact_file_hashes(materialized: dict[str, Path]) -> dict[str, str]:
"""Snapshot actual written bytes, including platform newline conversion."""
return {
path: hashlib.sha256(target.read_bytes()).hexdigest()
for path, target in materialized.items()
}


def artifact_file_changes(
materialized: dict[str, Path], expected: dict[str, str], root: Path,
) -> list[dict[str, str]]:
changes: list[dict[str, str]] = []
root = root.resolve()
for path, digest in expected.items():
target = materialized[path]
try:
if target.is_symlink() or target.resolve() != target or not target.is_relative_to(root):
reason = "path_changed"
elif hashlib.sha256(target.read_bytes()).hexdigest() != digest:
reason = "content_changed"
else:
continue
except (OSError, ValueError):
reason = "file_unavailable"
changes.append({"path": path, "reason": reason})
return changes
62 changes: 54 additions & 8 deletions core/forge/expiration_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,74 @@
from typing import Any, Iterable

from core.forge.contracts import BuildSpec, RequirementAtom
from core.forge.requirement_text import mask_quoted_literals
from core.forge.requirement_text import mask_quoted_literals, prose_mention_is_negated


# Detection is deliberately broader than extraction: a nearby numeric duration
# and expiration predicate are enough to require interpretation, not to prove a
# value or comparison. Keep unsupported policies on the existing unresolved path.
_EXPIRATION = r"\b(?:expires?|expired|expiring|expiration)\b"
_DURATION = r"(?<![\w.])[+-]?\d+(?:\.\d+)?(?:\s+|-)days?\b"
# An "or" inside a comparison does not introduce a separate property.
_BRIDGE = (
r"(?:(?![.,;!?]|\b(?:and|but|however|whereas|then)\b|"
r"\bor\b(?!\s+(?:before|after|below|above|equal)\b)).){0,100}?"
)
_NUMERIC_EXPIRATION = re.compile(
rf"(?:{_EXPIRATION}{_BRIDGE}{_DURATION}|{_DURATION}{_BRIDGE}{_EXPIRATION})",
re.IGNORECASE,
)


def _has_numeric_expiration_policy(text: str) -> bool:
# Negative-looking comparatives are not rejected obligations.
# Mask it only for polarity analysis, preserving recognizer offsets.
polarity_text = re.sub(
r"\b(?:no\s+(?:more|less|fewer)\s+than|(?:do(?:es)?|must|shall|may)\s+not\s+exceed)\b",
lambda match: " " * len(match.group()), text, flags=re.IGNORECASE,
Comment thread
Daniele-Cangi marked this conversation as resolved.
Resolved
)
return any(
not prose_mention_is_negated(polarity_text, match.start(), match.end())
and not prose_mention_is_negated(polarity_text, match.end(), match.end())
for match in _NUMERIC_EXPIRATION.finditer(text)
)


def compile_expiration_horizon(atoms: Iterable[RequirementAtom]) -> dict[str, Any] | None:
matches = []
unresolved_ids = []
requirement_ids = []
for atom in atoms:
if atom.strength not in {"hard", "universal"}:
continue
text = mask_quoted_literals(atom.text)
match = re.match(
r"^flags?\s+(?:(?:a|the)\s+)?contracts?\s+"
r"(?:expiring|(?:that|which)\s+expires?)\s+in\s+less\s+than\s+(-?\d+)\s+days?\b",
mask_quoted_literals(atom.text), re.IGNORECASE,
text, re.IGNORECASE,
)
if match and atom.strength in {"hard", "universal"}:
if match:
matches.append((int(match.group(1)), atom.requirement_id))
if not matches:
requirement_ids.append(atom.requirement_id)
# The strict extractor recognizes a prefix, not necessarily the whole
# atom. A second policy in its remainder still needs interpretation.
remainder = text[match.end():] if match else text
if _has_numeric_expiration_policy(remainder):
unresolved_ids.append(atom.requirement_id)
Comment thread
Daniele-Cangi marked this conversation as resolved.
Resolved
if not match:
requirement_ids.append(atom.requirement_id)
if not matches and not unresolved_ids:
return None
values = {value for value, _ in matches}
return {
contract = {
"observable_field": "is_expiring_within_horizon",
"comparison_relation": "less_than",
"threshold_days": next(iter(values)) if len(values) == 1 else None,
"requirement_ids": [requirement_id for _, requirement_id in matches],
"comparison_relation": None if unresolved_ids else "less_than",
"threshold_days": next(iter(values)) if len(values) == 1 and not unresolved_ids else None,
"requirement_ids": requirement_ids,
}
if unresolved_ids:
contract["unresolved_reason"] = "unsupported_numeric_expiration_wording"
return contract


def expiration_horizon_contract(spec: BuildSpec) -> dict[str, Any] | None:
Expand Down
11 changes: 10 additions & 1 deletion core/forge/packaging_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path
from typing import Any, Dict, List

from core.forge.artifact_files import ArtifactPathError, artifact_file_targets
from core.forge.contracts import (
BuildSpec,
CodeArtifact,
Expand Down Expand Up @@ -78,6 +79,13 @@ def package(

base_package_id = self._package_id(build_spec.build_id, plan.plan_id, code_artifact.artifact_id)
package_id, package_root = self._resolve_package_root(base_package_id)
try:
artifact_file_targets(code_artifact.files, package_root, reserved_names=(
"forge_package_manifest.json", "validation_evidence.json",
"code_artifact_manifest_dump.json",
))
except ArtifactPathError as exc:
raise PackagingRefusedError(str(exc)) from exc
code_artifact_digest = self._artifact_content_digest(code_artifact)
artifact_manifest_bytes = canonical_json_bytes(
self._to_jsonable(code_artifact.artifact_manifest)
Expand Down Expand Up @@ -207,8 +215,9 @@ def package(

def _write_code_artifact_files(self, package_root: Path, code_artifact: CodeArtifact) -> List[str]:
packaged_files: List[str] = []
targets = artifact_file_targets(code_artifact.files, package_root)
for generated_file in sorted(code_artifact.files, key=lambda item: item.path):
target = package_root / generated_file.path
target = targets[generated_file.path]
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(generated_file.content, encoding="utf-8")
packaged_files.append(generated_file.path)
Expand Down
58 changes: 34 additions & 24 deletions core/forge/requirement_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
explicit_test_coverage_target,
mask_quoted_literals,
normalize_requirement_text,
prose_mention_is_negated,
requirement_clause_key,
split_requirement_text,
)
Expand Down Expand Up @@ -97,7 +98,12 @@ def compile(self, requirement: str) -> BuildSpec:
if expiration_horizon is not None:
obligation_contract.context["expiration_horizon"] = expiration_horizon
if expiration_horizon["threshold_days"] is None:
ambiguity_flags.append("Materially unspecified expiration horizon: conflicting numeric thresholds.")
reason = (
"unsupported numeric wording"
if expiration_horizon.get("unresolved_reason") == "unsupported_numeric_expiration_wording"
else "conflicting numeric thresholds"
)
ambiguity_flags.append(f"Materially unspecified expiration horizon: {reason}.")
quality_contract = self._extract_quality_contract(normalized)

return BuildSpec(
Expand Down Expand Up @@ -127,7 +133,15 @@ def compile(self, requirement: str) -> BuildSpec:
)

def _extract_quality_contract(self, requirement: str) -> QualityContract:
lowered = requirement.lower()
lowered = mask_quoted_literals(requirement).lower()

def positive(pattern: str) -> bool:
return any(
not prose_mention_is_negated(lowered, match.start(), match.end())
for match in re.finditer(pattern, lowered)
)

production = positive(r"\b(?:production(?:-grade)?|prod-ready)\b")
quality = QualityContract(
auth_level="plaintext",
secrets_in_plaintext=True,
Expand All @@ -142,61 +156,57 @@ def _extract_quality_contract(self, requirement: str) -> QualityContract:
)

# Auth quality
if any(token in lowered for token in ("jwt", "bearer token", "oauth")):
if positive(r"\b(?:jwts?|bearer\s+tokens?|oauth2?)\b"):
quality.auth_level = "jwt"
quality.secrets_in_plaintext = False
elif any(token in lowered for token in ("hashed", "bcrypt", "argon2")):
elif positive(r"\b(?:hashed|bcrypt|argon2)\b"):
quality.auth_level = "hashed"
quality.secrets_in_plaintext = False
elif any(token in lowered for token in ("api key", "api-key", "authentication")):
elif positive(r"\b(?:api[- ]keys?|authentication)\b"):
quality.auth_level = "plaintext"
quality.secrets_in_plaintext = True
if "secure" in lowered:
if positive(
r"\bsecure\s+(?:api[- ]keys?|authentication)\b|"
r"\b(?:api[- ]keys?|authentication)\s+(?:must\s+be\s+)?secure\b"
):
quality.auth_level = "hashed"
quality.secrets_in_plaintext = False

self._extract_rate_limit_quality(requirement, quality)

# Persistence quality
if any(token in lowered for token in ("migrations", "versioned schema", "alembic")):
if positive(r"\b(?:migrations|versioned\s+schemas?|alembic)\b"):
quality.schema_versioned = True
if any(token in lowered for token in ("audit log", "audit trail", "event log", "full audit trail")):
if positive(r"\b(?:audit\s+(?:logs?|trails?)|event\s+logs?)\b"):
quality.audit_trail = True
if any(token in lowered for token in ("production", "prod-ready", "production-grade")):
if production:
quality.schema_versioned = True
quality.audit_trail = True

# Observability quality
if any(
token in lowered
for token in (
"health check",
"monitoring",
"observability",
"structured json logging",
"structured logging",
"structured error logging",
)
if positive(
r"\b(?:health\s+checks?|monitoring|observability|"
r"structured\s+(?:(?:json|error)\s+)?logging)\b"
):
quality.health_endpoint = True
quality.structured_logging = True
if any(token in lowered for token in ("production", "prod-ready", "production-grade")):
if production:
quality.health_endpoint = True

# Test quality
if any(token in lowered for token in ("integration tests", "end-to-end", "e2e")):
if positive(r"\b(?:integration\s+tests|end-to-end|e2e)\b"):
quality.integration_tests = True
if any(token in lowered for token in ("production", "prod-ready", "production-grade")):
if production:
quality.test_coverage_target = 0.8
quality.integration_tests = True
explicit_coverage = explicit_test_coverage_target(requirement)
if explicit_coverage is not None:
quality.test_coverage_target = explicit_coverage

computed_level = quality.compute_level()
if any(token in lowered for token in ("microservice", "service", "rest", "api")) and computed_level < 5:
if positive(r"\b(?:microservices?|services?|rest|apis?)\b") and computed_level < 5:
computed_level = 5
if any(token in lowered for token in ("production", "prod-ready", "production-grade")) and computed_level > 9:
if production and computed_level > 9:
computed_level = 9
quality.overall_level = computed_level
return quality
Expand Down
2 changes: 1 addition & 1 deletion core/forge/requirement_evidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def requirement_assertion_evidence(
):
if not function["semantic"]:
continue
function_source = str(function["source"])
function_source = str(function["evidence_source"])
matched_terms = [
term
for term in terms
Expand Down
Loading
Loading