"""A live run's applications and asset owners, grouped in code from the CMDB, tags and containers (ADR 0021). The order is the product owner's (2026-11-05): 1. **CMDB business service.** A record matched to a CMDB CI that names a business service joins that application. 2. **Application tag.** Otherwise, the resource's own application tag (`Application`, `app`, `application_name`, `business_service`, `service`, any case). - **Tag alias:** when every CMDB-grouped member of one application carries the same application tag, that tag names the same application. A tag-only record with that tag joins it, flagged for review. 3. **Container.** Otherwise, the resource group (Azure) or project (Google Cloud), flagged for review: sharing a container is weak evidence of sharing an application. 4. **Model.** Only records with none of the above are sent to the `propose_application_groups` sub-agent. Its groups are judged here, every one is flagged for review, and a reply that fails the checks leaves those records ungrouped. No record is in two applications. An owner comes from the matched CI (`owner`, else `support_group`), else an `owner` tag; an application's owner is set only when all its members that have one agree. """ from __future__ import annotations import json import re from typing import Any, Awaitable, Callable, Dict, List, Optional, Tuple from app.graph import container APP_TAGS = ("application", "app", "application_name", "app_name", "business_service", "service") OWNER_TAGS = ("owner", "owned_by", "application_owner", "app_owner") INSTRUCTION = re.compile(r"\b(ignore|disregard)\b.{0,40}\b(instruction|prompt|rule)s?\b", re.I) MODEL_BATCH = 50 def _tag(record: Dict[str, Any], names: Tuple[str, ...]) -> Optional[Tuple[str, str]]: tags = (record.get("provider_attributes") or {}).get("tags") or (record.get("provider_attributes") or {}).get("labels") or {} if not isinstance(tags, dict): return None lowered = {str(k).lower(): (k, v) for k, v in tags.items()} for name in names: if name in lowered and isinstance(lowered[name][1], str) and lowered[name][1].strip(): return lowered[name][0], lowered[name][1].strip() return None def _evidence(record: Dict[str, Any]) -> List[str]: return [s["evidence_ref"] for s in record.get("sources") or [] if s.get("evidence_ref")] def _container_name(provider: str, record: Dict[str, Any]) -> Optional[str]: found = container(provider, record.get("scope_entry_id") or "") return found.rstrip("/").split("/")[-1] if found else None def owners(records: List[Dict[str, Any]], cis: List[Dict[str, Any]]) -> List[Dict[str, Any]]: by_record = {c["matched_canonical_id"]: c for c in cis if c.get("result") == "matched" and c.get("matched_canonical_id")} found = [] for record in records: rid = record["canonical_id"] ci = by_record.get(rid) if ci and (ci.get("owner") or ci.get("support_group")): found.append({"canonical_id": rid, "owner": ci.get("owner") or ci["support_group"], "source": "cmdb", "ci_ref": ci["ci_ref"], "evidence_ref": ci.get("evidence_ref")}) continue tag = _tag(record, OWNER_TAGS) if tag: found.append({"canonical_id": rid, "owner": tag[1], "source": "tag", "ci_ref": None, "evidence_ref": (_evidence(record) or [None])[0]}) return found def _judge(reply: str, leftovers: Dict[str, Dict[str, Any]]) -> Optional[List[Dict[str, Any]]]: """The model's groups, or None when any check fails.""" try: groups = json.loads(reply).get("groups") except (ValueError, AttributeError): return None if not isinstance(groups, list): return None seen, judged = set(), [] for group in groups: if not isinstance(group, dict): return None name, members, reason = group.get("name"), group.get("members"), group.get("reason") if not (isinstance(name, str) and name.strip() and len(name) <= 80 and not INSTRUCTION.search(name)): return None if not (isinstance(members, list) and members and all(m in leftovers and m not in seen for m in members)): return None seen.update(members) judged.append({"name": name.strip(), "members": members, "reason": str(reason or "")[:300]}) return judged def _prompt(leftovers: Dict[str, Dict[str, Any]]) -> str: rows = [{"canonical_id": r["canonical_id"], "name": r.get("hostname"), "asset_class": r.get("asset_class"), "resource_type": (r.get("provider_attributes") or {}).get("type"), "environment": r.get("environment")} for r in leftovers.values()] return json.dumps({"assets": rows}, ensure_ascii=False) async def group(payload: Dict[str, Any], ask: Optional[Callable[[str], Awaitable[str]]] = None) -> Dict[str, Any]: provider = payload["provider"] records = [r for r in payload.get("records") or [] if r.get("canonical_id")] cis = payload.get("cmdb_cis") or [] by_record = {c["matched_canonical_id"]: c for c in cis if c.get("result") == "matched" and c.get("matched_canonical_id")} apps: Dict[Tuple[str, str], Dict[str, Any]] = {} placed: Dict[str, Tuple[str, str]] = {} def join(key: Tuple[str, str], record: Dict[str, Any], basis: str, evidence: List[str], review: Optional[str]): app = apps.setdefault(key, {"name": key[1], "basis": basis, "members": [], "evidence_refs": [], "review_required": False, "review_reasons": []}) app["members"].append(record["canonical_id"]) app["evidence_refs"] += [e for e in evidence if e and e not in app["evidence_refs"]] if review: app["review_required"] = True if review not in app["review_reasons"]: app["review_reasons"].append(review) placed[record["canonical_id"]] = key rest = [] for record in records: # 1. CMDB business service ci = by_record.get(record["canonical_id"]) if ci and ci.get("business_service"): join(("cmdb", ci["business_service"]), record, "cmdb_business_service", [ci.get("evidence_ref")], None) else: rest.append(record) # Tag aliases: an application whose CMDB-grouped members all carry the same application tag. aliases: Dict[str, Tuple[str, str]] = {} for key, app in apps.items(): values = {(_tag(r, APP_TAGS) or (None, None))[1] for r in records if r["canonical_id"] in app["members"]} if len(values) == 1 and None not in values: value = values.pop() aliases[value] = key if value not in aliases else ("", "") # two applications claim it: no alias aliases = {v: k for v, k in aliases.items() if k != ("", "")} leftover = [] for record in rest: # 2. application tag, joined to a CMDB application when it is that application's alias tag = _tag(record, APP_TAGS) if tag and tag[1] in aliases: join(aliases[tag[1]], record, "cmdb_business_service", _evidence(record), f"Joined by its {tag[0]} tag '{tag[1]}', which every CMDB-grouped member of this application carries") elif tag: join(("tag", tag[1]), record, "tag", _evidence(record), None) else: leftover.append(record) unplaced = {} for record in leftover: # 3. container name = _container_name(provider, record) if name: join(("container", name), record, "container", _evidence(record), "Grouped only by sharing a resource group or project") else: unplaced[record["canonical_id"]] = record model_note = None if unplaced and ask is not None: # 4. the model, judged here ids = list(unplaced) for start in range(0, len(ids), MODEL_BATCH): batch = {i: unplaced[i] for i in ids[start:start + MODEL_BATCH]} try: judged = _judge(await ask(_prompt(batch)), batch) except Exception as exc: # a failed call leaves the batch ungrouped, declared judged, model_note = None, f"the grouping model failed: {type(exc).__name__}" if judged is None: model_note = model_note or "the grouping model's reply failed the checks, so its assets stay ungrouped" continue for proposal in judged: for member in proposal["members"]: join(("model", proposal["name"]), batch[member], "model", _evidence(batch[member]), "Proposed by the model from names and types only: " + (proposal["reason"] or "no reason given")) asset_owners = owners(records, cis) owner_of = {o["canonical_id"]: o["owner"] for o in asset_owners} out = [] for number, (key, app) in enumerate(sorted(apps.items(), key=lambda kv: (kv[1]["basis"], kv[0][1])), start=1): named = {owner_of[m] for m in app["members"] if m in owner_of} out.append({"application_id": f"APP-{number:03d}", "name": app["name"], "basis": app["basis"], "members": app["members"], "owner": named.pop() if len(named) == 1 else None, "owners_disagree": len(named) > 1, "business_service": key[1] if key[0] == "cmdb" else None, "evidence_refs": app["evidence_refs"], "review_required": app["review_required"], "review_reasons": app["review_reasons"]}) ungrouped = sorted(r["canonical_id"] for r in records if r["canonical_id"] not in placed) reviewed = sum(1 for a in out if a["review_required"]) note = (f"{len(out)} applications from {len(placed)} of {len(records)} records; {reviewed} need review; " f"{len(ungrouped)} ungrouped; {len(asset_owners)} assets with an owner.") if model_note: note += f" Note: {model_note}." return {"applications": out, "asset_owners": asset_owners, "ungrouped_assets": ungrouped, "applications_note": note}