Repository navigation
Expand file tree
/
Copy paththread.test.ts
More file actions
294 lines (269 loc) · 10.5 KB
/
Copy paththread.test.ts
File metadata and controls
294 lines (269 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
import { AuthSessionId, ProjectId, RunId, ThreadId } from "@t3tools/contracts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as NetService from "@t3tools/shared/Net";
import * as ConfigProvider from "effect/ConfigProvider";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as TestConsole from "effect/testing/TestConsole";
import { Command } from "effect/cli";
import { vi } from "vite-plus/test";
import * as McpBridge from "../mcp/AcpMcpStdioBridge.ts";
import {
makePersistedServerRuntimeState,
persistServerRuntimeState,
} from "../serverRuntimeState.ts";
import {
readLatestMessages,
threadCommand,
type ThreadTools,
titleFromMessage,
waitForRun,
} from "./thread.ts";
vi.mock("../auth/EnvironmentAuth.ts", async (importOriginal) => {
const actual = await importOriginal<typeof import("../auth/EnvironmentAuth.ts")>();
return {
...actual,
layerRuntime: Layer.mock(actual.EnvironmentAuth)({
issueMcpClientSession: () =>
Effect.map(DateTime.now, (expiresAt) => ({
sessionId: AuthSessionId.make("test-session"),
token: "synthetic-token",
expiresAt,
})),
revokeSession: () => Effect.succeed(true),
}),
};
});
const threadId = ThreadId.make("thread-1");
const runId = RunId.make("run-1");
it.effect(
"start resolves home-relative project paths while preserving ids and relative paths",
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs
.makeTempDirectoryScoped({ prefix: "t3-thread-project-path-" })
.pipe(Effect.flatMap((directory) => fs.realPath(directory)));
const home = path.join(root, "home");
const cwd = path.join(root, "current");
const workspaceRoot = path.join(home, "My Project");
const nestedRoot = path.join(workspaceRoot, "nested");
const baseDir = path.join(root, "state");
yield* fs.makeDirectory(nestedRoot, { recursive: true });
yield* fs.makeDirectory(cwd);
const projects = [
{ id: ProjectId.make("current-project"), title: "Current", workspaceRoot: cwd },
{ id: ProjectId.make("home-project"), title: "Home", workspaceRoot: home },
{ id: ProjectId.make("intended-project"), title: "Intended", workspaceRoot },
{ id: ProjectId.make("nested-project"), title: "Nested", workspaceRoot: nestedRoot },
{ id: ProjectId.make("~/literal-id"), title: "Exact id", workspaceRoot: cwd },
];
yield* persistServerRuntimeState({
path: path.join(baseDir, "userdata", "server-runtime.json"),
state: yield* makePersistedServerRuntimeState({
config: { host: "127.0.0.1", devUrl: undefined },
port: 3_773,
}),
});
using _session = vi.spyOn(McpBridge, "mcpToolSession").mockReturnValue(
Effect.succeed({
call: (tool, args) => {
switch (tool) {
case "t3_project_list":
return Effect.succeed({ structuredContent: { projects, nextCursor: null } });
case "t3_thread_launch":
return Effect.succeed({
structuredContent: { threadId, projectId: args.projectId, runId: null },
});
default:
return Effect.die(`unexpected ${tool} call`);
}
},
}),
);
for (const [identifier, expectedProjectId] of [
["intended-project", "intended-project"],
["~/literal-id", "~/literal-id"],
[workspaceRoot, "intended-project"],
[path.relative(cwd, workspaceRoot), "intended-project"],
[".", "current-project"],
["~/My Project", "intended-project"],
["~", "home-project"],
["~/My Project/nested", "nested-project"],
["~/My Project/missing", "intended-project"],
] as const) {
yield* Command.runWith(threadCommand, { version: "0.0.0" })([
"start",
identifier,
"Inspect this project",
"--json",
"--base-dir",
baseDir,
]).pipe(
Effect.provideService(HostProcess.HomeDirectory, home),
Effect.provideService(HostProcess.WorkingDirectory, cwd),
);
assert.strictEqual(
(yield* TestConsole.logLines).at(-1),
JSON.stringify({ threadId, projectId: expectedProjectId, runId: null }),
identifier,
);
}
}).pipe(
Effect.scoped,
Effect.provide(
Layer.mergeAll(
NodeServices.layer,
NodeCrypto.layer,
NetService.layer,
ConfigProvider.layer(ConfigProvider.fromEnv({ env: { T3CODE_PORT: "3773" } })),
),
),
),
);
/** Answers each tool call with the next scripted response for that tool. */
const scriptedTools = (script: Record<string, Array<unknown>>) => {
const calls: Array<{ readonly tool: string; readonly args: unknown }> = [];
const call = ((tool: string, args: unknown) => {
calls.push({ tool, args });
const next = script[tool]?.shift();
return next === undefined ? Effect.die(`unscripted ${tool} call`) : Effect.succeed(next);
}) as ThreadTools["call"];
return { tools: { call } satisfies ThreadTools, calls };
};
it.effect("--wait follows the sent run to its continuation's terminal status", () =>
Effect.gen(function* () {
const continuation = RunId.make("run-2");
const { tools, calls } = scriptedTools({
t3_thread_wait: [
{ threadId, runId, status: "running", timedOut: true },
{ threadId, runId: continuation, status: "failed", timedOut: false },
],
t3_thread_read: [{ thread: { pendingRequestCount: 0 } }],
});
const outcome = yield* waitForRun(tools, threadId, runId);
assert.deepStrictEqual(outcome, { type: "ended", runId: continuation, status: "failed" });
assert.deepStrictEqual(
calls.filter((entry) => entry.tool === "t3_thread_wait").map((entry) => entry.args),
[
{ threadId, runId, timeoutMs: 15_000 },
{ threadId, runId, timeoutMs: 15_000 },
],
);
}),
);
it.effect("--wait stops when the thread waits for an answer instead of polling forever", () =>
Effect.gen(function* () {
const { tools } = scriptedTools({
t3_thread_wait: [
{ threadId, runId, status: "queued", timedOut: true },
{ threadId, runId, status: "queued", timedOut: true },
],
t3_thread_read: [{ thread: { pendingRequestCount: 1 } }],
});
assert.deepStrictEqual(yield* waitForRun(tools, threadId, runId), { type: "needs_input" });
}),
);
it.effect("--wait reports its run's result when a later run is the one waiting", () =>
Effect.gen(function* () {
const { tools } = scriptedTools({
t3_thread_wait: [
{ threadId, runId, status: "running", timedOut: true },
{ threadId, runId, status: "completed", timedOut: false },
],
t3_thread_read: [{ thread: { pendingRequestCount: 1 } }],
});
assert.deepStrictEqual(yield* waitForRun(tools, threadId, runId), {
type: "ended",
runId,
status: "completed",
});
}),
);
/** Serves t3_thread_read pages the way the server does: positions count every item. */
const timelineTools = (types: ReadonlyArray<string>) => {
const items = types.map((type, position) => ({ position, itemId: `item-${position}`, type }));
const served: Array<number> = [];
const call = ((_tool: string, input: unknown) => {
const args = input as { readonly afterPosition?: number; readonly limit: number };
// Scanning one match past the page keeps reads of huge fixtures linear.
const matching: typeof items = [];
for (let position = (args.afterPosition ?? -1) + 1; position < items.length; position++) {
const item = items[position]!;
if (["user_message", "assistant_message", "proposed_plan"].includes(item.type)) {
matching.push(item);
if (matching.length > args.limit) break;
}
}
const page = matching.slice(0, args.limit);
served.push(page.length);
return Effect.succeed({
thread: { itemCount: items.length },
items: page,
nextPosition: page.at(-1)?.position ?? null,
hasMore: matching.length > page.length,
});
}) as ThreadTools["call"];
const latest = (count: number) =>
items
.filter((item) => item.type === "user_message" || item.type === "assistant_message")
.slice(-count)
.map((item) => item.itemId);
return { tools: { call } satisfies ThreadTools, served, latest };
};
const tail = (tools: number) => [...Array<string>(tools).fill("tool_call"), "assistant_message"];
it.effect("show reads the end of a long thread, not its whole history", () =>
Effect.gen(function* () {
const { tools, served, latest } = timelineTools([
...Array.from({ length: 1000 }, (_, index) =>
index % 2 === 0 ? "user_message" : "assistant_message",
),
...Array.from({ length: 10 }, () => tail(30)).flat(),
"proposed_plan",
]);
const { messages } = yield* readLatestMessages(tools, threadId, 10);
assert.deepStrictEqual(
messages.map((item) => item.itemId),
latest(10),
);
assert.isBelow(
served.reduce((sum, count) => sum + count, 0),
100,
);
}),
);
it.effect("show widens back to the first item when the thread has fewer messages", () =>
Effect.gen(function* () {
const { tools, latest } = timelineTools([
"user_message",
...Array.from({ length: 4 }, () => tail(150)).flat(),
]);
const { messages } = yield* readLatestMessages(tools, threadId, 10);
assert.deepStrictEqual(
messages.map((item) => item.itemId),
latest(10),
);
assert.lengthOf(messages, 5);
}),
);
it.effect("show returns a --messages count larger than one call can spread", () =>
Effect.gen(function* () {
const { tools, latest } = timelineTools(Array<string>(200_000).fill("assistant_message"));
const { messages } = yield* readLatestMessages(tools, threadId, 200_000);
assert.deepStrictEqual(
messages.map((item) => item.itemId),
latest(200_000),
);
}),
);
it("titles from a long first line without splitting an emoji", () => {
const title = titleFromMessage(`${"a".repeat(78)}😀 and the rest of a long first line`);
assert.strictEqual(title, `${"a".repeat(78)}…`);
});