From 13ac6006fa44e769c96a96357443302dbd41ae7b Mon Sep 17 00:00:00 2001 From: unsc9 <438590997@qq.com> Date: Fri, 6 Nov 2026 17:52:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(search):=20=E7=A6=81=E7=94=A8=E5=BC=95?= =?UTF-8?q?=E6=93=8E=E5=86=B7=E5=8D=B4=E5=90=8E=E9=87=8D=E8=AF=95=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E6=95=B4=E4=B8=AA=E4=BC=9A=E8=AF=9D=E7=A6=81?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/search-engines.ts | 30 +++++++++++++++++++++++------- tests/search-engines.spec.ts | 21 +++++++++++++++++++++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/search-engines.ts b/src/search-engines.ts index 886c9c7c..2353021f 100644 --- a/src/search-engines.ts +++ b/src/search-engines.ts @@ -16,8 +16,11 @@ * * 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). + * one search, and a timeout breaks the failure streak. A disabled engine is + * retried after a cooldown — a transient cause (a replaced binary, one EMFILE + * burst) must not cost the fast path for the rest of the session — and a + * success recovers it immediately. Hooks are swappable for tests + * (setEngineHooks). */ import { spawn, type ChildProcess } from 'node:child_process' import { createInterface } from 'node:readline' @@ -513,10 +516,13 @@ 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 disabled after repeated failures; cleared on the next success. */ -const broken = new Set() +/** Engines disabled after repeated failures → the moment each may be retried; + * a success clears the entry outright. */ +const broken = new Map() /** Streak length that disables an engine (failure policy: see module header). */ const BROKEN_AFTER = 3 +/** Cooldown before a disabled engine gets one more attempt. */ +const BROKEN_RETRY_MS = 60_000 const failures = new Map() let probePromise: Promise | null = null @@ -568,7 +574,7 @@ export async function runEngine( failures.set(probe.engine, count) const why = error instanceof Error ? error.message : String(error) if (count >= BROKEN_AFTER) { - broken.add(probe.engine) + broken.set(probe.engine, Date.now() + BROKEN_RETRY_MS) debugLog(`[dsh-search] engine ${probe.engine} failed ${String(count)} times in a row, disabled: ${why}`) } else { debugLog(`[dsh-search] engine ${probe.engine} query failed (${String(count)}/${String(BROKEN_AFTER)}), will retry: ${why}`) @@ -578,10 +584,20 @@ export async function runEngine( } } -/** The engines a caller may actually try (verified, not broken). */ +/** The engines a caller may actually try (verified, and not inside a disable + * cooldown). */ export async function usableEngines(): Promise { const probes = await probeEngines() - return probes.filter((probe) => !broken.has(probe.engine)) + const now = Date.now() + return probes.filter((probe) => { + const retryAt = broken.get(probe.engine) + if (retryAt === undefined) return true + if (now < retryAt) return false + // Probation: one attempt. A failure re-arms the cooldown, a success lifts + // the mark entirely (runEngine). + broken.delete(probe.engine) + return true + }) } /** Test seam: replace the probe / child-runner / spawn implementations. */ diff --git a/tests/search-engines.spec.ts b/tests/search-engines.spec.ts index 5e07dc3c..2bfac4fd 100644 --- a/tests/search-engines.spec.ts +++ b/tests/search-engines.spec.ts @@ -581,6 +581,27 @@ describe('probe cache and failure handling', () => { await Promise.allSettled(calls) expect((await usableEngines()).map(probe => probe.engine)).toEqual(['fd', 'rg']) }) + + // Disabling is not permanent: without the cooldown the only way back is a + // process restart, which is how a transient cause (a replaced binary, one + // EMFILE burst) used to cost the fast path for the rest of the session. + it('retries a disabled engine once the cooldown elapses', async () => { + vi.useFakeTimers() + try { + vi.setSystemTime(0) + setEngineHooks({ prober: async () => [fdProbe, rgProbe] }) + setEngineHooks({ runner: async () => { throw new Error('boom') } }) + for (let i = 0; i < 3; i += 1) { + await runEngine(fdProbe, '/w', 'x', 10).catch(() => {}) + } + expect((await usableEngines()).map(probe => probe.engine)).toEqual(['rg']) + + vi.setSystemTime(60_000) + expect((await usableEngines()).map(probe => probe.engine)).toEqual(['fd', 'rg']) + } finally { + vi.useRealTimers() + } + }) }) /**