
    :j(                     <    d Z ddlmZ ddlmZmZmZ  G d d      Zy)a  
Configuration Validator

This module provides comprehensive validation for configuration objects.
It ensures configuration completeness, correctness, and consistency.

Key Features:
- Type validation
- Business rule validation
- Dependency checking
- Error reporting
    )List   )ConfigLoadBalanceStrategy
TaskConfigc                       e Zd ZdZd ZdeddfdZdeddfdZdeddfdZdeddfd	Z	deddfd
Z
deddfdZdeddfdZdeddfdZdeddfdZy)ConfigValidatorz1Configuration validator with comprehensive checksc                     g | _         y)z"Initialize configuration validatorN)errors)selfs    7/root/.openclaw/workspace/harvester/config/validator.py__init__zConfigValidator.__init__   s	    !#    configreturnNc                    | j                   j                          | j                  |       | j                  |       | j	                  |       | j                  |       | j                  |       | j                  |       | j                  |       | j                   r0ddj                  d | j                   D              z   }t        |      y)zValidate complete configuration

        Args:
            config: Configuration object to validate

        Raises:
            ValueError: If validation fails
        z!Configuration validation failed:

c              3   &   K   | ]	  }d |   yw)z- N ).0errors     r   	<genexpr>z+ConfigValidator.validate.<locals>.<genexpr>>   s     HoZ_2eWHos   N)r   clear_validate_global_config_validate_pipeline_config_validate_monitoring_config_validate_tasks_config_validate_worker_manager_config_validate_rate_limits_validate_display_configjoin
ValueError)r   r   	error_msgs      r   validatezConfigValidator.validate   s     	 	$$V, 	&&v. 	((0 	##F+ 	,,V4 	""6* 	%%f- ;;<tyyHocgcncnHo?ooIY'' r   c                     |j                   }|j                  s| j                  j                  d       |j                  }|j
                  s'|j                  s| j                  j                  d       |j                  t        vr(| j                  j                  d|j                          |j                  s| j                  j                  d       |j                  dk  r| j                  j                  d       yy)zfValidate global configuration section

        Args:
            config: Configuration object
        z Global workspace cannot be emptyz5At least one GitHub session or token must be providedzInvalid load balance strategy: z(At least one user agent must be providedr   z)Max retries requeued must be non-negativeN)global_config	workspacer   appendgithub_credentialssessionstokensstrategyr   user_agentsmax_retries_requeued)r   r   r&   credentialss       r   r   z'ConfigValidator._validate_global_configA   s     ,, &&KKAB $66##K,>,>KKVW '::KK!@AUAU@VWX ((KKIJ --1KKJK 2r   c                    |j                   }h d}|D ]  }||j                  vr| j                  j                  d|        n1|j                  |   dk  r| j                  j                  d| d       ||j                  vr| j                  j                  d|        |j                  |   dk  s| j                  j                  d| d        y)	zhValidate pipeline configuration section

        Args:
            config: Configuration object
        >   checkgathersearchinspectz Missing thread count for stage: r   zThread count for z must be positivezMissing queue size for stage: zQueue size for N)pipelinethreadsr   r(   queue_sizes)r   r   r5   required_stagesstages        r   r   z)ConfigValidator._validate_pipeline_config^   s     ??B$ 	OEH,,,""%EeW#MN!!%(A-""%6ug=N#OP H000""%CE7#KL%%e,1""_UG;L#MN	Or   c                    |j                   }|j                  dk  r| j                  j                  d       d|j                  cxk  rdk  sn | j                  j                  d       |j
                  dk  r| j                  j                  d       |j                  dk  r| j                  j                  d       |j                  dk  r| j                  j                  d       yy)	zjValidate monitoring configuration section

        Args:
            config: Configuration object
        r   z+Monitoring update interval must be positiver   z'Error threshold must be between 0 and 1z$Queue threshold must be non-negativez!Memory threshold must be positivez#Response threshold must be positiveN)
monitoringupdate_intervalr   r(   error_thresholdqueue_thresholdmemory_thresholdresponse_threshold)r   r   r;   s      r   r   z+ConfigValidator._validate_monitoring_configt   s     &&
%%*KKLMZ//414KKHI%%)KKEF&&!+KKBC((A-KKDE .r   c                    |j                   s| j                  j                  d       |j                   D cg c]  }|j                  s| }}|s| j                  j                  d       |j                   D cg c]  }|j                  s|j                   }}t        |      t        t        |            k7  r| j                  j                  d       |j                   D ]  }| j                  |        yc c}w c c}w )zeValidate tasks configuration section

        Args:
            config: Configuration object
        z$At least one task must be configuredz!At least one task must be enabledzTask names must be uniqueN)tasksr   r(   enablednamelenset_validate_task)r   r   taskenabled_tasks
task_namess        r   r   z&ConfigValidator._validate_tasks_config   s     ||KKEF*0,,G$$,,GGKKBC -3LLIDDLLdiiI
Iz?c#j/22KK:; LL 	&D%	& H
 Js   C9C99C>C>rH   c                    |j                   sy|j                  s| j                  j                  d       |j                  s(| j                  j                  d|j                          |j
                  j                  r?|j
                  j                  s)| j                  j                  d|j                   d       	 |j                  j                          |j                  s)| j                  j                  d|j                          yt        |j                        D ]  \  }}|j                  j                  s.| j                  j                  d|d	z    d
|j                          |j                  rW|j                  j                  rn| j                  j                  d|d	z    d
|j                           y# t        $ r6}| j                  j                  d|j                   d|        Y d}~#d}~ww xY w)zjValidate individual task configuration

        Args:
            task: Task configuration object
        NzTask name cannot be emptyz(Provider type cannot be empty for task: zTask z( storage.plan requires storage.directoryz stage validation failed: z*At least one condition required for task: z#Key pattern required for condition r   z
 in task: z3Either query or key_pattern required for condition )rC   rD   r   r(   provider_typestorageplan	directorystagesr$   r"   
conditions	enumeratepatternskey_patternquery)r   rH   ei	conditions        r   rG   zConfigValidator._validate_task   s    ||yyKK:;!!KK!I$))UV<<T\\%;%;KKtyyk1YZ[	QKK  "
 KK!KDII;WX !*$// : y9 ))55KK&&)LQqSEQ[\`\e\e[f'gh y/A/A/M/MKK&&)\]^_`]`\aaklplulukv'wxy  	QKKtyyk1KA3OPP	Qs   ?G 	H+G<<Hc                    |j                   }|j                  dk  r| j                  j                  d       |j                  |j                  k  r| j                  j                  d       |j
                  dk  r| j                  j                  d       |j                  dk  r| j                  j                  d       d|j                  cxk  rdk  sn | j                  j                  d       d|j                  cxk  rdk  sn | j                  j                  d       |j                  |j                  k\  r| j                  j                  d	       y
y
)zfValidate worker manager configuration

        Args:
            config: Configuration object
        r   z-Worker manager min_workers must be at least 1z1Worker manager max_workers must be >= min_workersr   z5Worker manager target_queue_size must be non-negativez3Worker manager adjustment_interval must be positivez9Worker manager scale_up_threshold must be between 0 and 1z;Worker manager scale_down_threshold must be between 0 and 1z@Worker manager scale_down_threshold must be < scale_up_thresholdN)	workermin_workersr   r(   max_workerstarget_queue_sizeadjustment_intervalscale_up_thresholdscale_down_threshold)r   r   worker_managers      r   r   z/ConfigValidator._validate_worker_manager_config   s     %%)KKNO%%(B(BBKKRS++a/KKVW--2KKTUN5599KKZ[N77;!;KK\]...2S2SSKKab Tr   c                 h   |j                   j                         D ]  \  }}|j                  dk  r| j                  j	                  d|        |j
                  dk  r| j                  j	                  d|        d|j                  cxk  rdk  rvn | j                  j	                  d|         y)zcValidate rate limits configuration

        Args:
            config: Configuration object
        r   z+Base rate must be positive for rate limit: z-Burst limit must be positive for rate limit: r   z7Backoff factor must be between 0 and 1 for rate limit: N)
ratelimitsitems	base_rater   r(   burst_limitbackoff_factor)r   r   rD   
rate_limits       r   r   z%ConfigValidator._validate_rate_limits   s     !' 1 1 7 7 9 	eD*##q(""%PQUPV#WX%%*""%RSWRX#YZ
115A5""%\]a\b#cd	er   c                    |j                   s| j                  j                  d       y|j                   j                  s| j                  j                  d       y|j                   j                  j	                         D ]h  \  }}|s| j                  j                  d|        (|j	                         D ]-  \  }}d| d| d}|j
                  dk  r| j                  j                  | d	       n[|j
                  d
k  r| j                  j                  | d       n-|j
                  dkD  r| j                  j                  | d       |j                  dk  r| j                  j                  | d       n-|j                  dkD  r| j                  j                  | d       |j                  j                         r| j                  j                  | d       0 k y)zkValidate display configuration

        Args:
            config: Configuration object to validate
        z Display configuration is missingNz)Display contexts configuration is missingz)No display modes configured for context: zDisplay config [.]r   z: width must be positive(   z(: width should be at least 40 characters   z(: width should not exceed 200 charactersz': max_alerts_per_level must be positive   z+: max_alerts_per_level should not exceed 20z: title cannot be empty)	displayr   r(   contextsrd   widthmax_alerts_per_leveltitlestrip)r   r   context_namecontext_modes	mode_namemode_configprefixs          r   r    z(ConfigValidator._validate_display_config   s    ~~KKAB~~&&KKJK ,2>>+B+B+H+H+J 	K'L- ""%N|n#]^*7*=*=*? K&	;+L>9+QG $$)KK&&&1I'JK &&+KK&&&1Y'Z[ &&,KK&&&1Y'Z[ 33q8KK&&&1X'YZ 55:KK&&&1\']^ #((..0KK&&&1H'IJ'K	Kr   )__name__
__module____qualname____doc__r   r   r$   r   r   r   r   r   rG   r   r   r    r   r   r   r	   r	      s    ;$#(v #($ #(JLf L L:O O4 O,F& FT F.&V & &,"y: "y$ "yHcf c c:eF et e 'Kv 'K$ 'Kr   r	   N)r}   typingr   schemasr   r   r   r	   r   r   r   <module>r      s#     < <DK DKr   