Skip to content

fix(sessions): preserve concurrent writes during compaction - #4736

Open
seratch wants to merge 1 commit into
mainfrom
fix/preserve-concurrent-compaction-writes
Open

fix(sessions): preserve concurrent writes during compaction#4736
seratch wants to merge 1 commit into
mainfrom
fix/preserve-concurrent-compaction-writes

Conversation

@seratch

@seratch seratch commented Aug 28, 2026

Copy link
Copy Markdown
Member

This pull request fixes #4679 by serializing OpenAIResponsesCompactionSession mutations across the full responses.compact request and replacement window.

Before this change, run_compaction released its mutation lock while awaiting responses.compact, so a same-wrapper add_items could be overwritten by the later clear-and-replace, and a concurrent clear_session could be undone. The compaction lock now remains held from the history snapshot through replacement, so queued mutations run afterward and determine the final Session state.

The regression tests use deterministic event ordering to verify that a queued append survives after compacted output and that a queued clear leaves the Session empty. This change intentionally does not add backend-wide coordination for separate compaction wrapper instances because the released Session protocol does not expose a shared revision or compare-and-swap boundary.

This pull request resolves #4679.

@seratch seratch added this to the 0.22.x milestone Aug 28, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 28, 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-08-28T09:49:33.208936Z 03583c3 New commits
🔒 Security Review Completed 2026-08-28T09:49:59.338816Z 03583c3 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: a02203e033

ℹ️ 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_responses_compaction_session.py Outdated
@seratch
seratch force-pushed the fix/preserve-concurrent-compaction-writes branch from a02203e to 22a822d Compare August 28, 2026 08:23

@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: 22a822dc98

ℹ️ 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_responses_compaction_session.py Outdated
@seratch
seratch force-pushed the fix/preserve-concurrent-compaction-writes branch from 22a822d to 84f047a Compare August 28, 2026 09:11

@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: 84f047a28d

ℹ️ 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_responses_compaction_session.py Outdated
Co-authored-by: Om Singhal <wengsinghal@gmail.com>
@seratch
seratch force-pushed the fix/preserve-concurrent-compaction-writes branch from 84f047a to 03583c3 Compare August 28, 2026 09:47

@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: 03583c342e

ℹ️ 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 on lines +184 to +185
async with self._mutation_lock:
await self._run_compaction_locked(args, wrapper=wrapper)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Revalidate history after acquiring the compaction lock

When run A releases this lock after add_items(), an already-waiting run B can acquire it and append its batch before A reacquires it here. In the default previous_response_id path, A then compacts only resp_A while the local history already includes B and replaces that history with A's compacted output, silently deleting B's committed items; the new tests only cover the opposite ordering where compaction acquires the lock first. Preserve ownership across the append-to-compaction boundary or reject replacement when the generation changed.

AGENTS.md reference: AGENTS.md:L149-L149

Useful? React with 👍 / 👎.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAIResponsesCompactionSession drops session items written while responses.compact is in flight

1 participant