@@ -97,6 +97,51 @@ func (m *appModel) handleBranchFromEdit(msg messages.BranchFromEditMsg) (tea.Mod
9797 )
9898}
9999
100+ func (m * appModel ) handleForkSession () (tea.Model , tea.Cmd ) {
101+ currentSession := m .application .Session ()
102+ if currentSession == nil {
103+ return m , notification .ErrorCmd ("No active session to fork" )
104+ }
105+
106+ store := m .application .SessionStore ()
107+ if store == nil {
108+ return m , notification .ErrorCmd ("No session store configured" )
109+ }
110+
111+ spawner := m .supervisor .Spawner ()
112+ if spawner == nil {
113+ return m , notification .ErrorCmd ("Session spawning not available" )
114+ }
115+
116+ ctx := context .Background ()
117+
118+ // Fork the session and clone all messages.
119+ forkedSession , err := session .BranchSession (currentSession , len (currentSession .Messages ))
120+ if err != nil {
121+ return m , notification .ErrorCmd (fmt .Sprintf ("Failed to fork session: %v" , err ))
122+ }
123+
124+ if err := store .AddSession (ctx , forkedSession ); err != nil {
125+ return m , notification .ErrorCmd (fmt .Sprintf ("Failed to save forked session: %v" , err ))
126+ }
127+
128+ a , _ , cleanup , err := spawner (ctx , forkedSession .WorkingDir )
129+ if err != nil {
130+ return m , notification .ErrorCmd (fmt .Sprintf ("Failed to create runtime for fork: %v" , err ))
131+ }
132+
133+ a .ReplaceSession (ctx , forkedSession )
134+ m .supervisor .AddSession (ctx , a , forkedSession , forkedSession .WorkingDir , cleanup )
135+
136+ if m .tuiStore != nil {
137+ if err := m .tuiStore .AddTab (ctx , forkedSession .ID , forkedSession .WorkingDir ); err != nil {
138+ slog .Warn ("Failed to persist forked tab" , "error" , err )
139+ }
140+ }
141+
142+ return m .handleSwitchTab (forkedSession .ID )
143+ }
144+
100145func (m * appModel ) handleToggleSessionStar (sessionID string ) (tea.Model , tea.Cmd ) {
101146 store := m .application .SessionStore ()
102147 if store == nil {
0 commit comments