diff --git a/src/search-engines.ts b/src/search-engines.ts index 26fc53eb..b3e90f48 100644 --- a/src/search-engines.ts +++ b/src/search-engines.ts @@ -14,10 +14,10 @@ * whose name matches (no file path carries it, so rg cannot see it; fd and * the walk both report it). * - * Repeated runtime failures disable that engine for the rest of the process - * (a broken binary must not slow every later search); a single bad query or a - * timeout only degrades THAT one search, so the next one retries the engine. - * Hooks are swappable for tests (setEngineHooks). + * Repeated consecutive failures disable that engine (a broken binary must not + * slow every later search); a single bad query or a timeout only degrades THAT + * one search, and a timeout breaks the failure streak. A success recovers the + * engine. Hooks are swappable for tests (setEngineHooks). */ import { spawn, type ChildProcess } from 'node:child_process' import { createInterface } from 'node:readline' @@ -513,7 +513,7 @@ export type EngineSpawner = ( /** Child spawn behind runChild — injectable so tests can drive the real * orchestration with scripted children (event order, stream timing). */ let spawner: EngineSpawner = spawn -/** Engines failed at runtime: skipped for the rest of this process. */ +/** Engines disabled after repeated failures; cleared on the next success. */ const broken = new Set() /** Consecutive runtime failures per engine. A single bad query (or a timeout) * is not evidence of a dead binary, so only repeated failures disable it. */ @@ -544,9 +544,9 @@ export function probeEngines(): Promise { } /** Run one engine and get capped, normalized matches. A single runtime - * failure (bad query) or a timeout only degrades THIS search — the engine - * stays usable so the next one retries it; only repeated consecutive - * failures disable the engine for the rest of the process. */ + * failure (bad query) or a timeout only degrades THIS search; a timeout also + * breaks the failure streak. Only repeated consecutive failures disable the + * engine, and the next success re-enables it. */ export async function runEngine( probe: EngineProbe, root: string, @@ -555,10 +555,16 @@ export async function runEngine( ): Promise { try { const result = await runner(probe, root, query, maxMatches) + // Success is recovery: clear the streak and lift any `broken` mark, so a + // late in-flight success cannot leave the engine filtered out forever. failures.delete(probe.engine) + broken.delete(probe.engine) return result } catch (error) { if (error instanceof EngineTimeoutError) { + // A timeout breaks the streak like a success: a huge tree is not a dead + // binary, so its count must not roll into the next search. + failures.delete(probe.engine) debugLog(`[dsh-search] engine ${probe.engine} timed out (tree too large), falling back`) } else { const count = (failures.get(probe.engine) ?? 0) + 1 diff --git a/tests/search-engines.spec.ts b/tests/search-engines.spec.ts index 4d99a04f..5e07dc3c 100644 --- a/tests/search-engines.spec.ts +++ b/tests/search-engines.spec.ts @@ -536,6 +536,51 @@ describe('probe cache and failure handling', () => { }) expect((await usableEngines()).map(probe => probe.engine)).toEqual(['fd']) }) + + // A timeout must break the consecutive-failure streak like a success: + // otherwise the residual count from two earlier failures rolls into the + // next one and "fail, fail, timeout, fail" disables a healthy engine, + // contradicting "only repeated consecutive failures". + it('a timeout breaks the streak (fail, fail, timeout, fail does not disable)', async () => { + setEngineHooks({ prober: async () => [fdProbe, rgProbe] }) + const outcomes = ['fail', 'fail', 'timeout', 'fail'] as const + let step = 0 + setEngineHooks({ + runner: async () => { + const outcome = outcomes[step] + step += 1 + if (outcome === 'timeout') throw new EngineTimeoutError('search engine timed out') + throw new Error('boom') + }, + }) + for (let i = 0; i < outcomes.length; i += 1) { + await runEngine(fdProbe, '/w', 'x', 10).catch(() => {}) + } + expect((await usableEngines()).map(probe => probe.engine)).toEqual(['fd', 'rg']) + }) + + // Concurrency: three failures can add `broken` while a fourth, in-flight + // request later succeeds. The success clears `failures` — it must also lift + // `broken`, or usableEngines() keeps filtering out an engine that recovered. + it('a success after the streak tripped BROKEN_AFTER re-enables the engine', async () => { + setEngineHooks({ prober: async () => [fdProbe, rgProbe] }) + let call = 0 + setEngineHooks({ + runner: async () => { + call += 1 + if (call <= 3) throw new Error('boom') + return { paths: [], dirs: [], truncated: false } + }, + }) + const calls = [ + runEngine(fdProbe, '/w', 'x', 10), + runEngine(fdProbe, '/w', 'x', 10), + runEngine(fdProbe, '/w', 'x', 10), + runEngine(fdProbe, '/w', 'x', 10), + ] + await Promise.allSettled(calls) + expect((await usableEngines()).map(probe => probe.engine)).toEqual(['fd', 'rg']) + }) }) /**