Coverage for app / services / system_resources / reasoning.py: 100%

18 statements  

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

1""" 

2The Reasoning built-ins — frameworks CHAIN-R writes for agents to apply. 

3 

4General methods (systems thinking, delivery lifecycles, …) are the knowledge 

5least likely to differ between users, so CHAIN-R writes them once and every 

6user may grant them to their own agents. Each repository holds one written 

7source (``WrittenSourceSpec``) of the same name: one markdown document per 

8framework in ``frameworks/<slug>/``, indexed like the user guide 

9(:mod:`.written`). Being Reasoning knowledge, an agent applies one as the 

10method for a request that calls for analysis or a decision, never cites it 

11as evidence for facts (``docs/guides/retrieval.md#retrieval-by-category``). 

12 

13**How a document is written** (pinned by ``tests/test_builtin_frameworks.py``): 

14a title, a one-paragraph summary and a ``Credit:`` line naming whose work it 

15follows; then *Use it when* (tasks phrased as a request would put them — 

16what the Reasoning lane's query planner searches with), *Not for*, *How to 

17apply it* (numbered steps), *Questions to ask*, *What it produces*, *Common 

18mistakes* and *Works well with* (other built-in frameworks, by title). In 

19CHAIN-R's own words: no copyrighted text is quoted, the originators are 

20credited, and a generic name is used where a method's name is a trademark. 

21 

22**Source descriptions say which methods are inside**: triage, the knowledge 

23tools and the relevance judge read a source's name and description, never 

24its repository's, and the judge reads the first 300 characters. A 

25repository's description fits the 200 characters agent setup's catalogue 

26shows. 

27 

28Data only. The ids are fixed: repositories ``…0601`` up, their sources 

29``…0611`` up. 

30""" 

31 

32from __future__ import annotations 

33 

34from pathlib import Path 

35from uuid import UUID 

36 

37from app.util.knowledge_categories import KnowledgeCategory 

38 

39from .definition import ( 

40 BuiltInRepository, 

41 SystemKnowledgeSpec, 

42 SystemRepositorySpec, 

43 WrittenSourceSpec, 

44) 

45 

46#: Where the frameworks live, one folder per repository. Shipped with the 

47#: code, so the frameworks an image indexes are always its release's. 

48FRAMEWORKS_DIR = Path(__file__).parent / "frameworks" 

49 

50_ID_PREFIX = "c4a1b0e7-0000-4000-8000-" 

51 

52#: Who wrote them, for every source's trust panel (a document's own 

53#: ``Credit:`` line names whose work it follows). 

54CREDITS = ( 

55 "Written by CHAIN-R in its own words; each framework credits the " 

56 "people whose work it follows." 

57) 

58 

59 

60def _framework_set( 

61 n: int, 

62 *, 

63 slug: str, 

64 name: str, 

65 description: str, 

66 holds: str, 

67 icon: str, 

68 accent: str, 

69 topics: tuple[str, ...], 

70) -> BuiltInRepository: 

71 """One Reasoning repository holding one written source of its name.""" 

72 return BuiltInRepository( 

73 repository=SystemRepositorySpec( 

74 id=UUID(f"{_ID_PREFIX}{600 + n:012d}"), 

75 name=name, 

76 description=description, 

77 icon=icon, 

78 accent=accent, 

79 category=KnowledgeCategory.reasoning, 

80 ), 

81 sources=( 

82 WrittenSourceSpec( 

83 knowledge=SystemKnowledgeSpec( 

84 id=UUID(f"{_ID_PREFIX}{610 + n:012d}"), 

85 name=name, 

86 description=holds, 

87 ), 

88 folder=FRAMEWORKS_DIR / slug, 

89 credits=CREDITS, 

90 ), 

91 ), 

92 label=f"{name} repository", 

93 source_label="framework source", 

94 topics=topics, 

95 ) 

96 

97 

98SYSTEMS_THINKING = _framework_set( 

99 1, 

100 slug="systems-thinking", 

101 name="Systems thinking", 

102 description=( 

103 "Methods for seeing how the parts of a situation shape one another " 

104 "over time: feedback loops, stocks and flows, archetypes, leverage " 

105 "points. Written by CHAIN-R." 

106 ), 

107 holds=( 

108 "Systems thinking methods: systems and their parts; stocks, flows and " 

109 "delays; feedback loops and causal loop diagrams; the iceberg model; " 

110 "leverage points; system archetypes; second-order effects; boundaries " 

111 "and perspectives (CATWOE)." 

112 ), 

113 icon="Network", 

114 accent="emerald", 

115 topics=( 

116 "system", 

117 "systems", 

118 "feedback", 

119 "loop", 

120 "cycle", 

121 "vicious", 

122 "spiral", 

123 "unintended", 

124 "consequences", 

125 "knock-on", 

126 "dynamics", 

127 "structure", 

128 "incentive", 

129 "keeps", 

130 "recurring", 

131 "backfire", 

132 ), 

133) 

134 

135DELIVERY_LIFECYCLES = _framework_set( 

136 2, 

137 slug="delivery-lifecycles", 

138 name="Delivery lifecycles", 

139 description=( 

140 "Methods for planning and running a piece of work: choosing a " 

141 "lifecycle, waterfall, the V-model, iterative and agile delivery, " 

142 "scope and estimates. Written by CHAIN-R." 

143 ), 

144 holds=( 

145 "Delivery lifecycle methods: choosing a lifecycle; waterfall; " 

146 "V-model; incremental and iterative; spiral; agile principles; " 

147 "Scrum; Kanban and flow; lean and waste; phase-gate reviews; " 

148 "continuous delivery; prioritising scope; planning and estimating." 

149 ), 

150 icon="Workflow", 

151 accent="blue", 

152 topics=( 

153 "project", 

154 "delivery", 

155 "deliver", 

156 "lifecycle", 

157 "waterfall", 

158 "agile", 

159 "scrum", 

160 "kanban", 

161 "sprint", 

162 "release", 

163 "requirements", 

164 "plan", 

165 "planning", 

166 "estimate", 

167 "roadmap", 

168 "milestone", 

169 "scope", 

170 "deadline", 

171 "testing", 

172 ), 

173) 

174 

175PROBLEM_SOLVING = _framework_set( 

176 3, 

177 slug="problem-solving-and-decisions", 

178 name="Problem solving & decisions", 

179 description=( 

180 "Methods for framing a problem, finding its cause and choosing what " 

181 "to do: issue trees, five whys, decision matrices, expected value, " 

182 "pre-mortems. Written by CHAIN-R." 

183 ), 

184 holds=( 

185 "Problem-solving and decision methods: problem framing; issue trees; " 

186 "hypotheses; five whys; fishbone; PDCA; DMAIC; weighted decision " 

187 "matrix; cost–benefit analysis; decision trees and expected value; " 

188 "reversible decisions; pre-mortem; urgent and important; first " 

189 "principles; OODA loop." 

190 ), 

191 icon="Lightbulb", 

192 accent="amber", 

193 topics=( 

194 "problem", 

195 "decide", 

196 "decision", 

197 "choose", 

198 "choice", 

199 "option", 

200 "options", 

201 "alternatives", 

202 "root", 

203 "cause", 

204 "why", 

205 "prioritise", 

206 "trade-off", 

207 "compare", 

208 "fix", 

209 "improve", 

210 "should", 

211 ), 

212) 

213 

214EVIDENCE = _framework_set( 

215 4, 

216 slug="evidence-and-critical-thinking", 

217 name="Evidence & critical thinking", 

218 description=( 

219 "Methods for weighing claims and evidence: judging a source, testing " 

220 "an argument, spotting bias, reading statistics and stating " 

221 "uncertainty. Written by CHAIN-R." 

222 ), 

223 holds=( 

224 "Methods for weighing evidence: facts, assumptions and judgements; " 

225 "evaluating a source; argument structure (Toulmin); fallacies; " 

226 "cognitive biases; base rates and Bayesian updating; correlation and " 

227 "causation; competing hypotheses; Fermi estimates; statistics; " 

228 "uncertainty." 

229 ), 

230 icon="Microscope", 

231 accent="indigo", 

232 topics=( 

233 "evidence", 

234 "source", 

235 "sources", 

236 "claim", 

237 "claims", 

238 "true", 

239 "reliable", 

240 "credible", 

241 "bias", 

242 "statistic", 

243 "statistics", 

244 "study", 

245 "probability", 

246 "likely", 

247 "argument", 

248 "verify", 

249 "fact-check", 

250 "misleading", 

251 ), 

252) 

253 

254RISK_AND_ASSURANCE = _framework_set( 

255 5, 

256 slug="risk-and-assurance", 

257 name="Risk & assurance", 

258 description=( 

259 "Methods for finding, weighing and treating risk: risk assessment, " 

260 "FMEA, bow-ties, threat modelling, controls, DPIAs, AI risk and " 

261 "incident review. Written by CHAIN-R." 

262 ), 

263 holds=( 

264 "Risk and assurance methods: the risk assessment cycle; " 

265 "likelihood–impact matrices; FMEA; bow-tie analysis; fault trees; " 

266 "defence in depth; STRIDE threat modelling; control types; " 

267 "reasoning through a DPIA; AI risk management; business impact " 

268 "analysis; blameless post-incident review." 

269 ), 

270 icon="ShieldCheck", 

271 accent="rose", 

272 topics=( 

273 "risk", 

274 "risks", 

275 "threat", 

276 "hazard", 

277 "safety", 

278 "safe", 

279 "failure", 

280 "fail", 

281 "incident", 

282 "outage", 

283 "control", 

284 "controls", 

285 "audit", 

286 "assurance", 

287 "compliance", 

288 "vulnerability", 

289 "dpia", 

290 "continuity", 

291 "resilience", 

292 ), 

293) 

294 

295STRATEGY = _framework_set( 

296 6, 

297 slug="strategy-and-environment", 

298 name="Strategy & environment", 

299 description=( 

300 "Methods for reading the environment and setting direction: PESTLE, " 

301 "SWOT, five forces, scenarios, stakeholder maps, theory of change " 

302 "and OKRs. Written by CHAIN-R." 

303 ), 

304 holds=( 

305 "Strategy and environment methods: PESTLE analysis; SWOT analysis; " 

306 "five forces; scenario planning; stakeholder mapping (power and " 

307 "interest); theory of change and logic models; value chains and " 

308 "user needs; objectives and key results." 

309 ), 

310 icon="Telescope", 

311 accent="violet", 

312 topics=( 

313 "strategy", 

314 "strategic", 

315 "market", 

316 "competitor", 

317 "competitors", 

318 "competition", 

319 "environment", 

320 "trend", 

321 "trends", 

322 "future", 

323 "scenario", 

324 "scenarios", 

325 "stakeholder", 

326 "stakeholders", 

327 "goal", 

328 "goals", 

329 "objectives", 

330 "okr", 

331 "position", 

332 ), 

333) 

334 

335#: The Reasoning built-ins, in the order pages list them. 

336REASONING: tuple[BuiltInRepository, ...] = ( 

337 SYSTEMS_THINKING, 

338 DELIVERY_LIFECYCLES, 

339 PROBLEM_SOLVING, 

340 EVIDENCE, 

341 RISK_AND_ASSURANCE, 

342 STRATEGY, 

343) 

344 

345 

346__all__ = [ 

347 "CREDITS", 

348 "DELIVERY_LIFECYCLES", 

349 "EVIDENCE", 

350 "FRAMEWORKS_DIR", 

351 "PROBLEM_SOLVING", 

352 "REASONING", 

353 "RISK_AND_ASSURANCE", 

354 "STRATEGY", 

355 "SYSTEMS_THINKING", 

356]