From f165049f550c9547e93e74e05a038a43a720c61c Mon Sep 17 00:00:00 2001 From: Octopus Date: Sat, 4 Apr 2026 10:15:09 +0800 Subject: [PATCH] fix: track spawned profile ID to correctly attribute rate limit errors When a task process hits a rate limit, detectRateLimit() was called without a profileId and fell back to getActiveProfile(). If the user switched profiles after the task started, the rate limit was attributed to the wrong profile. Fix by returning profileId from setupProcessEnvironment(), recording it via state.assignProfileToTask() at spawn time, then passing it to detectRateLimit() in handleProcessFailure(). Fixes #1903 --- apps/desktop/src/main/agent/agent-process.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/apps/desktop/src/main/agent/agent-process.ts b/apps/desktop/src/main/agent/agent-process.ts index d3f114211f..bdc5643122 100644 --- a/apps/desktop/src/main/agent/agent-process.ts +++ b/apps/desktop/src/main/agent/agent-process.ts @@ -163,7 +163,7 @@ export class AgentProcessManager { private setupProcessEnvironment( extraEnv: Record - ): NodeJS.ProcessEnv { + ): { env: NodeJS.ProcessEnv; profileId: string; profileName: string } { // Get best available Claude profile environment (automatically handles rate limits) const profileResult = getBestAvailableProfileEnv(); const profileEnv = profileResult.env; @@ -253,7 +253,7 @@ export class AgentProcessManager { apiKeyPrefix: mergedEnv.ANTHROPIC_API_KEY?.substring(0, 8) || '(not set)', }); - return mergedEnv; + return { env: mergedEnv, profileId: profileResult.profileId, profileName: profileResult.profileName }; } private handleProcessFailure( @@ -263,7 +263,8 @@ export class AgentProcessManager { ): boolean { console.log('[AgentProcess] Checking for rate limit in output (last 500 chars):', allOutput.slice(-500)); - const rateLimitDetection = detectRateLimit(allOutput); + const profileAssignment = this.state.getTaskProfileAssignment(taskId); + const rateLimitDetection = detectRateLimit(allOutput, profileAssignment?.profileId); console.log('[AgentProcess] Rate limit detection result:', { isRateLimited: rateLimitDetection.isRateLimited, resetTime: rateLimitDetection.resetTime, @@ -549,7 +550,8 @@ export class AgentProcessManager { spawnId }); - const env = this.setupProcessEnvironment(extraEnv); + const { env, profileId: spawnedProfileId, profileName: spawnedProfileName } = this.setupProcessEnvironment(extraEnv); + this.state.assignProfileToTask(taskId, spawnedProfileId, spawnedProfileName, 'proactive'); // Get active API profile environment variables let apiProfileEnv: Record = {};