-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_policies.py
More file actions
316 lines (289 loc) · 12.5 KB
/
Copy pathplugin_policies.py
File metadata and controls
316 lines (289 loc) · 12.5 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
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Mapping
EVIDENCE_AUTOMATION_APPROVED = "automation_approved"
EVIDENCE_NOTIFICATION_ONLY = "notification_only"
EVIDENCE_DEPRECATED_COMPATIBILITY = "deprecated_compatibility"
PLUGIN_LIFECYCLE_AUTOMATION_APPROVED = "automation_approved"
PLUGIN_LIFECYCLE_NOTIFICATION_ONLY = "notification_only"
PLUGIN_LIFECYCLE_DEPRECATED_COMPATIBILITY = "deprecated_compatibility"
GENERAL_MARKET_REGIME_NOTIFICATION_TARGET = "market_regime_notification"
PLUGIN_CRISIS_RESPONSE_SHADOW = "crisis_response_shadow"
PLUGIN_MARKET_REGIME_CONTROL = "market_regime_control"
PLUGIN_MACRO_RISK_GOVERNOR = "macro_risk_governor"
PLUGIN_PANIC_REVERSAL_SHADOW = "panic_reversal_shadow"
PLUGIN_TACO_REBOUND_SHADOW = "taco_rebound_shadow"
PLUGIN_SCHEMA_VERSIONS: dict[str, tuple[str, ...]] = {
PLUGIN_CRISIS_RESPONSE_SHADOW: ("crisis_response_shadow.v1",),
PLUGIN_MARKET_REGIME_CONTROL: ("market_regime_control.v1",),
PLUGIN_MACRO_RISK_GOVERNOR: ("macro_risk_governor.v1",),
PLUGIN_PANIC_REVERSAL_SHADOW: ("panic_reversal_shadow.v1",),
PLUGIN_TACO_REBOUND_SHADOW: ("taco_rebound_shadow.v2",),
}
PLUGIN_DEPRECATED_SUCCESSORS: dict[str, str] = {
PLUGIN_CRISIS_RESPONSE_SHADOW: PLUGIN_MARKET_REGIME_CONTROL,
PLUGIN_MACRO_RISK_GOVERNOR: PLUGIN_MARKET_REGIME_CONTROL,
PLUGIN_TACO_REBOUND_SHADOW: PLUGIN_MARKET_REGIME_CONTROL,
}
PLUGIN_RESEARCH_ONLY_REASONS: dict[str, str] = {}
AUDITABLE_POSITION_CONTROL_FIELDS: tuple[str, ...] = (
"evidence_package_id",
"evidence_valid_until",
"bounded_budget",
)
@dataclass(frozen=True)
class PluginConsumptionPolicy:
plugin: str
strategy: str
notification_allowed: bool
position_control_allowed: bool
evidence_status: str
since_version: str
description: str
intended_strategy_role: str | None = None
manual_review_notification_target: str | None = None
@dataclass(frozen=True)
class PluginNotificationTargetPolicy:
plugin: str
notification_target: str
notification_allowed: bool
position_control_allowed: bool
evidence_status: str
since_version: str
description: str
notification_role: str
@dataclass(frozen=True)
class PluginLifecyclePolicy:
plugin: str
lifecycle_stage: str
schema_versions: tuple[str, ...]
new_mount_allowed: bool
replay_only: bool
description: str
successor: str | None = None
PLUGIN_LIFECYCLE_POLICIES: tuple[PluginLifecyclePolicy, ...] = (
PluginLifecyclePolicy(
plugin=PLUGIN_MARKET_REGIME_CONTROL,
lifecycle_stage=PLUGIN_LIFECYCLE_AUTOMATION_APPROVED,
schema_versions=PLUGIN_SCHEMA_VERSIONS[PLUGIN_MARKET_REGIME_CONTROL],
new_mount_allowed=True,
replay_only=False,
description="Unified market-regime sidecar; automated consumption still requires per-strategy policy approval.",
),
PluginLifecyclePolicy(
plugin=PLUGIN_PANIC_REVERSAL_SHADOW,
lifecycle_stage=PLUGIN_LIFECYCLE_NOTIFICATION_ONLY,
schema_versions=PLUGIN_SCHEMA_VERSIONS[PLUGIN_PANIC_REVERSAL_SHADOW],
new_mount_allowed=True,
replay_only=False,
description="Research notification sidecar; never grants automated position control.",
),
PluginLifecyclePolicy(
plugin=PLUGIN_CRISIS_RESPONSE_SHADOW,
lifecycle_stage=PLUGIN_LIFECYCLE_DEPRECATED_COMPATIBILITY,
schema_versions=PLUGIN_SCHEMA_VERSIONS[PLUGIN_CRISIS_RESPONSE_SHADOW],
new_mount_allowed=False,
replay_only=True,
successor=PLUGIN_MARKET_REGIME_CONTROL,
description="Legacy crisis sidecar retained for historical replay; new mounts use market_regime_control.",
),
PluginLifecyclePolicy(
plugin=PLUGIN_MACRO_RISK_GOVERNOR,
lifecycle_stage=PLUGIN_LIFECYCLE_DEPRECATED_COMPATIBILITY,
schema_versions=PLUGIN_SCHEMA_VERSIONS[PLUGIN_MACRO_RISK_GOVERNOR],
new_mount_allowed=False,
replay_only=True,
successor=PLUGIN_MARKET_REGIME_CONTROL,
description="Legacy macro sidecar retained for historical replay; new mounts use market_regime_control.",
),
PluginLifecyclePolicy(
plugin=PLUGIN_TACO_REBOUND_SHADOW,
lifecycle_stage=PLUGIN_LIFECYCLE_DEPRECATED_COMPATIBILITY,
schema_versions=PLUGIN_SCHEMA_VERSIONS[PLUGIN_TACO_REBOUND_SHADOW],
new_mount_allowed=False,
replay_only=True,
successor=PLUGIN_MARKET_REGIME_CONTROL,
description="Legacy event rebound notifier retained for replay; new mounts use market_regime_control/manual review.",
),
)
PLUGIN_LIFECYCLE_POLICY_REGISTRY: dict[str, PluginLifecyclePolicy] = {
policy.plugin: policy for policy in PLUGIN_LIFECYCLE_POLICIES
}
PLUGIN_CONSUMPTION_POLICIES: tuple[PluginConsumptionPolicy, ...] = (
PluginConsumptionPolicy(
plugin=PLUGIN_MARKET_REGIME_CONTROL,
strategy="tqqq_growth_income",
notification_allowed=True,
position_control_allowed=True,
evidence_status=EVIDENCE_AUTOMATION_APPROVED,
since_version="strategy_plugins.v1",
description="Backtested automatic macro/crisis risk controls for the TQQQ growth-income strategy.",
manual_review_notification_target=GENERAL_MARKET_REGIME_NOTIFICATION_TARGET,
),
PluginConsumptionPolicy(
plugin=PLUGIN_MARKET_REGIME_CONTROL,
strategy="global_etf_rotation",
notification_allowed=True,
position_control_allowed=False,
evidence_status=EVIDENCE_NOTIFICATION_ONLY,
since_version="strategy_plugins.v1",
description="Pending 25-30 year market-regime-control validation for broad ETF rotation.",
manual_review_notification_target=GENERAL_MARKET_REGIME_NOTIFICATION_TARGET,
),
PluginConsumptionPolicy(
plugin=PLUGIN_MARKET_REGIME_CONTROL,
strategy="russell_1000_multi_factor_defensive",
notification_allowed=True,
position_control_allowed=False,
evidence_status=EVIDENCE_NOTIFICATION_ONLY,
since_version="strategy_plugins.v1",
description="Pending 25-30 year market-regime-control validation for the Russell 1000 defensive sleeve.",
manual_review_notification_target=GENERAL_MARKET_REGIME_NOTIFICATION_TARGET,
),
PluginConsumptionPolicy(
plugin=PLUGIN_MARKET_REGIME_CONTROL,
strategy="mega_cap_leader_rotation_top50_balanced",
notification_allowed=True,
position_control_allowed=False,
evidence_status=EVIDENCE_NOTIFICATION_ONLY,
since_version="strategy_plugins.v1",
description="Pending 25-30 year market-regime-control validation for the mega-cap leader rotation profile.",
manual_review_notification_target=GENERAL_MARKET_REGIME_NOTIFICATION_TARGET,
),
PluginConsumptionPolicy(
plugin=PLUGIN_MARKET_REGIME_CONTROL,
strategy="soxl_soxx_trend_income",
notification_allowed=True,
position_control_allowed=True,
evidence_status=EVIDENCE_AUTOMATION_APPROVED,
since_version="strategy_plugins.v1",
description="Backtested automatic macro/crisis risk controls for the SOXL/SOXX trend-income strategy.",
manual_review_notification_target=GENERAL_MARKET_REGIME_NOTIFICATION_TARGET,
),
PluginConsumptionPolicy(
plugin=PLUGIN_CRISIS_RESPONSE_SHADOW,
strategy="tqqq_growth_income",
notification_allowed=True,
position_control_allowed=False,
evidence_status=EVIDENCE_DEPRECATED_COMPATIBILITY,
since_version="strategy_plugins.v1",
description="Deprecated direct crisis shadow mount kept for historical replay; new consumers use market_regime_control.",
),
PluginConsumptionPolicy(
plugin=PLUGIN_MACRO_RISK_GOVERNOR,
strategy="tqqq_growth_income",
notification_allowed=True,
position_control_allowed=False,
evidence_status=EVIDENCE_DEPRECATED_COMPATIBILITY,
since_version="strategy_plugins.v1",
description="Deprecated direct macro governor mount kept for historical replay; new consumers use market_regime_control.",
),
PluginConsumptionPolicy(
plugin=PLUGIN_TACO_REBOUND_SHADOW,
strategy="tqqq_growth_income",
notification_allowed=True,
position_control_allowed=False,
evidence_status=EVIDENCE_NOTIFICATION_ONLY,
since_version="strategy_plugins.v1",
description="Manual-review event rebound notifier for TQQQ only.",
),
PluginConsumptionPolicy(
plugin=PLUGIN_PANIC_REVERSAL_SHADOW,
strategy="tqqq_growth_income",
notification_allowed=True,
position_control_allowed=False,
evidence_status=EVIDENCE_NOTIFICATION_ONLY,
since_version="strategy_plugins.v1",
description="Research-only VIX panic reversal notifier for TQQQ manual review.",
),
)
PLUGIN_CONSUMPTION_POLICY_REGISTRY: dict[tuple[str, str], PluginConsumptionPolicy] = {
(policy.plugin, policy.strategy): policy for policy in PLUGIN_CONSUMPTION_POLICIES
}
PLUGIN_NOTIFICATION_TARGET_POLICIES: tuple[PluginNotificationTargetPolicy, ...] = (
PluginNotificationTargetPolicy(
plugin=PLUGIN_MARKET_REGIME_CONTROL,
notification_target=GENERAL_MARKET_REGIME_NOTIFICATION_TARGET,
notification_allowed=True,
position_control_allowed=False,
evidence_status=EVIDENCE_NOTIFICATION_ONLY,
since_version="strategy_plugins.v1",
description="General market-regime notice. Not mounted into an automated strategy runtime.",
notification_role="general_market_regime_notification",
),
PluginNotificationTargetPolicy(
plugin=PLUGIN_PANIC_REVERSAL_SHADOW,
notification_target=GENERAL_MARKET_REGIME_NOTIFICATION_TARGET,
notification_allowed=True,
position_control_allowed=False,
evidence_status=EVIDENCE_NOTIFICATION_ONLY,
since_version="strategy_plugins.v1",
description="General research-only panic reversal notice. Not mounted into an automated strategy runtime.",
notification_role="panic_reversal_notification",
),
)
PLUGIN_NOTIFICATION_TARGET_POLICY_REGISTRY: dict[tuple[str, str], PluginNotificationTargetPolicy] = {
(policy.plugin, policy.notification_target): policy for policy in PLUGIN_NOTIFICATION_TARGET_POLICIES
}
def extract_auditable_position_control_context(raw: Mapping[str, Any] | None) -> dict[str, Any]:
"""Return the auditable position-control evidence block if present.
The preferred shape is ``{"auditable_position_control": {...}}`` but the
helper also accepts a flat mapping so existing configs can opt in without a
schema migration.
"""
if not isinstance(raw, Mapping):
return {}
candidate = raw.get("auditable_position_control")
if isinstance(candidate, Mapping):
raw = candidate
return {
key: raw[key]
for key in AUDITABLE_POSITION_CONTROL_FIELDS
if raw.get(key) is not None
}
PLUGIN_COMPATIBLE_STRATEGIES: dict[str, tuple[str, ...]] = {
plugin: tuple(
policy.strategy
for policy in PLUGIN_CONSUMPTION_POLICIES
if policy.plugin == plugin and policy.notification_allowed
)
for plugin in sorted({policy.plugin for policy in PLUGIN_CONSUMPTION_POLICIES})
}
PLUGIN_COMPATIBLE_NOTIFICATION_TARGETS: dict[str, tuple[str, ...]] = {
plugin: tuple(
policy.notification_target
for policy in PLUGIN_NOTIFICATION_TARGET_POLICIES
if policy.plugin == plugin and policy.notification_allowed
)
for plugin in sorted({policy.plugin for policy in PLUGIN_NOTIFICATION_TARGET_POLICIES})
}
__all__ = [
"EVIDENCE_AUTOMATION_APPROVED",
"EVIDENCE_DEPRECATED_COMPATIBILITY",
"EVIDENCE_NOTIFICATION_ONLY",
"AUDITABLE_POSITION_CONTROL_FIELDS",
"GENERAL_MARKET_REGIME_NOTIFICATION_TARGET",
"PLUGIN_COMPATIBLE_NOTIFICATION_TARGETS",
"PLUGIN_COMPATIBLE_STRATEGIES",
"PLUGIN_CONSUMPTION_POLICIES",
"PLUGIN_CONSUMPTION_POLICY_REGISTRY",
"PLUGIN_CRISIS_RESPONSE_SHADOW",
"PLUGIN_DEPRECATED_SUCCESSORS",
"PLUGIN_LIFECYCLE_AUTOMATION_APPROVED",
"PLUGIN_LIFECYCLE_DEPRECATED_COMPATIBILITY",
"PLUGIN_LIFECYCLE_NOTIFICATION_ONLY",
"PLUGIN_LIFECYCLE_POLICIES",
"PLUGIN_LIFECYCLE_POLICY_REGISTRY",
"PLUGIN_MACRO_RISK_GOVERNOR",
"PLUGIN_MARKET_REGIME_CONTROL",
"PLUGIN_NOTIFICATION_TARGET_POLICIES",
"PLUGIN_NOTIFICATION_TARGET_POLICY_REGISTRY",
"PLUGIN_PANIC_REVERSAL_SHADOW",
"PLUGIN_SCHEMA_VERSIONS",
"PLUGIN_TACO_REBOUND_SHADOW",
"PLUGIN_RESEARCH_ONLY_REASONS",
"PluginConsumptionPolicy",
"PluginLifecyclePolicy",
"PluginNotificationTargetPolicy",
"extract_auditable_position_control_context",
]