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b7
 
 
 
 
 
 
 
 
 
 
0bb4dfa
a763505
 
 
 
 
 
 
 
 
 
 
 
 
83add3c
 
cdef1a0
0bb4dfa
 
 
 
cdef1a0
83add3c
 
c44fab6
83add3c
 
cdef1a0
 
 
9a51d6a
 
0bb4dfa
cdef1a0
 
a763505
 
 
 
0bb4dfa
cdef1a0
a763505
83add3c
c44fab6
0bb4dfa
 
 
 
 
 
 
 
 
a763505
 
 
 
 
 
0bb4dfa
 
 
 
9a51d6a
 
0bb4dfa
 
 
83add3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11bf9b7
83add3c
 
a763505
0bb4dfa
 
 
83add3c
a763505
0bb4dfa
83add3c
 
0bb4dfa
 
 
 
 
83add3c
 
 
 
 
 
 
 
 
 
 
 
 
 
0bb4dfa
 
 
 
9a51d6a
 
0bb4dfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a763505
 
 
9a51d6a
 
 
 
 
 
a763505
 
 
 
 
0bb4dfa
 
 
 
 
 
a763505
 
1ec996f
 
0bb4dfa
 
a763505
1ec996f
0bb4dfa
 
 
1ec996f
 
 
 
0bb4dfa
 
 
 
9a51d6a
 
 
 
0bb4dfa
a763505
 
 
1ec996f
0bb4dfa
 
 
c44fab6
0bb4dfa
 
a763505
0bb4dfa
 
a763505
0bb4dfa
 
 
 
 
a763505
 
0bb4dfa
11bf9b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83add3c
 
 
11bf9b7
 
 
0bb4dfa
 
 
 
 
c44fab6
 
a763505
 
 
 
83add3c
 
cdef1a0
0bb4dfa
 
 
 
cdef1a0
83add3c
 
0bb4dfa
83add3c
 
cdef1a0
 
 
9a51d6a
 
0bb4dfa
cdef1a0
 
 
 
 
0bb4dfa
 
83add3c
0bb4dfa
 
 
 
 
 
 
 
 
 
 
a763505
83add3c
0bb4dfa
 
 
 
 
 
 
 
 
 
 
 
83add3c
 
 
0bb4dfa
83add3c
 
 
 
 
 
 
 
 
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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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
"""CCAI orchestrator: six-phase state machine driving a multi-participant
group discussion to a consensus (or to a documented failure-to-consense).

Phase outline (matches the build plan):

    1. Initial Opinions (independent, no peeking)
    1.5. Credential Summary (built concurrently during Phase 1)
    2. Critique x 2 rounds (full history visible)
    3. Status Assessment (max 3 iterations of targeted follow-ups)
    4. Opinion Finalization
    5. Consensus Gathering (alliance-aware, addressed-to aware)
    6. Closure (majority report, or unaddressed-factor probe + retry,
       or failure report)

Two failsafes pause the loop until the user clicks "Continue":
    - Participant-message cap: 60, then +20.
    - Orchestrator-call cap: 100, then +50.

Every LLM response runs through `app.utils.sanitize.strip_thinking` on
its way into history, into the orchestrator's prompts, and into the
summarizer.
"""
from __future__ import annotations

import asyncio
import json
import logging
import time
import uuid
from dataclasses import asdict
from typing import Any, AsyncIterator

from app.clients.llm_router import chat_completion
from app.config import settings
from app.services import context_budget, human_io
from app.services.consensus import (
    assess_consensus_status,
    classify_addressed_to,
    detect_alliances,
    find_unaddressed_factor,
)
from app.services.context_budget import (
    ContextSummary,
    DEFAULT_REPLY_BUDGET,
    KEEP_RECENT_MESSAGES,
    build_compressed_transcript_block,
    cap_max_tokens_for_window,
    context_window_for,
    estimate_messages_tokens,
    replace_embedded_transcript,
    run_summarize,
    select_summarizer_model_id,
    should_summarize,
)
from app.services.credential import (
    assemble_credential_summary_list,
    build_credential_for_participant,
    credentials_to_block,
)
from app.services.json_calls import orchestrator_call
from app.services.orchestrator_speed import (
    _AiTurnResult,
    _AiTurnSpec,
    compact_transcript_for_orchestrator,
    orchestrator_fast_model_id,
    run_initial_opinions_roster,
    run_roster_ai_turns_parallel,
)
from app.services.resilience import run_resilient_turn
from app.services.models import (
    DEFAULT_MAX_PARTICIPANTS,
    MAX_MAX_PARTICIPANTS,
    MIN_MAX_PARTICIPANTS,
    Participant,
    Phase,
    Session,
)
from app.services.prompts import (
    CONSENSUS_ALLIED_PROMPT,
    CONSENSUS_SOLO_PROMPT,
    CONTRIBUTION_SUMMARY_PROMPT,
    CRITIQUE_PROMPT,
    FINALIZATION_PROMPT,
    INITIAL_OPINION_PROMPT,
    MAJORITY_REPORT_PROMPT,
    NO_CONSENSUS_REPORT_PROMPT,
    NO_REASONING_DIRECTIVE,
    PARTICIPANT_BASE_DIRECTIVE,
    STATUS_ASSESSMENT_PROMPT,
    TARGETED_FOLLOWUP_PROMPT,
    TARGETED_FOLLOWUP_FROM_PARTICIPANT_PROMPT,
)
from app.utils.sanitize import strip_thinking

LOG = logging.getLogger(__name__)


# ---------------------------------------------------------------------------
# Session registry
# ---------------------------------------------------------------------------

_sessions: dict[str, Session] = {}


def get_session(sid: str) -> Session | None:
    return _sessions.get(sid)


def create_session() -> Session:
    s = Session()
    _sessions[s.session_id] = s
    return s


# ---------------------------------------------------------------------------
# SSE helpers
# ---------------------------------------------------------------------------

def _sse(event: str, data: dict[str, Any]) -> str:
    return f"event: {event}\ndata: {json.dumps(data)}\n\n"


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

def _active_participants(session: Session) -> list[Participant]:
    return [p for p in session.participants if p.enabled]


def _orchestrator_model_id(session: Session) -> str:
    return session.orchestrator_model_id or settings.orchestrator_model


def _summarizer_model_id(session: Session) -> str:
    return select_summarizer_model_id(
        session.summarizer_model_id,
        session.orchestrator_model_id,
    )


def _format_history(
    messages: list[dict[str, Any]],
    *,
    include_orchestrator: bool = True,
) -> str:
    lines: list[str] = []
    for m in messages:
        if m.get("role") == "orchestrator" and not include_orchestrator:
            continue
        speaker = m.get("speaker_name") or m.get("speaker_id") or "(anon)"
        if m.get("role") == "orchestrator":
            speaker = "Orchestrator"
        lines.append(f"{speaker}: {m.get('text', '')}")
    return "\n".join(lines)


def _participant_roster_string(
    speaker: Participant,
    participants: list[Participant],
) -> str:
    others = [p.name for p in participants if p.participant_id != speaker.participant_id]
    return ", ".join(others) if others else "(no other participants)"


# Per-prompt cap on how much of a long pending-thread message we quote back
# to the speaker. The full message is still in the transcript above; this
# block is a "what specifically is owed to you" reminder, not a re-render.
_PENDING_TRUNCATE_CHARS = 600


def _pending_addressed_for(
    session: Session,
    speaker: Participant,
) -> list[tuple[str, str, str]]:
    """Return (asker_id, asker_name, message_text) for participant messages
    that addressed `speaker` since `speaker`'s last own-message turn.

    Used to (1) inject an "Open threads directed at you" block into per-
    speaker prompt templates and (2) populate the `replying_to` field on
    the speaker's outgoing message so the frontend can render a
    "Replying to X, Y" pill above the bubble.
    """
    last_own_idx = -1
    for i, m in enumerate(session.messages):
        if (
            m.get("role") == "participant"
            and m.get("speaker_id") == speaker.participant_id
        ):
            last_own_idx = i

    pending: list[tuple[str, str, str]] = []
    for m in session.messages[last_own_idx + 1:]:
        if m.get("role") != "participant":
            continue
        if m.get("addressed_to") != speaker.participant_id:
            continue
        asker_id = m.get("speaker_id") or "unknown"
        asker_name = m.get("speaker_name") or "another participant"
        text = (m.get("text") or "").strip()
        if not text:
            continue
        pending.append((asker_id, asker_name, text))
    return pending


def _format_pending_block(
    pending: list[tuple[str, str, str]],
) -> str:
    """Render the open-threads section that gets interpolated into per-
    speaker prompt templates. Always non-empty so templates read naturally;
    we explicitly print "(none)" when there are no open threads.
    """
    if not pending:
        return (
            "Open threads directed at you since your last turn: (none).\n\n"
        )
    lines = ["Open threads directed at you since your last turn:"]
    for _asker_id, asker_name, text in pending:
        snippet = text
        if len(snippet) > _PENDING_TRUNCATE_CHARS:
            snippet = snippet[:_PENDING_TRUNCATE_CHARS].rstrip() + "..."
        lines.append(f'  - {asker_name} said to you: "{snippet}"')
    return "\n".join(lines) + "\n\n"


def _replying_to_ids(pending: list[tuple[str, str, str]]) -> list[str]:
    """Stable, de-duplicated list of asker participant_ids extracted from a
    pending-thread list. Used to populate the message's `replying_to`
    field so the frontend can render the "Replying to X, Y" pill.
    """
    seen: set[str] = set()
    out: list[str] = []
    for asker_id, _name, _text in pending:
        if asker_id in seen:
            continue
        seen.add(asker_id)
        out.append(asker_id)
    return out


# ---------------------------------------------------------------------------
# Failsafe checks
# ---------------------------------------------------------------------------

def _participant_msg_cap_hit(session: Session) -> bool:
    return session.total_participant_messages >= session.participant_message_cap


def _orchestrator_cap_hit(session: Session) -> bool:
    return session.orchestrator_call_count >= session.orchestrator_call_cap


def _bump_orchestrator_count(session: Session) -> None:
    session.orchestrator_call_count += 1


async def _wait_for_continue(
    session: Session,
    reason: str,
) -> AsyncIterator[str]:
    """Pause the state machine until the user clicks Continue.

    Increment values come from `session.limits`, which the user can
    tune via the settings menu. Defaults match the historical
    PARTICIPANT_MESSAGE_PAUSE_INC / ORCHESTRATOR_CALL_PAUSE_INC.
    """
    session.paused_for_continue = True
    session.pause_reason = reason
    if reason == "messages":
        bump_inc = session.limits.participant_message_pause_inc
        msg = (
            f"Conversation paused after {session.total_participant_messages} "
            "participant messages. Click Continue to allow another "
            f"{bump_inc} messages."
        )
        evt = "failsafe_pause"
    else:
        bump_inc = session.limits.orchestrator_call_pause_inc
        msg = (
            f"Conversation paused after {session.orchestrator_call_count} "
            "orchestrator calls. Click Continue to allow another "
            f"{bump_inc} orchestrator calls."
        )
        evt = "orchestrator_cap_pause"

    yield _sse(evt, {
        "reason": reason,
        "message": msg,
        "participant_messages": session.total_participant_messages,
        "orchestrator_calls": session.orchestrator_call_count,
    })

    # Block until pending_continue is flipped by the API layer.
    while session.paused_for_continue and not session.pending_continue:
        await asyncio.sleep(0.25)
    session.pending_continue = False
    session.paused_for_continue = False
    if reason == "messages":
        session.participant_message_cap += bump_inc
    else:
        session.orchestrator_call_cap += bump_inc
    session.pause_reason = None
    for chunk in _orchestrator_banner_sse(session, "Resuming conversation..."):
        yield chunk


# ---------------------------------------------------------------------------
# Human-participant turn
# ---------------------------------------------------------------------------

async def _wait_for_human_text(
    session: Session,
    participant: Participant,
    *,
    phase: Phase,
    addressed_to: str | None = None,
    asker_id: str | None = None,
    asker_name: str | None = None,
    prompt_context: str | None = None,
) -> AsyncIterator[str]:
    """Pause the orchestrator until the human types a response (or skips).

    Yields a `human_turn_needed` SSE event with the metadata the
    frontend needs to render the input slot and the lower-screen
    "waiting for your input" cue, then polls the human_io slot until
    the API layer's POST /human-response sets it, then yields a
    `human_turn_cleared` event so the frontend can dismiss the cue.

    The actual response text + skipped flag are NOT returned from this
    generator (async gens can't return values cleanly). The caller
    reads them via `human_io.slot_for(session.session_id)` AFTER the
    iteration completes:

        slot.response_text   (str)
        slot.skipped         (bool)
        slot.started_at      (float)  - subtract from now() for elapsed
        slot.pending_snapshot (list)  - pending threads at turn-start

    Caller is expected to reset_slot after consuming the result.
    """
    started = time.time()
    pending = _pending_addressed_for(session, participant)
    slot = human_io.slot_for(session.session_id)
    slot.event.clear()
    slot.response_text = ""
    slot.skipped = False
    slot.started_at = started
    slot.pending_snapshot = pending

    awaiting = {
        "speaker_id": participant.participant_id,
        "speaker_name": participant.name,
        "phase": phase.value,
        "addressed_to": addressed_to,
        "asker_id": asker_id,
        "asker_name": asker_name,
        "prompt_context": prompt_context,
    }
    session.awaiting_human = awaiting
    session.paused_for_continue = True
    session.pause_reason = "human_turn"

    yield _sse("human_turn_needed", awaiting)

    try:
        # Poll with the same 0.25s cadence as _wait_for_continue so
        # SSE-stream cancellation propagates promptly to the user
        # clicking Stop.
        while not slot.event.is_set():
            await asyncio.sleep(0.25)
    finally:
        session.paused_for_continue = False
        session.pause_reason = None
        session.awaiting_human = None

    yield _sse("human_turn_cleared", {
        "speaker_id": participant.participant_id,
    })


async def _do_human_turn(
    session: Session,
    participant: Participant,
    *,
    phase: Phase,
    actives: list[Participant],
    addressed_to_target: str | None = None,
    asker_id: str | None = None,
    asker_name: str | None = None,
    prompt_context: str | None = None,
    classify_addressed: bool = False,
    track_initial_opinion: bool = False,
    track_final_opinion: bool = False,
    addressed_state: dict[str, Any] | None = None,
) -> AsyncIterator[str]:
    """End-to-end human turn: emit human_turn_needed, await response,
    emit human_turn_cleared, then either record a skip note or append a
    participant message (with addressed-to classification when asked).
    Yields SSE chunks throughout, then runs the failsafe-pause check.

    `addressed_state`, when provided, is a caller-owned dict that gets
    mutated with {"last_addressed": <participant_id|None>} after the
    turn so the consensus phase can update its routing variable
    without a return value sneaking out of the generator.
    """
    async for chunk in _wait_for_human_text(
        session, participant, phase=phase,
        addressed_to=addressed_to_target,
        asker_id=asker_id, asker_name=asker_name,
        prompt_context=prompt_context,
    ):
        yield chunk

    slot = human_io.slot_for(session.session_id)
    text = (slot.response_text or "").strip()
    skipped = slot.skipped
    elapsed = max(0.0, time.time() - slot.started_at)
    pending = list(slot.pending_snapshot or [])
    human_io.reset_slot(session.session_id)

    if skipped or not text:
        note = _add_orchestrator_message(
            session,
            f"{participant.name} declined to comment this turn.",
            kind="status",
        )
        yield _sse("orchestrator", _msg_payload(note))
        if addressed_state is not None:
            addressed_state["last_addressed"] = None
        return

    addressed: str | None = None
    if classify_addressed:
        addressed = await classify_addressed_to(
            orchestrator_model_id=orchestrator_fast_model_id(session),
            participants=actives,
            speaker_name=participant.name,
            message=text,
            api_log=session.api_log,
        )
        _bump_orchestrator_count(session)

    msg = _add_participant_message(
        session, participant, text,
        phase=phase, elapsed=elapsed,
        addressed_to=addressed,
        replying_to=_replying_to_ids(pending),
    )
    if track_initial_opinion:
        session.initial_opinions[participant.participant_id] = text
    if track_final_opinion:
        session.final_opinions[participant.participant_id] = text
    if addressed_state is not None:
        addressed_state["last_addressed"] = addressed

    yield _sse("message", _msg_payload(msg))

    if _participant_msg_cap_hit(session):
        async for chunk in _wait_for_continue(session, "messages"):
            yield chunk
    if _orchestrator_cap_hit(session):
        async for chunk in _wait_for_continue(session, "orchestrator"):
            yield chunk


# ---------------------------------------------------------------------------
# Participant turn (with context budgeting + summarize-on-demand)
# ---------------------------------------------------------------------------

async def _maybe_summarize_for_participant(
    session: Session,
    participant: Participant,
    api_messages: list[dict[str, Any]],
) -> None:
    """If this participant's input estimate exceeds the threshold, run a
    summarize call against the configured summarizer model and update
    `participant.summary` in place."""
    needs_sum, _trim, _budget = should_summarize(
        participant.model_id, api_messages, participant.summary,
    )
    if not needs_sum:
        return

    # Build a transcript that excludes orchestrator status banners (those
    # don't add information value to a summary) but keeps everything the
    # participant has said and heard.
    summarizable_msgs = [
        m for m in session.messages
        if m.get("role") != "orchestrator_status"
    ]
    if not summarizable_msgs:
        return

    transcript = _format_history(summarizable_msgs, include_orchestrator=False)
    if not transcript.strip():
        return

    summarizer_id = _summarizer_model_id(session)
    summary_text = await run_summarize(summarizer_id, transcript)
    # The summarizer counts as an orchestrator-side call for cap purposes.
    session.orchestrator_call_count += 1
    if summary_text:
        participant.summary.summary_text = summary_text
        participant.summary.summarized_through_idx = len(session.messages) - 1


async def _call_participant(
    *,
    session: Session,
    participant: Participant,
    user_prompt: str,
    label: str,
    max_tokens: int = 600,
    timeout: float = 45.0,
    stream_events: list[str] | None = None,
    stream_message_id: str | None = None,
) -> tuple[str, float, bool, str]:
    """Run one participant turn.

    Returns ``(text, elapsed_seconds, ok, error_kind)``.

    ``error_kind`` is ``""`` on success. On failure it's one of:
      * ``"transient"`` — HTTP 5xx, 429, timeout, connection error. The
        same model is worth retrying.
      * ``"permanent"`` — auth, invalid request, content filter, model
        gone. Retrying the same model won't help.
      * ``"empty"`` — call returned a 200 with an empty body. Treated
        as transient by the resilience layer (retry once before
        substituting).
      * ``"unknown"`` — orchestrator-side exception we couldn't
        classify.

    The state-machine handles auto-disable on repeated failure; the
    resilience layer (`services.resilience.run_resilient_turn`) handles
    in-turn retry / alternate / substitution under speed-priority.
    """
    others = _participant_roster_string(participant, _active_participants(session))
    base_directive = PARTICIPANT_BASE_DIRECTIVE.format(
        n_participants=len(_active_participants(session)),
        other_participants=others,
    )
    system_text = (
        f"{participant.role_prompt}\n\n{base_directive}\n\n{NO_REASONING_DIRECTIVE}"
    )
    api_messages: list[dict[str, Any]] = [
        {"role": "system", "content": system_text},
        {"role": "user", "content": user_prompt},
    ]

    await _maybe_summarize_for_participant(session, participant, api_messages)

    needs_sum, needs_trim, input_budget = should_summarize(
        participant.model_id, api_messages, participant.summary,
    )

    # CCAI embeds the transcript inside the user prompt. When over budget,
    # swap that block for summary + recent tail (AskJerry pattern).
    if needs_sum or needs_trim:
        recent_transcript = _format_history(
            session.messages[-KEEP_RECENT_MESSAGES:],
            include_orchestrator=False,
        )
        compressed_block = build_compressed_transcript_block(
            participant.summary,
            recent_transcript,
        )
        if compressed_block:
            new_prompt = replace_embedded_transcript(user_prompt, compressed_block)
            user_prompt = new_prompt
            api_messages[1] = {"role": "user", "content": user_prompt}
        elif len(user_prompt) > input_budget * 4:
            # Last-resort: keep prompt head + tail if no transcript header matched.
            keep = max(512, input_budget * 2)
            user_prompt = (
                user_prompt[:keep]
                + "\n\n[…middle truncated for context…]\n\n"
                + user_prompt[-keep:]
            )
            api_messages[1] = {"role": "user", "content": user_prompt}

    max_tokens = cap_max_tokens_for_window(
        participant.model_id, api_messages, max_tokens,
    )

    resolved = {
        "model_id": participant.model_id,
        "base_url": participant.base_url,
        "api_key": participant.api_key,
        "is_neon": participant.is_neon,
        "hana_model_id": participant.hana_model_id,
        "persona_name": participant.persona_name,
        "neon_direct_vllm": participant.neon_direct_vllm,
        "vllm_base_url": participant.vllm_base_url,
        "vllm_api_key": participant.vllm_api_key,
    }

    log_entry: dict[str, Any] = {
        "timestamp": time.time(),
        "label": f"participant:{participant.participant_id}:{label}",
        "model": participant.model_id,
        "request": {"messages": api_messages, "max_tokens": max_tokens},
    }

    msg_id = stream_message_id or str(uuid.uuid4())
    on_text_delta_cb = None
    if stream_events is not None:
        stream_events.append(_sse("message_stream_start", {
            "message_id": msg_id,
            "speaker_id": participant.participant_id,
            "speaker_name": participant.name,
            "kind": participant.kind,
            "phase": session.phase.value,
            "model_id": participant.model_id,
            "model_display": participant.display_name,
        }))

        def on_text_delta_cb(piece: str) -> None:
            stream_events.append(_sse("message_delta", {
                "message_id": msg_id,
                "delta": piece,
            }))

    try:
        result = await chat_completion(
            resolved=resolved,
            messages=api_messages,
            temperature=0.7,
            max_tokens=max_tokens,
            timeout=timeout,
            on_text_delta=on_text_delta_cb,
        )
    except Exception as exc:
        LOG.exception("Participant %s call failed: %s", participant.participant_id, exc)
        log_entry["response"] = {"error": str(exc)}
        session.api_log.append(log_entry)
        participant.consecutive_failures += 1
        return "", 0.0, False, "unknown"

    log_entry["response"] = result
    session.api_log.append(log_entry)

    if result.get("error"):
        participant.consecutive_failures += 1
        return (
            "",
            result.get("elapsed_seconds", 0),
            False,
            result.get("error_kind") or "permanent",
        )

    text = strip_thinking(result.get("response", ""))
    elapsed = float(result.get("elapsed_seconds", 0) or 0)
    if not text.strip():
        # 200 OK but the model returned nothing usable. Worth one
        # retry / substitute attempt before we surface participant_error.
        participant.consecutive_failures += 1
        return "", elapsed, False, "empty"

    participant.consecutive_failures = 0
    return text, elapsed, True, ""


def _add_participant_message(
    session: Session,
    participant: Participant,
    text: str,
    *,
    phase: Phase,
    elapsed: float,
    addressed_to: str | None = None,
    replying_to: list[str] | None = None,
    message_id: str | None = None,
) -> dict[str, Any]:
    msg = {
        "message_id": message_id or str(uuid.uuid4()),
        "speaker_id": participant.participant_id,
        "speaker_name": participant.name,
        "role": "participant",
        # `kind` lets the frontend distinguish a human participant's
        # message ("human") from LLM messages ("neon" | "extra" |
        # "expert") so the green left-edge accent can be applied
        # independently of the rotating color palette.
        "kind": participant.kind,
        "text": text,
        "phase": phase.value,
        "timestamp": time.time(),
        "elapsed_seconds": round(elapsed, 2),
        "addressed_to": addressed_to,
        # `replying_to` mirrors the pending-thread list we showed the
        # speaker at turn-start: ordered, de-duplicated participant_ids of
        # everyone whose questions this turn was supposed to address.
        # Empty list when there were no open threads. The frontend renders
        # this as a "Replying to X, Y" pill above the bubble.
        "replying_to": list(replying_to) if replying_to else [],
        "model_id": participant.model_id,
        "model_display": participant.display_name,
    }
    session.messages.append(msg)
    session.total_participant_messages += 1
    return msg


def _add_orchestrator_message(
    session: Session,
    text: str,
    *,
    kind: str,
    extra: dict[str, Any] | None = None,
) -> dict[str, Any]:
    msg = {
        "speaker_id": "orchestrator",
        "speaker_name": "Orchestrator",
        "role": "orchestrator",
        "kind": kind,  # "status" | "factor" | "majority_report" | "no_consensus_report"
        "text": text,
        "phase": session.phase.value,
        "timestamp": time.time(),
    }
    if extra:
        msg.update(extra)
    session.messages.append(msg)
    return msg


def _msg_payload(msg: dict[str, Any]) -> dict[str, Any]:
    """Public payload for a message event over SSE."""
    return msg


def _orchestrator_banner_sse(
    session: Session,
    text: str,
    *,
    kind: str = "status",
    extra: dict[str, Any] | None = None,
) -> list[str]:
    """Append an orchestrator line to the transcript and emit chat + status SSE."""
    msg = _add_orchestrator_message(session, text, kind=kind, extra=extra)
    return [
        _sse("orchestrator", _msg_payload(msg)),
        _sse("status", {"message": text}),
    ]


def _participant_turn_failure_sse(
    session: Session,
    participant: Participant,
) -> list[str]:
    """Emit participant_error and auto-disable banner when threshold hit."""
    out = [
        _sse("participant_error", {
            "participant_id": participant.participant_id,
            "name": participant.name,
            "phase": session.phase.value,
        }),
    ]
    if participant.consecutive_failures >= session.limits.auto_disable_failures:
        participant.enabled = False
        out.extend(_orchestrator_banner_sse(
            session,
            f"{participant.name} auto-disabled after "
            f"{session.limits.auto_disable_failures} consecutive failures.",
        ))
    return out


# ---------------------------------------------------------------------------
# Credential summary (concurrent with Phase 1)
# ---------------------------------------------------------------------------

async def _credential_build_runner(
    session: Session,
    participant: Participant,
    initial_opinion: str,
) -> None:
    """Background task: one participant's credential row."""
    try:
        cred = await build_credential_for_participant(
            orchestrator_model_id=_orchestrator_model_id(session),
            question=session.question,
            participant=participant,
            initial_opinion=initial_opinion,
            api_log=session.api_log,
        )
        session.credential_entries_by_pid[participant.participant_id] = cred
        session.credential_model_by_pid[participant.participant_id] = (
            participant.model_id
        )
        _bump_orchestrator_count(session)
    except Exception as exc:
        LOG.exception(
            "Credential build failed for %s: %s",
            participant.participant_id,
            exc,
        )


def _schedule_phase1_credential_build(
    session: Session,
    participant: Participant,
    initial_opinion: str,
) -> None:
    """Start (or restart) a background credential build for one AI participant."""
    if participant.kind == "human":
        return
    if not (initial_opinion or "").strip():
        return

    pid = participant.participant_id
    existing = session.credential_build_tasks.get(pid)
    if existing is not None and not existing.done():
        existing.cancel()

    session.credential_build_tasks[pid] = asyncio.create_task(
        _credential_build_runner(session, participant, initial_opinion),
        name=f"credential:{pid}",
    )


def _sync_credential_summary_from_entries(session: Session) -> None:
    session.credential_summary = assemble_credential_summary_list(
        participants=_active_participants(session),
        credential_entries_by_pid=session.credential_entries_by_pid,
        human_credential=session.human_credential,
    )


async def _await_phase1_credential_tasks(session: Session) -> None:
    """Wait for any in-flight per-participant credential builds."""
    tasks = [
        t for t in session.credential_build_tasks.values()
        if t is not None and not t.done()
    ]
    if tasks:
        await asyncio.gather(*tasks, return_exceptions=True)
    _sync_credential_summary_from_entries(session)


async def _rebuild_participant_credential_on_model_change(
    session: Session,
    participant: Participant,
) -> bool:
    """Rebuild one credential row when the backing LLM model changes."""
    if participant.kind == "human":
        return False
    pid = participant.participant_id
    opinion = (session.initial_opinions or {}).get(pid, "")
    if not opinion.strip():
        return False
    prior_model = session.credential_model_by_pid.get(pid)
    if not prior_model or prior_model == participant.model_id:
        return False

    cred = await build_credential_for_participant(
        orchestrator_model_id=_orchestrator_model_id(session),
        question=session.question,
        participant=participant,
        initial_opinion=opinion,
        api_log=session.api_log,
    )
    _bump_orchestrator_count(session)
    session.credential_entries_by_pid[pid] = cred
    session.credential_model_by_pid[pid] = participant.model_id
    _sync_credential_summary_from_entries(session)
    return True


# ---------------------------------------------------------------------------
# Phase implementations
# ---------------------------------------------------------------------------

async def _phase_initial_opinions(session: Session) -> AsyncIterator[str]:
    session.phase = Phase.INITIAL_OPINIONS
    for chunk in _orchestrator_banner_sse(
        session, "Phase 1: collecting independent first opinions...",
    ):
        yield chunk

    actives = _active_participants(session)

    async def _human_initial(p: Participant) -> AsyncIterator[str]:
        async for chunk in _do_human_turn(
            session, p, phase=session.phase, actives=actives,
            track_initial_opinion=True,
            prompt_context=(
                "Share your initial opinion on the question. "
                "You're speaking BEFORE seeing the other participants."
            ),
        ):
            yield chunk

    async def _post_initial(result: _AiTurnResult) -> dict[str, Any]:
        speaker = result.turn.speaker
        session.initial_opinions[speaker.participant_id] = result.turn.text
        _schedule_phase1_credential_build(
            session, speaker, result.turn.text,
        )
        return {}

    def _build_initial_spec(p: Participant) -> _AiTurnSpec | None:
        if p.kind == "human":
            return None
        return _AiTurnSpec(
            participant=p,
            user_prompt=INITIAL_OPINION_PROMPT.format(question=session.question),
            label="initial_opinion",
            max_tokens=700,
        )

    async for chunk in run_initial_opinions_roster(
        session,
        actives,
        build_spec=_build_initial_spec,
        call_participant=_call_participant,
        on_human_turn=_human_initial,
        post_process=_post_initial,
    ):
        yield chunk

    # Credential rows were built in parallel as each opinion landed;
    # only wait here if any background task is still finishing.
    await _await_phase1_credential_tasks(session)
    yield _sse("credentials_updated", {
        "stage": "built",
        "credentials": session.credential_summary,
    })


_CRITIQUE_PHASES = {
    1: Phase.CRITIQUE_ROUND_1,
    2: Phase.CRITIQUE_ROUND_2,
    3: Phase.CRITIQUE_ROUND_3,
    4: Phase.CRITIQUE_ROUND_4,
}


def _critique_phase_for(round_number: int) -> Phase:
    """Map a critique round number to the matching Phase enum value.
    Falls back to CRITIQUE_ROUND_2 for unknown numbers - the API layer
    clamps `critique_rounds` to the bounds, so this fallback is purely
    defensive."""
    return _CRITIQUE_PHASES.get(round_number, Phase.CRITIQUE_ROUND_2)


async def _phase_critique(session: Session, round_number: int) -> AsyncIterator[str]:
    session.phase = _critique_phase_for(round_number)
    round_total = session.limits.critique_rounds
    for chunk in _orchestrator_banner_sse(
        session,
        f"Phase 2: critique round {round_number} of {round_total}...",
    ):
        yield chunk
    cred_block = credentials_to_block(session.credential_summary)
    actives = _active_participants(session)
    # Freeze transcript + pending threads at round start so parallel
    # turns see the same context and reply metadata stays consistent.
    transcript_snapshot = _format_history(session.messages)
    pending_snapshot = {
        p.participant_id: _pending_addressed_for(session, p)
        for p in actives
        if p.kind != "human"
    }

    async def _human_critique(p: Participant) -> AsyncIterator[str]:
        async for chunk in _do_human_turn(
            session, p, phase=session.phase, actives=actives,
            classify_addressed=True,
            prompt_context=(
                f"Critique round {round_number} of {round_total}. "
                "Push back on, agree with, or build on what others "
                "have said. Address other participants by name."
            ),
        ):
            yield chunk

    def _build_critique_spec(p: Participant) -> _AiTurnSpec | None:
        if p.kind == "human":
            return None
        pending = pending_snapshot.get(p.participant_id, [])
        pending_block = _format_pending_block(pending)
        prompt = CRITIQUE_PROMPT.format(
            round_number=round_number,
            round_total=round_total,
            question=session.question,
            credential_summary=cred_block,
            transcript=transcript_snapshot,
            pending_block=pending_block,
        )
        return _AiTurnSpec(
            participant=p,
            user_prompt=prompt,
            label=f"critique_round_{round_number}",
            max_tokens=700,
        )

    async def _post_critique(result: _AiTurnResult) -> dict[str, Any]:
        speaker = result.turn.speaker
        addressed = await classify_addressed_to(
            orchestrator_model_id=orchestrator_fast_model_id(session),
            participants=_active_participants(session),
            speaker_name=speaker.name,
            message=result.turn.text,
            api_log=session.api_log,
        )
        _bump_orchestrator_count(session)
        return {
            "addressed_to": addressed,
            "replying_to": _replying_to_ids(result.pending),
        }

    async for chunk in run_roster_ai_turns_parallel(
        session,
        actives,
        phase=session.phase,
        build_spec=_build_critique_spec,
        call_participant=_call_participant,
        on_human_turn=_human_critique,
        post_process=_post_critique,
    ):
        yield chunk


async def _phase_status_assessment(session: Session) -> AsyncIterator[str]:
    session.phase = Phase.STATUS_ASSESSMENT
    for chunk in _orchestrator_banner_sse(
        session, "Phase 3: assessing whether more questions are needed...",
    ):
        yield chunk

    cred_block = credentials_to_block(session.credential_summary)

    for iteration in range(session.limits.status_assessment_max):
        session.status_assessment_iterations = iteration + 1
        transcript = await compact_transcript_for_orchestrator(
            session,
            orchestrator_model_id=orchestrator_fast_model_id(session),
        )
        prompt = STATUS_ASSESSMENT_PROMPT.format(
            question=session.question,
            credential_summary=cred_block,
            transcript=transcript,
        )
        _raw, parsed = await orchestrator_call(
            orchestrator_model_id=_orchestrator_model_id(session),
            user_prompt=prompt,
            label=f"status_assessment_{iteration + 1}",
            api_log=session.api_log,
            max_tokens=512,
        )
        _bump_orchestrator_count(session)

        opinions_solidified = bool(
            isinstance(parsed, dict) and parsed.get("opinions_solidified")
        )
        open_qs: list[dict[str, Any]] = []
        if isinstance(parsed, dict):
            open_qs = parsed.get("open_questions") or []

        if opinions_solidified or not open_qs:
            msg = _add_orchestrator_message(
                session,
                "Opinions appear solidified - moving to finalization.",
                kind="status",
            )
            yield _sse("orchestrator", _msg_payload(msg))
            return

        # Otherwise run targeted follow-ups
        active_ids = {p.participant_id for p in _active_participants(session)}
        for oq in open_qs:
            pid = oq.get("participant_id")
            question_text = (oq.get("question") or "").strip()
            if not pid or pid not in active_ids or not question_text:
                continue
            target = next(p for p in session.participants if p.participant_id == pid)

            # Decide synthesized vs verbatim. Source of truth is
            # asker_participant_id - if it resolves to a real, *different*,
            # active participant we treat the question as verbatim from
            # them. Otherwise we treat it as orchestrator-synthesized.
            asker_id = (oq.get("asker_participant_id") or "").strip() or None
            asker: Participant | None = None
            if asker_id and asker_id in active_ids and asker_id != pid:
                asker = next(
                    p for p in session.participants
                    if p.participant_id == asker_id
                )

            if asker is not None:
                announce = (
                    f"{asker.name} raised a question earlier, to "
                    f"{target.name}: \"{question_text}\""
                )
            else:
                announce = (
                    f"I have a follow-up question for {target.name}: "
                    f"\"{question_text}\""
                )
            announce_msg = _add_orchestrator_message(session, announce, kind="status")
            yield _sse("orchestrator", _msg_payload(announce_msg))

            if target.kind == "human":
                async for chunk in _do_human_turn(
                    session, target, phase=session.phase,
                    actives=_active_participants(session),
                    asker_id=(asker.participant_id if asker else None),
                    asker_name=(asker.name if asker else None),
                    prompt_context=question_text,
                ):
                    yield chunk
                continue

            transcript = _format_history(session.messages)
            if asker is not None:
                prompt2 = TARGETED_FOLLOWUP_FROM_PARTICIPANT_PROMPT.format(
                    transcript=transcript,
                    credential_summary=cred_block,
                    targeted_question=question_text,
                    asker_name=asker.name,
                )
            else:
                prompt2 = TARGETED_FOLLOWUP_PROMPT.format(
                    transcript=transcript,
                    credential_summary=cred_block,
                    targeted_question=question_text,
                )
            stream_events: list[str] = []
            stream_msg_id = str(uuid.uuid4())
            turn = await run_resilient_turn(
                session=session, participant=target,
                user_prompt=prompt2,
                label="targeted_followup",
                max_tokens=600,
                call_participant=_call_participant,
                stream_events=stream_events,
                stream_message_id=stream_msg_id,
            )
            for ev in stream_events:
                yield ev
            for ev in turn.sse_events:
                yield ev
            if not turn.ok:
                for chunk in _participant_turn_failure_sse(session, target):
                    yield chunk
                continue
            speaker = turn.speaker
            text, elapsed = turn.text, turn.elapsed
            # When the orchestrator is relaying a verbatim question from
            # another participant, mark this turn as replying to that
            # asker so the frontend can render the "Replying to X" pill.
            replying_to = [asker.participant_id] if asker is not None else []
            msg = _add_participant_message(
                session, speaker, text, phase=session.phase, elapsed=elapsed,
                replying_to=replying_to,
                message_id=stream_msg_id,
            )
            yield _sse("message", _msg_payload(msg))

            if _participant_msg_cap_hit(session):
                async for chunk in _wait_for_continue(session, "messages"):
                    yield chunk
            if _orchestrator_cap_hit(session):
                async for chunk in _wait_for_continue(session, "orchestrator"):
                    yield chunk

    msg = _add_orchestrator_message(
        session,
        "Moving to finalization.",
        kind="status",
    )
    yield _sse("orchestrator", _msg_payload(msg))


async def _phase_finalization(session: Session) -> AsyncIterator[str]:
    session.phase = Phase.FINALIZATION
    for chunk in _orchestrator_banner_sse(session, "Phase 4: opinion finalization..."):
        yield chunk

    cred_block = credentials_to_block(session.credential_summary)
    actives = _active_participants(session)
    transcript_snapshot = _format_history(session.messages)
    pending_snapshot = {
        p.participant_id: _pending_addressed_for(session, p)
        for p in actives
        if p.kind != "human"
    }

    async def _human_final(p: Participant) -> AsyncIterator[str]:
        async for chunk in _do_human_turn(
            session, p, phase=session.phase, actives=actives,
            track_final_opinion=True,
            prompt_context=(
                "Phase 4: state your final opinion on the question, "
                "incorporating whatever you've learned in the discussion."
            ),
        ):
            yield chunk

    def _build_final_spec(p: Participant) -> _AiTurnSpec | None:
        if p.kind == "human":
            return None
        pending = pending_snapshot.get(p.participant_id, [])
        pending_block = _format_pending_block(pending)
        prompt = FINALIZATION_PROMPT.format(
            question=session.question,
            credential_summary=cred_block,
            transcript=transcript_snapshot,
            pending_block=pending_block,
        )
        return _AiTurnSpec(
            participant=p,
            user_prompt=prompt,
            label="finalization",
            max_tokens=600,
        )

    async def _post_final(result: _AiTurnResult) -> dict[str, Any]:
        session.final_opinions[result.participant.participant_id] = result.turn.text
        return {"replying_to": _replying_to_ids(result.pending)}

    async for chunk in run_roster_ai_turns_parallel(
        session,
        actives,
        phase=session.phase,
        build_spec=_build_final_spec,
        call_participant=_call_participant,
        on_human_turn=_human_final,
        post_process=_post_final,
    ):
        yield chunk


async def _phase_consensus(session: Session) -> AsyncIterator[str]:
    session.phase = Phase.CONSENSUS
    for chunk in _orchestrator_banner_sse(session, "Phase 5: consensus gathering..."):
        yield chunk

    cred_block = credentials_to_block(session.credential_summary)
    actives = _active_participants(session)

    # Initial alliance detection from the finalization-phase opinions
    groups = await detect_alliances(
        orchestrator_model_id=_orchestrator_model_id(session),
        question=session.question,
        participants=actives,
        final_opinions=session.final_opinions,
        api_log=session.api_log,
    )
    _bump_orchestrator_count(session)
    session.alliance_groups = groups

    # Render alliance group members using the same display names shown
    # in the sidebar (Participant.name), not raw participant_ids.
    id_to_name = {p.participant_id: p.name for p in actives}
    alliance_prefix = (
        "Updated alliance groups detected: "
        if session.consensus_attempts > 0
        else "Alliance groups detected: "
    )
    announce = alliance_prefix + "; ".join(
        f"\"{g.get('stance', '')}\" -> ["
        + ", ".join(
            id_to_name.get(m, m) for m in (g.get("members") or [])
        )
        + "]"
        for g in groups
    )
    msg = _add_orchestrator_message(session, announce, kind="status")
    yield _sse("orchestrator", _msg_payload(msg))

    # Round-robin among active participants, but yield to the addressed-to
    # target whenever the previous message named one explicitly. To keep
    # two participants from monopolizing the floor with an A->B->A->B
    # loop, we cap consecutive addressed-to routings at the configured
    # `dyad_cap`. After that many in a row, we force a round-robin pick.
    queue: list[Participant] = list(actives)
    last_addressed: str | None = None
    dyad_run: int = 0
    dyad_cap = session.limits.dyad_cap

    # Hard backstop on this phase: if we make a lot of consensus turns
    # without resolving, exit and let closure handle it. The orchestrator-
    # call cap will usually hit before this, but it's a clean upper bound.
    max_consensus_turns = (
        session.limits.consensus_turns_per_participant * len(actives)
    )
    consensus_turns = 0

    while consensus_turns < max_consensus_turns:
        consensus_turns += 1
        actives = _active_participants(session)
        if len(actives) < 2:
            break
        queue = [p for p in queue if p.enabled]

        # Pick speaker. Prefer the addressed-to target (dyadic exchange)
        # only while we're under the consecutive-routing cap. Once the
        # cap is hit, force a round-robin pick so a third voice can join.
        if last_addressed and dyad_run < dyad_cap:
            speaker = next(
                (p for p in actives if p.participant_id == last_addressed),
                None,
            )
            if speaker is None:
                speaker = queue[0] if queue else actives[0]
                dyad_run = 0
            else:
                queue = [p for p in queue if p.participant_id != speaker.participant_id]
                dyad_run += 1
            last_addressed = None
        else:
            if not queue:
                queue = list(actives)
            speaker = queue.pop(0)
            dyad_run = 0
            last_addressed = None

        if speaker.kind == "human":
            addressed_state: dict[str, Any] = {}
            async for chunk in _do_human_turn(
                session, speaker, phase=session.phase, actives=actives,
                classify_addressed=True,
                addressed_state=addressed_state,
                prompt_context=(
                    "Phase 5: weigh in on whether you agree, disagree, "
                    "or want to refine. Address other participants by "
                    "name when you're responding to something specific "
                    "they said."
                ),
            ):
                yield chunk
            # Propagate addressed_to so dyad routing also works when the
            # last speaker was the human.
            last_addressed = addressed_state.get("last_addressed")
            # Status check every full round (every len(actives) turns).
            # Replicated here because the LLM-path code below also does
            # it, and we need it on the human path too.
            if consensus_turns % max(1, len(actives)) == 0:
                terminal = await _consensus_status_terminal_sse(session, actives)
                if terminal:
                    yield terminal
                    return
            continue

        # Decide allied vs solo prompt
        speaker_group, other_groups = _find_speaker_group(speaker, session.alliance_groups)
        prompt = _build_consensus_prompt(
            session, speaker, speaker_group, other_groups,
            actives, cred_block,
        )

        # Snapshot pending threads BEFORE the call so the outgoing
        # message records who this turn was supposed to be replying to.
        pending = _pending_addressed_for(session, speaker)

        stream_events: list[str] = []
        stream_msg_id = str(uuid.uuid4())
        turn = await run_resilient_turn(
            session=session, participant=speaker,
            user_prompt=prompt,
            label="consensus",
            max_tokens=700,
            call_participant=_call_participant,
            stream_events=stream_events,
            stream_message_id=stream_msg_id,
        )
        for ev in stream_events:
            yield ev
        for ev in turn.sse_events:
            yield ev
        if not turn.ok:
            for chunk in _participant_turn_failure_sse(session, speaker):
                yield chunk
            continue
        # Consensus phase doesn't swap participants, only LLMs behind
        # them, so turn.speaker is the same instance as `speaker`. Use
        # turn.speaker to stay consistent with other phases.
        speaker = turn.speaker
        text, elapsed = turn.text, turn.elapsed

        addressed = await classify_addressed_to(
            orchestrator_model_id=orchestrator_fast_model_id(session),
            participants=actives,
            speaker_name=speaker.name,
            message=text,
            api_log=session.api_log,
        )
        _bump_orchestrator_count(session)
        last_addressed = addressed

        msg = _add_participant_message(
            session, speaker, text, phase=session.phase, elapsed=elapsed,
            addressed_to=addressed,
            replying_to=_replying_to_ids(pending),
            message_id=stream_msg_id,
        )
        yield _sse("message", _msg_payload(msg))

        if _participant_msg_cap_hit(session):
            async for chunk in _wait_for_continue(session, "messages"):
                yield chunk
        if _orchestrator_cap_hit(session):
            async for chunk in _wait_for_continue(session, "orchestrator"):
                yield chunk

        # Status check every full round (every len(actives) turns)
        if consensus_turns % max(1, len(actives)) == 0:
            terminal = await _consensus_status_terminal_sse(session, actives)
            if terminal:
                yield terminal
                return


async def _consensus_status_terminal_sse(
    session: Session,
    actives: list[Participant],
) -> str | None:
    """Run a consensus status check. Returns an SSE chunk when the phase
    should end (majority or unproductive), else None."""
    transcript = await compact_transcript_for_orchestrator(
        session,
        orchestrator_model_id=orchestrator_fast_model_id(session),
    )
    status = await assess_consensus_status(
        orchestrator_model_id=orchestrator_fast_model_id(session),
        question=session.question,
        transcript=transcript,
        alliance_groups=session.alliance_groups,
        api_log=session.api_log,
    )
    _bump_orchestrator_count(session)
    if status.get("status") == "majority":
        session.alliance_groups = await _refresh_alliance_groups(session, actives)
        msg = _add_orchestrator_message(
            session,
            f"Majority reached. {status.get('rationale', '')}".strip(),
            kind="status",
        )
        return _sse("orchestrator", _msg_payload(msg))
    if status.get("status") == "unproductive":
        msg = _add_orchestrator_message(
            session,
            f"Conversation no longer productive. {status.get('rationale', '')}".strip(),
            kind="status",
        )
        return _sse("orchestrator", _msg_payload(msg))
    return None


async def _refresh_alliance_groups(
    session: Session,
    actives: list[Participant],
) -> list[dict[str, Any]]:
    """Re-cluster after the consensus phase, treating the latest round of
    consensus statements as each participant's current stance."""
    latest_by_id: dict[str, str] = {}
    for m in session.messages:
        if m.get("role") != "participant":
            continue
        if m.get("phase") != Phase.CONSENSUS.value:
            continue
        latest_by_id[m["speaker_id"]] = m["text"]
    # Fall back to finalization opinions for any participant who didn't
    # speak in the consensus phase yet.
    merged: dict[str, str] = dict(session.final_opinions)
    merged.update(latest_by_id)
    groups = await detect_alliances(
        orchestrator_model_id=_orchestrator_model_id(session),
        question=session.question,
        participants=actives,
        final_opinions=merged,
        api_log=session.api_log,
    )
    _bump_orchestrator_count(session)
    return groups


def _find_speaker_group(
    speaker: Participant,
    groups: list[dict[str, Any]],
) -> tuple[dict[str, Any] | None, list[dict[str, Any]]]:
    speaker_group: dict[str, Any] | None = None
    others: list[dict[str, Any]] = []
    for g in groups:
        if speaker.participant_id in (g.get("members") or []):
            speaker_group = g
        else:
            others.append(g)
    return speaker_group, others


def _build_consensus_prompt(
    session: Session,
    speaker: Participant,
    speaker_group: dict[str, Any] | None,
    other_groups: list[dict[str, Any]],
    actives: list[Participant],
    cred_block: str,
) -> str:
    transcript = _format_history(session.messages)
    pending_block = _format_pending_block(
        _pending_addressed_for(session, speaker)
    )

    # NOTE: The "speaker is whoever was addressed last" routing happens in
    # _phase_consensus, NOT here. This function only renders the prompt the
    # speaker receives. Whatever needs answering shows up in pending_block,
    # so a single unified allied/solo template handles both targeted and
    # broadcast turns - the prompt's "FIRST address open threads" rule
    # naturally focuses the speaker on whoever was just talking to them.
    if speaker_group and len(speaker_group.get("members") or []) > 1:
        members = ", ".join(
            p.name for p in actives
            if p.participant_id in (speaker_group.get("members") or [])
            and p.participant_id != speaker.participant_id
        ) or "(no co-allies named)"
        return CONSENSUS_ALLIED_PROMPT.format(
            alliance_members=members,
            alliance_stance=speaker_group.get("stance", "(unspecified)"),
            question=session.question,
            credential_summary=cred_block,
            transcript=transcript,
            pending_block=pending_block,
        )

    other_groups_block = "\n".join(
        f"  - \"{g.get('stance', '')}\" supported by " + ", ".join(
            p.name for p in actives if p.participant_id in (g.get("members") or [])
        )
        for g in other_groups
    ) or "(no other groups)"
    return CONSENSUS_SOLO_PROMPT.format(
        your_stance=(speaker_group or {}).get("stance", "(unspecified)"),
        other_groups_block=other_groups_block,
        question=session.question,
        credential_summary=cred_block,
        transcript=transcript,
        pending_block=pending_block,
    )


# ---------------------------------------------------------------------------
# Closure
# ---------------------------------------------------------------------------

async def _phase_closure(session: Session) -> AsyncIterator[str]:
    session.phase = Phase.CLOSURE
    for chunk in _orchestrator_banner_sse(session, "Phase 6: closure..."):
        yield chunk

    cred_block = credentials_to_block(session.credential_summary)
    transcript = await compact_transcript_for_orchestrator(
        session,
        orchestrator_model_id=orchestrator_fast_model_id(session),
    )

    status = await assess_consensus_status(
        orchestrator_model_id=orchestrator_fast_model_id(session),
        question=session.question,
        transcript=transcript,
        alliance_groups=session.alliance_groups,
        api_log=session.api_log,
    )
    _bump_orchestrator_count(session)

    actives = _active_participants(session)
    if status.get("status") == "majority":
        idx = status.get("majority_group_index")
        majority_group = None
        if isinstance(idx, int) and 0 <= idx < len(session.alliance_groups):
            majority_group = session.alliance_groups[idx]
        else:
            # Fallback: largest group wins
            if session.alliance_groups:
                majority_group = max(
                    session.alliance_groups,
                    key=lambda g: len(g.get("members") or []),
                )
        if majority_group:
            members_names = [
                p.name for p in actives
                if p.participant_id in (majority_group.get("members") or [])
            ]
            stance = majority_group.get("stance", "")
            prompt = MAJORITY_REPORT_PROMPT.format(
                question=session.question,
                credential_summary=cred_block,
                majority_members=", ".join(members_names),
                majority_stance=stance,
                transcript=transcript,
            )
            raw, _ = await orchestrator_call(
                orchestrator_model_id=_orchestrator_model_id(session),
                user_prompt=prompt,
                label="majority_report",
                api_log=session.api_log,
                expect_json=False,
                max_tokens=900,
                temperature=0.3,
            )
            _bump_orchestrator_count(session)
            session.final_report = {
                "kind": "majority",
                "text": raw,
                "majority_members": members_names,
                "majority_stance": stance,
                "alliance_groups": session.alliance_groups,
            }
            msg = _add_orchestrator_message(
                session, raw, kind="majority_report",
                extra={"majority_members": members_names, "majority_stance": stance},
            )
            yield _sse("orchestrator", _msg_payload(msg))
            return

    # Not productive / no majority. We may surface an unaddressed
    # factor and re-run consensus up to `stall_recovery_attempts` times
    # before giving up and emitting the no-consensus report.
    if session.consensus_attempts < session.limits.stall_recovery_attempts:
        session.consensus_attempts += 1
        factor = await find_unaddressed_factor(
            orchestrator_model_id=_orchestrator_model_id(session),
            question=session.question,
            credential_summary_block=cred_block,
            transcript=transcript,
            api_log=session.api_log,
        )
        _bump_orchestrator_count(session)
        if factor and factor.get("factor"):
            announce = (
                f"The discussion has stalled. The orchestrator surfaces a new "
                f"factor for the group to consider: {factor['factor']}"
            )
            msg = _add_orchestrator_message(
                session, announce, kind="factor",
                extra={"expected_to_shift": factor.get("expected_to_shift") or []},
            )
            yield _sse("orchestrator", _msg_payload(msg))
            # Re-run the consensus phase once more
            async for chunk in _phase_consensus(session):
                yield chunk
            async for chunk in _phase_closure(session):
                yield chunk
            return

    # Failed twice (or no factor surfaced) -> emit no-consensus report
    prompt = NO_CONSENSUS_REPORT_PROMPT.format(
        question=session.question,
        credential_summary=cred_block,
        alliance_block="\n".join(
            f"  - \"{g.get('stance', '')}\": "
            + ", ".join(
                p.name for p in actives
                if p.participant_id in (g.get("members") or [])
            )
            for g in session.alliance_groups
        ),
        transcript=transcript,
    )
    raw, _ = await orchestrator_call(
        orchestrator_model_id=_orchestrator_model_id(session),
        user_prompt=prompt,
        label="no_consensus_report",
        api_log=session.api_log,
        expect_json=False,
        max_tokens=900,
        temperature=0.3,
    )
    _bump_orchestrator_count(session)
    session.final_report = {
        "kind": "no_consensus",
        "text": raw,
        "alliance_groups": session.alliance_groups,
    }
    msg = _add_orchestrator_message(session, raw, kind="no_consensus_report")
    yield _sse("orchestrator", _msg_payload(msg))


# ---------------------------------------------------------------------------
# Public driver
# ---------------------------------------------------------------------------

async def run_conversation(session: Session) -> AsyncIterator[str]:
    """Drive the full conversation, yielding SSE chunks.

    The flow is structure → decision: the chosen ConversationStructure
    runs its phases, then hands a DecisionInput to the chosen
    DecisionMethod which runs the decision phase(s). Both are
    resolved from `session.conversation_structure_id` /
    `session.decision_method_id` (defaults: collaborative + consensus,
    which preserves the original CCAI behavior).
    """
    # Lazy import so the conversation package can import orchestrator
    # helpers without a circular module load.
    from app.services.conversation import get_structure, get_decision

    actives = _active_participants(session)
    if len(actives) < 2:
        yield _sse("error", {
            "message": "Need at least 2 active participants to start.",
        })
        yield _sse("done", {})
        return
    if len(actives) > session.max_participants:
        # Defense in depth - the API layer should have already enforced this.
        for extra in actives[session.max_participants:]:
            extra.enabled = False

    structure_cls = get_structure(session.conversation_structure_id)
    decision_cls = get_decision(session.decision_method_id)
    structure = structure_cls(session)

    try:
        async for chunk in structure.run():
            yield chunk

        # Kick off contribution summaries in the background just before
        # the decision phase. The Table View blocks on this task only if
        # the user opens it before it finishes - usually it'll be done
        # by then, so the table loads instantly.
        _start_contribution_summary_task(session)

        decision_input = structure.build_decision_input()
        decision = decision_cls(session, decision_input)
        async for chunk in decision.run():
            yield chunk
    except Exception as exc:
        LOG.exception("Conversation crashed: %s", exc)
        yield _sse("error", {"message": f"Internal error: {exc}"})
    finally:
        session.finished = True
        session.phase = Phase.FINISHED
        # Drop the human-input slot (if any) so its asyncio.Event
        # doesn't outlive the session in the module-level registry.
        human_io.drop_session(session.session_id)

    yield _sse("system", {"text": "End of Chat", "phase": session.phase.value})
    yield _sse("done", {})


def _start_contribution_summary_task(session: Session) -> None:
    """Schedule the contribution-summary build as a background task.

    Idempotent: if a task is already in flight (or completed) we don't
    start another one. Errors in the background task are swallowed and
    logged - the Table View endpoint will fall back to a synchronous
    build if needed.
    """
    if session.contribution_summary_task is not None:
        return
    if any((session.contribution_summaries or {}).values()):
        return

    async def _runner() -> None:
        try:
            await _build_contribution_summaries(session)
        except Exception as exc:  # noqa: BLE001
            LOG.warning(
                "Background contribution_summaries failed for %s: %s",
                session.session_id, exc,
            )

    try:
        session.contribution_summary_task = asyncio.create_task(_runner())
    except RuntimeError:
        session.contribution_summary_task = None


async def ensure_contribution_summaries(session: Session) -> None:
    """Block on contribution summaries for the Table View.

    Order of preference:
      1. Cached - return immediately.
      2. Background task in flight - await it.
      3. Nothing started - build synchronously.
    """
    if any((session.contribution_summaries or {}).values()):
        return
    task = session.contribution_summary_task
    if task is not None and not task.done():
        try:
            await task
        except Exception as exc:  # noqa: BLE001
            LOG.warning("contribution_summary_task await failed: %s", exc)
    if any((session.contribution_summaries or {}).values()):
        return
    await _build_contribution_summaries(session)


async def _build_contribution_summaries(session: Session) -> None:
    actives = _active_participants(session)
    roster = "\n".join(
        f"- id: {p.participant_id} | name: {p.name}" for p in actives
    )
    transcript = _format_history(session.messages)
    prompt = CONTRIBUTION_SUMMARY_PROMPT.format(
        roster_block=roster,
        transcript=transcript,
    )
    _raw, parsed = await orchestrator_call(
        orchestrator_model_id=_orchestrator_model_id(session),
        user_prompt=prompt,
        label="contribution_summaries",
        api_log=session.api_log,
        max_tokens=900,
    )
    session.orchestrator_call_count += 1
    if isinstance(parsed, dict) and isinstance(parsed.get("contributions"), list):
        for c in parsed["contributions"]:
            pid = c.get("participant_id")
            summary = (c.get("summary") or "").strip()
            if pid and summary:
                session.contribution_summaries[pid] = summary