use super::legacy::{extract_cwd_from_env_context, normalize_line};
use super::records::CodexSessionMetaPayload;
use anyhow::{Context, Result};
use serde_json::Value;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
pub(crate) fn read_session_meta(path: &Path) -> Result<Option<CodexSessionMetaPayload>> {
let file = File::open(path)
.with_context(|| format!("Failed to open Codex session {}", path.display()))?;
let mut meta: Option<CodexSessionMetaPayload> = None;
let mut saw_first = false;
for line in BufReader::new(file).lines() {
let line = line.with_context(|| format!("Failed to read {}", path.display()))?;
if line.trim().is_empty() {
continue;
}
if !saw_first {
saw_first = true;
match normalize_line(&line, None)
.with_context(|| format!("Failed to parse {}", path.display()))?
{
Some(record) if record.kind == "session_meta" => {
let payload: CodexSessionMetaPayload = serde_json::from_value(record.payload)
.with_context(|| {
format!("Invalid session_meta in {}", path.display())
})?;
if !payload.cwd.is_empty() {
return Ok(Some(payload));
}
meta = Some(payload);
continue;
}
_ => return Ok(None),
}
}
if meta.is_some() {
let value: Value = match serde_json::from_str(&line) {
Ok(v) => v,
Err(_) => continue, };
if let Some(cwd) = extract_cwd_from_env_context(&value) {
let mut payload = meta.take().expect("meta is Some");
payload.cwd = cwd;
return Ok(Some(payload));
}
}
}
Ok(meta)
}