claude-turing 4.3.0 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,466 @@
1
+ #!/usr/bin/env python3
2
+ """Harness self-diagnosis for the autoresearch pipeline.
3
+
4
+ Checks environment health, project integrity, resource availability,
5
+ and git state. Identifies common issues and auto-fixes where safe.
6
+
7
+ Usage:
8
+ python scripts/harness_doctor.py
9
+ python scripts/harness_doctor.py --fix
10
+ python scripts/harness_doctor.py --verbose
11
+ python scripts/harness_doctor.py --json
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ import argparse
17
+ import ast
18
+ import json
19
+ import shutil
20
+ import sys
21
+ from datetime import datetime, timezone
22
+ from pathlib import Path
23
+
24
+ import yaml
25
+
26
+ from scripts.turing_io import load_config, load_experiments
27
+
28
+ DEFAULT_LOG_PATH = "experiments/log.jsonl"
29
+ MIN_DISK_MB = 1024 # 1 GB
30
+
31
+ REQUIRED_SCRIPTS = ["train.py", "prepare.py", "evaluate.py"]
32
+ REQUIRED_CONFIG_FIELDS = ["evaluation"]
33
+
34
+ CHECK_CATEGORIES = ["environment", "dependencies", "config", "experiment_log",
35
+ "scripts", "disk_space", "git_state"]
36
+
37
+
38
+ # --- Individual Checks ---
39
+
40
+
41
+ def check_environment() -> dict:
42
+ """Check Python environment health."""
43
+ issues = []
44
+ version = sys.version_info
45
+
46
+ if version < (3, 10):
47
+ issues.append(f"Python {version.major}.{version.minor} — recommend 3.10+")
48
+
49
+ # Check if running in a venv
50
+ in_venv = hasattr(sys, "real_prefix") or (hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix)
51
+
52
+ return {
53
+ "name": "Python environment",
54
+ "status": "PASS" if not issues else "WARN",
55
+ "detail": f"Python {version.major}.{version.minor}.{version.micro}, venv={'active' if in_venv else 'not active'}",
56
+ "issues": issues,
57
+ }
58
+
59
+
60
+ def check_dependencies(required: list[str] | None = None) -> dict:
61
+ """Check that required packages are importable."""
62
+ if required is None:
63
+ required = ["yaml", "numpy", "sklearn", "pandas", "scipy"]
64
+
65
+ missing = []
66
+ for pkg in required:
67
+ try:
68
+ __import__(pkg)
69
+ except ImportError:
70
+ missing.append(pkg)
71
+
72
+ if missing:
73
+ return {
74
+ "name": "Dependencies",
75
+ "status": "FAIL",
76
+ "detail": f"{len(missing)} packages missing: {', '.join(missing)}",
77
+ "issues": [f"Cannot import: {pkg}" for pkg in missing],
78
+ "fix": f"pip install {' '.join(missing)}",
79
+ }
80
+
81
+ return {
82
+ "name": "Dependencies",
83
+ "status": "PASS",
84
+ "detail": f"All {len(required)} packages importable",
85
+ "issues": [],
86
+ }
87
+
88
+
89
+ def check_config(config_path: str = "config.yaml") -> dict:
90
+ """Check config.yaml validity and required fields."""
91
+ path = Path(config_path)
92
+ issues = []
93
+
94
+ if not path.exists():
95
+ return {
96
+ "name": "Config",
97
+ "status": "FAIL",
98
+ "detail": f"{config_path} not found",
99
+ "issues": [f"{config_path} missing"],
100
+ "fix": "Run /turing:init to scaffold the project",
101
+ }
102
+
103
+ try:
104
+ with open(path) as f:
105
+ config = yaml.safe_load(f)
106
+ except yaml.YAMLError as e:
107
+ return {
108
+ "name": "Config",
109
+ "status": "FAIL",
110
+ "detail": f"{config_path} has YAML parse error",
111
+ "issues": [str(e)],
112
+ }
113
+
114
+ if not isinstance(config, dict):
115
+ return {
116
+ "name": "Config",
117
+ "status": "FAIL",
118
+ "detail": f"{config_path} is not a YAML mapping",
119
+ "issues": ["Config must be a YAML dict"],
120
+ }
121
+
122
+ for field in REQUIRED_CONFIG_FIELDS:
123
+ if field not in config:
124
+ issues.append(f"Missing required field: {field}")
125
+
126
+ status = "PASS" if not issues else "WARN"
127
+ return {
128
+ "name": "Config",
129
+ "status": status,
130
+ "detail": f"{config_path} valid, {len(config)} top-level keys",
131
+ "issues": issues,
132
+ }
133
+
134
+
135
+ def check_experiment_log(log_path: str = DEFAULT_LOG_PATH) -> dict:
136
+ """Check experiment log integrity."""
137
+ path = Path(log_path)
138
+
139
+ if not path.exists():
140
+ return {
141
+ "name": "Experiment log",
142
+ "status": "WARN",
143
+ "detail": "No experiment log yet — run /turing:train first",
144
+ "issues": [],
145
+ }
146
+
147
+ issues = []
148
+ total_lines = 0
149
+ valid_lines = 0
150
+ corrupt_lines = []
151
+ missing_fields = []
152
+
153
+ with open(path) as f:
154
+ for i, line in enumerate(f, 1):
155
+ total_lines += 1
156
+ line = line.strip()
157
+ if not line:
158
+ continue
159
+ try:
160
+ entry = json.loads(line)
161
+ valid_lines += 1
162
+ # Check for expected fields
163
+ if "metrics" not in entry:
164
+ missing_fields.append(i)
165
+ except json.JSONDecodeError:
166
+ corrupt_lines.append(i)
167
+
168
+ if corrupt_lines:
169
+ issues.append(f"{len(corrupt_lines)} corrupt lines: {corrupt_lines[:5]}")
170
+ if missing_fields:
171
+ issues.append(f"{len(missing_fields)} entries missing 'metrics' field")
172
+
173
+ status = "FAIL" if corrupt_lines else ("WARN" if missing_fields else "PASS")
174
+ return {
175
+ "name": "Experiment log",
176
+ "status": status,
177
+ "detail": f"{valid_lines}/{total_lines} valid entries",
178
+ "issues": issues,
179
+ "corrupt_lines": corrupt_lines,
180
+ "fixable": len(corrupt_lines) > 0,
181
+ }
182
+
183
+
184
+ def check_scripts(script_dir: str = ".") -> dict:
185
+ """Check that required scripts exist and are syntactically valid."""
186
+ issues = []
187
+ checked = 0
188
+
189
+ for script in REQUIRED_SCRIPTS:
190
+ path = Path(script_dir) / script
191
+ if not path.exists():
192
+ issues.append(f"{script} not found")
193
+ continue
194
+
195
+ try:
196
+ source = path.read_text(encoding="utf-8")
197
+ ast.parse(source, filename=script)
198
+ checked += 1
199
+ except SyntaxError as e:
200
+ issues.append(f"{script} has syntax error: {e.msg} (line {e.lineno})")
201
+
202
+ status = "PASS" if not issues else ("WARN" if checked > 0 else "FAIL")
203
+ return {
204
+ "name": "Scripts",
205
+ "status": status,
206
+ "detail": f"{checked}/{len(REQUIRED_SCRIPTS)} scripts valid",
207
+ "issues": issues,
208
+ }
209
+
210
+
211
+ def check_disk_space(project_dir: str = ".", min_mb: int = MIN_DISK_MB) -> dict:
212
+ """Check available disk space."""
213
+ try:
214
+ usage = shutil.disk_usage(project_dir)
215
+ free_mb = usage.free / (1024 * 1024)
216
+ total_mb = usage.total / (1024 * 1024)
217
+
218
+ if free_mb < min_mb:
219
+ return {
220
+ "name": "Disk space",
221
+ "status": "FAIL",
222
+ "detail": f"{free_mb:.0f} MB remaining — below {min_mb} MB threshold",
223
+ "issues": [f"Low disk space: {free_mb:.0f} MB free of {total_mb:.0f} MB"],
224
+ "fix": "Run /turing:archive to reclaim space",
225
+ "free_mb": round(free_mb),
226
+ }
227
+
228
+ return {
229
+ "name": "Disk space",
230
+ "status": "PASS",
231
+ "detail": f"{free_mb:.0f} MB free",
232
+ "issues": [],
233
+ "free_mb": round(free_mb),
234
+ }
235
+ except OSError as e:
236
+ return {
237
+ "name": "Disk space",
238
+ "status": "WARN",
239
+ "detail": f"Could not check disk: {e}",
240
+ "issues": [str(e)],
241
+ }
242
+
243
+
244
+ def check_git_state(project_dir: str = ".") -> dict:
245
+ """Check git working tree state."""
246
+ import subprocess
247
+
248
+ try:
249
+ result = subprocess.run(
250
+ ["git", "status", "--porcelain"],
251
+ capture_output=True, text=True, timeout=10,
252
+ cwd=project_dir,
253
+ )
254
+ if result.returncode != 0:
255
+ return {
256
+ "name": "Git state",
257
+ "status": "WARN",
258
+ "detail": "Not a git repository or git not available",
259
+ "issues": [],
260
+ }
261
+
262
+ modified = result.stdout.strip().split("\n") if result.stdout.strip() else []
263
+ issues = []
264
+
265
+ # Check if critical files are modified
266
+ critical = {"evaluate.py", "prepare.py"}
267
+ for line in modified:
268
+ if len(line) >= 3:
269
+ filepath = line[3:].strip()
270
+ if any(c in filepath for c in critical):
271
+ issues.append(f"Uncommitted changes to {filepath} — evaluation integrity at risk")
272
+
273
+ status = "WARN" if issues else "PASS"
274
+ detail = "Working tree clean" if not modified else f"{len(modified)} modified files"
275
+
276
+ return {
277
+ "name": "Git state",
278
+ "status": status,
279
+ "detail": detail,
280
+ "issues": issues,
281
+ }
282
+ except (subprocess.TimeoutExpired, FileNotFoundError):
283
+ return {
284
+ "name": "Git state",
285
+ "status": "WARN",
286
+ "detail": "Git check skipped (timeout or not available)",
287
+ "issues": [],
288
+ }
289
+
290
+
291
+ # --- Fix Operations ---
292
+
293
+
294
+ def fix_corrupt_log(log_path: str = DEFAULT_LOG_PATH) -> dict:
295
+ """Remove corrupt lines from experiment log."""
296
+ path = Path(log_path)
297
+ if not path.exists():
298
+ return {"fixed": False, "reason": "Log not found"}
299
+
300
+ valid_lines = []
301
+ removed = 0
302
+
303
+ with open(path) as f:
304
+ for line in f:
305
+ line_stripped = line.strip()
306
+ if not line_stripped:
307
+ continue
308
+ try:
309
+ json.loads(line_stripped)
310
+ valid_lines.append(line)
311
+ except json.JSONDecodeError:
312
+ removed += 1
313
+
314
+ if removed > 0:
315
+ # Backup first
316
+ backup = path.with_suffix(".jsonl.bak")
317
+ shutil.copy2(path, backup)
318
+ with open(path, "w") as f:
319
+ f.writelines(valid_lines)
320
+ return {"fixed": True, "removed": removed, "backup": str(backup)}
321
+
322
+ return {"fixed": False, "reason": "No corrupt lines found"}
323
+
324
+
325
+ # --- Full Doctor ---
326
+
327
+
328
+ def run_doctor(
329
+ config_path: str = "config.yaml",
330
+ log_path: str = DEFAULT_LOG_PATH,
331
+ fix: bool = False,
332
+ verbose: bool = False,
333
+ ) -> dict:
334
+ """Run all diagnostic checks.
335
+
336
+ Args:
337
+ config_path: Path to config.yaml.
338
+ log_path: Path to experiment log.
339
+ fix: If True, auto-fix safe issues.
340
+ verbose: Include detailed info.
341
+
342
+ Returns:
343
+ Doctor report with all check results and score.
344
+ """
345
+ checks = [
346
+ check_environment(),
347
+ check_dependencies(),
348
+ check_config(config_path),
349
+ check_experiment_log(log_path),
350
+ check_scripts(),
351
+ check_disk_space(),
352
+ check_git_state(),
353
+ ]
354
+
355
+ # Apply fixes if requested
356
+ fixes_applied = []
357
+ if fix:
358
+ log_check = next((c for c in checks if c["name"] == "Experiment log"), None)
359
+ if log_check and log_check.get("fixable"):
360
+ fix_result = fix_corrupt_log(log_path)
361
+ if fix_result.get("fixed"):
362
+ fixes_applied.append(f"Removed {fix_result['removed']} corrupt log entries (backup: {fix_result['backup']})")
363
+ # Re-run log check
364
+ for i, c in enumerate(checks):
365
+ if c["name"] == "Experiment log":
366
+ checks[i] = check_experiment_log(log_path)
367
+ break
368
+
369
+ # Compute score
370
+ passed = sum(1 for c in checks if c["status"] == "PASS")
371
+ warned = sum(1 for c in checks if c["status"] == "WARN")
372
+ failed = sum(1 for c in checks if c["status"] == "FAIL")
373
+ total = len(checks)
374
+
375
+ return {
376
+ "checks": checks,
377
+ "score": {"passed": passed, "warned": warned, "failed": failed, "total": total},
378
+ "fixes_applied": fixes_applied,
379
+ "overall": "HEALTHY" if failed == 0 and warned == 0 else ("DEGRADED" if failed == 0 else "UNHEALTHY"),
380
+ "generated_at": datetime.now(timezone.utc).isoformat(),
381
+ }
382
+
383
+
384
+ # --- Report Formatting ---
385
+
386
+
387
+ def save_doctor_report(report: dict, output_dir: str = "experiments/doctor") -> Path:
388
+ """Save doctor report to YAML."""
389
+ out_path = Path(output_dir)
390
+ out_path.mkdir(parents=True, exist_ok=True)
391
+ ts = datetime.now(timezone.utc).strftime("%Y%m%d-%H%M%S")
392
+ filepath = out_path / f"doctor-{ts}.yaml"
393
+ with open(filepath, "w") as f:
394
+ yaml.dump(report, f, default_flow_style=False, sort_keys=False)
395
+ return filepath
396
+
397
+
398
+ def format_doctor_report(report: dict) -> str:
399
+ """Format doctor report as readable text."""
400
+ lines = ["# Turing Doctor Report", ""]
401
+
402
+ status_icons = {"PASS": "✓ PASS ", "WARN": "⚠ WARN ", "FAIL": "✗ FAIL "}
403
+
404
+ for check in report.get("checks", []):
405
+ icon = status_icons.get(check["status"], "? ")
406
+ lines.append(f"{icon} {check['name']} ({check.get('detail', '')})")
407
+ for issue in check.get("issues", []):
408
+ lines.append(f" {issue}")
409
+ fix = check.get("fix")
410
+ if fix:
411
+ lines.append(f" Fix: {fix}")
412
+
413
+ score = report.get("score", {})
414
+ lines.extend([
415
+ "",
416
+ f"Score: {score.get('passed', 0)}/{score.get('total', 0)} pass, "
417
+ f"{score.get('warned', 0)} warning{'s' if score.get('warned', 0) != 1 else ''}, "
418
+ f"{score.get('failed', 0)} failure{'s' if score.get('failed', 0) != 1 else ''}",
419
+ f"Overall: {report.get('overall', 'UNKNOWN')}",
420
+ ])
421
+
422
+ fixes = report.get("fixes_applied", [])
423
+ if fixes:
424
+ lines.extend(["", "Fixes applied:"])
425
+ for f in fixes:
426
+ lines.append(f" - {f}")
427
+
428
+ lines.append("")
429
+ lines.append(f"*Generated: {report.get('generated_at', 'N/A')}*")
430
+ return "\n".join(lines)
431
+
432
+
433
+ # --- CLI ---
434
+
435
+
436
+ def main():
437
+ parser = argparse.ArgumentParser(
438
+ description="Harness self-diagnosis — check environment, project, and resource health"
439
+ )
440
+ parser.add_argument("--fix", action="store_true", help="Auto-fix safe issues")
441
+ parser.add_argument("--verbose", action="store_true", help="Show detailed info")
442
+ parser.add_argument("--config", default="config.yaml", help="Path to config.yaml")
443
+ parser.add_argument("--log", default=DEFAULT_LOG_PATH, help="Path to experiment log")
444
+ parser.add_argument("--json", action="store_true", help="Output raw JSON")
445
+
446
+ args = parser.parse_args()
447
+
448
+ report = run_doctor(
449
+ config_path=args.config,
450
+ log_path=args.log,
451
+ fix=args.fix,
452
+ verbose=args.verbose,
453
+ )
454
+
455
+ if args.json:
456
+ print(json.dumps(report, indent=2))
457
+ else:
458
+ print(format_doctor_report(report))
459
+
460
+ saved = save_doctor_report(report)
461
+ if not args.json:
462
+ print(f"\nSaved: {saved}")
463
+
464
+
465
+ if __name__ == "__main__":
466
+ main()