+
    .pÐj6"  ã                  óž   € R t ^ RIHt ^ RIHt ^ RIHtHtHtH	t	 Rt
 ! R R]4      t] ! R R4      4       tR	 R
 ltR R lt ! R R4      tR# )u…  Carrier-aware user-turn rewind (``/undo``, ``/retry``) â€” the ONE implementation behind the CLI, the
gateway and the TUI. Rewind is a persisted-history operation: the durable transcript is the authority,
the warm (in-memory) history only has to agree with it. A composite compaction carrier (retained
summary + live human ask in one row) keeps its hidden handoff scaffold as the new head.)Úannotations)Ú	dataclass)ÚAnyÚDictÚListÚOptionalz<session history changed before the rewind could be persistedc                  ó   € ] tR t^tRtRtR# )ÚRewindTargetUnavailableErrora  The requested user turn is not a rewindable target of the active transcript: no user turns, an
ordinal past the newest one, a row that is not user-originated, or a plain turn where the caller
required a compaction carrier. Surfaces map this to their own "nothing to undo" message.© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__static_attributes__r
   ó    Ú"/opt/hermes/hermes_state_rewind.pyr	   r	      s   † õ`r   r	   c                  óJ   € ] tR t^t$ R]R&   R]R&   R]R&   R]R&   R]R	&   R
tR# )ÚRewindOutcomeúlist[dict[str, Any]]Úprefixúdict[str, Any]Ú	live_viewÚstrÚ	live_textÚintÚrewound_countÚturns_undoner
   N)r   r   r   r   Ú__annotations__r   r
   r   r   r   r      s   ‡ à Ó ØÓØƒNØÓØ×r   r   c               ó    € V ^8„  d   QhRRRR/# )é   Úmessagesr   Úreturnz	list[int]r
   )Úformats   "r   Ú__annotate__r$      s   € ÷ [ñ [Ð0ð [°Yñ [r   c                óp   € ^ RI Hp \        V 4       UUu. uF  w  r#V! V4      f   K  VNK  	  upp# u uppi )é    )Úuser_originated_turn_view)Úagent.context_compressorr'   Ú	enumerate)r!   r'   ÚiÚms   &   r   Ú_user_indicesr,      s0   € ÝBÜ# HÔ-ÔZÑ-‘$�!Ñ1JÈ1Ó1M�AˆAÑ-ÒZÐZùÓZs   •2¨2c               ó    € V ^8„  d   QhRRRR/# )r    Úmessager   r"   r   r
   )r#   s   "r   r$   r$   "   s   € ÷ tñ t ð t°Cñ tr   c                ó†   € ^ RI Hp ^ RIHp VP	                  V P                  R4      V! V P                  R4      4      4      # )z“Project content the way the durable row stores it (flush projection, then the read-side sanitize) so a
warm row and its durable twin compare equal.)Ú_durable_content)ÚSessionMessagesMixinÚroleÚcontent)Úagent.session_persistencer0   Úhermes_state_messagesr1   Ú_loaded_view_contentÚget)r.   r0   r1   s   &  r   Ú_comparison_contentr8   "   s9   € õ ;Ý:Ø×4Ñ4°W·[±[ÀÓ5HÑJZÐ[b×[fÑ[fÐgpÓ[qÓJrÓsÐsr   c                  ó:   € ] tR t^*tRtRRRRRRRR/R R	 lltR
tR# )ÚSessionRewindMixinz_``SessionDB`` mixin: soft-delete from one user turn onward, validated against the warm history.Úwarm_historyNÚrequire_retryableFÚrequire_compositeÚadopt_row_idsc               ó4   € V ^8„  d   QhRRRRRRRRR	RR
RRR/# )r    Ú
session_idr   Úuser_ordinalr   r;   zOptional[list[dict[str, Any]]]r<   Úboolr=   r>   r"   r   r
   )r#   s   "r   r$   ÚSessionRewindMixin.__annotate__-   sX   € ÷ Znñ ZnØðZnØ-0ðZnØCaðZnàðZnà<@ðZnàY]ðZnð 
ñZnr   c               óô  a'a(€ ^ RI HpHpHp	Hp
Ho( ^ RIHp ^ RIH	pH
p ^ RIHp V P                  V4      pV P                  VRR7      pV P                  VRRR7      p\        V4      pV^ 8  d   \!        \#        V4      V,           ^ 4      pV\#        V4      8¼  d   \%        R4      hVV,          pVV,          pV! VV4      w  ppV
! V4      w  ppV'       d   Vf   \%        R
4      hTpVe�   V Uu. uF  pV! V4      '       d   K  VNK  	  pp\        V4      p\#        V4      \#        V4      8w  d   \'        \(        4      hV! VVV,          4      w  pp\+        V4      \+        V4      8w  d   \'        \(        4      hV'       d   V	! VP-                  R4      4      MR	pVP-                  R4      o'\/        S'\0        4      '       g   \'        R4      h\3        V'V(3R lV 4       R	4      pVf   \'        \(        4      h V P5                  VS'VR	JVVP-                  R4      R7      p Teˆ   T P-                  R4      p"\/        T"\0        4      '       d	   T'       g   \'        R4      hTR,          P;                  RT"TR/4       T P-                  R4      ;p#'       d   T#TR,          T&   TR,          TR&   T'       dÇ   TTJdÁ   \#        T4      \#        T4      8X  d¨   \<        ;QJ d&    R \?        TT4       4       F  '       d   K   RM	  RM! R \?        TT4       4       4      '       dZ   \?        TT4       FI  w  pp$\/        T$P-                  R4      ;p%\0        4      '       d   T%TR&   T! T$4      ;p&'       g   KD  T&TT&   KK  	  \A        TTTe   TMT! TP-                  R4      4      \1        T P-                  R^ 4      4      \#        T4      T,
          R7      # u upi   \6         d   p!\%        \9        T!4      4      T!hR	p!?!ii ; i)aZ  Rewind the active transcript to just before user turn ``user_ordinal`` (0 = oldest; negative counts
back from the newest and clamps to the oldest, so ``-n`` is ``/undo n``). ``warm_history`` (CLI/TUI):
the in-memory view must have the same user turns and the same live target text as the durable
transcript, else ``RuntimeError`` and nothing changes; its (richer) prefix is what gets installed.
``require_retryable``: the live payload must be losslessly replayable as text (``ValueError`` from
:func:`retryable_user_text` before any write). ``require_composite``: the target must be a compaction
carrier. ``adopt_row_ids`` (TUI): copy durable ``_row_id`` identities onto the installed warm prefix so
clients can address follow-ups by row; the CLI leaves its history shape alone. Out-of-range /
wrong-shape targets raise :class:`RewindTargetUnavailableError`.)Ú_DB_PERSISTED_MARKERÚ#history_before_user_originated_turnÚretryable_user_textÚsplit_user_originated_turnr'   )Úflatten_message_text)ÚMESSAGE_UIDÚmessage_uid_or_none)Ú_is_ephemeral_scaffoldingT)Úinclude_row_ids)rM   Úrepair_alternationz3target user message is no longer in session historyNz/target user message is not a compaction carrierr3   Ú_row_idz)rewind target has no durable row identityc              3  ób   <"  € T F$  qP                  R 4      S8X  g   K  S! V4      x € K&  	  R# 5i)rO   N)r7   )Ú.0r+   Útarget_row_idr'   s   & €€r   Ú	<genexpr>Ú6SessionRewindMixin.rewind_user_turn.<locals>.<genexpr>f   s,   øé € Ð_±6¨a¿U¹UÀ9Ó=MÐQ^Ñ=^Ô)Ñ& q×)Ð)³6ùs   ƒ/Ÿ/)Úpreserve_compaction_handoffÚexpected_active_idsÚexpected_target_contentÚreplacement_message_idz,rewind did not retain its compaction handoffÚreplacement_message_uidc              3  ó$  "  € T F†  w  rVP                  R 4      VP                  R 4      8H  ;'       dU    \        VP                  R4      4      \        VP                  R4      4      8H  ;'       d    \        V4      \        V4      8H  x € Kˆ  	  R# 5i)r2   Údisplay_kindN)r7   rB   r8   )rQ   ÚwarmÚdurable_messages   &  r   rS   rT   x   s‘   é € ð i
ñ *EÑ%�ð �H‰H�VÓ × 3Ñ 3°FÓ ;Ñ;÷ Rð RÜ�T—X‘X˜nÓ-Ó.´$°×7JÑ7JÈ>Ó7ZÓ2[Ñ[÷Rð Rä# DÓ)Ô-@ÀÓ-QÑQôRó *Eùs   ‚0B³<BÁ0 BFr   )r   r   r   r   r   éÿÿÿÿ)!r(   rE   rF   rG   rH   r'   Úagent.message_contentrI   Úagent.message_metadatarJ   rK   r4   rL   Úget_active_message_idsÚget_messages_as_conversationr,   ÚmaxÚlenr	   ÚRuntimeErrorÚ_HISTORY_CHANGEDr8   r7   Ú
isinstancer   ÚnextÚrewind_to_messageÚ
ValueErrorr   ÚupdateÚallÚzipr   ))Úselfr@   rA   r;   r<   r=   r>   rE   rF   rG   rH   rI   rJ   rK   rL   rV   ÚstoredÚdurableÚdurable_userÚtarget_indexÚtargetÚdurable_prefixr   ÚscaffoldÚ_r   r+   r\   Ú	warm_userÚwarm_live_viewr   Ústored_viewÚresultÚexcÚreplacement_idÚreplacement_uidr]   Úrow_idÚuidrR   r'   s)   &&&$$$$                                @@r   Úrewind_user_turnÚ#SessionRewindMixin.rewind_user_turn-   s×  ù€ ÷	Cõ 	Cõ 	?ßKÝGà"×9Ñ9¸*ÓEÐØ×2Ñ2°:ÈtÐ2ÓTˆð
 ×3Ñ3°JÐPTÐimÐ3ÓnˆÜ$ WÓ-ˆØ˜!ÔÜœs <Ó0°<Õ?ÀÓCˆLØœ3˜|Ó,Ô,Ü.Ð/dÓeÐeØ# LÕ1ˆØ˜Õ&ˆÙ$GÈÐQ]Ó$^Ñ!ˆ˜	Ù0°Ó8‰ˆ�!ß Ò!1Ü.Ð/`ÓaÐaàˆØÒ#Ù+ÓP™|˜!Ñ3LÈQ×3O—A�A™|ˆDÐPÜ% dÓ+ˆIÜ�9‹~¤ \Ó!2Ô2Ü"Ô#3Ó4Ð4Ù%HÈÈyÐYeÕOfÓ%gÑ"ˆF�NÜ" 9Ó-Ô1DÀ^Ó1TÔTÜ"Ô#3Ó4Ð4÷ FWÑ'¨	¯©°iÓ(@ÔAÐ\`ˆ	ØŸ
™
 9Ó-ˆÜ˜-¬×-Ò-ÜÐJÓKÐKô Ý_±6Ó_ÐaeógˆàÒÜÔ/Ó0Ð0ð	BØ×+Ñ+Ø˜MÀxÐW[ÐG[Ø$7ÐQ\×Q`ÑQ`ÐajÓQkð ,ó mˆFð
 ÒØ#ŸZ™ZÐ(@ÓAˆNÜ˜n¬c×2Ò2¿.Ü"Ð#QÓRÐRà˜2Õ×%Ñ% y°.ÐBVÐX\Ð&]Ô^Ø"(§*¡*Ð-FÓ"GÐGˆÖGØ2A�˜rÕ" ;Ñ/Ø'¨Õ+ˆF�2‰Jß˜V¨>Ó9¼cÀ&»kÌSÐQ_ÓM`Ô>`×ehÓehñ i
ô *-¨V°^Ô)Dó	i
×eh×ehÒehñ i
ô *-¨V°^Ô)Dó	i
÷ f
ò f
ô *-¨V°^Ö)DÑ%��oÜ¨×(;Ñ(;¸IÓ(FÐF˜fÌ×LÒLØ&,�D˜‘OÙ-¨oÓ>Ð>�3×>Ð>Ø(+�D˜Ó%ñ	 *Eô
 Ø YØ#,Ò#8‘iÑ>RÐS\×S`ÑS`ÐajÓSkÓ>lÜ˜fŸj™j¨¸!Ó<Ó=ÌCÐP\ÓL]Ð`lÕLlônð 	nùò] Qøô. ô 	BÜ.¬s°3«xÓ8¸cÐAûð	Bús$   Ã0OÄOÇ9'O ÏO7ÏO2Ï2O7r
   )r   r   r   r   r   r€   r   r
   r   r   r:   r:   *   s;   † ÙiðZnØdhðZnà"'ðZnàCHðZnà`e÷Znó Znr   r:   N)r   Ú
__future__r   Údataclassesr   Útypingr   r   r   r   rf   rj   r	   r   r,   r8   r:   r
   r   r   Ú<module>r…      s`   ðñ[õ
 #å !ß ,Ó ,àQÐ ô` :ô `ð ÷ð ó ðõ[õ
t÷]nó ]nr   