Coverage for src/governed_harness/review/providers.py: 65%

58 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-12 16:06 +0000

1"""The providers of the reviewers and their isolation (#57, item 8). 

2 

3A reviewer runs on a configured provider (``agentProviders``) or the deterministic ``simulated`` 

4one. Under ``runtime.agentSandbox: enforce`` its process runs in the agent sandbox with the whole 

5workspace read-only (it is protected after the write paths are allowed), so a reviewer cannot 

6change what it reviews; an unavailable sandbox is an answer that never came (``UNKNOWN``), not an 

7unconfined call. Only the environment the provider declares reaches it. 

8 

9The MCP servers a reviewer may use are read from the repository's ``.mcp.json`` and kept only 

10when ``review.panel.mcpServers`` allows them.""" 

11 

12from __future__ import annotations 

13 

14import json 

15from dataclasses import dataclass 

16from pathlib import Path 

17from typing import Any 

18 

19from governed_harness.agents import CommandAgentProvider, SimulatedAgentProvider 

20from governed_harness.agents.command import CommandAgentConfiguration 

21from governed_harness.agents.environment import provider_environment 

22from governed_harness.agents.native import native_provider 

23from governed_harness.configuration.models import ResolvedConfiguration 

24from governed_harness.domain.enums import ActorType 

25from governed_harness.domain.models import Actor 

26from governed_harness.evidence.hashing import sha256_json 

27from governed_harness.review.invoke import BuiltProvider 

28from governed_harness.runtime.sandbox import ( 

29 SandboxHost, 

30 SandboxPlan, 

31 SandboxUnavailable, 

32 build_sandbox, 

33) 

34 

35MCP_FILE = ".mcp.json" 

36 

37 

38@dataclass(frozen=True) 

39class ReviewerProvider: 

40 built: BuiltProvider 

41 sandbox: SandboxPlan | None 

42 

43 

44def mcp_servers(workspace: Path, allowlist: tuple[str, ...] | None) -> dict[str, Any]: 

45 """The definitions of the allowed MCP servers (``.mcp.json`` of the repository).""" 

46 if not allowlist: 46 ↛ 48line 46 didn't jump to line 48 because the condition on line 46 was always true

47 return {} 

48 path = workspace / MCP_FILE 

49 try: 

50 data = json.loads(path.read_text(encoding="utf-8")) 

51 except (OSError, ValueError): 

52 return {} 

53 servers = data.get("mcpServers") if isinstance(data, dict) else None 

54 if not isinstance(servers, dict): 

55 return {} 

56 return {name: servers[name] for name in sorted(servers) if name in set(allowlist)} 

57 

58 

59def mcp_digest(servers: dict[str, Any]) -> str: 

60 return sha256_json(servers) 

61 

62 

63def build_reviewer_provider( 

64 resolved: ResolvedConfiguration, 

65 provider_id: str, 

66 *, 

67 workspace: Path, 

68 protected: tuple[Path, ...] = (), 

69 host: SandboxHost | None = None, 

70 allow_network: bool = True, 

71) -> ReviewerProvider | str: 

72 """The provider of a reviewer, or why it cannot run.""" 

73 if provider_id == "simulated": 

74 actor = Actor(actor_type=ActorType.AGENT, actor_id="agent.simulated", version="1") 

75 return ReviewerProvider(BuiltProvider(SimulatedAgentProvider(), actor), None) 

76 project = resolved.project 

77 config = project.agent_providers.get(provider_id) 

78 if config is None: 78 ↛ 79line 78 didn't jump to line 79 because the condition on line 78 was never true

79 return f"provider {provider_id!r} is not configured in agentProviders" 

80 sandbox: SandboxPlan | None = None 

81 if project.runtime.effective_agent_sandbox == "enforce": 81 ↛ 82line 81 didn't jump to line 82 because the condition on line 81 was never true

82 try: 

83 sandbox = build_sandbox( 

84 workspace, 

85 project.runtime.sandbox_write_paths or (), 

86 host or SandboxHost.detect(), 

87 protected=(workspace, *protected), 

88 allow_network=allow_network, 

89 ) 

90 except SandboxUnavailable as error: 

91 return f"the agent sandbox is enforced but unavailable: {error}" 

92 environment = None 

93 if config.pass_env is not None or config.env is not None: 93 ↛ 94line 93 didn't jump to line 94 because the condition on line 93 was never true

94 environment = provider_environment(config) 

95 if environment.missing: 

96 return ( 

97 f"provider {provider_id!r} needs environment variable(s) " 

98 f"{', '.join(environment.missing)}" 

99 ) 

100 configuration = CommandAgentConfiguration( 

101 provider_id=provider_id, 

102 argv_prefix=config.effective_command, 

103 model=config.model, 

104 sandbox_prefix=sandbox.prefix if sandbox else (), 

105 environment=environment, 

106 extra_args=config.args or (), 

107 ) 

108 provider = ( 

109 native_provider(config.kind, configuration) 

110 if config.native 

111 else CommandAgentProvider(configuration) 

112 ) 

113 actor = Actor(actor_type=ActorType.AGENT, actor_id=f"agent.{provider_id}", version="1") 

114 return ReviewerProvider(BuiltProvider(provider, actor), sandbox) 

115 

116 

117__all__ = [ 

118 "MCP_FILE", 

119 "ReviewerProvider", 

120 "build_reviewer_provider", 

121 "mcp_digest", 

122 "mcp_servers", 

123]