-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop_priority.py
More file actions
294 lines (268 loc) · 9.52 KB
/
loop_priority.py
File metadata and controls
294 lines (268 loc) · 9.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from datetime import datetime as dt
from config import settings
from db.database import get_conn
from thesis import get_current_thesis
from training.summary import get_training_summary
def _days_since(value: str | None) -> int:
if not value:
return 9999
try:
return max(0, (dt.utcnow() - dt.fromisoformat(value)).days)
except Exception:
return 9999
def _hours_since(value: str | None) -> float:
if not value:
return 999999.0
try:
return max(0.0, (dt.utcnow() - dt.fromisoformat(value)).total_seconds() / 3600)
except Exception:
return 999999.0
def _card(
kind: str,
title: str,
body: str,
action_type: str,
action_label: str,
*,
evidence_count: int = 0,
confidence: str = "emerging",
subject_type: str | None = None,
subject_id: str | None = None,
payload: dict | None = None,
) -> dict:
return {
"kind": kind,
"title": title,
"body": body,
"evidence_count": evidence_count,
"confidence": confidence,
"subject_type": subject_type or kind,
"subject_id": subject_id,
"action": {
"type": action_type,
"label": action_label,
"payload": payload or {},
},
}
async def get_today_priority(user_id: str) -> dict:
summary = await get_training_summary(user_id)
thesis = await get_current_thesis(user_id)
today = dt.utcnow().strftime("%Y-%m-%d")
async with get_conn() as db:
async with db.execute(
"SELECT COUNT(*) as cnt FROM training_pairs WHERE user_id=?",
(user_id,),
) as cur:
total_pairs = (await cur.fetchone())["cnt"]
async with db.execute(
"""
SELECT id, name, topic, first_seen, evidence_count, escalation_level
FROM echo_threads
WHERE user_id=? AND status='active'
ORDER BY escalation_level DESC, evidence_count DESC, last_seen DESC
LIMIT 1
""",
(user_id,),
) as cur:
top_thread = await cur.fetchone()
async with db.execute(
"""
SELECT id, rep_title, observation, rep_instruction
FROM practice_reps
WHERE user_id=? AND date=?
LIMIT 1
""",
(user_id, today),
) as cur:
today_rep = await cur.fetchone()
rep_logged = False
if today_rep:
async with db.execute(
"SELECT done FROM practice_log WHERE user_id=? AND rep_id=?",
(user_id, today_rep["id"]),
) as cur:
rep_logged = await cur.fetchone() is not None
async with db.execute(
"""
SELECT id, prompt, topic, winning_style, created_at
FROM tournament_runs
WHERE user_id=? AND status='complete'
ORDER BY created_at DESC
LIMIT 1
""",
(user_id,),
) as cur:
latest_battle = await cur.fetchone()
async with db.execute(
"""
SELECT id, created_at
FROM tournament_runs
WHERE user_id=?
ORDER BY created_at DESC
LIMIT 1
""",
(user_id,),
) as cur:
any_battle = await cur.fetchone()
async with db.execute(
"""
SELECT id, summary, payload_json, created_at
FROM echo_events
WHERE user_id=? AND event_type='onboarding_first_signal'
ORDER BY created_at DESC
LIMIT 1
""",
(user_id,),
) as cur:
first_signal = await cur.fetchone()
if total_pairs < 5:
if first_signal:
payload = {}
try:
import json
payload = json.loads(first_signal["payload_json"] or "{}")
except Exception:
payload = {}
return _card(
"first_read",
first_signal["summary"] or "Echo has a first read.",
payload.get("read") or "Echo has enough of a first signal to start watching a real pattern.",
"none",
"Keep talking",
evidence_count=max(1, total_pairs),
confidence="early",
subject_type="onboarding_first_signal",
subject_id=first_signal["id"],
)
return _card(
"listen",
"Echo is still learning your signal.",
"Talk normally for a little longer. The first useful pattern appears after Echo has enough real moments.",
"none",
"Keep talking",
evidence_count=total_pairs,
confidence="early",
)
if top_thread and top_thread["escalation_level"] >= 4:
days = max(1, _days_since(top_thread["first_seen"]) + 1)
context = (
f"Pattern: \"{top_thread['name']}\" - observed for {days} days "
f"across {top_thread['evidence_count']} messages."
)
return _card(
"council",
"This pattern is ready for the council.",
context,
"open_council",
"Enter council",
evidence_count=top_thread["evidence_count"],
confidence="strong",
subject_type="thread",
subject_id=top_thread["id"],
payload={"thread_id": top_thread["id"], "thread_context": context},
)
if latest_battle and _hours_since(latest_battle["created_at"]) <= 36:
style = latest_battle["winning_style"] or "A shadow"
return _card(
"tournament_result",
f"{style} won your latest battle.",
"That choice is now training signal. Echo is learning which version of you actually helps.",
"run_tournament",
"Send clones again",
evidence_count=max(1, summary["tournament_battles"]),
confidence="emerging",
subject_type="tournament",
subject_id=latest_battle["id"],
)
if top_thread and top_thread["escalation_level"] >= 2:
prompt = (
f"I keep seeing this pattern in myself: {top_thread['name']}. "
"What version of me would handle it best?"
)
return _card(
"thread_tournament",
"Send clones into this pattern.",
f"Echo has seen \"{top_thread['name']}\" {top_thread['evidence_count']} times.",
"run_tournament",
"Send clones",
evidence_count=top_thread["evidence_count"],
confidence="emerging",
subject_type="thread",
subject_id=top_thread["id"],
payload={"prompt": prompt},
)
if (
thesis.get("stage") != "forming"
and float(thesis.get("confidence") or 0.0) < 0.75
and total_pairs >= 8
and (not any_battle or _days_since(any_battle["created_at"]) >= 2)
):
action = thesis.get("next_action") or {}
payload = action.get("payload") if isinstance(action.get("payload"), dict) else {}
return _card(
"thesis_test",
"Test Echo's read with clones.",
thesis.get("statement") or "Echo has a belief worth testing against a real choice.",
"run_tournament",
"Send clones",
evidence_count=int(thesis.get("evidence_count") or 0),
confidence=thesis.get("confidence_label") or "emerging",
subject_type="thesis",
subject_id=thesis.get("id"),
payload=payload,
)
if today_rep and not rep_logged:
return _card(
"practice",
today_rep["rep_title"],
today_rep["observation"] or today_rep["rep_instruction"],
"log_practice",
"Mark done",
evidence_count=1,
confidence="emerging",
subject_type="practice_rep",
subject_id=today_rep["id"],
payload={"rep_id": today_rep["id"]},
)
if summary["ready_for_training"]:
return _card(
"training_ready",
"Your clone has enough new material.",
(
f"{summary['untrained_pairs']} moments are waiting. "
f"{summary['preference_signals']} are direct preference signals."
),
"open_training",
"Update clone",
evidence_count=summary["untrained_pairs"],
confidence="strong",
)
if total_pairs >= 5 and (not any_battle or _days_since(any_battle["created_at"]) >= 3):
return _card(
"tournament",
"Send clones.",
"Bring one real situation. Four versions of you will compete, and your choice teaches Echo.",
"run_tournament",
"Send clones",
evidence_count=total_pairs,
confidence="early",
)
if total_pairs >= settings.min_pairs_for_training:
return _card(
"mirror",
"Your weekly mirror is ready.",
"Echo has enough moments to reflect one pattern back with evidence.",
"open_mirror",
"Open mirror",
evidence_count=total_pairs,
confidence="emerging",
)
return _card(
"signal",
"Echo is collecting your pattern.",
"Keep using Talk or voice. The system is watching for repeated strengths, friction, and response styles.",
"none",
"Keep talking",
evidence_count=total_pairs,
confidence="early",
)