Skip to content

Commit b53b02e

Browse files
committed
fix(cleanup): skip chat attachments whose key is not a string
`collectChatFiles` accepted any truthy `fileAttachments[].key` and then called string methods on it, so one message row carrying a numeric or object key threw and failed the whole chat purge, leaving every other key of the batch behind. It now uses the same guard as the reference check in `chat-cleanup.ts` (`isRecordLike` plus a non-empty string key) and skips the entry. Autopsy: the purge tests only fed well-formed attachments; message content is untyped jsonb written by several clients, and the cast to string hid the gap from type-check. The new case fails on the old code with `key.startsWith is not a function`.
1 parent 099fad8 commit b53b02e

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

‎apps/sim/lib/cleanup/chat-cleanup.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ describe('chat purge storage', () => {
8080
expect(deleted).toEqual({ mothership: [own] })
8181
})
8282

83+
it('skips an attachment whose key is not a string instead of failing the purge', async () => {
84+
const deleted = await purge([
85+
{
86+
role: 'user',
87+
content: 'look',
88+
fileAttachments: [{ ...attachment('x'), key: 42 }, attachment('copilot/1234/kept.png')],
89+
},
90+
])
91+
expect(deleted).toEqual({ copilot: ['copilot/1234/kept.png'] })
92+
})
93+
8394
it('deletes the inline images an assistant message published', async () => {
8495
const deleted = await purge([
8596
{

‎apps/sim/lib/cleanup/chat-files.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,8 @@ export async function collectChatFiles(
150150
const attachments = (msg as Record<string, unknown>).fileAttachments
151151
if (!Array.isArray(attachments)) continue
152152
for (const attachment of attachments) {
153-
if (
154-
attachment &&
155-
typeof attachment === 'object' &&
156-
(attachment as Record<string, unknown>).key
157-
) {
158-
const key = (attachment as Record<string, unknown>).key as string
153+
if (isRecordLike(attachment) && typeof attachment.key === 'string' && attachment.key) {
154+
const key = attachment.key
159155
if (seen.has(key)) continue
160156
const organizationId = organizationAttachmentOwner(key)
161157
if (organizationId) {

0 commit comments

Comments
 (0)