repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

fix: share lazy imports across concurrent extension calls

e2061b3ef0f1410128099808207769a21154ff54

Oliver Jan Krylow <oliver@bugabinga.net> ·

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (8)

extensions/angel/index.ts

Modified
378 378 return normalized.length <= 72 ? normalized : `${normalized.slice(0, 69)}...`;
381 +// Shared so concurrent consultations evaluate ./advisor.ts once.
382 +// Pi's jiti loader (module cache disabled) can otherwise hand one caller a
383 +// partially initialized module whose imports are still undefined.
384 +let advisorModule: Promise<typeof import("./advisor.ts")> | undefined;
385 +const loadAdvisor = (): Promise<typeof import("./advisor.ts")> => {
386 + advisorModule ??= import("./advisor.ts");
387 + return advisorModule;
381 390 async function realRunConsultation(
382 391 ctx: ExtensionContext,
383 392 request: ConsultationRequest,
384 393 options: RunConsultationOptions,
385 394 ): Promise<ConsultationResult> {
386 - const implementation = await import("./advisor.ts");
395 + const implementation = await loadAdvisor();
387 396 return implementation.runConsultation(ctx, request, options);

extensions/git-safe/index.ts

Modified
36 36 import { isMarkerPresent } from "./marker.js";
37 37 import { createMarkers, wrapUntrusted } from "./spotlight.js";
39 +// Shared so parallel git_clone_safe calls evaluate ./clone.js once.
40 +// Pi's jiti loader (module cache disabled) can otherwise hand one caller a
41 +// partially initialized module whose imports are still undefined.
42 +let cloneModule: Promise<typeof import("./clone.js")> | undefined;
43 +const loadClone = (): Promise<typeof import("./clone.js")> => {
44 + cloneModule ??= import("./clone.js");
45 + return cloneModule;
46 +};
39 48 // ── Git-specific spotlight preamble ────────────────────────────────────────
41 50 const GIT_PREAMBLE = [
285 - const { safeClone } = await import("./clone.js");
294 + const { safeClone } = await loadClone();
286 295 const result = await safeClone(params.url, params.path, {
287 296 branch: params.branch,
288 297 sparse: params.sparse,

extensions/strata/implementation.ts

Modified
29 29 const emptyDraft = (): Draft => ({ reviewed: [], findings: [], notes: "" });
31 +// Shared so overlapping review flows evaluate ./github.js once.
32 +// Pi's jiti loader (module cache disabled) can otherwise hand one caller a
33 +// partially initialized module whose imports are still undefined.
34 +let githubModule: Promise<typeof import("./github.js")> | undefined;
35 +const loadGithub = (): Promise<typeof import("./github.js")> => {
36 + githubModule ??= import("./github.js");
37 + return githubModule;
38 +};
31 40 export class Strata {
32 41 private controller?: AbortController;
33 42 private server?: ReviewServer;
73 82 let forge: ForgeReview | undefined;
74 83 let source: Source;
75 84 if (request.kind === "pr") {
76 - github = await import("./github.js");
85 + github = await loadGithub();
77 86 forge = await github.openGitHubReview(
78 87 ctx.cwd,
79 88 request.identifier,

extensions/strata/index.ts

Modified
8 8 let runtime: Strata | undefined;
9 9 let generation = 0;
10 10 const openings = new Set<Promise<void>>();
11 + // Shared so repeated invocations evaluate ./implementation.js once.
12 + // Pi's jiti loader (module cache disabled) can otherwise hand one caller a
13 + // partially initialized module whose imports are still undefined.
14 + let implementationModule:
15 + | Promise<typeof import("./implementation.js")>
16 + | undefined;
17 + const loadImplementation = (): Promise<
18 + typeof import("./implementation.js")
19 + > => {
20 + implementationModule ??= import("./implementation.js");
21 + return implementationModule;
22 + };
11 23 pi.on("session_start", (_event, ctx) => {
12 24 const version = ++generation;
13 25 void hasGit().then((available) => {
35 47 return;
37 49 const version = generation;
38 - const { Strata } = await import("./implementation.js");
50 + const { Strata } = await loadImplementation();
39 51 if (generation !== version) return;
40 52 runtime ??= new Strata(pi);
41 53 const opening = runtime.open(args, ctx);

extensions/usage/index.ts

Modified
1 1 import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
3 +// Shared so repeated invocations evaluate ./implementation.js once.
4 +// Pi's jiti loader (module cache disabled) can otherwise hand one caller a
5 +// partially initialized module whose imports are still undefined.
6 +let implementationModule:
7 + | Promise<typeof import("./implementation.js")>
8 + | undefined;
9 +const loadImplementation = (): Promise<
10 + typeof import("./implementation.js")
11 +> => {
12 + implementationModule ??= import("./implementation.js");
13 + return implementationModule;
14 +};
3 16 export default function usageExtension(pi: ExtensionAPI): void {
4 17 pi.registerCommand("usage", {
5 18 description: "Show usage statistics dashboard",
6 19 handler: async (args, ctx) =>
7 - (await import("./implementation.js")).showUsage(args, ctx),
20 + (await loadImplementation()).showUsage(args, ctx),

extensions/web/__tests__/lazy.test.ts

Added
new file mode 100644
1 +import { describe, expect, it } from "vitest";
2 +import { lazy } from "../lazy";
4 +describe("lazy", () => {
5 + it("shares one evaluation among concurrent callers", async () => {
6 + let calls = 0;
7 + let resolveLoad: (value: { ok: boolean }) => void = () => {};
8 + const load = lazy(() => {
9 + calls++;
10 + return new Promise<{ ok: boolean }>((resolve) => {
11 + resolveLoad = resolve;
12 + });
13 + });
15 + const first = load();
16 + const second = load();
17 + resolveLoad({ ok: true });
19 + await expect(first).resolves.toEqual({ ok: true });
20 + await expect(second).resolves.toEqual({ ok: true });
21 + expect(calls).toBe(1);
22 + });
24 + it("re-evaluates after a failed load", async () => {
25 + let calls = 0;
26 + const load = lazy(async () => {
27 + calls++;
28 + if (calls === 1) throw new Error("boom");
29 + return calls;
30 + });
32 + await expect(load()).rejects.toThrow("boom");
33 + await expect(load()).resolves.toBe(2);
34 + });
35 +});

extensions/web/index.ts

Modified
32 32 DEFAULT_TIMEOUT_MS,
33 33 type SearchResult,
34 34 } from "./constants.js";
35 +import { lazy } from "./lazy.js";
35 36 import { checkToolAvailability, spawnToText, TOOL_HINTS } from "./spawn.js";
36 37 import {
37 38 createMarkers,
39 40 wrapUntrusted,
40 41 } from "./spotlight.js";
43 +// ── Lazy implementation modules ───────────────────────────────────────────
44 +// Loaded on first tool execution to keep extension startup fast.
45 +// The shared promise also prevents jiti (pi's loader, module cache disabled)
46 +// from handing concurrent tool calls a partially initialized module.
47 +const loadFetch = lazy(() => import("./fetch.js"));
48 +const loadExtract = lazy(() => import("./extract.js"));
49 +const loadPandoc = lazy(() => import("./pandoc.js"));
50 +const loadStore = lazy(() => import("./store.js"));
51 +const loadLineMatch = lazy(() => import("./line-match.js"));
52 +const loadMap = lazy(() => import("./map.js"));
53 +const loadSearch = lazy(() => import("./search.js"));
42 55 // ── Shared formatters ────────────────────────────────────────────────────
44 57 function fmtSize(bytes: number): string {
216 229 const dir = await createTempDir("pi-web-");
217 230 const tmpPath = join(dir, `input.${fromFormat}`);
218 231 await writeFile(tmpPath, data);
219 - const { runPandocFile } = await import("./pandoc.js");
232 + const { runPandocFile } = await loadPandoc();
220 233 const text = await runPandocFile(tmpPath, fromFormat, "plain");
221 234 return { text, tmpPath };
581 594 if (hint) throw new Error(hint);
583 596 const [{ extractMainContent }, { runPandoc }] = await Promise.all([
584 - import("./extract.js"),
585 - import("./pandoc.js"),
597 + loadExtract(),
598 + loadPandoc(),
587 600 const extracted = extractMainContent(body.toString("utf-8"));
588 601 let md: string;
1029 1042 let contentType: string;
1030 1043 let finalUrl = params.url;
1032 - const { fetchUrlStream } = await import("./fetch.js");
1045 + const { fetchUrlStream } = await loadFetch();
1033 1046 const result = await fetchUrlStream(params.url, onProgress, signal, {
1034 1047 maxBytes: params.maxBytes,
1035 1048 timeoutMs: params.timeout,
1077 1090 const fullOutput = processed.output;
1078 1091 const [{ saveJsonResult }, { filterLineMatches }] = await Promise.all([
1079 - import("./store.js"),
1080 - import("./line-match.js"),
1092 + loadStore(),
1093 + loadLineMatch(),
1082 1095 const stored = await saveJsonResult("web", ctx?.cwd ?? process.cwd(), {
1083 1096 url: params.url,
1229 1242 const [{ discoverSiteMap }, { saveJsonResult }] = await Promise.all([
1230 - import("./map.js"),
1231 - import("./store.js"),
1243 + loadMap(),
1244 + loadStore(),
1233 1246 const map = await discoverSiteMap(
1234 1247 params.url,
1366 1379 details: {},
1369 - const { searchWeb } = await import("./search.js");
1382 + const { searchWeb } = await loadSearch();
1370 1383 const { results, source } = await searchWeb({
1371 1384 query: params.query,

extensions/web/lazy.ts

Added
new file mode 100644
1 +/**
2 + * Memoize a dynamic import so concurrent callers share one evaluation.
3 + *
4 + * Pi loads extensions through jiti with the module cache disabled.
5 + * Two concurrent imports of the same module can then race: the second
6 + * caller receives a partially initialized module whose top-level awaits
7 + * have not finished and whose imported bindings are still `undefined`
8 + * (for example `Cannot read properties of undefined (reading
9 + * 'isCodexAvailable')` from parallel web searches).
10 + *
11 + * Routing every lazy import through one shared promise closes that window.
12 + * A rejected load clears the memo so the next call retries.
13 + */
14 +export function lazy<T>(load: () => Promise<T>): () => Promise<T> {
15 + let promise: Promise<T> | undefined;
16 + return () =>
17 + (promise ??= load().catch((error: unknown) => {
18 + promise = undefined;
19 + throw error;
20 + }));