-
Notifications
You must be signed in to change notification settings - Fork 705
feat: autonomous error tracking and memory management #932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,28 +2,49 @@ | |||||||||||||||||
|
|
||||||||||||||||||
| from epyxid import XID | ||||||||||||||||||
|
|
||||||||||||||||||
| from intentkit.core.agent_activity import create_agent_activity | ||||||||||||||||||
| from intentkit.core.chat import clear_thread_memory | ||||||||||||||||||
| from intentkit.core.client import execute_agent | ||||||||||||||||||
| from intentkit.models.agent_activity import AgentActivityCreate | ||||||||||||||||||
| from intentkit.models.chat import AuthorType, ChatMessageCreate | ||||||||||||||||||
|
|
||||||||||||||||||
| logger = logging.getLogger(__name__) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| async def run_autonomous_task( | ||||||||||||||||||
| agent_id: str, agent_owner: str, task_id: str, prompt: str | ||||||||||||||||||
| agent_id: str, | ||||||||||||||||||
| agent_owner: str, | ||||||||||||||||||
| task_id: str, | ||||||||||||||||||
| prompt: str, | ||||||||||||||||||
| has_memory: bool = True, | ||||||||||||||||||
| ): | ||||||||||||||||||
| """ | ||||||||||||||||||
| Run a specific autonomous task for an agent. | ||||||||||||||||||
|
|
||||||||||||||||||
| Args: | ||||||||||||||||||
| agent_id: The ID of the agent | ||||||||||||||||||
| agent_owner: The owner of the agent | ||||||||||||||||||
| task_id: The ID of the autonomous task | ||||||||||||||||||
| prompt: The autonomous prompt to execute | ||||||||||||||||||
| has_memory: Whether to retain conversation memory between runs. | ||||||||||||||||||
| If False, clears thread memory before execution. | ||||||||||||||||||
| """ | ||||||||||||||||||
| logger.info(f"Running autonomous task {task_id} for agent {agent_id}") | ||||||||||||||||||
|
|
||||||||||||||||||
| try: | ||||||||||||||||||
| # Run the autonomous action | ||||||||||||||||||
| chat_id = f"autonomous-{task_id}" | ||||||||||||||||||
|
|
||||||||||||||||||
| # Clear thread memory if has_memory is False | ||||||||||||||||||
| if not has_memory: | ||||||||||||||||||
| try: | ||||||||||||||||||
| _ = await clear_thread_memory(agent_id, chat_id) | ||||||||||||||||||
| logger.debug( | ||||||||||||||||||
| f"Cleared thread memory for task {task_id} (has_memory=False)" | ||||||||||||||||||
| ) | ||||||||||||||||||
| except Exception as e: | ||||||||||||||||||
| # Log the error but continue with execution | ||||||||||||||||||
| logger.warning(f"Failed to clear thread memory for task {task_id}: {e}") | ||||||||||||||||||
| message = ChatMessageCreate( | ||||||||||||||||||
| id=str(XID()), | ||||||||||||||||||
| agent_id=agent_id, | ||||||||||||||||||
|
|
@@ -43,7 +64,56 @@ async def run_autonomous_task( | |||||||||||||||||
| f"Task {task_id} completed: " + "\n".join(str(m) for m in resp), | ||||||||||||||||||
| extra={"aid": agent_id}, | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| # Check response and create error activity if needed | ||||||||||||||||||
| if not resp: | ||||||||||||||||||
| try: | ||||||||||||||||||
| activity = AgentActivityCreate( | ||||||||||||||||||
| agent_id=agent_id, | ||||||||||||||||||
| text="Unexpected result: empty response", | ||||||||||||||||||
| ) | ||||||||||||||||||
| _ = await create_agent_activity(activity) | ||||||||||||||||||
|
||||||||||||||||||
| except Exception as e: | ||||||||||||||||||
| logger.warning( | ||||||||||||||||||
| f"Failed to create error activity for task {task_id}: {e}", | ||||||||||||||||||
| extra={"aid": agent_id}, | ||||||||||||||||||
| ) | ||||||||||||||||||
| else: | ||||||||||||||||||
| last_msg = resp[-1] | ||||||||||||||||||
| error_text = None | ||||||||||||||||||
|
|
||||||||||||||||||
| if last_msg.author_type == AuthorType.AGENT: | ||||||||||||||||||
| pass # Success, do nothing | ||||||||||||||||||
| elif last_msg.author_type == AuthorType.SYSTEM: | ||||||||||||||||||
| error_text = f"Task execution error: {last_msg.message}" | ||||||||||||||||||
| else: | ||||||||||||||||||
|
Comment on lines
+85
to
+89
|
||||||||||||||||||
| if last_msg.author_type == AuthorType.AGENT: | |
| pass # Success, do nothing | |
| elif last_msg.author_type == AuthorType.SYSTEM: | |
| error_text = f"Task execution error: {last_msg.message}" | |
| else: | |
| if last_msg.author_type == AuthorType.SYSTEM: | |
| error_text = f"Task execution error: {last_msg.message}" | |
| elif last_msg.author_type != AuthorType.AGENT: |
Copilot
AI
Jan 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message "Unexpected return error" is vague and doesn't provide useful context about what went wrong. Consider including more details such as the actual author_type value that was encountered to help with debugging. For example: "Unexpected author type in response: {last_msg.author_type}"
| error_text = "Unexpected return error" | |
| error_text = f"Unexpected author type in response: {last_msg.author_type}" |
Copilot
AI
Jan 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The underscore assignment pattern is used here, but the return value from create_agent_activity is not used anywhere. While this is valid Python, it's more idiomatic to simply call the function without assignment when the return value is intentionally unused. Consider changing to: await create_agent_activity(activity)
Copilot
AI
Jan 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The underscore assignment pattern is used here, but the return value from create_agent_activity is not used anywhere. While this is valid Python, it's more idiomatic to simply call the function without assignment when the return value is intentionally unused. Consider changing to: await create_agent_activity(activity)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The underscore assignment pattern is used here, but the return value from clear_thread_memory is not used anywhere. While this is valid Python, it's more idiomatic to simply call the function without assignment when the return value is intentionally unused. Consider changing to: await clear_thread_memory(agent_id, chat_id)