From 2385378051cc4b9305735ba034987d95a6bbe025 Mon Sep 17 00:00:00 2001 From: itvi-1234 Date: Tue, 17 Nov 2026 22:32:26 +0530 Subject: [PATCH 1/2] chore: remove files nothing uses and add the MIT license Remove archive/research-v1, about 2,300 lines of the first research prototype that no package, test or workflow reads and whose own paths no longer exist; profiler.py, a seven-line copy of the console script, which the interop plan already says profiling must not need; and the 0.1.0 core profile schema, which the profiler does not load (the distribution carries only the 0.4.0 one). Add the MIT LICENSE the other profilers carry. --- LICENSE | 21 + archive/research-v1/sdk/composition.py | 1032 ----------------- archive/research-v1/sdk/discovery.py | 185 --- archive/research-v1/sdk/python.py | 452 -------- archive/research-v1/test_profiler.py | 633 ---------- profiler.py | 7 - ...ntimeconditions.profile.v0.1.0.schema.yaml | 100 -- 7 files changed, 21 insertions(+), 2409 deletions(-) create mode 100644 LICENSE delete mode 100644 archive/research-v1/sdk/composition.py delete mode 100644 archive/research-v1/sdk/discovery.py delete mode 100644 archive/research-v1/sdk/python.py delete mode 100644 archive/research-v1/test_profiler.py delete mode 100644 profiler.py delete mode 100644 runtimeconditions_profiler/runtimeconditions.profile.v0.1.0.schema.yaml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2afe312 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 Runtime Conditions + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/archive/research-v1/sdk/composition.py b/archive/research-v1/sdk/composition.py deleted file mode 100644 index aa44a81..0000000 --- a/archive/research-v1/sdk/composition.py +++ /dev/null @@ -1,1032 +0,0 @@ -from __future__ import annotations - -import ast -from dataclasses import dataclass, field -from pathlib import Path -from typing import Any - -from ..errors import RuntimeConditionsError -from ..models import SDKMappingArtifact -from .python import ResolvedCondition, SDKConditionResolver - - -MappingKey = tuple[str, str] - - -@dataclass(frozen=True) -class UnknownValue: - pass - - -UNKNOWN = UnknownValue() - - -@dataclass(frozen=True) -class LiteralValue: - value: Any - - -@dataclass(frozen=True) -class ExternalSymbol: - name: str - - -@dataclass(frozen=True) -class ExternalInstance: - type_name: str - - -@dataclass(frozen=True) -class FunctionValue: - name: str - - -@dataclass(frozen=True) -class ClassValue: - name: str - - -@dataclass(frozen=True) -class AppInstance: - class_name: str - fields: tuple[tuple[str, Any], ...] = () - - def field(self, name: str) -> Any: - return dict(self.fields).get(name, UNKNOWN) - - -@dataclass(frozen=True) -class BoundAppMethod: - instance: AppInstance - function_name: str - - -@dataclass(frozen=True) -class SDKClientValue: - mapping: MappingKey - factory_owner: MappingKey - - -@dataclass(frozen=True) -class BoundSDKClientMethod: - client: SDKClientValue - method: str - - -@dataclass(frozen=True) -class SDKResourceValue: - mapping: MappingKey - resource: str - - -@dataclass(frozen=True) -class BoundSDKResourceMember: - resource: SDKResourceValue - member: str - - -@dataclass(frozen=True) -class SDKStatefulFlowValue: - mapping: MappingKey - flow_id: str - member_path: tuple[str, ...] = () - - -@dataclass(frozen=True) -class BoundSDKStatefulProducer: - flow: SDKStatefulFlowValue - - -@dataclass(frozen=True) -class SDKStatefulResourceValue: - mapping: MappingKey - flow_id: str - state: tuple[tuple[str, Any], ...] - - def field(self, name: str) -> Any: - return dict(self.state).get(name, UNKNOWN) - - -@dataclass(frozen=True) -class BoundSDKStatefulResourceMethod: - resource: SDKStatefulResourceValue - method: str - - -@dataclass(frozen=True) -class SDKTypedValueObject: - type_name: str - fields: tuple[tuple[str, Any], ...] - - def field(self, name: str) -> Any: - return dict(self.fields).get(name, UNKNOWN) - - -@dataclass(frozen=True) -class SDKTypedStateValue: - mapping: MappingKey - state_type: str - dependency_identity: str - fields: tuple[tuple[str, Any], ...] = () - - def field(self, name: str) -> Any: - return dict(self.fields).get(name, UNKNOWN) - - -@dataclass(frozen=True) -class BoundSDKTypedStateMethod: - state: SDKTypedStateValue - method: str - - -@dataclass -class ModuleInfo: - name: str - path: Path - tree: ast.Module - imports: dict[str, str] = field(default_factory=dict) - literals: dict[str, LiteralValue] = field(default_factory=dict) - runtime_values: dict[str, Any] = field(default_factory=dict) - - -@dataclass(frozen=True) -class FunctionInfo: - name: str - module: str - node: ast.FunctionDef | ast.AsyncFunctionDef - class_name: str | None = None - - -@dataclass(frozen=True) -class ClassInfo: - name: str - module: str - node: ast.ClassDef - - -@dataclass -class Frame: - module: ModuleInfo - values: dict[str, Any] - self_fields: dict[str, Any] | None = None - - -@dataclass(frozen=True) -class ExecutionResult: - value: Any - instance: AppInstance | None = None - - -class SDKCompositionRegistry: - def __init__(self, artifacts: list[SDKMappingArtifact], resolver: SDKConditionResolver) -> None: - self.artifacts: dict[MappingKey, SDKMappingArtifact] = {} - self.resolver = resolver - self.aliases: dict[str, str] = {} - self.client_factories: list[tuple[SDKMappingArtifact, dict[str, Any], str]] = [] - self.resource_factories: list[tuple[SDKMappingArtifact, dict[str, Any], str]] = [] - self.stateful_factories: list[tuple[SDKMappingArtifact, dict[str, Any], str]] = [] - self.typed_factories: list[tuple[SDKMappingArtifact, dict[str, Any], str]] = [] - self.value_types: list[tuple[SDKMappingArtifact, dict[str, Any], str]] = [] - self.typed_calls: dict[tuple[MappingKey, str, str], list[dict[str, Any]]] = {} - for artifact in artifacts: - key = (artifact.distribution, artifact.name) - previous = self.artifacts.get(key) - if previous is not None and (previous.distribution_version != artifact.distribution_version or previous.mapping_sha256 != artifact.mapping_sha256): - raise RuntimeConditionsError(f"ambiguous SDK mapping versions for {key[0]}/{key[1]}") - self.artifacts[key] = artifact - python = artifact.mapping.get("python", {}) - if not isinstance(python, dict): - continue - for alias in python.get("aliases", []): - if isinstance(alias, dict) and isinstance(alias.get("symbol"), str) and isinstance(alias.get("target"), str): - previous_alias = self.aliases.get(alias["symbol"]) - if previous_alias is not None and previous_alias != alias["target"]: - raise RuntimeConditionsError(f"conflicting SDK aliases for {alias['symbol']}") - self.aliases[alias["symbol"]] = alias["target"] - for factory in python.get("clientFactories", []): - if isinstance(factory, dict): - for symbol in factory.get("symbols", []): - if isinstance(symbol, str): - self.client_factories.append((artifact, factory, symbol)) - for factory in python.get("resourceFactories", []): - if isinstance(factory, dict): - for symbol in factory.get("symbols", []): - if isinstance(symbol, str): - self.resource_factories.append((artifact, factory, symbol)) - for flow in python.get("statefulResourceFlows", []): - if not isinstance(flow, dict) or not isinstance(flow.get("id"), str): - raise RuntimeConditionsError(f"{artifact.name}: invalid stateful resource flow") - for symbol in flow.get("constructorSymbols", []): - if not isinstance(symbol, str) or not symbol: - raise RuntimeConditionsError(f"{artifact.name}: invalid stateful resource constructor symbol") - self.stateful_factories.append((artifact, flow, symbol)) - for factory in python.get("factories", []): - if not isinstance(factory, dict): - raise RuntimeConditionsError(f"{artifact.name}: invalid typed-state factory") - for symbol in factory.get("symbols", []): - if not isinstance(symbol, str) or not symbol: - raise RuntimeConditionsError(f"{artifact.name}: invalid typed-state factory symbol") - self.typed_factories.append((artifact, factory, symbol)) - for value_type in python.get("valueTypes", []): - if not isinstance(value_type, dict) or not isinstance(value_type.get("id"), str): - raise RuntimeConditionsError(f"{artifact.name}: invalid Python SDK value type") - for symbol in value_type.get("symbols", []): - if not isinstance(symbol, str) or not symbol: - raise RuntimeConditionsError(f"{artifact.name}: invalid Python SDK value-type symbol") - self.value_types.append((artifact, value_type, symbol)) - for call in python.get("calls", []): - if not isinstance(call, dict) or not isinstance(call.get("receiverState"), str): - continue - symbols = call.get("symbols", []) - if not isinstance(symbols, list) or not symbols: - raise RuntimeConditionsError(f"{artifact.name}: typed-state call has no symbols") - methods = {symbol.get("method") for symbol in symbols if isinstance(symbol, dict) and isinstance(symbol.get("method"), str)} - if not methods: - raise RuntimeConditionsError(f"{artifact.name}: typed-state call must declare at least one method name") - for method in methods: - self.typed_calls.setdefault((key, call["receiverState"], method), []).append(call) - - def canonical_symbol(self, name: str) -> str: - seen: set[str] = set() - while name in self.aliases and name not in seen: - seen.add(name) - name = self.aliases[name] - return name - - def factory(self, symbol: str, positional: list[Any], keywords: dict[str, Any]) -> Any: - canonical = self.canonical_symbol(symbol) - matched_symbol = False - for artifact, factory, declared_symbol in self.client_factories: - if self.canonical_symbol(declared_symbol) != canonical: - continue - matched_symbol = True - if not self._service_matches(factory, positional, keywords): - continue - self._ensure_dependencies(artifact) - produced = factory.get("produces", {}) - target = self._mapping_key(produced, artifact) - self._artifact(target) - return SDKClientValue(target, (artifact.distribution, artifact.name)) - for artifact, factory, declared_symbol in self.resource_factories: - if self.canonical_symbol(declared_symbol) != canonical: - continue - matched_symbol = True - if not self._service_matches(factory, positional, keywords): - continue - self._ensure_dependencies(artifact) - produced = factory.get("produces", {}) - target = self._mapping_key(produced, artifact) - self._artifact(target) - resource = produced.get("resource") - if not isinstance(resource, str) or not resource: - raise RuntimeConditionsError(f"{artifact.name}: resource factory has no produced resource") - return SDKResourceValue(target, resource) - for artifact, flow, declared_symbol in self.stateful_factories: - if self.canonical_symbol(declared_symbol) != canonical: - continue - matched_symbol = True - self._ensure_dependencies(artifact) - return SDKStatefulFlowValue((artifact.distribution, artifact.name), flow["id"]) - return UNKNOWN if matched_symbol else None - - def typed_factory(self, symbol: str, positional: list[Any], keywords: dict[str, Any], identity_seed: str) -> tuple[list[ResolvedCondition], Any] | None: - canonical = self.canonical_symbol(symbol) - value_matches = [(artifact, value_type) for artifact, value_type, declared_symbol in self.value_types if self.canonical_symbol(declared_symbol) == canonical] - if len(value_matches) > 1: - raise RuntimeConditionsError(f"ambiguous Python SDK value type {canonical}") - if value_matches: - artifact, value_type = value_matches[0] - self._ensure_dependencies(artifact) - fields = self._resolve_bindings(value_type.get("fields", {}), None, positional, keywords) - return [], SDKTypedValueObject(value_type["id"], tuple(sorted(fields.items()))) - factory_matches = [(artifact, factory) for artifact, factory, declared_symbol in self.typed_factories if self.canonical_symbol(declared_symbol) == canonical] - if len(factory_matches) > 1: - raise RuntimeConditionsError(f"ambiguous Python SDK typed-state factory {canonical}") - if not factory_matches: - return None - artifact, factory = factory_matches[0] - self._ensure_dependencies(artifact) - dependency_identity = self._factory_dependency_identity(factory, positional, keywords, identity_seed) - conditions = self._resolve_typed_operation(artifact, factory, None, positional, keywords, dependency_identity) - argument_state = factory.get("argumentState") - binding = argument_state.get("argument") if isinstance(argument_state, dict) else None - receiver = self._argument(binding, positional, keywords) - inherited = receiver if isinstance(receiver, SDKTypedStateValue) else None - return conditions, self._produce_typed_state(artifact, factory.get("produces"), inherited, positional, keywords, identity_seed) - - def typed_state_method(self, value: SDKTypedStateValue, method: str, positional: list[Any], keywords: dict[str, Any], identity_seed: str) -> tuple[list[ResolvedCondition], Any]: - matches = self.typed_calls.get((value.mapping, value.state_type, method), []) - if len(matches) > 1: - artifact = self._artifact(value.mapping) - raise RuntimeConditionsError(f"{artifact.name}: ambiguous typed-state call {value.state_type}.{method}") - if not matches: - return [], UNKNOWN - artifact = self._artifact(value.mapping) - call = matches[0] - conditions = self._resolve_typed_operation(artifact, call, value, positional, keywords, value.dependency_identity) - return conditions, self._produce_typed_state(artifact, call.get("produces"), value, positional, keywords, identity_seed) - - def stateful_member(self, value: SDKStatefulFlowValue, member: str) -> Any: - artifact, flow = self._stateful_flow(value) - producer = flow.get("producer", {}) - member_path = producer.get("memberPath") - if not isinstance(member_path, list) or any(not isinstance(item, str) or not item for item in member_path): - raise RuntimeConditionsError(f"{artifact.name}: {flow.get('id')} has an invalid producer member path") - next_path = (*value.member_path, member) - expected = tuple(member_path) - if next_path == expected: - return BoundSDKStatefulProducer(value) - if expected[: len(next_path)] == next_path: - return SDKStatefulFlowValue(value.mapping, value.flow_id, next_path) - return UNKNOWN - - def produce_stateful_resource(self, value: SDKStatefulFlowValue, positional: list[Any], keywords: dict[str, Any]) -> Any: - artifact, flow = self._stateful_flow(value) - producer = flow.get("producer", {}) - bindings = producer.get("selectorArguments", {}) - if not isinstance(bindings, dict): - raise RuntimeConditionsError(f"{artifact.name}: {flow.get('id')} has invalid selector bindings") - selected = {field: self._literal_string(self._argument(binding, positional, keywords)) for field, binding in bindings.items()} - api_version = selected.get("apiVersion") - kind = selected.get("kind") - group = selected.get("group") - if not isinstance(api_version, str) or not isinstance(kind, str): - return UNKNOWN - if group is None and "/" in api_version: - group, api_version = api_version.split("/", 1) - catalog = producer.get("catalog") - if not isinstance(catalog, list): - raise RuntimeConditionsError(f"{artifact.name}: {flow.get('id')} has no resource catalog") - candidates = [] - for case in catalog: - selector = case.get("selector", {}) if isinstance(case, dict) else {} - if selector.get("apiVersion") != api_version or selector.get("kind") != kind: - continue - if group is not None and selector.get("apiGroup") != group: - continue - candidates.append(case) - if len(candidates) != 1 or not isinstance(candidates[0].get("state"), dict): - return UNKNOWN - return SDKStatefulResourceValue(value.mapping, value.flow_id, tuple(sorted(candidates[0]["state"].items()))) - - def resolve_stateful_resource_method(self, value: SDKStatefulResourceValue, method_name: str, positional: list[Any], keywords: dict[str, Any]) -> tuple[list[ResolvedCondition], Any]: - artifact, flow = self._stateful_flow(value) - methods = [item for item in flow.get("methods", []) if isinstance(item, dict) and item.get("method") == method_name] - if len(methods) != 1: - return [], UNKNOWN - method = methods[0] - branches = method.get("operations") - if not isinstance(branches, list) or not branches: - raise RuntimeConditionsError(f"{artifact.name}: {flow.get('id')}.{method_name} has no operations") - selected = [] - for branch in branches: - when = branch.get("when") if isinstance(branch, dict) else None - if isinstance(when, dict): - if self._stateful_branch_matches(when, positional, keywords): - selected.append(branch) - elif branch.get("otherwise") is True: - selected.append(branch) - else: - selected.append(branch) - if selected: - break - if len(selected) != 1: - return [], UNKNOWN - reference = selected[0].get("operationRef") - target = self._artifact(self._mapping_key(reference, artifact)) - operation_name = reference.get("operation") if isinstance(reference, dict) else None - if not isinstance(operation_name, str): - raise RuntimeConditionsError(f"{artifact.name}: {flow.get('id')}.{method_name} has an invalid operation reference") - state = dict(value.state) - template = next((item.get("conditionTemplate") for item in target.mapping.get("operations", []) if isinstance(item, dict) and item.get("name") == operation_name), None) - if not isinstance(template, dict): - raise RuntimeConditionsError(f"{target.name}: stateful operation {operation_name} has no template") - scope = self._resolve_stateful_scope(template, state, method.get("scopeArgument"), positional, keywords) - if scope is None: - return [], UNKNOWN - return self.resolver.resolve_stateful_operation(target, operation_name, state, scope), UNKNOWN - - def client_method(self, value: SDKClientValue, method: str) -> tuple[list[ResolvedCondition], Any]: - artifact = self._artifact(value.mapping) - client = artifact.mapping.get("python", {}).get("client", {}) - if isinstance(client, dict): - matches = [item for item in client.get("methods", []) if isinstance(item, dict) and item.get("method") == method] - if len(matches) > 1: - raise RuntimeConditionsError(f"{artifact.name}: duplicate client method {method}") - if matches: - operation = matches[0].get("operation") - if not isinstance(operation, str): - raise RuntimeConditionsError(f"{artifact.name}: client method {method} has no operation") - return self.resolver.resolve_static_operation(artifact, operation), UNKNOWN - owner = self._artifact(value.factory_owner) - wrappers = owner.mapping.get("python", {}).get("clientWrappers", []) - matches = [item for item in wrappers if isinstance(item, dict) and item.get("method") == method] - if len(matches) > 1: - raise RuntimeConditionsError(f"{owner.name}: duplicate client wrapper {method}") - if matches: - return self.resolve_call(matches[0].get("callRef"), owner), UNKNOWN - return [], UNKNOWN - - def resource_member(self, value: SDKResourceValue, member: str) -> tuple[str, Any] | None: - artifact = self._artifact(value.mapping) - record = self._resource(artifact, value.resource) - for key, kind in (("actions", "operation"), ("waiters", "waiter"), ("wrappers", "call"), ("relations", "resource")): - matches = [item for item in record.get(key, []) if isinstance(item, dict) and item.get("method", item.get("member")) == member] - if len(matches) > 1: - raise RuntimeConditionsError(f"{artifact.name}: duplicate {value.resource}.{member}") - if matches: - return kind, matches[0] - return None - - def resolve_resource_member(self, value: SDKResourceValue, member: str) -> tuple[list[ResolvedCondition], Any]: - artifact = self._artifact(value.mapping) - found = self.resource_member(value, member) - if found is None: - return [], UNKNOWN - kind, record = found - if kind == "operation": - return self.resolve_operation_ref(record.get("operationRef"), artifact), UNKNOWN - if kind == "call": - return self.resolve_call(record.get("callRef"), artifact), UNKNOWN - if kind == "resource": - target = record.get("resource") - if not isinstance(target, str) or not target: - raise RuntimeConditionsError(f"{artifact.name}: relation {value.resource}.{member} has no resource") - return [], SDKResourceValue(value.mapping, target) - if kind == "waiter": - return self.resolve_waiter_ref(record.get("waiterRef"), artifact), UNKNOWN - return [], UNKNOWN - - def resolve_operation_ref(self, reference: Any, owner: SDKMappingArtifact) -> list[ResolvedCondition]: - if not isinstance(reference, dict): - raise RuntimeConditionsError(f"{owner.name}: invalid operationRef") - artifact = self._artifact(self._mapping_key(reference, owner)) - operation = reference.get("operation") - if not isinstance(operation, str) or not operation: - raise RuntimeConditionsError(f"{owner.name}: operationRef has no operation") - return self.resolver.resolve_static_operation(artifact, operation) - - def resolve_waiter_ref(self, reference: Any, owner: SDKMappingArtifact) -> list[ResolvedCondition]: - if not isinstance(reference, dict): - raise RuntimeConditionsError(f"{owner.name}: invalid waiterRef") - artifact = self._artifact(self._mapping_key(reference, owner)) - waiter_name = reference.get("waiter") - client = artifact.mapping.get("python", {}).get("client", {}) - factory = client.get("waiterFactory", {}) if isinstance(client, dict) else {} - matches = [item for item in factory.get("items", []) if isinstance(item, dict) and item.get("name") == waiter_name] - if len(matches) != 1 or not isinstance(matches[0].get("operation"), str): - raise RuntimeConditionsError(f"{artifact.name}: unresolved waiter {waiter_name}") - return self.resolver.resolve_static_operation(artifact, matches[0]["operation"]) - - def resolve_call(self, reference: Any, owner: SDKMappingArtifact) -> list[ResolvedCondition]: - if not isinstance(reference, dict): - raise RuntimeConditionsError(f"{owner.name}: invalid callRef") - artifact = self._artifact(self._mapping_key(reference, owner)) - call_name = reference.get("call") - calls = artifact.mapping.get("python", {}).get("calls", []) - matches = [item for item in calls if isinstance(item, dict) and item.get("name") == call_name] - if len(matches) != 1: - raise RuntimeConditionsError(f"{artifact.name}: unresolved call {call_name}") - conditions: list[ResolvedCondition] = [] - for operation_ref in self._operation_refs(matches[0]): - for condition in self.resolve_operation_ref(operation_ref, artifact): - if condition not in conditions: - conditions.append(condition) - return conditions - - def _operation_refs(self, value: Any) -> list[dict[str, Any]]: - if isinstance(value, list): - return [reference for item in value for reference in self._operation_refs(item)] - if not isinstance(value, dict): - return [] - result = [value["operationRef"]] if isinstance(value.get("operationRef"), dict) else [] - for key, item in value.items(): - if key != "operationRef": - result.extend(self._operation_refs(item)) - return result - - def _factory_dependency_identity(self, factory: dict[str, Any], positional: list[Any], keywords: dict[str, Any], identity_seed: str) -> str: - produced = factory.get("produces") - if not isinstance(produced, dict): - raise RuntimeConditionsError("typed-state factory has no produced state") - policy = produced.get("dependencyIdentity") - if policy == "new": - return identity_seed - if policy == "inherit": - argument_state = factory.get("argumentState") - binding = argument_state.get("argument") if isinstance(argument_state, dict) else None - value = self._argument(binding, positional, keywords) - if isinstance(value, SDKTypedStateValue): - return value.dependency_identity - raise RuntimeConditionsError("typed-state factory cannot inherit an unresolved dependency identity") - raise RuntimeConditionsError("typed-state factory must declare dependencyIdentity as new or inherit") - - def _produce_typed_state(self, artifact: SDKMappingArtifact, produced: Any, receiver: SDKTypedStateValue | None, positional: list[Any], keywords: dict[str, Any], identity_seed: str) -> Any: - if produced is None: - return UNKNOWN - if not isinstance(produced, dict) or not isinstance(produced.get("stateType"), str): - raise RuntimeConditionsError(f"{artifact.name}: typed-state producer has an invalid state type") - policy = produced.get("dependencyIdentity") - if policy == "inherit" and receiver is not None: - dependency_identity = receiver.dependency_identity - elif policy == "new": - dependency_identity = identity_seed - else: - raise RuntimeConditionsError(f"{artifact.name}: typed-state producer must declare a resolvable dependency identity") - fields = self._resolve_bindings(produced.get("bindings", {}), receiver, positional, keywords) - return SDKTypedStateValue((artifact.distribution, artifact.name), produced["stateType"], dependency_identity, tuple(sorted(fields.items()))) - - def _resolve_typed_operation(self, artifact: SDKMappingArtifact, record: dict[str, Any], receiver: SDKTypedStateValue | None, positional: list[Any], keywords: dict[str, Any], dependency_identity: str) -> list[ResolvedCondition]: - if "operationRef" in record and "operations" in record: - raise RuntimeConditionsError(f"{artifact.name}: typed-state call cannot declare both operationRef and operations") - if "operationRef" not in record and "operations" not in record: - return [] - entries = record.get("operations") if "operations" in record else [record] - if not isinstance(entries, list): - raise RuntimeConditionsError(f"{artifact.name}: typed-state call operations must be a list") - resolved: list[ResolvedCondition] = [] - for entry in entries: - if not isinstance(entry, dict): - raise RuntimeConditionsError(f"{artifact.name}: typed-state call contains an invalid operation entry") - reference = entry.get("operationRef") - target = artifact - if isinstance(reference, str): - operation_name = reference - elif isinstance(reference, dict): - operation_name = reference.get("operation") - if "mapping" in reference: - target = self._artifact(self._mapping_key(reference, artifact)) - else: - operation_name = None - if not isinstance(operation_name, str) or not operation_name: - raise RuntimeConditionsError(f"{artifact.name}: typed-state call has an invalid operation reference") - values = self._resolve_bindings(entry.get("operationBindings", {}), receiver, positional, keywords) - for condition in self.resolver.resolve_bound_operation(target, operation_name, {field: self._native_value(value) for field, value in values.items()}, dependency_identity): - if condition not in resolved: - resolved.append(condition) - return resolved - - def _resolve_bindings(self, bindings: Any, receiver: SDKTypedStateValue | None, positional: list[Any], keywords: dict[str, Any]) -> dict[str, Any]: - if not isinstance(bindings, dict): - raise RuntimeConditionsError("typed-state bindings must be an object") - result: dict[str, Any] = {} - for field, binding in bindings.items(): - if not isinstance(field, str): - raise RuntimeConditionsError("typed-state binding field names must be strings") - value = self._resolve_binding(binding, receiver, positional, keywords) - if self._binding_value_is_present(value): - result[field] = value - return result - - def _resolve_binding(self, binding: Any, receiver: SDKTypedStateValue | None, positional: list[Any], keywords: dict[str, Any]) -> Any: - if not isinstance(binding, dict): - return UNKNOWN - alternatives = binding.get("anyOf") - if isinstance(alternatives, list): - for alternative in alternatives: - value = self._resolve_binding(alternative, receiver, positional, keywords) - if self._binding_value_is_present(value): - return value - return UNKNOWN - argument = binding.get("argument") - if isinstance(argument, dict): - value = self._argument(argument, positional, keywords) - field = argument.get("field") - if field is None: - return value - if not isinstance(field, str) or not field: - raise RuntimeConditionsError("typed-state argument field must be a non-empty string") - if isinstance(value, SDKTypedValueObject): - return value.field(field) - if isinstance(value, LiteralValue) and isinstance(value.value, dict) and field in value.value: - return LiteralValue(value.value[field]) - return UNKNOWN - state_field = binding.get("state") - if isinstance(state_field, str) and receiver is not None: - return receiver.field(state_field) - if "literal" in binding: - return LiteralValue(binding["literal"]) - return UNKNOWN - - def _binding_value_is_present(self, value: Any) -> bool: - return value is not UNKNOWN and not (isinstance(value, LiteralValue) and value.value is None) - - def _native_value(self, value: Any) -> Any: - if isinstance(value, LiteralValue): - return value.value - raise RuntimeConditionsError("typed-state operation binding did not resolve to a literal value") - - def _service_matches(self, factory: dict[str, Any], positional: list[Any], keywords: dict[str, Any]) -> bool: - selector = factory.get("serviceSelector", {}) - value = self._argument(selector, positional, keywords) - return isinstance(value, LiteralValue) and value.value == factory.get("service") - - def _argument(self, binding: Any, positional: list[Any], keywords: dict[str, Any]) -> Any: - if not isinstance(binding, dict): - return UNKNOWN - position = binding.get("position") - if isinstance(position, int) and position < len(positional): - return positional[position] - keyword = binding.get("keyword") - return keywords.get(keyword, UNKNOWN) if isinstance(keyword, str) else UNKNOWN - - def _argument_provided(self, binding: Any, positional: list[Any], keywords: dict[str, Any]) -> bool: - if not isinstance(binding, dict): - return False - position = binding.get("position") - if isinstance(position, int) and position < len(positional): - value = positional[position] - return not (isinstance(value, LiteralValue) and not value.value) - keyword = binding.get("keyword") - if not isinstance(keyword, str) or keyword not in keywords: - return False - value = keywords[keyword] - return not (isinstance(value, LiteralValue) and not value.value) - - def _stateful_branch_matches(self, when: dict[str, Any], positional: list[Any], keywords: dict[str, Any]) -> bool: - if isinstance(when.get("argument"), dict): - return self._argument_provided(when["argument"], positional, keywords) == bool(when.get("provided")) - alternatives = when.get("any") - if isinstance(alternatives, list) and alternatives: - return any( - isinstance(item, dict) - and isinstance(item.get("argument"), dict) - and self._argument_provided(item["argument"], positional, keywords) == bool(item.get("provided")) - for item in alternatives - ) - return False - - def _literal_string(self, value: Any) -> str | None: - return value.value if isinstance(value, LiteralValue) and isinstance(value.value, str) else None - - def _resolve_stateful_scope(self, template: dict[str, Any], state: dict[str, Any], argument: Any, positional: list[Any], keywords: dict[str, Any]) -> str | None: - resolution = template.get("scopeResolution") - if not isinstance(resolution, dict) or not isinstance(resolution.get("stateField"), str): - raise RuntimeConditionsError("stateful operation has no declarative scope resolution") - state_value = state.get(resolution["stateField"], UNKNOWN) - cases = resolution.get("cases") - if not isinstance(cases, list) or not cases: - raise RuntimeConditionsError("stateful operation has no scope resolution cases") - matches = [case for case in cases if isinstance(case, dict) and case.get("equals", UNKNOWN) == state_value] - if len(matches) != 1: - return None - case = matches[0] - if isinstance(case.get("value"), str): - return case["value"] - provided = case.get("argumentProvided") - if not isinstance(provided, dict): - raise RuntimeConditionsError("stateful operation scope case has no result") - key = "provided" if self._argument_provided(argument, positional, keywords) else "omitted" - return provided.get(key) if isinstance(provided.get(key), str) else None - - def _stateful_flow(self, value: SDKStatefulFlowValue | SDKStatefulResourceValue) -> tuple[SDKMappingArtifact, dict[str, Any]]: - artifact = self._artifact(value.mapping) - flows = [item for item in artifact.mapping.get("python", {}).get("statefulResourceFlows", []) if isinstance(item, dict) and item.get("id") == value.flow_id] - if len(flows) != 1: - raise RuntimeConditionsError(f"{artifact.name}: unresolved stateful resource flow {value.flow_id}") - return artifact, flows[0] - - def _mapping_key(self, reference: Any, owner: SDKMappingArtifact) -> MappingKey: - if not isinstance(reference, dict): - raise RuntimeConditionsError(f"{owner.name}: invalid mapping reference") - distribution = reference.get("distribution", owner.distribution) - mapping = reference.get("mapping") - if not isinstance(distribution, str) or not isinstance(mapping, str): - raise RuntimeConditionsError(f"{owner.name}: incomplete mapping reference") - key = (distribution, mapping) - owner_key = (owner.distribution, owner.name) - if key != owner_key: - declared = { - (item.get("distribution"), item.get("mapping")) - for item in owner.mapping.get("dependencies", []) - if isinstance(item, dict) and item.get("kind") == "sdkMapping" - } - if key not in declared: - raise RuntimeConditionsError(f"{owner.name}: reference to undeclared SDK mapping {key[0]}/{key[1]}") - return key - - def _ensure_dependencies(self, artifact: SDKMappingArtifact, visiting: set[MappingKey] | None = None) -> None: - key = (artifact.distribution, artifact.name) - visiting = set() if visiting is None else visiting - if key in visiting: - raise RuntimeConditionsError(f"SDK mapping dependency cycle at {key[0]}/{key[1]}") - visiting.add(key) - for dependency in artifact.mapping.get("dependencies", []): - if not isinstance(dependency, dict) or dependency.get("kind") != "sdkMapping": - continue - target = self._mapping_key(dependency, artifact) - self._ensure_dependencies(self._artifact(target), visiting) - visiting.remove(key) - - def _artifact(self, key: MappingKey) -> SDKMappingArtifact: - artifact = self.artifacts.get(key) - if artifact is None: - raise RuntimeConditionsError(f"required SDK mapping {key[0]}/{key[1]} was not discovered") - return artifact - - def _resource(self, artifact: SDKMappingArtifact, name: str) -> dict[str, Any]: - python = artifact.mapping.get("python", {}) - candidates = [] - service = python.get("serviceResource") if isinstance(python, dict) else None - if isinstance(service, dict) and service.get("name") == name: - candidates.append(service) - if isinstance(python, dict): - candidates.extend(item for item in python.get("resources", []) if isinstance(item, dict) and item.get("name") == name) - if len(candidates) != 1: - raise RuntimeConditionsError(f"{artifact.name}: unresolved resource {name}") - return candidates[0] - - -class SDKCompositionAnalyzer: - def __init__(self, source_files: list[Path], project_root: Path, registry: SDKCompositionRegistry) -> None: - self.project_root = project_root.resolve() - self.registry = registry - self.modules: dict[str, ModuleInfo] = {} - self.functions: dict[str, FunctionInfo] = {} - self.classes: dict[str, ClassInfo] = {} - self.conditions: list[ResolvedCondition] = [] - self.active_calls: set[str] = set() - for path in source_files: - self._index_module(path) - self._index_imports() - - def analyze(self) -> list[ResolvedCondition]: - for module in self.modules.values(): - frame = Frame(module, {}) - self._exec_block([node for node in module.tree.body if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef, ast.Import, ast.ImportFrom))], frame) - module.runtime_values.update(frame.values) - for function in self.functions.values(): - if function.class_name is None: - self._execute(function, [], {}) - return self.conditions - - def _index_module(self, path: Path) -> None: - module_name = self._module_name(path) - tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path)) - module = ModuleInfo(module_name, path, tree) - for node in tree.body: - if isinstance(node, (ast.Assign, ast.AnnAssign)): - value = node.value - if isinstance(value, ast.Constant) and isinstance(value.value, (str, int, float, bool)): - targets = node.targets if isinstance(node, ast.Assign) else [node.target] - for target in targets: - if isinstance(target, ast.Name): - module.literals[target.id] = LiteralValue(value.value) - elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): - name = f"{module_name}.{node.name}" - self.functions[name] = FunctionInfo(name, module_name, node) - elif isinstance(node, ast.ClassDef): - class_name = f"{module_name}.{node.name}" - self.classes[class_name] = ClassInfo(class_name, module_name, node) - for child in node.body: - if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef)): - name = f"{class_name}.{child.name}" - self.functions[name] = FunctionInfo(name, module_name, child, class_name) - self.modules[module_name] = module - - def _index_imports(self) -> None: - for module in self.modules.values(): - package = module.name if module.path.name == "__init__.py" else module.name.rpartition(".")[0] - for node in module.tree.body: - if isinstance(node, ast.Import): - for alias in node.names: - module.imports[alias.asname or alias.name.split(".")[0]] = alias.name if alias.asname else alias.name.split(".")[0] - elif isinstance(node, ast.ImportFrom): - base = self._import_base(package, node.module or "", node.level) - for alias in node.names: - if alias.name != "*": - module.imports[alias.asname or alias.name] = f"{base}.{alias.name}" if base else alias.name - - def _module_name(self, path: Path) -> str: - resolved = path.resolve() - for base in (self.project_root / "src", self.project_root): - try: - relative = resolved.relative_to(base.resolve()) - except ValueError: - continue - parts = list(relative.parts) - if parts[-1] == "__init__.py": - parts.pop() - else: - parts[-1] = Path(parts[-1]).stem - return ".".join(parts) - raise RuntimeConditionsError(f"Python source {path} is outside project {self.project_root}") - - def _import_base(self, package: str, module: str, level: int) -> str: - if level == 0: - return module - parts = package.split(".") if package else [] - keep = max(0, len(parts) - (level - 1)) - prefix = parts[:keep] - if module: - prefix.extend(module.split(".")) - return ".".join(prefix) - - def _execute(self, function: FunctionInfo, positional: list[Any], keywords: dict[str, Any], instance: AppInstance | None = None) -> ExecutionResult: - if function.name in self.active_calls: - return ExecutionResult(UNKNOWN, instance) - self.active_calls.add(function.name) - try: - values = self._bind_arguments(function.node, positional, keywords, instance) - fields = dict(instance.fields) if instance is not None else None - frame = Frame(self.modules[function.module], values, fields) - returned, value = self._exec_block(function.node.body, frame) - updated = AppInstance(instance.class_name, tuple(sorted((frame.self_fields or {}).items()))) if instance is not None else None - return ExecutionResult(value if returned else UNKNOWN, updated) - finally: - self.active_calls.remove(function.name) - - def _bind_arguments(self, node: ast.FunctionDef | ast.AsyncFunctionDef, positional: list[Any], keywords: dict[str, Any], instance: AppInstance | None) -> dict[str, Any]: - parameters = [*node.args.posonlyargs, *node.args.args] - values: dict[str, Any] = {} - offset = 0 - if parameters and parameters[0].arg in {"self", "cls"}: - values[parameters[0].arg] = instance or UNKNOWN - offset = 1 - for index, parameter in enumerate(parameters[offset:]): - values[parameter.arg] = positional[index] if index < len(positional) else keywords.get(parameter.arg, UNKNOWN) - for parameter in node.args.kwonlyargs: - values[parameter.arg] = keywords.get(parameter.arg, UNKNOWN) - return values - - def _exec_block(self, statements: list[ast.stmt], frame: Frame) -> tuple[bool, Any]: - for statement in statements: - if isinstance(statement, ast.Return): - return True, self._eval(statement.value, frame) if statement.value is not None else UNKNOWN - if isinstance(statement, ast.Assign): - value = self._eval(statement.value, frame) - for target in statement.targets: - self._assign(target, value, frame) - elif isinstance(statement, ast.AnnAssign) and statement.value is not None: - self._assign(statement.target, self._eval(statement.value, frame), frame) - elif isinstance(statement, ast.Expr): - self._eval(statement.value, frame) - elif isinstance(statement, ast.If): - returned, value = self._exec_block(statement.body, frame) - if returned: - return True, value - returned, value = self._exec_block(statement.orelse, frame) - if returned: - return True, value - elif isinstance(statement, (ast.With, ast.AsyncWith)): - for item in statement.items: - value = self._eval(item.context_expr, frame) - if item.optional_vars is not None: - self._assign(item.optional_vars, value, frame) - returned, value = self._exec_block(statement.body, frame) - if returned: - return True, value - elif isinstance(statement, (ast.For, ast.AsyncFor, ast.While, ast.Try)): - bodies = [getattr(statement, "body", []), getattr(statement, "orelse", []), getattr(statement, "finalbody", [])] - if isinstance(statement, ast.Try): - bodies.extend(handler.body for handler in statement.handlers) - for body in bodies: - returned, value = self._exec_block(body, frame) - if returned: - return True, value - return False, UNKNOWN - - def _assign(self, target: ast.expr, value: Any, frame: Frame) -> None: - if isinstance(target, ast.Name): - frame.values[target.id] = value - elif isinstance(target, ast.Attribute) and isinstance(target.value, ast.Name) and target.value.id == "self" and frame.self_fields is not None: - frame.self_fields[target.attr] = value - - def _eval(self, expression: ast.expr | None, frame: Frame) -> Any: - if expression is None: - return UNKNOWN - if isinstance(expression, ast.Constant): - return LiteralValue(expression.value) - if isinstance(expression, ast.Await): - return self._eval(expression.value, frame) - if isinstance(expression, ast.Name): - return self._name(expression.id, frame) - if isinstance(expression, ast.Attribute): - owner = self._eval(expression.value, frame) - if isinstance(owner, ExternalSymbol): - return ExternalSymbol(f"{owner.name}.{expression.attr}") - if isinstance(owner, ExternalInstance): - return ExternalSymbol(f"{owner.type_name}.{expression.attr}") - if isinstance(owner, AppInstance): - field_value = owner.field(expression.attr) - if field_value is not UNKNOWN: - return field_value - method = f"{owner.class_name}.{expression.attr}" - return BoundAppMethod(owner, method) if method in self.functions else UNKNOWN - if isinstance(owner, SDKClientValue): - return BoundSDKClientMethod(owner, expression.attr) - if isinstance(owner, SDKResourceValue): - return BoundSDKResourceMember(owner, expression.attr) - if isinstance(owner, SDKStatefulFlowValue): - return self.registry.stateful_member(owner, expression.attr) - if isinstance(owner, SDKStatefulResourceValue): - return BoundSDKStatefulResourceMethod(owner, expression.attr) - if isinstance(owner, SDKTypedStateValue): - return BoundSDKTypedStateMethod(owner, expression.attr) - return UNKNOWN - if isinstance(expression, ast.Call): - callee = self._eval(expression.func, frame) - positional = [self._eval(item, frame) for item in expression.args] - keywords = {item.arg: self._eval(item.value, frame) for item in expression.keywords if item.arg is not None} - identity_seed = f"{frame.module.name}:{expression.lineno}:{expression.col_offset}" - return self._call(callee, positional, keywords, identity_seed) - if isinstance(expression, (ast.List, ast.Tuple)): - values = [self._eval(item, frame) for item in expression.elts] - if all(isinstance(value, LiteralValue) for value in values): - native = [value.value for value in values] - return LiteralValue(tuple(native) if isinstance(expression, ast.Tuple) else native) - return UNKNOWN - if isinstance(expression, ast.Set): - for item in expression.elts: - self._eval(item, frame) - return UNKNOWN - if isinstance(expression, ast.Dict): - for item in expression.values: - self._eval(item, frame) - return UNKNOWN - if isinstance(expression, ast.Subscript): - self._eval(expression.value, frame) - self._eval(expression.slice, frame) - return UNKNOWN - if isinstance(expression, ast.BinOp): - self._eval(expression.left, frame) - self._eval(expression.right, frame) - return UNKNOWN - return UNKNOWN - - def _name(self, name: str, frame: Frame) -> Any: - if name in frame.values: - return frame.values[name] - if name in frame.module.runtime_values: - return frame.module.runtime_values[name] - if name in frame.module.literals: - return frame.module.literals[name] - target = frame.module.imports.get(name) - if target: - return self._qualified(target) - local_function = f"{frame.module.name}.{name}" - if local_function in self.functions: - return FunctionValue(local_function) - local_class = f"{frame.module.name}.{name}" - if local_class in self.classes: - return ClassValue(local_class) - return ExternalSymbol(name) - - def _qualified(self, name: str) -> Any: - if name in self.functions: - return FunctionValue(name) - if name in self.classes: - return ClassValue(name) - module_name, _, member = name.rpartition(".") - module = self.modules.get(module_name) - if module: - if member in module.literals: - return module.literals[member] - if member in module.runtime_values: - return module.runtime_values[member] - return ExternalSymbol(name) - - def _call(self, callee: Any, positional: list[Any], keywords: dict[str, Any], identity_seed: str) -> Any: - if isinstance(callee, FunctionValue): - return self._execute(self.functions[callee.name], positional, keywords).value - if isinstance(callee, ClassValue): - instance = AppInstance(callee.name) - initializer = self.functions.get(f"{callee.name}.__init__") - return self._execute(initializer, positional, keywords, instance).instance if initializer else instance - if isinstance(callee, BoundAppMethod): - function = self.functions.get(callee.function_name) - return self._execute(function, positional, keywords, callee.instance).value if function else UNKNOWN - if isinstance(callee, ExternalSymbol): - typed = self.registry.typed_factory(callee.name, positional, keywords, identity_seed) - if typed is not None: - conditions, value = typed - self._add(conditions) - return value - produced = self.registry.factory(callee.name, positional, keywords) - if produced is not None: - return produced - return ExternalInstance(self.registry.canonical_symbol(callee.name)) - if isinstance(callee, BoundSDKClientMethod): - conditions, value = self.registry.client_method(callee.client, callee.method) - self._add(conditions) - return value - if isinstance(callee, BoundSDKResourceMember): - conditions, value = self.registry.resolve_resource_member(callee.resource, callee.member) - self._add(conditions) - return value - if isinstance(callee, BoundSDKStatefulProducer): - return self.registry.produce_stateful_resource(callee.flow, positional, keywords) - if isinstance(callee, BoundSDKStatefulResourceMethod): - conditions, value = self.registry.resolve_stateful_resource_method(callee.resource, callee.method, positional, keywords) - self._add(conditions) - return value - if isinstance(callee, BoundSDKTypedStateMethod): - conditions, value = self.registry.typed_state_method(callee.state, callee.method, positional, keywords, identity_seed) - self._add(conditions) - return value - return UNKNOWN - - def _add(self, conditions: list[ResolvedCondition]) -> None: - for condition in conditions: - if condition not in self.conditions: - self.conditions.append(condition) diff --git a/archive/research-v1/sdk/discovery.py b/archive/research-v1/sdk/discovery.py deleted file mode 100644 index d2481d0..0000000 --- a/archive/research-v1/sdk/discovery.py +++ /dev/null @@ -1,185 +0,0 @@ -from __future__ import annotations - -import hashlib -import json -from importlib import metadata -from pathlib import Path -from typing import Any, Callable - -from ..constants import EXTENSION_KIND, SDK_MAPPING_API_VERSION, SDK_MAPPING_INDEX_KIND, SDK_MAPPING_KIND -from ..extension.definition import parse_extension_definition -from ..models import Diagnostic, SDKExtensionArtifact, SDKMappingArtifact -from ..util import as_map, ignored_path, scalar -from ..yamlio import Yaml - - -def sha256(path: Path) -> str: - return hashlib.sha256(path.read_bytes()).hexdigest() - - -def semantic_sha256(value: Any) -> str: - encoded = json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode("utf-8") - return hashlib.sha256(encoded).hexdigest() - - -def require(value: Any, expected: Any, description: str) -> None: - if value != expected: - raise ValueError(f"{description}: got {value!r}, expected {expected!r}") - - -def safe_relative_path(value: Any) -> Path: - if not isinstance(value, str) or not value: - raise ValueError("indexed mapping path is required") - path = Path(value) - if path.is_absolute() or ".." in path.parts: - raise ValueError(f"unsafe indexed mapping path {value!r}") - return path - - -class SDKArtifactDiscovery: - def discover(self, roots: list[Path], include_installed: bool) -> tuple[list[SDKMappingArtifact], list[SDKExtensionArtifact], list[Diagnostic]]: - mappings: list[SDKMappingArtifact] = [] - extensions: list[SDKExtensionArtifact] = [] - diagnostics: list[Diagnostic] = [] - seen_indexes: set[Path] = set() - seen_extensions: set[Path] = set() - for root in roots: - resolved = root.absolute().resolve() - for path in self._index_paths(resolved): - if path in seen_indexes: - continue - seen_indexes.add(path) - try: - mappings.extend(self._load_index(path, lambda relative, base=resolved: self._resolve_explicit(base, path, relative))) - except Exception as exc: - diagnostics.append(Diagnostic("error", "sdk-mapping", str(path), str(exc))) - for path in self._extension_paths(resolved): - if path in seen_extensions: - continue - seen_extensions.add(path) - try: - extension = self._load_extension(path) - if extension is not None: - extensions.append(extension) - except Exception as exc: - diagnostics.append(Diagnostic("error", "sdk-extension", str(path), str(exc))) - if include_installed: - for distribution in metadata.distributions(): - for relative in distribution.files or []: - if not str(relative).endswith("/runtimeconditions/index.yaml"): - continue - path = Path(distribution.locate_file(relative)).resolve() - if path in seen_indexes: - continue - seen_indexes.add(path) - try: - mappings.extend(self._load_index(path, lambda item, current=distribution: Path(current.locate_file(item)).resolve(), distribution.metadata.get("Name"), distribution.version)) - except Exception as exc: - diagnostics.append(Diagnostic("error", "sdk-mapping", str(path), str(exc))) - mappings, mapping_diagnostics = self._dedupe_mappings(mappings) - extensions, extension_diagnostics = self._dedupe_extensions(extensions) - diagnostics.extend(mapping_diagnostics) - diagnostics.extend(extension_diagnostics) - return mappings, extensions, diagnostics - - def _index_paths(self, root: Path) -> list[Path]: - if root.is_file(): - return [root] if root.name == "index.yaml" and root.parent.name == "runtimeconditions" else [] - if not root.is_dir(): - return [] - return [path.resolve() for path in sorted(root.rglob("index.yaml")) if path.parent.name == "runtimeconditions" and not ignored_path(path)] - - def _extension_paths(self, root: Path) -> list[Path]: - if root.is_file(): - return [root] if root.name == "runtimeconditions.extension.yaml" else [] - if not root.is_dir(): - return [] - return [path.resolve() for path in sorted(root.rglob("runtimeconditions.extension.yaml")) if not ignored_path(path)] - - def _resolve_explicit(self, root: Path, index_path: Path, relative: Path) -> Path: - candidates = [root / relative] - candidates.extend(parent / relative for parent in index_path.parents) - for candidate in candidates: - if candidate.is_file(): - return candidate.resolve() - raise ValueError(f"indexed mapping is absent: {relative.as_posix()}") - - def _load_index(self, index_path: Path, locate: Callable[[Path], Path], installed_name: str | None = None, installed_version: str | None = None) -> list[SDKMappingArtifact]: - index = Yaml.load(index_path) - require(index.get("apiVersion"), SDK_MAPPING_API_VERSION, "index apiVersion") - require(index.get("kind"), SDK_MAPPING_INDEX_KIND, "index kind") - index_metadata = as_map(index.get("metadata")) - distribution = scalar(index_metadata.get("distribution")) or "" - distribution_version = scalar(index_metadata.get("distributionVersion")) or "" - require(scalar(index_metadata.get("language")), "python", "index language") - if not distribution or not distribution_version: - raise ValueError("index distribution and distributionVersion are required") - if installed_name: - require(distribution.lower().replace("_", "-"), installed_name.lower().replace("_", "-"), "installed distribution") - if installed_version: - require(distribution_version, installed_version, "installed distribution version") - entries = index.get("mappings") - if not isinstance(entries, list) or not entries: - raise ValueError("index mappings are required") - result: list[SDKMappingArtifact] = [] - for entry in entries: - if not isinstance(entry, dict): - raise ValueError("index mapping entry must be a mapping") - relative = safe_relative_path(entry.get("path")) - mapping_path = locate(relative) - expected_sha = scalar(entry.get("sha256")) or "" - require(sha256(mapping_path), expected_sha, "mapping file digest") - mapping = Yaml.load(mapping_path) - require(mapping.get("apiVersion"), SDK_MAPPING_API_VERSION, "mapping apiVersion") - require(mapping.get("kind"), SDK_MAPPING_KIND, "mapping kind") - mapping_metadata = as_map(mapping.get("metadata")) - name = scalar(mapping_metadata.get("name")) or "" - require(name, scalar(entry.get("name")), "mapping name") - require(scalar(mapping_metadata.get("distribution")), distribution, "mapping distribution") - require(scalar(mapping_metadata.get("distributionVersion")), distribution_version, "mapping distribution version") - require(scalar(mapping_metadata.get("language")), "python", "mapping language") - require(scalar(mapping_metadata.get("semanticSha256")), semantic_sha256({"operations": mapping.get("operations", []), "python": mapping.get("python", {})}), "mapping semantic digest") - result.append(SDKMappingArtifact(distribution, distribution_version, name, index_path, mapping_path, expected_sha, mapping)) - return result - - def _load_extension(self, path: Path) -> SDKExtensionArtifact | None: - document = Yaml.load(path) - if document.get("kind") != EXTENSION_KIND: - return None - metadata_value = as_map(document.get("metadata")) - extension_id = scalar(metadata_value.get("id")) or "" - version = scalar(metadata_value.get("version")) or "" - semantic_digest = scalar(metadata_value.get("semanticSha256")) or "" - if not extension_id or not version or not semantic_digest: - return None - require(semantic_digest, semantic_sha256(document.get("spec", {})), "extension semantic digest") - definition = parse_extension_definition(document, extension_id, path.as_uri()) - return SDKExtensionArtifact(extension_id, version, semantic_digest, path, definition, document) - - def _dedupe_mappings(self, artifacts: list[SDKMappingArtifact]) -> tuple[list[SDKMappingArtifact], list[Diagnostic]]: - result: list[SDKMappingArtifact] = [] - seen: dict[tuple[str, str, str], SDKMappingArtifact] = {} - diagnostics: list[Diagnostic] = [] - for artifact in artifacts: - key = (artifact.distribution, artifact.distribution_version, artifact.name) - previous = seen.get(key) - if previous is None: - seen[key] = artifact - result.append(artifact) - elif previous.mapping_sha256 != artifact.mapping_sha256: - diagnostics.append(Diagnostic("error", "sdk-mapping", str(artifact.mapping_path), f"conflicting SDK mapping {key} already discovered at {previous.mapping_path}")) - return result, diagnostics - - def _dedupe_extensions(self, artifacts: list[SDKExtensionArtifact]) -> tuple[list[SDKExtensionArtifact], list[Diagnostic]]: - result: list[SDKExtensionArtifact] = [] - seen: dict[tuple[str, str], SDKExtensionArtifact] = {} - diagnostics: list[Diagnostic] = [] - for artifact in artifacts: - key = (artifact.id, artifact.version) - previous = seen.get(key) - if previous is None: - seen[key] = artifact - result.append(artifact) - elif previous.semantic_sha256 != artifact.semantic_sha256: - diagnostics.append(Diagnostic("error", "sdk-extension", str(artifact.path), f"conflicting extension release {key} already discovered at {previous.path}")) - return result, diagnostics diff --git a/archive/research-v1/sdk/python.py b/archive/research-v1/sdk/python.py deleted file mode 100644 index 319a503..0000000 --- a/archive/research-v1/sdk/python.py +++ /dev/null @@ -1,452 +0,0 @@ -from __future__ import annotations - -import ast -import copy -from dataclasses import dataclass, field -from pathlib import Path -from typing import Any, Optional - -from jsonschema import Draft202012Validator - -from ..errors import RuntimeConditionsError -from ..models import SDKExtensionArtifact, SDKMappingArtifact -from ..source.python import expression_name -from ..util import add_extension_closure - - -@dataclass(frozen=True) -class BoundCall: - positional: list[ast.expr] - keywords: dict[str, ast.expr] - - @staticmethod - def from_call(call: ast.Call) -> "BoundCall": - return BoundCall(list(call.args), {item.arg: item.value for item in call.keywords if item.arg is not None}) - - def argument(self, binding: dict[str, Any]) -> ast.expr | None: - position = binding.get("position") - if isinstance(position, int) and position < len(self.positional): - return self.positional[position] - keyword = binding.get("keyword") - return self.keywords.get(keyword) if isinstance(keyword, str) else None - - def argument_source(self, binding: dict[str, Any]) -> tuple[ast.expr | None, str]: - position = binding.get("position") - if isinstance(position, int) and position < len(self.positional): - return self.positional[position], "positional" - keyword = binding.get("keyword") - if isinstance(keyword, str) and keyword in self.keywords: - return self.keywords[keyword], "keyword" - return None, "" - - -@dataclass(frozen=True) -class PythonTypeRef: - module_root: str - class_name: str - - -@dataclass(frozen=True) -class MappedMethod: - artifact: SDKMappingArtifact - method: dict[str, Any] - - -@dataclass(frozen=True) -class MappedDelegation: - artifact: SDKMappingArtifact - delegation: dict[str, Any] - - -@dataclass(frozen=True) -class ResolvedCondition: - extension_id: str - kind: str - interface_type: str - operation: dict[str, Any] - dependency_identity: str | None = None - - -class SDKMappingIndex: - def __init__(self, artifacts: list[SDKMappingArtifact]) -> None: - self.methods: dict[tuple[str, str, str], list[MappedMethod]] = {} - self.delegations: dict[tuple[str, str, str], list[MappedDelegation]] = {} - self.classes: set[tuple[str, str]] = set() - for artifact in artifacts: - python = artifact.mapping.get("python") - if not isinstance(python, dict): - continue - for method in python.get("apiMethods", []): - if not isinstance(method, dict): - continue - for symbol in method.get("symbols", []): - key = self._symbol_key(symbol) - self.methods.setdefault(key, []).append(MappedMethod(artifact, method)) - self.classes.add((key[0], key[1])) - for delegation in python.get("conditionDelegations", []): - if not isinstance(delegation, dict): - continue - for symbol in delegation.get("symbols", []): - key = self._symbol_key(symbol) - self.delegations.setdefault(key, []).append(MappedDelegation(artifact, delegation)) - self.classes.add((key[0], key[1])) - - def _symbol_key(self, symbol: Any) -> tuple[str, str, str]: - if not isinstance(symbol, dict): - raise RuntimeConditionsError("SDK mapping contains an invalid Python symbol") - module = symbol.get("module") - class_name = symbol.get("class") - method = symbol.get("method") - if not all(isinstance(item, str) and item for item in (module, class_name, method)): - raise RuntimeConditionsError("SDK mapping contains an incomplete Python symbol") - return module.split(".")[0], class_name, method - - def has_class(self, reference: PythonTypeRef) -> bool: - return (reference.module_root, reference.class_name) in self.classes - - def method(self, reference: PythonTypeRef, member: str) -> Optional[MappedMethod]: - return self._one(self.methods.get((reference.module_root, reference.class_name, member), []), f"{reference.module_root}.{reference.class_name}.{member}") - - def delegation(self, reference: PythonTypeRef, member: str) -> Optional[MappedDelegation]: - return self._one(self.delegations.get((reference.module_root, reference.class_name, member), []), f"{reference.module_root}.{reference.class_name}.{member}") - - def _one(self, candidates: list[Any], name: str) -> Any: - unique: list[Any] = [] - seen: set[tuple[str, str]] = set() - for candidate in candidates: - key = (str(candidate.artifact.mapping_path), str(candidate.method if isinstance(candidate, MappedMethod) else candidate.delegation)) - if key not in seen: - seen.add(key) - unique.append(candidate) - if len(unique) > 1: - raise RuntimeConditionsError(f"ambiguous installed SDK mapping for {name}") - return unique[0] if unique else None - - -class ImportState: - def __init__(self, other: "ImportState | None" = None) -> None: - self.aliases = dict(other.aliases) if other else {} - self.variables = dict(other.variables) if other else {} - - def normalize(self, name: str) -> str: - parts = name.split(".") - replacement = self.aliases.get(parts[0]) - return ".".join([replacement, *parts[1:]]) if replacement else name - - -class SDKSourceVisitor(ast.NodeVisitor): - def __init__(self, mapping_index: SDKMappingIndex, resolver: "SDKConditionResolver") -> None: - self.mapping_index = mapping_index - self.resolver = resolver - self.states = [ImportState()] - self.resolved: list[ResolvedCondition] = [] - - @property - def state(self) -> ImportState: - return self.states[-1] - - def visit_Import(self, node: ast.Import) -> None: - for alias in node.names: - self.state.aliases[alias.asname or alias.name.split(".")[0]] = alias.name - - def visit_ImportFrom(self, node: ast.ImportFrom) -> None: - if not node.module: - return - for alias in node.names: - if alias.name != "*": - self.state.aliases[alias.asname or alias.name] = f"{node.module}.{alias.name}" - - def visit_FunctionDef(self, node: ast.FunctionDef) -> None: - self.states.append(ImportState(self.state)) - for statement in node.body: - self.visit(statement) - self.states.pop() - - def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None: - self.visit_FunctionDef(node) # type: ignore[arg-type] - - def visit_Assign(self, node: ast.Assign) -> None: - self.visit(node.value) - reference = self.constructor_reference(node.value) - if reference is None: - return - for target in node.targets: - if isinstance(target, ast.Name): - self.state.variables[target.id] = reference - - def visit_AnnAssign(self, node: ast.AnnAssign) -> None: - if node.value is None: - return - self.visit(node.value) - reference = self.constructor_reference(node.value) - if reference is not None and isinstance(node.target, ast.Name): - self.state.variables[node.target.id] = reference - - def visit_Call(self, node: ast.Call) -> None: - identity = self.method_identity(node.func) - if identity is not None: - reference, member = identity - delegation = self.mapping_index.delegation(reference, member) - if delegation is not None: - self._resolve_delegation(node, delegation) - else: - method = self.mapping_index.method(reference, member) - if method is not None: - self._add(self.resolver.resolve(method, BoundCall.from_call(node))) - self.generic_visit(node) - - def constructor_reference(self, expression: ast.expr) -> PythonTypeRef | None: - if not isinstance(expression, ast.Call): - return None - name = expression_name(expression.func) - if not name: - return None - normalized = self.state.normalize(name) - parts = normalized.split(".") - if len(parts) < 2: - return None - reference = PythonTypeRef(parts[0], parts[-1]) - return reference if self.mapping_index.has_class(reference) else None - - def method_identity(self, expression: ast.expr) -> tuple[PythonTypeRef, str] | None: - if not isinstance(expression, ast.Attribute): - return None - if isinstance(expression.value, ast.Name): - reference = self.state.variables.get(expression.value.id) - if reference is not None: - return reference, expression.attr - reference = self.constructor_reference(expression.value) - if reference is not None: - return reference, expression.attr - return None - - def _resolve_delegation(self, call: ast.Call, mapped: MappedDelegation) -> None: - bound = BoundCall.from_call(call) - delegate = mapped.delegation.get("delegate", {}) - callable_binding = delegate.get("callableArgument", {}) - callable_expression, source = bound.argument_source(callable_binding) - if callable_expression is None: - return - identity = self.method_identity(callable_expression) - if identity is None: - return - target = self.mapping_index.method(*identity) - if target is None: - return - forwarded = delegate.get("forwardedArguments", {}) - positional = forwarded.get("positional", {}) if isinstance(forwarded, dict) else {} - start = positional.get("fromPosition") - if not isinstance(start, int): - raise RuntimeConditionsError(f"{mapped.delegation.get('id')}: invalid forwarded positional arguments") - forwarded_positionals = list(bound.positional[start if source == "positional" else max(0, start - 1) :]) - forwarded_keywords = dict(bound.keywords) if forwarded.get("keywords") is True else {} - keyword_name = callable_binding.get("keyword") - if isinstance(keyword_name, str): - forwarded_keywords.pop(keyword_name, None) - for activation in delegate.get("activateTargetConditionals", []): - argument = activation.get("argument", {}) if isinstance(activation, dict) else {} - keyword = argument.get("keyword") - if isinstance(keyword, str) and activation.get("equals") is not None: - forwarded_keywords[keyword] = ast.Constant(value=activation["equals"]) - self._add(self.resolver.resolve(target, BoundCall(forwarded_positionals, forwarded_keywords))) - - def _add(self, conditions: list[ResolvedCondition]) -> None: - for condition in conditions: - if condition not in self.resolved: - self.resolved.append(condition) - - -class SDKConditionResolver: - def __init__(self, extensions: list[SDKExtensionArtifact]) -> None: - self.extensions = extensions - - def resolve(self, mapped: MappedMethod, call: BoundCall) -> list[ResolvedCondition]: - mapping = mapped.artifact.mapping - operations = {item.get("name"): item for item in mapping.get("operations", []) if isinstance(item, dict) and isinstance(item.get("name"), str)} - reference = mapped.method.get("operationRef", {}) - record = operations.get(reference.get("operation")) - if record is None: - raise RuntimeConditionsError(f"unknown SDK operation reference {reference}") - resolved: list[ResolvedCondition] = [] - if "conditions" in record: - conditions = record.get("conditions") - if not isinstance(conditions, list): - raise RuntimeConditionsError("SDK operation conditions must be a list") - for item in conditions: - if not isinstance(item, dict) or not isinstance(item.get("operation"), dict): - raise RuntimeConditionsError("SDK operation condition is invalid") - resolved.append(ResolvedCondition(mapping["extension"]["id"], item["kind"], item["interfaceType"], copy.deepcopy(item["operation"]))) - else: - template = record.get("conditionTemplate") - if not isinstance(template, dict): - raise RuntimeConditionsError("SDK operation has neither conditions nor conditionTemplate") - operation = self._resolve_template(call, template) - if operation is None: - return [] - resolved.append(ResolvedCondition(mapping["extension"]["id"], template["kind"], template["interfaceType"], operation)) - conditional = mapped.method.get("conditionalOperation") - if isinstance(conditional, dict): - predicate = call.argument(conditional.get("when", {}).get("argument", {})) - if isinstance(predicate, ast.Constant) and predicate.value == conditional.get("when", {}).get("equals"): - for item in resolved: - item.operation.update(conditional.get("operationOverride", {})) - elif predicate is not None and not isinstance(predicate, ast.Constant): - return [] - extension = self._extension_for(mapping) - for item in resolved: - self._validate_condition(extension, item) - return resolved - - def resolve_static_operation(self, artifact: SDKMappingArtifact, operation_name: str) -> list[ResolvedCondition]: - mapping = artifact.mapping - matches = [item for item in mapping.get("operations", []) if isinstance(item, dict) and item.get("name") == operation_name] - if len(matches) != 1: - raise RuntimeConditionsError(f"{artifact.name}: expected one SDK operation {operation_name}, found {len(matches)}") - conditions = matches[0].get("conditions") - if not isinstance(conditions, list) or not conditions: - raise RuntimeConditionsError(f"{artifact.name}: SDK operation {operation_name} has no static conditions") - resolved: list[ResolvedCondition] = [] - for item in conditions: - if not isinstance(item, dict) or not isinstance(item.get("operation"), dict) or not isinstance(item.get("kind"), str) or not isinstance(item.get("interfaceType"), str): - raise RuntimeConditionsError(f"{artifact.name}: SDK operation {operation_name} contains an invalid condition") - resolved.append(ResolvedCondition(mapping["extension"]["id"], item["kind"], item["interfaceType"], copy.deepcopy(item["operation"]))) - extension = self._extension_for(mapping) - for item in resolved: - self._validate_condition(extension, item) - return resolved - - def resolve_bound_operation(self, artifact: SDKMappingArtifact, operation_name: str, values: dict[str, Any], dependency_identity: str | None) -> list[ResolvedCondition]: - mapping = artifact.mapping - matches = [item for item in mapping.get("operations", []) if isinstance(item, dict) and item.get("name") == operation_name] - if len(matches) != 1: - raise RuntimeConditionsError(f"{artifact.name}: expected one bound SDK operation {operation_name}, found {len(matches)}") - conditions = matches[0].get("conditions") - if not isinstance(conditions, list) or not conditions: - raise RuntimeConditionsError(f"{artifact.name}: SDK operation {operation_name} has no conditions") - resolved: list[ResolvedCondition] = [] - for item in conditions: - if not isinstance(item, dict) or not isinstance(item.get("operation"), dict) or not isinstance(item.get("kind"), str) or not isinstance(item.get("interfaceType"), str): - raise RuntimeConditionsError(f"{artifact.name}: SDK operation {operation_name} contains an invalid condition") - bindings = item.get("bindings", {}) - required = bindings.get("required", []) if isinstance(bindings, dict) else [] - optional = bindings.get("optional", []) if isinstance(bindings, dict) else [] - if not isinstance(required, list) or not isinstance(optional, list) or any(not isinstance(field, str) for field in [*required, *optional]): - raise RuntimeConditionsError(f"{artifact.name}: SDK operation {operation_name} contains invalid binding requirements") - if any(field not in values for field in required): - continue - operation = copy.deepcopy(item["operation"]) - for field in [*required, *optional]: - if field in values: - operation[field] = copy.deepcopy(values[field]) - resolved.append(ResolvedCondition(mapping["extension"]["id"], item["kind"], item["interfaceType"], operation, dependency_identity)) - extension = self._extension_for(mapping) - for item in resolved: - self._validate_condition(extension, item) - return resolved - - def resolve_stateful_operation(self, artifact: SDKMappingArtifact, operation_name: str, state: dict[str, Any], scope: str) -> list[ResolvedCondition]: - mapping = artifact.mapping - matches = [item for item in mapping.get("operations", []) if isinstance(item, dict) and item.get("name") == operation_name] - if len(matches) != 1: - raise RuntimeConditionsError(f"{artifact.name}: expected one stateful SDK operation {operation_name}, found {len(matches)}") - template = matches[0].get("conditionTemplate") - if not isinstance(template, dict) or not isinstance(template.get("operation"), dict): - raise RuntimeConditionsError(f"{artifact.name}: stateful SDK operation {operation_name} has no condition template") - operation = copy.deepcopy(template["operation"]) - bindings = template.get("stateBindings") - if not isinstance(bindings, dict) or not bindings: - raise RuntimeConditionsError(f"{artifact.name}: stateful SDK operation {operation_name} has no state bindings") - for field, state_field in bindings.items(): - value = state.get(state_field) if isinstance(state_field, str) else None - if not isinstance(value, str): - return [] - operation[field] = value - operation["scope"] = scope - verb = operation.get("verb") - supported = state.get("operations") - if not isinstance(verb, str) or not isinstance(supported, list): - return [] - allowed = any(isinstance(item, dict) and item.get("verb") == verb and scope in item.get("scopes", []) for item in supported) - if not allowed: - return [] - kind = template.get("kind") - interface_type = template.get("interfaceType") - if not isinstance(kind, str) or not isinstance(interface_type, str): - raise RuntimeConditionsError(f"{artifact.name}: stateful SDK operation {operation_name} has invalid condition coordinates") - resolved = ResolvedCondition(mapping["extension"]["id"], kind, interface_type, operation) - self._validate_condition(self._extension_for(mapping), resolved) - return [resolved] - - def _resolve_template(self, call: BoundCall, template: dict[str, Any]) -> dict[str, Any] | None: - operation = copy.deepcopy(template.get("operation", {})) - if "pathTemplate" in operation: - path = operation.pop("pathTemplate") - for variable, binding in template.get("pathVariables", {}).items(): - value = self._string_argument(call, binding) - if value is None: - return None - path = path.replace("{" + variable + "}", value) - operation["path"] = path - return operation - for field, binding in template.get("operationBindings", {}).items(): - value = self._string_argument(call, binding) - if value is None: - return None - operation[field] = value - return operation - - def _string_argument(self, call: BoundCall, binding: dict[str, Any]) -> str | None: - value = call.argument(binding) - return value.value if isinstance(value, ast.Constant) and isinstance(value.value, str) else None - - def _extension_for(self, mapping: dict[str, Any]) -> SDKExtensionArtifact: - coordinates = mapping.get("extension", {}) - matches = [item for item in self.extensions if item.id == coordinates.get("id") and item.version == str(coordinates.get("version")) and item.semantic_sha256 == coordinates.get("semanticSha256")] - if len(matches) != 1: - raise RuntimeConditionsError(f"SDK mapping requires one exact extension release {coordinates}, found {len(matches)}") - return matches[0] - - def _validate_condition(self, extension: SDKExtensionArtifact, resolved: ResolvedCondition) -> None: - schemas = extension.document.get("spec", {}).get("schemas", []) - matches = [item.get("schema") for item in schemas if isinstance(item, dict) and item.get("appliesToKind") == resolved.kind and item.get("appliesToInterfaceType") == resolved.interface_type] - if len(matches) != 1 or not isinstance(matches[0], dict): - raise RuntimeConditionsError(f"extension {extension.id} does not define exactly one schema for {resolved.kind}/{resolved.interface_type}") - condition = {"kind": resolved.kind, "interface": {"type": resolved.interface_type, "operations": [resolved.operation]}} - errors = sorted(Draft202012Validator(matches[0]).iter_errors(condition), key=lambda error: list(error.path)) - if errors: - raise RuntimeConditionsError(f"SDK-resolved condition does not align to extension {extension.id}: {errors[0].message}") - - -class SDKPythonExtractor: - def __init__(self, mappings: list[SDKMappingArtifact], extensions: list[SDKExtensionArtifact]) -> None: - self.mappings = mappings - self.extensions = extensions - - def extract(self, source_files: list[Path], project_root: Path) -> tuple[list[dict[str, Any]], list[str]]: - mapping_index = SDKMappingIndex(self.mappings) - resolver = SDKConditionResolver(self.extensions) - resolved: list[ResolvedCondition] = [] - for source in source_files: - tree = ast.parse(source.read_text(encoding="utf-8"), filename=str(source)) - visitor = SDKSourceVisitor(mapping_index, resolver) - visitor.visit(tree) - for item in visitor.resolved: - if item not in resolved: - resolved.append(item) - from .composition import SDKCompositionAnalyzer, SDKCompositionRegistry - - composition = SDKCompositionAnalyzer(source_files, project_root, SDKCompositionRegistry(self.mappings, resolver)) - for item in composition.analyze(): - if item not in resolved: - resolved.append(item) - grouped: dict[tuple[str, str, str, str | None], list[dict[str, Any]]] = {} - for item in resolved: - operations = grouped.setdefault((item.extension_id, item.kind, item.interface_type, item.dependency_identity), []) - if item.operation not in operations: - operations.append(item.operation) - conditions = [{"kind": kind, "interface": {"type": interface_type, "operations": operations}} for (_, kind, interface_type, _), operations in grouped.items()] - dependencies = {item.id: item.definition.dependencies for item in self.extensions} - extension_ids: set[str] = set() - for extension_id, _, _, _ in grouped: - add_extension_closure(extension_id, dependencies, extension_ids) - return conditions, sorted(extension_ids) diff --git a/archive/research-v1/test_profiler.py b/archive/research-v1/test_profiler.py deleted file mode 100644 index eaaef9c..0000000 --- a/archive/research-v1/test_profiler.py +++ /dev/null @@ -1,633 +0,0 @@ -from __future__ import annotations - -import copy -import hashlib -import shutil -import sys -import tempfile -from importlib import metadata -from pathlib import Path -from unittest.mock import patch - -import pytest -import yaml - -ROOT = Path(__file__).resolve().parents[2] -PROFILER_ROOT = ROOT / "python-rc-profiler" -TESTDATA = PROFILER_ROOT / "testdata" - -sys.path.insert(0, str(PROFILER_ROOT)) - -from runtimeconditions_profiler.main import ( # noqa: E402 - ArtifactDiscovery, - ArtifactValidator, - DiscoveryOptions, - ProfileExtractor, - ProfileOptions, - ProfileValidator, - ProfileYamlWriter, - ProjectDiscovery, - RuntimeConditionsError, -) -from runtimeconditions_profiler.models import SDKMappingArtifact # noqa: E402 -from runtimeconditions_profiler.sdk.python import SDKPythonExtractor # noqa: E402 - - -COMMON = ROOT / "extensions" / "catalog" / "rc" / "common-integrations" / "python" -ENV = ROOT / "extensions" / "catalog" / "rc" / "env-configuration" / "python" -KUBERNETES_EXTENSION = ROOT / "extensions" / "catalog" / "kubernetes" / "api" / "releases" / "0.1.0" / "runtimeconditions.extension.yaml" -KUBERNETES_MAPPING = ROOT / "sdk" / "archive" / "research-v1" / "authorship" / "kubernetes-python" / "mappings" / "runtimeconditions.sdk-mapping.yaml" -KUBERNETES_CONFIGMAP_APP = ROOT / "sdk" / "archive" / "research-v1" / "kubernetes" / "python" / "configmap-reader" -KUBERNETES_WATCH_APP = ROOT / "sdk" / "archive" / "research-v1" / "kubernetes" / "python" / "pod-watcher" -KUBERNETES_DYNAMIC_CONFIGMAP_APP = ROOT / "sdk" / "archive" / "research-v1" / "kubernetes" / "python" / "dynamic-configmap-lifecycle" -KUBERNETES_DYNAMIC_CRD_APP = ROOT / "sdk" / "archive" / "research-v1" / "kubernetes" / "python" / "dynamic-crd-reader" -KUBERNETES_DYNAMIC_WATCH_APP = ROOT / "sdk" / "archive" / "research-v1" / "kubernetes" / "python" / "dynamic-pod-watcher" -AWS_EXTENSION = ROOT / "extensions" / "catalog" / "aws" / "s3" / "releases" / "0.1.0" / "runtimeconditions.extension.yaml" -AWS_MAPPINGS = { - "boto3": ROOT / "extensions" / "catalog" / "aws" / "s3" / "archive" / "research-v1" / "mappings" / "boto3" / "runtimeconditions.sdk-mapping.yaml", - "botocore": ROOT / "extensions" / "catalog" / "aws" / "s3" / "archive" / "research-v1" / "mappings" / "botocore" / "runtimeconditions.sdk-mapping.yaml", - "s3transfer": ROOT / "extensions" / "catalog" / "aws" / "s3" / "archive" / "research-v1" / "mappings" / "s3transfer" / "runtimeconditions.sdk-mapping.yaml", -} -AWS_APPS = ROOT / "sdk" / "s3" / "python" -AWS_PROFILE_RESULTS = ROOT / "sdk" / "archive" / "research-v1" / "authorship" / "aws-python" / "results" / "profiles" -NATS_EXTENSION = ROOT / "extensions" / "catalog" / "nats" / "service" / "releases" / "0.1.0" / "runtimeconditions.extension.yaml" -NATS_MAPPING = ROOT / "sdk" / "archive" / "research-v1" / "authorship" / "nats-python" / "mappings" / "runtimeconditions.sdk-mapping.yaml" -NATS_APPS = ROOT / "sdk" / "archive" / "research-v1" / "nats" / "python" -NATS_PROFILE_RESULTS = ROOT / "sdk" / "archive" / "research-v1" / "authorship" / "nats-python" / "results" / "profiles" - - -def test_authoring_fixtures() -> None: - fixtures_root = TESTDATA / "authoring" - for fixture in sorted(path for path in fixtures_root.iterdir() if path.is_dir()): - config = yaml.safe_load((fixture / "fixture.yaml").read_text()) - result = ProjectDiscovery().discover(fixture, DiscoveryOptions()) - if config["valid"]: - assert not result.has_errors, f"{fixture.name} should be valid: {messages(result.diagnostics)}" - else: - assert result.has_errors, f"{fixture.name} should fail" - expected = config.get("wantErrorContains") - if expected: - assert expected in messages(result.diagnostics) - - -def test_repository_python_bindings_validate() -> None: - artifacts = [] - discovery = ArtifactDiscovery() - artifacts.extend(discovery.discover_path_artifact(COMMON)) - artifacts.extend(discovery.discover_path_artifact(ENV)) - validated = ArtifactValidator().validate(artifacts) - diagnostics = [diagnostic for artifact in validated for diagnostic in artifact.diagnostics] - assert diagnostics == [] - assert len(validated) == 2 - common = next(item for item in validated if item.manifest and item.manifest.package == "runtimeconditions_common_integrations") - env = next(item for item in validated if item.manifest and item.manifest.package == "runtimeconditions_env_configuration") - assert len(common.manifest.constants) == 10 - assert len(common.manifest.declarations) == 3 - assert len(env.manifest.options) == 2 - - -def test_pyproject_package_path_resolution() -> None: - result = ProjectDiscovery().discover( - TESTDATA / "profile-generation" / "declarative-app", - DiscoveryOptions(resolve_package_paths=True), - ) - assert not result.has_errors, messages(result.diagnostics) - assert COMMON.resolve() in result.package_paths - assert ENV.resolve() in result.package_paths - assert len(result.validated_artifacts) == 2 - - -@pytest.mark.parametrize( - ("fixture", "name", "workload_uri", "package_paths", "golden"), - [ - ( - "declarative-app", - "python-declarative-app", - "example/python-declarative-app", - [COMMON, ENV], - "declarative-app.golden.yaml", - ), - ( - "wildcard-import", - "python-wildcard-cache", - "example/python-wildcard-cache", - [COMMON], - "wildcard-cache.golden.yaml", - ), - ( - "semantic-resolution", - "python-semantic-resolution", - "example/python-semantic-resolution", - [COMMON, ENV], - "semantic-resolution.golden.yaml", - ), - ( - "unused-extension", - "python-unused-extension", - "example/python-unused-extension", - [COMMON, ENV], - "unused-extension.golden.yaml", - ), - ], -) -def test_profile_generation_golden( - fixture: str, - name: str, - workload_uri: str, - package_paths: list[Path], - golden: str, -) -> None: - project = TESTDATA / "profile-generation" / fixture - profile = extract_profile(project, name, workload_uri, "test", package_paths) - assert normalize(ProfileYamlWriter.write(profile)) == normalize((TESTDATA / "golden" / golden).read_text()) - - -def test_request_logger_demo_profile_generation() -> None: - project = ROOT / "rc-demos" / "apps" / "request-logger-http-python" - profile = extract_profile( - project, - "request-logger-http", - "github.com/runtimeconditions/rc-demos/apps/request-logger-http-python", - "dev", - [COMMON, ENV], - ) - assert normalize(ProfileYamlWriter.write(profile)) == normalize( - (TESTDATA / "golden" / "request-logger-http-python.golden.yaml").read_text() - ) - - -def test_generated_profile_validation_rejects_bad_values() -> None: - project = TESTDATA / "profile-generation" / "declarative-app" - options = DiscoveryOptions(package_paths=[COMMON, ENV]) - discovery = ProjectDiscovery().discover(project, options) - profile = ProfileExtractor().extract( - project, - ProfileOptions( - name="python-declarative-app", - workload_uri="example/python-declarative-app", - workload_version="test", - discovery_options=options, - ), - ) - - unknown_kind = copy.deepcopy(profile) - unknown_kind["conditions"][0]["kind"] = "worker" - assert_profile_invalid(unknown_kind, discovery, "conditions[0].kind worker") - - unknown_interface = copy.deepcopy(profile) - unknown_interface["conditions"][0]["interface"]["type"] = "grpc" - assert_profile_invalid(unknown_interface, discovery, "conditions[0].interface.type api/grpc") - - invalid_method = copy.deepcopy(profile) - invalid_method["conditions"][0]["interface"]["operations"][0]["method"] = "FETCH" - assert_profile_invalid(invalid_method, discovery, "conditions[0].interface.operations[0].method FETCH") - - invalid_env = copy.deepcopy(profile) - invalid_env["conditions"][0]["configuration"]["env"][0]["property"] = "apiKey" - assert_profile_invalid(invalid_env, discovery, "conditions[0].configuration.env[0].property apiKey") - - missing_dependency = copy.deepcopy(profile) - missing_dependency["extensions"] = [ - "https://runtimeconditions.io/extensions/env-configuration/v1alpha1/runtimeconditions.extension.yaml" - ] - assert_profile_invalid( - missing_dependency, - discovery, - "extensions missing dependency https://runtimeconditions.io/extensions/common-integrations/v1alpha1/runtimeconditions.extension.yaml", - ) - - -def test_generate_fails_before_extraction_when_extension_validation_fails() -> None: - with pytest.raises(RuntimeConditionsError) as exc: - extract_profile( - TESTDATA / "profile-generation" / "declarative-app", - "broken", - "example/broken", - "test", - [TESTDATA / "authoring" / "binding-vocabulary-invalid" / "extension"], - ) - assert "artifact validation failed" in str(exc.value) - - -def test_installed_sdk_mapping_resolves_direct_kubernetes_call_without_bindings() -> None: - with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - package_paths = stage_kubernetes_sdk_catalog(root) - installed = metadata.Distribution.at(root / "site/kubernetes-36.0.3.dist-info") - before = {name for name in sys.modules if name == "kubernetes" or name.startswith("kubernetes.")} - with patch("runtimeconditions_profiler.sdk.discovery.metadata.distributions", return_value=[installed]): - profile = extract_profile(KUBERNETES_CONFIGMAP_APP, "kubernetes-configmap-reader", "example/kubernetes-configmap-reader", "test", [package_paths[1]]) - after = {name for name in sys.modules if name == "kubernetes" or name.startswith("kubernetes.")} - - assert before == after - assert profile["extensions"] == ["https://runtimeconditions.io/extensions/kubernetes-api/0.1.0/runtimeconditions.extension.yaml"] - assert profile["conditions"] == [{"kind": "kubernetes", "interface": {"type": "api", "operations": [{"verb": "get", "apiGroup": "", "apiVersion": "v1", "resource": "configmaps", "scope": "namespaced"}]}}] - - -def test_aws_direct_client_resolves_factory_and_botocore_operation() -> None: - with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - package_paths = stage_aws_sdk_catalog(root) - installed = [metadata.Distribution.at(root / f"site/{distribution}-{yaml.safe_load(source.read_text(encoding='utf-8'))['metadata']['distributionVersion']}.dist-info") for distribution, source in AWS_MAPPINGS.items()] - before = {name for name in sys.modules if name in AWS_MAPPINGS or any(name.startswith(f"{distribution}.") for distribution in AWS_MAPPINGS)} - with patch("runtimeconditions_profiler.sdk.discovery.metadata.distributions", return_value=installed): - profile = extract_aws_profile("direct-client", [package_paths[1]]) - after = {name for name in sys.modules if name in AWS_MAPPINGS or any(name.startswith(f"{distribution}.") for distribution in AWS_MAPPINGS)} - - assert before == after - assert profile["extensions"] == ["https://runtimeconditions.io/extensions/aws-s3/0.1.0/runtimeconditions.extension.yaml"] - assert profile["conditions"] == [{"kind": "aws.s3", "interface": {"type": "bucket", "operations": [{"name": "PutObject"}]}}] - assert_aws_profile_golden("direct-client", profile) - - -@pytest.mark.parametrize("fixture", ["session-client", "factory-wrapper", "dependency-injection", "resource-api"]) -def test_aws_put_object_client_flows_resolve_without_application_annotations(fixture: str) -> None: - with tempfile.TemporaryDirectory() as directory: - package_paths = stage_aws_sdk_catalog(Path(directory)) - profile = extract_aws_profile(fixture, package_paths) - - assert profile["conditions"] == [{"kind": "aws.s3", "interface": {"type": "bucket", "operations": [{"name": "PutObject"}]}}] - assert_aws_profile_golden(fixture, profile) - - -def test_aws_dynamic_service_does_not_infer_s3_from_an_unresolved_factory_selector() -> None: - with tempfile.TemporaryDirectory() as directory: - package_paths = stage_aws_sdk_catalog(Path(directory)) - profile = extract_aws_profile("dynamic-service", package_paths) - - assert profile["extensions"] == [] - assert profile["conditions"] == [] - assert_aws_profile_golden("dynamic-service", profile) - - -@pytest.mark.parametrize("fixture", ["core-messaging", "jetstream-publisher", "jetstream-consumer", "key-value", "object-store", "complete-service"]) -def test_nats_python_typed_state_flows_match_reviewed_profiles(fixture: str) -> None: - with tempfile.TemporaryDirectory() as directory: - package_paths = stage_nats_sdk_catalog(Path(directory)) - profile = extract_profile(NATS_APPS / fixture, f"nats-python-{fixture}", f"https://github.com/runtimeconditions/sdk-authorship-discovery/tree/main/nats/python/{fixture}", "0.1.0", package_paths) - - assert normalize(ProfileYamlWriter.write(profile)) == normalize((NATS_PROFILE_RESULTS / f"{fixture}.yaml").read_text(encoding="utf-8")) - - -def test_nats_python_does_not_emit_an_operation_with_an_unresolved_required_binding() -> None: - source = """import nats - -async def run(subject: str) -> None: - client = await nats.connect() - await client.publish(subject, b\"payload\") -""" - with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - project = root / "project" - project.mkdir() - (project / "app.py").write_text(source, encoding="utf-8") - profile = extract_profile(project, "nats-dynamic-subject", "example/nats-dynamic-subject", "test", stage_nats_sdk_catalog(root / "catalog")) - - assert profile["conditions"] == [{"kind": "nats", "interface": {"type": "service", "operations": [{"resource": "connection", "action": "connect"}]}}] - - -def test_generic_typed_value_bindings_fall_through_and_omit_explicit_none() -> None: - source = """import nats -from nats.js.api import ConsumerConfig, StreamConfig - -async def run() -> None: - client = await nats.connect() - jetstream = client.jetstream() - await jetstream.add_stream(StreamConfig(name="ORDERS", subjects=None)) - await jetstream.add_consumer("ORDERS", ConsumerConfig(name=None, durable_name="ORDERS_WORKER")) -""" - with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - project = root / "project" - project.mkdir() - (project / "app.py").write_text(source, encoding="utf-8") - profile = extract_profile(project, "nats-explicit-none", "example/nats-explicit-none", "test", stage_nats_sdk_catalog(root / "catalog")) - - assert profile["conditions"] == [{"kind": "nats", "interface": {"type": "service", "operations": [ - {"resource": "connection", "action": "connect"}, - {"resource": "stream", "action": "create", "name": "ORDERS"}, - {"resource": "consumer", "action": "create", "stream": "ORDERS", "name": "ORDERS_WORKER"}, - ]}}] - - -def test_generic_typed_state_call_can_resolve_multiple_canonical_operations() -> None: - source = """import nats - -async def run() -> None: - client = await nats.connect() - await client.combined(\"events.created\") -""" - with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - package_paths = stage_nats_sdk_catalog(root / "catalog") - project = root / "project" - project.mkdir() - source_path = project / "app.py" - source_path.write_text(source, encoding="utf-8") - discovery = ProjectDiscovery().discover(project, DiscoveryOptions(package_paths=package_paths, discover_installed_sdk_mappings=True)) - original = discovery.sdk_mappings[0] - document = copy.deepcopy(original.mapping) - document["python"]["calls"].append( - { - "id": "combined", - "symbols": [{"module": "example", "class": "CombinedClient", "method": "combined"}], - "receiverState": "nats.connection", - "operations": [ - {"operationRef": "subject.publish", "operationBindings": {"subject": {"argument": {"position": 0, "keyword": "subject"}}}}, - {"operationRef": "subject.subscribe", "operationBindings": {"subject": {"argument": {"position": 0, "keyword": "subject"}}}}, - ], - } - ) - mapping = SDKMappingArtifact(original.distribution, original.distribution_version, original.name, original.index_path, original.mapping_path, original.mapping_sha256, document) - conditions, extensions = SDKPythonExtractor([mapping], discovery.sdk_extensions).extract([source_path], project) - - assert extensions == ["https://runtimeconditions.io/extensions/nats-service/0.1.0/runtimeconditions.extension.yaml"] - assert conditions == [{"kind": "nats", "interface": {"type": "service", "operations": [{"resource": "connection", "action": "connect"}, {"resource": "subject", "action": "publish", "subject": "events.created"}, {"resource": "subject", "action": "subscribe", "subject": "events.created"}]}}] - - -def test_aws_managed_transfer_composes_all_mapped_runtime_paths() -> None: - with tempfile.TemporaryDirectory() as directory: - package_paths = stage_aws_sdk_catalog(Path(directory)) - profile = extract_aws_profile("managed-transfer", package_paths) - - assert profile["conditions"] == [{"kind": "aws.s3", "interface": {"type": "bucket", "operations": [ - {"name": "PutObject"}, - {"name": "CreateMultipartUpload"}, - {"name": "UploadPart"}, - {"name": "CompleteMultipartUpload"}, - {"name": "AbortMultipartUpload"}, - ]}}] - assert_aws_profile_golden("managed-transfer", profile) - - -def test_aws_factory_fails_closed_when_an_owner_mapping_dependency_is_absent() -> None: - with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - package_paths = stage_aws_sdk_catalog(root) - (root / "site/s3transfer/runtimeconditions/index.yaml").unlink() - with pytest.raises(RuntimeConditionsError) as exc: - extract_aws_profile("direct-client", package_paths) - - assert "required SDK mapping s3transfer/s3transfer.aws.s3 was not discovered" in str(exc.value) - - -def test_sdk_condition_delegation_resolves_watch_without_a_generic_wrapper_condition() -> None: - with tempfile.TemporaryDirectory() as directory: - package_paths = stage_kubernetes_sdk_catalog(Path(directory)) - profile = extract_profile(KUBERNETES_WATCH_APP, "kubernetes-pod-watcher", "example/kubernetes-pod-watcher", "test", package_paths) - - assert profile["conditions"] == [{"kind": "kubernetes", "interface": {"type": "api", "operations": [{"verb": "watch", "apiGroup": "", "apiVersion": "v1", "resource": "pods", "scope": "namespaced"}]}}] - - -def test_dynamic_client_resolves_distinct_built_in_resource_operations() -> None: - with tempfile.TemporaryDirectory() as directory: - package_paths = stage_kubernetes_sdk_catalog(Path(directory)) - profile = extract_profile(KUBERNETES_DYNAMIC_CONFIGMAP_APP, "kubernetes-dynamic-configmaps", "example/kubernetes-dynamic-configmaps", "test", package_paths) - - assert profile["conditions"] == [{"kind": "kubernetes", "interface": {"type": "api", "operations": [ - {"verb": "create", "apiGroup": "", "apiVersion": "v1", "resource": "configmaps", "scope": "namespaced"}, - {"verb": "get", "apiGroup": "", "apiVersion": "v1", "resource": "configmaps", "scope": "namespaced"}, - {"verb": "list", "apiGroup": "", "apiVersion": "v1", "resource": "configmaps", "scope": "namespaced"}, - {"verb": "patch", "apiGroup": "", "apiVersion": "v1", "resource": "configmaps", "scope": "namespaced"}, - {"verb": "delete", "apiGroup": "", "apiVersion": "v1", "resource": "configmaps", "scope": "namespaced"}, - ]}}] - - -def test_dynamic_client_watch_is_one_state_bound_operation() -> None: - with tempfile.TemporaryDirectory() as directory: - package_paths = stage_kubernetes_sdk_catalog(Path(directory)) - profile = extract_profile(KUBERNETES_DYNAMIC_WATCH_APP, "kubernetes-dynamic-pod-watcher", "example/kubernetes-dynamic-pod-watcher", "test", package_paths) - - assert profile["conditions"] == [{"kind": "kubernetes", "interface": {"type": "api", "operations": [{"verb": "watch", "apiGroup": "", "apiVersion": "v1", "resource": "pods", "scope": "namespaced"}]}}] - - -def test_dynamic_client_unmodeled_crd_emits_no_invented_resource_condition() -> None: - with tempfile.TemporaryDirectory() as directory: - package_paths = stage_kubernetes_sdk_catalog(Path(directory)) - profile = extract_profile(KUBERNETES_DYNAMIC_CRD_APP, "kubernetes-dynamic-crd", "example/kubernetes-dynamic-crd", "test", package_paths) - - assert profile["extensions"] == [] - assert profile["conditions"] == [] - - -def test_dynamic_client_unresolved_selector_emits_nothing() -> None: - profile = extract_kubernetes_source_profile("from kubernetes import client, dynamic\n\ndef resources(api_version, kind):\n resource = dynamic.DynamicClient(client.ApiClient()).resources.get(api_version=api_version, kind=kind)\n return resource.get(namespace='default')\n") - - assert profile["extensions"] == [] - assert profile["conditions"] == [] - - -def test_dynamic_client_derives_cluster_and_all_namespace_scopes_from_resource_state() -> None: - profile = extract_kubernetes_source_profile("from kubernetes import client, dynamic\n\ndef resources():\n dynamic_client = dynamic.DynamicClient(client.ApiClient())\n nodes = dynamic_client.resources.get(api_version='v1', kind='Node')\n configmaps = dynamic_client.resources.get(api_version='v1', kind='ConfigMap')\n nodes.get(name='worker-1')\n return configmaps.get()\n") - - assert profile["conditions"][0]["interface"]["operations"] == [ - {"verb": "get", "apiGroup": "", "apiVersion": "v1", "resource": "nodes", "scope": "cluster"}, - {"verb": "list", "apiGroup": "", "apiVersion": "v1", "resource": "configmaps", "scope": "all_namespaces"}, - ] - - -def test_dynamic_client_branches_on_meaningful_arguments_without_inventing_invalid_delete() -> None: - profile = extract_kubernetes_source_profile("from kubernetes import client, dynamic\n\ndef resources():\n resource = dynamic.DynamicClient(client.ApiClient()).resources.get(api_version='v1', kind='ConfigMap')\n resource.get(name=None, namespace='default')\n resource.delete(label_selector='app=example', namespace='default')\n resource.delete(namespace='default')\n") - - assert profile["conditions"][0]["interface"]["operations"] == [ - {"verb": "list", "apiGroup": "", "apiVersion": "v1", "resource": "configmaps", "scope": "namespaced"}, - {"verb": "deletecollection", "apiGroup": "", "apiVersion": "v1", "resource": "configmaps", "scope": "namespaced"}, - ] - - -def test_sdk_mapping_extraction_is_additive_to_existing_declarative_bindings() -> None: - with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - project = root / "project" - shutil.copytree(TESTDATA / "profile-generation" / "declarative-app", project) - app = project / "src/app.py" - app.write_text(app.read_text(encoding="utf-8") + "\n\nfrom kubernetes import client\n\ndef read_config_map():\n return client.CoreV1Api().read_namespaced_config_map(name='settings', namespace='default')\n", encoding="utf-8") - sdk_paths = stage_kubernetes_sdk_catalog(root / "catalog") - profile = extract_profile(project, "mixed", "example/mixed", "test", [COMMON, ENV, *sdk_paths]) - - assert len(profile["conditions"]) == 3 - assert {condition["kind"] for condition in profile["conditions"]} == {"api", "cache", "kubernetes"} - assert profile["extensions"] == [ - "https://runtimeconditions.io/extensions/common-integrations/v1alpha1/runtimeconditions.extension.yaml", - "https://runtimeconditions.io/extensions/env-configuration/v1alpha1/runtimeconditions.extension.yaml", - "https://runtimeconditions.io/extensions/kubernetes-api/0.1.0/runtimeconditions.extension.yaml", - ] - - -def test_direct_list_call_remains_list_without_the_wrapper_transformation() -> None: - profile = extract_kubernetes_source_profile("from kubernetes import client\n\ndef pods():\n api = client.CoreV1Api()\n return api.list_namespaced_pod(namespace='payments')\n") - - assert profile["conditions"][0]["interface"]["operations"] == [{"verb": "list", "apiGroup": "", "apiVersion": "v1", "resource": "pods", "scope": "namespaced"}] - - -def test_watch_stream_preserves_a_delegated_operation_without_a_matching_conditional() -> None: - profile = extract_kubernetes_source_profile("from kubernetes import client, watch\n\ndef logs():\n api = client.CoreV1Api()\n return watch.Watch().stream(api.read_namespaced_pod_log, name='worker', namespace='payments')\n") - - assert profile["conditions"][0]["interface"]["operations"] == [{"verb": "get", "apiGroup": "", "apiVersion": "v1", "resource": "pods", "scope": "namespaced", "subresource": "log"}] - - -def test_unresolved_delegated_callable_does_not_emit_a_broad_sdk_condition() -> None: - with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - package_paths = stage_kubernetes_sdk_catalog(root / "catalog") - project = root / "project" - (project / "src").mkdir(parents=True) - (project / "src/app.py").write_text("from kubernetes import watch\n\ndef events(func):\n return watch.Watch().stream(func, namespace='default')\n", encoding="utf-8") - profile = extract_profile(project, "unresolved-watch", "example/unresolved-watch", "test", package_paths) - - assert profile["extensions"] == [] - assert profile["conditions"] == [] - - -def test_tampered_sdk_mapping_is_rejected_before_source_extraction() -> None: - with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - package_paths = stage_kubernetes_sdk_catalog(root) - mapping_path = root / "site/kubernetes/runtimeconditions/mappings/kubernetes-api.yaml" - mapping_path.write_text(mapping_path.read_text(encoding="utf-8") + "\n", encoding="utf-8") - with pytest.raises(RuntimeConditionsError) as exc: - extract_profile(KUBERNETES_CONFIGMAP_APP, "tampered", "example/tampered", "test", package_paths) - - assert "mapping file digest" in str(exc.value) - - -def test_tampered_sdk_extension_is_rejected_before_source_extraction() -> None: - with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - package_paths = stage_kubernetes_sdk_catalog(root) - extension_path = root / "extensions/kubernetes-api/0.1.0/runtimeconditions.extension.yaml" - extension = yaml.safe_load(extension_path.read_text(encoding="utf-8")) - extension["spec"]["kinds"][0]["name"] = "tampered" - extension_path.write_text(yaml.safe_dump(extension, sort_keys=False), encoding="utf-8") - with pytest.raises(RuntimeConditionsError) as exc: - extract_profile(KUBERNETES_CONFIGMAP_APP, "tampered", "example/tampered", "test", package_paths) - - assert "extension semantic digest" in str(exc.value) - - -def extract_profile( - project: Path, - name: str, - workload_uri: str, - workload_version: str, - package_paths: list[Path], -) -> dict: - return ProfileExtractor().extract( - project, - ProfileOptions( - name=name, - workload_uri=workload_uri, - workload_version=workload_version, - discovery_options=DiscoveryOptions( - package_paths=package_paths, - discover_installed_sdk_mappings=True, - ), - ), - ) - - -def extract_aws_profile(fixture: str, package_paths: list[Path]) -> dict: - return extract_profile( - AWS_APPS / fixture, - f"aws-s3-{fixture}", - f"https://github.com/runtimeconditions/sdk-authorship-discovery/tree/main/s3/python/{fixture}", - "0.1.0", - package_paths, - ) - - -def assert_aws_profile_golden(fixture: str, profile: dict) -> None: - assert normalize(ProfileYamlWriter.write(profile)) == normalize((AWS_PROFILE_RESULTS / f"{fixture}.yaml").read_text(encoding="utf-8")) - - -def stage_kubernetes_sdk_catalog(root: Path) -> list[Path]: - mapping_path = root / "site/kubernetes/runtimeconditions/mappings/kubernetes-api.yaml" - mapping_path.parent.mkdir(parents=True) - shutil.copyfile(KUBERNETES_MAPPING, mapping_path) - mapping_digest = hashlib.sha256(mapping_path.read_bytes()).hexdigest() - index = { - "apiVersion": "runtimeconditions.io/sdk-mapping/v1alpha1", - "kind": "RuntimeConditionsSDKMappingIndex", - "metadata": {"distribution": "kubernetes", "distributionVersion": "36.0.3", "language": "python"}, - "mappings": [{"name": "kubernetes.api", "service": "kubernetes-api", "path": "kubernetes/runtimeconditions/mappings/kubernetes-api.yaml", "sha256": mapping_digest}], - } - index_path = root / "site/kubernetes/runtimeconditions/index.yaml" - index_path.write_text(yaml.safe_dump(index, sort_keys=False), encoding="utf-8") - extension_path = root / "extensions/kubernetes-api/0.1.0/runtimeconditions.extension.yaml" - extension_path.parent.mkdir(parents=True) - shutil.copyfile(KUBERNETES_EXTENSION, extension_path) - dist_info = root / "site/kubernetes-36.0.3.dist-info" - dist_info.mkdir() - (dist_info / "METADATA").write_text("Metadata-Version: 2.1\nName: kubernetes\nVersion: 36.0.3\n", encoding="utf-8") - (dist_info / "RECORD").write_text("kubernetes/runtimeconditions/index.yaml,,\nkubernetes/runtimeconditions/mappings/kubernetes-api.yaml,,\n", encoding="utf-8") - return [root / "site", root / "extensions"] - - -def stage_aws_sdk_catalog(root: Path) -> list[Path]: - site = root / "site" - for distribution, source in AWS_MAPPINGS.items(): - mapping = yaml.safe_load(source.read_text(encoding="utf-8")) - version = str(mapping["metadata"]["distributionVersion"]) - mapping_path = site / distribution / "runtimeconditions/mappings/aws-s3.yaml" - mapping_path.parent.mkdir(parents=True) - shutil.copyfile(source, mapping_path) - index = { - "apiVersion": "runtimeconditions.io/sdk-mapping/v1alpha1", - "kind": "RuntimeConditionsSDKMappingIndex", - "metadata": {"distribution": distribution, "distributionVersion": version, "language": "python"}, - "mappings": [{"name": mapping["metadata"]["name"], "service": "s3", "path": f"{distribution}/runtimeconditions/mappings/aws-s3.yaml", "sha256": hashlib.sha256(mapping_path.read_bytes()).hexdigest()}], - } - index_path = site / distribution / "runtimeconditions/index.yaml" - index_path.write_text(yaml.safe_dump(index, sort_keys=False), encoding="utf-8") - dist_info = site / f"{distribution}-{version}.dist-info" - dist_info.mkdir() - (dist_info / "METADATA").write_text(f"Metadata-Version: 2.1\nName: {distribution}\nVersion: {version}\n", encoding="utf-8") - (dist_info / "RECORD").write_text(f"{distribution}/runtimeconditions/index.yaml,,\n{distribution}/runtimeconditions/mappings/aws-s3.yaml,,\n", encoding="utf-8") - extension_path = root / "extensions/aws-s3/0.1.0/runtimeconditions.extension.yaml" - extension_path.parent.mkdir(parents=True) - shutil.copyfile(AWS_EXTENSION, extension_path) - return [site, root / "extensions"] - - -def stage_nats_sdk_catalog(root: Path) -> list[Path]: - site = root / "site" - mapping = yaml.safe_load(NATS_MAPPING.read_text(encoding="utf-8")) - version = str(mapping["metadata"]["distributionVersion"]) - mapping_path = site / "nats/runtimeconditions/mappings/nats-service.yaml" - mapping_path.parent.mkdir(parents=True) - shutil.copyfile(NATS_MAPPING, mapping_path) - index = { - "apiVersion": "runtimeconditions.io/sdk-mapping/v1alpha1", - "kind": "RuntimeConditionsSDKMappingIndex", - "metadata": {"distribution": "nats-py", "distributionVersion": version, "language": "python"}, - "mappings": [{"name": mapping["metadata"]["name"], "service": "nats", "path": "nats/runtimeconditions/mappings/nats-service.yaml", "sha256": hashlib.sha256(mapping_path.read_bytes()).hexdigest()}], - } - index_path = site / "nats/runtimeconditions/index.yaml" - index_path.write_text(yaml.safe_dump(index, sort_keys=False), encoding="utf-8") - extension_path = root / "extensions/nats-service/0.1.0/runtimeconditions.extension.yaml" - extension_path.parent.mkdir(parents=True) - shutil.copyfile(NATS_EXTENSION, extension_path) - return [site, root / "extensions"] - - -def extract_kubernetes_source_profile(source: str) -> dict: - with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - package_paths = stage_kubernetes_sdk_catalog(root / "catalog") - project = root / "project" - (project / "src").mkdir(parents=True) - (project / "src/app.py").write_text(source, encoding="utf-8") - return extract_profile(project, "kubernetes-source", "example/kubernetes-source", "test", package_paths) - - -def assert_profile_invalid(profile: dict, discovery, expected: str) -> None: - diagnostics = ProfileValidator().validate(profile, discovery) - assert expected in messages(diagnostics) - - -def messages(diagnostics) -> str: - return "; ".join(diagnostic.message for diagnostic in diagnostics) - - -def normalize(value: str) -> str: - return value.replace("\r\n", "\n").strip() + "\n" diff --git a/profiler.py b/profiler.py deleted file mode 100644 index 3749b57..0000000 --- a/profiler.py +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env python3 -from runtimeconditions_profiler.cli import main - - -if __name__ == "__main__": - raise SystemExit(main()) - diff --git a/runtimeconditions_profiler/runtimeconditions.profile.v0.1.0.schema.yaml b/runtimeconditions_profiler/runtimeconditions.profile.v0.1.0.schema.yaml deleted file mode 100644 index 5a2a5fa..0000000 --- a/runtimeconditions_profiler/runtimeconditions.profile.v0.1.0.schema.yaml +++ /dev/null @@ -1,100 +0,0 @@ -$schema: https://json-schema.org/draft/2020-12/schema -$id: https://runtimeconditions.io/schemas/profile/0.1.0/runtimeconditions.profile.schema.yaml -title: Runtime Conditions Profile core schema -description: >- - Core structural rules for Runtime Conditions Profiles, version 0.1.0. - Extension-owned vocabulary and applicable extension schemas are validated - against the resolved extension closure after this schema succeeds. -x-runtimeconditions-version: '0.1.0' -type: object -additionalProperties: false -required: - - apiVersion - - kind - - metadata - - workload - - extensions - - conditions -properties: - apiVersion: - const: runtimeconditions.io/v1alpha1 - kind: - const: RuntimeConditionsProfile - metadata: - $ref: '#/$defs/metadata' - workload: - $ref: '#/$defs/workload' - extensions: - type: array - uniqueItems: true - items: - $ref: '#/$defs/extensionIdentifier' - conditions: - type: array - items: - $ref: '#/$defs/condition' -allOf: - - if: - properties: - conditions: - type: array - minItems: 1 - required: [conditions] - then: - properties: - extensions: - minItems: 1 -$defs: - nonEmptyString: - type: string - minLength: 1 - extensionIdentifier: - description: >- - An absolute extension URI. The resolver must also verify exact identity, - availability, and the transitive dependency closure. - type: string - minLength: 1 - pattern: '^[A-Za-z][A-Za-z0-9+.-]*:\S+$' - format: uri - metadata: - type: object - additionalProperties: false - required: [name] - properties: - name: - $ref: '#/$defs/nonEmptyString' - labels: - type: object - propertyNames: - $ref: '#/$defs/nonEmptyString' - additionalProperties: - type: string - workload: - type: object - additionalProperties: false - required: [uri] - properties: - uri: - $ref: '#/$defs/nonEmptyString' - version: - $ref: '#/$defs/nonEmptyString' - condition: - type: object - required: [kind, interface] - properties: - name: - $ref: '#/$defs/nonEmptyString' - optional: - type: boolean - kind: - $ref: '#/$defs/nonEmptyString' - interface: - $ref: '#/$defs/interface' - additionalProperties: true - interface: - type: object - required: [type] - properties: - type: - $ref: '#/$defs/nonEmptyString' - additionalProperties: true From 4647f9bf347f3a0334ccafd223c36f5a40a55a0b Mon Sep 17 00:00:00 2001 From: itvi-1234 Date: Tue, 17 Nov 2026 22:32:26 +0530 Subject: [PATCH 2/2] feat: match the command line and output of the other profilers Found by running the profiler end to end on the real common-integrations and env-configuration bindings and comparing with the Go, TypeScript and Java profilers. - --version [--json] prints the name, version and core profile schemas (0.4.0). The rc command runs an add-on only after reading this, so without it rc profile python could not work. - The profile word is optional: generate and verify-bindings work on their own, so rc can pass them straight through. - --workload-version is optional, as in the TypeScript and Java profilers; without it the profile has no workload.version. It was required. - The YAML indents a list under its key (" - item"), keeps non-ASCII names as written and never folds a long value, as the other profilers do. The output for the same declarations is now byte for byte the Java profiler's. - verify-bindings marks the packages only a dependency of the imports with (dependency) and a direct flag in the JSON, which also gives VerifiedBindingSet.imported_packages a use in the program, not only in a test. Tested on the real packages: one workload that declares an api, a datastore and a cache with environment configuration gives exactly the profile the Java profiler's tests review. Six new tests; the writer tests fail without the writer change. 119 tests pass. --- README.md | 28 +++++-- runtimeconditions_profiler/cli.py | 33 +++++++- .../profile/semantic.py | 4 +- runtimeconditions_profiler/yamlio.py | 13 ++- tests/test_installed_binding_discovery.py | 83 +++++++++++++++++++ 5 files changed, 145 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e711342..2f2e1af 100644 --- a/README.md +++ b/README.md @@ -76,15 +76,25 @@ where the profiler or binding packages were built: --out "$PROJECT_DIR/runtimeconditions.profile.yaml" ``` -`verify-bindings` lists the distributions and extension IDs found through -workload imports; add `--json` for machine-readable output. `profile generate` -uses the installed profiler's approved core schema and the verified extension -definitions from the binding distributions. It validates the complete profile, -including schemas from transitive dependencies, before atomically writing the -YAML file. The profile lists extensions that directly contribute declarations; -validation-only dependencies remain in the checked closure. An error exits -nonzero and leaves an existing output file untouched. Omit `--out` to print -the profile to standard output. +`verify-bindings` lists the distributions found through workload imports, with +the ones only a dependency of those marked `(dependency)`; add `--json` for +machine-readable output, where each package has a `direct` flag. `profile +generate` uses the installed profiler's approved core schema and the verified +extension definitions from the binding distributions. It validates the complete +profile, including schemas from transitive dependencies, before atomically +writing the YAML file. The profile lists extensions that directly contribute +declarations; validation-only dependencies remain in the checked closure. An +error exits nonzero and leaves an existing output file untouched. Omit `--out` +to print the profile to standard output. + +`--workload-version` is optional; without it the profile's `workload` has only +its `uri`. The `profile` word is optional too, so `runtimeconditions-python-profiler +generate ...` works, which lets the `rc` command pass its arguments straight +through. `--version --json` prints the profiler's name, version, and the core +profile schemas it emits, which `rc` checks before it runs the profiler. + +The profile is written the way the Go, TypeScript, and Java profilers write it, +so the same declarations give the same YAML in every language. ## Extension IDs and remote resolution diff --git a/runtimeconditions_profiler/cli.py b/runtimeconditions_profiler/cli.py index dc103f0..fd26ac4 100644 --- a/runtimeconditions_profiler/cli.py +++ b/runtimeconditions_profiler/cli.py @@ -5,6 +5,7 @@ import os import sys import tempfile +from importlib import metadata from pathlib import Path from .errors import RuntimeConditionsError @@ -12,11 +13,32 @@ from .project.installed import InstalledBindingDiscovery from .project.verify import InstalledBindingVerifier, VerifiedBindingSet from .profile.generated import GeneratedBindingExtractor -from .profile.semantic import GeneratedProfileValidator +from .profile.semantic import CORE_VERSION, GeneratedProfileValidator + +NAME = "runtimeconditions-python-profiler" +COMMANDS = ("generate", "verify-bindings") + + +def version() -> str: + try: + return metadata.version("runtimeconditions-profiler") + except metadata.PackageNotFoundError: + return "0.0.0+unknown" def main(argv: list[str] | None = None) -> int: - parser = argparse.ArgumentParser(prog="runtimeconditions-python-profiler") + argv = list(sys.argv[1:] if argv is None else argv) + if argv[:1] == ["--version"]: + # The rc command asks for this to see which core profile schema the add-on emits. + if "--json" in argv[1:]: + print(json.dumps({"name": NAME, "version": version(), "coreSchemas": [CORE_VERSION]}, separators=(",", ":"))) + else: + print(version()) + return 0 + if argv[:1] and argv[0] in COMMANDS: + # The profile prefix is optional, so rc can pass the subcommand straight through. + argv = ["profile", *argv] + parser = argparse.ArgumentParser(prog=NAME) subparsers = parser.add_subparsers(dest="command", required=True) profile = subparsers.add_parser("profile") @@ -25,7 +47,7 @@ def main(argv: list[str] | None = None) -> int: profile_generate.add_argument("--project", default=".") profile_generate.add_argument("--name", required=True) profile_generate.add_argument("--workload-uri", required=True) - profile_generate.add_argument("--workload-version", required=True) + profile_generate.add_argument("--workload-version") profile_generate.add_argument("--out") profile_verify = profile_commands.add_parser( "verify-bindings", @@ -82,9 +104,11 @@ def verified_installed_bindings(project: Path) -> VerifiedBindingSet: def run_profile_verify_bindings(args: argparse.Namespace) -> int: verified = verified_installed_bindings(Path(args.project)) + direct = {item.installed.distribution for item in verified.imported_packages} packages = [ { "distribution": item.installed.distribution, + "direct": item.installed.distribution in direct, "version": item.installed.version, "importPackage": item.installed.import_package, "extensionId": item.model["rootExtension"]["id"], @@ -97,7 +121,8 @@ def run_profile_verify_bindings(args: argparse.Namespace) -> int: print(json.dumps({"packages": packages}, sort_keys=True)) else: for item in packages: - print(f"verified {item['distribution']}=={item['version']} ({item['importPackage']})") + marker = "" if item["direct"] else " (dependency)" + print(f"verified {item['distribution']}=={item['version']} ({item['importPackage']}){marker}") return 0 diff --git a/runtimeconditions_profiler/profile/semantic.py b/runtimeconditions_profiler/profile/semantic.py index e4ba39b..a0f3765 100644 --- a/runtimeconditions_profiler/profile/semantic.py +++ b/runtimeconditions_profiler/profile/semantic.py @@ -106,14 +106,14 @@ def __init__(self, verified: VerifiedBindingSet) -> None: def build( self, extracted: tuple[ExtractedCondition, ...], - name: str, workload_uri: str, workload_version: str, + name: str, workload_uri: str, workload_version: str | None = None, ) -> dict[str, Any]: direct = {extension for item in extracted for extension in item.direct_extensions} profile = { "apiVersion": API_VERSION, "kind": "RuntimeConditionsProfile", "metadata": {"name": name}, - "workload": {"uri": workload_uri, "version": workload_version}, + "workload": {"uri": workload_uri, **({"version": workload_version} if workload_version else {})}, "extensions": [profile_reference(ref) for ref in sorted(direct)], "conditions": [item.condition for item in extracted], } diff --git a/runtimeconditions_profiler/yamlio.py b/runtimeconditions_profiler/yamlio.py index eb88974..d46674f 100644 --- a/runtimeconditions_profiler/yamlio.py +++ b/runtimeconditions_profiler/yamlio.py @@ -31,8 +31,19 @@ def load(path: Path) -> dict[str, Any]: return Yaml.load_text(path.read_text(encoding="utf-8")) +if yaml is not None: + + class _Dumper(yaml.SafeDumper): + def increase_indent(self, flow: bool = False, indentless: bool = False) -> None: + # Indent a list under its key, as the other profilers do: " - item", not "- item". + return super().increase_indent(flow, False) + + def dump_yaml(data: dict[str, Any]) -> str: if yaml is None: raise RuntimeConditionsError(f"PyYAML is required: {YAML_IMPORT_ERROR}") - return yaml.safe_dump(data, sort_keys=False, default_flow_style=False) + # Keep non-ASCII names as written, and never fold a long value over several lines. + return yaml.dump( + data, Dumper=_Dumper, sort_keys=False, default_flow_style=False, allow_unicode=True, width=2**31 - 1, + ) diff --git a/tests/test_installed_binding_discovery.py b/tests/test_installed_binding_discovery.py index e61d9ea..fb2004b 100644 --- a/tests/test_installed_binding_discovery.py +++ b/tests/test_installed_binding_discovery.py @@ -892,3 +892,86 @@ def fail_replace(source: Path, destination: Path) -> None: assert "replace failed" in capsys.readouterr().err assert output.read_text() == "previous result\n" assert sorted(path.name for path in tmp_path.iterdir()) == ["profile.yaml", "workload"] + + +def run_cli(args: list[str], capsys: pytest.CaptureFixture[str]) -> tuple[int, str, str]: + status = cli.main(args) + captured = capsys.readouterr() + return status, captured.out, captured.err + + +def test_version_reports_the_core_schema_the_rc_command_looks_for(capsys: pytest.CaptureFixture[str]) -> None: + status, out, _ = run_cli(["--version", "--json"], capsys) + document = json.loads(out) + assert status == 0 and out.count("\n") == 1 and " " not in out + assert document == {"name": "runtimeconditions-python-profiler", "version": cli.version(), "coreSchemas": [CORE_VERSION]} + status, out, _ = run_cli(["--version"], capsys) + assert (status, out) == (0, cli.version() + "\n") + + +def generated_profile(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, args: list[str], source: str, name: str = "workload") -> str: + workload = tmp_path / name + workload.mkdir() + (workload / "app.py").write_text(source) + monkeypatch.setattr(cli, "verified_installed_bindings", lambda project: semantic_set(base_semantic_package())) + output = tmp_path / f"{name}.yaml" + assert cli.main([*args, "--project", str(workload), "--name", "sample", "--workload-uri", "example/sample", "--out", str(output)]) == 0 + return output.read_text(encoding="utf-8") + + +SERVICE = "import example_binding as b\nb.service(b.Http())\n" + + +def test_the_profile_prefix_is_optional_so_rc_can_pass_subcommands_straight_through( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, +) -> None: + with_prefix = generated_profile(tmp_path, monkeypatch, ["profile", "generate"], SERVICE, "a") + without = generated_profile(tmp_path, monkeypatch, ["generate"], SERVICE, "b") + assert with_prefix == without and "kind: service" in with_prefix + + +def test_the_workload_version_is_optional_and_left_out_when_not_given(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + without = yaml.safe_load(generated_profile(tmp_path, monkeypatch, ["generate"], SERVICE, "a")) + assert without["workload"] == {"uri": "example/sample"} + given = yaml.safe_load(generated_profile(tmp_path, monkeypatch, ["generate", "--workload-version", "2.4.0"], SERVICE, "b")) + assert given["workload"] == {"uri": "example/sample", "version": "2.4.0"} + + +def test_the_profile_is_laid_out_like_the_other_profilers_write_it(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + text = generated_profile(tmp_path, monkeypatch, ["generate", "--workload-version", "1"], SERVICE) + assert text == ( + "apiVersion: runtimeconditions.io/v1alpha1\n" + "kind: RuntimeConditionsProfile\n" + "metadata:\n name: sample\n" + "workload:\n uri: example/sample\n version: '1'\n" + "extensions:\n - https://example.test/base:1.0.0:1.0.0\n" + "conditions:\n - kind: service\n interface:\n type: http\n" + ) + + +def test_the_yaml_writer_keeps_unicode_and_never_folds_a_long_value() -> None: + from runtimeconditions_profiler.yamlio import dump_yaml + + long_value = "https://example.test/" + "a" * 200 + text = dump_yaml({"café": "latte", "list": ["x", {"nested": [long_value]}]}) + assert text == f"café: latte\nlist:\n - x\n - nested:\n - {long_value}\n" + + +def test_verify_bindings_says_which_packages_the_workload_imports_and_which_only_its_dependencies( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], +) -> None: + base = base_semantic_package() + extra = semantic_package("https://example.test/extra:1.0.0", "extra_binding", {"dependencies": [base.model["rootExtension"]["id"]]}) + both = semantic_set(base, extra) + for package in both.packages: + package.model["metadata"] = {"semanticSha256": "0" * 64} + imported_extra_only = verify.VerifiedBindingSet(both.packages, (extra,)) + monkeypatch.setattr(cli, "verified_installed_bindings", lambda project: imported_extra_only) + status, out, _ = run_cli(["verify-bindings", "--project", str(tmp_path)], capsys) + assert status == 0 + by_name = {line.split()[1].split("==")[0]: line for line in out.splitlines()} + assert "(dependency)" not in by_name["extra_binding"] + assert by_name[base.installed.distribution].endswith("(dependency)") + status, out, _ = run_cli(["verify-bindings", "--project", str(tmp_path), "--json"], capsys) + assert {item["distribution"]: item["direct"] for item in json.loads(out)["packages"]} == { + "extra_binding": True, base.installed.distribution: False}