Files
hermes-agent/tests/tools/test_connectors_operation.py
alt-glitch c2b13dc3d8 feat(desktop): every desktop chat can install catalog plugins; tools gated by session platform
Toolsets that exist only for one kind of session now say so on their spec:
`platforms=frozenset({"desktop"})`. `TOOLSET_SESSION_PLATFORMS` is derived
from the specs that carry the key, `all` / `*` / no selection skip those
toolsets, and `toolsets.agent_tool_drops(agent)` removes their tools from any
agent whose own session platform is not listed. `agent_init._load_tools` and
`tools/mcp_tool_agent.py` (MCP refresh and prefix restore) call that one
helper. `tool_search._DIRECT_SURFACE_TOOLSETS` is derived from
`CLIENT_SURFACE_TOOLSETS` and `TOOLSET_SESSION_PLATFORMS`.

This replaces the profile-role gate: `profile_role_toolsets`, the role code in
`_with_session_toolsets` and the explicit-pin branch, the strip step in
`_select_tool_names`, and main's `setup` toolset (whose only tool moves out)
are deleted. `manage_catalog` moves into a new `catalog` toolset, which the
desktop client folds into every desktop chat like `project` and `desktop_ui`.
It is deferred behind tool_search by default (`config_defaults` defer list).
The shared-metrics schemas' toolset enum follows (`setup` out, `catalog` in).

The install card works outside the setup chat (#129650, with review fixes):
- Parallel installs: `_set_plugin_enabled` holds a per-home lock around the
  read-then-admit section, so rows enabled at once no longer refuse each other.
- A catalog plugin already on disk and not enabled is enabled in place, so Try
  again after a refused enable can finish.
- A failed catalog row counts as resolved (`contract.resolves`), so it no longer
  holds the turn to the 300 s deadline. The snapshot carries `resolved`, and the
  card reads it instead of a hand copy of the backend's rule.
- Each card draws its own operation by op id, because models reuse call ids.
- The row carries the approved non-secret choices (`approved`: force, enable,
  ref) and the chosen profile, so Try again after the card settled repeats them.
- Install phases are ids (`InstallPhase`, wire `CatalogInstallPhase`) and the
  installed-row notes are fields (`enabled`, `missing_env`, `server_errors`,
  `already_installed`, `requires_hermes`); the card words them in English
  strings (translations follow later). Failure reasons stay raw text, and
  `detail` keeps the English notes for the model.
- Installs and search target the calling chat's own home, carried as a path;
  the row's profile name is only a label, with no `'default'` fallback in
  Python or TypeScript.
- `dashboard_install_plugin` is split into `_resolve_source`, `_place_tree` and
  `_enable_placed`.

Desktop English strings for the card move to `en_catalog_install.ts` /
`types_catalog_install.ts`, which offsets the growth of `en.ts` and `types.ts`.
2026-10-07 01:01:14 +05:30

90 lines
4.5 KiB
Python

"""Connection operation: transitions checked against the contract, exactly-once settlement,
server-owned deadline as a constant, wake on every change."""
import pytest
from tools.connectors import contract as c
from tools.connectors import operation as op
def _two(kind="connector"):
return [op.Target("gmail", kind, "connect"), op.Target("notion", kind, "connect")]
def test_transition_enforces_the_contract_and_names_the_actor():
operation = op.ConnectionOperation(_two())
operation.transition("gmail", c.TargetState.initiated, c.Actor.backend_watcher)
with pytest.raises(op.IllegalTransition):
operation.transition("gmail", c.TargetState.connected, c.Actor.user) # a card cannot claim connected
with pytest.raises(op.IllegalTransition):
operation.transition("gmail", c.TargetState.pending, c.Actor.backend_watcher) # no edge back
operation.transition("gmail", c.TargetState.connected, c.Actor.backend_watcher)
assert operation.target("gmail").state == c.TargetState.connected
def test_transition_returns_the_change_and_wakes_the_waiter():
operation = op.ConnectionOperation(_two())
assert not operation.wake.is_set()
change = operation.transition("gmail", c.TargetState.initiated, c.Actor.backend_watcher, detail="link minted")
assert change == {"target": "gmail", "from": "pending", "to": "initiated", "actor": "backend_watcher", "detail": "link minted"}
assert operation.wake.is_set()
def test_same_state_transition_is_a_no_op_not_an_error():
operation = op.ConnectionOperation(_two())
operation.transition("gmail", c.TargetState.initiated, c.Actor.backend_watcher)
operation.wake.clear()
assert operation.transition("gmail", c.TargetState.initiated, c.Actor.backend_watcher) is None
assert not operation.wake.is_set()
def test_settle_is_exactly_once_and_stamps_not_connected():
operation = op.ConnectionOperation(_two())
operation.transition("gmail", c.TargetState.initiated, c.Actor.backend_watcher)
operation.transition("gmail", c.TargetState.connected, c.Actor.backend_watcher)
assert operation.settle(c.SettleReason.continue_) is True
frozen = operation.result()
assert operation.settle(c.SettleReason.deadline) is False
assert operation.result() == frozen
by = {t["name"]: t for t in frozen["targets"]}
assert by["gmail"]["state"] == "connected"
# The settle reason is on the operation, never copied into a row's detail (the card showed it as red text).
assert by["notion"]["state"] == "not_connected" and "detail" not in by["notion"]
def test_all_resolved_settles_on_connected_or_skipped_only():
operation = op.ConnectionOperation(_two())
operation.transition("gmail", c.TargetState.initiated, c.Actor.backend_watcher)
operation.transition("gmail", c.TargetState.failed, c.Actor.backend_watcher, detail="oauth denied")
operation.transition("notion", c.TargetState.skipped, c.Actor.user)
assert operation.settle_if_all_resolved() is False # failed keeps the op open
operation.transition("gmail", c.TargetState.skipped, c.Actor.user)
assert operation.settle_if_all_resolved() is True
assert operation.settled_by == c.SettleReason.all_resolved
def test_target_keeps_the_link_and_the_mint_detail_across_transitions():
operation = op.ConnectionOperation(_two())
operation.transition("gmail", c.TargetState.initiated, c.Actor.backend_watcher,
connect_url="https://link/gmail", detail="")
operation.transition("gmail", c.TargetState.failed, c.Actor.backend_watcher, detail="vendor said no")
snap = operation.target("gmail").snapshot()
assert snap["connect_url"] == "https://link/gmail"
assert snap["detail"] == "vendor said no"
def test_request_payload_carries_the_live_target_snapshot():
operation = op.ConnectionOperation(
[op.Target("gmail", "connector", "reconnect", instructions="Finish setup")],
tool_call_id="call-1")
operation.transition("gmail", c.TargetState.initiated, c.Actor.backend_watcher, connect_url="https://l/gmail")
payload = operation.request_payload()
(target,) = payload["targets"]
assert target == {"name": "gmail", "kind": "connector", "action": "reconnect", "state": "initiated", "resolved": False,
"instructions": "Finish setup", "connect_url": "https://l/gmail"}
# The model's own id keys the card to its tool row; a later op for the same apps gets a new one.
assert payload["tool_call_id"] == "call-1"
assert "reason" not in payload