
    yj                         d 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e
dede
d	e
dz  d
e
f
dZ G d de          ZdS )z
Base event classes and idempotency key generation for CloudEvents telemetry.

This module provides:
- BaseEvent: Abstract base class for all Honcho telemetry events
- generate_event_id(): Deterministic event ID generation for idempotency
    N)UTCdatetime)ClassVar)	BaseModelField
event_type	timestampresource_idhoncho_versionreturnc                 D   |pd}|  d| d|                                  d| }t          j        |                                                                          dd         }t          j        |                                                              d          }d| S )uU  Generate a deterministic event ID for idempotency.

    Same inputs always produce the same ID, so retries are automatically
    deduplicated on the receiving end. `honcho_version` is folded into the
    payload so two deploys emitting the same logical event produce distinct
    IDs — protects against silently merging events whose payload shape may
    have shifted between versions.

    Args:
        event_type: The CloudEvents type (e.g., "honcho.work.representation.completed")
        timestamp: When the event occurred
        resource_id: A unique identifier for the resource (can include workspace_id if relevant)
        honcho_version: The honcho package version emitting the event;
            included in the hash so cross-deploy events don't dedupe.

    Returns:
        A deterministic event ID in the format "evt_{base64_hash}"
     :N   =evt_)		isoformathashlibsha256encodedigestbase64urlsafe_b64encodedecoderstrip)r   r	   r
   r   version_segmentpayload
hash_bytesencodeds           A/DATA/AppData/hermes/projects/honcho/src/telemetry/events/base.pygenerate_event_idr!      s    0 %*OUUkUUI,?,?,A,AUUOUUG 0 01188::3B3?J&z2299;;BB3GGG'    c                      e Zd ZU dZee         ed<   ee         ed<   ee         ed<   dZee         ed<    e	d d	          Z
eed
<   edefd            Zedefd            Zedefd            Zedefd            ZdefdZdefdZdS )	BaseEventa  Base class for all Honcho telemetry events.

    All events inherit from this class and define:
    - _event_type: The CloudEvents type string
    - _schema_version: Integer version for schema evolution
    - _category: Event category (work, activity, resource)

    Subclasses must implement the abstract class methods by setting class variables
    and providing get_resource_id(). Subclasses define their own context fields
    (e.g., workspace_id, workspace_name) as needed - not all events have workspace context.
    _event_type_schema_version	_categoryground_truth_volume_classc                  4    t          j        t                    S N)r   nowr    r"   r    <lambda>zBaseEvent.<lambda>K   s    S 1 1 r"   zWhen the event occurred (UTC))default_factorydescriptionr	   r   c                     | j         S )z2Return the CloudEvents type string for this event.)r%   clss    r    r   zBaseEvent.event_typeO   s     r"   c                     | j         S )z)Return the schema version for this event.)r&   r2   s    r    schema_versionzBaseEvent.schema_versionT   s     ""r"   c                     | j         S )z8Return the event category (work, activity, or resource).)r'   r2   s    r    categoryzBaseEvent.categoryY   s     }r"   c                     | j         S )zFReturn the volume class for sampling: 'ground_truth' or 'high_volume'.)r)   r2   s    r    volume_classzBaseEvent.volume_class^   s       r"   c                      t          d          )zReturn the resource ID for idempotency key generation.

        This should be a unique identifier for the specific operation,
        such as a task_id or a hash of message IDs.

        Must be implemented by subclasses.
        z+Subclasses must implement get_resource_id())NotImplementedError)selfs    r    get_resource_idzBaseEvent.get_resource_idc   s     ""OPPPr"   c                     ddl m} t          |                                 | j        |                                 |          S )u&  Generate a deterministic event ID for this event instance.

        Folds in the honcho package version so the same logical event from
        two different deploys produces distinct ids — downstream dedupe by
        id won't silently merge events whose body shape may have shifted.
        r   )HONCHO_VERSION)r   r	   r
   r   )src._versionr?   r!   r   r	   r=   )r<   r?   s     r    generate_idzBaseEvent.generate_idm   sQ     	0///// ((n,,..)	
 
 
 	
r"   N)__name__
__module____qualname____doc__r   str__annotations__intr)   r   r	   r   classmethodr   r5   r7   r9   r=   rA   r-   r"   r    r$   r$   1   sq        
 
 #c]"""} $2M8C=111  %113  Ix   
 3    [ #s # # # [#     [ !S ! ! ! [!Q Q Q Q Q
S 
 
 
 
 
 
r"   r$   r+   )rE   r   r   r   r   typingr   pydanticr   r   rF   r!   r$   r-   r"   r    <module>rL      s       " " " " " " " "       % % % % % % % % "&	   $J	
 	   @J
 J
 J
 J
 J
	 J
 J
 J
 J
 J
r"   