@@ -477,6 +477,112 @@ describe("OrchestrationEngine", () => {
477477 await system . dispose ( ) ;
478478 } ) ;
479479
480+ it ( "does not regress a generated branch to a stale temporary worktree branch" , async ( ) => {
481+ const system = await createOrchestrationSystem ( ) ;
482+ const { engine } = system ;
483+ const createdAt = now ( ) ;
484+
485+ await system . run (
486+ engine . dispatch ( {
487+ type : "project.create" ,
488+ commandId : CommandId . make ( "cmd-branch-race-project-create" ) ,
489+ projectId : asProjectId ( "project-branch-race" ) ,
490+ title : "Branch Race Project" ,
491+ workspaceRoot : "/tmp/project-branch-race" ,
492+ defaultModelSelection : {
493+ instanceId : ProviderInstanceId . make ( "codex" ) ,
494+ model : "gpt-5-codex" ,
495+ } ,
496+ createdAt,
497+ } ) ,
498+ ) ;
499+ await system . run (
500+ engine . dispatch ( {
501+ type : "thread.create" ,
502+ commandId : CommandId . make ( "cmd-branch-race-thread-create" ) ,
503+ threadId : ThreadId . make ( "thread-branch-race" ) ,
504+ projectId : asProjectId ( "project-branch-race" ) ,
505+ title : "Branch Race Thread" ,
506+ modelSelection : {
507+ instanceId : ProviderInstanceId . make ( "codex" ) ,
508+ model : "gpt-5-codex" ,
509+ } ,
510+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
511+ runtimeMode : "approval-required" ,
512+ branch : "t3code/generated-branch-name" ,
513+ worktreePath : "/tmp/project-branch-race-worktree" ,
514+ createdAt,
515+ } ) ,
516+ ) ;
517+
518+ await system . run (
519+ engine . dispatch ( {
520+ type : "thread.meta.update" ,
521+ commandId : CommandId . make ( "cmd-stale-temporary-branch-sync" ) ,
522+ threadId : ThreadId . make ( "thread-branch-race" ) ,
523+ branch : "t3code/1234abcd" ,
524+ expectedBranch : "t3code/1234abcd" ,
525+ } ) ,
526+ ) ;
527+
528+ const snapshot = await system . readModel ( ) ;
529+ expect ( snapshot . threads [ 0 ] ?. branch ) . toBe ( "t3code/generated-branch-name" ) ;
530+ await system . dispose ( ) ;
531+ } ) ;
532+
533+ it ( "allows authoritative worktree bootstrap to assign a temporary branch" , async ( ) => {
534+ const system = await createOrchestrationSystem ( ) ;
535+ const { engine } = system ;
536+ const createdAt = now ( ) ;
537+
538+ await system . run (
539+ engine . dispatch ( {
540+ type : "project.create" ,
541+ commandId : CommandId . make ( "cmd-worktree-bootstrap-project-create" ) ,
542+ projectId : asProjectId ( "project-worktree-bootstrap" ) ,
543+ title : "Worktree Bootstrap Project" ,
544+ workspaceRoot : "/tmp/project-worktree-bootstrap" ,
545+ defaultModelSelection : {
546+ instanceId : ProviderInstanceId . make ( "codex" ) ,
547+ model : "gpt-5-codex" ,
548+ } ,
549+ createdAt,
550+ } ) ,
551+ ) ;
552+ await system . run (
553+ engine . dispatch ( {
554+ type : "thread.create" ,
555+ commandId : CommandId . make ( "cmd-worktree-bootstrap-thread-create" ) ,
556+ threadId : ThreadId . make ( "thread-worktree-bootstrap" ) ,
557+ projectId : asProjectId ( "project-worktree-bootstrap" ) ,
558+ title : "Worktree Bootstrap Thread" ,
559+ modelSelection : {
560+ instanceId : ProviderInstanceId . make ( "codex" ) ,
561+ model : "gpt-5-codex" ,
562+ } ,
563+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
564+ runtimeMode : "approval-required" ,
565+ branch : "main" ,
566+ worktreePath : null ,
567+ createdAt,
568+ } ) ,
569+ ) ;
570+ await system . run (
571+ engine . dispatch ( {
572+ type : "thread.meta.update" ,
573+ commandId : CommandId . make ( "cmd-authoritative-worktree-bootstrap" ) ,
574+ threadId : ThreadId . make ( "thread-worktree-bootstrap" ) ,
575+ branch : "t3code/1234abcd" ,
576+ worktreePath : "/tmp/project-worktree-bootstrap-worktree" ,
577+ } ) ,
578+ ) ;
579+
580+ const snapshot = await system . readModel ( ) ;
581+ expect ( snapshot . threads [ 0 ] ?. branch ) . toBe ( "t3code/1234abcd" ) ;
582+ expect ( snapshot . threads [ 0 ] ?. worktreePath ) . toBe ( "/tmp/project-worktree-bootstrap-worktree" ) ;
583+ await system . dispose ( ) ;
584+ } ) ;
585+
480586 it ( "records command ack duration using the first committed event type" , async ( ) => {
481587 const system = await createOrchestrationSystem ( ) ;
482588 const { engine } = system ;
0 commit comments