# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-reconcile-a-cross-domain-capability-warrant-whose-subject-identifier-resolves-to-two-distinct-agents-under-split-horizon-federa.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-reconcile-a-cross-domain-capability-warrant-whose-subject-identifier-resolves-to-two-distinct-agents-under-split-horizon-federa.warrant.json
# Markdown SHA-256:   ca49fbfef05f0bc5d6fa2e206e2f31a27d357a643480e3ea1b26d698f8cde504
# Code AST SHA-256:   4f9a0ca03320febb5294c1c07dd92332619a753275e8eb47e410336474f60bcc
# Verified Classes:   SubjectCollision
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class SubjectCollision(RuntimeError):
    """Raised when a foreign warrant subject resolves to two distinct key-bound principals."""
    pass


def resolve_warrant_subject(warrant, foreign_dir, local_dir):
    subject = warrant["subject"]
    want_key = warrant["subject_key_thumbprint"]

    foreign = foreign_dir.lookup(subject)
    local = local_dir.lookup(subject)

    if foreign is None:
        raise SubjectCollision(
            f"issuer directory has no entry for {subject!r}; cannot attest subject_key"
        )

    if foreign["key"] != want_key:
        # Ordinary mismatch: warrant does not match what the issuer attests.
        raise SubjectCollision(
            f"warrant key {want_key} != issuer-attested key {foreign['key']} for {subject!r}"
        )

    if local is not None and local["key"] != want_key:
        # Split-horizon collision: same name, two keys, both attested.
        # Refuse; do NOT pick a winner by lookup order.
        raise SubjectCollision(
            f"subject {subject!r} collides: issuer key {want_key} "
            f"vs local key {local['key']} (local_id={local['local_id']}); "
            f"quarantine local row and require issuer re-attestation"
        )

    # Join on thumbprint only; local row is a read-only cache projection.
    return {"local_id": foreign["local_id"], "key": want_key,
            "resolved_by_thumbprint": True}
