forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe_requests.py
More file actions
1395 lines (1260 loc) · 55 KB
/
Copy pathframe_requests.py
File metadata and controls
1395 lines (1260 loc) · 55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Firestore adapter for the additive just-in-time frame-request queue.
All documents live under the authenticated user's namespace. The adapter
stores frame metadata and upload references only; it never accepts or emits
pixel bytes. The pure policy module owns lifecycle and quota rules.
"""
from __future__ import annotations
import hashlib
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from typing import Any, Callable
from google.cloud import firestore
from database._client import get_data_plane_firestore_client
from database.conversations import conversations_collection, prepare_photo_for_write
from database.durable_queue import ProcessOutcome, drain_isolated
from database.firestore_index_registry import (
FRAME_REQUEST_METADATA_EXPIRY_QUERY,
FRAME_VISION_OUTPUT_EXPIRY_QUERY,
)
from database.read_boundary import parse_payload_strict, parse_snapshot_strict
from models.frame_request import (
TERMINAL_FRAME_REQUEST_STATES,
FrameRequest,
FrameRequestCleanupState,
FrameRequestState,
)
from utils.retrieval.frame_request_policy import (
FRAME_REQUEST_MAX_BATCH,
FRAME_REQUEST_MAX_BYTES_PER_DEVICE,
FRAME_REQUEST_MAX_BYTES_PER_CONVERSATION,
FRAME_REQUEST_MAX_TTL_SECONDS,
FRAME_REQUEST_DEDUPE_WINDOW_SECONDS,
FRAME_REQUEST_MAX_ATTACHED_PER_CONVERSATION,
check_device_quota,
is_expired,
request_expiry,
validate_transition,
)
USERS_COLLECTION = "users"
FRAME_REQUESTS_COLLECTION = "frame_requests"
FRAME_UPLOAD_ORPHANS_COLLECTION = "frame_upload_orphans"
FRAME_DELETION_OUTBOX_COLLECTION = "frame_deletion_outbox"
FRAME_VISION_RECEIPTS_COLLECTION = "frame_vision_receipts"
@dataclass(frozen=True)
class FrameCleanupPage:
processed: int
cleaned: int
def _get_client(firestore_client: Any | None) -> Any:
"""Resolve the production client at the call boundary.
Keeping the injectable client keyword-only makes queue policy tests and the
Firestore emulator independent from ambient ADC while avoiding construction
of a customer-data client during module import.
"""
return firestore_client if firestore_client is not None else get_data_plane_firestore_client()
def _collection(uid: str, *, firestore_client: Any | None = None) -> Any:
owner_uid = uid.strip()
if not owner_uid:
raise ValueError("uid is required")
client = _get_client(firestore_client)
return client.collection(USERS_COLLECTION).document(owner_uid).collection(FRAME_REQUESTS_COLLECTION)
def _orphan_collection(uid: str, *, firestore_client: Any | None = None) -> Any:
owner_uid = uid.strip()
if not owner_uid:
raise ValueError("uid is required")
client = _get_client(firestore_client)
return client.collection(USERS_COLLECTION).document(owner_uid).collection(FRAME_UPLOAD_ORPHANS_COLLECTION)
def _deletion_outbox_collection(uid: str, *, firestore_client: Any | None = None) -> Any:
owner_uid = uid.strip()
if not owner_uid:
raise ValueError("uid is required")
client = _get_client(firestore_client)
return client.collection(USERS_COLLECTION).document(owner_uid).collection(FRAME_DELETION_OUTBOX_COLLECTION)
def _deletion_receipt_id(conversation_id: str, storage_id: str) -> str:
return hashlib.sha256(f"{conversation_id}\0{storage_id}".encode("utf-8")).hexdigest()
def reserve_frame_vision_invocation(
uid: str,
authority_key: str,
*,
request_id: str,
account_generation: int,
now: datetime | None = None,
firestore_client: Any | None = None,
) -> dict[str, Any]:
"""Durably reserve the sole paid invocation for one stable chat turn.
The receipt is written *before* provider egress. An expired lease is an
honest indeterminate outcome, not permission to pay twice after a crash.
"""
if not authority_key.strip():
raise ValueError("stable vision authority is required")
current = _utc(now or datetime.now(timezone.utc))
client = _get_client(firestore_client)
receipt_id = hashlib.sha256(authority_key.encode("utf-8")).hexdigest()
ref = (
client.collection(USERS_COLLECTION)
.document(uid)
.collection(FRAME_VISION_RECEIPTS_COLLECTION)
.document(receipt_id)
)
transaction = client.transaction()
@firestore.transactional
def _reserve(transaction: Any) -> dict[str, Any]:
snapshot = ref.get(transaction=transaction)
if snapshot.exists:
row = snapshot.to_dict() or {}
if row.get("request_id") != request_id or row.get("account_generation") != account_generation:
raise PermissionError("vision receipt authority mismatch")
return row
row = {
"request_id": request_id,
"account_generation": account_generation,
"state": "invoked",
"created_at": current,
"lease_expires_at": current + timedelta(minutes=5),
}
transaction.create(ref, row)
return {**row, "reserved": True}
return _reserve(transaction)
def complete_frame_vision_invocation(
uid: str,
authority_key: str,
*,
request_id: str,
account_generation: int,
description: str,
now: datetime | None = None,
firestore_client: Any | None = None,
) -> None:
"""Store only the bounded derived result; never pixels or prompt content."""
bounded = description[:4000]
ref = (
_get_client(firestore_client)
.collection(USERS_COLLECTION)
.document(uid)
.collection(FRAME_VISION_RECEIPTS_COLLECTION)
.document(hashlib.sha256(authority_key.encode("utf-8")).hexdigest())
)
snapshot = ref.get()
row = snapshot.to_dict() if snapshot.exists else {}
if row.get("request_id") != request_id or row.get("account_generation") != account_generation:
raise PermissionError("vision receipt authority mismatch")
completed_at = _utc(now or datetime.now(timezone.utc))
ref.update(
{
"state": "completed",
"description": bounded,
"completed_at": completed_at,
"output_expires_at": completed_at + timedelta(seconds=FRAME_REQUEST_MAX_TTL_SECONDS),
}
)
def persist_conversation_frame_deletion_outbox(
uid: str,
conversation_id: str,
storage_ids: list[str],
*,
now: datetime | None = None,
firestore_client: Any | None = None,
) -> None:
"""Durably record every object before its owning conversation is removed."""
if not conversation_id.strip():
raise ValueError("conversation_id is required")
current = _utc(now or datetime.now(timezone.utc))
client = _get_client(firestore_client)
collection = _deletion_outbox_collection(uid, firestore_client=client)
for storage_id in dict.fromkeys(storage_ids):
if not storage_id or "/" in storage_id or "\\" in storage_id:
raise ValueError("invalid frame storage id")
collection.document(_deletion_receipt_id(conversation_id, storage_id)).set(
{
"conversation_id": conversation_id,
"storage_id": storage_id,
"cleanup_next_attempt_at": current,
"cleanup_attempts": 0,
},
merge=True,
)
def acknowledge_conversation_frame_deletion(
uid: str,
conversation_id: str,
storage_id: str,
*,
firestore_client: Any | None = None,
) -> None:
_deletion_outbox_collection(uid, firestore_client=firestore_client).document(
_deletion_receipt_id(conversation_id, storage_id)
).delete()
def cleanup_conversation_frame_deletion_outbox(
uid: str,
*,
delete_storage: Callable[[str], None],
now: datetime | None = None,
limit: int = FRAME_REQUEST_MAX_BATCH,
firestore_client: Any | None = None,
report_page: bool = False,
) -> int | FrameCleanupPage:
"""Retry conversation-owned object deletion after the conversation is gone."""
current = _utc(now or datetime.now(timezone.utc))
query = (
_deletion_outbox_collection(uid, firestore_client=firestore_client)
.where(filter=firestore.FieldFilter("cleanup_next_attempt_at", "<=", current))
.order_by("cleanup_next_attempt_at", direction=firestore.Query.ASCENDING)
.limit(limit)
)
snapshots = list(query.stream())
processed = cleaned = 0
def process_one(snapshot: Any) -> ProcessOutcome:
nonlocal processed, cleaned
row = snapshot.to_dict() or {}
storage_id = row.get("storage_id")
attempts = max(0, int(row.get("cleanup_attempts") or 0))
processed += 1
if not isinstance(storage_id, str) or not storage_id or "/" in storage_id or "\\" in storage_id:
snapshot.reference.delete()
return ProcessOutcome.reject("invalid frame storage id", reason="malformed")
try:
delete_storage(storage_id)
except Exception as exc:
snapshot.reference.update(
{
"cleanup_attempts": attempts + 1,
"cleanup_next_attempt_at": current + timedelta(seconds=min(86400, 2 ** min(attempts, 16))),
"last_error_text": str(exc)[:2000],
}
)
return ProcessOutcome.retry(str(exc), reason="storage_delete_failed")
snapshot.reference.delete()
cleaned += 1
return ProcessOutcome.ack()
drain_isolated(snapshots, process_one)
page = FrameCleanupPage(processed=processed, cleaned=cleaned)
return page if report_page else page.cleaned
def _request_from_snapshot(snapshot: Any) -> FrameRequest:
return parse_snapshot_strict(FrameRequest, snapshot, document_id_field="request_id")
def _request_from_payload(payload: dict[str, Any], *, document_path: str) -> FrameRequest:
return parse_payload_strict(FrameRequest, payload, document_path=document_path)
def _request_path(uid: str, request_id: str) -> str:
return f"{USERS_COLLECTION}/{uid}/{FRAME_REQUESTS_COLLECTION}/{request_id}"
def _request_data(request: FrameRequest) -> dict[str, Any]:
return request.model_dump(mode="python", exclude_none=True)
def get_frame_request(uid: str, request_id: str, *, firestore_client: Any | None = None) -> FrameRequest:
"""Read one owner-scoped queue row without exposing another account."""
snapshot = _collection(uid, firestore_client=firestore_client).document(request_id).get()
if not snapshot.exists:
raise KeyError("frame request not found")
return _request_from_snapshot(snapshot)
def list_attached_frame_requests(
uid: str,
conversation_id: str,
*,
firestore_client: Any | None = None,
limit: int = 2,
) -> list[FrameRequest]:
"""Return the bounded set used to enforce one permanent keyframe."""
if not conversation_id.strip() or not 1 <= limit <= 2:
raise ValueError("invalid conversation frame-request lookup")
query = (
_collection(uid, firestore_client=firestore_client)
.where(filter=firestore.FieldFilter("conversation_id", "==", conversation_id))
.where(filter=firestore.FieldFilter("state", "==", FrameRequestState.attached.value))
.limit(limit)
)
return [_request_from_snapshot(snapshot) for snapshot in query.stream()]
def attach_frame_request_to_conversation(
uid: str,
request_id: str,
*,
device_id: str,
account_generation: int,
conversation_id: str,
permanent_storage_id: str,
now: datetime | None = None,
firestore_client: Any | None = None,
) -> FrameRequest:
"""Atomically attach one uploaded frame and its permanent photo metadata.
The row, photo subcollection document, conversation marker, and one-photo
invariant are committed in a single transaction. A retry after a committed
response/transport ambiguity returns the already-attached row without
touching the permanent object.
"""
if not conversation_id.strip() or not permanent_storage_id.strip():
raise ValueError("conversation_id and permanent_storage_id are required")
current_time = _utc(now or datetime.now(timezone.utc))
client = _get_client(firestore_client)
ref = _collection(uid, firestore_client=client).document(request_id)
conversation_ref = (
client.collection(USERS_COLLECTION)
.document(uid.strip())
.collection(conversations_collection)
.document(conversation_id)
)
photos_ref = conversation_ref.collection("photos")
photo_ref = photos_ref.document(request_id)
promotion_receipt_ref = _orphan_collection(uid, firestore_client=client).document(
hashlib.sha256(permanent_storage_id.encode("utf-8")).hexdigest()
)
transaction = client.transaction()
@firestore.transactional
def _attach(transaction: Any) -> dict[str, Any]:
snapshot = ref.get(transaction=transaction)
if not snapshot.exists:
raise KeyError("frame request not found")
request = _request_from_snapshot(snapshot)
if request.uid != uid or request.device_id != device_id or request.account_generation != account_generation:
raise PermissionError("frame request owner or account generation mismatch")
if request.conversation_id != conversation_id:
raise PermissionError("conversation ownership mismatch")
if request.state == FrameRequestState.attached:
if request.storage_id != permanent_storage_id:
raise ValueError("attached frame uses a different permanent object")
# A concurrent caller may have reserved the same deterministic copy
# receipt after the winner committed. Clear it transactionally so
# cleanup can never delete referenced permanent evidence.
transaction.delete(promotion_receipt_ref)
return _request_data(request)
if request.state != FrameRequestState.uploaded or not request.storage_id:
raise ValueError("only uploaded frame requests may be promoted")
conversation_snapshot = conversation_ref.get(transaction=transaction)
if not conversation_snapshot.exists:
raise KeyError("conversation not found")
# Firestore reads in this transaction establish a contention fence for
# all attached rows in the conversation; a concurrent winner retries
# this transaction and is then observed as the existing row above.
attached_rows = [
_request_from_snapshot(item)
for item in _collection(uid, firestore_client=client)
.where(filter=firestore.FieldFilter("conversation_id", "==", conversation_id))
.where(filter=firestore.FieldFilter("state", "==", FrameRequestState.attached.value))
.limit(FRAME_REQUEST_MAX_ATTACHED_PER_CONVERSATION + 1)
.stream(transaction=transaction)
]
if any(item.request_id != request_id for item in attached_rows):
raise ValueError("conversation already has permanent frame evidence")
if (
sum(item.byte_count for item in attached_rows) + request.byte_count
> FRAME_REQUEST_MAX_BYTES_PER_CONVERSATION
):
raise ValueError("conversation frame evidence byte budget exceeded")
existing_photo = photo_ref.get(transaction=transaction)
if existing_photo.exists:
existing_storage_id = (existing_photo.to_dict() or {}).get("storage_id")
if existing_storage_id != permanent_storage_id:
raise ValueError("conversation photo id is already used")
else:
level = (conversation_snapshot.to_dict() or {}).get("data_protection_level", "standard")
photo_data = prepare_photo_for_write(
{
"id": request.request_id,
"base64": "",
"storage_id": permanent_storage_id,
"content_type": request.content_type,
"description": "Just-in-time frame evidence",
"discarded": False,
"created_at": request.created_at,
},
uid,
level,
)
transaction.set(photo_ref, photo_data)
transaction.update(conversation_ref, {"has_content": True, "has_photos": True})
candidate = _request_from_payload(
{
**_request_data(request),
"state": FrameRequestState.attached.value,
"attached_at": current_time,
"expires_at": request.created_at,
"cleanup_state": FrameRequestCleanupState.permanent.value,
"cleanup_next_attempt_at": None,
"storage_id": permanent_storage_id,
},
document_path=_request_path(uid, request_id),
)
transaction.update(ref, _request_data(candidate))
transaction.delete(promotion_receipt_ref)
return _request_data(candidate)
return _request_from_payload(_attach(transaction), document_path=_request_path(uid, request_id))
def reserve_frame_promotion_copy(
uid: str,
request_id: str,
permanent_storage_id: str,
*,
now: datetime | None = None,
firestore_client: Any | None = None,
) -> None:
"""Persist a cleanup receipt before copying into the no-expiry bucket.
Successful attachment deletes the receipt in the same transaction as the
permanent reference. A failed/ambiguous promotion therefore leaves either
a referenced permanent object or a discoverable object due for cleanup.
"""
current = _utc(now or datetime.now(timezone.utc))
if not request_id.strip() or not permanent_storage_id.strip():
raise ValueError("promotion receipt identities are required")
receipt = _orphan_collection(uid, firestore_client=firestore_client).document(
hashlib.sha256(permanent_storage_id.encode("utf-8")).hexdigest()
)
receipt.set(
{
"storage_id": permanent_storage_id,
"request_id": request_id,
"cleanup_state": FrameRequestCleanupState.pending.value,
"cleanup_attempts": 0,
# Leave ample time for copy + Firestore transaction retries while
# remaining far inside the temporary retention deadline.
"cleanup_next_attempt_at": current + timedelta(hours=1),
"created_at": current,
"kind": "promotion_copy",
},
merge=False,
)
def reserve_frame_storage_cleanup(
uid: str,
request_id: str,
storage_id: str,
*,
now: datetime | None = None,
firestore_client: Any | None = None,
) -> None:
"""Persist an independently retryable cleanup receipt for a displaced object."""
current = _utc(now or datetime.now(timezone.utc))
receipt = _orphan_collection(uid, firestore_client=firestore_client).document(
hashlib.sha256(storage_id.encode("utf-8")).hexdigest()
)
receipt.set(
{
"storage_id": storage_id,
"request_id": request_id,
"cleanup_state": FrameRequestCleanupState.pending.value,
"cleanup_attempts": 0,
"cleanup_next_attempt_at": current + timedelta(hours=1),
"created_at": current,
"kind": "displaced_temporary",
},
merge=False,
)
def acknowledge_frame_storage_cleanup(
uid: str,
storage_id: str,
*,
firestore_client: Any | None = None,
) -> None:
_orphan_collection(uid, firestore_client=firestore_client).document(
hashlib.sha256(storage_id.encode("utf-8")).hexdigest()
).delete()
def enqueue_frame_request(
uid: str,
*,
device_id: str,
dedupe_key: str,
conversation_id: str | None = None,
screenshot_id: str | None = None,
account_generation: int = 0,
requested_ttl_seconds: int | None = None,
device_retention_seconds: int | None = None,
now: datetime | None = None,
firestore_client: Any | None = None,
) -> tuple[FrameRequest, bool]:
"""Create one idempotent request; return ``(request, deduplicated)``.
The dedupe key is the document id, so retries need one transactional read
and cannot enqueue duplicate work even when two requests race.
"""
owner_uid = uid.strip()
owner_device = device_id.strip()
stable_dedupe_key = dedupe_key.strip()
if not owner_uid or not owner_device or not stable_dedupe_key:
raise ValueError("uid, device_id, and dedupe_key are required")
if account_generation < 0:
raise ValueError("account_generation must be nonnegative")
created = _utc(now or datetime.now(timezone.utc))
dedupe_window = int(created.timestamp()) // FRAME_REQUEST_DEDUPE_WINDOW_SECONDS
# Never persist caller wording. Include the target identities in the
# opaque key so a reused intent cannot collapse different screenshots or
# conversations into one attempt.
dedupe_identity = hashlib.sha256(
"\x00".join((stable_dedupe_key, conversation_id or "", screenshot_id or "")).encode("utf-8")
).hexdigest()
expires = request_expiry(
created_at=created,
requested_ttl_seconds=requested_ttl_seconds,
device_retention_seconds=device_retention_seconds,
)
client = _get_client(firestore_client)
collection = _collection(owner_uid, firestore_client=client)
transaction = client.transaction()
@firestore.transactional
def _create(transaction: Any) -> tuple[dict[str, Any], bool]:
# Dedupe is scoped to the account generation and the full active
# logical-request lifetime. ``dedupe_window`` remains an observability
# bucket/index hint, never an expiry boundary for active work.
attempts = [
_request_from_snapshot(item)
for item in collection.where(filter=firestore.FieldFilter("device_id", "==", owner_device))
.where(filter=firestore.FieldFilter("account_generation", "==", account_generation))
.where(filter=firestore.FieldFilter("dedupe_key", "==", dedupe_identity))
.order_by("attempt_number", direction=firestore.Query.DESCENDING)
.limit(FRAME_REQUEST_MAX_BATCH)
.stream(transaction=transaction)
]
for existing in attempts:
if existing.state not in {
FrameRequestState.requested,
FrameRequestState.claimed,
FrameRequestState.uploaded,
}:
continue
if not is_expired(existing, now=created):
return _request_data(existing), True
if conversation_id:
conversation_rows = [
_request_from_snapshot(item)
for item in collection.where(filter=firestore.FieldFilter("conversation_id", "==", conversation_id))
.where(
filter=firestore.FieldFilter(
"state",
"in",
[
FrameRequestState.requested.value,
FrameRequestState.claimed.value,
FrameRequestState.uploaded.value,
FrameRequestState.attached.value,
],
)
)
.limit(FRAME_REQUEST_MAX_ATTACHED_PER_CONVERSATION + 1)
.stream(transaction=transaction)
]
if conversation_rows:
raise ValueError("conversation already has an active frame request")
attempt_number = max((item.attempt_number for item in attempts), default=-1) + 1
dedupe_digest = hashlib.sha256(
f"{dedupe_identity}:{account_generation}:{dedupe_window}:{attempt_number}".encode("utf-8")
).hexdigest()
request_id = f"frame-{dedupe_digest}"
request = FrameRequest(
request_id=request_id,
uid=owner_uid,
device_id=owner_device,
account_generation=account_generation,
dedupe_key=dedupe_identity,
dedupe_window=dedupe_window,
attempt_number=attempt_number,
conversation_id=conversation_id,
screenshot_id=screenshot_id,
state=FrameRequestState.requested,
created_at=created,
expires_at=expires,
)
existing_rows = [
_request_from_snapshot(item)
for item in collection.where(filter=firestore.FieldFilter("device_id", "==", owner_device))
.where(filter=firestore.FieldFilter("account_generation", "==", account_generation))
.where(
filter=firestore.FieldFilter(
"state",
"in",
[
FrameRequestState.requested.value,
FrameRequestState.claimed.value,
FrameRequestState.uploaded.value,
],
)
)
.limit(FRAME_REQUEST_MAX_BATCH)
.stream(transaction=transaction)
]
quota = check_device_quota(existing_rows, uid=owner_uid, device_id=owner_device, now=created)
if not quota.allowed:
raise ValueError(f"frame request quota exceeded: {quota.reason}")
transaction.create(collection.document(request_id), _request_data(request))
return _request_data(request), False
data, deduplicated = _create(transaction)
return (
_request_from_payload(
data,
document_path=_request_path(owner_uid, str(data["request_id"])),
),
deduplicated,
)
def list_pending_frame_requests(
uid: str,
*,
device_id: str,
account_generation: int,
now: datetime | None = None,
limit: int = FRAME_REQUEST_MAX_BATCH,
firestore_client: Any | None = None,
cleanup_storage: Callable[[str], None] | None = None,
) -> list[FrameRequest]:
"""Return only current-owner requests routed to this exact device."""
if not 1 <= limit <= FRAME_REQUEST_MAX_BATCH:
raise ValueError("limit is outside the bounded frame-request window")
current = _utc(now or datetime.now(timezone.utc))
# Keep stale rows from occupying the bounded delivery window. This is a
# caller-side bounded worker; a scheduled worker can call the same helper.
prune_expired_frame_requests(
uid,
account_generation=account_generation,
now=current,
limit=FRAME_REQUEST_MAX_BATCH,
firestore_client=firestore_client,
# External deletion belongs to the scheduled retention worker. A
# device polling endpoint must not be the only janitor.
cleanup_storage=None,
)
rows: list[FrameRequest] = []
query = (
_collection(uid, firestore_client=firestore_client)
.where(filter=firestore.FieldFilter("device_id", "==", device_id))
.where(filter=firestore.FieldFilter("account_generation", "==", account_generation))
.where(filter=firestore.FieldFilter("state", "==", FrameRequestState.requested.value))
.order_by("created_at", direction=firestore.Query.ASCENDING)
.limit(limit)
)
for snapshot in query.stream():
request = _request_from_snapshot(snapshot)
if is_expired(request, now=current):
continue
rows.append(request)
return rows
def list_recoverable_frame_requests(
uid: str,
*,
device_id: str,
account_generation: int,
now: datetime | None = None,
limit: int = FRAME_REQUEST_MAX_BATCH,
firestore_client: Any | None = None,
) -> list[FrameRequest]:
"""Return requested/claimed/uploaded rows for in-flight device recovery."""
if not 1 <= limit <= FRAME_REQUEST_MAX_BATCH:
raise ValueError("limit is outside the bounded frame-request window")
current = _utc(now or datetime.now(timezone.utc))
query = (
_collection(uid, firestore_client=firestore_client)
.where(filter=firestore.FieldFilter("device_id", "==", device_id))
.where(filter=firestore.FieldFilter("account_generation", "==", account_generation))
.where(
filter=firestore.FieldFilter(
"state",
"in",
[
FrameRequestState.requested.value,
FrameRequestState.claimed.value,
FrameRequestState.uploaded.value,
],
)
)
.order_by("created_at", direction=firestore.Query.ASCENDING)
.limit(limit)
)
return [
request
for snapshot in query.stream()
if not is_expired(request := _request_from_snapshot(snapshot), now=current)
]
def transition_frame_request(
uid: str,
request_id: str,
*,
next_state: FrameRequestState,
device_id: str,
account_generation: int,
terminal_reason: str | None = None,
storage_id: str | None = None,
byte_count: int = 0,
content_type: str | None = None,
now: datetime | None = None,
firestore_client: Any | None = None,
cleanup_storage: Callable[[str], None] | None = None,
) -> FrameRequest:
"""Apply one owner-fenced lifecycle transition transactionally."""
current_time = _utc(now or datetime.now(timezone.utc))
client = _get_client(firestore_client)
ref = _collection(uid, firestore_client=client).document(request_id)
transaction = client.transaction()
@firestore.transactional
def _transition(transaction: Any) -> dict[str, Any]:
snapshot = ref.get(transaction=transaction)
if not snapshot.exists:
raise KeyError("frame request not found")
request = _request_from_snapshot(snapshot)
validate_transition(
request,
next_state=next_state,
uid=uid,
device_id=device_id,
account_generation=account_generation,
now=current_time,
)
if byte_count < 0 or byte_count > 10 * 1024 * 1024:
raise ValueError("frame upload exceeds the bounded byte limit")
if next_state != FrameRequestState.uploaded and byte_count:
raise ValueError("byte_count is only valid when uploading a frame")
if next_state != FrameRequestState.uploaded and (storage_id or content_type):
raise ValueError("storage metadata is only valid when uploading a frame")
if next_state == FrameRequestState.uploaded and not storage_id:
raise ValueError("uploaded frame requests require storage_id")
if next_state in TERMINAL_FRAME_REQUEST_STATES - {FrameRequestState.attached} and not terminal_reason:
raise ValueError("terminal frame requests require a bounded reason")
if next_state not in TERMINAL_FRAME_REQUEST_STATES and terminal_reason:
raise ValueError("active frame requests must not carry a terminal reason")
if next_state == FrameRequestState.attached and not request.conversation_id:
raise ValueError("only conversation-bound requests may be attached")
if next_state == FrameRequestState.attached and (storage_id or byte_count or content_type):
raise ValueError("attached transition cannot replace upload metadata")
if next_state == FrameRequestState.uploaded:
# Byte quota is enforced at the upload transition, after the
# object has been authenticated and before metadata is committed.
active_rows = [
_request_from_snapshot(item)
for item in _collection(uid, firestore_client=client)
.where(filter=firestore.FieldFilter("device_id", "==", device_id))
.where(filter=firestore.FieldFilter("account_generation", "==", account_generation))
.where(
filter=firestore.FieldFilter(
"state",
"in",
[
FrameRequestState.requested.value,
FrameRequestState.claimed.value,
FrameRequestState.uploaded.value,
],
)
)
# Sum the full bounded active set; using only the pending-count
# cap would let old uploaded rows evade the byte quota.
.limit(FRAME_REQUEST_MAX_BATCH).stream(transaction=transaction)
]
quota = check_device_quota(
active_rows,
uid=uid,
device_id=device_id,
now=current_time,
additional_bytes=byte_count,
)
if quota.pending_bytes + byte_count > FRAME_REQUEST_MAX_BYTES_PER_DEVICE:
raise ValueError("frame request quota exceeded: pending_bytes")
update: dict[str, Any] = {
"state": next_state.value,
"terminal_reason": terminal_reason,
}
if next_state == FrameRequestState.claimed:
update["claimed_at"] = current_time
if next_state == FrameRequestState.uploaded:
update.update(
{
"uploaded_at": current_time,
"storage_id": storage_id,
"byte_count": byte_count,
"content_type": content_type,
}
)
if next_state == FrameRequestState.attached:
update["attached_at"] = current_time
# Attached conversation evidence follows conversation lifetime; do
# not leave a temporary TTL for a cleanup worker to delete.
update["expires_at"] = request.created_at
update["cleanup_state"] = FrameRequestCleanupState.permanent.value
elif request.storage_id and next_state in TERMINAL_FRAME_REQUEST_STATES:
# Terminal metadata must not imply that the external object was
# deleted. A scheduled worker retries this independently.
update["cleanup_state"] = FrameRequestCleanupState.pending.value
update["cleanup_next_attempt_at"] = current_time
elif next_state == FrameRequestState.uploaded:
update["cleanup_state"] = FrameRequestCleanupState.pending.value
update["cleanup_next_attempt_at"] = current_time
candidate = _request_from_payload(
{**_request_data(request), **update},
document_path=_request_path(uid, request_id),
)
transaction.update(ref, _request_data(candidate))
return _request_data(candidate)
return _request_from_payload(_transition(transaction), document_path=_request_path(uid, request_id))
def reconcile_ambiguous_frame_upload(
uid: str,
request_id: str,
*,
device_id: str,
account_generation: int,
storage_id: str,
byte_count: int,
content_type: str | None,
now: datetime | None = None,
firestore_client: Any | None = None,
) -> FrameRequest | None:
"""Resolve an uncertain upload commit without losing an object reference.
If the upload transition committed, return the uploaded/attached row. If a
strongly consistent read does not find that exact reference, persist an
independent owner-scoped orphan receipt for the newly uploaded object. The
existing request reference is never overwritten, and the object is never
deleted in this ambiguity path.
"""
current_time = _utc(now or datetime.now(timezone.utc))
client = _get_client(firestore_client)
ref = _collection(uid, firestore_client=client).document(request_id)
orphan_ref = _orphan_collection(uid, firestore_client=client).document(
hashlib.sha256(storage_id.encode("utf-8")).hexdigest()
)
transaction = client.transaction()
@firestore.transactional
def _reconcile(transaction: Any) -> dict[str, Any] | None:
snapshot = ref.get(transaction=transaction)
orphan_snapshot = orphan_ref.get(transaction=transaction)
orphan_data = {
"storage_id": storage_id,
"request_id": request_id,
"cleanup_state": FrameRequestCleanupState.pending.value,
"cleanup_attempts": 0,
"cleanup_next_attempt_at": current_time,
"created_at": current_time,
}
def ensure_orphan_receipt() -> None:
# A repeated ambiguity reconciliation must not reset a receipt
# that the cleanup worker already terminalized.
if not orphan_snapshot.exists:
transaction.set(orphan_ref, orphan_data)
if not snapshot.exists:
ensure_orphan_receipt()
return None
request = _request_from_snapshot(snapshot)
if request.uid != uid or request.device_id != device_id or request.account_generation != account_generation:
ensure_orphan_receipt()
return None
if request.storage_id == storage_id:
return _request_data(request)
ensure_orphan_receipt()
if request.state in TERMINAL_FRAME_REQUEST_STATES or request.storage_id:
return _request_data(request)
candidate = _request_from_payload(
{
**_request_data(request),
"state": FrameRequestState.failed.value,
"terminal_reason": "upload_commit_ambiguous",
},
document_path=_request_path(uid, request_id),
)
transaction.update(ref, _request_data(candidate))
return _request_data(candidate)
result = _reconcile(transaction)
return _request_from_payload(result, document_path=_request_path(uid, request_id)) if result else None
def cleanup_ambiguous_frame_upload_pixels(
uid: str,
*,
delete_storage: Callable[[str], None],
now: datetime | None = None,
limit: int = FRAME_REQUEST_MAX_BATCH,
firestore_client: Any | None = None,
report_page: bool = False,
) -> int | FrameCleanupPage:
"""Converge durable ambiguous-upload receipts independently of request rows."""
if not 1 <= limit <= FRAME_REQUEST_MAX_BATCH:
raise ValueError("limit is outside the bounded frame-request window")
current = _utc(now or datetime.now(timezone.utc))
query = (
_orphan_collection(uid, firestore_client=firestore_client)
.where(filter=firestore.FieldFilter("cleanup_next_attempt_at", "<=", current))
.order_by("cleanup_next_attempt_at", direction=firestore.Query.ASCENDING)
.limit(limit)
)
processed = cleaned = 0
for snapshot in query.stream():
row = snapshot.to_dict() or {}
storage_id = row.get("storage_id")
cleanup_state = row.get("cleanup_state")
if (
not isinstance(storage_id, str)
or not storage_id.strip()
or "/" in storage_id
or "\\" in storage_id
or cleanup_state not in {FrameRequestCleanupState.pending.value, FrameRequestCleanupState.failed.value}
):
snapshot.reference.delete()
processed += 1
continue
processed += 1
attempts = max(0, int(row.get("cleanup_attempts") or 0))
try:
delete_storage(storage_id)
except Exception:
retry_at = current + timedelta(seconds=min(24 * 60 * 60, 2 ** min(attempts, 16)))
snapshot.reference.update(
{
"cleanup_state": FrameRequestCleanupState.failed.value,
"cleanup_attempts": attempts + 1,
"cleanup_next_attempt_at": retry_at,
}
)
continue
snapshot.reference.update(
{
"cleanup_state": FrameRequestCleanupState.deleted.value,
"cleanup_attempts": attempts + 1,
"cleanup_next_attempt_at": None,
}
)
cleaned += 1
page = FrameCleanupPage(processed=processed, cleaned=cleaned)
return page if report_page else page.cleaned
def prune_expired_frame_requests(
uid: str,
*,
account_generation: int | None = None,
now: datetime | None = None,
limit: int = FRAME_REQUEST_MAX_BATCH,
firestore_client: Any | None = None,