From 98b2e2010fa7ad825c67108f9bb09e13a8db5b41 Mon Sep 17 00:00:00 2001 From: Dev Date: Thu, 19 Nov 2026 14:03:21 +0800 Subject: [PATCH 1/3] fix(tunnel): expire stale poll readiness and recover it in place Bound green status to a recent successful poll, distinguish live recovery from process exit, and preserve the protected command ledger during network recovery. Constraint: Keep the same Tunnel identity, limits, and replay protections Confidence: high Scope-risk: moderate --- apps/desktop/src-tauri/src/connections.rs | 4 ++ .../src-tauri/src/connections/tests.rs | 20 ++++++- .../connection/ConnectionCard.test.tsx | 26 ++++++++- .../features/connection/ConnectionCard.tsx | 13 +++-- apps/desktop/src/i18n/connections-tools.ts | 2 + crates/webcodex-openai-tunnel/README.md | 4 ++ .../src/control_plane.rs | 7 +-- crates/webcodex-openai-tunnel/src/health.rs | 26 ++++++--- crates/webcodex-openai-tunnel/src/poller.rs | 16 +++--- .../tests/boundedness.rs | 55 +++++++++++++++++++ .../tests/startup_poll.rs | 53 ++++++++++++++++++ 11 files changed, 200 insertions(+), 26 deletions(-) diff --git a/apps/desktop/src-tauri/src/connections.rs b/apps/desktop/src-tauri/src/connections.rs index 8c8940b3e..f4d34dc6f 100644 --- a/apps/desktop/src-tauri/src/connections.rs +++ b/apps/desktop/src-tauri/src/connections.rs @@ -289,6 +289,8 @@ impl ConnectionRuntimes { ConnectionLifecycle::Error }; entry.state.ready = false; + entry.state.tunnel_ready = None; + entry.state.local_mcp_ready = None; entry.state.health = ConnectionHealth::Unknown; entry.state.last_error = if success { None @@ -489,6 +491,8 @@ impl ConnectionRuntimes { if self.update(id, generation, |state| { state.lifecycle = ConnectionLifecycle::Error; state.ready = false; + state.tunnel_ready = None; + state.local_mcp_ready = None; state.health = ConnectionHealth::Degraded; state.last_error = Some(failure); if state.reason_code.is_none() { diff --git a/apps/desktop/src-tauri/src/connections/tests.rs b/apps/desktop/src-tauri/src/connections/tests.rs index 141389d46..d030513f0 100644 --- a/apps/desktop/src-tauri/src/connections/tests.rs +++ b/apps/desktop/src-tauri/src/connections/tests.rs @@ -255,13 +255,26 @@ async fn optional_command_diagnostics_preserve_health_and_reset_for_a_new_proces .runtime .command_diagnostics .is_none()); - registry.stopping(id); + // End the child without cancelling the observer, as on an unexpected exit. supervisor .lock() .await .stop_checked(ProcessKey::RegularTunnel(id)) .await .unwrap(); + let deadline = Instant::now() + Duration::from_secs(8); + loop { + let mut current = snapshot(&[id]); + registry.project(&mut current, true); + let state = ¤t.profiles[0].runtime; + if state.lifecycle == ConnectionLifecycle::Error { + assert_eq!(state.tunnel_ready, None); + assert_eq!(state.local_mcp_ready, None); + break; + } + assert!(Instant::now() < deadline, "child exit was not observed: {current:?}"); + tokio::time::sleep(Duration::from_millis(20)).await; + } registry.stopped(id, true); } @@ -292,6 +305,11 @@ async fn concurrent_observers_are_independent_and_stale_generation_cannot_kill_r .await .unwrap(); registry.stopped(a, true); + let mut stopped = snapshot(&[a, b]); + registry.project(&mut stopped, true); + assert_eq!(stopped.profiles[0].runtime.tunnel_ready, None); + assert_eq!(stopped.profiles[0].runtime.local_mcp_ready, None); + assert_eq!(stopped.profiles[1].runtime.tunnel_ready, Some(true)); assert_eq!( supervisor .lock() diff --git a/apps/desktop/src/features/connection/ConnectionCard.test.tsx b/apps/desktop/src/features/connection/ConnectionCard.test.tsx index c59a78025..1444ccb17 100644 --- a/apps/desktop/src/features/connection/ConnectionCard.test.tsx +++ b/apps/desktop/src/features/connection/ConnectionCard.test.tsx @@ -18,8 +18,8 @@ describe("Connection advanced details", () => { const profile = connectionFixture({ runtime_directory: path, startup_timings_ms: { cli_total: 5540, initial_poll: 5510, poll_attempts: 1, poll_request_ms: 5502 }, - tunnel_ready: true, - local_mcp_ready: false, + tunnel_ready: null, + local_mcp_ready: null, process_ready: true, process_started: true, command_diagnostics: { rejected_commands: 2, terminal_response_delivery_failures: 1 }, @@ -63,7 +63,7 @@ describe("Connection advanced details", () => { expect(pairs.has(connectionsToolsText(locale, key))).toBe(true); } expect(pairs.get(connectionsToolsText(locale, "processReady"))).toBe(connectionsToolsText(locale, "yes")); - expect(pairs.get(connectionsToolsText(locale, "localMcpReady"))).toBe(connectionsToolsText(locale, "no")); + expect(pairs.get(connectionsToolsText(locale, "localMcpReady"))).toBe("—"); expect(pairs.get(connectionsToolsText(locale, "pollRetryWait"))).toBe("8 ms"); expect(pairs.get(connectionsToolsText(locale, "runtimeDirectory"))).toBe(path); expect(pairs.get(connectionsToolsText(locale, "rejectedCommands"))).toBe("2"); @@ -73,6 +73,26 @@ describe("Connection advanced details", () => { expect(details?.querySelector("dd.connection-meta-path code:last-child")).toBeTruthy(); }); + it("shows polling separately from local access and waits for automatic recovery while the process is running", () => { + localStorage.setItem("webcodex.desktop.locale", "zh-CN"); + const ready = connectionFixture({ lifecycle: "running", ready: true, tunnel_ready: true, local_mcp_ready: true }); + const card = (profile: typeof ready) => + undefined} onEdit={() => undefined} onDelete={() => undefined} + onCopy={() => undefined} onSettings={() => undefined} /> + ; + const { container, rerender } = render(card(ready)); + const checks = () => Array.from(container.querySelectorAll("dl.connection-checks dd"), (check) => check.textContent); + expect(checks()).toEqual(["轮询正常", "可访问"]); + + rerender(card({ ...ready, ready: false, tunnel_ready: false, last_error: "tunnel_unavailable", reason_code: "tunnel_unavailable" })); + expect(checks()).toEqual(["等待轮询自动恢复", "可访问"]); + expect(container.querySelector(".connection-recovery")).toBeNull(); + + rerender(card(ready)); + expect(checks()).toEqual(["轮询正常", "可访问"]); + }); + it("omits unavailable optional diagnostics without creating empty labels", () => { const profile = connectionFixture({ startup_timings_ms: null, local_mcp_ready: null, runtime_directory: null }); const { container } = render( diff --git a/apps/desktop/src/features/connection/ConnectionCard.tsx b/apps/desktop/src/features/connection/ConnectionCard.tsx index 2cbdd87d3..7afa904e3 100644 --- a/apps/desktop/src/features/connection/ConnectionCard.tsx +++ b/apps/desktop/src/features/connection/ConnectionCard.tsx @@ -55,11 +55,13 @@ export function ConnectionCard({ profile, canStart, busy, copied, onAction, onEd const failed = !profile.ready && Boolean(profile.last_error || profile.lifecycle === "error"); const recovery = recoveryFor(profile); const healthStale = profile.last_error === "health_stale"; + const waitingForPoll = profile.lifecycle === "running" && profile.last_error === "tunnel_unavailable" + && profile.reason_code === "tunnel_unavailable" && profile.tunnel_ready === false && profile.local_mcp_ready === true; const status = profile.server_restart_required ? c("pendingServerRestart") : profile.ready ? p("running") - : failed + : waitingForPoll ? c("awaitingStatus") : failed ? c("connectionUnavailable") : profile.lifecycle === "starting" ? p("starting") @@ -111,11 +113,14 @@ export function ConnectionCard({ profile, canStart, busy, copied, onAction, onEd if (profile.local_mcp_url) detailRows.push({ label: "mcpAddress", value: {profile.local_mcp_url}, path: true }); if (profile.runtime_directory) detailRows.push({ label: "runtimeDirectory", value: {profile.runtime_directory}, path: true }); return
-

{profile.name}

{owner}