@@ -63,6 +63,7 @@ const adapter = {
6363
6464interface HarnessOptions {
6565 readonly createWorktree ?: GitWorkflow . GitWorkflowService [ "Service" ] [ "createWorktree" ] ;
66+ readonly renameBranch ?: GitWorkflow . GitWorkflowService [ "Service" ] [ "renameBranch" ] ;
6667 readonly runSetup ?: ProjectSetupScriptRunner . ProjectSetupScriptRunner [ "Service" ] [ "runForThread" ] ;
6768 readonly generateTitle ?: TextGeneration . TextGeneration [ "Service" ] [ "generateThreadTitle" ] ;
6869 readonly generateBranchName ?: TextGeneration . TextGeneration [ "Service" ] [ "generateBranchName" ] ;
@@ -83,9 +84,9 @@ function makeHarness(options: HarnessOptions = {}) {
8384 const outbox = EffectOutbox . layer . pipe ( Layer . provide ( database ) ) ;
8485 const createWorktree = vi . fn (
8586 options . createWorktree ??
86- ( ( ) =>
87+ ( ( input ) =>
8788 Effect . succeed ( {
88- worktree : { path : "/repo-worktrees/feature" , refName : "feature" , headSha : "abc" } ,
89+ worktree : { path : "/repo-worktrees/feature" , refName : input . newRefName , headSha : "abc" } ,
8990 } as never ) ) ,
9091 ) ;
9192 const runSetup = vi . fn (
@@ -94,6 +95,12 @@ function makeHarness(options: HarnessOptions = {}) {
9495 const generateBranchName = vi . fn (
9596 options . generateBranchName ?? ( ( ) => Effect . succeed ( { branch : "generated-branch" } ) ) ,
9697 ) ;
98+ const generateTitle = vi . fn (
99+ options . generateTitle ?? ( ( ) => Effect . succeed ( { title : "Generated title" } ) ) ,
100+ ) ;
101+ const renameBranch = vi . fn (
102+ options . renameBranch ?? ( ( input ) => Effect . succeed ( { branch : input . newBranch } ) ) ,
103+ ) ;
97104 const externalServices = Layer . mergeAll (
98105 Layer . succeed ( ProjectService . ProjectService , {
99106 create : ( ) => Effect . die ( "unused" ) ,
@@ -106,6 +113,7 @@ function makeHarness(options: HarnessOptions = {}) {
106113 } ) ,
107114 Layer . mock ( GitWorkflow . GitWorkflowService ) ( {
108115 createWorktree,
116+ renameBranch,
109117 fetchRemote : ( ) => Effect . void ,
110118 removeWorktree : ( ) => Effect . void ,
111119 resolveRemoteTrackingCommit : ( ) =>
@@ -115,8 +123,7 @@ function makeHarness(options: HarnessOptions = {}) {
115123 runForThread : runSetup ,
116124 } ) ,
117125 Layer . mock ( TextGeneration . TextGeneration ) ( {
118- generateThreadTitle :
119- options . generateTitle ?? ( ( ) => Effect . succeed ( { title : "Generated title" } ) ) ,
126+ generateThreadTitle : generateTitle ,
120127 generateBranchName,
121128 } ) ,
122129 ServerSettings . layerTest ( options . serverSettings ) ,
@@ -128,7 +135,9 @@ function makeHarness(options: HarnessOptions = {}) {
128135 return {
129136 layer : Layer . mergeAll ( launch , threadManagement , outbox , database ) ,
130137 createWorktree,
138+ renameBranch,
131139 generateBranchName,
140+ generateTitle,
132141 runSetup,
133142 } ;
134143}
@@ -137,13 +146,16 @@ function launchInput(input: {
137146 readonly command : string ;
138147 readonly thread : string ;
139148 readonly message ?: string ;
149+ readonly title ?: string ;
150+ readonly titleSeed ?: string ;
140151 readonly workspace ?: ThreadLaunch . ThreadLaunchWorkspaceStrategy ;
141152} ) {
142153 return {
143154 commandId : CommandId . make ( input . command ) ,
144155 threadId : ThreadId . make ( input . thread ) ,
145156 projectId,
146- title : "New thread" ,
157+ title : input . title ?? "New thread" ,
158+ ...( input . titleSeed === undefined ? { } : { titleSeed : input . titleSeed } ) ,
147159 modelSelection,
148160 runtimeMode : "full-access" as const ,
149161 interactionMode : "default" as const ,
@@ -548,6 +560,249 @@ it.effect("falls back when the source control writer is unavailable", () =>
548560 } ) ,
549561) ;
550562
563+ it . effect ( "replaces a title seeded from the first message with a generated title" , ( ) =>
564+ Effect . gen ( function * ( ) {
565+ const harness = makeHarness ( ) ;
566+ yield * Effect . gen ( function * ( ) {
567+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
568+ const threads = yield * ThreadManagement . ThreadManagementService ;
569+ const launched = yield * launches . launch (
570+ launchInput ( {
571+ command : "command:launch:seeded-title" ,
572+ thread : "thread:launch:seeded-title" ,
573+ message : "fix the login bug please" ,
574+ title : "fix the login bug please" ,
575+ } ) ,
576+ ) ;
577+ yield * waitUntil ( ( ) =>
578+ threads
579+ . getThreadProjection ( launched . threadId )
580+ . pipe ( Effect . map ( ( projection ) => projection . thread . title === "Generated title" ) ) ,
581+ ) ;
582+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
583+ } ) ,
584+ ) ;
585+
586+ it . effect ( "replaces a title matching the client title seed with a generated title" , ( ) =>
587+ Effect . gen ( function * ( ) {
588+ const harness = makeHarness ( ) ;
589+ yield * Effect . gen ( function * ( ) {
590+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
591+ const threads = yield * ThreadManagement . ThreadManagementService ;
592+ const launched = yield * launches . launch (
593+ launchInput ( {
594+ command : "command:launch:title-seed" ,
595+ thread : "thread:launch:title-seed" ,
596+ message : "the full message body with extra context appended" ,
597+ title : "Image: screenshot.png" ,
598+ titleSeed : "Image: screenshot.png" ,
599+ } ) ,
600+ ) ;
601+ yield * waitUntil ( ( ) =>
602+ threads
603+ . getThreadProjection ( launched . threadId )
604+ . pipe ( Effect . map ( ( projection ) => projection . thread . title === "Generated title" ) ) ,
605+ ) ;
606+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
607+ } ) ,
608+ ) ;
609+
610+ it . effect ( "keeps a user-provided title instead of generating one" , ( ) =>
611+ Effect . gen ( function * ( ) {
612+ const harness = makeHarness ( ) ;
613+ yield * Effect . gen ( function * ( ) {
614+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
615+ const threads = yield * ThreadManagement . ThreadManagementService ;
616+ const launched = yield * launches . launch (
617+ launchInput ( {
618+ command : "command:launch:custom-title" ,
619+ thread : "thread:launch:custom-title" ,
620+ message : "fix the login bug please" ,
621+ title : "My custom title" ,
622+ } ) ,
623+ ) ;
624+ yield * waitUntil ( ( ) =>
625+ threads
626+ . getThreadProjection ( launched . threadId )
627+ . pipe ( Effect . map ( ( projection ) => projection . runs [ 0 ] ?. status === "starting" ) ) ,
628+ ) ;
629+ assert . equal ( harness . generateTitle . mock . calls . length , 0 ) ;
630+ assert . equal (
631+ ( yield * threads . getThreadProjection ( launched . threadId ) ) . thread . title ,
632+ "My custom title" ,
633+ ) ;
634+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
635+ } ) ,
636+ ) ;
637+
638+ it . effect ( "names the worktree itself when the client provides no branch" , ( ) =>
639+ Effect . gen ( function * ( ) {
640+ const harness = makeHarness ( ) ;
641+ yield * Effect . gen ( function * ( ) {
642+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
643+ const threads = yield * ThreadManagement . ThreadManagementService ;
644+ const launched = yield * launches . launch (
645+ launchInput ( {
646+ command : "command:launch:server-named-branch" ,
647+ thread : "thread:launch:server-named-branch" ,
648+ message : "Build the feature" ,
649+ workspace : { type : "worktree" , baseRef : "main" } ,
650+ } ) ,
651+ ) ;
652+ yield * waitUntil ( ( ) => Effect . sync ( ( ) => harness . createWorktree . mock . calls . length === 1 ) ) ;
653+ assert . match (
654+ harness . createWorktree . mock . calls [ 0 ] ?. [ 0 ] . newRefName ?? "" ,
655+ / ^ t 3 c o d e \/ [ 0 - 9 a - f ] { 8 } $ / u,
656+ ) ;
657+ yield * waitUntil ( ( ) =>
658+ threads
659+ . getThreadProjection ( launched . threadId )
660+ . pipe ( Effect . map ( ( projection ) => projection . thread . branch === "generated-branch" ) ) ,
661+ ) ;
662+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
663+ } ) ,
664+ ) ;
665+
666+ it . effect ( "renames a temporary t3code/<hash> branch off the provisioning critical path" , ( ) =>
667+ Effect . gen ( function * ( ) {
668+ const branchNameStarted = yield * Deferred . make < void > ( ) ;
669+ const allowBranchName = yield * Deferred . make < void > ( ) ;
670+ const harness = makeHarness ( {
671+ createWorktree : ( input ) =>
672+ Effect . succeed ( {
673+ worktree : { path : "/repo-worktrees/temp" , refName : input . newRefName , headSha : "abc" } ,
674+ } as never ) ,
675+ generateBranchName : ( ) =>
676+ Deferred . succeed ( branchNameStarted , undefined ) . pipe (
677+ Effect . andThen ( Deferred . await ( allowBranchName ) ) ,
678+ Effect . as ( { branch : "generated-branch" } ) ,
679+ ) ,
680+ } ) ;
681+ yield * Effect . gen ( function * ( ) {
682+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
683+ const threads = yield * ThreadManagement . ThreadManagementService ;
684+ const launched = yield * launches . launch (
685+ launchInput ( {
686+ command : "command:launch:temp-branch" ,
687+ thread : "thread:launch:temp-branch" ,
688+ message : "Build the feature" ,
689+ workspace : { type : "worktree" , baseRef : "main" , branch : "t3code/abcd1234" } ,
690+ } ) ,
691+ ) ;
692+ yield * Deferred . await ( branchNameStarted ) ;
693+ assert . equal ( harness . createWorktree . mock . calls [ 0 ] ?. [ 0 ] . newRefName , "t3code/abcd1234" ) ;
694+ yield * waitUntil ( ( ) =>
695+ threads
696+ . getThreadProjection ( launched . threadId )
697+ . pipe ( Effect . map ( ( projection ) => projection . runs [ 0 ] ?. status === "starting" ) ) ,
698+ ) ;
699+ assert . equal (
700+ ( yield * threads . getThreadProjection ( launched . threadId ) ) . thread . branch ,
701+ "t3code/abcd1234" ,
702+ ) ;
703+ yield * Deferred . succeed ( allowBranchName , undefined ) ;
704+ yield * waitUntil ( ( ) =>
705+ threads
706+ . getThreadProjection ( launched . threadId )
707+ . pipe ( Effect . map ( ( projection ) => projection . thread . branch === "generated-branch" ) ) ,
708+ ) ;
709+ assert . deepEqual ( harness . renameBranch . mock . calls [ 0 ] ?. [ 0 ] , {
710+ cwd : "/repo-worktrees/temp" ,
711+ oldBranch : "t3code/abcd1234" ,
712+ newBranch : "generated-branch" ,
713+ } ) ;
714+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
715+ } ) ,
716+ ) ;
717+
718+ it . effect ( "keeps an explicit branch name instead of generating one" , ( ) =>
719+ Effect . gen ( function * ( ) {
720+ const harness = makeHarness ( ) ;
721+ yield * Effect . gen ( function * ( ) {
722+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
723+ yield * launches . launch (
724+ launchInput ( {
725+ command : "command:launch:explicit-branch" ,
726+ thread : "thread:launch:explicit-branch" ,
727+ message : "Build the feature" ,
728+ workspace : { type : "worktree" , baseRef : "main" , branch : "my-feature" } ,
729+ } ) ,
730+ ) ;
731+ yield * waitUntil ( ( ) => Effect . sync ( ( ) => harness . createWorktree . mock . calls . length === 1 ) ) ;
732+ assert . equal ( harness . generateBranchName . mock . calls . length , 0 ) ;
733+ assert . equal ( harness . createWorktree . mock . calls [ 0 ] ?. [ 0 ] . newRefName , "my-feature" ) ;
734+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
735+ } ) ,
736+ ) ;
737+
738+ it . effect ( "keeps the temporary branch when branch generation fails" , ( ) =>
739+ Effect . gen ( function * ( ) {
740+ const harness = makeHarness ( {
741+ createWorktree : ( input ) =>
742+ Effect . succeed ( {
743+ worktree : { path : "/repo-worktrees/temp" , refName : input . newRefName , headSha : "abc" } ,
744+ } as never ) ,
745+ generateBranchName : ( ) => Effect . die ( "branch generation is down" ) ,
746+ } ) ;
747+ yield * Effect . gen ( function * ( ) {
748+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
749+ const threads = yield * ThreadManagement . ThreadManagementService ;
750+ const launched = yield * launches . launch (
751+ launchInput ( {
752+ command : "command:launch:branch-fallback" ,
753+ thread : "thread:launch:branch-fallback" ,
754+ message : "Build the feature" ,
755+ workspace : { type : "worktree" , baseRef : "main" , branch : "t3code/abcd1234" } ,
756+ } ) ,
757+ ) ;
758+ yield * waitUntil ( ( ) => Effect . sync ( ( ) => harness . generateBranchName . mock . calls . length === 1 ) ) ;
759+ assert . equal ( harness . createWorktree . mock . calls [ 0 ] ?. [ 0 ] . newRefName , "t3code/abcd1234" ) ;
760+ yield * waitUntil ( ( ) =>
761+ threads
762+ . getThreadProjection ( launched . threadId )
763+ . pipe ( Effect . map ( ( projection ) => projection . runs [ 0 ] ?. status === "starting" ) ) ,
764+ ) ;
765+ assert . equal ( harness . renameBranch . mock . calls . length , 0 ) ;
766+ assert . equal (
767+ ( yield * threads . getThreadProjection ( launched . threadId ) ) . thread . branch ,
768+ "t3code/abcd1234" ,
769+ ) ;
770+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
771+ } ) ,
772+ ) ;
773+
774+ it . effect ( "renames a temporary branch on an existing worktree to a generated name" , ( ) =>
775+ Effect . gen ( function * ( ) {
776+ const harness = makeHarness ( ) ;
777+ yield * Effect . gen ( function * ( ) {
778+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
779+ const threads = yield * ThreadManagement . ThreadManagementService ;
780+ const launched = yield * launches . launch (
781+ launchInput ( {
782+ command : "command:launch:existing-worktree-rename" ,
783+ thread : "thread:launch:existing-worktree-rename" ,
784+ message : "Build the feature" ,
785+ workspace : {
786+ type : "existing_worktree" ,
787+ worktreePath : "/repo-worktrees/t3code-abcd1234" ,
788+ branch : "t3code/abcd1234" ,
789+ } ,
790+ } ) ,
791+ ) ;
792+ yield * waitUntil ( ( ) =>
793+ threads
794+ . getThreadProjection ( launched . threadId )
795+ . pipe ( Effect . map ( ( projection ) => projection . thread . branch === "generated-branch" ) ) ,
796+ ) ;
797+ assert . deepEqual ( harness . renameBranch . mock . calls [ 0 ] ?. [ 0 ] , {
798+ cwd : "/repo-worktrees/t3code-abcd1234" ,
799+ oldBranch : "t3code/abcd1234" ,
800+ newBranch : "generated-branch" ,
801+ } ) ;
802+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
803+ } ) ,
804+ ) ;
805+
551806for ( const failurePoint of [ "worktree" , "setup" ] as const ) {
552807 it . effect (
553808 `${ failurePoint } failure keeps the thread and message visible and emits failure items` ,
0 commit comments