Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Feat: Add AdvancedSQLiteSession with conversation branching & usage tracking#1662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
seratch merged 21 commits intoopenai:mainfromhabema:session-schema-improvements
Sep 24, 2025

Conversation

habema
Copy link
Contributor

@habemahabema commentedSep 4, 2025
edited
Loading

Resolves#1385

This PR introducesAdvancedSQLiteSession as a drop-in replacement forSQLiteSession with enhanced conversation management capabilities. Addresses#1385 and builds on the foundation from#1474.

Key Features

  • Structured queries: Get conversations by turns, tool usage analytics, etc.
  • Usage tracking: Turn-level token usage with detailed breakdowns
  • Conversation branching: Soft deletion allows "undoing" parts of conversations and continuing from any point

New Table Schema

-- Message structure with soft deletionmessage_structure (    id, session_id, message_id, message_type, sequence_number,    user_turn_number, tool_name, is_active, created_at, deactivated_at)-- Turn-level usage trackingturn_usage (    id, session_id, user_turn_number, requests, input_tokens,     output_tokens, total_tokens, input_tokens_details, output_tokens_details)

Usage Pattern

Currently requires manual usage storage:

result=awaitRunner.run(agent,"Hello",session=session)awaitsession.store_run_usage(result)# Manual step

Question: Should usage storage be automatic?

Currently users need to manually callstore_run_usage() after each run to get the analytics features (turn-level token tracking, usage breakdowns, etc.). It works fine but could be better UX-wise.

However, since this is an extension feature, it shouldn't require changes to the core SDK (run.py). Making it automatic could be quite a complex change - as it could need to patch the runner itself, and potentially break the clean separation between core and extensions.

What's the preference here - keep the explicit manual approach or explore automatic storage despite the complexity?

Testing Notes

Tests skip inold_versions_test due to sqlalchemy dependency in the same directory (same issue as#1357). The advanced session itself doesn't need sqlalchemy, but the import path requires it.

@habemahabema changed the titleFeat: Add StructuredSQLiteSession with conversation branching & usage trackingFeat: Add AdvancedSQLiteSession with conversation branching & usage trackingSep 4, 2025
@seratchseratch added enhancementNew feature or request feature:sessions labelsSep 5, 2025
@seratch
Copy link
Member

@codex Can you review this PR? Share both good things and rooms for improvements.

Copy link

@chatgpt-codex-connectorchatgpt-codex-connectorbot left a comment

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 suggestions.

Reply with@codex fix comments to fix any unresolved comments.

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, or 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 fix this CI failure" or "@codex address that feedback".

Comment on lines 351 to 354
def _update_sync():
conn = self._get_connection()

# Serialize token details as JSON

Choose a reason for hiding this comment

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

[P2] Guard turn usage writes with session lock

The detailed turn-level usage tracking is helpful, however_update_turn_usage_internal acquires the shared SQLite connection and performs writes without usingself._lock. For in-memory sessions that share a single connection, two concurrent calls tostore_run_usage() or an overlap withadd_items() can execute on the same connection from different threads, producingsqlite3.ProgrammingError/OperationalError (“database is locked”). Base session methods wrap all access in the lock for this reason; usage writes should do the same or otherwise ensure only one thread uses the connection at a time.

Useful? React with 👍 / 👎.

habema reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

This is valid, but SQLiteSession needs to reflect this as well

@habema
Copy link
ContributorAuthor

I added theactive_only param to usage methods. Instead of modifying turn_usage table, I kept all usage data intact (for cost accounting) but added SQL JOINs to filter by active messages when needed.

Forasyncio.Lock, I keptthreading.Lock to match base class. Changing it inAdvancedSQLiteSession only will require overriding all base class methods, so theasyncio refactor should be a separate PR that touches both classes.

@habema
Copy link
ContributorAuthor

I ran into a bug in my implementation ofreactivate_from_turn(), which uncovered deeper issues. I replaced the broken soft deletion approach with actual branch-based conversation management.

Key changes:

  • Addedbranch_id andbranch_turn_number columns to schema, removedis_active/deactivated_at
  • Replaceddeactivate_from_turn()/reactivate_from_turn() withcreate_branch_from_turn() that properly copies conversation history
  • Fixed the original bug where reactivating would affect both original and new branches
  • Added proper branch switching withswitch_to_branch() andlist_branches()
  • Updated unique constraints to includebranch_id so turn numbers can overlap between branches

Now each branch is truly isolated - no more weird cross-branch contamination when doing operations.

I understand this might require some extensive review, so take your time.

@habema
Copy link
ContributorAuthor

@seratch awaiting your review

@seratch
Copy link
Member

@habema Thanks for being patient here. I didn't have enough time for intensively reviewing this one today, but I will do tomorrow!

habema reacted with heart emoji

Copy link
Member

@seratchseratch left a comment

Choose a reason for hiding this comment

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

Great work! Left a few minor comments

habema reacted with heart emoji
Copy link
Member

@seratchseratch left a comment

Choose a reason for hiding this comment

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

Thanks for adding this!

habema reacted with heart emoji
@seratchseratch merged commit4f54878 intoopenai:mainSep 24, 2025
5 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@chatgpt-codex-connectorchatgpt-codex-connector[bot]chatgpt-codex-connector[bot] left review comments

@seratchseratchseratch approved these changes

Assignees
No one assigned
Labels
enhancementNew feature or requestfeature:sessions
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Suggestion: Database schema for conversations could be better
2 participants
@habema@seratch

[8]ページ先頭

©2009-2025 Movatter.jp