Skip to content

fix: defer AI chat loading until AI tab opens#3956

Open
ahmedhesham6 wants to merge 1 commit intosuperplanehq:mainfrom
ahmedhesham6:fix/defer-ai-builder-chat-loading
Open

fix: defer AI chat loading until AI tab opens#3956
ahmedhesham6 wants to merge 1 commit intosuperplanehq:mainfrom
ahmedhesham6:fix/defer-ai-builder-chat-loading

Conversation

@ahmedhesham6
Copy link
Copy Markdown

What changed:

  • defer loading AI chat sessions and messages until the AI Builder tab is opened
  • preserve loaded AI chat state when switching between Components and AI Builder
  • reset AI chat state when the canvas changes
  • add regression tests covering lazy loading, tab reopen behavior, and canvas changes

Why:

How:

  • gate the AI session and conversation effects behind a hasOpenedAiTab flag and showAiBuilderTab
  • clear sidebar AI state only when the canvas changes
  • add focused sidebar tests for the affected state transitions

Related issues:

Test plan:

  • cd web_src && npx eslint src/ui/BuildingBlocksSidebar/index.tsx src/ui/BuildingBlocksSidebar/index.spec.tsx
  • cd web_src && npx prettier --check src/ui/BuildingBlocksSidebar/index.tsx src/ui/BuildingBlocksSidebar/index.spec.tsx
  • cd web_src && npm run test:run -- src/ui/BuildingBlocksSidebar/index.spec.tsx
  • make check.build.ui

What changed:
- defer loading AI chat sessions and messages until the AI Builder tab is opened
- preserve loaded AI chat state when switching between Components and AI Builder
- reset AI chat state when the canvas changes
- add regression tests covering lazy loading, tab reopen behavior, and canvas changes

Why:
- avoid unnecessary agent connection attempts during sidebar render when the agent is not configured
- prevent the noisy console error reported in issue superplanehq#3837

How:
- gate the AI session and conversation effects behind a hasOpenedAiTab flag and showAiBuilderTab
- clear sidebar AI state only when the canvas changes
- add focused sidebar tests for the affected state transitions

Related issues:
- Refs superplanehq#3837

Test plan:
- cd web_src && npx eslint src/ui/BuildingBlocksSidebar/index.tsx src/ui/BuildingBlocksSidebar/index.spec.tsx
- cd web_src && npx prettier --check src/ui/BuildingBlocksSidebar/index.tsx src/ui/BuildingBlocksSidebar/index.spec.tsx
- cd web_src && npm run test:run -- src/ui/BuildingBlocksSidebar/index.spec.tsx
- make check.build.ui

Signed-off-by: Ahmed Hesham Abdelkader <23265119+ahmedhesham6@users.noreply.github.com>
@superplanehq-integration
Copy link
Copy Markdown

👋 Commands for maintainers:

  • /sp start - Start an ephemeral machine (takes ~30s)
  • /sp stop - Stop a running machine (auto-executed on pr close)

@ahmedhesham6 ahmedhesham6 changed the title fix(building-blocks-sidebar): defer AI chat loading until AI tab opens fix: defer AI chat loading until AI tab opens Apr 4, 2026
Copy link
Copy Markdown
Contributor

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 06e4f19. Configure here.

cancelled = true;
};
}, [canvasId, organizationId]);
}, [canvasId, hasOpenedAiTab, organizationId, showAiBuilderTab]);
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.

Stale state triggers unnecessary API calls on canvas change

Low Severity

When canvasId changes while hasOpenedAiTab is true, the canvas-reset effect (line 429) and the session/conversation loading effects (lines 458, 500) run in the same React commit. The loading effects see the stale hasOpenedAiTab = true from the previous render — the batched setHasOpenedAiTab(false) from the reset effect hasn't committed yet. This causes loadChatSessions and potentially loadChatConversation to fire unnecessary API calls that are subsequently cancelled on the next render. This contradicts the PR's goal of avoiding unnecessary requests when the AI tab hasn't been opened for the current canvas.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 06e4f19. Configure here.

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.

Don't try to connect to the agent if it is not configured

1 participant