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

main
ts 448 lines 15.4 KB
Raw
1 import { describe, expect, it } from "@effect/vitest";
2 import {
3 DEFAULT_SERVER_SETTINGS,
4 EventId,
5 MessageId,
6 type OrchestrationV2ThreadProjection,
7 NodeId,
8 ProjectId,
9 ProviderDriverKind,
10 ProviderInstanceId,
11 ProviderThreadId,
12 ProviderTurnId,
13 RunId,
14 ThreadId,
15 TurnItemId,
16 } from "@t3tools/contracts";
17 import * as DateTime from "effect/DateTime";
18 import * as Deferred from "effect/Deferred";
19 import * as Effect from "effect/Effect";
20 import * as Layer from "effect/Layer";
21 import * as Stream from "effect/Stream";
22 import * as Tracer from "effect/Tracer";
23 import * as TestClock from "effect/testing/TestClock";
24
25 import * as ServerSettings from "../serverSettings.ts";
26 import * as IdleCompactionService from "./IdleCompactionService.ts";
27 import * as Orchestrator from "./Orchestrator.ts";
28 import * as ProjectionStore from "./ProjectionStore.ts";
29
30 import { idleCompactionWindow, type IdleCompactionRecords } from "./IdleCompaction.ts";
31
32 const NOW_MS = Date.parse("2026-10-19T12:00:00.000Z");
33 const MINUTE_MS = 60_000;
34 const at = (offset = 0) => DateTime.makeUnsafe(NOW_MS + offset);
35 const threadId = ThreadId.make("idle-thread");
36 const runId = RunId.make("run-1");
37 const instanceId = ProviderInstanceId.make("claudeAgent");
38 const providerThreadId = ProviderThreadId.make("provider-thread");
39 const modelSelection = { instanceId, model: "claude-opus-5-5" };
40
41 function records(
42 usage: { readonly usedTokens?: number; readonly cacheExpiresInMs?: number } = {},
43 ): { -readonly [Key in keyof IdleCompactionRecords]: IdleCompactionRecords[Key] } {
44 const messageId = MessageId.make("message-1");
45 const nodeId = NodeId.make("root-node");
46 return {
47 thread: {
48 id: threadId,
49 projectId: ProjectId.make("project"),
50 title: "Idle thread",
51 createdBy: "user",
52 creationSource: "web",
53 providerInstanceId: instanceId,
54 modelSelection,
55 runtimeMode: "full-access",
56 interactionMode: "default",
57 branch: null,
58 worktreePath: null,
59 activeProviderThreadId: providerThreadId,
60 lineage: { rootThreadId: threadId, parentThreadId: null, relationshipToParent: null },
61 forkedFrom: null,
62 createdAt: at(-MINUTE_MS),
63 updatedAt: at(),
64 archivedAt: null,
65 settledOverride: null,
66 settledAt: null,
67 lastVisitedAt: at(),
68 deletedAt: null,
69 },
70 runs: [
71 {
72 id: runId,
73 threadId,
74 ordinal: 1,
75 providerInstanceId: instanceId,
76 modelSelection,
77 providerThreadId,
78 userMessageId: messageId,
79 rootNodeId: nodeId,
80 activeAttemptId: null,
81 status: "completed",
82 requestedAt: at(-MINUTE_MS),
83 startedAt: at(-MINUTE_MS),
84 completedAt: at(),
85 checkpointId: null,
86 contextHandoffId: null,
87 },
88 ],
89 messages: [
90 {
91 id: messageId,
92 threadId,
93 runId,
94 nodeId,
95 role: "user",
96 text: "Explain this code",
97 attachments: [],
98 streaming: false,
99 createdBy: "user",
100 creationSource: "web",
101 createdAt: at(-MINUTE_MS),
102 updatedAt: at(-MINUTE_MS),
103 },
104 ],
105 providerThreads: [
106 {
107 id: providerThreadId,
108 driver: ProviderDriverKind.make("claudeAgent"),
109 providerInstanceId: instanceId,
110 providerSessionId: null,
111 appThreadId: threadId,
112 ownerNodeId: nodeId,
113 nativeThreadRef: null,
114 nativeConversationHeadRef: null,
115 status: "idle",
116 firstRunOrdinal: 1,
117 lastRunOrdinal: 1,
118 handoffIds: [],
119 forkedFrom: null,
120 pendingBackgroundTasks: [],
121 createdAt: at(-MINUTE_MS),
122 updatedAt: at(),
123 },
124 ],
125 providerTurns: [
126 {
127 id: ProviderTurnId.make("provider-turn-1"),
128 providerThreadId,
129 nodeId,
130 runAttemptId: null,
131 nativeTurnRef: null,
132 ordinal: 1,
133 status: "completed",
134 startedAt: at(-MINUTE_MS),
135 completedAt: at(),
136 tokenUsage: {
137 usedTokens: usage.usedTokens ?? 250_000,
138 ...(usage.cacheExpiresInMs === undefined
139 ? {}
140 : { promptCacheExpiresAt: DateTime.formatIso(at(usage.cacheExpiresInMs)) }),
141 updatedAt: DateTime.formatIso(at()),
142 },
143 },
144 ],
145 runtimeRequests: [],
146 turnItems: [],
147 contextTransfers: [],
148 };
149 }
150
151 describe("idleCompactionWindow", () => {
152 it("skips small contexts, unknown expiry, other providers, switched threads, compactions, and side chats", () => {
153 const cache = { cacheExpiresInMs: 60 * MINUTE_MS };
154 expect(idleCompactionWindow(records({ ...cache, usedTokens: 199_999 }), NOW_MS)).toBeNull();
155 expect(idleCompactionWindow(records(), NOW_MS)).toBeNull();
156 const codex = records(cache);
157 codex.providerThreads = [
158 { ...codex.providerThreads[0]!, driver: ProviderDriverKind.make("codex") },
159 ];
160 expect(idleCompactionWindow(codex, NOW_MS)).toBeNull();
161 const switched = records(cache);
162 switched.thread = { ...switched.thread, providerInstanceId: ProviderInstanceId.make("codex") };
163 expect(idleCompactionWindow(switched, NOW_MS)).toBeNull();
164 const compacted = records(cache);
165 compacted.messages = [{ ...compacted.messages[0]!, text: "/compact" }];
166 expect(idleCompactionWindow(compacted, NOW_MS)).toBeNull();
167 const steered = records(cache);
168 steered.runs = [
169 ...steered.runs,
170 {
171 ...steered.runs[0]!,
172 id: RunId.make("run-2"),
173 ordinal: 2,
174 userMessageId: MessageId.make("message-2"),
175 status: "cancelled",
176 startedAt: null,
177 },
178 ];
179 expect(idleCompactionWindow(steered, NOW_MS)?.runId).toBe(runId);
180 // A turn Claude itself reported as cancelled did run, so it stays the latest.
181 steered.runs = steered.runs.map((run) => ({ ...run, startedAt: at(-MINUTE_MS) }));
182 expect(idleCompactionWindow(steered, NOW_MS)).toBeNull();
183 const archived = records(cache);
184 archived.thread = { ...archived.thread, archivedAt: at() };
185 expect(idleCompactionWindow(archived, NOW_MS)).toBeNull();
186 const sideChat = records(cache);
187 expect(idleCompactionWindow(sideChat, NOW_MS)).not.toBeNull();
188 sideChat.thread = {
189 ...sideChat.thread,
190 lineage: { ...sideChat.thread.lineage, relationshipToParent: "fork", sideChat: true },
191 };
192 expect(idleCompactionWindow(sideChat, NOW_MS)).toBeNull();
193 });
194
195 it("still compacts while a delegated child thread runs on its own", () => {
196 const delegating = records({ cacheExpiresInMs: 60 * MINUTE_MS });
197 delegating.turnItems = [
198 {
199 id: TurnItemId.make("delegate-item"),
200 threadId,
201 runId,
202 nodeId: NodeId.make("delegate-node"),
203 providerThreadId,
204 providerTurnId: null,
205 nativeItemRef: null,
206 parentItemId: null,
207 ordinal: 1,
208 status: "running",
209 title: "Implement the feature",
210 startedAt: at(-MINUTE_MS),
211 completedAt: null,
212 updatedAt: at(-MINUTE_MS),
213 type: "subagent",
214 subagentId: NodeId.make("delegate-node"),
215 origin: "app_owned",
216 driver: ProviderDriverKind.make("codex"),
217 providerInstanceId: ProviderInstanceId.make("codex"),
218 childThreadId: ThreadId.make("delegate-child"),
219 prompt: "Implement the feature",
220 result: null,
221 },
222 ];
223 expect(idleCompactionWindow(delegating, NOW_MS)?.runId).toBe(runId);
224 });
225 });
226
227 describe("IdleCompactionService", () => {
228 it.effect.each([
229 { mode: "onReturn", reads: 0 },
230 { mode: "off", reads: 0 },
231 { mode: "beforeCacheExpires", reads: 1 },
232 { mode: "beforeCacheExpires", override: "off", reads: 0 },
233 { mode: "onReturn", override: "beforeCacheExpires", reads: 1 },
234 { mode: "beforeCacheExpires", projectDisabled: true, reads: 0 },
235 { mode: "beforeCacheExpires", enabled: false, reads: 0 },
236 { mode: "beforeCacheExpires", driver: "codex", instance: "codex", reads: 0 },
237 { mode: "beforeCacheExpires", driver: "codex", instance: "custom-codex", reads: 0 },
238 { mode: "beforeCacheExpires", driver: "claudeAgent", instance: "custom-claude", reads: 1 },
239 { mode: "beforeCacheExpires", unconfigured: true, reads: 1 },
240 ] as const)(
241 "loads history only for eligible idle compaction: $mode/$override/$enabled/$projectDisabled/$instance/$unconfigured",
242 (scenario) => {
243 const test = {
244 override: "default",
245 enabled: true,
246 projectDisabled: false,
247 ...scenario,
248 } as const;
249 return Effect.gen(function* () {
250 yield* TestClock.setTime(NOW_MS);
251 const selectedInstance = ProviderInstanceId.make(
252 "instance" in test ? test.instance : "claudeAgent",
253 );
254 const driver = ProviderDriverKind.make("driver" in test ? test.driver : "claudeAgent");
255 const base = records({ cacheExpiresInMs: 2 * MINUTE_MS });
256 const data: OrchestrationV2ThreadProjection = {
257 ...base,
258 thread: {
259 ...base.thread,
260 providerInstanceId: selectedInstance,
261 modelSelection: { ...base.thread.modelSelection, instanceId: selectedInstance },
262 },
263 runs: base.runs.map((run) => ({
264 ...run,
265 providerInstanceId: selectedInstance,
266 modelSelection: { ...run.modelSelection, instanceId: selectedInstance },
267 })),
268 providerThreads: base.providerThreads.map((thread) => ({
269 ...thread,
270 driver,
271 providerInstanceId: selectedInstance,
272 })),
273 attempts: [],
274 nodes: [],
275 subagents: [],
276 providerSessions: [],
277 plans: [],
278 checkpointScopes: [],
279 checkpoints: [],
280 contextHandoffs: [],
281 visibleTurnItems: [],
282 updatedAt: at(),
283 };
284 let historyReads = 0;
285 let dispatches = 0;
286 const done = yield* Deferred.make<void>();
287 const tracer = Tracer.make({
288 span(options) {
289 const span = new Tracer.NativeSpan(options);
290 const end = span.end.bind(span);
291 span.end = (...args) => {
292 end(...args);
293 if (span.name === "IdleCompactionService.compactWhenDue")
294 Deferred.doneUnsafe(done, Effect.void);
295 };
296 return span;
297 },
298 });
299 const dependencies = Layer.mergeAll(
300 Layer.mock(Orchestrator.OrchestratorV2)({
301 streamDomainEvents: Stream.make({
302 id: EventId.make("idle-completed"),
303 type: "run.updated",
304 threadId,
305 runId,
306 occurredAt: at(),
307 payload: data.runs[0]!,
308 }),
309 dispatch: () =>
310 Effect.sync(() => {
311 dispatches++;
312 return { sequence: 1, storedEvents: [] };
313 }),
314 }),
315 Layer.mock(ProjectionStore.ProjectionStoreV2)({
316 getThread: () => Effect.succeed(data.thread),
317 getThreadRecords: () =>
318 Effect.sync(() => {
319 historyReads++;
320 return data;
321 }),
322 }),
323 Layer.mock(ServerSettings.ServerSettingsService)({
324 getSettings: Effect.succeed({
325 ...DEFAULT_SERVER_SETTINGS,
326 idleCompaction: test.mode,
327 providerInstances:
328 "unconfigured" in test
329 ? {}
330 : {
331 [selectedInstance]: {
332 driver,
333 enabled: test.enabled,
334 config: { idleCompaction: test.override },
335 },
336 },
337 projectSettingsOverrides: test.projectDisabled
338 ? {
339 [data.thread.projectId]: {
340 providerInstances: { [selectedInstance]: { enabled: false } },
341 },
342 }
343 : {},
344 }),
345 }),
346 );
347 yield* Effect.gen(function* () {
348 const service = yield* IdleCompactionService.make;
349 yield* service.start();
350 yield* Deferred.await(done);
351 }).pipe(Effect.provide(dependencies), Effect.withTracer(tracer));
352 expect(historyReads).toBe(test.reads);
353 expect(dispatches).toBe(test.reads);
354 });
355 },
356 );
357
358 it.effect("keeps the latest run's compaction when an earlier run reports completed again", () =>
359 Effect.gen(function* () {
360 yield* TestClock.setTime(NOW_MS);
361 const base = records({ cacheExpiresInMs: 10 * MINUTE_MS });
362 const laterRunId = RunId.make("run-2");
363 const laterMessageId = MessageId.make("message-2");
364 const earlierRun = base.runs[0]!;
365 const laterRun = {
366 ...earlierRun,
367 id: laterRunId,
368 ordinal: 2,
369 userMessageId: laterMessageId,
370 };
371 const cancelledRun = {
372 ...earlierRun,
373 id: RunId.make("run-3"),
374 ordinal: 3,
375 userMessageId: MessageId.make("message-3"),
376 status: "cancelled" as const,
377 startedAt: null,
378 };
379 const data: OrchestrationV2ThreadProjection = {
380 ...base,
381 runs: [earlierRun, laterRun, cancelledRun],
382 messages: [
383 ...base.messages,
384 { ...base.messages[0]!, id: laterMessageId, runId: laterRunId, text: "Keep going" },
385 ],
386 providerThreads: [{ ...base.providerThreads[0]!, lastRunOrdinal: 2 }],
387 attempts: [],
388 nodes: [],
389 subagents: [],
390 providerSessions: [],
391 plans: [],
392 checkpointScopes: [],
393 checkpoints: [],
394 contextHandoffs: [],
395 visibleTurnItems: [],
396 updatedAt: at(),
397 };
398 const completed = (run: typeof earlierRun) => ({
399 id: EventId.make(`completed-${run.id}`),
400 type: "run.updated" as const,
401 threadId,
402 runId: run.id,
403 occurredAt: at(),
404 payload: run,
405 });
406 const dispatched: Array<string> = [];
407 const streamDone = yield* Deferred.make<void>();
408 const dependencies = Layer.mergeAll(
409 Layer.mock(Orchestrator.OrchestratorV2)({
410 // A delegated completion on the earlier run lands after the later run completed.
411 streamDomainEvents: Stream.make(completed(laterRun), completed(earlierRun)).pipe(
412 Stream.ensuring(Deferred.succeed(streamDone, undefined)),
413 ),
414 dispatch: (command) =>
415 Effect.sync(() => {
416 dispatched.push(command.commandId);
417 return { sequence: 1, storedEvents: [] };
418 }),
419 }),
420 Layer.mock(ProjectionStore.ProjectionStoreV2)({
421 getThread: () => Effect.succeed(data.thread),
422 getThreadRecords: (_threadId, _keys, options) =>
423 Effect.succeed({
424 ...data,
425 messages: data.messages.filter(
426 (message) =>
427 options?.messageRunIds === undefined ||
428 (message.runId !== null && options.messageRunIds.includes(message.runId)),
429 ),
430 }),
431 }),
432 Layer.mock(ServerSettings.ServerSettingsService)({
433 getSettings: Effect.succeed({
434 ...DEFAULT_SERVER_SETTINGS,
435 idleCompaction: "beforeCacheExpires",
436 }),
437 }),
438 );
439 yield* Effect.gen(function* () {
440 const service = yield* IdleCompactionService.make;
441 yield* service.start();
442 yield* Deferred.await(streamDone);
443 yield* TestClock.adjust(9 * MINUTE_MS);
444 }).pipe(Effect.provide(dependencies));
445 expect(dispatched).toEqual(["server:idle-compact:idle-thread:run-2"]);
446 }),
447 );
448 });