Skip to content

The agent that grows with you

fix(connectors): a repair by account id targets exactly that account

Siddharth Balyand0ef95ec5026
diff --git a/tools/connectors/account.py b/tools/connectors/account.py
index 90d97f28ad1..d169338c2d5 100644
--- a/tools/connectors/account.py
+++ b/tools/connectors/account.py
@@ -23,9 +23,9 @@ class AccountOperationStart:
failed: bool = False
-def _matching_open_operation(names: list[str], *, alias: str | None,
+def _matching_open_operation(names: list[str], *, alias: str | None, repair_id: str | None,
profile_home: str | None) -> ConnectionOperation | None:
- matches = [live.find_target(name, alias=alias, profile_home=profile_home) for name in names]
+ matches = [live.find_target(name, alias=alias, repair_id=repair_id, profile_home=profile_home) for name in names]
first = next((operation for operation in matches if operation is not None), None)
if first is None:
return None
@@ -45,7 +45,7 @@ def find_or_start_operation(
"""``alias`` names the one account of ``names[0]`` to connect; ``repair_id`` names the existing
account a reconnect repairs. The caller passes one name with either."""
with _start_lock:
- if operation := _matching_open_operation(names, alias=alias, profile_home=profile_home):
+ if operation := _matching_open_operation(names, alias=alias, repair_id=repair_id, profile_home=profile_home):
return AccountOperationStart(operation=operation, started=False)
operation = ConnectionOperation(
[Target(name, "connector", action, alias=alias, repair_id=repair_id) for name in names],
diff --git a/tools/connectors/live.py b/tools/connectors/live.py
index c90695c6311..03a880b9dd1 100644
--- a/tools/connectors/live.py
+++ b/tools/connectors/live.py
@@ -63,16 +63,16 @@ def get_by_op_id(op_id: str, *, profile_home: Optional[str] = None) -> Optional[
if key == profile_key and operation.op_id == op_id and not operation.settled), None)
-def find_target(name: str, *, alias: Optional[str] = None,
+def find_target(name: str, *, alias: Optional[str] = None, repair_id: Optional[str] = None,
profile_home: Optional[str] = None) -> Optional[ConnectionOperation]:
- """The open operation connecting this account: the connector ``name`` and the same ``alias``,
- so an open operation for one account is never reused for another account of the same app."""
+ """The open operation connecting this account: the connector ``name`` and the same ``alias`` and
+ repaired account id, so an open operation for one account is never reused for another of the same app."""
profile_key = _profile_key(profile_home)
with _lock:
return next((operation for (key, _), operation in _open.items()
if key == profile_key and not operation.settled
and (target := operation.target(name)) is not None and target.kind == "connector"
- and target.alias == alias), None)
+ and target.alias == alias and target.repair_id == repair_id), None)
def close(operation: ConnectionOperation) -> None:
diff --git a/tools/connectors/managed.py b/tools/connectors/managed.py
index 3a56e147b88..e17b18fec16 100644
--- a/tools/connectors/managed.py
+++ b/tools/connectors/managed.py
@@ -207,18 +207,22 @@ def _prepare(client: Any, action: str, force: bool) -> Callable[[ConnectionOpera
mint(client, operation, names, reinitiate=False, actor=Actor.backend_watcher)
_mark_misrouted(operation)
return
+ # A named account is checked on its own row, read before force so a label resolves to its
+ # account either way: the connector-wide flag reports any account.
+ named = _alias_status(operation.targets)
if force:
# The re-mint names a new account; the watcher reads that one, never the old row.
- mint(client, operation, names, reinitiate=True, actor=Actor.backend_watcher)
+ account = next((s["connection_id"] for s in (named or {}).values() if s.get("connection_id")), None)
+ mint(client, operation, names, reinitiate=True, actor=Actor.backend_watcher, connection_id=account)
_mark_misrouted(operation)
return
- # A named account is checked on its own row: the connector-wide flag reports any account.
- status = _alias_status(operation.targets) or _status_by_slug(client)
+ status = named or _status_by_slug(client)
repair = []
for name in names:
target = operation.target(name)
- if target is not None and target.repair_id:
- # Asked to repair one account by id: the connector's other accounts say nothing about it.
+ if target is not None and target.repair_id and name not in (named or {}):
+ # A surface asked to repair one account by id: its Reconnect is the user's choice, and
+ # the connector's other accounts say nothing about this one.
repair.append(name)
elif status.get(name, {}).get("connected"):
operation.transition(name, TargetState.initiated, Actor.backend_watcher)
diff --git a/tui_gateway/methods_connectors.py b/tui_gateway/methods_connectors.py
index c34e0cbeab9..a2ae2d98636 100644
--- a/tui_gateway/methods_connectors.py
+++ b/tui_gateway/methods_connectors.py
@@ -149,9 +149,11 @@ def _session_connector_rpc(rid, request, session, action):
return _connector_rpc_error(
rid, 4004, ConnectorErrorReason.unknown_operation, "No open connection operation for this session."
)
- # A retry without an alias means the open target; one naming another account must not re-mint this one.
- if request.alias is not None and any(
- (target := operation.target(name)) is not None and target.alias != request.alias
+ # A retry without an alias or id means the open target; one naming another account must not re-mint this one.
+ if (request.alias is not None or request.connection_id is not None) and any(
+ (target := operation.target(name)) is not None
+ and (target.alias != request.alias
+ or request.connection_id not in (None, target.repair_id, target.connection_id))
for name in request.connectors):
return _connector_rpc_error(rid, 4004, ConnectorErrorReason.unknown_target, "No such target on the open operation.")
return _reissue(rid, operation, args)