Skip to content

Commit 1e24145

Browse files
feat(server): continue threads on a linked environment
1 parent 5a0b3de commit 1e24145

36 files changed

Lines changed: 3083 additions & 157 deletions

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type WsRpcMethod = RpcGroup.Rpcs<typeof WsRpcGroup>["_tag"];
4040
export const RPC_REQUIRED_SCOPES = {
4141
[WS_METHODS.peerLinksList]: AuthAccessReadScope,
4242
...CLIENT_GUARDED_RPC_SCOPES,
43+
[WS_METHODS.threadHandoffOptions]: AuthOrchestrationReadScope,
4344
[ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: AuthOrchestrationOperateScope,
4445
[ORCHESTRATION_V2_WS_METHODS.getWorkflowScript]: AuthOrchestrationReadScope,
4546
[ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: AuthOrchestrationOperateScope,

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ export const make = Effect.gen(function* () {
220220
mcpModeLimitHeader: true,
221221
linkFence: true,
222222
threadImport: true,
223+
threadHandoff: true,
223224
connectionProbe: true,
224225
attachmentUploads: true,
225226
questionAttachments: true,

‎apps/server/src/mcp/McpHttpServer.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ const HANDOFF_TOOLS: ReadonlySet<string> = new Set([
802802
"create_threads",
803803
"t3_thread_launch",
804804
"t3_thread_import",
805+
"t3_thread_handoff",
805806
"t3_thread_send",
806807
"t3_thread_send_attachments",
807808
"t3_thread_fork",

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,7 @@ function threadDetail(
778778
interactionMode: projection.thread.interactionMode,
779779
linkedPullRequest: projection.thread.linkedPullRequest ?? null,
780780
pullRequests: threadPullRequests(projection.thread.pullRequests),
781+
handoff: projection.thread.handoff ?? null,
781782
titleRegeneration:
782783
projection.thread.titleRegeneration === undefined ||
783784
projection.thread.titleRegeneration === null

‎apps/server/src/mcp/toolkits/project/handlers.ts‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as Effect from "effect/Effect";
99
import * as FileSystem from "effect/FileSystem";
1010
import * as Option from "effect/Option";
1111
import * as ThreadImport from "../../../orchestration-v2/ThreadImportService.ts";
12+
import * as HandoffImport from "../../../peer/handoff/HandoffImport.ts";
1213
import * as ThreadLaunch from "../../../orchestration-v2/ThreadLaunchService.ts";
1314
import * as ThreadMessageIntake from "../../../orchestration-v2/ThreadMessageIntake.ts";
1415
import * as Claims from "../../../orchestration-v2/AttachmentClaims.ts";
@@ -62,14 +63,36 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
6263
(input) => input,
6364
(input, modes) =>
6465
Effect.gen(function* () {
65-
const imports = yield* ThreadImport.ThreadImportService;
66-
return yield* imports.importThread({ ...input, ...modes }).pipe(
66+
const handoff = yield* Effect.serviceOption(HandoffImport.HandoffImport);
67+
const imports = yield* Effect.serviceOption(ThreadImport.ThreadImportService);
68+
if (Option.isNone(handoff) && (input.bundle !== undefined || Option.isNone(imports)))
69+
return yield* new OrchestratorMcpFailure({
70+
code: "invalid_request",
71+
message: "This server cannot receive handoff bundles.",
72+
});
73+
const accept = Option.isSome(handoff)
74+
? handoff.value.importThread({ ...input, ...modes })
75+
: Option.isSome(imports)
76+
? imports.value.importThread({ ...input, ...modes })
77+
: Effect.die("unreachable import service");
78+
return yield* accept.pipe(
6779
Effect.mapError(
6880
(error) =>
6981
new OrchestratorMcpFailure({
7082
code:
71-
error._tag === "ThreadImportError" ? "orchestration_error" : "invalid_request",
72-
message: error.message,
83+
error._tag === "ThreadImportError" || error._tag === "HandoffImportOperationError"
84+
? "orchestration_error"
85+
: "invalid_request",
86+
message:
87+
error._tag === "HandoffImportGitRefusedError"
88+
? error.cause.message
89+
: error.message,
90+
...(error._tag === "HandoffImportRefusedError"
91+
? { handoffNotAccepted: error.notAccepted }
92+
: error._tag === "HandoffImportProviderDisabledError" ||
93+
error._tag === "HandoffImportGitRefusedError"
94+
? { handoffNotAccepted: { source: error.source, projectId: error.projectId } }
95+
: {}),
7396
}),
7497
),
7598
);
@@ -228,11 +251,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
228251
t3_project_list: McpToolAccess.reads((input) =>
229252
Effect.gen(function* () {
230253
const projects = yield* access;
231-
const snapshot = yield* projects.snapshot.pipe(Effect.mapError(unavailable));
232-
const rows = snapshot.projects.filter((project) => project.deletedAt === null);
233-
const start = input.cursor ?? 0,
234-
end = start + (input.limit ?? 20);
235-
return { projects: rows.slice(start, end), nextCursor: end < rows.length ? end : null };
254+
return yield* projects.list(input).pipe(Effect.mapError(unavailable));
236255
}),
237256
),
238257
t3_project_read: McpToolAccess.reads((input) =>

‎apps/server/src/mcp/toolkits/project/tools.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import {
2424
import * as FileSystem from "effect/FileSystem";
2525
import * as ServerConfig from "../../../config.ts";
2626
import * as ServerSettings from "../../../serverSettings.ts";
27-
import * as ThreadImportService from "../../../orchestration-v2/ThreadImportService.ts";
2827
import * as ThreadLaunchService from "../../../orchestration-v2/ThreadLaunchService.ts";
2928
import * as Crypto from "effect/Crypto";
3029
import * as Schema from "effect/Schema";
3130
import { Tool, Toolkit } from "effect/ai";
3231
import * as ProjectService from "../../../project/ProjectService.ts";
32+
import * as RepositoryIdentityResolver from "../../../project/RepositoryIdentityResolver.ts";
3333
import * as ManagedProjectFolders from "../../../project/ManagedProjectFolders.ts";
3434
import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
3535
import * as SourceControlRepositoryService from "../../../sourceControl/SourceControlRepositoryService.ts";
@@ -49,12 +49,22 @@ const shared = {
4949
};
5050
const ProjectListTool = Tool.make("t3_project_list", {
5151
...shared,
52+
dependencies: [
53+
McpInvocationContext.McpInvocationContext,
54+
ThreadManagementService.ThreadManagementService,
55+
ProjectService.ProjectService,
56+
RepositoryIdentityResolver.RepositoryIdentityResolver,
57+
],
5258
description:
5359
"List registered projects locally or in a linked environment with environmentId. Pages use the current project snapshot and may shift between calls.",
5460
parameters: Schema.Struct({
5561
environmentId: OrchestratorMcpEnvironmentTarget,
5662
cursor: Schema.optional(NonNegativeInt),
5763
limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100 }))),
64+
resolveRepositoryIdentity: Schema.optional(Schema.Boolean).annotate({
65+
description:
66+
"Wait for repository identities on this page when matching a handoff destination.",
67+
}),
5868
}),
5969
success: Schema.Struct({
6070
projects: Schema.Array(Project),
@@ -65,6 +75,11 @@ const ProjectListTool = Tool.make("t3_project_list", {
6575
.annotate(Tool.Destructive, false);
6676
const ProjectReadTool = Tool.make("t3_project_read", {
6777
...shared,
78+
dependencies: [
79+
McpInvocationContext.McpInvocationContext,
80+
ThreadManagementService.ThreadManagementService,
81+
ProjectService.ProjectService,
82+
],
6883
description:
6984
"Read a registered project locally or in a linked environment with environmentId, including its workspace and saved scripts.",
7085
parameters: Schema.Struct({
@@ -183,7 +198,10 @@ const ThreadImportTool = Tool.make("t3_thread_import", {
183198
environmentId: OrchestratorMcpEnvironmentTarget,
184199
}),
185200
success: OrchestratorMcpThreadImportResult,
186-
dependencies: [...shared.dependencies, ThreadImportService.ThreadImportService],
201+
dependencies: [
202+
McpInvocationContext.McpInvocationContext,
203+
ThreadManagementService.ThreadManagementService,
204+
],
187205
})
188206
.annotate(Tool.Destructive, true)
189207
.annotate(Tool.OpenWorld, true);

‎apps/server/src/mcp/toolkits/thread/handlers.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import {
88
type OrchestrationV2Command,
99
} from "@t3tools/contracts";
1010
import * as Effect from "effect/Effect";
11+
import * as Option from "effect/Option";
1112
import { modelSelectionCommandType } from "@t3tools/shared/model";
1213

14+
import * as ThreadHandoff from "../../../peer/handoff/ThreadHandoff.ts";
15+
import * as McpInvocationContext from "../../McpInvocationContext.ts";
1316
import * as McpToolAccess from "../../McpToolAccess.ts";
1417
import {
1518
dispatchFailure,
@@ -75,6 +78,27 @@ const writesThread = <P extends { readonly threadId?: ThreadId | undefined }, A,
7578
) => McpToolAccess.writesThreads((params: P) => [params.threadId], handle);
7679

7780
export const layer = McpToolAccess.toLayer(ThreadToolkit, {
81+
t3_thread_handoff: writesThread((input) =>
82+
Effect.gen(function* () {
83+
const scope = yield* McpInvocationContext.McpInvocationContext;
84+
const handoff = yield* Effect.serviceOption(ThreadHandoff.ThreadHandoff);
85+
if (Option.isNone(handoff))
86+
return yield* new OrchestratorMcpFailure({
87+
code: "invalid_request",
88+
message: "This server does not support thread handoff.",
89+
});
90+
return yield* handoff.value.start({ ...input, callerThreadId: scope.thread?.threadId }).pipe(
91+
Effect.mapError(
92+
(cause) =>
93+
new OrchestratorMcpFailure({
94+
code: cause.reason === "failed" ? "orchestration_error" : "invalid_request",
95+
message: cause.message,
96+
}),
97+
),
98+
);
99+
}),
100+
),
101+
78102
run_scheduled_task_now: McpToolAccess.writesEnvironment((input) =>
79103
Effect.gen(function* () {
80104
const scheduler = yield* ScheduledTasks.ScheduledTaskService;

‎apps/server/src/mcp/toolkits/thread/tools.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {
22
OrchestratorMcpEnvironmentTarget,
3+
OrchestratorMcpThreadHandoffInput,
4+
OrchestratorMcpThreadHandoffResult,
35
ScheduledTaskId,
46
ScheduledTask,
57
OrchestrationSearchThreadsInput,
@@ -308,7 +310,23 @@ const ScheduledTaskRunTool = Tool.make("run_scheduled_task_now", {
308310
.annotate(Tool.Destructive, true)
309311
.annotate(Tool.OpenWorld, true);
310312

313+
const ThreadHandoffTool = Tool.make("t3_thread_handoff", {
314+
description:
315+
"Move a thread's conversation and Git work to a linked environment. Omit threadId to move your own thread after its active turn fully finishes. New instructions cancel a pending move. Pass projectId from that environment or omit it to match the repository. Read current handoff metadata with t3_thread_read. Retry a departing move with its handoffId and saved environmentId/projectId, omitting continuationPrompt; retries preserve the original request. The same ID after departure returns its saved destination.",
316+
parameters: OrchestratorMcpThreadHandoffInput,
317+
success: OrchestratorMcpThreadHandoffResult,
318+
failure: OrchestratorMcpFailure,
319+
failureMode: "return" as const,
320+
dependencies: [
321+
McpInvocationContext.McpInvocationContext,
322+
ThreadManagementService.ThreadManagementService,
323+
],
324+
})
325+
.annotate(Tool.Destructive, true)
326+
.annotate(Tool.OpenWorld, true);
327+
311328
export const ThreadToolkit = Toolkit.make(
329+
ThreadHandoffTool,
312330
ScheduledTaskRunTool,
313331
ThreadSearchTool,
314332
ThreadForkTool,

‎apps/server/src/mcp/toolkits/worktree/registration.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as GitWorkflowService from "../../../git/GitWorkflowService.ts";
1616
import * as ProviderAdapterRegistry from "../../../orchestration-v2/ProviderAdapterRegistry.ts";
1717
import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
1818
import * as ProjectService from "../../../project/ProjectService.ts";
19+
import * as RepositoryIdentityResolver from "../../../project/RepositoryIdentityResolver.ts";
1920
import * as PullRequestReviewGuideService from "../../../pullRequest/PullRequestReviewGuideService.ts";
2021
import * as ProjectSetupScriptRunner from "../../../project/ProjectSetupScriptRunner.ts";
2122
import * as ProviderRegistry from "../../../provider/ProviderRegistry.ts";
@@ -47,6 +48,7 @@ const layerStubServices = Layer.mergeAll(
4748
Layer.mock(ScheduledTaskService.ScheduledTaskService)({}),
4849
Layer.mock(SecretRequests.SecretRequests)({}),
4950
Layer.mock(ProjectService.ProjectService)({}),
51+
Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({}),
5052
Layer.mock(PullRequestReviewGuideService.PullRequestReviewGuideService)({}),
5153
ServerSettings.layerTest({}),
5254
Layer.mock(GitWorkflowService.GitWorkflowService)({}),

‎apps/server/src/observability/RpcInstrumentation.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ type WsRpcMethod = RpcGroup.Rpcs<typeof WsRpcGroup>["_tag"];
1414
* even where they differ from the method prefix.
1515
*/
1616
const RPC_AGGREGATES = {
17+
[WS_METHODS.threadHandoffOptions]: "threadHandoff",
18+
[WS_METHODS.threadHandoffStart]: "threadHandoff",
19+
[WS_METHODS.threadHandoffCancel]: "threadHandoff",
1720
[WS_METHODS.peerLinksList]: "peerLinks",
1821
[WS_METHODS.peerLinksLink]: "peerLinks",
1922
[WS_METHODS.peerLinksUnlink]: "peerLinks",

0 commit comments

Comments
 (0)