
    i]                      U d Z ddl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mZmZ ddlmZ ddlmZmZmZ h dZh dZd	Zd
ZdZdZdZdZdZ ej        d          ZddZ ddZ!ddZ"ddZ#ddZ$ddZ%ddZ&dddZ'ddd!Z(ddd"Z)ddd#Z*ddd$Z+ddd%Z,dd&Z-ddd(Z.dddddd)d d0Z/ddddd1d!d2Z0d3d4d"d7Z1d3d8d#d:Z2e G d; d<                      Z3e G d= d>                      Z4e G d? d@                      Z5e G dA dB                      Z6dCZ7 e8            Z9dDe:dE<   	 dddFd$dJZ;	 dddFd%dKZ<d&dMZ=ej>        d'dN            Z?ddOZ@ddPZAd(dRZBddddSddddTdUdddddVd)dhZCd*djZDd+dmZEddddUddnd,drZFd-dtZGd.dwZHd/dxZId/dyZJd0dzZKd0d{ZLd1d}ZMd2dZNd3dZOd4dZP	 dddd5dZQdddddd6dZRd7dZSddddd8dZTd9dZUeddd:dZVeddd;dZWddd9dZXdddd<dZYdUddd=dZZd>dZ[ ej        d          Z\d?dZ] G d de^          Z_ddddddd@dZ`ddddAdZaddddBdZbdCdZcdddddDdZddCdZeddFdEdZfdFdZgdZhehZidZje G d d                      ZkdZldZmi Znde:d<   dGdńZodHdǄZpdIdȄZqdddJd˄Zrddd̜dKd΄ZsdddLdτZtdMdфZudLd҄ZvddUdUddӜdNd؄ZwddٜdOdڄZxdPdۄZydQd܄ZzezZ{dRd݄Z|dedUdeiddޜdSdZ}dTdZ~ddFdUdZddeiddddVdZdWdZdXdZdYdZddddZdZ	 dd[dZddd\dZdddd]dZddd^dZddd_dZdddd`dZddFdadZddddbdZdcdZddd	Zd3d
dedZdfdZdgdZdgdZdhdZdidZdS (j  u   SQLite-backed Kanban board for multi-profile, multi-project collaboration.

In a fresh install the board lives at ``<root>/kanban.db`` where
``<root>`` is the **shared Hermes root** (the parent of any active
profile). Profiles intentionally collapse onto a shared board: it IS
the cross-profile coordination primitive. A worker spawned with
``hermes -p <profile>`` joins the same board as the dispatcher that
claimed the task. The same applies to ``<root>/kanban/workspaces/`` and
``<root>/kanban/logs/``.

**Multiple boards (projects):** users can create additional boards to
separate unrelated streams of work (e.g. one per project / repo / domain).
Each board is a directory under ``<root>/kanban/boards/<slug>/`` with
its own ``kanban.db``, ``workspaces/``, and ``logs/``. All boards share
the profile's Hermes home but are otherwise isolated: a worker spawned
for a task on board ``atm10-server`` sees only that board's tasks,
cannot enumerate other boards, and its dispatcher ticks don't touch
other boards' DBs.

The first (and for single-project users, only) board is ``default``.
For back-compat its on-disk DB is ``<root>/kanban.db`` (not
``boards/default/kanban.db``), so installs that predate the boards
feature keep working with zero migration. See :func:`kanban_db_path`.

Board resolution order (highest precedence first, all optional):

* ``board=`` argument passed directly to :func:`connect` / :func:`init_db`
  (explicit — used by the CLI ``--board`` flag and the dashboard
  ``?board=...`` query param).
* ``HERMES_KANBAN_BOARD`` env var (used by the dispatcher to pin workers
  to the board their task lives on — workers cannot see other boards).
* ``HERMES_KANBAN_DB`` env var (pins the DB file path directly — legacy
  override still honoured; highest precedence when the file path itself
  is what the caller wants to force).
* ``<root>/kanban/current`` — a one-line text file holding the slug of
  the "currently selected" board. Written by ``hermes kanban boards
  switch <slug>``. When absent, the active board is ``default``.

In standard installs ``<root>`` is ``~/.hermes``. In Docker / custom
deployments where ``HERMES_HOME`` points outside ``~/.hermes`` (e.g.
``/opt/hermes``), ``<root>`` is ``HERMES_HOME``. Legacy env-var
overrides still work:

* ``HERMES_KANBAN_DB`` — pin the database file path directly.
* ``HERMES_KANBAN_WORKSPACES_ROOT`` — pin the workspaces root directly.
* ``HERMES_KANBAN_HOME`` — pin the umbrella root that anchors kanban
  paths. Useful for tests and unusual deployments.

The dispatcher injects ``HERMES_KANBAN_DB``,
``HERMES_KANBAN_WORKSPACES_ROOT``, and ``HERMES_KANBAN_BOARD`` into
worker subprocess env so workers converge on the exact DB the
dispatcher used to claim their task — even under unusual symlink or
Docker layouts.

Schema is intentionally small: tasks, task_links, task_comments,
task_events.  The ``workspace_kind`` field decouples coordination from git
worktrees so that research / ops / digital-twin workloads work alongside
coding workloads.  See ``docs/hermes-kanban-v1-spec.pdf`` for the full
design specification.

Concurrency strategy: WAL mode + ``BEGIN IMMEDIATE`` for write
transactions + compare-and-swap (CAS) updates on ``tasks.status`` and
``tasks.claim_lock``.  SQLite serializes writers via its WAL lock, so at
most one claimer can win any given task.  Losers observe zero affected
rows and move on -- no retry loops, no distributed-lock machinery.
The CAS coordination is **per-board** — each board is a separate DB,
so multi-board installs get the same atomicity guarantees without any
new locking.
    )annotationsN)	dataclassfield)Path)AnyIterableOptional>   donetodoreadytriageblockedrunningarchived>   dirscratchworktreei  
      i   i    i   defaultz^[a-z0-9][a-z0-9\-_]{0,63}$slugOptional[str]returnc                    | dS t          |                                                                           }|sdS t                              |          st          d| d          |S )z>Lowercase + strip a slug; validate; return ``None`` for empty.Nzinvalid board slug zc: must be 1-64 chars, lowercase alphanumerics / hyphens / underscores, not starting with '-' or '_')strstriplower_BOARD_SLUG_REmatch
ValueError)r   ss     9/home/piyush/.hermes/hermes-agent/hermes_cli/kanban_db.py_normalize_board_slugr#      s    |tD		!!A t"" 
S$ S S S
 
 	
 H    r   c                     t           j                            dd                                          } | r!t	          |                                           S ddlm}  |            S )a  Return the shared Hermes root that anchors the kanban board.

    Resolution order:

    1. ``HERMES_KANBAN_HOME`` env var when set and non-empty (explicit
       override for tests and unusual deployments).
    2. ``get_default_hermes_root()``, which already returns ``<root>``
       when ``HERMES_HOME`` is ``<root>/profiles/<name>``, and returns
       ``HERMES_HOME`` directly for Docker / custom deployments.

    The kanban board is shared across profiles **by design** (see the
    module docstring). Resolving the kanban paths through the active
    profile's ``HERMES_HOME`` would silently fork the board per profile,
    which breaks the dispatcher / worker handoff.
    HERMES_KANBAN_HOME r   get_default_hermes_root)osenvirongetr   r   
expanduserhermes_constantsr)   )overrider)   s     r"   kanban_homer0      sg      z~~2B77==??H +H~~((***888888""$$$r$   c                 *    t                      dz  dz  S )ua  Return ``<root>/kanban/boards`` — the parent of non-default board dirs.

    ``default`` is intentionally NOT under this directory — its DB lives at
    ``<root>/kanban.db`` for back-compat with pre-boards installs. This
    function returns the directory where *additional* named boards live,
    used by :func:`list_boards` to enumerate them.
    kanbanboardsr0    r$   r"   boards_rootr6      s     ==8#h..r$   c                 *    t                      dz  dz  S )zReturn the path to ``<root>/kanban/current``.

    One-line text file written by ``hermes kanban boards switch <slug>``
    to persist the user's board selection across CLI invocations. Absent
    by default (meaning: active board is ``default``).
    r2   currentr4   r5   r$   r"   current_board_pathr9      s     ==8#i//r$   r   c                    t           j                            dd                                          } | r%	 t	          |           }|r|S n# t
          $ r Y nw xY w	 t                      }|                                r^|                    d                                          }|r4	 t	          |          }|rt          |          r|S n# t
          $ r Y nw xY wn# t          $ r Y nw xY wt          S )u`  Return the active board slug, honouring the resolution chain.

    Order (highest precedence first):

    1. ``HERMES_KANBAN_BOARD`` env var (set by the dispatcher on worker
       spawn, or manually for ad-hoc overrides).
    2. ``<root>/kanban/current`` on disk (set by ``hermes kanban boards
       switch``), but only when that board still exists.
    3. ``DEFAULT_BOARD`` (``"default"``).

    A malformed or stale slug at any step falls through to the next layer
    with a best-effort warning — the dispatcher must never crash because a
    user hand-edited a file or removed a board directory.
    HERMES_KANBAN_BOARDr'   utf-8encoding)r*   r+   r,   r   r#   r    r9   exists	read_textboard_existsOSErrorDEFAULT_BOARD)envnormedfvals       r"   get_current_boardrH      s/    *...
3
3
9
9
;
;C
 	*3//F  	 	 	D	  88:: 	++w+//5577C 2377F &,v"6"6 &%!   D   sH   A
 

AAAC (!C 
C 
CC CC 
C)(C)c                    t          |           }|st          d          t                      }|j                            dd           |                    |dz   d           |S )uK  Persist ``slug`` as the active board. Returns the file written.

    Writes ``<root>/kanban/current``. The caller should validate the slug
    exists first (via :func:`board_exists`) — this function does not —
    so that ``hermes kanban boards switch <typo>`` returns an error
    instead of silently pointing at nothing.
    board slug is requiredTparentsexist_ok
r<   r=   )r#   r    r9   parentmkdir
write_text)r   rE   paths      r"   set_current_boardrS      sm     #4((F 31222DKdT222OOFTMGO444Kr$   Nonec                 j    	 t                                                       dS # t          $ r Y dS w xY w)zLRemove ``<root>/kanban/current`` so the active board reverts to ``default``.N)r9   unlinkFileNotFoundErrorr5   r$   r"   clear_current_boardrX      sG    ##%%%%%   s    $ 
22boardc                P    t          |           pt          }t                      |z  S )u  Return the on-disk directory for ``board``.

    ``default`` is ``<root>/kanban/boards/default/`` **for metadata only**
    (board.json + workspaces/ + logs/). Its DB file stays at
    ``<root>/kanban.db`` for back-compat — see :func:`kanban_db_path`.

    All other boards live at ``<root>/kanban/boards/<slug>/`` with
    everything inside that directory including the ``kanban.db``.
    )r#   rC   r6   rY   r   s     r"   	board_dirr\      s%     !''8=D==4r$   boolc                    t          |           pt          }|t          k    rdS t          |          }|                                p|dz                                  S )u  Return True if the board has a DB or a metadata dir on disk.

    ``default`` is considered to always exist — its DB is created
    on first :func:`connect` and there's no way for it to be missing
    in a configuration where the kanban feature is usable at all.
    T	kanban.db)r#   rC   r\   is_dirr?   )rY   r   ds      r"   rA   rA     sT     !''8=D}t$A88::3!k/11333r$   c                F   t           j                            dd                                          }|r!t	          |                                          S t          |           }|t                      }|t          k    rt                      dz  S t          |          dz  S )u^  Return the path to the ``kanban.db`` for ``board``.

    Resolution (highest precedence first):

    1. ``HERMES_KANBAN_DB`` env var — pins the path directly. Honoured for
       back-compat and for the dispatcher→worker handoff (defense in
       depth: dispatcher injects this into worker env so workers are
       immune to any path-resolution disagreement).
    2. When ``board`` arg is None, the active board from
       :func:`get_current_board` is used.
    3. Board ``default`` → ``<root>/kanban.db`` (back-compat path).
       Other boards → ``<root>/kanban/boards/<slug>/kanban.db``.
    HERMES_KANBAN_DBr'   Nr_   r*   r+   r,   r   r   r-   r#   rH   rC   r0   r\   rY   r/   r   s      r"   kanban_db_pathrf     s     z~~0"55;;==H +H~~((*** ''D| ""}}}{**T??[((r$   c                L   t           j                            dd                                          }|r!t	          |                                          S t          |           }|t                      }|t          k    rt                      dz  dz  S t          |          dz  S )u  Return the directory under which ``scratch`` workspaces are created.

    Anchored per-board so workspaces don't leak between projects.
    ``HERMES_KANBAN_WORKSPACES_ROOT`` pins the path directly (highest
    precedence) — the dispatcher injects this into worker env.

    ``default`` keeps the legacy path ``<root>/kanban/workspaces/`` so
    that existing scratch workspaces from before the boards feature are
    preserved. Other boards use ``<root>/kanban/boards/<slug>/workspaces/``.
    HERMES_KANBAN_WORKSPACES_ROOTr'   Nr2   
workspacesrd   re   s      r"   workspaces_rootrj   /  s     z~~=rBBHHJJH +H~~((*** ''D| ""}}}x',66T??\))r$   c                    t          |           }|t                      }|t          k    rt                      dz  dz  S t	          |          dz  S )uD  Return the directory under which per-task worker logs are written.

    ``default`` keeps the legacy path ``<root>/kanban/logs/``. Other
    boards use ``<root>/kanban/boards/<slug>/logs/``. Logs follow the
    board — makes ``hermes kanban log`` unambiguous even when multiple
    boards have tasks with the same id.
    Nr2   logs)r#   rH   rC   r0   r\   r[   s     r"   worker_logs_dirrm   E  sQ     !''D| ""}}}x'&00T??V##r$   c                R    t          |           pt          }t          |          dz  S )zReturn the path to ``board.json`` for ``board``.

    Stores display metadata (display name, description, icon, color,
    created_at). The on-disk slug is the canonical identity; this file
    is purely for presentation in the CLI / dashboard.
    
board.json)r#   rC   r\   r[   s     r"   board_metadata_pathrp   U  s'     !''8=DT??\))r$   c                    d                     d |                     dd                              d          D                       p| S )u   Turn a slug into a reasonable default display name.

    ``atm10-server`` → ``Atm10 Server``. Users can override via
    ``board.json`` but the default should look presentable in the
    dashboard without any follow-up editing.
     c              3  B   K   | ]}||                                 V  d S N)
capitalize).0parts     r"   	<genexpr>z._default_board_display_name.<locals>.<genexpr>g  s2      \\$W[\DOO%%\\\\\\r$   _-)joinreplacesplit)r   s    r"   _default_board_display_namer~   `  sG     88\\$,,sC2H2H2N2Ns2S2S\\\\\d`ddr$   dictc                   t          |           pt          }|t          |          dddddd}	 t          |          }|                                rWt          j        |                    d                    }t          |t                    r||d<   |
                    |           n# t          t
          j        f$ r Y nw xY wt          t          |                    |d<   |S )	u8  Return ``board.json`` contents (or synthesized defaults).

    Never raises — a missing / malformed ``board.json`` falls back to a
    synthesised entry so the dashboard always has something to render.
    Includes the canonical ``slug`` and ``db_path`` so the caller
    doesn't need to reconstruct them.
    r'   NF)r   namedescriptioniconcolor
created_atr   r<   r=   r   db_path)r#   rC   r~   rp   r?   jsonloadsr@   
isinstancer   updaterB   JSONDecodeErrorr   rf   )rY   r   metapraws        r"   read_board_metadatar   j  s     !''8=D+D11 D
%%88:: 	!*Q[['[::;;C#t$$ ! #FC   T)*   ...//DOKs   A:B* *CC)r   r   r   r   r   r   r   r   r   r   Optional[bool]c                  t          |           pt          }t          |          }|                    dd           |3t	          |                                          pt          |          |d<   |t	          |          |d<   |t	          |          |d<   |t	          |          |d<   |t          |          |d<   |                    d          s#t          t          j
                              |d<   t          |          }|j                            d	d	
           |                    t          j        |dd          dz   d           t	          t#          |                    |d<   |S )zCreate / update ``board.json`` for ``board``.

    Preserves any existing fields not mentioned in the call. Sets
    ``created_at`` on first write. Returns the resulting metadata dict.
    r   Nr   r   r   r   r   r   TrK      F)indentensure_asciirN   r<   r=   )r#   rC   r   popr   r   r~   r]   r,   inttimerp   rO   rP   rQ   r   dumpsrf   )	rY   r   r   r   r   r   r   r   rR   s	            r"   write_board_metadatar     si    !''8=Dt$$D 	HHY4yy((M,G,M,MV!+..]4yyVE

W>>Z88L!! . --\t$$DKdT222OO
4666=     ...//DOKr$   r   r   r   r   c                   t          |           }|st          d          t          |||||          }t          |           |S )u
  Create a new board directory + DB + metadata. Idempotent.

    Returns the resulting metadata. Raises :class:`ValueError` for a
    malformed slug; returns the existing metadata (not an error) if the
    board already exists — matching ``mkdir -p`` semantics.
    rJ   r   rY   )r#   r    r   init_db)r   r   r   r   r   rE   r   s          r"   create_boardr     sc     #4((F 31222  D &Kr$   T)include_archivedr   
list[dict]c                   g }t                      }|                    t          t                               |                    t                     t                      }|                                rt          |                                d           D ]}|                                s|j	        }	 t          |          }n# t          $ r Y ;w xY w|r||v rF|dz                                  }|dz                                  }|s|syt          |          }	|	                    d          r| s|                    |	           |                    |           |S )a  Enumerate all boards that exist on disk.

    Always includes ``default`` (even when the ``boards/default/``
    metadata dir doesn't exist, because its DB is at the legacy path).
    Other boards are discovered by scanning ``boards/`` for subdirectories
    that either contain a ``kanban.db`` or a ``board.json``.

    Returns a list of metadata dicts, sorted with ``default`` first and
    the rest alphabetically.
    c                4    | j                                         S rt   )r   r   )r   s    r"   <lambda>zlist_boards.<locals>.<lambda>  s    !&,,.. r$   keyr_   ro   r   )setappendr   rC   addr6   r`   sortediterdirr   r#   r    r?   r,   )
r   entriesseenrootchildr   rE   has_dbhas_metar   s
             r"   list_boardsr     sy    GUUD NN&}55666HH]==D{{}} DLLNN0H0HIII 	 	E<<>> :D.t44    Vt^^k)1133F,4466H h &v..Dxx
## ,< NN4   HHVNs   7C
CC)archiver   c                  t          |           }|st          d          |t          k    rt          d          t          |          }|                                st          d|d          t                      |k    rt                       |rt                      dz  }|                    dd           t          t          j
                              }|| d| z  }d	}|                                r&|| d| d| z  }|d	z  }|                                &|                    |           |d
t          |          dS ddl}|                    |           |dddS )u  Remove or archive a board.

    ``archive=True`` (default) moves the board's directory to
    ``<root>/kanban/boards/_archived/<slug>-<timestamp>/`` so the data
    is recoverable. ``archive=False`` deletes the directory outright.

    The ``default`` board cannot be removed — raises :class:`ValueError`.
    Returns a summary dict describing what happened (``{"slug", "action",
    "new_path"}``).
    rJ   z%the 'default' board cannot be removedzboard z does not exist	_archivedTrK   rz      r   )r   actionnew_pathr   Ndeletedr'   )r#   r    rC   r\   r?   rH   rX   r6   rP   r   r   renamer   shutilrmtree)	r   r   rE   ra   archive_roottstargetsuffixr   s	            r"   remove_boardr     s    #4((F 31222@AAA&A88:: =;&;;;<<< f$$ E"}}{24$7776 0 0B 0 00mmoo 	!v$=$=$=$=V$=$==FaKF mmoo 	 	
*#f++NNNa)DDDr$   c                  j   e Zd ZU dZded<   ded<   ded<   ded<   ded<   d	ed
<   ded<   d	ed<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   dZded<   dZded<   dZd	ed<   dZded<   dZ	ded<   dZ
ded<   dZded<   dZded<   dZded<   dZded <   dZd!ed"<   dZded#<   ed)d(            ZdS )*Taskz1In-memory view of a row from the ``tasks`` table.r   idtitler   bodyassigneestatusr   priority
created_byr   Optional[int]
started_atcompleted_atworkspace_kindworkspace_path
claim_lockclaim_expirestenantNresultidempotency_keyr   consecutive_failures
worker_pidlast_failure_errormax_runtime_secondslast_heartbeat_atcurrent_run_idworkflow_template_idcurrent_step_keyzOptional[list]skillsmax_retriesrowsqlite3.Rowr   'Task'c                   t          |                                          }d }d|v rW|d         rO	 t          j        |d                   }t	          |t
                    rd |D             }n# t          $ r d }Y nw xY w | d i d|d         d|d         d|d         d|d         d|d         d|d         d	|d	         d
|d
         d|d         d|d         d|d         d|d         d|d         d|d         dd|v r|d         nd dd|v r|d         nd dd|v r|d         nd dd|v r|d         nd|v r|d         nddd|v r|d         nd dd|v r|d         nd|v r|d         nd dd|v r|d         nd dd|v r|d         nd dd|v r|d         nd dd|v r|d         nd dd|v r|d         nd d|dd|v r|d         nd S )!Nr   c                0    g | ]}|t          |          S r5   )r   )rv   r!   s     r"   
<listcomp>z!Task.from_row.<locals>.<listcomp>h  s#    #@#@#@qa#@CFF#@#@#@r$   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   spawn_failuresr   r   r   last_spawn_errorr   r   r   r   r   r   r5   )r   keysr   r   r   list	Exception)clsr   r   skills_valueparseds        r"   from_rowzTask.from_row_  sc   388::'+tH$CM22fd++ A#@#@F#@#@#@L $ $ $#$s 3
 3
 3
4yy3
g,,3
 V3
 __	3

 x==3
 __3
 <((3
 <((3
 <((3
 ^,,3
 /003
 /003
 <((3
 o..3
 %-$4$43x==$3
  %-$4$43x==$!3
" 7H46O6OC 122UY#3
& 0F/M/M*++
 0@4/G/Gc*++Q13
4 -9D,@,@s<((d53
8 .BT-I-I())1Ct1K1Kc,--QU=3
B /Dt.K.K)**QUC3
H -@4,G,G'((TI3
N *:T)A)A$%%tO3
T 0F/M/M*++SWU3
Z ,>+E+E&''4[3
^  <_3
b '4t&;&;M""c3
 3	
s   ;A- -A<;A<)r   r   r   r   )__name__
__module____qualname____doc____annotations__r   r   r   r   r   r   r   r   r   r   r   r   classmethodr   r5   r$   r"   r   r   +  s        ;;GGGJJJKKKMMMOOO!!!!     F    %)O)))) !"!!!! $J$$$$ )-,,,,)-----'+++++$(N((((*.....&*****
 "F!!!! "&K%%%%>
 >
 >
 [>
 >
 >
r$   r   c                      e Zd ZU dZded<   ded<   ded<   ded<   ded	<   ded
<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   edd            ZdS )Runur  In-memory view of a ``task_runs`` row.

    A run is one attempt to execute a task — created on claim, closed
    on complete/block/crash/timeout/spawn_failure/reclaim. Multiple runs
    per task when retries happen. Carries the claim machinery, PID,
    heartbeat, and the structured handoff summary that downstream workers
    read via ``build_worker_context``.
    r   r   r   task_idr   profilestep_keyr   r   r   r   r   r   r   r   ended_atoutcomesummaryOptional[dict]metadataerrorr   r   r   'Run'c           	        	 |d         rt          j        |d                   nd }n# t          $ r d }Y nw xY w | di dt          |d                   d|d         d|d         d|d         d|d         d|d         d|d         d	|d	         d
|d
         d|d         dt          |d                   d|d         t          |d                   nd d|d         d|d         d|d|d         S )Nr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r5   )r   r   r   r   )r   r   r   s      r"   r   zRun.from_row  s   	25j/K4:c*o...tDD 	 	 	DDD	s 
 
 
3t9~~~
	NN
 	NN
 __	

 x==
 <((
 o..
 <((
 !$$9 : :
 ""566
 3|,---
 /2*o.Ic#j/***t
 	NN
 	NN
 T
  g,,!
 	
s   $' 66N)r   r   r   r   )r   r   r   r   r   r   r   r5   r$   r"   r   r     s           GGGLLLKKK    &&&&$$$$OOO
 
 
 [
 
 
r$   r   c                  B    e Zd ZU ded<   ded<   ded<   ded<   ded<   dS )	Commentr   r   r   r   authorr   r   N)r   r   r   r   r5   r$   r"   r  r    s=         GGGLLLKKKIIIOOOOOr$   r  c                  P    e Zd ZU ded<   ded<   ded<   ded<   ded<   d	Zd
ed<   d	S )Eventr   r   r   r   kindr   payloadr   Nr   run_id)r   r   r   r   r  r5   r$   r"   r  r    sS         GGGLLLIIIOOO F      r$   r  u  
CREATE TABLE IF NOT EXISTS tasks (
    id                   TEXT PRIMARY KEY,
    title                TEXT NOT NULL,
    body                 TEXT,
    assignee             TEXT,
    status               TEXT NOT NULL,
    priority             INTEGER DEFAULT 0,
    created_by           TEXT,
    created_at           INTEGER NOT NULL,
    started_at           INTEGER,
    completed_at         INTEGER,
    workspace_kind       TEXT NOT NULL DEFAULT 'scratch',
    workspace_path       TEXT,
    claim_lock           TEXT,
    claim_expires        INTEGER,
    tenant               TEXT,
    result               TEXT,
    idempotency_key      TEXT,
    -- Unified consecutive-failure counter. Incremented on spawn
    -- failure, timeout, or crash; reset only on successful completion.
    -- The circuit breaker in _record_task_failure trips when this
    -- exceeds DEFAULT_FAILURE_LIMIT consecutive non-successes.
    consecutive_failures INTEGER NOT NULL DEFAULT 0,
    worker_pid           INTEGER,
    -- Short excerpt of the most recent failure's error text.
    last_failure_error   TEXT,
    max_runtime_seconds  INTEGER,
    last_heartbeat_at    INTEGER,
    -- Pointer into task_runs for the currently-active run (NULL if no
    -- run is in-flight). Denormalised for cheap reads.
    current_run_id       INTEGER,
    -- Forward-compat for v2 workflow routing. In v1 the kernel writes
    -- these when the task is opted into a template but otherwise ignores
    -- them; the dispatcher doesn't consult them for routing yet.
    workflow_template_id TEXT,
    current_step_key     TEXT,
    -- Force-loaded skills for the worker on this task, stored as JSON.
    -- Appended to the dispatcher's built-in `--skills kanban-worker`.
    -- NULL or empty array = no extras.
    skills               TEXT,
    -- Per-task override for the consecutive-failure circuit breaker.
    -- The value is the failure count at which the breaker trips — e.g.
    -- ``max_retries=1`` blocks on the first failure. NULL (the common
    -- case) falls through to the dispatcher-level ``kanban.failure_limit``
    -- config and then ``DEFAULT_FAILURE_LIMIT``.
    max_retries          INTEGER
);

CREATE TABLE IF NOT EXISTS task_links (
    parent_id  TEXT NOT NULL,
    child_id   TEXT NOT NULL,
    PRIMARY KEY (parent_id, child_id)
);

CREATE TABLE IF NOT EXISTS task_comments (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    task_id    TEXT NOT NULL,
    author     TEXT NOT NULL,
    body       TEXT NOT NULL,
    created_at INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS task_events (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    task_id    TEXT NOT NULL,
    run_id     INTEGER,
    kind       TEXT NOT NULL,
    payload    TEXT,
    created_at INTEGER NOT NULL
);

-- Historical attempt record. Each time the dispatcher claims a task, a
-- new row is created here; claim state, PID, heartbeat, runtime cap,
-- and structured summary all live on the run, not the task. Multiple
-- rows per task id when the task was retried after crash/timeout/block.
-- v2 of the kanban schema will use ``step_key`` to drive per-stage
-- workflow routing; in v1 the column is nullable and unused (kernel
-- ignores it).
CREATE TABLE IF NOT EXISTS task_runs (
    id                  INTEGER PRIMARY KEY AUTOINCREMENT,
    task_id             TEXT NOT NULL,
    profile             TEXT,
    step_key            TEXT,
    status              TEXT NOT NULL,
    -- status: running | done | blocked | crashed | timed_out | failed | released
    claim_lock          TEXT,
    claim_expires       INTEGER,
    worker_pid          INTEGER,
    max_runtime_seconds INTEGER,
    last_heartbeat_at   INTEGER,
    started_at          INTEGER NOT NULL,
    ended_at            INTEGER,
    outcome             TEXT,
    -- outcome: completed | blocked | crashed | timed_out | spawn_failed |
    --          gave_up | reclaimed | (null while still running)
    summary             TEXT,
    metadata            TEXT,
    error               TEXT
);

-- Subscription from a gateway source (platform + chat + thread) to a
-- task. The gateway's kanban-notifier watcher tails task_events and
-- pushes ``completed`` / ``blocked`` / ``spawn_auto_blocked`` events to
-- the original requester so human-in-the-loop workflows close the loop.
CREATE TABLE IF NOT EXISTS kanban_notify_subs (
    task_id       TEXT NOT NULL,
    platform      TEXT NOT NULL,
    chat_id       TEXT NOT NULL,
    thread_id     TEXT NOT NULL DEFAULT '',
    user_id       TEXT,
    created_at    INTEGER NOT NULL,
    last_event_id INTEGER NOT NULL DEFAULT 0,
    PRIMARY KEY (task_id, platform, chat_id, thread_id)
);

CREATE INDEX IF NOT EXISTS idx_tasks_assignee_status ON tasks(assignee, status);
CREATE INDEX IF NOT EXISTS idx_tasks_status          ON tasks(status);
CREATE INDEX IF NOT EXISTS idx_tasks_tenant          ON tasks(tenant);
CREATE INDEX IF NOT EXISTS idx_tasks_idempotency     ON tasks(idempotency_key);
CREATE INDEX IF NOT EXISTS idx_links_child           ON task_links(child_id);
CREATE INDEX IF NOT EXISTS idx_links_parent          ON task_links(parent_id);
CREATE INDEX IF NOT EXISTS idx_comments_task         ON task_comments(task_id, created_at);
CREATE INDEX IF NOT EXISTS idx_events_task           ON task_events(task_id, created_at);
CREATE INDEX IF NOT EXISTS idx_events_run            ON task_events(run_id, id);
CREATE INDEX IF NOT EXISTS idx_runs_task             ON task_runs(task_id, started_at);
CREATE INDEX IF NOT EXISTS idx_runs_status           ON task_runs(status);
CREATE INDEX IF NOT EXISTS idx_notify_task           ON kanban_notify_subs(task_id);
zset[str]_INITIALIZED_PATHSr   r   Optional[Path]sqlite3.Connectionc               .   | | }nt          |          }|j                            dd           t          |                                          }|t
          v}t          j        t          |          dd          }t          j        |_	        |
                    d           |
                    d           |
                    d	           |rC|                    t                     t          |           t
                              |           |S )
u  Open (and initialize if needed) the kanban DB.

    WAL mode is enabled on every connection; it's a no-op after the first
    time but keeps the code robust if the DB file is ever re-created.

    The first connection to a given path auto-runs :func:`init_db` so
    fresh installs and test harnesses that construct `connect()`
    directly don't have to remember a separate init step. Subsequent
    connections skip the schema check via a module-level path cache.

    Path resolution:

    * ``db_path`` explicit → used as-is (legacy callers, tests).
    * ``board`` explicit → resolves to that board's DB.
    * Neither → :func:`kanban_db_path` resolves via
      ``HERMES_KANBAN_DB`` env → ``HERMES_KANBAN_BOARD`` env →
      ``<root>/kanban/current`` → ``default``.
    Nr   TrK   r   )isolation_leveltimeoutzPRAGMA journal_mode=WALzPRAGMA synchronous=NORMALzPRAGMA foreign_keys=ON)rf   rO   rP   r   resolver  sqlite3connectRowrow_factoryexecuteexecutescript
SCHEMA_SQL_migrate_add_optional_columnsr   )r   rY   rR   resolved
needs_initconns         r"   r  r  x  s    . E***KdT2224<<>>""H!33J?3t99dBGGGD{DLL*+++LL,---LL)*** ) 	:&&&%d+++x(((Kr$   c               R   | | }nt          |          }|j                            dd           t          |                                          }t
                              |           t          j        t          |                    5  	 ddd           n# 1 swxY w Y   |S )u  Create the schema if it doesn't exist; return the path used.

    Kept as a public entry point so CLI ``hermes kanban init`` and the
    daemon have something explicit to call. Unlike :func:`connect`'s
    first-time auto-init (which caches by path), ``init_db`` always
    re-runs the migration pass. Callers that know the on-disk schema
    may have drifted — tests that write legacy event kinds directly,
    external tools that upgrade an old DB file — can call this to
    force re-migration.
    Nr   TrK   )
rf   rO   rP   r   r  r  discard
contextlibclosingr  )r   rY   rR   r  s       r"   r   r     s     E***KdT2224<<>>""H x(((		GDMM	*	*                Ks   BB #B r  c                   d |                      d          D             }d|vr|                      d           d|vr|                      d           d|vr*|                      d           |                      d	           d
|vr.|                      d           d|v r|                      d           d|vr|                      d           d|vr.|                      d           d|v r|                      d           d|vr|                      d           d|vr|                      d           d|vr|                      d           d|vr|                      d           d|vr|                      d           d|vr|                      d           d |vr|                      d!           d" |                      d#          D             }d$|vr*|                      d%           |                      d&           |                      d'                                          d(u}|r-t          |           5  |                      d)                                          }|D ]}|d*         pt	          t          j                              }|                      d+|d,         |d-         |d.         |d/         |d         |d         |d         |f          }|                      d0|j        |d,         f          }|j        d1k    r;|                      d2t	          t          j                              |j        f           	 d(d(d(           n# 1 swxY w Y   d3}	|	D ]\  }
}|                      d4||
f           d(S )5zAdd columns that were introduced after v1 release to legacy DBs.

    Called by ``init_db`` so opening an old DB is always safe.
    c                    h | ]
}|d          S r   r5   rv   r   s     r"   	<setcomp>z0_migrate_add_optional_columns.<locals>.<setcomp>  s    LLLCCKLLLr$   zPRAGMA table_info(tasks)r   z(ALTER TABLE tasks ADD COLUMN tenant TEXTr   z(ALTER TABLE tasks ADD COLUMN result TEXTr   z1ALTER TABLE tasks ADD COLUMN idempotency_key TEXTzJCREATE INDEX IF NOT EXISTS idx_tasks_idempotency ON tasks(idempotency_key)r   zLALTER TABLE tasks ADD COLUMN consecutive_failures INTEGER NOT NULL DEFAULT 0r   zCUPDATE tasks SET consecutive_failures = COALESCE(spawn_failures, 0)r   z/ALTER TABLE tasks ADD COLUMN worker_pid INTEGERr   z4ALTER TABLE tasks ADD COLUMN last_failure_error TEXTr   z6UPDATE tasks SET last_failure_error = last_spawn_errorr   z8ALTER TABLE tasks ADD COLUMN max_runtime_seconds INTEGERr   z6ALTER TABLE tasks ADD COLUMN last_heartbeat_at INTEGERr   z3ALTER TABLE tasks ADD COLUMN current_run_id INTEGERr   z6ALTER TABLE tasks ADD COLUMN workflow_template_id TEXTr   z2ALTER TABLE tasks ADD COLUMN current_step_key TEXTr   z(ALTER TABLE tasks ADD COLUMN skills TEXTr   z0ALTER TABLE tasks ADD COLUMN max_retries INTEGERc                    h | ]
}|d          S r   r5   r!  s     r"   r"  z0_migrate_add_optional_columns.<locals>.<setcomp>  s    UUUss6{UUUr$   zPRAGMA table_info(task_events)r  z1ALTER TABLE task_events ADD COLUMN run_id INTEGERzDCREATE INDEX IF NOT EXISTS idx_events_run ON task_events(run_id, id)zFSELECT name FROM sqlite_master WHERE type='table' AND name='task_runs'NzSELECT id, assignee, claim_lock, claim_expires, worker_pid,        max_runtime_seconds, last_heartbeat_at, started_at FROM tasks WHERE status = 'running' AND current_run_id IS NULLr   aV  
                    INSERT INTO task_runs (
                        task_id, profile, status,
                        claim_lock, claim_expires, worker_pid,
                        max_runtime_seconds, last_heartbeat_at,
                        started_at
                    ) VALUES (?, ?, 'running', ?, ?, ?, ?, ?, ?)
                    r   r   r   r   zKUPDATE tasks SET current_run_id = ? WHERE id = ? AND current_run_id IS NULLr   z_UPDATE task_runs SET status = 'reclaimed',     outcome = 'reclaimed', ended_at = ? WHERE id = ?))r   promoted)r   reprioritized)spawn_auto_blockedgave_upz.UPDATE task_events SET kind = ? WHERE kind = ?)r  fetchone	write_txnfetchallr   r   	lastrowidrowcount)r  colsev_cols
runs_existinflightr   startedcurupd_EVENT_RENAMESoldnews               r"   r  r    sz   
 ML4<<0J#K#KLLLDt?@@@t?@@@$$HIII(	
 	
 	
* T)))	
 	
 	
 t##LLU   4FGGG4''KLLL%%LLH   D((OPPP$&&MNNNt##JKKKT))MNNN%%IJJJt 	?@@@D   	GHHH VUdll3S&T&TUUUGwHIII)	
 	
 	
 P hjjJ  *t__ )	 )	||F 
 hjj    " "l+?s49;;/?/?ll D	3z?C4EO,c,.?12C8K4L	 * ll>]CI. 
 <1$$LL' TY[[))3=9	  ;")	 )	 )	 )	 )	 )	 )	 )	 )	 )	 )	 )	 )	 )	 )	^N # 
 
S<#J	
 	
 	
 	

 
s   4DMMMc              #     K   |                      d           	 | V  |                      d           dS # t          $ r |                      d            w xY w)a  Context manager for an IMMEDIATE write transaction.

    Use for any multi-statement write (creating a task + link, claiming a
    task + recording an event, etc.).  A claim CAS inside this context is
    atomic -- at most one concurrent writer can succeed.
    zBEGIN IMMEDIATECOMMITROLLBACKN)r  r   )r  s    r"   r)  r)  ]  su       	LL"###



 	X	    Z   s	   4 !Ac                 0    dt          j        d          z   S )a  Generate a short, URL-safe task id.

    4 hex bytes = ~4.3B possibilities. At 10k tasks the collision
    probability is ~1.2e-5; at 100k it's ~1.2e-3. Previously we used 2
    hex bytes (65k possibilities) which hit the birthday paradox hard:
    ~5% collision probability at 1k tasks, ~50% at 10k. Callers that
    care about idempotency should pass ``idempotency_key`` to
    :func:`create_task` rather than rely on id uniqueness.
    t_   )secrets	token_hexr5   r$   r"   _new_task_idr?  s  s     '#A&&&&r$   c                     ddl } 	 |                                 pd}n# t          $ r d}Y nw xY w| dt          j                     S )z:Return a ``host:pid`` string that identifies this claimer.r   Nunknown:)socketgethostnamer   r*   getpid)rC  hosts     r"   _claimer_idrG    sf    MMM!!##0y   ""RY[["""s    ,,r   c                ,    | dS ddl m}  ||           S )zHLowercase-assignee normalization for Kanban rows (dashboard/CLI parity).Nr   normalize_profile_name)hermes_cli.profilesrJ  )r   rJ  s     r"   _canonical_assigneerL    s0    t::::::!!(+++r$   r   r5   F)r   r   r   r   r   r   r   rL   r   r   r   r   r   r   r   r   r   r   r   r   r   rL   Iterable[str]r   r   r   r   r   Optional[Iterable[str]]r   c                  t          |          }|r|                                st          d          |t          vr't          dt	          t                     d|          t          d |	D                       }	d}|g }t                      }|D ]o}|st          |                                          }|s)d|v rt          d|d          ||v rE|                    |           |	                    |           p|}|r3| 
                    d	|f                                          }|r|d
         S t          t          j                              }t          d          D ]}t                      }	 t!          |           5  |
rd}nd}|	rt#          | |	          }|r%t          dd                    |                     | 
                    dd                    dt'          |	          z            z   dz   |	                                          }t+          d |D                       rd}|
r9|	r7t#          | |	          }|r%t          dd                    |                     | 
                    d||                                |||||||||||rt          |          nd|t-          j        |          nd|t          |          ndf           |	D ]}| 
                    d||f           t1          | |d||t3          |	          ||rt3          |          ndd           ddd           n# 1 swxY w Y   |c S # t4          j        $ r |dk    r Y w xY wt9          d          )u  Create a new task and optionally link it under parent tasks.

    Returns the new task id.  Status is ``ready`` when there are no
    parents (or all parents already ``done``), otherwise ``todo``.
    If ``triage=True``, status is forced to ``triage`` regardless of
    parents — a specifier/triager is expected to promote the task to
    ``todo`` once the spec is fleshed out.

    If ``idempotency_key`` is provided and a non-archived task with the
    same key already exists, returns the existing task's id instead of
    creating a duplicate. Useful for retried webhooks / automation that
    should not double-write.

    ``max_runtime_seconds`` caps how long a worker may run before the
    dispatcher SIGTERMs (then SIGKILLs after a grace window) and
    re-queues the task. ``None`` means no cap (default).

    ``skills`` is an optional list of skill names to force-load into
    the worker when dispatched. Stored as JSON; the dispatcher passes
    each name to ``hermes --skills ...`` alongside the built-in
    ``kanban-worker``. Use this to pin a task to a specialist skill
    (e.g. ``skills=["translation"]`` so the worker loads the
    translation skill regardless of the profile's default config).
    ztitle is requiredzworkspace_kind must be one of z, got c              3     K   | ]}||V  	d S rt   r5   rv   r   s     r"   rx   zcreate_task.<locals>.<genexpr>  s'      ,,!!,A,,,,,,r$   N,z!skill name cannot contain comma: zA (pass a list of separate names instead of a comma-joined string)zhSELECT id FROM tasks WHERE idempotency_key = ? AND status != 'archived' ORDER BY created_at DESC LIMIT 1r   r   r   r   zunknown parent task(s): , z&SELECT status FROM tasks WHERE id IN (?)c              3  .   K   | ]}|d          dk    V  dS r   r
   Nr5   rv   rs     r"   rx   zcreate_task.<locals>.<genexpr>  s+      CCq{f4CCCCCCr$   r   a  
                    INSERT INTO tasks (
                        id, title, body, assignee, status, priority,
                        created_by, created_at, workspace_kind, workspace_path,
                        tenant, idempotency_key, max_runtime_seconds, skills,
                        max_retries
                    ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                    DINSERT OR IGNORE INTO task_links (parent_id, child_id) VALUES (?, ?)created)r   r   rL   r   r   r   unreachable)rL  r   r    VALID_WORKSPACE_KINDSr   tupler   r   r   r   r  r(  r   r   ranger?  r)  _find_missing_parentsr{   lenr*  anyr   r   _append_eventr   r  IntegrityErrorRuntimeError)r  r   r   r   r   r   r   r   r   rL   r   r   r   r   r   skills_listcleanedr   r!   r   r   nowattemptr   initial_statusmissingrowspids                               r"   create_taskrn    s   T #8,,H . .,---222&V4I-J-J & &!& &
 
 	
 ,,w,,,,,G (,K 	! 	!A q66<<>>D d{{ X X X X   t||HHTNNNNN4      ll/ 	
 

 (** 	  	t9
dikk

C 88 N N..L	4 E E  4%-NN%,N 4"7g"F"F" ^",-\		RYHZHZ-\-\"]"]]#|| "%((3W+=">">?ADE#    #(**	 
 CCdCCCCC 4-3N  Zg Z3D'BBG Z()XDIIgDVDV)X)XYYY   & "&&'4GQ/000T3>3J
;///PT,7,CK(((  6 #  CLL^g    $,"0#'=="(7B"L${"3"3"3 	  uE E E E E E E E E E E E E E EL NNN% 	 	 	!||H		
 }
%
%%s7   <MF*M5MM	MM		MM*)M*	list[str]c                    t          |          }|sg S d                    dt          |          z            }|                     d| d|                                          }d |D             fd|D             S )NrR  rT  "SELECT id FROM tasks WHERE id IN (rU  c                    h | ]
}|d          S r   r5   rX  s     r"   r"  z(_find_missing_parents.<locals>.<setcomp>R  s    %%%1qw%%%r$   c                    g | ]}|v|	S r5   r5   )rv   r   presents     r"   r   z)_find_missing_parents.<locals>.<listcomp>S  s#    333!!7"2"2A"2"2"2r$   )r   r{   ra  r  r*  )r  rL   placeholdersrl  ru  s       @r"   r`  r`  I  s    7mmG 	88C#g,,.//L<<<\<<<  hjj 	 &%%%%G3333w3333r$   r   Optional[Task]c                    |                      d|f                                          }|rt                              |          nd S )Nz SELECT * FROM tasks WHERE id = ?)r  r(  r   r   r  r   r   s      r"   get_taskrz  V  s@    
,,9G:
F
F
O
O
Q
QC!$.4==$.r$   )r   r   r   r   limitr   r{  
list[Task]c                  d}g }|'|dz  }|                     t          |                     |G|t          vr$t          dt	          t                               |dz  }|                     |           ||dz  }|                     |           |s|dk    r|dz  }|dz  }|r|d	t          |           z  }|                     ||                                          }d
 |D             S )NzSELECT * FROM tasks WHERE 1=1z AND assignee = ?zstatus must be one of z AND status = ?z AND tenant = ?r   z AND status != 'archived'z' ORDER BY priority DESC, created_at ASCz LIMIT c                B    g | ]}t                               |          S r5   )r   r   rX  s     r"   r   zlist_tasks.<locals>.<listcomp>w  s$    +++DMM!+++r$   )r   rL  VALID_STATUSESr    r   r   r  r*  )	r  r   r   r   r   r{  queryparamsrl  s	            r"   
list_tasksr  [  s&    ,EF$$)(33444''Nf^6L6LNNOOO""f""f -* 4 4,,	66E ('3u::'''<<v&&//11D++d++++r$   r   c                   t          |          }t          |           5  |                     d|f                                          }|s	 ddd           dS |d         |d         dk    rt	          d| d          |d	         |k    r|                     d
||f           n|                     d||f           t          | |dd	|i           	 ddd           dS # 1 swxY w Y   dS )zAssign or reassign a task.  Returns True on success.

    Refuses to reassign a task that's currently running (claim_lock set).
    Reassign after the current run completes if needed.
    z;SELECT status, claim_lock, assignee FROM tasks WHERE id = ?NFr   r   r   zcannot reassign zS: currently running (claimed). Wait for completion or reclaim the stale lock first.r   z_UPDATE tasks SET assignee = ?, consecutive_failures = 0, last_failure_error = NULL WHERE id = ?z*UPDATE tasks SET assignee = ? WHERE id = ?assignedT)rL  r)  r  r(  re  rc  )r  r   r   r   s       r"   assign_taskr  z  s    "'**G	4  llIG:
 

(** 	  	        |(S]i-G-GG7 G G G   z?g%% LL9'"    LLEQXGYZZZdGZ*g1FGGG/                 s   -CA9CC#&C#	parent_idchild_idc           	     2   ||k    rt          d          t          |           5  t          | ||g          }|r%t          dd                    |                     t	          | ||          rt          d| d| d          |                     d||f           |                     d|f                                          d	         }|d
k    r|                     d|f           t          | |d||d           d d d            d S # 1 swxY w Y   d S )Nza task cannot depend on itselfzunknown task(s): rS  zlinking z -> z would create a cyclerZ  %SELECT status FROM tasks WHERE id = ?r   r
   zBUPDATE tasks SET status = 'todo' WHERE id = ? AND status = 'ready'linkedrO   r   )r    r)  r`  r{   _would_cycler  r(  rc  )r  r  r  rk  parent_statuss        r"   
link_tasksr    s   H9:::	4 
 
'y(.CDD 	GE71C1CEEFFFi22 	I9II(III   	R!	
 	
 	

 3i\
 

(**X F""LLT   	(H 844	
 	
 	
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   CDDDc                ,   t                      }|g}|r|                                }||k    rdS ||v r#|                    |           |                     d|f                                          }|                    d |D                        |dS )zReturn True if adding parent->child creates a cycle.

    A cycle exists iff ``parent_id`` is already a descendant of
    ``child_id`` via existing parent->child links.  We walk downward
    from ``child_id`` and check whether we reach ``parent_id``.
    Tz3SELECT child_id FROM task_links WHERE parent_id = ?c              3  &   K   | ]}|d          V  dS )r  Nr5   rX  s     r"   rx   z_would_cycle.<locals>.<genexpr>  s&      11qQz]111111r$   F)r   r   r   r  r*  extend)r  r  r  r   stacknoderl  s          r"   r  r    s     55DJE
 
2yy{{944<<||AD7
 

(** 	 	11D111111  
2 5r$   c           	         t          |           5  |                     d||f          }|j        rt          | |d||d           |j        dk    cd d d            S # 1 swxY w Y   d S )Nz;DELETE FROM task_links WHERE parent_id = ? AND child_id = ?unlinkedr  r   )r)  r  r,  rc  )r  r  r  r2  s       r"   unlink_tasksr    s    	4 
  
 llI!
 
 < 	h
$x88   |a
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 s   ?AA #A c                l    |                      d|f                                          }d |D             S )NFSELECT parent_id FROM task_links WHERE child_id = ? ORDER BY parent_idc                    g | ]
}|d          S r  r5   rX  s     r"   r   zparent_ids.<locals>.<listcomp>  s    )))qAkN)))r$   r  r*  r  r   rl  s      r"   
parent_idsr    sA    <<P	
  hjj 	 *)D))))r$   c                l    |                      d|f                                          }d |D             S )NzESELECT child_id FROM task_links WHERE parent_id = ? ORDER BY child_idc                    g | ]
}|d          S )r  r5   rX  s     r"   r   zchild_ids.<locals>.<listcomp>  s    (((aAjM(((r$   r  r  s      r"   	child_idsr    sA    <<O	
  hjj 	 )(4((((r$   list[tuple[str, Optional[str]]]c                l    |                      d|f                                          }d |D             S )zDReturn ``(parent_id, result)`` for every done parent of ``task_id``.z
        SELECT t.id AS id, t.result AS result
        FROM tasks t
        JOIN task_links l ON l.parent_id = t.id
        WHERE l.child_id = ? AND t.status = 'done'
        ORDER BY t.completed_at ASC
        c                .    g | ]}|d          |d         fS )r   r   r5   rX  s     r"   r   z"parent_results.<locals>.<listcomp>  s%    111qQtWak"111r$   r  r  s      r"   parent_resultsr    sE    <<	 

	 	 hjj 	 21D1111r$   r  c           
        |r|                                 st          d          |r|                                 st          d          t          t          j                              }t	          |           5  |                     d|f                                          st          d|           |                     d||                                 |                                 |f          }t          | |d|t          |          d           t          |j	        pd          cd d d            S # 1 swxY w Y   d S )	Nzcomment body is requiredzcomment author is requiredz SELECT 1 FROM tasks WHERE id = ?unknown task QINSERT INTO task_comments (task_id, author, body, created_at) VALUES (?, ?, ?, ?)	commented)r  ra  r   )
r   r    r   r   r)  r  r(  rc  ra  r+  )r  r   r  r   rh  r2  s         r"   add_commentr    s}     5tzz|| 53444 7 75666
dikk

C	4 ' '||.

 

(**	8 6W66777ll"fllnndjjllC8
 

 	dG[VCPTII2V2VWWW3=%A&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 's   :B1D88D<?D<list[Comment]c                l    |                      d|f                                          }d |D             S )NzESELECT * FROM task_comments WHERE task_id = ? ORDER BY created_at ASCc           
     r    g | ]4}t          |d          |d         |d         |d         |d                   5S )r   r   r  r   r   )r   r   r  r   r   )r  rX  s     r"   r   z!list_comments.<locals>.<listcomp>   s\     	 	 	  	wiLX;6	
 	
 	
	 	 	r$   r  r  s      r"   list_commentsr    sN    <<O	
  hjj 		 	 	 	 	 	r$   list[Event]c                   |                      d|f                                          }g }|D ]}	 |d         rt          j        |d                   nd }n# t          $ r d }Y nw xY w|                    t          |d         |d         |d         ||d         d|                                v r|d         t          |d                   nd                      |S )	NzKSELECT * FROM task_events WHERE task_id = ? ORDER BY created_at ASC, id ASCr  r   r   r  r   r  r   r   r  r  r   r  )	r  r*  r   r   r   r   r  r   r   )r  r   rl  outrY  r  s         r"   list_eventsr  ,  s   <<U	
  hjj 	 C 
 
	23I,Hdj9...DGG 	 	 	GGG	

T7)vY\?,4,@,@Qx[E\AhK(((bf  		
 		
 		
 		
 Js   $AA%$A%r  r  r  r   r  c                   t          t          j                              }|rt          j        |d          nd}|                     d|||||f           dS )a2  Record an event row.  Called from within an already-open txn.

    ``run_id`` is optional: pass the current run id so UIs can group
    events by attempt. For events that aren't scoped to a single run
    (task created/edited/archived, dependency promotion) leave it None
    and the row carries NULL.
    Fr   Nz[INSERT INTO task_events (task_id, run_id, kind, payload, created_at) VALUES (?, ?, ?, ?, ?))r   r   r   r   r  )r  r   r  r  r  rh  pls          r"   rc  rc  D  sh     dikk

C4;	EG%	0	0	0	0BLL	!	&$C(    r$   )r   r   r   r   r   r   r   r   c               v   t          t          j                              }|                     d|f                                          }|r|d         sdS t          |d                   }	|                     d|p|||||rt	          j        |d          nd||	f           |                     d|f           |	S )a  Close the currently-active run for ``task_id`` and clear the pointer.

    ``outcome`` is the semantic result (completed / blocked / crashed /
    timed_out / spawn_failed / gave_up / reclaimed). ``status`` is the
    run-row status (usually just ``outcome``, but callers can pass it
    explicitly). Returns the closed run_id or ``None`` if no active run
    existed (e.g. a CLI user calling ``hermes kanban complete`` on a
    task that was never claimed).
    -SELECT current_run_id FROM tasks WHERE id = ?r   Na  
        UPDATE task_runs
           SET status        = ?,
               outcome       = ?,
               summary       = ?,
               error         = ?,
               metadata      = ?,
               ended_at      = ?,
               claim_lock    = NULL,
               claim_expires = NULL,
               worker_pid    = NULL
         WHERE id = ?
           AND ended_at IS NULL
        Fr  z3UPDATE tasks SET current_run_id = NULL WHERE id = ?)r   r   r  r(  r   r   )
r  r   r   r   r   r   r   rh  r   r  s
             r"   _end_runr  \  s    & dikk

C
,,7' hjj   c*+ t%&''FLL	 g8@JDJxe4444d	
  2 	LL=z   Mr$   c                    |                      d|f                                          }|r|d         rt          |d                   nd S )Nr  r   )r  r(  r   ry  s      r"   _current_run_idr    sS    
,,7' hjj  *-P5E1FP3s#$%%%DPr$   )r   r   r   c               `   t          t          j                              }|                     d|f                                          }|r|d         nd}|r|d         nd}	|                     d|||	|||||rt	          j        |d          nd||f
          }
t          |
j        pd          S )	a  Insert a zero-duration, already-closed run row.

    Used when a terminal transition happens on a task that was never
    claimed (CLI user calling ``hermes kanban complete <ready-task>
    --summary X``, or dashboard "mark done" on a ready task). Without
    this, the handoff fields (summary / metadata / error) would be
    silently dropped: ``_end_run`` is a no-op because there's no
    current run.

    The synthetic run has ``started_at == ended_at == now`` so it
    shows up in attempt history as "instant" and doesn't skew elapsed
    stats. Caller is responsible for leaving ``current_run_id`` NULL
    (or for clearing it elsewhere in the same txn) since this
    function does NOT touch the tasks row.
    z9SELECT assignee, current_step_key FROM tasks WHERE id = ?r   Nr   z
        INSERT INTO task_runs (
            task_id, profile, step_key,
            status, outcome,
            summary, error, metadata,
            started_at, ended_at
        ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        Fr  r   )r   r   r  r(  r   r   r+  )r  r   r   r   r   r   rh  trowr   r   r2  s              r"   _synthesize_ended_runr    s    0 dikk

C<<C	
  hjj 	 #'0d:DG+/9t&''TH
,,	 WhWU8@JDJxe4444d	
 C" s}!"""r$   c                   d}t          |           5  |                     d                                          }|D ]z}|d         }|                     d|f                                          }t          d |D                       r.|                     d|f           t	          | |dd           |d	z  }{	 ddd           n# 1 swxY w Y   |S )
zPromote ``todo`` tasks to ``ready`` when all parents are ``done``.

    Returns the number of tasks promoted.  Safe to call inside or outside
    an existing transaction; it opens its own IMMEDIATE txn.
    r   z*SELECT id FROM tasks WHERE status = 'todo'r   zYSELECT t.status FROM tasks t JOIN task_links l ON l.parent_id = t.id WHERE l.child_id = ?c              3  .   K   | ]}|d          dk    V  dS rW  r5   rQ  s     r"   rx   z"recompute_ready.<locals>.<genexpr>  s+      ::Q1X;&(::::::r$   zBUPDATE tasks SET status = 'ready' WHERE id = ? AND status = 'todo'r$  Nr   )r)  r  r*  allrc  )r  r$  	todo_rowsr   r   rL   s         r"   recompute_readyr    s>    H	4  LL8
 

(** 	  	 	C$iGll' 
	 
 hjj  ::'::::: XJ   dGZ>>>A		              & Os   B%CCC)ttl_secondsclaimerr  r  c               P   t          t          j                              }|pt                      }|t          |          z   }t          |           5  |                     d|f                                          }|r3|d         r+|                     d|t          |d                   f           |                     d||||f          }|j        dk    r	 ddd           dS |                     d|f                                          }	|                     d||	r|	d	         nd|	r|	d
         nd|||	r|	d         nd|f          }
|
j        }|                     d||f           t          | |d|||d|           t          | |          cddd           S # 1 swxY w Y   dS )zAtomically transition ``ready -> running``.

    Returns the claimed ``Task`` on success, ``None`` if the task was
    already claimed (or is not in ``ready`` status).
    zBSELECT current_run_id FROM tasks WHERE id = ? AND status = 'ready'r   av  
                UPDATE task_runs
                   SET status = 'reclaimed', outcome = 'reclaimed',
                       summary = COALESCE(summary, 'invariant recovery on re-claim'),
                       ended_at = ?,
                       claim_lock = NULL, claim_expires = NULL, worker_pid = NULL
                 WHERE id = ? AND ended_at IS NULL
                a?  
            UPDATE tasks
               SET status        = 'running',
                   claim_lock    = ?,
                   claim_expires = ?,
                   started_at    = COALESCE(started_at, ?)
             WHERE id = ?
               AND status = 'ready'
               AND claim_lock IS NULL
            r   NzNSELECT assignee, max_runtime_seconds, current_step_key FROM tasks WHERE id = ?z
            INSERT INTO task_runs (
                task_id, profile, step_key, status,
                claim_lock, claim_expires, max_runtime_seconds,
                started_at
            ) VALUES (?, ?, ?, 'running', ?, ?, ?, ?)
            r   r   r   z0UPDATE tasks SET current_run_id = ? WHERE id = ?claimed)lockexpiresr  r  )
r   r   rG  r)  r  r(  r,  r+  rc  rz  )r  r   r  r  rh  r  r  staler2  r  run_curr  s               r"   
claim_taskr    s    dikk

C#kmmDC$$$G	4 G' G'
 PJ
 
 (** 	  	U+, 	LL c% 01223
 
 
 ll	 7C)
 
 <1GG' G' G' G' G' G' G' G'L ||&J
 
 (**	 	
 ,, $(2Z  d,0:'((d/3=*++
 
$ ">W	
 	
 	
 	'9g@@	
 	
 	
 	

 g&&OG' G' G' G' G' G' G' G' G' G' G' G' G' G' G' G' G' G's   BF$B*FF"Fc                  t          t          j                              t          |          z   }|pt                      }t          |           5  |                     d|||f          }|j        dk    r8t          | |          }||                     d||f           	 ddd           dS 	 ddd           dS # 1 swxY w Y   dS )zExtend a running claim.  Returns True if we still own it.

    Workers that know they'll exceed 15 minutes should call this every
    few minutes to keep ownership.
    zYUPDATE tasks SET claim_expires = ? WHERE id = ? AND status = 'running' AND claim_lock = ?r   Nz3UPDATE task_runs SET claim_expires = ? WHERE id = ?TF)r   r   rG  r)  r  r,  r  )r  r   r  r  r  r  r2  r  s           r"   heartbeat_claimr  M  s<    $)++[!1!11G#kmmD	4  llEgt$
 

 <1$T733F!If%                            s   AB;-B;;B?B?	signal_fnc               n   t          t          j                              }d}|                     d|f                                          }|D ]}t	          |d         |d         |          }t          |           5  |                     d|d         |d         |f          }|j        dk    r	 d	d	d	           mt          | |d         d
d
d|d          |          }d|d         i}	|	                    |           t          | |d         d
|	|           |dz  }d	d	d	           n# 1 swxY w Y   |S )zReset any ``running`` task whose claim has expired.

    Returns the number of stale claims reclaimed.  Safe to call often.
    r   zySELECT id, claim_lock, worker_pid FROM tasks WHERE status = 'running' AND claim_expires IS NOT NULL AND claim_expires < ?r   r   r  zUPDATE tasks SET status = 'ready', claim_lock = NULL, claim_expires = NULL, worker_pid = NULL WHERE id = ? AND status = 'running' AND claim_lock IS ? AND claim_expires IS NOT NULL AND claim_expires < ?r   r   N	reclaimedzstale_lock=r   r   r   r   
stale_lockr  )
r   r   r  r*  _terminate_reclaimed_workerr)  r,  r  r   rc  )
r  r  rh  r  r  r   terminationr2  r  r  s
             r"   release_stale_claimsr  l  s    dikk

CILL	W	  hjj	 

   1s<0I
 
 
 t__ 	 	,,F TC-s3 C |q  	 	 	 	 	 	 	 c$i#K7C$577$	  F $S%67GNN;'''c$i   
 NI/	 	 	 	 	 	 	 	 	 	 	 	 	 	 	0 s   =2D);A"D))D-	0D-	)reasonr  r  c          	     8   |                      d|f                                          }|sdS |d         dk    r
|d         dS |d         }t          |d         ||          }t          |           5  |                      d	||f          }|j        d
k    r	 ddd           dS t          | |dd|rd| nd| |          }d||d}	|	                    |           t          | |d|	|           ddd           n# 1 swxY w Y   t          | |           dS )a!  Operator-driven reclaim: release the claim and reset to ``ready``.

    Unlike :func:`release_stale_claims` which only acts on tasks whose
    ``claim_expires`` has passed, this function reclaims immediately
    regardless of TTL. Intended for the dashboard/CLI recovery flow
    when an operator wants to abort a running worker without waiting
    for the TTL to expire (e.g. after seeing a hallucination warning).

    Returns True if a reclaim happened, False if the task isn't in a
    reclaimable state (not running, or doesn't exist).
    z=SELECT status, claim_lock, worker_pid FROM tasks WHERE id = ?Fr   r   r   Nr   r  zUPDATE tasks SET status = 'ready', claim_lock = NULL, claim_expires = NULL, worker_pid = NULL WHERE id = ? AND status IN ('running', 'ready', 'blocked') AND claim_lock IS ?r   r  zmanual_reclaim: zmanual_reclaim lock=r  T)manualr  	prev_lockr  )	r  r(  r  r)  r,  r  r   rc  _clear_failure_counter)
r  r   r  r  r   r  r  r2  r  r  s
             r"   reclaim_taskr    s   $ ,,G	
  hjj   u
8}	!!c,&7&?uL!I-L9	  K 
4 
 
ll" i 
 
 <1
 
 
 
 
 
 
 
 '/5 8+6+++7I77 
 
 
 "
 

 	{###';	
 	
 	
 	
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
D 4)))4s   3%C?%AC??DD)reclaim_firstr  r  c               t    |rt          | ||pd           	 t          | ||          S # t          $ r Y dS w xY w)a  Reassign a task, optionally reclaiming a stuck running worker first.

    This is the recovery path for "this profile's model is broken, try
    a different one". If ``reclaim_first`` is True, any active claim is
    released (via :func:`reclaim_task`) before the reassign happens;
    otherwise the function refuses to reassign a currently-running task
    and returns False (caller can retry with ``reclaim_first=True``).

    Returns True if the reassign landed. ``profile`` may be ``None`` to
    unassign entirely.
    reassign)r  F)r  r  re  )r  r   r   r  r  s        r"   reassign_taskr    sb    &  AT76+?Z@@@@4'222    uus   ) 
77completing_task_idclaimed_idstuple[list[str], list[str]]c                t   d |pg D             }|sg g fS t                      }g }|D ]0}||vr*|                    |           |                    |           1|                     d|f                                          }|g |fS |d         }d                    dgt          |          z            }	|                     d|	 dt          |                                                    }
d	 |
D             }t          t          | |                    }g }g }|D ]}|
                    |          }||                    |           /|r||k    r|                    |           M||k    r|                    |           i||v r|                    |           |                    |           ||fS )
u  Partition ``claimed_ids`` into (verified, phantom).

    A card is "verified" iff a row exists in ``tasks`` AND at least one
    of the following holds:

    * ``created_by`` matches the completing task's ``assignee`` profile
      (the common case: worker A spawns a card via ``kanban_create``,
      which stamps ``created_by=A``).
    * ``created_by`` matches the completing task's id (edge case where
      a worker passed its own task id as the ``created_by`` value).
    * The card is linked as a ``task_links.child`` of the completing
      task — i.e. the worker explicitly called ``kanban_create`` with
      ``parents=[<current_task>]``. This accepts cards created through
      the dashboard/CLI by a different principal but then attached to
      the completing task by the worker.

    ``phantom`` returns ids that either don't exist at all, or exist
    but don't satisfy any of the three trust conditions. The caller
    decides what to do with each bucket; this helper never mutates.
    c                    g | ]D}t          |                                          #t          |                                          ES r5   )r   r   )rv   xs     r"   r   z)_verify_created_cards.<locals>.<listcomp>  s9    MMM!c!ffllnnMs1vv||~~MMMr$   z'SELECT assignee FROM tasks WHERE id = ?Nr   rR  rT  z.SELECT id, created_by FROM tasks WHERE id IN (rU  c                ,    i | ]}|d          |d         S )r   r   r5   rX  s     r"   
<dictcomp>z)_verify_created_cards.<locals>.<dictcomp>1  s"    444!QtWao444r$   )r   r   r   r  r(  r{   ra  r^  r*  r  r,   )r  r  r  r  r   orderedcidr   completing_assigneerv  rl  foundlinked_childrenverifiedphantomr   s                   r"   _verify_created_cardsr    s   2 NM(9rMMMG 2vUUDG    d??HHSMMMNN3
,,14F3H hjj  {7{j/ 88SECLL011L<<HHHHg  hjj 	 54t444E !$Id4F$G$G H HOHG    YYs^^
NN3 	 :1D#D#DOOC    ---OOC    O##OOC    NN3Wr$   z\bt_[a-f0-9]{8,}\btextc                   |sg S t                               |          }|sg S t                      }g }|D ]0}||vr*|                    |           |                    |           1d                    dgt          |          z            }|                     d| dt          |                    	                                }d |D             fd|D             S )ue  Regex-scan free-form text for ``t_<hex>`` references; return the
    ones that don't exist in ``tasks``.

    Used as a non-blocking advisory check on completion summaries. An
    empty return means "no suspicious references found" — either the
    text had no IDs at all, or every ID it mentioned resolves to a real
    task. Duplicates are deduped.
    rR  rT  rq  rU  c                    h | ]
}|d          S rs  r5   rX  s     r"   r"  z._scan_prose_for_phantom_ids.<locals>.<setcomp>m  s    &&&A$&&&r$   c                    g | ]}|v|	S r5   r5   )rv   mexistings     r"   r   z/_scan_prose_for_phantom_ids.<locals>.<listcomp>n  s#    333!(!2!2A!2!2!2r$   )
_TASK_ID_PROSE_REfindallr   r   r   r{   ra  r  r^  r*  )	r  r  matchesr   uniquer  rv  rl  r  s	           @r"   _scan_prose_for_phantom_idsr  P  s      	''--G 	UUDF  D==HHQKKKMM!88SECKK/00L<<<\<<<f  hjj 	 '&&&&H3333v3333r$   c                  $     e Zd ZdZd fdZ xZS )HallucinatedCardsErroraO  Raised by ``complete_task`` when ``created_cards`` contains ids
    that don't exist or weren't created by the completing worker.

    The phantom list is attached as ``.phantom`` for callers that want
    structured access. Kept as ``ValueError`` subclass so existing
    tool-error handlers treat it as a recoverable user error.
    r  ro  r  r   c                    t          |          | _        || _        t                                          dd                    |                      d S )Nz`completion blocked: claimed created_cards that do not exist or were not created by this worker: rS  )r   r  r  super__init__r{   )selfr  r  	__class__s      r"   r  zHallucinatedCardsError.__init__z  s_    G}}"4H3799W3E3EH H	
 	
 	
 	
 	
r$   )r  ro  r  r   )r   r   r   r   r  __classcell__)r  s   @r"   r  r  q  sG         
 
 
 
 
 
 
 
 
 
r$   r  )r   r   r   created_cardsexpected_run_idr   r  r  c               6   t          t          j                              }|rt          | ||          \  }|rt          |           5  t	          | |d||s|r8|p|pd                                                                d         dd         ndd           ddd           n# 1 swxY w Y   t          ||          ng t          |           5  ||                     d|||f          }	n'|                     d|||t          |          f          }	|	j	        d	k    r	 ddd           d
S t          | |dd||n||          }
|
|s|s|rt          | |d||n||          }
||n|pd}|r4|                                                                d         dd         nd}|rt          |          nd|pdd}r|d<   t	          | |d||
           ddd           n# 1 swxY w Y   d                    t          d||g                    }|r^t          | |          }fd|D             }|r>t          |           5  t	          | |d|dd|
           ddd           n# 1 swxY w Y   t!          | |           t#          |            dS )u  Transition ``running|ready -> done`` and record ``result``.

    Accepts a task that is merely ``ready`` too, so a manual CLI
    completion (``hermes kanban complete <id>``) works without requiring
    a claim/start/complete sequence.

    ``summary`` and ``metadata`` are stored on the closing run (if any)
    and surfaced to downstream children via :func:`build_worker_context`.
    When ``summary`` is omitted we fall back to ``result`` so single-run
    callers do not have to pass both. ``metadata`` is a free-form dict
    (e.g. ``{"changed_files": [...], "tests_run": [...]}``) — workers
    are encouraged to use it for structured handoff facts.

    ``created_cards`` is an optional list of task ids the completing
    worker claims to have created. Each id is verified against
    ``tasks.created_by``. If any id is phantom (does not exist or was
    not created by this worker's assignee profile), completion is blocked
    with a ``HallucinatedCardsError`` and a
    ``completion_blocked_hallucination`` event is emitted so the rejected
    attempt is auditable. When all ids verify, they are recorded on the
    ``completed`` event payload.

    After a successful completion, ``summary`` and ``result`` are scanned
    for prose references like ``t_deadbeefcafe`` that do not resolve.
    Any suspected phantom references are recorded as a
    ``suspected_hallucinated_references`` event. This pass is advisory
    and never blocks.
     completion_blocked_hallucinationr'   r   N   )phantom_cardsverified_cardssummary_previewa  
                UPDATE tasks
                   SET status       = 'done',
                       result       = ?,
                       completed_at = ?,
                       claim_lock   = NULL,
                       claim_expires= NULL,
                       worker_pid   = NULL
                 WHERE id = ?
                   AND status IN ('running', 'ready', 'blocked')
                a  
                UPDATE tasks
                   SET status       = 'done',
                       result       = ?,
                       completed_at = ?,
                       claim_lock   = NULL,
                       claim_expires= NULL,
                       worker_pid   = NULL
                 WHERE id = ?
                   AND status IN ('running', 'ready', 'blocked')
                   AND current_run_id = ?
                r   F	completedr
   )r   r   r   r   r   r   r     )
result_lenr   r  r  rr   c                6    g | ]}|t                    v|S r5   r   )rv   r   r  s     r"   r   z!complete_task.<locals>.<listcomp>	  s+    PPPa1C<O<O3O3O3O3O3Or$   !suspected_hallucinated_referencescompletion_summary)phantom_refssourceT)r   r   r  r)  rc  r   
splitlinesr  r  r,  r  r  ra  r{   filterr  r  r  )r  r   r   r   r   r  r  rh  r
  r2  r  
ev_summarycompleted_payload	scan_textr  r  s                  @r"   complete_taskr    s   L dikk

C  (='=)
 )
%  	A4  '#E)6*8 !(&+1&W44";;==HHJJ1MdsdSS!%                  )@@@	A  	4 C
 C
",,
 g& CC ,, gs?';';< C <1CC
 C
 C
 C
 C
 C
 C
 C
D '&2GG	
 
 
 >w>(>f>*g##*#6F!	  F ")!4gg&GR
AKSZ%%''2244Q7==QS
)/6#f+++Q!)T#
 #
  	A2@./';	
 	
 	
 	
C
 C
 C
 C
 C
 C
 C
 C
 C
 C
 C
 C
 C
 C
 C
P w&78899I 24CC QPPP<PPP 		4  '#F(4"6  "                  4)))D4s?   	AB((B,/B,AG*2B,G**G.1G.I//I36I3)r   r   c          
     2   ||n|}t          |           5  |                     d|f                                          }|r|d         dk    r	 ddd           dS |                     d||f           |                     d|f                                          }|rt          |d                   nd}|t	          | |d	||
          }nF|                     d||f           |,|                     dt          j        |d          |f           |r4|                                                                d         dd         nd}	t          | |dddg|dgng z   |rt          |          nd|	pdd|           ddd           n# 1 swxY w Y   dS )z?Backfill the user-visible result for an already completed task.Nr  r   r
   Fz(UPDATE tasks SET result = ? WHERE id = ?z
            SELECT id FROM task_runs
             WHERE task_id = ?
               AND outcome = 'completed'
             ORDER BY COALESCE(ended_at, started_at, 0) DESC, id DESC
             LIMIT 1
            r   r  r  z-UPDATE task_runs SET summary = ? WHERE id = ?z.UPDATE task_runs SET metadata = ? WHERE id = ?r  r   r  r'   editedr   r   r   )fieldsr  r   r  T)r)  r  r(  r   r  r   r   r   r  rc  ra  )
r  r   r   r   r   handoff_summaryr   runr  r  s
             r"   edit_completed_task_resultr"  )	  s{    ")!4gg&O	4 5
 5
ll3gZ
 

(** 	  	c(mv--5
 5
 5
 5
 5
 5
 5
 5
 	6W	
 	
 	
 ll J	
 	
 (** 	 $'0SYD>*g#'!	  FF LL? &)   #DZu===vF   'O!!##..003DSD99$& 	 	'8 y)'/';
||E .4:c&kkk%-  	
 	
 	
 	
U5
 5
 5
 5
 5
 5
 5
 5
 5
 5
 5
 5
 5
 5
 5
l 4s   9FD$FFF)r  r  c                  t          |           5  ||                     d|f          }n%|                     d|t          |          f          }|j        dk    r	 ddd           dS t	          | |dd|          }||rt          | |d|          }t          | |dd	|i|
           	 ddd           dS # 1 swxY w Y   dS )z"Transition ``running -> blocked``.Na6  
                UPDATE tasks
                   SET status       = 'blocked',
                       claim_lock   = NULL,
                       claim_expires= NULL,
                       worker_pid   = NULL
                 WHERE id = ?
                   AND status IN ('running', 'ready')
                a`  
                UPDATE tasks
                   SET status       = 'blocked',
                       claim_lock   = NULL,
                       claim_expires= NULL,
                       worker_pid   = NULL
                 WHERE id = ?
                   AND status IN ('running', 'ready')
                   AND current_run_id = ?
                r   Fr   r   r   r   )r   r   r  r  T)r)  r  r   r,  r  r  rc  )r  r   r  r  r2  r  s         r"   
block_taskr%  l	  sr    
4 , ,",, 
 CC ,,	 #o../ C <1;, , , , , , , ,< 'i
 
 
 >f>*g!  F
 	dGY60B6RRRRY, , , , , , , , , , , , , , , , , ,s   AB8)AB88B<?B<c           	        t          t          j                              }t          |           5  |                     d|f                                          }|r3|d         r+|                     d|t          |d                   f           |                     d|f          }|j        dk    r	 ddd           dS t          | |dd           	 ddd           d	S # 1 swxY w Y   dS )
u  Transition ``blocked -> ready``.

    Defensively closes any stale ``current_run_id`` pointer before flipping
    status. In the common path (``block_task`` closed the run already) this
    is a no-op. If a future or external write left the pointer dangling,
    the leaked run is closed as ``reclaimed`` inside the same txn so the
    runs invariant (``current_run_id IS NULL`` ⇔ run row in terminal
    state) holds for the rest of this function's lifetime.
    zDSELECT current_run_id FROM tasks WHERE id = ? AND status = 'blocked'r   au  
                UPDATE task_runs
                   SET status = 'reclaimed', outcome = 'reclaimed',
                       summary = COALESCE(summary, 'invariant recovery on unblock'),
                       ended_at = ?,
                       claim_lock = NULL, claim_expires = NULL, worker_pid = NULL
                 WHERE id = ? AND ended_at IS NULL
                z\UPDATE tasks SET status = 'ready', current_run_id = NULL WHERE id = ? AND status = 'blocked'r   NF	unblockedT)r   r   r)  r  r(  r,  rc  )r  r   rh  r  r2  s        r"   unblock_taskr(  	  sl    dikk

C	4  RJ
 
 (** 	  	U+, 	LL c% 01223
 
 
 ll2J
 

 <1/       0 	dG[$7773                 s   BC?CC#&C#)r   r   r  c                  |#|                                 st          d          t          |           5  |                     d|f                                          }|	 ddd           dS dg}g }g }|q|                                 |d         pdk    rQ|                    d           |                    |                                            |                    d           |O|pd|d	         pdk    r?|                    d
           |                    |           |                    d	           |                    |           |                     dd                    |           dt          |                    }	|	j        dk    r	 ddd           dS |ry|rw|                                 rc|                     d||                                 dd                    |          z   dz   t          t          j
                              f           t          | |d|rd|ind           ddd           n# 1 swxY w Y   t          |            dS )u&  Flesh out a triage task and promote it to ``todo``.

    Atomically updates ``title`` / ``body`` (when provided) and transitions
    ``status: triage -> todo`` in a single write txn. Returns False when
    the task is missing or not in the ``triage`` column — callers should
    surface that as "nothing to specify" rather than an error.

    ``todo`` (not ``ready``) is the correct landing column: ``recompute_ready``
    promotes parent-free / parent-done todos to ``ready`` on the next
    dispatcher tick, which keeps the normal parent-gating behaviour intact
    for specified tasks that happen to have open parents.

    ``author`` is recorded on an audit comment only when at least one of
    ``title`` / ``body`` actually changed — avoids noisy comment spam for
    status-only promotions.
    Nztitle cannot be blankz@SELECT title, body FROM tasks WHERE id = ? AND status = 'triage'Fzstatus = 'todo'r   r'   z	title = ?r   zbody = ?zUPDATE tasks SET rS  z# WHERE id = ? AND status = 'triage'r   r  u   Specified — updated z and promoted to todo.	specifiedchanged_fieldsT)r   r    r)  r  r(  r   r{   r^  r,  r   r   rc  r  )
r  r   r   r   r  r  setsr  r+  r2  s
             r"   specify_triage_taskr-  	  s   0 0111	4 1
 1
<<NJ
 
 (** 	 1
 1
 1
 1
 1
 1
 1
 1
 --$&8G3D3J!K!KKK$$$MM%++--(((!!'***&1A1GR H HKK
###MM$!!&)))gll2		$ 2 2 2&MM
 

 <131
 1
 1
 1
 1
 1
 1
 1
4  	f 	 	 LL& LLNN,ii//0./ 	$$   	2@J~..d		
 	
 	
Y1
 1
 1
 1
 1
 1
 1
 1
 1
 1
 1
 1
 1
 1
 1
n D4s   -I/D&I"BIIIc                   t          |           5  |                     d|f          }|j        dk    r	 d d d            dS t          | |ddd          }t	          | |dd |           	 d d d            d	S # 1 swxY w Y   d S )
NzUPDATE tasks SET status = 'archived',     claim_lock = NULL, claim_expires = NULL, worker_pid = NULL WHERE id = ? AND status != 'archived'r   Fr  z#task archived with run still activer$  r   r  T)r)  r  r,  r  rc  )r  r   r2  r  s       r"   archive_taskr/  
  s   	4  ll4 J	
 
 <1        '9
 
 

 	dGZfEEEE%                 s   $A7)A77A;>A;taskc                  | j         pd}|dk    r| j        r[t          | j                                                  }|                                s t          d| j         d| j        d          nt          |          | j        z  }|                    dd           |S |dk    r| j        st          d| j         d	          t          | j                                                  }|                                s t          d| j         d| j        d
          |                    dd           |S |dk    r| j        st          j	                    dz  | j        z  S t          | j                                                  }|                                s t          d| j         d| j        d          |S t          d|           )u  Resolve (and create if needed) the workspace for a task.

    - ``scratch``: a fresh dir under ``<board-root>/workspaces/<id>/``,
      where ``<board-root>`` is the active board's root. The path is the
      same for the dispatcher and every profile worker, so handoff is
      path-stable.
    - ``dir:<path>``: the path stored in ``workspace_path``.  Created
      if missing.  MUST be absolute — relative paths are rejected to
      prevent confused-deputy traversal where ``../../../tmp/attacker``
      resolves against the dispatcher's CWD instead of a meaningful
      root.  Users who want a kanban-root-relative workspace should
      compute the absolute path themselves.
    - ``worktree``: a git worktree at ``workspace_path``.  Not created
      automatically in v1 -- the kanban-worker skill documents
      ``git worktree add`` as a worker-side step.  Returns the intended path.

    Persist the resolved path back to the task row via ``set_workspace_path``
    so subsequent runs reuse the same directory.
    r   task z! has non-absolute workspace_path z"; workspace paths must be absoluter   TrK   r   z- has workspace_kind=dir but no workspace_pathzR; use an absolute path (relative paths are ambiguous against the dispatcher's CWD)r   z
.worktreesz  has non-absolute worktree path z; use an absolute pathzunknown workspace_kind: )
r   r   r   r-   is_absoluter    r   rj   rP   cwd)r0  rY   r  r   s       r"   resolve_workspacer5  9
  sG   ( +)Dy 	7 T())4466A==??  QDG Q Q*Q Q Q    e,,,tw6A	t,,,u}}" 	NNNN   $%%0022}} 	O O O&O O O  
 	
t,,,z" 	78::,tw66$%%0022}} 	A A A&A A A   
666
7
77r$   rR   
Path | strc                    t          |           5  |                     dt          |          |f           d d d            d S # 1 swxY w Y   d S )Nz0UPDATE tasks SET workspace_path = ? WHERE id = ?)r)  r  r   )r  r   rR   s      r"   set_workspace_pathr8  y
  s     
4 
 
>YY 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   &AA
Ar   i    c                     e Zd ZU dZdZded<   dZded<    ee          Z	ded<   	  ee          Z
d	ed
<   	  ee          Zd	ed<   	  ee          Zd	ed<   	  ee          Zd	ed<   	  ee          Zd	ed<   dS )DispatchResultz&Outcome of a single ``dispatch`` pass.r   r   r  r$  )default_factoryzlist[tuple[str, str, str]]spawnedro  skipped_unassignedskipped_nonspawnablecrashedauto_blocked	timed_outN)r   r   r   r   r  r   r$  r   r   r<  r=  r>  r?  r@  rA  r5   r$   r"   r:  r:  
  s        00IH*/%*E*E*EGEEEEB$)E$$?$?$?????L&+eD&A&A&AAAAAJ t444G4444B#eD999L9999E 5666I6666BBr$   r:  iX  z'dict[int, tuple[int, float]]'_recent_worker_exitsrm  
raw_statusc                n   | r| dk    rdS t          j                     }t          |          |ft          t          |           <   t          t                    t          dz  k    rM|t
          z
  fdt                                          D             D ]}t                              |d           t          t                    t          k    rdt          t                                          d           }|dt          |          dz           D ]"\  }}t                              |d           !dS dS )zRecord a reaped child's exit status for later classification.

    Called from the reap loop in ``dispatch_once``. Safe to call many
    times; duplicate pids overwrite (pids can cycle, latest wins).
    r   Nr   c                ,    g | ]\  }\  }}|k     |S r5   r5   )rv   r   _stcutoffs       r"   r   z'_record_worker_exit.<locals>.<listcomp>
  s&    TTT:1gr1VQr$   c                    | d         d         S )Nr   r5   )kvs    r"   r   z%_record_worker_exit.<locals>.<lambda>
  s    beAh r$   r   )	r   r   rB  ra  _RECENT_WORKER_EXITS_MAX_RECENT_WORKER_EXIT_TTL_SECONDSitemsr   r   )rm  rC  rh  _pidr  ry   rH  s         @r"   _record_worker_exitrO  
  s;     #((
)++C&)*oos%;S"
  #;q#@@@66TTTT)=)C)C)E)ETTT 	1 	1D $$T40000
  #;;;-3355;N;NOOO2W!223 	1 	1GD! $$T40000	 <;	1 	1r$   'tuple[str, Optional[int]]'c                F   t                               t          |                     }|dS |\  }}	 t          j        |          r t          j        |          }|dk    rdS d|fS t          j        |          rdt          j        |          fS n# t          $ r Y nw xY wdS )u  Classify a recently-reaped worker by pid.

    Returns ``(kind, code)`` where ``kind`` is one of:

    * ``"clean_exit"`` — ``WIFEXITED`` with ``WEXITSTATUS == 0``. When the
      task is still ``running`` in the DB, this is a protocol violation
      (worker exited without calling ``kanban_complete`` / ``kanban_block``)
      and should be auto-blocked immediately — retrying will just loop.
    * ``"nonzero_exit"`` — ``WIFEXITED`` with non-zero status. Real error.
    * ``"signaled"`` — ``WIFSIGNALED`` (OOM killer, SIGKILL, etc). Real crash.
    * ``"unknown"`` — pid was not in the reap registry (either reaped by
      something else, or died between reap tick and liveness check). Fall
      back to existing crashed-counter behavior.

    ``code`` is the exit status (for ``clean_exit`` / ``nonzero_exit``) or
    the signal number (for ``signaled``), or ``None`` for ``unknown``.
    N)rA  Nr   )
clean_exitr   nonzero_exitsignaled)	rB  r,   r   r*   	WIFEXITEDWEXITSTATUSWIFSIGNALEDWTERMSIGr   )rm  entryr   ry   codes        r"   _classify_worker_exitr[  
  s    $ !$$SXX..E}  FC	< 	*>#&&Dqyy(("D))># 	2C 0 011	2   s   .B "B &)B 
BBc                   | r| dk    rdS 	 t          t          d          r"t          j        t          |           d           n)# t          $ r Y dS t
          $ r Y dS t          $ r Y dS w xY wt          j        dk    r	 t          dt          |            dd          5 }|D ]E}|
                    d	          r.d
|                    dd          d         v r ddd           dS  nFddd           n# 1 swxY w Y   n# t          t
          t          f$ r Y nw xY wt          j        dk    r	 t          j        ddddt          t          |                     gt          j        t          j        ddd          }|j        dk    rdS d
|j        pd                                v rdS n"# t          t          j        t,          f$ r Y nw xY wdS )ug  Return True if ``pid`` is still running on this host.

    Cross-platform: uses ``os.kill(pid, 0)`` on POSIX and ``OpenProcess``
    on Windows. Returns False for falsy PIDs or on any OS error.

    **Zombie handling:** ``os.kill(pid, 0)`` succeeds against
    zombie processes (post-exit, pre-reap) because the process table
    entry still exists. A worker that exits without being reaped by its
    parent would stay "alive" to the dispatcher forever. Dispatcher
    workers are started via ``start_new_session=True`` + intentional
    Popen handle abandonment, so init reaps them quickly — but during
    the window between exit and reap, we'd otherwise see stale "alive"
    signals. On Linux we peek at ``/proc/<pid>/status`` and treat
    ``State: Z`` as dead. On macOS we ask ``ps`` for the BSD ``stat``
    field and treat values containing ``Z`` as dead.
    r   FkillTlinuxz/proc/z/statusrY  zState:ZrB  r   Ndarwinpsz-ozstat=-p)stdoutstderrr  r  checkr'   )hasattrr*   r]  r   ProcessLookupErrorPermissionErrorrB   sysplatformopen
startswithr}   rW   
subprocessr!  r   PIPEDEVNULL
returncoderc  r   SubprocessErrorTimeoutError)rm  rF   lineprocs       r"   
_pid_aliveru  
  s   "  #((u	2v 	!GCHHa      uu   tt   uu |w	0s3xx000#66 !  Dx00 $**S!"4"4Q"777#(        	               "?G< 	 	 	 D		
 
	!	!	>tWdCCMM:!!)  D !##ut{(b//1111u 23\B 	 	 	D	 4sx   7A 
A*	A*	A*)A*>!D  9C4D  %C4(D  4C88D  ;C8<D   DD.AF( F( (GGr   dict[str, Any]c                  ddl }| rt          |           ndddddd}| r| dk    s|s|S t                                          dd          d          d}t	          |                              |          s|S d|d<   ||n"t          t          d	          rt          j        nd}||S d|d
<   	  |t          |           |j	                   n# t          t          f$ r |cY S w xY wt          d          D ].}t          |           s	d|d<   |c S t          j        d           /t          |           r>	  |t          |           |j                   d|d<   n# t          t          f$ r |cY S w xY wt          |            |d<   |S )z<Best-effort host-local worker termination for reclaim paths.r   NF)prev_pid
host_localtermination_attempted
terminatedsigkillrB  r   Try  r]  rz  r   r{        ?r|  )signalr   rG  r}   r   rl  rf  r*   r]  SIGTERMrg  rB   r_  ru  r   sleepSIGKILL)rm  r   r  r~  infohost_prefixr]  ry   s           r"   r  r  6  s    MMM !$-CHHH!& D  #((*( ]]((a003666Kz??%%k22 D!-992v&&0D 	 |$(D	 !SXXv~&&&&(    2YY  # 	!%DKKK
3# 	DS6>***"DOO"G, 	 	 	KKK	 (__,DKs$   /C C$#C$5#E E/.E/)noter  r  c          	        t          t          j                              }t          |           5  ||                     d||f          }n&|                     d||t          |          f          }|j        dk    r	 ddd           dS |t          |          nt          | |          }||                     d||f           t          | |d|rd|ind|	           ddd           n# 1 swxY w Y   d
S )a  Record a ``heartbeat`` event + touch ``last_heartbeat_at``.

    Called by long-running workers as a liveness signal orthogonal to
    the PID check. A worker that forks a long-lived child (train loop,
    video encode, web crawl) can have its Python still alive while the
    actual work process is stuck; periodic heartbeats catch that.

    Returns True on success, False if the task is not in a state that
    should be heartbeating (not running, or claim expired).
    NzJUPDATE tasks SET last_heartbeat_at = ? WHERE id = ? AND status = 'running'zaUPDATE tasks SET last_heartbeat_at = ? WHERE id = ? AND status = 'running' AND current_run_id = ?r   Fz7UPDATE task_runs SET last_heartbeat_at = ? WHERE id = ?	heartbeatr  r  T)r   r   r)  r  r,  r  rc  )r  r   r  r  rh  r2  r  s          r"   heartbeat_workerr  k  s   " dikk

C	4 
 
",,6g CC ,,Mgs?334 C
 <1
 
 
 
 
 
 
 
" *     w// 	
 LLIf   	';",VTNN	
 	
 	
 	
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
< 4s   AC,AC,,C03C0c                  ddl }g }t          t          j                              }t                                          dd          d          d}|                     d                                          }|D ]Y}|d         pd}|                    |          s#|t          |d                   z
  }	|	t          |d	                   k     rUt          |d
                   }
|d         }d}||n"t          t          d          rt          j
        nd}|	  ||
|j                   n# t          t          f$ r Y nw xY wt          d          D ]'}t          |
          s nt          j        d           (t          |
          r,	  ||
|j                   d}n# t          t          f$ r Y nw xY wt%          |           5  |                     d|f          }|j        dk    r|
t          |	          t          |d	                   |d}t)          | |dddt          |	           dt          |d	                    d|          }t+          | |d||           |                    |           ddd           n# 1 swxY w Y   |j        dk    r@t/          | |dt          |	           dt          |d	                    dddd|
|d           [|S )uR  Terminate workers whose per-task ``max_runtime_seconds`` has elapsed.

    Sends SIGTERM, waits a short grace window, then SIGKILL. Emits a
    ``timed_out`` event and drops the task back to ``ready`` so the next
    dispatcher tick re-spawns it — unless the spawn-failure circuit
    breaker has already given up, in which case the task stays blocked
    where ``_record_spawn_failure`` parked it.

    Runs host-local: only tasks claimed by this host are candidates
    (same reasoning as ``detect_crashed_workers``). ``signal_fn`` is a
    test hook; defaults to ``os.kill`` on POSIX.
    r   NrB  r   a\  SELECT t.id, t.worker_pid,        COALESCE(r.started_at, t.started_at) AS active_started_at,        t.max_runtime_seconds, t.claim_lock FROM tasks t LEFT JOIN task_runs r ON r.id = t.current_run_id WHERE t.status = 'running' AND t.max_runtime_seconds IS NOT NULL   AND COALESCE(r.started_at, t.started_at) IS NOT NULL   AND t.worker_pid IS NOT NULLr   r'   active_started_atr   r   r   Fr]  r   r}  TzUPDATE tasks SET status = 'ready', claim_lock = NULL, claim_expires = NULL, worker_pid = NULL, last_heartbeat_at = NULL WHERE id = ? AND status = 'running')rm  elapsed_secondslimit_secondsr|  rA  zelapsed z
s > limit r!   r  r  )rm  r|  )r   r   release_claimend_runevent_payload_extra)r~  r   r   rG  r}   r  r*  rl  rf  r*   r]  r  rg  rB   r_  ru  r  r  r)  r,  r  rc  r   _record_task_failure)r  r  r~  rA  rh  r  rl  r   r  elapsedrm  tidkilledr]  ry   r2  r  r  s                     r"   enforce_max_runtimer    s   " MMMI
dikk

C ]]((a003666K<<	)	 	 hjj 	  K K< &B{++ 	 C 34555S234444#l#$$$i %1yyr6**4BGG 	 S&.))))&0    2YY    !# E
3# Dfn---!FF*G4   D t__ 	& 	&,,6  C |q  '*7||%(-B)C%D%D%	  "#'_S\\__SEZA[=\=\___$	   #{GF      %%%1	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&< <1 c[W[[SAV=W9X9X[[[##,/F$C$C    s7   D++D?>D?	FF10F1B/I>>J	J	secondsc                    t          |           5  |                     d|t          |          nd|f          }ddd           n# 1 swxY w Y   |j        dk    S )zKSet or clear the per-task max_runtime_seconds. Returns True on
    success.z5UPDATE tasks SET max_runtime_seconds = ? WHERE id = ?Nr   )r)  r  r   r,  )r  r   r  r2  s       r"   set_max_runtimer    s     
4 
 
llC$0S\\\dGD
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 

 <1s   *AA
A
c                x   g }g }t          |           5  |                     d                                          }t                                          dd          d          d}|D ]i}|d         pd}|                    |          s#t          |d                   r9t          |d                   }t          |          \  }}	|dk    rd	}
d
}d}||d         |	d}nEd}
|dk    r	d| d|	 }n|dk    r	d| d|	 }nd| d}d}||d         d}|	|dk    r
||d<   |	|d<   |                     d|d         f          }|j	        dk    rt          | |d         dd|t          |                    }t          | |d         |||           |                    |d                    |                    |d         ||d         |
|f           k	 ddd           n# 1 swxY w Y   g }|D ]=\  }}}}
}t          | ||d|
rdnddd||d          }|r|                    |           >|t          _        |S )u  Reclaim ``running`` tasks whose worker PID is no longer alive.

    Appends a ``crashed`` event and drops the task back to ``ready``.
    Different from ``release_stale_claims``: this checks liveness
    immediately rather than waiting for the claim TTL.

    Only considers tasks claimed by *this host* — PIDs from other hosts
    are meaningless here. The host-local check is enough because
    ``_default_spawn`` always runs the worker on the same host as the
    dispatcher (the whole design is single-host).

    When the reap registry shows the worker exited cleanly (rc=0) but
    the task was still ``running`` in the DB, treat it as a protocol
    violation (worker answered conversationally without calling
    ``kanban_complete`` / ``kanban_block``) and trip the circuit breaker
    on the first occurrence — retrying a worker whose CLI keeps
    returning 0 without a terminal transition just loops forever.
    z`SELECT id, worker_pid, claim_lock FROM tasks WHERE status = 'running' AND worker_pid IS NOT NULLrB  r   r   r   r'   r   rR  Tuc   worker exited cleanly (rc=0) without calling kanban_complete or kanban_block — protocol violationprotocol_violation)rm  r  	exit_codeFrS  zpid z exited with code rT  z killed by signal z
 not aliver?  )rm  r  NrA  	exit_kindr  zUPDATE tasks SET status = 'ready', claim_lock = NULL, claim_expires = NULL, worker_pid = NULL WHERE id = ? AND status = 'running'r   r  r  )r   r   failure_limitr  r  r  )r)  r  r*  rG  r}   rl  ru  r   r[  r,  r  r   rc  r   r  detect_crashed_workers_last_auto_blocked)r  r?  crash_detailsrl  r  r   r  rm  r  rZ  r  
error_text
event_kindevent_payloadr2  r  r@  r  r  trippeds                       r"   r  r    s   & G <>M	4 D D||B
 
 (** 	 %,,S!44Q7::: >	 >	C|$*D??;// #l+,, c,'((C.s33JD$|##
 &*"M  2
"<0!%! ! &+">))!E!E!Et!E!EJJZ''!E!E!Et!E!EJJ!7!7!7!7J&
(+L8I J J#	(9(915M+.15M+.,,6 T	 C |q  !#d)%i$!-00	   #d)Z!!   
 s4y)))$$YS%6'5  w>	D D D D D D D D D D D D D D D^ !L=J % %9S'-z&# 2<11(+ @ @
 
 
  	%$$$
 1=-Ns   G G!!G%(G%)r  r  r  r  r  r  r  r  c                  |t           }d}t          |           5  |                     d|f                                          }	|		 ddd           dS t	          |	d                   dz   }
|	d         }d|	                                v r|	d         nd}|t	          |          }d}nt	          |          }d	}|
|k    r|r"|                     d
|
|dd         |f           n!|                     d|
|dd         |f           d}|r"t          | |dd|dd         |
|||d          }|
|||dd         |d}|r|                    |           t          | |d||           d}n|r"|                     d|
|dd         |f           n!|                     d|
|dd         |f           |r>t          | ||||dd         d|
i          }t          | |||dd         |
d|           ddd           n# 1 swxY w Y   |S )u=  Record a non-success outcome (spawn_failed / crashed / timed_out)
    and maybe trip the circuit breaker.

    Unified replacement for the old spawn-only ``_record_spawn_failure``.
    Every path that ends a task with a non-success outcome funnels
    through here so the ``consecutive_failures`` counter and the
    auto-block threshold stay consistent.

    Returns True when the task was auto-blocked (counter reached
    ``failure_limit``), False when it was just updated in place.

    Modes:

    * ``release_claim=True, end_run=True`` — spawn-failure path.
      Caller has a running task with an open run; this transitions
      it back to ``ready`` (or ``blocked`` when the breaker trips),
      releases the claim, and closes the run with ``outcome=<outcome>``.

    * ``release_claim=False, end_run=False`` — timeout/crash path.
      Caller has ALREADY flipped the task to ``ready`` and closed the
      run with the appropriate outcome. This just increments the
      counter; if the breaker trips, the task is re-transitioned
      ``ready → blocked`` and a ``gave_up`` event is emitted.

    ``event_payload_extra`` merges into the ``gave_up`` event payload
    when the breaker trips, so callers can include outcome-specific
    context (e.g. pid on crash, elapsed on timeout).

    Resolution order for the effective threshold:
      1. per-task ``max_retries`` if set (nothing else overrides)
      2. caller-supplied ``failure_limit`` (gateway passes the config
         value from ``kanban.failure_limit``; tests pass fixed values)
      3. ``DEFAULT_FAILURE_LIMIT``
    NFzHSELECT consecutive_failures, status, max_retries FROM tasks WHERE id = ?r   r   r   r   r0  
dispatcherzUPDATE tasks SET status = 'blocked', claim_lock = NULL, claim_expires = NULL, worker_pid = NULL, consecutive_failures = ?, last_failure_error = ? WHERE id = ? AND status IN ('running', 'ready')i  zUPDATE tasks SET status = 'blocked', consecutive_failures = ?, last_failure_error = ? WHERE id = ? AND status IN ('ready', 'running')r'  )failurestrigger_outcomeeffective_limitlimit_sourcer  )r  r  r  r   r  r  TzUPDATE tasks SET status = 'ready', claim_lock = NULL, claim_expires = NULL, worker_pid = NULL, consecutive_failures = ?, last_failure_error = ? WHERE id = ? AND status = 'running'zNUPDATE tasks SET consecutive_failures = ?, last_failure_error = ? WHERE id = ?r  )r   r  )	DEFAULT_FAILURE_LIMITr)  r  r(  r   r   r  r   rc  )r  r   r   r   r  r  r  r  r   r   r  
cur_statustask_overrider  r  r  r  s                    r"   r  r    s   Z -G	4 e ell&(/z
 
 (** 	 ;e e e e e e e e s1233a7]

 #0388::"="=C4 	 $!-00O!LL!-00O'L&& F uTcT{G4    F uTcT{G4	   F !'%i+$,+2+:(4	 	
 
 
 %#2 ,tt#* G # 42333gy'&    GG  : uTcT{G4    :uTcT{G4  
  !'#G+((3	   '7#DSDkx@@!   Ce e e e e e e e e e e e e e eN Ns   -G:FG::G>G>r  c          	     .    t          | ||d|dd          S )Nspawn_failedT)r   r  r  r  )r  )r  r   r   r  s       r"   _record_spawn_failurer  7  s.      gu#   r$   c           
     T   t          |           5  |                     dt          |          |f           t          | |          }|%|                     dt          |          |f           t	          | |ddt          |          i|           ddd           dS # 1 swxY w Y   dS )zRecord the spawned child's pid + emit a ``spawned`` event.

    The event's payload carries the pid so a human reading ``hermes kanban
    tail`` can correlate log lines with OS-level traces without opening
    the drawer.
    z,UPDATE tasks SET worker_pid = ? WHERE id = ?Nz0UPDATE task_runs SET worker_pid = ? WHERE id = ?r<  rm  r  )r)  r  r   r  rc  )r  r   rm  r  s       r"   _set_worker_pidr  G  s    
4 R R:XXw	
 	
 	
 !w//LLBS6"   	dGYC0A&QQQQR R R R R R R R R R R R R R R R R Rs   B BB!$B!c                    t          |           5  |                     d|f           ddd           dS # 1 swxY w Y   dS )u  Reset the unified consecutive-failures counter.

    Called from ``complete_task`` on successful completion — a fresh
    success means the task + profile combination is working and any
    past failures are history. NOT called on spawn success anymore:
    a successful spawn proves the worker could start but says nothing
    about whether the run will succeed, so we need to let timeouts and
    crashes accumulate across spawn boundaries.
    zQUPDATE tasks SET consecutive_failures = 0, last_failure_error = NULL WHERE id = ?N)r)  r  )r  r   s     r"   r  r  \  s     
4 
 
5J	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   599c                    |                      d                                          }|sdS 	 ddlm} n# t          $ r Y dS w xY w|D ]} ||d                   r dS dS )u  Return True iff there is at least one ready+assigned+unclaimed task
    whose assignee maps to a real Hermes profile.

    Used by the gateway- and CLI-embedded dispatchers' health telemetry to
    decide whether ``0 spawned`` is a "stuck" condition (real spawnable
    work waiting) or a "correctly idle" condition (only control-plane
    lanes like ``orion-cc`` / ``orion-research`` waiting on terminals
    that pull tasks via ``claim_task`` directly).

    Falls back to "any ready+assigned" if ``profile_exists`` is not
    importable (e.g. partial install) — preserves the old behavior so
    the warning still fires in degraded environments.
    znSELECT DISTINCT assignee FROM tasks WHERE status = 'ready' AND assignee IS NOT NULL     AND claim_lock IS NULLFr   profile_existsTr   )r  r*  rK  r  r   )r  rl  r  r   s       r"   has_spawnable_readyr  r  s     <<	%  hjj	 	
  u6666666   tt   >#j/** 	44	5s   4 
AA)spawn_fnr  dry_run	max_spawnr  rY   r  r  c          	        	 	 	 t          j        dt           j                  \  }}n# t          $ r Y nw xY w|dk    rnt	          ||           Ln# t
          $ r Y nw xY wt                      }	t          |           |	_        t          |           |	_
        t          t          dg           }
|
r|	j                            |
           t          |           |	_        t!          |           |	_        |                     d                                          }d}|D ]}|	||k    r n|d         s!|	j                            |d                    7	 dd	lm} n# t
          $ r d}Y nw xY w|2 ||d                   s!|	j                            |d                    |r*|	j                            |d         |d         d
f           t5          | |d         |          }|	 t7          ||          }nT# t
          $ rG}t9          | |j        d| |          }|r|	j                            |j                   Y d}~+d}~ww xY wt=          | |j        t?          |                     ||nt@          }	 ddl!}	 |"                    |          }d|j#        v r ||t?          |          |          }n ||t?          |                    }n0# tH          tJ          f$ r  ||t?          |                    }Y nw xY w|r#tM          | |j        tO          |                     |	j                            |j        |j(        pd
t?          |          f           |dz  }M# t
          $ rQ}t9          | |j        t?          |          |          }|r|	j                            |j                   Y d}~d}~ww xY w|	S )u  Run one dispatcher tick.

    Steps:
      1. Reclaim stale running tasks (TTL expired).
      2. Reclaim crashed running tasks (host-local PID no longer alive).
      3. Promote todo -> ready where all parents are done.
      4. For each ready task with an assignee, atomically claim and call
         ``spawn_fn(task, workspace_path, board) -> Optional[int]``. The
         return value (if any) is recorded as ``worker_pid`` so subsequent
         ticks can detect crashes before the TTL expires.

    Spawn failures are counted per-task. After ``failure_limit`` consecutive
    failures the task is auto-blocked with the last error as its reason —
    prevents the dispatcher from thrashing forever on an unfixable task.

    ``spawn_fn`` defaults to ``_default_spawn``. Tests pass a stub.
    ``board`` pins workspace/log/db resolution for this tick to a specific
    board. When omitted, the current-board resolution chain is used.
    Tr   r  zsSELECT id, assignee FROM tasks WHERE status = 'ready' AND claim_lock IS NULL ORDER BY priority DESC, created_at ASCNr   r   r  r'   )r  r   zworkspace: r  rY   r   ))r*   waitpidWNOHANGChildProcessErrorrO  r   r:  r  r  r  r?  getattrr@  r  r  rA  r  r$  r  r*  r=  r   rK  r  r>  r<  r  r5  r  r   r8  r   _default_spawninspect	signature
parameters	TypeErrorr    r  r   r   )r  r  r  r  r  r  rY   rN  _statusr   _crash_auto_blocked
ready_rowsr<  r   r  r  	workspaceexcauto_spawnr  sigrm  s                          r"   dispatch_oncer    s   V
	/ "
2rz : :gg$   qyyg...	/     F+D11F+D11FN " 4b   8""#6777*400F%d++FO	1  hjj	 
 G N7 N7 W	%9%9E: 	%,,SY777	"::::::: 	" 	" 	"!NNN	"%nnS_.M.M% '..s4y999 	N!!3t9c*or"BCCCT3t9+FFF?		)'???II 	 	 	(gj"5"5"5+  D  7#**7:666HHHH	 	4S^^<<<%1~	7 NNN6''//cn,, &#i..FFFCC &#i..99Cz* 6 6 6fWc)nn556 <gj#c((;;; N!!7:w/?/E2s9~~"VWWWqLGG 	7 	7 	7(gj#c((+  D  7#**7:666	7 Ms   A "' A 
4A 4A 
AAEE)(E)(G::
I<II=M(AKM(*L M(LA#M((
O2AN>>Olog_path	max_bytesc                l   	 |                                  sdS |                                 j        |k    rdS |                     | j        dz             }	 |                                 r|                                 n# t          $ r Y nw xY w|                     |           dS # t          $ r Y dS w xY w)u   Rotate ``<log>`` to ``<log>.1`` if it exceeds ``max_bytes``.

    Single-generation rotation — one old file kept, newer one replaces it.
    Keeps disk usage bounded while still giving the user a chance to grab
    the prior run's output.
    Nz.1)r?   statst_sizewith_suffixr   rV   rB   r   )r  r  rotateds      r"   _rotate_worker_logr  /  s       	F==??"i//F&&x'=>>	~~ !    	 	 	D	        s?   B% B% B% (A> =B% >
BB% 
BB% %
B32B3r  c          	     n   ddl }| j        st          d| j         d          ddlm}  || j                  }d| j         }t          t          j                  }| j	        r
| j	        |d<   | j        |d<   ||d	<   | j
        t          | j
                  |d
<   | j        r
| j        |d<   t          t          |                    |d<   t          t          |                    |d<   t          |          pt!                      }||d<   ||d<   dd|ddg}	| j        r)| j        D ]!}
|
r|
dk    r|	                    d|
g           "|	                    dd|g           t'          |          }|                    dd           || j         dz  }t+          |t,                     t/          |d          }	  |j        |	t          j                            |          r|nd|j        ||j        |d          }n1# t:          $ r$ |                                 t?          d          w xY w|j         S )a|  Fire-and-forget ``hermes -p <profile> chat -q ...`` subprocess.

    Returns the spawned child's PID so the dispatcher can detect crashes
    before the claim TTL expires. The child's completion is still observed
    via the ``complete`` / ``block`` transitions the worker writes itself;
    the PID check is a safety net for crashes, OOM kills, and Ctrl+C.

    ``board`` pins the child's kanban context to that board: the child's
    ``HERMES_KANBAN_DB`` / ``HERMES_KANBAN_BOARD`` / workspaces_root env
    vars all resolve to the same board the dispatcher claimed the task
    from. Workers cannot accidentally see other boards.
    r   Nr2  z has no assigneerI  zwork kanban task HERMES_TENANTHERMES_KANBAN_TASKHERMES_KANBAN_WORKSPACEHERMES_KANBAN_RUN_IDHERMES_KANBAN_CLAIM_LOCKr   rc   rh   r;   HERMES_PROFILEhermesrb  z--skillszkanban-workerchatz-qTrK   .logab)r4  stdinrc  rd  rD   start_new_sessionzv`hermes` executable not found on PATH. Install Hermes Agent or activate its venv before running the kanban dispatcher.)!rm  r   r    r   rK  rJ  r   r*   r+   r   r   r   r   rf   rj   r#   rH   r   r  rm   rP   r  DEFAULT_LOG_ROTATE_BYTESrk  PopenrR   isdirro  STDOUTrW   closere  rm  )r0  r  rY   rm  rJ  profile_argpromptrD   resolved_boardcmdsklog_dirr  log_frt  s                  r"   r  r  F  s   $ = <::::;;;::::::((77K***F
rz

C{ +#{O $C%.C!"&&)$*=&>&>"# :*./&' ".u"="="=>>C+.U/K/K/K+L+LC'( +511H5F5H5HN!/C
 (C 	k 	OC( { -+ 	- 	-B -bO++

J+,,,JJf    E***GMM$M...DG))))Hx!9::: 4  E
zW]]955?		4$$"
 
 
  
 
 
^
 
 	

 8Os   <AG? ?.H-g      N@)intervalr  r  
stop_eventon_tickr  floatc                   ddl }ddl}|                                fd}|                                |                                u rGdD ]D}t          ||d          }	|	/	 |                     |	|           -# t          t          f$ r Y @w xY wE                                s	 t          j
        t                                5 }
t          |
||          }ddd           n# 1 swxY w Y   |	  ||           n# t          $ r Y nw xY wn(# t          $ r ddl}|                                 Y nw xY w                    |                                            dS dS )aO  Run the dispatcher in a loop until interrupted.

    Calls :func:`dispatch_once` every ``interval`` seconds. Exits cleanly
    on SIGINT / SIGTERM so ``hermes kanban daemon`` is systemd-friendly.
    ``stop_event`` (a :class:`threading.Event`) and ``on_tick`` (a
    callable receiving the :class:`DispatchResult`) are test hooks.
    r   Nc                0                                      d S rt   r  )_signum_framer  s     r"   _handlezrun_daemon.<locals>._handle  s    r$   )SIGINTr  )r  r  )r  )r~  	threadingr  current_threadmain_threadr  r    rB   is_setr  r  r  r  r   	traceback	print_excwait)r  r  r  r  r  r~  r  r  sig_namer  r  resr  s      `         r"   
run_daemonr    s3    MMM__&&
    
 !!Y%:%:%<%<<<- 	 	H&(D11CMM#w////"G,   D  !! *	"#GII.. $#'"/                 "GCLLLL    D 	" 	" 	"!!!!!	" 	)))# !! * * * * *sl   %A<<BB) D 	C(D (C,,D /C,0D 6D D 
DD DD "D87D8c                   t          | |          }|st          d|           t          fdCd}g }|                    d	|j         d
|j                    |                    d           |                    d|j        pd            |                    d|j                    |j        r|                    d|j                    |                    d|j	         d|j
        pd            |                    d           |j        rl|j                                        rS|                    d           |                     ||j        t                               |                    d           d t          | |          D             }t          |          t           k    r-t          |          t           z
  }|t            d         }|dz   }nd}|}d}|r|                    d           |r4|                    d| d|dk    rdnd dt          |           d           t#          |          D ]e\  }	}
||	z   }t%          j        dt%          j        |
j                            }|
j        pd}|
j        p|
j        }|                    d| d | d!| d"| d#	           |
j        r<|
j                                        r#|                     ||
j                             |
j        r?|
j                                        r&|                    d$ ||
j                              |
j        rP	 t7          j        |
j        d%d&'          }|                    d( ||           d)           n# t:          $ r Y nw xY w|                    d           g|                     d*|f                                          }d+ |D             }|rd%}|D ]}t          | |          }|r|j        d,k    r!d- t          | |          D             }|                     d. d&/           |r|d         nd}
|s|                    d0           d&}|                    d1|            g }|
D|
j        r=|
j                                        r$|                     ||
j                             n@|j!        r$|                     ||j!                             n|                    d2           |
W|
j        rP	 t7          j        |
j        d%d&'          }|                    d( ||           d)           n# t:          $ r Y nw xY w|"                    |           |                    d           |j        r|                     d3|j        |f                                          }|r|                    d4|j                    |D ]}t%          j        dt%          j        tG          |d5                                       }|d6         pd                                $                                }|r|d         dd7         nd8}|                    d9|d:          d |d;          d!| d<|            |                    d           tK          | |          }t          |          tL          k    r(t          |          tL          z
  }|tL           d         }nd}|}|r|                    d=           |r4|                    d| d>|dk    rdnd dt          |           d           |D ]}t%          j        dt%          j        |j'                            }|                    d?|j(         d@| dA           |                     ||j        tR                               |                    d           dB*                    |          +                                dBz   S )Da2  Return the full text a worker should read to understand its task.

    Order:
      1. Task title (mandatory).
      2. Task body (optional opening post, capped at 8 KB).
      3. Prior attempts on THIS task (most recent ``_CTX_MAX_PRIOR_ATTEMPTS``
         shown; older attempts collapsed into a one-line summary).
         Each attempt's ``summary`` / ``error`` / ``metadata`` capped at
         ``_CTX_MAX_FIELD_BYTES`` each.
      4. Structured handoff results of every done parent task. Prefers
         ``run.summary`` / ``run.metadata`` when the parent was executed
         via a run; falls back to ``task.result`` for older data. Same
         per-field cap.
      5. Cross-task role history for the assignee (most recent 5
         completed runs on other tasks).
      6. Comment thread (most recent ``_CTX_MAX_COMMENTS`` shown, older
         collapsed).

    All caps exist so worker prompts stay bounded even on pathological
    boards (retry-heavy tasks, comment storms). The per-field char cap
    prevents a single 1 MB summary from dominating context.
    r  r!   r   r{  r   r   r   c                    | sdS |                                  } t          |           |k    r| S | d|         dt          |           |z
   dz   S )z;Truncate a string to `limit` chars with a visible ellipsis.r'   Nu   … [truncated, z chars omitted])r   ra  )r!   r{  s     r"   _capz"build_worker_context.<locals>._cap  sY     	2GGIIq66U??H%yMc!ffunMMMMMr$   z# Kanban task z: r'   z
Assignee: z(unassigned)z
Status:   z
Tenant:   zWorkspace: z @ z(unresolved)z## Bodyc                     g | ]}|j         	|S rt   )r   rX  s     r"   r   z(build_worker_context.<locals>.<listcomp>3  s    OOOq
8N8N8N8Nr$   Nr   r   z## Prior attempts on this taskz_(z earlier attemptz omitted; showing most recent z)_z%Y-%m-%d %H:%Mz	(unknown)z### Attempt u    — z (rS  rU  z	_error_: FT)r   	sort_keysz_metadata_: ``r  c                    g | ]
}|d          S r  r5   rX  s     r"   r   z(build_worker_context.<locals>.<listcomp>]  s    666Q!K.666r$   r
   c                (    g | ]}|j         d k    |S )r  )r   rX  s     r"   r   z(build_worker_context.<locals>.<listcomp>e  s$    PPP!qyK7O7OA7O7O7Or$   c                    | j         S rt   )r   )rY  s    r"   r   z&build_worker_context.<locals>.<lambda>f  s    AL r$   )r   reversez## Parent task resultsz### z(no result recorded)zSELECT t.id, t.title, r.summary, r.ended_at FROM task_runs r JOIN tasks t ON r.task_id = t.id WHERE r.profile = ? AND r.task_id != ?   AND r.outcome = 'completed' ORDER BY r.ended_at DESC LIMIT 5z## Recent work by @r   r   r	  z(no summary)z- r   r   z): z## Comment threadz earlier commentz**z** (z):rN   )r!   r   r{  r   r   r   ),rz  r    _CTX_MAX_FIELD_BYTESr   r   r   r   r   r   r   r   r   r   _CTX_MAX_BODY_BYTES	list_runsra  _CTX_MAX_PRIOR_ATTEMPTS	enumerater   strftime	localtimer   r   r   r   r   r   r   r   r   r  r*  sortr   r  r   r  r  _CTX_MAX_COMMENTSr   r  _CTX_MAX_COMMENT_BYTESr{   rstrip)r  r   r0  r  lines	all_prioromittedshownfirst_shown_idxoffsetr!  idxr   r   r   meta_strparent_rowsr  wrote_headerrm  ptruns
body_lines	role_rowsr   r!   firstall_comments	omitted_cshown_ccs                                  r"   build_worker_contextr+    s	   . D'""D 4222333,@ N N N N N E	LL9$'99TZ99:::	LL	LL?dm=~??@@@	LL+dk++,,,{ 1/$+//000	LL^t2^^t7J7\n^^___	LLy TY__&& YTT$)%899:::R POIdG44OOOI
9~~///i..#::22334!A+ 5666 	LL?W ? ?W\\ccr ? ?03E

? ? ?   %U++ 	 	KFC!F*C/1O1OPPBk0[Gk/SZGLLMMM'MMWMMMMMNNN{ 0s{0022 0TT#+..///y <SY__.. <:ci::;;;| #z#,UVZ[[[HLL!Bh!B!B!BCCCC    DLL
 ,,P	
  hjj  76+666J  	 	C$$$B f,,PPys33PPPDII00$I???!+$q''tC $5666#LL&&&$&J3;3;3D3D3F3F!!$$s{"3"34444 :!!$$ry//2222!!"89993<#z#,UVZ[[[H%%&Gdd8nn&G&G&GHHHH    DLL$$$LL } LL/
 ]G$
 
 (** 	  		LL>t}>>???  R R]$dnSZ5I5I&J&J  ^)r0022==??&';!TcT

^P#d)PP#g,PP"PPPPQQQQLL
 !w//L
<,,,%%(99	 11223	 ())) 	LLAY A AyA~~2 A A03GA A A    	 	A/1M1MNNBLL2ah22B222333LLaf&<==>>>LL99U""$$t++s$   >>M==
N
	N
>U
UUc                   i }|                      d          D ] }t          |d                   ||d         <   !i }|                      d          D ]:}t          |d                   |                    |d         i           |d         <   ;|                      d                                          }t          t	          j                              }|r |d         |t          |d                   z
  nd}||||d	S )
zPer-status + per-assignee counts, plus the oldest ``ready`` age in
    seconds (the clearest staleness signal for a router or HUD).
    zRSELECT status, COUNT(*) AS n FROM tasks WHERE status != 'archived' GROUP BY statusnr   SELECT assignee, status, COUNT(*) AS n FROM tasks WHERE status != 'archived' AND assignee IS NOT NULL GROUP BY assignee, statusr   z>SELECT MIN(created_at) AS ts FROM tasks WHERE status = 'ready'r   N)	by_statusby_assigneeoldest_ready_age_secondsrh  )r  r   
setdefaultr(  r   )r  r/  r   r0  
oldest_rowrh  oldest_ready_ages          r"   board_statsr5    s0    !#I||	5  1 1 $'s3x==	#h-  -/K||	$  S S
 FIS]]s:33CMBBH hjj  dikk

C 	A$T*6 
s:d#$$	$	$<@  "$4	  r$   c                @   t          t          j                              }| j        r|t          | j                  z
  nd}| j        r|t          | j                  z
  nd}| j        r0t          | j                  t          | j        p| j                  z
  nd}|||dS )zEReturn age metrics for a single task. All values are seconds or None.N)created_age_secondsstarted_age_secondstime_to_complete_seconds)r   r   r   r   r   )r0  rh  age_since_createdage_since_startedtime_to_completes        r"   task_ager=    s    
dikk

C6:oOc$/22224&*o?c$/""""4 
 	'DT_%G!H!HHH"& 
  10$4  r$   )	thread_iduser_idrj  chat_idr>  r?  c          
         t          t          j                              }t          |           5  |                     d||||pd||f           ddd           dS # 1 swxY w Y   dS )zRegister a gateway source that wants terminal-state notifications
    for ``task_id``. Idempotent on (task, platform, chat, thread).z
            INSERT OR IGNORE INTO kanban_notify_subs
                (task_id, platform, chat_id, thread_id, user_id, created_at)
            VALUES (?, ?, ?, ?, ?, ?)
            r'   N)r   r   r)  r  )r  r   rj  r@  r>  r?  rh  s          r"   add_notify_subrB    s     dikk

C	4 
 

 hb'3G	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   AA #A c                    |*|                      d|f                                          }n'|                      d                                          }d |D             S )Nz2SELECT * FROM kanban_notify_subs WHERE task_id = ?z SELECT * FROM kanban_notify_subsc                ,    g | ]}t          |          S r5   )r   rX  s     r"   r   z$list_notify_subs.<locals>.<listcomp>  s    """DGG"""r$   r  r  s      r"   list_notify_subsrE    sf     ||@7*
 

(** 	 ||>??HHJJ""T""""r$   )r>  c                   t          |           5  |                     d||||pdf          }d d d            n# 1 swxY w Y   |j        dk    S )NzcDELETE FROM kanban_notify_subs WHERE task_id = ? AND platform = ? AND chat_id = ? AND thread_id = ?r'   r   )r)  r  r,  )r  r   rj  r@  r>  r2  s         r"   remove_notify_subrG    s     
4 
 
llAhb9
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 <!s   9= =)r>  kindsrH  tuple[int, list[Event]]c               `   |                      d||||pdf                                          }|dg fS t          |d                   }|rt          |          nd}d|r+dd                    d	t          |          z            z   d
z   ndz   dz   }	||g}
|r|
                    |           |                      |	|
                                          }g }|}|D ]}	 |d         rt          j	        |d                   nd}n# t          $ r d}Y nw xY w|                    t          |d         |d         |d         ||d         d|                                v r|d         t          |d                   nd                     t          |t          |d                             }||fS )a  Return ``(new_cursor, events)`` for a given subscription.

    Only events with ``id > last_event_id`` are returned. The subscription's
    cursor is NOT advanced here; call :func:`advance_notify_cursor` after
    the gateway has successfully delivered the notifications.
    zqSELECT last_event_id FROM kanban_notify_subs WHERE task_id = ? AND platform = ? AND chat_id = ? AND thread_id = ?r'   Nr   last_event_idz7SELECT * FROM task_events WHERE task_id = ? AND id > ? zAND kind IN (rR  rT  z) zORDER BY id ASCr  r   r   r  r   r  r  )r  r(  r   r   r{   ra  r  r*  r   r   r   r   r  r   max)r  r   rj  r@  r>  rH  r   cursor	kind_listqr  rl  r  max_idrY  r  s                   r"   unseen_events_for_subrQ  %  s    ,,	O	(GY_"5  hjj	 
 {"u_%&&F$.U$IAFOW?SXXcC	NN&:;;;dBBUW	Y
	 
 !&)F !i   <<6""++--DCF 
+ 
+	23I,Hdj9...DGG 	 	 	GGG	

5w)1V9,(0AFFHH(<(<8AXC($$$^b
 
 
 	 	 	
 VS4\\**3;s   $DDD
new_cursorc          	         t          |           5  |                     dt          |          ||||pdf           d d d            d S # 1 swxY w Y   d S )NztUPDATE kanban_notify_subs SET last_event_id = ? WHERE task_id = ? AND platform = ? AND chat_id = ? AND thread_id = ?r'   )r)  r  r   )r  r   rj  r@  r>  rR  s         r"   advance_notify_cursorrT  V  s     
4 
 
S__gx)/rJ	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   +AAAi ' )older_than_secondsrU  c               
   t          t          j                              t          |          z
  }t          |           5  |                     d|f          }ddd           n# 1 swxY w Y   t          |j        pd          S )zDelete task_events rows older than ``older_than_seconds`` for tasks
    in a terminal state (``done`` or ``archived``). Returns the number of
    rows deleted. Running / ready / blocked tasks keep their full event
    history.zwDELETE FROM task_events WHERE created_at < ? AND task_id IN (SELECT id FROM tasks WHERE status IN ('done', 'archived'))Nr   )r   r   r)  r  r,  )r  rU  rH  r2  s       r"   	gc_eventsrW  k  s     $6 7 77F	4 
 
llJI
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 s| q!!!s    A$$A(+A()rU  rY   c                f   t          |          }|                                sdS t          j                    | z
  }d}|                                D ]]}	 |                                r6|                                j        |k     r|                                 |dz  }N# t          $ r Y Zw xY w|S )uH  Delete worker log files older than ``older_than_seconds``. Returns
    the number of files removed. Kept separate from ``gc_events`` because
    log files live on disk, not in SQLite. Scoped to ``board`` (defaults
    to the active board) — per-board isolation means deleting logs from
    board A cannot touch board B's logs.r   r   r   )	rm   r?   r   r   is_filer  st_mtimerV   rB   )rU  rY   r  rH  removedr   s         r"   gc_worker_logsr\  |  s     E***G>> qY[[--FG__  	yy{{ qvvxx0699


1 	 	 	H	Ns   A
B!!
B.-B.c               .    t          |          |  dz  S )uR  Return the path to a worker's log file. The file may not exist
    (task never spawned, or log already GC'd).

    When ``board`` is None, resolves via the active board (env var →
    current-board file → default). The dispatcher always passes the
    board explicitly to avoid any resolution ambiguity when multiple
    boards exist.r   r  )rm   )r   rY   s     r"   worker_log_pathr^    s#     '''W*:*:*:::r$   )
tail_bytesrY   r_  c                  t          | |          }|                                sdS 	 ||                    dd          S |                                j        }t          |d          5 }||k    r|                    ||z
             |                                }|                                }|	                    d          s-|                                |k    r|                    |           |
                                }ddd           n# 1 swxY w Y   |                    dd          S # t          $ r Y dS w xY w)	zRead the worker log for ``task_id``. Returns None if the file
    doesn't exist. If ``tail_bytes`` is set, only the last N bytes are
    returned (useful for the dashboard drawer which shouldn't page megabytes).r   Nr<   r|   )r>   errorsrb   
)ra  )r^  r?   r@   r  r  rk  seektellreadlineendswithreaddecoderB   )	r   r_  rY   rR   sizerF   probepartialdatas	            r"   read_worker_logrn    sz    7%000D;;== t>>79>EEEyy{{"$ 	j  tj()))
 **,,''.. "16688t3C3CFF5MMM6688D	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 {{79{555   tts<   D6 )D6 +BDD6 DD6 DD6 6
EEc                     	 ddl m}   |             }|dz  }n# t          $ r g cY S w xY wt                      }|                                r|                    d           |                                r|	 t          |                                          D ]H}|                                s|dz  	                                r|                    |j
                   In# t          $ r Y nw xY wt          |          S )a  Return the set of assignee/profile names discovered on disk.

    Includes:
    - named profiles under ``<default-root>/profiles/<name>/config.yaml``
    - the implicit ``default`` profile when the default Hermes root exists

    Reads profile paths directly so this module has no import dependency on
    ``hermes_cli.profiles`` (which pulls in a large chunk of the CLI startup
    path).
    r   r(   profilesr   zconfig.yaml)r.   r)   r   r   r?   r   r`   r   r   rY  r   rB   )r)   default_rootprofiles_dirnamesrY  s        r"   list_profiles_on_diskrt    s:   <<<<<<..00#j0   			 eeE 		) 	 4 4 6 677 * *||~~ M)2244 *IIej)))	*
  	 	 	D	 %==s    ''6A*C! !
C.-C.c                `   t          t                                i |                     d          D ]:}t          |d                                       |d         i           |d         <   ;t          t                                                    z            }fd|D             S )a  Return every assignee name known to the board or on disk.

    Each entry is ``{"name": str, "on_disk": bool, "counts": {status: n}}``.
    A name is included when it's a configured profile on disk OR when
    any non-archived task has it as the assignee. Used by:

    - ``hermes kanban assignees`` for the terminal.
    - The dashboard assignee dropdown (so a fresh profile appears in
      the picker even before it's been given any task).
    - Router-profile heuristics ("who's overloaded?") without scanning
      the whole board.
    r.  r-  r   r   c                H    g | ]}||v                      |i           d S ))r   on_diskcounts)r,   )rv   r   rx  rw  s     r"   r   z#known_assignees.<locals>.<listcomp>  sM        	 wjjr**	
 	
  r$   )r   rt  r  r   r2  r   r   )r  r   rs  rx  rw  s      @@r"   known_assigneesry    s     '))**G )+F||	$  N N
 ADCH#j/2..s8}==7S///00E        r$   )include_activerz  	list[Run]c                   d}|g}|s|dz  }|dz  }|                      ||                                          }d |D             S )zReturn all runs for ``task_id`` in start order.

    ``include_active=True`` (default) includes the currently-running
    attempt if any. Set False to return only closed runs (useful for
    "how many prior attempts have there been?" checks).
    z)SELECT * FROM task_runs WHERE task_id = ?z AND ended_at IS NOT NULLz  ORDER BY started_at ASC, id ASCc                B    g | ]}t                               |          S r5   )r   r   rX  s     r"   r   zlist_runs.<locals>.<listcomp>"  s"    ***CLLOO***r$   r  )r  r   rz  rO  r  rl  s         r"   r  r    sa     	4A 	F )	((	++A<<6""++--D**T****r$   Optional[Run]c                    |                      dt          |          f                                          }|rt                              |          nd S )Nz$SELECT * FROM task_runs WHERE id = ?)r  r   r(  r   r   )r  r  r   s      r"   get_runr  %  sL    
,,.V hjj  !$-3<<-r$   c                    |                      d|f                                          }|rt                              |          ndS )zEReturn the currently-open run for ``task_id`` (``ended_at IS NULL``).z_SELECT * FROM task_runs WHERE task_id = ? AND ended_at IS NULL ORDER BY started_at DESC LIMIT 1Nr  r(  r   r   ry  s      r"   
active_runr  ,  sK    
,,	+	
  hjj	 
 !$-3<<-r$   c                    |                      d|f                                          }|rt                              |          ndS )zDReturn the most recent run regardless of outcome (active or closed).zSSELECT * FROM task_runs WHERE task_id = ? ORDER BY started_at DESC, id DESC LIMIT 1Nr  ry  s      r"   
latest_runr  6  sK    
,,	4	
  hjj	 
 !$-3<<-r$   c                l    |                      d|f                                          }|r|d         ndS )ub  Return the latest non-null ``task_runs.summary`` for ``task_id``.

    The kanban-worker skill writes its handoff to ``task_runs.summary``
    via ``complete_task(summary=...)``; ``tasks.result`` is left empty
    unless the caller passes ``result=`` explicitly. Dashboards and CLI
    "show" views need this value to surface what a worker actually did
    — without it, ``tasks.result`` is NULL and the task looks like a
    no-op even when the run completed.

    Picks the most recent run by ``ended_at`` (falling back to ``id``
    for ties or unfinished rows). Returns None if no run has a summary.
    zSELECT summary FROM task_runs WHERE task_id = ? AND summary IS NOT NULL AND summary != '' ORDER BY COALESCE(ended_at, started_at) DESC, id DESC LIMIT 1r   N)r  r(  ry  s      r"   latest_summaryr  @  sG     ,,	H 

	 
 hjj  !*3y>>d*r$   task_idsdict[str, str]c                    t          |          }|si S d                    d |D                       }|                     d| d|                                          }d |D             S )u  Batch-fetch latest non-null summaries for a list of task ids.

    Used by the dashboard board endpoint to attach ``latest_summary`` to
    every card in a single SQL query, avoiding the N+1 pattern of
    calling :func:`latest_summary` per task. Returns a dict mapping
    ``task_id`` → summary string, omitting tasks with no summary.

    Approach: a window function picks the newest non-null-summary row
    per ``task_id``; works against SQLite ≥ 3.25 (default on every
    supported platform).
    rR  c              3     K   | ]}d V  dS )rT  Nr5   )rv   ry   s     r"   rx   z#latest_summaries.<locals>.<genexpr>g  s"      --AC------r$   aD  
        SELECT task_id, summary FROM (
            SELECT task_id, summary,
                   ROW_NUMBER() OVER (
                       PARTITION BY task_id
                       ORDER BY COALESCE(ended_at, started_at) DESC, id DESC
                   ) AS rn
              FROM task_runs
             WHERE task_id IN (zZ)
               AND summary IS NOT NULL AND summary != ''
        ) WHERE rn = 1
        c                ,    i | ]}|d          |d         S )r   r   r5   rX  s     r"   r  z$latest_summaries.<locals>.<dictcomp>w  s"    5551AiL!I,555r$   )r   r{   r  r*  )r  r  idsrv  rl  s        r"   latest_summariesr  V  s     x..C 	88-------L<<	 !-	 	 	 	  hjj 	 655555r$   )r   r   r   r   )r   r   )r   r   )r   r   r   r   )r   rT   rt   )rY   r   r   r   )rY   r   r   r]   )r   r   r   r   )rY   r   r   r   )rY   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	  rY   r   r   r
  )r   r	  rY   r   r   r   )r  r
  r   rT   )r  r
  )r   r   r   r   ) r  r
  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rL   rM  r   r]   r   r   r   r   r   rN  r   r   r   r   )r  r
  rL   rM  r   ro  )r  r
  r   r   r   rw  )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   rT   )r  r
  r  r   r  r   r   r]   )r  r
  r   r   r   ro  )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   r   rT   )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
  r   r   )
r  r
  r   r   r  r   r  r   r   rw  )
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  rM  r   r  )r  r
  r  r   r   ro  )r  r
  r   r   r   r   r   r   r   r   r  rN  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   r   r   r  r   r   r]   )r0  r   rY   r   r   r   )r  r
  r   r   rR   r6  r   rT   )rm  r   rC  r   r   rT   )rm  r   r   rP  )rm  r   r   r]   )rm  r   r   r   r   rv  )
r  r
  r   r   r  r   r  r   r   r]   )r  r
  r   ro  )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
  r   r   rm  r   r   rT   )r  r
  r   r   r   rT   )r  r
  r   r]   )r  r
  r  r   r  r]   r  r   r  r   rY   r   r   r:  )r  r   r  r   r   rT   )r0  r   r  r   rY   r   r   r   )r  r  r  r   r  r   r   rT   )r  r
  r   r   r   r   )r  r
  r   r   )r0  r   r   r   )r  r
  r   r   rj  r   r@  r   r>  r   r?  r   r   rT   )r  r
  r   r   r   r   )r  r
  r   r   rj  r   r@  r   r>  r   r   r]   )r  r
  r   r   rj  r   r@  r   r>  r   rH  rN  r   rI  )r  r
  r   r   rj  r   r@  r   r>  r   rR  r   r   rT   )r  r
  rU  r   r   r   )rU  r   rY   r   r   r   )r   r   rY   r   r   r   )r   r   r_  r   rY   r   r   r   )r   ro  )r  r
  r   r   )r  r
  r   r   rz  r]   r   r{  )r  r
  r  r   r   r~  )r  r
  r   r   r   r~  )r  r
  r   r   r   r   )r  r
  r  rM  r   r  )r   
__future__r   r  r   r*   rer=  r  rm  ri  r   dataclassesr   r   pathlibr   typingr   r   r	   r  r]  DEFAULT_CLAIM_TTL_SECONDSr  r  r  r  r  rC   compiler   r#   r0   r6   r9   rH   rS   rX   r\   rA   rf   rj   rm   rp   r~   r   r   r   r   r   r   r   r  r  r  r   r  r   r  r   r  contextmanagerr)  r?  rG  rL  rn  r`  rz  r  r  r  r  r  r  r  r  r  r  r  rc  r  r  r  r  r  r  r  r  r  r  r  r  r    r  r  r"  r%  r(  r-  r/  r5  r8  r  DEFAULT_SPAWN_FAILURE_LIMITr  r:  rL  rK  rB  rO  r[  ru  r  r  r  r  r  r  r  r  r  _clear_spawn_failuresr  r  r  r  r  r+  r5  r=  rB  rE  rG  rQ  rT  rW  r\  r^  rn  rt  ry  r  r  r  r  r  r  r5   r$   r"   <module>r     sG  D D DL # " " " " "      				 				       



  ( ( ( ( ( ( ( (       * * * * * * * * * * WVV666  $    " " "   :;;   % % % %./ / / /0 0 0 0$ $ $ $N   "            4 4 4 4 4) ) ) ) )2* * * * *,$ $ $ $ $ * * * * *e e e e    H !%#& & & & & &X !%     : -1 ) ) ) ) ) )X 04 'E 'E 'E 'E 'E 'E\ r
 r
 r
 r
 r
 r
 r
 r
j 2
 2
 2
 2
 2
 2
 2
 2
j         ! ! ! ! ! ! ! !@
N  #suu  $ $ $ $ #*  * * * * * *\ #       :X
 X
 X
 X
v    *
' 
' 
' 
'# # # #, , , , " $#$( %))-&*!%!o& o& o& o& o& o&d
4 
4 
4 
4/ / / / #  ", , , , , ,>   J
 
 
 
<   .       * * * *) ) ) )2 2 2 2&' ' ' ',   "   8 #	 !     : "# 6 6 6 6 6 6rQ Q Q Q "#0# 0# 0# 0# 0# 0#n   J 1!V' V' V' V' V' V'z 1!     D , , , , , ,f !B B B B B BT        >H H H H\ BJ455 4 4 4 4B
 
 
 
 
Z 
 
 
, !!#-1%)c c c c c cV "#@ @ @ @ @ @N !%)4 4 4 4 4 4n$ $ $ $V   R R R R R Rj   4 =A =8 =8 =8 =8 =8 =8@
 
 
 
$  3  +  C C C C C C C CB #&  79  9 9 9 91 1 1 1.       F> > > >J 	2 2 2 2 2 2r %)0 0 0 0 0 0l l l l l l l^   | | | |J *.W W W W W WB       R R R R*
 
 
 
& /    F 0#4Z Z Z Z Z Zz   6  	s s s s s sx #44* 4* 4* 4* 4* 4*vu, u, u, u,x! ! ! !H   8  $!
 
 
 
 
 
0 8<	# 	# 	# 	# 	#$  $     .  $%). . . . . .n  $
 
 
 
 
 
, <J" " " " " "$ "0     8 =A ; ; ; ; ; ; 26     F       F       V  	+ + + + + +*. . . .. . . .. . . .+ + + +,!6 !6 !6 !6 !6 !6r$   