Coverage for src/governed_harness/runtime/patches.py: 88%

58 statements  

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

1from __future__ import annotations 

2 

3import os 

4import tempfile 

5from pathlib import Path 

6 

7from governed_harness.capabilities.authorizer import ( 

8 CapabilityAuthorizer, 

9 CapabilityDenied, 

10 contained_path, 

11) 

12from governed_harness.domain.models import Actor, CapabilityGrant, FilePatch 

13from governed_harness.evidence.hashing import sha256_file 

14 

15 

16class PatchApplier: 

17 def __init__(self, workspace: Path, authorizer: CapabilityAuthorizer | None = None) -> None: 

18 self.workspace = workspace.resolve(strict=True) 

19 self.authorizer = authorizer or CapabilityAuthorizer() 

20 

21 def apply(self, patch: FilePatch, *, actor: Actor, grants: list[CapabilityGrant]) -> Path: 

22 self.authorizer.authorize( 

23 actor=actor, 

24 capability="filesystem.write", 

25 resource=patch.path, 

26 grants=grants, 

27 ) 

28 path = contained_path(self.workspace, Path(patch.path)) 

29 self._check_precondition(patch, path) 

30 if patch.operation == "delete": 

31 self._delete(path, grants) 

32 return path 

33 if patch.operation == "create" and path.exists(): 

34 existing = path.read_text(encoding="utf-8") 

35 if existing == (patch.content or ""): 35 ↛ 37line 35 didn't jump to line 37 because the condition on line 35 was always true

36 return path 

37 raise FileExistsError(path) 

38 if patch.operation == "replace" and not path.exists(): 38 ↛ 39line 38 didn't jump to line 39 because the condition on line 38 was never true

39 raise FileNotFoundError(path) 

40 if patch.operation == "append": 

41 self._append(path, patch.content or "") 

42 return path 

43 path.parent.mkdir(parents=True, exist_ok=True) 

44 self._atomic_write(path, (patch.content or "").encode("utf-8")) 

45 return path 

46 

47 @staticmethod 

48 def _check_precondition(patch: FilePatch, path: Path) -> None: 

49 if patch.expected_sha256 is not None and ( 49 ↛ 52line 49 didn't jump to line 52 because the condition on line 49 was never true

50 not path.exists() or sha256_file(path) != patch.expected_sha256 

51 ): 

52 raise CapabilityDenied(f"precondition digest mismatch for {patch.path}") 

53 

54 @staticmethod 

55 def _delete(path: Path, grants: list[CapabilityGrant]) -> None: 

56 if not any( 56 ↛ 62line 56 didn't jump to line 62 because the condition on line 56 was always true

57 grant.conditions.get("allowDelete") 

58 for grant in grants 

59 if grant.capability == "filesystem.write" 

60 ): 

61 raise CapabilityDenied("delete requires an explicit allowDelete grant condition") 

62 path.unlink(missing_ok=False) 

63 

64 @staticmethod 

65 def _append(path: Path, content: str) -> None: 

66 path.parent.mkdir(parents=True, exist_ok=True) 

67 with path.open("a", encoding="utf-8", newline="") as handle: 

68 handle.write(content) 

69 handle.flush() 

70 os.fsync(handle.fileno()) 

71 

72 @staticmethod 

73 def _atomic_write(path: Path, data: bytes) -> None: 

74 descriptor, temporary_name = tempfile.mkstemp(prefix=f".{path.name}.", dir=path.parent) 

75 temporary = Path(temporary_name) 

76 try: 

77 with os.fdopen(descriptor, "wb") as handle: 

78 handle.write(data) 

79 handle.flush() 

80 os.fsync(handle.fileno()) 

81 os.replace(temporary, path) 

82 finally: 

83 temporary.unlink(missing_ok=True)