Compare commits

...
1 Commits
Author SHA1 Message Date
tben 3e13d02341 Adopt only the account a sign-in here made, so a provider leftover cannot double a card
CI / check (push) Canceled after 0s
Release image / image (release) Canceled after 0s
Release image / computer-image (release) Canceled after 0s
A fresh app adopted the first connected account its provider listed, even
when no request here started it. A leftover at the provider (a sign-in made
there, or an earlier attempt's) took that card, and the user's own sign-in
was then claimed beside it, so every connect in production made two cards
for one mailbox.

A row is now made only for a connected account a pending request names, an
unfinished sign-in stores nothing, and the insert refuses a second row for
the same provider account when a poll and the cron reach it at once.

Release 0.27.9.
2026-11-23 05:57:53 +01:00
7 changed files with 193 additions and 134 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "cobble",
"version": "0.27.8",
"version": "0.27.9",
"module": "index.ts",
"type": "module",
"private": true,
@@ -0,0 +1,85 @@
import { Database } from "bun:sqlite";
import { describe, expect, test } from "bun:test";
import insertConnectorAccountSql from "@/src/functions/backend/db/connectors/insert-connector-account-sql";
function scratch() {
const db = new Database(":memory:");
db.run(`CREATE TABLE connector_accounts (
id INTEGER PRIMARY KEY,
created_at INTEGER,
updated_at INTEGER,
connector_id INTEGER,
user_id INTEGER,
label TEXT,
slug TEXT,
provider_account_id TEXT,
status TEXT,
is_default INTEGER,
connected_at INTEGER,
last_checked_at INTEGER,
last_error TEXT
)`);
return db;
}
function insert(db: Database, { label, slug }: { label: string; slug: string }) {
const statement = insertConnectorAccountSql({
now: 1000,
connector_id: 1,
user_id: 1,
label,
slug,
provider_account_id: "ca_one",
status: "connected",
});
db.query(statement.sql).run(...statement.values);
}
describe("insertConnectorAccountSql", () => {
test("inserts the sign-in once", () => {
const db = scratch();
insert(db, { label: "Ben", slug: "ben" });
const rows = db.query(`SELECT label, slug, status, is_default, last_error FROM connector_accounts`).all();
expect(rows).toEqual([{ label: "Ben", slug: "ben", status: "connected", is_default: 0, last_error: "" }]);
});
test("keeps the row a second sync already wrote for the same account", () => {
const db = scratch();
insert(db, { label: "Ben", slug: "ben" });
insert(db, { label: "Ben again", slug: "ben-2" });
const rows = db.query(`SELECT label, slug FROM connector_accounts`).all();
expect(rows).toEqual([{ label: "Ben", slug: "ben" }]);
});
test("adds a different account of the same app", () => {
const db = scratch();
insert(db, { label: "Ben", slug: "ben" });
const statement = insertConnectorAccountSql({
now: 1000,
connector_id: 1,
user_id: 1,
label: "Ben work",
slug: "ben-work",
provider_account_id: "ca_two",
status: "connected",
});
db.query(statement.sql).run(...statement.values);
expect(db.query(`SELECT provider_account_id FROM connector_accounts ORDER BY id`).all()).toEqual([
{ provider_account_id: "ca_one" },
{ provider_account_id: "ca_two" },
]);
});
});
@@ -0,0 +1,50 @@
import type { ProviderAccountStatus } from "@/src/harness/connectors/connector-provider";
type Params = {
now: number;
connector_id: number;
user_id: number;
label: string;
slug: string;
provider_account_id: string;
status: ProviderAccountStatus;
};
/**
* One row per provider account, even when two syncs (a poll beside the cron)
* reach it at once: the statement itself refuses a second row, so neither can
* insert the same sign-in twice.
*/
export default function insertConnectorAccountSql({
now,
connector_id,
user_id,
label,
slug,
provider_account_id,
status,
}: Params) {
return {
sql: `INSERT INTO connector_accounts
(created_at, updated_at, connector_id, user_id, label, slug, provider_account_id, status, is_default, connected_at, last_checked_at, last_error)
SELECT ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ''
WHERE NOT EXISTS (
SELECT 1 FROM connector_accounts WHERE connector_id = ? AND user_id = ? AND provider_account_id = ?
)`,
values: [
now,
now,
connector_id,
user_id,
label,
slug,
provider_account_id,
status,
status == "connected" ? now : null,
now,
connector_id,
user_id,
provider_account_id,
],
};
}
@@ -2,63 +2,18 @@ import { describe, expect, test } from "bun:test";
import shouldAdoptConnectorAccount from "@/src/functions/backend/db/connectors/should-adopt-connector-account";
describe("shouldAdoptConnectorAccount", () => {
test("adopts a connected account into the app's free card", () => {
expect(shouldAdoptConnectorAccount({ status: "connected", reconnect: false, claimed: false, free_slot: true })).toBe(
true,
);
test("adopts a connected account its own sign-in made", () => {
expect(shouldAdoptConnectorAccount({ status: "connected", reconnect: true, claimed: false })).toBe(true);
expect(shouldAdoptConnectorAccount({ status: "connected", reconnect: false, claimed: true })).toBe(true);
});
test("leaves a second connected account at the provider once the card is taken", () => {
expect(shouldAdoptConnectorAccount({ status: "connected", reconnect: false, claimed: false, free_slot: false })).toBe(
false,
);
test("leaves a connected account nothing here asked for at the provider", () => {
expect(shouldAdoptConnectorAccount({ status: "connected", reconnect: false, claimed: false })).toBe(false);
});
test("adopts a connected account a request here started, even with no free card", () => {
expect(shouldAdoptConnectorAccount({ status: "connected", reconnect: false, claimed: true, free_slot: false })).toBe(
true,
);
});
test("adopts a pending account a request here started", () => {
expect(shouldAdoptConnectorAccount({ status: "pending", reconnect: false, claimed: true, free_slot: false })).toBe(
true,
);
});
test("adopts a pending account that reconnects a row", () => {
expect(shouldAdoptConnectorAccount({ status: "pending", reconnect: true, claimed: false, free_slot: false })).toBe(
true,
);
});
test("skips a pending account nothing here started", () => {
expect(shouldAdoptConnectorAccount({ status: "pending", reconnect: false, claimed: false, free_slot: true })).toBe(
false,
);
expect(shouldAdoptConnectorAccount({ status: "expired", reconnect: false, claimed: false, free_slot: true })).toBe(
false,
);
});
test("skips a stranger once the provider lists none of the app's accounts", () => {
expect(
shouldAdoptConnectorAccount({
status: "connected",
reconnect: false,
claimed: false,
free_slot: true,
wiped: true,
}),
).toBe(false);
expect(
shouldAdoptConnectorAccount({
status: "connected",
reconnect: false,
claimed: true,
free_slot: true,
wiped: true,
}),
).toBe(true);
test("leaves an unfinished sign-in alone until it connects", () => {
expect(shouldAdoptConnectorAccount({ status: "pending", reconnect: true, claimed: false })).toBe(false);
expect(shouldAdoptConnectorAccount({ status: "pending", reconnect: false, claimed: true })).toBe(false);
expect(shouldAdoptConnectorAccount({ status: "expired", reconnect: false, claimed: false })).toBe(false);
});
});
@@ -6,36 +6,17 @@ type Params = {
reconnect: boolean;
/** A pending request here started this sign-in. */
claimed: boolean;
/**
* Whether the app has no live account here yet: one card per app is
* filled by the provider's account, and any other account arrives through
* its own sign-in (claimed).
*/
free_slot: boolean;
/**
* The provider lists none of the accounts the app knows: they were
* replaced there, so no stranger takes their place (the cards offer
* Reconnect instead).
*/
wiped?: boolean;
};
/**
* Whether a provider account becomes a local row. One a request here started
* (or is reconnecting) always does. A connected one the app doesn't know
* gets the app's one free card, so leftovers from earlier sign-ins at the
* provider can't pile up as four rows of the same mailbox; a second account
* appears by signing in again, which claims its own row.
* Whether a provider account becomes a local row: only one a request here
* started or is reconnecting, and only once it is connected. One card per
* sign-in, so an account the provider already held (a leftover from an earlier
* attempt, or a sign-in made at the provider itself) never arrives as a second
* card of the same mailbox. A sign-in that hasn't finished has no card either:
* a link that connects under another id would leave a dead row beside the real
* one.
*/
export default function shouldAdoptConnectorAccount({
status,
reconnect,
claimed,
free_slot,
wiped = false,
}: Params) {
if (reconnect || claimed) return true;
if (wiped) return false;
return status == "connected" && free_slot;
export default function shouldAdoptConnectorAccount({ status, reconnect, claimed }: Params) {
return status == "connected" && (reconnect || claimed);
}
@@ -7,6 +7,7 @@ import connectorSlug from "@/src/utils/connector-slug";
import grabConnector from "./grab-connector";
import parseConnectorAccountRow from "./parse-connector-account-row";
import shouldAdoptConnectorAccount from "./should-adopt-connector-account";
import insertConnectorAccountSql from "./insert-connector-account-sql";
type Params = {
user_id: number | string;
@@ -20,15 +21,15 @@ type Params = {
/**
* Reads the user's accounts for a connector at its provider and brings the
* local rows in line: new sign-ins are added (the first connected one is the
* default), ones the provider reports expired say so, and ones it no longer
* lists are marked revoked without losing their label. Pending connect
* requests are closed as soon as the account they made is connected.
* local rows in line: the account a sign-in here made is added (the first
* connected one is the default), ones the provider reports expired say so, and
* ones it no longer lists are marked revoked without losing their label.
* Pending connect requests are closed as soon as the account they made is
* connected.
*
* One card per app unless the user signed in again: an account the provider
* holds but no request here made fills the app's free card once and is left
* at the provider after that, so a pile of earlier sign-ins cannot arrive as
* four rows of the same mailbox.
* One card per sign-in: an account no request here made is left at the
* provider, so a leftover from an earlier attempt there cannot arrive as a
* second card of the same mailbox beside the one its own sign-in made.
*/
export default async function syncConnectorAccounts({ user_id, connector_id, label }: Params) {
const connector = await grabConnector({ connector_id });
@@ -51,34 +52,29 @@ export default async function syncConnectorAccounts({ user_id, connector_id, lab
const reconnect_for = new Map(
requests
.filter((row) => Number(row.account_id) != 0 && row.provider_account_id)
// A row disconnected since is no target: that sign-in arrives as a new one.
.filter((request) => local.some((account) => Number(account.id) == Number(request.account_id)))
.map((row) => [String(row.provider_account_id), Number(row.account_id)]),
);
let reconnect_row = pendingReconnectRow(requests);
// A row disconnected since is no target: that sign-in arrives as a new one.
if (reconnect_row && !local.some((row) => Number(row.id) == reconnect_row)) reconnect_row = null;
/**
* A sign-in only becomes a row once it is connected, or while the request
* that started it is still open. A pending account nothing here asked for
* is an abandoned or replaced sign-in at the provider (Composio keeps one
* A sign-in only becomes a row once it is connected, and only the account
* its own request names. A pending account nothing here asked for is an
* abandoned or replaced sign-in at the provider (Composio keeps one
* `INITIALIZING` per abandoned link), and adopting it would put a second,
* never connected account beside the one a reconnect replaced.
*/
const claimed = new Set(requests.map((row) => String(row.provider_account_id || "")).filter(Boolean));
/**
* A new sign-in the provider named no account for: the first connected
* account it didn't already have is that sign-in, once.
*/
let unclaimed_sign_in = requests.some((row) => Number(row.account_id) == 0 && !row.provider_account_id);
const seen = new Set(accounts.map((account) => account.provider_account_id));
const now = Date.now();
/**
* The provider lists none of the accounts the app knows: they were
* replaced there (a sign-in made outside the app), so its leftover takes
* no card and the known ones offer Reconnect.
*/
const wiped = local.length > 0 && !local.some((row) => seen.has(String(row.provider_account_id || "")));
/**
* The app's one free card: a live row still listed by the provider holds
* it. An account arriving without a sign-in here (an older attempt at
* the provider) may fill it once, never pile up beside it.
*/
let free_slot = !local.some((row) => row.status == "connected" && seen.has(String(row.provider_account_id || "")));
/**
* A listing that names no account is the provider unable to answer, not
@@ -96,7 +92,7 @@ export default async function syncConnectorAccounts({ user_id, connector_id, lab
}
}
let firstConnected: number | null = free_slot ? 0 : null;
let firstConnected: number | null = local.some((row) => row.status == "connected") ? null : 0;
for (const account of accounts) {
const provider_account_id = String(account.provider_account_id);
@@ -105,10 +101,11 @@ export default async function syncConnectorAccounts({ user_id, connector_id, lab
if (
!shouldAdoptConnectorAccount({
status: account.status,
reconnect: Boolean(reconnect_id) || (account.status == "connected" && Boolean(reconnect_row)),
claimed: claimed.has(provider_account_id),
free_slot,
wiped,
reconnect: Boolean(reconnect_id) || Boolean(reconnect_row),
claimed:
claimed.has(provider_account_id) ||
(unclaimed_sign_in &&
!local.some((row) => String(row.provider_account_id || "") == provider_account_id)),
})
)
continue;
@@ -119,14 +116,11 @@ export default async function syncConnectorAccounts({ user_id, connector_id, lab
account,
fallback_label: label,
reconnect_id,
reconnect_row: account.status == "connected" ? reconnect_row : null,
reconnect_row,
});
if (account.status == "connected" && reconnect_row) reconnect_row = null;
if (account.status != "connected") continue;
if (free_slot) free_slot = false;
reconnect_row = null;
unclaimed_sign_in = false;
if (firstConnected === 0) {
const created = (
@@ -273,23 +267,17 @@ async function upsertAccount({ connector_id, user_id, account, fallback_label, r
slug = `${connectorSlug({ name: display_name, max: 8, fallback: "account" })}-${index}`;
}
await BunSQLite.sql({
sql: `INSERT INTO connector_accounts
(created_at, updated_at, connector_id, user_id, label, slug, provider_account_id, status, is_default, connected_at, last_checked_at, last_error)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, '')`,
values: [
now,
now,
connector_id,
user_id,
display_name,
slug,
account.provider_account_id,
account.status,
account.status == "connected" ? now : null,
now,
],
const { sql, values } = insertConnectorAccountSql({
now,
connector_id,
user_id,
label: display_name,
slug,
provider_account_id: account.provider_account_id,
status: account.status,
});
await BunSQLite.sql({ sql, values });
}
type SettleParams = {