Skip to content

fix(memory): skip usage for empty sessions - #4843

Open
Hughhhhcoder wants to merge 1 commit into
openai:mainfrom
Hughhhhcoder:codex/openai-agents-phantom-usage
Open

fix(memory): skip usage for empty sessions#4843
Hughhhhcoder wants to merge 1 commit into
openai:mainfrom
Hughhhhcoder:codex/openai-agents-phantom-usage

Conversation

@Hughhhhcoder

Copy link
Copy Markdown
Contributor

Fixes #4821

Bug

AdvancedSQLiteSession.store_run_usage() could insert a turn_usage row for user turn 0 when the current branch had no conversation turn. This made an empty session report usage and total_turns: 1 even though it contained no items. The same could happen after all turns on a branch were popped.

Root cause

_capture_current_turn() correctly returned turn_anchor=None when no message_structure row existed, but store_run_usage() still passed current_turn=0 to the write path. The write path only guarded anchored stale writes, so it inserted the phantom row.

Fix

Return without writing when the captured turn has no anchor. This preserves the existing stale-write protection and prevents usage from being attributed to a nonexistent turn.

Validation

  • PYTHONPATH=src .../python -m pytest -q tests/extensions/memory/test_advanced_sqlite_session.py (130 passed)
  • ruff check src/agents/extensions/memory/advanced_sqlite_session.py tests/extensions/memory/test_advanced_sqlite_session.py
  • ruff format --check src/agents/extensions/memory/advanced_sqlite_session.py tests/extensions/memory/test_advanced_sqlite_session.py
  • PYTHONPATH=src .../python -m mypy src/agents/extensions/memory/advanced_sqlite_session.py

The regression tests cover both an initially empty session and a branch whose only turn has been popped.

@Hughhhhcoder

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 3, 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-09-03T10:45:11.739502Z a1baa2a Manual request
🔒 Security Review Completed 2026-09-03T10:45:36.513287Z a1baa2a Manual request
ℹ️ 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: a1baa2aa97

ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

Security review completed. No security issues were found in this pull request.

Reviewed commit: a1baa2aa97

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

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.

AdvancedSQLiteSession.store_run_usage() records a phantom usage row for turn 0 when the current branch has no turn

1 participant