Closes CODAGT-933 Long agent sessions were hitting the 50 attachment cap, after which every upload, screenshot and `attach_file` call was rejected, so people were abandoning otherwise healthy chats. Rather than raise the cap (which just moves the cliff and multiplies the worst-case per-chat `bytea` storage), a chat now keeps its 50 most recent attachments. `LinkChatFilesAfterLock` deletes the oldest `chat_files` rows in the same statement and the cascade drops their links. Files in the incoming batch are never evicted, so the only remaining rejection is a single message with more than 50 files. Nothing in the product ever linked one file to two chats, so rather than guard the eviction against it, this adds `UNIQUE (file_id)` on `chat_file_links`, with the migration dropping any existing duplicates in favour of the oldest chat. Evicted attachments render as the existing "Attachment expired" tile. A `pinned` flag is the obvious follow-up if someone needs to keep a specific file around.
50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package chatstate_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/lib/pq"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uber.org/mock/gomock"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbmock"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatstate"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
func TestLinkFilesUnavailable(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for name, dbErr := range map[string]*pq.Error{
|
|
"missing": {
|
|
Code: pq.ErrorCode("23503"),
|
|
Constraint: string(database.ForeignKeyChatFileLinksFileID),
|
|
},
|
|
"linked to another chat": {
|
|
Code: pq.ErrorCode("23505"),
|
|
Constraint: string(database.UniqueChatFileLinksFileIDKey),
|
|
},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctrl := gomock.NewController(t)
|
|
store := dbmock.NewMockStore(ctrl)
|
|
chatID := uuid.New()
|
|
fileID := uuid.New()
|
|
store.EXPECT().LinkChatFiles(gomock.Any(), database.LinkChatFilesParams{
|
|
ChatID: chatID,
|
|
MaxFileLinks: int32(codersdk.MaxChatFileIDs),
|
|
FileIds: []uuid.UUID{fileID},
|
|
}).Return(int32(0), dbErr)
|
|
|
|
err := chatstate.LinkFiles(context.Background(), store, chatID, []uuid.UUID{fileID})
|
|
require.ErrorIs(t, err, chatstate.ErrChatFileUnavailable)
|
|
require.ErrorIs(t, err, dbErr)
|
|
})
|
|
}
|
|
}
|