@educa-corp/sdd-framework 0.2.4 → 0.2.6

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 (152) hide show
  1. package/commands/generate-architecture.md +706 -0
  2. package/commands/generate-architecture.tmpl +194 -0
  3. package/commands/generate-code.md +35 -9
  4. package/commands/generate-code.tmpl +35 -9
  5. package/commands/generate-tech-docs.md +259 -246
  6. package/commands/generate-tech-docs.tmpl +21 -0
  7. package/core/FRAMEWORK_VERSION +1 -1
  8. package/core/commands/generate-architecture.md +706 -0
  9. package/core/commands/generate-code.md +35 -9
  10. package/core/commands/generate-tech-docs.md +259 -246
  11. package/core/skills/setup-ai-first/SKILL.md +12 -4
  12. package/core/templates/architecture.template.md +392 -111
  13. package/core/templates/tech-design.template.md +238 -246
  14. package/docs/01-getting-started/installation.md +47 -112
  15. package/docs/01-getting-started/quickstart.md +58 -72
  16. package/docs/01-getting-started/what-is-sdd.md +75 -0
  17. package/docs/02-concepts/architecture.md +109 -0
  18. package/docs/02-concepts/glossary.md +87 -0
  19. package/docs/02-concepts/overview.md +93 -0
  20. package/docs/02-concepts/pipeline-steps/00-setup.md +102 -0
  21. package/docs/02-concepts/pipeline-steps/01-discovery.md +129 -0
  22. package/docs/02-concepts/pipeline-steps/02-specification.md +130 -0
  23. package/docs/02-concepts/pipeline-steps/03-design-spec.md +90 -0
  24. package/docs/02-concepts/pipeline-steps/04-bdd.md +120 -0
  25. package/docs/02-concepts/pipeline-steps/05-tech-docs.md +101 -0
  26. package/docs/02-concepts/pipeline-steps/06-code.md +119 -0
  27. package/docs/02-concepts/pipeline-steps/07-dev-selftest.md +92 -0
  28. package/docs/02-concepts/pipeline-steps/08-qc-automation.md +102 -0
  29. package/docs/02-concepts/pipeline-steps/09-validate-traces.md +104 -0
  30. package/docs/02-concepts/pipeline-steps/10-feedback-loop.md +105 -0
  31. package/docs/02-concepts/pipeline-steps/README.md +92 -0
  32. package/docs/02-concepts/roles-and-hitl.md +73 -0
  33. package/docs/02-concepts/traceability.md +94 -0
  34. package/docs/03-guides/architect.md +98 -0
  35. package/docs/03-guides/developer.md +76 -0
  36. package/docs/03-guides/product-owner.md +68 -0
  37. package/docs/03-guides/tester-qa.md +70 -0
  38. package/docs/04-reference/commands.md +105 -0
  39. package/docs/04-reference/configuration.md +94 -0
  40. package/docs/04-reference/model-selection.md +68 -0
  41. package/docs/04-reference/modules.md +74 -0
  42. package/docs/04-reference/trace-schema.md +93 -0
  43. package/docs/README.md +29 -40
  44. package/docs/explain/00-setup-ai-first.md +77 -0
  45. package/docs/explain/00b-generate-architecture.md +76 -0
  46. package/docs/explain/01-define-product.md +79 -0
  47. package/docs/explain/02-generate-prd.md +78 -0
  48. package/docs/explain/03-refine-prd.md +86 -0
  49. package/docs/explain/04-review-context.md +100 -0
  50. package/docs/explain/05-generate-design-spec.md +73 -0
  51. package/docs/explain/06-generate-bdd.md +77 -0
  52. package/docs/explain/07-generate-tech-docs.md +71 -0
  53. package/docs/explain/08-review-tech-docs.md +79 -0
  54. package/docs/explain/09-generate-code.md +78 -0
  55. package/docs/explain/10-review-code.md +70 -0
  56. package/docs/explain/11-map-testids.md +69 -0
  57. package/docs/explain/12-dev-gen-test.md +66 -0
  58. package/docs/explain/13-dev-run-test.md +69 -0
  59. package/docs/explain/14-dev-smoke-test.md +67 -0
  60. package/docs/explain/15-qc-analyze.md +68 -0
  61. package/docs/explain/16-qc-plan.md +61 -0
  62. package/docs/explain/17-qc-design-test.md +61 -0
  63. package/docs/explain/18-qc-review.md +59 -0
  64. package/docs/explain/19-qc-run-test.md +67 -0
  65. package/docs/explain/20-qc-report.md +61 -0
  66. package/docs/explain/21-validate-traces.md +68 -0
  67. package/docs/explain/22-generate-spec-manifest.md +60 -0
  68. package/docs/explain/23-fix-bug.md +69 -0
  69. package/docs/explain/24-debug.md +61 -0
  70. package/docs/explain/25-report-bug.md +65 -0
  71. package/docs/explain/26-propose-scenario.md +63 -0
  72. package/docs/explain/27-learn.md +65 -0
  73. package/docs/explain/28-sync.md +70 -0
  74. package/docs/explain/29-update-framework.md +65 -0
  75. package/docs/explain/README.md +134 -0
  76. package/package.json +1 -1
  77. package/skills/setup-ai-first/SKILL.md +12 -4
  78. package/skills/setup-ai-first/SKILL.tmpl +12 -4
  79. package/templates/architecture.template.md +392 -111
  80. package/templates/tech-design.template.md +238 -246
  81. package/docs/01-getting-started/README.md +0 -19
  82. package/docs/01-getting-started/core-concepts.md +0 -102
  83. package/docs/02-guides/README.md +0 -26
  84. package/docs/02-guides/bdd-input-checklist.md +0 -68
  85. package/docs/02-guides/developer/README.md +0 -49
  86. package/docs/02-guides/developer/bdd-and-trace.md +0 -126
  87. package/docs/02-guides/developer/commands.md +0 -76
  88. package/docs/02-guides/developer/pr-checklist.md +0 -16
  89. package/docs/02-guides/developer/scenarios.md +0 -460
  90. package/docs/02-guides/developer/workflow.md +0 -121
  91. package/docs/02-guides/prd-input-checklist.md +0 -94
  92. package/docs/02-guides/product-owner/README.md +0 -81
  93. package/docs/02-guides/product-owner/commands.md +0 -30
  94. package/docs/02-guides/product-owner/handoff-checklist.md +0 -42
  95. package/docs/02-guides/product-owner/prd-writing-rules.md +0 -45
  96. package/docs/02-guides/product-owner/scenarios.md +0 -438
  97. package/docs/02-guides/tech-docs-input-checklist.md +0 -109
  98. package/docs/02-guides/tester/README.md +0 -75
  99. package/docs/02-guides/tester/bug-reporting.md +0 -117
  100. package/docs/02-guides/tester/qc-automation.md +0 -165
  101. package/docs/02-guides/tester/reading-specs.md +0 -79
  102. package/docs/02-guides/tester/scenarios.md +0 -186
  103. package/docs/02-guides/tester/spec-manifest.md +0 -130
  104. package/docs/02-guides/tester/test-checklist.md +0 -31
  105. package/docs/02-guides/tester/workflow.md +0 -77
  106. package/docs/03-concepts/README.md +0 -20
  107. package/docs/03-concepts/architecture.md +0 -248
  108. package/docs/03-concepts/mechanisms-explained.md +0 -124
  109. package/docs/03-concepts/pipeline.md +0 -278
  110. package/docs/03-concepts/traceability.md +0 -152
  111. package/docs/04-operations/README.md +0 -33
  112. package/docs/04-operations/bug-flow.md +0 -364
  113. package/docs/04-operations/publishing.md +0 -154
  114. package/docs/04-operations/sync-and-update.md +0 -522
  115. package/docs/05-reference/README.md +0 -34
  116. package/docs/05-reference/command-cheatsheet.md +0 -147
  117. package/docs/05-reference/commands.md +0 -234
  118. package/docs/05-reference/model-selection.md +0 -74
  119. package/docs/05-reference/modules.md +0 -110
  120. package/docs/05-reference/trace-schema.md +0 -154
  121. package/docs/06-commands/README.md +0 -75
  122. package/docs/06-commands/explain-debug.md +0 -32
  123. package/docs/06-commands/explain-define-product.md +0 -43
  124. package/docs/06-commands/explain-dev-gen-test.md +0 -28
  125. package/docs/06-commands/explain-dev-run-test.md +0 -24
  126. package/docs/06-commands/explain-dev-smoke-test.md +0 -25
  127. package/docs/06-commands/explain-fix-bug.md +0 -28
  128. package/docs/06-commands/explain-generate-bdd.md +0 -45
  129. package/docs/06-commands/explain-generate-code.md +0 -53
  130. package/docs/06-commands/explain-generate-design-spec.md +0 -54
  131. package/docs/06-commands/explain-generate-prd.md +0 -45
  132. package/docs/06-commands/explain-generate-spec-manifest.md +0 -20
  133. package/docs/06-commands/explain-generate-tech-docs.md +0 -56
  134. package/docs/06-commands/explain-learn.md +0 -21
  135. package/docs/06-commands/explain-map-testids.md +0 -28
  136. package/docs/06-commands/explain-propose-scenario.md +0 -24
  137. package/docs/06-commands/explain-qc-analyze.md +0 -22
  138. package/docs/06-commands/explain-qc-design-test.md +0 -20
  139. package/docs/06-commands/explain-qc-plan.md +0 -21
  140. package/docs/06-commands/explain-qc-report.md +0 -23
  141. package/docs/06-commands/explain-qc-review.md +0 -24
  142. package/docs/06-commands/explain-qc-run-test.md +0 -27
  143. package/docs/06-commands/explain-refine-prd.md +0 -51
  144. package/docs/06-commands/explain-report-bug.md +0 -24
  145. package/docs/06-commands/explain-review-code.md +0 -45
  146. package/docs/06-commands/explain-review-context.md +0 -68
  147. package/docs/06-commands/explain-review-tech-docs.md +0 -45
  148. package/docs/06-commands/explain-setup-ai-first.md +0 -25
  149. package/docs/06-commands/explain-sync.md +0 -24
  150. package/docs/06-commands/explain-update-framework.md +0 -22
  151. package/docs/06-commands/explain-validate-traces.md +0 -25
  152. package/docs/t-sample.md +0 -826
@@ -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}} |