"""The slots report: one line per contested slot, its totals and its JSON form.""" from __future__ import annotations from packs.slots import ADDRESSEE, Claim, Decision from resolve.collisions import Collision from resolve.localfile import resolution_is_hash_exact def _pinned_elsewhere(claims: list[Claim], winner: str | None) -> set[str]: """Emulators that pin content the served file does not carry.""" return { c.emulator for c in claims if c.emulator and c.local_path != winner and resolution_is_hash_exact(c.status) } def format_decision(decision: Decision) -> str: """One line naming the contested slot, the winner and the ground for it.""" conflict = decision.conflict if decision.serves_both: winner = decision.winner.local_path # A claim that only matched a path is answered by whatever file sits # there. Only one that pins content can be left wanting. pinned_elsewhere = _pinned_elsewhere(conflict.profile_claims, winner) served = sorted({ c.emulator for c in conflict.profile_claims if c.emulator } - pinned_elsewhere) others = sorted(pinned_elsewhere) line = ( f"{conflict.destination}: serve {winner} " f"({', '.join(served) or 'profile'}): the frontend only " "checks the path, so both are satisfied" ) if others: # One path holds one file: an emulator pinning another is not served. line += f"; {', '.join(others)} expected another file" return line if decision.reason == ADDRESSEE: return ( f"{conflict.destination}: serve {decision.winner.local_path}, " "the pack answers to the emulator" ) rejected = ", ".join( f"{path}, the file {', '.join(emulators) or 'the profile'} reads" for path, emulators, _status in conflict.files_read() ) return ( f"{conflict.destination}: keep {decision.winner.local_path}, the frontend " f"verifies content and would reject {rejected}" ) def _collision_json(collision: Collision) -> dict: return { "destination": collision.destination, "resolved": list(collision.resolved), "shipped": collision.shipped, "declarations": [ {"system": system, "name": entry.get("name", ""), "file": local} for system, (entry, local) in zip(collision.systems, collision.declarations) ], } def format_collision(collision: Collision) -> str: """One line naming the destination, which system declares which file, and the one the pack ships there.""" owners: dict[str, list[str]] = {} for system, (_entry, local) in zip(collision.systems, collision.declarations): if local and system not in owners.setdefault(local, []): owners[local].append(system) parts = [ f"{path} ({', '.join(owners[path])}) " + ("packed" if path == collision.shipped else "left out") for path in collision.resolved ] return f"{collision.destination}: " + ", ".join(parts) def report_json( decided: dict[str, tuple[str, list[Decision]]], collided: dict[str, list[Collision]], ) -> dict: """The --json document: every contested slot and every doubled destination.""" # --strict gates on collisions too, so a document that carries only # conflicts hands a consumer an empty answer and a non-zero exit. return { "conflicts": { platform: [ { "destination": d.conflict.destination, "ships": d.winner.local_path, "ships_evidence": d.winner.status, "reason": d.reason, "serves_both": d.serves_both, "emulators": d.conflict.emulators, "expected": [ {"file": path, "evidence": status, "emulators": emulators} for path, emulators, status in d.conflict.files_read() ], } for d in decisions ] for platform, (_mode, decisions) in decided.items() }, "collisions": { platform: [_collision_json(c) for c in platform_collisions] for platform, platform_collisions in collided.items() }, } def report_lines( decided: dict[str, tuple[str, list[Decision]]], collided: dict[str, list[Collision]], strict: bool, ) -> list[str]: """The text report: each decision with its ground, then the totals.""" lines: list[str] = [] fixable = 0 for platform, (mode, decisions) in decided.items(): lines.append(f"{platform}: {len(decisions)} contradicted [{mode}]") for decision in decisions: fixable += decision.serves_both lines.append(f" {format_decision(decision)}") total = sum(len(decisions) for _mode, decisions in decided.values()) for platform, collisions in collided.items(): lines.append(f"{platform}: {len(collisions)} destinations declared twice") lines.extend(f" {format_collision(collision)}" for collision in collisions) # The totals close the run, collisions counted: printed before that # section, they were not the last word and left it out. doubled = sum(len(c) for c in collided.values()) lines.append( f"\n{total} contested destinations. {fixable} the pack settles on " f"its own, {total - fixable} follow the platform's list. " f"{doubled} destinations declared twice." ) if not strict and (total or doubled): lines.append( "Reported, not failed: the remainder changes when the platform's " "list does, which no build can do. Use --strict to gate on them." ) return lines