From 36db6e842e80cc4a29e3380398427bcf4e50f917 Mon Sep 17 00:00:00 2001 From: fforres Date: Sat, 14 Nov 2026 10:01:56 -0800 Subject: [PATCH] fix(sdk): rebuild the connection tool catalog with upsert and prune instead of delete and insert On engines without interactive transactions (Cloudflare D1) the delete and insert statements auto-commit, so readers saw an empty catalog between them and overlapping rebuilds failed on the unique tool index. Upsert the new rows on the unique key, then delete only the rows that are no longer produced. --- packages/core/sdk/src/connections.test.ts | 181 ++++++++++++++++++++++ packages/core/sdk/src/executor.ts | 42 ++++- packages/core/sdk/src/sqlite-test-db.ts | 22 +++ packages/core/sdk/src/test-config.ts | 10 ++ 4 files changed, 251 insertions(+), 4 deletions(-) diff --git a/packages/core/sdk/src/connections.test.ts b/packages/core/sdk/src/connections.test.ts index 8167c2938e..3ec53b0f8f 100644 --- a/packages/core/sdk/src/connections.test.ts +++ b/packages/core/sdk/src/connections.test.ts @@ -3918,3 +3918,184 @@ describe("connections.checkHealth flip telemetry", () => { }), ); }); + +describe("tool catalog rebuild on an engine without interactive transactions", () => { + const catalogPlugin = ( + catalog: { current: readonly { readonly name: string; readonly description: string }[] }, + release?: Deferred.Deferred, + ) => + definePlugin(() => ({ + id: "catalog" as const, + credentialProviders: [memoryProvider()], + storage: () => ({}), + resolveTools: () => + Effect.gen(function* () { + const tools = catalog.current.map((tool) => ({ + name: ToolName.make(tool.name), + description: tool.description, + })); + if (release) yield* Deferred.await(release); + return { tools }; + }), + invokeTool: ({ toolRow }) => Effect.succeed({ ran: toolRow.name }), + extension: (ctx) => ({ + seed: () => + ctx.core.integrations.register({ slug: INTEG, description: "Vercel", config: {} }), + }), + }))(); + + const MAIN = { + owner: "org", + integration: INTEG, + name: ConnectionName.make("main"), + } as const; + + const toolRows = (config: ReturnType) => + Effect.promise(() => config.db.findMany("tool", {})); + + /** Holds each catalog write until `width` writes are waiting (or a short + * timeout passes), so concurrent executors run their statements in lockstep + * the way separate isolates sharing one D1 database can. */ + const lockstepCatalogWrites = (width: number) => { + let waiting: (() => void)[] = []; + const releaseAll = () => { + const batch = waiting; + waiting = []; + for (const resolve of batch) resolve(); + }; + return (sql: string) => + /^\s*(delete|insert)/i.test(sql) && /tool|definition/.test(sql) + ? new Promise((resolve) => { + waiting.push(resolve); + if (waiting.length >= width) releaseAll(); + else setTimeout(releaseAll, 50); + }) + : Promise.resolve(); + }; + + it.effect("rebuilds that overlap across executors both succeed and keep each tool once", () => + Effect.scoped( + Effect.gen(function* () { + const catalog = { + current: [ + { name: "deploy", description: "deploy" }, + { name: "list", description: "list" }, + ], + }; + const release = yield* Deferred.make(); + const config = makeTestConfig({ + plugins: [catalogPlugin(catalog, release)] as const, + interactiveTransactions: false, + statementGate: lockstepCatalogWrites(5), + }); + yield* Deferred.succeed(release, undefined); + const first = yield* createExecutor(config); + const others = yield* Effect.all([1, 2, 3, 4].map(() => createExecutor(config))); + yield* first.catalog.seed(); + yield* first.connections.create({ + ...MAIN, + template: TEMPLATE, + value: "secret-token", + }); + + const results = yield* Effect.all( + [first, ...others].map((executor) => Effect.result(executor.connections.refresh(MAIN))), + { concurrency: "unbounded" }, + ); + expect(results.map((result) => Result.isSuccess(result))).toEqual([ + true, + true, + true, + true, + true, + ]); + + const rows = yield* toolRows(config); + expect(rows.map((row) => String(row.name)).sort()).toEqual(["deploy", "list"]); + }), + ), + ); + + it.effect("a rebuild never exposes an empty catalog to concurrent readers", () => + Effect.scoped( + Effect.gen(function* () { + const catalog = { + current: [ + { name: "deploy", description: "deploy" }, + { name: "list", description: "list" }, + ], + }; + const config = makeTestConfig({ + plugins: [catalogPlugin(catalog)] as const, + interactiveTransactions: false, + statementGate: lockstepCatalogWrites(2), + }); + const executor = yield* createExecutor(config); + yield* executor.catalog.seed(); + yield* executor.connections.create({ + ...MAIN, + template: TEMPLATE, + value: "secret-token", + }); + + let polling = true; + const counts: number[] = []; + const poller = Effect.promise(async () => { + while (polling) { + const rows = await config.db.findMany("tool", {}); + counts.push(rows.length); + await new Promise((resolve) => setTimeout(resolve, 2)); + } + }); + const pollerFiber = yield* Effect.forkChild(poller); + yield* executor.connections.refresh(MAIN); + polling = false; + yield* Fiber.join(pollerFiber); + + expect(counts.length).toBeGreaterThan(0); + expect(Math.min(...counts)).toBe(2); + }), + ), + ); + + it.effect("a rebuild removes dropped tools and updates changed descriptions", () => + Effect.scoped( + Effect.gen(function* () { + const catalog = { + current: [ + { name: "deploy", description: "deploy v1" }, + { name: "list", description: "list v1" }, + ], + }; + const config = makeTestConfig({ + plugins: [catalogPlugin(catalog)] as const, + interactiveTransactions: false, + }); + const executor = yield* createExecutor(config); + yield* executor.catalog.seed(); + yield* executor.connections.create({ + ...MAIN, + template: TEMPLATE, + value: "secret-token", + }); + + catalog.current = [ + { name: "deploy", description: "deploy v2" }, + { name: "status", description: "status v1" }, + ]; + const refreshed = yield* executor.connections.refresh(MAIN); + expect(refreshed.map((tool) => String(tool.name)).sort()).toEqual(["deploy", "status"]); + + const rows = yield* toolRows(config); + const byName = Object.fromEntries( + rows.map((row) => [String(row.name), String(row.description)]), + ); + expect(byName).toEqual({ deploy: "deploy v2", status: "status v1" }); + + catalog.current = []; + yield* executor.connections.refresh(MAIN); + expect(yield* toolRows(config)).toEqual([]); + }), + ), + ); +}); diff --git a/packages/core/sdk/src/executor.ts b/packages/core/sdk/src/executor.ts index e8b5b762b7..75af2a2315 100644 --- a/packages/core/sdk/src/executor.ts +++ b/packages/core/sdk/src/executor.ts @@ -238,6 +238,7 @@ import { makeShapeMemory, observedShapeToJsonSchema, SHAPE_MEMORY_PLUGIN_ID } fr import { isUnauthorizedToolFailure } from "./auth-tool-failure"; const PLUGIN_STORAGE_DELETE_KEY_BATCH_SIZE = 90; +const CATALOG_PRUNE_NAME_BATCH_SIZE = 90; const MAX_APPROVAL_ARGUMENT_PREVIEW_CHARS = 4_000; // --------------------------------------------------------------------------- @@ -4211,12 +4212,45 @@ export const createExecutor = row.schema)), }); + const pruneStale = (table: "tool" | "definition", keptNames: readonly string[]) => + Effect.gen(function* () { + const kept = new Set(keptNames); + const existing = yield* core.findMany(table, { where, select: ["name"] }); + const stale = existing.map((row) => String(row.name)).filter((name) => !kept.has(name)); + for (let offset = 0; offset < stale.length; offset += CATALOG_PRUNE_NAME_BATCH_SIZE) { + const batch = stale.slice(offset, offset + CATALOG_PRUNE_NAME_BATCH_SIZE); + yield* core.deleteMany(table, { + where: (b: AnyCb) => b.and(where(b), b("name", "in", batch)), + }); + } + }); yield* persistCatalog( Effect.gen(function* () { - yield* core.deleteMany("tool", { where }); - yield* core.deleteMany("definition", { where }); - yield* core.createMany("tool", toolRows); - yield* core.createMany("definition", definitionRows); + yield* core.upsertMany("tool", { + target: ["tenant", "owner", "subject", "integration", "connection", "name"], + update: [ + "plugin_id", + "description", + "input_schema", + "output_schema", + "annotations", + "updated_at", + ], + values: toolRows, + }); + yield* core.upsertMany("definition", { + target: ["tenant", "owner", "subject", "integration", "connection", "name"], + update: ["plugin_id", "schema"], + values: definitionRows, + }); + yield* pruneStale( + "tool", + toolRows.map((row) => row.name), + ); + yield* pruneStale( + "definition", + definitionRows.map((row) => row.name), + ); yield* stampSynced(existingRow); }), ); diff --git a/packages/core/sdk/src/sqlite-test-db.ts b/packages/core/sdk/src/sqlite-test-db.ts index f68b2b33ab..ad3c6ccbee 100644 --- a/packages/core/sdk/src/sqlite-test-db.ts +++ b/packages/core/sdk/src/sqlite-test-db.ts @@ -29,6 +29,11 @@ export interface CreateSqliteTestFumaDbOptions Promise; } export const createSqliteTestFumaDb = async ( @@ -46,6 +51,22 @@ export const createSqliteTestFumaDb = async ( !options.path || options.path === ":memory:" ? ":memory:" : `file:${resolve(options.path)}`; const client = createClient({ url }); await client.execute("PRAGMA foreign_keys = ON"); + const gate = options.statementGate; + if (gate) { + const execute = client.execute.bind(client); + const batch = client.batch.bind(client); + const sqlOf = (statement: unknown) => + typeof statement === "string" ? statement : String((statement as { sql?: unknown }).sql); + client.execute = (async (statement: Parameters[0]) => { + await gate(sqlOf(statement)); + return execute(statement); + }) as Client["execute"]; + client.batch = (async (...args: Parameters) => { + const first = args[0][0]; + if (first !== undefined) await gate(sqlOf(first)); + return batch(...args); + }) as Client["batch"]; + } const schema = createDrizzleRuntimeSchemaFromTables({ tables: options.tables, @@ -69,6 +90,7 @@ export const createSqliteTestFumaDb = async ( namespace, version, provider: "sqlite", + interactiveTransactions: options.interactiveTransactions, }); return { diff --git a/packages/core/sdk/src/test-config.ts b/packages/core/sdk/src/test-config.ts index 3aa40c2a6d..d3b4bbb9d5 100644 --- a/packages/core/sdk/src/test-config.ts +++ b/packages/core/sdk/src/test-config.ts @@ -27,6 +27,8 @@ const makeLazyTestFumaDb = (options: { readonly tables: ReturnType; readonly backend: TestDatabaseBackend; readonly dataDir?: string; + readonly interactiveTransactions?: boolean; + readonly statementGate?: (sql: string) => Promise; }): TestFumaDb => { let started: Promise | undefined; const start = () => { @@ -36,6 +38,8 @@ const makeLazyTestFumaDb = (options: { tables: options.tables, namespace: "executor_test", path: options.dataDir ? `${options.dataDir}/test.db` : undefined, + interactiveTransactions: options.interactiveTransactions, + statementGate: options.statementGate, }), ); } @@ -126,6 +130,10 @@ export type TestConfigOptions Promise; readonly coreTools?: ExecutorConfig["coreTools"]; /** Override the OAuth callback URL. Pass `null` to construct an executor with * no OAuth callback (exercises the fail-loud redirect path). */ @@ -157,6 +165,8 @@ export const makeTestConfig =