Skip to content

fix(desktop): recover invalid connection context - #2963

Open
jackie-cqz wants to merge 5 commits into
oceanbase:masterfrom
jackie-cqz:codex/desktop-recover-invalid-space
Open

jackie-cqz wants to merge 5 commits into
oceanbase:masterfrom
jackie-cqz:codex/desktop-recover-invalid-space

Conversation

@jackie-cqz

Copy link
Copy Markdown
Contributor

Which issue or RFC does this PR close?

Closes #2937.

Rationale for this change

When a Server is replaced at the same endpoint, the Desktop native layer invalidates the active connection. Home statistics, Retry read, and top-bar space selection could leave the renderer on the old space. The saved service then lacked an explicit activation action.

What changes are included in this PR?

  • Reconcile the renderer with native connection state when a statistics read, retry, or scope switch fails.
  • Expose the native retry generation in the Desktop IPC snapshot so the renderer can show Retry read during a temporary interruption without retaining private statistics. Retire the retry generation when the original service or scope is permanently unavailable.
  • Clear the remembered active selection when the native connection is lost. Keep the saved service profile available for explicit activation.
  • Add regression coverage for Server replacement, temporary outages, starting the renderer during an interruption, and permission denial isolated to the target scope.

Are there any user-facing changes?

Home and the space switcher discard stale context after permanent invalidation. A temporary interruption offers Retry read and restores the same space once the original Server is verified. A denied target scope leaves the current space available.

How was this change tested?

Local validation on macOS Apple Silicon, based on upstream master 667f77e8.

  • vitest run passed 92 frontend tests using mocked Desktop IPC.
  • cargo test --locked --manifest-path desktop/src-tauri/Cargo.toml --test deployment_identity passed 32 tests with loopback HTTP fixtures.
  • The generated IPC matched the Rust definitions in cargo run --locked --manifest-path desktop/src-tauri/Cargo.toml --example export_ipc -- --check.
  • cargo clippy --locked --manifest-path desktop/src-tauri/Cargo.toml --all-targets -- -D warnings passed on Rust 1.95.0.
  • TypeScript, Prettier, Oxlint, Rust formatting, vite build, and git diff --check all passed. The repository hooks also passed with uv run --locked prek run -a.

Packaged Desktop acceptance remains pending.

AI usage statement

OpenAI Codex (GPT-6) implemented the fix and regression tests, reviewed the diff, and ran local verification.

@hidb4ai hidb4ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No confirmed correctness defect found in the recovery changes. Merge validation is still incomplete: the current CI preflight failed during Git checkout (TLS/HTTP2 fetch errors), so Desktop validation and the downstream checks were skipped. Please obtain passing checks for the current head and its merge result before merging; this checkout failure is not evidence of a code regression. Packaged Desktop acceptance also remains pending; renderer tests with mocked IPC and native loopback HTTP fixtures do not establish that acceptance.

The optional DesktopState.retryGeneration field expands the native IPC contract. There are no database or migration changes.

<Button
size="sm"
variant="outline"
disabled={busy || contextPending}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Reset the busy flag on interruption

If a space switch starts while Home statistics are still loading and then fails during a transient outage, the statistics effect cleanup invalidates its ticket without clearing busy, leaving Retry read disabled even after the service returns. Reset the canceled read's busy state when entering interruption so the original space can be recovered.

@hidb4ai hidb4ai added the module:desktop Primary module: desktop app and platform integration label Dec 2, 2026
@PsiACE

PsiACE commented Dec 2, 2026

Copy link
Copy Markdown
Member

Retry restores the original space after a brief outage. If access to that space has been revoked, keep the verified service and let the user choose another space. Please rerun the canceled macOS ARM check.

Concept mockup of temporary service failure and revoked space access

@knqiufan knqiufan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit af56b235.

Requesting changes for 2 blocking P2 gaps in #2937 recovery (plus 2 small P3 feedback items):

  • P2 (blocking): Preserve the verified service when retry_connection succeeds on identity verification but the original Scope is revoked (session.rs, follow-up on #2963 (comment)).
  • P2 (blocking): Opening the scope picker in desktop/ui/src/app/TopBar.tsx (ScopeMenu, around lines 348–360) calls desktopApi.scopes(), which runs native identity_unchanged and clears active on deployment replacement. Currently ScopeMenu only renders a dropdown error and never calls refreshConnectionState, leaving stale Home scope/statistics on screen and preventing the user from ever clicking another space to trigger onSelectScope recovery. (The test helper selectOtherSpace in app.test.tsx:367-376 mocked scopes as succeeding, masking this native path.) Please reconcile global connection state when desktopApi.scopes() fails.
  • P3 (non-blocking): Clear stale nativeError after a successful Retry read recovery (App.tsx), and optionally surface a brief error on permanent Retry read invalidation (Overview.tsx, without duplicating PR #2960's Home service-selection UI).

Comment on lines 429 to 435
Err(error) => {
if !Self::retryable_interruption(error.code) {
inner.interrupted = None;
// Publish a new generation when the retry handle is retired.
self.invalidate(&mut inner)?;
}
return Err(error);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Keep the verified service active when the original Scope is revoked on retry (blocking)

Follow-up on #2963 (comment): after verify_identity confirms the deployment and principal are unchanged (line 417), if interrupted.api.scope(scope_id) returns Forbidden or NotFound, this branch calls self.invalidate(&mut inner)? and leaves active = None, dropping the verified service instead of allowing the user to pick another authorized Scope (inconsistent with check_expected at session.rs:785-797).

Required outcome: when identity verification succeeds but the original Scope returns Forbidden/NotFound, retire interrupted and restore active with scope: None (advancing generation) so the user can choose another authorized Scope on the verified service.

Comment on lines +629 to +632
.then(receiveState, async (error: unknown) => {
setNativeError(error);
await refreshConnectionState(receiveState);
})

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Clear stale switch error after Retry read recovers the connection (non-blocking)

When a scope switch triggers a transient interruption, setNativeError(error) stores the outage globally. After Retry read restores the original scope via onState(next) -> receiveState, nativeError is never cleared, so the outage banner and Manage connections link remain visible across all pages.

Required outcome: clear nativeError in receiveState when transitioning out of an interrupted state back to an active connection (while keeping nativeError intact when a target scope is denied without interrupting the current scope).

Comment on lines +1228 to +1233
await refreshConnectionState((next) => {
if (ticket !== request.current) return;
setBusy(false);
onState(next);
});
},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Keep a brief failure explanation visible when Retry read permanently invalidates the session (non-blocking)

When Retry read receives stale_context (e.g. deployment replaced), handleReadFailure stores the error in UsageDashboard local state and then onState(next) unmounts UsageDashboard in favor of FirstRunHome, discarding the error message so the user sees no reason why retry ended.

Required outcome: surface the permanent invalidation error via the existing app-level nativeError feedback (no need to add a Home service-selector UI here, as that is covered by PR #2960).

@knqiufan knqiufan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit 3b58fe0f. All earlier review items are resolved; requesting changes for two remaining P2 edge cases in the new recovery UI paths:

  • P2: Overview shows "Choose a memory space" even when active is unverified/unreachable and TopBar hides the space picker (including on startup restore with an offline saved server).
  • P2: receiveState only clears nativeError when recovering from retryGeneration, leaving the stale invalidation banner pinned after reconnecting from FirstRunHome via Quick Connect.

Comment on lines +710 to +726
if (props.state.active && !props.state.active.scope) {
const zh = props.language === "zh";
return (
<PageFeedback
kind="empty"
title={zh ? "选择记忆空间" : "Choose a memory space"}
>
<p>
{zh
? "请使用顶部的空间选择器继续。"
: "Use the space picker above to continue."}
</p>
</PageFeedback>
);
}
if (!props.state.active && props.state.retryGeneration === undefined)
return <FirstRunHome {...props} />;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Only show "Choose a memory space" when the active service supports listing spaces

ConnectionManager::check_expected (session.rs:840-850) still sets active with scope: None when activate == true even if probing fails or compatibility is unverified (compatibility_verified: false, supported_operations: []). Both startup restore (App.tsx:320-323) and switching to an unreachable/incompatible service (App.tsx:446-452) reach this state. Because TopBar.tsx:84-86 hides the space picker whenever list_scopes is unsupported and active.scope is null, Home hides FirstRunHome and directs the user to a space picker that isn't rendered.

Please gate this branch on list_scopes support (matching TopBar.tsx:84-86), and fall back to FirstRunHome when there is no scope, no list_scopes support, and retryGeneration === undefined.

Comment on lines +230 to +238
if (next.active && previous?.retryGeneration !== undefined) {
setNativeError(null);
} else if (
readError != null &&
!next.active &&
next.retryGeneration === undefined
) {
setNativeError(readError);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Stale invalidation banner stays visible after reconnecting via Quick Connect

After permanent invalidation sets nativeError and returns Home to FirstRunHome, reconnecting via "Connect an existing service" calls receiveState(selected) with previous.active == null and previous.retryGeneration === undefined. Because line 230 only clears nativeError when previous?.retryGeneration !== undefined, the old invalidation error stays pinned above the newly connected workspace.

Please clear nativeError whenever a connection transitions from disconnected to active (if (next.active && !previous?.active)), which covers both retry recovery and Quick Connect while keeping target-scope errors on an active connection.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:desktop Primary module: desktop app and platform integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(desktop): recover when the active service or space becomes invalid

5 participants