ó
    À‘´j#  ã                  ó”   • S r SSKJr  SSKJr  SSKJrJrJrJ	r	  SSK
Jr  SSKJr  SSKJr  SSKJr  \(       a  SS	KJr  S
rSrSS jrg)u  Document heading review â€” demote page decoration the parser called a heading.

The parser labels certain printed lines as section headings, and downstream
consumers treat a heading as governing every chunk beneath it. When the label
is wrong â€” a banner the page layout repeats at the top of its pages, or a
table column label that leaked into the heading stream â€” that false context
spreads across a large share of the document's text.

Demotion is deliberately hard, because wrongly demoting a real heading strips
section context from everything under it. Two witnesses must agree:

1. Geometry nominates: the same text appears as a heading on
   ``NOMINATION_PAGES`` or more pages, computed from the parse.
2. The heading-review agent confirms, judging only the nominated texts.

A heading that appears on fewer pages is never put to the agent, and an
unavailable review demotes nothing.

One demotion follows from another instead of from a verdict. The parser can
split a running header in two and label its first half a heading. Once the
agent confirms a text as a running header, any heading occurrence whose text
opens that header and is printed within ``SPLIT_HEADER_TOLERANCE`` of the
header's position band is demoted too, whatever its page count.

Every demotion is returned as a flag record, the run's record that the relabel
happened and why. A verdict demotion names the text, its pages, the verdict and
reason, and how many text items it governed. A split-header demotion names the
page and where the line is printed against the header's band.
é    )Úannotations)Údefaultdict)ÚTYPE_CHECKINGÚDictÚListÚSet)ÚDocItemLabel)Úlogger)Ú	LLMClient)ÚCellFlag)ÚDoclingDocumenté   g{®Gáz”?c              ƒ  óÒ  ^#   • [        [        5      n[        [        5      m[        [        5      nU R                  =(       d    0 R                  5        VVs0 s H$  u  pVXVR                  R                  =(       d    S_M&     nnnU R                   GH]  nUR                  [        R                  :X  d  M$  UR                  R                  5       (       d  ME  UR                  (       a  UR                  S   R                  OSn	X8R                  R                  5          R!                  U	5        XHR                  R                  5       ==   S-  ss'   UR                  (       d  MÓ  UR                  S   R"                  c  Mï  UR%                  U	S5      n
TUR                  R                  5          R'                  [)        SX¨R                  S   R"                  R*                  -
  U
-  5      5        GM`     UR                  5        VVs0 s H  u  p¼[-        U5      [.        :¼  d  M  X¼_M     nnnU(       d  / $ S(U4S jjn[1        UR                  5       S S9 VVs/ s HF  u  p¼S	U S
XK    S[-        U5       SSR3                  S [1        U5       5       5       SU" U5       3
PMH     nnnUR5                  SSR3                  U5      -   5      I Sh  v•N nUc"  [6        R8                  " S[-        U5      5        / $ UR:                   Vs0 s HP  nUR<                  S:w  d  M  UR                  R                  5       U;   d  M5  UR                  R                  5       U_MR     nnU(       d  / $ [        [        5      nSnU R                   HR  nUR                  [        R                  :X  a  UR                  R                  5       nM=  UU;   d  ME  UU==   S-  ss'   MT     / nUR                  5        GH-  u  nnSnU R                   Hm  nUR                  [        R                  :X  d  M#  UR                  R                  5       U:X  d  MC  [>        RA                  US[        RB                  5        US-  nMo     [1        XÛ   5      n[6        RD                  " SUR<                  USS [-        U5      UU   5        UR'                  [G        UUS   UUSUR<                   S[-        U5       SSR3                  S U 5       5       SU SUU    SURH                   3S95        GM0     UR                  5        VVs0 s HM  u  nnUR<                  S:X  d  M  TR%                  U5      (       d  M0  U[K        TU   5      [)        TU   5      4_MO     nnnU R                   GHª  nUR                  [        R                  :w  d+  UR                  (       a  UR                  S   R"                  c  MO  UR                  R                  5       nUR                  S   R                  n	UR%                  U	S5      n
[)        SX¨R                  S   R"                  R*                  -
  U
-  5      nUR                  5        HÐ  u  nu  nnURM                  U5      (       a  [-        U5      [-        U5      :  d  M9  U[N        -
  Us=::  a  U[N        -   ::  d  O  MX  [>        RA                  US[        RB                  5        [6        RD                  " S USS U	UUSS 5        UR'                  [G        UU	UUSS!U< S"U	 S#US$ S%US$ S&US$ S'3S95          GM¨     GM­     U$ s  snnf s  snnf s  snnf  GNŠs  snf s  snnf 7f))a.  Demote confirmed page decoration from the document's heading stream.

Relabels demoted headings to ``page_header`` in place on ``document`` and
returns one flag record per demotion. Returns an empty list when nothing
is nominated, the review backend does not support it, or the agent keeps
everything.
g      ð?r   é   Ng        c                ó¸   >• TR                  U 5      nU(       d  g[        U5      [        U5      p2X2-
  S:”  a
  US SUS S3OUS S3nX2-
  S::  a  SOSnSU S	U 3$ )
Nzposition unknowngš™™™™™©?ú.0%Ú-z down the pageza fixed positionzvarying positionszprinted at ú, )ÚgetÚminÚmax)ÚtextÚtopsÚloÚhiÚbandÚfixedÚheading_topss         €Ú>/home/mande/repo/quber/src/quber/core/fusion/heading_review.pyÚposition_evidenceÚ*review_headings.<locals>.position_evidenceR   sv   ø€ Ø×Ñ Ó%ˆÞØ%Ü�T“œC ›IˆBØ57±W¸t³^�"�S�˜˜2˜c˜( .Ñ1ÈBÈsÈ8ÐSaÐIbˆØ&(¡g°£oÑ"Ð;NˆØ˜U˜G 2 d VÐ,Ð,ó    c                ó    • [        U S   5      * $ )Nr   )Úlen)Úkvs    r   Ú<lambda>Ú!review_headings.<locals>.<lambda>^   s   € ÄSÈÈAÉÃZÁKr"   )Úkeyz- "u   " â€” z# occurrence(s) as a heading across z pages (r   c              3  ó8   #   • U  H  n[        U5      v •  M     g 7f©N©Ústr©Ú.0Úps     r   Ú	<genexpr>Ú"review_headings.<locals>.<genexpr>]   s   é € Ð4¢m ”c˜!—f�f¢mùó   ‚z); z&Nominated headings of this document:

Ú
zOheading review unavailable on this backend; keeping all {} nominated heading(s)Úsection_headingÚ ÚlabeluN   heading demoted ({}): {!r} â€” a heading on {} pages, governed {} text item(s)é<   Úheading_demotedz on c              3  ó8   #   • U  H  n[        U5      v •  M     g 7fr*   r+   r-   s     r   r0   r1   –   s   é € Ð!8²%¨Q¤# a§& &²%ùr2   z); relabeled z heading item(s) governing z text item(s); )ÚsourceÚpageÚtitler   ÚstatusÚnoteÚrunning_headerzZheading demoted (split running header): {!r} on page {} at {:.0%} down the page opens {!r}z!first half of the running header z, printed on page z at r   z' down the page where that header sits (r   Ú))r   r,   Úreturnr,   )(r   ÚsetÚlistÚintÚpagesÚitemsÚsizeÚheightÚtextsr6   r	   ÚSECTION_HEADERr   ÚstripÚprovÚpage_noÚaddÚbboxr   Úappendr   Útr$   ÚNOMINATION_PAGESÚsortedÚjoinÚreview_headingsr
   ÚwarningÚverdictsÚverdictÚobjectÚ__setattr__ÚPAGE_HEADERÚinfor   Úreasonr   Ú
startswithÚSPLIT_HEADER_TOLERANCE)ÚdocumentÚllmr:   Úheading_pagesÚheading_countÚnor/   Úpage_heightsÚitemr;   Úhr   rE   Ú	nominatedr    ÚlinesÚreviewÚvÚdemotedÚgovernedÚcurrentÚflagsrX   Ú	relabeledÚbandsÚtopÚheaderr   r   r   s                                @r   rU   rU   7   s�  øé € ô *5´SÓ)9€MÜ+6´tÓ+<€LÜ$/´Ó$4€MØ;C¿>¹>×;OÈR×:VÑ:VÔ:XÔYÒ:X±°�BŸ™Ÿ™×,¨Ò,Ñ:X€LÑYØ—•ˆØ�:‰:œ×4Ñ4Õ4¸¿¹¿¹×9JÓ9JØ+/¯9¯9�4—9‘9˜Q‘<×'Ò'¸!ˆDØŸ)™)Ÿ/™/Ó+Ñ,×0Ñ0°Ô6ØŸ)™)Ÿ/™/Ó+Ó,°Ñ1Ó,Ø�y�y‰y˜TŸY™Y q™\×.Ñ.Ó:ð !×$Ñ$ T¨3Ó/�Ø˜TŸY™YŸ_™_Ó.Ñ/×6Ñ6´s¸3ÀÇYÁYÈqÁ\×EVÑEV×EXÑEXÑAXÐ\]Ñ@]Ó7^×_ñ ð 1>×0CÑ0CÔ0EÔhÒ0E¡ ÌÈUËÔWgÑIg“�’Ñ0E€IÑhÞØˆ	÷-ô " )§/¡/Ó"3Ñ9OÒPôò Q‰KˆDð ˆdˆV�6˜-Ñ-Ð.Ð.QÔRUÐV[ÓR\ÐQ]ð ^Ø�I‰IÑ4¤f¨U¤mÓ4Ó4Ð5°SÑ9JÈ4Ó9PÐ8Qó	SáPð 
ñ ð
 ×&Ñ&Ð'QÐTX×T]ÑT]Ð^cÓTdÑ'dÓe×e€FØ�~Ü�ŠØ]Ü�	‹Nô	
ð ˆ	ð —’óâ ˆAØ�9‰9Ð)Ñ)ó 	à./¯f©f¯l©l«nÀ	Ñ.Ió 	ˆ�‰�‰‹˜ÒÙ ð ð ö
 Øˆ	ô
  +¬3Ó/€HØ€GØ—”ˆØ�:‰:œ×4Ñ4Ó4Ø—i‘i—o‘oÓ'ŠGØ˜ÕØ�WÓ Ñ"Õñ	 ð €EØ Ÿ™Ÿ‰ˆˆgØˆ	Ø—N”NˆDØ�z‰zœ\×8Ñ8Õ8¸T¿Y¹Y¿_¹_Ó=NÐRVÕ=Vô ×"Ñ" 4¨´,×2JÑ2JÔKØ˜Q‘’	ñ #ô �y‘Ó'ˆÜ�ŠØ\Ø�O‰OØ��"ˆIÜ�‹JØ�T‰Nô	
ð 	�‰ÜØØ˜1‘XØØØ(à—‘Ð' t¬C°«J¨<ð 8ØŸ	™	Ñ!8±%Ó!8Ó8Ð9¸ÀyÀkð R)Ø)1°$©Ð(8¸ÈÏÉÐGWðYñ÷	
ñ# )ðR %Ÿ]™]œ_ôâ,‰MˆD�'Ø�?‰?Ð.Ñ.ó 	Aà3?×3CÑ3CÀD×3Ió 	AˆŒs�< Ñ%Ó&¬¨L¸Ñ,>Ó(?Ð@Ò@Ù,ð 
ñ ð
 —•ˆØ�:‰:œ×4Ñ4Ó4¸D¿I¿IÈÏÉÐSTÉ×IZÑIZÑIbÙØ�y‰y�‰Ó ˆØ�y‰y˜‰|×#Ñ#ˆØ×Ñ˜T 3Ó'ˆÜ�#˜ŸI™I a™L×-Ñ-×/Ñ/Ñ/°1Ñ4Ó5ˆØ %§¡¦ÑˆF‘H�R˜Ø×%Ñ% d×+Ñ+´°D³	¼CÀ»KÓ0GÙØÔ/Ñ/°3ÕU¸"Ô?UÑ:UÕUÙÜ×Ñ˜t W¬l×.FÑ.FÔGÜ�KŠKØlØ�S�b�	ØØØ�s˜�ôð �L‰LÜØ!ØØØØ,à;¸F¹:ÐEWÐX\ÐW]ð ^Ø! #˜YÐ&MÈbÐQTÈXÐUVÐWYÐZ]ÐV^Ð^_ðañ
ôó ô5 !.ñ ðD €LùóQ Zùó iùóò
 fùòùóvùs£   ƒA]'Á+]Â/]'Â5]'ÃB
]'Å$]'Æ B]'È]È#]È)0]'ÉA]Ê&)]'Ë]Ë7]'Ì]Ì]Ì?]ÍA)]'ÏA]'Ð&]'ÑC-]'Ô3]!Õ]!Õ&!]!ÖG]'Ý]'N)r`   z'DoclingDocument'ra   r   r:   r,   rA   zList[CellFlag])Ú__doc__Ú
__future__r   Úcollectionsr   Útypingr   r   r   r   Údocling_core.types.doc.labelsr	   Úlogurur
   Úquber.agents.llm_clientr   Úquber.core.extractors.baser   Údocling_core.types.doc.documentr   rR   r_   rU   © r"   r   Ú<module>r~      s@   ðñõ< #å #ß 1Ó 1å 6Ý å -Ý /æÝ?ð
 Ð ð Ð õSr"   