| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "strings" |
| 6 | |
| 7 | "reasonix/internal/agent" |
| 8 | "reasonix/internal/boot" |
| 9 | "reasonix/internal/control" |
| 10 | "reasonix/internal/event" |
| 11 | "reasonix/internal/evidence" |
| 12 | goaldomain "reasonix/internal/goal" |
| 13 | "reasonix/internal/session" |
| 14 | ) |
| 15 | |
| 16 | func (a *App) metaForTab(tabID string) Meta { |
| 17 | for { |
| 18 | a.mu.RLock() |
| 19 | tab := a.tabByIDLocked(tabID) |
| 20 | snap := snapshotTabRuntimeLocked(tab) |
| 21 | runtimeView := a.sessionRuntimeViewLocked(tab) |
| 22 | var historical *SessionSourceRef |
| 23 | headID := "" |
| 24 | if tab != nil { |
| 25 | historical, headID = tab.HistoricalSource, tab.SessionHeadID |
| 26 | } |
| 27 | a.mu.RUnlock() |
| 28 | if tab == nil { |
| 29 | meta := Meta{EventChannel: eventChannel} |
| 30 | if ref, ok := a.remoteTabRefFor(tabID); ok { |
| 31 | meta.Remote = &ref |
| 32 | } |
| 33 | return meta |
| 34 | } |
| 35 | cwd := snap.workspaceRoot |
| 36 | if cwd == "" { |
| 37 | cwd, _ = os.Getwd() |
| 38 | } |
| 39 | extras, refreshExtras := tabMetaExtrasFor(tab, cwd, snap.model) |
| 40 | if capability, ok := snap.ctrl.(interface{ ImageInputSnapshot() (bool, bool, bool) }); ok { |
| 41 | if enabled, fallback, available := capability.ImageInputSnapshot(); available { |
| 42 | extras.imageInputEnabled, extras.visionFallbackEnabled = enabled, fallback |
| 43 | } |
| 44 | } |
| 45 | if refreshExtras { |
| 46 | a.scheduleTabMetaExtrasRefresh(tab.ID) |
| 47 | } |
| 48 | autoApproveTools := snap.ctrl != nil && snap.ctrl.AutoApproveTools() |
| 49 | goal := snap.currentGoal() |
| 50 | goalStatus := snap.currentGoalStatus() |
| 51 | var runtimeStateSnapshot *event.RuntimeStateSnapshot |
| 52 | var goalView *goaldomain.View |
| 53 | var canonicalTodos *[]evidence.TodoItem |
| 54 | if snap.ctrl != nil { |
| 55 | state := controllerRuntimeState(snap.ctrl) |
| 56 | if state.SchemaVersion == 1 { |
| 57 | runtimeStateSnapshot = &state |
| 58 | goalView = state.Goal |
| 59 | canonicalTodos = runtimeSnapshotTodos(state) |
| 60 | } else { |
| 61 | canonicalTodos = ctrlTodos(snap.ctrl) |
| 62 | } |
| 63 | } |
| 64 | sessionPath := strings.TrimSpace(snap.sessionPath) |
| 65 | // Read off the lock, like every other controller read in this function. |
| 66 | if native, ok := snap.ctrl.(*control.Controller); ok && historical == nil { |
| 67 | path := sessionPath |
| 68 | if path == "" { |
| 69 | path = native.SessionPath() |
| 70 | } |
| 71 | historical = nativeHistoricalSource(native, path, headID) |
| 72 | } |
| 73 | sessionID := strings.TrimSpace(snap.sessionID) |
| 74 | var sessionRef *session.SessionRef |
| 75 | if sessionID != "" { |
| 76 | ref := session.SessionRef{HostID: localDesktopHostID, SessionID: sessionID} |
| 77 | sessionRef = &ref |
| 78 | } |
| 79 | var sessionRevision int64 |
| 80 | var sessionDigest string |
| 81 | if branchMeta, ok, err := agent.LoadBranchMeta(sessionPath); err == nil && ok { |
| 82 | sessionRevision = branchMeta.Revision |
| 83 | sessionDigest = branchMeta.ContentDigest |
| 84 | } |
| 85 | meta := Meta{ |
| 86 | Label: snap.label, Ready: runtimeView.Phase == sessionRuntimeReady && snap.ctrl != nil, |
| 87 | Runtime: runtimeView, StartupErr: snap.startupErr, EventChannel: eventChannel, |
| 88 | HistoricalSource: historical, |
| 89 | SessionPath: sessionPath, SessionID: sessionID, Session: sessionRef, |
| 90 | SessionRevision: sessionRevision, SessionDigest: sessionDigest, |
| 91 | SessionGeneration: snap.sessionGeneration, RuntimeStateSnapshot: runtimeStateSnapshot, |
| 92 | Cwd: cwd, WorkspaceRoot: cwd, WorkspaceName: tabWorkspaceNameForScope(snap.scope, cwd), WorkspacePath: cwd, |
| 93 | GitBranch: extras.gitBranch, ImageInputEnabled: extras.imageInputEnabled, |
| 94 | VisionFallbackEnabled: extras.visionFallbackEnabled, AutoApproveTools: autoApproveTools, |
| 95 | Bypass: autoApproveTools, CollaborationMode: snap.collaborationMode(), |
| 96 | TokenMode: boot.TokenModeFull, AgentPreset: boot.AgentPresetBalanced, |
| 97 | ToolApprovalMode: snap.currentToolApprovalMode(), Goal: goal, GoalStatus: goalStatus, |
| 98 | GoalView: goalView, GoalRuntime: goalRuntimeViewFromController(snap.ctrl), CanonicalTodos: canonicalTodos, |
| 99 | PinnedFiles: buildPinnedContext(snap.workspaceRoot, tab.GetPinnedFiles()).Infos, |
| 100 | } |
| 101 | a.mu.RLock() |
| 102 | currentTab := a.tabByIDLocked(tabID) |
| 103 | current := snapshotTabRuntimeLocked(currentTab) |
| 104 | valid := currentTab == tab && sameSessionAPI(current.ctrl, snap.ctrl) && |
| 105 | current.sessionID == snap.sessionID && current.sessionPath == snap.sessionPath && |
| 106 | current.sessionGeneration == snap.sessionGeneration |
| 107 | a.mu.RUnlock() |
| 108 | if valid { |
| 109 | return meta |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func runtimeSnapshotTodos(state event.RuntimeStateSnapshot) *[]evidence.TodoItem { |
| 115 | todos := make([]evidence.TodoItem, len(state.Todos)) |
| 116 | for i, todo := range state.Todos { |
| 117 | todos[i] = evidence.TodoItem{Content: todo.Content, Status: todo.Status} |
| 118 | } |
| 119 | return &todos |
| 120 | } |
| 121 |