Skip to content

fix: resolve cyclic structure serialization crash in token counter#839

Open
OmarAlaaeldein wants to merge 1 commit into
CodebuffAI:mainfrom
OmarAlaaeldein:fix/zod-cyclic-structure-crash
Open

fix: resolve cyclic structure serialization crash in token counter#839
OmarAlaaeldein wants to merge 1 commit into
CodebuffAI:mainfrom
OmarAlaaeldein:fix/zod-cyclic-structure-crash

Conversation

@OmarAlaaeldein

Copy link
Copy Markdown

This PR fixes a TypeError crash: JSON.stringify cannot serialize cyclic structures when the token count API fails and falls back to local estimation.

  • Implement safeJsonStringify in token-counter.ts to handle circular references.
  • Sanitize the inputSchema inside toolDefinitions at creation time using JSON.parse(safeJsonStringify(schema)) so it is a plain, serializable object.
  • This also prevents circular structure errors when writing the chat run state to disk (run-state.json).

When the token count API call fails or is not available, the CLI fallback logic
serializes  using . However,
contains raw Zod schema objects (), which have circular references,
causing a  TypeError.

Changes:
- Added a  utility to strip circular references from objects.
- Sanitize the  inside  at creation time using
   so it is a plain, serializable object.
  This also prevents similar serialization failures when saving run state to disk.
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.

1 participant