Coverage for app / services / rules / catalogue.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-10-31 11:21 +0000

1""" 

2The rule vocabulary as a catalogue, served to the rule builder. 

3 

4fe-core used to hard-code the field, operator and action lists, which meant 

5every widening of the grammar was a frontend release and a chance for the two 

6to drift. The builder now reads this, so a new condition field is visible in 

7the UI as soon as the class exists. 

8""" 

9 

10from __future__ import annotations 

11 

12from typing import Any 

13 

14from app.db.postgres.enums import RuleEnum 

15from app.services.rules import actions as action_registry 

16from app.services.rules import fields as field_registry 

17from app.services.rules import layers as layer_registry 

18from app.services.rules import operators as operator_registry 

19from app.services.rules import pii 

20from app.services.rules import scopes as scope_registry 

21 

22 

23def layer_spec(layer: RuleEnum.Layer) -> dict[str, Any]: 

24 """Everything the builder needs to compose a rule in one layer: what the 

25 layer is, the fields its conditions can see and the actions it offers.""" 

26 registered = layer_registry.get(layer) 

27 return { 

28 **registered.spec(), 

29 "fields": field_registry.catalogue(registered.phase), 

30 "actions": action_registry.catalogue(layer), 

31 } 

32 

33 

34def build() -> dict[str, Any]: 

35 """The full catalogue: operators once, then each layer's fields/actions. 

36 

37 Operators are listed once rather than per layer because they are 

38 genuinely shared — which fields they may be paired with is already 

39 recorded on each field's ``operators`` list. 

40 """ 

41 return { 

42 "operators": operator_registry.catalogue(), 

43 # Full specs rather than bare keys. The builder's scope picker needs 

44 # to know what each scope governs, whether it can be pinned to one 

45 # resource, and how to label "all of them" — and a scope added to the 

46 # registry should reach the UI the same way a field or action does, 

47 # without a matching frontend release. 

48 "scopes": scope_registry.catalogue(), 

49 # In flow order, which is the order the Rules page lays them out in. 

50 "layers": [layer_spec(layer) for layer in RuleEnum.Layer], 

51 # The kinds of personal data the PII detectors know — what a field or 

52 # parameter with ``vocabulary: "pii_kinds"`` holds — with the label, 

53 # example and group the builder shows for each, so the picker is the 

54 # detectors' registry rather than a list kept beside it. 

55 "pii_kinds": pii.catalogue(), 

56 # What the quota actions' choices are — their period, whose usage, 

57 # what happens when reached, which kinds of work — with the label 

58 # the builder shows for each. 

59 **action_registry.quota_vocabularies(), 

60 } 

61 

62 

63def generation_vocabulary( 

64 layers: tuple[RuleEnum.Layer, ...] | None = None, 

65) -> dict[str, Any]: 

66 """The catalogue svc-ai writes generated rules against — these registries. 

67 

68 The same structure :func:`build` serves the rule builder, trimmed to what a 

69 generator needs, and narrowed to ``layers`` when given. Every flow that 

70 asks a model for rules sends this rather than letting svc-ai hold a copy: 

71 a copy is how agent setup kept offering the ``log`` action after it was 

72 removed. svc-core re-validates whatever comes back 

73 (``app.services.rules.proposals``). 

74 """ 

75 catalogue = build() 

76 wanted = {layer.value for layer in layers} if layers else None 

77 return { 

78 "operators": [ 

79 { 

80 "key": operator["key"], 

81 "label": operator["label"], 

82 "description": operator["description"], 

83 "applies_to": operator["applies_to"], 

84 } 

85 for operator in catalogue["operators"] 

86 ], 

87 "scopes": [ 

88 { 

89 "key": scope["key"], 

90 "label": scope["label"], 

91 "description": scope["description"], 

92 "targets_resource": scope["targets_resource"], 

93 } 

94 for scope in catalogue["scopes"] 

95 ], 

96 "layers": [ 

97 { 

98 "layer": spec["layer"], 

99 "label": spec["label"], 

100 "summary": spec["summary"], 

101 "description": spec["description"], 

102 "phase": spec["phase"], 

103 "fields": [ 

104 { 

105 "key": f["key"], 

106 "label": f["label"], 

107 "description": f["description"], 

108 "value_type": f["value_type"], 

109 "operators": f["operators"], 

110 "options": f["options"], 

111 "vocabulary": f["vocabulary"], 

112 } 

113 for f in spec["fields"] 

114 ], 

115 "actions": [ 

116 { 

117 "type": a["type"], 

118 "label": a["label"], 

119 "description": a["description"], 

120 "params": [ 

121 { 

122 "key": p["key"], 

123 "label": p["label"], 

124 "value_type": p["value_type"], 

125 "required": p["required"], 

126 "description": p["description"], 

127 "options": p["options"], 

128 "vocabulary": p["vocabulary"], 

129 } 

130 for p in a["params"] 

131 ], 

132 } 

133 for a in spec["actions"] 

134 ], 

135 } 

136 for spec in catalogue["layers"] 

137 if wanted is None or spec["layer"] in wanted 

138 ], 

139 # What each PII kind is, so a generator choosing ``kinds`` for "redact 

140 # NHS numbers" names ``nhs_number`` rather than guessing at a key. 

141 "pii_kinds": [ 

142 {"key": kind["key"], "description": kind["description"]} 

143 for kind in catalogue["pii_kinds"] 

144 ], 

145 }