@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 454 lines 17.3 KB
Raw
1 import {
2 EnvironmentId,
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ServerProvider,
6 } from "@t3tools/contracts";
7 import * as Effect from "effect/Effect";
8 import { Atom, AtomRegistry } from "effect/reactivity";
9 import { afterEach, describe, expect, it, vi } from "vite-plus/test";
10
11 import {
12 createProviderWorkspaceSnapshotRefreshAtomFamily,
13 getProviderSkillsForSlashMenu,
14 hasCompleteProviderWorkspaceSnapshot,
15 isProviderWorkspaceSnapshotScanning,
16 resolveProviderSkillsForCwd,
17 resolveProviderSlashCommandsForCwd,
18 resolveProvidersForCwd,
19 } from "./providerSkills.ts";
20
21 const provider = {
22 instanceId: ProviderInstanceId.make("codex"),
23 driver: ProviderDriverKind.make("codex"),
24 enabled: true,
25 installed: true,
26 version: "1.0.0",
27 status: "ready",
28 auth: { status: "authenticated" },
29 checkedAt: "2026-01-01T00:00:00.000Z",
30 models: [],
31 slashCommands: [{ name: "global" }],
32 skills: [{ name: "global", path: "/global/SKILL.md", enabled: true }],
33 workspaceSnapshots: [
34 {
35 cwd: "/workspace/project-a",
36 checkedAt: "2026-01-01T00:01:00.000Z",
37 slashCommands: [{ name: "project" }],
38 skills: [{ name: "project", path: "/workspace/project-a/SKILL.md", enabled: true }],
39 },
40 ],
41 } satisfies ServerProvider;
42
43 describe("getProviderSkillsForSlashMenu", () => {
44 it("shows one row when enabled skills share a name", () => {
45 const skills = [
46 {
47 name: "babysit-pr",
48 path: "/Users/matt/.codex/skills/babysit-pr/SKILL.md",
49 enabled: true,
50 },
51 {
52 name: "browser",
53 path: "/Users/matt/.agents/skills/browser/SKILL.md",
54 enabled: true,
55 },
56 {
57 name: "babysit-pr",
58 path: "/Users/matt/.agents/skills/babysit-pr/SKILL.md",
59 enabled: true,
60 },
61 ];
62
63 expect(getProviderSkillsForSlashMenu(skills, true).map((skill) => skill.name)).toEqual([
64 "babysit-pr",
65 "browser",
66 ]);
67 });
68
69 it("keeps an enabled skill when a disabled duplicate appears first", () => {
70 const enabledSkill = {
71 name: "babysit-pr",
72 path: "/Users/matt/.agents/skills/babysit-pr/SKILL.md",
73 enabled: true,
74 };
75 const skills = [
76 {
77 name: "babysit-pr",
78 path: "/Users/matt/.codex/skills/babysit-pr/SKILL.md",
79 enabled: false,
80 },
81 enabledSkill,
82 ];
83
84 expect(getProviderSkillsForSlashMenu(skills, true)).toEqual([enabledSkill]);
85 });
86 });
87
88 describe("workspace provider snapshots", () => {
89 it("uses the cwd snapshot after a provider session has populated it", () => {
90 expect(resolveProviderSkillsForCwd(provider, "/workspace/project-a")).toEqual([
91 { name: "project", path: "/workspace/project-a/SKILL.md", enabled: true },
92 ]);
93 expect(resolveProviderSlashCommandsForCwd(provider, "/workspace/project-a")).toEqual([
94 { name: "project" },
95 ]);
96 });
97
98 it("keeps the machine snapshot before this cwd has a provider snapshot", () => {
99 expect(resolveProviderSkillsForCwd(provider, "/workspace/project-b")).toEqual(provider.skills);
100 expect(resolveProviderSlashCommandsForCwd(provider, null)).toEqual(provider.slashCommands);
101 });
102
103 it("uses partial workspace skills and commands while keeping discovery retryable", () => {
104 const partial = {
105 ...provider,
106 workspaceSnapshots: provider.workspaceSnapshots.map((snapshot) => ({
107 ...snapshot,
108 slashCommands: [{ name: "compact" }],
109 slashCommandsPending: true,
110 })),
111 } satisfies ServerProvider;
112 expect(resolveProviderSkillsForCwd(partial, "/workspace/project-a")).toEqual(
113 provider.workspaceSnapshots[0]?.skills,
114 );
115 expect(resolveProviderSlashCommandsForCwd(partial, "/workspace/project-a")).toEqual([
116 { name: "compact" },
117 ]);
118 expect(hasCompleteProviderWorkspaceSnapshot(partial, "/workspace/project-a")).toBe(false);
119 expect(hasCompleteProviderWorkspaceSnapshot(provider, "/workspace/project-a")).toBe(true);
120 expect(hasCompleteProviderWorkspaceSnapshot(provider, "/workspace/project-b")).toBe(false);
121 expect(hasCompleteProviderWorkspaceSnapshot(undefined, "/workspace/project-a")).toBe(false);
122 expect(hasCompleteProviderWorkspaceSnapshot(provider, null)).toBe(false);
123 });
124
125 it("lists a project's own models only for that project", () => {
126 const projectModel = {
127 slug: "uni/medium-gpt",
128 name: "Medium GPT",
129 isCustom: false,
130 capabilities: null,
131 } satisfies ServerProvider["models"][number];
132 const withProjectModels = {
133 ...provider,
134 workspaceSnapshots: provider.workspaceSnapshots.map((snapshot) => ({
135 ...snapshot,
136 models: [projectModel],
137 })),
138 } satisfies ServerProvider;
139 const providers = [provider, withProjectModels];
140
141 expect(resolveProvidersForCwd(providers, "/workspace/project-a").map((p) => p.models)).toEqual([
142 [],
143 [projectModel],
144 ]);
145 expect(resolveProvidersForCwd(providers, "/workspace/project-a")[0]).toBe(provider);
146 expect(resolveProvidersForCwd(providers, "/workspace/project-b")).toBe(providers);
147 });
148 });
149
150 describe("provider workspace snapshot refresh atom", () => {
151 afterEach(() => {
152 vi.useRealTimers();
153 });
154
155 const cwd = "/workspace/project-a";
156 const environmentId = EnvironmentId.make("env");
157 const withSnapshot = (
158 checkedAt: string,
159 slashCommandsPending?: boolean,
160 ): ReadonlyArray<ServerProvider> => [
161 {
162 ...provider,
163 workspaceSnapshots: [
164 {
165 ...provider.workspaceSnapshots[0]!,
166 checkedAt,
167 ...(slashCommandsPending ? { slashCommandsPending } : {}),
168 },
169 ],
170 },
171 ];
172 const noSnapshot: ReadonlyArray<ServerProvider> = [{ ...provider, workspaceSnapshots: [] }];
173
174 type Response = () =>
175 | ReadonlyArray<ServerProvider>
176 | null
177 | Promise<ReadonlyArray<ServerProvider> | null>;
178
179 function setup(
180 initialProviders: ReadonlyArray<ServerProvider>,
181 initialResponses?: Response[],
182 // The client drops a reply whose provider changed while it was in flight.
183 options?: { readonly droppedReplies?: number },
184 ) {
185 let droppedReplies = options?.droppedReplies ?? 0;
186 const providersAtom = Atom.make<ReadonlyArray<ServerProvider> | null>(initialProviders);
187 const responses: Response[] = [...(initialResponses ?? [])];
188 const run = vi.fn(() =>
189 Effect.promise(async () => (responses.shift() ?? (() => null))()).pipe(
190 Effect.flatMap((providers) => {
191 if (providers === null) return Effect.fail("offline");
192 if (droppedReplies > 0) droppedReplies -= 1;
193 else registry.set(providersAtom, providers);
194 return Effect.succeed({ providers });
195 }),
196 ),
197 );
198 const family = createProviderWorkspaceSnapshotRefreshAtomFamily({
199 providers: () => providersAtom,
200 refreshProviders: { label: "refresh", run },
201 });
202 const registry = AtomRegistry.make();
203 const atom = family({ environmentId, instanceId: provider.instanceId, cwd });
204 const unmount = registry.mount(atom);
205 return { registry, atom, family, providersAtom, responses, run, unmount };
206 }
207
208 it("scans a missing snapshot once and skips a repeat inside the TTL", async () => {
209 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
210 const { registry, atom, family, responses, run, unmount } = setup(noSnapshot);
211 // A second composer on the same target shares the scan.
212 registry.mount(family({ environmentId, instanceId: provider.instanceId, cwd }))();
213 expect(run).toHaveBeenCalledTimes(1);
214 expect(run).toHaveBeenCalledWith(expect.anything(), {
215 environmentId,
216 input: { instanceId: provider.instanceId, cwd },
217 });
218 responses.push(() => withSnapshot("2026-01-01T00:00:00.000Z"));
219 await vi.advanceTimersByTimeAsync(0);
220 registry.refresh(atom);
221 expect(run).toHaveBeenCalledTimes(1);
222 unmount();
223 });
224
225 it.each(["already expired", "expires during discovery"])(
226 "retries an unchanged catalog that %s after the short cooldown",
227 async (expiry) => {
228 const start = Date.parse("2026-01-01T00:05:00.000Z");
229 vi.useFakeTimers({ now: expiry === "expires during discovery" ? start - 1 : start });
230 const stale = withSnapshot("2026-01-01T00:00:00.000Z");
231 const { registry, atom, responses, run, unmount } = setup(
232 expiry === "expires during discovery"
233 ? withSnapshot("2026-01-01T00:00:00.000Z", true)
234 : stale,
235 [
236 () => {
237 if (expiry === "expires during discovery") vi.setSystemTime(start + 1);
238 return stale;
239 },
240 ],
241 );
242 await vi.advanceTimersByTimeAsync(0);
243 await vi.advanceTimersByTimeAsync(9_999);
244 registry.refresh(atom);
245 expect(run).toHaveBeenCalledTimes(1);
246 responses.push(() => withSnapshot("2026-01-01T00:05:10.000Z"));
247 await vi.advanceTimersByTimeAsync(1);
248 registry.refresh(atom);
249 expect(run).toHaveBeenCalledTimes(2);
250 unmount();
251 },
252 );
253
254 it("asks again at once when the client dropped a complete reply", async () => {
255 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
256 const scanned = withSnapshot("2026-01-01T00:00:00.000Z");
257 const { registry, atom, run, unmount } = setup(noSnapshot, [() => scanned, () => scanned], {
258 droppedReplies: 1,
259 });
260 await vi.advanceTimersByTimeAsync(0);
261 expect(run).toHaveBeenCalledTimes(2);
262 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
263 registry.refresh(atom);
264 expect(run).toHaveBeenCalledTimes(2);
265 unmount();
266 });
267
268 it("keeps the TTL guard for a newly scanned catalog when the client clock is ahead", async () => {
269 vi.useFakeTimers({ now: Date.parse("2026-01-01T01:00:00.000Z") });
270 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z"), [
271 () => withSnapshot("2026-01-01T00:01:00.000Z"),
272 ]);
273 await vi.advanceTimersByTimeAsync(10_000);
274 registry.refresh(atom);
275 expect(run).toHaveBeenCalledTimes(1);
276 unmount();
277 });
278
279 it("bounds retries when discovery repeatedly returns the same expired catalog", async () => {
280 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:05:00.000Z") });
281 const stale = withSnapshot("2026-01-01T00:00:00.000Z");
282 const { registry, atom, run, unmount } = setup(stale, [() => stale, () => stale, () => stale]);
283 await vi.advanceTimersByTimeAsync(0);
284 await vi.advanceTimersByTimeAsync(10_000);
285 registry.refresh(atom);
286 await vi.advanceTimersByTimeAsync(0);
287 await vi.advanceTimersByTimeAsync(10_000);
288 registry.refresh(atom);
289 await vi.advanceTimersByTimeAsync(0);
290 await vi.advanceTimersByTimeAsync(10_000);
291 registry.refresh(atom);
292 expect(run).toHaveBeenCalledTimes(3);
293 unmount();
294 });
295
296 it("does not rescan a current snapshot, and rescans once it ages past the TTL", () => {
297 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:01:00.000Z") });
298 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z"));
299 expect(run).not.toHaveBeenCalled();
300 vi.setSystemTime(Date.parse("2026-01-01T00:05:00.000Z"));
301 registry.refresh(atom);
302 expect(run).toHaveBeenCalledTimes(1);
303 unmount();
304 });
305
306 it("cools down a failed scan, then retries on its own while the scan stays pending", async () => {
307 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
308 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z", true));
309 expect(run).toHaveBeenCalledTimes(1);
310 await vi.advanceTimersByTimeAsync(0);
311 registry.refresh(atom);
312 expect(run).toHaveBeenCalledTimes(1);
313 await vi.advanceTimersByTimeAsync(9_999);
314 expect(run).toHaveBeenCalledTimes(1);
315 await vi.advanceTimersByTimeAsync(1);
316 expect(run).toHaveBeenCalledTimes(2);
317 unmount();
318 await vi.advanceTimersByTimeAsync(20_000);
319 expect(run).toHaveBeenCalledTimes(2);
320 expect(vi.getTimerCount()).toBe(0);
321 });
322
323 it("rescans a current snapshot whose commands are complete but models are pending", () => {
324 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
325 const [current] = withSnapshot("2026-01-01T00:00:00.000Z");
326 const { run, unmount } = setup([
327 {
328 ...current!,
329 workspaceSnapshots: current!.workspaceSnapshots!.map((snapshot) => ({
330 ...snapshot,
331 modelsPending: true,
332 })),
333 },
334 ]);
335 expect(run).toHaveBeenCalledTimes(1);
336 unmount();
337 });
338
339 it("stops retrying after three failed scans until the TTL lapses", async () => {
340 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
341 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z", true));
342 await vi.advanceTimersByTimeAsync(60_000);
343 expect(run).toHaveBeenCalledTimes(3);
344 registry.refresh(atom);
345 await vi.advanceTimersByTimeAsync(0);
346 expect(run).toHaveBeenCalledTimes(3);
347 expect(vi.getTimerCount()).toBe(0);
348 vi.setSystemTime(Date.parse("2026-01-01T00:10:00.000Z"));
349 registry.refresh(atom);
350 expect(run).toHaveBeenCalledTimes(4);
351 unmount();
352 });
353
354 it("reports skills loading while a missing snapshot is scanned, until it arrives", async () => {
355 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
356 const scanned = withSnapshot("2026-01-01T00:00:00.000Z");
357 const { registry, atom, providersAtom, unmount } = setup(noSnapshot, [() => scanned]);
358 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(true);
359 registry.set(providersAtom, scanned);
360 await vi.advanceTimersByTimeAsync(0);
361 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
362 unmount();
363 });
364
365 it("stops reporting skills loading when the scan fails", async () => {
366 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
367 const { registry, atom, unmount } = setup(noSnapshot);
368 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(true);
369 await vi.advanceTimersByTimeAsync(0);
370 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
371 unmount();
372 });
373
374 it("keeps reporting skills loading while the scan returns a pending snapshot", async () => {
375 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
376 const pending = withSnapshot("2026-01-01T00:00:00.000Z", true);
377 const { registry, atom, providersAtom, unmount } = setup(pending, [() => pending]);
378 await vi.advanceTimersByTimeAsync(0);
379 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(true);
380 registry.set(providersAtom, withSnapshot("2026-01-01T00:00:00.000Z"));
381 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
382 unmount();
383 });
384
385 it("stops reporting skills loading once a pending snapshot exhausts its retries", async () => {
386 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
387 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z", true));
388 await vi.advanceTimersByTimeAsync(60_000);
389 expect(run).toHaveBeenCalledTimes(3);
390 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
391 unmount();
392 });
393
394 it("does not report skills loading for a current snapshot", () => {
395 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:01:00.000Z") });
396 const { registry, atom, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z"));
397 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
398 unmount();
399 });
400
401 it("does not retry on a timer when the failed scan left no pending snapshot", async () => {
402 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
403 const { registry, atom, run, unmount } = setup(noSnapshot);
404 await vi.advanceTimersByTimeAsync(10_000);
405 expect(run).toHaveBeenCalledTimes(1);
406 registry.refresh(atom);
407 expect(run).toHaveBeenCalledTimes(2);
408 unmount();
409 });
410
411 it("rescans at once when the server drops the snapshot, lifting TTL and cooldown", async () => {
412 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
413 const { registry, providersAtom, run, unmount } = setup(noSnapshot);
414 await vi.advanceTimersByTimeAsync(0);
415 expect(run).toHaveBeenCalledTimes(1);
416 registry.set(providersAtom, withSnapshot("2026-01-01T00:00:00.000Z"));
417 expect(run).toHaveBeenCalledTimes(1);
418 registry.set(providersAtom, noSnapshot);
419 expect(run).toHaveBeenCalledTimes(2);
420 unmount();
421 });
422
423 it("does not scan again when a scan fails after the last subscriber left", async () => {
424 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
425 let release = () => {};
426 const gate = new Promise<void>((resolve) => {
427 release = resolve;
428 });
429 const { run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z", true), [
430 async () => {
431 await gate;
432 return null;
433 },
434 ]);
435 expect(run).toHaveBeenCalledTimes(1);
436 unmount();
437 await vi.advanceTimersByTimeAsync(0);
438 release();
439 await vi.advanceTimersByTimeAsync(20_000);
440 expect(run).toHaveBeenCalledTimes(1);
441 expect(vi.getTimerCount()).toBe(0);
442 });
443
444 it("ignores provider updates that leave the snapshot status unchanged", () => {
445 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:01:00.000Z") });
446 const { registry, providersAtom, run, unmount } = setup(
447 withSnapshot("2026-01-01T00:00:00.000Z"),
448 );
449 vi.setSystemTime(Date.parse("2026-01-01T00:06:00.000Z"));
450 registry.set(providersAtom, withSnapshot("2026-01-01T00:00:00.000Z"));
451 expect(run).not.toHaveBeenCalled();
452 unmount();
453 });
454 });