Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/durable/chat-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,7 @@ export function handleChatTurn(input: RunChatTurnInput): ChatTurnResult {
? await hooks.transformFinalText(rawFinal)
: rawFinal

try {
await hooks.persistAssistantMessage({ identity, finalText })
} catch (err) {
log('[chat-engine] persistAssistantMessage threw', {
error: err instanceof Error ? err.message : String(err),
})
}
await hooks.persistAssistantMessage({ identity, finalText })
if (hooks.onTurnComplete) {
try {
await hooks.onTurnComplete({ identity, finalText })
Expand Down
11 changes: 7 additions & 4 deletions src/durable/tests/chat-engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe('handleChatTurn', () => {
expect(persisted).toBe('SSN [REDACTED]')
})

it('a throwing persist hook is swallowed — the turn still completes', async () => {
it('a throwing persist hook fails the turn before completion', async () => {
const { body } = handleChatTurn({
identity: IDENTITY,
log: () => undefined,
Expand All @@ -146,7 +146,10 @@ describe('handleChatTurn', () => {
},
})
const events = await drain(body)
expect(events.at(-1)?.type).toBe('session.run.completed')
expect(events.find((e) => e.type === 'session.run.completed')).toBeUndefined()
const err = events.find((e) => e.type === 'error')
expect(err?.data?.message).toBe('db down')
expect(events.at(-1)?.type).toBe('session.run.failed')
})

it('traceFlush is handed to waitUntil so the worker isolate survives the POST', async () => {
Expand All @@ -173,7 +176,7 @@ describe('handleChatTurn', () => {
expect(flushAwaited).toBe(true)
})

it('swallowed hook errors are logged to console.error by default', async () => {
it('persist hook errors are logged as turn failures by default', async () => {
const spy = vi.spyOn(console, 'error').mockImplementation(() => undefined)
try {
const { body } = handleChatTurn({
Expand All @@ -188,7 +191,7 @@ describe('handleChatTurn', () => {
await drain(body)
expect(spy).toHaveBeenCalled()
const messages = spy.mock.calls.map((c) => c[0])
expect(messages.some((m) => String(m).includes('persistAssistantMessage'))).toBe(true)
expect(messages.some((m) => String(m).includes('turn failed'))).toBe(true)
} finally {
spy.mockRestore()
}
Expand Down
Loading