fix: serialize Hermes activity lifecycle
This commit is contained in:
2 files changed
+139
-44
No files matched your search
@@ -64,6 +64,10 @@ class _TurnState:
|
||||
tool_items: dict[str, str] = field(default_factory=dict)
|
||||
generic_activity_id: str | None = None
|
||||
generic_activity_detail: str | None = None
|
||||
generic_activity_lock: asyncio.Lock = field(
|
||||
default_factory=asyncio.Lock,
|
||||
repr=False,
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -728,64 +732,70 @@ class T3PlatformAdapter(BasePlatformAdapter):
|
||||
title="Hermes response",
|
||||
)
|
||||
)
|
||||
if turn.generic_activity_id is not None:
|
||||
async with turn.generic_activity_lock:
|
||||
if self._active_turns.get(turn.thread_id) is not turn:
|
||||
return
|
||||
if turn.generic_activity_id is not None:
|
||||
await self._send_frame(
|
||||
frame(
|
||||
"item.completed",
|
||||
threadId=turn.thread_id,
|
||||
sessionId=turn.session_id,
|
||||
turnId=turn.turn_id,
|
||||
itemId=turn.generic_activity_id,
|
||||
itemType="unknown",
|
||||
status="completed",
|
||||
title="Hermes activity",
|
||||
**(
|
||||
{"detail": turn.generic_activity_detail}
|
||||
if turn.generic_activity_detail
|
||||
else {}
|
||||
),
|
||||
)
|
||||
)
|
||||
await self._send_frame(
|
||||
frame(
|
||||
"item.completed",
|
||||
"turn.completed",
|
||||
threadId=turn.thread_id,
|
||||
sessionId=turn.session_id,
|
||||
turnId=turn.turn_id,
|
||||
itemId=turn.generic_activity_id,
|
||||
itemType="unknown",
|
||||
status="completed",
|
||||
title="Hermes activity",
|
||||
**(
|
||||
{"detail": turn.generic_activity_detail}
|
||||
if turn.generic_activity_detail
|
||||
else {}
|
||||
),
|
||||
state="completed",
|
||||
stopReason=None,
|
||||
)
|
||||
)
|
||||
await self._send_frame(
|
||||
frame(
|
||||
"turn.completed",
|
||||
threadId=turn.thread_id,
|
||||
sessionId=turn.session_id,
|
||||
turnId=turn.turn_id,
|
||||
state="completed",
|
||||
stopReason=None,
|
||||
)
|
||||
)
|
||||
self._active_turns.pop(turn.thread_id, None)
|
||||
self._active_turns.pop(turn.thread_id, None)
|
||||
await self._send_status()
|
||||
|
||||
async def _emit_generic_activity(self, turn: _TurnState, detail: str) -> None:
|
||||
if not detail:
|
||||
return
|
||||
normalized_detail = str(detail)[:2_000]
|
||||
if turn.generic_activity_detail == normalized_detail:
|
||||
return
|
||||
activity_id = turn.generic_activity_id
|
||||
if activity_id is None:
|
||||
activity_id = item_id()
|
||||
event_type = "item.started"
|
||||
else:
|
||||
event_type = "item.updated"
|
||||
await self._send_frame(
|
||||
frame(
|
||||
event_type,
|
||||
threadId=turn.thread_id,
|
||||
sessionId=turn.session_id,
|
||||
turnId=turn.turn_id,
|
||||
itemId=activity_id,
|
||||
itemType="unknown",
|
||||
status="inProgress",
|
||||
title="Hermes activity",
|
||||
detail=normalized_detail,
|
||||
async with turn.generic_activity_lock:
|
||||
if self._active_turns.get(turn.thread_id) is not turn:
|
||||
return
|
||||
if turn.generic_activity_detail == normalized_detail:
|
||||
return
|
||||
activity_id = turn.generic_activity_id
|
||||
if activity_id is None:
|
||||
activity_id = item_id()
|
||||
event_type = "item.started"
|
||||
else:
|
||||
event_type = "item.updated"
|
||||
await self._send_frame(
|
||||
frame(
|
||||
event_type,
|
||||
threadId=turn.thread_id,
|
||||
sessionId=turn.session_id,
|
||||
turnId=turn.turn_id,
|
||||
itemId=activity_id,
|
||||
itemType="unknown",
|
||||
status="inProgress",
|
||||
title="Hermes activity",
|
||||
detail=normalized_detail,
|
||||
)
|
||||
)
|
||||
)
|
||||
turn.generic_activity_id = activity_id
|
||||
turn.generic_activity_detail = normalized_detail
|
||||
turn.generic_activity_id = activity_id
|
||||
turn.generic_activity_detail = normalized_detail
|
||||
|
||||
def emit_tool_started(
|
||||
self,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import dataclasses
|
||||
import enum
|
||||
import importlib.util
|
||||
@@ -397,6 +398,90 @@ class AdapterTests(unittest.IsolatedAsyncioTestCase):
|
||||
self.assertEqual(updated["itemId"], started["itemId"])
|
||||
self.assertEqual(updated["detail"], "Running tests")
|
||||
|
||||
async def test_concurrent_generic_activity_updates_share_one_lifecycle(self):
|
||||
await self._start_turn("thread-activity-concurrent", "turn-activity-concurrent")
|
||||
turn = self.adapter._active_turns["thread-activity-concurrent"]
|
||||
original_send = self.connection.send
|
||||
first_send_started = asyncio.Event()
|
||||
release_first_send = asyncio.Event()
|
||||
|
||||
async def block_first_activity_send(message):
|
||||
if message["type"] == "item.started" and not first_send_started.is_set():
|
||||
first_send_started.set()
|
||||
await release_first_send.wait()
|
||||
await original_send(message)
|
||||
|
||||
self.connection.send = block_first_activity_send
|
||||
first_update = asyncio.create_task(
|
||||
self.adapter._emit_generic_activity(turn, "Reading repository")
|
||||
)
|
||||
await first_send_started.wait()
|
||||
second_update = asyncio.create_task(
|
||||
self.adapter._emit_generic_activity(turn, "Running tests")
|
||||
)
|
||||
await asyncio.sleep(0)
|
||||
release_first_send.set()
|
||||
await asyncio.gather(first_update, second_update)
|
||||
|
||||
activity_frames = [
|
||||
message
|
||||
for message in self.connection.messages
|
||||
if message["type"] in {"item.started", "item.updated"}
|
||||
]
|
||||
self.assertEqual(
|
||||
[message["type"] for message in activity_frames],
|
||||
["item.started", "item.updated"],
|
||||
)
|
||||
self.assertEqual(activity_frames[0]["itemId"], activity_frames[1]["itemId"])
|
||||
self.assertEqual(turn.generic_activity_id, activity_frames[0]["itemId"])
|
||||
self.assertEqual(turn.generic_activity_detail, "Running tests")
|
||||
|
||||
async def test_turn_completion_waits_for_in_flight_generic_activity_update(self):
|
||||
await self._start_turn("thread-activity-complete", "turn-activity-complete")
|
||||
turn = self.adapter._active_turns["thread-activity-complete"]
|
||||
await self.adapter._emit_generic_activity(turn, "Reading repository")
|
||||
activity_id = turn.generic_activity_id
|
||||
original_send = self.connection.send
|
||||
update_send_started = asyncio.Event()
|
||||
release_update_send = asyncio.Event()
|
||||
|
||||
async def block_activity_update(message):
|
||||
if message["type"] == "item.updated":
|
||||
update_send_started.set()
|
||||
await release_update_send.wait()
|
||||
await original_send(message)
|
||||
|
||||
self.connection.send = block_activity_update
|
||||
in_flight_update = asyncio.create_task(
|
||||
self.adapter._emit_generic_activity(turn, "Running tests")
|
||||
)
|
||||
await update_send_started.wait()
|
||||
completion = asyncio.create_task(self.adapter._complete_turn(turn))
|
||||
await asyncio.sleep(0)
|
||||
self.assertFalse(completion.done())
|
||||
|
||||
release_update_send.set()
|
||||
await asyncio.gather(in_flight_update, completion)
|
||||
|
||||
lifecycle_frames = [
|
||||
message
|
||||
for message in self.connection.messages
|
||||
if message["type"]
|
||||
in {"item.started", "item.updated", "item.completed", "turn.completed"}
|
||||
]
|
||||
self.assertEqual(
|
||||
[message["type"] for message in lifecycle_frames],
|
||||
["item.started", "item.updated", "item.completed", "turn.completed"],
|
||||
)
|
||||
self.assertTrue(
|
||||
all(
|
||||
message["itemId"] == activity_id
|
||||
for message in lifecycle_frames
|
||||
if message["type"].startswith("item.")
|
||||
)
|
||||
)
|
||||
self.assertNotIn("thread-activity-complete", self.adapter._active_turns)
|
||||
|
||||
async def test_exact_t3_home_channel_notice_is_suppressed(self):
|
||||
await self._start_turn("thread-notice", "turn-notice")
|
||||
content_start = len(self.connection.messages)
|
||||
|
||||
Reference in new issue
Block a user