11import {
2+ ClaudeSessionAttachmentResult ,
23 OrchestratorMcpFailure ,
34 OrchestratorMcpThreadListResult ,
45 OrchestratorMcpThreadReadResult ,
@@ -24,7 +25,7 @@ import * as Runtime from "effect/Runtime";
2425import * as Schema from "effect/Schema" ;
2526import * as Stdio from "effect/Stdio" ;
2627import * as Stream from "effect/Stream" ;
27- import { Argument , Command , Flag , GlobalFlag } from "effect/cli" ;
28+ import { Argument , Command , Flag , GlobalFlag , Prompt } from "effect/cli" ;
2829
2930import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts" ;
3031import * as ServerConfig from "../config.ts" ;
@@ -696,7 +697,7 @@ const threadSendCommand = Command.make("send", {
696697 { threadId, message, mode : "queue" } ,
697698 OrchestratorMcpThreadSendResult ,
698699 ) ;
699- if ( flags . wait ) {
700+ if ( flags . wait && sent . runId !== null ) {
700701 return yield * finishRun ( tools , { threadId, runId : sent . runId , json : flags . json } ) ;
701702 }
702703 yield * flags . json
@@ -762,9 +763,161 @@ const threadShowCommand = Command.make("show", {
762763 ) ,
763764) ;
764765
766+ type HandoffFlags = { readonly wait : boolean ; readonly interrupt : boolean ; readonly json : boolean } ;
767+ const handoffFlags = {
768+ wait : Flag . Boolean ( "wait" ) . pipe (
769+ Flag . withDescription ( "Wait for the source CLI and its agents to exit." ) ,
770+ Flag . withDefault ( false ) ,
771+ ) ,
772+ interrupt : Flag . Boolean ( "interrupt" ) . pipe (
773+ Flag . withDescription ( "Stop the source CLI and its agents, then resume work in T3 (Linux)." ) ,
774+ Flag . withDefault ( false ) ,
775+ ) ,
776+ } ;
777+ const validateHandoffFlags = ( flags : HandoffFlags ) =>
778+ flags . wait && flags . interrupt
779+ ? Effect . fail ( new ThreadUsageError ( { detail : "Choose --wait or --interrupt, not both." } ) )
780+ : Effect . void ;
781+ const runHandoff = Effect . fn ( "cli.thread.runHandoff" ) ( function * (
782+ tools : ThreadTools ,
783+ threadId : ThreadId ,
784+ flags : HandoffFlags ,
785+ ) {
786+ yield * validateHandoffFlags ( flags ) ;
787+ const choose = flags . interrupt
788+ ? Effect . succeed ( "interrupt" as const )
789+ : flags . wait
790+ ? Effect . succeed ( "wait" as const )
791+ : Effect . gen ( function * ( ) {
792+ if ( flags . json || ! process . stdin . isTTY || ! process . stdout . isTTY )
793+ return yield * new ThreadUsageError ( {
794+ detail :
795+ "The source CLI is still running. Use --wait to wait for it and its agents to exit, or --interrupt to stop them and resume the conversation in T3." ,
796+ } ) ;
797+ const interrupt = yield * Prompt . run (
798+ Prompt . Confirm ( {
799+ message :
800+ "The source CLI is still running. Interrupt it and its agents, transfer the conversation, and resume work in T3? Choose No to wait for them to exit." ,
801+ initial : false ,
802+ } ) ,
803+ ) . pipe (
804+ Effect . mapError (
805+ ( ) =>
806+ new ThreadUsageError ( {
807+ detail : "Handoff cancelled; the source was not interrupted." ,
808+ } ) ,
809+ ) ,
810+ ) ;
811+ return interrupt ? ( "interrupt" as const ) : ( "wait" as const ) ;
812+ } ) ;
813+ const result = yield * handoffThread (
814+ tools ,
815+ threadId ,
816+ choose . pipe (
817+ Effect . tap ( ( policy ) =>
818+ flags . json
819+ ? Effect . void
820+ : Console . error (
821+ policy === "wait"
822+ ? "Waiting for the source CLI and its agents to exit. Ctrl+C cancels this wait."
823+ : "Stopping the source and its agents, then importing history and resuming in T3." ,
824+ ) ,
825+ ) ,
826+ ) ,
827+ ) ;
828+ return result ;
829+ } ) ;
830+
831+ const threadAttachCommand = Command . make ( "attach" , {
832+ ...projectLocationFlags ,
833+ ...handoffFlags ,
834+ session : Argument . String ( "session" ) ,
835+ provider : Flag . String ( "provider" ) . pipe ( Flag . optional ) ,
836+ project : Flag . String ( "project" ) . pipe ( Flag . withDefault ( "." ) ) ,
837+ json : jsonFlag ,
838+ } ) . pipe (
839+ Command . withDescription (
840+ "Move a local Claude conversation into T3, waiting or explicitly interrupting its source CLI." ,
841+ ) ,
842+ Command . withHandler ( ( flags ) =>
843+ withThreadTools (
844+ flags ,
845+ Effect . fn ( "cli.thread.attach" ) ( function * ( tools ) {
846+ yield * validateHandoffFlags ( flags ) ;
847+ const project = yield * resolveProject ( tools , flags . project ) ;
848+ const attached = yield * tools . call (
849+ "t3_thread_attach" ,
850+ {
851+ action : "attach" ,
852+ projectId : project . id ,
853+ session : flags . session ,
854+ ...Option . match ( flags . provider , {
855+ onNone : ( ) => ( { } ) ,
856+ onSome : ( providerInstanceId ) => ( { providerInstanceId } ) ,
857+ } ) ,
858+ } ,
859+ ClaudeSessionAttachmentResult ,
860+ ) ;
861+ const result = yield * runHandoff ( tools , attached . threadId , flags ) ;
862+ yield * flags . json
863+ ? printJson ( result )
864+ : Console . log ( `Handoff complete. Continue in ${ result . link ?? result . threadId } .` ) ;
865+ } ) ,
866+ ) . pipe ( reportThreadErrors ) ,
867+ ) ,
868+ ) ;
869+ export const handoffThread = Effect . fn ( "cli.thread.handoffThread" ) ( function * < R > (
870+ tools : ThreadTools ,
871+ threadId : ThreadId ,
872+ choose : Effect . Effect < "wait" | "interrupt" , ThreadUsageError , R > ,
873+ wait = Effect . sleep ( "1 second" ) ,
874+ ) {
875+ let policy : "wait" | "interrupt" | undefined ;
876+ while ( true ) {
877+ const result = yield * tools . call (
878+ "t3_thread_attach" ,
879+ { action : "handoff" , threadId, ...( policy === "interrupt" ? { interrupt : true } : { } ) } ,
880+ ClaudeSessionAttachmentResult ,
881+ ) ;
882+ if ( ! result . blocked ) return result ;
883+ if ( policy === "interrupt" ) {
884+ return yield * new ThreadUsageError ( {
885+ detail : "The source did not stop; handoff was not completed." ,
886+ } ) ;
887+ }
888+ policy ??= yield * choose ;
889+ if ( policy === "wait" ) yield * wait ;
890+ }
891+ } ) ;
892+
893+ const threadHandoffCommand = Command . make ( "handoff" , {
894+ ...projectLocationFlags ,
895+ thread : threadArgument ,
896+ json : jsonFlag ,
897+ ...handoffFlags ,
898+ } ) . pipe (
899+ Command . withDescription (
900+ "Continue an attached Claude conversation through T3 after Claude and its agents have exited." ,
901+ ) ,
902+ Command . withHandler ( ( flags ) =>
903+ withThreadTools (
904+ flags ,
905+ Effect . fn ( "cli.thread.handoff" ) ( function * ( tools ) {
906+ const threadId = yield * decodeThreadId ( flags . thread ) ;
907+ const result = yield * runHandoff ( tools , threadId , flags ) ;
908+ yield * flags . json
909+ ? printJson ( result )
910+ : Console . log ( `Handoff complete. Continue in ${ result . link ?? result . threadId } .` ) ;
911+ } ) ,
912+ ) . pipe ( reportThreadErrors ) ,
913+ ) ,
914+ ) ;
915+
765916export const threadCommand = Command . make ( "thread" ) . pipe (
766917 Command . withDescription ( "Start, message, and read threads on the running server." ) ,
767918 Command . withSubcommands ( [
919+ threadAttachCommand ,
920+ threadHandoffCommand ,
768921 threadListCommand ,
769922 threadStartCommand ,
770923 threadSendCommand ,
0 commit comments