Ë
    ß½Ùjl  ã                   óÆ  — d Z ddlZddlZddlmZ ej
                  j                  d e ee«      j                  «      «       ddl
Z
ddlmZmZ ddlmZ ddlmZmZ ddlZddlmZ dZd	„ Zdd
„Zedk(  rmdZdej8                  v r+ej8                  ej8                  j;                  d«      dz      Z eej8                  d   e¬«      Z e ej@                  ed¬«      «       yy)u|  
AI Room Designer â€” Stage 3 generation orchestrator: backend + QC v2 gate +
AUTOMATED RETRY (estimate phase 13, completed here).

Flow for one job (job dir prepared by pipeline.prepare / the app server):
  attempt 0   -> backend.edit(before, locked structure prompt)
  score it with the QC v2 architectural-mask gate
  FAIL/PARTIAL -> regenerate with the FRAMING LOCK appended
                  (the fix that rescued Budget Refresh in Stage 2),
                  up to config.max_retries() retries (default 2)
  end         -> the BEST attempt (gate rank, then arch, then layout) is
                 finished through pipeline.finish (composites + qc + plan);
                 if no attempt passed, the best is still surfaced and the
                 job is FLAGGED in generation.json + qc.json (gate_final).

Usage:
  python3 pipeline/generate.py <job-dir> [--provider gemini|mock|standin]
é    N)ÚPath)Úget_backendÚBackendError)Úbuild_prompt)ÚscoreÚverdict)ÚImagez¯ CRITICAL FRAMING LOCK: keep the EXACT same camera position, framing, crop and field of view as the input photo. Do not zoom, widen, shift, pan or reframe the shot in any way.c                 óR   — | j                  d«      rdS | j                  d«      rdS dS )NÚPASSé   ÚPARTIALé   r   )Ú
startswith)Úvs    ú+/var/www/room-designer/pipeline/generate.pyÚ_rankr   &   s(   € Ø—‘˜VÔ$ˆ1ÐM¨q¯|©|¸IÔ/F¨!ÐMÈAÐMó    c                 óš  — t        | «      }t        j                  |dz  j                  «       «      }|d   }|dz  }|xs t	        |«      }|dz  }|j                  d¬«       t        |«      }g d }
}	t        t        j                  «       dz   «      D �]9  }|dk(  r|n|t        z   }|d	|› d
�z  }	 |j                  t        |«      |t        |«      «       |d	|› d�z  }t        j                   |«      j#                  d«      j%                  t        j                   |«      j&                  «      j)                  |«       t+        t        |«      t        |«      «      }t-        |«      }|t        |«      |||dkD  t/        |dd «      dœ}|	j                  |«       t1        |«      |d   |d   f}|
�||
d   kD  r||f}
|j3                  d«      s�Œ: n t/        |d|xs t        j4                  «       «      t/        |dd «      |	t7        dt9        |	D �cg c]	  }d|v sŒ|‘Œ c}«      dz
  «      dœ}|
€W|j;                  dd|	r|	d   j=                  dd«      nddœ«       |dz  j?                  t        j@                  |d¬«      «       |S |
d   }|d   j3                  d«      }|j;                  |rd nd!| |rd nd"|d	   |d   d#œ«       tC        jD                  t        |«      |d$   «       t        j                  |d%z  j                  «       «      }d&D �ci c]  }|||   “Œ
 c}|d'<   |rdnd(d)|d	   › d*|d+   › d,�z   |d-<   |d%z  j?                  t        j@                  |d¬«      «       |dz  j?                  t        j@                  |d¬«      «       |S # t        $ r)}|	j                  |t        |«      dœ«       Y d }~ �Œíd }~ww xY wc c}w c c}w ).Nzjob.jsonÚmodez
before.jpgÚattemptsT)Úexist_okr   r   Úattemptz.png)r   Úerrorz_matched.pngÚRGBÚ
last_usage)r   ÚimageÚscoresr   Úframing_lockÚusageÚarch_edge_corrÚlayout_corrr   ÚnameÚmodelr   )Úproviderr#   r   Úretries_usedr   éÿÿÿÿzno attempt producedzno attempts)ÚstatusÚflaggedÚflagzgeneration.jsonr   ©Úindentr   Údonezdone-flaggedz?no attempt passed the QC gate; best attempt surfaced for review)r'   r(   r)   Úbest_attemptÚfinal_verdictr   zqc.json)r$   r#   r%   r(   Ú
generationu!   FLAGGED â€” surfaced best attemptz
 (attempt z, r%   z	 retries)Ú
gate_final)#r   ÚjsonÚloadsÚ	read_textr   Úmkdirr   ÚrangeÚconfigÚmax_retriesÚFRAMING_LOCKÚeditÚstrr   Úappendr	   ÚopenÚconvertÚresizeÚsizeÚsaver   r   Úgetattrr   r   r$   ÚmaxÚlenÚupdateÚgetÚ
write_textÚdumpsÚplÚfinish)Újob_dirr$   ÚbackendÚjobÚmetar   ÚbeforeÚattempts_dirÚbase_promptr   ÚbestÚiÚpromptÚrawÚeÚnormÚsr   ÚrecÚkeyÚaÚgenÚbest_recÚpassedÚqcÚks                             r   Úgenerater`   *   s¿  € Ü
ˆw‹-€CÜ�:‰:�s˜ZÑ'×2Ñ2Ó4Ó5€DØ�‰<€DØ�<Ñ€FØÒ.œ XÓ.€GØ˜Ñ#€LØ×Ñ ÐÔ%ä˜tÓ$€KØ˜ˆd€HÜ”6×%Ñ%Ó'¨!Ñ+Ó,ó ˆØ ! Q¢‘¨K¼,Ñ,FˆØ˜w q c¨Ð.Ñ.ˆð	Ø�L‰Lœ˜V› f¬c°#«hÔ7ð
  ¨ s¨,Ð7Ñ7ˆÜ�
‰
�3‹×Ñ Ó&×-Ñ-¬e¯j©j¸Ó.@×.EÑ.EÓF×KÑKÈDÔQÜ”#�f“+œs 4›yÓ)ˆÜ�A‹JˆØ¤c¨$£i¸1ÈØ  1™uÜ ¨°tÓ<ñ>ˆð 	�‰˜ÔÜ�Q‹x˜Ð+Ñ,¨a°Ñ.>Ð?ˆØˆ<˜3  a¡š=Ø˜�:ˆDØ�<‰<˜ÖÙð+ô. ˜w¨°Ò0M¼F¿O¹OÓ<MÓNÜ˜G W¨dÓ3ØÜ˜q¤#°(Ö&L¨Q¸hÈ!ºm¢qÒ&LÓ"MÐPQÑ"QÓRñT€Cð €|Ø�
‰
˜g°$ÙPX˜H R™L×,Ñ,¨WÐ6KÔLÐ^kñmô 	nà	Ð Ñ	 ×,Ñ,¬T¯Z©Z¸ÀAÔ-FÔGØˆ
à�A‰w€HØ�iÑ ×+Ñ+¨FÓ3€FØ‡J�J¡F™&°Ø%˜:Ù &™ØYØ (¨Ñ 3Ø!)¨)Ñ!4ñ6ô 7ô ‡I�IŒc�#‹h˜ Ñ)Ô*Ü	�‰�S˜9‘_×/Ñ/Ó1Ó	2€BØ+[Ö\ a˜˜3˜q™6™	Ò\€B€|ÑÙ"(™Ð.QØ# H¨YÑ$7Ð#8¸¸3¸~Ñ;NÐ:OÈyÐYñZ€B€|Ñàˆ9�_× Ñ ¤§¡¨B°qÔ!9Ô:ØÐÑ×(Ñ(¬¯©°CÀÔ)BÔCØ€Jøô[ ò 	Ø�O‰O¨´C¸³FÑ;Ô<Þûð	üò* 'Mùò& ]s*   Â/%NÈ	OÈOÌOÎ	O ÎN;Î;O Ú__main__z
--providerr   )r$   r   r*   )NN)!Ú__doc__r1   ÚsysÚpathlibr   ÚpathÚinsertr:   Ú__file__Úparentr6   Úbackendsr   r   Úmodesr   Úqc_scorer   r   ÚpipelinerH   ÚPILr	   r8   r   r`   Ú__name__ÚprovÚargvÚindexÚoutÚprintrG   © r   r   ú<module>ru      sÇ   ðñó$ Û 
Ý à ‡�‡��‘3‘t˜H“~×,Ñ,Ó-Ô .Û ß .Ý ß #Û Ý ð*ð òNó=ð@ ˆzÒØ€DØ�s—x‘xÑØ�x‰x˜Ÿ™Ÿ™ |Ó4°qÑ8Ñ9ˆÙ
�3—8‘8˜A‘;¨Ô
.€CÙ	ˆ*ˆ$�*‰*�S Ô
#Õ$ð r   