From 39426be9a126f261ef5901b1436289fef6d60dd7 Mon Sep 17 00:00:00 2001 From: yiliang114 <1204183885@qq.com> Date: Tue, 18 Nov 2025 14:25:05 +0800 Subject: [PATCH] wip --- .../src/WebViewProvider.ts | 11 +++ .../src/agents/QwenAgentManager.ts | 91 +++++++++++++------ .../vscode-ide-companion/src/extension.ts | 6 ++ 3 files changed, 78 insertions(+), 30 deletions(-) diff --git a/packages/vscode-ide-companion/src/WebViewProvider.ts b/packages/vscode-ide-companion/src/WebViewProvider.ts index a2d485a8..6a112ebd 100644 --- a/packages/vscode-ide-companion/src/WebViewProvider.ts +++ b/packages/vscode-ide-companion/src/WebViewProvider.ts @@ -459,6 +459,17 @@ export class WebViewProvider { `; } + /** + * Reset agent initialization state + * Call this when auth cache is cleared to force re-authentication + */ + resetAgentState(): void { + console.log('[WebViewProvider] Resetting agent state'); + this.agentInitialized = false; + // Disconnect existing connection + this.agentManager.disconnect(); + } + dispose(): void { this.panel?.dispose(); this.agentManager.disconnect(); diff --git a/packages/vscode-ide-companion/src/agents/QwenAgentManager.ts b/packages/vscode-ide-companion/src/agents/QwenAgentManager.ts index bee8637d..b89bd53b 100644 --- a/packages/vscode-ide-companion/src/agents/QwenAgentManager.ts +++ b/packages/vscode-ide-companion/src/agents/QwenAgentManager.ts @@ -62,6 +62,12 @@ export class QwenAgentManager { workingDir: string, authStateManager?: AuthStateManager, ): Promise { + const connectId = Date.now(); + console.log(`\n========================================`); + console.log(`[QwenAgentManager] šŸš€ CONNECT() CALLED - ID: ${connectId}`); + console.log(`[QwenAgentManager] Call stack:\n${new Error().stack}`); + console.log(`========================================\n`); + this.currentWorkingDir = workingDir; const config = vscode.workspace.getConfiguration('qwenCode'); const cliPath = config.get('qwen.cliPath', 'qwen'); @@ -156,43 +162,54 @@ export class QwenAgentManager { // Create new session if we couldn't restore one if (!sessionRestored) { console.log('[QwenAgentManager] Creating new session...'); + console.log( + `[QwenAgentManager] āš ļø WORKAROUND: Skipping explicit authenticate() call`, + ); + console.log( + `[QwenAgentManager] āš ļø Reason: newSession() internally calls refreshAuth(), which triggers device flow`, + ); + console.log( + `[QwenAgentManager] āš ļø Calling authenticate() first causes double authentication`, + ); - // Authenticate only if needed (not cached or session restore failed) - if (needsAuth) { - await this.authenticateWithRetry(authMethod, 3); - // Save successful auth to cache - if (authStateManager) { - await authStateManager.saveAuthState(workingDir, authMethod); - } - } + // WORKAROUND: Skip explicit authenticate() call + // The newSession() method will internally call config.refreshAuth(), + // which will trigger device flow if no valid token exists. + // Calling authenticate() first causes a duplicate OAuth flow due to a bug in Qwen CLI + // where authenticate() doesn't properly save refresh token for newSession() to use. - // Try to create session + // Try to create session (which will trigger auth internally if needed) try { + console.log( + `\nšŸ” [AUTO AUTH] newSession will handle authentication automatically\n`, + ); await this.newSessionWithRetry(workingDir, 3); console.log('[QwenAgentManager] New session created successfully'); - } catch (sessionError) { - // If we used cached auth but session creation failed, - // the cached auth might be invalid (token expired on server) - // Clear cache and retry with fresh authentication - if (!needsAuth && authStateManager) { - console.log( - '[QwenAgentManager] Session creation failed with cached auth, clearing cache and re-authenticating...', - ); - await authStateManager.clearAuthState(); - // Retry with fresh authentication - await this.authenticateWithRetry(authMethod, 3); - await authStateManager.saveAuthState(workingDir, authMethod); - await this.newSessionWithRetry(workingDir, 3); + // Save auth state after successful session creation + if (authStateManager) { console.log( - '[QwenAgentManager] Successfully authenticated and created session after cache invalidation', + '[QwenAgentManager] Saving auth state after successful session creation', ); - } else { - // If we already tried with fresh auth, or no auth manager, just throw - throw sessionError; + await authStateManager.saveAuthState(workingDir, authMethod); } + } catch (sessionError) { + console.log(`\nāš ļø [SESSION FAILED] newSessionWithRetry threw error\n`); + console.log(`[QwenAgentManager] Error details:`, sessionError); + + // If session creation failed, clear cache and let user retry + if (authStateManager) { + console.log('[QwenAgentManager] Clearing auth cache due to failure'); + await authStateManager.clearAuthState(); + } + + throw sessionError; } } + + console.log(`\n========================================`); + console.log(`[QwenAgentManager] āœ… CONNECT() COMPLETED SUCCESSFULLY`); + console.log(`========================================\n`); } /** @@ -202,19 +219,31 @@ export class QwenAgentManager { authMethod: string, maxRetries: number, ): Promise { + const timestamp = new Date().toISOString(); + const callStack = new Error().stack; + console.log( + `[QwenAgentManager] šŸ” AUTHENTICATION CALL STARTED at ${timestamp}`, + ); + console.log( + `[QwenAgentManager] Auth method: ${authMethod}, Max retries: ${maxRetries}`, + ); + console.log(`[QwenAgentManager] Call stack:\n${callStack}`); + for (let attempt = 1; attempt <= maxRetries; attempt++) { try { console.log( - `[QwenAgentManager] Authenticating (attempt ${attempt}/${maxRetries})...`, + `[QwenAgentManager] šŸ“ Authenticating (attempt ${attempt}/${maxRetries})...`, ); await this.connection.authenticate(authMethod); - console.log('[QwenAgentManager] Authentication successful'); + console.log( + `[QwenAgentManager] āœ… Authentication successful on attempt ${attempt}`, + ); return; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); console.error( - `[QwenAgentManager] Authentication attempt ${attempt} failed:`, + `[QwenAgentManager] āŒ Authentication attempt ${attempt} failed:`, errorMessage, ); @@ -226,7 +255,9 @@ export class QwenAgentManager { // Wait before retrying (exponential backoff) const delay = Math.min(1000 * Math.pow(2, attempt - 1), 5000); - console.log(`[QwenAgentManager] Retrying in ${delay}ms...`); + console.log( + `[QwenAgentManager] ā³ Retrying in ${delay}ms... (${maxRetries - attempt} retries remaining)`, + ); await new Promise((resolve) => setTimeout(resolve, delay)); } } diff --git a/packages/vscode-ide-companion/src/extension.ts b/packages/vscode-ide-companion/src/extension.ts index 44f0889f..f9b14ab9 100644 --- a/packages/vscode-ide-companion/src/extension.ts +++ b/packages/vscode-ide-companion/src/extension.ts @@ -147,6 +147,12 @@ export async function activate(context: vscode.ExtensionContext) { }), vscode.commands.registerCommand('qwenCode.clearAuthCache', async () => { await authStateManager.clearAuthState(); + + // Reset WebView agent state to force re-authentication + if (webViewProvider) { + webViewProvider.resetAgentState(); + } + vscode.window.showInformationMessage( 'Qwen Code authentication cache cleared. You will need to login again on next connection.', );