From e5df038a88532ea6b78d9eee08adaeebe614d492 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 23:50:40 -0600 Subject: [PATCH] fix(mcpx): don't cool down transient daemon failures --- pkgs/mcpx/plugin/opencode/mcpx-session.ts | 3 +- pkgs/mcpx/plugin/opencode/v2/tools.test.ts | 49 ++++++++++++++++++++++ pkgs/mcpx/plugin/opencode/v2/tools.ts | 4 +- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/pkgs/mcpx/plugin/opencode/mcpx-session.ts b/pkgs/mcpx/plugin/opencode/mcpx-session.ts index 5087c10d0..9f29373ec 100644 --- a/pkgs/mcpx/plugin/opencode/mcpx-session.ts +++ b/pkgs/mcpx/plugin/opencode/mcpx-session.ts @@ -260,7 +260,8 @@ export default (async ({ directory, worktree, project, client }, options) => { }; const forget = () => { found = undefined; - missedAt = Date.now(); + // A transient daemon failure is not a discovery miss; retry immediately. + missedAt = 0; }; // One discovery at boot, awaited. diff --git a/pkgs/mcpx/plugin/opencode/v2/tools.test.ts b/pkgs/mcpx/plugin/opencode/v2/tools.test.ts index 4df7288e5..8b70bc670 100644 --- a/pkgs/mcpx/plugin/opencode/v2/tools.test.ts +++ b/pkgs/mcpx/plugin/opencode/v2/tools.test.ts @@ -281,6 +281,55 @@ describe("rediscovery precedence", () => { await rt.look(); expect(rememberedSeen).toEqual([targetKey(picked)]); }); + + test("a transient failure immediately rediscovers at the default miss cooldown", async () => { + let discoverCalls = 0; + let failExec = true; + const rt = runtime({ + discoverFn: async () => { + discoverCalls += 1; + return discoveryWith([candidate(socketTarget)]); + }, + clientFor: () => + fakeClient({ + exec: async () => { + if (failExec) { + failExec = false; + throw new Error("daemon went away"); + } + return { output: "recovered through daemon" }; + }, + }), + }); + const exec = rt.tools().find((t) => t.name === "exec")!; + + await expect( + exec.execute({ source: "1" } as never, { sessionID: "s1" }), + ).rejects.toThrow(); + expect(discoverCalls).toBe(1); + + const recovered = await exec.execute( + { source: "1" } as never, + { sessionID: "s1" }, + ); + expect(recovered.content).toContain("recovered through daemon"); + expect(discoverCalls).toBe(2); + }); + + test("a genuine discovery miss still observes the default cooldown", async () => { + let discoverCalls = 0; + const rt = runtime({ + discoverFn: async () => { + discoverCalls += 1; + return { candidates: [], rung: 6, reason: "none", ambiguous: false }; + }, + }); + + await rt.look(); + await rt.look(); + + expect(discoverCalls).toBe(1); + }); }); describe("tool executors", () => { diff --git a/pkgs/mcpx/plugin/opencode/v2/tools.ts b/pkgs/mcpx/plugin/opencode/v2/tools.ts index 47a44b2ea..478d221d9 100644 --- a/pkgs/mcpx/plugin/opencode/v2/tools.ts +++ b/pkgs/mcpx/plugin/opencode/v2/tools.ts @@ -237,7 +237,9 @@ export function createToolRuntime(deps: ToolRuntimeDeps): ToolRuntime { const forget = (): void => { found = undefined; - missedAt = Date.now(); + // A transient daemon failure is not a discovery miss; retry the ladder + // immediately on the next call. + missedAt = 0; }; /**