File size: 26,790 Bytes
9d24374
 
 
 
 
 
 
 
 
 
 
9f9fbec
 
 
 
 
9d24374
 
 
b784950
 
 
 
9d24374
 
 
 
 
b784950
 
 
9d24374
b784950
9d24374
 
b784950
9d24374
 
b3d11b8
b784950
 
 
b3d11b8
 
b784950
 
 
 
 
9d24374
b784950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3d11b8
b784950
 
 
 
 
 
 
 
 
 
 
 
b3d11b8
b784950
9d24374
b784950
 
 
 
 
 
 
 
 
 
 
9d24374
b784950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a2b2e4
 
b3d11b8
 
 
 
 
 
 
 
b784950
b3d11b8
6d68f94
b784950
6d68f94
 
b784950
6d68f94
 
b784950
 
 
 
 
 
 
6d68f94
b784950
6d68f94
b784950
 
 
 
 
 
b3d11b8
b784950
 
 
 
 
 
 
b3d11b8
b784950
 
 
 
 
 
b3d11b8
b784950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3d11b8
 
9d24374
b784950
 
 
 
 
 
 
3a2b2e4
b784950
 
 
 
 
b3d11b8
b784950
 
 
 
 
 
 
 
b3d11b8
b784950
 
b3d11b8
b784950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3d11b8
b784950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3d11b8
b784950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d24374
b784950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d24374
b784950
 
 
 
 
9d24374
b784950
9d24374
b784950
 
 
 
9d24374
 
 
b784950
 
 
 
9d24374
 
b784950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffa621b
b784950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3d11b8
 
b784950
 
 
 
 
 
9d24374
b784950
 
 
 
 
9d24374
b784950
9d24374
b784950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d24374
b784950
 
 
 
b3d11b8
b784950
 
 
 
 
b3d11b8
 
b784950
 
 
 
 
 
 
 
 
 
 
 
3a2b2e4
 
b784950
 
 
 
 
 
 
 
3a2b2e4
 
 
9d24374
b784950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d24374
 
b784950
 
 
 
9d24374
b784950
 
 
 
 
 
b3d11b8
b784950
 
9d24374
b784950
9d24374
 
b784950
9d24374
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
from __future__ import annotations

import argparse
import json
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from experiments.common import ARTIFACT_DIR
from featurelens.stats import (
    bootstrap_mean_ci,
    paired_bootstrap_difference_ci,
    paired_sign_flip_pvalue,
)


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Build a truthful experiment report from saved metrics."
    )
    parser.add_argument("--artifact-dir", type=Path, default=ARTIFACT_DIR)
    return parser.parse_args()


def _selected_features(catalog: pd.DataFrame) -> pd.DataFrame:
    scored = catalog.copy()
    scored["activation_contrast"] = (
        scored["activation_rate_pos"] - scored["activation_rate_neg"]
    )
    ordered = scored.sort_values(
        ["concept", "train_auroc", "activation_contrast"],
        ascending=[True, False, False],
    )
    return ordered.groupby("concept", as_index=False).first()


def _effect_column(frame: pd.DataFrame) -> str:
    if "target_mean_logprob_delta" in frame.columns:
        return "target_mean_logprob_delta"
    return "target_logprob_delta"


def _causal_file(artifact_dir: Path, policy: str) -> Path:
    if policy == "final_token":
        name = "causal_results_final_token.csv"
    else:
        name = "causal_results_max_active.csv"

    path = artifact_dir / name
    if path.exists():
        return path

    legacy = artifact_dir / "causal_results.csv"
    if policy == "final_token" and legacy.exists():
        return legacy
    return path


def _task_level_stats(
    frame: pd.DataFrame,
    *,
    seed: int,
    active_only: bool = False,
) -> dict:
    metric = _effect_column(frame)
    sae_rows = frame[frame["condition"] == "sae_feature"].copy()

    if active_only:
        if "feature_active_at_intervention" in frame.columns:
            active_col = "feature_active_at_intervention"
        else:
            active_col = "feature_activation"

        active_mask = (
            pd.to_numeric(sae_rows[active_col], errors="coerce").fillna(0) > 0
        )
        active_ids = sae_rows.loc[active_mask, "task_id"].unique()
        frame = frame[frame["task_id"].isin(active_ids)].copy()
        sae_rows = frame[frame["condition"] == "sae_feature"].copy()

    sae = (
        sae_rows.assign(
            _abs=lambda data: np.abs(
                pd.to_numeric(data[metric], errors="coerce")
            )
        )
        .groupby("task_id", as_index=False)
        .agg(sae_abs=("_abs", "mean"))
    )
    random = (
        frame[frame["condition"] == "random_norm_matched"]
        .assign(
            _abs=lambda data: np.abs(
                pd.to_numeric(data[metric], errors="coerce")
            )
        )
        .groupby(["task_id", "intervention"], as_index=False)["_abs"]
        .mean()
        .groupby("task_id", as_index=False)["_abs"]
        .mean()
        .rename(columns={"_abs": "random_abs"})
    )
    paired = sae.merge(random, on="task_id", how="inner")

    if paired.empty:
        nan = float("nan")
        return {
            "sae_abs": nan,
            "random_abs": nan,
            "ratio": nan,
            "advantage": nan,
            "ci": [nan, nan],
            "pvalue": nan,
            "n_tasks": 0,
        }

    sae_effect = paired["sae_abs"].to_numpy(float)
    random_effect = paired["random_abs"].to_numpy(float)
    low, high = paired_bootstrap_difference_ci(
        sae_effect,
        random_effect,
        seed=seed,
    )
    return {
        "sae_abs": float(sae_effect.mean()),
        "random_abs": float(random_effect.mean()),
        "ratio": float(
            sae_effect.mean() / max(float(random_effect.mean()), 1e-12)
        ),
        "advantage": float((sae_effect - random_effect).mean()),
        "ci": [float(low), float(high)],
        "pvalue": float(
            paired_sign_flip_pvalue(
                sae_effect,
                random_effect,
                seed=seed + 1,
            )
        ),
        "n_tasks": int(len(paired)),
    }


def _paired_stats(
    frame: pd.DataFrame,
    *,
    index: list[str],
    sae_condition: str,
    random_condition: str,
    seed: int,
) -> dict[str, float | list[float]]:
    """Legacy helper retained for report-control regression tests."""
    metric = _effect_column(frame)
    sae = (
        frame[frame["condition"] == sae_condition]
        .groupby(index, as_index=False)[metric]
        .first()
        .rename(columns={metric: "sae_effect"})
    )
    random = (
        frame[frame["condition"] == random_condition]
        .assign(
            _abs_effect=lambda data: np.abs(
                pd.to_numeric(data[metric], errors="coerce")
            )
        )
        .groupby(index, as_index=False)["_abs_effect"]
        .mean()
        .rename(columns={"_abs_effect": "random_abs_effect"})
    )
    paired = sae.merge(random, on=index, how="inner")
    sae_effect = np.abs(paired["sae_effect"].to_numpy(dtype=float))
    random_effect = paired["random_abs_effect"].to_numpy(dtype=float)

    if sae_effect.size == 0:
        nan = float("nan")
        return {
            "sae_abs": nan,
            "random_abs": nan,
            "ratio": nan,
            "paired_advantage": nan,
            "ci": [nan, nan],
            "pvalue": nan,
            "n_pairs": 0,
        }

    low, high = paired_bootstrap_difference_ci(
        sae_effect,
        random_effect,
        seed=seed,
    )
    return {
        "sae_abs": float(sae_effect.mean()),
        "random_abs": float(random_effect.mean()),
        "ratio": float(
            sae_effect.mean() / max(float(random_effect.mean()), 1e-12)
        ),
        "paired_advantage": float((sae_effect - random_effect).mean()),
        "ci": [float(low), float(high)],
        "pvalue": float(
            paired_sign_flip_pvalue(
                sae_effect,
                random_effect,
                seed=seed + 1,
            )
        ),
        "n_pairs": int(sae_effect.size),
    }


def _feature_set_stats(frame: pd.DataFrame, *, seed: int) -> dict:
    metric = _effect_column(frame)
    sae = (
        frame[frame["condition"] == "sae_feature_set"]
        .groupby("task_id", as_index=False)[metric]
        .first()
        .rename(columns={metric: "sae"})
    )
    random = (
        frame[frame["condition"] == "random_norm_matched"]
        .assign(
            _abs=lambda data: np.abs(
                pd.to_numeric(data[metric], errors="coerce")
            )
        )
        .groupby("task_id", as_index=False)["_abs"]
        .mean()
        .rename(columns={"_abs": "random"})
    )
    paired = sae.merge(random, on="task_id")
    sae_effect = np.abs(paired["sae"].to_numpy(float))
    random_effect = paired["random"].to_numpy(float)

    if sae_effect.size == 0:
        return {}

    low, high = paired_bootstrap_difference_ci(
        sae_effect,
        random_effect,
        seed=seed,
    )
    return {
        "sae_abs": float(sae_effect.mean()),
        "random_abs": float(random_effect.mean()),
        "ratio": float(
            sae_effect.mean() / max(float(random_effect.mean()), 1e-12)
        ),
        "advantage": float((sae_effect - random_effect).mean()),
        "ci": [float(low), float(high)],
        "pvalue": float(
            paired_sign_flip_pvalue(
                sae_effect,
                random_effect,
                seed=seed + 1,
            )
        ),
        "n_tasks": int(sae_effect.size),
    }


def _save_figure(fig: plt.Figure, path: Path) -> None:
    fig.tight_layout()
    fig.savefig(path, dpi=160)
    plt.close(fig)


def _save_plots(
    artifact_dir: Path,
    selected: pd.DataFrame,
    layers: pd.DataFrame,
    max_active: pd.DataFrame,
    feature_sets: pd.DataFrame | None,
    position: pd.DataFrame,
    study: pd.DataFrame,
) -> None:
    fig_dir = artifact_dir / "figures"
    fig_dir.mkdir(parents=True, exist_ok=True)

    fig = plt.figure(figsize=(7.5, 4.2))
    ax = fig.add_subplot(111)
    ordered = selected.sort_values("auroc")
    ax.barh(ordered["concept"], ordered["auroc"])
    ax.axvline(0.5, linewidth=1, linestyle="--")
    ax.set_xlabel("Held-out AUROC")
    ax.set_title("Selected SAE feature predictiveness")
    _save_figure(fig, fig_dir / "feature_auroc.png")

    fig = plt.figure(figsize=(7.0, 4.2))
    ax = fig.add_subplot(111)
    ax.plot(
        layers["layer"],
        layers["linear_probe_macro_auroc"],
        marker="o",
        label="Linear probe AUROC",
    )
    ax.plot(
        layers["layer"],
        layers["reconstruction_cosine"],
        marker="o",
        label="SAE reconstruction cosine",
    )
    ax.set_xlabel("Layer")
    ax.set_ylim(0, 1.05)
    ax.set_title("Layer-wise representation diagnostics")
    ax.legend()
    _save_figure(fig, fig_dir / "layer_diagnostics.png")

    metric = _effect_column(max_active)
    grouped = (
        max_active.groupby(["intervention", "condition"])[metric]
        .apply(lambda values: float(np.mean(np.abs(values))))
        .reset_index(name="mean_abs_effect")
    )
    pivot = grouped.pivot(
        index="intervention",
        columns="condition",
        values="mean_abs_effect",
    )
    fig = plt.figure(figsize=(7, 4.2))
    ax = fig.add_subplot(111)
    pivot.plot(kind="bar", ax=ax)
    ax.set_ylabel("Mean |Δ mean log p/token|")
    ax.set_title("Max-active SAE edits vs norm-matched controls")
    ax.tick_params(axis="x", rotation=0)
    _save_figure(fig, fig_dir / "causal_effects.png")

    if feature_sets is not None and not feature_sets.empty:
        feature_set_metric = _effect_column(feature_sets)
        grouped_sets = (
            feature_sets.groupby(["set_size", "condition"])[feature_set_metric]
            .apply(lambda values: float(np.mean(np.abs(values))))
            .reset_index(name="mean_abs_effect")
        )
        set_pivot = grouped_sets.pivot(
            index="set_size",
            columns="condition",
            values="mean_abs_effect",
        )
        fig = plt.figure(figsize=(7, 4.2))
        ax = fig.add_subplot(111)
        set_pivot.plot(kind="line", marker="o", ax=ax)
        ax.set_xlabel("Jointly ablated feature count")
        ax.set_ylabel("Mean |Δ mean log p/token|")
        ax.set_title("Final-token feature-set diagnostic")
        _save_figure(fig, fig_dir / "feature_set_effects.png")

    overall = position[position["concept"] == "__all__"].copy()
    policy_order = ["final_token", "max_feature_activation"]
    overall["position_policy"] = pd.Categorical(
        overall["position_policy"],
        categories=policy_order,
        ordered=True,
    )
    overall = overall.sort_values("position_policy")
    position_plot = pd.DataFrame(
        {
            "Policy": ["Final token", "Max feature activation"],
            "SAE effect": overall["target_sae_abs_mean"].to_numpy(float),
            "Random control": overall["target_random_abs_mean"].to_numpy(float),
        }
    )
    fig = plt.figure(figsize=(7.2, 4.4))
    ax = fig.add_subplot(111)
    x_positions = np.arange(len(position_plot))
    width = 0.34
    ax.bar(
        x_positions - width / 2,
        position_plot["SAE effect"],
        width,
        label="SAE effect",
    )
    ax.bar(
        x_positions + width / 2,
        position_plot["Random control"],
        width,
        label="Random control",
    )
    ax.set_xticks(x_positions, position_plot["Policy"])
    ax.set_ylabel("Task-level mean |Δ mean log p/token|")
    ax.set_title("Causal position sensitivity")
    ax.legend()
    _save_figure(fig, fig_dir / "causal_position_sensitivity.png")

    if not study.empty:
        fig = plt.figure(figsize=(7.2, 4.6))
        ax = fig.add_subplot(111)
        ax.scatter(
            study["heldout_auroc"],
            study["max_active_target_specificity_ratio"],
        )
        for row in study.itertuples():
            ax.annotate(
                str(row.concept),
                (row.heldout_auroc, row.max_active_target_specificity_ratio),
                fontsize=8,
            )
        ax.set_xlabel("Held-out feature AUROC")
        ax.set_ylabel("Max-active target specificity ratio")
        ax.set_title("Association evidence vs max-active causality")
        _save_figure(fig, fig_dir / "association_vs_causality.png")


def _coverage(
    frame: pd.DataFrame,
    column: str,
    fallback: str = "feature_activation",
) -> float:
    name = column if column in frame.columns else fallback
    values = pd.to_numeric(frame[name], errors="coerce").fillna(0)
    return float((values > 0).mean())


def _build_interpretation(
    *,
    max_stats: dict,
    final_coverage: float,
    max_coverage: float,
) -> str:
    strong = (
        max_stats["ratio"] >= 1.5
        and max_stats["ci"][0] > 0
        and max_stats["pvalue"] < 0.05
    )
    if strong:
        interpretation = (
            "Max-active interventions produced larger task-level target effects than "
            "norm-matched random controls with paired uncertainty excluding zero. "
            "Predictive SAE features therefore show causal specificity when intervened "
            "where the selected feature is actually represented, while the final-token "
            "baseline quantifies sensitivity to intervention location."
        )
    elif max_stats["ratio"] >= 1.5:
        interpretation = (
            "Max-active interventions had a larger point-estimate effect than norm-matched "
            "random controls, but task-level paired uncertainty did not support a strong "
            "significance claim. The result is therefore reported as suggestive causal "
            "specificity rather than conclusive evidence."
        )
    else:
        interpretation = (
            "Held-out feature predictiveness was strong, but max-active causal effects were "
            "only modest relative to norm-matched random controls. FeatureLens therefore "
            "separates predictive association from causal control rather than treating them "
            "as interchangeable."
        )

    if max_coverage > final_coverage + 0.1:
        interpretation += (
            " Moving from the final prompt token to the feature's maximum-activation token "
            f"increased intervention coverage from {final_coverage:.1%} to "
            f"{max_coverage:.1%}, showing that causal conclusions depend materially on "
            "where the representation is tested."
        )
    return interpretation


def _build_report_lines(
    *,
    headline: str,
    interpretation: str,
    highlights: list[str],
    feature_sets: pd.DataFrame | None,
    study: pd.DataFrame,
) -> list[str]:
    lines = [
        "# FeatureLens experiment report",
        "",
        "## Research question",
        "",
        "**Do sparse features that predict a concept also causally influence model behaviour?**",
        "",
        "## Executive summary",
        "",
        headline,
        "",
        interpretation,
        "",
        "## Key measurements",
        "",
        *[f"- {item}" for item in highlights],
        "",
        "## Experimental design",
        "",
        "- Model: Qwen3-1.7B-Base.",
        "- SAEs: Qwen-Scope residual-stream TopK SAEs at configured early/middle/late layers.",
        "- Discovery evidence: prompt-wide maximum SAE activation across non-padding tokens; final-token activations are saved separately.",
        "- Split discipline: paraphrase groups remain entirely in train or held-out test.",
        "- Feature selection: training-split AUROC plus activation contrast; held-out AUROC/F1 are reported separately.",
        "- Causal position policies: final prompt token and maximum selected-feature activation within the prompt. Max-active positions are selected from SAE activation only, never from behavioral outcomes.",
        "- Primary causal statistical unit: causal task. Ablation and 2× amplification are averaged within task before paired bootstrap/sign-flip inference.",
        "- Negative control: deterministic norm-matched random residual directions.",
        "- Primary target metric: exact full continuation mean log probability per token under teacher forcing.",
        "- Coverage and conditional-on-active effect strength are reported separately.",
        "- Feature-set analysis remains a final-token diagnostic and is not conflated with the max-active single-feature study.",
        "",
        "## Figures",
        "",
        "![Feature AUROC](figures/feature_auroc.png)",
        "",
        "![Layer diagnostics](figures/layer_diagnostics.png)",
        "",
        "![Causal position sensitivity](figures/causal_position_sensitivity.png)",
        "",
        "![Max-active causal effects](figures/causal_effects.png)",
    ]

    if feature_sets is not None and not feature_sets.empty:
        lines.extend(
            [
                "",
                "![Feature-set diagnostic](figures/feature_set_effects.png)",
            ]
        )

    if not study.empty:
        lines.extend(
            [
                "",
                "![Association vs causality](figures/association_vs_causality.png)",
                "",
                "## Position sensitivity",
                "",
                "The final-token policy asks whether the selected feature matters at the conventional last-prompt-token intervention site. The max-active policy asks whether it matters where that same feature is most strongly represented in the prompt. Reporting both prevents low final-token coverage from being mistaken for evidence that a predictive feature is globally non-causal.",
                "",
                "## Association vs causality across concepts",
                "",
                "Cross-concept correlations use max-active random-normalized specificity and are descriptive because the study has seven controlled concepts.",
            ]
        )

    lines.extend(
        [
            "",
            "## Interpretation guardrails",
            "",
            "High held-out AUROC is correlational evidence. Causal claims require downstream changes relative to norm-matched random controls. Max-active positions are chosen without reference to behavioral effect size. Task-level uncertainty treats ablation and amplification on the same causal prompt as repeated interventions, not independent experimental units.",
            "",
            "## Reproducibility",
            "",
            "Run `python -m experiments.run_all --resume` for a fresh full study. The causal-addendum notebook is retained as a migration utility for an already-completed final-token baseline.",
            "",
        ]
    )
    return lines


def main() -> None:
    args = parse_args()
    artifact_dir = args.artifact_dir

    catalog = pd.read_csv(artifact_dir / "feature_catalog.csv")
    layers = pd.read_csv(artifact_dir / "layer_metrics.csv")
    stability = pd.read_csv(artifact_dir / "stability.csv")
    final = pd.read_csv(_causal_file(artifact_dir, "final_token"))
    max_active = pd.read_csv(
        _causal_file(artifact_dir, "max_feature_activation")
    )

    feature_set_path = artifact_dir / "feature_set_results.csv"
    feature_sets = (
        pd.read_csv(feature_set_path) if feature_set_path.exists() else None
    )

    study_path = artifact_dir / "study_feature_summary.csv"
    study = pd.read_csv(study_path) if study_path.exists() else pd.DataFrame()
    position = pd.read_csv(artifact_dir / "causal_position_summary.csv")

    study_summary_path = artifact_dir / "study_summary.json"
    if study_summary_path.exists():
        study_summary = json.loads(study_summary_path.read_text())
    else:
        study_summary = {}

    selected = _selected_features(catalog)
    _save_plots(
        artifact_dir,
        selected,
        layers,
        max_active,
        feature_sets,
        position,
        study,
    )

    mean_auc = float(selected["auroc"].mean())
    median_auc = float(selected["auroc"].median())
    auc_low, auc_high = bootstrap_mean_ci(
        selected["auroc"].to_numpy(),
        seed=42,
    )

    best = layers.sort_values(
        "linear_probe_macro_auroc",
        ascending=False,
    ).iloc[0]
    mean_jaccard = float(stability["topk_jaccard"].mean())
    mean_cosine = float(stability["sparse_cosine"].mean())

    final_stats = _task_level_stats(final, seed=43)
    final_active_stats = _task_level_stats(
        final,
        seed=44,
        active_only=True,
    )
    max_stats = _task_level_stats(max_active, seed=45)
    max_active_stats = _task_level_stats(
        max_active,
        seed=46,
        active_only=True,
    )

    final_sae = final[final["condition"] == "sae_feature"]
    max_sae = max_active[max_active["condition"] == "sae_feature"]
    final_coverage = _coverage(
        final_sae,
        "feature_active_at_intervention",
    )
    anywhere_coverage = _coverage(
        max_sae,
        "feature_active_anywhere",
    )
    max_coverage = _coverage(
        max_sae,
        "feature_active_at_intervention",
    )

    set_summary: dict[int, dict] = {}
    if feature_sets is not None and not feature_sets.empty:
        sizes = sorted(int(value) for value in feature_sets["set_size"].unique())
        for size in sizes:
            subset = feature_sets[feature_sets["set_size"] == size]
            set_summary[size] = _feature_set_stats(
                subset,
                seed=100 + size,
            )

    interpretation = _build_interpretation(
        max_stats=max_stats,
        final_coverage=final_coverage,
        max_coverage=max_coverage,
    )

    headline = (
        f"Selected SAE features averaged {mean_auc:.3f} held-out AUROC. "
        f"Max-active interventions covered {max_coverage:.1%} of causal tasks and "
        f"changed mean log p/token by {max_stats['sae_abs']:.3f} in absolute value "
        f"on average versus {max_stats['random_abs']:.3f} for norm-matched random "
        f"controls ({max_stats['ratio']:.2f}×)."
    )

    highlights = [
        (
            f"Median selected-feature held-out AUROC: {median_auc:.3f}; mean AUROC "
            f"95% bootstrap CI [{auc_low:.3f}, {auc_high:.3f}]."
        ),
        (
            f"Best residual linear-probe layer: {int(best['layer'])} with macro AUROC "
            f"{float(best['linear_probe_macro_auroc']):.3f}."
        ),
        (
            f"Mean paraphrase TopK Jaccard: {mean_jaccard:.3f}; sparse activation "
            f"cosine: {mean_cosine:.3f}."
        ),
        (
            f"Feature coverage: final-token policy {final_coverage:.1%}; active "
            f"anywhere in prompt {anywhere_coverage:.1%}; max-active intervention "
            f"{max_coverage:.1%}."
        ),
        (
            f"Final-token task-level SAE/random ratio: {final_stats['ratio']:.2f}×; "
            f"paired advantage {final_stats['advantage']:+.4f}, 95% CI "
            f"[{final_stats['ci'][0]:+.4f}, {final_stats['ci'][1]:+.4f}], "
            f"sign-flip p={final_stats['pvalue']:.4f}."
        ),
        (
            f"Max-active task-level SAE/random ratio: {max_stats['ratio']:.2f}×; "
            f"paired advantage {max_stats['advantage']:+.4f}, 95% CI "
            f"[{max_stats['ci'][0]:+.4f}, {max_stats['ci'][1]:+.4f}], "
            f"sign-flip p={max_stats['pvalue']:.4f}."
        ),
        (
            "Conditional on feature-active tasks, max-active SAE/random ratio: "
            f"{max_active_stats['ratio']:.2f}× "
            f"(n={max_active_stats['n_tasks']})."
        ),
    ]

    if set_summary:
        max_size = max(set_summary)
        set_stats = set_summary[max_size]
        highlights.append(
            f"Final-token top-{max_size} joint ablation SAE/random ratio: "
            f"{set_stats['ratio']:.2f}×; paired advantage "
            f"{set_stats['advantage']:+.4f}, 95% CI "
            f"[{set_stats['ci'][0]:+.4f}, {set_stats['ci'][1]:+.4f}], "
            f"sign-flip p={set_stats['pvalue']:.4f}."
        )

    correlations = study_summary.get("correlations", {})
    target_corr = correlations.get(
        "heldout_auroc_vs_max_active_target_specificity",
        {},
    )
    js_corr = correlations.get(
        "heldout_auroc_vs_max_active_js_specificity",
        {},
    )
    if target_corr:
        target_rho = float(target_corr.get("rho", float("nan")))
        js_rho = float(js_corr.get("rho", float("nan")))
        highlights.extend(
            [
                (
                    "Across seven concepts, held-out AUROC vs max-active target "
                    f"specificity Spearman ρ={target_rho:+.3f}; descriptive only."
                ),
                (
                    "Held-out AUROC vs max-active JS specificity Spearman "
                    f"ρ={js_rho:+.3f}; descriptive only."
                ),
            ]
        )

    summary = {
        "headline": headline,
        "highlights": highlights,
        "interpretation": interpretation,
        "metrics": {
            "mean_selected_feature_test_auroc": mean_auc,
            "mean_selected_feature_test_auroc_bootstrap_ci_95": [
                auc_low,
                auc_high,
            ],
            "median_selected_feature_test_auroc": median_auc,
            "best_linear_probe_layer": int(best["layer"]),
            "best_linear_probe_macro_auroc": float(
                best["linear_probe_macro_auroc"]
            ),
            "mean_paraphrase_topk_jaccard": mean_jaccard,
            "mean_paraphrase_sparse_cosine": mean_cosine,
            "final_token_feature_coverage": final_coverage,
            "prompt_anywhere_feature_coverage": anywhere_coverage,
            "max_active_feature_coverage": max_coverage,
            "final_token_task_level": final_stats,
            "final_token_active_only": final_active_stats,
            "max_active_task_level": max_stats,
            "max_active_active_only": max_active_stats,
            "feature_set_results": {
                str(key): value for key, value in set_summary.items()
            },
            "study_summary": study_summary,
        },
    }
    (artifact_dir / "summary.json").write_text(
        json.dumps(summary, indent=2),
        encoding="utf-8",
    )

    report_lines = _build_report_lines(
        headline=headline,
        interpretation=interpretation,
        highlights=highlights,
        feature_sets=feature_sets,
        study=study,
    )
    report_path = artifact_dir / "report.md"
    report_path.write_text("\n".join(report_lines), encoding="utf-8")
    print(headline)
    print(f"Wrote {report_path}")


if __name__ == "__main__":
    main()