okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b0fc432ed
main
1"""Wide, read-only detail pages for the terminal monitor. 2 3Every value comes from the bounded telemetry snapshot. These pages are a 4presentation layer: they never inspect prompt bodies or query the engine. 5""" 6 7from __future__import annotations 8 9from typing import Any 10 11from rich .console import Group 12from rich .panel import Panel 13from rich .text import Text 14 15from strata_cli import nav 16from strata_cli .client import complete_slots 17from strata_cli .dashboard import (set_ascii_boxes ,_rounded ,_age ,_current_batch_value ,_current_sample , 18_device_role ,_gib ,_meter ,_rate_spark ,_spark , 19_state ,_visual_pair ,_visual_panel ,MonitorHistory ) 20from strata_cli .render import (VIEWS ,_device_stage ,_queue_wait ,_recent_detail , 21_request_detail ,_slot_detail ,activity_continuity_notice , 22activity_label ,activity_rows ,field ,number , 23request_rows ,safe ) 24 25 26def _palette (color :bool ,theme :str )-> dict [str ,str ]: 27if not color : 28return {key :"default" for key in ("cyan" ,"green" ,"amber" ,"violet" ,"red" )} 29if theme == "light" : 30return {"cyan" :"#087b9a" ,"green" :"#0c8a60" ,"amber" :"#946000" , 31"violet" :"#7842af" ,"red" :"#b01a2e" } 32return {"cyan" :"#22d3ee" ,"green" :"#34d399" ,"amber" :"#fbbf24" , 33"violet" :"#a78bfa" ,"red" :"#ff8a98" } 34 35 36def _txt (value :str ,style :str = "" )-> Text : 37return Text (value ,style = style ,no_wrap = True ,overflow = "crop" ) 38 39 40def _panel (lines :list [Text ],title :str ,accent :str ,color :bool ,height :int )-> Panel : 41capacity = max (1 ,height - 2 ) 42if len (lines )> capacity : 43lines = lines [:capacity - 1 ]+ [_txt ("… more rows; use Up/Down or filter" ,"dim" )] 44return _visual_panel (lines ,title ,accent ,color ,height = height ) 45 46 47def _window (rows :list [Any ],selected :int ,capacity :int )-> tuple [int ,list [Any ]]: 48start = min (max (0 ,selected - capacity // 2 ),max (0 ,len (rows )- capacity )) 49return start ,rows [start :start + capacity ] 50 51 52def _route (row :dict )-> str : 53replica = f"R { row [ 'replica' ] } /" if type (row .get ("replica" ))is int else "" 54slot = f"S { row [ 'slot' ] } " if type (row .get ("slot" ))is int else "--" 55return replica + slot 56 57 58def _request_line (row :dict ,snapshot :dict ,slots :list [dict ]| None , 59* ,selected :bool ,live :bool ,color :bool ,palette :dict [str ,str ])-> Text : 60state = safe (row .get ("state" ),10 ) 61prompt = (f" { number ( row . get ( 'prompt_read' )) } / { number ( row . get ( 'prompt_tokens' )) } " 62if state == "reading" else number (row .get ("prompt_tokens" ))) 63rate = next ((slot .get ("tok_s" )for slot in slots or [] 64if slot .get ("slot" )== row .get ("slot" )and slot .get ("request_id" )== row .get ("id" )),None ) 65wait = number (_queue_wait (row ,snapshot .get ("sampled_at" )),"s" ,1 ) 66first = number (row .get ("first_engine_token_s" ),"s" ,2 ) 67line = (f" { '▸' if selected else ' ' } { field ( row . get ( 'id' ), 12 ) } { state :<10 } " 68f" { _route ( row ):<7 } { prompt :<11 } { number ( row . get ( 'generated' )):>5 } " 69f" { number ( rate if live else None , '' , 1 ):>6 } { wait :>5 } { first :>6 } " ) 70tint = (palette ["amber" ]if state in ("queued" ,"reading" )else 71palette ["green" ]if state == "decoding" else palette ["violet" ]) 72return _txt (line ,tint if color else "" ) 73 74 75def _request_heading ()-> Text : 76return _txt (" ID STATE ROUTE PROMPT OUT SLOT/s WAIT 1ST" ,"dim" ) 77 78 79def _device_lines (row :dict ,* ,selected :bool ,topology :dict , 80history :MonitorHistory ,glyphs :str ,live :bool , 81palette :dict [str ,str ],color :bool )-> list [Text ]: 82index = row .get ("index" ) 83util = row .get ("util" )if live else None 84used = row .get ("mem_used" )if live else None 85total = row .get ("mem_total" )if live else None 86power = row .get ("power" )if live else None 87temp = row .get ("temp" )if live else None 88marker = "▸" if selected else " " 89name = field (row .get ("name" ),20 ) 90header = _txt (f" { marker } GPU { number ( index ):<2 } { name } { _device_role ( index , topology ):<4 } " 91f" { number ( util , '%' ):>4 } { number ( temp , 'C' ):>4 } { number ( power , 'W' ):>5 } " , 92palette ["green" ]if color else "" ) 93bars = _txt (f" LOAD { _meter ( util , 100 , 14 , glyphs ) } " 94f"VRAM { _meter ( used , total , 12 , glyphs ) } { _gib ( used ) } / { _gib ( total ) } G" , 95palette ["green" ]if color else "" ) 96trend = _txt (f" RECENT { _spark ( history . device_util . get ( index ) if live else (), glyphs , 17 ) } " 97f"VRAM { _spark ( history . device_vram . get ( index ) if live else (), glyphs , 15 ) } " , 98"dim" ) 99return [header ,bars ,trend ] 100 101 102def _detail_lines (kind :str ,row :dict ,snapshot :dict ,width :int , 103live :bool )-> list [Text ]: 104if kind == "device" : 105topology = snapshot .get ("topology" )or {} 106lines = [f"GPU { number ( row . get ( 'index' )) } { safe ( row . get ( 'name' ), 40 ) } " , 107f"Load { number ( row . get ( 'util' ) if live else None , '%' ) } " 108f"VRAM { _gib ( row . get ( 'mem_used' ) if live else None ) } /" 109f" { _gib ( row . get ( 'mem_total' ) if live else None ) } GiB" , 110f"Temp { number ( row . get ( 'temp' ) if live else None , 'C' ) } " 111f"Power { number ( row . get ( 'power' ) if live else None , 'W' ) } " , 112_device_stage (topology ,row .get ("index" ),width )] 113elif kind == "slot" : 114lines = _slot_detail (row if live else {** row ,"tok_s" :None },width )[1 :] 115elif kind == "recent" : 116lines = _recent_detail (row ,width )[1 :] 117else : 118lines = _request_detail (row ,snapshot .get ("sampled_at" ),width )[2 :] 119return [_txt (line )for line in lines ] 120 121 122def _serving (snapshot :dict ,history :MonitorHistory ,* ,heights :tuple [int ,int ,int ], 123selected :int ,detail :bool ,pane :str ,filter_text :str , 124live :bool ,glyphs :str ,color :bool ,palette :dict [str ,str ], 125width :int )-> Group : 126work ,rates = snapshot .get ("work" )or {},snapshot .get ("rates" )or {} 127topology ,devices = snapshot .get ("topology" )or {},snapshot .get ("devices" )or [] 128slots = complete_slots (work ) 129top_h ,mid_h ,low_h = heights 130focus = f"PANE { pane . upper () } · Tab: devices / requests / groups" 131top_left = [_txt (f" { number ( work . get ( 'active' )) } ACTIVE " 132f" { number ( work . get ( 'occupied_slots' )) } / { number ( work . get ( 'slots_effective' )) } SLOTS " 133f" { number ( work . get ( 'waiting' )) } ENGINE WAIT" ,palette ["green" ]if color else "" ), 134_txt (f"ALL OUTPUT { number ( rates . get ( 'aggregate_output_tok_s' ) if live else None , ' tok/s' , 1 ) } " 135f"PROMPT { number ( rates . get ( 'prefill_tok_s' ) if live else None , ' tok/s' , 1 ) } " , 136palette ["cyan" ]if color else "" ), 137_txt (f" { safe (( snapshot . get ( 'model' ) or {}). get ( 'id' ), 35 ) } · " 138f" { safe ( topology . get ( 'mode' ), 25 ) } " ,"dim" ), 139_txt (focus ,palette ["amber" ]if color else "" )] 140top_right = [_txt (f" { number ( _current_batch_value ( work , 'effective_rows_window' ), '' , 1 ) } " 141f"ROWS/WINDOW processed " 142f" { number ( _current_batch_value ( work , 'batch_active_rows_window' ), '' , 1 ) } active" , 143palette ["violet" ]if color else "" ), 144_txt (f" { number ( work . get ( 'queued' )) } PRE-ADMIT " 145f" { number ( work . get ( 'admission_reserved' )) } / { number ( work . get ( 'admission_limit' )) } " 146f"RESERVED/LIMIT" ,palette ["amber" ]if color else "" ), 147_txt (f"Recent rows { _rate_spark ( history . batch_rows if live else (), glyphs ) } " 148f"pre-admit { _rate_spark ( history . pre_admit if live else (), glyphs ) } " ,"dim" ), 149_txt ("Rows/window is an engine sample; queue is before admission." ,"dim" )] 150replica_batches = work .get ("replica_batches" )or [] 151if replica_batches : 152top_right = [_txt (f"R { number ( row . get ( 'replica' )) } ROWS/WIN " 153f" { number ( _current_batch_value ( row , 'effective_rows_window' ), '' , 1 ) } processed / " 154f" { number ( _current_batch_value ( row , 'batch_active_rows_window' ), '' , 1 ) } active" , 155palette ["violet" ]if color else "" ) 156for row in replica_batches [:2 ]] 157top_right .append (_txt (f" { number ( work . get ( 'queued' )) } PRE-ADMIT " 158f" { number ( work . get ( 'admission_reserved' )) } /" 159f" { number ( work . get ( 'admission_limit' )) } RESERVED/LIMIT" , 160palette ["amber" ]if color else "" )) 161top_right .append (_txt ("Replica sample windows are independent." ,"dim" )) 162requests ,_ = request_rows (snapshot ,filter_text ) 163request_start ,request_shown = _window (requests ,selected if pane == "requests" else 0 , 164max (1 ,mid_h - 5 )) 165flow = [_request_heading ()] 166for i ,row in enumerate (request_shown ,request_start ): 167flow .append (_request_line (row ,snapshot ,slots ,selected = pane == "requests" and i == selected , 168live = live ,color = color ,palette = palette )) 169if not requests : 170flow .append (_txt ("Request identities unavailable" if work .get ("requests" )is None else 171"No requests match the filter" if filter_text else 172"No active requests" ,"dim" )) 173if len (requests )> len (request_shown ): 174flow .append (_txt (f"Showing { request_start + 1 } - { request_start + len ( request_shown ) } " 175f"of { len ( requests ) } ; use Requests for the full list" ,"dim" )) 176queue = [_txt (f"PRE-ADMIT { _meter ( work . get ( 'queued' ), work . get ( 'max_queue' ), 20 , glyphs ) } " 177f" { number ( work . get ( 'queued' )) } / { number ( work . get ( 'max_queue' )) } " , 178palette ["amber" ]if color else "" ), 179_txt (f"Oldest wait { number ( work . get ( 'oldest_queue_s' ), 's' , 1 ) } " 180f"timeout { number ( work . get ( 'queue_timeout_s' ), 's' , 1 ) } " ), 181_txt (f"Rejected { number ( work . get ( 'admission_rejected' )) } " 182f"timed out { number ( work . get ( 'queue_timeouts' )) } " 183f"disconnect cancels { number ( work . get ( 'client_disconnect_cancels' )) } " ), 184_txt ("" ,"" ),_txt ("ENGINE SLOT FLOW" ,"dim" )] 185for state ,label ,shade in (("reading" ,"PREFILL" ,palette ["amber" ]), 186 ("decoding" ,"DECODE" ,palette ["green" ]), 187 ("idle" ,"IDLE" ,"dim" )): 188count = sum (row .get ("state" )== state for row in slots or []) 189queue .append (_txt (f" { label :<8 } { number ( count ) if slots is not None else '--' :>3 } " 190f" { _meter ( count if slots is not None else None , len ( slots or []), 20 , glyphs ) } " , 191shade if color else "" )) 192queue .append (_txt ("Groups are pipeline batch groups, not GPU assignments." ,"dim" )) 193slot_rows = work .get ("slots" )or [] 194slot_start ,slot_shown = _window (slot_rows ,selected if pane == "groups" else 0 , 195max (1 ,low_h - 5 )) 196slot_lines = [_txt (" SLOT GROUP STATE REQUEST HELD / CONTEXT SLOT/s" ,"dim" )] 197context = (snapshot .get ("model" )or {}).get ("context" ) 198for i ,row in enumerate (slot_shown ,slot_start ): 199mark = "▸" if pane == "groups" and i == selected else " " 200slot_lines .append (_txt (f" { mark } S { number ( row . get ( 'slot' )):<4 } " 201f"G { number ( row . get ( 'group' )):<5 } { safe ( row . get ( 'state' ), 10 ):<11 } " 202f" { field ( row . get ( 'request_id' ), 12 ) } " 203f" { number ( row . get ( 'held_tokens' )):>6 } / { number ( context ):<7 } " 204f" { number ( row . get ( 'tok_s' ) if live else None , '' , 1 ):>6 } " , 205palette ["green" ]if row .get ("state" )== "decoding" and color else 206palette ["amber" ]if row .get ("state" )== "reading" and color else "dim" )) 207if not slot_rows : 208slot_lines .append (_txt ("Slot detail unavailable" ,"dim" )) 209if len (slot_rows )> len (slot_shown ): 210slot_lines .append (_txt (f"Slots { slot_start + 1 } - { slot_start + len ( slot_shown ) } of { len ( slot_rows ) } " ,"dim" )) 211device_lines :list [Text ]= [] 212device_start ,device_shown = _window (devices ,selected if pane == "devices" else 0 ,4 ) 213for i ,row in enumerate (device_shown ,device_start ): 214index = row .get ("index" ) 215device_lines .append (_txt (f" { '▸' if pane == 'devices' and i == selected else ' ' } GPU " 216f" { number ( index ) } { field ( row . get ( 'name' ), 18 ) } " 217f" { _device_role ( index , topology ):<4 } " 218f" { number ( row . get ( 'util' ) if live else None , '%' ):>4 } " 219f" { _gib ( row . get ( 'mem_used' ) if live else None ) } /" 220f" { _gib ( row . get ( 'mem_total' ) if live else None ) } G" , 221palette ["green" ]if color else "" )) 222device_lines .append (_txt (f" { _meter ( row . get ( 'util' ) if live else None , 100 , 14 , glyphs ) } " 223f"VRAM { _meter ( row . get ( 'mem_used' ) if live else None , row . get ( 'mem_total' ) if live else None , 13 , glyphs ) } " , 224palette ["green" ]if color else "" )) 225if not devices : 226device_lines .append (_txt ("Per-device readings unavailable" ,"dim" )) 227if len (devices )> 4 : 228device_lines .append (_txt (f"Cards { device_start + 1 } - { device_start + len ( device_shown ) } " 229f"of { len ( devices ) } · Hardware shows all" ,"dim" )) 230if detail : 231rows = devices if pane == "devices" else requests if pane == "requests" else slot_rows 232if rows and selected < len (rows ): 233kind = "device" if pane == "devices" else "live" if pane == "requests" else "slot" 234device_lines .extend (_detail_lines (kind ,rows [selected ],snapshot ,width - 70 ,live )[- 3 :]) 235return Group (_visual_pair (_panel (top_left ,"SERVING / REQUEST PRESSURE" ,palette ["cyan" ],color ,top_h ), 236_panel (top_right ,"BATCHING / ADMISSION" ,palette ["violet" ],color ,top_h ),1 ,1 ), 237_visual_pair (_panel (flow ,f"REQUEST FLOW · { len ( requests ) } visible" ,palette ["cyan" ],color ,mid_h ), 238_panel (queue ,"QUEUE / SLOT PHASES" ,palette ["amber" ],color ,mid_h ),4 ,3 ), 239_visual_pair (_panel (slot_lines ,"ENGINE SLOTS / CONTEXT" ,palette ["violet" ],color ,low_h ), 240_panel (device_lines ,"GPU FLEET / FOCUS" ,palette ["green" ],color ,low_h ),4 ,3 )) 241 242 243def _requests (snapshot :dict ,* ,heights :tuple [int ,int ,int ],selected :int , 244detail :bool ,filter_text :str ,live :bool ,glyphs :str , 245color :bool ,palette :dict [str ,str ],width :int )-> Group : 246work ,rates = snapshot .get ("work" )or {},snapshot .get ("rates" )or {} 247slots = complete_slots (work ) 248active ,recent = request_rows (snapshot ,filter_text ) 249rows = [("live" ,row )for row in active ]+ [("recent" ,row )for row in recent ] 250top_h ,mid_h ,low_h = heights 251top_left = [_txt (f" { len ( active ) } LIVE { len ( recent ) } RECENT " 252f" { number ( work . get ( 'queued' )) } PRE-ADMIT " 253f" { number ( work . get ( 'waiting' )) } ENGINE WAIT" , 254palette ["green" ]if color else "" ), 255_txt (f" { number ( rates . get ( 'aggregate_output_tok_s' ) if live else None , ' tok/s' , 1 ) } " 256f"ALL OUTPUT { number ( rates . get ( 'answer_tok_s' ) if live else None , ' tok/s' , 1 ) } " 257f"ANSWER" ,palette ["cyan" ]if color else "" ), 258_txt ("IDs and counts only · no prompt or tool text" ,"dim" )] 259top_right = [_txt (f"Occupied { number ( work . get ( 'occupied_slots' )) } /" 260f" { number ( work . get ( 'slots_effective' )) } slots " 261f"Context { ( number (( snapshot . get ( 'model' ) or {}). get ( 'context' ))) } / slot" , 262palette ["violet" ]if color else "" ), 263_txt (f"Oldest queue { number ( work . get ( 'oldest_queue_s' ), 's' , 1 ) } " 264f"Last first engine { number ( recent [ 0 ]. get ( 'first_engine_token_s' ) if recent else None , 's' , 2 ) } " ), 265_txt ("Up/Down selects · Enter opens detail · / filters" ,"dim" )] 266live_start ,live_shown = _window (active ,selected if selected < len (active )else 0 , 267max (1 ,mid_h - 5 )) 268live_lines = [_request_heading ()] 269for i ,row in enumerate (live_shown ,live_start ): 270live_lines .append (_request_line (row ,snapshot ,slots ,selected = i == selected , 271live = live ,color = color ,palette = palette )) 272if not active : 273live_lines .append (_txt ("No live request summaries" if not filter_text else 274"No live requests match the filter" ,"dim" )) 275if len (active )> len (live_shown ): 276live_lines .append (_txt (f"Showing { live_start + 1 } - { live_start + len ( live_shown ) } of { len ( active ) } " ,"dim" )) 277recent_lines = [_txt (" ID FINISH IN CACHE OUT TIME FIRST" ,"dim" )] 278recent_start ,recent_shown = _window (recent ,max (0 ,selected - len (active )),max (1 ,low_h - 4 )) 279for i ,row in enumerate (recent_shown ,recent_start ): 280recent_lines .append (_txt (f" { '▸' if selected == len ( active ) + i else ' ' } " 281f" { field ( row . get ( 'id' ), 12 ) } { safe ( row . get ( 'finish' ), 10 ):<11 } " 282f" { number ( row . get ( 'prompt_tokens' )):>6 } " 283f" { number ( row . get ( 'reused' )):>5 } " 284f" { number ( row . get ( 'output_tokens' )):>5 } " 285f" { number ( row . get ( 'duration_s' ), 's' , 1 ):>6 } " 286f" { number ( row . get ( 'first_engine_token_s' ), 's' , 2 ):>5 } " ,"dim" )) 287if not recent : 288recent_lines .append (_txt ("No completed request summaries retained" ,"dim" )) 289if len (recent )> len (recent_shown ): 290recent_lines .append (_txt (f"Showing { recent_start + 1 } - { recent_start + len ( recent_shown ) } of { len ( recent ) } " ,"dim" )) 291detail_lines :list [Text ]= [] 292if rows : 293kind ,row = rows [min (max (0 ,selected ),len (rows )- 1 )] 294detail_lines .append (_txt (f" { safe ( row . get ( 'id' ), 28 ) } · { kind . upper () } " , 295palette ["green" ]if color else "" )) 296if detail : 297detail_lines .extend (_detail_lines (kind ,row ,snapshot ,width // 2 ,live )) 298elif kind == "live" : 299detail_lines .extend ([_txt (f" { safe ( row . get ( 'state' ), 12 ) } · { _route ( row ) } · " 300f" { number ( row . get ( 'prompt_tokens' )) } prompt tokens" ), 301_txt (f" { number ( row . get ( 'generated' )) } output · " 302f"wait { number ( _queue_wait ( row , snapshot . get ( 'sampled_at' )), 's' , 1 ) } " )]) 303else : 304detail_lines .extend ([_txt (f" { safe ( row . get ( 'finish' ), 12 ) } · " 305f" { number ( row . get ( 'output_tokens' )) } output tokens" ), 306_txt (f"Duration { number ( row . get ( 'duration_s' ), 's' , 2 ) } " )]) 307else : 308detail_lines .append (_txt ("Select a request to inspect its redacted telemetry" ,"dim" )) 309detail_lines .append (_txt ("Read only. Request text is never collected." ,"dim" )) 310if not detail : 311detail_lines .append (_txt ("Enter expands the selected row" ,"dim" )) 312return Group (_visual_pair (_panel (top_left ,"REQUESTS / CONCURRENCY" ,palette ["cyan" ],color ,top_h ), 313_panel (top_right ,"LATENCY / CONTEXT" ,palette ["amber" ],color ,top_h ),1 ,1 ), 314_panel (live_lines ,f"IN FLIGHT · { len ( active ) } " ,palette ["green" ],color ,mid_h ), 315_visual_pair (_panel (recent_lines ,f"RECENTLY FINISHED · { len ( recent ) } " ,palette ["violet" ],color ,low_h ), 316_panel (detail_lines ,"SELECTED REQUEST" ,palette ["cyan" ],color ,low_h ),1 ,1 )) 317 318 319def _engine (snapshot :dict ,history :MonitorHistory ,* ,heights :tuple [int ,int ,int ], 320live :bool ,glyphs :str ,color :bool ,palette :dict [str ,str ])-> Group : 321model ,work = snapshot .get ("model" )or {},snapshot .get ("work" )or {} 322rates ,topology = snapshot .get ("rates" )or {},snapshot .get ("topology" )or {} 323slots = complete_slots (work ) 324top_h ,mid_h ,low_h = heights 325top_left = [_txt (f" { safe ( topology . get ( 'mode' ), 30 ) } " 326f" { number ( len ( topology . get ( 'selected_devices' ) or [])) } SELECTED GPUS" , 327palette ["cyan" ]if color else "" ), 328_txt (f" { number ( work . get ( 'occupied_slots' )) } /" 329f" { number ( work . get ( 'slots_effective' )) } SLOTS " 330f" { number ( work . get ( 'groups' )) } BATCH GROUPS " 331f" { number ( model . get ( 'context' )) } CONTEXT" ,palette ["violet" ]if color else "" ), 332_txt ("Layer mapping is from the engine start log when available." ,"dim" )] 333top_right = [_txt (f"ANSWER { number ( rates . get ( 'answer_tok_s' ) if live else None , ' tok/s' , 1 ) } " 334f"PROMPT { number ( rates . get ( 'prefill_tok_s' ) if live else None , ' tok/s' , 1 ) } " , 335palette ["cyan" ]if color else "" ), 336_txt (f"ROWS/WIN { number ( _current_batch_value ( work , 'effective_rows_window' ), '' , 1 ) } " 337f"processed / { number ( _current_batch_value ( work , 'batch_active_rows_window' ), '' , 1 ) } active" , 338palette ["amber" ]if color else "" ), 339_txt ("Host in-flight span is not GPU kernel utilization." ,"dim" )] 340stages :list [Text ]= [] 341replica_samples = {row .get ("replica" ):row for row in work .get ("replica_stage_samples" )or []} 342if topology .get ("replica_stages" ): 343for entry in topology ["replica_stages" ]: 344replica = entry .get ("replica" ) 345sample = replica_samples .get (replica )or {} 346fresh = _current_sample (sample ,"stage_sample_age_s" )and live 347measured = {row .get ("stage" ):row for row in sample .get ("stage_in_flight" )or []}if fresh else {} 348stages .append (_txt (f"R { number ( replica ) } stage host span · " 349f" { number ( sample . get ( 'stage_sample_span_s' ) if fresh else None , 's' , 1 ) } window" , 350palette ["violet" ]if color else "" )) 351for stage in entry .get ("stages" )or []: 352value = (measured .get (stage .get ("stage" ))or {}).get ("percent" ) 353stages .append (_txt (f" S { number ( stage . get ( 'stage' )):<2 } GPU { number ( stage . get ( 'device_index' )):<2 } " 354f"L { number ( stage . get ( 'layer_start' )) } - { number ( stage . get ( 'layer_end' )):<7 } " 355f" { _meter ( value , 100 , 16 , glyphs ) } { number ( value , '%' ) } " , 356palette ["violet" ]if color else "" )) 357elif topology .get ("stages" ): 358fresh = _current_sample (work ,"stage_sample_age_s" )and live 359measured = {row .get ("stage" ):row for row in work .get ("stage_in_flight" )or []}if fresh else {} 360for stage in topology ["stages" ]: 361value = (measured .get (stage .get ("stage" ))or {}).get ("percent" ) 362stages .append (_txt (f"S { number ( stage . get ( 'stage' )):<2 } GPU { number ( stage . get ( 'device_index' )):<2 } " 363f"LAYERS { number ( stage . get ( 'layer_start' )):>3 } -" 364f" { number ( stage . get ( 'layer_end' )):<4 } " 365f" { _meter ( value , 100 , 18 , glyphs ) } { number ( value , '%' ):>4 } " , 366palette ["violet" ]if color else "" )) 367else : 368stages .append (_txt ("Stage/layer mapping unavailable" ,"dim" )) 369stages .append (_txt ("Host poll delay is included; this is not kernel occupancy." ,"dim" )) 370batch = [_txt (f"Engine rows/window " 371f" { number ( _current_batch_value ( work , 'effective_rows_window' ), '' , 1 ) } processed " 372f" { number ( _current_batch_value ( work , 'batch_active_rows_window' ), '' , 1 ) } active" , 373palette ["amber" ]if color else "" ), 374_txt (f"Trend { _rate_spark ( history . batch_rows if live else (), glyphs ) } " ,"dim" ), 375_txt (f"Sample { number ( work . get ( 'batch_sample_windows' )) } windows / " 376f" { number ( work . get ( 'batch_sample_span_s' ), 's' , 1 ) } " 377f"age { number ( work . get ( 'batch_sample_age_s' ), 's' , 1 ) } " ), 378_txt ("" )] 379for row in (work .get ("replica_batches" )or [])[:6 ]: 380current = _current_sample (row ,"batch_sample_age_s" )and live 381batch .append (_txt (f"R { number ( row . get ( 'replica' )) } " 382f" { number ( row . get ( 'effective_rows_window' ) if current else None , '' , 1 ) } " 383f"processed / { number ( row . get ( 'batch_active_rows_window' ) if current else None , '' , 1 ) } " 384f"active age { number ( row . get ( 'batch_sample_age_s' ), 's' , 1 ) } " , 385palette ["amber" ]if color else "" )) 386batch .extend ([_txt ("" ),_txt (f" { number ( work . get ( 'waiting' )) } engine waiting · " 387f" { number ( work . get ( 'queued' )) } before admission" ), 388_txt ("Rows/window counts engine batch work, not requests." ,"dim" )]) 389context = model .get ("context" ) 390contexts = [_txt ("SLOT STATE HELD / CAPACITY REQUEST" ,"dim" )] 391if slots is None : 392contexts .append (_txt ("Complete slot map unavailable" ,"dim" )) 393else : 394for row in slots [:max (1 ,low_h - 5 )]: 395held = row .get ("held_tokens" )if live and row .get ("state" )!= "idle" else 0 if live and row .get ("state" )== "idle" else None 396contexts .append (_txt (f"S { number ( row . get ( 'slot' )):<4 } " 397f" { safe ( row . get ( 'state' ), 10 ):<10 } " 398f" { _meter ( held , context , 14 , glyphs ) } " 399f" { number ( held ):>6 } / { number ( context ):<7 } " 400f" { field ( row . get ( 'request_id' ), 12 ) } " , 401palette ["green" ]if row .get ("state" )== "decoding" and color else 402palette ["amber" ]if row .get ("state" )== "reading" and color else "dim" )) 403note = [_txt ("MEASUREMENT SOURCES" ,palette ["cyan" ]if color else "" ), 404_txt ("Layer ranges engine start log" ), 405_txt ("Stage in-flight host scheduling sample" ), 406_txt ("GPU load device sensor; see Hardware" ), 407_txt ("Batch rows engine window; may be unavailable" ), 408_txt ("Context held tokens in active slot" ), 409_txt ("" ), 410_txt ("Pipeline groups are concurrency partitions." ,"dim" ), 411_txt ("They do not identify GPU assignments." ,"dim" )] 412return Group (_visual_pair (_panel (top_left ,"ENGINE / TOPOLOGY" ,palette ["cyan" ],color ,top_h ), 413_panel (top_right ,"ENGINE / PERFORMANCE" ,palette ["amber" ],color ,top_h ),1 ,1 ), 414_visual_pair (_panel (stages ,"STAGES / HOST SPAN" ,palette ["violet" ],color ,mid_h ), 415_panel (batch ,"BATCH WINDOW" ,palette ["amber" ],color ,mid_h ),1 ,1 ), 416_visual_pair (_panel (contexts ,"SLOT CONTEXTS" ,palette ["green" ],color ,low_h ), 417_panel (note ,"READING THE NUMBERS" ,palette ["cyan" ],color ,low_h ),4 ,3 )) 418 419 420def _hardware (snapshot :dict ,history :MonitorHistory ,* ,heights :tuple [int ,int ,int ], 421selected :int ,detail :bool ,live :bool ,glyphs :str , 422color :bool ,palette :dict [str ,str ],width :int )-> Group : 423devices ,topology = snapshot .get ("devices" )or [],snapshot .get ("topology" )or {} 424ram = snapshot .get ("ram" )or {} 425top_h ,mid_h ,low_h = heights 426count = len (devices ) 427watts = [row .get ("power" )for row in devices ] 428total_watts = sum (watts )if watts and all (isinstance (w , (int ,float ))and not isinstance (w ,bool )for w in watts )else None 429top_left = [_txt (f" { count } GPU DEVICES " 430f" { number ( total_watts if live else None , ' W' , 0 ) } TOTAL BOARD POWER" , 431palette ["green" ]if color else "" ), 432_txt (f" { safe ( topology . get ( 'mode' ), 34 ) } " 433f"selected { number ( len ( topology . get ( 'selected_devices' ) or [])) } " ,"dim" ), 434_txt ("Per-card values come from device sensors." ,"dim" )] 435used ,total = ram .get ("used" )if live else None ,ram .get ("total" )if live else None 436top_right = [_txt (f"HOST RAM { _gib ( used ) } / { _gib ( total ) } GiB " 437f" { _meter ( used , total , 20 , glyphs ) } " , 438palette ["violet" ]if color else "" ), 439_txt (f"GPU load history { _rate_spark ( history . device_util . get ( devices [ 0 ]. get ( 'index' )) if devices and live else (), glyphs ) } " , 440"dim" ), 441_txt ("↑↓ select GPU · Enter placement details" ,"dim" )] 442# Two cards per column; selection scrolls in four-card pages. 443page = min (max (0 ,selected // 4 * 4 ),max (0 ,count - 4 )) 444shown = devices [page :page + 4 ] 445columns :list [list [Text ]]= [[], []] 446for offset ,row in enumerate (shown ): 447column = columns [offset // 2 ] 448column .extend (_device_lines (row ,selected = page + offset == selected , 449topology = topology ,history = history , 450glyphs = glyphs ,live = live ,palette = palette , 451color = color )) 452column .append (_txt ("" )) 453if not devices : 454columns [0 ].append (_txt ("Per-device sensor readings unavailable" ,"dim" )) 455for column in columns : 456column .append (_txt (f"Showing GPU { page + 1 } - { page + len ( shown ) } of { count } " if count else 457"No selected cards reported" ,"dim" )) 458trends :list [Text ]= [_txt ("GPU UTILIZATION TREND VRAM TREND" ,"dim" )] 459for row in devices [:max (1 ,low_h - 5 )]: 460index = row .get ("index" ) 461trends .append (_txt (f"GPU { number ( index ):<3 } " 462f" { _spark ( history . device_util . get ( index ) if live else (), glyphs , 22 ):<23 } " 463f" { _spark ( history . device_vram . get ( index ) if live else (), glyphs , 18 ) } " , 464palette ["green" ]if color else "" )) 465if not devices : 466trends .append (_txt ("History unavailable" ,"dim" )) 467trends .append (_txt ("Gaps in the sparkline mark missing samples." ,"dim" )) 468focus :list [Text ]= [] 469if devices : 470row = devices [min (max (0 ,selected ),len (devices )- 1 )] 471selected_lines = _detail_lines ("device" ,row ,snapshot ,width // 2 ,live ) 472focus .extend (selected_lines if detail else selected_lines [:3 ]) 473else : 474focus .append (_txt ("Select a GPU when sensors become available" ,"dim" )) 475focus .append (_txt ("" ,"" )) 476focus .append (_txt (f"HOST RAM { _gib ( used ) } / { _gib ( total ) } GiB" )) 477focus .append (_txt (f" { _meter ( used , total , 24 , glyphs ) } " ,palette ["violet" ]if color else "" )) 478focus .append (_txt ("Placement follows the resolved engine log when reported." ,"dim" )) 479if not detail : 480focus .append (_txt ("Enter expands selection; Tab changes Serving pane." ,"dim" )) 481return Group (_visual_pair (_panel (top_left ,"HARDWARE / GPU FLEET" ,palette ["green" ],color ,top_h ), 482_panel (top_right ,"HOST MEMORY / NAVIGATION" ,palette ["violet" ],color ,top_h ),1 ,1 ), 483_visual_pair (_panel (columns [0 ],"GPU CARDS / A" ,palette ["green" ],color ,mid_h ), 484_panel (columns [1 ],"GPU CARDS / B" ,palette ["green" ],color ,mid_h ),1 ,1 ), 485_visual_pair (_panel (trends ,"SENSOR HISTORY" ,palette ["cyan" ],color ,low_h ), 486_panel (focus ,"SELECTED DEVICE" ,palette ["violet" ],color ,low_h ),1 ,1 )) 487 488 489def _logs (snapshot :dict ,* ,heights :tuple [int ,int ,int ],filter_text :str , 490color :bool ,palette :dict [str ,str ])-> Group : 491activity = snapshot .get ("activity" )or {} 492events = activity_rows (snapshot ,filter_text ) 493top_h ,mid_h ,low_h = heights 494top_left = [_txt (f" { len ( events ) } VISIBLE EVENTS " 495f"LAST SEQ { number ( activity . get ( 'last_seq' )) } " , 496palette ["cyan" ]if color else "" ), 497_txt ("Recent request activity · redacted IDs and lifecycle" ,"dim" ), 498_txt ("This is a recent telemetry ring, not durable server logs." ,"dim" )] 499notice = activity_continuity_notice (snapshot ) 500top_right = [_txt ("CONTINUITY " + (notice or "No gap reported" ), 501palette ["amber" ]if notice and color else "" ), 502_txt (f"Server source { safe ( snapshot . get ( 'source' ), 34 ) } " ), 503_txt ("/ filters by request ID, event, or outcome" ,"dim" )] 504timeline = [_txt ("SEQ TIME REQUEST ID EVENT / OUTCOME" ,"dim" )] 505if not activity .get ("available" ): 506timeline .append (_txt ("Request activity unavailable on this server" ,"dim" )) 507elif not events : 508timeline .append (_txt ("No transitions retained" if not filter_text else 509"No events match the filter" ,"dim" )) 510else : 511for row in events [- max (1 ,mid_h - 4 ):]: 512event = safe (row .get ("event" ),16 ) 513tint = (palette ["red" ]if event in ("cancelled" ,"error" ,"rejected" )else 514palette ["green" ]if event in ("finished" ,"first_token" )else 515palette ["amber" ]if event in ("queued" ,"reading" )else palette ["cyan" ]) 516timeline .append (_txt (activity_label (row ,95 ),tint if color else "" )) 517if len (events )> mid_h - 4 : 518timeline .append (_txt (f"Newest { mid_h - 4 } of { len ( events ) } events shown" ,"dim" )) 519by_request :dict [str ,list [str ]]= {} 520for row in events : 521ident = safe (row .get ("request_id" ),12 ) 522by_request .setdefault (ident , []).append (safe (row .get ("event" ),12 )) 523flow = [_txt ("REQUEST ID OBSERVED LIFECYCLE" ,"dim" )] 524for ident ,states in list (by_request .items ())[- max (1 ,low_h - 5 ):]: 525flow .append (_txt (f" { ident :<14 } { ' → ' . join ( states [ - 5 :]) } " ,palette ["cyan" ]if color else "" )) 526if not by_request : 527flow .append (_txt ("No request lifecycle available" ,"dim" )) 528flow .append (_txt ("Only events retained in the current ring appear here." ,"dim" )) 529limits = [_txt ("PRIVACY / RETENTION" ,palette ["violet" ]if color else "" ), 530_txt ("Request IDs are bounded telemetry identifiers." ), 531_txt ("Prompt bodies, output text and tool content are absent." ), 532_txt ("The ring may roll over between polls." ), 533_txt ("A sequence gap means events were missed." ), 534_txt ("" ), 535_txt (f"Newest sequence { number ( activity . get ( 'last_seq' )) } " ), 536_txt (f"Visible after filter { len ( events ) } " ), 537_txt ("Use strata logs for a one-shot redacted snapshot." ,"dim" )] 538return Group (_visual_pair (_panel (top_left ,"LOGS / REQUEST ACTIVITY" ,palette ["cyan" ],color ,top_h ), 539_panel (top_right ,"CONTINUITY / SOURCE" ,palette ["amber" ],color ,top_h ),1 ,1 ), 540_panel (timeline ,"RECENT EVENT TIMELINE" ,palette ["green" ],color ,mid_h ), 541_visual_pair (_panel (flow ,"REQUEST LIFECYCLES" ,palette ["cyan" ],color ,low_h ), 542_panel (limits ,"WHAT THIS VIEW CONTAINS" ,palette ["violet" ],color ,low_h ),1 ,1 )) 543 544 545def render_detail_dashboard (snapshot :dict ,view :str ,* ,width :int ,height :int , 546history :MonitorHistory ,connection :str = "" , 547connection_state :str = "connected" ,frozen :bool = False , 548filter_text :str = "" ,glyphs :str = "ascii" , 549color :bool = True ,theme :str = "dark" ,selected :int = 0 , 550detail :bool = False ,serving_pane :str = "devices" ,connect :bool = False )-> Group : 551"""Render the five wide pages with the visual Overview's panel language.""" 552set_ascii_boxes (glyphs == "ascii" ) 553palette = _palette (color ,theme ) 554state ,live = _state (snapshot ,connection_state ,frozen ) 555active = palette ["green" ]if state == "LIVE" else palette ["amber" ]if "STALE" in state else palette ["red" ] 556heading = Text () 557heading .append (f"STRATA / { view . upper () } " ,style = f"bold { palette [ 'cyan' ] } " ) 558heading .append (" " ) 559heading .append (state ,style = f"bold { active } " if color else "bold" ) 560heading .append (f" { safe ( connection , max ( 0 , width - 48 )) } " ) 561header = Panel (heading ,box = _rounded (),border_style = active if color else "" , 562padding = (0 ,1 )) 563age = _age (snapshot ) 564hints = f"Tab pane ↑↓ select Enter detail / filter age { number ( age , 's' , 1 ) } " 565footer = nav .styled (VIEWS .index (view ),max (24 ,width - len (hints )- 5 ),connect ,accent = palette ["cyan" ]if color else "" ,color = color ) 566footer .append (" " ) 567footer .append (hints ,style = "dim" ) 568top_h = 6 if height >= 40 else 5 569room = max (0 ,height - 4 - top_h ) 570mid_h = room // 2 + (3 if view in ("serving" ,"requests" ,"logs" )else 1 ) 571low_h = room - mid_h 572shared = dict (heights = (top_h ,mid_h ,low_h ),live = live ,glyphs = glyphs , 573color = color ,palette = palette ) 574if view == "serving" : 575body = _serving (snapshot ,history ,selected = selected ,detail = detail , 576pane = serving_pane ,filter_text = filter_text ,width = width ,** shared ) 577elif view == "requests" : 578body = _requests (snapshot ,selected = selected ,detail = detail , 579filter_text = filter_text ,width = width ,** {k :v for k ,v in shared .items ()if k != "heights" }, 580heights = shared ["heights" ]) 581elif view == "engine" : 582body = _engine (snapshot ,history ,** shared ) 583elif view == "hardware" : 584body = _hardware (snapshot ,history ,selected = selected ,detail = detail , 585width = width ,** shared ) 586elif view == "logs" : 587body = _logs (snapshot ,heights = shared ["heights" ],filter_text = filter_text , 588color = color ,palette = palette ) 589else : 590raise ValueError ("visual detail view must be serving, requests, engine, hardware or logs" ) 591return Group (header ,body ,footer )