nikivdev/code · error

No exchanges found in session

Error message

No exchanges found in session

What it means

read_codex_last_context reads a Codex session file and, if the parsed exchange list is empty (no user/assistant exchanges recorded), bails with 'No exchanges found in session'. The library requires at least one exchange to build the last-N context string.

Source

Thrown at src/ai.rs:1934

        context.push_str("\n\n");
        context.push_str("A: ");
        context.push_str(&truncate_message(assistant_msg, MAX_MSG_CHARS));
        context.push_str("\n\n");
    }

    while context.ends_with('\n') {
        context.pop();
    }
    context.push('\n');

    Ok((context, last_ts))
}

fn read_codex_last_context(session_file: &PathBuf, count: usize) -> Result<String> {
    let (exchanges, _last_ts) = read_codex_exchanges(session_file, None, None)?;

    if exchanges.is_empty() {
        bail!("No exchanges found in session");
    }

    let start = exchanges.len().saturating_sub(count);
    let last_exchanges = &exchanges[start..];

    let mut context = String::new();
    for (user_msg, assistant_msg, _ts) in last_exchanges {
        context.push_str("Human: ");
        context.push_str(user_msg);
        context.push_str("\n\n");
        context.push_str("Assistant: ");
        context.push_str(assistant_msg);
        context.push_str("\n\n");
    }

    while context.ends_with('\n') {
        context.pop();
    }

View on GitHub (pinned to a747e741ae)

Solutions

  1. Send at least one message in the session before requesting its last context.
  2. Check the session .jsonl file contains valid Codex message entries.
  3. Point the command at the correct session file (a different, active session).

Example fix

// before
f codex context --session fresh-session  // no messages yet
// after
f codex "say hi" && f codex context --session fresh-session
Defensive patterns

Strategy: validation

Validate before calling

// rust
let (exchanges, _) = read_codex_exchanges(file, None, None)?;
if exchanges.is_empty() { eprintln!("session has no exchanges yet"); return; }

Type guard

fn has_exchanges(file: &PathBuf) -> bool {
    read_codex_exchanges(file, None, None).map(|(e, _)| !e.is_empty()).unwrap_or(false)
}

Try / catch

// rust
match read_codex_last_context(&file, n) {
    Err(e) if e.to_string() == "No exchanges found in session" => {
        eprintln!("Send a message first; session is empty.");
    }
    Err(e) => return Err(e),
    Ok(ctx) => ctx,
}

Prevention

When it happens

Trigger: Calling read_codex_last_context on a Codex session whose JSONL file parses to zero exchanges — e.g. an empty/new session, a file containing only metadata lines, or count requested from an empty history.

Common situations: Requesting last context immediately after starting a session with no messages; a corrupted or truncated session file where message lines failed to parse.

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/87e9dd4f039d9fd4. Report an issue: GitHub.