# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-lose-taint-provenance-at-the-sandbox-syscall-boundary-when-a-tool-re-enters-through-a-delegated-helper-process.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-lose-taint-provenance-at-the-sandbox-syscall-boundary-when-a-tool-re-enters-through-a-delegated-helper-process.warrant.json
# Markdown SHA-256:   d95b4af7cdc72d9110d63c20fb42d63d34549b724681fa6a36cf20b365331b3b
# Code AST SHA-256:   fb7d2e954e56c089fde71c9e094e58f95bb3385d384bf5be59fc35c4687b516a
# Verified Classes:   TaintHandoffError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class TaintHandoffError(RuntimeError):
    """Raised when a delegated process's taint register is not a sound
    over-approximation of the provenance of its syscall arguments."""
    pass


# Sentinel taint lattice; HIGH dominates LOW. Declassification is explicit only.
LOW, HIGH = 0, 1


def policed_syscall(child_register: int,
                    arg_provenance: int,
                    syscall: str,
                    handoff_complete: bool) -> None:
    """Evaluate a capability-restricted syscall against the live register.

    Invariant: the register must dominate the provenance of every argument,
    and the taint handoff (register + source map) must have completed before
    the child is allowed to issue a policed syscall. A snapshot taken at fork
    time is not sufficient; a reset-to-LOW child is not sufficient.
    """
    if not handoff_complete:
        raise TaintHandoffError(
            f"syscall {syscall!r}: child issued a policed syscall before the "
            f"taint handoff completed; register={child_register} "
            f"arg_provenance={arg_provenance}"
        )

    if arg_provenance > child_register:
        # The register under-approximates the data it is gating. This is the
        # laundering condition: a HIGH-taint argument reaching a LOW register
        # via a fork/exec or IPC boundary that dropped the source map.
        raise TaintHandoffError(
            f"syscall {syscall!r}: taint laundering detected; "
            f"register={child_register} < arg_provenance={arg_provenance}. "
            f"Refusing to evaluate policy against an unsound register."
        )

    # Only now is it safe to apply the capability policy.
    if child_register == HIGH and syscall in ("execve", "open_wronly", "connect", "unlink"):
        raise TaintHandoffError(
            f"syscall {syscall!r} denied: HIGH taint with no explicit "
            f"declassification on the argument path"
        )


def launch_delegated_helper(register: int, source_map: dict, argv: list) -> None:
    """Trampoline: exec a helper only after a complete taint handoff."""
    handoff_complete = _serialize_handoff(register, source_map)
    if not handoff_complete:
        raise TaintHandoffError(
            "refusing to exec helper: source map could not be re-established "
            "in the child; delegation would launder taint"
        )
    _exec_under_shim(argv)


def _serialize_handoff(register: int, source_map: dict) -> bool:
    # Real implementation writes register + fd->source map to a sealed fd
    # that the shim reads before dropping privileges. Returns False if any
    # inherited fd lacks a registered source.
    return all(fd in source_map for fd in source_map.get("__inherited__", []))


def _exec_under_shim(argv: list) -> None:
    ...
