Coverage for app / services / rule_catalogue.py: 97%

102 statements  

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

1"""The governance-rule vocabulary svc-core sends, and how a prompt quotes it. 

2 

3A rule's condition fields, operators and actions are registries in svc-core 

4(``app.services.rules``). Every flow here that proposes rules — rule setup and 

5agent setup — receives that catalogue on the request and renders it into the 

6prompt with :func:`render_vocabulary`, rather than holding a copy: a copy is 

7how the agent generator came to keep offering the ``log`` action after svc-core 

8removed it. The proposal schemas below are plain strings for the same reason — 

9pinning the vocabulary as ``Literal`` types would freeze svc-core's registries 

10into this image — and svc-core re-validates every proposed rule against its 

11registries before anything is stored. 

12 

13The enforcement-side mirror in ``app/inference/rule_vocabulary.py`` is a 

14different thing: what svc-ai can *evaluate* at inference time. 

15""" 

16 

17from __future__ import annotations 

18 

19from pydantic import BaseModel, Field 

20 

21# --------------------------------------------------------------------------- 

22# The catalogue, as svc-core sends it 

23# --------------------------------------------------------------------------- 

24 

25 

26class CatalogOperator(BaseModel): 

27 key: str 

28 label: str = "" 

29 description: str = "" 

30 applies_to: list[str] = [] 

31 

32 

33class CatalogField(BaseModel): 

34 key: str 

35 label: str = "" 

36 description: str = "" 

37 value_type: str = "text" 

38 operators: list[str] = [] 

39 options: list[str] = [] 

40 #: The catalogue section describing ``options`` ("pii_kinds"). 

41 vocabulary: str | None = None 

42 

43 

44class CatalogActionParam(BaseModel): 

45 key: str 

46 label: str = "" 

47 value_type: str = "text" 

48 required: bool = False 

49 description: str = "" 

50 options: list[str] = [] 

51 #: The catalogue section describing ``options`` ("pii_kinds"). 

52 vocabulary: str | None = None 

53 

54 

55class CatalogPiiKind(BaseModel): 

56 """One kind of personal data svc-core's detectors know.""" 

57 

58 key: str 

59 description: str = "" 

60 

61 

62class CatalogAction(BaseModel): 

63 type: str 

64 label: str = "" 

65 description: str = "" 

66 params: list[CatalogActionParam] = [] 

67 

68 

69class CatalogLayer(BaseModel): 

70 """One layer of a request a rule can act in, with the fields and actions 

71 its rules may use — svc-core's ``app/services/rules/layers.py``.""" 

72 

73 layer: str 

74 label: str = "" 

75 #: The question its rules answer ("May it run?"). 

76 summary: str = "" 

77 #: When its rules run and what they can do. 

78 description: str = "" 

79 phase: str = "" 

80 fields: list[CatalogField] = [] 

81 actions: list[CatalogAction] = [] 

82 

83 

84class CatalogScope(BaseModel): 

85 key: str 

86 label: str = "" 

87 description: str = "" 

88 targets_resource: bool = False 

89 

90 

91class RuleVocabulary(BaseModel): 

92 """The layers (with their fields and actions) and operators a flow may 

93 write rules in — the part of the catalogue every rule-proposing flow 

94 needs.""" 

95 

96 layers: list[CatalogLayer] = [] 

97 operators: list[CatalogOperator] = [] 

98 #: What a field or parameter with ``vocabulary: "pii_kinds"`` holds. 

99 pii_kinds: list[CatalogPiiKind] = [] 

100 

101 

102# --------------------------------------------------------------------------- 

103# A proposed rule's blocks — plain strings, checked by svc-core 

104# --------------------------------------------------------------------------- 

105 

106 

107class ProposedCondition(BaseModel): 

108 field: str 

109 operator: str = "eq" 

110 value: str = "" 

111 

112 

113class ProposedActionParam(BaseModel): 

114 """One filled-in parameter on an action block. 

115 

116 A list of key/value pairs rather than a free-form object: structured 

117 output is far more reliable over a fixed schema, and svc-core coerces 

118 each value to the type its registry declares for that parameter. 

119 """ 

120 

121 key: str 

122 value: str = "" 

123 

124 

125class ProposedAction(BaseModel): 

126 type: str 

127 params: list[ProposedActionParam] = Field(default_factory=list) 

128 

129 

130# --------------------------------------------------------------------------- 

131# Rendering 

132# --------------------------------------------------------------------------- 

133 

134 

135#: The section each named vocabulary is listed under in the prompt. 

136_VOCABULARY_SECTIONS = {"pii_kinds": "PII KINDS"} 

137 

138 

139def _values(options: list[str], vocabulary_name: str | None) -> str: 

140 """How a field's or parameter's allowed values are quoted: a named 

141 vocabulary by its section (listed once, described), anything else inline.""" 

142 if not options: 

143 return "" 

144 if vocabulary_name in _VOCABULARY_SECTIONS: 

145 return f" values: any of the {_VOCABULARY_SECTIONS[vocabulary_name]} below." 

146 return f" values: {', '.join(options)}." 

147 

148 

149def render_vocabulary(vocabulary: RuleVocabulary) -> str: 

150 """The catalogue svc-core sent, as a prompt's grammar section. 

151 

152 Per layer, in the order a request meets them: what the layer is for, its 

153 condition fields (type, meaning, allowed values and operators) and its 

154 actions (meaning and parameters, ``*`` marking a required one, with what 

155 each takes). Then each named vocabulary a field 

156 or parameter draws on — the PII kinds — once, described. Empty when 

157 nothing was sent. 

158 """ 

159 operator_labels = {o.key: (o.label or o.key) for o in vocabulary.operators} 

160 blocks: list[str] = [] 

161 for layer in vocabulary.layers: 

162 heading = f"LAYER {layer.layer}" 

163 if layer.label: 

164 heading += f" — {layer.label}" 

165 if layer.summary: 

166 heading += f": {layer.summary}" 

167 lines = [heading] 

168 if layer.description: 

169 lines.append(f" {layer.description}") 

170 lines.append(" Condition fields:") 

171 for field in layer.fields or []: 

172 operators = ", ".join( 

173 f"{key} ({operator_labels.get(key, key)})" for key in field.operators 

174 ) 

175 options = _values(field.options, field.vocabulary) 

176 lines.append( 

177 f" - {field.key} [{field.value_type}] {field.description}" 

178 f"{options} operators: {operators or '(none)'}" 

179 ) 

180 if not layer.fields: 

181 lines.append(" (none)") 

182 lines.append(" Actions:") 

183 for action in layer.actions or []: 

184 params = "; ".join( 

185 f"{p.key}[{p.value_type}]{'*' if p.required else ''}" 

186 + (f" ({p.description})" if p.description else "") 

187 + _values(p.options, p.vocabulary).rstrip(".") 

188 for p in action.params 

189 ) 

190 lines.append( 

191 f" - {action.type}: {action.description}" 

192 f" parameters: {params or '(none)'}" 

193 ) 

194 if not layer.actions: 

195 lines.append(" (none)") 

196 blocks.append("\n".join(lines)) 

197 if vocabulary.pii_kinds: 

198 blocks.append( 

199 "PII KINDS (a list value names these keys, comma-separated)\n" 

200 + "\n".join(f" - {k.key}: {k.description}" for k in vocabulary.pii_kinds) 

201 ) 

202 return "\n\n".join(blocks) + "\n\n" if blocks else "" 

203 

204 

205def render_scopes(scopes: list[CatalogScope]) -> str: 

206 """The scopes a rule may be aimed at, one line each.""" 

207 if not scopes: 

208 return " (no scopes offered — use 'global')\n" 

209 lines = [] 

210 for scope in scopes: 

211 pinnable = ( 

212 "may be pinned to one resource" 

213 if scope.targets_resource 

214 else "covers everything in it" 

215 ) 

216 lines.append( 

217 f" - {scope.key}: {scope.description or scope.label} ({pinnable})" 

218 ) 

219 return "\n".join(lines) + "\n"