forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenroll_canonical_memory_user.py
More file actions
316 lines (271 loc) · 11.3 KB
/
Copy pathenroll_canonical_memory_user.py
File metadata and controls
316 lines (271 loc) · 11.3 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
#!/usr/bin/env python3
"""Prepare or apply canonical-memory rollout Firestore docs for explicit UIDs.
Dry-run is the default. Applying writes requires an explicit Firestore project,
UID confirmation, and an existing-doc update acknowledgement. The script writes
only rollout control documents; it validates v3 read-proof prerequisites instead
of fabricating projection data.
"""
from __future__ import annotations
import argparse
import json
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import Any, cast
from google.cloud import firestore
BACKEND_DIR = Path(__file__).resolve().parents[1]
if str(BACKEND_DIR) not in sys.path:
sys.path.insert(0, str(BACKEND_DIR))
from config.memory_rollout import MemoryRolloutMode, PASSED
from database.google_credentials import prepare_google_credentials
from database.memory_collections import MemoryCollections
from utils.memory.default_read_rollout import DEFAULT_READ_ROLLOUT_SCHEMA_VERSION
from utils.memory.v3_limited_rollout_config import GLOBAL_READ_GATE_PATH, WRITE_CONVERGENCE_GATE_PATH
FIRST_USER_UID = "vi7SA9ckQCe4ccobWNxlbdcNdC23"
DEFAULT_OWNER = "memory_platform"
DEFAULT_ROUTE_SCOPE = "get_v3_memories"
@dataclass(frozen=True)
class RolloutDocumentPlan:
path: str
payload: dict[str, Any]
def build_global_read_gate(*, stage: str, owner: str = DEFAULT_OWNER) -> dict[str, Any]:
read_enabled = stage == "read"
return {
"route_scope": DEFAULT_ROUTE_SCOPE,
"purpose": "v3_runtime_enablement",
"owner": owner,
"config_schema_version": 1,
"memory_reads_enabled": read_enabled,
"kill_switch_active": not read_enabled,
}
def build_write_convergence_gate(*, stage: str, owner: str = DEFAULT_OWNER) -> dict[str, Any]:
ready = stage in {"write", "read"}
return {
"route_scope": DEFAULT_ROUTE_SCOPE,
"purpose": "v3_write_convergence_gate",
"owner": owner,
"config_schema_version": 1,
"durable_outbox_enabled": ready,
"dual_write_projection_ready": ready,
"delete_convergence_ready": ready,
"idempotency_contract_ready": ready,
}
def build_user_control_state(
*,
uid: str,
stage: str,
account_generation: int,
default_memory_grant: bool | None = None,
archive_grant: bool = False,
) -> dict[str, Any]:
if not uid:
raise ValueError("uid is required")
if account_generation < 0:
raise ValueError("account_generation must be nonnegative")
mode = MemoryRolloutMode(stage)
read_stage = stage == "read"
write_stage = stage in {"write", "read"}
default_memory = read_stage if default_memory_grant is None else default_memory_grant
return {
"uid": uid,
"schema_version": DEFAULT_READ_ROLLOUT_SCHEMA_VERSION,
"mode": mode.value,
"mode_epoch": 1,
"cutover_epoch": 1 if read_stage else 0,
"account_generation": account_generation,
"fallback_projection_ready": read_stage,
"persistent_memory_writes_started": write_stage,
"decommission_reconciled": False,
"writes_blocked": not write_stage,
"stage_gates": {
"shadow": PASSED if write_stage else "blocked",
"write": PASSED if write_stage else "blocked",
"read": PASSED if read_stage else "blocked",
},
"grants": {"omi_chat": {"default_memory": default_memory, "archive": archive_grant}},
"vector_repair_outbox_enabled": False,
}
def build_rollout_documents(
*,
uid: str,
stage: str,
account_generation: int,
default_memory_grant: bool | None = None,
archive_grant: bool = False,
owner: str = DEFAULT_OWNER,
) -> list[RolloutDocumentPlan]:
paths = MemoryCollections(uid=uid)
return [
RolloutDocumentPlan(GLOBAL_READ_GATE_PATH, build_global_read_gate(stage=stage, owner=owner)),
RolloutDocumentPlan(WRITE_CONVERGENCE_GATE_PATH, build_write_convergence_gate(stage=stage, owner=owner)),
RolloutDocumentPlan(
paths.memory_control_state,
build_user_control_state(
uid=uid,
stage=stage,
account_generation=account_generation,
default_memory_grant=default_memory_grant,
archive_grant=archive_grant,
),
),
]
def v3_read_prerequisite_paths(uid: str) -> list[str]:
paths = MemoryCollections(uid=uid)
return [
paths.memory_state_head,
paths.v3_compatibility_projection_state,
]
def _snapshot_data(snapshot: Any) -> dict[str, Any] | None:
if not getattr(snapshot, "exists", False):
return None
raw: object = snapshot.to_dict()
return cast(dict[str, Any], raw) if isinstance(raw, dict) else None
def inspect_existing_docs(db_client: Any, documents: list[RolloutDocumentPlan]) -> dict[str, Any]:
existing: dict[str, Any] = {}
for document in documents:
existing[document.path] = _snapshot_data(db_client.document(document.path).get())
return existing
def inspect_v3_read_prerequisites(db_client: Any, *, uid: str) -> dict[str, bool]:
result: dict[str, bool] = {}
for path in v3_read_prerequisite_paths(uid):
result[path] = _snapshot_data(db_client.document(path).get()) is not None
return result
def assert_v3_read_prerequisites_ready(prerequisites: dict[str, bool]) -> None:
missing = [path for path, exists in prerequisites.items() if not exists]
if missing:
raise RuntimeError("--stage read --apply requires existing v3 read prerequisite docs: " + ", ".join(missing))
def apply_documents(
db_client: Any,
documents: list[RolloutDocumentPlan],
*,
allow_existing_update: bool,
) -> dict[str, Any]:
existing = inspect_existing_docs(db_client, documents)
changed_existing = [
path
for path, current in existing.items()
if current is not None and current != _payload_by_path(documents)[path]
]
if changed_existing and not allow_existing_update:
raise RuntimeError(
"Refusing to update existing differing docs without --allow-existing-update: " + ", ".join(changed_existing)
)
for document in documents:
db_client.document(document.path).set(document.payload, merge=True)
return {
"written_paths": [document.path for document in documents],
"updated_existing_paths": changed_existing,
}
def _payload_by_path(documents: list[RolloutDocumentPlan]) -> dict[str, dict[str, Any]]:
return {document.path: document.payload for document in documents}
def build_report(
*,
uid: str,
stage: str,
account_generation: int,
firestore_project: str | None,
documents: list[RolloutDocumentPlan],
existing_docs: dict[str, Any] | None = None,
v3_read_prerequisites: dict[str, bool] | None = None,
writes: dict[str, Any] | None = None,
) -> dict[str, Any]:
return {
"artifact": "canonical_memory_user_enrollment",
"uid": uid,
"stage": stage,
"account_generation": account_generation,
"firestore_project": firestore_project,
"dry_run": writes is None,
"document_count": len(documents),
"documents": _payload_by_path(documents),
"existing_docs": existing_docs,
"v3_read_prerequisites": v3_read_prerequisites,
"writes": writes,
"operator_notes": [
"This script writes only rollout control docs when --apply is supplied.",
"It does not add users to CANONICAL_MEMORY_USERS or MEMORY_ENABLED_USERS.",
"It does not fabricate memory_state/head or v3 compatibility projection data.",
"For first-user dev launch, target Firestore project is based-hardware.",
],
}
def _load_firestore_client(*, firestore_project: str):
prepare_google_credentials()
return firestore.Client(project=firestore_project)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Prepare/apply canonical memory rollout Firestore docs for a UID.")
parser.add_argument("--uid", required=True)
parser.add_argument("--stage", choices=("off", "write", "read"), default="write")
parser.add_argument("--account-generation", type=int, default=1)
parser.add_argument(
"--firestore-project", help="Explicit Firestore project for inspection/apply, e.g. based-hardware."
)
parser.add_argument(
"--default-memory-grant", action="store_true", help="Force grants.omi_chat.default_memory=true."
)
parser.add_argument("--archive-grant", action="store_true", help="Set grants.omi_chat.archive=true.")
parser.add_argument("--owner", default=DEFAULT_OWNER)
parser.add_argument(
"--inspect-existing", action="store_true", help="Read existing target docs and include them in output."
)
parser.add_argument(
"--check-v3-read-prereqs",
action="store_true",
help="Read-only check for memory_state/head and v3 compatibility projection state.",
)
parser.add_argument("--apply", action="store_true", help="Write docs. Default is dry-run only.")
parser.add_argument("--confirm-uid", help="Required with --apply; must exactly match --uid.")
parser.add_argument(
"--allow-existing-update",
action="store_true",
help="Required with --apply if existing target docs differ from the requested payload.",
)
return parser.parse_args()
def main() -> int:
args = parse_args()
default_memory_grant: bool | None = True if args.default_memory_grant else None
documents = build_rollout_documents(
uid=args.uid,
stage=args.stage,
account_generation=args.account_generation,
default_memory_grant=default_memory_grant,
archive_grant=args.archive_grant,
owner=args.owner,
)
existing_docs = None
v3_read_prerequisites = None
writes = None
db_client = None
needs_firestore = args.inspect_existing or args.check_v3_read_prereqs or args.apply
if needs_firestore:
if not args.firestore_project:
raise SystemExit("--firestore-project is required for inspection or apply")
db_client = _load_firestore_client(firestore_project=args.firestore_project)
if args.inspect_existing or args.apply:
existing_docs = inspect_existing_docs(db_client, documents)
if args.check_v3_read_prereqs or (args.apply and args.stage == "read"):
v3_read_prerequisites = inspect_v3_read_prerequisites(db_client, uid=args.uid)
if args.apply:
if args.confirm_uid != args.uid:
raise SystemExit("--confirm-uid must exactly match --uid when --apply is used")
if args.stage == "read":
assert_v3_read_prerequisites_ready(v3_read_prerequisites or {})
writes = apply_documents(db_client, documents, allow_existing_update=args.allow_existing_update)
print(
json.dumps(
build_report(
uid=args.uid,
stage=args.stage,
account_generation=args.account_generation,
firestore_project=args.firestore_project,
documents=documents,
existing_docs=existing_docs,
v3_read_prerequisites=v3_read_prerequisites,
writes=writes,
),
indent=2,
sort_keys=True,
)
)
return 0
if __name__ == "__main__":
raise SystemExit(main())