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
18 changes: 15 additions & 3 deletions gateway/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -20539,19 +20539,31 @@ async def _rename_discord_auto_thread_for_session_title(
"relay" if use_connector_guard else "native",
thread_name,
)
rename_kwargs = (
{
"prefer_connector_created": True,
"parent_chat_id": parent_chat_id,
}
if use_connector_guard
else {"only_if_current_name": guard_name}
)
try:
renamed = await rename_thread(
target_thread_id,
thread_name,
prefer_connector_created=use_connector_guard,
only_if_current_name=guard_name,
parent_chat_id=parent_chat_id,
**rename_kwargs,
)
logger.info(
"discord auto-thread rename result: thread=%s applied=%s",
target_thread_id,
bool(renamed),
)
except TypeError:
logger.warning(
"Discord semantic thread rename raised TypeError (adapter=%s)",
type(adapter).__name__,
exc_info=True,
)
except Exception:
logger.debug("Failed to rename Discord auto-thread for generated session title", exc_info=True)

Expand Down
50 changes: 49 additions & 1 deletion tests/gateway/test_session_title_rename_lane.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import pytest

from gateway.config import Platform
from gateway.run import TurnRunner
from gateway.run import GatewayRunner, TurnRunner


def _attach(lane):
Expand Down Expand Up @@ -53,3 +53,51 @@ def test_the_rename_waits_for_the_model_title(lane):

callback("Fix flaky auth test", "llm")
assert renames == ["Fix flaky auth test"]


@pytest.mark.asyncio
async def test_native_thread_rename_passes_only_the_initial_name_guard():
"""The shared rename lane must honor the strict native adapter contract."""
calls: list[tuple[str, str, str | None]] = []

class StrictNativeAdapter:
async def rename_thread(
self,
thread_id: str,
name: str,
*,
only_if_current_name: str | None = None,
) -> bool:
calls.append((thread_id, name, only_if_current_name))
return True

class NativeRenameRunner:
_is_discord_auto_thread_lane = GatewayRunner._is_discord_auto_thread_lane
_sanitize_discord_thread_title = GatewayRunner._sanitize_discord_thread_title
_rename_discord_auto_thread_for_session_title = (
GatewayRunner._rename_discord_auto_thread_for_session_title
)

def __init__(self, adapter):
self.adapters = {Platform.DISCORD: adapter}

def _adapter_for_source(self, source):
return self.adapters[source.platform]

source = types.SimpleNamespace(
platform=Platform.DISCORD,
chat_id="999",
chat_type="thread",
thread_id="999",
auto_thread_created=True,
auto_thread_initial_name="Initial words",
)

runner = NativeRenameRunner(StrictNativeAdapter())
await runner._rename_discord_auto_thread_for_session_title(
source,
"session-1",
"Semantic Session Title",
)

assert calls == [("999", "Semantic Session Title", "Initial words")]
Loading