Commit 2df1b48
fix(util): last complete object wins when skipping JSON preamble (#718)
`createPartialJsonStream({ skipPreamble: true })` latched onto the FIRST
syntactically valid `{…}` in the preamble and permanently ignored the real
payload:
input : '<think>the schema is {"type":"object"} so I will emit</think>{"name":"Bob"}'
finish: {"type":"object"} complete: true
`consumeSeek` opened a candidate at the first `{`; when it closed,
`afterValue()` set `Mode.Done` and `consume()` returned on every later chunk.
The `fail()` recovery only rescued candidates that produced NO data
(`rootIsEmpty()`), so a well-formed prose object was unrecoverable — and
`complete` reported `true`, so nothing downstream signalled a problem.
Both `skipPreamble` call sites (HFT, TF-MediaPipe) are exactly the local /
thinking models that restate the schema or a few-shot example before
answering. A model emitting `{"name":"Alice"}` as an illustration inside
`<think>` and then the real `{"name":"Bob"}` yielded `finish.data.object =
{"name":"Alice"}` — schema-valid, so `StructuredGenerationTask`'s
re-validation accepted it and the WRONG record was persisted.
A closed root is now provisional under `skipPreamble`: it is retained as
`lastComplete` and the parser returns to `Mode.Seek`, so the next `{` at
depth 0 starts a fresh candidate that supersedes it. Nothing is re-scanned —
a restart begins at the `{` that triggered it — so the parser stays O(total
input) and the live-root aliasing contract is untouched. `finish()` returns
the live root when it carries data (a genuinely truncated real payload),
else `lastComplete`, else `{}`.
Also in this change:
- `finish()` is declared `JsonValue` (new exported type) instead of
`Record<string, unknown>`. It always returned arrays and scalars for
array-/scalar-rooted documents; the declared type was a lie, so
`Object.keys(stream.finish())` on an array root type-checked and produced
`["0","1","2"]`. New `finishObject()` returns `{}` for a non-object root;
all 11 provider run-fns migrated to it, so an array root now fails
required-key validation loudly.
- `complete` no longer reports `true` for a root that only closed because
`finish()` repaired a truncated token (`push('"abc')`), which contradicted
its own JSDoc.
- `resolveRef.ts`: the per-container `hasRef` probe inside `walk` re-scanned
each node's whole subtree, making `resolveOutput` O(n·depth); `containsCycle`
added a second full traversal, and both recursed once per level so a deeply
nested value raised a `RangeError` out of cache resolution. One iterative
pass now answers all three questions up front (which containers hold a
reachable ref, whether any ref exists, whether there is a back-edge).
Measured: 4k-deep chain 590ms -> 30ms; a 50k-deep ref-free value no longer
overflows.
Co-authored-by: Claude <noreply@anthropic.com>1 parent 0efd950 commit 2df1b48
17 files changed
Lines changed: 433 additions & 114 deletions
File tree
- .claude
- packages
- task-graph/src
- cache
- task-graph/__tests__
- util
- src/json-schema
- __tests__
- providers
- anthropic/src/ai/common
- chrome-ai/src/ai/common
- deepseek/src/ai/common
- google-gemini/src/ai/common
- huggingface-transformers/src/ai/common
- node-llama-cpp/src/ai/common
- ollama/src/ai/common
- openai/src/ai/common
- openrouter/src/ai/common
- tf-mediapipe/src/ai/common
- xai/src/ai/common
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
247 | 247 | | |
248 | 248 | | |
249 | 249 | | |
250 | | - | |
| 250 | + | |
251 | 251 | | |
252 | 252 | | |
253 | 253 | | |
254 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
255 | 274 | | |
256 | 275 | | |
257 | 276 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
117 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
118 | 123 | | |
119 | 124 | | |
120 | 125 | | |
121 | 126 | | |
122 | 127 | | |
123 | 128 | | |
124 | | - | |
125 | | - | |
| 129 | + | |
| 130 | + | |
126 | 131 | | |
127 | 132 | | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
156 | 149 | | |
157 | 150 | | |
158 | 151 | | |
159 | | - | |
160 | | - | |
161 | | - | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
162 | 156 | | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
168 | 167 | | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
176 | 172 | | |
177 | | - | |
178 | | - | |
179 | | - | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
180 | 190 | | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
186 | 195 | | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
192 | 201 | | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
199 | 206 | | |
200 | | - | |
| 207 | + | |
| 208 | + | |
201 | 209 | | |
202 | | - | |
| 210 | + | |
| 211 | + | |
203 | 212 | | |
204 | 213 | | |
205 | 214 | | |
206 | 215 | | |
207 | 216 | | |
208 | 217 | | |
209 | 218 | | |
210 | | - | |
| 219 | + | |
| 220 | + | |
211 | 221 | | |
212 | 222 | | |
213 | 223 | | |
| |||
226 | 236 | | |
227 | 237 | | |
228 | 238 | | |
229 | | - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
230 | 242 | | |
231 | | - | |
| 243 | + | |
232 | 244 | | |
233 | 245 | | |
234 | 246 | | |
235 | 247 | | |
236 | | - | |
| 248 | + | |
237 | 249 | | |
238 | 250 | | |
239 | 251 | | |
240 | 252 | | |
241 | 253 | | |
242 | 254 | | |
243 | 255 | | |
244 | | - | |
| 256 | + | |
| 257 | + | |
245 | 258 | | |
246 | 259 | | |
247 | | - | |
| 260 | + | |
248 | 261 | | |
249 | 262 | | |
250 | 263 | | |
251 | 264 | | |
252 | 265 | | |
253 | | - | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
254 | 269 | | |
255 | 270 | | |
256 | 271 | | |
257 | 272 | | |
258 | 273 | | |
259 | 274 | | |
260 | 275 | | |
261 | | - | |
| 276 | + | |
262 | 277 | | |
263 | 278 | | |
264 | 279 | | |
| |||
273 | 288 | | |
274 | 289 | | |
275 | 290 | | |
276 | | - | |
| 291 | + | |
277 | 292 | | |
278 | 293 | | |
279 | 294 | | |
| |||
Lines changed: 49 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
192 | 192 | | |
193 | 193 | | |
194 | 194 | | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
195 | 244 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 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 | + | |
3 | 38 | | |
4 | 39 | | |
5 | 40 | | |
| |||
0 commit comments