"""Reading the agreed operating spec, as every Specialist is given it. The engine writes the spec's data file from what the scope analyst recorded: what the customer stated, nested by field; what was assumed for them, as a list; and which stated fields bind. Code that checks a Deliverable against the spec reads it here, and a value counts whichever of the two it came from. """ from __future__ import annotations import json from collections.abc import Mapping from typing import Any from app.operating_model.names import OPERATING_SCOPE, SCOPE_DATA_FILE Upstream = Mapping[str, Mapping[str, Any]] def agreed(upstream: Upstream | None) -> dict[str, Any]: """The agreed spec from the Deliverables a submission builds on; {} without one.""" text = ( ((upstream or {}).get(OPERATING_SCOPE) or {}) .get("attachments", {}) .get(SCOPE_DATA_FILE, "") ) try: data = json.loads(text) if text else {} except ValueError: return {} return data if isinstance(data, dict) else {} def value(scope: Mapping[str, Any], path: str, default: Any = None) -> Any: """A field's value, stated or assumed; `default` when it is neither.""" node: Any = scope.get("stated") or {} for part in path.split("."): if not isinstance(node, Mapping) or part not in node: node = None break node = node[part] if node is not None: return node for item in scope.get("assumed") or []: if isinstance(item, Mapping) and item.get("field") == path: return item.get("value", default) return default def settled(scope: Mapping[str, Any], path: str) -> bool: return value(scope, path) is not None def records(scope: Mapping[str, Any], path: str) -> list[dict[str, Any]]: found = value(scope, path) return ( [dict(item) for item in found if isinstance(item, Mapping)] if isinstance(found, list) else [] ) def items(scope: Mapping[str, Any]) -> list[dict[str, Any]]: """What is to be operated.""" return records(scope, "solution.items") def conditions(scope: Mapping[str, Any]) -> list[dict[str, Any]]: """What must be kept true of an Item while it is run.""" return records(scope, "solution.conditions") def business_outcomes(scope: Mapping[str, Any]) -> list[dict[str, Any]]: """What the customer's business wants from the solution, in their words: what every measure of operations is ultimately for.""" return records(scope, "intent.business_outcomes") def stated_requirements(scope: Mapping[str, Any]) -> list[dict[str, Any]]: return records(scope, "intent.requirements") def binding(scope: Mapping[str, Any]) -> list[str]: """The stated fields an operating model must honour.""" return [str(path) for path in scope.get("binding") or []] def ids(entries: list[dict[str, Any]]) -> list[str]: return [str(entry["id"]) for entry in entries if entry.get("id")]