-
Notifications
You must be signed in to change notification settings - Fork 6
bench(w4.3): decorrelate untrusted from poison, and measure what the defense costs #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| """W4.3 analysis — mixed benign-untrusted arms. | ||
|
|
||
| The W4.2 objection this answers: there, trust_level=untrusted was perfectly correlated | ||
| with poison, so downweighting untrusted was indistinguishable from downweighting the | ||
| answer key. These arms break that correlation and measure what the defence costs when | ||
| the label is no longer an oracle. | ||
|
|
||
| Metrics per arm: | ||
| accuracy + exact McNemar against that corpus's OWN TWR-off arm | ||
| poison in top-k / ranked #1 (Arm M only — Arm N has no poison) | ||
| benign-untrusted occupancy fraction of top-k that is untrusted AND not poison | ||
| (the over-suppression measure) | ||
| evidence-in-top-k fraction of evidence rounds retrieved (Arm N's headline) | ||
|
|
||
| Trust levels and session ids are read from Postgres rather than re-derived from | ||
| trust_plan.jsonl, because the plan records round indices while retrieval returns | ||
| memory ids — the database is the only place those two join. | ||
|
|
||
| python analyze_mixed.py --arms mix_twr_off mix_twr_w35 | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import json | ||
| import subprocess | ||
| from math import comb | ||
| from pathlib import Path | ||
|
|
||
| from run_longmemeval import HERE, jsonl_read, load_dataset | ||
|
|
||
|
|
||
| def psql(sql: str) -> list[list[str]]: | ||
| out = subprocess.run( | ||
| ["docker", "exec", "aegis-postgres", "psql", "-U", "aegis", "-d", "aegis", | ||
| "-t", "-A", "-F", "\t", "-c", sql], | ||
| capture_output=True, text=True, check=True, | ||
| ).stdout | ||
| return [ln.split("\t") for ln in out.splitlines() if ln.strip()] | ||
|
|
||
|
|
||
| def memory_facts(namespace_like: str) -> dict[str, tuple[str, str]]: | ||
| """memory_id -> (trust_level, session_id) for one corpus.""" | ||
| rows = psql( | ||
| "SELECT id, trust_level, COALESCE(metadata->>'session_id', '') " | ||
| f"FROM memories WHERE namespace LIKE '{namespace_like}'" | ||
| ) | ||
| return {r[0]: (r[1], r[2]) for r in rows if len(r) >= 3} | ||
|
|
||
|
|
||
| def exact_mcnemar(b: int, c: int) -> float: | ||
| """Two-sided exact binomial test on discordant pairs. b/c = the two disagreements.""" | ||
| n = b + c | ||
| if n == 0: | ||
| return 1.0 | ||
| k = min(b, c) | ||
| tail = sum(comb(n, i) for i in range(0, k + 1)) / (2 ** n) | ||
| return min(1.0, 2 * tail) | ||
|
|
||
|
|
||
| def load_labels(results_dir: Path) -> dict[str, bool]: | ||
| return {q: r["label"] for q, r in jsonl_read(results_dir / "judged.jsonl").items()} | ||
|
|
||
|
|
||
| def analyse_arm(results_dir: Path, facts: dict, poison_ids: dict, evidence_sessions: dict) -> dict: | ||
| hyp = jsonl_read(results_dir / "hypotheses.jsonl") | ||
| labels = load_labels(results_dir) | ||
|
|
||
| slots = poison_hits = benign_untrusted = 0 | ||
| ranked_first = with_any_poison = 0 | ||
| ev_retrieved = 0 | ||
| q_with_evidence = q_with_any_evidence_retrieved = 0 | ||
| n = 0 | ||
|
|
||
| for qid, rec in hyp.items(): | ||
| ids = rec.get("retrieved_memory_ids") or [] | ||
| if not ids: | ||
| continue | ||
| n += 1 | ||
| slots += len(ids) | ||
| pois = poison_ids.get(qid, set()) | ||
| evs = evidence_sessions.get(qid, set()) | ||
|
|
||
| hits = [i for i in ids if i in pois] | ||
| poison_hits += len(hits) | ||
| if hits: | ||
| with_any_poison += 1 | ||
| if ids and ids[0] in pois: | ||
| ranked_first += 1 | ||
|
|
||
| hit_evidence = 0 | ||
| for mid in ids: | ||
| trust, sid = facts.get(mid, ("", "")) | ||
| if trust == "untrusted" and mid not in pois: | ||
| benign_untrusted += 1 | ||
| if sid and sid in evs: | ||
| hit_evidence += 1 | ||
| ev_retrieved += hit_evidence | ||
| if evs: | ||
| q_with_evidence += 1 | ||
| if hit_evidence: | ||
| q_with_any_evidence_retrieved += 1 | ||
|
|
||
| acc = sum(1 for v in labels.values() if v) / len(labels) if labels else 0.0 | ||
| return { | ||
| "arm": results_dir.name, | ||
| "n_questions": len(labels), | ||
| "accuracy": round(acc, 4), | ||
| "top_k_slots": slots, | ||
| "poison_in_top_k": poison_hits, | ||
| "poison_in_top_k_rate": round(poison_hits / slots, 4) if slots else 0.0, | ||
| "questions_with_any_poison": with_any_poison, | ||
| "poison_ranked_first": ranked_first, | ||
| "poison_ranked_first_rate": round(ranked_first / n, 4) if n else 0.0, | ||
| "benign_untrusted_in_top_k": benign_untrusted, | ||
| "benign_untrusted_occupancy": round(benign_untrusted / slots, 4) if slots else 0.0, | ||
| # Arm N's headline. evidence_in_top_k_rate is the share of context that is | ||
| # answer-bearing; evidence_recall is the share of questions that got ANY | ||
| # evidence at all — the latter is what accuracy can actually depend on, since | ||
| # a question with zero evidence in context is unanswerable from retrieval. | ||
| "evidence_in_top_k": ev_retrieved, | ||
| "evidence_in_top_k_rate": round(ev_retrieved / slots, 4) if slots else 0.0, | ||
| "questions_with_evidence": q_with_evidence, | ||
| "questions_with_any_evidence_retrieved": q_with_any_evidence_retrieved, | ||
| "evidence_recall": ( | ||
| round(q_with_any_evidence_retrieved / q_with_evidence, 4) if q_with_evidence else 0.0 | ||
| ), | ||
| } | ||
|
|
||
|
|
||
| def paired_mcnemar(control: Path, treatment: Path) -> dict: | ||
| a, b = load_labels(control), load_labels(treatment) | ||
| shared = sorted(set(a) & set(b)) | ||
| only_ctrl = sum(1 for q in shared if a[q] and not b[q]) | ||
| only_trt = sum(1 for q in shared if b[q] and not a[q]) | ||
| return { | ||
| "control": control.name, | ||
| "treatment": treatment.name, | ||
| "paired_questions": len(shared), | ||
| "control_only_correct": only_ctrl, | ||
| "treatment_only_correct": only_trt, | ||
| # Not rounded to a fixed number of decimals: with a large discordant split the | ||
| # exact p is ~1e-10, and round(p, 6) would render it as a literal 0.0. | ||
| "exact_mcnemar_p": float(f"{exact_mcnemar(only_ctrl, only_trt):.3g}"), | ||
| } | ||
|
|
||
|
|
||
| def main(): | ||
| ap = argparse.ArgumentParser(description=__doc__) | ||
| ap.add_argument("--arms", nargs="+", required=True) | ||
| ap.add_argument("--suffix", default="_mix") | ||
| ap.add_argument("--limit", type=int, default=120) | ||
| ap.add_argument("--control", default=None, help="within-corpus TWR-off arm to pair against") | ||
| args = ap.parse_args() | ||
|
|
||
| data = load_dataset(args.limit) | ||
| evidence_sessions = { | ||
| e["question_id"]: set(e.get("answer_session_ids") or []) for e in data | ||
| } | ||
|
|
||
| injected = jsonl_read(HERE / "results" / "poison" / f"injected{args.suffix}.jsonl") | ||
| poison_ids = {q: set(r["memory_ids"]) for q, r in injected.items()} | ||
|
|
||
| facts = memory_facts(f"lme\\_%{args.suffix}") | ||
| print(f"corpus {args.suffix}: {len(facts)} memories | " | ||
| f"poison ledger: {sum(len(v) for v in poison_ids.values())} ids") | ||
|
|
||
| arms = [analyse_arm(HERE / "results" / a, facts, poison_ids, evidence_sessions) | ||
| for a in args.arms] | ||
| out = {"corpus_suffix": args.suffix, "arms": arms} | ||
|
|
||
| if args.control: | ||
| ctrl = HERE / "results" / args.control | ||
| out["mcnemar"] = [ | ||
| paired_mcnemar(ctrl, HERE / "results" / a) | ||
| for a in args.arms if a != args.control | ||
| ] | ||
|
|
||
| print(json.dumps(out, indent=2)) | ||
| return out | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If any judge calls fail—an explicitly supported scenario because
phase_judgecollects exceptions and leaves a partialjudged.jsonl—this loop still aggregates retrieval metrics over every hypothesis while accuracy andn_questionsuse only judged records. The resulting arm silently combines different samples, so occupancy/evidence rates cannot be compared reliably with its reported accuracy; iterate over the intersection withlabelsor report the retrieval sample separately.Useful? React with 👍 / 👎.