fix: strip chatgpt transcript tags from agent messages

This commit is contained in:
Ousama Ben Younes 2026-07-10 23:13:03 +00:00
parent 8551339130
commit d317572f5b
2 changed files with 54 additions and 2 deletions

View file

@ -5,6 +5,7 @@ from __future__ import annotations
import asyncio
import json
import logging
import re
import tempfile
from dataclasses import dataclass, field
from pathlib import Path
@ -29,6 +30,22 @@ Status = Literal["running", "waiting", "completed", "stopped", "crashed", "faile
# on other agents is re-checked on a timer.
WaitKind = Literal["user", "agents", "stalled"]
_CHATGPT_TRANSCRIPT_TAGS = (
"analysis",
"assistant",
"channel",
"final",
"user",
)
_CHATGPT_TRANSCRIPT_TAG_RE = re.compile(
rf"</?(?:{'|'.join(_CHATGPT_TRANSCRIPT_TAGS)})\s*>",
flags=re.IGNORECASE,
)
def _strip_chatgpt_transcript_tags(content: str) -> str:
return _CHATGPT_TRANSCRIPT_TAG_RE.sub("", content)
@dataclass(slots=True)
class AgentRuntime:
@ -372,7 +389,7 @@ class AgentCoordinator:
await self._maybe_snapshot()
async def cancel_descendants(self, agent_id: str) -> None:
tasks = []
tasks: list[asyncio.Task[Any]] = []
async with self._lock:
for aid in reversed(self._subtree_order_locked(agent_id)):
task = self.runtimes.get(aid, AgentRuntime()).task
@ -437,7 +454,7 @@ class AgentCoordinator:
def _message_to_session_item(self, message: dict[str, Any]) -> TResponseInputItem:
sender = str(message.get("from", "unknown"))
content = str(message.get("content", ""))
content = _strip_chatgpt_transcript_tags(str(message.get("content", "")))
if sender == "user":
return cast("TResponseInputItem", {"role": "user", "content": content})
sender_name = self.names.get(sender, sender)

View file

@ -0,0 +1,35 @@
"""Tests for agent-to-session message conversion."""
from __future__ import annotations
from strix.core.agents import AgentCoordinator
CHATGPT_TRANSCRIPT_CONTENT = (
"<analysis>checked target</analysis>\n<channel>final</channel>\n<final>done</final>"
)
def test_message_to_session_item_strips_chatgpt_transcript_tags() -> None:
coordinator = AgentCoordinator()
coordinator.names["child"] = "Researcher"
item = coordinator._message_to_session_item(
{
"from": "child",
"type": "information",
"priority": "normal",
"content": CHATGPT_TRANSCRIPT_CONTENT,
}
)
content = str(item["content"])
assert "<analysis>" not in content
assert "</analysis>" not in content
assert "<channel>" not in content
assert "</channel>" not in content
assert "<final>" not in content
assert "</final>" not in content
assert "checked target" in content
assert "done" in content