-
Notifications
You must be signed in to change notification settings - Fork 162
fix(email): allow move_to_label to restore inbox #3267
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
Open
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2c5e0ca
fix(email): allow move_to_label to restore inbox
mikemikimike 3f68d28
fix(email): restore inbox in batch label moves
mikemikimike 72e74aa
test(email): exercise batch inbox restoration through the tool
mikemikimike 5479085
fix(email): remove duplicate inbox changelog entries
mikemikimike File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
hub/agents/email/python/tests/test_move_to_label_inbox_2626.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| # Copyright(C) 2025-2026 Advanced Micro Devices, Inc. All rights reserved. | ||
| # SPDX-License-Identifier: MIT | ||
| """Regression tests for restoring a message with ``move_to_label`` (#2626).""" | ||
|
|
||
| import json | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
||
| from gaia_agent_email import action_store | ||
| from gaia_agent_email.tools.organize_tools import OrganizeToolsMixin, move_to_label_impl | ||
|
|
||
| from gaia.agents.base.tools import _TOOL_REGISTRY, get_tool_metadata | ||
| from gaia.database.mixin import DatabaseMixin | ||
|
|
||
|
|
||
| class _FakeMailbox: | ||
| def __init__(self): | ||
| self.messages = { | ||
| "outside": {"labelIds": ["Label_1"]}, | ||
| "inbox": {"labelIds": ["INBOX"]}, | ||
| } | ||
| self.labels = [ | ||
| {"id": "INBOX", "name": "INBOX"}, | ||
| {"id": "Label_1", "name": "Archive target"}, | ||
| ] | ||
| self.archive_calls = 0 | ||
|
|
||
| def list_labels(self): | ||
| return list(self.labels) | ||
|
|
||
| def get_message(self, message_id): | ||
| return {"labelIds": list(self.messages[message_id]["labelIds"])} | ||
|
|
||
| def add_label(self, message_id, label_id): | ||
| labels = self.messages[message_id]["labelIds"] | ||
| if label_id not in labels: | ||
| labels.append(label_id) | ||
|
|
||
| def archive_message(self, message_id): | ||
| self.archive_calls += 1 | ||
| labels = self.messages[message_id]["labelIds"] | ||
| if "INBOX" in labels: | ||
| labels.remove("INBOX") | ||
|
|
||
|
|
||
| class _DB(DatabaseMixin): | ||
| pass | ||
|
|
||
|
|
||
| def _make_db(): | ||
| db = _DB() | ||
| db.init_db(":memory:") | ||
| action_store.init_schema(db) | ||
| return db | ||
|
|
||
|
|
||
| class _FakeAgent(OrganizeToolsMixin, DatabaseMixin): | ||
| """Minimal host that registers the real organize tool closures.""" | ||
|
|
||
| def __init__(self, mailbox): | ||
| self.config = SimpleNamespace(debug=False, undo_window_seconds=30) | ||
| self._backends = {"google": mailbox} | ||
| self._providers = { | ||
| message_id: "google" for message_id in mailbox.messages | ||
| } | ||
| self._organize_batch_id = "test-batch" | ||
| self._last_archive_batch_id = None | ||
| self.init_db(":memory:") | ||
| action_store.init_schema(self) | ||
| self._register_organize_tools() | ||
|
|
||
| def _organize_batch_threshold_exceeded(self): | ||
| return False | ||
|
|
||
| def _provider_for_message(self, message_id, mailbox=None): | ||
| return self._providers[message_id] | ||
|
|
||
| def _backend_for_message(self, message_id): | ||
| return self._backends[self._providers[message_id]] | ||
|
|
||
| def _record_organize_op(self, message_id, sender): | ||
| pass | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _preserve_tool_registry(): | ||
| snapshot = dict(_TOOL_REGISTRY) | ||
| yield | ||
| _TOOL_REGISTRY.clear() | ||
| _TOOL_REGISTRY.update(snapshot) | ||
|
|
||
|
|
||
| def test_move_to_label_inbox_restores_without_archiving(): | ||
| mailbox = _FakeMailbox() | ||
|
|
||
| move_to_label_impl(mailbox, _make_db(), message_id="outside", label_id="INBOX") | ||
|
|
||
| assert "INBOX" in mailbox.messages["outside"]["labelIds"] | ||
| assert mailbox.archive_calls == 0 | ||
|
|
||
|
|
||
| def test_move_to_label_non_inbox_target_still_archives(): | ||
| mailbox = _FakeMailbox() | ||
|
|
||
| move_to_label_impl( | ||
| mailbox, _make_db(), message_id="inbox", label_id="Archive target" | ||
| ) | ||
|
|
||
| assert "Label_1" in mailbox.messages["inbox"]["labelIds"] | ||
| assert "INBOX" not in mailbox.messages["inbox"]["labelIds"] | ||
| assert mailbox.archive_calls == 1 | ||
|
|
||
|
|
||
| def test_move_to_label_batch_restores_every_message_without_archiving(): | ||
| mailbox = _FakeMailbox() | ||
| mailbox.messages.update( | ||
| { | ||
| "outside-2": {"labelIds": ["Label_1"]}, | ||
| "outside-3": {"labelIds": ["Label_1"]}, | ||
| } | ||
| ) | ||
| agent = _FakeAgent(mailbox) | ||
|
|
||
| move_batch = get_tool_metadata("move_to_label_batch")["function"] | ||
| result = json.loads(move_batch(["outside", "outside-2", "outside-3"], "INBOX")) | ||
|
|
||
| assert result["ok"] is True, result | ||
| assert len(result["data"]["succeeded"]) == 3 | ||
| assert result["data"]["failed"] == [] | ||
| assert all( | ||
| "INBOX" in mailbox.messages[mid]["labelIds"] | ||
| for mid in ("outside", "outside-2", "outside-3") | ||
| ) | ||
| assert mailbox.archive_calls == 0 | ||
|
|
||
| agent.close_db() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.