From f0b887024ada060ec3699a2dea18489bc5f7649d Mon Sep 17 00:00:00 2001 From: anuzzolilli-oss Date: Sat, 14 Nov 2026 10:24:37 -0500 Subject: [PATCH 1/2] fix: recheck zcode route ownership after replacement discovery on exit handoff handoffZcodeRoutingOnExit checked that the shared store pointed at its own origin only before awaiting replacement discovery. Another process could take the route during that await, and the exiting process then overwrote it with the replacement or reverted it to direct. Ownership is now re-read after the await and again before the direct revert that follows a failed re-route. A store that changed owner is left untouched and the outcome is "not-ours". Refs #13 Assisted-by: Cindy (https://cindy.app) --- src/zcode/native.ts | 11 +++++++++-- tests/zcode-native.test.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/zcode/native.ts b/src/zcode/native.ts index e4bbb41375..25d6f71e67 100644 --- a/src/zcode/native.ts +++ b/src/zcode/native.ts @@ -592,9 +592,15 @@ export async function handoffZcodeRoutingOnExit(opts: ExitHandoffOptions): Promi const env = opts.env ?? process.env; const log = opts.log ?? defaultLog; const dataDir = opts.dataDir ?? resolveZcodeDataDir(env); - const current = detectCurrentZcodeOrigin(dataDir, env); - if (!current || current !== opts.ownOrigin) return "not-ours"; + const stillOurs = () => detectCurrentZcodeOrigin(dataDir, env) === opts.ownOrigin; + if (!stillOurs()) return "not-ours"; const replacement = await (opts.findReplacement ?? defaultFindReplacement)(); + // Replacement discovery awaits; another process may have taken the shared + // route meanwhile, and its route must not be overwritten or removed. + if (!stillOurs()) { + log("exit handoff: the store changed owner during replacement discovery — leaving it untouched"); + return "not-ours"; + } if (replacement) { const routed = await routeZcodeConfig({ origin: replacement.origin, env, dataDir: opts.dataDir, log, policy: zcodePolicyFromEnv(env) }); if (routed) { @@ -602,6 +608,7 @@ export async function handoffZcodeRoutingOnExit(opts: ExitHandoffOptions): Promi log(`exit handoff: store → ${replacement.origin}`); return "handed-off"; } + if (!stillOurs()) return "not-ours"; } unrouteZcode({ env, dataDir: opts.dataDir, log }); log("exit handoff: store reverted to direct upstream"); diff --git a/tests/zcode-native.test.ts b/tests/zcode-native.test.ts index d4ec19845b..afb2e8e655 100644 --- a/tests/zcode-native.test.ts +++ b/tests/zcode-native.test.ts @@ -710,6 +710,34 @@ test("handoffZcodeRoutingOnExit reverts to direct when no replacement lives (#16 } }); +for (const replacementFound of [false, true]) { + test(`handoffZcodeRoutingOnExit leaves a route another owner took during replacement discovery (replacement=${replacementFound})`, async () => { + const dir = dataDir(); + const newOwner = "http://127.0.0.1:38787"; + try { + await routeZcodeConfig({ origin: "http://127.0.0.1:18787", dataDir: dir, log: NO_LOG }); + let taken = ""; + assert.equal( + await handoffZcodeRoutingOnExit({ + ownOrigin: "http://127.0.0.1:18787", + dataDir: dir, + log: NO_LOG, + findReplacement: async () => { + await routeZcodeConfig({ origin: newOwner, dataDir: dir, log: NO_LOG }); + taken = readFileSync(storeFile(dir), "utf8"); + return replacementFound ? { origin: "http://127.0.0.1:28787" } : undefined; + }, + }), + "not-ours", + ); + assert.equal(detectCurrentZcodeOrigin(dir), newOwner); + assert.equal(readFileSync(storeFile(dir), "utf8"), taken); + } finally { + rmrf(dir); + } + }); +} + // #1892: the zero-wrapped path must explain itself instead of leaving the // client a bare "Connection closed". An empty personal store is NOT "no // provider" — this build sources active providers from the built-in/account From d24bea3127d97b953b4a3b72cf02989de5bf95c4 Mon Sep 17 00:00:00 2001 From: anuzzolilli-oss Date: Sat, 14 Nov 2026 10:42:26 -0500 Subject: [PATCH 2/2] test: cover both outcomes of the post-reroute ownership guard Review of #62: the guard before the direct revert that follows a refused re-route had no test. A signing-era store with route "plans" makes the re-route refuse; the injected log callback moves the store to another owner in one case. Changed ownership must give "not-ours" with the store byte-identical; retained ownership must give "reverted-direct". The new handoff tests also pass an isolated env so no ambient store override can redirect them. Refs #13 Assisted-by: Cindy (https://cindy.app) --- tests/zcode-native.test.ts | 45 +++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/tests/zcode-native.test.ts b/tests/zcode-native.test.ts index afb2e8e655..c61bdb5802 100644 --- a/tests/zcode-native.test.ts +++ b/tests/zcode-native.test.ts @@ -715,22 +715,23 @@ for (const replacementFound of [false, true]) { const dir = dataDir(); const newOwner = "http://127.0.0.1:38787"; try { - await routeZcodeConfig({ origin: "http://127.0.0.1:18787", dataDir: dir, log: NO_LOG }); + await routeZcodeConfig({ origin: "http://127.0.0.1:18787", dataDir: dir, env: {}, log: NO_LOG }); let taken = ""; assert.equal( await handoffZcodeRoutingOnExit({ ownOrigin: "http://127.0.0.1:18787", dataDir: dir, + env: {}, log: NO_LOG, findReplacement: async () => { - await routeZcodeConfig({ origin: newOwner, dataDir: dir, log: NO_LOG }); + await routeZcodeConfig({ origin: newOwner, dataDir: dir, env: {}, log: NO_LOG }); taken = readFileSync(storeFile(dir), "utf8"); return replacementFound ? { origin: "http://127.0.0.1:28787" } : undefined; }, }), "not-ours", ); - assert.equal(detectCurrentZcodeOrigin(dir), newOwner); + assert.equal(detectCurrentZcodeOrigin(dir, {}), newOwner); assert.equal(readFileSync(storeFile(dir), "utf8"), taken); } finally { rmrf(dir); @@ -738,6 +739,44 @@ for (const replacementFound of [false, true]) { }); } +for (const takenDuringReroute of [false, true]) { + test(`handoffZcodeRoutingOnExit after a refused re-route reverts only a route it still owns (taken=${takenDuringReroute})`, async () => { + const dir = mkdtempSync(path.join(tmpdir(), "zcode-native-refused-")); + mkdirSync(path.join(dir, "v2"), { recursive: true }); + const file = zcodeStoreCandidates(dir, "new", {})[0]; + writeFileSync( + file, + JSON.stringify({ schemaVersion: 1, config: { providerConfigRules: { providerRules: [{ providerId: "account:deepseek", config: { api: { baseUrl: "https://api.deepseek.com/v1" } } }] } } }) + "\n", + ); + try { + assert.ok(await routeZcodeConfig({ origin: "http://127.0.0.1:18787", dataDir: dir, env: {}, log: NO_LOG })); + let taken: string | undefined; + const outcome = await handoffZcodeRoutingOnExit({ + ownOrigin: "http://127.0.0.1:18787", + dataDir: dir, + // route:"plans" on the signing-era store makes the re-route refuse (#1621). + env: { BILI_ZCODE_ROUTE: "plans" }, + log: (msg) => { + if (!takenDuringReroute || taken !== undefined || !/client[- ]signing/.test(msg)) return; + taken = readFileSync(file, "utf8").split("127.0.0.1:18787").join("127.0.0.1:38787"); + writeFileSync(file, taken); + }, + findReplacement: async () => ({ origin: "http://127.0.0.1:28787" }), + }); + if (takenDuringReroute) { + assert.equal(outcome, "not-ours"); + assert.equal(detectCurrentZcodeOrigin(dir, {}), "http://127.0.0.1:38787"); + assert.equal(readFileSync(file, "utf8"), taken); + } else { + assert.equal(outcome, "reverted-direct"); + assert.doesNotMatch(readFileSync(file, "utf8"), /\/bili\//); + } + } finally { + rmrf(dir); + } + }); +} + // #1892: the zero-wrapped path must explain itself instead of leaving the // client a bare "Connection closed". An empty personal store is NOT "no // provider" — this build sources active providers from the built-in/account