Coverage for src/pullapprove/presets.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-08-25 16:05 -0500

1"""The commands a preset name stands for. 

2 

3A preset is PullApprove's own maintained invocation of a reviewer CLI, named by 

4a word instead of pasted into every config. It lives here, in the library, 

5because it is part of the config contract: `check` resolves the name, so an 

6unknown preset is a config error a person sees at commit time rather than a run 

7that fails an hour later. 

8 

9Nothing here runs anything. These strings are data — the app splices them and 

10executes them, and the execution side is what will smoke whether an invocation 

11is actually right for the CLI's current release. A preset that stops working 

12because its CLI changed is a change to this table. 

13""" 

14 

15from __future__ import annotations 

16 

17import re 

18from dataclasses import dataclass 

19 

20# What may stand in for `{base}`: a commit hash, or a ref made of the safe 

21# subset of ref characters. `{base}` lands inside shell text — a claude 

22# preset's template puts it in a double-quoted argument — and branch names may 

23# legally contain `"`, `$`, backticks and `;`, so a permissive substitution 

24# here would let whoever names a branch choose what runs in the sandbox. 

25_SAFE_BASE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._/-]*$") 

26 

27 

28def substitute_base(template: str, *, base: str) -> str: 

29 """`{base}` filled in with the change's base commit. 

30 

31 The one place that substitution happens, so what `{base}` means has a 

32 single answer rather than one per caller — including the refusal below. 

33 """ 

34 if not _SAFE_BASE.match(base): 

35 raise ValueError( 

36 f"base {base!r} is not a commit hash or plain ref name — refusing " 

37 "to splice it into a shell command" 

38 ) 

39 return template.replace("{base}", base) 

40 

41 

42@dataclass(frozen=True) 

43class Preset: 

44 """One named reviewer command, with the base filled in later.""" 

45 

46 # `{base}` is the base commit — the one fact about the change a command 

47 # can't learn from inside the checkout, where HEAD is the change itself. 

48 template: str 

49 

50 def command(self, *, base: str) -> str: 

51 """The command to run, with this base spelled in.""" 

52 return substitute_base(self.template, base=base) 

53 

54 

55PRESETS: dict[str, Preset] = { 

56 # The CLI's own review skill, pointed at the base. Their reviewer, not 

57 # ours: the skill carries its own prompt. 

58 # The range spelling matters: a bare sha reviews that commit itself; 

59 # the review wanted is HEAD against the base. 

60 # Model and effort are pinned so the record says what reviewed: an 

61 # unpinned CLI picks its own default, which can change under a release 

62 # or an account without a word in the config. The reviewer is the one 

63 # stage that can produce a finding — the later stages only kill or 

64 # weigh — so it gets the top-tier model at high effort. The Claude alias 

65 # tracks the latest in its family; Codex has no aliases, so its model is 

66 # a literal someone has to bump. 

67 # The budget cap fails the run at the CLI (exit 1), which reads as a 

68 # FAILED review — the gate holds rather than scoring a partial read. 

69 # Codex has no budget flag; its preset is bounded by the timeout alone. 

70 "claude-code-review": Preset( 

71 template=( 

72 'claude -p "/code-review {base}..HEAD"' 

73 " --model opus --effort high --max-budget-usd 20" 

74 ) 

75 ), 

76 # Codex's own review task, run headlessly. Effort has no flag of its own 

77 # on Codex; it is a config override. The sandbox bypass is the documented 

78 # mode for externally-sandboxed environments — the hosted run IS the 

79 # sandbox, and without it Codex's own sandbox fails to start inside one 

80 # and the review reads nothing. 

81 "codex-review": Preset( 

82 template=( 

83 "codex exec review --base {base}" 

84 " -m gpt-5.6-sol -c model_reasoning_effort=high" 

85 " --dangerously-bypass-approvals-and-sandbox" 

86 ) 

87 ), 

88} 

89 

90PRESET_NAMES = tuple(PRESETS) 

91 

92 

93def resolve_preset(name: str) -> Preset: 

94 """The preset `name` stands for. 

95 

96 Raises ValueError naming the menu, because the caller is always either 

97 validating a config someone just wrote or compiling one to run. 

98 """ 

99 try: 

100 return PRESETS[name] 

101 except KeyError: 

102 raise ValueError( 

103 f"unknown preset '{name}'. Available presets: {', '.join(PRESET_NAMES)}." 

104 ) from None