-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcli.py
More file actions
1231 lines (1006 loc) · 44.5 KB
/
cli.py
File metadata and controls
1231 lines (1006 loc) · 44.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""CLI commands for telemetry tracking.
Provides commands to view, analyze, and manage local usage telemetry data.
Copyright 2025 Smart-AI-Memory
Licensed under Fair Source License 0.9
"""
import csv
import json
import sys
from datetime import datetime
from typing import Any
try:
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.text import Text
RICH_AVAILABLE = True
except ImportError:
RICH_AVAILABLE = False
Console = None # type: ignore
from empathy_os.config import _validate_file_path
from .usage_tracker import UsageTracker
# _validate_file_path is now imported from empathy_os.config
# This eliminates the duplicate definition that previously existed here (lines 30-69)
def cmd_telemetry_show(args: Any) -> int:
"""Show recent telemetry entries.
Args:
args: Parsed command-line arguments
Returns:
Exit code (0 for success)
"""
tracker = UsageTracker.get_instance()
limit = getattr(args, "limit", 20)
days = getattr(args, "days", None)
entries = tracker.get_recent_entries(limit=limit, days=days)
if not entries:
print("No telemetry data found.")
print(f"Data location: {tracker.telemetry_dir}")
return 0
if RICH_AVAILABLE and Console is not None:
console = Console()
table = Table(title="Recent LLM Calls", show_header=True, header_style="bold magenta")
table.add_column("Time", style="cyan", width=19)
table.add_column("Workflow", style="green")
table.add_column("Stage", style="blue")
table.add_column("Tier", style="yellow")
table.add_column("Cost", style="red", justify="right")
table.add_column("Tokens", justify="right")
table.add_column("Cache", style="green")
table.add_column("Duration", justify="right")
total_cost = 0.0
total_duration = 0
for entry in entries:
ts = entry.get("ts", "")
# Format timestamp
try:
dt = datetime.fromisoformat(ts.rstrip("Z"))
ts_display = dt.strftime("%Y-%m-%d %H:%M:%S")
except (ValueError, AttributeError):
ts_display = ts[:19] if len(ts) >= 19 else ts
workflow = entry.get("workflow", "unknown")
stage = entry.get("stage", "-")
tier = entry.get("tier", "unknown")
cost = entry.get("cost", 0.0)
tokens = entry.get("tokens", {})
cache = entry.get("cache", {})
duration_ms = entry.get("duration_ms", 0)
tokens_str = f"{tokens.get('input', 0)}/{tokens.get('output', 0)}"
cache_str = "HIT" if cache.get("hit") else "MISS"
if cache.get("hit"):
cache_type = cache.get("type", "")
if cache_type:
cache_str += f" ({cache_type})"
table.add_row(
ts_display,
workflow[:20],
stage[:15] if stage else "-",
tier,
f"${cost:.4f}",
tokens_str,
cache_str,
f"{duration_ms}ms",
)
total_cost += cost
total_duration += duration_ms
console.print(table)
console.print()
console.print(f"[bold]Total Cost:[/bold] ${total_cost:.4f}")
console.print(f"[bold]Avg Duration:[/bold] {total_duration // len(entries)}ms")
console.print(f"\n[dim]Data location: {tracker.telemetry_dir}[/dim]")
else:
# Fallback to plain text
print(
f"\n{'Time':<19} {'Workflow':<20} {'Stage':<15} {'Tier':<10} {'Cost':>10} {'Cache':<10} {'Duration':>10}"
)
print("-" * 120)
total_cost = 0.0
for entry in entries:
ts = entry.get("ts", "")[:19]
workflow = entry.get("workflow", "unknown")[:20]
stage = entry.get("stage", "-")[:15]
tier = entry.get("tier", "unknown")
cost = entry.get("cost", 0.0)
cache = entry.get("cache", {})
duration_ms = entry.get("duration_ms", 0)
cache_str = "HIT" if cache.get("hit") else "MISS"
print(
f"{ts:<19} {workflow:<20} {stage:<15} {tier:<10} ${cost:>9.4f} {cache_str:<10} {duration_ms:>9}ms"
)
total_cost += cost
print("-" * 120)
print(f"Total Cost: ${total_cost:.4f}")
print(f"\nData location: {tracker.telemetry_dir}")
return 0
def cmd_telemetry_savings(args: Any) -> int:
"""Calculate and display cost savings.
Args:
args: Parsed command-line arguments
Returns:
Exit code (0 for success)
"""
tracker = UsageTracker.get_instance()
days = getattr(args, "days", 30)
savings = tracker.calculate_savings(days=days)
if savings["total_calls"] == 0:
print("No telemetry data found for the specified period.")
return 0
if RICH_AVAILABLE and Console is not None:
console = Console()
# Create savings report
title = Text("Cost Savings Analysis", style="bold magenta")
content_lines = []
content_lines.append(f"Period: Last {days} days")
content_lines.append("")
content_lines.append("Usage Pattern:")
for tier, pct in sorted(savings["tier_distribution"].items()):
content_lines.append(f" {tier:8}: {pct:5.1f}%")
content_lines.append("")
content_lines.append("Cost Comparison:")
content_lines.append(f" Baseline (all PREMIUM): ${savings['baseline_cost']:.2f}")
content_lines.append(f" Actual (tier routing): ${savings['actual_cost']:.2f}")
content_lines.append("")
savings_color = "green" if savings["savings"] > 0 else "red"
content_lines.append(
f"[bold {savings_color}]YOUR SAVINGS: ${savings['savings']:.2f} ({savings['savings_percent']:.1f}%)[/bold {savings_color}]"
)
content_lines.append("")
content_lines.append(f"Cache savings: ${savings['cache_savings']:.2f}")
content_lines.append(f"Total calls: {savings['total_calls']}")
panel = Panel(
"\n".join(content_lines),
title=title,
border_style="cyan",
)
console.print(panel)
else:
# Fallback to plain text
print("\n" + "=" * 60)
print("COST SAVINGS ANALYSIS")
print("=" * 60)
print(f"Period: Last {days} days\n")
print("Usage Pattern:")
for tier, pct in sorted(savings["tier_distribution"].items()):
print(f" {tier:8}: {pct:5.1f}%")
print("\nCost Comparison:")
print(f" Baseline (all PREMIUM): ${savings['baseline_cost']:.2f}")
print(f" Actual (tier routing): ${savings['actual_cost']:.2f}")
print(f"\nYOUR SAVINGS: ${savings['savings']:.2f} ({savings['savings_percent']:.1f}%)")
print(f"\nCache savings: ${savings['cache_savings']:.2f}")
print(f"Total calls: {savings['total_calls']}")
print("=" * 60)
return 0
def cmd_telemetry_cache_stats(args: Any) -> int:
"""Show prompt caching performance statistics.
Displays cache hit rates, cost savings, and workflow-level stats.
Args:
args: Parsed command-line arguments
Returns:
Exit code (0 for success)
"""
tracker = UsageTracker.get_instance()
days = getattr(args, "days", 7)
stats = tracker.get_cache_stats(days=days)
if stats["total_requests"] == 0:
print("No telemetry data found for cache analysis.")
print(f"Data location: {tracker.telemetry_dir}")
return 0
if RICH_AVAILABLE and Console is not None:
console = Console()
# Main stats table
table = Table(
title=f"Prompt Caching Stats (Last {days} Days)",
show_header=True,
header_style="bold magenta",
)
table.add_column("Metric", style="cyan")
table.add_column("Value", style="green", justify="right")
# Cache hit rate
hit_rate_color = "green" if stats["hit_rate"] > 0.5 else "yellow"
table.add_row(
"Cache Hit Rate",
f"[{hit_rate_color}]{stats['hit_rate']:.1%}[/{hit_rate_color}]",
)
# Tokens
table.add_row("Cache Reads", f"{stats['total_reads']:,} tokens")
table.add_row("Cache Writes", f"{stats['total_writes']:,} tokens")
# Cost savings
savings_color = "green" if stats["savings"] > 0 else "dim"
table.add_row(
"Estimated Savings",
f"[bold {savings_color}]${stats['savings']:.2f}[/bold {savings_color}]",
)
# Requests
table.add_row("Requests with Cache Hits", f"{stats['hit_count']:,}")
table.add_row("Total Requests", f"{stats['total_requests']:,}")
console.print(table)
# Per-workflow breakdown
if stats["by_workflow"]:
console.print("\n")
wf_table = Table(
title="Cache Performance by Workflow",
show_header=True,
header_style="bold magenta",
)
wf_table.add_column("Workflow", style="cyan")
wf_table.add_column("Hit Rate", justify="right")
wf_table.add_column("Reads", justify="right")
wf_table.add_column("Writes", justify="right")
# Sort by hit rate descending
sorted_workflows = sorted(
stats["by_workflow"].items(),
key=lambda x: x[1].get("hit_rate", 0),
reverse=True,
)
for workflow, wf_stats in sorted_workflows[:10]: # Top 10
hit_rate = wf_stats.get("hit_rate", 0.0)
hit_rate_color = "green" if hit_rate > 0.5 else "yellow"
wf_table.add_row(
workflow,
f"[{hit_rate_color}]{hit_rate:.1%}[/{hit_rate_color}]",
f"{wf_stats['reads']:,}",
f"{wf_stats['writes']:,}",
)
console.print(wf_table)
# Recommendations
if stats["hit_rate"] < 0.3:
console.print("\n")
console.print(
Panel(
"[yellow]⚠ Cache hit rate is low (<30%)[/yellow]\n\n"
"Recommendations:\n"
" • Increase reuse of system prompts across requests\n"
" • Group similar requests together (5-min cache TTL)\n"
" • Consider using workflow batching\n"
" • Structure prompts with static content first",
title="Optimization Tips",
border_style="yellow",
)
)
else:
# Fallback to plain text
print("\n" + "=" * 60)
print(f"PROMPT CACHING STATS (LAST {days} DAYS)")
print("=" * 60)
print(f"Cache Hit Rate: {stats['hit_rate']:.1%}")
print(f"Cache Reads: {stats['total_reads']:,} tokens")
print(f"Cache Writes: {stats['total_writes']:,} tokens")
print(f"Estimated Savings: ${stats['savings']:.2f}")
print(f"Requests with Cache Hits: {stats['hit_count']:,}")
print(f"Total Requests: {stats['total_requests']:,}")
print("=" * 60)
if stats["hit_rate"] < 0.3:
print("\n⚠ Cache hit rate is low (<30%)")
print("Recommendations:")
print(" • Increase reuse of system prompts across requests")
print(" • Group similar requests together (5-min cache TTL)")
print(" • Consider using workflow batching")
return 0
def cmd_telemetry_compare(args: Any) -> int:
"""Compare telemetry across two time periods.
Args:
args: Parsed command-line arguments
Returns:
Exit code (0 for success)
"""
tracker = UsageTracker.get_instance()
period1_days = getattr(args, "period1", 7)
period2_days = getattr(args, "period2", 30)
# Get stats for both periods
stats1 = tracker.get_stats(days=period1_days)
stats2 = tracker.get_stats(days=period2_days)
if stats1["total_calls"] == 0 or stats2["total_calls"] == 0:
print("Insufficient telemetry data for comparison.")
return 0
if RICH_AVAILABLE and Console is not None:
console = Console()
table = Table(title="Telemetry Comparison", show_header=True, header_style="bold magenta")
table.add_column("Metric", style="cyan")
table.add_column(f"Last {period1_days} days", justify="right", style="green")
table.add_column(f"Last {period2_days} days", justify="right", style="yellow")
table.add_column("Change", justify="right", style="blue")
# Total calls
calls_change = (
((stats1["total_calls"] - stats2["total_calls"]) / stats2["total_calls"] * 100)
if stats2["total_calls"] > 0
else 0
)
table.add_row(
"Total Calls",
str(stats1["total_calls"]),
str(stats2["total_calls"]),
f"{calls_change:+.1f}%",
)
# Total cost
cost_change = (
((stats1["total_cost"] - stats2["total_cost"]) / stats2["total_cost"] * 100)
if stats2["total_cost"] > 0
else 0
)
table.add_row(
"Total Cost",
f"${stats1['total_cost']:.2f}",
f"${stats2['total_cost']:.2f}",
f"{cost_change:+.1f}%",
)
# Avg cost per call
avg1 = stats1["total_cost"] / stats1["total_calls"] if stats1["total_calls"] > 0 else 0
avg2 = stats2["total_cost"] / stats2["total_calls"] if stats2["total_calls"] > 0 else 0
avg_change = ((avg1 - avg2) / avg2 * 100) if avg2 > 0 else 0
table.add_row(
"Avg Cost/Call",
f"${avg1:.4f}",
f"${avg2:.4f}",
f"{avg_change:+.1f}%",
)
# Cache hit rate
cache_change = stats1["cache_hit_rate"] - stats2["cache_hit_rate"]
table.add_row(
"Cache Hit Rate",
f"{stats1['cache_hit_rate']:.1f}%",
f"{stats2['cache_hit_rate']:.1f}%",
f"{cache_change:+.1f}pp",
)
console.print(table)
else:
# Fallback to plain text
print("\n" + "=" * 80)
print("TELEMETRY COMPARISON")
print("=" * 80)
print(
f"{'Metric':<20} {'Last ' + str(period1_days) + ' days':>20} {'Last ' + str(period2_days) + ' days':>20} {'Change':>15}"
)
print("-" * 80)
calls_change = (
((stats1["total_calls"] - stats2["total_calls"]) / stats2["total_calls"] * 100)
if stats2["total_calls"] > 0
else 0
)
print(
f"{'Total Calls':<20} {stats1['total_calls']:>20} {stats2['total_calls']:>20} {calls_change:>14.1f}%"
)
cost_change = (
((stats1["total_cost"] - stats2["total_cost"]) / stats2["total_cost"] * 100)
if stats2["total_cost"] > 0
else 0
)
print(
f"{'Total Cost':<20} ${stats1['total_cost']:>19.2f} ${stats2['total_cost']:>19.2f} {cost_change:>14.1f}%"
)
avg1 = stats1["total_cost"] / stats1["total_calls"] if stats1["total_calls"] > 0 else 0
avg2 = stats2["total_cost"] / stats2["total_calls"] if stats2["total_calls"] > 0 else 0
avg_change = ((avg1 - avg2) / avg2 * 100) if avg2 > 0 else 0
print(f"{'Avg Cost/Call':<20} ${avg1:>19.4f} ${avg2:>19.4f} {avg_change:>14.1f}%")
cache_change = stats1["cache_hit_rate"] - stats2["cache_hit_rate"]
print(
f"{'Cache Hit Rate':<20} {stats1['cache_hit_rate']:>19.1f}% {stats2['cache_hit_rate']:>19.1f}% {cache_change:>14.1f}pp"
)
print("=" * 80)
return 0
def cmd_telemetry_reset(args: Any) -> int:
"""Reset/clear all telemetry data.
Args:
args: Parsed command-line arguments
Returns:
Exit code (0 for success)
"""
tracker = UsageTracker.get_instance()
confirm = getattr(args, "confirm", False)
if not confirm:
print("WARNING: This will permanently delete all telemetry data.")
print(f"Location: {tracker.telemetry_dir}")
print("\nUse --confirm to proceed.")
return 1
count = tracker.reset()
print(f"Deleted {count} telemetry entries.")
print("New tracking starts now.")
return 0
def cmd_telemetry_export(args: Any) -> int:
"""Export telemetry data to JSON or CSV.
Args:
args: Parsed command-line arguments
Returns:
Exit code (0 for success)
"""
tracker = UsageTracker.get_instance()
format_type = getattr(args, "format", "json")
output_file = getattr(args, "output", None)
days = getattr(args, "days", None)
entries = tracker.export_to_dict(days=days)
if not entries:
print("No telemetry data to export.")
return 0
if format_type == "json":
# Export as JSON
if output_file:
validated_path = _validate_file_path(output_file)
with open(validated_path, "w", encoding="utf-8") as f:
json.dump(entries, f, indent=2)
print(f"Exported {len(entries)} entries to {validated_path}")
else:
print(json.dumps(entries, indent=2))
elif format_type == "csv":
# Export as CSV
if not entries:
print("No data to export.")
return 0
# Get all possible fields
fieldnames = [
"ts",
"workflow",
"stage",
"tier",
"model",
"provider",
"cost",
"tokens_input",
"tokens_output",
"cache_hit",
"cache_type",
"duration_ms",
]
if output_file:
validated_path = _validate_file_path(output_file)
with open(validated_path, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for entry in entries:
row = {
"ts": entry.get("ts", ""),
"workflow": entry.get("workflow", ""),
"stage": entry.get("stage", ""),
"tier": entry.get("tier", ""),
"model": entry.get("model", ""),
"provider": entry.get("provider", ""),
"cost": entry.get("cost", 0.0),
"tokens_input": entry.get("tokens", {}).get("input", 0),
"tokens_output": entry.get("tokens", {}).get("output", 0),
"cache_hit": entry.get("cache", {}).get("hit", False),
"cache_type": entry.get("cache", {}).get("type", ""),
"duration_ms": entry.get("duration_ms", 0),
}
writer.writerow(row)
print(f"Exported {len(entries)} entries to {validated_path}")
else:
# Print to stdout
writer = csv.DictWriter(sys.stdout, fieldnames=fieldnames)
writer.writeheader()
for entry in entries:
row = {
"ts": entry.get("ts", ""),
"workflow": entry.get("workflow", ""),
"stage": entry.get("stage", ""),
"tier": entry.get("tier", ""),
"model": entry.get("model", ""),
"provider": entry.get("provider", ""),
"cost": entry.get("cost", 0.0),
"tokens_input": entry.get("tokens", {}).get("input", 0),
"tokens_output": entry.get("tokens", {}).get("output", 0),
"cache_hit": entry.get("cache", {}).get("hit", False),
"cache_type": entry.get("cache", {}).get("type", ""),
"duration_ms": entry.get("duration_ms", 0),
}
writer.writerow(row)
else:
print(f"Unknown format: {format_type}")
print("Supported formats: json, csv")
return 1
return 0
# ==============================================================================
# Dashboard Commands
# ==============================================================================
# cmd_telemetry_dashboard and cmd_file_test_dashboard have been moved to:
# src/empathy_os/telemetry/commands/dashboard_commands.py
# They are imported at the top of this file for backward compatibility.
# ==============================================================================
# ==============================================================================
# Tier 1 Automation Monitoring CLI Commands
# ==============================================================================
# ==============================================================================
# Dashboard Commands (Extracted to Separate Module)
# ==============================================================================
# cmd_telemetry_dashboard and cmd_file_test_dashboard moved to:
# src/empathy_os/telemetry/commands/dashboard_commands.py
# Imported at top of file for backward compatibility.
# ==============================================================================
def cmd_tier1_status(args: Any) -> int:
"""Show comprehensive Tier 1 automation status.
Args:
args: Parsed command-line arguments (hours)
Returns:
Exit code (0 for success)
"""
from datetime import timedelta
from empathy_os.models.telemetry import TelemetryAnalytics, get_telemetry_store
try:
store = get_telemetry_store()
analytics = TelemetryAnalytics(store)
hours = getattr(args, "hours", 24)
since = datetime.utcnow() - timedelta(hours=hours)
summary = analytics.tier1_summary(since=since)
except Exception as e:
print(f"Error retrieving Tier 1 status: {e}")
return 1
if RICH_AVAILABLE and Console is not None:
console = Console()
# Task Routing Panel
routing = summary["task_routing"]
routing_text = Text()
routing_text.append(f"Total Tasks: {routing['total_tasks']}\n")
routing_text.append(f"Success Rate: {routing['accuracy_rate']:.1%}\n", style="green bold")
routing_text.append(f"Avg Confidence: {routing['avg_confidence']:.2f}\n")
# Test Execution Panel
tests = summary["test_execution"]
tests_text = Text()
tests_text.append(f"Total Runs: {tests['total_executions']}\n")
tests_text.append(f"Success Rate: {tests['success_rate']:.1%}\n", style="green bold")
tests_text.append(f"Avg Duration: {tests['avg_duration_seconds']:.1f}s\n")
tests_text.append(f"Total Failures: {tests['total_failures']}\n")
# Coverage Panel
coverage = summary["coverage"]
coverage_text = Text()
coverage_text.append(f"Current: {coverage['current_coverage']:.1f}%\n", style="cyan bold")
coverage_text.append(f"Change: {coverage['change']:+.1f}%\n")
coverage_text.append(f"Trend: {coverage['trend']}\n")
coverage_text.append(f"Critical Gaps: {coverage['critical_gaps_count']}\n")
# Agent Performance Panel
agent = summary["agent_performance"]
agent_text = Text()
agent_text.append(f"Active Agents: {len(agent['by_agent'])}\n")
agent_text.append(f"Automation Rate: {agent['automation_rate']:.1%}\n", style="green bold")
agent_text.append(f"Human Review Rate: {agent['human_review_rate']:.1%}\n")
# Display all panels
console.print(f"\n[bold]Tier 1 Automation Status[/bold] (last {hours} hours)\n")
console.print(Panel(routing_text, title="Task Routing", border_style="blue"))
console.print(Panel(tests_text, title="Test Execution", border_style="green"))
console.print(Panel(coverage_text, title="Coverage", border_style="cyan"))
console.print(Panel(agent_text, title="Agent Performance", border_style="magenta"))
else:
# Plain text fallback
routing = summary["task_routing"]
tests = summary["test_execution"]
coverage = summary["coverage"]
agent = summary["agent_performance"]
print(f"\nTier 1 Automation Status (last {hours} hours)")
print("=" * 50)
print("\nTask Routing:")
print(f" Total Tasks: {routing['total_tasks']}")
print(f" Success Rate: {routing['accuracy_rate']:.1%}")
print(f" Avg Confidence: {routing['avg_confidence']:.2f}")
print("\nTest Execution:")
print(f" Total Runs: {tests['total_executions']}")
print(f" Success Rate: {tests['success_rate']:.1%}")
print(f" Avg Duration: {tests['avg_duration_seconds']:.1f}s")
print("\nCoverage:")
print(f" Current: {coverage['current_coverage']:.1f}%")
print(f" Trend: {coverage['trend']}")
print("\nAgent Performance:")
print(f" Active Agents: {len(agent['by_agent'])}")
print(f" Automation Rate: {agent['automation_rate']:.1%}")
return 0
def cmd_task_routing_report(args: Any) -> int:
"""Show detailed task routing report.
Args:
args: Parsed command-line arguments (hours)
Returns:
Exit code (0 for success)
"""
from datetime import timedelta
from empathy_os.models.telemetry import TelemetryAnalytics, get_telemetry_store
try:
store = get_telemetry_store()
analytics = TelemetryAnalytics(store)
hours = getattr(args, "hours", 24)
since = datetime.utcnow() - timedelta(hours=hours)
stats = analytics.task_routing_accuracy(since=since)
except Exception as e:
print(f"Error retrieving task routing report: {e}")
return 1
if not stats["total_tasks"]:
print(f"No task routing data found in the last {hours} hours.")
return 0
if RICH_AVAILABLE and Console is not None:
console = Console()
# Summary table
table = Table(title=f"Task Routing Report (last {hours} hours)")
table.add_column("Metric", style="cyan")
table.add_column("Value", style="green", justify="right")
table.add_row("Total Tasks", str(stats["total_tasks"]))
table.add_row("Successful", str(stats["successful_routing"]))
table.add_row("Accuracy Rate", f"{stats['accuracy_rate']:.1%}")
table.add_row("Avg Confidence", f"{stats['avg_confidence']:.2f}")
console.print(table)
# By task type table
if stats["by_task_type"]:
type_table = Table(title="Breakdown by Task Type")
type_table.add_column("Task Type", style="cyan")
type_table.add_column("Total", justify="right")
type_table.add_column("Success", justify="right")
type_table.add_column("Rate", justify="right", style="green")
for task_type, data in stats["by_task_type"].items():
type_table.add_row(
task_type, str(data["total"]), str(data["success"]), f"{data['rate']:.1%}"
)
console.print(type_table)
else:
# Plain text fallback
print(f"\nTask Routing Report (last {hours} hours)")
print("=" * 50)
print(f"Total Tasks: {stats['total_tasks']}")
print(f"Successful: {stats['successful_routing']}")
print(f"Accuracy Rate: {stats['accuracy_rate']:.1%}")
print(f"Avg Confidence: {stats['avg_confidence']:.2f}")
if stats["by_task_type"]:
print("\nBy Task Type:")
for task_type, data in stats["by_task_type"].items():
print(f" {task_type}: {data['success']}/{data['total']} ({data['rate']:.1%})")
return 0
def cmd_test_status(args: Any) -> int:
"""Show test execution status.
Args:
args: Parsed command-line arguments (hours)
Returns:
Exit code (0 for success)
"""
from datetime import timedelta
from empathy_os.models.telemetry import TelemetryAnalytics, get_telemetry_store
try:
store = get_telemetry_store()
analytics = TelemetryAnalytics(store)
hours = getattr(args, "hours", 24)
since = datetime.utcnow() - timedelta(hours=hours)
stats = analytics.test_execution_trends(since=since)
coverage = analytics.coverage_progress(since=since)
except Exception as e:
print(f"Error retrieving test status: {e}")
return 1
if not stats["total_executions"]:
print(f"No test execution data found in the last {hours} hours.")
return 0
if RICH_AVAILABLE and Console is not None:
console = Console()
# Test execution table
table = Table(title=f"Test Execution Status (last {hours} hours)")
table.add_column("Metric", style="cyan")
table.add_column("Value", style="green", justify="right")
table.add_row("Total Runs", str(stats["total_executions"]))
table.add_row("Success Rate", f"{stats['success_rate']:.1%}")
table.add_row("Avg Duration", f"{stats['avg_duration_seconds']:.1f}s")
table.add_row("Total Tests Run", str(stats["total_tests_run"]))
table.add_row("Total Failures", str(stats["total_failures"]))
table.add_row("Current Coverage", f"{coverage['current_coverage']:.1f}%")
table.add_row("Coverage Trend", coverage["trend"])
console.print(table)
# Most failing tests
if stats["most_failing_tests"]:
fail_table = Table(title="Most Frequently Failing Tests")
fail_table.add_column("Test Name", style="cyan")
fail_table.add_column("Failures", justify="right", style="red")
for test in stats["most_failing_tests"][:10]:
fail_table.add_row(test["name"], str(test["failures"]))
console.print(fail_table)
else:
# Plain text fallback
print(f"\nTest Execution Status (last {hours} hours)")
print("=" * 50)
print(f"Total Runs: {stats['total_executions']}")
print(f"Success Rate: {stats['success_rate']:.1%}")
print(f"Avg Duration: {stats['avg_duration_seconds']:.1f}s")
print(f"Total Tests Run: {stats['total_tests_run']}")
print(f"Total Failures: {stats['total_failures']}")
print(f"Current Coverage: {coverage['current_coverage']:.1f}%")
if stats["most_failing_tests"]:
print("\nMost Frequently Failing Tests:")
for test in stats["most_failing_tests"][:10]:
print(f" {test['name']}: {test['failures']} failures")
return 0
def cmd_agent_performance(args: Any) -> int:
"""Show agent performance metrics.
Args:
args: Parsed command-line arguments (hours)
Returns:
Exit code (0 for success)
"""
from datetime import timedelta
from empathy_os.models.telemetry import TelemetryAnalytics, get_telemetry_store
try:
store = get_telemetry_store()
analytics = TelemetryAnalytics(store)
hours = getattr(args, "hours", 168) # Default 7 days for agent performance
since = datetime.utcnow() - timedelta(hours=hours)
stats = analytics.agent_performance(since=since)
except Exception as e:
print(f"Error retrieving agent performance: {e}")
return 1
if not stats["by_agent"]:
print(f"No agent assignment data found in the last {hours} hours.")
return 0
if RICH_AVAILABLE and Console is not None:
console = Console()
# Agent performance table
table = Table(title=f"Agent Performance (last {hours} hours)")
table.add_column("Agent", style="cyan")
table.add_column("Assignments", justify="right")
table.add_column("Completed", justify="right")
table.add_column("Success Rate", justify="right", style="green")
table.add_column("Avg Duration", justify="right")
for agent, data in stats["by_agent"].items():
table.add_row(
agent,
str(data["assignments"]),
str(data["completed"]),
f"{data['success_rate']:.1%}",
f"{data['avg_duration_hours']:.2f}h",
)
console.print(table)
# Summary panel
summary_text = Text()
summary_text.append(
f"Automation Rate: {stats['automation_rate']:.1%}\n", style="green bold"
)
summary_text.append(f"Human Review Rate: {stats['human_review_rate']:.1%}\n")
console.print(Panel(summary_text, title="Summary", border_style="blue"))
else:
# Plain text fallback
print(f"\nAgent Performance (last {hours} hours)")
print("=" * 50)
for agent, data in stats["by_agent"].items():
print(f"\n{agent}:")
print(f" Assignments: {data['assignments']}")
print(f" Completed: {data['completed']}")
print(f" Success Rate: {data['success_rate']:.1%}")
print(f" Avg Duration: {data['avg_duration_hours']:.2f}h")
print(f"\nAutomation Rate: {stats['automation_rate']:.1%}")
print(f"Human Review Rate: {stats['human_review_rate']:.1%}")
return 0
def cmd_sonnet_opus_analysis(args: Any) -> int:
"""Show Sonnet 4.5 → Opus 4.5 fallback analysis and cost savings.
Args:
args: Parsed command-line arguments (days)
Returns:
Exit code (0 for success)
"""
from datetime import timedelta
from empathy_os.models.telemetry import TelemetryAnalytics, get_telemetry_store
store = get_telemetry_store()
analytics = TelemetryAnalytics(store)
days = getattr(args, "days", 30)
since = datetime.utcnow() - timedelta(days=days)
stats = analytics.sonnet_opus_fallback_analysis(since=since)
if stats["total_calls"] == 0:
print(f"No Sonnet/Opus calls found in the last {days} days.")
return 0
if RICH_AVAILABLE and Console is not None:
console = Console()
# Fallback Performance Panel
perf_text = Text()
perf_text.append(f"Total Anthropic Calls: {stats['total_calls']}\n")
perf_text.append(f"Sonnet 4.5 Attempts: {stats['sonnet_attempts']}\n")
perf_text.append(
f"Sonnet Success Rate: {stats['success_rate_sonnet']:.1f}%\n",
style="green bold",
)
perf_text.append(f"Opus Fallbacks: {stats['opus_fallbacks']}\n")
perf_text.append(
f"Fallback Rate: {stats['fallback_rate']:.1f}%\n",
style="yellow bold" if stats["fallback_rate"] > 10 else "green",
)
console.print(
Panel(
perf_text,
title=f"Sonnet 4.5 → Opus 4.5 Fallback Performance (last {days} days)",
border_style="cyan",
)
)
# Cost Savings Panel
savings_text = Text()
savings_text.append(f"Actual Cost: ${stats['actual_cost']:.2f}\n")
savings_text.append(f"Always-Opus Cost: ${stats['always_opus_cost']:.2f}\n")
savings_text.append(
f"Savings: ${stats['savings']:.2f} ({stats['savings_percent']:.1f}%)\n",
style="green bold",
)
savings_text.append("\n")
savings_text.append(f"Avg Cost/Call (actual): ${stats['avg_cost_per_call']:.4f}\n")
savings_text.append(f"Avg Cost/Call (all Opus): ${stats['avg_opus_cost_per_call']:.4f}\n")
console.print(Panel(savings_text, title="Cost Savings Analysis", border_style="green"))
# Recommendation
if stats["fallback_rate"] < 5:
rec_text = Text()
rec_text.append("✅ Excellent Performance!\n", style="green bold")
rec_text.append(