| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "reasonix/internal/control" |
| 7 | ) |
| 8 | |
| 9 | // A receipt belongs to a durable session, not the UI tab which originally |
| 10 | // submitted it. Rebinding is read-only; enqueue still uses the original fence. |
| 11 | func (a *App) remoteReceiptTarget(original InboxTargetView) (InboxTargetView, error) { |
| 12 | if original.HostID == "" || original.Workspace == "" { |
| 13 | return original, nil // old callers cannot safely rebind without identity |
| 14 | } |
| 15 | a.remoteTabMu.Lock() |
| 16 | defer a.remoteTabMu.Unlock() |
| 17 | var chosen *remoteTab |
| 18 | for _, tab := range a.remoteTabs { |
| 19 | if tab == nil || tab.ref.HostID != original.HostID || tab.ref.Workspace != original.Workspace || |
| 20 | tab.routing.currentPath != original.SessionPath || tab.state != "ready" || |
| 21 | tab.client == nil || tab.routing.rehydratingPath != "" { |
| 22 | continue |
| 23 | } |
| 24 | if chosen == nil || tab.id < chosen.id || tab.id == original.TabID { |
| 25 | chosen = tab |
| 26 | } |
| 27 | if tab.id == original.TabID { |
| 28 | break |
| 29 | } |
| 30 | } |
| 31 | if chosen == nil { |
| 32 | return InboxTargetView{}, fmt.Errorf("inbox receipt session unavailable") |
| 33 | } |
| 34 | return InboxTargetView{TabID: chosen.id, SessionPath: original.SessionPath, Generation: chosen.gen, |
| 35 | Selection: chosen.selectionRevision, Remote: true, HostID: chosen.ref.HostID, Workspace: chosen.ref.Workspace}, nil |
| 36 | } |
| 37 | |
| 38 | type localReceiptOwner struct { |
| 39 | tab *WorkspaceTab |
| 40 | ctrl control.SessionAPI |
| 41 | path string |
| 42 | generation uint64 |
| 43 | } |
| 44 | |
| 45 | func (a *App) localReceiptTarget(path string) (localReceiptOwner, error) { |
| 46 | key := sessionRuntimeKey(path) |
| 47 | if key == "" { |
| 48 | return localReceiptOwner{}, fmt.Errorf("inbox receipt session unavailable") |
| 49 | } |
| 50 | a.mu.RLock() |
| 51 | defer a.mu.RUnlock() |
| 52 | var owner localReceiptOwner |
| 53 | for _, tab := range a.runtimeTabsLocked() { |
| 54 | if tab.Ctrl == nil || tab.ReadOnly || sessionRuntimeKey(tab.SessionPath) != key { |
| 55 | continue |
| 56 | } |
| 57 | if owner.ctrl != nil && owner.ctrl != tab.Ctrl { |
| 58 | return localReceiptOwner{}, fmt.Errorf("inbox receipt owner is ambiguous") |
| 59 | } |
| 60 | owner = localReceiptOwner{tab: tab, ctrl: tab.Ctrl, path: tab.SessionPath, generation: tab.SessionGeneration} |
| 61 | } |
| 62 | if owner.ctrl == nil { |
| 63 | return owner, fmt.Errorf("inbox receipt session unavailable") |
| 64 | } |
| 65 | return owner, nil |
| 66 | } |
| 67 | |
| 68 | func (a *App) localReceiptOwnerCurrent(owner localReceiptOwner) bool { |
| 69 | if sessionRuntimeKey(owner.ctrl.SessionPath()) != sessionRuntimeKey(owner.path) { |
| 70 | return false |
| 71 | } |
| 72 | a.mu.RLock() |
| 73 | defer a.mu.RUnlock() |
| 74 | for _, tab := range a.runtimeTabsLocked() { |
| 75 | if tab == owner.tab && tab.Ctrl == owner.ctrl && !tab.ReadOnly && tab.SessionGeneration == owner.generation && |
| 76 | sessionRuntimeKey(tab.SessionPath) == sessionRuntimeKey(owner.path) { |
| 77 | return true |
| 78 | } |
| 79 | } |
| 80 | return false |
| 81 | } |
| 82 |