20) -> tuple[Path, Path]:
22 json_path = cwd / _JSON_NAME
23 png_path = cwd / _PNG_NAME
26 "saved_at": datetime.now(timezone.utc).isoformat(),
30 json_path.write_text(json.dumps(payload, indent=2, default=str), encoding=
"utf-8")
32 return json_path, png_path
37 result: dict[str, Any],
38 tool_name: str |
None,
44 import matplotlib.pyplot
as plt
47 if result.get(
"error"):
48 fig, ax = plt.subplots(figsize=(6, 2), facecolor=
"#0f1117")
50 ax.text(0.5, 0.5, f
"Error: {result['error']}", ha=
"center", va=
"center", wrap=
True, color=
"#e5e7eb")
51 fig.savefig(png_path, dpi=140, bbox_inches=
"tight", facecolor=
"#0f1117")
60 stability = result.get(
"stability")
or {}
61 ood = result.get(
"ood")
or {}
62 localize = result.get(
"localize")
if isinstance(result.get(
"localize"), dict)
else None
63 stab_layers = stability.get(
"layers")
or []
64 ood_layers = ood.get(
"layers")
or []
65 loc_layers = (localize
or {}).get(
"layers")
or []
67 n_cols = 3
if loc_layers
else 2
68 fig, axes = plt.subplots(1, n_cols, figsize=(5.5 * n_cols, 4.5), facecolor=
"#0f1117")
71 model = str(result.get(
"model_id")
or "")
72 title = f
"Layer analysis (LLM) — {model}"
73 if localize
and localize.get(
"collapse_layer")
is not None:
74 title += f
" · collapse L{localize.get('collapse_layer')}"
75 fig.suptitle(title, color=
"#e5e7eb", fontsize=11, y=1.02)
79 xs = [int(r[
"layer"])
for r
in stab_layers]
80 pc1 = [float(r.get(
"top1_variance_ratio")
or 0)
for r
in stab_layers]
83 status = str(r.get(
"status")
or "ok")
84 colors.append({
"dead":
"#f87171",
"collapsed":
"#fbbf24",
"ok":
"#34d399"}.get(status,
"#9ca3af"))
85 ax0.bar(xs, pc1, color=colors, width=0.8)
86 ax0.axhline(0.85, color=
"#fbbf24", linestyle=
"--", linewidth=0.8, alpha=0.6, label=
"collapse")
87 ax0.set_xlabel(
"Layer", color=
"#9ca3af", fontsize=9)
88 ax0.set_ylabel(
"PC1 variance ratio", color=
"#9ca3af", fontsize=9)
89 ax0.set_title(
"Activation stability", color=
"#e5e7eb", fontsize=10)
90 ax0.tick_params(colors=
"#6b7280", labelsize=8)
93 ax0.text(0.5, 0.5,
"No stability data", ha=
"center", va=
"center", color=
"#9ca3af")
97 xs = [int(r[
"layer"])
for r
in ood_layers]
98 sep = [float(r.get(
"separation")
or 0)
for r
in ood_layers]
99 ax1.plot(xs, sep, color=
"#34d399", marker=
"o", markersize=4, linewidth=1.5)
100 peak = ood.get(
"peak_layer")
102 ax1.axvline(int(peak), color=
"#facc15", linestyle=
"--", linewidth=0.8, alpha=0.7)
103 ax1.set_xlabel(
"Layer", color=
"#9ca3af", fontsize=9)
104 ax1.set_ylabel(
"OOD separation", color=
"#9ca3af", fontsize=9)
105 ax1.set_title(
"In-domain vs OOD", color=
"#e5e7eb", fontsize=10)
106 ax1.tick_params(colors=
"#6b7280", labelsize=8)
109 ax1.text(0.5, 0.5,
"No OOD data", ha=
"center", va=
"center", color=
"#9ca3af")
113 xs = [int(r[
"layer"])
for r
in loc_layers]
114 sig = [float(r.get(
"signal")
or 0)
for r
in loc_layers]
115 ax2.plot(xs, sig, color=
"#a78bfa", marker=
"o", markersize=4, linewidth=1.5)
116 peak = localize.get(
"peak_layer")
if localize
else None
117 collapse = localize.get(
"collapse_layer")
if localize
else None
119 ax2.axvline(int(peak), color=
"#34d399", linestyle=
"--", linewidth=0.8, alpha=0.8, label=
"peak")
120 if collapse
is not None:
121 ax2.axvline(int(collapse), color=
"#f87171", linestyle=
"--", linewidth=0.8, alpha=0.8, label=
"collapse")
122 ax2.set_xlabel(
"Layer", color=
"#9ca3af", fontsize=9)
123 ax2.set_ylabel(
"Deception signal", color=
"#9ca3af", fontsize=9)
124 ax2.set_title(
"Localize collapse", color=
"#e5e7eb", fontsize=10)
125 ax2.tick_params(colors=
"#6b7280", labelsize=8)
126 ax2.legend(fontsize=7, labelcolor=
"#9ca3af", frameon=
False)
129 fig.savefig(png_path, dpi=140, bbox_inches=
"tight", facecolor=
"#0f1117")