forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_open_obligations.py
More file actions
440 lines (378 loc) · 16.5 KB
/
Copy pathfirst_open_obligations.py
File metadata and controls
440 lines (378 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
"""Durable, owner-generation-fenced first-open conversation obligations."""
from __future__ import annotations
import uuid
from collections.abc import Mapping
from datetime import datetime, timedelta, timezone
from typing import TYPE_CHECKING, Any, Optional
from google.cloud import firestore
from google.cloud.firestore_v1 import FieldFilter
from database._client import get_firestore_client, run_transactional
from database.account_deletion_policy import account_deletion_blocks_access, normalize_account_deletion_status
from database.firestore_index_registry import FIRST_OPEN_FOLDER_CONVERSATION_COUNT_QUERY
from database.read_boundary import parse_snapshot_strict
if TYPE_CHECKING:
from models.memory_apply import MemoryControlState
CONVERSATIONS_COLLECTION = 'conversations'
# Automatic goal-progress updates are excluded from the JIT featureset: goals
# change only through explicit user action for JIT-admitted conversations.
# Obligations persisted before that removal may still carry a goal_progress
# effect row; ``_effects`` normalizes to this tuple, so completion never waits
# on the retired effect.
FIRST_OPEN_EFFECTS = ('folder_assignment', 'app_fanout')
def _refs(client: Any, uid: str) -> tuple[Any, Any, Any]:
user = client.collection('users').document(uid)
return (
user,
client.collection('account_deletions').document(uid),
user.collection('memory_state').document('apply_control'),
)
def _memory_control_state_model() -> type[Any]:
"""Load the JIT control model only at the first-open execution boundary.
``database.conversations`` is also imported by narrow legacy query seams
which intentionally stub the rest of the backend model graph. Importing
the control model while that module is merely loaded couples those reads
to JIT execution dependencies they never exercise.
"""
from models.memory_apply import MemoryControlState
return MemoryControlState
def _authority(transaction: Any, client: Any, uid: str) -> Optional[MemoryControlState]:
user_ref, deletion_ref, control_ref = _refs(client, uid)
user = user_ref.get(transaction=transaction)
deletion = deletion_ref.get(transaction=transaction)
control_snapshot = control_ref.get(transaction=transaction)
payload = deletion.to_dict() if deletion.exists else None
status = normalize_account_deletion_status(
marker_exists=deletion.exists,
raw_status=payload.get('wipe_status') if isinstance(payload, Mapping) else None,
)
if not user.exists or account_deletion_blocks_access(status) or not control_snapshot.exists:
return None
try:
control = parse_snapshot_strict(_memory_control_state_model(), control_snapshot)
except Exception:
return None
return control if control.uid == uid else None
def _matches(state: Mapping[str, Any], control: MemoryControlState) -> bool:
return (
state.get('account_generation') == control.account_generation
and state.get('source_generation') == control.source_generation
)
def _effects(state: Mapping[str, Any]) -> dict[str, dict[str, Any]]:
raw = state.get('effects')
if not isinstance(raw, Mapping):
return {effect: {'state': 'pending'} for effect in FIRST_OPEN_EFFECTS}
return {
effect: dict(raw[effect]) if isinstance(raw.get(effect), Mapping) else {'state': 'pending'}
for effect in FIRST_OPEN_EFFECTS
}
def _conversation_ref(client: Any, uid: str, conversation_id: str) -> Any:
return client.collection('users').document(uid).collection(CONVERSATIONS_COLLECTION).document(conversation_id)
def initialize_first_open_work(uid: str, conversation_id: str, *, firestore_client: Any = None) -> bool:
client = firestore_client or get_firestore_client()
ref = _conversation_ref(client, uid, conversation_id)
@firestore.transactional
def initialize(transaction):
snapshot = ref.get(transaction=transaction)
control = _authority(transaction, client, uid)
if not snapshot.exists or control is None:
return False
existing = (snapshot.to_dict() or {}).get('jit_first_open')
if existing is not None:
return isinstance(existing, Mapping) and _matches(existing, control)
transaction.update(
ref,
{
'jit_first_open': {
'version': 1,
'state': 'pending',
'attempt': 0,
'account_generation': control.account_generation,
'source_generation': control.source_generation,
'effects': {effect: {'state': 'pending'} for effect in FIRST_OPEN_EFFECTS},
'updated_at': firestore.SERVER_TIMESTAMP,
}
},
)
return True
return bool(run_transactional(client, initialize))
def _live_effect(snapshot: Any, control: Optional[MemoryControlState], token: str, effect: str) -> bool:
if not snapshot.exists or control is None or effect not in FIRST_OPEN_EFFECTS:
return False
state = (snapshot.to_dict() or {}).get('jit_first_open') or {}
return (
_matches(state, control)
and state.get('state') == 'in_flight'
and state.get('lease_token') == token
and _effects(state)[effect].get('state') != 'complete'
)
def first_open_effect_is_authorized(
uid: str, conversation_id: str, token: str, effect: str, *, firestore_client: Any = None
) -> bool:
client = firestore_client or get_firestore_client()
ref = _conversation_ref(client, uid, conversation_id)
@firestore.transactional
def authorize(transaction):
snapshot = ref.get(transaction=transaction)
return _live_effect(snapshot, _authority(transaction, client, uid), token, effect)
return bool(run_transactional(client, authorize))
def commit_first_open_conversation_patch(
uid: str,
conversation_id: str,
token: str,
effect: str,
patch: Mapping[str, Any],
*,
firestore_client: Any = None,
) -> bool:
if not patch:
return False
client = firestore_client or get_firestore_client()
ref = _conversation_ref(client, uid, conversation_id)
@firestore.transactional
def commit(transaction):
snapshot = ref.get(transaction=transaction)
if not _live_effect(snapshot, _authority(transaction, client, uid), token, effect):
return False
transaction.update(ref, dict(patch))
return True
return bool(run_transactional(client, commit))
def commit_first_open_app_result(
uid: str,
conversation_id: str,
token: str,
app_id: str,
patch: Mapping[str, Any],
*,
firestore_client: Any = None,
) -> bool:
"""Persist an app result and its resumable receipt in one transaction."""
if not app_id or not patch:
return False
client = firestore_client or get_firestore_client()
ref = _conversation_ref(client, uid, conversation_id)
@firestore.transactional
def commit(transaction):
snapshot = ref.get(transaction=transaction)
control = _authority(transaction, client, uid)
if not _live_effect(snapshot, control, token, 'app_fanout'):
return False
row = snapshot.to_dict() or {}
state = row.get('jit_first_open') or {}
effects = _effects(state)
app_effect = effects['app_fanout']
raw_receipts = app_effect.get('app_receipts')
receipts = dict(raw_receipts) if isinstance(raw_receipts, Mapping) else {}
prior = receipts.get(app_id)
receipt = dict(prior) if isinstance(prior, Mapping) else {}
receipts[app_id] = {**receipt, 'result_persisted': True}
effects['app_fanout'] = {**app_effect, 'app_receipts': receipts}
transaction.update(
ref,
{
**dict(patch),
'jit_first_open': {**state, 'effects': effects, 'updated_at': firestore.SERVER_TIMESTAMP},
},
)
return True
return bool(run_transactional(client, commit))
def complete_first_open_effect(
uid: str,
conversation_id: str,
token: str,
effect: str,
*,
conversation_patch: Optional[Mapping[str, Any]] = None,
firestore_client: Any = None,
) -> bool:
if effect not in FIRST_OPEN_EFFECTS:
raise ValueError(f'unknown first-open effect: {effect}')
client = firestore_client or get_firestore_client()
ref = _conversation_ref(client, uid, conversation_id)
@firestore.transactional
def complete(transaction):
snapshot = ref.get(transaction=transaction)
control = _authority(transaction, client, uid)
if not snapshot.exists or control is None:
return False
row = snapshot.to_dict() or {}
state = row.get('jit_first_open') or {}
if not _matches(state, control) or state.get('state') != 'in_flight' or state.get('lease_token') != token:
return False
effects = _effects(state)
if effects[effect].get('state') == 'complete':
return True
if effect == 'app_fanout':
raw_receipts = effects[effect].get('app_receipts')
receipts = raw_receipts if isinstance(raw_receipts, Mapping) else {}
app_results = row.get('apps_results', [])
if not isinstance(app_results, list):
return False
for result in app_results:
if not isinstance(result, Mapping) or not isinstance(result.get('app_id'), str):
return False
receipt = receipts.get(result['app_id'])
if not isinstance(receipt, Mapping) or not (
receipt.get('result_persisted') is True and receipt.get('usage_persisted') is True
):
return False
effects[effect] = {**effects[effect], 'state': 'complete', 'completed_at': firestore.SERVER_TIMESTAMP}
patch: dict[str, Any] = {
'jit_first_open': {**state, 'effects': effects, 'updated_at': firestore.SERVER_TIMESTAMP}
}
if conversation_patch:
patch.update(dict(conversation_patch))
transaction.update(ref, patch)
return True
return bool(run_transactional(client, complete))
def commit_first_open_folder_count(
uid: str, conversation_id: str, token: str, folder_id: str, *, firestore_client: Any = None
) -> bool:
client = firestore_client or get_firestore_client()
user = client.collection('users').document(uid)
query = FIRST_OPEN_FOLDER_CONVERSATION_COUNT_QUERY.build(
user.collection(CONVERSATIONS_COLLECTION),
{'folder_id': folder_id, 'discarded': False},
field_filter_factory=FieldFilter,
)
count = int(query.count().get()[0][0].value or 0)
conversation_ref = _conversation_ref(client, uid, conversation_id)
folder_ref = user.collection('folders').document(folder_id)
@firestore.transactional
def commit(transaction):
conversation = conversation_ref.get(transaction=transaction)
control = _authority(transaction, client, uid)
folder = folder_ref.get(transaction=transaction)
if not folder.exists or not _live_effect(conversation, control, token, 'folder_assignment'):
return False
transaction.update(folder_ref, {'conversation_count': count})
return True
return bool(run_transactional(client, commit))
def commit_first_open_app_usage(
uid: str,
conversation_id: str,
token: str,
app_id: str,
usage_type: str,
*,
firestore_client: Any = None,
) -> bool:
client = firestore_client or get_firestore_client()
conversation_ref = _conversation_ref(client, uid, conversation_id)
plugin_ref = client.collection('plugins_data').document(app_id)
usage_ref = client.collection('plugins').document(app_id).collection('usage_history').document(conversation_id)
@firestore.transactional
def commit(transaction):
conversation = conversation_ref.get(transaction=transaction)
plugin = plugin_ref.get(transaction=transaction)
if not _live_effect(conversation, _authority(transaction, client, uid), token, 'app_fanout'):
return False
row = conversation.to_dict() or {}
state = row.get('jit_first_open') or {}
effects = _effects(state)
app_effect = effects['app_fanout']
raw_receipts = app_effect.get('app_receipts')
receipts = dict(raw_receipts) if isinstance(raw_receipts, Mapping) else {}
prior = receipts.get(app_id)
receipt = dict(prior) if isinstance(prior, Mapping) else {}
if receipt.get('result_persisted') is not True:
return False
if receipt.get('usage_persisted') is True:
return True
if not plugin.exists:
return False
receipts[app_id] = {**receipt, 'usage_persisted': True}
effects['app_fanout'] = {**app_effect, 'app_receipts': receipts}
transaction.set(
usage_ref,
{
'uid': uid,
'memory_id': conversation_id,
'message_id': None,
'timestamp': firestore.SERVER_TIMESTAMP,
'type': usage_type,
},
)
transaction.update(
conversation_ref,
{'jit_first_open': {**state, 'effects': effects, 'updated_at': firestore.SERVER_TIMESTAMP}},
)
return True
return bool(run_transactional(client, commit))
def claim_first_open_work(
uid: str,
conversation_id: str,
*,
lease_seconds: int = 300,
now: Optional[datetime] = None,
firestore_client: Any = None,
) -> Optional[str]:
client = firestore_client or get_firestore_client()
ref = _conversation_ref(client, uid, conversation_id)
current_time = now or datetime.now(timezone.utc)
token = str(uuid.uuid4())
@firestore.transactional
def claim(transaction):
snapshot = ref.get(transaction=transaction)
control = _authority(transaction, client, uid)
state = (snapshot.to_dict() or {}).get('jit_first_open') or {} if snapshot.exists else {}
if not snapshot.exists or control is None or not _matches(state, control) or state.get('state') == 'complete':
return None
expires = state.get('lease_expires_at')
if state.get('state') == 'in_flight' and isinstance(expires, datetime) and expires > current_time:
return None
attempt = state.get('attempt', 0)
attempt = attempt if type(attempt) is int and attempt >= 0 else 0
transaction.update(
ref,
{
'jit_first_open': {
**state,
'version': 1,
'effects': _effects(state),
'state': 'in_flight',
'attempt': attempt + 1,
'lease_token': token,
'lease_expires_at': current_time + timedelta(seconds=max(30, lease_seconds)),
'updated_at': firestore.SERVER_TIMESTAMP,
}
},
)
return token
return run_transactional(client, claim)
def claim_authorized_first_open_work(uid: str, conversation_id: str, source: str | None) -> Optional[str]:
from utils.jit_first_open_policy import outstanding_first_open_work_permitted
if not outstanding_first_open_work_permitted(uid=uid, source=source):
return None
return claim_first_open_work(uid, conversation_id)
def finish_first_open_work(
uid: str,
conversation_id: str,
token: str,
*,
succeeded: bool,
firestore_client: Any = None,
) -> bool:
del succeeded
client = firestore_client or get_firestore_client()
ref = _conversation_ref(client, uid, conversation_id)
@firestore.transactional
def finish(transaction):
snapshot = ref.get(transaction=transaction)
control = _authority(transaction, client, uid)
if not snapshot.exists or control is None:
return False
state = (snapshot.to_dict() or {}).get('jit_first_open') or {}
if not _matches(state, control) or state.get('state') != 'in_flight' or state.get('lease_token') != token:
return False
effects = _effects(state)
next_state = {
**state,
'effects': effects,
'state': 'complete' if all(value.get('state') == 'complete' for value in effects.values()) else 'pending',
'updated_at': firestore.SERVER_TIMESTAMP,
}
next_state.pop('lease_token', None)
next_state.pop('lease_expires_at', None)
transaction.update(ref, {'jit_first_open': next_state})
return True
return bool(run_transactional(client, finish))