+
    t,i*                    >   R t ^ RIHt ^ RIt^ RIt^ RIt^ RIHt ^ RIHt ]P                  P                  ^ ]! ]! ]4      P                  4       P                  4      4       ^ RIHtHtHt R R ltR R	 ltR
 R lt]R8X  d   ]P.                  ! ]! 4       4       R# R# )u  G4.4 canary — token-count delta (system.md, tiktoken cl100k_base) (Task 4.5).

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

Override O-1 binding gate (signed convention):
  token_delta = candidate_token_count - baseline_token_count
  pass condition: token_delta <= -540  (system-only)
which is mathematically equivalent to baseline - candidate >= 540.

`system_plus_inject_delta <= -150` = secondary report-only metric (no gate).

R3-M2 carry — this script is **CP-free**: it reads prompt files directly
via filesystem and does NOT import `compute_continuity_snapshot_hash`. The
`continuity_card_snapshot_hash` echoed in pinning is operator-supplied and
verified by analyzer cross-check against the 4 metric scripts.

Trap #6 — token gate signed: candidate < baseline ⇒ delta is negative.

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

Usage (config mode):
    python scripts/canary/g4_4_token_count.py \
        --config canary_config.json \
        --baseline-version 18.202605041549 \
        --candidate-version 19.<timestamp> \
        --output results/g4_4_token_count.json
)annotationsN)datetime)Path)emit_outputload_pinning_from_argsvalidate_pinningc                   V ^8  d   QhRR/# )   returnstr )formats   ""scripts/canary/g4_4_token_count.py__annotate__r   0   s     / /S /    c                 V    \         P                  ! 4       P                  4       R ,           # )Z)r   utcnow	isoformatr   r   r   iso8601_nowr   0   s    ??&&(3..r   c                   V ^8  d   QhRR/# )r	   r
   intr   )r   s   "r   r   r   4   s     \! \!c \!r   c                 
   \         P                  ! R R7      p V P                  R\        RR7       V P                  R\        RRR7       V P                  R	\        RR
R7       V P                  R\        RRR7       V P                  R\        RJRR7       V P                  R\        RKRR7       V P                  R\        RRR7       V P                  R\        RRR7       V P                  R\        RR7       V P                  R\        RR7       V P                  R\        RR7       V P                  R\        RR7       V P                  R\        R. RLOR7       V P                  R\        RR7       V P                  R\        RR7       V P                  R\        RR 7       V P                  4       p\        V4      p\        V4       . p ^ RIpTP!                  R$4      p\#        TP$                  4      pYP&                  ,          R%,          p	YP(                  ,          R%,          p
T	P+                  4       '       g   TP                  R&T	 24       T
P+                  4       '       g   TP                  R'T
 24       T'       dA   \        YT4      p\        Ya4       T F!  p\        R"T 2\        P                  R#7       K#  	  ^#  T	P-                  R(R)7      pT
P-                  R(R)7      p \1        TP3                  T4      4      p\1        TP3                  T4      4      pY,
          pTTP4                  8*  pTP6                  e@   TP8                  e2   TP6                  pTP8                  pTT,
          pTTP:                  8*  pMRpRpRpRp/ R.\=        4       bR/TbR0TP&                  bR1TP(                  bR2TbR3TbR4TbR5TP4                  bR6TbR7TbR8TbR9TbR:TP:                  bR;TbR<R=bR>TbR?R@RA/bp\        Ya4       \        RBTP&                   RCT RDTP(                   RCT RET RFTP4                   RGT RHT RI2\        P                  R#7       T'       d   ^ # ^#   \         dM    R!pTP                  T4       \        YT4      p\        Ya4       \        R"T 2\        P                  R#7        ^# i ; i  \.         dU   pTP                  R*T 24       \        YT4      p\        Ya4       \        R+T 2\        P                  R#7        Rp?^# Rp?ii ; i  \.         dU   pTP                  R,T 24       \        YT4      p\        Ya4       \        R-T 2\        P                  R#7        Rp?^# Rp?ii ; i)MzPG4.4 canary: token-count delta (system.md, tiktoken cl100k_base, R3-M2 CP-free).)descriptionz--configN)typedefaultz--baseline-versionTz=Baseline prompt version directory name (e.g. 18.202605041549))r   requiredhelpz--candidate-versionz=Candidate prompt version directory name (e.g. 19.<timestamp>)z--prompt-base-pathzprompts/_base/scene_detailz)Base directory containing version subdirs)r   r   r   z--exit-criteria-thresholdzFtoken_delta upper bound for pass (default: -540, Override O-1 binding)z(--system-plus-inject-secondary-thresholdzGsystem+inject combined delta secondary threshold (report-only, no gate)z--baseline-system-plus-injectzVOptional baseline system+inject token count (operator-supplied, secondary report-only)z--candidate-system-plus-injectzWOptional candidate system+inject token count (operator-supplied, secondary report-only)z--pidz--scene-index-listz--shot-index-list-per-scenez--model-routingz--prompt-source-mode)r   r   choicesz--card-commit-hashz--continuity-card-snapshot-hashz--output)r   r   zatiktoken not installed. Install via `pip install tiktoken` (or backend venv). NO silent fallback.z[g4_4_token_count] ERROR: )filecl100k_basez	system.mdzbaseline system.md not found: zcandidate system.md not found: zutf-8)encodingzprompt file read failed: z3[g4_4_token_count] ERROR: prompt file read failed: ztiktoken encode failed: z2[g4_4_token_count] ERROR: tiktoken encode failed: 	timestamppinningbaseline_versioncandidate_versionbaseline_token_countcandidate_token_counttoken_deltaexit_criteria_thresholdexit_criteria_passsystem_plus_inject_baselinesystem_plus_inject_candidatesystem_plus_inject_delta&system_plus_inject_secondary_threshold!system_plus_inject_secondary_passmeasurement_tooltiktoken cl100k_basemeasurement_failuresmeasurement_scriptstoken_countzcscripts/canary/g4_4_token_count.py (system.md, tiktoken cl100k_base, R3-M2 CP-free, Trap #6 signed)z[g4_4_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: z
  system_plus_inject_delta: z (secondary report-only)iij)Nr   db)argparseArgumentParseradd_argumentr   r   
parse_argsr   r   tiktokenImportErrorappend_failure_outputr   printsysstderrget_encodingr   prompt_base_pathr$   r%   exists	read_text	Exceptionlenencoder)   baseline_system_plus_injectcandidate_system_plus_injectr.   r   )parserargsr#   r2   r:   msgoutencoderbase_dirbaseline_pathcandidate_pathbaseline_textcandidate_textexcr&   r'   r(   	exit_passspi_baselinespi_candidate	spi_deltaspi_secondary_passs                         r   mainrZ   4   sZ   $$4F 
d;
!	   L	   ,8	   #4   2%   '9   (9   c48
,3E
5CN
)TB
$	   ,3E
)T   
t<D$T*GW ') ##M2GD))*H444{BM 6 66DN !!##,]O<	
   ""##-n-=>	
 d-ABC'C.se43::F (%///A'1171C"7>>-#@A #GNN>$B C (>Kt;;;I 	((4--97799!L0	DDD 	 	![]7 	D11 	T33	
 	 4 	 !6 	{ 	"4#?#? 	i 	&| 	' 	#I 	177  	,-?!" 	2#$ 	 4%& 	" 
'C8 	,,-S
  !../s 
! "% '445 6!!* ,''0k 2"
	# ZZ 1 q   " 	
 	##C(d-ABC*3%0szzBL  ##&?u$EFd-ABCA#G	
   ##&>se$DEd-ABC@F	
 sE   Q $R3 )4T AR0/R03T>A	TTU4 A	U//U4c               (    V ^8  d   QhRRRRRRRR/# )r	   rK   zargparse.Namespacer#   dictr2   listr
   r   )r   s   "r   r   r     s0      
  
	r   c                    / R \        4       bRVbRV P                  bRV P                  bRRbRRbRRbRV P                  bR	R
bRRbRRbRRbRV P                  bRRbRRbRVbRRR/b# )r"   r#   r$   r%   r&   Nr'   r(   r)   r*   Fr+   r,   r-   r.   r/   r0   r1   r2   r3   r4   zSscripts/canary/g4_4_token_count.py (system.md, tiktoken cl100k_base, R3-M2 CP-free))r   r$   r%   r)   r.   )rK   r#   r2   s   &&&r   r=   r=     s    
[]7 	D11 	T33	
 	 	  	t 	"4#?#? 	e 	&t 	' 	#D 	177  	,T!" 	2#$ 	 4%& 	C 
' r   __main__)__doc__
__future__r   r6   jsonr?   r   pathlibr   pathinsertr   __file__resolveparent_g4_4_commonr   r   r   r   rZ   r=   __name__exitr   r   r   <module>rl      s   : #   
   3tH~--/667 8 /\!~B zHHTV r   