feat(server): t3_thread_import, conversation only #16731
main fromt3code/peer/thread-importopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
juliusmarminge added labelsmacroscope-review
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- @@ -0,0 +60,21 @@60+ crypto61+ .digest(62+ "SHA-256",63+ new TextEncoder().encode(64+ `${source.environmentId}\n${source.threadId}\n${source.handoffId}`,65+ ),66+ )67+ .pipe(68+ Effect.map((digest) => ThreadId.make(`thread:import:${Hex.encode(digest).slice(0, 32)}`)),69+ Effect.orDie,70+ );71+72+ const historyEvents = (73+ threadId: ThreadId,74+ messages: OrchestratorMcpThreadImportInput["messages"],75+ ): ReadonlyArray<OrchestrationV2DomainEvent> =>76+ messages.flatMap((message, index) => {77+ const suffix = String(index).padStart(6, "0");78+ const messageId = MessageId.make(`${threadId}:${suffix}`);79+ const turnItemId = TurnItemId.make(`${IMPORT_EVENT_PREFIX}:turn-item:${threadId}:${suffix}`);80+ const at = DateTime.makeUnsafe(message.createdAt);@@ -0,0 +196,21 @@196+ })197+ .pipe(198+ // A concurrent retry may have written the same events first.199+ Effect.catchCause((cause) =>200+ threads.getThreadShell(threadId).pipe(201+ Effect.orElseSucceed(() => null),202+ Effect.flatMap((raced) => (raced === null ? Effect.failCause(cause) : Effect.void)),203+ ),204+ ),205+ Effect.mapError(() =>206+ failure("orchestration_error", "The thread could not be imported."),207+ ),208+ );209+ created = true;210+ } else if (existing.projectId !== input.projectId) {211+ return yield* failure(212+ "invalid_request",213+ `This handoff was already imported into another project (${existing.projectId}).`,214+ );215+ }216+ const runId = yield* startContinuation(threadId, input);@@ -0,0 +222,21 @@222+ threadId: ThreadId,223+ input: OrchestratorMcpThreadImportInput,224+ ): Effect.Effect<RunId | null, OrchestratorMcpFailure> =>225+ input.continuationPrompt === undefined226+ ? Effect.succeed(null)227+ : threads228+ .dispatch({229+ type: "message.dispatch",230+ commandId: CommandId.make(`command:thread-import:${threadId}:continue`),231+ threadId,232+ messageId: MessageId.make(`${threadId}:continue`),233+ text: input.continuationPrompt,234+ attachments: [],235+ modelSelection: input.modelSelection,236+ dispatchMode: { type: "start_immediately" },237+ createdBy: "agent",238+ creationSource: "mcp",239+ })240+ .pipe(241+ Effect.flatMap(() => threads.getThreadRecords(threadId, ["runs"])),242+ Effect.map((records) => Option.fromNullishOr(records.runs.at(-1)?.id)),
- @@ -0,0 +196,21 @@196+ })197+ .pipe(198+ // A concurrent retry may have written the same events first.199+ Effect.catchCause((cause) =>200+ threads.getThreadShell(threadId).pipe(201+ Effect.orElseSucceed(() => null),202+ Effect.flatMap((raced) => (raced === null ? Effect.failCause(cause) : Effect.void)),203+ ),204+ ),205+ Effect.mapError(() =>206+ failure("orchestration_error", "The thread could not be imported."),207+ ),208+ );209+ created = true;210+ } else if (existing.projectId !== input.projectId) {211+ return yield* failure(212+ "invalid_request",213+ `This handoff was already imported into another project (${existing.projectId}).`,214+ );215+ }216+ const runId = yield* startContinuation(threadId, input);
- @@ -209,3 +210,20 @@209210 // The launched thread carries the caller's link, if it has one.210211 "stamped",211212 ),213+ t3_thread_import: McpToolAccess.startsThreads(214+ (input) => input,215+ (input, { runtimeMode, interactionMode, linkOrigin }) =>216+ Effect.gen(function* () {217+ const projects = yield* access;218+ const project = yield* projects219+ .getById(input.projectId)220+ .pipe(Effect.mapError(unavailable), Effect.map(Option.getOrUndefined));221+ if (project === undefined)222+ return yield* new OrchestratorMcpFailure({223+ code: "invalid_request",224+ message: "The project was not found.",225+ });226+ if (input.worktreePath !== undefined)227+ yield* assertProjectWorktree(project.workspaceRoot, input.worktreePath);228+ const imports = yield* ThreadImportService.ThreadImportService;229+ return yield* imports.importThread({ ...input, runtimeMode, interactionMode, linkOrigin });
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
github-actions[bot](GitHub profile, opens in a new tab) added labels📱 Native Change
github-actions[bot](GitHub profile, opens in a new tab) removed labels📱 Native Change
- @@ -203,3 +204,17 @@203204 // The launched thread carries the caller's link, if it has one.204205 "stamped",205206 ),207+ t3_thread_import: McpToolAccess.startsThreads(208+ (input) => input,209+ (input, { runtimeMode, interactionMode, linkOrigin }) =>210+ Effect.gen(function* () {211+ const projects = yield* access;212+ const project = yield* projects213+ .getById(input.projectId)214+ .pipe(Effect.mapError(unavailable), Effect.map(Option.getOrUndefined));215+ if (project === undefined)216+ return yield* new OrchestratorMcpFailure({217+ code: "invalid_request",218+ message: "The project was not found.",219+ });220+ if (input.worktreePath !== undefined)
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XXL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- @@ -0,0 +189,21 @@189+ threadId,190+ providerInstanceId: input.modelSelection.instanceId,191+ occurredAt: now,192+ payload: thread,193+ },194+ ...historyEvents(threadId, input.messages),195+ ],196+ })197+ .pipe(198+ // A concurrent retry may have written the same events first.199+ Effect.catchCause((cause) =>200+ threads.getThreadShell(threadId).pipe(201+ Effect.orElseSucceed(() => null),202+ Effect.flatMap((raced) => (raced === null ? Effect.failCause(cause) : Effect.void)),203+ ),204+ ),205+ Effect.mapError(() =>206+ failure("orchestration_error", "The thread could not be imported."),207+ ),208+ );209+ created = true;
juliusmarminge assignedjuliusmarminge
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XXL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- @@ -0,0 +82,21 @@82+ input: OrchestratorMcpThreadImportInput & {83+ readonly runtimeMode: RuntimeMode;84+ readonly interactionMode: ProviderInteractionMode;85+ readonly linkOrigin: OrchestrationV2LinkOrigin | undefined;86+ },87+ ) => Effect.Effect<OrchestratorMcpThreadImportResult, ThreadImportError>;88+ }89+>()("t3/orchestration-v2/ThreadImportService") {}90+91+const make = Effect.gen(function* () {92+ const sink = yield* EventSink.EventSinkV2;93+ const threads = yield* ThreadManagement.ThreadManagementService;94+ const crypto = yield* Crypto.Crypto;95+96+ /** The thread an import of `source` creates here, the same on every retry. */97+ const importedThreadId = (source: OrchestratorMcpThreadImportInput["source"]) =>98+ crypto99+ .digest(100+ "SHA-256",101+ new TextEncoder().encode(102+ `${source.environmentId}\n${source.threadId}\n${source.handoffId}`,
tanstack-source added labelsfeature
juliusmarminge closed this
Sign in to join this conversation.
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Baseline:
5e9d564· PR result:d1d2890· Source CI: failureScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.