GeezerVison is showing github.com — feat(server): t3_thread_launch takes clientRequestId (#16654) · pingdotgg/t3code@888e444
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/toolkits/project/handlers.test.ts +162-3Lines changed: 162 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,10 +6,13 @@ import { | 6 | 6 | type Project as ProjectRecord, | 7 | 7 | type ModelSelection, | 8 | 8 | type RuntimeMode, | | 9 | + type OrchestrationV2ThreadShell, | 9 | 10 | } from "@t3tools/contracts"; | 10 | 11 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; | 11 | 12 | import * as NodeServices from "@effect/platform-node/NodeServices"; | | 13 | +import * as Cause from "effect/Cause"; | 12 | 14 | import * as Effect from "effect/Effect"; | | 15 | +import * as Exit from "effect/Exit"; | 13 | 16 | import * as FileSystem from "effect/FileSystem"; | 14 | 17 | import * as Path from "effect/Path"; | 15 | 18 | import * as Layer from "effect/Layer"; | @@ -44,6 +47,10 @@ const clientLaunchHarness = (input: { | 44 | 47 | instanceId: ProviderInstanceId.make("claudeAgent"), | 45 | 48 | model: "claude-opus", | 46 | 49 | }; | | 50 | + // The project's own default model, which a test can clear between launches. | | 51 | + const projectDefaults: { model: ModelSelection | null } = { | | 52 | + model: input.overrideModel === undefined ? modelSelection : null, | | 53 | + }; | 47 | 54 | const layerDependencies = Layer.mergeAll( | 48 | 55 | ServerSettings.layerTest({ | 49 | 56 | ...(input.defaultRuntimeMode === undefined | @@ -71,7 +78,22 @@ const clientLaunchHarness = (input: { | 71 | 78 | issuedAt: 0, | 72 | 79 | capabilities: new Set(["orchestration" as const]), | 73 | 80 | }), | 74 | | - Layer.mock(ThreadManagement.ThreadManagementService)({}), | | 81 | + Layer.mock(ThreadManagement.ThreadManagementService)({ | | 82 | + // A thread exists once a launch started it. | | 83 | + getThreadShell: (threadId) => { | | 84 | + const launch = input.launched.find((candidate) => candidate.threadId === threadId); | | 85 | + return Effect.succeed( | | 86 | + launch === undefined | | 87 | + ? null | | 88 | + : ({ | | 89 | + id: launch.threadId, | | 90 | + projectId: launch.projectId, | | 91 | + modelSelection: launch.modelSelection, | | 92 | + deletedAt: null, | | 93 | + } as unknown as OrchestrationV2ThreadShell), | | 94 | + ); | | 95 | + }, | | 96 | + }), | 75 | 97 | Layer.mock(ThreadLaunch.ThreadLaunchService)({ | 76 | 98 | checkWorktreeBase: input.checkWorktreeBase ?? (() => Effect.void), | 77 | 99 | launch: (launch) => { | @@ -98,7 +120,7 @@ const clientLaunchHarness = (input: { | 98 | 120 | ? Option.some({ | 99 | 121 | id, | 100 | 122 | workspaceRoot: input.workspaceRoot ?? "/projects/client-target", | 101 | | - defaultModelSelection: input.overrideModel === undefined ? modelSelection : null, | | 123 | + defaultModelSelection: projectDefaults.model, | 102 | 124 | } as unknown as ProjectRecord) | 103 | 125 | : Option.none(), | 104 | 126 | ), | @@ -115,7 +137,7 @@ const clientLaunchHarness = (input: { | 115 | 137 | ), | 116 | 138 | Layer.provideMerge(NodeServices.layer), | 117 | 139 | ); | 118 | | - return { projectId, modelSelection, dependencies: layerDependencies }; | | 140 | + return { projectId, modelSelection, projectDefaults, dependencies: layerDependencies }; | 119 | 141 | }; | 120 | 142 |
| 121 | 143 | it.effect( | @@ -200,6 +222,100 @@ it.effect( | 200 | 222 | }), | 201 | 223 | ); | 202 | 224 |
| | 225 | +it.effect("a launch retried with the same clientRequestId replays the first one", () => | | 226 | + Effect.gen(function* () { | | 227 | + const launched: Array<ThreadLaunch.ThreadLaunchInput> = []; | | 228 | + const { projectId, dependencies } = clientLaunchHarness({ | | 229 | + runtimeModeCeiling: "auto", | | 230 | + launched, | | 231 | + }); | | 232 | + const toolkit = yield* ProjectToolkit.pipe( | | 233 | + Effect.provide( | | 234 | + McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe(Layer.provide(dependencies)), | | 235 | + ), | | 236 | + ); | | 237 | + const launch = (clientRequestId: string | undefined) => | | 238 | + toolkit | | 239 | + .handle("t3_thread_launch", { | | 240 | + title: "Fix", | | 241 | + projectId, | | 242 | + message: "Fix the bug", | | 243 | + ...(clientRequestId === undefined ? {} : { clientRequestId }), | | 244 | + }) | | 245 | + .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies)); | | 246 | + | | 247 | + yield* launch("fix-1"); | | 248 | + yield* launch("fix-1"); | | 249 | + yield* launch("fix-2"); | | 250 | + yield* launch(undefined); | | 251 | + yield* launch(undefined); | | 252 | + const ids = launched.map((input) => [input.commandId, input.threadId]); | | 253 | + // The same key sends the same command and thread ids, which the launch | | 254 | + // service replays instead of starting a second thread. | | 255 | + expect(ids[0]).toEqual(ids[1]); | | 256 | + expect(ids[0]?.[0]).toMatch(/^mcp:launch:[0-9a-f]{64}$/); | | 257 | + expect(ids[2]).not.toEqual(ids[0]); | | 258 | + // Without a key every launch is new. | | 259 | + expect(ids[3]).not.toEqual(ids[4]); | | 260 | + }), | | 261 | +); | | 262 | + | | 263 | +it.effect("a launch key of any length or content gives a short id and never a defect", () => | | 264 | + Effect.gen(function* () { | | 265 | + const launched: Array<ThreadLaunch.ThreadLaunchInput> = []; | | 266 | + const { projectId, dependencies } = clientLaunchHarness({ | | 267 | + runtimeModeCeiling: "auto", | | 268 | + launched, | | 269 | + }); | | 270 | + const toolkit = yield* ProjectToolkit.pipe( | | 271 | + Effect.provide( | | 272 | + McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe(Layer.provide(dependencies)), | | 273 | + ), | | 274 | + ); | | 275 | + const launch = (clientRequestId: string) => | | 276 | + toolkit | | 277 | + .handle("t3_thread_launch", { title: "Fix", projectId, clientRequestId }) | | 278 | + .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies)); | | 279 | + | | 280 | + // Percent-encoding would grow this six times; scratch folders are named from the id. | | 281 | + yield* launch("\u00e9".repeat(256)); | | 282 | + expect(launched.map((input) => input.commandId)).toEqual([ | | 283 | + expect.stringMatching(/^mcp:launch:[0-9a-f]{64}$/), | | 284 | + ]); | | 285 | + | | 286 | + const loneSurrogate = yield* launch("key-\ud800").pipe(Effect.exit); | | 287 | + expect(Exit.isFailure(loneSurrogate) && Cause.hasDies(loneSurrogate.cause)).toBe(false); | | 288 | + expect(launched).toHaveLength(1); | | 289 | + }), | | 290 | +); | | 291 | + | | 292 | +it.effect("a retried launch replays after the project loses its default model", () => | | 293 | + Effect.gen(function* () { | | 294 | + const launched: Array<ThreadLaunch.ThreadLaunchInput> = []; | | 295 | + const { projectId, modelSelection, projectDefaults, dependencies } = clientLaunchHarness({ | | 296 | + runtimeModeCeiling: "auto", | | 297 | + launched, | | 298 | + }); | | 299 | + const toolkit = yield* ProjectToolkit.pipe( | | 300 | + Effect.provide( | | 301 | + McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe(Layer.provide(dependencies)), | | 302 | + ), | | 303 | + ); | | 304 | + const launch = toolkit | | 305 | + .handle("t3_thread_launch", { title: "Fix", projectId, clientRequestId: "fix-1" }) | | 306 | + .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies)); | | 307 | + | | 308 | + yield* launch; | | 309 | + projectDefaults.model = null; | | 310 | + const retried = (yield* launch).at(-1)?.result; | | 311 | + expect(retried).toMatchObject({ projectId, modelSelection }); | | 312 | + expect(launched.map((input) => input.threadId)).toEqual([ | | 313 | + launched[0]?.threadId, | | 314 | + launched[0]?.threadId, | | 315 | + ]); | | 316 | + }), | | 317 | +); | | 318 | + | 203 | 319 | it.effect("a launch binds only an existing checkout that is one of the project's worktrees", () => | 204 | 320 | Effect.gen(function* () { | 205 | 321 | const fileSystem = yield* FileSystem.FileSystem; | @@ -302,3 +418,46 @@ it.effect("a launch into a new worktree creates nothing when its base ref has no | 302 | 418 | expect(launched).toHaveLength(1); | 303 | 419 | }), | 304 | 420 | ); | | 421 | + | | 422 | +it.effect("a retried worktree launch replays after its base ref is gone", () => | | 423 | + Effect.gen(function* () { | | 424 | + const launched: Array<ThreadLaunch.ThreadLaunchInput> = []; | | 425 | + const base = { exists: true }; | | 426 | + const { projectId, dependencies } = clientLaunchHarness({ | | 427 | + runtimeModeCeiling: "auto", | | 428 | + launched, | | 429 | + checkWorktreeBase: ({ projectId, workspaceStrategy }) => | | 430 | + base.exists || workspaceStrategy.type !== "worktree" | | 431 | + ? Effect.void | | 432 | + : Effect.fail( | | 433 | + new ThreadLaunch.ThreadLaunchBaseRefError({ | | 434 | + projectId, | | 435 | + projectTitle: "Client target", | | 436 | + workspaceRoot: "/projects/client-target", | | 437 | + baseRef: workspaceStrategy.baseRef, | | 438 | + }), | | 439 | + ), | | 440 | + }); | | 441 | + const toolkit = yield* ProjectToolkit.pipe( | | 442 | + Effect.provide( | | 443 | + McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe(Layer.provide(dependencies)), | | 444 | + ), | | 445 | + ); | | 446 | + const launch = toolkit | | 447 | + .handle("t3_thread_launch", { | | 448 | + title: "Fix", | | 449 | + projectId, | | 450 | + clientRequestId: "fix-worktree", | | 451 | + workspaceStrategy: { type: "worktree", baseRef: "feature", startFromOrigin: false }, | | 452 | + }) | | 453 | + .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies)); | | 454 | + | | 455 | + yield* launch; | | 456 | + base.exists = false; | | 457 | + expect((yield* launch).at(-1)?.result).toMatchObject({ projectId }); | | 458 | + expect(launched.map((input) => input.threadId)).toEqual([ | | 459 | + launched[0]?.threadId, | | 460 | + launched[0]?.threadId, | | 461 | + ]); | | 462 | + }), | | 463 | +); |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/toolkits/project/handlers.ts +43-6Lines changed: 43 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { MessageId, ThreadId, OrchestratorMcpFailure, ProjectId } from "@t3tools/contracts"; | | 1 | +import { | | 2 | + CommandId, | | 3 | + MessageId, | | 4 | + ThreadId, | | 5 | + OrchestratorMcpFailure, | | 6 | + ProjectId, | | 7 | +} from "@t3tools/contracts"; | | 8 | +import * as Crypto from "effect/Crypto"; | 2 | 9 | import * as Effect from "effect/Effect"; | | 10 | +import * as Hex from "effect/encoding/Hex"; | 3 | 11 | import * as FileSystem from "effect/FileSystem"; | 4 | 12 | import * as Option from "effect/Option"; | 5 | 13 | import * as ThreadLaunch from "../../../orchestration-v2/ThreadLaunchService.ts"; | @@ -48,6 +56,18 @@ const assertProjectWorktree = Effect.fn("mcp.assertProjectWorktree")(function* ( | 48 | 56 | }); | 49 | 57 | }); | 50 | 58 |
| | 59 | +/** A fixed-length id, so a long key fits in the scratch folder name derived from it. */ | | 60 | +const launchCommandId = Effect.fn("mcp.launchCommandId")(function* ( | | 61 | + namespace: string, | | 62 | + key: string, | | 63 | +) { | | 64 | + const crypto = yield* Crypto.Crypto; | | 65 | + // @effect-diagnostics-next-line preferSchemaOverJson:off - a JSON pair keeps the caller apart from the key. | | 66 | + const bytes = new TextEncoder().encode(JSON.stringify([namespace, key])); | | 67 | + const digest = yield* crypto.digest("SHA-256", bytes).pipe(Effect.mapError(unavailable)); | | 68 | + return CommandId.make(`mcp:launch:${Hex.encode(digest)}`); | | 69 | +}); | | 70 | + | 51 | 71 | const access = Effect.gen(function* () { | 52 | 72 | yield* readCaller(); | 53 | 73 | return yield* Project.ProjectService; | @@ -58,8 +78,14 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { | 58 | 78 | (input, { runtimeMode, interactionMode }) => | 59 | 79 | Effect.gen(function* () { | 60 | 80 | const context = yield* readCaller(); | 61 | | - const { caller } = context; | 62 | | - const commandId = yield* newCommandId(); | | 81 | + const { caller, scope } = context; | | 82 | + // A retry with the same key replays the first launch: the ids derive | | 83 | + // from the caller and the key, and the launch service replays a | | 84 | + // command it already accepted. | | 85 | + const commandId = | | 86 | + input.clientRequestId === undefined | | 87 | + ? yield* newCommandId() | | 88 | + : yield* launchCommandId(scope.requestNamespace, input.clientRequestId); | 63 | 89 | const threadId = ThreadId.make(commandId); | 64 | 90 | const messageId = MessageId.make(commandId); | 65 | 91 | const attachments = input.attachments ?? []; | @@ -95,7 +121,17 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { | 95 | 121 | Effect.mapError(unavailable), | 96 | 122 | Effect.map(Option.getOrUndefined), | 97 | 123 | ); | 98 | | - if (input.workspaceStrategy?.type === "existing_worktree") { | | 124 | + // A retry of an accepted launch replays it: it keeps the model it started | | 125 | + // with and skips the checks only a new launch needs, which the project | | 126 | + // may no longer pass, such as a base branch deleted since. | | 127 | + const launched = | | 128 | + input.clientRequestId === undefined | | 129 | + ? null | | 130 | + : yield* context.threads.getThreadShell(threadId).pipe(Effect.mapError(unavailable)); | | 131 | + const launchedModel = | | 132 | + launched === null || launched.deletedAt !== null ? undefined : launched.modelSelection; | | 133 | + const replaying = launchedModel !== undefined; | | 134 | + if (!replaying && input.workspaceStrategy?.type === "existing_worktree") { | 99 | 135 | const project = yield* readProject; | 100 | 136 | if (project === undefined) | 101 | 137 | return yield* new OrchestratorMcpFailure({ | @@ -106,7 +142,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { | 106 | 142 | } | 107 | 143 | // Reject unresolved base refs before accepting a launch; worktree provisioning happens later. | 108 | 144 | const strategy = input.workspaceStrategy; | 109 | | - if (strategy?.type === "worktree") | | 145 | + if (!replaying && strategy?.type === "worktree") | 110 | 146 | yield* ThreadLaunch.ThreadLaunchService.pipe( | 111 | 147 | Effect.flatMap((launches) => | 112 | 148 | launches.checkWorktreeBase({ projectId, workspaceStrategy: strategy }), | @@ -127,7 +163,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { | 127 | 163 | // project's effective defaults, as the composer does. Its mode stays | 128 | 164 | // within the ceiling it was approved with. | 129 | 165 | const projectDefaults = | 130 | | - caller === undefined | | 166 | + caller === undefined && !replaying | 131 | 167 | ? yield* ServerSettings.ServerSettingsService.pipe( | 132 | 168 | Effect.flatMap((settings) => settings.getSettings), | 133 | 169 | Effect.mapError(unavailable), | @@ -141,6 +177,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { | 141 | 177 | : undefined; | 142 | 178 | const modelSelection = | 143 | 179 | input.modelSelection ?? | | 180 | + launchedModel ?? | 144 | 181 | caller?.modelSelection ?? | 145 | 182 | projectDefaults?.defaultModelSelection ?? | 146 | 183 | undefined; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/toolkits/project/tools.ts +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ import { | 13 | 13 | ProjectCreatePayload, | 14 | 14 | ProjectUpdatePayload, | 15 | 15 | ProjectId, | | 16 | + OrchestratorMcpClientRequestId, | 16 | 17 | OrchestratorMcpFailure, | 17 | 18 | SourceControlCloneRepositoryInput, | 18 | 19 | SourceControlCloneRepositoryResult, | @@ -101,7 +102,7 @@ const ProjectCloneTool = Tool.make("t3_project_clone", { | 101 | 102 | const ThreadLaunchTool = Tool.make("t3_thread_launch", { | 102 | 103 | ...shared, | 103 | 104 | description: | 104 | | - "Create one new top-level thread, bound to its workspace before the agent starts. Task in `message`. Omit `projectId`, model and modes to inherit yours (outside a thread, pass `projectId` or `scratch`); modes cannot exceed yours. No retry key: after an error or lost reply, check `t3_thread_list` before retrying. Follow it with `t3_thread_read` or `t3_thread_wait`. Mention it with its `link`.", | | 105 | + "Create one new top-level thread, bound to its workspace before the agent starts. Task in `message`. Omit `projectId`, model and modes to inherit yours (outside a thread, pass `projectId` or `scratch`); modes cannot exceed yours. After an error or lost reply, retry with the same `clientRequestId`. Follow it with `t3_thread_read` or `t3_thread_wait`. Mention it with its `link`.", | 105 | 106 | parameters: Schema.Struct({ | 106 | 107 | projectId: Schema.optional(ProjectId), | 107 | 108 | scratch: Schema.optional( | @@ -126,6 +127,7 @@ const ThreadLaunchTool = Tool.make("t3_thread_launch", { | 126 | 127 | }), | 127 | 128 | ), | 128 | 129 | attachments: Schema.optional(Schema.Array(McpAttachmentInput).check(Schema.isMaxLength(8))), | | 130 | + clientRequestId: Schema.optional(OrchestratorMcpClientRequestId), | 129 | 131 | }), | 130 | 132 | success: Schema.Struct({ | 131 | 133 | threadId: ThreadId, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ThreadLaunchService.test.ts +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1423,6 +1423,40 @@ it.effect("replays an accepted launch after the project disables the instance", | 1423 | 1423 | }).pipe(Effect.provide(harness.layer)); | 1424 | 1424 | }); | 1425 | 1425 |
| | 1426 | +it.effect("an agent's launch never starts work above the modes it asks for", () => { | | 1427 | + const harness = makeHarness(); | | 1428 | + return Effect.gen(function* () { | | 1429 | + const launches = yield* ThreadLaunch.ThreadLaunchService; | | 1430 | + const threads = yield* ThreadManagement.ThreadManagementService; | | 1431 | + // The first launch, at full access, creates the thread without a message. | | 1432 | + const created = yield* launches.launch( | | 1433 | + launchInput({ command: "command:launch:narrowed-retry", thread: "thread:narrowed" }), | | 1434 | + ); | | 1435 | + // The same key comes back from a narrower agent, now carrying a message, | | 1436 | + // as a retry or a concurrent launch that lost the create would. | | 1437 | + const failed = yield* launches | | 1438 | + .launch({ | | 1439 | + ...launchInput({ | | 1440 | + command: "command:launch:narrowed-retry", | | 1441 | + thread: "thread:narrowed", | | 1442 | + message: "Run this", | | 1443 | + }), | | 1444 | + runtimeMode: "approval-required", | | 1445 | + interactionMode: "plan", | | 1446 | + createdBy: "agent", | | 1447 | + creationSource: "mcp", | | 1448 | + }) | | 1449 | + .pipe(Effect.flip); | | 1450 | + if (failed._tag !== "ThreadLaunchError") { | | 1451 | + assert.fail(`expected ThreadLaunchError, got ${failed._tag}`); | | 1452 | + } | | 1453 | + assert.equal(failed.operation, "dispatch-message"); | | 1454 | + const projection = yield* threads.getThreadProjection(created.threadId); | | 1455 | + assert.equal(projection.runs.length, 0); | | 1456 | + assert.equal(projection.messages.length, 0); | | 1457 | + }).pipe(Effect.provide(harness.layer)); | | 1458 | +}); | | 1459 | + | 1426 | 1460 | it.effect("rejects a server-allocated launch replay with a mismatching thread id", () => { | 1427 | 1461 | const harness = makeHarness(); | 1428 | 1462 | return Effect.gen(function* () { | |
|