#!/usr/bin/env python3
"""Match every authorized inventory row through MCP and save a private receipt.

Requires Python 3.9+. Does not collect inventory, patch software, install a schedule
or validate the final assessment. Stdio commands must be operator-configured.
"""
import argparse
from datetime import datetime, timezone, timedelta
from email.utils import parsedate_to_datetime
import hashlib
import json
import os
from pathlib import Path
from queue import Queue, Empty
import re
import subprocess
from threading import Thread
import time
import urllib.error
import urllib.parse
import urllib.request
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError

MAX_RESPONSE = 8 * 1024 * 1024
# Owner audit, 16 Nov 2026: 5 products per call (server guidance); retries and index pages are bounded per batch.
BATCH = 5
RETRIES, MAX_PAGES = 2, 10
# Owner audit, 16 Nov 2026: the server allows 60 requests a minute per address; pace requests (env values are for tests).
def env_number(name, default):
    try:
        return float(os.environ.get(name, default))
    except ValueError:
        return default


PACE, SLEEP_SCALE = env_number("MATCH_PACE", 1.05), env_number("MATCH_SLEEP_SCALE", 1.0)
RETRYABLE = {"index_busy", "file_limit", "time_limit", "response_budget", "not_checked"}  # reasons resending can fix
DOWN_BATCHES = 3


class NoRedirect(urllib.request.HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, headers, newurl):
        return None


class Client:
    def __init__(self, endpoint, command):
        self.identifier, self.session, self.waited, self.calls, self.last = 0, None, 0, 0, 0.0
        self.process = None
        self.endpoint = endpoint
        if command:
            self.process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
            self.queue = Queue()

            def reader():
                while True:
                    line = self.process.stdout.readline(MAX_RESPONSE + 1)
                    self.queue.put(line)
                    if not line or len(line) > MAX_RESPONSE:
                        return

            Thread(target=reader, daemon=True).start()
        else:
            url = urllib.parse.urlsplit(endpoint)
            if url.username or url.password or url.fragment or (url.scheme != "https" and not (url.scheme == "http" and url.hostname in ["localhost", "127.0.0.1", "::1"])):
                raise ValueError("MCP endpoint requires HTTPS (or loopback HTTP), with no credentials or fragment")
            self.opener = urllib.request.build_opener(NoRedirect)
        self.rpc("initialize", {"protocolVersion": "2025-11-25", "capabilities": {}, "clientInfo": {"name": "sovereign-inventory-batcher", "version": "1.0.0"}})
        self.rpc("notifications/initialized", {}, notification=True)

    def rpc(self, method, params, notification=False):
        self.identifier += 1
        request = {"jsonrpc": "2.0", "method": method, "params": params}
        if not notification:
            request["id"] = self.identifier
        data = json.dumps(request, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
        if not self.process:
            time.sleep(max(0.0, self.last + PACE - time.monotonic()))
            self.last = time.monotonic()
        if len(data) > 65536:
            raise ValueError("MCP request would exceed the 64 KiB interface limit")
        if self.process:
            self.process.stdin.write(data + b"\n")
            self.process.stdin.flush()
            if notification:
                return None
            end = time.monotonic() + 30
            while True:
                try:
                    line = self.queue.get(timeout=max(.01, end - time.monotonic()))
                except Empty as error:
                    raise TimeoutError("MCP response timed out") from error
                if not line or len(line) > MAX_RESPONSE:
                    raise RuntimeError("MCP response missing or oversized")
                response = json.loads(line)
                if response.get("id") == self.identifier:
                    break
                if time.monotonic() >= end:
                    raise TimeoutError("MCP response timed out")
        else:
            headers = {"Content-Type": "application/json", "Accept": "application/json, text/event-stream", "MCP-Protocol-Version": "2025-11-25"}
            if self.session:
                headers["Mcp-Session-Id"] = self.session
            with self.opener.open(urllib.request.Request(self.endpoint, data=data, headers=headers), timeout=30) as result:
                self.session = result.headers.get("Mcp-Session-Id", self.session)
                raw = result.read(MAX_RESPONSE + 1)
                if notification and not raw:
                    return None
                if len(raw) > MAX_RESPONSE:
                    raise RuntimeError("MCP response oversized")
                text = raw.decode("utf-8")
                events = [line[6:] for line in text.splitlines() if line.startswith("data: ")]
                response = json.loads(events[-1] if events else text)
        if "error" in response:
            raise RuntimeError("MCP rejected the request")
        if not notification and response.get("id") != self.identifier:
            raise RuntimeError("MCP response identifier does not match the request")
        return response.get("result")

    def tool(self, name, arguments):
        for attempt in range(3):
            try:
                self.calls += 1
                response = self.rpc("tools/call", {"name": name, "arguments": arguments})
                if response.get("isError"):
                    raise RuntimeError("MCP tool failed; assessment is incomplete")
                # Owner audit, 16 Nov 2026: disclosed() puts a disclaimer block first; the JSON is structuredContent or the last block.
                if isinstance(response.get("structuredContent"), dict):
                    return response["structuredContent"]
                return json.loads(response["content"][-1]["text"])
            except urllib.error.HTTPError as error:
                if error.code not in [429, 500, 502, 503, 504] or attempt == 2:
                    raise RuntimeError("MCP HTTP request failed with status " + str(error.code)) from error
                if error.code == 429:
                    value = error.headers.get("Retry-After", "30")
                    try:
                        delay = int(value)
                    except ValueError:
                        delay = max(0, int((parsedate_to_datetime(value) - datetime.now(timezone.utc)).total_seconds()) + 1)
                else:
                    delay = [2, 5][attempt]
            except (TimeoutError, urllib.error.URLError) as error:
                if attempt == 2:
                    raise RuntimeError("MCP request unavailable after bounded retries") from error
                delay = [2, 5][attempt]
            if delay < 0 or self.waited + delay > min(900, 120 + 60 * (self.calls // 50)):
                raise RuntimeError("MCP retry delay exceeds the assessment wait budget")
            self.waited += delay
            time.sleep(delay * SLEEP_SCALE)

    def close(self):
        if self.process:
            self.process.stdin.close()
            try:
                self.process.wait(timeout=5)
            except subprocess.TimeoutExpired:
                self.process.terminate()
                self.process.wait(timeout=5)


def product_rows(inventory):
    if not isinstance(inventory, list) or not inventory:
        raise ValueError("Inventory must be a nonempty array")
    rows = []
    for item in inventory:
        if not isinstance(item, dict):
            raise ValueError("Inventory rows must be objects")
        row = {}
        for field, maximum in [("vendor", 200), ("product", 400), ("version", 100), ("distro", 60), ("source", 200)]:
            value = item.get(field)
            if value is None and field != "product":
                continue
            if not isinstance(value, str) or not value.strip() or len(value) > maximum:
                raise ValueError("Inventory contains an invalid " + field)
            row[field] = value
        rows.append(row)
    return rows


def observed_version(product):
    if product.get("version") is not None:
        return product["version"]
    identity = product["product"]
    if identity.startswith("cpe:2.3:"):
        parts = re.split(r"(?<!\\):", identity)
        if len(parts) == 13 and parts[5] not in ["*", "-"]:
            return parts[5] + (parts[6] if parts[6] not in ["*", "-"] else "")
    if identity.startswith("pkg:"):
        clean = urllib.parse.unquote(re.split(r"[?#]", identity, maxsplit=1)[0])
        _, marker, version = clean.rpartition("@")
        if marker and version:
            return version
    return None


def issue_index(client):
    first = client.tool("list_daily", {"view": "index"})
    if first.get("version") != "sovereigngrc.daily-index/1":
        raise ValueError("Server must support the compact issue index")
    entries, offset = list(first["entries"]), first["next_offset"]
    offsets = set()
    while offset is not None:
        if not isinstance(offset, int) or offset < len(entries) or offset in offsets:
            raise ValueError("Invalid issue pagination")
        offsets.add(offset)
        page = client.tool("list_daily", {"view": "index", "offset": offset})
        if any(page[key] != first[key] for key in ["date", "checked", "status", "total"]):
            raise ValueError("Issue changed during pagination")
        entries.extend(page["entries"])
        offset = page["next_offset"]
    if len(entries) != first["total"] or len({e["id"] for e in entries}) != len(entries):
        raise ValueError("Issue index is incomplete")
    if first["status"] not in ["material", "quiet"] or (first["status"] == "quiet" and entries):
        raise ValueError("Issue status and entries disagree")
    return {key: first[key] for key in ["date", "checked", "status", "total"]} | {"entries": entries}


def check_call(client, batch, sent, offsets, build):
    """One check_stack call for the batch inputs listed in sent; local() maps a returned input_index back to the batch."""
    arguments = {"products": [batch[i] for i in sent]}
    offs = {str(k): offsets[i] for k, i in enumerate(sent) if offsets[i]}
    if offs:
        arguments["index_offsets"] = offs
    if build:
        arguments["index_build"] = build
    result = client.tool("check_stack", arguments)
    if not isinstance(result.get("matches"), list):
        raise ValueError("Invalid matching response")

    def local(hit):
        index = hit.get("input_index")
        if not isinstance(index, int) or index < 0 or index >= len(sent):
            raise ValueError("Matching response has an invalid input index")
        return sent[index]
    return result, local


def share_repeats(done, aliases):
    """A repeated row (same vendor, product, version, distro and source, trimmed) gets no hits of its own from the
    server; it takes the results and coverage of the row it repeats (duplicate_inputs), so it is never reported clean."""
    for dup, first in aliases.items():
        done["issue"] += [(dup, hit) for i, hit in list(done["issue"]) if i == first]
        done["complete"] += [(dup, hit) for i, hit in list(done["complete"]) if i == first]
        done["index"].update({(dup, key[1]): hit for key, hit in list(done["index"].items()) if key[0] == first})
        for state in ("unchecked", "partial", "truncated"):
            if first in done[state]:
                done[state].add(dup)
        if first in done["reasons"]:
            done["reasons"][dup] = done["reasons"][first]
    return done


def run_batch(client, batch, restarted, retry_index=True):
    """Match one batch, retry unchecked inputs alone, page the index. Returns None when the index build changed (restart once)."""
    count = len(batch)
    offsets, retries, build, pages = [0] * count, [0] * count, None, 0
    issue_hits, list_hits, index_hits = [], [], {}
    pending, unchecked, partial, reasons, aliases, truncated = set(range(count)), set(), set(), {}, {}, set()
    sends, first = [(False, list(range(count)))], True
    while sends:
        for retrying, sent in sends:
            pages += 0 if retrying else 1
            result, local = check_call(client, batch, sent, offsets, build)
            repeats = result.get("duplicate_inputs") or {}
            if not isinstance(repeats, dict):
                raise ValueError("Invalid duplicate_inputs")
            for repeat, original in repeats.items():
                aliases[local({"input_index": int(repeat)})] = local({"input_index": original})
            if first:
                first = False
                issue_hits = [(local(hit), hit) for hit in result["matches"]]
                if result.get("matches_truncated"):
                    # Issue matches were cut at the response budget: every input has incomplete issue results (T14).
                    truncated = set(range(count))
                complete = result.get("complete_list")
                list_hits = [(local(hit), hit) for hit in (complete.get("matches") if isinstance(complete, dict) and isinstance(complete.get("matches"), list) else [])]
            index = result.get("index")
            if isinstance(index, dict) and index.get("available") is False:
                # Owner audit, 16 Nov 2026: index present but unavailable (busy): these inputs are not checked, retried alone below.
                for i in sent:
                    unchecked.add(i)
                    reasons[i] = "index_busy" if index.get("index_busy") else "index_unavailable"
                    if reasons[i] not in RETRYABLE or not retry_index:
                        pending.discard(i)
                continue
            if not isinstance(index, dict) or not isinstance(index.get("pages"), list):
                return share_repeats({"issue": issue_hits, "complete": list_hits, "index": {}, "unchecked": set(), "partial": set(), "truncated": truncated, "reasons": {}, "indexed": False, "down": False}, aliases)
            if index.get("build_changed"):
                if not restarted:
                    return None
                partial |= pending
                pending.clear()
                break
            build = build or index.get("build")
            for hit in index.get("matches") or []:
                index_hits.setdefault((local(hit), hit["id"]), hit)
            for page in index["pages"]:
                i, nxt = local(page), page.get("next_offset")
                if page.get("not_checked"):
                    unchecked.add(i)
                    reasons[i] = page.get("reason") or "not_checked"
                    if reasons[i] not in RETRYABLE:
                        pending.discard(i)  # resending never helps
                    offsets[i] = nxt if isinstance(nxt, int) else page.get("offset", 0)
                    continue
                unchecked.discard(i)
                if nxt is None:
                    pending.discard(i)
                elif not isinstance(nxt, int) or nxt <= offsets[i]:
                    partial.add(i)
                    pending.discard(i)
                else:
                    offsets[i] = nxt
        retry = [i for i in sorted(pending & unchecked) if retries[i] < RETRIES]
        pending -= {i for i in pending & unchecked if retries[i] >= RETRIES}
        for i in retry:
            retries[i] += 1
        paging = sorted(pending - unchecked)
        if paging and pages >= MAX_PAGES:
            partial.update(paging)
            pending.difference_update(paging)
            paging = []
        sends = [(True, [i]) for i in retry] + ([(False, paging)] if paging else [])
    return share_repeats({"issue": issue_hits, "complete": list_hits, "index": index_hits, "unchecked": unchecked - partial, "partial": partial, "truncated": truncated, "reasons": reasons, "indexed": True,
                          "down": any(reasons.get(i) in ("index_busy", "index_unavailable") for i in unchecked)}, aliases)


def match_all(client, products, names, allow_historical):
    issue = issue_index(client)
    now = datetime.now(timezone.utc)
    try:
        today = now.astimezone(ZoneInfo("America/Toronto")).date().isoformat()
        date_zone = "America/Toronto"
    except ZoneInfoNotFoundError:
        today, date_zone = now.date().isoformat(), "UTC (conservative fallback; Eastern timezone unavailable)"
    checked = datetime.fromisoformat(issue["checked"].replace("Z", "+00:00"))
    if checked.tzinfo is None or checked > now + timedelta(minutes=5) or issue["date"] > today:
        raise ValueError("Feed timing is invalid or in the future")
    stale = now - checked > timedelta(hours=24)
    historical = stale or issue["date"] < today
    if historical and not allow_historical:
        raise ValueError("Historical feed requires explicit --allow-historical")
    ids = {entry["id"] for entry in issue["entries"]}
    older, matches, batches, list_matches, index_matches, not_checked, with_index = [], [], [], [], [], [], True
    offset, down, index_not_checked = 0, 0, 0
    while offset < len(products) and issue["status"] != "quiet":
        batch = []
        for row in products[offset:offset + BATCH]:
            candidate = batch + [row]
            body = {"jsonrpc": "2.0", "id": 999999, "method": "tools/call", "params": {"name": "check_stack", "arguments": {"products": candidate}}}
            if len(json.dumps(body, ensure_ascii=False).encode("utf-8")) > 64000:
                break
            batch = candidate
        if not batch:
            raise ValueError("A product cannot fit the MCP request limit")
        # Owner audit, 16 Nov 2026: after 3 batches in a row with the index down, stop retrying it for the rest of the run.
        done = run_batch(client, batch, False, down < DOWN_BATCHES)
        if done is None:
            # Owner audit, 16 Nov 2026: the index build changed while paging; restart this batch once, then stop.
            done = run_batch(client, batch, True, down < DOWN_BATCHES)
        down = down + 1 if done["down"] else 0 if down < DOWN_BATCHES else down
        for index, hit in done["issue"]:
            if hit["id"] not in ids:
                # Owner audit, 16 Nov 2026: a match from an older issue is a lead, not dropped.
                older.append({"name": names[offset + index], "id": hit["id"], "layer": "older_issue", "version_status": hit["version_status"], "hit_reason": hit["hit_reason"]})
                continue
            source_index = offset + index
            matches.append({"entry_id": hit["id"], "input_index": source_index, "name": names[source_index],
                            "version": observed_version(products[source_index]), "version_status": hit["version_status"], "hit_reason": hit["hit_reason"]})
        for index, hit in done["complete"]:
            list_matches.append({"name": names[offset + index], "id": hit["row"]["id"], "layer": "complete_list", "version_status": hit["version_status"], "hit_reason": hit["hit_reason"]})
        for (index, _), hit in done["index"].items():
            index_matches.append({"name": names[offset + index], "id": hit["id"], "layer": hit.get("layer", "cve_index"), "version_status": hit["version_status"], "hit_reason": hit["hit_reason"]})
        with_index = with_index and done["indexed"]
        # Count only index gaps; keep issue truncation and both reasons on the asset (17 Nov 2026, T14).
        index_gaps = done["unchecked"] | done["partial"]
        index_not_checked += len(index_gaps)
        for i in sorted(index_gaps | done["truncated"]):
            reason = ("not_fully_paged" if i in done["partial"] else done["reasons"].get(i, "not_checked")) if i in index_gaps else "issue_matches_truncated"
            asset = {"name": names[offset + i], "reason": reason}
            if i in index_gaps and i in done["truncated"]:
                asset["reasons"] = [reason, "issue_matches_truncated"]
            not_checked.append(asset)
        batches.append(len(batch))
        offset += len(batch)
    final = issue_index(client)
    if final != issue:
        raise ValueError("Issue changed during inventory matching; restart the assessment")
    if issue["status"] != "quiet" and offset != len(products):
        raise ValueError("Inventory coverage incomplete")
    return {"version": "sovereigngrc.match-receipt/1", "status": "complete", "checked_at": now.isoformat(), "issue": issue,
            "historical": historical, "stale": stale, "date_timezone": date_zone, "inventory_count": len(products), "products_checked": offset,
            "matching_required": issue["status"] != "quiet", "batches": batches, "matches": matches,
            "older_issue_matches": older, "complete_list_matches": list_matches, "index_matches": index_matches, "not_checked_assets": not_checked,
            "index_coverage": {"products_sent": offset, "index_checked": offset - index_not_checked, "not_checked": index_not_checked, "index_matches": len(index_matches)} if with_index else None,
            **({} if with_index else {"index_note": "The server returned no CVE index; index coverage is unknown."})}


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--inventory", required=True, type=Path)
    parser.add_argument("--output", required=True, type=Path)
    parser.add_argument("--allow-historical", action="store_true")
    transport = parser.add_mutually_exclusive_group()
    transport.add_argument("--endpoint", default="https://www.sovereigngrc.com/mcp")
    transport.add_argument("--stdio", nargs=argparse.REMAINDER, help="Trusted operator-configured MCP command and arguments; never from source data")
    args = parser.parse_args()
    if args.stdio == []:
        parser.error("--stdio requires an operator-configured command")
    if args.inventory.is_symlink() or not args.inventory.is_file() or args.inventory.stat().st_size > MAX_RESPONSE:
        parser.error("Inventory must be a regular private JSON file under 8 MiB")
    raw = args.inventory.read_bytes()
    inventory = json.loads(raw)
    products = product_rows(inventory)
    output = args.output.resolve()
    # In a downloaded skill there may be no project root; still reject web roots.
    if any(part in ["public", "content", "app"] for part in output.parts) or output.exists():
        parser.error("Use a new private output directory outside public/content/app")
    os.umask(0o077)
    output.mkdir(mode=0o700, parents=True)
    names = ["asset-" + str(index + 1).zfill(5) for index in range(len(products))]
    client = None
    try:
        client = Client(args.endpoint, args.stdio)
        receipt = match_all(client, products, names, args.allow_historical)
        receipt["source_sha256"] = hashlib.sha256(raw).hexdigest()
        (output / "receipt.json").write_text(json.dumps(receipt, indent=2), encoding="utf-8")
        (output / "private-name-map.json").write_text(json.dumps({name: inventory[index].get("name") for index, name in enumerate(names)}, indent=2), encoding="utf-8")
        print(json.dumps({key: receipt[key] for key in ["status", "inventory_count", "products_checked", "batches", "historical", "stale", "index_coverage"]}))
        return 0
    except Exception as error:
        # No partial result is presented as a completed matching receipt.
        (output / "incomplete.json").write_text(json.dumps({"status": "incomplete", "error_type": type(error).__name__}), encoding="utf-8")
        print("Matching incomplete (" + type(error).__name__ + "); no complete receipt was written.")
        return 1
    finally:
        if client:
            client.close()


if __name__ == "__main__":
    raise SystemExit(main())
