@@ -239,6 +239,134 @@ fn model_href(run: &EvalRun, model: &str) -> String {
239239 format ! ( "run-{}-{}.html" , run. name, model:: sanitise_name( model) )
240240}
241241
242+ /// One round's session trace lives on its own page: a long round runs to
243+ /// hundreds of events, which would bury the ratings on the model page.
244+ fn trace_href ( run : & EvalRun , model : & str , round : u32 ) -> String {
245+ format ! (
246+ "trace-{}-{}-r{round}.html" ,
247+ run. name,
248+ model:: sanitise_name( model)
249+ )
250+ }
251+
252+ fn fmt_elapsed ( ms : i64 ) -> String {
253+ let secs = ms. max ( 0 ) / 1000 ;
254+ format ! ( "{}:{:02}" , secs / 60 , secs % 60 )
255+ }
256+
257+ fn fmt_json_compact ( value : & serde_json:: Value , limit : usize ) -> Option < String > {
258+ let text = match value {
259+ serde_json:: Value :: Null => return None ,
260+ serde_json:: Value :: String ( s) => s. clone ( ) ,
261+ other => other. to_string ( ) ,
262+ } ;
263+ let text = text. trim ( ) ;
264+ if text. is_empty ( ) || text == "{}" {
265+ return None ;
266+ }
267+ Some ( clamp ( text, limit) )
268+ }
269+
270+ /// Long tool payloads are truncated: a `valid_next_pieces` result runs to
271+ /// kilobytes and the interesting part is always at the front.
272+ fn clamp ( text : & str , limit : usize ) -> String {
273+ if text. chars ( ) . count ( ) <= limit {
274+ return text. to_string ( ) ;
275+ }
276+ let head: String = text. chars ( ) . take ( limit) . collect ( ) ;
277+ format ! ( "{head}… ({} chars)" , text. chars( ) . count( ) )
278+ }
279+
280+ fn build_trace_page (
281+ run : & EvalRun ,
282+ model : & ModelRun ,
283+ round : & Round ,
284+ base_url : Option < & str > ,
285+ out : & Path ,
286+ ) -> Result < ( ) > {
287+ let tools = round. trace . iter ( ) . filter ( |e| e. is_tool ( ) ) . count ( ) ;
288+ let rejected = round. trace . iter ( ) . filter ( |e| e. is_rejection ( ) ) . count ( ) ;
289+ let elapsed = round. trace . iter ( ) . filter_map ( |e| e. ms ) . max ( ) . unwrap_or ( 0 ) ;
290+ let cost: f64 = round. trace . iter ( ) . filter_map ( |e| e. cost_usd ) . sum ( ) ;
291+
292+ let mut summary = vec ! [
293+ view:: TraceSummary {
294+ label: "events" . into( ) ,
295+ value: round. trace. len( ) . to_string( ) ,
296+ class: String :: new( ) ,
297+ } ,
298+ view:: TraceSummary {
299+ label: "tool calls" . into( ) ,
300+ value: tools. to_string( ) ,
301+ class: String :: new( ) ,
302+ } ,
303+ view:: TraceSummary {
304+ label: "rejected" . into( ) ,
305+ value: rejected. to_string( ) ,
306+ class: if rejected > 0 { "fail" } else { "dim" } . into( ) ,
307+ } ,
308+ view:: TraceSummary {
309+ label: "session" . into( ) ,
310+ value: fmt_elapsed( elapsed) ,
311+ class: String :: new( ) ,
312+ } ,
313+ ] ;
314+ if cost > 0.0 {
315+ summary. push ( view:: TraceSummary {
316+ label : "cost" . into ( ) ,
317+ value : format ! ( "${cost:.2}" ) ,
318+ class : "usage" . into ( ) ,
319+ } ) ;
320+ }
321+
322+ let rows = round
323+ . trace
324+ . iter ( )
325+ . map ( |event| view:: TraceRow {
326+ kind : event. kind . clone ( ) ,
327+ at : event. ms . map ( fmt_elapsed) . unwrap_or_default ( ) ,
328+ text : event. text . as_ref ( ) . map ( |t| clamp ( t, 4000 ) ) ,
329+ tool : event. name . clone ( ) ,
330+ input : event
331+ . input
332+ . as_ref ( )
333+ . and_then ( |v| fmt_json_compact ( v, 1200 ) ) ,
334+ output : event
335+ . output
336+ . as_ref ( )
337+ . and_then ( |v| fmt_json_compact ( v, 1200 ) ) ,
338+ failed : event. is_rejection ( ) ,
339+ duration : event. dur_ms . map ( |d| format ! ( "{d} ms" ) ) ,
340+ cost : event
341+ . cost_usd
342+ . filter ( |c| * c > 0.0 )
343+ . map ( |c| format ! ( "${c:.4}" ) ) ,
344+ } )
345+ . collect ( ) ;
346+
347+ let path = trace_href ( run, & model. model , round. number ) ;
348+ let page = view:: TracePage {
349+ chrome : Chrome :: new (
350+ & format ! (
351+ "Coaster Evals — {} · {} · round {}" ,
352+ run. name, model. model, round. number
353+ ) ,
354+ & format ! ( "TRACE · ROUND {}" , round. number) ,
355+ & path,
356+ base_url,
357+ )
358+ . width ( view:: Width :: Mid ) ,
359+ run_name : run. name . clone ( ) ,
360+ run_href : format ! ( "run-{}.html" , run. name) ,
361+ model : model. model . clone ( ) ,
362+ model_href : model_href ( run, & model. model ) ,
363+ round : round. number ,
364+ summary,
365+ rows,
366+ } ;
367+ write_page ( & out. join ( & path) , & page. render ( ) ?)
368+ }
369+
242370/// Builds one model's view (chart, studied designs, every round). Copies the
243371/// round captures into the site as a side effect, so it runs once per model
244372/// and both the run comparison and the model detail page render from it.
@@ -259,6 +387,10 @@ fn build_model_view(
259387 let stats = round_stats ( round) ;
260388 rounds. push ( RoundView {
261389 number : round. number ,
390+ trace_href : ( !round. trace . is_empty ( ) )
391+ . then ( || trace_href ( run, & model. model , round. number ) ) ,
392+ trace_events : round. trace . len ( ) ,
393+ trace_rejections : round. trace . iter ( ) . filter ( |e| e. is_rejection ( ) ) . count ( ) ,
262394 badge : round_badge ( round, stats. as_ref ( ) ) ,
263395 build_error : round. build_error . clone ( ) ,
264396 unrated_note : stats. is_none ( ) && round. build_error . is_none ( ) ,
@@ -377,6 +509,11 @@ fn build_run_page(
377509 for ( i, view) in models. iter ( ) . enumerate ( ) {
378510 build_model_page ( run, view, i + 1 , base_url, out) ?;
379511 }
512+ for model in & run. models {
513+ for round in model. rounds . iter ( ) . filter ( |r| !r. trace . is_empty ( ) ) {
514+ build_trace_page ( run, model, round, base_url, out) ?;
515+ }
516+ }
380517
381518 let path = format ! ( "run-{}.html" , run. name) ;
382519 let page = RunPage {
0 commit comments