mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Add Ralph status dialog and session management improvements (#171)
This commit is contained in:
1 parent
27352ea5cf
commit
86613b1ea4
12 files changed
+1132
-65
No files matched your search
@@ -8,6 +8,7 @@ import { resolveProjectId } from '../services/project-id-resolver'
|
||||
import { getRepoById } from '../db/queries'
|
||||
import { getWorkspacePath, getConfigPath } from '@opencode-manager/shared/config/env'
|
||||
import { parseJsonc } from '@opencode-manager/shared/utils'
|
||||
import { OPENCODE_SERVER_URL } from '../services/proxy'
|
||||
import {
|
||||
CreateMemoryRequestSchema,
|
||||
UpdateMemoryRequestSchema,
|
||||
@@ -16,7 +17,9 @@ import {
|
||||
PluginConfigSchema,
|
||||
CreateKvEntryRequestSchema,
|
||||
UpdateKvEntryRequestSchema,
|
||||
RalphStateSchema,
|
||||
type PluginConfig,
|
||||
type RalphState,
|
||||
} from '@opencode-manager/shared/schemas'
|
||||
|
||||
function resolveMemoryDataDir(): string {
|
||||
@@ -525,6 +528,51 @@ export function createMemoryRoutes(db: Database): Hono {
|
||||
}
|
||||
})
|
||||
|
||||
app.get('/ralph/status', async (c) => {
|
||||
const repoIdParam = c.req.query('repoId')
|
||||
|
||||
if (!repoIdParam) {
|
||||
return c.json({ error: 'Missing repoId' }, 400)
|
||||
}
|
||||
|
||||
const repoId = parseInt(repoIdParam, 10)
|
||||
|
||||
if (isNaN(repoId)) {
|
||||
return c.json({ error: 'Invalid repoId' }, 400)
|
||||
}
|
||||
|
||||
try {
|
||||
const repo = getRepoById(db, repoId)
|
||||
|
||||
if (!repo) {
|
||||
return c.json({ loops: [], projectId: null })
|
||||
}
|
||||
|
||||
const projectId = await resolveProjectId(repo.fullPath)
|
||||
|
||||
if (!projectId) {
|
||||
return c.json({ loops: [], projectId: null })
|
||||
}
|
||||
|
||||
const entries = pluginMemory.listKv(projectId, 'ralph:')
|
||||
const loops = entries
|
||||
.map(e => e.data)
|
||||
.filter((data): data is Record<string, unknown> =>
|
||||
data !== null && typeof data === 'object' && 'active' in data
|
||||
)
|
||||
.map(data => {
|
||||
const result = RalphStateSchema.safeParse(data)
|
||||
return result.success ? result.data : null
|
||||
})
|
||||
.filter((loop): loop is RalphState => loop !== null)
|
||||
|
||||
return c.json({ loops, projectId })
|
||||
} catch (error) {
|
||||
logger.error('Failed to get Ralph status:', error)
|
||||
return c.json({ error: 'Failed to get Ralph status' }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/ralph/cancel', async (c) => {
|
||||
try {
|
||||
const body = await c.req.json()
|
||||
@@ -552,9 +600,16 @@ export function createMemoryRoutes(db: Database): Hono {
|
||||
return c.json({ cancelled: false })
|
||||
}
|
||||
|
||||
const state = kvEntry.data as { active?: boolean; worktreeName?: string } | undefined
|
||||
const result = RalphStateSchema.safeParse(kvEntry.data)
|
||||
|
||||
if (!state?.active) {
|
||||
if (!result.success) {
|
||||
logger.warn('Failed to parse Ralph state for cancel:', result.error)
|
||||
return c.json({ cancelled: false })
|
||||
}
|
||||
|
||||
const state = result.data
|
||||
|
||||
if (!state.active) {
|
||||
return c.json({ cancelled: false })
|
||||
}
|
||||
|
||||
@@ -567,6 +622,14 @@ export function createMemoryRoutes(db: Database): Hono {
|
||||
|
||||
pluginMemory.setKv(projectId, `ralph:${sessionId}`, updatedState)
|
||||
|
||||
try {
|
||||
const abortUrl = new URL(`${OPENCODE_SERVER_URL}/session/${sessionId}/abort`)
|
||||
abortUrl.searchParams.set('directory', repo.fullPath)
|
||||
await fetch(abortUrl.toString(), { method: 'POST' })
|
||||
} catch {
|
||||
// Session may already be idle
|
||||
}
|
||||
|
||||
return c.json({ cancelled: true, worktreeName: state.worktreeName })
|
||||
} catch (error) {
|
||||
logger.error('Failed to cancel Ralph loop:', error)
|
||||
|
||||
@@ -0,0 +1,248 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
vi.mock('bun:sqlite', () => ({
|
||||
Database: vi.fn().mockImplementation(() => ({})),
|
||||
}))
|
||||
|
||||
const mockListKv = vi.fn()
|
||||
|
||||
vi.mock('../../src/utils/logger', () => ({
|
||||
logger: {
|
||||
info: vi.fn(),
|
||||
error: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('../../src/services/project-id-resolver', () => ({
|
||||
resolveProjectId: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('../../src/services/plugin-memory', () => ({
|
||||
PluginMemoryService: vi.fn().mockImplementation(() => ({
|
||||
listKv: mockListKv,
|
||||
})),
|
||||
}))
|
||||
|
||||
vi.mock('../../src/db/queries', () => ({
|
||||
getRepoById: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@opencode-manager/shared/config/env', () => ({
|
||||
getWorkspacePath: vi.fn(() => '/tmp/test-workspace'),
|
||||
getReposPath: vi.fn(() => '/tmp/test-repos'),
|
||||
getOpenCodeConfigFilePath: vi.fn(() => '/tmp/test-workspace/.config/opencode.json'),
|
||||
getAgentsMdPath: vi.fn(() => '/tmp/test-workspace/AGENTS.md'),
|
||||
getDatabasePath: vi.fn(() => ':memory:'),
|
||||
getConfigPath: vi.fn(() => '/tmp/test-workspace/config'),
|
||||
ENV: {
|
||||
SERVER: { PORT: 5003, HOST: '0.0.0.0', NODE_ENV: 'test' },
|
||||
AUTH: { TRUSTED_ORIGINS: 'http://localhost:5173', SECRET: 'test-secret-for-encryption-key-32c' },
|
||||
WORKSPACE: { BASE_PATH: '/tmp/test-workspace', REPOS_DIR: 'repos', CONFIG_DIR: 'config', AUTH_FILE: 'auth.json' },
|
||||
OPENCODE: { PORT: 5551, HOST: '127.0.0.1' },
|
||||
DATABASE: { PATH: ':memory:' },
|
||||
FILE_LIMITS: {
|
||||
MAX_SIZE_BYTES: 1024 * 1024,
|
||||
MAX_UPLOAD_SIZE_BYTES: 10 * 1024 * 1024,
|
||||
},
|
||||
},
|
||||
FILE_LIMITS: {
|
||||
MAX_SIZE_BYTES: 1024 * 1024,
|
||||
MAX_UPLOAD_SIZE_BYTES: 10 * 1024 * 1024,
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@opencode-manager/shared/utils', () => ({
|
||||
parseJsonc: vi.fn(),
|
||||
}))
|
||||
|
||||
import { createMemoryRoutes } from '../../src/routes/memory'
|
||||
import { resolveProjectId } from '../../src/services/project-id-resolver'
|
||||
import { getRepoById } from '../../src/db/queries'
|
||||
|
||||
const mockResolveProjectId = resolveProjectId as ReturnType<typeof vi.fn>
|
||||
const mockGetRepoById = getRepoById as ReturnType<typeof vi.fn>
|
||||
|
||||
describe('Memory Routes - Ralph Status', () => {
|
||||
let memoryApp: ReturnType<typeof createMemoryRoutes>
|
||||
let testDb: any
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockResolveProjectId.mockReset()
|
||||
mockListKv.mockReset()
|
||||
mockGetRepoById.mockReset()
|
||||
|
||||
testDb = {} as any
|
||||
memoryApp = createMemoryRoutes(testDb)
|
||||
})
|
||||
|
||||
describe('GET /ralph/status', () => {
|
||||
it('should return 400 when repoId query param is missing', async () => {
|
||||
const req = new Request('http://localhost/ralph/status')
|
||||
const res = await memoryApp.fetch(req)
|
||||
|
||||
expect(res.status).toBe(400)
|
||||
const json = await res.json() as Record<string, unknown>
|
||||
expect(json.error).toBe('Missing repoId')
|
||||
})
|
||||
|
||||
it('should return 400 when repoId is not a valid number', async () => {
|
||||
const req = new Request('http://localhost/ralph/status?repoId=abc')
|
||||
const res = await memoryApp.fetch(req)
|
||||
|
||||
expect(res.status).toBe(400)
|
||||
const json = await res.json() as Record<string, unknown>
|
||||
expect(json.error).toBe('Invalid repoId')
|
||||
})
|
||||
|
||||
it('should return 200 with empty loops when repo is not found in DB', async () => {
|
||||
mockGetRepoById.mockReturnValue(null)
|
||||
|
||||
const req = new Request('http://localhost/ralph/status?repoId=1')
|
||||
const res = await memoryApp.fetch(req)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json() as Record<string, unknown>
|
||||
expect(json.loops).toEqual([])
|
||||
})
|
||||
|
||||
it('should return 200 with empty loops when resolveProjectId returns null', async () => {
|
||||
mockGetRepoById.mockReturnValue({
|
||||
id: 1,
|
||||
fullPath: '/tmp/test-repo',
|
||||
repoUrl: 'https://github.com/test/repo.git',
|
||||
localPath: 'test-repo',
|
||||
sourcePath: '',
|
||||
branch: 'main',
|
||||
currentBranch: 'main',
|
||||
cloneStatus: 'ready',
|
||||
isWorktree: false,
|
||||
openCodeConfigName: 'default',
|
||||
})
|
||||
mockResolveProjectId.mockResolvedValue(null)
|
||||
|
||||
const req = new Request('http://localhost/ralph/status?repoId=1')
|
||||
const res = await memoryApp.fetch(req)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json() as Record<string, unknown>
|
||||
expect(json.loops).toEqual([])
|
||||
})
|
||||
|
||||
it('should return 200 with loops array for a valid repo with active loops', async () => {
|
||||
const mockRepo = {
|
||||
id: 1,
|
||||
fullPath: '/tmp/test-repo',
|
||||
repoUrl: 'https://github.com/test/repo.git',
|
||||
localPath: 'test-repo',
|
||||
sourcePath: '',
|
||||
branch: 'main',
|
||||
currentBranch: 'main',
|
||||
cloneStatus: 'ready',
|
||||
isWorktree: false,
|
||||
openCodeConfigName: 'default',
|
||||
}
|
||||
mockGetRepoById.mockReturnValue(mockRepo)
|
||||
mockResolveProjectId.mockResolvedValue('test-project-id')
|
||||
mockListKv.mockReturnValue([
|
||||
{
|
||||
key: 'ralph:session-123',
|
||||
data: {
|
||||
active: true,
|
||||
sessionId: 'session-123',
|
||||
worktreeName: 'test-worktree',
|
||||
worktreeDir: '/tmp/worktrees/test',
|
||||
iteration: 1,
|
||||
maxIterations: 10,
|
||||
startedAt: '2024-01-01T00:00:00.000Z',
|
||||
prompt: 'Test prompt',
|
||||
phase: 'coding',
|
||||
audit: false,
|
||||
errorCount: 0,
|
||||
auditCount: 0,
|
||||
},
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now(),
|
||||
expiresAt: Date.now() + 86400000,
|
||||
},
|
||||
])
|
||||
|
||||
const req = new Request('http://localhost/ralph/status?repoId=1')
|
||||
const res = await memoryApp.fetch(req)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json() as Record<string, unknown>
|
||||
expect(json.loops).toHaveLength(1)
|
||||
expect((json.loops as Array<Record<string, unknown>>)[0]?.active).toBe(true)
|
||||
})
|
||||
|
||||
it('should filter out KV entries that do not have an active field', async () => {
|
||||
const mockRepo = {
|
||||
id: 1,
|
||||
fullPath: '/tmp/test-repo',
|
||||
repoUrl: 'https://github.com/test/repo.git',
|
||||
localPath: 'test-repo',
|
||||
sourcePath: '',
|
||||
branch: 'main',
|
||||
currentBranch: 'main',
|
||||
cloneStatus: 'ready',
|
||||
isWorktree: false,
|
||||
openCodeConfigName: 'default',
|
||||
}
|
||||
mockGetRepoById.mockReturnValue(mockRepo)
|
||||
mockResolveProjectId.mockResolvedValue('test-project-id')
|
||||
mockListKv.mockReturnValue([
|
||||
{
|
||||
key: 'ralph:session-123',
|
||||
data: { active: true, sessionId: 'session-123' },
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now(),
|
||||
expiresAt: Date.now() + 86400000,
|
||||
},
|
||||
{
|
||||
key: 'ralph:session-456',
|
||||
data: null,
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now(),
|
||||
expiresAt: Date.now() + 86400000,
|
||||
},
|
||||
{
|
||||
key: 'ralph:session-789',
|
||||
data: 'string-data',
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now(),
|
||||
expiresAt: Date.now() + 86400000,
|
||||
},
|
||||
{
|
||||
key: 'ralph:session-abc',
|
||||
data: { sessionId: 'session-abc' },
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now(),
|
||||
expiresAt: Date.now() + 86400000,
|
||||
},
|
||||
])
|
||||
|
||||
const req = new Request('http://localhost/ralph/status?repoId=1')
|
||||
const res = await memoryApp.fetch(req)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json() as Record<string, unknown>
|
||||
expect(json.loops).toHaveLength(1)
|
||||
expect((json.loops as Array<Record<string, unknown>>)[0]?.active).toBe(true)
|
||||
})
|
||||
|
||||
it('should return 500 when an unexpected error is thrown', async () => {
|
||||
mockGetRepoById.mockImplementation(() => {
|
||||
throw new Error('Database error')
|
||||
})
|
||||
|
||||
const req = new Request('http://localhost/ralph/status?repoId=1')
|
||||
const res = await memoryApp.fetch(req)
|
||||
|
||||
expect(res.status).toBe(500)
|
||||
const json = await res.json() as Record<string, unknown>
|
||||
expect(json.error).toBe('Failed to get Ralph status')
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user