fix(web): remove composer attachments without waiting for cleanup (#2128)

* fix(web): remove composer attachments without waiting for cleanup

* test(web): run attachment removal regression in CI
This commit is contained in:
Ananovo
2026-10-25 15:33:37 +08:00
committed by GitHub
parent 33a9a740e8
commit 097a693348
8 changed files with 326 additions and 4 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ jobs:
# Required by cli/src/utils/windowsProcessTree.pwsh.test.ts — image
# inspect alone does not pull; silent skip was fake B1 coverage (#1911).
- run: docker pull mcr.microsoft.com/powershell:latest
- run: bun run test:e2e -- terminal-wrap-fidelity.spec.ts composer-copy.spec.ts session-list-scroll.spec.ts cold-initial-tail.spec.ts scrollbar-auto-hide.spec.ts
- run: bun run test:e2e -- terminal-wrap-fidelity.spec.ts composer-copy.spec.ts session-list-scroll.spec.ts cold-initial-tail.spec.ts scrollbar-auto-hide.spec.ts attachment-removal.spec.ts
- run: bun run test
# Serial runner-integration suite: starts real detached runner/session
+153
View File
@@ -0,0 +1,153 @@
import { expect, test, type Page } from '@playwright/test'
test.use({ serviceWorkers: 'block' })
const png = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII=', 'base64')
function gate() {
let release!: () => void
const promise = new Promise<void>((resolve) => { release = resolve })
return { promise, release }
}
const chip = (page: Page, name: string) => page.locator(`[data-name="${name}"]`)
async function add(page: Page, name: string) {
await page.getByLabel('Add image').setInputFiles({ name, mimeType: 'image/png', buffer: png })
}
async function ready(page: Page, name: string) {
await expect.poll(async () => {
const attachments: { name: string, status: string }[] = JSON.parse((await page.getByTestId('state').textContent())!)
return attachments.find((attachment) => attachment.name === name)?.status
}).toBe('requires-action')
}
async function remove(page: Page, name: string) {
await chip(page, name).hover()
await chip(page, name).getByRole('button', { name: 'Remove attachment' }).click()
}
for (const mode of ['chat', 'scratchlist'] as const) {
test.describe(mode, () => {
let releaseUpload: ReturnType<typeof gate>
let releaseDelete: ReturnType<typeof gate>
let holdUpload: string | undefined
let failDelete: boolean
let deleted: string[]
let uploads: string[]
let errors: string[]
test.beforeEach(async ({ page }) => {
releaseUpload = gate()
releaseDelete = gate()
holdUpload = undefined
failDelete = false
deleted = []
uploads = []
errors = []
page.on('pageerror', (error) => errors.push(error.message))
await page.route((url) => url.pathname.startsWith('/api/'), async (route) => {
const request = route.request()
const url = new URL(request.url())
if (url.pathname.endsWith('/upload')) {
const { filename } = request.postDataJSON()
uploads.push(filename)
if (filename === holdUpload) await releaseUpload.promise
const path = `/synthetic/${filename}`
await route.fulfill({ json: mode === 'chat'
? { success: true, path }
: { success: true, attachment: { id: filename, filename, path, mimeType: 'image/png', size: png.length } },
})
} else if (url.pathname.endsWith('/upload/delete') || request.method() === 'DELETE') {
deleted.push(mode === 'chat' ? request.postDataJSON().path : decodeURIComponent(url.pathname.split('/').at(-1)!))
await releaseDelete.promise
if (failDelete) await route.abort('failed')
else await route.fulfill({ json: { success: true, ok: true } })
} else {
throw new Error(`Unexpected fixture request: ${request.method()} ${url.pathname}`)
}
})
await page.goto(`/e2e-fixtures/upload-delete-fixture.html?mode=${mode}`, { waitUntil: 'domcontentloaded' })
})
test.afterEach(async ({ page }) => {
releaseUpload.release()
releaseDelete.release()
await page.unrouteAll({ behavior: 'wait' })
expect(errors).toEqual([])
})
test('removes A before cleanup completes while B continues uploading', async ({ page }) => {
await add(page, 'A.png')
await ready(page, 'A.png')
holdUpload = 'B.png'
await add(page, 'B.png')
await expect(page.getByTestId('state')).toContainText('running')
await expect.poll(() => uploads).toContain('B.png')
await remove(page, 'A.png')
await expect(chip(page, 'A.png')).toHaveCount(0)
await expect(page.getByTestId('state')).toContainText('running')
await expect.poll(() => deleted).toHaveLength(1)
releaseUpload.release()
await ready(page, 'B.png')
await expect(chip(page, 'A.png')).toHaveCount(0)
releaseDelete.release()
await expect(chip(page, 'B.png')).toBeVisible()
})
test('does not send a removed image while its cleanup is pending', async ({ page }) => {
await add(page, 'A.png')
await ready(page, 'A.png')
await add(page, 'B.png')
await ready(page, 'B.png')
await remove(page, 'A.png')
await expect(chip(page, 'A.png')).toHaveCount(0)
await page.getByRole('button', { name: 'Send', exact: true }).click()
await expect(page.getByTestId('sent')).toContainText('B.png')
const message = JSON.parse((await page.getByTestId('sent').textContent())!)
expect(message.attachments).toHaveLength(1)
expect(message.attachments[0].name).toBe('B.png')
expect(JSON.stringify(message)).not.toContain('A.png')
await expect.poll(() => deleted).toHaveLength(1)
})
test('does not restore a removed image when cleanup fails', async ({ page }) => {
await add(page, 'A.png')
await ready(page, 'A.png')
await remove(page, 'A.png')
await expect(chip(page, 'A.png')).toHaveCount(0)
failDelete = true
const failure = page.waitForEvent('requestfailed', (request) => new URL(request.url()).pathname.startsWith('/api/'))
releaseDelete.release()
await failure
await add(page, 'B.png')
await ready(page, 'B.png')
await expect(chip(page, 'A.png')).toHaveCount(0)
await expect(chip(page, 'B.png')).toBeVisible()
})
test('cleans up a removed in-flight upload without bringing its chip back', async ({ page }) => {
holdUpload = 'A.png'
await add(page, 'A.png')
await expect(page.getByTestId('state')).toContainText('running')
await expect.poll(() => uploads).toContain('A.png')
await remove(page, 'A.png')
await expect(chip(page, 'A.png')).toHaveCount(0)
expect(deleted).toHaveLength(0)
releaseUpload.release()
await expect.poll(() => deleted).toEqual([mode === 'chat' ? '/synthetic/A.png' : 'A.png'])
releaseDelete.release()
await add(page, 'B.png')
await ready(page, 'B.png')
await expect(chip(page, 'A.png')).toHaveCount(0)
})
test('removes multiple uploaded images without serializing their cleanup', async ({ page }) => {
await add(page, 'A.png')
await ready(page, 'A.png')
await add(page, 'B.png')
await ready(page, 'B.png')
await remove(page, 'A.png')
await expect(chip(page, 'A.png')).toHaveCount(0)
await remove(page, 'B.png')
await expect(page.getByTestId('state')).toHaveText('[]')
await expect.poll(() => deleted).toHaveLength(2)
})
})
}
+20
View File
@@ -0,0 +1,20 @@
import { defineConfig, devices } from '@playwright/test'
import base from './playwright.config'
export default defineConfig({
...base,
testMatch: '**/attachment-removal.spec.ts',
retries: 0,
timeout: 60_000,
reporter: 'list',
outputDir: process.env.HAPI_E2E_ARTIFACT_DIR ?? 'test-results/attachments',
use: { ...base.use, serviceWorkers: 'block', video: process.env.HAPI_RECORD_E2E ? 'on' : 'retain-on-failure' },
projects: [
{ name: 'chromium', use: { ...devices['Desktop Chrome'] } },
{ name: 'firefox', use: { ...devices['Desktop Firefox'] } },
{ name: 'webkit', use: { ...devices['Desktop Safari'] } },
{ name: 'chromium-mobile', use: { ...devices['Pixel 5'] } },
{ name: 'webkit-mobile', use: { ...devices['iPhone 13'] } },
...(process.env.HAPI_TEST_EDGE ? [{ name: 'edge', use: { channel: 'msedge' as const } }] : []),
],
})
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>HAPI attachment removal fixture</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="./upload-delete-fixture.tsx"></script>
</body>
</html>
@@ -0,0 +1,52 @@
import React, { useCallback, useMemo, useState } from 'react'
import { createRoot } from 'react-dom/client'
import { AssistantRuntimeProvider, ComposerPrimitive, useExternalStoreRuntime, useAuiState } from '@assistant-ui/react'
import { ApiClient } from '../src/api/client'
import { createAttachmentAdapter } from '../src/lib/attachmentAdapter'
import { createScratchlistAttachmentAdapter } from '../src/lib/scratchlistAttachmentAdapter'
import type { AppendMessage, ThreadMessage } from '@assistant-ui/react'
import { AttachmentItem } from '../src/components/AssistantChat/AttachmentItem'
import { I18nProvider } from '../src/lib/i18n-context'
import '../src/index.css'
function Contents() {
const attachments = useAuiState((s) => s.composer.attachments)
return <ComposerPrimitive.Root>
<div style={{ display: 'flex', gap: 16, marginBlock: 20 }}>
{attachments.map((attachment, index) => <div key={attachment.id} data-hapi-composer-attachment-id={attachment.id} data-name={attachment.name}>
<ComposerPrimitive.AttachmentByIndex index={index} components={{ Attachment: AttachmentItem }} />
</div>)}
</div>
<pre data-testid="state" style={{ whiteSpace: 'pre-wrap' }}>
{JSON.stringify(attachments.map((a) => ({ name: a.name, status: a.status.type })))}
</pre>
<ComposerPrimitive.Input placeholder="Message" />
<ComposerPrimitive.Send aria-label="Send">Send</ComposerPrimitive.Send>
</ComposerPrimitive.Root>
}
function Fixture() {
const [sent, setSent] = useState<AppendMessage | null>(null)
const adapter = useMemo(() => {
const api = new ApiClient('')
return new URLSearchParams(location.search).get('mode') === 'scratchlist'
? createScratchlistAttachmentAdapter(api, 'fixture')
: createAttachmentAdapter(api, 'fixture')
}, [])
const onNew = useCallback(async (message: AppendMessage) => { setSent(message) }, [])
const runtime = useExternalStoreRuntime<ThreadMessage>({ messages: [], onNew, adapters: { attachments: adapter } })
return <AssistantRuntimeProvider runtime={runtime}>
<main style={{ padding: 32, fontFamily: 'system-ui' }}>
<h1>Issue #2118: attachment removal</h1>
<p>Real HAPI attachment UI, adapter and composer runtime. Synthetic images; controlled HTTP responses.</p>
<label>Add image <input type="file" accept="image/*" onChange={(event) => {
const file = event.target.files?.[0]
if (file) void runtime.thread.composer.addAttachment(file)
event.target.value = ''
}} /></label>
<Contents />
<pre data-testid="sent" style={{ whiteSpace: 'pre-wrap' }}>{JSON.stringify(sent)}</pre>
</main>
</AssistantRuntimeProvider>
}
createRoot(document.getElementById('root')!).render(<I18nProvider><Fixture /></I18nProvider>)
+3 -1
View File
@@ -187,7 +187,9 @@ export function createAttachmentAdapter(
cancelledAttachmentIds.add(attachment.id)
const path = (attachment as PendingUploadAttachment).path
const uploadSessionId = (attachment as PendingUploadAttachment).uploadSessionId
await deleteUpload(path, uploadSessionId)
// The composer waits for remove() before dropping the chip. Remote
// cleanup is best effort and must not delay that local update.
void deleteUpload(path, uploadSessionId)
},
async send(attachment: PendingAttachment): Promise<CompleteAttachment> {
+82
View File
@@ -0,0 +1,82 @@
import { describe, expect, it, vi } from 'vitest'
import { createAttachmentAdapter } from './attachmentAdapter'
import { createScratchlistAttachmentAdapter } from './scratchlistAttachmentAdapter'
function deferred<T>() {
let resolve!: (value: T) => void
let reject!: (error: Error) => void
const promise = new Promise<T>((yes, no) => { resolve = yes; reject = no })
return { promise, resolve, reject }
}
describe.each(['chat', 'scratchlist', 'scratchlist-chat-path'] as const)('%s attachment removal', (mode) => {
function setup() {
const cleanup = deferred<void>()
const deleteUploadFile = vi.fn(() => cleanup.promise)
const deleteScratchlistAttachment = vi.fn(() => cleanup.promise)
const api = { deleteUploadFile, deleteScratchlistAttachment } as never
const adapter = mode === 'chat'
? createAttachmentAdapter(api, 'current-session')
: createScratchlistAttachmentAdapter(api, 'current-session')
const attachment = {
id: 'a', type: 'file', name: 'A.png', contentType: 'image/png',
status: { type: 'requires-action', reason: 'composer-send' },
path: '/uploads/A.png',
uploadSessionId: 'upload-owner',
...(mode === 'scratchlist' ? { hubAttachment: { id: 'hub-a' } } : {}),
}
return { adapter, attachment, cleanup, deleteUploadFile, deleteScratchlistAttachment }
}
it('allows the composer to remove a chip before remote cleanup settles', async () => {
const { adapter, attachment, cleanup, deleteUploadFile, deleteScratchlistAttachment } = setup()
const removed = vi.fn()
const removal = adapter.remove(attachment as never).then(removed)
try {
await vi.waitFor(() => expect(removed).toHaveBeenCalledOnce(), { timeout: 500 })
if (mode === 'scratchlist') {
expect(deleteScratchlistAttachment).toHaveBeenCalledExactlyOnceWith('current-session', 'hub-a')
expect(deleteUploadFile).not.toHaveBeenCalled()
} else {
expect(deleteUploadFile).toHaveBeenCalledExactlyOnceWith(
mode === 'chat' ? 'upload-owner' : 'current-session', '/uploads/A.png',
)
expect(deleteScratchlistAttachment).not.toHaveBeenCalled()
}
} finally {
cleanup.resolve()
await removal
}
})
it('handles a cleanup rejection after the attachment has been removed', async () => {
const { adapter, attachment, cleanup } = setup()
const removed = vi.fn()
const removal = adapter.remove(attachment as never).then(removed)
try {
await vi.waitFor(() => expect(removed).toHaveBeenCalledOnce(), { timeout: 500 })
} finally {
cleanup.reject(new Error('Network unavailable'))
await removal
}
})
})
it('cleans up a cancelled chat upload without publishing its late completion', async () => {
const upload = deferred<{ success: boolean, path: string }>()
const started = deferred<void>()
const deleteUploadFile = vi.fn().mockResolvedValue({ success: true })
const adapter = createAttachmentAdapter({
uploadFile: vi.fn(() => { started.resolve(); return upload.promise }),
deleteUploadFile,
} as never, 'upload-owner')
const iterator = adapter.add({ file: new File(['a'], 'A.txt', { type: 'text/plain' }) }) as AsyncGenerator
await iterator.next()
const uploading = await iterator.next()
const completion = iterator.next()
await started.promise
await adapter.remove(uploading.value)
upload.resolve({ success: true, path: '/uploads/late.txt' })
expect(await completion).toEqual({ done: true, value: undefined })
expect(deleteUploadFile).toHaveBeenCalledExactlyOnceWith('upload-owner', '/uploads/late.txt')
})
+3 -2
View File
@@ -191,13 +191,14 @@ export function createScratchlistAttachmentAdapter(
cancelledAttachmentIds.add(attachment.id)
const pending = attachment as PendingScratchlistAttachment
const hubId = pending.hubAttachment?.id
// Let the composer drop the chip while best-effort cleanup runs.
if (hubId) {
await api.deleteScratchlistAttachment(sessionId, hubId).catch(() => {})
void api.deleteScratchlistAttachment(sessionId, hubId).catch(() => {})
return
}
// Chat-path chip attached before scratchlist mode was enabled (#1226).
if (pending.path && !isHubScratchlistAttachmentPath(pending.path)) {
await api.deleteUploadFile(sessionId, pending.path).catch(() => {})
void api.deleteUploadFile(sessionId, pending.path).catch(() => {})
}
},