Ë
    âß²jVN ã            5       óì  — U d Z ddl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	ddl
Z
ddlZddlZddlZddlZddlZddlZ	 ddlZ	 ddlZddlmZmZmZ ddlmZ ddlmZ ddlmZmZmZ dd	lm Z  dd
l!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*  e	jV                  e,«      Z-ddl.m/Z0 ddl.m1Z1 ddl2m3Z3m4Z4 da5da6e"e7   e8d<   de7fd„Z9 e«       ju                  «       Z;e;dz  Z<e<dz  Z=e<dz  Z>e<dz  Z?dZ@ ej‚                  «       ZB ej†                  «       ZDi ZEe#eFej‚                  f   e8d<    ejŽ                  «       ZH ej†                  «       ZIdZJe<dz  ZKdZL ed¬«       G d„ d«      «       ZM edd¬«      ZNee"eM      e8d <    G d!„ d"«      ZO ed#d¬«      ZPee"eO      e8d$<   ej¢                  d%eFfd&„«       ZRd%eFde7fd'„ZS eMe<e=eK«      ZTdeMfd(„ZUej¢                  d)e)eFef   fd*„«       ZVdefd+„ZWd,ZXd-ZYdeZfd.„Z[d%eFde7fd/„Z\defd0„Z]d1eZde"e7   fd2„Z^�dnd3„Z_ej¢                  d4„ «       Z`ej¢                  d%eFfd5„«       Zad%eFd6e&g e%f   de%fd7„Zbej¢                  d%eFd8eFfd9„«       Zc edd:h«      Zed%eFdefd;„Zf�dod<e"eF   d=e"e%   de$eF   fd>„Zgd?e#eFe%f   de#eFe%f   fd@„Zh�dpdAe%dBeFdeFfdC„Zi edh dD£«      ZjdEZkdFZld?e#eFe%f   de7fdG„Zmd?e#eFe%f   deFfdH„Znd?e#eFe%f   de#eFe%f   fdI„Zod?e#eFe%f   de7fdJ„Zpd?e#eFe%f   de7fdK„Zqd?e#eFe%f   deFfdL„Zrd?e#eFe%f   de7fdM„Zsd?e#eFe%f   de7fdN„ZtdOefdP„ZudOefdQ„ZvdOedRe"ejî                     ddfdS„ZxdOede7fdT„ZydUeddfdV„ZzdW„ Z{dXe%de"e|   fdY„Z}dZdd[d\œZ~d]eFde|fd^„Zi d_d`“dad`“dbdc“dddc“dedf“dgdf“dhdf“didj“dkdj“dldj“dmdn“dodn“dpdn“dqdn“drds“dtds“dudv“dwdvi¥Z€dxdxdxdydydzdzd{œZ�d|eFde"e‚   fd}„Zƒd~eFde"eF   fd„Z„d€eFd�eFd‚eFdƒeFde#eFe%f   f
d„„Z…d…e|de#eFe%f   fd†„Z†d‡eFde#eFe%f   fdˆ„Z‡d‰edefdŠ„ZˆdAe%de"e   fd‹„Z‰dŒed�ede7fdŽ„ZŠdŒed�ede7fd�„Z‹dd�œd‡e#eFe%f   d‘ed’e"eF   de"eF   fd“„ZŒdZ�d”ZŽd‡e�de|fd•„Z�d–Z‘d—eZd˜e|deFfd™„Z’da“e|e8dš<   d›Z”g Z•e–e8dœ<   d?e#eFe%f   d‡e#eFe%f   d‘ede7fd�„Z—i Z˜e#eFe"eZ   f   e8dž<   d‡e#eFe%f   de"eZ   fdŸ„Z™d eFd¡e#eFe%f   d¢e–ddfd£„Zšd?e#eFe%f   d¤eFddfd¥„Z›de#eFe%f   fd¦„Zœd‡e#eFe%f   d§ede7fd¨„Z�d©ZždªZŸd«Z d‡e#eFe%f   d¬ed§edeFfd­„Z¡da¢e|e8d®<   g Z£e–e8d¯<   d?e#eFe%f   d¬ed§eddfd°„Z¤de#eFe%f   fd±„Z¥�dqd‡e#eFe%f   d’e"eF   de"eF   fd²„Z¦d³eFd|eFd´eFddfdµ„Z§�drd·e7ddfd¸„Z¨d³eFde"eZ   fd¹„Z©de"eZ   fdº„Zªde"eZ   fd»„Z«de|fd¼„Z¬�dnd½„Z­d¾eFddfd¿„Z®�dndÀ„Z¯de"eF   fdÁ„Z°dÂede(e%e7f   fdÃ„Z±de$e#eFe%f      fdÄ„Z²de"e$e#eFe%f         fdÅ„Z³dÂede"e(e|e|e|f      fdÆ„Z´dÇe"e(e|e|e|f      ddfdÈ„ZµdÉe$e#eFe%f      dÊe"e*eF      de$e#eFe%f      fdË„Z¶ddÌœdÉe$e#eFe%f      dÊe"e*eF      de$e#eFe%f      fdÍ„Z·dOe"eF   ddfdÎ„Z¸dÂedÉe$e#eFe%f      deFfdÏ„Z¹dÐZºdd¶dÑœdÉe$e#eFe%f      dÊe"e*eF      dÒe7fdÓ„Z»dd¶dÑœdÉe$e#eFe%f      dÊe"e*eF      dÒe7fdÔ„Z¼ e½«       Z¾	 �dqd%e%d6e&e$e#eFe%f      e|e#eFe%f   ge%f   dÕe%de%fdÖ„Z¿d?e#eFe%f   ddfd×„ZÀd?e#eFe%f   ddfdØ„ZÁdÙe"eF   de"eF   fdÚ„ZÂde(e"eF   e"eF   f   fdÛ„ZÃd¶dÜœdAe%dÝe7de"eF   fdÞ„ZÄdAe%de"eF   fdß„ZÅdàe%de"e$eF      fdá„ZÆdAe%de"e$eF      fdâ„ZÇdAe%de"eF   fdã„ZÈdAe%de"eF   fdä„ZÉeÄeÄeÅeÄeÄeÄdå„ eÂe7eÇeÈdæœZÊe#eFe&e%ge%f   f   e8dç<   dè„ eÄeÄeÉdéœZËe#eFe&e%ge%f   f   e8dê<   dëe"eF   dìe"eF   díe7dîe"eF   ddf
dï„ZÌdðe%deÍfdñ„ZÎdòe#eFe%f   dóeFdôe%dõeFde"eF   f
dö„ZÏ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 �dsd÷e"eF   d‡eFd³e"eF   dXe"e|   døe"eF   dùe"e#eFe%f      d<e"eF   d=e"e$eF      dúe"eF   dûe"eF   düe"eF   dîe"eF   dýe"e)eFe$eF   f      dþe"e$eF      dÙe"eF   díe7dÿe"e7   dëe"eF   dìe"eF   �d e"eF   �de"eF   �de7�de"eF   �de7de#eFe%f   f2�d„ZÐd%eFde"e#eFe%f      f�d„ZÑ G �d„ �deÒ«      ZÓ�d	eFde"e#eFe%f      f�d
„ZÔ�dr�de7de$e#eFe%f      f�d„ZÕd?e#eFe%f   �de#eFe%f   d%eFddf�d„ZÖd?e#eFe%f   �de#eFe%f   ddf�d„Z×d?e#eFe%f   �de#eFe%f   ddf�d„ZØd?e#eFe%f   �de#eFe%f   ddf�d„ZÙ�de#eFe%f   �de#eFe%f   d%eFddf�d„ZÚ�de#eFe%f   ddf�d„ZÛd%eF�de#eFe%f   de"e#eFe%f      f�d„ZÜ�dqd%eF�de"eF   de"e#eFe%f      f�d„ZÝd%eFde"e#eFe%f      f�d„ZÞ�dqd%eF�de"eF   de"e#eFe%f      f�d„Zß�de#eFe%f   de7f�d„Zà�de%d‘e�deZde7f�d„Zá�dZâd%eFdðe%de"e#eFe%f      f�d „Zãd%eFde7f�d!„Zäd%eF�d"eFdAe7de7f�d#„Zåd%eFde7f�d$„Zæd%eFddf�d%„Zçd%eF�d&eFde7f�d'„Zèd?e#eFe%f   d·e7�d(e"eF   �d)e"eF   �d*e"eF   d‘eFddf�d+„Zéd?e#eFe%f   d‘eFddf�d,„Zê	 	 	 �dtdd¶d�d-œd%eFd·e7�d(e"eF   �d)e"eF   �d*e"eF   �d.e"eF   �d/e7�d0e"eZ   de7f�d1„Zëd?e#eFe%f   d|eFdõeFde7f�d2„Zìd?e#eFe%f   ddf�d3„Zíd?e#eFe%f   �d4eFddf�d5„Zîd%eFde7f�d6„ZïdÉe$e#eFe%f      �de%d8eFde7f�d7„Zðd%eFd8eFde7f�d8„Zñd%eFde7f�d9„Zòde|f�d:„Zód%eFde7f�d;„ZôdeFf�d<„Zõed¶d¶d¶�d=œd%eF�d>e|�d?e7�d@e7�dAe7de)e7e#eFe%f   f   f�dB„Zöd%eFd8eFde7f�dC„Z÷�dDZø�dEeF�dFe%�dGe&e%ge%f   de%f�dH„ZùdeZf�dI„ZúddÌœ�dJe$e#eFe%f      d‘edÊe"e'eF      de7f�dK„Zûde$e#eFe%f      f�dL„Züe G �dM„ �dN«      «       Zý�dJe$e#eFe%f      de7f�dO„Zþd?e#eFe%f   �dPeýddf�dQ„Zÿd?e#eFe%f   �dPeýde"eF   f�dR„�Z e G �dS„ �dT«      «       �Z�dU�ede7f�dV„�Z�dU�edef�dW„�Z�dU�ede7f�dX„�Z�dU�e�dYe|de7f�dZ„�Z�dU�ede7f�d[„�Zd?e#eFe%f   �dPeýde7f�d\„�Zd?e#eFe%f   �dPeýde"eF   f�d]„�Zd?e#eFe%f   �dPeý�d^eZde7f�d_„�Z	de$e#eFe%f      f�d`„�Z
�da�Zde|f�db„�Z�dce�dde|de|f�de„�Zd%eFdeFf�df„�Z�dge%deFf�dh„�Zde'eF   f�di„�Z	 �do�dje"e#eFeFf      �dke"e$eF      de#eFe%f   f�dl„�Zd%eFddf�dm„�Zy# e$ r dZY �Œ!w xY w# e$ r dZY �Œ*w xY w(u  zcCron job storage: ~/.hermes/cron/jobs.json; output in
~/.hermes/cron/output/{job_id}/{timestamp}.mdé    N)Ú
ContextVar)Ú	dataclassÚfield)ÚdatetimeÚ	timedeltaÚtimezone)ÚPath)Úget_hermes_home)ÚCLAIM_TTL_INACTIVITY_HEADROOMÚFIRE_CLAIM_SKEW_SECONDSÚFIRE_CLAIM_TTL_SECONDS)Úcron_env_setting)	ÚOptionalÚDictÚListÚAnyÚCallableÚSetÚTupleÚUnionÚ
Collection)Únow)Úget_timezone)Úatomic_replaceÚatomic_write_textÚHAS_CRONITERÚreturnc                  óž   — t         €	 ddlm}  | ada t        t         «      S t        t         «      S # t        $ r da Y t        t         «      S w xY w)zDImport croniter on first use; honor a pre-set HAS_CRONITER override.r   )ÚcroniterTF)r   r   ÚImportErrorÚbool)Ú	_croniters    ú./home/ubuntu/.hermes/hermes-agent/cron/jobs.pyÚ_ensure_croniterr$   /   sT   € ô Ðð	!Ý6Ø ˆHØˆLô ”ÓÐŒ4”ÓÐøô ò 	!Ø ‰LÜ”ÓÐð	!ús   ˆ
0 °AÁAÚcronú	jobs.jsonÚticker_heartbeatÚticker_last_successé<   Ú_fire_fence_locksg      >@Úoutputéx   T)Úfrozenc                   óF   — e Zd ZU eed<   eed<   eed<   ededd fd„«       Zy)Ú_CronStorePathsÚcron_dirÚ	jobs_fileÚ
output_dirr   c                 ó"   —  | ||dz  |dz  «      S )Nr&   r+   © )Úclsr0   s     r#   Úfor_dirz_CronStorePaths.for_dirl   s   € á�8˜X¨Ñ3°XÀÑ5HÓIÐIó    N)Ú__name__Ú
__module__Ú__qualname__r	   Ú__annotations__Úclassmethodr6   r4   r7   r#   r/   r/   f   s7   … àƒNØƒOØÓàðJ˜tð JÐ(9ò Jó ñJr7   r/   Úcron_store_override)ÚdefaultÚ_cron_store_overridec                   ó   — e Zd ZdZdefd„Zy)Ú_SelfRemovalDeliveryzKMutable run-local marker shared with the agent's copied ContextVar context.Újob_idc                 ó    — || _         d| _        y ©NF)rB   Úremoved©ÚselfrB   s     r#   Ú__init__z_SelfRemovalDelivery.__init__x   s   € ØˆŒØˆ�r7   N)r8   r9   r:   Ú__doc__ÚstrrH   r4   r7   r#   rA   rA   u   s   „ ÙUð˜sô r7   rA   Úself_removal_deliveryÚ_self_removal_deliveryrB   c              #   ó´   K  — t        | «      }t        j                  |«      }	 |–— t        j                  |«       y# t        j                  |«       w xY w­w)zEPermit this run's final delivery after it removes its own job record.N)rA   rL   ÚsetÚreset)rB   ÚmarkerÚtokens      r#   Úself_removal_delivery_scoperR   �   sI   è ø€ ô " &Ó)€FÜ"×&Ñ& vÓ.€Eð,ØŠä×$Ñ$ UÕ+øÔ×$Ñ$ UÕ+üs   ‚!A¤> ¨A¾AÁAc                 ó    ‡ — t         j                  «       }|�|j                  ‰ k7  s|j                  syt	        ˆ fd„t        «       D «       «      S )z®Whether the active run deleted exactly its own job record and no record has since taken
    its id (a replacement record belongs to another owner, so that stays fail-closed).Fc              3   óF   •K  — | ]  }|j                  d «      ‰k7  –— Œ y­w©ÚidN©Úget©Ú.0ÚitemrB   s     €r#   ú	<genexpr>z0self_removal_delivery_allowed.<locals>.<genexpr>’   s   øè ø€ Ò@¨Dˆt�x‰x˜‹~ Õ'Ñ@ùs   ƒ!)rL   rX   rB   rE   ÚallÚ	load_jobs)rB   rP   s   ` r#   Úself_removal_delivery_allowedr_   Œ   s?   ø€ ô $×'Ñ'Ó)€FØ€~˜Ÿ™¨&Ò0¸¿ºØÜÓ@´I³KÔ@Ó@Ð@r7   c                  óð   — t         j                  «       } | �| S t        t        t        t
        «      }|t        k7  r|S t        «       j                  «       }|t        k(  r|S t        j                  |dz  «      S )a^  Paths pinned to this execution context's profile. Precedence: (1) active use_cron_store()
    override; (2) deliberately re-pointed module constants; (3) the ACTIVE profile home via
    get_hermes_home(), so re-pointing HERMES_HOME after import uses ITS OWN store rather than the
    user's real jobs.json frozen at import; (4) import-time constants.r%   )r?   rX   r/   ÚCRON_DIRÚ	JOBS_FILEÚ
OUTPUT_DIRÚ_IMPORT_STOREr
   ÚresolveÚ
HERMES_DIRr6   )ÚoverrideÚlive_constantsÚhomes      r#   Ú_current_cron_storerj   ™   sm   € ô
 $×'Ñ'Ó)€HØÐØˆÜ$¤X¬y¼*ÓE€NØœÒ&ØÐÜÓ×$Ñ$Ó&€DØŒzÒØÐÜ×"Ñ" 4¨&¡=Ó1Ð1r7   ri   c              #   ó  K  — t         j                  t        j                  t	        | «      j                  «       j                  «       dz  «      «      }	 d–— t         j                  |«       y# t         j                  |«       w xY w­w)z@Route cron storage to ``home`` without mutating process globals.r%   N)r?   rN   r/   r6   r	   Ú
expanduserre   rO   )ri   rQ   s     r#   Úuse_cron_storerm   ª   sg   è ø€ ô !×$Ñ$Ü×Ñ¤ T£
× 5Ñ 5Ó 7× ?Ñ ?Ó AÀFÑ JÓKóM€Eð*Ûä×"Ñ" 5Õ)øÔ×"Ñ" 5Õ)üs   ‚ABÁA. ÁBÁ.BÂBc                  ó*   — t        «       j                  S )z>Return the output directory for the active cron store context.)rj   r2   r4   r7   r#   Úget_cron_output_dirro   µ   s   € äÓ ×+Ñ+Ð+r7   i  g     À‚@c                  óú   — t        d«      j                  «       } 	 | rt        | «      nt        }|dk  rt        t        «      S t        |t        z  t        t        «      «      S # t        t
        f$ r	 t        }Y ŒKw xY w)u²   One-shot running-claim TTL from ``HERMES_CRON_TIMEOUT``: unset/invalid â†’ 600s â†’ 1800s;
    ``0`` (unlimited) â†’ the fixed floor; positive N â†’ ``max(N * headroom, floor)``.ÚHERMES_CRON_TIMEOUTr   )	r   ÚstripÚfloatÚ _DEFAULT_CRON_INACTIVITY_TIMEOUTÚ
ValueErrorÚ	TypeErrorÚONESHOT_RUN_CLAIM_TTL_SECONDSÚmaxr   )ÚrawÚtimeouts     r#   Ú_oneshot_run_claim_ttl_secondsr{   Å   st   € ô Ð0Ó
1×
7Ñ
7Ó
9€Cð3Ù #”%˜”*Ô)Iˆð �!‚|ÜÔ2Ó3Ð3ÜˆwÔ6Ñ6¼Ô>[Ó8\Ó]Ð]øô	 œ	Ð"ò 3Ü2Šð3ús   ›A" Á"A:Á9A:c                 ón   — 	 ddl m}  || «      S # t        $ r t        j	                  d| d¬«       Y yw xY w)u4  True when the scheduler in THIS process is still running ``job_id``: the run_claim TTL alone
    cannot distinguish "claiming tick died" from "alive but slow". Lazy import: scheduler imports
    us.

    Direct liveness signal for stale-entry recovery (#62002): the run_claim TTL alone cannot distinguish
    "the claiming tick died" from "the run is alive but slow" â€” a run stalled on network I/O (or a laptop
    that slept mid-run) legitimately outlives the TTL. The in-process ticker and the run share this process,
    so the scheduler's running set settles the common single-gateway case without any claim-age guesswork.

    Home-scoped: one process ticks every profile, so the host-wide union of bare job ids would let
    profile A's running ``daily-brief`` keep profile B's stale one-shot alive forever.
    r   )Úis_job_runningzsCron running-set liveness check failed for job %r; keeping the entry to avoid deleting a possibly live one-shot runT©Úexc_info)Úcron.schedulerr}   Ú	ExceptionÚloggerÚwarning)rB   r}   s     r#   Ú_job_running_in_this_processr„   Ò   sE   € ðÝ1Ù˜fÓ%Ð%øÜò Ü�‰ðCà˜Tð 	ô 	#ñ ðús   ‚ �!4³4c                  ó0   — t        «       j                  dz  S )z=Return the advisory lock path for the current cron directory.z
.jobs.lock)rj   r0   r4   r7   r#   Ú_jobs_lock_filer†   ê   s   € äÓ ×)Ñ)¨LÑ8Ð8r7   rz   c                 ó¬  — t         �Ot        j                  «       |z   }	 	 t        j                  | t         j                  t         j
                  z  «       yt        �5 t        t        d«      | j                  «       t        t        d«      d«       yy# t        t        f$ r1 t        j                  «       |k\  rY yt        j                  d«       Y nw xY wŒ·)a'  Bounded exclusive lock: True when acquired, False on timeout, None when no backend exists. A
    blocking flock(LOCK_EX) taken under the in-process lock would let a wedged sibling freeze
    EVERY cron function forever, so poll LOCK_NB against a deadline; the caller picks the
    degraded mode.NTFgš™™™™™¹?ÚlockingÚLK_LOCKé   )ÚfcntlÚtimeÚ	monotonicÚflockÚLOCK_EXÚLOCK_NBÚOSErrorÚIOErrorÚsleepÚmsvcrtÚgetattrÚfileno)Úlock_fdrz   Údeadlines      r#   Ú_acquire_flockr™   ï   s«   € ô
 ÐÜ—>‘>Ó# gÑ-ˆØð Ü—‘˜G¤U§]¡]´U·]±]Ñ%BÔCØô
 ÐØ"Œ”˜	Ó" 7§>¡>Ó#3´W¼VÀYÓ5OÐQRÔSØØøô œWÐ%ò  Ü—>‘>Ó# xÒ/Ù Ü—
‘
˜3–ð úð	 s    5B Â&CÂ:CÃCc                 óD  — 	 t         �%t        j                  | t         j                  «       n:t        �4 t	        t        d«      | j                  «       t	        t        d«      d«       | j                  «        y# t        t        f$ r Y Œ"w xY w# | j                  «        w xY w)zIUnlock (best effort) and close a lock file opened for ``_acquire_flock``.Nrˆ   ÚLK_UNLCKrŠ   )	r‹   rŽ   ÚLOCK_UNr”   r•   r–   r‘   r’   Úclose)r—   s    r#   Ú_release_flockrž     sy   € ðÜÐÜ�K‰K˜¤§¡Õ/ÜÐØ&ŒG”F˜IÓ& w§~¡~Ó'7¼ÄÈÓ9TÐVWÔXð 	�‰�øô ”WÐò Ùðûð 	�‰�ús$   ‚A%A8 Á8B
ÂB Â	B
Â
B ÂBc               #   ó’  K  — t        t        dd«      } | r-| dz   t        _        	 d–— t        xj                  dz  c_        yt        5  dt        _        dt        _        d}	 	 t        «        t        t        «       dd¬«      }|j                  d«       t        |t        «      du rWt        j                  d	t        t        «       «       t        j                  t        «      5  |j!                  «        ddd«       d}	 d–— |�t'        |«       	 dt        _        dt        _        	 ddd«       y# t        xj                  dz  c_        w xY w# 1 sw Y   Œ\xY w# t        t"        f$ r }t        j%                  d
|«       Y d}~Œ‡d}~ww xY w# |�t'        |«       w w xY w# dt        _        dt        _        w xY w# 1 sw Y   yxY w­w)uÜ  Serialize a load_jobsâ†’modifyâ†’save_jobs critical section: in-process RLock (parallel tick
    threads) plus a cross-process flock on ``<cron dir>/.jobs.lock`` (gateway vs. CLI writes â€”
    otherwise a `cron pause` could be clobbered and keep firing). Nested calls in one thread
    reuse the held lock. Without a flock backend, or on flock timeout (logged loudly), it
    degrades to in-process-only locking: a briefly torn cross-process write beats a dead
    scheduler.Údepthr   rŠ   Núa+úutf-8©ÚencodingFu«   Timed out after %.0fs waiting for the cron jobs lock (%s) â€” another process is holding it. Proceeding with in-process locking only so the scheduler stays alive (#60703).zSjobs.json cross-process lock unavailable (%s); proceeding with in-process lock only)r•   Ú_jobs_lock_stater    Ú_jobs_file_lockÚ
load_stampÚensure_dirsÚopenr†   Úseekr™   Ú_JOBS_LOCK_TIMEOUT_SECONDSr‚   ÚerrorÚ
contextlibÚsuppressr‘   r�   r’   rƒ   rž   )r    r—   Úes      r#   Ú
_jobs_lockr°     s¢  è ø€ ô Ô$ g¨qÓ1€EÙØ!&¨¡ÔÔð	(Ûä×"Ò" aÑ'Õ"Øä	ñ "/Ø!"ÔÔð
 '+ÔÔ#Øˆð	/ðJÜ”ÜœÓ0°$ÀÔI�Ø—‘˜Q”Ü! 'Ô+EÓFÈ%ÑOÜ—L‘LðAô 3´OÓ4EôGô $×,Ñ,¬WÓ5ñ (ØŸ™œ÷(à"�Gð
,ÛàÐ&Ü" 7Õ+à%&ÔÔ"Ø*.ÔÕ'÷E"/ð "/øô ×"Ò" aÑ'Ö"ú÷.(ð (ûô œWÐ%ò Jä—‘ð  FØGH÷Jñ JûðJûð Ð&Ü" 7Õ+ð 'ûð &'ÔÔ"Ø*.ÔÕ'ú÷E"/ð "/üsš   ‚"G¥D1 © GÁ	F;Á$A?EÃ#EÃ4
EÃ?FÄF ÄF;Ä(	GÄ1EÅGÅE	ÅEÅF
Å*FÆ F ÆF
Æ
F ÆFÆF Æ F8Æ8F;Æ;GÇ Gc              #   óä  K  — t        «       j                  }|j                  «       › d| › �}t        5  t        j                  |t        j                  «       «      }ddd«       j                  t        ¬«      st        j                  d|«       d–— yt        j                  j                  di «      }||v r	 ||   –— |j                  «        y	 t        «        |dt!        j"                  t         j$                  |«      j&                  › d�z  }d}d}	 t)        |d	d
¬«      }|j+                  d«       t-        |t        «      }|€t        j                  d«       n|st        j                  d|«       t/        |«      }|||<   	 |–— |j5                  |d«       |�|rt7        |«       n|j9                  «        	 |j                  «        y# 1 sw Y   �ŒuxY w# |j                  «        w xY w# t0        t2        f$ r!}	t        j                  d| |	«       Y d}	~	Œ›d}	~	ww xY w# |j5                  |d«       |�|rt7        |«       w |j9                  «        w w xY w# |j                  «        w xY w­w)a  Serialize one job's owner mutations and external side effects. Unlike the global jobs lock
    this may be held across network delivery; scoped to one profile + job so unrelated jobs keep
    progressing. Fails closed when cross-process locking is unavailable.ú::N)rz   z9Timed out waiting for local fire fence %s; failing closedFÚheldz.fire-z.lockr¡   r¢   r£   r   z1No cross-process lock backend for cron fire fencez3Timed out waiting for fire fence %s; failing closedz&Cron fire fence unavailable for %s: %s)rj   r0   re   Ú_fire_fence_locks_guardr*   Ú
setdefaultÚ	threadingÚRLockÚacquirer«   r‚   r¬   Ú_fire_fence_lock_stateÚ__dict__Úreleaser¨   ÚuuidÚuuid5ÚNAMESPACE_URLÚhexr©   rª   r™   r!   r‘   r’   Úpoprž   r�   )
rB   r0   Úlock_keyÚ
local_lockÚ
held_locksÚ	lock_pathr—   ÚacquiredÚresultÚexcs
             r#   Ú_fire_job_lockrÈ   G  s.  è ø€ ô
 #Ó$×-Ñ-€HØ×"Ñ"Ó$Ð% R¨ xÐ0€HÜ	 ñ OÜ&×1Ñ1°(¼I¿O¹OÓ<MÓNˆ
÷Oð ×ÑÔ&@ÐÔAÜ�‰ÐPÐRZÔ[ØŠØä'×0Ñ0×;Ñ;¸FÀBÓG€JØ�:Ñð	!Ø˜XÑ&Ò&à×ÑÔ ØðÜŒØ ¬¯
©
´4×3EÑ3EÀxÓ(P×(TÑ(TÐ'UÐUZÐ[Ñ[ˆ	ØˆØˆð
	PÜ˜9 d°WÔ=ˆGØ�L‰L˜ŒOÜ# GÔ-GÓHˆFØˆ~Ü—‘ÐPÕQÙÜ—‘ÐRÐT]Ô^Ü˜F“|ˆHð  (ˆ
�8Ñð	$ØŠNà�N‰N˜8 TÔ*ØÐ"ÙÜ" 7Õ+à—M‘M•Oà×ÑÕ÷YOñ Oûð ×ÑÕ ûô" œÐ!ò 	PÜ�L‰LÐAÀ6È3×OÑOûð	Pûð �N‰N˜8 TÔ*ØÐ"ÙÜ" 7Õ+à—M‘M•Oð	 #ûð 	×ÑÕüs‘   ‚0I0²)GÁAI0Â9G Ã I0ÃAI ÄA*G0 Æ I ÆH# Æ
3I Æ=I0ÇGÇI0ÇG-Ç-I0Ç0H Ç?HÈI ÈH È I È#5IÉI ÉI-É-I0Úfnc                 ój   — t        | «      5 }|s
	 ddd«       y |«       cddd«       S # 1 sw Y   yxY w)zYRun ``fn()`` holding the job's fire fence; False (fail closed) when it can't be acquired.NF)rÈ   )rB   rÉ   rÅ   s      r#   Ú_under_fire_fencerË   }  s8   € ä	˜Ó	ð  8ÙØ÷ð ñ ‹t÷÷ ò ús   Œ)™)©2Úexpected_ownerc             #   óš  ‡ K  — t        ‰ «      5 }|sd–— 	 ddd«       yt        «       5  t        ˆ fd„t        «       D «       d«      }t	        |t
        «      r|j                  d«      nd}t	        |t
        «      xr |j                  d«      |k(  }|€t        ‰ «      }ddd«       –— ddd«       y# 1 sw Y   ŒxY w# 1 sw Y   yxY w­w)z¥Hold a per-job fence while an owner performs an external side effect. A missing record
    is accepted only for the active run that removed this exact job (#111039).FNc              3   óL   •K  — | ]  }|j                  d «      ‰k(  sŒ|–— Œ y­wrU   rW   rY   s     €r#   r\   z#fire_claim_fence.<locals>.<genexpr>Ž  s    øè ø€ ÒQ ¸¿¹À»È&Ó8PœÑQùs   ƒ$�$Ú
fire_claimÚby)rÈ   r°   Únextr^   Ú
isinstanceÚdictrX   r_   )rB   rÌ   rÅ   ÚjobÚclaimÚ
owns_claims   `     r#   Úfire_claim_fencer×   …  sÄ   øè ø€ ô 
˜Ó	ð 
 8ÙØŠKØ÷
ð 
ô ‹\ñ 	CÜÓQ¬«ÔQÐSWÓXˆCÜ-7¸¼TÔ-B�C—G‘G˜LÔ)ÈˆEÜ# E¬4Ó0ÒV°U·Y±Y¸t³_ÈÑ5VˆJØˆ{Ü:¸6ÓB�
÷	Cð Ò÷
ð 
÷	Cð 	Cú÷	
ð 
üs>   ƒC�B?—	C 
B?ªA4B3ÂB?Â*	CÂ3B<	Â8B?Â?CÃCrV   c                 óþ   — t        | xs d«      j                  «       }|r:|dv s6d|v s2d|v s.t        |«      j                  «       st        |«      j                  rt        d| ›�«      ‚t        «       j                  |z  S )zœResolve a job's output directory, rejecting any path-escape attempt (``..``, absolute
    paths, separators): only a single safe path component is accepted.Ú >   ú..ú.ú/ú\z%Invalid cron job id for output path: )rJ   rr   r	   Úis_absoluteÚdriveru   rj   r2   )rB   Útexts     r#   Ú_job_output_dirrá   ›  st   € ô ˆvŠ|˜Ó×"Ñ"Ó$€Dá�D˜KÑ'¨3°$©;¸$À$¹,Ü�‹:×!Ñ!Ô#¤t¨D£z×'7Ò'7äÐ@ÀÀ
ÐKÓLÐLÜÓ ×+Ñ+¨dÑ2Ð2r7   ÚskillÚskillsc                 óÖ   — |€| r| gng }nt        |t        «      r|g}nt        |«      }g }|D ]8  }t        |xs d«      j                  «       }|sŒ#||vsŒ(|j	                  |«       Œ: |S )zPNormalize legacy/single-skill and multi-skill inputs into a unique ordered list.rÙ   )rÒ   rJ   Úlistrr   Úappend)râ   rã   Ú	raw_itemsÚ
normalizedr[   rà   s         r#   Ú_normalize_skill_listré   §  sv   € à€~Ù$�U‘G¨"‰	Ü	�FœCÔ	 Ø�H‰	ä˜“Lˆ	Ø€JØò $ˆÜ�4’:˜2‹×$Ñ$Ó&ˆÚ�D 
Ò*Ø×Ñ˜dÕ#ð$ð Ðr7   rÔ   c                 ó’   — t        | «      }t        |j                  d«      |j                  d«      «      }||d<   |r|d   nd|d<   |S )zLReturn a job dict with canonical `skills` and legacy `skill` fields aligned.râ   rã   r   N)rÓ   ré   rX   )rÔ   rè   rã   s      r#   Ú_apply_skill_fieldsrë   ·  sK   € ä�c“€JÜ" :§>¡>°'Ó#:¸J¿N¹NÈ8Ó<TÓU€FØ!€JˆxÑÙ'-˜& š)°4€JˆwÑØÐr7   ÚvalueÚfallbackc                 ó    — | €|S t        | «      S )zFCoerce legacy/hand-edited nullable cron fields to strings for readers.)rJ   )rì   rí   s     r#   Ú_coerce_job_textrï   À  s   € à�}ˆ8Ð4¬#¨e«*Ð4r7   >   râ   ÚpromptÚscriptrã   Úno_agentz†Cron job has nothing to run: the prompt is blank and no script or skill(s) are set. Provide a prompt, a script, or at least one skill.ud   no_agent=True requires a script â€” with no agent and no script there is nothing for the job to run.c                 ó$  ‡ — t        ‰ j                  d«      «      j                  «       s(t        ‰ j                  d«      «      j                  «       ryt        ‰ j                  d«      ‰ j                  d«      «      ryt	        ˆ fd„dD «       «      S )z²True when a job record has nothing runnable (blank prompt, no script, no skills) AND at
    least one payload field is explicitly present. ``no_agent`` already requires a script.rð   rñ   Frâ   rã   c              3   ó&   •K  — | ]  }|‰v –— Œ
 y ­w©Nr4   )rZ   ÚkrÔ   s     €r#   r\   z'job_payload_is_empty.<locals>.<genexpr>Ú  s   øè ø€ ÒI˜Aˆq�CŒxÑIùó   ƒ)rð   rñ   râ   rã   )rï   rX   rr   ré   Úany©rÔ   s   `r#   Újob_payload_is_emptyrú   Ó  sm   ø€ ô ˜Ÿ™ Ó)Ó*×0Ñ0Ô2Ô6FÀsÇwÁwÈxÓGXÓ6Y×6_Ñ6_Ô6aØÜ˜SŸW™W WÓ-¨s¯w©w°xÓ/@ÔAØÜÓIÐ!HÔIÓIÐIr7   c                 ó$  — t        | j                  d«      «      j                  «       }|r|S | j                  d«      }t        |t        «      r6dD ]0  }t        |j                  |«      «      j                  «       }|sŒ.|c S  y|�t        |«      S y)NÚschedule_displayÚschedule)Údisplayrì   ÚexprÚrun_atú?)rï   rX   rr   rÒ   rÓ   rJ   )rÔ   rþ   rý   Úkeyrà   s        r#   Ú_schedule_display_for_jobr  Ý  sŒ   € Ü˜sŸw™wÐ'9Ó:Ó;×AÑAÓC€GÙØˆØ�w‰w�zÓ"€HÜ�(œDÔ!Ø9ò 	ˆCÜ# H§L¡L°Ó$5Ó6×<Ñ<Ó>ˆDÚØ’ð	ð ð 
Ð	Ü�8‹}ÐØr7   c                 ó   — t        | «      }t        |j                  d«      d«      x}|d<   t        |j                  d«      «      x}|d<   t        |j                  d«      «      j                  «       }|sh|xsM |j                  d«      r|d   d   ndxs0 t        |j                  d«      «      j                  «       xs |xs d	}|d
d j                  «       xs d	}||d<   t	        |«      |d<   t        |«      |d<   |S )z¬Read-safe job shape: legacy/hand-edited records may have nullable ``prompt``, ``name``,
    ``schedule_display``. Storage is untouched; consumers never crash on formatting.rV   Úunknownrð   Únamerã   r   rÙ   rñ   úcron jobNé2   rü   Ústate)rë   rï   rX   rr   r  Úeffective_job_state)rÔ   rè   rB   rð   r  Úlabel_sources         r#   Ú_normalize_job_recordr  ì  s  € ô % SÓ)€JÜ 0°·±ÀÓ1EÀyÓ QÐQ€FˆZ˜ÑÜ$4°Z·^±^ÀHÓ5MÓ$NÐN€FˆZ˜Ñ!Ü˜JŸN™N¨6Ó2Ó3×9Ñ9Ó;€DÙàò Ø+5¯>©>¸(Ô+C�
˜8Ñ$ QÒ'Èòä 
§¡¨xÓ 8Ó9×?Ñ?ÓAòð òð ð 	ð ˜C˜RÐ ×&Ñ&Ó(Ò6¨JˆØ€JˆvÑÜ%>¸zÓ%J€JÐ!Ñ"ô .¨jÓ9€JˆwÑØÐr7   c                 ó�   — t        | j                  d«      «      j                  «       dk(  xs t        | j                  d«      «      S )z>True when the record carries any operator-facing pause signal.r	  ÚpausedÚ	paused_at)rï   rX   rr   r!   rù   s    r#   Ú_has_pause_markerr    s8   € ä˜CŸG™G GÓ,Ó-×3Ñ3Ó5¸ÑAÒ_ÄTÈ#Ï'É'ÐR]ÓJ^ÓE_Ð_r7   c                 óT   — t        | j                  dd«      «      xr t        | «       S )z«True iff the scheduler may fire this job: ``enabled`` plus pause markers as a second gate so
    a contradictory half-paused record never fires even before self-heal runs.ÚenabledT)r!   rX   r  rù   s    r#   Úis_job_runnabler  	  s(   € ô �—‘˜	 4Ó(Ó)ÒHÔ2CÀCÓ2HÐ.HÐHr7   c                 óê   — t        | j                  d«      «      j                  «       }|dv r|S | j                  dd«      st        | «      s|dk(  ry|xs dS |dk(  s| j                  d«      ry|xs dS )z¶Operator-facing state derived from ``enabled``: an enabled job must never display as paused
    (list looked frozen while jobs kept firing). Terminal states are preserved regardless.r	  >   r¬   Ú	completedr  Tr  r  Ú	scheduled)rï   rX   rr   r  )rÔ   Ústoreds     r#   r
  r
    sz   € ô ˜cŸg™g gÓ.Ó/×5Ñ5Ó7€FØÐ'Ñ'ØˆØ�7‰7�9˜dÔ#Ü˜SÔ! V¨xÒ%7ØØÒ!˜Ð!à�Ò˜SŸW™W [Ô1ØØÒ �[Ð r7   c                 ó(   — | j                  d«      dv S )z=Return whether a job record is in a terminal scheduler state.r	  >   r¬   r  rW   rù   s    r#   Úis_terminal_jobr    s   € à�7‰7�7ÓÐ5Ð5Ð5r7   c                 óz   — | j                  d«      dk(  xr& | j                  d«      xs i j                  d«      dv S )u’  True for a recurring job stuck in ``state=error`` (set ONLY when ``compute_next_run()`` fails
    for a cron/interval job: croniter missing, malformed schedule). Such a job still has future
    occurrences once the issue resolves, so treating it as terminal would block due-scan self-heal,
    pre-advance, dispatch claim and ``resume_job`` â€” wedging it forever.

    Unlike ``state=completed`` (a one-shot that genuinely has no more occurrences, ever), an error-state
    recurring job still has a schedule with future occurrences once the underlying issue resolves â€” it is
    stuck pending a ``next_run_at`` recompute, not truly done. See #16265.
    r	  r¬   rý   Úkind>   r%   ÚintervalrW   rù   s    r#   Ú_is_recoverable_error_jobr  $  sB   € ð 	�‰�Ó˜GÑ#ò 	LØ�W‰W�ZÓ Ò& B×+Ñ+¨FÓ3Ð7KÐKðr7   Úpathc                 ó    — ddl m}  || «       y)z£Owner-only (0700) via the shared helper, so cron/ and cron/output honor the same managed/
    container/HERMES_HOME_MODE rules as the rest of HERMES_HOME (#10757).r   )Ú_secure_dirN)Úhermes_cli.configr   )r  Ú_shared_secure_dirs     r#   r   r   4  s   € õ DÙ�tÕr7   c                 ó    — ddl m}  || «       y)zJOwner-only (0600) via the shared helper (managed/container skip included).r   )Ú_secure_fileN)r!  r$  )r  Ú_shared_secure_files     r#   r$  r$  ;  s   € åEÙ˜Õr7   Úbeforec                 óº  — |�t         j                  dk7  ryt        t         dd«      }t        t         dd«      }|�|€y	  |«       }|dk7  s"|j                  |j                  f| |«       fk(  ryt        j
                  | |j                  |j                  «       y# t        $ r7}t        j                  d| |j                  |j                  |«       Y d}~yd}~ww xY w)a  Restore a rewritten file's previous owner (POSIX, root writer only): atomic replace makes the
    file owned by the writer's euid, so a root CLI write (e.g. ``docker exec``) against the
    unprivileged gateway's store would flip jobs.json to root:root 0600 and lock the ticker out.NÚposixÚgeteuidÚgetegidr   u«   Could not restore ownership of %s to uid=%s gid=%s after rewrite: %s â€” if the gateway runs as a different user, its cron ticker may now be locked out (see issue #68483).)	Úosr  r•   Úst_uidÚst_gidÚchownr‘   r‚   rƒ   )r  r&  r)  r*  Úeuidr¯   s         r#   Ú_preserve_file_ownershipr0  A  sÁ   € ð €~œŸ™ GÒ+ØÜ”b˜) TÓ*€GÜ”b˜) TÓ*€GØ€˜'˜/Øð
3Ù‹yˆØ�1Š9˜Ÿ™¨¯©Ð6¸4ÁÃÐ:KÒKØÜ
�‰��v—}‘} f§m¡mÕ4øÜò 3Ü�‰ð0ð �&—-‘- §¡°÷		3ñ 	3ûð3ús   ¿.B Á.+B Â	CÂ#-CÃCc                 óÒ   — t        j                  t        t        «      5  d| j	                  «       j
                  v r
	 ddd«       y	 ddd«       d| j
                  v S # 1 sw Y   ŒxY w)z·True if *path* is under ``<hermes_home>/profiles/<name>/`` (default/custom homes are not).
    Checks the resolved path (symlinked parents) and the raw path (symlinked profile homes).ÚprofilesNT)r­   r®   r‘   ÚRuntimeErrorre   Úparts©r  s    r#   Ú_is_named_profile_pathr6  X  s^   € ô 
×	Ñ	œW¤lÓ	3ñ Ø˜Ÿ™›×-Ñ-Ñ-Ø÷ð Ø-÷ð ˜Ÿ™Ð#Ð#÷ð ús   ŸAÁA&r0   c                 óf   — t        | «      r| j                  d¬«       y| j                  dd¬«       y)a  Create a cron directory without resurrecting a deleted profile home: a stale multiplex
    scheduler may still hold a deleted profile's path, so named profiles use ``parents=False`` and
    fail closed. Default/custom homes keep ``parents=True`` so first-run creation works.T)Úexist_okN)Úparentsr8  )r6  Úmkdir)r0   s    r#   Ú_ensure_cron_dirr;  a  s-   € ô ˜hÔ'Ø�‰ ˆÔ%ØØ‡N�N˜4¨$€NÕ/r7   c                  óÀ   — t        «       } t        | j                  «       t        | j                  «       t	        | j                  «       t	        | j                  «       y)z6Ensure cron directories exist with secure permissions.N)rj   r;  r0   r2   r   )Ústores    r#   r¨   r¨   k  s>   € äÓ!€EÜ�U—^‘^Ô$Ü�U×%Ñ%Ô&Ü�—‘ÔÜ�× Ñ Õ!r7   Úrepeatc                 óì   — | €yt        | t        «      r4| j                  «       j                  «       }|dv ry|dv ry	 t	        |«      } | dk  rdS t	        | «      S # t
        $ r t        d| ›d�«      ‚w xY w)aU  Coerce a repeat value (int or user-facing string) into ``Optional[int]``:
    ``'forever'``-family -> None, ``'once'``-family -> 1, numeric -> int, 0/negative -> None,
    else ValueError.

    The tool schema exposes ``repeat`` as an integer, but agents and users legitimately pass the user-facing
    strings ``'forever'``/``'once'`` or numeric strings (``'3'``). Uncoerced strings previously died with
    ``'<=' not supported between instances of 'str' and 'int'`` at create
    (#66824/#64520/#7142/#71987/#95706) and were stored raw by update paths, breaking ``mark_job_run``
    later.
    N)ÚforeverÚinfiniteÚinfÚnonerÙ   )ÚonceÚoneÚ1xrŠ   zInvalid repeat value z': use an integer, 'forever', or 'once'.r   )rÒ   rJ   rr   ÚlowerÚintru   )r>  Ú
repeat_strs     r#   Únormalize_repeat_valuerJ  v  s–   € ð €~ØÜ�&œ#ÔØ—\‘\“^×)Ñ)Ó+ˆ
ØÐCÑCØØÐ.Ñ.Øð	Ü˜“_ˆFð ˜Q’;ˆ4Ð/¤C¨£KÐ/øô ò 	ÜØ'¨ zð 2(ð )óð ð	ús   ½A ÁA3rŠ   i   )ÚmÚhÚdÚsc                 ó"  — | j                  «       j                  «       } t        j                  d| «      }|st	        d| › d�«      ‚|j                  d«      rt        |j                  d«      «      nd}|t        |j                  d«      d      z  S )u]   Parse a duration into minutes: "30m" â†’ 30, "2h" â†’ 120, "1d" â†’ 1440, bare "hour" â†’ 60.zD^(\d*)\s*(m|min|mins|minute|minutes|h|hr|hrs|hour|hours|d|day|days)$zInvalid duration: 'zQ'. Use format like '30m', '2h', '1d', or a bare unit like 'hour' (defaults to 1).rŠ   é   r   )rr   rG  ÚreÚmatchru   ÚgrouprH  Ú_DURATION_MULTIPLIERS)rN  rR  rì   s      r#   Úparse_durationrU  –  s…   € à	�‰‹	�‰Ó€AÜ�H‰HÐ\Ð^_Ó`€EÙÜØ! ! ð %:ð :ó;ð 	;ð $)§;¡;¨q¤>ŒC�—‘˜A“Ô°q€EØÔ(¨¯©°Q«¸Ñ):Ñ;Ñ;Ð;r7   ÚsundayÚ0ÚsunÚmondayÚ1ÚmonÚtuesdayÚ2ÚtueÚtuesÚ	wednesdayÚ3ÚwedÚwedsÚthursdayÚ4ÚthuÚthurÚthursÚfridayÚ5ÚfriÚsaturdayÚ6ÚsatÚ*z1-5z0,6)ÚdayÚdailyÚeverydayÚweekdayÚweekdaysÚweekendÚweekendsrà   c                 óš  — | j                  «       j                  «       j                  dd«      }|sy|dv ry|dk(  ryt        j                  d|«      }|syt        |j                  d	«      «      }t        |j                  d
«      xs d«      }|j                  d«      }|rd	|cxk  rdk  sy y|dz  |dk(  rdndz   }|dkD  s|dkD  ry||fS )z–Parse ``9am``/``9:30am``/``14:00``/``7`` (bare 24h hour)/``noon``/``midnight`` into a
    24-hour ``(hour, minute)`` tuple, or None when unrecognized.Ú rÙ   N)ÚnoonÚmidday)é   r   Úmidnight)r   r   z ^(\d{1,2})(?::(\d{2}))?(am|pm)?$rŠ   rP  r   é   r{  Úpmé   é;   )rr   rG  ÚreplacerQ  rR  rH  rS  )rà   ÚtrR  ÚhourÚminuteÚmeridiems         r#   Ú_parse_clock_timer†  ¶  sØ   € ð 	�
‰
‹×ÑÓ×$Ñ$ S¨"Ó-€AÙØØÐÑØØˆJ‚ØÜ�H‰HÐ8¸!Ó<€EÙØÜˆu�{‰{˜1‹~Ó€DÜ�—‘˜Q“Ò$ 1Ó%€FØ�{‰{˜1‹~€HÙØ�DŒ˜BŠØð ØØ�b‰y (¨dÒ"2™B¸Ñ:ˆØˆb‚y�F˜R’KØØ�&ˆ>Ðr7   Úrestc                 óø  — | j                  «       j                  dd«      j                  «       }|syt        j	                  |d   «      }d}|€kg }t        |«      }t        |«      D ]<  \  }}|dk(  rŒt        j	                  |«      }|€|} n||vsŒ,|j                  |«       Œ> |sydj                  |«      }||d }|r|d   dk(  r|dd }|syt        dj                  |«      «      }	|	€y|	\  }
}|› d|
› d|› �S )	z¹Convert ``<when> [at] <time>`` ("monday 9am", "weekday at 9am", "monday, wednesday at 9am")
    to a 5-field cron expr, or None so ``parse_schedule`` can fall back to the interval path.ú,rx  Nr   rŠ   ÚandÚatz * * )rG  r�  ÚsplitÚ_DAYSPEC_TO_CRON_DOWrX   ÚlenÚ	enumerateÚ_WEEKDAY_TO_CRON_DOWræ   Újoinr†  )r‡  ÚtokensÚdowÚidxÚdaysÚiÚtokÚmappedÚtime_tokensÚparsedrƒ  r„  s               r#   Ú_natural_every_to_cronr›  Ï  s$  € ð �Z‰Z‹\×!Ñ! # sÓ+×1Ñ1Ó3€FÙØä
×
"Ñ
" 6¨!¡9Ó
-€CØ
€CØ
€{ØˆÜ�&‹kˆÜ Ó'ò 	$‰FˆAˆsØ�eŠ|ØÜ)×-Ñ-¨cÓ2ˆFØˆ~Ø�ÙØ˜TÒ!Ø—‘˜FÕ#ð	$ñ ØØ�h‰h�t‹nˆØ˜˜�,€KÙ�{ 1‘~¨Ò-Ø! ! "�oˆÙØÜ˜sŸx™x¨Ó4Ó5€FØ€~ØØ�L€Dˆ&ØˆX�Q�t�f˜E # Ð'Ð'r7   rÿ   rþ   Úmissing_croniterÚinvalid_labelc           	      ó    — t        «       st        |› d�«      ‚	 t        | «       d| |dœS # t        $ r}t        d|› d|› d|› �«      ‚d}~ww xY w)zLValidate a cron expression with croniter and build the stored schedule dict.z# Install with: pip install croniterzInvalid z 'ú': Nr%   )r  rÿ   rþ   )r$   ru   r   r�   )rÿ   rþ   rœ  r�  r¯   s        r#   Ú_cron_scheduler   ó  sm   € ô ÔÜÐ,Ð-Ð-PÐQÓRÐRðFÜ�Œð  D°WÑ=Ð=øô ò FÜ˜8 M ?°"°W°I¸SÀÀÐDÓEÐEûðFús   š+ «	A´AÁAÚminutesc                 ó   — d| d| › d�dœS )Nr  úevery rK  )r  r¡  rþ   r4   ©r¡  s    r#   Ú_interval_scheduler¥     s   € Ø¨7ÀÈÀyÐPQÐ?RÑSÐSr7   rý   c                 ó  — | j                  «       } | }| j                  «       }|j                  d«      }|r| dd j                  «       n|}t        |«      }|�|rdnd}t	        ||d|› d�d«      S |rt        t        |«      «      S | j                  «       }t        |«      d	k\  r#t        d
„ |dd	 D «       «      rt	        | | dd«      S d| v st        j                  d| «      r|	 t        j                  | j                  dd«      «      }|j                  €$|j                  t!        «       j                  ¬«      }d|j#                  «       d|j%                  d«      › �dœS |j                  d«      r…| dd j                  «       }
	 t        |
«      }t!        «       }|j)                  t*        j,                  «      t/        |¬«      z   j)                  |j                  «      }d|j#                  «       d|
› �dœS t1        j2                  t&        «      5  t        t        | «      «      cddd«       S # t&        $ r}	t'        d| › d|	› �«      ‚d}	~	ww xY w# t&        $ r t'        d|
› d�«      ‚w xY w# 1 sw Y   nxY wt'        d|› d�«      ‚) zðParse a schedule string into ``{"kind": "once"|"interval"|"cron", ...}`` with ``run_at`` /
    ``minutes`` / ``expr``. "30m" and "every 30m" are recurring intervals; "every monday 9am" and
    "0 9 * * *" are cron; an ISO timestamp is once.r£  é   Nzevery monday 9amzweekdays at 9amzWeekday/time schedules like 'z!' require the 'croniter' package.rý   é   c              3   óH   K  — | ]  }t        j                  d |«      –— Œ y­w)z^[A-Za-z\d\*\-,/]+$N)rQ  rR  ©rZ   Úps     r#   r\   z!parse_schedule.<locals>.<genexpr>  s   è ø€ ÒVÀqœrŸx™xÐ(>À×BÑVùs   ‚ "z,Cron expressions require 'croniter' package.zcron expressionÚTz^\d{4}-\d{2}-\d{2}ÚZz+00:00©ÚtzinforD  zonce at z%Y-%m-%d %H:%M)r  r   rþ   zInvalid timestamp 'rŸ  zin r}  zInvalid duration 'z*' after 'in '. Use e.g. 'in 30m', 'in 2h'.r¤  zonce in zInvalid schedule 'a  '. Use:
  - Interval: '30m', 'every 30m', 'every 2h' (recurring)
  - One-shot delay: 'in 30m', 'in 2h' (fires once)
  - Weekly/daily: 'every monday 9am', 'weekdays at 9am' (recurring)
  - Cron: '0 9 * * *' (cron expression)
  - Timestamp: '2026-02-03T14:00:00' (one-shot at time))rr   rG  Ú
startswithr›  r   r¥  rU  rŒ  rŽ  r]   rQ  rR  r   Úfromisoformatr�  r¯  Ú_hermes_nowÚ	isoformatÚstrftimeru   Ú
astimezoner   Úutcr   r­   r®   )rý   ÚoriginalÚschedule_lowerÚis_everyr‡  Ú	cron_exprÚexampler4  Údtr¯   Úduration_strr¡  r   r   s                 r#   Úparse_scheduler¾    s¬  € ð �~‰~Ó€HØ€HØ—^‘^Ó%€Nð ×(Ñ(¨Ó2€HÙ#+ˆ8�A�Bˆ<×ÑÔ°€DÜ& tÓ,€IàÐÙ(0Ñ$Ð6GˆÜØ�xØ+¨G¨9Ð4UÐVÐXbódð 	dñ Ü!¤.°Ó"6Ó7Ð7ð �N‰NÓ€EÜ
ˆ5ƒz�Q‚œ3ÑVÈEÐRTÐSTÈIÔVÔVÜØ�hÐ NÐPaócð 	cð ˆh�œ"Ÿ(™(Ð#8¸(ÔCð	EÜ×'Ñ'¨×(8Ñ(8¸¸hÓ(GÓHˆBð �y‰yÐ Ø—Z‘Z¤{£}×';Ñ';�ZÓ<�àØŸ,™,›.Ø% b§k¡kÐ2BÓ&CÐ%DÐEñð ð × Ñ  Ô'Ø  �|×)Ñ)Ó+ˆð	_Ü$ \Ó2ˆGô ‹mˆà—.‘.¤§¡Ó.´À7Ô1KÑK×WÑWÐX[×XbÑXbÓcˆØ¨&×*:Ñ*:Ó*<È8ÐT`ÐSaÐIbÑcÐcÜ	×	Ñ	œZÓ	(ñ <Ü!¤.°Ó":Ó;÷<ñ <øô! ò 	EÜÐ2°8°*¸CÀ¸sÐCÓDÐDûð	Eûô ò 	_ÜØ$ \ NÐ2\Ð]ó_ð _ð	_ú÷<ð <úô Ø
˜X˜Jð 'Bð 	Cóð s1   Ã"A:H* ÆI ÈI(È*	I	È3IÉI	ÉI%É(I1r¼  c                 óú   — t        «       j                  }| j                  €K| j                  t        j                  «       j                  «       j                  ¬«      j                  |«      S | j                  |«      S )zæAware datetime in the configured Hermes timezone. Legacy naive values are read as
    *system-local* wall time (what created them) then converted, preserving ordering across
    timezone changes and avoiding false not-due results.r®  )r²  r¯  r�  r   r   rµ  )r¼  Ú	target_tzs     r#   Ú_ensure_awarerÁ  P  s\   € ô “×$Ñ$€IØ	‡y�yÐØ�z‰z¤§¡£×!:Ñ!:Ó!<×!CÑ!CˆzÓD×OÑOÐPYÓZÐZØ�=‰=˜Ó#Ð#r7   c                 ó^   — 	 t        t        j                  | «      «      S # t        $ r Y yw xY w)zi``_ensure_aware(datetime.fromisoformat(value))``, or None when *value* is not a parseable ISO
    string.N)rÁ  r   r±  r�   ©rì   s    r#   Ú_parse_awarerÄ  Z  s/   € ðÜœX×3Ñ3°EÓ:Ó;Ð;øÜò Ùðús   ‚   	,«,r  Úcurrentc                 óv   — | j                   �|j                   €y| j                  «       |j                  «       k7  S )z¼True when a stored aware timestamp uses a different UTC offset. Naive values return False:
    they are normalized by ``_ensure_aware`` and intentionally never take the offset-repair path.F)r¯  Ú	utcoffset©r  rÅ  s     r#   Ú_timezone_offset_mismatchrÉ  c  s7   € ð ‡}�}Ð §¡Ð 6ØØ×ÑÓ ×!2Ñ!2Ó!4Ñ4Ð4r7   c                 óL   — | j                  d¬«      |j                  d¬«      kD  S )u  True when the stored local wall-clock time has not arrived yet. Cron expresses wall-clock
    intent; after a timezone change an old offset can make a future run look due (21:00+10 â†’
    13:00+02). Comparing naive wall clocks separates that from a genuine miss.Nr®  )r�  rÈ  s     r#   Ú_stored_wall_clock_is_futurerË  k  s%   € ð �>‰> ˆ>Ó&¨¯©À¨Ó)EÑEÐEr7   ©Úlast_run_atr   rÍ  c                óÆ   — t        | t        «      r| j                  d«      dk7  s|ry| j                  d«      }|rt        |«      nd}|�||t	        t
        ¬«      z
  k\  r|S y)z˜One-shot run time if still eligible: a small grace window covers jobs created just after
    their minute; once run, a one-shot is never eligible again.r  rD  Nr   ©Úseconds)rÒ   rÓ   rX   rÄ  r   ÚONESHOT_GRACE_SECONDS)rý   r   rÍ  r   Ú	run_at_dts        r#   Ú_recoverable_oneshot_run_atrÓ  r  s_   € ô
 �h¤Ô%¨¯©°fÓ)=ÀÒ)GÉ;ØØ�\‰\˜(Ó#€FÙ(.”˜VÔ$°D€IØÐ ¨c´IÔF[Ô4\Ñ.\Ò!\ØˆØr7   i   c                 ó|   — t        | «      }|st        S t        t        t        t	        |«      dz  t
        «      «      S )z­How late a job can be and still catch up rather than fast-forward: half the period, clamped
    to [120s, 2h], so daily jobs catch up but frequent jobs fast-forward quickly.rP  )Ú_schedule_cadence_secondsÚ_MIN_GRACE_SECONDSrx   ÚminrH  Ú_MAX_GRACE_SECONDS)rý   Úperiod_secondss     r#   Ú_compute_grace_secondsrÚ  „  s7   € ô /¨xÓ8€NÙÜ!Ð!ÜÔ!¤3¤s¨>Ó':¸aÑ'?ÔASÓ#TÓUÐUr7   i,  Úlateness_secondsÚgrace_secondsc                 ó$   — | |kD  ry| t         kD  ryy)z¢``on_time`` (within ticker slack), ``late`` (within the catch-up grace window), or
    ``catch_up`` (beyond grace; accumulated misses skipped, executed once now).Úcatch_upÚlateÚon_time)Ú _LATE_DISPATCH_TOLERANCE_SECONDS)rÛ  rÜ  s     r#   Ú_classify_dispatch_latenessrâ  “  s   € ð ˜-Ò'ØØÔ:Ò:ØØr7   Ú_persisted_error_recoveriesé   Ú"_persisted_error_recoveries_recentc                 óš  — | j                  d«      dk7  ryddlm}  || |«      ryt        t	        | j                  d«      xs d«      «      ryt        | j                  d«      |t        «      ry| j                  d	«      }|rt        |«      nd
}|€y||z
  j                  «       }|dk  ryt        |«      }t        |«      }|€|}|||z   kD  S )u�  True when a recurring job (caller-checked) is wedged in a stale persisted error state:
    ``last_status == "error"``, not running in this process (never re-arm a live run underneath
    itself), and ``last_run_at`` older than ``cadence + grace`` (a job merely erroring-and-retrying
    on schedule stays fresh and is not flagged).

    Condition (all must hold): * it has NOT successfully re-fired within its natural cadence â€” its
    ``last_run_at`` is older than ``cadence + grace``, so this is not a normal transient-error retry that
    will fire on its own soon, it is a job that has been sitting errored for a full period with no recovery;
    See #62002.
    Úlast_statusr¬   Fr   )Úhold_activerV   rÙ   rÏ   rÍ  N)rX   Úcron.quota_holdrè  r„   rJ   Ú_claim_is_liver   rÄ  Útotal_secondsrÚ  rÕ  )	rÔ   rý   r   rè  Úlast_runÚlast_run_dtÚage_secondsÚgraceÚcadence_secondss	            r#   Ú_job_is_stale_error_recurringrñ  ¥  sÑ   € ð ‡w�wˆ}Ó Ò(ØÝ+Ù�3˜ÔØÜ#¤C¨¯©°«Ò(;¸Ó$<Ô=Øô �c—g‘g˜lÓ+¨SÔ2HÔIØØ�w‰w�}Ó%€HÙ,4”,˜xÔ(¸$€KØÐØØ˜Ñ$×3Ñ3Ó5€KØ�Q‚ØÜ" 8Ó,€EÜ/°Ó9€OØÐàˆØ˜/¨EÑ1Ñ2Ð2r7   Ú_cron_cadence_cachec                 ób  — t        | t        «      sy| j                  d«      }|dk(  r$| j                  d«      }	 |rt        |«      dz  S dS |dk7  s
t        «       sy| j                  d«      }|sy|t        v r	t        |   S 	 t        |t        «       «      }|j                  t        «      }|j                  t        «      |z
  j                  «       }|dkD  r|nd}t        t        «      d	k\  rt        j                  «        |t        |<   |S # t        t
        f$ r Y yw xY w# t        $ r d}Y ŒSw xY w)
a  Approximate schedule period in seconds, or None (croniter missing / malformed expr). Cron
    results are cached per expr because this runs under ``_jobs_lock`` every tick; the gap can vary
    with base time for irregular exprs, acceptable for a staleness *threshold*.Nr  r  r¡  g      N@r%   rÿ   r   é   )rÒ   rÓ   rX   rs   rv   ru   r$   rò  r   r²  Úget_nextr   rë  r�   rŽ  Úclear)rý   r  r¡  rÿ   ÚitÚfirstÚgaprÆ   s           r#   rÕ  rÕ  Ð  s.  € ô �h¤Ô%ØØ�<‰<˜Ó€DØˆzÒØ—,‘,˜yÓ)ˆð	Ù,3”5˜“> DÑ(Ð=¸Ð=ð ˆv‚~Ô-Ô/ØØ�<‰<˜Ó€DÙØØÔ"Ñ"Ü" 4Ñ(Ð(ðÜ�dœK›MÓ*ˆØ—‘œHÓ%ˆØ�{‰{œ8Ó$ uÑ,×;Ñ;Ó=ˆØ˜aš‘ Tˆô ÔÓ 3Ò&Ü×!Ñ!Ô#Ø &Ô˜ÑØ€Møô) œ:Ð&ò 	Ùð	ûô ò ØŠðús*   ºD Á
D ÂAD  ÄDÄDÄ D.Ä-D.ÚfilenameÚentryÚrecentc                 ó‚  — |j                  |«       |dt         …= 	 t        «       j                  | z  }t	        |j
                  «       t        |dd¬«      5 }|j                  t        j                  |«      dz   «       ddd«       y# 1 sw Y   yxY w# t        $ r!}t        j                  d| |«       Y d}~yd}~ww xY w)uá   Record ``entry`` in the bounded ``recent`` list and append to ``<cron_dir>/<filename>``
    (best effort â€” telemetry must never break a tick). Counters stay module-level ints per
    metric because tests reset them by name.NÚar¢   r£   Ú
zCould not append %s record: %s)ræ   Ú_TELEMETRY_RECENT_HISTORYrj   r0   r;  Úparentr©   ÚwriteÚjsonÚdumpsr�   r‚   Údebug)rú  rû  rü  r  ÚfhrÇ   s         r#   Ú_append_telemetry_recordr  ò  s©   € ð ‡M�M�%ÔØÐ*Ô*Ð*Ð*Ð+ðFÜ"Ó$×-Ñ-°Ñ8ˆÜ˜Ÿ™Ô%Ü�$˜ gÔ.ð 	/°"Ø�H‰H”T—Z‘Z Ó&¨Ñ-Ô.÷	/÷ 	/ñ 	/ûäò FÜ�‰Ð5°xÀ×EÑEûðFús5   �:B Á(BÁ?B ÂBÂB ÂB Â	B>ÂB9Â9B>Úprevious_next_runc                 óÔ   — | j                  d«      | j                  d«      xs | j                  d«      |t        «       j                  «       dœ}t        dz  at	        d|t
        «       y)zEPersist a countable, probe-visible signal for one stale-error re-arm.rV   r  )rB   r  Úprevious_next_run_atÚ
rearmed_atrŠ   z persisted_error_recoveries.jsonlN)rX   r²  r³  rã  r  rå  )rÔ   r  rû  s      r#   Ú _record_persisted_error_recoveryr    s]   € ð —'‘'˜$“-Ø—‘˜“Ò0 3§7¡7¨4£=Ø 1Ü!“m×-Ñ-Ó/ñ	€Eô   1Ñ$ÐÜØ*¨EÔ3UõWr7   c                  ó.   — t         t        t        «      dœS )z5Probe-visible snapshot of persisted-error recoveries.)Úpersisted_error_recoveriesrü  )rã  rå   rå  r4   r7   r#   Ú"get_persisted_error_recovery_statsr    s   € ô 'BÜÔ9Ó:ñð r7   Únext_run_dtc                 ó<  — | j                  d«      dk7  ry| j                  d«      }|rt        «       rt        €y	 t        t        |«      |t	        d¬«      z   «      j                  t        «      }t        ||z
  j                  «       «      dk  S # t        $ r Y yw xY w)aÕ  Whether ``next_run_dt`` is an occurrence of the schedule's current expr (detects a
    hand-edited ``schedule.expr`` whose stored ``next_run_at`` came from the old one).
    Best-effort: anything uncheckable (non-cron, no expr, no croniter, malformed) reports a
    match.

    A direct ``jobs.json`` edit can change ``schedule.expr`` while leaving the stored ``next_run_at``
    computed under the *old* expression (#93049). The stored instant is stale exactly when it is not an
    occurrence of the current expression. Validation is best-effort: anything that cannot be checked
    (non-cron kind, missing expr, croniter unavailable, malformed input) reports a match so the fire path
    keeps its existing semantics.
    r  r%   Trÿ   rŠ   rÏ  g      ð?)
rX   r$   r   rJ   r   Úget_prevr   Úabsrë  r�   )rý   r  rÿ   Úprevs       r#   Ú_cron_next_run_matches_exprr    s”   € ð ‡|�|�FÓ˜vÒ%ØØ�<‰<˜Ó€DÙÔ'Ô)¬XÐ-=Øðô œ˜D›	 ;´À1Ô1EÑ#EÓF×OÑOÔPXÓYˆÜ�D˜;Ñ&×5Ñ5Ó7Ó8¸3Ñ>Ð>øÜò Ùðús   »AB Â	BÂBrR  Útimezone_migrationÚ	expr_editÚraw_next_run_dtc                 ó¤   — t        | |«      rt        S |j                  d¬«      |j                  d¬«      k7  }|rt        | |«      rt        S t        S )uM  Explain WHY a stored ``next_run_at`` misses the current cron lattice; the causes need
    opposite actions. ``expr_edit``: a hand edit changed ``schedule.expr`` â€” re-anchor WITHOUT
    firing. ``timezone_migration``: only the offset representation changed (legacy UTC rows
    normalized into the profile tz); treating it as an edit would skip a due, never-fired
    occurrence. Discriminator: normalization moved the wall clock AND the stored instant's OWN
    wall clock is a legal occurrence (when offsets agree a genuine expr edit can never be misread
    as a migration).

    * ``expr_edit`` â€” a direct ``jobs.json`` edit changed ``schedule.expr`` while leaving ``next_run_at``
    computed under the old one (#93049). Upgrading from a UTC-scheduling build to one that honours the
    profile timezone leaves legacy rows like ``2026-09-02T04:00:00+00:00`` for ``0 4 * * *``; normalizing to
    Europe/Brussels turns that into ``06:00+02``, which the expression excludes. Treating it as a stale edit
    re-anchored to tomorrow and silently skipped a due occurrence that had never fired.
    Nr®  )r  ÚSTALE_CRON_MATCHr�  ÚSTALE_CRON_TIMEZONE_MIGRATIONÚSTALE_CRON_EXPR_EDIT)rý   r  r  Úwall_clock_shifteds       r#   Ú_classify_stale_cron_next_runr  8  sU   € ô" # 8¨[Ô9ÜÐØ(×0Ñ0¸Ð0Ó=À×ATÑATÐ\`ÐATÓAaÑaÐÙÔ9¸(ÀOÔTÜ,Ð,ÜÐr7   Ú_timezone_migration_catchupsÚ#_timezone_migration_catchups_recentc                 óT  — | j                  d«      | j                  d«      xs | j                  d«      | j                  d«      xs i j                  d«      |j                  «       |j                  «       t        «       j                  «       dœ}t        dz  at	        d|t
        «       y)	zBPersist a countable signal for one offset-migration catch-up fire.rV   r  rý   rÿ   )rB   r  rÿ   Ústored_next_run_atÚnormalized_next_run_atÚfired_atrŠ   z!timezone_migration_catchups.jsonlN)rX   r³  r²  r  r  r   )rÔ   r  r  rû  s       r#   Ú"_record_timezone_migration_catchupr%  W  sŽ   € ð —'‘'˜$“-Ø—‘˜“Ò0 3§7¡7¨4£=Ø—‘˜Ó$Ò*¨×/Ñ/°Ó7Ø-×7Ñ7Ó9Ø"-×"7Ñ"7Ó"9Ü“M×+Ñ+Ó-ñ€Eô ! AÑ%Ð ÜØ+¨UÔ4WõYr7   c                  ó.   — t         t        t        «      dœS )z:Probe-visible snapshot of offset-migration catch-up fires.)Útimezone_migration_catchupsrü  )r  rå   r   r4   r7   r#   Ú$get_timezone_migration_catchup_statsr(  i  s   € ô (DÜÔ:Ó;ñð r7   c                 óÚ  — t        «       }t        | t        «      sy| j                  d«      }|dk(  rt	        | ||¬«      S |rt        |«      ndxs |}|dk(  ri| j                  d«      }|€y|j                  t        j                  «      t        |¬«      z   }|j                  |j                  «      j                  «       S |dk(  �r%| j                  d	«      }|syt        «       st        j                  d
|«       yt        «       xs |j                  }|j                  |«      j!                  d¬«      }	t#        ||	«      }
|j%                  «       }|
j'                  t(        «      }t+        d«      D ]X  }dD ]<  }|j!                  ||¬«      }|j%                  «       |kD  sŒ*|j                  «       c c S  |
j'                  t(        «      }ŒZ |j!                  |¬«      j                  «       S y)zSCompute the next run time for a schedule as an ISO string, or None if no more runs.Nr  rD  rÌ  r  r¡  r¤  r%   rÿ   zÀCannot compute next run for cron schedule %r: 'croniter' is not installed. croniter is a core dependency as of v0.9.x; reinstall hermes-agent or run 'pip install croniter' in your runtime env.r®  rP  )r   rŠ   )r¯  Úfold)r²  rÒ   rÓ   rX   rÓ  rÄ  rµ  r   r¶  r   r¯  r³  r$   r‚   rƒ   r   r�  r   Ú	timestamprõ  r   Úrange)rý   rÍ  r   r  Ú	base_timer¡  Únext_runrÿ   ÚzoneÚ	base_wallr÷  Úbase_tsÚ	next_wallÚ_r*  Ú	candidates                   r#   Úcompute_next_runr5  q  sË  € ä
‹-€CÜ�h¤Ô%ØØ�<‰<˜Ó€DØˆv‚~Ü*¨8°SÀkÔRÐRá.9”˜kÔ*¸tÒKÈ€IØˆzÒØ—,‘,˜yÓ)ˆØˆ?Øà×'Ñ'¬¯©Ó5¼	È'Ô8RÑRˆØ×"Ñ" 9×#3Ñ#3Ó4×>Ñ>Ó@Ð@Øˆvƒ~Ø�|‰|˜FÓ#ˆÙØÜÔ!Ü�N‰Nð\ð ô	ð
 ô ‹~Ò1 ×!1Ñ!1ˆØ×(Ñ(¨Ó.×6Ñ6¸dÐ6ÓCˆ	Ü�d˜IÓ&ˆð ×%Ñ%Ó'ˆØ—K‘K¤Ó)ˆ	Ü�q“ò 	.ˆAØò 1�Ø%×-Ñ-°TÀÐ-ÓE�	Ø×&Ñ&Ó(¨7Ó2Ø$×.Ñ.Ó0Ô0ð1ð Ÿ™¤HÓ-‰Ið	.ð × Ñ ¨Ð Ó-×7Ñ7Ó9Ð9Ør7   r  Ú
tmp_prefixc                 ó€   — 	 t        «        t        t        «       j                  | z  ||d¬«       y# t        $ r Y yw xY w)zeAtomic (never torn) best-effort marker write; failures swallowed so markers never break the
    tick.é€  ©r6  ÚmodeN)r¨   r   rj   r0   r�   )r  rà   r6  s      r#   Ú_write_markerr;  °  s=   € ðÜŒÜÔ-Ó/×8Ñ8¸4Ñ?ÀÐR\ÐchÖiøÜò Ùðús   ‚.1 ±	=¼=FÚsuccessc                 óª   — t        dt        t        j                  «       «      d«       | r)t        dt        t        j                  «       «      d«       yy)uC  Record ticker liveness (+ last-success marker when ``success``) so `cron status` can tell
    "alive but failing" from "firing"; scoped per profile store.

    The ticker calls this once per loop iteration. ``success=True`` additionally bumps the *last successful
    tick* marker. We track two distinct signals so `hermes cron status` can tell a thread that is merely
    *alive and looping* (heartbeat fresh, success stale) from one that is actually *firing jobs* (both
    fresh) â€” a ticker stuck failing every tick would otherwise keep the plain heartbeat fresh and falsely
    report healthy (#32612, #32895).
    Resolution uses ``_current_cron_store()`` so the heartbeat is correctly scoped to the active profile's
    store â€” critical under multiplex_profiles where each profile needs its own liveness signal (#69377).
    r'   z.hb_r(   N)r;  rJ   rŒ   )r<  s    r#   Úrecord_ticker_heartbeatr>  º  s<   € ô Ð$¤c¬$¯)©)«+Ó&6¸Ô?ÙÜÐ+¬S´·±³Ó-=¸vÕFð r7   c                 óà   — 	 t        «       j                  | z  j                  d¬«      j                  «       }t	        dt        j
                  «       t        |«      z
  «      S # t        $ r Y yw xY w)zYSeconds since the epoch stamp stored in ``<cron_dir>/<name>``; None = missing/unreadable.r¢   r£   ç        N)rj   r0   Ú	read_textrr   rx   rŒ   rs   r�   )r  ry   s     r#   Ú_epoch_file_agerB  Ë  s_   € ðÜ"Ó$×-Ñ-°Ñ4×?Ñ?ÈÐ?ÓQ×WÑWÓYˆÜ�3œŸ	™	›¤e¨C£jÑ0Ó1Ð1øÜò Ùðús   ‚AA! Á!	A-Á,A-c                  ó   — t        d«      S )uQ  Seconds since the ticker loop last iterated; None = missing/unreadable ("cannot determine",
    not "dead").

    Resolution uses ``_current_cron_store()`` so the heartbeat is correctly scoped to the active profile â€”
    critical under multiplex_profiles where ``hermes cron status`` must report per-profile liveness
    (#69377).
    r'   ©rB  r4   r7   r#   Úget_ticker_heartbeat_agerE  Ô  s   € ô Ð-Ó.Ð.r7   c                  ó   — t        d«      S )u-  Seconds since the ticker last completed a tick WITHOUT raising, or None.

    Resolution uses ``_current_cron_store()`` so the heartbeat is correctly scoped to the active profile â€”
    critical under multiplex_profiles where ``hermes cron status`` must report per-profile liveness
    (#69377).
    r(   rD  r4   r7   r#   Úget_ticker_success_agerG  ß  s   € ô Ð0Ó1Ð1r7   c                  óÂ   — t        «       j                  dz  } 	 t        dt        | j	                  d¬«      j                  «       «      «      S # t        t        f$ r Y yw xY w)z7Return the profile-local stale-schedule catch-up count.Úcatch_up_occurrencesr   r¢   r£   )rj   r0   rx   rH  rA  rr   r‘   ru   r5  s    r#   Úget_catch_up_occurrence_countrJ  é  sX   € äÓ ×)Ñ)Ð,BÑB€DðÜ�1”c˜$Ÿ.™.°'˜.Ó:×@Ñ@ÓBÓCÓDÐDøÜ”ZÐ ò Ùðús   ™2A ÁAÁAc                  óF   — t        dt        t        «       dz   «      d«       y)zIIncrement the profile-local stale-schedule catch-up counter, best effort.rI  rŠ   z.count_N)r;  rJ   rJ  r4   r7   r#   Úrecord_catch_up_occurrencerL  ò  s   € äÐ(¬#Ô.KÓ.MÐPQÑ.QÓ*RÐT]Õ^r7   Úmessagec                 ój   — t        dt        j                  «       › d| j                  «       › d�d«       y)zhPersist the latest tick failure so `cron status` (another process) can show WHY, not just
    staleness.Úticker_last_errorrÿ  z.terr_N)r;  rŒ   rr   )rM  s    r#   Úrecord_ticker_errorrP  ÷  s+   € ô Ð%¬$¯)©)«+¨°b¸¿¹»Ð8IÈÐ'LÈhÕWr7   c                  óª   — t        j                  t        «      5  t        «       j                  dz  j                  «        ddd«       y# 1 sw Y   yxY w)zGRemove the last-tick-error marker after a successful tick. Best-effort.rO  N)r­   r®   r‘   rj   r0   Úunlinkr4   r7   r#   Úclear_ticker_errorrS  ý  sD   € ä	×	Ñ	œWÓ	%ñ HÜ	Ó	×	'Ñ	'Ð*=Ñ	=×EÑEÔG÷H÷ Hñ Hús   š&A	Á	Ac                  óú   — 	 t        «       j                  dz  j                  d¬«      } | j	                  «       }t        |«      dk  rydj                  |dd «      j                  «       xs dS # t        $ r Y yw xY w)z<Return the most recent recorded tick error message, or None.rO  r¢   r£   NrP  rÿ  rŠ   )rj   r0   rA  r�   Ú
splitlinesrŽ  r‘  rr   )ry   Úliness     r#   Úget_ticker_last_errorrW    s}   € ðÜ"Ó$×-Ñ-Ð0CÑC×NÑNÐX_ÐNÓ`ˆð �N‰NÓ€EÜ
ˆ5ƒz�A‚~ØØ�9‰9�U˜1˜2�YÓ×%Ñ%Ó'Ò/¨4Ð/øô ò Ùðús   ‚'A. Á.	A:Á9A:r1   c                 óü   — t        | dd¬«      5 }|j                  «       }ddd«       	 t        j                  «      dfS # 1 sw Y   Œ!xY w# t        j                  $ r t        j                  d¬«      dfcY S w xY w)zÊTolerant jobs.json parse -> ``(data, used_strict_fallback)``: utf-8-sig absorbs a BOM, strict
    failure retries with ``strict=False``. IO/fallback errors propagate (caller decides repair vs
    bail).Úrz	utf-8-sigr£   NF)ÚstrictT)r©   Úreadr  ÚloadsÚJSONDecodeError)r1   Úfry   s      r#   Ú_parse_jobs_filer_    sv   € ô 
ˆi˜ {Ô	3ð °qØ�f‰f‹hˆ÷ð3Ü�z‰z˜#‹ Ð%Ð%÷ð ûô ×Ñò 3Ü�z‰z˜# eÔ,¨dÐ2Ò2ð3ús   �A ©A Á A	Á,A;Á:A;c                  óx  — t        «       j                  } t        «        t        | «      }| j	                  «       st        d«       g S 	 t        | «      \  }}|rdnd}t        |t        «      rõ|j                  dg «      }t        |t        «      �r|j                  «       D ��cg c]  \  }}t        |t        «      rŒ|‘Œ }	}}|	rNt        j!                  dt#        |	«      t#        |	«      d	k(  rd
nddj%                  t'        t(        |	«      «      «       |j                  «       D ��cg c]1  \  }}t        |t        «      sŒi |¥d|j                  d«      xs |i¥‘Œ3 }}}d}n6t        |t*        «      r|}d}n!t        dt-        |«      j.                  › �«      ‚|r#|r!t1        |«       t        j!                  d|«       t        |«       |S # t        $ r*}t        j                  d|«       t        d|› �«      |‚d}~wt        $ r*}t        j                  d|«       t        d|› �«      |‚d}~ww xY wc c}}w c c}}w )zLoad all jobs from storage.NzIOError reading jobs.json: %szFailed to read cron database: z#Failed to auto-repair jobs.json: %sz*Cron database corrupted and unrepairable: zhad invalid control charactersÚjobsz4Skipping %d non-dict entr%s in id-keyed jobs map: %srŠ   ÚyÚiesú, rV   z#id-keyed jobs map flattened to listzbare list wrapped as dictz7Cron database corrupted: expected {'jobs': [...]}, got zAuto-repaired jobs.json (%s))rj   r1   r¨   Ú_jobs_file_stampÚexistsÚ_record_load_stampr_  r’   r‚   r¬   r3  r�   rÒ   rÓ   rX   Úitemsrƒ   rŽ  r‘  ÚmapÚreprrå   Útyper8   Ú	save_jobs)
r1   Úpre_read_stampÚdataÚ_strict_retryr¯   Úrepairra  rö   ÚvÚskippeds
             r#   r^   r^     sÿ  € ä#Ó%×/Ñ/€IÜ„Mô & iÓ0€NØ×ÑÔÜ˜4Ô Øˆ	ðTÜ.¨yÓ9Ñˆˆmñ 2?Ñ-ÀD€FÜ�$œÔØ�x‰x˜ Ó#ˆÜ�dœDÕ!ð &*§Z¡Z£\×M™T˜Q ¼ÀAÄtÕ9L’qÐMˆGÑMÙÜ—‘ØJÜ˜“L¬¨W«¸Ò):¡#ÀØ—I‘Iœc¤$¨Ó0Ó1ô3ð ?C¿j¹j»l×b±d°a¸ÌjÐYZÔ\`ÕNaÐ1�qÐ1˜$ §¡ d£Ò 0¨qÓ1ÐbˆDÑbØ:‰FÜ	�Dœ$Ô	ØˆØ,‰äØGÌÈTË
×H[ÑH[ÐG\Ð]ó_ð 	_á‘Ü�$ŒÜ�‰Ð5°vÔ>Ü�~Ô&Ø€KøôC ò HÜ�‰Ð4°aÔ8ÜÐ;¸A¸3Ð?Ó@ÀaÐGûÜò TÜ�‰Ð:¸AÔ>ÜÐGÈÀsÐKÓLÐRSÐSûðTüó Nùó cs<   ÁG	 Â"H0Â;H0Ä%H6Ä>H6Ç		H-Ç%G7Ç7H-È%H(È(H-c                  ó  — t        «       j                  } | j                  «       sg S 	 t        | «      \  }}t        |t        «      r&|j                  dg «      }t        |t        «      r|S dS t        |t        «      r|S dS # t        $ r Y yw xY w)u©   Repair-free read under ``_jobs_lock()``: ``[]`` if missing, ``None`` if corrupt (never
    shrink-merge against an unknown baseline). Never saves â€” that would recurse.Nra  )	rj   r1   rf  r_  r�   rÒ   rÓ   rX   rå   )r1   rn  r3  ra  s       r#   Ú_peek_jobs_unlockedrt  N  sŽ   € ô $Ó%×/Ñ/€IØ×ÑÔØˆ	ðÜ" 9Ó-‰ˆˆaô �$œÔØ�x‰x˜ Ó#ˆÜ! $¬Ô-ˆtÐ7°4Ð7Ü˜d¤DÔ)ˆ4Ð3¨tÐ3øô ò Ùðús   ¨B  Â 	BÂBc                 óˆ   — 	 | j                  «       }|j                  |j                  |j                  fS # t        $ r Y yw xY w)zÕShrink-merge fast-path stamp ``(mtime_ns, size, ino)``; ``None`` if unstatable. ``st_ino`` is
    included because every writer uses mkstemp+rename, so a same-size write in one mtime quantum
    can't false-match.N)ÚstatÚst_mtime_nsÚst_sizeÚst_inor‘   )r1   Ústs     r#   re  re  ^  s>   € ðØ�^‰^ÓˆØ—‘ §
¡
¨B¯I©IÐ6Ð6øÜò Ùðús   ‚25 µ	AÁ AÚstampc                 ó>   — t        t        dd«      r| t        _        yy)a×  Remember jobs.json's stamp for the enclosing _jobs_lock() section (no-op outside one) so the
    save path can skip the shrink-merge when disk provably hasn't changed. Capture it BEFORE
    reading: a mid-read sibling then mismatches (fail-safe); stamping after would certify an
    unseen write.

    Stamping after the read would let that sibling's write be certified as "seen" without being in the
    loaded payload, wrongly suppressing the recovery. See #80703.
    r    r   N)r•   r¥   r§   )r{  s    r#   rg  rg  i  s   € ô Ô ¨!Ô,Ø&+ÔÕ#ð -r7   ra  Úremoved_idsc                 ó"  — t        t        dd«      }|�"t        t        «       j                  «      |k(  rg S t        «       }|€g S | D �ch c]3  }t        |t        «      sŒ|j                  d«      sŒ&t        |d   «      ’Œ5 }}||xs dD �ch c]  }|sŒt        |«      ’Œ c}z  }g }|D ]Y  }t        |t        «      r|j                  d«      sŒ%t        |d   «      }	|	|vsŒ8|j                  |«       |j                  |	«       Œ[ |S c c}w c c}w )zÄOn-disk jobs missing from *jobs* and not intentionally removed. Stamp match => nothing
    landed, return without parsing; unreadable store => ``[]`` (never merge against an unknown
    baseline).r§   NrV   r4   )r•   r¥   re  rj   r1   rt  rÒ   rÓ   rX   rJ   ræ   Úadd)
ra  r}  r{  Ú	disk_jobsÚjÚseenr–  Ú	recoveredÚdisk_jobÚdisk_ids
             r#   Ú_unmerged_disk_jobsr†  v  s  € ô Ô$ l°DÓ9€EØÐÔ-Ô.AÓ.C×.MÑ.MÓNÐRWÒWØˆ	Ü#Ó%€IØÐØˆ	Ø"&ÖN˜Q¬*°Q¼Õ*=À!Ç%Á%ÈÅ+ŒC��$‘�LÐN€DÐNØ˜kÒ/¨RÖ6˜²AŒS��VÒ6Ñ6€DØ&(€IØò ˆÜ˜(¤DÔ)°·±¸dÔ1CØÜ�h˜t‘nÓ%ˆØ˜$ÒØ×Ñ˜XÔ&Ø�H‰H�WÕðð Ðùò OùÚ6s   ÁDÁDÁ0DÂDÂD©r}  c          
      ó°   — t        | |«      }|s| S t        j                  dt        |«      |D �cg c]  }|j	                  d«      ‘Œ c}«       | |z   S c c}w )zÏ*jobs* plus on-disk jobs absent from the payload (under the degraded flock-timeout path a
    stale writer would otherwise clobber concurrent creates). Deletes pass ``removed_ids``; never
    mutates *jobs*.z™Preserved %d cron job(s) present on disk but missing from the in-memory save payload (concurrent create under degraded lock or stale writer) (#80624): %srV   )r†  r‚   rƒ   rŽ  rX   )ra  r}  rƒ  r�  s       r#   Ú_merge_unexpected_disk_jobsr‰  �  sY   € ô $ D¨+Ó6€IÙØˆÜ
‡N�Nð	(ô 	ˆI‹¨iÖ8¨˜Ÿ™˜t�Ò8ô	:ð
 �)ÑÐùò 9s   ¯Ac                 ó�   — | �8t        j                  t        «      5  t        j                  | «       d d d «       y y # 1 sw Y   y xY wrõ   )r­   r®   r‘   r+  rR  r5  s    r#   Ú_unlink_quietr‹     s@   € ØÐÜ× Ñ ¤Ó)ñ 	Ü�I‰I�dŒO÷	ð 	ð ÷	ð 	ús	   œ<¼Ac                 ó¼  — t        j                  t        | j                  «      dd¬«      \  }}	 t	        j
                  |dd¬«      5 }t        j                  |t        «       j                  «       dœ|dd	¬
«       |j                  «        t	        j                  |j                  «       «       ddd«       |S # 1 sw Y   |S xY w# t        $ r t        |«       ‚ w xY w)zXSerialize the store payload to a fsynced temp file next to *jobs_file*; return its path.z.tmpz.jobs_)ÚdirÚsuffixÚprefixÚwr¢   r£   )ra  Ú
updated_atrP  F)ÚindentÚensure_asciiN)ÚtempfileÚmkstemprJ   r  r+  Úfdopenr  Údumpr²  r³  ÚflushÚfsyncr–   ÚBaseExceptionr‹  )r1   ra  ÚfdÚtmp_pathr^  s        r#   Ú_stage_jobs_payloadr�  ¦  s¹   € ä×#Ñ#¬¨I×,<Ñ,<Ó(=ÀfÐU]Ô^�L€Bˆð	Ü�Y‰Y�r˜3¨Ô1ð 	!°QÜ�I‰IØ¬[«]×-DÑ-DÓ-FÑGØ˜!¨%õ1ð �G‰GŒIÜ�H‰H�Q—X‘X“ZÔ ÷	!ð €O÷	!ð €Oûô ò Ü�hÔØðús*   °C ÁA&B8Â.C Â8CÂ=C ÃC ÃCr¨  ©r}  r�  r�  c                ó<  — t        «       j                  }t        «        d}||j                  fD ];  }t	        j
                  t        «      5  t        j                  |«      }	 ddd«        n d}	 t        t        dz   «      D ]s  }|st        | |¬«      } t        || «      }|s#|t        k  rt        | |«      rt        |«       d}ŒCt        ||«       d}t!        |«       t#        ||«       t%        d«        y y# 1 sw Y   ŒÓxY w# t&        $ r t        |«       ‚ w xY w)z»Save all jobs; caller must hold _jobs_lock(). ``removed_ids`` = intentional deletes;
    ``replace=True`` skips the shrink-merge guard (wholesale rewrite for tests/disaster
    recovery).NrŠ   r‡  )rj   r1   r¨   r  r­   r®   r‘   r+  rv  r,  Ú_SAVE_JOBS_MERGE_ATTEMPTSr‰  r�  r†  r‹  r   r$  r0  rg  rš  )ra  r}  r�  r1   Ú_stat_beforeÚproberœ  Úattempts           r#   Ú_save_jobs_unlockedr¤  ¹  s   € ô $Ó%×/Ñ/€IÜ„Mà€LØ˜Y×-Ñ-Ð.ò ˆÜ× Ñ ¤Ó)ñ 	ÜŸ7™7 5›>ˆLØ÷	ñ 	ðð €HðÜÔ6¸Ñ:Ó;ò 	ˆGÙÜ2°4À[ÔQ�Ü*¨9°dÓ;ˆHñ ØÔ7Ò7Ü'¨¨kÔ:ä˜hÔ'Ø�ØÜ˜8 YÔ/ØˆHÜ˜Ô#Ü$ Y°Ô=ô ˜tÔ$Ùñ)	÷	ð 	ûô8 ò Ü�hÔØðús   ÁC9Á0BD Ã7D Ã9D	ÄDc                ó^   — t        «       5  t        | ||¬«       ddd«       y# 1 sw Y   yxY w)zZSave all jobs under the lock; see ``_save_jobs_unlocked`` for ``removed_ids``/``replace``.rž  N)r°   r¤  )ra  r}  r�  s      r#   rl  rl  æ  s.   € ô
 
‹ñ LÜ˜D¨kÀ7ÕK÷L÷ Lñ Lús   ‹#£,Úmissingc                 óÔ   — t        «       5  t        «       }t        |«      D ].  \  }}|j                  d«      | k(  sŒ ||||«      c cddd«       S  	 ddd«       |S # 1 sw Y   |S xY w)zhRun ``fn(jobs, i, job)`` on the first match under ``_jobs_lock()``; ``fn`` saves. *missing*
    if none.rV   N)r°   r^   r�  rX   )rB   rÉ   r¦  ra  r–  rÔ   s         r#   Ú	_with_jobr¨  ò  sn   € ô
 
‹ñ (Ü‹{ˆÜ “oò 	(‰FˆAˆsØ�w‰w�t‹} Ó&Ù˜$  3Ó'Ñ'÷	(ñ (ñ	(÷(ð
 €N÷(ð
 €Nús   ‹0A¼AÁAÁA'c                 ó,   — | j                  ddd¬«       y)zPRetire *job* in place as a terminal completion (record kept for `cronjob list`).Fr  N)r  r	  Únext_run_at©Úupdaterù   s    r#   Ú_complete_job_recordr­  ÿ  s   € à‡J�J�u K¸T€JÕBr7   c                 ó.   — | j                  dddd¬«       y)z8Clear pause markers in place so *job* is runnable again.Tr  N©r  r	  r  Úpaused_reasonr«  rù   s    r#   Ú_activate_job_recordr±    s   € à‡J�J�t ;¸$Èd€JÕSr7   Úworkdirc                 ó^  — | €yt        | «      j                  «       }|syt        |«      j                  «       }|j	                  «       st        d|›d�«      ‚|j                  «       }|j                  «       st        d|› �«      ‚|j                  «       st        d|› �«      ‚t        |«      S )a  Workdir -> absolute path, or None when empty. ``~`` expands; relative paths are rejected
    (cron runs detached from any cwd); must be an existing dir now but is deliberately NOT
    re-checked at run time (scheduler falls back with a warning). ValueError when invalid.Nz+Cron workdir must be an absolute path (got zN). Cron jobs run detached from any shell cwd, so relative paths are ambiguous.zCron workdir does not exist: z!Cron workdir is not a directory: )	rJ   rr   r	   rl   rÞ   ru   re   rf  Úis_dir)r²  ry   ÚexpandedÚresolveds       r#   Ú_normalize_workdirr·  	  s¹   € ð €ØÜ
ˆg‹,×
Ñ
Ó
€CÙØÜ�C‹y×#Ñ#Ó%€HØ×ÑÔ!ÜØ9¸#¸ð AZð [ó\ð 	\ð ×ÑÓ!€HØ�?‰?ÔÜÐ8¸¸
ÐCÓDÐDØ�?‰?ÔÜÐ<¸X¸JÐGÓHÐHÜˆx‹=Ðr7   c                  óì  — ddl m}  t        «       dz  }|j                  «       r | |«      ni }|j	                  d«      xs i }t        |t        «      r$|j	                  d«      xs |j	                  d«      n|}t        |«      }|syd}t        j                  t        «      5  ddlm} t         |d¬	«      j	                  d
«      «      }ddd«       |r|j                  «       |fS d|fS # 1 sw Y   Œ!xY w)a$  ``(provider, model)`` the main agent runs on right now (``model.default`` + the provider it
    resolves to), for ``pinned=True`` jobs: the lock is a plain per-job pin, so the scheduler needs
    no second precedence axis. ``(None, None)`` when nothing is configured (the job stays unpinned).r   )Úload_user_config_effectivezconfig.yamlÚmodelr>   ©NNN)Úresolve_runtime_provider)Ú	requestedÚprovider)Úhermes_cli.config_effectiver¹  r
   rf  rX   rÒ   rÓ   Ú_normalize_job_optional_textr­   r®   r�   Úhermes_cli.runtime_providerr¼  rG  )r¹  Úcfg_pathÚcfgÚ	model_cfgrº  r¾  r¼  s          r#   Ú_main_model_pinrÅ    sã   € õ GäÓ  =Ñ0€HØ2:·/±/Ô2CÑ
$ XÔ
.È€CØ—‘˜Ó Ò& B€IÜBLÈYÔX\ÔB]ˆI�M‰M˜)Ó$Ò>¨	¯©°gÔ(>Ðcl€EÜ(¨Ó/€EÙØØ€HÜ	×	Ñ	œYÓ	'ñ jÝHÜ/Ñ0HÐSWÔ0X×0\Ñ0\Ð]gÓ0hÓiˆ÷jñ !)ˆH�N‰NÓ°UÐ:Ð:¨d°UÐ:Ð:÷jð jús   Â"(C*Ã*C3©Ústrip_trailing_slashrÇ  c                óŽ   — t        | t        «      sy |r| j                  «       j                  d«      n| j                  «       xs d S )NrÜ   )rÒ   rJ   rr   Úrstrip)rì   rÇ  s     r#   rÀ  rÀ  3  s8   € ô �eœSÔ!ØÙ)=ˆE�K‰K‹M× Ñ  Ô%À5Ç;Á;Ã=ÒYÐUYÐYr7   c                 ó   — t        | d¬«      S )NTrÆ  )rÀ  rÃ  s    r#   Ú_normalize_base_urlrË  ;  s   € Ü'¨ÀDÔIÐIr7   rh  c                 ó–   — | D �cg c]5  }t        |«      j                  «       sŒt        |«      j                  «       ‘Œ7 c}xs dS c c}w )zBNon-blank stripped items of *items*, or None when nothing remains.N©rJ   rr   )rh  r�  s     r#   Ú_normalize_str_listrÎ  ?  s1   € à$)Ö<˜q¬S°«V¯\©\­^ŒC�‹F�L‰L�NÒ<ÒDÀÐDùÒ<s
   …A¤Ac                 ób   — t        | t        «      r| g} t        | t        «      rt        | «      S dS )z8Accept a job id or a list of ids; anything else is None.N)rÒ   rJ   rå   rÎ  rÃ  s    r#   Ú_normalize_context_fromrÐ  D  s-   € ä�%œÔØ�ˆÜ)3°E¼4Ô)@Ô˜uÓ%ÐJÀdÐJr7   c                 ó|   — t        | t        t        f«      rdj                  d„ | D «       «      xs dS t	        | «      S )zâfailure_deliver shares deliver's value grammar; flatten str/list like the tool layer's
    _normalize_deliver_param for direct create_job callers. Semantic validation happens at
    resolution time via the shared deliver path.r‰  c              3   ó‚   K  — | ]7  }t        |«      j                  «       sŒt        |«      j                  «       –— Œ9 y ­wrõ   rÍ  rª  s     r#   r\   z-_normalize_failure_deliver.<locals>.<genexpr>P  s&   è ø€ ÒH¨1¼¸Q»¿¹½œ˜A›Ÿ™ŸÑHùs   ‚?¡?N)rÒ   rå   Útupler‘  rÀ  rÃ  s    r#   Ú_normalize_failure_deliverrÔ  K  s7   € ô �%œ$¤˜Ô'Ø�x‰xÑH°ÔHÓHÒPÈDÐPÜ'¨Ó.Ð.r7   c                 ó¤   — | €yt        | «      j                  «       j                  «       }|syddlm}  ||«      €t        d| ›d�«      ‚|dv ry|S )a	  Spelling-only validation via the shared parser (cron knob never stricter/looser than
    config.yaml); model capability is deliberately NOT checked (model unknowable at create time,
    transports clamp at send time). None for unset, lowercase level, or ValueError.Nr   )Úparse_reasoning_effortzInvalid reasoning_effort zg. Valid levels: none, minimal, low, medium, high, xhigh, max, ultra (empty string clears the override).>   ÚfalseÚdisabledrC  )rJ   rr   rG  Úhermes_constantsrÖ  ru   )rì   rà   rÖ  s      r#   Ú_normalize_reasoning_effortrÚ  T  sl   € ð €}ØÜˆu‹:×ÑÓ×#Ñ#Ó%€DÙØÝ7á˜dÓ#Ð+ÜØ'¨ yð 12ð 2ó3ð 	3ð Ð$Ñ$ØØ€Kr7   c                 ó    — | rt        | «      S d S rõ   )rÎ  ©rq  s    r#   ú<lambda>rÝ  r  s   € ¹AÔ"5°aÓ"8€ À4€ r7   )rº  r¾  Úbase_urlrñ   Úmonitor_scriptÚmonitor_urlÚenabled_toolsetsr²  rò   Úcontext_fromÚfailure_deliverÚ_CREATE_FIELD_NORMALIZERSc                 ó$   — | dv rd S t        | «      S )N>   FNrÙ   )r·  rÜ  s    r#   rÝ  rÝ  y  s   €  Ð&7Ñ!7˜€ Ô=OÐPQÓ=R€ r7   )r²  rß  rà  Úreasoning_effortÚ_UPDATE_FIELD_NORMALIZERSrß  rà  rò   rñ   c                 ól   — | r|rt        d«      ‚| s|r|rt        d«      ‚|r|st        t        «      ‚yy)zbExecution-mode invariants shared by create_job and update_job (no bypass via the update
    door).ua   monitor_script and monitor_url are mutually exclusive â€” a job can only have one monitor source.uÈ   monitor_script/monitor_url cannot be combined with no_agent=True â€” the whole point of a monitor job is to suppress or wake the AGENT based on source changes. Use a plain no_agent script job instead.N)ru   ÚNO_AGENT_WITHOUT_SCRIPT_ERROR)rß  rà  rò   rñ   s       r#   Ú_validate_job_mode_invariantsrê  €  sS   € ñ ™+Üð0ó1ð 	1ñ 	™+©8ÜðPóQð 	Qñ ™ÜÔ6Ó7Ð7ð €xr7   r   c                 ó.   — t        d| › dt        › d�«      S )NúRequested one-shot time z is more than z&s in the past and cannot be scheduled.)ru   rÑ  )r   s    r#   Ú_oneshot_past_grace_errorrí  •  s)   € ÜØ
" 6 (¨.Ü Ð
!Ð!Gð	IóJð Jr7   Úparsed_scheduleÚlabelÚfallback_run_atÚwhatc                 óÂ   — t        | «      }| j                  d«      dk(  r?|€=| j                  d«      xs |}t        j                  d|||t        «       t        |«      ‚|S )z¡``compute_next_run`` that raises (after a warning log) for a one-shot outside the grace
    window, so a ghost job with ``next_run_at=None`` can never be stored.r  rD  r   zMRejecting one-shot cron job %s'%s': run_at %s is outside the %ss grace window)r5  rX   r‚   rƒ   rÑ  rí  )rî  rï  rð  rñ  rª  r   s         r#   Ú _next_run_or_reject_past_oneshotró  ›  sg   € ô
 # ?Ó3€KØ×Ñ˜6Ó" fÒ,°Ð1DØ ×$Ñ$ XÓ.ÒA°/ˆÜ�‰Ø[Ø�%˜Ô!6ô	8ô (¨Ó/Ð/ØÐr7   rð   ÚdeliverÚoriginrº  r¾  rÞ  râ  rá  Úattach_to_sessionræ  rã  r  r°  Úpinnedc           	      óÜ  — t        |t        «      st        d«      ‚|�t        |t        «      st        d«      ‚|�|st        d«      ‚t	        |«      }t        |«      }|d   dk(  r|€d}|€|rdnd	}t        j                  «       j                  dd
 }t        «       j                  «       }t        «       }t        j                  «       D ��ci c]  \  }}| |||   «      “Œ }}}t        ||«      }t        |t        «      r|nd} t        |«      }!t!        |d   |d   |d   |d   «       t#        | «      j%                  «       }"|"s|d   s|st        t&        «      ‚ddlm}#  |#|"|d   «       |"xs |r|d   ndxs |d   r|d   ndxs d}$|xs |$dd j%                  «       }|r|d   st-        «       \  |d<   |d<   t/        |||d«      }%i d|“d|“d|"“d|“d|r|d   nd“d|d   “d|d   “d|d   “d|d   “d|d   “d|d   “d|d   “dd“d|d   “d|“d|j1                  d |«      “d!|dd"œ“i d#| “d$|rd%nd&“d'|r|nd“d(|r|xs dj%                  «       xs d)nd“d*|“d+|rdn|%“d,d“d-d“d.d“d/d“d0d“d1d“d2|“d|“d3|d3   “d4|d4   “¥}&d5| fd6|!fd7|d7   ffD ]  \  }}'|'€Œ	|'|&|<   Œ t3        «       5  t5        t7        «       |&gz   «       ddd«       |&S c c}}w # 1 sw Y   |&S xY w)8an  Create a new cron job and return the stored record.

    deliver defaults to "origin" when ``origin`` is given, else "local"; repeat None = forever.
    script: stdout is injected as prompt context, or with ``no_agent=True`` IS the job (stdout
    delivered verbatim, requires ``script``). context_from: job id(s) whose latest output is
    injected. workdir: absolute cwd for tools/scripts. monitor_script/monitor_url: cheap monitor
    source run FIRST each tick; unchanged output suppresses the agent run (mutually exclusive,
    incompatible with ``no_agent``). reasoning_effort: per-job pin; capability NOT validated.zpaused must be a boolean.Nzpaused_reason must be a string.z#paused_reason requires paused=True.r  rD  rŠ   rõ  Úlocalr{  rß  rà  rò   rñ   r   )Úcheck_gateway_lifecycler  r  rº  r¾  rÙ   rV   r  rð   rã   râ   rÞ  Úmonitor_staterâ  rý   rü   rþ   r>  ©Útimesr  r  r	  r  r  r  r°  z+Created paused; awaiting operator approval.Ú
created_atrª  rÍ  rç  Ú
last_errorÚlast_delivery_errorÚlast_delivery_unverifiedÚfailure_streakrô  rá  r²  rö  ræ  rã  )rÒ   r!   ru   rJ   r¾  rJ  r¼   Úuuid4r¿   r²  r³  Úlocalsrä  rh  ré   rÚ  rê  rï   rr   ÚEMPTY_PAYLOAD_ERRORÚcron.lifecycle_guardrú  rÅ  ró  rX   r°   rl  r^   )(rð   rý   r  r>  rô  rõ  râ   rã   rº  r¾  rÞ  rñ   râ  rá  r²  rò   rö  rß  rà  ræ  rã  r  r°  r÷  rî  rB   r   ry   r  Únormr^  Únormalized_skillsÚnormalized_attachÚnormalized_reasoning_effortÚprompt_textrú  r  rª  rÔ   rì   s(                                           r#   Ú
create_jobr  ª  s~  € ôD �fœdÔ#ÜÐ4Ó5Ð5ØÐ ¬°MÄ3Ô)GÜÐ:Ó;Ð;ØÐ ©ÜÐ>Ó?Ð?Ü$ XÓ.€Oô $ FÓ+€FØ�vÑ &Ò(¨V¨^ØˆØ€Ù$‘(¨'ˆÜ�Z‰Z‹\×Ñ˜c˜rÐ"€FÜ
‹-×
!Ñ
!Ó
#€Cä
‹(€CÜ.G×.MÑ.MÓ.O×P¡  dˆ‰d�3�s‘8‹nÑ	ÐP€AÑPÜ-¨e°VÓ<ÐÜ-7Ð8IÌ4Ô-PÑ)ÐVZÐÜ"=Ð>NÓ"OÐä! !Ð$4Ñ"5°q¸Ñ7GÈÈ:ÉÐXYÐZbÑXcÔdÜ" 6Ó*×0Ñ0Ó2€KÙ˜q š{Ñ3DÜÔ,Ó-Ð-å<Ù˜K¨¨8©Ô5ð 	ò 	Ù$5Ð˜aÒ ¸4ò	à˜Zš=ˆAˆhŠK¨dò	ð ð	 ð Ò,�<  Ð$×*Ñ*Ó,€DÙ�a˜’jÜ$3Ó$5Ñ!ˆˆ*‰�q˜‘zÜ2°?ÀDÈ(ÐTVÓW€Kð#Øˆfð#à�ð#ð 	�+ð#ð 	Ð#ð	#ð
 	Ñ):Ð" 1Ò%Àð#ð 	��7‘ð#ð 	�A�j‘Mð#ð 	�A�j‘Mð#ð 	�!�H‘+ð#ð 	�A�j‘Mð#ð 	˜!Ð,Ñ-ð#ð 	�q˜Ñ'ð#ð 	˜ð#ð 	˜˜.Ñ)ð#ð 	�Oð#ð  	˜O×/Ñ/°	¸8ÓDð!#ð" 	˜F°Ñ3ñ##ð$ 	�v�:ð%#ð& 	™V‘¨ð'#ð( 	™F‘S¨ð)#ð* 	Ñms˜=Ò.¨B×5Ñ5Ó7ÒhÑ;hÐy}ð+#ð, 	�cð-#ð. 	™v‘t¨;ð/#ð0 	�tð1#ð2 	�tð3#ð4 	�dð5#ð6 	˜tð7#ð: 	# Dð;#ð< 	˜!ð=#ð> 	�7ð?#ð@ 	�&ðA#ðB 	˜AÐ0Ñ1ðC#ðD 	�1�Y‘<ñE#€CðP 
Ð/Ð0Ð3EÐGbÐ2cØ	˜AÐ/Ñ0Ð1ðò ‰
ˆˆUð ÑØˆC�ŠHðô 
‹ñ 'Ü”)“+  Ñ%Ô&÷'à€JùóQ 	Q÷L'à€Jús   ÃKÊ9K!Ë!K+c                 ó\   ‡ — t        ˆ fd„t        «       D «       d«      }|�t        |«      S dS )zGet a job by ID.c              3   ó4   •K  — | ]  }|d    ‰k(  sŒ|–— Œ y­wrU   r4   )rZ   r�  rB   s     €r#   r\   zget_job.<locals>.<genexpr>,  s   øè ø€ Ò<�a¨!¨D©'°VÓ*;”Ñ<ùó   ƒ‘N)rÑ   r^   r  )rB   rÔ   s   ` r#   Úget_jobr  *  s,   ø€ ä
Ó<œ9›;Ô<¸dÓ
C€CØ),¨Ô  Ó%ÐB¸dÐBr7   c                   ó<   ‡ — e Zd ZdZdedeeeef      fˆ fd„Zˆ xZ	S )ÚAmbiguousJobReferencez1Raised when a job name matches more than one job.ÚrefÚmatchesc           	      ó–   •— || _         || _        dj                  d„ |D «       «      }t        ‰| �  d|› dt        |«      › d|› d�«       y )Nrd  c              3   ó&   K  — | ]	  }|d    –— Œ y­wrU   r4   )rZ   rK  s     r#   r\   z1AmbiguousJobReference.__init__.<locals>.<genexpr>6  s   è ø€ Ò1 A˜˜$�Ñ1ùs   ‚z
Job name 'u   ' is ambiguous â€” matches z jobs: z. Use the job ID instead.)r  r  r‘  ÚsuperrH   rŽ  )rG   r  r  ÚidsÚ	__class__s       €r#   rH   zAmbiguousJobReference.__init__3  sU   ø€ ØˆŒØˆŒØ�i‰iÑ1¨Ô1Ó1ˆÜ‰ÑØ˜˜Ð8¼¸W»¸ÀgÈcÈUð S&ð 'õ	(r7   )
r8   r9   r:   rI   rJ   r   r   r   rH   Ú__classcell__)r  s   @r#   r  r  0  s+   ø„ Ù;ð(˜Cð (¨$¨t°C¸°H©~Ñ*>÷ (ñ (r7   r  r  c           	      ó†  ‡ — ‰ syt        «       }t        ˆ fd„|D «       d«      }|�t        |«      S ‰ j                  «       }|D �cg c]+  }|j	                  d«      xs dj                  «       |k(  sŒ*|‘Œ- }}|syt        |«      dkD  r#t        ‰ |D �cg c]  }t        |«      ‘Œ c}«      ‚t        |d   «      S c c}w c c}w )z¦Resolve an ID or name to a job record. Exact ID wins, then case-insensitive name; an
    ambiguous name raises AmbiguousJobReference rather than silently picking one.Nc              3   ó4   •K  — | ]  }|d    ‰k(  sŒ|–— Œ y­wrU   r4   )rZ   r�  r  s     €r#   r\   z"resolve_job_ref.<locals>.<genexpr>B  s   øè ø€ Ò4˜ Q t¡W°£^”!Ñ4ùr  r  rÙ   rŠ   r   )r^   rÑ   r  rG  rX   rŽ  r  )r  ra  Úby_idÚ	ref_lowerr�  Úname_matchess   `     r#   Úresolve_job_refr   <  s¼   ø€ ñ ØÜ‹;€DÜÓ4˜TÔ4°dÓ;€EØÐÜ$ UÓ+Ð+Ø—	‘	“€IØ#ÖR˜!¨¯©¨f«Ò(;¸×'BÑ'BÓ'DÈ	Ó'Q’AÐR€LÐRÙØÜ
ˆ<Ó˜1ÒÜ# CÈLÖ)YÀqÔ*?ÀÕ*BÒ)YÓZÐZÜ  ¨a¡Ó1Ð1ùò Sùò *Zs   Á+B9Á1B9ÂB>
Úinclude_disabledc           	      ó~  — t        «       D �cg c]  }t        |«      ‘Œ }}| s"|D �cg c]  }|j                  dd«      sŒ|‘Œ }}	 ddlm}  ||D �cg c]  }|j                  dd«      ‘Œ c}«      }|D ]&  }|j                  |j                  dd«      «      |d<   Œ( |S c c}w c c}w c c}w # t
        $ r i }Y ŒIw xY w)z2List all jobs, optionally including disabled ones.r  Tr   )Úlatest_executionsrV   rÙ   Úlatest_execution)r^   r  rX   Úcron.executionsr#  r�   )r!  r�  ra  r#  rÔ   Úlatests         r#   Ú	list_jobsr'  N  sÁ   € ä.7«kÖ:¨Ô! !Õ$Ð:€DÐ:ÙØÖ:�a 1§5¡5¨°DÕ#9’Ð:ˆÐ:ðÝ5á"ÀÖ#F¸# C§G¡G¨D°"Õ$5Ò#FÓGˆð ò @ˆØ"(§*¡*¨S¯W©W°T¸2Ó->Ó"?ˆÐÒð@à€Kùò ;ùâ:ùò $GøÜò ØŠðús3   �B§B$¿B$ÁB. ÁB)Á+B. Â)B. Â.B<Â;B<Úupdatedc                 óà   — t        | «      rct        | «      sW|j                  d«      dvs$|j                  d«      du s|j                  d«      �t        d| j                  d|«      › d	�«      ‚yyy)
zTA genuinely terminal job cannot be reactivated through update_job (use cron resume).r	  >   r¬   r  r  Trª  Nz#Cannot activate terminal cron job 'r  z8' through update_job; use cron resume --run-now or --at.)r  r  rX   ru   )rÔ   r(  rB   s      r#   Ú_reject_terminal_activationr*  ^  s†   € ô 	˜ÔÜ)¨#Ô.à�K‰K˜Ó Ð(>Ñ>Ø�{‰{˜9Ó%¨Ñ-Ø�{‰{˜=Ó)Ð5ô Ø1°#·'±'¸&À&Ó2IÐ1Jð KEð EóFð 	Fð 6ð	 /ð 	r7   Úupdatesc                 óÄ   — d|vryt        |j                  d«      «      }d|v ry|r/t        | j                  d«      «      st	        «       \  |d<   |d<   yyd\  |d<   |d<   y)a  ``pinned`` is not stored; it rewrites the per-job pin. ``True`` with no explicit model locks
    the main agent's current provider+model onto the job, ``False`` releases both so the job follows
    the main model again (an explicit ``model`` in the same update wins over either).r÷  Nrº  r¾  r»  )r!   rÀ   rÀ  rX   rÅ  )rÔ   r+  r÷  s      r#   Ú_apply_pin_updater-  n  ss   € ð �wÑØÜ�'—+‘+˜hÓ'Ó(€FØ�'ÑØÙÜ+¨C¯G©G°GÓ,<Ô=Ü4CÓ4EÑ1ˆG�JÑ ¨Ò!1ð >ð 1;Ñ-ˆ�
Ñ˜W WÒ-r7   c                 ó‚  — t         j                  «       D ]  \  }}||v sŒ |||   «      ||<   Œ d|v rŒ|d   }| j                  d«      xs i j                  dd«      }t        |t        «      r@t	        |«      }t        |j                  d«      «      |d<   |j                  d|«       ||d<   yt        |«      |dœ|d<   yy)z±Normalize updates in place like create_job; invalid values raise BEFORE the merge. ``repeat``
    accepts the stored dict or a bare value (coerced, completed counter preserved).r>  r  r   rý  rü  N)rç  rh  rX   rÒ   rÓ   rJ  rµ   )rÔ   r+  r  r  Ú_rpr  s         r#   Ú_normalize_job_updatesr0  ~  sÉ   € ô /×4Ñ4Ó6ò .‰	ˆˆTØ�'Š>Ù ¨¡Ó-ˆG�CŠLð.ð �7ÑØ�hÑˆØ—W‘W˜XÓ&Ò,¨"×1Ñ1°+¸qÓAˆ	Ü�cœ4Ô Ü�s“)ˆCÜ1°#·'±'¸'Ó2BÓCˆC�‰LØ�N‰N˜;¨	Ô2Ø #ˆG�HÒä*@ÀÓ*EÐT]Ñ ^ˆG�HÒð r7   c                 ó¨  — d|vsd|v ry|d   }t        |t        «      rt        |«      }||d<   | j                  d«      xs i j                  d«      }|j                  d«      }||k(  ryt	        | j                  d«      xs i «      }|j                  d«      }|dk(  r|€d|d<   n|dk7  r|dk(  r|dk(  rd|d<   ny|j                  dd	«       ||d<   y)
aÙ  Re-derive the ``repeat`` default when a schedule update flips the kind.

    ``create_job`` derives it from the schedule kind (once -> 1, recurring -> forever); the update
    path must honour the same contract, otherwise a one-shot turned recurring keeps its ``times=1``
    budget and retires after one fire, while a recurring job turned one-shot never completes. An
    explicit ``repeat`` in the same update wins; a same-kind schedule edit leaves ``repeat`` alone.
    rý   r>  Nr  rý  rD  rŠ   r  r   )rÒ   rJ   r¾  rX   rÓ   rµ   )rÔ   r+  Únew_scheduleÚold_kindÚnew_kindr>  rý  s          r#   Ú$_rederive_repeat_for_schedule_changer5  �  sí   € ð ˜Ñ  H°Ñ$7ØØ˜:Ñ&€LÜ�,¤Ô$Ü% lÓ3ˆØ*ˆ�
ÑØ—‘˜
Ó#Ò) r×.Ñ.¨vÓ6€HØ×Ñ Ó'€HØ�8ÒØÜ�#—'‘'˜(Ó#Ò) rÓ*€FØ�J‰J�wÓ€EØ�6Ò˜e˜mØˆˆwŠØ	�VÒ	 ¨FÒ 2°uÀ²zØˆˆwŠàØ
×Ñ�k 1Ô%Ø€GˆHÒr7   c           	      ó"  — | d   }t        |t        «      rt        |«      }|| d<   |j                  d|j                  d| j                  d«      «      «      | d<   | j                  d«      dk7  r"t	        || j                  d|«      |d«      | d<   y	y	)
zZParse a string schedule, refresh ``schedule_display`` and (unless paused) ``next_run_at``.rý   rü   rþ   r	  r  r  zupdate rª  N)rÒ   rJ   r¾  rX   ró  )r(  r+  rB   Úupdated_schedules       r#   Ú_apply_schedule_updater8  °  s›   € à˜zÑ*ÐÜÐ"¤CÔ(Ü)Ð*:Ó;ÐØ.ˆ�
ÑØ")§+¡+ØÐ,×0Ñ0°¸G¿K¹KÐHZÓ<[Ó\ó#^€GÐÑà‡{�{�7Ó˜xÒ'Ü!AØ˜gŸk™k¨&°&Ó9Ð;KÈYó"Xˆ�Òð (r7   c                 ó"  — | j                  dd«      r%| j                  d«      dk(  s| j                  d«      ryt        | d   «      }|€B| d   j                  d«      d	k(  r+| d   j                  d
d«      }t        d|› dt        › d�«      ‚|| d<   y)zbAn enabled, unpaused record must never persist without ``next_run_at`` (it would never fire).
    r  Tr	  r  rª  Nrý   r  rD  r   r  rì  ú is in the past (grace window: zs) and cannot be scheduled.)rX   r5  ru   rÑ  )r(  r.  r   s      r#   Ú_fill_missing_next_runr;  ½  s«   € ð �K‰K˜	 4Ô(Ø�;‰;�wÓ 8Ò+Ø�;‰;�}Ô%àÜ ¨
Ñ 3Ó4€HØÐ˜G JÑ/×3Ñ3°FÓ;¸vÒEØ˜Ñ$×(Ñ(¨°9Ó=ˆÜØ& v hð /Ü3Ð4Ð4OðQóRð 	Rð &€GˆMÒr7   c                 ó¬   ‡ ‡— t         j                  ‰xs i «      }|r&t        ddj                  t	        |«      «      › �«      ‚ˆ ˆfd„}t        ‰ |«      S )zCUpdate a job by ID, refreshing derived schedule fields when needed.z%Cron job field(s) cannot be updated: rd  c           
      óÄ  •— t        |‰«       t        |‰«       t        |‰«       t        i |¥‰¥«      }t	        ||‰«       h d£j                  ‰«      rdt        |j                  d«      xs d |j                  d«      xs d t        |j                  d«      «      t        |j                  d«      «      «       t        ˆfd„t        D «       «      rt        |«      rt        t        «      ‚d‰v rt        |‰‰«       dd	lm}  ||«       h d
£j                  ‰«      r|j%                  dd «       t'        |«       t	        ||‰«       || |<   t)        | «       t+        |«      S )N>   rñ   rò   rà  rß  rß  rà  rò   rñ   c              3   ó&   •K  — | ]  }|‰v –— Œ
 y ­wrõ   r4   )rZ   rö   r+  s     €r#   r\   z,update_job.<locals>.apply.<locals>.<genexpr>ã  s   øè ø€ Ò5 ˆq�GŒ|Ñ5ùr÷   rý   r   )Úclear_state>   r	  r  rý   rª  Úpending_slot)r5  r0  r-  rë   r*  Úintersectionrê  rX   r!   rÀ  rø   Ú_PAYLOAD_FIELDSrú   ru   r  r8  ré  r?  rÀ   r;  rl  r  )ra  r–  rÔ   r(  Ú_clear_quota_holdrB   r+  s        €€r#   Úapplyzupdate_job.<locals>.applyÖ  s2  ø€ Ü,¨S°'Ô:Ü˜s GÔ,Ü˜#˜wÔ'Ü%Ð&8¨Ð&8°Ð&8Ó9ˆÜ# C¨°&Ô9âB×OÑOÐPWÔXÜ)Ø—‘Ð,Ó-Ò5°Ø—‘˜MÓ*Ò2¨dÜ�W—[‘[ Ó,Ó-Ü,¨W¯[©[¸Ó-BÓCô	Eô
 Ó5¤_Ô5Ô5Ô:NÈwÔ:WÜÔ0Ó1Ð1Ø˜Ñ Ü" 7¨G°VÔ<õ IÙ˜gÔ&Ú:×GÑGÈÔPð �K‰K˜¨Ô-Ü˜wÔ'Ü# C¨°&Ô9ØˆˆQ‰Ü�$ŒÜ$ WÓ-Ð-r7   )Ú_IMMUTABLE_JOB_FIELDSrA  ru   r‘  Úsortedr¨  )rB   r+  Ú
bad_fieldsrD  s   ``  r#   Ú
update_jobrH  Ï  sT   ù€ ô '×3Ñ3°G²M¸rÓB€JÙÜÐ@ÀÇÁÌ6ÐR\ÓK]ÓA^Ð@_Ð`ÓaÐaõ.ô@ �V˜UÓ#Ð#r7   Úreasonc                 ór   — t        | «      }|syt        |d   ddt        «       j                  «       |dœ«      S )z:Pause a job without deleting it. Accepts a job ID or name.NrV   Fr  r¯  )r   rH  r²  r³  )rB   rI  rÔ   s      r#   Ú	pause_jobrK  ù  sA   € ä
˜&Ó
!€CÙØÜ�c˜$‘iØØÜ “]×,Ñ,Ó.Øñ	"ó ð r7   c           	      óÐ  — t        | «      }|sy|j                  d«      }|rt        |«      nd}|d   j                  d«      dv r<|�:|t        «       k  r-|}t        j                  d|j                  d|d   «      |«       nt        |d   «      }|€B|d   j                  d«      d	k(  r+|d   j                  d
d«      }t        d|› dt        › d�«      ‚t        |d   dddd|dœ«      S )uÜ  Resume a paused job. Accepts a job ID or name.

    A recurring job paused across one of its slots must not lose that slot silently: the stored
    ``next_run_at`` (already past) survives resume as the due instant, and the ordinary late /
    catch-up / ``cron.catch_up_missed`` policy in the due scan decides what happens to it â€” one
    fire or a logged skip, never a silent re-anchor past it (#113603). One-shots and future
    instants recompute from now as before.
    Nrª  rý   r  >   r%   r  z€Job '%s' resumed with occurrence %s that elapsed while paused kept due; the next tick fires it (late/catch-up) or logs the skip.r  rV   rD  r   r  zCannot resume: one-shot time r:  zs) and will never fire.Tr  )r  r	  r  r°  rª  )
r   rX   rÄ  r²  r‚   Úinfor5  ru   rÑ  rH  )rB   rÔ   Ústored_nextÚ	stored_dtrª  r   s         r#   Ú
resume_jobrP    s  € ô ˜&Ó
!€CÙØØ—'‘'˜-Ó(€KÙ-8”˜[Ô)¸d€IàˆJ‰×Ñ˜FÓ#Ð';Ñ;ØÐ!Øœ›Ò&à!ˆÜ�‰ð>à�G‰G�F˜C ™IÓ&¨õ	5ô
 ' s¨:¡Ó7ˆØÐ˜s :™×2Ñ2°6Ó:¸fÒDØ�Z‘×$Ñ$ X¨yÓ9ˆÜØ+¨F¨8ð 4Ü3Ð4Ð4KðMóNð 	Nô �c˜$‘iØØØØØ"ñ"ó ð r7   Úextra_promptc                 ó  — t        | «      }|syt        |«      r6|j                  d| «      }t        d|› d|j                  d«      › d|› d�«      ‚t	        «       j                  «       }t        |d   d	d
dd|||xs ddœ«      S )zžSchedule a job for the next tick (ID or name). ``extra_prompt`` is stamped as
    ``manual_run_prompt`` for that single fire only; ``mark_job_run`` clears it.Nr  zCannot run: job 'z' is r	  z> (terminal). Create a new occurrence with 'hermes cron resume z! --run-now' or '--at <ISO-8601>'.rV   Tr  )r  r	  r  r°  rª  Úmanual_run_atÚmanual_run_prompt)r   r  rX   ru   r²  r³  rH  )rB   rQ  rÔ   r  rS  s        r#   Útrigger_jobrU  .  sª   € ô ˜&Ó
!€CÙØÜ�sÔØ�w‰w�v˜vÓ&ˆÜØ ˜v U¨3¯7©7°7Ó+;Ð*<ð =@Ø@D¸vð F/ð/ó0ð 	0ô  “M×+Ñ+Ó-€MÜ�c˜$‘iØØØØØ$à&Ø*Ò2¨dñ	"ó 	ð 	r7   rÕ   c                 ó,  — t        | j                  d«      xs d«      j                  d«      }t        |«      dk  s|d   j	                  «       sy	 ddl}|d   |j                  «       k7  rydd	lm}  |t        |d   «      «       S # t        $ r Y yw xY w)
a  True when the claim's ``by`` names a process on THIS host that provably no longer exists.
    ``_machine_id()`` stamps ``host:pid[:token]``; a foreign host, an explicit HERMES_MACHINE_ID,
    or any liveness-probe failure returns False (fail safe: only a proven death shortens the TTL).rÐ   rÙ   ú:rP  rŠ   Fr   N)Ú_pid_exists)rJ   rX   rŒ  rŽ  ÚisdigitÚsocketÚgethostnameÚgateway.statusrX  rH  r�   )rÕ   r4  rZ  rX  s       r#   Ú_claim_owner_is_deadr]  G  sŽ   € ô �—	‘	˜$“Ò% 2Ó&×,Ñ,¨SÓ1€EÜ
ˆ5ƒz�A‚~˜U 1™X×-Ñ-Ô/ØðÛØ�‰8�v×)Ñ)Ó+Ò+ØÝ.Ùœs 5¨¡8›}Ó-Ð-Ð-øÜò Ùðús   ÁB Á,B Â	BÂBÚttl_secondsc                 ó¼   — t        | t        «      r| j                  d«      syt        | d   «      }|�d||z
  j	                  «       cxk  r|k  sy yt        | «       S )aœ  True for a well-formed claim aged within ``[0, ttl)`` whose owner is not provably dead:
    future-dated (clock/TZ skew) or malformed claims count as stale so they can never wedge a
    job, and a same-host owner pid that has exited releases the claim immediately instead of
    after the TTL (a killed ``hermes cron run`` otherwise blocks the next manual run for the
    full window with "already being fired").r‹  Fr   )rÒ   rÓ   rX   rÄ  rë  r]  )rÕ   r   r^  Ú
claimed_ats       r#   rê  rê  X  sc   € ô �eœTÔ"¨%¯)©)°D¬/ØÜ˜e D™kÓ*€JØÐ !¨¨jÑ(8×'GÑ'GÓ'IÔ"WÈKÒ"WØð #XØÜ# EÓ*Ð*Ð*r7   zTCannot re-arm recurring jobs: re-arm is one-shot-only; use plain resume or cron run.c                 óT  ‡‡‡— t        | «      }|syt        ‰t        «      r‰j                  «       Št	        t        ‰«      «      Š‰j                  d«      dk7  rt        t        «      ‚t        ‰«      Š‰€t        ‰j                  d«      xs ‰«      ‚ˆˆˆfd„}t        |d   |«      S )z:Re-arm a completed one-shot as an explicit new occurrence.Nr  rD  r   c           	      ó  •— t        «       }t        |j                  d«      |t        «       «      rt	        d«      ‚t        |j                  d«      |t
        «      rt	        d«      ‚|j                  di «      j                  d«      dk7  rt	        t        «      ‚|j                  d«      xs i }d	|d
<   |j                  ‰‰j                  dt        ‰«      «      |d d ¬«       t        |«       ‰|d<   t        | «       t        |«      S )NÚ	run_claimz-Cannot re-arm one-shot over a live run claim.rÏ   z.Cannot re-arm one-shot over a live fire claim.rý   r  rD  r>  r   r  rþ   )rý   rü   r>  rc  rÏ   rª  )r²  rê  rX   r{   ru   r   Ú_REARM_RECURRING_ERRORr¬  rJ   r±  rl  r  )ra  Ú_irÔ   r   r>  rª  rî  r   s        €€€r#   rD  zrearm_oneshot.<locals>.applyy  sï   ø€ Ü‹mˆÜ˜#Ÿ'™' +Ó.°Ô5SÓ5UÔVÜÐLÓMÐMÜ˜#Ÿ'™' ,Ó/°Ô6LÔMÜÐMÓNÐNØ�7‰7�:˜rÓ"×&Ñ& vÓ.°&Ò8ÜÔ3Ó4Ð4Ø—‘˜Ó"Ò( bˆØˆˆ{ÑØ�
‰
Ø$°×7JÑ7JÈ9ÔVYÐZ`ÓVaÓ7bØ T°dð 	ô 	<ô 	˜SÔ!Ø(ˆˆMÑÜ�$ŒÜ$ SÓ)Ð)r7   rV   )r   rÒ   r   r³  r¾  rJ   rX   ru   rd  r5  rí  r¨  )rB   r   Újob_refrD  rª  rî  s    `  @@r#   Úrearm_oneshotrg  k  s›   ú€ ä˜fÓ%€GÙØÜ�&œ(Ô#Ø×!Ñ!Ó#ˆÜ$¤S¨£[Ó1€OØ×Ñ˜6Ó" fÒ,ÜÔ/Ó0Ð0Ü" ?Ó3€KØÐÜ'¨×(;Ñ(;¸HÓ(EÒ(OÈÓPÐPö*ô$ �W˜T‘] EÓ*Ð*r7   c                 óî  — t        | «      }|sy|d   }t        «       5  t        «       }t        |«      }|D �cg c]  }|d   |k7  sŒ|‘Œ }}t        |«      |k(  r
	 ddd«       yt	        |«      }t        ||h¬«       t        j                  «       }|�|j                  |k(  rd|_	        |j                  «       rt        j                  |«       	 ddlm}  ||«       t%        «       j&                  j)                  «       › d
|› �}	t*        5  t,        j/                  |	d«       ddd«       	 ddd«       yc c}w # t        $ r t         j#                  d|d¬	«       Y Œ~w xY w# 1 sw Y   Œ?xY w# 1 sw Y   yxY w)zRemove a job by ID or name.FrV   Nr‡  Tr   )Úclear_notepadz*Failed to clear notepad for removed job %sr~   r²   )r   r°   r^   rŽ  rá   rl  rL   rX   rB   rE   rf  ÚshutilÚrmtreeÚcron.notepadri  r�   r‚   r  rj   r0   re   r´   r*   rÀ   )
rB   rÔ   Úcanonical_idra  Úoriginal_lenr�  Újob_output_dirrP   ri  Ú
_fence_keys
             r#   Ú
remove_jobrq  Ž  sd  € ä
˜&Ó
!€CÙØØ�t‘9€LÜ	‹ñ Ü‹{ˆÜ˜4“yˆØÖ;�a 1 T¡7¨lÓ#:’Ð;ˆÐ;Üˆt‹9˜Ò$Ø÷ð ô )¨Ó6ˆÜ�$ \ NÕ3Ü'×+Ñ+Ó-ˆØÐ &§-¡-°<Ò"?Ø!ˆFŒNØ× Ñ Ô"Ü�M‰M˜.Ô)ð	dÝ2Ù˜,Ô'ô ,Ó-×6Ñ6×>Ñ>Ó@ÐAÀÀLÀ>ÐRˆ
Ü$ñ 	4Ü×!Ñ! *¨dÔ3÷	4à÷/ð ùò <øô ò 	dÜ�L‰LÐEÀ|Ð^bˆLÖcð	dú÷	4ð 	4ú÷+ð úse   žE+¸D3ÁD3Á
E+Á$A*E+ÃD8Ã-E+Ä
EÄ!	E+Ä3E+Ä8!EÅE+ÅEÅE+ÅE(	Å$E+Å+E4r   c                 ó,   ‡‡— ˆˆfd„}t        | |d«      S )aÂ  Set/clear a persisted alert-dedup marker (alert exactly once until the condition heals;
    survives restarts) and return the PRIOR value. Field: ``preflight_alerted`` (blocked config).

    The marker records that the operator was already alerted about this job's condition, so the scheduler
    alerts exactly once and stays silent on subsequent ticks until the condition heals (same alert-once
    shape as the dead-pin auto-pause in #73506).
    c                 ó�   •— t        |j                  ‰«      «      }‰rd|‰<   n|j                  ‰d «       |‰k7  rt        | «       |S )NT)r!   rX   rÀ   rl  )ra  re  rÔ   Úpriorr   rì   s       €€r#   rD  z_set_alert_flag.<locals>.apply¶  sB   ø€ Ü�S—W‘W˜U“^Ó$ˆÙØˆC�ŠJà�G‰G�E˜4Ô Ø�EŠ>Ü�dŒOØˆr7   F©r¨  )rB   r   rì   rD  s    `` r#   Ú_set_alert_flagrv  ®  s   ù€ õô �V˜U EÓ*Ð*r7   c                 ó   — t        | dd«      S )zAMark the job as preflight-alerted; return True if it already was.Úpreflight_alertedT©rv  ©rB   s    r#   Úmark_preflight_alertedr{  Ã  s   € ä˜6Ð#6¸Ó=Ð=r7   c                 ó   — t        | dd«       y)z@Clear the preflight alert-dedup marker (config validates again).rx  FNry  rz  s    r#   Úclear_preflight_alertedr}  È  s   € ä�FÐ/°Õ7r7   Údetailc                 ó(   ‡— ˆfd„}t        | |d«      S )u)  Durably record (as ``last_fire_error``) that a scheduled fire could not be handed to the
    runner â€” written by the dashboard fire webhook when the loopback forward fails. Without it
    the miss is invisible (no execution row, last_status only covers started runs); mark_job_run
    clears it.c                 óx   •— t        «       j                  «       t        ‰xs d«      d d dœ|d<   t        | «       y)NrÙ   iô  )r‹  r~  Úlast_fire_errorT)r²  r³  rJ   rl  )ra  re  rÔ   r~  s      €r#   rD  z(note_fire_forward_failure.<locals>.applyÒ  s;   ø€ ä“-×)Ñ)Ó+´s¸6º<ÀRÓ7HÈÈ#Ð7Nñ"PˆÐÑä�$ŒØr7   Fru  )rB   r~  rD  s    ` r#   Únote_fire_forward_failurer‚  Í  s   ø€ ô
ô �V˜U EÓ*Ð*r7   r¬   Údelivery_errorÚstatusc                 óæ  — || d<   | j                  dd«       | j                  dd«       t        |t        «      xr t        |j	                  «       «      }|xs
 |sdn|rdnd| d<   |rdn|| d	<   |r*| j                  d
d«       | j                  dd«       d| d<   n$t        | j                  d«      xs d«      dz   | d<   || d<   d| d<   | j                  dd«       | j                  d«      �d| d<   yy)zYStamp one completed run onto *job*: status fields, failure streak, alert markers, claims.rÍ  rS  NrT  r¬   Údelivery_failedÚokrç  rÿ  rx  r�  r   r  rŠ   r   rÏ   r@  rc  )rÀ   rÒ   rJ   r!   rr   rH  rX   )rÔ   r<  r¬   rƒ  r„  r   r†  s          r#   Ú_record_run_outcomerˆ  Û  s  € ð
 €CˆÑØ‡G�GˆO˜TÔ"à‡G�GÐ Ô&Ü  ´Ó5ÒV¼$¸~×?SÑ?SÓ?UÓ:V€OØò VÙ‰¹/Ñ%6Ètð ˆÑá '™¨U€CˆÑÙð 	�‰Ð# TÔ*Ø�‰Ð! 4Ô(Ø !ˆÐÒô !$ C§G¡GÐ,<Ó$=Ò$BÀÓ CÀaÑ GˆÐÑØ!/€CÐÑà€CˆÑØ‡G�GˆN˜DÔ!Ø
‡w�wˆ{ÓÐ'ØˆˆKÒð (r7   c           
      óB  — | j                  di «      j                  d«      }| j                  d«      }|rW|j                  d«      }|duxr |dkD  }|j                  dd«      }|dk(  r|r|dkD  s
|d	z  }||d<   |r||k\  rt        | «       yt        | d   |«      | d
<   | d
   �| j                  d«      dk7  rd| d<   yy|dv rSd| d<   | j                  d«      sd| d<   t        j	                  d| j                  d| j                  dd«      «      |«       yt        | «       y)zªBump ``repeat.completed`` and recompute ``next_run_at``; retire the record as a terminal
    completion when the repeat limit is reached or a one-shot has no further run.rý   r  r>  rý  Nr   r  rD  rŠ   rª  r	  r  r  >   r%   r  r¬   rÿ  ztFailed to compute next run for recurring schedule (is the 'croniter' package installed in the gateway's Python env?)zyJob '%s' (%s) could not compute next_run_at; leaving enabled and marking state=error so the job is not silently disabled.r  rV   r  )rX   r­  r5  r‚   r¬   )rÔ   r   r  r>  rý  Úfiniter  s          r#   Ú_advance_after_runr‹  ú  sE  € ð �7‰7�:˜rÓ"×&Ñ& vÓ.€Dð �W‰W�XÓ€FÙØ—
‘
˜7Ó#ˆØ˜dÐ"Ò0 u¨q¡yˆØ—J‘J˜{¨AÓ.ˆ	ð ˜’¡6¨i¸!ªmØ˜‰NˆIØ"+ˆF�;ÑÙ�i 5Ò(ô ! Ô%Øä)¨#¨j©/¸3Ó?€CˆÑØ
ˆ=ÑÐ%Ø�7‰7�7Ó˜xÒ'Ø&ˆC�ŠLð (à	Ð%Ñ	%ð ˆˆG‰Ø�w‰w�|Ô$ð:ð �Ñô 	�‰ð[à�G‰G�F˜CŸG™G D¨#Ó.Ó/°õ	7ô
 	˜SÕ!r7   )Úexpected_fire_ownerÚmodel_unreachableÚquota_hold_secondsrŒ  r�  rŽ  c                óP   ‡ ‡‡‡‡‡‡‡‡	— ˆˆˆˆ ˆˆˆˆfd„Š	ˆ	ˆ fd„}t        ‰ |«      S )uª  Mark a job as run: update last_run_at/last_status, bump completed, recompute next_run_at,
    and retire the record as a terminal completion when the repeat limit is reached.

    ``delivery_error`` is separate from the agent error: agent succeeded but delivery failed records
    ``last_status = "delivery_failed"`` (never "ok") while ``failure_streak`` is left alone. An
    explicit ``status`` (e.g. "blocked_config") overrides the derived value. False when the fence
    can't be taken, the job is missing, or ``expected_fire_owner`` no longer holds the fire claim.

    ``model_unreachable``: this failed run never reached the model (transient network/DNS error,
    zero API calls). Recurring jobs then get a bounded automatic re-run â€” ``next_run_at`` is pulled
    earlier per ``cron.unreachable_retry.RETRY_DELAYS_SECONDS`` â€” instead of waiting a full period
    (Cowork-style; see cron/unreachable_retry.py).

    ``quota_hold_seconds``: the provider said it stays closed for this long (a quota 429 with
    ``retry after <N>s``). Recurring jobs are parked at their first occurrence after the window
    instead of re-firing into it on every tick (cron/quota_hold.py, #89376).
    c                 óâ  •— ‰
�L|j                  d«      }t        |t        «      r|j                  d«      ‰
k7  rt        j	                  d‰«       yt        «       j                  «       }t        |‰‰	‰‰|«       t        ||«       ddl	m
} ddlm}m} ‰s‰rt        |«      s	 ||«       n ||«       ‰s ‰rt        |«      s|j                  |‰«       n|j                  |«       t!        | «       y)	NrÏ   rÐ   zMmark_job_run: job_id %s fire claim owner changed; discarding stale completionFr   )Ú
quota_hold)r?  Ú
plan_retryT)rX   rÒ   rÓ   r‚   rƒ   r²  r³  rˆ  r‹  r%   r‘  Úcron.unreachable_retryr?  r’  r  Ú	plan_holdrl  )ra  re  rÔ   rÕ   r   r‘  r?  r’  rƒ  r¬   rŒ  rB   r�  rŽ  r„  r<  s           €€€€€€€€r#   rD  zmark_job_run.<locals>.applyE	  sÎ   ø€ ØÐ*Ø—G‘G˜LÓ)ˆEÜ˜e¤TÔ*¨e¯i©i¸«oÐATÒ.TÜ—‘ØcØôð Ü‹m×%Ñ%Ó'ˆÜ˜C ¨%°ÀÈÔMÜ˜3 Ô$Ý#ßBáÑ,´_ÀSÔ5IÙ�s�Oñ ˜ÔÙÑ-´oÀcÔ6JØ× Ñ  Ð&8Õ9à×"Ñ" 3Ô'Ü�$ŒØr7   c                  ój   •— t        ‰‰t        ¬«      } | t        u rt        j                  d‰«       y| S )N©r¦  z0mark_job_run: job_id %s not found, skipping saveF)r¨  Ú_MISSINGr‚   rƒ   )ÚfoundrD  rB   s    €€r#   Úlockedzmark_job_run.<locals>.locked_	  s0   ø€ Ü˜& %´Ô:ˆØ”HÑÜ�N‰NÐMÈvÔVØØˆr7   ©rË   )
rB   r<  r¬   rƒ  r„  rŒ  r�  rŽ  r™  rD  s
   ```````` @r#   Úmark_job_runr›  )	  s"   ÿø€ ÷8ó õ4ô ˜V VÓ,Ð,r7   c                 ó¸   — 	 t        | j                  dd«      |«       y# t        $ r1}t        j	                  d|| j                  d«      |«       Y d}~yd}~ww xY w)zTBest-effort operator-visible trace in the job's output dir; never breaks the caller.rV   rÙ   Tz,Failed to write %s diagnostic for job %r: %sNF)Úsave_job_outputrX   r�   r‚   r  )rÔ   rà   rñ  r¯   s       r#   Ú_write_oneshot_diagnosticrž  i	  sO   € ðÜ˜Ÿ™  bÓ)¨4Ô0ØøÜò Ü�‰ÐCÀTÈ3Ï7É7ÐSWË=ÐZ[Ô\Üûðús   ‚ Ÿ	A¨'AÁAc                 ó  — | j                  d«      �y| j                  d«      xs i }| j                  d«      xs i }t        | d| j                  d«      › d| j                  d«      › d	|j                  d
d«      › d|j                  dd«      › d|j                  dd«      › d|j                  dd«      › dt        «       j                  «       › d�d«      }|r7t        j                  d| j                  d| j                  dd«      «      «       yy)uÁ  Trace for a wedged one-shot removal: dispatch was claimed but mark_job_run never ran
    (interrupted mid-run); removing it silently would leave no output, error, or record.

    A finite one-shot whose dispatch was claimed (``repeat.completed`` >= ``repeat.times``) but which never
    reached ``mark_job_run`` (``last_run_at`` is null) was interrupted mid-run â€” scheduler restart, gateway
    kill, or a non-Exception escape (#73973). The recovery guards remove such jobs so they stop appearing
    due, but a silent removal leaves the user with no output, no error, and no job record. Write a small
    diagnostic file into the job's output directory so the removal is observable and debuggable.
    rÍ  Nr>  rc  z7# Cron job removed without producing output

- job id: rV   ú	
- name: r  z
- dispatch claimed: r  r  rÜ   rý  z
- run claimed at: r‹  r  z by rÐ   z
- removed at: u   

This one-shot job's dispatch was claimed, but the run never completed (`last_run_at` was never written) â€” the scheduler process was most likely killed or restarted mid-execution. The job has been removed to stop it re-firing; recreate it to run again.
zwedged-oneshotuX   Job '%s': removed without a completed run â€” diagnostic written to its output directory)rX   rž  r²  r³  r‚   rƒ   )rÔ   r>  rÕ   Úwrittens       r#   Ú _write_wedged_oneshot_diagnosticr¢  s	  s  € ð ‡w�wˆ}ÓÐ)ØØ�W‰W�XÓÒ$ "€FØ�G‰G�KÓ Ò& B€EÜ'Øð	Ø—W‘W˜T“]�Oð $Ø—7‘7˜6“?Ð#ð $Ø%Ÿz™z¨+°sÓ;Ð<¸A¸f¿j¹jÈÐRUÓ>VÐ=Wð XØ"ŸY™Y t¨YÓ7Ð8¸¸U¿Y¹YÀtÈYÓ=WÐ<Xð YÜ$›×0Ñ0Ó2Ð3ð 4ð
	ð 	ó€Gñ Ü�‰ð#à�G‰G�F˜CŸG™G D¨#Ó.Ó/õ	1ð r7   r.  c                 ó®   — t        | d| j                  d«      › d| j                  d«      › d|› dt        › dt        «       j	                  «       › d�d	«       y
)zaTrace for a never-ran one-shot retired outside the grace window (else it would just vanish).
    zL# Cron job removed before firing (run time outside grace window)

- job id: rV   r   r  z
- scheduled run time: z
- grace window: zs
- removed at: a?  

This one-shot's run time is more than the grace window in the past (scheduler down past the window, host asleep, or jobs.json edited), which is outside the 'will never fire' contract enforced at create/update/resume time. The job was removed without running; recreate it (or use the Run button) to schedule it again.
zmissed-oneshotN)rž  rX   rÑ  r²  r³  )rÔ   r.  s     r#   Ú _write_missed_oneshot_diagnosticr¤  –	  sn   € ô Øð	Ø—W‘W˜T“]�Oð $Ø—7‘7˜6“?Ð#ð $!Ø!) 
ð +Ü0Ð1ð 2Ü$›×0Ñ0Ó2Ð3ð 4ð	ð 	õr7   c                 ót   ‡ — ˆ fd„}t        ‰ |t        ¬«      }|t        u rt        j                  d‰ «       y|S )u!  Atomically claim a finite one-shot dispatch BEFORE execution: ``repeat.completed`` is bumped
    and persisted under the jobs lock so a tick dying mid-execution cannot lose the dispatch
    (*at-most-times* instead of *at-least-once*). True if the caller may run the job; False when
    the limit is already reached. Only ``kind == "once"`` with ``repeat.times > 0`` is claimed.

    Increments ``repeat.completed`` under the cross-process jobs lock and persists the claim immediately, so
    that if the tick dies mid-execution (gateway kill, OOM, segfault, hard-timeout) the dispatch is not
    lost. This converts finite one-shot jobs from *at-least-once* to *at-most-times* semantics â€” a job that
    self-destructs fires at most ``repeat.times`` times instead of infinitely (issue #38758).
    c                 ó~  •— |j                  d«      xs i }|j                  d«      }|j                  di «      j                  d«      dk7  s|�|dk  ry|j                  dd«      }|j                  d	|j                  d
d«      «      }||k\  rƒ|j                  d«      �/t        |«       t        | «       t        j	                  d|||«       y| j                  |«       t        | ‰h¬«       t        |«       t        j	                  d|||«       y|dz   |d<   t        | «       t        j                  d||d   |«       y)Nr>  rý  rý   r  rD  r   Tr  r  rV   r  rÍ  u>   Job '%s': dispatch limit reached (%d/%d) â€” marking completedFr‡  u5   Job '%s': dispatch limit reached (%d/%d) â€” removingrŠ   z Job '%s': claimed dispatch %d/%d)rX   r­  rl  r‚   rM  rÀ   r¢  r  )ra  r–  rÔ   r>  rý  r  rï  rB   s          €r#   rD  zclaim_dispatch.<locals>.applyµ	  s+  ø€ Ø—‘˜Ó"Ò( bˆØ—
‘
˜7Ó#ˆà�7‰7�:˜rÓ"×&Ñ& vÓ.°&Ò8¸E¸MÈUÐVWÊZØØ—J‘J˜{¨AÓ.ˆ	Ø—‘˜ §¡¨¨cÓ 2Ó3ˆØ˜ÒØ�w‰w�}Ó%Ð1ô % SÔ)Ü˜$”Ü—‘ØTØ˜9 eô-ð ð �H‰H�QŒKä�d¨¨Õ1Ü,¨SÔ1Ü�K‰KØGÈÐPYÐ[`ôbàà'¨!™mˆˆ{ÑÜ�$ŒÜ�‰Ð7¸ÀÀ{Ñ@SÐUZÔ[Ør7   r–  u|   claim_dispatch: job_id %s not in store â€” proceeding without claim (handed-in job dict; nothing to persist a claim against)T)r¨  r—  r‚   r  )rB   rD  Úclaimeds   `  r#   Úclaim_dispatchr¨  ª	  s@   ø€ ô ôD ˜ ¬xÔ8€GØ”(ÑÜ�‰ðGàô	ð Ø€Nr7   c                 óš   — t        |t        «      r|j                  d«      |k7  ryt        «       j	                  «       |d<   t        | «       y)zYCompare-and-refresh a claim's ``at`` stamp; False unless *expected_owner* still holds it.rÐ   Fr‹  T)rÒ   rÓ   rX   r²  r³  rl  )ra  rÕ   rÌ   s      r#   Ú_refresh_claimrª  á	  s<   € ä�eœTÔ" e§i¡i°£o¸Ò&GØÜ“-×)Ñ)Ó+€Eˆ$�KÜˆd„OØr7   c                ó(   ‡— ˆfd„}t        | |d«      S )a†  Refresh a one-shot's ``run_claim`` timestamp while its run is alive, so an expired claim
    really means the claiming process died. Compare-and-refresh on ``expected_owner`` stops a stale
    runner from extending a claim another process has since taken over.

    Called periodically from the scheduler's run monitor (#62002) so a legitimately long run keeps its claim
    fresh: an expired claim then really does mean "the claiming process died", and neither another process's
    tick nor this process's own next tick will re-dispatch or stale-remove the job while the run is in
    flight. mark_job_run() clears the claim on completion.
    c                 ó†   •— |j                  di «      j                  d«      dk7  ryt        | |j                  d«      ‰«      S )Nrý   r  rD  Frc  )rX   rª  ©ra  re  rÔ   rÌ   s      €r#   rD  z"heartbeat_run_claim.<locals>.applyô	  s<   ø€ Ø�7‰7�:˜rÓ"×&Ñ& vÓ.°&Ò8ØÜ˜d C§G¡G¨KÓ$8¸.ÓIÐIr7   Fru  ©rB   rÌ   rD  s    ` r#   Úheartbeat_run_claimr¯  ê	  s   ø€ ôJô
 �V˜U EÓ*Ð*r7   c                 ó"   — d„ }t        | |d«      S )a]  Clear a one-shot's ``run_claim`` when dispatch itself fails: such a job never reaches
    mark_job_run, so the stale claim would block re-dispatch until the TTL expires.

    Calling this on every early-exit path restores the "the job stays due and will fire on the next healthy
    tick" invariant that the scheduler comment promises (#86522).
    c                 ó�   — |j                  di «      j                  d«      dk7  s|j                  d«      €yd |d<   t        | «       y)Nrý   r  rD  rc  FT)rX   rl  )ra  re  rÔ   s      r#   rD  zclear_run_claim.<locals>.apply
  sE   € Ø�7‰7�:˜rÓ"×&Ñ& vÓ.°&Ò8¸C¿G¹GÀKÓ<PÐ<XØØˆˆKÑÜ�$ŒØr7   Fru  )rB   rD  s     r#   Úclear_run_claimr²  ü	  s   € òô �V˜U EÓ*Ð*r7   c                 ó¶  — t        | «      }|syt        «       5  t        «       }t        «       j	                  «       }d}|D ]t  }|d   |vs9t        |«      rt        |«      r#|j                  di «      j                  d«      dvrŒDt        |d   |«      }|sŒV||j                  d«      k7  sŒk||d<   |dz  }Œv |rt        |«       |cddd«       S # 1 sw Y   yxY w)	a  Batch form of :func:`advance_next_run`: one load + at most one save for the whole due set;
    one-shot/unknown ids are skipped. Returns the count advanced. Persisted once at the end, so a
    crash mid-batch re-fires the whole set on restart rather than a prefix (sub-10ms window).r   rV   rý   r  >   r%   r  rª  rŠ   N)
rN   r°   r^   r²  r³  r  r  rX   r5  rl  )Újob_idsr  ra  r   ÚadvancedrÔ   Únew_nexts          r#   Úadvance_next_runsr·  
  sÛ   € ô ˆg‹,€CÙØÜ	‹ñ Ü‹{ˆÜ‹m×%Ñ%Ó'ˆØˆØò 
	ˆCà�D‘	 Ñ$Ü# CÔ(Ô1JÈ3Ô1OØ—7‘7˜: rÓ*×.Ñ.¨vÓ6Ð>RÑRàÜ'¨¨J©¸Ó=ˆHÚ˜H¨¯©°Ó(>Ó>Ø%-��MÑ"Ø˜A‘‘ð
	ñ Ü�dŒOØ÷#÷ ò ús   ™A<CÂCÂ+CÃCc                 ó    — t        | g«      dk\  S )u  Advance a recurring job's next_run_at BEFORE run_job() so a mid-run crash cannot re-fire it
    on restart (at-most-once for recurring jobs â€” one missed run beats a crash-loop burst).
    One-shots are left unchanged so they can retry. Returns True if next_run_at was advanced.rŠ   )r·  rz  s    r#   Úadvance_next_runr¹  (
  s   € ô
 ˜f˜XÓ&¨!Ñ+Ð+r7   c                  óÐ   — t        j                  dd«      j                  «       } | r| S 	 ddl}|j	                  «       }|› dt        j                  «       › �S # t
        $ r d}Y Œ&w xY w)u¢   Claim attribution/debugging id (NOT correctness â€” that comes from the file lock and the
    fresh-claim check): ``HERMES_MACHINE_ID`` if set, else hostname:pid.ÚHERMES_MACHINE_IDrÙ   r   Nr  rW  )r+  Úgetenvrr   rZ  r[  r�   Úgetpid)ÚexplicitrZ  Úhosts      r#   Ú_machine_idrÀ  0
  sk   € ô �y‰yÐ,¨bÓ1×7Ñ7Ó9€HÙØˆðÛØ×!Ñ!Ó#ˆð ˆV�1”R—Y‘Y“[�MÐ"Ð"øô ò ØŠðús   ªA ÁA%Á$A%)Úclaim_ttl_secondsÚforceÚmanualÚ
return_jobrÁ  rÂ  rÃ  rÄ  c                ó>   ‡ ‡‡‡‡‡— ˆˆˆˆfd„Št        ‰ ˆˆ fd„«      S )a8  Atomically claim a job for one external 'fire' (multi-machine at-most-once); True iff THIS
    caller won (``CronScheduler.fire_due``: exactly one of N replicas runs a job). Under the
    fence + file lock: reject missing/terminal/paused jobs unless ``force`` (explicit manual
    fire, which also resumes the job atomically; external callbacks must leave it false so a
    stale callback cannot resurrect a paused job). ``manual`` = off-tick run-now without the
    resume: no occurrence stamp, so the still-pending ``next_run_at`` slot is not skipped. Lose if a claim younger than
    ``claim_ttl_seconds`` exists (the TTL lets another fire reclaim after a crash; mark_job_run
    clears the claim). Otherwise stamp ``fire_claim`` and, for recurring jobs, advance
    ``next_run_at`` so a stale re-delivery cannot re-fire.c                 óö  •— t        |«      rt        |«      sy‰
st        |«      syt        «       }t	        |j                  d«      |‰	«      ryddlm}m} ‰
xs' ‰xs# |j                  d«      |j                  d«      k(  }|rd n ||j                  d«      «      }|�+t        j                  |«      |z
  t        t        ¬«      k\  rd }|r\ |||«      rS|j                  di «      j                  d	«      d
v r/t        |d   |j                  «       «      }|r||d<   t        | «       y‰
rt!        |«       |j                  «       t#        «       › dt%        j&                  «       j(                  › �dœ|d<   |j+                  dd «       |j                  di «      j                  d	«      d
v r$t        |d   |j                  «       «      }|r||d<   t        | «       ‰r t-        t/        j0                  |«      |¬«      S dS )NFrÏ   r   ©Úcompleted_occurrenceÚscheduled_instantrS  rª  rÏ  rý   r  >   r%   r  rW  ©r‹  rÐ   r@  )Ú_scheduled_instantT)r  r  r  r²  rê  rX   Úcron.occurrencesrÈ  rÉ  r   r±  r   r   r5  r³  rl  r±  rÀ  r¼   r  r¿   rÀ   rÓ   ÚcopyÚdeepcopy)ra  re  rÔ   r   rÈ  rÉ  Úmanual_fireÚinstantÚnxtrÁ  rÂ  rÃ  rÄ  s            €€€€r#   rD  z!claim_job_for_fire.<locals>.applyK
  s½  ø€ Ü˜3ÔÔ(AÀ#Ô(FØñ œ_¨SÔ1ØÜ‹mˆÜ˜#Ÿ'™' ,Ó/°Ð6GÔHØßLð
 Ò[˜vÒ[¨¯©°Ó)AÀSÇWÁWÈ]ÓE[Ñ)[ˆÙ%‘$Ñ+<¸S¿W¹WÀ]Ó=SÓ+Tˆð ÐÜ×*Ñ*¨7Ó3°cÑ9¼YÔOfÔ=gÒgØˆGÙÑ+¨C°Ô9Ø�w‰w�z 2Ó&×*Ñ*¨6Ó2Ð6JÑJÜ& s¨:¡¸¿¹»ÓH�ÙØ),�C˜Ñ&Ü˜d”OØÙÜ  Ô%ð $'§=¡=£?¼[»]¸OÈ1ÌTÏZÉZË\×M]ÑM]ÐL^Ð:_Ñ`ˆˆLÑà�‰� Ô%Ø�7‰7�:˜rÓ"×&Ñ& vÓ.Ð2FÑFÜ" 3 z¡?°C·M±M³OÓDˆCÙØ%(��MÑ"Ü�$ŒÙGQŒt”D—M‘M #Ó&¸7ÔCÐ[ÐW[Ð[r7   c                  ó   •— t        ‰‰ d«      S rD   ru  )rD  rB   s   €€r#   rÝ  z$claim_job_for_fire.<locals>.<lambda>€
  s   ø€ ¬Y°v¸uÀeÓ-L€ r7   rš  )rB   rÁ  rÂ  rÃ  rÄ  rD  s   `````@r#   Úclaim_job_for_firerÓ  >
  s   ý€ ÷3\ôj ˜VÔ%LÓMÐMr7   c                ó(   ‡— ˆfd„}t        | |d«      S )aÜ  Refresh an active ``fire_claim`` without extending another owner's lease: an execution may
    outlive the TTL, and the owner check stops a stale runner from refreshing a recovered claim.
    Deliberately NOT under ``_under_fire_fence``: the run thread holds the per-job fence across
    delivery, and the fence's reentrancy is per thread, so the heartbeat thread would time out and
    report a false ownership loss. ``_jobs_lock`` already serializes the compare-and-refresh.c                 ó<   •— t        | |j                  d«      ‰«      S )NrÏ   )rª  rX   r­  s      €r#   rD  z#heartbeat_fire_claim.<locals>.apply‰
  s   ø€ Ü˜d C§G¡G¨LÓ$9¸>ÓJÐJr7   Fru  r®  s    ` r#   Úheartbeat_fire_claimrÖ  ƒ
  s   ø€ ôKô �V˜U EÓ*Ð*r7   é   r  r>   Úcastc                 óÌ   — 	 ddl m}  |«       xs i }t        |t        «      r|j	                  di «      ni } ||j	                  | |«      «      S # t
        $ r  ||«      cY S w xY w)zTRead ``cron.<key>`` from config as *cast*, falling back to *default* on any failure.r   )Úload_configr%   )r!  rÚ  rÒ   rÓ   rX   r�   )r  r>   rØ  rÚ  rÃ  Úcron_cfgs         r#   Ú_cron_config_numberrÜ  ”
  s`   € ðÝ1Ù‹mÒ!˜rˆÜ*4°S¼$Ô*?�3—7‘7˜6 2Ô&ÀRˆÙ�H—L‘L  gÓ.Ó/Ð/øÜò Ù�G‹}Òðús   ‚AA ÁA#Á"A#c                  ó,   — t        dt        t        «      S )zZ``cron.completed_retention_days``; non-positive disables the sweep (records kept forever).Úcompleted_retention_days)rÜ  Ú COMPLETED_ONESHOT_RETENTION_DAYSrs   r4   r7   r#   Ú!_completed_oneshot_retention_daysrà  Ÿ
  s   € äÐ9Ô;[Ô]bÓcÐcr7   Úraw_jobsc                óØ  — t        «       }|dk  ry|t        |¬«      z
  }d}t        | «      D �]  }	 |j                  d«      dk7  rŒ|j                  d«      }t	        |t
        «      r|j                  d«      ndd	k7  rŒR|j                  d
«      }t	        |t        «      rt        |«      nd}	|	�|	|k\  rŒˆ| j                  |«       d}|j                  d«      }
|�|
r|j                  t        |
«      «       t        j                  d|j                  d|j                  dd«      «      ||«       �Œ |S # t        $ r, t        j                  d|j                  dd«      d¬«       Y �Œ;w xY w)a*  Prune completed one-shot records past retention (in place; True when anything was removed).
    Removed ids go into *removed_ids* so save_jobs's shrink-merge guard allows the delete. Age is
    measured from ``last_run_at``; a record without a parseable one is kept (never guess into
    deletion).r   F)r•  r	  r  rý   r  NrD  rÍ  TrV   zNJob '%s': pruning completed one-shot record (finished %s, retention %.1f days)r  r  z/Retention sweep skipped malformed job record %rr~   )rà  r   rå   rX   rÒ   rÓ   rJ   rÄ  Úremover  r‚   rM  r�   r  )rá  r   r}  Úretention_daysÚcutoffrE   Úrjrý   rì  rí  Úrids              r#   Ú_sweep_completed_oneshotsrè  ¤
  s^  € ô 7Ó8€NØ˜ÒØØ”9 .Ô1Ñ1€FØ€GÜ�8‹nó eˆð	eØ�v‰v�g‹ +Ò-ØØ—v‘v˜jÓ)ˆHÜ(2°8¼TÔ(B�—‘˜VÔ$ÈÐQWÒWØØ—v‘v˜mÓ,ˆHÜ4>¸xÌÔ4Mœ, xÔ0ÐSWˆKØÐ" k°VÒ&;ØØ�O‰O˜BÔØˆGØ—&‘&˜“,ˆCØÐ&©3Ø—‘¤ C£Ô)Ü�K‰KØ`Ø—‘�v˜rŸv™v d¨CÓ0Ó1°8¸^öMð!eð, €Nøô ò 	eÜ�L‰LØAÀ2Ç6Á6È$ÐPSÓCTÐ_cð ÷ eð	eús$   ²D4Á7D4Á?5D4Â5A:D4Ä41E)Å(E)c                  óV   — t        «       5  t        «       cddd«       S # 1 sw Y   yxY w)uù  Return all jobs due now. A recurring job more than one period stale (gateway down, or a run
    overran the interval) has its backlog collapsed â€” next_run_at fast-forwards so nothing
    burst-fires â€” but still fires ONCE now (via mark_job_run, consuming one ``repeat.times`` run),
    avoiding the perpetual-defer loop for runs longer than interval + grace.

    This prevents the perpetual-defer loop (#33315) where a job whose runtime exceeds ``interval + grace``
    would be skipped forever.
    N)r°   Ú_get_due_jobs_lockedr4   r7   r#   Úget_due_jobsrë  Ë
  s$   € ô 
‹ñ &Ü#Ó%÷&÷ &ò &ús   ‹
Ÿ(c                   óª   — e Zd ZU dZeeeef      ed<   e	ed<   dZ
eed<    ee¬«      Zee   ed<   ded	eeeef      fd
„Zdeded	dfd„Zded	dfd„Zy)Ú_DueScanzXMutable state threaded through one due scan: the raw store records plus what to persist.rá  r   FÚ
needs_save)Údefault_factoryrE   rB   r   c                 óB   ‡— t        ˆfd„| j                  D «       d «      S )Nc              3   ó4   •K  — | ]  }|d    ‰k(  sŒ|–— Œ y­wrU   r4   )rZ   ræ  rB   s     €r#   r\   z _DueScan.find.<locals>.<genexpr>â
  s   øè ø€ ÒF˜B°2°d±8¸vÓ3E”RÑFùr  )rÑ   rá  rF   s    `r#   Úfindz_DueScan.findá
  s   ø€ ÜÓF $§-¡-ÔFÈÓMÐMr7   ÚfieldsNc                 ó\   — | j                  |«      }|�|j                  |«       d| _        yy)zSWrite *fields* onto the raw record for *job_id* and flag a save (no-op if missing).NT)rò  r¬  rî  )rG   rB   ró  ræ  s       r#   Úpersistz_DueScan.persistä
  s-   € à�Y‰Y�vÓˆØˆ>Ø�I‰I�fÔØ"ˆD�Oð r7   c                 ó¸   — | j                  |«      }|�G| j                  j                  |«       | j                  j	                  t        |«      «       d| _        yy)z;Drop the raw record for *job_id* as an intentional removal.NT)rò  rá  rã  rE   r  rJ   rî  )rG   rB   ræ  s      r#   Úretirez_DueScan.retireë
  sI   € à�Y‰Y�vÓˆØˆ>Ø�M‰M× Ñ  Ô$Ø�L‰L×ÑœS ›[Ô)Ø"ˆD�Oð r7   )r8   r9   r:   rI   r   r   rJ   r   r;   r   rî  r!   r   rN   rE   r   r   rò  rõ  r÷  r4   r7   r#   rí  rí  Ø
  sŠ   … ábà�4˜˜S˜‘>Ñ"Ó"Ø	ƒMØ€J�ÓÙ¨cÔ2€GˆS�‰XÓ2ðN˜3ð N 8¨D°°c°©NÑ#;ó Nð#˜cð #¨Sð #°Tó #ð#˜Sð # Tô #r7   rí  c                 óp  — d}| D ]®  }|j                  d«      s:|j                  dd«      xs! t        j                  «       j                  dd |d<   d}t        |j                  d«      t        «      si |d<   d}dD ]6  }|j                  |«      }|€Œt        |«      �Œ#|j                  |d«       d}Œ8 Œ° |S )	a  Repair malformed store records in place BEFORE the due scan keys off them: a missing ``id``
    (older writers used ``job_id``), non-dict ``schedule``, or non-ISO timestamp used to abort the
    whole scan before save_jobs(), freezing the scheduler in a fast-forward loop.FrV   rB   Nr{  Trý   )rª  rÍ  )rX   rÀ   r¼   r  r¿   rÒ   rÓ   rÄ  )rá  Úchangedræ  r  rì   s        r#   Ú_normalize_due_scan_recordsrú  ô
  s»   € ð €GØò ˆØ�v‰v�dŒ|Ø—v‘v˜h¨Ó-ÒF´·±³×1AÑ1AÀ#À2Ð1FˆBˆt‰HØˆGÜ˜"Ÿ&™& Ó,¬dÔ3ØˆBˆz‰NØˆGØ1ò 	ˆCØ—F‘F˜3“KˆEØÑ ¤\°%Ó%8Ñ%@Ø—‘�s˜DÔ!Ø‘ñ		ðð €Nr7   Úscanc                 óX  — | j                  d«      }t        j                  d| j                  d|«      |«       |j                  |«      }|€y|j	                  dd¬«       |j                  d«      s|j
                  j                  «       |d<   |j                  d	«      sd
|d	<   d|_        y)a  Self-heal enabled=true with pause markers: the operator believes the job is frozen while the
    scheduler would still fire it. Force enabled=false so listings are honest; logged loudly since
    pause_job sets both fields atomically, so this should be rare.rV   zsJob '%s' (%s) has pause markers while enabled=true; self-disabling so it cannot fire (pause must be authoritative).r  NFr  )r  r	  r  r°  z+auto-disabled: enabled+paused contradictionT)rX   r‚   r¬   rò  r¬  r   r³  rî  )rÔ   rû  Újidræ  s       r#   Ú_self_disable_half_pausedrþ    s™   € ð �'‰'�$‹-€CÜ
‡L�Lð	Jà�‰�˜Ó˜cô#ð 
�‰�3‹€BØ	€zØØ‡I�I�e 8€IÔ,Ø�6‰6�+ÔØŸ(™(×,Ñ,Ó.ˆˆ;‰Ø�6‰6�/Ô"ØKˆˆ?ÑØ€D…Or7   c           
      ó®  — | j                  di «      }|j                  d«      }t        ||j                  | j                  d«      ¬«      }|rdnd}|s,|dv r(t        ||j                  j	                  «       «      }|r|}|sy|| d<   t
        j                  d	| j                  d
| j                  dd«      «      ||«       |j                  | d   |¬«       |S )zñRecompute and persist a missing ``next_run_at``; None when unrecoverable. One-shots use the
    grace window; recurring jobs only get here after a direct jobs.json edit bypassed add_job(),
    and would otherwise be silently skipped forever.rý   r  rÍ  rÌ  zone-shotN>   r%   r  rª  z4Job '%s' had no next_run_at; recovering %s run at %sr  rV   r  ©rª  )rX   rÓ  r   r5  r³  r‚   rM  rõ  )rÔ   rû  rý   r  Úrecovered_nextÚrecovery_kinds         r#   Ú_recover_missing_next_runr    sÏ   € ð �w‰w�z 2Ó&€HØ�<‰<˜Ó€DÜ0Ø�$—(‘(¨¯©°Ó(>ô@€Ná"0‘J°d€MÙ˜dÐ&:Ñ:Ü)¨(°D·H±H×4FÑ4FÓ4HÓIˆÙØ ˆMÙØØ'€CˆÑÜ
‡K�KØ>Ø�‰�˜Ÿ™  cÓ*Ó+¨]¸NôLð 	‡L�L��T‘¨€LÔ7ØÐr7   c                   ó²   — e Zd ZU dZeeef   ed<   eed<   eed<   e	ed<   e	ed<   e
deeef   fd„«       Ze
dee   fd	„«       Ze
defd
„«       Zdee   fd„Zy)Ú_DueJobzcOne candidate under evaluation: its record, schedule and the stored next_run in raw/aware
    form.rÔ   rû  r.  r  r  r   c                 ó:   — | j                   j                  di «      S )Nrý   ©rÔ   rX   ©rG   s    r#   rý   z_DueJob.schedule>  s   € à�x‰x�|‰|˜J¨Ó+Ð+r7   c                 ó8   — | j                   j                  d«      S )Nr  )rý   rX   r  s    r#   r  z_DueJob.kindB  s   € à�}‰}× Ñ  Ó(Ð(r7   c                 ón   — | j                   j                  d| j                   j                  dd«      «      S )Nr  rV   r  r  r  s    r#   rï  z_DueJob.labelF  s&   € à�x‰x�|‰|˜F D§H¡H§L¡L°°sÓ$;Ó<Ð<r7   c                 ór   — t        | j                  | j                  j                  j	                  «       «      S rõ   )r5  rý   rû  r   r³  r  s    r#   Úrecompute_nextz_DueJob.recompute_nextJ  s$   € Ü §¡¨t¯y©y¯}©}×/FÑ/FÓ/HÓIÐIr7   N)r8   r9   r:   rI   r   rJ   r   r;   rí  r   Úpropertyrý   r   r  rï  r  r4   r7   r#   r  r  3  sš   … ñð 
ˆc�3ˆh‰ÓØ
ƒNØƒMØÓØÓàð,˜$˜s C˜x™.ò ,ó ð,ð ð)�h˜s‘mò )ó ð)ð ð=�sò =ó ð=ðJ ¨¡ô Jr7   r  rM  c                 ó´  — | j                   j                  }| j                  |k  r,t        | j                  |«      rt        | j                  |«      sy| j                  «       }|syt        j                  d| j                  | j                  j                  «       |j                  «       |«       | j                   j                  | j                  d   |¬«       y)aÔ  Repair a cron job whose stored offset no longer matches now's (TZ migration).

    next_run_at is an absolute instant but the expr means local wall clock, so a TZ change can make
    it look due hours early. If the stored wall clock is still in the future, recompute so we fire
    at the intended local time. True when re-anchored (caller skips this tick). TRADE-OFF: a DST
    offset change meeting the same conditions SKIPS the pending occurrence; accepted as rare.FzlJob '%s' next_run_at offset changed (%s -> %s). Recomputing cron run to preserve local wall-clock intent: %srV   r   T)rû  r   r  rÉ  r  rË  r  r‚   rM  rï  rÇ  rõ  rÔ   )rM  r   r¶  s      r#   Ú_repair_timezone_shifted_cronr  N  sª   € ð �&‰&�*‰*€Cà	�‰˜ÒÜ% a×&7Ñ&7¸Ô=Ü(¨×):Ñ):¸CÔ@àØ×ÑÓ!€HÙØÜ
‡K�Kð	Gà	�‰�×"Ñ"×,Ñ,Ó.°·±³ÀôKð ‡F�F‡N�N�1—5‘5˜‘;¨H€NÔ5Ør7   c                 ó¸  — | j                   j                  }| j                  dv r0| j                  |kD  r!t	        | j
                  | j                  |«      s| j                  S | j                  dk(  r|j                  «       }|}n| j                  «       }|rt        |«      nd}|r|�|| j                  k  s| j                  S | j
                  j                  d«      }t        j                  d| j
                  j                  d|«      ||«       t        | j
                  | j                  «       || j
                  d<   | j                   j                  ||¬«       |S )	aÞ  Re-arm a recurring job wedged in persisted last_status=error; returns the effective
    next_run_dt.

    Such a job errored, mark_job_run parked next_run_at in the future, and nothing re-dispatched it
    (the in-memory stale-claim sweep cannot see it). Interval jobs re-arm to now (always a legal
    fire); cron jobs re-arm to the next LEGAL occurrence, since re-arming to now would fire at times
    the expression excludes. A correctly-parked cron value is left as-is.
    )r%   r  r  NrV   uÐ   cron.persisted_error.recovered job='%s' id=%s â€” recurring job wedged in stale last_status=error without re-firing for a full cadence; re-arming next_run_at to %s so it re-dispatches without force-run/resumer  rª  r   )rû  r   r  r  rñ  rÔ   rý   r³  r  rÄ  rX   r‚   rƒ   r  r.  rõ  )rM  r   r  Úrecovered_next_dtrý  s        r#   Ú_rearm_stale_error_recurringr  g  s  € ð �&‰&�*‰*€Cà	�‰Ð&Ñ&Ø�M‰M˜CÒÜ)¨!¯%©%°·±¸SÔAà�}‰}ÐØ‡v�v�ÒØŸ™›ˆØ03Ñà×)Ñ)Ó+ˆÙ<JœL¨Ô8ÐPTÐÙÐ0Ð<ÐARÐUV×UbÑUbÒAbØ�}‰}ÐØ
�%‰%�)‰)�D‹/€CÜ
‡N�Nð	cð 	
�‰�	‰	�&˜#Ó  ^ô	5ô
 % Q§U¡U¨A¯J©JÔ7Ø)€A‡E�Eˆ-ÑØ‡F�F‡N�N�3 N€NÔ3ØÐr7   c                 óB  — t        | j                  | j                  | j                  «      }|t        k(  rƒ| j                  «       }t        j                  d| j                  | j                  | j                  j                  d«      |«       |r*| j                  j                  | j                  d   |¬«       y|t        k(  rßt        j                  d| j                  | j                  j                  d«      | j                  j                  d«      | j                  | j                  j!                  «       | j                  j#                  «       | j                  j$                  j#                  «       «       t'        | j                  | j                  | j                  «       y)u  Stale-schedule guard for a due cron instant; True when re-anchored without firing.

    A direct edit of schedule.expr leaves next_run_at on the old lattice, so re-anchor first (from
    the current expr, so this converges). An offset-representation migration also moves a legacy
    instant off the lattice, and re-anchoring THAT swallowed a due occurrence â€” so classify, and
    let
    the migration case fall through to fire ONCE (at-most-once holds: nothing re-reads the legacy
    instant after advance/mark_job_run rewrites it).z‚Job '%s' next_run_at %s does not match its current cron expression %r (direct jobs.json edit?); re-anchoring to %s without firing.rÿ   rV   r   Tuû   cron.timezone_migration.catch_up job='%s' id=%s expr=%r stored=%s normalized=%s â€” stored next_run_at carries a pre-migration UTC offset (%s, now %s) and is a legal occurrence at its own wall clock; firing the due run instead of re-anchoring past it.F)r  rý   r  r  r  r  r‚   rM  rï  r.  rX   rû  rõ  rÔ   r  rƒ   r³  rÇ  r   r%  )rM  Ústale_classr¶  s      r#   Ú_reanchor_stale_cronr  ‹  s'  € ô 0°·
±
¸A×<MÑ<MÈqÏ}É}Ó]€KØÔ*Ò*Ø×#Ñ#Ó%ˆÜ�‰ð^à�G‰G�Q—Z‘Z §¡§¡°Ó!7¸ô	Cñ Ø�F‰F�N‰N˜1Ÿ5™5 ™;°HˆNÔ=ØØÔ3Ò3Ü�‰ðdð �G‰G�Q—U‘U—Y‘Y˜t“_ a§j¡j§n¡n°VÓ&<¸a¿j¹jØ�M‰M×#Ñ#Ó% q×'8Ñ'8×'BÑ'BÓ'DÀaÇfÁfÇjÁj×FZÑFZÓF\ô	^ô 	+¨1¯5©5°!×2CÑ2CÀQÇ]Á]ÔSØr7   rï  c                 ó8  — | j                   j                  | j                  z
  j                  «       |k  ry| j	                  «       }|sy| j                   j                  | j                  d   |¬«       t        t        j                  |«      «      | j                   j                  kD  r<t        ddd„ «      s.t        j                  d| j                  | j                  ||«       yt        j                  d| j                  | j                  ||«       t        «        y)	u„  Re-anchor accumulated misses; return whether catch-up was explicitly disabled.

    The fast-forward is persisted immediately â€” NOT redundant with advance_next_run/mark_job_run:
    it
    protects the crash window before mark_job_run and covers the external fire_due path, which never
    calls advance_next_run. mark_job_run re-anchors on completion, so the value is provisional.
    FrV   r   Úcatch_up_missedTc                 ó
   — | duS rD   r4   rÃ  s    r#   rÝ  z0_fast_forward_missed_recurring.<locals>.<lambda>¹  s   € ÈuÐ\aÐOa€ r7   z‚Job '%s' missed its scheduled time (%s, grace=%ds). Skipping missed occurrence because cron.catch_up_missed is false; next run: %sz~Job '%s' missed its scheduled time (%s, grace=%ds). Running now; next run provisionally set to: %s (re-anchored on completion))rû  r   r  rë  r  rõ  rÔ   rÁ  r   r±  rÜ  r‚   rM  rï  r.  rL  )rM  rï  r¶  s      r#   Ú_fast_forward_missed_recurringr  ª  sÝ   € ð 	
�‰�
‰
�Q—]‘]Ñ"×1Ñ1Ó3°uÒ<ØØ×ÑÓ!€HÙØØ‡F�F‡N�N�1—5‘5˜‘;¨H€NÔ5Ü”h×,Ñ,¨XÓ6Ó7¸!¿&¹&¿*¹*ÒDÜ'Ð(9¸4ÑAaÔbÜ�‰ð]à�G‰G�Q—Z‘Z ¨ô	2ð Ü
‡K�Kð	Uà	�‰�—‘˜U Hô.ô Ô Ør7   c                 ór  — | j                   j                  | j                  z
  j                  «       t        k  ry| j
                  j                  d«      sc| j
                  j                  d«      sHt        | j
                  | j                  «       | j                   j                  | j
                  d   «       y)uÀ  One-shot grace gate; True when the job must not fire this tick.

    A one-shot beyond the grace window must never fire (create/update/resume reject such schedules
    and recovery never revives them; only the due scan used to dispatch them hours late). With no
    claim stamped, retire it with a diagnostic (never silently delete). A claim may mean a run is
    still in flight elsewhere â€” skip but keep the record so its mark_job_run can land.Frc  rÏ   rV   T)
rû  r   r  rë  rÑ  rÔ   rX   r¤  r.  r÷  )rM  s    r#   Ú_retire_expired_oneshotr  Ç  sv   € ð 	
�‰�
‰
�Q—]‘]Ñ"×1Ñ1Ó3Ô7LÒLØØ�E‰E�I‰I�kÔ" a§e¡e§i¡i°Ô&=Ü(¨¯©°·
±
Ô;Ø	�‰�‰�a—e‘e˜D‘kÔ"Ør7   c           	      ó  — | j                  d«      xs i }|j                  d«      }|j                  dd«      }|�
|dk  s||k  ry| j                  d| j                  dd«      «      }t        | j                  dd	«      «      rt        j                  d
|||«       y| j                  d«      �)t        j	                  d|||| j                  d«      «       nt        j                  d|||«       |j                  | d   «       t        | «       y)u\  One-shot dispatch-limit guard; True when the job must not fire this tick.

    A finite one-shot claimed via claim_dispatch() whose tick died before mark_job_run has
    completed >= times while still looking due. Remove it instead of re-firing â€” unless THIS
    process is still running it (a run outliving the run_claim TTL is slow, not stale).r>  rý  r  r   Fr  rV   r  rÙ   ui   Job '%s': dispatch limit reached (%d/%d) but its run is still in flight in this process â€” keeping entryTrÍ  u  Job '%s': one-shot dispatch limit reached (%d/%d) on a record that already completed a run (last_run_at=%s) â€” removing it WITHOUT firing. This record was re-armed without a budget reset (pre-#93615 store or hand edit); re-run it with 'hermes cron resume <job> --run-now' (#93524).uN   Job '%s': one-shot dispatch limit reached (%d/%d) â€” removing stale due entry)rX   r„   r‚   rM  rƒ   r÷  r¢  )rÔ   rû  r>  rý  r  r  s         r#   Ú_oneshot_dispatch_limit_reachedr  Ö  s  € ð �W‰W�XÓÒ$ "€FØ�J‰J�wÓ€EØ—
‘
˜;¨Ó*€IØ€}˜ š
 i°%Ò&7ØØ�7‰7�6˜3Ÿ7™7 4¨Ó-Ó.€Dô $ C§G¡G¨D°"Ó$5Ô6Ü�‰ð(à�)˜Uô	$ð Ø
‡w�wˆ}ÓÐ)ô 	�‰ð=ð �)˜U C§G¡G¨MÓ$:õ	<ô 	�‰Ø\Ø�)˜Uô	$ð 	‡K�K��D‘	Ôä$ SÔ)Ør7   c           	      ó”  — ddl m}  || |j                  «      }|€yt        j	                  d| j                  d| j                  d«      «      | j                  d«      || j                  d«      «       || d<   | j                  dd«       |j                  | d   «      }|�|j                  dd«       |j                  | d   |¬	«       |S )
uf  Put an occurrence the dispatcher advanced past but never claimed back on the schedule
    (#107485); returns the restored ``next_run_at`` or None. Restored ONCE: the stamp is dropped
    here, so the slot then meets the ordinary late / fast-forward / ``cron.catch_up_missed``
    policy like any other overdue instant â€” never a replay of every missed slot.r   )Úunclaimed_pending_slotNz˜Job '%s' (%s): occurrence %s was taken off the schedule but never claimed (scheduler stopped before dispatch); restoring it as the due instant (was %s).r  rV   rª  r@  r   )	rÌ  r  r   r‚   rƒ   rX   rÀ   rò  rõ  )rÔ   rû  r  Úslotræ  s        r#   Ú_restore_unclaimed_slotr!     s·   € õ
 8á! # t§x¡xÓ0€DØ€|ØÜ
‡N�Nð	Yà�‰�˜Ÿ™ ›Ó&¨¯©°«°t¸S¿W¹WÀ]Ó=SôUð €CˆÑØ‡G�GˆN˜DÔ!Ø	�‰�3�t‘9Ó	€BØ	€~Ø
�‰ˆ~˜tÔ$Ø‡L�L��T‘¨€LÔ-Ø€Kr7   Úrun_claim_ttlc           	      óÔ  — |j                   }| j                  di «      j                  d«      dk(  rt        | j                  d«      ||«      ryt        | |«      xs | j                  d«      xs t	        | |«      }|syt        j                  |«      }t        | |||t        |«      «      }|j                  }|dv }| j                  d«      |k(  }	d	d
l
m}
m} |	s6 |
| |«      r-|r|j                  «       nd}|r|j                  | d   |¬«       y|dk(  r|	st        |«      ryt!        |«      |_        |j"                  |kD  ry|	rdn || j                  d«      «      | d<   |	s|dk(  rt%        |«      ryt'        |j(                  «      }|	s|rt+        ||«      ry|dk(  rNt-        |«      st/        | |«      ry|j1                  «       t3        «       dœ}|| d<   |j                  | d   |¬«       |	s{|ryt5        d||j"                  z
  j7                  «       «      }||j1                  «       t9        |d«      t;        ||«      dœ}|| d<   d	dl
m} |j                  | d   | |||«      ¬«       y)ah  Decide whether one enabled, non-terminal job fires this tick, persisting any repairs.
    Ordering matters: recover missing next_run_at, repair timezone shifts, re-arm stale-error
    recurring jobs; then once due: re-anchor stale cron instants, fast-forward missed recurring
    runs, retire/guard one-shots, and finally stamp the run claim / dispatch record.rý   r  rD  rc  Frª  >   r%   r  rS  r   rÇ  NrV   r   r%   rË  rÊ  )rc  r@  rŠ   )Úscheduled_atÚdispatched_atrÛ  r  Úlast_dispatch)Úpending_slot_stamp)r&  r@  T)r   rX   rê  r!  r  r   r±  r  rÁ  r  rÌ  rÈ  rÉ  r  rõ  r  r  r  r  rÚ  rý   r  r  r  r³  rÀ  rx   rë  Úroundrâ  r'  )rÔ   rû  r"  r   r.  r  rM  r  Ú	recurringÚ
manual_runrÈ  rÉ  r¶  rï  rÕ   ÚlatenessÚdispatch_stampr'  s                     r#   Ú_evaluate_due_jobr-    sZ  € ð
 �(‰(€Cð
 	�‰�
˜BÓ×#Ñ# FÓ+¨vÒ5Ü˜3Ÿ7™7 ;Ó/°°mÔDàä& s¨DÓ1Òs°S·W±W¸]Ó5KÒsÔOhÐilÐnrÓOs€HÙØÜ×,Ñ,¨XÓ6€OÜ��T˜8 _´mÀOÓ6TÓU€AØ�6‰6€DØÐ,Ð,€Ið —‘˜Ó)¨XÑ5€JßHáÑ.¨s°HÔ=Ù)2�1×#Ñ#Ô%¸ˆÙØ�L‰L˜˜T™°ˆLÔ9ØØˆv‚~™jÔ-JÈ1Ô-MØÜ0°Ó3€A„MØ‡}�}�sÒØñ )3¡Ñ8IÈ#Ï'É'ÐR_ÓJ`Ó8a€CÐÑá˜$ &š.Ô-AÀ!Ô-DØÜ" 1§:¡:Ó.€EÙ™)Ô(FÀqÈ%Ô(PØØˆv‚~Ü" 1Ô%Ô)HÈÈdÔ)SØð
 —}‘}“¬k«mÑ<ˆØ ˆˆKÑØ�‰�S˜‘Y¨%ˆÔ0ñ
 ™)Ü�s˜S 1§=¡=Ñ0×?Ñ?ÓAÓBˆð %Ø Ÿ]™]›_Ü % h°Ó 2Ü/°¸%Ó@ñ	
ˆð  .ˆˆOÑõ 	8à�‰Ø�‰I ^Ù+¨H°cÓ:ð 	ô 	<ð r7   c                  óJ  — t        «       } t        | t        «       «      }t        | «      |_        t        j                  | «      D �cg c]  }t        |«      ‘Œ }}t        «       }t        | |j                  |j                  ¬«      r7d|_        |D �cg c]%  }|j                  |j                  d«      «      €Œ$|‘Œ' }}g }|D ]c  }	 t        |«      rt        |«      sŒ|j                  dd«      sŒ.t!        |«      rt#        ||«       ŒFt%        |||«      r|j'                  |«       Œe |j                  rt/        | |j                  xs d¬«       |S c c}w c c}w # t(        $ r? t*        j-                  d|j                  d«      xs |j                  d«      xs d«       Y Œàw xY w)	zLInner implementation of get_due_jobs(); must be called with _jobs_lock held.r‡  TrV   Nr  z.Skipping malformed cron job %r during due scanr  r  )r^   rí  r²  rú  rî  rÍ  rÎ  rë   r{   rè  r   rE   rò  rX   r  r  r  rþ  r-  ræ   r�   r‚   Ú	exceptionrl  )rá  rû  r�  ra  r"  ÚduerÔ   s          r#   rê  rê  h  si  € ä‹{€HÜ�Hœk›mÓ,€DÜ1°(Ó;€D„OÜ,0¯M©M¸(Ó,CÖD qÔ Õ"ÐD€DÐDä2Ó4€Mô ! ¨4¯8©8ÀÇÁÕNØˆŒØÖF�a 4§9¡9¨Q¯U©U°4«[Ó#9Ñ#E’ÐFˆÐFà
€CØò 9ˆð	9Ü˜sÔ#Ô,EÀcÔ,JØØ—7‘7˜9 dÔ+ØÜ  Ô%Ü)¨#¨tÔ4ØÜ   d¨MÔ:Ø—
‘
˜3”øð9ð& ‡‚Ü�(¨¯©Ò(<¸Õ=Ø€Jùò? Eùò Gøô" ò 	9Ü×ÑØ@Ø—‘˜“Ò7 3§7¡7¨4£=Ò7°Cö9ð	9ús7   ÁEÂ%EÂ7EÃEÃEÃ/EÄEÅAF"Æ!F"r  c                  ó,   — t        dt        t        «      S )z>Per-job output-file retention cap (``cron.output_retention``).Úoutput_retention)rÜ  Ú_CRON_OUTPUT_DEFAULT_KEEPrH  r4   r7   r#   Ú_cron_output_keepr4  ™  s   € äÐ1Ô3LÌcÓRÐRr7   ro  Úkeepc                 ó.  — |dk  ry	 t        d„ | j                  d«      D «       d„ d¬«      }d}||d D ]  }	 |j                  «        |dz  }Œ |S # t        $ r Y yw xY w# t        $ r+}t        j                  d	|j                  |«       Y d}~Œ[d}~ww xY w)
a  Remove the oldest ``*.md`` run-output files beyond *keep*; returns count deleted. Filenames
    are timestamps, so a reverse lexical sort is newest-first. Non-positive *keep* disables pruning;
    failures are swallowed so they can never break output saving.r   c              3   óB   K  — | ]  }|j                  «       sŒ|–— Œ y ­wrõ   )Úis_file)rZ   r^  s     r#   r\   z$_prune_job_output.<locals>.<genexpr>¦  s   è ø€ ÒC�1°q·y±yµ{ŒQÑCùs   ‚˜z*.mdc                 ó   — | j                   S rõ   )r  )r^  s    r#   rÝ  z#_prune_job_output.<locals>.<lambda>§  s
   € ˜!Ÿ&™&€ r7   T)r  ÚreverseNrŠ   z"Failed to prune cron output %s: %s)rF  Úglobr‘   rR  r‚   r  r  )ro  r5  ÚfilesÚdeletedÚstalerÇ   s         r#   Ú_prune_job_outputr?  ž  s±   € ð ˆq‚yØðÜÙC˜×+Ñ+¨FÓ3ÔCÙ ¨$ô0ˆð
 €GØ�t�u�ò Pˆð	PØ�L‰LŒNØ�q‰L‰GðPð €Nøô ò Ùðûô ò 	PÜ�L‰LÐ=¸u¿z¹zÈ3×OÑOûð	Pús(   ˆ%A ¸A Á	AÁAÁ 	BÁ)!BÂBc                 óö   — t        «        t        | «      }t        |«       t        |«       |t	        «       j                  d«      › d�z  }t        ||dd¬«       t        |«       t        |t        «       «       |S )zSave job output to file.z%Y-%m-%d_%H-%M-%Sz.mdz.output_r8  r9  )
r¨   rá   r;  r   r²  r´  r   r$  r?  r4  )rB   r+   ro  Úoutput_files       r#   r�  r�  ´  sl   € ä„MÜ$ VÓ,€NÜ�^Ô$Ü�ÔØ ¤k£m×&<Ñ&<Ð=PÓ&QÐ%RÐRUÐ#VÑV€KÜ�k 6°jÀuÕMÜ�Ôä�nÔ&7Ó&9Ô:ØÐr7   ry   c                 óô   — t        | xs d«      j                  «       }|sy	 ddlm}  ||«      xs |}|j                  «       j                  d«      S # t        $ r t
        j                  d| d¬«       Y ŒBw xY w)u&  Reduce a job skill reference (possibly an absolute path) to the bare name the curator matches
    on, resolving as the scheduler does â€” otherwise a path-referencing job's skill looks
    unreferenced and gets archived. Falls back to plain cleanup so a broken import can never lose
    a name.rÙ   r   )Únormalize_skill_lookup_namez8referenced_skill_names: could not normalize skill ref %rTr~   rÜ   )rJ   rr   Úagent.skill_utilsrC  r�   r‚   r  Úlstrip)ry   rì   rC  s      r#   Ú_canonical_skill_refrF  Ä  sy   € ô
 �’	�r‹N× Ñ Ó"€EÙØðeÝAÙ+¨EÓ2Ò;°eˆð �;‰;‹=×Ñ Ó$Ð$øô ò eÜ�‰ÐOÐQTÐ_cˆÖdðeús   ¢A Á!A7Á6A7c            
      ó@  — 	 t        «       } | D ��ch c]P  }t        |t        «      r>t        |j                  d«      |j                  d«      «      D ]  }t        |«      x}r|’Œ ŒR c}}S # t        $ r$ t        j                  dd¬«       t	        «       cY S w xY wc c}}w )a   Skill names referenced by ANY cron job, deliberately including paused/disabled ones (resuming
    must still find them); the curator protects these from inactivity archival. Canonicalized as the
    scheduler does, so absolute paths are protected too. A corrupt store yields an empty set.z0referenced_skill_names: failed to load cron jobsTr~   râ   rã   )
r^   r�   r‚   r  rN   rÒ   rÓ   ré   rX   rF  )ra  rÔ   r  Úcleaneds       r#   Úreferenced_skill_namesrI  Ô  s¡   € ðÜ‹{ˆð ÷àÜ�cœ4Ô Ü)¨#¯'©'°'Ó*:¸C¿G¹GÀHÓ<MÓNò	ð Ü+¨DÓ1Ð1ˆGÐ1ò	 	ðØóð øô ò Ü�‰ÐGÐRVˆÔWÜ‹uŠðüós   ‚
A* ‘ABÁ**BÂBÚconsolidatedÚprunedc                 óv  — t        | xs i «      } t        |xs g «      t        | j                  «       «      z
  }| s|sg dddœS t        «       5  t	        «       }g }|D �]  }t        |j                  d«      |j                  d«      «      }|sŒ1i }g }g }	|D ]V  }
|
| v r$| |
   }|||
<   |sŒ||	vsŒ|	j                  |«       Œ+|
|v r|j                  |
«       ŒA|
|	vsŒF|	j                  |
«       ŒX |s|sŒ—|	|d<   |	r|	d   nd|d<   |j                  |j                  d«      |j                  d«      xs |j                  d«      t        |«      t        |	«      ||dœ«       �Œ |r*t        |«       t        j                  d	t        |«      «       |t        |«      t        |«      dœcddd«       S # 1 sw Y   yxY w)
aâ  Rewrite cron job skill references after a curator consolidation pass (a job listing a
    consolidated/pruned skill would otherwise run without it). Consolidated names map to their
    umbrella target without duplication, pruned names are dropped, ordering is preserved, and the
    legacy ``skill`` field is realigned. Returns ``{"rewrites": [{job_id, job_name, before,
    after, mapped, dropped}, ...], "jobs_updated": N, "jobs_scanned": M}``. Load/save exceptions
    propagate.r   )ÚrewritesÚjobs_updatedÚjobs_scannedrâ   rã   NrV   r  )rB   Újob_namer&  Úafterr˜  Údroppedz2Curator rewrote skill references in %d cron job(s))rÓ   rN   Úkeysr°   r^   ré   rX   ræ   rå   rl  r‚   rM  rŽ  )rJ  rK  Ú
pruned_setra  rM  rÔ   Úskills_beforer˜  rR  Ú
new_skillsr  Útargets               r#   Úrewrite_skill_refsrX  æ  sÂ  € ô ˜Ò*¨Ó+€Lä�V’\˜rÓ"¤S¨×):Ñ):Ó)<Ó%=Ñ=€JÙ¡
Ø°À1ÑEÐEä	‹ñ #`Ü‹{ˆØ)+ˆØó 	ˆCÜ1°#·'±'¸'Ó2BÀCÇGÁGÈHÓDUÓVˆMÙ ØØ%'ˆFØ!#ˆGØ$&ˆJØ%ò 	,�Ø˜<Ñ'Ø)¨$Ñ/�FØ#)�F˜4‘LÚ &°
Ò":Ø"×)Ñ)¨&Õ1Ø˜ZÑ'Ø—N‘N 4Õ(Ø Ò+Ø×%Ñ% dÕ+ð	,ñ ¡'ØØ&ˆC�‰MÙ,6˜: aš=¸DˆC�‰LØ�O‰OØŸ'™' $›-ØŸG™G F›OÒ<¨s¯w©w°t«}Ü˜}Ó-Ü˜jÓ)Ø Ø"ñö ð+	ñ: Ü�dŒOÜ�K‰KÐLÌcÐRZËmÔ\Ø$´c¸(³mÔUXÐY]ÓU^Ñ_÷G#`÷ #`ò #`ús    ÁAF/Â)F/Â.,F/ÃC
F/Æ/F8c                 ó   — t        | dd«       y)z>Clear the drift alert-dedup marker (resolution matches again).Údrift_alertedFNry  rz  s    r#   Úclear_drift_alertedr[     s   € ä�F˜O¨UÕ3r7   )r   Nr»  )rÙ   rõ   )F)NNNNNNNNNNNNNFNNNNNFNF)NNN(  rI   r­   rÍ  Úcontextvarsr   Údataclassesr   r   r  Úloggingrj  r”  r¶   rŒ   r+  rQ  r¼   r‹   r    r”   r   r   r   Úpathlibr	   rÙ  r
   Úcron.constantsr   r   r   Úcron.env_settingsr   Útypingr   r   r   r   r   r   r   r   r   Ú	getLoggerr8   r‚   Úhermes_timer   r²  r   Úutilsr   r   r   r   r!   r;   r$   re   rf   ra   rb   ÚTICKER_HEARTBEAT_FILEÚTICKER_SUCCESS_FILEÚTICKER_INTERVAL_SECONDSr·   r¦   rù  r¥   r*   rJ   ÚLockr´   r¹   r«   rc   rÑ  r/   r?   rA   rL   ÚcontextmanagerrR   r_   rd   rj   rm   ro   rw   rt   rs   r{   r„   r†   r™   rž   r°   rÈ   rË   r×   Ú	frozensetrE  rá   ré   rë   rï   rB  r  ré  rú   r  r  r  r  r
  r  r  r   r$  Ústat_resultr0  r6  r;  r¨   rH  rJ  rT  rU  r�  r�  rÓ  r†  r›  r   r¥  r¾  rÁ  rÄ  rÉ  rË  rÓ  rÖ  rØ  rÓ   rÚ  rá  râ  rã  r   rå  rå   rñ  rò  rÕ  r  r  r  r  r  r  r  r  r  r   r%  r(  r5  r;  r>  rB  rE  rG  rJ  rL  rP  rS  rW  r_  r^   rt  re  rg  r†  r‰  r‹  r�  r   r¤  rl  Úobjectr—  r¨  r­  r±  r·  rÅ  rÀ  rË  rÎ  rÐ  rÔ  rÚ  rä  rç  rê  ru   rí  ró  r  r  ÚLookupErrorr  r   r'  r*  r-  r0  r5  r8  r;  rH  rK  rP  rU  r]  rê  rd  rg  rq  rv  r{  r}  r‚  rˆ  r‹  r›  rž  r¢  r¤  r¨  rª  r¯  r²  r·  r¹  rÀ  rÓ  rÖ  rß  rÜ  rà  rè  rë  rí  rú  rþ  r  r  r  r  r  r  r  r  r!  r-  rê  r3  r4  r?  r�  rF  rI  rX  r[  r4   r7   r#   ú<module>ro     so  ðò1ó Û Ý "ß (Û Û Û Û Û Û Û 	Û 	Û ðÛðÛ÷ 3Ñ 2Ý Ý ,ß iÑ iÝ .ß U× UÕ Uà	ˆ×	Ñ	˜8Ó	$€å *Ý $ß 3ð €Ø#€ˆh�t‰nÓ #ð
˜$ó 
ñ. Ó×&Ñ&Ó(€
ð ˜Ñ€Ø�{Ñ"€	ð !Ð#5Ñ5Ð ØÐ!6Ñ6Ð ð Ð ð "�)—/‘/Ó#€Ø"�9—?‘?Ó$Ð Ø02Ð �4˜˜YŸ_™_Ð,Ñ-Ó 2Ø(˜)Ÿ.™.Ó*Ð Ø(˜Ÿ™Ó*Ð ð
 "Ð Ø˜Ñ €
ØÐ ñ �$Ô÷Jð Jó ðJñ ?IØ 4ô?)Ð �j ¨/Ñ!:Ñ;ó )÷ñ ñ FPØ TôF+Ð ˜
 8Ð,@Ñ#AÑBó +ð ×Ñð,¨ò ,ó ð,ðA¨#ð A°$ó Añ   ¨)°ZÓ@€ð2˜_ó 2ð" ×Ñð*˜˜s D˜yÑ)ò *ó ð*ð,˜Tó ,ð !%Ð ð
 $)Ð  ð
^¨ó 
^ð¨ð °ó ð09˜ó 9ð
 Uð ¨x¸©~ó ô*
ð ×Ññ2/ó ð2/ðj ×Ñð2˜3ò 2ó ð2ðj˜cð  x°°C°Ñ'8ð ¸Só ð ×Ñð˜Sð °Sò ó ðñ$ " 4 &Ó)Ð ð	3˜Cð 	3 Dó 	3ò ¨#¡ð ¸xÈ¹}ð ÐX\Ð]`ÑXaó ð ˜T # s (™^ð °°S¸#°X±ó ò5˜Cð 5¨3ð 5¸ó 5ñ ÒOÓP€ðKð ð+ð ðJ˜d 3¨ 8™nð J°ó Jð 4¨¨S¨¡>ð °có ð˜t C¨ H™~ð °$°s¸C°x±.ó ð0`˜4  S ™>ð `¨dó `ð
I˜˜c 3˜h™ð I¨Dó Ið!˜T # s (™^ð !°ó !ð 6˜˜c 3˜h™ð 6¨Dó 6ð
 4¨¨S¨¡>ð °dó ð �dó ð�tó ð3 4ð 3°¸"¿.¹.Ñ1Ið 3Èdó 3ð.$ ð $¨$ó $ð0˜tð 0¨ó 0ò"ð0 3ð 0¨8°C©=ó 0ð:   b¨tÑ4Ð ð	<�cð 	<˜có 	<ðØˆcðØ˜#ðàˆcðà˜#ðð ˆsðð ˜3ðð !'¨ðð �ð	ð ˜Sð	ð #)¨#ð	ð
 �ðð
 ˜Cðð
 "(¨ðð
 /6°sðð ˆcðð ˜#ðð �ðð ˜CñÐ ð ˜¨#Ø %Ø %ñÐ ð˜Cð  H¨U¡Oó ð2!( ð !(¨°#©ó !(ðH
>Ø
ð
>Øð
>Ø/2ð
>ØCFð
>à	ˆ#ˆsˆ(�^ó
>ðT ð T¨¨S°#¨X©ó TðI˜Sð I T¨#¨s¨(¡^ó IðX$�hð $ 8ó $ð˜ð  ¨Ñ 2ó ð5 hð 5¸ð 5Àdó 5ðF¨ð F¸Hð FÈó Fð NRòØ�3˜�8‰nðØ#+ðØ=EÀc¹]ðàˆc�]óð Ð ØÐ ðV Tð V¨có Vð $'Ð  ð°%ð Èð ÐPSó ð $%Ð ˜SÓ $àÐ Ø+-Ð " DÓ -ð$3Ø	ˆc�3ˆh‰ð$3Ø#'¨¨S¨¡>ð$3Ø8@ð$3à	ó$3ðP 35Ð �T˜#˜x¨™Ð.Ñ/Ó 4ð¨¨S°#¨X©ð ¸8ÀE¹?ó ðDF sð F°4¸¸S¸±>ð FÈ4ð FÐTXó FðW¨$¨s°C¨x©.ð WÈSð WÐUYó Wð¨D°°c°©Nó ð¨$¨s°C¨x©.ð Àxð ÐTXó ð8 Ð Ø 4Ð Ø"Ð ð Ø�3˜�8‰nð Ø/7ð ØFNð àó ð6 %&Ð ˜cÓ %Ø,.Ð # TÓ .ðYØ	ˆc�3ˆh‰ðYØ*2ðYØAIðYà	óYð$¨d°3¸°8©nó ò:˜t C¨ H™~ð :¸HÀS¹Mð :ÐU]Ð^aÑUbó :ð~˜ð  3ð °Cð ¸Dó òG Tð G°dó Gð"˜#ð  (¨5¡/ó ð/ (¨5¡/ó /ð2 ¨¡ó 2ð só ô_ð
X ð X¨ó XôHð	0˜x¨™}ó 	0ð	3 ð 	3¨¨s°D¨yÑ)9ó 	3ð.�4˜˜S #˜X™Ñ'ó .ðb4˜X d¨4°°S°©>Ñ&:Ñ;ó 4ð  ð ¨°%¸¸SÀ#¸Ñ2FÑ)Gó ð
,˜h u¨S°#°s¨]Ñ';Ñ<ð 
,Àó 
,ðØ
ˆt�C˜�H‰~Ñ
ðØ-5°jÀ±oÑ-Fðà	ˆ$ˆs�Cˆx‰.Ñóð4 MQòØ
ˆt�C˜�H‰~Ñ
ðØ08¸ÀC¹Ñ0Iðà	ˆ$ˆs�Cˆx‰.Ñóð"˜ ™ð ¨$ó ð 4ð ¨t°D¸¸c¸±NÑ/Cð Èó ð  Ð ð MQØò*Ø
ˆt�C˜�H‰~Ñ
ð*Ø08¸ÀC¹Ñ0Ið*àó*ð\ MQØòLØ
ˆt�C˜�H‰~Ñ
ðLØ08¸ÀC¹Ñ0IðLàóLñ ‹8€ð aeò
Øð
Ø˜t D¨¨c¨¡NÑ3°S¸$¸sÀC¸x¹.ÐIÈ3ÐNÑOð
ØZ]ð
àó
ðC˜d 3¨ 8™nð C°ó Cð
T˜d 3¨ 8™nð T°ó Tð
 ¨¡ð °(¸3±-ó ð,;˜˜x¨™}¨h°s©mÐ;Ñ<ó ;ð* 16òZØðZØ)-ðZàˆc�]óZðJ˜sð J x°¡}ó JðE˜sð E x°°S±	Ñ':ó Eð
K 3ð K¨8°D¸±IÑ+>ó Kð/ cð /¨h°s©mó /ð sð ¨x¸©}ó ð0 *Ø,Ø#Ø*Ø2Ø/ÙGØ!ØØ+Ø1ñ>Ð ˜4  X¨s¨e°S¨jÑ%9Ð 9Ñ:ó ñ SØ2Ø/Ø3ñ	>Ð ˜4  X¨s¨e°S¨jÑ%9Ð 9Ñ:ó ð8Ø˜S‘Mð8à˜#‘ð8ð ð8ð �S‰Mð	8ð
 
ó8ð*J cð J¨jó JðØ˜#˜s˜(‘^ðØ,/ðØBEðØMPðàˆc�]óð$ Ø Ø!Ø'+ØØ"&ØØ"Ø"Ø Ø48Ø,0Ø!ØØ(,Ø$(Ø!%Ø&*Ø%)ØØ#'Øò1}Ø�S‰Mð}àð}ð �3‰-ð}ð �S‰Mð	}ð
 �c‰]ð}ð �T˜#˜s˜(‘^Ñ$ð}ð �C‰=ð}ð �T˜#‘YÑð}ð �C‰=ð}ð �s‰mð}ð �s‰mð}ð �S‰Mð}ð ˜5  d¨3¡i Ñ0Ñ1ð}ð ˜t C™yÑ)ð}ð �c‰]ð}ð  ð!}ð"   ‘~ð#}ð$ ˜S‘Mð%}ð& ˜#‘ñ'}ð( ˜s‘mñ)}ð* ˜c‘]ñ+}ð, ñ-}ð. ˜C‘=ñ/}ð0 ð1}ð2 
ˆ#ˆsˆ(�^ô3}ð@C�Cð C˜H T¨#¨s¨(¡^Ñ4ô Cö	(˜Kô 	(ñ2˜ð 2 ¨$¨s°C¨x©.Ñ!9ô 2ó$ ð °°d¸3À¸8±nÑ1Eô ð F T¨#¨s¨(¡^ñ F¸dÀ3ÈÀ8¹nð FÐVYð FÐ^bô Fð ;˜4  S ™>ñ ;°D¸¸c¸±Nð ;Àtô ;ð _  S¨# X¡ñ _¸¸cÀ3¸h¹ð _ÈDô _ð$Ø	ˆc�3ˆh‰ñØ"& s¨C x¡.ðà	ôñ@
X D¨¨c¨¡Nñ 
X¸TÀ#ÀsÀ(¹^ð 
XÐUXð 
XÐ]aô 
Xñ& D¨¨c¨¡Nð &°tô &ð$'$�sñ '$ T¨#¨s¨(¡^ð '$¸ÀÀcÈ3ÀhÁÑ8Pô '$òT
�cñ 
 8¨C¡=ð 
¸HÀTÈ#ÈsÈ(Á^Ñ<Tô 
ð%�sð %˜x¨¨S°#¨X©Ñ7ô %òP˜ñ ¨8°C©=ð ÀHÈTÐRUÐWZÐRZÉ^ÑD\ô ñ2  S¨# X¡ð °4ô ñ"+˜#ð + Hñ +¸5ð +ÀTô +ñ [ð ð
 +˜#ð  + sð  +¨x¸¸SÀ#¸X¹Ñ/Gô  +ðF�sð ˜tô ð@+˜Cñ +¨ð +°Dð +¸Tô +ð*> 3ð >¨4ô >ð
8 Cð 8¨Dô 8ð
+ cñ +°3ð +¸4ô +ð Ø	ˆc�3ˆh‰ð Ø"&ñ Ø/7¸©}ñ ØNVÐWZÉmñ à�S‰Mð à #ð ð 
ô ð>,"˜D  c ™Nð ,"°ð ,"¸ô ,"ðd  Ø$(Ø ñ=-ð *.Ø#Ø*.ó=-Øð=-àñ=-ð �C‰=ñ=-ð ˜S‘Mñ	=-ð
 �S‰Mñ=-ð " #™ñ=-ð ñ=-ð ! ™ð=-ð 
ô=-ð@ 4¨¨S¨¡>ð ¸ð ÀCð ÈDô ð 1¨$¨s°C¨x©.ð  1¸Tô  1ðF¨$¨s°C¨x©.ñ ÀCð ÈDô ð(4˜3ð 4 4ô 4ðn˜˜d 3¨ 8™nÑ-ñ °cð È3ð ÐSWô ð+ ð +¸ð +Àô +ð$+˜Cð + Dô +ð" #ô ð6,˜Sð , Tô ,ð#�Sô #ð .DÐSXØ¨UóBNØñBNØ'*ñBNØLPñBNàñBNà&*ðBNð ˆ4��c˜3�h‘ÐÑ ôBNðJ	+ ð 	+¸ð 	+Àô 	+ñ $%Ð  ñ˜Sñ ¨3ñ °hÀ¸uÀc¸zÑ6Jð Èsô ðd¨5ô dð Y]ó"Ø�4˜˜S˜‘>Ñ"ð"Ø)1ð"ØCKÈCÐPSÉHÑCUð"à	ô"ðN
&�d˜4  S ™>Ñ*ô 
&ð ÷#ò #ó ð#ñ6¨$¨t°C¸°H©~Ñ*>ð À4ô ð( 4¨¨S¨¡>ñ ¸ð Àdô ð( 4¨¨S¨¡>ñ ¸ð ÀhÈsÁmõ ð. ÷Jò Jó ñJñ4¡Wð °õ ñ2!¡Gð !°õ !ñH™Gð ¨õ ñ>¡gñ °cð ¸dõ ñ:™wð ¨4õ ð'¨¨c°3¨h©ñ '¸xð 'ÈDõ 'ðT  c¨3 h¡ñ °xð ÀHÈSÁMõ ð.N˜4  S ™>ñ N°ñ NÈ%ð NÐTXõ Nðb$˜d 4¨¨S¨¡>Ñ2õ $ñ\ Ñ ðS˜3õ Sñ
 dñ °#ð ¸#õ ð,˜Cð ¨õ ñ %˜cð % cõ %ð   C¡õ ð& RVó2`Ø˜4  S ™>Ñ*ñ2`Ø;CÀDÈÁIÑ;Nð2`à	ˆ#ˆsˆ(�^õ2`ðt4 ð 4¨ö 4øðUh ò ØƒEðûð ò ØƒFðús#   ¿q Áq( ñq%ñ$q%ñ(q3ñ2q3