fix(builtins): archive sidechat sessions when their tabs close
Closing a bound side chat tab now archives that thread through uiWorkspace.archiveSession with stopActivity, then disposes the live agent. A missing workspace service or a failed archive still disposes the agent, and the close path settles on its own. A tab with no thread id skips both steps. The header action that saves a copy as a new session, and the explicit archive control, stay as they were. Fixes #942
This commit is contained in:
2 files changed
+124
-6
No files matched your search
@@ -37,6 +37,38 @@ import { api } from '../api.ts'
|
||||
import type { Context } from '../../context-types.ts'
|
||||
import type { TabDescriptor } from '../service.ts'
|
||||
|
||||
/** Host session surface (`ctx.get('uiWorkspace')`). Archive is optional. */
|
||||
interface ArchiveSessionFace {
|
||||
archiveSession?: (sessionId: string, options: { stopActivity: boolean }) => Promise<void>
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide a closed Side Chat thread, then release its live agent.
|
||||
*
|
||||
* `stopActivity` settles a running turn before the session leaves the list.
|
||||
* The workspace service is optional: a missing service or a rejected archive
|
||||
* still disposes, and the close path itself never rejects. An unbound tab
|
||||
* never reaches here.
|
||||
*/
|
||||
function releaseSidechat(ctx: Context, threadId: string): void {
|
||||
const dispose = (): void => {
|
||||
void api.sidechatDispose(threadId).catch(() => {})
|
||||
}
|
||||
void (async () => {
|
||||
try {
|
||||
const service = ctx.get('uiWorkspace') as unknown as ArchiveSessionFace | undefined
|
||||
// Method call: an unbound `archiveSession` would lose the service as `this`.
|
||||
if (service?.archiveSession !== undefined) {
|
||||
await service.archiveSession(threadId, { stopActivity: true })
|
||||
}
|
||||
} catch {
|
||||
// Archive is best-effort. Dispose still runs in finally.
|
||||
} finally {
|
||||
dispose()
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
/** The 6 built-in tab descriptors (the host owns the right-Sidebar terminal and browser). */
|
||||
export function builtinTabs(ctx: Context): readonly TabDescriptor[] {
|
||||
return [
|
||||
@@ -227,13 +259,13 @@ export function builtinTabs(ctx: Context): readonly TabDescriptor[] {
|
||||
// One tab per thread: an already-open thread focuses instead of
|
||||
// duplicating; unbound fresh tabs never dedupe (each mints its own).
|
||||
dedupeKey: (tab) => sidechatThreadIdOf(tab),
|
||||
// Closing the tab releases the thread's live agent; the session and
|
||||
// its history stay persisted (reopen from any thread's header menu).
|
||||
// A bound thread is archived on close, then its live agent is released.
|
||||
// Saving a copy first is the header's existing "save as new session";
|
||||
// that action (and the explicit archive control) is unchanged. An
|
||||
// unbound fresh tab has no session, so close cleans nothing up.
|
||||
onClose: (tab) => {
|
||||
const threadId = sidechatThreadIdOf(tab)
|
||||
if (threadId !== undefined) {
|
||||
void api.sidechatDispose(threadId).catch(() => {})
|
||||
}
|
||||
if (threadId !== undefined) releaseSidechat(ctx, threadId)
|
||||
},
|
||||
component: ({ ctx, scope, tab, visible }) => (
|
||||
<SideChatView ctx={ctx} scope={scope} tab={tab} visible={visible} />
|
||||
|
||||
+87
-1
@@ -14,16 +14,18 @@
|
||||
* terminal, which is `bottomOnly` and therefore contributes no native tab
|
||||
* type. It is the one built-in whose carrier surface is asserted here.
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { ReactElement } from 'react'
|
||||
import { VscCommentDiscussion, VscGitCommit, VscLayers } from 'react-icons/vsc'
|
||||
// First import: browser globals before the xterm-carrying builtin graph loads.
|
||||
import './browser-globals.ts'
|
||||
|
||||
import type { Context } from '../src/context-types.ts'
|
||||
import { api } from '../src/client/api.ts'
|
||||
import { createBetterSidebarService } from '../src/client/service.ts'
|
||||
import { planFsReadOutcome } from '../src/client/editor-load.ts'
|
||||
import { createSidebarStore } from '../src/client/state.ts'
|
||||
import { builtinTabs } from '../src/client/builtins/tabs.tsx'
|
||||
import { registerBuiltins } from '../src/client/builtins/index.ts'
|
||||
import { parkSidechatReopen } from '../src/client/SideChatView.tsx'
|
||||
|
||||
@@ -231,6 +233,90 @@ describe('built-in tab registrations', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('side chat close archives the bound thread', () => {
|
||||
const scope = { sessionId: 'parent' }
|
||||
|
||||
function bound(threadId: string) {
|
||||
return { id: `sidechat:${threadId}`, type: 'sidechat', title: 'Side Chat', meta: { threadId } }
|
||||
}
|
||||
|
||||
function ctxWithArchive(
|
||||
archiveSession?: (sessionId: string, options: { stopActivity: boolean }) => Promise<void>,
|
||||
): Context {
|
||||
return {
|
||||
get: (name: string) => (
|
||||
name === 'uiWorkspace' && archiveSession !== undefined ? { archiveSession } : undefined
|
||||
),
|
||||
} as unknown as Context
|
||||
}
|
||||
|
||||
function sidechatClose(ctx: Context) {
|
||||
return builtinTabs(ctx).find(tab => tab.id === 'sidechat')?.onClose
|
||||
}
|
||||
|
||||
afterEach(() => { vi.restoreAllMocks() })
|
||||
|
||||
it('archives a bound thread with stopActivity, then disposes the live agent', async () => {
|
||||
const order: string[] = []
|
||||
let settle!: () => void
|
||||
const gate = new Promise<void>(resolve => { settle = resolve })
|
||||
const archiveSession = vi.fn(async (sessionId: string) => {
|
||||
order.push(`archive:${sessionId}`)
|
||||
await gate
|
||||
})
|
||||
const dispose = vi.spyOn(api, 'sidechatDispose').mockImplementation(async (childId) => {
|
||||
order.push(`dispose:${childId}`)
|
||||
return { accepted: true }
|
||||
})
|
||||
sidechatClose(ctxWithArchive(archiveSession))?.(bound('child-1'), scope)
|
||||
expect(archiveSession).toHaveBeenCalledWith('child-1', { stopActivity: true })
|
||||
expect(dispose).not.toHaveBeenCalled()
|
||||
settle()
|
||||
await vi.waitFor(() => {
|
||||
expect(order).toEqual(['archive:child-1', 'dispose:child-1'])
|
||||
})
|
||||
})
|
||||
|
||||
it('still disposes when archive is unavailable or rejects, with no unhandled rejection', async () => {
|
||||
const rejections: unknown[] = []
|
||||
const onRejection = (reason: unknown): void => { rejections.push(reason) }
|
||||
process.on('unhandledRejection', onRejection)
|
||||
const dispose = vi.spyOn(api, 'sidechatDispose').mockResolvedValue({ accepted: true })
|
||||
try {
|
||||
sidechatClose(ctxWithArchive())?.(bound('child-missing'), scope)
|
||||
const throwing = {
|
||||
get: () => { throw new Error('no workspace service') },
|
||||
} as unknown as Context
|
||||
sidechatClose(throwing)?.(bound('child-throw'), scope)
|
||||
const archiveSession = vi.fn(() => Promise.reject(new Error('archive failed')))
|
||||
sidechatClose(ctxWithArchive(archiveSession))?.(bound('child-reject'), scope)
|
||||
await vi.waitFor(() => {
|
||||
expect(dispose.mock.calls.map(call => call[0])).toEqual(['child-missing', 'child-throw', 'child-reject'])
|
||||
})
|
||||
expect(archiveSession).toHaveBeenCalledWith('child-reject', { stopActivity: true })
|
||||
await new Promise(resolve => { setTimeout(resolve, 0) })
|
||||
expect(rejections).toEqual([])
|
||||
} finally {
|
||||
process.off('unhandledRejection', onRejection)
|
||||
}
|
||||
})
|
||||
|
||||
it('does not archive or dispose an unbound autoCreate tab', async () => {
|
||||
const archiveSession = vi.fn(async () => {})
|
||||
const dispose = vi.spyOn(api, 'sidechatDispose').mockResolvedValue({ accepted: true })
|
||||
sidechatClose(ctxWithArchive(archiveSession))?.({
|
||||
id: 'sidechat:new-1',
|
||||
type: 'sidechat',
|
||||
title: 'Untitled',
|
||||
meta: { autoCreate: true },
|
||||
}, scope)
|
||||
await Promise.resolve()
|
||||
await Promise.resolve()
|
||||
expect(archiveSession).not.toHaveBeenCalled()
|
||||
expect(dispose).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('built-in file viewer registrations', () => {
|
||||
it('registers the 4 built-in file viewers (the read-only previews yielded to the host)', () => {
|
||||
const { service } = setup()
|
||||
|
||||
Reference in new issue
Block a user