Skip to content

Alembic migration to make session_name required on messages#166

Merged
VVoruganti merged 8 commits into
mainfrom
rajat/DEV-994
Jul 22, 2025
Merged

Alembic migration to make session_name required on messages#166
VVoruganti merged 8 commits into
mainfrom
rajat/DEV-994

Conversation

@Rajat-Ahuja1997

@Rajat-Ahuja1997 Rajat-Ahuja1997 commented Jul 21, 2025

Copy link
Copy Markdown
Collaborator

Tested locally:

INFO  [alembic.runtime.migration] Running upgrade 917195d9b5e9 -> 05486ce795d5, make session_name required on messages
Found 11 orphaned messages without session_name
Found 4 workspace-peer combinations with orphaned messages
Creating default session 'default_session_GraceMoore996' for workspace 'LoadTest_tzu299tn' and peer 'GraceMoore996'
Created session peer association for peer 'GraceMoore996' in default session 'default_session_GraceMoore996'
Creating default session 'default_session_peer23' for workspace 'LoadTest_yexpjkps' and peer 'peer23'
Created session peer association for peer 'peer23' in default session 'default_session_peer23'
Creating default session 'default_session_alice' for workspace 'test' and peer 'alice'
Created session peer association for peer 'alice' in default session 'default_session_alice'
Creating default session 'default_session_AliceTaylor268' for workspace 'test_app' and peer 'AliceTaylor268'
Created session peer association for peer 'AliceTaylor268' in default session 'default_session_AliceTaylor268'
Making session_name NOT NULL

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • All messages and message embeddings must now be associated with a session, improving data consistency.
    • Increased the maximum allowed length for collection names from 512 to 1025 characters.
  • Bug Fixes

    • Messages and embeddings without a session are automatically assigned to default sessions to maintain integrity.
  • Documentation

    • Updated comments to reflect the mandatory session association for all messages and embeddings.

@coderabbitai

coderabbitai Bot commented Jul 21, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The changes enforce that every message must be associated with a session. This is achieved by updating the database schema, data models, and schemas to make the session_name field required. Orphaned messages are migrated to default sessions during the database migration process. Additionally, the maximum allowed length for collection names is increased from 512 to 1025 characters.

Changes

File(s) Change Summary
migrations/versions/05486ce795d5_make_session_name_required_on_messages.py Adds Alembic migration: migrates orphaned messages to default sessions, updates session-peers, and makes session_name non-nullable on messages and embeddings; increases collection name length constraint.
src/models.py Changes session_name in Message and MessageEmbedding models to non-nullable; updates collection name length constraint from 512 to 1025 characters.
src/schemas.py Changes Message.session_name field from optional to required in Pydantic schema.

Sequence Diagram(s)

sequenceDiagram
    participant MigrationScript
    participant DB
    participant SessionsTable
    participant SessionPeersTable

    MigrationScript->>DB: Find messages with null session_name
    loop For each workspace-peer with orphaned messages
        MigrationScript->>SessionsTable: Insert default session (handle conflicts)
        MigrationScript->>SessionPeersTable: Associate peer with new session (handle conflicts)
        MigrationScript->>DB: Update orphaned messages and embeddings to new session
    end
    MigrationScript->>DB: Ensure no orphaned messages remain
    MigrationScript->>DB: Alter messages and embeddings tables to make session_name non-nullable
    MigrationScript->>DB: Update collection name length constraint to 1025
Loading

Estimated code review effort

3 (~40 minutes)

Suggested reviewers

  • dr-frmr

Poem

In the warren of code, a session for all,
No message left lonely, no value too small.
Orphans are gathered, assigned with great care,
Now every chat knows its session is there.
🐇 With columns aligned and schemas anew,
This migration is done—hip-hop, hooray for you!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6517684 and e1b1733.

📒 Files selected for processing (2)
  • migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (1 hunks)
  • src/models.py (3 hunks)
📓 Path-based instructions (3)
src/**/*.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

src/**/*.py: Follow isort conventions with absolute imports preferred
snake_case for variables/functions; PascalCase for classes
Line length: 88 chars (Black compatible)
Explicit error handling with appropriate exception types
Docstrings: Use Google style docstrings
Use environment variables via python-dotenv (.env)
Use specific exception types (ResourceNotFoundException, ValidationException, etc.)
Proper logging with context instead of print statements

Files:

  • src/models.py
src/models.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

src/models.py: Use explicit type hints with SQLAlchemy mapped_column annotations
All tables use text IDs (nanoid format) as primary keys
Composite foreign keys for multi-tenant relationships
Feature flags on workspace, peer, and session levels
JSONB metadata fields for extensibility
HNSW indexes for vector similarity search

Files:

  • src/models.py
src/{models,crud}.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

Token counting on messages for usage tracking

Files:

  • src/models.py
🧠 Learnings (3)
📓 Common learnings
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:918-942
Timestamp: 2025-06-19T14:32:02.934Z
Learning: The queue and active_queue_sessions tables in the migration script `migrations/versions/d429de0e5338_adopt_peer_paradigm.py` are expected to remain small, so per-row updates are acceptable and performance optimizations like set-based updates are not necessary.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:188-194
Timestamp: 2025-06-18T15:58:51.202Z
Learning: In the migration file `migrations/versions/d429de0e5338_adopt_peer_paradigm.py`, the team has explicitly decided to accept SQL injection risks from f-string interpolation of schema names, despite the security concerns around DDL injection via the DATABASE_SCHEMA environment variable.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script `migrations/versions/d429de0e5338_adopt_peer_paradigm.py` properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.
Learnt from: dr-frmr
PR: plastic-labs/honcho#106
File: migrations/versions/a1b2c3d4e5f6_initial_schema.py:172-179
Timestamp: 2025-05-13T15:48:42.783Z
Learning: The `queue` and `active_queue_sessions` tables in the database schema reference `sessions.id` (numeric) rather than `sessions.public_id` (text) because they are internal-only tables not exposed through the API, where performance considerations outweigh consistent identifier usage.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#144
File: src/crud.py:1250-1250
Timestamp: 2025-06-26T18:39:54.942Z
Learning: Rajat-Ahuja1997 is comfortable exposing token counts in MessageCreate schema as a public property when it improves code maintainability and avoids type checking errors.
src/models.py (8)

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : All tables use text IDs (nanoid format) as primary keys

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : Use explicit type hints with SQLAlchemy mapped_column annotations

Learnt from: Rajat-Ahuja1997
PR: #166
File: migrations/versions/05486ce795d5_make_session_name_required_on_messages.py:60-114
Timestamp: 2025-07-21T20:57:42.135Z
Learning: Peer names in the Honcho codebase are restricted to the pattern r"^[a-zA-Z0-9_-]+$" (same as session names), which prevents SQL injection and JSON syntax errors when peer names are interpolated into SQL queries and JSON strings during migrations.

Learnt from: dr-frmr
PR: #131
File: tests/test_schema_validations.py:76-80
Timestamp: 2025-06-17T21:40:26.817Z
Learning: In the MessageCreate schema, peer_name is the actual field name with "peer_id" as an alias, so when constructing MessageCreate(peer_id="value"), it sets the peer_name attribute to "value".

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : Feature flags on workspace, peer, and session levels

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : Composite foreign keys for multi-tenant relationships

migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (10)

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:918-942
Timestamp: 2025-06-19T14:32:02.934Z
Learning: The queue and active_queue_sessions tables in the migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py are expected to remain small, so per-row updates are acceptable and performance optimizations like set-based updates are not necessary.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:188-194
Timestamp: 2025-06-18T15:58:51.202Z
Learning: In the migration file migrations/versions/d429de0e5338_adopt_peer_paradigm.py, the team has explicitly decided to accept SQL injection risks from f-string interpolation of schema names, despite the security concerns around DDL injection via the DATABASE_SCHEMA environment variable.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.

Learnt from: Rajat-Ahuja1997
PR: #166
File: migrations/versions/05486ce795d5_make_session_name_required_on_messages.py:60-114
Timestamp: 2025-07-21T20:57:42.135Z
Learning: Peer names in the Honcho codebase are restricted to the pattern r"^[a-zA-Z0-9_-]+$" (same as session names), which prevents SQL injection and JSON syntax errors when peer names are interpolated into SQL queries and JSON strings during migrations.

Learnt from: dr-frmr
PR: #131
File: tests/test_schema_validations.py:76-80
Timestamp: 2025-06-17T21:40:26.817Z
Learning: In the MessageCreate schema, peer_name is the actual field name with "peer_id" as an alias, so when constructing MessageCreate(peer_id="value"), it sets the peer_name attribute to "value".

Learnt from: dr-frmr
PR: #131
File: src/routers/sessions.py:206-213
Timestamp: 2025-06-18T20:42:06.458Z
Learning: The get_or_create_session function in this codebase is designed to handle both session creation and adding peers to existing sessions. When called with peers, it will add those peers to an existing session rather than creating a duplicate session.

Learnt from: Rajat-Ahuja1997
PR: #131
File: src/crud.py:503-505
Timestamp: 2025-06-18T14:50:59.967Z
Learning: The Honcho project prefers upsert behavior for update operations across all resources (sessions, peers, workspaces). Update operations should create the resource if it doesn't exist rather than failing fast. This is an explicit design decision that differs from typical REST semantics but provides a more forgiving API experience.

Learnt from: Rajat-Ahuja1997
PR: #144
File: src/crud.py:1250-1250
Timestamp: 2025-06-26T18:39:54.942Z
Learning: Rajat-Ahuja1997 is comfortable exposing token counts in MessageCreate schema as a public property when it improves code maintainability and avoids type checking errors.

Learnt from: VVoruganti
PR: #144
File: src/crud.py:118-125
Timestamp: 2025-06-25T17:34:47.676Z
Learning: In embedding generation contexts (like message creation), prefer fail-fast error handling over graceful recovery to ensure data integrity. If embeddings cannot be generated when required, the operation should fail rather than continue with incomplete data.

🪛 Ruff (0.12.2)
migrations/versions/05486ce795d5_make_session_name_required_on_messages.py

42-42: Possible SQL injection vector through string-based query construction

(S608)


42-42: Trailing comma missing

Add trailing comma

(COM812)


50-54: Possible SQL injection vector through string-based query construction

(S608)


54-54: Trailing comma missing

Add trailing comma

(COM812)


60-60: Trailing comma missing

Add trailing comma

(COM812)


68-68: Trailing comma missing

Add trailing comma

(COM812)


73-86: Possible SQL injection vector through string-based query construction

(S608)


86-86: Trailing comma missing

Add trailing comma

(COM812)


91-103: Possible SQL injection vector through string-based query construction

(S608)


103-103: Trailing comma missing

Add trailing comma

(COM812)


106-106: Trailing comma missing

Add trailing comma

(COM812)


111-117: Possible SQL injection vector through string-based query construction

(S608)


117-117: Trailing comma missing

Add trailing comma

(COM812)


122-128: Possible SQL injection vector through string-based query construction

(S608)


128-128: Trailing comma missing

Add trailing comma

(COM812)


133-133: Possible SQL injection vector through string-based query construction

(S608)


133-133: Trailing comma missing

Add trailing comma

(COM812)


137-139: Create your own exception

(TRY002)


137-139: Avoid specifying long messages outside the exception class

(TRY003)


138-138: Trailing comma missing

Add trailing comma

(COM812)


148-148: Possible SQL injection vector through string-based query construction

(S608)


148-148: Trailing comma missing

Add trailing comma

(COM812)


149-149: Trailing comma missing

Add trailing comma

(COM812)


153-155: Create your own exception

(TRY002)


153-155: Avoid specifying long messages outside the exception class

(TRY003)


154-154: Trailing comma missing

Add trailing comma

(COM812)


167-167: Trailing comma missing

Add trailing comma

(COM812)


179-179: Trailing comma missing

Add trailing comma

(COM812)


185-185: Trailing comma missing

Add trailing comma

(COM812)

🧰 Additional context used
📓 Path-based instructions (3)
src/**/*.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

src/**/*.py: Follow isort conventions with absolute imports preferred
snake_case for variables/functions; PascalCase for classes
Line length: 88 chars (Black compatible)
Explicit error handling with appropriate exception types
Docstrings: Use Google style docstrings
Use environment variables via python-dotenv (.env)
Use specific exception types (ResourceNotFoundException, ValidationException, etc.)
Proper logging with context instead of print statements

Files:

  • src/models.py
src/models.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

src/models.py: Use explicit type hints with SQLAlchemy mapped_column annotations
All tables use text IDs (nanoid format) as primary keys
Composite foreign keys for multi-tenant relationships
Feature flags on workspace, peer, and session levels
JSONB metadata fields for extensibility
HNSW indexes for vector similarity search

Files:

  • src/models.py
src/{models,crud}.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

Token counting on messages for usage tracking

Files:

  • src/models.py
🧠 Learnings (3)
📓 Common learnings
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:918-942
Timestamp: 2025-06-19T14:32:02.934Z
Learning: The queue and active_queue_sessions tables in the migration script `migrations/versions/d429de0e5338_adopt_peer_paradigm.py` are expected to remain small, so per-row updates are acceptable and performance optimizations like set-based updates are not necessary.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:188-194
Timestamp: 2025-06-18T15:58:51.202Z
Learning: In the migration file `migrations/versions/d429de0e5338_adopt_peer_paradigm.py`, the team has explicitly decided to accept SQL injection risks from f-string interpolation of schema names, despite the security concerns around DDL injection via the DATABASE_SCHEMA environment variable.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script `migrations/versions/d429de0e5338_adopt_peer_paradigm.py` properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.
Learnt from: dr-frmr
PR: plastic-labs/honcho#106
File: migrations/versions/a1b2c3d4e5f6_initial_schema.py:172-179
Timestamp: 2025-05-13T15:48:42.783Z
Learning: The `queue` and `active_queue_sessions` tables in the database schema reference `sessions.id` (numeric) rather than `sessions.public_id` (text) because they are internal-only tables not exposed through the API, where performance considerations outweigh consistent identifier usage.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#144
File: src/crud.py:1250-1250
Timestamp: 2025-06-26T18:39:54.942Z
Learning: Rajat-Ahuja1997 is comfortable exposing token counts in MessageCreate schema as a public property when it improves code maintainability and avoids type checking errors.
src/models.py (8)

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : All tables use text IDs (nanoid format) as primary keys

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : Use explicit type hints with SQLAlchemy mapped_column annotations

Learnt from: Rajat-Ahuja1997
PR: #166
File: migrations/versions/05486ce795d5_make_session_name_required_on_messages.py:60-114
Timestamp: 2025-07-21T20:57:42.135Z
Learning: Peer names in the Honcho codebase are restricted to the pattern r"^[a-zA-Z0-9_-]+$" (same as session names), which prevents SQL injection and JSON syntax errors when peer names are interpolated into SQL queries and JSON strings during migrations.

Learnt from: dr-frmr
PR: #131
File: tests/test_schema_validations.py:76-80
Timestamp: 2025-06-17T21:40:26.817Z
Learning: In the MessageCreate schema, peer_name is the actual field name with "peer_id" as an alias, so when constructing MessageCreate(peer_id="value"), it sets the peer_name attribute to "value".

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : Feature flags on workspace, peer, and session levels

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : Composite foreign keys for multi-tenant relationships

migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (10)

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:918-942
Timestamp: 2025-06-19T14:32:02.934Z
Learning: The queue and active_queue_sessions tables in the migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py are expected to remain small, so per-row updates are acceptable and performance optimizations like set-based updates are not necessary.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:188-194
Timestamp: 2025-06-18T15:58:51.202Z
Learning: In the migration file migrations/versions/d429de0e5338_adopt_peer_paradigm.py, the team has explicitly decided to accept SQL injection risks from f-string interpolation of schema names, despite the security concerns around DDL injection via the DATABASE_SCHEMA environment variable.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.

Learnt from: Rajat-Ahuja1997
PR: #166
File: migrations/versions/05486ce795d5_make_session_name_required_on_messages.py:60-114
Timestamp: 2025-07-21T20:57:42.135Z
Learning: Peer names in the Honcho codebase are restricted to the pattern r"^[a-zA-Z0-9_-]+$" (same as session names), which prevents SQL injection and JSON syntax errors when peer names are interpolated into SQL queries and JSON strings during migrations.

Learnt from: dr-frmr
PR: #131
File: tests/test_schema_validations.py:76-80
Timestamp: 2025-06-17T21:40:26.817Z
Learning: In the MessageCreate schema, peer_name is the actual field name with "peer_id" as an alias, so when constructing MessageCreate(peer_id="value"), it sets the peer_name attribute to "value".

Learnt from: dr-frmr
PR: #131
File: src/routers/sessions.py:206-213
Timestamp: 2025-06-18T20:42:06.458Z
Learning: The get_or_create_session function in this codebase is designed to handle both session creation and adding peers to existing sessions. When called with peers, it will add those peers to an existing session rather than creating a duplicate session.

Learnt from: Rajat-Ahuja1997
PR: #131
File: src/crud.py:503-505
Timestamp: 2025-06-18T14:50:59.967Z
Learning: The Honcho project prefers upsert behavior for update operations across all resources (sessions, peers, workspaces). Update operations should create the resource if it doesn't exist rather than failing fast. This is an explicit design decision that differs from typical REST semantics but provides a more forgiving API experience.

Learnt from: Rajat-Ahuja1997
PR: #144
File: src/crud.py:1250-1250
Timestamp: 2025-06-26T18:39:54.942Z
Learning: Rajat-Ahuja1997 is comfortable exposing token counts in MessageCreate schema as a public property when it improves code maintainability and avoids type checking errors.

Learnt from: VVoruganti
PR: #144
File: src/crud.py:118-125
Timestamp: 2025-06-25T17:34:47.676Z
Learning: In embedding generation contexts (like message creation), prefer fail-fast error handling over graceful recovery to ensure data integrity. If embeddings cannot be generated when required, the operation should fail rather than continue with incomplete data.

🪛 Ruff (0.12.2)
migrations/versions/05486ce795d5_make_session_name_required_on_messages.py

42-42: Possible SQL injection vector through string-based query construction

(S608)


42-42: Trailing comma missing

Add trailing comma

(COM812)


50-54: Possible SQL injection vector through string-based query construction

(S608)


54-54: Trailing comma missing

Add trailing comma

(COM812)


60-60: Trailing comma missing

Add trailing comma

(COM812)


68-68: Trailing comma missing

Add trailing comma

(COM812)


73-86: Possible SQL injection vector through string-based query construction

(S608)


86-86: Trailing comma missing

Add trailing comma

(COM812)


91-103: Possible SQL injection vector through string-based query construction

(S608)


103-103: Trailing comma missing

Add trailing comma

(COM812)


106-106: Trailing comma missing

Add trailing comma

(COM812)


111-117: Possible SQL injection vector through string-based query construction

(S608)


117-117: Trailing comma missing

Add trailing comma

(COM812)


122-128: Possible SQL injection vector through string-based query construction

(S608)


128-128: Trailing comma missing

Add trailing comma

(COM812)


133-133: Possible SQL injection vector through string-based query construction

(S608)


133-133: Trailing comma missing

Add trailing comma

(COM812)


137-139: Create your own exception

(TRY002)


137-139: Avoid specifying long messages outside the exception class

(TRY003)


138-138: Trailing comma missing

Add trailing comma

(COM812)


148-148: Possible SQL injection vector through string-based query construction

(S608)


148-148: Trailing comma missing

Add trailing comma

(COM812)


149-149: Trailing comma missing

Add trailing comma

(COM812)


153-155: Create your own exception

(TRY002)


153-155: Avoid specifying long messages outside the exception class

(TRY003)


154-154: Trailing comma missing

Add trailing comma

(COM812)


167-167: Trailing comma missing

Add trailing comma

(COM812)


179-179: Trailing comma missing

Add trailing comma

(COM812)


185-185: Trailing comma missing

Add trailing comma

(COM812)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: test-python
🔇 Additional comments (5)
src/models.py (3)

180-182: LGTM! Model changes align with the migration.

The changes correctly update the session_name field to be non-nullable and the comment accurately reflects the migration strategy.


247-247: LGTM! MessageEmbedding model updated consistently.

The session_name field is correctly made non-nullable to match the Message model changes.


305-305: Collection name length appropriately increased.

The constraint increase from 512 to 1025 correctly accommodates the worst-case scenario where two peer names of maximum length (512 chars each) plus a separator character need to be stored in the collection name.

migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (2)

27-169: Well-structured migration with proper conflict handling.

The migration properly handles orphaned messages by:

  • Creating default sessions with ON CONFLICT DO NOTHING to avoid duplicate key errors
  • Creating session-peer associations with proper conflict handling
  • Performing sanity checks before making schema changes
  • Handling both messages and message_embeddings tables consistently

The use of peer_name as the default session name is a reasonable approach for orphaned messages.


171-192: Appropriate downgrade strategy preserves data integrity.

The downgrade correctly reverses the schema changes while preserving the data migrations. This is the right approach as it prevents data loss and maintains referential integrity.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Rajat-Ahuja1997 Rajat-Ahuja1997 changed the title Rajat/dev 994 Make session_name required on messages Jul 21, 2025
@Rajat-Ahuja1997 Rajat-Ahuja1997 marked this pull request as ready for review July 21, 2025 20:51
@Rajat-Ahuja1997 Rajat-Ahuja1997 changed the title Make session_name required on messages Alembic migration to make session_name required on messages Jul 21, 2025

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 5

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 54fc522 and 62bfedb.

📒 Files selected for processing (3)
  • migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (1 hunks)
  • src/models.py (1 hunks)
  • src/schemas.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
src/**/*.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

src/**/*.py: Follow isort conventions with absolute imports preferred
snake_case for variables/functions; PascalCase for classes
Line length: 88 chars (Black compatible)
Explicit error handling with appropriate exception types
Docstrings: Use Google style docstrings
Use environment variables via python-dotenv (.env)
Use specific exception types (ResourceNotFoundException, ValidationException, etc.)
Proper logging with context instead of print statements

Files:

  • src/schemas.py
  • src/models.py
src/models.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

src/models.py: Use explicit type hints with SQLAlchemy mapped_column annotations
All tables use text IDs (nanoid format) as primary keys
Composite foreign keys for multi-tenant relationships
Feature flags on workspace, peer, and session levels
JSONB metadata fields for extensibility
HNSW indexes for vector similarity search

Files:

  • src/models.py
src/{models,crud}.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

Token counting on messages for usage tracking

Files:

  • src/models.py
🧠 Learnings (4)
📓 Common learnings
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:918-942
Timestamp: 2025-06-19T14:32:02.934Z
Learning: The queue and active_queue_sessions tables in the migration script `migrations/versions/d429de0e5338_adopt_peer_paradigm.py` are expected to remain small, so per-row updates are acceptable and performance optimizations like set-based updates are not necessary.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:188-194
Timestamp: 2025-06-18T15:58:51.202Z
Learning: In the migration file `migrations/versions/d429de0e5338_adopt_peer_paradigm.py`, the team has explicitly decided to accept SQL injection risks from f-string interpolation of schema names, despite the security concerns around DDL injection via the DATABASE_SCHEMA environment variable.
Learnt from: dr-frmr
PR: plastic-labs/honcho#106
File: migrations/versions/a1b2c3d4e5f6_initial_schema.py:172-179
Timestamp: 2025-05-13T15:48:42.783Z
Learning: The `queue` and `active_queue_sessions` tables in the database schema reference `sessions.id` (numeric) rather than `sessions.public_id` (text) because they are internal-only tables not exposed through the API, where performance considerations outweigh consistent identifier usage.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script `migrations/versions/d429de0e5338_adopt_peer_paradigm.py` properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#144
File: src/crud.py:1250-1250
Timestamp: 2025-06-26T18:39:54.942Z
Learning: Rajat-Ahuja1997 is comfortable exposing token counts in MessageCreate schema as a public property when it improves code maintainability and avoids type checking errors.
src/schemas.py (2)

Learnt from: dr-frmr
PR: #131
File: tests/test_schema_validations.py:76-80
Timestamp: 2025-06-17T21:40:26.817Z
Learning: In the MessageCreate schema, peer_name is the actual field name with "peer_id" as an alias, so when constructing MessageCreate(peer_id="value"), it sets the peer_name attribute to "value".

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.893Z
Learning: Applies to src/models.py : JSONB metadata fields for extensibility

src/models.py (3)

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.893Z
Learning: Applies to src/models.py : Use explicit type hints with SQLAlchemy mapped_column annotations

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.893Z
Learning: Applies to src/models.py : All tables use text IDs (nanoid format) as primary keys

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.

migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (4)

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:918-942
Timestamp: 2025-06-19T14:32:02.934Z
Learning: The queue and active_queue_sessions tables in the migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py are expected to remain small, so per-row updates are acceptable and performance optimizations like set-based updates are not necessary.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:188-194
Timestamp: 2025-06-18T15:58:51.202Z
Learning: In the migration file migrations/versions/d429de0e5338_adopt_peer_paradigm.py, the team has explicitly decided to accept SQL injection risks from f-string interpolation of schema names, despite the security concerns around DDL injection via the DATABASE_SCHEMA environment variable.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.

🪛 Ruff (0.12.2)
migrations/versions/05486ce795d5_make_session_name_required_on_messages.py

39-39: Possible SQL injection vector through string-based query construction

(S608)


39-39: Trailing comma missing

Add trailing comma

(COM812)


47-51: Possible SQL injection vector through string-based query construction

(S608)


51-51: Trailing comma missing

Add trailing comma

(COM812)


57-57: Trailing comma missing

Add trailing comma

(COM812)


66-66: Trailing comma missing

Add trailing comma

(COM812)


71-83: Possible SQL injection vector through string-based query construction

(S608)


83-83: Trailing comma missing

Add trailing comma

(COM812)


88-99: Possible SQL injection vector through string-based query construction

(S608)


99-99: Trailing comma missing

Add trailing comma

(COM812)


102-102: Trailing comma missing

Add trailing comma

(COM812)


107-113: Possible SQL injection vector through string-based query construction

(S608)


113-113: Trailing comma missing

Add trailing comma

(COM812)


118-118: Possible SQL injection vector through string-based query construction

(S608)


118-118: Trailing comma missing

Add trailing comma

(COM812)


122-124: Create your own exception

(TRY002)


122-124: Avoid specifying long messages outside the exception class

(TRY003)


123-123: Trailing comma missing

Add trailing comma

(COM812)

🔇 Additional comments (1)
migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (1)

131-138: Downgrade implementation is appropriate.

The downgrade function correctly makes the column nullable again while preserving the default sessions created during upgrade. The comment clearly documents this behavior.

Comment thread migrations/versions/05486ce795d5_make_session_name_required_on_messages.py Outdated
Comment thread src/models.py Outdated
Comment thread src/schemas.py
peer_name: str = Field(serialization_alias="peer_id")
# NOTE: Messages in Honcho 2.0 could historically be stored outside of a session. See models.py for more details.
session_name: str | None = Field(serialization_alias="session_id")
session_name: str = Field(serialization_alias="session_id")

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.

💡 Verification agent

🧩 Analysis chain

Schema change aligns with database constraints.

The change to make session_name required is consistent with the database migration and model changes. This enforces data integrity at the API level.

Since this is a breaking API change, please verify that API documentation has been updated to reflect this requirement:


🌐 Web query:

Does the Honcho API documentation mention that session_name/session_id is now a required field for Message creation?

💡 Result:

Based on the available Honcho API documentation, there is no explicit mention that session_name or session_id are required fields when creating a message. The documentation for creating a batch of messages for a session indicates that the session_id is a required path parameter, but it does not specify the necessity of session_name for message creation. (docs.honcho.dev)

However, in the Honcho Python SDK, the create method for messages requires the session_id as a parameter, but not session_name. (github.com)

Given this, it appears that session_id is necessary for message creation, but session_name is not. For the most accurate and up-to-date information, I recommend consulting the latest Honcho API documentation or reaching out to Honcho's support channels.

Citations:


Update API documentation to reflect required session_name
The Honcho API docs and Python SDK currently don’t state that session_name (aliased as session_id) is now a required field when creating messages. Please update:

  • The API reference for Create Batch Messages for Session to note that session_id (a.k.a. session_name) is mandatory in the request body.
  • The Honcho Python SDK documentation (create method for messages) to include session_name/session_id as a required parameter.
🤖 Prompt for AI Agents
In src/schemas.py at line 145, the field session_name is marked with a
serialization alias session_id and is required. Update the API documentation for
the Create Batch Messages for Session endpoint to explicitly state that
session_id (session_name) is a mandatory field in the request body.
Additionally, update the Honcho Python SDK documentation for the create method
related to messages to include session_name/session_id as a required parameter,
ensuring both docs clearly reflect this requirement.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

♻️ Duplicate comments (4)
src/models.py (1)

180-182: Fix type annotation to match non-nullable constraint.

The type annotation should be str instead of str | None since nullable=False.

migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (3)

62-84: Handle unique constraint conflicts on default session names.

The insertion could fail if a session with the same name already exists for the workspace.


134-136: Use a specific exception type instead of generic Exception.

Define and use a migration-specific exception for better error handling.


150-152: Use a specific exception type for consistency.

Similar to the earlier exception, use a specific exception type here as well.

Define a custom exception at the module level and use it for both error cases:

+class OrphanedRecordsError(Exception):
+    """Raised when orphaned records remain after migration."""
+    pass
+
 def upgrade() -> None:
     ...
     if remaining_orphaned_embeddings and remaining_orphaned_embeddings > 0:
-        raise Exception(
+        raise OrphanedRecordsError(
             f"Still have {remaining_orphaned_embeddings} orphaned message embeddings after migration"
         )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 62bfedb and 0b472af.

📒 Files selected for processing (2)
  • migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (1 hunks)
  • src/models.py (2 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
src/**/*.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

src/**/*.py: Follow isort conventions with absolute imports preferred
snake_case for variables/functions; PascalCase for classes
Line length: 88 chars (Black compatible)
Explicit error handling with appropriate exception types
Docstrings: Use Google style docstrings
Use environment variables via python-dotenv (.env)
Use specific exception types (ResourceNotFoundException, ValidationException, etc.)
Proper logging with context instead of print statements

Files:

  • src/models.py
src/models.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

src/models.py: Use explicit type hints with SQLAlchemy mapped_column annotations
All tables use text IDs (nanoid format) as primary keys
Composite foreign keys for multi-tenant relationships
Feature flags on workspace, peer, and session levels
JSONB metadata fields for extensibility
HNSW indexes for vector similarity search

Files:

  • src/models.py
src/{models,crud}.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

Token counting on messages for usage tracking

Files:

  • src/models.py
🧠 Learnings (3)
📓 Common learnings
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:918-942
Timestamp: 2025-06-19T14:32:02.934Z
Learning: The queue and active_queue_sessions tables in the migration script `migrations/versions/d429de0e5338_adopt_peer_paradigm.py` are expected to remain small, so per-row updates are acceptable and performance optimizations like set-based updates are not necessary.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:188-194
Timestamp: 2025-06-18T15:58:51.202Z
Learning: In the migration file `migrations/versions/d429de0e5338_adopt_peer_paradigm.py`, the team has explicitly decided to accept SQL injection risks from f-string interpolation of schema names, despite the security concerns around DDL injection via the DATABASE_SCHEMA environment variable.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script `migrations/versions/d429de0e5338_adopt_peer_paradigm.py` properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.
Learnt from: dr-frmr
PR: plastic-labs/honcho#106
File: migrations/versions/a1b2c3d4e5f6_initial_schema.py:172-179
Timestamp: 2025-05-13T15:48:42.783Z
Learning: The `queue` and `active_queue_sessions` tables in the database schema reference `sessions.id` (numeric) rather than `sessions.public_id` (text) because they are internal-only tables not exposed through the API, where performance considerations outweigh consistent identifier usage.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#144
File: src/crud.py:1250-1250
Timestamp: 2025-06-26T18:39:54.942Z
Learning: Rajat-Ahuja1997 is comfortable exposing token counts in MessageCreate schema as a public property when it improves code maintainability and avoids type checking errors.
src/models.py (4)

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.893Z
Learning: Applies to src/models.py : All tables use text IDs (nanoid format) as primary keys

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.893Z
Learning: Applies to src/models.py : Use explicit type hints with SQLAlchemy mapped_column annotations

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.893Z
Learning: Applies to src/models.py : Feature flags on workspace, peer, and session levels

migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (10)

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:918-942
Timestamp: 2025-06-19T14:32:02.934Z
Learning: The queue and active_queue_sessions tables in the migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py are expected to remain small, so per-row updates are acceptable and performance optimizations like set-based updates are not necessary.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:188-194
Timestamp: 2025-06-18T15:58:51.202Z
Learning: In the migration file migrations/versions/d429de0e5338_adopt_peer_paradigm.py, the team has explicitly decided to accept SQL injection risks from f-string interpolation of schema names, despite the security concerns around DDL injection via the DATABASE_SCHEMA environment variable.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.

Learnt from: Rajat-Ahuja1997
PR: #166
File: migrations/versions/05486ce795d5_make_session_name_required_on_messages.py:60-114
Timestamp: 2025-07-21T20:57:42.096Z
Learning: Peer names in the Honcho codebase are restricted to the pattern r"^[a-zA-Z0-9_-]+$" (same as session names), which prevents SQL injection and JSON syntax errors when peer names are interpolated into SQL queries and JSON strings during migrations.

Learnt from: dr-frmr
PR: #131
File: tests/test_schema_validations.py:76-80
Timestamp: 2025-06-17T21:40:26.817Z
Learning: In the MessageCreate schema, peer_name is the actual field name with "peer_id" as an alias, so when constructing MessageCreate(peer_id="value"), it sets the peer_name attribute to "value".

Learnt from: dr-frmr
PR: #131
File: src/routers/sessions.py:206-213
Timestamp: 2025-06-18T20:42:06.458Z
Learning: The get_or_create_session function in this codebase is designed to handle both session creation and adding peers to existing sessions. When called with peers, it will add those peers to an existing session rather than creating a duplicate session.

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.893Z
Learning: Applies to src/**/*.py : Explicit error handling with appropriate exception types

Learnt from: Rajat-Ahuja1997
PR: #131
File: src/crud.py:503-505
Timestamp: 2025-06-18T14:50:59.967Z
Learning: The Honcho project prefers upsert behavior for update operations across all resources (sessions, peers, workspaces). Update operations should create the resource if it doesn't exist rather than failing fast. This is an explicit design decision that differs from typical REST semantics but provides a more forgiving API experience.

Learnt from: Rajat-Ahuja1997
PR: #144
File: src/crud.py:1250-1250
Timestamp: 2025-06-26T18:39:54.942Z
Learning: Rajat-Ahuja1997 is comfortable exposing token counts in MessageCreate schema as a public property when it improves code maintainability and avoids type checking errors.

🪛 Ruff (0.12.2)
migrations/versions/05486ce795d5_make_session_name_required_on_messages.py

40-40: Possible SQL injection vector through string-based query construction

(S608)


40-40: Trailing comma missing

Add trailing comma

(COM812)


48-52: Possible SQL injection vector through string-based query construction

(S608)


52-52: Trailing comma missing

Add trailing comma

(COM812)


58-58: Trailing comma missing

Add trailing comma

(COM812)


67-67: Trailing comma missing

Add trailing comma

(COM812)


72-84: Possible SQL injection vector through string-based query construction

(S608)


84-84: Trailing comma missing

Add trailing comma

(COM812)


89-100: Possible SQL injection vector through string-based query construction

(S608)


100-100: Trailing comma missing

Add trailing comma

(COM812)


103-103: Trailing comma missing

Add trailing comma

(COM812)


108-114: Possible SQL injection vector through string-based query construction

(S608)


114-114: Trailing comma missing

Add trailing comma

(COM812)


119-125: Possible SQL injection vector through string-based query construction

(S608)


125-125: Trailing comma missing

Add trailing comma

(COM812)


130-130: Possible SQL injection vector through string-based query construction

(S608)


130-130: Trailing comma missing

Add trailing comma

(COM812)


134-136: Create your own exception

(TRY002)


134-136: Avoid specifying long messages outside the exception class

(TRY003)


135-135: Trailing comma missing

Add trailing comma

(COM812)


145-145: Possible SQL injection vector through string-based query construction

(S608)


145-145: Trailing comma missing

Add trailing comma

(COM812)


146-146: Trailing comma missing

Add trailing comma

(COM812)


150-152: Create your own exception

(TRY002)


150-152: Avoid specifying long messages outside the exception class

(TRY003)


151-151: Trailing comma missing

Add trailing comma

(COM812)

🔇 Additional comments (2)
src/models.py (1)

247-247: LGTM! Type annotation correctly matches the non-nullable constraint.

The session_name field is properly defined as required with matching type annotation and column constraint.

migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (1)

159-169: LGTM! Downgrade appropriately reverses schema changes.

The function correctly makes the columns nullable again and clearly documents that it doesn't attempt to restore the original data state.

Comment thread migrations/versions/05486ce795d5_make_session_name_required_on_messages.py Outdated

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

♻️ Duplicate comments (1)
migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (1)

135-137: Use specific exception types for better error handling.

Generic exceptions make it difficult for callers to handle specific error cases.

Define a custom exception at the module level:

class MigrationError(Exception):
    """Raised when migration encounters an error."""
    pass

Then use it:

-        raise Exception(
+        raise MigrationError(
             f"Still have {remaining_orphaned} orphaned messages after migration"
         )
-        raise Exception(
+        raise MigrationError(
             f"Still have {remaining_orphaned_embeddings} orphaned message embeddings after migration"
         )

Also applies to: 151-153

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0b472af and d4a4bf5.

📒 Files selected for processing (1)
  • migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:918-942
Timestamp: 2025-06-19T14:32:02.934Z
Learning: The queue and active_queue_sessions tables in the migration script `migrations/versions/d429de0e5338_adopt_peer_paradigm.py` are expected to remain small, so per-row updates are acceptable and performance optimizations like set-based updates are not necessary.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:188-194
Timestamp: 2025-06-18T15:58:51.202Z
Learning: In the migration file `migrations/versions/d429de0e5338_adopt_peer_paradigm.py`, the team has explicitly decided to accept SQL injection risks from f-string interpolation of schema names, despite the security concerns around DDL injection via the DATABASE_SCHEMA environment variable.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script `migrations/versions/d429de0e5338_adopt_peer_paradigm.py` properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.
Learnt from: dr-frmr
PR: plastic-labs/honcho#106
File: migrations/versions/a1b2c3d4e5f6_initial_schema.py:172-179
Timestamp: 2025-05-13T15:48:42.783Z
Learning: The `queue` and `active_queue_sessions` tables in the database schema reference `sessions.id` (numeric) rather than `sessions.public_id` (text) because they are internal-only tables not exposed through the API, where performance considerations outweigh consistent identifier usage.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#144
File: src/crud.py:1250-1250
Timestamp: 2025-06-26T18:39:54.942Z
Learning: Rajat-Ahuja1997 is comfortable exposing token counts in MessageCreate schema as a public property when it improves code maintainability and avoids type checking errors.
migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (9)

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:918-942
Timestamp: 2025-06-19T14:32:02.934Z
Learning: The queue and active_queue_sessions tables in the migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py are expected to remain small, so per-row updates are acceptable and performance optimizations like set-based updates are not necessary.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:188-194
Timestamp: 2025-06-18T15:58:51.202Z
Learning: In the migration file migrations/versions/d429de0e5338_adopt_peer_paradigm.py, the team has explicitly decided to accept SQL injection risks from f-string interpolation of schema names, despite the security concerns around DDL injection via the DATABASE_SCHEMA environment variable.

Learnt from: Rajat-Ahuja1997
PR: #166
File: migrations/versions/05486ce795d5_make_session_name_required_on_messages.py:60-114
Timestamp: 2025-07-21T20:57:42.096Z
Learning: Peer names in the Honcho codebase are restricted to the pattern r"^[a-zA-Z0-9_-]+$" (same as session names), which prevents SQL injection and JSON syntax errors when peer names are interpolated into SQL queries and JSON strings during migrations.

Learnt from: dr-frmr
PR: #131
File: tests/test_schema_validations.py:76-80
Timestamp: 2025-06-17T21:40:26.817Z
Learning: In the MessageCreate schema, peer_name is the actual field name with "peer_id" as an alias, so when constructing MessageCreate(peer_id="value"), it sets the peer_name attribute to "value".

Learnt from: dr-frmr
PR: #131
File: src/routers/sessions.py:206-213
Timestamp: 2025-06-18T20:42:06.458Z
Learning: The get_or_create_session function in this codebase is designed to handle both session creation and adding peers to existing sessions. When called with peers, it will add those peers to an existing session rather than creating a duplicate session.

Learnt from: Rajat-Ahuja1997
PR: #131
File: src/crud.py:503-505
Timestamp: 2025-06-18T14:50:59.967Z
Learning: The Honcho project prefers upsert behavior for update operations across all resources (sessions, peers, workspaces). Update operations should create the resource if it doesn't exist rather than failing fast. This is an explicit design decision that differs from typical REST semantics but provides a more forgiving API experience.

Learnt from: Rajat-Ahuja1997
PR: #144
File: src/crud.py:1250-1250
Timestamp: 2025-06-26T18:39:54.942Z
Learning: Rajat-Ahuja1997 is comfortable exposing token counts in MessageCreate schema as a public property when it improves code maintainability and avoids type checking errors.

🪛 Ruff (0.12.2)
migrations/versions/05486ce795d5_make_session_name_required_on_messages.py

40-40: Possible SQL injection vector through string-based query construction

(S608)


40-40: Trailing comma missing

Add trailing comma

(COM812)


48-52: Possible SQL injection vector through string-based query construction

(S608)


52-52: Trailing comma missing

Add trailing comma

(COM812)


58-58: Trailing comma missing

Add trailing comma

(COM812)


66-66: Trailing comma missing

Add trailing comma

(COM812)


71-84: Possible SQL injection vector through string-based query construction

(S608)


84-84: Trailing comma missing

Add trailing comma

(COM812)


89-101: Possible SQL injection vector through string-based query construction

(S608)


101-101: Trailing comma missing

Add trailing comma

(COM812)


104-104: Trailing comma missing

Add trailing comma

(COM812)


109-115: Possible SQL injection vector through string-based query construction

(S608)


115-115: Trailing comma missing

Add trailing comma

(COM812)


120-126: Possible SQL injection vector through string-based query construction

(S608)


126-126: Trailing comma missing

Add trailing comma

(COM812)


131-131: Possible SQL injection vector through string-based query construction

(S608)


131-131: Trailing comma missing

Add trailing comma

(COM812)


135-137: Create your own exception

(TRY002)


135-137: Avoid specifying long messages outside the exception class

(TRY003)


136-136: Trailing comma missing

Add trailing comma

(COM812)


146-146: Possible SQL injection vector through string-based query construction

(S608)


146-146: Trailing comma missing

Add trailing comma

(COM812)


147-147: Trailing comma missing

Add trailing comma

(COM812)


151-153: Create your own exception

(TRY002)


151-153: Avoid specifying long messages outside the exception class

(TRY003)


152-152: Trailing comma missing

Add trailing comma

(COM812)

🔇 Additional comments (4)
migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (4)

1-24: Clean migration setup with appropriate imports.

The migration header and imports follow Alembic conventions correctly.


26-44: Effective orphaned message detection.

The logic correctly identifies messages without sessions and provides helpful logging.


87-128: Comprehensive handling of session associations and message updates.

The migration correctly:

  • Creates session-peer associations with conflict handling
  • Updates both messages and message_embeddings tables
  • Uses precise WHERE clauses to target only orphaned records

160-171: Appropriate downgrade implementation.

The downgrade correctly reverses the schema changes while preserving data integrity. The comments clearly document that default sessions and message assignments are retained.

Comment thread migrations/versions/05486ce795d5_make_session_name_required_on_messages.py Outdated

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

♻️ Duplicate comments (2)
migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (2)

81-81: Fix invalid JSON syntax in internal_metadata.

The JSON key contains a space which makes it invalid. Keys in JSON cannot contain spaces without being escaped.

Apply this fix:

-                        '{{"migration note" : "Default session created for orphaned messages from peer {peer_name}"}}',
+                        '{{"migration_note": "Default session created for orphaned messages from peer {peer_name}"}}',

137-139: Use a specific exception type instead of generic Exception.

Using a generic Exception makes error handling more difficult for callers.

Define and use a specific exception:

+class MigrationError(Exception):
+    """Raised when migration encounters an error."""
+    pass
+
 def upgrade() -> None:
     ...
     if remaining_orphaned and remaining_orphaned > 0:
-        raise Exception(
+        raise MigrationError(
             f"Still have {remaining_orphaned} orphaned messages after migration"
         )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d4a4bf5 and 6517684.

📒 Files selected for processing (2)
  • migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (1 hunks)
  • src/models.py (3 hunks)
📓 Path-based instructions (3)
src/**/*.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

src/**/*.py: Follow isort conventions with absolute imports preferred
snake_case for variables/functions; PascalCase for classes
Line length: 88 chars (Black compatible)
Explicit error handling with appropriate exception types
Docstrings: Use Google style docstrings
Use environment variables via python-dotenv (.env)
Use specific exception types (ResourceNotFoundException, ValidationException, etc.)
Proper logging with context instead of print statements

Files:

  • src/models.py
src/models.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

src/models.py: Use explicit type hints with SQLAlchemy mapped_column annotations
All tables use text IDs (nanoid format) as primary keys
Composite foreign keys for multi-tenant relationships
Feature flags on workspace, peer, and session levels
JSONB metadata fields for extensibility
HNSW indexes for vector similarity search

Files:

  • src/models.py
src/{models,crud}.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

Token counting on messages for usage tracking

Files:

  • src/models.py
🧠 Learnings (3)
📓 Common learnings
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:918-942
Timestamp: 2025-06-19T14:32:02.934Z
Learning: The queue and active_queue_sessions tables in the migration script `migrations/versions/d429de0e5338_adopt_peer_paradigm.py` are expected to remain small, so per-row updates are acceptable and performance optimizations like set-based updates are not necessary.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:188-194
Timestamp: 2025-06-18T15:58:51.202Z
Learning: In the migration file `migrations/versions/d429de0e5338_adopt_peer_paradigm.py`, the team has explicitly decided to accept SQL injection risks from f-string interpolation of schema names, despite the security concerns around DDL injection via the DATABASE_SCHEMA environment variable.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script `migrations/versions/d429de0e5338_adopt_peer_paradigm.py` properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.
Learnt from: dr-frmr
PR: plastic-labs/honcho#106
File: migrations/versions/a1b2c3d4e5f6_initial_schema.py:172-179
Timestamp: 2025-05-13T15:48:42.783Z
Learning: The `queue` and `active_queue_sessions` tables in the database schema reference `sessions.id` (numeric) rather than `sessions.public_id` (text) because they are internal-only tables not exposed through the API, where performance considerations outweigh consistent identifier usage.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#144
File: src/crud.py:1250-1250
Timestamp: 2025-06-26T18:39:54.942Z
Learning: Rajat-Ahuja1997 is comfortable exposing token counts in MessageCreate schema as a public property when it improves code maintainability and avoids type checking errors.
src/models.py (7)

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : All tables use text IDs (nanoid format) as primary keys

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : Use explicit type hints with SQLAlchemy mapped_column annotations

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : Feature flags on workspace, peer, and session levels

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.

Learnt from: Rajat-Ahuja1997
PR: #166
File: migrations/versions/05486ce795d5_make_session_name_required_on_messages.py:60-114
Timestamp: 2025-07-21T20:57:42.135Z
Learning: Peer names in the Honcho codebase are restricted to the pattern r"^[a-zA-Z0-9_-]+$" (same as session names), which prevents SQL injection and JSON syntax errors when peer names are interpolated into SQL queries and JSON strings during migrations.

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : Composite foreign keys for multi-tenant relationships

migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (10)

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:918-942
Timestamp: 2025-06-19T14:32:02.934Z
Learning: The queue and active_queue_sessions tables in the migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py are expected to remain small, so per-row updates are acceptable and performance optimizations like set-based updates are not necessary.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:188-194
Timestamp: 2025-06-18T15:58:51.202Z
Learning: In the migration file migrations/versions/d429de0e5338_adopt_peer_paradigm.py, the team has explicitly decided to accept SQL injection risks from f-string interpolation of schema names, despite the security concerns around DDL injection via the DATABASE_SCHEMA environment variable.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.

Learnt from: Rajat-Ahuja1997
PR: #166
File: migrations/versions/05486ce795d5_make_session_name_required_on_messages.py:60-114
Timestamp: 2025-07-21T20:57:42.135Z
Learning: Peer names in the Honcho codebase are restricted to the pattern r"^[a-zA-Z0-9_-]+$" (same as session names), which prevents SQL injection and JSON syntax errors when peer names are interpolated into SQL queries and JSON strings during migrations.

Learnt from: dr-frmr
PR: #131
File: tests/test_schema_validations.py:76-80
Timestamp: 2025-06-17T21:40:26.817Z
Learning: In the MessageCreate schema, peer_name is the actual field name with "peer_id" as an alias, so when constructing MessageCreate(peer_id="value"), it sets the peer_name attribute to "value".

Learnt from: dr-frmr
PR: #131
File: src/routers/sessions.py:206-213
Timestamp: 2025-06-18T20:42:06.458Z
Learning: The get_or_create_session function in this codebase is designed to handle both session creation and adding peers to existing sessions. When called with peers, it will add those peers to an existing session rather than creating a duplicate session.

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/**/*.py : Explicit error handling with appropriate exception types

Learnt from: Rajat-Ahuja1997
PR: #131
File: src/crud.py:503-505
Timestamp: 2025-06-18T14:50:59.967Z
Learning: The Honcho project prefers upsert behavior for update operations across all resources (sessions, peers, workspaces). Update operations should create the resource if it doesn't exist rather than failing fast. This is an explicit design decision that differs from typical REST semantics but provides a more forgiving API experience.

Learnt from: Rajat-Ahuja1997
PR: #144
File: src/crud.py:1250-1250
Timestamp: 2025-06-26T18:39:54.942Z
Learning: Rajat-Ahuja1997 is comfortable exposing token counts in MessageCreate schema as a public property when it improves code maintainability and avoids type checking errors.

🪛 Ruff (0.12.2)
migrations/versions/05486ce795d5_make_session_name_required_on_messages.py

42-42: Possible SQL injection vector through string-based query construction

(S608)


42-42: Trailing comma missing

Add trailing comma

(COM812)


50-54: Possible SQL injection vector through string-based query construction

(S608)


54-54: Trailing comma missing

Add trailing comma

(COM812)


60-60: Trailing comma missing

Add trailing comma

(COM812)


68-68: Trailing comma missing

Add trailing comma

(COM812)


73-86: Possible SQL injection vector through string-based query construction

(S608)


86-86: Trailing comma missing

Add trailing comma

(COM812)


91-103: Possible SQL injection vector through string-based query construction

(S608)


103-103: Trailing comma missing

Add trailing comma

(COM812)


106-106: Trailing comma missing

Add trailing comma

(COM812)


111-117: Possible SQL injection vector through string-based query construction

(S608)


117-117: Trailing comma missing

Add trailing comma

(COM812)


122-128: Possible SQL injection vector through string-based query construction

(S608)


128-128: Trailing comma missing

Add trailing comma

(COM812)


133-133: Possible SQL injection vector through string-based query construction

(S608)


133-133: Trailing comma missing

Add trailing comma

(COM812)


137-139: Create your own exception

(TRY002)


137-139: Avoid specifying long messages outside the exception class

(TRY003)


138-138: Trailing comma missing

Add trailing comma

(COM812)


148-148: Possible SQL injection vector through string-based query construction

(S608)


148-148: Trailing comma missing

Add trailing comma

(COM812)


149-149: Trailing comma missing

Add trailing comma

(COM812)


153-155: Create your own exception

(TRY002)


153-155: Avoid specifying long messages outside the exception class

(TRY003)


154-154: Trailing comma missing

Add trailing comma

(COM812)


167-167: Trailing comma missing

Add trailing comma

(COM812)


179-179: Trailing comma missing

Add trailing comma

(COM812)


185-185: Trailing comma missing

Add trailing comma

(COM812)

🧰 Additional context used
📓 Path-based instructions (3)
src/**/*.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

src/**/*.py: Follow isort conventions with absolute imports preferred
snake_case for variables/functions; PascalCase for classes
Line length: 88 chars (Black compatible)
Explicit error handling with appropriate exception types
Docstrings: Use Google style docstrings
Use environment variables via python-dotenv (.env)
Use specific exception types (ResourceNotFoundException, ValidationException, etc.)
Proper logging with context instead of print statements

Files:

  • src/models.py
src/models.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

src/models.py: Use explicit type hints with SQLAlchemy mapped_column annotations
All tables use text IDs (nanoid format) as primary keys
Composite foreign keys for multi-tenant relationships
Feature flags on workspace, peer, and session levels
JSONB metadata fields for extensibility
HNSW indexes for vector similarity search

Files:

  • src/models.py
src/{models,crud}.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

Token counting on messages for usage tracking

Files:

  • src/models.py
🧠 Learnings (3)
📓 Common learnings
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:918-942
Timestamp: 2025-06-19T14:32:02.934Z
Learning: The queue and active_queue_sessions tables in the migration script `migrations/versions/d429de0e5338_adopt_peer_paradigm.py` are expected to remain small, so per-row updates are acceptable and performance optimizations like set-based updates are not necessary.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:188-194
Timestamp: 2025-06-18T15:58:51.202Z
Learning: In the migration file `migrations/versions/d429de0e5338_adopt_peer_paradigm.py`, the team has explicitly decided to accept SQL injection risks from f-string interpolation of schema names, despite the security concerns around DDL injection via the DATABASE_SCHEMA environment variable.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script `migrations/versions/d429de0e5338_adopt_peer_paradigm.py` properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.
Learnt from: dr-frmr
PR: plastic-labs/honcho#106
File: migrations/versions/a1b2c3d4e5f6_initial_schema.py:172-179
Timestamp: 2025-05-13T15:48:42.783Z
Learning: The `queue` and `active_queue_sessions` tables in the database schema reference `sessions.id` (numeric) rather than `sessions.public_id` (text) because they are internal-only tables not exposed through the API, where performance considerations outweigh consistent identifier usage.
Learnt from: Rajat-Ahuja1997
PR: plastic-labs/honcho#144
File: src/crud.py:1250-1250
Timestamp: 2025-06-26T18:39:54.942Z
Learning: Rajat-Ahuja1997 is comfortable exposing token counts in MessageCreate schema as a public property when it improves code maintainability and avoids type checking errors.
src/models.py (7)

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : All tables use text IDs (nanoid format) as primary keys

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : Use explicit type hints with SQLAlchemy mapped_column annotations

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : Feature flags on workspace, peer, and session levels

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.

Learnt from: Rajat-Ahuja1997
PR: #166
File: migrations/versions/05486ce795d5_make_session_name_required_on_messages.py:60-114
Timestamp: 2025-07-21T20:57:42.135Z
Learning: Peer names in the Honcho codebase are restricted to the pattern r"^[a-zA-Z0-9_-]+$" (same as session names), which prevents SQL injection and JSON syntax errors when peer names are interpolated into SQL queries and JSON strings during migrations.

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/models.py : Composite foreign keys for multi-tenant relationships

migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (10)

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:36-77
Timestamp: 2025-06-19T14:07:32.309Z
Learning: The migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py properly handles foreign key constraints during table updates by explicitly dropping foreign key constraints before making schema changes, preventing constraint violations during the migration process.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:918-942
Timestamp: 2025-06-19T14:32:02.934Z
Learning: The queue and active_queue_sessions tables in the migration script migrations/versions/d429de0e5338_adopt_peer_paradigm.py are expected to remain small, so per-row updates are acceptable and performance optimizations like set-based updates are not necessary.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:188-194
Timestamp: 2025-06-18T15:58:51.202Z
Learning: In the migration file migrations/versions/d429de0e5338_adopt_peer_paradigm.py, the team has explicitly decided to accept SQL injection risks from f-string interpolation of schema names, despite the security concerns around DDL injection via the DATABASE_SCHEMA environment variable.

Learnt from: Rajat-Ahuja1997
PR: #131
File: migrations/versions/d429de0e5338_adopt_peer_paradigm.py:438-441
Timestamp: 2025-06-18T16:44:57.733Z
Learning: In the Honcho database schema, messages.app_id references workspaces.public_id (string nanoid), not workspaces.id (integer primary key). This means that during migrations when workspaces.public_id is renamed to workspaces.id, the foreign key relationship with messages.app_id remains valid.

Learnt from: Rajat-Ahuja1997
PR: #166
File: migrations/versions/05486ce795d5_make_session_name_required_on_messages.py:60-114
Timestamp: 2025-07-21T20:57:42.135Z
Learning: Peer names in the Honcho codebase are restricted to the pattern r"^[a-zA-Z0-9_-]+$" (same as session names), which prevents SQL injection and JSON syntax errors when peer names are interpolated into SQL queries and JSON strings during migrations.

Learnt from: dr-frmr
PR: #131
File: tests/test_schema_validations.py:76-80
Timestamp: 2025-06-17T21:40:26.817Z
Learning: In the MessageCreate schema, peer_name is the actual field name with "peer_id" as an alias, so when constructing MessageCreate(peer_id="value"), it sets the peer_name attribute to "value".

Learnt from: dr-frmr
PR: #131
File: src/routers/sessions.py:206-213
Timestamp: 2025-06-18T20:42:06.458Z
Learning: The get_or_create_session function in this codebase is designed to handle both session creation and adding peers to existing sessions. When called with peers, it will add those peers to an existing session rather than creating a duplicate session.

Learnt from: CR
PR: plastic-labs/honcho#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T14:51:09.913Z
Learning: Applies to src/**/*.py : Explicit error handling with appropriate exception types

Learnt from: Rajat-Ahuja1997
PR: #131
File: src/crud.py:503-505
Timestamp: 2025-06-18T14:50:59.967Z
Learning: The Honcho project prefers upsert behavior for update operations across all resources (sessions, peers, workspaces). Update operations should create the resource if it doesn't exist rather than failing fast. This is an explicit design decision that differs from typical REST semantics but provides a more forgiving API experience.

Learnt from: Rajat-Ahuja1997
PR: #144
File: src/crud.py:1250-1250
Timestamp: 2025-06-26T18:39:54.942Z
Learning: Rajat-Ahuja1997 is comfortable exposing token counts in MessageCreate schema as a public property when it improves code maintainability and avoids type checking errors.

🪛 Ruff (0.12.2)
migrations/versions/05486ce795d5_make_session_name_required_on_messages.py

42-42: Possible SQL injection vector through string-based query construction

(S608)


42-42: Trailing comma missing

Add trailing comma

(COM812)


50-54: Possible SQL injection vector through string-based query construction

(S608)


54-54: Trailing comma missing

Add trailing comma

(COM812)


60-60: Trailing comma missing

Add trailing comma

(COM812)


68-68: Trailing comma missing

Add trailing comma

(COM812)


73-86: Possible SQL injection vector through string-based query construction

(S608)


86-86: Trailing comma missing

Add trailing comma

(COM812)


91-103: Possible SQL injection vector through string-based query construction

(S608)


103-103: Trailing comma missing

Add trailing comma

(COM812)


106-106: Trailing comma missing

Add trailing comma

(COM812)


111-117: Possible SQL injection vector through string-based query construction

(S608)


117-117: Trailing comma missing

Add trailing comma

(COM812)


122-128: Possible SQL injection vector through string-based query construction

(S608)


128-128: Trailing comma missing

Add trailing comma

(COM812)


133-133: Possible SQL injection vector through string-based query construction

(S608)


133-133: Trailing comma missing

Add trailing comma

(COM812)


137-139: Create your own exception

(TRY002)


137-139: Avoid specifying long messages outside the exception class

(TRY003)


138-138: Trailing comma missing

Add trailing comma

(COM812)


148-148: Possible SQL injection vector through string-based query construction

(S608)


148-148: Trailing comma missing

Add trailing comma

(COM812)


149-149: Trailing comma missing

Add trailing comma

(COM812)


153-155: Create your own exception

(TRY002)


153-155: Avoid specifying long messages outside the exception class

(TRY003)


154-154: Trailing comma missing

Add trailing comma

(COM812)


167-167: Trailing comma missing

Add trailing comma

(COM812)


179-179: Trailing comma missing

Add trailing comma

(COM812)


185-185: Trailing comma missing

Add trailing comma

(COM812)

🔇 Additional comments (5)
src/models.py (3)

181-182: Comment accurately reflects the migration.

The updated comment properly documents that orphaned messages have been assigned to default sessions.


247-247: Consistent enforcement of non-nullable session_name.

Making session_name non-nullable in MessageEmbedding aligns with the same change in the Message model.


305-305: Increased collection name length limit.

The change from 512 to 1024 characters provides more flexibility for collection names.

migrations/versions/05486ce795d5_make_session_name_required_on_messages.py (2)

72-86: Good handling of potential conflicts.

The use of ON CONFLICT DO NOTHING properly handles cases where default sessions or session-peer associations already exist, making the migration idempotent.

Also applies to: 91-103


164-168: Proper constraint management.

Using constraint_exists before dropping constraints prevents errors if the constraint doesn't exist.

Also applies to: 182-186

Comment thread src/models.py Outdated
CheckConstraint("length(id) = 21", name="id_length"),
CheckConstraint("id ~ '^[A-Za-z0-9_-]+$'", name="id_format"),
CheckConstraint("length(name) <= 512", name="name_length"),
CheckConstraint("length(name) <= 1024", name="name_length"),

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

currently, if two peers (peer1, peer2) have names where len(peer1.name) + len(peer2.name) > 512, the collection will fail to build. the collection name length should be the max length of two peer names + 1 for some character to divide them, e.g. peer1.name_peer2.name

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should be 1025 then right?

Comment thread src/models.py Outdated
CheckConstraint("length(id) = 21", name="id_length"),
CheckConstraint("id ~ '^[A-Za-z0-9_-]+$'", name="id_format"),
CheckConstraint("length(name) <= 512", name="name_length"),
CheckConstraint("length(name) <= 1024", name="name_length"),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should be 1025 then right?

@VVoruganti VVoruganti merged commit ffaae28 into main Jul 22, 2025
10 of 11 checks passed
@VVoruganti VVoruganti deleted the rajat/DEV-994 branch July 22, 2025 19:25
@coderabbitai coderabbitai Bot mentioned this pull request Jul 23, 2025
ranc1 pushed a commit to ranc1/honcho that referenced this pull request Apr 16, 2026
…labs#166)

* model changes

* add migration

* fix migration note

* handle MessageEmbedding table

* fix json issue and make script idempotent

* add collections name_length change

* make 1025

* coderabbit
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.

2 participants