-
Notifications
You must be signed in to change notification settings - Fork 672
fix: preserve custom lead agent names during reprovisioning #283
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
aramirez087
wants to merge
5
commits into
abhi1693:master
Choose a base branch
from
aramirez087:fix/preserve-lead-agent-name
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c6c99c7
fix: preserve custom lead agent names during reprovisioning
aramirez087 3f8bff9
Merge branch 'master' into fix/preserve-lead-agent-name
aramirez087 316724e
test: cover lead agent name preservation
aramirez087 1a82710
Merge branch 'master' into fix/preserve-lead-agent-name
aramirez087 031e695
Merge branch 'master' into fix/preserve-lead-agent-name
aramirez087 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| # ruff: noqa: S101 | ||
| """Unit tests for board-lead provisioning name behavior.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Any | ||
| from uuid import UUID, uuid4 | ||
|
|
||
| import pytest | ||
|
|
||
| import app.services.openclaw.provisioning_db as provisioning_db | ||
| from app.models.agents import Agent | ||
| from app.models.boards import Board | ||
| from app.models.gateways import Gateway | ||
| from app.services.openclaw.gateway_rpc import GatewayConfig as GatewayClientConfig | ||
| from app.services.openclaw.provisioning_db import ( | ||
| LeadAgentOptions, | ||
| LeadAgentRequest, | ||
| OpenClawProvisioningService, | ||
| ) | ||
|
|
||
|
|
||
| class _ExecResult: | ||
| def __init__(self, value: Agent | None) -> None: | ||
| self._value = value | ||
|
|
||
| def first(self) -> Agent | None: | ||
| return self._value | ||
|
|
||
|
|
||
| @dataclass | ||
| class _FakeSession: | ||
| existing: Agent | None | ||
| commits: int = 0 | ||
| added: list[object] = field(default_factory=list) | ||
| refreshed: list[object] = field(default_factory=list) | ||
|
|
||
| async def exec(self, _statement: object) -> _ExecResult: | ||
| return _ExecResult(self.existing) | ||
|
|
||
| def add(self, value: object) -> None: | ||
| self.added.append(value) | ||
|
|
||
| async def commit(self) -> None: | ||
| self.commits += 1 | ||
|
|
||
| async def refresh(self, value: object) -> None: | ||
| self.refreshed.append(value) | ||
|
|
||
|
|
||
| def _board() -> Board: | ||
| organization_id = uuid4() | ||
| gateway_id = uuid4() | ||
| return Board( | ||
| id=uuid4(), | ||
| organization_id=organization_id, | ||
| gateway_id=gateway_id, | ||
| name="Roadmap", | ||
| slug="roadmap", | ||
| ) | ||
|
|
||
|
|
||
| def _gateway(*, organization_id: UUID) -> Gateway: | ||
| return Gateway( | ||
| id=uuid4(), | ||
| organization_id=organization_id, | ||
| name="Gateway", | ||
| url="ws://gateway.example/ws", | ||
| workspace_root="/tmp/openclaw", | ||
| ) | ||
|
|
||
|
|
||
| def _request( | ||
| *, | ||
| board: Board, | ||
| gateway: Gateway, | ||
| options: LeadAgentOptions | None = None, | ||
| ) -> LeadAgentRequest: | ||
| return LeadAgentRequest( | ||
| board=board, | ||
| gateway=gateway, | ||
| config=GatewayClientConfig(url=gateway.url, token=None), | ||
| user=None, | ||
| options=options or LeadAgentOptions(), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_ensure_board_lead_agent_preserves_existing_custom_name_without_override() -> None: | ||
| board = _board() | ||
| gateway = _gateway(organization_id=board.organization_id) | ||
| existing = Agent( | ||
| id=uuid4(), | ||
| board_id=board.id, | ||
| gateway_id=gateway.id, | ||
| name="Roadmap Captain", | ||
| is_board_lead=True, | ||
| openclaw_session_id=OpenClawProvisioningService.lead_session_key(board), | ||
| ) | ||
| session = _FakeSession(existing=existing) | ||
| service = OpenClawProvisioningService(session) # type: ignore[arg-type] | ||
|
|
||
| lead, created = await service.ensure_board_lead_agent( | ||
| request=_request(board=board, gateway=gateway), | ||
| ) | ||
|
|
||
| assert created is False | ||
| assert lead.name == "Roadmap Captain" | ||
| assert session.commits == 0 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_ensure_board_lead_agent_defaults_new_lead_name_when_none_provided( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| board = _board() | ||
| gateway = _gateway(organization_id=board.organization_id) | ||
| session = _FakeSession(existing=None) | ||
| service = OpenClawProvisioningService(session) # type: ignore[arg-type] | ||
| captured: dict[str, Any] = {} | ||
|
|
||
| monkeypatch.setattr(provisioning_db, "mint_agent_token", lambda _agent: "raw-token") | ||
|
|
||
| class _FakeOrchestrator: | ||
| def __init__(self, _session: object) -> None: | ||
| captured["session"] = _session | ||
|
|
||
| async def run_lifecycle(self, **kwargs: Any) -> Agent: | ||
| captured["kwargs"] = kwargs | ||
| agent = next(item for item in session.added if isinstance(item, Agent)) | ||
| return agent | ||
|
|
||
| monkeypatch.setattr(provisioning_db, "AgentLifecycleOrchestrator", _FakeOrchestrator) | ||
|
|
||
| lead, created = await service.ensure_board_lead_agent( | ||
| request=_request(board=board, gateway=gateway), | ||
| ) | ||
|
|
||
| assert created is True | ||
| assert lead.name == "Lead Agent" | ||
| assert lead.is_board_lead is True | ||
| assert lead.openclaw_session_id == OpenClawProvisioningService.lead_session_key(board) | ||
| assert session.commits == 1 | ||
| assert captured["session"] is session | ||
| assert captured["kwargs"]["auth_token"] == "raw-token" |
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.
Uh oh!
There was an error while loading. Please reload this page.