ndomo 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (247) hide show
  1. package/.bun-version +1 -0
  2. package/.dockerignore +79 -0
  3. package/.editorconfig +18 -0
  4. package/.env.example +19 -0
  5. package/.github/CODEOWNERS +8 -0
  6. package/.github/ISSUE_TEMPLATE/bug_report.yml +62 -0
  7. package/.github/ISSUE_TEMPLATE/config.yml +2 -0
  8. package/.github/ISSUE_TEMPLATE/feature_request.yml +34 -0
  9. package/.github/dependabot.yml +36 -0
  10. package/.github/pull_request_template.md +24 -0
  11. package/.github/release.yml +30 -0
  12. package/.github/workflows/gitleaks.yml +28 -0
  13. package/.github/workflows/release-please.yml +27 -0
  14. package/.github/workflows/smoke.yml +29 -0
  15. package/.husky/commit-msg +1 -0
  16. package/CHANGELOG.md +114 -0
  17. package/Dockerfile +32 -0
  18. package/README.es.md +174 -0
  19. package/README.md +187 -0
  20. package/agents/chronicler.md +98 -0
  21. package/agents/ci-smith.md +136 -0
  22. package/agents/craftsman.md +341 -0
  23. package/agents/deploy-smith.md +138 -0
  24. package/agents/foreman.md +377 -0
  25. package/agents/go-smith.md +164 -0
  26. package/agents/guild.md +188 -0
  27. package/agents/inspector.md +83 -0
  28. package/agents/js-smith.md +127 -0
  29. package/agents/ops-scout.md +173 -0
  30. package/agents/painter.md +200 -0
  31. package/agents/python-smith.md +120 -0
  32. package/agents/ranger.md +307 -0
  33. package/agents/release-smith.md +165 -0
  34. package/agents/rust-smith.md +159 -0
  35. package/agents/sage.md +178 -0
  36. package/agents/scout.md +144 -0
  37. package/agents/scribe.md +156 -0
  38. package/agents/smith.md +201 -0
  39. package/agents/vue-smith.md +155 -0
  40. package/agents/warden.md +216 -0
  41. package/agents/zig-smith.md +156 -0
  42. package/bin/ndomo-analyses.ts +4 -0
  43. package/bin/ndomo-status.ts +4 -0
  44. package/biome.json +57 -0
  45. package/bun.lock +514 -0
  46. package/commitlint.config.js +3 -0
  47. package/config/ndomo.config.json +258 -0
  48. package/config/ndomo.schema.json +166 -0
  49. package/docs/agents.md +375 -0
  50. package/docs/bugs/plan-create-orphan-fk.md +131 -0
  51. package/docs/bugs/task_create_batch-order-index-collision.md +158 -0
  52. package/docs/configuration.md +276 -0
  53. package/docs/database.md +364 -0
  54. package/docs/features/feature-flexible-builder-v1.md +724 -0
  55. package/docs/features/feature-flexible-builder-v2.md +882 -0
  56. package/docs/features/feature-flexible-builder.md +974 -0
  57. package/docs/http-server.md +244 -0
  58. package/docs/installation.md +259 -0
  59. package/docs/integrations.md +129 -0
  60. package/docs/operations/anti-pattern-sub-agent-verify-2026-06-21.md +32 -0
  61. package/docs/operations/audit-v1.md +417 -0
  62. package/docs/operations/audit-v2.md +197 -0
  63. package/docs/operations/audit-v3.md +306 -0
  64. package/docs/operations/db-optimize-foundations.md +123 -0
  65. package/docs/operations/verify-gate-architecture.md +82 -0
  66. package/docs/workflows.md +448 -0
  67. package/opencode.json +5 -0
  68. package/package.json +65 -0
  69. package/release-please-config.json +11 -0
  70. package/scripts/dev-bust-cache.sh +164 -0
  71. package/scripts/install.sh +688 -0
  72. package/scripts/smoke-e2e.ts +704 -0
  73. package/scripts/smoke-hot.ts +417 -0
  74. package/scripts/smoke-http.sh +228 -0
  75. package/scripts/smoke-v4.ts +256 -0
  76. package/scripts/smoke-v5.ts +397 -0
  77. package/scripts/smoke.sh +9 -0
  78. package/scripts/uninstall.sh +224 -0
  79. package/skills/api-security-best-practices/SKILL.md +915 -0
  80. package/skills/bash-scripting/SKILL.md +201 -0
  81. package/skills/bun/SKILL.md +313 -0
  82. package/skills/cavecrew/SKILL.md +82 -0
  83. package/skills/caveman/SKILL.md +74 -0
  84. package/skills/caveman-review/README.md +33 -0
  85. package/skills/caveman-review/SKILL.md +55 -0
  86. package/skills/find-skills/SKILL.md +142 -0
  87. package/skills/frontend-design/LICENSE.txt +177 -0
  88. package/skills/frontend-design/SKILL.md +55 -0
  89. package/skills/golang-patterns/SKILL.md +674 -0
  90. package/skills/golang-security/SKILL.md +185 -0
  91. package/skills/golang-security/evals/evals.json +595 -0
  92. package/skills/golang-security/references/architecture.md +268 -0
  93. package/skills/golang-security/references/checklist.md +80 -0
  94. package/skills/golang-security/references/cookies.md +200 -0
  95. package/skills/golang-security/references/cryptography.md +424 -0
  96. package/skills/golang-security/references/filesystem.md +285 -0
  97. package/skills/golang-security/references/injection.md +315 -0
  98. package/skills/golang-security/references/logging.md +163 -0
  99. package/skills/golang-security/references/memory-safety.md +241 -0
  100. package/skills/golang-security/references/network.md +253 -0
  101. package/skills/golang-security/references/secrets.md +189 -0
  102. package/skills/golang-security/references/third-party.md +159 -0
  103. package/skills/golang-security/references/threat-modeling.md +189 -0
  104. package/skills/golang-testing/SKILL.md +720 -0
  105. package/skills/grill-me/SKILL.md +7 -0
  106. package/skills/javascript-testing-patterns/SKILL.md +537 -0
  107. package/skills/javascript-testing-patterns/references/advanced-testing-patterns.md +513 -0
  108. package/skills/modern-javascript-patterns/SKILL.md +43 -0
  109. package/skills/modern-javascript-patterns/references/advanced-patterns.md +487 -0
  110. package/skills/modern-javascript-patterns/references/details.md +457 -0
  111. package/skills/python-anti-patterns/SKILL.md +349 -0
  112. package/skills/python-design-patterns/SKILL.md +85 -0
  113. package/skills/python-design-patterns/references/details.md +353 -0
  114. package/skills/python-error-handling/SKILL.md +193 -0
  115. package/skills/python-error-handling/references/details.md +171 -0
  116. package/skills/python-testing-patterns/SKILL.md +278 -0
  117. package/skills/python-testing-patterns/references/advanced-patterns.md +411 -0
  118. package/skills/python-testing-patterns/references/details.md +349 -0
  119. package/skills/rust-patterns/SKILL.md +500 -0
  120. package/skills/rust-testing/SKILL.md +501 -0
  121. package/skills/security-review/SKILL.md +504 -0
  122. package/skills/security-review/cloud-infrastructure-security.md +361 -0
  123. package/skills/vue-best-practices/SKILL.md +154 -0
  124. package/skills/vue-best-practices/references/animation-class-based-technique.md +254 -0
  125. package/skills/vue-best-practices/references/animation-state-driven-technique.md +291 -0
  126. package/skills/vue-best-practices/references/component-async.md +97 -0
  127. package/skills/vue-best-practices/references/component-data-flow.md +307 -0
  128. package/skills/vue-best-practices/references/component-fallthrough-attrs.md +174 -0
  129. package/skills/vue-best-practices/references/component-keep-alive.md +137 -0
  130. package/skills/vue-best-practices/references/component-slots.md +216 -0
  131. package/skills/vue-best-practices/references/component-suspense.md +228 -0
  132. package/skills/vue-best-practices/references/component-teleport.md +108 -0
  133. package/skills/vue-best-practices/references/component-transition-group.md +128 -0
  134. package/skills/vue-best-practices/references/component-transition.md +125 -0
  135. package/skills/vue-best-practices/references/composables.md +290 -0
  136. package/skills/vue-best-practices/references/directives.md +162 -0
  137. package/skills/vue-best-practices/references/perf-avoid-component-abstraction-in-lists.md +159 -0
  138. package/skills/vue-best-practices/references/perf-v-once-v-memo-directives.md +182 -0
  139. package/skills/vue-best-practices/references/perf-virtualize-large-lists.md +187 -0
  140. package/skills/vue-best-practices/references/plugins.md +166 -0
  141. package/skills/vue-best-practices/references/reactivity.md +344 -0
  142. package/skills/vue-best-practices/references/render-functions.md +201 -0
  143. package/skills/vue-best-practices/references/sfc.md +310 -0
  144. package/skills/vue-best-practices/references/state-management.md +135 -0
  145. package/skills/vue-best-practices/references/updated-hook-performance.md +187 -0
  146. package/skills/vue-pinia-best-practices/SKILL.md +21 -0
  147. package/skills/vue-pinia-best-practices/reference/pinia-no-active-pinia-error.md +248 -0
  148. package/skills/vue-pinia-best-practices/reference/pinia-setup-store-return-all-state.md +227 -0
  149. package/skills/vue-pinia-best-practices/reference/pinia-store-destructuring-breaks-reactivity.md +193 -0
  150. package/skills/vue-pinia-best-practices/reference/state-url-for-ephemeral-filters.md +238 -0
  151. package/skills/vue-pinia-best-practices/reference/state-use-pinia-for-large-apps.md +262 -0
  152. package/skills/vue-pinia-best-practices/reference/store-method-binding-parentheses.md +191 -0
  153. package/skills/zig-0.16/SKILL.md +840 -0
  154. package/skills/zig-0.16/scripts/check-zig-version.sh +21 -0
  155. package/src/cli/analyses.ts +280 -0
  156. package/src/cli/index.ts +108 -0
  157. package/src/cli/serve.ts +192 -0
  158. package/src/cli/smoke.ts +131 -0
  159. package/src/cli/status.test.ts +204 -0
  160. package/src/cli/status.ts +263 -0
  161. package/src/cli/vacuum.test.ts +82 -0
  162. package/src/cli/vacuum.ts +96 -0
  163. package/src/config/schema.test.ts +88 -0
  164. package/src/config/schema.ts +64 -0
  165. package/src/db/analyses-migration.test.ts +210 -0
  166. package/src/db/analyses.test.ts +466 -0
  167. package/src/db/analyses.ts +375 -0
  168. package/src/db/auto-checkpoint.ts +131 -0
  169. package/src/db/client.test.ts +129 -0
  170. package/src/db/client.ts +55 -0
  171. package/src/db/fts-escape.ts +20 -0
  172. package/src/db/incidents.test.ts +201 -0
  173. package/src/db/incidents.ts +93 -0
  174. package/src/db/index.ts +86 -0
  175. package/src/db/migrations-v13.test.ts +141 -0
  176. package/src/db/migrations-v8.test.ts +301 -0
  177. package/src/db/migrations.ts +147 -0
  178. package/src/db/plan-archive.test.ts +180 -0
  179. package/src/db/plan-archive.ts +274 -0
  180. package/src/db/plan-create.test.ts +276 -0
  181. package/src/db/plan-create.ts +78 -0
  182. package/src/db/plan-files.test.ts +289 -0
  183. package/src/db/plan-update-status.ts +287 -0
  184. package/src/db/plans.test.ts +490 -0
  185. package/src/db/plans.ts +534 -0
  186. package/src/db/resolve-project-dir.test.ts +143 -0
  187. package/src/db/resolve-project-dir.ts +75 -0
  188. package/src/db/rollbacks.test.ts +150 -0
  189. package/src/db/rollbacks.ts +67 -0
  190. package/src/db/schema.ts +907 -0
  191. package/src/db/sessions.test.ts +80 -0
  192. package/src/db/sessions.ts +135 -0
  193. package/src/db/shutdown.test.ts +147 -0
  194. package/src/db/shutdown.ts +45 -0
  195. package/src/db/tasks.test.ts +921 -0
  196. package/src/db/tasks.ts +747 -0
  197. package/src/db/types.ts +619 -0
  198. package/src/http/__tests__/auth.test.ts +196 -0
  199. package/src/http/__tests__/routes.test.ts +465 -0
  200. package/src/http/__tests__/sse.test.ts +317 -0
  201. package/src/http/auth.ts +72 -0
  202. package/src/http/middleware/cors.ts +53 -0
  203. package/src/http/middleware/security-headers.ts +21 -0
  204. package/src/http/routes/events.ts +112 -0
  205. package/src/http/routes/health.ts +51 -0
  206. package/src/http/routes/plans.ts +66 -0
  207. package/src/http/routes/sessions.ts +50 -0
  208. package/src/http/routes/tasks.ts +60 -0
  209. package/src/http/server.ts +95 -0
  210. package/src/http/sse.ts +116 -0
  211. package/src/index.ts +37 -0
  212. package/src/lib.ts +65 -0
  213. package/src/mem/scoped.ts +65 -0
  214. package/src/orchestrator/background.test.ts +268 -0
  215. package/src/orchestrator/background.ts +293 -0
  216. package/src/orchestrator/memory-hook.ts +182 -0
  217. package/src/orchestrator/reconciler.ts +123 -0
  218. package/src/orchestrator/scheduler.test.ts +300 -0
  219. package/src/orchestrator/scheduler.ts +243 -0
  220. package/src/plugin.test.ts +2574 -0
  221. package/src/plugin.ts +1690 -0
  222. package/src/sdk/client.ts +66 -0
  223. package/src/worktrees/manager.ts +236 -0
  224. package/src/worktrees/state.ts +87 -0
  225. package/tests/integration/ranger-flow.test.ts +257 -0
  226. package/tools/analysis_archive.ts +28 -0
  227. package/tools/analysis_create.ts +55 -0
  228. package/tools/analysis_get.ts +33 -0
  229. package/tools/analysis_link_plan.ts +44 -0
  230. package/tools/analysis_list.ts +48 -0
  231. package/tools/analysis_search.ts +36 -0
  232. package/tools/analysis_update.ts +44 -0
  233. package/tools/plan_approve.ts +31 -0
  234. package/tools/plan_create.ts +58 -0
  235. package/tools/plan_get.ts +40 -0
  236. package/tools/plan_list.ts +37 -0
  237. package/tools/plan_search.ts +34 -0
  238. package/tools/plan_update_status.ts +71 -0
  239. package/tools/session_checkpoint.ts +31 -0
  240. package/tools/session_end.ts +26 -0
  241. package/tools/session_start.ts +43 -0
  242. package/tools/task_create_batch.ts +70 -0
  243. package/tools/task_list.ts +35 -0
  244. package/tools/task_next_for_agent.ts +30 -0
  245. package/tools/task_search.ts +34 -0
  246. package/tools/task_update_status.ts +37 -0
  247. package/tsconfig.json +31 -0
@@ -0,0 +1,353 @@
1
+ # python-design-patterns — detailed patterns and worked examples
2
+
3
+ ## Fundamental Patterns
4
+
5
+ ### Pattern 1: KISS - Keep It Simple
6
+
7
+ Before adding complexity, ask: does a simpler solution work?
8
+
9
+ ```python
10
+ # Over-engineered: Factory with registration
11
+ class OutputFormatterFactory:
12
+ _formatters: dict[str, type[Formatter]] = {}
13
+
14
+ @classmethod
15
+ def register(cls, name: str):
16
+ def decorator(formatter_cls):
17
+ cls._formatters[name] = formatter_cls
18
+ return formatter_cls
19
+ return decorator
20
+
21
+ @classmethod
22
+ def create(cls, name: str) -> Formatter:
23
+ return cls._formatters[name]()
24
+
25
+ @OutputFormatterFactory.register("json")
26
+ class JsonFormatter(Formatter):
27
+ ...
28
+
29
+ # Simple: Just use a dictionary
30
+ FORMATTERS = {
31
+ "json": JsonFormatter,
32
+ "csv": CsvFormatter,
33
+ "xml": XmlFormatter,
34
+ }
35
+
36
+ def get_formatter(name: str) -> Formatter:
37
+ """Get formatter by name."""
38
+ if name not in FORMATTERS:
39
+ raise ValueError(f"Unknown format: {name}")
40
+ return FORMATTERS[name]()
41
+ ```
42
+
43
+ The factory pattern adds code without adding value here. Save patterns for when they solve real problems.
44
+
45
+ ### Pattern 2: Single Responsibility Principle
46
+
47
+ Each class or function should have one reason to change.
48
+
49
+ ```python
50
+ # BAD: Handler does everything
51
+ class UserHandler:
52
+ async def create_user(self, request: Request) -> Response:
53
+ # HTTP parsing
54
+ data = await request.json()
55
+
56
+ # Validation
57
+ if not data.get("email"):
58
+ return Response({"error": "email required"}, status=400)
59
+
60
+ # Database access
61
+ user = await db.execute(
62
+ "INSERT INTO users (email, name) VALUES ($1, $2) RETURNING *",
63
+ data["email"], data["name"]
64
+ )
65
+
66
+ # Response formatting
67
+ return Response({"id": user.id, "email": user.email}, status=201)
68
+
69
+ # GOOD: Separated concerns
70
+ class UserService:
71
+ """Business logic only."""
72
+
73
+ def __init__(self, repo: UserRepository) -> None:
74
+ self._repo = repo
75
+
76
+ async def create_user(self, data: CreateUserInput) -> User:
77
+ # Only business rules here
78
+ user = User(email=data.email, name=data.name)
79
+ return await self._repo.save(user)
80
+
81
+ class UserHandler:
82
+ """HTTP concerns only."""
83
+
84
+ def __init__(self, service: UserService) -> None:
85
+ self._service = service
86
+
87
+ async def create_user(self, request: Request) -> Response:
88
+ data = CreateUserInput(**(await request.json()))
89
+ user = await self._service.create_user(data)
90
+ return Response(user.to_dict(), status=201)
91
+ ```
92
+
93
+ Now HTTP changes don't affect business logic, and vice versa.
94
+
95
+ ### Pattern 3: Separation of Concerns
96
+
97
+ Organize code into distinct layers with clear responsibilities.
98
+
99
+ ```
100
+ ┌─────────────────────────────────────────────────────┐
101
+ │ API Layer (handlers) │
102
+ │ - Parse requests │
103
+ │ - Call services │
104
+ │ - Format responses │
105
+ └─────────────────────────────────────────────────────┘
106
+
107
+
108
+ ┌─────────────────────────────────────────────────────┐
109
+ │ Service Layer (business logic) │
110
+ │ - Domain rules and validation │
111
+ │ - Orchestrate operations │
112
+ │ - Pure functions where possible │
113
+ └─────────────────────────────────────────────────────┘
114
+
115
+
116
+ ┌─────────────────────────────────────────────────────┐
117
+ │ Repository Layer (data access) │
118
+ │ - SQL queries │
119
+ │ - External API calls │
120
+ │ - Cache operations │
121
+ └─────────────────────────────────────────────────────┘
122
+ ```
123
+
124
+ Each layer depends only on layers below it:
125
+
126
+ ```python
127
+ # Repository: Data access
128
+ class UserRepository:
129
+ async def get_by_id(self, user_id: str) -> User | None:
130
+ row = await self._db.fetchrow(
131
+ "SELECT * FROM users WHERE id = $1", user_id
132
+ )
133
+ return User(**row) if row else None
134
+
135
+ # Service: Business logic
136
+ class UserService:
137
+ def __init__(self, repo: UserRepository) -> None:
138
+ self._repo = repo
139
+
140
+ async def get_user(self, user_id: str) -> User:
141
+ user = await self._repo.get_by_id(user_id)
142
+ if user is None:
143
+ raise UserNotFoundError(user_id)
144
+ return user
145
+
146
+ # Handler: HTTP concerns
147
+ @app.get("/users/{user_id}")
148
+ async def get_user(user_id: str) -> UserResponse:
149
+ user = await user_service.get_user(user_id)
150
+ return UserResponse.from_user(user)
151
+ ```
152
+
153
+ ### Pattern 4: Composition Over Inheritance
154
+
155
+ Build behavior by combining objects rather than inheriting.
156
+
157
+ ```python
158
+ # Inheritance: Rigid and hard to test
159
+ class EmailNotificationService(NotificationService):
160
+ def __init__(self):
161
+ super().__init__()
162
+ self._smtp = SmtpClient() # Hard to mock
163
+
164
+ def notify(self, user: User, message: str) -> None:
165
+ self._smtp.send(user.email, message)
166
+
167
+ # Composition: Flexible and testable
168
+ class NotificationService:
169
+ """Send notifications via multiple channels."""
170
+
171
+ def __init__(
172
+ self,
173
+ email_sender: EmailSender,
174
+ sms_sender: SmsSender | None = None,
175
+ push_sender: PushSender | None = None,
176
+ ) -> None:
177
+ self._email = email_sender
178
+ self._sms = sms_sender
179
+ self._push = push_sender
180
+
181
+ async def notify(
182
+ self,
183
+ user: User,
184
+ message: str,
185
+ channels: set[str] | None = None,
186
+ ) -> None:
187
+ channels = channels or {"email"}
188
+
189
+ if "email" in channels:
190
+ await self._email.send(user.email, message)
191
+
192
+ if "sms" in channels and self._sms and user.phone:
193
+ await self._sms.send(user.phone, message)
194
+
195
+ if "push" in channels and self._push and user.device_token:
196
+ await self._push.send(user.device_token, message)
197
+
198
+ # Easy to test with fakes
199
+ service = NotificationService(
200
+ email_sender=FakeEmailSender(),
201
+ sms_sender=FakeSmsSender(),
202
+ )
203
+ ```
204
+
205
+ ## Advanced Patterns
206
+
207
+ ### Pattern 5: Rule of Three
208
+
209
+ Wait until you have three instances before abstracting.
210
+
211
+ ```python
212
+ # Two similar functions? Don't abstract yet
213
+ def process_orders(orders: list[Order]) -> list[Result]:
214
+ results = []
215
+ for order in orders:
216
+ validated = validate_order(order)
217
+ result = process_validated_order(validated)
218
+ results.append(result)
219
+ return results
220
+
221
+ def process_returns(returns: list[Return]) -> list[Result]:
222
+ results = []
223
+ for ret in returns:
224
+ validated = validate_return(ret)
225
+ result = process_validated_return(validated)
226
+ results.append(result)
227
+ return results
228
+
229
+ # These look similar, but wait! Are they actually the same?
230
+ # Different validation, different processing, different errors...
231
+ # Duplication is often better than the wrong abstraction
232
+
233
+ # Only after a third case, consider if there's a real pattern
234
+ # But even then, sometimes explicit is better than abstract
235
+ ```
236
+
237
+ ### Pattern 6: Function Size Guidelines
238
+
239
+ Keep functions focused. Extract when a function:
240
+
241
+ - Exceeds 20-50 lines (varies by complexity)
242
+ - Serves multiple distinct purposes
243
+ - Has deeply nested logic (3+ levels)
244
+
245
+ ```python
246
+ # Too long, multiple concerns mixed
247
+ def process_order(order: Order) -> Result:
248
+ # 50 lines of validation...
249
+ # 30 lines of inventory check...
250
+ # 40 lines of payment processing...
251
+ # 20 lines of notification...
252
+ pass
253
+
254
+ # Better: Composed from focused functions
255
+ def process_order(order: Order) -> Result:
256
+ """Process a customer order through the complete workflow."""
257
+ validate_order(order)
258
+ reserve_inventory(order)
259
+ payment_result = charge_payment(order)
260
+ send_confirmation(order, payment_result)
261
+ return Result(success=True, order_id=order.id)
262
+ ```
263
+
264
+ ### Pattern 7: Dependency Injection
265
+
266
+ Pass dependencies through constructors for testability.
267
+
268
+ ```python
269
+ from typing import Protocol
270
+
271
+ class Logger(Protocol):
272
+ def info(self, msg: str, **kwargs) -> None: ...
273
+ def error(self, msg: str, **kwargs) -> None: ...
274
+
275
+ class Cache(Protocol):
276
+ async def get(self, key: str) -> str | None: ...
277
+ async def set(self, key: str, value: str, ttl: int) -> None: ...
278
+
279
+ class UserService:
280
+ """Service with injected dependencies."""
281
+
282
+ def __init__(
283
+ self,
284
+ repository: UserRepository,
285
+ cache: Cache,
286
+ logger: Logger,
287
+ ) -> None:
288
+ self._repo = repository
289
+ self._cache = cache
290
+ self._logger = logger
291
+
292
+ async def get_user(self, user_id: str) -> User:
293
+ # Check cache first
294
+ cached = await self._cache.get(f"user:{user_id}")
295
+ if cached:
296
+ self._logger.info("Cache hit", user_id=user_id)
297
+ return User.from_json(cached)
298
+
299
+ # Fetch from database
300
+ user = await self._repo.get_by_id(user_id)
301
+ if user:
302
+ await self._cache.set(f"user:{user_id}", user.to_json(), ttl=300)
303
+
304
+ return user
305
+
306
+ # Production
307
+ service = UserService(
308
+ repository=PostgresUserRepository(db),
309
+ cache=RedisCache(redis),
310
+ logger=StructlogLogger(),
311
+ )
312
+
313
+ # Testing
314
+ service = UserService(
315
+ repository=InMemoryUserRepository(),
316
+ cache=FakeCache(),
317
+ logger=NullLogger(),
318
+ )
319
+ ```
320
+
321
+ ### Pattern 8: Avoiding Common Anti-Patterns
322
+
323
+ **Don't expose internal types:**
324
+
325
+ ```python
326
+ # BAD: Leaking ORM model to API
327
+ @app.get("/users/{id}")
328
+ def get_user(id: str) -> UserModel: # SQLAlchemy model
329
+ return db.query(UserModel).get(id)
330
+
331
+ # GOOD: Use response schemas
332
+ @app.get("/users/{id}")
333
+ def get_user(id: str) -> UserResponse:
334
+ user = db.query(UserModel).get(id)
335
+ return UserResponse.from_orm(user)
336
+ ```
337
+
338
+ **Don't mix I/O with business logic:**
339
+
340
+ ```python
341
+ # BAD: SQL embedded in business logic
342
+ def calculate_discount(user_id: str) -> float:
343
+ user = db.query("SELECT * FROM users WHERE id = ?", user_id)
344
+ orders = db.query("SELECT * FROM orders WHERE user_id = ?", user_id)
345
+ # Business logic mixed with data access
346
+
347
+ # GOOD: Repository pattern
348
+ def calculate_discount(user: User, order_history: list[Order]) -> float:
349
+ # Pure business logic, easily testable
350
+ if len(order_history) > 10:
351
+ return 0.15
352
+ return 0.0
353
+ ```
@@ -0,0 +1,193 @@
1
+ ---
2
+ name: python-error-handling
3
+ description: Python error handling patterns including input validation, exception hierarchies, and partial failure handling. Use when implementing validation logic, designing exception strategies, handling batch processing failures, or building robust APIs.
4
+ ---
5
+
6
+ # Python Error Handling
7
+
8
+ Build robust Python applications with proper input validation, meaningful exceptions, and graceful failure handling. Good error handling makes debugging easier and systems more reliable.
9
+
10
+ ## When to Use This Skill
11
+
12
+ - Validating user input and API parameters
13
+ - Designing exception hierarchies for applications
14
+ - Handling partial failures in batch operations
15
+ - Converting external data to domain types
16
+ - Building user-friendly error messages
17
+ - Implementing fail-fast validation patterns
18
+
19
+ ## Core Concepts
20
+
21
+ ### 1. Fail Fast
22
+
23
+ Validate inputs early, before expensive operations. Report all validation errors at once when possible.
24
+
25
+ ### 2. Meaningful Exceptions
26
+
27
+ Use appropriate exception types with context. Messages should explain what failed, why, and how to fix it.
28
+
29
+ ### 3. Partial Failures
30
+
31
+ In batch operations, don't let one failure abort everything. Track successes and failures separately.
32
+
33
+ ### 4. Preserve Context
34
+
35
+ Chain exceptions to maintain the full error trail for debugging.
36
+
37
+ ## Quick Start
38
+
39
+ ```python
40
+ def fetch_page(url: str, page_size: int) -> Page:
41
+ if not url:
42
+ raise ValueError("'url' is required")
43
+ if not 1 <= page_size <= 100:
44
+ raise ValueError(f"'page_size' must be 1-100, got {page_size}")
45
+ # Now safe to proceed...
46
+ ```
47
+
48
+ ## Fundamental Patterns
49
+
50
+ ### Pattern 1: Early Input Validation
51
+
52
+ Validate all inputs at API boundaries before any processing begins.
53
+
54
+ ```python
55
+ def process_order(
56
+ order_id: str,
57
+ quantity: int,
58
+ discount_percent: float,
59
+ ) -> OrderResult:
60
+ """Process an order with validation."""
61
+ # Validate required fields
62
+ if not order_id:
63
+ raise ValueError("'order_id' is required")
64
+
65
+ # Validate ranges
66
+ if quantity <= 0:
67
+ raise ValueError(f"'quantity' must be positive, got {quantity}")
68
+
69
+ if not 0 <= discount_percent <= 100:
70
+ raise ValueError(
71
+ f"'discount_percent' must be 0-100, got {discount_percent}"
72
+ )
73
+
74
+ # Validation passed, proceed with processing
75
+ return _process_validated_order(order_id, quantity, discount_percent)
76
+ ```
77
+
78
+ ### Pattern 2: Convert to Domain Types Early
79
+
80
+ Parse strings and external data into typed domain objects at system boundaries.
81
+
82
+ ```python
83
+ from enum import Enum
84
+
85
+ class OutputFormat(Enum):
86
+ JSON = "json"
87
+ CSV = "csv"
88
+ PARQUET = "parquet"
89
+
90
+ def parse_output_format(value: str) -> OutputFormat:
91
+ """Parse string to OutputFormat enum.
92
+
93
+ Args:
94
+ value: Format string from user input.
95
+
96
+ Returns:
97
+ Validated OutputFormat enum member.
98
+
99
+ Raises:
100
+ ValueError: If format is not recognized.
101
+ """
102
+ try:
103
+ return OutputFormat(value.lower())
104
+ except ValueError:
105
+ valid_formats = [f.value for f in OutputFormat]
106
+ raise ValueError(
107
+ f"Invalid format '{value}'. "
108
+ f"Valid options: {', '.join(valid_formats)}"
109
+ )
110
+
111
+ # Usage at API boundary
112
+ def export_data(data: list[dict], format_str: str) -> bytes:
113
+ output_format = parse_output_format(format_str) # Fail fast
114
+ # Rest of function uses typed OutputFormat
115
+ ...
116
+ ```
117
+
118
+ ### Pattern 3: Pydantic for Complex Validation
119
+
120
+ Use Pydantic models for structured input validation with automatic error messages.
121
+
122
+ ```python
123
+ from pydantic import BaseModel, Field, field_validator
124
+
125
+ class CreateUserInput(BaseModel):
126
+ """Input model for user creation."""
127
+
128
+ email: str = Field(..., min_length=5, max_length=255)
129
+ name: str = Field(..., min_length=1, max_length=100)
130
+ age: int = Field(ge=0, le=150)
131
+
132
+ @field_validator("email")
133
+ @classmethod
134
+ def validate_email_format(cls, v: str) -> str:
135
+ if "@" not in v or "." not in v.split("@")[-1]:
136
+ raise ValueError("Invalid email format")
137
+ return v.lower()
138
+
139
+ @field_validator("name")
140
+ @classmethod
141
+ def normalize_name(cls, v: str) -> str:
142
+ return v.strip().title()
143
+
144
+ # Usage
145
+ try:
146
+ user_input = CreateUserInput(
147
+ email="user@example.com",
148
+ name="john doe",
149
+ age=25,
150
+ )
151
+ except ValidationError as e:
152
+ # Pydantic provides detailed error information
153
+ print(e.errors())
154
+ ```
155
+
156
+ ### Pattern 4: Map Errors to Standard Exceptions
157
+
158
+ Use Python's built-in exception types appropriately, adding context as needed.
159
+
160
+ | Failure Type | Exception | Example |
161
+ |--------------|-----------|---------|
162
+ | Invalid input | `ValueError` | Bad parameter values |
163
+ | Wrong type | `TypeError` | Expected string, got int |
164
+ | Missing item | `KeyError` | Dict key not found |
165
+ | Operational failure | `RuntimeError` | Service unavailable |
166
+ | Timeout | `TimeoutError` | Operation took too long |
167
+ | File not found | `FileNotFoundError` | Path doesn't exist |
168
+ | Permission denied | `PermissionError` | Access forbidden |
169
+
170
+ ```python
171
+ # Good: Specific exception with context
172
+ raise ValueError(f"'page_size' must be 1-100, got {page_size}")
173
+
174
+ # Avoid: Generic exception, no context
175
+ raise Exception("Invalid parameter")
176
+ ```
177
+
178
+ ## Detailed worked examples and patterns
179
+
180
+ Detailed sections (starting with `## Advanced Patterns`) live in `references/details.md`. Read that file when the navigation summary above is insufficient.
181
+
182
+ ## Best Practices Summary
183
+
184
+ 1. **Validate early** - Check inputs before expensive operations
185
+ 2. **Use specific exceptions** - `ValueError`, `TypeError`, not generic `Exception`
186
+ 3. **Include context** - Messages should explain what, why, and how to fix
187
+ 4. **Convert types at boundaries** - Parse strings to enums/domain types early
188
+ 5. **Chain exceptions** - Use `raise ... from e` to preserve debug info
189
+ 6. **Handle partial failures** - Don't abort batches on single item errors
190
+ 7. **Use Pydantic** - For complex input validation with structured errors
191
+ 8. **Document failure modes** - Docstrings should list possible exceptions
192
+ 9. **Log with context** - Include IDs, counts, and other debugging info
193
+ 10. **Test error paths** - Verify exceptions are raised correctly
@@ -0,0 +1,171 @@
1
+ # python-error-handling — detailed worked examples
2
+
3
+ ## Advanced Patterns
4
+
5
+ ### Pattern 5: Custom Exceptions with Context
6
+
7
+ Create domain-specific exceptions that carry structured information.
8
+
9
+ ```python
10
+ class ApiError(Exception):
11
+ """Base exception for API errors."""
12
+
13
+ def __init__(
14
+ self,
15
+ message: str,
16
+ status_code: int,
17
+ response_body: str | None = None,
18
+ ) -> None:
19
+ self.status_code = status_code
20
+ self.response_body = response_body
21
+ super().__init__(message)
22
+
23
+ class RateLimitError(ApiError):
24
+ """Raised when rate limit is exceeded."""
25
+
26
+ def __init__(self, retry_after: int) -> None:
27
+ self.retry_after = retry_after
28
+ super().__init__(
29
+ f"Rate limit exceeded. Retry after {retry_after}s",
30
+ status_code=429,
31
+ )
32
+
33
+ # Usage
34
+ def handle_response(response: Response) -> dict:
35
+ match response.status_code:
36
+ case 200:
37
+ return response.json()
38
+ case 401:
39
+ raise ApiError("Invalid credentials", 401)
40
+ case 404:
41
+ raise ApiError(f"Resource not found: {response.url}", 404)
42
+ case 429:
43
+ retry_after = int(response.headers.get("Retry-After", 60))
44
+ raise RateLimitError(retry_after)
45
+ case code if 400 <= code < 500:
46
+ raise ApiError(f"Client error: {response.text}", code)
47
+ case code if code >= 500:
48
+ raise ApiError(f"Server error: {response.text}", code)
49
+ ```
50
+
51
+ ### Pattern 6: Exception Chaining
52
+
53
+ Preserve the original exception when re-raising to maintain the debug trail.
54
+
55
+ ```python
56
+ import httpx
57
+
58
+ class ServiceError(Exception):
59
+ """High-level service operation failed."""
60
+ pass
61
+
62
+ def upload_file(path: str) -> str:
63
+ """Upload file and return URL."""
64
+ try:
65
+ with open(path, "rb") as f:
66
+ response = httpx.post("https://upload.example.com", files={"file": f})
67
+ response.raise_for_status()
68
+ return response.json()["url"]
69
+ except FileNotFoundError as e:
70
+ raise ServiceError(f"Upload failed: file not found at '{path}'") from e
71
+ except httpx.HTTPStatusError as e:
72
+ raise ServiceError(
73
+ f"Upload failed: server returned {e.response.status_code}"
74
+ ) from e
75
+ except httpx.RequestError as e:
76
+ raise ServiceError(f"Upload failed: network error") from e
77
+ ```
78
+
79
+ ### Pattern 7: Batch Processing with Partial Failures
80
+
81
+ Never let one bad item abort an entire batch. Track results per item.
82
+
83
+ ```python
84
+ from dataclasses import dataclass
85
+
86
+ @dataclass
87
+ class BatchResult[T]:
88
+ """Results from batch processing."""
89
+
90
+ succeeded: dict[int, T] # index -> result
91
+ failed: dict[int, Exception] # index -> error
92
+
93
+ @property
94
+ def success_count(self) -> int:
95
+ return len(self.succeeded)
96
+
97
+ @property
98
+ def failure_count(self) -> int:
99
+ return len(self.failed)
100
+
101
+ @property
102
+ def all_succeeded(self) -> bool:
103
+ return len(self.failed) == 0
104
+
105
+ def process_batch(items: list[Item]) -> BatchResult[ProcessedItem]:
106
+ """Process items, capturing individual failures.
107
+
108
+ Args:
109
+ items: Items to process.
110
+
111
+ Returns:
112
+ BatchResult with succeeded and failed items by index.
113
+ """
114
+ succeeded: dict[int, ProcessedItem] = {}
115
+ failed: dict[int, Exception] = {}
116
+
117
+ for idx, item in enumerate(items):
118
+ try:
119
+ result = process_single_item(item)
120
+ succeeded[idx] = result
121
+ except Exception as e:
122
+ failed[idx] = e
123
+
124
+ return BatchResult(succeeded=succeeded, failed=failed)
125
+
126
+ # Caller handles partial results
127
+ result = process_batch(items)
128
+ if not result.all_succeeded:
129
+ logger.warning(
130
+ f"Batch completed with {result.failure_count} failures",
131
+ failed_indices=list(result.failed.keys()),
132
+ )
133
+ ```
134
+
135
+ ### Pattern 8: Progress Reporting for Long Operations
136
+
137
+ Provide visibility into batch progress without coupling business logic to UI.
138
+
139
+ ```python
140
+ from collections.abc import Callable
141
+
142
+ ProgressCallback = Callable[[int, int, str], None] # current, total, status
143
+
144
+ def process_large_batch(
145
+ items: list[Item],
146
+ on_progress: ProgressCallback | None = None,
147
+ ) -> BatchResult:
148
+ """Process batch with optional progress reporting.
149
+
150
+ Args:
151
+ items: Items to process.
152
+ on_progress: Optional callback receiving (current, total, status).
153
+ """
154
+ total = len(items)
155
+ succeeded = {}
156
+ failed = {}
157
+
158
+ for idx, item in enumerate(items):
159
+ if on_progress:
160
+ on_progress(idx, total, f"Processing {item.id}")
161
+
162
+ try:
163
+ succeeded[idx] = process_single_item(item)
164
+ except Exception as e:
165
+ failed[idx] = e
166
+
167
+ if on_progress:
168
+ on_progress(total, total, "Complete")
169
+
170
+ return BatchResult(succeeded=succeeded, failed=failed)
171
+ ```