Commit 6cc1576
refactor: migrate Serializable type to Zod schema and fix circular reference handling (#12865)
### What this PR does
Before this PR:
- `Serializable` type and `isSerializable` function were maintained
separately
- `SerializableValue` used `any` workaround (with FIXME comment about
TS2589)
- No runtime validation schema available for consumers
- `error.ts` directly assigned `error.response` without serialization,
causing type errors with `Date` objects
- `isSerializable` incorrectly returned `true` for circular references,
despite `JSON.stringify` throwing on them
After this PR:
- Unified type definition using Zod schema backed by the existing
`isSerializable` type guard via `z.custom()`, ensuring consistent
validation behavior
- Added `SerializableSchema` export for use by other modules
- Removed unused helper functions (`assertSerializable`,
`tryParseSerializable`) to keep exports minimal
- Fixed `error.ts` to properly serialize `response` field using
`safeSerialize()`
- Fixed `isSerializable` to correctly reject circular references
- Improved `isSerializable` return type to `value is Serializable` for
better type narrowing
- Added comprehensive unit tests for `isSerializable` and
`SerializableSchema`
- Removed misleading FIXME comment about TS2589
Fixes #
### Why we need it and why it was done in this way
The following tradeoffs were made:
- Used `z.custom(isSerializable)` instead of building a recursive Zod
schema with `z.lazy()`, to guarantee the Zod schema and the hand-written
type guard share identical validation logic (e.g. circular reference
handling, prototype chain checks, built-in object rejection)
- Kept existing `isSerializable()` implementation as the single source
of truth for runtime checks
The following alternatives were considered:
- Building a standalone recursive Zod schema with `z.lazy()` +
`z.union()` + `z.record()`, but this had subtle behavioral differences
from `isSerializable()` (e.g. `z.record()` does not check prototype
chains or reject `Date`/class instances)
- Using `z.infer` to derive type from schema, but `z.lazy()` requires
explicit type annotation to avoid `any` inference
- **Using Zod 4's built-in `z.json()`** — this was evaluated as a
potential replacement for the entire hand-written `isSerializable` +
`z.custom()` approach. `z.json()` validates the same recursive union
(`string | number | boolean | null | JsonValue[] | Record<string,
JsonValue>`), which is structurally identical to our `Serializable`
type. However, it was not adopted for the following reasons:
1. **Circular reference safety**: `z.json()` is built on `z.lazy()`,
which recurses without cycle detection. Passing a circular reference
causes a stack overflow instead of returning a validation failure. Our
`isSerializable()` uses a `seen` Set to detect cycles and safely returns
`false`. This is critical because `utils/serialize.ts` calls
`isSerializable()` on arbitrary values of unknown provenance.
2. **Performance**: The hand-written check uses direct `typeof`
comparisons and a single recursive walk. `z.json()` uses
`z.union([...])` which must attempt each branch in order, adding
overhead for deep/large objects.
3. **Explicit built-in object rejection**: `isSerializable()` explicitly
rejects `Date`, `RegExp`, `Map`, `Set`, `Error`, `File`, and `Blob` with
clear `instanceof` checks and a prototype chain guard, making the
rejection reasons self-documenting. `z.json()` rejects them implicitly
(they don't match any union branch), which is correct but less obvious
to readers.
### Breaking changes
None. All existing exports remain unchanged:
- `Serializable` type (same signature)
- `isSerializable` function (same behavior for all valid inputs;
circular references now correctly return `false` instead of `true` —
this is a bug fix, as circular references are not JSON-serializable and
`tryLenientSerialize` already expects them to be rejected)
### Special notes for your reviewer
- The `error.ts` change (line 196) fixes a type error that surfaced
after making `Serializable` stricter — `error.response` is
`LanguageModelResponseMetadata` which contains `Date` objects, so it
needs `safeSerialize()` instead of direct assignment.
- `SerializableSchema` uses `z.custom()` to delegate all validation to
`isSerializable`, so the two are guaranteed to be consistent.
- The circular reference fix is a bug fix: `isSerializable` previously
returned `true` for circular references to "avoid infinite recursion",
but this was incorrect — circular references cause `JSON.stringify` to
throw, so they are by definition not serializable. The
`tryLenientSerialize` design also assumes circular references are not
serializable.
### Checklist
- [x] PR: The PR description is expressive enough and will help future
contributors
- [x] Code: [Write code that humans can
understand](https://en.wikiquote.org/wiki/Martin_Fowler#code-for-humans)
and [Keep it simple](https://en.wikipedia.org/wiki/KISS_principle)
- [x] Refactor: You have [left the code cleaner than you found it (Boy
Scout
Rule)](https://learning.oreilly.com/library/view/97-things-every/9780096809515/ch08.html)
- [x] Upgrade: Impact of this change on upgrade flows was considered and
addressed if required
- [ ] Documentation: A [user-guide update](https://docs.cherry-ai.com)
was considered and is present (link) or not required. You want a
user-guide update if it's a user facing feature.
### Release note
```release-note
NONE
```
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Happy <yesreply@happy.engineering>1 parent 894664f commit 6cc1576
File tree
3 files changed
+165
-17
lines changed- src/renderer/src
- types
- __tests__
- utils
3 files changed
+165
-17
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | | - | |
4 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | | - | |
8 | | - | |
| 9 | + | |
| 10 | + | |
9 | 11 | | |
| 12 | + | |
10 | 13 | | |
11 | | - | |
12 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
13 | 20 | | |
14 | 21 | | |
15 | 22 | | |
16 | | - | |
| 23 | + | |
17 | 24 | | |
18 | 25 | | |
19 | 26 | | |
| |||
23 | 30 | | |
24 | 31 | | |
25 | 32 | | |
26 | | - | |
| 33 | + | |
27 | 34 | | |
28 | | - | |
| 35 | + | |
29 | 36 | | |
30 | 37 | | |
31 | 38 | | |
32 | 39 | | |
33 | 40 | | |
34 | 41 | | |
35 | 42 | | |
36 | | - | |
| 43 | + | |
37 | 44 | | |
38 | 45 | | |
39 | | - | |
| 46 | + | |
40 | 47 | | |
41 | 48 | | |
42 | | - | |
| 49 | + | |
43 | 50 | | |
44 | 51 | | |
45 | 52 | | |
| |||
52 | 59 | | |
53 | 60 | | |
54 | 61 | | |
55 | | - | |
| 62 | + | |
56 | 63 | | |
57 | 64 | | |
58 | 65 | | |
59 | | - | |
| 66 | + | |
60 | 67 | | |
61 | 68 | | |
62 | 69 | | |
63 | 70 | | |
64 | 71 | | |
65 | 72 | | |
66 | | - | |
| 73 | + | |
67 | 74 | | |
68 | 75 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
193 | 193 | | |
194 | 194 | | |
195 | 195 | | |
196 | | - | |
| 196 | + | |
197 | 197 | | |
198 | 198 | | |
199 | 199 | | |
| |||
0 commit comments