|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { expect, it } from "@effect/vitest"; |
| 3 | +import { |
| 4 | + CommandId, |
| 5 | + EnvironmentId, |
| 6 | + ProjectId, |
| 7 | + ProviderInstanceId, |
| 8 | + ThreadId, |
| 9 | +} from "@t3tools/contracts"; |
| 10 | +import * as Effect from "effect/Effect"; |
| 11 | +import * as Layer from "effect/Layer"; |
| 12 | +import * as Option from "effect/Option"; |
| 13 | +import * as Stream from "effect/Stream"; |
| 14 | +import { McpSchema, McpServer } from "effect/unstable/ai"; |
| 15 | + |
| 16 | +import { ServerConfig } from "../../../config.ts"; |
| 17 | +import { OrchestrationEngineLive } from "../../../orchestration/Layers/OrchestrationEngine.ts"; |
| 18 | +import { OrchestrationProjectionPipelineLive } from "../../../orchestration/Layers/ProjectionPipeline.ts"; |
| 19 | +import { OrchestrationProjectionSnapshotQueryLive } from "../../../orchestration/Layers/ProjectionSnapshotQuery.ts"; |
| 20 | +import { OrchestrationEngineService } from "../../../orchestration/Services/OrchestrationEngine.ts"; |
| 21 | +import { ProjectionSnapshotQuery } from "../../../orchestration/Services/ProjectionSnapshotQuery.ts"; |
| 22 | +import * as ThreadBackgroundLiveness from "../../../orchestration/ThreadBackgroundLiveness.ts"; |
| 23 | +import * as ThreadPlanProgress from "../../../orchestration/ThreadPlanProgress.ts"; |
| 24 | +import { OrchestrationCommandReceiptRepositoryLive } from "../../../persistence/Layers/OrchestrationCommandReceipts.ts"; |
| 25 | +import { OrchestrationEventStoreLive } from "../../../persistence/Layers/OrchestrationEventStore.ts"; |
| 26 | +import { SqlitePersistenceMemory } from "../../../persistence/Layers/Sqlite.ts"; |
| 27 | +import { RepositoryIdentityResolver } from "../../../project/RepositoryIdentityResolver.ts"; |
| 28 | +import { ThreadsToolkitRegistrationLive } from "../../McpHttpServer.ts"; |
| 29 | +import * as McpInvocationContext from "../../McpInvocationContext.ts"; |
| 30 | + |
| 31 | +const threadId = ThreadId.make("calling-thread"); |
| 32 | +const otherThreadId = ThreadId.make("other-thread"); |
| 33 | +const projectId = ProjectId.make("project"); |
| 34 | +const providerInstanceId = ProviderInstanceId.make("codex"); |
| 35 | +const invocation: McpInvocationContext.McpInvocationScope = { |
| 36 | + environmentId: EnvironmentId.make("test-environment"), |
| 37 | + threadId, |
| 38 | + providerSessionId: "test-session", |
| 39 | + providerInstanceId, |
| 40 | + capabilities: new Set(["thread-metadata"]), |
| 41 | + issuedAt: 1, |
| 42 | +}; |
| 43 | +const client = McpSchema.McpServerClient.of({ |
| 44 | + clientId: 1, |
| 45 | + clientCapabilities: {}, |
| 46 | + clientInfo: { name: "rename-test", version: "1.0.0" }, |
| 47 | + protocolVersion: "2025-06-18", |
| 48 | + initializePayload: { |
| 49 | + protocolVersion: "2025-06-18", |
| 50 | + capabilities: {}, |
| 51 | + clientInfo: { name: "rename-test", version: "1.0.0" }, |
| 52 | + }, |
| 53 | + getClient: Effect.die("unused"), |
| 54 | +}); |
| 55 | + |
| 56 | +const EngineLayer = Layer.mergeAll( |
| 57 | + OrchestrationEngineLive.pipe( |
| 58 | + Layer.provide(OrchestrationProjectionSnapshotQueryLive), |
| 59 | + Layer.provide(OrchestrationProjectionPipelineLive), |
| 60 | + ), |
| 61 | + OrchestrationProjectionSnapshotQueryLive, |
| 62 | +).pipe( |
| 63 | + Layer.provide(ThreadBackgroundLiveness.layer), |
| 64 | + Layer.provide(ThreadPlanProgress.layer), |
| 65 | + Layer.provide(OrchestrationEventStoreLive), |
| 66 | + Layer.provide(OrchestrationCommandReceiptRepositoryLive), |
| 67 | + Layer.provide(Layer.succeed(RepositoryIdentityResolver, { resolve: () => Effect.succeed(null) })), |
| 68 | + Layer.provide(SqlitePersistenceMemory), |
| 69 | +); |
| 70 | +const TestLayer = ThreadsToolkitRegistrationLive.pipe( |
| 71 | + Layer.provideMerge(McpServer.McpServer.layer), |
| 72 | + Layer.provideMerge(EngineLayer), |
| 73 | + Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-mcp-rename-test-" })), |
| 74 | + Layer.provide(NodeServices.layer), |
| 75 | +); |
| 76 | + |
| 77 | +const makeHarness = Effect.gen(function* () { |
| 78 | + const server = yield* McpServer.McpServer; |
| 79 | + const engine = yield* OrchestrationEngineService; |
| 80 | + const snapshots = yield* ProjectionSnapshotQuery; |
| 81 | + const createdAt = "2026-09-16T00:00:00.000Z"; |
| 82 | + yield* engine.dispatch({ |
| 83 | + type: "project.create", |
| 84 | + commandId: CommandId.make("create-project"), |
| 85 | + projectId, |
| 86 | + title: "Project", |
| 87 | + workspaceRoot: "/tmp/mcp-rename-test", |
| 88 | + createdAt, |
| 89 | + }); |
| 90 | + for (const id of [threadId, otherThreadId]) { |
| 91 | + yield* engine.dispatch({ |
| 92 | + type: "thread.create", |
| 93 | + commandId: CommandId.make(`create-${id}`), |
| 94 | + threadId: id, |
| 95 | + projectId, |
| 96 | + title: "Original title", |
| 97 | + modelSelection: { instanceId: providerInstanceId, model: "gpt-5" }, |
| 98 | + runtimeMode: "full-access", |
| 99 | + interactionMode: "default", |
| 100 | + branch: null, |
| 101 | + worktreePath: null, |
| 102 | + createdAt, |
| 103 | + }); |
| 104 | + } |
| 105 | + const call = (args: Record<string, unknown>, scope = invocation) => |
| 106 | + server |
| 107 | + .callTool({ name: "rename_thread", arguments: args }) |
| 108 | + .pipe( |
| 109 | + Effect.provideService(McpInvocationContext.McpInvocationContext, scope), |
| 110 | + Effect.provideService(McpSchema.McpServerClient, client), |
| 111 | + ); |
| 112 | + const readThread = (id = threadId) => |
| 113 | + snapshots.getThreadDetailById(id).pipe(Effect.map(Option.getOrThrow)); |
| 114 | + return { server, engine, call, readThread }; |
| 115 | +}); |
| 116 | + |
| 117 | +it.effect( |
| 118 | + "renames only the calling thread, persists a manual title, and supports renaming again", |
| 119 | + () => |
| 120 | + Effect.gen(function* () { |
| 121 | + const { server, engine, call, readThread } = yield* makeHarness; |
| 122 | + const tool = server.tools.find(({ tool }) => tool.name === "rename_thread")?.tool; |
| 123 | + expect(tool?.inputSchema.properties).toHaveProperty("title"); |
| 124 | + expect(tool?.inputSchema.properties).not.toHaveProperty("threadId"); |
| 125 | + expect(tool?.annotations).toMatchObject({ |
| 126 | + readOnlyHint: false, |
| 127 | + destructiveHint: false, |
| 128 | + idempotentHint: true, |
| 129 | + openWorldHint: false, |
| 130 | + }); |
| 131 | + |
| 132 | + const title = "CU-869y9uv0 change the button to green and keep the ticket ID"; |
| 133 | + const before = yield* engine.latestSequence; |
| 134 | + // An extra target supplied by a caller cannot override the credential's thread. |
| 135 | + const result = yield* call({ title: ` ${title}\n`, threadId: otherThreadId }); |
| 136 | + expect(result.isError).toBe(false); |
| 137 | + expect(result.structuredContent).toEqual({ threadId, title }); |
| 138 | + expect(yield* readThread()).toMatchObject({ |
| 139 | + title, |
| 140 | + titleState: { source: "manual", needsRefinement: false }, |
| 141 | + }); |
| 142 | + expect(yield* readThread(otherThreadId)).toMatchObject({ title: "Original title" }); |
| 143 | + const events = yield* engine.readEvents(before).pipe(Stream.runCollect); |
| 144 | + expect(events).toMatchObject([ |
| 145 | + { |
| 146 | + type: "thread.meta-updated", |
| 147 | + aggregateId: threadId, |
| 148 | + payload: { threadId, title, titleState: { source: "manual" } }, |
| 149 | + }, |
| 150 | + ]); |
| 151 | + |
| 152 | + expect((yield* call({ title })).isError).toBe(false); |
| 153 | + expect((yield* call({ title: "LIN-42 another name" })).structuredContent).toEqual({ |
| 154 | + threadId, |
| 155 | + title: "LIN-42 another name", |
| 156 | + }); |
| 157 | + expect(yield* readThread()).toMatchObject({ title: "LIN-42 another name" }); |
| 158 | + }).pipe(Effect.provide(TestLayer)), |
| 159 | +); |
| 160 | + |
| 161 | +it.effect("keeps the agent's title when title generation or regeneration finishes late", () => |
| 162 | + Effect.gen(function* () { |
| 163 | + const { engine, call, readThread } = yield* makeHarness; |
| 164 | + const requestId = CommandId.make("regenerate-title"); |
| 165 | + yield* engine.dispatch({ |
| 166 | + type: "thread.meta.update", |
| 167 | + commandId: requestId, |
| 168 | + threadId, |
| 169 | + regenerateTitle: true, |
| 170 | + }); |
| 171 | + yield* call({ title: "CU-869y9uv0 chosen title" }); |
| 172 | + yield* engine.dispatch({ |
| 173 | + type: "thread.title.regeneration.complete", |
| 174 | + commandId: CommandId.make("late-regeneration"), |
| 175 | + threadId, |
| 176 | + requestId, |
| 177 | + title: "Unwanted regenerated title", |
| 178 | + }); |
| 179 | + yield* engine.dispatch({ |
| 180 | + type: "thread.title.generate.complete", |
| 181 | + commandId: CommandId.make("late-generation"), |
| 182 | + threadId, |
| 183 | + expectedTitle: "Original title", |
| 184 | + expectedVersion: null, |
| 185 | + title: "Unwanted generated title", |
| 186 | + needsRefinement: true, |
| 187 | + }); |
| 188 | + expect(yield* readThread()).toMatchObject({ |
| 189 | + title: "CU-869y9uv0 chosen title", |
| 190 | + titleState: { source: "manual", needsRefinement: false }, |
| 191 | + titleRegeneration: null, |
| 192 | + }); |
| 193 | + }).pipe(Effect.provide(TestLayer)), |
| 194 | +); |
| 195 | + |
| 196 | +it.effect("rejects missing, blank, and non-string titles without writing events", () => |
| 197 | + Effect.gen(function* () { |
| 198 | + const { engine, call, readThread } = yield* makeHarness; |
| 199 | + const before = yield* engine.latestSequence; |
| 200 | + for (const args of [{}, { title: "" }, { title: " \n\t " }, { title: 42 }]) { |
| 201 | + expect((yield* call(args).pipe(Effect.flip))._tag).toBe("InvalidParams"); |
| 202 | + } |
| 203 | + expect(yield* engine.latestSequence).toBe(before); |
| 204 | + expect(yield* readThread()).toMatchObject({ title: "Original title" }); |
| 205 | + }).pipe(Effect.provide(TestLayer)), |
| 206 | +); |
| 207 | + |
| 208 | +it.effect( |
| 209 | + "reports missing permission and missing threads as tool errors without renaming another thread", |
| 210 | + () => |
| 211 | + Effect.gen(function* () { |
| 212 | + const { engine, call, readThread } = yield* makeHarness; |
| 213 | + const before = yield* engine.latestSequence; |
| 214 | + const denied = yield* call( |
| 215 | + { title: "New title" }, |
| 216 | + { ...invocation, capabilities: new Set(["preview"]) }, |
| 217 | + ); |
| 218 | + expect(denied.isError).toBe(true); |
| 219 | + expect(denied.content).toEqual([ |
| 220 | + { type: "text", text: "MCP credential does not grant the thread-metadata capability." }, |
| 221 | + ]); |
| 222 | + const missing = yield* call( |
| 223 | + { title: "New title" }, |
| 224 | + { ...invocation, threadId: ThreadId.make("missing-thread") }, |
| 225 | + ); |
| 226 | + expect(missing.isError).toBe(true); |
| 227 | + expect(missing.content).toEqual([ |
| 228 | + { type: "text", text: "Could not rename the current thread." }, |
| 229 | + ]); |
| 230 | + expect(yield* engine.latestSequence).toBe(before); |
| 231 | + expect(yield* readThread()).toMatchObject({ title: "Original title" }); |
| 232 | + }).pipe(Effect.provide(TestLayer)), |
| 233 | +); |
0 commit comments