# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-lose-syscall-level-confinement-when-an-mcp-server-s-declared-capability-set-is-attenuated-mid-call.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-lose-syscall-level-confinement-when-an-mcp-server-s-declared-capability-set-is-attenuated-mid-call.warrant.json
# Markdown SHA-256:   1f413962e9def5af83dde91146bbfbed67e76ada21af9e00e121f7c2f5739925
# Code AST SHA-256:   46e32a6252e4c676d0a936d03276b12030bdd82bd75557cdb962316fc50bad69
# Verified Classes:   CapabilityAttenuationInconsistency, SandboxedMCPServer
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class CapabilityAttenuationInconsistency(RuntimeError):
    """Raised when an attenuation is requested against a sealed syscall policy
    without a corresponding sandbox restart, leaving kernel and RPC layers divergent."""
    pass


class SandboxedMCPServer:
    def __init__(self, manifest: dict, sealed_at_spawn: bool = True):
        self.manifest = manifest
        self.sealed_at_spawn = sealed_at_spawn
        self.spawn_manifest = dict(manifest)  # what the kernel filter was compiled from
        self.results_tainted = False

    def attenuate(self, new_manifest: dict, restart: bool = False) -> None:
        if self.sealed_at_spawn and not restart:
            # The kernel filter still reflects spawn_manifest; the RPC layer would
            # now check new_manifest. These disagree. Refuse the in-place edit.
            raise CapabilityAttenuationInconsistency(
                f"cannot attenuate {self.spawn_manifest} -> {new_manifest} on a "
                f"sealed sandbox without restart; kernel filter would remain permissive "
                f"to superseded grants and server-side caches built under them"
            )
        if restart:
            self.spawn_manifest = dict(new_manifest)
            self.manifest = dict(new_manifest)
            self.results_tainted = False
        else:
            self.manifest = dict(new_manifest)
            self.results_tainted = True  # stateless-certified path only

    def call_tool(self, name: str, params: dict) -> dict:
        if self.results_tainted:
            # Results may reflect syscalls authorized under a superseded manifest.
            return {"result": self._invoke(name, params), "scope": "unbounded",
                    "provenance": "post_attenuation_no_restart"}
        return {"result": self._invoke(name, params), "scope": "bounded",
                "provenance": "spawn_manifest"}

    def _invoke(self, name: str, params: dict) -> dict:
        return {"tool": name, "params": params}


def plan_attenuation(server: SandboxedMCPServer, narrowed: dict, certified_stateless: bool) -> None:
    if not certified_stateless and server.sealed_at_spawn:
        # Only sound path: restart with the narrowed manifest.
        server.attenuate(narrowed, restart=True)
    elif certified_stateless:
        server.attenuate(narrowed, restart=False)
    else:
        raise CapabilityAttenuationInconsistency(
            "server is neither stateless-certified nor restartable; "
            "attenuation would leave kernel/RPC divergence unbounded"
        )
