Ë
    MÞëj �  ã                  ó   — U d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
mZ ddlZddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZmZmZ ddlmZ ddlmZ ddlmZm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z*m+Z+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2  ejf                  dd¬«      Z4e4jk                  e&d¬«       e4jk                  e(d¬«       dZ6dZ7dZ8dZ9dZ:d Z;d!e<d"<    e=d#„ e0D «       «      Z>d$e<d%<   ddd&„Z?ded'„Z@dfd(„ZAdgd)„ZBd*d+œ	 	 	 	 	 	 	 	 	 dhd,„ZCdid-„ZDdjd.„ZEdkd/„ZFdld0„ZGdmd1„ZHdnd2„ZIe4j•                  «       dod3„«       ZKe4j•                  «        ej˜                  d4d5d6d7¬8«       ej˜                  d9d:d;¬8«       ej˜                  d<d=d>d?¬8«      ddf	 	 	 	 	 	 	 	 	 	 	 dpd@„«       ZMdqdA„ZNe4j•                  «        ej˜                  d4d:dBdC¬8«      dde;dDf	 	 	 	 	 	 	 	 	 drdE„«       ZOdsdF„ZPe4j•                  «        ej˜                  d4dGdH¬8«       ej˜                  ddIdJ¬8«       ej˜                  ddKdL¬8«       ej˜                  dMdNdO¬8«       ej˜                  ddPdQ¬8«       ej˜                  ddRdS¬8«       ej˜                  dTdUdV¬8«       ej˜                  dTdWdX¬8«       ej˜                  dYdZd[¬8«       ej˜                  dd\d]¬8«       ej˜                  d^d_d`¬8«      f	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dtda„«       ZQdodb„ZReSdck(  r eR«        yy)uuÕ  CyberBrain26 command line interface.

Every command in this module resolves its filesystem locations through
:mod:`cyberbrain26.product.local_store` â€” the single existing resolution path
that honours ``CYBERBRAIN_DATA_DIR`` / ``CYBERBRAIN_DB_PATH`` and creates the
data directory with mode ``0o700``. No command here computes a data directory
of its own, so ``roadmaps.json`` and the SQLite store can never diverge.

Execution model (decided by the product owner, 2 Oct 2026)
---------------------------------------------------------
``cyberbrain run`` routes every roadmap step through
:meth:`PtahBridge.execute_step`, which is the only entry that always applies
``InMemoryTaskDispatcher._authorize`` (identity -> scope -> policy -> approval)
and writes an audit record. There is **no model call anywhere in this path**:
a model turn has no action and no scope in the policy system, so it cannot be
authorized through ``AuthorizedExecutionService``, and executing model-generated
text would run untrusted input in the sandbox.

The direct ``ModelGateway.chat`` call that previously lived in ``run`` is gone,
and with it the ``--model`` and ``--lang`` options. As a direct consequence,
roadmaps produced by ``cyberbrain plan`` carry planner steps whose payload
declares no action; running such a roadmap executes nothing and reports every
step as refused. That is the accepted, visible consequence of the decision and
is stated by the command instead of being papered over.

Operator-facing output
---------------------
Diagnostics this module controls go to stderr via ``typer.echo(..., err=True)``
and every failure path exits non-zero. Progress lines go to stdout via
``rich.print``. This module never re-raises a handled exception, so a command
failure never prints a Python traceback of its own. The libraries below it do
log their own diagnostics (including stack traces) at ERROR level on stderr when
a step is denied; suppressing that would hide real operational detail, so it is
left in place.
é    )ÚannotationsN)ÚSequence)ÚPath)Ú	AnnotatedÚAny)Úprint)Ú__version__)ÚSQLiteTaskQueue)ÚSQLiteDatabase)ÚDocumentTrustÚKG1NodeÚKnowledgeSourceÚSensitivity)ÚEmbeddingConfig)ÚMemoryService)Ú
PtahBridgeÚRoadmapÚRoadmapStatusÚRoadmapStepÚ
StepResultÚ
StepStatus)Úapprovals_app)Ú
doctor_app)Úensure_data_dirÚopen_databaseÚresolve_db_path)ÚAuditLog)ÚPermissionScope)ÚSecurityErrorÚ
cyberbrainzCyberBrain26 CLI Interface)ÚnameÚhelpÚ	approvals)r!   Údoctorz7Explicit path to the CyberBrain26 SQLite database file.z2Explicit data directory holding the database file.zçSeconds to wait for a human approval decision per step. Default 300 because the library default (30 s) is unusable for a two-terminal flow: on timeout the request is persisted as 'cancelled' and disappears from 'approvals pending'.a*  Comma-separated permission scopes granted to the coder identity, e.g. 'python,shell'. Default is empty, and an empty value grants nothing: a step carrying code then ends closed at the scope gate. LIMITATION: --scopes currently applies only to the 'coder' role; it is NOT applied to security, evaluator, research, browser or planner steps. A code-carrying step with a non-coder role is not granted these scope(s) and is denied at the scope gate. This is a known limitation of the current identity_scopes schema and may be widened in a later design review.zroadmaps.jsong     Àr@ÚfloatÚ!_DEFAULT_APPROVAL_TIMEOUT_SECONDSc              #  ó4   K  — | ]  }|j                   –— Œ y ­w©N©Úvalue)Ú.0Úscopes     ú6/home/oem/CyberBrain26-unified/src/cyberbrain26/cli.pyú	<genexpr>r.   h   s   è ø€ Ò)S¸%¨%¯+­+Ñ)Sùs   ‚zfrozenset[str]Ú_VALID_SCOPESc                ó   — | t         z  S )zCReturn the roadmap index inside an already-resolved data directory.)Ú_ROADMAPS_FILENAME©Údata_dirs    r-   Ú_roadmaps_filer4   n   s   € àÔ(Ñ(Ð(ó    c                óž  — t        | «      }|j                  «       si S 	 t        j                  |j	                  «       «      }t        |t        «      s0t        j                  d|› d�d¬«       t        j                  d«      ‚|S # t
        t        f$ r9}t        j                  d|› d|› d�d¬«       t        j                  d«      d‚d}~ww xY w)	aG  Read the roadmap index, or an empty mapping when nothing is stored yet.

    Raises:
        typer.Exit: With code 1 when the index exists but cannot be decoded.
            A corrupt index is refused rather than silently treated as empty,
            because "no roadmaps" and "unreadable roadmaps" must never look alike.
    zRoadmap index z could not be read: z5. Refusing to run; repair or move the file and retry.T©Úerré   Nz3 is not a JSON object of roadmaps. Refusing to run.)r4   ÚexistsÚjsonÚloadsÚ	read_textÚOSErrorÚ
ValueErrorÚtyperÚechoÚExitÚ
isinstanceÚdict)r3   ÚpathÚdataÚexcs       r-   Ú_load_roadmapsrH   s   sÃ   € ô ˜(Ó#€DØ�;‰;Œ=Øˆ	ð&Ü�z‰z˜$Ÿ.™.Ó*Ó+ˆô �dœDÔ!Ü�
‰
Ø˜T˜FÐ"UÐVØõ	
ô �j‰j˜‹mÐØ€Køô ”ZÐ ò &Ü�
‰
Ø˜T˜FÐ"6°s°eð <1ð 1àõ	
ô
 �j‰j˜‹m Ð%ûð&ús   Ÿ#B ÂCÂ4CÃCc                ób   — t        | «      j                  t        j                  |d¬«      «       y)zÉWrite the roadmap index into an already-created data directory.

    The directory is created by :func:`ensure_data_dir` with mode ``0o700``
    before this is reached; it is never recreated here.
    é   )ÚindentN)r4   Ú
write_textr;   Údumps)r3   rF   s     r-   Ú_save_roadmapsrN   �   s"   € ô �8Ó×'Ñ'¬¯
©
°4ÀÔ(BÕCr5   c                óP   — t        | |d¬«      }|€t        j                  d«      ‚|S )aŒ  Open the persistent SQLite store shared by every execution path.

    Persistence is mandatory: approval requests and audit records written by
    ``run`` must be readable by a second terminal running ``approvals``, so a
    store-less run would be a silent pass. There is deliberately no
    ``--no-persist`` flag on ``plan`` or ``run``.

    Args:
        db: Optional explicit database file path.
        data_dir: Resolved data directory used to derive the default path.

    Returns:
        The opened database handle.

    Raises:
        typer.BadParameter: If no handle could be opened. Never falls back to
            an in-memory store.
    T)Údb_pathr3   ÚpersistzÏPersistent storage is mandatory for this command, but no SQLite database could be opened. Fix --db/--data-dir and retry; refusing to run without persistence because approvals and audit records would be lost.)r   r@   ÚBadParameter)Údbr3   Údatabases      r-   Ú_open_database_or_failrU   ™   s8   € ô&  R°(ÀDÔI€HØÐÜ× Ñ ðMó
ð 	
ð
 €Or5   © )Úcoder_scopesc                óX   — t        || «      }|rdt        |«      ind}t        |||¬«      S )a›  Build a SQLite-backed :class:`PtahBridge` on the single shared store.

    Args:
        data_dir: Resolved data directory holding ``roadmaps.json``.
        db: Optional explicit database file path.
        approval_timeout_seconds: How long the bridge waits for each approval.
        coder_scopes: Scopes merged into the coder identity. Empty means the
            identity keeps no scopes, so a code step is denied at the scope
            gate before policy and approval are consulted.

    Returns:
        A bridge whose task queue, audit log and approval manager all share the
        same database file, so a decision made in another process is visible.
    ÚcoderN)rT   Úapproval_timeout_secondsÚidentity_scopes)rU   Úlistr   )r3   rS   rZ   rW   rT   r[   s         r-   Ú_build_bridger]   ¶   s9   € ô* & b¨(Ó3€HÙ7C�w¤ \Ó 2Ñ3È€OÜØØ!9Ø'ôð r5   c           
     ó\  — | sg S g }| j                  d«      D ]+  }|j                  «       }|sŒ||vsŒ|j                  |«       Œ- |D �cg c]  }|t        vsŒ|‘Œ }}|rGt	        j
                  ddj                  |«      › ddj                  t        t        «      «      › d�«      ‚|S c c}w )a   Parse and validate a ``--scopes`` CSV against the security plane's set.

    Args:
        raw: Raw option value; empty or ``None`` means "grant nothing".

    Returns:
        The validated, de-duplicated scope list, preserving the given order.

    Raises:
        typer.BadParameter: If any entry is not a scope the identity layer
            defines. Guessing is refused instead of silently dropping values.
    ú,zunknown scope(s) in --scopes: ú, z. Valid scopes are: ú.)ÚsplitÚstripÚappendr/   r@   rR   ÚjoinÚsorted)ÚrawÚscopesÚitemr,   Úunknowns        r-   Ú_parse_scopesrk   ×   s»   € ñ Øˆ	Ø€FØ—	‘	˜#“ò !ˆØ—
‘
“ˆÚ�U &Ò(Ø�M‰M˜%Õ ð!ð #)ÖG˜¨E¼Ò,FŠuÐG€GÐGÙÜ× Ñ Ø,¨T¯Y©Y°wÓ-?Ð,@ð A!Ø!%§¡¬6´-Ó+@Ó!AÐ BÀ!ðEó
ð 	
ð €Mùò Hs   Á
B)ÁB)c                óB   — | dk  rt        j                  d| › d�«      ‚| S )z›Reject a non-positive approval timeout before it reaches the bridge.

    Raises:
        typer.BadParameter: If the timeout is not a positive number.
    r   z=--approval-timeout must be a positive number of seconds, got ra   )r@   rR   r)   s    r-   Ú_validate_approval_timeoutrm   ô   s2   € ð �‚zÜ× Ñ ØKÈEÈ7ÐRSÐTó
ð 	
ð €Lr5   c                óš   — | j                   j                  d«      }t        |t        «      r |j	                  «       r|j	                  «       S y)a  Return the step's explicitly declared action, or ``None`` when absent.

    No default is inferred. The dispatcher would silently fall back to
    ``"execute"`` for an absent action; the CLI refuses instead, so the operator
    sees which step forgot to declare one.
    ÚactionN)ÚpayloadÚgetrC   Ústrrc   )Ústepro   s     r-   Ú_declared_actionrt     s9   € ð �\‰\×Ñ˜hÓ'€FÜ�&œ#Ô 6§<¡<¤>Ø�|‰|‹~ÐØr5   c                óX  — | j                   | j                  | j                  D �cg c]Z  }|j                   |j                  |j                  |j
                  |j                  t        |j                  «      j                  dœ‘Œ\ c}t        | j                  «      j                  dœS c c}w )an  Render a roadmap into the JSON-compatible form stored in the index.

    ``depends_on`` is part of the persisted shape on purpose: dropping it here
    would make a stored roadmap claim to be dependency-free when it is not.

    Args:
        roadmap: The roadmap to render.

    Returns:
        A mapping that round-trips through :func:`_deserialize_roadmap`.
    ©ÚidÚroleÚdescriptionrp   Ú
depends_onÚstatus©rw   r!   Ústepsr{   )rw   r!   r}   rx   ry   rp   rz   r   r{   r*   r   )ÚroadmapÚss     r-   Ú_serialize_roadmapr€     sŠ   € ð �j‰jØ—‘ð —]‘]ö

ð ð —d‘dØŸ™Ø Ÿ}™}ØŸ9™9ØŸl™lÜ$ Q§X¡XÓ.×4Ñ4óò

ô   §¡Ó/×5Ñ5ñð ùò

s   ¥AB'c                óV  — | j                  dg «      D �cg c]\  }t        |d   |d   |d   |d   |j                  dg «      |j                  d«      rt        |d   «      nt        j                  ¬«      ‘Œ^ }}t	        | d   | d	   |t        | j                  dd
«      «      ¬«      S c c}w )ap  Rebuild a roadmap from its stored JSON form.

    Args:
        data: One entry of the ``roadmaps.json`` index.

    Returns:
        The roadmap, with step identity and ``depends_on`` preserved.

    Raises:
        ValueError: If a step or the roadmap carries an unknown status value.
        KeyError: If the required ``id``, ``name`` or step keys are missing.
    r}   rw   rx   ry   rp   rz   r{   rv   r!   Úpendingr|   )rq   r   r   ÚPENDINGr   r   )rF   r   r}   s      r-   Ú_deserialize_roadmapr„   /  s´   € ð, —‘˜' 2Ó&ö
ð ô 	Ø�‰wØ�6‘Ø˜-Ñ(Ø�i‘LØ—u‘u˜\¨2Ó.Ø./¯e©e°H¬o”:˜a ™kÔ*Ä:×CUÑCUö	
ð
€Eð 
ô Ø�‰:Ø�&‰\ØÜ˜TŸX™X h°	Ó:Ó;ô	ð ùò
s   •A!B&c                ó&  — | j                  |«      }|�|S t        |«      }||vr<t        j                  d|› dt	        |«      › d�d¬«       t        j
                  d«      ‚	 t        ||   «      }| j                  |j                  |j                  |j                  «      S # t        t        t        f$ r[}t        j                  d|› dt	        |«      › d	t        |«      j                  › d
|› d�	d¬«       t        j
                  d«      d‚d}~ww xY w)aÎ  Resolve a roadmap from the live bridge or from the stored index.

    Args:
        bridge: The bridge the roadmap will be executed through.
        data_dir: Resolved data directory holding ``roadmaps.json``.
        roadmap_id: Roadmap identifier supplied on the command line.

    Returns:
        The roadmap, re-registered on ``bridge`` so ``execute_step`` can find it.

    Raises:
        typer.Exit: With code 1 when no such roadmap exists anywhere.
    Nú	Roadmap 'z' was not found in z). Create it with 'cyberbrain plan' first.Tr7   r9   ú' in z is malformed (ú: z.). Refusing to run it; no step was dispatched.)Úget_roadmaprH   r@   rA   r4   rB   r„   Úcreate_roadmaprw   r!   r}   ÚKeyErrorÚ	TypeErrorr?   ÚtypeÚ__name__)Úbridger3   Ú
roadmap_idr~   ÚstoredrG   s         r-   Ú_load_roadmap_for_runr’   O  s  € ð × Ñ  Ó,€GØÐØˆÜ˜HÓ%€FØ˜ÑÜ�
‰
Ø˜
�|Ð#6´~ÀhÓ7OÐ6Pð Q6ð 6àõ	
ô
 �j‰j˜‹mÐð&Ü& v¨jÑ'9Ó:ˆð
 ×$Ñ$ W§Z¡Z°·±¸w¿}¹}ÓMÐMøÜ”i¤Ð,ò &Ü�
‰
Ø˜
�| 5¬¸Ó)AÐ(Bð CÜ�S“	×"Ñ"Ð# 2 c UÐ*XðZàõ	
ô
 �j‰j˜‹m Ð%ûð&ús   Á">B! Â!DÂ5ADÄDc                 ó*   — t        dt        › d�«       y)z%Display current CyberBrain26 version.z5[bold green]CyberBrain26[/bold green] version: [cyan]z[/cyan]N)r   r	   rV   r5   r-   Úversionr”   {  s   € ô 
ÐAÄ+ÀÈgÐ
VÕWr5   .z--goalz-gzThe high-level goal to plan.)r"   Údefaultz--idzRoadmap identifier.é   z--stepsz-nzNumber of planning steps.c                óº  — t        |«      }t        ||t        ¬«      }d|› d�}t        |j                  t
        «      r|j                  j                  |«       t        j                  |j                  |t        ddd| › �| |ddœ¬	«      «      «      }|j                  rø|j                  j                  dg «      }	t        |	d
¬«      D �
�cg c]:  \  }
}t        d|
› �d|j                  dd«      | |j                  dd«      dœ¬	«      ‘Œ< }}
}|j                  || |«      }t!        |«      }t#        |«      ||<   t%        ||«       t'        d|› dt)        |«      › d�«       |j*                  D ]'  }t'        d|j,                  › d|j.                  › �«       Œ) yt1        j2                  d|› d|j4                  xs d› d�d¬«       t1        j6                  d
«      ‚c c}}
w )a;  Generate a roadmap plan from a goal using the PlannerAgent.

    Planning is not an execution path: the planner only builds an in-memory task
    list, so there is nothing to authorize here. The generated roadmap still
    carries action-less steps, which 'run' refuses. Exits non-zero when
    planning fails.
    )r3   rS   rZ   zptah:z:step-0zstep-0ÚplannerzPlan for goal: Úplan)ÚgoalÚ	max_stepsro   )rw   rx   ry   rp   r9   )Ústartzstep-ry   Ú )rš   Úsub_taskz[bold green]Roadmap 'z' created with z steps[/bold green]z  - rˆ   zPlanning failed for roadmap 'z': úno error detail returnedz. No roadmap was written.Tr7   N)r   r]   r&   rC   Úqueuer
   Ú
delete_jobÚasyncioÚrunÚexecute_stepr   ÚsuccessrF   rq   Ú	enumeraterŠ   rH   r€   rN   r   Úlenr}   rw   ry   r@   rA   ÚerrorrB   )rš   r�   r}   r3   rS   Úresolved_data_dirr�   Ú
durable_idÚresultÚ	plan_dataÚir   Ústep_recordsr~   Úroadmapss                  r-   r™   r™   �  sá  € ô" (¨Ó1ÐÜØ"ØÜ!Bô€Fð ˜˜ GÐ,€JÜ�&—,‘,¤Ô0Ø�‰×Ñ 
Ô+Ü�[‰[˜×,Ñ,ØÜØØØ)¨$¨Ð0ð "°ÀÑHô	
óó €Fð ‡~‚~Ø—K‘K—O‘O F¨BÓ/ˆ	ô " )°1Ô5÷
ñ ��1ô Ø˜1˜#�;ØØŸE™E -°Ó4Ø!%°1·5±5¸ÈÓ3KÑLö	ð
ˆñ 
ð ×'Ñ'¨
°D¸,ÓGˆÜ!Ð"3Ó4ˆÜ1°'Ó:ˆ�ÑÜÐ(¨(Ô3ÜÐ% j \°ÄÀ\ÓARÐ@SÐSfÐgÔhØ—‘ò 	2ˆAÜ�D˜Ÿ™˜˜b §¡ Ð0Õ1ñ	2ô
 	�
‰
Ø+¨J¨<°sØ�|‰|Ò9Ð9Ð:Ð:SðUàõ	
ô
 �j‰j˜‹mÐùó1
s   Ã
?Gc           	     óì   — |j                   r&t        d| j                  › dt        | «      › d�«       yt	        j
                  d| j                  › dt        | «      › d|j                  xs d› d	�d¬
«       y)z6Print one step outcome; return True when it succeeded.z[green]z[/green]: action 'z' completedTúStep 'z' (action 'z') was refused or failed: rŸ   zP. Nothing is assumed to have run; inspect 'approvals pending' and the audit log.r7   F)r¥   r   rw   rt   r@   rA   r¨   )rs   r«   s     r-   Ú_report_step_resultr²   Ê  sz   € à‡~‚~Ü�˜Ÿ™�yÐ 2Ô3CÀDÓ3IÐ2JÈ+ÐVÔWØÜ	‡J�JØ
�—‘�	˜Ô%5°dÓ%;Ð$<Ð<VØ�<‰<Ò5Ð5Ð
6ð 79ð	9ð õ	ð r5   z-izRoadmap identifier to execute.r�   c                ó  ‡ ‡‡‡‡‡— t        |«      }t        ||¬«      }t        |«      }t        |«      }t	        ||||¬«      Št        j                  d‰ › d|› d|rdj                  |«      nd› d�«       t        ‰|‰ «      Š‰j                  s<t        j                  d‰ › d	t        |«      › d
�d¬«       t        j                  d«      ‚g Št        «       Št        «       Šdˆˆˆˆˆ ˆfd„}		 t        j                   |	«       «       ‰r‚‰rdt'        ‰«      › d�nd}t        j                  d‰ › dt'        ‰«      › dt'        ‰j                  «      › ddj                  t)        ‰«      «      › d|› �
d¬«       t        j                  d«      ‚t        j                  d‰ › dt'        ‰j                  «      › d�«       y# t        t         t"        t$        f$ r8}
t        j                  d‰ › d|
› �d¬«       t        j                  d«      d‚d}
~
ww xY w)aõ  Execute a roadmap's explicitly declared actions through the security plane.

    No model is called. Each step is dispatched via ``PtahBridge.execute_step``,
    which applies identity, scope, policy and approval before the agent runs,
    and records an audit entry either way. A step whose payload declares no
    ``action`` is refused rather than executed under an inferred default, and a
    step whose ``depends_on`` did not succeed is blocked rather than dispatched
    on a broken premise.
    r2   )r3   rS   rZ   rW   r†   z' on z; coder scopes: r`   z(none)ra   r‡   z) contains no steps. Nothing was executed.Tr7   r9   c            	   “  ó  •K  — ‰j                   D �]i  } t        | «      €Vt        j                  | _        t        j                  d| j                  › d�d¬«       ‰j                  | j                  «       Œet        t        | j                  «      ‰z
  «      }|r„t        j                  | _        t        j                  d| j                  › ddj                  |«      › d�d¬«       ‰j                  | j                  «       ‰j                  | j                  «       �Œ‰j                  ‰| «      ƒ d {  –—† }t!        | |«      r‰j                  | j                  «       �ŒO‰j                  | j                  «       �Œl y 7 ŒL­w)Nr±   z¶' declares no 'action' in its payload, so there is nothing to authorize. It was refused; add an explicit action to the step payload and try again. No default action is ever inferred.Tr7   z' depends on r`   z;, which did not succeed in this run. It was not dispatched.)r}   rt   r   ÚFAILEDr{   r@   rA   rw   rd   rf   Úsetrz   ÚBLOCKEDre   Úaddr¤   r²   )	rs   Úunmetr«   Úblockedr�   Úfailuresr~   r�   Ú	succeededs	      €€€€€€r-   Ú_executezrun.<locals>._execute
  s:  øè ø€ Ø—M‘Mó 	)ˆDÜ Ó%Ð-Ü(×/Ñ/�”Ü—
‘
Ø˜TŸW™W˜Ið &Vð Vð õ	ð —‘ §¡Ô(ØÜœ3˜tŸ™Ó/°)Ñ;Ó<ˆEÙô )×0Ñ0�”Ü—
‘
Ø˜TŸW™W˜I ]°4·9±9¸UÓ3CÐ2Dð EBð Bàõð
 —‘˜DŸG™GÔ$Ø—‘ §¡Ô(ÙØ!×.Ñ.¨z¸4Ó@×@ˆFÜ" 4¨Ô0Ø—‘˜dŸg™gÖ&à—‘ §¡Ö(ñ9	)ð0 Aús   ƒD.F Ä1E>Ä2AF z' aborted: Nz Of those, zJ step(s) were never dispatched because their dependencies did not succeed.r�   z' did NOT complete: z of z step(s) failed (z).z' completed: zQ step(s) executed and authorized. No model was called; only declared actions ran.©ÚreturnÚNone)r   r   rk   rm   r]   r@   rA   re   r’   r}   r4   rB   r¶   r¢   r£   r   ÚRuntimeErrorr>   r?   r§   rf   )r�   r3   rS   Úapproval_timeoutrh   r©   rP   rW   Útimeout_secondsr½   rG   Údetailrº   r�   r»   r~   r¼   s   `           @@@@@r-   r£   r£   Ø  s  ý€ ô* (¨Ó1ÐÜ˜bÐ+<Ô=€GÜ  Ó(€LÜ0Ð1AÓB€OÜØ"ØØ!0Ø!ô	€Fô 
‡J�JØ
�J�<˜u W IÐ-=Ù&2ˆ4�9‰9�\Ô"¸Ð
AÀð	Dôô $ FÐ,=¸zÓJ€Gà�=Š=ä�
‰
Ø˜
�| 5¬Ð8IÓ)JÐ(Kð L+ð +àõ	
ô
 �j‰j˜‹mÐà€HÜ›%€IÜ›€G÷)ò )ð>
&Ü�‰‘H“JÔñ ñ ð œ#˜g›,˜ð (,ñ ,ð ð	 	ô 	�
‰
Ø˜
�|Ð#7¼¸H»°ÀdÜ�7—=‘=Ó!Ð"Ð"3°D·I±I¼fÀXÓ>NÓ4OÐ3PÐPRØˆhðð õ		
ô �j‰j˜‹mÐÜ	‡J�JØ
�J�<˜}¬S°·±Ó-?Ð,@ð AFð 	Fõøô1 œ<¬´*Ð=ò &ô 	�
‰
�Y˜z˜l¨+°c°UÐ;ÀÕFÜ�j‰j˜‹m Ð%ûð&ús   Ã-F; Æ;HÇ3HÈHc                 óÈ   — t        d«      } t        | ¬«      }t        «       }|j                  j	                  dd¬«       t        «       }t        |«      }t        |||¬«      }|S )u3   VytvoÅ™Ã­ MemoryService s vÃ½chozÃ­mi komponentami.Nr2   T)ÚparentsÚexist_ok)rT   Ú	audit_logÚconfig)r   r   r   Úlancedb_directoryÚmkdirr   r   r   )r3   rP   rÉ   rÈ   rT   Úmemory_services         r-   Ú_get_memory_servicerÍ   N  si   € ô
 ˜tÓ$€HÜ xÔ0€GäÓ€FØ
×Ñ×"Ñ"¨4¸$Ð"Ô?ô “
€Iä˜gÓ&€Hô #ØØØô€Nð Ðr5   z--actionz;Action: ingest, search, hybrid-search, graph-search, or kg1z--pathz Path for ingest/ingest-directoryz--queryzQuery for searchÚtopicz
--kg1-typezType for KG1 nodez--business-semanticszBusiness semantics for KG1 nodez--domain-constraintszDomain constraints for KG1 nodeTz"--auto-classify/--no-auto-classifyz0Enable/disable auto-classification during ingestz$--validate-stale/--no-validate-stalez-Filter out superseded documents during searchFz
--timelinez=Show document timeline with valid_from/valid_to/superseded_byz--traversalz-Entity traversal: neighbors, path, or relatedrJ   z--depthu+   Hloubka prochÃ¡zenÃ­ grafu pro graph-searchc                óÖ  — | dk(  r.|€,t        j                  dd¬«       t        j                  d«      ‚| dk(  r.|€,t        j                  dd¬«       t        j                  d«      ‚| d	k(  r.|€,t        j                  d
d¬«       t        j                  d«      ‚t        «       }| dk(  �r|€J ‚t        j                  d|› �«       |rt        j                  d«       	 t	        |«      j                  «       }d}d}|j                  d«      D ]•  }|j                  «       sŒ|j                  j                  d«      rŒ0	 |j                  |«      \  }}|r(t        j                  d|j                  › �«       |dz  }n't        j                  d|j                  › �«       |dz  }Œ— t        j                  d|› d|› d�«       y| dv �r:|€J ‚t        j                  d|› �«       |rt        j                  d«       	 | d	k(  r)|j                  |d¬«      }t        j                  d«       n|j                  |d¬«      }|r¾|D ]·  }|j                  |j                  dd«      «      }|sŒ't        j                  d |j                  d«      › �«       t        j                  d!|j                   › �«       t        j                  d"|j"                  › �«       t        j                  d#|j$                  xs d$› �«       Œ¹ �n|rët        j                  d%t'        |«      › d&�«       t)        |d«      D ]¹  \  }}t'        |j                  d'd«      «      d(kD  r|j                  d'd«      dd( d)z   n|j                  d'd«      }|j                  d*d«      }|j                  d+d«      }t        j                  d,|› d-|› �«       t        j                  d.|j                  d«      › d/|› d0|› �«       Œ» nt        j                  d1«       yyy| d3k(  �ro|€J ‚t        j                  d4|› �«       	 |j+                  |d|
¬5«      }|r¾|D ]·  }|j                  |j                  dd«      «      }|sŒ't        j                  d |j                  d«      › �«       t        j                  d!|j                   › �«       t        j                  d"|j"                  › �«       t        j                  d#|j$                  xs d$› �«       Œ¹ �nV|�r>t        j                  d%t'        |«      › d6�«       t)        |d«      D �]  \  }}t'        |j                  d'd«      «      d(kD  r|j                  d'd«      dd( d)z   n|j                  d'd«      }t        j                  d,|› d-|› �«       t        j                  d.|j                  d«      › d7|j                  d8d«      d9›�«       t        j                  d:|j                  d;d$«      › �«       |j                  d<g «      }|sŒât        j                  d=d>j-                  |dd? «      › �«       �Œ nt        j                  d1«       t        j                  dA|› �«       |s,t        j                  dBd¬«       t        j                  d«      ‚	 t/        dC|› dDt1        |«      › �|t3        dEdFt4        j6                  t8        j:                  d¬G«      dH|idI|xs dJig ¬K«      }|j=                  |«      }t        j                  dL|j>                  › �«       t        j                  dM|j@                  › �«       t        j                  dNtC        |jD                  jG                  «       «      › �«       yt        j                  dP| › �d¬«       t        j                  d«      ‚# t        $ r7}t        j                  d|j                  › d|› �d¬«       |dz  }Y d}~�ŒÓd}~ww xY w# t        $ r5}t        j                  d|› �d¬«       t        j                  d«      d‚d}~ww xY w# t        $ r5}t        j                  d2|› �d¬«       t        j                  d«      d‚d}~ww xY w# t        $ r5}t        j                  d@|› �d¬«       t        j                  d«      d‚d}~ww xY w# t        $ r5}t        j                  dO|› �d¬«       t        j                  d«      d‚d}~ww xY w)QzQKnowledge plane entry: ingest directory, search embeddings, or register KG1 node.ÚingestNz--path is required for 'ingest'Tr7   r9   Úsearchz --query is required for 'search'úhybrid-searchz'--query is required for 'hybrid-search'u   IngestovÃ¡nÃ­ adresÃ¡Å™e: z0Auto-classifikace: zapnuta (entitace, typologie)r   Ú*ra   u     ZaregistrovÃ¡n novÃ½ soubor: u&     Soubor jiÅ¾ existuje (pÅ™eskoÄ�en): u     Chyba pÅ™i zpracovÃ¡nÃ­ rˆ   u   KompletnÄ› zpracovÃ¡no u    novÃ½ch souborÅ¯, u    pÅ™eskoÄ�eno.u   Chyba pÅ™i ingestovÃ¡nÃ­: )rÑ   rÒ   u   HledÃ¡nÃ­ s dotazem: uL   Staleness validace: zapnuta (vÃ½sledky bez platnÃ½ch entit budou vynechÃ¡ny)é
   )Úlimitz.Hybrid search: keyword + semantic + RRF fusionrw   r�   z  Dokument: z    valid_from: z    valid_to: z    superseded_by: zN/Az	Nalezeno u    vÃ½sledkÅ¯:Útextéd   z...Ú	source_idrE   z  z. z	     ID: z	, Zdroj: z	, Cesta: u   Å½Ã¡dnÃ© vÃ½sledky nenalezeny.u   Chyba pÅ™i vyhledÃ¡vÃ¡nÃ­: zgraph-searchzGraph search pro entitu: )rÕ   Údepthu$    vÃ½sledkÅ¯ (graph-based retrieval):u
   , SkÃ³re: Úscorez.3fu        GrafovÃ¡ vzdÃ¡lenost: Úgraph_distanceÚmatched_entitiesz     Matched entity: r`   é   u!   Chyba pÅ™i vyhledÃ¡vÃ¡nÃ­ grafu: u   RegistraÄ�nÃ­ KG1 node typu: u3   --business-semantics je povinnÃ© pro KG1 registracizkg1-ú-z	cli-inputzcli://input)rØ   Ú
source_uriÚtrustÚsensitivityÚapprovedÚprimaryr,   Úgeneral)rw   r�   ÚsourceÚbusiness_semanticsÚdomain_constraintsÚevidenceu   KG1 node zaregistrovÃ¡n: z  Typ: z  Business semantics: u   Chyba pÅ™i registraci KG1: u   NeznÃ¡mÃ¡ akce: )$r@   rA   rB   rÍ   r   ÚresolveÚrglobÚis_filer!   Ú
startswithÚregister_filer>   Úhybrid_searchÚsemantic_searchÚget_documentrq   Ú
valid_fromÚvalid_toÚsuperseded_byr§   r¦   Úgraph_searchre   r   Úhashr   r   ÚREVIEWEDr   ÚPUBLICÚregister_kg1_noderw   r�   r\   ræ   Úkeys)ro   rE   ÚqueryÚkg1_typeræ   rç   Úauto_classifyÚvalidate_staleÚtimelineÚ	traversalrÙ   rÌ   Ú	root_pathÚfiles_processedÚfiles_skippedÚ	file_pathÚ	_documentÚcreatedÚeÚresultsr«   Údocr­   rÖ   rØ   ÚentitiesÚkg1_nodeÚregistered_nodes                               r-   Ú	knowledger  g  s9  € ð �Ò˜d˜lÜ�
‰
Ð4¸$Õ?Ü�j‰j˜‹mÐØ�Ò˜e˜mÜ�
‰
Ð5¸4Õ@Ü�j‰j˜‹mÐØ�Ò  U ]Ü�
‰
Ð<À$ÕGÜ�j‰j˜‹mÐä(Ó*€Nà�ÓàÐÐÐÜ�
‰
Ð/°¨vÐ6Ô7ÙÜ�J‰JÐIÔJð	*ä˜T›
×*Ñ*Ó,ˆIØˆOØˆMà&Ÿ_™_¨SÓ1ò +�	Ø×$Ñ$Õ&¨y¯~©~×/HÑ/HÈÕ/Mð
+Ø-;×-IÑ-IÈ)Ó-TÑ*˜	 7Ù"Ü!ŸJ™JÐ)HÈÏÉÐHXÐ'YÔZØ+¨qÑ0™Oä!ŸJ™JÐ)OÐPY×P^ÑP^ÐO_Ð'`ÔaØ)¨QÑ.˜Møð+ô �J‰JÐ0°Ð0AÐATÐUbÐTcÐcqÐrÕsð 
Ð.Ò	.àÐ Ð Ð Ü�
‰
Ð*¨5¨'Ð2Ô3ÙÜ�J‰JÐeÔfð	*Ø˜Ò(Ø(×6Ñ6°uÀBÐ6ÓG�Ü—
‘
ÐKÕLà(×8Ñ8¸ÀbÐ8ÓI�áà%ò W�FØ(×5Ñ5°f·j±jÀÀrÓ6JÓK�CÚÜŸ
™
 \°&·*±*¸TÓ2BÐ1CÐ#DÔEÜŸ
™
Ð%5°c·n±nÐ5EÐ#FÔGÜŸ
™
 ^°C·L±L°>Ð#BÔCÜŸ
™
Ð%8¸×9JÑ9JÒ9SÈeÐ8TÐ#UÕVòWñ Ü—
‘
˜Y¤s¨7£| n°LÐAÔBÜ!*¨7°AÓ!6ò b‘I�A�vÜCFÀvÇzÁzÐRXÐZ\ÓG]ÓC^ÐadÒCd˜6Ÿ:™: f¨bÓ1°$°3Ð7¸%Ò?Ðjp×jtÑjtÐu{Ð}ó  kA�DØ &§
¡
¨;¸Ó ;�IØ!Ÿ:™: f¨bÓ1�DÜ—J‘J  A 3 b¨¨Ð/Ô0Ü—J‘J ¨6¯:©:°dÓ+;Ð*<¸IÀiÀ[ÐPYÐZ^ÐY_Ð`Õañbô —
‘
Ð;Õ<ðbðWð, 
�>Ó	!àÐ Ð Ð Ü�
‰
Ð.¨u¨gÐ6Ô7ð	*Ø$×1Ñ1°%¸rÈÐ1ÓOˆGÙØ%ò W�FØ(×5Ñ5°f·j±jÀÀrÓ6JÓK�CÚÜŸ
™
 \°&·*±*¸TÓ2BÐ1CÐ#DÔEÜŸ
™
Ð%5°c·n±nÐ5EÐ#FÔGÜŸ
™
 ^°C·L±L°>Ð#BÔCÜŸ
™
Ð%8¸×9JÑ9JÒ9SÈeÐ8TÐ#UÕVòWò Ü—
‘
˜Y¤s¨7£| nÐ4XÐYÔZÜ!*¨7°AÓ!6ó V‘I�A�vÜCFÀvÇzÁzÐRXÐZ\ÓG]ÓC^ÐadÒCd˜6Ÿ:™: f¨bÓ1°$°3Ð7¸%Ò?Ðjp×jtÑjtÐu{Ð}ó  kA�DÜ—J‘J  A 3 b¨¨Ð/Ô0Ü—J‘J ¨6¯:©:°dÓ+;Ð*<¸JÀvÇzÁzÐRYÐ[\ÓG]Ð^aÐFbÐcÔdÜ—J‘JÐ!<¸V¿Z¹ZÐHXÐZ_Ó=`Ð<aÐbÔcØ%Ÿz™zÐ*<¸bÓA�HÚÜŸ
™
Ð%:¸4¿9¹9ÀXÈbÈqÀ\Ó;RÐ:SÐ#TÖUñVô —
‘
Ð;Ô<ô
 	�
‰
Ð2°8°*Ð=Ô>á!Ü�J‰JÐLÐRVÕWÜ—*‘*˜Q“-Ðð	*äØ˜(˜ 1¤TÐ*<Ó%=Ð$>Ð?ØÜ&Ø)Ø,Ü'×0Ñ0Ü +× 2Ñ 2Ø!ôð %.Ð/AÐ#BØ$+Ð-?Ò-LÀ9Ð#MØôˆHð -×>Ñ>¸xÓHˆOÜ�J‰JÐ2°?×3EÑ3EÐ2FÐGÔHÜ�J‰J˜ ×!5Ñ!5Ð 6Ð7Ô8Ü�J‰JÐ/´°_×5WÑ5W×5\Ñ5\Ó5^Ó0_Ð/`ÐaÕbô 	�
‰
Ð% f XÐ.°DÕ9Ü�j‰j˜‹mÐøô_ #ò +ÜŸ
™
Ð%?À	ÇÁÐ?OÈrÐRSÐQTÐ#UÐ[_Õ`Ø%¨Ñ*žûð+ûô ò 	*Ü�J‰JÐ3°A°3Ð7¸TÕBÜ—*‘*˜Q“- TÐ)ûð	*ûôJ ò 	*Ü�J‰JÐ4°Q°CÐ8¸dÕCÜ—*‘*˜Q“- TÐ)ûð	*ûô< ò 	*Ü�J‰JÐ:¸1¸#Ð>ÀDÕIÜ—*‘*˜Q“- TÐ)ûð	*ûô@ ò 	*Ü�J‰JÐ4°Q°CÐ8¸dÕCÜ—*‘*˜Q“- TÐ)ûð	*ús©   ÃA_' Ä _' Ä=A%^$Æ"_' Ç:A+`( É&F`( Ð#>a) Ñ"F'a) Ø
Aa) ÚC b* Þ$	_$Þ-,_ß_' ß_$ß$_' ß'	`%ß00` à `%à(	a&à10a!á!a&á)	b'á20b"â"b'â*	c(â30c#ã#c(c                 ó   — t        «        y r(   )ÚapprV   r5   r-   Úmainr  
  s   € Ü…Er5   Ú__main__)r3   r   r¿   r   )r3   r   r¿   údict[str, Any])r3   r   rF   r  r¿   rÀ   )rS   úPath | Noner3   r   r¿   r   )
r3   r   rS   r  rZ   r%   rW   zSequence[str]r¿   r   )rg   rr   r¿   z	list[str])r*   r%   r¿   r%   )rs   r   r¿   ú
str | None)r~   r   r¿   r  )rF   r  r¿   r   )r�   r   r3   r   r�   rr   r¿   r   r¾   )rš   rr   r�   rr   r}   Úintr3   úGAnnotated[Path | None, typer.Option('--data-dir', help=_DATA_DIR_HELP)]rS   ú;Annotated[Path | None, typer.Option('--db', help=_DB_HELP)]r¿   rÀ   )rs   r   r«   r   r¿   Úbool)
r�   rr   r3   r  rS   r  rÂ   zQAnnotated[float, typer.Option('--approval-timeout', help=_APPROVAL_TIMEOUT_HELP)]rh   z;Annotated[str, typer.Option('--scopes', help=_SCOPES_HELP)])r¿   r   )ro   rr   rE   r  rú   r  rû   rr   ræ   rr   rç   rr   rü   r  rý   r  rþ   r  rÿ   r  rÙ   r  )TÚ__doc__Ú
__future__r   r¢   r;   Úcollections.abcr   Úpathlibr   Útypingr   r   r@   Úrichr   Úcyberbrain26r	   Úcyberbrain26.control_planer
   Ú$cyberbrain26.data_plane.sqlite_storer   Ú&cyberbrain26.knowledge_plane.contractsr   r   r   r   Ú-cyberbrain26.knowledge_plane.embedding_configr   Ú+cyberbrain26.knowledge_plane.memory_servicer   Ú&cyberbrain26.orchestration.ptah_bridger   r   r   r   r   r   Úcyberbrain26.product.approvalsr   Úcyberbrain26.product.doctorr   Ú cyberbrain26.product.local_storer   r   r   Ú!cyberbrain26.security_plane.auditr   Ú$cyberbrain26.security_plane.identityr   Ú&cyberbrain26.security_plane.permissionr   ÚTyperr  Ú	add_typerÚ_DB_HELPÚ_DATA_DIR_HELPÚ_APPROVAL_TIMEOUT_HELPÚ_SCOPES_HELPr1   r&   Ú__annotations__Ú	frozensetr/   r4   rH   rN   rU   r]   rk   rm   rt   r€   r„   r’   Úcommandr”   ÚOptionr™   r²   r£   rÍ   r  r  rŽ   rV   r5   r-   ú<module>r5     s   ðò"õH #ã Û Ý $Ý ß !ã Ý å $Ý 6Ý ?÷ó õ JÝ E÷÷ õ 9Ý 2ß \Ñ \Ý 6Ý @Ý @à€e‡k�k�|Ð*FÔG€à ‡�ˆm +€Ô .Ø ‡�ˆj˜x€Ô (àD€ØE€ðKð ðJð ð %Ð ð ,1Ð ! 5Ó 0ñ !*Ñ)SÀ?Ô)SÓ S€ˆ~Ó Só)ó
ó:DóðD #%ñàðð 	ðð $ð	ð
  ðð óóBó:
ó
ó ó<ó@&&ðX ‡�ƒòXó ðXð
 ‡�ƒà�—‘˜S (¨DÐ7UÔVØ"�e—l‘l 9¨fÐ;PÔQØ�—‘˜a ¨DÐ7RÔSð 	ØFJðEØ
ðEàðEð ðEðð	Eð 	DðEð 
òEó ðEóPð ‡�ƒà"�e—l‘l 3¨°Ð;[Ô\ð 	ØFJð 	*ØJLðmØðmððmð
 	Dðmððmð Hòmó ðmójð2 ‡�ƒà�%—,‘,˜s JÐ5rÔsØ#�u—|‘| D¨(Ð9[Ô\Ø$˜Ÿ™ T¨9Ð;MÔNØ �E—L‘L ¨,Ð=PÔQØ*˜eŸl™l¨4Ð1GÐNoÔpØ*˜eŸl™l¨4Ð1GÐNoÔpØ&˜%Ÿ,™, tÐ-Qð  YKô  LØ'˜5Ÿ<™<¨Ð.Tð  \Kô  LØ!�U—\‘\ %¨Ð<{Ô|Ø(˜EŸL™L¨¨}ÐCrÔsØ�—‘˜a Ð1^Ô_ð_Øð_à
ð_ð ð_ð ð	_ð
 ð_ð ð_ð ð_ð ð_ð ð_ð ð_ð ò_ó ð_óD
ð ˆzÒÙ…Fð r5   