# Result Summary

TA9 Safe Runtime Slot Design Freeze Audit completed as static review only.

Conclusion:
- decision_class: TA9_SHADOW_DESIGN_READY
- P1_implementation_allowed: false
- shadow_logging_implementation_allowed: true
- Exact shadow slot: KOUCHA_GOLD_KIWAMI_SURVIVAL_TICK.mq5 / OnTick / after lines 2155-2163 and before lines 2164-2171.
- Real TA9 or TA5 Exit remains not allowed.

Key findings:
- `distance_to_hardstop_yen = GetHardStopThresholdYen() - GetFloatingLossYen(ps)`.
- `ps.floatingProfit` includes `POSITION_PROFIT + POSITION_SWAP`; commission is not explicitly added.
- The observed `distance=-8.16` with `HardStopNow=false` is explained by diagnostic stage traces passing `hardStop=false`, not by the HardStop formula itself.
- TA5 is not a fallback for "no priority change"; it also needs the same pre-HardStop structural slot for real Exit.

Next allowed action:
- Implement non-intervening TA9 Shadow Logging only, then validate same-tick parity and distance boundary behavior out-of-sample.


# Next Decision

Do not implement TA9 or TA5 Exit.

Next permitted task:
TA9 Shadow Logging implementation only.

Requirements for next task:
- no order sending
- no CloseAllEaPositions call
- one immutable same-tick snapshot per basket/eval cycle
- shared HardStopNow and distance calculation
- current HTE / Recovery / BasketClose eligibility logged and used only for suppression classification
- SNAPSHOT_PARITY_ERROR when distance<=0 but HardStopNow=false
- out-of-sample validation before any P1 Exit comparison


selected_design: TA9_SHADOW_SAME_TICK_SNAPSHOT_PRE_HARDSTOP_SLOT
decision_class: TA9_SHADOW_DESIGN_READY
P1_implementation_allowed: false
shadow_logging_implementation_allowed: true
exact_runtime_slot: OnTick() after TraceRuntimeEvaluationStage("BEFORE_HARDSTOP_EVALUATION") lines 2155-2163 and before CheckPreHardStopS2StrictGuardExit() lines 2164-2171
exact_source_file: KOUCHA_GOLD_KIWAMI_SURVIVAL_TICK.mq5
exact_function: OnTick
slot_addition_required: true
existing_exit_relative_order_change_required: false for shadow; false for future production only if current HTE/Recovery/BasketClose eligibility suppresses TA9
hardstop_relative_order_change_required: false for shadow; true for future production order sending before the HardStop side-effect gate
hardstop_preemption_required: false for shadow; true for future production close when HardStopNow=false, never when HardStopNow=true
existing_eligible_close_preemption_possible: yes if guards omitted; no under the proposed decision table for current eligibility
priority_order_change_required: false for shadow behavior; true for future TA9/TA5 real Exit because a new pre-HardStop structural slot would be used
distance_formula_verified: true
distance_sign_semantics_verified: true
negative_distance_with_HardStopNow_false_explained: true; old diagnostic stage traces passed hardStop=false while distance was computed from real floating loss
same_tick_snapshot_contract_ready: true
numeric_boundary_policy: no epsilon; distance<=0 suppresses; exact zero is boundary/HardStop or parity error if HardStopNow=false
epsilon_used: none
TA5_without_priority_change_valid: false
additional_logging_required: true
unresolved_risks: production order-sending arbiter, commission treatment, future HTE opportunity, out-of-sample validation
next_action: implement non-intervening TA9 Shadow Logging only; do not implement TA9/TA5 close orders


# distance_to_hardstop_yen Semantics Audit

Verified source formula:
- `GetPositionState` lines 9554-9607 builds `ps.floatingProfit` as the sum of `POSITION_PROFIT + POSITION_SWAP` for EA positions.
- It does not explicitly add commission.
- `GetFloatingLossYen` lines 2045-2050 returns `-ps.floatingProfit` when floating P/L is negative, otherwise 0.
- `GetHardStopThresholdYen` lines 2060-2073 selects fixed, initial-balance-R, or balance-percent threshold.
- For the current baseline, `HardStopBasis = HARDSTOP_BALANCE_PERCENT` and `HardStopFloatingLossPercent = 12.0`.
- `GetBalancePercentHardStopThresholdYen` lines 2052-2057 uses `AccountInfoDouble(ACCOUNT_BALANCE) * HardStopFloatingLossPercent / 100.0`.
- `IsHardStopTriggeredByBasis` lines 2076-2081 returns true when floating loss is >= threshold, or margin level is <= `HardStopMarginLevel`.
- `PostHedgeDiagDistanceToHardStop` lines 7294-7299 returns `threshold - GetFloatingLossYen(ps)`.

Meaning:
- Unit: account currency yen.
- Positive distance: floating-loss hardstop threshold has not been reached.
- Zero distance: floating-loss threshold is exactly reached.
- Negative distance: floating-loss threshold is exceeded.
- Floating P/L includes swap via source; commission is not explicitly included.
- Spread is included indirectly through MT5 current `POSITION_PROFIT`, which depends on current bid/ask valuation.
- Slippage is not included.
- Balance-percent threshold includes realized balance changes through `ACCOUNT_BALANCE`.
- 50k / 100k runs differ because the balance-percent threshold depends on the account balance.

Why distance=-8.16 with HardStopNow=false appeared:
- In source, `TraceRuntimeEvaluationStage` trusts the boolean `hardStop` argument passed to it.
- OnTick lines 2137-2163 call `TraceRuntimeEvaluationStage` for TICK_START / AFTER_FLOATING_PNL_UPDATE / BEFORE_HARDSTOP_EVALUATION with `hardStop=false`.
- OnTick line 2209 also passes `hardStop=false` for AFTER_MARKET_STATE_UPDATE.
- Therefore HardStopNow=false in those EVAL_STAGE_TRACE rows is not the same as recomputing `IsHardStopTriggeredByBasis` from the same `PositionState`.
- The distance field is computed from the real floating loss formula, while the diagnostic stage's HardStopNow field can be forced false by the stage call.
- Most likely root cause: diagnostic stage labeling created an artificial pre-HardStop state, not a formula mismatch in `GetFloatingLossYen`.

Design consequence:
- TA9 safe logic must not use stage-passed HardStopNow from old EVAL_STAGE_TRACE rows as source of truth.
- A shadow evaluator must create a single immutable snapshot and compute both `distance_to_hardstop_yen` and `HardStopNow` through the same shared function on the same `PositionState`.
- Any snapshot where `distance_to_hardstop_yen <= 0` and `HardStopNow=false` must be logged as `SNAPSHOT_PARITY_ERROR` before any future order-sending design is considered.


# TA5 Priority-Change Resolution

Problem:
Prior output said both:
- If priority change is not accepted, fall back to TA5.
- TA5 also needs POST_HEDGE_SLOT and `priority_order_change_required=true`.

Resolution:
- The phrase `priority_order_change_required` was too coarse.
- It must be split into:
  - slot_addition_required
  - existing_exit_relative_order_change_required
  - hardstop_relative_order_change_required
  - hardstop_preemption_required
  - existing_eligible_close_preemption_possible
  - structural_insertion_only
  - current_behavior_semantically_changed

TA9:
- slot_addition_required: true
- existing_exit_relative_order_change_required: false if current HTE/Recovery/BasketClose eligibility suppresses TA9
- hardstop_relative_order_change_required: true for real order sending before the HardStop gate
- hardstop_preemption_required: true for future production close, but must be false when HardStopNow=true
- existing_eligible_close_preemption_possible: yes if guards are omitted; no under the proposed decision table
- structural_insertion_only: true for shadow logging only
- current_behavior_semantically_changed: false for shadow; true for real close

TA5:
- slot_addition_required: true
- existing_exit_relative_order_change_required: false if current HTE/Recovery/BasketClose eligibility suppresses TA5
- hardstop_relative_order_change_required: true for real order sending before the HardStop gate
- hardstop_preemption_required: true for future production close, but must be false when HardStopNow=true
- existing_eligible_close_preemption_possible: yes if guards are omitted; no under the same decision table
- structural_insertion_only: true for shadow logging only
- current_behavior_semantically_changed: false for shadow; true for real close

Conclusion:
- TA5 is not a valid alternative if the business rule is "no pre-HardStop slot / no HardStop-relative behavior change".
- TA5 is only a lower-effect, simpler-condition alternative after the same pre-HardStop slot concept is accepted.
- Therefore `TA5_without_priority_change_valid=false`.


# TA9 Shadow Logging Specification

Non-intervention rule:
- Shadow Logging must never call `trade.PositionClose`.
- Shadow Logging must never call `CloseAllEaPositions`.
- Shadow Logging must not set `tradeActionDone`.
- Shadow Logging must not set close intent state.
- Shadow Logging must not mutate basket lifecycle except local shadow counters.

Events:
- CLOSE_PRIORITY_SNAPSHOT
- TA9_RAW_CANDIDATE
- TA9_SAFE_CANDIDATE
- TA9_SUPPRESSED
- EXISTING_CLOSE_SELECTED
- HARDSTOP_SELECTED
- CLOSE_ORDER_ATTEMPT
- CLOSE_ORDER_RESULT
- CLOSE_RETRY
- SNAPSHOT_PARITY_ERROR

Logging volume control:
- Emit CLOSE_PRIORITY_SNAPSHOT at first TA9 raw/safe state change per basket/eval stage.
- Emit TA9_RAW_CANDIDATE only on first raw match or when raw state changes false->true.
- Emit TA9_SAFE_CANDIDATE only on first safe match or when safe state changes false->true.
- Emit TA9_SUPPRESSED on suppression reason changes.
- Always emit SNAPSHOT_PARITY_ERROR.
- Always emit a basket-end aggregate row.
- Do not emit every tick unless parity error or close event occurs.

Required implementation location for shadow only:
- Source: KOUCHA_GOLD_KIWAMI_SURVIVAL_TICK.mq5
- Function: OnTick()
- Slot: after the BEFORE_HARDSTOP_EVALUATION stage trace at lines 2155-2163 and before the existing disabled S2 call at lines 2164-2171.
- The shadow evaluator must use the same `PositionState ps` and must compute a shared snapshot, including actual `hardStop = IsHardStopTriggeredByBasis(ps, marginLevel)`.

Safety:
- If HardStopNow=true, log TA9_SUPPRESSED / HARDSTOP_NOW_TRUE.
- If HTE/Recovery/BasketClose currently eligible, log TA9_SUPPRESSED with the current eligible reason.
- If distance<=0, log TA9_SUPPRESSED / DISTANCE_NOT_POSITIVE.
- If distance<=0 but HardStopNow=false, log SNAPSHOT_PARITY_ERROR.
