
    YZj(                       d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	 ddl
m
Z
mZ ddlmZ  ed          Zedz  Zed	z  Zed
z  Zedz  Zedz  Zedz  dz  Zej                            dd           dZ eej                            dd          pd          Zd                                Zd+dZd,dZd-dZ d.d Z!d/d!Z"d0d"Z#d1d&Z$d0d'Z%d2d)Z&e'd*k    r e( e&                      dS )3am  Resumable manual replay control worker launcher.

This worker does not confirm trades by itself. It launches small Hermes one-shot
runs that must inspect TradingView replay for a small batch and write non-final
manual review decisions. The wrapper maintains a cursor and lock so work can
continue until the queue is complete without overwriting existing artifacts.
    )annotationsN)Counter)datetimetimezone)Pathz9/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026hybridreviewsz%manual_replay_readiness_manifest.jsonzmanual_replay_progress.jsonzmanual_replay_worker.lockreportszmanual_replay_worker.logT)parentsexist_ok   MANUAL_REPLAY_MAX_CHILD_RUNS0aw  
You are Aryy Finance manual replay reviewer. Process the next chronological mini-batch of XAUUSD hybrid manual replay items.

Project root: /home/aryy/.hermes/profiles/finance/backtests/xauusd_2026
Read these files:
- hybrid/reviews/manual_replay_readiness_manifest.json
- hybrid/reviews/manual_replay_progress.json
- hybrid/data/ohlcv_W.json, D, H4, H1, M30, M15, M5 as needed

Process exactly {batch_size} items starting at manifest.manual_replay_items[{start_index}] unless the queue ends.

Rules:
- Analysis-only. Do not trade, do not execute broker/order actions.
- Do not overwrite existing journal/backtest data blindly.
- Do not send raw OHLCV in final output; use Python/local scripts to compute summaries/evidence.
- Use TradingView MCP Bar Replay when possible for chronological review. If TradingView cannot jump/render a historical date, use local OHLCV chronological evidence and mark decision as 'needs_more_context' rather than accepting.
- Do NOT promote to journal unless a candidate is clearly valid with HTF narrative, M30 structure, M15/M5 execution, Fib 50/62 + PD array, and chronological entry/SL/TP evidence. When in doubt, keep as rejected/shadow/needs_more_context.
- For this autonomous batch, prefer conservative labels; confirmed journal promotion should be rare and must include exact Entry/SL/TP evidence. If not fully certain, do not promote.
- Append/write a mini-batch result file: hybrid/reviews/manual_replay_results/replay_result_{start_index:05d}_{end_index:05d}.json
- Update hybrid/reviews/manual_replay_progress.json cursor atomically after writing results.
- Validate all changed JSON with python3 -m json.tool.

Result schema per item:
{{
  "candidate_id": "...",
  "timestamp": "...",
  "decision": "accepted|rejected|shadow|needs_more_context",
  "reason": "concise chronological reason",
  "htf_context": "summary",
  "m30_structure": "summary",
  "m15_m5_execution": "summary",
  "entry": null or number,
  "stop": null or number,
  "target": null or number,
  "r": null or number,
  "promoted_to_journal": false
}}

Final response: one compact line with processed count, decisions breakdown, result file, next_index.
returnstrc                     t          j        t          j                                      d                                                              dd          S )Nr   )microsecondz+00:00Z)r   nowr   utcreplace	isoformat     &hybrid/scripts/manual_replay_worker.pynow_isor   J   sC    <%%--!-<<FFHHPPQY[^___r   msgNonec                    t                       d|  }t          |d           t                              dd          5 }|                    |dz              d d d            d S # 1 swxY w Y   d S )N T)flushautf-8encoding
)r   printLOGopenwrite)r   linehandles      r   logr-   N   s    ii#D	$d	#	(	( "FTD[!!!" " " " " " " " " " " " " " " " " "s   A''A+.A+pathr   dictc                R    t          j        |                     d                    S )Nr#   r$   )jsonloads	read_text)r.   s    r   loadr4   U   s     :dnngn66777r   objc                d    |                      t          j        |dd          dz   d           d S )N   F)indentensure_asciir&   r#   r$   )
write_textr1   dumps)r.   r5   s     r   saver<   Y   s5    OODJs15AAADHSZO[[[[[r   c                   d| v rgt          |                     d          t                    r?|                     dg            |                     ddt	                      ddd           | S t          |                     d|                     d	d
                    pd
          }t          |                     d|                     d|                    p|          }dt	                      dddd||t          |                     dd
          pd
          t          |                     dd
          pd
          t          |                     dd
          pd
          t          |                     dd
          pd
          |                     d          |                     d          d|                     dg           dS )zGAccept legacy/simple child-written progress schemas and normalize them.cursorrunsmetadatamanual_replay_progress_state4hybrid/reviews/manual_replay_readiness_manifest.jsonFpurpose
created_atsourceisFinalBacktestcurrent_index
next_indexr   total_processedprocessed_countz%Normalized from legacy/simple schema.rD   rE   rF   rG   notesaccepted_countrejected_countshadow_countneeds_more_context_countlast_candidate_idlast_updatedrI   rK   rN   rO   rP   rQ   rR   rS   r@   r>   r?   )
isinstancegetr/   
setdefaultr   int)r5   rH   rJ   s      r   normalize_staterZ   ]   s   3:cggh&7&7>>vr"""z/M]d]f]f  si  ~C  $D  $D  	E  	E  	E
q1I1IJJOaPPM#''"3SWW=NP]5^5^__pcpqqO >gii  dZ  ot  f  g  g!.?fijmjqjq  sC  EF  kG  kG  kL  KL  gM  gM  ad  eh  el  el  m}  @  eA  eA  eF  EF  aG  aG  Y\  ]`  ]d  ]d  es  uv  ]w  ]w  ]|  {|  Y}  Y}  [^  _b  _f  _f  gA  CD  _E  _E  _J  IJ  [K  [K  be  bi  bi  j}  b~  b~  PS  PW  PW  Xf  Pg  Pg  h  h##  r   c            
        t                                           s7dt                      dddddddddd d dg d} t          t           |            | S t	          t                     }t          |          } d|vrt          t           |            | S )	NrA   rB   FrC   r   rT   rU   r>   )STATEexistsr   r<   r4   rZ   )r5   raws     r   ensure_stater_   l   s    <<>> $BRYR[R[  h^  sx  y  y%&1PQefxy  XY  pt  FJ  K  K
 

 	UC

u++C
#

CsUCJr   payloadobject
list[dict]c                    t          | t                    rd | D             S t          | t                    r=dD ]:}|                     |          }t          |t                    rd |D             c S ;g S )Nc                <    g | ]}t          |t                    |S r   rV   r/   .0xs     r   
<listcomp>z result_items.<locals>.<listcomp>~   s'    :::ajD&9&9::::r   )itemsresults	decisionsc                <    g | ]}t          |t                    |S r   re   rf   s     r   ri   z result_items.<locals>.<listcomp>   s'    >>>a*Q*=*=>>>>r   )rV   listr/   rW   )r`   keyvals      r   result_itemsrq   |   s    '4   ;::7::::'4   ?4 	? 	?C++c""C#t$$ ?>>3>>>>>>?Ir   c            
        d} t                      }d }g }t          t          dz                      d                    D ]}t	          j        d|j                  }|st          t          |	                                          \  }}|| k    r nt          t          |                    }|} |                    t          |                    t                                         |D ]=}	||	                    dd          xx         dz  cc<   |	                    d          p|}>d	t#                      d
ddd| | |                    dd          |                    dd          |                    dd          |                    dd          |t#                      dt#                      |t%          |          dgdS )Nr   manual_replay_resultszreplay_result_*.jsonz replay_result_(\d+)_(\d+)\.json$decisionunknown   candidate_idrA   rB   Fz,Derived from contiguous replay_result files.rL   acceptedrejectedshadowneeds_more_contextrT   )
derived_atresult_filesdecision_countsrU   )r   sortedREVIEWSglobrematchnamemaprY   groupsrq   r4   appendr   relative_toROOTrW   r   r/   )
r>   countslastfilesr.   r   startendrj   items
             r   derive_state_from_resultsr      sR   F"99FDE"99??@VWWXX 4 4<diHH 	ellnn--
sF??ET$ZZ((S))$//00111 	4 	4D488J	22333q833388N++3tDD	4 !?gii  dZ  ot  m  n  n!'FV\V`V`akmnVoVo  DJ  DN  DN  OY  [\  D]  D]  ou  oy  oy  zB  DE  oF  oF  dj  dn  dn  oC  EF  dG  dG  ^b  t{  t}  t}  ~  ~ '		5UYZ`UaUabbc  r   rY   c                 
   t          j        t                     t                                          rt          dt           d           dS t                              t          j        t                      t          j
                    dd          dz              	 t          t                    } t          |                     dg                     }d}	 t                      }t!          |                    d
i                               dd                    }||k    r^t          d| d|            	 	 t                              d	           dS # t$          $ r}t          d|            Y d }~dS d }~ww xY wt'          |t(          z   |          }t*          rn|t*          k    rct          dt*           d|            	 	 t                              d	           dS # t$          $ r}t          d|            Y d }~dS d }~ww xY w|dz  }t,                              t(          ||          }t           j                                        }d|d<   dddddd|g}	t          d| d| d|            t5          j        |	t9          t                    |d	d	d           }
t          d!|
j                    |
j                                        rEt          d"|
j                                                             dd#          d$d          z              |
j!                                        rEt          d%|
j!                                                             dd#          d$d          z              |
j        dk    r[t          d&           |
j        	 t                              d	           S # t$          $ r}t          d|            Y d }~S d }~ww xY wt                      }t!          |                    d
i                               dd                    }||k    rtE                      }t!          |                    d
i                               dd                    }||k    r.tG          tH          |           t          d'| d(| d)|            nat          d*| d+| d)|            	 	 t                              d	           d,S # t$          $ r}t          d|            Y d }~d,S d }~ww xY wtK          j&        d           A# 	 t                              d	           w # t$          $ r}t          d|            Y d }~w d }~ww xY wxY w)-NzLOCK_EXISTS z	; exitingr7   )
started_atpid)r8   r&   manual_replay_itemsr   Tr>   rI   zCOMPLETE next_index=z total=)
missing_okzLOCK_REMOVE_ERROR zSTOP max_child_runs=z next_index=rv   )
batch_sizestart_index	end_indexfinanceHERMES_PROFILEhermesz
--providerzcustom:9routerz--modelzag/gemini-pro-agentz-zzRUN_CHILD run=z start=z end=i  )cwdenvtextcapture_outputtimeoutzHERMES_EXIT code=zSTDOUT z | i`zSTDERR zSTOP nonzero child exitzCURSOR_DERIVED start=z old_new_index=z derived_index=zSTOP cursor_not_advanced start=z new_index=r   )'oschdirr   LOCKr]   r-   r:   r1   r;   r   getpidr4   MANIFESTlenrW   r_   rY   unlink	Exceptionmin
BATCH_SIZEMAX_CHILD_RUNSPROMPT_TEMPLATEformatenvironcopy
subprocessrunr   
returncodestdoutstripr   stderrr   r<   r\   timesleep)manifesttotalr?   stater   excr   promptr   cmdproc	new_state	new_indexderivedderived_indexs                  r   mainr      s   HTNNN{{}} *4***+++qOODJgii	LLUVWWWZ^^___0,>>HLL!6;;<<'	 NNE		(B//33L!DDEEE~~@5@@@@AAAH	,KK4K((((( 	, 	, 	,*S**+++++++++	,K ej(%00C $."8"8N>NNuNNOOO@	,KK4K((((( 	, 	, 	,*S**+++++++++	,C AID$++zu`c+ddF*//##C$-C !\+;YH]_ceklC???e??#??@@@>#3t99#DY]gklllD5DO55666{  "" RI 1 1 3 3 ; ;D% H H PPQQQ{  "" RI 1 1 3 3 ; ;D% H H PPQQQ!##-..."	,KK4K(((( 	, 	, 	,*S**++++++++	, %IIMM(B77;;L!LLMMIE!!355 #GKK"$=$=$A$A,PQ$R$R S S 5(((((oooioo`mooppppu%uuIuufsuuvvv	,KK4K((((( 	, 	, 	,*S**+++++++++	,	 JqMMMO'	R	,KK4K(((( 	, 	, 	,*S**++++++++	,s   BT ;E 
E?"E::E?AT G% %
H/HHF
T N66
O OOCT =S 
T$S<<TT U!T87U!8
UUU!UU!__main__)r   r   )r   r   r   r   )r.   r   r   r/   )r.   r   r5   r/   r   r   )r5   r/   r   r/   )r   r/   )r`   ra   r   rb   )r   rY   ))__doc__
__future__r   r1   r   r   r   r   collectionsr   r   r   pathlibr   r   HYBRIDr   r   r\   r   r(   parentmkdirr   rY   r   rW   r   r   r   r   r-   r4   r<   rZ   r_   rq   r   r   __name__
SystemExitr   r   r   <module>r      s(    # " " " " "  				 				            ' ' ' ' ' ' ' '      tGHH	
9
<<//,,y55 
    - - -
RZ^^$BCHHMANN'N 
EGGO T` ` ` `" " " "8 8 8 8\ \ \ \             26, 6, 6, 6,r z
*TTVV

 r   