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

main
ts 79 lines 3.18 KB
Raw
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { assert, it } from "@effect/vitest";
3 import { ProviderDriverKind } from "@t3tools/contracts";
4 import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
5 import * as HostProcess from "@t3tools/shared/HostProcess";
6 import * as Effect from "effect/Effect";
7 import * as Layer from "effect/Layer";
8 import * as Path from "effect/Path";
9 import * as ChildProcess from "effect/process/ChildProcess";
10 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11 import * as Stream from "effect/Stream";
12 import * as SqlClient from "effect/sql/SqlClient";
13
14 import * as SqlitePersistence from "../persistence/Sqlite.ts";
15 import { layerIdAllocator } from "./ProviderOrchestrationAdapterInfrastructure.ts";
16
17 it.effect("subagent threads stored under a legacy id keep it when re-derived", () =>
18 Effect.gen(function* () {
19 const stored = {
20 driver: ProviderDriverKind.make("claudeAgent"),
21 nativeThreadId: "parent:task-1",
22 };
23 const legacyId = IdAllocator.deriveThreadFromProviderThread(stored);
24 const layerStored = Layer.effectDiscard(
25 Effect.gen(function* () {
26 const sql = yield* SqlClient.SqlClient;
27 yield* sql`
28 INSERT INTO orchestration_v2_projection_threads (
29 thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
30 created_at, updated_at, payload_json
31 ) VALUES (
32 ${legacyId}, 'project-1', 'Subagent', 'claudeAgent', 'full-access', 'default',
33 '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z', '{}'
34 )
35 `;
36 }),
37 ).pipe(Layer.provideMerge(SqlitePersistence.layerMemory));
38 const ids = yield* IdAllocator.IdAllocatorV2.pipe(
39 Effect.provide(layerIdAllocator.pipe(Layer.provide(layerStored))),
40 );
41
42 assert.strictEqual(ids.derive.subagentThread(stored), legacyId);
43 assert.match(
44 ids.derive.subagentThread({ ...stored, nativeThreadId: "parent:task-2" }),
45 /^thread:subagent:[0-9a-f]{16}$/,
46 );
47 }),
48 );
49
50 it.live("spawns the __cursor-sdk-worker CLI fast path and shuts down cleanly on close", () =>
51 Effect.scoped(
52 Effect.gen(function* () {
53 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
54 const path = yield* Path.Path;
55 const executable = yield* HostProcess.ExecutablePath;
56 const entry = yield* path.fromFileUrl(new URL("../bin.ts", import.meta.url));
57 const handle = yield* spawner.spawn(
58 ChildProcess.make(executable, [entry, "__cursor-sdk-worker"], {
59 stdin: { stream: "pipe", endOnDone: false },
60 stdout: "pipe",
61 stderr: "pipe",
62 }),
63 );
64 yield* Stream.run(
65 Stream.encodeText(Stream.make(`${JSON.stringify({ id: 1, type: "close" })}\n`)),
66 handle.stdin,
67 );
68 const stdout = yield* handle.stdout.pipe(Stream.decodeText(), Stream.mkString);
69 const exitCode = Number(yield* handle.exitCode);
70 assert.strictEqual(exitCode, 0);
71 assert.deepStrictEqual(JSON.parse(stdout.trim()), {
72 type: "response",
73 id: 1,
74 ok: true,
75 result: null,
76 });
77 }),
78 ).pipe(Effect.provide(NodeServices.layer)),
79 );