Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(llm): share one in-flight resolution of lazy tool registries
Elian Doran452e118f1de2
diff --git a/packages/trilium-core/src/services/llm/tools/index.spec.ts b/packages/trilium-core/src/services/llm/tools/index.spec.tsnew file mode 100644index 0000000000..e158f11040--- /dev/null+++ b/packages/trilium-core/src/services/llm/tools/index.spec.ts@@ -0,0 +1,85 @@+import { afterEach, describe, expect, it } from "vitest";+import { z } from "zod";++import { allToolRegistries, resolveToolRegistries } from "./index.js";+import { registerToolRegistryLoader } from "./registration.js";+import { defineTools, type ToolRegistry } from "./tool_registry.js";++function makeRegistry(name: string): ToolRegistry {+ return defineTools({+ [name]: {+ description: "test tool",+ inputSchema: z.object({}),+ execute: () => ({ ok: true })+ }+ });+}++describe("resolveToolRegistries", () => {+ const added: ToolRegistry[] = [];++ afterEach(() => {+ // allToolRegistries is a module singleton; leave it as this spec found it.+ for (const registry of added.splice(0, added.length)) {+ const index = allToolRegistries.indexOf(registry);+ if (index >= 0) {+ allToolRegistries.splice(index, 1);+ }+ }+ });++ it("loads a registered loader into allToolRegistries once", async () => {+ const registry = makeRegistry("once_tool");+ added.push(registry);+ registerToolRegistryLoader(async () => registry);++ await resolveToolRegistries();+ expect(allToolRegistries).toContain(registry);++ // A drained loader is gone; resolving again must not duplicate.+ await resolveToolRegistries();+ expect(allToolRegistries.filter((r) => r === registry)).toHaveLength(1);+ });++ it("makes a concurrent caller wait for the in-flight load instead of returning early", async () => {+ const registry = makeRegistry("race_tool");+ added.push(registry);+ let release: (r: ToolRegistry) => void = () => {};+ registerToolRegistryLoader(() => new Promise((resolve) => {+ release = resolve;+ }));++ // The first call drains the queue and blocks on the loader; the second+ // arrives while the queue is already empty — the shape of two+ // overlapping first-use chat/MCP requests.+ const first = resolveToolRegistries();+ const second = resolveToolRegistries();++ let secondSettled = false;+ void second.then(() => {+ secondSettled = true;+ });+ await new Promise((r) => setTimeout(r, 10));+ expect(secondSettled).toBe(false);+ expect(allToolRegistries).not.toContain(registry);++ release(registry);+ await Promise.all([first, second]);+ expect(allToolRegistries).toContain(registry);+ });++ it("picks up a loader registered after an earlier resolve completed", async () => {+ const early = makeRegistry("early_tool");+ const late = makeRegistry("late_tool");+ added.push(early, late);++ registerToolRegistryLoader(async () => early);+ await resolveToolRegistries();++ registerToolRegistryLoader(async () => late);+ await resolveToolRegistries();++ expect(allToolRegistries).toContain(early);+ expect(allToolRegistries).toContain(late);+ });+});diff --git a/packages/trilium-core/src/services/llm/tools/index.ts b/packages/trilium-core/src/services/llm/tools/index.tsindex 765bf6d4f8..ee82b492b2 100644--- a/packages/trilium-core/src/services/llm/tools/index.ts+++ b/packages/trilium-core/src/services/llm/tools/index.ts@@ -56,14 +56,42 @@ export function registerToolRegistry(registry: ToolRegistry) {}}+let inFlightResolve: Promise<void> | null = null;+/*** Loads every host-registered lazy registry into {@link allToolRegistries}.* The chat pipeline and the MCP server call this at the start of their async* entry points, which is what lets `buildTools()` and other readers stay* synchronous: by the time they iterate the array, it is complete.+ *+ * Concurrent callers share one in-flight resolution. Draining the queue+ * empties it before the imports finish, so without the shared promise a+ * second first-use request would see nothing left to load and proceed+ * against a still-incomplete array.*/-export async function resolveToolRegistries(): Promise<void> {- for (const loader of drainToolRegistryLoaders()) {- registerToolRegistry(await loader());+export function resolveToolRegistries(): Promise<void> {+ if (inFlightResolve) {+ return inFlightResolve;}++ // Draining before creating the promise keeps the empty case fully+ // synchronous. Routing it through the async closure below would let the+ // closure settle while its own creation is still being evaluated, and the+ // assignment would then store an already-settled promise that the+ // `finally` can never clear again.+ const loaders = drainToolRegistryLoaders();+ if (loaders.length === 0) {+ return Promise.resolve();+ }++ inFlightResolve = (async () => {+ try {+ for (const loader of loaders) {+ registerToolRegistry(await loader());+ }+ } finally {+ inFlightResolve = null;+ }+ })();+ return inFlightResolve;}