( ctx context.Context, )
| 910 | } |
| 911 | |
| 912 | func (c *turnWorkspaceContext) workspaceAgentIDForConn( |
| 913 | ctx context.Context, |
| 914 | ) (database.Chat, uuid.UUID, error) { |
| 915 | for attempt := 0; attempt < 2; attempt++ { |
| 916 | chatSnapshot := c.currentChatSnapshot() |
| 917 | if !chatSnapshot.WorkspaceID.Valid || !chatSnapshot.AgentID.Valid { |
| 918 | updatedChat, agent, err := c.ensureWorkspaceAgent(ctx) |
| 919 | if err != nil { |
| 920 | return updatedChat, uuid.Nil, err |
| 921 | } |
| 922 | return updatedChat, agent.ID, nil |
| 923 | } |
| 924 | |
| 925 | currentAgentID, err := c.latestWorkspaceAgentID( |
| 926 | ctx, |
| 927 | chatSnapshot.WorkspaceID.UUID, |
| 928 | ) |
| 929 | if err != nil { |
| 930 | if xerrors.Is(err, errChatHasNoWorkspaceAgent) { |
| 931 | c.clearCachedWorkspaceState() |
| 932 | } |
| 933 | return chatSnapshot, uuid.Nil, err |
| 934 | } |
| 935 | |
| 936 | latestChat, workspaceMatches := c.currentWorkspaceMatches( |
| 937 | chatSnapshot.WorkspaceID, |
| 938 | ) |
| 939 | if !workspaceMatches { |
| 940 | continue |
| 941 | } |
| 942 | return latestChat, currentAgentID, nil |
| 943 | } |
| 944 | |
| 945 | chatSnapshot := c.currentChatSnapshot() |
| 946 | return chatSnapshot, uuid.Nil, xerrors.New( |
| 947 | "chat workspace changed while resolving agent", |
| 948 | ) |
| 949 | } |
| 950 | |
| 951 | // getWorkspaceConnLocked returns the cached connection when it still matches |
| 952 | // the current workspace. When the workspace changed, it clears the stale |
no test coverage detected