aether-quant 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. aether_quant-0.0.1.dist-info/METADATA +7 -0
  2. aether_quant-0.0.1.dist-info/RECORD +99 -0
  3. aether_quant-0.0.1.dist-info/WHEEL +5 -0
  4. aether_quant-0.0.1.dist-info/entry_points.txt +2 -0
  5. aether_quant-0.0.1.dist-info/licenses/LICENSE +133 -0
  6. aether_quant-0.0.1.dist-info/top_level.txt +13 -0
  7. analyzer/README.md +69 -0
  8. analyzer/__init__.py +17 -0
  9. analyzer/market_analyzer.py +521 -0
  10. aq_cli.py +3633 -0
  11. audit/README.md +47 -0
  12. audit/__init__.py +38 -0
  13. audit/hash_chain.py +52 -0
  14. audit/postgres_audit.py +61 -0
  15. audit/postgres_worker.py +402 -0
  16. audit/redis_queue.py +124 -0
  17. audit/status_export.py +62 -0
  18. data_pipeline/README.md +257 -0
  19. data_pipeline/__init__.py +6 -0
  20. data_pipeline/bar_synthesis.py +97 -0
  21. data_pipeline/dividend_backfill.py +243 -0
  22. data_pipeline/factor_file_backfill.py +276 -0
  23. data_pipeline/fetch.py +206 -0
  24. data_pipeline/fred_backfill.py +435 -0
  25. data_pipeline/ib_backfill.py +287 -0
  26. data_pipeline/v2_manifest.py +75 -0
  27. data_pipeline/yfinance_backfill.py +501 -0
  28. evaluation/README.md +152 -0
  29. evaluation/__init__.py +73 -0
  30. evaluation/ablation.py +310 -0
  31. evaluation/benchmark_comparison.py +195 -0
  32. evaluation/confidence_threshold_calibration.py +157 -0
  33. evaluation/factor_neutralization_check.py +94 -0
  34. evaluation/feature_parity.py +321 -0
  35. evaluation/feature_reconciliation.py +286 -0
  36. evaluation/impact_model.py +77 -0
  37. evaluation/kill_switch_replay.py +186 -0
  38. evaluation/limit_fill_simulator.py +152 -0
  39. evaluation/model_predictions.py +192 -0
  40. evaluation/monte_carlo.py +161 -0
  41. evaluation/public_benchmarks.py +117 -0
  42. evaluation/rank_book_simulator.py +494 -0
  43. evaluation/rank_ic_core.py +204 -0
  44. evaluation/rank_signal_calibration.py +690 -0
  45. evaluation/rolling_ic_gate_calibration.py +190 -0
  46. evaluation/rolling_ic_gate_replay.py +138 -0
  47. execution/README.md +281 -0
  48. execution/__init__.py +80 -0
  49. execution/config_cache.py +48 -0
  50. execution/cost_model.py +213 -0
  51. execution/lean_config_render.py +131 -0
  52. execution/live_credentials.py +38 -0
  53. execution/live_credentials_io.py +52 -0
  54. execution/order_gate.py +388 -0
  55. execution/paper_readiness.py +181 -0
  56. execution/paper_readiness_io.py +54 -0
  57. execution/paper_readiness_report.py +103 -0
  58. execution/paper_readiness_scheduler.py +137 -0
  59. execution/reconciliation.py +160 -0
  60. execution/runtime_config_io.py +38 -0
  61. execution/secret_scan.py +73 -0
  62. features/README.md +77 -0
  63. features/__init__.py +137 -0
  64. features/alt_data_features.py +154 -0
  65. features/bond_features.py +275 -0
  66. features/cross_asset_sensitivity.py +79 -0
  67. features/derivatives_macro_features.py +72 -0
  68. features/macro_features.py +79 -0
  69. features/options_greeks.py +329 -0
  70. features/sector_map.py +53 -0
  71. features/technical_indicators.py +185 -0
  72. generate_backtest_report.py +311 -0
  73. generate_evaluation_report.py +712 -0
  74. inference/README.md +105 -0
  75. inference/__init__.py +36 -0
  76. inference/exported_model.py +929 -0
  77. inference/parallel_inference.py +160 -0
  78. inference/strategy_selector_inference.py +75 -0
  79. json_safety.py +130 -0
  80. portfolio/README.md +447 -0
  81. portfolio/__init__.py +41 -0
  82. portfolio/book_construction.py +786 -0
  83. portfolio/book_neutrality.py +178 -0
  84. portfolio/hrp_allocation.py +159 -0
  85. portfolio/options_arbitrage_detector.py +232 -0
  86. portfolio/options_assignment_risk.py +119 -0
  87. portfolio/options_margin_sizing.py +333 -0
  88. portfolio/options_strategy.py +1999 -0
  89. portfolio/rank_signal.py +219 -0
  90. portfolio/rolling_ic_gate.py +183 -0
  91. risk/README.md +275 -0
  92. risk/__init__.py +38 -0
  93. risk/asset_class_router.py +584 -0
  94. risk/forex_risk.py +158 -0
  95. risk/futures_risk.py +202 -0
  96. risk/kill_switch.py +261 -0
  97. risk/manual_override.py +118 -0
  98. risk/position_sizing.py +303 -0
  99. risk/rl_sizing.py +189 -0
execution/__init__.py ADDED
@@ -0,0 +1,80 @@
1
+ from .cost_model import (
2
+ NetEdgeDecision,
3
+ build_net_edge_decision,
4
+ estimate_round_trip_cost_bps,
5
+ expected_edge_bps,
6
+ )
7
+ from .live_credentials import (
8
+ DEFAULT_DEV_DB_PASSWORD,
9
+ credentials_present,
10
+ describe_missing_fields,
11
+ postgres_dsn_is_live_safe,
12
+ )
13
+ from .order_gate import (
14
+ DEFAULT_FILL_SLIPPAGE_SOURCE,
15
+ MAX_LIQUIDITY_SLIPPAGE_BPS,
16
+ PENDING_ORDER_STATUS_NAMES,
17
+ TERMINAL_CANCELED_STATUS_NAMES,
18
+ TERMINAL_FILLED_STATUS_NAMES,
19
+ VALID_FILL_SLIPPAGE_SOURCES,
20
+ VALID_MODES,
21
+ classify_order_status,
22
+ is_real_order_placement,
23
+ liquidity_cost_fraction,
24
+ resolve_fill_slippage,
25
+ resolve_fill_slippage_source,
26
+ resolve_limit_order_timeout_action,
27
+ resolve_limit_price,
28
+ resolve_order_permission,
29
+ resolve_runtime_mode,
30
+ resolve_slippage_bps,
31
+ should_clear_pending_limit_order,
32
+ simulate_fill,
33
+ slippage_amount,
34
+ )
35
+ from .paper_readiness import (
36
+ evaluate_broker_config,
37
+ evaluate_live_broker_config,
38
+ evaluate_live_risk_posture,
39
+ evaluate_observation_readiness,
40
+ evaluate_paper_broker_config,
41
+ )
42
+ from .reconciliation import ReconciliationReport, reconcile_positions
43
+
44
+ __all__ = [
45
+ "NetEdgeDecision",
46
+ "build_net_edge_decision",
47
+ "estimate_round_trip_cost_bps",
48
+ "expected_edge_bps",
49
+ "DEFAULT_FILL_SLIPPAGE_SOURCE",
50
+ "MAX_LIQUIDITY_SLIPPAGE_BPS",
51
+ "PENDING_ORDER_STATUS_NAMES",
52
+ "TERMINAL_CANCELED_STATUS_NAMES",
53
+ "TERMINAL_FILLED_STATUS_NAMES",
54
+ "VALID_FILL_SLIPPAGE_SOURCES",
55
+ "VALID_MODES",
56
+ "classify_order_status",
57
+ "is_real_order_placement",
58
+ "liquidity_cost_fraction",
59
+ "resolve_fill_slippage",
60
+ "resolve_fill_slippage_source",
61
+ "resolve_limit_order_timeout_action",
62
+ "resolve_limit_price",
63
+ "resolve_order_permission",
64
+ "resolve_runtime_mode",
65
+ "resolve_slippage_bps",
66
+ "should_clear_pending_limit_order",
67
+ "simulate_fill",
68
+ "slippage_amount",
69
+ "evaluate_broker_config",
70
+ "evaluate_live_broker_config",
71
+ "evaluate_live_risk_posture",
72
+ "evaluate_observation_readiness",
73
+ "evaluate_paper_broker_config",
74
+ "credentials_present",
75
+ "describe_missing_fields",
76
+ "postgres_dsn_is_live_safe",
77
+ "DEFAULT_DEV_DB_PASSWORD",
78
+ "ReconciliationReport",
79
+ "reconcile_positions",
80
+ ]
@@ -0,0 +1,48 @@
1
+ """Mtime-gated read cache for config.json, shared by the per-bar/per-poll
2
+ config readers (risk/manual_override.py, execution/paper_readiness_io.py,
3
+ execution/runtime_config_io.py) that are all called far more often than
4
+ config.json actually changes - once per bar in a Lean backtest, once per
5
+ poll-loop iteration in retraining/worker.py.
6
+
7
+ The check-cadence in those callers is correct by design (see
8
+ main.py::_refresh_risk_state()'s own comments) - this module only removes
9
+ the redundant open()+json.load() cost of reading a file that, in practice,
10
+ almost never changes between calls.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ from pathlib import Path
16
+ from typing import Callable, TypeVar
17
+
18
+ T = TypeVar("T")
19
+
20
+ # Keyed by (config_path, loader), not just config_path - multiple distinct
21
+ # readers (read_manual_trade_lock_override, read_paper_trading_config,
22
+ # read_runtime_mode) all read the same config.json path with different
23
+ # loaders. Keying by path alone would let one reader's cached value leak
24
+ # into another reader's result whenever both are called in the same bar
25
+ # with the file's mtime unchanged in between - caught via a real Lean
26
+ # backtest run, not a unit test (each reader's tests use their own
27
+ # isolated tmp_path with only one loader ever touching it).
28
+ _cache: dict[tuple[Path, Callable], tuple[float, object]] = {}
29
+
30
+
31
+ def read_cached(config_path: Path, loader: Callable[[Path], T]) -> T:
32
+ """Returns loader(config_path)'s cached result if config_path's mtime
33
+ hasn't changed since the last call for this exact (path, loader) pair;
34
+ otherwise calls loader fresh and updates the cache. Falls back to
35
+ calling loader directly (bypassing the cache) if the file doesn't
36
+ exist, preserving loader's own missing-file handling."""
37
+ if not config_path.exists():
38
+ return loader(config_path)
39
+
40
+ mtime = config_path.stat().st_mtime
41
+ cache_key = (config_path, loader)
42
+ cached = _cache.get(cache_key)
43
+ if cached is not None and cached[0] == mtime:
44
+ return cached[1]
45
+
46
+ value = loader(config_path)
47
+ _cache[cache_key] = (mtime, value)
48
+ return value
@@ -0,0 +1,213 @@
1
+ """Expected-net-edge cost model (V5.1 Phase 0/1, item 3 of the V5.1
2
+ roadmap - the actual root cause of the fee drag: $2,769 in fees on $3,159
3
+ gross profit). Answers the one question nothing in this codebase asked
4
+ before: does this trade's expected edge clear its expected cost?
5
+
6
+ Deliberately its own module in the existing execution/ package (sibling to
7
+ order_gate.py, which already owns every other fill/slippage-cost function):
8
+ this is the ENTRY-DECISION side of cost-awareness (should we trade at all,
9
+ how much edge is left after cost), where order_gate.py is entirely the
10
+ FILL side (how the already-decided trade's price gets adjusted). Reuses
11
+ order_gate-adjacent liquidity data rather than recomputing it - see
12
+ estimate_round_trip_cost_bps()'s docstring.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import math
18
+ from dataclasses import asdict, dataclass
19
+
20
+
21
+ @dataclass(frozen=True)
22
+ class NetEdgeDecision:
23
+ expected_edge_bps: float
24
+ expected_cost_bps: float
25
+ net_edge_bps: float
26
+ passes: bool
27
+ reason: str
28
+
29
+ def to_dict(self) -> dict:
30
+ return asdict(self)
31
+
32
+
33
+ def estimate_round_trip_cost_bps(
34
+ liquidity_payload: dict,
35
+ *,
36
+ commission_bps_per_side: float,
37
+ min_commission_usd: float,
38
+ order_value: float,
39
+ extra_slippage_bps: float,
40
+ ) -> float:
41
+ """Reads liquidity_payload["estimated_round_trip_cost"] - already
42
+ computed by liquidity/market_liquidity.py::build_liquidity_decision()
43
+ (a fraction, e.g. 0.001 = 10bps) - and NEVER recomputes slippage; one
44
+ estimate per bar, consistently used everywhere (the same rule
45
+ main.py:1768-1771's own comment states). Adds the commission leg,
46
+ expressed as bps of order_value with a min_commission_usd dollar floor
47
+ (a small order can be commission-dominated even at a low bps rate),
48
+ plus any extra_slippage_bps buffer (a deliberately simple manual
49
+ safety margin, config-gated, defaults to 0.0).
50
+
51
+ order_value <= 0 (no order, or a zero-target-weight decision) returns
52
+ just the liquidity round-trip cost plus extra_slippage_bps - the
53
+ commission-floor conversion to bps is undefined at zero order value,
54
+ so it is skipped rather than divided-by-zero.
55
+
56
+ V5.4.5 (development/Problems.md #116): the commission leg now covers
57
+ BOTH sides of the round trip. The original math charged exactly one
58
+ side's commission (rate leg and min_commission_usd floor alike) against
59
+ a function whose contract - and this parameter's own name - is per-SIDE
60
+ cost for a ROUND-TRIP estimate, understating expected cost by up to 2x
61
+ and letting trades through whose true net edge was below the gate."""
62
+ round_trip_fraction = float(liquidity_payload.get("estimated_round_trip_cost", 0.0) or 0.0)
63
+ round_trip_bps = round_trip_fraction * 10_000.0
64
+
65
+ commission_bps_effective = 0.0
66
+ if order_value > 0.0:
67
+ commission_from_rate_usd = order_value * commission_bps_per_side / 10_000.0
68
+ # Each side's commission = max(rate leg, dollar floor); a round trip
69
+ # pays it twice (entry + exit), same notional both ways.
70
+ per_side_commission_usd = max(commission_from_rate_usd, min_commission_usd)
71
+ round_trip_commission_usd = 2.0 * per_side_commission_usd
72
+ commission_bps_effective = round_trip_commission_usd / order_value * 10_000.0
73
+
74
+ return round_trip_bps + commission_bps_effective + extra_slippage_bps
75
+
76
+
77
+ def expected_edge_bps(
78
+ predicted_rank: float | None,
79
+ *,
80
+ edge_bps_per_rank_unit: float,
81
+ holding_bars: int,
82
+ horizon_days: int,
83
+ trade_direction: int = 1,
84
+ ) -> float:
85
+ """Linear-in-rank-deviation expected edge estimate, measured IN THE
86
+ DIRECTION OF THE TRADE. rank_deviation = (rank - 0.5) * 2 is positive
87
+ for a top-ranked asset (expected to rise) and negative for a
88
+ bottom-ranked one (expected to fall) - `trade_direction` (+1 long, -1
89
+ short) says which side the caller actually intends to trade, so the
90
+ result is the edge realized if that trade is entered, not merely the
91
+ raw long-side edge.
92
+
93
+ A rank of 0.5 (median - no view) always has zero expected edge. A rank
94
+ of 1.0/0.0 has the full edge_bps_per_rank_unit magnitude, on whichever
95
+ side the rank itself favors (long the top, short the bottom) - the
96
+ book case, where trade_direction is derived from the same rank that
97
+ picked the side, so the product is always the FULL magnitude,
98
+ never negative. A mismatched trade (e.g. going long a bottom-ranked
99
+ asset) correctly comes out negative and gets vetoed downstream.
100
+
101
+ Scaled down by min(1, holding_bars/horizon_days) - the rank head's own
102
+ forward horizon (horizon_days, e.g. 20 for rank_20d) is how long the
103
+ FULL predicted move is expected to take; holding for fewer bars than
104
+ that only captures a fraction of it.
105
+
106
+ predicted_rank=None (model unavailable this bar) -> 0.0, the safe
107
+ no-edge default, matching every other "missing prediction never
108
+ changes trading behavior" contract in this codebase.
109
+
110
+ GUARDRAIL (Problems.md - net-edge gate blocked 100% of shorts): before
111
+ this parameter existed, the gate measured only the raw long-side edge
112
+ and applied it unconditionally to buy/sell/short decisions alike, so
113
+ every short entry (negative rank_deviation) was vetoed on every bar.
114
+ trade_direction must always be threaded through from the caller's own
115
+ signed intent (e.g. sign of the pre-cost target_weight) - never left
116
+ at the default 1 for a short trade."""
117
+ if predicted_rank is None:
118
+ return 0.0
119
+ rank_deviation = (float(predicted_rank) - 0.5) * 2.0
120
+ # V5.4.5: horizon_days <= 0 is a misconfiguration - previously the
121
+ # else-branch handed back the FULL edge fraction (1.0), maximizing
122
+ # exactly the trade the broken config should have neutralized. A
123
+ # horizon that can't be interpreted conservatively yields NO edge.
124
+ if horizon_days <= 0:
125
+ return 0.0
126
+ horizon_fraction = min(1.0, float(holding_bars) / float(horizon_days))
127
+ direction_sign = 1.0 if trade_direction >= 0 else -1.0
128
+ edge = edge_bps_per_rank_unit * rank_deviation * horizon_fraction * direction_sign
129
+ return edge if math.isfinite(edge) else 0.0
130
+
131
+
132
+ def build_net_edge_decision(
133
+ predicted_rank: float | None,
134
+ liquidity_payload: dict,
135
+ order_value: float,
136
+ cost_config: dict,
137
+ *,
138
+ trade_direction: int = 1,
139
+ ) -> NetEdgeDecision:
140
+ """The one call site analyzer/market_analyzer.py's Priority 6.5 tier
141
+ and risk/position_sizing.py::cost_sizing_multiplier() both consume.
142
+
143
+ `trade_direction` (+1 long, -1 short) must be the sign of the trade
144
+ actually being evaluated - see expected_edge_bps()'s docstring for why
145
+ this parameter exists at all. Defaults to 1 (long) only so callers
146
+ that genuinely never trade short (or existing tests exercising the
147
+ long side) do not need to pass it; every live call site must supply
148
+ the real signed direction.
149
+
150
+ FAIL-OPEN, always: returns passes=True, reason="net_edge_gate_disabled"
151
+ whenever cost_config["enabled"] is false, edge_bps_per_rank_unit is
152
+ 0.0 (uncalibrated - see `aq evaluate --calibrate-edge`), or
153
+ predicted_rank is None. An uncalibrated or disabled cost model must
154
+ never block a trade - the exact same "missing/degraded signal never
155
+ changes trading behavior" contract every other optional overlay in
156
+ this codebase (topology_sizing_multiplier(), rank_sizing_multiplier())
157
+ already follows."""
158
+ enabled = bool(cost_config.get("enabled", False))
159
+ edge_bps_per_rank_unit = float(cost_config.get("edge_bps_per_rank_unit", 0.0))
160
+
161
+ if not enabled or edge_bps_per_rank_unit == 0.0 or predicted_rank is None:
162
+ return NetEdgeDecision(
163
+ expected_edge_bps=0.0,
164
+ expected_cost_bps=0.0,
165
+ net_edge_bps=0.0,
166
+ passes=True,
167
+ reason="net_edge_gate_disabled",
168
+ )
169
+
170
+ expected_edge = expected_edge_bps(
171
+ predicted_rank,
172
+ edge_bps_per_rank_unit=edge_bps_per_rank_unit,
173
+ holding_bars=int(cost_config.get("holding_bars", 10)),
174
+ horizon_days=int(cost_config.get("horizon_days", 20)),
175
+ trade_direction=trade_direction,
176
+ )
177
+ expected_cost = estimate_round_trip_cost_bps(
178
+ liquidity_payload,
179
+ commission_bps_per_side=float(cost_config.get("commission_bps_per_side", 0.0)),
180
+ min_commission_usd=float(cost_config.get("min_commission_usd", 0.0)),
181
+ order_value=order_value,
182
+ extra_slippage_bps=float(cost_config.get("extra_slippage_bps", 0.0)),
183
+ )
184
+ # V5.4.5 (development/Problems.md #117): a NaN cost/edge previously
185
+ # flowed straight into the comparison - `nan >= threshold` is False, so
186
+ # the gate still failed closed but with the misleading generic
187
+ # "net_edge_below_min_threshold" reason (indistinguishable from an
188
+ # ordinary signal veto). NaN is now caught explicitly: fail CLOSED with
189
+ # a reason that names the real cause. Reported magnitudes stay finite
190
+ # (1e12 sentinel) so JSON state exports remain valid.
191
+ if not math.isfinite(expected_cost):
192
+ return NetEdgeDecision(
193
+ expected_edge_bps=expected_edge,
194
+ expected_cost_bps=1e12,
195
+ net_edge_bps=-1e12,
196
+ passes=False,
197
+ reason="non_finite_cost_estimate",
198
+ )
199
+ net_edge = expected_edge - expected_cost
200
+ min_net_edge_bps = float(cost_config.get("min_net_edge_bps", 0.0))
201
+ passes = math.isfinite(net_edge) and net_edge >= min_net_edge_bps
202
+
203
+ return NetEdgeDecision(
204
+ expected_edge_bps=expected_edge,
205
+ expected_cost_bps=expected_cost,
206
+ net_edge_bps=net_edge if math.isfinite(net_edge) else -1e12,
207
+ passes=passes,
208
+ reason=(
209
+ "net_edge_clears_cost" if passes
210
+ else "non_finite_expected_edge" if not math.isfinite(expected_edge)
211
+ else "net_edge_below_min_threshold"
212
+ ),
213
+ )
@@ -0,0 +1,131 @@
1
+ """Render a secret-bearing runtime Lean config from env vars (Phase V2-22 security).
2
+
3
+ The tracked `lean.json` is the full stock Lean template with EVERY brokerage/
4
+ API-secret field left empty (`ib-password`, `polygon-api-key`, … all `""`), so
5
+ it is safe to share - anyone who pulls the repo gets the usable config structure
6
+ but none of the keys. Lean itself does **not** expand environment variables
7
+ inside `lean.json` (verified against the Lean CLI's own
8
+ `components/config/lean_config_manager.py` - values are read literally), so
9
+ "put `${VAR}` in the field" does not work: the literal string would be sent to
10
+ the broker. Instead this module renders a gitignored `lean.live.json` that
11
+ overlays the real secret values (pulled from `.env.live` / `AETHER_*` env vars)
12
+ onto the empty template, and live/paper deployment points Lean at that rendered
13
+ file via `--lean-config`. Backtests are untouched - they use the plain, empty
14
+ `lean.json`.
15
+
16
+ Pure/IO split, matching execution/live_credentials(_io).py:
17
+ - `render_lean_config()` / `parse_env_file()` are pure (no disk, no os.environ,
18
+ never return secret VALUES - only the field NAMES that were filled).
19
+ - `load_env_file()` / `write_rendered_config()` are the thin IO wrappers.
20
+ """
21
+
22
+ from __future__ import annotations
23
+
24
+ import json
25
+ import os
26
+ from collections.abc import Mapping
27
+ from pathlib import Path
28
+
29
+ # lean.json field -> environment variable that supplies its value.
30
+ # Deliberately a small, explicit allow-list (IB live creds + the two data-feed
31
+ # keys this project actually uses) rather than "fill anything that looks like a
32
+ # secret" - rendering only touches fields we know how to source, and leaves the
33
+ # rest of the stock template untouched. Extend here (and in .env.live.example)
34
+ # when a new provider is adopted.
35
+ SECRET_ENV_MAP: dict[str, str] = {
36
+ "ib-account": "AETHER_IB_ACCOUNT",
37
+ "ib-user-name": "AETHER_IB_USER_NAME",
38
+ "ib-password": "AETHER_IB_PASSWORD",
39
+ "ib-trading-mode": "AETHER_IB_TRADING_MODE",
40
+ "polygon-api-key": "AETHER_POLYGON_API_KEY",
41
+ "iex-cloud-api-key": "AETHER_IEX_CLOUD_API_KEY",
42
+ }
43
+
44
+
45
+ def parse_env_file(text: str) -> dict[str, str]:
46
+ """Minimal `.env` parser (no python-dotenv dependency, mirroring this
47
+ codebase's deferred/optional-import style). Handles `KEY=VALUE`, blank
48
+ lines, `#` comments, surrounding quotes, and a leading `export `. Later
49
+ duplicate keys win. Never raises on a malformed line - it is skipped."""
50
+ result: dict[str, str] = {}
51
+ for raw in text.splitlines():
52
+ line = raw.strip()
53
+ if not line or line.startswith("#"):
54
+ continue
55
+ if line.startswith("export "):
56
+ line = line[len("export "):].lstrip()
57
+ if "=" not in line:
58
+ continue
59
+ key, value = line.split("=", 1)
60
+ key = key.strip()
61
+ if not key:
62
+ continue
63
+ value = value.strip()
64
+ if len(value) >= 2 and value[0] == value[-1] and value[0] in "\"'":
65
+ value = value[1:-1]
66
+ result[key] = value
67
+ return result
68
+
69
+
70
+ def render_lean_config(
71
+ base_config: Mapping[str, object], env: Mapping[str, str]
72
+ ) -> tuple[dict, list[str]]:
73
+ """Pure render. Returns `(rendered_config, filled_field_names)`.
74
+
75
+ Overlays onto a copy of `base_config` every `SECRET_ENV_MAP` field whose
76
+ env var is present and non-empty (after strip). Fields whose env var is
77
+ unset/blank are left exactly as they were in the template. The returned
78
+ list contains only the lean.json FIELD names that were populated - never
79
+ the secret values - so callers can safely log/report it.
80
+ """
81
+ rendered = dict(base_config)
82
+ filled: list[str] = []
83
+ for field, env_name in SECRET_ENV_MAP.items():
84
+ value = str(env.get(env_name, "")).strip()
85
+ if value:
86
+ rendered[field] = value
87
+ filled.append(field)
88
+ return rendered, filled
89
+
90
+
91
+ def load_env_file(path: str | Path) -> dict[str, str]:
92
+ """Read a `.env`-style file into a dict. Missing file -> empty dict."""
93
+ p = Path(path)
94
+ if not p.exists():
95
+ return {}
96
+ return parse_env_file(p.read_text(encoding="utf-8"))
97
+
98
+
99
+ def build_render_environment(
100
+ env_file: str | Path | None = None,
101
+ os_environ: Mapping[str, str] | None = None,
102
+ ) -> dict[str, str]:
103
+ """Merge a `.env.live` file (if given/exists) under the real process
104
+ environment - process env wins, so an explicitly-exported `AETHER_*`
105
+ overrides the file, matching docker-compose's own `.env` precedence."""
106
+ merged: dict[str, str] = {}
107
+ if env_file is not None:
108
+ merged.update(load_env_file(env_file))
109
+ merged.update(dict(os_environ if os_environ is not None else os.environ))
110
+ return merged
111
+
112
+
113
+ def write_rendered_config(
114
+ base_path: str | Path,
115
+ out_path: str | Path,
116
+ env: Mapping[str, str],
117
+ ) -> list[str]:
118
+ """Read the empty template at `base_path`, render with `env`, write the
119
+ secret-bearing result to `out_path` (pretty JSON). Returns the list of
120
+ filled field names (never values). Raises FileNotFoundError if the template
121
+ is missing - never silently writes an all-empty file that would look valid.
122
+ """
123
+ base = Path(base_path)
124
+ if not base.exists():
125
+ raise FileNotFoundError(f"Lean config template not found: {base}")
126
+ base_config = json.loads(base.read_text(encoding="utf-8"))
127
+ rendered, filled = render_lean_config(base_config, env)
128
+ Path(out_path).write_text(
129
+ json.dumps(rendered, indent=4) + "\n", encoding="utf-8"
130
+ )
131
+ return filled
@@ -0,0 +1,38 @@
1
+ """Pure validation over loaded live-broker credentials (Phase V2-22).
2
+
3
+ No I/O here on purpose - see execution/live_credentials_io.py for how
4
+ credentials actually get loaded (from ib_config.py or AETHER_IB_* env vars).
5
+ This module only decides whether what was loaded is complete enough to
6
+ consider live_credentials_present=True for execution.paper_readiness's
7
+ evaluate_live_broker_config().
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ REQUIRED_FIELDS = ("ib_account", "ib_user_name", "ib_password")
13
+
14
+ # The Postgres password docker-compose.yml / the DSN defaults ship with. Safe
15
+ # for local dev but published in the public repo, so it is effectively no
16
+ # password at all - live mode must refuse to run against it. Keep in sync with
17
+ # docker-compose.yml's ${POSTGRES_PASSWORD:-aether_dev_password} default.
18
+ DEFAULT_DEV_DB_PASSWORD = "aether_dev_password"
19
+
20
+
21
+ def credentials_present(credentials: dict) -> bool:
22
+ """True only if every field in REQUIRED_FIELDS is a non-empty string."""
23
+ return all(str(credentials.get(field, "")).strip() for field in REQUIRED_FIELDS)
24
+
25
+
26
+ def describe_missing_fields(credentials: dict) -> list[str]:
27
+ return [field for field in REQUIRED_FIELDS if not str(credentials.get(field, "")).strip()]
28
+
29
+
30
+ def postgres_dsn_is_live_safe(dsn: str) -> bool:
31
+ """False if the DSN is empty or still carries the published dev-default
32
+ password - either case is unacceptable for a live deployment. An empty DSN
33
+ is treated as unsafe so a missing/unconfigured value fails closed. A DSN
34
+ that simply omits a password (e.g. peer/trust auth) is considered safe -
35
+ only the known-published default is rejected."""
36
+ if not str(dsn).strip():
37
+ return False
38
+ return DEFAULT_DEV_DB_PASSWORD not in dsn
@@ -0,0 +1,52 @@
1
+ """IO layer for loading live-broker credentials (Phase V2-22).
2
+
3
+ Two equally-supported patterns, tried in order:
4
+ 1. `ib_config.py` (repo-root, gitignored, planned-but-absent per
5
+ .gitignore - a human creates it locally, never committed).
6
+ 2. AETHER_IB_ACCOUNT / AETHER_IB_USER_NAME / AETHER_IB_PASSWORD /
7
+ AETHER_IB_TRADING_MODE environment variables (populated from `.env.live`,
8
+ see .env.live.example).
9
+
10
+ This is pre-flight validation only, feeding execution.live_credentials's
11
+ credentials_present() - it does not wire Lean itself. The tracked `lean.json`
12
+ ships with ib-account/ib-user-name/ib-password/ib-trading-mode as empty
13
+ placeholders; real values are rendered into a gitignored `lean.live.json` by
14
+ scripts/render_lean_credentials.py (`aq render-lean-config`) from these same
15
+ AETHER_* vars, and Lean is pointed at that file via `--lean-config` (see
16
+ execution/lean_config_render.py and development/infrastructure.md's V2-22
17
+ runbook). Never raises - missing everything just yields all-empty-string values.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import os
23
+
24
+
25
+ def load_live_credentials() -> dict:
26
+ try:
27
+ import ib_config # type: ignore[import-not-found]
28
+
29
+ return {
30
+ "ib_account": str(getattr(ib_config, "IB_ACCOUNT", "")),
31
+ "ib_user_name": str(getattr(ib_config, "IB_USER_NAME", "")),
32
+ "ib_password": str(getattr(ib_config, "IB_PASSWORD", "")),
33
+ "ib_trading_mode": str(getattr(ib_config, "IB_TRADING_MODE", "paper")),
34
+ }
35
+ except ImportError:
36
+ pass
37
+
38
+ return {
39
+ "ib_account": os.environ.get("AETHER_IB_ACCOUNT", ""),
40
+ "ib_user_name": os.environ.get("AETHER_IB_USER_NAME", ""),
41
+ "ib_password": os.environ.get("AETHER_IB_PASSWORD", ""),
42
+ "ib_trading_mode": os.environ.get("AETHER_IB_TRADING_MODE", "paper"),
43
+ }
44
+
45
+
46
+ def load_postgres_dsn() -> str:
47
+ """The runtime Postgres DSN, read from AETHER_POSTGRES_DSN (the same var
48
+ docker-compose.yml injects). Empty string if unset. Fed to
49
+ execution.paper_readiness's live gate so a live run against the published
50
+ dev-default password fails closed - see
51
+ execution.live_credentials.postgres_dsn_is_live_safe()."""
52
+ return os.environ.get("AETHER_POSTGRES_DSN", "")