| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "sync" |
| 7 | ) |
| 8 | |
| 9 | type navigationIntentFence struct { |
| 10 | mu sync.Mutex |
| 11 | token string |
| 12 | // beforeCloseFinalHook is test-only. It pauses after the off-lock snapshot |
| 13 | // and before the navigation-fenced removal phase. |
| 14 | beforeCloseFinalHook func() |
| 15 | } |
| 16 | |
| 17 | // RegisterNavigationIntent publishes the latest frontend navigation intent. |
| 18 | // CloseMergedWorktreeTab checks the exact token at its removal linearization |
| 19 | // point, so an older async merge lifecycle cannot close a newly selected tab. |
| 20 | func (a *App) RegisterNavigationIntent(token string) error { |
| 21 | token = strings.TrimSpace(token) |
| 22 | if token == "" || len(token) > 160 { |
| 23 | return fmt.Errorf("navigation intent token is invalid") |
| 24 | } |
| 25 | for _, char := range token { |
| 26 | if char < 0x21 || char > 0x7e { |
| 27 | return fmt.Errorf("navigation intent token is invalid") |
| 28 | } |
| 29 | } |
| 30 | a.navigationIntent.mu.Lock() |
| 31 | a.navigationIntent.token = token |
| 32 | a.navigationIntent.mu.Unlock() |
| 33 | return nil |
| 34 | } |
| 35 | |
| 36 | func (a *App) requireNavigationIntent(token string) error { |
| 37 | token = strings.TrimSpace(token) |
| 38 | if token == "" { |
| 39 | return fmt.Errorf("navigation intent token is required; resources were preserved") |
| 40 | } |
| 41 | a.navigationIntent.mu.Lock() |
| 42 | current := a.navigationIntent.token |
| 43 | a.navigationIntent.mu.Unlock() |
| 44 | if current != token { |
| 45 | return fmt.Errorf("navigation changed before worktree close; resources were preserved") |
| 46 | } |
| 47 | return nil |
| 48 | } |
| 49 |