fix(plugins): keep loading gate when cached manifests include a /chat override

The sessionStorage seed set loading=false whenever any cache existed, which
defeats App.tsx's load-bearing pluginsLoading gate: with a cached manifest
that declares tab.override === "/chat", the persistent ChatPage host must
NOT mount before plugins resolve, or it spawns a PTY and gets yanked when
the override plugin takes over the route.

Seed loading=false from the cache only when no cached manifest overrides
/chat (canSeedLoadedFromCache); manifests are still seeded either way so
plugin routes register synchronously on refresh. Adds focused tests for
the gate, including the /chat-override case.
This commit is contained in:
kshitij
2026-08-03 18:11:51 +05:30
parent 41bba3c13e
commit c0c45ab68a
2 changed files with 66 additions and 1 deletions
+42
View File
@@ -2,6 +2,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import {
getCachedManifests,
cacheManifests,
canSeedLoadedFromCache,
MANIFEST_CACHE_KEY,
} from "./usePlugins";
import type { PluginManifest } from "./types";
@@ -100,3 +101,44 @@ describe("plugin manifest cache helpers", () => {
expect(() => cacheManifests([exampleManifest])).not.toThrow();
});
});
describe("canSeedLoadedFromCache (loading seed gate)", () => {
it("returns false when there is no cache (first visit keeps loading=true)", () => {
expect(canSeedLoadedFromCache(null)).toBe(false);
});
it("returns true for an empty cached list", () => {
expect(canSeedLoadedFromCache([])).toBe(true);
});
it("returns true when no cached manifest overrides /chat", () => {
const list: PluginManifest[] = [
exampleManifest,
{
...exampleManifest,
name: "other",
tab: { path: "/other", override: "/skills" },
},
];
expect(canSeedLoadedFromCache(list)).toBe(true);
});
it("returns false when a cached manifest overrides /chat — loading must stay true so App.tsx's pluginsLoading gate keeps the persistent chat host unmounted", () => {
const list: PluginManifest[] = [
exampleManifest,
{
...exampleManifest,
name: "chat-replacer",
tab: { path: "/chat-alt", override: "/chat" },
},
];
expect(canSeedLoadedFromCache(list)).toBe(false);
});
it("tolerates malformed cached entries missing a tab object", () => {
const malformed = [
{ ...exampleManifest, tab: undefined },
] as unknown as PluginManifest[];
expect(canSeedLoadedFromCache(malformed)).toBe(true);
});
});
+24 -1
View File
@@ -38,6 +38,25 @@ export function cacheManifests(manifests: PluginManifest[]): void {
}
}
/**
* Whether it is safe to skip the initial plugin-loading gate for a set of
* cached manifests.
*
* App.tsx waits on `pluginsLoading` before mounting the persistent ChatPage
* host: if a plugin overrides /chat (`tab.override === "/chat"`), mounting
* the built-in chat first would spawn a PTY and then yank it out from under
* the user when the plugin resolves. That gate is load-bearing — so we may
* only seed `loading = false` from the cache when no cached manifest
* declares a /chat override. Manifests are still seeded either way; only
* the loading flag stays conservative.
*/
export function canSeedLoadedFromCache(
cached: PluginManifest[] | null,
): boolean {
if (cached === null) return false;
return !cached.some((m) => m.tab?.override === "/chat");
}
export function usePlugins() {
// Lazy initialisers run once at mount — safe to read sessionStorage here.
// This avoids the "cannot access ref during render" lint error that would
@@ -51,8 +70,12 @@ export function usePlugins() {
// registered synchronously on the first render after a refresh.
// The catch-all in App.tsx is only a safety net for the very first visit
// (no cache yet). On subsequent visits this flag starts false immediately.
//
// Exception: if any cached manifest overrides /chat we must keep
// loading=true — App.tsx's pluginsLoading gate around the persistent
// ChatPage host is load-bearing (see canSeedLoadedFromCache).
const [loading, setLoading] = useState<boolean>(
() => getCachedManifests() === null,
() => !canSeedLoadedFromCache(getCachedManifests()),
);
const loadedScripts = useRef<Set<string>>(new Set());