moai-adk 0.4.8__py3-none-any.whl → 0.4.11__py3-none-any.whl
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.
Potentially problematic release.
This version of moai-adk might be problematic. Click here for more details.
- moai_adk/templates/.claude/hooks/alfred/HOOK_SCHEMA_VALIDATION.md +313 -0
- moai_adk/templates/.claude/hooks/alfred/alfred_hooks.py +1 -0
- moai_adk/templates/.claude/hooks/alfred/core/__init__.py +32 -29
- moai_adk/templates/.claude/hooks/alfred/core/tags.py +198 -0
- moai_adk/templates/.claude/hooks/alfred/handlers/tool.py +34 -14
- moai_adk/templates/.claude/hooks/alfred/test_hook_output.py +175 -0
- moai_adk/templates/.claude/skills/moai-cc-hooks/scripts/validate-bash-command.py +2 -2
- moai_adk/templates/CLAUDE.md +72 -38
- {moai_adk-0.4.8.dist-info → moai_adk-0.4.11.dist-info}/METADATA +44 -25
- {moai_adk-0.4.8.dist-info → moai_adk-0.4.11.dist-info}/RECORD +13 -10
- {moai_adk-0.4.8.dist-info → moai_adk-0.4.11.dist-info}/WHEEL +0 -0
- {moai_adk-0.4.8.dist-info → moai_adk-0.4.11.dist-info}/entry_points.txt +0 -0
- {moai_adk-0.4.8.dist-info → moai_adk-0.4.11.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
# Hook JSON 스키마 검증 및 해결 보고서
|
|
2
|
+
|
|
3
|
+
**작성일**: 2025-10-23
|
|
4
|
+
**태그**: @CODE:HOOKS-REFACTOR-001
|
|
5
|
+
**상태**: ✅ 해결 완료
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 📋 문제 요약
|
|
10
|
+
|
|
11
|
+
### 초기 오류
|
|
12
|
+
```
|
|
13
|
+
SessionStart:startup hook error: JSON validation failed: Hook JSON output validation failed
|
|
14
|
+
Expected schema: { ... "systemMessage": ... }
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### 근본 원인
|
|
18
|
+
Claude Code Hook 스키마에서 `systemMessage`가 **최상위 필드**여야 하지만, 일부 구현에서는 이를 `hookSpecificOutput` 내부에 중첩시키고 있었습니다.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## 🔍 분석 결과
|
|
23
|
+
|
|
24
|
+
### Claude Code 공식 Hook 스키마
|
|
25
|
+
|
|
26
|
+
#### 1. 일반 Hook 이벤트 (SessionStart, PreToolUse, PostToolUse, SessionEnd 등)
|
|
27
|
+
|
|
28
|
+
```json
|
|
29
|
+
{
|
|
30
|
+
"continue": true|false, // ✅ 기본 필드
|
|
31
|
+
"systemMessage": "string", // ✅ 최상위 필드 (NOT in hookSpecificOutput)
|
|
32
|
+
"decision": "approve"|"block"|undefined, // ✅ 선택적
|
|
33
|
+
"reason": "string", // ✅ 선택적
|
|
34
|
+
"permissionDecision": "allow"|"deny"|"ask"|undefined, // ✅ 선택적
|
|
35
|
+
"suppressOutput": true|false // ✅ 선택적
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
#### 2. UserPromptSubmit 전용 스키마
|
|
40
|
+
|
|
41
|
+
```json
|
|
42
|
+
{
|
|
43
|
+
"continue": true,
|
|
44
|
+
"hookSpecificOutput": { // ✅ UserPromptSubmit에만 사용
|
|
45
|
+
"hookEventName": "UserPromptSubmit",
|
|
46
|
+
"additionalContext": "string"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 핵심 규칙
|
|
52
|
+
|
|
53
|
+
| 규칙 | 설명 |
|
|
54
|
+
|------|------|
|
|
55
|
+
| **systemMessage 위치** | 최상위 필드 (`output["systemMessage"]`) |
|
|
56
|
+
| **hookSpecificOutput** | UserPromptSubmit 전용 |
|
|
57
|
+
| **내부 필드** | `context_files`, `suggestions`, `exit_code`는 Python 로직용 (JSON 출력 제외) |
|
|
58
|
+
| **JSON 직렬화** | 모든 필드는 JSON 직렬화 가능해야 함 |
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## ✅ 해결 방안
|
|
63
|
+
|
|
64
|
+
### 1. 코드 수정
|
|
65
|
+
|
|
66
|
+
**파일**: `.claude/hooks/alfred/core/__init__.py`
|
|
67
|
+
|
|
68
|
+
#### `to_dict()` 메서드 (라인 63-118)
|
|
69
|
+
```python
|
|
70
|
+
def to_dict(self) -> dict[str, Any]:
|
|
71
|
+
"""Claude Code 표준 Hook 출력 스키마로 변환"""
|
|
72
|
+
output: dict[str, Any] = {}
|
|
73
|
+
|
|
74
|
+
# 1. decision 또는 continue 추가
|
|
75
|
+
if self.decision:
|
|
76
|
+
output["decision"] = self.decision
|
|
77
|
+
else:
|
|
78
|
+
output["continue"] = self.continue_execution
|
|
79
|
+
|
|
80
|
+
# 2. reason 추가 (decision 또는 permissionDecision과 함께)
|
|
81
|
+
if self.reason:
|
|
82
|
+
output["reason"] = self.reason
|
|
83
|
+
|
|
84
|
+
# 3. suppressOutput 추가 (True인 경우만)
|
|
85
|
+
if self.suppress_output:
|
|
86
|
+
output["suppressOutput"] = True
|
|
87
|
+
|
|
88
|
+
# 4. permissionDecision 추가
|
|
89
|
+
if self.permission_decision:
|
|
90
|
+
output["permissionDecision"] = self.permission_decision
|
|
91
|
+
|
|
92
|
+
# 5. ⭐ systemMessage를 최상위 필드로 추가 (NOT in hookSpecificOutput)
|
|
93
|
+
if self.system_message:
|
|
94
|
+
output["systemMessage"] = self.system_message
|
|
95
|
+
|
|
96
|
+
# 🚫 내부 필드는 JSON 출력에서 제외
|
|
97
|
+
# - context_files: JIT 문맥 로드 (내부용)
|
|
98
|
+
# - suggestions: 제안 (내부용)
|
|
99
|
+
# - exit_code: 진단 (내부용)
|
|
100
|
+
|
|
101
|
+
return output
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
#### `to_user_prompt_submit_dict()` 메서드 (라인 120-160)
|
|
105
|
+
```python
|
|
106
|
+
def to_user_prompt_submit_dict(self) -> dict[str, Any]:
|
|
107
|
+
"""UserPromptSubmit Hook 전용 스키마"""
|
|
108
|
+
if self.context_files:
|
|
109
|
+
context_str = "\n".join([f"📎 Context: {f}" for f in self.context_files])
|
|
110
|
+
else:
|
|
111
|
+
context_str = ""
|
|
112
|
+
|
|
113
|
+
if self.system_message:
|
|
114
|
+
if context_str:
|
|
115
|
+
context_str = f"{self.system_message}\n\n{context_str}"
|
|
116
|
+
else:
|
|
117
|
+
context_str = self.system_message
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
"continue": self.continue_execution,
|
|
121
|
+
"hookSpecificOutput": {
|
|
122
|
+
"hookEventName": "UserPromptSubmit",
|
|
123
|
+
"additionalContext": context_str
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### 2. 설정 검증
|
|
129
|
+
|
|
130
|
+
**파일**: `.claude/settings.json` (라인 8-60)
|
|
131
|
+
|
|
132
|
+
```json
|
|
133
|
+
{
|
|
134
|
+
"hooks": {
|
|
135
|
+
"SessionStart": [
|
|
136
|
+
{
|
|
137
|
+
"hooks": [
|
|
138
|
+
{
|
|
139
|
+
"command": "uv run \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/alfred/alfred_hooks.py SessionStart",
|
|
140
|
+
"type": "command"
|
|
141
|
+
}
|
|
142
|
+
]
|
|
143
|
+
}
|
|
144
|
+
],
|
|
145
|
+
"UserPromptSubmit": [
|
|
146
|
+
{
|
|
147
|
+
"hooks": [
|
|
148
|
+
{
|
|
149
|
+
"command": "uv run \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/alfred/alfred_hooks.py UserPromptSubmit",
|
|
150
|
+
"type": "command"
|
|
151
|
+
}
|
|
152
|
+
]
|
|
153
|
+
}
|
|
154
|
+
]
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## 🧪 검증 결과
|
|
162
|
+
|
|
163
|
+
### 1. 자동 테스트 (8/8 통과)
|
|
164
|
+
|
|
165
|
+
**파일**: `.claude/hooks/alfred/test_hook_output.py`
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
$ cd .claude/hooks/alfred && python test_hook_output.py
|
|
169
|
+
|
|
170
|
+
✅ Test 1: Basic output - PASSED
|
|
171
|
+
✅ Test 2: systemMessage (top-level) - PASSED
|
|
172
|
+
✅ Test 3: decision + reason - PASSED
|
|
173
|
+
✅ Test 4: UserPromptSubmit schema - PASSED
|
|
174
|
+
✅ Test 5: permissionDecision - PASSED
|
|
175
|
+
✅ Test 6: SessionStart typical output - PASSED
|
|
176
|
+
✅ Test 7: JSON serializable - PASSED
|
|
177
|
+
✅ Test 8: UserPromptSubmit with system_message - PASSED
|
|
178
|
+
|
|
179
|
+
✅ ALL 8 TESTS PASSED
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### 2. 실제 Hook 실행 검증
|
|
183
|
+
|
|
184
|
+
#### SessionStart (compact phase)
|
|
185
|
+
```bash
|
|
186
|
+
$ echo '{"cwd": ".", "phase": "compact"}' | uv run .claude/hooks/alfred/alfred_hooks.py SessionStart
|
|
187
|
+
|
|
188
|
+
{
|
|
189
|
+
"continue": true,
|
|
190
|
+
"systemMessage": "🚀 MoAI-ADK Session Started\n Language: python\n Branch: develop (d905363)\n Changes: 215\n SPEC Progress: 30/31 (96%)\n Checkpoints: 2 available\n - delete-20251022-134841\n - critical-file-20251019-230247\n Restore: /alfred:0-project restore"
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
✅ **검증**:
|
|
195
|
+
- `systemMessage`가 최상위 필드
|
|
196
|
+
- JSON 유효성 확인
|
|
197
|
+
- `hookSpecificOutput` 없음 (올바름)
|
|
198
|
+
|
|
199
|
+
#### SessionStart (clear phase)
|
|
200
|
+
```bash
|
|
201
|
+
$ echo '{"cwd": ".", "phase": "clear"}' | uv run .claude/hooks/alfred/alfred_hooks.py SessionStart
|
|
202
|
+
|
|
203
|
+
{"continue": true}
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
✅ **검증**:
|
|
207
|
+
- 최소 스키마 (continue만)
|
|
208
|
+
- clear 단계에서 중복 출력 방지
|
|
209
|
+
|
|
210
|
+
#### UserPromptSubmit
|
|
211
|
+
```bash
|
|
212
|
+
$ echo '{"cwd": ".", "userPrompt": "test"}' | uv run .claude/hooks/alfred/alfred_hooks.py UserPromptSubmit
|
|
213
|
+
|
|
214
|
+
{
|
|
215
|
+
"continue": true,
|
|
216
|
+
"hookSpecificOutput": {
|
|
217
|
+
"hookEventName": "UserPromptSubmit",
|
|
218
|
+
"additionalContext": "📎 Loaded 1 context file(s)\n\n📎 Context: tests/"
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
✅ **검증**:
|
|
224
|
+
- UserPromptSubmit 특수 스키마
|
|
225
|
+
- `hookSpecificOutput` 사용 (올바름)
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
## 📚 각 Hook 이벤트별 스키마 가이드
|
|
230
|
+
|
|
231
|
+
| 이벤트 | 최소 JSON | 예시 | 차단 가능 |
|
|
232
|
+
|--------|-----------|------|----------|
|
|
233
|
+
| **SessionStart** | `{"continue": true}` | 프로젝트 상태 표시 | ❌ No |
|
|
234
|
+
| **SessionEnd** | `{"continue": true}` | 정리 작업 | ❌ No |
|
|
235
|
+
| **PreToolUse** | `{"continue": true}` | 도구 실행 승인/차단 | ✅ Yes |
|
|
236
|
+
| **PostToolUse** | `{"continue": true}` | 도구 실행 후 피드백 | ❌ No* |
|
|
237
|
+
| **UserPromptSubmit** | 특수 스키마 | 프롬프트 문맥 추가 | ✅ Yes |
|
|
238
|
+
| **Notification** | `{"continue": true}` | 알림 처리 | ❌ No |
|
|
239
|
+
| **Stop** | `{"continue": true}` | 종료 차단 | ✅ Yes |
|
|
240
|
+
| **SubagentStop** | `{"continue": true}` | 서브에이전트 종료 차단 | ✅ Yes |
|
|
241
|
+
|
|
242
|
+
*: PostToolUse는 도구가 이미 실행되었으므로 차단 불가능하지만, 피드백 제공 가능
|
|
243
|
+
|
|
244
|
+
---
|
|
245
|
+
|
|
246
|
+
## 🔧 구현 세부사항
|
|
247
|
+
|
|
248
|
+
### HookResult 클래스 필드
|
|
249
|
+
|
|
250
|
+
```python
|
|
251
|
+
@dataclass
|
|
252
|
+
class HookResult:
|
|
253
|
+
# ✅ Claude Code 표준 필드 (JSON에 포함)
|
|
254
|
+
continue_execution: bool = True
|
|
255
|
+
suppress_output: bool = False
|
|
256
|
+
decision: Literal["approve", "block"] | None = None
|
|
257
|
+
reason: str | None = None
|
|
258
|
+
permission_decision: Literal["allow", "deny", "ask"] | None = None
|
|
259
|
+
system_message: str | None = None # ⭐ TOP-LEVEL in JSON
|
|
260
|
+
|
|
261
|
+
# 🚫 내부 필드 (JSON 출력 제외)
|
|
262
|
+
context_files: list[str] = field(default_factory=list)
|
|
263
|
+
suggestions: list[str] = field(default_factory=list)
|
|
264
|
+
exit_code: int = 0
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
### 메서드별 역할
|
|
268
|
+
|
|
269
|
+
| 메서드 | 사용 사건 | 반환 스키마 |
|
|
270
|
+
|--------|---------|----------|
|
|
271
|
+
| `to_dict()` | 일반 Hook 이벤트 | 표준 Claude Code 스키마 |
|
|
272
|
+
| `to_user_prompt_submit_dict()` | UserPromptSubmit 이벤트 | 특수 스키마 + hookSpecificOutput |
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
## 📖 참고 문서
|
|
277
|
+
|
|
278
|
+
### 공식 Claude Code 문서
|
|
279
|
+
- **Claude Code Hooks**: https://docs.claude.com/en/docs/claude-code/hooks
|
|
280
|
+
- **Hook Output Schema**: https://docs.claude.com/en/docs/claude-code/hooks#output-schema
|
|
281
|
+
|
|
282
|
+
### Context7 참고 자료
|
|
283
|
+
- **Claude Code Hooks Mastery** (Trust Score: 8.3, 100+ 코드 스니펫)
|
|
284
|
+
- **Claude Code Templates** (Trust Score: 10)
|
|
285
|
+
|
|
286
|
+
### 프로젝트 문서
|
|
287
|
+
- **CLAUDE.md**: `Error Message Standard (Shared)` 섹션
|
|
288
|
+
- **Hook 구현**: `.claude/hooks/alfred/handlers/` 디렉토리
|
|
289
|
+
|
|
290
|
+
---
|
|
291
|
+
|
|
292
|
+
## 🎯 결론
|
|
293
|
+
|
|
294
|
+
✅ **상태**: 해결 완료
|
|
295
|
+
✅ **검증**: 8/8 자동 테스트 통과
|
|
296
|
+
✅ **실제 실행**: 모든 Hook 이벤트 정상 작동
|
|
297
|
+
|
|
298
|
+
### 핵심 수정사항
|
|
299
|
+
1. `systemMessage`를 최상위 필드로 이동 (NOT in hookSpecificOutput)
|
|
300
|
+
2. UserPromptSubmit 특수 스키마 분리
|
|
301
|
+
3. 내부 필드 JSON 출력 제외
|
|
302
|
+
4. 모든 Hook 이벤트 스키마 정규화
|
|
303
|
+
|
|
304
|
+
### 다음 단계
|
|
305
|
+
- ✅ Hook 스키마 검증 자동화
|
|
306
|
+
- ✅ 테스트 스크립트 작성
|
|
307
|
+
- ⏭️ 현재 상태 유지 및 모니터링
|
|
308
|
+
|
|
309
|
+
---
|
|
310
|
+
|
|
311
|
+
**검증 완료**: 2025-10-23
|
|
312
|
+
**담당자**: @agent-cc-manager
|
|
313
|
+
**참고**: @CODE:HOOKS-REFACTOR-001
|
|
@@ -28,18 +28,23 @@ class HookResult:
|
|
|
28
28
|
Attributes conform to Claude Code Hook output specification:
|
|
29
29
|
https://docs.claude.com/en/docs/claude-code/hooks
|
|
30
30
|
|
|
31
|
-
Standard Fields (Claude Code schema):
|
|
31
|
+
Standard Fields (Claude Code schema - included in JSON output):
|
|
32
32
|
continue_execution: Allow execution to continue (default True)
|
|
33
33
|
suppress_output: Suppress hook output display (default False)
|
|
34
34
|
decision: "approve" or "block" operation (optional)
|
|
35
35
|
reason: Explanation for decision (optional)
|
|
36
36
|
permission_decision: "allow", "deny", or "ask" (optional)
|
|
37
|
+
system_message: Message displayed to user (top-level field)
|
|
37
38
|
|
|
38
|
-
MoAI-ADK
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
Internal Fields (MoAI-ADK only - NOT in JSON output):
|
|
40
|
+
context_files: List of context files to load (internal use only)
|
|
41
|
+
suggestions: Suggestions for user (internal use only)
|
|
42
|
+
exit_code: Exit code for diagnostics (internal use only)
|
|
43
|
+
|
|
44
|
+
Note:
|
|
45
|
+
- systemMessage appears at TOP LEVEL in JSON output
|
|
46
|
+
- hookSpecificOutput is ONLY used for UserPromptSubmit events
|
|
47
|
+
- Internal fields are used for Python logic but not serialized to JSON
|
|
43
48
|
"""
|
|
44
49
|
|
|
45
50
|
# Claude Code standard fields
|
|
@@ -60,8 +65,10 @@ class HookResult:
|
|
|
60
65
|
|
|
61
66
|
Returns:
|
|
62
67
|
Dictionary conforming to Claude Code Hook specification with:
|
|
63
|
-
- Top-level fields: continue, suppressOutput, decision, reason,
|
|
64
|
-
|
|
68
|
+
- Top-level fields: continue, suppressOutput, decision, reason,
|
|
69
|
+
permissionDecision, systemMessage
|
|
70
|
+
- MoAI-ADK internal fields (context_files, suggestions, exit_code)
|
|
71
|
+
are NOT included in JSON output (used for internal logic only)
|
|
65
72
|
|
|
66
73
|
Examples:
|
|
67
74
|
>>> result = HookResult(continue_execution=True)
|
|
@@ -72,20 +79,27 @@ class HookResult:
|
|
|
72
79
|
>>> result.to_dict()
|
|
73
80
|
{'decision': 'block', 'reason': 'Dangerous'}
|
|
74
81
|
|
|
75
|
-
>>> result = HookResult(system_message="Test"
|
|
82
|
+
>>> result = HookResult(system_message="Test")
|
|
76
83
|
>>> result.to_dict()
|
|
77
|
-
{'continue': True, '
|
|
84
|
+
{'continue': True, 'systemMessage': 'Test'}
|
|
85
|
+
|
|
86
|
+
Note:
|
|
87
|
+
- systemMessage is a TOP-LEVEL field (not nested in hookSpecificOutput)
|
|
88
|
+
- hookSpecificOutput is ONLY used for UserPromptSubmit events
|
|
89
|
+
- context_files, suggestions, exit_code are internal-only fields
|
|
78
90
|
"""
|
|
79
91
|
output: dict[str, Any] = {}
|
|
80
92
|
|
|
81
93
|
# Add decision or continue flag
|
|
82
94
|
if self.decision:
|
|
83
95
|
output["decision"] = self.decision
|
|
84
|
-
if self.reason:
|
|
85
|
-
output["reason"] = self.reason
|
|
86
96
|
else:
|
|
87
97
|
output["continue"] = self.continue_execution
|
|
88
98
|
|
|
99
|
+
# Add reason if provided (works with both decision and permissionDecision)
|
|
100
|
+
if self.reason:
|
|
101
|
+
output["reason"] = self.reason
|
|
102
|
+
|
|
89
103
|
# Add suppressOutput if True
|
|
90
104
|
if self.suppress_output:
|
|
91
105
|
output["suppressOutput"] = True
|
|
@@ -94,24 +108,12 @@ class HookResult:
|
|
|
94
108
|
if self.permission_decision:
|
|
95
109
|
output["permissionDecision"] = self.permission_decision
|
|
96
110
|
|
|
97
|
-
#
|
|
98
|
-
hook_output: dict[str, Any] = {}
|
|
99
|
-
|
|
111
|
+
# Add systemMessage at TOP LEVEL (required by Claude Code schema)
|
|
100
112
|
if self.system_message:
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if self.context_files:
|
|
104
|
-
hook_output["contextFiles"] = self.context_files
|
|
105
|
-
|
|
106
|
-
if self.suggestions:
|
|
107
|
-
hook_output["suggestions"] = self.suggestions
|
|
108
|
-
|
|
109
|
-
if self.exit_code != 0:
|
|
110
|
-
hook_output["exitCode"] = self.exit_code
|
|
113
|
+
output["systemMessage"] = self.system_message
|
|
111
114
|
|
|
112
|
-
#
|
|
113
|
-
|
|
114
|
-
output["hookSpecificOutput"] = hook_output
|
|
115
|
+
# Note: context_files, suggestions, exit_code are internal-only fields
|
|
116
|
+
# and are NOT included in the JSON output per Claude Code schema
|
|
115
117
|
|
|
116
118
|
return output
|
|
117
119
|
|
|
@@ -134,7 +136,8 @@ class HookResult:
|
|
|
134
136
|
Examples:
|
|
135
137
|
>>> result = HookResult(context_files=["tests/"])
|
|
136
138
|
>>> result.to_user_prompt_submit_dict()
|
|
137
|
-
{'continue': True, 'hookSpecificOutput':
|
|
139
|
+
{'continue': True, 'hookSpecificOutput': \
|
|
140
|
+
{'hookEventName': 'UserPromptSubmit', 'additionalContext': '📎 Context: tests/'}}
|
|
138
141
|
"""
|
|
139
142
|
# Convert context_files to additionalContext string
|
|
140
143
|
if self.context_files:
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# @CODE:HOOK-TAG-001 | SPEC: TBD | TEST: tests/hooks/test_tag_validation.py
|
|
3
|
+
"""TAG validation helpers for MoAI-ADK hooks
|
|
4
|
+
|
|
5
|
+
Fast checks used by PreToolUse/PostToolUse to nudge users when
|
|
6
|
+
new or modified files are missing required @TAG annotations.
|
|
7
|
+
|
|
8
|
+
Configurable rules with sensible defaults:
|
|
9
|
+
- Load patterns from .moai/tag-rules.json if present.
|
|
10
|
+
- Otherwise, apply default glob patterns (folder names are not hard-coded only).
|
|
11
|
+
|
|
12
|
+
Defaults (order matters; first match wins):
|
|
13
|
+
1) SPEC
|
|
14
|
+
- .moai/specs/**
|
|
15
|
+
- **/SPEC-*/spec.md
|
|
16
|
+
2) TEST
|
|
17
|
+
- **/*_test.py, **/test_*.py, **/*.test.* (ts,tsx,js,jsx,go,rs)
|
|
18
|
+
- **/*.spec.* (ts,tsx,js,jsx)
|
|
19
|
+
- tests/**
|
|
20
|
+
3) DOC
|
|
21
|
+
- docs/**/*.md, **/README.md, **/*.api.md
|
|
22
|
+
4) CODE
|
|
23
|
+
- Source extensions: .py,.ts,.tsx,.js,.jsx,.go,.rs,.java,.kt,.rb,.php,.c,.cpp,.cs,.swift,.scala
|
|
24
|
+
- Excluding TEST patterns
|
|
25
|
+
|
|
26
|
+
Notes:
|
|
27
|
+
- Best-effort: skip binary/large files and non-target paths
|
|
28
|
+
- Do not block execution; return a list of issues for messaging
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
from __future__ import annotations
|
|
32
|
+
|
|
33
|
+
import fnmatch
|
|
34
|
+
import json
|
|
35
|
+
import subprocess
|
|
36
|
+
from dataclasses import dataclass
|
|
37
|
+
from pathlib import Path
|
|
38
|
+
from typing import Iterable, List, Optional
|
|
39
|
+
|
|
40
|
+
DEFAULT_CODE_EXTS = (
|
|
41
|
+
".py", ".ts", ".tsx", ".js", ".jsx", ".go", ".rs",
|
|
42
|
+
".java", ".kt", ".rb", ".php", ".c", ".cpp", ".cs",
|
|
43
|
+
".swift", ".scala"
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass
|
|
48
|
+
class TagIssue:
|
|
49
|
+
path: str
|
|
50
|
+
expected: str # one of @SPEC, @TEST, @CODE, @DOC
|
|
51
|
+
reason: str
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass
|
|
55
|
+
class Rule:
|
|
56
|
+
include: List[str]
|
|
57
|
+
expect: str # '@SPEC:' | '@TEST:' | '@CODE:' | '@DOC:'
|
|
58
|
+
exclude: List[str]
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _load_rules(cwd: str) -> List[Rule]:
|
|
62
|
+
"""Load tag rules from .moai/tag-rules.json or return defaults.
|
|
63
|
+
|
|
64
|
+
Schema example:
|
|
65
|
+
{
|
|
66
|
+
"rules": [
|
|
67
|
+
{"include": ["**/*_test.py", "**/*.test.ts"], "expect": "@TEST:", "exclude": []},
|
|
68
|
+
{"include": ["docs/**/*.md", "**/README.md"], "expect": "@DOC:", "exclude": []}
|
|
69
|
+
]
|
|
70
|
+
}
|
|
71
|
+
"""
|
|
72
|
+
cfg = Path(cwd) / ".moai" / "tag-rules.json"
|
|
73
|
+
if cfg.exists():
|
|
74
|
+
try:
|
|
75
|
+
data = json.loads(cfg.read_text(encoding="utf-8"))
|
|
76
|
+
items = data.get("rules", [])
|
|
77
|
+
rules: List[Rule] = []
|
|
78
|
+
for it in items:
|
|
79
|
+
include = list(it.get("include", []))
|
|
80
|
+
expect = str(it.get("expect", ""))
|
|
81
|
+
exclude = list(it.get("exclude", []))
|
|
82
|
+
if include and expect in ("@SPEC:", "@TEST:", "@CODE:", "@DOC:"):
|
|
83
|
+
rules.append(Rule(include=include, expect=expect, exclude=exclude))
|
|
84
|
+
if rules:
|
|
85
|
+
return rules
|
|
86
|
+
except Exception:
|
|
87
|
+
pass
|
|
88
|
+
|
|
89
|
+
# Defaults (ordered)
|
|
90
|
+
return [
|
|
91
|
+
Rule(
|
|
92
|
+
include=[".moai/specs/**", "**/SPEC-*/spec.md"],
|
|
93
|
+
expect="@SPEC:",
|
|
94
|
+
exclude=[]
|
|
95
|
+
),
|
|
96
|
+
Rule(
|
|
97
|
+
include=[
|
|
98
|
+
"**/*_test.py", "**/test_*.py", "**/*.test.ts",
|
|
99
|
+
"**/*.test.tsx", "**/*.test.js", "**/*.test.jsx",
|
|
100
|
+
"**/*.test.go", "**/*.test.rs", "**/*.spec.ts",
|
|
101
|
+
"**/*.spec.tsx", "tests/**"
|
|
102
|
+
],
|
|
103
|
+
expect="@TEST:",
|
|
104
|
+
exclude=[".claude/**"]
|
|
105
|
+
),
|
|
106
|
+
Rule(
|
|
107
|
+
include=["docs/**/*.md", "**/README.md", "**/*.api.md"],
|
|
108
|
+
expect="@DOC:",
|
|
109
|
+
exclude=[".claude/**"]
|
|
110
|
+
),
|
|
111
|
+
Rule(
|
|
112
|
+
include=["**/*"],
|
|
113
|
+
expect="@CODE:",
|
|
114
|
+
exclude=[
|
|
115
|
+
"tests/**", "docs/**", ".moai/**", ".claude/**",
|
|
116
|
+
"**/*.md", "**/*.json", "**/*.yml", "**/*.yaml",
|
|
117
|
+
"**/*.toml", "**/*.lock", "**/*.svg", "**/*.png",
|
|
118
|
+
"**/*.jpg", "**/*.jpeg", "**/*.gif"
|
|
119
|
+
]
|
|
120
|
+
),
|
|
121
|
+
]
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _match_any(path: str, patterns: List[str]) -> bool:
|
|
125
|
+
return any(fnmatch.fnmatch(path, pat) for pat in patterns)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _needs_tag_str(path_str: str, rules: List[Rule]) -> Optional[str]:
|
|
129
|
+
p = path_str
|
|
130
|
+
for rule in rules:
|
|
131
|
+
if _match_any(p, rule.include) and not _match_any(p, rule.exclude):
|
|
132
|
+
if rule.expect == "@CODE:":
|
|
133
|
+
# CODE: limit to source-like extensions to reduce noise
|
|
134
|
+
if not any(p.endswith(ext) for ext in DEFAULT_CODE_EXTS):
|
|
135
|
+
continue
|
|
136
|
+
return rule.expect
|
|
137
|
+
return None
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _has_tag(content: str, expected: str) -> bool:
|
|
141
|
+
return expected in content
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _iter_recent_changes(cwd: str) -> Iterable[Path]:
|
|
145
|
+
root = Path(cwd)
|
|
146
|
+
try:
|
|
147
|
+
# Staged files
|
|
148
|
+
r1 = subprocess.run(
|
|
149
|
+
["git", "diff", "--name-only", "--cached"],
|
|
150
|
+
cwd=cwd, capture_output=True, text=True, timeout=1
|
|
151
|
+
)
|
|
152
|
+
# Modified (unstaged) tracked files
|
|
153
|
+
r2 = subprocess.run(
|
|
154
|
+
["git", "ls-files", "-m"],
|
|
155
|
+
cwd=cwd, capture_output=True, text=True, timeout=1
|
|
156
|
+
)
|
|
157
|
+
# Untracked (other) files respecting .gitignore
|
|
158
|
+
r3 = subprocess.run(
|
|
159
|
+
["git", "ls-files", "-o", "--exclude-standard"],
|
|
160
|
+
cwd=cwd, capture_output=True, text=True, timeout=1
|
|
161
|
+
)
|
|
162
|
+
names = set()
|
|
163
|
+
if r1.returncode == 0:
|
|
164
|
+
names.update([line.strip() for line in r1.stdout.splitlines() if line.strip()])
|
|
165
|
+
if r2.returncode == 0:
|
|
166
|
+
names.update([line.strip() for line in r2.stdout.splitlines() if line.strip()])
|
|
167
|
+
if r3.returncode == 0:
|
|
168
|
+
names.update([line.strip() for line in r3.stdout.splitlines() if line.strip()])
|
|
169
|
+
for n in names:
|
|
170
|
+
p = (root / n).resolve()
|
|
171
|
+
if p.is_file():
|
|
172
|
+
yield p
|
|
173
|
+
except Exception:
|
|
174
|
+
return []
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def scan_recent_changes_for_missing_tags(cwd: str) -> list[TagIssue]:
|
|
178
|
+
issues: list[TagIssue] = []
|
|
179
|
+
rules = _load_rules(cwd)
|
|
180
|
+
root = Path(cwd).resolve()
|
|
181
|
+
for path in _iter_recent_changes(cwd):
|
|
182
|
+
try:
|
|
183
|
+
content = path.read_text(encoding="utf-8", errors="ignore")
|
|
184
|
+
except Exception:
|
|
185
|
+
continue
|
|
186
|
+
# compute relative path once and use for matching/excluding
|
|
187
|
+
try:
|
|
188
|
+
rel = path.resolve().relative_to(root)
|
|
189
|
+
rel_s = rel.as_posix()
|
|
190
|
+
except Exception:
|
|
191
|
+
rel_s = path.name
|
|
192
|
+
|
|
193
|
+
expected = _needs_tag_str(rel_s, rules)
|
|
194
|
+
if not expected:
|
|
195
|
+
continue
|
|
196
|
+
if not _has_tag(content, expected):
|
|
197
|
+
issues.append(TagIssue(path=rel_s, expected=expected, reason="missing tag"))
|
|
198
|
+
return issues
|