File size: 20,996 Bytes
9d29c62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# domain/semantic_bank.py โ€” V7.2.5: Semantic Composer
#
# The SemanticBank is a governed, deterministic source of pedagogical narratives.
# Instead of letting LLM #2 generate free text (risky), the NarrativeComposer picks
# from curated, teacher-grade Hebrew templates and injects server-signed results.
#
# CTO directive: text must sound like a real private tutor speaking to a 10th/12th grader.
# Write at eye level โ€” encouraging, intuitive, NOT like system error messages.

import random
import logging
from dataclasses import dataclass, field
from collections import Counter
from typing import List, Dict, Optional

from domain import telemetry

logger = logging.getLogger(__name__)

DRIFT_ALERT_THRESHOLD = 0.35  # Emit alert when one variant dominates above this fraction


# โ”€โ”€โ”€ Data Model โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

@dataclass
class SemanticEntry:
    """
    A single pedagogical concept in the bank.
    Each entry holds multiple variant fragments to ensure narrative diversity.
    """
    concept_tag: str           # matches Planner Enum action (e.g. "SOLVE_EQUATION")
    openings: List[str]        # 3 opening phrases โ€” how to START the explanation
    bridges: List[str]         # 3 logic bridges โ€” HOW to connect the steps
    analogies: List[str]       # 2 analogy/metaphor sentences โ€” the "aha!" moments
    closing: str               # 1 closing encouragement sentence


# โ”€โ”€โ”€ The Bank โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

SEMANTIC_BANK: Dict[str, SemanticEntry] = {

    "SOLVE_EQUATION": SemanticEntry(
        concept_tag="SOLVE_EQUATION",
        openings=[
            "ื‘ื•ืื• ื ืคืชื•ืจ ืืช ื”ืžืฉื•ื•ืื” ืฆืขื“ ืื—ืจ ืฆืขื“, ืืœ ืชื“ืื’, ื–ื” ืคื—ื•ืช ืžืคื—ื™ื“ ืžืฉื ืจืื”.",
            "ื›ื“ื™ ืœื’ืœื•ืช ืืช ืขืจืš ื”ื ืขืœื, ืื ื—ื ื• ื”ื•ืœื›ื™ื ืœื‘ื•ื“ื“ ืื•ืชื• ืžืฉื ื™ ืฆื™ื“ื™ ื”ืžืฉื•ื•ืื”.",
            "ื ื—ืฉื•ื‘ ืขืœ ื”ืžืฉื•ื•ืื” ื›ืžื• ืžืื–ื ื™ื™ื: ื›ืœ ืžื” ืฉืขื•ืฉื™ื ื‘ืฆื“ ื™ืžื™ืŸ, ืขื•ืฉื™ื ื’ื ื‘ืฆื“ ืฉืžืืœ.",
        ],
        bridges=[
            "ืื—ืจื™ ื”ืคืขื•ืœื” ื”ื–ื•, ื›ืœ ืžื” ืฉื ื•ืชืจ ื”ื•ื:",
            "ื•ื›ืฉื ืคืฉื˜ ืืช ื›ืœ ืžื” ืฉืงื™ื‘ืœื ื•, ื”ืชื•ืฆืื” ืžืชื’ืœื”:",
            "ื”ืžืฉื•ื•ืื” 'ื”ืชื ืงืชื”' ื•ืขื›ืฉื™ื• ื‘ืจื•ืจ ืฉื”ืชืฉื•ื‘ื” ื”ื™ื:",
        ],
        analogies=[
            "ื—ืฉื•ื‘ ืขืœ ื–ื” ื›ืžื• ื—ื™ื“ื” โ€” ืื ื—ื ื• ืžืกื™ืจื™ื ืจืžื–ื™ื ืื—ื“ ืื—ื“ ืขื“ ืฉื”ื ืขืœื ื—ืฉื•ืฃ ืœื’ืžืจื™.",
            "ื‘ื“ื™ื•ืง ื›ืžื• ืฉืžืฉื—ืงื™ื ืžื—ื‘ื•ืื™ื โ€” ืื ื—ื ื• ืžื—ืคืฉื™ื ืืช x ืขื“ ืฉื”ื•ื ื›ื‘ืจ ืœื ื™ื›ื•ืœ ืœื”ืชื—ื‘ื.",
        ],
        closing="ืขืฉื™ืช ืืช ื–ื”! ื–ื• ื”ื“ืจืš ื”ืจืฉืžื™ืช ืฉื‘ื” ืžืชืžื˜ื™ืงืื™ื ืคื•ืชืจื™ื ืžืฉื•ื•ืื•ืช.",
    ),

    "SIMPLIFY": SemanticEntry(
        concept_tag="SIMPLIFY",
        openings=[
            "ื”ื‘ื™ื˜ื•ื™ ื”ื–ื” ื ืจืื” ืžืกื•ื‘ืš, ืื‘ืœ ืื ื ืกื“ืจ ืื•ืชื• โ€” ื”ื•ื ืžืชืงืคืœ ืœืฆื•ืจื” ื”ืจื‘ื” ื™ื•ืชืจ ื ืงื™ื™ื”.",
            "ื‘ืคื™ืฉื•ื˜, ื”ืžื˜ืจื” ื”ื™ื ืœื›ืชื•ื‘ ืืช ืื•ืชื• ื”ื“ื‘ืจ โ€” ืจืง ื‘ืฆื•ืจื” ื”ืžื™ื ื™ืžืœื™ืช ื‘ื™ื•ืชืจ.",
            "ื›ืžื• ืœืกื“ืจ ื—ื“ืจ ืžื‘ื•ืœื’ืŸ โ€” ืื ื—ื ื• ืื•ืกืคื™ื ืื™ื‘ืจื™ื ื“ื•ืžื™ื ื•ืžืกืœืงื™ื ืžื” ืฉืžื™ื•ืชืจ.",
        ],
        bridges=[
            "ืื—ืจื™ ืฉื ืืกื•ืฃ ืืช ื›ืœ ื”ืื™ื‘ืจื™ื ื”ื“ื•ืžื™ื ื™ื—ื“, ื”ื‘ื™ื˜ื•ื™ ื”ืคืฉื•ื˜ ื”ื•ื:",
            "ื›ืฉืžืกืœืงื™ื ืืช ื›ืœ 'ื”ืจืขืฉ ื”ืžืชืžื˜ื™', ืžื” ืฉื ืฉืืจ ื”ื•ื:",
            "ื”ืฆื•ืจื” ื”ืคืฉื•ื˜ื” ื•ื”ื ืงื™ื™ื” ื‘ื™ื•ืชืจ ืฉืœ ื”ื‘ื™ื˜ื•ื™:",
        ],
        analogies=[
            "ื“ืžื™ื™ืŸ ืฉื™ืฉ ืœืš ื”ืจื‘ื” ืฉื˜ืจื•ืช ืฉืœ ื›ืกืฃ โ€” ืืชื” ืžื—ืœื™ืฃ ืื•ืชื ืœืฉื˜ืจ ืื—ื“ ื’ื“ื•ืœ. ื–ื” ื‘ื“ื™ื•ืง ืžื” ืฉืื ื—ื ื• ืขื•ืฉื™ื ืขื ื”ื‘ื™ื˜ื•ื™.",
            "ื›ืžื• ืœืงืคืœ ืžืคืช ืขื ืง โ€” ื‘ืกื•ืฃ ืงื™ื‘ืœืช ืืช ืื•ืชื• ื”ืžื™ื“ืข, ืจืง ื‘ื’ื•ื“ืœ ืฉื ื•ื— ืœืฉืืช.",
        ],
        closing="ื™ื•ืคื™! ืคื™ืฉื•ื˜ ื–ื” ืื—ืช ื”ื›ื™ืฉื•ืจื™ื ื”ื›ื™ ืฉื™ืžื•ืฉื™ื™ื ื‘ืžืชืžื˜ื™ืงื” โ€” ื•ืขื›ืฉื™ื• ืืชื” ื™ื•ื“ืข ืœืขืฉื•ืช ืืช ื–ื”.",
    ),

    "FACTOR": SemanticEntry(
        concept_tag="FACTOR",
        openings=[
            "ืคื™ืจื•ืง ืœื’ื•ืจืžื™ื ื–ื” ื›ืžื• ืœืžืฆื•ื ืืช 'ื”ื‘ื ื™ื™ื ื™ื' ืฉืžืจื›ื™ื‘ื™ื ืืช ื”ื‘ื™ื˜ื•ื™ ืฉืœื ื•.",
            "ื‘ืžืงื•ื ืœืจืื•ืช ื‘ื™ื˜ื•ื™ ืื—ื“ ื’ื“ื•ืœ, ืื ื—ื ื• ืžื—ืคืฉื™ื ืฉื ื™ ื‘ื™ื˜ื•ื™ื™ื ืงื˜ื ื™ื ืฉืžื•ื›ืคืœื™ื ื–ื” ื‘ื–ื”.",
            "ื”ื˜ืจื™ืง ื‘ืคื™ืจื•ืง ืœื’ื•ืจืžื™ื ื”ื•ื ืœื–ื”ื•ืช ืžื” 'ืžืกืชืชืจ' ื‘ืชื•ืš ื”ื‘ื™ื˜ื•ื™ ื•ืืคืฉืจ ืœื”ื•ืฆื™ื ื”ื—ื•ืฆื”.",
        ],
        bridges=[
            "ื›ืฉื ื•ืฆื™ื ืืช ื”ื’ื•ืจื ื”ืžืฉื•ืชืฃ, ื ืงื‘ืœ:",
            "ืื—ืจื™ ื”ืคื™ืจื•ืง, ื”ื‘ื™ื˜ื•ื™ ื ื›ืชื‘ ื‘ืฆื•ืจื” ื”ื›ืคืœื™ืช:",
            "ื”ื’ื•ืจืžื™ื ืฉืžืจื›ื™ื‘ื™ื ืืช ื”ื‘ื™ื˜ื•ื™ ื”ื:",
        ],
        analogies=[
            "ืคื™ืจื•ืง ืœื’ื•ืจืžื™ื ื”ื•ื ื›ืžื• ืœืคืจืง ืžืกืคืจ ืœื—ืœื•ืงื” ืจืืฉื•ื ื™ืช โ€” ืื ื—ื ื• ืžื•ืฆืื™ื ืืช ื”-DNA ื”ืžืชืžื˜ื™ ืฉืœ ื”ื‘ื™ื˜ื•ื™.",
            "ื“ืžื™ื™ืŸ ืฉืืชื” ืžืคืจืง ืืจื’ื– ื’ื“ื•ืœ ืœืงื•ืคืกืื•ืช ืงื˜ื ื•ืช โ€” ื›ืœ ืงื•ืคืกื ื”ื™ื ื’ื•ืจื. ื‘ื™ื—ื“ ื”ืŸ ืžืจื›ื™ื‘ื•ืช ืืช ื”ืžืงื•ืจ.",
        ],
        closing="ืคื™ืจื•ืง ืœื’ื•ืจืžื™ื ื”ื•ื ื‘ื“ื™ื•ืง ืžื” ืฉืžืืคืฉืจ ืœื ื• ืœืคืชื•ืจ ืžืฉื•ื•ืื•ืช ืจื™ื‘ื•ืขื™ื•ืช โ€” ืชื›ื™ืจ ืืช ื”ื›ืœื™ ื”ื–ื” ื˜ื•ื‘.",
    ),

    "EXPAND": SemanticEntry(
        concept_tag="EXPAND",
        openings=[
            "ืขื›ืฉื™ื• ื ืคืจื•ืฉ ืืช ื”ืกื•ื’ืจื™ื™ื โ€” ื ื›ืคื™ืœ ื›ืœ ืื™ื‘ืจ ื‘ืชื•ืš ื”ืกื•ื’ืจื™ื™ื ืขื ื›ืœ ืžื” ืฉืžื—ื•ืฆื” ืœื”ื.",
            "ืคืชื™ื—ืช ืกื•ื’ืจื™ื™ื ื”ื™ื ื›ืžื• 'ืœืคืชื•ื—' ืžืชื ื” ืขื˜ื•ืคื” โ€” ืžื” ืฉืžืกืชืชืจ ื‘ืคื ื™ื ื™ื•ืฆื ืœืื•ืจ.",
            "ื ืฉืชืžืฉ ื‘ื—ื•ืง ื”ืคื™ืœื•ื’ ื›ื“ื™ ืœื”ืจื—ื™ื‘ ืืช ื”ื‘ื™ื˜ื•ื™ ื•ืœืจืื•ืช ืืช ื›ืœ ื”ืื™ื‘ืจื™ื ื‘ื ืคืจื“.",
        ],
        bridges=[
            "ืœืื—ืจ ืคืชื™ื—ืช ื”ืกื•ื’ืจื™ื™ื ื•ืื™ืกื•ืฃ ื”ืื™ื‘ืจื™ื ื”ื“ื•ืžื™ื:",
            "ื›ืฉืžืจื—ื™ื‘ื™ื ื”ื›ืœ ื•ืžืกื“ืจื™ื:",
            "ื”ื‘ื™ื˜ื•ื™ ื”ืžื•ืจื—ื‘, ืขื ื›ืœ ื”ืื™ื‘ืจื™ื ื’ืœื•ื™ื™ื:",
        ],
        analogies=[
            "ื—ืฉื•ื‘ ืขืœ ื–ื” ื›ืžื• ืœื”ื›ืคื™ืœ ืชืžื—ื™ืจ โ€” ืื ืงื ื™ืช ืฉืœื•ืฉื” ืฉืงื™ื, ื›ืœ ืื—ื“ ืขื ืชืคื•ื—ื™ื ื•ื‘ื ื ื•ืช, ืืชื” ืžื—ืฉื‘ ื›ืžื” ืชืคื•ื—ื™ื ื•ื›ืžื” ื‘ื ื ื•ืช ื™ืฉ ื‘ืกืš ื”ื›ืœ.",
            "ืคืชื™ื—ืช ืกื•ื’ืจื™ื™ื ื”ื™ื ื›ืžื• ืœื’ืœื’ืœ ื‘ืฆืง โ€” ืœื•ื—ืฆื™ื ื•ืžืจื—ื™ื‘ื™ื ืขื“ ืฉื”ื›ืœ ืฉื˜ื•ื— ื•ื ืจืื”.",
        ],
        closing="ื”ืจื—ื‘ืช ื”ื‘ื™ื˜ื•ื™ ื–ื• ืžื™ื•ืžื ื•ืช ื‘ืกื™ืกื™ืช ืฉืชืฉืชืžืฉ ื‘ื” ืฉื•ื‘ ื•ืฉื•ื‘ โ€” ื•ื›ื‘ืจ ืฉืœื˜ืช ื‘ื”.",
    ),

    "FIND_DERIVATIVE": SemanticEntry(
        concept_tag="FIND_DERIVATIVE",
        openings=[
            "ื”ื ื’ื–ืจืช ืื•ืžืจืช ืœื ื• ื›ืžื” ืžื”ืจ ื”ืคื•ื ืงืฆื™ื” ืขื•ืœื” ืื• ื™ื•ืจื“ืช โ€” ื”ื™ื ื›ืžื• 'ืžื“ ื”ืžื”ื™ืจื•ืช' ืฉืœ ื”ื’ืจืฃ.",
            "ื›ื“ื™ ืœืžืฆื•ื ืืช ื”ื ื’ื–ืจืช, ืื ื—ื ื• ืฉื•ืืœื™ื: ืื x ื–ื– ืงืฆืช ืงื“ื™ืžื” โ€” ื›ืžื” y ืžืฉืชื ื”?",
            "ื ื—ืฉื‘ ืืช ื”ื ื’ื–ืจืช ืœืคื™ ื›ืœืœื™ ื”ื’ื–ื™ืจื” ืฉื ืœืžื“ื• โ€” ื–ื” ื”ืจื‘ื” ื™ื•ืชืจ ืžื”ื™ืจ ืžื”ื”ื’ื“ืจื” ื”ื‘ืกื™ืกื™ืช.",
        ],
        bridges=[
            "ืœืคื™ ื›ืœืœ ื”ื’ื–ื™ืจื” ื•ืจืฉื™ืžืช ื”ื ื’ื–ืจื•ืช, ืงื™ื‘ืœื ื•:",
            "ื”ื ื’ื–ืจืช, ืฉืžื™ื™ืฆื’ืช ืืช ืฉื™ืคื•ืข ื”ืžืฉื™ืง ืœืคื•ื ืงืฆื™ื”, ื”ื™ื:",
            "ืงืฆื‘ ื”ืฉื™ื ื•ื™ ื”ืžื™ื™ื“ื™ ืฉืœ ื”ืคื•ื ืงืฆื™ื” ื”ื•ื:",
        ],
        analogies=[
            "ืื ื”ืคื•ื ืงืฆื™ื” ื”ื™ื ืžืกืœื•ืœ ื ืกื™ืขื”, ื”ื ื’ื–ืจืช ื”ื™ื ื”ืžื“-ืžื”ื™ืจื•ืช โ€” ื”ื™ื ืื•ืžืจืช ืœืš ื›ืžื” ืžื”ืจ ืืชื” ื ืข ื‘ื›ืœ ืจื’ืข.",
            "ื“ืžื™ื™ืŸ ืฉืืชื” ืžื˜ืคืก ืขืœ ื”ืจ โ€” ื”ื ื’ื–ืจืช ืื•ืžืจืช ืœืš ื‘ื›ืœ ื ืงื•ื“ื” ืขื“ ื›ืžื” ื”ืชืœื™ืœื•ืช. ื—ื™ื•ื‘ื™ืช = ืขื•ืœื™ื, ืฉืœื™ืœื™ืช = ื™ื•ืจื“ื™ื.",
        ],
        closing="ื”ื ื’ื–ืจืช ืคื•ืชื—ืช ืขื•ืœื ืฉืœื โ€” ืžื ื™ืชื•ื— ืงืฆื•ื•ืช ื•ืขื“ ืžื›ื ื™ืงื”. ื›ืœ ื”ื›ื‘ื•ื“ ืขืœ ื”ืžื™ื•ืžื ื•ืช ื”ื–ื•.",
    ),

    "FIND_INTEGRAL": SemanticEntry(
        concept_tag="FIND_INTEGRAL",
        openings=[
            "ื”ืื™ื ื˜ื’ืจืœ ื”ื•ื ื”ืคืขื•ืœื” ื”ื”ืคื•ื›ื” ืœื’ื–ื™ืจื” โ€” ืื ื—ื ื• ืฉื•ืืœื™ื 'ืžื” ื”ืคื•ื ืงืฆื™ื” ืฉื”ื ื’ื–ืจืช ืฉืœื” ื”ื™ื ื–ื•?'.",
            "ื›ื“ื™ ืœื—ืฉื‘ ืืช ื”ืื™ื ื˜ื’ืจืœ, ื ืฉืชืžืฉ ื‘ื ื•ืกื—ืื•ืช ื”ื‘ืกื™ืกื™ื•ืช ื•ื‘ื—ื•ืง ื”-C, ื”-ืงื‘ื•ืข ื”ืื™ื ื˜ื’ืจืฆื™ื”.",
            "ื”ืื™ื ื˜ื’ืจืœ ื”ืœื ืžืกื•ื™ื ื ื•ืชืŸ ืœื ื• ืžืฉืคื—ื” ืฉืœืžื” ืฉืœ ืคื•ื ืงืฆื™ื•ืช โ€” ืฉื•ื ื•ืช ื–ื• ืžื–ื• ื‘ืงื‘ื•ืข ื‘ืœื‘ื“.",
        ],
        bridges=[
            "ืื—ืจื™ ืื™ื ื˜ื’ืจืฆื™ื” ืœืคื™ ื›ืœืœ ื”ื”ืคื™ื›ื” ืฉืœ ื”ื ื’ื–ืจืช:",
            "ื”ืคื•ื ืงืฆื™ื” ื”ืคืจื™ืžื™ื˜ื™ื‘ื™ืช ืฉืžืจื›ื™ื‘ื” ืืช ื”ืื™ื ื˜ื’ืจืœ ื”ื™ื:",
            "ื‘ื™ืฆื•ืข ื”ืื™ื ื˜ื’ืจืฆื™ื” ื ื•ืชืŸ ืœื ื•:",
        ],
        analogies=[
            "ืื ื”ื ื’ื–ืจืช ื”ื™ื ืžื“-ื”ืžื”ื™ืจื•ืช, ื”ืื™ื ื˜ื’ืจืœ ื”ื•ื ืžื“-ื”ืžืจื—ืง โ€” ื”ื•ื ืื•ืกืฃ ืืช ื›ืœ ื”ืฉื™ื ื•ื™ื™ื ื”ืงื˜ื ื™ื ืœืกื›ื•ื ืื—ื“.",
            "ื—ืฉื•ื‘ ืขืœ ืื™ื ื˜ื’ืจืฆื™ื” ื›ืžื• ืœื’ืœื’ืœ ืกืจื˜ ืœืื—ื•ืจ โ€” ืื ื—ื ื• 'ืžื—ื–ื™ืจื™ื' ืืช ื”ื’ื–ื™ืจื” ืœื ืงื•ื“ืช ื”ืžื•ืฆื ืฉืœื”.",
        ],
        closing="ืื™ื ื˜ื’ืจืฆื™ื” ื”ื™ื ืœื‘ ื—ืฉื‘ื•ืŸ ืื™ื ืคื™ื ื™ื˜ืกื™ืžืœื™ โ€” ื•ืขื›ืฉื™ื• ื™ืฉ ืœืš ืืช ื”ื›ืœื™ ืœื—ืฉื‘ ืฉื˜ื—ื™ื, ื ืคื—ื™ื ื•ืขื•ื“.",
    ),

    "SUBSTITUTE": SemanticEntry(
        concept_tag="SUBSTITUTE",
        openings=[
            "ื›ื“ื™ ืœื‘ื“ื•ืง ืืช ื”ืคืชืจื•ืŸ (ืื• ืœื”ืฉืœื™ื ื—ื™ืฉื•ื‘), ื ืฆื™ื‘ ืืช ื”ืขืจืš ื”ื™ื“ื•ืข ื•ื ืคืฉื˜.",
            "ื”ื”ืฆื‘ื” ื”ื™ื ื”ื“ืจืš ืฉืœื ื• ืœื—ื‘ืจ ื‘ื™ืŸ ืฉื ื™ ื—ืœืงื™ ื”ื‘ืขื™ื” โ€” ืžืฆื™ื‘ื™ื ืžื” ืฉื™ื•ื“ืขื™ื ื•ืจื•ืื™ื ืžื” ื™ื•ืฆื.",
            "ื ื—ืœื™ืฃ ืืช ื”ืžืฉืชื ื” ื‘ืขืจืš ื”ื ืชื•ืŸ ื•ื ื—ืฉื‘ โ€” ื–ื” ื™ืฉืื™ืจ ืœื ื• ื‘ื™ื˜ื•ื™ ื”ืจื‘ื” ื™ื•ืชืจ ืคืฉื•ื˜.",
        ],
        bridges=[
            "ืœืื—ืจ ื”ื”ืฆื‘ื” ื•ื”ืคื™ืฉื•ื˜:",
            "ื›ืฉื ืฆื™ื‘ ื•ื ืคืฉื˜ ืืช ื”ื‘ื™ื˜ื•ื™ ืฉืงื™ื‘ืœื ื•:",
            "ืขืจืš ื”ื”ืฆื‘ื” ืžื ื™ื‘:",
        ],
        analogies=[
            "ื”ืฆื‘ื” ื”ื™ื ื›ืžื• ืœืžืœื ืฉื ื‘ื˜ื•ืคืก โ€” ืื ื—ื ื• ืžื—ืœื™ืคื™ื ืืช 'ื”ื ืขืœื' ื‘ืขืจืš ื”ืžืžืฉื™ ืฉืžืฆืื ื•.",
            "ื“ืžื™ื™ืŸ ืžืชื›ื•ืŸ ืฉืื•ืžืจ 'ืกืคืœ ืกื•ื›ืจ' โ€” ื‘ืจื’ืข ืฉืืชื” ื™ื•ื“ืข ื›ืžื” ื’ื“ื•ืœ ื”ืกืคืœ, ืืชื” ื™ื›ื•ืœ ืœื—ืฉื‘ ื›ืžื•ืช ืžื“ื•ื™ืงืช.",
        ],
        closing="ืžืฆื•ื™ืŸ! ื”ื”ืฆื‘ื” ื”ื™ื ื’ื ื“ืจืš ืžืฆื•ื™ื ืช ืœื‘ื“ื•ืง ืฉื”ืคืชืจื•ืŸ ืฉืžืฆืืช ื”ื•ื ื ื›ื•ืŸ.",
    ),

    # โ”€โ”€ V7.3: Geometry / Analytic Entries โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    "FIND_AXIS_INTERSECTIONS": SemanticEntry(
        concept_tag="FIND_AXIS_INTERSECTIONS",
        openings=[
            "ื›ื“ื™ ืœื’ืœื•ืช ืื™ืคื” ื”ืžืขื’ืœ ืคื•ื’ืฉ ืืช ื”ืฆื™ืจื™ื, ื ืฉืืœ ืฉืืœื” ืคืฉื•ื˜ื”: ืžื” ืงื•ืจื” ื›ืฉื ืงื•ื“ื” ื ืžืฆืืช ืžืžืฉ ืขืœ ืฆื™ืจ ื”-x? ื”-y ืฉืœื” ืฉื•ื•ื” ืœืืคืก! ื•ืœื”ืคืš.",
            "ื ืงื•ื“ื•ืช ื—ื™ืชื•ืš ืขื ื”ืฆื™ืจื™ื ื”ืŸ ื”ื ืงื•ื“ื•ืช ืฉื‘ื”ืŸ ื”ืžืขื’ืœ ื—ื•ืฆื” ืืช 'ื”ื›ื‘ื™ืฉื™ื' ืฉืœ ืžืขืจื›ืช ื”ืฆื™ืจื™ื.",
            "ื”ื“ืจืš ืœืืชืจ ืืช ื ืงื•ื“ื•ืช ื”ื—ื™ืชื•ืš ืขื ื”ืฆื™ืจื™ื ื”ื™ื ืœื”ืฆื™ื‘ ืืคืก: ืคืขื ืขื‘ื•ืจ x ื•ืคืขื ืขื‘ื•ืจ y, ื•ืœืจืื•ืช ืžื” ื”ืคืชืจื•ืŸ ืฉื™ื•ืฆื.",
        ],
        bridges=[
            "ื›ืฉื ืฆื™ื‘ ื•ื ืคืชื•ืจ, ื”ื ืงื•ื“ื•ืช ืฉืžืฆืื ื• ื”ืŸ:",
            "ืœืื—ืจ ื”ื”ืฆื‘ื” ื•ืงื‘ืœืช ื”ืคืชืจื•ื ื•ืช, ื ืงื•ื“ื•ืช ื”ื—ื™ืชื•ืš ืขื ื”ืฆื™ืจื™ื ื”ืŸ:",
            "ื”ืคืชืจื•ื ื•ืช ืฉืงื™ื‘ืœื ื• ืžืฆื™ื™ื ื™ื ืืช ื”ื ืงื•ื“ื•ืช ืฉื‘ื”ืŸ ื”ืžืขื’ืœ ื—ื•ืฆื” ืืช ื”ืฆื™ืจื™ื:",
        ],
        analogies=[
            "ื—ืฉื•ื‘ ืขืœ ืžืขื’ืœ ื›ืžื• ื›ื“ื•ืจ ืฉืžืชื’ืœื’ืœ ืขืœ ืจืฆืคื” โ€” ื ืงื•ื“ื•ืช ื”ื—ื™ืชื•ืš ืขื ืฆื™ืจ ื”-x ื”ืŸ ื‘ื“ื™ื•ืง ื”ืžืงื•ืžื•ืช ืฉื”ื›ื“ื•ืจ ื ื•ื’ืข ื‘ืจืฆืคื”.",
            "ื“ืžื™ื™ืŸ ืฉืืชื” ืžืกืชื›ืœ ืขืœ ืžืคื” ื•ืžื—ืคืฉ ืื™ืคื” ื›ื‘ื™ืฉ ืจืืฉื™ ื—ื•ืฆื” ืืช ื”ื ื”ืจ โ€” ื–ื” ื‘ื“ื™ื•ืง ืžื” ืฉืื ื—ื ื• ืขื•ืฉื™ื ืขื ื”ืžืขื’ืœ ื•ื”ืฆื™ืจื™ื.",
        ],
        closing="ืžืฆืื ื• ืืช ื›ืœ ื ืงื•ื“ื•ืช ื”ืžื’ืข ืฉืœ ื”ืžืขื’ืœ ืขื ืžืขืจื›ืช ื”ืฆื™ืจื™ื โ€” ืขื‘ื•ื“ื” ืžื“ื•ื™ืงืช!",
    ),

    "CALCULATE_SLOPE_AND_LINE": SemanticEntry(
        concept_tag="CALCULATE_SLOPE_AND_LINE",
        openings=[
            "ื›ื“ื™ ืœืžืฆื•ื ืืช ืžืฉื•ื•ืืช ื”ื™ืฉืจ ื”ืขื•ื‘ืจ ื“ืจืš ืฉืชื™ ื ืงื•ื“ื•ืช, ื ื—ืฉื‘ ืงื•ื“ื ืืช ื”ืฉื™ืคื•ืข โ€” ื›ืžื” 'ืชืœื•ืœ' ื”ื™ืฉืจ.",
            "ืฉื™ืคื•ืข ื”ื™ืฉืจ ื”ื•ื ื”ื™ื—ืก ื‘ื™ืŸ ื”ืฉื™ื ื•ื™ ื‘ื’ื•ื‘ื” ืœืฉื™ื ื•ื™ ื‘ื”ื™ืงืฃ: ืขืœื™ื™ื” ื—ืœืงื™ ื”ื–ื–ื” ืื•ืคืงื™ืช.",
            "ื”ื™ืฉืจ ืฉืžื—ื‘ืจ ืฉืชื™ ื ืงื•ื“ื•ืช ืžื•ื’ื“ืจ ืœื—ืœื•ื˜ื™ืŸ ืขืœ ื™ื“ื™ ื”ืฉื™ืคื•ืข ืฉืœื• ื•ื ืงื•ื“ืช ืžืขื‘ืจ ืื—ืช.",
        ],
        bridges=[
            "ืœืื—ืจ ื—ื™ืฉื•ื‘ ื”ืฉื™ืคื•ืข, ืžืฉื•ื•ืืช ื”ื™ืฉืจ ื”ื™ื:",
            "ื›ืฉื ื•ืกื™ืฃ ืืช ื”ืฉื™ืคื•ืข ืฉืžืฆืื ื• ืœื ื•ืกื—ืช ื”ื™ืฉืจ, ื ืงื‘ืœ:",
            "ื”ื™ืฉืจ ืฉืžื—ื‘ืจ ืžืจื›ื– ื”ืžืขื’ืœ ืœืจืืฉื™ืช ื”ืฆื™ืจื™ื ื”ื•ื:",
        ],
        analogies=[
            "ืฉื™ืคื•ืข ื”ื•ื ื›ืžื• ืžื“ืจื’ื•ืช โ€” ืื ืขื•ืœื™ื ืืจื‘ืข ืงื•ืžื•ืช ืœื›ืœ ืฉืœื•ืฉื” ืฆืขื“ื™ื ืงื“ื™ืžื”, ื”ืฉื™ืคื•ืข ื”ื•ื ืืจื‘ืขื” ื—ืœืงื™ ืฉืœื•ืฉื”.",
            "ื“ืžื™ื™ืŸ ื™ืฉืจ ื›ืžื• ื“ืจืš ื‘ื™ืŸ ืฉืชื™ ืขืจื™ื โ€” ื”ืฉื™ืคื•ืข ื”ื•ื ื›ืžื” ืžื˜ืจื™ื ืขื•ืœื™ื ืœื›ืœ ืงื™ืœื•ืžื˜ืจ ืฉื ื•ืกืขื™ื.",
        ],
        closing="ืงื™ื‘ืœื ื• ืืช ืžืฉื•ื•ืืช ื”ื™ืฉืจ ื‘ืฆื•ืจื” ื”ืžืคื•ืจืฉืช โ€” ืงื• ื™ืฉืจ ืฉืขื•ื‘ืจ ื‘ื“ื™ื•ืง ื“ืจืš ืฉืชื™ ื”ื ืงื•ื“ื•ืช ื”ื ืชื•ื ื•ืช.",
    ),

    "CALCULATE_DISTANCE": SemanticEntry(
        concept_tag="CALCULATE_DISTANCE",
        openings=[
            "ื›ื“ื™ ืœื‘ื“ื•ืง ื”ืื ื ืงื•ื“ื” ื ืžืฆืืช ืขืœ ื”ืžืขื’ืœ, ื ืžื“ื•ื“ ืืช ื”ืžืจื—ืง ื‘ื™ื ื” ืœื‘ื™ืŸ ืžืจื›ื– ื”ืžืขื’ืœ ื•ื ืฉื•ื•ื” ืœืจื“ื™ื•ืก.",
            "ื ื•ืกื—ืช ื”ืžืจื—ืง ื‘ื™ืŸ ืฉืชื™ ื ืงื•ื“ื•ืช ื”ื™ื ืฉื•ืจืฉ ืฉืœ ืกื›ื•ื ืจื™ื‘ื•ืขื™ ื”ื”ืคืจืฉื™ื โ€” ื‘ื“ื™ื•ืง ื›ืžื• ืžืฉืคื˜ ืคื™ืชื’ื•ืจืก.",
            "ื”ืฉืืœื” ืคืฉื•ื˜ื”: ื”ืื ื”ื ืงื•ื“ื” ื ืžืฆืืช ื‘ื“ื™ื•ืง ื‘ืจื“ื™ื•ืก ืžื”ืžืจื›ื–, ืงืจื•ื‘ ื™ื•ืชืจ, ืื• ืจื—ื•ืง ื™ื•ืชืจ?",
        ],
        bridges=[
            "ืœืื—ืจ ื—ื™ืฉื•ื‘ ื”ืžืจื—ืง, ื”ืžืžืฆื ื”ื•ื:",
            "ื›ืฉืžืฉื•ื•ื™ื ืืช ื”ืžืจื—ืง ืฉืžืฆืื ื• ืœืจื“ื™ื•ืก ื”ืžืขื’ืœ, ืžืชื‘ืจืจ ืฉ:",
            "ืชื•ืฆืืช ื—ื™ืฉื•ื‘ ื”ืžืจื—ืง ืžื•ืœ ื”ืจื“ื™ื•ืก:",
        ],
        analogies=[
            "ื“ืžื™ื™ืŸ ืžื“ื•ื–ื” ืขื’ื•ืœื” โ€” ื›ืœ ื ืงื•ื“ื” ืขืœ ื’ื‘ื” ื ืžืฆืืช ื‘ื“ื™ื•ืง ื‘ืื•ืชื• ืžืจื—ืง ืžื”ืžืจื›ื–. ืื ื ืงื•ื“ื” ืงืจื•ื‘ื” ื™ื•ืชืจ โ€” ื”ื™ื ื‘ืชื•ื›ื”. ืจื—ื•ืงื” ื™ื•ืชืจ โ€” ืžื—ื•ืฆื” ืœื”.",
            "ืžืจื—ืง ืฉืชื™ ื ืงื•ื“ื•ืช ื–ื” ื›ืžื• ืœืžื“ื•ื“ ืขื ืกืจื’ืœ ื‘ื™ืŸ ืฉืชื™ ืขืจื™ื ืขืœ ืžืคื” โ€” ืคื™ืชื’ื•ืจืก ืขื•ืฉื” ืืช ื”ืขื‘ื•ื“ื”.",
        ],
        closing="ื‘ื“ืงื ื• ืžื“ื•ื™ืง: ื”ืžืจื—ืง ื—ื•ืฉื‘, ื”ื•ืฉื•ื•ื” ืœืจื“ื™ื•ืก, ื•ืžื™ืงื•ื ื”ื ืงื•ื“ื” ื ืงื‘ืข ื‘ื•ื•ื“ืื•ืช ืžืชืžื˜ื™ืช.",
    ),

}


# โ”€โ”€โ”€ Diversity Engine โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

class DiversityEngine:
    """
    Selects narrative variants from the SemanticBank with diversity enforcement.

    Tracks selection history per concept to detect "narrative laziness" (Drift):
    if one variant is chosen more than DRIFT_ALERT_THRESHOLD fraction of the time,
    emit a PEDAGOGICAL_DRIFT telemetry alert.
    """

    def __init__(self):
        # Counter per concept_tag: tracks how many times each (variant_type, index) was picked
        self._history: Dict[str, Counter] = {}

    def _record(self, concept_tag: str, variant_key: str):
        if concept_tag not in self._history:
            self._history[concept_tag] = Counter()
        self._history[concept_tag][variant_key] += 1

    def drift_score(self, concept_tag: str) -> float:
        """
        Returns the dominance fraction of the most-selected variant for this concept.
        Score = max_count / total_count.
        0.0 = perfectly uniform, 1.0 = always the same variant.
        """
        if concept_tag not in self._history or not self._history[concept_tag]:
            return 0.0
        counts = self._history[concept_tag]
        total = sum(counts.values())
        if total == 0:
            return 0.0
        return max(counts.values()) / total

    def _pick(self, concept_tag: str, variant_type: str, options: List[str]) -> str:
        """
        Weighted random selection that avoids the most recently over-used variants.
        Falls back to uniform random if history is thin (< 5 picks).
        """
        n = len(options)
        history_key_prefix = f"{variant_type}:"
        if concept_tag not in self._history or sum(self._history[concept_tag].values()) < 5:
            # Not enough history โ€” uniform random
            idx = random.randrange(n)
        else:
            counts = self._history[concept_tag]
            # Weight = inverse of how often we've picked this index
            weights = []
            for i in range(n):
                pick_count = counts.get(f"{history_key_prefix}{i}", 0)
                weights.append(1.0 / (1 + pick_count))
            # Weighted choice
            total_w = sum(weights)
            r = random.uniform(0, total_w)
            cumulative = 0.0
            idx = n - 1  # fallback
            for i, w in enumerate(weights):
                cumulative += w
                if r <= cumulative:
                    idx = i
                    break

        key = f"{history_key_prefix}{idx}"
        self._record(concept_tag, key)

        # Check drift and emit telemetry
        score = self.drift_score(concept_tag)
        if score > DRIFT_ALERT_THRESHOLD:
            logger.warning(
                f"[DRIFT_MONITOR] Pedagogical drift detected for '{concept_tag}': "
                f"drift_score={score:.2f} > threshold={DRIFT_ALERT_THRESHOLD}. "
                f"Selection history: {dict(self._history[concept_tag])}"
            )
            telemetry.emit_pedagogical_drift(concept_tag, score)

        return options[idx]

    def compose(self, action: str, signed_steps: list) -> str:
        """
        Main entry point: builds a full Hebrew pedagogical narrative for the given action.
        Inserts {{step_id}} placeholders where the server results will be shown.

        Returns a string with {{placeholder}} notation (not yet injected).
        """
        entry = SEMANTIC_BANK.get(action)
        if entry is None:
            logger.warning(
                f"[SEMANTIC_BANK] No entry for action '{action}'. "
                f"Falling back to LLM #2 renderer."
            )
            return None  # Signal to caller: use LLM fallback

        concept_tag = entry.concept_tag

        # Pick diverse variants
        opening  = self._pick(concept_tag, "opening",  entry.openings)
        bridge   = self._pick(concept_tag, "bridge",   entry.bridges)
        analogy  = self._pick(concept_tag, "analogy",  entry.analogies)
        closing  = entry.closing  # Always the same (one closing per concept by design)

        # Build placeholder string from signed steps
        if not signed_steps:
            placeholder_str = ""
        elif len(signed_steps) == 1:
            placeholder_str = f"{{{{{signed_steps[0]['id']}}}}}"
        else:
            parts = [f"{{{{{s['id']}}}}}" for s in signed_steps]
            placeholder_str = " โ† ".join(parts)

        # Compose the narrative (V7.2.5: no emojis/em-dashes โ€” must pass scan_for_math_leakage)
        narrative = (
            f"{opening}\n\n"
            f"{analogy}\n\n"
            f"{bridge} {placeholder_str}\n\n"
            f"{closing}"
        )

        logger.info(
            f"[SEMANTIC_BANK] Composed narrative for '{action}' | "
            f"drift_score={self.drift_score(concept_tag):.2f}"
        )
        return narrative


# Module-level singleton โ€” shared across all requests in the process
_engine = DiversityEngine()

def get_diversity_engine() -> DiversityEngine:
    """Returns the process-level DiversityEngine singleton."""
    return _engine