Skip to content

fix(extensions): skip AdvancedSQLiteSession usage store when the branch has no turn - #4822

Open
rioyu123 wants to merge 1 commit into
openai:mainfrom
rioyu123:fix/advanced-sqlite-usage-no-turn
Open

fix(extensions): skip AdvancedSQLiteSession usage store when the branch has no turn#4822
rioyu123 wants to merge 1 commit into
openai:mainfrom
rioyu123:fix/advanced-sqlite-usage-no-turn

Conversation

@rioyu123

@rioyu123 rioyu123 commented Sep 2, 2026

Copy link
Copy Markdown

Summary

store_run_usage() reads the current turn with _capture_current_turn(), which returns turn 0 and a None anchor when the current branch has no turn rows. _update_turn_usage_internal only ran the removed-turn guard when an anchor was present, so a None anchor bypassed it and INSERT OR REPLACE recorded a phantom usage row for turn 0. On an empty session get_session_usage() reported total_turns: 1 while get_items() was [].

This treats a None anchor as "no turn to attribute usage to" and skips the write with a debug log. The guard lives inside _update_turn_usage_internal, so store_run_usage's error handling (and the existing test that patches that method on an empty session) is unchanged; the turn_anchor docstring documents the contract.

Implementation scope. Required behavior: usage is only recorded against an existing turn on the current branch; an empty branch leaves get_session_usage() as None and get_turn_usage() as []. Compatibility: the persisted schema is unchanged, existing rows are untouched, the single caller already passes the anchor. Intentionally unsupported: attributing run usage to a session that has not stored a user turn yet (the usage is dropped, logged at debug level). Existing alternative: call store_run_usage() after Runner.run has persisted the turn, which is the documented flow.

Test plan

  • Added test_store_run_usage_skips_when_current_branch_has_no_turn in tests/extensions/memory/test_advanced_sqlite_session.py: empty session, a branch emptied by pop_item, then a real turn (usage requests == 2, total_turns == 1, turn numbers [1]). Fails on main (assert {... 'requests': 1, ...} is None), passes with this change.
  • Ran the Makefile commands directly: uv run ruff format, uv run ruff check, the optional-truthiness script, uv run mypy src, uv run pyright --project pyrightconfig.json, and the make tests equivalent: 8323 passed, 81 skipped, 77 serial passed; the 19 failures are the same pre-existing Windows-only symlink and daytona-example tests as on an untouched main checkout.

Issue number

Closes #4821

Checks

  • I've added new tests, if relevant
  • I've run .agents/skills/code-change-verification/scripts/run.sh (no make on this Windows host; I ran the equivalent Makefile commands one by one, see the test plan)
  • I've confirmed all verification steps pass (except 19 pre-existing Windows-only failures that are identical on an untouched main checkout)
  • If using Codex, I've run /review before submitting this PR

…ch has no turn

store_run_usage() reads the current turn with _capture_current_turn(),
which returns turn 0 and a None anchor when the current branch has no
turn rows. _update_turn_usage_internal only ran the removed-turn guard
when an anchor was present, so a None anchor bypassed it and recorded a
phantom usage row for turn 0 on an empty session: get_session_usage()
reported total_turns=1 while get_items() was empty.

Treat a None anchor as "no turn to attribute usage to" and skip the
write. The guard stays inside _update_turn_usage_internal so the
failure handling in store_run_usage is unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 2, 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-02T06:08:04.066439Z 103f91b PR opened
🔒 Security Review Completed 2026-09-02T06:09:37.205204Z 103f91b PR opened
ℹ️ 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.

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