| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "reasonix/desktop/internal/workspacestate" |
| 5 | "reasonix/internal/control" |
| 6 | "reasonix/internal/session" |
| 7 | ) |
| 8 | |
| 9 | // runtimeRebuildMu and the destination turnStartMu are held. Reuse the exact |
| 10 | // live owner before asking the writer registry for a competing controller. |
| 11 | func (a *App) reattachCanonicalSessionRuntime(tab *WorkspaceTab, current control.SessionAPI, ref session.SessionRef, workspace workspacestate.Workspace, navigation uint64) (control.SessionAPI, error) { |
| 12 | preserveSource := controllerHasActiveRuntimeWork(current) |
| 13 | a.mu.Lock() |
| 14 | if navigation != 0 && a.desktopSessions.navigationSeq.Load() != navigation { |
| 15 | a.mu.Unlock() |
| 16 | return nil, errSessionNavigationSuperseded |
| 17 | } |
| 18 | source := a.liveRuntimeTabMatchingLocked(tab, sessionRoute(ref.SessionID)) |
| 19 | if source == nil || source == tab || source.Ctrl == nil || !source.Ready { |
| 20 | a.mu.Unlock() |
| 21 | return nil, nil |
| 22 | } |
| 23 | if source.SessionWorkspace.ID != workspace.ID || canonicalWorkspaceScope(workspace) != source.Scope { |
| 24 | a.mu.Unlock() |
| 25 | return nil, errSessionWorkspaceConflict |
| 26 | } |
| 27 | if a.tabs[tab.ID] != tab || tab.Ctrl != current || tab.removed { |
| 28 | a.mu.Unlock() |
| 29 | return nil, errSessionNavigationSuperseded |
| 30 | } |
| 31 | oldHost := tab.SharedHostKey |
| 32 | oldSink := tab.sink |
| 33 | var terminals []*terminalSession |
| 34 | if a.terminals != nil { |
| 35 | terminals = a.terminals.detachForTab(tab.ID) |
| 36 | } |
| 37 | if preserveSource { |
| 38 | if !a.detachRuntimeForReplacementLocked(tab) { |
| 39 | a.mu.Unlock() |
| 40 | return nil, errSessionNavigationSuperseded |
| 41 | } |
| 42 | } else { |
| 43 | a.releaseSessionRuntimeLocked(tab) |
| 44 | } |
| 45 | a.unregisterDetachedRuntimeLocked(source) |
| 46 | if a.tabs[source.ID] == source { |
| 47 | delete(a.tabs, source.ID) |
| 48 | a.removeTabOrderLocked(source.ID) |
| 49 | } |
| 50 | if a.activeTabID == source.ID { |
| 51 | a.activeTabID = tab.ID |
| 52 | } |
| 53 | applyCanonicalWorkspaceLocked(tab, workspace, true) |
| 54 | applyRuntimeTab(tab, source, sessionRoute(ref.SessionID), a.ctx, a) |
| 55 | a.supersedeTabBuildLocked(tab) |
| 56 | a.saveTabsLocked() |
| 57 | adopted, sink := tab.Ctrl, tab.sink |
| 58 | epoch := a.advanceSessionRuntimeEpochLocked(tab) |
| 59 | a.mu.Unlock() |
| 60 | if !preserveSource { |
| 61 | fenceCanonicalNavigationSink(oldSink) |
| 62 | retireReplacedController(current, adopted) |
| 63 | if oldHost != "" { |
| 64 | a.releaseSharedHost(oldHost) |
| 65 | } |
| 66 | } |
| 67 | a.finishCanonicalWorkspaceMove(tab.ID, terminals) |
| 68 | a.replayPendingPromptsAfterRuntimeAttach(tab.ID, sink, adopted, epoch) |
| 69 | return adopted, nil |
| 70 | } |
| 71 | |
| 72 | func (a *App) finishCanonicalWorkspaceMove(tabID string, terminals []*terminalSession) { |
| 73 | if a.workspaceHub != nil { |
| 74 | a.workspaceHub.reconcileRoots() |
| 75 | } |
| 76 | if a.terminals != nil { |
| 77 | a.terminals.reopenForTab(tabID) |
| 78 | a.terminals.closeSessions(terminals) |
| 79 | } |
| 80 | } |
| 81 |