empathy-framework 3.8.3__py3-none-any.whl → 3.9.0__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.
Files changed (50) hide show
  1. {empathy_framework-3.8.3.dist-info → empathy_framework-3.9.0.dist-info}/METADATA +25 -6
  2. {empathy_framework-3.8.3.dist-info → empathy_framework-3.9.0.dist-info}/RECORD +50 -39
  3. {empathy_framework-3.8.3.dist-info → empathy_framework-3.9.0.dist-info}/top_level.txt +0 -4
  4. empathy_os/.empathy/costs.json +60 -0
  5. empathy_os/.empathy/discovery_stats.json +15 -0
  6. empathy_os/.empathy/workflow_runs.json +45 -0
  7. empathy_os/cli.py +372 -13
  8. empathy_os/cli_unified.py +111 -0
  9. empathy_os/config/xml_config.py +45 -3
  10. empathy_os/config.py +46 -2
  11. empathy_os/memory/control_panel.py +128 -8
  12. empathy_os/memory/long_term.py +26 -4
  13. empathy_os/memory/short_term.py +110 -0
  14. empathy_os/models/token_estimator.py +25 -0
  15. empathy_os/pattern_library.py +81 -8
  16. empathy_os/patterns/debugging/all_patterns.json +81 -0
  17. empathy_os/patterns/debugging/workflow_20260107_1770825e.json +77 -0
  18. empathy_os/patterns/refactoring_memory.json +89 -0
  19. empathy_os/telemetry/__init__.py +11 -0
  20. empathy_os/telemetry/cli.py +451 -0
  21. empathy_os/telemetry/usage_tracker.py +475 -0
  22. empathy_os/tier_recommender.py +422 -0
  23. empathy_os/workflows/base.py +220 -23
  24. empathy_os/workflows/config.py +50 -5
  25. empathy_os/workflows/tier_tracking.py +408 -0
  26. {empathy_framework-3.8.3.dist-info → empathy_framework-3.9.0.dist-info}/WHEEL +0 -0
  27. {empathy_framework-3.8.3.dist-info → empathy_framework-3.9.0.dist-info}/entry_points.txt +0 -0
  28. {empathy_framework-3.8.3.dist-info → empathy_framework-3.9.0.dist-info}/licenses/LICENSE +0 -0
  29. {hot_reload → empathy_os/hot_reload}/README.md +0 -0
  30. {hot_reload → empathy_os/hot_reload}/__init__.py +0 -0
  31. {hot_reload → empathy_os/hot_reload}/config.py +0 -0
  32. {hot_reload → empathy_os/hot_reload}/integration.py +0 -0
  33. {hot_reload → empathy_os/hot_reload}/reloader.py +0 -0
  34. {hot_reload → empathy_os/hot_reload}/watcher.py +0 -0
  35. {hot_reload → empathy_os/hot_reload}/websocket.py +0 -0
  36. {scaffolding → empathy_os/scaffolding}/README.md +0 -0
  37. {scaffolding → empathy_os/scaffolding}/__init__.py +0 -0
  38. {scaffolding → empathy_os/scaffolding}/__main__.py +0 -0
  39. {scaffolding → empathy_os/scaffolding}/cli.py +0 -0
  40. {test_generator → empathy_os/test_generator}/__init__.py +0 -0
  41. {test_generator → empathy_os/test_generator}/__main__.py +0 -0
  42. {test_generator → empathy_os/test_generator}/cli.py +0 -0
  43. {test_generator → empathy_os/test_generator}/generator.py +0 -0
  44. {test_generator → empathy_os/test_generator}/risk_analyzer.py +0 -0
  45. {workflow_patterns → empathy_os/workflow_patterns}/__init__.py +0 -0
  46. {workflow_patterns → empathy_os/workflow_patterns}/behavior.py +0 -0
  47. {workflow_patterns → empathy_os/workflow_patterns}/core.py +0 -0
  48. {workflow_patterns → empathy_os/workflow_patterns}/output.py +0 -0
  49. {workflow_patterns → empathy_os/workflow_patterns}/registry.py +0 -0
  50. {workflow_patterns → empathy_os/workflow_patterns}/structural.py +0 -0
@@ -0,0 +1,475 @@
1
+ """Usage Tracker for Empathy Framework Telemetry.
2
+
3
+ Privacy-first, local-only tracking of LLM calls to measure actual cost savings.
4
+ All data stored locally in ~/.empathy/telemetry/ as JSON Lines format.
5
+
6
+ Copyright 2025 Smart-AI-Memory
7
+ Licensed under Fair Source License 0.9
8
+ """
9
+
10
+ import hashlib
11
+ import json
12
+ import logging
13
+ import threading
14
+ from datetime import datetime, timedelta
15
+ from pathlib import Path
16
+ from typing import Any
17
+
18
+ logger = logging.getLogger(__name__)
19
+
20
+
21
+ class UsageTracker:
22
+ """Privacy-first local telemetry tracker.
23
+
24
+ Tracks LLM calls to JSON Lines format with automatic rotation
25
+ and 90-day retention. Thread-safe with atomic writes.
26
+
27
+ All user identifiers are SHA256 hashed for privacy.
28
+ No prompts, responses, file paths, or PII are ever tracked.
29
+ """
30
+
31
+ # Class-level lock for thread safety across all instances
32
+ _lock = threading.Lock()
33
+ # Singleton instance
34
+ _instance: "UsageTracker | None" = None
35
+
36
+ def __init__(
37
+ self,
38
+ telemetry_dir: Path | None = None,
39
+ retention_days: int = 90,
40
+ max_file_size_mb: int = 10,
41
+ ):
42
+ """Initialize UsageTracker.
43
+
44
+ Args:
45
+ telemetry_dir: Directory for telemetry files.
46
+ Defaults to ~/.empathy/telemetry/
47
+ retention_days: Days to retain telemetry data (default: 90)
48
+ max_file_size_mb: Max size in MB before rotation (default: 10)
49
+
50
+ """
51
+ self.telemetry_dir = telemetry_dir or Path.home() / ".empathy" / "telemetry"
52
+ self.retention_days = retention_days
53
+ self.max_file_size_mb = max_file_size_mb
54
+ self.usage_file = self.telemetry_dir / "usage.jsonl"
55
+
56
+ # Create directory if needed (gracefully handle permission errors)
57
+ try:
58
+ self.telemetry_dir.mkdir(parents=True, exist_ok=True)
59
+ except (OSError, PermissionError):
60
+ # Can't create directory - telemetry will be disabled
61
+ logger.debug(f"Failed to create telemetry directory: {self.telemetry_dir}")
62
+
63
+ @classmethod
64
+ def get_instance(cls, **kwargs: Any) -> "UsageTracker":
65
+ """Get singleton instance of UsageTracker.
66
+
67
+ Args:
68
+ **kwargs: Arguments passed to __init__ if creating new instance
69
+
70
+ Returns:
71
+ Singleton UsageTracker instance
72
+
73
+ """
74
+ if cls._instance is None:
75
+ cls._instance = cls(**kwargs)
76
+ return cls._instance
77
+
78
+ def track_llm_call(
79
+ self,
80
+ workflow: str,
81
+ stage: str | None,
82
+ tier: str,
83
+ model: str,
84
+ provider: str,
85
+ cost: float,
86
+ tokens: dict[str, int],
87
+ cache_hit: bool,
88
+ cache_type: str | None,
89
+ duration_ms: int,
90
+ user_id: str | None = None,
91
+ ) -> None:
92
+ """Track a single LLM call.
93
+
94
+ Args:
95
+ workflow: Workflow name (e.g., "code-review")
96
+ stage: Stage name (e.g., "analysis"), optional
97
+ tier: Model tier (CHEAP, CAPABLE, PREMIUM)
98
+ model: Model ID (e.g., "claude-sonnet-4.5")
99
+ provider: Provider name (anthropic, openai, etc.)
100
+ cost: Cost in USD
101
+ tokens: Dict with "input" and "output" keys
102
+ cache_hit: Whether this was a cache hit
103
+ cache_type: Cache type if hit ("hash", "hybrid", etc.)
104
+ duration_ms: Call duration in milliseconds
105
+ user_id: Optional user identifier (will be hashed)
106
+
107
+ """
108
+ # Build entry
109
+ entry: dict[str, Any] = {
110
+ "v": "1.0",
111
+ "ts": datetime.utcnow().isoformat() + "Z",
112
+ "workflow": workflow,
113
+ "tier": tier,
114
+ "model": model,
115
+ "provider": provider,
116
+ "cost": round(cost, 6),
117
+ "tokens": tokens,
118
+ "cache": {"hit": cache_hit},
119
+ "duration_ms": duration_ms,
120
+ "user_id": self._hash_user_id(user_id or "default"),
121
+ }
122
+
123
+ # Add optional fields
124
+ if stage:
125
+ entry["stage"] = stage
126
+ if cache_hit and cache_type:
127
+ entry["cache"]["type"] = cache_type
128
+
129
+ # Write entry (thread-safe, atomic)
130
+ try:
131
+ self._write_entry(entry)
132
+ # Check if rotation needed
133
+ self._rotate_if_needed()
134
+ except OSError as e:
135
+ # File system errors - log but don't crash
136
+ logger.debug(f"Failed to write telemetry entry: {e}")
137
+ except Exception as ex:
138
+ # INTENTIONAL: Telemetry failures should never crash the workflow
139
+ logger.debug(f"Unexpected error writing telemetry entry: {ex}")
140
+
141
+ def _hash_user_id(self, user_id: str) -> str:
142
+ """Hash user ID with SHA256 for privacy.
143
+
144
+ Args:
145
+ user_id: User identifier to hash
146
+
147
+ Returns:
148
+ First 16 characters of SHA256 hash
149
+
150
+ """
151
+ return hashlib.sha256(user_id.encode()).hexdigest()[:16]
152
+
153
+ def _write_entry(self, entry: dict[str, Any]) -> None:
154
+ """Write entry to JSON Lines file atomically.
155
+
156
+ Uses atomic write pattern: write to temp file, then rename.
157
+ This ensures no partial writes even with concurrent access.
158
+
159
+ Args:
160
+ entry: Dictionary entry to write
161
+
162
+ """
163
+ with self._lock:
164
+ # Write to temp file
165
+ temp_file = self.usage_file.with_suffix(".tmp")
166
+ try:
167
+ # Append to temp file
168
+ with open(temp_file, "a", encoding="utf-8") as f:
169
+ json.dump(entry, f, separators=(",", ":"))
170
+ f.write("\n")
171
+
172
+ # Atomic rename: temp -> usage.jsonl
173
+ # If usage.jsonl exists, we need to append
174
+ if self.usage_file.exists():
175
+ # Read temp file content
176
+ with open(temp_file, encoding="utf-8") as f:
177
+ new_line = f.read()
178
+ # Append to main file
179
+ with open(self.usage_file, "a", encoding="utf-8") as f:
180
+ f.write(new_line)
181
+ # Clean up temp file
182
+ temp_file.unlink()
183
+ else:
184
+ # Just rename temp to main
185
+ temp_file.replace(self.usage_file)
186
+ except OSError:
187
+ # Clean up temp file if it exists
188
+ if temp_file.exists():
189
+ try:
190
+ temp_file.unlink()
191
+ except OSError:
192
+ pass
193
+ raise
194
+
195
+ def _rotate_if_needed(self) -> None:
196
+ """Rotate log file if size exceeds max_file_size_mb.
197
+
198
+ Rotates usage.jsonl -> usage.YYYY-MM-DD.jsonl
199
+ Also cleans up files older than retention_days.
200
+ """
201
+ if not self.usage_file.exists():
202
+ return
203
+
204
+ # Check file size
205
+ size_mb = self.usage_file.stat().st_size / (1024 * 1024)
206
+ if size_mb < self.max_file_size_mb:
207
+ return
208
+
209
+ with self._lock:
210
+ # Rotate: usage.jsonl -> usage.YYYY-MM-DD.jsonl
211
+ timestamp = datetime.now().strftime("%Y-%m-%d")
212
+ rotated_file = self.telemetry_dir / f"usage.{timestamp}.jsonl"
213
+
214
+ # If rotated file already exists, append a counter
215
+ counter = 1
216
+ while rotated_file.exists():
217
+ rotated_file = self.telemetry_dir / f"usage.{timestamp}.{counter}.jsonl"
218
+ counter += 1
219
+
220
+ # Rename current file
221
+ self.usage_file.rename(rotated_file)
222
+
223
+ # Clean up old files
224
+ self._cleanup_old_files()
225
+
226
+ def _cleanup_old_files(self) -> None:
227
+ """Remove files older than retention_days."""
228
+ cutoff = datetime.now() - timedelta(days=self.retention_days)
229
+
230
+ for file in self.telemetry_dir.glob("usage.*.jsonl"):
231
+ try:
232
+ # Get file modification time
233
+ mtime = datetime.fromtimestamp(file.stat().st_mtime)
234
+ if mtime < cutoff:
235
+ file.unlink()
236
+ logger.debug(f"Deleted old telemetry file: {file.name}")
237
+ except (OSError, ValueError):
238
+ # File system errors - log but continue
239
+ logger.debug(f"Failed to clean up telemetry file: {file.name}")
240
+
241
+ def get_recent_entries(
242
+ self,
243
+ limit: int = 20,
244
+ days: int | None = None,
245
+ ) -> list[dict[str, Any]]:
246
+ """Read recent telemetry entries.
247
+
248
+ Args:
249
+ limit: Maximum number of entries to return (default: 20)
250
+ days: Only return entries from last N days (optional)
251
+
252
+ Returns:
253
+ List of telemetry entries (most recent first)
254
+
255
+ """
256
+ entries: list[dict[str, Any]] = []
257
+ cutoff_time = datetime.utcnow() - timedelta(days=days) if days else None
258
+
259
+ # Read all relevant files
260
+ files = sorted(self.telemetry_dir.glob("usage*.jsonl"), reverse=True)
261
+
262
+ for file in files:
263
+ if not file.exists():
264
+ continue
265
+
266
+ try:
267
+ with open(file, encoding="utf-8") as f:
268
+ for line in f:
269
+ if not line.strip():
270
+ continue
271
+ try:
272
+ entry = json.loads(line)
273
+ # Check timestamp if filtering by days
274
+ if cutoff_time:
275
+ ts = datetime.fromisoformat(entry["ts"].rstrip("Z"))
276
+ if ts < cutoff_time:
277
+ continue
278
+ entries.append(entry)
279
+ except (json.JSONDecodeError, KeyError, ValueError):
280
+ # Skip invalid entries
281
+ continue
282
+ except OSError:
283
+ # File read errors - log but continue
284
+ logger.debug(f"Failed to read telemetry file: {file.name}")
285
+ continue
286
+
287
+ # Sort by timestamp (most recent first) and limit
288
+ entries.sort(key=lambda e: e.get("ts", ""), reverse=True)
289
+ return entries[:limit]
290
+
291
+ def get_stats(self, days: int = 30) -> dict[str, Any]:
292
+ """Calculate telemetry statistics.
293
+
294
+ Args:
295
+ days: Number of days to analyze (default: 30)
296
+
297
+ Returns:
298
+ Dictionary with statistics including:
299
+ - total_calls: Total number of LLM calls
300
+ - total_cost: Total cost in USD
301
+ - total_tokens_input: Total input tokens
302
+ - total_tokens_output: Total output tokens
303
+ - cache_hits: Number of cache hits
304
+ - cache_misses: Number of cache misses
305
+ - cache_hit_rate: Cache hit rate as percentage
306
+ - by_tier: Cost breakdown by tier
307
+ - by_workflow: Cost breakdown by workflow
308
+ - by_provider: Cost breakdown by provider
309
+
310
+ """
311
+ entries = self.get_recent_entries(limit=100000, days=days)
312
+
313
+ if not entries:
314
+ return {
315
+ "total_calls": 0,
316
+ "total_cost": 0.0,
317
+ "total_tokens_input": 0,
318
+ "total_tokens_output": 0,
319
+ "cache_hits": 0,
320
+ "cache_misses": 0,
321
+ "cache_hit_rate": 0.0,
322
+ "by_tier": {},
323
+ "by_workflow": {},
324
+ "by_provider": {},
325
+ }
326
+
327
+ # Aggregate stats
328
+ total_cost = 0.0
329
+ total_tokens_input = 0
330
+ total_tokens_output = 0
331
+ cache_hits = 0
332
+ cache_misses = 0
333
+ by_tier: dict[str, float] = {}
334
+ by_workflow: dict[str, float] = {}
335
+ by_provider: dict[str, float] = {}
336
+
337
+ for entry in entries:
338
+ cost = entry.get("cost", 0.0)
339
+ tokens = entry.get("tokens", {})
340
+ cache = entry.get("cache", {})
341
+ tier = entry.get("tier", "unknown")
342
+ workflow = entry.get("workflow", "unknown")
343
+ provider = entry.get("provider", "unknown")
344
+
345
+ total_cost += cost
346
+ total_tokens_input += tokens.get("input", 0)
347
+ total_tokens_output += tokens.get("output", 0)
348
+
349
+ if cache.get("hit"):
350
+ cache_hits += 1
351
+ else:
352
+ cache_misses += 1
353
+
354
+ by_tier[tier] = by_tier.get(tier, 0.0) + cost
355
+ by_workflow[workflow] = by_workflow.get(workflow, 0.0) + cost
356
+ by_provider[provider] = by_provider.get(provider, 0.0) + cost
357
+
358
+ total_calls = len(entries)
359
+ cache_hit_rate = (
360
+ (cache_hits / total_calls * 100) if total_calls > 0 else 0.0
361
+ )
362
+
363
+ return {
364
+ "total_calls": total_calls,
365
+ "total_cost": round(total_cost, 2),
366
+ "total_tokens_input": total_tokens_input,
367
+ "total_tokens_output": total_tokens_output,
368
+ "cache_hits": cache_hits,
369
+ "cache_misses": cache_misses,
370
+ "cache_hit_rate": round(cache_hit_rate, 1),
371
+ "by_tier": by_tier,
372
+ "by_workflow": by_workflow,
373
+ "by_provider": by_provider,
374
+ }
375
+
376
+ def calculate_savings(self, days: int = 30) -> dict[str, Any]:
377
+ """Calculate actual savings vs all-PREMIUM baseline.
378
+
379
+ Args:
380
+ days: Number of days to analyze (default: 30)
381
+
382
+ Returns:
383
+ Dictionary with savings calculation:
384
+ - actual_cost: Actual cost with tier routing
385
+ - baseline_cost: Cost if all calls used PREMIUM tier
386
+ - savings: Dollar amount saved
387
+ - savings_percent: Percentage saved
388
+ - tier_distribution: Percentage of calls by tier
389
+ - cache_savings: Additional savings from cache hits
390
+
391
+ """
392
+ entries = self.get_recent_entries(limit=100000, days=days)
393
+
394
+ if not entries:
395
+ return {
396
+ "actual_cost": 0.0,
397
+ "baseline_cost": 0.0,
398
+ "savings": 0.0,
399
+ "savings_percent": 0.0,
400
+ "tier_distribution": {},
401
+ "cache_savings": 0.0,
402
+ "total_calls": 0,
403
+ }
404
+
405
+ # Calculate actual cost
406
+ actual_cost = sum(e.get("cost", 0.0) for e in entries)
407
+
408
+ # Calculate baseline cost (all PREMIUM)
409
+ # Get average PREMIUM cost from actual data, or use standard rate
410
+ premium_costs = [e.get("cost", 0.0) for e in entries if e.get("tier") == "PREMIUM"]
411
+ avg_premium_cost = (sum(premium_costs) / len(premium_costs)) if premium_costs else 0.05
412
+ baseline_cost = len(entries) * avg_premium_cost
413
+
414
+ # Tier distribution
415
+ tier_counts: dict[str, int] = {}
416
+ for entry in entries:
417
+ tier = entry.get("tier", "unknown")
418
+ tier_counts[tier] = tier_counts.get(tier, 0) + 1
419
+
420
+ total_calls = len(entries)
421
+ tier_distribution = {
422
+ tier: round(count / total_calls * 100, 1)
423
+ for tier, count in tier_counts.items()
424
+ }
425
+
426
+ # Cache savings estimation
427
+ cache_hits = sum(1 for e in entries if e.get("cache", {}).get("hit"))
428
+ avg_cost_per_call = actual_cost / total_calls if total_calls > 0 else 0.0
429
+ cache_savings = cache_hits * avg_cost_per_call
430
+
431
+ savings = baseline_cost - actual_cost
432
+ savings_percent = (savings / baseline_cost * 100) if baseline_cost > 0 else 0.0
433
+
434
+ return {
435
+ "actual_cost": round(actual_cost, 2),
436
+ "baseline_cost": round(baseline_cost, 2),
437
+ "savings": round(savings, 2),
438
+ "savings_percent": round(savings_percent, 1),
439
+ "tier_distribution": tier_distribution,
440
+ "cache_savings": round(cache_savings, 2),
441
+ "total_calls": total_calls,
442
+ }
443
+
444
+ def reset(self) -> int:
445
+ """Clear all telemetry data.
446
+
447
+ Returns:
448
+ Number of entries deleted
449
+
450
+ """
451
+ count = 0
452
+ with self._lock:
453
+ for file in self.telemetry_dir.glob("usage*.jsonl"):
454
+ try:
455
+ # Count entries before deleting
456
+ with open(file, encoding="utf-8") as f:
457
+ count += sum(1 for line in f if line.strip())
458
+ file.unlink()
459
+ except OSError:
460
+ # File system errors - log but continue
461
+ logger.debug(f"Failed to delete telemetry file: {file.name}")
462
+
463
+ return count
464
+
465
+ def export_to_dict(self, days: int | None = None) -> list[dict[str, Any]]:
466
+ """Export all entries as list of dicts.
467
+
468
+ Args:
469
+ days: Only export entries from last N days (optional)
470
+
471
+ Returns:
472
+ List of telemetry entries
473
+
474
+ """
475
+ return self.get_recent_entries(limit=1000000, days=days)