Add Ralph status dialog and session management improvements (#171)

This commit is contained in:
Chris Scott authored and GitHub committed 2026-03-24 20:25:09 -04:00
1 parent 27352ea5cf
commit 86613b1ea4
12 files changed
+1132 -65

No files matched your search

+132 -39
View File
@@ -64,17 +64,19 @@ export function createRalphEventHandler(
logger.error(`Ralph: failed to commit changes in worktree ${state.worktreeDir}`, err)
}
try {
const gitCommonDir = execSync('git rev-parse --git-common-dir', { cwd: state.worktreeDir, encoding: 'utf-8' }).trim()
const gitRoot = resolve(state.worktreeDir, gitCommonDir, '..')
const removeResult = spawnSync('git', ['worktree', 'remove', '-f', state.worktreeDir], { cwd: gitRoot, encoding: 'utf-8' })
if (removeResult.status !== 0) {
throw new Error(removeResult.stderr || 'git worktree remove failed')
if (state.worktreeDir && state.worktreeBranch) {
try {
const gitCommonDir = execSync('git rev-parse --git-common-dir', { cwd: state.worktreeDir, encoding: 'utf-8' }).trim()
const gitRoot = resolve(state.worktreeDir, gitCommonDir, '..')
const removeResult = spawnSync('git', ['worktree', 'remove', '-f', state.worktreeDir], { cwd: gitRoot, encoding: 'utf-8' })
if (removeResult.status !== 0) {
throw new Error(removeResult.stderr || 'git worktree remove failed')
}
cleaned = true
logger.log(`Ralph: removed worktree ${state.worktreeDir}, branch ${state.worktreeBranch} preserved`)
} catch (err) {
logger.error(`Ralph: failed to remove worktree ${state.worktreeDir}`, err)
}
cleaned = true
logger.log(`Ralph: removed worktree ${state.worktreeDir}, branch ${state.worktreeBranch} preserved`)
} catch (err) {
logger.error(`Ralph: failed to remove worktree ${state.worktreeDir}`, err)
}
return { committed, cleaned }
@@ -188,7 +190,7 @@ export function createRalphEventHandler(
logger.log(`Ralph loop terminated: reason="${reason}", worktree="${state.worktreeName}", iteration=${state.iteration}`)
let commitResult: { committed: boolean; cleaned: boolean } | undefined
if (reason === 'completed') {
if (reason === 'completed' || reason === 'cancelled') {
commitResult = await commitAndCleanupWorktree(state)
}
}
@@ -227,7 +229,7 @@ export function createRalphEventHandler(
}
}
async function getLastAssistantText(sessionId: string, worktreeDir: string): Promise<string | null> {
async function getLastAssistantInfo(sessionId: string, worktreeDir: string): Promise<{ text: string | null; error: string | null }> {
try {
const messagesResult = await v2Client.session.messages({
sessionID: sessionId,
@@ -236,21 +238,25 @@ export function createRalphEventHandler(
})
const messages = (messagesResult.data ?? []) as Array<{
info: { role: string }
info: { role: string; error?: { name?: string; data?: { message?: string } } }
parts: Array<{ type: string; text?: string }>
}>
const lastAssistant = [...messages].reverse().find((m) => m.info.role === 'assistant')
if (!lastAssistant) return null
if (!lastAssistant) return { text: null, error: null }
return lastAssistant.parts
const text = lastAssistant.parts
.filter((p) => p.type === 'text' && typeof p.text === 'string')
.map((p) => p.text as string)
.join('\n')
.join('\n') || null
const error = lastAssistant.info.error?.data?.message ?? lastAssistant.info.error?.name ?? null
return { text, error }
} catch (err) {
logger.error(`Ralph: could not read session messages`, err)
return null
return { text: null, error: null }
}
}
@@ -292,9 +298,31 @@ export function createRalphEventHandler(
return
}
if (!currentState.worktreeDir) {
logger.error(`Ralph: loop ${sessionId} missing worktreeDir in coding phase, terminating`)
await terminateLoop(sessionId, currentState, 'missing_worktree_dir')
return
}
let assistantErrorDetected = false
if (currentState.completionPromise) {
const textContent = await getLastAssistantText(sessionId, currentState.worktreeDir)
if (textContent && ralphService.checkCompletionPromise(textContent, currentState.completionPromise)) {
const { text: textContent, error: assistantError } = await getLastAssistantInfo(sessionId, currentState.worktreeDir)
if (assistantError) {
assistantErrorDetected = true
logger.error(`Ralph: assistant error detected in coding phase: ${assistantError}`)
const isModelError = /provider|auth|model|api\s*error/i.test(assistantError)
if (isModelError) {
const nextErrorCount = (currentState.errorCount ?? 0) + 1
if (nextErrorCount >= MAX_RETRIES) {
await terminateLoop(sessionId, currentState, `error_max_retries: assistant error: ${assistantError}`)
return
}
ralphService.setState(sessionId, { ...currentState, modelFailed: true, errorCount: nextErrorCount })
logger.log(`Ralph: marking model as failed, will fall back to default model (error ${nextErrorCount}/${MAX_RETRIES})`)
currentState = ralphService.getActiveState(sessionId)!
}
}
if (textContent && currentState.completionPromise && ralphService.checkCompletionPromise(textContent, currentState.completionPromise)) {
const currentAuditCount = currentState.auditCount ?? 0
if (!currentState.audit || currentAuditCount >= minAudits) {
await terminateLoop(sessionId, currentState, 'completed')
@@ -305,14 +333,20 @@ export function createRalphEventHandler(
}
}
if (currentState.maxIterations > 0 && currentState.iteration >= currentState.maxIterations) {
if (!assistantErrorDetected && currentState.errorCount && currentState.errorCount > 0) {
ralphService.setState(sessionId, { ...currentState, errorCount: 0 })
logger.log(`Ralph: resetting error count after successful retry in coding phase`)
currentState = ralphService.getActiveState(sessionId)!
}
if ((currentState.maxIterations ?? 0) > 0 && (currentState.iteration ?? 0) >= (currentState.maxIterations ?? 0)) {
await terminateLoop(sessionId, currentState, 'max_iterations')
return
}
if (currentState.audit) {
ralphService.setState(sessionId, { ...currentState, phase: 'auditing', errorCount: 0 })
logger.log(`Ralph iteration ${currentState.iteration} complete, running auditor for session ${sessionId}`)
logger.log(`Ralph iteration ${currentState.iteration ?? 0} complete, running auditor for session ${sessionId}`)
const auditPrompt = {
sessionID: sessionId,
@@ -353,19 +387,26 @@ export function createRalphEventHandler(
logger.error(`Ralph: session rotation failed, continuing with existing session`, err)
}
const nextIteration = currentState.iteration + 1
const nextIteration = (currentState.iteration ?? 0) + 1
ralphService.setState(activeSessionId, {
...currentState,
sessionId: activeSessionId,
iteration: nextIteration,
errorCount: 0,
errorCount: assistantErrorDetected ? currentState.errorCount : 0,
})
const continuationPrompt = ralphService.buildContinuationPrompt({ ...currentState, iteration: nextIteration })
logger.log(`Ralph iteration ${nextIteration} for session ${activeSessionId}`)
const currentConfig = getConfig()
const ralphModel = parseModelString(currentConfig.ralph?.model) ?? parseModelString(currentConfig.executionModel)
const freshStateForModel = ralphService.getActiveState(activeSessionId)
const ralphModel = freshStateForModel?.modelFailed
? undefined
: (parseModelString(currentConfig.ralph?.model) ?? parseModelString(currentConfig.executionModel))
if (freshStateForModel?.modelFailed) {
logger.log(`Ralph: configured model previously failed, using default model`)
}
const sendContinuationPromptWithModel = async () => {
const freshState = ralphService.getActiveState(activeSessionId)
@@ -376,7 +417,7 @@ export function createRalphEventHandler(
sessionID: activeSessionId,
directory: freshState.worktreeDir,
parts: [{ type: 'text' as const, text: continuationPrompt }],
model: ralphModel!,
model: ralphModel,
})
return { data: result.data, error: result.error }
}
@@ -405,7 +446,8 @@ export function createRalphEventHandler(
const retryFn = async () => {
const result = await sendContinuationPromptWithoutModel()
if (result.error) {
throw result.error
await handlePromptError(activeSessionId, currentState, 'retry failed', result.error)
return
}
}
await handlePromptError(activeSessionId, currentState, 'failed to send continuation prompt', promptResult.error, retryFn)
@@ -423,17 +465,46 @@ export function createRalphEventHandler(
async function handleAuditingPhase(sessionId: string, state: RalphState): Promise<void> {
// Re-fetch and validate state to catch aborts that happened during idle event processing
const currentState = ralphService.getActiveState(sessionId)
let currentState = ralphService.getActiveState(sessionId)
if (!currentState?.active) {
logger.log(`Ralph: loop ${sessionId} no longer active, skipping auditing phase`)
return
}
const auditText = await getLastAssistantText(sessionId, currentState.worktreeDir)
if (!currentState.worktreeDir) {
logger.error(`Ralph: loop ${sessionId} missing worktreeDir in auditing phase, terminating`)
await terminateLoop(sessionId, currentState, 'missing_worktree_dir')
return
}
const nextIteration = currentState.iteration + 1
const { text: auditText, error: assistantError } = await getLastAssistantInfo(sessionId, currentState.worktreeDir)
let assistantErrorDetected = false
if (assistantError) {
assistantErrorDetected = true
logger.error(`Ralph: assistant error detected in auditing phase: ${assistantError}`)
const isModelError = /provider|auth|model|api\s*error/i.test(assistantError)
if (isModelError) {
const nextErrorCount = (currentState.errorCount ?? 0) + 1
if (nextErrorCount >= MAX_RETRIES) {
await terminateLoop(sessionId, currentState, `error_max_retries: assistant error: ${assistantError}`)
return
}
ralphService.setState(sessionId, { ...currentState, modelFailed: true, errorCount: nextErrorCount })
logger.log(`Ralph: marking model as failed, will fall back to default model (error ${nextErrorCount}/${MAX_RETRIES})`)
currentState = ralphService.getActiveState(sessionId)!
}
}
if (!assistantErrorDetected && currentState.errorCount && currentState.errorCount > 0) {
ralphService.setState(sessionId, { ...currentState, errorCount: 0 })
logger.log(`Ralph: resetting error count after successful retry in auditing phase`)
currentState = ralphService.getActiveState(sessionId)!
}
const nextIteration = (currentState.iteration ?? 0) + 1
const newAuditCount = (currentState.auditCount ?? 0) + 1
logger.log(`Ralph audit ${newAuditCount} at iteration ${currentState.iteration}`)
logger.log(`Ralph audit ${newAuditCount} at iteration ${currentState.iteration ?? 0}`)
// Always pass the full audit response to the code agent
const auditFindings = auditText ?? undefined
@@ -450,7 +521,7 @@ export function createRalphEventHandler(
}
}
if (currentState.maxIterations > 0 && nextIteration > currentState.maxIterations) {
if ((currentState.maxIterations ?? 0) > 0 && nextIteration > (currentState.maxIterations ?? 0)) {
await terminateLoop(sessionId, currentState, 'max_iterations')
return
}
@@ -469,7 +540,7 @@ export function createRalphEventHandler(
phase: 'coding',
lastAuditResult: auditFindings,
auditCount: newAuditCount,
errorCount: 0,
errorCount: assistantErrorDetected ? currentState.errorCount : 0,
})
const continuationPrompt = ralphService.buildContinuationPrompt(
@@ -479,7 +550,14 @@ export function createRalphEventHandler(
logger.log(`Ralph iteration ${nextIteration} for session ${activeSessionId}`)
const currentConfig = getConfig()
const ralphModel = parseModelString(currentConfig.ralph?.model) ?? parseModelString(currentConfig.executionModel)
const freshStateForModel = ralphService.getActiveState(activeSessionId)
const ralphModel = freshStateForModel?.modelFailed
? undefined
: (parseModelString(currentConfig.ralph?.model) ?? parseModelString(currentConfig.executionModel))
if (freshStateForModel?.modelFailed) {
logger.log(`Ralph: configured model previously failed, using default model`)
}
const sendContinuationPromptWithModel = async () => {
const freshState = ralphService.getActiveState(activeSessionId)
@@ -490,7 +568,7 @@ export function createRalphEventHandler(
sessionID: activeSessionId,
directory: freshState.worktreeDir,
parts: [{ type: 'text' as const, text: continuationPrompt }],
model: ralphModel!,
model: ralphModel,
})
return { data: result.data, error: result.error }
}
@@ -523,7 +601,8 @@ export function createRalphEventHandler(
}
const result = await sendContinuationPromptWithoutModel()
if (result.error) {
throw result.error
await handlePromptError(activeSessionId, currentState, 'retry failed after audit', result.error)
return
}
}
await handlePromptError(activeSessionId, currentState, 'failed to send continuation prompt after audit', promptResult.error, retryFn)
@@ -558,17 +637,31 @@ export function createRalphEventHandler(
}
if (event.type === 'session.error') {
const errorProps = event.properties as { sessionID?: string; error?: { name?: string } }
const errorProps = event.properties as { sessionID?: string; error?: { name?: string; data?: { message?: string } } }
const eventSessionId = errorProps?.sessionID
const errorName = errorProps?.error?.name
const isAbort = errorName === 'MessageAbortedError' || errorName === 'AbortError'
if (!eventSessionId || !isAbort) return
if (!eventSessionId) return
if (isAbort) {
const state = ralphService.getActiveState(eventSessionId)
if (state?.active) {
logger.log(`Ralph: session ${eventSessionId} aborted, terminating loop`)
await terminateLoop(eventSessionId, state, 'user_aborted')
}
return
}
const state = ralphService.getActiveState(eventSessionId)
if (state?.active) {
logger.log(`Ralph: session ${eventSessionId} aborted, terminating loop`)
await terminateLoop(eventSessionId, state, 'user_aborted')
const errorMessage = errorProps?.error?.data?.message ?? errorName ?? 'unknown error'
logger.error(`Ralph: session error for ${eventSessionId}: ${errorMessage}`)
const isModelError = /provider|auth|model|api\s*error/i.test(errorMessage)
if (isModelError && !state.modelFailed) {
logger.log(`Ralph: marking model as failed, will fall back to default on next iteration`)
ralphService.setState(eventSessionId, { ...state, modelFailed: true })
}
}
return
}
+19 -18
View File
@@ -12,23 +12,24 @@ export const RECENT_MESSAGES_COUNT = 5
export interface RalphState {
active: boolean
sessionId: string
worktreeName: string
worktreeDir: string
worktreeName?: string
worktreeDir?: string
worktreeBranch?: string
workspaceId: string
iteration: number
maxIterations: number
workspaceId?: string
iteration?: number
maxIterations?: number
completionPromise: string | null
startedAt: string
prompt: string
phase: 'coding' | 'auditing'
audit: boolean
startedAt?: string
prompt?: string
phase?: 'coding' | 'auditing'
audit?: boolean
lastAuditResult?: string
errorCount: number
auditCount: number
errorCount?: number
auditCount?: number
terminationReason?: string
completedAt?: string
inPlace?: boolean
modelFailed?: boolean
}
export interface RalphService {
@@ -86,17 +87,17 @@ export function createRalphService(
}
function buildContinuationPrompt(state: RalphState, auditFindings?: string): string {
let systemLine = `Ralph iteration ${state.iteration}`
let systemLine = `Ralph iteration ${state.iteration ?? 0}`
if (state.completionPromise) {
systemLine += ` | To stop: output <promise>${state.completionPromise}</promise> (ONLY when all requirements are met)`
} else if (state.maxIterations > 0) {
} else if ((state.maxIterations ?? 0) > 0) {
systemLine += ` / ${state.maxIterations}`
} else {
systemLine += ` | No completion promise set - loop runs until cancelled`
}
let prompt = `[${systemLine}]\n\n${state.prompt}`
let prompt = `[${systemLine}]\n\n${state.prompt ?? ''}`
if (auditFindings) {
const completionInstruction = state.completionPromise
@@ -109,13 +110,13 @@ export function createRalphService(
}
function buildAuditPrompt(state: RalphState): string {
const taskSummary = state.prompt.length > 200
? `${state.prompt.substring(0, 197)}...`
: state.prompt
const taskSummary = (state.prompt?.length ?? 0) > 200
? `${state.prompt?.substring(0, 197)}...`
: (state.prompt ?? '')
const branchInfo = state.worktreeBranch ? ` (branch: ${state.worktreeBranch})` : ''
return [
`Post-iteration ${state.iteration} code review${branchInfo}.`,
`Post-iteration ${state.iteration ?? 0} code review${branchInfo}.`,
'',
`Task context: ${taskSummary}`,
'',
+471 -2
View File
@@ -476,13 +476,11 @@ describe('RalphService', () => {
errorCount: 2,
auditCount: 1,
terminationReason: undefined,
parentSessionId: 'parent-session-123',
}
ralphService.setState('session-err', state)
const retrieved = ralphService.getActiveState('session-err')
expect(retrieved?.errorCount).toBe(2)
expect(retrieved?.auditCount).toBe(1)
expect(retrieved?.parentSessionId).toBe('parent-session-123')
})
test('state defaults errorCount to 0', () => {
@@ -1169,3 +1167,474 @@ describe('session rotation', () => {
expect(existingState?.sessionId).toBe(sessionId)
})
})
describe('Assistant Error Detection', () => {
let db: Database
let kvService: ReturnType<typeof createKvService>
let ralphService: ReturnType<typeof createRalphService>
const projectId = 'test-project'
beforeEach(() => {
db = createTestDb()
kvService = createKvService(db)
ralphService = createRalphService(kvService, projectId, createMockLogger())
})
afterEach(() => {
db.close()
})
test('detects assistant error in coding phase and triggers error handling', async () => {
const { createRalphEventHandler } = require('../src/hooks/ralph')
const sessionId = 'error-session'
const mockClient = {
session: {
promptAsync: async () => ({ data: undefined, error: undefined }),
create: async () => ({ data: { id: sessionId }, error: undefined }),
messages: async () => ({ data: [] }),
status: async () => ({ data: {} }),
abort: async () => ({ data: undefined, error: undefined }),
},
worktree: {
create: async () => ({ data: { id: 'wt-1', directory: '/tmp/wt', branch: 'main' }, error: undefined }),
remove: async () => ({ data: undefined, error: undefined }),
},
} as any
const mockV2Client = {
session: {
create: async () => ({ data: { id: sessionId }, error: undefined }),
delete: async () => ({ data: undefined, error: undefined }),
promptAsync: async () => ({ data: undefined, error: undefined }),
messages: async () => ({
data: [
{
info: {
role: 'assistant',
error: { name: 'ProviderError', data: { message: 'Model not found' } },
},
parts: [{ type: 'text', text: '' }],
},
],
}),
status: async () => ({ data: {} }),
abort: async () => ({ data: undefined, error: undefined }),
},
} as any
const mockGetConfig = () => ({ ralph: {}, executionModel: undefined, auditorModel: undefined })
const handler = createRalphEventHandler(ralphService, mockClient, mockV2Client, createMockLogger(), mockGetConfig)
const state = {
active: true,
sessionId,
worktreeName: 'test-worktree',
worktreeDir: '/tmp/test-worktree',
worktreeBranch: 'main',
workspaceId: 'wrk-test',
iteration: 1,
maxIterations: 5,
completionPromise: 'DONE',
startedAt: new Date().toISOString(),
prompt: 'Test prompt',
phase: 'coding' as const,
audit: false,
errorCount: 0,
auditCount: 0,
}
ralphService.setState(sessionId, state)
await handler.onEvent({
event: {
type: 'session.idle',
properties: { sessionID: sessionId },
},
})
const updatedState = ralphService.getActiveState(sessionId)
expect(updatedState?.errorCount).toBe(1)
expect(updatedState?.modelFailed).toBe(true)
})
test('detects assistant error in auditing phase and triggers error handling', async () => {
const { createRalphEventHandler } = require('../src/hooks/ralph')
const sessionId = 'audit-error-session'
const mockClient = {
session: {
promptAsync: async () => ({ data: undefined, error: undefined }),
create: async () => ({ data: { id: sessionId }, error: undefined }),
messages: async () => ({ data: [] }),
status: async () => ({ data: {} }),
abort: async () => ({ data: undefined, error: undefined }),
},
worktree: {
create: async () => ({ data: { id: 'wt-1', directory: '/tmp/wt', branch: 'main' }, error: undefined }),
remove: async () => ({ data: undefined, error: undefined }),
},
} as any
const mockV2Client = {
session: {
create: async () => ({ data: { id: sessionId }, error: undefined }),
delete: async () => ({ data: undefined, error: undefined }),
promptAsync: async () => ({ data: undefined, error: undefined }),
messages: async () => ({
data: [
{
info: {
role: 'assistant',
error: { name: 'AuthError', data: { message: 'Authentication failed' } },
},
parts: [{ type: 'text', text: '' }],
},
],
}),
status: async () => ({ data: {} }),
abort: async () => ({ data: undefined, error: undefined }),
},
} as any
const mockGetConfig = () => ({ ralph: {}, executionModel: undefined, auditorModel: undefined })
const handler = createRalphEventHandler(ralphService, mockClient, mockV2Client, createMockLogger(), mockGetConfig)
const state = {
active: true,
sessionId,
worktreeName: 'test-worktree',
worktreeDir: '/tmp/test-worktree',
worktreeBranch: 'main',
workspaceId: 'wrk-test',
iteration: 1,
maxIterations: 5,
completionPromise: null,
startedAt: new Date().toISOString(),
prompt: 'Test prompt',
phase: 'auditing' as const,
audit: true,
errorCount: 0,
auditCount: 0,
}
ralphService.setState(sessionId, state)
await handler.onEvent({
event: {
type: 'session.idle',
properties: { sessionID: sessionId },
},
})
const updatedState = ralphService.getActiveState(sessionId)
expect(updatedState?.errorCount).toBe(1)
expect(updatedState?.modelFailed).toBe(true)
})
test('session.error event with non-abort error sets modelFailed flag', async () => {
const { createRalphEventHandler } = require('../src/hooks/ralph')
const sessionId = 'session-error-test'
const mockClient = {
session: {
promptAsync: async () => ({ data: undefined, error: undefined }),
create: async () => ({ data: { id: sessionId }, error: undefined }),
messages: async () => ({ data: [] }),
status: async () => ({ data: {} }),
abort: async () => ({ data: undefined, error: undefined }),
},
worktree: {
create: async () => ({ data: { id: 'wt-1', directory: '/tmp/wt', branch: 'main' }, error: undefined }),
remove: async () => ({ data: undefined, error: undefined }),
},
} as any
const mockV2Client = {
session: {
create: async () => ({ data: { id: sessionId }, error: undefined }),
delete: async () => ({ data: undefined, error: undefined }),
promptAsync: async () => ({ data: undefined, error: undefined }),
messages: async () => ({ data: [] }),
status: async () => ({ data: {} }),
abort: async () => ({ data: undefined, error: undefined }),
},
} as any
const mockGetConfig = () => ({ ralph: {}, executionModel: undefined, auditorModel: undefined })
const handler = createRalphEventHandler(ralphService, mockClient, mockV2Client, createMockLogger(), mockGetConfig)
const state = {
active: true,
sessionId,
worktreeName: 'test-worktree',
worktreeDir: '/tmp/test-worktree',
worktreeBranch: 'main',
workspaceId: 'wrk-test',
iteration: 1,
maxIterations: 5,
completionPromise: null,
startedAt: new Date().toISOString(),
prompt: 'Test prompt',
phase: 'coding' as const,
audit: false,
errorCount: 0,
auditCount: 0,
}
ralphService.setState(sessionId, state)
await handler.onEvent({
event: {
type: 'session.error',
properties: {
sessionID: sessionId,
error: {
name: 'ProviderError',
data: { message: 'Model not found' },
},
},
},
})
const updatedState = ralphService.getActiveState(sessionId)
expect(updatedState?.modelFailed).toBe(true)
})
test('session.error event with abort error terminates loop immediately', async () => {
const { createRalphEventHandler } = require('../src/hooks/ralph')
const sessionId = 'abort-session'
const mockClient = {
session: {
promptAsync: async () => ({ data: undefined, error: undefined }),
create: async () => ({ data: { id: sessionId }, error: undefined }),
messages: async () => ({ data: [] }),
status: async () => ({ data: {} }),
abort: async () => ({ data: undefined, error: undefined }),
},
worktree: {
create: async () => ({ data: { id: 'wt-1', directory: '/tmp/wt', branch: 'main' }, error: undefined }),
remove: async () => ({ data: undefined, error: undefined }),
},
} as any
const mockV2Client = {
session: {
create: async () => ({ data: { id: sessionId }, error: undefined }),
delete: async () => ({ data: undefined, error: undefined }),
promptAsync: async () => ({ data: undefined, error: undefined }),
messages: async () => ({ data: [] }),
status: async () => ({ data: {} }),
abort: async () => ({ data: undefined, error: undefined }),
},
} as any
const mockGetConfig = () => ({ ralph: {}, executionModel: undefined, auditorModel: undefined })
const handler = createRalphEventHandler(ralphService, mockClient, mockV2Client, createMockLogger(), mockGetConfig)
const state = {
active: true,
sessionId,
worktreeName: 'test-worktree',
worktreeDir: '/tmp/test-worktree',
worktreeBranch: 'main',
workspaceId: 'wrk-test',
iteration: 1,
maxIterations: 5,
completionPromise: null,
startedAt: new Date().toISOString(),
prompt: 'Test prompt',
phase: 'coding' as const,
audit: false,
errorCount: 0,
auditCount: 0,
}
ralphService.setState(sessionId, state)
await handler.onEvent({
event: {
type: 'session.error',
properties: {
sessionID: sessionId,
error: {
name: 'MessageAbortedError',
},
},
},
})
const updatedState = ralphService.getActiveState(sessionId)
expect(updatedState).toBeNull()
})
test('modelFailed flag causes default model usage in coding phase', async () => {
const { createRalphEventHandler } = require('../src/hooks/ralph')
const sessionId = 'model-fail-session'
let modelUsed: string | undefined
const mockClient = {
session: {
promptAsync: async () => ({ data: undefined, error: undefined }),
create: async () => ({ data: { id: sessionId }, error: undefined }),
messages: async () => ({ data: [] }),
status: async () => ({ data: {} }),
abort: async () => ({ data: undefined, error: undefined }),
},
worktree: {
create: async () => ({ data: { id: 'wt-1', directory: '/tmp/wt', branch: 'main' }, error: undefined }),
remove: async () => ({ data: undefined, error: undefined }),
},
} as any
const mockV2Client = {
session: {
create: async () => ({ data: { id: sessionId }, error: undefined }),
delete: async () => ({ data: undefined, error: undefined }),
promptAsync: async (params: any) => {
modelUsed = params.model
return { data: undefined, error: undefined }
},
messages: async () => ({ data: [] }),
status: async () => ({ data: {} }),
abort: async () => ({ data: undefined, error: undefined }),
},
} as any
const mockGetConfig = () => ({
ralph: { model: 'custom/model' },
executionModel: 'execution/model',
auditorModel: undefined,
})
const handler = createRalphEventHandler(ralphService, mockClient, mockV2Client, createMockLogger(), mockGetConfig)
const state = {
active: true,
sessionId,
worktreeName: 'test-worktree',
worktreeDir: '/tmp/test-worktree',
worktreeBranch: 'main',
workspaceId: 'wrk-test',
iteration: 1,
maxIterations: 5,
completionPromise: null,
startedAt: new Date().toISOString(),
prompt: 'Test prompt',
phase: 'coding' as const,
audit: false,
errorCount: 0,
auditCount: 0,
modelFailed: true,
}
ralphService.setState(sessionId, state)
await handler.onEvent({
event: {
type: 'session.idle',
properties: { sessionID: sessionId },
},
})
expect(modelUsed).toBeUndefined()
})
test('three consecutive errors terminate loop', async () => {
const { createRalphEventHandler } = require('../src/hooks/ralph')
const sessionId = 'three-errors-session'
const mockClient = {
session: {
promptAsync: async () => ({ data: undefined, error: undefined }),
create: async () => ({ data: { id: sessionId }, error: undefined }),
messages: async () => ({ data: [] }),
status: async () => ({ data: {} }),
abort: async () => ({ data: undefined, error: undefined }),
},
worktree: {
create: async () => ({ data: { id: 'wt-1', directory: '/tmp/wt', branch: 'main' }, error: undefined }),
remove: async () => ({ data: undefined, error: undefined }),
},
} as any
const mockV2Client = {
session: {
create: async () => ({ data: { id: sessionId }, error: undefined }),
delete: async () => ({ data: undefined, error: undefined }),
promptAsync: async () => ({ data: undefined, error: undefined }),
messages: async () => ({
data: [
{
info: {
role: 'assistant',
error: { name: 'ProviderError', data: { message: 'Model not found' } },
},
parts: [{ type: 'text', text: '' }],
},
],
}),
status: async () => ({ data: {} }),
abort: async () => ({ data: undefined, error: undefined }),
},
} as any
const mockGetConfig = () => ({ ralph: {}, executionModel: undefined, auditorModel: undefined })
const handler = createRalphEventHandler(ralphService, mockClient, mockV2Client, createMockLogger(), mockGetConfig)
const state = {
active: true,
sessionId,
worktreeName: 'test-worktree',
worktreeDir: '/tmp/test-worktree',
worktreeBranch: 'main',
workspaceId: 'wrk-test',
iteration: 1,
maxIterations: 5,
completionPromise: 'DONE',
startedAt: new Date().toISOString(),
prompt: 'Test prompt',
phase: 'coding' as const,
audit: false,
errorCount: 0,
auditCount: 0,
}
ralphService.setState(sessionId, state)
await handler.onEvent({
event: {
type: 'session.idle',
properties: { sessionID: sessionId },
},
})
let stateAfterSecondError = ralphService.getActiveState(sessionId)
expect(stateAfterSecondError?.errorCount).toBe(1)
await handler.onEvent({
event: {
type: 'session.idle',
properties: { sessionID: sessionId },
},
})
let stateAfterThirdError = ralphService.getActiveState(sessionId)
expect(stateAfterThirdError?.errorCount).toBe(2)
await handler.onEvent({
event: {
type: 'session.idle',
properties: { sessionID: sessionId },
},
})
const finalState = ralphService.getActiveState(sessionId)
expect(finalState).toBeNull()
const terminatedState = ralphService.getAnyState(sessionId)
expect(terminatedState?.active).toBe(false)
expect(terminatedState?.terminationReason).toContain('error_max_retries')
})
})