| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829 |
- from hermes_mcp.decision_engine import assess_wallet_state, make_decision, normalize_strategy_snapshot, score_strategy_fit
- def test_assess_wallet_state_marks_one_sided_wallet_as_critically_unbalanced():
- concern = {"account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- account_info = {
- "balances": [
- {"asset_code": "XRP", "available": 1},
- {"asset_code": "USD", "available": 1000},
- ]
- }
- wallet = assess_wallet_state(account_info=account_info, concern=concern, price=2.0)
- assert wallet["inventory_state"] == "critically_unbalanced"
- assert wallet["rebalance_needed"] is True
- assert wallet["grid_ready"] is False
- def test_assess_wallet_state_infers_base_and_quote_from_market_symbol_when_missing():
- concern = {"account_id": "a1", "market_symbol": "xrpusd", "base_currency": None, "quote_currency": None}
- account_info = {
- "balances": [
- {"asset_code": "XRP", "available": 64.68158},
- {"asset_code": "USD", "available": 12.64},
- ]
- }
- wallet = assess_wallet_state(account_info=account_info, concern=concern, price=1.318, strategies=[])
- assert wallet["base_currency"] == "XRP"
- assert wallet["quote_currency"] == "USD"
- assert wallet["base_available"] == 64.68158
- assert wallet["quote_available"] == 12.64
- def test_score_strategy_fit_penalizes_grid_when_wallet_unbalanced():
- strategy = normalize_strategy_snapshot({
- "id": "grid-1",
- "strategy_type": "grid_trader",
- "mode": "active",
- "account_id": "a1",
- "state": {},
- "config": {},
- })
- narrative = {"stance": "constructive_bullish", "opportunity_map": {"continuation": 0.7, "mean_reversion": 0.1, "reversal": 0.1, "wait": 0.1}}
- wallet_state = {"inventory_state": "critically_unbalanced", "rebalance_needed": True}
- fit = score_strategy_fit(strategy=strategy, narrative=narrative, wallet_state=wallet_state)
- assert fit["score"] < 0
- assert any("grid" in block or "wallet" in block for block in fit["blocks"])
- def test_score_strategy_fit_rewards_trend_when_breakout_is_confirmed():
- strategy = normalize_strategy_snapshot({
- "id": "trend-1",
- "strategy_type": "trend_follower",
- "mode": "off",
- "account_id": "a1",
- "state": {},
- "config": {},
- })
- base_narrative = {
- "stance": "constructive_bullish",
- "opportunity_map": {"continuation": 0.72, "mean_reversion": 0.08, "reversal": 0.05, "wait": 0.15},
- "grid_breakout_pressure": {"phase": "developing"},
- }
- confirmed_narrative = {
- **base_narrative,
- "grid_breakout_pressure": {"phase": "confirmed", "persistent": True},
- }
- wallet_state = {"inventory_state": "balanced", "rebalance_needed": False}
- base_fit = score_strategy_fit(strategy=strategy, narrative=base_narrative, wallet_state=wallet_state)
- confirmed_fit = score_strategy_fit(strategy=strategy, narrative=confirmed_narrative, wallet_state=wallet_state)
- assert confirmed_fit["score"] > base_fit["score"]
- assert any("confirmed breakout" in reason for reason in confirmed_fit["reasons"])
- def test_assess_wallet_state_counts_reserved_orders_in_effective_inventory():
- concern = {"account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- account_info = {
- "balances": [
- {"asset_code": "XRP", "available": 0},
- {"asset_code": "USD", "available": 0},
- ]
- }
- strategies = [
- {
- "id": "grid-1",
- "strategy_type": "grid_trader",
- "mode": "active",
- "account_id": "a1",
- "market_symbol": "xrpusd",
- "state": {
- "orders": [
- {"side": "sell", "status": "open", "amount": "10", "price": "1.50"},
- {"side": "buy", "status": "open", "amount": "10", "price": "1.40"},
- ]
- },
- }
- ]
- wallet = assess_wallet_state(account_info=account_info, concern=concern, price=1.45, strategies=strategies)
- assert wallet["inventory_state"] == "balanced"
- assert wallet["base_reserved"] == 10.0
- assert wallet["quote_reserved"] == 14.0
- assert wallet["base_effective"] == 10.0
- assert wallet["quote_effective"] == 14.0
- def test_make_decision_keeps_grid_when_imbalance_is_manageable():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.72,
- "opportunity_map": {"continuation": 0.75, "mean_reversion": 0.1, "reversal": 0.05, "wait": 0.1},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.8,
- "quote_ratio": 0.2,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "observe"
- assert decision.action == "keep_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_does_not_replace_grid_with_rebalancer_only_because_grid_mentions_handoff_readiness():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "cautious_bullish",
- "confidence": 0.74,
- "opportunity_map": {"continuation": 0.5, "mean_reversion": 0.25, "reversal": 0.05, "wait": 0.2},
- "scoped_state": {
- "micro": {"impulse": "mixed", "trend_bias": "mixed"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.64,
- "quote_ratio": 0.36,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": False, "side_capacity": {"buy": False, "sell": True}}}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "keep_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_replaces_grid_when_breakout_pressure_is_persistent():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.78,
- "opportunity_map": {"continuation": 0.72, "mean_reversion": 0.08, "reversal": 0.05, "wait": 0.15},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- }
- wallet_state = {
- "inventory_state": "critically_unbalanced",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.88,
- "quote_ratio": 0.12,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "replace_with_exposure_protector"
- assert decision.target_strategy == "protect-1"
- def test_make_decision_keeps_grid_when_critically_unbalanced_but_grid_still_has_working_side_capacity():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "cautious_bullish",
- "confidence": 0.7,
- "opportunity_map": {"continuation": 0.55, "mean_reversion": 0.2, "reversal": 0.05, "wait": 0.2},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "location": "upper_half", "reversal_risk": "low"},
- "meso": {"structure": "range", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- "features_by_timeframe": {"1m": {"raw": {"price": 1.4374}}},
- }
- wallet_state = {
- "inventory_state": "critically_unbalanced",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.86,
- "quote_ratio": 0.14,
- }
- strategies = [
- {
- "id": "grid-1",
- "strategy_type": "grid_trader",
- "mode": "active",
- "account_id": "a1",
- "state": {
- "last_price": 1.4374,
- "open_order_count": 4,
- "orders": [
- {"side": "sell", "status": "open", "price": "1.43956", "amount": "7"},
- {"side": "sell", "status": "open", "price": "1.44500", "amount": "7"},
- ],
- },
- "config": {},
- "report": {"supervision": {"capacity_available": False, "side_capacity": {"buy": False, "sell": True}, "inventory_pressure": "critical", "degraded": False}},
- },
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "observe"
- assert decision.action == "keep_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_keeps_grid_when_trend_has_only_eaten_two_levels():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.78,
- "opportunity_map": {"continuation": 0.72, "mean_reversion": 0.08, "reversal": 0.05, "wait": 0.15},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "location": "upper_half", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- "features_by_timeframe": {"1m": {"raw": {"price": 110.0}}},
- }
- wallet_state = {
- "inventory_state": "balanced",
- "rebalance_needed": False,
- "grid_ready": True,
- "base_ratio": 0.52,
- "quote_ratio": 0.48,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {"center_price": 100.0}, "config": {"grid_step_pct": 0.05}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "warn"
- assert decision.action == "keep_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_replaces_grid_when_third_level_is_sustained():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.82,
- "opportunity_map": {"continuation": 0.82, "mean_reversion": 0.05, "reversal": 0.03, "wait": 0.1},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "location": "near_upper_band", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- "features_by_timeframe": {"1m": {"raw": {"price": 116.0}}},
- }
- wallet_state = {
- "inventory_state": "balanced",
- "rebalance_needed": False,
- "grid_ready": True,
- "base_ratio": 0.52,
- "quote_ratio": 0.48,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {"center_price": 100.0}, "config": {"grid_step_pct": 0.05}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "act"
- assert decision.action == "replace_with_trend_follower"
- assert decision.target_strategy == "trend-1"
- assert decision.payload["grid_breakout_pressure"]["phase"] == "confirmed"
- def test_make_decision_marks_breakout_as_developing_under_partial_alignment():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "cautious_bullish",
- "confidence": 0.76,
- "opportunity_map": {"continuation": 0.62, "mean_reversion": 0.12, "reversal": 0.06, "wait": 0.2},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.74,
- "quote_ratio": 0.26,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": False, "side_capacity": {"buy": True, "sell": False}}}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "keep_grid"
- assert decision.payload["grid_breakout_pressure"]["phase"] == "developing"
- assert decision.reason_summary == "breakout pressure is developing, but grid can still work and should not be abandoned yet"
- def test_make_decision_argus_compression_stays_context_only():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.82,
- "opportunity_map": {"continuation": 0.82, "mean_reversion": 0.05, "reversal": 0.03, "wait": 0.1},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- "argus_context": {
- "regime": "compression",
- "regime_confidence": 0.72,
- "regime_components": {"compression": 0.81},
- },
- "features_by_timeframe": {"1m": {"raw": {"price": 112.0}}},
- }
- wallet_state = {
- "inventory_state": "balanced",
- "rebalance_needed": False,
- "grid_ready": True,
- "base_ratio": 0.52,
- "quote_ratio": 0.48,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {"center_price": 100.0}, "config": {"grid_step_pct": 0.05}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "keep_grid"
- assert decision.payload["grid_breakout_pressure"]["argus_compression_active"] is True
- assert decision.payload["grid_breakout_pressure"]["phase"] == "confirmed"
- assert decision.payload["argus_decision_context"]["compression_active"] is True
- def test_make_decision_promotes_developing_breakout_from_time_window_memory():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "generated_at": "2026-04-18T20:15:00+00:00",
- "stance": "cautious_bullish",
- "confidence": 0.76,
- "opportunity_map": {"continuation": 0.62, "mean_reversion": 0.12, "reversal": 0.06, "wait": 0.2},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.74,
- "quote_ratio": 0.26,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": False, "side_capacity": {"buy": True, "sell": False}}}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- history_window = {
- "window_seconds": 15 * 60,
- "recent_states": [
- {
- "created_at": "2026-04-18T20:06:00+00:00",
- "payload_json": '{"scoped_state":{"micro":{"impulse":"up","trend_bias":"bullish"},"meso":{"structure":"trend_continuation","momentum_bias":"bullish"},"macro":{"bias":"bullish"}},"cross_scope_summary":{"alignment":"partial_alignment","friction":"medium"}}',
- },
- {
- "created_at": "2026-04-18T20:10:30+00:00",
- "payload_json": '{"scoped_state":{"micro":{"impulse":"up","trend_bias":"bullish"},"meso":{"structure":"trend_continuation","momentum_bias":"bullish"},"macro":{"bias":"bullish"}},"cross_scope_summary":{"alignment":"partial_alignment","friction":"medium"}}',
- },
- ],
- }
- decision = make_decision(
- concern=concern,
- narrative_payload=narrative,
- wallet_state=wallet_state,
- strategies=strategies,
- history_window=history_window,
- )
- assert decision.payload["grid_breakout_pressure"]["phase"] == "confirmed"
- assert decision.payload["grid_breakout_pressure"]["time_window_memory"]["promoted_to_confirmed"] is True
- assert decision.payload["grid_breakout_pressure"]["time_window_memory"]["same_direction_seconds"] >= 540
- def test_make_decision_replaces_grid_with_trend_when_breakout_is_persistent_but_inventory_is_only_base_heavy():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.78,
- "opportunity_map": {"continuation": 0.72, "mean_reversion": 0.08, "reversal": 0.05, "wait": 0.15},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.64,
- "quote_ratio": 0.36,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": False, "side_capacity": {"buy": True, "sell": False}}}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "keep_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_prefers_active_grid_over_observe_trend_as_current_primary():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.72,
- "opportunity_map": {"continuation": 0.75, "mean_reversion": 0.1, "reversal": 0.05, "wait": 0.1},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.81,
- "quote_ratio": 0.19,
- }
- strategies = [
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "observe", "account_id": "a1", "state": {}, "config": {}},
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "keep_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_prefers_trend_over_rebalancer_on_bullish_breakout_with_depleted_base():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.9,
- "opportunity_map": {"continuation": 0.85, "mean_reversion": 0.05, "reversal": 0.02, "wait": 0.08},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- }
- wallet_state = {
- "inventory_state": "critically_unbalanced",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.0,
- "quote_ratio": 1.0,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "replace_with_exposure_protector"
- assert decision.target_strategy == "protect-1"
- def test_make_decision_replaces_grid_when_next_sell_is_close_but_confirmed_trend_handoff_is_ready():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.9,
- "opportunity_map": {"continuation": 0.85, "mean_reversion": 0.05, "reversal": 0.02, "wait": 0.08},
- "features_by_timeframe": {
- "1m": {"raw": {"price": 1.4374, "atr_percent": 0.11}},
- },
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.65,
- "quote_ratio": 0.35,
- }
- strategies = [
- {
- "id": "grid-1",
- "strategy_type": "grid_trader",
- "mode": "active",
- "account_id": "a1",
- "market_symbol": "xrpusd",
- "state": {
- "last_price": 1.4374,
- "center_price": 1.24,
- "orders": [
- {"side": "sell", "status": "open", "price": "1.43956", "amount": "7"},
- {"side": "buy", "status": "open", "price": "1.42523", "amount": "7"},
- ],
- },
- "config": {"grid_step_pct": 0.05},
- },
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "observe", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "replace_with_trend_follower"
- assert decision.target_strategy == "trend-1"
- assert decision.payload["grid_fill_context"]["near_fill_side"] == "sell"
- def test_make_decision_replaces_grid_when_time_promoted_confirmation_clears_lower_handoff_threshold():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "generated_at": "2026-04-18T20:15:00+00:00",
- "stance": "breakout_watch",
- "confidence": 0.78,
- "opportunity_map": {"continuation": 0.7, "mean_reversion": 0.1, "reversal": 0.04, "wait": 0.16},
- "features_by_timeframe": {
- "1m": {"raw": {"price": 111.0, "atr_percent": 0.35}},
- },
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.66,
- "quote_ratio": 0.34,
- }
- strategies = [
- {
- "id": "grid-1",
- "strategy_type": "grid_trader",
- "mode": "active",
- "account_id": "a1",
- "market_symbol": "xrpusd",
- "state": {"last_price": 111.0, "center_price": 100.0},
- "config": {"grid_step_pct": 0.05},
- },
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "observe", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- history_window = {
- "window_seconds": 15 * 60,
- "recent_states": [
- {
- "created_at": "2026-04-18T20:06:00+00:00",
- "payload_json": '{"scoped_state":{"micro":{"impulse":"up","trend_bias":"bullish"},"meso":{"structure":"trend_continuation","momentum_bias":"bullish"},"macro":{"bias":"bullish"}},"cross_scope_summary":{"alignment":"partial_alignment","friction":"medium"}}',
- },
- {
- "created_at": "2026-04-18T20:10:30+00:00",
- "payload_json": '{"scoped_state":{"micro":{"impulse":"up","trend_bias":"bullish"},"meso":{"structure":"trend_continuation","momentum_bias":"bullish"},"macro":{"bias":"bullish"}},"cross_scope_summary":{"alignment":"partial_alignment","friction":"medium"}}',
- },
- ],
- }
- decision = make_decision(
- concern=concern,
- narrative_payload=narrative,
- wallet_state=wallet_state,
- strategies=strategies,
- history_window=history_window,
- )
- assert decision.action == "replace_with_trend_follower"
- assert decision.target_strategy == "trend-1"
- assert decision.payload["grid_breakout_pressure"]["time_window_memory"]["promoted_to_confirmed"] is True
- def test_normalize_strategy_snapshot_uses_live_report_contract_and_supervision():
- normalized = normalize_strategy_snapshot({
- "id": "grid-1",
- "strategy_type": "grid_trader",
- "mode": "active",
- "account_id": "a1",
- "report": {
- "fit": {
- "role": "primary",
- "inventory_behavior": "balanced",
- "safe_when_unbalanced": False,
- "can_run_with": ["exposure_protector"],
- },
- "state": {"last_action": "hold", "open_order_count": 12},
- "supervision": {"inventory_pressure": "base_heavy", "capacity_available": False, "side_capacity": {"buy": True, "sell": False}, "degraded": False},
- },
- })
- assert normalized["contract"]["inventory_behavior"] == "balanced"
- assert normalized["supervision"]["capacity_available"] is False
- assert normalized["open_order_count"] == 12
- def test_make_decision_keeps_trend_during_directional_regime_even_if_wallet_is_skewed():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.7,
- "opportunity_map": {"continuation": 0.8, "mean_reversion": 0.1, "reversal": 0.05, "wait": 0.05},
- }
- wallet_state = {
- "inventory_state": "critically_unbalanced",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.88,
- "quote_ratio": 0.12,
- }
- strategies = [
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "observe"
- assert decision.action == "keep_trend"
- assert decision.target_strategy == "trend-1"
- def test_make_decision_replaces_trend_with_rebalancer_after_trend_cools_and_wallet_needs_repair():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "neutral_rotational",
- "confidence": 0.65,
- "opportunity_map": {"continuation": 0.15, "mean_reversion": 0.25, "reversal": 0.2, "wait": 0.4},
- }
- wallet_state = {
- "inventory_state": "critically_unbalanced",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.88,
- "quote_ratio": 0.12,
- }
- strategies = [
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "warn"
- assert decision.action == "keep_trend"
- assert decision.target_strategy == "trend-1"
- def test_make_decision_replaces_trend_with_rebalancer_on_edge_cooling_even_before_full_rotational_stance():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.74,
- "opportunity_map": {"continuation": 0.58, "mean_reversion": 0.12, "reversal": 0.08, "wait": 0.22},
- "scoped_state": {
- "micro": {"impulse": "mixed", "trend_bias": "mixed", "location": "near_upper_band"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.74,
- "quote_ratio": 0.26,
- }
- strategies = [
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "warn"
- assert decision.action == "keep_trend"
- assert decision.target_strategy == "trend-1"
- def test_make_decision_replaces_trend_with_rebalancer_when_micro_reversal_risk_spikes():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.76,
- "opportunity_map": {"continuation": 0.62, "mean_reversion": 0.1, "reversal": 0.18, "wait": 0.1},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "location": "near_upper_band", "reversal_risk": "high"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.72,
- "quote_ratio": 0.28,
- }
- strategies = [
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "warn"
- assert decision.action == "keep_trend"
- assert decision.target_strategy == "trend-1"
- def test_make_decision_replaces_rebalancer_with_grid_when_balanced_and_rotational():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "neutral_rotational",
- "confidence": 0.68,
- "opportunity_map": {"continuation": 0.15, "mean_reversion": 0.72, "reversal": 0.05, "wait": 0.08},
- }
- wallet_state = {
- "inventory_state": "balanced",
- "rebalance_needed": False,
- "grid_ready": True,
- "base_ratio": 0.49,
- "quote_ratio": 0.51,
- }
- strategies = [
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "act"
- assert decision.action == "replace_with_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_replaces_rebalancer_with_grid_when_trend_is_directional_but_not_sustained():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.72,
- "opportunity_map": {"continuation": 0.4, "mean_reversion": 0.4, "reversal": 0.08, "wait": 0.12},
- "scoped_state": {
- "micro": {"impulse": "mixed", "trend_bias": "mixed", "location": "centered", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- }
- wallet_state = {
- "inventory_state": "balanced",
- "rebalance_needed": False,
- "grid_ready": True,
- "base_ratio": 0.51,
- "quote_ratio": 0.49,
- }
- strategies = [
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "off", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": True, "side_capacity": {"buy": True, "sell": True}, "inventory_pressure": "balanced", "degraded": False}}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": True, "trend_strength": 0.58, "inventory_pressure": "balanced", "degraded": False}}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "act"
- assert decision.action == "replace_with_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_replaces_rebalancer_with_trend_when_breakout_is_still_strong():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.84,
- "opportunity_map": {"continuation": 0.82, "mean_reversion": 0.08, "reversal": 0.03, "wait": 0.07},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "location": "near_upper_band", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.74,
- "quote_ratio": 0.26,
- }
- strategies = [
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "off", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": True, "side_capacity": {"buy": True, "sell": True}, "inventory_pressure": "balanced", "degraded": False}}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": True, "trend_strength": 0.92, "inventory_pressure": "balanced", "degraded": False}}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "observe"
- assert decision.action == "keep_rebalancer"
- assert decision.target_strategy == "protect-1"
|