forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecurrence_inbox.py
More file actions
208 lines (172 loc) · 6.96 KB
/
Copy pathrecurrence_inbox.py
File metadata and controls
208 lines (172 loc) · 6.96 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
"""Durable workflow-owned handoff for canonical recurrence signals."""
import hashlib
from datetime import datetime, timezone
from typing import Any
from google.cloud import firestore
from google.cloud.firestore_v1.base_query import FieldFilter
from database._client import db as default_db
from models.memory_recurrence import CanonicalRecurrenceSignal
from models.workstream_association import (
RecurrenceInboxReceipt,
RecurrenceInboxStatus,
RecurrenceOutcomeKind,
)
from models.task_intelligence import TaskWorkflowControl, TaskWorkflowMode
RECURRENCE_INBOX_COLLECTION = 'task_recurrence_inbox'
TASK_INTELLIGENCE_CONTROL_COLLECTION = 'task_intelligence_control'
TASK_INTELLIGENCE_CONTROL_DOCUMENT = 'state'
class RecurrenceGenerationMismatchError(RuntimeError):
pass
def _get_db(firestore_client: Any = None):
return firestore_client if firestore_client is not None else default_db
def _receipt_id(uid: str, loop_key: str, account_generation: int) -> str:
digest = hashlib.sha256(f'{uid}:{account_generation}:{loop_key}'.encode('utf-8')).hexdigest()[:40]
return f'recurrence_inbox_{digest}'
def _receipt_ref(uid: str, receipt_id: str, *, firestore_client: Any = None):
return (
_get_db(firestore_client)
.collection('users')
.document(uid)
.collection(RECURRENCE_INBOX_COLLECTION)
.document(receipt_id)
)
def _control_ref(uid: str, *, firestore_client: Any = None):
return (
_get_db(firestore_client)
.collection('users')
.document(uid)
.collection(TASK_INTELLIGENCE_CONTROL_COLLECTION)
.document(TASK_INTELLIGENCE_CONTROL_DOCUMENT)
)
def _validate_generation(snapshot: Any, account_generation: int) -> None:
control = TaskWorkflowControl.model_validate(snapshot.to_dict() or {}) if snapshot.exists else TaskWorkflowControl()
if control.account_generation != account_generation:
raise RecurrenceGenerationMismatchError('account generation mismatch')
if control.workflow_mode not in {TaskWorkflowMode.write, TaskWorkflowMode.read}:
raise RecurrenceGenerationMismatchError('task workflow mode changed')
def _from_snapshot(snapshot: Any) -> RecurrenceInboxReceipt:
return RecurrenceInboxReceipt.model_validate(snapshot.to_dict() or {})
def _storage(receipt: RecurrenceInboxReceipt) -> dict[str, Any]:
payload = receipt.model_dump(mode='json')
payload['created_at'] = receipt.created_at
payload['updated_at'] = receipt.updated_at
return payload
def enqueue_recurrence_signal(
uid: str,
signal: CanonicalRecurrenceSignal,
*,
account_generation: int,
firestore_client: Any = None,
) -> RecurrenceInboxReceipt:
"""Persist before mutation; completed receipts never reopen within a generation."""
client = _get_db(firestore_client)
receipt_id = _receipt_id(uid, signal.stable_loop_key, account_generation)
ref = _receipt_ref(uid, receipt_id, firestore_client=client)
transaction = client.transaction()
now = datetime.now(timezone.utc)
@firestore.transactional
def apply(write_transaction):
_validate_generation(
_control_ref(uid, firestore_client=client).get(transaction=write_transaction), account_generation
)
snapshot = ref.get(transaction=write_transaction)
if snapshot.exists:
stored = _from_snapshot(snapshot)
# Freeze the first proposal until completion. If Candidate creation
# committed but this receipt ack failed, mutating the proposal would
# reuse its idempotency key with different content forever.
return stored
receipt = RecurrenceInboxReceipt(
receipt_id=receipt_id,
loop_key=signal.stable_loop_key,
account_generation=account_generation,
status=RecurrenceInboxStatus.pending,
signal=signal,
created_at=now,
updated_at=now,
)
write_transaction.create(ref, _storage(receipt))
return receipt
return apply(transaction)
def list_pending_recurrence_receipts(
uid: str,
*,
account_generation: int,
limit: int = 100,
firestore_client: Any = None,
) -> list[RecurrenceInboxReceipt]:
query = (
_get_db(firestore_client)
.collection('users')
.document(uid)
.collection(RECURRENCE_INBOX_COLLECTION)
.where(filter=FieldFilter('status', '==', RecurrenceInboxStatus.pending.value))
.where(filter=FieldFilter('account_generation', '==', account_generation))
.limit(limit)
)
return [_from_snapshot(snapshot) for snapshot in query.stream()]
def complete_recurrence_receipt(
uid: str,
receipt_id: str,
*,
outcome: RecurrenceOutcomeKind,
account_generation: int,
firestore_client: Any = None,
) -> None:
client = _get_db(firestore_client)
ref = _receipt_ref(uid, receipt_id, firestore_client=client)
transaction = client.transaction()
@firestore.transactional
def apply(write_transaction):
_validate_generation(
_control_ref(uid, firestore_client=client).get(transaction=write_transaction), account_generation
)
snapshot = ref.get(transaction=write_transaction)
if not snapshot.exists or _from_snapshot(snapshot).account_generation != account_generation:
raise RecurrenceGenerationMismatchError('recurrence receipt generation mismatch')
write_transaction.update(
ref,
{
'status': RecurrenceInboxStatus.completed.value,
'last_outcome': outcome.value,
'last_error_code': None,
'attempts': firestore.Increment(1),
'updated_at': datetime.now(timezone.utc),
},
)
apply(transaction)
def retry_recurrence_receipt(
uid: str,
receipt_id: str,
*,
error_code: str,
account_generation: int,
firestore_client: Any = None,
) -> None:
client = _get_db(firestore_client)
ref = _receipt_ref(uid, receipt_id, firestore_client=client)
transaction = client.transaction()
@firestore.transactional
def apply(write_transaction):
_validate_generation(
_control_ref(uid, firestore_client=client).get(transaction=write_transaction), account_generation
)
snapshot = ref.get(transaction=write_transaction)
if not snapshot.exists or _from_snapshot(snapshot).account_generation != account_generation:
raise RecurrenceGenerationMismatchError('recurrence receipt generation mismatch')
write_transaction.update(
ref,
{
'last_error_code': error_code[:128],
'attempts': firestore.Increment(1),
'updated_at': datetime.now(timezone.utc),
},
)
apply(transaction)
__all__ = [
'complete_recurrence_receipt',
'enqueue_recurrence_signal',
'list_pending_recurrence_receipts',
'retry_recurrence_receipt',
'RecurrenceGenerationMismatchError',
]