xtrm-tools 2.4.0 → 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 +23 -9
  2. package/cli/dist/index.cjs +774 -240
  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,482 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ Scaffolder for creating-service-skills.
4
+
5
+ Phase 1 of the two-phase workflow: generates a structural skeleton for a new
6
+ service skill by parsing docker-compose.yml, Dockerfiles, and dependency files.
7
+ The skeleton contains [PENDING RESEARCH] markers for the agent to fill in Phase 2.
8
+
9
+ Output location: .claude/skills/<service-id>/
10
+ """
11
+
12
+ import sys
13
+ from pathlib import Path
14
+
15
+ # Resolve bootstrap from this script's directory
16
+ script_dir = Path(__file__).parent
17
+ sys.path.insert(0, str(script_dir))
18
+
19
+ from bootstrap import RootResolutionError, get_project_root, register_service # noqa: E402
20
+
21
+ # ---------------------------------------------------------------------------
22
+ # Official documentation map — populated from detected technologies
23
+ # ---------------------------------------------------------------------------
24
+ OFFICIAL_DOCS: dict[str, tuple[str, str]] = {
25
+ # Docker images / databases
26
+ "postgres": ("PostgreSQL", "https://www.postgresql.org/docs/"),
27
+ "timescale": ("TimescaleDB", "https://docs.timescale.com/"),
28
+ "timescaledb": ("TimescaleDB", "https://docs.timescale.com/"),
29
+ "redis": ("Redis", "https://redis.io/docs/"),
30
+ "mysql": ("MySQL", "https://dev.mysql.com/doc/"),
31
+ "mongodb": ("MongoDB", "https://www.mongodb.com/docs/"),
32
+ "mongo": ("MongoDB", "https://www.mongodb.com/docs/"),
33
+ "elasticsearch": ("Elasticsearch", "https://www.elastic.co/guide/"),
34
+ "rabbitmq": ("RabbitMQ", "https://www.rabbitmq.com/documentation.html"),
35
+ "kafka": ("Apache Kafka", "https://kafka.apache.org/documentation/"),
36
+ "clickhouse": ("ClickHouse", "https://clickhouse.com/docs/"),
37
+ # Python packages
38
+ "fastapi": ("FastAPI", "https://fastapi.tiangolo.com/"),
39
+ "flask": ("Flask", "https://flask.palletsprojects.com/"),
40
+ "django": ("Django", "https://docs.djangoproject.com/"),
41
+ "sqlalchemy": ("SQLAlchemy", "https://docs.sqlalchemy.org/"),
42
+ "alembic": ("Alembic", "https://alembic.sqlalchemy.org/en/latest/"),
43
+ "prisma": ("Prisma", "https://www.prisma.io/docs/"),
44
+ "celery": ("Celery", "https://docs.celeryq.dev/"),
45
+ "pydantic": ("Pydantic", "https://docs.pydantic.dev/"),
46
+ "asyncpg": ("asyncpg", "https://magicstack.github.io/asyncpg/"),
47
+ "psycopg2": ("psycopg2", "https://www.psycopg.org/docs/"),
48
+ "psycopg": ("psycopg3", "https://www.psycopg.org/psycopg3/docs/"),
49
+ "aiohttp": ("aiohttp", "https://docs.aiohttp.org/"),
50
+ "httpx": ("HTTPX", "https://www.python-httpx.org/"),
51
+ }
52
+
53
+
54
+ def scaffold_service_skill(service_id: str, compose_data: dict) -> Path:
55
+ """
56
+ Main entry point for Phase 1 scaffolding.
57
+ """
58
+ try:
59
+ project_root = get_project_root()
60
+ except RootResolutionError as e:
61
+ print(f"Error: {e}")
62
+ sys.exit(1)
63
+
64
+ skill_dir = Path(project_root) / ".claude" / "skills" / service_id
65
+ if skill_dir.exists():
66
+ print(f"Skill directory already exists: {skill_dir}")
67
+ print("Aborting to prevent overwriting. Delete it manually if you want to re-scaffold.")
68
+ sys.exit(1)
69
+
70
+ print(f"Scaffolding new service skill: {service_id}")
71
+ print(f"Target directory: {skill_dir}")
72
+
73
+ skill_dir.mkdir(parents=True)
74
+ (skill_dir / "scripts").mkdir()
75
+ (skill_dir / "references").mkdir()
76
+ (skill_dir / "assets").mkdir()
77
+
78
+ # Detect service details from compose
79
+ service_config = compose_data.get("services", {}).get(service_id, {})
80
+
81
+ # 1. Generate SKILL.md
82
+ write_skill_md(service_id, service_config, skill_dir)
83
+
84
+ # 2. Generate script stubs
85
+ write_script_stubs(service_id, skill_dir)
86
+
87
+ # 3. Generate reference stubs
88
+ write_reference_stubs(service_id, skill_dir)
89
+
90
+ # 4. Register service in bootstrap state
91
+ # [TODO] Fill in territory and name properly
92
+ register_service(
93
+ service_id, service_id, [], str((skill_dir / "SKILL.md").relative_to(project_root))
94
+ )
95
+
96
+ print(f"\n✅ Phase 1 Complete for {service_id}")
97
+ print("Next step: Run Phase 2 deep dive for this service.")
98
+ return skill_dir
99
+
100
+
101
+ def write_skill_md(service_id: str, config: dict, skill_dir: Path) -> None:
102
+ """Generate the root SKILL.md file."""
103
+ name = service_id.replace("-", " ").replace("_", " ").title()
104
+ persona = f"{name} Expert"
105
+
106
+ # Detect docs to link
107
+ docs_section = ""
108
+ # [PENDING] Implement documentation auto-detection logic here
109
+
110
+ content = f"""---
111
+ name: {service_id}
112
+ description: >-
113
+ [PENDING RESEARCH] Specialized knowledge for the {name} service.
114
+ Use when debugging, analyzing performance, or understanding this service.
115
+ allowed-tools: Bash(python3 *), Read, Grep, Glob
116
+ ---
117
+
118
+ # {name}
119
+
120
+ ## Service Overview
121
+
122
+ [PENDING RESEARCH] Describe what this service does, its role in the system,
123
+ and whether it runs continuously, as a one-shot job, or on a schedule.
124
+
125
+ **Persona**: {persona}
126
+
127
+ ## Architecture
128
+
129
+ [PENDING RESEARCH]
130
+
131
+ **Entry Point**: [Verify in Dockerfile CMD and docker-compose `command:` field]
132
+ **Container Name**: {service_id}
133
+ **Restart Policy**: [PENDING RESEARCH]
134
+
135
+ **Primary Modules**:
136
+ - [PENDING RESEARCH] List key modules after reading the source tree
137
+
138
+ **Dependencies**: [PENDING RESEARCH] PostgreSQL? Redis? External APIs?
139
+
140
+ ## ⚠️ CRITICAL REQUIREMENTS
141
+
142
+ [PENDING RESEARCH] Add any mandatory patterns, initialization calls, or
143
+ invariants that must not be violated when modifying this service.
144
+
145
+ ## Data Flows
146
+
147
+ [PENDING RESEARCH] Trace the primary data paths through the service.
148
+
149
+ ## Database Interactions
150
+
151
+ [PENDING RESEARCH]
152
+
153
+ | Table | Operation | Timestamp Column | Stale Threshold |
154
+ |-------|-----------|-----------------|-----------------|
155
+ | [table] | INSERT/SELECT | [col] | [N min] |
156
+
157
+ ## Common Operations
158
+
159
+ ### Service Management
160
+
161
+ ```bash
162
+ # Start the service
163
+ docker compose up -d {service_id}
164
+
165
+ # Check logs
166
+ docker logs {service_id} --tail 50
167
+
168
+ # Restart
169
+ docker compose restart {service_id}
170
+ ```
171
+
172
+ ### Data Inspection
173
+
174
+ - **Health check**: `python3 .claude/skills/{service_id}/scripts/health_probe.py`
175
+ - **Log analysis**: `python3 .claude/skills/{service_id}/scripts/log_hunter.py`
176
+ - **Data explorer**: `python3 .claude/skills/{service_id}/scripts/data_explorer.py`
177
+
178
+ ## Troubleshooting Guide
179
+
180
+ [PENDING RESEARCH] Fill from exception handlers and code comments.
181
+
182
+ | Symptom | Likely Cause | Resolution |
183
+ |---------|-------------|------------|
184
+ | [what you see] | [root cause] | [exact command to fix] |
185
+
186
+ Minimum 5 rows required.
187
+
188
+ <!-- SEMANTIC_START -->
189
+ ## Semantic Deep Dive (Human/Agent Refined)
190
+
191
+ [PENDING RESEARCH] Add deep operational knowledge after Phase 2 deep dive.
192
+
193
+ <!-- SEMANTIC_END -->
194
+
195
+ ## Scripts
196
+
197
+ - `scripts/health_probe.py` — Container status + table freshness check
198
+ - `scripts/log_hunter.py` — Service-specific log pattern analysis
199
+ - `scripts/data_explorer.py` — Safe database inspection (read-only)
200
+
201
+ ## References
202
+ {docs_section}
203
+ - `references/deep_dive.md` — Detailed Phase 2 research notes
204
+ - `references/architecture_ssot.md` — Architecture SSOT (link from project SSOT if available)
205
+
206
+ ---
207
+
208
+ *Generated by creating-service-skills Phase 1. Run Phase 2 to fill [PENDING RESEARCH] markers.*
209
+ """ # nosec B608
210
+ (skill_dir / "SKILL.md").write_text(content, encoding="utf-8")
211
+
212
+
213
+ def write_script_stubs(service_id: str, skill_dir: Path) -> None:
214
+ """
215
+ Write Phase 1 script stubs into the skill's scripts/ directory.
216
+ """
217
+ scripts_dir = skill_dir / "scripts"
218
+ scripts_dir.mkdir(parents=True, exist_ok=True)
219
+
220
+ # health_probe.py stub (using replace to avoid f-string escaping hell)
221
+ health_probe_tpl = '''#!/usr/bin/env python3
222
+ """Health probe for {{SERVICE_ID}}.
223
+
224
+ [PENDING RESEARCH] Replace all [FILL] markers during Phase 2 deep dive.
225
+ """
226
+ import json
227
+ import subprocess
228
+ import sys
229
+
230
+ CONTAINER = "{{SERVICE_ID}}"
231
+ # [PENDING RESEARCH] Set the actual external-mapped DB port (e.g. 5433 for host, 5432 for container)
232
+ DB_PORT = 5433
233
+ # [PENDING RESEARCH] Set the actual output table(s) and stale thresholds in minutes
234
+ STALE_CHECKS: list[dict] = [
235
+ # {"table": "table_name", "ts_col": "created_at", "stale_minutes": 10},
236
+ ]
237
+
238
+
239
+ def check_container() -> bool:
240
+ result = subprocess.run(
241
+ ["docker", "inspect", "-f", "{{.State.Running}}", CONTAINER],
242
+ capture_output=True, text=True
243
+ )
244
+ running = result.stdout.strip() == "true"
245
+ print(f"Container {CONTAINER}: {'RUNNING' if running else 'STOPPED'}")
246
+ return running
247
+
248
+
249
+ def check_table_freshness() -> bool:
250
+ """[PENDING RESEARCH] Query actual output tables with correct stale thresholds."""
251
+ if not STALE_CHECKS:
252
+ print("Table freshness: NOT CONFIGURED (Phase 2 required)")
253
+ return True
254
+ # [PENDING RESEARCH] Implement actual DB checks here
255
+ return True
256
+
257
+
258
+ def main(as_json: bool = False) -> None:
259
+ ok = check_container()
260
+ ok &= check_table_freshness()
261
+ if as_json:
262
+ print(json.dumps({"healthy": ok, "service": CONTAINER}))
263
+ else:
264
+ print(f"\\nOverall: {'HEALTHY' if ok else 'UNHEALTHY'}")
265
+ sys.exit(0 if ok else 1)
266
+
267
+
268
+ if __name__ == "__main__":
269
+ import argparse
270
+ p = argparse.ArgumentParser()
271
+ p.add_argument("--json", action="store_true")
272
+ args = p.parse_args()
273
+ main(as_json=args.json)
274
+ '''
275
+ (scripts_dir / "health_probe.py").write_text(
276
+ health_probe_tpl.replace("{{SERVICE_ID}}", service_id), encoding="utf-8"
277
+ )
278
+
279
+ # log_hunter.py stub
280
+ log_hunter_tpl = '''#!/usr/bin/env python3
281
+ """Log hunter for {{SERVICE_ID}}.
282
+
283
+ [PENDING RESEARCH] Replace generic patterns with actual error strings
284
+ found in the codebase exception handlers during Phase 2 deep dive.
285
+ """
286
+ import json
287
+ import re
288
+ import subprocess
289
+ import sys
290
+ from collections import defaultdict
291
+
292
+ CONTAINER = "{{SERVICE_ID}}"
293
+
294
+ # [PENDING RESEARCH] Replace with patterns sourced from the actual codebase.
295
+ # Find them with: search_for_pattern("logger.error|raise|panic!")
296
+ PATTERNS: list[tuple[str, str, str]] = [
297
+ ("ConnectionError", "ERROR", "Database or Redis connectivity issue"),
298
+ ("TimeoutError", "WARNING", "External service latency detected"),
299
+ ]
300
+
301
+
302
+ def hunt_logs(tail: int = 200) -> dict:
303
+ """Tails logs and matches against patterns."""
304
+ result = subprocess.run(
305
+ ["docker", "logs", "--tail", str(tail), CONTAINER],
306
+ capture_output=True, text=True
307
+ )
308
+ logs = result.stdout + result.stderr
309
+ matches = defaultdict(int)
310
+
311
+ for line in logs.splitlines():
312
+ for pattern, level, desc in PATTERNS:
313
+ if pattern in line:
314
+ matches[pattern] += 1
315
+
316
+ return dict(matches)
317
+
318
+
319
+ def main() -> None:
320
+ results = hunt_logs()
321
+ print(f"Log anomalies for {CONTAINER}:")
322
+ if not results:
323
+ print(" ✓ No known error patterns detected in recent logs.")
324
+ else:
325
+ for p, count in results.items():
326
+ print(f" - {p}: {count} occurrences")
327
+
328
+
329
+ if __name__ == "__main__":
330
+ main()
331
+ '''
332
+ (scripts_dir / "log_hunter.py").write_text(
333
+ log_hunter_tpl.replace("{{SERVICE_ID}}", service_id), encoding="utf-8"
334
+ )
335
+
336
+ # data_explorer.py stub
337
+ data_explorer_tpl = '''#!/usr/bin/env python3
338
+ """Data explorer for {{SERVICE_ID}} — read-only DB inspection.
339
+
340
+ [PENDING RESEARCH] Fill in actual table names, columns, and host port
341
+ during Phase 2 deep dive. All queries must use parameterized %s placeholders.
342
+ """
343
+ import json
344
+ import sys
345
+
346
+ # [PENDING RESEARCH] Set the actual table and connection settings
347
+ TABLE = "[PENDING RESEARCH]"
348
+ DB_HOST = "localhost"
349
+ DB_PORT = 5433 # [PENDING RESEARCH] external mapped port, not container-internal
350
+ DB_NAME = "[PENDING RESEARCH]"
351
+ DB_USER = "postgres"
352
+
353
+
354
+ def recent_rows(limit: int = 20, as_json: bool = False) -> None:
355
+ """[PENDING RESEARCH] Query most recent rows from the output table."""
356
+ print(f"[PENDING RESEARCH] Implement: SELECT * FROM {TABLE} ORDER BY created_at DESC LIMIT %s")
357
+ print("Use parameterized queries only — no f-strings in SQL.")
358
+
359
+
360
+ def main() -> None:
361
+ import argparse
362
+ p = argparse.ArgumentParser()
363
+ p.add_argument("--limit", type=int, default=20)
364
+ p.add_argument("--json", action="store_true")
365
+ args = p.parse_args()
366
+ recent_rows(args.limit, args.json)
367
+
368
+
369
+ if __name__ == "__main__":
370
+ main()
371
+ '''
372
+ (scripts_dir / "data_explorer.py").write_text(
373
+ data_explorer_tpl.replace("{{SERVICE_ID}}", service_id), encoding="utf-8"
374
+ )
375
+
376
+ # Makefile — standard diagnostic runner for every skill
377
+ makefile_tpl = """# Skill diagnostic scripts for {{SERVICE_ID}}
378
+ # Usage: make <target> (from this directory)
379
+ # Override python: make health PYTHON=/path/to/python3
380
+
381
+ # Auto-detect: prefer project venv (4 levels up), fall back to system python3
382
+ _VENV := $(wildcard ../../../../venv/bin/python3)
383
+ PYTHON ?= $(if $(_VENV),../../../../venv/bin/python3,python3)
384
+
385
+ .PHONY: health health-json data data-json logs errors db help
386
+
387
+ help:
388
+ \t@echo "Available targets:"
389
+ \t@echo " health - Run health probe (human readable)"
390
+ \t@echo " health-json - Run health probe (JSON output)"
391
+ \t@echo " data - Show latest DB records"
392
+ \t@echo " data-json - Show latest DB records (JSON, limit 5)"
393
+ \t@echo " logs - Tail and analyze recent logs"
394
+ \t@echo " errors - Show errors/criticals only"
395
+ \t@echo " db - Run DB helper example queries"
396
+ \t@echo ""
397
+ \t@echo "Python: $(PYTHON)"
398
+
399
+ health:
400
+ \t$(PYTHON) health_probe.py
401
+
402
+ health-json:
403
+ \t$(PYTHON) health_probe.py --json
404
+
405
+ data:
406
+ \t$(PYTHON) data_explorer.py
407
+
408
+ data-json:
409
+ \t$(PYTHON) data_explorer.py --json --limit 5
410
+
411
+ logs:
412
+ \t$(PYTHON) log_hunter.py --tail 50
413
+
414
+ errors:
415
+ \t$(PYTHON) log_hunter.py --errors-only --tail 50
416
+
417
+ db:
418
+ \t$(PYTHON) db_helper.py
419
+ """
420
+ (scripts_dir / "Makefile").write_text(
421
+ makefile_tpl.replace("{{SERVICE_ID}}", service_id), encoding="utf-8"
422
+ )
423
+
424
+
425
+ def write_reference_stubs(service_id: str, skill_dir: Path) -> None:
426
+ """Generate reference markdown files."""
427
+ name = service_id.replace("-", " ").replace("_", " ").title()
428
+
429
+ # deep_dive.md
430
+ (skill_dir / "references" / "deep_dive.md").write_text(
431
+ f"""# Phase 2 Research: {name}
432
+
433
+ ## Source Analysis
434
+ - **Entry Point**: [FILL]
435
+ - **Main Loop**: [FILL]
436
+ - **Error Handlers**: [FILL]
437
+
438
+ ## Logic Trace
439
+ 1. [Step 1]
440
+ 2. [Step 2]
441
+
442
+ ## Invariants
443
+ - [Must always X]
444
+ - [Must never Y]
445
+ """,
446
+ encoding="utf-8",
447
+ )
448
+
449
+ # architecture_ssot.md (stub)
450
+ (skill_dir / "references" / "architecture_ssot.md").write_text(
451
+ f"""# {name} Architecture
452
+
453
+ [PENDING RESEARCH] Replace with link to project-level SSOT if exists,
454
+ otherwise document high-level components here.
455
+ """,
456
+ encoding="utf-8",
457
+ )
458
+
459
+
460
+ if __name__ == "__main__":
461
+ import yaml
462
+
463
+ if len(sys.argv) < 2:
464
+ print("Usage: scaffolder.py <docker-compose-path> [service-id]")
465
+ sys.exit(1)
466
+
467
+ compose_path = Path(sys.argv[1])
468
+ if not compose_path.exists():
469
+ print(f"Compose file not found: {compose_path}")
470
+ sys.exit(1)
471
+
472
+ with open(compose_path) as f:
473
+ data = yaml.safe_load(f)
474
+
475
+ if len(sys.argv) > 2:
476
+ # Scaffold specific service
477
+ sid = sys.argv[2]
478
+ scaffold_service_skill(sid, data)
479
+ else:
480
+ # Scaffold all services in compose
481
+ for sid in data.get("services", {}).keys():
482
+ scaffold_service_skill(sid, data)
@@ -0,0 +1,231 @@
1
+ ---
2
+ name: scoping-service-skills
3
+ description: >-
4
+ Task intake and service routing for any task type. Reads service-registry.json
5
+ directly, detects intent, maps to the right expert skill(s), and emits a
6
+ structured XML scope plan before any files are touched. Invoke via
7
+ /scope "task description" before starting any investigation, feature,
8
+ refactor, config-change, or exploration task.
9
+ allowed-tools: Bash(python3 *), Read
10
+ ---
11
+
12
+ # Scoping Service Skills ( /scope )
13
+
14
+ Ground every task in the right expert context **before any files are touched**.
15
+
16
+ ## Trigger
17
+
18
+ User types `/scope "task description"` — or `/scope` with task context already
19
+ in the conversation.
20
+
21
+ ---
22
+
23
+ ## Execution Flow
24
+
25
+ ### Step 1 — Read the Registry
26
+
27
+ Run this at the start, before deep task reasoning:
28
+
29
+ ```bash
30
+ python3 "$CLAUDE_PROJECT_DIR/.claude/skills/scoping-service-skills/scripts/scope.py"
31
+ ```
32
+
33
+ This outputs every registered service: ID, container, territory paths, skill path,
34
+ and description. If the registry is missing, report:
35
+
36
+ > "No service-registry.json found at this project. Run /creating-service-skills first."
37
+
38
+ ---
39
+
40
+ ### Step 2 — Detect Intent
41
+
42
+ Scan the task description for keywords:
43
+
44
+ | Intent | Signal keywords |
45
+ |---|---|
46
+ | `investigation` | broken, error, failing, problem, not working, issue, crash, down, missing, slow, 502, 404, 429, timeout |
47
+ | `feature` | add, implement, create, new, build, introduce, support |
48
+ | `refactor` | refactor, restructure, clean, reorganize, simplify, rename, extract |
49
+ | `config-change` | update, change, modify, set, configure, adjust, tune |
50
+ | `exploration` | how, explain, understand, what is, show me, why, walk me through |
51
+
52
+ **Default when ambiguous → `investigation`** (safer: check first, act second).
53
+
54
+ ---
55
+
56
+ ### Step 3 — Map to Services
57
+
58
+ Using the registry output, reason about which service(s) the task involves. Match on:
59
+
60
+ - Explicit service name in task description (`traefik`, `grafana`, `loki` …)
61
+ - Symptom-to-service knowledge:
62
+ - `502 / route not found` → traefik
63
+ - `logs not appearing` → loki / promtail
64
+ - `alert not firing` → alertmanager / prometheus
65
+ - `dashboard broken` → grafana
66
+ - `API key rejected` → api-gateway
67
+ - `disk full` → node-exporter / loki (chunks)
68
+ - `container memory` → cadvisor
69
+ - Config file or directory mentioned (`routes.yml` → traefik, `prometheus.yml` → prometheus)
70
+ - Container name mentioned (`infra-*`)
71
+
72
+ ---
73
+
74
+ ### Step 4 — Output XML Scope Block
75
+
76
+ Emit this block before moving into implementation:
77
+
78
+ ```xml
79
+ <scope>
80
+ <task>"user's original description"</task>
81
+ <intent>investigation</intent>
82
+ <confidence>high|medium|low</confidence>
83
+ <services>
84
+ <service id="traefik" confidence="high">
85
+ <reason>user mentioned 502 on dashboard route</reason>
86
+ <skill>.claude/skills/traefik/SKILL.md</skill>
87
+ <load>now</load>
88
+ </service>
89
+ </services>
90
+ <workflow>
91
+ <phase order="1" name="diagnose">
92
+ Consult traefik SKILL.md failure modes table.
93
+ Run health_probe.py and log_hunter.py before any ad-hoc docker commands.
94
+ </phase>
95
+ <phase order="2" name="fix">
96
+ Apply targeted fix based on diagnosis.
97
+ </phase>
98
+ <phase order="3" name="regression-test">
99
+ <decision>
100
+ Code behavior bug → write test in repo test suite (pytest/unit).
101
+ Operational/infra issue → extend health_probe.py OR add script to
102
+ .claude/skills/traefik/scripts/.
103
+ </decision>
104
+ Name the function after the failure mode it catches.
105
+ Commit the test alongside the fix — never separately.
106
+ </phase>
107
+ </workflow>
108
+ </scope>
109
+ ```
110
+
111
+ Adapt the phases to the detected intent (see Intent Workflows below).
112
+
113
+ ---
114
+
115
+ ### Step 5 — Load Skills
116
+
117
+ For each `<service>` with `<load>now</load>`, immediately read the skill file:
118
+
119
+ ```
120
+ Read: .claude/skills/<service-id>/SKILL.md
121
+ ```
122
+
123
+ Load all matched skills before proceeding with the task.
124
+ Adopt the expert persona, constraints, and diagnostic approach from each loaded skill.
125
+
126
+ ---
127
+
128
+ ### Step 6 — Execute
129
+
130
+ Follow the workflow phases in order. For `investigation` tasks, include the
131
+ regression-test phase — it keeps fixes durable.
132
+
133
+ ---
134
+
135
+ ## Intent Workflows
136
+
137
+ ### `investigation` — Problem / Error / Broken
138
+
139
+ ```
140
+ diagnose → fix → regression-test
141
+ ```
142
+
143
+ - Start with the skill's **failure modes table** — not ad-hoc docker commands.
144
+ - Use the skill's **diagnostic scripts** (`health_probe.py`, `log_hunter.py`) first.
145
+ - After the fix is applied and verified: write a regression test (see below).
146
+
147
+ ### `feature` — New Capability
148
+
149
+ ```
150
+ design → skill-check → implement → test
151
+ ```
152
+
153
+ - Read the skill's **architecture section** before designing anything.
154
+ - Check integration points (what calls this service, what does it call).
155
+ - Write tests alongside implementation, not as a follow-up.
156
+
157
+ ### `refactor` — Structural Change
158
+
159
+ ```
160
+ scope → skill-check → change → verify
161
+ ```
162
+
163
+ - Read the skill's **integration diagram** to understand what depends on this.
164
+ - Use `find_referencing_symbols` before renaming or restructuring.
165
+ - Verify no external callers break after the change.
166
+
167
+ ### `config-change` — Setting / Parameter
168
+
169
+ ```
170
+ read-current → validate → modify → confirm
171
+ ```
172
+
173
+ - Read the current config state before touching anything.
174
+ - Validate the intended change against the skill's known constraints and limits.
175
+ - After applying: confirm the service is healthy (`health_probe.py`).
176
+
177
+ ### `exploration` — Understanding / Analysis
178
+
179
+ ```
180
+ load-skill → answer
181
+ ```
182
+
183
+ - Load the skill and answer from its documented knowledge.
184
+ - No file modification. No action needed unless user explicitly asks.
185
+
186
+ ---
187
+
188
+ ## Regression Test Binding
189
+
190
+ When `intent = investigation` and a fix has been applied, write a regression
191
+ test. Use this decision tree:
192
+
193
+ ```
194
+ Is the bug in application code logic?
195
+ YES → write pytest/unit test in repo's test suite
196
+
197
+ NO (operational / infra / config issue) →
198
+ Does the skill's health_probe.py already check this condition?
199
+ YES → extend the existing check function
200
+ NO → add a new check function to health_probe.py
201
+ OR create a dedicated script in .claude/skills/<service>/scripts/
202
+ ```
203
+
204
+ **Naming convention** — name after the failure mode, not the fix:
205
+
206
+ ```python
207
+ def check_route_not_returning_502(): # ✅ descriptive
208
+ def check_cert_not_expiring_soon(): # ✅ descriptive
209
+ def test_fix(): # ❌ meaningless
210
+ def test_issue_123(): # ❌ meaningless
211
+ ```
212
+
213
+ Commit the test in the same commit as the fix.
214
+
215
+ ---
216
+
217
+ ## No Match Handling
218
+
219
+ If no registered service matches the task:
220
+
221
+ 1. Report: `"No registered skill covers this area."`
222
+ 2. Offer: `"I can create one — use /creating-service-skills."`
223
+ 3. Fall back to general expert mode (no skill enforcement).
224
+
225
+ ---
226
+
227
+ ## Related Skills
228
+
229
+ - `/using-service-skills` — Passive catalog at session start
230
+ - `/creating-service-skills` — Scaffold new expert skill packages
231
+ - `/updating-service-skills` — Sync skills after implementation drift