lightning icon indicating copy to clipboard operation
lightning copied to clipboard

fix: scope job chat sessions by step

Open elias-ba opened this issue 1 month ago • 1 comments

Description

This PR fixes AI chat session job scoping to prevent incorrect chat sessions from appearing when switching between jobs in the workflow editor.

Previously, when a user had a chat session open for Job A and switched to Job B, the chat session from Job A would persist or the wrong session would be shown. This PR implements strict job scoping with three layers of protection:

  1. URL parameter management: Clears the j-chat parameter when switching jobs (but preserves it on initial page load)
  2. Session ownership validation: Validates that loaded sessions belong to the currently selected job
  3. Graceful error handling: Falls back to showing the session list if validation fails

Closes #3745

Validation steps

  1. Test URL parameter clearing when switching jobs:

    • Create a workflow with two jobs (Job A and Job B)
    • Open Job A's AI assistant and create a chat session
    • Switch to Job B by clicking it on the canvas
    • Verify that Job B shows its own session list, not Job A's session
  2. Test session list filtering:

    • Create chat sessions for both Job A and Job B
    • Open Job A's AI assistant
    • Verify only Job A's sessions are shown in the list
    • Switch to Job B
    • Verify only Job B's sessions are shown
  3. Test URL tampering protection:

    • Create a chat session for Job A
    • Manually modify the URL to include Job B's ID with Job A's session ID (e.g., ?s=job_b_id&j-chat=job_a_session_id)
    • Verify the page doesn't crash and shows Job B's session list instead
  4. Test persistence on page reload:

    • Open Job A with a chat session
    • Refresh the page
    • Verify the chat session persists correctly
  5. Run the test suite:

    mix test test/lightning_web/live/ai_assistant_live_test.exs
    mix test test/lightning/ai_assistant/ai_assistant_test.exs
    mix test test/lightning_web/ai_assistant/job_code_test.exs
    

Additional notes for the reviewer

AI Usage

Please disclose how you've used AI in this work (it's cool, we just want to know!):

  • [x] Code generation (copilot but not intellisense)
  • [ ] Learning or fact checking
  • [ ] Strategy / design
  • [ ] Optimisation / refactoring
  • [ ] Translation / spellchecking / doc gen
  • [ ] Other
  • [ ] I have not used AI

You can read more details in our Responsible AI Policy

Pre-submission checklist

  • [x] I have performed a self-review of my code.
  • [x] I have implemented and tested all related authorization policies. (e.g., :owner, :admin, :editor, :viewer)
  • [x] I have updated the changelog.
  • [x] I have ticked a box in "AI usage" in this PR

elias-ba avatar Oct 24 '25 01:10 elias-ba

Codecov Report

:x: Patch coverage is 87.87879% with 4 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 88.85%. Comparing base (94bbbb7) to head (353fa4f).

Files with missing lines Patch % Lines
lib/lightning/ai_assistant/ai_assistant.ex 82.35% 3 Missing :warning:
lib/lightning_web/live/workflow_live/edit.ex 83.33% 1 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3841      +/-   ##
==========================================
+ Coverage   88.84%   88.85%   +0.01%     
==========================================
  Files         422      422              
  Lines       19116    19136      +20     
==========================================
+ Hits        16984    17004      +20     
  Misses       2132     2132              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Oct 24 '25 02:10 codecov[bot]

@elias-ba I think I understand the change, I think this is good to merge. While this change didn't jump out as an issue, the logic around switching the job chat and chat in general makes me nervous about what it's going to take to get this into the new collaborative editor.

stuartc avatar Nov 17 '25 12:11 stuartc