Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion build_scripts/check_sqlserver_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from contextlib import contextmanager

import sqlalchemy as sa
from alembic import command
from alembic.script import ScriptDirectory

from pyrit.memory.migration import (
Expand Down Expand Up @@ -46,13 +47,19 @@ def _temporary_database() -> Generator[sa.Engine, None, None]:


def main() -> None:
"""Apply all migrations to a fresh database and validate the resulting schema."""
"""Apply all migrations to a fresh database, round-trip the newest one, and validate the schema."""
with _temporary_database() as engine:
with engine.connect() as connection:
if sa.inspect(connection).get_table_names():
raise RuntimeError("SQL Server migration validation requires a fresh database.")
run_schema_migrations(engine=engine)
check_schema_migrations(engine=engine)
# Round-trip the newest migration so its SQL Server downgrade runs too.
with engine.begin() as connection:
config = _make_config(connection=connection)
command.downgrade(config, "-1")
command.upgrade(config, "head")
check_schema_migrations(engine=engine)
with engine.connect() as connection:
heads = ScriptDirectory.from_config(_make_config(connection=connection)).get_heads()
versions = (
Expand Down
8 changes: 8 additions & 0 deletions pyrit/backend/services/target_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ async def detect_capabilities_async(self, *, target_registry_name: str, expected
try:
results.append(await self._probe_capabilities_async(source))
except (CapabilityDiscoveryError, ValueError) as exc:
logger.warning(
"Capability detection failed for target %r (child %d, %s): %s",
target_registry_name,
index + 1,
type(source).__name__,
exc,
exc_info=exc,
)
if isinstance(target, RoundRobinTarget):
error_type = (
CapabilityDiscoveryError if isinstance(exc, CapabilityDiscoveryError) else ValueError
Expand Down
138 changes: 138 additions & 0 deletions pyrit/memory/alembic/versions/0d3592c62ad8_widen_conversation_ids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

"""
Store conversation ids up to 128 characters in every table.

``Conversations.conversation_id`` and ``AttackResultEntries.conversation_id`` were
``VARCHAR(36)``, sized for a bare UUID, while ``PromptMemoryEntries`` already stores
128. Conversation ids are free-form, so SQL Server rejected longer ids such as the
capability-discovery probes' ``capability-probe-<uuid>``. SQLite does not enforce
string lengths; its declared types are updated so the schema matches the models.

Revision ID: 0d3592c62ad8
Revises: 3e55dd099e38
Create Date: 2026-11-20 18:00:00.000000
"""

import re
from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "0d3592c62ad8"
down_revision: str | Sequence[str] | None = "3e55dd099e38"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None

_TABLES = ("Conversations", "AttackResultEntries")

# SQL Server cannot alter a primary-key column in place, so the key is dropped and
# re-created with its original name and clustering.
_RESIZE_CONVERSATION_KEY_SQL = """
DECLARE @pk sysname, @pk_type nvarchar(60), @sql nvarchar(max);
SELECT @pk = key_constraint.name, @pk_type = idx.type_desc
FROM sys.key_constraints AS key_constraint
JOIN sys.indexes AS idx
ON idx.object_id = key_constraint.parent_object_id AND idx.index_id = key_constraint.unique_index_id
WHERE key_constraint.parent_object_id = OBJECT_ID(N'Conversations') AND key_constraint.type = 'PK';
IF @pk IS NULL THROW 50000, 'Conversations primary key is missing.', 1;
SET @sql = N'ALTER TABLE [Conversations] DROP CONSTRAINT ' + QUOTENAME(@pk);
EXEC sp_executesql @sql;
ALTER TABLE [Conversations] ALTER COLUMN [conversation_id] VARCHAR({length}) NOT NULL;
SET @sql = N'ALTER TABLE [Conversations] ADD CONSTRAINT ' + QUOTENAME(@pk) + N' PRIMARY KEY '
+ @pk_type + N' ([conversation_id])';
EXEC sp_executesql @sql;
"""

_REJECT_LONG_CONVERSATION_IDS_SQL = """
IF EXISTS (SELECT 1 FROM [Conversations] WHERE DATALENGTH([conversation_id]) > 36)
OR EXISTS (SELECT 1 FROM [AttackResultEntries] WHERE DATALENGTH([conversation_id]) > 36)
THROW 50000, 'Conversation ids longer than 36 characters are stored; earlier releases cannot hold them.', 1;
"""

# SQL Server widens an indexed VARCHAR in place but cannot narrow one, so the
# downgrade re-creates the AttackResultEntries indexes that cover conversation_id.
_ATTACK_RESULT_INDEXES = (
("ix_AttackResultEntries_conversation_timestamp_id", ["conversation_id", "timestamp", "id"], []),
("ix_AttackResultEntries_operator_timestamp_id", ["operator", "timestamp", "id"], ["conversation_id"]),
("ix_AttackResultEntries_operation_timestamp_id", ["operation", "timestamp", "id"], ["conversation_id"]),
)


def upgrade() -> None:
"""Widen conversation ids to match ``PromptMemoryEntries``."""
if op.get_bind().dialect.name == "mssql":
op.execute(sa.text(_RESIZE_CONVERSATION_KEY_SQL.format(length=128)))
op.execute(sa.text("ALTER TABLE [AttackResultEntries] ALTER COLUMN [conversation_id] VARCHAR(128) NOT NULL"))
return
_set_sqlite_declared_length(old_length=36, new_length=128)


def downgrade() -> None:
"""
Restore the 36-character columns earlier releases' schema checks expect.

SQL Server refuses when longer ids are stored, since narrowing would lose them.
SQLite never enforced the length, so only its declaration is restored.
"""
if op.get_bind().dialect.name == "sqlite":
_set_sqlite_declared_length(old_length=128, new_length=36)
return
op.execute(sa.text(_REJECT_LONG_CONVERSATION_IDS_SQL))
op.execute(sa.text(_RESIZE_CONVERSATION_KEY_SQL.format(length=36)))
for name, _, _ in _ATTACK_RESULT_INDEXES:
op.drop_index(name, table_name="AttackResultEntries")
op.alter_column(
"AttackResultEntries",
"conversation_id",
existing_type=sa.String(length=128),
type_=sa.String(length=36),
existing_nullable=False,
)
for name, columns, include in _ATTACK_RESULT_INDEXES:
op.create_index(name, "AttackResultEntries", columns, mssql_include=include)


def _set_sqlite_declared_length(*, old_length: int, new_length: int) -> None:
"""
Change the declared ``conversation_id`` length without rebuilding either table.

SQLite ignores the length and stores the same TEXT values either way, so this is
the schema-only edit SQLite documents for changes that leave stored content valid
(https://www.sqlite.org/lang_altertable.html#otheralter). A rebuild would have to
re-create ``AttackResultEntries``' generated analytics column and would rewrite
its column-level foreign keys, which earlier downgrades drop in place.

Raises:
RuntimeError: If a table does not declare the expected column exactly once,
or the database fails its integrity check afterwards.
"""
bind = op.get_bind()
pattern = re.compile(rf"(\bconversation_id\s+)VARCHAR\({old_length}\)", re.IGNORECASE)
replacements: dict[str, str] = {}
for table_name in _TABLES:
sql = bind.execute(
sa.text("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = :name"), {"name": table_name}
).scalar_one()
updated, count = pattern.subn(rf"\g<1>VARCHAR({new_length})", sql)
if count != 1:
raise RuntimeError(f"{table_name} does not declare conversation_id as VARCHAR({old_length}).")
replacements[table_name] = updated
schema_version = bind.execute(sa.text("PRAGMA schema_version")).scalar_one()
bind.execute(sa.text("PRAGMA writable_schema = ON"))
try:
for table_name, sql in replacements.items():
bind.execute(
sa.text("UPDATE sqlite_master SET sql = :sql WHERE type = 'table' AND name = :name"),
{"sql": sql, "name": table_name},
)
# A new schema version makes every connection reload the edited definitions.
bind.execute(sa.text(f"PRAGMA schema_version = {int(schema_version) + 1}"))
finally:
bind.execute(sa.text("PRAGMA writable_schema = OFF"))
result = bind.execute(sa.text("PRAGMA integrity_check")).scalar_one()
if result != "ok":
raise RuntimeError(f"SQLite integrity check failed after widening conversation ids: {result}")
39 changes: 34 additions & 5 deletions pyrit/memory/memory_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
Mapped,
mapped_column,
relationship,
validates,
)
from sqlalchemy.types import Uuid

Expand Down Expand Up @@ -98,9 +99,35 @@

# Default pyrit_version for database records created before version tracking was added
LEGACY_PYRIT_VERSION = "<0.10.0"

#: Longest conversation_id any table stores. Conversation ids are free-form
#: caller-supplied strings, not necessarily UUIDs, so every table that stores one
#: uses this bound. SQL Server enforces it; SQLite does not, so each entry's
#: ``conversation_id`` validator checks it on every assignment, including updates.
CONVERSATION_ID_MAX_LENGTH = 128
_LEGACY_UNSUPPORTED_ATTACK_IDENTIFIER_KEY = "__pyrit_legacy_unsupported_identifier__"


def _bounded_conversation_id( # pyrit-keyword-only-exempt SQLAlchemy @validates callback
_entry: object, _key: str, conversation_id: str | None
) -> str | None:
"""
Reject a conversation_id that no database column can store.

Returns:
str | None: The unchanged id.

Raises:
ValueError: If the id is longer than ``CONVERSATION_ID_MAX_LENGTH``.
"""
if conversation_id is not None and len(conversation_id) > CONVERSATION_ID_MAX_LENGTH:
raise ValueError(
f"conversation_id is {len(conversation_id)} characters; the most memory stores is "
f"{CONVERSATION_ID_MAX_LENGTH}: {conversation_id[:CONVERSATION_ID_MAX_LENGTH]}..."
)
return conversation_id


def _load_identifier(
stored: dict[str, Any] | None,
*,
Expand Down Expand Up @@ -637,9 +664,9 @@ class PromptMemoryEntry(Base):
)
id = mapped_column(CustomUUID, nullable=False, primary_key=True)
role: Mapped[ChatMessageRole] = mapped_column(String, nullable=False)
# Bounded so SQL Server accepts it as an index key. 128 rather than 36 because
# conversation_id is a free-form caller-supplied string, not necessarily a UUID.
conversation_id = mapped_column(String(128), nullable=False)
# Bounded so SQL Server accepts it as an index key.
conversation_id = mapped_column(String(CONVERSATION_ID_MAX_LENGTH), nullable=False)
_validate_conversation_id = validates("conversation_id")(_bounded_conversation_id)
sequence = mapped_column(INTEGER, nullable=False)
timestamp = mapped_column(UTCDateTime, nullable=False)
prompt_metadata: Mapped[dict[str, str | int]] = mapped_column(JSON)
Expand Down Expand Up @@ -1389,7 +1416,8 @@ class ConversationEntry(Base):
__tablename__ = "Conversations"
__table_args__ = {"extend_existing": True}

conversation_id = mapped_column(String(36), primary_key=True, nullable=False)
conversation_id = mapped_column(String(CONVERSATION_ID_MAX_LENGTH), primary_key=True, nullable=False)
_validate_conversation_id = validates("conversation_id")(_bounded_conversation_id)
target_identifier: Mapped[dict[str, str] | None] = mapped_column(JSON, nullable=True)
#: Foreign key to the content-addressed ``TargetIdentifiers`` row. Nullable:
#: a conversation may be registered without a known target.
Expand Down Expand Up @@ -2293,7 +2321,8 @@ class AttackResultEntry(Base):
{"extend_existing": True},
)
id = mapped_column(CustomUUID, nullable=False, primary_key=True)
conversation_id = mapped_column(String(36), nullable=False)
conversation_id = mapped_column(String(CONVERSATION_ID_MAX_LENGTH), nullable=False)
_validate_conversation_id = validates("conversation_id")(_bounded_conversation_id)
objective = mapped_column(Unicode, nullable=False)
atomic_attack_identifier: Mapped[dict[str, Any] | None] = mapped_column(JSON, nullable=True)
atomic_attack_identifier_hash: Mapped[str | None] = mapped_column(
Expand Down
Loading
Loading