Skip to content

MBS-10651: Use correct context for AI calls#68

Merged
marcusgreen merged 1 commit intomarcusgreen:mainfrom
bycs-lp:MBS-10651-Use_correct_context_for_AI_call
Mar 16, 2026
Merged

MBS-10651: Use correct context for AI calls#68
marcusgreen merged 1 commit intomarcusgreen:mainfrom
bycs-lp:MBS-10651-Use_correct_context_for_AI_call

Conversation

@PhMemmel
Copy link
Copy Markdown
Contributor

@PhMemmel PhMemmel commented Mar 9, 2026

The current code uses the wrong context when sending AI requests. The context of the question is being used right now which is for example the context of the question bank the question is living in.

However, submitting an attempt in a quiz you definitely want the context of the quiz to be the one being used in the LLM request (which is being used for permission checks in local_ai_manager for example), because that's actually the context you are operating in.

Steps for testing:

  • Install block_ai_control
  • Install local_ai_manager
  • Select local_ai_manager as backend for qtype_aitext in the admin settings
  • Create course A with a qbank containing a qtype_aitext question
  • Create course B with a quiz activity and add the question from the question bank from course A
  • Check the "Add AI control center" in the course settings of course B.
  • Enable all AI functionalitites in the control center block.
  • Start an attempt as student, submit the attempt. You should not receive an error anymore (because now the correct context is being checked).

Bonus testing steps:

  • Whenever making a request check the database entries in local_ai_manager_request_log / context tables.
  • When submitting an attempt from inside a quiz, the contextid column in local_ai_manager_request_log table should have the quiz course module context id
  • When doing a preview of the question in the question edit form or from the dropdown in the question bank list view, the contextin in local_ai_manager_request_log should be the context of the mod_qbank instance
  • When doing a full quiz preview as teacher, the contextid in local_ai_manager_request_log should belong to the quiz course module
  • Maybe you can think of other use cases :)

@PhMemmel PhMemmel marked this pull request as draft March 9, 2026 14:31
@marcusgreen
Copy link
Copy Markdown
Owner

Thanks Phillip I will look at it after Wednesday, when I am one of the presenters at the Moodle Leeds events (and will be showing some ByCS things). I don't doubt your code, I just want to understand it before I merge.

@PhMemmel
Copy link
Copy Markdown
Contributor Author

No rush and thanks in advance! Final version will be pushed soon.

@PhMemmel PhMemmel force-pushed the MBS-10651-Use_correct_context_for_AI_call branch from 17f8bf8 to 4ce9229 Compare March 10, 2026 07:41
@PhMemmel PhMemmel marked this pull request as ready for review March 10, 2026 07:57
@PhMemmel
Copy link
Copy Markdown
Contributor Author

I added some more context to the description. I think you got the issue now. It's now also easily testable manually. Unit test coverage should be pretty decent as well :)

@marcusgreen marcusgreen merged commit cc636cd into marcusgreen:main Mar 16, 2026
1 check passed
@marcusgreen
Copy link
Copy Markdown
Owner

Thanks Phillip, I now understand your block ai_control plugin which I would not have done otherwise. I did a test with the Core AI subsystem and it worked as expected.

@PhMemmel
Copy link
Copy Markdown
Contributor Author

Thanks for reviewing, testing and merging!

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