Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(picker): keep a load that lands after the user steps back

Jack Rudenko101933ea2464
diff --git a/packages/cli/src/picker/ModelPicker.test.tsx b/packages/cli/src/picker/ModelPicker.test.tsx
index b22ba88a..058aea36 100644
--- a/packages/cli/src/picker/ModelPicker.test.tsx
+++ b/packages/cli/src/picker/ModelPicker.test.tsx
@@ -79,6 +79,8 @@ interface FakeOpts {
descriptions?: Record<string, string>;
/** Built-in local providers the profile config has not opted into. */
notEnabledLocal?: string[];
+ /** Delay the catalog and description loads without leaving them pending forever. */
+ loadDelayMs?: number;
/** Never settles — the in-flight states. */
hang?: boolean;
}
@@ -106,7 +108,11 @@ function fakeSource(opts: FakeOpts = {}): PickerDataSource & { calls: Calls } {
},
ensureCatalog: (): Promise<void> => {
calls.catalog++;
- return opts.hang ? never : Promise.resolve();
+ return opts.hang
+ ? never
+ : opts.loadDelayMs === undefined
+ ? Promise.resolve()
+ : new Promise((resolve) => setTimeout(resolve, opts.loadDelayMs));
},
servedModels: (p) => opts.byProvider?.[p] ?? opts.served ?? [model()],
discoverModelsCatalog: (p): Promise<PickerDiscoveryOutcome> => {
@@ -120,12 +126,15 @@ function fakeSource(opts: FakeOpts = {}): PickerDataSource & { calls: Calls } {
},
descriptions: (): Promise<DescriptionIndex> => {
calls.descriptions++;
+ const index = {
+ get: (id: string) => descriptions[id],
+ size: Object.keys(descriptions).length,
+ };
return opts.hang
? never
- : Promise.resolve({
- get: (id: string) => descriptions[id],
- size: Object.keys(descriptions).length,
- });
+ : opts.loadDelayMs === undefined
+ ? Promise.resolve(index)
+ : new Promise((resolve) => setTimeout(() => resolve(index), opts.loadDelayMs));
},
};
}
@@ -1515,6 +1524,30 @@ describe("a fallback list says it is a fallback — and that it may not work", (
// ── what the screen says while it is waiting ─────────────────────────────────────
describe("the loading states", () => {
+ test("leaving the model view mid-load does not strand it — `a`, `esc`, `a` reaches the list", async () => {
+ // Both one-shot loads used to discard their result when Escape disabled the model
+ // view, leaving the second visit stuck on `fetching` with no request left to make.
+ const source = fakeSource({
+ loadDelayMs: 300,
+ served: [model({ id: "a" })],
+ descriptions: { a: "A sentence about model a." },
+ });
+ const d = await draw(<ModelPicker source={source} onDone={() => {}} />);
+ try {
+ await d.press(["a"]);
+ await d.press(["ESCAPE"]);
+ await new Promise((resolve) => setTimeout(resolve, 600));
+ await d.press(["a"]);
+ await d.until(listPainted);
+ const frame = d.recapture().text;
+ expect(joined(frame)).not.toContain("fetching");
+ expect(source.calls.catalog).toBe(1);
+ expect(flat(frame)).toContain("A sentence about model a.");
+ } finally {
+ d.destroy();
+ }
+ });
+
test("startup names the ONE thing it is doing, and claims no other", async () => {
// The screen the owner rejected said `cloud catalog fetching…` beside a `0/11 providers`
// discovery meter, before he had asked for either. Startup now probes credentials
diff --git a/packages/cli/src/picker/hooks/useModelDescriptions.ts b/packages/cli/src/picker/hooks/useModelDescriptions.ts
index e2971c40..f8caa06f 100644
--- a/packages/cli/src/picker/hooks/useModelDescriptions.ts
+++ b/packages/cli/src/picker/hooks/useModelDescriptions.ts
@@ -19,6 +19,7 @@ import {
type DescriptionIndex,
emptyDescriptionIndex,
} from "../../providers/model-descriptions.js";
+import { useMounted } from "../../tui/hooks/useMounted.js";
import type { PickerDataSource } from "../PickerDataSource.js";
export function useModelDescriptions(
@@ -35,24 +36,24 @@ export function useModelDescriptions(
const [index, setIndex] = useState<DescriptionIndex>(emptyDescriptionIndex);
/** Asked for ONCE per picker open, however often `enabled` flips. */
const asked = useRef(false);
+ const mounted = useMounted();
useEffect(() => {
if (!enabled || asked.current) return;
asked.current = true;
- let live = true;
void source.descriptions().then(
(loaded) => {
- if (live) setIndex(loaded);
+ // Kept whenever it lands. It is asked for once, so dropping it because
+ // the user left the model view mid-fetch left every description blank
+ // for the rest of the run. Only an unmount discards it.
+ if (mounted.current) setIndex(loaded);
},
() => {
// Documented never to reject; guarded anyway. A missing sentence must
// never be able to take the picker down.
}
);
- return () => {
- live = false;
- };
- }, [source, enabled]);
+ }, [source, enabled, mounted]);
return index;
}
diff --git a/packages/cli/src/picker/hooks/usePickerModels.ts b/packages/cli/src/picker/hooks/usePickerModels.ts
index 712d09e8..4c0c7b5e 100644
--- a/packages/cli/src/picker/hooks/usePickerModels.ts
+++ b/packages/cli/src/picker/hooks/usePickerModels.ts
@@ -39,6 +39,7 @@ import {
resolveProviderDisplayPrice,
resolveProviderExternalId,
} from "../../model-selector.js";
+import { useMounted } from "../../tui/hooks/useMounted.js";
import type { PickerDataSource, PickerProviderChoice } from "../PickerDataSource.js";
import { priceLabel } from "../rows.js";
@@ -118,26 +119,28 @@ export function usePickerModels(
const cache = useRef(new Map<string, PickerRow[]>());
/** The warm is asked for ONCE per picker open, however often `enabled` flips. */
const warmed = useRef(false);
+ const mounted = useMounted();
useEffect(() => {
if (!enabled || warmed.current) return;
warmed.current = true;
- let live = true;
setPhase("loading");
void (async () => {
// Bounded, and documented never to throw. A cold cache is the only case
// this actually waits for; a warm one returns on the first line.
await source.ensureCatalog();
- if (!live) return;
+ // RECORDED WHENEVER IT LANDS, not only while the model view is showing.
+ // The warm is asked for once, so a result dropped because the user stepped
+ // back mid-fetch (`a`, `esc`, `a` on a cold cache) was never asked for
+ // again, and `cloud catalog fetching…` stayed on screen for the rest of the
+ // run with the catalog already on disk. Only an UNMOUNT discards it.
+ if (!mounted.current) return;
// The warm may have REPLACED the slim cache on disk, so anything derived
// from it before now is stale by construction.
cache.current.clear();
setPhase("ready");
})();
- return () => {
- live = false;
- };
- }, [source, enabled]);
+ }, [source, enabled, mounted]);
// The ready set is re-derived per render by `usePickerProviders`, so it cannot
// be a memo dependency directly — a new Set every frame would rebuild the whole
diff --git a/packages/cli/src/tui/hooks/useMounted.ts b/packages/cli/src/tui/hooks/useMounted.ts
new file mode 100644
index 00000000..17f1099d
--- /dev/null
+++ b/packages/cli/src/tui/hooks/useMounted.ts
@@ -0,0 +1,27 @@
+/**
+ * useMounted — a ref that reads `true` while the component is mounted, `false` after.
+ *
+ * For an async result that must survive the component's own state changes but not
+ * its unmount. The tempting alternative — a `let live = true` flipped by the
+ * effect's cleanup — is tied to the effect's DEPENDENCIES, so it fires on every
+ * dependency change, not only on unmount. That quietly turns "is anyone still here
+ * to receive this?" into "is the same view still showing?", and a result that is
+ * asked for only once is then dropped for good. The picker's catalog warm and its
+ * description index both did exactly that: step back mid-fetch, and the dialog
+ * said `fetching…` for the rest of the run with the data already on disk.
+ */
+
+import { type RefObject, useEffect, useRef } from "react";
+
+export function useMounted(): RefObject<boolean> {
+ const mounted = useRef(true);
+ useEffect(() => {
+ // Re-asserted on mount, so a renderer that mounts, unmounts and remounts the
+ // same instance does not leave the ref reading `false` while it is on screen.
+ mounted.current = true;
+ return () => {
+ mounted.current = false;
+ };
+ }, []);
+ return mounted;
+}