-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
1369 lines (1201 loc) · 44.8 KB
/
query.py
File metadata and controls
1369 lines (1201 loc) · 44.8 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
#!/usr/bin/env python3
"""
query.py — CLI query interface for the behavioral mechanisms knowledge base.
Usage:
python query.py --mechanism loss_aversion
python query.py --domain status_dominance
python query.py --domain status_dominance --filter "replication=strong"
python query.py --interaction amplifies --target status_threat_response
python query.py --interaction amplifies --source loss_aversion
python query.py --list # list all mechanisms
python query.py --list --domain ingroup_outgroup
python query.py --search "reference point" # full-text search in name/description
python query.py --stats # DB summary
# Prediction: score mechanisms against a profile + situation
python query.py --dim big_five_N:+ --dim bis_sensitivity:+ --feature stakes --feature social_visibility
python query.py --scenario "high-stakes public negotiation with a rival team"
python query.py --scenario "alone at night, unfamiliar city, low on money" --top 5
python query.py --dim dark_triad_narcissism:+ --export json
# Comparison: side-by-side two mechanisms
python query.py --compare shame_response guilt
python query.py --compare loss_aversion prospect_theory
"""
import argparse
import csv
import io
import json
import sqlite3
import sys
from pathlib import Path
ROOT = Path(__file__).parent
DB_PATH = ROOT / "db" / "mechanisms.sqlite"
# ─── DB connection ────────────────────────────────────────────────────────────
def get_conn() -> sqlite3.Connection:
if not DB_PATH.exists():
print(f"Database not found: {DB_PATH}", file=sys.stderr)
print("Run: python db_load.py", file=sys.stderr)
sys.exit(1)
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
conn.execute("PRAGMA query_only=ON")
return conn
# ─── Scoring (mirrors mcp_server._score_mechanisms) ──────────────────────────
STRENGTH_WEIGHT = {"strong": 1.5, "moderate": 1.0, "weak": 0.5}
SITUATION_MULTIPLIER = 0.5
def score_mechanisms(
conn: sqlite3.Connection, profile: dict, situation: list[str], top_n: int = 10
) -> list[dict]:
"""Score all mechanisms against a profile and situation. Returns sorted list."""
mechs = conn.execute(
"SELECT id, name, domain, description, summary, "
"behavioral_outputs, outputs, plain_language_outputs, narrative_outputs, "
"accuracy_score, effect_size, replication, replication_status "
"FROM mechanisms"
).fetchall()
situation_set = set(situation)
results = []
for mech in mechs:
mid = mech["id"]
person_score = 0.0
situation_score = 0.0
person_matches: list[dict] = []
situation_matches: list[dict] = []
excluded = False
pms = conn.execute(
"SELECT dimension, direction, strength, note FROM person_moderators WHERE mechanism_id=?",
(mid,),
).fetchall()
for pm in pms:
dim = pm["dimension"]
if dim not in profile:
continue
w = STRENGTH_WEIGHT.get(pm["strength"] or "moderate", 1.0)
user_dir = profile[dim]
mech_dir = pm["direction"]
if mech_dir == "mixed":
person_score += 0.25
person_matches.append({"dimension": dim, "direction": "mixed", "weight": 0.25})
elif user_dir == mech_dir:
person_score += w
person_matches.append(
{"dimension": dim, "direction": mech_dir, "weight": w, "effect": "amplifies"}
)
else:
person_score -= w * 0.5
person_matches.append(
{
"dimension": dim,
"direction": mech_dir,
"weight": -w * 0.5,
"effect": "dampens",
}
)
sas = conn.execute(
"SELECT feature, effect, note FROM situation_activators WHERE mechanism_id=?", (mid,)
).fetchall()
for sa in sas:
feat = sa["feature"]
effect = sa["effect"]
if effect == "required":
if feat in situation_set:
situation_score += 2.0
situation_matches.append(
{"feature": feat, "effect": "required+present", "weight": 2.0}
)
else:
excluded = True
break
elif feat in situation_set:
if effect == "activates":
situation_score += 2.0
situation_matches.append(
{"feature": feat, "effect": "activates", "weight": 2.0}
)
elif effect == "amplifies":
situation_score += 1.0
situation_matches.append(
{"feature": feat, "effect": "amplifies", "weight": 1.0}
)
elif effect == "dampens":
situation_score -= 1.0
situation_matches.append({"feature": feat, "effect": "dampens", "weight": -1.0})
if excluded:
continue
if person_score <= 0:
continue
total = person_score * (1 + situation_score * SITUATION_MULTIPLIER)
if total <= 0:
continue
plo = mech["plain_language_outputs"]
outputs = plo or mech["behavioral_outputs"] or mech["outputs"]
try:
outputs_parsed = json.loads(outputs) if outputs else None
except (json.JSONDecodeError, TypeError):
outputs_parsed = outputs
narr = mech["narrative_outputs"]
try:
narr_parsed = json.loads(narr) if narr else None
except (json.JSONDecodeError, TypeError):
narr_parsed = narr
results.append(
{
"id": mid,
"name": mech["name"],
"domain": mech["domain"],
"score": round(total, 2),
"person_score": round(person_score, 2),
"situation_score": round(situation_score, 2),
"person_matches": person_matches,
"situation_matches": situation_matches,
"outputs": outputs_parsed,
"narrative_outputs": narr_parsed,
"description": mech["description"] or mech["summary"],
"evidence": {
"effect_size": mech["effect_size"],
"replication": mech["replication"] or mech["replication_status"],
"accuracy_score": mech["accuracy_score"],
},
}
)
results.sort(key=lambda x: x["score"], reverse=True)
return results[:top_n]
# ─── Feature extraction from natural language ─────────────────────────────────
_FEATURE_KEYWORDS: dict[str, list[str]] = {
"stakes": [
"high stakes",
"important",
"risky",
"consequential",
"critical",
"matter",
"significant",
"big decision",
],
"social_visibility": [
"public",
"watched",
"audience",
"observed",
"visible",
"everyone",
"crowd",
"on display",
"in front of",
],
"time_pressure": [
"urgent",
"rushed",
"deadline",
"hurry",
"quickly",
"time pressure",
"fast",
"immediate",
"no time",
],
"ambiguity": [
"uncertain",
"unclear",
"ambiguous",
"confusing",
"vague",
"unpredictable",
"don't know",
"unknown",
"mixed signals",
"not sure",
"unsure",
],
"out_group_salience": [
"them",
"outgroup",
"enemy",
"competitor",
"rival",
"other group",
"us vs them",
"opposition",
"outsider",
],
"power_differential": [
"boss",
"authority",
"powerful",
"hierarchy",
"subordinate",
"unequal",
"superior",
"inferior",
],
"power_holder": [
"in charge",
"has authority",
"holds power",
"the boss",
"leader",
"in command",
"in control",
],
"power_low": [
"outranked",
"under authority",
"low status",
"subordinate",
"powerless",
"answering to",
"junior",
],
"resource_availability": [
"scarce",
"limited",
"not enough",
"shortage",
"lack",
"plenty",
"abundant",
"running out",
"constrained",
],
"novelty": [
"new",
"unfamiliar",
"novel",
"first time",
"never before",
"strange",
"unexpected",
"unusual",
"foreign",
],
"relationship_type": [
"friend",
"partner",
"colleague",
"stranger",
"family",
"relationship",
"close",
"intimate",
"acquaintance",
],
"anonymity": [
"anonymous",
"private",
"no one watching",
"secret",
"hidden",
"unknown",
"incognito",
"unidentified",
],
"conflict_present": [
"conflict",
"argument",
"disagreement",
"fight",
"dispute",
"confrontation",
"tension",
"clash",
"angry",
"upset",
"rude",
"mad",
"hostile",
"insult",
"yell",
"bully",
"harsh",
"offend",
"heated",
],
"group_context": [
"group",
"team",
"crowd",
"social",
"community",
"together",
"collective",
"meeting",
"committee",
],
"outcome_reversibility": [
"irreversible",
"permanent",
"can't undo",
"final",
"irrevocable",
"no going back",
"committed",
"locked in",
],
"physical_threat": [
"danger",
"threat",
"physical",
"pain",
"harm",
"violence",
"attack",
"unsafe",
"injury",
"fear",
],
"social_norms_clarity": [
"rules",
"norms",
"expectations",
"standards",
"appropriate",
"proper",
"protocol",
"etiquette",
],
"surveillance": [
"monitored",
"surveillance",
"being watched",
"tracked",
"evaluated",
"assessed",
"recorded",
],
"prior_commitment": [
"committed",
"promised",
"already decided",
"invested",
"obligation",
"pledge",
"agreed to",
"signed up",
],
}
def _text_to_features(text: str) -> list[str]:
"""Extract situation features from natural language text via keyword matching."""
text_lower = text.lower()
return [feat for feat, kws in _FEATURE_KEYWORDS.items() if any(kw in text_lower for kw in kws)]
# ─── Print prediction results ─────────────────────────────────────────────────
def print_predictions(results: list[dict], profile: dict, situation: list[str], export: str = None):
if export == "json":
print(json.dumps(results, indent=2))
return
if export == "csv":
buf = io.StringIO()
w = csv.writer(buf)
w.writerow(
["rank", "id", "name", "domain", "score", "person_score", "situation_score", "outputs"]
)
for i, r in enumerate(results, 1):
outs = r["outputs"]
if isinstance(outs, list):
outs = "; ".join(str(o) for o in outs)
w.writerow(
[
i,
r["id"],
r["name"],
r["domain"],
r["score"],
r["person_score"],
r["situation_score"],
outs or "",
]
)
print(buf.getvalue())
return
if profile:
print(f"\nProfile: {', '.join(f'{k}:{v}' for k, v in profile.items())}")
if situation:
print(f"Situation: {', '.join(situation)}")
print(f"\nTop {len(results)} mechanisms:\n")
for i, r in enumerate(results, 1):
outs = r["outputs"]
if isinstance(outs, list):
outs_str = ", ".join(str(o) for o in outs[:4])
if len(outs) > 4:
outs_str += f" +{len(outs) - 4}"
else:
outs_str = _truncate(str(outs or ""), 80)
evid = r["evidence"]
evid_parts = []
if evid.get("effect_size"):
evid_parts.append(f"effect={evid['effect_size']}")
if evid.get("replication"):
evid_parts.append(f"rep={evid['replication']}")
evid_str = f" [{', '.join(evid_parts)}]" if evid_parts else ""
print(
f" {i:2}. {r['id']:<38} score={r['score']:5.2f}"
f" (person={r['person_score']:+.1f}, sit={r['situation_score']:+.1f}){evid_str}"
)
print(f" {r['name']} [{r['domain']}]")
if outs_str:
print(f" → {outs_str}")
amp = [m["dimension"] for m in r["person_matches"] if m.get("effect") == "amplifies"]
dmp = [m["dimension"] for m in r["person_matches"] if m.get("effect") == "dampens"]
if amp:
print(f" ✓ person: {', '.join(amp)}")
if dmp:
print(f" ✗ person: {', '.join(dmp)}")
sit_pos = [
m["feature"] for m in r["situation_matches"] if "dampens" not in m.get("effect", "")
]
if sit_pos:
print(f" ✓ sit: {', '.join(sit_pos)}")
print()
# ─── Compare two mechanisms ────────────────────────────────────────────────────
def compare_mechanisms(conn: sqlite3.Connection, id_a: str, id_b: str):
def _get(mid: str):
row = conn.execute("SELECT * FROM mechanisms WHERE id=?", (mid,)).fetchone()
if row is None:
rows = conn.execute(
"SELECT * FROM mechanisms WHERE id LIKE ?", (f"%{mid}%",)
).fetchall()
if len(rows) == 1:
row = rows[0]
elif len(rows) > 1:
print(f"Multiple matches for '{mid}': {', '.join(r['id'] for r in rows)}")
return None
else:
print(f"Not found: {mid}")
return None
pms = conn.execute(
"SELECT dimension, direction, strength, note FROM person_moderators WHERE mechanism_id=?",
(row["id"],),
).fetchall()
sas = conn.execute(
"SELECT feature, effect, note FROM situation_activators WHERE mechanism_id=?",
(row["id"],),
).fetchall()
return row, list(pms), list(sas)
ra = _get(id_a)
rb = _get(id_b)
if ra is None or rb is None:
return
row_a, pms_a, sas_a = ra
row_b, pms_b, sas_b = rb
pm_a = {r["dimension"]: r for r in pms_a}
pm_b = {r["dimension"]: r for r in pms_b}
sa_a = {r["feature"]: r for r in sas_a}
sa_b = {r["feature"]: r for r in sas_b}
def evid_str(r):
parts = []
if r["effect_size"]:
parts.append(f"effect={r['effect_size']}")
rep = r["replication"] or r["replication_status"]
if rep:
parts.append(f"rep={rep}")
if r["accuracy_score"]:
parts.append(f"score={r['accuracy_score']:.2f}")
return ", ".join(parts) or "—"
def parse_outputs(r):
plo = r["plain_language_outputs"]
out = plo or r["behavioral_outputs"] or r["outputs"]
if not out:
return []
try:
o = json.loads(out)
if isinstance(o, list):
return [str(x) for x in o]
if isinstance(o, dict):
return [f"{k}: {v}" for k, v in list(o.items())[:5]]
except (json.JSONDecodeError, TypeError):
pass
return [str(out)[:80]]
print(f"\n{'═' * 70}")
print(" COMPARISON")
print(f" A: {row_a['name']} [{row_a['id']}]")
print(f" B: {row_b['name']} [{row_b['id']}]")
print(f"{'─' * 70}")
print(f"\n Domain A: {row_a['domain']}")
print(f" B: {row_b['domain']}")
print(f"\n Evidence A: {evid_str(row_a)}")
print(f" B: {evid_str(row_b)}")
# Shared person moderators
shared_dims = sorted(set(pm_a) & set(pm_b))
if shared_dims:
print(f"\n Shared person moderators ({len(shared_dims)}):")
for dim in shared_dims:
da = pm_a[dim]["direction"]
db = pm_b[dim]["direction"]
sa_ = (pm_a[dim]["strength"] or "mod")[:3]
sb_ = (pm_b[dim]["strength"] or "mod")[:3]
conflict = " ← CONFLICT" if (da != db and "mixed" not in (da, db)) else ""
same = " ← SAME" if da == db else ""
print(f" {dim:<35} A:{da}({sa_}) B:{db}({sb_}){conflict}{same}")
only_a_dims = sorted(set(pm_a) - set(pm_b))
only_b_dims = sorted(set(pm_b) - set(pm_a))
if only_a_dims:
print(f"\n Only A's moderators: {', '.join(only_a_dims)}")
if only_b_dims:
print(f"\n Only B's moderators: {', '.join(only_b_dims)}")
# Shared situation activators
shared_feats = sorted(set(sa_a) & set(sa_b))
if shared_feats:
print(f"\n Shared situation features ({len(shared_feats)}):")
for feat in shared_feats:
ea = sa_a[feat]["effect"]
eb = sa_b[feat]["effect"]
conflict = " ← CONFLICT" if ea != eb else " ← SAME"
print(f" {feat:<30} A:{ea} B:{eb}{conflict}")
only_a_feats = sorted(set(sa_a) - set(sa_b))
only_b_feats = sorted(set(sa_b) - set(sa_a))
if only_a_feats:
print(f"\n Only A's situation: {', '.join(only_a_feats)}")
if only_b_feats:
print(f"\n Only B's situation: {', '.join(only_b_feats)}")
# Outputs
oa = parse_outputs(row_a)
ob = parse_outputs(row_b)
print(f"\n Outputs A: {', '.join(oa[:5])}")
print(f" Outputs B: {', '.join(ob[:5])}")
print()
# ─── Formatters ───────────────────────────────────────────────────────────────
def fmt_json_field(val: str | None, label: str, indent: int = 2) -> str:
if val is None:
return ""
try:
obj = json.loads(val)
if isinstance(obj, list):
lines = [f"{' ' * indent}{label}:"]
for item in obj:
lines.append(f"{' ' * (indent + 1)}- {item}")
return "\n".join(lines)
elif isinstance(obj, dict):
lines = [f"{' ' * indent}{label}:"]
for k, v in obj.items():
lines.append(f"{' ' * (indent + 1)}{k}: {v}")
return "\n".join(lines)
except (json.JSONDecodeError, TypeError):
pass
return f"{' ' * indent}{label}: {val}"
def format_mechanism(
row: sqlite3.Row,
props: list[sqlite3.Row],
interactions: list[sqlite3.Row],
pms: list[sqlite3.Row] = None,
sas: list[sqlite3.Row] = None,
) -> str:
lines = []
lines.append(f"\n{'═' * 60}")
lines.append(f" {row['name']} [{row['id']}]")
lines.append(f" Domain: {row['domain'] or '—'}")
if row["accuracy_score"]:
lines.append(f" Accuracy: {row['accuracy_score']:.2f}")
lines.append(f"{'─' * 60}")
if row["description"] or row["summary"]:
desc = row["description"] or row["summary"]
lines.append(f"\n {desc}")
# Evidence
evid_parts = []
if row["effect_size"]:
evid_parts.append(f"effect_size={row['effect_size']}")
if row["replication"] or row["replication_status"]:
evid_parts.append(f"replication={row['replication'] or row['replication_status']}")
if row["cross_cultural"] or row["cross_cultural_status"]:
evid_parts.append(f"cross_cultural={row['cross_cultural'] or row['cross_cultural_status']}")
if evid_parts:
lines.append(f"\n Evidence: {', '.join(evid_parts)}")
# Triggers
t = fmt_json_field(row["triggers"], "triggers")
if t:
lines.append(f"\n{t}")
# Outputs (prefer plain_language_outputs if present)
plo = row["plain_language_outputs"]
out = plo or row["behavioral_outputs"] or row["outputs"]
label = "plain_language_outputs" if plo else "behavioral_outputs"
o = fmt_json_field(out, label)
if o:
lines.append(f"\n{o}")
# Individual variation
iv = row["individual_variation"] or row["variation"]
if iv:
lines.append(f"\n individual_variation: {_truncate(iv, 120)}")
# Person moderators
if pms:
lines.append("\n Person moderators:")
for pm in pms:
strength = f" ({pm['strength']})" if pm["strength"] else ""
note = f" — {_truncate(pm['note'] or '', 80)}" if pm["note"] else ""
lines.append(f" {pm['direction']:4} {pm['dimension']}{strength}{note}")
# Situation activators
if sas:
lines.append("\n Situation activators:")
for sa in sas:
note = f" — {_truncate(sa['note'] or '', 80)}" if sa["note"] else ""
lines.append(f" {sa['effect']:10} {sa['feature']}{note}")
# Properties (optional fields)
if props:
lines.append("\n Properties:")
for p in props:
val = _truncate(p["value"] or "", 100)
lines.append(f" {p['key']}: {val}")
# Interactions
if interactions:
lines.append("\n Interactions:")
for ix in interactions:
strength = f" ({ix['strength']})" if ix["strength"] else ""
notes = f" — {ix['notes']}" if ix["notes"] else ""
lines.append(f" {ix['relationship']:20} → {ix['mechanism_b']}{strength}{notes}")
if row["notes"]:
lines.append(f"\n Notes: {_truncate(row['notes'], 200)}")
return "\n".join(lines)
def _truncate(s: str, n: int) -> str:
if len(s) <= n:
return s
return s[:n] + "…"
# ─── Queries ──────────────────────────────────────────────────────────────────
def query_mechanism(conn: sqlite3.Connection, mid: str):
row = conn.execute("SELECT * FROM mechanisms WHERE id=?", (mid,)).fetchone()
if row is None:
rows = conn.execute("SELECT * FROM mechanisms WHERE id LIKE ?", (f"%{mid}%",)).fetchall()
if not rows:
print(f"Mechanism not found: {mid}")
return
if len(rows) > 1:
print(f"Multiple matches for '{mid}':")
for r in rows:
print(f" {r['id']} {r['name']}")
return
row = rows[0]
props = conn.execute(
"SELECT * FROM mechanism_properties WHERE mechanism_id=? ORDER BY key",
(row["id"],),
).fetchall()
interactions = conn.execute(
"SELECT * FROM interactions WHERE mechanism_a=? ORDER BY relationship, mechanism_b",
(row["id"],),
).fetchall()
pms = conn.execute(
"SELECT dimension, direction, strength, note FROM person_moderators "
"WHERE mechanism_id=? ORDER BY strength DESC, dimension",
(row["id"],),
).fetchall()
sas = conn.execute(
"SELECT feature, effect, note FROM situation_activators "
"WHERE mechanism_id=? ORDER BY effect, feature",
(row["id"],),
).fetchall()
print(format_mechanism(row, props, interactions, pms=pms, sas=sas))
def query_domain(conn: sqlite3.Connection, domain: str, filters: list[str] = None):
rows = conn.execute(
"SELECT * FROM mechanisms WHERE domain LIKE ? ORDER BY name",
(f"%{domain}%",),
).fetchall()
if not rows:
print(f"No mechanisms found for domain: {domain}")
return
if filters:
filtered = []
for row in rows:
match = True
for f in filters:
if "=" not in f:
continue
k, _, v = f.partition("=")
k = k.strip()
v = v.strip().lower()
row_val = (row[k] or "").lower() if k in row.keys() else ""
if v not in row_val:
match = False
break
if match:
filtered.append(row)
rows = filtered
print(f"\nDomain: {domain} ({len(rows)} mechanism(s))\n")
for row in rows:
evid = []
if row["effect_size"]:
evid.append(f"effect={row['effect_size']}")
if row["replication"] or row["replication_status"]:
evid.append(f"rep={row['replication'] or row['replication_status']}")
evid_str = f" [{', '.join(evid)}]" if evid else ""
print(f" {row['id']:<40} {row['name'][:40]}{evid_str}")
def query_interaction(
conn: sqlite3.Connection, relationship: str, source: str = None, target: str = None
):
where = ["relationship LIKE ?"]
params = [f"%{relationship}%"]
if source:
where.append("mechanism_a LIKE ?")
params.append(f"%{source}%")
if target:
where.append("mechanism_b LIKE ?")
params.append(f"%{target}%")
sql = f"SELECT * FROM interactions WHERE {' AND '.join(where)} ORDER BY mechanism_a"
rows = conn.execute(sql, params).fetchall()
if not rows:
print(
f"No interactions found for: relationship={relationship}"
+ (f", source={source}" if source else "")
+ (f", target={target}" if target else "")
)
return
print(f"\nInteractions ({relationship}) — {len(rows)} result(s)\n")
for row in rows:
strength = f" ({row['strength']})" if row["strength"] else ""
notes = f"\n {row['notes']}" if row["notes"] else ""
print(
f" {row['mechanism_a']:<35} {row['relationship']:20} → {row['mechanism_b']}{strength}{notes}"
)
def list_mechanisms(conn: sqlite3.Connection, domain: str = None):
if domain:
rows = conn.execute(
"SELECT * FROM mechanisms WHERE domain LIKE ? ORDER BY domain, name",
(f"%{domain}%",),
).fetchall()
else:
rows = conn.execute("SELECT * FROM mechanisms ORDER BY domain, name").fetchall()
current_domain = None
for row in rows:
if row["domain"] != current_domain:
current_domain = row["domain"]
print(f"\n── {current_domain or 'unknown'} ──")
evid = []
if row["effect_size"]:
evid.append(row["effect_size"])
if row["replication"] or row["replication_status"]:
evid.append(row["replication"] or row["replication_status"])
tag = f" [{', '.join(evid)}]" if evid else ""
print(f" {row['id']:<42} {row['name'][:50]}{tag}")
def search_mechanisms(conn: sqlite3.Connection, query: str):
q = f"%{query}%"
rows = conn.execute(
"""SELECT m.*, GROUP_CONCAT(p.key || '=' || p.value, '; ') as props
FROM mechanisms m
LEFT JOIN mechanism_properties p ON m.id = p.mechanism_id
WHERE m.name LIKE ? OR m.description LIKE ? OR m.summary LIKE ?
OR m.notes LIKE ? OR m.id LIKE ?
GROUP BY m.id
ORDER BY m.name""",
(q, q, q, q, q),
).fetchall()
if not rows:
print(f"No results for: '{query}'")
return
print(f"\nSearch results for '{query}' — {len(rows)} match(es)\n")
for row in rows:
desc = _truncate(row["description"] or row["summary"] or "", 120)
print(f" {row['id']}")
print(f" {row['name']} [{row['domain']}]")
if desc:
print(f" {desc}")
print()
def show_stats(conn: sqlite3.Connection):
n_mech = conn.execute("SELECT COUNT(*) FROM mechanisms").fetchone()[0]
n_props = conn.execute("SELECT COUNT(*) FROM mechanism_properties").fetchone()[0]
n_inter = conn.execute("SELECT COUNT(*) FROM interactions").fetchone()[0]
n_pm = conn.execute("SELECT COUNT(*) FROM person_moderators").fetchone()[0]
n_sa = conn.execute("SELECT COUNT(*) FROM situation_activators").fetchone()[0]
n_plo = conn.execute(
"SELECT COUNT(*) FROM mechanisms WHERE plain_language_outputs IS NOT NULL"
).fetchone()[0]
print(f"\nDatabase: {DB_PATH}\n")
print(f" Mechanisms: {n_mech}")
print(f" plain_language_outputs: {n_plo}/{n_mech}")
print(f" Properties: {n_props}")
print(f" Interactions: {n_inter}")
print(f" Person moderators: {n_pm}")
print(f" Situation activators: {n_sa}")
print("\nBy domain:")
rows = conn.execute(
"SELECT domain, COUNT(*) as n FROM mechanisms GROUP BY domain ORDER BY domain"
).fetchall()
for r in rows:
print(f" {r['domain'] or 'unknown':<45} {r['n']}")
print("\nRelationship types:")
rows = conn.execute(
"SELECT relationship, COUNT(*) as n FROM interactions GROUP BY relationship ORDER BY n DESC"
).fetchall()
for r in rows:
print(f" {r['relationship']:<30} {r['n']}")
print("\nTop optional fields:")
rows = conn.execute(
"SELECT key, COUNT(*) as n FROM mechanism_properties GROUP BY key ORDER BY n DESC LIMIT 15"
).fetchall()
for r in rows:
print(f" {r['key']:<30} {r['n']}")
print("\nDimension coverage (person_moderators):")
rows = conn.execute(
"SELECT dimension, COUNT(DISTINCT mechanism_id) as n FROM person_moderators "
"GROUP BY dimension ORDER BY n DESC LIMIT 15"
).fetchall()
for r in rows:
print(f" {r['dimension']:<35} {r['n']} mechanisms")
print("\nSituation feature coverage:")
rows = conn.execute(
"SELECT feature, effect, COUNT(DISTINCT mechanism_id) as n FROM situation_activators "
"GROUP BY feature, effect ORDER BY feature, n DESC"
).fetchall()
current_feat = None
for r in rows:
if r["feature"] != current_feat:
current_feat = r["feature"]
print(f" {r['feature']}")
print(f" {r['effect']:<15} {r['n']} mechanisms")
# ─── Rationalization verbalization ───────────────────────────────────────────
def verbalize_behavior(
conn: sqlite3.Connection,
hidden_id: str,
action: str,
profile: dict = None,
situation: list[str] = None,
framing: str = "first_person",
):
"""
Given a hidden mechanism and an action, generate surface rationalizations.
Prints results to stdout.
"""
# Look up hidden mechanism
row = conn.execute(
"SELECT id, name, domain, description, summary, plain_language_outputs "
"FROM mechanisms WHERE id=?",
(hidden_id,),
).fetchone()
if row is None:
print(f"Error: mechanism '{hidden_id}' not found.", file=sys.stderr)
return
plo_raw = row["plain_language_outputs"]
try:
hidden_plo = json.loads(plo_raw) if plo_raw else []
except (json.JSONDecodeError, TypeError):
hidden_plo = []
# description may be in mechanism_properties if not in top-level columns
desc = row["description"] or row["summary"] or ""
if not desc:
prop = conn.execute(
"SELECT value FROM mechanism_properties WHERE mechanism_id=? AND key='definition'",
(hidden_id,),
).fetchone()
if prop:
desc = prop["value"] or ""
hidden = {
"id": row["id"],
"name": row["name"],
"domain": row["domain"],
"description": desc,
"plain_language_outputs": hidden_plo,
}
# Score posthoc_rationalization mechanisms
profile = profile or {}
situation = situation or []
situation_set = set(situation)
rat_mechs = conn.execute(