mirror of
https://github.com/supabase/supabase.git
synced 2026-10-07 18:35:07 +03:00
## Problem
- Assistant responses were capped at 120 seconds and 10 steps, which is
too short for longer reasoning or multi-step tool work.
- When the hosting platform ended a request at that limit, the
connection just dropped. The user got no explanation, and "Thinking…"
and tool rows kept spinning.
- Studio's own tools ignored the request's abort signal, so a stop,
disconnect or deadline couldn't cancel their in-flight requests.
- Aborted responses never closed their Braintrust span. Under TanStack
Start, the remote MCP client was only released on `res.on('close')`,
which the adapter never emits.
## Solution
Uses AI SDK options instead of custom stream handling:
- `maxDuration` goes to 300s and the step limit to 20. `streamText({
timeout: { totalMs } })` stops the response at 270s, leaving time to
finish the stream before the platform cutoff.
- `toUIMessageStream({ messageMetadata })` marks an aborted response
`timedOut: true`. `Chat` ignores `abort` chunks, so the client reads
this flag instead and shows a timeout alert with Retry. The flag is
saved with the message, so the alert survives a reload.
- `toUIMessageStream({ onEnd })` aborts the request whenever the stream
ends, releasing the MCP client on both runtimes. `streamText({ onAbort
})` ends the Braintrust span.
- Studio tools pass the SDK's `abortSignal` to their fetches. MCP tools
already did.
- Reasoning and server-tool rows that never finished show "Response
interrupted" instead of a spinner or "Ran X ✓".
There's no per-tool timeout. Approved SQL and migrations can
legitimately run longer, and aborting the HTTP request doesn't stop the
query in Postgres.
## Review instructions
1. Run the unit tests: `cd apps/studio && pnpm vitest run
lib/api/generate-v4.test.ts lib/ai components/ui/AIAssistantPanel`
2. To see a timeout without waiting 4.5 minutes, temporarily set
`ASSISTANT_TIMEOUT_MS` in `apps/studio/lib/ai/assistant-timeout.ts` to
`15_000` and run `pnpm dev:studio`.
3. Ask the Assistant something that needs several tool calls or long
reasoning, for example "Audit my schema for missing indexes and RLS
gaps, then write the fixes."
4. After 15 seconds, check that:
- the response stops and a "Assistant response timed out" alert appears
with Retry
- any in-progress reasoning or tool row shows "Response interrupted"
instead of spinning
- Retry starts a new response
- reloading the page still shows the alert on that chat
5. Stop a response with the Stop button before the deadline. It should
stop without the timeout alert.
6. With the default 270s, confirm that a normal response completes as
before.
## Checklist
Check all before review:
- [ ] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which references
[WORD_LIST](https://github.com/supabase/supabase/blob/master/apps/docs/WORD_LIST.md)
and the docs
[CONTRIBUTING](https://github.com/supabase/supabase/blob/master/apps/docs/CONTRIBUTING.md)
guide
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Improvements**
* AI assistant responses can now run for up to five minutes, supporting
longer requests.
* When a response times out, the assistant displays a message suggesting
you retry or ask for a smaller change.
* Incomplete responses now show a “Response interrupted” notice, and
loading indicators stop when generation ends.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
248 lines
8.3 KiB
TypeScript
248 lines
8.3 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { getIncidentTools } from './incident-tools'
|
|
|
|
// Mock IS_PLATFORM
|
|
vi.mock('common', () => ({
|
|
IS_PLATFORM: true,
|
|
}))
|
|
|
|
const executeOptions = { toolCallId: 'test', messages: [], context: {} }
|
|
|
|
describe('ai/tools/incident-tools', () => {
|
|
let mockFetch: ReturnType<typeof vi.fn>
|
|
let mockAbortSignal: AbortSignal
|
|
|
|
beforeEach(() => {
|
|
mockFetch = vi.fn()
|
|
global.fetch = mockFetch as typeof fetch
|
|
|
|
// Mock AbortSignal.timeout
|
|
mockAbortSignal = new AbortController().signal
|
|
if (!AbortSignal.timeout) {
|
|
AbortSignal.timeout = vi.fn(() => mockAbortSignal) as any
|
|
}
|
|
})
|
|
|
|
describe('getIncidentTools', () => {
|
|
it('should return an object with get_active_incidents tool', () => {
|
|
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
|
|
|
|
expect(tools).toBeDefined()
|
|
expect(tools.get_active_incidents).toBeDefined()
|
|
})
|
|
|
|
it('should have correct description for get_active_incidents', () => {
|
|
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
|
|
|
|
expect(tools.get_active_incidents.description).toContain('Check for active incidents')
|
|
expect(tools.get_active_incidents.description).toContain('Supabase service')
|
|
})
|
|
|
|
it('should have empty input schema', () => {
|
|
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
|
|
const schema = tools.get_active_incidents.inputSchema
|
|
|
|
// The schema is a Zod object that accepts empty object
|
|
expect(schema).toBeDefined()
|
|
expect((schema as any)._def.typeName).toBe('ZodObject')
|
|
})
|
|
|
|
describe('execute function', () => {
|
|
it('should return empty incidents when not on platform', async () => {
|
|
const common = await import('common')
|
|
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(false)
|
|
|
|
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
|
|
const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)
|
|
|
|
expect(result).toEqual({
|
|
incidents: [],
|
|
message: 'Incident checking is only available on Supabase platform.',
|
|
})
|
|
expect(mockFetch).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('should fetch incidents from correct URL', async () => {
|
|
const common = await import('common')
|
|
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)
|
|
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () => [],
|
|
})
|
|
|
|
const tools = getIncidentTools({ baseUrl: 'https://example.com/dashboard' })
|
|
if (!tools.get_active_incidents.execute) throw new Error('execute is undefined')
|
|
await tools.get_active_incidents.execute(
|
|
{},
|
|
{ toolCallId: 'test', messages: [], context: {} }
|
|
)
|
|
|
|
expect(mockFetch).toHaveBeenCalledWith(
|
|
'https://example.com/dashboard/api/incident-status',
|
|
{
|
|
signal: expect.any(AbortSignal),
|
|
}
|
|
)
|
|
})
|
|
|
|
it('should return message when no incidents', async () => {
|
|
const common = await import('common')
|
|
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)
|
|
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () => [],
|
|
})
|
|
|
|
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
|
|
const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)
|
|
|
|
expect(result).toEqual({
|
|
incidents: [],
|
|
message: expect.stringContaining('No active incidents'),
|
|
})
|
|
})
|
|
|
|
it('should return incident summaries when incidents exist', async () => {
|
|
const common = await import('common')
|
|
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)
|
|
|
|
const mockIncidents = [
|
|
{
|
|
name: 'Database slowness',
|
|
status: 'investigating',
|
|
impact: 'minor',
|
|
active_since: '2024-01-01T10:00:00Z',
|
|
extra_field: 'should be filtered',
|
|
},
|
|
]
|
|
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () => mockIncidents,
|
|
})
|
|
|
|
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
|
|
const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)
|
|
|
|
expect((result as any).incidents).toEqual([
|
|
{
|
|
name: 'Database slowness',
|
|
status: 'investigating',
|
|
impact: 'minor',
|
|
active_since: '2024-01-01T10:00:00Z',
|
|
},
|
|
])
|
|
expect((result as any).message).toContain('1 active incident')
|
|
expect((result as any).message).toContain('status.supabase.com')
|
|
})
|
|
|
|
it('should handle multiple incidents', async () => {
|
|
const common = await import('common')
|
|
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)
|
|
|
|
const mockIncidents = [
|
|
{
|
|
name: 'Database issue',
|
|
status: 'investigating',
|
|
impact: 'major',
|
|
active_since: '2024-01-01T10:00:00Z',
|
|
},
|
|
{
|
|
name: 'Storage issue',
|
|
status: 'identified',
|
|
impact: 'minor',
|
|
active_since: '2024-01-01T11:00:00Z',
|
|
},
|
|
]
|
|
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () => mockIncidents,
|
|
})
|
|
|
|
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
|
|
const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)
|
|
|
|
expect((result as any).incidents).toHaveLength(2)
|
|
expect((result as any).message).toContain('2 active incidents')
|
|
})
|
|
|
|
it('should handle fetch errors', async () => {
|
|
const common = await import('common')
|
|
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)
|
|
|
|
mockFetch.mockRejectedValue(new Error('Network error'))
|
|
|
|
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
|
|
const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)
|
|
|
|
expect(result).toEqual({
|
|
incidents: [],
|
|
error: 'Unable to check incident status at this time.',
|
|
})
|
|
})
|
|
|
|
it('should handle non-ok responses', async () => {
|
|
const common = await import('common')
|
|
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)
|
|
|
|
mockFetch.mockResolvedValue({
|
|
ok: false,
|
|
status: 500,
|
|
})
|
|
|
|
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
|
|
const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)
|
|
|
|
expect(result).toEqual({
|
|
incidents: [],
|
|
error: 'Unable to check incident status at this time.',
|
|
})
|
|
})
|
|
|
|
it('should use timeout signal', async () => {
|
|
const common = await import('common')
|
|
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)
|
|
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () => [],
|
|
})
|
|
|
|
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
|
|
if (!tools.get_active_incidents.execute) throw new Error('execute is undefined')
|
|
await tools.get_active_incidents.execute(
|
|
{},
|
|
{ toolCallId: 'test', messages: [], context: {} }
|
|
)
|
|
|
|
const callArgs = mockFetch.mock.calls[0]
|
|
expect(callArgs[1].signal).toBeInstanceOf(AbortSignal)
|
|
})
|
|
|
|
it('cancels the request when the Assistant request is aborted', async () => {
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () => [],
|
|
})
|
|
const abortController = new AbortController()
|
|
|
|
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
|
|
if (!tools.get_active_incidents.execute) throw new Error('execute is undefined')
|
|
await tools.get_active_incidents.execute(
|
|
{},
|
|
{ ...executeOptions, abortSignal: abortController.signal }
|
|
)
|
|
|
|
const { signal } = mockFetch.mock.calls[0][1]
|
|
expect(signal.aborted).toBe(false)
|
|
abortController.abort()
|
|
expect(signal.aborted).toBe(true)
|
|
})
|
|
})
|
|
})
|
|
})
|