Skip to content

fix: batch OpenAI conversation session writes within the API limit - #5334

Merged
jbeckwith-oai merged 9 commits into
mainfrom
codex/fix-conversations-session-batching
Oct 8, 2026
Merged

jbeckwith-oai merged 9 commits into
mainfrom
codex/fix-conversations-session-batching

Conversation

@jbeckwith-oai

@jbeckwith-oai jbeckwith-oai commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This pull request fixes oversized conversation-session writes. OpenAIConversationsSession.add_items() sent the full input list in one request, exceeding the Conversations API's 20-item limit for larger writes. Send sequential batches of at most 20 items while preserving input order and the existing public API. A per-instance mutation lock keeps concurrent reads, appends, pops, and clears from interleaving with a batched append.

For multi-request appends, cancellation waits for the current request to complete or fail while retaining the instance lock, then raises the original cancellation without sending later chunks. A checkpoint between chunks delivers queued cancellation before scheduling the next mutation. Single-request cancellation behavior is unchanged. Multi-request appends also disable automatic client retries for each chunk, including configured retries, so a lost response raises an error instead of silently duplicating a saved chunk. The shared client’s settings and single-request retry behavior are unchanged.

Multi-request writes are non-atomic: failure or cancellation stops later batches and leaves earlier batches saved. The method docstring explains that callers must reconcile remote history before retrying; no automatic rollback or whole-list retry is added. Release-timed guidance in docs/sessions/index.md is a separate documentation follow-up.

Runner resume also rejects ambiguous partial writes when repeated history could appear unchanged or fully committed. The existing repair-required error prevents duplicate appends or silently discarded pending items; provider ID matching and serialized RunState formats are unchanged. The new check is limited to multi-request Conversations writes; atomic backends and single-request recovery retain their existing behavior. When the prior snapshot contains all history, its item count distinguishes unchanged, partial, and full progress.

Test plan

  • Real OpenAI client with httpx.MockTransport: 0, 20, 21, and 41 items; request ordering; partial failure; controlled cancellation; first/later lost-response chunks with default/custom retry settings and subsequent single-request retry compatibility; streaming and non-streaming Runner integration.
  • Focused suites: 272 passed, including controlled read/append/pop/clear ordering and public approval/handoff resume regressions with short and long repeated history, JSON restoration, partial failure, and unambiguous recovery.
  • Two independent reviews completed with no findings.
  • Full verification: formatting, lint, mypy, Pyright, and 12,209 tests passed; 69 skipped. Native macOS sandbox tests are skipped locally under the repository's Codex sandbox policy.

Issue number

Closes #5333

Checks

  • I've added new tests, if relevant
  • I've run .agents/skills/code-change-verification/scripts/run.sh
  • I've confirmed all verification steps pass
  • If using Codex, I've run /review before submitting this PR

@jbeckwith-oai
jbeckwith-oai requested review from a team, rm-openai and seratch as code owners October 7, 2026 17:07
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-07T20:08:27.571010Z d166df7 New commits
🔒 Security Review ✅ Completed 2026-10-07T20:11:34.747103Z d166df7 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5e5a30566b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agents/memory/openai_conversations_session.py Outdated
Comment thread src/agents/memory/openai_conversations_session.py Outdated
Comment thread tests/memory/test_openai_conversations_session.py Fixed
Comment thread tests/memory/test_openai_conversations_session.py Fixed
Comment thread tests/memory/test_openai_conversations_session.py Fixed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4b3a79b717

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agents/memory/openai_conversations_session.py
Comment thread tests/memory/test_openai_conversations_session.py

@markstuart-oai markstuart-oai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One P2 finding: a partial batch can be replayed by the built-in Runner resume path when its saved suffix matches the previous history. The batching and lock ordering otherwise look sound. Reviewed the pinned source and hosted CI; I did not run repository tests locally.

Comment thread src/agents/memory/openai_conversations_session.py Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ea1da4f367

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agents/run_internal/session_persistence.py Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a674687b44

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agents/memory/openai_conversations_session.py Outdated
Comment thread src/agents/memory/openai_conversations_session.py

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 972d573d3e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agents/memory/openai_conversations_session.py Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8ecddf7a3e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agents/memory/openai_conversations_session.py
@jbeckwith-oai
jbeckwith-oai added this pull request to the merge queue Oct 8, 2026
Merged via the queue into main with commit 26345c1 Oct 8, 2026
27 checks passed
@jbeckwith-oai
jbeckwith-oai deleted the codex/fix-conversations-session-batching branch October 8, 2026 02:02
@Tivonsico

Copy link
Copy Markdown

Thanks to everyone involved for fixing this and carefully handling concurrency, cancellation, and partial-write recovery. The implementation and review discussion helped me understand these failure cases much better. Really appreciate the work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAIConversationsSession sends writes above the documented 20-item request limit

4 participants