forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_recommendations.py
More file actions
1049 lines (942 loc) · 41.6 KB
/
Copy pathtask_recommendations.py
File metadata and controls
1049 lines (942 loc) · 41.6 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
"""Persistence for attributable task feedback and derived attention projections."""
import hashlib
import json
import logging
from datetime import datetime, timezone
from typing import Any, Optional, cast
from google.cloud import firestore
from google.cloud.firestore_v1 import FieldFilter
from pydantic import ValidationError
from database._client import get_firestore_client
from database.firestore_index_registry import ACTIVE_ATTENTION_OVERRIDE_QUERY
from database.read_boundary import parse_snapshot_or_none, parse_snapshot_strict
from models.task_recommendation import (
DecisionRecord,
FeedbackCreate,
FeedbackRecord,
InterventionCreate,
InterventionRecord,
NormalizedContextSnapshot,
OpenLoopSnapshot,
OutcomeCreate,
OutcomeRecord,
SnapshotReceipt,
WhatMattersNowProjection,
)
from models.task_intelligence import TaskWorkflowControl
from utils.observability.fallback import record_fallback
logger = logging.getLogger(__name__)
FEEDBACK_COLLECTION = 'task_feedback'
OUTCOMES_COLLECTION = 'task_outcomes'
INTERVENTIONS_COLLECTION = 'task_interventions'
ATTENTION_OVERRIDES_COLLECTION = 'task_attention_overrides'
PROJECTIONS_COLLECTION = 'task_recommendation_projections'
DECISIONS_COLLECTION = 'task_recommendation_decisions'
CONTEXT_SNAPSHOTS_COLLECTION = 'task_context_snapshots'
OPEN_LOOP_SNAPSHOTS_COLLECTION = 'task_open_loop_snapshots'
SNAPSHOT_RECEIPTS_COLLECTION = 'task_snapshot_receipts'
MAX_DECISION_HISTORY_PER_DEVICE = 24
TASK_INTELLIGENCE_CONTROL_COLLECTION = 'task_intelligence_control'
TASK_INTELLIGENCE_CONTROL_DOCUMENT = 'state'
class TaskRecommendationStoreError(RuntimeError):
pass
class IdempotencyConflictError(TaskRecommendationStoreError):
pass
class InterventionNotFoundError(TaskRecommendationStoreError):
pass
class AttributionChainNotFoundError(TaskRecommendationStoreError):
pass
class StaleSnapshotError(TaskRecommendationStoreError):
pass
class RecommendationGenerationMismatchError(TaskRecommendationStoreError):
pass
def _get_db(firestore_client: Any = None) -> Any:
return firestore_client or get_firestore_client()
def _user_ref(uid: str, *, firestore_client: Any = None):
return _get_db(firestore_client).collection('users').document(uid)
def _control_ref(uid: str, *, firestore_client: Any = None):
return (
_user_ref(uid, firestore_client=firestore_client)
.collection(TASK_INTELLIGENCE_CONTROL_COLLECTION)
.document(TASK_INTELLIGENCE_CONTROL_DOCUMENT)
)
def _validate_generation(
snapshot: Any,
*,
uid: str,
account_generation: int,
) -> None:
control = TaskWorkflowControl()
if snapshot.exists:
control = parse_snapshot_strict(TaskWorkflowControl, snapshot)
if control.account_generation != account_generation:
raise RecommendationGenerationMismatchError('account generation mismatch')
def _without_generation(payload: dict[str, Any]) -> dict[str, Any]:
result = dict(payload)
result.pop('account_generation', None)
result.pop('_override_expires_at', None)
result.pop('_receipt_id', None)
return result
def _snapshot_dict(snapshot: Any) -> dict[str, Any]:
payload = snapshot.to_dict()
return cast(dict[str, Any], payload) if isinstance(payload, dict) else {}
def _record_malformed_embedded_payload(*, evaluation_id: str, error: ValidationError) -> None:
error_types = [
str(item.get('type', 'unknown')) for item in error.errors(include_input=False, include_url=False)[:5]
]
logger.warning(
'Malformed embedded Firestore payload evaluation_id=%s validation_types=%s', evaluation_id, error_types
)
record_fallback(
component='firestore_read',
from_mode='firestore_document',
to_mode='skip_malformed_document',
reason='malformed_doc',
outcome='degraded',
log=logger,
)
def _stable_id(prefix: str, *parts: object) -> str:
raw = '\x1f'.join(str(part) for part in parts).encode('utf-8')
return f'{prefix}_{hashlib.sha256(raw).hexdigest()[:32]}'
def _request_hash(payload: dict[str, Any]) -> str:
serialized = json.dumps(payload, sort_keys=True, separators=(',', ':'), default=str)
return hashlib.sha256(serialized.encode('utf-8')).hexdigest()
def _cleanup_expired_snapshot_receipts(uid: str, *, now: datetime, firestore_client: Any) -> None:
collection = _user_ref(uid, firestore_client=firestore_client).collection(SNAPSHOT_RECEIPTS_COLLECTION)
for snapshot in collection.where('expires_at', '<=', now).limit(50).stream():
snapshot.reference.delete()
def get_intervention(
uid: str,
intervention_id: str,
*,
account_generation: int = 0,
firestore_client: Any = None,
) -> Optional[dict[str, Any]]:
snapshot = (
_user_ref(uid, firestore_client=firestore_client)
.collection(INTERVENTIONS_COLLECTION)
.document(intervention_id)
.get()
)
payload = _snapshot_dict(snapshot) if snapshot.exists else None
return payload if payload is not None and payload.get('account_generation') == account_generation else None
def create_intervention(
uid: str,
request: InterventionCreate,
*,
idempotency_key: str,
account_generation: int = 0,
now: datetime,
firestore_client: Any = None,
) -> tuple[InterventionRecord, bool]:
user_ref = _user_ref(uid, firestore_client=firestore_client)
intervention_id = _stable_id('intervention', uid, account_generation, request.surface.value, idempotency_key)
record = InterventionRecord(
**request.model_dump(mode='python'),
intervention_id=intervention_id,
attribution_chain_id=_stable_id('attr', uid, account_generation, intervention_id),
created_at=now,
)
payload = record.model_dump(mode='python')
payload['_request_hash'] = _request_hash(request.model_dump(mode='json'))
payload['account_generation'] = account_generation
ref = user_ref.collection(INTERVENTIONS_COLLECTION).document(intervention_id)
client = _get_db(firestore_client)
transaction = client.transaction()
@firestore.transactional
def apply(write_transaction):
_validate_generation(
_control_ref(uid, firestore_client=client).get(transaction=write_transaction),
uid=uid,
account_generation=account_generation,
)
existing = ref.get(transaction=write_transaction)
if not existing.exists:
write_transaction.set(ref, payload)
return record, True
stored = _snapshot_dict(existing)
if stored.get('_request_hash') != payload['_request_hash']:
raise IdempotencyConflictError('idempotency key was used for a different intervention')
stored.pop('_request_hash', None)
return (
parse_snapshot_strict(
InterventionRecord, existing, payload_from_snapshot=lambda _snapshot: _without_generation(stored)
),
False,
)
return apply(transaction)
def save_projection(
uid: str,
*,
device_scope: str,
projection: WhatMattersNowProjection,
decisions: list[DecisionRecord],
account_generation: int = 0,
firestore_client: Any = None,
) -> WhatMattersNowProjection:
client = _get_db(firestore_client)
user_ref = _user_ref(uid, firestore_client=client)
projection_ref = user_ref.collection(PROJECTIONS_COLLECTION).document(
_stable_id('projection', account_generation, device_scope)
)
decisions_collection = user_ref.collection(DECISIONS_COLLECTION)
decision_ref = decisions_collection.document(
_stable_id('decision', account_generation, device_scope, projection.evaluation_id)
)
transaction = client.transaction()
@firestore.transactional
def publish(write_transaction: Any) -> tuple[WhatMattersNowProjection, bool]:
_validate_generation(
_control_ref(uid, firestore_client=client).get(transaction=write_transaction),
uid=uid,
account_generation=account_generation,
)
current_snapshot = projection_ref.get(transaction=write_transaction)
if current_snapshot.exists:
current = parse_snapshot_strict(
WhatMattersNowProjection,
current_snapshot,
payload_from_snapshot=lambda _snapshot: _without_generation(_snapshot_dict(current_snapshot)),
)
if current.generated_at > projection.generated_at or (
current.material_version == projection.material_version and current.expires_at > projection.generated_at
):
return current, False
intervention_writes = []
for recommendation in projection.recommendations:
intervention_payload = recommendation.model_dump(mode='python')
intervention_ref = user_ref.collection(INTERVENTIONS_COLLECTION).document(recommendation.intervention_id)
existing_intervention = intervention_ref.get(transaction=write_transaction)
existing_created_at = (
_snapshot_dict(existing_intervention).get('created_at') if existing_intervention.exists else None
)
intervention_payload.update(
{
'evaluation_id': projection.evaluation_id,
'attribution_chain_id': _stable_id('attr', uid, recommendation.intervention_id),
'account_generation': account_generation,
'surface': 'what_matters_now',
'feedback_subject_kind': recommendation.feedback_subject_kind.value,
'feedback_subject_id': recommendation.feedback_subject_id,
'created_at': existing_created_at or projection.generated_at,
}
)
intervention_writes.append((intervention_ref, intervention_payload))
projection_payload = projection.model_dump(mode='python')
projection_payload['account_generation'] = account_generation
write_transaction.set(projection_ref, projection_payload)
write_transaction.set(
decision_ref,
{
'device_scope': device_scope,
'account_generation': account_generation,
'evaluation_id': projection.evaluation_id,
'evaluated_at': projection.generated_at,
'expires_at': projection.expires_at,
'projection': projection.model_dump(mode='python'),
'decisions': [decision.model_dump(mode='python') for decision in decisions],
},
)
for intervention_ref, intervention_payload in intervention_writes:
write_transaction.set(intervention_ref, intervention_payload)
return projection, True
published_projection, did_publish = publish(transaction)
if not did_publish:
return published_projection
batch = client.batch()
history = []
for snapshot in (
decisions_collection.where('device_scope', '==', device_scope)
.where('account_generation', '==', account_generation)
.stream()
):
payload = _snapshot_dict(snapshot)
if payload.get('evaluation_id') == projection.evaluation_id:
continue
history.append((payload.get('evaluated_at'), payload.get('expires_at'), snapshot.reference))
history.sort(key=lambda item: item[0] or datetime.min.replace(tzinfo=timezone.utc), reverse=True)
for index, (_, expires_at, ref) in enumerate(history):
if expires_at is None or expires_at <= projection.generated_at or index >= MAX_DECISION_HISTORY_PER_DEVICE - 1:
batch.delete(ref)
batch.commit()
return published_projection
def get_projection(
uid: str,
*,
device_scope: str,
now: datetime,
include_expired: bool = False,
account_generation: int = 0,
firestore_client: Any = None,
) -> Optional[WhatMattersNowProjection]:
client = _get_db(firestore_client)
ref = (
_user_ref(uid, firestore_client=client)
.collection(PROJECTIONS_COLLECTION)
.document(_stable_id('projection', account_generation, device_scope))
)
transaction = client.transaction()
@firestore.transactional
def read(read_transaction):
_validate_generation(
_control_ref(uid, firestore_client=client).get(transaction=read_transaction),
uid=uid,
account_generation=account_generation,
)
return ref.get(transaction=read_transaction)
snapshot = read(transaction)
if not snapshot.exists:
return None
payload = _snapshot_dict(snapshot)
if payload.get('account_generation') != account_generation:
return None
projection = parse_snapshot_or_none(
WhatMattersNowProjection,
snapshot,
payload_from_snapshot=lambda _snapshot: _without_generation(payload),
)
if projection is None:
return None
return projection if include_expired or projection.expires_at > now else None
def _decision_records(raw_records: list[Any], evaluation_id: str) -> list[DecisionRecord]:
"""Build DecisionRecord objects from stored decision dicts, skipping a malformed one.
DecisionRecord is extra='forbid', so a legacy or schema-drifted audit record would raise
ValidationError and 500 the whole recommendation read. Skip such a record rather than fail the
batch; unexpected errors still propagate. Sorted by subject_id to match the caller.
"""
records: list[DecisionRecord] = []
for record in raw_records:
try:
records.append(DecisionRecord.model_validate(record))
except ValidationError as e:
_record_malformed_embedded_payload(evaluation_id=evaluation_id, error=e)
records.sort(key=lambda record: record.subject_id)
return records
def get_decisions(
uid: str,
evaluation_id: str,
*,
device_scope: str,
account_generation: int = 0,
firestore_client: Any = None,
) -> list[DecisionRecord]:
client = _get_db(firestore_client)
ref = (
_user_ref(uid, firestore_client=client)
.collection(DECISIONS_COLLECTION)
.document(_stable_id('decision', account_generation, device_scope, evaluation_id))
)
transaction = client.transaction()
@firestore.transactional
def read(read_transaction):
_validate_generation(
_control_ref(uid, firestore_client=client).get(transaction=read_transaction),
uid=uid,
account_generation=account_generation,
)
return ref.get(transaction=read_transaction)
snapshot = read(transaction)
if not snapshot.exists:
return []
payload = _snapshot_dict(snapshot)
if payload.get('evaluation_id') != evaluation_id or payload.get('account_generation') != account_generation:
return []
raw_records = payload.get('decisions')
if not isinstance(raw_records, list):
return []
return _decision_records(raw_records, evaluation_id)
def _valid_evaluation_projection(
raw_projection: Any, evaluation_id: str, now: datetime
) -> Optional[WhatMattersNowProjection]:
"""Build a WhatMattersNowProjection from a stored projection dict, or None if unusable.
The stored projection was validated with no guard, so a legacy or schema-drifted doc would raise
ValidationError and 500 the recommendation read. Treat a malformed (or non-dict, stale, or
id-mismatched) projection as absent, consistent with this reader's other None returns.
"""
if not isinstance(raw_projection, dict):
return None
try:
projection = WhatMattersNowProjection.model_validate(raw_projection)
except ValidationError as e:
_record_malformed_embedded_payload(evaluation_id=evaluation_id, error=e)
return None
if projection.evaluation_id != evaluation_id or projection.expires_at <= now:
return None
return projection
def get_evaluation_projection(
uid: str,
evaluation_id: str,
*,
device_scope: str,
now: datetime,
account_generation: int = 0,
firestore_client: Any = None,
) -> Optional[WhatMattersNowProjection]:
client = _get_db(firestore_client)
ref = (
_user_ref(uid, firestore_client=client)
.collection(DECISIONS_COLLECTION)
.document(_stable_id('decision', account_generation, device_scope, evaluation_id))
)
transaction = client.transaction()
@firestore.transactional
def read(read_transaction):
_validate_generation(
_control_ref(uid, firestore_client=client).get(transaction=read_transaction),
uid=uid,
account_generation=account_generation,
)
return ref.get(transaction=read_transaction)
snapshot = read(transaction)
if not snapshot.exists:
return None
payload = _snapshot_dict(snapshot)
if payload.get('account_generation') != account_generation:
return None
return _valid_evaluation_projection(payload.get('projection'), evaluation_id, now)
def create_feedback(
uid: str,
request: FeedbackCreate,
*,
idempotency_key: str,
now: datetime,
override_expires_at: Optional[datetime],
account_generation: int = 0,
firestore_client: Any = None,
) -> tuple[FeedbackRecord, bool]:
client = _get_db(firestore_client)
user_ref = _user_ref(uid, firestore_client=client)
feedback_id = _stable_id('feedback', uid, account_generation, idempotency_key)
attribution_chain_id = _stable_id('attr', uid, account_generation, request.subject_kind.value, request.subject_id)
dedupe_key: Optional[str] = None
proposed_completion = request.reason is not None and request.reason.value == 'already_handled'
record = FeedbackRecord(
**request.model_dump(mode='python'),
feedback_id=feedback_id,
attribution_chain_id=attribution_chain_id,
created_at=now,
dedupe_key=dedupe_key,
proposed_completion=proposed_completion,
)
payload = record.model_dump(mode='python')
payload['_request_hash'] = _request_hash(request.model_dump(mode='json'))
payload['account_generation'] = account_generation
payload['_override_expires_at'] = override_expires_at
ref = user_ref.collection(FEEDBACK_COLLECTION).document(feedback_id)
transaction = client.transaction()
@firestore.transactional
def apply(write_transaction):
nonlocal attribution_chain_id, dedupe_key, record, payload
_validate_generation(
_control_ref(uid, firestore_client=client).get(transaction=write_transaction),
uid=uid,
account_generation=account_generation,
)
if request.intervention_id is not None:
intervention_ref = user_ref.collection(INTERVENTIONS_COLLECTION).document(request.intervention_id)
intervention_snapshot = intervention_ref.get(transaction=write_transaction)
intervention_payload = _snapshot_dict(intervention_snapshot)
if not intervention_snapshot.exists or intervention_payload.get('account_generation') != account_generation:
raise InterventionNotFoundError(request.intervention_id)
intervention_subject_kind = intervention_payload.get(
'feedback_subject_kind', intervention_payload.get('subject_kind')
)
intervention_subject_id = intervention_payload.get(
'feedback_subject_id', intervention_payload.get('subject_id')
)
if intervention_subject_kind != request.subject_kind.value or intervention_subject_id != request.subject_id:
raise IdempotencyConflictError('feedback subject does not match intervention')
attribution_chain_id = str(intervention_payload['attribution_chain_id'])
dedupe_key = str(intervention_payload['dedupe_key'])
record = record.model_copy(update={'attribution_chain_id': attribution_chain_id, 'dedupe_key': dedupe_key})
payload = record.model_dump(mode='python') | {
'_request_hash': _request_hash(request.model_dump(mode='json')),
'account_generation': account_generation,
'_override_expires_at': override_expires_at,
}
existing = ref.get(transaction=write_transaction)
if existing.exists:
stored = _snapshot_dict(existing)
if stored.get('_request_hash') != payload['_request_hash']:
raise IdempotencyConflictError('idempotency key was used for different feedback')
stored_dedupe_key = stored.get('dedupe_key')
stored_override_expiry = stored.get('_override_expires_at')
if isinstance(stored_dedupe_key, str) and isinstance(stored_override_expiry, datetime):
override_id = _stable_id('override', uid, account_generation, stored_dedupe_key)
override_ref = user_ref.collection(ATTENTION_OVERRIDES_COLLECTION).document(override_id)
if not override_ref.get(transaction=write_transaction).exists:
write_transaction.set(
override_ref,
{
'override_id': override_id,
'account_generation': account_generation,
'dedupe_key': stored_dedupe_key,
'intervention_id': request.intervention_id,
'feedback_id': feedback_id,
'action': request.action.value,
'reason': request.reason.value if request.reason is not None else None,
'created_at': stored.get('created_at', now),
'expires_at': stored_override_expiry,
},
)
stored.pop('_request_hash', None)
return (
parse_snapshot_strict(
FeedbackRecord, existing, payload_from_snapshot=lambda _snapshot: _without_generation(stored)
),
False,
)
write_transaction.set(ref, payload)
if override_expires_at is not None and dedupe_key is not None:
override_id = _stable_id('override', uid, account_generation, dedupe_key)
write_transaction.set(
user_ref.collection(ATTENTION_OVERRIDES_COLLECTION).document(override_id),
{
'override_id': override_id,
'account_generation': account_generation,
'dedupe_key': dedupe_key,
'intervention_id': request.intervention_id,
'feedback_id': feedback_id,
'action': request.action.value,
'reason': request.reason.value if request.reason is not None else None,
'created_at': now,
'expires_at': override_expires_at,
},
)
return record, True
return apply(transaction)
def list_active_override_dedupe_keys(
uid: str,
*,
now: datetime,
account_generation: int = 0,
firestore_client: Any = None,
) -> set[str]:
query = ACTIVE_ATTENTION_OVERRIDE_QUERY.build(
_user_ref(uid, firestore_client=firestore_client).collection(ATTENTION_OVERRIDES_COLLECTION),
{'account_generation': account_generation, 'now': now},
field_filter_factory=FieldFilter,
)
return {
str(payload['dedupe_key'])
for snapshot in query.stream()
if (payload := _snapshot_dict(snapshot)).get('dedupe_key')
}
def link_feedback_completion_candidate(
uid: str,
feedback_id: str,
candidate_id: str,
*,
account_generation: int,
firestore_client: Any = None,
) -> None:
client = _get_db(firestore_client)
ref = _user_ref(uid, firestore_client=client).collection(FEEDBACK_COLLECTION).document(feedback_id)
transaction = client.transaction()
@firestore.transactional
def apply(write_transaction):
_validate_generation(
_control_ref(uid, firestore_client=client).get(transaction=write_transaction),
uid=uid,
account_generation=account_generation,
)
snapshot = ref.get(transaction=write_transaction)
payload = _snapshot_dict(snapshot)
if not snapshot.exists or payload.get('account_generation') != account_generation:
raise RecommendationGenerationMismatchError('feedback generation mismatch')
payload['proposed_completion_candidate_id'] = candidate_id
write_transaction.set(ref, payload)
apply(transaction)
def _first_chain_record(
user_ref: Any, collection_name: str, attribution_chain_id: str, account_generation: int
) -> Optional[dict[str, Any]]:
matches = list(
user_ref.collection(collection_name)
.where(filter=FieldFilter('attribution_chain_id', '==', attribution_chain_id))
.where(filter=FieldFilter('account_generation', '==', account_generation))
.limit(1)
.stream()
)
return _snapshot_dict(matches[0]) if matches else None
def _outcome_matches_chain(user_ref: Any, request: OutcomeCreate, source: dict[str, Any]) -> bool:
raw_source_kind = source.get('feedback_subject_kind') or source.get('subject_kind') or ''
source_kind = str(getattr(raw_source_kind, 'value', raw_source_kind))
source_id = str(source.get('feedback_subject_id') or source.get('subject_id') or '')
allowed: set[tuple[str, str]] = {(source_kind, source_id)}
workstream_ids: set[str] = set()
if source_kind == 'candidate':
candidate_snapshot = user_ref.collection('candidates').document(source_id).get()
if candidate_snapshot.exists:
candidate = _snapshot_dict(candidate_snapshot)
result_task_id = candidate.get('result_task_id')
result_workstream_id = candidate.get('result_workstream_id')
if isinstance(result_task_id, str):
allowed.add(('task', result_task_id))
task_snapshot = user_ref.collection('action_items').document(result_task_id).get()
if task_snapshot.exists:
task_workstream_id = _snapshot_dict(task_snapshot).get('workstream_id')
if isinstance(task_workstream_id, str):
workstream_ids.add(task_workstream_id)
if isinstance(result_workstream_id, str):
workstream_ids.add(result_workstream_id)
elif source_kind == 'task':
task_snapshot = user_ref.collection('action_items').document(source_id).get()
if task_snapshot.exists:
workstream_id = _snapshot_dict(task_snapshot).get('workstream_id')
if isinstance(workstream_id, str):
workstream_ids.add(workstream_id)
elif source_kind == 'workstream':
workstream_ids.add(source_id)
allowed.update(('workstream', workstream_id) for workstream_id in workstream_ids)
if request.subject_kind.value == 'artifact':
for workstream_id in workstream_ids:
artifact = (
user_ref.collection('workstreams')
.document(workstream_id)
.collection('artifact_refs')
.document(request.subject_id)
.get()
)
if artifact.exists:
allowed.add(('artifact', request.subject_id))
break
expected_kind = {
'task_completed': 'task',
'artifact_approved': 'artifact',
'artifact_delivered': 'artifact',
'decision_resolved': 'decision',
'agent_output_applied': 'workstream',
'workstream_advanced': 'workstream',
}[request.outcome_code.value]
return request.subject_kind.value == expected_kind and (request.subject_kind.value, request.subject_id) in allowed
def create_outcome(
uid: str,
request: OutcomeCreate,
*,
idempotency_key: str,
now: datetime,
account_generation: int = 0,
firestore_client: Any = None,
) -> tuple[OutcomeRecord, bool]:
user_ref = _user_ref(uid, firestore_client=firestore_client)
source = _first_chain_record(user_ref, INTERVENTIONS_COLLECTION, request.attribution_chain_id, account_generation)
if source is None:
source = _first_chain_record(user_ref, FEEDBACK_COLLECTION, request.attribution_chain_id, account_generation)
if source is None:
raise AttributionChainNotFoundError(request.attribution_chain_id)
if not _outcome_matches_chain(user_ref, request, source):
raise IdempotencyConflictError('outcome subject or code does not match attribution chain')
outcome_id = _stable_id('outcome', uid, account_generation, idempotency_key)
record = OutcomeRecord(**request.model_dump(mode='python'), outcome_id=outcome_id, occurred_at=now)
payload = record.model_dump(mode='python')
payload['_request_hash'] = _request_hash(request.model_dump(mode='json'))
payload['account_generation'] = account_generation
ref = user_ref.collection(OUTCOMES_COLLECTION).document(outcome_id)
client = _get_db(firestore_client)
transaction = client.transaction()
@firestore.transactional
def apply(write_transaction):
_validate_generation(
_control_ref(uid, firestore_client=client).get(transaction=write_transaction),
uid=uid,
account_generation=account_generation,
)
existing = ref.get(transaction=write_transaction)
if not existing.exists:
write_transaction.set(ref, payload)
return record, True
stored = _snapshot_dict(existing)
if stored.get('_request_hash') != payload['_request_hash']:
raise IdempotencyConflictError('idempotency key was used for a different outcome')
stored.pop('_request_hash', None)
return (
parse_snapshot_strict(
OutcomeRecord, existing, payload_from_snapshot=lambda _snapshot: _without_generation(stored)
),
False,
)
return apply(transaction)
def replace_context_snapshot(
uid: str,
snapshot: NormalizedContextSnapshot,
*,
account_generation: int = 0,
idempotency_key: str | None = None,
firestore_client: Any = None,
) -> SnapshotReceipt:
client = _get_db(firestore_client)
ref = (
_user_ref(uid, firestore_client=client)
.collection(CONTEXT_SNAPSHOTS_COLLECTION)
.document(_stable_id('context', account_generation, snapshot.device_id))
)
transaction = client.transaction()
request_key = idempotency_key or snapshot.snapshot_id
request_hash = _request_hash(snapshot.model_dump(mode='json'))
receipt_ref = (
_user_ref(uid, firestore_client=client)
.collection(SNAPSHOT_RECEIPTS_COLLECTION)
.document(_stable_id('snapshot-receipt', account_generation, 'context', request_key))
)
@firestore.transactional
def apply(write_transaction):
_validate_generation(
_control_ref(uid, firestore_client=client).get(transaction=write_transaction),
uid=uid,
account_generation=account_generation,
)
prior_receipt = receipt_ref.get(transaction=write_transaction)
if prior_receipt.exists:
prior = _snapshot_dict(prior_receipt)
if prior.get('request_hash') != request_hash:
raise IdempotencyConflictError('idempotency key was used for a different context snapshot')
return SnapshotReceipt.model_validate(prior['receipt'])
stored_snapshot = ref.get(transaction=write_transaction)
replaced = stored_snapshot.exists
if replaced:
stored_payload = _snapshot_dict(stored_snapshot)
stored = parse_snapshot_strict(
NormalizedContextSnapshot,
stored_snapshot,
payload_from_snapshot=lambda _snapshot: _without_generation(stored_payload),
)
if snapshot.generated_at < stored.generated_at:
raise StaleSnapshotError('context snapshot is older than stored state')
if snapshot.generated_at == stored.generated_at and snapshot != stored:
raise IdempotencyConflictError('context snapshot timestamp was reused for different state')
payload = snapshot.model_dump(mode='python')
payload['account_generation'] = account_generation
payload['_receipt_id'] = receipt_ref.id
write_transaction.set(ref, payload)
receipt = SnapshotReceipt(snapshot_id=snapshot.snapshot_id, replaced=replaced, expires_at=snapshot.expires_at)
write_transaction.set(
receipt_ref,
{
'account_generation': account_generation,
'request_hash': request_hash,
'receipt': receipt.model_dump(mode='python'),
'expires_at': snapshot.expires_at,
},
)
return receipt
result = apply(transaction)
_cleanup_expired_snapshot_receipts(uid, now=snapshot.generated_at, firestore_client=client)
return result
def get_context_snapshot(
uid: str,
device_id: str,
*,
now: datetime,
account_generation: int = 0,
firestore_client: Any = None,
) -> Optional[NormalizedContextSnapshot]:
ref = (
_user_ref(uid, firestore_client=firestore_client)
.collection(CONTEXT_SNAPSHOTS_COLLECTION)
.document(_stable_id('context', account_generation, device_id))
)
snapshot = ref.get()
if not snapshot.exists:
return None
payload = _snapshot_dict(snapshot)
if payload.get('account_generation') != account_generation:
return None
record = parse_snapshot_or_none(
NormalizedContextSnapshot,
snapshot,
payload_from_snapshot=lambda _snapshot: _without_generation(payload),
)
if record is None:
return None
if record.expires_at <= now:
ref.delete()
receipt_id = payload.get('_receipt_id')
if isinstance(receipt_id, str):
_user_ref(uid, firestore_client=firestore_client).collection(SNAPSHOT_RECEIPTS_COLLECTION).document(
receipt_id
).delete()
_cleanup_expired_snapshot_receipts(uid, now=now, firestore_client=_get_db(firestore_client))
return None
return record
def replace_open_loop_snapshot(
uid: str,
snapshot: OpenLoopSnapshot,
*,
account_generation: int = 0,
idempotency_key: str | None = None,
firestore_client: Any = None,
) -> SnapshotReceipt:
client = _get_db(firestore_client)
snapshot_key = _stable_id(
'loop-snapshot', account_generation, snapshot.device_id, snapshot.runtime_id, snapshot.workstream_id
)
ref = _user_ref(uid, firestore_client=client).collection(OPEN_LOOP_SNAPSHOTS_COLLECTION).document(snapshot_key)
transaction = client.transaction()
request_key = idempotency_key or snapshot_key
request_hash = _request_hash(snapshot.model_dump(mode='json'))
receipt_ref = (
_user_ref(uid, firestore_client=client)
.collection(SNAPSHOT_RECEIPTS_COLLECTION)
.document(_stable_id('snapshot-receipt', account_generation, 'open-loop', request_key))
)
@firestore.transactional
def apply(write_transaction):
_validate_generation(
_control_ref(uid, firestore_client=client).get(transaction=write_transaction),
uid=uid,
account_generation=account_generation,
)
prior_receipt = receipt_ref.get(transaction=write_transaction)
if prior_receipt.exists:
prior = _snapshot_dict(prior_receipt)
if prior.get('request_hash') != request_hash:
raise IdempotencyConflictError('idempotency key was used for a different open-loop snapshot')
return SnapshotReceipt.model_validate(prior['receipt'])
stored_snapshot = ref.get(transaction=write_transaction)
replaced = stored_snapshot.exists
if replaced:
stored_payload = _snapshot_dict(stored_snapshot)
stored = parse_snapshot_strict(
OpenLoopSnapshot,
stored_snapshot,
payload_from_snapshot=lambda _snapshot: _without_generation(stored_payload),
)
if snapshot.generated_at < stored.generated_at:
raise StaleSnapshotError('open-loop snapshot is older than stored state')
if snapshot.generated_at == stored.generated_at and snapshot != stored:
raise IdempotencyConflictError('open-loop snapshot timestamp was reused for different state')
payload = snapshot.model_dump(mode='python')
payload['account_generation'] = account_generation
payload['_receipt_id'] = receipt_ref.id
write_transaction.set(ref, payload)
receipt = SnapshotReceipt(snapshot_id=snapshot_key, replaced=replaced, expires_at=snapshot.expires_at)
write_transaction.set(
receipt_ref,
{
'account_generation': account_generation,
'request_hash': request_hash,
'receipt': receipt.model_dump(mode='python'),
'expires_at': snapshot.expires_at,
},
)
return receipt
result = apply(transaction)
_cleanup_expired_snapshot_receipts(uid, now=snapshot.generated_at, firestore_client=client)
return result
def list_open_loop_snapshots(
uid: str,
*,
device_id: str,
now: datetime,
account_generation: int,
firestore_client: Any = None,
) -> list[OpenLoopSnapshot]:
collection = _user_ref(uid, firestore_client=firestore_client).collection(OPEN_LOOP_SNAPSHOTS_COLLECTION)
query = collection.where('device_id', '==', device_id).where('account_generation', '==', account_generation)
records: list[OpenLoopSnapshot] = []
for snapshot in query.stream():
payload = _snapshot_dict(snapshot)
record = parse_snapshot_or_none(
OpenLoopSnapshot,
snapshot,
payload_from_snapshot=lambda _snapshot: _without_generation(payload),
)
if record is None:
continue
if record.expires_at <= now:
snapshot.reference.delete()
receipt_id = payload.get('_receipt_id')
if isinstance(receipt_id, str):
_user_ref(uid, firestore_client=firestore_client).collection(SNAPSHOT_RECEIPTS_COLLECTION).document(
receipt_id
).delete()
continue
records.append(record)
_cleanup_expired_snapshot_receipts(uid, now=now, firestore_client=_get_db(firestore_client))
records.sort(key=lambda record: (record.workstream_id, record.runtime_id))
return records
def load_canonical_product_state(
uid: str, *, account_generation: int = 0, firestore_client: Any = None
) -> dict[str, list[dict[str, Any]]]:
"""Load bounded canonical state; device-local execution state is loaded separately."""
user_ref = _user_ref(uid, firestore_client=firestore_client)
def load_collection(name: str, limit: int) -> list[dict[str, Any]]:
records: list[dict[str, Any]] = []
collection = user_ref.collection(name)
query = (
collection.where('account_generation', '==', account_generation).limit(limit)
if account_generation > 0
else collection.limit(limit)
)
for snapshot in query.stream():
payload = _snapshot_dict(snapshot)
payload.setdefault('id', snapshot.id)
stored_generation = payload.get('account_generation', 0)
if stored_generation == account_generation:
records.append(payload)
return records
tasks = load_collection('action_items', 500)
candidates = load_collection('candidates', 200)
goals = load_collection('goals', 100)
workstreams = load_collection('workstreams', 200)
artifacts: list[dict[str, Any]] = []
workstream_events: list[dict[str, Any]] = []
for workstream in workstreams:
if len(artifacts) >= 200 and len(workstream_events) >= 200:
break
workstream_id = str(workstream.get('workstream_id') or workstream.get('id') or '')
if not workstream_id:
continue
workstream_ref = user_ref.collection('workstreams').document(workstream_id)
if len(artifacts) < 200:
for snapshot in workstream_ref.collection('artifact_refs').limit(100).stream():
payload = _snapshot_dict(snapshot)
payload.setdefault('artifact_id', snapshot.id)
payload.setdefault('workstream_id', workstream_id)