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

main
ts 356 lines 12 KB
Raw
1 import { assert, it } from "@effect/vitest";
2 import {
3 CommandId,
4 GuestMessageId,
5 GuestShareId,
6 MessageId,
7 ProjectId,
8 ProviderDriverKind,
9 ProviderInstanceId,
10 ThreadId,
11 } from "@t3tools/contracts";
12 import * as DateTime from "effect/DateTime";
13 import * as Effect from "effect/Effect";
14 import * as Exit from "effect/Exit";
15 import * as Layer from "effect/Layer";
16 import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
17
18 import * as SqlitePersistence from "../persistence/Sqlite.ts";
19 import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
20 import * as EffectOutbox from "./EffectOutbox.ts";
21 import * as Orchestrator from "./Orchestrator.ts";
22 import * as ProjectionStore from "./ProjectionStore.ts";
23 import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
24 import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
25
26 const instanceId = ProviderInstanceId.make("codex");
27 const adapter = {
28 instanceId,
29 driver: ProviderDriverKind.make("codex"),
30 getCapabilities: () => Effect.succeed(CodexProviderCapabilitiesV2),
31 planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" as const }),
32 openSession: () => Effect.die("No provider process needed for guest shares"),
33 } as ProviderAdapter.ProviderAdapterV2["Service"];
34 const layerDatabase = SqlitePersistence.layerMemory;
35 const layerTest = Layer.mergeAll(
36 layerDatabase,
37 ProjectionStore.layer.pipe(Layer.provide(layerDatabase)),
38 EffectOutbox.layer.pipe(Layer.provide(layerDatabase)),
39 ProviderReplayHarness.layerWithRegistry(
40 { name: "guest-share" },
41 ProviderAdapterRegistry.layerFromAdapters([adapter]),
42 { databaseLayer: layerDatabase, runEffectWorker: false },
43 ),
44 );
45
46 const threadId = ThreadId.make("thread:guest-share");
47 const shareId = GuestShareId.make("share:one");
48 const guestMessageId = GuestMessageId.make("guest-message:one");
49
50 const setup = Effect.gen(function* () {
51 const orchestrator = yield* Orchestrator.OrchestratorV2;
52 yield* orchestrator.dispatch({
53 type: "thread.create",
54 commandId: CommandId.make("create-guest-share"),
55 threadId,
56 projectId: ProjectId.make("project:guest-share"),
57 title: "Review me",
58 modelSelection: { instanceId, model: "gpt-5.1-codex" },
59 runtimeMode: "full-access",
60 interactionMode: "default",
61 branch: null,
62 worktreePath: null,
63 createdBy: "user",
64 creationSource: "web",
65 });
66 yield* orchestrator.dispatch({
67 type: "thread.guest-share.open",
68 commandId: CommandId.make("open-guest-share"),
69 threadId,
70 shareId,
71 host: "env.t3.test",
72 expiresAt: DateTime.formatIso(DateTime.add(yield* DateTime.now, { hours: 24 })),
73 });
74 return orchestrator;
75 });
76
77 const sendGuestMessage = (orchestrator: Orchestrator.OrchestratorV2["Service"], id: string) =>
78 orchestrator.dispatch({
79 type: "message.dispatch",
80 commandId: CommandId.make(id),
81 threadId,
82 messageId: MessageId.make(id),
83 guestMessageId,
84 text: "ignored: the server sends the guest's own text",
85 attachments: [],
86 dispatchMode: { type: "defer_start" },
87 createdBy: "user",
88 creationSource: "web",
89 });
90
91 it.effect("redeems a guest link once and sends each guest message once, with its turn", () =>
92 Effect.gen(function* () {
93 const orchestrator = yield* setup;
94 const projections = yield* ProjectionStore.ProjectionStoreV2;
95 const redeem = (id: string) =>
96 orchestrator.dispatch({
97 type: "thread.guest-share.redeem",
98 commandId: CommandId.make(id),
99 threadId,
100 shareId,
101 guestName: "Sam",
102 });
103 yield* redeem("redeem-1");
104 assert.isTrue(Exit.isFailure(yield* Effect.exit(redeem("redeem-2"))));
105
106 yield* orchestrator.dispatch({
107 type: "thread.guest-message.submit",
108 commandId: CommandId.make("submit-1"),
109 threadId,
110 shareId,
111 guestMessageId,
112 text: "Why not keep the old cache?",
113 });
114 assert.equal((yield* projections.getThreadShell(threadId))?.pendingGuestMessageCount, 1);
115
116 const now = yield* DateTime.now;
117 const replacement = orchestrator.dispatch({
118 type: "thread.guest-share.open",
119 commandId: CommandId.make("replace-pending-share"),
120 threadId,
121 shareId: GuestShareId.make("share:replacement"),
122 host: "env.t3.test",
123 expiresAt: DateTime.formatIso(DateTime.add(now, { hours: 24 })),
124 });
125 assert.isTrue(Exit.isFailure(yield* Effect.exit(replacement)));
126 assert.equal((yield* projections.getThread(threadId)).guestShare?.id, shareId);
127 assert.equal((yield* projections.getThreadShell(threadId))?.pendingGuestMessageCount, 1);
128
129 yield* sendGuestMessage(orchestrator, "send-1");
130 const projection = yield* projections.getThreadProjection(threadId);
131 assert.deepEqual(
132 projection.thread.guestShare?.messages.map((message) => message.status),
133 ["sent"],
134 );
135 const sent = projection.messages.find((message) => message.id === MessageId.make("send-1"));
136 assert.equal(
137 sent?.text,
138 "Message from guest Sam, sent by the owner:\n\nWhy not keep the old cache?",
139 );
140 assert.deepEqual(sent?.context?.guest, {
141 guestMessageId,
142 name: "Sam",
143 text: "Why not keep the old cache?",
144 shareId,
145 });
146 assert.lengthOf(projection.runs, 1);
147 assert.equal((yield* projections.getThreadShell(threadId))?.pendingGuestMessageCount, 0);
148
149 // A second owner device clicking Send must not start another turn.
150 assert.isTrue(Exit.isFailure(yield* Effect.exit(sendGuestMessage(orchestrator, "send-2"))));
151 assert.lengthOf((yield* projections.getThreadProjection(threadId)).runs, 1);
152 }).pipe(Effect.provide(layerTest)),
153 );
154
155 it.effect("rejects guest messages until the link is redeemed and after archive", () =>
156 Effect.gen(function* () {
157 const orchestrator = yield* setup;
158 const projections = yield* ProjectionStore.ProjectionStoreV2;
159 const submit = orchestrator.dispatch({
160 type: "thread.guest-message.submit",
161 commandId: CommandId.make("submit-early"),
162 threadId,
163 shareId,
164 guestMessageId,
165 text: "Hello",
166 });
167 assert.isTrue(Exit.isFailure(yield* Effect.exit(submit)));
168
169 yield* orchestrator.dispatch({
170 type: "thread.archive",
171 commandId: CommandId.make("archive-guest-share"),
172 threadId,
173 });
174 assert.isNull((yield* projections.getThread(threadId)).guestShare ?? null);
175 }).pipe(Effect.provide(layerTest)),
176 );
177
178 it.effect("drops guest attribution that a client sends on its own message", () =>
179 Effect.gen(function* () {
180 const orchestrator = yield* setup;
181 const projections = yield* ProjectionStore.ProjectionStoreV2;
182 yield* orchestrator.dispatch({
183 type: "message.dispatch",
184 commandId: CommandId.make("owner-message"),
185 threadId,
186 messageId: MessageId.make("owner-message"),
187 text: "Mine",
188 context: { version: 1, records: [], guest: { guestMessageId, name: "Sam", text: "Mine" } },
189 attachments: [],
190 dispatchMode: { type: "defer_start" },
191 createdBy: "user",
192 creationSource: "web",
193 });
194 const projection = yield* projections.getThreadProjection(threadId);
195 const sent = projection.messages.find(
196 (message) => message.id === MessageId.make("owner-message"),
197 );
198 assert.isUndefined(sent?.context?.guest);
199 }).pipe(Effect.provide(layerTest)),
200 );
201
202 it.effect("keeps a queued guest message unchanged when the owner tries to edit it", () =>
203 Effect.gen(function* () {
204 const orchestrator = yield* setup;
205 const projections = yield* ProjectionStore.ProjectionStoreV2;
206 yield* orchestrator.dispatch({
207 type: "message.dispatch",
208 commandId: CommandId.make("owner-first"),
209 threadId,
210 messageId: MessageId.make("owner-first"),
211 text: "Start",
212 attachments: [],
213 dispatchMode: { type: "defer_start" },
214 createdBy: "user",
215 creationSource: "web",
216 });
217 yield* orchestrator.dispatch({
218 type: "thread.guest-share.redeem",
219 commandId: CommandId.make("redeem"),
220 threadId,
221 shareId,
222 guestName: "Sam",
223 });
224 yield* orchestrator.dispatch({
225 type: "thread.guest-message.submit",
226 commandId: CommandId.make("submit"),
227 threadId,
228 shareId,
229 guestMessageId,
230 text: "Why?",
231 });
232 yield* orchestrator.dispatch({
233 type: "message.dispatch",
234 commandId: CommandId.make("send-queued"),
235 threadId,
236 messageId: MessageId.make("send-queued"),
237 guestMessageId,
238 text: "",
239 attachments: [],
240 dispatchMode: { type: "queue_after_active" },
241 createdBy: "user",
242 creationSource: "web",
243 });
244 const queued = (yield* projections.getThreadProjection(threadId)).runs.find(
245 (run) => run.status === "queued",
246 );
247 assert.isDefined(queued);
248
249 const edit = orchestrator.dispatch({
250 type: "queued-run.edit",
251 commandId: CommandId.make("edit"),
252 threadId,
253 runId: queued.id,
254 text: "My own words",
255 });
256 assert.isTrue(Exit.isFailure(yield* Effect.exit(edit)));
257 const message = (yield* projections.getThreadProjection(threadId)).messages.find(
258 (candidate) => candidate.id === MessageId.make("send-queued"),
259 );
260 assert.equal(message?.context?.guest?.text, "Why?");
261 }).pipe(Effect.provide(layerTest)),
262 );
263
264 it.effect("shows a guest message as dismissed when the owner cancels its queued send", () =>
265 Effect.gen(function* () {
266 const orchestrator = yield* setup;
267 const projections = yield* ProjectionStore.ProjectionStoreV2;
268 yield* orchestrator.dispatch({
269 type: "message.dispatch",
270 commandId: CommandId.make("owner-first"),
271 threadId,
272 messageId: MessageId.make("owner-first"),
273 text: "Start",
274 attachments: [],
275 dispatchMode: { type: "defer_start" },
276 createdBy: "user",
277 creationSource: "web",
278 });
279 yield* orchestrator.dispatch({
280 type: "thread.guest-share.redeem",
281 commandId: CommandId.make("redeem"),
282 threadId,
283 shareId,
284 guestName: "Sam",
285 });
286 yield* orchestrator.dispatch({
287 type: "thread.guest-message.submit",
288 commandId: CommandId.make("submit"),
289 threadId,
290 shareId,
291 guestMessageId,
292 text: "Why?",
293 });
294 yield* orchestrator.dispatch({
295 type: "message.dispatch",
296 commandId: CommandId.make("send-queued"),
297 threadId,
298 messageId: MessageId.make("send-queued"),
299 guestMessageId,
300 text: "",
301 attachments: [],
302 dispatchMode: { type: "queue_after_active" },
303 createdBy: "user",
304 creationSource: "web",
305 });
306 const queued = (yield* projections.getThreadProjection(threadId)).runs.find(
307 (run) => run.status === "queued",
308 );
309 assert.isDefined(queued);
310 yield* orchestrator.dispatch({
311 type: "queued-run.cancel",
312 commandId: CommandId.make("cancel"),
313 threadId,
314 runId: queued.id,
315 });
316 const { thread } = yield* projections.getThreadProjection(threadId);
317 assert.equal(
318 thread.guestShare?.messages.find((message) => message.id === guestMessageId)?.status,
319 "dismissed",
320 );
321 }).pipe(Effect.provide(layerTest)),
322 );
323
324 it.effect("brings a settled thread back when its guest sends a request", () =>
325 Effect.gen(function* () {
326 const orchestrator = yield* setup;
327 const projections = yield* ProjectionStore.ProjectionStoreV2;
328 yield* orchestrator.dispatch({
329 type: "thread.guest-share.redeem",
330 commandId: CommandId.make("redeem"),
331 threadId,
332 shareId,
333 guestName: "Sam",
334 });
335 yield* orchestrator.dispatch({
336 type: "thread.settle",
337 commandId: CommandId.make("settle"),
338 threadId,
339 });
340 assert.equal(
341 (yield* projections.getThreadProjection(threadId)).thread.settledOverride,
342 "settled",
343 );
344 yield* orchestrator.dispatch({
345 type: "thread.guest-message.submit",
346 commandId: CommandId.make("submit"),
347 threadId,
348 shareId,
349 guestMessageId,
350 text: "Why?",
351 });
352 const { thread } = yield* projections.getThreadProjection(threadId);
353 assert.isNull(thread.settledOverride);
354 assert.isNull(thread.settledAt);
355 }).pipe(Effect.provide(layerTest)),
356 );