Build the unhashed turn data (transcript + reasoning) from the agent's transcript file and the recorded change's FileOps. Returns `None` if the transcript is not available, empty, or unparseable. Reasoning generation failure is logged and skipped (non-fatal).
(
options: &TurnRecordOptions<'_>,
recorded_files: &[String],
)
| 263 | /// Returns `None` if the transcript is not available, empty, or unparseable. |
| 264 | /// Reasoning generation failure is logged and skipped (non-fatal). |
| 265 | pub(crate) fn build_unhashed_turn_data( |
| 266 | options: &TurnRecordOptions<'_>, |
| 267 | recorded_files: &[String], |
| 268 | ) -> Option<transcript::UnhashedTurnData> { |
| 269 | // Check if we have a transcript file |
| 270 | let transcript_path = options.session.transcript_path.as_ref()?; |
| 271 | if !transcript_path.exists() { |
| 272 | log::debug!( |
| 273 | "Transcript file not found at {} — skipping", |
| 274 | transcript_path.display(), |
| 275 | ); |
| 276 | return None; |
| 277 | } |
| 278 | |
| 279 | // Read raw transcript |
| 280 | let raw = match std::fs::read(transcript_path) { |
| 281 | Ok(data) => data, |
| 282 | Err(e) => { |
| 283 | log::warn!( |
| 284 | "Failed to read transcript at {}: {}", |
| 285 | transcript_path.display(), |
| 286 | e |
| 287 | ); |
| 288 | return None; |
| 289 | } |
| 290 | }; |
| 291 | |
| 292 | if raw.is_empty() { |
| 293 | return None; |
| 294 | } |
| 295 | |
| 296 | // Determine format from agent name |
| 297 | let format = transcript::format_for_agent(&options.session.agent_name); |
| 298 | |
| 299 | // Condense into structured entries |
| 300 | let entries = transcript::condense_transcript(&raw, format); |
| 301 | if entries.is_empty() { |
| 302 | log::debug!("Condensed transcript is empty — skipping"); |
| 303 | return None; |
| 304 | } |
| 305 | |
| 306 | // Build the base unhashed data |
| 307 | let mut data = transcript::UnhashedTurnData::new( |
| 308 | &options.session.session_id, |
| 309 | options.turn_number, |
| 310 | format, |
| 311 | entries, |
| 312 | recorded_files, |
| 313 | ); |
| 314 | |
| 315 | // The turn's LLM usage rides the hook event's raw JSON (the sherpa |
| 316 | // turn-end handler inserts it alongside turn_number). The cost of a |
| 317 | // turn belongs to its record as much as what it did. |
| 318 | if let Some(raw) = options.event.raw_json.as_ref() { |
| 319 | let get_u64 = |key: &str| raw.get(key).and_then(|v| v.as_u64()); |
| 320 | if let Some(input) = get_u64("input_tokens") { |
| 321 | data.input_tokens = Some(input); |
| 322 | } |
no test coverage detected