ai-developer-skill-os 9.1.1 → 9.3.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 (63) hide show
  1. package/.agents/AGENTS.md +139 -42
  2. package/.agents/DEV_PROFILE.md +95 -0
  3. package/.agents/registry/capability-graph.yml +171 -334
  4. package/.agents/registry/graph.json +52 -18
  5. package/.agents/registry/index.yaml +66 -12
  6. package/.agents/registry/skills-index.yml +204 -447
  7. package/.agents/rules/coding.md +23 -0
  8. package/.agents/rules/global.md +44 -30
  9. package/.agents/skills/_template/SKILL.md +2 -300
  10. package/.agents/skills/qk-access-policy/SKILL.md +98 -393
  11. package/.agents/skills/qk-agent-observability/SKILL.md +3 -359
  12. package/.agents/skills/qk-ai-builder/SKILL.md +132 -485
  13. package/.agents/skills/qk-api-consumer/SKILL.md +256 -0
  14. package/.agents/skills/qk-api-consumer/capability.yaml +21 -0
  15. package/.agents/skills/qk-api-consumer/evals/scorecard.yaml +29 -0
  16. package/.agents/skills/qk-api-lifecycle/SKILL.md +133 -376
  17. package/.agents/skills/qk-bug-resolution/SKILL.md +128 -481
  18. package/.agents/skills/qk-code-review/SKILL.md +142 -340
  19. package/.agents/skills/qk-context-loader/SKILL.md +99 -391
  20. package/.agents/skills/qk-data-engineer/SKILL.md +253 -0
  21. package/.agents/skills/qk-data-lifecycle/SKILL.md +89 -408
  22. package/.agents/skills/qk-db-optimizer/SKILL.md +102 -408
  23. package/.agents/skills/qk-design-system-engineering/SKILL.md +76 -381
  24. package/.agents/skills/qk-devops-platform/SKILL.md +77 -383
  25. package/.agents/skills/qk-docs/SKILL.md +88 -414
  26. package/.agents/skills/qk-engineering-standard/SKILL.md +4 -508
  27. package/.agents/skills/qk-fe-api-integration/SKILL.md +343 -263
  28. package/.agents/skills/qk-fe-api-integration/evals/scorecard.yaml +29 -29
  29. package/.agents/skills/qk-feature-delivery/SKILL.md +137 -365
  30. package/.agents/skills/qk-feature-delivery/evals/scorecard.yaml +1 -1
  31. package/.agents/skills/qk-frontend-architecture/SKILL.md +5 -399
  32. package/.agents/skills/qk-help/evals/scorecard.yaml +13 -13
  33. package/.agents/skills/qk-orchestrator/SKILL.md +63 -422
  34. package/.agents/skills/qk-orchestrator/references/routing-table.md +10 -14
  35. package/.agents/skills/qk-product-specification/SKILL.md +70 -399
  36. package/.agents/skills/qk-production-release/SKILL.md +80 -457
  37. package/.agents/skills/qk-project-audit/SKILL.md +174 -0
  38. package/.agents/skills/qk-project-bootstrap/SKILL.md +244 -392
  39. package/.agents/skills/qk-project-health/SKILL.md +97 -416
  40. package/.agents/skills/qk-project-memory/SKILL.md +76 -21
  41. package/.agents/skills/qk-refactor/SKILL.md +167 -304
  42. package/.agents/skills/qk-security-audit/SKILL.md +141 -383
  43. package/.agents/skills/qk-security-audit/capability.yaml +1 -2
  44. package/.agents/skills/qk-system-evolution/SKILL.md +343 -263
  45. package/.agents/skills/qk-system-evolution/evals/scorecard.yaml +26 -26
  46. package/.agents/skills/qk-test-engineering/SKILL.md +119 -429
  47. package/.agents/skills/qk-ui-audit/SKILL.md +73 -457
  48. package/.agents/skills/qk-ui-builder/SKILL.md +521 -482
  49. package/.agents/skills/qk-ui-system-builder/SKILL.md +68 -434
  50. package/.agents/skills/qk-upgrade/SKILL.md +301 -0
  51. package/.agents/skills/qk-upgrade/capability.yaml +24 -0
  52. package/.agents/skills/qk-upgrade/evals/scorecard.yaml +26 -0
  53. package/.agents/skills/qk-validation-gate/SKILL.md +4 -523
  54. package/.agents/skills/qk-web-quality-gate/SKILL.md +85 -383
  55. package/.agents/workflows/bug-resolution.yml +6 -6
  56. package/.agents/workflows/context-discovery.yml +94 -0
  57. package/.agents/workflows/feature-delivery.yml +8 -4
  58. package/.agents/workflows/refactor.yml +6 -3
  59. package/.agents/workflows/shared/quality-gate.yml +94 -0
  60. package/.agents/workflows/skin-governance.yml +115 -0
  61. package/README.md +152 -67
  62. package/package.json +2 -2
  63. package/tooling/build-registry.js +30 -8
@@ -1,9 +1,9 @@
1
- ---
1
+ ---
2
2
  # ── Identity ───────────────────────────────────────────────
3
3
  name: qk-api-lifecycle
4
- version: 9.1.0
4
+ version: 9.2.0
5
5
  status: stable
6
- description: "Thiết kế, triển khai API endpoints với Zero-Trust — contract trước, code sau."
6
+ description: "Thiết kế implement API endpoint mới với Zero-Trust — contract trước, code sau. Dùng skill này khi user nhắc đến: viết api, tạo endpoint, thiết kế api, build api, rest, graphql, trpc, route handler, controller, request/response schema — kể cả khi chỉ hỏi \"API này nên nhận input gì, trả output gì\"."
7
7
  platforms: [antigravity, claude-code, cursor, windsurf, kilo-code]
8
8
 
9
9
  # ── V9: Classification ─────────────────────────────────────
@@ -21,11 +21,18 @@ complexity:
21
21
  has_external_dependency: true
22
22
  has_breaking_change: false
23
23
 
24
- triggers:
25
- - "viết api"
26
- - "tạo endpoint"
27
- - "build api"
28
- - "thiết kế api"
24
+ triggers:
25
+ - "viết api"
26
+ - "tạo endpoint"
27
+ - "thiết kế api"
28
+ - "build api"
29
+ - "rest"
30
+ - "graphql"
31
+ - "trpc"
32
+ - "route handler"
33
+ - "controller"
34
+ - "request/response schema"
35
+
29
36
 
30
37
  # ── V8: References ─────────────────────────────────────────
31
38
  workflow: feature-delivery
@@ -77,418 +84,168 @@ token_budget:
77
84
  max_shell_commands: 1
78
85
  stop_early: true
79
86
 
80
- exit_codes: [SUCCESS, BLOCKED, FAILED, PARTIAL]
87
+ exit_codes: [SUCCESS, BLOCKED, FAILED, PARTIAL]
81
88
  ---
82
89
 
83
90
  # qk-api-lifecycle — API Design & Implementation
84
91
 
85
92
  > **Language rule:** Code, identifiers, file names → English. Explanations, summaries → Vietnamese.
86
93
 
94
+ Chịu trách nhiệm thiết kế và triển khai API endpoints mới hoặc nâng cấp endpoint hiện hữu theo triết lý **Zero-Trust: Contract First (Schema & Validation) → Route/Controller → Service/Business Logic → Error Handling & Documentation**.
95
+
87
96
  ---
88
97
 
89
- ## Memory Workflow
90
-
91
- ### 0. Self-Init Protocol (Khởi Tạo Bộ Nhớ Local & Gitignore)
92
- - Trước khi tra cứu hoặc lưu trữ tri thức, BẮT BUỘC kiểm tra sự tồn tại của thư mục `.ai-local/` tại gốc dự án:
93
- - **Tự động tạo mới:** Nếu `.ai-local/` chưa tồn tại, AI phải tự động tạo cấu trúc thư mục `.ai-local/knowledge/` (và file `index.yaml` nếu cần thiết) cùng `.ai-local/candidates/`. Tuyệt đối không ngưng chạy hay hỏi ý kiến người dùng về thao tác khởi tạo tiêu chuẩn này.
94
- - **Bảo mật Gitignore:** BẮT BUỘC kiểm tra file `.gitignore` của dự án, nếu chưa có dòng `.ai-local/` thì phải tự động thêm vào để tuyệt đối bảo mật tri thức cá nhân và tránh lộ lọt lên Git.
95
-
96
- ---
97
-
98
- ### Pre-flight Retrieve (Trước khi thực thi)
99
- - Trước các task có tính lặp lại, debug, refactor, kiến trúc hoặc rủi ro cao:
100
- bắt buộc tra cứu:
101
- - `.ai-local/knowledge/index.yaml` (Private Local Knowledge)
102
-
103
- - Ưu tiên sử dụng các Knowledge đang có trạng thái `Active` thuộc:
104
- - Architecture
105
- - Hard Bug
106
- - Convention
107
- - Pattern
108
- - Tech Debt Pattern
109
- - 👉 *Domain Focus:* Convention/Pattern (vd: cấu trúc wrapper { status, data, error }, pagination format).
110
-
111
- - Memory chỉ đóng vai trò **Navigator (bản đồ chỉ đường)**.
112
- Không được xem Memory là Source of Truth.
113
- Luôn xác minh lại bằng source code, configuration và trạng thái hiện tại của dự án trước khi áp dụng.
114
-
115
- ---
116
-
117
- ### Learning Flow (AI tự học có kiểm soát)
118
- - Trong quá trình làm việc, AI được phép tự phát hiện và tạo **Candidate Memory** khi nhận thấy:
119
- - Hard Bug có khả năng tái diễn.
120
- - Pattern làm việc lặp lại trong dự án.
121
- - Convention hoặc quy tắc kiến trúc mới.
122
- - Quyết định Architecture quan trọng.
123
- - Tech Debt Pattern hoặc Code Smell có tính hệ thống.
124
- - 👉 *Domain Harvest:* Pattern hoặc Convention mới của dự án (vd: chuẩn xử lý lỗi API mới, rate limiting).
125
-
126
- - Candidate Memory chỉ là bản nháp quan sát, chưa phải tri thức chính thức.
127
- - Candidate Memory có thể lưu tạm tại: `.ai-local/candidates/`
128
- - AI không được tự động Promote Candidate Memory thành Project Knowledge.
129
-
130
- ---
131
-
132
- ### Post-flight Harvest (Đề xuất → Phê duyệt)
133
- Sau khi hoàn thành task:
134
- - AI đánh giá các Candidate Memory đã tạo.
135
- - Nếu phát hiện tri thức có giá trị tái sử dụng:
136
- - Đề xuất người dùng xem xét.
137
- - Gửi yêu cầu phê duyệt thông qua:
138
- - `/learn`
139
- - `qk-project-memory`
140
- - Chỉ sau khi được phê duyệt, Candidate Memory mới được chuyển thành Knowledge chính thức:
141
-
142
- ```
143
- .ai-local/candidates/ ──(Approve)──> .ai-local/knowledge/index.yaml
144
- ```
145
-
146
- - Project Knowledge phải được xem như tài sản kỹ thuật của dự án:
147
- - Có thể review, cập nhật, loại bỏ và có lịch sử thay đổi.
148
-
149
- ---
150
-
151
- ### Ignore (Không đưa vào Memory)
152
- Không lưu:
153
- - Trace log của một session đơn lẻ.
154
- - Temporary debugging data.
155
- - Output của một lần chạy test/scan.
156
- - Report health tạm thời của một đợt kiểm tra.
157
- - Lỗi nhỏ chỉ xảy ra một lần.
158
- - Thông tin không có khả năng tái sử dụng.
159
- - 👉 *Domain Ignore:* Thao tác thêm 1 CRUD endpoint thông thường theo Hợp đồng sẵn có.
160
-
161
- ---
162
-
163
- ### Golden Rule
164
- > **AI được phép học, nhưng không được tự quyết định tri thức chính thức.**
165
- > **AI quan sát → Đề xuất → Con người phê duyệt → Dự án tiến hóa.**
166
-
167
- ---
168
-
169
-
170
-
171
- ### Pre-flight Retrieve (Trước khi thực thi)
172
- - Trước các task có tính lặp lại, debug, refactor, kiến trúc hoặc rủi ro cao:
173
- bắt buộc tra cứu:
174
- - `.agents/knowledge/index.yaml` (Shared Project Knowledge)
175
- - `.ai-local/knowledge/index.yaml` (Private Local Knowledge)
176
-
177
- - Ưu tiên sử dụng các Knowledge đang có trạng thái `Active` thuộc:
178
- - Architecture
179
- - Hard Bug
180
- - Convention
181
- - Pattern
182
- - Tech Debt Pattern
183
- - 👉 *Domain Focus:* Convention/Pattern (vd: cấu trúc wrapper { status, data, error }, pagination format).
184
-
185
- - Memory chỉ đóng vai trò **Navigator (bản đồ chỉ đường)**.
186
- Không được xem Memory là Source of Truth.
187
- Luôn xác minh lại bằng source code, configuration và trạng thái hiện tại của dự án trước khi áp dụng.
188
-
189
- ---
190
-
191
- ### Learning Flow (AI tự học có kiểm soát)
192
- - Trong quá trình làm việc, AI được phép tự phát hiện và tạo **Candidate Memory** khi nhận thấy:
193
- - Hard Bug có khả năng tái diễn.
194
- - Pattern làm việc lặp lại trong dự án.
195
- - Convention hoặc quy tắc kiến trúc mới.
196
- - Quyết định Architecture quan trọng.
197
- - Tech Debt Pattern hoặc Code Smell có tính hệ thống.
198
- - 👉 *Domain Harvest:* Pattern hoặc Convention mới của dự án (vd: chuẩn xử lý lỗi API mới, rate limiting).
199
-
200
- - Candidate Memory chỉ là bản nháp quan sát, chưa phải tri thức chính thức.
201
- - Candidate Memory có thể lưu tạm tại: `.ai-local/candidates/`
202
- - AI không được tự động Promote Candidate Memory thành Project Knowledge.
203
-
204
- ---
205
-
206
- ### Post-flight Harvest (Đề xuất → Phê duyệt)
207
- Sau khi hoàn thành task:
208
- - AI đánh giá các Candidate Memory đã tạo.
209
- - Nếu phát hiện tri thức có giá trị tái sử dụng:
210
- - Đề xuất người dùng xem xét.
211
- - Gửi yêu cầu phê duyệt thông qua:
212
- - `/learn`
213
- - `qk-project-memory`
214
- - Chỉ sau khi được phê duyệt, Candidate Memory mới được chuyển thành Knowledge chính thức:
215
-
216
- ```
217
- .ai-local/candidates/ ──(Approve)──> .agents/knowledge/index.yaml
218
- ```
219
-
220
- - Project Knowledge phải được xem như tài sản kỹ thuật của dự án:
221
- - Có thể review, cập nhật, loại bỏ và có lịch sử thay đổi.
222
-
223
- ---
224
-
225
- ### Ignore (Không đưa vào Memory)
226
- Không lưu:
227
- - Trace log của một session đơn lẻ.
228
- - Temporary debugging data.
229
- - Output của một lần chạy test/scan.
230
- - Report health tạm thời của một đợt kiểm tra.
231
- - Lỗi nhỏ chỉ xảy ra một lần.
232
- - Thông tin không có khả năng tái sử dụng.
233
- - 👉 *Domain Ignore:* Thao tác thêm 1 CRUD endpoint thông thường theo Hợp đồng sẵn có.
234
-
235
- ---
236
-
237
- ### Golden Rule
238
- > **AI được phép học, nhưng không được tự quyết định tri thức chính thức.**
239
- > **AI quan sát → Đề xuất → Con người phê duyệt → Dự án tiến hóa.**
240
-
241
- ---
242
- ---
243
-
244
- ### Learning Flow (AI tự học có kiểm soát)
245
- - Trong quá trình làm việc, AI được phép tự phát hiện và tạo **Candidate Memory** khi nhận thấy:
246
- - Hard Bug có khả năng tái diễn.
247
- - Pattern làm việc lặp lại trong dự án.
248
- - Convention hoặc quy tắc kiến trúc mới.
249
- - Quyết định Architecture quan trọng.
250
- - Tech Debt Pattern hoặc Code Smell có tính hệ thống.
251
- - 👉 *Domain Harvest:* Pattern hoặc Convention mới của dự án (vd: chuẩn xử lý lỗi API mới, rate limiting).
252
-
253
- - Candidate Memory chỉ là bản nháp quan sát, chưa phải tri thức chính thức.
254
- - Candidate Memory có thể lưu tạm tại: `.ai-local/candidates/`
255
- - AI không được tự động Promote Candidate Memory thành Project Knowledge.
256
-
257
- ---
258
-
259
- ### Post-flight Harvest (Đề xuất → Phê duyệt)
260
- Sau khi hoàn thành task:
261
- - AI đánh giá các Candidate Memory đã tạo.
262
- - Nếu phát hiện tri thức có giá trị tái sử dụng:
263
- - Đề xuất người dùng xem xét.
264
- - Gửi yêu cầu phê duyệt thông qua:
265
- - `/learn`
266
- - `qk-project-memory`
267
- - Chỉ sau khi được phê duyệt, Candidate Memory mới được chuyển thành Knowledge chính thức:
268
-
269
- ```
270
- .ai-local/candidates/ ──(Approve)──> .agents/knowledge/index.yaml
271
- ```
272
-
273
- - Project Knowledge phải được xem như tài sản kỹ thuật của dự án:
274
- - Có thể review, cập nhật, loại bỏ và có lịch sử thay đổi.
275
-
276
- ---
277
-
278
- ### Ignore (Không đưa vào Memory)
279
- Không lưu:
280
- - Trace log của một session đơn lẻ.
281
- - Temporary debugging data.
282
- - Output của một lần chạy test/scan.
283
- - Report health tạm thời của một đợt kiểm tra.
284
- - Lỗi nhỏ chỉ xảy ra một lần.
285
- - Thông tin không có khả năng tái sử dụng.
286
- - 👉 *Domain Ignore:* Thao tác thêm 1 CRUD endpoint thông thường theo Hợp đồng sẵn có.
287
-
288
- ---
289
-
290
- ### Golden Rule
291
- > **AI được phép học, nhưng không được tự quyết định tri thức chính thức.**
292
- > **AI quan sát → Đề xuất → Con người phê duyệt → Dự án tiến hóa.**
293
-
294
- ---
295
- ---
296
-
297
- ### Learning Flow (AI tự học có kiểm soát)
298
- - Trong quá trình làm việc, AI được phép tự phát hiện và tạo **Candidate Memory** khi nhận thấy:
299
- - Hard Bug có khả năng tái diễn.
300
- - Pattern làm việc lặp lại trong dự án.
301
- - Convention hoặc quy tắc kiến trúc mới.
302
- - Quyết định Architecture quan trọng.
303
- - Tech Debt Pattern hoặc Code Smell có tính hệ thống.
304
- - 👉 *Domain Harvest:* Pattern hoặc Convention mới của dự án (vd: chuẩn xử lý lỗi API mới, rate limiting).
305
-
306
- - Candidate Memory chỉ là bản nháp quan sát, chưa phải tri thức chính thức.
307
- - Candidate Memory có thể lưu tạm tại: `.ai-local/candidates/`
308
- - AI không được tự động Promote Candidate Memory thành Project Knowledge.
309
-
310
- ---
311
-
312
- ### Post-flight Harvest (Đề xuất → Phê duyệt)
313
- Sau khi hoàn thành task:
314
- - AI đánh giá các Candidate Memory đã tạo.
315
- - Nếu phát hiện tri thức có giá trị tái sử dụng:
316
- - Đề xuất người dùng xem xét.
317
- - Gửi yêu cầu phê duyệt thông qua:
318
- - `/learn`
319
- - `qk-project-memory`
320
- - Chỉ sau khi được phê duyệt, Candidate Memory mới được chuyển thành Knowledge chính thức:
321
-
322
- ```
323
- .ai-local/candidates/ ──(Approve)──> .agents/knowledge/index.yaml
324
- ```
325
-
326
- - Project Knowledge phải được xem như tài sản kỹ thuật của dự án:
327
- - Có thể review, cập nhật, loại bỏ và có lịch sử thay đổi.
328
-
329
- ---
330
-
331
- ### Ignore (Không đưa vào Memory)
332
- Không lưu:
333
- - Trace log của một session đơn lẻ.
334
- - Temporary debugging data.
335
- - Output của một lần chạy test/scan.
336
- - Report health tạm thời của một đợt kiểm tra.
337
- - Lỗi nhỏ chỉ xảy ra một lần.
338
- - Thông tin không có khả năng tái sử dụng.
339
- - 👉 *Domain Ignore:* Thao tác thêm 1 CRUD endpoint thông thường theo Hợp đồng sẵn có.
340
-
341
- ---
342
-
343
- ### Golden Rule
344
- > **AI được phép học, nhưng không được tự quyết định tri thức chính thức.**
345
- > **AI quan sát → Đề xuất → Con người phê duyệt → Dự án tiến hóa.**
346
-
347
- ---
348
- ---
349
- ---
350
- ---
351
-
352
98
  ## Preconditions
353
- - [ ] API purpose and resource name are defined
354
- - [ ] Request/response shape is specified (or sample JSON provided)
355
99
 
356
- ```
357
- On missing precondition:
358
- EXIT: BLOCKED
359
- Message: "Cần định nghĩa: resource name + expected request/response shape trước khi code."
360
- ```
100
+ Trước khi viết code API mới, AI BẮT BUỘC kiểm tra:
101
+
102
+ - [ ] Xác định backend framework và routing pattern từ `.agents/DEV_PROFILE.md` (Express, Next.js API Routes, FastAPI, NestJS, Go Fiber, v.v.).
103
+ - [ ] Xác định chế Authentication & Authorization cần thiết (Public, JWT, Session, API Key, RBAC).
104
+ - [ ] Định nghĩa rõ ràng Request schema (headers, params, body) và Response schema (success + error cases).
105
+ - [ ] Nếu vi phạm quy ước REST/HTTP status codes hoặc thiếu validation schema:
106
+ → **EXIT: BLOCKED**
107
+ → Báo cáo user đề xuất chuẩn hóa contract trước khi implement.
361
108
 
362
109
  ---
363
110
 
364
111
  ## Scope
365
- - ✅ Define OpenAPI/Swagger contract BEFORE writing code
366
- - ✅ Implement endpoint strictly to contract
367
- - ✅ Validate all inputs, handle all error cases
368
- - ✅ Ensure backward compatibility
369
112
 
370
- ## Non-Goals
371
- - Introduce breaking changes without versioning
372
- - Bypass auth checks
373
- - Guess external API shapes without evidence
113
+ Skill này làm:
114
+ - Thiết kế Request/Response DTO validation schema (Zod, Joi, Pydantic, Class-validator).
115
+ - Cài đặt Route handlers, Controllers, Middlewares xử lý request.
116
+ - Tích hợp Service layer xử lý logic nghiệp vụ an toàn.
117
+ - Chuẩn hóa HTTP status codes (200, 201, 400, 401, 403, 404, 422, 500) và error response envelope.
118
+ - Đảm bảo sanitized inputs chống SQL Injection, XSS, mass assignment.
374
119
 
375
- ---
376
-
377
- ## Priority Order
378
-
379
- | Priority | Task | Skip Threshold |
380
- |----------|------|----------------|
381
- | P1 | Contract definition (TypeScript types) | Never |
382
- | P2 | Success response + correct status code | Never |
383
- | P3 | Input validation middleware | Never |
384
- | P4 | Error responses (400, 401, 404, 422, 500) | Budget < 30% |
385
- | P5 | Documentation (JSDoc or OpenAPI annotation) | Budget < 60% |
120
+ ❌ Skill này KHÔNG làm:
121
+ - Thay đổi cấu trúc bảng database hoặc migration (→ `qk-data-lifecycle`).
122
+ - Viết giao diện Frontend để gọi API này (→ `qk-api-consumer`).
123
+ - Tối ưu câu query SQL bên dưới (→ `qk-db-optimizer`).
386
124
 
387
125
  ---
388
126
 
389
- ## Workflow
390
-
391
- ### Phase 1 — Contract Definition
392
- 1. Define TypeScript interfaces for Request + Response
393
- 2. Specify HTTP method, route, status codes
394
- 3. Document error cases
127
+ ## Execution Steps
395
128
 
396
- **Decision:**
129
+ ### Step 1 — Contract & Schema Definition
397
130
  ```
398
- IF contract is clear go to Phase 2
399
- ELSE → EXIT: BLOCKED — define contract first
131
+ Inputs: Yêu cầu endpoint từ user, DEV_PROFILE.md
132
+ Actions:
133
+ - Khai báo Schema cho Request (Path params, Query params, Request body).
134
+ - Khai báo Schema cho Response (Success payload, Error payload).
135
+ - Áp dụng các rules kiểm tra tính hợp lệ dữ liệu đầu vào.
136
+ Output: Type-safe Schema definitions
400
137
  ```
401
138
 
402
- ### Phase 2 — Implementation
403
- 1. Build route handler strictly matching contract
404
- 2. Add input validation (zod/yup/class-validator). **BẮT BUỘC tuân thủ R-SEC-04 (Zero-Trust Boundary): Mọi endpoint phải validate payload ở cổng vào, và sanitize/mask response ở cổng ra.**
405
- 3. Handle all error cases with correct HTTP status
406
-
407
- ### Phase 3 — Verification
408
- 1. Read implemented code vs defined contract
409
- 2. Verify: method, route, response shape, error codes match
410
-
411
- **Decision:**
139
+ ### Step 2 — Route & Middleware Setup
412
140
  ```
413
- IF contract and implementation match → EXIT: SUCCESS
414
- ELSE → fix gaps, EXIT: PARTIAL
141
+ Inputs: Schema definitions
142
+ Actions:
143
+ - Khai báo route path và HTTP method tương ứng.
144
+ - Gắn các middlewares bảo vệ: Auth, Rate-limiting, Input validation.
145
+ Output: Secure Route registration
415
146
  ```
416
147
 
417
- ---
418
-
419
- ## Confidence Model
420
- | Level | Condition | Action |
421
- |-------|-----------|--------|
422
- | HIGH | Contract defined, implementation matches | Report SUCCESS |
423
- | MEDIUM | Contract inferred from context | Note assumption |
424
- | LOW | No contract, guessing from usage | EXIT: BLOCKED |
425
-
426
- ---
427
-
428
- ## Evidence Format
148
+ ### Step 3 — Controller & Service Implementation
429
149
  ```
430
- [SEVERITY] src/routes/[resource].ts:LINE
431
- Issue: [mismatch between contract and implementation]
432
- Confidence: HIGH
433
- Fix: [specific change needed]
150
+ Inputs: Validated input data
151
+ Actions:
152
+ - Gọi Service/Repository để xử lý nghiệp vụ.
153
+ - Bọc trong khối try/catch hoặc error boundary trung tâm của framework.
154
+ - Tuyệt đối không để lộ sensitive data (passwords, tokens, internal stack traces) ra client.
155
+ Output: Completed API endpoint
434
156
  ```
435
157
 
436
- ---
437
-
438
- ## Handoff Contract
439
- ### Consumes
440
- ```json
441
- { "from": "user", "required_fields": ["resource_name", "request_shape", "response_shape"] }
158
+ ### Step 4 — Verification & Contract Testing
442
159
  ```
443
- ### Produces
444
- ```json
445
- { "to": "qk-validation-gate", "output_fields": ["route_file", "types_file", "contract_doc", "exit_code"] }
160
+ Inputs: Endpoint implementation
161
+ Actions:
162
+ - Kiểm tra status codes trả về ứng với từng kịch bản (success, missing field, unauthorized).
163
+ - Chạy linter và typecheck.
164
+ Exit: SUCCESS nếu endpoint tuân thủ 100% schema contract và coding standards.
446
165
  ```
447
166
 
448
167
  ---
449
168
 
450
- ## Exit Codes
451
- | Code | Meaning | When |
452
- |------|---------|------|
453
- | SUCCESS | Contract defined, implemented, verified | Implementation matches contract |
454
- | PARTIAL | Implemented but missing error cases or docs | Core logic done, edge cases missing |
455
- | BLOCKED | No contract defined — cannot code | Missing payload/schema |
456
- | FAILED | Implementation breaks existing contract | Backward incompatibility |
169
+ ## Prompt Template
457
170
 
458
- ---
171
+ AI đọc `DEV_PROFILE.md` để biết backend stack + auth pattern. Mô tả API contract — AI lo phần còn lại.
459
172
 
460
- ## Severity
461
- | Level | Definition | Example |
462
- |-------|-----------|---------|
463
- | CRITICAL | Broken contract on public API | Renamed `userId` to `id` in response |
464
- | HIGH | Missing input validation | SQL Injection or crash on null input |
465
- | MEDIUM | Returning 500 for validation errors | Not using 400/422 |
466
- | LOW | Missing JSDoc | Undocumented route |
173
+ ```
174
+ Endpoint: [Method + Path vd: POST /api/orders]
175
+ Mục đích: [API này làm gì trong business context]
176
+ Auth: [Public / user JWT / admin only / service-to-service]
177
+ Input: [Request body / query params / path params]
178
+ Output: [Response shape khi success + khi lỗi]
179
+ Rules: [Business rules, validation, side effects quan trọng]
180
+ ```
467
181
 
468
182
  ---
469
183
 
470
- ## Retry Policy
184
+ ### Theo Role — AI thiết kế theo chiều sâu khác nhau:
185
+
186
+ **role: be**
471
187
  ```
472
- Contract verification fails
473
- └─ Implementation does not match contract
474
- ├─ Fix route handler to match contract
475
- └─ Do NOT retry more than 1 time
188
+ Endpoint: POST /api/orders
189
+ Mục đích: Tạo đơn hàng mới, trừ stock, gửi email xác nhận
190
+ Auth: User JWT (phải login)
191
+ Input: { items: [{ productId, quantity }], shippingAddressId, paymentMethod }
192
+ Output: 201 { orderId, totalAmount, estimatedDelivery }
193
+ 400 nếu product hết hàng, 404 nếu product không tồn tại
194
+ Rules: Transaction: trừ stock + tạo order phải atomic. Stock < 0 thì rollback.
195
+ Email gửi async (không block response). Idempotency key từ client.
476
196
  ```
197
+ → AI thiết kế: DB transaction scope, optimistic vs pessimistic locking cho stock,
198
+ idempotency key pattern, async job queue cho email, error taxonomy (4xx vs 5xx),
199
+ OpenAPI spec trước khi code.
477
200
 
478
- ---
201
+ **role: fullstack**
202
+ ```
203
+ Endpoint: GET /api/dashboard/stats
204
+ Mục đích: Trả thống kê cho dashboard — revenue, orders, users hôm nay
205
+ Auth: Admin JWT
206
+ Input: ?from=2024-01-01&to=2024-01-31&timezone=Asia/Ho_Chi_Minh
207
+ Output: { revenue: number, orders: number, newUsers: number, topProducts: [] }
208
+ Rules: Cache 5 phút. Timezone-aware aggregation. Chỉ admin mới gọi được.
209
+ ```
210
+ → AI thiết kế: query aggregation strategy, DB index cho date range,
211
+ timezone handling (store UTC, convert at query time), cache layer (Redis vs in-memory),
212
+ type-safe response contract cho FE (zod schema hoặc OpenAPI codegen).
479
213
 
480
- ## Escalation Rules
214
+ **role: fe** *(khi FE cần hiểu API để tích hợp đúng)*
481
215
  ```
482
- BLOCKED: No contract defined
483
- Missing:
484
- - Request payload structure
485
- - Response payload structure
486
- Questions:
487
- 1. Payload gửi lên (Request) có những field gì?
488
- 2. Payload trả về (Response) có cấu trúc thế nào?
489
- Recommended Assumptions:
490
- - Default JSON response with `{ data, message }` wrapper
216
+ Endpoint: GET /api/products (đã có — cần hiểu contract để tích hợp)
217
+ Mục đích: Lấy danh sách sản phẩm với filter + pagination
218
+ Input: ?page=1&limit=20&category=shoes&minPrice=100&sort=price_asc
219
+ Output: { data: Product[], total: number, page: number, hasNextPage: boolean }
220
+ Rules: Cần biết: khi nào trả 404 vs data rỗng? Error shape là gì?
491
221
  ```
222
+ → AI phân tích: type generation từ API response, error boundary design,
223
+ loading state granularity, stale-while-revalidate strategy, mock data cho dev.
492
224
 
493
- ---
225
+ **role: data** *(API nhận dữ liệu từ pipeline)*
226
+ ```
227
+ Endpoint: POST /api/ingest/events (webhook từ data pipeline)
228
+ Mục đích: Nhận batch events từ Kafka consumer, validate và lưu vào DB
229
+ Auth: Service-to-service API key
230
+ Input: { events: [{ type, payload, timestamp, source }], batchId }
231
+ Output: 202 { accepted: N, rejected: M, errors: [] }
232
+ Rules: Idempotent theo batchId. Reject invalid schema nhưng không fail toàn batch.
233
+ Rate limit: 1000 events/request, 100 req/min.
234
+ ```
235
+ → AI thiết kế: bulk insert strategy, partial failure handling,
236
+ schema validation per-row vs batch-level, idempotency key indexing,
237
+ backpressure mechanism khi DB chậm.
494
238
 
239
+ **role: devops** *(API health + observability endpoints)*
240
+ ```
241
+ Endpoint: GET /health, GET /metrics, GET /ready
242
+ Mục đích: Kubernetes liveness/readiness probe + Prometheus scraping
243
+ Auth: Internal only (không expose ra public)
244
+ Output: /health: { status: "ok", uptime, version }
245
+ /metrics: Prometheus format
246
+ /ready: 200 nếu DB connected, 503 nếu không
247
+ Rules: /health không check DB (liveness). /ready check DB (readiness).
248
+ /metrics không cần auth nhưng chỉ bind localhost.
249
+ ```
250
+ → AI thiết kế: liveness vs readiness semantics, graceful shutdown handling,
251
+ metric naming convention (RED method), scrape security, k8s probe config.