Skip to content

fix: add connectionManager @W-22389160@#278

Open
EstebanRomero84 wants to merge 8 commits into
mainfrom
er/fixTokenHandling
Open

fix: add connectionManager @W-22389160@#278
EstebanRomero84 wants to merge 8 commits into
mainfrom
er/fixTokenHandling

Conversation

@EstebanRomero84
Copy link
Copy Markdown
Contributor

@EstebanRomero84 EstebanRomero84 commented May 8, 2026

This PR introduces a new ConnectionManager class that centralizes and improves the management of JWT and standard Salesforce connections for agent operations. The main goal is to fix JWT token handling issues and prevent token clobbering when agents interact with both SFAP (Salesforce AI Platform) APIs and org-instance operations.

Key Changes:

  1. New ConnectionManager class (src/connectionManager.ts):

    • Automatically creates and validates JWT tokens for SFAP API calls
    • Maintains separate connections: JWT for SFAP, standard for org operations
    • Provides JWT validation utilities for debugging and error handling
    • Prevents JWT token corruption by keeping connections isolated
    • Includes refreshStandardConnection() method for connection refresh operations
  2. Updated Agent.init() method (src/agent.ts):

    • Replaced manual JWT connection creation logic with ConnectionManager.create()
    • Simplified initialization of ScriptAgent and ProductionAgent
    • Both agent types now receive a ConnectionManager instead of a raw connection
  3. Refactored AgentBase class (src/agents/agentBase.ts):

    • Changed from storing Connection to storing ConnectionManager
    • Maintained backward compatibility: kept restoreConnection() method for downstream applications
    • Method now delegates to ConnectionManager.refreshStandardConnection() internally
    • Provides cleaner separation of concerns
  4. Updated Agent implementations (ProductionAgent and ScriptAgent):

    • Use connectionManager.getJwtConnection() for SFAP API calls
    • Use connectionManager.getStandardConnection() for org operations (SOQL queries, tooling API, metadata operations)
    • Eliminates manual JWT token management and refresh logic
  5. Simplified ScriptAgentPublisher (src/agents/scriptAgentPublisher.ts):

    • Removed manual createStandardConnection() method
    • Removed try/finally blocks for connection restoration
    • Delegates connection management to ConnectionManager
  6. Comprehensive test updates:

    • Added JWT token protection tests
    • Updated all agent tests to work with ConnectionManager
    • Improved mock setup for connection management

Why is this important?

This fix addresses a critical issue where JWT tokens could be clobbered (overwritten) by auto-refresh attempts during org operations, causing agent operations to fail. By isolating JWT and standard connections, we:

  • Prevent JWT token corruption from auto-refresh mechanisms
  • Provide explicit APIs for different connection types
  • Enable better debugging with JWT validation utilities
  • Simplify connection lifecycle management
  • Reduce error-prone manual connection handling

Breaking Changes

None - All public APIs are maintained for backward compatibility. The restoreConnection() method continues to work exactly as before for downstream applications.

Technical Details

The ConnectionManager provides:

  • getJwtConnection(): Returns JWT-authenticated connection for SFAP API endpoints
  • getStandardConnection(): Returns standard connection for org-instance operations
  • refreshStandardConnection(): Refreshes the standard connection (used internally by restoreConnection())
  • inspectJwt(): Returns detailed JWT validation information for troubleshooting
  • Automatic JWT validation with detailed error reporting

Issues Addressed

@W-22389160@

Comment thread src/agents/agentBase.ts

protected constructor(protected readonly connection: Connection) {}

public async restoreConnection(): Promise<void> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like the ConnectionManager a lot more, but will removing this be breaking anywhere in the plugin/VSC?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I'l revert this change

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants