mirror of
https://github.com/Sea-Haven-Industries/open-swe.git
synced 2026-10-01 07:23:14 +00:00
refactor: extract recent comment logic and add tests
This commit is contained in:
parent
f4b9b525fb
commit
0e9f830f64
3 changed files with 69 additions and 19 deletions
40
apps/agent/agent/utils/comments.py
Normal file
40
apps/agent/agent/utils/comments.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
"""Helpers for Linear comment processing."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Sequence
|
||||
|
||||
|
||||
def get_recent_comments(
|
||||
comments: Sequence[dict[str, Any]], bot_message_prefixes: Sequence[str]
|
||||
) -> list[dict[str, Any]] | None:
|
||||
"""Return user comments since the last agent response, or None if none.
|
||||
|
||||
Args:
|
||||
comments: Linear issue comments.
|
||||
bot_message_prefixes: Prefixes that identify agent/bot responses.
|
||||
|
||||
Returns:
|
||||
Chronological list of comments since the last agent response, or None.
|
||||
"""
|
||||
if not comments:
|
||||
return None
|
||||
|
||||
sorted_comments = sorted(
|
||||
comments,
|
||||
key=lambda comment: comment.get("createdAt", ""),
|
||||
reverse=True,
|
||||
)
|
||||
|
||||
recent_user_comments: list[dict[str, Any]] = []
|
||||
for comment in sorted_comments:
|
||||
body = comment.get("body", "")
|
||||
if any(body.startswith(prefix) for prefix in bot_message_prefixes):
|
||||
break # Everything after this is from before the last agent response
|
||||
recent_user_comments.append(comment)
|
||||
|
||||
if not recent_user_comments:
|
||||
return None
|
||||
|
||||
recent_user_comments.reverse()
|
||||
return recent_user_comments
|
||||
|
|
@ -15,6 +15,7 @@ from langgraph_sdk import get_client
|
|||
|
||||
# Local import for encryption
|
||||
from .encryption import encrypt_token
|
||||
from .utils.comments import get_recent_comments
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -586,25 +587,7 @@ async def process_linear_issue( # noqa: PLR0912, PLR0915
|
|||
)
|
||||
|
||||
if comments:
|
||||
# Sort newest-first so we can collect forward until we hit the last agent response
|
||||
sorted_comments = sorted(
|
||||
comments,
|
||||
key=lambda c: c.get("createdAt", ""),
|
||||
reverse=True,
|
||||
)
|
||||
|
||||
# Collect user comments since the last agent response.
|
||||
# Iterate newest-first and stop as soon as we see a bot message.
|
||||
recent_user_comments = []
|
||||
for comment in sorted_comments:
|
||||
body = comment.get("body", "")
|
||||
if any(body.startswith(prefix) for prefix in bot_message_prefixes):
|
||||
break # Everything after this is from before the last agent response
|
||||
recent_user_comments.append(comment)
|
||||
|
||||
# Reverse to restore chronological (oldest-first) order for the prompt
|
||||
recent_user_comments.reverse()
|
||||
|
||||
recent_user_comments = get_recent_comments(comments, bot_message_prefixes)
|
||||
if recent_user_comments:
|
||||
comments_text = "\n\n## Comments:\n"
|
||||
for comment in recent_user_comments:
|
||||
|
|
|
|||
27
apps/agent/tests/test_recent_comments.py
Normal file
27
apps/agent/tests/test_recent_comments.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
from agent.utils.comments import get_recent_comments
|
||||
|
||||
|
||||
def test_get_recent_comments_returns_none_for_empty() -> None:
|
||||
assert get_recent_comments([], ("🤖 **Agent Response**",)) is None
|
||||
|
||||
|
||||
def test_get_recent_comments_returns_none_when_newest_is_bot_message() -> None:
|
||||
comments = [
|
||||
{"body": "🤖 **Agent Response** latest", "createdAt": "2024-01-03T00:00:00Z"},
|
||||
{"body": "user comment", "createdAt": "2024-01-02T00:00:00Z"},
|
||||
]
|
||||
|
||||
assert get_recent_comments(comments, ("🤖 **Agent Response**",)) is None
|
||||
|
||||
|
||||
def test_get_recent_comments_collects_since_last_bot_message() -> None:
|
||||
comments = [
|
||||
{"body": "first user", "createdAt": "2024-01-01T00:00:00Z"},
|
||||
{"body": "🤖 **Agent Response** done", "createdAt": "2024-01-02T00:00:00Z"},
|
||||
{"body": "follow up 1", "createdAt": "2024-01-03T00:00:00Z"},
|
||||
{"body": "follow up 2", "createdAt": "2024-01-04T00:00:00Z"},
|
||||
]
|
||||
|
||||
result = get_recent_comments(comments, ("🤖 **Agent Response**",))
|
||||
assert result is not None
|
||||
assert [comment["body"] for comment in result] == ["follow up 1", "follow up 2"]
|
||||
Loading…
Add table
Reference in a new issue