@educa-corp/sdd-framework 0.2.3 → 0.2.5

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 (150) hide show
  1. package/commands/generate-architecture.md +706 -0
  2. package/commands/generate-architecture.tmpl +194 -0
  3. package/commands/generate-code.md +16 -2
  4. package/commands/generate-code.tmpl +16 -2
  5. package/commands/generate-tech-docs.md +19 -0
  6. package/commands/generate-tech-docs.tmpl +19 -0
  7. package/core/FRAMEWORK_VERSION +1 -1
  8. package/core/commands/generate-architecture.md +706 -0
  9. package/core/commands/generate-code.md +16 -2
  10. package/core/commands/generate-tech-docs.md +19 -0
  11. package/core/skills/setup-ai-first/SKILL.md +12 -4
  12. package/core/templates/architecture.template.md +392 -111
  13. package/docs/01-getting-started/installation.md +47 -112
  14. package/docs/01-getting-started/quickstart.md +58 -72
  15. package/docs/01-getting-started/what-is-sdd.md +75 -0
  16. package/docs/02-concepts/architecture.md +109 -0
  17. package/docs/02-concepts/glossary.md +87 -0
  18. package/docs/02-concepts/overview.md +93 -0
  19. package/docs/02-concepts/pipeline-steps/00-setup.md +102 -0
  20. package/docs/02-concepts/pipeline-steps/01-discovery.md +129 -0
  21. package/docs/02-concepts/pipeline-steps/02-specification.md +130 -0
  22. package/docs/02-concepts/pipeline-steps/03-design-spec.md +90 -0
  23. package/docs/02-concepts/pipeline-steps/04-bdd.md +120 -0
  24. package/docs/02-concepts/pipeline-steps/05-tech-docs.md +101 -0
  25. package/docs/02-concepts/pipeline-steps/06-code.md +119 -0
  26. package/docs/02-concepts/pipeline-steps/07-dev-selftest.md +92 -0
  27. package/docs/02-concepts/pipeline-steps/08-qc-automation.md +102 -0
  28. package/docs/02-concepts/pipeline-steps/09-validate-traces.md +104 -0
  29. package/docs/02-concepts/pipeline-steps/10-feedback-loop.md +105 -0
  30. package/docs/02-concepts/pipeline-steps/README.md +92 -0
  31. package/docs/02-concepts/roles-and-hitl.md +73 -0
  32. package/docs/02-concepts/traceability.md +94 -0
  33. package/docs/03-guides/architect.md +98 -0
  34. package/docs/03-guides/developer.md +76 -0
  35. package/docs/03-guides/product-owner.md +68 -0
  36. package/docs/03-guides/tester-qa.md +70 -0
  37. package/docs/04-reference/commands.md +105 -0
  38. package/docs/04-reference/configuration.md +94 -0
  39. package/docs/04-reference/model-selection.md +68 -0
  40. package/docs/04-reference/modules.md +74 -0
  41. package/docs/04-reference/trace-schema.md +93 -0
  42. package/docs/README.md +29 -40
  43. package/docs/explain/00-setup-ai-first.md +77 -0
  44. package/docs/explain/00b-generate-architecture.md +76 -0
  45. package/docs/explain/01-define-product.md +79 -0
  46. package/docs/explain/02-generate-prd.md +78 -0
  47. package/docs/explain/03-refine-prd.md +86 -0
  48. package/docs/explain/04-review-context.md +100 -0
  49. package/docs/explain/05-generate-design-spec.md +73 -0
  50. package/docs/explain/06-generate-bdd.md +77 -0
  51. package/docs/explain/07-generate-tech-docs.md +71 -0
  52. package/docs/explain/08-review-tech-docs.md +79 -0
  53. package/docs/explain/09-generate-code.md +78 -0
  54. package/docs/explain/10-review-code.md +70 -0
  55. package/docs/explain/11-map-testids.md +69 -0
  56. package/docs/explain/12-dev-gen-test.md +66 -0
  57. package/docs/explain/13-dev-run-test.md +69 -0
  58. package/docs/explain/14-dev-smoke-test.md +67 -0
  59. package/docs/explain/15-qc-analyze.md +68 -0
  60. package/docs/explain/16-qc-plan.md +61 -0
  61. package/docs/explain/17-qc-design-test.md +61 -0
  62. package/docs/explain/18-qc-review.md +59 -0
  63. package/docs/explain/19-qc-run-test.md +67 -0
  64. package/docs/explain/20-qc-report.md +61 -0
  65. package/docs/explain/21-validate-traces.md +68 -0
  66. package/docs/explain/22-generate-spec-manifest.md +60 -0
  67. package/docs/explain/23-fix-bug.md +69 -0
  68. package/docs/explain/24-debug.md +61 -0
  69. package/docs/explain/25-report-bug.md +65 -0
  70. package/docs/explain/26-propose-scenario.md +63 -0
  71. package/docs/explain/27-learn.md +65 -0
  72. package/docs/explain/28-sync.md +70 -0
  73. package/docs/explain/29-update-framework.md +65 -0
  74. package/docs/explain/README.md +134 -0
  75. package/package.json +1 -1
  76. package/skills/setup-ai-first/SKILL.md +12 -4
  77. package/skills/setup-ai-first/SKILL.tmpl +12 -4
  78. package/templates/architecture.template.md +392 -111
  79. package/docs/01-getting-started/README.md +0 -19
  80. package/docs/01-getting-started/core-concepts.md +0 -102
  81. package/docs/02-guides/README.md +0 -26
  82. package/docs/02-guides/bdd-input-checklist.md +0 -68
  83. package/docs/02-guides/developer/README.md +0 -49
  84. package/docs/02-guides/developer/bdd-and-trace.md +0 -126
  85. package/docs/02-guides/developer/commands.md +0 -76
  86. package/docs/02-guides/developer/pr-checklist.md +0 -16
  87. package/docs/02-guides/developer/scenarios.md +0 -460
  88. package/docs/02-guides/developer/workflow.md +0 -121
  89. package/docs/02-guides/prd-input-checklist.md +0 -94
  90. package/docs/02-guides/product-owner/README.md +0 -81
  91. package/docs/02-guides/product-owner/commands.md +0 -30
  92. package/docs/02-guides/product-owner/handoff-checklist.md +0 -42
  93. package/docs/02-guides/product-owner/prd-writing-rules.md +0 -45
  94. package/docs/02-guides/product-owner/scenarios.md +0 -438
  95. package/docs/02-guides/tech-docs-input-checklist.md +0 -109
  96. package/docs/02-guides/tester/README.md +0 -75
  97. package/docs/02-guides/tester/bug-reporting.md +0 -117
  98. package/docs/02-guides/tester/qc-automation.md +0 -165
  99. package/docs/02-guides/tester/reading-specs.md +0 -79
  100. package/docs/02-guides/tester/scenarios.md +0 -186
  101. package/docs/02-guides/tester/spec-manifest.md +0 -130
  102. package/docs/02-guides/tester/test-checklist.md +0 -31
  103. package/docs/02-guides/tester/workflow.md +0 -77
  104. package/docs/03-concepts/README.md +0 -20
  105. package/docs/03-concepts/architecture.md +0 -248
  106. package/docs/03-concepts/mechanisms-explained.md +0 -124
  107. package/docs/03-concepts/pipeline.md +0 -278
  108. package/docs/03-concepts/traceability.md +0 -152
  109. package/docs/04-operations/README.md +0 -33
  110. package/docs/04-operations/bug-flow.md +0 -364
  111. package/docs/04-operations/publishing.md +0 -154
  112. package/docs/04-operations/sync-and-update.md +0 -522
  113. package/docs/05-reference/README.md +0 -34
  114. package/docs/05-reference/command-cheatsheet.md +0 -147
  115. package/docs/05-reference/commands.md +0 -234
  116. package/docs/05-reference/model-selection.md +0 -74
  117. package/docs/05-reference/modules.md +0 -110
  118. package/docs/05-reference/trace-schema.md +0 -154
  119. package/docs/06-commands/README.md +0 -75
  120. package/docs/06-commands/explain-debug.md +0 -32
  121. package/docs/06-commands/explain-define-product.md +0 -43
  122. package/docs/06-commands/explain-dev-gen-test.md +0 -28
  123. package/docs/06-commands/explain-dev-run-test.md +0 -24
  124. package/docs/06-commands/explain-dev-smoke-test.md +0 -25
  125. package/docs/06-commands/explain-fix-bug.md +0 -28
  126. package/docs/06-commands/explain-generate-bdd.md +0 -45
  127. package/docs/06-commands/explain-generate-code.md +0 -53
  128. package/docs/06-commands/explain-generate-design-spec.md +0 -54
  129. package/docs/06-commands/explain-generate-prd.md +0 -45
  130. package/docs/06-commands/explain-generate-spec-manifest.md +0 -20
  131. package/docs/06-commands/explain-generate-tech-docs.md +0 -56
  132. package/docs/06-commands/explain-learn.md +0 -21
  133. package/docs/06-commands/explain-map-testids.md +0 -28
  134. package/docs/06-commands/explain-propose-scenario.md +0 -24
  135. package/docs/06-commands/explain-qc-analyze.md +0 -22
  136. package/docs/06-commands/explain-qc-design-test.md +0 -20
  137. package/docs/06-commands/explain-qc-plan.md +0 -21
  138. package/docs/06-commands/explain-qc-report.md +0 -23
  139. package/docs/06-commands/explain-qc-review.md +0 -24
  140. package/docs/06-commands/explain-qc-run-test.md +0 -27
  141. package/docs/06-commands/explain-refine-prd.md +0 -51
  142. package/docs/06-commands/explain-report-bug.md +0 -24
  143. package/docs/06-commands/explain-review-code.md +0 -45
  144. package/docs/06-commands/explain-review-context.md +0 -68
  145. package/docs/06-commands/explain-review-tech-docs.md +0 -45
  146. package/docs/06-commands/explain-setup-ai-first.md +0 -25
  147. package/docs/06-commands/explain-sync.md +0 -24
  148. package/docs/06-commands/explain-update-framework.md +0 -22
  149. package/docs/06-commands/explain-validate-traces.md +0 -25
  150. package/docs/t-sample.md +0 -826
@@ -443,6 +443,8 @@ Lệnh này giới hạn nghiêm ngặt trong **một file feature** được tr
443
443
  3. CLAUDE.md §architecture + §coding_standards
444
444
  4. **(chỉ FE/App)** Design Spec — nạp qua **Guard** bên dưới (gate approved/độ-tươi + sanity), là nguồn của màn hình, component inventory, và link Figma frame từng-màn.
445
445
 
446
+ > **Phạm vi vét nguồn (SRC-CHAIN):** khi một giá trị còn thiếu ở nguồn chính, được phép đọc thêm các artifact **cùng feature-package** `{paths.specs_dir}/{domain}/{prd-slug}/` — PRD `{TICKET-ID}-{prd-slug}.md`, các `.feature` khác (system/web/app), design-spec, tech-doc anh em — cùng `core-entities.md`/`business-dictionary.md`. Đọc **theo nhu cầu** để phân giải giá trị trước khi hỏi người (xem §Quy tắc nguồn giá trị).
447
+
446
448
  ---
447
449
 
448
450
  ## Guard — BDD & Design Spec đã sẵn sàng chưa *(cảnh báo MỀM — đồng bộ generate-bdd)*
@@ -775,7 +777,18 @@ DTOs → Entity/Model → Repository → Service interface → Service impl →
775
777
 
776
778
  > **Quy tắc entry-point:** `@trace.implements` phải xuất hiện ở **layer entry-point** như định nghĩa trong `CLAUDE.md §2`. Với REST API → Controller. Với module event-driven → event handler / consumer class. Với context-engineering → hàm orchestration prompt. Không bao giờ chỉ đặt ở layer trong.
777
779
 
778
- > **Quy tắc nguồn giá trị (chống hard-code):** MỌI giá trị cụ thể (endpoint path, error code, tên field/DTO, enum, limit/timeout, header) phải lấy từ **nguồn đã chốt** — tech-doc §4 (contract) · `core-entities.md` (enum/field) · config/env — **KHÔNG bịa inline**. Nếu một hằng số nghiệp vụ lặp lại hoặc mang ý nghĩa (retry count, ngưỡng, key) → **đặt tên hằng số** (constant/config), không rải magic number/string trong code. Không có nguồn cho một giá trị → đây là GAP: dừng và hỏi, đừng chế bừa (đồng bộ Cổng 2 của generate-tech-docs). *(DS3 đã đảm bảo có §4 contract trước khi tới đây với BE.)*
780
+ > **Quy tắc nguồn giá trị (chống hard-code):** MỌI giá trị cụ thể (endpoint path, error code, tên field/DTO, enum, limit/timeout, header) phải lấy từ **nguồn đã chốt** — **KHÔNG bịa inline**. Nếu một hằng số nghiệp vụ lặp lại hoặc mang ý nghĩa (retry count, ngưỡng, key) → **đặt tên hằng số** (constant/config), không rải magic number/string trong code.
781
+ >
782
+ > **VÉT CẠN NGUỒN TRƯỚC KHI HỎI (SRC-CHAIN) — bắt buộc.** Khi một giá trị chưa thấy ở nguồn chính, PHẢI quét lần lượt các nguồn đã có trong context/spec-package theo thứ tự sau, **dừng ngay khi tìm thấy** (skip-if-answered), KHÔNG hỏi người ngay:
783
+ > 1. Tech-doc gộp §4 (contract: §4.1 endpoint · §4.2 request/response · §4.3 error · §4.5.4 client integration)
784
+ > 2. `core-entities.md` (tên field / type / enum) · `business-dictionary.md` (thuật ngữ chuẩn)
785
+ > 3. PRD của UC (nhất là Appendix "Existing API Contract" khi `API Source: existing`, và metadata)
786
+ > 4. Design-spec (FE/App: field/label/state màn hình)
787
+ > 5. System/platform BDD — mệnh đề `Then` (behavior + giá trị fixture)
788
+ > 6. Code/adapter đã sinh ở lần chạy trước (vd mock adapter `--phase=ui` đã chốt shape port/DTO) · config/env
789
+ > 7. Tech-doc anh em cùng domain
790
+ >
791
+ > Chỉ giá trị **thật sự không nguồn nào có** mới là GAP. **Gom TẤT CẢ GAP còn lại vào MỘT checkpoint** (mỗi GAP ghi rõ "đã tìm ở: {các nguồn}"), hỏi một lượt — KHÔNG hỏi lắt nhắt từng câu, KHÔNG chế bừa (đồng bộ Cổng 2 của generate-tech-docs). *(DS3 đã đảm bảo có §4 contract trước khi tới đây với BE.)*
779
792
 
780
793
  ### Test Selectors — emit element ID ổn định *(chỉ UI FE/App)*
781
794
 
@@ -829,7 +842,8 @@ Dựng mock từ `mock_source` đã phân giải ở Phase Detection — **shape
829
842
  *Bỏ qua hoàn toàn section này nếu `--phase` không phải `integration`.*
830
843
 
831
844
  1. **Đọc integration design.** Trong tech-doc gộp `{paths.tech_docs_dir}/{domain}/{prd-slug}/tech-docs/{TICKET-ID}-tech-design.md`: ưu tiên §4.5.4 (mapping port→endpoint→DTO→error của platform), dùng §4.1/§4.2/§4.3 làm nguồn endpoint / request-response / error-code. Nếu doc chưa có §4.5.4 cho platform này, trích endpoint + shape + error code trực tiếp từ §4.1–§4.3.
832
- 2. **Đọc mock adapter sẵn** interface (`{UC-ID}ApiPort`) từ output `--phase=ui`.
845
+ - **Nếu §4/§4.5.4 thiếu bất kỳ chi tiết integration nào (endpoint, field/DTO, error-code, mapping):** ÁP DỤNG **SRC-CHAIN** (xem §Quy tắc nguồn giá trị) — vét cạn PRD (Appendix Existing API Contract), design-spec, mệnh đề `Then` của System/platform BDD, `core-entities.md`, **mock adapter đã sinh ở `--phase=ui`** (shape port/DTO đã chốt — nguồn shape mạnh nhất, đừng bỏ quên), tech-doc anh em cùng domain — TRƯỚC khi coi là GAP. Skip-if-answered. Chỉ hỏi cái không nguồn nào có, và **gộp mọi GAP còn lại vào MỘT checkpoint** (ghi rõ đã tìm ở đâu). *(Đây là fix cho tình trạng phase=integration hỏi nhiều dù đáp án đã nằm trong tài liệu khác.)*
846
+ 2. **Đọc mock adapter có sẵn** interface (`{UC-ID}ApiPort`) từ output `--phase=ui`. Real adapter implements **cùng** interface này → shape port/DTO đã cố định từ mock; **không hỏi lại shape** đã có ở đây.
833
847
  3. **Sinh real API adapter** tại `{paths.src_dir}/{domain}/{UC-ID}ApiAdapter.{ext}`:
834
848
  - Implements cùng interface `{UC-ID}ApiPort` như mock adapter
835
849
  - Gọi HTTP thật tới endpoint từ contract tech-doc
@@ -463,6 +463,25 @@ Lưu `input_features`, `platforms_present`, danh sách scenario theo từng UC,
463
463
 
464
464
  ---
465
465
 
466
+ ## Bước 0.5 — [ARCH] Nạp Architecture Context (nếu có)
467
+
468
+ Tech-design chắt lọc kiến trúc hệ thống thành API contract + client design, nên đây là **nơi duy nhất** trong pipeline nạp `architecture.md` (SSOT cross-cutting sinh bởi `/generate-architecture`). Không nạp toàn cục ở context-loader — chỉ lệnh này cần nó ở mức sâu.
469
+
470
+ 1. **Phân giải path:** `{paths.specs_dir}/architecture.md` (mặc định `specs/architecture.md`). Chế độ umbrella: context-loader đã trỏ `specs_dir`/`service_root` về service đang active → dùng `architecture.md` của chính service đó (kiến trúc là code-level, per-service).
471
+ 2. **Nếu file KHÔNG tồn tại** → bỏ qua âm thầm, đặt `arch = none`. Vẫn dựa vào CLAUDE.md §2 (layers/rules) + core-entities như trước. (Gợi ý mềm một lần trong report cuối: "Chưa có architecture.md — cân nhắc chạy `/generate-architecture` để tech-design bám kiến trúc hệ thống.")
472
+ 3. **Nếu tồn tại** → đọc **có chọn lọc theo tier** (mỗi mục có marker `<!-- tier: core|conditional|ops -->`):
473
+ - **Nạp** thân các mục **`core` + `conditional`** — đây là phần ảnh hưởng code/API/data: layer boundaries + dependency direction, phân loại/nguồn dữ liệu, injection rules, luồng xác thực, response API chuẩn, caching/sharding/messaging, multi-tenant/identity-resolution.
474
+ - **BỎ QUA** các mục **`ops`** (Observability, Triển khai & DevOps, Chiến lược kiểm thử, NFR) — không đổi thiết kế API/data-model, chỉ làm nhiễu context.
475
+ - Các section §2–§4 (data model, API contract, integration) PHẢI nhất quán với phần đã nạp — KHÔNG tự suy khác.
476
+ 4. **Mục đang STUB (`<!-- status: stub -->`):** nếu một UC trong batch **chạm** tới một concern mà mục tương ứng còn stub (vd UC có tenant scoping nhưng §Multi-tenant là stub) → **cảnh báo mềm** trong report: *"§{Mục} chưa tài liệu hoá trong architecture.md — chạy `/generate-architecture --section={slug}` để tech-design chính xác hơn."* Vẫn tiếp tục dựa vào CLAUDE.md.
477
+ 5. **Trust-gate — đọc frontmatter `verified_by`:**
478
+ - `verified_by: AI-draft` (hoặc trống) → nội dung do AI dựng từ config/tài liệu/code, CHƯA ai verify. Vẫn dùng làm tham chiếu nhưng **cảnh báo** trong report: *"⚠️ architecture.md còn là AI-draft chưa verify — tech-design có thể kế thừa giả định sai. Nên để Tech Lead verify (đổi `verified_by`) trước khi chốt."* Khi mâu thuẫn với CLAUDE.md/BDD thì ưu tiên CLAUDE.md/BDD.
479
+ - `verified_by: {người thật}` → coi là ràng buộc kiến trúc chính thức.
480
+
481
+ Lưu `arch` (`none` | `ai-draft` | `verified`) để dùng ở các bước sinh section và report.
482
+
483
+ ---
484
+
466
485
  ## Bước 1 — Chế độ Fresh vs Append
467
486
 
468
487
  Kiểm tra `output_path` đã tồn tại chưa.
@@ -1,11 +1,19 @@
1
1
  ---
2
- description: Cài đặt framework Spec-Driven Development trong bất kỳ backend project nào từ đầu. Trigger when: "/setup-ai-first", "setup spec-driven workflow", "initialize ai-first framework", "cài đặt framework", "khởi tạo spec-driven", "set up this workflow", "how do I start using this framework".
2
+ description: Cài đặt framework Spec-Driven Development, sinh/làm mới tài liệu kiến trúc (architecture.md SSOT). Trigger when: "/setup-ai-first", "/generate-architecture", "setup spec-driven workflow", "initialize ai-first framework", "cài đặt framework", "khởi tạo spec-driven", "set up this workflow", "how do I start using this framework", "sinh architecture", "tạo tài liệu kiến trúc", "generate architecture doc", "refresh architecture", "scan codebase architecture".
3
3
  ---
4
4
 
5
- # /setup-ai-first — Initialize SDD Framework in a Project
5
+ # /setup-ai-first · /generate-architecture Foundation Skills (SA / Tech Lead)
6
6
 
7
- Skill này xử lý `/setup-ai-first`. Để **không lệch** (umbrella/services routing, cấu trúc), skill KHÔNG nhân bản — thực thi **y hệt** command.
7
+ Skill này xử lý `/setup-ai-first` và `/generate-architecture`. Để **không lệch** (umbrella/services routing, cấu trúc, trust-gate), skill KHÔNG nhân bản — mỗi lệnh thực thi **y hệt** command tương ứng.
8
+
9
+ ## /setup-ai-first — Initialize SDD Framework in a Project
8
10
 
9
11
  → **Đọc và tuân theo `commands/setup-ai-first.md`** với cùng `$ARGUMENTS`.
10
12
 
11
- Command lo: tạo cấu trúc thư mục + feature-package on demand · CLAUDE.md + `.agent/project-context.yaml` (gồm umbrella `services` routing) · nhắc PO đặt row `Domain` (bảng Metadata) khớp services config · verify môi trường.
13
+ Command lo: tạo cấu trúc thư mục + feature-package on demand · CLAUDE.md + `.agent/project-context.yaml` (gồm umbrella `services` routing) · seed `architecture.md` (Step 3.5) · nhắc PO đặt row `Domain` (bảng Metadata) khớp services config · verify môi trường.
14
+
15
+ ## /generate-architecture — Sinh / làm mới Architecture Context (SSOT)
16
+
17
+ → **Đọc và tuân theo `commands/generate-architecture.md`** với cùng `$ARGUMENTS`.
18
+
19
+ Command lo: phân giải target (single-service / per-service umbrella) · phát hiện greenfield vs brownfield · **scan codebase** (build-file/DI/middleware/config/CI) → draft từng section kèm bằng chứng · xoá section [OPTIONAL] không dùng · **trust-gate** `verified_by: AI-draft` · refresh có kiểm soát (không đè bản người đã verify) · bàn giao cho người verify.
@@ -1,113 +1,394 @@
1
- # §1. Project Overview
1
+ ---
2
+ last_verified: {{YYYY-MM-DD}}
3
+ verified_by: {{AUTHOR}}
4
+ ---
2
5
 
3
- Project : {{PROJECT_NAME}}
4
- Language : {{LANGUAGE}} # vd: Java 17 / TypeScript / C# / Go
5
- Framework : {{FRAMEWORK}} # vd: Spring Boot 3.2 / Angular 17 / .NET 8
6
- Build : {{BUILD_COMMAND}} # vd: mvn clean install -DskipTests / dotnet build / ng build
7
- Test : {{TEST_COMMAND}} # vd: mvn test / dotnet test / ng test
8
- Domains : {{COMMA_SEPARATED_DOMAINS}}
9
-
10
- # §2. Architecture
6
+ # {{SYSTEM_NAME}} — Bối cảnh Kiến trúc (Architecture Context)
11
7
 
12
- style: "{{ARCH_STYLE}}" # vd: Layered / Clean / Hexagonal / Component-based
13
-
14
- layers: "{{LAYER_STACK}}"
15
- # dụ:
16
- # Java/Spring: Controller Facade Service Repository
17
- # .NET Clean: Presentation → Application → Domain → Infrastructure
18
- # Angular: Component Service HTTP Client Backend API
19
- # Go: Handler UseCase Repository Domain
20
-
21
- rules:
22
- - "{{ARCH_RULE_1}}" # vd: Controller không được chứa business logic
23
- - "{{ARCH_RULE_2}}" # vd: Service sở hữu ranh giới transaction
24
- - "{{ARCH_RULE_3}}" # vd: Repository không được gọi service
25
-
26
- # Chiều phụ thuộc giữa các layer (layer trong không được phụ thuộc layer ngoài):
27
- # {{OUTER_LAYER}} → {{MIDDLE_LAYER}} → {{INNER_LAYER}}
28
-
29
- # §3. Coding Standards
30
-
31
- naming:
32
- classes: "{{CLASS_NAMING}}" # vd: PascalCase / PascalCase+Suffix
33
- methods: "{{METHOD_NAMING}}" # vd: camelCase / PascalCase
34
- packages: "{{PACKAGE_NAMING}}" # vd: lowercase / lowercase.snake_case
35
- files: "{{FILE_NAMING}}" # vd: PascalCase.java / kebab-case.ts
36
-
37
- patterns:
38
- response_wrapper: "{{RESPONSE_WRAPPER}}" # vd: ApiResponse<T> / Result<T> / IActionResult
39
- mapping: "{{MAPPING_LIBRARY}}" # vd: MapStruct / AutoMapper / manual
40
- exception_base: "{{BASE_EXCEPTION}}" # vd: ResourceNotFoundException / DomainException
41
-
42
- forbidden:
43
- - "Magic number dùng hằng số tên"
44
- - "Lệnh debug print trong code production"
45
- - "{{PROJECT_SPECIFIC_FORBIDDEN_PATTERN}}"
46
-
47
- # §4. API Conventions
48
-
49
- versioning: "{{API_VERSIONING}}" # vd: tiền tố /v1/ / theo header / query param
50
- auth: "{{AUTH_MECHANISM}}" # vd: JWT Bearer / OAuth2 / API Key
51
-
52
- http_status:
53
- get_list: 200 # danh sách phân trang hoặc đầy đủ
54
- get_single: 200
55
- create: 201
56
- update: 200
57
- delete: 204
58
- bad_request: 400
59
- unauthorized: 401
60
- forbidden: 403
61
- not_found: 404
62
- server_error: 500
63
-
64
- error_response_format: |
65
- {
66
- "code": "ERROR_CODE",
67
- "message": "Human readable message",
68
- "details": {} // optional field-level errors
69
- }
70
-
71
- # §5. Error Handling
72
-
73
- not_found_exception: "{{NOT_FOUND_EXCEPTION_CLASS}}" # vd: ResourceNotFoundException
74
- validation_exception: "{{VALIDATION_EXCEPTION_CLASS}}" # vd: ValidationException
75
- domain_exception: "{{DOMAIN_EXCEPTION_CLASS}}" # vd: DomainException / BusinessRuleViolationException
76
-
77
- global_handler: "{{GLOBAL_EXCEPTION_HANDLER}}" # vd: @ControllerAdvice / ExceptionHandlerMiddleware
78
-
79
- rules:
80
- - "Không bao giờ nuốt exception âm thầm"
81
- - "Log mức error kèm full stack trace cho lỗi 5xx"
82
- - "{{PROJECT_SPECIFIC_ERROR_RULE}}"
83
-
84
- # §6. Testing Standards
85
-
86
- unit_test_framework: "{{UNIT_TEST_FW}}" # vd: JUnit 5 + Mockito / xUnit + Moq / Jest
87
- integration_test_framework: "{{IT_TEST_FW}}" # vd: Spring Boot Test / WebApplicationFactory
88
-
89
- coverage_targets:
90
- unit: "{{UNIT_COVERAGE_PCT}}%" # vd: 80%
91
- integration: "{{IT_COVERAGE_PCT}}%" # vd: phủ các flow chính
92
-
93
- naming_pattern: "{{TEST_METHOD_NAMING}}" # vd: methodName_whenCondition_shouldExpectation
94
-
95
- rules:
96
- - "Unit test chỉ mock các dependency trực tiếp"
97
- - "Integration test phủ happy path + các luồng lỗi chính"
98
- - "Mọi scenario trong .feature phải có test tương ứng"
99
-
100
- # §7. Git Conventions
101
-
102
- branch_feature: "feature/{{TICKET_PREFIX}}-{N}-{slug}"
103
- branch_fix: "fix/{{TICKET_PREFIX}}-{N}-{slug}"
104
- branch_chore: "chore/{slug}"
105
-
106
- commit_feature: "feat({{TICKET_PREFIX}}-{N}): {description}"
107
- commit_fix: "fix({{TICKET_PREFIX}}-{N}): {description}"
108
- commit_chore: "chore: {description}"
109
- commit_docs: "docs: {description}"
110
-
111
- pr_title: "{{TICKET_PREFIX}}-{N}: {feature name}"
112
- pr_requires_review: true
113
- pr_branch_protection: "{{BASE_BRANCH}}" # vd: main / develop
8
+ > Nguồn chân duy nhất (single source of truth) về kiến trúc hệ thống — phục vụ cả bối cảnh sinh code (AI agents) lẫn các vấn đề vận hành (triển khai, CI/CD, NFR).
9
+ >
10
+ > HƯỚNG DẪN DÙNG TEMPLATE:
11
+ > - Điền các `{{PLACEHOLDER}}`, xoá comment hướng dẫn (`<!-- ... -->`) sau khi điền.
12
+ > - Không cần điền tay: nên chạy `/generate-architecture` để tự hút từ config/tài liệu/code + phỏng vấn, rồi con người verify.
13
+ >
14
+ > PHÂN TẦNG (tier) mỗi mục marker `<!-- tier: core|conditional|ops -->`:
15
+ > - **core** luôn điền. Kiến trúc nền mà mọi feature cần.
16
+ > - **conditional** — chỉ giữ nếu hệ thống thực sự có (multi-tenant, message bus, sharding…). Không dùng → để dạng STUB hoặc xoá hẳn.
17
+ > - **ops** — vận hành, chỉ để người đọc. AI **KHÔNG** nạp khi sinh code/tech-design.
18
+ >
19
+ > ĐIỀN DẦN: không phải điền hết một lần. Mục chưa làm để dạng STUB rồi lấp sau bằng `/generate-architecture --section=<slug>`:
20
+ > ```
21
+ > ## Multi-tenant <!-- tier: conditional --> <!-- status: stub -->
22
+ > > Chưa tài liệu hoá. Chạy `/generate-architecture --section=multi-tenant` khi cần, hoặc điền tay.
23
+ > ```
24
+
25
+ <!-- ════════════════════ CORE (luôn điền) ════════════════════ -->
26
+
27
+ ## Công nghệ sử dụng (Tech Stack) <!-- tier: core -->
28
+
29
+ | Tầng | Công nghệ | Phiên bản / Ghi chú |
30
+ |---|---|---|
31
+ | Backend | {{BACKEND_TECH}} | {{VERSION}} |
32
+ | Frontend | {{FRONTEND_TECH}} | {{VERSION}} |
33
+ | ORM / Truy cập dữ liệu | {{ORM}} | {{VERSION}} |
34
+ | Database | {{DATABASE}} | {{PRIMARY_STORE_NOTE}} |
35
+ | Caching | {{CACHE}} | {{CACHE_NOTE}} |
36
+ | Event Bus / Messaging | {{MESSAGING}} | {{MESSAGING_NOTE}} |
37
+ | API Gateway | {{GATEWAY}} | {{GATEWAY_NOTE}} |
38
+ | Kiểm thử | {{TEST_STACK}} | {{TEST_NOTE}} |
39
+ <!-- Thêm/bớt dòng theo thực tế. Cột Ghi chú nên ghi ràng buộc cụ thể AI cần biết (vd: "dùng cho mọi màn hình list", "thư viện private wrapper"). -->
40
+
41
+ ## Các tầng kiến trúc (Architecture Layers) <!-- tier: core -->
42
+
43
+ Kiểu kiến trúc: `{{ARCH_STYLE}}` <!-- vd: Layered / Clean / Hexagonal / Component-based -->
44
+
45
+ ```
46
+ {{LAYER_DIAGRAM}}
47
+ ```
48
+ <!-- Ví dụ Clean Architecture:
49
+ ┌──────────────────────────────────────┐
50
+ Presentation │ Controllers, Middleware
51
+ ├──────────────────────────────────────┤
52
+ Application │ Use Cases, DTOs, Interfaces
53
+ ├──────────────────────────────────────┤
54
+ Domain │ Entities, Value Objects, Events
55
+ ├──────────────────────────────────────┤
56
+ Infrastructure │ ORM, Cache, Messaging
57
+ └──────────────────────────────────────┘
58
+ -->
59
+
60
+ ### Chiều phụ thuộc
61
+ - {{OUTER}} → {{MIDDLE}} → {{INNER}} ← {{INFRASTRUCTURE}}
62
+ - {{INNER_LAYER}} có **zero** phụ thuộc ngoài.
63
+ - Infrastructure hiện thực các interface do Application/Domain định nghĩa.
64
+ - Tầng trong KHÔNG được phụ thuộc tầng ngoài.
65
+
66
+ ## Quy ước đặt tên <!-- tier: core -->
67
+
68
+ | Thành phần | Quy ước | Ví dụ |
69
+ |---|---|---|
70
+ | Entity | {{ENTITY_CONV}} | {{EXAMPLE}} |
71
+ | Value Object | {{VO_CONV}} | {{EXAMPLE}} |
72
+ | Interface | {{INTERFACE_CONV}} | {{EXAMPLE}} |
73
+ | Use Case Command | {{COMMAND_CONV}} | {{EXAMPLE}} |
74
+ | Use Case Query | {{QUERY_CONV}} | {{EXAMPLE}} |
75
+ | Handler | {{HANDLER_CONV}} | {{EXAMPLE}} |
76
+ | DTO | {{DTO_CONV}} | {{EXAMPLE}} |
77
+ | Controller | {{CONTROLLER_CONV}} | {{EXAMPLE}} |
78
+ | DI Extension | {{DI_EXT_CONV}} | {{EXAMPLE}} |
79
+ | Class test | {{TEST_CLASS_CONV}} | {{EXAMPLE}} |
80
+ | Method test | {{TEST_METHOD_CONV}} | {{EXAMPLE}} |
81
+
82
+ > Quy ước đặt tên của frontend / từng repo xem `.ai-project-guide.md` tương ứng.
83
+
84
+ ## Định dạng response API chuẩn <!-- tier: core -->
85
+
86
+ ```{{LANG}}
87
+ // Thành công
88
+ {{SUCCESS_WRAPPER}}
89
+ {{SUCCESS_JSON_SHAPE}}
90
+
91
+ // Thất bại
92
+ {{FAILURE_WRAPPER}}
93
+ {{FAILURE_JSON_SHAPE}}
94
+ ```
95
+
96
+ ## Quy ước API <!-- tier: core -->
97
+
98
+ | Khía cạnh | Quy ước |
99
+ |---|---|
100
+ | Base URL | {{BASE_URL}} |
101
+ | Versioning | {{VERSIONING}} <!-- vd: URL path /v1/, theo header, query param --> |
102
+ | Đặt tên URL | {{URL_NAMING}} <!-- vd: kebab-case --> |
103
+ | Phân trang | {{PAGINATION}} <!-- vd: ?page=1&pageSize=20 → meta{...} --> |
104
+ | Lọc | {{FILTERING}} |
105
+ | Sắp xếp | {{SORTING}} |
106
+ | Định dạng ngày | {{DATE_FORMAT}} <!-- vd: ISO 8601 --> |
107
+ | Ngừng hỗ trợ (Deprecation) | {{DEPRECATION_POLICY}} |
108
+
109
+ ### Các endpoint chính theo service <!-- [TUỲ CHỌN] -->
110
+
111
+ | Service | Endpoints |
112
+ |---|---|
113
+ | {{SERVICE}} | {{ENDPOINTS}} |
114
+
115
+ > Tài liệu API đầy đủ: {{API_DOCS_LOCATION}} <!-- vd: /swagger cho mỗi service -->
116
+
117
+ ## Quy ước Database <!-- tier: core -->
118
+
119
+ - **Migration**: {{MIGRATION_STRATEGY}} <!-- vd: code-first mỗi service -->
120
+ - **Đặt tên**: {{DB_NAMING}} <!-- vd: PascalCase cho entity, snake_case cho cột -->
121
+ - **Soft Delete**: {{SOFT_DELETE_RULE}}
122
+ - **Cột audit**: {{AUDIT_COLUMNS}} <!-- vd: CreatedAt NOT NULL, ModifiedAt nullable -->
123
+ - **Xử lý đồng thời**: {{CONCURRENCY_MECHANISM}} <!-- vd: ROWVERSION / optimistic lock -->
124
+ - **Quy ước ID**: {{PK_TYPE}} <!-- vd: bigint (không dùng Guid) -->
125
+
126
+ ### Quy ước Migration <!-- [TUỲ CHỌN] khi dùng SQL migration thủ công -->
127
+ - **Vị trí**: {{MIGRATION_LOCATION}}
128
+ - **Đặt tên**: {{MIGRATION_NAMING}} <!-- vd: V{YYYYMMDD}_{NN}_{description}.sql -->
129
+ - **Tính idempotent**: {{IDEMPOTENCY_RULE}} <!-- vd: dùng guard IF NOT EXISTS -->
130
+
131
+ <!-- ════════════════════ CONDITIONAL (chỉ giữ nếu hệ thống có) ════════════════════ -->
132
+
133
+ ## Luồng dữ liệu tổng quan <!-- tier: conditional -->
134
+
135
+ <!-- Mô tả 1-2 câu bản chất luồng dữ liệu của hệ thống (vd: "hệ tổng hợp dữ liệu: master data ở hệ ngoài, config/transaction ở DB local, gộp tại runtime"). -->
136
+ {{DATA_FLOW_SUMMARY}}
137
+
138
+ ```
139
+ {{DATA_FLOW_DIAGRAM}}
140
+ ```
141
+ <!-- Vẽ ASCII: Clients → Gateway → Services → điểm composition ở Application → Infrastructure (API ngoài / DB / cache / bus).
142
+ Ghi rõ phương thức auth của từng client và các điểm composition quan trọng. -->
143
+
144
+ ### Phân loại nguồn dữ liệu <!-- [TUỲ CHỌN] khi dữ liệu đến từ nhiều nguồn (API ngoài + DB local) -->
145
+
146
+ | Dữ liệu | Nguồn | Cách truy cập | Caching |
147
+ |---|---|---|---|
148
+ | {{DATA}} | {{SOURCE}} | {{PATH}} | {{TTL_OR_DASH}} |
149
+
150
+ ### Giao tiếp giữa các service <!-- [TUỲ CHỌN] khi multi-service -->
151
+
152
+ ```
153
+ {{SERVICE_A}} ──{{PROTOCOL}}──▶ {{SERVICE_B}} ({{VIA_CLIENT}})
154
+ ```
155
+
156
+ > **Endpoint nội bộ** ({{INTERNAL_ROUTE_PREFIX}}): {{INTERNAL_AUTH_MECHANISM}} — vd: header shared-secret + IP whitelist. Xem §Luồng Xác thực.
157
+
158
+ ## Repositories & Hướng dẫn nền tảng <!-- tier: conditional --> <!-- chỉ giữ nếu là hệ multi-repo -->
159
+
160
+ | Repository | Stack | Platform Guide |
161
+ |---|---|---|
162
+ | `{{repo-name}}/` | {{STACK_SUMMARY}} | `{{repo-name}}/.ai-project-guide.md` |
163
+
164
+ > **Cấu trúc thư mục, scan path, quy ước namespace, code pattern** → xem `.ai-project-guide.md` của từng repo.
165
+ > File này tập trung vào **kiến trúc cắt ngang** dùng chung cho mọi repo.
166
+
167
+ ## Phân loại Entity <!-- tier: conditional --> <!-- khi hệ thống tích hợp dữ liệu từ hệ ngoài -->
168
+
169
+ Entity được chia thành các nhóm sau. Mọi code, tài liệu, và BDD spec PHẢI dùng các thuật ngữ này nhất quán.
170
+
171
+ | Nhóm | Thuật ngữ | Lưu ở DB? | ORM quản lý? | Truy cập qua | Ví dụ |
172
+ |---|---|---|---|---|---|
173
+ | **DB Entity** | `[entity] entity` | CÓ | CÓ | {{DB_REPO_INTERFACE}} | {{EXAMPLES}} |
174
+ | **Model nguồn-API** | `[entity] model` | KHÔNG | KHÔNG — POCO/DTO | {{API_SERVICE_INTERFACE}} | {{EXAMPLES}} |
175
+ | **Projection Entity** | `[entity] projection` | CÓ | CÓ | {{PROJECTION_REPO}} | {{EXAMPLES}} |
176
+
177
+ **Quy tắc:**
178
+ - Model nguồn-API KHÔNG có bảng DB, KHÔNG migration — là POCO/DTO được populate từ API ngoài.
179
+ - {{ID_RULE}} <!-- vd: Model nguồn-API Id = external ID; DB entity Id = tự sinh -->
180
+ - {{PROJECT_SPECIFIC_RULE}}
181
+
182
+ ## Phân loại truy cập dữ liệu — Ranh giới các tầng <!-- tier: conditional --> <!-- khi gộp dữ liệu nhiều nguồn (API ngoài + DB) -->
183
+
184
+ Cách các tầng truy cập dữ liệu và ranh giới giữa chúng:
185
+
186
+ ```
187
+ {{ACCESS_TREE}}
188
+ ```
189
+ <!-- Ví dụ:
190
+ Handler / Controller
191
+ ├── IProductCatalogService (Application — điểm gộp DUY NHẤT)
192
+ │ ├── IProductService (Infrastructure — API ngoài)
193
+ │ └── IDisplayConfigRepository (Infrastructure — DB)
194
+ └── IOrderRepository (Infrastructure — DB, inject trực tiếp)
195
+ -->
196
+
197
+ | Tầng | Mục đích | Sở hữu | Ví dụ |
198
+ |---|---|---|---|
199
+ | **{{API_SERVICE_LAYER}}** (Infrastructure) | Gọi API ngoài; tự quản cache/retry/circuit-breaker/mapping | Model nguồn-API | {{EXAMPLES}} |
200
+ | **{{DB_REPO_LAYER}}** (Infrastructure) | Thao tác DB local; repository pattern chuẩn | DB entity | {{EXAMPLES}} |
201
+ | **{{APP_SERVICE_LAYER}}** (Application) | Gộp dữ liệu nhiều nguồn; điểm composition duy nhất | Domain object đã enrich | {{EXAMPLES}} |
202
+ | **{{HANDLER_LAYER}}** (Presentation/Application) | Điều phối use-case; mapping request→response | — | {{EXAMPLES}} |
203
+
204
+ **Quy tắc Injection (BẮT BUỘC tuân theo):**
205
+ 1. {{RULE_1}} <!-- vd: Service gọi API ngoài chỉ inject vào Application Service, KHÔNG inject thẳng vào Handler -->
206
+ 2. {{RULE_2}}
207
+ 3. {{RULE_3}}
208
+
209
+ ## Trách nhiệm của từng service <!-- tier: conditional --> <!-- khi multi-service -->
210
+
211
+ | Service | Domain | Trách nhiệm chính |
212
+ |---|---|---|
213
+ | {{SERVICE}} | {{DOMAIN}} | {{RESPONSIBILITIES}} |
214
+
215
+ ## Mô hình đăng ký DI <!-- tier: conditional -->
216
+
217
+ Mỗi feature đăng ký dependency qua extension method / module:
218
+ ```{{LANG}}
219
+ {{DI_REGISTRATION_EXAMPLE}}
220
+ ```
221
+
222
+ ## Mô hình Multi-tenant <!-- tier: conditional --> <!-- khi một hệ thống phục vụ nhiều khách hàng/chi nhánh, dữ liệu tách riêng -->
223
+
224
+ Mọi entity (trừ {{GLOBAL_ENTITIES}}) PHẢI có `{{TENANT_KEY}}`. {{ISOLATION_MECHANISM}} đảm bảo cách ly:
225
+ ```{{LANG}}
226
+ {{QUERY_FILTER_EXAMPLE}}
227
+ ```
228
+
229
+ Truy cập tenant context qua: `{{TENANT_CONTEXT_ACCESSOR}}` <!-- các field: TenantId, AppId, UserId... -->
230
+
231
+ ## Phân giải định danh — External ID vs Internal ID <!-- tier: conditional --> <!-- khi tích hợp hệ ngoài có ID riêng -->
232
+
233
+ Hệ thống tích hợp với {{EXTERNAL_SYSTEM}}. Mỗi entity ngoài có ID riêng, **KHÔNG trùng** với internal ID. Hai hệ ID này phải luôn được phân biệt rõ.
234
+
235
+ | | DB Entity (local) | Model nguồn-API (ngoài) |
236
+ |---|---|---|
237
+ | **`Id`** | {{INTERNAL_ID_RULE}} (tự sinh) | Gán từ response hệ ngoài — KHÔNG tự sinh |
238
+ | **`ExternalId`** | Lưu ID hệ ngoài để tham chiếu chéo | Không áp dụng — `Id` chính là external ID |
239
+
240
+ ### Quy tắc bắt buộc
241
+ 1. {{RULE_1}} <!-- vd: DB Entity Id luôn tự sinh, TUYỆT ĐỐI KHÔNG gán external ID vào Id -->
242
+ 2. {{RULE_2}}
243
+ 3. {{RULE_3}}
244
+
245
+ ### Anti-pattern (CẤM)
246
+ ```{{LANG}}
247
+ // ❌ SAI — {{ANTIPATTERN_DESC}}
248
+ {{BAD_EXAMPLE}}
249
+
250
+ // ✅ ĐÚNG
251
+ {{GOOD_EXAMPLE}}
252
+ ```
253
+
254
+ ## API Gateway <!-- tier: conditional -->
255
+
256
+ | Trách nhiệm | Chi tiết |
257
+ |---|---|
258
+ | Xác minh chữ ký / auth | {{DETAIL}} |
259
+ | Rate limiting | {{DETAIL}} |
260
+ | Xử lý CORS | {{DETAIL}} |
261
+ | Định tuyến request | {{DETAIL}} |
262
+ | Cân bằng tải | {{DETAIL}} |
263
+ | IP whitelist | {{DETAIL}} |
264
+
265
+ **Nguyên tắc thiết kế:** Stateless, không chứa business logic, scale ngang được.
266
+
267
+ ## Chiến lược Caching <!-- tier: conditional -->
268
+
269
+ **Mô hình**: {{CACHE_PATTERN}} <!-- vd: Cache-Aside (read-through) -->
270
+ **Định dạng key**: {{KEY_FORMAT}} <!-- vd: {entity}:{tenantId}:{id} -->
271
+
272
+ | Loại dữ liệu | TTL | Cách invalidate |
273
+ |---|---|---|
274
+ | {{DATA}} | {{TTL}} | {{INVALIDATION}} |
275
+
276
+ ## Event Bus <!-- tier: conditional -->
277
+
278
+ > **Trạng thái:** {{STATUS}} <!-- vd: đã cấp hạ tầng nhưng chưa implement / đã chạy production -->
279
+
280
+ **Nguyên tắc thiết kế:**
281
+ - {{PARTITIONING}} <!-- vd: partition theo TenantId để đảm bảo thứ tự trong mỗi tenant -->
282
+ - {{DELIVERY_GUARANTEE}} <!-- vd: at-least-once + theo dõi idempotency -->
283
+ - {{DLQ_RETRY}} <!-- vd: DLQ {topic}.dlq, retry 3 lần với exponential backoff -->
284
+
285
+ ## Sharding <!-- tier: conditional -->
286
+
287
+ - {{SHARD_STRATEGY}} <!-- vd: mô hình Shard Registry + cache -->
288
+ - {{SHARD_RESOLVER}} <!-- vd: IShardResolver: cache → Registry DB → tự gán -->
289
+
290
+ ## Feature Toggle <!-- tier: conditional -->
291
+
292
+ Hệ thống dùng {{FEATURE_TOGGLE_TOOL}} cho feature flag lúc runtime.
293
+
294
+ | Điều kiện | Hành vi |
295
+ |---|---|
296
+ | Toggle bị tắt | {{BEHAVIOR}} <!-- thường: fail-open / cho phép -->|
297
+ | Lỗi khi đánh giá | {{BEHAVIOR}} <!-- thường: fail-open để sự cố không khoá user -->|
298
+ | Flag trả về false | {{BEHAVIOR}} |
299
+
300
+ ## Luồng Xác thực <!-- tier: conditional -->
301
+
302
+ ### {{AUTH_METHOD}} <!-- vd: JWT Bearer / API Key (HMAC) / OAuth2 / Token Exchange -->
303
+
304
+ ```
305
+ {{AUTH_FLOW_STEPS}}
306
+ ```
307
+ <!-- Liệt kê tuần tự các bước middleware xử lý: extract → verify → resolve → build context.
308
+ Nếu có nhiều luồng auth (S2S, browser, internal), mô tả từng luồng. -->
309
+
310
+ **Quy tắc chính:**
311
+ - {{AUTH_RULE_1}}
312
+ - {{AUTH_RULE_2}}
313
+
314
+ **Middleware pipeline:**
315
+ ```
316
+ {{ROUTE_PATTERN}} → {{MIDDLEWARE_OR_BYPASS}}
317
+ {{HEALTH_ROUTE}} → bypass (health probe)
318
+ ```
319
+
320
+ <!-- ════════════════════ OPS (chỉ người đọc — AI không nạp khi sinh code) ════════════════════ -->
321
+
322
+ ## Observability (Khả năng quan sát) <!-- tier: ops -->
323
+
324
+ ### Ghi log (Logging)
325
+ - Thư viện: {{LOGGING_LIB}}
326
+ - Định dạng: {{LOG_FORMAT}} <!-- vd: Compact JSON -->
327
+ - Sink: {{LOG_SINKS}} <!-- vd: file xoay vòng theo ngày, giữ 30 ngày; console chỉ khi dev -->
328
+ - **Correlation ID**: {{CORRELATION_MECHANISM}} <!-- vd: X-Correlation-Id đẩy vào log context -->
329
+ - Mức log: {{LOG_LEVELS}}
330
+
331
+ ### Metrics <!-- [TUỲ CHỌN] -->
332
+
333
+ | Metric | Loại | Mô tả |
334
+ |---|---|---|
335
+ | {{METRIC}} | {{TYPE}} | {{DESC}} |
336
+
337
+ ### Distributed Tracing <!-- [TUỲ CHỌN] -->
338
+ - {{TRACING_TOOL}} — {{PROPAGATION_SCOPE}} <!-- vd: OpenTelemetry, propagate qua HTTP + messaging -->
339
+
340
+ ### Health Check
341
+ ```{{LANG}}
342
+ {{HEALTH_CHECK_REGISTRATION}}
343
+ ```
344
+ - `{{LIVENESS_ROUTE}}` — liveness (chỉ tự kiểm tra)
345
+ - `{{READINESS_ROUTE}}` — readiness (phụ thuộc: DB + cache + bus)
346
+
347
+ ## Triển khai & DevOps <!-- tier: ops -->
348
+
349
+ ### Chiến lược môi trường
350
+
351
+ | Môi trường | Mục đích | Hạ tầng |
352
+ |---|---|---|
353
+ | Development | {{PURPOSE}} | {{INFRA}} |
354
+ | Staging | {{PURPOSE}} | {{INFRA}} |
355
+ | Production | {{PURPOSE}} | {{INFRA}} |
356
+
357
+ ### CI/CD Pipeline
358
+ ```
359
+ {{PIPELINE_STAGES}}
360
+ ```
361
+ <!-- vd: Code Push → Build → Unit Test → Integration Test → Docker Build → Push Registry → Deploy Staging → Smoke → Duyệt tay → Production -->
362
+
363
+ ### Quản lý cấu hình
364
+
365
+ | Loại cấu hình | Lưu ở | Ví dụ |
366
+ |---|---|---|
367
+ | App Settings | {{STORAGE}} | {{EXAMPLE}} |
368
+ | Secrets | {{SECRET_STORE}} | {{EXAMPLE}} <!-- secret production TUYỆT ĐỐI KHÔNG commit vào repo -->|
369
+ | Biến môi trường | {{ENV_STORE}} | {{EXAMPLE}} |
370
+
371
+ ## Chiến lược kiểm thử <!-- tier: ops -->
372
+
373
+ | Cấp độ | Phạm vi | Công cụ |
374
+ |---|---|---|
375
+ | Unit Test | {{SCOPE}} | {{TOOLS}} |
376
+ | Integration Test | {{SCOPE}} | {{TOOLS}} |
377
+ | Functional Test | {{SCOPE}} | {{TOOLS}} |
378
+ | Load Test | {{SCOPE}} | {{TOOLS}} |
379
+
380
+ - Đặt tên: {{TEST_NAMING}} <!-- vd: MethodName_Scenario_ExpectedResult -->
381
+ - {{CI_TEST_RULE}} <!-- vd: CI chạy unit + integration mỗi PR -->
382
+
383
+ ## Yêu cầu phi chức năng (NFR) <!-- tier: ops -->
384
+
385
+ | Yêu cầu | Mục tiêu |
386
+ |---|---|
387
+ | Khả năng mở rộng | {{TARGET}} |
388
+ | Tính sẵn sàng | {{TARGET}} <!-- vd: SLA uptime 99.9% -->|
389
+ | Triển khai | {{TARGET}} <!-- vd: zero-downtime, rolling update -->|
390
+ | Tính nhất quán | {{TARGET}} |
391
+ | Độ trễ | {{TARGET}} <!-- vd: API p95 < 200ms -->|
392
+ | Thông lượng | {{TARGET}} |
393
+ | Lưu trữ dữ liệu | {{TARGET}} |
394
+ | Sao lưu | {{TARGET}} |