Skip to content

The agent that grows with you

fix(connectors): reconnecting an unnamed account repairs it instead of adding a second one

Siddharth Balyanb20d7904f824
diff --git a/tools/connectors/gateway/merge.py b/tools/connectors/gateway/merge.py
index 4f39e9b19db..a5c5539e915 100644
--- a/tools/connectors/gateway/merge.py
+++ b/tools/connectors/gateway/merge.py
@@ -97,6 +97,10 @@ def render_remote_entry(planned: PlannedCall, remote: Mapping[str, Any]) -> dict
hint=_opt_str(error.get("hint")),
surface=connection_surface(),
)
+ # The account the link repairs or waits on; with no connect_url the user already holds its link.
+ connection_id = _opt_str(error.get("connection_id"))
+ if connection_id:
+ payload["connection_id"] = connection_id
else:
payload = {"code": code, "message": message}
connector = _opt_str(error.get("connector"))
diff --git a/tools/connectors/managed.py b/tools/connectors/managed.py
index 83b2117dfaa..3a56e147b88 100644
--- a/tools/connectors/managed.py
+++ b/tools/connectors/managed.py
@@ -73,8 +73,9 @@ def mint(client: Any, operation: ConnectionOperation, names: list[str], *, reini
return
# One aliased target per call, so the first named target is the request's.
named = next((t for t in operation.targets if t.name in names and t.alias), None)
- if reinitiate and connection_id is None and named is not None:
- connection_id = named.connection_id
+ if reinitiate and connection_id is None:
+ repaired = next((t.repair_id for t in operation.targets if t.name in names and t.repair_id), None)
+ connection_id = repaired or (named.connection_id if named is not None else None)
alias = None if connection_id or named is None else named.alias
response = client.connections(names, reinitiate=reinitiate, alias=alias, connection_id=connection_id,
**return_to_args(op=operation.op_id))
@@ -215,7 +216,11 @@ def _prepare(client: Any, action: str, force: bool) -> Callable[[ConnectionOpera
status = _alias_status(operation.targets) or _status_by_slug(client)
repair = []
for name in names:
- if status.get(name, {}).get("connected"):
+ 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.
+ repair.append(name)
+ elif status.get(name, {}).get("connected"):
operation.transition(name, TargetState.initiated, Actor.backend_watcher)
operation.transition(name, TargetState.connected, Actor.backend_watcher)
else:
@@ -245,7 +250,13 @@ def _alias_status(targets: list[Target]) -> Optional[dict[str, dict[str, Any]]]:
rows = portal_accounts()
status: dict[str, dict[str, Any]] = {}
for t in aliased:
- row = next((r for r in rows if r.get("connector") == t.name and r.get("alias") == t.alias), None)
+ mine = [r for r in rows if r.get("connector") == t.name and not r.get("disabled")]
+ row = next((r for r in mine if r.get("alias") == t.alias), None)
+ if row is None:
+ # An unnamed account is addressed by its label, as rename does; it stays unnamed.
+ row = next((r for r in mine if not r.get("alias") and r.get("label") == t.alias), None)
+ if row is not None:
+ t.alias, t.repair_id = None, row.get("connectionId")
status[t.name] = {"connected": bool(row) and row.get("status") == "active",
"connection_id": row.get("connectionId") if row else None}
return status
@@ -282,14 +293,17 @@ def _status_result(client: Any, connectors: list[str]) -> str:
else:
by_slug: dict[str, list[dict[str, Any]]] = {}
for row in rows:
- by_slug.setdefault(str(row.get("connector", "")).lower(), []).append(
- {"alias": row.get("alias"), "label": row.get("label"), "status": row.get("status"),
- "active": row.get("active")})
+ entry = {"alias": row.get("alias"), "label": row.get("label"), "status": row.get("status"),
+ "active": row.get("active")}
+ if row.get("disabled"):
+ entry["retired"] = True
+ by_slug.setdefault(str(row.get("connector", "")).lower(), []).append(entry)
items = [dict(i, accounts=by_slug.get(str(i.get("connector", "")).lower(), [])) for i in items]
return json.dumps({"connectors": items, "hint": (
"connected=false means calls to that connector will return CONNECTION_REQUIRED. "
"Use action 'connect' to start an authorization. 'accounts' lists each account by its name "
- "(alias) and the vendor's label.")}, ensure_ascii=False)
+ "(alias) and the vendor's label; address an unnamed account by its label. A retired account was "
+ "replaced by a reconnect and is kept only for the record.")}, ensure_ascii=False)
def _rename_result(target: HostedTarget) -> str:
diff --git a/tools/connectors/targets.py b/tools/connectors/targets.py
index 9086ed005da..0be988a3d3c 100644
--- a/tools/connectors/targets.py
+++ b/tools/connectors/targets.py
@@ -113,6 +113,9 @@ def _validate_accounts(action: str, managed: list[HostedTarget]) -> Optional[str
return _bad_alias("to", managed[0].to)
if any(t.alias for t in managed) and len(managed) > 1:
return "a call with an 'alias' takes that one target only; name other accounts in separate calls."
+ if action == "reconnect":
+ # Like rename, a repair may address an unnamed account by its label; the account read checks it.
+ return None
return next((e for t in managed if (e := _bad_alias("alias", t.alias))), None)