Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions packages/core/sdk/src/connections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>,
) =>
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<typeof makeTestConfig>) =>
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<void>((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<void>();
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([]);
}),
),
);
});
42 changes: 38 additions & 4 deletions packages/core/sdk/src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -4211,12 +4212,45 @@ export const createExecutor = <const TPlugins extends readonly AnyPlugin[] = rea
),
largestDefinitionChars: largestJsonChars(definitionRows.map((row) => 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);
}),
);
Expand Down
22 changes: 22 additions & 0 deletions packages/core/sdk/src/sqlite-test-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export interface CreateSqliteTestFumaDbOptions<TTables extends FumaTables = Fuma
readonly namespace?: string;
readonly version?: string;
readonly path?: string;
/** `false` models Cloudflare D1: no interactive transactions, each statement auto-commits. */
readonly interactiveTransactions?: boolean;
/** Awaited before every statement reaches the engine, so a test can hold
* statements back and force a chosen interleaving between callers. */
readonly statementGate?: (sql: string) => Promise<void>;
}

export const createSqliteTestFumaDb = async <const TTables extends FumaTables>(
Expand All @@ -46,6 +51,22 @@ export const createSqliteTestFumaDb = async <const TTables extends FumaTables>(
!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<Client["execute"]>[0]) => {
await gate(sqlOf(statement));
return execute(statement);
}) as Client["execute"];
client.batch = (async (...args: Parameters<Client["batch"]>) => {
const first = args[0][0];
if (first !== undefined) await gate(sqlOf(first));
return batch(...args);
}) as Client["batch"];
}

const schema = createDrizzleRuntimeSchemaFromTables({
tables: options.tables,
Expand All @@ -69,6 +90,7 @@ export const createSqliteTestFumaDb = async <const TTables extends FumaTables>(
namespace,
version,
provider: "sqlite",
interactiveTransactions: options.interactiveTransactions,
});

return {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/sdk/src/test-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const makeLazyTestFumaDb = (options: {
readonly tables: ReturnType<typeof collectTables>;
readonly backend: TestDatabaseBackend;
readonly dataDir?: string;
readonly interactiveTransactions?: boolean;
readonly statementGate?: (sql: string) => Promise<void>;
}): TestFumaDb => {
let started: Promise<SqliteTestFumaDb> | undefined;
const start = () => {
Expand All @@ -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,
}),
);
}
Expand Down Expand Up @@ -126,6 +130,10 @@ export type TestConfigOptions<TPlugins extends readonly AnyPlugin[] = readonly [
readonly plugins?: TPlugins;
readonly backend?: TestDatabaseBackend;
readonly dataDir?: string;
/** `false` models Cloudflare D1: transactions are not atomic. */
readonly interactiveTransactions?: boolean;
/** Awaited before every SQL statement; lets a test force an interleaving. */
readonly statementGate?: (sql: string) => Promise<void>;
readonly coreTools?: ExecutorConfig<TPlugins>["coreTools"];
/** Override the OAuth callback URL. Pass `null` to construct an executor with
* no OAuth callback (exercises the fail-loud redirect path). */
Expand Down Expand Up @@ -157,6 +165,8 @@ export const makeTestConfig = <const TPlugins extends readonly AnyPlugin[] = rea
tables,
backend: options?.backend ?? "sqlite",
dataDir: options?.dataDir,
interactiveTransactions: options?.interactiveTransactions,
statementGate: options?.statementGate,
});
const db = withQueryContext(testDb.db, {
tenant,
Expand Down
Loading