Skip to content

fix(core): preserve shared references in JSON serialization - #29626

Open
zero-dot7 wants to merge 2 commits into
google-gemini:mainfrom
zero-dot7:fix/29406-shared-references-json-serialization
Open

zero-dot7 wants to merge 2 commits into
google-gemini:mainfrom
zero-dot7:fix/29406-shared-references-json-serialization

Conversation

@zero-dot7

Copy link
Copy Markdown

Problem

safeJsonStringify used a single global "seen" WeakSet to detect circular references. An object referenced more than once (but not part of a cycle) was wrongly replaced with [Circular] — e.g. the OTel metrics endTime object shared between data points and histogram boundaries reused across buckets. Reported in #29406.

Root cause

A reference appearing twice on different DFS branches is not circular, but a global visited-set cannot distinguish "already visited elsewhere" from "on the current path".

Fix

Track only the current DFS path: the replacer adds each object key via this before serializing its subtree and removes it after the subtree completes. True cycles still contain the reference on the active path and are still rendered as [Circular]; shared (diamond) references are serialized normally on every occurrence.

Tests

  • New regression tests in packages/core/src/utils/safeJsonStringify.test.ts: shared references (endTime-style, histogram-boundary-style) are preserved; true cycles still produce [Circular]; nesting/escaping behavior unchanged.
  • Sabotage check: reverting the fix makes the new regression tests fail, confirming they bite.
  • npx vitest run src/utils/safeJsonStringify.test.ts → 11/11 pass.
  • Callers (mcp-tool, recordingContentGenerator) suites: 69/69 pass; eslint + prettier clean.

Fixes #29406

safeJsonStringify used a global 'seen' WeakSet, so any shared (non-circular)
object reference was replaced with [Circular]. OpenTelemetry records share
endTime objects and histogram bound arrays, which corrupted exported
telemetry files.

Track only the current depth-first ancestor path via the replacer's
'this' (the holder of the current key) and unwind completed subtrees.
True cycles still produce [Circular]; shared references are now
serialized at each occurrence.

Fixes google-gemini#29406
@zero-dot7
zero-dot7 requested a review from a team as a code owner October 4, 2026 07:53
@google-cla

google-cla Bot commented Oct 4, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions Bot added the size/m A medium sized PR label Oct 4, 2026
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 68
  • Additions: +63
  • Deletions: -5
  • Files changed: 2

@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 an issue where the safeJsonStringify utility incorrectly flagged shared object references as circular. By switching from a global tracking mechanism to a path-based stack, the utility now correctly distinguishes between shared references (diamond dependencies) and true circular structures, ensuring accurate JSON serialization for complex objects like OpenTelemetry metrics.

Highlights

  • Circular Reference Detection Logic: Replaced the global WeakSet-based circular reference detection with a stack-based approach that tracks only the current DFS path, allowing shared (non-circular) references to be serialized correctly.
  • Regression Testing: Added comprehensive test cases in safeJsonStringify.test.ts to verify that shared object references and shared array references are preserved, while actual circular references continue to be handled as [Circular].
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. ↩

@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 updates the safeJsonStringify utility to use an ancestor stack instead of a global WeakSet for circular reference detection. This allows shared, non-circular references to be serialized correctly instead of being incorrectly flagged as [Circular], while still properly detecting true circular references. Unit tests have been added to verify these scenarios. I have no feedback to provide as the implementation is correct and well-tested.

@zero-dot7

Copy link
Copy Markdown
Author

@google-cla-bot recheck

1 similar comment
@zero-dot7

Copy link
Copy Markdown
Author

@google-cla-bot recheck

@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/enterprise Issues related to Telemetry, Policy, Quota / Licensing labels Oct 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/enterprise Issues related to Telemetry, Policy, Quota / Licensing priority/p2 Important but can be addressed in a future release. size/m A medium sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Telemetry outfile writes "[Circular]" for shared metric endTime and histogram boundaries

1 participant