Ë
    ÚNñj¾  ã                  ó¨   — d Z ddlmZ ddlZ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mZmZ dZ ej                   «       Zd	Zd
Zdd„Zdd„Zddd„Zy)uE  Backlot event stream â€” append-only tool-event log per project.

Written by the BaseTool instrumentation layer (tools/base_tool.py) whenever a
tool executes against a project directory; consumed by the Backlot board's
watcher to power live activity and per-scene generating states.

Design rules:
- Observability must never break production: every public function swallows
  its own errors. A failed event write is silently dropped.
- Zero agent burden: project attribution is inferred from the tool's inputs
  (explicit ``project_dir`` or any path argument under ``projects/``).
é    )ÚannotationsN)ÚdatetimeÚtimezone)ÚPath)ÚAnyÚOptional)ÚPROJECTS_DIRÚ	REPO_ROOTzevents.jsonl)Úproject_dirÚproject_path)Úoutput_pathÚ
output_dirÚoutput_fileÚ
input_pathÚ
video_pathÚ
audio_pathÚ
image_pathÚ	file_pathc                óÂ  — t        | t        «      sy	 t        j                  «       }t        t
        z   D ]„  }| j                  |«      }t        |t        t        f«      rt        |«      sŒ6	 t        |«      j                  «       }|j                  |«      }|j                  sŒnt        |j                  d   z  c S  	 y# t        t        f$ r Y Œšw xY w# t        $ r Y yw xY w)uÖ   Best-effort: which project directory does this tool call belong to?

    Returns None when the call can't be attributed â€” the event is then
    simply not emitted (principle: never guess loudly, never fail).
    Nr   )Ú
isinstanceÚdictr	   ÚresolveÚ_EXPLICIT_PROJECT_KEYSÚ_PATH_HINT_KEYSÚgetÚstrr   Úrelative_toÚ
ValueErrorÚOSErrorÚpartsÚ	Exception)ÚinputsÚprojects_rootÚkeyÚvalueÚresolvedÚrels         ú8C:\Ai_Coding\apps\studio\tools\OpenMontage\lib\events.pyÚinfer_project_dirr)   .   sÍ   € ô �fœdÔ#Øðô %×,Ñ,Ó.ˆÜ)¬OÔ;ˆCØ—J‘J˜s“OˆEÜ˜e¤c¬4 [Ô1¼¸U¼ØðÜ ›;×.Ñ.Ó0�Ø×*Ñ*¨=Ó9�ð �y‹yÜ# c§i¡i°¡lÑ2Ò2ñ <ð øô ¤Ð(ò Ùðûô ò ÙðúsB   “AC Á+*B=ÂC Â"C Â:C Â=CÃC ÃCÃC Ã	CÃCc                ó&  — 	 t        | «      } | j                  «       sydt        j                  t        j
                  «      j                  «       i}|j                  |j                  «       D ��ci c]  \  }}|€Œ	||“Œ c}}«       | t        z  }t        j                  |t        ¬«      }t        5  t        |dd¬«      5 }|j                  |dz   «       ddd«       ddd«       yc c}}w # 1 sw Y   ŒxY w# 1 sw Y   yxY w# t         $ r Y yw xY w)u¹   Append one event to the project's events.jsonl. Never raises.

    Writes only into an EXISTING project directory â€” a typo'd path must not
    spawn a ghost project on the board.
    NÚts)ÚdefaultÚaúutf-8)ÚencodingÚ
)r   Úis_dirr   Únowr   ÚutcÚ	isoformatÚupdateÚitemsÚEVENTS_FILENAMEÚjsonÚdumpsr   Ú_write_lockÚopenÚwriter!   )r   ÚpayloadÚentryÚkÚvÚpathÚlineÚfs           r(   Ú
emit_eventrD   M   sÞ   € ðÜ˜;Ó'ˆØ×!Ñ!Ô#ØØ”x—|‘|¤H§L¡LÓ1×;Ñ;Ó=Ð>ˆØ�‰ w§}¡}¤ÔH¡™t˜q !¸!¹-�a˜‘d ÒHÔIØœ_Ñ,ˆÜ�z‰z˜%¬Ô-ˆÞÜ�d˜C¨'Õ2°aØ—‘˜˜t™Ô$÷ 3÷ ˆ[ùó I÷ 3Ð2ú÷ ˆ[ûô ò Ùðúse   ‚D žAD Á/
C&
Á:C&
Á?2D Â1C8Ã C,ÃC8ÃD Ã&D Ã,C5	Ã1C8Ã8DÃ=D ÄD Ä	DÄDc                ó~  — t        | «      t        z  }|j                  «       sg S g }	 t        |dd¬«      5 }|D ]:  }|j	                  «       }|sŒ	 |j                  t        j                  |«      «       Œ< 	 ddd«       |�|| d S |S # t        j                  $ r Y Œew xY w# 1 sw Y   Œ,xY w# t        $ r g cY S w xY w)zDRead events for a project (oldest first). Tolerates malformed lines.r.   Úreplace)r/   ÚerrorsN)
r   r7   Úexistsr;   ÚstripÚappendr8   ÚloadsÚJSONDecodeErrorr   )r   ÚlimitrA   ÚeventsrC   rB   s         r(   Úread_eventsrO   b   sÄ   € ä�ÓœÑ.€DØ�;‰;Œ=Øˆ	Ø#%€FðÜ�$ °Õ;¸qÛ�Ø—z‘z“|�ÙØðØ—M‘M¤$§*¡*¨TÓ"2Õ3ñ ÷ <ð ÐØ�u�f�gˆÐØ€Møô ×+Ñ+ò Ùðú÷ <Ð;ûô ò ØŠ	ðúsL   ¨B. ¶B"Á$B	Á4B"Á7B. Â	BÂB"ÂBÂB"Â"B+Â'B. Â.B<Â;B<)r"   r   ÚreturnzOptional[Path])r   ú
Path | strr=   zdict[str, Any]rP   ÚNone)N)r   rQ   rM   zOptional[int]rP   zlist[dict[str, Any]])Ú__doc__Ú
__future__r   r8   Ú	threadingr   r   Úpathlibr   Útypingr   r   Ú	lib.pathsr	   r
   r7   ÚLockr:   r   r   r)   rD   rO   © ó    r(   Ú<module>r\      sT   ðñõ #ã Û ß 'Ý ß  ç -à €ð
 ˆi�n‰nÓ€ð 9Ð ð	€óó>õ*r[   