xtrm-tools 2.4.1 → 2.4.2

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 (125) hide show
  1. package/README.md +15 -6
  2. package/cli/dist/index.cjs +738 -239
  3. package/cli/dist/index.cjs.map +1 -1
  4. package/cli/package.json +1 -1
  5. package/config/hooks.json +10 -0
  6. package/config/pi/extensions/core/adapter.ts +2 -14
  7. package/config/pi/extensions/core/guard-rules.ts +70 -0
  8. package/config/pi/extensions/core/session-state.ts +59 -0
  9. package/config/pi/extensions/main-guard.ts +10 -14
  10. package/config/pi/extensions/plan-mode/README.md +65 -0
  11. package/config/pi/extensions/plan-mode/index.ts +340 -0
  12. package/config/pi/extensions/plan-mode/utils.ts +168 -0
  13. package/config/pi/extensions/service-skills.ts +51 -7
  14. package/config/pi/extensions/session-flow.ts +117 -0
  15. package/hooks/beads-claim-sync.mjs +123 -2
  16. package/hooks/beads-compact-restore.mjs +41 -9
  17. package/hooks/beads-compact-save.mjs +36 -5
  18. package/hooks/beads-gate-messages.mjs +27 -1
  19. package/hooks/beads-stop-gate.mjs +58 -8
  20. package/hooks/guard-rules.mjs +86 -0
  21. package/hooks/hooks.json +28 -18
  22. package/hooks/main-guard.mjs +3 -21
  23. package/hooks/quality-check.cjs +1286 -0
  24. package/hooks/quality-check.py +345 -0
  25. package/hooks/session-state.mjs +138 -0
  26. package/package.json +2 -1
  27. package/project-skills/quality-gates/.claude/settings.json +1 -24
  28. package/skills/creating-service-skills/SKILL.md +433 -0
  29. package/skills/creating-service-skills/references/script_quality_standards.md +425 -0
  30. package/skills/creating-service-skills/references/service_skill_system_guide.md +278 -0
  31. package/skills/creating-service-skills/scripts/bootstrap.py +326 -0
  32. package/skills/creating-service-skills/scripts/deep_dive.py +304 -0
  33. package/skills/creating-service-skills/scripts/scaffolder.py +482 -0
  34. package/skills/scoping-service-skills/SKILL.md +231 -0
  35. package/skills/scoping-service-skills/scripts/scope.py +74 -0
  36. package/skills/sync-docs/SKILL.md +235 -0
  37. package/skills/sync-docs/evals/evals.json +89 -0
  38. package/skills/sync-docs/references/doc-structure.md +104 -0
  39. package/skills/sync-docs/references/schema.md +103 -0
  40. package/skills/sync-docs/scripts/context_gatherer.py +246 -0
  41. package/skills/sync-docs/scripts/doc_structure_analyzer.py +495 -0
  42. package/skills/sync-docs/scripts/validate_doc.py +365 -0
  43. package/skills/sync-docs-workspace/iteration-1/benchmark.json +293 -0
  44. package/skills/sync-docs-workspace/iteration-1/benchmark.md +13 -0
  45. package/skills/sync-docs-workspace/iteration-1/eval-doc-audit/eval_metadata.json +27 -0
  46. package/skills/sync-docs-workspace/iteration-1/eval-doc-audit/with_skill/outputs/result.md +210 -0
  47. package/skills/sync-docs-workspace/iteration-1/eval-doc-audit/with_skill/run-1/grading.json +28 -0
  48. package/skills/sync-docs-workspace/iteration-1/eval-doc-audit/with_skill/run-1/timing.json +1 -0
  49. package/skills/sync-docs-workspace/iteration-1/eval-doc-audit/without_skill/outputs/result.md +101 -0
  50. package/skills/sync-docs-workspace/iteration-1/eval-doc-audit/without_skill/run-1/grading.json +28 -0
  51. package/skills/sync-docs-workspace/iteration-1/eval-doc-audit/without_skill/run-1/timing.json +5 -0
  52. package/skills/sync-docs-workspace/iteration-1/eval-doc-audit/without_skill/timing.json +5 -0
  53. package/skills/sync-docs-workspace/iteration-1/eval-fix-mode/eval_metadata.json +27 -0
  54. package/skills/sync-docs-workspace/iteration-1/eval-fix-mode/with_skill/outputs/result.md +198 -0
  55. package/skills/sync-docs-workspace/iteration-1/eval-fix-mode/with_skill/run-1/grading.json +28 -0
  56. package/skills/sync-docs-workspace/iteration-1/eval-fix-mode/with_skill/run-1/timing.json +1 -0
  57. package/skills/sync-docs-workspace/iteration-1/eval-fix-mode/without_skill/outputs/result.md +94 -0
  58. package/skills/sync-docs-workspace/iteration-1/eval-fix-mode/without_skill/run-1/grading.json +28 -0
  59. package/skills/sync-docs-workspace/iteration-1/eval-fix-mode/without_skill/run-1/timing.json +1 -0
  60. package/skills/sync-docs-workspace/iteration-1/eval-sprint-closeout/eval_metadata.json +27 -0
  61. package/skills/sync-docs-workspace/iteration-1/eval-sprint-closeout/with_skill/outputs/result.md +237 -0
  62. package/skills/sync-docs-workspace/iteration-1/eval-sprint-closeout/with_skill/run-1/grading.json +28 -0
  63. package/skills/sync-docs-workspace/iteration-1/eval-sprint-closeout/with_skill/run-1/timing.json +1 -0
  64. package/skills/sync-docs-workspace/iteration-1/eval-sprint-closeout/without_skill/outputs/result.md +134 -0
  65. package/skills/sync-docs-workspace/iteration-1/eval-sprint-closeout/without_skill/run-1/grading.json +28 -0
  66. package/skills/sync-docs-workspace/iteration-1/eval-sprint-closeout/without_skill/run-1/timing.json +1 -0
  67. package/skills/sync-docs-workspace/iteration-2/benchmark.json +297 -0
  68. package/skills/sync-docs-workspace/iteration-2/benchmark.md +13 -0
  69. package/skills/sync-docs-workspace/iteration-2/eval-doc-audit/eval_metadata.json +27 -0
  70. package/skills/sync-docs-workspace/iteration-2/eval-doc-audit/with_skill/outputs/result.md +137 -0
  71. package/skills/sync-docs-workspace/iteration-2/eval-doc-audit/with_skill/run-1/grading.json +92 -0
  72. package/skills/sync-docs-workspace/iteration-2/eval-doc-audit/with_skill/run-1/timing.json +1 -0
  73. package/skills/sync-docs-workspace/iteration-2/eval-doc-audit/without_skill/outputs/result.md +134 -0
  74. package/skills/sync-docs-workspace/iteration-2/eval-doc-audit/without_skill/run-1/grading.json +86 -0
  75. package/skills/sync-docs-workspace/iteration-2/eval-doc-audit/without_skill/run-1/timing.json +1 -0
  76. package/skills/sync-docs-workspace/iteration-2/eval-fix-mode/eval_metadata.json +27 -0
  77. package/skills/sync-docs-workspace/iteration-2/eval-fix-mode/with_skill/outputs/result.md +193 -0
  78. package/skills/sync-docs-workspace/iteration-2/eval-fix-mode/with_skill/run-1/grading.json +72 -0
  79. package/skills/sync-docs-workspace/iteration-2/eval-fix-mode/with_skill/run-1/timing.json +1 -0
  80. package/skills/sync-docs-workspace/iteration-2/eval-fix-mode/without_skill/outputs/result.md +211 -0
  81. package/skills/sync-docs-workspace/iteration-2/eval-fix-mode/without_skill/run-1/grading.json +91 -0
  82. package/skills/sync-docs-workspace/iteration-2/eval-fix-mode/without_skill/run-1/timing.json +5 -0
  83. package/skills/sync-docs-workspace/iteration-2/eval-sprint-closeout/eval_metadata.json +27 -0
  84. package/skills/sync-docs-workspace/iteration-2/eval-sprint-closeout/with_skill/outputs/result.md +182 -0
  85. package/skills/sync-docs-workspace/iteration-2/eval-sprint-closeout/with_skill/run-1/grading.json +95 -0
  86. package/skills/sync-docs-workspace/iteration-2/eval-sprint-closeout/with_skill/run-1/timing.json +1 -0
  87. package/skills/sync-docs-workspace/iteration-2/eval-sprint-closeout/without_skill/outputs/result.md +222 -0
  88. package/skills/sync-docs-workspace/iteration-2/eval-sprint-closeout/without_skill/run-1/grading.json +88 -0
  89. package/skills/sync-docs-workspace/iteration-2/eval-sprint-closeout/without_skill/run-1/timing.json +5 -0
  90. package/skills/sync-docs-workspace/iteration-3/benchmark.json +298 -0
  91. package/skills/sync-docs-workspace/iteration-3/benchmark.md +13 -0
  92. package/skills/sync-docs-workspace/iteration-3/eval-doc-audit/eval_metadata.json +27 -0
  93. package/skills/sync-docs-workspace/iteration-3/eval-doc-audit/with_skill/outputs/result.md +125 -0
  94. package/skills/sync-docs-workspace/iteration-3/eval-doc-audit/with_skill/run-1/grading.json +97 -0
  95. package/skills/sync-docs-workspace/iteration-3/eval-doc-audit/with_skill/run-1/timing.json +5 -0
  96. package/skills/sync-docs-workspace/iteration-3/eval-doc-audit/without_skill/outputs/result.md +144 -0
  97. package/skills/sync-docs-workspace/iteration-3/eval-doc-audit/without_skill/run-1/grading.json +78 -0
  98. package/skills/sync-docs-workspace/iteration-3/eval-doc-audit/without_skill/run-1/timing.json +5 -0
  99. package/skills/sync-docs-workspace/iteration-3/eval-fix-mode/eval_metadata.json +27 -0
  100. package/skills/sync-docs-workspace/iteration-3/eval-fix-mode/with_skill/outputs/result.md +104 -0
  101. package/skills/sync-docs-workspace/iteration-3/eval-fix-mode/with_skill/run-1/grading.json +91 -0
  102. package/skills/sync-docs-workspace/iteration-3/eval-fix-mode/with_skill/run-1/timing.json +5 -0
  103. package/skills/sync-docs-workspace/iteration-3/eval-fix-mode/without_skill/outputs/result.md +79 -0
  104. package/skills/sync-docs-workspace/iteration-3/eval-fix-mode/without_skill/run-1/grading.json +82 -0
  105. package/skills/sync-docs-workspace/iteration-3/eval-fix-mode/without_skill/run-1/timing.json +5 -0
  106. package/skills/sync-docs-workspace/iteration-3/eval-sprint-closeout/eval_metadata.json +27 -0
  107. package/skills/sync-docs-workspace/iteration-3/eval-sprint-closeout/with_skill/outputs/phase1_context.json +302 -0
  108. package/skills/sync-docs-workspace/iteration-3/eval-sprint-closeout/with_skill/outputs/phase2_drift.txt +33 -0
  109. package/skills/sync-docs-workspace/iteration-3/eval-sprint-closeout/with_skill/outputs/phase3_analysis.json +114 -0
  110. package/skills/sync-docs-workspace/iteration-3/eval-sprint-closeout/with_skill/outputs/phase4_fix.txt +118 -0
  111. package/skills/sync-docs-workspace/iteration-3/eval-sprint-closeout/with_skill/outputs/phase5_validate.txt +38 -0
  112. package/skills/sync-docs-workspace/iteration-3/eval-sprint-closeout/with_skill/outputs/result.md +158 -0
  113. package/skills/sync-docs-workspace/iteration-3/eval-sprint-closeout/with_skill/run-1/grading.json +95 -0
  114. package/skills/sync-docs-workspace/iteration-3/eval-sprint-closeout/with_skill/run-1/timing.json +5 -0
  115. package/skills/sync-docs-workspace/iteration-3/eval-sprint-closeout/without_skill/outputs/result.md +71 -0
  116. package/skills/sync-docs-workspace/iteration-3/eval-sprint-closeout/without_skill/run-1/grading.json +90 -0
  117. package/skills/sync-docs-workspace/iteration-3/eval-sprint-closeout/without_skill/run-1/timing.json +5 -0
  118. package/skills/updating-service-skills/SKILL.md +136 -0
  119. package/skills/updating-service-skills/scripts/drift_detector.py +222 -0
  120. package/skills/using-quality-gates/SKILL.md +254 -0
  121. package/skills/using-service-skills/SKILL.md +108 -0
  122. package/skills/using-service-skills/scripts/cataloger.py +74 -0
  123. package/skills/using-service-skills/scripts/skill_activator.py +152 -0
  124. package/skills/using-service-skills/scripts/test_skill_activator.py +58 -0
  125. package/skills/using-xtrm/SKILL.md +34 -38
@@ -0,0 +1,345 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ Python Quality Gate - PostToolUse hook for Claude Code.
4
+ Runs ruff (linting/formatting) and mypy (type checking) on edited Python files.
5
+
6
+ Exit codes:
7
+ 0 - All checks passed
8
+ 1 - Fatal error
9
+ 2 - Blocking errors found (Claude must fix)
10
+ """
11
+
12
+ import json
13
+ import os
14
+ import sys
15
+ import subprocess
16
+ import shutil
17
+ from pathlib import Path
18
+
19
+ # Colors for output
20
+ class Colors:
21
+ RED = '\x1b[0;31m'
22
+ GREEN = '\x1b[0;32m'
23
+ YELLOW = '\x1b[0;33m'
24
+ BLUE = '\x1b[0;34m'
25
+ CYAN = '\x1b[0;36m'
26
+ RESET = '\x1b[0m'
27
+
28
+ def log_info(msg: str):
29
+ print(f"{Colors.BLUE}[INFO]{Colors.RESET} {msg}", file=sys.stderr)
30
+
31
+ def log_error(msg: str):
32
+ print(f"{Colors.RED}[ERROR]{Colors.RESET} {msg}", file=sys.stderr)
33
+
34
+ def log_success(msg: str):
35
+ print(f"{Colors.GREEN}[OK]{Colors.RESET} {msg}", file=sys.stderr)
36
+
37
+ def log_warning(msg: str):
38
+ print(f"{Colors.YELLOW}[WARN]{Colors.RESET} {msg}", file=sys.stderr)
39
+
40
+ def log_debug(msg: str):
41
+ if os.environ.get('CLAUDE_HOOKS_DEBUG', 'false').lower() == 'true':
42
+ print(f"{Colors.CYAN}[DEBUG]{Colors.RESET} {msg}", file=sys.stderr)
43
+
44
+ def find_project_root(file_path: str) -> str:
45
+ """Find project root by looking for pyproject.toml, setup.py, or .git"""
46
+ path = Path(file_path).parent
47
+ while path != path.parent:
48
+ if (path / 'pyproject.toml').exists() or \
49
+ (path / 'setup.py').exists() or \
50
+ (path / '.git').exists():
51
+ return str(path)
52
+ path = path.parent
53
+ return str(path)
54
+
55
+ def is_python_file(file_path: str) -> bool:
56
+ """Check if file is a Python source file"""
57
+ return file_path.endswith('.py') and not file_path.endswith('__init__.py')
58
+
59
+
60
+ def has_python_project_config(project_root: str) -> bool:
61
+ """Return True when Python quality checks are configured for this project."""
62
+ root = Path(project_root)
63
+ return (root / 'pyproject.toml').exists() or (root / '.python-version').exists()
64
+
65
+ def check_ruff(file_path: str, project_root: str, autofix: bool = False) -> tuple[bool, list[str], list[str]]:
66
+ """
67
+ Run ruff linting and formatting checks.
68
+ Returns: (passed, errors, autofixes)
69
+ """
70
+ errors = []
71
+ autofixes = []
72
+
73
+ # Check if ruff is available
74
+ ruff_path = shutil.which('ruff')
75
+ if not ruff_path:
76
+ log_debug('Ruff not found in PATH - skipping ruff checks')
77
+ return True, errors, autofixes
78
+
79
+ log_info('Running Ruff linting...')
80
+
81
+ # Run ruff check
82
+ cmd = ['ruff', 'check', '--output-format=full', file_path]
83
+ try:
84
+ result = subprocess.run(cmd, capture_output=True, text=True, cwd=project_root)
85
+
86
+ if result.returncode != 0:
87
+ if autofix:
88
+ log_warning('Ruff issues found, attempting auto-fix...')
89
+ fix_cmd = ['ruff', 'check', '--fix', file_path]
90
+ fix_result = subprocess.run(fix_cmd, capture_output=True, text=True, cwd=project_root)
91
+
92
+ if fix_result.returncode == 0:
93
+ log_success('Ruff auto-fixed all issues!')
94
+ autofixes.append('Ruff auto-fixed linting issues')
95
+ else:
96
+ errors.append(f'Ruff found issues that could not be auto-fixed')
97
+ errors.extend(result.stdout.strip().split('\n'))
98
+ else:
99
+ errors.append(f'Ruff found linting issues in {os.path.basename(file_path)}')
100
+ errors.extend(result.stdout.strip().split('\n'))
101
+ else:
102
+ log_success('Ruff linting passed')
103
+
104
+ except Exception as e:
105
+ log_debug(f'Ruff check error: {e}')
106
+
107
+ # Run ruff format check
108
+ log_info('Running Ruff format check...')
109
+ format_cmd = ['ruff', 'format', '--check', file_path]
110
+ try:
111
+ result = subprocess.run(format_cmd, capture_output=True, text=True, cwd=project_root)
112
+
113
+ if result.returncode != 0:
114
+ if autofix:
115
+ log_warning('Ruff format issues found, auto-formatting...')
116
+ format_fix_cmd = ['ruff', 'format', file_path]
117
+ format_fix_result = subprocess.run(format_fix_cmd, capture_output=True, text=True, cwd=project_root)
118
+
119
+ if format_fix_result.returncode == 0:
120
+ log_success('Ruff auto-formatted the file!')
121
+ autofixes.append('Ruff auto-formatted the file')
122
+ else:
123
+ errors.append(f'Ruff formatting issues in {os.path.basename(file_path)}')
124
+ else:
125
+ errors.append(f'Ruff formatting issues in {os.path.basename(file_path)}')
126
+ else:
127
+ log_success('Ruff formatting correct')
128
+
129
+ except Exception as e:
130
+ log_debug(f'Ruff format error: {e}')
131
+
132
+ return len(errors) == 0, errors, autofixes
133
+
134
+ def check_mypy(file_path: str, project_root: str) -> tuple[bool, list[str]]:
135
+ """
136
+ Run mypy type checking.
137
+ Returns: (passed, errors)
138
+ """
139
+ errors = []
140
+
141
+ # Check if mypy is available
142
+ mypy_path = shutil.which('mypy')
143
+ if not mypy_path:
144
+ log_debug('Mypy not found in PATH - skipping type checking')
145
+ return True, errors
146
+
147
+ log_info('Running Mypy type checking...')
148
+
149
+ # Build mypy command with strictness flags
150
+ # Default: --disallow-untyped-defs catches untyped function parameters
151
+ # Opt-in: CLAUDE_HOOKS_MYPY_STRICT=true enables full --strict mode
152
+ mypy_strict = os.environ.get('CLAUDE_HOOKS_MYPY_STRICT', 'false').lower() == 'true'
153
+
154
+ if mypy_strict:
155
+ cmd = ['mypy', '--strict', '--pretty', file_path]
156
+ log_debug('Running mypy with --strict (full strictness)')
157
+ else:
158
+ cmd = ['mypy', '--disallow-untyped-defs', '--pretty', file_path]
159
+ log_debug('Running mypy with --disallow-untyped-defs (baseline strictness)')
160
+ try:
161
+ result = subprocess.run(cmd, capture_output=True, text=True, cwd=project_root)
162
+
163
+ if result.returncode != 0:
164
+ errors.append(f'Mypy found type errors in {os.path.basename(file_path)}')
165
+ if result.stdout:
166
+ errors.extend(result.stdout.strip().split('\n'))
167
+ if result.stderr:
168
+ errors.extend(result.stderr.strip().split('\n'))
169
+ else:
170
+ log_success('Mypy type checking passed')
171
+
172
+ except Exception as e:
173
+ log_debug(f'Mypy error: {e}')
174
+
175
+ return len(errors) == 0, errors
176
+
177
+ def check_pytest_suggestions(file_path: str, project_root: str):
178
+ """Suggest running tests if test file exists"""
179
+ base_name = file_path.replace('.py', '')
180
+ test_paths = [
181
+ f'{base_name}_test.py',
182
+ f'{base_name}_tests.py',
183
+ f'test_{Path(file_path).name}',
184
+ ]
185
+
186
+ # Check same directory
187
+ for test_path in test_paths:
188
+ if Path(test_path).exists():
189
+ log_warning(f'💡 Related test found: {os.path.basename(test_path)}')
190
+ log_warning(' Consider running: pytest')
191
+ return
192
+
193
+ # Check __tests__ directory
194
+ tests_dir = Path(file_path).parent / '__tests__'
195
+ if tests_dir.exists():
196
+ for test_path in tests_dir.glob(f'test_{Path(file_path).name}'):
197
+ log_warning(f'💡 Related test found: __tests__/{test_path.name}')
198
+ log_warning(' Consider running: pytest')
199
+ return
200
+
201
+ log_warning(f'💡 No test file found for {os.path.basename(file_path)}')
202
+
203
+ def print_summary(errors: list[str], autofixes: list[str]):
204
+ """Print summary of errors and autofixes"""
205
+ if autofixes:
206
+ print(f'\n{Colors.BLUE}═══ Auto-fixes Applied ═══{Colors.RESET}', file=sys.stderr)
207
+ for fix in autofixes:
208
+ print(f'{Colors.GREEN}✨{Colors.RESET} {fix}', file=sys.stderr)
209
+ print(f'{Colors.GREEN}Automatically fixed {len(autofixes)} issue(s)!{Colors.RESET}', file=sys.stderr)
210
+
211
+ if errors:
212
+ print(f'\n{Colors.BLUE}═══ Quality Check Summary ═══{Colors.RESET}', file=sys.stderr)
213
+ for error in errors:
214
+ print(f'{Colors.RED}❌{Colors.RESET} {error}', file=sys.stderr)
215
+ print(f'\n{Colors.RED}Found {len(errors)} issue(s) that MUST be fixed!{Colors.RESET}', file=sys.stderr)
216
+ print(f'{Colors.RED}══════════════════════════════════════{Colors.RESET}', file=sys.stderr)
217
+ print(f'{Colors.RED}❌ ALL ISSUES ARE BLOCKING ❌{Colors.RESET}', file=sys.stderr)
218
+ print(f'{Colors.RED}══════════════════════════════════════{Colors.RESET}', file=sys.stderr)
219
+ print(f'{Colors.RED}Fix EVERYTHING above until all checks are ✅ GREEN{Colors.RESET}', file=sys.stderr)
220
+
221
+ def parse_json_input() -> dict:
222
+ """Parse JSON input from stdin"""
223
+ input_data = sys.stdin.read().strip()
224
+
225
+ if not input_data:
226
+ log_warning('No JSON input provided.')
227
+ print(f'\n{Colors.YELLOW}👉 Hook executed but no input to process.{Colors.RESET}', file=sys.stderr)
228
+ sys.exit(0)
229
+
230
+ try:
231
+ return json.loads(input_data)
232
+ except json.JSONDecodeError as e:
233
+ log_error(f'Failed to parse JSON: {e}')
234
+ sys.exit(1)
235
+
236
+ def extract_file_path(input_data: dict) -> str | None:
237
+ """Extract file path from tool input, including Serena relative_path."""
238
+ tool_input = input_data.get('tool_input', {})
239
+ file_path = (
240
+ tool_input.get('file_path')
241
+ or tool_input.get('path')
242
+ or tool_input.get('relative_path')
243
+ )
244
+ if not file_path:
245
+ return None
246
+
247
+ # Serena tools pass relative_path relative to the project root.
248
+ if not os.path.isabs(file_path):
249
+ project_root = os.environ.get('CLAUDE_PROJECT_DIR') or os.getcwd()
250
+ return str(Path(project_root) / file_path)
251
+
252
+ return file_path
253
+
254
+ def main():
255
+ """Main entry point"""
256
+ print('', file=sys.stderr)
257
+ print(f'📦 Python Quality Check - Starting...', file=sys.stderr)
258
+ print('─────────────────────────────────────', file=sys.stderr)
259
+
260
+ # Parse input
261
+ input_data = parse_json_input()
262
+ file_path = extract_file_path(input_data)
263
+
264
+ if not file_path:
265
+ log_warning('No file path found in JSON input.')
266
+ print(f'\n{Colors.YELLOW}👉 No file to check - tool may not be file-related.{Colors.RESET}', file=sys.stderr)
267
+ sys.exit(0)
268
+
269
+ # Check if file exists
270
+ if not Path(file_path).exists():
271
+ log_info(f'File does not exist: {file_path}')
272
+ print(f'\n{Colors.YELLOW}👉 File skipped - doesn\'t exist.{Colors.RESET}', file=sys.stderr)
273
+ sys.exit(0)
274
+
275
+ # Skip non-Python files
276
+ if not is_python_file(file_path):
277
+ log_info(f'Skipping non-Python file: {file_path}')
278
+ print(f'\n{Colors.GREEN}✅ No checks needed for {os.path.basename(file_path)}{Colors.RESET}', file=sys.stderr)
279
+ sys.exit(0)
280
+
281
+ # Update header
282
+ print('', file=sys.stderr)
283
+ print(f'🔍 Validating: {os.path.basename(file_path)}', file=sys.stderr)
284
+ print('─────────────────────────────────────', file=sys.stderr)
285
+ log_info(f'Checking: {file_path}')
286
+
287
+ # Find project root
288
+ project_root = find_project_root(file_path)
289
+ log_debug(f'Project root: {project_root}')
290
+
291
+ if not has_python_project_config(project_root):
292
+ log_info('No pyproject.toml or .python-version found - skipping Python quality checks')
293
+ print(f'\n{Colors.GREEN}✅ No Python project config detected; skipping checks for {os.path.basename(file_path)}{Colors.RESET}', file=sys.stderr)
294
+ sys.exit(0)
295
+
296
+ # Get config from environment
297
+ autofix = os.environ.get('CLAUDE_HOOKS_AUTOFIX', 'true').lower() == 'true'
298
+ ruff_enabled = os.environ.get('CLAUDE_HOOKS_RUFF_ENABLED', 'true').lower() != 'false'
299
+ mypy_enabled = os.environ.get('CLAUDE_HOOKS_MYPY_ENABLED', 'true').lower() != 'false'
300
+
301
+ all_errors = []
302
+ all_autofixes = []
303
+
304
+ # Run ruff checks
305
+ if ruff_enabled:
306
+ ruff_passed, ruff_errors, ruff_autofixes = check_ruff(file_path, project_root, autofix)
307
+ all_errors.extend(ruff_errors)
308
+ all_autofixes.extend(ruff_autofixes)
309
+
310
+ # Run mypy checks
311
+ if mypy_enabled:
312
+ mypy_passed, mypy_errors = check_mypy(file_path, project_root)
313
+ all_errors.extend(mypy_errors)
314
+
315
+ # Print summary
316
+ print_summary(all_errors, all_autofixes)
317
+
318
+ # Exit with appropriate code
319
+ if all_errors:
320
+ print(f'\n{Colors.RED}🛑 FAILED - Fix issues in your edited file! 🛑{Colors.RESET}', file=sys.stderr)
321
+ print(f'{Colors.CYAN}💡 CLAUDE.md CHECK:{Colors.RESET}', file=sys.stderr)
322
+ print(f'{Colors.CYAN} → What CLAUDE.md pattern would have prevented this?{Colors.RESET}', file=sys.stderr)
323
+ print(f'{Colors.YELLOW}📋 NEXT STEPS:{Colors.RESET}', file=sys.stderr)
324
+ print(f'{Colors.YELLOW} 1. Fix the issues listed above{Colors.RESET}', file=sys.stderr)
325
+ print(f'{Colors.YELLOW} 2. The hook will run again automatically{Colors.RESET}', file=sys.stderr)
326
+ print(f'{Colors.YELLOW} 3. Continue once all checks pass{Colors.RESET}', file=sys.stderr)
327
+ sys.exit(2)
328
+ else:
329
+ print(f'\n{Colors.GREEN}✅ Quality check passed for {os.path.basename(file_path)}{Colors.RESET}', file=sys.stderr)
330
+ if all_autofixes:
331
+ print(f'\n{Colors.YELLOW}👉 File quality verified. Auto-fixes applied. Continue with your task.{Colors.RESET}', file=sys.stderr)
332
+ else:
333
+ print(f'\n{Colors.YELLOW}👉 File quality verified. Continue with your task.{Colors.RESET}', file=sys.stderr)
334
+
335
+ # Suggest tests
336
+ check_pytest_suggestions(file_path, project_root)
337
+
338
+ sys.exit(0)
339
+
340
+ if __name__ == '__main__':
341
+ try:
342
+ main()
343
+ except Exception as e:
344
+ log_error(f'Fatal error: {e}')
345
+ sys.exit(1)
@@ -0,0 +1,138 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { execSync } from 'node:child_process';
4
+ import { existsSync, readFileSync, writeFileSync } from 'node:fs';
5
+ import path from 'node:path';
6
+
7
+ export const SESSION_STATE_FILE = '.xtrm-session-state.json';
8
+
9
+ export const SESSION_PHASES = [
10
+ 'claimed',
11
+ 'phase1-done',
12
+ 'waiting-merge',
13
+ 'conflicting',
14
+ 'pending-cleanup',
15
+ 'merged',
16
+ 'cleanup-done',
17
+ ];
18
+
19
+ const ALLOWED_TRANSITIONS = {
20
+ claimed: ['phase1-done', 'waiting-merge', 'conflicting', 'pending-cleanup', 'cleanup-done'],
21
+ 'phase1-done': ['waiting-merge', 'conflicting', 'pending-cleanup', 'cleanup-done'],
22
+ 'waiting-merge': ['conflicting', 'pending-cleanup', 'merged', 'cleanup-done'],
23
+ conflicting: ['waiting-merge', 'pending-cleanup', 'merged', 'cleanup-done'],
24
+ 'pending-cleanup': ['waiting-merge', 'conflicting', 'merged', 'cleanup-done'],
25
+ merged: ['cleanup-done'],
26
+ 'cleanup-done': [],
27
+ };
28
+
29
+ function nowIso() {
30
+ return new Date().toISOString();
31
+ }
32
+
33
+ function isValidPhase(phase) {
34
+ return typeof phase === 'string' && SESSION_PHASES.includes(phase);
35
+ }
36
+
37
+ function normalizeState(state) {
38
+ if (!state || typeof state !== 'object') throw new Error('Invalid session state payload');
39
+ if (!state.issueId || !state.branch || !state.worktreePath) {
40
+ throw new Error('Session state requires issueId, branch, and worktreePath');
41
+ }
42
+ if (!isValidPhase(state.phase)) throw new Error(`Invalid phase: ${String(state.phase)}`);
43
+
44
+ return {
45
+ issueId: String(state.issueId),
46
+ branch: String(state.branch),
47
+ worktreePath: String(state.worktreePath),
48
+ prNumber: state.prNumber ?? null,
49
+ prUrl: state.prUrl ?? null,
50
+ phase: state.phase,
51
+ conflictFiles: Array.isArray(state.conflictFiles) ? state.conflictFiles.map(String) : [],
52
+ startedAt: state.startedAt || nowIso(),
53
+ lastChecked: nowIso(),
54
+ };
55
+ }
56
+
57
+ function canTransition(from, to) {
58
+ if (!isValidPhase(from) || !isValidPhase(to)) return false;
59
+ if (from === to) return true;
60
+ return (ALLOWED_TRANSITIONS[from] || []).includes(to);
61
+ }
62
+
63
+ function findAncestorStateFile(startCwd) {
64
+ let current = path.resolve(startCwd || process.cwd());
65
+ for (;;) {
66
+ const candidate = path.join(current, SESSION_STATE_FILE);
67
+ if (existsSync(candidate)) return candidate;
68
+ const parent = path.dirname(current);
69
+ if (parent === current) return null;
70
+ current = parent;
71
+ }
72
+ }
73
+
74
+ function findRepoRoot(cwd) {
75
+ try {
76
+ return execSync('git rev-parse --show-toplevel', {
77
+ encoding: 'utf8',
78
+ cwd,
79
+ stdio: ['pipe', 'pipe', 'pipe'],
80
+ timeout: 5000,
81
+ }).trim();
82
+ } catch {
83
+ return null;
84
+ }
85
+ }
86
+
87
+ export function findSessionStateFile(startCwd) {
88
+ return findAncestorStateFile(startCwd);
89
+ }
90
+
91
+ export function readSessionState(startCwd) {
92
+ const filePath = findSessionStateFile(startCwd);
93
+ if (!filePath) return null;
94
+
95
+ try {
96
+ const parsed = JSON.parse(readFileSync(filePath, 'utf8'));
97
+ const state = normalizeState(parsed);
98
+ return { ...state, _filePath: filePath };
99
+ } catch {
100
+ return null;
101
+ }
102
+ }
103
+
104
+ export function resolveSessionStatePath(cwd) {
105
+ const existing = findSessionStateFile(cwd);
106
+ if (existing) return existing;
107
+
108
+ const repoRoot = findRepoRoot(cwd);
109
+ if (repoRoot) return path.join(repoRoot, SESSION_STATE_FILE);
110
+ return path.join(cwd, SESSION_STATE_FILE);
111
+ }
112
+
113
+ export function writeSessionState(state, opts = {}) {
114
+ const cwd = opts.cwd || process.cwd();
115
+ const filePath = opts.filePath || resolveSessionStatePath(cwd);
116
+ const normalized = normalizeState(state);
117
+ writeFileSync(filePath, JSON.stringify(normalized, null, 2) + '\n', 'utf8');
118
+ return filePath;
119
+ }
120
+
121
+ export function updateSessionPhase(startCwd, nextPhase, patch = {}) {
122
+ if (!isValidPhase(nextPhase)) throw new Error(`Invalid phase: ${String(nextPhase)}`);
123
+ const existing = readSessionState(startCwd);
124
+ if (!existing) throw new Error('Session state file not found');
125
+ if (!canTransition(existing.phase, nextPhase)) {
126
+ throw new Error(`Invalid phase transition: ${existing.phase} -> ${nextPhase}`);
127
+ }
128
+
129
+ const nextState = {
130
+ ...existing,
131
+ ...patch,
132
+ phase: nextPhase,
133
+ };
134
+
135
+ delete nextState._filePath;
136
+ const filePath = writeSessionState(nextState, { filePath: existing._filePath, cwd: startCwd });
137
+ return { ...nextState, _filePath: filePath };
138
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xtrm-tools",
3
- "version": "2.4.1",
3
+ "version": "2.4.2",
4
4
  "description": "Claude Code tools installer (skills, hooks, MCP servers)",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -39,6 +39,7 @@
39
39
  "sync:cli-version": "node scripts/sync-cli-version.mjs",
40
40
  "compile-policies": "node scripts/compile-policies.mjs",
41
41
  "check-policies": "node scripts/compile-policies.mjs --check",
42
+ "check-pi": "node scripts/compile-policies.mjs --check-pi",
42
43
  "prebuild": "npm run sync:cli-version",
43
44
  "build": "npm run build --workspace cli",
44
45
  "typecheck": "npm run typecheck --workspace cli",
@@ -1,26 +1,3 @@
1
1
  {
2
- "hooks": {
3
- "PostToolUse": [
4
- {
5
- "matcher": "Write|Edit|MultiEdit|mcp__serena__rename_symbol|mcp__serena__replace_symbol_body|mcp__serena__insert_after_symbol|mcp__serena__insert_before_symbol",
6
- "hooks": [
7
- {
8
- "type": "command",
9
- "command": "node \"$CLAUDE_PROJECT_DIR/.claude/hooks/quality-check.cjs\"",
10
- "timeout": 30
11
- }
12
- ]
13
- },
14
- {
15
- "matcher": "Write|Edit|MultiEdit|mcp__serena__rename_symbol|mcp__serena__replace_symbol_body|mcp__serena__insert_after_symbol|mcp__serena__insert_before_symbol",
16
- "hooks": [
17
- {
18
- "type": "command",
19
- "command": "python3 \"$CLAUDE_PROJECT_DIR/.claude/hooks/quality-check.py\"",
20
- "timeout": 30
21
- }
22
- ]
23
- }
24
- ]
25
- }
2
+ "hooks": {}
26
3
  }