@@ -34,6 +34,22 @@ function formatCommand(program, args) {
3434 return [ program , ...args . map ( shellQuote ) ] . join ( ' ' ) ;
3535}
3636
37+ function buildTemplateVariables ( values ) {
38+ return Object . entries ( values ) . reduce ( ( accumulator , [ key , value ] ) => {
39+ const stringValue = String ( value ) ;
40+ const quotedValue = shellQuote ( stringValue ) ;
41+
42+ accumulator [ key ] = stringValue ;
43+ accumulator [ `${ key } _raw` ] = stringValue ;
44+ accumulator [ `${ key } _sh` ] = quotedValue ;
45+ return accumulator ;
46+ } , { } ) ;
47+ }
48+
49+ function buildSessionBannerCommand ( sessionName , coordinationDir ) {
50+ return `printf '%s\\n' ${ shellQuote ( `Session: ${ sessionName } ` ) } ${ shellQuote ( `Coordination: ${ coordinationDir } ` ) } ` ;
51+ }
52+
3753function normalizeSeedPaths ( seedPaths , repoRoot ) {
3854 const resolvedRepoRoot = path . resolve ( repoRoot ) ;
3955 const entries = Array . isArray ( seedPaths ) ? seedPaths : [ ] ;
@@ -239,7 +255,7 @@ function buildOrchestrationPlan(config = {}) {
239255 'send-keys' ,
240256 '-t' ,
241257 sessionName ,
242- `printf '%s\\n' 'Session: ${ sessionName } ' 'Coordination: ${ coordinationDir } '` ,
258+ buildSessionBannerCommand ( sessionName , coordinationDir ) ,
243259 'C-m'
244260 ] ,
245261 description : 'Print orchestrator session details'
@@ -400,14 +416,82 @@ function cleanupExisting(plan) {
400416 }
401417}
402418
403- function executePlan ( plan ) {
404- runCommand ( 'git' , [ 'rev-parse' , '--is-inside-work-tree' ] , { cwd : plan . repoRoot } ) ;
405- runCommand ( 'tmux' , [ '-V' ] ) ;
419+ function rollbackCreatedResources ( plan , createdState , runtime = { } ) {
420+ const runCommandImpl = runtime . runCommand || runCommand ;
421+ const listWorktreesImpl = runtime . listWorktrees || listWorktrees ;
422+ const branchExistsImpl = runtime . branchExists || branchExists ;
423+ const errors = [ ] ;
424+
425+ if ( createdState . sessionCreated ) {
426+ try {
427+ runCommandImpl ( 'tmux' , [ 'kill-session' , '-t' , plan . sessionName ] , { cwd : plan . repoRoot } ) ;
428+ } catch ( error ) {
429+ errors . push ( error . message ) ;
430+ }
431+ }
432+
433+ for ( const workerPlan of [ ...createdState . workerPlans ] . reverse ( ) ) {
434+ const expectedWorktreePath = canonicalizePath ( workerPlan . worktreePath ) ;
435+ const existingWorktree = listWorktreesImpl ( plan . repoRoot ) . find (
436+ worktree => worktree . canonicalPath === expectedWorktreePath
437+ ) ;
438+
439+ if ( existingWorktree ) {
440+ try {
441+ runCommandImpl ( 'git' , [ 'worktree' , 'remove' , '--force' , existingWorktree . listedPath ] , {
442+ cwd : plan . repoRoot
443+ } ) ;
444+ } catch ( error ) {
445+ errors . push ( error . message ) ;
446+ }
447+ } else if ( fs . existsSync ( workerPlan . worktreePath ) ) {
448+ fs . rmSync ( workerPlan . worktreePath , { force : true , recursive : true } ) ;
449+ }
450+
451+ try {
452+ runCommandImpl ( 'git' , [ 'worktree' , 'prune' , '--expire' , 'now' ] , { cwd : plan . repoRoot } ) ;
453+ } catch ( error ) {
454+ errors . push ( error . message ) ;
455+ }
456+
457+ if ( branchExistsImpl ( plan . repoRoot , workerPlan . branchName ) ) {
458+ try {
459+ runCommandImpl ( 'git' , [ 'branch' , '-D' , workerPlan . branchName ] , { cwd : plan . repoRoot } ) ;
460+ } catch ( error ) {
461+ errors . push ( error . message ) ;
462+ }
463+ }
464+ }
465+
466+ if ( createdState . removeCoordinationDir && fs . existsSync ( plan . coordinationDir ) ) {
467+ fs . rmSync ( plan . coordinationDir , { force : true , recursive : true } ) ;
468+ }
469+
470+ if ( errors . length > 0 ) {
471+ throw new Error ( `rollback failed: ${ errors . join ( '; ' ) } ` ) ;
472+ }
473+ }
474+
475+ function executePlan ( plan , runtime = { } ) {
476+ const spawnSyncImpl = runtime . spawnSync || spawnSync ;
477+ const runCommandImpl = runtime . runCommand || runCommand ;
478+ const materializePlanImpl = runtime . materializePlan || materializePlan ;
479+ const overlaySeedPathsImpl = runtime . overlaySeedPaths || overlaySeedPaths ;
480+ const cleanupExistingImpl = runtime . cleanupExisting || cleanupExisting ;
481+ const rollbackCreatedResourcesImpl = runtime . rollbackCreatedResources || rollbackCreatedResources ;
482+ const createdState = {
483+ workerPlans : [ ] ,
484+ sessionCreated : false ,
485+ removeCoordinationDir : ! fs . existsSync ( plan . coordinationDir )
486+ } ;
487+
488+ runCommandImpl ( 'git' , [ 'rev-parse' , '--is-inside-work-tree' ] , { cwd : plan . repoRoot } ) ;
489+ runCommandImpl ( 'tmux' , [ '-V' ] ) ;
406490
407491 if ( plan . replaceExisting ) {
408- cleanupExisting ( plan ) ;
492+ cleanupExistingImpl ( plan ) ;
409493 } else {
410- const hasSession = spawnSync ( 'tmux' , [ 'has-session' , '-t' , plan . sessionName ] , {
494+ const hasSession = spawnSyncImpl ( 'tmux' , [ 'has-session' , '-t' , plan . sessionName ] , {
411495 encoding : 'utf8' ,
412496 stdio : [ 'ignore' , 'pipe' , 'pipe' ]
413497 } ) ;
@@ -416,61 +500,76 @@ function executePlan(plan) {
416500 }
417501 }
418502
419- materializePlan ( plan ) ;
420-
421- for ( const workerPlan of plan . workerPlans ) {
422- runCommand ( 'git' , workerPlan . gitArgs , { cwd : plan . repoRoot } ) ;
423- overlaySeedPaths ( {
424- repoRoot : plan . repoRoot ,
425- seedPaths : workerPlan . seedPaths ,
426- worktreePath : workerPlan . worktreePath
427- } ) ;
428- }
429-
430- runCommand (
431- 'tmux' ,
432- [ 'new-session' , '-d' , '-s' , plan . sessionName , '-n' , 'orchestrator' , '-c' , plan . repoRoot ] ,
433- { cwd : plan . repoRoot }
434- ) ;
435- runCommand (
436- 'tmux' ,
437- [
438- 'send-keys' ,
439- '-t' ,
440- plan . sessionName ,
441- `printf '%s\\n' 'Session: ${ plan . sessionName } ' 'Coordination: ${ plan . coordinationDir } '` ,
442- 'C-m'
443- ] ,
444- { cwd : plan . repoRoot }
445- ) ;
503+ try {
504+ materializePlanImpl ( plan ) ;
505+
506+ for ( const workerPlan of plan . workerPlans ) {
507+ runCommandImpl ( 'git' , workerPlan . gitArgs , { cwd : plan . repoRoot } ) ;
508+ createdState . workerPlans . push ( workerPlan ) ;
509+ overlaySeedPathsImpl ( {
510+ repoRoot : plan . repoRoot ,
511+ seedPaths : workerPlan . seedPaths ,
512+ worktreePath : workerPlan . worktreePath
513+ } ) ;
514+ }
446515
447- for ( const workerPlan of plan . workerPlans ) {
448- const splitResult = runCommand (
516+ runCommandImpl (
449517 'tmux' ,
450- [ 'split-window ' , '-d' , '-P ' , '-F' , '#{pane_id} ' , '-t ' , plan . sessionName , '-c' , workerPlan . worktreePath ] ,
518+ [ 'new-session ' , '-d' , '-s ' , plan . sessionName , '-n ' , 'orchestrator ' , '-c' , plan . repoRoot ] ,
451519 { cwd : plan . repoRoot }
452520 ) ;
453- const paneId = splitResult . stdout . trim ( ) ;
454-
455- if ( ! paneId ) {
456- throw new Error ( `tmux split-window did not return a pane id for ${ workerPlan . workerName } ` ) ;
457- }
458-
459- runCommand ( 'tmux' , [ 'select-layout' , '-t' , plan . sessionName , 'tiled' ] , { cwd : plan . repoRoot } ) ;
460- runCommand ( 'tmux' , [ 'select-pane' , '-t' , paneId , '-T' , workerPlan . workerSlug ] , {
461- cwd : plan . repoRoot
462- } ) ;
463- runCommand (
521+ createdState . sessionCreated = true ;
522+ runCommandImpl (
464523 'tmux' ,
465524 [
466525 'send-keys' ,
467526 '-t' ,
468- paneId ,
469- `cd ${ shellQuote ( workerPlan . worktreePath ) } && ${ workerPlan . launchCommand } ` ,
527+ plan . sessionName ,
528+ buildSessionBannerCommand ( plan . sessionName , plan . coordinationDir ) ,
470529 'C-m'
471530 ] ,
472531 { cwd : plan . repoRoot }
473532 ) ;
533+
534+ for ( const workerPlan of plan . workerPlans ) {
535+ const splitResult = runCommandImpl (
536+ 'tmux' ,
537+ [ 'split-window' , '-d' , '-P' , '-F' , '#{pane_id}' , '-t' , plan . sessionName , '-c' , workerPlan . worktreePath ] ,
538+ { cwd : plan . repoRoot }
539+ ) ;
540+ const paneId = splitResult . stdout . trim ( ) ;
541+
542+ if ( ! paneId ) {
543+ throw new Error ( `tmux split-window did not return a pane id for ${ workerPlan . workerName } ` ) ;
544+ }
545+
546+ runCommandImpl ( 'tmux' , [ 'select-layout' , '-t' , plan . sessionName , 'tiled' ] , { cwd : plan . repoRoot } ) ;
547+ runCommandImpl ( 'tmux' , [ 'select-pane' , '-t' , paneId , '-T' , workerPlan . workerSlug ] , {
548+ cwd : plan . repoRoot
549+ } ) ;
550+ runCommandImpl (
551+ 'tmux' ,
552+ [
553+ 'send-keys' ,
554+ '-t' ,
555+ paneId ,
556+ `cd ${ shellQuote ( workerPlan . worktreePath ) } && ${ workerPlan . launchCommand } ` ,
557+ 'C-m'
558+ ] ,
559+ { cwd : plan . repoRoot }
560+ ) ;
561+ }
562+ } catch ( error ) {
563+ try {
564+ rollbackCreatedResourcesImpl ( plan , createdState , {
565+ branchExists : runtime . branchExists ,
566+ listWorktrees : runtime . listWorktrees ,
567+ runCommand : runCommandImpl
568+ } ) ;
569+ } catch ( cleanupError ) {
570+ error . message = `${ error . message } ; cleanup failed: ${ cleanupError . message } ` ;
571+ }
572+ throw error ;
474573 }
475574
476575 return {
@@ -486,6 +585,7 @@ module.exports = {
486585 materializePlan,
487586 normalizeSeedPaths,
488587 overlaySeedPaths,
588+ rollbackCreatedResources,
489589 renderTemplate,
490590 slugify
491591} ;
0 commit comments