Ë
    ÚNñj"  ã                  ó$  — d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	Z	ddl
Z
 ee«      j                  «       j                  j                  dz  Z ee«      j                  «       j                  j                  dz  dz  d	z  Zdd
lmZ  ed¬«      d%d„«       Z ed¬«      d&d„«       Zd'd(d„Zd'd(d„Zd'd)d„Zd*d„Zd+d„Zd,d„Zdddœ	 	 	 	 	 	 	 	 	 d-d„Zdddœ	 	 	 	 	 	 	 d.d„Zd/d„Zd0d„Zd1d„Zd2d„Zd3d „Z  G d!„ d"e!«      Z"	 	 	 	 	 	 d4d#„Z#d5d$„Z$y)6z\Pipeline manifest loader.

Loads and validates pipeline YAML manifests from pipeline_defs/.
é    )ÚannotationsN)ÚPath)ÚAnyÚOptionalÚpipeline_defsÚschemasÚ	pipelineszpipeline_manifest.schema.json)Ú	lru_cacheé   )Úmaxsizec                 óz   — t        t        d¬«      5 } t        j                  | «      cd d d «       S # 1 sw Y   y xY w)Núutf-8©Úencoding)ÚopenÚSCHEMA_PATHÚjsonÚload)Úfs    úAC:\Ai_Coding\apps\studio\tools\OpenMontage\lib\pipeline_loader.pyÚ_load_manifest_schemar      s%   € ä	Œk GÕ	,°Ü�y‰y˜‹|÷ 
-×	,Ò	,ús   ’1±:é@   c                ó<   — t        | |rt        |«      «      S d«      S )z;Cached manifest load. Treat the returned dict as READ-ONLY.N)Úload_pipeliner   )ÚnameÚdefs_dir_keys     r   Ú_load_pipeline_cachedr   !   s    € ô ˜±\œt LÓ1ÓLÐLÀtÓLÐLó    c                ó<   — t        | |rt        |«      «      S d«      S )a  Load a manifest through a cache. The result MUST NOT be mutated.

    Manifests are immutable within a run; hot paths (gate checks on every
    checkpoint write, board state derivation) should use this instead of
    re-parsing YAML + re-validating the schema each call.
    Ú )r   Ústr)r   Údefs_dirs     r   Úload_pipeline_readonlyr#   '   s   € ô ! ¹¤s¨8£}ÓIÐIÀbÓIÐIr   c                ó  — |xs t         }|| › d�z  }|j                  «       st        d|› �«      ‚t        |d¬«      5 }t	        j
                  |«      }ddd«       t        «       }t        j                  |¬«       |S # 1 sw Y   Œ,xY w)zìLoad and validate a pipeline manifest by name.

    Args:
        name: Pipeline name (without .yaml extension).
        defs_dir: Override directory for pipeline definitions.

    Returns:
        Validated pipeline manifest dict.
    z.yamlzPipeline manifest not found: r   r   N)ÚinstanceÚschema)	ÚPIPELINE_DEFS_DIRÚexistsÚFileNotFoundErrorr   ÚyamlÚ	safe_loadr   Ú
jsonschemaÚvalidate)r   r"   Úpathr   Úmanifestr&   s         r   r   r   1   s‚   € ð Ò,Ô,€HØ˜˜˜e�nÑ$€DØ�;‰;Œ=ÜÐ"?À¸vÐ FÓGÐGä	ˆd˜WÕ	%¨Ü—>‘> !Ó$ˆ÷ 
&ô #Ó$€FÜ×Ñ °&Õ9à€O÷ 
&Ð	%ús   ¾A?Á?Bc                ór   — | xs t         } | j                  d«      D �cg c]  }|j                  ‘Œ c}S c c}w )z+List all available pipeline manifest names.z*.yaml)r'   ÚglobÚstem)r"   Úps     r   Úlist_pipelinesr4   I   s4   € àÒ,Ô,€HØ$ŸM™M¨(Ô3Ó4Ñ3�qˆA�F‹FÐ3Ñ4Ð4ùÒ4s   ž4c                óB   — | sy|syt        |j                  | «      «      S )z=Evaluate a simple manifest condition against runtime context.TF©ÚboolÚget)Ú	conditionÚcontexts     r   Ú_condition_is_activer;   O   s"   € áØÙØÜ�—‘˜IÓ&Ó'Ð'r   c                ó.   — | j                  di «      xs i S )z=Return reference-input configuration, defaulting to disabled.Úreference_input©r8   ©r/   s    r   Úget_reference_input_configr@   X   s   € à�<‰<Ð)¨2Ó.Ò4°"Ð4r   c                óJ   — t        t        | «      j                  dd«      «      S )z@Whether the manifest declares support for reference-video input.Ú	supportedF)r7   r@   r8   r?   s    r   Ú!pipeline_supports_reference_inputrC   ]   s    € äÔ*¨8Ó4×8Ñ8¸ÀeÓLÓMÐMr   T©r:   Úinclude_inactivec          	     óÐ   — | d   D ]X  }|d   |k7  rŒt        |j                  dg «      «      }|r|c S |D �cg c]  }t        |j                  d«      |«      r|‘Œ! c}c S  g S c c}w )zìReturn sub-stage definitions for a stage.

    By default this returns all declared sub-stages so agents can inspect the
    full workflow shape. Pass ``include_inactive=False`` with context to filter
    to active sub-stages only.
    Ústagesr   Ú
sub_stagesr9   )Úlistr8   r;   )r/   Ú
stage_namer:   rE   ÚstagerH   Ú	sub_stages          r   Úget_stage_sub_stagesrM   b   s�   € ð ˜(Ô#ˆØ�‰=˜JÒ&ØÜ˜%Ÿ)™) L°"Ó5Ó6ˆ
ÙØÒñ (ó
á'�	Ü# I§M¡M°+Ó$>ÀÔHò Ø'ñ
ò 	
ð $ð €Iùò
s   ·$A#F)Úinclude_sub_stagesr:   c               ó¶   — g }| d   D ]N  }|j                  |d   «       |sŒt        | |d   ||du ¬«      D ]  }|j                  |d   › d|d   › �«       Œ  ŒP |S )a  Extract the ordered list of stage names from a manifest.

    ``include_sub_stages=True`` exposes declarative sample/preview units to the
    agent without turning them into mandatory checkpoint stages. Sub-stages are
    emitted as ``<stage>.<sub_stage>``.
    rG   r   NrD   Ú.)ÚappendrM   )r/   rN   r:   ÚorderrK   rL   s         r   Úget_stage_orderrS   }   s{   € ð €EØ˜(Ô#ˆØ�‰�U˜6‘]Ô#Ù!ØÜ-ØØ�&‰MØØ$¨˜_÷	
ˆIð �L‰L˜E &™M˜?¨!¨I°fÑ,=Ð+>Ð?Õ@ñ
ð	 $ð €Lr   c                ó¸  — t        «       }| d   D ]�  }|j                  |j                  dg «      «       |j                  |j                  dg «      «       |j                  |j                  dg «      «       |j                  dg «      D ]#  }|j                  |j                  dg «      «       Œ% ŒŸ |j                  t        | «      j                  dg «      «       |S )zFCollect tools across stages, sub-stages, and reference-input analysis.rG   Úpreferred_toolsÚfallback_toolsÚtools_availablerH   Úanalysis_tools)ÚsetÚupdater8   r@   )r/   ÚtoolsrK   rL   s       r   Úget_required_toolsr\   ˜   s´   € ä“e€EØ˜(Ô#ˆØ�‰�U—Y‘YÐ0°"Ó5Ô6Ø�‰�U—Y‘YÐ/°Ó4Ô5Ø�‰�U—Y‘YÐ0°"Ó5Ô6ØŸ™ <°Ö4ˆIØ�L‰L˜Ÿ™Ð'8¸"Ó=Õ>ñ 5ð	 $ð 
‡L�LÔ+¨HÓ5×9Ñ9Ð:JÈBÓOÔPØ€Lr   c           	     ó(  — d}| d   D ]  }|d   |k7  rŒt        «       }|D ]#  }|j                  |j                  |g «      «       Œ% |j                  dg «      D ]*  }|D ]#  }|j                  |j                  |g «      «       Œ% Œ, |c S  t        «       S )z=Collect every tool a stage or one of its sub-stages declares.)Úrequired_toolsÚoptional_toolsrU   rV   rW   rG   r   rH   )rY   rZ   r8   )r/   rJ   Útool_fieldsrK   r[   ÚfieldrL   s          r   Úget_stage_toolsrb   ¥   s—   € ð€Kð ˜(Ô#ˆØ�‰=˜JÒ&ØÜ›%ˆÛ ˆEØ�L‰L˜Ÿ™ 5¨"Ó-Õ.ð !àŸ™ <°Ö4ˆIÛ$�Ø—‘˜YŸ]™]¨5°"Ó5Õ6ñ %ð 5ð Šð $ô ‹5€Lr   c                óN   — | d   D ]  }|d   |k(  sŒ|j                  d«      c S  y)z3Get the skill path for an instruction-driven stage.rG   r   ÚskillNr>   ©r/   rJ   rK   s      r   Úget_stage_skillrf   »   s1   € à˜(Ô#ˆØ�‰=˜JÓ&Ø—9‘9˜WÓ%Ò%ð $ð r   c                ób   — | d   D ]'  }|d   |k(  sŒt        |j                  dd«      «      c S  y)uè   Whether a stage gates on human approval. None if the stage isn't declared.

    This is the single lookup used by gate enforcement (lib/checkpoint.py)
    and the Backlot board â€” keep them reading the same field the same way.
    rG   r   Úhuman_approval_defaultFNr6   re   s      r   Ú get_stage_human_approval_defaultri   Ã   s;   € ð ˜(Ô#ˆØ�‰=˜JÓ&Ü˜Ÿ	™	Ð":¸EÓBÓCÒCð $ð r   c                óR   — | d   D ]  }|d   |k(  sŒ|j                  dg «      c S  g S )z'Get the review focus items for a stage.rG   r   Úreview_focusr>   re   s      r   Úget_stage_review_focusrl   Ï   s5   € à˜(Ô#ˆØ�‰=˜JÓ&Ø—9‘9˜^¨RÓ0Ò0ð $ð €Ir   c                  ó   — e Zd ZdZy)ÚExtensionNotPermittedzMRaised when a capability extension is used but not permitted by the pipeline.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   rn   rn   Û   s   „ ÚWr   rn   c           	     óÚ   — h d£}||vrt        d|›dt        |«      › �«      ‚| j                  di «      }|j                  |d«      s%t        d| j                  dd«      ›d	|› d
|› d�«      ‚y)aV  Enforce that a capability extension is permitted by the pipeline manifest.

    Args:
        manifest: Loaded pipeline manifest dict.
        extension_type: One of 'custom_scripts', 'custom_playbooks',
                        'custom_skills', 'custom_tools'.

    Raises:
        ExtensionNotPermitted: If the extension is not allowed.
    >   Úcustom_toolsÚcustom_skillsÚcustom_scriptsÚcustom_playbookszUnknown extension type z. Valid types: Ú
extensionsFz	Pipeline r   Úunknownz does not permit z. Set extensions.z.: true in the pipeline manifest to allow this.N)Ú
ValueErrorÚsortedr8   rn   )r/   Úextension_typeÚvalid_extensionsry   s       r   Úcheck_extension_permittedr   ß   s    € ò _ÐØÐ-Ñ-ÜØ% nÐ%7ð 8Ü"Ð#3Ó4Ð5ð7ó
ð 	
ð
 —‘˜l¨BÓ/€JØ�>‰>˜.¨%Ô0Ü#Ø˜Ÿ™ V¨YÓ7Ð:Ð:KØÐÐ/°Ð/?ð @/ð0ó
ð 	
ð 1r   c           	     ó¨   — dddddœ}| j                  di «      }|j                  «       D ��ci c]  \  }}||j                  ||«      “Œ c}}S c c}}w )z5Return the extension permission flags for a pipeline.F)rw   rx   rv   ru   ry   )r8   Úitems)r/   Údefaultsry   ÚkÚvs        r   Úget_permitted_extensionsr…   ý   s^   € ð  Ø!ØØñ	€Hð —‘˜l¨BÓ/€JØ08·±Ô0@ÔAÑ0@©¨¨1ˆAˆz�~‰~˜a Ó#Ñ#Ð0@ÒAÐAùÓAs   ­A)ÚreturnÚdict)r   r!   r   r!   r†   údict[str, Any])N)r   r!   r"   úOptional[Path]r†   rˆ   )r"   r‰   r†   ú	list[str])r9   úOptional[str]r:   úOptional[dict[str, Any]]r†   r7   )r/   r‡   r†   rˆ   )r/   r‡   r†   r7   )
r/   r‡   rJ   r!   r:   rŒ   rE   r7   r†   zlist[dict[str, Any]])r/   r‡   rN   r7   r:   rŒ   r†   rŠ   )r/   r‡   r†   úset[str])r/   r‡   rJ   r!   r†   r�   )r/   r‡   rJ   r!   r†   r‹   )r/   r‡   rJ   r!   r†   zOptional[bool])r/   r‡   rJ   r!   r†   rŠ   )r/   r‡   r}   r!   r†   ÚNone)r/   r‡   r†   zdict[str, bool])%rr   Ú
__future__r   r   Úpathlibr   Útypingr   r   r*   r,   Ú__file__ÚresolveÚparentr'   r   Ú	functoolsr
   r   r   r#   r   r4   r;   r@   rC   rM   rS   r\   rb   rf   ri   rl   ÚPermissionErrorrn   r   r…   rs   r   r   Ú<module>r—      s�  ðñõ
 #ã Ý ß  ã Û á˜“N×*Ñ*Ó,×3Ñ3×:Ñ:¸_ÑLÐ áˆƒN×ÑÓ×#Ñ#×*Ñ*Øñàñð &ñ&ð õ  ñ �1Ôòó ðñ
 �2ÔòMó ðMô
Jôô05ó(ó5ó
Nð )-Ø!ñØðàðð &ð	ð
 ðð óð<  %Ø(,ñ	Øðð ðð &ð	ð
 óó6
óó,ó	óôX˜Oô Xð
Øð
àð
ð 
ó
ô<	Br   