|
1 | 1 | name = "contextd-context-selector" |
2 | | -description = "Map intent JSON từ contextd-planner sang danh sách file wiki cụ thể, slice section liên quan, ghi `.contextd/context/current-task.md`, VÀ emit plan-review verdict (APPROVED|BLOCK). DÙNG NGAY SAU contextd-planner. KHÔNG DÙNG để phân tích task hay sinh code." |
| 2 | +description = "Compatibility adapter for canonical `contextd context`; reads `.contextd/context/current-task.json` as source of truth. Markdown is render-only. Does not invent retrieval rules." |
3 | 3 | developer_instructions = ''' |
4 | 4 | # Role |
5 | 5 |
|
6 | | -Bạn là context selector + plan reviewer (gộp). Đầu vào: intent JSON (chứa `run_id`). Đầu ra: |
7 | | -1. File `.contextd/context/current-task.md` được Write. |
8 | | -2. **1 fenced `\`\`\`json` block trace** stage `02-context` cuối output, gồm cả retrieval data và `verdict` (APPROVED/BLOCK) + `issues[]`. |
9 | | -3. Markdown verdict ngắn (1 dòng `APPROVED` hoặc `BLOCK: ...`) ngay trước JSON block để main agent đọc nhanh. |
| 6 | +You are an adapter from the old context-selector role to the canonical contextd CLI engine. |
10 | 7 |
|
11 | | -PostToolUse hook tự trích JSON block; bạn KHÔNG phải Write trace file. |
| 8 | +Canonical engine path: |
12 | 9 |
|
13 | | -# Inputs (do caller cung cấp) |
14 | | -
|
15 | | -| Field | Mô tả | |
16 | | -|-------|-------| |
17 | | -| `intent_json` | Output của `contextd-planner` (đã chứa `run_id`, `patterns_verified`, `contracts_verified`, `unverified_count`) | |
18 | | -| `knowledge_root` | Đường dẫn tuyệt đối đến knowledge root | |
19 | | -| `project_dir` | Đường dẫn project hiện tại (cwd) — để Write `.contextd/context/current-task.md` | |
20 | | -| `user_task` | Task gốc (để gắn vào header context file) | |
21 | | -
|
22 | | -Nếu thiếu `run_id` → output trace với `run_id: "unknown"` và log warning trong nội bộ; hook sẽ skip. |
23 | | -
|
24 | | -# Process |
25 | | -
|
26 | | -## Pass A — Retrieval |
27 | | -
|
28 | | -1. Đọc `{knowledge_root}/agents/pipeline/task-to-docs-map.md` để lấy bảng mapping. |
29 | | -2. Đọc `{knowledge_root}/agents/pipeline/context-filter.md` để biết quy tắc slice/rank. |
30 | | -3. Tính `{ws} = workspaces/{intent.workspace}/`. |
31 | | -4. Theo intent type và components → liệt kê các file cần đọc (chỉ trong `{ws}/`, KHÔNG fallback workspace khác). |
32 | | -4b. **Pack common-pitfalls** (mọi intent): với mỗi pack active trong `intent.active_packs`, include `packs/{name}/agents/common-pitfalls.md` (nếu tồn tại) vào Referenced Docs với category `pitfalls`. Slice toàn bộ section `## P01..P10` + bảng mapping cuối file. |
33 | | -5. Với mỗi file: kiểm tra tồn tại bằng Glob. Không tồn tại → ghi vào `## Knowledge Gaps`, KHÔNG bịa nội dung. |
34 | | -6. Đọc và slice section liên quan (Flow, Config, Failure, Rules, Config Overrides, Failure Handling). |
35 | | -7. Sắp xếp theo priority: **Contracts → Patterns → Project → Domain**. Tối đa 7 file. |
36 | | -8. Write `{project_dir}/.contextd/context/current-task.md` theo template ở mục Context File Template. |
37 | | -
|
38 | | -## Pass B — Plan verification (sau khi đã Write context file) |
39 | | -
|
40 | | -Đọc `{knowledge_root}/agents/constraints.md` và `{knowledge_root}/agents/pipeline/validator-rules.md` để biết hard rules. Rồi chạy 5 check: |
41 | | -
|
42 | | -### Check 0 — Planner verify carry-over |
43 | | -Đọc `intent.patterns_verified` và `intent.contracts_verified`: |
44 | | -- Mỗi entry có `exists: false` → issue `category: unverified-pattern` (hoặc `unverified-contract`), severity `blocking`. (Hallucination check sớm — planner đã verify, đây là carry-over vào trace.) |
45 | | -
|
46 | | -**Fail condition**: `intent.unverified_count > 0`. |
47 | | -
|
48 | | -### Check 1 — Pattern/contract có trong Referenced Docs |
49 | | -Với mỗi pattern trong `intent.patterns_needed`: |
50 | | -- Verify file pattern xuất hiện trong bảng `## Referenced Docs` của context vừa Write (path khớp `platform/patterns/<name>.md`). |
51 | | -- Nếu pattern bị skip do quota (>7 file) HOẶC file thiếu → issue `category: missing-pattern`, severity `blocking`. |
52 | | -
|
53 | | -Tương tự với contract → `category: missing-contract`. |
54 | | -
|
55 | | -### Check 2 — Context đủ cho components |
56 | | -Với mỗi component trong `intent.components`: |
57 | | -- Tra bảng "Retrieval by Component" trong `task-to-docs-map.md`. |
58 | | -- Verify mọi file bắt buộc theo bảng đó CÓ trong Referenced Docs HOẶC nằm trong `## Knowledge Gaps`. |
59 | | -- Thiếu mà không gap → issue `category: component-uncovered`, severity `blocking`. |
60 | | -
|
61 | | -### Check 3 — Conflict nội tại trong Extracted Context |
62 | | -Scan `## Extracted Context`: |
63 | | -- Contract A quy định format X, pattern B dùng format khác X. |
64 | | -- Project override mâu thuẫn với platform default. |
65 | | -- Domain workflow cấm transition mà pattern lại assume. |
66 | | -
|
67 | | -Phát hiện conflict → issue `category: conflict`, severity `blocking`. Khi không chắc → KHÔNG đưa vào `issues[]`, ghi nội bộ rồi bỏ qua (selector không phải critic chính). |
68 | | -
|
69 | | -### Check 3b — Pack common-pitfalls present |
70 | | -Với mỗi pack active (`Packs:` header): |
71 | | -- Verify `packs/{name}/agents/common-pitfalls.md` đã có trong Referenced Docs (category `pitfalls`). |
72 | | -- Thiếu → issue `category: missing-pitfalls`, severity `warning` (KHÔNG block). |
73 | | -
|
74 | | -### Check 4 — Gap blocking severity |
75 | | -- Gap thuộc Contracts → BLOCKING. |
76 | | -- Gap thuộc Patterns và `intent.type == implement_feature` → BLOCKING. |
77 | | -- Gap thuộc Domain workflow và task touch domain logic → BLOCKING. |
78 | | -- Gap khác → NON-BLOCKING (warning trong `issues[]`). |
79 | | -
|
80 | | -Với mỗi gap, classify vào `blocking_hint: true|false` của entry `gaps[]` (đồng bộ với severity). |
81 | | -
|
82 | | -## Pass C — Emit verdict |
83 | | -
|
84 | | -- Nếu có ít nhất 1 issue `severity: blocking` → `verdict = BLOCK`. |
85 | | -- Ngược lại → `verdict = APPROVED` (có thể vẫn có `severity: warning` issues). |
86 | | -
|
87 | | -Sau khi có verdict, append section `## Plan Review` vào `current-task.md` (KHÔNG re-write toàn bộ, chỉ append nếu có warnings): |
88 | | -
|
89 | | -```md |
90 | | -## Plan Review |
91 | | -
|
92 | | -Verdict: APPROVED | BLOCK |
93 | | -- Patterns verified: {N} |
94 | | -- Contracts verified: {N} |
95 | | -- Components covered: {list} |
96 | | -- Blocking gaps: {N} |
97 | | -- Warnings: {N} |
| 10 | +```bash |
| 11 | +contextd context "{user_task}" --format json |
98 | 12 | ``` |
99 | 13 |
|
100 | | -(BLOCK → main agent sẽ STOP và báo user; APPROVED có warnings → main agent đọc warnings để xử lý gracefully.) |
101 | | -
|
102 | | -# Context File Template (Write tới `.contextd/context/current-task.md`) |
103 | | -
|
104 | | -```md |
105 | | -# Wiki Context — {mô tả ngắn task} |
| 14 | +The CLI resolves workspace config, classifies intent, retrieves and slices deterministic context, validates refs, emits JSON, renders markdown, and materializes the static context pack. |
106 | 15 |
|
107 | | -Generated: {ISO datetime} |
108 | | -Workspace: {intent.workspace} |
109 | | -Packs: {comma-separated active_packs từ intent, hoặc "(none)"} |
110 | | -Run ID: {intent.run_id} |
| 16 | +# Inputs |
111 | 17 |
|
112 | | -## Intent |
| 18 | +| Field | Description | |
| 19 | +|-------|-------------| |
| 20 | +| `project_dir` | Current project root. | |
| 21 | +| `user_task` | Original user task. | |
| 22 | +| `artifact_path` | Optional; default `{project_dir}/.contextd/context/current-task.json`. | |
113 | 23 |
|
114 | | -| Field | Value | |
115 | | -|-------|-------| |
116 | | -| type | {intent.type} | |
117 | | -| domain | {intent.domain} | |
118 | | -| components | {intent.components} | |
119 | | -| scope | {intent.scope} | |
120 | | -| patterns_needed | {intent.patterns_needed} | |
121 | | -
|
122 | | -## Referenced Docs (priority order) |
123 | | -
|
124 | | -| # | Category | File | Sections sliced | |
125 | | -|---|----------|------|----------------| |
126 | | -
|
127 | | -## Extracted Context |
128 | | -
|
129 | | -### [contract] {file} |
130 | | -{slice} |
131 | | -
|
132 | | ---- |
133 | | -
|
134 | | -### [pattern] {file} |
135 | | -{slice} |
136 | | -
|
137 | | -## Knowledge Gaps |
138 | | -
|
139 | | -- {file thiếu hoặc "(none)"} |
140 | | -
|
141 | | -## Plan Review |
142 | | -
|
143 | | -Verdict: APPROVED | BLOCK |
144 | | -- Patterns verified: {N} |
145 | | -- Contracts verified: {N} |
146 | | -- Components covered: {list} |
147 | | -- Blocking gaps: {N} |
148 | | -- Warnings: {N} |
149 | | -``` |
150 | | -
|
151 | | -# Output (sau khi đã Write context file) |
152 | | -
|
153 | | -Output gồm **2 phần theo thứ tự**: |
154 | | -
|
155 | | -## Phần A — Markdown verdict ngắn (1 dòng) |
156 | | -
|
157 | | -``` |
158 | | -APPROVED |
159 | | -``` |
160 | | -hoặc |
161 | | -``` |
162 | | -BLOCK: {short reason} |
163 | | -``` |
164 | | -
|
165 | | -Nếu APPROVED kèm warnings, có thể thêm `## Warnings` block phía dưới với bullet list ngắn (tối đa 5 bullet). |
166 | | -
|
167 | | -## Phần B — Trace JSON (cuối output, đúng 1 fenced ```json block) |
168 | | -
|
169 | | -Shape theo canonical schema [run-trace.schema.json](../../templates/run-trace.schema.json) `oneOf[1]` (stage `02-context`). KHÔNG restate fields ở đây — đọc schema để biết required. |
170 | | -
|
171 | | -Quick recap: |
172 | | -- Common: `run_id`, `stage: "02-context"`, `ts`, `workspace_at_run`. |
173 | | -- Retrieval: `context_file`, `referenced_docs[]` (mỗi entry `{category, path, sections}`; category ∈ contract|pattern|project|domain|decision|runbook|pitfalls), `gaps[]` (`{category, missing, blocking_hint}`), `file_count`, `gap_count`, `total_chars`. |
174 | | -- Plan verdict: `verdict` (`APPROVED|BLOCK`, **required**), `issues[]` (`{id, category, severity, detail, evidence?}`), `checks_summary` (`patterns_verified`, `contracts_verified`, `components_covered[]`, `blocking_gaps`, `conflicts`). |
| 24 | +# Process |
175 | 25 |
|
176 | | -Caller dùng heuristic confirm: `Context written: {file_count} docs, {gap_count} gaps, verdict={verdict}`. PostToolUse hook ghi `{cwd}/.contextd/runs/{run_id}/02-context.json`. |
| 26 | +1. If the artifact does not exist, ask the caller to run `contextd context "{user_task}" --format json`. |
| 27 | +2. Read `.contextd/context/current-task.json`. |
| 28 | +3. Verify minimum shape: |
| 29 | + - `artifact_type == "contextd_task_context.v1"` |
| 30 | + - fields include `workspace`, `intent`, `referenced_docs`, `gaps`, `warnings`, `contextPack`, `retrieval_policy`, and `source_hashes` |
| 31 | +4. If humans need a rendered view, read `.contextd/context/current-task.md` only as markdown generated from JSON. Do not patch or rewrite it. |
| 32 | +5. Emit a short verdict: |
| 33 | + - `BLOCK` if the artifact has blocking gaps for contracts, patterns, or domain workflow. |
| 34 | + - `APPROVED` if no blocking gap exists. |
177 | 35 |
|
178 | 36 | # Hard constraints |
179 | 37 |
|
180 | | -- CHỈ retrieve file trong `{ws}/`. Bất kỳ path nào ngoài `workspaces/{intent.workspace}/` → KHÔNG đọc. |
181 | | -- Tối đa 7 file trong bảng Referenced Docs. |
182 | | -- KHÔNG sinh code, KHÔNG đưa ra recommendation. Verification chỉ flag vấn đề, KHÔNG đề xuất fix. |
183 | | -- KHÔNG đọc full pattern file nếu chỉ cần 1 section. |
184 | | -- File thiếu → ghi `Knowledge Gaps`, KHÔNG bịa nội dung thay thế. |
185 | | -- Write CHỈ vào `{project_dir}/.contextd/context/current-task.md`. KHÔNG Write nơi khác (trace là việc của hook). |
186 | | -- Verdict logic dựa thuần vào issues: ≥1 blocking → BLOCK, else APPROVED. KHÔNG override bằng "feeling". |
187 | | -- Output cuối phải có đúng 1 fenced ```json block với schema 02-context (gồm `verdict` field). KHÔNG có block ```json khác. |
188 | | -- Khi không chắc một thứ là conflict thật → bỏ qua, KHÔNG đẩy vào `issues[]`. Selector là quick gate, không phải critic sâu — đó là việc của `contextd-reviewer` ở Stage 4.''' |
| 38 | +- Do not run a parallel retrieval, ranking, slicing, or plan-review pipeline. |
| 39 | +- Do not write `.contextd/context/current-task.md`. |
| 40 | +- Do not create another source of truth besides `.contextd/context/current-task.json`. |
| 41 | +- Do not read another workspace. |
| 42 | +- Do not override deterministic `referenced_docs` with `contextd find` or RAG/search output. |
| 43 | +''' |
0 commit comments