
    `gj=                    l    d Z ddlmZ ddlmZmZmZ dZdd	 	 	 	 	 	 	 	 	 d	dZdd	 	 	 	 	 	 	 	 	 d	dZ	y)
a  Derive ACP session-provenance metadata from the existing compression chain.

This is an additive Hermes extension surfaced under ACP ``_meta.hermes`` so
existing ACP clients ignore it. It carries no new persisted state: everything
is derived on demand from the ``sessions`` table (``parent_session_id`` /
``end_reason``), which already models compression-continuation chains.

The ACP/editor ``session_id`` stays the stable public handle. When context
compression rotates the internal Hermes head, ``build_session_provenance`` lets
a client see the previous/current internal ids and the lineage root without
parsing status text, guessing from token drops, or reading ``state.db``.
    )annotations)AnyDictOptionald   Nprevious_hermes_session_idc                  	 | j                  |      }|sy|j                  d      }|j                  d      }|}d}|}	|h}
t        t              D ]]  }|	r|	|
v r nU|
j                  |	       	 | j                  |	      }|s n.|	}|j                  d      dk(  r|dz  }|j                  d      }	_ d}|r*	 | j                  |      }|r|j                  d      dk(  rd}t        |xr ||k7        }|||||rd	nd
|d}|r||d<   |r
d|d<   d|d<   |S # t        $ r Y yw xY w# t        $ r d}Y w xY w# t        $ r d}Y yw xY w)az  Build ``_meta.hermes.sessionProvenance`` for an ACP session.

    Args:
        db: A ``SessionDB`` (must expose ``get_session``).
        acp_session_id: The stable ACP/editor-facing session handle.
        current_hermes_session_id: The live internal Hermes DB session id
            (``state.agent.session_id``).
        previous_hermes_session_id: The internal id from before the most recent
            turn, when known. Supplied by ``prompt()`` to flag a rotation.

    Returns:
        A dict suitable for ``{"hermes": {"sessionProvenance": <dict>}}`` under
        ACP ``_meta``, or ``None`` if the session can't be read.
    Nparent_session_id
end_reasonr   compression   FTcontinuationroot)acpSessionIdcurrentHermesSessionIdrootHermesSessionIdparentHermesSessionIdsessionKindcompressionDepthpreviousHermesSessionIdreasoncreatorKind)get_session	Exceptiongetrange	_MAX_WALKaddbool)dbacp_session_idcurrent_hermes_session_idr	   row	parent_idr   root_idcompression_depthcursor_parentseen_prowis_continuationimmediate_parentrotated
provenances                    I/root/.hermes/venv/lib/python3.12/site-packages/acp_adapter/provenance.pybuild_session_provenancer1      s   *nn67 +,I&J (GM%&D9 6 5	>>-0D 88L!]2"!456" O	$!~~i8  0 4 4\ Bm S"O" 	D&*CCG '";&!*)8~f-"J "0J
,-  -
8$1
=!  ,  	D	  	$#	$s5   D -D!5D2 	DD!D/.D/2E ?E c               2    t        | |||      }|ydd|iiS )zMReturn a ready ``_meta`` payload: ``{"hermes": {"sessionProvenance": ...}}``.r   NhermessessionProvenance)r1   )r!   r"   r#   r	   provs        r0   session_provenance_metar6   o   s5     $
!#=	D |*D122    )
r!   r   r"   strr#   r8   r	   zOptional[str]returnzOptional[Dict[str, Any]])
__doc__
__future__r   typingr   r   r   r   r1   r6    r7   r0   <module>r>      s    # & & 	 15VVV  #V
 !.V V| 15333  #3
 !.3 3r7   