diff --git a/.env.example b/.env.example index dc60c65d..b0ecc49e 100644 --- a/.env.example +++ b/.env.example @@ -59,7 +59,6 @@ WORKSPACE_PATH=./workspace # ============================================ PROCESS_START_WAIT_MS=2000 PROCESS_VERIFY_WAIT_MS=1000 -HEALTH_CHECK_INTERVAL_MS=5000 HEALTH_CHECK_TIMEOUT_MS=30000 # ============================================ diff --git a/backend/src/routes/health.ts b/backend/src/routes/health.ts index 546af95a..2a239dd8 100644 --- a/backend/src/routes/health.ts +++ b/backend/src/routes/health.ts @@ -70,9 +70,9 @@ export function createHealthRoutes(db: Database, openCodeSupervisor?: OpenCodeSu const opencodeManagerVersion = await opencodeManagerVersionPromise const dbCheck = db.prepare('SELECT 1').get() const lifecycle = openCodeSupervisor - ? await openCodeSupervisor.checkNow('api_probe') + ? openCodeSupervisor.getStatus() : null - const opencodeHealthy = lifecycle?.healthy ?? await opencodeServerManager.checkHealth() + const opencodeHealthy = await opencodeServerManager.checkHealth() const startupError = lifecycle?.lastError ?? opencodeServerManager.getLastStartupError() const status = lifecycle?.state === 'recovering' @@ -117,9 +117,9 @@ export function createHealthRoutes(db: Database, openCodeSupervisor?: OpenCodeSu app.get('/processes', async (c) => { try { const lifecycle = openCodeSupervisor - ? await openCodeSupervisor.checkNow('api_probe') + ? openCodeSupervisor.getStatus() : null - const opencodeHealthy = lifecycle?.healthy ?? await opencodeServerManager.checkHealth() + const opencodeHealthy = await opencodeServerManager.checkHealth() return c.json({ opencode: { diff --git a/backend/src/services/opencode-single-server.ts b/backend/src/services/opencode-single-server.ts index 964269b5..95223cab 100644 --- a/backend/src/services/opencode-single-server.ts +++ b/backend/src/services/opencode-single-server.ts @@ -31,7 +31,6 @@ import { mkdirSafe } from '../utils/fs-safe' const MIN_OPENCODE_VERSION = '1.0.137' const MAX_STDERR_SIZE = 10240 -const HEALTH_CHECK_TIMEOUT_MS = 3000 const PLUGIN_INSTALL_TIMEOUT_MS = 120000 const PROCESS_EXIT_GRACE_MS = 2000 const PROCESS_EXIT_POLL_MS = 50 @@ -734,8 +733,8 @@ class OpenCodeServerManager { try { const response = await this.openCodeClient.forward({ method: 'GET', - path: '/doc', - signal: AbortSignal.timeout(HEALTH_CHECK_TIMEOUT_MS), + path: '/global/health', + signal: AbortSignal.timeout(ENV.TIMEOUTS.HEALTH_CHECK_TIMEOUT_MS), }) return response.ok } catch { diff --git a/backend/src/services/opencode-supervisor.ts b/backend/src/services/opencode-supervisor.ts index fb0404e6..cf0213e0 100644 --- a/backend/src/services/opencode-supervisor.ts +++ b/backend/src/services/opencode-supervisor.ts @@ -30,7 +30,6 @@ export type OpenCodeRecoveryAction = (typeof OPENCODE_RECOVERY_ACTIONS)[number] export type OpenCodeOperationReason = | 'backend_startup' | 'health_poll' - | 'api_probe' | 'settings_restart' | 'settings_reload' | 'manual' diff --git a/backend/test/routes/health.test.ts b/backend/test/routes/health.test.ts index 84d1f164..12c3fb46 100644 --- a/backend/test/routes/health.test.ts +++ b/backend/test/routes/health.test.ts @@ -24,6 +24,7 @@ vi.mock('bun:sqlite', () => ({ import { opencodeServerManager } from '../../src/services/opencode-single-server' import { createHealthRoutes } from '../../src/routes/health' +import type { OpenCodeSupervisor } from '../../src/services/opencode-supervisor' describe('Health Routes', () => { let healthApp: ReturnType @@ -113,5 +114,38 @@ describe('Health Routes', () => { expect(json.status).toBe('unhealthy') expect(json.error).toBe('Database error') }) + + it('reads the supervisor status without triggering recovery', async () => { + mockDb.prepare().get.mockReturnValue({ 1: 1 }) + ;(opencodeServerManager.checkHealth as ReturnType).mockResolvedValue(true) + const supervisor = { + getStatus: vi.fn(() => ({ + state: 'healthy', + healthy: true, + port: 5551, + version: '1.0.0', + minVersion: '1.0.137', + versionSupported: true, + lastError: null, + activeRecoveryAction: null, + attemptedRecoveryActions: [], + nextRecoveryAction: null, + failureCount: 0, + watching: true, + updatedAt: new Date().toISOString(), + })), + checkNow: vi.fn(), + } as unknown as OpenCodeSupervisor + healthApp = createHealthRoutes(mockDb, supervisor) + + const res = await healthApp.fetch(new Request('http://localhost/')) + const processesRes = await healthApp.fetch(new Request('http://localhost/processes')) + + expect(res.status).toBe(200) + expect(processesRes.status).toBe(200) + expect(supervisor.getStatus).toHaveBeenCalledTimes(2) + expect(supervisor.checkNow).not.toHaveBeenCalled() + expect(opencodeServerManager.checkHealth).toHaveBeenCalledTimes(2) + }) }) }) diff --git a/backend/test/services/opencode-single-server.test.ts b/backend/test/services/opencode-single-server.test.ts index 9caf2da2..e63a5abc 100644 --- a/backend/test/services/opencode-single-server.test.ts +++ b/backend/test/services/opencode-single-server.test.ts @@ -35,6 +35,7 @@ vi.mock('@opencode-manager/shared/config/env', () => ({ AUTH: { TRUSTED_ORIGINS: 'http://localhost:5173', SECRET: 'test-secret-for-encryption-key-32c' }, WORKSPACE: { BASE_PATH: '/test/workspace', REPOS_DIR: 'repos', CONFIG_DIR: 'config', AUTH_FILE: 'auth.json' }, OPENCODE: { PORT: 5551, HOST: '127.0.0.1', SERVER_PASSWORD: '', SERVER_USERNAME: 'opencode', PUBLIC_URL: '' }, + TIMEOUTS: { HEALTH_CHECK_TIMEOUT_MS: 50 }, DATABASE: { PATH: ':memory:' }, FILE_LIMITS: { MAX_SIZE_BYTES: 1024 * 1024, @@ -394,6 +395,23 @@ describe('OpenCodeServerManager - reloadConfig', () => { }) describe('OpenCodeServerManager - checkHealth', () => { + it('uses the OpenCode liveness endpoint', async () => { + const { opencodeServerManager } = await import('../../src/services/opencode-single-server') + const { createStubOpenCodeClient } = await import('../helpers/stub-opencode-client') + const forward = vi.fn(async () => new Response(JSON.stringify({ healthy: true }), { status: 200 })) + const timeout = vi.spyOn(AbortSignal, 'timeout') + opencodeServerManager.setOpenCodeClient(createStubOpenCodeClient({ forward })) + + await opencodeServerManager.checkHealth() + + expect(forward).toHaveBeenCalledWith(expect.objectContaining({ + method: 'GET', + path: '/global/health', + signal: expect.any(AbortSignal), + })) + expect(timeout).toHaveBeenCalledWith(ENV.TIMEOUTS.HEALTH_CHECK_TIMEOUT_MS) + }) + it('returns false when the upstream times out and aborts the upstream fetch', async () => { const { opencodeServerManager } = await import('../../src/services/opencode-single-server') const { createStubOpenCodeClient } = await import('../helpers/stub-opencode-client') diff --git a/backend/test/services/opencode-supervisor.test.ts b/backend/test/services/opencode-supervisor.test.ts index 03c96722..02d829a2 100644 --- a/backend/test/services/opencode-supervisor.test.ts +++ b/backend/test/services/opencode-supervisor.test.ts @@ -132,7 +132,7 @@ describe('OpenCodeSupervisor', () => { manager.checkHealth.mockResolvedValueOnce(false) - const status = await supervisor.checkNow('api_probe') + const status = await supervisor.checkNow('manual') expect(status.state).toBe('unhealthy') expect(status.failureCount).toBe(1) @@ -152,7 +152,7 @@ describe('OpenCodeSupervisor', () => { .mockResolvedValueOnce(false) .mockResolvedValueOnce(true) - const status = await supervisor.checkNow('api_probe') + const status = await supervisor.checkNow('manual') expect(status.healthy).toBe(true) expect(ensureDirectoryExists).toHaveBeenCalled() @@ -167,7 +167,7 @@ describe('OpenCodeSupervisor', () => { manager.isOperationInProgress.mockReturnValue(true) - await supervisor.checkNow('api_probe') + await supervisor.checkNow('manual') expect(manager.checkHealth).not.toHaveBeenCalled() }) diff --git a/docker-compose.yml b/docker-compose.yml index 1902cef0..9abe6a52 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -24,7 +24,6 @@ services: - WORKSPACE_PATH=/workspace - PROCESS_START_WAIT_MS=2000 - PROCESS_VERIFY_WAIT_MS=1000 - - HEALTH_CHECK_INTERVAL_MS=5000 - HEALTH_CHECK_TIMEOUT_MS=30000 - MAX_FILE_SIZE_MB=50 - MAX_UPLOAD_SIZE_MB=50 diff --git a/docs/configuration/docker.md b/docs/configuration/docker.md index 0121e0c8..e64bf1a0 100644 --- a/docs/configuration/docker.md +++ b/docs/configuration/docker.md @@ -56,7 +56,6 @@ services: - WORKSPACE_PATH=/workspace - PROCESS_START_WAIT_MS=2000 - PROCESS_VERIFY_WAIT_MS=1000 - - HEALTH_CHECK_INTERVAL_MS=5000 - HEALTH_CHECK_TIMEOUT_MS=30000 - MAX_FILE_SIZE_MB=50 - MAX_UPLOAD_SIZE_MB=50 diff --git a/docs/configuration/environment.md b/docs/configuration/environment.md index 93a44827..b7f52398 100644 --- a/docs/configuration/environment.md +++ b/docs/configuration/environment.md @@ -114,8 +114,7 @@ When configured, users can enable push notifications in Settings → Notificatio |----------|-------------|---------| | `PROCESS_START_WAIT_MS` | Wait time for OpenCode process to start | `2000` | | `PROCESS_VERIFY_WAIT_MS` | Wait time for process health verification | `1000` | -| `HEALTH_CHECK_INTERVAL_MS` | Health check polling interval | `5000` | -| `HEALTH_CHECK_TIMEOUT_MS` | Health check timeout | `30000` | +| `HEALTH_CHECK_TIMEOUT_MS` | OpenCode liveness probe timeout | `30000` | ## File Limits diff --git a/shared/src/config/defaults.ts b/shared/src/config/defaults.ts index 60458c95..5ab6f1fe 100644 --- a/shared/src/config/defaults.ts +++ b/shared/src/config/defaults.ts @@ -34,7 +34,6 @@ export const DEFAULTS = { TIMEOUTS: { PROCESS_START_WAIT_MS: 2000, PROCESS_VERIFY_WAIT_MS: 1000, - HEALTH_CHECK_INTERVAL_MS: 5000, HEALTH_CHECK_TIMEOUT_MS: 30000, }, diff --git a/shared/src/config/env.ts b/shared/src/config/env.ts index 47c657c4..cefcc636 100644 --- a/shared/src/config/env.ts +++ b/shared/src/config/env.ts @@ -90,7 +90,6 @@ export const ENV = { TIMEOUTS: { PROCESS_START_WAIT_MS: getEnvNumber('PROCESS_START_WAIT_MS', DEFAULTS.TIMEOUTS.PROCESS_START_WAIT_MS), PROCESS_VERIFY_WAIT_MS: getEnvNumber('PROCESS_VERIFY_WAIT_MS', DEFAULTS.TIMEOUTS.PROCESS_VERIFY_WAIT_MS), - HEALTH_CHECK_INTERVAL_MS: getEnvNumber('HEALTH_CHECK_INTERVAL_MS', DEFAULTS.TIMEOUTS.HEALTH_CHECK_INTERVAL_MS), HEALTH_CHECK_TIMEOUT_MS: getEnvNumber('HEALTH_CHECK_TIMEOUT_MS', DEFAULTS.TIMEOUTS.HEALTH_CHECK_TIMEOUT_MS), },