forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-canonical-maintenance.py
More file actions
126 lines (101 loc) · 4.69 KB
/
Copy pathrun-canonical-maintenance.py
File metadata and controls
126 lines (101 loc) · 4.69 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
#!/usr/bin/env python3
"""Run the full canonical memory maintenance pipeline against the local emulator."""
from __future__ import annotations
import argparse
import json
import os
import sys
from dataclasses import asdict, is_dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, cast
REPO_ROOT = Path(__file__).resolve().parents[2]
BACKEND_ROOT = REPO_ROOT / "backend"
sys.path.insert(0, str(BACKEND_ROOT))
sys.path.insert(0, str(REPO_ROOT / "scripts" / "dev-harness"))
from dev_harness import config # noqa: E402
def _jsonable(value: object) -> object:
if is_dataclass(value):
return {key: _jsonable(item) for key, item in asdict(cast(Any, value)).items()}
# Duck-type pydantic models without importing pydantic at module scope.
# The dev-harness test lane runs under bare python3 without backend deps;
# this script is loaded via runpy.run_path() in repo-checks CI.
model_dump = getattr(value, "model_dump", None)
if callable(model_dump):
return _jsonable(model_dump(mode="json"))
if isinstance(value, dict):
return {key: _jsonable(item) for key, item in value.items()}
if isinstance(value, list):
return [_jsonable(item) for item in value]
if isinstance(value, datetime):
return value.isoformat()
return value
def _apply_harness_env(cfg: config.HarnessConfig) -> None:
child_env = config.child_env_for(cfg)
os.environ.clear()
os.environ.update(child_env)
def _outbox_failure_counts(outbox: object) -> dict[str, int]:
if not isinstance(outbox, dict):
return {
"retryable": 0,
"dead_letter": 0,
"ack": 0,
"errors": 0,
}
raw_errors = outbox.get("errors")
error_count = len(raw_errors) if isinstance(raw_errors, list) else int(bool(raw_errors))
return {
"retryable": int(outbox.get("retryable_failure_count") or 0),
"dead_letter": int(outbox.get("dead_letter_count") or 0),
"ack": int(outbox.get("ack_failed_count") or 0),
"errors": error_count,
}
def _resolve_uid(cfg: config.HarnessConfig, user: str) -> str:
manifest_path = cfg.layout.state_root / "manifests" / "canonical-auth-uids.json"
if manifest_path.is_file():
payload = json.loads(manifest_path.read_text(encoding="utf-8"))
users = payload.get("users")
if isinstance(users, dict) and user in users:
return str(users[user])
raise SystemExit(f"Cannot resolve Firebase uid for {user!r}; seed happy_path first.")
def _configure_local_universal_memory(uid: str) -> None:
"""Validate the synthetic emulator principal before direct maintenance."""
if not os.environ.get("FIRESTORE_EMULATOR_HOST"):
raise SystemExit("Canonical maintenance harness requires the Firestore emulator.")
if os.environ.get("ENVIRONMENT") != "local-dev-harness":
raise SystemExit("Canonical maintenance harness requires ENVIRONMENT=local-dev-harness.")
os.environ["MEMORY_MODE"] = "read"
if not uid.strip():
raise SystemExit("Canonical maintenance harness requires a non-empty emulator uid.")
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("user", nargs="?", default="alice", help="Synthetic harness user (default: alice)")
parser.add_argument("--run-id", default="", help="Maintenance run id (default: manual-<utc timestamp>)")
args = parser.parse_args(argv)
cfg = config.load_config(REPO_ROOT, create_layout=False)
_apply_harness_env(cfg)
uid = _resolve_uid(cfg, args.user)
_configure_local_universal_memory(uid)
from utils.memory.short_term_promotion import run_canonical_short_term_maintenance # noqa: E402
run_id = args.run_id or f"manual-{datetime.now(timezone.utc).strftime('%Y%m%d%H%M%S')}"
report = run_canonical_short_term_maintenance(uid, run_id=run_id)
print(json.dumps(_jsonable(report), indent=2, sort_keys=True))
if report.skipped_reason:
print(f"canonical maintenance skipped: {report.skipped_reason}", file=sys.stderr)
return 2
outbox_failures = _outbox_failure_counts(report.outbox)
if any(outbox_failures.values()):
print(
"canonical maintenance outbox delivery failed: "
f"retryable={outbox_failures['retryable']} "
f"dead_letter={outbox_failures['dead_letter']} "
f"ack={outbox_failures['ack']} "
f"errors={outbox_failures['errors']}",
file=sys.stderr,
)
return 2
if report.promoted_count:
print(f"promoted_count={report.promoted_count}", file=sys.stderr)
return 0
if __name__ == "__main__":
raise SystemExit(main())