From 23e7145cee8f157b2f337fe976b6e61f7ff69bf8 Mon Sep 17 00:00:00 2001 From: arc53-machine <232052973+arc53-machine@users.noreply.github.com> Date: Tue, 29 Sep 2026 15:09:18 +0100 Subject: [PATCH] Give a member the account they just connected Member-mode resolution ranked accounts by last_used_at first, so a connection added by Connect to continue (never used) lost to any older used account. Accounts now rank by the later of last use and creation. --- docsgpt/connectors/resolve.py | 8 +++++--- tests/connectors/test_runtime.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/docsgpt/connectors/resolve.py b/docsgpt/connectors/resolve.py index 4ed4d0fa..6d07f7af 100644 --- a/docsgpt/connectors/resolve.py +++ b/docsgpt/connectors/resolve.py @@ -122,8 +122,9 @@ def _member_connection(repo: ConnectorSessionsRepository, owned: Optional[dict], """The invoker's own connection to the service the owner's connection is for. A member with several connected accounts of that service gets the one - they used most recently (then the most recently connected): the account - they are working in, and the one a "Connect to continue" just added. + most recently used or connected, whichever is later: the account they + are working in, or the one a "Connect to continue" just added (never + used yet). Ties go to the last used, then the last updated. """ if owned is None: return None @@ -144,7 +145,8 @@ def _member_connection(repo: ConnectorSessionsRepository, owned: Optional[dict], return value if value.tzinfo else value.replace(tzinfo=timezone.utc) def recency(row: dict) -> tuple: - return tuple(when(row.get(field)) for field in ("last_used_at", "updated_at", "created_at")) + used, updated, created = (when(row.get(field)) for field in ("last_used_at", "updated_at", "created_at")) + return max(used, created), used, updated, created return max(candidates, key=recency) diff --git a/tests/connectors/test_runtime.py b/tests/connectors/test_runtime.py index 7488323a..c2a2201f 100644 --- a/tests/connectors/test_runtime.py +++ b/tests/connectors/test_runtime.py @@ -104,6 +104,22 @@ class TestResolution: resolved = resolve_connection(_tool(owner, mode="member"), "bob") assert resolved.available and resolved.connection_id == newer + def test_member_gets_the_account_they_just_connected(self, pg_conn): + from docsgpt.connectors.resolve import resolve_connection + + owner = _connection(pg_conn) + used = _connection(pg_conn, user="bob") + pg_conn.execute(text( + "UPDATE connector_sessions SET account_label = 'bob-work', created_at = now() - interval '30 days', " + "updated_at = now() - interval '30 days', last_used_at = now() - interval '1 hour' " + "WHERE id = CAST(:i AS uuid)" + ), {"i": used}) + # Added by "Connect to continue": never used yet. + added = _connection(pg_conn, user="bob") + with _service_db(pg_conn): + resolved = resolve_connection(_tool(owner, mode="member"), "bob") + assert resolved.available and resolved.connection_id == added + def test_member_mode_without_own_connection_is_unavailable(self, pg_conn): from docsgpt.connectors.resolve import resolve_connection