@relipa/ai-flow-kit 0.1.5 → 0.1.7-beta.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 (49) hide show
  1. package/bin/aiflow.js +71 -33
  2. package/custom/rules/ml-conventions.md +11 -8
  3. package/custom/rules/project-conventions.md +18 -2
  4. package/custom/skills/design-experiment/SKILL.md +2 -2
  5. package/custom/skills/evaluate-model/SKILL.md +2 -2
  6. package/custom/skills/explore-data/SKILL.md +1 -1
  7. package/custom/skills/figma-to-component/SKILL.md +222 -20
  8. package/custom/skills/frame-ml-problem/SKILL.md +1 -1
  9. package/custom/skills/generate-spec/SKILL.md +19 -0
  10. package/custom/skills/read-study-requirement/SKILL.md +69 -1
  11. package/custom/skills/review-plan/SKILL.md +42 -0
  12. package/custom/templates/memory/CODEOWNERS +8 -0
  13. package/custom/templates/memory/ci/memory-finalize.yml +9 -0
  14. package/custom/templates/memory/ci/memory-lint.yml +10 -0
  15. package/custom/templates/memory/gitlab/merge_request_templates/memory.md +18 -0
  16. package/custom/templates/memory/memory-item.md +25 -0
  17. package/custom/templates/memory/skeleton/00.Shared/architecture/_global/.gitkeep +0 -0
  18. package/custom/templates/memory/skeleton/00.Shared/decisions/.gitkeep +0 -0
  19. package/custom/templates/memory/skeleton/00.Shared/domain/_global/.gitkeep +0 -0
  20. package/custom/templates/memory/skeleton/00.Shared/glossary/.gitkeep +0 -0
  21. package/custom/templates/memory/skeleton/01.Lessons/ba/_global/.gitkeep +0 -0
  22. package/custom/templates/memory/skeleton/01.Lessons/dev/_global/.gitkeep +0 -0
  23. package/custom/templates/memory/skeleton/01.Lessons/pm/_global/.gitkeep +0 -0
  24. package/custom/templates/memory/skeleton/01.Lessons/qa/_global/.gitkeep +0 -0
  25. package/custom/templates/memory/skeleton/02.Instincts/approved/_global/.gitkeep +0 -0
  26. package/custom/templates/memory/skeleton/03.Retro/.gitkeep +0 -0
  27. package/custom/templates/memory/skeleton/MEMORY.md +7 -0
  28. package/custom/templates/memory/skeleton/_deprecated/.gitkeep +0 -0
  29. package/custom/templates/shared/create-spec-workflow.md +68 -1
  30. package/custom/templates/shared/create-testcase-workflow.md +67 -0
  31. package/custom/templates/shared/gate-workflow.md +122 -3
  32. package/custom/templates/shared/ml-gate-workflow.md +16 -9
  33. package/docs/common/AIFLOW.md +11 -1
  34. package/docs/common/CHANGELOG.md +32 -0
  35. package/docs/common/cli-reference.md +3 -1
  36. package/docs/common/workflows/figma.md +176 -105
  37. package/package.json +2 -2
  38. package/scripts/create-score-excel.js +135 -14
  39. package/scripts/detect.js +11 -0
  40. package/scripts/docs-branch.js +264 -0
  41. package/scripts/hooks/figma-rate-limit.js +83 -0
  42. package/scripts/hooks/session-start.js +146 -8
  43. package/scripts/init.js +53 -1
  44. package/scripts/memory-store.js +391 -0
  45. package/scripts/memory.js +176 -247
  46. package/scripts/prompt.js +45 -0
  47. package/scripts/task.js +30 -15
  48. package/scripts/update.js +12 -0
  49. package/scripts/use.js +7 -5
@@ -76,7 +76,9 @@ When creating `AK-Docs/04.Coding/04.Reviews/[functionId]/[ticketId].md` during G
76
76
 
77
77
  5. Display Gate 1 prompt and wait for `APPROVED`.
78
78
 
79
- > After APPROVED, proceed to Gate 2. The lean doc is intentional — Gate 2 plan will fill in the details.
79
+ > After APPROVED, proceed to Gate 2.
80
+ > - **`gen-doc` tasks:** Gate 2 generates the output document immediately (no `plan.md`, no TDD). Self-review + task-summary.md are created automatically.
81
+ > - **All other tasks:** Gate 2 plan fills in the implementation details.
80
82
 
81
83
  ---
82
84
 
@@ -111,6 +113,63 @@ Do this investigation **before** asking any questions. Come prepared.
111
113
 
112
114
  ---
113
115
 
116
+ ### Step 1.5: Detect & Read Figma Design (UI tickets only)
117
+
118
+ Run this only when the ticket touches UI. Skip entirely for non-UI tickets.
119
+
120
+ 1. **Detect a Figma URL** in this order:
121
+ - `current.json` → `description`
122
+ - `current.json` → `supplementaryContext[]` items
123
+ - Pattern: `https://www.figma.com/design/<fileKey>/...?node-id=<nodeId>`
124
+ 2. **No URL but ticket looks like UI** (mentions screen, page, component, layout, "theo design"):
125
+ ask the DEV **once** to paste a Figma URL. If DEV skips → write "Design: TBD" in the
126
+ Design Analysis section and continue. Do NOT ask again.
127
+ 3. **URL found** → invoke `figma-to-component` Steps 0–2.5 (verify MCP, read design,
128
+ detect & export images). Then persist the shared artifact:
129
+ - Create `plan/[ticket-id]/design/`
130
+ - Save raw response to `plan/[ticket-id]/design/nodes.json` (cache for later gates)
131
+ - Save exported images to `plan/[ticket-id]/design/images/`
132
+ - Write `plan/[ticket-id]/design/figma-manifest.json` (nodeId → image file) — per-ticket,
133
+ NOT a shared global manifest
134
+ - Write `plan/[ticket-id]/design/design-context.md` using the template below
135
+ 4. **Figma MCP not connected** → tell DEV to run `aiflow init -a figma` (or `-a figma-desktop`),
136
+ mark Design Analysis as "⚠️ pending MCP", and continue with the non-UI parts of the requirement.
137
+ Do NOT abort the ticket.
138
+
139
+ **`design-context.md` template:**
140
+
141
+ ```markdown
142
+ # Design Context: [ticket-id]
143
+
144
+ **Figma URL:** <url> **fileKey:** xxx **nodeId:** 123-456
145
+ **Fetched:** [YYYY-MM-DD] **Adapter:** figma (REST) | figma-desktop
146
+
147
+ ## 1. Layout Structure
148
+ - Node tree: Frame > Header / Body > Card[] / Footer
149
+ - Flex direction, gap, padding, alignment per region
150
+
151
+ ## 2. Design Tokens
152
+ | Type | Figma value | Project token |
153
+ |------|-------------|---------------|
154
+ | Color | #3B82F6 | blue-500 / --color-primary |
155
+ | Type | 16/600 | text-base font-semibold |
156
+ | Space | 16px | p-4 / gap-4 |
157
+
158
+ ## 3. Components to build
159
+ - [ ] UserCard (variants: default, hover) — nodeId 78-910
160
+ - [ ] Header — nodeId 12-3
161
+
162
+ ## 4. Image Map
163
+ | nodeId | layer | file |
164
+ |--------|-------|------|
165
+ | 123-456 | Banner Top | design/images/banner-top-123-456.png |
166
+
167
+ ## 5. Notes
168
+ - Anything not mapped 1:1 (custom fonts, effects…)
169
+ ```
170
+
171
+ ---
172
+
114
173
  ### Step 2: Clarify Requirements via Q&A
115
174
 
116
175
  Ask clarifying questions directly — **one question at a time**, wait for the developer's response before asking the next.
@@ -227,6 +286,15 @@ Save to `AK-Docs/04.Coding/01.Requirements/[functionId]/[ticketId].md`:
227
286
 
228
287
  ---
229
288
 
289
+ ## Design Analysis (UI tickets only)
290
+
291
+ - **Source:** [Figma URL or "TBD" or "⚠️ pending MCP"]
292
+ - **Artifact:** `plan/[ticket-id]/design/design-context.md`
293
+ - **Summary:** layout, key components to build, design tokens to map, images exported
294
+ - **Impact on approach:** how the design shapes the proposed solution
295
+
296
+ ---
297
+
230
298
  ## 4. Impact Analysis
231
299
 
232
300
  **Impact Level:** 🟢 Low / 🟡 Medium / 🔴 High
@@ -6,6 +6,8 @@ keywords: review, code review, approve, summary, checklist
6
6
 
7
7
  # Review Plan — Gate 4
8
8
 
9
+ > ⚠️ **Skip this gate for `gen-doc` tasks.** gen-doc tasks only use Gates 1 and 2 (Gate 2 includes self-review automatically). This gate applies to `feature`, `bug-fix`, `refactor`, `documentation`, and `investigation` tasks.
10
+
9
11
  > **GATE 4: Runs after AI has completed code generation.**
10
12
  >
11
13
  > Principle: AI self-reviews first (verification + impact-analysis), then presents a summary to the developer. The developer decides: APPROVED or report a bug.
@@ -77,6 +79,26 @@ Run in the following mandatory order:
77
79
 
78
80
  **If any step fails → fix first, do not proceed to Step 2.**
79
81
 
82
+ ### Step 1.5: Design Conformance Check (UI tickets only)
83
+
84
+ If `plan/[ticket-id]/design/design-context.md` exists, compare the generated UI against the
85
+ design — **visually against the rendered reference image, not just the text checklist.** Open
86
+ `public/assets/figma/_reference-<nodeId>.png` (or the Figma frame) and the running UI, and check
87
+ region by region (header / panel / sections / footer):
88
+
89
+ - [ ] **Reference image compared** region by region; every visible difference listed
90
+ - [ ] **Background** is the correct layer (paint order) and actually visible
91
+ - [ ] **No-fabricate**: no element rendered that isn't in the design (no extra button/section)
92
+ - [ ] **No-omit**: every text label + every image node is present in its region
93
+ - [ ] Layout matches (arrangement/position per region, not a generic flow)
94
+ - [ ] Colors match the Design Tokens table (project token or arbitrary value)
95
+ - [ ] Typography matches (size, weight, line-height)
96
+ - [ ] Every component listed in "Components to build" exists
97
+ - [ ] Every image in the Image Map exists under `public/assets/figma/`
98
+
99
+ Any mismatch → treat as a coding bug: fix before Step 2. A text-only checklist tick is NOT enough —
100
+ if you have not actually looked at the reference image, say so. Skip this step for non-UI tickets.
101
+
80
102
  ### Step 2: Create Summary Report
81
103
 
82
104
  Create file `AK-Docs/04.Coding/04.Reviews/[functionId]/[ticketId].md`:
@@ -139,8 +161,25 @@ Create file `AK-Docs/04.Coding/04.Reviews/[functionId]/[ticketId].md`:
139
161
  - [ ] Naming and Code Style follow conventions
140
162
  - [ ] Sufficient tests (unit + integration)
141
163
  - [ ] No dead code / leftover console.logs
164
+ - [ ] UI matches `design-context.md` (layout, color, typography, spacing) — UI tickets only
142
165
  ```
143
166
 
167
+ ### Step 2.5: Retrospect + propose memory drafts
168
+
169
+ Synthesize what was learned this task. **Priority order — human corrections first:** if this is a repeat pass through Gate 4 (the developer already sent "BUG: ..." at least once this task), the developer's own correction is the single highest-value lesson — scan back through this session for it and draft it even if you already drafted something for the earlier BUG at Step 4 (dedup will catch an exact repeat; don't skip capturing it out of caution). Only after that, add anything else genuinely new: architecture facts discovered, decisions made and why. For each candidate, create a **local draft** (no approval needed yet — `_pending/` is local-only, doc `docs/internal/Memory-Architecture-v1.0.md`):
170
+
171
+ ```
172
+ ak memory draft --category 01.Lessons/dev --function-id [functionId] \
173
+ --slug <short-kebab-slug> --content "<≤150 từ, 1 fact>" \
174
+ --tags <tag1,tag2> --workflows coding --source "[ticket-id] / Gate 4"
175
+ ```
176
+
177
+ Other useful categories here: `00.Shared/architecture` (facts about the system discovered while coding), `00.Shared/decisions` (a technical choice made and why). Skip this step if nothing genuinely new was learned — don't manufacture a memory just to have one.
178
+
179
+ If `ak memory draft` reports a conflict (a similar memory already exists), don't create a duplicate — mention it to the developer instead so they can decide whether to edit the existing one.
180
+
181
+ List the created draft(s) in the Step 3 message below so the developer knows what's sitting in `_pending/` — they (or anyone on the team) can inspect and `ak memory submit` it later when ready to share with the team.
182
+
144
183
  ### Step 3: GATE 4 — Present to Developer
145
184
 
146
185
  ```markdown
@@ -154,6 +193,7 @@ Summary: AK-Docs/04.Coding/04.Reviews/[functionId]/[ticketId].md
154
193
  - Tests: ✅ [N] passed / ❌ [N] failed
155
194
  - Impact: [Low/Medium/High]
156
195
  - Checklist: [N/N] items ✅
196
+ - Memory drafts created: [N] (local, `_pending/` — `ak memory submit` when ready to share)
157
197
 
158
198
  **You need to:**
159
199
  1. Review the summary above
@@ -176,10 +216,12 @@ Summary: AK-Docs/04.Coding/04.Reviews/[functionId]/[ticketId].md
176
216
  → Guide on creating a Pull Request
177
217
 
178
218
  **If the developer types "BUG: [description]" — coding bug:**
219
+ → **Immediately draft a memory** capturing exactly what the developer flagged and why it was wrong (`ak memory draft --category 01.Lessons/dev --function-id [functionId] --slug <slug> --content "<the bug + the fix>" --workflows coding --source "[ticket-id] / Gate 4 dev feedback"`) — do this before fixing, while the correction is fresh; don't wait for Step 2.5 on the next pass to catch it in hindsight.
179
220
  → Analyze bug, fix, rerun verification
180
221
  → Repeat from Step 1 of Gate 4
181
222
 
182
223
  **If the developer types "BUG: [description]" — requirement bug:**
224
+ → **Immediately draft a memory** (same category/command as above) capturing what was misunderstood about the requirement and what it actually should have been — this is a requirement-comprehension lesson, valuable even though Gate 1 itself has no retrospect step of its own.
183
225
  → Notify: "This is a requirement bug, requirement document needs update"
184
226
  → Return to Gate 1, update requirement.md, wait for APPROVED again
185
227
 
@@ -0,0 +1,8 @@
1
+ # Áp dụng thủ công vào file CODEOWNERS của repo AK-Docs (mục 5.1.2 Memory-Architecture-v1.0.md).
2
+ # GitLab tự gợi ý các reviewer này làm Consult khi diff đụng đúng thư mục — không phải approver
3
+ # (approver luôn là PM, cấu hình riêng ở MR approval rule, không qua CODEOWNERS).
4
+
5
+ 99.Memory/00.Shared/architecture/ @TL
6
+ 99.Memory/00.Shared/domain/ @BA
7
+ 99.Memory/00.Shared/glossary/ @BA
8
+ 99.Memory/02.Instincts/ @TL
@@ -0,0 +1,9 @@
1
+ # Áp dụng thủ công vào .gitlab-ci.yml của repo AK-Docs (mục 5.1.2 Memory-Architecture-v1.0.md).
2
+ # Chạy khi MR label `memory` đã đủ approval từ PM — flip status, rebuild MEMORY.md, commit lên branch.
3
+ memory-finalize:
4
+ stage: finalize
5
+ rules:
6
+ - if: '$CI_MERGE_REQUEST_LABELS =~ /memory/ && $CI_MERGE_REQUEST_APPROVED == "true"'
7
+ script:
8
+ - node scripts/ci/memory-finalize.js # flip status: approved, điền reviewed_by, rebuild MEMORY.md
9
+ # (script CI thực tế nằm trong repo AK-Docs, không phải kit)
@@ -0,0 +1,10 @@
1
+ # Áp dụng thủ công vào .gitlab-ci.yml của repo AK-Docs (mục 5.1.2 Memory-Architecture-v1.0.md).
2
+ # Chạy trên mọi MR có label `memory`, TRƯỚC khi PM approve.
3
+ memory-lint:
4
+ stage: lint
5
+ rules:
6
+ - if: '$CI_MERGE_REQUEST_LABELS =~ /memory/'
7
+ script:
8
+ - node scripts/ci/memory-lint.js # validate frontmatter đủ trường, ≤150 từ, 1 fact/file,
9
+ # id không trùng, id khớp đường dẫn file, quét secret/PII
10
+ # (script CI thực tế nằm trong repo AK-Docs, không phải kit)
@@ -0,0 +1,18 @@
1
+ ## Memory MR — Ticket: <!-- TICKET-123 -->
2
+
3
+ **Loại:** Thêm mới / Gỡ bỏ (soft-remove / hard-remove)
4
+ **Mem-id(s):** <!-- mem-F003-refund-flow, mem-F003-prevent-429-error -->
5
+
6
+ ### Checklist PM (bắt buộc trước khi approve)
7
+
8
+ - [ ] Đúng sự thật và còn hiệu lực?
9
+ - [ ] Trace được nguồn gốc (`source:` trỏ đúng ticket/Gate)?
10
+ - [ ] Nội dung ngắn gọn (≤150 từ), đúng khuôn mẫu 1 file = 1 fact?
11
+ - [ ] Có hành động cụ thể áp dụng được (mục "Áp dụng")?
12
+ - [ ] Phạm vi (`scope:`) hẹp nhất có thể — không lạm dụng `global`?
13
+ - [ ] Không mâu thuẫn với `.rules/`?
14
+ - [ ] Đã tham vấn đúng người cho nội dung nhạy cảm (TL cho kỹ thuật, BA/Comtor cho glossary/domain)?
15
+
16
+ ### Consult (tự động theo CODEOWNERS)
17
+
18
+ TL/BA được tag tự động theo thư mục bị đụng — đây là góp ý, quyền quyết vẫn ở PM.
@@ -0,0 +1,25 @@
1
+ ---
2
+ id: mem-<functionId>-<slug> # CLI tự sinh từ vị trí file: mem-<functionId>-<slug>
3
+ # slug do AI đặt — ngắn, đọc là hiểu memory lưu gì
4
+ # (vd: basic-payment-flow, refund-flow, prevent-429-error).
5
+ # KHÔNG có timestamp: trùng slug trong cùng folder chính là
6
+ # tín hiệu chống trùng lặp — CLI hỏi "memory tương tự đã có,
7
+ # cập nhật bản cũ thay vì tạo mới?"
8
+ type: lesson | fact | decision | glossary | instinct
9
+ workflows: [coding, create-testcase] # workflow nào cần recall; [all] nếu mọi workflow
10
+ tags: [payment, refund, http-429] # ascii-lowercase, AI tự sinh khi tạo draft —
11
+ # scorer CHỈ so khớp trên trường này (không so body — JP/VN)
12
+ scope: F-003_Payment | global # trùng với folder chứa file; global → file đặt trong _global/
13
+ confidence: 0.6 # AI tự chấm 0.3–0.7; reviewer chỉnh khi duyệt
14
+ source: TICKET-123 / Gate 4 # trace: học được từ đâu
15
+ status: pending # pending | approved | deprecated
16
+ hits: 0 # consolidate cập nhật từ telemetry local — không sửa tay
17
+ created: 2026-07-09
18
+ reviewed_by: # điền khi approve
19
+ refs: [] # (tùy chọn) file/dir code hoặc docs mà memory phụ thuộc
20
+ verified_commit: # commit đã verify refs lần cuối (CLI tự điền)
21
+ stale: false # hook tự set true khi refs thay đổi sau verified_commit
22
+ ---
23
+ <Nội dung: TỐI ĐA 150 từ, 1 file = 1 fact duy nhất.>
24
+
25
+ **Áp dụng:** <hành động cụ thể AI/người cần làm khi gặp tình huống này.>
@@ -0,0 +1,7 @@
1
+ # 99.Memory — Project Brain Index
2
+
3
+ > Auto-generated by `ak memory submit` / `ak memory remove` when a Merge Request is merged into `main`.
4
+ > **Do not edit this file by hand** — it is rebuilt from the frontmatter of every `status: approved` file under `99.Memory/`.
5
+ > One line per memory: `- [mem-id](path/to/file.md) — <hook ≤15 từ> (type, scope, workflows)`
6
+
7
+ See `docs/internal/Memory-Architecture-v1.0.md` in ai-flow-kit for the full design.
@@ -7,6 +7,26 @@
7
7
 
8
8
  ---
9
9
 
10
+ ### Pre-flight BẮT BUỘC — Đồng bộ Source & Docs (đầu MỖI Gate)
11
+
12
+ > Áp dụng cho **cả 4 Gate** bên dưới — không chỉ Gate 1. Chạy đủ các bước sau **trước khi** thực hiện bất kỳ hành động nào khác của gate đó.
13
+
14
+ 1. **Sync repo source (bên trong, không phải AK-Docs/Shared-Docs):** xác định thư mục repo source liên quan (field `repo` trong context, hoặc repo duy nhất mở trong workspace) → `cd` vào đó → chạy `git status --porcelain`; nếu working tree sạch, chạy `git pull --ff-only`. Nếu có thay đổi chưa commit, branch diverged, hoặc không có remote tracking branch → bỏ qua pull (không phải lỗi).
15
+ 2. **Sync `AK-Docs/`:** `cd` vào `AK-Docs/` (sibling folder ở workspace root) → chạy `git pull`.
16
+ 3. **Sync `Shared-Docs/`:** `cd` vào `Shared-Docs/` (sibling folder ở workspace root) → chạy `git pull`.
17
+ 4. Sau khi xong, `cd` quay lại thư mục làm việc ban đầu trước khi tiếp tục các bước khác của gate.
18
+
19
+ Nếu `AK-Docs/` hoặc `Shared-Docs/` chưa tồn tại tại workspace root, hoặc không phải git repo → bỏ qua bước tương ứng, không cảnh báo.
20
+
21
+ **Nếu bất kỳ lệnh `git pull` nào ở Bước 1–3 thất bại** → **KHÔNG dừng workflow** — hiển thị cảnh báo và tiếp tục gate với dữ liệu local hiện có:
22
+
23
+ ```
24
+ ⚠️ CẢNH BÁO: Không thể pull [tên repo] — [lý do lỗi].
25
+ → Đang tiếp tục Gate [N] với dữ liệu local hiện tại, có thể chưa mới nhất.
26
+ ```
27
+
28
+ ---
29
+
10
30
  ### BA Skills
11
31
 
12
32
  Các skill sau đây được cài tự động vào `.claude/skills/ba-skills/` khi chạy `ak init` hoặc `ak up`.
@@ -76,6 +96,8 @@ Bàn giao Dev/Test
76
96
 
77
97
  **Bước thực hiện:**
78
98
 
99
+ **Pre-flight (bắt buộc, chạy trước Bước 0):** chạy [Pre-flight — Đồng bộ Source & Docs](#pre-flight-bắt-buộc--đồng-bộ-source--docs-đầu-mỗi-gate) ở đầu file. Lỗi → hiển thị ⚠️ cảnh báo, không dừng gate.
100
+
79
101
  #### Bước 0: Pre-flight — Xác định functionId và thư mục đầu ra
80
102
 
81
103
  **Xác định `functionId` (BẮT BUỘC):**
@@ -115,6 +137,20 @@ Vui lòng cung cấp mã định danh cho chức năng này (ví dụ: AD10, UC-
115
137
  → Bắt đầu Gate 1...
116
138
  ```
117
139
 
140
+ #### Bước 0.5: Đảm bảo đang làm việc trên branch riêng của task (AK-Docs)
141
+
142
+ Trước khi ghi bất kỳ file nào vào `02.BA-Specs/`, đảm bảo AK-Docs đang ở branch riêng của task này, không phải `main` (mọi thay đổi `AK-Docs` phải qua branch + Merge Request, PM duyệt cuối cùng trước khi merge vào `main`):
143
+
144
+ 1. Lấy `taskId` từ trường `taskId` trong `.aiflow/context/current.json`.
145
+ 2. Kiểm tra `AK-Docs` hiện đang ở branch nào (`git -C AK-Docs branch --show-current`).
146
+ 3. Nếu **chưa** ở branch `feature/[functionId]/[taskId]`:
147
+ - Hỏi BA: "Chưa có branch riêng cho task này trong AK-Docs. Tạo branch `feature/[functionId]/[taskId]` từ `main` — đồng ý không?"
148
+ - BA đồng ý → chạy `ak docs branch [functionId] [taskId] --yes`
149
+ - BA từ chối → tiếp tục Gate 1 trên nhánh hiện tại của AK-Docs (BA tự quản lý branch)
150
+ 4. Nếu **đã** ở đúng branch (ví dụ resume từ session trước) → bỏ qua, tiếp tục.
151
+
152
+ > ❌ Không tự thêm `--yes` khi chưa thấy BA gõ xác nhận rõ ràng trong hội thoại.
153
+
118
154
  #### Bước 1: Đọc và tổng hợp đầu vào
119
155
  - Đọc `.aiflow/context/current.json` — tiêu đề, mô tả, acceptance criteria, liên kết tài liệu
120
156
  - Nếu description có URL → chạy `ak fetch-links <url>` để tải nội dung
@@ -215,6 +251,8 @@ Vui lòng cung cấp câu trả lời theo một trong 3 cách:
215
251
 
216
252
  **Bước thực hiện:**
217
253
 
254
+ **Pre-flight (bắt buộc, chạy trước Bước 1):** chạy [Pre-flight — Đồng bộ Source & Docs](#pre-flight-bắt-buộc--đồng-bộ-source--docs-đầu-mỗi-gate) ở đầu file. Lỗi → hiển thị ⚠️ cảnh báo, không dừng gate.
255
+
218
256
  #### Bước 1: Tiếp nhận và đánh giá câu trả lời
219
257
  - Đối chiếu từng câu trả lời với câu hỏi trong `QnA-Log_v(m).md`
220
258
  - Phân loại:
@@ -292,6 +330,8 @@ File phân tích: [02.BA-Specs/01.Analysis/[functionId]/Analysis_v(n+1).md](02
292
330
 
293
331
  **Bước thực hiện:**
294
332
 
333
+ **Pre-flight (bắt buộc, chạy trước Bước 1):** chạy [Pre-flight — Đồng bộ Source & Docs](#pre-flight-bắt-buộc--đồng-bộ-source--docs-đầu-mỗi-gate) ở đầu file. Lỗi → hiển thị ⚠️ cảnh báo, không dừng gate.
334
+
295
335
  #### Bước 1: Liệt kê thành phần giao diện
296
336
  - Đọc tài liệu phân tích đã chốt
297
337
  - Xác định: ô nhập liệu, nút bấm, liên kết, vùng hiển thị lỗi, vùng hiển thị dữ liệu
@@ -345,6 +385,8 @@ File phân tích: [02.BA-Specs/01.Analysis/[functionId]/Analysis_v(n+1).md](02
345
385
 
346
386
  **Bước thực hiện:**
347
387
 
388
+ **Pre-flight (bắt buộc, chạy trước Bước 1):** chạy [Pre-flight — Đồng bộ Source & Docs](#pre-flight-bắt-buộc--đồng-bộ-source--docs-đầu-mỗi-gate) ở đầu file. Lỗi → hiển thị ⚠️ cảnh báo, không dừng gate.
389
+
348
390
  #### Bước 1: Khởi tạo Spec từ Template
349
391
  - **READ template:** `.claude/skills/ba-skills/skill-ba-uc-template-v1.md`
350
392
  - **READ skill:** `.claude/skills/ba-skills/skill-ba-write-uc-spec-v1.md`
@@ -387,7 +429,7 @@ File phân tích: [02.BA-Specs/01.Analysis/[functionId]/Analysis_v(n+1).md](02
387
429
  → Nếu blocked: làm theo gate-review skill response protocol
388
430
 
389
431
  **Phản hồi đặc biệt:**
390
- - BA gõ `REVISION: [nội dung]` → cập nhật section liên quan → re-generate review file → hiển thị lại gate pause
432
+ - BA gõ `REVISION: [nội dung]` → **trước khi sửa**, tạo ngay 1 memory draft ghi lại chính xác điều BA vừa chỉnh (`ak memory draft --category 01.Lessons/ba --function-id [functionId] --slug <slug> --content "<điều BA sửa + vì sao>" --workflows create-spec --source "[functionId] / Gate 4 BA feedback"`) — đây là tín hiệu giá trị nhất, đừng chờ Bước 7.5 mới bắt lại theo trí nhớ → cập nhật section liên quan → re-generate review file → hiển thị lại gate pause
391
433
 
392
434
  **Definition of Done:**
393
435
  - [ ] File `UC-Spec_v1.md` đúng vị trí và cấu trúc template
@@ -399,6 +441,30 @@ File phân tích: [02.BA-Specs/01.Analysis/[functionId]/Analysis_v(n+1).md](02
399
441
  > **Telemetry:** Run `ak gate 4 start --ticket [functionId]` khi bắt đầu gate này.
400
442
  > Run `ak gate 4 approved --ticket [functionId]` sau khi gate-review verify passed. Run as-is — không thêm shell redirects.
401
443
 
444
+ #### Bước 7.5: Retrospect + đề xuất memory draft
445
+
446
+ Sau khi Gate 4 APPROVED, đúc kết những gì học được. **Ưu tiên feedback của BA trước:** nếu có vòng `REVISION` nào đã xảy ra, đảm bảo điều BA sửa đã có draft (thường đã tạo ngay lúc REVISION ở trên — kiểm tra lại, đừng bỏ sót). Sau đó mới thêm những gì mới khác: business rule mới confirm, thuật ngữ cần làm rõ, quyết định đặc tả và lý do. Tạo **draft local** cho từng candidate (chưa cần duyệt — `_pending/` chỉ ở local, xem `docs/internal/Memory-Architecture-v1.0.md`):
447
+
448
+ ```
449
+ ak memory draft --category 00.Shared/domain --function-id [functionId] \
450
+ --slug <slug-ngắn> --content "<≤150 từ, 1 fact>" \
451
+ --tags <tag1,tag2> --workflows create-spec --source "[functionId] / Gate 4"
452
+ ```
453
+
454
+ Category khác hữu ích ở đây: `00.Shared/glossary` (thuật ngữ JP↔VN↔EN mới xác nhận — flat, không cần `--function-id`), `01.Lessons/ba` (bài học khi làm spec), `00.Shared/decisions` (quyết định đặc tả). Bỏ qua bước này nếu không có gì thực sự mới. Nếu `ak memory draft` báo trùng, đừng tạo bản mới — báo cho BA để họ quyết định sửa bản cũ.
455
+
456
+ #### Bước 8: Submit AK-Docs lên remote qua Merge Request
457
+
458
+ Sau khi Gate 4 đã APPROVED (UC Spec hoàn thành):
459
+
460
+ 1. Soạn title + description cho Merge Request (tóm tắt UC Spec vừa hoàn thành, link ticket gốc), hiển thị cho BA xem trước.
461
+ 2. Hỏi BA: "Nội dung commit/MR như trên — đồng ý submit AK-Docs không?"
462
+ - BA đồng ý → chạy `ak docs submit --title "..." --description "..." --yes`
463
+ - BA từ chối → dừng, để BA tự commit/tạo MR khi sẵn sàng
464
+ 3. Thông báo BA: MR đã mở, chờ **PM review & merge vào `main`** — đây là bước duyệt cuối cùng cho tài liệu, không phải BA tự merge.
465
+
466
+ > ❌ Không tự thêm `--yes` khi chưa thấy BA gõ xác nhận rõ ràng trong hội thoại.
467
+
402
468
  ---
403
469
 
404
470
  ### Bản đồ Skills theo Gate
@@ -419,6 +485,7 @@ File phân tích: [02.BA-Specs/01.Analysis/[functionId]/Analysis_v(n+1).md](02
419
485
  - ❌ **KHÔNG** tiến Gate 3 khi vẫn còn câu hỏi Open
420
486
  - ❌ **KHÔNG chấp nhận APPROVED** ở Gate 1 hoặc Gate 2 khi còn bất kỳ câu hỏi nào trạng thái **Open** — từ chối và hiển thị danh sách câu hỏi cần trả lời
421
487
  - ✅ **BẮT BUỘC** chạy Bước 0 ở Gate 1 — xác nhận `functionId` và thư mục đầu ra trước khi làm bất cứ điều gì
488
+ - ✅ **BẮT BUỘC** chạy Bước 0.5 ở Gate 1 — đảm bảo AK-Docs đang ở branch `feature/[functionId]/[taskId]` trước khi ghi file đầu tiên
422
489
  - ✅ **BẮT BUỘC** đọc skill từ `.claude/skills/ba-skills/` — không suy luận từ bộ nhớ
423
490
  - ✅ **BẮT BUỘC** invoke `gate-review` cuối mỗi gate và chờ APPROVED
424
491
  - ✅ **BẮT BUỘC** mọi thẻ tương tác trong HTML có `id` duy nhất
@@ -7,6 +7,26 @@
7
7
 
8
8
  ---
9
9
 
10
+ ### Pre-flight BẮT BUỘC — Đồng bộ Source & Docs (đầu MỖI Gate)
11
+
12
+ > Áp dụng cho **cả 4 Gate** bên dưới — không chỉ Gate 1. Chạy đủ các bước sau **trước khi** thực hiện bất kỳ hành động nào khác của gate đó.
13
+
14
+ 1. **Sync repo source (bên trong, không phải AK-Docs/Shared-Docs):** xác định thư mục repo source liên quan (field `repo` trong context, hoặc repo duy nhất mở trong workspace) → `cd` vào đó → chạy `git status --porcelain`; nếu working tree sạch, chạy `git pull --ff-only`. Nếu có thay đổi chưa commit, branch diverged, hoặc không có remote tracking branch → bỏ qua pull (không phải lỗi).
15
+ 2. **Sync `AK-Docs/`:** `cd` vào `AK-Docs/` (sibling folder ở workspace root) → chạy `git pull`.
16
+ 3. **Sync `Shared-Docs/`:** `cd` vào `Shared-Docs/` (sibling folder ở workspace root) → chạy `git pull`.
17
+ 4. Sau khi xong, `cd` quay lại thư mục làm việc ban đầu trước khi tiếp tục các bước khác của gate.
18
+
19
+ Nếu `AK-Docs/` hoặc `Shared-Docs/` chưa tồn tại tại workspace root, hoặc không phải git repo → bỏ qua bước tương ứng, không cảnh báo.
20
+
21
+ **Nếu bất kỳ lệnh `git pull` nào ở Bước 1–3 thất bại** → **KHÔNG dừng workflow** — hiển thị cảnh báo và tiếp tục gate với dữ liệu local hiện có:
22
+
23
+ ```
24
+ ⚠️ CẢNH BÁO: Không thể pull [tên repo] — [lý do lỗi].
25
+ → Đang tiếp tục Gate [N] với dữ liệu local hiện tại, có thể chưa mới nhất.
26
+ ```
27
+
28
+ ---
29
+
10
30
  ### QA Skills
11
31
 
12
32
  Các skill sau đây được cài tự động vào `.claude/skills/test-skills/` khi chạy `ak init` hoặc `ak up`.
@@ -96,6 +116,8 @@ Sẵn sàng thực thi kiểm thử
96
116
 
97
117
  **Bước thực hiện:**
98
118
 
119
+ **Pre-flight (bắt buộc, chạy trước Bước 0):** chạy [Pre-flight — Đồng bộ Source & Docs](#pre-flight-bắt-buộc--đồng-bộ-source--docs-đầu-mỗi-gate) ở đầu file. Lỗi → hiển thị ⚠️ cảnh báo, không dừng gate.
120
+
99
121
  #### Bước 0: Pre-flight — Xác định functionId và thư mục đầu ra
100
122
 
101
123
  **Xác định `functionId` (BẮT BUỘC):**
@@ -133,6 +155,20 @@ Vui lòng cung cấp mã định danh cho chức năng này (ví dụ: AD06, TC-
133
155
  → Bắt đầu Gate 1...
134
156
  ```
135
157
 
158
+ #### Bước 0.5: Đảm bảo đang làm việc trên branch riêng của task (AK-Docs)
159
+
160
+ Trước khi ghi bất kỳ file nào vào `03.Testing/`, đảm bảo AK-Docs đang ở branch riêng của task này, không phải `main` (mọi thay đổi `AK-Docs` phải qua branch + Merge Request, PM duyệt cuối cùng trước khi merge vào `main`):
161
+
162
+ 1. Lấy `taskId` từ trường `taskId` trong `.aiflow/context/current.json`.
163
+ 2. Kiểm tra `AK-Docs` hiện đang ở branch nào (`git -C AK-Docs branch --show-current`).
164
+ 3. Nếu **chưa** ở branch `feature/[functionId]/[taskId]`:
165
+ - Hỏi QA: "Chưa có branch riêng cho task này trong AK-Docs. Tạo branch `feature/[functionId]/[taskId]` từ `main` — đồng ý không?"
166
+ - QA đồng ý → chạy `ak docs branch [functionId] [taskId] --yes`
167
+ - QA từ chối → tiếp tục Gate 1 trên nhánh hiện tại của AK-Docs (QA tự quản lý branch)
168
+ 4. Nếu **đã** ở đúng branch (ví dụ resume từ session trước) → bỏ qua, tiếp tục.
169
+
170
+ > ❌ Không tự thêm `--yes` khi chưa thấy QA gõ xác nhận rõ ràng trong hội thoại.
171
+
136
172
  #### Bước 1: Đọc và tổng hợp đầu vào
137
173
  - Đọc `.aiflow/context/current.json` — tiêu đề, mô tả, acceptance criteria, liên kết tài liệu
138
174
  - Nếu description có URL → chạy `ak fetch-links <url>` để tải nội dung
@@ -216,6 +252,8 @@ Vui lòng cung cấp mã định danh cho chức năng này (ví dụ: AD06, TC-
216
252
 
217
253
  **Bước thực hiện:**
218
254
 
255
+ **Pre-flight (bắt buộc, chạy trước Bước 1):** chạy [Pre-flight — Đồng bộ Source & Docs](#pre-flight-bắt-buộc--đồng-bộ-source--docs-đầu-mỗi-gate) ở đầu file. Lỗi → hiển thị ⚠️ cảnh báo, không dừng gate.
256
+
219
257
  #### Bước 1: Đọc artifact đầu vào
220
258
  - Đọc `[functionId]_01_Requirement_Analysis_Result.md` — nắm toàn bộ 16 mục, Risk Matrix, Business Rules
221
259
 
@@ -287,6 +325,8 @@ Vui lòng cung cấp mã định danh cho chức năng này (ví dụ: AD06, TC-
287
325
 
288
326
  **Bước thực hiện:**
289
327
 
328
+ **Pre-flight (bắt buộc, chạy trước Bước 1):** chạy [Pre-flight — Đồng bộ Source & Docs](#pre-flight-bắt-buộc--đồng-bộ-source--docs-đầu-mỗi-gate) ở đầu file. Lỗi → hiển thị ⚠️ cảnh báo, không dừng gate.
329
+
290
330
  #### Bước 1: Đọc template và quy ước bắt buộc
291
331
  - **READ template:** `.claude/skills/test-skills/template/testcase-template.md` — **BẮT BUỘC** tuân thủ 100%
292
332
  - **READ skill:** `.claude/skills/test-skills/rules/qa-writing-standards.md`
@@ -376,6 +416,8 @@ Vui lòng cung cấp mã định danh cho chức năng này (ví dụ: AD06, TC-
376
416
 
377
417
  **Bước thực hiện:**
378
418
 
419
+ **Pre-flight (bắt buộc, chạy trước Bước 1):** chạy [Pre-flight — Đồng bộ Source & Docs](#pre-flight-bắt-buộc--đồng-bộ-source--docs-đầu-mỗi-gate) ở đầu file. Lỗi → hiển thị ⚠️ cảnh báo, không dừng gate.
420
+
379
421
  #### Bước 1: Review nội dung và đánh giá bao phủ
380
422
  - **READ skill:** `.claude/skills/test-skills/categories/00-core/00.04.testcase-review.md`
381
423
  - **READ skill:** `.claude/skills/test-skills/categories/99-review/99.01.coverage-review.md`
@@ -443,6 +485,30 @@ Vui lòng cung cấp mã định danh cho chức năng này (ví dụ: AD06, TC-
443
485
  > **Telemetry:** Run `ak gate 4 start --ticket [functionId]` khi bắt đầu gate này.
444
486
  > Run `ak gate 4 approved --ticket [functionId]` sau khi gate-review verify passed. Run as-is — không thêm shell redirects.
445
487
 
488
+ #### Bước 7.5: Retrospect + đề xuất memory draft
489
+
490
+ Sau khi Gate 4 APPROVED, đúc kết những gì học được. **Ưu tiên trước:** nếu review ở Bước 7 từng phát hiện Major/Critical issue phải sửa lại, đó là bài học giá trị nhất — đảm bảo có draft cho đúng issue đó. Sau đó mới thêm lỗi/bẫy môi trường gặp khi test, business rule cần làm rõ thêm. Tạo **draft local** cho từng candidate (chưa cần duyệt — `_pending/` chỉ ở local, xem `docs/internal/Memory-Architecture-v1.0.md`):
491
+
492
+ ```
493
+ ak memory draft --category 01.Lessons/qa --function-id [functionId] \
494
+ --slug <slug-ngắn> --content "<≤150 từ, 1 fact>" \
495
+ --tags <tag1,tag2> --workflows create-testcase --source "[functionId] / Gate 4"
496
+ ```
497
+
498
+ Category khác hữu ích ở đây: `00.Shared/domain` (business rule phát hiện khi thiết kế test), `00.Shared/glossary` (thuật ngữ — flat, không cần `--function-id`). Bỏ qua bước này nếu không có gì thực sự mới. Nếu `ak memory draft` báo trùng, đừng tạo bản mới — báo cho QA để họ quyết định sửa bản cũ.
499
+
500
+ #### Bước 8: Submit AK-Docs lên remote qua Merge Request
501
+
502
+ Sau khi Gate 4 đã APPROVED (bộ Test Case hoàn thành):
503
+
504
+ 1. Soạn title + description cho Merge Request (tóm tắt bộ Test Case vừa hoàn thành, sign-off status, link ticket gốc), hiển thị cho QA xem trước.
505
+ 2. Hỏi QA: "Nội dung commit/MR như trên — đồng ý submit AK-Docs không?"
506
+ - QA đồng ý → chạy `ak docs submit --title "..." --description "..." --yes`
507
+ - QA từ chối → dừng, để QA tự commit/tạo MR khi sẵn sàng
508
+ 3. Thông báo QA: MR đã mở, chờ **PM review & merge vào `main`** — đây là bước duyệt cuối cùng cho tài liệu, không phải QA tự merge.
509
+
510
+ > ❌ Không tự thêm `--yes` khi chưa thấy QA gõ xác nhận rõ ràng trong hội thoại.
511
+
446
512
  ---
447
513
 
448
514
  ### Bản đồ Skills theo Gate
@@ -463,6 +529,7 @@ Vui lòng cung cấp mã định danh cho chức năng này (ví dụ: AD06, TC-
463
529
  - ❌ **KHÔNG** sinh `04_Final_Result.md` mà thiếu `[functionId]_TestCase.md` (hoặc ngược lại)
464
530
  - ❌ **KHÔNG** tiến gate tiếp theo khi còn issue Critical hoặc Major chưa giải quyết
465
531
  - ✅ **BẮT BUỘC** chạy Bước 0 ở Gate 1 — xác nhận `functionId` và thư mục đầu ra trước khi làm bất cứ điều gì
532
+ - ✅ **BẮT BUỘC** chạy Bước 0.5 ở Gate 1 — đảm bảo AK-Docs đang ở branch `feature/[functionId]/[taskId]` trước khi ghi file đầu tiên
466
533
  - ✅ **BẮT BUỘC** đọc skill từ `.claude/skills/test-skills/` — không suy luận từ bộ nhớ
467
534
  - ✅ **BẮT BUỘC** invoke `gate-review` cuối mỗi gate và chờ APPROVED
468
535
  - ✅ **BẮT BUỘC** tuân thủ 100% `testcase-template.md` ở Gate 3 và Gate 4