Skip to content

Fix unsafe assertions in code_assist folder.#19706

Merged
gundermanc merged 2 commits intomainfrom
gundermanc/linter2
Feb 20, 2026
Merged

Fix unsafe assertions in code_assist folder.#19706
gundermanc merged 2 commits intomainfrom
gundermanc/linter2

Conversation

@gundermanc
Copy link
Member

Summary

Fix unsafe type assertions in client code. Follow up to #19691.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @gundermanc, 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 focuses on enhancing the type safety and robustness of the codebase within the code_assist folder. It systematically addresses and eliminates various unsafe type assertions, replacing them with explicit type annotations, type guard functions, and more thorough runtime checks. The changes aim to prevent potential runtime errors and improve code maintainability by leveraging TypeScript's type system more effectively.

Highlights

  • Improved Type Safety in converter.ts: A new type guard function isPart was introduced to safely determine the type of content, replacing an unsafe type assertion within the toContent function.
  • Explicit Typing for JSON.parse Results: Explicit type annotations were added to the results of JSON.parse in experiments.ts and oauth-credential-storage.ts, removing previous unsafe type assertions.
  • Robust Type Checks in oauth2.ts: Unsafe type assertions for credential types were replaced with more robust typeof and in operator checks, and a null check was added for network address retrieval.
  • Enhanced Type Inference and Error Handling in server.ts: Generic type parameters were added to client requests, multiple unsafe type assertions were removed, and error handling for VPC SC was refactored using a new type guard and interface.
Changelog
  • packages/core/src/code_assist/converter.ts
    • Added a new type guard function isPart to safely determine if an object is a PartUnion.
    • Updated the toContent function to leverage the isPart type guard for more robust type checking.
    • Removed an explicit and unsafe as Part type assertion within toContent.
  • packages/core/src/code_assist/experiments/experiments.ts
    • Provided an explicit type annotation for the result of JSON.parse to ListExperimentsResponse.
    • Eliminated an unsafe type assertion when calling parseExperiments.
  • packages/core/src/code_assist/oauth-credential-storage.ts
    • Assigned an explicit Credentials type to the JSON.parse output, removing the need for an unsafe type assertion.
  • packages/core/src/code_assist/oauth2.ts
    • Replaced an unsafe type assertion with more robust typeof and in operator checks for the credentials.type property.
    • Introduced a null and type check for the server.address() return value to safely access its port property.
  • packages/core/src/code_assist/server.ts
    • Removed the unused GoogleRpcResponse import.
    • Applied generic type parameters to this.client.request calls for improved type inference.
    • Eliminated several unsafe as T type assertions on res.data and JSON.parse results.
    • Refactored the isVpcScAffectedUser function by introducing a VpcScErrorResponse interface and an isVpcScErrorResponse type guard for safer and more explicit error object validation.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gundermanc gundermanc marked this pull request as ready for review February 20, 2026 20:22
@gundermanc gundermanc requested a review from a team as a code owner February 20, 2026 20:22
debugLogger.debug('Reading experiments from', expPath);
const content = await fs.promises.readFile(expPath, 'utf8');
const response = JSON.parse(content);
const response: ListExperimentsResponse = JSON.parse(content);
Copy link
Member Author

Choose a reason for hiding this comment

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

NOTE: JSON.parse() is also not type-safe because it returns any. I will follow up by disallowing unsafe-assignment as well in eslint config.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request successfully addresses several unsafe type assertions across the code_assist module by introducing robust type guards and leveraging TypeScript's generic request methods. These changes significantly improve the type safety and maintainability of the codebase. I have identified one high-severity issue in a new type guard that could lead to a runtime crash if passed a null value, and provided a suggestion to fix it.

@github-actions
Copy link

github-actions bot commented Feb 20, 2026

Size Change: +504 B (0%)

Total Size: 25.2 MB

ℹ️ View Unchanged
Filename Size Change
./bundle/gemini.js 25.2 MB +504 B (0%)
./bundle/sandbox-macos-permissive-open.sb 890 B 0 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB 0 B
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB 0 B
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB 0 B
./bundle/sandbox-macos-strict-open.sb 4.82 kB 0 B
./bundle/sandbox-macos-strict-proxied.sb 5.02 kB 0 B

compressed-size-action

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@gundermanc gundermanc added this pull request to the merge queue Feb 20, 2026
Merged via the queue into main with commit b7555ab Feb 20, 2026
27 checks passed
@gundermanc gundermanc deleted the gundermanc/linter2 branch February 20, 2026 20:57
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