Skip to content

fix(core): fall back to empty on malformed agents.json shape - #29208

Open
soroush5 wants to merge 2 commits into
google-gemini:mainfrom
soroush5:fix-acknowledged-agents-shape
Open

fix(core): fall back to empty on malformed agents.json shape#29208
soroush5 wants to merge 2 commits into
google-gemini:mainfrom
soroush5:fix-acknowledged-agents-shape

Conversation

@soroush5

@soroush5 soroush5 commented Sep 4, 2026

Copy link
Copy Markdown

Closes #29207.

A corrupt agents.json (valid JSON, wrong shape — null, scalar, or array, from an interrupted save, full disk, sync conflict, or hand edit) crashed isAcknowledged/acknowledge with a raw TypeError, or silently dropped the acknowledgment (array shape). load now validates the parsed shape with a type guard and falls back to {} with a logged error, like every other load failure already does.

How to test:

  1. Write null into ~/.gemini/acknowledgments/agents.json and trigger an agent acknowledgment check — no throw, treated as not-acknowledged.
  2. Run npx vitest run src/agents/acknowledgedAgents.test.ts in packages/core — all 8 pass, including 4 new regression cases (null/42/"str"/[]; the first three fail without the fix).

@soroush5
soroush5 requested a review from a team as a code owner September 4, 2026 08:59
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 potential crash in the agent acknowledgment service caused by corrupted or malformed agents.json files. By introducing structural validation during the loading process, the application now safely handles unexpected data shapes by falling back to a default empty state instead of propagating errors to the rest of the system.

Highlights

  • Robustness Improvement: Added a type guard to validate the structure of the agents.json file during load, preventing crashes caused by malformed JSON shapes.
  • Error Handling: Implemented a fallback mechanism that resets the acknowledgedAgents state to an empty object and logs an error when the file content does not match the expected map structure.
  • Regression Testing: Added new test cases to verify that various malformed JSON inputs (null, scalars, arrays) are handled gracefully without throwing exceptions.
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 the 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 counterproductive. 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.

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.

@github-actions github-actions Bot added the size/s A small PR label Sep 4, 2026
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 58
  • Additions: +56
  • Deletions: -2
  • Files changed: 2

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

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.

Code Review

This pull request introduces safe JSON parsing for the acknowledged agents file, falling back to an empty object if the parsed JSON has an unexpected shape, and adds corresponding unit tests. The review feedback correctly identifies that the new isAcknowledgedAgentsMap type guard only performs a shallow check, which could still lead to runtime crashes if nested values are primitives. The reviewer suggests a more robust type guard implementation and additional test cases to cover these nested malformed shapes.

Comment on lines +20 to +24
function isAcknowledgedAgentsMap(
value: unknown,
): value is AcknowledgedAgentsMap {
return !!value && typeof value === 'object' && !Array.isArray(value);
}

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.

high

The type guard isAcknowledgedAgentsMap only performs a shallow check on the top-level value. If the JSON is an object but contains non-object values (e.g., {"/project": "string"}), isAcknowledgedAgentsMap will return true. This will cause acknowledge to crash with a TypeError when trying to set a property on a primitive string (e.g., TypeError: Cannot create property 'Agent' on string).

To prevent this, the type guard should also verify that all values within the map are non-null, non-array objects.

function isAcknowledgedAgentsMap(
  value: unknown,
): value is AcknowledgedAgentsMap {
  if (!value || typeof value !== 'object' || Array.isArray(value)) {
    return false;
  }
  return Object.values(value).every(
    (val) => !!val && typeof val === 'object' && !Array.isArray(val),
  );
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You're right — I reproduced it: {"/project": "str"} passes the old guard and then acknowledge() throws TypeError: Cannot create property 'Agent' on string. Fixed by requiring every project entry to be a non-null, non-array object (falls back to empty otherwise), plus regression tests for the nested shapes. Pushed in b8cd71b — 12/12 green.

);
});

it.each(['null', '42', '"str"', '[]'])(

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.

high

Add test cases for nested malformed shapes (such as an object containing a string or null value) to ensure the improved type guard prevents crashes on these shapes as well.

Suggested change
it.each(['null', '42', '"str"', '[]'])(
it.each(['null', '42', '"str"', '[]', '{"/project": "str"}', '{"/project": null}'])(

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done — added {"/project": "str"}, 42, null and [] nested cases to the shape test. Without the guard fix, the truthy-primitive ones fail; with it, all 12 pass.

@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality labels Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality priority/p2 Important but can be addressed in a future release. size/m A medium sized PR size/s A small PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Corrupt agents.json (valid JSON, wrong shape) crashes acknowledgment

1 participant