diff --git a/backend/src/services/repo.ts b/backend/src/services/repo.ts index ed36f9c5..d409604e 100644 --- a/backend/src/services/repo.ts +++ b/backend/src/services/repo.ts @@ -37,6 +37,9 @@ export class RepoInputError extends Error { } } +const HTTP_AUTH_STATUS_PATTERN = /\b(?:http|status|error):?\s+401\b/ +const HTTP_NOT_FOUND_STATUS_PATTERN = /\b(?:http|status|error):?\s+404\b/ + function notFoundOrDeniedMessage(repoUrl: string): string { return `Repository not found or access denied: ${repoUrl}. Check the URL and add credentials in Settings > Git Credentials if it is private.` } @@ -52,7 +55,26 @@ function enhanceCloneError(repoUrl: string, originalMessage: string): RepoInputE return new RepoInputError(`Repository not reachable: ${repoUrl}. Check the URL and your network connection.`) } - if (message.includes('repository not found') || message.includes('not found') || message.includes('does not exist') || message.includes('404')) { + if ( + message.includes('authentication failed') || + message.includes('could not authenticate') || + message.includes('invalid credentials') || + message.includes('invalid username or password') || + message.includes('could not read username') || + message.includes('could not read password') || + HTTP_AUTH_STATUS_PATTERN.test(message) + ) { + return isGitHubHttpsUrl(repoUrl) + ? new RepoInputError(notFoundOrDeniedMessage(repoUrl)) + : new RepoInputError(`Authentication failed for ${repoUrl}. Check your credentials in Settings > Git Credentials.`) + } + + if ( + message.includes('repository not found') || + message.includes('not found') || + message.includes('does not exist') || + HTTP_NOT_FOUND_STATUS_PATTERN.test(message) + ) { return new RepoInputError(notFoundOrDeniedMessage(repoUrl)) } @@ -62,12 +84,6 @@ function enhanceCloneError(repoUrl: string, originalMessage: string): RepoInputE : new RepoInputError(notFoundOrDeniedMessage(repoUrl)) } - if (message.includes('authentication failed') || message.includes('could not authenticate') || message.includes('invalid credentials') || message.includes('could not read username')) { - return isGitHubHttpsUrl(repoUrl) - ? new RepoInputError(notFoundOrDeniedMessage(repoUrl)) - : new RepoInputError(`Authentication failed for ${repoUrl}. Check your credentials in Settings > Git Credentials.`) - } - if (message.includes('not a git repository') || message.includes('fatal:')) { return new RepoInputError(notFoundOrDeniedMessage(repoUrl)) } diff --git a/backend/test/services/opencode-single-server.test.ts b/backend/test/services/opencode-single-server.test.ts index 7aa36fec..d09288fc 100644 --- a/backend/test/services/opencode-single-server.test.ts +++ b/backend/test/services/opencode-single-server.test.ts @@ -17,6 +17,15 @@ const spawnMock = vi.hoisted(() => vi.fn(() => ({ const spawnSyncMock = vi.hoisted(() => vi.fn()) +const processKillMock = vi.hoisted(() => vi.fn((_pid: number, signal?: number | NodeJS.Signals) => { + if (signal === 0) { + const error = new Error('kill ESRCH') as NodeJS.ErrnoException + error.code = 'ESRCH' + throw error + } + return true +})) + const readFileSyncMock = vi.hoisted(() => vi.fn()) vi.mock('bun:sqlite', () => ({ @@ -186,6 +195,16 @@ const routeVersionProbeThroughExecSyncStub = () => { beforeEach(routeVersionProbeThroughExecSyncStub) +const originalProcessKill = process.kill + +beforeEach(() => { + process.kill = processKillMock as unknown as typeof process.kill +}) + +afterEach(() => { + process.kill = originalProcessKill +}) + // Reset singleton before any tests run to clear any polluted state from previous test files beforeAll(async () => { const { OpenCodeServerManager } = await import('../../src/services/opencode-single-server') @@ -2823,6 +2842,7 @@ describe('OpenCodeServerManager - server auth', () => { await manager.start() await manager.stop() + expect(processKillMock).toHaveBeenCalledWith(1234, 'SIGTERM') const rmMock = fs.rm as unknown as ReturnType expect(rmMock).toHaveBeenCalledWith('/test/workspace/.opencode/state/opencode-server-child.json', { force: true }) } finally { @@ -2869,6 +2889,7 @@ describe('OpenCodeServerManager - server auth', () => { await secondManager.start() expect(spawnMock).toHaveBeenCalledTimes(2) + expect(processKillMock).toHaveBeenCalledWith(1234, 'SIGKILL') } finally { readFileMock.mockReset() Object.defineProperty(ENV.SERVER, 'NODE_ENV', { value: originalNodeEnv, configurable: true, writable: true }) diff --git a/backend/test/services/repo-git.test.ts b/backend/test/services/repo-git.test.ts index 5948bad6..93f154de 100644 --- a/backend/test/services/repo-git.test.ts +++ b/backend/test/services/repo-git.test.ts @@ -88,6 +88,14 @@ describe('repo service real git', () => { }) } + function createTlsCloneGitAuth(): GitAuthService { + return createGitAuthService({ + GIT_TERMINAL_PROMPT: '0', + GIT_CONFIG_NOSYSTEM: '1', + GIT_CONFIG_GLOBAL: '/dev/null', + }) + } + describe('initLocalRepo', () => { it('initializes a relative repo and marks it ready', async () => { const { initLocalRepo } = await import('../../src/services/repo') @@ -756,7 +764,7 @@ describe('repo service real git', () => { const url = `https://127.0.0.1:${port}/404/repo-missing.git` process.env.GIT_SSL_NO_VERIFY = 'true' try { - await expect(cloneRepo(db, gitAuth, url, { directoryName: name })).rejects.toThrow(/Repository not found/) + await expect(cloneRepo(db, createTlsCloneGitAuth(), url, { directoryName: name })).rejects.toThrow(/Repository not found/) } finally { delete process.env.GIT_SSL_NO_VERIFY } @@ -773,7 +781,24 @@ describe('repo service real git', () => { const url = `https://user:pass@127.0.0.1:${port}/repo-auth.git` process.env.GIT_SSL_NO_VERIFY = 'true' try { - await expect(cloneRepo(db, gitAuth, url, { directoryName: name })).rejects.toThrow(/Authentication failed/) + await expect(cloneRepo(db, createTlsCloneGitAuth(), url, { directoryName: name })).rejects.toThrow(/Authentication failed/) + } finally { + delete process.env.GIT_SSL_NO_VERIFY + } + }) + + expect(getRepoByLocalPath(db, name)).toBeNull() + }) + + it('maps an authentication clone failure whose URL contains a 404 sequence', async () => { + const { cloneRepo } = await import('../../src/services/repo') + const name = uniqueName('clone-auth-404') + + await withTlsServer(401, async (port) => { + const url = `https://user:pass@127.0.0.1:${port}/404/repo-auth.git` + process.env.GIT_SSL_NO_VERIFY = 'true' + try { + await expect(cloneRepo(db, createTlsCloneGitAuth(), url, { directoryName: name })).rejects.toThrow(/Authentication failed/) } finally { delete process.env.GIT_SSL_NO_VERIFY }