# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-reconcile-taint-labels-across-a-fork-when-the-child-inherits-capability-scopes-but-not-the-parent-s-egress-budget.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-reconcile-taint-labels-across-a-fork-when-the-child-inherits-capability-scopes-but-not-the-parent-s-egress-budget.warrant.json
# Markdown SHA-256:   a1c1f8c79595b28bdd5ab081d92fb46361539643cdc987521cd0b69f05cfc6eb
# Code AST SHA-256:   bf9e56712f1d10bc8fecb31b45b69fa0989505150cc4751769892b445da136bd
# Verified Classes:   TaintForkInvariantError
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class TaintForkInvariantError(RuntimeError):
    """Raised when a forked child's taint/budget snapshot does not satisfy
    child_authority ⊆ parent_authority and child_obligations ⊇ parent_obligations."""


def snapshot_child(parent_record: dict, child_pid: int, observed_at_ns: int,
                   fork_at_ns: int, max_window_ns: int = 1_000_000) -> dict:
    """Atomically derive the child's task record from the parent's at fork time.

    Must be called from the clone-stop handler before the child runs.
    """
    if parent_record is None:
        raise TaintForkInvariantError("no parent record; taint lineage lost")

    # The window check: if we are past the allowed handshake latency, the child
    # may already have executed syscalls under stale authority.
    if observed_at_ns - fork_at_ns > max_window_ns:
        raise TaintForkInvariantError(
            f"fork snapshot late by {observed_at_ns - fork_at_ns} ns "
            f"(max {max_window_ns}); child may have egressed under stale taint"
        )

    parent_taint = frozenset(parent_record["taint"])
    parent_remaining = parent_record["egress_budget_remaining"]

    # Budget must be conserved across the lineage: grant the child from the
    # parent's remaining, never from the allowlist default.
    grant = min(parent_record.get("child_grant_bytes", 0), parent_remaining)
    if grant < 0:
        raise TaintForkInvariantError("negative budget grant")

    child = {
        "pid": child_pid,
        "taint": parent_taint,                 # obligations: superset (inherited)
        "egress_allowlist": parent_record["egress_allowlist"],  # authority: subset
        "egress_budget_remaining": grant,
        "parent_pid": parent_record["pid"],
    }

    # Post-condition: authority subset, obligations superset, budget conserved.
    if not child["taint"] >= parent_taint:
        raise TaintForkInvariantError("child taint is not a superset of parent taint")
    if not set(child["egress_allowlist"]) <= set(parent_record["egress_allowlist"]):
        raise TaintForkInvariantError("child authority exceeds parent authority")
    if child["egress_budget_remaining"] + (parent_remaining - grant) != parent_remaining:
        raise TaintForkInvariantError("lineage budget not conserved across fork")

    parent_record["egress_budget_remaining"] = parent_remaining - grant
    return child
