|
|
@@ -499,6 +499,17 @@ def cmd_lint_inner(config_path: str, level: str, fmt: str) -> int:
|
|
|
# trace
|
|
|
# ════════════════════════════════════════════════════════════
|
|
|
|
|
|
+def _trace_get(e, key, default=None):
|
|
|
+ """兼容两种 trace 格式(v1: term/duration_ms, v2: term_type+name/elapsed_ms)。"""
|
|
|
+ if key == "term":
|
|
|
+ return e.get("term") or f"{e.get('term_type', '')}:{e.get('name', '')}"
|
|
|
+ if key == "duration_ms":
|
|
|
+ return e.get("duration_ms") or e.get("elapsed_ms", 0)
|
|
|
+ if key == "tokens":
|
|
|
+ return e.get("tokens") or (e.get("tokens_in", 0) + e.get("tokens_out", 0))
|
|
|
+ return e.get(key, default)
|
|
|
+
|
|
|
+
|
|
|
def cmd_trace(args) -> int:
|
|
|
"""查看 β-规约追踪"""
|
|
|
if not os.path.exists(args.file):
|
|
|
@@ -511,12 +522,12 @@ def cmd_trace(args) -> int:
|
|
|
if args.step is not None:
|
|
|
if 0 <= args.step < len(trace):
|
|
|
e = trace[args.step]
|
|
|
- print(f"β[{e['step']}] {e['term']}")
|
|
|
- print(f" Duration: {e['duration_ms']}ms")
|
|
|
+ print(f"β[{e.get('step', args.step)}] {_trace_get(e, 'term')}")
|
|
|
+ print(f" Duration: {_trace_get(e, 'duration_ms'):.0f}ms")
|
|
|
print(f" Model: {e.get('model', 'N/A')}")
|
|
|
- print(f" Tokens: {e.get('tokens', 'N/A')}")
|
|
|
- print(f" Input: {e['input'][:300]}")
|
|
|
- print(f" Output: {e['output'][:300]}")
|
|
|
+ print(f" Tokens: {_trace_get(e, 'tokens') or 'N/A'}")
|
|
|
+ print(f" Input: {str(e.get('input', ''))[:300]}")
|
|
|
+ print(f" Output: {str(e.get('output', ''))[:300]}")
|
|
|
else:
|
|
|
print(f"Step {args.step} not found (total: {len(trace)})")
|
|
|
return 0
|
|
|
@@ -526,20 +537,21 @@ def cmd_trace(args) -> int:
|
|
|
cumulative = 0
|
|
|
print("Time ──────────────────────────────────────────→")
|
|
|
for e in trace:
|
|
|
- ms = e["duration_ms"]
|
|
|
+ ms = _trace_get(e, "duration_ms")
|
|
|
bar = "█" * max(1, int(ms / 100))
|
|
|
- print(f" {cumulative/1000:6.1f}s ├{bar} {e['term']} ({ms:.0f}ms)")
|
|
|
+ print(f" {cumulative/1000:6.1f}s ├{bar} {_trace_get(e, 'term')} ({ms:.0f}ms)")
|
|
|
cumulative += ms
|
|
|
print(f" {cumulative/1000:6.1f}s ┤ END")
|
|
|
return 0
|
|
|
|
|
|
# 默认:打印全部
|
|
|
for e in trace:
|
|
|
- inp = str(e["input"])[:60]
|
|
|
- out = str(e["output"])[:60]
|
|
|
- print(f" β[{e['step']}] {e['term']} ({e['duration_ms']:.0f}ms): {inp} → {out}")
|
|
|
+ inp = str(e.get("input", ""))[:60]
|
|
|
+ out = str(e.get("output", ""))[:60]
|
|
|
+ ms = _trace_get(e, "duration_ms")
|
|
|
+ print(f" β[{e.get('step', '?')}] {_trace_get(e, 'term')} ({ms:.0f}ms): {inp} → {out}")
|
|
|
|
|
|
- total_ms = sum(e["duration_ms"] for e in trace)
|
|
|
+ total_ms = sum(_trace_get(e, "duration_ms") for e in trace)
|
|
|
print(f"\nTotal: {len(trace)} β-reductions, {total_ms/1000:.1f}s")
|
|
|
return 0
|
|
|
|