{
  "version": "position-manager.workflow.v1",
  "fixtures": [
    {
      "name": "startup_hydrated_position_queues_work_before_service_ready",
      "intent": "REST-hydrated open positions queue reconciliation before service readiness.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" },
      "initial": {
        "config": { "mode": "dry_run", "configuredSymbols": ["BTCUSDT"] },
        "accountState": { "serviceReady": false, "positions": [], "openOrders": [] },
        "orderContextStore": {},
        "pendingConfirmations": [],
        "clockMs": 0
      },
      "steps": [
        { "type": "rest_hydration", "subject": "startup", "payload": { "positions": [{ "symbol": "BTCUSDT", "managedSide": "LONG", "size": "0.01", "entryPrice": "65000" }], "openOrders": [] } },
        { "type": "start", "payload": { "privateStreamReady": false } },
        { "type": "planner_tick", "atMs": 1 }
      ],
      "expect": {
        "dirtyScopes": [{ "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" }],
        "phase": "protective",
        "logs": [{ "event": "startup_position_queued_before_service_ready" }],
        "accountState": { "serviceReady": false }
      },
      "forbid": ["exposure_increasing_dca_before_protection_trusted"]
    },
    {
      "name": "private_event_marks_dirty_and_schedules_reconcile",
      "intent": "Private account events update accountstate and schedule scoped workflow work.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" },
      "initial": {
        "config": { "quietWindowMs": 50, "maxDelayMs": 250, "configuredSymbols": ["BTCUSDT"] },
        "accountState": { "positions": [{ "symbol": "BTCUSDT", "managedSide": "LONG", "size": "0.01" }], "openOrders": [] },
        "orderContextStore": {},
        "pendingConfirmations": [],
        "clockMs": 0
      },
      "steps": [
        { "type": "private_event", "subject": "order", "payload": { "symbol": "BTCUSDT", "customOrderId": "oid-a", "status": "New" } },
        { "type": "timer", "atMs": 50, "payload": { "reason": "quiet_window" } }
      ],
      "expect": {
        "dirtyScopes": [{ "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" }],
        "phase": "protective",
        "logs": [{ "event": "private_event_applied" }, { "event": "reconcile_scheduled" }]
      },
      "forbid": ["dirty_scope_without_drain"]
    },
    {
      "name": "private_event_burst_debounces_until_quiet_window",
      "intent": "Event bursts update state immediately but trigger planning once after the quiet window.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" },
      "initial": {
        "config": { "quietWindowMs": 50, "maxDelayMs": 250 },
        "accountState": { "eventVersion": 0 },
        "orderContextStore": {},
        "pendingConfirmations": [],
        "clockMs": 0
      },
      "steps": [
        { "type": "private_event", "atMs": 1, "subject": "position", "payload": { "version": 1 } },
        { "type": "private_event", "atMs": 10, "subject": "position", "payload": { "version": 2 } },
        { "type": "private_event", "atMs": 20, "subject": "order", "payload": { "version": 3 } },
        { "type": "timer", "atMs": 70, "payload": { "reason": "quiet_window" } }
      ],
      "expect": {
        "dirtyScopes": [{ "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" }],
        "phase": "protective",
        "logs": [{ "event": "coalesced_reconcile", "rawEventCount": 3 }]
      },
      "forbid": ["planner_run_per_raw_event"]
    },
    {
      "name": "opaque_custom_id_registry_routes_exchange_echoes",
      "intent": "Opaque exchange custom IDs route through one order-context store.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG", "role": "TP" },
      "initial": {
        "config": { "customIds": "opaque_registry_only" },
        "accountState": { "openOrders": [] },
        "orderContextStore": { "oid-random-001": { "symbol": "BTCUSDT", "managedSide": "LONG", "role": "TP", "slotKey": "BTCUSDT:LONG:TP" } },
        "pendingConfirmations": [{ "customOrderId": "oid-random-001", "action": "place" }],
        "clockMs": 0
      },
      "steps": [
        { "type": "private_event", "subject": "order", "payload": { "customOrderId": "oid-random-001", "status": "New" } }
      ],
      "expect": {
        "dirtyScopes": [{ "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG", "role": "TP" }],
        "pendingConfirmations": [],
        "logs": [{ "event": "custom_id_registry_lookup_hit", "role": "TP" }]
      },
      "forbid": ["parse_slot_state_from_custom_id"]
    },
    {
      "name": "pending_confirmation_does_not_unlock_dependent_phase",
      "intent": "Accepted mutations stay provisional until private or recovery state confirms them.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" },
      "initial": {
        "config": { "requiresProtectiveBeforeDca": true },
        "accountState": { "positions": [{ "symbol": "BTCUSDT", "managedSide": "LONG", "size": "0.01" }], "openOrders": [] },
        "orderContextStore": { "sl-001": { "role": "SL" }, "tp-001": { "role": "TP" } },
        "pendingConfirmations": [{ "customOrderId": "sl-001", "action": "place" }, { "customOrderId": "tp-001", "action": "place" }],
        "clockMs": 0
      },
      "steps": [
        { "type": "rest_accept", "subject": "place_protective", "payload": { "customOrderIds": ["sl-001", "tp-001"] } },
        { "type": "planner_tick", "atMs": 1 }
      ],
      "expect": {
        "phase": "blocked",
        "pendingConfirmations": [{ "customOrderId": "sl-001", "action": "place" }, { "customOrderId": "tp-001", "action": "place" }],
        "blocks": [{ "reason": "protective_confirmation_pending" }]
      },
      "forbid": ["dca_unlocked_by_rest_acceptance"]
    },
    {
      "name": "private_confirmation_before_rest_accept_is_consumed",
      "intent": "Private confirmation before REST acceptance clears without stale pending state.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG", "role": "SL" },
      "initial": {
        "config": {},
        "accountState": { "openOrders": [] },
        "orderContextStore": { "sl-early": { "role": "SL", "slotKey": "BTCUSDT:LONG:SL" } },
        "pendingConfirmations": [],
        "clockMs": 0
      },
      "steps": [
        { "type": "private_event", "subject": "order", "payload": { "customOrderId": "sl-early", "status": "New", "role": "SL" } },
        { "type": "rest_accept", "subject": "place", "payload": { "customOrderId": "sl-early" } }
      ],
      "expect": {
        "accountState": { "openOrders": [{ "customOrderId": "sl-early", "status": "New", "trusted": true }] },
        "pendingConfirmations": [],
        "logs": [{ "event": "early_private_confirmation_consumed" }]
      },
      "forbid": ["stale_pending_confirmation", "private_row_downgraded_by_rest_accept"]
    },
    {
      "name": "terminal_event_during_cancel_or_amend_is_benign_race",
      "intent": "Terminal private proof makes a later missing-target cancel/amend response benign.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG", "role": "TP" },
      "initial": {
        "config": {},
        "accountState": { "openOrders": [{ "customOrderId": "tp-old", "role": "TP", "status": "New" }] },
        "orderContextStore": { "tp-old": { "role": "TP", "slotKey": "BTCUSDT:LONG:TP" } },
        "pendingConfirmations": [{ "customOrderId": "tp-old", "action": "cancel" }],
        "clockMs": 0
      },
      "steps": [
        { "type": "private_event", "subject": "order", "payload": { "customOrderId": "tp-old", "status": "Cancelled" } },
        { "type": "rest_reject", "subject": "cancel", "payload": { "customOrderId": "tp-old", "class": "unknown_order" } },
        { "type": "planner_tick", "atMs": 1 }
      ],
      "expect": {
        "pendingConfirmations": [],
        "recoveryRequired": false,
        "phase": "protective",
        "logs": [{ "event": "terminal_race_acknowledged" }]
      },
      "forbid": ["broad_recovery_with_terminal_proof"]
    },
    {
      "name": "reconnect_queues_known_scopes_for_recovery_even_when_quiet",
      "intent": "Reconnect schedules scoped recovery for known scopes without waiting for later events.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" },
      "initial": {
        "config": {},
        "accountState": { "positions": [{ "symbol": "BTCUSDT", "managedSide": "LONG", "size": "0.01" }], "openOrders": [{ "customOrderId": "tp-001" }] },
        "orderContextStore": { "tp-001": { "role": "TP", "slotKey": "BTCUSDT:LONG:TP" } },
        "pendingConfirmations": [],
        "clockMs": 0
      },
      "steps": [
        { "type": "reconnect", "subject": "private_stream", "payload": { "gapPossible": true } },
        { "type": "planner_tick", "atMs": 1 },
        { "type": "rest_hydration", "subject": "scoped_recovery", "payload": { "positions": "refreshed", "openOrders": "refreshed", "fills": "refreshed", "readiness": "refreshed" } }
      ],
      "expect": {
        "recoveryRequired": false,
        "syncRequiredSubjects": [],
        "dirtyScopes": [{ "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" }],
        "logs": [{ "event": "reconnect_recovery_queued" }, { "event": "recovery_hydration_completed" }]
      },
      "forbid": ["live_mutation_before_recovery_hydration"]
    },
    {
      "name": "slot_filter_failures_are_blocked_not_silent_or_retried",
      "intent": "Filter and sizing failures produce explicit blocks, not silent no-ops or retry loops.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG", "role": "DCA", "step": 1 },
      "initial": {
        "config": { "filters": { "minQty": "0.001", "minNotional": "5" } },
        "accountState": { "positions": [{ "symbol": "BTCUSDT", "managedSide": "LONG", "size": "0.0001" }] },
        "orderContextStore": {},
        "pendingConfirmations": [],
        "clockMs": 0
      },
      "steps": [
        { "type": "planner_tick", "subject": "dca", "payload": { "desiredQty": "0", "desiredNotional": "0" } }
      ],
      "expect": {
        "phase": "blocked",
        "blocks": [{ "reason": "slot_filter_failure", "role": "DCA", "filter": "min_qty_or_notional" }],
        "logs": [{ "event": "slot_blocked", "qty": "0", "minQty": "0.001" }]
      },
      "forbid": ["silent_noop", "tight_retry_loop", "submit_locally_invalid_order"]
    },
    {
      "name": "flat_position_cleans_app_owned_orders",
      "intent": "Flat positions remove leftover app-owned orders without touching manual orders.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" },
      "initial": {
        "config": { "takeoverUnownedOrders": false },
        "accountState": { "positions": [], "openOrders": [{ "customOrderId": "tp-001", "owner": "app" }, { "customOrderId": "manual-1", "owner": "external" }] },
        "orderContextStore": { "tp-001": { "role": "TP", "slotKey": "BTCUSDT:LONG:TP" } },
        "pendingConfirmations": [],
        "clockMs": 0
      },
      "steps": [
        { "type": "planner_tick", "subject": "cleanup" },
        { "type": "private_event", "subject": "order", "payload": { "customOrderId": "tp-001", "status": "Cancelled" } }
      ],
      "expect": {
        "phase": "noop",
        "accountState": { "positions": [], "openOrders": [{ "customOrderId": "manual-1", "owner": "external" }] },
        "logs": [{ "event": "flat_cleanup_completed" }]
      },
      "forbid": ["manual_order_cancelled", "managed_order_left_active_after_flat"]
    },
    {
      "name": "rest_acceptance_does_not_downgrade_trusted_private_order",
      "intent": "REST acceptance cannot replace a more authoritative private active-order row.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG", "role": "TP" },
      "initial": {
        "config": {},
        "accountState": { "openOrders": [{ "customOrderId": "tp-002", "price": "66500", "qty": "0.01", "trusted": true }] },
        "orderContextStore": { "tp-002": { "role": "TP" } },
        "pendingConfirmations": [{ "customOrderId": "tp-002", "action": "place" }],
        "clockMs": 0
      },
      "steps": [
        { "type": "rest_accept", "subject": "place", "payload": { "customOrderId": "tp-002", "partialFields": true } }
      ],
      "expect": {
        "accountState": { "openOrders": [{ "customOrderId": "tp-002", "price": "66500", "qty": "0.01", "trusted": true }] },
        "pendingConfirmations": [],
        "logs": [{ "event": "rest_accept_matched_existing_private_order" }]
      },
      "forbid": ["trusted_open_order_downgraded"]
    },
    {
      "name": "terminal_order_status_is_not_open_order_confirmation",
      "intent": "Terminal or transition statuses clear/terminalize latches but do not confirm active protection.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG", "role": "SL" },
      "initial": {
        "config": {},
        "accountState": { "openOrders": [] },
        "orderContextStore": { "sl-terminal": { "role": "SL" } },
        "pendingConfirmations": [{ "customOrderId": "sl-terminal", "action": "place" }],
        "clockMs": 0
      },
      "steps": [
        { "type": "private_event", "subject": "order", "payload": { "customOrderId": "sl-terminal", "status": "Rejected" } }
      ],
      "expect": {
        "phase": "blocked",
        "pendingConfirmations": [],
        "blocks": [{ "reason": "protective_slot_terminal", "role": "SL" }],
        "logs": [{ "event": "terminal_order_status_routed" }]
      },
      "forbid": ["active_order_confirmed_from_terminal_status"]
    },
    {
      "name": "execution_fill_is_not_open_order_confirmation",
      "intent": "Execution rows update fills and dirty scopes but do not confirm active open orders.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG", "role": "TP" },
      "initial": {
        "config": {},
        "accountState": { "fills": [] },
        "orderContextStore": { "tp-fill": { "role": "TP", "slotKey": "BTCUSDT:LONG:TP" } },
        "pendingConfirmations": [{ "customOrderId": "tp-fill", "action": "place" }],
        "clockMs": 0
      },
      "steps": [
        { "type": "private_event", "subject": "execution", "payload": { "customOrderId": "tp-fill", "execId": "fill-1", "execQty": "0.005" } }
      ],
      "expect": {
        "dirtyScopes": [{ "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG", "role": "TP" }],
        "accountState": { "fills": [{ "customOrderId": "tp-fill", "execId": "fill-1" }] },
        "pendingConfirmations": [{ "customOrderId": "tp-fill", "action": "place" }]
      },
      "forbid": ["active_order_confirmation_cleared_from_execution_only"]
    },
    {
      "name": "default_dca_active_order_does_not_advance_next_step",
      "intent": "Default non-ladder DCA keeps one active next-step order and amends it if needed.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG", "role": "DCA", "step": 1 },
      "initial": {
        "config": { "dcaMode": "single_next_step" },
        "accountState": { "positions": [{ "symbol": "BTCUSDT", "managedSide": "LONG", "size": "0.01" }], "openOrders": [{ "customOrderId": "dca-1", "role": "DCA", "step": 1, "price": "62000" }] },
        "orderContextStore": { "dca-1": { "role": "DCA", "step": 1 } },
        "pendingConfirmations": [],
        "clockMs": 0
      },
      "steps": [
        { "type": "planner_tick", "subject": "dca", "payload": { "newDesiredPrice": "62100" } }
      ],
      "expect": {
        "phase": "dca",
        "intents": [{ "action": "amend", "role": "DCA", "step": 1 }],
        "logs": [{ "event": "single_dca_slot_amend_selected" }]
      },
      "forbid": ["place_dca_step_2_while_step_1_active"]
    },
    {
      "name": "startup_adopts_active_contexts_and_prunes_absent_contexts",
      "intent": "Startup uses trusted active orders to adopt current contexts and prune stale ones.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" },
      "initial": {
        "config": { "contextPersistence": true },
        "accountState": { "openOrders": [] },
        "orderContextStore": { "tp-live": { "role": "TP" }, "tp-stale": { "role": "TP" } },
        "pendingConfirmations": [],
        "clockMs": 0
      },
      "steps": [
        { "type": "rest_hydration", "subject": "startup_open_orders", "payload": { "openOrders": [{ "customOrderId": "tp-live", "role": "TP" }] } }
      ],
      "expect": {
        "dirtyScopes": [{ "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" }],
        "logs": [{ "event": "context_adopted", "customOrderId": "tp-live" }, { "event": "context_pruned", "customOrderId": "tp-stale" }]
      },
      "forbid": ["recover_slot_meaning_by_parsing_custom_id"]
    },
    {
      "name": "ambiguous_event_routes_bounded_fallback_then_exact_scope",
      "intent": "Ambiguous private rows use bounded fallback and later exact routing from normalized position state.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "UNKNOWN" },
      "initial": {
        "config": { "configuredSymbols": ["BTCUSDT"] },
        "accountState": { "positions": [] },
        "orderContextStore": {},
        "pendingConfirmations": [],
        "clockMs": 0
      },
      "steps": [
        { "type": "private_event", "subject": "order", "payload": { "symbol": "BTCUSDT", "customOrderId": "unknown-1" } },
        { "type": "private_event", "subject": "position", "payload": { "symbol": "BTCUSDT", "managedSide": "LONG", "size": "0.01" } }
      ],
      "expect": {
        "dirtyScopes": [{ "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" }],
        "logs": [{ "event": "bounded_fallback_scope_marked" }, { "event": "exact_scope_resolved_from_position" }]
      },
      "forbid": ["broad_product_wide_planning"]
    },
    {
      "name": "pending_confirmation_timeout_enters_scoped_recovery",
      "intent": "Confirmation timeout enters scoped recovery instead of duplicate placement.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG", "role": "SL" },
      "initial": {
        "config": { "confirmationTimeoutMs": 5000 },
        "accountState": { "positions": [{ "symbol": "BTCUSDT", "managedSide": "LONG", "size": "0.01" }] },
        "orderContextStore": { "sl-timeout": { "role": "SL" } },
        "pendingConfirmations": [{ "customOrderId": "sl-timeout", "action": "place", "createdAtMs": 0 }],
        "clockMs": 0
      },
      "steps": [
        { "type": "timer", "atMs": 5001, "payload": { "reason": "confirmation_timeout" } },
        { "type": "planner_tick", "atMs": 5001 }
      ],
      "expect": {
        "recoveryRequired": true,
        "phase": "recovery",
        "logs": [{ "event": "pending_confirmation_timeout", "action": "place" }]
      },
      "forbid": ["duplicate_place_before_recovery"]
    },
    {
      "name": "recovery_hydration_blocks_normal_phases_until_trust_restored",
      "intent": "Recovery-required state hydrates first and blocks normal phases until trust is restored.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" },
      "initial": {
        "config": {},
        "accountState": { "recoveryRequired": true, "syncRequiredSubjects": ["positions", "openOrders", "fills", "balances", "readiness"] },
        "orderContextStore": {},
        "pendingConfirmations": [],
        "clockMs": 0
      },
      "steps": [
        { "type": "planner_tick", "subject": "normal_phase_attempt" },
        { "type": "rest_hydration", "subject": "scoped_recovery", "payload": { "positions": [], "openOrders": [], "fills": [], "balances": [], "readiness": { "readyToTrade": true } } },
        { "type": "planner_tick", "subject": "after_recovery" }
      ],
      "expect": {
        "recoveryRequired": false,
        "syncRequiredSubjects": [],
        "dirtyScopes": [{ "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" }],
        "logs": [{ "event": "normal_phase_blocked_by_recovery" }, { "event": "recovery_hydration_completed" }]
      },
      "forbid": ["cleanup_or_protective_or_dca_during_untrusted_recovery", "increase_exposure_during_recovery"]
    },
    {
      "name": "configured_symbol_allowlist_blocks_out_of_scope_events",
      "intent": "Configured symbol allowlists prevent unrelated scopes from reaching planning.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "ETHUSDT", "managedSide": "LONG" },
      "initial": {
        "config": { "configuredSymbols": ["BTCUSDT"] },
        "accountState": { "positions": [] },
        "orderContextStore": {},
        "pendingConfirmations": [],
        "clockMs": 0
      },
      "steps": [
        { "type": "private_event", "subject": "position", "payload": { "symbol": "ETHUSDT", "managedSide": "LONG", "size": "1" } },
        { "type": "planner_tick", "atMs": 50 }
      ],
      "expect": {
        "phase": "noop",
        "dirtyScopes": [],
        "logs": [{ "event": "out_of_scope_event_ignored_for_planning", "symbol": "ETHUSDT" }]
      },
      "forbid": ["dirty_unconfigured_symbol", "submit_for_unconfigured_symbol"]
    },
    {
      "name": "dry_run_status_doctor_cannot_submit_live_orders",
      "intent": "Dry-run and inspection commands stay safe even when live credentials exist.",
      "requiredForLiveCapableDemo": true,
      "scope": { "exchange": "generic", "product": "linear", "symbol": "BTCUSDT", "managedSide": "LONG" },
      "initial": {
        "config": { "liveCredentialsPresent": true, "command": "doctor" },
        "accountState": {},
        "orderContextStore": {},
        "pendingConfirmations": [],
        "clockMs": 0
      },
      "steps": [
        { "type": "operator_command", "subject": "doctor", "payload": { "requestedMode": "inspect" } },
        { "type": "planner_tick", "subject": "safe_command" }
      ],
      "expect": {
        "phase": "noop",
        "logs": [{ "event": "safe_command_forced_read_only", "command": "doctor" }],
        "intents": []
      },
      "forbid": ["live_submit", "live_amend", "live_cancel", "dry_run_context_pollution"]
    }
  ]
}
