Skip to slide
016 / 221

CH 02 · The Agent Loop Pattern · 04/15

Read as one page

Real code: Codex never sends a call without its output

Codex, OpenAI's open-source coding agent written in Rust, applies the "synthesise a result" rule right before every request to the model: the function below finds tool calls with no output and adds one. Watch where it puts each fix.

pub(crate) fn ensure_call_outputs_present(items: &mut Vec<ResponseItemEnvelope>) {  // [1]
    let mut function_output_ids = HashSet::new();
    // ... (22 lines: record the call_id of every output already in the history)

    // Collect synthetic outputs to insert immediately after their calls.
    // Store the insertion position (index of call) alongside the item so
    // we can insert in reverse order and avoid index shifting.
    let mut missing_outputs_to_insert: Vec<(usize, ResponseItemEnvelope)> = Vec::new();

    for (idx, envelope) in items.iter().enumerate() {
        match &envelope.item {
            ResponseItem::FunctionCall { id, call_id, .. }
                if !function_output_ids.contains(call_id.as_str()) =>  // [2]
            {
                info!("Function call output is missing for call id: {call_id}");
                missing_outputs_to_insert.push((
                    idx,
                    ResponseItemEnvelope::new(ResponseItem::FunctionCallOutput {
                        id: synthetic_output_id("fco", id.as_deref()),
                        call_id: Some(call_id.clone()),
                        name: None,
                        namespace: None,
                        output: FunctionCallOutputPayload::from_text("aborted".to_string()),  // [3]
                        internal_chat_message_metadata_passthrough: None,
                    }),
                ));
            }
            // ... (81 lines: tool-search inserts an empty completed output; custom-tool and local-shell insert "aborted", unless a debug build panics first because the call is not marked inherited)
    // Insert synthetic outputs in reverse index order to avoid re-indexing.
    for (idx, output_item) in missing_outputs_to_insert.into_iter().rev() {
        items.insert(idx + 1, output_item);  // [4]
    }
}

Source: openai/codex, codex-rs/core/src/context_manager/normalize.rs (Apache-2.0)

What to notice

  1. [1] items is the conversation history: each ResponseItemEnvelope wraps one ResponseItem (a message, a tool call, a tool output) plus metadata, and &mut lets the function edit the list in place. The skipped lines collect the call_id of every existing output into sets; a call_id is the id that ties a call to its output.
  2. [2] A second pass visits every call. A FunctionCall whose call_id is not in the set has no output.
  3. [3] The made-up output is the text "aborted", and success is left unset (None), so the next request shows that the call produced no normal result. synthetic_output_id derives its id from the call's own id, so the same history always produces the same request and the provider's prompt cache (its saved work on a prompt it has seen) stays valid.
  4. [4] Each fix goes in at idx + 1, straight after its call, keeping the "I asked for X, X returned Y" order. Every insert shifts later positions by one, so the loop works from the back of the list forward.

This is a net under a net. Cancelling a function or custom tool already records an output whose text contains "aborted by user" (core/src/tools/parallel.rs); a cancelled tool-search call records an empty completed output instead. This function catches what still slips through, such as a conversation forked while a call was running. It runs inside for_prompt on a copy of the history, so only the outgoing request is patched. Without it, one missing output would stay in the history and break every later request.

Read it yourself: in history.rs, read the comment on normalize_history. Its second rule is the mirror problem, an output whose call is gone: find remove_orphan_outputs in normalize.rs to see the fix, then read the test normalize_mixed_inserts_and_removals in history_tests.rs. That test is compiled only without debug assertions; a debug build has normalize_mixed_inserts_and_removals_panics_in_debug instead. Question: for a custom-tool call that is not marked inherited_user_message, the branch calls error_or_panic instead of info!. Local-shell uses the same guard. What does error_or_panic (in core/src/util.rs) do in a debug build?

Finished this idea?

Your learning progress stays in this browser.