Skip to content

Commit b9f47b8

Browse files
feat(server): import linked conversations atomically
1 parent 33174eb commit b9f47b8

17 files changed

Lines changed: 847 additions & 5 deletions

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

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

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

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

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import * as Effect from "effect/Effect";
99
import * as FileSystem from "effect/FileSystem";
1010
import * as Option from "effect/Option";
11+
import * as ThreadImport from "../../../orchestration-v2/ThreadImportService.ts";
1112
import * as ThreadLaunch from "../../../orchestration-v2/ThreadLaunchService.ts";
1213
import * as ThreadMessageIntake from "../../../orchestration-v2/ThreadMessageIntake.ts";
1314
import * as Claims from "../../../orchestration-v2/AttachmentClaims.ts";
@@ -57,6 +58,24 @@ const access = Effect.gen(function* () {
5758
return yield* Project.ProjectService;
5859
});
5960
export const layer = McpToolAccess.toLayer(ProjectToolkit, {
61+
t3_thread_import: McpToolAccess.startsThreads(
62+
(input) => input,
63+
(input, modes) =>
64+
Effect.gen(function* () {
65+
const imports = yield* ThreadImport.ThreadImportService;
66+
return yield* imports.importThread({ ...input, ...modes }).pipe(
67+
Effect.mapError(
68+
(error) =>
69+
new OrchestratorMcpFailure({
70+
code:
71+
error._tag === "ThreadImportError" ? "orchestration_error" : "invalid_request",
72+
message: error.message,
73+
}),
74+
),
75+
);
76+
}),
77+
"stamped",
78+
),
6079
t3_thread_launch: McpToolAccess.startsThreads(
6180
(input) => input,
6281
(input, { runtimeMode, interactionMode, linkOrigin }) =>

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { McpAttachmentInput } from "../attachment/input.ts";
22
import {
3+
OrchestratorMcpThreadImportInput,
4+
OrchestratorMcpThreadImportResult,
35
NonNegativeInt,
46
OrchestratorMcpEnvironmentTarget,
57
OrchestratorMcpClientRequestId,
@@ -22,6 +24,7 @@ import {
2224
import * as FileSystem from "effect/FileSystem";
2325
import * as ServerConfig from "../../../config.ts";
2426
import * as ServerSettings from "../../../serverSettings.ts";
27+
import * as ThreadImportService from "../../../orchestration-v2/ThreadImportService.ts";
2528
import * as ThreadLaunchService from "../../../orchestration-v2/ThreadLaunchService.ts";
2629
import * as Crypto from "effect/Crypto";
2730
import * as Schema from "effect/Schema";
@@ -171,8 +174,23 @@ const ThreadLaunchTool = Tool.make("t3_thread_launch", {
171174
.annotate(Tool.Destructive, true)
172175
.annotate(Tool.OpenWorld, true);
173176

177+
const ThreadImportTool = Tool.make("t3_thread_import", {
178+
...shared,
179+
description:
180+
"Import a conversation into a registered project locally or in a linked environment with environmentId. Reuse source environmentId, threadId and handoffId for retries; the first accepted body is preserved. History becomes context for the first provider turn. worktreePath must already be one of the project's Git worktrees; omit it for the project root. Omit continuationPrompt to leave the thread idle. Modes cannot exceed the caller's.",
181+
parameters: Schema.Struct({
182+
...OrchestratorMcpThreadImportInput.fields,
183+
environmentId: OrchestratorMcpEnvironmentTarget,
184+
}),
185+
success: OrchestratorMcpThreadImportResult,
186+
dependencies: [...shared.dependencies, ThreadImportService.ThreadImportService],
187+
})
188+
.annotate(Tool.Destructive, true)
189+
.annotate(Tool.OpenWorld, true);
190+
174191
export const ProjectToolkit = Toolkit.make(
175192
ThreadLaunchTool,
193+
ThreadImportTool,
176194
ProjectListTool,
177195
ProjectReadTool,
178196
ProjectCreateTool,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import * as ThreadLaunchService from "../../../orchestration-v2/ThreadLaunchServ
2929
import * as ThreadSearch from "../../../orchestration-v2/ThreadSearch.ts";
3030
import * as PreviewManager from "../../../preview/Manager.ts";
3131
import * as SourceControlRepositoryService from "../../../sourceControl/SourceControlRepositoryService.ts";
32+
import * as ThreadImportService from "../../../orchestration-v2/ThreadImportService.ts";
3233
import * as GitVcsDriver from "../../../vcs/GitVcsDriver.ts";
3334
import * as McpHttpServer from "../../McpHttpServer.ts";
3435
import * as McpSessionRegistry from "../../McpSessionRegistry.ts";
@@ -37,6 +38,7 @@ import * as PreviewBrowser from "../../../preview/PreviewBrowser.ts";
3738

3839
const layerStubServices = Layer.mergeAll(
3940
Layer.mock(Orchestrator.OrchestratorV2)({}),
41+
Layer.mock(ThreadImportService.ThreadImportService)({}),
4042
Layer.mock(ProjectionStore.ProjectionStoreV2)({}),
4143
Layer.mock(DeviceService.DeviceService)({}),
4244
Layer.mock(ThreadManagementService.ThreadManagementService)({}),

‎apps/server/src/orchestration-v2/ContextHandoffService.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function makeLegacyImportSummary(items: ReadonlyArray<OrchestrationV2TurnItem>):
153153
}
154154
});
155155
const header =
156-
"Imported conversation history from the previous T3 Code orchestrator. Use it as context; do not repeat it unless the user asks.";
156+
"Imported conversation history. Use it as context; do not repeat it unless the user asks.";
157157
const maxChars = 32_000;
158158
const selected: Array<string> = [];
159159
let remaining = maxChars - header.length - 2;

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import {
6060
RuntimeMode,
6161
ThreadLinkedPullRequest,
6262
ThreadId,
63-
type TurnItemId,
63+
TurnItemId,
6464
} from "@t3tools/contracts";
6565
import { modelSelectionsEqual } from "@t3tools/shared/model";
6666
import {
@@ -2272,6 +2272,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
22722272
branch: command.branch,
22732273
worktreePath: command.worktreePath,
22742274
activeProviderThreadId: null,
2275+
...(command.importedConversation === undefined
2276+
? {}
2277+
: { historyOrigin: "v1_import" as const }),
22752278
...(command.linkOrigin === undefined ? {} : { linkOrigin: command.linkOrigin }),
22762279
pullRequests: [],
22772280
lineage: {
@@ -5384,7 +5387,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
53845387
const latestHandoffRun = projection.runs.findLast(isHandoffSourceRun);
53855388
const legacyImportItems =
53865389
projection.thread.historyOrigin === "v1_import"
5387-
? yield* readHandoffItems(command.threadId, [null])
5390+
? (yield* Ref.get(events)).some(
5391+
(event) => event.threadId === command.threadId && event.type === "thread.created",
5392+
)
5393+
? projection.turnItems.filter((item) => item.runId === null)
5394+
: yield* readHandoffItems(command.threadId, [null])
53885395
: [];
53895396
const isProviderSwitch =
53905397
activeProviderThread !== undefined &&
@@ -10977,6 +10984,95 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1097710984
switch (command.type) {
1097810985
case "thread.create":
1097910986
yield* dispatchThreadCreate(command, events);
10987+
if (command.importedConversation !== undefined) {
10988+
const importedAt = yield* DateTime.now;
10989+
for (const [index, message] of command.importedConversation.messages.entries()) {
10990+
const messageId = MessageId.make(`${command.threadId}:history:${index}`);
10991+
const at = DateTime.makeUnsafe(message.createdAt);
10992+
const common = {
10993+
id: TurnItemId.make(`${command.threadId}:history:${index}`),
10994+
threadId: command.threadId,
10995+
runId: null,
10996+
nodeId: null,
10997+
providerThreadId: null,
10998+
providerTurnId: null,
10999+
nativeItemRef: null,
11000+
parentItemId: null,
11001+
ordinal: index + 1,
11002+
status: "completed" as const,
11003+
title: null,
11004+
startedAt: at,
11005+
completedAt: at,
11006+
updatedAt: importedAt,
11007+
};
11008+
yield* emit(
11009+
events,
11010+
command,
11011+
)({
11012+
type: "message.updated",
11013+
threadId: command.threadId,
11014+
occurredAt: importedAt,
11015+
payload: {
11016+
id: messageId,
11017+
threadId: command.threadId,
11018+
runId: null,
11019+
nodeId: null,
11020+
createdBy: message.role === "user" ? "user" : "agent",
11021+
creationSource: "server",
11022+
role: message.role,
11023+
text: message.text,
11024+
attachments: [],
11025+
streaming: false,
11026+
createdAt: at,
11027+
updatedAt: importedAt,
11028+
},
11029+
});
11030+
yield* emit(
11031+
events,
11032+
command,
11033+
)({
11034+
type: "turn-item.updated",
11035+
threadId: command.threadId,
11036+
occurredAt: importedAt,
11037+
payload:
11038+
message.role === "user"
11039+
? {
11040+
...common,
11041+
type: "user_message",
11042+
messageId,
11043+
text: message.text,
11044+
attachments: [],
11045+
createdBy: "user",
11046+
creationSource: "server",
11047+
inputIntent: "turn_start",
11048+
}
11049+
: {
11050+
...common,
11051+
type: "assistant_message",
11052+
messageId,
11053+
text: message.text,
11054+
streaming: false,
11055+
},
11056+
});
11057+
}
11058+
if (command.importedConversation.continuationPrompt !== undefined) {
11059+
yield* dispatchMessage(
11060+
{
11061+
type: "message.dispatch",
11062+
commandId: command.commandId,
11063+
threadId: command.threadId,
11064+
messageId: MessageId.make(`${command.threadId}:continuation`),
11065+
text: command.importedConversation.continuationPrompt,
11066+
attachments: [],
11067+
dispatchMode: { type: "start_immediately" },
11068+
createdBy: command.createdBy,
11069+
creationSource: command.creationSource,
11070+
},
11071+
events,
11072+
effects,
11073+
);
11074+
}
11075+
}
1098011076
break;
1098111077
case "thread.visit":
1098211078
yield* dispatchThreadVisit(command, events);

0 commit comments

Comments
 (0)