forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchive_visibility_readiness.py
More file actions
237 lines (210 loc) · 9.36 KB
/
Copy patharchive_visibility_readiness.py
File metadata and controls
237 lines (210 loc) · 9.36 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
# LIFECYCLE: one-time
# DELETE-AFTER: INV-MEM-3
"""Canonical module for ``scripts.archive_visibility_readiness`` (WS-G8b).
Moved from ``utils/memory/v3/`` — its only consumer is the one-shot readiness
script ``scripts/p1_3_v3_archive_short_term_visibility_readiness.py``."""
from __future__ import annotations
from copy import deepcopy
from typing import Any, Dict, Iterable, List
VISIBLE = 'visible'
NOT_VISIBLE = 'not_visible'
BLOCKED = 'BLOCKED'
_ALLOWED_VISIBILITIES = {'short_term', 'long_term', 'archived_evidence'}
_ALLOWED_LIFECYCLES = {'working', 'active', 'context_only', 'review', 'superseded', 'rejected', 'hidden', None, ''}
_ALLOWED_SOURCE_FRESHNESS = {'fresh', 'stable', 'historical', 'stale'}
_SENSITIVE_KEYS = {
'content',
'text',
'quote',
'evidence',
'source_refs',
'evidence_quotes',
'cursor',
'next_cursor',
'page_token',
'api_key',
'token',
'secret',
}
def _sanitized_decision(decision: Dict[str, Any]) -> Dict[str, Any]:
return {
'memory_layer': decision.get('memory_layer') or 'unknown',
'visibility': decision.get('visibility') or 'unknown',
'source_freshness': decision.get('source_freshness') or 'unknown',
'default_visible': bool(decision.get('default_visible')),
'opt_in_visible': bool(decision.get('opt_in_visible')),
'reason': decision.get('reason') or 'missing_reason',
'agent_use': decision.get('agent_use') or 'not_available',
}
def _drop_sensitive_fields(record: Dict[str, Any]) -> Dict[str, Any]:
return {key: deepcopy(value) for key, value in record.items() if key not in _SENSITIVE_KEYS}
def decide_default_visibility(
record: Dict[str, Any],
*,
include_archive: bool = False,
include_historical: bool = False,
server_archive_capability: bool = False,
server_historical_capability: bool = False,
) -> Dict[str, Any]:
"""Pure readiness-only default visibility decision for future GET /v3/memories.
The contract is intentionally fail-closed. It does not call production services,
import routers, mutate persistence, emit telemetry, or wire runtime behavior.
"""
safe_record = _drop_sensitive_fields(dict(record))
visibility = safe_record.get('visibility')
lifecycle = safe_record.get('lifecycle_status') or safe_record.get('status')
source_freshness = safe_record.get('source_freshness')
memory_layer = safe_record.get('memory_layer')
decision = {
**safe_record,
'default_visible': False,
'opt_in_visible': False,
'agent_use': 'not_default_visible',
'reason': 'fail_closed',
}
if visibility not in _ALLOWED_VISIBILITIES:
decision['reason'] = 'unknown_visibility_fail_closed'
return decision
if lifecycle not in _ALLOWED_LIFECYCLES:
decision['reason'] = 'unknown_lifecycle_fail_closed'
return decision
if source_freshness not in _ALLOWED_SOURCE_FRESHNESS:
decision['reason'] = 'unknown_source_freshness_fail_closed'
return decision
if lifecycle in {'hidden', 'rejected'}:
decision['reason'] = 'hidden_or_rejected_fail_closed'
return decision
if visibility == 'archived_evidence' or memory_layer == 'l1_archive':
if include_archive and include_historical:
if server_archive_capability and server_historical_capability:
decision['opt_in_visible'] = True
decision['agent_use'] = 'archived_evidence_not_stable_profile'
decision['reason'] = 'archive_visible_only_by_explicit_opt_in'
else:
decision['reason'] = 'archive_requires_server_capability'
else:
decision['reason'] = 'archive_requires_explicit_opt_in'
return decision
if visibility == 'short_term':
if lifecycle != 'working':
decision['reason'] = 'short_term_requires_approved_working_lifecycle'
return decision
if source_freshness == 'stale':
if include_historical:
decision['opt_in_visible'] = True
decision['agent_use'] = 'historical_short_term_context_not_default_visible'
decision['reason'] = 'stale_short_term_visible_only_by_explicit_opt_in'
else:
decision['reason'] = 'stale_short_term_requires_explicit_opt_in'
return decision
if source_freshness != 'fresh':
decision['reason'] = 'short_term_requires_fresh_source'
return decision
if safe_record.get('source_backed_projection') is not True:
decision['reason'] = 'short_term_requires_source_backed_projection'
return decision
decision['default_visible'] = True
decision['agent_use'] = 'fresh_short_term_source_backed_context'
decision['reason'] = 'fresh_short_term_source_backed_default_visible'
return decision
if visibility == 'long_term':
if lifecycle == 'active' and source_freshness == 'stable':
decision['default_visible'] = True
decision['agent_use'] = 'stable_profile_fact'
decision['reason'] = 'active_long_term_default_visible'
return decision
if lifecycle in {'review', 'context_only', 'superseded'}:
decision['reason'] = 'non_active_long_term_not_default_visible'
return decision
decision['reason'] = 'long_term_requires_active_stable_source'
return decision
return decision
def _sample_records() -> List[Dict[str, Any]]:
return [
{
'memory_id': 'sample_archive',
'memory_layer': 'l1_archive',
'archive_class': 'general',
'visibility': 'archived_evidence',
'source_freshness': 'historical',
'source_backed_projection': True,
},
{
'memory_id': 'sample_stale_short',
'memory_layer': 'working',
'lifecycle_status': 'working',
'visibility': 'short_term',
'source_freshness': 'stale',
'source_backed_projection': True,
},
{
'memory_id': 'sample_fresh_short',
'memory_layer': 'working',
'lifecycle_status': 'working',
'visibility': 'short_term',
'source_freshness': 'fresh',
'source_backed_projection': True,
},
{
'memory_id': 'sample_long_term',
'memory_layer': 'durable',
'lifecycle_status': 'active',
'visibility': 'long_term',
'source_freshness': 'stable',
'source_backed_projection': True,
},
]
def evaluate_archive_short_term_visibility_readiness(
*, sample_records: Iterable[Dict[str, Any]] | None = None
) -> Dict[str, Any]:
records = list(sample_records) if sample_records is not None else _sample_records()
decisions = [decide_default_visibility(record) for record in records]
sanitized_decisions = [_sanitized_decision(decision) for decision in decisions]
reason_counts: Dict[str, int] = {}
for decision in sanitized_decisions:
reason = decision['reason']
reason_counts[reason] = reason_counts.get(reason, 0) + 1
default_visible_count = sum(1 for decision in sanitized_decisions if decision['default_visible'])
opt_in_visible_count = sum(1 for decision in sanitized_decisions if decision['opt_in_visible'])
return {
'script': 'p1_3_v3_archive_short_term_visibility_readiness',
'status': BLOCKED,
'proof_status': BLOCKED,
'approval': False,
'read_only': True,
'route_wiring': False,
'runtime_behavior_changed': False,
'production_call_count': 0,
'firestore_read_count': 0,
'firestore_write_count': 0,
'network_call_count': 0,
'telemetry_sink_call_count': 0,
'provider_or_vector_call_count': 0,
'legacy_fallback_or_merge': False,
'case_count': len(sanitized_decisions),
'default_visible_count': default_visible_count,
'blocked_or_opt_in_required_count': len(sanitized_decisions) - default_visible_count,
'opt_in_visible_count': opt_in_visible_count,
'reason_counts': reason_counts,
'decisions': sanitized_decisions,
'contract': {
'archive_default_visible': False,
'stale_short_term_default_visible': False,
'archive_requires_explicit_opt_in': True,
'historical_context_requires_explicit_opt_in': True,
'fresh_short_term_source_backed_projection_default_visible': True,
'long_term_active_stable_synthesis_allowed': True,
'unknown_visibility_lifecycle_or_freshness_fail_closed': True,
'memory_failure_legacy_fallback_or_merge_allowed': False,
},
'summary': {
'status': BLOCKED,
'case_count': len(sanitized_decisions),
'default_visible_count': default_visible_count,
'blocked_or_opt_in_required_count': len(sanitized_decisions) - default_visible_count,
'archive_opt_in_required_count': reason_counts.get('archive_requires_explicit_opt_in', 0),
'server_capability_required_count': reason_counts.get('archive_requires_server_capability', 0),
},
'remaining_blocker': 'future GET /v3/memories route wiring and real service runtime evidence are intentionally absent',
}
# Neutral symbol aliases (memory names remain valid via shim)