runtime.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Fixed, local-only plugin handlers and execution guard.
  2. No submitted artifact is imported or evaluated. The mapping below is the
  3. complete executable surface for the engineering fixture baseline.
  4. """
  5. from __future__ import annotations
  6. import hashlib
  7. import time
  8. from typing import Any
  9. class PluginExecutionError(ValueError):
  10. """A requested invocation cannot pass the fixed execution boundary."""
  11. _HANDLERS = {
  12. "connector": frozenset({"discover", "snapshot", "evidence"}),
  13. "parser": frozenset({"parse", "evidence"}),
  14. "quality": frozenset({"evaluate", "evidence"}),
  15. "notification": frozenset({"prepare", "evidence"}),
  16. "approval": frozenset({"evaluate", "evidence"}),
  17. "agent_mcp": frozenset({"read", "suggest", "evidence"}),
  18. }
  19. class BuiltinFixtureDispatcher:
  20. """Deterministic hash-only handler for the six fixed plugin categories."""
  21. def execute(self, manifest: dict[str, Any], *, operation: str, input_digest: str) -> dict[str, Any]:
  22. if not isinstance(manifest, dict) or manifest.get("distribution", {}).get("kind") != "builtin_fixture" or manifest.get("distribution", {}).get("fixture_id") != "ENGINEERING_EVIDENCE_ONLY":
  23. raise PluginExecutionError("execution_fixture_denied")
  24. permissions = manifest.get("permissions")
  25. if permissions != {"child_process": False, "file": False, "network": False, "secret": False}:
  26. raise PluginExecutionError("execution_permissions_denied")
  27. plugin_type = manifest.get("type")
  28. if plugin_type not in _HANDLERS or operation not in _HANDLERS[plugin_type] or operation not in manifest.get("capabilities", []):
  29. raise PluginExecutionError("execution_capability_denied")
  30. if not isinstance(input_digest, str) or len(input_digest) != 64 or set(input_digest) - set("0123456789abcdef"):
  31. raise PluginExecutionError("execution_input_digest_invalid")
  32. resource = manifest.get("resource")
  33. if not isinstance(resource, dict) or set(resource) != {"timeout_ms", "max_output_bytes", "max_concurrency", "max_retries"}:
  34. raise PluginExecutionError("execution_resource_denied")
  35. if any(isinstance(value, bool) or not isinstance(value, int) for value in resource.values()) or not 1 <= resource["timeout_ms"] <= 5000 or not 64 <= resource["max_output_bytes"] <= 65536 or not 1 <= resource["max_concurrency"] <= 1 or not 1 <= resource["max_retries"] <= 3:
  36. raise PluginExecutionError("execution_resource_denied")
  37. started = time.monotonic_ns()
  38. output_digest = hashlib.sha256(
  39. f"ENGINEERING_EVIDENCE_ONLY|{plugin_type}|{operation}|{manifest.get('manifest_digest')}|{input_digest}".encode("ascii")
  40. ).hexdigest()
  41. if len(output_digest.encode("ascii")) > resource["max_output_bytes"] or (time.monotonic_ns() - started) > resource["timeout_ms"] * 1_000_000:
  42. raise PluginExecutionError("execution_resource_exceeded")
  43. return {"output_digest": output_digest, "fixture_id": "ENGINEERING_EVIDENCE_ONLY", "provider_enabled": False}
  44. __all__ = ["BuiltinFixtureDispatcher", "PluginExecutionError"]