-
Notifications
You must be signed in to change notification settings - Fork 582
feat(api): rebase JP's session turns/streams backend onto v0.105.5 #5375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
api/oss/databases/postgres/migrations/core_oss/versions/oss000000014_add_session_turns.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| """add session_turns table | ||
|
|
||
| Revision ID: oss000000014 | ||
| Revises: oss000000013 | ||
| Create Date: 2026-07-17 00:00:00.000000 | ||
|
|
||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
| from sqlalchemy.dialects import postgresql | ||
|
|
||
| revision: str = "oss000000014" | ||
| down_revision: Union[str, None] = "oss000000013" | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.create_table( | ||
| "session_turns", | ||
| sa.Column("id", sa.UUID(as_uuid=True), nullable=False), | ||
| sa.Column("project_id", sa.UUID(as_uuid=True), nullable=False), | ||
| sa.Column("session_id", sa.String(), nullable=False), | ||
| sa.Column("stream_id", sa.UUID(as_uuid=True), nullable=False), | ||
| sa.Column("turn_index", sa.Integer(), nullable=False), | ||
| sa.Column("harness_kind", sa.String(), nullable=False), | ||
| sa.Column("agent_session_id", sa.String(), nullable=True), | ||
| sa.Column("sandbox_id", sa.String(), nullable=True), | ||
| sa.Column( | ||
| "references", | ||
| postgresql.JSONB(none_as_null=True), | ||
| nullable=True, | ||
| ), | ||
| sa.Column("trace_id", sa.UUID(as_uuid=True), nullable=True), | ||
| # OTel span id: 64-bit / 16 hex chars, NOT a UUID (128-bit / 32 hex). Text, not UUID. | ||
| sa.Column("span_id", sa.String(), nullable=True), | ||
| sa.Column("start_time", sa.TIMESTAMP(timezone=True), nullable=True), | ||
| sa.Column("end_time", sa.TIMESTAMP(timezone=True), nullable=True), | ||
| sa.Column( | ||
| "created_at", | ||
| sa.TIMESTAMP(timezone=True), | ||
| server_default=sa.func.current_timestamp(), | ||
| nullable=True, | ||
| ), | ||
| sa.Column("updated_at", sa.TIMESTAMP(timezone=True), nullable=True), | ||
| sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True), | ||
| sa.Column("created_by_id", sa.UUID(as_uuid=True), nullable=True), | ||
| sa.Column("updated_by_id", sa.UUID(as_uuid=True), nullable=True), | ||
| sa.Column("deleted_by_id", sa.UUID(as_uuid=True), nullable=True), | ||
| sa.ForeignKeyConstraint( | ||
| ["project_id"], | ||
| ["projects.id"], | ||
| ondelete="CASCADE", | ||
| ), | ||
| sa.ForeignKeyConstraint( | ||
| ["project_id", "stream_id"], | ||
| ["session_streams.project_id", "session_streams.id"], | ||
| ondelete="NO ACTION", | ||
| ), | ||
| sa.PrimaryKeyConstraint("project_id", "id"), | ||
| ) | ||
| op.create_index( | ||
| "ix_session_turns_project_id_session_id", | ||
| "session_turns", | ||
| ["project_id", "session_id"], | ||
| ) | ||
| op.create_index( | ||
| "ix_session_turns_project_id_session_id_turn_index", | ||
| "session_turns", | ||
| ["project_id", "session_id", "turn_index"], | ||
| unique=True, | ||
| ) | ||
| op.create_index( | ||
| "ix_session_turns_references", | ||
| "session_turns", | ||
| ["references"], | ||
| postgresql_using="gin", | ||
| postgresql_ops={"references": "jsonb_path_ops"}, | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_index( | ||
| "ix_session_turns_references", | ||
| table_name="session_turns", | ||
| ) | ||
| op.drop_index( | ||
| "ix_session_turns_project_id_session_id_turn_index", | ||
| table_name="session_turns", | ||
| ) | ||
| op.drop_index( | ||
| "ix_session_turns_project_id_session_id", | ||
| table_name="session_turns", | ||
| ) | ||
| op.drop_table("session_turns") | ||
33 changes: 33 additions & 0 deletions
33
...atabases/postgres/migrations/core_oss/versions/oss000000015_add_session_streams_header.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| """add name/description header to session_streams | ||
|
|
||
| Revision ID: oss000000015 | ||
| Revises: oss000000014 | ||
| Create Date: 2026-07-17 00:00:00.000000 | ||
|
|
||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
| revision: str = "oss000000015" | ||
| down_revision: Union[str, None] = "oss000000014" | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.add_column( | ||
| "session_streams", | ||
| sa.Column("name", sa.String(), nullable=True), | ||
| ) | ||
| op.add_column( | ||
| "session_streams", | ||
| sa.Column("description", sa.String(), nullable=True), | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_column("session_streams", "description") | ||
| op.drop_column("session_streams", "name") |
59 changes: 59 additions & 0 deletions
59
api/oss/databases/postgres/migrations/core_oss/versions/oss000000016_add_mounts_agent_id.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| """add mounts agent_id column, index, and backfill | ||
|
|
||
| Mirrors session_id: a bare, nullable varchar populated only for agent mounts | ||
| (session mounts stay agent_id-null), with a partial index for querying "mounts | ||
| for agent X" the same way session_id already supports "mounts for session X". | ||
|
|
||
| Backfill: mounts are core DB, so a data migration is allowed here (unlike the | ||
| tracing DB). Agent-mount slugs are minted as | ||
| `__ag__agent__<canonical_artifact_id>__<name>` (`mint_agent_slug`, | ||
| `core/mounts/service.py`) — the artifact id is the raw canonical (lowercase) | ||
| UUID, not a hash, so it is deterministically recoverable straight out of the | ||
| slug. Session-mount slugs (`__ag__session__<uuid5(session_id)>__<name>`) hash | ||
| the session id, so no session_id backfill is possible or attempted here. | ||
|
|
||
| Revision ID: oss000000016 | ||
| Revises: oss000000015 | ||
| Create Date: 2026-07-17 00:00:00.000000 | ||
|
|
||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
| revision: str = "oss000000016" | ||
| down_revision: Union[str, None] = "oss000000015" | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
| _AGENT_SLUG_PREFIX = "__ag__agent__" | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.add_column("mounts", sa.Column("agent_id", sa.String(), nullable=True)) | ||
|
|
||
| op.create_index( | ||
| "ix_mounts_project_id_agent_id", | ||
| "mounts", | ||
| ["project_id", "agent_id"], | ||
| unique=False, | ||
| postgresql_where=sa.text("agent_id IS NOT NULL"), | ||
| ) | ||
|
|
||
| conn = op.get_bind() | ||
| conn.execute( | ||
| sa.text( | ||
| f""" | ||
| UPDATE mounts | ||
| SET agent_id = split_part(substr(slug, {len(_AGENT_SLUG_PREFIX) + 1}), '__', 1) | ||
| WHERE left(slug, {len(_AGENT_SLUG_PREFIX)}) = '{_AGENT_SLUG_PREFIX}' | ||
| """ | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_index("ix_mounts_project_id_agent_id", table_name="mounts") | ||
| op.drop_column("mounts", "agent_id") |
61 changes: 61 additions & 0 deletions
61
api/oss/databases/postgres/migrations/core_oss/versions/oss000000017_drop_session_states.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """drop session_states table | ||
|
|
||
| Superseded by the session_streams header (name/description) — the /sessions/states/ | ||
| router now reads/writes the merged stream row via streams_service. The standalone | ||
| table, its DAO/service, and DBE are orphaned; drop the physical table. | ||
|
|
||
| Revision ID: oss000000017 | ||
| Revises: oss000000016 | ||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
| from sqlalchemy.dialects import postgresql | ||
|
|
||
|
|
||
| revision: str = "oss000000017" | ||
| down_revision: Union[str, None] = "oss000000016" | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.drop_table("session_states") | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.create_table( | ||
| "session_states", | ||
| sa.Column("project_id", sa.UUID(), nullable=False), | ||
| sa.Column("id", sa.UUID(), nullable=False), | ||
| sa.Column("session_id", sa.String(), nullable=False), | ||
| sa.Column("sandbox_id", sa.String(), nullable=True), | ||
| sa.Column( | ||
| "data", | ||
| postgresql.JSON(astext_type=sa.Text()), | ||
| nullable=True, | ||
| ), | ||
| sa.Column("flags", postgresql.JSONB(none_as_null=True), nullable=True), | ||
| sa.Column("tags", postgresql.JSONB(none_as_null=True), nullable=True), | ||
| sa.Column("meta", postgresql.JSON(none_as_null=True), nullable=True), | ||
| sa.Column( | ||
| "created_at", | ||
| sa.TIMESTAMP(timezone=True), | ||
| server_default=sa.text("CURRENT_TIMESTAMP"), | ||
| nullable=True, | ||
| ), | ||
| sa.Column("updated_at", sa.TIMESTAMP(timezone=True), nullable=True), | ||
| sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True), | ||
| sa.Column("created_by_id", sa.UUID(), nullable=True), | ||
| sa.Column("updated_by_id", sa.UUID(), nullable=True), | ||
| sa.Column("deleted_by_id", sa.UUID(), nullable=True), | ||
| sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"), | ||
| sa.PrimaryKeyConstraint("project_id", "id"), | ||
| sa.UniqueConstraint( | ||
| "project_id", | ||
| "session_id", | ||
| name="uq_session_states_project_session_id", | ||
| ), | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommend
unique=Trueon this index while the migration is still unreleased. Append-only turns are the concurrency substrate for everything multi-surface (two writers racing the same turn index must conflict cleanly, with the loser retrying as the next index), and today nothing prevents a duplicate (session_id, turn_index) pair, which would makelatest_turnreads ambiguous. As a bonus it also converts an at-least-once append retry that races its own success into a harmless conflict instead of a silent duplicate row. One-line change now; a data-dedup migration later.