fix(js): reflection context is always an object for runAction#4910
fix(js): reflection context is always an object for runAction#4910Ehesp wants to merge 1 commit intofirebase:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a subtle but critical issue in the reflection API's handling of action contexts. Previously, if a client did not explicitly provide a context when calling Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request addresses a bug where runAction in the reflection API could pass an undefined context, leading to potential runtime errors. The proposed change ensures the context is always an object by defaulting to {} when it's not provided, which is a correct and robust fix. I've added one suggestion to refactor the implementation slightly to avoid code duplication and improve long-term maintainability.
| }; | ||
| const result = await action.run(input, { | ||
| context, | ||
| context: context || {}, |
There was a problem hiding this comment.
While this correctly fixes the issue, the same logic context || {} is duplicated for the non-streaming case on line 307. To improve maintainability and adhere to the DRY (Don't Repeat Yourself) principle, you could define the default context before the if (stream === 'true') block and reuse it in both action.run calls.
For example:
const { key, input, context, telemetryLabels } = request.body;
const effectiveContext = context || {};
// ...
// in streaming case:
await action.run(input, {
context: effectiveContext,
// ...
});
// ...
// in non-streaming case:
await action.run(input, {
context: effectiveContext,
// ...
});This would centralize the logic for handling the context.
In the reflection API, when we
POST /api/runAction, the reflection server forwards context from the request body. When the client (e.g. Dev UI) omits it, context isundefinedand is passed through toaction.run():That leads to
runWithContext(undefined, actFn). In that case no context is stored in async context, sogetContext()/ai.currentContext()returnsundefinedfor the whole run. Any code that assumes a run-scoped context object then breaks. So if something like a plugin wants to write to context, e.g.getContext().someKey = value,getContext()isundefined.So the actual problem is in code that writes to context (plugins or flows) and assumes
getContext()is a mutable object. The reflection fix (defaulting to{}) makes that assumption valid for runs started via the reflection API.Checklist (if applicable):