返回 DeepSeek-Reasonix
session_write_authority.go
根目录 / desktop / session_write_authority.go
1 package main
2
3 import (
4 "context"
5 "fmt"
6
7 "reasonix/internal/agent"
8 "reasonix/internal/control"
9 )
10
11 // bindTabWriteAuthority issues a generation-bound write authority from the
12 // tab's current session lease onto ctrl. Must run after the lease is adopted
13 // and before the controller is published as Ready/autosave-capable. Missing lease or a
14 // non-Controller SessionAPI is a no-op (tests and read-only tabs).
15 func bindTabWriteAuthority(tab *WorkspaceTab, ctrl control.SessionAPI) error {
16 if tab == nil || ctrl == nil {
17 return nil
18 }
19 c, ok := ctrl.(*control.Controller)
20 if !ok || c == nil {
21 return nil
22 }
23 tab.sessionLeaseMu.Lock()
24 lease := tab.sessionLease
25 tab.sessionLeaseMu.Unlock()
26 return c.BindSessionWriteAuthority(lease)
27 }
28
29 // authorizeTabReplacementLocked validates and binds a replacement before it
30 // is published. App.mu must be held by the caller.
31 func (a *App) authorizeTabReplacementLocked(tab *WorkspaceTab, ctrl control.SessionAPI, action, authority string) error {
32 if !a.ownsRuntimeTabLocked(tab) {
33 return fmt.Errorf("tab %q changed while %s; retry", tab.ID, action)
34 }
35 if err := bindTabWriteAuthority(tab, ctrl); err != nil {
36 return fmt.Errorf("bind %s session authority: %w", authority, err)
37 }
38 return nil
39 }
40
41 func bindCandidateWriteAuthority(ctrl control.SessionAPI, lease *agent.SessionLease) error {
42 concrete, ok := ctrl.(*control.Controller)
43 if !ok || concrete == nil {
44 return nil
45 }
46 return control.IssueAndBindWriteAuthority(concrete, lease)
47 }
48
49 // validateAndBindSessionRebindLocked performs the final identity check and
50 // authority bind as one publication admission. App.mu must be held.
51 func (a *App) validateAndBindSessionRebindLocked(tab *WorkspaceTab, source tabRuntimeSnapshot, transition sessionRuntimePathTransition, candidate *sessionRebindCandidate, lease *agent.SessionLease) error {
52 if tab.removed ||
53 a.tabs[tab.ID] != tab ||
54 tab.Ctrl != source.ctrl ||
55 a.runtimeForTabLocked(tab) != transition.runtime ||
56 !a.sessionRuntimePathTransitionValidLocked(transition) {
57 return fmt.Errorf("tab runtime changed while switching sessions; retry")
58 }
59 if err := bindCandidateWriteAuthority(candidate.ctrl, lease); err != nil {
60 return fmt.Errorf("resume session: bind write authority: %w", err)
61 }
62 return nil
63 }
64
65 // commitStartupWriteAuthorityLocked commits extension registration and write
66 // authority under the final publication lock. App.mu must be held on entry;
67 // failure paths release it and retire the unpublished controller.
68 func (a *App) commitStartupWriteAuthorityLocked(tab *WorkspaceTab, ctrl control.SessionAPI, registration *sharedHostMCPRegistration, rootKey, acquiredLeaseKey string, appCtx context.Context) bool {
69 if !registration.commit() {
70 a.mu.Unlock()
71 a.abandonSupersededBuild(tab, ctrl, rootKey, acquiredLeaseKey)
72 a.scheduleDeferredStartupBuild(tab.ID)
73 return false
74 }
75 if err := bindTabWriteAuthority(tab, ctrl); err != nil {
76 _, save := a.markTabStartupFailureLocked(tab, err, suppressStartupRestore)
77 a.mu.Unlock()
78 a.writeTabsSaveRequest(save)
79 a.abandonSupersededBuild(tab, ctrl, rootKey, acquiredLeaseKey)
80 a.emitReady(appCtx, tab.ID)
81 return false
82 }
83 return true
84 }
85
85 lines GO