( ctx context.Context, )
| 784 | } |
| 785 | |
| 786 | func (c *turnWorkspaceContext) loadWorkspaceAgentLocked( |
| 787 | ctx context.Context, |
| 788 | ) (database.Chat, database.WorkspaceAgent, error) { |
| 789 | chatSnapshot := c.currentChatSnapshot() |
| 790 | |
| 791 | for attempt := 0; attempt < 2; attempt++ { |
| 792 | if !chatSnapshot.WorkspaceID.Valid { |
| 793 | refreshedChat, refreshErr := refreshChatWorkspaceSnapshot( |
| 794 | ctx, |
| 795 | chatSnapshot, |
| 796 | c.loadChatSnapshot, |
| 797 | ) |
| 798 | if refreshErr != nil { |
| 799 | return chatSnapshot, database.WorkspaceAgent{}, refreshErr |
| 800 | } |
| 801 | if refreshedChat.WorkspaceID.Valid { |
| 802 | c.setCurrentChat(refreshedChat) |
| 803 | chatSnapshot = refreshedChat |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | if !chatSnapshot.WorkspaceID.Valid { |
| 808 | return chatSnapshot, database.WorkspaceAgent{}, xerrors.New("no workspace is associated with this chat. Use the create_workspace tool to create one") |
| 809 | } |
| 810 | |
| 811 | if chatSnapshot.AgentID.Valid { |
| 812 | agent, err := c.server.db.GetWorkspaceAgentByID(ctx, chatSnapshot.AgentID.UUID) |
| 813 | if err == nil { |
| 814 | latestChat, workspaceMatches := c.currentWorkspaceMatches(chatSnapshot.WorkspaceID) |
| 815 | if !workspaceMatches { |
| 816 | chatSnapshot = latestChat |
| 817 | continue |
| 818 | } |
| 819 | c.agent = agent |
| 820 | c.agentLoaded = true |
| 821 | c.cachedWorkspaceID = chatSnapshot.WorkspaceID |
| 822 | return chatSnapshot, c.agent, nil |
| 823 | } |
| 824 | if !xerrors.Is(err, sql.ErrNoRows) { |
| 825 | c.server.logger.Warn(ctx, "agent binding lookup failed, re-resolving", |
| 826 | slog.F("agent_id", chatSnapshot.AgentID.UUID), |
| 827 | slog.Error(err), |
| 828 | ) |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | agents, err := c.server.db.GetWorkspaceAgentsInLatestBuildByWorkspaceID( |
| 833 | ctx, |
| 834 | chatSnapshot.WorkspaceID.UUID, |
| 835 | ) |
| 836 | if err != nil { |
| 837 | return chatSnapshot, database.WorkspaceAgent{}, xerrors.Errorf( |
| 838 | "get workspace agents in latest build: %w", |
| 839 | err, |
| 840 | ) |
| 841 | } |
| 842 | if len(agents) == 0 { |
| 843 | return chatSnapshot, database.WorkspaceAgent{}, errChatHasNoWorkspaceAgent |
no test coverage detected