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

main
ts 260 lines 10.9 KB
Raw
1 import { describe, expect, it } from "@effect/vitest";
2 import { ProviderInstanceId } from "@t3tools/contracts";
3 import * as Deferred from "effect/Deferred";
4 import * as Effect from "effect/Effect";
5 import * as Fiber from "effect/Fiber";
6 import * as Option from "effect/Option";
7
8 import type { AcpRegistryAvailableCommands } from "./probe.ts";
9 import * as AcpRegistryRuntimeCoordinator from "./AcpRegistryRuntimeCoordinator.ts";
10
11 describe("AcpRegistryRuntimeCoordinator", () => {
12 it.effect("suppresses a background probe while foreground startup is active", () =>
13 Effect.gen(function* () {
14 const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator;
15 const foregroundStarted = yield* Deferred.make<void>();
16 const releaseForeground = yield* Deferred.make<void>();
17 const foreground = yield* coordinator
18 .withForegroundStartup(
19 "kilo",
20 Deferred.succeed(foregroundStarted, undefined).pipe(
21 Effect.andThen(Deferred.await(releaseForeground)),
22 ),
23 )
24 .pipe(Effect.forkChild);
25 yield* Deferred.await(foregroundStarted);
26
27 const probed = yield* coordinator.runBackgroundProbe("kilo", Effect.succeed("unexpected"));
28 expect(Option.isNone(probed)).toBe(true);
29
30 yield* Deferred.succeed(releaseForeground, undefined);
31 yield* Fiber.join(foreground);
32 }).pipe(Effect.provide(AcpRegistryRuntimeCoordinator.layer)),
33 );
34
35 it.effect("interrupts an active background probe when foreground startup begins", () =>
36 Effect.gen(function* () {
37 const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator;
38 const probeStarted = yield* Deferred.make<void>();
39 const probeFinalized = yield* Deferred.make<void>();
40 const releaseForeground = yield* Deferred.make<void>();
41 const probe = yield* coordinator
42 .runBackgroundProbe(
43 "kilo",
44 Deferred.succeed(probeStarted, undefined).pipe(
45 Effect.andThen(Effect.never),
46 Effect.ensuring(Deferred.succeed(probeFinalized, undefined).pipe(Effect.ignore)),
47 ),
48 )
49 .pipe(Effect.forkChild);
50 yield* Deferred.await(probeStarted);
51
52 const foreground = yield* coordinator
53 .withForegroundStartup("kilo", Deferred.await(releaseForeground))
54 .pipe(Effect.forkChild);
55 expect(Option.isNone(yield* Fiber.join(probe))).toBe(true);
56 yield* Deferred.await(probeFinalized);
57
58 yield* Deferred.succeed(releaseForeground, undefined);
59 yield* Fiber.join(foreground);
60 }).pipe(Effect.provide(AcpRegistryRuntimeCoordinator.layer)),
61 );
62
63 it.effect("serializes native session mutations across callers", () =>
64 Effect.gen(function* () {
65 const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator;
66 const firstEntered = yield* Deferred.make<void>();
67 const releaseFirst = yield* Deferred.make<void>();
68 const secondEntered = yield* Deferred.make<void>();
69 const first = yield* coordinator
70 .withSessionMutation(
71 Deferred.succeed(firstEntered, undefined).pipe(
72 Effect.andThen(Deferred.await(releaseFirst)),
73 ),
74 )
75 .pipe(Effect.forkChild);
76 yield* Deferred.await(firstEntered);
77
78 const second = yield* coordinator
79 .withSessionMutation(Deferred.succeed(secondEntered, undefined))
80 .pipe(Effect.forkChild);
81 expect(yield* Deferred.poll(secondEntered)).toEqual(Option.none());
82
83 yield* Deferred.succeed(releaseFirst, undefined);
84 yield* Fiber.join(first);
85 yield* Fiber.join(second);
86 expect(Option.isSome(yield* Deferred.poll(secondEntered))).toBe(true);
87 }).pipe(Effect.provide(AcpRegistryRuntimeCoordinator.layer)),
88 );
89
90 it.effect("replays and replaces late command advertisements per provider instance", () =>
91 Effect.gen(function* () {
92 const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator;
93 const codex = ProviderInstanceId.make("acpRegistry_codex");
94 const kilo = ProviderInstanceId.make("acpRegistry_kilo");
95 const firstSeen = yield* Deferred.make<void>();
96 const replacementSeen = yield* Deferred.make<void>();
97 const received: Array<AcpRegistryAvailableCommands> = [];
98
99 yield* coordinator.publishAvailableCommands(codex, {
100 slashCommands: [{ name: "status" }],
101 skills: [{ name: "workspace-skill", path: "acp://skill/workspace-skill", enabled: true }],
102 });
103 const consumer = yield* coordinator
104 .watchAvailableCommands(codex, (commands) =>
105 Effect.gen(function* () {
106 received.push(commands);
107 yield* received.length === 1
108 ? Deferred.succeed(firstSeen, undefined)
109 : Deferred.succeed(replacementSeen, undefined);
110 }),
111 )
112 .pipe(Effect.forkChild);
113 yield* Deferred.await(firstSeen);
114
115 yield* coordinator.publishAvailableCommands(kilo, {
116 slashCommands: [{ name: "review" }],
117 skills: [],
118 });
119 yield* coordinator.publishAvailableCommands(codex, { slashCommands: [], skills: [] });
120 yield* Deferred.await(replacementSeen);
121 yield* Fiber.interrupt(consumer);
122
123 expect(received).toEqual([
124 {
125 slashCommands: [{ name: "status" }],
126 skills: [{ name: "workspace-skill", path: "acp://skill/workspace-skill", enabled: true }],
127 },
128 { slashCommands: [], skills: [] },
129 ]);
130 expect(yield* coordinator.getAvailableCommands(kilo)).toEqual(
131 Option.some({ slashCommands: [{ name: "review" }], skills: [] }),
132 );
133 yield* coordinator.clearAvailableCommands(codex);
134 expect(Option.isNone(yield* coordinator.getAvailableCommands(codex))).toBe(true);
135 }).pipe(Effect.provide(AcpRegistryRuntimeCoordinator.layer)),
136 );
137
138 it.effect("requires matching user consent before accepting URL authentication", () =>
139 Effect.gen(function* () {
140 const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator;
141 const instanceId = ProviderInstanceId.make("acpRegistry_antigravity");
142 const action = {
143 elicitationId: "login-1",
144 url: "https://accounts.example.com/login",
145 message: "Continue in your browser",
146 };
147 const actionSeen = yield* Deferred.make<void>();
148 const consumer = yield* coordinator
149 .watchUrlAuthAction(instanceId, (current) =>
150 current === null
151 ? Effect.void
152 : Deferred.succeed(actionSeen, undefined).pipe(Effect.asVoid),
153 )
154 .pipe(Effect.forkChild);
155 const request = yield* coordinator
156 .requestUrlAuthentication(instanceId, action)
157 .pipe(Effect.forkChild);
158 yield* Deferred.await(actionSeen);
159
160 expect(yield* coordinator.getUrlAuthAction(instanceId)).toEqual(
161 Option.some({
162 ...action,
163 createdAt: "1970-01-01T00:00:00.000Z",
164 expiresAt: "1970-01-01T00:10:00.000Z",
165 }),
166 );
167
168 expect(
169 yield* coordinator.acceptUrlAuthentication({ instanceId, elicitationId: "stale" }),
170 ).toBe(false);
171 expect(
172 yield* coordinator.acceptUrlAuthentication({ instanceId, elicitationId: "login-1" }),
173 ).toBe(true);
174 expect(yield* Fiber.join(request)).toBe(true);
175 expect(Option.isNone(yield* coordinator.getUrlAuthAction(instanceId))).toBe(true);
176 yield* Fiber.interrupt(consumer);
177 }).pipe(Effect.provide(AcpRegistryRuntimeCoordinator.layer)),
178 );
179
180 it.effect("publishes a replacement URL action before retiring the previous request", () =>
181 Effect.gen(function* () {
182 const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator;
183 const instanceId = ProviderInstanceId.make("acpRegistry_replacement");
184 const received: Array<string | null> = [];
185 const firstSeen = yield* Deferred.make<void>();
186 const replacementSeen = yield* Deferred.make<void>();
187 const consumer = yield* coordinator
188 .watchUrlAuthAction(instanceId, (current) =>
189 Effect.sync(() => {
190 received.push(current?.elicitationId ?? null);
191 }).pipe(
192 Effect.andThen(
193 current?.elicitationId === "login-1"
194 ? Deferred.succeed(firstSeen, undefined).pipe(Effect.asVoid)
195 : current?.elicitationId === "login-2"
196 ? Deferred.succeed(replacementSeen, undefined).pipe(Effect.asVoid)
197 : Effect.void,
198 ),
199 ),
200 )
201 .pipe(Effect.forkChild);
202 const first = yield* coordinator
203 .requestUrlAuthentication(instanceId, {
204 elicitationId: "login-1",
205 url: "https://accounts.example.com/first",
206 message: "Continue with the first login",
207 })
208 .pipe(Effect.forkChild);
209 yield* Deferred.await(firstSeen);
210 const replacement = yield* coordinator
211 .requestUrlAuthentication(instanceId, {
212 elicitationId: "login-2",
213 url: "https://accounts.example.com/replacement",
214 message: "Continue with the replacement login",
215 })
216 .pipe(Effect.forkChild);
217 yield* Deferred.await(replacementSeen);
218
219 expect(yield* Fiber.join(first)).toBe(false);
220 expect(
221 (yield* coordinator.getUrlAuthAction(instanceId)).pipe(Option.getOrThrow),
222 ).toMatchObject({ elicitationId: "login-2" });
223 expect(received.slice(0, 2)).toEqual(["login-1", "login-2"]);
224 expect(
225 yield* coordinator.acceptUrlAuthentication({ instanceId, elicitationId: "login-2" }),
226 ).toBe(true);
227 expect(yield* Fiber.join(replacement)).toBe(true);
228 yield* Fiber.interrupt(consumer);
229 }).pipe(Effect.provide(AcpRegistryRuntimeCoordinator.layer)),
230 );
231
232 it.effect("clears a published URL action when its request is interrupted", () =>
233 Effect.gen(function* () {
234 const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator;
235 const instanceId = ProviderInstanceId.make("acpRegistry_interrupted");
236 const actionSeen = yield* Deferred.make<void>();
237 const cleared = yield* Deferred.make<void>();
238 const consumer = yield* coordinator
239 .watchUrlAuthAction(instanceId, (current) =>
240 current === null
241 ? Deferred.succeed(cleared, undefined).pipe(Effect.asVoid)
242 : Deferred.succeed(actionSeen, undefined).pipe(Effect.asVoid),
243 )
244 .pipe(Effect.forkChild);
245 const request = yield* coordinator
246 .requestUrlAuthentication(instanceId, {
247 elicitationId: "interrupted-login",
248 url: "https://accounts.example.com/interrupted",
249 message: "Continue with login",
250 })
251 .pipe(Effect.forkChild);
252 yield* Deferred.await(actionSeen);
253
254 yield* Fiber.interrupt(request);
255 yield* Deferred.await(cleared);
256 expect(Option.isNone(yield* coordinator.getUrlAuthAction(instanceId))).toBe(true);
257 yield* Fiber.interrupt(consumer);
258 }).pipe(Effect.provide(AcpRegistryRuntimeCoordinator.layer)),
259 );
260 });