Merge pull request #39 from SLEEPYBQ/codex/macos-project-approval-focus
fix(macos): refocus pending project approval on explicit open
- 作者
- Shihao Li
- 时间
- 2026/11/11 16:11
- 提交
- 1a7ff2f
这是一个合并提交,下面是和第一个上一提交相比的改动。
fix(macos): refocus pending project approval on explicit open
这是一个合并提交,下面是和第一个上一提交相比的改动。
| @@ -6,10 +6,12 @@ import ( | ||
| 6 | 6 | "crypto/rand" |
| 7 | 7 | "encoding/json" |
| 8 | 8 | "encoding/pem" |
| 9 | 新增: "errors" | |
| 9 | 10 | "fmt" |
| 10 | 11 | "io" |
| 11 | 12 | "net" |
| 12 | 13 | "net/http" |
| 14 | 新增: "net/http/httptest" | |
| 13 | 15 | "os" |
| 14 | 16 | "path" |
| 15 | 17 | "path/filepath" |
| @@ -180,6 +182,8 @@ func (g *macWorkspaceGuest) execute(channel ssh.Channel, command string) uint32 | ||
| 180 | 182 | json.NewEncoder(channel).Encode(macOSResourceSample{Version: 1, Total: 4 << 30, Available: 2 << 30, |
| 181 | 183 | Free: 1 << 30, Inactive: 1 << 30, CPUs: 2, Boot: 10, CPUTicks: [4]uint32{ticks, 10, 80 + ticks, 0}}) |
| 182 | 184 | return 0 |
| 185 | 新增: case command == "/usr/bin/open -a /Applications/CageProjectAccess.app": | |
| 186 | 新增: return 0 | |
| 183 | 187 | case strings.HasPrefix(command, "set -e;"): |
| 184 | 188 | return 0 |
| 185 | 189 | case strings.HasPrefix(command, "exec /usr/local/libexec/cage-sshfs "): |
| @@ -442,3 +446,57 @@ func TestMacOSDefaultWorkspaceNewAndLegacyConversations(t *testing.T) { | ||
| 442 | 446 | } |
| 443 | 447 | } |
| 444 | 448 | } |
| 449 | 新增: | |
| 450 | 新增:func TestMacOSProjectOpenRestoresPendingApprovalWindow(t *testing.T) { | |
| 451 | 新增: var activations atomic.Int32 | |
| 452 | 新增: var activationFails atomic.Bool | |
| 453 | 新增: guest := newMacWorkspaceGuest(t, func(channel ssh.Channel, command string) (uint32, bool) { | |
| 454 | 新增: if strings.Contains(command, "/usr/bin/open -a /Applications/CageProjectAccess.app") { | |
| 455 | 新增: activations.Add(1) | |
| 456 | 新增: if activationFails.Load() { | |
| 457 | 新增: return 1, true | |
| 458 | 新增: } | |
| 459 | 新增: return 0, true | |
| 460 | 新增: } | |
| 461 | 新增: return 0, false | |
| 462 | 新增: }) | |
| 463 | 新增: s := guest.s | |
| 464 | 新增: s.restoreProjectShares() | |
| 465 | 新增: share := s.projectShares[guest.hostPath] | |
| 466 | 新增: if share == nil || share.approval == "" { | |
| 467 | 新增: t.Fatal("expected a pending project authorization") | |
| 468 | 新增: } | |
| 469 | 新增: grant := share.approval | |
| 470 | 新增: if err := s.ensureProjectShare(guest.hostPath); !errors.Is(err, errMacOSProjectApproval) { | |
| 471 | 新增: t.Fatalf("background project probe: %v", err) | |
| 472 | 新增: } | |
| 473 | 新增: if activations.Load() != 0 { | |
| 474 | 新增: t.Fatal("background project recovery stole guest focus") | |
| 475 | 新增: } | |
| 476 | 新增: body, _ := json.Marshal(map[string]any{"workspace": guest.hostPath, "add": false}) | |
| 477 | 新增: open := func() *httptest.ResponseRecorder { | |
| 478 | 新增: return windowsBrowserRequestForTest(s, http.MethodPost, "/api/projects/open", body) | |
| 479 | 新增: } | |
| 480 | 新增: response := open() | |
| 481 | 新增: var pending struct{ Workspace, Approval string } | |
| 482 | 新增: if response.Code != http.StatusOK || json.Unmarshal(response.Body.Bytes(), &pending) != nil || pending.Approval != grant || pending.Workspace != guest.hostPath { | |
| 483 | 新增: t.Fatalf("pending project open: %d %s", response.Code, response.Body.String()) | |
| 484 | 新增: } | |
| 485 | 新增: if activations.Load() != 1 || share.detach != nil { | |
| 486 | 新增: t.Fatal("explicit project open did not show the pending window without approving it") | |
| 487 | 新增: } | |
| 488 | 新增: activationFails.Store(true) | |
| 489 | 新增: response = open() | |
| 490 | 新增: if response.Code == http.StatusOK || share.approval != grant || s.projectShares[guest.hostPath] != share || share.detach != nil { | |
| 491 | 新增: t.Fatal("failed window activation lost its pending grant or reported success") | |
| 492 | 新增: } | |
| 493 | 新增: activationFails.Store(false) | |
| 494 | 新增: guest.approved.Store(true) | |
| 495 | 新增: response = open() | |
| 496 | 新增: if response.Code != http.StatusOK || strings.Contains(response.Body.String(), "approval") || share.detach == nil { | |
| 497 | 新增: t.Fatalf("approved project open: %d %s", response.Code, response.Body.String()) | |
| 498 | 新增: } | |
| 499 | 新增: if activations.Load() != 2 { | |
| 500 | 新增: t.Fatal("opening an approved project stole guest focus") | |
| 501 | 新增: } | |
| 502 | 新增:} | |
| @@ -85,6 +85,16 @@ func (s *Server) openProject(w http.ResponseWriter, r *http.Request) { | ||
| 85 | 85 | } |
| 86 | 86 | s.mu.Unlock() |
| 87 | 87 | if grant != "" { |
| 88 | 新增: // Only an explicit project open should restore the approval app; | |
| 89 | 新增: // background probes and frame polling must not take guest focus. | |
| 90 | 新增: ctx, cancel := context.WithTimeout(s.operationContext(), 5*time.Second) | |
| 91 | 新增: _, openErr := s.guestControl(ctx, "/usr/bin/open -a /Applications/CageProjectAccess.app", "macos-project-approval") | |
| 92 | 新增: cancel() | |
| 93 | 新增: if openErr != nil { | |
| 94 | 新增: w.Header().Set("X-Cage-Recovery", "project-approval") | |
| 95 | 新增: requestError(w, errors.New("未能显示客机项目授权窗口,请重试")) | |
| 96 | 新增: return | |
| 97 | 新增: } | |
| 88 | 98 | jsonReply(w, map[string]string{"workspace": path, "approval": grant}) |
| 89 | 99 | return |
| 90 | 100 | } |