okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8feca419f
main
1"""Read golden files and print, as Markdown, where the entry points differ and (with --against) what changed between two sets of golden files. 2 3python -m tests.snapshot.report (the golden files of this tree) 4python -m tests.snapshot.report --golden A --against B (A = this tree's, B = another tree's, e.g. the branch f-0213-df2ux) 5""" 6 7from __future__import annotations 8 9import argparse 10import collections 11import json 12import sys 13from pathlib import Path 14 15from .matrix import ENTRY_POINTS ,MACHINES ,MODEL_SETS 16 17GOLDEN = Path (__file__ ).resolve ().parent / "golden" 18 19#: config keys and variables that differ between the Terminal and the app by design: where things live and who they belong to (not what the engine runs). 20IDENTITY_KEYS = {"port" ,"api_key" ,"aliases" ,"log" ,"mcp" ,"engine_ready_s" ,"fit_max_tokens" } 21 22 23def load (root :Path ): 24out = {} 25for m in MACHINES : 26for s in MODEL_SETS : 27p = root / m .key / f" { s . key } .json" 28if p .is_file (): 29out [(m .key ,s .key )]= json .loads (p .read_text (encoding = "utf-8" ))["entries" ] 30return out 31 32 33def flags (argv ): 34out ,i = [],0 35while i < len (argv ): 36if argv [i ].startswith ("--" )and i + 1 < len (argv )and not argv [i + 1 ].startswith ("--" ): 37out .append ((argv [i ],argv [i + 1 ])) 38i += 2 39else : 40out .append ((argv [i ],None )) 41i += 1 42return out 43 44 45def drafter (rec )-> str : 46eng = rec .get ("engine" ) 47if not eng : 48return "not driven" if rec .get ("not_driven" )else "?" 49a = eng ["argv" ] 50if "--dspark" in a : 51return "DSpark " + a [a .index ("--dspark" )+ 1 ] 52if "--draft" in a : 53pack = a [a .index ("--draft-model" )+ 1 ] 54return "DFlash2 %s conf %s spec %s" % ("tuned" if "strata1" in pack else "stock" ,a [a .index ("--draft-conf" )+ 1 ],a [a .index ("--spec" )+ 1 ]) 55return "MTP" if "--mtp" in a else "none" 56 57 58def table (data )-> list [str ]: 59lines = ["| machine class | set | Terminal launch | app plain Load | app Models-screen Load | same? |" ,"|---|---|---|---|---|---|" ] 60for m in MACHINES : 61for s in MODEL_SETS : 62e = data .get ((m .key ,s .key )) 63if not e : 64continue 65la ,pl ,mo = drafter (e ["launch" ]),drafter (e ["app_plain" ]),drafter (e ["app_models" ]) 66lines .append (f"| { m . key } | { s . key } | { la } | { pl } | { mo } | { 'yes' if la == pl == mo else '**NO**' } |" ) 67return lines 68 69 70def differences (data ,a = "launch" ,b = "app_plain" ): 71kinds = collections .defaultdict (list ) 72for (mk ,sk ),e in data .items (): 73A ,B = e [a ],e [b ] 74fa ,fb = set (flags (A ["engine" ]["argv" ])),set (flags (B ["engine" ]["argv" ])) 75for x in sorted (fa - fb ,key = str ): 76kinds [("argument" ,f"only in { a } : { ' ' . join ( v for v in x if v ) } " )].append (f" { mk } / { sk } " ) 77for x in sorted (fb - fa ,key = str ): 78kinds [("argument" ,f"only in { b } : { ' ' . join ( v for v in x if v ) } " )].append (f" { mk } / { sk } " ) 79ea ,eb = A ["engine" ]["env_set" ],B ["engine" ]["env_set" ] 80for k in sorted (set (ea )| set (eb )): 81if ea .get (k )!= eb .get (k ): 82kinds [("environment" ,f" { k } : { a } { ea . get ( k , '<unset>' ) } | { b } { eb . get ( k , '<unset>' ) } " )].append (f" { mk } / { sk } " ) 83ca ,cb = A ["config_file" ],B ["config_file" ] 84for k in sorted (set (ca )| set (cb )): 85if k in ("args" ,"args_derived" ,"args_written" ): 86continue 87if ca .get (k ,"<absent>" )!= cb .get (k ,"<absent>" ): 88kind = "identity/location" if k in IDENTITY_KEYS else "config key" 89kinds [(kind ,f" { k } : { a } { short ( ca . get ( k , '<absent>' )) } | { b } { short ( cb . get ( k , '<absent>' )) } " )].append (f" { mk } / { sk } " ) 90if [f ["file" ]for f in A .get ("fetches" )or []]!= [f ["file" ]for f in B .get ("fetches" )or []]: 91kinds [("download" ,f"drafter fetched: { a } vs { b } " )].append (f" { mk } / { sk } " ) 92return kinds 93 94 95def short (v )-> str : 96s = json .dumps (v )if not isinstance (v ,str )else v 97s = s .replace ("<ROOT>" ,"" ).replace ("/local/Strata/launch/" ,"launch/" ) 98return s if len (s )< 48 else s [:45 ]+ "..." 99 100 101def render_differences (data ,a ,b )-> list [str ]: 102kinds = differences (data ,a ,b ) 103lines = [f"### { a } vs { b } " ,"" ,"| kind | difference | cells |" ,"|---|---|---|" ] 104for (kind ,text ),cells in sorted (kinds .items ()): 105shown = ", " .join (cells [:3 ])+ (f" ... ( { len ( cells ) } )" if len (cells )> 3 else "" ) 106lines .append (f"| { kind } | { text } | { shown } |" ) 107if not kinds : 108lines .append ("| - | none | - |" ) 109return lines + ["" ] 110 111 112def changed (a ,b )-> list [str ]: 113cnt = collections .defaultdict (list ) 114for key in sorted (a ): 115for entry in ENTRY_POINTS : 116ra ,rb = a [key ].get (entry ), (b .get (key )or {}).get (entry ) 117if ra != rb : 118parts = sorted (k for k in set (ra or {})| set (rb or {})if (ra or {}).get (k )!= (rb or {}).get (k )) 119cnt [(entry ,tuple (parts ))].append ("%s/%s" % key ) 120lines = ["| entry | what changed | cells |" ,"|---|---|---|" ] 121for (entry ,parts ),cells in sorted (cnt .items ()): 122lines .append (f"| { entry } | { ', ' . join ( parts ) } | { len ( cells ) } : { ', ' . join ( cells [: 4 ]) } { ' ...' if len ( cells ) > 4 else '' } |" ) 123return lines 124 125 126def main (argv = None )-> int : 127ap = argparse .ArgumentParser () 128ap .add_argument ("--golden" ,default = str (GOLDEN )) 129ap .add_argument ("--against" ) 130a = ap .parse_args (argv ) 131data = load (Path (a .golden )) 132"## Drafter each entry point starts the engine with\n" ) 133"\n" .join (table (data ))+ "\n" ) 134"## Where the entry points differ\n" ) 135for x ,y in (("launch" ,"app_plain" ), ("launch" ,"app_models" ), ("app_models" ,"app_plain" )): 136"\n" .join (render_differences (data ,x ,y ))) 137if a .against : 138other = load (Path (a .against )) 139"## Changes between the two sets of golden files\n" ) 140"\n" .join (changed (data ,other ))+ "\n" ) 141"## The other set's drafter table\n" ) 142"\n" .join (table (other ))+ "\n" ) 143return 0 144 145 146if __name__ == "__main__" : 147sys .exit (main ())