
    `i                       d Z ddlmZ ddlZddlZddlZddlmZ ddlmZ ej                  j                  d e ee      j                         j                               ddlmZmZ d
dZddZed	k(  r ej*                   e              yy)u  G4.3 canary — token-count delta (system.md, tiktoken cl100k_base) (Task 4.5).

Computes baseline (v17) and candidate (v18) token counts for
`prompts/_base/scene_detail/<version>/system.md` and emits the signed delta.

Override R4-B4 carry: signed convention everywhere —
  token_delta = candidate_token_count - baseline_token_count
  pass condition: token_delta <= -1050
which is mathematically equivalent to baseline - candidate >= 1050.

Override R1R2-B2 / R2-I3 carry: token gate `≥ -1050` after Phase 2 ID-policy
lift. Estimate -1065 with 15-token slack.

Exit criteria (analyzer):
  exit_criteria_pass == True  iff  token_delta <= -1050  (default threshold)

Usage (config mode):
    python scripts/canary/g4_3_token_count.py \
        --config canary_config.json \
        --baseline-prompt-version 17.202605042018 \
        --candidate-prompt-version 18.<timestamp> \
        --output results/g4_3_token_count.json
    )annotationsN)datetime)Path)load_pinning_from_argsvalidate_pinningc                 L    t        j                         j                         dz   S )NZ)r   utcnow	isoformat     "scripts/canary/g4_3_token_count.pyiso8601_nowr   )   s    ??&&(3..r   c                 (   t        j                  d      } | j                  dt        d        | j                  dt        dd       | j                  d	t        dd
       | j                  dt        dd       | j                  dt        dd       | j                  dt        d        | j                  dt        d        | j                  dt        d        | j                  dt        d        | j                  dt        d g d       | j                  dt        d        | j                  dt        d        | j                  dt        d       | j                         }t        |      }t        |       	 dd l}|j                  d!      }t        |j                        }||j                   z  d"z  }||j"                  z  d"z  }|j%                         st        d#| t        j                         y |j%                         st        d$| t        j                         y |j'                  d%&      }|j'                  d%&      }	t)        |j+                  |            }
t)        |j+                  |	            }||
z
  }||j,                  k  }t/               ||j                   |j"                  |
|||j,                  |d'd(d)id*}|j0                  d+k(  rHt3        j4                  |t        j6                  d,d-d.       t        j6                  j9                  d/       nht        |j0                        }|j:                  j=                  dd0       |j?                  d1d%&      5 }t3        j4                  ||d,d-d.       d d d        t        d2|j                    d3|
 d4|j"                   d3| d5| d6|j,                   d7| t        j                         |rdS d S # t        $ r t        dt        j                         Y y w xY w# 1 sw Y   xY w)8NzAG4.3 canary: token-count delta (system.md, tiktoken cl100k_base).)descriptionz--config)typedefaultz--baseline-prompt-versionTz=Baseline prompt version directory name (e.g. 17.202605042018))r   requiredhelpz--candidate-prompt-versionz=Candidate prompt version directory name (e.g. 18.<timestamp>)z--prompt-base-pathzprompts/_base/scene_detailz)Base directory containing version subdirs)r   r   r   z--exit-criteria-thresholdiz>token_delta upper bound for pass (default: -1050, R4-B4 carry)z--pidz--scene-index-listz--shot-index-list-per-scenez--model-routingz--prompt-source-mode)Nfiledb)r   r   choicesz--card-commit-hashz--id-policy-card-snapshot-hashz--output)r   r   r   z{[g4_3_token_count] ERROR: tiktoken not installed. Install via `pip install tiktoken` (or backend venv). NO silent fallback.)r      cl100k_basez	system.mdz8[g4_3_token_count] ERROR: baseline system.md not found: z9[g4_3_token_count] ERROR: candidate system.md not found: zutf-8)encodingztiktoken cl100k_basetoken_countzRscripts/canary/g4_3_token_count.py (system.md, tiktoken cl100k_base, R4-B4 signed))	timestamppinningbaseline_prompt_versioncandidate_prompt_version"system_prompt_token_count_baseline#system_prompt_token_count_candidatetoken_deltaexit_criteria_thresholdexit_criteria_passmeasurement_toolmeasurement_scripts-F   )ensure_asciiindent	sort_keys
)parentsexist_okwz[g4_3_token_count]
  baseline (z): z tokens
  candidate (z tokens
  token_delta: z.  (signed: candidate - baseline)
  threshold: z<  (pass iff token_delta <= threshold)
  exit_criteria_pass: ) argparseArgumentParseradd_argumentstrint
parse_argsr   r   tiktokenImportErrorprintsysstderrget_encodingr   prompt_base_pathr   r    exists	read_textlenencoder$   r   outputjsondumpstdoutwriteparentmkdiropen)parserargsr   r7   encoderbase_dirbaseline_pathcandidate_pathbaseline_textcandidate_textbaseline_token_countcandidate_token_countr#   	exit_passoutout_pathfps                    r   mainrX   -   s   $$WF 
d;
3#\  ^
43\  ^
,3 <H  J 3#u]  _ c48
,3E
5CN
)TB
.S$ 4  6
,3E
8sDQ

t<D$T*GW ##M2GD))*Ht;;;kIM = ==KN !F}oV	
   "GGWX	
 !++W+=M#--w-?Nw~~m<=~ >? (*>>Kt;;;I !]#'#?#?$($A$A.B/D"#'#?#?'2o 
C  {{c		#szza4P

$dT:]]3]1RIIc2E!tL 2 
334C8L7M N556c:O9P Q% '445 6!!*	- ZZ 1 q Q  X	

 x 21s   =O 'P$PPP__main__)returnr4   )rZ   r5   )__doc__
__future__r   r1   rC   r:   r   pathlibr   pathinsertr4   __file__resolverG   _g4_3_commonr   r   r   rX   __name__exitr   r   r   <module>re      sx   . #   
   3tH~--/667 8/l!^ zCHHTV r   