-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_settings.py
More file actions
1277 lines (1107 loc) · 52.9 KB
/
Copy pathruntime_settings.py
File metadata and controls
1277 lines (1107 loc) · 52.9 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Validate and render QuantStrategyLab runtime target settings."""
from __future__ import annotations
import argparse
import hashlib
import json
import os
import re
import shlex
import subprocess
import sys
from dataclasses import dataclass
from datetime import date
from pathlib import Path
from typing import Any
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
ROOT = Path(__file__).resolve().parents[2]
LOCAL_TARGETS_DIR = ROOT / "local" / "targets"
EXAMPLE_TARGETS_DIR = ROOT / "examples" / "targets"
LOCAL_POLICY_PATH = ROOT / "local" / "policy.json"
PLATFORM_CONFIG_PATH = ROOT / "platform-config.json"
SUPPORTED_PLATFORMS = {
"schwab": {"plugin_mounts_prefix": "SCHWAB_", "repository": "QuantStrategyLab/CharlesSchwabPlatform"},
"longbridge": {"plugin_mounts_prefix": "LONGBRIDGE_", "repository": "QuantStrategyLab/LongBridgePlatform"},
"ibkr": {"plugin_mounts_prefix": "IBKR_", "repository": "QuantStrategyLab/InteractiveBrokersPlatform"},
"firstrade": {"plugin_mounts_prefix": "FIRSTRADE_", "repository": "QuantStrategyLab/FirstradePlatform"},
"qmt": {"plugin_mounts_prefix": "QMT_", "repository": "QuantStrategyLab/QmtPlatform"},
"binance": {"plugin_mounts_prefix": "BINANCE_", "repository": "QuantStrategyLab/BinancePlatform"},
}
PLATFORM_REPOSITORY_ENV = {
"schwab": "RUNTIME_SETTINGS_SCHWAB_REPO",
"longbridge": "RUNTIME_SETTINGS_LONGBRIDGE_REPO",
"ibkr": "RUNTIME_SETTINGS_IBKR_REPO",
"firstrade": "RUNTIME_SETTINGS_FIRSTRADE_REPO",
"qmt": "RUNTIME_SETTINGS_QMT_REPO",
"binance": "RUNTIME_SETTINGS_BINANCE_REPO",
}
RUNTIME_REQUIRED_FIELDS = (
"platform_id",
"strategy_profile",
"dry_run_only",
"deployment_selector",
"account_selector",
"account_scope",
"service_name",
"execution_mode",
)
WINDOW_MODES = {
"precheck": {"notify_only", "dry_run"},
"execution": {"live", "paper", "dry_run"},
}
SCHEDULER_FIELDS = frozenset({"timezone", "main_time", "probe_time", "precheck_time"})
MARKET_FIELDS = ("market", "market_calendar", "market_timezone")
STRATEGY_RELEASE_REQUIRED_FIELDS = (
"release_id",
"manifest_sha256",
"strategy_revision",
"config_sha256",
"risk_policy_sha256",
"evidence_sha256",
"plugin_bundle_sha256",
"effective_session",
)
STRATEGY_RELEASE_DIGEST_FIELDS = frozenset(
{
"manifest_sha256",
"config_sha256",
"risk_policy_sha256",
"evidence_sha256",
"plugin_bundle_sha256",
}
)
STRATEGY_RELEASE_ID_PATTERN = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{2,127}$")
SHA256_PATTERN = re.compile(r"^(?:sha256:)?[0-9a-fA-F]{64}$")
LIVE_CONTINUITY_STATES = frozenset(
{
"ACTIVE_LKG",
"ACTIVE_REDUCED",
"RECONCILE_ONLY",
"RISK_REDUCTION_ONLY",
"PAUSED",
"ROLLBACK_LKG",
}
)
LIVE_CONTINUITY_BASELINE_KINDS = frozenset({"legacy_authorized", "release_attested"})
LIVE_CONTINUITY_FIELDS = frozenset(
{
"state",
"baseline_kind",
"baseline_id",
"baseline_target_sha256",
"captured_at",
}
)
LIVE_CONTINUITY_BASELINE_ID_PATTERN = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{2,127}$")
GENERATED_VARIABLES = {"RUNTIME_TARGET_JSON", "STRATEGY_PROFILE"}
SECRET_MARKERS = ("PASSWORD", "PRIVATE_KEY", "TOKEN", "API_KEY", "ACCESS_KEY", "CLIENT_SECRET", "SECRET")
LEGACY_INCOME_LAYER_VARIABLES = frozenset(
{
"INCOME_THRESHOLD_USD",
"QQQI_INCOME_RATIO",
"INCOME_LAYER_QQQI_WEIGHT",
"INCOME_LAYER_SPYI_WEIGHT",
}
)
OPTION_OVERLAY_VARIABLES = frozenset(
{
"OPTION_OVERLAY_ENABLED",
"OPTION_GROWTH_OVERLAY_ENABLED",
"OPTION_GROWTH_OVERLAY_RECIPE",
"OPTION_GROWTH_OVERLAY_START_USD",
"OPTION_GROWTH_OVERLAY_NAV_BUDGET_RATIO",
"OPTION_INCOME_OVERLAY_ENABLED",
"OPTION_INCOME_OVERLAY_RECIPE",
"OPTION_INCOME_OVERLAY_START_USD",
"OPTION_INCOME_OVERLAY_NAV_RISK_RATIO",
}
)
OPTION_OVERLAY_ENABLED_VARIABLES = frozenset(
{
"OPTION_OVERLAY_ENABLED",
"OPTION_GROWTH_OVERLAY_ENABLED",
"OPTION_INCOME_OVERLAY_ENABLED",
}
)
OPTION_OVERLAY_RECIPE_VARIABLES = frozenset(
{
"OPTION_GROWTH_OVERLAY_RECIPE",
"OPTION_INCOME_OVERLAY_RECIPE",
}
)
OPTION_OVERLAY_AMOUNT_VARIABLES = frozenset(
{
"OPTION_GROWTH_OVERLAY_START_USD",
"OPTION_INCOME_OVERLAY_START_USD",
}
)
OPTION_OVERLAY_RATIO_VARIABLES = frozenset(
{
"OPTION_GROWTH_OVERLAY_NAV_BUDGET_RATIO",
"OPTION_INCOME_OVERLAY_NAV_RISK_RATIO",
}
)
RESEARCH_ONLY_EXTRA_VARIABLES = LEGACY_INCOME_LAYER_VARIABLES
PLATFORM_DRY_RUN_VARIABLES = {
"schwab": "SCHWAB_DRY_RUN_ONLY",
"longbridge": "LONGBRIDGE_DRY_RUN_ONLY",
"ibkr": "IBKR_DRY_RUN_ONLY",
"firstrade": "FIRSTRADE_DRY_RUN_ONLY",
"binance": "BINANCE_DRY_RUN",
}
@dataclass(frozen=True)
class Assignment:
target_id: str
repository: str
variable_scope: str
environment: str | None
name: str
value: str
@property
def deletes_variable(self) -> bool:
return self.value == ""
def gh_command(self, *, redact_body: bool = False, redact_metadata: bool = False) -> list[str]:
repository = redacted_value() if redact_metadata else self.repository
if self.deletes_variable:
command = ["gh", "variable", "delete", self.name, "--repo", repository]
else:
body = redacted_value() if redact_body else self.value
command = ["gh", "variable", "set", self.name, "--repo", repository, "--body", body]
if self.variable_scope == "environment":
environment = redacted_value() if redact_metadata else (self.environment or "")
command.extend(["--env", environment])
return command
def shell_command(self, *, redact_body: bool = False, redact_metadata: bool = False) -> str:
return " ".join(
shlex.quote(part) for part in self.gh_command(redact_body=redact_body, redact_metadata=redact_metadata)
)
def redacted_value() -> str:
return "<redacted>"
def assignment_payload(assignment: Assignment, *, redact_values: bool = False) -> dict[str, Any]:
payload = {
"target_id": assignment.target_id,
"repository": assignment.repository,
"variable_scope": assignment.variable_scope,
"environment": assignment.environment,
"name": assignment.name,
"action": "delete" if assignment.deletes_variable else "set",
"value": redacted_value() if redact_values else assignment.value,
}
if redact_values:
payload["value_redacted"] = True
return payload
def compact_json(value: Any) -> str:
return json.dumps(value, ensure_ascii=False, separators=(",", ":"))
def runtime_target_fingerprint(runtime_target: dict[str, Any]) -> str:
"""Fingerprint a frozen target excluding only its current continuity state."""
payload = dict(runtime_target)
payload.pop("live_continuity", None)
encoded = json.dumps(payload, ensure_ascii=True, sort_keys=True, separators=(",", ":"))
return hashlib.sha256(encoded.encode("utf-8")).hexdigest()
def env_string(value: Any) -> str:
if isinstance(value, str):
return value
if isinstance(value, bool):
return "true" if value else "false"
if isinstance(value, (dict, list)):
return compact_json(value)
if value is None:
return ""
return str(value)
def is_repository_name(value: str) -> bool:
if not isinstance(value, str) or "/" not in value or len(value) > 160:
return False
allowed = set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-")
parts = value.split("/", 1)
return all(part and set(part) <= allowed for part in parts)
def platform_repositories(env: dict[str, str] | None = None) -> dict[str, str]:
env = env or os.environ
repositories = {platform: config["repository"] for platform, config in SUPPORTED_PLATFORMS.items()}
raw_json = str(env.get("RUNTIME_SETTINGS_PLATFORM_REPOSITORIES_JSON") or "").strip()
if raw_json:
try:
payload = json.loads(raw_json)
except json.JSONDecodeError as exc:
raise ValueError("RUNTIME_SETTINGS_PLATFORM_REPOSITORIES_JSON must be valid JSON") from exc
if not isinstance(payload, dict):
raise ValueError("RUNTIME_SETTINGS_PLATFORM_REPOSITORIES_JSON must be a JSON object")
for platform, repository in payload.items():
if platform not in SUPPORTED_PLATFORMS:
raise ValueError(f"unsupported platform repository override: {platform}")
repository = str(repository or "").strip()
if not is_repository_name(repository):
raise ValueError(f"repository override for {platform} must be owner/repo")
repositories[platform] = repository
for platform, env_name in PLATFORM_REPOSITORY_ENV.items():
repository = str(env.get(env_name) or "").strip()
if not repository:
continue
if not is_repository_name(repository):
raise ValueError(f"{env_name} must be owner/repo")
repositories[platform] = repository
return repositories
def platform_repository(platform: str, env: dict[str, str] | None = None) -> str:
if platform not in SUPPORTED_PLATFORMS:
raise ValueError(f"unsupported platform: {platform}")
return platform_repositories(env)[platform]
def platform_settings_activation(platform: str) -> str:
if platform not in SUPPORTED_PLATFORMS:
raise ValueError(f"unsupported platform: {platform}")
try:
payload = json.loads(PLATFORM_CONFIG_PATH.read_text(encoding="utf-8"))
value = payload["platforms"][platform]["deployment"]["settings_activation"]
except (KeyError, TypeError, json.JSONDecodeError) as exc:
raise ValueError(f"platform {platform} settings_activation is not configured") from exc
value = str(value or "").strip()
if not value:
raise ValueError(f"platform {platform} settings_activation is not configured")
return value
def discover_target_paths(paths: list[str]) -> list[Path]:
if paths:
return [Path(path).resolve() for path in paths]
local_targets = sorted(LOCAL_TARGETS_DIR.glob("*/*.json"))
if local_targets:
return local_targets
return sorted(EXAMPLE_TARGETS_DIR.glob("*/*.json"))
def load_target(path: Path) -> dict[str, Any]:
with path.open("r", encoding="utf-8") as handle:
return json.load(handle)
def display_path(path: Path) -> str:
try:
return str(path.relative_to(ROOT))
except ValueError:
return str(path)
def load_local_policy() -> dict[str, Any]:
if not LOCAL_POLICY_PATH.exists():
return {}
with LOCAL_POLICY_PATH.open("r", encoding="utf-8") as handle:
policy = json.load(handle)
if not isinstance(policy, dict):
raise ValueError("local/policy.json must contain a JSON object")
return policy
def load_platform_config() -> dict[str, Any]:
if not PLATFORM_CONFIG_PATH.exists():
return {}
with PLATFORM_CONFIG_PATH.open("r", encoding="utf-8") as handle:
config = json.load(handle)
return config if isinstance(config, dict) else {}
def target_path_id(path: Path) -> str | None:
relative = None
for base in (LOCAL_TARGETS_DIR, EXAMPLE_TARGETS_DIR):
try:
relative = path.resolve().relative_to(base)
break
except ValueError:
continue
if relative is None:
return None
if relative.suffix != ".json" or len(relative.parts) != 2:
return None
stem = relative.stem.removesuffix(".example")
return f"{relative.parent.as_posix()}/{stem}"
def is_secret_variable_name(name: str) -> bool:
upper_name = name.upper()
allowed_secret_pointer_suffixes = (
"_SECRET_ID",
"_SECRET_NAME",
"_SECRET_REF",
"_SECRET_RESOURCE",
"_SECRET_RESOURCE_NAME",
"_SECRET_VERSION",
)
if upper_name.endswith(allowed_secret_pointer_suffixes):
return False
return any(marker in upper_name for marker in SECRET_MARKERS)
def validate_github(target: dict[str, Any], errors: list[str]) -> None:
github = target.get("github")
if not isinstance(github, dict):
errors.append("github must be an object")
return
repository = github.get("repository")
if not isinstance(repository, str) or "/" not in repository:
errors.append("github.repository must be owner/repo")
scope = github.get("variable_scope")
if scope not in {"repository", "environment"}:
errors.append("github.variable_scope must be repository or environment")
if scope == "environment" and not str(github.get("environment") or "").strip():
errors.append("github.environment is required for environment variable scope")
if scope == "repository" and github.get("environment"):
errors.append("github.environment must be omitted for repository variable scope")
def validate_runtime_target(target: dict[str, Any], errors: list[str]) -> None:
runtime_target = target.get("runtime_target")
if not isinstance(runtime_target, dict):
errors.append("runtime_target must be an object")
return
for field in RUNTIME_REQUIRED_FIELDS:
if field not in runtime_target:
errors.append(f"runtime_target.{field} is required")
platform_id = runtime_target.get("platform_id")
if platform_id not in SUPPORTED_PLATFORMS:
errors.append(f"runtime_target.platform_id is unsupported: {platform_id!r}")
strategy_profile = runtime_target.get("strategy_profile")
if not isinstance(strategy_profile, str) or not strategy_profile.strip():
errors.append("runtime_target.strategy_profile must be a non-empty string")
if not isinstance(runtime_target.get("dry_run_only"), bool):
errors.append("runtime_target.dry_run_only must be boolean")
account_selector = runtime_target.get("account_selector")
if not isinstance(account_selector, list) or not account_selector:
errors.append("runtime_target.account_selector must be a non-empty list")
elif not all(isinstance(item, str) and item.strip() for item in account_selector):
errors.append("runtime_target.account_selector must only contain non-empty strings")
execution_mode = runtime_target.get("execution_mode")
if execution_mode not in {"live", "paper", "dry_run"}:
errors.append("runtime_target.execution_mode must be live, paper, or dry_run")
else:
dry_run_only = runtime_target.get("dry_run_only")
if execution_mode == "live" and dry_run_only is not False:
errors.append("runtime_target.execution_mode live requires dry_run_only false")
if execution_mode == "dry_run" and dry_run_only is not True:
errors.append("runtime_target.execution_mode dry_run requires dry_run_only true")
validate_runtime_target_strategy_policy(runtime_target, errors)
execution_windows = runtime_target.get("execution_windows")
if execution_windows is not None:
if not isinstance(execution_windows, dict):
errors.append("runtime_target.execution_windows must be an object when present")
else:
for window_name, allowed_modes in WINDOW_MODES.items():
window = execution_windows.get(window_name)
if window is None:
continue
if not isinstance(window, dict):
errors.append(f"runtime_target.execution_windows.{window_name} must be an object")
continue
for field in window:
if field not in {"enabled", "offset_minutes", "mode"}:
errors.append(f"runtime_target.execution_windows.{window_name}.{field} is unsupported")
if "enabled" in window and not isinstance(window["enabled"], bool):
errors.append(f"runtime_target.execution_windows.{window_name}.enabled must be boolean")
if "offset_minutes" in window:
offset_minutes = window["offset_minutes"]
if not isinstance(offset_minutes, int) or offset_minutes < 0:
errors.append(
"runtime_target.execution_windows."
f"{window_name}.offset_minutes must be a non-negative integer"
)
mode = window.get("mode")
if mode is not None and mode not in allowed_modes:
errors.append(
f"runtime_target.execution_windows.{window_name}.mode must be one of {sorted(allowed_modes)}"
)
for window_name in execution_windows:
if window_name not in WINDOW_MODES:
errors.append("runtime_target.execution_windows only supports precheck and execution")
break
scheduler = runtime_target.get("scheduler")
if scheduler is not None:
if not isinstance(scheduler, dict):
errors.append("runtime_target.scheduler must be an object when present")
else:
for field in scheduler:
if field not in SCHEDULER_FIELDS:
errors.append(f"runtime_target.scheduler.{field} is unsupported")
timezone = scheduler.get("timezone")
if not isinstance(timezone, str) or not timezone.strip():
errors.append("runtime_target.scheduler.timezone must be a non-empty string")
for field in ("main_time", "probe_time", "precheck_time"):
value = scheduler.get(field)
if not isinstance(value, str) or len(value.split()) not in {2, 5}:
errors.append(f"runtime_target.scheduler.{field} must have 2 time fields or 5 cron fields")
validate_live_ibkr_us_scheduler(runtime_target, scheduler, errors)
configured_market_fields = []
for field in MARKET_FIELDS:
value = runtime_target.get(field)
if value is not None:
configured_market_fields.append(field)
if value is not None and (not isinstance(value, str) or not value.strip()):
errors.append(f"runtime_target.{field} must be a non-empty string when present")
if configured_market_fields and len(configured_market_fields) != len(MARKET_FIELDS):
errors.append(
"runtime_target market metadata must include "
"market, market_calendar, and market_timezone together"
)
market_timezone = runtime_target.get("market_timezone")
if isinstance(market_timezone, str) and market_timezone.strip():
try:
ZoneInfo(market_timezone)
except (ZoneInfoNotFoundError, ValueError):
errors.append(f"runtime_target.market_timezone is invalid: {market_timezone!r}")
validate_strategy_release(runtime_target, errors)
validate_live_continuity(runtime_target, errors)
def validate_strategy_release(runtime_target: dict[str, Any], errors: list[str]) -> None:
"""Validate an optional immutable release identity without enabling it.
Existing targets intentionally remain valid without ``strategy_release``
during the read-only migration. Once present, however, a partial identity
is never accepted because it could be mistaken for a verified release.
"""
release = runtime_target.get("strategy_release")
if release is None:
return
if not isinstance(release, dict):
errors.append("runtime_target.strategy_release must be an object when present")
return
unexpected = sorted(set(release) - set(STRATEGY_RELEASE_REQUIRED_FIELDS))
if unexpected:
errors.append(
"runtime_target.strategy_release contains unsupported fields: "
+ ", ".join(unexpected)
)
for field in STRATEGY_RELEASE_REQUIRED_FIELDS:
value = release.get(field)
if not isinstance(value, str) or not value.strip():
errors.append(f"runtime_target.strategy_release.{field} is required")
continue
if field == "release_id" and not STRATEGY_RELEASE_ID_PATTERN.fullmatch(value.strip()):
errors.append("runtime_target.strategy_release.release_id has invalid characters")
if field in STRATEGY_RELEASE_DIGEST_FIELDS and not SHA256_PATTERN.fullmatch(value.strip()):
errors.append(f"runtime_target.strategy_release.{field} must be a SHA-256 digest")
if field == "effective_session":
try:
date.fromisoformat(value.strip())
except ValueError:
errors.append(
"runtime_target.strategy_release.effective_session must be an ISO-8601 date"
)
def validate_live_continuity(runtime_target: dict[str, Any], errors: list[str]) -> None:
"""Validate a frozen incumbent baseline independently of candidate policy."""
continuity = runtime_target.get("live_continuity")
if continuity is None:
return
if not isinstance(continuity, dict):
errors.append("runtime_target.live_continuity must be an object when present")
return
unsupported = sorted(set(continuity) - LIVE_CONTINUITY_FIELDS)
if unsupported:
errors.append(
"runtime_target.live_continuity contains unsupported fields: " + ", ".join(unsupported)
)
for field in sorted(LIVE_CONTINUITY_FIELDS):
value = continuity.get(field)
if not isinstance(value, str) or not value.strip():
errors.append(f"runtime_target.live_continuity.{field} is required")
state = str(continuity.get("state") or "").strip().upper()
if state not in LIVE_CONTINUITY_STATES:
errors.append(
"runtime_target.live_continuity.state must be one of "
+ ", ".join(sorted(LIVE_CONTINUITY_STATES))
)
baseline_kind = str(continuity.get("baseline_kind") or "").strip()
if baseline_kind not in LIVE_CONTINUITY_BASELINE_KINDS:
errors.append(
"runtime_target.live_continuity.baseline_kind must be one of "
+ ", ".join(sorted(LIVE_CONTINUITY_BASELINE_KINDS))
)
baseline_id = str(continuity.get("baseline_id") or "").strip()
if baseline_id and not LIVE_CONTINUITY_BASELINE_ID_PATTERN.fullmatch(baseline_id):
errors.append("runtime_target.live_continuity.baseline_id has invalid characters")
digest = str(continuity.get("baseline_target_sha256") or "").strip().lower()
digest = digest.removeprefix("sha256:")
if not SHA256_PATTERN.fullmatch(digest):
errors.append("runtime_target.live_continuity.baseline_target_sha256 must be a SHA-256 digest")
elif digest != runtime_target_fingerprint(runtime_target):
errors.append(
"runtime_target.live_continuity.baseline_target_sha256 does not match the runtime target"
)
captured_at = str(continuity.get("captured_at") or "").strip()
if captured_at:
try:
date.fromisoformat(captured_at)
except ValueError:
errors.append("runtime_target.live_continuity.captured_at must be an ISO-8601 date")
if baseline_kind == "release_attested" and not isinstance(runtime_target.get("strategy_release"), dict):
errors.append("release_attested live_continuity requires runtime_target.strategy_release")
def is_live_continuity_target(runtime_target: dict[str, Any]) -> bool:
return isinstance(runtime_target.get("live_continuity"), dict)
def validate_live_ibkr_us_scheduler(
runtime_target: dict[str, Any],
scheduler: dict[str, Any],
errors: list[str],
) -> None:
if (
runtime_target.get("platform_id") != "ibkr"
or runtime_target.get("execution_mode") != "live"
or runtime_target.get("dry_run_only") is not False
):
return
market = str(runtime_target.get("market") or "").strip().upper()
if not market:
config = load_platform_config()
strategies = config.get("strategies", {})
domains = config.get("domains", {})
profile = str(runtime_target.get("strategy_profile") or "").strip()
strategy = strategies.get(profile) if isinstance(strategies, dict) else None
domain = (
domains.get(strategy.get("domain"))
if isinstance(strategy, dict) and isinstance(domains, dict)
else None
)
market = str(domain.get("market") or "").strip().upper() if isinstance(domain, dict) else ""
if market != "US":
return
for field in ("main_time", "probe_time", "precheck_time"):
value = scheduler.get(field)
if not isinstance(value, str):
continue
cron = value.split()
if len(cron) == 2:
continue
if len(cron) == 5 and cron[2] == "*" and cron[4] == "1-5":
continue
errors.append(
f"runtime_target.scheduler.{field} must be a two-field time or Mon-Fri cron "
"for live IBKR US targets"
)
def validate_runtime_target_strategy_policy(runtime_target: dict[str, Any], errors: list[str]) -> None:
config = load_platform_config()
strategies = config.get("strategies", {})
platforms = config.get("platforms", {})
domains = config.get("domains", {})
if not isinstance(strategies, dict) or not isinstance(platforms, dict) or not isinstance(domains, dict):
return
profile = str(runtime_target.get("strategy_profile") or "").strip()
strategy = strategies.get(profile)
if not isinstance(strategy, dict):
return
platform_id = str(runtime_target.get("platform_id") or "").strip()
platform = platforms.get(platform_id)
domain = str(strategy.get("domain") or "").strip()
domain_config = domains.get(domain)
supported_domains = platform.get("supported_domains", []) if isinstance(platform, dict) else []
if domain and isinstance(supported_domains, list) and supported_domains and domain not in supported_domains:
errors.append(f"runtime_target.strategy_profile domain {domain} is not supported by {platform_id}")
if isinstance(domain_config, dict):
if any(runtime_target.get(field) is not None for field in MARKET_FIELDS):
for field in MARKET_FIELDS:
actual = runtime_target.get(field)
expected = domain_config.get(field)
if isinstance(actual, str) and actual.strip() and actual.strip() != expected:
errors.append(
f"runtime_target.{field} must match strategy domain {domain}: expected {expected!r}"
)
scheduler = runtime_target.get("scheduler")
if isinstance(scheduler, dict):
actual_timezone = scheduler.get("timezone")
expected_timezone = domain_config.get("market_timezone")
if (
isinstance(actual_timezone, str)
and actual_timezone.strip()
and actual_timezone.strip() != expected_timezone
):
errors.append(
"runtime_target.scheduler.timezone must match strategy domain "
f"{domain}: expected {expected_timezone!r}"
)
execution_mode = effective_execution_mode(runtime_target)
deployment = platform.get("deployment", {}) if isinstance(platform, dict) else {}
if (
execution_mode == "live"
and isinstance(deployment, dict)
and deployment.get("live_configured") is False
):
errors.append(f"platform {platform_id} has no live runtime configuration")
supported_execution_modes = (
normalize_allowed_execution_modes(deployment.get("supported_execution_modes"))
if isinstance(deployment, dict)
else []
)
if supported_execution_modes and execution_mode not in supported_execution_modes:
errors.append(
f"platform {platform_id} does not support {execution_mode} control execution"
)
continuity_target = is_live_continuity_target(runtime_target)
allowed_modes = normalize_allowed_execution_modes(strategy.get("allowed_execution_modes"))
if allowed_modes and execution_mode not in allowed_modes and not continuity_target:
errors.append(f"runtime_target.strategy_profile {profile} does not allow {execution_mode} execution")
if execution_mode != "live":
return
if continuity_target:
continuity_policy = strategy.get("live_continuity")
if not isinstance(continuity_policy, dict) or continuity_policy.get("eligible") is not True:
errors.append(
f"runtime_target.strategy_profile {profile} is not eligible for live continuity"
)
return
allowed_platforms = continuity_policy.get("allowed_platforms")
if not isinstance(allowed_platforms, list) or platform_id not in allowed_platforms:
errors.append(
f"runtime_target.strategy_profile {profile} live continuity is not allowed on {platform_id}"
)
return
lifecycle_stage = str(strategy.get("lifecycle_stage") or "").strip()
if strategy.get("runtime_enabled") is not True:
errors.append(f"runtime_target.strategy_profile {profile} is not runtime_enabled")
if strategy.get("can_switch_live") is not True:
errors.append(f"runtime_target.strategy_profile {profile} cannot switch live")
if lifecycle_stage not in {"live_enabled", "runtime_enabled"}:
errors.append(
f"runtime_target.strategy_profile {profile} lifecycle_stage must be "
"live_enabled (or legacy runtime_enabled) for live"
)
if "live" not in allowed_modes:
errors.append(
f"runtime_target.strategy_profile {profile} must explicitly allow live execution"
)
blocked_reason = str(strategy.get("blocked_live_reason") or "").strip()
if blocked_reason:
errors.append(f"runtime_target.strategy_profile {profile} is blocked for live: {blocked_reason}")
def normalize_allowed_execution_modes(value: Any) -> list[str]:
if isinstance(value, str):
raw_modes = re.split(r"[,\s/|]+", value)
elif isinstance(value, (list, tuple, set)):
raw_modes = list(value)
else:
return []
modes: list[str] = []
for item in raw_modes:
mode = str(item or "").strip().lower()
if mode and mode not in modes:
modes.append(mode)
return modes
def effective_execution_mode(runtime_target: dict[str, Any]) -> str:
"""Return the policy mode without changing a legacy serialized target.
``execution_mode=paper`` combined with ``dry_run_only=true`` is the
deployed no-order envelope, not a P4 paper-broker authorization. The
manual switch accepts canonical ``dry_run`` and serializes that envelope
until every platform parser has migrated.
"""
mode = str(runtime_target.get("execution_mode") or "").strip().lower()
if runtime_target.get("dry_run_only") is True and mode in {"paper", "dry_run"}:
return "dry_run"
return mode
def validate_plugin_mounts(target: dict[str, Any], errors: list[str]) -> None:
runtime_target = target.get("runtime_target") if isinstance(target.get("runtime_target"), dict) else {}
strategy_profile = runtime_target.get("strategy_profile")
platform_id = runtime_target.get("platform_id")
mounts_variable = target.get("plugin_mounts_variable")
mounts = target.get("plugin_mounts", [])
if mounts_variable is not None:
if not isinstance(mounts_variable, str) or not mounts_variable.strip():
errors.append("plugin_mounts_variable must be a non-empty string when present")
elif platform_id in SUPPORTED_PLATFORMS:
expected_prefix = SUPPORTED_PLATFORMS[platform_id]["plugin_mounts_prefix"]
if not mounts_variable.startswith(expected_prefix):
errors.append(f"plugin_mounts_variable should start with {expected_prefix!r} for {platform_id}")
if mounts_variable is None and mounts:
errors.append("plugin_mounts_variable is required when plugin_mounts are present")
return
if not isinstance(mounts, list):
errors.append("plugin_mounts must be a list")
return
matching_plugins: set[str] = set()
for index, mount in enumerate(mounts):
if not isinstance(mount, dict):
errors.append(f"plugin_mounts[{index}] must be an object")
continue
for field in ("strategy", "plugin", "signal_path", "enabled", "expected_mode"):
if field not in mount:
errors.append(f"plugin_mounts[{index}].{field} is required")
if not isinstance(mount.get("enabled"), bool):
errors.append(f"plugin_mounts[{index}].enabled must be boolean")
expected_schema_version = mount.get("expected_schema_version")
if expected_schema_version is not None and (
not isinstance(expected_schema_version, str) or not expected_schema_version.strip()
):
errors.append(f"plugin_mounts[{index}].expected_schema_version must be a non-empty string")
expected_mode = mount.get("expected_mode")
if not isinstance(expected_mode, str) or expected_mode not in {"dry_run", "paper", "shadow"}:
errors.append(
f"plugin_mounts[{index}].expected_mode must be dry_run, paper, or shadow; plugins cannot request live execution"
)
signal_path = mount.get("signal_path")
if not isinstance(signal_path, str) or not signal_path.startswith("gs://"):
errors.append(f"plugin_mounts[{index}].signal_path must be a gs:// URI")
if mount.get("strategy") == strategy_profile and mount.get("enabled") is True:
plugin = mount.get("plugin")
if isinstance(plugin, str):
matching_plugins.add(plugin)
policy = load_local_policy()
required_plugins_by_strategy = policy.get("required_plugins_by_strategy", {})
if required_plugins_by_strategy and not isinstance(required_plugins_by_strategy, dict):
errors.append("local policy required_plugins_by_strategy must be an object")
return
required_plugins = required_plugins_by_strategy.get(strategy_profile, [])
if isinstance(required_plugins, str):
required_plugins = [required_plugins]
if not isinstance(required_plugins, list):
errors.append(f"local policy required plugins for {strategy_profile} must be a list or string")
return
for plugin in required_plugins:
if plugin not in matching_plugins:
errors.append(f"{strategy_profile} requires an enabled {plugin} plugin mount")
def option_bool_value(value: Any) -> bool | None:
text = str(value if value is not None else "").strip().lower()
if text in {"1", "true", "yes", "y", "on"}:
return True
if text in {"0", "false", "no", "n", "off"}:
return False
return None
def validate_option_overlay_variables(extra_variables: dict[str, Any], errors: list[str]) -> None:
if not any(name in extra_variables for name in OPTION_OVERLAY_VARIABLES):
return
values = {
name: str(extra_variables.get(name) if extra_variables.get(name) is not None else "").strip()
for name in OPTION_OVERLAY_VARIABLES
}
for name in OPTION_OVERLAY_ENABLED_VARIABLES:
if values[name] and option_bool_value(values[name]) is None:
errors.append(f"extra_variables.{name} must be true or false")
for name in OPTION_OVERLAY_RECIPE_VARIABLES:
if values[name] and not re.fullmatch(r"[A-Za-z0-9._=-]{1,120}", values[name]):
errors.append(f"extra_variables.{name} must be a recipe slug")
for name in OPTION_OVERLAY_AMOUNT_VARIABLES:
if values[name] and not re.fullmatch(r"(?:\d+|\d*\.\d+)", values[name]):
errors.append(f"extra_variables.{name} must be a non-negative decimal")
for name in OPTION_OVERLAY_RATIO_VARIABLES:
if values[name]:
if not re.fullmatch(r"(?:\d+|\d*\.\d+)", values[name]):
errors.append(f"extra_variables.{name} must be a ratio between 0 and 1")
continue
numeric = float(values[name])
if numeric < 0 or numeric > 1:
errors.append(f"extra_variables.{name} must be a ratio between 0 and 1")
overlay_enabled = option_bool_value(values["OPTION_OVERLAY_ENABLED"]) if values["OPTION_OVERLAY_ENABLED"] else None
family_enabled: dict[str, bool | None] = {}
family_fields = {
"GROWTH": (
"OPTION_GROWTH_OVERLAY_ENABLED",
"OPTION_GROWTH_OVERLAY_RECIPE",
"OPTION_GROWTH_OVERLAY_START_USD",
"OPTION_GROWTH_OVERLAY_NAV_BUDGET_RATIO",
),
"INCOME": (
"OPTION_INCOME_OVERLAY_ENABLED",
"OPTION_INCOME_OVERLAY_RECIPE",
"OPTION_INCOME_OVERLAY_START_USD",
"OPTION_INCOME_OVERLAY_NAV_RISK_RATIO",
),
}
for family, (enabled_name, recipe_name, start_name, ratio_name) in family_fields.items():
enabled = option_bool_value(values[enabled_name]) if values[enabled_name] else None
family_enabled[family] = enabled
family_payload = [values[recipe_name], values[start_name], values[ratio_name]]
if enabled is True and not all(family_payload):
errors.append(f"extra_variables.{enabled_name} requires recipe, start_usd, and ratio fields")
if enabled is False and any(family_payload):
errors.append(f"extra_variables.{enabled_name} is false but {family.lower()} overlay fields are still set")
if overlay_enabled is True and not any(value is True for value in family_enabled.values()):
errors.append("extra_variables.OPTION_OVERLAY_ENABLED is true but no option overlay family is enabled")
if overlay_enabled is False and any(value is True for value in family_enabled.values()):
errors.append("extra_variables.OPTION_OVERLAY_ENABLED is false but an option overlay family is enabled")
def validate_extra_variables(target: dict[str, Any], errors: list[str]) -> None:
extra_variables = target.get("extra_variables", {})
if not isinstance(extra_variables, dict):
errors.append("extra_variables must be an object")
return
generated_names = set(GENERATED_VARIABLES)
plugin_mounts_variable = target.get("plugin_mounts_variable")
if isinstance(plugin_mounts_variable, str):
generated_names.add(plugin_mounts_variable)
for name, value in extra_variables.items():
if name in generated_names:
errors.append(f"extra_variables.{name} duplicates a generated variable")
if name in RESEARCH_ONLY_EXTRA_VARIABLES:
errors.append(f"extra_variables.{name} is research-only and must not be stored in live switch settings")
if is_secret_variable_name(name):
errors.append(f"extra_variables.{name} looks like a secret and must not be stored here")
if isinstance(value, str) and "\n" in value:
errors.append(f"extra_variables.{name} must be a single-line value")
validate_option_overlay_variables(extra_variables, errors)
runtime_target = target.get("runtime_target") if isinstance(target.get("runtime_target"), dict) else {}
dry_run_only = runtime_target.get("dry_run_only")
platform_id = runtime_target.get("platform_id")
dry_run_variable = PLATFORM_DRY_RUN_VARIABLES.get(str(platform_id or ""))
platform_dry_run = extra_variables.get(dry_run_variable) if dry_run_variable else None
if platform_dry_run is not None and env_string(platform_dry_run).lower() != env_string(dry_run_only):
errors.append(f"extra_variables.{dry_run_variable} must match runtime_target.dry_run_only")
def validate_repository_variables(target: dict[str, Any], errors: list[str]) -> None:
repository_variables = target.get("repository_variables", {})
if not isinstance(repository_variables, dict):
errors.append("repository_variables must be an object")
return
extra_variables = target.get("extra_variables")
extra_variable_names = set(extra_variables) if isinstance(extra_variables, dict) else set()
for name, value in repository_variables.items():
if name in GENERATED_VARIABLES:
errors.append(f"repository_variables.{name} duplicates a generated variable")
if name in extra_variable_names:
errors.append(f"repository_variables.{name} duplicates extra_variables.{name}")
if name in RESEARCH_ONLY_EXTRA_VARIABLES:
errors.append(
f"repository_variables.{name} is research-only and must not be stored in live switch settings"
)
if is_secret_variable_name(name):
errors.append(f"repository_variables.{name} looks like a secret and must not be stored here")
if isinstance(value, str) and "\n" in value:
errors.append(f"repository_variables.{name} must be a single-line value")
def validate_target(target: dict[str, Any], path: Path | None = None) -> list[str]:
errors: list[str] = []
target_id = target.get("target_id")
if not isinstance(target_id, str) or "/" not in target_id:
errors.append("target_id must be platform/name")
elif path is not None:
expected_id = target_path_id(path)
if expected_id and expected_id != target_id:
errors.append(f"target_id {target_id!r} does not match path id {expected_id!r}")
validate_github(target, errors)
validate_runtime_target(target, errors)
validate_plugin_mounts(target, errors)
validate_extra_variables(target, errors)
validate_repository_variables(target, errors)
runtime_target = target.get("runtime_target") if isinstance(target.get("runtime_target"), dict) else {}
github = target.get("github") if isinstance(target.get("github"), dict) else {}
platform_id = runtime_target.get("platform_id")
if platform_id in SUPPORTED_PLATFORMS:
try:
expected_repository = platform_repository(platform_id)
except ValueError as exc:
errors.append(str(exc))
else:
if github.get("repository") != expected_repository:
errors.append(
f"github.repository does not match platform {platform_id}: expected {expected_repository}"
)
return errors
def build_assignments(target: dict[str, Any]) -> list[Assignment]:
errors = validate_target(target)
if errors:
raise ValueError("; ".join(errors))
target_id = target["target_id"]
github = target["github"]
runtime_target = target["runtime_target"]
repository = github["repository"]
scope = github["variable_scope"]
environment = github.get("environment")
assignments = [
Assignment(target_id, repository, scope, environment, "RUNTIME_TARGET_JSON", compact_json(runtime_target)),
Assignment(target_id, repository, scope, environment, "STRATEGY_PROFILE", runtime_target["strategy_profile"]),
]